PR target/79080
[official-gcc.git] / gcc / ada / sem_aggr.adb
blob92b9da6f303995de8ce4ab61a618ed394adbbbe1
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-2016, 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_Tss; use Exp_Tss;
34 with Exp_Util; use Exp_Util;
35 with Freeze; use Freeze;
36 with Itypes; use Itypes;
37 with Lib; use Lib;
38 with Lib.Xref; use Lib.Xref;
39 with Namet; use Namet;
40 with Namet.Sp; use Namet.Sp;
41 with Nmake; use Nmake;
42 with Nlists; use Nlists;
43 with Opt; use Opt;
44 with Restrict; use Restrict;
45 with Rident; use Rident;
46 with Sem; use Sem;
47 with Sem_Aux; use Sem_Aux;
48 with Sem_Cat; use Sem_Cat;
49 with Sem_Ch3; use Sem_Ch3;
50 with Sem_Ch8; use Sem_Ch8;
51 with Sem_Ch13; use Sem_Ch13;
52 with Sem_Dim; use Sem_Dim;
53 with Sem_Eval; use Sem_Eval;
54 with Sem_Res; use Sem_Res;
55 with Sem_Util; use Sem_Util;
56 with Sem_Type; use Sem_Type;
57 with Sem_Warn; use Sem_Warn;
58 with Sinfo; use Sinfo;
59 with Snames; use Snames;
60 with Stringt; use Stringt;
61 with Stand; use Stand;
62 with Style; use Style;
63 with Targparm; use Targparm;
64 with Tbuild; use Tbuild;
65 with Uintp; use Uintp;
67 package body Sem_Aggr is
69 type Case_Bounds is record
70 Lo : Node_Id;
71 -- Low bound of choice. Once we sort the Case_Table, then entries
72 -- will be in order of ascending Choice_Lo values.
74 Hi : Node_Id;
75 -- High Bound of choice. The sort does not pay any attention to the
76 -- high bound, so choices 1 .. 4 and 1 .. 5 could be in either order.
78 Highest : Uint;
79 -- If there are duplicates or missing entries, then in the sorted
80 -- table, this records the highest value among Choice_Hi values
81 -- seen so far, including this entry.
83 Choice : Node_Id;
84 -- The node of the choice
85 end record;
87 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
88 -- Table type used by Check_Case_Choices procedure. Entry zero is not
89 -- used (reserved for the sort). Real entries start at one.
91 -----------------------
92 -- Local Subprograms --
93 -----------------------
95 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
96 -- Sort the Case Table using the Lower Bound of each Choice as the key. A
97 -- simple insertion sort is used since the choices in a case statement will
98 -- usually be in near sorted order.
100 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id);
101 -- Ada 2005 (AI-231): Check bad usage of null for a component for which
102 -- null exclusion (NOT NULL) is specified. Typ can be an E_Array_Type for
103 -- the array case (the component type of the array will be used) or an
104 -- E_Component/E_Discriminant entity in the record case, in which case the
105 -- type of the component will be used for the test. If Typ is any other
106 -- kind of entity, the call is ignored. Expr is the component node in the
107 -- aggregate which is known to have a null value. A warning message will be
108 -- issued if the component is null excluding.
110 -- It would be better to pass the proper type for Typ ???
112 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id);
113 -- Check that Expr is either not limited or else is one of the cases of
114 -- expressions allowed for a limited component association (namely, an
115 -- aggregate, function call, or <> notation). Report error for violations.
116 -- Expression is also OK in an instance or inlining context, because we
117 -- have already pre-analyzed and it is known to be type correct.
119 procedure Check_Qualified_Aggregate (Level : Nat; Expr : Node_Id);
120 -- Given aggregate Expr, check that sub-aggregates of Expr that are nested
121 -- at Level are qualified. If Level = 0, this applies to Expr directly.
122 -- Only issue errors in formal verification mode.
124 function Is_Top_Level_Aggregate (Expr : Node_Id) return Boolean;
125 -- Return True of Expr is an aggregate not contained directly in another
126 -- aggregate.
128 ------------------------------------------------------
129 -- Subprograms used for RECORD AGGREGATE Processing --
130 ------------------------------------------------------
132 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
133 -- This procedure performs all the semantic checks required for record
134 -- aggregates. Note that for aggregates analysis and resolution go
135 -- hand in hand. Aggregate analysis has been delayed up to here and
136 -- it is done while resolving the aggregate.
138 -- N is the N_Aggregate node.
139 -- Typ is the record type for the aggregate resolution
141 -- While performing the semantic checks, this procedure builds a new
142 -- Component_Association_List where each record field appears alone in a
143 -- Component_Choice_List along with its corresponding expression. The
144 -- record fields in the Component_Association_List appear in the same order
145 -- in which they appear in the record type Typ.
147 -- Once this new Component_Association_List is built and all the semantic
148 -- checks performed, the original aggregate subtree is replaced with the
149 -- new named record aggregate just built. Note that subtree substitution is
150 -- performed with Rewrite so as to be able to retrieve the original
151 -- aggregate.
153 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
154 -- yields the aggregate format expected by Gigi. Typically, this kind of
155 -- tree manipulations are done in the expander. However, because the
156 -- semantic checks that need to be performed on record aggregates really go
157 -- hand in hand with the record aggregate normalization, the aggregate
158 -- subtree transformation is performed during resolution rather than
159 -- expansion. Had we decided otherwise we would have had to duplicate most
160 -- of the code in the expansion procedure Expand_Record_Aggregate. Note,
161 -- however, that all the expansion concerning aggregates for tagged records
162 -- is done in Expand_Record_Aggregate.
164 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
166 -- 1. Make sure that the record type against which the record aggregate
167 -- has to be resolved is not abstract. Furthermore if the type is a
168 -- null aggregate make sure the input aggregate N is also null.
170 -- 2. Verify that the structure of the aggregate is that of a record
171 -- aggregate. Specifically, look for component associations and ensure
172 -- that each choice list only has identifiers or the N_Others_Choice
173 -- node. Also make sure that if present, the N_Others_Choice occurs
174 -- last and by itself.
176 -- 3. If Typ contains discriminants, the values for each discriminant is
177 -- looked for. If the record type Typ has variants, we check that the
178 -- expressions corresponding to each discriminant ruling the (possibly
179 -- nested) variant parts of Typ, are static. This allows us to determine
180 -- the variant parts to which the rest of the aggregate must conform.
181 -- The names of discriminants with their values are saved in a new
182 -- association list, New_Assoc_List which is later augmented with the
183 -- names and values of the remaining components in the record type.
185 -- During this phase we also make sure that every discriminant is
186 -- assigned exactly one value. Note that when several values for a given
187 -- discriminant are found, semantic processing continues looking for
188 -- further errors. In this case it's the first discriminant value found
189 -- which we will be recorded.
191 -- IMPORTANT NOTE: For derived tagged types this procedure expects
192 -- First_Discriminant and Next_Discriminant to give the correct list
193 -- of discriminants, in the correct order.
195 -- 4. After all the discriminant values have been gathered, we can set the
196 -- Etype of the record aggregate. If Typ contains no discriminants this
197 -- is straightforward: the Etype of N is just Typ, otherwise a new
198 -- implicit constrained subtype of Typ is built to be the Etype of N.
200 -- 5. Gather the remaining record components according to the discriminant
201 -- values. This involves recursively traversing the record type
202 -- structure to see what variants are selected by the given discriminant
203 -- values. This processing is a little more convoluted if Typ is a
204 -- derived tagged types since we need to retrieve the record structure
205 -- of all the ancestors of Typ.
207 -- 6. After gathering the record components we look for their values in the
208 -- record aggregate and emit appropriate error messages should we not
209 -- find such values or should they be duplicated.
211 -- 7. We then make sure no illegal component names appear in the record
212 -- aggregate and make sure that the type of the record components
213 -- appearing in a same choice list is the same. Finally we ensure that
214 -- the others choice, if present, is used to provide the value of at
215 -- least a record component.
217 -- 8. The original aggregate node is replaced with the new named aggregate
218 -- built in steps 3 through 6, as explained earlier.
220 -- Given the complexity of record aggregate resolution, the primary goal of
221 -- this routine is clarity and simplicity rather than execution and storage
222 -- efficiency. If there are only positional components in the aggregate the
223 -- running time is linear. If there are associations the running time is
224 -- still linear as long as the order of the associations is not too far off
225 -- the order of the components in the record type. If this is not the case
226 -- the running time is at worst quadratic in the size of the association
227 -- list.
229 procedure Check_Misspelled_Component
230 (Elements : Elist_Id;
231 Component : Node_Id);
232 -- Give possible misspelling diagnostic if Component is likely to be a
233 -- misspelling of one of the components of the Assoc_List. This is called
234 -- by Resolve_Aggr_Expr after producing an invalid component error message.
236 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id);
237 -- An optimization: determine whether a discriminated subtype has a static
238 -- constraint, and contains array components whose length is also static,
239 -- either because they are constrained by the discriminant, or because the
240 -- original component bounds are static.
242 -----------------------------------------------------
243 -- Subprograms used for ARRAY AGGREGATE Processing --
244 -----------------------------------------------------
246 function Resolve_Array_Aggregate
247 (N : Node_Id;
248 Index : Node_Id;
249 Index_Constr : Node_Id;
250 Component_Typ : Entity_Id;
251 Others_Allowed : Boolean) return Boolean;
252 -- This procedure performs the semantic checks for an array aggregate.
253 -- True is returned if the aggregate resolution succeeds.
255 -- The procedure works by recursively checking each nested aggregate.
256 -- Specifically, after checking a sub-aggregate nested at the i-th level
257 -- we recursively check all the subaggregates at the i+1-st level (if any).
258 -- Note that for aggregates analysis and resolution go hand in hand.
259 -- Aggregate analysis has been delayed up to here and it is done while
260 -- resolving the aggregate.
262 -- N is the current N_Aggregate node to be checked.
264 -- Index is the index node corresponding to the array sub-aggregate that
265 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
266 -- corresponding index type (or subtype).
268 -- Index_Constr is the node giving the applicable index constraint if
269 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
270 -- contexts [...] that can be used to determine the bounds of the array
271 -- value specified by the aggregate". If Others_Allowed below is False
272 -- there is no applicable index constraint and this node is set to Index.
274 -- Component_Typ is the array component type.
276 -- Others_Allowed indicates whether an others choice is allowed
277 -- in the context where the top-level aggregate appeared.
279 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
281 -- 1. Make sure that the others choice, if present, is by itself and
282 -- appears last in the sub-aggregate. Check that we do not have
283 -- positional and named components in the array sub-aggregate (unless
284 -- the named association is an others choice). Finally if an others
285 -- choice is present, make sure it is allowed in the aggregate context.
287 -- 2. If the array sub-aggregate contains discrete_choices:
289 -- (A) Verify their validity. Specifically verify that:
291 -- (a) If a null range is present it must be the only possible
292 -- choice in the array aggregate.
294 -- (b) Ditto for a non static range.
296 -- (c) Ditto for a non static expression.
298 -- In addition this step analyzes and resolves each discrete_choice,
299 -- making sure that its type is the type of the corresponding Index.
300 -- If we are not at the lowest array aggregate level (in the case of
301 -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
302 -- recursively on each component expression. Otherwise, resolve the
303 -- bottom level component expressions against the expected component
304 -- type ONLY IF the component corresponds to a single discrete choice
305 -- which is not an others choice (to see why read the DELAYED
306 -- COMPONENT RESOLUTION below).
308 -- (B) Determine the bounds of the sub-aggregate and lowest and
309 -- highest choice values.
311 -- 3. For positional aggregates:
313 -- (A) Loop over the component expressions either recursively invoking
314 -- Resolve_Array_Aggregate on each of these for multi-dimensional
315 -- array aggregates or resolving the bottom level component
316 -- expressions against the expected component type.
318 -- (B) Determine the bounds of the positional sub-aggregates.
320 -- 4. Try to determine statically whether the evaluation of the array
321 -- sub-aggregate raises Constraint_Error. If yes emit proper
322 -- warnings. The precise checks are the following:
324 -- (A) Check that the index range defined by aggregate bounds is
325 -- compatible with corresponding index subtype.
326 -- We also check against the base type. In fact it could be that
327 -- Low/High bounds of the base type are static whereas those of
328 -- the index subtype are not. Thus if we can statically catch
329 -- a problem with respect to the base type we are guaranteed
330 -- that the same problem will arise with the index subtype
332 -- (B) If we are dealing with a named aggregate containing an others
333 -- choice and at least one discrete choice then make sure the range
334 -- specified by the discrete choices does not overflow the
335 -- aggregate bounds. We also check against the index type and base
336 -- type bounds for the same reasons given in (A).
338 -- (C) If we are dealing with a positional aggregate with an others
339 -- choice make sure the number of positional elements specified
340 -- does not overflow the aggregate bounds. We also check against
341 -- the index type and base type bounds as mentioned in (A).
343 -- Finally construct an N_Range node giving the sub-aggregate bounds.
344 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
345 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
346 -- to build the appropriate aggregate subtype. Aggregate_Bounds
347 -- information is needed during expansion.
349 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
350 -- expressions in an array aggregate may call Duplicate_Subexpr or some
351 -- other routine that inserts code just outside the outermost aggregate.
352 -- If the array aggregate contains discrete choices or an others choice,
353 -- this may be wrong. Consider for instance the following example.
355 -- type Rec is record
356 -- V : Integer := 0;
357 -- end record;
359 -- type Acc_Rec is access Rec;
360 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
362 -- Then the transformation of "new Rec" that occurs during resolution
363 -- entails the following code modifications
365 -- P7b : constant Acc_Rec := new Rec;
366 -- RecIP (P7b.all);
367 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
369 -- This code transformation is clearly wrong, since we need to call
370 -- "new Rec" for each of the 3 array elements. To avoid this problem we
371 -- delay resolution of the components of non positional array aggregates
372 -- to the expansion phase. As an optimization, if the discrete choice
373 -- specifies a single value we do not delay resolution.
375 function Array_Aggr_Subtype (N : Node_Id; Typ : Node_Id) return Entity_Id;
376 -- This routine returns the type or subtype of an array aggregate.
378 -- N is the array aggregate node whose type we return.
380 -- Typ is the context type in which N occurs.
382 -- This routine creates an implicit array subtype whose bounds are
383 -- those defined by the aggregate. When this routine is invoked
384 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
385 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
386 -- sub-aggregate bounds. When building the aggregate itype, this function
387 -- traverses the array aggregate N collecting such Aggregate_Bounds and
388 -- constructs the proper array aggregate itype.
390 -- Note that in the case of multidimensional aggregates each inner
391 -- sub-aggregate corresponding to a given array dimension, may provide a
392 -- different bounds. If it is possible to determine statically that
393 -- some sub-aggregates corresponding to the same index do not have the
394 -- same bounds, then a warning is emitted. If such check is not possible
395 -- statically (because some sub-aggregate bounds are dynamic expressions)
396 -- then this job is left to the expander. In all cases the particular
397 -- bounds that this function will chose for a given dimension is the first
398 -- N_Range node for a sub-aggregate corresponding to that dimension.
400 -- Note that the Raises_Constraint_Error flag of an array aggregate
401 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
402 -- is set in Resolve_Array_Aggregate but the aggregate is not
403 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
404 -- first construct the proper itype for the aggregate (Gigi needs
405 -- this). After constructing the proper itype we will eventually replace
406 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
407 -- Of course in cases such as:
409 -- type Arr is array (integer range <>) of Integer;
410 -- A : Arr := (positive range -1 .. 2 => 0);
412 -- The bounds of the aggregate itype are cooked up to look reasonable
413 -- (in this particular case the bounds will be 1 .. 2).
415 procedure Make_String_Into_Aggregate (N : Node_Id);
416 -- A string literal can appear in a context in which a one dimensional
417 -- array of characters is expected. This procedure simply rewrites the
418 -- string as an aggregate, prior to resolution.
420 ------------------------
421 -- Array_Aggr_Subtype --
422 ------------------------
424 function Array_Aggr_Subtype
425 (N : Node_Id;
426 Typ : Entity_Id) return Entity_Id
428 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
429 -- Number of aggregate index dimensions
431 Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
432 -- Constrained N_Range of each index dimension in our aggregate itype
434 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
435 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
436 -- Low and High bounds for each index dimension in our aggregate itype
438 Is_Fully_Positional : Boolean := True;
440 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
441 -- N is an array (sub-)aggregate. Dim is the dimension corresponding
442 -- to (sub-)aggregate N. This procedure collects and removes the side
443 -- effects of the constrained N_Range nodes corresponding to each index
444 -- dimension of our aggregate itype. These N_Range nodes are collected
445 -- in Aggr_Range above.
447 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
448 -- bounds of each index dimension. If, when collecting, two bounds
449 -- corresponding to the same dimension are static and found to differ,
450 -- then emit a warning, and mark N as raising Constraint_Error.
452 -------------------------
453 -- Collect_Aggr_Bounds --
454 -------------------------
456 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
457 This_Range : constant Node_Id := Aggregate_Bounds (N);
458 -- The aggregate range node of this specific sub-aggregate
460 This_Low : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
461 This_High : constant Node_Id := High_Bound (Aggregate_Bounds (N));
462 -- The aggregate bounds of this specific sub-aggregate
464 Assoc : Node_Id;
465 Expr : Node_Id;
467 begin
468 Remove_Side_Effects (This_Low, Variable_Ref => True);
469 Remove_Side_Effects (This_High, Variable_Ref => True);
471 -- Collect the first N_Range for a given dimension that you find.
472 -- For a given dimension they must be all equal anyway.
474 if No (Aggr_Range (Dim)) then
475 Aggr_Low (Dim) := This_Low;
476 Aggr_High (Dim) := This_High;
477 Aggr_Range (Dim) := This_Range;
479 else
480 if Compile_Time_Known_Value (This_Low) then
481 if not Compile_Time_Known_Value (Aggr_Low (Dim)) then
482 Aggr_Low (Dim) := This_Low;
484 elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
485 Set_Raises_Constraint_Error (N);
486 Error_Msg_Warn := SPARK_Mode /= On;
487 Error_Msg_N ("sub-aggregate low bound mismatch<<", N);
488 Error_Msg_N ("\Constraint_Error [<<", N);
489 end if;
490 end if;
492 if Compile_Time_Known_Value (This_High) then
493 if not Compile_Time_Known_Value (Aggr_High (Dim)) then
494 Aggr_High (Dim) := This_High;
496 elsif
497 Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
498 then
499 Set_Raises_Constraint_Error (N);
500 Error_Msg_Warn := SPARK_Mode /= On;
501 Error_Msg_N ("sub-aggregate high bound mismatch<<", N);
502 Error_Msg_N ("\Constraint_Error [<<", N);
503 end if;
504 end if;
505 end if;
507 if Dim < Aggr_Dimension then
509 -- Process positional components
511 if Present (Expressions (N)) then
512 Expr := First (Expressions (N));
513 while Present (Expr) loop
514 Collect_Aggr_Bounds (Expr, Dim + 1);
515 Next (Expr);
516 end loop;
517 end if;
519 -- Process component associations
521 if Present (Component_Associations (N)) then
522 Is_Fully_Positional := False;
524 Assoc := First (Component_Associations (N));
525 while Present (Assoc) loop
526 Expr := Expression (Assoc);
527 Collect_Aggr_Bounds (Expr, Dim + 1);
528 Next (Assoc);
529 end loop;
530 end if;
531 end if;
532 end Collect_Aggr_Bounds;
534 -- Array_Aggr_Subtype variables
536 Itype : Entity_Id;
537 -- The final itype of the overall aggregate
539 Index_Constraints : constant List_Id := New_List;
540 -- The list of index constraints of the aggregate itype
542 -- Start of processing for Array_Aggr_Subtype
544 begin
545 -- Make sure that the list of index constraints is properly attached to
546 -- the tree, and then collect the aggregate bounds.
548 Set_Parent (Index_Constraints, N);
549 Collect_Aggr_Bounds (N, 1);
551 -- Build the list of constrained indexes of our aggregate itype
553 for J in 1 .. Aggr_Dimension loop
554 Create_Index : declare
555 Index_Base : constant Entity_Id :=
556 Base_Type (Etype (Aggr_Range (J)));
557 Index_Typ : Entity_Id;
559 begin
560 -- Construct the Index subtype, and associate it with the range
561 -- construct that generates it.
563 Index_Typ :=
564 Create_Itype (Subtype_Kind (Ekind (Index_Base)), Aggr_Range (J));
566 Set_Etype (Index_Typ, Index_Base);
568 if Is_Character_Type (Index_Base) then
569 Set_Is_Character_Type (Index_Typ);
570 end if;
572 Set_Size_Info (Index_Typ, (Index_Base));
573 Set_RM_Size (Index_Typ, RM_Size (Index_Base));
574 Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base));
575 Set_Scalar_Range (Index_Typ, Aggr_Range (J));
577 if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then
578 Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ)));
579 end if;
581 Set_Etype (Aggr_Range (J), Index_Typ);
583 Append (Aggr_Range (J), To => Index_Constraints);
584 end Create_Index;
585 end loop;
587 -- Now build the Itype
589 Itype := Create_Itype (E_Array_Subtype, N);
591 Set_First_Rep_Item (Itype, First_Rep_Item (Typ));
592 Set_Convention (Itype, Convention (Typ));
593 Set_Depends_On_Private (Itype, Has_Private_Component (Typ));
594 Set_Etype (Itype, Base_Type (Typ));
595 Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ));
596 Set_Is_Aliased (Itype, Is_Aliased (Typ));
597 Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
599 Copy_Suppress_Status (Index_Check, Typ, Itype);
600 Copy_Suppress_Status (Length_Check, Typ, Itype);
602 Set_First_Index (Itype, First (Index_Constraints));
603 Set_Is_Constrained (Itype, True);
604 Set_Is_Internal (Itype, True);
606 -- A simple optimization: purely positional aggregates of static
607 -- components should be passed to gigi unexpanded whenever possible, and
608 -- regardless of the staticness of the bounds themselves. Subsequent
609 -- checks in exp_aggr verify that type is not packed, etc.
611 Set_Size_Known_At_Compile_Time
612 (Itype,
613 Is_Fully_Positional
614 and then Comes_From_Source (N)
615 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
617 -- We always need a freeze node for a packed array subtype, so that we
618 -- can build the Packed_Array_Impl_Type corresponding to the subtype. If
619 -- expansion is disabled, the packed array subtype is not built, and we
620 -- must not generate a freeze node for the type, or else it will appear
621 -- incomplete to gigi.
623 if Is_Packed (Itype)
624 and then not In_Spec_Expression
625 and then Expander_Active
626 then
627 Freeze_Itype (Itype, N);
628 end if;
630 return Itype;
631 end Array_Aggr_Subtype;
633 --------------------------------
634 -- Check_Misspelled_Component --
635 --------------------------------
637 procedure Check_Misspelled_Component
638 (Elements : Elist_Id;
639 Component : Node_Id)
641 Max_Suggestions : constant := 2;
643 Nr_Of_Suggestions : Natural := 0;
644 Suggestion_1 : Entity_Id := Empty;
645 Suggestion_2 : Entity_Id := Empty;
646 Component_Elmt : Elmt_Id;
648 begin
649 -- All the components of List are matched against Component and a count
650 -- is maintained of possible misspellings. When at the end of the
651 -- analysis there are one or two (not more) possible misspellings,
652 -- these misspellings will be suggested as possible corrections.
654 Component_Elmt := First_Elmt (Elements);
655 while Nr_Of_Suggestions <= Max_Suggestions
656 and then Present (Component_Elmt)
657 loop
658 if Is_Bad_Spelling_Of
659 (Chars (Node (Component_Elmt)),
660 Chars (Component))
661 then
662 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
664 case Nr_Of_Suggestions is
665 when 1 => Suggestion_1 := Node (Component_Elmt);
666 when 2 => Suggestion_2 := Node (Component_Elmt);
667 when others => null;
668 end case;
669 end if;
671 Next_Elmt (Component_Elmt);
672 end loop;
674 -- Report at most two suggestions
676 if Nr_Of_Suggestions = 1 then
677 Error_Msg_NE -- CODEFIX
678 ("\possible misspelling of&", Component, Suggestion_1);
680 elsif Nr_Of_Suggestions = 2 then
681 Error_Msg_Node_2 := Suggestion_2;
682 Error_Msg_NE -- CODEFIX
683 ("\possible misspelling of& or&", Component, Suggestion_1);
684 end if;
685 end Check_Misspelled_Component;
687 ----------------------------------------
688 -- Check_Expr_OK_In_Limited_Aggregate --
689 ----------------------------------------
691 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id) is
692 begin
693 if Is_Limited_Type (Etype (Expr))
694 and then Comes_From_Source (Expr)
695 then
696 if In_Instance_Body or else In_Inlined_Body then
697 null;
699 elsif not OK_For_Limited_Init (Etype (Expr), Expr) then
700 Error_Msg_N
701 ("initialization not allowed for limited types", Expr);
702 Explain_Limited_Type (Etype (Expr), Expr);
703 end if;
704 end if;
705 end Check_Expr_OK_In_Limited_Aggregate;
707 -------------------------------
708 -- Check_Qualified_Aggregate --
709 -------------------------------
711 procedure Check_Qualified_Aggregate (Level : Nat; Expr : Node_Id) is
712 Comp_Expr : Node_Id;
713 Comp_Assn : Node_Id;
715 begin
716 if Level = 0 then
717 if Nkind (Parent (Expr)) /= N_Qualified_Expression then
718 Check_SPARK_05_Restriction ("aggregate should be qualified", Expr);
719 end if;
721 else
722 Comp_Expr := First (Expressions (Expr));
723 while Present (Comp_Expr) loop
724 if Nkind (Comp_Expr) = N_Aggregate then
725 Check_Qualified_Aggregate (Level - 1, Comp_Expr);
726 end if;
728 Comp_Expr := Next (Comp_Expr);
729 end loop;
731 Comp_Assn := First (Component_Associations (Expr));
732 while Present (Comp_Assn) loop
733 Comp_Expr := Expression (Comp_Assn);
735 if Nkind (Comp_Expr) = N_Aggregate then
736 Check_Qualified_Aggregate (Level - 1, Comp_Expr);
737 end if;
739 Comp_Assn := Next (Comp_Assn);
740 end loop;
741 end if;
742 end Check_Qualified_Aggregate;
744 ----------------------------------------
745 -- Check_Static_Discriminated_Subtype --
746 ----------------------------------------
748 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id) is
749 Disc : constant Entity_Id := First_Discriminant (T);
750 Comp : Entity_Id;
751 Ind : Entity_Id;
753 begin
754 if Has_Record_Rep_Clause (T) then
755 return;
757 elsif Present (Next_Discriminant (Disc)) then
758 return;
760 elsif Nkind (V) /= N_Integer_Literal then
761 return;
762 end if;
764 Comp := First_Component (T);
765 while Present (Comp) loop
766 if Is_Scalar_Type (Etype (Comp)) then
767 null;
769 elsif Is_Private_Type (Etype (Comp))
770 and then Present (Full_View (Etype (Comp)))
771 and then Is_Scalar_Type (Full_View (Etype (Comp)))
772 then
773 null;
775 elsif Is_Array_Type (Etype (Comp)) then
776 if Is_Bit_Packed_Array (Etype (Comp)) then
777 return;
778 end if;
780 Ind := First_Index (Etype (Comp));
781 while Present (Ind) loop
782 if Nkind (Ind) /= N_Range
783 or else Nkind (Low_Bound (Ind)) /= N_Integer_Literal
784 or else Nkind (High_Bound (Ind)) /= N_Integer_Literal
785 then
786 return;
787 end if;
789 Next_Index (Ind);
790 end loop;
792 else
793 return;
794 end if;
796 Next_Component (Comp);
797 end loop;
799 -- On exit, all components have statically known sizes
801 Set_Size_Known_At_Compile_Time (T);
802 end Check_Static_Discriminated_Subtype;
804 -------------------------
805 -- Is_Others_Aggregate --
806 -------------------------
808 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean is
809 begin
810 return No (Expressions (Aggr))
811 and then
812 Nkind (First (Choice_List (First (Component_Associations (Aggr))))) =
813 N_Others_Choice;
814 end Is_Others_Aggregate;
816 ----------------------------
817 -- Is_Top_Level_Aggregate --
818 ----------------------------
820 function Is_Top_Level_Aggregate (Expr : Node_Id) return Boolean is
821 begin
822 return Nkind (Parent (Expr)) /= N_Aggregate
823 and then (Nkind (Parent (Expr)) /= N_Component_Association
824 or else Nkind (Parent (Parent (Expr))) /= N_Aggregate);
825 end Is_Top_Level_Aggregate;
827 --------------------------------
828 -- Make_String_Into_Aggregate --
829 --------------------------------
831 procedure Make_String_Into_Aggregate (N : Node_Id) is
832 Exprs : constant List_Id := New_List;
833 Loc : constant Source_Ptr := Sloc (N);
834 Str : constant String_Id := Strval (N);
835 Strlen : constant Nat := String_Length (Str);
836 C : Char_Code;
837 C_Node : Node_Id;
838 New_N : Node_Id;
839 P : Source_Ptr;
841 begin
842 P := Loc + 1;
843 for J in 1 .. Strlen loop
844 C := Get_String_Char (Str, J);
845 Set_Character_Literal_Name (C);
847 C_Node :=
848 Make_Character_Literal (P,
849 Chars => Name_Find,
850 Char_Literal_Value => UI_From_CC (C));
851 Set_Etype (C_Node, Any_Character);
852 Append_To (Exprs, C_Node);
854 P := P + 1;
855 -- Something special for wide strings???
856 end loop;
858 New_N := Make_Aggregate (Loc, Expressions => Exprs);
859 Set_Analyzed (New_N);
860 Set_Etype (New_N, Any_Composite);
862 Rewrite (N, New_N);
863 end Make_String_Into_Aggregate;
865 -----------------------
866 -- Resolve_Aggregate --
867 -----------------------
869 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
870 Loc : constant Source_Ptr := Sloc (N);
871 Pkind : constant Node_Kind := Nkind (Parent (N));
873 Aggr_Subtyp : Entity_Id;
874 -- The actual aggregate subtype. This is not necessarily the same as Typ
875 -- which is the subtype of the context in which the aggregate was found.
877 begin
878 -- Ignore junk empty aggregate resulting from parser error
880 if No (Expressions (N))
881 and then No (Component_Associations (N))
882 and then not Null_Record_Present (N)
883 then
884 return;
885 end if;
887 -- If the aggregate has box-initialized components, its type must be
888 -- frozen so that initialization procedures can properly be called
889 -- in the resolution that follows. The replacement of boxes with
890 -- initialization calls is properly an expansion activity but it must
891 -- be done during resolution.
893 if Expander_Active
894 and then Present (Component_Associations (N))
895 then
896 declare
897 Comp : Node_Id;
899 begin
900 Comp := First (Component_Associations (N));
901 while Present (Comp) loop
902 if Box_Present (Comp) then
903 Insert_Actions (N, Freeze_Entity (Typ, N));
904 exit;
905 end if;
907 Next (Comp);
908 end loop;
909 end;
910 end if;
912 -- An unqualified aggregate is restricted in SPARK to:
914 -- An aggregate item inside an aggregate for a multi-dimensional array
916 -- An expression being assigned to an unconstrained array, but only if
917 -- the aggregate specifies a value for OTHERS only.
919 if Nkind (Parent (N)) = N_Qualified_Expression then
920 if Is_Array_Type (Typ) then
921 Check_Qualified_Aggregate (Number_Dimensions (Typ), N);
922 else
923 Check_Qualified_Aggregate (1, N);
924 end if;
925 else
926 if Is_Array_Type (Typ)
927 and then Nkind (Parent (N)) = N_Assignment_Statement
928 and then not Is_Constrained (Etype (Name (Parent (N))))
929 then
930 if not Is_Others_Aggregate (N) then
931 Check_SPARK_05_Restriction
932 ("array aggregate should have only OTHERS", N);
933 end if;
935 elsif Is_Top_Level_Aggregate (N) then
936 Check_SPARK_05_Restriction ("aggregate should be qualified", N);
938 -- The legality of this unqualified aggregate is checked by calling
939 -- Check_Qualified_Aggregate from one of its enclosing aggregate,
940 -- unless one of these already causes an error to be issued.
942 else
943 null;
944 end if;
945 end if;
947 -- Check for aggregates not allowed in configurable run-time mode.
948 -- We allow all cases of aggregates that do not come from source, since
949 -- these are all assumed to be small (e.g. bounds of a string literal).
950 -- We also allow aggregates of types we know to be small.
952 if not Support_Aggregates_On_Target
953 and then Comes_From_Source (N)
954 and then (not Known_Static_Esize (Typ) or else Esize (Typ) > 64)
955 then
956 Error_Msg_CRT ("aggregate", N);
957 end if;
959 -- Ada 2005 (AI-287): Limited aggregates allowed
961 -- In an instance, ignore aggregate subcomponents tnat may be limited,
962 -- because they originate in view conflicts. If the original aggregate
963 -- is legal and the actuals are legal, the aggregate itself is legal.
965 if Is_Limited_Type (Typ)
966 and then Ada_Version < Ada_2005
967 and then not In_Instance
968 then
969 Error_Msg_N ("aggregate type cannot be limited", N);
970 Explain_Limited_Type (Typ, N);
972 elsif Is_Class_Wide_Type (Typ) then
973 Error_Msg_N ("type of aggregate cannot be class-wide", N);
975 elsif Typ = Any_String
976 or else Typ = Any_Composite
977 then
978 Error_Msg_N ("no unique type for aggregate", N);
979 Set_Etype (N, Any_Composite);
981 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
982 Error_Msg_N ("null record forbidden in array aggregate", N);
984 elsif Is_Record_Type (Typ) then
985 Resolve_Record_Aggregate (N, Typ);
987 elsif Is_Array_Type (Typ) then
989 -- First a special test, for the case of a positional aggregate
990 -- of characters which can be replaced by a string literal.
992 -- Do not perform this transformation if this was a string literal to
993 -- start with, whose components needed constraint checks, or if the
994 -- component type is non-static, because it will require those checks
995 -- and be transformed back into an aggregate.
997 if Number_Dimensions (Typ) = 1
998 and then Is_Standard_Character_Type (Component_Type (Typ))
999 and then No (Component_Associations (N))
1000 and then not Is_Limited_Composite (Typ)
1001 and then not Is_Private_Composite (Typ)
1002 and then not Is_Bit_Packed_Array (Typ)
1003 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
1004 and then Is_OK_Static_Subtype (Component_Type (Typ))
1005 then
1006 declare
1007 Expr : Node_Id;
1009 begin
1010 Expr := First (Expressions (N));
1011 while Present (Expr) loop
1012 exit when Nkind (Expr) /= N_Character_Literal;
1013 Next (Expr);
1014 end loop;
1016 if No (Expr) then
1017 Start_String;
1019 Expr := First (Expressions (N));
1020 while Present (Expr) loop
1021 Store_String_Char (UI_To_CC (Char_Literal_Value (Expr)));
1022 Next (Expr);
1023 end loop;
1025 Rewrite (N, Make_String_Literal (Loc, End_String));
1027 Analyze_And_Resolve (N, Typ);
1028 return;
1029 end if;
1030 end;
1031 end if;
1033 -- Here if we have a real aggregate to deal with
1035 Array_Aggregate : declare
1036 Aggr_Resolved : Boolean;
1038 Aggr_Typ : constant Entity_Id := Etype (Typ);
1039 -- This is the unconstrained array type, which is the type against
1040 -- which the aggregate is to be resolved. Typ itself is the array
1041 -- type of the context which may not be the same subtype as the
1042 -- subtype for the final aggregate.
1044 begin
1045 -- In the following we determine whether an OTHERS choice is
1046 -- allowed inside the array aggregate. The test checks the context
1047 -- in which the array aggregate occurs. If the context does not
1048 -- permit it, or the aggregate type is unconstrained, an OTHERS
1049 -- choice is not allowed (except that it is always allowed on the
1050 -- right-hand side of an assignment statement; in this case the
1051 -- constrainedness of the type doesn't matter).
1053 -- If expansion is disabled (generic context, or semantics-only
1054 -- mode) actual subtypes cannot be constructed, and the type of an
1055 -- object may be its unconstrained nominal type. However, if the
1056 -- context is an assignment, we assume that OTHERS is allowed,
1057 -- because the target of the assignment will have a constrained
1058 -- subtype when fully compiled.
1060 -- Note that there is no node for Explicit_Actual_Parameter.
1061 -- To test for this context we therefore have to test for node
1062 -- N_Parameter_Association which itself appears only if there is a
1063 -- formal parameter. Consequently we also need to test for
1064 -- N_Procedure_Call_Statement or N_Function_Call.
1066 -- The context may be an N_Reference node, created by expansion.
1067 -- Legality of the others clause was established in the source,
1068 -- so the context is legal.
1070 Set_Etype (N, Aggr_Typ); -- May be overridden later on
1072 if Pkind = N_Assignment_Statement
1073 or else (Is_Constrained (Typ)
1074 and then
1075 (Pkind = N_Parameter_Association or else
1076 Pkind = N_Function_Call or else
1077 Pkind = N_Procedure_Call_Statement or else
1078 Pkind = N_Generic_Association or else
1079 Pkind = N_Formal_Object_Declaration or else
1080 Pkind = N_Simple_Return_Statement or else
1081 Pkind = N_Object_Declaration or else
1082 Pkind = N_Component_Declaration or else
1083 Pkind = N_Parameter_Specification or else
1084 Pkind = N_Qualified_Expression or else
1085 Pkind = N_Reference or else
1086 Pkind = N_Aggregate or else
1087 Pkind = N_Extension_Aggregate or else
1088 Pkind = N_Component_Association))
1089 then
1090 Aggr_Resolved :=
1091 Resolve_Array_Aggregate
1093 Index => First_Index (Aggr_Typ),
1094 Index_Constr => First_Index (Typ),
1095 Component_Typ => Component_Type (Typ),
1096 Others_Allowed => True);
1097 else
1098 Aggr_Resolved :=
1099 Resolve_Array_Aggregate
1101 Index => First_Index (Aggr_Typ),
1102 Index_Constr => First_Index (Aggr_Typ),
1103 Component_Typ => Component_Type (Typ),
1104 Others_Allowed => False);
1105 end if;
1107 if not Aggr_Resolved then
1109 -- A parenthesized expression may have been intended as an
1110 -- aggregate, leading to a type error when analyzing the
1111 -- component. This can also happen for a nested component
1112 -- (see Analyze_Aggr_Expr).
1114 if Paren_Count (N) > 0 then
1115 Error_Msg_N
1116 ("positional aggregate cannot have one component", N);
1117 end if;
1119 Aggr_Subtyp := Any_Composite;
1121 else
1122 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1123 end if;
1125 Set_Etype (N, Aggr_Subtyp);
1126 end Array_Aggregate;
1128 elsif Is_Private_Type (Typ)
1129 and then Present (Full_View (Typ))
1130 and then (In_Inlined_Body or In_Instance_Body)
1131 and then Is_Composite_Type (Full_View (Typ))
1132 then
1133 Resolve (N, Full_View (Typ));
1135 else
1136 Error_Msg_N ("illegal context for aggregate", N);
1137 end if;
1139 -- If we can determine statically that the evaluation of the aggregate
1140 -- raises Constraint_Error, then replace the aggregate with an
1141 -- N_Raise_Constraint_Error node, but set the Etype to the right
1142 -- aggregate subtype. Gigi needs this.
1144 if Raises_Constraint_Error (N) then
1145 Aggr_Subtyp := Etype (N);
1146 Rewrite (N,
1147 Make_Raise_Constraint_Error (Loc, Reason => CE_Range_Check_Failed));
1148 Set_Raises_Constraint_Error (N);
1149 Set_Etype (N, Aggr_Subtyp);
1150 Set_Analyzed (N);
1151 end if;
1153 Check_Function_Writable_Actuals (N);
1154 end Resolve_Aggregate;
1156 -----------------------------
1157 -- Resolve_Array_Aggregate --
1158 -----------------------------
1160 function Resolve_Array_Aggregate
1161 (N : Node_Id;
1162 Index : Node_Id;
1163 Index_Constr : Node_Id;
1164 Component_Typ : Entity_Id;
1165 Others_Allowed : Boolean) return Boolean
1167 Loc : constant Source_Ptr := Sloc (N);
1169 Failure : constant Boolean := False;
1170 Success : constant Boolean := True;
1172 Index_Typ : constant Entity_Id := Etype (Index);
1173 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1174 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1175 -- The type of the index corresponding to the array sub-aggregate along
1176 -- with its low and upper bounds.
1178 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1179 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1180 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1181 -- Ditto for the base type
1183 Others_Present : Boolean := False;
1185 Nb_Choices : Nat := 0;
1186 -- Contains the overall number of named choices in this sub-aggregate
1188 function Add (Val : Uint; To : Node_Id) return Node_Id;
1189 -- Creates a new expression node where Val is added to expression To.
1190 -- Tries to constant fold whenever possible. To must be an already
1191 -- analyzed expression.
1193 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1194 -- Checks that AH (the upper bound of an array aggregate) is less than
1195 -- or equal to BH (the upper bound of the index base type). If the check
1196 -- fails, a warning is emitted, the Raises_Constraint_Error flag of N is
1197 -- set, and AH is replaced with a duplicate of BH.
1199 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1200 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1201 -- warning if not and sets the Raises_Constraint_Error flag in N.
1203 procedure Check_Length (L, H : Node_Id; Len : Uint);
1204 -- Checks that range L .. H contains at least Len elements. Emits a
1205 -- warning if not and sets the Raises_Constraint_Error flag in N.
1207 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1208 -- Returns True if range L .. H is dynamic or null
1210 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1211 -- Given expression node From, this routine sets OK to False if it
1212 -- cannot statically evaluate From. Otherwise it stores this static
1213 -- value into Value.
1215 function Resolve_Aggr_Expr
1216 (Expr : Node_Id;
1217 Single_Elmt : Boolean) return Boolean;
1218 -- Resolves aggregate expression Expr. Returns False if resolution
1219 -- fails. If Single_Elmt is set to False, the expression Expr may be
1220 -- used to initialize several array aggregate elements (this can happen
1221 -- for discrete choices such as "L .. H => Expr" or the OTHERS choice).
1222 -- In this event we do not resolve Expr unless expansion is disabled.
1223 -- To know why, see the DELAYED COMPONENT RESOLUTION note above.
1225 -- NOTE: In the case of "... => <>", we pass the in the
1226 -- N_Component_Association node as Expr, since there is no Expression in
1227 -- that case, and we need a Sloc for the error message.
1229 procedure Resolve_Iterated_Component_Association
1230 (N : Node_Id;
1231 Index_Typ : Entity_Id);
1232 -- For AI12-061
1234 ---------
1235 -- Add --
1236 ---------
1238 function Add (Val : Uint; To : Node_Id) return Node_Id is
1239 Expr_Pos : Node_Id;
1240 Expr : Node_Id;
1241 To_Pos : Node_Id;
1243 begin
1244 if Raises_Constraint_Error (To) then
1245 return To;
1246 end if;
1248 -- First test if we can do constant folding
1250 if Compile_Time_Known_Value (To)
1251 or else Nkind (To) = N_Integer_Literal
1252 then
1253 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1254 Set_Is_Static_Expression (Expr_Pos);
1255 Set_Etype (Expr_Pos, Etype (To));
1256 Set_Analyzed (Expr_Pos, Analyzed (To));
1258 if not Is_Enumeration_Type (Index_Typ) then
1259 Expr := Expr_Pos;
1261 -- If we are dealing with enumeration return
1262 -- Index_Typ'Val (Expr_Pos)
1264 else
1265 Expr :=
1266 Make_Attribute_Reference
1267 (Loc,
1268 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1269 Attribute_Name => Name_Val,
1270 Expressions => New_List (Expr_Pos));
1271 end if;
1273 return Expr;
1274 end if;
1276 -- If we are here no constant folding possible
1278 if not Is_Enumeration_Type (Index_Base) then
1279 Expr :=
1280 Make_Op_Add (Loc,
1281 Left_Opnd => Duplicate_Subexpr (To),
1282 Right_Opnd => Make_Integer_Literal (Loc, Val));
1284 -- If we are dealing with enumeration return
1285 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1287 else
1288 To_Pos :=
1289 Make_Attribute_Reference
1290 (Loc,
1291 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1292 Attribute_Name => Name_Pos,
1293 Expressions => New_List (Duplicate_Subexpr (To)));
1295 Expr_Pos :=
1296 Make_Op_Add (Loc,
1297 Left_Opnd => To_Pos,
1298 Right_Opnd => Make_Integer_Literal (Loc, Val));
1300 Expr :=
1301 Make_Attribute_Reference
1302 (Loc,
1303 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1304 Attribute_Name => Name_Val,
1305 Expressions => New_List (Expr_Pos));
1307 -- If the index type has a non standard representation, the
1308 -- attributes 'Val and 'Pos expand into function calls and the
1309 -- resulting expression is considered non-safe for reevaluation
1310 -- by the backend. Relocate it into a constant temporary in order
1311 -- to make it safe for reevaluation.
1313 if Has_Non_Standard_Rep (Etype (N)) then
1314 declare
1315 Def_Id : Entity_Id;
1317 begin
1318 Def_Id := Make_Temporary (Loc, 'R', Expr);
1319 Set_Etype (Def_Id, Index_Typ);
1320 Insert_Action (N,
1321 Make_Object_Declaration (Loc,
1322 Defining_Identifier => Def_Id,
1323 Object_Definition =>
1324 New_Occurrence_Of (Index_Typ, Loc),
1325 Constant_Present => True,
1326 Expression => Relocate_Node (Expr)));
1328 Expr := New_Occurrence_Of (Def_Id, Loc);
1329 end;
1330 end if;
1331 end if;
1333 return Expr;
1334 end Add;
1336 -----------------
1337 -- Check_Bound --
1338 -----------------
1340 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1341 Val_BH : Uint;
1342 Val_AH : Uint;
1344 OK_BH : Boolean;
1345 OK_AH : Boolean;
1347 begin
1348 Get (Value => Val_BH, From => BH, OK => OK_BH);
1349 Get (Value => Val_AH, From => AH, OK => OK_AH);
1351 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1352 Set_Raises_Constraint_Error (N);
1353 Error_Msg_Warn := SPARK_Mode /= On;
1354 Error_Msg_N ("upper bound out of range<<", AH);
1355 Error_Msg_N ("\Constraint_Error [<<", AH);
1357 -- You need to set AH to BH or else in the case of enumerations
1358 -- indexes we will not be able to resolve the aggregate bounds.
1360 AH := Duplicate_Subexpr (BH);
1361 end if;
1362 end Check_Bound;
1364 ------------------
1365 -- Check_Bounds --
1366 ------------------
1368 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1369 Val_L : Uint;
1370 Val_H : Uint;
1371 Val_AL : Uint;
1372 Val_AH : Uint;
1374 OK_L : Boolean;
1375 OK_H : Boolean;
1377 OK_AL : Boolean;
1378 OK_AH : Boolean;
1379 pragma Warnings (Off, OK_AL);
1380 pragma Warnings (Off, OK_AH);
1382 begin
1383 if Raises_Constraint_Error (N)
1384 or else Dynamic_Or_Null_Range (AL, AH)
1385 then
1386 return;
1387 end if;
1389 Get (Value => Val_L, From => L, OK => OK_L);
1390 Get (Value => Val_H, From => H, OK => OK_H);
1392 Get (Value => Val_AL, From => AL, OK => OK_AL);
1393 Get (Value => Val_AH, From => AH, OK => OK_AH);
1395 if OK_L and then Val_L > Val_AL then
1396 Set_Raises_Constraint_Error (N);
1397 Error_Msg_Warn := SPARK_Mode /= On;
1398 Error_Msg_N ("lower bound of aggregate out of range<<", N);
1399 Error_Msg_N ("\Constraint_Error [<<", N);
1400 end if;
1402 if OK_H and then Val_H < Val_AH then
1403 Set_Raises_Constraint_Error (N);
1404 Error_Msg_Warn := SPARK_Mode /= On;
1405 Error_Msg_N ("upper bound of aggregate out of range<<", N);
1406 Error_Msg_N ("\Constraint_Error [<<", N);
1407 end if;
1408 end Check_Bounds;
1410 ------------------
1411 -- Check_Length --
1412 ------------------
1414 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1415 Val_L : Uint;
1416 Val_H : Uint;
1418 OK_L : Boolean;
1419 OK_H : Boolean;
1421 Range_Len : Uint;
1423 begin
1424 if Raises_Constraint_Error (N) then
1425 return;
1426 end if;
1428 Get (Value => Val_L, From => L, OK => OK_L);
1429 Get (Value => Val_H, From => H, OK => OK_H);
1431 if not OK_L or else not OK_H then
1432 return;
1433 end if;
1435 -- If null range length is zero
1437 if Val_L > Val_H then
1438 Range_Len := Uint_0;
1439 else
1440 Range_Len := Val_H - Val_L + 1;
1441 end if;
1443 if Range_Len < Len then
1444 Set_Raises_Constraint_Error (N);
1445 Error_Msg_Warn := SPARK_Mode /= On;
1446 Error_Msg_N ("too many elements<<", N);
1447 Error_Msg_N ("\Constraint_Error [<<", N);
1448 end if;
1449 end Check_Length;
1451 ---------------------------
1452 -- Dynamic_Or_Null_Range --
1453 ---------------------------
1455 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1456 Val_L : Uint;
1457 Val_H : Uint;
1459 OK_L : Boolean;
1460 OK_H : Boolean;
1462 begin
1463 Get (Value => Val_L, From => L, OK => OK_L);
1464 Get (Value => Val_H, From => H, OK => OK_H);
1466 return not OK_L or else not OK_H
1467 or else not Is_OK_Static_Expression (L)
1468 or else not Is_OK_Static_Expression (H)
1469 or else Val_L > Val_H;
1470 end Dynamic_Or_Null_Range;
1472 ---------
1473 -- Get --
1474 ---------
1476 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1477 begin
1478 OK := True;
1480 if Compile_Time_Known_Value (From) then
1481 Value := Expr_Value (From);
1483 -- If expression From is something like Some_Type'Val (10) then
1484 -- Value = 10.
1486 elsif Nkind (From) = N_Attribute_Reference
1487 and then Attribute_Name (From) = Name_Val
1488 and then Compile_Time_Known_Value (First (Expressions (From)))
1489 then
1490 Value := Expr_Value (First (Expressions (From)));
1491 else
1492 Value := Uint_0;
1493 OK := False;
1494 end if;
1495 end Get;
1497 -----------------------
1498 -- Resolve_Aggr_Expr --
1499 -----------------------
1501 function Resolve_Aggr_Expr
1502 (Expr : Node_Id;
1503 Single_Elmt : Boolean) return Boolean
1505 Nxt_Ind : constant Node_Id := Next_Index (Index);
1506 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
1507 -- Index is the current index corresponding to the expression
1509 Resolution_OK : Boolean := True;
1510 -- Set to False if resolution of the expression failed
1512 begin
1513 -- Defend against previous errors
1515 if Nkind (Expr) = N_Error
1516 or else Error_Posted (Expr)
1517 then
1518 return True;
1519 end if;
1521 -- If the array type against which we are resolving the aggregate
1522 -- has several dimensions, the expressions nested inside the
1523 -- aggregate must be further aggregates (or strings).
1525 if Present (Nxt_Ind) then
1526 if Nkind (Expr) /= N_Aggregate then
1528 -- A string literal can appear where a one-dimensional array
1529 -- of characters is expected. If the literal looks like an
1530 -- operator, it is still an operator symbol, which will be
1531 -- transformed into a string when analyzed.
1533 if Is_Character_Type (Component_Typ)
1534 and then No (Next_Index (Nxt_Ind))
1535 and then Nkind_In (Expr, N_String_Literal, N_Operator_Symbol)
1536 then
1537 -- A string literal used in a multidimensional array
1538 -- aggregate in place of the final one-dimensional
1539 -- aggregate must not be enclosed in parentheses.
1541 if Paren_Count (Expr) /= 0 then
1542 Error_Msg_N ("no parenthesis allowed here", Expr);
1543 end if;
1545 Make_String_Into_Aggregate (Expr);
1547 else
1548 Error_Msg_N ("nested array aggregate expected", Expr);
1550 -- If the expression is parenthesized, this may be
1551 -- a missing component association for a 1-aggregate.
1553 if Paren_Count (Expr) > 0 then
1554 Error_Msg_N
1555 ("\if single-component aggregate is intended, "
1556 & "write e.g. (1 ='> ...)", Expr);
1557 end if;
1559 return Failure;
1560 end if;
1561 end if;
1563 -- If it's "... => <>", nothing to resolve
1565 if Nkind (Expr) = N_Component_Association then
1566 pragma Assert (Box_Present (Expr));
1567 return Success;
1568 end if;
1570 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
1571 -- Required to check the null-exclusion attribute (if present).
1572 -- This value may be overridden later on.
1574 Set_Etype (Expr, Etype (N));
1576 Resolution_OK := Resolve_Array_Aggregate
1577 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1579 else
1580 -- If it's "... => <>", nothing to resolve
1582 if Nkind (Expr) = N_Component_Association then
1583 pragma Assert (Box_Present (Expr));
1584 return Success;
1585 end if;
1587 -- Do not resolve the expressions of discrete or others choices
1588 -- unless the expression covers a single component, or the
1589 -- expander is inactive.
1591 -- In SPARK mode, expressions that can perform side-effects will
1592 -- be recognized by the gnat2why back-end, and the whole
1593 -- subprogram will be ignored. So semantic analysis can be
1594 -- performed safely.
1596 if Single_Elmt
1597 or else not Expander_Active
1598 or else In_Spec_Expression
1599 then
1600 Analyze_And_Resolve (Expr, Component_Typ);
1601 Check_Expr_OK_In_Limited_Aggregate (Expr);
1602 Check_Non_Static_Context (Expr);
1603 Aggregate_Constraint_Checks (Expr, Component_Typ);
1604 Check_Unset_Reference (Expr);
1605 end if;
1606 end if;
1608 -- If an aggregate component has a type with predicates, an explicit
1609 -- predicate check must be applied, as for an assignment statement,
1610 -- because the aggegate might not be expanded into individual
1611 -- component assignments. If the expression covers several components
1612 -- the analysis and the predicate check take place later.
1614 if Present (Predicate_Function (Component_Typ))
1615 and then Analyzed (Expr)
1616 then
1617 Apply_Predicate_Check (Expr, Component_Typ);
1618 end if;
1620 if Raises_Constraint_Error (Expr)
1621 and then Nkind (Parent (Expr)) /= N_Component_Association
1622 then
1623 Set_Raises_Constraint_Error (N);
1624 end if;
1626 -- If the expression has been marked as requiring a range check,
1627 -- then generate it here. It's a bit odd to be generating such
1628 -- checks in the analyzer, but harmless since Generate_Range_Check
1629 -- does nothing (other than making sure Do_Range_Check is set) if
1630 -- the expander is not active.
1632 if Do_Range_Check (Expr) then
1633 Generate_Range_Check (Expr, Component_Typ, CE_Range_Check_Failed);
1634 end if;
1636 return Resolution_OK;
1637 end Resolve_Aggr_Expr;
1639 --------------------------------------------
1640 -- Resolve_Iterated_Component_Association --
1641 --------------------------------------------
1643 procedure Resolve_Iterated_Component_Association
1644 (N : Node_Id;
1645 Index_Typ : Entity_Id)
1647 Id : constant Entity_Id := Defining_Identifier (N);
1648 Loc : constant Source_Ptr := Sloc (N);
1650 Choice : Node_Id;
1651 Dummy : Boolean;
1652 Ent : Entity_Id;
1654 begin
1655 Choice := First (Discrete_Choices (N));
1657 while Present (Choice) loop
1658 if Nkind (Choice) = N_Others_Choice then
1659 Others_Present := True;
1661 else
1662 Analyze_And_Resolve (Choice, Index_Typ);
1663 end if;
1665 Next (Choice);
1666 end loop;
1668 -- Create a scope in which to introduce an index, which is usually
1669 -- visible in the expression for the component, and needed for its
1670 -- analysis.
1672 Ent := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L');
1673 Set_Etype (Ent, Standard_Void_Type);
1674 Set_Parent (Ent, Parent (N));
1676 Enter_Name (Id);
1677 Set_Etype (Id, Index_Typ);
1678 Set_Ekind (Id, E_Variable);
1679 Set_Scope (Id, Ent);
1681 Push_Scope (Ent);
1682 Dummy := Resolve_Aggr_Expr (Expression (N), False);
1683 End_Scope;
1684 end Resolve_Iterated_Component_Association;
1686 -- Local variables
1688 Assoc : Node_Id;
1689 Choice : Node_Id;
1690 Expr : Node_Id;
1691 Discard : Node_Id;
1693 Aggr_Low : Node_Id := Empty;
1694 Aggr_High : Node_Id := Empty;
1695 -- The actual low and high bounds of this sub-aggregate
1697 Case_Table_Size : Nat;
1698 -- Contains the size of the case table needed to sort aggregate choices
1700 Choices_Low : Node_Id := Empty;
1701 Choices_High : Node_Id := Empty;
1702 -- The lowest and highest discrete choices values for a named aggregate
1704 Delete_Choice : Boolean;
1705 -- Used when replacing a subtype choice with predicate by a list
1707 Nb_Elements : Uint := Uint_0;
1708 -- The number of elements in a positional aggregate
1710 Nb_Discrete_Choices : Nat := 0;
1711 -- The overall number of discrete choices (not counting others choice)
1713 -- Start of processing for Resolve_Array_Aggregate
1715 begin
1716 -- Ignore junk empty aggregate resulting from parser error
1718 if No (Expressions (N))
1719 and then No (Component_Associations (N))
1720 and then not Null_Record_Present (N)
1721 then
1722 return False;
1723 end if;
1725 -- STEP 1: make sure the aggregate is correctly formatted
1727 if Present (Component_Associations (N)) then
1728 Assoc := First (Component_Associations (N));
1729 while Present (Assoc) loop
1730 if Nkind (Assoc) = N_Iterated_Component_Association then
1731 Resolve_Iterated_Component_Association (Assoc, Index_Typ);
1732 end if;
1734 Choice := First (Choice_List (Assoc));
1735 Delete_Choice := False;
1736 while Present (Choice) loop
1737 if Nkind (Choice) = N_Others_Choice then
1738 Others_Present := True;
1740 if Choice /= First (Choice_List (Assoc))
1741 or else Present (Next (Choice))
1742 then
1743 Error_Msg_N
1744 ("OTHERS must appear alone in a choice list", Choice);
1745 return Failure;
1746 end if;
1748 if Present (Next (Assoc)) then
1749 Error_Msg_N
1750 ("OTHERS must appear last in an aggregate", Choice);
1751 return Failure;
1752 end if;
1754 if Ada_Version = Ada_83
1755 and then Assoc /= First (Component_Associations (N))
1756 and then Nkind_In (Parent (N), N_Assignment_Statement,
1757 N_Object_Declaration)
1758 then
1759 Error_Msg_N
1760 ("(Ada 83) illegal context for OTHERS choice", N);
1761 end if;
1763 elsif Is_Entity_Name (Choice) then
1764 Analyze (Choice);
1766 declare
1767 E : constant Entity_Id := Entity (Choice);
1768 New_Cs : List_Id;
1769 P : Node_Id;
1770 C : Node_Id;
1772 begin
1773 if Is_Type (E) and then Has_Predicates (E) then
1774 Freeze_Before (N, E);
1776 if Has_Dynamic_Predicate_Aspect (E) then
1777 Error_Msg_NE
1778 ("subtype& has dynamic predicate, not allowed "
1779 & "in aggregate choice", Choice, E);
1781 elsif not Is_OK_Static_Subtype (E) then
1782 Error_Msg_NE
1783 ("non-static subtype& has predicate, not allowed "
1784 & "in aggregate choice", Choice, E);
1785 end if;
1787 -- If the subtype has a static predicate, replace the
1788 -- original choice with the list of individual values
1789 -- covered by the predicate. Do not perform this
1790 -- transformation if we need to preserve the source
1791 -- for ASIS use.
1792 -- This should be deferred to expansion time ???
1794 if Present (Static_Discrete_Predicate (E))
1795 and then not ASIS_Mode
1796 then
1797 Delete_Choice := True;
1799 New_Cs := New_List;
1800 P := First (Static_Discrete_Predicate (E));
1801 while Present (P) loop
1802 C := New_Copy (P);
1803 Set_Sloc (C, Sloc (Choice));
1804 Append_To (New_Cs, C);
1805 Next (P);
1806 end loop;
1808 Insert_List_After (Choice, New_Cs);
1809 end if;
1810 end if;
1811 end;
1812 end if;
1814 Nb_Choices := Nb_Choices + 1;
1816 declare
1817 C : constant Node_Id := Choice;
1819 begin
1820 Next (Choice);
1822 if Delete_Choice then
1823 Remove (C);
1824 Nb_Choices := Nb_Choices - 1;
1825 Delete_Choice := False;
1826 end if;
1827 end;
1828 end loop;
1830 Next (Assoc);
1831 end loop;
1832 end if;
1834 -- At this point we know that the others choice, if present, is by
1835 -- itself and appears last in the aggregate. Check if we have mixed
1836 -- positional and discrete associations (other than the others choice).
1838 if Present (Expressions (N))
1839 and then (Nb_Choices > 1
1840 or else (Nb_Choices = 1 and then not Others_Present))
1841 then
1842 Error_Msg_N
1843 ("named association cannot follow positional association",
1844 First (Choice_List (First (Component_Associations (N)))));
1845 return Failure;
1846 end if;
1848 -- Test for the validity of an others choice if present
1850 if Others_Present and then not Others_Allowed then
1851 Error_Msg_N
1852 ("OTHERS choice not allowed here",
1853 First (Choices (First (Component_Associations (N)))));
1854 return Failure;
1855 end if;
1857 -- Protect against cascaded errors
1859 if Etype (Index_Typ) = Any_Type then
1860 return Failure;
1861 end if;
1863 -- STEP 2: Process named components
1865 if No (Expressions (N)) then
1866 if Others_Present then
1867 Case_Table_Size := Nb_Choices - 1;
1868 else
1869 Case_Table_Size := Nb_Choices;
1870 end if;
1872 Step_2 : declare
1873 function Empty_Range (A : Node_Id) return Boolean;
1874 -- If an association covers an empty range, some warnings on the
1875 -- expression of the association can be disabled.
1877 -----------------
1878 -- Empty_Range --
1879 -----------------
1881 function Empty_Range (A : Node_Id) return Boolean is
1882 R : constant Node_Id := First (Choices (A));
1883 begin
1884 return No (Next (R))
1885 and then Nkind (R) = N_Range
1886 and then Compile_Time_Compare
1887 (Low_Bound (R), High_Bound (R), False) = GT;
1888 end Empty_Range;
1890 -- Local variables
1892 Low : Node_Id;
1893 High : Node_Id;
1894 -- Denote the lowest and highest values in an aggregate choice
1896 S_Low : Node_Id := Empty;
1897 S_High : Node_Id := Empty;
1898 -- if a choice in an aggregate is a subtype indication these
1899 -- denote the lowest and highest values of the subtype
1901 Table : Case_Table_Type (0 .. Case_Table_Size);
1902 -- Used to sort all the different choice values. Entry zero is
1903 -- reserved for sorting purposes.
1905 Single_Choice : Boolean;
1906 -- Set to true every time there is a single discrete choice in a
1907 -- discrete association
1909 Prev_Nb_Discrete_Choices : Nat;
1910 -- Used to keep track of the number of discrete choices in the
1911 -- current association.
1913 Errors_Posted_On_Choices : Boolean := False;
1914 -- Keeps track of whether any choices have semantic errors
1916 -- Start of processing for Step_2
1918 begin
1919 -- STEP 2 (A): Check discrete choices validity
1921 Assoc := First (Component_Associations (N));
1922 while Present (Assoc) loop
1923 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
1924 Choice := First (Choice_List (Assoc));
1926 loop
1927 Analyze (Choice);
1929 if Nkind (Choice) = N_Others_Choice then
1930 Single_Choice := False;
1931 exit;
1933 -- Test for subtype mark without constraint
1935 elsif Is_Entity_Name (Choice) and then
1936 Is_Type (Entity (Choice))
1937 then
1938 if Base_Type (Entity (Choice)) /= Index_Base then
1939 Error_Msg_N
1940 ("invalid subtype mark in aggregate choice",
1941 Choice);
1942 return Failure;
1943 end if;
1945 -- Case of subtype indication
1947 elsif Nkind (Choice) = N_Subtype_Indication then
1948 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
1950 if Has_Dynamic_Predicate_Aspect
1951 (Entity (Subtype_Mark (Choice)))
1952 then
1953 Error_Msg_NE
1954 ("subtype& has dynamic predicate, "
1955 & "not allowed in aggregate choice",
1956 Choice, Entity (Subtype_Mark (Choice)));
1957 end if;
1959 -- Does the subtype indication evaluation raise CE?
1961 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
1962 Get_Index_Bounds (Choice, Low, High);
1963 Check_Bounds (S_Low, S_High, Low, High);
1965 -- Case of range or expression
1967 else
1968 Resolve (Choice, Index_Base);
1969 Check_Unset_Reference (Choice);
1970 Check_Non_Static_Context (Choice);
1972 -- If semantic errors were posted on the choice, then
1973 -- record that for possible early return from later
1974 -- processing (see handling of enumeration choices).
1976 if Error_Posted (Choice) then
1977 Errors_Posted_On_Choices := True;
1978 end if;
1980 -- Do not range check a choice. This check is redundant
1981 -- since this test is already done when we check that the
1982 -- bounds of the array aggregate are within range.
1984 Set_Do_Range_Check (Choice, False);
1986 -- In SPARK, the choice must be static
1988 if not (Is_OK_Static_Expression (Choice)
1989 or else (Nkind (Choice) = N_Range
1990 and then Is_OK_Static_Range (Choice)))
1991 then
1992 Check_SPARK_05_Restriction
1993 ("choice should be static", Choice);
1994 end if;
1995 end if;
1997 -- If we could not resolve the discrete choice stop here
1999 if Etype (Choice) = Any_Type then
2000 return Failure;
2002 -- If the discrete choice raises CE get its original bounds
2004 elsif Nkind (Choice) = N_Raise_Constraint_Error then
2005 Set_Raises_Constraint_Error (N);
2006 Get_Index_Bounds (Original_Node (Choice), Low, High);
2008 -- Otherwise get its bounds as usual
2010 else
2011 Get_Index_Bounds (Choice, Low, High);
2012 end if;
2014 if (Dynamic_Or_Null_Range (Low, High)
2015 or else (Nkind (Choice) = N_Subtype_Indication
2016 and then
2017 Dynamic_Or_Null_Range (S_Low, S_High)))
2018 and then Nb_Choices /= 1
2019 then
2020 Error_Msg_N
2021 ("dynamic or empty choice in aggregate "
2022 & "must be the only choice", Choice);
2023 return Failure;
2024 end if;
2026 if not (All_Composite_Constraints_Static (Low)
2027 and then All_Composite_Constraints_Static (High)
2028 and then All_Composite_Constraints_Static (S_Low)
2029 and then All_Composite_Constraints_Static (S_High))
2030 then
2031 Check_Restriction (No_Dynamic_Sized_Objects, Choice);
2032 end if;
2034 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
2035 Table (Nb_Discrete_Choices).Lo := Low;
2036 Table (Nb_Discrete_Choices).Hi := High;
2037 Table (Nb_Discrete_Choices).Choice := Choice;
2039 Next (Choice);
2041 if No (Choice) then
2043 -- Check if we have a single discrete choice and whether
2044 -- this discrete choice specifies a single value.
2046 Single_Choice :=
2047 (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1)
2048 and then (Low = High);
2050 exit;
2051 end if;
2052 end loop;
2054 -- Ada 2005 (AI-231)
2056 if Ada_Version >= Ada_2005
2057 and then Known_Null (Expression (Assoc))
2058 and then not Empty_Range (Assoc)
2059 then
2060 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2061 end if;
2063 -- Ada 2005 (AI-287): In case of default initialized component
2064 -- we delay the resolution to the expansion phase.
2066 if Box_Present (Assoc) then
2068 -- Ada 2005 (AI-287): In case of default initialization of a
2069 -- component the expander will generate calls to the
2070 -- corresponding initialization subprogram. We need to call
2071 -- Resolve_Aggr_Expr to check the rules about
2072 -- dimensionality.
2074 if not Resolve_Aggr_Expr
2075 (Assoc, Single_Elmt => Single_Choice)
2076 then
2077 return Failure;
2078 end if;
2080 elsif not Resolve_Aggr_Expr
2081 (Expression (Assoc), Single_Elmt => Single_Choice)
2082 then
2083 return Failure;
2085 -- Check incorrect use of dynamically tagged expression
2087 -- We differentiate here two cases because the expression may
2088 -- not be decorated. For example, the analysis and resolution
2089 -- of the expression associated with the others choice will be
2090 -- done later with the full aggregate. In such case we
2091 -- duplicate the expression tree to analyze the copy and
2092 -- perform the required check.
2094 elsif not Present (Etype (Expression (Assoc))) then
2095 declare
2096 Save_Analysis : constant Boolean := Full_Analysis;
2097 Expr : constant Node_Id :=
2098 New_Copy_Tree (Expression (Assoc));
2100 begin
2101 Expander_Mode_Save_And_Set (False);
2102 Full_Analysis := False;
2104 -- Analyze the expression, making sure it is properly
2105 -- attached to the tree before we do the analysis.
2107 Set_Parent (Expr, Parent (Expression (Assoc)));
2108 Analyze (Expr);
2110 -- Compute its dimensions now, rather than at the end of
2111 -- resolution, because in the case of multidimensional
2112 -- aggregates subsequent expansion may lead to spurious
2113 -- errors.
2115 Check_Expression_Dimensions (Expr, Component_Typ);
2117 -- If the expression is a literal, propagate this info
2118 -- to the expression in the association, to enable some
2119 -- optimizations downstream.
2121 if Is_Entity_Name (Expr)
2122 and then Present (Entity (Expr))
2123 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
2124 then
2125 Analyze_And_Resolve
2126 (Expression (Assoc), Component_Typ);
2127 end if;
2129 Full_Analysis := Save_Analysis;
2130 Expander_Mode_Restore;
2132 if Is_Tagged_Type (Etype (Expr)) then
2133 Check_Dynamically_Tagged_Expression
2134 (Expr => Expr,
2135 Typ => Component_Type (Etype (N)),
2136 Related_Nod => N);
2137 end if;
2138 end;
2140 elsif Is_Tagged_Type (Etype (Expression (Assoc))) then
2141 Check_Dynamically_Tagged_Expression
2142 (Expr => Expression (Assoc),
2143 Typ => Component_Type (Etype (N)),
2144 Related_Nod => N);
2145 end if;
2147 Next (Assoc);
2148 end loop;
2150 -- If aggregate contains more than one choice then these must be
2151 -- static. Check for duplicate and missing values.
2153 -- Note: there is duplicated code here wrt Check_Choice_Set in
2154 -- the body of Sem_Case, and it is possible we could just reuse
2155 -- that procedure. To be checked ???
2157 if Nb_Discrete_Choices > 1 then
2158 Check_Choices : declare
2159 Choice : Node_Id;
2160 -- Location of choice for messages
2162 Hi_Val : Uint;
2163 Lo_Val : Uint;
2164 -- High end of one range and Low end of the next. Should be
2165 -- contiguous if there is no hole in the list of values.
2167 Lo_Dup : Uint;
2168 Hi_Dup : Uint;
2169 -- End points of duplicated range
2171 Missing_Or_Duplicates : Boolean := False;
2172 -- Set True if missing or duplicate choices found
2174 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id);
2175 -- Output continuation message with a representation of the
2176 -- bounds (just Lo if Lo = Hi, else Lo .. Hi). C is the
2177 -- choice node where the message is to be posted.
2179 ------------------------
2180 -- Output_Bad_Choices --
2181 ------------------------
2183 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id) is
2184 begin
2185 -- Enumeration type case
2187 if Is_Enumeration_Type (Index_Typ) then
2188 Error_Msg_Name_1 :=
2189 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Lo, Loc));
2190 Error_Msg_Name_2 :=
2191 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Hi, Loc));
2193 if Lo = Hi then
2194 Error_Msg_N ("\\ %!", C);
2195 else
2196 Error_Msg_N ("\\ % .. %!", C);
2197 end if;
2199 -- Integer types case
2201 else
2202 Error_Msg_Uint_1 := Lo;
2203 Error_Msg_Uint_2 := Hi;
2205 if Lo = Hi then
2206 Error_Msg_N ("\\ ^!", C);
2207 else
2208 Error_Msg_N ("\\ ^ .. ^!", C);
2209 end if;
2210 end if;
2211 end Output_Bad_Choices;
2213 -- Start of processing for Check_Choices
2215 begin
2216 Sort_Case_Table (Table);
2218 -- First we do a quick linear loop to find out if we have
2219 -- any duplicates or missing entries (usually we have a
2220 -- legal aggregate, so this will get us out quickly).
2222 for J in 1 .. Nb_Discrete_Choices - 1 loop
2223 Hi_Val := Expr_Value (Table (J).Hi);
2224 Lo_Val := Expr_Value (Table (J + 1).Lo);
2226 if Lo_Val <= Hi_Val
2227 or else (Lo_Val > Hi_Val + 1
2228 and then not Others_Present)
2229 then
2230 Missing_Or_Duplicates := True;
2231 exit;
2232 end if;
2233 end loop;
2235 -- If we have missing or duplicate entries, first fill in
2236 -- the Highest entries to make life easier in the following
2237 -- loops to detect bad entries.
2239 if Missing_Or_Duplicates then
2240 Table (1).Highest := Expr_Value (Table (1).Hi);
2242 for J in 2 .. Nb_Discrete_Choices loop
2243 Table (J).Highest :=
2244 UI_Max
2245 (Table (J - 1).Highest, Expr_Value (Table (J).Hi));
2246 end loop;
2248 -- Loop through table entries to find duplicate indexes
2250 for J in 2 .. Nb_Discrete_Choices loop
2251 Lo_Val := Expr_Value (Table (J).Lo);
2252 Hi_Val := Expr_Value (Table (J).Hi);
2254 -- Case where we have duplicates (the lower bound of
2255 -- this choice is less than or equal to the highest
2256 -- high bound found so far).
2258 if Lo_Val <= Table (J - 1).Highest then
2260 -- We move backwards looking for duplicates. We can
2261 -- abandon this loop as soon as we reach a choice
2262 -- highest value that is less than Lo_Val.
2264 for K in reverse 1 .. J - 1 loop
2265 exit when Table (K).Highest < Lo_Val;
2267 -- Here we may have duplicates between entries
2268 -- for K and J. Get range of duplicates.
2270 Lo_Dup :=
2271 UI_Max (Lo_Val, Expr_Value (Table (K).Lo));
2272 Hi_Dup :=
2273 UI_Min (Hi_Val, Expr_Value (Table (K).Hi));
2275 -- Nothing to do if duplicate range is null
2277 if Lo_Dup > Hi_Dup then
2278 null;
2280 -- Otherwise place proper message
2282 else
2283 -- We place message on later choice, with a
2284 -- line reference to the earlier choice.
2286 if Sloc (Table (J).Choice) <
2287 Sloc (Table (K).Choice)
2288 then
2289 Choice := Table (K).Choice;
2290 Error_Msg_Sloc := Sloc (Table (J).Choice);
2291 else
2292 Choice := Table (J).Choice;
2293 Error_Msg_Sloc := Sloc (Table (K).Choice);
2294 end if;
2296 if Lo_Dup = Hi_Dup then
2297 Error_Msg_N
2298 ("index value in array aggregate "
2299 & "duplicates the one given#!", Choice);
2300 else
2301 Error_Msg_N
2302 ("index values in array aggregate "
2303 & "duplicate those given#!", Choice);
2304 end if;
2306 Output_Bad_Choices (Lo_Dup, Hi_Dup, Choice);
2307 end if;
2308 end loop;
2309 end if;
2310 end loop;
2312 -- Loop through entries in table to find missing indexes.
2313 -- Not needed if others, since missing impossible.
2315 if not Others_Present then
2316 for J in 2 .. Nb_Discrete_Choices loop
2317 Lo_Val := Expr_Value (Table (J).Lo);
2318 Hi_Val := Table (J - 1).Highest;
2320 if Lo_Val > Hi_Val + 1 then
2322 declare
2323 Error_Node : Node_Id;
2325 begin
2326 -- If the choice is the bound of a range in
2327 -- a subtype indication, it is not in the
2328 -- source lists for the aggregate itself, so
2329 -- post the error on the aggregate. Otherwise
2330 -- post it on choice itself.
2332 Choice := Table (J).Choice;
2334 if Is_List_Member (Choice) then
2335 Error_Node := Choice;
2336 else
2337 Error_Node := N;
2338 end if;
2340 if Hi_Val + 1 = Lo_Val - 1 then
2341 Error_Msg_N
2342 ("missing index value "
2343 & "in array aggregate!", Error_Node);
2344 else
2345 Error_Msg_N
2346 ("missing index values "
2347 & "in array aggregate!", Error_Node);
2348 end if;
2350 Output_Bad_Choices
2351 (Hi_Val + 1, Lo_Val - 1, Error_Node);
2352 end;
2353 end if;
2354 end loop;
2355 end if;
2357 -- If either missing or duplicate values, return failure
2359 Set_Etype (N, Any_Composite);
2360 return Failure;
2361 end if;
2362 end Check_Choices;
2363 end if;
2365 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
2367 if Nb_Discrete_Choices > 0 then
2368 Choices_Low := Table (1).Lo;
2369 Choices_High := Table (Nb_Discrete_Choices).Hi;
2370 end if;
2372 -- If Others is present, then bounds of aggregate come from the
2373 -- index constraint (not the choices in the aggregate itself).
2375 if Others_Present then
2376 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2378 -- Abandon processing if either bound is already signalled as
2379 -- an error (prevents junk cascaded messages and blow ups).
2381 if Nkind (Aggr_Low) = N_Error
2382 or else
2383 Nkind (Aggr_High) = N_Error
2384 then
2385 return False;
2386 end if;
2388 -- No others clause present
2390 else
2391 -- Special processing if others allowed and not present. This
2392 -- means that the bounds of the aggregate come from the index
2393 -- constraint (and the length must match).
2395 if Others_Allowed then
2396 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2398 -- Abandon processing if either bound is already signalled
2399 -- as an error (stop junk cascaded messages and blow ups).
2401 if Nkind (Aggr_Low) = N_Error
2402 or else
2403 Nkind (Aggr_High) = N_Error
2404 then
2405 return False;
2406 end if;
2408 -- If others allowed, and no others present, then the array
2409 -- should cover all index values. If it does not, we will
2410 -- get a length check warning, but there is two cases where
2411 -- an additional warning is useful:
2413 -- If we have no positional components, and the length is
2414 -- wrong (which we can tell by others being allowed with
2415 -- missing components), and the index type is an enumeration
2416 -- type, then issue appropriate warnings about these missing
2417 -- components. They are only warnings, since the aggregate
2418 -- is fine, it's just the wrong length. We skip this check
2419 -- for standard character types (since there are no literals
2420 -- and it is too much trouble to concoct them), and also if
2421 -- any of the bounds have values that are not known at
2422 -- compile time.
2424 -- Another case warranting a warning is when the length
2425 -- is right, but as above we have an index type that is
2426 -- an enumeration, and the bounds do not match. This is a
2427 -- case where dubious sliding is allowed and we generate a
2428 -- warning that the bounds do not match.
2430 if No (Expressions (N))
2431 and then Nkind (Index) = N_Range
2432 and then Is_Enumeration_Type (Etype (Index))
2433 and then not Is_Standard_Character_Type (Etype (Index))
2434 and then Compile_Time_Known_Value (Aggr_Low)
2435 and then Compile_Time_Known_Value (Aggr_High)
2436 and then Compile_Time_Known_Value (Choices_Low)
2437 and then Compile_Time_Known_Value (Choices_High)
2438 then
2439 -- If any of the expressions or range bounds in choices
2440 -- have semantic errors, then do not attempt further
2441 -- resolution, to prevent cascaded errors.
2443 if Errors_Posted_On_Choices then
2444 return Failure;
2445 end if;
2447 declare
2448 ALo : constant Node_Id := Expr_Value_E (Aggr_Low);
2449 AHi : constant Node_Id := Expr_Value_E (Aggr_High);
2450 CLo : constant Node_Id := Expr_Value_E (Choices_Low);
2451 CHi : constant Node_Id := Expr_Value_E (Choices_High);
2453 Ent : Entity_Id;
2455 begin
2456 -- Warning case 1, missing values at start/end. Only
2457 -- do the check if the number of entries is too small.
2459 if (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2461 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2462 then
2463 Error_Msg_N
2464 ("missing index value(s) in array aggregate??",
2467 -- Output missing value(s) at start
2469 if Chars (ALo) /= Chars (CLo) then
2470 Ent := Prev (CLo);
2472 if Chars (ALo) = Chars (Ent) then
2473 Error_Msg_Name_1 := Chars (ALo);
2474 Error_Msg_N ("\ %??", N);
2475 else
2476 Error_Msg_Name_1 := Chars (ALo);
2477 Error_Msg_Name_2 := Chars (Ent);
2478 Error_Msg_N ("\ % .. %??", N);
2479 end if;
2480 end if;
2482 -- Output missing value(s) at end
2484 if Chars (AHi) /= Chars (CHi) then
2485 Ent := Next (CHi);
2487 if Chars (AHi) = Chars (Ent) then
2488 Error_Msg_Name_1 := Chars (Ent);
2489 Error_Msg_N ("\ %??", N);
2490 else
2491 Error_Msg_Name_1 := Chars (Ent);
2492 Error_Msg_Name_2 := Chars (AHi);
2493 Error_Msg_N ("\ % .. %??", N);
2494 end if;
2495 end if;
2497 -- Warning case 2, dubious sliding. The First_Subtype
2498 -- test distinguishes between a constrained type where
2499 -- sliding is not allowed (so we will get a warning
2500 -- later that Constraint_Error will be raised), and
2501 -- the unconstrained case where sliding is permitted.
2503 elsif (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2505 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2506 and then Chars (ALo) /= Chars (CLo)
2507 and then
2508 not Is_Constrained (First_Subtype (Etype (N)))
2509 then
2510 Error_Msg_N
2511 ("bounds of aggregate do not match target??", N);
2512 end if;
2513 end;
2514 end if;
2515 end if;
2517 -- If no others, aggregate bounds come from aggregate
2519 Aggr_Low := Choices_Low;
2520 Aggr_High := Choices_High;
2521 end if;
2522 end Step_2;
2524 -- STEP 3: Process positional components
2526 else
2527 -- STEP 3 (A): Process positional elements
2529 Expr := First (Expressions (N));
2530 Nb_Elements := Uint_0;
2531 while Present (Expr) loop
2532 Nb_Elements := Nb_Elements + 1;
2534 -- Ada 2005 (AI-231)
2536 if Ada_Version >= Ada_2005 and then Known_Null (Expr) then
2537 Check_Can_Never_Be_Null (Etype (N), Expr);
2538 end if;
2540 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
2541 return Failure;
2542 end if;
2544 -- Check incorrect use of dynamically tagged expression
2546 if Is_Tagged_Type (Etype (Expr)) then
2547 Check_Dynamically_Tagged_Expression
2548 (Expr => Expr,
2549 Typ => Component_Type (Etype (N)),
2550 Related_Nod => N);
2551 end if;
2553 Next (Expr);
2554 end loop;
2556 if Others_Present then
2557 Assoc := Last (Component_Associations (N));
2559 -- Ada 2005 (AI-231)
2561 if Ada_Version >= Ada_2005 and then Known_Null (Assoc) then
2562 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2563 end if;
2565 -- Ada 2005 (AI-287): In case of default initialized component,
2566 -- we delay the resolution to the expansion phase.
2568 if Box_Present (Assoc) then
2570 -- Ada 2005 (AI-287): In case of default initialization of a
2571 -- component the expander will generate calls to the
2572 -- corresponding initialization subprogram. We need to call
2573 -- Resolve_Aggr_Expr to check the rules about
2574 -- dimensionality.
2576 if not Resolve_Aggr_Expr (Assoc, Single_Elmt => False) then
2577 return Failure;
2578 end if;
2580 elsif not Resolve_Aggr_Expr (Expression (Assoc),
2581 Single_Elmt => False)
2582 then
2583 return Failure;
2585 -- Check incorrect use of dynamically tagged expression. The
2586 -- expression of the others choice has not been resolved yet.
2587 -- In order to diagnose the semantic error we create a duplicate
2588 -- tree to analyze it and perform the check.
2590 else
2591 declare
2592 Save_Analysis : constant Boolean := Full_Analysis;
2593 Expr : constant Node_Id :=
2594 New_Copy_Tree (Expression (Assoc));
2596 begin
2597 Expander_Mode_Save_And_Set (False);
2598 Full_Analysis := False;
2599 Analyze (Expr);
2600 Full_Analysis := Save_Analysis;
2601 Expander_Mode_Restore;
2603 if Is_Tagged_Type (Etype (Expr)) then
2604 Check_Dynamically_Tagged_Expression
2605 (Expr => Expr,
2606 Typ => Component_Type (Etype (N)),
2607 Related_Nod => N);
2608 end if;
2609 end;
2610 end if;
2611 end if;
2613 -- STEP 3 (B): Compute the aggregate bounds
2615 if Others_Present then
2616 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2618 else
2619 if Others_Allowed then
2620 Get_Index_Bounds (Index_Constr, Aggr_Low, Discard);
2621 else
2622 Aggr_Low := Index_Typ_Low;
2623 end if;
2625 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
2626 Check_Bound (Index_Base_High, Aggr_High);
2627 end if;
2628 end if;
2630 -- STEP 4: Perform static aggregate checks and save the bounds
2632 -- Check (A)
2634 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
2635 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
2637 -- Check (B)
2639 if Others_Present and then Nb_Discrete_Choices > 0 then
2640 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
2641 Check_Bounds (Index_Typ_Low, Index_Typ_High,
2642 Choices_Low, Choices_High);
2643 Check_Bounds (Index_Base_Low, Index_Base_High,
2644 Choices_Low, Choices_High);
2646 -- Check (C)
2648 elsif Others_Present and then Nb_Elements > 0 then
2649 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
2650 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
2651 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
2652 end if;
2654 if Raises_Constraint_Error (Aggr_Low)
2655 or else Raises_Constraint_Error (Aggr_High)
2656 then
2657 Set_Raises_Constraint_Error (N);
2658 end if;
2660 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
2662 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
2663 -- since the addition node returned by Add is not yet analyzed. Attach
2664 -- to tree and analyze first. Reset analyzed flag to ensure it will get
2665 -- analyzed when it is a literal bound whose type must be properly set.
2667 if Others_Present or else Nb_Discrete_Choices > 0 then
2668 Aggr_High := Duplicate_Subexpr (Aggr_High);
2670 if Etype (Aggr_High) = Universal_Integer then
2671 Set_Analyzed (Aggr_High, False);
2672 end if;
2673 end if;
2675 -- If the aggregate already has bounds attached to it, it means this is
2676 -- a positional aggregate created as an optimization by
2677 -- Exp_Aggr.Convert_To_Positional, so we don't want to change those
2678 -- bounds.
2680 if Present (Aggregate_Bounds (N)) and then not Others_Allowed then
2681 Aggr_Low := Low_Bound (Aggregate_Bounds (N));
2682 Aggr_High := High_Bound (Aggregate_Bounds (N));
2683 end if;
2685 Set_Aggregate_Bounds
2686 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
2688 -- The bounds may contain expressions that must be inserted upwards.
2689 -- Attach them fully to the tree. After analysis, remove side effects
2690 -- from upper bound, if still needed.
2692 Set_Parent (Aggregate_Bounds (N), N);
2693 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
2694 Check_Unset_Reference (Aggregate_Bounds (N));
2696 if not Others_Present and then Nb_Discrete_Choices = 0 then
2697 Set_High_Bound
2698 (Aggregate_Bounds (N),
2699 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
2700 end if;
2702 -- Check the dimensions of each component in the array aggregate
2704 Analyze_Dimension_Array_Aggregate (N, Component_Typ);
2706 return Success;
2707 end Resolve_Array_Aggregate;
2709 ---------------------------------
2710 -- Resolve_Extension_Aggregate --
2711 ---------------------------------
2713 -- There are two cases to consider:
2715 -- a) If the ancestor part is a type mark, the components needed are the
2716 -- difference between the components of the expected type and the
2717 -- components of the given type mark.
2719 -- b) If the ancestor part is an expression, it must be unambiguous, and
2720 -- once we have its type we can also compute the needed components as in
2721 -- the previous case. In both cases, if the ancestor type is not the
2722 -- immediate ancestor, we have to build this ancestor recursively.
2724 -- In both cases, discriminants of the ancestor type do not play a role in
2725 -- the resolution of the needed components, because inherited discriminants
2726 -- cannot be used in a type extension. As a result we can compute
2727 -- independently the list of components of the ancestor type and of the
2728 -- expected type.
2730 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
2731 A : constant Node_Id := Ancestor_Part (N);
2732 A_Type : Entity_Id;
2733 I : Interp_Index;
2734 It : Interp;
2736 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean;
2737 -- If the type is limited, verify that the ancestor part is a legal
2738 -- expression (aggregate or function call, including 'Input)) that does
2739 -- not require a copy, as specified in 7.5(2).
2741 function Valid_Ancestor_Type return Boolean;
2742 -- Verify that the type of the ancestor part is a non-private ancestor
2743 -- of the expected type, which must be a type extension.
2745 ----------------------------
2746 -- Valid_Limited_Ancestor --
2747 ----------------------------
2749 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean is
2750 begin
2751 if Is_Entity_Name (Anc) and then Is_Type (Entity (Anc)) then
2752 return True;
2754 -- The ancestor must be a call or an aggregate, but a call may
2755 -- have been expanded into a temporary, so check original node.
2757 elsif Nkind_In (Anc, N_Aggregate,
2758 N_Extension_Aggregate,
2759 N_Function_Call)
2760 then
2761 return True;
2763 elsif Nkind (Original_Node (Anc)) = N_Function_Call then
2764 return True;
2766 elsif Nkind (Anc) = N_Attribute_Reference
2767 and then Attribute_Name (Anc) = Name_Input
2768 then
2769 return True;
2771 elsif Nkind (Anc) = N_Qualified_Expression then
2772 return Valid_Limited_Ancestor (Expression (Anc));
2774 else
2775 return False;
2776 end if;
2777 end Valid_Limited_Ancestor;
2779 -------------------------
2780 -- Valid_Ancestor_Type --
2781 -------------------------
2783 function Valid_Ancestor_Type return Boolean is
2784 Imm_Type : Entity_Id;
2786 begin
2787 Imm_Type := Base_Type (Typ);
2788 while Is_Derived_Type (Imm_Type) loop
2789 if Etype (Imm_Type) = Base_Type (A_Type) then
2790 return True;
2792 -- The base type of the parent type may appear as a private
2793 -- extension if it is declared as such in a parent unit of the
2794 -- current one. For consistency of the subsequent analysis use
2795 -- the partial view for the ancestor part.
2797 elsif Is_Private_Type (Etype (Imm_Type))
2798 and then Present (Full_View (Etype (Imm_Type)))
2799 and then Base_Type (A_Type) = Full_View (Etype (Imm_Type))
2800 then
2801 A_Type := Etype (Imm_Type);
2802 return True;
2804 -- The parent type may be a private extension. The aggregate is
2805 -- legal if the type of the aggregate is an extension of it that
2806 -- is not a private extension.
2808 elsif Is_Private_Type (A_Type)
2809 and then not Is_Private_Type (Imm_Type)
2810 and then Present (Full_View (A_Type))
2811 and then Base_Type (Full_View (A_Type)) = Etype (Imm_Type)
2812 then
2813 return True;
2815 else
2816 Imm_Type := Etype (Base_Type (Imm_Type));
2817 end if;
2818 end loop;
2820 -- If previous loop did not find a proper ancestor, report error
2822 Error_Msg_NE ("expect ancestor type of &", A, Typ);
2823 return False;
2824 end Valid_Ancestor_Type;
2826 -- Start of processing for Resolve_Extension_Aggregate
2828 begin
2829 -- Analyze the ancestor part and account for the case where it is a
2830 -- parameterless function call.
2832 Analyze (A);
2833 Check_Parameterless_Call (A);
2835 -- In SPARK, the ancestor part cannot be a type mark
2837 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
2838 Check_SPARK_05_Restriction ("ancestor part cannot be a type mark", A);
2840 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
2841 -- must not have unknown discriminants.
2843 if Has_Unknown_Discriminants (Root_Type (Typ)) then
2844 Error_Msg_NE
2845 ("aggregate not available for type& whose ancestor "
2846 & "has unknown discriminants", N, Typ);
2847 end if;
2848 end if;
2850 if not Is_Tagged_Type (Typ) then
2851 Error_Msg_N ("type of extension aggregate must be tagged", N);
2852 return;
2854 elsif Is_Limited_Type (Typ) then
2856 -- Ada 2005 (AI-287): Limited aggregates are allowed
2858 if Ada_Version < Ada_2005 then
2859 Error_Msg_N ("aggregate type cannot be limited", N);
2860 Explain_Limited_Type (Typ, N);
2861 return;
2863 elsif Valid_Limited_Ancestor (A) then
2864 null;
2866 else
2867 Error_Msg_N
2868 ("limited ancestor part must be aggregate or function call", A);
2869 end if;
2871 elsif Is_Class_Wide_Type (Typ) then
2872 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
2873 return;
2874 end if;
2876 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
2877 A_Type := Get_Full_View (Entity (A));
2879 if Valid_Ancestor_Type then
2880 Set_Entity (A, A_Type);
2881 Set_Etype (A, A_Type);
2883 Validate_Ancestor_Part (N);
2884 Resolve_Record_Aggregate (N, Typ);
2885 end if;
2887 elsif Nkind (A) /= N_Aggregate then
2888 if Is_Overloaded (A) then
2889 A_Type := Any_Type;
2891 Get_First_Interp (A, I, It);
2892 while Present (It.Typ) loop
2894 -- Only consider limited interpretations in the Ada 2005 case
2896 if Is_Tagged_Type (It.Typ)
2897 and then (Ada_Version >= Ada_2005
2898 or else not Is_Limited_Type (It.Typ))
2899 then
2900 if A_Type /= Any_Type then
2901 Error_Msg_N ("cannot resolve expression", A);
2902 return;
2903 else
2904 A_Type := It.Typ;
2905 end if;
2906 end if;
2908 Get_Next_Interp (I, It);
2909 end loop;
2911 if A_Type = Any_Type then
2912 if Ada_Version >= Ada_2005 then
2913 Error_Msg_N
2914 ("ancestor part must be of a tagged type", A);
2915 else
2916 Error_Msg_N
2917 ("ancestor part must be of a nonlimited tagged type", A);
2918 end if;
2920 return;
2921 end if;
2923 else
2924 A_Type := Etype (A);
2925 end if;
2927 if Valid_Ancestor_Type then
2928 Resolve (A, A_Type);
2929 Check_Unset_Reference (A);
2930 Check_Non_Static_Context (A);
2932 -- The aggregate is illegal if the ancestor expression is a call
2933 -- to a function with a limited unconstrained result, unless the
2934 -- type of the aggregate is a null extension. This restriction
2935 -- was added in AI05-67 to simplify implementation.
2937 if Nkind (A) = N_Function_Call
2938 and then Is_Limited_Type (A_Type)
2939 and then not Is_Null_Extension (Typ)
2940 and then not Is_Constrained (A_Type)
2941 then
2942 Error_Msg_N
2943 ("type of limited ancestor part must be constrained", A);
2945 -- Reject the use of CPP constructors that leave objects partially
2946 -- initialized. For example:
2948 -- type CPP_Root is tagged limited record ...
2949 -- pragma Import (CPP, CPP_Root);
2951 -- type CPP_DT is new CPP_Root and Iface ...
2952 -- pragma Import (CPP, CPP_DT);
2954 -- type Ada_DT is new CPP_DT with ...
2956 -- Obj : Ada_DT := Ada_DT'(New_CPP_Root with others => <>);
2958 -- Using the constructor of CPP_Root the slots of the dispatch
2959 -- table of CPP_DT cannot be set, and the secondary tag of
2960 -- CPP_DT is unknown.
2962 elsif Nkind (A) = N_Function_Call
2963 and then Is_CPP_Constructor_Call (A)
2964 and then Enclosing_CPP_Parent (Typ) /= A_Type
2965 then
2966 Error_Msg_NE
2967 ("??must use 'C'P'P constructor for type &", A,
2968 Enclosing_CPP_Parent (Typ));
2970 -- The following call is not needed if the previous warning
2971 -- is promoted to an error.
2973 Resolve_Record_Aggregate (N, Typ);
2975 elsif Is_Class_Wide_Type (Etype (A))
2976 and then Nkind (Original_Node (A)) = N_Function_Call
2977 then
2978 -- If the ancestor part is a dispatching call, it appears
2979 -- statically to be a legal ancestor, but it yields any member
2980 -- of the class, and it is not possible to determine whether
2981 -- it is an ancestor of the extension aggregate (much less
2982 -- which ancestor). It is not possible to determine the
2983 -- components of the extension part.
2985 -- This check implements AI-306, which in fact was motivated by
2986 -- an AdaCore query to the ARG after this test was added.
2988 Error_Msg_N ("ancestor part must be statically tagged", A);
2989 else
2990 Resolve_Record_Aggregate (N, Typ);
2991 end if;
2992 end if;
2994 else
2995 Error_Msg_N ("no unique type for this aggregate", A);
2996 end if;
2998 Check_Function_Writable_Actuals (N);
2999 end Resolve_Extension_Aggregate;
3001 ------------------------------
3002 -- Resolve_Record_Aggregate --
3003 ------------------------------
3005 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
3006 New_Assoc_List : constant List_Id := New_List;
3007 -- New_Assoc_List is the newly built list of N_Component_Association
3008 -- nodes.
3010 Others_Etype : Entity_Id := Empty;
3011 -- This variable is used to save the Etype of the last record component
3012 -- that takes its value from the others choice. Its purpose is:
3014 -- (a) make sure the others choice is useful
3016 -- (b) make sure the type of all the components whose value is
3017 -- subsumed by the others choice are the same.
3019 -- This variable is updated as a side effect of function Get_Value.
3021 Box_Node : Node_Id;
3022 Is_Box_Present : Boolean := False;
3023 Others_Box : Integer := 0;
3024 -- Ada 2005 (AI-287): Variables used in case of default initialization
3025 -- to provide a functionality similar to Others_Etype. Box_Present
3026 -- indicates that the component takes its default initialization;
3027 -- Others_Box counts the number of components of the current aggregate
3028 -- (which may be a sub-aggregate of a larger one) that are default-
3029 -- initialized. A value of One indicates that an others_box is present.
3030 -- Any larger value indicates that the others_box is not redundant.
3031 -- These variables, similar to Others_Etype, are also updated as a side
3032 -- effect of function Get_Value. Box_Node is used to place a warning on
3033 -- a redundant others_box.
3035 procedure Add_Association
3036 (Component : Entity_Id;
3037 Expr : Node_Id;
3038 Assoc_List : List_Id;
3039 Is_Box_Present : Boolean := False);
3040 -- Builds a new N_Component_Association node which associates Component
3041 -- to expression Expr and adds it to the association list being built,
3042 -- either New_Assoc_List, or the association being built for an inner
3043 -- aggregate.
3045 procedure Add_Discriminant_Values
3046 (New_Aggr : Node_Id;
3047 Assoc_List : List_Id);
3048 -- The constraint to a component may be given by a discriminant of the
3049 -- enclosing type, in which case we have to retrieve its value, which is
3050 -- part of the enclosing aggregate. Assoc_List provides the discriminant
3051 -- associations of the current type or of some enclosing record.
3053 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean;
3054 -- If aggregate N is a regular aggregate this routine will return True.
3055 -- Otherwise, if N is an extension aggregate, then Input_Discr denotes
3056 -- a discriminant whose value may already have been specified by N's
3057 -- ancestor part. This routine checks whether this is indeed the case
3058 -- and if so returns False, signaling that no value for Input_Discr
3059 -- should appear in N's aggregate part. Also, in this case, the routine
3060 -- appends to New_Assoc_List the discriminant value specified in the
3061 -- ancestor part.
3063 -- If the aggregate is in a context with expansion delayed, it will be
3064 -- reanalyzed. The inherited discriminant values must not be reinserted
3065 -- in the component list to prevent spurious errors, but they must be
3066 -- present on first analysis to build the proper subtype indications.
3067 -- The flag Inherited_Discriminant is used to prevent the re-insertion.
3069 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id;
3070 -- AI05-0115: Find earlier ancestor in the derivation chain that is
3071 -- derived from private view Typ. Whether the aggregate is legal depends
3072 -- on the current visibility of the type as well as that of the parent
3073 -- of the ancestor.
3075 function Get_Value
3076 (Compon : Node_Id;
3077 From : List_Id;
3078 Consider_Others_Choice : Boolean := False) return Node_Id;
3079 -- Given a record component stored in parameter Compon, this function
3080 -- returns its value as it appears in the list From, which is a list
3081 -- of N_Component_Association nodes.
3083 -- If no component association has a choice for the searched component,
3084 -- the value provided by the others choice is returned, if there is one,
3085 -- and Consider_Others_Choice is set to true. Otherwise Empty is
3086 -- returned. If there is more than one component association giving a
3087 -- value for the searched record component, an error message is emitted
3088 -- and the first found value is returned.
3090 -- If Consider_Others_Choice is set and the returned expression comes
3091 -- from the others choice, then Others_Etype is set as a side effect.
3092 -- An error message is emitted if the components taking their value from
3093 -- the others choice do not have same type.
3095 function New_Copy_Tree_And_Copy_Dimensions
3096 (Source : Node_Id;
3097 Map : Elist_Id := No_Elist;
3098 New_Sloc : Source_Ptr := No_Location;
3099 New_Scope : Entity_Id := Empty) return Node_Id;
3100 -- Same as New_Copy_Tree (defined in Sem_Util), except that this routine
3101 -- also copies the dimensions of Source to the returned node.
3103 procedure Propagate_Discriminants
3104 (Aggr : Node_Id;
3105 Assoc_List : List_Id);
3106 -- Nested components may themselves be discriminated types constrained
3107 -- by outer discriminants, whose values must be captured before the
3108 -- aggregate is expanded into assignments.
3110 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id);
3111 -- Analyzes and resolves expression Expr against the Etype of the
3112 -- Component. This routine also applies all appropriate checks to Expr.
3113 -- It finally saves a Expr in the newly created association list that
3114 -- will be attached to the final record aggregate. Note that if the
3115 -- Parent pointer of Expr is not set then Expr was produced with a
3116 -- New_Copy_Tree or some such.
3118 ---------------------
3119 -- Add_Association --
3120 ---------------------
3122 procedure Add_Association
3123 (Component : Entity_Id;
3124 Expr : Node_Id;
3125 Assoc_List : List_Id;
3126 Is_Box_Present : Boolean := False)
3128 Choice_List : constant List_Id := New_List;
3129 Loc : Source_Ptr;
3131 begin
3132 -- If this is a box association the expression is missing, so use the
3133 -- Sloc of the aggregate itself for the new association.
3135 if Present (Expr) then
3136 Loc := Sloc (Expr);
3137 else
3138 Loc := Sloc (N);
3139 end if;
3141 Append_To (Choice_List, New_Occurrence_Of (Component, Loc));
3143 Append_To (Assoc_List,
3144 Make_Component_Association (Loc,
3145 Choices => Choice_List,
3146 Expression => Expr,
3147 Box_Present => Is_Box_Present));
3148 end Add_Association;
3150 -----------------------------
3151 -- Add_Discriminant_Values --
3152 -----------------------------
3154 procedure Add_Discriminant_Values
3155 (New_Aggr : Node_Id;
3156 Assoc_List : List_Id)
3158 Assoc : Node_Id;
3159 Discr : Entity_Id;
3160 Discr_Elmt : Elmt_Id;
3161 Discr_Val : Node_Id;
3162 Val : Entity_Id;
3164 begin
3165 Discr := First_Discriminant (Etype (New_Aggr));
3166 Discr_Elmt := First_Elmt (Discriminant_Constraint (Etype (New_Aggr)));
3167 while Present (Discr_Elmt) loop
3168 Discr_Val := Node (Discr_Elmt);
3170 -- If the constraint is given by a discriminant then it is a
3171 -- discriminant of an enclosing record, and its value has already
3172 -- been placed in the association list.
3174 if Is_Entity_Name (Discr_Val)
3175 and then Ekind (Entity (Discr_Val)) = E_Discriminant
3176 then
3177 Val := Entity (Discr_Val);
3179 Assoc := First (Assoc_List);
3180 while Present (Assoc) loop
3181 if Present (Entity (First (Choices (Assoc))))
3182 and then Entity (First (Choices (Assoc))) = Val
3183 then
3184 Discr_Val := Expression (Assoc);
3185 exit;
3186 end if;
3188 Next (Assoc);
3189 end loop;
3190 end if;
3192 Add_Association
3193 (Discr, New_Copy_Tree (Discr_Val),
3194 Component_Associations (New_Aggr));
3196 -- If the discriminant constraint is a current instance, mark the
3197 -- current aggregate so that the self-reference can be expanded
3198 -- later. The constraint may refer to the subtype of aggregate, so
3199 -- use base type for comparison.
3201 if Nkind (Discr_Val) = N_Attribute_Reference
3202 and then Is_Entity_Name (Prefix (Discr_Val))
3203 and then Is_Type (Entity (Prefix (Discr_Val)))
3204 and then Base_Type (Etype (N)) = Entity (Prefix (Discr_Val))
3205 then
3206 Set_Has_Self_Reference (N);
3207 end if;
3209 Next_Elmt (Discr_Elmt);
3210 Next_Discriminant (Discr);
3211 end loop;
3212 end Add_Discriminant_Values;
3214 --------------------------
3215 -- Discriminant_Present --
3216 --------------------------
3218 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean is
3219 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
3221 Ancestor_Is_Subtyp : Boolean;
3223 Loc : Source_Ptr;
3225 Ancestor : Node_Id;
3226 Ancestor_Typ : Entity_Id;
3227 Comp_Assoc : Node_Id;
3228 Discr : Entity_Id;
3229 Discr_Expr : Node_Id;
3230 Discr_Val : Elmt_Id := No_Elmt;
3231 Orig_Discr : Entity_Id;
3233 begin
3234 if Regular_Aggr then
3235 return True;
3236 end if;
3238 -- Check whether inherited discriminant values have already been
3239 -- inserted in the aggregate. This will be the case if we are
3240 -- re-analyzing an aggregate whose expansion was delayed.
3242 if Present (Component_Associations (N)) then
3243 Comp_Assoc := First (Component_Associations (N));
3244 while Present (Comp_Assoc) loop
3245 if Inherited_Discriminant (Comp_Assoc) then
3246 return True;
3247 end if;
3249 Next (Comp_Assoc);
3250 end loop;
3251 end if;
3253 Ancestor := Ancestor_Part (N);
3254 Ancestor_Typ := Etype (Ancestor);
3255 Loc := Sloc (Ancestor);
3257 -- For a private type with unknown discriminants, use the underlying
3258 -- record view if it is available.
3260 if Has_Unknown_Discriminants (Ancestor_Typ)
3261 and then Present (Full_View (Ancestor_Typ))
3262 and then Present (Underlying_Record_View (Full_View (Ancestor_Typ)))
3263 then
3264 Ancestor_Typ := Underlying_Record_View (Full_View (Ancestor_Typ));
3265 end if;
3267 Ancestor_Is_Subtyp :=
3268 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
3270 -- If the ancestor part has no discriminants clearly N's aggregate
3271 -- part must provide a value for Discr.
3273 if not Has_Discriminants (Ancestor_Typ) then
3274 return True;
3276 -- If the ancestor part is an unconstrained subtype mark then the
3277 -- Discr must be present in N's aggregate part.
3279 elsif Ancestor_Is_Subtyp
3280 and then not Is_Constrained (Entity (Ancestor))
3281 then
3282 return True;
3283 end if;
3285 -- Now look to see if Discr was specified in the ancestor part
3287 if Ancestor_Is_Subtyp then
3288 Discr_Val :=
3289 First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
3290 end if;
3292 Orig_Discr := Original_Record_Component (Input_Discr);
3294 Discr := First_Discriminant (Ancestor_Typ);
3295 while Present (Discr) loop
3297 -- If Ancestor has already specified Disc value then insert its
3298 -- value in the final aggregate.
3300 if Original_Record_Component (Discr) = Orig_Discr then
3301 if Ancestor_Is_Subtyp then
3302 Discr_Expr := New_Copy_Tree (Node (Discr_Val));
3303 else
3304 Discr_Expr :=
3305 Make_Selected_Component (Loc,
3306 Prefix => Duplicate_Subexpr (Ancestor),
3307 Selector_Name => New_Occurrence_Of (Input_Discr, Loc));
3308 end if;
3310 Resolve_Aggr_Expr (Discr_Expr, Input_Discr);
3311 Set_Inherited_Discriminant (Last (New_Assoc_List));
3312 return False;
3313 end if;
3315 Next_Discriminant (Discr);
3317 if Ancestor_Is_Subtyp then
3318 Next_Elmt (Discr_Val);
3319 end if;
3320 end loop;
3322 return True;
3323 end Discriminant_Present;
3325 ---------------------------
3326 -- Find_Private_Ancestor --
3327 ---------------------------
3329 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id is
3330 Par : Entity_Id;
3332 begin
3333 Par := Typ;
3334 loop
3335 if Has_Private_Ancestor (Par)
3336 and then not Has_Private_Ancestor (Etype (Base_Type (Par)))
3337 then
3338 return Par;
3340 elsif not Is_Derived_Type (Par) then
3341 return Empty;
3343 else
3344 Par := Etype (Base_Type (Par));
3345 end if;
3346 end loop;
3347 end Find_Private_Ancestor;
3349 ---------------
3350 -- Get_Value --
3351 ---------------
3353 function Get_Value
3354 (Compon : Node_Id;
3355 From : List_Id;
3356 Consider_Others_Choice : Boolean := False) return Node_Id
3358 Typ : constant Entity_Id := Etype (Compon);
3359 Assoc : Node_Id;
3360 Expr : Node_Id := Empty;
3361 Selector_Name : Node_Id;
3363 begin
3364 Is_Box_Present := False;
3366 if No (From) then
3367 return Empty;
3368 end if;
3370 Assoc := First (From);
3371 while Present (Assoc) loop
3372 Selector_Name := First (Choices (Assoc));
3373 while Present (Selector_Name) loop
3374 if Nkind (Selector_Name) = N_Others_Choice then
3375 if Consider_Others_Choice and then No (Expr) then
3377 -- We need to duplicate the expression for each
3378 -- successive component covered by the others choice.
3379 -- This is redundant if the others_choice covers only
3380 -- one component (small optimization possible???), but
3381 -- indispensable otherwise, because each one must be
3382 -- expanded individually to preserve side-effects.
3384 -- Ada 2005 (AI-287): In case of default initialization
3385 -- of components, we duplicate the corresponding default
3386 -- expression (from the record type declaration). The
3387 -- copy must carry the sloc of the association (not the
3388 -- original expression) to prevent spurious elaboration
3389 -- checks when the default includes function calls.
3391 if Box_Present (Assoc) then
3392 Others_Box := Others_Box + 1;
3393 Is_Box_Present := True;
3395 if Expander_Active then
3396 return
3397 New_Copy_Tree_And_Copy_Dimensions
3398 (Expression (Parent (Compon)),
3399 New_Sloc => Sloc (Assoc));
3400 else
3401 return Expression (Parent (Compon));
3402 end if;
3404 else
3405 if Present (Others_Etype)
3406 and then Base_Type (Others_Etype) /= Base_Type (Typ)
3407 then
3408 -- If the components are of an anonymous access
3409 -- type they are distinct, but this is legal in
3410 -- Ada 2012 as long as designated types match.
3412 if (Ekind (Typ) = E_Anonymous_Access_Type
3413 or else Ekind (Typ) =
3414 E_Anonymous_Access_Subprogram_Type)
3415 and then Designated_Type (Typ) =
3416 Designated_Type (Others_Etype)
3417 then
3418 null;
3419 else
3420 Error_Msg_N
3421 ("components in OTHERS choice must have same "
3422 & "type", Selector_Name);
3423 end if;
3424 end if;
3426 Others_Etype := Typ;
3428 -- Copy the expression so that it is resolved
3429 -- independently for each component, This is needed
3430 -- for accessibility checks on compoents of anonymous
3431 -- access types, even in compile_only mode.
3433 if not Inside_A_Generic then
3435 -- In ASIS mode, preanalyze the expression in an
3436 -- others association before making copies for
3437 -- separate resolution and accessibility checks.
3438 -- This ensures that the type of the expression is
3439 -- available to ASIS in all cases, in particular if
3440 -- the expression is itself an aggregate.
3442 if ASIS_Mode then
3443 Preanalyze_And_Resolve (Expression (Assoc), Typ);
3444 end if;
3446 return
3447 New_Copy_Tree_And_Copy_Dimensions
3448 (Expression (Assoc));
3450 else
3451 return Expression (Assoc);
3452 end if;
3453 end if;
3454 end if;
3456 elsif Chars (Compon) = Chars (Selector_Name) then
3457 if No (Expr) then
3459 -- Ada 2005 (AI-231)
3461 if Ada_Version >= Ada_2005
3462 and then Known_Null (Expression (Assoc))
3463 then
3464 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
3465 end if;
3467 -- We need to duplicate the expression when several
3468 -- components are grouped together with a "|" choice.
3469 -- For instance "filed1 | filed2 => Expr"
3471 -- Ada 2005 (AI-287)
3473 if Box_Present (Assoc) then
3474 Is_Box_Present := True;
3476 -- Duplicate the default expression of the component
3477 -- from the record type declaration, so a new copy
3478 -- can be attached to the association.
3480 -- Note that we always copy the default expression,
3481 -- even when the association has a single choice, in
3482 -- order to create a proper association for the
3483 -- expanded aggregate.
3485 -- Component may have no default, in which case the
3486 -- expression is empty and the component is default-
3487 -- initialized, but an association for the component
3488 -- exists, and it is not covered by an others clause.
3490 -- Scalar and private types have no initialization
3491 -- procedure, so they remain uninitialized. If the
3492 -- target of the aggregate is a constant this
3493 -- deserves a warning.
3495 if No (Expression (Parent (Compon)))
3496 and then not Has_Non_Null_Base_Init_Proc (Typ)
3497 and then not Has_Aspect (Typ, Aspect_Default_Value)
3498 and then not Is_Concurrent_Type (Typ)
3499 and then Nkind (Parent (N)) = N_Object_Declaration
3500 and then Constant_Present (Parent (N))
3501 then
3502 Error_Msg_Node_2 := Typ;
3503 Error_Msg_NE
3504 ("component&? of type& is uninitialized",
3505 Assoc, Selector_Name);
3507 -- An additional reminder if the component type
3508 -- is a generic formal.
3510 if Is_Generic_Type (Base_Type (Typ)) then
3511 Error_Msg_NE
3512 ("\instance should provide actual type with "
3513 & "initialization for&", Assoc, Typ);
3514 end if;
3515 end if;
3517 return
3518 New_Copy_Tree_And_Copy_Dimensions
3519 (Expression (Parent (Compon)));
3521 else
3522 if Present (Next (Selector_Name)) then
3523 Expr := New_Copy_Tree_And_Copy_Dimensions
3524 (Expression (Assoc));
3525 else
3526 Expr := Expression (Assoc);
3527 end if;
3528 end if;
3530 Generate_Reference (Compon, Selector_Name, 'm');
3532 else
3533 Error_Msg_NE
3534 ("more than one value supplied for &",
3535 Selector_Name, Compon);
3537 end if;
3538 end if;
3540 Next (Selector_Name);
3541 end loop;
3543 Next (Assoc);
3544 end loop;
3546 return Expr;
3547 end Get_Value;
3549 ---------------------------------------
3550 -- New_Copy_Tree_And_Copy_Dimensions --
3551 ---------------------------------------
3553 function New_Copy_Tree_And_Copy_Dimensions
3554 (Source : Node_Id;
3555 Map : Elist_Id := No_Elist;
3556 New_Sloc : Source_Ptr := No_Location;
3557 New_Scope : Entity_Id := Empty) return Node_Id
3559 New_Copy : constant Node_Id :=
3560 New_Copy_Tree (Source, Map, New_Sloc, New_Scope);
3562 begin
3563 -- Move the dimensions of Source to New_Copy
3565 Copy_Dimensions (Source, New_Copy);
3566 return New_Copy;
3567 end New_Copy_Tree_And_Copy_Dimensions;
3569 -----------------------------
3570 -- Propagate_Discriminants --
3571 -----------------------------
3573 procedure Propagate_Discriminants
3574 (Aggr : Node_Id;
3575 Assoc_List : List_Id)
3577 Loc : constant Source_Ptr := Sloc (N);
3579 Needs_Box : Boolean := False;
3581 procedure Process_Component (Comp : Entity_Id);
3582 -- Add one component with a box association to the inner aggregate,
3583 -- and recurse if component is itself composite.
3585 -----------------------
3586 -- Process_Component --
3587 -----------------------
3589 procedure Process_Component (Comp : Entity_Id) is
3590 T : constant Entity_Id := Etype (Comp);
3591 New_Aggr : Node_Id;
3593 begin
3594 if Is_Record_Type (T) and then Has_Discriminants (T) then
3595 New_Aggr := Make_Aggregate (Loc, New_List, New_List);
3596 Set_Etype (New_Aggr, T);
3598 Add_Association
3599 (Comp, New_Aggr, Component_Associations (Aggr));
3601 -- Collect discriminant values and recurse
3603 Add_Discriminant_Values (New_Aggr, Assoc_List);
3604 Propagate_Discriminants (New_Aggr, Assoc_List);
3606 else
3607 Needs_Box := True;
3608 end if;
3609 end Process_Component;
3611 -- Local variables
3613 Aggr_Type : constant Entity_Id := Base_Type (Etype (Aggr));
3614 Components : constant Elist_Id := New_Elmt_List;
3615 Def_Node : constant Node_Id :=
3616 Type_Definition (Declaration_Node (Aggr_Type));
3618 Comp : Node_Id;
3619 Comp_Elmt : Elmt_Id;
3620 Errors : Boolean;
3622 -- Start of processing for Propagate_Discriminants
3624 begin
3625 -- The component type may be a variant type. Collect the components
3626 -- that are ruled by the known values of the discriminants. Their
3627 -- values have already been inserted into the component list of the
3628 -- current aggregate.
3630 if Nkind (Def_Node) = N_Record_Definition
3631 and then Present (Component_List (Def_Node))
3632 and then Present (Variant_Part (Component_List (Def_Node)))
3633 then
3634 Gather_Components (Aggr_Type,
3635 Component_List (Def_Node),
3636 Governed_By => Component_Associations (Aggr),
3637 Into => Components,
3638 Report_Errors => Errors);
3640 Comp_Elmt := First_Elmt (Components);
3641 while Present (Comp_Elmt) loop
3642 if Ekind (Node (Comp_Elmt)) /= E_Discriminant then
3643 Process_Component (Node (Comp_Elmt));
3644 end if;
3646 Next_Elmt (Comp_Elmt);
3647 end loop;
3649 -- No variant part, iterate over all components
3651 else
3652 Comp := First_Component (Etype (Aggr));
3653 while Present (Comp) loop
3654 Process_Component (Comp);
3655 Next_Component (Comp);
3656 end loop;
3657 end if;
3659 if Needs_Box then
3660 Append_To (Component_Associations (Aggr),
3661 Make_Component_Association (Loc,
3662 Choices => New_List (Make_Others_Choice (Loc)),
3663 Expression => Empty,
3664 Box_Present => True));
3665 end if;
3666 end Propagate_Discriminants;
3668 -----------------------
3669 -- Resolve_Aggr_Expr --
3670 -----------------------
3672 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id) is
3673 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
3674 -- If the expression is an aggregate (possibly qualified) then its
3675 -- expansion is delayed until the enclosing aggregate is expanded
3676 -- into assignments. In that case, do not generate checks on the
3677 -- expression, because they will be generated later, and will other-
3678 -- wise force a copy (to remove side-effects) that would leave a
3679 -- dynamic-sized aggregate in the code, something that gigi cannot
3680 -- handle.
3682 ---------------------------
3683 -- Has_Expansion_Delayed --
3684 ---------------------------
3686 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
3687 begin
3688 return
3689 (Nkind_In (Expr, N_Aggregate, N_Extension_Aggregate)
3690 and then Present (Etype (Expr))
3691 and then Is_Record_Type (Etype (Expr))
3692 and then Expansion_Delayed (Expr))
3693 or else
3694 (Nkind (Expr) = N_Qualified_Expression
3695 and then Has_Expansion_Delayed (Expression (Expr)));
3696 end Has_Expansion_Delayed;
3698 -- Local variables
3700 Expr_Type : Entity_Id := Empty;
3701 New_C : Entity_Id := Component;
3702 New_Expr : Node_Id;
3704 Relocate : Boolean;
3705 -- Set to True if the resolved Expr node needs to be relocated when
3706 -- attached to the newly created association list. This node need not
3707 -- be relocated if its parent pointer is not set. In fact in this
3708 -- case Expr is the output of a New_Copy_Tree call. If Relocate is
3709 -- True then we have analyzed the expression node in the original
3710 -- aggregate and hence it needs to be relocated when moved over to
3711 -- the new association list.
3713 -- Start of processing for Resolve_Aggr_Expr
3715 begin
3716 -- If the type of the component is elementary or the type of the
3717 -- aggregate does not contain discriminants, use the type of the
3718 -- component to resolve Expr.
3720 if Is_Elementary_Type (Etype (Component))
3721 or else not Has_Discriminants (Etype (N))
3722 then
3723 Expr_Type := Etype (Component);
3725 -- Otherwise we have to pick up the new type of the component from
3726 -- the new constrained subtype of the aggregate. In fact components
3727 -- which are of a composite type might be constrained by a
3728 -- discriminant, and we want to resolve Expr against the subtype were
3729 -- all discriminant occurrences are replaced with their actual value.
3731 else
3732 New_C := First_Component (Etype (N));
3733 while Present (New_C) loop
3734 if Chars (New_C) = Chars (Component) then
3735 Expr_Type := Etype (New_C);
3736 exit;
3737 end if;
3739 Next_Component (New_C);
3740 end loop;
3742 pragma Assert (Present (Expr_Type));
3744 -- For each range in an array type where a discriminant has been
3745 -- replaced with the constraint, check that this range is within
3746 -- the range of the base type. This checks is done in the init
3747 -- proc for regular objects, but has to be done here for
3748 -- aggregates since no init proc is called for them.
3750 if Is_Array_Type (Expr_Type) then
3751 declare
3752 Index : Node_Id;
3753 -- Range of the current constrained index in the array
3755 Orig_Index : Node_Id := First_Index (Etype (Component));
3756 -- Range corresponding to the range Index above in the
3757 -- original unconstrained record type. The bounds of this
3758 -- range may be governed by discriminants.
3760 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
3761 -- Range corresponding to the range Index above for the
3762 -- unconstrained array type. This range is needed to apply
3763 -- range checks.
3765 begin
3766 Index := First_Index (Expr_Type);
3767 while Present (Index) loop
3768 if Depends_On_Discriminant (Orig_Index) then
3769 Apply_Range_Check (Index, Etype (Unconstr_Index));
3770 end if;
3772 Next_Index (Index);
3773 Next_Index (Orig_Index);
3774 Next_Index (Unconstr_Index);
3775 end loop;
3776 end;
3777 end if;
3778 end if;
3780 -- If the Parent pointer of Expr is not set, Expr is an expression
3781 -- duplicated by New_Tree_Copy (this happens for record aggregates
3782 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
3783 -- Such a duplicated expression must be attached to the tree
3784 -- before analysis and resolution to enforce the rule that a tree
3785 -- fragment should never be analyzed or resolved unless it is
3786 -- attached to the current compilation unit.
3788 if No (Parent (Expr)) then
3789 Set_Parent (Expr, N);
3790 Relocate := False;
3791 else
3792 Relocate := True;
3793 end if;
3795 Analyze_And_Resolve (Expr, Expr_Type);
3796 Check_Expr_OK_In_Limited_Aggregate (Expr);
3797 Check_Non_Static_Context (Expr);
3798 Check_Unset_Reference (Expr);
3800 -- Check wrong use of class-wide types
3802 if Is_Class_Wide_Type (Etype (Expr)) then
3803 Error_Msg_N ("dynamically tagged expression not allowed", Expr);
3804 end if;
3806 if not Has_Expansion_Delayed (Expr) then
3807 Aggregate_Constraint_Checks (Expr, Expr_Type);
3808 end if;
3810 -- If an aggregate component has a type with predicates, an explicit
3811 -- predicate check must be applied, as for an assignment statement,
3812 -- because the aggegate might not be expanded into individual
3813 -- component assignments.
3815 if Present (Predicate_Function (Expr_Type))
3816 and then Analyzed (Expr)
3817 then
3818 Apply_Predicate_Check (Expr, Expr_Type);
3819 end if;
3821 if Raises_Constraint_Error (Expr) then
3822 Set_Raises_Constraint_Error (N);
3823 end if;
3825 -- If the expression has been marked as requiring a range check, then
3826 -- generate it here. It's a bit odd to be generating such checks in
3827 -- the analyzer, but harmless since Generate_Range_Check does nothing
3828 -- (other than making sure Do_Range_Check is set) if the expander is
3829 -- not active.
3831 if Do_Range_Check (Expr) then
3832 Generate_Range_Check (Expr, Expr_Type, CE_Range_Check_Failed);
3833 end if;
3835 -- Add association Component => Expr if the caller requests it
3837 if Relocate then
3838 New_Expr := Relocate_Node (Expr);
3840 -- Since New_Expr is not gonna be analyzed later on, we need to
3841 -- propagate here the dimensions form Expr to New_Expr.
3843 Copy_Dimensions (Expr, New_Expr);
3845 else
3846 New_Expr := Expr;
3847 end if;
3849 Add_Association (New_C, New_Expr, New_Assoc_List);
3850 end Resolve_Aggr_Expr;
3852 -- Local variables
3854 Components : constant Elist_Id := New_Elmt_List;
3855 -- Components is the list of the record components whose value must be
3856 -- provided in the aggregate. This list does include discriminants.
3858 Expr : Node_Id;
3859 Component : Entity_Id;
3860 Component_Elmt : Elmt_Id;
3861 Positional_Expr : Node_Id;
3863 -- Start of processing for Resolve_Record_Aggregate
3865 begin
3866 -- A record aggregate is restricted in SPARK:
3868 -- Each named association can have only a single choice.
3869 -- OTHERS cannot be used.
3870 -- Positional and named associations cannot be mixed.
3872 if Present (Component_Associations (N))
3873 and then Present (First (Component_Associations (N)))
3874 then
3875 if Present (Expressions (N)) then
3876 Check_SPARK_05_Restriction
3877 ("named association cannot follow positional one",
3878 First (Choices (First (Component_Associations (N)))));
3879 end if;
3881 declare
3882 Assoc : Node_Id;
3884 begin
3885 Assoc := First (Component_Associations (N));
3886 while Present (Assoc) loop
3887 if List_Length (Choices (Assoc)) > 1 then
3888 Check_SPARK_05_Restriction
3889 ("component association in record aggregate must "
3890 & "contain a single choice", Assoc);
3891 end if;
3893 if Nkind (First (Choices (Assoc))) = N_Others_Choice then
3894 Check_SPARK_05_Restriction
3895 ("record aggregate cannot contain OTHERS", Assoc);
3896 end if;
3898 Assoc := Next (Assoc);
3899 end loop;
3900 end;
3901 end if;
3903 -- We may end up calling Duplicate_Subexpr on expressions that are
3904 -- attached to New_Assoc_List. For this reason we need to attach it
3905 -- to the tree by setting its parent pointer to N. This parent point
3906 -- will change in STEP 8 below.
3908 Set_Parent (New_Assoc_List, N);
3910 -- STEP 1: abstract type and null record verification
3912 if Is_Abstract_Type (Typ) then
3913 Error_Msg_N ("type of aggregate cannot be abstract", N);
3914 end if;
3916 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
3917 Set_Etype (N, Typ);
3918 return;
3920 elsif Present (First_Entity (Typ))
3921 and then Null_Record_Present (N)
3922 and then not Is_Tagged_Type (Typ)
3923 then
3924 Error_Msg_N ("record aggregate cannot be null", N);
3925 return;
3927 -- If the type has no components, then the aggregate should either
3928 -- have "null record", or in Ada 2005 it could instead have a single
3929 -- component association given by "others => <>". For Ada 95 we flag an
3930 -- error at this point, but for Ada 2005 we proceed with checking the
3931 -- associations below, which will catch the case where it's not an
3932 -- aggregate with "others => <>". Note that the legality of a <>
3933 -- aggregate for a null record type was established by AI05-016.
3935 elsif No (First_Entity (Typ))
3936 and then Ada_Version < Ada_2005
3937 then
3938 Error_Msg_N ("record aggregate must be null", N);
3939 return;
3940 end if;
3942 -- STEP 2: Verify aggregate structure
3944 Step_2 : declare
3945 Assoc : Node_Id;
3946 Bad_Aggregate : Boolean := False;
3947 Selector_Name : Node_Id;
3949 begin
3950 if Present (Component_Associations (N)) then
3951 Assoc := First (Component_Associations (N));
3952 else
3953 Assoc := Empty;
3954 end if;
3956 while Present (Assoc) loop
3957 Selector_Name := First (Choices (Assoc));
3958 while Present (Selector_Name) loop
3959 if Nkind (Selector_Name) = N_Identifier then
3960 null;
3962 elsif Nkind (Selector_Name) = N_Others_Choice then
3963 if Selector_Name /= First (Choices (Assoc))
3964 or else Present (Next (Selector_Name))
3965 then
3966 Error_Msg_N
3967 ("OTHERS must appear alone in a choice list",
3968 Selector_Name);
3969 return;
3971 elsif Present (Next (Assoc)) then
3972 Error_Msg_N
3973 ("OTHERS must appear last in an aggregate",
3974 Selector_Name);
3975 return;
3977 -- (Ada 2005): If this is an association with a box,
3978 -- indicate that the association need not represent
3979 -- any component.
3981 elsif Box_Present (Assoc) then
3982 Others_Box := 1;
3983 Box_Node := Assoc;
3984 end if;
3986 else
3987 Error_Msg_N
3988 ("selector name should be identifier or OTHERS",
3989 Selector_Name);
3990 Bad_Aggregate := True;
3991 end if;
3993 Next (Selector_Name);
3994 end loop;
3996 Next (Assoc);
3997 end loop;
3999 if Bad_Aggregate then
4000 return;
4001 end if;
4002 end Step_2;
4004 -- STEP 3: Find discriminant Values
4006 Step_3 : declare
4007 Discrim : Entity_Id;
4008 Missing_Discriminants : Boolean := False;
4010 begin
4011 if Present (Expressions (N)) then
4012 Positional_Expr := First (Expressions (N));
4013 else
4014 Positional_Expr := Empty;
4015 end if;
4017 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
4018 -- must not have unknown discriminants.
4020 if Is_Derived_Type (Typ)
4021 and then Has_Unknown_Discriminants (Root_Type (Typ))
4022 and then Nkind (N) /= N_Extension_Aggregate
4023 then
4024 Error_Msg_NE
4025 ("aggregate not available for type& whose ancestor "
4026 & "has unknown discriminants ", N, Typ);
4027 end if;
4029 if Has_Unknown_Discriminants (Typ)
4030 and then Present (Underlying_Record_View (Typ))
4031 then
4032 Discrim := First_Discriminant (Underlying_Record_View (Typ));
4033 elsif Has_Discriminants (Typ) then
4034 Discrim := First_Discriminant (Typ);
4035 else
4036 Discrim := Empty;
4037 end if;
4039 -- First find the discriminant values in the positional components
4041 while Present (Discrim) and then Present (Positional_Expr) loop
4042 if Discriminant_Present (Discrim) then
4043 Resolve_Aggr_Expr (Positional_Expr, Discrim);
4045 -- Ada 2005 (AI-231)
4047 if Ada_Version >= Ada_2005
4048 and then Known_Null (Positional_Expr)
4049 then
4050 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
4051 end if;
4053 Next (Positional_Expr);
4054 end if;
4056 if Present (Get_Value (Discrim, Component_Associations (N))) then
4057 Error_Msg_NE
4058 ("more than one value supplied for discriminant&",
4059 N, Discrim);
4060 end if;
4062 Next_Discriminant (Discrim);
4063 end loop;
4065 -- Find remaining discriminant values if any among named components
4067 while Present (Discrim) loop
4068 Expr := Get_Value (Discrim, Component_Associations (N), True);
4070 if not Discriminant_Present (Discrim) then
4071 if Present (Expr) then
4072 Error_Msg_NE
4073 ("more than one value supplied for discriminant &",
4074 N, Discrim);
4075 end if;
4077 elsif No (Expr) then
4078 Error_Msg_NE
4079 ("no value supplied for discriminant &", N, Discrim);
4080 Missing_Discriminants := True;
4082 else
4083 Resolve_Aggr_Expr (Expr, Discrim);
4084 end if;
4086 Next_Discriminant (Discrim);
4087 end loop;
4089 if Missing_Discriminants then
4090 return;
4091 end if;
4093 -- At this point and until the beginning of STEP 6, New_Assoc_List
4094 -- contains only the discriminants and their values.
4096 end Step_3;
4098 -- STEP 4: Set the Etype of the record aggregate
4100 -- ??? This code is pretty much a copy of Sem_Ch3.Build_Subtype. That
4101 -- routine should really be exported in sem_util or some such and used
4102 -- in sem_ch3 and here rather than have a copy of the code which is a
4103 -- maintenance nightmare.
4105 -- ??? Performance WARNING. The current implementation creates a new
4106 -- itype for all aggregates whose base type is discriminated. This means
4107 -- that for record aggregates nested inside an array aggregate we will
4108 -- create a new itype for each record aggregate if the array component
4109 -- type has discriminants. For large aggregates this may be a problem.
4110 -- What should be done in this case is to reuse itypes as much as
4111 -- possible.
4113 if Has_Discriminants (Typ)
4114 or else (Has_Unknown_Discriminants (Typ)
4115 and then Present (Underlying_Record_View (Typ)))
4116 then
4117 Build_Constrained_Itype : declare
4118 Constrs : constant List_Id := New_List;
4119 Loc : constant Source_Ptr := Sloc (N);
4120 Def_Id : Entity_Id;
4121 Indic : Node_Id;
4122 New_Assoc : Node_Id;
4123 Subtyp_Decl : Node_Id;
4125 begin
4126 New_Assoc := First (New_Assoc_List);
4127 while Present (New_Assoc) loop
4128 Append_To (Constrs, Duplicate_Subexpr (Expression (New_Assoc)));
4129 Next (New_Assoc);
4130 end loop;
4132 if Has_Unknown_Discriminants (Typ)
4133 and then Present (Underlying_Record_View (Typ))
4134 then
4135 Indic :=
4136 Make_Subtype_Indication (Loc,
4137 Subtype_Mark =>
4138 New_Occurrence_Of (Underlying_Record_View (Typ), Loc),
4139 Constraint =>
4140 Make_Index_Or_Discriminant_Constraint (Loc,
4141 Constraints => Constrs));
4142 else
4143 Indic :=
4144 Make_Subtype_Indication (Loc,
4145 Subtype_Mark =>
4146 New_Occurrence_Of (Base_Type (Typ), Loc),
4147 Constraint =>
4148 Make_Index_Or_Discriminant_Constraint (Loc,
4149 Constraints => Constrs));
4150 end if;
4152 Def_Id := Create_Itype (Ekind (Typ), N);
4154 Subtyp_Decl :=
4155 Make_Subtype_Declaration (Loc,
4156 Defining_Identifier => Def_Id,
4157 Subtype_Indication => Indic);
4158 Set_Parent (Subtyp_Decl, Parent (N));
4160 -- Itypes must be analyzed with checks off (see itypes.ads)
4162 Analyze (Subtyp_Decl, Suppress => All_Checks);
4164 Set_Etype (N, Def_Id);
4165 Check_Static_Discriminated_Subtype
4166 (Def_Id, Expression (First (New_Assoc_List)));
4167 end Build_Constrained_Itype;
4169 else
4170 Set_Etype (N, Typ);
4171 end if;
4173 -- STEP 5: Get remaining components according to discriminant values
4175 Step_5 : declare
4176 Dnode : Node_Id;
4177 Errors_Found : Boolean := False;
4178 Record_Def : Node_Id;
4179 Parent_Typ : Entity_Id;
4180 Parent_Typ_List : Elist_Id;
4181 Parent_Elmt : Elmt_Id;
4182 Root_Typ : Entity_Id;
4184 begin
4185 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
4186 Parent_Typ_List := New_Elmt_List;
4188 -- If this is an extension aggregate, the component list must
4189 -- include all components that are not in the given ancestor type.
4190 -- Otherwise, the component list must include components of all
4191 -- ancestors, starting with the root.
4193 if Nkind (N) = N_Extension_Aggregate then
4194 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
4196 else
4197 -- AI05-0115: check legality of aggregate for type with a
4198 -- private ancestor.
4200 Root_Typ := Root_Type (Typ);
4201 if Has_Private_Ancestor (Typ) then
4202 declare
4203 Ancestor : constant Entity_Id :=
4204 Find_Private_Ancestor (Typ);
4205 Ancestor_Unit : constant Entity_Id :=
4206 Cunit_Entity
4207 (Get_Source_Unit (Ancestor));
4208 Parent_Unit : constant Entity_Id :=
4209 Cunit_Entity (Get_Source_Unit
4210 (Base_Type (Etype (Ancestor))));
4211 begin
4212 -- Check whether we are in a scope that has full view
4213 -- over the private ancestor and its parent. This can
4214 -- only happen if the derivation takes place in a child
4215 -- unit of the unit that declares the parent, and we are
4216 -- in the private part or body of that child unit, else
4217 -- the aggregate is illegal.
4219 if Is_Child_Unit (Ancestor_Unit)
4220 and then Scope (Ancestor_Unit) = Parent_Unit
4221 and then In_Open_Scopes (Scope (Ancestor))
4222 and then
4223 (In_Private_Part (Scope (Ancestor))
4224 or else In_Package_Body (Scope (Ancestor)))
4225 then
4226 null;
4228 else
4229 Error_Msg_NE
4230 ("type of aggregate has private ancestor&!",
4231 N, Root_Typ);
4232 Error_Msg_N ("must use extension aggregate!", N);
4233 return;
4234 end if;
4235 end;
4236 end if;
4238 Dnode := Declaration_Node (Base_Type (Root_Typ));
4240 -- If we don't get a full declaration, then we have some error
4241 -- which will get signalled later so skip this part. Otherwise
4242 -- gather components of root that apply to the aggregate type.
4243 -- We use the base type in case there is an applicable stored
4244 -- constraint that renames the discriminants of the root.
4246 if Nkind (Dnode) = N_Full_Type_Declaration then
4247 Record_Def := Type_Definition (Dnode);
4248 Gather_Components
4249 (Base_Type (Typ),
4250 Component_List (Record_Def),
4251 Governed_By => New_Assoc_List,
4252 Into => Components,
4253 Report_Errors => Errors_Found);
4255 if Errors_Found then
4256 Error_Msg_N
4257 ("discriminant controlling variant part is not static",
4259 return;
4260 end if;
4261 end if;
4262 end if;
4264 Parent_Typ := Base_Type (Typ);
4265 while Parent_Typ /= Root_Typ loop
4266 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
4267 Parent_Typ := Etype (Parent_Typ);
4269 if Nkind (Parent (Base_Type (Parent_Typ))) =
4270 N_Private_Type_Declaration
4271 or else Nkind (Parent (Base_Type (Parent_Typ))) =
4272 N_Private_Extension_Declaration
4273 then
4274 if Nkind (N) /= N_Extension_Aggregate then
4275 Error_Msg_NE
4276 ("type of aggregate has private ancestor&!",
4277 N, Parent_Typ);
4278 Error_Msg_N ("must use extension aggregate!", N);
4279 return;
4281 elsif Parent_Typ /= Root_Typ then
4282 Error_Msg_NE
4283 ("ancestor part of aggregate must be private type&",
4284 Ancestor_Part (N), Parent_Typ);
4285 return;
4286 end if;
4288 -- The current view of ancestor part may be a private type,
4289 -- while the context type is always non-private.
4291 elsif Is_Private_Type (Root_Typ)
4292 and then Present (Full_View (Root_Typ))
4293 and then Nkind (N) = N_Extension_Aggregate
4294 then
4295 exit when Base_Type (Full_View (Root_Typ)) = Parent_Typ;
4296 end if;
4297 end loop;
4299 -- Now collect components from all other ancestors, beginning
4300 -- with the current type. If the type has unknown discriminants
4301 -- use the component list of the Underlying_Record_View, which
4302 -- needs to be used for the subsequent expansion of the aggregate
4303 -- into assignments.
4305 Parent_Elmt := First_Elmt (Parent_Typ_List);
4306 while Present (Parent_Elmt) loop
4307 Parent_Typ := Node (Parent_Elmt);
4309 if Has_Unknown_Discriminants (Parent_Typ)
4310 and then Present (Underlying_Record_View (Typ))
4311 then
4312 Parent_Typ := Underlying_Record_View (Parent_Typ);
4313 end if;
4315 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
4316 Gather_Components (Empty,
4317 Component_List (Record_Extension_Part (Record_Def)),
4318 Governed_By => New_Assoc_List,
4319 Into => Components,
4320 Report_Errors => Errors_Found);
4322 Next_Elmt (Parent_Elmt);
4323 end loop;
4325 -- Typ is not a derived tagged type
4327 else
4328 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
4330 if Null_Present (Record_Def) then
4331 null;
4333 elsif not Has_Unknown_Discriminants (Typ) then
4334 Gather_Components
4335 (Base_Type (Typ),
4336 Component_List (Record_Def),
4337 Governed_By => New_Assoc_List,
4338 Into => Components,
4339 Report_Errors => Errors_Found);
4341 else
4342 Gather_Components
4343 (Base_Type (Underlying_Record_View (Typ)),
4344 Component_List (Record_Def),
4345 Governed_By => New_Assoc_List,
4346 Into => Components,
4347 Report_Errors => Errors_Found);
4348 end if;
4349 end if;
4351 if Errors_Found then
4352 return;
4353 end if;
4354 end Step_5;
4356 -- STEP 6: Find component Values
4358 Component := Empty;
4359 Component_Elmt := First_Elmt (Components);
4361 -- First scan the remaining positional associations in the aggregate.
4362 -- Remember that at this point Positional_Expr contains the current
4363 -- positional association if any is left after looking for discriminant
4364 -- values in step 3.
4366 while Present (Positional_Expr) and then Present (Component_Elmt) loop
4367 Component := Node (Component_Elmt);
4368 Resolve_Aggr_Expr (Positional_Expr, Component);
4370 -- Ada 2005 (AI-231)
4372 if Ada_Version >= Ada_2005 and then Known_Null (Positional_Expr) then
4373 Check_Can_Never_Be_Null (Component, Positional_Expr);
4374 end if;
4376 if Present (Get_Value (Component, Component_Associations (N))) then
4377 Error_Msg_NE
4378 ("more than one value supplied for Component &", N, Component);
4379 end if;
4381 Next (Positional_Expr);
4382 Next_Elmt (Component_Elmt);
4383 end loop;
4385 if Present (Positional_Expr) then
4386 Error_Msg_N
4387 ("too many components for record aggregate", Positional_Expr);
4388 end if;
4390 -- Now scan for the named arguments of the aggregate
4392 while Present (Component_Elmt) loop
4393 Component := Node (Component_Elmt);
4394 Expr := Get_Value (Component, Component_Associations (N), True);
4396 -- Note: The previous call to Get_Value sets the value of the
4397 -- variable Is_Box_Present.
4399 -- Ada 2005 (AI-287): Handle components with default initialization.
4400 -- Note: This feature was originally added to Ada 2005 for limited
4401 -- but it was finally allowed with any type.
4403 if Is_Box_Present then
4404 Check_Box_Component : declare
4405 Ctyp : constant Entity_Id := Etype (Component);
4407 begin
4408 -- If there is a default expression for the aggregate, copy
4409 -- it into a new association. This copy must modify the scopes
4410 -- of internal types that may be attached to the expression
4411 -- (e.g. index subtypes of arrays) because in general the type
4412 -- declaration and the aggregate appear in different scopes,
4413 -- and the backend requires the scope of the type to match the
4414 -- point at which it is elaborated.
4416 -- If the component has an initialization procedure (IP) we
4417 -- pass the component to the expander, which will generate
4418 -- the call to such IP.
4420 -- If the component has discriminants, their values must
4421 -- be taken from their subtype. This is indispensable for
4422 -- constraints that are given by the current instance of an
4423 -- enclosing type, to allow the expansion of the aggregate to
4424 -- replace the reference to the current instance by the target
4425 -- object of the aggregate.
4427 if Present (Parent (Component))
4428 and then Nkind (Parent (Component)) = N_Component_Declaration
4429 and then Present (Expression (Parent (Component)))
4430 then
4431 Expr :=
4432 New_Copy_Tree_And_Copy_Dimensions
4433 (Expression (Parent (Component)),
4434 New_Scope => Current_Scope,
4435 New_Sloc => Sloc (N));
4437 Add_Association
4438 (Component => Component,
4439 Expr => Expr,
4440 Assoc_List => New_Assoc_List);
4441 Set_Has_Self_Reference (N);
4443 -- A box-defaulted access component gets the value null. Also
4444 -- included are components of private types whose underlying
4445 -- type is an access type. In either case set the type of the
4446 -- literal, for subsequent use in semantic checks.
4448 elsif Present (Underlying_Type (Ctyp))
4449 and then Is_Access_Type (Underlying_Type (Ctyp))
4450 then
4451 -- If the component's type is private with an access type as
4452 -- its underlying type then we have to create an unchecked
4453 -- conversion to satisfy type checking.
4455 if Is_Private_Type (Ctyp) then
4456 declare
4457 Qual_Null : constant Node_Id :=
4458 Make_Qualified_Expression (Sloc (N),
4459 Subtype_Mark =>
4460 New_Occurrence_Of
4461 (Underlying_Type (Ctyp), Sloc (N)),
4462 Expression => Make_Null (Sloc (N)));
4464 Convert_Null : constant Node_Id :=
4465 Unchecked_Convert_To
4466 (Ctyp, Qual_Null);
4468 begin
4469 Analyze_And_Resolve (Convert_Null, Ctyp);
4470 Add_Association
4471 (Component => Component,
4472 Expr => Convert_Null,
4473 Assoc_List => New_Assoc_List);
4474 end;
4476 -- Otherwise the component type is non-private
4478 else
4479 Expr := Make_Null (Sloc (N));
4480 Set_Etype (Expr, Ctyp);
4482 Add_Association
4483 (Component => Component,
4484 Expr => Expr,
4485 Assoc_List => New_Assoc_List);
4486 end if;
4488 -- Ada 2012: If component is scalar with default value, use it
4490 elsif Is_Scalar_Type (Ctyp)
4491 and then Has_Default_Aspect (Ctyp)
4492 then
4493 Add_Association
4494 (Component => Component,
4495 Expr =>
4496 Default_Aspect_Value
4497 (First_Subtype (Underlying_Type (Ctyp))),
4498 Assoc_List => New_Assoc_List);
4500 elsif Has_Non_Null_Base_Init_Proc (Ctyp)
4501 or else not Expander_Active
4502 then
4503 if Is_Record_Type (Ctyp)
4504 and then Has_Discriminants (Ctyp)
4505 and then not Is_Private_Type (Ctyp)
4506 then
4507 -- We build a partially initialized aggregate with the
4508 -- values of the discriminants and box initialization
4509 -- for the rest, if other components are present.
4511 -- The type of the aggregate is the known subtype of
4512 -- the component. The capture of discriminants must be
4513 -- recursive because subcomponents may be constrained
4514 -- (transitively) by discriminants of enclosing types.
4515 -- For a private type with discriminants, a call to the
4516 -- initialization procedure will be generated, and no
4517 -- subaggregate is needed.
4519 Capture_Discriminants : declare
4520 Loc : constant Source_Ptr := Sloc (N);
4521 Expr : Node_Id;
4523 begin
4524 Expr := Make_Aggregate (Loc, New_List, New_List);
4525 Set_Etype (Expr, Ctyp);
4527 -- If the enclosing type has discriminants, they have
4528 -- been collected in the aggregate earlier, and they
4529 -- may appear as constraints of subcomponents.
4531 -- Similarly if this component has discriminants, they
4532 -- might in turn be propagated to their components.
4534 if Has_Discriminants (Typ) then
4535 Add_Discriminant_Values (Expr, New_Assoc_List);
4536 Propagate_Discriminants (Expr, New_Assoc_List);
4538 elsif Has_Discriminants (Ctyp) then
4539 Add_Discriminant_Values
4540 (Expr, Component_Associations (Expr));
4541 Propagate_Discriminants
4542 (Expr, Component_Associations (Expr));
4544 else
4545 declare
4546 Comp : Entity_Id;
4548 begin
4549 -- If the type has additional components, create
4550 -- an OTHERS box association for them.
4552 Comp := First_Component (Ctyp);
4553 while Present (Comp) loop
4554 if Ekind (Comp) = E_Component then
4555 if not Is_Record_Type (Etype (Comp)) then
4556 Append_To
4557 (Component_Associations (Expr),
4558 Make_Component_Association (Loc,
4559 Choices =>
4560 New_List (
4561 Make_Others_Choice (Loc)),
4562 Expression => Empty,
4563 Box_Present => True));
4564 end if;
4566 exit;
4567 end if;
4569 Next_Component (Comp);
4570 end loop;
4571 end;
4572 end if;
4574 Add_Association
4575 (Component => Component,
4576 Expr => Expr,
4577 Assoc_List => New_Assoc_List);
4578 end Capture_Discriminants;
4580 -- Otherwise the component type is not a record, or it has
4581 -- not discriminants, or it is private.
4583 else
4584 Add_Association
4585 (Component => Component,
4586 Expr => Empty,
4587 Assoc_List => New_Assoc_List,
4588 Is_Box_Present => True);
4589 end if;
4591 -- Otherwise we only need to resolve the expression if the
4592 -- component has partially initialized values (required to
4593 -- expand the corresponding assignments and run-time checks).
4595 elsif Present (Expr)
4596 and then Is_Partially_Initialized_Type (Ctyp)
4597 then
4598 Resolve_Aggr_Expr (Expr, Component);
4599 end if;
4600 end Check_Box_Component;
4602 elsif No (Expr) then
4604 -- Ignore hidden components associated with the position of the
4605 -- interface tags: these are initialized dynamically.
4607 if not Present (Related_Type (Component)) then
4608 Error_Msg_NE
4609 ("no value supplied for component &!", N, Component);
4610 end if;
4612 else
4613 Resolve_Aggr_Expr (Expr, Component);
4614 end if;
4616 Next_Elmt (Component_Elmt);
4617 end loop;
4619 -- STEP 7: check for invalid components + check type in choice list
4621 Step_7 : declare
4622 Assoc : Node_Id;
4623 New_Assoc : Node_Id;
4625 Selectr : Node_Id;
4626 -- Selector name
4628 Typech : Entity_Id;
4629 -- Type of first component in choice list
4631 begin
4632 if Present (Component_Associations (N)) then
4633 Assoc := First (Component_Associations (N));
4634 else
4635 Assoc := Empty;
4636 end if;
4638 Verification : while Present (Assoc) loop
4639 Selectr := First (Choices (Assoc));
4640 Typech := Empty;
4642 if Nkind (Selectr) = N_Others_Choice then
4644 -- Ada 2005 (AI-287): others choice may have expression or box
4646 if No (Others_Etype) and then Others_Box = 0 then
4647 Error_Msg_N
4648 ("OTHERS must represent at least one component", Selectr);
4650 elsif Others_Box = 1 and then Warn_On_Redundant_Constructs then
4651 Error_Msg_N ("others choice is redundant?", Box_Node);
4652 Error_Msg_N
4653 ("\previous choices cover all components?", Box_Node);
4654 end if;
4656 exit Verification;
4657 end if;
4659 while Present (Selectr) loop
4660 New_Assoc := First (New_Assoc_List);
4661 while Present (New_Assoc) loop
4662 Component := First (Choices (New_Assoc));
4664 if Chars (Selectr) = Chars (Component) then
4665 if Style_Check then
4666 Check_Identifier (Selectr, Entity (Component));
4667 end if;
4669 exit;
4670 end if;
4672 Next (New_Assoc);
4673 end loop;
4675 -- If no association, this is not a legal component of the type
4676 -- in question, unless its association is provided with a box.
4678 if No (New_Assoc) then
4679 if Box_Present (Parent (Selectr)) then
4681 -- This may still be a bogus component with a box. Scan
4682 -- list of components to verify that a component with
4683 -- that name exists.
4685 declare
4686 C : Entity_Id;
4688 begin
4689 C := First_Component (Typ);
4690 while Present (C) loop
4691 if Chars (C) = Chars (Selectr) then
4693 -- If the context is an extension aggregate,
4694 -- the component must not be inherited from
4695 -- the ancestor part of the aggregate.
4697 if Nkind (N) /= N_Extension_Aggregate
4698 or else
4699 Scope (Original_Record_Component (C)) /=
4700 Etype (Ancestor_Part (N))
4701 then
4702 exit;
4703 end if;
4704 end if;
4706 Next_Component (C);
4707 end loop;
4709 if No (C) then
4710 Error_Msg_Node_2 := Typ;
4711 Error_Msg_N ("& is not a component of}", Selectr);
4712 end if;
4713 end;
4715 elsif Chars (Selectr) /= Name_uTag
4716 and then Chars (Selectr) /= Name_uParent
4717 then
4718 if not Has_Discriminants (Typ) then
4719 Error_Msg_Node_2 := Typ;
4720 Error_Msg_N ("& is not a component of}", Selectr);
4721 else
4722 Error_Msg_N
4723 ("& is not a component of the aggregate subtype",
4724 Selectr);
4725 end if;
4727 Check_Misspelled_Component (Components, Selectr);
4728 end if;
4730 elsif No (Typech) then
4731 Typech := Base_Type (Etype (Component));
4733 -- AI05-0199: In Ada 2012, several components of anonymous
4734 -- access types can appear in a choice list, as long as the
4735 -- designated types match.
4737 elsif Typech /= Base_Type (Etype (Component)) then
4738 if Ada_Version >= Ada_2012
4739 and then Ekind (Typech) = E_Anonymous_Access_Type
4740 and then
4741 Ekind (Etype (Component)) = E_Anonymous_Access_Type
4742 and then Base_Type (Designated_Type (Typech)) =
4743 Base_Type (Designated_Type (Etype (Component)))
4744 and then
4745 Subtypes_Statically_Match (Typech, (Etype (Component)))
4746 then
4747 null;
4749 elsif not Box_Present (Parent (Selectr)) then
4750 Error_Msg_N
4751 ("components in choice list must have same type",
4752 Selectr);
4753 end if;
4754 end if;
4756 Next (Selectr);
4757 end loop;
4759 Next (Assoc);
4760 end loop Verification;
4761 end Step_7;
4763 -- STEP 8: replace the original aggregate
4765 Step_8 : declare
4766 New_Aggregate : constant Node_Id := New_Copy (N);
4768 begin
4769 Set_Expressions (New_Aggregate, No_List);
4770 Set_Etype (New_Aggregate, Etype (N));
4771 Set_Component_Associations (New_Aggregate, New_Assoc_List);
4772 Set_Check_Actuals (New_Aggregate, Check_Actuals (N));
4774 Rewrite (N, New_Aggregate);
4775 end Step_8;
4777 -- Check the dimensions of the components in the record aggregate
4779 Analyze_Dimension_Extension_Or_Record_Aggregate (N);
4780 end Resolve_Record_Aggregate;
4782 -----------------------------
4783 -- Check_Can_Never_Be_Null --
4784 -----------------------------
4786 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
4787 Comp_Typ : Entity_Id;
4789 begin
4790 pragma Assert
4791 (Ada_Version >= Ada_2005
4792 and then Present (Expr)
4793 and then Known_Null (Expr));
4795 case Ekind (Typ) is
4796 when E_Array_Type =>
4797 Comp_Typ := Component_Type (Typ);
4799 when E_Component
4800 | E_Discriminant
4802 Comp_Typ := Etype (Typ);
4804 when others =>
4805 return;
4806 end case;
4808 if Can_Never_Be_Null (Comp_Typ) then
4810 -- Here we know we have a constraint error. Note that we do not use
4811 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
4812 -- seem the more natural approach. That's because in some cases the
4813 -- components are rewritten, and the replacement would be missed.
4814 -- We do not mark the whole aggregate as raising a constraint error,
4815 -- because the association may be a null array range.
4817 Error_Msg_N
4818 ("(Ada 2005) null not allowed in null-excluding component??", Expr);
4819 Error_Msg_N
4820 ("\Constraint_Error will be raised at run time??", Expr);
4822 Rewrite (Expr,
4823 Make_Raise_Constraint_Error
4824 (Sloc (Expr), Reason => CE_Access_Check_Failed));
4825 Set_Etype (Expr, Comp_Typ);
4826 Set_Analyzed (Expr);
4827 end if;
4828 end Check_Can_Never_Be_Null;
4830 ---------------------
4831 -- Sort_Case_Table --
4832 ---------------------
4834 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
4835 U : constant Int := Case_Table'Last;
4836 K : Int;
4837 J : Int;
4838 T : Case_Bounds;
4840 begin
4841 K := 1;
4842 while K < U loop
4843 T := Case_Table (K + 1);
4845 J := K + 1;
4846 while J > 1
4847 and then Expr_Value (Case_Table (J - 1).Lo) > Expr_Value (T.Lo)
4848 loop
4849 Case_Table (J) := Case_Table (J - 1);
4850 J := J - 1;
4851 end loop;
4853 Case_Table (J) := T;
4854 K := K + 1;
4855 end loop;
4856 end Sort_Case_Table;
4858 end Sem_Aggr;