[NDS32] new attribute no_prologue and new option -mret-in-naked-func.
[official-gcc.git] / gcc / ada / sem_aggr.adb
blob5eaf4622d80bd32379891d6f1acc712a8baa8e7c
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. Ditto if the context is an
1072 -- initialization procedure where a component may have a predicate
1073 -- function that carries the base type.
1075 -- Note that there is no node for Explicit_Actual_Parameter.
1076 -- To test for this context we therefore have to test for node
1077 -- N_Parameter_Association which itself appears only if there is a
1078 -- formal parameter. Consequently we also need to test for
1079 -- N_Procedure_Call_Statement or N_Function_Call.
1081 -- The context may be an N_Reference node, created by expansion.
1082 -- Legality of the others clause was established in the source,
1083 -- so the context is legal.
1085 Set_Etype (N, Aggr_Typ); -- May be overridden later on
1087 if Pkind = N_Assignment_Statement
1088 or else Inside_Init_Proc
1089 or else (Is_Constrained (Typ)
1090 and then
1091 (Pkind = N_Parameter_Association or else
1092 Pkind = N_Function_Call or else
1093 Pkind = N_Procedure_Call_Statement or else
1094 Pkind = N_Generic_Association or else
1095 Pkind = N_Formal_Object_Declaration or else
1096 Pkind = N_Simple_Return_Statement or else
1097 Pkind = N_Object_Declaration or else
1098 Pkind = N_Component_Declaration or else
1099 Pkind = N_Parameter_Specification or else
1100 Pkind = N_Qualified_Expression or else
1101 Pkind = N_Reference or else
1102 Pkind = N_Aggregate or else
1103 Pkind = N_Extension_Aggregate or else
1104 Pkind = N_Component_Association))
1105 then
1106 Aggr_Resolved :=
1107 Resolve_Array_Aggregate
1109 Index => First_Index (Aggr_Typ),
1110 Index_Constr => First_Index (Typ),
1111 Component_Typ => Component_Type (Typ),
1112 Others_Allowed => True);
1113 else
1114 Aggr_Resolved :=
1115 Resolve_Array_Aggregate
1117 Index => First_Index (Aggr_Typ),
1118 Index_Constr => First_Index (Aggr_Typ),
1119 Component_Typ => Component_Type (Typ),
1120 Others_Allowed => False);
1121 end if;
1123 if not Aggr_Resolved then
1125 -- A parenthesized expression may have been intended as an
1126 -- aggregate, leading to a type error when analyzing the
1127 -- component. This can also happen for a nested component
1128 -- (see Analyze_Aggr_Expr).
1130 if Paren_Count (N) > 0 then
1131 Error_Msg_N
1132 ("positional aggregate cannot have one component", N);
1133 end if;
1135 Aggr_Subtyp := Any_Composite;
1137 else
1138 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1139 end if;
1141 Set_Etype (N, Aggr_Subtyp);
1142 end Array_Aggregate;
1144 elsif Is_Private_Type (Typ)
1145 and then Present (Full_View (Typ))
1146 and then (In_Inlined_Body or In_Instance_Body)
1147 and then Is_Composite_Type (Full_View (Typ))
1148 then
1149 Resolve (N, Full_View (Typ));
1151 else
1152 Error_Msg_N ("illegal context for aggregate", N);
1153 end if;
1155 -- If we can determine statically that the evaluation of the aggregate
1156 -- raises Constraint_Error, then replace the aggregate with an
1157 -- N_Raise_Constraint_Error node, but set the Etype to the right
1158 -- aggregate subtype. Gigi needs this.
1160 if Raises_Constraint_Error (N) then
1161 Aggr_Subtyp := Etype (N);
1162 Rewrite (N,
1163 Make_Raise_Constraint_Error (Loc, Reason => CE_Range_Check_Failed));
1164 Set_Raises_Constraint_Error (N);
1165 Set_Etype (N, Aggr_Subtyp);
1166 Set_Analyzed (N);
1167 end if;
1169 Check_Function_Writable_Actuals (N);
1170 end Resolve_Aggregate;
1172 -----------------------------
1173 -- Resolve_Array_Aggregate --
1174 -----------------------------
1176 function Resolve_Array_Aggregate
1177 (N : Node_Id;
1178 Index : Node_Id;
1179 Index_Constr : Node_Id;
1180 Component_Typ : Entity_Id;
1181 Others_Allowed : Boolean) return Boolean
1183 Loc : constant Source_Ptr := Sloc (N);
1185 Failure : constant Boolean := False;
1186 Success : constant Boolean := True;
1188 Index_Typ : constant Entity_Id := Etype (Index);
1189 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1190 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1191 -- The type of the index corresponding to the array sub-aggregate along
1192 -- with its low and upper bounds.
1194 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1195 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1196 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1197 -- Ditto for the base type
1199 Others_Present : Boolean := False;
1201 Nb_Choices : Nat := 0;
1202 -- Contains the overall number of named choices in this sub-aggregate
1204 function Add (Val : Uint; To : Node_Id) return Node_Id;
1205 -- Creates a new expression node where Val is added to expression To.
1206 -- Tries to constant fold whenever possible. To must be an already
1207 -- analyzed expression.
1209 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1210 -- Checks that AH (the upper bound of an array aggregate) is less than
1211 -- or equal to BH (the upper bound of the index base type). If the check
1212 -- fails, a warning is emitted, the Raises_Constraint_Error flag of N is
1213 -- set, and AH is replaced with a duplicate of BH.
1215 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1216 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1217 -- warning if not and sets the Raises_Constraint_Error flag in N.
1219 procedure Check_Length (L, H : Node_Id; Len : Uint);
1220 -- Checks that range L .. H contains at least Len elements. Emits a
1221 -- warning if not and sets the Raises_Constraint_Error flag in N.
1223 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1224 -- Returns True if range L .. H is dynamic or null
1226 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1227 -- Given expression node From, this routine sets OK to False if it
1228 -- cannot statically evaluate From. Otherwise it stores this static
1229 -- value into Value.
1231 function Resolve_Aggr_Expr
1232 (Expr : Node_Id;
1233 Single_Elmt : Boolean) return Boolean;
1234 -- Resolves aggregate expression Expr. Returns False if resolution
1235 -- fails. If Single_Elmt is set to False, the expression Expr may be
1236 -- used to initialize several array aggregate elements (this can happen
1237 -- for discrete choices such as "L .. H => Expr" or the OTHERS choice).
1238 -- In this event we do not resolve Expr unless expansion is disabled.
1239 -- To know why, see the DELAYED COMPONENT RESOLUTION note above.
1241 -- NOTE: In the case of "... => <>", we pass the in the
1242 -- N_Component_Association node as Expr, since there is no Expression in
1243 -- that case, and we need a Sloc for the error message.
1245 procedure Resolve_Iterated_Component_Association
1246 (N : Node_Id;
1247 Index_Typ : Entity_Id);
1248 -- For AI12-061
1250 ---------
1251 -- Add --
1252 ---------
1254 function Add (Val : Uint; To : Node_Id) return Node_Id is
1255 Expr_Pos : Node_Id;
1256 Expr : Node_Id;
1257 To_Pos : Node_Id;
1259 begin
1260 if Raises_Constraint_Error (To) then
1261 return To;
1262 end if;
1264 -- First test if we can do constant folding
1266 if Compile_Time_Known_Value (To)
1267 or else Nkind (To) = N_Integer_Literal
1268 then
1269 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1270 Set_Is_Static_Expression (Expr_Pos);
1271 Set_Etype (Expr_Pos, Etype (To));
1272 Set_Analyzed (Expr_Pos, Analyzed (To));
1274 if not Is_Enumeration_Type (Index_Typ) then
1275 Expr := Expr_Pos;
1277 -- If we are dealing with enumeration return
1278 -- Index_Typ'Val (Expr_Pos)
1280 else
1281 Expr :=
1282 Make_Attribute_Reference
1283 (Loc,
1284 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1285 Attribute_Name => Name_Val,
1286 Expressions => New_List (Expr_Pos));
1287 end if;
1289 return Expr;
1290 end if;
1292 -- If we are here no constant folding possible
1294 if not Is_Enumeration_Type (Index_Base) then
1295 Expr :=
1296 Make_Op_Add (Loc,
1297 Left_Opnd => Duplicate_Subexpr (To),
1298 Right_Opnd => Make_Integer_Literal (Loc, Val));
1300 -- If we are dealing with enumeration return
1301 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1303 else
1304 To_Pos :=
1305 Make_Attribute_Reference
1306 (Loc,
1307 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1308 Attribute_Name => Name_Pos,
1309 Expressions => New_List (Duplicate_Subexpr (To)));
1311 Expr_Pos :=
1312 Make_Op_Add (Loc,
1313 Left_Opnd => To_Pos,
1314 Right_Opnd => Make_Integer_Literal (Loc, Val));
1316 Expr :=
1317 Make_Attribute_Reference
1318 (Loc,
1319 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1320 Attribute_Name => Name_Val,
1321 Expressions => New_List (Expr_Pos));
1323 -- If the index type has a non standard representation, the
1324 -- attributes 'Val and 'Pos expand into function calls and the
1325 -- resulting expression is considered non-safe for reevaluation
1326 -- by the backend. Relocate it into a constant temporary in order
1327 -- to make it safe for reevaluation.
1329 if Has_Non_Standard_Rep (Etype (N)) then
1330 declare
1331 Def_Id : Entity_Id;
1333 begin
1334 Def_Id := Make_Temporary (Loc, 'R', Expr);
1335 Set_Etype (Def_Id, Index_Typ);
1336 Insert_Action (N,
1337 Make_Object_Declaration (Loc,
1338 Defining_Identifier => Def_Id,
1339 Object_Definition =>
1340 New_Occurrence_Of (Index_Typ, Loc),
1341 Constant_Present => True,
1342 Expression => Relocate_Node (Expr)));
1344 Expr := New_Occurrence_Of (Def_Id, Loc);
1345 end;
1346 end if;
1347 end if;
1349 return Expr;
1350 end Add;
1352 -----------------
1353 -- Check_Bound --
1354 -----------------
1356 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1357 Val_BH : Uint;
1358 Val_AH : Uint;
1360 OK_BH : Boolean;
1361 OK_AH : Boolean;
1363 begin
1364 Get (Value => Val_BH, From => BH, OK => OK_BH);
1365 Get (Value => Val_AH, From => AH, OK => OK_AH);
1367 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1368 Set_Raises_Constraint_Error (N);
1369 Error_Msg_Warn := SPARK_Mode /= On;
1370 Error_Msg_N ("upper bound out of range<<", AH);
1371 Error_Msg_N ("\Constraint_Error [<<", AH);
1373 -- You need to set AH to BH or else in the case of enumerations
1374 -- indexes we will not be able to resolve the aggregate bounds.
1376 AH := Duplicate_Subexpr (BH);
1377 end if;
1378 end Check_Bound;
1380 ------------------
1381 -- Check_Bounds --
1382 ------------------
1384 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1385 Val_L : Uint;
1386 Val_H : Uint;
1387 Val_AL : Uint;
1388 Val_AH : Uint;
1390 OK_L : Boolean;
1391 OK_H : Boolean;
1393 OK_AL : Boolean;
1394 OK_AH : Boolean;
1395 pragma Warnings (Off, OK_AL);
1396 pragma Warnings (Off, OK_AH);
1398 begin
1399 if Raises_Constraint_Error (N)
1400 or else Dynamic_Or_Null_Range (AL, AH)
1401 then
1402 return;
1403 end if;
1405 Get (Value => Val_L, From => L, OK => OK_L);
1406 Get (Value => Val_H, From => H, OK => OK_H);
1408 Get (Value => Val_AL, From => AL, OK => OK_AL);
1409 Get (Value => Val_AH, From => AH, OK => OK_AH);
1411 if OK_L and then Val_L > Val_AL then
1412 Set_Raises_Constraint_Error (N);
1413 Error_Msg_Warn := SPARK_Mode /= On;
1414 Error_Msg_N ("lower bound of aggregate out of range<<", N);
1415 Error_Msg_N ("\Constraint_Error [<<", N);
1416 end if;
1418 if OK_H and then Val_H < Val_AH then
1419 Set_Raises_Constraint_Error (N);
1420 Error_Msg_Warn := SPARK_Mode /= On;
1421 Error_Msg_N ("upper bound of aggregate out of range<<", N);
1422 Error_Msg_N ("\Constraint_Error [<<", N);
1423 end if;
1424 end Check_Bounds;
1426 ------------------
1427 -- Check_Length --
1428 ------------------
1430 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1431 Val_L : Uint;
1432 Val_H : Uint;
1434 OK_L : Boolean;
1435 OK_H : Boolean;
1437 Range_Len : Uint;
1439 begin
1440 if Raises_Constraint_Error (N) then
1441 return;
1442 end if;
1444 Get (Value => Val_L, From => L, OK => OK_L);
1445 Get (Value => Val_H, From => H, OK => OK_H);
1447 if not OK_L or else not OK_H then
1448 return;
1449 end if;
1451 -- If null range length is zero
1453 if Val_L > Val_H then
1454 Range_Len := Uint_0;
1455 else
1456 Range_Len := Val_H - Val_L + 1;
1457 end if;
1459 if Range_Len < Len then
1460 Set_Raises_Constraint_Error (N);
1461 Error_Msg_Warn := SPARK_Mode /= On;
1462 Error_Msg_N ("too many elements<<", N);
1463 Error_Msg_N ("\Constraint_Error [<<", N);
1464 end if;
1465 end Check_Length;
1467 ---------------------------
1468 -- Dynamic_Or_Null_Range --
1469 ---------------------------
1471 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1472 Val_L : Uint;
1473 Val_H : Uint;
1475 OK_L : Boolean;
1476 OK_H : Boolean;
1478 begin
1479 Get (Value => Val_L, From => L, OK => OK_L);
1480 Get (Value => Val_H, From => H, OK => OK_H);
1482 return not OK_L or else not OK_H
1483 or else not Is_OK_Static_Expression (L)
1484 or else not Is_OK_Static_Expression (H)
1485 or else Val_L > Val_H;
1486 end Dynamic_Or_Null_Range;
1488 ---------
1489 -- Get --
1490 ---------
1492 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1493 begin
1494 OK := True;
1496 if Compile_Time_Known_Value (From) then
1497 Value := Expr_Value (From);
1499 -- If expression From is something like Some_Type'Val (10) then
1500 -- Value = 10.
1502 elsif Nkind (From) = N_Attribute_Reference
1503 and then Attribute_Name (From) = Name_Val
1504 and then Compile_Time_Known_Value (First (Expressions (From)))
1505 then
1506 Value := Expr_Value (First (Expressions (From)));
1507 else
1508 Value := Uint_0;
1509 OK := False;
1510 end if;
1511 end Get;
1513 -----------------------
1514 -- Resolve_Aggr_Expr --
1515 -----------------------
1517 function Resolve_Aggr_Expr
1518 (Expr : Node_Id;
1519 Single_Elmt : Boolean) return Boolean
1521 Nxt_Ind : constant Node_Id := Next_Index (Index);
1522 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
1523 -- Index is the current index corresponding to the expression
1525 Resolution_OK : Boolean := True;
1526 -- Set to False if resolution of the expression failed
1528 begin
1529 -- Defend against previous errors
1531 if Nkind (Expr) = N_Error
1532 or else Error_Posted (Expr)
1533 then
1534 return True;
1535 end if;
1537 -- If the array type against which we are resolving the aggregate
1538 -- has several dimensions, the expressions nested inside the
1539 -- aggregate must be further aggregates (or strings).
1541 if Present (Nxt_Ind) then
1542 if Nkind (Expr) /= N_Aggregate then
1544 -- A string literal can appear where a one-dimensional array
1545 -- of characters is expected. If the literal looks like an
1546 -- operator, it is still an operator symbol, which will be
1547 -- transformed into a string when analyzed.
1549 if Is_Character_Type (Component_Typ)
1550 and then No (Next_Index (Nxt_Ind))
1551 and then Nkind_In (Expr, N_String_Literal, N_Operator_Symbol)
1552 then
1553 -- A string literal used in a multidimensional array
1554 -- aggregate in place of the final one-dimensional
1555 -- aggregate must not be enclosed in parentheses.
1557 if Paren_Count (Expr) /= 0 then
1558 Error_Msg_N ("no parenthesis allowed here", Expr);
1559 end if;
1561 Make_String_Into_Aggregate (Expr);
1563 else
1564 Error_Msg_N ("nested array aggregate expected", Expr);
1566 -- If the expression is parenthesized, this may be
1567 -- a missing component association for a 1-aggregate.
1569 if Paren_Count (Expr) > 0 then
1570 Error_Msg_N
1571 ("\if single-component aggregate is intended, "
1572 & "write e.g. (1 ='> ...)", Expr);
1573 end if;
1575 return Failure;
1576 end if;
1577 end if;
1579 -- If it's "... => <>", nothing to resolve
1581 if Nkind (Expr) = N_Component_Association then
1582 pragma Assert (Box_Present (Expr));
1583 return Success;
1584 end if;
1586 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
1587 -- Required to check the null-exclusion attribute (if present).
1588 -- This value may be overridden later on.
1590 Set_Etype (Expr, Etype (N));
1592 Resolution_OK := Resolve_Array_Aggregate
1593 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1595 else
1596 -- If it's "... => <>", nothing to resolve
1598 if Nkind (Expr) = N_Component_Association then
1599 pragma Assert (Box_Present (Expr));
1600 return Success;
1601 end if;
1603 -- Do not resolve the expressions of discrete or others choices
1604 -- unless the expression covers a single component, or the
1605 -- expander is inactive.
1607 -- In SPARK mode, expressions that can perform side effects will
1608 -- be recognized by the gnat2why back-end, and the whole
1609 -- subprogram will be ignored. So semantic analysis can be
1610 -- performed safely.
1612 if Single_Elmt
1613 or else not Expander_Active
1614 or else In_Spec_Expression
1615 then
1616 Analyze_And_Resolve (Expr, Component_Typ);
1617 Check_Expr_OK_In_Limited_Aggregate (Expr);
1618 Check_Non_Static_Context (Expr);
1619 Aggregate_Constraint_Checks (Expr, Component_Typ);
1620 Check_Unset_Reference (Expr);
1621 end if;
1622 end if;
1624 -- If an aggregate component has a type with predicates, an explicit
1625 -- predicate check must be applied, as for an assignment statement,
1626 -- because the aggegate might not be expanded into individual
1627 -- component assignments. If the expression covers several components
1628 -- the analysis and the predicate check take place later.
1630 if Present (Predicate_Function (Component_Typ))
1631 and then Analyzed (Expr)
1632 then
1633 Apply_Predicate_Check (Expr, Component_Typ);
1634 end if;
1636 if Raises_Constraint_Error (Expr)
1637 and then Nkind (Parent (Expr)) /= N_Component_Association
1638 then
1639 Set_Raises_Constraint_Error (N);
1640 end if;
1642 -- If the expression has been marked as requiring a range check,
1643 -- then generate it here. It's a bit odd to be generating such
1644 -- checks in the analyzer, but harmless since Generate_Range_Check
1645 -- does nothing (other than making sure Do_Range_Check is set) if
1646 -- the expander is not active.
1648 if Do_Range_Check (Expr) then
1649 Generate_Range_Check (Expr, Component_Typ, CE_Range_Check_Failed);
1650 end if;
1652 return Resolution_OK;
1653 end Resolve_Aggr_Expr;
1655 --------------------------------------------
1656 -- Resolve_Iterated_Component_Association --
1657 --------------------------------------------
1659 procedure Resolve_Iterated_Component_Association
1660 (N : Node_Id;
1661 Index_Typ : Entity_Id)
1663 Loc : constant Source_Ptr := Sloc (N);
1665 Choice : Node_Id;
1666 Dummy : Boolean;
1667 Ent : Entity_Id;
1668 Expr : Node_Id;
1669 Id : Entity_Id;
1671 begin
1672 Choice := First (Discrete_Choices (N));
1674 while Present (Choice) loop
1675 if Nkind (Choice) = N_Others_Choice then
1676 Others_Present := True;
1678 else
1679 Analyze (Choice);
1681 -- Choice can be a subtype name, a range, or an expression
1683 if Is_Entity_Name (Choice)
1684 and then Is_Type (Entity (Choice))
1685 and then Base_Type (Entity (Choice)) = Base_Type (Index_Typ)
1686 then
1687 null;
1689 else
1690 Analyze_And_Resolve (Choice, Index_Typ);
1691 end if;
1692 end if;
1694 Next (Choice);
1695 end loop;
1697 -- Create a scope in which to introduce an index, which is usually
1698 -- visible in the expression for the component, and needed for its
1699 -- analysis.
1701 Ent := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L');
1702 Set_Etype (Ent, Standard_Void_Type);
1703 Set_Parent (Ent, Parent (N));
1704 Push_Scope (Ent);
1705 Id :=
1706 Make_Defining_Identifier (Loc,
1707 Chars => Chars (Defining_Identifier (N)));
1709 -- Insert and decorate the index variable in the current scope.
1710 -- The expression has to be analyzed once the index variable is
1711 -- directly visible. Mark the variable as referenced to prevent
1712 -- spurious warnings, given that subsequent uses of its name in the
1713 -- expression will reference the internal (synonym) loop variable.
1715 Enter_Name (Id);
1716 Set_Etype (Id, Index_Typ);
1717 Set_Ekind (Id, E_Variable);
1718 Set_Scope (Id, Ent);
1719 Set_Referenced (Id);
1721 -- Analyze a copy of the expression, to verify legality. We use
1722 -- a copy because the expression will be analyzed anew when the
1723 -- enclosing aggregate is expanded, and the construct is rewritten
1724 -- as a loop with a new index variable.
1726 Expr := New_Copy_Tree (Expression (N));
1727 Dummy := Resolve_Aggr_Expr (Expr, False);
1729 -- An iterated_component_association may appear in a nested
1730 -- aggregate for a multidimensional structure: preserve the bounds
1731 -- computed for the expression, as well as the anonymous array
1732 -- type generated for it; both are needed during array expansion.
1733 -- This does not work for more than two levels of nesting. ???
1735 if Nkind (Expr) = N_Aggregate then
1736 Set_Aggregate_Bounds (Expression (N), Aggregate_Bounds (Expr));
1737 Set_Etype (Expression (N), Etype (Expr));
1738 end if;
1740 End_Scope;
1741 end Resolve_Iterated_Component_Association;
1743 -- Local variables
1745 Assoc : Node_Id;
1746 Choice : Node_Id;
1747 Expr : Node_Id;
1748 Discard : Node_Id;
1750 Aggr_Low : Node_Id := Empty;
1751 Aggr_High : Node_Id := Empty;
1752 -- The actual low and high bounds of this sub-aggregate
1754 Case_Table_Size : Nat;
1755 -- Contains the size of the case table needed to sort aggregate choices
1757 Choices_Low : Node_Id := Empty;
1758 Choices_High : Node_Id := Empty;
1759 -- The lowest and highest discrete choices values for a named aggregate
1761 Delete_Choice : Boolean;
1762 -- Used when replacing a subtype choice with predicate by a list
1764 Nb_Elements : Uint := Uint_0;
1765 -- The number of elements in a positional aggregate
1767 Nb_Discrete_Choices : Nat := 0;
1768 -- The overall number of discrete choices (not counting others choice)
1770 -- Start of processing for Resolve_Array_Aggregate
1772 begin
1773 -- Ignore junk empty aggregate resulting from parser error
1775 if No (Expressions (N))
1776 and then No (Component_Associations (N))
1777 and then not Null_Record_Present (N)
1778 then
1779 return False;
1780 end if;
1782 -- STEP 1: make sure the aggregate is correctly formatted
1784 if Present (Component_Associations (N)) then
1785 Assoc := First (Component_Associations (N));
1786 while Present (Assoc) loop
1787 if Nkind (Assoc) = N_Iterated_Component_Association then
1788 Resolve_Iterated_Component_Association (Assoc, Index_Typ);
1789 end if;
1791 Choice := First (Choice_List (Assoc));
1792 Delete_Choice := False;
1793 while Present (Choice) loop
1794 if Nkind (Choice) = N_Others_Choice then
1795 Others_Present := True;
1797 if Choice /= First (Choice_List (Assoc))
1798 or else Present (Next (Choice))
1799 then
1800 Error_Msg_N
1801 ("OTHERS must appear alone in a choice list", Choice);
1802 return Failure;
1803 end if;
1805 if Present (Next (Assoc)) then
1806 Error_Msg_N
1807 ("OTHERS must appear last in an aggregate", Choice);
1808 return Failure;
1809 end if;
1811 if Ada_Version = Ada_83
1812 and then Assoc /= First (Component_Associations (N))
1813 and then Nkind_In (Parent (N), N_Assignment_Statement,
1814 N_Object_Declaration)
1815 then
1816 Error_Msg_N
1817 ("(Ada 83) illegal context for OTHERS choice", N);
1818 end if;
1820 elsif Is_Entity_Name (Choice) then
1821 Analyze (Choice);
1823 declare
1824 E : constant Entity_Id := Entity (Choice);
1825 New_Cs : List_Id;
1826 P : Node_Id;
1827 C : Node_Id;
1829 begin
1830 if Is_Type (E) and then Has_Predicates (E) then
1831 Freeze_Before (N, E);
1833 if Has_Dynamic_Predicate_Aspect (E) then
1834 Error_Msg_NE
1835 ("subtype& has dynamic predicate, not allowed "
1836 & "in aggregate choice", Choice, E);
1838 elsif not Is_OK_Static_Subtype (E) then
1839 Error_Msg_NE
1840 ("non-static subtype& has predicate, not allowed "
1841 & "in aggregate choice", Choice, E);
1842 end if;
1844 -- If the subtype has a static predicate, replace the
1845 -- original choice with the list of individual values
1846 -- covered by the predicate. Do not perform this
1847 -- transformation if we need to preserve the source
1848 -- for ASIS use.
1849 -- This should be deferred to expansion time ???
1851 if Present (Static_Discrete_Predicate (E))
1852 and then not ASIS_Mode
1853 then
1854 Delete_Choice := True;
1856 New_Cs := New_List;
1857 P := First (Static_Discrete_Predicate (E));
1858 while Present (P) loop
1859 C := New_Copy (P);
1860 Set_Sloc (C, Sloc (Choice));
1861 Append_To (New_Cs, C);
1862 Next (P);
1863 end loop;
1865 Insert_List_After (Choice, New_Cs);
1866 end if;
1867 end if;
1868 end;
1869 end if;
1871 Nb_Choices := Nb_Choices + 1;
1873 declare
1874 C : constant Node_Id := Choice;
1876 begin
1877 Next (Choice);
1879 if Delete_Choice then
1880 Remove (C);
1881 Nb_Choices := Nb_Choices - 1;
1882 Delete_Choice := False;
1883 end if;
1884 end;
1885 end loop;
1887 Next (Assoc);
1888 end loop;
1889 end if;
1891 -- At this point we know that the others choice, if present, is by
1892 -- itself and appears last in the aggregate. Check if we have mixed
1893 -- positional and discrete associations (other than the others choice).
1895 if Present (Expressions (N))
1896 and then (Nb_Choices > 1
1897 or else (Nb_Choices = 1 and then not Others_Present))
1898 then
1899 Error_Msg_N
1900 ("named association cannot follow positional association",
1901 First (Choice_List (First (Component_Associations (N)))));
1902 return Failure;
1903 end if;
1905 -- Test for the validity of an others choice if present
1907 if Others_Present and then not Others_Allowed then
1908 Error_Msg_N
1909 ("OTHERS choice not allowed here",
1910 First (Choices (First (Component_Associations (N)))));
1911 return Failure;
1912 end if;
1914 -- Protect against cascaded errors
1916 if Etype (Index_Typ) = Any_Type then
1917 return Failure;
1918 end if;
1920 -- STEP 2: Process named components
1922 if No (Expressions (N)) then
1923 if Others_Present then
1924 Case_Table_Size := Nb_Choices - 1;
1925 else
1926 Case_Table_Size := Nb_Choices;
1927 end if;
1929 Step_2 : declare
1930 function Empty_Range (A : Node_Id) return Boolean;
1931 -- If an association covers an empty range, some warnings on the
1932 -- expression of the association can be disabled.
1934 -----------------
1935 -- Empty_Range --
1936 -----------------
1938 function Empty_Range (A : Node_Id) return Boolean is
1939 R : constant Node_Id := First (Choices (A));
1940 begin
1941 return No (Next (R))
1942 and then Nkind (R) = N_Range
1943 and then Compile_Time_Compare
1944 (Low_Bound (R), High_Bound (R), False) = GT;
1945 end Empty_Range;
1947 -- Local variables
1949 Low : Node_Id;
1950 High : Node_Id;
1951 -- Denote the lowest and highest values in an aggregate choice
1953 S_Low : Node_Id := Empty;
1954 S_High : Node_Id := Empty;
1955 -- if a choice in an aggregate is a subtype indication these
1956 -- denote the lowest and highest values of the subtype
1958 Table : Case_Table_Type (0 .. Case_Table_Size);
1959 -- Used to sort all the different choice values. Entry zero is
1960 -- reserved for sorting purposes.
1962 Single_Choice : Boolean;
1963 -- Set to true every time there is a single discrete choice in a
1964 -- discrete association
1966 Prev_Nb_Discrete_Choices : Nat;
1967 -- Used to keep track of the number of discrete choices in the
1968 -- current association.
1970 Errors_Posted_On_Choices : Boolean := False;
1971 -- Keeps track of whether any choices have semantic errors
1973 -- Start of processing for Step_2
1975 begin
1976 -- STEP 2 (A): Check discrete choices validity
1978 Assoc := First (Component_Associations (N));
1979 while Present (Assoc) loop
1980 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
1981 Choice := First (Choice_List (Assoc));
1983 loop
1984 Analyze (Choice);
1986 if Nkind (Choice) = N_Others_Choice then
1987 Single_Choice := False;
1988 exit;
1990 -- Test for subtype mark without constraint
1992 elsif Is_Entity_Name (Choice) and then
1993 Is_Type (Entity (Choice))
1994 then
1995 if Base_Type (Entity (Choice)) /= Index_Base then
1996 Error_Msg_N
1997 ("invalid subtype mark in aggregate choice",
1998 Choice);
1999 return Failure;
2000 end if;
2002 -- Case of subtype indication
2004 elsif Nkind (Choice) = N_Subtype_Indication then
2005 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
2007 if Has_Dynamic_Predicate_Aspect
2008 (Entity (Subtype_Mark (Choice)))
2009 then
2010 Error_Msg_NE
2011 ("subtype& has dynamic predicate, "
2012 & "not allowed in aggregate choice",
2013 Choice, Entity (Subtype_Mark (Choice)));
2014 end if;
2016 -- Does the subtype indication evaluation raise CE?
2018 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
2019 Get_Index_Bounds (Choice, Low, High);
2020 Check_Bounds (S_Low, S_High, Low, High);
2022 -- Case of range or expression
2024 else
2025 Resolve (Choice, Index_Base);
2026 Check_Unset_Reference (Choice);
2027 Check_Non_Static_Context (Choice);
2029 -- If semantic errors were posted on the choice, then
2030 -- record that for possible early return from later
2031 -- processing (see handling of enumeration choices).
2033 if Error_Posted (Choice) then
2034 Errors_Posted_On_Choices := True;
2035 end if;
2037 -- Do not range check a choice. This check is redundant
2038 -- since this test is already done when we check that the
2039 -- bounds of the array aggregate are within range.
2041 Set_Do_Range_Check (Choice, False);
2043 -- In SPARK, the choice must be static
2045 if not (Is_OK_Static_Expression (Choice)
2046 or else (Nkind (Choice) = N_Range
2047 and then Is_OK_Static_Range (Choice)))
2048 then
2049 Check_SPARK_05_Restriction
2050 ("choice should be static", Choice);
2051 end if;
2052 end if;
2054 -- If we could not resolve the discrete choice stop here
2056 if Etype (Choice) = Any_Type then
2057 return Failure;
2059 -- If the discrete choice raises CE get its original bounds
2061 elsif Nkind (Choice) = N_Raise_Constraint_Error then
2062 Set_Raises_Constraint_Error (N);
2063 Get_Index_Bounds (Original_Node (Choice), Low, High);
2065 -- Otherwise get its bounds as usual
2067 else
2068 Get_Index_Bounds (Choice, Low, High);
2069 end if;
2071 if (Dynamic_Or_Null_Range (Low, High)
2072 or else (Nkind (Choice) = N_Subtype_Indication
2073 and then
2074 Dynamic_Or_Null_Range (S_Low, S_High)))
2075 and then Nb_Choices /= 1
2076 then
2077 Error_Msg_N
2078 ("dynamic or empty choice in aggregate "
2079 & "must be the only choice", Choice);
2080 return Failure;
2081 end if;
2083 if not (All_Composite_Constraints_Static (Low)
2084 and then All_Composite_Constraints_Static (High)
2085 and then All_Composite_Constraints_Static (S_Low)
2086 and then All_Composite_Constraints_Static (S_High))
2087 then
2088 Check_Restriction (No_Dynamic_Sized_Objects, Choice);
2089 end if;
2091 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
2092 Table (Nb_Discrete_Choices).Lo := Low;
2093 Table (Nb_Discrete_Choices).Hi := High;
2094 Table (Nb_Discrete_Choices).Choice := Choice;
2096 Next (Choice);
2098 if No (Choice) then
2100 -- Check if we have a single discrete choice and whether
2101 -- this discrete choice specifies a single value.
2103 Single_Choice :=
2104 (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1)
2105 and then (Low = High);
2107 exit;
2108 end if;
2109 end loop;
2111 -- Ada 2005 (AI-231)
2113 if Ada_Version >= Ada_2005
2114 and then Known_Null (Expression (Assoc))
2115 and then not Empty_Range (Assoc)
2116 then
2117 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2118 end if;
2120 -- Ada 2005 (AI-287): In case of default initialized component
2121 -- we delay the resolution to the expansion phase.
2123 if Box_Present (Assoc) then
2125 -- Ada 2005 (AI-287): In case of default initialization of a
2126 -- component the expander will generate calls to the
2127 -- corresponding initialization subprogram. We need to call
2128 -- Resolve_Aggr_Expr to check the rules about
2129 -- dimensionality.
2131 if not Resolve_Aggr_Expr
2132 (Assoc, Single_Elmt => Single_Choice)
2133 then
2134 return Failure;
2135 end if;
2137 elsif Nkind (Assoc) = N_Iterated_Component_Association then
2138 null; -- handled above, in a loop context.
2140 elsif not Resolve_Aggr_Expr
2141 (Expression (Assoc), Single_Elmt => Single_Choice)
2142 then
2143 return Failure;
2145 -- Check incorrect use of dynamically tagged expression
2147 -- We differentiate here two cases because the expression may
2148 -- not be decorated. For example, the analysis and resolution
2149 -- of the expression associated with the others choice will be
2150 -- done later with the full aggregate. In such case we
2151 -- duplicate the expression tree to analyze the copy and
2152 -- perform the required check.
2154 elsif not Present (Etype (Expression (Assoc))) then
2155 declare
2156 Save_Analysis : constant Boolean := Full_Analysis;
2157 Expr : constant Node_Id :=
2158 New_Copy_Tree (Expression (Assoc));
2160 begin
2161 Expander_Mode_Save_And_Set (False);
2162 Full_Analysis := False;
2164 -- Analyze the expression, making sure it is properly
2165 -- attached to the tree before we do the analysis.
2167 Set_Parent (Expr, Parent (Expression (Assoc)));
2168 Analyze (Expr);
2170 -- Compute its dimensions now, rather than at the end of
2171 -- resolution, because in the case of multidimensional
2172 -- aggregates subsequent expansion may lead to spurious
2173 -- errors.
2175 Check_Expression_Dimensions (Expr, Component_Typ);
2177 -- If the expression is a literal, propagate this info
2178 -- to the expression in the association, to enable some
2179 -- optimizations downstream.
2181 if Is_Entity_Name (Expr)
2182 and then Present (Entity (Expr))
2183 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
2184 then
2185 Analyze_And_Resolve
2186 (Expression (Assoc), Component_Typ);
2187 end if;
2189 Full_Analysis := Save_Analysis;
2190 Expander_Mode_Restore;
2192 if Is_Tagged_Type (Etype (Expr)) then
2193 Check_Dynamically_Tagged_Expression
2194 (Expr => Expr,
2195 Typ => Component_Type (Etype (N)),
2196 Related_Nod => N);
2197 end if;
2198 end;
2200 elsif Is_Tagged_Type (Etype (Expression (Assoc))) then
2201 Check_Dynamically_Tagged_Expression
2202 (Expr => Expression (Assoc),
2203 Typ => Component_Type (Etype (N)),
2204 Related_Nod => N);
2205 end if;
2207 Next (Assoc);
2208 end loop;
2210 -- If aggregate contains more than one choice then these must be
2211 -- static. Check for duplicate and missing values.
2213 -- Note: there is duplicated code here wrt Check_Choice_Set in
2214 -- the body of Sem_Case, and it is possible we could just reuse
2215 -- that procedure. To be checked ???
2217 if Nb_Discrete_Choices > 1 then
2218 Check_Choices : declare
2219 Choice : Node_Id;
2220 -- Location of choice for messages
2222 Hi_Val : Uint;
2223 Lo_Val : Uint;
2224 -- High end of one range and Low end of the next. Should be
2225 -- contiguous if there is no hole in the list of values.
2227 Lo_Dup : Uint;
2228 Hi_Dup : Uint;
2229 -- End points of duplicated range
2231 Missing_Or_Duplicates : Boolean := False;
2232 -- Set True if missing or duplicate choices found
2234 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id);
2235 -- Output continuation message with a representation of the
2236 -- bounds (just Lo if Lo = Hi, else Lo .. Hi). C is the
2237 -- choice node where the message is to be posted.
2239 ------------------------
2240 -- Output_Bad_Choices --
2241 ------------------------
2243 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id) is
2244 begin
2245 -- Enumeration type case
2247 if Is_Enumeration_Type (Index_Typ) then
2248 Error_Msg_Name_1 :=
2249 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Lo, Loc));
2250 Error_Msg_Name_2 :=
2251 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Hi, Loc));
2253 if Lo = Hi then
2254 Error_Msg_N ("\\ %!", C);
2255 else
2256 Error_Msg_N ("\\ % .. %!", C);
2257 end if;
2259 -- Integer types case
2261 else
2262 Error_Msg_Uint_1 := Lo;
2263 Error_Msg_Uint_2 := Hi;
2265 if Lo = Hi then
2266 Error_Msg_N ("\\ ^!", C);
2267 else
2268 Error_Msg_N ("\\ ^ .. ^!", C);
2269 end if;
2270 end if;
2271 end Output_Bad_Choices;
2273 -- Start of processing for Check_Choices
2275 begin
2276 Sort_Case_Table (Table);
2278 -- First we do a quick linear loop to find out if we have
2279 -- any duplicates or missing entries (usually we have a
2280 -- legal aggregate, so this will get us out quickly).
2282 for J in 1 .. Nb_Discrete_Choices - 1 loop
2283 Hi_Val := Expr_Value (Table (J).Hi);
2284 Lo_Val := Expr_Value (Table (J + 1).Lo);
2286 if Lo_Val <= Hi_Val
2287 or else (Lo_Val > Hi_Val + 1
2288 and then not Others_Present)
2289 then
2290 Missing_Or_Duplicates := True;
2291 exit;
2292 end if;
2293 end loop;
2295 -- If we have missing or duplicate entries, first fill in
2296 -- the Highest entries to make life easier in the following
2297 -- loops to detect bad entries.
2299 if Missing_Or_Duplicates then
2300 Table (1).Highest := Expr_Value (Table (1).Hi);
2302 for J in 2 .. Nb_Discrete_Choices loop
2303 Table (J).Highest :=
2304 UI_Max
2305 (Table (J - 1).Highest, Expr_Value (Table (J).Hi));
2306 end loop;
2308 -- Loop through table entries to find duplicate indexes
2310 for J in 2 .. Nb_Discrete_Choices loop
2311 Lo_Val := Expr_Value (Table (J).Lo);
2312 Hi_Val := Expr_Value (Table (J).Hi);
2314 -- Case where we have duplicates (the lower bound of
2315 -- this choice is less than or equal to the highest
2316 -- high bound found so far).
2318 if Lo_Val <= Table (J - 1).Highest then
2320 -- We move backwards looking for duplicates. We can
2321 -- abandon this loop as soon as we reach a choice
2322 -- highest value that is less than Lo_Val.
2324 for K in reverse 1 .. J - 1 loop
2325 exit when Table (K).Highest < Lo_Val;
2327 -- Here we may have duplicates between entries
2328 -- for K and J. Get range of duplicates.
2330 Lo_Dup :=
2331 UI_Max (Lo_Val, Expr_Value (Table (K).Lo));
2332 Hi_Dup :=
2333 UI_Min (Hi_Val, Expr_Value (Table (K).Hi));
2335 -- Nothing to do if duplicate range is null
2337 if Lo_Dup > Hi_Dup then
2338 null;
2340 -- Otherwise place proper message. Because
2341 -- of the missing expansion of subtypes with
2342 -- predicates in ASIS mode, do not report
2343 -- spurious overlap errors.
2345 elsif ASIS_Mode
2346 and then
2347 ((Is_Type (Entity (Table (J).Choice))
2348 and then Has_Predicates
2349 (Entity (Table (J).Choice)))
2350 or else
2351 (Is_Type (Entity (Table (K).Choice))
2352 and then Has_Predicates
2353 (Entity (Table (K).Choice))))
2354 then
2355 null;
2357 else
2358 -- We place message on later choice, with a
2359 -- line reference to the earlier choice.
2361 if Sloc (Table (J).Choice) <
2362 Sloc (Table (K).Choice)
2363 then
2364 Choice := Table (K).Choice;
2365 Error_Msg_Sloc := Sloc (Table (J).Choice);
2366 else
2367 Choice := Table (J).Choice;
2368 Error_Msg_Sloc := Sloc (Table (K).Choice);
2369 end if;
2371 if Lo_Dup = Hi_Dup then
2372 Error_Msg_N
2373 ("index value in array aggregate "
2374 & "duplicates the one given#!", Choice);
2375 else
2376 Error_Msg_N
2377 ("index values in array aggregate "
2378 & "duplicate those given#!", Choice);
2379 end if;
2381 Output_Bad_Choices (Lo_Dup, Hi_Dup, Choice);
2382 end if;
2383 end loop;
2384 end if;
2385 end loop;
2387 -- Loop through entries in table to find missing indexes.
2388 -- Not needed if others, since missing impossible.
2390 if not Others_Present then
2391 for J in 2 .. Nb_Discrete_Choices loop
2392 Lo_Val := Expr_Value (Table (J).Lo);
2393 Hi_Val := Table (J - 1).Highest;
2395 if Lo_Val > Hi_Val + 1 then
2397 declare
2398 Error_Node : Node_Id;
2400 begin
2401 -- If the choice is the bound of a range in
2402 -- a subtype indication, it is not in the
2403 -- source lists for the aggregate itself, so
2404 -- post the error on the aggregate. Otherwise
2405 -- post it on choice itself.
2407 Choice := Table (J).Choice;
2409 if Is_List_Member (Choice) then
2410 Error_Node := Choice;
2411 else
2412 Error_Node := N;
2413 end if;
2415 if Hi_Val + 1 = Lo_Val - 1 then
2416 Error_Msg_N
2417 ("missing index value "
2418 & "in array aggregate!", Error_Node);
2419 else
2420 Error_Msg_N
2421 ("missing index values "
2422 & "in array aggregate!", Error_Node);
2423 end if;
2425 Output_Bad_Choices
2426 (Hi_Val + 1, Lo_Val - 1, Error_Node);
2427 end;
2428 end if;
2429 end loop;
2430 end if;
2432 -- If either missing or duplicate values, return failure
2434 Set_Etype (N, Any_Composite);
2435 return Failure;
2436 end if;
2437 end Check_Choices;
2438 end if;
2440 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
2442 if Nb_Discrete_Choices > 0 then
2443 Choices_Low := Table (1).Lo;
2444 Choices_High := Table (Nb_Discrete_Choices).Hi;
2445 end if;
2447 -- If Others is present, then bounds of aggregate come from the
2448 -- index constraint (not the choices in the aggregate itself).
2450 if Others_Present then
2451 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2453 -- Abandon processing if either bound is already signalled as
2454 -- an error (prevents junk cascaded messages and blow ups).
2456 if Nkind (Aggr_Low) = N_Error
2457 or else
2458 Nkind (Aggr_High) = N_Error
2459 then
2460 return False;
2461 end if;
2463 -- No others clause present
2465 else
2466 -- Special processing if others allowed and not present. This
2467 -- means that the bounds of the aggregate come from the index
2468 -- constraint (and the length must match).
2470 if Others_Allowed then
2471 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2473 -- Abandon processing if either bound is already signalled
2474 -- as an error (stop junk cascaded messages and blow ups).
2476 if Nkind (Aggr_Low) = N_Error
2477 or else
2478 Nkind (Aggr_High) = N_Error
2479 then
2480 return False;
2481 end if;
2483 -- If others allowed, and no others present, then the array
2484 -- should cover all index values. If it does not, we will
2485 -- get a length check warning, but there is two cases where
2486 -- an additional warning is useful:
2488 -- If we have no positional components, and the length is
2489 -- wrong (which we can tell by others being allowed with
2490 -- missing components), and the index type is an enumeration
2491 -- type, then issue appropriate warnings about these missing
2492 -- components. They are only warnings, since the aggregate
2493 -- is fine, it's just the wrong length. We skip this check
2494 -- for standard character types (since there are no literals
2495 -- and it is too much trouble to concoct them), and also if
2496 -- any of the bounds have values that are not known at
2497 -- compile time.
2499 -- Another case warranting a warning is when the length
2500 -- is right, but as above we have an index type that is
2501 -- an enumeration, and the bounds do not match. This is a
2502 -- case where dubious sliding is allowed and we generate a
2503 -- warning that the bounds do not match.
2505 if No (Expressions (N))
2506 and then Nkind (Index) = N_Range
2507 and then Is_Enumeration_Type (Etype (Index))
2508 and then not Is_Standard_Character_Type (Etype (Index))
2509 and then Compile_Time_Known_Value (Aggr_Low)
2510 and then Compile_Time_Known_Value (Aggr_High)
2511 and then Compile_Time_Known_Value (Choices_Low)
2512 and then Compile_Time_Known_Value (Choices_High)
2513 then
2514 -- If any of the expressions or range bounds in choices
2515 -- have semantic errors, then do not attempt further
2516 -- resolution, to prevent cascaded errors.
2518 if Errors_Posted_On_Choices then
2519 return Failure;
2520 end if;
2522 declare
2523 ALo : constant Node_Id := Expr_Value_E (Aggr_Low);
2524 AHi : constant Node_Id := Expr_Value_E (Aggr_High);
2525 CLo : constant Node_Id := Expr_Value_E (Choices_Low);
2526 CHi : constant Node_Id := Expr_Value_E (Choices_High);
2528 Ent : Entity_Id;
2530 begin
2531 -- Warning case 1, missing values at start/end. Only
2532 -- do the check if the number of entries is too small.
2534 if (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2536 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2537 then
2538 Error_Msg_N
2539 ("missing index value(s) in array aggregate??",
2542 -- Output missing value(s) at start
2544 if Chars (ALo) /= Chars (CLo) then
2545 Ent := Prev (CLo);
2547 if Chars (ALo) = Chars (Ent) then
2548 Error_Msg_Name_1 := Chars (ALo);
2549 Error_Msg_N ("\ %??", N);
2550 else
2551 Error_Msg_Name_1 := Chars (ALo);
2552 Error_Msg_Name_2 := Chars (Ent);
2553 Error_Msg_N ("\ % .. %??", N);
2554 end if;
2555 end if;
2557 -- Output missing value(s) at end
2559 if Chars (AHi) /= Chars (CHi) then
2560 Ent := Next (CHi);
2562 if Chars (AHi) = Chars (Ent) then
2563 Error_Msg_Name_1 := Chars (Ent);
2564 Error_Msg_N ("\ %??", N);
2565 else
2566 Error_Msg_Name_1 := Chars (Ent);
2567 Error_Msg_Name_2 := Chars (AHi);
2568 Error_Msg_N ("\ % .. %??", N);
2569 end if;
2570 end if;
2572 -- Warning case 2, dubious sliding. The First_Subtype
2573 -- test distinguishes between a constrained type where
2574 -- sliding is not allowed (so we will get a warning
2575 -- later that Constraint_Error will be raised), and
2576 -- the unconstrained case where sliding is permitted.
2578 elsif (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2580 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2581 and then Chars (ALo) /= Chars (CLo)
2582 and then
2583 not Is_Constrained (First_Subtype (Etype (N)))
2584 then
2585 Error_Msg_N
2586 ("bounds of aggregate do not match target??", N);
2587 end if;
2588 end;
2589 end if;
2590 end if;
2592 -- If no others, aggregate bounds come from aggregate
2594 Aggr_Low := Choices_Low;
2595 Aggr_High := Choices_High;
2596 end if;
2597 end Step_2;
2599 -- STEP 3: Process positional components
2601 else
2602 -- STEP 3 (A): Process positional elements
2604 Expr := First (Expressions (N));
2605 Nb_Elements := Uint_0;
2606 while Present (Expr) loop
2607 Nb_Elements := Nb_Elements + 1;
2609 -- Ada 2005 (AI-231)
2611 if Ada_Version >= Ada_2005 and then Known_Null (Expr) then
2612 Check_Can_Never_Be_Null (Etype (N), Expr);
2613 end if;
2615 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
2616 return Failure;
2617 end if;
2619 -- Check incorrect use of dynamically tagged expression
2621 if Is_Tagged_Type (Etype (Expr)) then
2622 Check_Dynamically_Tagged_Expression
2623 (Expr => Expr,
2624 Typ => Component_Type (Etype (N)),
2625 Related_Nod => N);
2626 end if;
2628 Next (Expr);
2629 end loop;
2631 if Others_Present then
2632 Assoc := Last (Component_Associations (N));
2634 -- Ada 2005 (AI-231)
2636 if Ada_Version >= Ada_2005 and then Known_Null (Assoc) then
2637 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2638 end if;
2640 -- Ada 2005 (AI-287): In case of default initialized component,
2641 -- we delay the resolution to the expansion phase.
2643 if Box_Present (Assoc) then
2645 -- Ada 2005 (AI-287): In case of default initialization of a
2646 -- component the expander will generate calls to the
2647 -- corresponding initialization subprogram. We need to call
2648 -- Resolve_Aggr_Expr to check the rules about
2649 -- dimensionality.
2651 if not Resolve_Aggr_Expr (Assoc, Single_Elmt => False) then
2652 return Failure;
2653 end if;
2655 elsif not Resolve_Aggr_Expr (Expression (Assoc),
2656 Single_Elmt => False)
2657 then
2658 return Failure;
2660 -- Check incorrect use of dynamically tagged expression. The
2661 -- expression of the others choice has not been resolved yet.
2662 -- In order to diagnose the semantic error we create a duplicate
2663 -- tree to analyze it and perform the check.
2665 else
2666 declare
2667 Save_Analysis : constant Boolean := Full_Analysis;
2668 Expr : constant Node_Id :=
2669 New_Copy_Tree (Expression (Assoc));
2671 begin
2672 Expander_Mode_Save_And_Set (False);
2673 Full_Analysis := False;
2674 Analyze (Expr);
2675 Full_Analysis := Save_Analysis;
2676 Expander_Mode_Restore;
2678 if Is_Tagged_Type (Etype (Expr)) then
2679 Check_Dynamically_Tagged_Expression
2680 (Expr => Expr,
2681 Typ => Component_Type (Etype (N)),
2682 Related_Nod => N);
2683 end if;
2684 end;
2685 end if;
2686 end if;
2688 -- STEP 3 (B): Compute the aggregate bounds
2690 if Others_Present then
2691 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2693 else
2694 if Others_Allowed then
2695 Get_Index_Bounds (Index_Constr, Aggr_Low, Discard);
2696 else
2697 Aggr_Low := Index_Typ_Low;
2698 end if;
2700 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
2701 Check_Bound (Index_Base_High, Aggr_High);
2702 end if;
2703 end if;
2705 -- STEP 4: Perform static aggregate checks and save the bounds
2707 -- Check (A)
2709 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
2710 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
2712 -- Check (B)
2714 if Others_Present and then Nb_Discrete_Choices > 0 then
2715 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
2716 Check_Bounds (Index_Typ_Low, Index_Typ_High,
2717 Choices_Low, Choices_High);
2718 Check_Bounds (Index_Base_Low, Index_Base_High,
2719 Choices_Low, Choices_High);
2721 -- Check (C)
2723 elsif Others_Present and then Nb_Elements > 0 then
2724 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
2725 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
2726 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
2727 end if;
2729 if Raises_Constraint_Error (Aggr_Low)
2730 or else Raises_Constraint_Error (Aggr_High)
2731 then
2732 Set_Raises_Constraint_Error (N);
2733 end if;
2735 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
2737 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
2738 -- since the addition node returned by Add is not yet analyzed. Attach
2739 -- to tree and analyze first. Reset analyzed flag to ensure it will get
2740 -- analyzed when it is a literal bound whose type must be properly set.
2742 if Others_Present or else Nb_Discrete_Choices > 0 then
2743 Aggr_High := Duplicate_Subexpr (Aggr_High);
2745 if Etype (Aggr_High) = Universal_Integer then
2746 Set_Analyzed (Aggr_High, False);
2747 end if;
2748 end if;
2750 -- If the aggregate already has bounds attached to it, it means this is
2751 -- a positional aggregate created as an optimization by
2752 -- Exp_Aggr.Convert_To_Positional, so we don't want to change those
2753 -- bounds.
2755 if Present (Aggregate_Bounds (N)) and then not Others_Allowed then
2756 Aggr_Low := Low_Bound (Aggregate_Bounds (N));
2757 Aggr_High := High_Bound (Aggregate_Bounds (N));
2758 end if;
2760 Set_Aggregate_Bounds
2761 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
2763 -- The bounds may contain expressions that must be inserted upwards.
2764 -- Attach them fully to the tree. After analysis, remove side effects
2765 -- from upper bound, if still needed.
2767 Set_Parent (Aggregate_Bounds (N), N);
2768 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
2769 Check_Unset_Reference (Aggregate_Bounds (N));
2771 if not Others_Present and then Nb_Discrete_Choices = 0 then
2772 Set_High_Bound
2773 (Aggregate_Bounds (N),
2774 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
2775 end if;
2777 -- Check the dimensions of each component in the array aggregate
2779 Analyze_Dimension_Array_Aggregate (N, Component_Typ);
2781 return Success;
2782 end Resolve_Array_Aggregate;
2784 -----------------------------
2785 -- Resolve_Delta_Aggregate --
2786 -----------------------------
2788 procedure Resolve_Delta_Aggregate (N : Node_Id; Typ : Entity_Id) is
2789 Base : constant Node_Id := Expression (N);
2791 begin
2792 if not Is_Composite_Type (Typ) then
2793 Error_Msg_N ("not a composite type", N);
2794 end if;
2796 Analyze_And_Resolve (Base, Typ);
2798 if Is_Array_Type (Typ) then
2799 Resolve_Delta_Array_Aggregate (N, Typ);
2800 else
2801 Resolve_Delta_Record_Aggregate (N, Typ);
2802 end if;
2804 Set_Etype (N, Typ);
2805 end Resolve_Delta_Aggregate;
2807 -----------------------------------
2808 -- Resolve_Delta_Array_Aggregate --
2809 -----------------------------------
2811 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id) is
2812 Deltas : constant List_Id := Component_Associations (N);
2814 Assoc : Node_Id;
2815 Choice : Node_Id;
2816 Index_Type : Entity_Id;
2818 begin
2819 Index_Type := Etype (First_Index (Typ));
2821 Assoc := First (Deltas);
2822 while Present (Assoc) loop
2823 if Nkind (Assoc) = N_Iterated_Component_Association then
2824 Choice := First (Choice_List (Assoc));
2825 while Present (Choice) loop
2826 if Nkind (Choice) = N_Others_Choice then
2827 Error_Msg_N
2828 ("others not allowed in delta aggregate", Choice);
2830 else
2831 Analyze_And_Resolve (Choice, Index_Type);
2832 end if;
2834 Next (Choice);
2835 end loop;
2837 declare
2838 Id : constant Entity_Id := Defining_Identifier (Assoc);
2839 Ent : constant Entity_Id :=
2840 New_Internal_Entity
2841 (E_Loop, Current_Scope, Sloc (Assoc), 'L');
2843 begin
2844 Set_Etype (Ent, Standard_Void_Type);
2845 Set_Parent (Ent, Assoc);
2847 if No (Scope (Id)) then
2848 Enter_Name (Id);
2849 Set_Etype (Id, Index_Type);
2850 Set_Ekind (Id, E_Variable);
2851 Set_Scope (Id, Ent);
2852 end if;
2854 Push_Scope (Ent);
2855 Analyze_And_Resolve
2856 (New_Copy_Tree (Expression (Assoc)), Component_Type (Typ));
2857 End_Scope;
2858 end;
2860 else
2861 Choice := First (Choice_List (Assoc));
2862 while Present (Choice) loop
2863 if Nkind (Choice) = N_Others_Choice then
2864 Error_Msg_N
2865 ("others not allowed in delta aggregate", Choice);
2867 else
2868 Analyze (Choice);
2870 if Is_Entity_Name (Choice)
2871 and then Is_Type (Entity (Choice))
2872 then
2873 -- Choice covers a range of values
2875 if Base_Type (Entity (Choice)) /=
2876 Base_Type (Index_Type)
2877 then
2878 Error_Msg_NE
2879 ("choice does mat match index type of",
2880 Choice, Typ);
2881 end if;
2882 else
2883 Resolve (Choice, Index_Type);
2884 end if;
2885 end if;
2887 Next (Choice);
2888 end loop;
2890 Analyze_And_Resolve (Expression (Assoc), Component_Type (Typ));
2891 end if;
2893 Next (Assoc);
2894 end loop;
2895 end Resolve_Delta_Array_Aggregate;
2897 ------------------------------------
2898 -- Resolve_Delta_Record_Aggregate --
2899 ------------------------------------
2901 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
2903 -- Variables used to verify that discriminant-dependent components
2904 -- appear in the same variant.
2906 Comp_Ref : Entity_Id := Empty; -- init to avoid warning
2907 Variant : Node_Id;
2909 procedure Check_Variant (Id : Entity_Id);
2910 -- If a given component of the delta aggregate appears in a variant
2911 -- part, verify that it is within the same variant as that of previous
2912 -- specified variant components of the delta.
2914 function Get_Component_Type (Nam : Node_Id) return Entity_Id;
2915 -- Locate component with a given name and return its type. If none found
2916 -- report error.
2918 function Nested_In (V1 : Node_Id; V2 : Node_Id) return Boolean;
2919 -- Determine whether variant V1 is within variant V2
2921 function Variant_Depth (N : Node_Id) return Integer;
2922 -- Determine the distance of a variant to the enclosing type
2923 -- declaration.
2925 --------------------
2926 -- Check_Variant --
2927 --------------------
2929 procedure Check_Variant (Id : Entity_Id) is
2930 Comp : Entity_Id;
2931 Comp_Variant : Node_Id;
2933 begin
2934 if not Has_Discriminants (Typ) then
2935 return;
2936 end if;
2938 Comp := First_Entity (Typ);
2939 while Present (Comp) loop
2940 exit when Chars (Comp) = Chars (Id);
2941 Next_Component (Comp);
2942 end loop;
2944 -- Find the variant, if any, whose component list includes the
2945 -- component declaration.
2947 Comp_Variant := Parent (Parent (List_Containing (Parent (Comp))));
2948 if Nkind (Comp_Variant) = N_Variant then
2949 if No (Variant) then
2950 Variant := Comp_Variant;
2951 Comp_Ref := Comp;
2953 elsif Variant /= Comp_Variant then
2954 declare
2955 D1 : constant Integer := Variant_Depth (Variant);
2956 D2 : constant Integer := Variant_Depth (Comp_Variant);
2958 begin
2959 if D1 = D2
2960 or else
2961 (D1 > D2 and then not Nested_In (Variant, Comp_Variant))
2962 or else
2963 (D2 > D1 and then not Nested_In (Comp_Variant, Variant))
2964 then
2965 pragma Assert (Present (Comp_Ref));
2966 Error_Msg_Node_2 := Comp_Ref;
2967 Error_Msg_NE
2968 ("& and & appear in different variants", Id, Comp);
2970 -- Otherwise retain the deeper variant for subsequent tests
2972 elsif D2 > D1 then
2973 Variant := Comp_Variant;
2974 end if;
2975 end;
2976 end if;
2977 end if;
2978 end Check_Variant;
2980 ------------------------
2981 -- Get_Component_Type --
2982 ------------------------
2984 function Get_Component_Type (Nam : Node_Id) return Entity_Id is
2985 Comp : Entity_Id;
2987 begin
2988 Comp := First_Entity (Typ);
2989 while Present (Comp) loop
2990 if Chars (Comp) = Chars (Nam) then
2991 if Ekind (Comp) = E_Discriminant then
2992 Error_Msg_N ("delta cannot apply to discriminant", Nam);
2993 end if;
2995 return Etype (Comp);
2996 end if;
2998 Comp := Next_Entity (Comp);
2999 end loop;
3001 Error_Msg_NE ("type& has no component with this name", Nam, Typ);
3002 return Any_Type;
3003 end Get_Component_Type;
3005 ---------------
3006 -- Nested_In --
3007 ---------------
3009 function Nested_In (V1, V2 : Node_Id) return Boolean is
3010 Par : Node_Id;
3012 begin
3013 Par := Parent (V1);
3014 while Nkind (Par) /= N_Full_Type_Declaration loop
3015 if Par = V2 then
3016 return True;
3017 end if;
3019 Par := Parent (Par);
3020 end loop;
3022 return False;
3023 end Nested_In;
3025 -------------------
3026 -- Variant_Depth --
3027 -------------------
3029 function Variant_Depth (N : Node_Id) return Integer is
3030 Depth : Integer;
3031 Par : Node_Id;
3033 begin
3034 Depth := 0;
3035 Par := Parent (N);
3036 while Nkind (Par) /= N_Full_Type_Declaration loop
3037 Depth := Depth + 1;
3038 Par := Parent (Par);
3039 end loop;
3041 return Depth;
3042 end Variant_Depth;
3044 -- Local variables
3046 Deltas : constant List_Id := Component_Associations (N);
3048 Assoc : Node_Id;
3049 Choice : Node_Id;
3050 Comp_Type : Entity_Id := Empty; -- init to avoid warning
3052 -- Start of processing for Resolve_Delta_Record_Aggregate
3054 begin
3055 Variant := Empty;
3057 Assoc := First (Deltas);
3058 while Present (Assoc) loop
3059 Choice := First (Choice_List (Assoc));
3060 while Present (Choice) loop
3061 Comp_Type := Get_Component_Type (Choice);
3063 if Comp_Type /= Any_Type then
3064 Check_Variant (Choice);
3065 end if;
3067 Next (Choice);
3068 end loop;
3070 pragma Assert (Present (Comp_Type));
3071 Analyze_And_Resolve (Expression (Assoc), Comp_Type);
3072 Next (Assoc);
3073 end loop;
3074 end Resolve_Delta_Record_Aggregate;
3076 ---------------------------------
3077 -- Resolve_Extension_Aggregate --
3078 ---------------------------------
3080 -- There are two cases to consider:
3082 -- a) If the ancestor part is a type mark, the components needed are the
3083 -- difference between the components of the expected type and the
3084 -- components of the given type mark.
3086 -- b) If the ancestor part is an expression, it must be unambiguous, and
3087 -- once we have its type we can also compute the needed components as in
3088 -- the previous case. In both cases, if the ancestor type is not the
3089 -- immediate ancestor, we have to build this ancestor recursively.
3091 -- In both cases, discriminants of the ancestor type do not play a role in
3092 -- the resolution of the needed components, because inherited discriminants
3093 -- cannot be used in a type extension. As a result we can compute
3094 -- independently the list of components of the ancestor type and of the
3095 -- expected type.
3097 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
3098 A : constant Node_Id := Ancestor_Part (N);
3099 A_Type : Entity_Id;
3100 I : Interp_Index;
3101 It : Interp;
3103 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean;
3104 -- If the type is limited, verify that the ancestor part is a legal
3105 -- expression (aggregate or function call, including 'Input)) that does
3106 -- not require a copy, as specified in 7.5(2).
3108 function Valid_Ancestor_Type return Boolean;
3109 -- Verify that the type of the ancestor part is a non-private ancestor
3110 -- of the expected type, which must be a type extension.
3112 procedure Transform_BIP_Assignment (Typ : Entity_Id);
3113 -- For an extension aggregate whose ancestor part is a build-in-place
3114 -- call returning a nonlimited type, this is used to transform the
3115 -- assignment to the ancestor part to use a temp.
3117 ----------------------------
3118 -- Valid_Limited_Ancestor --
3119 ----------------------------
3121 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean is
3122 begin
3123 if Is_Entity_Name (Anc) and then Is_Type (Entity (Anc)) then
3124 return True;
3126 -- The ancestor must be a call or an aggregate, but a call may
3127 -- have been expanded into a temporary, so check original node.
3129 elsif Nkind_In (Anc, N_Aggregate,
3130 N_Extension_Aggregate,
3131 N_Function_Call)
3132 then
3133 return True;
3135 elsif Nkind (Original_Node (Anc)) = N_Function_Call then
3136 return True;
3138 elsif Nkind (Anc) = N_Attribute_Reference
3139 and then Attribute_Name (Anc) = Name_Input
3140 then
3141 return True;
3143 elsif Nkind (Anc) = N_Qualified_Expression then
3144 return Valid_Limited_Ancestor (Expression (Anc));
3146 else
3147 return False;
3148 end if;
3149 end Valid_Limited_Ancestor;
3151 -------------------------
3152 -- Valid_Ancestor_Type --
3153 -------------------------
3155 function Valid_Ancestor_Type return Boolean is
3156 Imm_Type : Entity_Id;
3158 begin
3159 Imm_Type := Base_Type (Typ);
3160 while Is_Derived_Type (Imm_Type) loop
3161 if Etype (Imm_Type) = Base_Type (A_Type) then
3162 return True;
3164 -- The base type of the parent type may appear as a private
3165 -- extension if it is declared as such in a parent unit of the
3166 -- current one. For consistency of the subsequent analysis use
3167 -- the partial view for the ancestor part.
3169 elsif Is_Private_Type (Etype (Imm_Type))
3170 and then Present (Full_View (Etype (Imm_Type)))
3171 and then Base_Type (A_Type) = Full_View (Etype (Imm_Type))
3172 then
3173 A_Type := Etype (Imm_Type);
3174 return True;
3176 -- The parent type may be a private extension. The aggregate is
3177 -- legal if the type of the aggregate is an extension of it that
3178 -- is not a private extension.
3180 elsif Is_Private_Type (A_Type)
3181 and then not Is_Private_Type (Imm_Type)
3182 and then Present (Full_View (A_Type))
3183 and then Base_Type (Full_View (A_Type)) = Etype (Imm_Type)
3184 then
3185 return True;
3187 else
3188 Imm_Type := Etype (Base_Type (Imm_Type));
3189 end if;
3190 end loop;
3192 -- If previous loop did not find a proper ancestor, report error
3194 Error_Msg_NE ("expect ancestor type of &", A, Typ);
3195 return False;
3196 end Valid_Ancestor_Type;
3198 ------------------------------
3199 -- Transform_BIP_Assignment --
3200 ------------------------------
3202 procedure Transform_BIP_Assignment (Typ : Entity_Id) is
3203 Loc : constant Source_Ptr := Sloc (N);
3204 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'Y', A);
3205 Obj_Decl : constant Node_Id :=
3206 Make_Object_Declaration (Loc,
3207 Defining_Identifier => Def_Id,
3208 Constant_Present => True,
3209 Object_Definition => New_Occurrence_Of (Typ, Loc),
3210 Expression => A,
3211 Has_Init_Expression => True);
3212 begin
3213 Set_Etype (Def_Id, Typ);
3214 Set_Ancestor_Part (N, New_Occurrence_Of (Def_Id, Loc));
3215 Insert_Action (N, Obj_Decl);
3216 end Transform_BIP_Assignment;
3218 -- Start of processing for Resolve_Extension_Aggregate
3220 begin
3221 -- Analyze the ancestor part and account for the case where it is a
3222 -- parameterless function call.
3224 Analyze (A);
3225 Check_Parameterless_Call (A);
3227 -- In SPARK, the ancestor part cannot be a type mark
3229 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
3230 Check_SPARK_05_Restriction ("ancestor part cannot be a type mark", A);
3232 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
3233 -- must not have unknown discriminants.
3235 if Has_Unknown_Discriminants (Root_Type (Typ)) then
3236 Error_Msg_NE
3237 ("aggregate not available for type& whose ancestor "
3238 & "has unknown discriminants", N, Typ);
3239 end if;
3240 end if;
3242 if not Is_Tagged_Type (Typ) then
3243 Error_Msg_N ("type of extension aggregate must be tagged", N);
3244 return;
3246 elsif Is_Limited_Type (Typ) then
3248 -- Ada 2005 (AI-287): Limited aggregates are allowed
3250 if Ada_Version < Ada_2005 then
3251 Error_Msg_N ("aggregate type cannot be limited", N);
3252 Explain_Limited_Type (Typ, N);
3253 return;
3255 elsif Valid_Limited_Ancestor (A) then
3256 null;
3258 else
3259 Error_Msg_N
3260 ("limited ancestor part must be aggregate or function call", A);
3261 end if;
3263 elsif Is_Class_Wide_Type (Typ) then
3264 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
3265 return;
3266 end if;
3268 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
3269 A_Type := Get_Full_View (Entity (A));
3271 if Valid_Ancestor_Type then
3272 Set_Entity (A, A_Type);
3273 Set_Etype (A, A_Type);
3275 Validate_Ancestor_Part (N);
3276 Resolve_Record_Aggregate (N, Typ);
3277 end if;
3279 elsif Nkind (A) /= N_Aggregate then
3280 if Is_Overloaded (A) then
3281 A_Type := Any_Type;
3283 Get_First_Interp (A, I, It);
3284 while Present (It.Typ) loop
3286 -- Consider limited interpretations if Ada 2005 or higher
3288 if Is_Tagged_Type (It.Typ)
3289 and then (Ada_Version >= Ada_2005
3290 or else not Is_Limited_Type (It.Typ))
3291 then
3292 if A_Type /= Any_Type then
3293 Error_Msg_N ("cannot resolve expression", A);
3294 return;
3295 else
3296 A_Type := It.Typ;
3297 end if;
3298 end if;
3300 Get_Next_Interp (I, It);
3301 end loop;
3303 if A_Type = Any_Type then
3304 if Ada_Version >= Ada_2005 then
3305 Error_Msg_N
3306 ("ancestor part must be of a tagged type", A);
3307 else
3308 Error_Msg_N
3309 ("ancestor part must be of a nonlimited tagged type", A);
3310 end if;
3312 return;
3313 end if;
3315 else
3316 A_Type := Etype (A);
3317 end if;
3319 if Valid_Ancestor_Type then
3320 Resolve (A, A_Type);
3321 Check_Unset_Reference (A);
3322 Check_Non_Static_Context (A);
3324 -- The aggregate is illegal if the ancestor expression is a call
3325 -- to a function with a limited unconstrained result, unless the
3326 -- type of the aggregate is a null extension. This restriction
3327 -- was added in AI05-67 to simplify implementation.
3329 if Nkind (A) = N_Function_Call
3330 and then Is_Limited_Type (A_Type)
3331 and then not Is_Null_Extension (Typ)
3332 and then not Is_Constrained (A_Type)
3333 then
3334 Error_Msg_N
3335 ("type of limited ancestor part must be constrained", A);
3337 -- Reject the use of CPP constructors that leave objects partially
3338 -- initialized. For example:
3340 -- type CPP_Root is tagged limited record ...
3341 -- pragma Import (CPP, CPP_Root);
3343 -- type CPP_DT is new CPP_Root and Iface ...
3344 -- pragma Import (CPP, CPP_DT);
3346 -- type Ada_DT is new CPP_DT with ...
3348 -- Obj : Ada_DT := Ada_DT'(New_CPP_Root with others => <>);
3350 -- Using the constructor of CPP_Root the slots of the dispatch
3351 -- table of CPP_DT cannot be set, and the secondary tag of
3352 -- CPP_DT is unknown.
3354 elsif Nkind (A) = N_Function_Call
3355 and then Is_CPP_Constructor_Call (A)
3356 and then Enclosing_CPP_Parent (Typ) /= A_Type
3357 then
3358 Error_Msg_NE
3359 ("??must use 'C'P'P constructor for type &", A,
3360 Enclosing_CPP_Parent (Typ));
3362 -- The following call is not needed if the previous warning
3363 -- is promoted to an error.
3365 Resolve_Record_Aggregate (N, Typ);
3367 elsif Is_Class_Wide_Type (Etype (A))
3368 and then Nkind (Original_Node (A)) = N_Function_Call
3369 then
3370 -- If the ancestor part is a dispatching call, it appears
3371 -- statically to be a legal ancestor, but it yields any member
3372 -- of the class, and it is not possible to determine whether
3373 -- it is an ancestor of the extension aggregate (much less
3374 -- which ancestor). It is not possible to determine the
3375 -- components of the extension part.
3377 -- This check implements AI-306, which in fact was motivated by
3378 -- an AdaCore query to the ARG after this test was added.
3380 Error_Msg_N ("ancestor part must be statically tagged", A);
3381 else
3382 -- We are using the build-in-place protocol, but we can't build
3383 -- in place, because we need to call the function before
3384 -- allocating the aggregate. Could do better for null
3385 -- extensions, and maybe for nondiscriminated types.
3386 -- This is wrong for limited, but those were wrong already.
3388 if not Is_Limited_View (A_Type)
3389 and then Is_Build_In_Place_Function_Call (A)
3390 then
3391 Transform_BIP_Assignment (A_Type);
3392 end if;
3394 Resolve_Record_Aggregate (N, Typ);
3395 end if;
3396 end if;
3398 else
3399 Error_Msg_N ("no unique type for this aggregate", A);
3400 end if;
3402 Check_Function_Writable_Actuals (N);
3403 end Resolve_Extension_Aggregate;
3405 ------------------------------
3406 -- Resolve_Record_Aggregate --
3407 ------------------------------
3409 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
3410 New_Assoc_List : constant List_Id := New_List;
3411 -- New_Assoc_List is the newly built list of N_Component_Association
3412 -- nodes.
3414 Others_Etype : Entity_Id := Empty;
3415 -- This variable is used to save the Etype of the last record component
3416 -- that takes its value from the others choice. Its purpose is:
3418 -- (a) make sure the others choice is useful
3420 -- (b) make sure the type of all the components whose value is
3421 -- subsumed by the others choice are the same.
3423 -- This variable is updated as a side effect of function Get_Value.
3425 Box_Node : Node_Id := Empty;
3426 Is_Box_Present : Boolean := False;
3427 Others_Box : Integer := 0;
3428 -- Ada 2005 (AI-287): Variables used in case of default initialization
3429 -- to provide a functionality similar to Others_Etype. Box_Present
3430 -- indicates that the component takes its default initialization;
3431 -- Others_Box counts the number of components of the current aggregate
3432 -- (which may be a sub-aggregate of a larger one) that are default-
3433 -- initialized. A value of One indicates that an others_box is present.
3434 -- Any larger value indicates that the others_box is not redundant.
3435 -- These variables, similar to Others_Etype, are also updated as a side
3436 -- effect of function Get_Value. Box_Node is used to place a warning on
3437 -- a redundant others_box.
3439 procedure Add_Association
3440 (Component : Entity_Id;
3441 Expr : Node_Id;
3442 Assoc_List : List_Id;
3443 Is_Box_Present : Boolean := False);
3444 -- Builds a new N_Component_Association node which associates Component
3445 -- to expression Expr and adds it to the association list being built,
3446 -- either New_Assoc_List, or the association being built for an inner
3447 -- aggregate.
3449 procedure Add_Discriminant_Values
3450 (New_Aggr : Node_Id;
3451 Assoc_List : List_Id);
3452 -- The constraint to a component may be given by a discriminant of the
3453 -- enclosing type, in which case we have to retrieve its value, which is
3454 -- part of the enclosing aggregate. Assoc_List provides the discriminant
3455 -- associations of the current type or of some enclosing record.
3457 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean;
3458 -- If aggregate N is a regular aggregate this routine will return True.
3459 -- Otherwise, if N is an extension aggregate, then Input_Discr denotes
3460 -- a discriminant whose value may already have been specified by N's
3461 -- ancestor part. This routine checks whether this is indeed the case
3462 -- and if so returns False, signaling that no value for Input_Discr
3463 -- should appear in N's aggregate part. Also, in this case, the routine
3464 -- appends to New_Assoc_List the discriminant value specified in the
3465 -- ancestor part.
3467 -- If the aggregate is in a context with expansion delayed, it will be
3468 -- reanalyzed. The inherited discriminant values must not be reinserted
3469 -- in the component list to prevent spurious errors, but they must be
3470 -- present on first analysis to build the proper subtype indications.
3471 -- The flag Inherited_Discriminant is used to prevent the re-insertion.
3473 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id;
3474 -- AI05-0115: Find earlier ancestor in the derivation chain that is
3475 -- derived from private view Typ. Whether the aggregate is legal depends
3476 -- on the current visibility of the type as well as that of the parent
3477 -- of the ancestor.
3479 function Get_Value
3480 (Compon : Node_Id;
3481 From : List_Id;
3482 Consider_Others_Choice : Boolean := False) return Node_Id;
3483 -- Given a record component stored in parameter Compon, this function
3484 -- returns its value as it appears in the list From, which is a list
3485 -- of N_Component_Association nodes.
3487 -- If no component association has a choice for the searched component,
3488 -- the value provided by the others choice is returned, if there is one,
3489 -- and Consider_Others_Choice is set to true. Otherwise Empty is
3490 -- returned. If there is more than one component association giving a
3491 -- value for the searched record component, an error message is emitted
3492 -- and the first found value is returned.
3494 -- If Consider_Others_Choice is set and the returned expression comes
3495 -- from the others choice, then Others_Etype is set as a side effect.
3496 -- An error message is emitted if the components taking their value from
3497 -- the others choice do not have same type.
3499 procedure Propagate_Discriminants
3500 (Aggr : Node_Id;
3501 Assoc_List : List_Id);
3502 -- Nested components may themselves be discriminated types constrained
3503 -- by outer discriminants, whose values must be captured before the
3504 -- aggregate is expanded into assignments.
3506 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id);
3507 -- Analyzes and resolves expression Expr against the Etype of the
3508 -- Component. This routine also applies all appropriate checks to Expr.
3509 -- It finally saves a Expr in the newly created association list that
3510 -- will be attached to the final record aggregate. Note that if the
3511 -- Parent pointer of Expr is not set then Expr was produced with a
3512 -- New_Copy_Tree or some such.
3514 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id);
3515 -- Rewrite a range node Rge when its bounds refer to non-stored
3516 -- discriminants from Root_Type, to replace them with the stored
3517 -- discriminant values. This is required in GNATprove mode, and is
3518 -- adopted in all modes to avoid special-casing GNATprove mode.
3520 ---------------------
3521 -- Add_Association --
3522 ---------------------
3524 procedure Add_Association
3525 (Component : Entity_Id;
3526 Expr : Node_Id;
3527 Assoc_List : List_Id;
3528 Is_Box_Present : Boolean := False)
3530 Choice_List : constant List_Id := New_List;
3531 Loc : Source_Ptr;
3533 begin
3534 -- If this is a box association the expression is missing, so use the
3535 -- Sloc of the aggregate itself for the new association.
3537 if Present (Expr) then
3538 Loc := Sloc (Expr);
3539 else
3540 Loc := Sloc (N);
3541 end if;
3543 Append_To (Choice_List, New_Occurrence_Of (Component, Loc));
3545 Append_To (Assoc_List,
3546 Make_Component_Association (Loc,
3547 Choices => Choice_List,
3548 Expression => Expr,
3549 Box_Present => Is_Box_Present));
3550 end Add_Association;
3552 -----------------------------
3553 -- Add_Discriminant_Values --
3554 -----------------------------
3556 procedure Add_Discriminant_Values
3557 (New_Aggr : Node_Id;
3558 Assoc_List : List_Id)
3560 Assoc : Node_Id;
3561 Discr : Entity_Id;
3562 Discr_Elmt : Elmt_Id;
3563 Discr_Val : Node_Id;
3564 Val : Entity_Id;
3566 begin
3567 Discr := First_Discriminant (Etype (New_Aggr));
3568 Discr_Elmt := First_Elmt (Discriminant_Constraint (Etype (New_Aggr)));
3569 while Present (Discr_Elmt) loop
3570 Discr_Val := Node (Discr_Elmt);
3572 -- If the constraint is given by a discriminant then it is a
3573 -- discriminant of an enclosing record, and its value has already
3574 -- been placed in the association list.
3576 if Is_Entity_Name (Discr_Val)
3577 and then Ekind (Entity (Discr_Val)) = E_Discriminant
3578 then
3579 Val := Entity (Discr_Val);
3581 Assoc := First (Assoc_List);
3582 while Present (Assoc) loop
3583 if Present (Entity (First (Choices (Assoc))))
3584 and then Entity (First (Choices (Assoc))) = Val
3585 then
3586 Discr_Val := Expression (Assoc);
3587 exit;
3588 end if;
3590 Next (Assoc);
3591 end loop;
3592 end if;
3594 Add_Association
3595 (Discr, New_Copy_Tree (Discr_Val),
3596 Component_Associations (New_Aggr));
3598 -- If the discriminant constraint is a current instance, mark the
3599 -- current aggregate so that the self-reference can be expanded
3600 -- later. The constraint may refer to the subtype of aggregate, so
3601 -- use base type for comparison.
3603 if Nkind (Discr_Val) = N_Attribute_Reference
3604 and then Is_Entity_Name (Prefix (Discr_Val))
3605 and then Is_Type (Entity (Prefix (Discr_Val)))
3606 and then Base_Type (Etype (N)) = Entity (Prefix (Discr_Val))
3607 then
3608 Set_Has_Self_Reference (N);
3609 end if;
3611 Next_Elmt (Discr_Elmt);
3612 Next_Discriminant (Discr);
3613 end loop;
3614 end Add_Discriminant_Values;
3616 --------------------------
3617 -- Discriminant_Present --
3618 --------------------------
3620 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean is
3621 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
3623 Ancestor_Is_Subtyp : Boolean;
3625 Loc : Source_Ptr;
3627 Ancestor : Node_Id;
3628 Ancestor_Typ : Entity_Id;
3629 Comp_Assoc : Node_Id;
3630 Discr : Entity_Id;
3631 Discr_Expr : Node_Id;
3632 Discr_Val : Elmt_Id := No_Elmt;
3633 Orig_Discr : Entity_Id;
3635 begin
3636 if Regular_Aggr then
3637 return True;
3638 end if;
3640 -- Check whether inherited discriminant values have already been
3641 -- inserted in the aggregate. This will be the case if we are
3642 -- re-analyzing an aggregate whose expansion was delayed.
3644 if Present (Component_Associations (N)) then
3645 Comp_Assoc := First (Component_Associations (N));
3646 while Present (Comp_Assoc) loop
3647 if Inherited_Discriminant (Comp_Assoc) then
3648 return True;
3649 end if;
3651 Next (Comp_Assoc);
3652 end loop;
3653 end if;
3655 Ancestor := Ancestor_Part (N);
3656 Ancestor_Typ := Etype (Ancestor);
3657 Loc := Sloc (Ancestor);
3659 -- For a private type with unknown discriminants, use the underlying
3660 -- record view if it is available.
3662 if Has_Unknown_Discriminants (Ancestor_Typ)
3663 and then Present (Full_View (Ancestor_Typ))
3664 and then Present (Underlying_Record_View (Full_View (Ancestor_Typ)))
3665 then
3666 Ancestor_Typ := Underlying_Record_View (Full_View (Ancestor_Typ));
3667 end if;
3669 Ancestor_Is_Subtyp :=
3670 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
3672 -- If the ancestor part has no discriminants clearly N's aggregate
3673 -- part must provide a value for Discr.
3675 if not Has_Discriminants (Ancestor_Typ) then
3676 return True;
3678 -- If the ancestor part is an unconstrained subtype mark then the
3679 -- Discr must be present in N's aggregate part.
3681 elsif Ancestor_Is_Subtyp
3682 and then not Is_Constrained (Entity (Ancestor))
3683 then
3684 return True;
3685 end if;
3687 -- Now look to see if Discr was specified in the ancestor part
3689 if Ancestor_Is_Subtyp then
3690 Discr_Val :=
3691 First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
3692 end if;
3694 Orig_Discr := Original_Record_Component (Input_Discr);
3696 Discr := First_Discriminant (Ancestor_Typ);
3697 while Present (Discr) loop
3699 -- If Ancestor has already specified Disc value then insert its
3700 -- value in the final aggregate.
3702 if Original_Record_Component (Discr) = Orig_Discr then
3703 if Ancestor_Is_Subtyp then
3704 Discr_Expr := New_Copy_Tree (Node (Discr_Val));
3705 else
3706 Discr_Expr :=
3707 Make_Selected_Component (Loc,
3708 Prefix => Duplicate_Subexpr (Ancestor),
3709 Selector_Name => New_Occurrence_Of (Input_Discr, Loc));
3710 end if;
3712 Resolve_Aggr_Expr (Discr_Expr, Input_Discr);
3713 Set_Inherited_Discriminant (Last (New_Assoc_List));
3714 return False;
3715 end if;
3717 Next_Discriminant (Discr);
3719 if Ancestor_Is_Subtyp then
3720 Next_Elmt (Discr_Val);
3721 end if;
3722 end loop;
3724 return True;
3725 end Discriminant_Present;
3727 ---------------------------
3728 -- Find_Private_Ancestor --
3729 ---------------------------
3731 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id is
3732 Par : Entity_Id;
3734 begin
3735 Par := Typ;
3736 loop
3737 if Has_Private_Ancestor (Par)
3738 and then not Has_Private_Ancestor (Etype (Base_Type (Par)))
3739 then
3740 return Par;
3742 elsif not Is_Derived_Type (Par) then
3743 return Empty;
3745 else
3746 Par := Etype (Base_Type (Par));
3747 end if;
3748 end loop;
3749 end Find_Private_Ancestor;
3751 ---------------
3752 -- Get_Value --
3753 ---------------
3755 function Get_Value
3756 (Compon : Node_Id;
3757 From : List_Id;
3758 Consider_Others_Choice : Boolean := False) return Node_Id
3760 Typ : constant Entity_Id := Etype (Compon);
3761 Assoc : Node_Id;
3762 Expr : Node_Id := Empty;
3763 Selector_Name : Node_Id;
3765 begin
3766 Is_Box_Present := False;
3768 if No (From) then
3769 return Empty;
3770 end if;
3772 Assoc := First (From);
3773 while Present (Assoc) loop
3774 Selector_Name := First (Choices (Assoc));
3775 while Present (Selector_Name) loop
3776 if Nkind (Selector_Name) = N_Others_Choice then
3777 if Consider_Others_Choice and then No (Expr) then
3779 -- We need to duplicate the expression for each
3780 -- successive component covered by the others choice.
3781 -- This is redundant if the others_choice covers only
3782 -- one component (small optimization possible???), but
3783 -- indispensable otherwise, because each one must be
3784 -- expanded individually to preserve side effects.
3786 -- Ada 2005 (AI-287): In case of default initialization
3787 -- of components, we duplicate the corresponding default
3788 -- expression (from the record type declaration). The
3789 -- copy must carry the sloc of the association (not the
3790 -- original expression) to prevent spurious elaboration
3791 -- checks when the default includes function calls.
3793 if Box_Present (Assoc) then
3794 Others_Box := Others_Box + 1;
3795 Is_Box_Present := True;
3797 if Expander_Active then
3798 return
3799 New_Copy_Tree_And_Copy_Dimensions
3800 (Expression (Parent (Compon)),
3801 New_Sloc => Sloc (Assoc));
3802 else
3803 return Expression (Parent (Compon));
3804 end if;
3806 else
3807 if Present (Others_Etype)
3808 and then Base_Type (Others_Etype) /= Base_Type (Typ)
3809 then
3810 -- If the components are of an anonymous access
3811 -- type they are distinct, but this is legal in
3812 -- Ada 2012 as long as designated types match.
3814 if (Ekind (Typ) = E_Anonymous_Access_Type
3815 or else Ekind (Typ) =
3816 E_Anonymous_Access_Subprogram_Type)
3817 and then Designated_Type (Typ) =
3818 Designated_Type (Others_Etype)
3819 then
3820 null;
3821 else
3822 Error_Msg_N
3823 ("components in OTHERS choice must have same "
3824 & "type", Selector_Name);
3825 end if;
3826 end if;
3828 Others_Etype := Typ;
3830 -- Copy the expression so that it is resolved
3831 -- independently for each component, This is needed
3832 -- for accessibility checks on compoents of anonymous
3833 -- access types, even in compile_only mode.
3835 if not Inside_A_Generic then
3837 -- In ASIS mode, preanalyze the expression in an
3838 -- others association before making copies for
3839 -- separate resolution and accessibility checks.
3840 -- This ensures that the type of the expression is
3841 -- available to ASIS in all cases, in particular if
3842 -- the expression is itself an aggregate.
3844 if ASIS_Mode then
3845 Preanalyze_And_Resolve (Expression (Assoc), Typ);
3846 end if;
3848 return
3849 New_Copy_Tree_And_Copy_Dimensions
3850 (Expression (Assoc));
3852 else
3853 return Expression (Assoc);
3854 end if;
3855 end if;
3856 end if;
3858 elsif Chars (Compon) = Chars (Selector_Name) then
3859 if No (Expr) then
3861 -- Ada 2005 (AI-231)
3863 if Ada_Version >= Ada_2005
3864 and then Known_Null (Expression (Assoc))
3865 then
3866 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
3867 end if;
3869 -- We need to duplicate the expression when several
3870 -- components are grouped together with a "|" choice.
3871 -- For instance "filed1 | filed2 => Expr"
3873 -- Ada 2005 (AI-287)
3875 if Box_Present (Assoc) then
3876 Is_Box_Present := True;
3878 -- Duplicate the default expression of the component
3879 -- from the record type declaration, so a new copy
3880 -- can be attached to the association.
3882 -- Note that we always copy the default expression,
3883 -- even when the association has a single choice, in
3884 -- order to create a proper association for the
3885 -- expanded aggregate.
3887 -- Component may have no default, in which case the
3888 -- expression is empty and the component is default-
3889 -- initialized, but an association for the component
3890 -- exists, and it is not covered by an others clause.
3892 -- Scalar and private types have no initialization
3893 -- procedure, so they remain uninitialized. If the
3894 -- target of the aggregate is a constant this
3895 -- deserves a warning.
3897 if No (Expression (Parent (Compon)))
3898 and then not Has_Non_Null_Base_Init_Proc (Typ)
3899 and then not Has_Aspect (Typ, Aspect_Default_Value)
3900 and then not Is_Concurrent_Type (Typ)
3901 and then Nkind (Parent (N)) = N_Object_Declaration
3902 and then Constant_Present (Parent (N))
3903 then
3904 Error_Msg_Node_2 := Typ;
3905 Error_Msg_NE
3906 ("component&? of type& is uninitialized",
3907 Assoc, Selector_Name);
3909 -- An additional reminder if the component type
3910 -- is a generic formal.
3912 if Is_Generic_Type (Base_Type (Typ)) then
3913 Error_Msg_NE
3914 ("\instance should provide actual type with "
3915 & "initialization for&", Assoc, Typ);
3916 end if;
3917 end if;
3919 return
3920 New_Copy_Tree_And_Copy_Dimensions
3921 (Expression (Parent (Compon)));
3923 else
3924 if Present (Next (Selector_Name)) then
3925 Expr := New_Copy_Tree_And_Copy_Dimensions
3926 (Expression (Assoc));
3927 else
3928 Expr := Expression (Assoc);
3929 end if;
3930 end if;
3932 Generate_Reference (Compon, Selector_Name, 'm');
3934 else
3935 Error_Msg_NE
3936 ("more than one value supplied for &",
3937 Selector_Name, Compon);
3939 end if;
3940 end if;
3942 Next (Selector_Name);
3943 end loop;
3945 Next (Assoc);
3946 end loop;
3948 return Expr;
3949 end Get_Value;
3951 -----------------------------
3952 -- Propagate_Discriminants --
3953 -----------------------------
3955 procedure Propagate_Discriminants
3956 (Aggr : Node_Id;
3957 Assoc_List : List_Id)
3959 Loc : constant Source_Ptr := Sloc (N);
3961 Needs_Box : Boolean := False;
3963 procedure Process_Component (Comp : Entity_Id);
3964 -- Add one component with a box association to the inner aggregate,
3965 -- and recurse if component is itself composite.
3967 -----------------------
3968 -- Process_Component --
3969 -----------------------
3971 procedure Process_Component (Comp : Entity_Id) is
3972 T : constant Entity_Id := Etype (Comp);
3973 New_Aggr : Node_Id;
3975 begin
3976 if Is_Record_Type (T) and then Has_Discriminants (T) then
3977 New_Aggr := Make_Aggregate (Loc, New_List, New_List);
3978 Set_Etype (New_Aggr, T);
3980 Add_Association
3981 (Comp, New_Aggr, Component_Associations (Aggr));
3983 -- Collect discriminant values and recurse
3985 Add_Discriminant_Values (New_Aggr, Assoc_List);
3986 Propagate_Discriminants (New_Aggr, Assoc_List);
3988 else
3989 Needs_Box := True;
3990 end if;
3991 end Process_Component;
3993 -- Local variables
3995 Aggr_Type : constant Entity_Id := Base_Type (Etype (Aggr));
3996 Components : constant Elist_Id := New_Elmt_List;
3997 Def_Node : constant Node_Id :=
3998 Type_Definition (Declaration_Node (Aggr_Type));
4000 Comp : Node_Id;
4001 Comp_Elmt : Elmt_Id;
4002 Errors : Boolean;
4004 -- Start of processing for Propagate_Discriminants
4006 begin
4007 -- The component type may be a variant type. Collect the components
4008 -- that are ruled by the known values of the discriminants. Their
4009 -- values have already been inserted into the component list of the
4010 -- current aggregate.
4012 if Nkind (Def_Node) = N_Record_Definition
4013 and then Present (Component_List (Def_Node))
4014 and then Present (Variant_Part (Component_List (Def_Node)))
4015 then
4016 Gather_Components (Aggr_Type,
4017 Component_List (Def_Node),
4018 Governed_By => Component_Associations (Aggr),
4019 Into => Components,
4020 Report_Errors => Errors);
4022 Comp_Elmt := First_Elmt (Components);
4023 while Present (Comp_Elmt) loop
4024 if Ekind (Node (Comp_Elmt)) /= E_Discriminant then
4025 Process_Component (Node (Comp_Elmt));
4026 end if;
4028 Next_Elmt (Comp_Elmt);
4029 end loop;
4031 -- No variant part, iterate over all components
4033 else
4034 Comp := First_Component (Etype (Aggr));
4035 while Present (Comp) loop
4036 Process_Component (Comp);
4037 Next_Component (Comp);
4038 end loop;
4039 end if;
4041 if Needs_Box then
4042 Append_To (Component_Associations (Aggr),
4043 Make_Component_Association (Loc,
4044 Choices => New_List (Make_Others_Choice (Loc)),
4045 Expression => Empty,
4046 Box_Present => True));
4047 end if;
4048 end Propagate_Discriminants;
4050 -----------------------
4051 -- Resolve_Aggr_Expr --
4052 -----------------------
4054 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id) is
4055 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
4056 -- If the expression is an aggregate (possibly qualified) then its
4057 -- expansion is delayed until the enclosing aggregate is expanded
4058 -- into assignments. In that case, do not generate checks on the
4059 -- expression, because they will be generated later, and will other-
4060 -- wise force a copy (to remove side effects) that would leave a
4061 -- dynamic-sized aggregate in the code, something that gigi cannot
4062 -- handle.
4064 ---------------------------
4065 -- Has_Expansion_Delayed --
4066 ---------------------------
4068 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
4069 begin
4070 return
4071 (Nkind_In (Expr, N_Aggregate, N_Extension_Aggregate)
4072 and then Present (Etype (Expr))
4073 and then Is_Record_Type (Etype (Expr))
4074 and then Expansion_Delayed (Expr))
4075 or else
4076 (Nkind (Expr) = N_Qualified_Expression
4077 and then Has_Expansion_Delayed (Expression (Expr)));
4078 end Has_Expansion_Delayed;
4080 -- Local variables
4082 Expr_Type : Entity_Id := Empty;
4083 New_C : Entity_Id := Component;
4084 New_Expr : Node_Id;
4086 Relocate : Boolean;
4087 -- Set to True if the resolved Expr node needs to be relocated when
4088 -- attached to the newly created association list. This node need not
4089 -- be relocated if its parent pointer is not set. In fact in this
4090 -- case Expr is the output of a New_Copy_Tree call. If Relocate is
4091 -- True then we have analyzed the expression node in the original
4092 -- aggregate and hence it needs to be relocated when moved over to
4093 -- the new association list.
4095 -- Start of processing for Resolve_Aggr_Expr
4097 begin
4098 -- If the type of the component is elementary or the type of the
4099 -- aggregate does not contain discriminants, use the type of the
4100 -- component to resolve Expr.
4102 if Is_Elementary_Type (Etype (Component))
4103 or else not Has_Discriminants (Etype (N))
4104 then
4105 Expr_Type := Etype (Component);
4107 -- Otherwise we have to pick up the new type of the component from
4108 -- the new constrained subtype of the aggregate. In fact components
4109 -- which are of a composite type might be constrained by a
4110 -- discriminant, and we want to resolve Expr against the subtype were
4111 -- all discriminant occurrences are replaced with their actual value.
4113 else
4114 New_C := First_Component (Etype (N));
4115 while Present (New_C) loop
4116 if Chars (New_C) = Chars (Component) then
4117 Expr_Type := Etype (New_C);
4118 exit;
4119 end if;
4121 Next_Component (New_C);
4122 end loop;
4124 pragma Assert (Present (Expr_Type));
4126 -- For each range in an array type where a discriminant has been
4127 -- replaced with the constraint, check that this range is within
4128 -- the range of the base type. This checks is done in the init
4129 -- proc for regular objects, but has to be done here for
4130 -- aggregates since no init proc is called for them.
4132 if Is_Array_Type (Expr_Type) then
4133 declare
4134 Index : Node_Id;
4135 -- Range of the current constrained index in the array
4137 Orig_Index : Node_Id := First_Index (Etype (Component));
4138 -- Range corresponding to the range Index above in the
4139 -- original unconstrained record type. The bounds of this
4140 -- range may be governed by discriminants.
4142 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
4143 -- Range corresponding to the range Index above for the
4144 -- unconstrained array type. This range is needed to apply
4145 -- range checks.
4147 begin
4148 Index := First_Index (Expr_Type);
4149 while Present (Index) loop
4150 if Depends_On_Discriminant (Orig_Index) then
4151 Apply_Range_Check (Index, Etype (Unconstr_Index));
4152 end if;
4154 Next_Index (Index);
4155 Next_Index (Orig_Index);
4156 Next_Index (Unconstr_Index);
4157 end loop;
4158 end;
4159 end if;
4160 end if;
4162 -- If the Parent pointer of Expr is not set, Expr is an expression
4163 -- duplicated by New_Tree_Copy (this happens for record aggregates
4164 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
4165 -- Such a duplicated expression must be attached to the tree
4166 -- before analysis and resolution to enforce the rule that a tree
4167 -- fragment should never be analyzed or resolved unless it is
4168 -- attached to the current compilation unit.
4170 if No (Parent (Expr)) then
4171 Set_Parent (Expr, N);
4172 Relocate := False;
4173 else
4174 Relocate := True;
4175 end if;
4177 Analyze_And_Resolve (Expr, Expr_Type);
4178 Check_Expr_OK_In_Limited_Aggregate (Expr);
4179 Check_Non_Static_Context (Expr);
4180 Check_Unset_Reference (Expr);
4182 -- Check wrong use of class-wide types
4184 if Is_Class_Wide_Type (Etype (Expr)) then
4185 Error_Msg_N ("dynamically tagged expression not allowed", Expr);
4186 end if;
4188 if not Has_Expansion_Delayed (Expr) then
4189 Aggregate_Constraint_Checks (Expr, Expr_Type);
4190 end if;
4192 -- If an aggregate component has a type with predicates, an explicit
4193 -- predicate check must be applied, as for an assignment statement,
4194 -- because the aggegate might not be expanded into individual
4195 -- component assignments.
4197 if Present (Predicate_Function (Expr_Type))
4198 and then Analyzed (Expr)
4199 then
4200 Apply_Predicate_Check (Expr, Expr_Type);
4201 end if;
4203 if Raises_Constraint_Error (Expr) then
4204 Set_Raises_Constraint_Error (N);
4205 end if;
4207 -- If the expression has been marked as requiring a range check, then
4208 -- generate it here. It's a bit odd to be generating such checks in
4209 -- the analyzer, but harmless since Generate_Range_Check does nothing
4210 -- (other than making sure Do_Range_Check is set) if the expander is
4211 -- not active.
4213 if Do_Range_Check (Expr) then
4214 Generate_Range_Check (Expr, Expr_Type, CE_Range_Check_Failed);
4215 end if;
4217 -- Add association Component => Expr if the caller requests it
4219 if Relocate then
4220 New_Expr := Relocate_Node (Expr);
4222 -- Since New_Expr is not gonna be analyzed later on, we need to
4223 -- propagate here the dimensions form Expr to New_Expr.
4225 Copy_Dimensions (Expr, New_Expr);
4227 else
4228 New_Expr := Expr;
4229 end if;
4231 Add_Association (New_C, New_Expr, New_Assoc_List);
4232 end Resolve_Aggr_Expr;
4234 -------------------
4235 -- Rewrite_Range --
4236 -------------------
4238 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id) is
4239 procedure Rewrite_Bound
4240 (Bound : Node_Id;
4241 Disc : Entity_Id;
4242 Expr_Disc : Node_Id);
4243 -- Rewrite a bound of the range Bound, when it is equal to the
4244 -- non-stored discriminant Disc, into the stored discriminant
4245 -- value Expr_Disc.
4247 -------------------
4248 -- Rewrite_Bound --
4249 -------------------
4251 procedure Rewrite_Bound
4252 (Bound : Node_Id;
4253 Disc : Entity_Id;
4254 Expr_Disc : Node_Id)
4256 begin
4257 if Nkind (Bound) = N_Identifier
4258 and then Entity (Bound) = Disc
4259 then
4260 Rewrite (Bound, New_Copy_Tree (Expr_Disc));
4261 end if;
4262 end Rewrite_Bound;
4264 -- Local variables
4266 Low, High : Node_Id;
4267 Disc : Entity_Id;
4268 Expr_Disc : Elmt_Id;
4270 -- Start of processing for Rewrite_Range
4272 begin
4273 if Has_Discriminants (Root_Type)
4274 and then Nkind (Rge) = N_Range
4275 then
4276 Low := Low_Bound (Rge);
4277 High := High_Bound (Rge);
4279 Disc := First_Discriminant (Root_Type);
4280 Expr_Disc := First_Elmt (Stored_Constraint (Etype (N)));
4281 while Present (Disc) loop
4282 Rewrite_Bound (Low, Disc, Node (Expr_Disc));
4283 Rewrite_Bound (High, Disc, Node (Expr_Disc));
4284 Next_Discriminant (Disc);
4285 Next_Elmt (Expr_Disc);
4286 end loop;
4287 end if;
4288 end Rewrite_Range;
4290 -- Local variables
4292 Components : constant Elist_Id := New_Elmt_List;
4293 -- Components is the list of the record components whose value must be
4294 -- provided in the aggregate. This list does include discriminants.
4296 Component : Entity_Id;
4297 Component_Elmt : Elmt_Id;
4298 Expr : Node_Id;
4299 Positional_Expr : Node_Id;
4301 -- Start of processing for Resolve_Record_Aggregate
4303 begin
4304 -- A record aggregate is restricted in SPARK:
4306 -- Each named association can have only a single choice.
4307 -- OTHERS cannot be used.
4308 -- Positional and named associations cannot be mixed.
4310 if Present (Component_Associations (N))
4311 and then Present (First (Component_Associations (N)))
4312 then
4313 if Present (Expressions (N)) then
4314 Check_SPARK_05_Restriction
4315 ("named association cannot follow positional one",
4316 First (Choices (First (Component_Associations (N)))));
4317 end if;
4319 declare
4320 Assoc : Node_Id;
4322 begin
4323 Assoc := First (Component_Associations (N));
4324 while Present (Assoc) loop
4325 if Nkind (Assoc) = N_Iterated_Component_Association then
4326 Error_Msg_N
4327 ("iterated component association can only appear in an "
4328 & "array aggregate", N);
4329 raise Unrecoverable_Error;
4331 else
4332 if List_Length (Choices (Assoc)) > 1 then
4333 Check_SPARK_05_Restriction
4334 ("component association in record aggregate must "
4335 & "contain a single choice", Assoc);
4336 end if;
4338 if Nkind (First (Choices (Assoc))) = N_Others_Choice then
4339 Check_SPARK_05_Restriction
4340 ("record aggregate cannot contain OTHERS", Assoc);
4341 end if;
4342 end if;
4344 Assoc := Next (Assoc);
4345 end loop;
4346 end;
4347 end if;
4349 -- We may end up calling Duplicate_Subexpr on expressions that are
4350 -- attached to New_Assoc_List. For this reason we need to attach it
4351 -- to the tree by setting its parent pointer to N. This parent point
4352 -- will change in STEP 8 below.
4354 Set_Parent (New_Assoc_List, N);
4356 -- STEP 1: abstract type and null record verification
4358 if Is_Abstract_Type (Typ) then
4359 Error_Msg_N ("type of aggregate cannot be abstract", N);
4360 end if;
4362 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
4363 Set_Etype (N, Typ);
4364 return;
4366 elsif Present (First_Entity (Typ))
4367 and then Null_Record_Present (N)
4368 and then not Is_Tagged_Type (Typ)
4369 then
4370 Error_Msg_N ("record aggregate cannot be null", N);
4371 return;
4373 -- If the type has no components, then the aggregate should either
4374 -- have "null record", or in Ada 2005 it could instead have a single
4375 -- component association given by "others => <>". For Ada 95 we flag an
4376 -- error at this point, but for Ada 2005 we proceed with checking the
4377 -- associations below, which will catch the case where it's not an
4378 -- aggregate with "others => <>". Note that the legality of a <>
4379 -- aggregate for a null record type was established by AI05-016.
4381 elsif No (First_Entity (Typ))
4382 and then Ada_Version < Ada_2005
4383 then
4384 Error_Msg_N ("record aggregate must be null", N);
4385 return;
4386 end if;
4388 -- STEP 2: Verify aggregate structure
4390 Step_2 : declare
4391 Assoc : Node_Id;
4392 Bad_Aggregate : Boolean := False;
4393 Selector_Name : Node_Id;
4395 begin
4396 if Present (Component_Associations (N)) then
4397 Assoc := First (Component_Associations (N));
4398 else
4399 Assoc := Empty;
4400 end if;
4402 while Present (Assoc) loop
4403 Selector_Name := First (Choices (Assoc));
4404 while Present (Selector_Name) loop
4405 if Nkind (Selector_Name) = N_Identifier then
4406 null;
4408 elsif Nkind (Selector_Name) = N_Others_Choice then
4409 if Selector_Name /= First (Choices (Assoc))
4410 or else Present (Next (Selector_Name))
4411 then
4412 Error_Msg_N
4413 ("OTHERS must appear alone in a choice list",
4414 Selector_Name);
4415 return;
4417 elsif Present (Next (Assoc)) then
4418 Error_Msg_N
4419 ("OTHERS must appear last in an aggregate",
4420 Selector_Name);
4421 return;
4423 -- (Ada 2005): If this is an association with a box,
4424 -- indicate that the association need not represent
4425 -- any component.
4427 elsif Box_Present (Assoc) then
4428 Others_Box := 1;
4429 Box_Node := Assoc;
4430 end if;
4432 else
4433 Error_Msg_N
4434 ("selector name should be identifier or OTHERS",
4435 Selector_Name);
4436 Bad_Aggregate := True;
4437 end if;
4439 Next (Selector_Name);
4440 end loop;
4442 Next (Assoc);
4443 end loop;
4445 if Bad_Aggregate then
4446 return;
4447 end if;
4448 end Step_2;
4450 -- STEP 3: Find discriminant Values
4452 Step_3 : declare
4453 Discrim : Entity_Id;
4454 Missing_Discriminants : Boolean := False;
4456 begin
4457 if Present (Expressions (N)) then
4458 Positional_Expr := First (Expressions (N));
4459 else
4460 Positional_Expr := Empty;
4461 end if;
4463 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
4464 -- must not have unknown discriminants.
4466 if Is_Derived_Type (Typ)
4467 and then Has_Unknown_Discriminants (Root_Type (Typ))
4468 and then Nkind (N) /= N_Extension_Aggregate
4469 then
4470 Error_Msg_NE
4471 ("aggregate not available for type& whose ancestor "
4472 & "has unknown discriminants ", N, Typ);
4473 end if;
4475 if Has_Unknown_Discriminants (Typ)
4476 and then Present (Underlying_Record_View (Typ))
4477 then
4478 Discrim := First_Discriminant (Underlying_Record_View (Typ));
4479 elsif Has_Discriminants (Typ) then
4480 Discrim := First_Discriminant (Typ);
4481 else
4482 Discrim := Empty;
4483 end if;
4485 -- First find the discriminant values in the positional components
4487 while Present (Discrim) and then Present (Positional_Expr) loop
4488 if Discriminant_Present (Discrim) then
4489 Resolve_Aggr_Expr (Positional_Expr, Discrim);
4491 -- Ada 2005 (AI-231)
4493 if Ada_Version >= Ada_2005
4494 and then Known_Null (Positional_Expr)
4495 then
4496 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
4497 end if;
4499 Next (Positional_Expr);
4500 end if;
4502 if Present (Get_Value (Discrim, Component_Associations (N))) then
4503 Error_Msg_NE
4504 ("more than one value supplied for discriminant&",
4505 N, Discrim);
4506 end if;
4508 Next_Discriminant (Discrim);
4509 end loop;
4511 -- Find remaining discriminant values if any among named components
4513 while Present (Discrim) loop
4514 Expr := Get_Value (Discrim, Component_Associations (N), True);
4516 if not Discriminant_Present (Discrim) then
4517 if Present (Expr) then
4518 Error_Msg_NE
4519 ("more than one value supplied for discriminant &",
4520 N, Discrim);
4521 end if;
4523 elsif No (Expr) then
4524 Error_Msg_NE
4525 ("no value supplied for discriminant &", N, Discrim);
4526 Missing_Discriminants := True;
4528 else
4529 Resolve_Aggr_Expr (Expr, Discrim);
4530 end if;
4532 Next_Discriminant (Discrim);
4533 end loop;
4535 if Missing_Discriminants then
4536 return;
4537 end if;
4539 -- At this point and until the beginning of STEP 6, New_Assoc_List
4540 -- contains only the discriminants and their values.
4542 end Step_3;
4544 -- STEP 4: Set the Etype of the record aggregate
4546 -- ??? This code is pretty much a copy of Sem_Ch3.Build_Subtype. That
4547 -- routine should really be exported in sem_util or some such and used
4548 -- in sem_ch3 and here rather than have a copy of the code which is a
4549 -- maintenance nightmare.
4551 -- ??? Performance WARNING. The current implementation creates a new
4552 -- itype for all aggregates whose base type is discriminated. This means
4553 -- that for record aggregates nested inside an array aggregate we will
4554 -- create a new itype for each record aggregate if the array component
4555 -- type has discriminants. For large aggregates this may be a problem.
4556 -- What should be done in this case is to reuse itypes as much as
4557 -- possible.
4559 if Has_Discriminants (Typ)
4560 or else (Has_Unknown_Discriminants (Typ)
4561 and then Present (Underlying_Record_View (Typ)))
4562 then
4563 Build_Constrained_Itype : declare
4564 Constrs : constant List_Id := New_List;
4565 Loc : constant Source_Ptr := Sloc (N);
4566 Def_Id : Entity_Id;
4567 Indic : Node_Id;
4568 New_Assoc : Node_Id;
4569 Subtyp_Decl : Node_Id;
4571 begin
4572 New_Assoc := First (New_Assoc_List);
4573 while Present (New_Assoc) loop
4574 Append_To (Constrs, Duplicate_Subexpr (Expression (New_Assoc)));
4575 Next (New_Assoc);
4576 end loop;
4578 if Has_Unknown_Discriminants (Typ)
4579 and then Present (Underlying_Record_View (Typ))
4580 then
4581 Indic :=
4582 Make_Subtype_Indication (Loc,
4583 Subtype_Mark =>
4584 New_Occurrence_Of (Underlying_Record_View (Typ), Loc),
4585 Constraint =>
4586 Make_Index_Or_Discriminant_Constraint (Loc,
4587 Constraints => Constrs));
4588 else
4589 Indic :=
4590 Make_Subtype_Indication (Loc,
4591 Subtype_Mark =>
4592 New_Occurrence_Of (Base_Type (Typ), Loc),
4593 Constraint =>
4594 Make_Index_Or_Discriminant_Constraint (Loc,
4595 Constraints => Constrs));
4596 end if;
4598 Def_Id := Create_Itype (Ekind (Typ), N);
4600 Subtyp_Decl :=
4601 Make_Subtype_Declaration (Loc,
4602 Defining_Identifier => Def_Id,
4603 Subtype_Indication => Indic);
4604 Set_Parent (Subtyp_Decl, Parent (N));
4606 -- Itypes must be analyzed with checks off (see itypes.ads)
4608 Analyze (Subtyp_Decl, Suppress => All_Checks);
4610 Set_Etype (N, Def_Id);
4611 Check_Static_Discriminated_Subtype
4612 (Def_Id, Expression (First (New_Assoc_List)));
4613 end Build_Constrained_Itype;
4615 else
4616 Set_Etype (N, Typ);
4617 end if;
4619 -- STEP 5: Get remaining components according to discriminant values
4621 Step_5 : declare
4622 Dnode : Node_Id;
4623 Errors_Found : Boolean := False;
4624 Record_Def : Node_Id;
4625 Parent_Typ : Entity_Id;
4626 Parent_Typ_List : Elist_Id;
4627 Parent_Elmt : Elmt_Id;
4628 Root_Typ : Entity_Id;
4630 begin
4631 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
4632 Parent_Typ_List := New_Elmt_List;
4634 -- If this is an extension aggregate, the component list must
4635 -- include all components that are not in the given ancestor type.
4636 -- Otherwise, the component list must include components of all
4637 -- ancestors, starting with the root.
4639 if Nkind (N) = N_Extension_Aggregate then
4640 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
4642 else
4643 -- AI05-0115: check legality of aggregate for type with a
4644 -- private ancestor.
4646 Root_Typ := Root_Type (Typ);
4647 if Has_Private_Ancestor (Typ) then
4648 declare
4649 Ancestor : constant Entity_Id :=
4650 Find_Private_Ancestor (Typ);
4651 Ancestor_Unit : constant Entity_Id :=
4652 Cunit_Entity
4653 (Get_Source_Unit (Ancestor));
4654 Parent_Unit : constant Entity_Id :=
4655 Cunit_Entity (Get_Source_Unit
4656 (Base_Type (Etype (Ancestor))));
4657 begin
4658 -- Check whether we are in a scope that has full view
4659 -- over the private ancestor and its parent. This can
4660 -- only happen if the derivation takes place in a child
4661 -- unit of the unit that declares the parent, and we are
4662 -- in the private part or body of that child unit, else
4663 -- the aggregate is illegal.
4665 if Is_Child_Unit (Ancestor_Unit)
4666 and then Scope (Ancestor_Unit) = Parent_Unit
4667 and then In_Open_Scopes (Scope (Ancestor))
4668 and then
4669 (In_Private_Part (Scope (Ancestor))
4670 or else In_Package_Body (Scope (Ancestor)))
4671 then
4672 null;
4674 else
4675 Error_Msg_NE
4676 ("type of aggregate has private ancestor&!",
4677 N, Root_Typ);
4678 Error_Msg_N ("must use extension aggregate!", N);
4679 return;
4680 end if;
4681 end;
4682 end if;
4684 Dnode := Declaration_Node (Base_Type (Root_Typ));
4686 -- If we don't get a full declaration, then we have some error
4687 -- which will get signalled later so skip this part. Otherwise
4688 -- gather components of root that apply to the aggregate type.
4689 -- We use the base type in case there is an applicable stored
4690 -- constraint that renames the discriminants of the root.
4692 if Nkind (Dnode) = N_Full_Type_Declaration then
4693 Record_Def := Type_Definition (Dnode);
4694 Gather_Components
4695 (Base_Type (Typ),
4696 Component_List (Record_Def),
4697 Governed_By => New_Assoc_List,
4698 Into => Components,
4699 Report_Errors => Errors_Found);
4701 if Errors_Found then
4702 Error_Msg_N
4703 ("discriminant controlling variant part is not static",
4705 return;
4706 end if;
4707 end if;
4708 end if;
4710 Parent_Typ := Base_Type (Typ);
4711 while Parent_Typ /= Root_Typ loop
4712 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
4713 Parent_Typ := Etype (Parent_Typ);
4715 if Nkind (Parent (Base_Type (Parent_Typ))) =
4716 N_Private_Type_Declaration
4717 or else Nkind (Parent (Base_Type (Parent_Typ))) =
4718 N_Private_Extension_Declaration
4719 then
4720 if Nkind (N) /= N_Extension_Aggregate then
4721 Error_Msg_NE
4722 ("type of aggregate has private ancestor&!",
4723 N, Parent_Typ);
4724 Error_Msg_N ("must use extension aggregate!", N);
4725 return;
4727 elsif Parent_Typ /= Root_Typ then
4728 Error_Msg_NE
4729 ("ancestor part of aggregate must be private type&",
4730 Ancestor_Part (N), Parent_Typ);
4731 return;
4732 end if;
4734 -- The current view of ancestor part may be a private type,
4735 -- while the context type is always non-private.
4737 elsif Is_Private_Type (Root_Typ)
4738 and then Present (Full_View (Root_Typ))
4739 and then Nkind (N) = N_Extension_Aggregate
4740 then
4741 exit when Base_Type (Full_View (Root_Typ)) = Parent_Typ;
4742 end if;
4743 end loop;
4745 -- Now collect components from all other ancestors, beginning
4746 -- with the current type. If the type has unknown discriminants
4747 -- use the component list of the Underlying_Record_View, which
4748 -- needs to be used for the subsequent expansion of the aggregate
4749 -- into assignments.
4751 Parent_Elmt := First_Elmt (Parent_Typ_List);
4752 while Present (Parent_Elmt) loop
4753 Parent_Typ := Node (Parent_Elmt);
4755 if Has_Unknown_Discriminants (Parent_Typ)
4756 and then Present (Underlying_Record_View (Typ))
4757 then
4758 Parent_Typ := Underlying_Record_View (Parent_Typ);
4759 end if;
4761 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
4762 Gather_Components (Empty,
4763 Component_List (Record_Extension_Part (Record_Def)),
4764 Governed_By => New_Assoc_List,
4765 Into => Components,
4766 Report_Errors => Errors_Found);
4768 Next_Elmt (Parent_Elmt);
4769 end loop;
4771 -- Typ is not a derived tagged type
4773 else
4774 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
4776 if Null_Present (Record_Def) then
4777 null;
4779 elsif not Has_Unknown_Discriminants (Typ) then
4780 Gather_Components
4781 (Base_Type (Typ),
4782 Component_List (Record_Def),
4783 Governed_By => New_Assoc_List,
4784 Into => Components,
4785 Report_Errors => Errors_Found);
4787 else
4788 Gather_Components
4789 (Base_Type (Underlying_Record_View (Typ)),
4790 Component_List (Record_Def),
4791 Governed_By => New_Assoc_List,
4792 Into => Components,
4793 Report_Errors => Errors_Found);
4794 end if;
4795 end if;
4797 if Errors_Found then
4798 return;
4799 end if;
4800 end Step_5;
4802 -- STEP 6: Find component Values
4804 Component := Empty;
4805 Component_Elmt := First_Elmt (Components);
4807 -- First scan the remaining positional associations in the aggregate.
4808 -- Remember that at this point Positional_Expr contains the current
4809 -- positional association if any is left after looking for discriminant
4810 -- values in step 3.
4812 while Present (Positional_Expr) and then Present (Component_Elmt) loop
4813 Component := Node (Component_Elmt);
4814 Resolve_Aggr_Expr (Positional_Expr, Component);
4816 -- Ada 2005 (AI-231)
4818 if Ada_Version >= Ada_2005 and then Known_Null (Positional_Expr) then
4819 Check_Can_Never_Be_Null (Component, Positional_Expr);
4820 end if;
4822 if Present (Get_Value (Component, Component_Associations (N))) then
4823 Error_Msg_NE
4824 ("more than one value supplied for Component &", N, Component);
4825 end if;
4827 Next (Positional_Expr);
4828 Next_Elmt (Component_Elmt);
4829 end loop;
4831 if Present (Positional_Expr) then
4832 Error_Msg_N
4833 ("too many components for record aggregate", Positional_Expr);
4834 end if;
4836 -- Now scan for the named arguments of the aggregate
4838 while Present (Component_Elmt) loop
4839 Component := Node (Component_Elmt);
4840 Expr := Get_Value (Component, Component_Associations (N), True);
4842 -- Note: The previous call to Get_Value sets the value of the
4843 -- variable Is_Box_Present.
4845 -- Ada 2005 (AI-287): Handle components with default initialization.
4846 -- Note: This feature was originally added to Ada 2005 for limited
4847 -- but it was finally allowed with any type.
4849 if Is_Box_Present then
4850 Check_Box_Component : declare
4851 Ctyp : constant Entity_Id := Etype (Component);
4853 begin
4854 -- If there is a default expression for the aggregate, copy
4855 -- it into a new association. This copy must modify the scopes
4856 -- of internal types that may be attached to the expression
4857 -- (e.g. index subtypes of arrays) because in general the type
4858 -- declaration and the aggregate appear in different scopes,
4859 -- and the backend requires the scope of the type to match the
4860 -- point at which it is elaborated.
4862 -- If the component has an initialization procedure (IP) we
4863 -- pass the component to the expander, which will generate
4864 -- the call to such IP.
4866 -- If the component has discriminants, their values must
4867 -- be taken from their subtype. This is indispensable for
4868 -- constraints that are given by the current instance of an
4869 -- enclosing type, to allow the expansion of the aggregate to
4870 -- replace the reference to the current instance by the target
4871 -- object of the aggregate.
4873 if Present (Parent (Component))
4874 and then Nkind (Parent (Component)) = N_Component_Declaration
4875 and then Present (Expression (Parent (Component)))
4876 then
4877 Expr :=
4878 New_Copy_Tree_And_Copy_Dimensions
4879 (Expression (Parent (Component)),
4880 New_Scope => Current_Scope,
4881 New_Sloc => Sloc (N));
4883 -- As the type of the copied default expression may refer
4884 -- to discriminants of the record type declaration, these
4885 -- non-stored discriminants need to be rewritten into stored
4886 -- discriminant values for the aggregate. This is required
4887 -- in GNATprove mode, and is adopted in all modes to avoid
4888 -- special-casing GNATprove mode.
4890 if Is_Array_Type (Etype (Expr)) then
4891 declare
4892 Rec_Typ : constant Entity_Id := Scope (Component);
4893 -- Root record type whose discriminants may be used as
4894 -- bounds in range nodes.
4896 Index : Node_Id;
4898 begin
4899 -- Rewrite the range nodes occurring in the indexes
4900 -- and their types.
4902 Index := First_Index (Etype (Expr));
4903 while Present (Index) loop
4904 Rewrite_Range (Rec_Typ, Index);
4905 Rewrite_Range
4906 (Rec_Typ, Scalar_Range (Etype (Index)));
4908 Next_Index (Index);
4909 end loop;
4911 -- Rewrite the range nodes occurring as aggregate
4912 -- bounds.
4914 if Nkind (Expr) = N_Aggregate
4915 and then Present (Aggregate_Bounds (Expr))
4916 then
4917 Rewrite_Range (Rec_Typ, Aggregate_Bounds (Expr));
4918 end if;
4919 end;
4920 end if;
4922 Add_Association
4923 (Component => Component,
4924 Expr => Expr,
4925 Assoc_List => New_Assoc_List);
4926 Set_Has_Self_Reference (N);
4928 -- A box-defaulted access component gets the value null. Also
4929 -- included are components of private types whose underlying
4930 -- type is an access type. In either case set the type of the
4931 -- literal, for subsequent use in semantic checks.
4933 elsif Present (Underlying_Type (Ctyp))
4934 and then Is_Access_Type (Underlying_Type (Ctyp))
4935 then
4936 -- If the component's type is private with an access type as
4937 -- its underlying type then we have to create an unchecked
4938 -- conversion to satisfy type checking.
4940 if Is_Private_Type (Ctyp) then
4941 declare
4942 Qual_Null : constant Node_Id :=
4943 Make_Qualified_Expression (Sloc (N),
4944 Subtype_Mark =>
4945 New_Occurrence_Of
4946 (Underlying_Type (Ctyp), Sloc (N)),
4947 Expression => Make_Null (Sloc (N)));
4949 Convert_Null : constant Node_Id :=
4950 Unchecked_Convert_To
4951 (Ctyp, Qual_Null);
4953 begin
4954 Analyze_And_Resolve (Convert_Null, Ctyp);
4955 Add_Association
4956 (Component => Component,
4957 Expr => Convert_Null,
4958 Assoc_List => New_Assoc_List);
4959 end;
4961 -- Otherwise the component type is non-private
4963 else
4964 Expr := Make_Null (Sloc (N));
4965 Set_Etype (Expr, Ctyp);
4967 Add_Association
4968 (Component => Component,
4969 Expr => Expr,
4970 Assoc_List => New_Assoc_List);
4971 end if;
4973 -- Ada 2012: If component is scalar with default value, use it
4975 elsif Is_Scalar_Type (Ctyp)
4976 and then Has_Default_Aspect (Ctyp)
4977 then
4978 Add_Association
4979 (Component => Component,
4980 Expr =>
4981 Default_Aspect_Value
4982 (First_Subtype (Underlying_Type (Ctyp))),
4983 Assoc_List => New_Assoc_List);
4985 elsif Has_Non_Null_Base_Init_Proc (Ctyp)
4986 or else not Expander_Active
4987 then
4988 if Is_Record_Type (Ctyp)
4989 and then Has_Discriminants (Ctyp)
4990 and then not Is_Private_Type (Ctyp)
4991 then
4992 -- We build a partially initialized aggregate with the
4993 -- values of the discriminants and box initialization
4994 -- for the rest, if other components are present.
4996 -- The type of the aggregate is the known subtype of
4997 -- the component. The capture of discriminants must be
4998 -- recursive because subcomponents may be constrained
4999 -- (transitively) by discriminants of enclosing types.
5000 -- For a private type with discriminants, a call to the
5001 -- initialization procedure will be generated, and no
5002 -- subaggregate is needed.
5004 Capture_Discriminants : declare
5005 Loc : constant Source_Ptr := Sloc (N);
5006 Expr : Node_Id;
5008 begin
5009 Expr := Make_Aggregate (Loc, New_List, New_List);
5010 Set_Etype (Expr, Ctyp);
5012 -- If the enclosing type has discriminants, they have
5013 -- been collected in the aggregate earlier, and they
5014 -- may appear as constraints of subcomponents.
5016 -- Similarly if this component has discriminants, they
5017 -- might in turn be propagated to their components.
5019 if Has_Discriminants (Typ) then
5020 Add_Discriminant_Values (Expr, New_Assoc_List);
5021 Propagate_Discriminants (Expr, New_Assoc_List);
5023 elsif Has_Discriminants (Ctyp) then
5024 Add_Discriminant_Values
5025 (Expr, Component_Associations (Expr));
5026 Propagate_Discriminants
5027 (Expr, Component_Associations (Expr));
5029 else
5030 declare
5031 Comp : Entity_Id;
5033 begin
5034 -- If the type has additional components, create
5035 -- an OTHERS box association for them.
5037 Comp := First_Component (Ctyp);
5038 while Present (Comp) loop
5039 if Ekind (Comp) = E_Component then
5040 if not Is_Record_Type (Etype (Comp)) then
5041 Append_To
5042 (Component_Associations (Expr),
5043 Make_Component_Association (Loc,
5044 Choices =>
5045 New_List (
5046 Make_Others_Choice (Loc)),
5047 Expression => Empty,
5048 Box_Present => True));
5049 end if;
5051 exit;
5052 end if;
5054 Next_Component (Comp);
5055 end loop;
5056 end;
5057 end if;
5059 Add_Association
5060 (Component => Component,
5061 Expr => Expr,
5062 Assoc_List => New_Assoc_List);
5063 end Capture_Discriminants;
5065 -- Otherwise the component type is not a record, or it has
5066 -- not discriminants, or it is private.
5068 else
5069 Add_Association
5070 (Component => Component,
5071 Expr => Empty,
5072 Assoc_List => New_Assoc_List,
5073 Is_Box_Present => True);
5074 end if;
5076 -- Otherwise we only need to resolve the expression if the
5077 -- component has partially initialized values (required to
5078 -- expand the corresponding assignments and run-time checks).
5080 elsif Present (Expr)
5081 and then Is_Partially_Initialized_Type (Ctyp)
5082 then
5083 Resolve_Aggr_Expr (Expr, Component);
5084 end if;
5085 end Check_Box_Component;
5087 elsif No (Expr) then
5089 -- Ignore hidden components associated with the position of the
5090 -- interface tags: these are initialized dynamically.
5092 if not Present (Related_Type (Component)) then
5093 Error_Msg_NE
5094 ("no value supplied for component &!", N, Component);
5095 end if;
5097 else
5098 Resolve_Aggr_Expr (Expr, Component);
5099 end if;
5101 Next_Elmt (Component_Elmt);
5102 end loop;
5104 -- STEP 7: check for invalid components + check type in choice list
5106 Step_7 : declare
5107 Assoc : Node_Id;
5108 New_Assoc : Node_Id;
5110 Selectr : Node_Id;
5111 -- Selector name
5113 Typech : Entity_Id;
5114 -- Type of first component in choice list
5116 begin
5117 if Present (Component_Associations (N)) then
5118 Assoc := First (Component_Associations (N));
5119 else
5120 Assoc := Empty;
5121 end if;
5123 Verification : while Present (Assoc) loop
5124 Selectr := First (Choices (Assoc));
5125 Typech := Empty;
5127 if Nkind (Selectr) = N_Others_Choice then
5129 -- Ada 2005 (AI-287): others choice may have expression or box
5131 if No (Others_Etype) and then Others_Box = 0 then
5132 Error_Msg_N
5133 ("OTHERS must represent at least one component", Selectr);
5135 elsif Others_Box = 1 and then Warn_On_Redundant_Constructs then
5136 Error_Msg_N ("others choice is redundant?", Box_Node);
5137 Error_Msg_N
5138 ("\previous choices cover all components?", Box_Node);
5139 end if;
5141 exit Verification;
5142 end if;
5144 while Present (Selectr) loop
5145 New_Assoc := First (New_Assoc_List);
5146 while Present (New_Assoc) loop
5147 Component := First (Choices (New_Assoc));
5149 if Chars (Selectr) = Chars (Component) then
5150 if Style_Check then
5151 Check_Identifier (Selectr, Entity (Component));
5152 end if;
5154 exit;
5155 end if;
5157 Next (New_Assoc);
5158 end loop;
5160 -- If no association, this is not a legal component of the type
5161 -- in question, unless its association is provided with a box.
5163 if No (New_Assoc) then
5164 if Box_Present (Parent (Selectr)) then
5166 -- This may still be a bogus component with a box. Scan
5167 -- list of components to verify that a component with
5168 -- that name exists.
5170 declare
5171 C : Entity_Id;
5173 begin
5174 C := First_Component (Typ);
5175 while Present (C) loop
5176 if Chars (C) = Chars (Selectr) then
5178 -- If the context is an extension aggregate,
5179 -- the component must not be inherited from
5180 -- the ancestor part of the aggregate.
5182 if Nkind (N) /= N_Extension_Aggregate
5183 or else
5184 Scope (Original_Record_Component (C)) /=
5185 Etype (Ancestor_Part (N))
5186 then
5187 exit;
5188 end if;
5189 end if;
5191 Next_Component (C);
5192 end loop;
5194 if No (C) then
5195 Error_Msg_Node_2 := Typ;
5196 Error_Msg_N ("& is not a component of}", Selectr);
5197 end if;
5198 end;
5200 elsif Chars (Selectr) /= Name_uTag
5201 and then Chars (Selectr) /= Name_uParent
5202 then
5203 if not Has_Discriminants (Typ) then
5204 Error_Msg_Node_2 := Typ;
5205 Error_Msg_N ("& is not a component of}", Selectr);
5206 else
5207 Error_Msg_N
5208 ("& is not a component of the aggregate subtype",
5209 Selectr);
5210 end if;
5212 Check_Misspelled_Component (Components, Selectr);
5213 end if;
5215 elsif No (Typech) then
5216 Typech := Base_Type (Etype (Component));
5218 -- AI05-0199: In Ada 2012, several components of anonymous
5219 -- access types can appear in a choice list, as long as the
5220 -- designated types match.
5222 elsif Typech /= Base_Type (Etype (Component)) then
5223 if Ada_Version >= Ada_2012
5224 and then Ekind (Typech) = E_Anonymous_Access_Type
5225 and then
5226 Ekind (Etype (Component)) = E_Anonymous_Access_Type
5227 and then Base_Type (Designated_Type (Typech)) =
5228 Base_Type (Designated_Type (Etype (Component)))
5229 and then
5230 Subtypes_Statically_Match (Typech, (Etype (Component)))
5231 then
5232 null;
5234 elsif not Box_Present (Parent (Selectr)) then
5235 Error_Msg_N
5236 ("components in choice list must have same type",
5237 Selectr);
5238 end if;
5239 end if;
5241 Next (Selectr);
5242 end loop;
5244 Next (Assoc);
5245 end loop Verification;
5246 end Step_7;
5248 -- STEP 8: replace the original aggregate
5250 Step_8 : declare
5251 New_Aggregate : constant Node_Id := New_Copy (N);
5253 begin
5254 Set_Expressions (New_Aggregate, No_List);
5255 Set_Etype (New_Aggregate, Etype (N));
5256 Set_Component_Associations (New_Aggregate, New_Assoc_List);
5257 Set_Check_Actuals (New_Aggregate, Check_Actuals (N));
5259 Rewrite (N, New_Aggregate);
5260 end Step_8;
5262 -- Check the dimensions of the components in the record aggregate
5264 Analyze_Dimension_Extension_Or_Record_Aggregate (N);
5265 end Resolve_Record_Aggregate;
5267 -----------------------------
5268 -- Check_Can_Never_Be_Null --
5269 -----------------------------
5271 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
5272 Comp_Typ : Entity_Id;
5274 begin
5275 pragma Assert
5276 (Ada_Version >= Ada_2005
5277 and then Present (Expr)
5278 and then Known_Null (Expr));
5280 case Ekind (Typ) is
5281 when E_Array_Type =>
5282 Comp_Typ := Component_Type (Typ);
5284 when E_Component
5285 | E_Discriminant
5287 Comp_Typ := Etype (Typ);
5289 when others =>
5290 return;
5291 end case;
5293 if Can_Never_Be_Null (Comp_Typ) then
5295 -- Here we know we have a constraint error. Note that we do not use
5296 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
5297 -- seem the more natural approach. That's because in some cases the
5298 -- components are rewritten, and the replacement would be missed.
5299 -- We do not mark the whole aggregate as raising a constraint error,
5300 -- because the association may be a null array range.
5302 Error_Msg_N
5303 ("(Ada 2005) null not allowed in null-excluding component??", Expr);
5304 Error_Msg_N
5305 ("\Constraint_Error will be raised at run time??", Expr);
5307 Rewrite (Expr,
5308 Make_Raise_Constraint_Error
5309 (Sloc (Expr), Reason => CE_Access_Check_Failed));
5310 Set_Etype (Expr, Comp_Typ);
5311 Set_Analyzed (Expr);
5312 end if;
5313 end Check_Can_Never_Be_Null;
5315 ---------------------
5316 -- Sort_Case_Table --
5317 ---------------------
5319 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
5320 U : constant Int := Case_Table'Last;
5321 K : Int;
5322 J : Int;
5323 T : Case_Bounds;
5325 begin
5326 K := 1;
5327 while K < U loop
5328 T := Case_Table (K + 1);
5330 J := K + 1;
5331 while J > 1
5332 and then Expr_Value (Case_Table (J - 1).Lo) > Expr_Value (T.Lo)
5333 loop
5334 Case_Table (J) := Case_Table (J - 1);
5335 J := J - 1;
5336 end loop;
5338 Case_Table (J) := T;
5339 K := K + 1;
5340 end loop;
5341 end Sort_Case_Table;
5343 end Sem_Aggr;