Small ChangeLog tweak.
[official-gcc.git] / gcc / ada / sem_aggr.adb
blob1249fa03fed62ddbe48bd3862c437708951ad208
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ A G G R --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2017, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Exp_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 of
990 -- characters which can be replaced by a string literal.
992 -- Do not perform this transformation if this was a string literal
993 -- to start with, whose components needed constraint checks, or if
994 -- the component type is non-static, because it will require those
995 -- checks and be transformed back into an aggregate. If the index
996 -- type is not Integer the aggregate may represent a user-defined
997 -- string type but the context might need the original type so we
998 -- do not perform the transformation at this point.
1000 if Number_Dimensions (Typ) = 1
1001 and then Is_Standard_Character_Type (Component_Type (Typ))
1002 and then No (Component_Associations (N))
1003 and then not Is_Limited_Composite (Typ)
1004 and then not Is_Private_Composite (Typ)
1005 and then not Is_Bit_Packed_Array (Typ)
1006 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
1007 and then Is_OK_Static_Subtype (Component_Type (Typ))
1008 and then Base_Type (Etype (First_Index (Typ))) =
1009 Base_Type (Standard_Integer)
1010 then
1011 declare
1012 Expr : Node_Id;
1014 begin
1015 Expr := First (Expressions (N));
1016 while Present (Expr) loop
1017 exit when Nkind (Expr) /= N_Character_Literal;
1018 Next (Expr);
1019 end loop;
1021 if No (Expr) then
1022 Start_String;
1024 Expr := First (Expressions (N));
1025 while Present (Expr) loop
1026 Store_String_Char (UI_To_CC (Char_Literal_Value (Expr)));
1027 Next (Expr);
1028 end loop;
1030 Rewrite (N, Make_String_Literal (Loc, End_String));
1032 Analyze_And_Resolve (N, Typ);
1033 return;
1034 end if;
1035 end;
1036 end if;
1038 -- Here if we have a real aggregate to deal with
1040 Array_Aggregate : declare
1041 Aggr_Resolved : Boolean;
1043 Aggr_Typ : constant Entity_Id := Etype (Typ);
1044 -- This is the unconstrained array type, which is the type against
1045 -- which the aggregate is to be resolved. Typ itself is the array
1046 -- type of the context which may not be the same subtype as the
1047 -- subtype for the final aggregate.
1049 begin
1050 -- In the following we determine whether an OTHERS choice is
1051 -- allowed inside the array aggregate. The test checks the context
1052 -- in which the array aggregate occurs. If the context does not
1053 -- permit it, or the aggregate type is unconstrained, an OTHERS
1054 -- choice is not allowed (except that it is always allowed on the
1055 -- right-hand side of an assignment statement; in this case the
1056 -- constrainedness of the type doesn't matter).
1058 -- If expansion is disabled (generic context, or semantics-only
1059 -- mode) actual subtypes cannot be constructed, and the type of an
1060 -- object may be its unconstrained nominal type. However, if the
1061 -- context is an assignment, we assume that OTHERS is allowed,
1062 -- because the target of the assignment will have a constrained
1063 -- subtype when fully compiled.
1065 -- Note that there is no node for Explicit_Actual_Parameter.
1066 -- To test for this context we therefore have to test for node
1067 -- N_Parameter_Association which itself appears only if there is a
1068 -- formal parameter. Consequently we also need to test for
1069 -- N_Procedure_Call_Statement or N_Function_Call.
1071 -- The context may be an N_Reference node, created by expansion.
1072 -- Legality of the others clause was established in the source,
1073 -- so the context is legal.
1075 Set_Etype (N, Aggr_Typ); -- May be overridden later on
1077 if Pkind = N_Assignment_Statement
1078 or else (Is_Constrained (Typ)
1079 and then
1080 (Pkind = N_Parameter_Association or else
1081 Pkind = N_Function_Call or else
1082 Pkind = N_Procedure_Call_Statement or else
1083 Pkind = N_Generic_Association or else
1084 Pkind = N_Formal_Object_Declaration or else
1085 Pkind = N_Simple_Return_Statement or else
1086 Pkind = N_Object_Declaration or else
1087 Pkind = N_Component_Declaration or else
1088 Pkind = N_Parameter_Specification or else
1089 Pkind = N_Qualified_Expression or else
1090 Pkind = N_Reference or else
1091 Pkind = N_Aggregate or else
1092 Pkind = N_Extension_Aggregate or else
1093 Pkind = N_Component_Association))
1094 then
1095 Aggr_Resolved :=
1096 Resolve_Array_Aggregate
1098 Index => First_Index (Aggr_Typ),
1099 Index_Constr => First_Index (Typ),
1100 Component_Typ => Component_Type (Typ),
1101 Others_Allowed => True);
1102 else
1103 Aggr_Resolved :=
1104 Resolve_Array_Aggregate
1106 Index => First_Index (Aggr_Typ),
1107 Index_Constr => First_Index (Aggr_Typ),
1108 Component_Typ => Component_Type (Typ),
1109 Others_Allowed => False);
1110 end if;
1112 if not Aggr_Resolved then
1114 -- A parenthesized expression may have been intended as an
1115 -- aggregate, leading to a type error when analyzing the
1116 -- component. This can also happen for a nested component
1117 -- (see Analyze_Aggr_Expr).
1119 if Paren_Count (N) > 0 then
1120 Error_Msg_N
1121 ("positional aggregate cannot have one component", N);
1122 end if;
1124 Aggr_Subtyp := Any_Composite;
1126 else
1127 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1128 end if;
1130 Set_Etype (N, Aggr_Subtyp);
1131 end Array_Aggregate;
1133 elsif Is_Private_Type (Typ)
1134 and then Present (Full_View (Typ))
1135 and then (In_Inlined_Body or In_Instance_Body)
1136 and then Is_Composite_Type (Full_View (Typ))
1137 then
1138 Resolve (N, Full_View (Typ));
1140 else
1141 Error_Msg_N ("illegal context for aggregate", N);
1142 end if;
1144 -- If we can determine statically that the evaluation of the aggregate
1145 -- raises Constraint_Error, then replace the aggregate with an
1146 -- N_Raise_Constraint_Error node, but set the Etype to the right
1147 -- aggregate subtype. Gigi needs this.
1149 if Raises_Constraint_Error (N) then
1150 Aggr_Subtyp := Etype (N);
1151 Rewrite (N,
1152 Make_Raise_Constraint_Error (Loc, Reason => CE_Range_Check_Failed));
1153 Set_Raises_Constraint_Error (N);
1154 Set_Etype (N, Aggr_Subtyp);
1155 Set_Analyzed (N);
1156 end if;
1158 Check_Function_Writable_Actuals (N);
1159 end Resolve_Aggregate;
1161 -----------------------------
1162 -- Resolve_Array_Aggregate --
1163 -----------------------------
1165 function Resolve_Array_Aggregate
1166 (N : Node_Id;
1167 Index : Node_Id;
1168 Index_Constr : Node_Id;
1169 Component_Typ : Entity_Id;
1170 Others_Allowed : Boolean) return Boolean
1172 Loc : constant Source_Ptr := Sloc (N);
1174 Failure : constant Boolean := False;
1175 Success : constant Boolean := True;
1177 Index_Typ : constant Entity_Id := Etype (Index);
1178 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1179 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1180 -- The type of the index corresponding to the array sub-aggregate along
1181 -- with its low and upper bounds.
1183 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1184 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1185 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1186 -- Ditto for the base type
1188 Others_Present : Boolean := False;
1190 Nb_Choices : Nat := 0;
1191 -- Contains the overall number of named choices in this sub-aggregate
1193 function Add (Val : Uint; To : Node_Id) return Node_Id;
1194 -- Creates a new expression node where Val is added to expression To.
1195 -- Tries to constant fold whenever possible. To must be an already
1196 -- analyzed expression.
1198 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1199 -- Checks that AH (the upper bound of an array aggregate) is less than
1200 -- or equal to BH (the upper bound of the index base type). If the check
1201 -- fails, a warning is emitted, the Raises_Constraint_Error flag of N is
1202 -- set, and AH is replaced with a duplicate of BH.
1204 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1205 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1206 -- warning if not and sets the Raises_Constraint_Error flag in N.
1208 procedure Check_Length (L, H : Node_Id; Len : Uint);
1209 -- Checks that range L .. H contains at least Len elements. Emits a
1210 -- warning if not and sets the Raises_Constraint_Error flag in N.
1212 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1213 -- Returns True if range L .. H is dynamic or null
1215 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1216 -- Given expression node From, this routine sets OK to False if it
1217 -- cannot statically evaluate From. Otherwise it stores this static
1218 -- value into Value.
1220 function Resolve_Aggr_Expr
1221 (Expr : Node_Id;
1222 Single_Elmt : Boolean) return Boolean;
1223 -- Resolves aggregate expression Expr. Returns False if resolution
1224 -- fails. If Single_Elmt is set to False, the expression Expr may be
1225 -- used to initialize several array aggregate elements (this can happen
1226 -- for discrete choices such as "L .. H => Expr" or the OTHERS choice).
1227 -- In this event we do not resolve Expr unless expansion is disabled.
1228 -- To know why, see the DELAYED COMPONENT RESOLUTION note above.
1230 -- NOTE: In the case of "... => <>", we pass the in the
1231 -- N_Component_Association node as Expr, since there is no Expression in
1232 -- that case, and we need a Sloc for the error message.
1234 procedure Resolve_Iterated_Component_Association
1235 (N : Node_Id;
1236 Index_Typ : Entity_Id);
1237 -- For AI12-061
1239 ---------
1240 -- Add --
1241 ---------
1243 function Add (Val : Uint; To : Node_Id) return Node_Id is
1244 Expr_Pos : Node_Id;
1245 Expr : Node_Id;
1246 To_Pos : Node_Id;
1248 begin
1249 if Raises_Constraint_Error (To) then
1250 return To;
1251 end if;
1253 -- First test if we can do constant folding
1255 if Compile_Time_Known_Value (To)
1256 or else Nkind (To) = N_Integer_Literal
1257 then
1258 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1259 Set_Is_Static_Expression (Expr_Pos);
1260 Set_Etype (Expr_Pos, Etype (To));
1261 Set_Analyzed (Expr_Pos, Analyzed (To));
1263 if not Is_Enumeration_Type (Index_Typ) then
1264 Expr := Expr_Pos;
1266 -- If we are dealing with enumeration return
1267 -- Index_Typ'Val (Expr_Pos)
1269 else
1270 Expr :=
1271 Make_Attribute_Reference
1272 (Loc,
1273 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1274 Attribute_Name => Name_Val,
1275 Expressions => New_List (Expr_Pos));
1276 end if;
1278 return Expr;
1279 end if;
1281 -- If we are here no constant folding possible
1283 if not Is_Enumeration_Type (Index_Base) then
1284 Expr :=
1285 Make_Op_Add (Loc,
1286 Left_Opnd => Duplicate_Subexpr (To),
1287 Right_Opnd => Make_Integer_Literal (Loc, Val));
1289 -- If we are dealing with enumeration return
1290 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1292 else
1293 To_Pos :=
1294 Make_Attribute_Reference
1295 (Loc,
1296 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1297 Attribute_Name => Name_Pos,
1298 Expressions => New_List (Duplicate_Subexpr (To)));
1300 Expr_Pos :=
1301 Make_Op_Add (Loc,
1302 Left_Opnd => To_Pos,
1303 Right_Opnd => Make_Integer_Literal (Loc, Val));
1305 Expr :=
1306 Make_Attribute_Reference
1307 (Loc,
1308 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1309 Attribute_Name => Name_Val,
1310 Expressions => New_List (Expr_Pos));
1312 -- If the index type has a non standard representation, the
1313 -- attributes 'Val and 'Pos expand into function calls and the
1314 -- resulting expression is considered non-safe for reevaluation
1315 -- by the backend. Relocate it into a constant temporary in order
1316 -- to make it safe for reevaluation.
1318 if Has_Non_Standard_Rep (Etype (N)) then
1319 declare
1320 Def_Id : Entity_Id;
1322 begin
1323 Def_Id := Make_Temporary (Loc, 'R', Expr);
1324 Set_Etype (Def_Id, Index_Typ);
1325 Insert_Action (N,
1326 Make_Object_Declaration (Loc,
1327 Defining_Identifier => Def_Id,
1328 Object_Definition =>
1329 New_Occurrence_Of (Index_Typ, Loc),
1330 Constant_Present => True,
1331 Expression => Relocate_Node (Expr)));
1333 Expr := New_Occurrence_Of (Def_Id, Loc);
1334 end;
1335 end if;
1336 end if;
1338 return Expr;
1339 end Add;
1341 -----------------
1342 -- Check_Bound --
1343 -----------------
1345 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1346 Val_BH : Uint;
1347 Val_AH : Uint;
1349 OK_BH : Boolean;
1350 OK_AH : Boolean;
1352 begin
1353 Get (Value => Val_BH, From => BH, OK => OK_BH);
1354 Get (Value => Val_AH, From => AH, OK => OK_AH);
1356 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1357 Set_Raises_Constraint_Error (N);
1358 Error_Msg_Warn := SPARK_Mode /= On;
1359 Error_Msg_N ("upper bound out of range<<", AH);
1360 Error_Msg_N ("\Constraint_Error [<<", AH);
1362 -- You need to set AH to BH or else in the case of enumerations
1363 -- indexes we will not be able to resolve the aggregate bounds.
1365 AH := Duplicate_Subexpr (BH);
1366 end if;
1367 end Check_Bound;
1369 ------------------
1370 -- Check_Bounds --
1371 ------------------
1373 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1374 Val_L : Uint;
1375 Val_H : Uint;
1376 Val_AL : Uint;
1377 Val_AH : Uint;
1379 OK_L : Boolean;
1380 OK_H : Boolean;
1382 OK_AL : Boolean;
1383 OK_AH : Boolean;
1384 pragma Warnings (Off, OK_AL);
1385 pragma Warnings (Off, OK_AH);
1387 begin
1388 if Raises_Constraint_Error (N)
1389 or else Dynamic_Or_Null_Range (AL, AH)
1390 then
1391 return;
1392 end if;
1394 Get (Value => Val_L, From => L, OK => OK_L);
1395 Get (Value => Val_H, From => H, OK => OK_H);
1397 Get (Value => Val_AL, From => AL, OK => OK_AL);
1398 Get (Value => Val_AH, From => AH, OK => OK_AH);
1400 if OK_L and then Val_L > Val_AL then
1401 Set_Raises_Constraint_Error (N);
1402 Error_Msg_Warn := SPARK_Mode /= On;
1403 Error_Msg_N ("lower bound of aggregate out of range<<", N);
1404 Error_Msg_N ("\Constraint_Error [<<", N);
1405 end if;
1407 if OK_H and then Val_H < Val_AH then
1408 Set_Raises_Constraint_Error (N);
1409 Error_Msg_Warn := SPARK_Mode /= On;
1410 Error_Msg_N ("upper bound of aggregate out of range<<", N);
1411 Error_Msg_N ("\Constraint_Error [<<", N);
1412 end if;
1413 end Check_Bounds;
1415 ------------------
1416 -- Check_Length --
1417 ------------------
1419 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1420 Val_L : Uint;
1421 Val_H : Uint;
1423 OK_L : Boolean;
1424 OK_H : Boolean;
1426 Range_Len : Uint;
1428 begin
1429 if Raises_Constraint_Error (N) then
1430 return;
1431 end if;
1433 Get (Value => Val_L, From => L, OK => OK_L);
1434 Get (Value => Val_H, From => H, OK => OK_H);
1436 if not OK_L or else not OK_H then
1437 return;
1438 end if;
1440 -- If null range length is zero
1442 if Val_L > Val_H then
1443 Range_Len := Uint_0;
1444 else
1445 Range_Len := Val_H - Val_L + 1;
1446 end if;
1448 if Range_Len < Len then
1449 Set_Raises_Constraint_Error (N);
1450 Error_Msg_Warn := SPARK_Mode /= On;
1451 Error_Msg_N ("too many elements<<", N);
1452 Error_Msg_N ("\Constraint_Error [<<", N);
1453 end if;
1454 end Check_Length;
1456 ---------------------------
1457 -- Dynamic_Or_Null_Range --
1458 ---------------------------
1460 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1461 Val_L : Uint;
1462 Val_H : Uint;
1464 OK_L : Boolean;
1465 OK_H : Boolean;
1467 begin
1468 Get (Value => Val_L, From => L, OK => OK_L);
1469 Get (Value => Val_H, From => H, OK => OK_H);
1471 return not OK_L or else not OK_H
1472 or else not Is_OK_Static_Expression (L)
1473 or else not Is_OK_Static_Expression (H)
1474 or else Val_L > Val_H;
1475 end Dynamic_Or_Null_Range;
1477 ---------
1478 -- Get --
1479 ---------
1481 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1482 begin
1483 OK := True;
1485 if Compile_Time_Known_Value (From) then
1486 Value := Expr_Value (From);
1488 -- If expression From is something like Some_Type'Val (10) then
1489 -- Value = 10.
1491 elsif Nkind (From) = N_Attribute_Reference
1492 and then Attribute_Name (From) = Name_Val
1493 and then Compile_Time_Known_Value (First (Expressions (From)))
1494 then
1495 Value := Expr_Value (First (Expressions (From)));
1496 else
1497 Value := Uint_0;
1498 OK := False;
1499 end if;
1500 end Get;
1502 -----------------------
1503 -- Resolve_Aggr_Expr --
1504 -----------------------
1506 function Resolve_Aggr_Expr
1507 (Expr : Node_Id;
1508 Single_Elmt : Boolean) return Boolean
1510 Nxt_Ind : constant Node_Id := Next_Index (Index);
1511 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
1512 -- Index is the current index corresponding to the expression
1514 Resolution_OK : Boolean := True;
1515 -- Set to False if resolution of the expression failed
1517 begin
1518 -- Defend against previous errors
1520 if Nkind (Expr) = N_Error
1521 or else Error_Posted (Expr)
1522 then
1523 return True;
1524 end if;
1526 -- If the array type against which we are resolving the aggregate
1527 -- has several dimensions, the expressions nested inside the
1528 -- aggregate must be further aggregates (or strings).
1530 if Present (Nxt_Ind) then
1531 if Nkind (Expr) /= N_Aggregate then
1533 -- A string literal can appear where a one-dimensional array
1534 -- of characters is expected. If the literal looks like an
1535 -- operator, it is still an operator symbol, which will be
1536 -- transformed into a string when analyzed.
1538 if Is_Character_Type (Component_Typ)
1539 and then No (Next_Index (Nxt_Ind))
1540 and then Nkind_In (Expr, N_String_Literal, N_Operator_Symbol)
1541 then
1542 -- A string literal used in a multidimensional array
1543 -- aggregate in place of the final one-dimensional
1544 -- aggregate must not be enclosed in parentheses.
1546 if Paren_Count (Expr) /= 0 then
1547 Error_Msg_N ("no parenthesis allowed here", Expr);
1548 end if;
1550 Make_String_Into_Aggregate (Expr);
1552 else
1553 Error_Msg_N ("nested array aggregate expected", Expr);
1555 -- If the expression is parenthesized, this may be
1556 -- a missing component association for a 1-aggregate.
1558 if Paren_Count (Expr) > 0 then
1559 Error_Msg_N
1560 ("\if single-component aggregate is intended, "
1561 & "write e.g. (1 ='> ...)", Expr);
1562 end if;
1564 return Failure;
1565 end if;
1566 end if;
1568 -- If it's "... => <>", nothing to resolve
1570 if Nkind (Expr) = N_Component_Association then
1571 pragma Assert (Box_Present (Expr));
1572 return Success;
1573 end if;
1575 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
1576 -- Required to check the null-exclusion attribute (if present).
1577 -- This value may be overridden later on.
1579 Set_Etype (Expr, Etype (N));
1581 Resolution_OK := Resolve_Array_Aggregate
1582 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1584 else
1585 -- If it's "... => <>", nothing to resolve
1587 if Nkind (Expr) = N_Component_Association then
1588 pragma Assert (Box_Present (Expr));
1589 return Success;
1590 end if;
1592 -- Do not resolve the expressions of discrete or others choices
1593 -- unless the expression covers a single component, or the
1594 -- expander is inactive.
1596 -- In SPARK mode, expressions that can perform side-effects will
1597 -- be recognized by the gnat2why back-end, and the whole
1598 -- subprogram will be ignored. So semantic analysis can be
1599 -- performed safely.
1601 if Single_Elmt
1602 or else not Expander_Active
1603 or else In_Spec_Expression
1604 then
1605 Analyze_And_Resolve (Expr, Component_Typ);
1606 Check_Expr_OK_In_Limited_Aggregate (Expr);
1607 Check_Non_Static_Context (Expr);
1608 Aggregate_Constraint_Checks (Expr, Component_Typ);
1609 Check_Unset_Reference (Expr);
1610 end if;
1611 end if;
1613 -- If an aggregate component has a type with predicates, an explicit
1614 -- predicate check must be applied, as for an assignment statement,
1615 -- because the aggegate might not be expanded into individual
1616 -- component assignments. If the expression covers several components
1617 -- the analysis and the predicate check take place later.
1619 if Present (Predicate_Function (Component_Typ))
1620 and then Analyzed (Expr)
1621 then
1622 Apply_Predicate_Check (Expr, Component_Typ);
1623 end if;
1625 if Raises_Constraint_Error (Expr)
1626 and then Nkind (Parent (Expr)) /= N_Component_Association
1627 then
1628 Set_Raises_Constraint_Error (N);
1629 end if;
1631 -- If the expression has been marked as requiring a range check,
1632 -- then generate it here. It's a bit odd to be generating such
1633 -- checks in the analyzer, but harmless since Generate_Range_Check
1634 -- does nothing (other than making sure Do_Range_Check is set) if
1635 -- the expander is not active.
1637 if Do_Range_Check (Expr) then
1638 Generate_Range_Check (Expr, Component_Typ, CE_Range_Check_Failed);
1639 end if;
1641 return Resolution_OK;
1642 end Resolve_Aggr_Expr;
1644 --------------------------------------------
1645 -- Resolve_Iterated_Component_Association --
1646 --------------------------------------------
1648 procedure Resolve_Iterated_Component_Association
1649 (N : Node_Id;
1650 Index_Typ : Entity_Id)
1652 Id : constant Entity_Id := Defining_Identifier (N);
1653 Loc : constant Source_Ptr := Sloc (N);
1655 Choice : Node_Id;
1656 Dummy : Boolean;
1657 Ent : Entity_Id;
1659 begin
1660 Choice := First (Discrete_Choices (N));
1662 while Present (Choice) loop
1663 if Nkind (Choice) = N_Others_Choice then
1664 Others_Present := True;
1666 else
1667 Analyze (Choice);
1669 -- Choice can be a subtype name, a range, or an expression
1671 if Is_Entity_Name (Choice)
1672 and then Is_Type (Entity (Choice))
1673 and then Base_Type (Entity (Choice)) = Base_Type (Index_Typ)
1674 then
1675 null;
1677 else
1678 Analyze_And_Resolve (Choice, Index_Typ);
1679 end if;
1680 end if;
1682 Next (Choice);
1683 end loop;
1685 -- Create a scope in which to introduce an index, which is usually
1686 -- visible in the expression for the component, and needed for its
1687 -- analysis.
1689 Ent := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L');
1690 Set_Etype (Ent, Standard_Void_Type);
1691 Set_Parent (Ent, Parent (N));
1693 -- Decorate the index variable in the current scope. The association
1694 -- may have several choices, each one leading to a loop, so we create
1695 -- this variable only once to prevent homonyms in this scope.
1696 -- The expression has to be analyzed once the index variable is
1697 -- directly visible.
1699 if No (Scope (Id)) then
1700 Enter_Name (Id);
1701 Set_Etype (Id, Index_Typ);
1702 Set_Ekind (Id, E_Variable);
1703 Set_Scope (Id, Ent);
1704 end if;
1706 Push_Scope (Ent);
1707 Dummy := Resolve_Aggr_Expr (Expression (N), False);
1708 End_Scope;
1709 end Resolve_Iterated_Component_Association;
1711 -- Local variables
1713 Assoc : Node_Id;
1714 Choice : Node_Id;
1715 Expr : Node_Id;
1716 Discard : Node_Id;
1718 Aggr_Low : Node_Id := Empty;
1719 Aggr_High : Node_Id := Empty;
1720 -- The actual low and high bounds of this sub-aggregate
1722 Case_Table_Size : Nat;
1723 -- Contains the size of the case table needed to sort aggregate choices
1725 Choices_Low : Node_Id := Empty;
1726 Choices_High : Node_Id := Empty;
1727 -- The lowest and highest discrete choices values for a named aggregate
1729 Delete_Choice : Boolean;
1730 -- Used when replacing a subtype choice with predicate by a list
1732 Nb_Elements : Uint := Uint_0;
1733 -- The number of elements in a positional aggregate
1735 Nb_Discrete_Choices : Nat := 0;
1736 -- The overall number of discrete choices (not counting others choice)
1738 -- Start of processing for Resolve_Array_Aggregate
1740 begin
1741 -- Ignore junk empty aggregate resulting from parser error
1743 if No (Expressions (N))
1744 and then No (Component_Associations (N))
1745 and then not Null_Record_Present (N)
1746 then
1747 return False;
1748 end if;
1750 -- STEP 1: make sure the aggregate is correctly formatted
1752 if Present (Component_Associations (N)) then
1753 Assoc := First (Component_Associations (N));
1754 while Present (Assoc) loop
1755 if Nkind (Assoc) = N_Iterated_Component_Association then
1756 Resolve_Iterated_Component_Association (Assoc, Index_Typ);
1757 end if;
1759 Choice := First (Choice_List (Assoc));
1760 Delete_Choice := False;
1761 while Present (Choice) loop
1762 if Nkind (Choice) = N_Others_Choice then
1763 Others_Present := True;
1765 if Choice /= First (Choice_List (Assoc))
1766 or else Present (Next (Choice))
1767 then
1768 Error_Msg_N
1769 ("OTHERS must appear alone in a choice list", Choice);
1770 return Failure;
1771 end if;
1773 if Present (Next (Assoc)) then
1774 Error_Msg_N
1775 ("OTHERS must appear last in an aggregate", Choice);
1776 return Failure;
1777 end if;
1779 if Ada_Version = Ada_83
1780 and then Assoc /= First (Component_Associations (N))
1781 and then Nkind_In (Parent (N), N_Assignment_Statement,
1782 N_Object_Declaration)
1783 then
1784 Error_Msg_N
1785 ("(Ada 83) illegal context for OTHERS choice", N);
1786 end if;
1788 elsif Is_Entity_Name (Choice) then
1789 Analyze (Choice);
1791 declare
1792 E : constant Entity_Id := Entity (Choice);
1793 New_Cs : List_Id;
1794 P : Node_Id;
1795 C : Node_Id;
1797 begin
1798 if Is_Type (E) and then Has_Predicates (E) then
1799 Freeze_Before (N, E);
1801 if Has_Dynamic_Predicate_Aspect (E) then
1802 Error_Msg_NE
1803 ("subtype& has dynamic predicate, not allowed "
1804 & "in aggregate choice", Choice, E);
1806 elsif not Is_OK_Static_Subtype (E) then
1807 Error_Msg_NE
1808 ("non-static subtype& has predicate, not allowed "
1809 & "in aggregate choice", Choice, E);
1810 end if;
1812 -- If the subtype has a static predicate, replace the
1813 -- original choice with the list of individual values
1814 -- covered by the predicate. Do not perform this
1815 -- transformation if we need to preserve the source
1816 -- for ASIS use.
1817 -- This should be deferred to expansion time ???
1819 if Present (Static_Discrete_Predicate (E))
1820 and then not ASIS_Mode
1821 then
1822 Delete_Choice := True;
1824 New_Cs := New_List;
1825 P := First (Static_Discrete_Predicate (E));
1826 while Present (P) loop
1827 C := New_Copy (P);
1828 Set_Sloc (C, Sloc (Choice));
1829 Append_To (New_Cs, C);
1830 Next (P);
1831 end loop;
1833 Insert_List_After (Choice, New_Cs);
1834 end if;
1835 end if;
1836 end;
1837 end if;
1839 Nb_Choices := Nb_Choices + 1;
1841 declare
1842 C : constant Node_Id := Choice;
1844 begin
1845 Next (Choice);
1847 if Delete_Choice then
1848 Remove (C);
1849 Nb_Choices := Nb_Choices - 1;
1850 Delete_Choice := False;
1851 end if;
1852 end;
1853 end loop;
1855 Next (Assoc);
1856 end loop;
1857 end if;
1859 -- At this point we know that the others choice, if present, is by
1860 -- itself and appears last in the aggregate. Check if we have mixed
1861 -- positional and discrete associations (other than the others choice).
1863 if Present (Expressions (N))
1864 and then (Nb_Choices > 1
1865 or else (Nb_Choices = 1 and then not Others_Present))
1866 then
1867 Error_Msg_N
1868 ("named association cannot follow positional association",
1869 First (Choice_List (First (Component_Associations (N)))));
1870 return Failure;
1871 end if;
1873 -- Test for the validity of an others choice if present
1875 if Others_Present and then not Others_Allowed then
1876 Error_Msg_N
1877 ("OTHERS choice not allowed here",
1878 First (Choices (First (Component_Associations (N)))));
1879 return Failure;
1880 end if;
1882 -- Protect against cascaded errors
1884 if Etype (Index_Typ) = Any_Type then
1885 return Failure;
1886 end if;
1888 -- STEP 2: Process named components
1890 if No (Expressions (N)) then
1891 if Others_Present then
1892 Case_Table_Size := Nb_Choices - 1;
1893 else
1894 Case_Table_Size := Nb_Choices;
1895 end if;
1897 Step_2 : declare
1898 function Empty_Range (A : Node_Id) return Boolean;
1899 -- If an association covers an empty range, some warnings on the
1900 -- expression of the association can be disabled.
1902 -----------------
1903 -- Empty_Range --
1904 -----------------
1906 function Empty_Range (A : Node_Id) return Boolean is
1907 R : constant Node_Id := First (Choices (A));
1908 begin
1909 return No (Next (R))
1910 and then Nkind (R) = N_Range
1911 and then Compile_Time_Compare
1912 (Low_Bound (R), High_Bound (R), False) = GT;
1913 end Empty_Range;
1915 -- Local variables
1917 Low : Node_Id;
1918 High : Node_Id;
1919 -- Denote the lowest and highest values in an aggregate choice
1921 S_Low : Node_Id := Empty;
1922 S_High : Node_Id := Empty;
1923 -- if a choice in an aggregate is a subtype indication these
1924 -- denote the lowest and highest values of the subtype
1926 Table : Case_Table_Type (0 .. Case_Table_Size);
1927 -- Used to sort all the different choice values. Entry zero is
1928 -- reserved for sorting purposes.
1930 Single_Choice : Boolean;
1931 -- Set to true every time there is a single discrete choice in a
1932 -- discrete association
1934 Prev_Nb_Discrete_Choices : Nat;
1935 -- Used to keep track of the number of discrete choices in the
1936 -- current association.
1938 Errors_Posted_On_Choices : Boolean := False;
1939 -- Keeps track of whether any choices have semantic errors
1941 -- Start of processing for Step_2
1943 begin
1944 -- STEP 2 (A): Check discrete choices validity
1946 Assoc := First (Component_Associations (N));
1947 while Present (Assoc) loop
1948 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
1949 Choice := First (Choice_List (Assoc));
1951 loop
1952 Analyze (Choice);
1954 if Nkind (Choice) = N_Others_Choice then
1955 Single_Choice := False;
1956 exit;
1958 -- Test for subtype mark without constraint
1960 elsif Is_Entity_Name (Choice) and then
1961 Is_Type (Entity (Choice))
1962 then
1963 if Base_Type (Entity (Choice)) /= Index_Base then
1964 Error_Msg_N
1965 ("invalid subtype mark in aggregate choice",
1966 Choice);
1967 return Failure;
1968 end if;
1970 -- Case of subtype indication
1972 elsif Nkind (Choice) = N_Subtype_Indication then
1973 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
1975 if Has_Dynamic_Predicate_Aspect
1976 (Entity (Subtype_Mark (Choice)))
1977 then
1978 Error_Msg_NE
1979 ("subtype& has dynamic predicate, "
1980 & "not allowed in aggregate choice",
1981 Choice, Entity (Subtype_Mark (Choice)));
1982 end if;
1984 -- Does the subtype indication evaluation raise CE?
1986 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
1987 Get_Index_Bounds (Choice, Low, High);
1988 Check_Bounds (S_Low, S_High, Low, High);
1990 -- Case of range or expression
1992 else
1993 Resolve (Choice, Index_Base);
1994 Check_Unset_Reference (Choice);
1995 Check_Non_Static_Context (Choice);
1997 -- If semantic errors were posted on the choice, then
1998 -- record that for possible early return from later
1999 -- processing (see handling of enumeration choices).
2001 if Error_Posted (Choice) then
2002 Errors_Posted_On_Choices := True;
2003 end if;
2005 -- Do not range check a choice. This check is redundant
2006 -- since this test is already done when we check that the
2007 -- bounds of the array aggregate are within range.
2009 Set_Do_Range_Check (Choice, False);
2011 -- In SPARK, the choice must be static
2013 if not (Is_OK_Static_Expression (Choice)
2014 or else (Nkind (Choice) = N_Range
2015 and then Is_OK_Static_Range (Choice)))
2016 then
2017 Check_SPARK_05_Restriction
2018 ("choice should be static", Choice);
2019 end if;
2020 end if;
2022 -- If we could not resolve the discrete choice stop here
2024 if Etype (Choice) = Any_Type then
2025 return Failure;
2027 -- If the discrete choice raises CE get its original bounds
2029 elsif Nkind (Choice) = N_Raise_Constraint_Error then
2030 Set_Raises_Constraint_Error (N);
2031 Get_Index_Bounds (Original_Node (Choice), Low, High);
2033 -- Otherwise get its bounds as usual
2035 else
2036 Get_Index_Bounds (Choice, Low, High);
2037 end if;
2039 if (Dynamic_Or_Null_Range (Low, High)
2040 or else (Nkind (Choice) = N_Subtype_Indication
2041 and then
2042 Dynamic_Or_Null_Range (S_Low, S_High)))
2043 and then Nb_Choices /= 1
2044 then
2045 Error_Msg_N
2046 ("dynamic or empty choice in aggregate "
2047 & "must be the only choice", Choice);
2048 return Failure;
2049 end if;
2051 if not (All_Composite_Constraints_Static (Low)
2052 and then All_Composite_Constraints_Static (High)
2053 and then All_Composite_Constraints_Static (S_Low)
2054 and then All_Composite_Constraints_Static (S_High))
2055 then
2056 Check_Restriction (No_Dynamic_Sized_Objects, Choice);
2057 end if;
2059 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
2060 Table (Nb_Discrete_Choices).Lo := Low;
2061 Table (Nb_Discrete_Choices).Hi := High;
2062 Table (Nb_Discrete_Choices).Choice := Choice;
2064 Next (Choice);
2066 if No (Choice) then
2068 -- Check if we have a single discrete choice and whether
2069 -- this discrete choice specifies a single value.
2071 Single_Choice :=
2072 (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1)
2073 and then (Low = High);
2075 exit;
2076 end if;
2077 end loop;
2079 -- Ada 2005 (AI-231)
2081 if Ada_Version >= Ada_2005
2082 and then Known_Null (Expression (Assoc))
2083 and then not Empty_Range (Assoc)
2084 then
2085 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2086 end if;
2088 -- Ada 2005 (AI-287): In case of default initialized component
2089 -- we delay the resolution to the expansion phase.
2091 if Box_Present (Assoc) then
2093 -- Ada 2005 (AI-287): In case of default initialization of a
2094 -- component the expander will generate calls to the
2095 -- corresponding initialization subprogram. We need to call
2096 -- Resolve_Aggr_Expr to check the rules about
2097 -- dimensionality.
2099 if not Resolve_Aggr_Expr
2100 (Assoc, Single_Elmt => Single_Choice)
2101 then
2102 return Failure;
2103 end if;
2105 elsif Nkind (Assoc) = N_Iterated_Component_Association then
2106 null; -- handled above, in a loop context.
2108 elsif not Resolve_Aggr_Expr
2109 (Expression (Assoc), Single_Elmt => Single_Choice)
2110 then
2111 return Failure;
2113 -- Check incorrect use of dynamically tagged expression
2115 -- We differentiate here two cases because the expression may
2116 -- not be decorated. For example, the analysis and resolution
2117 -- of the expression associated with the others choice will be
2118 -- done later with the full aggregate. In such case we
2119 -- duplicate the expression tree to analyze the copy and
2120 -- perform the required check.
2122 elsif not Present (Etype (Expression (Assoc))) then
2123 declare
2124 Save_Analysis : constant Boolean := Full_Analysis;
2125 Expr : constant Node_Id :=
2126 New_Copy_Tree (Expression (Assoc));
2128 begin
2129 Expander_Mode_Save_And_Set (False);
2130 Full_Analysis := False;
2132 -- Analyze the expression, making sure it is properly
2133 -- attached to the tree before we do the analysis.
2135 Set_Parent (Expr, Parent (Expression (Assoc)));
2136 Analyze (Expr);
2138 -- Compute its dimensions now, rather than at the end of
2139 -- resolution, because in the case of multidimensional
2140 -- aggregates subsequent expansion may lead to spurious
2141 -- errors.
2143 Check_Expression_Dimensions (Expr, Component_Typ);
2145 -- If the expression is a literal, propagate this info
2146 -- to the expression in the association, to enable some
2147 -- optimizations downstream.
2149 if Is_Entity_Name (Expr)
2150 and then Present (Entity (Expr))
2151 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
2152 then
2153 Analyze_And_Resolve
2154 (Expression (Assoc), Component_Typ);
2155 end if;
2157 Full_Analysis := Save_Analysis;
2158 Expander_Mode_Restore;
2160 if Is_Tagged_Type (Etype (Expr)) then
2161 Check_Dynamically_Tagged_Expression
2162 (Expr => Expr,
2163 Typ => Component_Type (Etype (N)),
2164 Related_Nod => N);
2165 end if;
2166 end;
2168 elsif Is_Tagged_Type (Etype (Expression (Assoc))) then
2169 Check_Dynamically_Tagged_Expression
2170 (Expr => Expression (Assoc),
2171 Typ => Component_Type (Etype (N)),
2172 Related_Nod => N);
2173 end if;
2175 Next (Assoc);
2176 end loop;
2178 -- If aggregate contains more than one choice then these must be
2179 -- static. Check for duplicate and missing values.
2181 -- Note: there is duplicated code here wrt Check_Choice_Set in
2182 -- the body of Sem_Case, and it is possible we could just reuse
2183 -- that procedure. To be checked ???
2185 if Nb_Discrete_Choices > 1 then
2186 Check_Choices : declare
2187 Choice : Node_Id;
2188 -- Location of choice for messages
2190 Hi_Val : Uint;
2191 Lo_Val : Uint;
2192 -- High end of one range and Low end of the next. Should be
2193 -- contiguous if there is no hole in the list of values.
2195 Lo_Dup : Uint;
2196 Hi_Dup : Uint;
2197 -- End points of duplicated range
2199 Missing_Or_Duplicates : Boolean := False;
2200 -- Set True if missing or duplicate choices found
2202 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id);
2203 -- Output continuation message with a representation of the
2204 -- bounds (just Lo if Lo = Hi, else Lo .. Hi). C is the
2205 -- choice node where the message is to be posted.
2207 ------------------------
2208 -- Output_Bad_Choices --
2209 ------------------------
2211 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id) is
2212 begin
2213 -- Enumeration type case
2215 if Is_Enumeration_Type (Index_Typ) then
2216 Error_Msg_Name_1 :=
2217 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Lo, Loc));
2218 Error_Msg_Name_2 :=
2219 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Hi, Loc));
2221 if Lo = Hi then
2222 Error_Msg_N ("\\ %!", C);
2223 else
2224 Error_Msg_N ("\\ % .. %!", C);
2225 end if;
2227 -- Integer types case
2229 else
2230 Error_Msg_Uint_1 := Lo;
2231 Error_Msg_Uint_2 := Hi;
2233 if Lo = Hi then
2234 Error_Msg_N ("\\ ^!", C);
2235 else
2236 Error_Msg_N ("\\ ^ .. ^!", C);
2237 end if;
2238 end if;
2239 end Output_Bad_Choices;
2241 -- Start of processing for Check_Choices
2243 begin
2244 Sort_Case_Table (Table);
2246 -- First we do a quick linear loop to find out if we have
2247 -- any duplicates or missing entries (usually we have a
2248 -- legal aggregate, so this will get us out quickly).
2250 for J in 1 .. Nb_Discrete_Choices - 1 loop
2251 Hi_Val := Expr_Value (Table (J).Hi);
2252 Lo_Val := Expr_Value (Table (J + 1).Lo);
2254 if Lo_Val <= Hi_Val
2255 or else (Lo_Val > Hi_Val + 1
2256 and then not Others_Present)
2257 then
2258 Missing_Or_Duplicates := True;
2259 exit;
2260 end if;
2261 end loop;
2263 -- If we have missing or duplicate entries, first fill in
2264 -- the Highest entries to make life easier in the following
2265 -- loops to detect bad entries.
2267 if Missing_Or_Duplicates then
2268 Table (1).Highest := Expr_Value (Table (1).Hi);
2270 for J in 2 .. Nb_Discrete_Choices loop
2271 Table (J).Highest :=
2272 UI_Max
2273 (Table (J - 1).Highest, Expr_Value (Table (J).Hi));
2274 end loop;
2276 -- Loop through table entries to find duplicate indexes
2278 for J in 2 .. Nb_Discrete_Choices loop
2279 Lo_Val := Expr_Value (Table (J).Lo);
2280 Hi_Val := Expr_Value (Table (J).Hi);
2282 -- Case where we have duplicates (the lower bound of
2283 -- this choice is less than or equal to the highest
2284 -- high bound found so far).
2286 if Lo_Val <= Table (J - 1).Highest then
2288 -- We move backwards looking for duplicates. We can
2289 -- abandon this loop as soon as we reach a choice
2290 -- highest value that is less than Lo_Val.
2292 for K in reverse 1 .. J - 1 loop
2293 exit when Table (K).Highest < Lo_Val;
2295 -- Here we may have duplicates between entries
2296 -- for K and J. Get range of duplicates.
2298 Lo_Dup :=
2299 UI_Max (Lo_Val, Expr_Value (Table (K).Lo));
2300 Hi_Dup :=
2301 UI_Min (Hi_Val, Expr_Value (Table (K).Hi));
2303 -- Nothing to do if duplicate range is null
2305 if Lo_Dup > Hi_Dup then
2306 null;
2308 -- Otherwise place proper message. Because
2309 -- of the missing expansion of subtypes with
2310 -- predicates in ASIS mode, do not report
2311 -- spurious overlap errors.
2313 elsif ASIS_Mode
2314 and then
2315 ((Is_Type (Entity (Table (J).Choice))
2316 and then Has_Predicates
2317 (Entity (Table (J).Choice)))
2318 or else
2319 (Is_Type (Entity (Table (K).Choice))
2320 and then Has_Predicates
2321 (Entity (Table (K).Choice))))
2322 then
2323 null;
2325 else
2326 -- We place message on later choice, with a
2327 -- line reference to the earlier choice.
2329 if Sloc (Table (J).Choice) <
2330 Sloc (Table (K).Choice)
2331 then
2332 Choice := Table (K).Choice;
2333 Error_Msg_Sloc := Sloc (Table (J).Choice);
2334 else
2335 Choice := Table (J).Choice;
2336 Error_Msg_Sloc := Sloc (Table (K).Choice);
2337 end if;
2339 if Lo_Dup = Hi_Dup then
2340 Error_Msg_N
2341 ("index value in array aggregate "
2342 & "duplicates the one given#!", Choice);
2343 else
2344 Error_Msg_N
2345 ("index values in array aggregate "
2346 & "duplicate those given#!", Choice);
2347 end if;
2349 Output_Bad_Choices (Lo_Dup, Hi_Dup, Choice);
2350 end if;
2351 end loop;
2352 end if;
2353 end loop;
2355 -- Loop through entries in table to find missing indexes.
2356 -- Not needed if others, since missing impossible.
2358 if not Others_Present then
2359 for J in 2 .. Nb_Discrete_Choices loop
2360 Lo_Val := Expr_Value (Table (J).Lo);
2361 Hi_Val := Table (J - 1).Highest;
2363 if Lo_Val > Hi_Val + 1 then
2365 declare
2366 Error_Node : Node_Id;
2368 begin
2369 -- If the choice is the bound of a range in
2370 -- a subtype indication, it is not in the
2371 -- source lists for the aggregate itself, so
2372 -- post the error on the aggregate. Otherwise
2373 -- post it on choice itself.
2375 Choice := Table (J).Choice;
2377 if Is_List_Member (Choice) then
2378 Error_Node := Choice;
2379 else
2380 Error_Node := N;
2381 end if;
2383 if Hi_Val + 1 = Lo_Val - 1 then
2384 Error_Msg_N
2385 ("missing index value "
2386 & "in array aggregate!", Error_Node);
2387 else
2388 Error_Msg_N
2389 ("missing index values "
2390 & "in array aggregate!", Error_Node);
2391 end if;
2393 Output_Bad_Choices
2394 (Hi_Val + 1, Lo_Val - 1, Error_Node);
2395 end;
2396 end if;
2397 end loop;
2398 end if;
2400 -- If either missing or duplicate values, return failure
2402 Set_Etype (N, Any_Composite);
2403 return Failure;
2404 end if;
2405 end Check_Choices;
2406 end if;
2408 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
2410 if Nb_Discrete_Choices > 0 then
2411 Choices_Low := Table (1).Lo;
2412 Choices_High := Table (Nb_Discrete_Choices).Hi;
2413 end if;
2415 -- If Others is present, then bounds of aggregate come from the
2416 -- index constraint (not the choices in the aggregate itself).
2418 if Others_Present then
2419 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2421 -- Abandon processing if either bound is already signalled as
2422 -- an error (prevents junk cascaded messages and blow ups).
2424 if Nkind (Aggr_Low) = N_Error
2425 or else
2426 Nkind (Aggr_High) = N_Error
2427 then
2428 return False;
2429 end if;
2431 -- No others clause present
2433 else
2434 -- Special processing if others allowed and not present. This
2435 -- means that the bounds of the aggregate come from the index
2436 -- constraint (and the length must match).
2438 if Others_Allowed then
2439 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2441 -- Abandon processing if either bound is already signalled
2442 -- as an error (stop junk cascaded messages and blow ups).
2444 if Nkind (Aggr_Low) = N_Error
2445 or else
2446 Nkind (Aggr_High) = N_Error
2447 then
2448 return False;
2449 end if;
2451 -- If others allowed, and no others present, then the array
2452 -- should cover all index values. If it does not, we will
2453 -- get a length check warning, but there is two cases where
2454 -- an additional warning is useful:
2456 -- If we have no positional components, and the length is
2457 -- wrong (which we can tell by others being allowed with
2458 -- missing components), and the index type is an enumeration
2459 -- type, then issue appropriate warnings about these missing
2460 -- components. They are only warnings, since the aggregate
2461 -- is fine, it's just the wrong length. We skip this check
2462 -- for standard character types (since there are no literals
2463 -- and it is too much trouble to concoct them), and also if
2464 -- any of the bounds have values that are not known at
2465 -- compile time.
2467 -- Another case warranting a warning is when the length
2468 -- is right, but as above we have an index type that is
2469 -- an enumeration, and the bounds do not match. This is a
2470 -- case where dubious sliding is allowed and we generate a
2471 -- warning that the bounds do not match.
2473 if No (Expressions (N))
2474 and then Nkind (Index) = N_Range
2475 and then Is_Enumeration_Type (Etype (Index))
2476 and then not Is_Standard_Character_Type (Etype (Index))
2477 and then Compile_Time_Known_Value (Aggr_Low)
2478 and then Compile_Time_Known_Value (Aggr_High)
2479 and then Compile_Time_Known_Value (Choices_Low)
2480 and then Compile_Time_Known_Value (Choices_High)
2481 then
2482 -- If any of the expressions or range bounds in choices
2483 -- have semantic errors, then do not attempt further
2484 -- resolution, to prevent cascaded errors.
2486 if Errors_Posted_On_Choices then
2487 return Failure;
2488 end if;
2490 declare
2491 ALo : constant Node_Id := Expr_Value_E (Aggr_Low);
2492 AHi : constant Node_Id := Expr_Value_E (Aggr_High);
2493 CLo : constant Node_Id := Expr_Value_E (Choices_Low);
2494 CHi : constant Node_Id := Expr_Value_E (Choices_High);
2496 Ent : Entity_Id;
2498 begin
2499 -- Warning case 1, missing values at start/end. Only
2500 -- do the check if the number of entries is too small.
2502 if (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2504 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2505 then
2506 Error_Msg_N
2507 ("missing index value(s) in array aggregate??",
2510 -- Output missing value(s) at start
2512 if Chars (ALo) /= Chars (CLo) then
2513 Ent := Prev (CLo);
2515 if Chars (ALo) = Chars (Ent) then
2516 Error_Msg_Name_1 := Chars (ALo);
2517 Error_Msg_N ("\ %??", N);
2518 else
2519 Error_Msg_Name_1 := Chars (ALo);
2520 Error_Msg_Name_2 := Chars (Ent);
2521 Error_Msg_N ("\ % .. %??", N);
2522 end if;
2523 end if;
2525 -- Output missing value(s) at end
2527 if Chars (AHi) /= Chars (CHi) then
2528 Ent := Next (CHi);
2530 if Chars (AHi) = Chars (Ent) then
2531 Error_Msg_Name_1 := Chars (Ent);
2532 Error_Msg_N ("\ %??", N);
2533 else
2534 Error_Msg_Name_1 := Chars (Ent);
2535 Error_Msg_Name_2 := Chars (AHi);
2536 Error_Msg_N ("\ % .. %??", N);
2537 end if;
2538 end if;
2540 -- Warning case 2, dubious sliding. The First_Subtype
2541 -- test distinguishes between a constrained type where
2542 -- sliding is not allowed (so we will get a warning
2543 -- later that Constraint_Error will be raised), and
2544 -- the unconstrained case where sliding is permitted.
2546 elsif (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2548 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2549 and then Chars (ALo) /= Chars (CLo)
2550 and then
2551 not Is_Constrained (First_Subtype (Etype (N)))
2552 then
2553 Error_Msg_N
2554 ("bounds of aggregate do not match target??", N);
2555 end if;
2556 end;
2557 end if;
2558 end if;
2560 -- If no others, aggregate bounds come from aggregate
2562 Aggr_Low := Choices_Low;
2563 Aggr_High := Choices_High;
2564 end if;
2565 end Step_2;
2567 -- STEP 3: Process positional components
2569 else
2570 -- STEP 3 (A): Process positional elements
2572 Expr := First (Expressions (N));
2573 Nb_Elements := Uint_0;
2574 while Present (Expr) loop
2575 Nb_Elements := Nb_Elements + 1;
2577 -- Ada 2005 (AI-231)
2579 if Ada_Version >= Ada_2005 and then Known_Null (Expr) then
2580 Check_Can_Never_Be_Null (Etype (N), Expr);
2581 end if;
2583 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
2584 return Failure;
2585 end if;
2587 -- Check incorrect use of dynamically tagged expression
2589 if Is_Tagged_Type (Etype (Expr)) then
2590 Check_Dynamically_Tagged_Expression
2591 (Expr => Expr,
2592 Typ => Component_Type (Etype (N)),
2593 Related_Nod => N);
2594 end if;
2596 Next (Expr);
2597 end loop;
2599 if Others_Present then
2600 Assoc := Last (Component_Associations (N));
2602 -- Ada 2005 (AI-231)
2604 if Ada_Version >= Ada_2005 and then Known_Null (Assoc) then
2605 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2606 end if;
2608 -- Ada 2005 (AI-287): In case of default initialized component,
2609 -- we delay the resolution to the expansion phase.
2611 if Box_Present (Assoc) then
2613 -- Ada 2005 (AI-287): In case of default initialization of a
2614 -- component the expander will generate calls to the
2615 -- corresponding initialization subprogram. We need to call
2616 -- Resolve_Aggr_Expr to check the rules about
2617 -- dimensionality.
2619 if not Resolve_Aggr_Expr (Assoc, Single_Elmt => False) then
2620 return Failure;
2621 end if;
2623 elsif not Resolve_Aggr_Expr (Expression (Assoc),
2624 Single_Elmt => False)
2625 then
2626 return Failure;
2628 -- Check incorrect use of dynamically tagged expression. The
2629 -- expression of the others choice has not been resolved yet.
2630 -- In order to diagnose the semantic error we create a duplicate
2631 -- tree to analyze it and perform the check.
2633 else
2634 declare
2635 Save_Analysis : constant Boolean := Full_Analysis;
2636 Expr : constant Node_Id :=
2637 New_Copy_Tree (Expression (Assoc));
2639 begin
2640 Expander_Mode_Save_And_Set (False);
2641 Full_Analysis := False;
2642 Analyze (Expr);
2643 Full_Analysis := Save_Analysis;
2644 Expander_Mode_Restore;
2646 if Is_Tagged_Type (Etype (Expr)) then
2647 Check_Dynamically_Tagged_Expression
2648 (Expr => Expr,
2649 Typ => Component_Type (Etype (N)),
2650 Related_Nod => N);
2651 end if;
2652 end;
2653 end if;
2654 end if;
2656 -- STEP 3 (B): Compute the aggregate bounds
2658 if Others_Present then
2659 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2661 else
2662 if Others_Allowed then
2663 Get_Index_Bounds (Index_Constr, Aggr_Low, Discard);
2664 else
2665 Aggr_Low := Index_Typ_Low;
2666 end if;
2668 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
2669 Check_Bound (Index_Base_High, Aggr_High);
2670 end if;
2671 end if;
2673 -- STEP 4: Perform static aggregate checks and save the bounds
2675 -- Check (A)
2677 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
2678 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
2680 -- Check (B)
2682 if Others_Present and then Nb_Discrete_Choices > 0 then
2683 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
2684 Check_Bounds (Index_Typ_Low, Index_Typ_High,
2685 Choices_Low, Choices_High);
2686 Check_Bounds (Index_Base_Low, Index_Base_High,
2687 Choices_Low, Choices_High);
2689 -- Check (C)
2691 elsif Others_Present and then Nb_Elements > 0 then
2692 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
2693 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
2694 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
2695 end if;
2697 if Raises_Constraint_Error (Aggr_Low)
2698 or else Raises_Constraint_Error (Aggr_High)
2699 then
2700 Set_Raises_Constraint_Error (N);
2701 end if;
2703 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
2705 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
2706 -- since the addition node returned by Add is not yet analyzed. Attach
2707 -- to tree and analyze first. Reset analyzed flag to ensure it will get
2708 -- analyzed when it is a literal bound whose type must be properly set.
2710 if Others_Present or else Nb_Discrete_Choices > 0 then
2711 Aggr_High := Duplicate_Subexpr (Aggr_High);
2713 if Etype (Aggr_High) = Universal_Integer then
2714 Set_Analyzed (Aggr_High, False);
2715 end if;
2716 end if;
2718 -- If the aggregate already has bounds attached to it, it means this is
2719 -- a positional aggregate created as an optimization by
2720 -- Exp_Aggr.Convert_To_Positional, so we don't want to change those
2721 -- bounds.
2723 if Present (Aggregate_Bounds (N)) and then not Others_Allowed then
2724 Aggr_Low := Low_Bound (Aggregate_Bounds (N));
2725 Aggr_High := High_Bound (Aggregate_Bounds (N));
2726 end if;
2728 Set_Aggregate_Bounds
2729 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
2731 -- The bounds may contain expressions that must be inserted upwards.
2732 -- Attach them fully to the tree. After analysis, remove side effects
2733 -- from upper bound, if still needed.
2735 Set_Parent (Aggregate_Bounds (N), N);
2736 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
2737 Check_Unset_Reference (Aggregate_Bounds (N));
2739 if not Others_Present and then Nb_Discrete_Choices = 0 then
2740 Set_High_Bound
2741 (Aggregate_Bounds (N),
2742 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
2743 end if;
2745 -- Check the dimensions of each component in the array aggregate
2747 Analyze_Dimension_Array_Aggregate (N, Component_Typ);
2749 return Success;
2750 end Resolve_Array_Aggregate;
2752 -----------------------------
2753 -- Resolve_Delta_Aggregate --
2754 -----------------------------
2756 procedure Resolve_Delta_Aggregate (N : Node_Id; Typ : Entity_Id) is
2757 Base : constant Node_Id := Expression (N);
2758 Deltas : constant List_Id := Component_Associations (N);
2760 function Get_Component_Type (Nam : Node_Id) return Entity_Id;
2762 ------------------------
2763 -- Get_Component_Type --
2764 ------------------------
2766 function Get_Component_Type (Nam : Node_Id) return Entity_Id is
2767 Comp : Entity_Id;
2769 begin
2770 Comp := First_Entity (Typ);
2772 while Present (Comp) loop
2773 if Chars (Comp) = Chars (Nam) then
2774 if Ekind (Comp) = E_Discriminant then
2775 Error_Msg_N ("delta cannot apply to discriminant", Nam);
2776 end if;
2778 return Etype (Comp);
2779 end if;
2781 Comp := Next_Entity (Comp);
2782 end loop;
2784 Error_Msg_NE ("type& has no component with this name", Nam, Typ);
2785 return Any_Type;
2786 end Get_Component_Type;
2788 -- Local variables
2790 Assoc : Node_Id;
2791 Choice : Node_Id;
2792 Comp_Type : Entity_Id;
2793 Index_Type : Entity_Id;
2795 -- Start of processing for Resolve_Delta_Aggregate
2797 begin
2798 if not Is_Composite_Type (Typ) then
2799 Error_Msg_N ("not a composite type", N);
2800 end if;
2802 Analyze_And_Resolve (Base, Typ);
2804 if Is_Array_Type (Typ) then
2805 Index_Type := Etype (First_Index (Typ));
2806 Assoc := First (Deltas);
2807 while Present (Assoc) loop
2808 if Nkind (Assoc) = N_Iterated_Component_Association then
2809 Choice := First (Choice_List (Assoc));
2810 while Present (Choice) loop
2811 if Nkind (Choice) = N_Others_Choice then
2812 Error_Msg_N
2813 ("others not allowed in delta aggregate", Choice);
2815 else
2816 Analyze_And_Resolve (Choice, Index_Type);
2817 end if;
2819 Next (Choice);
2820 end loop;
2822 declare
2823 Id : constant Entity_Id := Defining_Identifier (Assoc);
2824 Ent : constant Entity_Id :=
2825 New_Internal_Entity
2826 (E_Loop, Current_Scope, Sloc (Assoc), 'L');
2828 begin
2829 Set_Etype (Ent, Standard_Void_Type);
2830 Set_Parent (Ent, Assoc);
2832 if No (Scope (Id)) then
2833 Enter_Name (Id);
2834 Set_Etype (Id, Index_Type);
2835 Set_Ekind (Id, E_Variable);
2836 Set_Scope (Id, Ent);
2837 end if;
2839 Push_Scope (Ent);
2840 Analyze_And_Resolve
2841 (New_Copy_Tree (Expression (Assoc)), Component_Type (Typ));
2842 End_Scope;
2843 end;
2845 else
2846 Choice := First (Choice_List (Assoc));
2847 while Present (Choice) loop
2848 if Nkind (Choice) = N_Others_Choice then
2849 Error_Msg_N
2850 ("others not allowed in delta aggregate", Choice);
2852 else
2853 Analyze (Choice);
2854 if Is_Entity_Name (Choice)
2855 and then Is_Type (Entity (Choice))
2856 then
2857 -- Choice covers a range of values.
2858 if Base_Type (Entity (Choice)) /=
2859 Base_Type (Index_Type)
2860 then
2861 Error_Msg_NE
2862 ("choice does mat match index type of",
2863 Choice, Typ);
2864 end if;
2865 else
2866 Resolve (Choice, Index_Type);
2867 end if;
2868 end if;
2870 Next (Choice);
2871 end loop;
2873 Analyze_And_Resolve (Expression (Assoc), Component_Type (Typ));
2874 end if;
2876 Next (Assoc);
2877 end loop;
2879 else
2880 Assoc := First (Deltas);
2881 while Present (Assoc) loop
2882 Choice := First (Choice_List (Assoc));
2883 while Present (Choice) loop
2884 Comp_Type := Get_Component_Type (Choice);
2885 Next (Choice);
2886 end loop;
2888 Analyze_And_Resolve (Expression (Assoc), Comp_Type);
2889 Next (Assoc);
2890 end loop;
2891 end if;
2893 Set_Etype (N, Typ);
2894 end Resolve_Delta_Aggregate;
2896 ---------------------------------
2897 -- Resolve_Extension_Aggregate --
2898 ---------------------------------
2900 -- There are two cases to consider:
2902 -- a) If the ancestor part is a type mark, the components needed are the
2903 -- difference between the components of the expected type and the
2904 -- components of the given type mark.
2906 -- b) If the ancestor part is an expression, it must be unambiguous, and
2907 -- once we have its type we can also compute the needed components as in
2908 -- the previous case. In both cases, if the ancestor type is not the
2909 -- immediate ancestor, we have to build this ancestor recursively.
2911 -- In both cases, discriminants of the ancestor type do not play a role in
2912 -- the resolution of the needed components, because inherited discriminants
2913 -- cannot be used in a type extension. As a result we can compute
2914 -- independently the list of components of the ancestor type and of the
2915 -- expected type.
2917 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
2918 A : constant Node_Id := Ancestor_Part (N);
2919 A_Type : Entity_Id;
2920 I : Interp_Index;
2921 It : Interp;
2923 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean;
2924 -- If the type is limited, verify that the ancestor part is a legal
2925 -- expression (aggregate or function call, including 'Input)) that does
2926 -- not require a copy, as specified in 7.5(2).
2928 function Valid_Ancestor_Type return Boolean;
2929 -- Verify that the type of the ancestor part is a non-private ancestor
2930 -- of the expected type, which must be a type extension.
2932 ----------------------------
2933 -- Valid_Limited_Ancestor --
2934 ----------------------------
2936 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean is
2937 begin
2938 if Is_Entity_Name (Anc) and then Is_Type (Entity (Anc)) then
2939 return True;
2941 -- The ancestor must be a call or an aggregate, but a call may
2942 -- have been expanded into a temporary, so check original node.
2944 elsif Nkind_In (Anc, N_Aggregate,
2945 N_Extension_Aggregate,
2946 N_Function_Call)
2947 then
2948 return True;
2950 elsif Nkind (Original_Node (Anc)) = N_Function_Call then
2951 return True;
2953 elsif Nkind (Anc) = N_Attribute_Reference
2954 and then Attribute_Name (Anc) = Name_Input
2955 then
2956 return True;
2958 elsif Nkind (Anc) = N_Qualified_Expression then
2959 return Valid_Limited_Ancestor (Expression (Anc));
2961 else
2962 return False;
2963 end if;
2964 end Valid_Limited_Ancestor;
2966 -------------------------
2967 -- Valid_Ancestor_Type --
2968 -------------------------
2970 function Valid_Ancestor_Type return Boolean is
2971 Imm_Type : Entity_Id;
2973 begin
2974 Imm_Type := Base_Type (Typ);
2975 while Is_Derived_Type (Imm_Type) loop
2976 if Etype (Imm_Type) = Base_Type (A_Type) then
2977 return True;
2979 -- The base type of the parent type may appear as a private
2980 -- extension if it is declared as such in a parent unit of the
2981 -- current one. For consistency of the subsequent analysis use
2982 -- the partial view for the ancestor part.
2984 elsif Is_Private_Type (Etype (Imm_Type))
2985 and then Present (Full_View (Etype (Imm_Type)))
2986 and then Base_Type (A_Type) = Full_View (Etype (Imm_Type))
2987 then
2988 A_Type := Etype (Imm_Type);
2989 return True;
2991 -- The parent type may be a private extension. The aggregate is
2992 -- legal if the type of the aggregate is an extension of it that
2993 -- is not a private extension.
2995 elsif Is_Private_Type (A_Type)
2996 and then not Is_Private_Type (Imm_Type)
2997 and then Present (Full_View (A_Type))
2998 and then Base_Type (Full_View (A_Type)) = Etype (Imm_Type)
2999 then
3000 return True;
3002 else
3003 Imm_Type := Etype (Base_Type (Imm_Type));
3004 end if;
3005 end loop;
3007 -- If previous loop did not find a proper ancestor, report error
3009 Error_Msg_NE ("expect ancestor type of &", A, Typ);
3010 return False;
3011 end Valid_Ancestor_Type;
3013 -- Start of processing for Resolve_Extension_Aggregate
3015 begin
3016 -- Analyze the ancestor part and account for the case where it is a
3017 -- parameterless function call.
3019 Analyze (A);
3020 Check_Parameterless_Call (A);
3022 -- In SPARK, the ancestor part cannot be a type mark
3024 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
3025 Check_SPARK_05_Restriction ("ancestor part cannot be a type mark", A);
3027 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
3028 -- must not have unknown discriminants.
3030 if Has_Unknown_Discriminants (Root_Type (Typ)) then
3031 Error_Msg_NE
3032 ("aggregate not available for type& whose ancestor "
3033 & "has unknown discriminants", N, Typ);
3034 end if;
3035 end if;
3037 if not Is_Tagged_Type (Typ) then
3038 Error_Msg_N ("type of extension aggregate must be tagged", N);
3039 return;
3041 elsif Is_Limited_Type (Typ) then
3043 -- Ada 2005 (AI-287): Limited aggregates are allowed
3045 if Ada_Version < Ada_2005 then
3046 Error_Msg_N ("aggregate type cannot be limited", N);
3047 Explain_Limited_Type (Typ, N);
3048 return;
3050 elsif Valid_Limited_Ancestor (A) then
3051 null;
3053 else
3054 Error_Msg_N
3055 ("limited ancestor part must be aggregate or function call", A);
3056 end if;
3058 elsif Is_Class_Wide_Type (Typ) then
3059 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
3060 return;
3061 end if;
3063 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
3064 A_Type := Get_Full_View (Entity (A));
3066 if Valid_Ancestor_Type then
3067 Set_Entity (A, A_Type);
3068 Set_Etype (A, A_Type);
3070 Validate_Ancestor_Part (N);
3071 Resolve_Record_Aggregate (N, Typ);
3072 end if;
3074 elsif Nkind (A) /= N_Aggregate then
3075 if Is_Overloaded (A) then
3076 A_Type := Any_Type;
3078 Get_First_Interp (A, I, It);
3079 while Present (It.Typ) loop
3081 -- Only consider limited interpretations in the Ada 2005 case
3083 if Is_Tagged_Type (It.Typ)
3084 and then (Ada_Version >= Ada_2005
3085 or else not Is_Limited_Type (It.Typ))
3086 then
3087 if A_Type /= Any_Type then
3088 Error_Msg_N ("cannot resolve expression", A);
3089 return;
3090 else
3091 A_Type := It.Typ;
3092 end if;
3093 end if;
3095 Get_Next_Interp (I, It);
3096 end loop;
3098 if A_Type = Any_Type then
3099 if Ada_Version >= Ada_2005 then
3100 Error_Msg_N
3101 ("ancestor part must be of a tagged type", A);
3102 else
3103 Error_Msg_N
3104 ("ancestor part must be of a nonlimited tagged type", A);
3105 end if;
3107 return;
3108 end if;
3110 else
3111 A_Type := Etype (A);
3112 end if;
3114 if Valid_Ancestor_Type then
3115 Resolve (A, A_Type);
3116 Check_Unset_Reference (A);
3117 Check_Non_Static_Context (A);
3119 -- The aggregate is illegal if the ancestor expression is a call
3120 -- to a function with a limited unconstrained result, unless the
3121 -- type of the aggregate is a null extension. This restriction
3122 -- was added in AI05-67 to simplify implementation.
3124 if Nkind (A) = N_Function_Call
3125 and then Is_Limited_Type (A_Type)
3126 and then not Is_Null_Extension (Typ)
3127 and then not Is_Constrained (A_Type)
3128 then
3129 Error_Msg_N
3130 ("type of limited ancestor part must be constrained", A);
3132 -- Reject the use of CPP constructors that leave objects partially
3133 -- initialized. For example:
3135 -- type CPP_Root is tagged limited record ...
3136 -- pragma Import (CPP, CPP_Root);
3138 -- type CPP_DT is new CPP_Root and Iface ...
3139 -- pragma Import (CPP, CPP_DT);
3141 -- type Ada_DT is new CPP_DT with ...
3143 -- Obj : Ada_DT := Ada_DT'(New_CPP_Root with others => <>);
3145 -- Using the constructor of CPP_Root the slots of the dispatch
3146 -- table of CPP_DT cannot be set, and the secondary tag of
3147 -- CPP_DT is unknown.
3149 elsif Nkind (A) = N_Function_Call
3150 and then Is_CPP_Constructor_Call (A)
3151 and then Enclosing_CPP_Parent (Typ) /= A_Type
3152 then
3153 Error_Msg_NE
3154 ("??must use 'C'P'P constructor for type &", A,
3155 Enclosing_CPP_Parent (Typ));
3157 -- The following call is not needed if the previous warning
3158 -- is promoted to an error.
3160 Resolve_Record_Aggregate (N, Typ);
3162 elsif Is_Class_Wide_Type (Etype (A))
3163 and then Nkind (Original_Node (A)) = N_Function_Call
3164 then
3165 -- If the ancestor part is a dispatching call, it appears
3166 -- statically to be a legal ancestor, but it yields any member
3167 -- of the class, and it is not possible to determine whether
3168 -- it is an ancestor of the extension aggregate (much less
3169 -- which ancestor). It is not possible to determine the
3170 -- components of the extension part.
3172 -- This check implements AI-306, which in fact was motivated by
3173 -- an AdaCore query to the ARG after this test was added.
3175 Error_Msg_N ("ancestor part must be statically tagged", A);
3176 else
3177 Resolve_Record_Aggregate (N, Typ);
3178 end if;
3179 end if;
3181 else
3182 Error_Msg_N ("no unique type for this aggregate", A);
3183 end if;
3185 Check_Function_Writable_Actuals (N);
3186 end Resolve_Extension_Aggregate;
3188 ------------------------------
3189 -- Resolve_Record_Aggregate --
3190 ------------------------------
3192 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
3193 New_Assoc_List : constant List_Id := New_List;
3194 -- New_Assoc_List is the newly built list of N_Component_Association
3195 -- nodes.
3197 Others_Etype : Entity_Id := Empty;
3198 -- This variable is used to save the Etype of the last record component
3199 -- that takes its value from the others choice. Its purpose is:
3201 -- (a) make sure the others choice is useful
3203 -- (b) make sure the type of all the components whose value is
3204 -- subsumed by the others choice are the same.
3206 -- This variable is updated as a side effect of function Get_Value.
3208 Box_Node : Node_Id := Empty;
3209 Is_Box_Present : Boolean := False;
3210 Others_Box : Integer := 0;
3211 -- Ada 2005 (AI-287): Variables used in case of default initialization
3212 -- to provide a functionality similar to Others_Etype. Box_Present
3213 -- indicates that the component takes its default initialization;
3214 -- Others_Box counts the number of components of the current aggregate
3215 -- (which may be a sub-aggregate of a larger one) that are default-
3216 -- initialized. A value of One indicates that an others_box is present.
3217 -- Any larger value indicates that the others_box is not redundant.
3218 -- These variables, similar to Others_Etype, are also updated as a side
3219 -- effect of function Get_Value. Box_Node is used to place a warning on
3220 -- a redundant others_box.
3222 procedure Add_Association
3223 (Component : Entity_Id;
3224 Expr : Node_Id;
3225 Assoc_List : List_Id;
3226 Is_Box_Present : Boolean := False);
3227 -- Builds a new N_Component_Association node which associates Component
3228 -- to expression Expr and adds it to the association list being built,
3229 -- either New_Assoc_List, or the association being built for an inner
3230 -- aggregate.
3232 procedure Add_Discriminant_Values
3233 (New_Aggr : Node_Id;
3234 Assoc_List : List_Id);
3235 -- The constraint to a component may be given by a discriminant of the
3236 -- enclosing type, in which case we have to retrieve its value, which is
3237 -- part of the enclosing aggregate. Assoc_List provides the discriminant
3238 -- associations of the current type or of some enclosing record.
3240 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean;
3241 -- If aggregate N is a regular aggregate this routine will return True.
3242 -- Otherwise, if N is an extension aggregate, then Input_Discr denotes
3243 -- a discriminant whose value may already have been specified by N's
3244 -- ancestor part. This routine checks whether this is indeed the case
3245 -- and if so returns False, signaling that no value for Input_Discr
3246 -- should appear in N's aggregate part. Also, in this case, the routine
3247 -- appends to New_Assoc_List the discriminant value specified in the
3248 -- ancestor part.
3250 -- If the aggregate is in a context with expansion delayed, it will be
3251 -- reanalyzed. The inherited discriminant values must not be reinserted
3252 -- in the component list to prevent spurious errors, but they must be
3253 -- present on first analysis to build the proper subtype indications.
3254 -- The flag Inherited_Discriminant is used to prevent the re-insertion.
3256 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id;
3257 -- AI05-0115: Find earlier ancestor in the derivation chain that is
3258 -- derived from private view Typ. Whether the aggregate is legal depends
3259 -- on the current visibility of the type as well as that of the parent
3260 -- of the ancestor.
3262 function Get_Value
3263 (Compon : Node_Id;
3264 From : List_Id;
3265 Consider_Others_Choice : Boolean := False) return Node_Id;
3266 -- Given a record component stored in parameter Compon, this function
3267 -- returns its value as it appears in the list From, which is a list
3268 -- of N_Component_Association nodes.
3270 -- If no component association has a choice for the searched component,
3271 -- the value provided by the others choice is returned, if there is one,
3272 -- and Consider_Others_Choice is set to true. Otherwise Empty is
3273 -- returned. If there is more than one component association giving a
3274 -- value for the searched record component, an error message is emitted
3275 -- and the first found value is returned.
3277 -- If Consider_Others_Choice is set and the returned expression comes
3278 -- from the others choice, then Others_Etype is set as a side effect.
3279 -- An error message is emitted if the components taking their value from
3280 -- the others choice do not have same type.
3282 function New_Copy_Tree_And_Copy_Dimensions
3283 (Source : Node_Id;
3284 Map : Elist_Id := No_Elist;
3285 New_Sloc : Source_Ptr := No_Location;
3286 New_Scope : Entity_Id := Empty) return Node_Id;
3287 -- Same as New_Copy_Tree (defined in Sem_Util), except that this routine
3288 -- also copies the dimensions of Source to the returned node.
3290 procedure Propagate_Discriminants
3291 (Aggr : Node_Id;
3292 Assoc_List : List_Id);
3293 -- Nested components may themselves be discriminated types constrained
3294 -- by outer discriminants, whose values must be captured before the
3295 -- aggregate is expanded into assignments.
3297 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id);
3298 -- Analyzes and resolves expression Expr against the Etype of the
3299 -- Component. This routine also applies all appropriate checks to Expr.
3300 -- It finally saves a Expr in the newly created association list that
3301 -- will be attached to the final record aggregate. Note that if the
3302 -- Parent pointer of Expr is not set then Expr was produced with a
3303 -- New_Copy_Tree or some such.
3305 ---------------------
3306 -- Add_Association --
3307 ---------------------
3309 procedure Add_Association
3310 (Component : Entity_Id;
3311 Expr : Node_Id;
3312 Assoc_List : List_Id;
3313 Is_Box_Present : Boolean := False)
3315 Choice_List : constant List_Id := New_List;
3316 Loc : Source_Ptr;
3318 begin
3319 -- If this is a box association the expression is missing, so use the
3320 -- Sloc of the aggregate itself for the new association.
3322 if Present (Expr) then
3323 Loc := Sloc (Expr);
3324 else
3325 Loc := Sloc (N);
3326 end if;
3328 Append_To (Choice_List, New_Occurrence_Of (Component, Loc));
3330 Append_To (Assoc_List,
3331 Make_Component_Association (Loc,
3332 Choices => Choice_List,
3333 Expression => Expr,
3334 Box_Present => Is_Box_Present));
3335 end Add_Association;
3337 -----------------------------
3338 -- Add_Discriminant_Values --
3339 -----------------------------
3341 procedure Add_Discriminant_Values
3342 (New_Aggr : Node_Id;
3343 Assoc_List : List_Id)
3345 Assoc : Node_Id;
3346 Discr : Entity_Id;
3347 Discr_Elmt : Elmt_Id;
3348 Discr_Val : Node_Id;
3349 Val : Entity_Id;
3351 begin
3352 Discr := First_Discriminant (Etype (New_Aggr));
3353 Discr_Elmt := First_Elmt (Discriminant_Constraint (Etype (New_Aggr)));
3354 while Present (Discr_Elmt) loop
3355 Discr_Val := Node (Discr_Elmt);
3357 -- If the constraint is given by a discriminant then it is a
3358 -- discriminant of an enclosing record, and its value has already
3359 -- been placed in the association list.
3361 if Is_Entity_Name (Discr_Val)
3362 and then Ekind (Entity (Discr_Val)) = E_Discriminant
3363 then
3364 Val := Entity (Discr_Val);
3366 Assoc := First (Assoc_List);
3367 while Present (Assoc) loop
3368 if Present (Entity (First (Choices (Assoc))))
3369 and then Entity (First (Choices (Assoc))) = Val
3370 then
3371 Discr_Val := Expression (Assoc);
3372 exit;
3373 end if;
3375 Next (Assoc);
3376 end loop;
3377 end if;
3379 Add_Association
3380 (Discr, New_Copy_Tree (Discr_Val),
3381 Component_Associations (New_Aggr));
3383 -- If the discriminant constraint is a current instance, mark the
3384 -- current aggregate so that the self-reference can be expanded
3385 -- later. The constraint may refer to the subtype of aggregate, so
3386 -- use base type for comparison.
3388 if Nkind (Discr_Val) = N_Attribute_Reference
3389 and then Is_Entity_Name (Prefix (Discr_Val))
3390 and then Is_Type (Entity (Prefix (Discr_Val)))
3391 and then Base_Type (Etype (N)) = Entity (Prefix (Discr_Val))
3392 then
3393 Set_Has_Self_Reference (N);
3394 end if;
3396 Next_Elmt (Discr_Elmt);
3397 Next_Discriminant (Discr);
3398 end loop;
3399 end Add_Discriminant_Values;
3401 --------------------------
3402 -- Discriminant_Present --
3403 --------------------------
3405 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean is
3406 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
3408 Ancestor_Is_Subtyp : Boolean;
3410 Loc : Source_Ptr;
3412 Ancestor : Node_Id;
3413 Ancestor_Typ : Entity_Id;
3414 Comp_Assoc : Node_Id;
3415 Discr : Entity_Id;
3416 Discr_Expr : Node_Id;
3417 Discr_Val : Elmt_Id := No_Elmt;
3418 Orig_Discr : Entity_Id;
3420 begin
3421 if Regular_Aggr then
3422 return True;
3423 end if;
3425 -- Check whether inherited discriminant values have already been
3426 -- inserted in the aggregate. This will be the case if we are
3427 -- re-analyzing an aggregate whose expansion was delayed.
3429 if Present (Component_Associations (N)) then
3430 Comp_Assoc := First (Component_Associations (N));
3431 while Present (Comp_Assoc) loop
3432 if Inherited_Discriminant (Comp_Assoc) then
3433 return True;
3434 end if;
3436 Next (Comp_Assoc);
3437 end loop;
3438 end if;
3440 Ancestor := Ancestor_Part (N);
3441 Ancestor_Typ := Etype (Ancestor);
3442 Loc := Sloc (Ancestor);
3444 -- For a private type with unknown discriminants, use the underlying
3445 -- record view if it is available.
3447 if Has_Unknown_Discriminants (Ancestor_Typ)
3448 and then Present (Full_View (Ancestor_Typ))
3449 and then Present (Underlying_Record_View (Full_View (Ancestor_Typ)))
3450 then
3451 Ancestor_Typ := Underlying_Record_View (Full_View (Ancestor_Typ));
3452 end if;
3454 Ancestor_Is_Subtyp :=
3455 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
3457 -- If the ancestor part has no discriminants clearly N's aggregate
3458 -- part must provide a value for Discr.
3460 if not Has_Discriminants (Ancestor_Typ) then
3461 return True;
3463 -- If the ancestor part is an unconstrained subtype mark then the
3464 -- Discr must be present in N's aggregate part.
3466 elsif Ancestor_Is_Subtyp
3467 and then not Is_Constrained (Entity (Ancestor))
3468 then
3469 return True;
3470 end if;
3472 -- Now look to see if Discr was specified in the ancestor part
3474 if Ancestor_Is_Subtyp then
3475 Discr_Val :=
3476 First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
3477 end if;
3479 Orig_Discr := Original_Record_Component (Input_Discr);
3481 Discr := First_Discriminant (Ancestor_Typ);
3482 while Present (Discr) loop
3484 -- If Ancestor has already specified Disc value then insert its
3485 -- value in the final aggregate.
3487 if Original_Record_Component (Discr) = Orig_Discr then
3488 if Ancestor_Is_Subtyp then
3489 Discr_Expr := New_Copy_Tree (Node (Discr_Val));
3490 else
3491 Discr_Expr :=
3492 Make_Selected_Component (Loc,
3493 Prefix => Duplicate_Subexpr (Ancestor),
3494 Selector_Name => New_Occurrence_Of (Input_Discr, Loc));
3495 end if;
3497 Resolve_Aggr_Expr (Discr_Expr, Input_Discr);
3498 Set_Inherited_Discriminant (Last (New_Assoc_List));
3499 return False;
3500 end if;
3502 Next_Discriminant (Discr);
3504 if Ancestor_Is_Subtyp then
3505 Next_Elmt (Discr_Val);
3506 end if;
3507 end loop;
3509 return True;
3510 end Discriminant_Present;
3512 ---------------------------
3513 -- Find_Private_Ancestor --
3514 ---------------------------
3516 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id is
3517 Par : Entity_Id;
3519 begin
3520 Par := Typ;
3521 loop
3522 if Has_Private_Ancestor (Par)
3523 and then not Has_Private_Ancestor (Etype (Base_Type (Par)))
3524 then
3525 return Par;
3527 elsif not Is_Derived_Type (Par) then
3528 return Empty;
3530 else
3531 Par := Etype (Base_Type (Par));
3532 end if;
3533 end loop;
3534 end Find_Private_Ancestor;
3536 ---------------
3537 -- Get_Value --
3538 ---------------
3540 function Get_Value
3541 (Compon : Node_Id;
3542 From : List_Id;
3543 Consider_Others_Choice : Boolean := False) return Node_Id
3545 Typ : constant Entity_Id := Etype (Compon);
3546 Assoc : Node_Id;
3547 Expr : Node_Id := Empty;
3548 Selector_Name : Node_Id;
3550 begin
3551 Is_Box_Present := False;
3553 if No (From) then
3554 return Empty;
3555 end if;
3557 Assoc := First (From);
3558 while Present (Assoc) loop
3559 Selector_Name := First (Choices (Assoc));
3560 while Present (Selector_Name) loop
3561 if Nkind (Selector_Name) = N_Others_Choice then
3562 if Consider_Others_Choice and then No (Expr) then
3564 -- We need to duplicate the expression for each
3565 -- successive component covered by the others choice.
3566 -- This is redundant if the others_choice covers only
3567 -- one component (small optimization possible???), but
3568 -- indispensable otherwise, because each one must be
3569 -- expanded individually to preserve side-effects.
3571 -- Ada 2005 (AI-287): In case of default initialization
3572 -- of components, we duplicate the corresponding default
3573 -- expression (from the record type declaration). The
3574 -- copy must carry the sloc of the association (not the
3575 -- original expression) to prevent spurious elaboration
3576 -- checks when the default includes function calls.
3578 if Box_Present (Assoc) then
3579 Others_Box := Others_Box + 1;
3580 Is_Box_Present := True;
3582 if Expander_Active then
3583 return
3584 New_Copy_Tree_And_Copy_Dimensions
3585 (Expression (Parent (Compon)),
3586 New_Sloc => Sloc (Assoc));
3587 else
3588 return Expression (Parent (Compon));
3589 end if;
3591 else
3592 if Present (Others_Etype)
3593 and then Base_Type (Others_Etype) /= Base_Type (Typ)
3594 then
3595 -- If the components are of an anonymous access
3596 -- type they are distinct, but this is legal in
3597 -- Ada 2012 as long as designated types match.
3599 if (Ekind (Typ) = E_Anonymous_Access_Type
3600 or else Ekind (Typ) =
3601 E_Anonymous_Access_Subprogram_Type)
3602 and then Designated_Type (Typ) =
3603 Designated_Type (Others_Etype)
3604 then
3605 null;
3606 else
3607 Error_Msg_N
3608 ("components in OTHERS choice must have same "
3609 & "type", Selector_Name);
3610 end if;
3611 end if;
3613 Others_Etype := Typ;
3615 -- Copy the expression so that it is resolved
3616 -- independently for each component, This is needed
3617 -- for accessibility checks on compoents of anonymous
3618 -- access types, even in compile_only mode.
3620 if not Inside_A_Generic then
3622 -- In ASIS mode, preanalyze the expression in an
3623 -- others association before making copies for
3624 -- separate resolution and accessibility checks.
3625 -- This ensures that the type of the expression is
3626 -- available to ASIS in all cases, in particular if
3627 -- the expression is itself an aggregate.
3629 if ASIS_Mode then
3630 Preanalyze_And_Resolve (Expression (Assoc), Typ);
3631 end if;
3633 return
3634 New_Copy_Tree_And_Copy_Dimensions
3635 (Expression (Assoc));
3637 else
3638 return Expression (Assoc);
3639 end if;
3640 end if;
3641 end if;
3643 elsif Chars (Compon) = Chars (Selector_Name) then
3644 if No (Expr) then
3646 -- Ada 2005 (AI-231)
3648 if Ada_Version >= Ada_2005
3649 and then Known_Null (Expression (Assoc))
3650 then
3651 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
3652 end if;
3654 -- We need to duplicate the expression when several
3655 -- components are grouped together with a "|" choice.
3656 -- For instance "filed1 | filed2 => Expr"
3658 -- Ada 2005 (AI-287)
3660 if Box_Present (Assoc) then
3661 Is_Box_Present := True;
3663 -- Duplicate the default expression of the component
3664 -- from the record type declaration, so a new copy
3665 -- can be attached to the association.
3667 -- Note that we always copy the default expression,
3668 -- even when the association has a single choice, in
3669 -- order to create a proper association for the
3670 -- expanded aggregate.
3672 -- Component may have no default, in which case the
3673 -- expression is empty and the component is default-
3674 -- initialized, but an association for the component
3675 -- exists, and it is not covered by an others clause.
3677 -- Scalar and private types have no initialization
3678 -- procedure, so they remain uninitialized. If the
3679 -- target of the aggregate is a constant this
3680 -- deserves a warning.
3682 if No (Expression (Parent (Compon)))
3683 and then not Has_Non_Null_Base_Init_Proc (Typ)
3684 and then not Has_Aspect (Typ, Aspect_Default_Value)
3685 and then not Is_Concurrent_Type (Typ)
3686 and then Nkind (Parent (N)) = N_Object_Declaration
3687 and then Constant_Present (Parent (N))
3688 then
3689 Error_Msg_Node_2 := Typ;
3690 Error_Msg_NE
3691 ("component&? of type& is uninitialized",
3692 Assoc, Selector_Name);
3694 -- An additional reminder if the component type
3695 -- is a generic formal.
3697 if Is_Generic_Type (Base_Type (Typ)) then
3698 Error_Msg_NE
3699 ("\instance should provide actual type with "
3700 & "initialization for&", Assoc, Typ);
3701 end if;
3702 end if;
3704 return
3705 New_Copy_Tree_And_Copy_Dimensions
3706 (Expression (Parent (Compon)));
3708 else
3709 if Present (Next (Selector_Name)) then
3710 Expr := New_Copy_Tree_And_Copy_Dimensions
3711 (Expression (Assoc));
3712 else
3713 Expr := Expression (Assoc);
3714 end if;
3715 end if;
3717 Generate_Reference (Compon, Selector_Name, 'm');
3719 else
3720 Error_Msg_NE
3721 ("more than one value supplied for &",
3722 Selector_Name, Compon);
3724 end if;
3725 end if;
3727 Next (Selector_Name);
3728 end loop;
3730 Next (Assoc);
3731 end loop;
3733 return Expr;
3734 end Get_Value;
3736 ---------------------------------------
3737 -- New_Copy_Tree_And_Copy_Dimensions --
3738 ---------------------------------------
3740 function New_Copy_Tree_And_Copy_Dimensions
3741 (Source : Node_Id;
3742 Map : Elist_Id := No_Elist;
3743 New_Sloc : Source_Ptr := No_Location;
3744 New_Scope : Entity_Id := Empty) return Node_Id
3746 New_Copy : constant Node_Id :=
3747 New_Copy_Tree (Source, Map, New_Sloc, New_Scope);
3749 begin
3750 -- Move the dimensions of Source to New_Copy
3752 Copy_Dimensions (Source, New_Copy);
3753 return New_Copy;
3754 end New_Copy_Tree_And_Copy_Dimensions;
3756 -----------------------------
3757 -- Propagate_Discriminants --
3758 -----------------------------
3760 procedure Propagate_Discriminants
3761 (Aggr : Node_Id;
3762 Assoc_List : List_Id)
3764 Loc : constant Source_Ptr := Sloc (N);
3766 Needs_Box : Boolean := False;
3768 procedure Process_Component (Comp : Entity_Id);
3769 -- Add one component with a box association to the inner aggregate,
3770 -- and recurse if component is itself composite.
3772 -----------------------
3773 -- Process_Component --
3774 -----------------------
3776 procedure Process_Component (Comp : Entity_Id) is
3777 T : constant Entity_Id := Etype (Comp);
3778 New_Aggr : Node_Id;
3780 begin
3781 if Is_Record_Type (T) and then Has_Discriminants (T) then
3782 New_Aggr := Make_Aggregate (Loc, New_List, New_List);
3783 Set_Etype (New_Aggr, T);
3785 Add_Association
3786 (Comp, New_Aggr, Component_Associations (Aggr));
3788 -- Collect discriminant values and recurse
3790 Add_Discriminant_Values (New_Aggr, Assoc_List);
3791 Propagate_Discriminants (New_Aggr, Assoc_List);
3793 else
3794 Needs_Box := True;
3795 end if;
3796 end Process_Component;
3798 -- Local variables
3800 Aggr_Type : constant Entity_Id := Base_Type (Etype (Aggr));
3801 Components : constant Elist_Id := New_Elmt_List;
3802 Def_Node : constant Node_Id :=
3803 Type_Definition (Declaration_Node (Aggr_Type));
3805 Comp : Node_Id;
3806 Comp_Elmt : Elmt_Id;
3807 Errors : Boolean;
3809 -- Start of processing for Propagate_Discriminants
3811 begin
3812 -- The component type may be a variant type. Collect the components
3813 -- that are ruled by the known values of the discriminants. Their
3814 -- values have already been inserted into the component list of the
3815 -- current aggregate.
3817 if Nkind (Def_Node) = N_Record_Definition
3818 and then Present (Component_List (Def_Node))
3819 and then Present (Variant_Part (Component_List (Def_Node)))
3820 then
3821 Gather_Components (Aggr_Type,
3822 Component_List (Def_Node),
3823 Governed_By => Component_Associations (Aggr),
3824 Into => Components,
3825 Report_Errors => Errors);
3827 Comp_Elmt := First_Elmt (Components);
3828 while Present (Comp_Elmt) loop
3829 if Ekind (Node (Comp_Elmt)) /= E_Discriminant then
3830 Process_Component (Node (Comp_Elmt));
3831 end if;
3833 Next_Elmt (Comp_Elmt);
3834 end loop;
3836 -- No variant part, iterate over all components
3838 else
3839 Comp := First_Component (Etype (Aggr));
3840 while Present (Comp) loop
3841 Process_Component (Comp);
3842 Next_Component (Comp);
3843 end loop;
3844 end if;
3846 if Needs_Box then
3847 Append_To (Component_Associations (Aggr),
3848 Make_Component_Association (Loc,
3849 Choices => New_List (Make_Others_Choice (Loc)),
3850 Expression => Empty,
3851 Box_Present => True));
3852 end if;
3853 end Propagate_Discriminants;
3855 -----------------------
3856 -- Resolve_Aggr_Expr --
3857 -----------------------
3859 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id) is
3860 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
3861 -- If the expression is an aggregate (possibly qualified) then its
3862 -- expansion is delayed until the enclosing aggregate is expanded
3863 -- into assignments. In that case, do not generate checks on the
3864 -- expression, because they will be generated later, and will other-
3865 -- wise force a copy (to remove side-effects) that would leave a
3866 -- dynamic-sized aggregate in the code, something that gigi cannot
3867 -- handle.
3869 ---------------------------
3870 -- Has_Expansion_Delayed --
3871 ---------------------------
3873 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
3874 begin
3875 return
3876 (Nkind_In (Expr, N_Aggregate, N_Extension_Aggregate)
3877 and then Present (Etype (Expr))
3878 and then Is_Record_Type (Etype (Expr))
3879 and then Expansion_Delayed (Expr))
3880 or else
3881 (Nkind (Expr) = N_Qualified_Expression
3882 and then Has_Expansion_Delayed (Expression (Expr)));
3883 end Has_Expansion_Delayed;
3885 -- Local variables
3887 Expr_Type : Entity_Id := Empty;
3888 New_C : Entity_Id := Component;
3889 New_Expr : Node_Id;
3891 Relocate : Boolean;
3892 -- Set to True if the resolved Expr node needs to be relocated when
3893 -- attached to the newly created association list. This node need not
3894 -- be relocated if its parent pointer is not set. In fact in this
3895 -- case Expr is the output of a New_Copy_Tree call. If Relocate is
3896 -- True then we have analyzed the expression node in the original
3897 -- aggregate and hence it needs to be relocated when moved over to
3898 -- the new association list.
3900 -- Start of processing for Resolve_Aggr_Expr
3902 begin
3903 -- If the type of the component is elementary or the type of the
3904 -- aggregate does not contain discriminants, use the type of the
3905 -- component to resolve Expr.
3907 if Is_Elementary_Type (Etype (Component))
3908 or else not Has_Discriminants (Etype (N))
3909 then
3910 Expr_Type := Etype (Component);
3912 -- Otherwise we have to pick up the new type of the component from
3913 -- the new constrained subtype of the aggregate. In fact components
3914 -- which are of a composite type might be constrained by a
3915 -- discriminant, and we want to resolve Expr against the subtype were
3916 -- all discriminant occurrences are replaced with their actual value.
3918 else
3919 New_C := First_Component (Etype (N));
3920 while Present (New_C) loop
3921 if Chars (New_C) = Chars (Component) then
3922 Expr_Type := Etype (New_C);
3923 exit;
3924 end if;
3926 Next_Component (New_C);
3927 end loop;
3929 pragma Assert (Present (Expr_Type));
3931 -- For each range in an array type where a discriminant has been
3932 -- replaced with the constraint, check that this range is within
3933 -- the range of the base type. This checks is done in the init
3934 -- proc for regular objects, but has to be done here for
3935 -- aggregates since no init proc is called for them.
3937 if Is_Array_Type (Expr_Type) then
3938 declare
3939 Index : Node_Id;
3940 -- Range of the current constrained index in the array
3942 Orig_Index : Node_Id := First_Index (Etype (Component));
3943 -- Range corresponding to the range Index above in the
3944 -- original unconstrained record type. The bounds of this
3945 -- range may be governed by discriminants.
3947 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
3948 -- Range corresponding to the range Index above for the
3949 -- unconstrained array type. This range is needed to apply
3950 -- range checks.
3952 begin
3953 Index := First_Index (Expr_Type);
3954 while Present (Index) loop
3955 if Depends_On_Discriminant (Orig_Index) then
3956 Apply_Range_Check (Index, Etype (Unconstr_Index));
3957 end if;
3959 Next_Index (Index);
3960 Next_Index (Orig_Index);
3961 Next_Index (Unconstr_Index);
3962 end loop;
3963 end;
3964 end if;
3965 end if;
3967 -- If the Parent pointer of Expr is not set, Expr is an expression
3968 -- duplicated by New_Tree_Copy (this happens for record aggregates
3969 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
3970 -- Such a duplicated expression must be attached to the tree
3971 -- before analysis and resolution to enforce the rule that a tree
3972 -- fragment should never be analyzed or resolved unless it is
3973 -- attached to the current compilation unit.
3975 if No (Parent (Expr)) then
3976 Set_Parent (Expr, N);
3977 Relocate := False;
3978 else
3979 Relocate := True;
3980 end if;
3982 Analyze_And_Resolve (Expr, Expr_Type);
3983 Check_Expr_OK_In_Limited_Aggregate (Expr);
3984 Check_Non_Static_Context (Expr);
3985 Check_Unset_Reference (Expr);
3987 -- Check wrong use of class-wide types
3989 if Is_Class_Wide_Type (Etype (Expr)) then
3990 Error_Msg_N ("dynamically tagged expression not allowed", Expr);
3991 end if;
3993 if not Has_Expansion_Delayed (Expr) then
3994 Aggregate_Constraint_Checks (Expr, Expr_Type);
3995 end if;
3997 -- If an aggregate component has a type with predicates, an explicit
3998 -- predicate check must be applied, as for an assignment statement,
3999 -- because the aggegate might not be expanded into individual
4000 -- component assignments.
4002 if Present (Predicate_Function (Expr_Type))
4003 and then Analyzed (Expr)
4004 then
4005 Apply_Predicate_Check (Expr, Expr_Type);
4006 end if;
4008 if Raises_Constraint_Error (Expr) then
4009 Set_Raises_Constraint_Error (N);
4010 end if;
4012 -- If the expression has been marked as requiring a range check, then
4013 -- generate it here. It's a bit odd to be generating such checks in
4014 -- the analyzer, but harmless since Generate_Range_Check does nothing
4015 -- (other than making sure Do_Range_Check is set) if the expander is
4016 -- not active.
4018 if Do_Range_Check (Expr) then
4019 Generate_Range_Check (Expr, Expr_Type, CE_Range_Check_Failed);
4020 end if;
4022 -- Add association Component => Expr if the caller requests it
4024 if Relocate then
4025 New_Expr := Relocate_Node (Expr);
4027 -- Since New_Expr is not gonna be analyzed later on, we need to
4028 -- propagate here the dimensions form Expr to New_Expr.
4030 Copy_Dimensions (Expr, New_Expr);
4032 else
4033 New_Expr := Expr;
4034 end if;
4036 Add_Association (New_C, New_Expr, New_Assoc_List);
4037 end Resolve_Aggr_Expr;
4039 -- Local variables
4041 Components : constant Elist_Id := New_Elmt_List;
4042 -- Components is the list of the record components whose value must be
4043 -- provided in the aggregate. This list does include discriminants.
4045 Expr : Node_Id;
4046 Component : Entity_Id;
4047 Component_Elmt : Elmt_Id;
4048 Positional_Expr : Node_Id;
4050 -- Start of processing for Resolve_Record_Aggregate
4052 begin
4053 -- A record aggregate is restricted in SPARK:
4055 -- Each named association can have only a single choice.
4056 -- OTHERS cannot be used.
4057 -- Positional and named associations cannot be mixed.
4059 if Present (Component_Associations (N))
4060 and then Present (First (Component_Associations (N)))
4061 then
4062 if Present (Expressions (N)) then
4063 Check_SPARK_05_Restriction
4064 ("named association cannot follow positional one",
4065 First (Choices (First (Component_Associations (N)))));
4066 end if;
4068 declare
4069 Assoc : Node_Id;
4071 begin
4072 Assoc := First (Component_Associations (N));
4073 while Present (Assoc) loop
4074 if List_Length (Choices (Assoc)) > 1 then
4075 Check_SPARK_05_Restriction
4076 ("component association in record aggregate must "
4077 & "contain a single choice", Assoc);
4078 end if;
4080 if Nkind (First (Choices (Assoc))) = N_Others_Choice then
4081 Check_SPARK_05_Restriction
4082 ("record aggregate cannot contain OTHERS", Assoc);
4083 end if;
4085 Assoc := Next (Assoc);
4086 end loop;
4087 end;
4088 end if;
4090 -- We may end up calling Duplicate_Subexpr on expressions that are
4091 -- attached to New_Assoc_List. For this reason we need to attach it
4092 -- to the tree by setting its parent pointer to N. This parent point
4093 -- will change in STEP 8 below.
4095 Set_Parent (New_Assoc_List, N);
4097 -- STEP 1: abstract type and null record verification
4099 if Is_Abstract_Type (Typ) then
4100 Error_Msg_N ("type of aggregate cannot be abstract", N);
4101 end if;
4103 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
4104 Set_Etype (N, Typ);
4105 return;
4107 elsif Present (First_Entity (Typ))
4108 and then Null_Record_Present (N)
4109 and then not Is_Tagged_Type (Typ)
4110 then
4111 Error_Msg_N ("record aggregate cannot be null", N);
4112 return;
4114 -- If the type has no components, then the aggregate should either
4115 -- have "null record", or in Ada 2005 it could instead have a single
4116 -- component association given by "others => <>". For Ada 95 we flag an
4117 -- error at this point, but for Ada 2005 we proceed with checking the
4118 -- associations below, which will catch the case where it's not an
4119 -- aggregate with "others => <>". Note that the legality of a <>
4120 -- aggregate for a null record type was established by AI05-016.
4122 elsif No (First_Entity (Typ))
4123 and then Ada_Version < Ada_2005
4124 then
4125 Error_Msg_N ("record aggregate must be null", N);
4126 return;
4127 end if;
4129 -- STEP 2: Verify aggregate structure
4131 Step_2 : declare
4132 Assoc : Node_Id;
4133 Bad_Aggregate : Boolean := False;
4134 Selector_Name : Node_Id;
4136 begin
4137 if Present (Component_Associations (N)) then
4138 Assoc := First (Component_Associations (N));
4139 else
4140 Assoc := Empty;
4141 end if;
4143 while Present (Assoc) loop
4144 Selector_Name := First (Choices (Assoc));
4145 while Present (Selector_Name) loop
4146 if Nkind (Selector_Name) = N_Identifier then
4147 null;
4149 elsif Nkind (Selector_Name) = N_Others_Choice then
4150 if Selector_Name /= First (Choices (Assoc))
4151 or else Present (Next (Selector_Name))
4152 then
4153 Error_Msg_N
4154 ("OTHERS must appear alone in a choice list",
4155 Selector_Name);
4156 return;
4158 elsif Present (Next (Assoc)) then
4159 Error_Msg_N
4160 ("OTHERS must appear last in an aggregate",
4161 Selector_Name);
4162 return;
4164 -- (Ada 2005): If this is an association with a box,
4165 -- indicate that the association need not represent
4166 -- any component.
4168 elsif Box_Present (Assoc) then
4169 Others_Box := 1;
4170 Box_Node := Assoc;
4171 end if;
4173 else
4174 Error_Msg_N
4175 ("selector name should be identifier or OTHERS",
4176 Selector_Name);
4177 Bad_Aggregate := True;
4178 end if;
4180 Next (Selector_Name);
4181 end loop;
4183 Next (Assoc);
4184 end loop;
4186 if Bad_Aggregate then
4187 return;
4188 end if;
4189 end Step_2;
4191 -- STEP 3: Find discriminant Values
4193 Step_3 : declare
4194 Discrim : Entity_Id;
4195 Missing_Discriminants : Boolean := False;
4197 begin
4198 if Present (Expressions (N)) then
4199 Positional_Expr := First (Expressions (N));
4200 else
4201 Positional_Expr := Empty;
4202 end if;
4204 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
4205 -- must not have unknown discriminants.
4207 if Is_Derived_Type (Typ)
4208 and then Has_Unknown_Discriminants (Root_Type (Typ))
4209 and then Nkind (N) /= N_Extension_Aggregate
4210 then
4211 Error_Msg_NE
4212 ("aggregate not available for type& whose ancestor "
4213 & "has unknown discriminants ", N, Typ);
4214 end if;
4216 if Has_Unknown_Discriminants (Typ)
4217 and then Present (Underlying_Record_View (Typ))
4218 then
4219 Discrim := First_Discriminant (Underlying_Record_View (Typ));
4220 elsif Has_Discriminants (Typ) then
4221 Discrim := First_Discriminant (Typ);
4222 else
4223 Discrim := Empty;
4224 end if;
4226 -- First find the discriminant values in the positional components
4228 while Present (Discrim) and then Present (Positional_Expr) loop
4229 if Discriminant_Present (Discrim) then
4230 Resolve_Aggr_Expr (Positional_Expr, Discrim);
4232 -- Ada 2005 (AI-231)
4234 if Ada_Version >= Ada_2005
4235 and then Known_Null (Positional_Expr)
4236 then
4237 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
4238 end if;
4240 Next (Positional_Expr);
4241 end if;
4243 if Present (Get_Value (Discrim, Component_Associations (N))) then
4244 Error_Msg_NE
4245 ("more than one value supplied for discriminant&",
4246 N, Discrim);
4247 end if;
4249 Next_Discriminant (Discrim);
4250 end loop;
4252 -- Find remaining discriminant values if any among named components
4254 while Present (Discrim) loop
4255 Expr := Get_Value (Discrim, Component_Associations (N), True);
4257 if not Discriminant_Present (Discrim) then
4258 if Present (Expr) then
4259 Error_Msg_NE
4260 ("more than one value supplied for discriminant &",
4261 N, Discrim);
4262 end if;
4264 elsif No (Expr) then
4265 Error_Msg_NE
4266 ("no value supplied for discriminant &", N, Discrim);
4267 Missing_Discriminants := True;
4269 else
4270 Resolve_Aggr_Expr (Expr, Discrim);
4271 end if;
4273 Next_Discriminant (Discrim);
4274 end loop;
4276 if Missing_Discriminants then
4277 return;
4278 end if;
4280 -- At this point and until the beginning of STEP 6, New_Assoc_List
4281 -- contains only the discriminants and their values.
4283 end Step_3;
4285 -- STEP 4: Set the Etype of the record aggregate
4287 -- ??? This code is pretty much a copy of Sem_Ch3.Build_Subtype. That
4288 -- routine should really be exported in sem_util or some such and used
4289 -- in sem_ch3 and here rather than have a copy of the code which is a
4290 -- maintenance nightmare.
4292 -- ??? Performance WARNING. The current implementation creates a new
4293 -- itype for all aggregates whose base type is discriminated. This means
4294 -- that for record aggregates nested inside an array aggregate we will
4295 -- create a new itype for each record aggregate if the array component
4296 -- type has discriminants. For large aggregates this may be a problem.
4297 -- What should be done in this case is to reuse itypes as much as
4298 -- possible.
4300 if Has_Discriminants (Typ)
4301 or else (Has_Unknown_Discriminants (Typ)
4302 and then Present (Underlying_Record_View (Typ)))
4303 then
4304 Build_Constrained_Itype : declare
4305 Constrs : constant List_Id := New_List;
4306 Loc : constant Source_Ptr := Sloc (N);
4307 Def_Id : Entity_Id;
4308 Indic : Node_Id;
4309 New_Assoc : Node_Id;
4310 Subtyp_Decl : Node_Id;
4312 begin
4313 New_Assoc := First (New_Assoc_List);
4314 while Present (New_Assoc) loop
4315 Append_To (Constrs, Duplicate_Subexpr (Expression (New_Assoc)));
4316 Next (New_Assoc);
4317 end loop;
4319 if Has_Unknown_Discriminants (Typ)
4320 and then Present (Underlying_Record_View (Typ))
4321 then
4322 Indic :=
4323 Make_Subtype_Indication (Loc,
4324 Subtype_Mark =>
4325 New_Occurrence_Of (Underlying_Record_View (Typ), Loc),
4326 Constraint =>
4327 Make_Index_Or_Discriminant_Constraint (Loc,
4328 Constraints => Constrs));
4329 else
4330 Indic :=
4331 Make_Subtype_Indication (Loc,
4332 Subtype_Mark =>
4333 New_Occurrence_Of (Base_Type (Typ), Loc),
4334 Constraint =>
4335 Make_Index_Or_Discriminant_Constraint (Loc,
4336 Constraints => Constrs));
4337 end if;
4339 Def_Id := Create_Itype (Ekind (Typ), N);
4341 Subtyp_Decl :=
4342 Make_Subtype_Declaration (Loc,
4343 Defining_Identifier => Def_Id,
4344 Subtype_Indication => Indic);
4345 Set_Parent (Subtyp_Decl, Parent (N));
4347 -- Itypes must be analyzed with checks off (see itypes.ads)
4349 Analyze (Subtyp_Decl, Suppress => All_Checks);
4351 Set_Etype (N, Def_Id);
4352 Check_Static_Discriminated_Subtype
4353 (Def_Id, Expression (First (New_Assoc_List)));
4354 end Build_Constrained_Itype;
4356 else
4357 Set_Etype (N, Typ);
4358 end if;
4360 -- STEP 5: Get remaining components according to discriminant values
4362 Step_5 : declare
4363 Dnode : Node_Id;
4364 Errors_Found : Boolean := False;
4365 Record_Def : Node_Id;
4366 Parent_Typ : Entity_Id;
4367 Parent_Typ_List : Elist_Id;
4368 Parent_Elmt : Elmt_Id;
4369 Root_Typ : Entity_Id;
4371 begin
4372 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
4373 Parent_Typ_List := New_Elmt_List;
4375 -- If this is an extension aggregate, the component list must
4376 -- include all components that are not in the given ancestor type.
4377 -- Otherwise, the component list must include components of all
4378 -- ancestors, starting with the root.
4380 if Nkind (N) = N_Extension_Aggregate then
4381 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
4383 else
4384 -- AI05-0115: check legality of aggregate for type with a
4385 -- private ancestor.
4387 Root_Typ := Root_Type (Typ);
4388 if Has_Private_Ancestor (Typ) then
4389 declare
4390 Ancestor : constant Entity_Id :=
4391 Find_Private_Ancestor (Typ);
4392 Ancestor_Unit : constant Entity_Id :=
4393 Cunit_Entity
4394 (Get_Source_Unit (Ancestor));
4395 Parent_Unit : constant Entity_Id :=
4396 Cunit_Entity (Get_Source_Unit
4397 (Base_Type (Etype (Ancestor))));
4398 begin
4399 -- Check whether we are in a scope that has full view
4400 -- over the private ancestor and its parent. This can
4401 -- only happen if the derivation takes place in a child
4402 -- unit of the unit that declares the parent, and we are
4403 -- in the private part or body of that child unit, else
4404 -- the aggregate is illegal.
4406 if Is_Child_Unit (Ancestor_Unit)
4407 and then Scope (Ancestor_Unit) = Parent_Unit
4408 and then In_Open_Scopes (Scope (Ancestor))
4409 and then
4410 (In_Private_Part (Scope (Ancestor))
4411 or else In_Package_Body (Scope (Ancestor)))
4412 then
4413 null;
4415 else
4416 Error_Msg_NE
4417 ("type of aggregate has private ancestor&!",
4418 N, Root_Typ);
4419 Error_Msg_N ("must use extension aggregate!", N);
4420 return;
4421 end if;
4422 end;
4423 end if;
4425 Dnode := Declaration_Node (Base_Type (Root_Typ));
4427 -- If we don't get a full declaration, then we have some error
4428 -- which will get signalled later so skip this part. Otherwise
4429 -- gather components of root that apply to the aggregate type.
4430 -- We use the base type in case there is an applicable stored
4431 -- constraint that renames the discriminants of the root.
4433 if Nkind (Dnode) = N_Full_Type_Declaration then
4434 Record_Def := Type_Definition (Dnode);
4435 Gather_Components
4436 (Base_Type (Typ),
4437 Component_List (Record_Def),
4438 Governed_By => New_Assoc_List,
4439 Into => Components,
4440 Report_Errors => Errors_Found);
4442 if Errors_Found then
4443 Error_Msg_N
4444 ("discriminant controlling variant part is not static",
4446 return;
4447 end if;
4448 end if;
4449 end if;
4451 Parent_Typ := Base_Type (Typ);
4452 while Parent_Typ /= Root_Typ loop
4453 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
4454 Parent_Typ := Etype (Parent_Typ);
4456 if Nkind (Parent (Base_Type (Parent_Typ))) =
4457 N_Private_Type_Declaration
4458 or else Nkind (Parent (Base_Type (Parent_Typ))) =
4459 N_Private_Extension_Declaration
4460 then
4461 if Nkind (N) /= N_Extension_Aggregate then
4462 Error_Msg_NE
4463 ("type of aggregate has private ancestor&!",
4464 N, Parent_Typ);
4465 Error_Msg_N ("must use extension aggregate!", N);
4466 return;
4468 elsif Parent_Typ /= Root_Typ then
4469 Error_Msg_NE
4470 ("ancestor part of aggregate must be private type&",
4471 Ancestor_Part (N), Parent_Typ);
4472 return;
4473 end if;
4475 -- The current view of ancestor part may be a private type,
4476 -- while the context type is always non-private.
4478 elsif Is_Private_Type (Root_Typ)
4479 and then Present (Full_View (Root_Typ))
4480 and then Nkind (N) = N_Extension_Aggregate
4481 then
4482 exit when Base_Type (Full_View (Root_Typ)) = Parent_Typ;
4483 end if;
4484 end loop;
4486 -- Now collect components from all other ancestors, beginning
4487 -- with the current type. If the type has unknown discriminants
4488 -- use the component list of the Underlying_Record_View, which
4489 -- needs to be used for the subsequent expansion of the aggregate
4490 -- into assignments.
4492 Parent_Elmt := First_Elmt (Parent_Typ_List);
4493 while Present (Parent_Elmt) loop
4494 Parent_Typ := Node (Parent_Elmt);
4496 if Has_Unknown_Discriminants (Parent_Typ)
4497 and then Present (Underlying_Record_View (Typ))
4498 then
4499 Parent_Typ := Underlying_Record_View (Parent_Typ);
4500 end if;
4502 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
4503 Gather_Components (Empty,
4504 Component_List (Record_Extension_Part (Record_Def)),
4505 Governed_By => New_Assoc_List,
4506 Into => Components,
4507 Report_Errors => Errors_Found);
4509 Next_Elmt (Parent_Elmt);
4510 end loop;
4512 -- Typ is not a derived tagged type
4514 else
4515 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
4517 if Null_Present (Record_Def) then
4518 null;
4520 elsif not Has_Unknown_Discriminants (Typ) then
4521 Gather_Components
4522 (Base_Type (Typ),
4523 Component_List (Record_Def),
4524 Governed_By => New_Assoc_List,
4525 Into => Components,
4526 Report_Errors => Errors_Found);
4528 else
4529 Gather_Components
4530 (Base_Type (Underlying_Record_View (Typ)),
4531 Component_List (Record_Def),
4532 Governed_By => New_Assoc_List,
4533 Into => Components,
4534 Report_Errors => Errors_Found);
4535 end if;
4536 end if;
4538 if Errors_Found then
4539 return;
4540 end if;
4541 end Step_5;
4543 -- STEP 6: Find component Values
4545 Component := Empty;
4546 Component_Elmt := First_Elmt (Components);
4548 -- First scan the remaining positional associations in the aggregate.
4549 -- Remember that at this point Positional_Expr contains the current
4550 -- positional association if any is left after looking for discriminant
4551 -- values in step 3.
4553 while Present (Positional_Expr) and then Present (Component_Elmt) loop
4554 Component := Node (Component_Elmt);
4555 Resolve_Aggr_Expr (Positional_Expr, Component);
4557 -- Ada 2005 (AI-231)
4559 if Ada_Version >= Ada_2005 and then Known_Null (Positional_Expr) then
4560 Check_Can_Never_Be_Null (Component, Positional_Expr);
4561 end if;
4563 if Present (Get_Value (Component, Component_Associations (N))) then
4564 Error_Msg_NE
4565 ("more than one value supplied for Component &", N, Component);
4566 end if;
4568 Next (Positional_Expr);
4569 Next_Elmt (Component_Elmt);
4570 end loop;
4572 if Present (Positional_Expr) then
4573 Error_Msg_N
4574 ("too many components for record aggregate", Positional_Expr);
4575 end if;
4577 -- Now scan for the named arguments of the aggregate
4579 while Present (Component_Elmt) loop
4580 Component := Node (Component_Elmt);
4581 Expr := Get_Value (Component, Component_Associations (N), True);
4583 -- Note: The previous call to Get_Value sets the value of the
4584 -- variable Is_Box_Present.
4586 -- Ada 2005 (AI-287): Handle components with default initialization.
4587 -- Note: This feature was originally added to Ada 2005 for limited
4588 -- but it was finally allowed with any type.
4590 if Is_Box_Present then
4591 Check_Box_Component : declare
4592 Ctyp : constant Entity_Id := Etype (Component);
4594 begin
4595 -- If there is a default expression for the aggregate, copy
4596 -- it into a new association. This copy must modify the scopes
4597 -- of internal types that may be attached to the expression
4598 -- (e.g. index subtypes of arrays) because in general the type
4599 -- declaration and the aggregate appear in different scopes,
4600 -- and the backend requires the scope of the type to match the
4601 -- point at which it is elaborated.
4603 -- If the component has an initialization procedure (IP) we
4604 -- pass the component to the expander, which will generate
4605 -- the call to such IP.
4607 -- If the component has discriminants, their values must
4608 -- be taken from their subtype. This is indispensable for
4609 -- constraints that are given by the current instance of an
4610 -- enclosing type, to allow the expansion of the aggregate to
4611 -- replace the reference to the current instance by the target
4612 -- object of the aggregate.
4614 if Present (Parent (Component))
4615 and then Nkind (Parent (Component)) = N_Component_Declaration
4616 and then Present (Expression (Parent (Component)))
4617 then
4618 Expr :=
4619 New_Copy_Tree_And_Copy_Dimensions
4620 (Expression (Parent (Component)),
4621 New_Scope => Current_Scope,
4622 New_Sloc => Sloc (N));
4624 Add_Association
4625 (Component => Component,
4626 Expr => Expr,
4627 Assoc_List => New_Assoc_List);
4628 Set_Has_Self_Reference (N);
4630 -- A box-defaulted access component gets the value null. Also
4631 -- included are components of private types whose underlying
4632 -- type is an access type. In either case set the type of the
4633 -- literal, for subsequent use in semantic checks.
4635 elsif Present (Underlying_Type (Ctyp))
4636 and then Is_Access_Type (Underlying_Type (Ctyp))
4637 then
4638 -- If the component's type is private with an access type as
4639 -- its underlying type then we have to create an unchecked
4640 -- conversion to satisfy type checking.
4642 if Is_Private_Type (Ctyp) then
4643 declare
4644 Qual_Null : constant Node_Id :=
4645 Make_Qualified_Expression (Sloc (N),
4646 Subtype_Mark =>
4647 New_Occurrence_Of
4648 (Underlying_Type (Ctyp), Sloc (N)),
4649 Expression => Make_Null (Sloc (N)));
4651 Convert_Null : constant Node_Id :=
4652 Unchecked_Convert_To
4653 (Ctyp, Qual_Null);
4655 begin
4656 Analyze_And_Resolve (Convert_Null, Ctyp);
4657 Add_Association
4658 (Component => Component,
4659 Expr => Convert_Null,
4660 Assoc_List => New_Assoc_List);
4661 end;
4663 -- Otherwise the component type is non-private
4665 else
4666 Expr := Make_Null (Sloc (N));
4667 Set_Etype (Expr, Ctyp);
4669 Add_Association
4670 (Component => Component,
4671 Expr => Expr,
4672 Assoc_List => New_Assoc_List);
4673 end if;
4675 -- Ada 2012: If component is scalar with default value, use it
4677 elsif Is_Scalar_Type (Ctyp)
4678 and then Has_Default_Aspect (Ctyp)
4679 then
4680 Add_Association
4681 (Component => Component,
4682 Expr =>
4683 Default_Aspect_Value
4684 (First_Subtype (Underlying_Type (Ctyp))),
4685 Assoc_List => New_Assoc_List);
4687 elsif Has_Non_Null_Base_Init_Proc (Ctyp)
4688 or else not Expander_Active
4689 then
4690 if Is_Record_Type (Ctyp)
4691 and then Has_Discriminants (Ctyp)
4692 and then not Is_Private_Type (Ctyp)
4693 then
4694 -- We build a partially initialized aggregate with the
4695 -- values of the discriminants and box initialization
4696 -- for the rest, if other components are present.
4698 -- The type of the aggregate is the known subtype of
4699 -- the component. The capture of discriminants must be
4700 -- recursive because subcomponents may be constrained
4701 -- (transitively) by discriminants of enclosing types.
4702 -- For a private type with discriminants, a call to the
4703 -- initialization procedure will be generated, and no
4704 -- subaggregate is needed.
4706 Capture_Discriminants : declare
4707 Loc : constant Source_Ptr := Sloc (N);
4708 Expr : Node_Id;
4710 begin
4711 Expr := Make_Aggregate (Loc, New_List, New_List);
4712 Set_Etype (Expr, Ctyp);
4714 -- If the enclosing type has discriminants, they have
4715 -- been collected in the aggregate earlier, and they
4716 -- may appear as constraints of subcomponents.
4718 -- Similarly if this component has discriminants, they
4719 -- might in turn be propagated to their components.
4721 if Has_Discriminants (Typ) then
4722 Add_Discriminant_Values (Expr, New_Assoc_List);
4723 Propagate_Discriminants (Expr, New_Assoc_List);
4725 elsif Has_Discriminants (Ctyp) then
4726 Add_Discriminant_Values
4727 (Expr, Component_Associations (Expr));
4728 Propagate_Discriminants
4729 (Expr, Component_Associations (Expr));
4731 else
4732 declare
4733 Comp : Entity_Id;
4735 begin
4736 -- If the type has additional components, create
4737 -- an OTHERS box association for them.
4739 Comp := First_Component (Ctyp);
4740 while Present (Comp) loop
4741 if Ekind (Comp) = E_Component then
4742 if not Is_Record_Type (Etype (Comp)) then
4743 Append_To
4744 (Component_Associations (Expr),
4745 Make_Component_Association (Loc,
4746 Choices =>
4747 New_List (
4748 Make_Others_Choice (Loc)),
4749 Expression => Empty,
4750 Box_Present => True));
4751 end if;
4753 exit;
4754 end if;
4756 Next_Component (Comp);
4757 end loop;
4758 end;
4759 end if;
4761 Add_Association
4762 (Component => Component,
4763 Expr => Expr,
4764 Assoc_List => New_Assoc_List);
4765 end Capture_Discriminants;
4767 -- Otherwise the component type is not a record, or it has
4768 -- not discriminants, or it is private.
4770 else
4771 Add_Association
4772 (Component => Component,
4773 Expr => Empty,
4774 Assoc_List => New_Assoc_List,
4775 Is_Box_Present => True);
4776 end if;
4778 -- Otherwise we only need to resolve the expression if the
4779 -- component has partially initialized values (required to
4780 -- expand the corresponding assignments and run-time checks).
4782 elsif Present (Expr)
4783 and then Is_Partially_Initialized_Type (Ctyp)
4784 then
4785 Resolve_Aggr_Expr (Expr, Component);
4786 end if;
4787 end Check_Box_Component;
4789 elsif No (Expr) then
4791 -- Ignore hidden components associated with the position of the
4792 -- interface tags: these are initialized dynamically.
4794 if not Present (Related_Type (Component)) then
4795 Error_Msg_NE
4796 ("no value supplied for component &!", N, Component);
4797 end if;
4799 else
4800 Resolve_Aggr_Expr (Expr, Component);
4801 end if;
4803 Next_Elmt (Component_Elmt);
4804 end loop;
4806 -- STEP 7: check for invalid components + check type in choice list
4808 Step_7 : declare
4809 Assoc : Node_Id;
4810 New_Assoc : Node_Id;
4812 Selectr : Node_Id;
4813 -- Selector name
4815 Typech : Entity_Id;
4816 -- Type of first component in choice list
4818 begin
4819 if Present (Component_Associations (N)) then
4820 Assoc := First (Component_Associations (N));
4821 else
4822 Assoc := Empty;
4823 end if;
4825 Verification : while Present (Assoc) loop
4826 Selectr := First (Choices (Assoc));
4827 Typech := Empty;
4829 if Nkind (Selectr) = N_Others_Choice then
4831 -- Ada 2005 (AI-287): others choice may have expression or box
4833 if No (Others_Etype) and then Others_Box = 0 then
4834 Error_Msg_N
4835 ("OTHERS must represent at least one component", Selectr);
4837 elsif Others_Box = 1 and then Warn_On_Redundant_Constructs then
4838 Error_Msg_N ("others choice is redundant?", Box_Node);
4839 Error_Msg_N
4840 ("\previous choices cover all components?", Box_Node);
4841 end if;
4843 exit Verification;
4844 end if;
4846 while Present (Selectr) loop
4847 New_Assoc := First (New_Assoc_List);
4848 while Present (New_Assoc) loop
4849 Component := First (Choices (New_Assoc));
4851 if Chars (Selectr) = Chars (Component) then
4852 if Style_Check then
4853 Check_Identifier (Selectr, Entity (Component));
4854 end if;
4856 exit;
4857 end if;
4859 Next (New_Assoc);
4860 end loop;
4862 -- If no association, this is not a legal component of the type
4863 -- in question, unless its association is provided with a box.
4865 if No (New_Assoc) then
4866 if Box_Present (Parent (Selectr)) then
4868 -- This may still be a bogus component with a box. Scan
4869 -- list of components to verify that a component with
4870 -- that name exists.
4872 declare
4873 C : Entity_Id;
4875 begin
4876 C := First_Component (Typ);
4877 while Present (C) loop
4878 if Chars (C) = Chars (Selectr) then
4880 -- If the context is an extension aggregate,
4881 -- the component must not be inherited from
4882 -- the ancestor part of the aggregate.
4884 if Nkind (N) /= N_Extension_Aggregate
4885 or else
4886 Scope (Original_Record_Component (C)) /=
4887 Etype (Ancestor_Part (N))
4888 then
4889 exit;
4890 end if;
4891 end if;
4893 Next_Component (C);
4894 end loop;
4896 if No (C) then
4897 Error_Msg_Node_2 := Typ;
4898 Error_Msg_N ("& is not a component of}", Selectr);
4899 end if;
4900 end;
4902 elsif Chars (Selectr) /= Name_uTag
4903 and then Chars (Selectr) /= Name_uParent
4904 then
4905 if not Has_Discriminants (Typ) then
4906 Error_Msg_Node_2 := Typ;
4907 Error_Msg_N ("& is not a component of}", Selectr);
4908 else
4909 Error_Msg_N
4910 ("& is not a component of the aggregate subtype",
4911 Selectr);
4912 end if;
4914 Check_Misspelled_Component (Components, Selectr);
4915 end if;
4917 elsif No (Typech) then
4918 Typech := Base_Type (Etype (Component));
4920 -- AI05-0199: In Ada 2012, several components of anonymous
4921 -- access types can appear in a choice list, as long as the
4922 -- designated types match.
4924 elsif Typech /= Base_Type (Etype (Component)) then
4925 if Ada_Version >= Ada_2012
4926 and then Ekind (Typech) = E_Anonymous_Access_Type
4927 and then
4928 Ekind (Etype (Component)) = E_Anonymous_Access_Type
4929 and then Base_Type (Designated_Type (Typech)) =
4930 Base_Type (Designated_Type (Etype (Component)))
4931 and then
4932 Subtypes_Statically_Match (Typech, (Etype (Component)))
4933 then
4934 null;
4936 elsif not Box_Present (Parent (Selectr)) then
4937 Error_Msg_N
4938 ("components in choice list must have same type",
4939 Selectr);
4940 end if;
4941 end if;
4943 Next (Selectr);
4944 end loop;
4946 Next (Assoc);
4947 end loop Verification;
4948 end Step_7;
4950 -- STEP 8: replace the original aggregate
4952 Step_8 : declare
4953 New_Aggregate : constant Node_Id := New_Copy (N);
4955 begin
4956 Set_Expressions (New_Aggregate, No_List);
4957 Set_Etype (New_Aggregate, Etype (N));
4958 Set_Component_Associations (New_Aggregate, New_Assoc_List);
4959 Set_Check_Actuals (New_Aggregate, Check_Actuals (N));
4961 Rewrite (N, New_Aggregate);
4962 end Step_8;
4964 -- Check the dimensions of the components in the record aggregate
4966 Analyze_Dimension_Extension_Or_Record_Aggregate (N);
4967 end Resolve_Record_Aggregate;
4969 -----------------------------
4970 -- Check_Can_Never_Be_Null --
4971 -----------------------------
4973 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
4974 Comp_Typ : Entity_Id;
4976 begin
4977 pragma Assert
4978 (Ada_Version >= Ada_2005
4979 and then Present (Expr)
4980 and then Known_Null (Expr));
4982 case Ekind (Typ) is
4983 when E_Array_Type =>
4984 Comp_Typ := Component_Type (Typ);
4986 when E_Component
4987 | E_Discriminant
4989 Comp_Typ := Etype (Typ);
4991 when others =>
4992 return;
4993 end case;
4995 if Can_Never_Be_Null (Comp_Typ) then
4997 -- Here we know we have a constraint error. Note that we do not use
4998 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
4999 -- seem the more natural approach. That's because in some cases the
5000 -- components are rewritten, and the replacement would be missed.
5001 -- We do not mark the whole aggregate as raising a constraint error,
5002 -- because the association may be a null array range.
5004 Error_Msg_N
5005 ("(Ada 2005) null not allowed in null-excluding component??", Expr);
5006 Error_Msg_N
5007 ("\Constraint_Error will be raised at run time??", Expr);
5009 Rewrite (Expr,
5010 Make_Raise_Constraint_Error
5011 (Sloc (Expr), Reason => CE_Access_Check_Failed));
5012 Set_Etype (Expr, Comp_Typ);
5013 Set_Analyzed (Expr);
5014 end if;
5015 end Check_Can_Never_Be_Null;
5017 ---------------------
5018 -- Sort_Case_Table --
5019 ---------------------
5021 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
5022 U : constant Int := Case_Table'Last;
5023 K : Int;
5024 J : Int;
5025 T : Case_Bounds;
5027 begin
5028 K := 1;
5029 while K < U loop
5030 T := Case_Table (K + 1);
5032 J := K + 1;
5033 while J > 1
5034 and then Expr_Value (Case_Table (J - 1).Lo) > Expr_Value (T.Lo)
5035 loop
5036 Case_Table (J) := Case_Table (J - 1);
5037 J := J - 1;
5038 end loop;
5040 Case_Table (J) := T;
5041 K := K + 1;
5042 end loop;
5043 end Sort_Case_Table;
5045 end Sem_Aggr;