PR libgomp/64635
[official-gcc.git] / gcc / ada / sem_aggr.adb
blobf14381b2ceab41e11a07933fbc87492abf3854b7
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-2014, 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 Sem; use Sem;
46 with Sem_Aux; use Sem_Aux;
47 with Sem_Cat; use Sem_Cat;
48 with Sem_Ch3; use Sem_Ch3;
49 with Sem_Ch8; use Sem_Ch8;
50 with Sem_Ch13; use Sem_Ch13;
51 with Sem_Dim; use Sem_Dim;
52 with Sem_Eval; use Sem_Eval;
53 with Sem_Res; use Sem_Res;
54 with Sem_Util; use Sem_Util;
55 with Sem_Type; use Sem_Type;
56 with Sem_Warn; use Sem_Warn;
57 with Sinfo; use Sinfo;
58 with Snames; use Snames;
59 with Stringt; use Stringt;
60 with Stand; use Stand;
61 with Style; use Style;
62 with Targparm; use Targparm;
63 with Tbuild; use Tbuild;
64 with Uintp; use Uintp;
66 package body Sem_Aggr is
68 type Case_Bounds is record
69 Lo : Node_Id;
70 -- Low bound of choice. Once we sort the Case_Table, then entries
71 -- will be in order of ascending Choice_Lo values.
73 Hi : Node_Id;
74 -- High Bound of choice. The sort does not pay any attention to the
75 -- high bound, so choices 1 .. 4 and 1 .. 5 could be in either order.
77 Highest : Uint;
78 -- If there are duplicates or missing entries, then in the sorted
79 -- table, this records the highest value among Choice_Hi values
80 -- seen so far, including this entry.
82 Choice : Node_Id;
83 -- The node of the choice
84 end record;
86 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
87 -- Table type used by Check_Case_Choices procedure. Entry zero is not
88 -- used (reserved for the sort). Real entries start at one.
90 -----------------------
91 -- Local Subprograms --
92 -----------------------
94 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
95 -- Sort the Case Table using the Lower Bound of each Choice as the key. A
96 -- simple insertion sort is used since the choices in a case statement will
97 -- usually be in near sorted order.
99 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id);
100 -- Ada 2005 (AI-231): Check bad usage of null for a component for which
101 -- null exclusion (NOT NULL) is specified. Typ can be an E_Array_Type for
102 -- the array case (the component type of the array will be used) or an
103 -- E_Component/E_Discriminant entity in the record case, in which case the
104 -- type of the component will be used for the test. If Typ is any other
105 -- kind of entity, the call is ignored. Expr is the component node in the
106 -- aggregate which is known to have a null value. A warning message will be
107 -- issued if the component is null excluding.
109 -- It would be better to pass the proper type for Typ ???
111 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id);
112 -- Check that Expr is either not limited or else is one of the cases of
113 -- expressions allowed for a limited component association (namely, an
114 -- aggregate, function call, or <> notation). Report error for violations.
115 -- Expression is also OK in an instance or inlining context, because we
116 -- have already pre-analyzed and it is known to be type correct.
118 procedure Check_Qualified_Aggregate (Level : Nat; Expr : Node_Id);
119 -- Given aggregate Expr, check that sub-aggregates of Expr that are nested
120 -- at Level are qualified. If Level = 0, this applies to Expr directly.
121 -- Only issue errors in formal verification mode.
123 function Is_Top_Level_Aggregate (Expr : Node_Id) return Boolean;
124 -- Return True of Expr is an aggregate not contained directly in another
125 -- aggregate.
127 ------------------------------------------------------
128 -- Subprograms used for RECORD AGGREGATE Processing --
129 ------------------------------------------------------
131 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
132 -- This procedure performs all the semantic checks required for record
133 -- aggregates. Note that for aggregates analysis and resolution go
134 -- hand in hand. Aggregate analysis has been delayed up to here and
135 -- it is done while resolving the aggregate.
137 -- N is the N_Aggregate node.
138 -- Typ is the record type for the aggregate resolution
140 -- While performing the semantic checks, this procedure builds a new
141 -- Component_Association_List where each record field appears alone in a
142 -- Component_Choice_List along with its corresponding expression. The
143 -- record fields in the Component_Association_List appear in the same order
144 -- in which they appear in the record type Typ.
146 -- Once this new Component_Association_List is built and all the semantic
147 -- checks performed, the original aggregate subtree is replaced with the
148 -- new named record aggregate just built. Note that subtree substitution is
149 -- performed with Rewrite so as to be able to retrieve the original
150 -- aggregate.
152 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
153 -- yields the aggregate format expected by Gigi. Typically, this kind of
154 -- tree manipulations are done in the expander. However, because the
155 -- semantic checks that need to be performed on record aggregates really go
156 -- hand in hand with the record aggregate normalization, the aggregate
157 -- subtree transformation is performed during resolution rather than
158 -- expansion. Had we decided otherwise we would have had to duplicate most
159 -- of the code in the expansion procedure Expand_Record_Aggregate. Note,
160 -- however, that all the expansion concerning aggregates for tagged records
161 -- is done in Expand_Record_Aggregate.
163 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
165 -- 1. Make sure that the record type against which the record aggregate
166 -- has to be resolved is not abstract. Furthermore if the type is a
167 -- null aggregate make sure the input aggregate N is also null.
169 -- 2. Verify that the structure of the aggregate is that of a record
170 -- aggregate. Specifically, look for component associations and ensure
171 -- that each choice list only has identifiers or the N_Others_Choice
172 -- node. Also make sure that if present, the N_Others_Choice occurs
173 -- last and by itself.
175 -- 3. If Typ contains discriminants, the values for each discriminant is
176 -- looked for. If the record type Typ has variants, we check that the
177 -- expressions corresponding to each discriminant ruling the (possibly
178 -- nested) variant parts of Typ, are static. This allows us to determine
179 -- the variant parts to which the rest of the aggregate must conform.
180 -- The names of discriminants with their values are saved in a new
181 -- association list, New_Assoc_List which is later augmented with the
182 -- names and values of the remaining components in the record type.
184 -- During this phase we also make sure that every discriminant is
185 -- assigned exactly one value. Note that when several values for a given
186 -- discriminant are found, semantic processing continues looking for
187 -- further errors. In this case it's the first discriminant value found
188 -- which we will be recorded.
190 -- IMPORTANT NOTE: For derived tagged types this procedure expects
191 -- First_Discriminant and Next_Discriminant to give the correct list
192 -- of discriminants, in the correct order.
194 -- 4. After all the discriminant values have been gathered, we can set the
195 -- Etype of the record aggregate. If Typ contains no discriminants this
196 -- is straightforward: the Etype of N is just Typ, otherwise a new
197 -- implicit constrained subtype of Typ is built to be the Etype of N.
199 -- 5. Gather the remaining record components according to the discriminant
200 -- values. This involves recursively traversing the record type
201 -- structure to see what variants are selected by the given discriminant
202 -- values. This processing is a little more convoluted if Typ is a
203 -- derived tagged types since we need to retrieve the record structure
204 -- of all the ancestors of Typ.
206 -- 6. After gathering the record components we look for their values in the
207 -- record aggregate and emit appropriate error messages should we not
208 -- find such values or should they be duplicated.
210 -- 7. We then make sure no illegal component names appear in the record
211 -- aggregate and make sure that the type of the record components
212 -- appearing in a same choice list is the same. Finally we ensure that
213 -- the others choice, if present, is used to provide the value of at
214 -- least a record component.
216 -- 8. The original aggregate node is replaced with the new named aggregate
217 -- built in steps 3 through 6, as explained earlier.
219 -- Given the complexity of record aggregate resolution, the primary goal of
220 -- this routine is clarity and simplicity rather than execution and storage
221 -- efficiency. If there are only positional components in the aggregate the
222 -- running time is linear. If there are associations the running time is
223 -- still linear as long as the order of the associations is not too far off
224 -- the order of the components in the record type. If this is not the case
225 -- the running time is at worst quadratic in the size of the association
226 -- list.
228 procedure Check_Misspelled_Component
229 (Elements : Elist_Id;
230 Component : Node_Id);
231 -- Give possible misspelling diagnostic if Component is likely to be a
232 -- misspelling of one of the components of the Assoc_List. This is called
233 -- by Resolve_Aggr_Expr after producing an invalid component error message.
235 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id);
236 -- An optimization: determine whether a discriminated subtype has a static
237 -- constraint, and contains array components whose length is also static,
238 -- either because they are constrained by the discriminant, or because the
239 -- original component bounds are static.
241 -----------------------------------------------------
242 -- Subprograms used for ARRAY AGGREGATE Processing --
243 -----------------------------------------------------
245 function Resolve_Array_Aggregate
246 (N : Node_Id;
247 Index : Node_Id;
248 Index_Constr : Node_Id;
249 Component_Typ : Entity_Id;
250 Others_Allowed : Boolean) return Boolean;
251 -- This procedure performs the semantic checks for an array aggregate.
252 -- True is returned if the aggregate resolution succeeds.
254 -- The procedure works by recursively checking each nested aggregate.
255 -- Specifically, after checking a sub-aggregate nested at the i-th level
256 -- we recursively check all the subaggregates at the i+1-st level (if any).
257 -- Note that for aggregates analysis and resolution go hand in hand.
258 -- Aggregate analysis has been delayed up to here and it is done while
259 -- resolving the aggregate.
261 -- N is the current N_Aggregate node to be checked.
263 -- Index is the index node corresponding to the array sub-aggregate that
264 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
265 -- corresponding index type (or subtype).
267 -- Index_Constr is the node giving the applicable index constraint if
268 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
269 -- contexts [...] that can be used to determine the bounds of the array
270 -- value specified by the aggregate". If Others_Allowed below is False
271 -- there is no applicable index constraint and this node is set to Index.
273 -- Component_Typ is the array component type.
275 -- Others_Allowed indicates whether an others choice is allowed
276 -- in the context where the top-level aggregate appeared.
278 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
280 -- 1. Make sure that the others choice, if present, is by itself and
281 -- appears last in the sub-aggregate. Check that we do not have
282 -- positional and named components in the array sub-aggregate (unless
283 -- the named association is an others choice). Finally if an others
284 -- choice is present, make sure it is allowed in the aggregate context.
286 -- 2. If the array sub-aggregate contains discrete_choices:
288 -- (A) Verify their validity. Specifically verify that:
290 -- (a) If a null range is present it must be the only possible
291 -- choice in the array aggregate.
293 -- (b) Ditto for a non static range.
295 -- (c) Ditto for a non static expression.
297 -- In addition this step analyzes and resolves each discrete_choice,
298 -- making sure that its type is the type of the corresponding Index.
299 -- If we are not at the lowest array aggregate level (in the case of
300 -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
301 -- recursively on each component expression. Otherwise, resolve the
302 -- bottom level component expressions against the expected component
303 -- type ONLY IF the component corresponds to a single discrete choice
304 -- which is not an others choice (to see why read the DELAYED
305 -- COMPONENT RESOLUTION below).
307 -- (B) Determine the bounds of the sub-aggregate and lowest and
308 -- highest choice values.
310 -- 3. For positional aggregates:
312 -- (A) Loop over the component expressions either recursively invoking
313 -- Resolve_Array_Aggregate on each of these for multi-dimensional
314 -- array aggregates or resolving the bottom level component
315 -- expressions against the expected component type.
317 -- (B) Determine the bounds of the positional sub-aggregates.
319 -- 4. Try to determine statically whether the evaluation of the array
320 -- sub-aggregate raises Constraint_Error. If yes emit proper
321 -- warnings. The precise checks are the following:
323 -- (A) Check that the index range defined by aggregate bounds is
324 -- compatible with corresponding index subtype.
325 -- We also check against the base type. In fact it could be that
326 -- Low/High bounds of the base type are static whereas those of
327 -- the index subtype are not. Thus if we can statically catch
328 -- a problem with respect to the base type we are guaranteed
329 -- that the same problem will arise with the index subtype
331 -- (B) If we are dealing with a named aggregate containing an others
332 -- choice and at least one discrete choice then make sure the range
333 -- specified by the discrete choices does not overflow the
334 -- aggregate bounds. We also check against the index type and base
335 -- type bounds for the same reasons given in (A).
337 -- (C) If we are dealing with a positional aggregate with an others
338 -- choice make sure the number of positional elements specified
339 -- does not overflow the aggregate bounds. We also check against
340 -- the index type and base type bounds as mentioned in (A).
342 -- Finally construct an N_Range node giving the sub-aggregate bounds.
343 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
344 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
345 -- to build the appropriate aggregate subtype. Aggregate_Bounds
346 -- information is needed during expansion.
348 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
349 -- expressions in an array aggregate may call Duplicate_Subexpr or some
350 -- other routine that inserts code just outside the outermost aggregate.
351 -- If the array aggregate contains discrete choices or an others choice,
352 -- this may be wrong. Consider for instance the following example.
354 -- type Rec is record
355 -- V : Integer := 0;
356 -- end record;
358 -- type Acc_Rec is access Rec;
359 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
361 -- Then the transformation of "new Rec" that occurs during resolution
362 -- entails the following code modifications
364 -- P7b : constant Acc_Rec := new Rec;
365 -- RecIP (P7b.all);
366 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
368 -- This code transformation is clearly wrong, since we need to call
369 -- "new Rec" for each of the 3 array elements. To avoid this problem we
370 -- delay resolution of the components of non positional array aggregates
371 -- to the expansion phase. As an optimization, if the discrete choice
372 -- specifies a single value we do not delay resolution.
374 function Array_Aggr_Subtype (N : Node_Id; Typ : Node_Id) return Entity_Id;
375 -- This routine returns the type or subtype of an array aggregate.
377 -- N is the array aggregate node whose type we return.
379 -- Typ is the context type in which N occurs.
381 -- This routine creates an implicit array subtype whose bounds are
382 -- those defined by the aggregate. When this routine is invoked
383 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
384 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
385 -- sub-aggregate bounds. When building the aggregate itype, this function
386 -- traverses the array aggregate N collecting such Aggregate_Bounds and
387 -- constructs the proper array aggregate itype.
389 -- Note that in the case of multidimensional aggregates each inner
390 -- sub-aggregate corresponding to a given array dimension, may provide a
391 -- different bounds. If it is possible to determine statically that
392 -- some sub-aggregates corresponding to the same index do not have the
393 -- same bounds, then a warning is emitted. If such check is not possible
394 -- statically (because some sub-aggregate bounds are dynamic expressions)
395 -- then this job is left to the expander. In all cases the particular
396 -- bounds that this function will chose for a given dimension is the first
397 -- N_Range node for a sub-aggregate corresponding to that dimension.
399 -- Note that the Raises_Constraint_Error flag of an array aggregate
400 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
401 -- is set in Resolve_Array_Aggregate but the aggregate is not
402 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
403 -- first construct the proper itype for the aggregate (Gigi needs
404 -- this). After constructing the proper itype we will eventually replace
405 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
406 -- Of course in cases such as:
408 -- type Arr is array (integer range <>) of Integer;
409 -- A : Arr := (positive range -1 .. 2 => 0);
411 -- The bounds of the aggregate itype are cooked up to look reasonable
412 -- (in this particular case the bounds will be 1 .. 2).
414 procedure Make_String_Into_Aggregate (N : Node_Id);
415 -- A string literal can appear in a context in which a one dimensional
416 -- array of characters is expected. This procedure simply rewrites the
417 -- string as an aggregate, prior to resolution.
419 ------------------------
420 -- Array_Aggr_Subtype --
421 ------------------------
423 function Array_Aggr_Subtype
424 (N : Node_Id;
425 Typ : Entity_Id) return Entity_Id
427 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
428 -- Number of aggregate index dimensions
430 Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
431 -- Constrained N_Range of each index dimension in our aggregate itype
433 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
434 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
435 -- Low and High bounds for each index dimension in our aggregate itype
437 Is_Fully_Positional : Boolean := True;
439 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
440 -- N is an array (sub-)aggregate. Dim is the dimension corresponding
441 -- to (sub-)aggregate N. This procedure collects and removes the side
442 -- effects of the constrained N_Range nodes corresponding to each index
443 -- dimension of our aggregate itype. These N_Range nodes are collected
444 -- in Aggr_Range above.
446 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
447 -- bounds of each index dimension. If, when collecting, two bounds
448 -- corresponding to the same dimension are static and found to differ,
449 -- then emit a warning, and mark N as raising Constraint_Error.
451 -------------------------
452 -- Collect_Aggr_Bounds --
453 -------------------------
455 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
456 This_Range : constant Node_Id := Aggregate_Bounds (N);
457 -- The aggregate range node of this specific sub-aggregate
459 This_Low : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
460 This_High : constant Node_Id := High_Bound (Aggregate_Bounds (N));
461 -- The aggregate bounds of this specific sub-aggregate
463 Assoc : Node_Id;
464 Expr : Node_Id;
466 begin
467 Remove_Side_Effects (This_Low, Variable_Ref => True);
468 Remove_Side_Effects (This_High, Variable_Ref => True);
470 -- Collect the first N_Range for a given dimension that you find.
471 -- For a given dimension they must be all equal anyway.
473 if No (Aggr_Range (Dim)) then
474 Aggr_Low (Dim) := This_Low;
475 Aggr_High (Dim) := This_High;
476 Aggr_Range (Dim) := This_Range;
478 else
479 if Compile_Time_Known_Value (This_Low) then
480 if not Compile_Time_Known_Value (Aggr_Low (Dim)) then
481 Aggr_Low (Dim) := This_Low;
483 elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
484 Set_Raises_Constraint_Error (N);
485 Error_Msg_Warn := SPARK_Mode /= On;
486 Error_Msg_N ("sub-aggregate low bound mismatch<<", N);
487 Error_Msg_N ("\Constraint_Error [<<", N);
488 end if;
489 end if;
491 if Compile_Time_Known_Value (This_High) then
492 if not Compile_Time_Known_Value (Aggr_High (Dim)) then
493 Aggr_High (Dim) := This_High;
495 elsif
496 Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
497 then
498 Set_Raises_Constraint_Error (N);
499 Error_Msg_Warn := SPARK_Mode /= On;
500 Error_Msg_N ("sub-aggregate high bound mismatch<<", N);
501 Error_Msg_N ("\Constraint_Error [<<", N);
502 end if;
503 end if;
504 end if;
506 if Dim < Aggr_Dimension then
508 -- Process positional components
510 if Present (Expressions (N)) then
511 Expr := First (Expressions (N));
512 while Present (Expr) loop
513 Collect_Aggr_Bounds (Expr, Dim + 1);
514 Next (Expr);
515 end loop;
516 end if;
518 -- Process component associations
520 if Present (Component_Associations (N)) then
521 Is_Fully_Positional := False;
523 Assoc := First (Component_Associations (N));
524 while Present (Assoc) loop
525 Expr := Expression (Assoc);
526 Collect_Aggr_Bounds (Expr, Dim + 1);
527 Next (Assoc);
528 end loop;
529 end if;
530 end if;
531 end Collect_Aggr_Bounds;
533 -- Array_Aggr_Subtype variables
535 Itype : Entity_Id;
536 -- The final itype of the overall aggregate
538 Index_Constraints : constant List_Id := New_List;
539 -- The list of index constraints of the aggregate itype
541 -- Start of processing for Array_Aggr_Subtype
543 begin
544 -- Make sure that the list of index constraints is properly attached to
545 -- the tree, and then collect the aggregate bounds.
547 Set_Parent (Index_Constraints, N);
548 Collect_Aggr_Bounds (N, 1);
550 -- Build the list of constrained indexes of our aggregate itype
552 for J in 1 .. Aggr_Dimension loop
553 Create_Index : declare
554 Index_Base : constant Entity_Id :=
555 Base_Type (Etype (Aggr_Range (J)));
556 Index_Typ : Entity_Id;
558 begin
559 -- Construct the Index subtype, and associate it with the range
560 -- construct that generates it.
562 Index_Typ :=
563 Create_Itype (Subtype_Kind (Ekind (Index_Base)), Aggr_Range (J));
565 Set_Etype (Index_Typ, Index_Base);
567 if Is_Character_Type (Index_Base) then
568 Set_Is_Character_Type (Index_Typ);
569 end if;
571 Set_Size_Info (Index_Typ, (Index_Base));
572 Set_RM_Size (Index_Typ, RM_Size (Index_Base));
573 Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base));
574 Set_Scalar_Range (Index_Typ, Aggr_Range (J));
576 if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then
577 Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ)));
578 end if;
580 Set_Etype (Aggr_Range (J), Index_Typ);
582 Append (Aggr_Range (J), To => Index_Constraints);
583 end Create_Index;
584 end loop;
586 -- Now build the Itype
588 Itype := Create_Itype (E_Array_Subtype, N);
590 Set_First_Rep_Item (Itype, First_Rep_Item (Typ));
591 Set_Convention (Itype, Convention (Typ));
592 Set_Depends_On_Private (Itype, Has_Private_Component (Typ));
593 Set_Etype (Itype, Base_Type (Typ));
594 Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ));
595 Set_Is_Aliased (Itype, Is_Aliased (Typ));
596 Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
598 Copy_Suppress_Status (Index_Check, Typ, Itype);
599 Copy_Suppress_Status (Length_Check, Typ, Itype);
601 Set_First_Index (Itype, First (Index_Constraints));
602 Set_Is_Constrained (Itype, True);
603 Set_Is_Internal (Itype, True);
605 -- A simple optimization: purely positional aggregates of static
606 -- components should be passed to gigi unexpanded whenever possible, and
607 -- regardless of the staticness of the bounds themselves. Subsequent
608 -- checks in exp_aggr verify that type is not packed, etc.
610 Set_Size_Known_At_Compile_Time (Itype,
611 Is_Fully_Positional
612 and then Comes_From_Source (N)
613 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
615 -- We always need a freeze node for a packed array subtype, so that we
616 -- can build the Packed_Array_Impl_Type corresponding to the subtype. If
617 -- expansion is disabled, the packed array subtype is not built, and we
618 -- must not generate a freeze node for the type, or else it will appear
619 -- incomplete to gigi.
621 if Is_Packed (Itype)
622 and then not In_Spec_Expression
623 and then Expander_Active
624 then
625 Freeze_Itype (Itype, N);
626 end if;
628 return Itype;
629 end Array_Aggr_Subtype;
631 --------------------------------
632 -- Check_Misspelled_Component --
633 --------------------------------
635 procedure Check_Misspelled_Component
636 (Elements : Elist_Id;
637 Component : Node_Id)
639 Max_Suggestions : constant := 2;
641 Nr_Of_Suggestions : Natural := 0;
642 Suggestion_1 : Entity_Id := Empty;
643 Suggestion_2 : Entity_Id := Empty;
644 Component_Elmt : Elmt_Id;
646 begin
647 -- All the components of List are matched against Component and a count
648 -- is maintained of possible misspellings. When at the end of the the
649 -- analysis there are one or two (not more) possible misspellings,
650 -- these misspellings will be suggested as possible correction.
652 Component_Elmt := First_Elmt (Elements);
653 while Nr_Of_Suggestions <= Max_Suggestions
654 and then Present (Component_Elmt)
655 loop
656 if Is_Bad_Spelling_Of
657 (Chars (Node (Component_Elmt)),
658 Chars (Component))
659 then
660 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
662 case Nr_Of_Suggestions is
663 when 1 => Suggestion_1 := Node (Component_Elmt);
664 when 2 => Suggestion_2 := Node (Component_Elmt);
665 when others => exit;
666 end case;
667 end if;
669 Next_Elmt (Component_Elmt);
670 end loop;
672 -- Report at most two suggestions
674 if Nr_Of_Suggestions = 1 then
675 Error_Msg_NE -- CODEFIX
676 ("\possible misspelling of&", Component, Suggestion_1);
678 elsif Nr_Of_Suggestions = 2 then
679 Error_Msg_Node_2 := Suggestion_2;
680 Error_Msg_NE -- CODEFIX
681 ("\possible misspelling of& or&", Component, Suggestion_1);
682 end if;
683 end Check_Misspelled_Component;
685 ----------------------------------------
686 -- Check_Expr_OK_In_Limited_Aggregate --
687 ----------------------------------------
689 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id) is
690 begin
691 if Is_Limited_Type (Etype (Expr))
692 and then Comes_From_Source (Expr)
693 then
694 if In_Instance_Body or else In_Inlined_Body then
695 null;
697 elsif not OK_For_Limited_Init (Etype (Expr), Expr) then
698 Error_Msg_N
699 ("initialization not allowed for limited types", Expr);
700 Explain_Limited_Type (Etype (Expr), Expr);
701 end if;
702 end if;
703 end Check_Expr_OK_In_Limited_Aggregate;
705 -------------------------------
706 -- Check_Qualified_Aggregate --
707 -------------------------------
709 procedure Check_Qualified_Aggregate (Level : Nat; Expr : Node_Id) is
710 Comp_Expr : Node_Id;
711 Comp_Assn : Node_Id;
713 begin
714 if Level = 0 then
715 if Nkind (Parent (Expr)) /= N_Qualified_Expression then
716 Check_SPARK_05_Restriction ("aggregate should be qualified", Expr);
717 end if;
719 else
720 Comp_Expr := First (Expressions (Expr));
721 while Present (Comp_Expr) loop
722 if Nkind (Comp_Expr) = N_Aggregate then
723 Check_Qualified_Aggregate (Level - 1, Comp_Expr);
724 end if;
726 Comp_Expr := Next (Comp_Expr);
727 end loop;
729 Comp_Assn := First (Component_Associations (Expr));
730 while Present (Comp_Assn) loop
731 Comp_Expr := Expression (Comp_Assn);
733 if Nkind (Comp_Expr) = N_Aggregate then
734 Check_Qualified_Aggregate (Level - 1, Comp_Expr);
735 end if;
737 Comp_Assn := Next (Comp_Assn);
738 end loop;
739 end if;
740 end Check_Qualified_Aggregate;
742 ----------------------------------------
743 -- Check_Static_Discriminated_Subtype --
744 ----------------------------------------
746 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id) is
747 Disc : constant Entity_Id := First_Discriminant (T);
748 Comp : Entity_Id;
749 Ind : Entity_Id;
751 begin
752 if Has_Record_Rep_Clause (T) then
753 return;
755 elsif Present (Next_Discriminant (Disc)) then
756 return;
758 elsif Nkind (V) /= N_Integer_Literal then
759 return;
760 end if;
762 Comp := First_Component (T);
763 while Present (Comp) loop
764 if Is_Scalar_Type (Etype (Comp)) then
765 null;
767 elsif Is_Private_Type (Etype (Comp))
768 and then Present (Full_View (Etype (Comp)))
769 and then Is_Scalar_Type (Full_View (Etype (Comp)))
770 then
771 null;
773 elsif Is_Array_Type (Etype (Comp)) then
774 if Is_Bit_Packed_Array (Etype (Comp)) then
775 return;
776 end if;
778 Ind := First_Index (Etype (Comp));
779 while Present (Ind) loop
780 if Nkind (Ind) /= N_Range
781 or else Nkind (Low_Bound (Ind)) /= N_Integer_Literal
782 or else Nkind (High_Bound (Ind)) /= N_Integer_Literal
783 then
784 return;
785 end if;
787 Next_Index (Ind);
788 end loop;
790 else
791 return;
792 end if;
794 Next_Component (Comp);
795 end loop;
797 -- On exit, all components have statically known sizes
799 Set_Size_Known_At_Compile_Time (T);
800 end Check_Static_Discriminated_Subtype;
802 -------------------------
803 -- Is_Others_Aggregate --
804 -------------------------
806 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean is
807 begin
808 return No (Expressions (Aggr))
809 and then
810 Nkind (First (Choices (First (Component_Associations (Aggr)))))
811 = N_Others_Choice;
812 end Is_Others_Aggregate;
814 ----------------------------
815 -- Is_Top_Level_Aggregate --
816 ----------------------------
818 function Is_Top_Level_Aggregate (Expr : Node_Id) return Boolean is
819 begin
820 return Nkind (Parent (Expr)) /= N_Aggregate
821 and then (Nkind (Parent (Expr)) /= N_Component_Association
822 or else Nkind (Parent (Parent (Expr))) /= N_Aggregate);
823 end Is_Top_Level_Aggregate;
825 --------------------------------
826 -- Make_String_Into_Aggregate --
827 --------------------------------
829 procedure Make_String_Into_Aggregate (N : Node_Id) is
830 Exprs : constant List_Id := New_List;
831 Loc : constant Source_Ptr := Sloc (N);
832 Str : constant String_Id := Strval (N);
833 Strlen : constant Nat := String_Length (Str);
834 C : Char_Code;
835 C_Node : Node_Id;
836 New_N : Node_Id;
837 P : Source_Ptr;
839 begin
840 P := Loc + 1;
841 for J in 1 .. Strlen loop
842 C := Get_String_Char (Str, J);
843 Set_Character_Literal_Name (C);
845 C_Node :=
846 Make_Character_Literal (P,
847 Chars => Name_Find,
848 Char_Literal_Value => UI_From_CC (C));
849 Set_Etype (C_Node, Any_Character);
850 Append_To (Exprs, C_Node);
852 P := P + 1;
853 -- Something special for wide strings???
854 end loop;
856 New_N := Make_Aggregate (Loc, Expressions => Exprs);
857 Set_Analyzed (New_N);
858 Set_Etype (New_N, Any_Composite);
860 Rewrite (N, New_N);
861 end Make_String_Into_Aggregate;
863 -----------------------
864 -- Resolve_Aggregate --
865 -----------------------
867 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
868 Loc : constant Source_Ptr := Sloc (N);
869 Pkind : constant Node_Kind := Nkind (Parent (N));
871 Aggr_Subtyp : Entity_Id;
872 -- The actual aggregate subtype. This is not necessarily the same as Typ
873 -- which is the subtype of the context in which the aggregate was found.
875 begin
876 -- Ignore junk empty aggregate resulting from parser error
878 if No (Expressions (N))
879 and then No (Component_Associations (N))
880 and then not Null_Record_Present (N)
881 then
882 return;
883 end if;
885 -- If the aggregate has box-initialized components, its type must be
886 -- frozen so that initialization procedures can properly be called
887 -- in the resolution that follows. The replacement of boxes with
888 -- initialization calls is properly an expansion activity but it must
889 -- be done during resolution.
891 if Expander_Active
892 and then Present (Component_Associations (N))
893 then
894 declare
895 Comp : Node_Id;
897 begin
898 Comp := First (Component_Associations (N));
899 while Present (Comp) loop
900 if Box_Present (Comp) then
901 Insert_Actions (N, Freeze_Entity (Typ, N));
902 exit;
903 end if;
905 Next (Comp);
906 end loop;
907 end;
908 end if;
910 -- An unqualified aggregate is restricted in SPARK to:
912 -- An aggregate item inside an aggregate for a multi-dimensional array
914 -- An expression being assigned to an unconstrained array, but only if
915 -- the aggregate specifies a value for OTHERS only.
917 if Nkind (Parent (N)) = N_Qualified_Expression then
918 if Is_Array_Type (Typ) then
919 Check_Qualified_Aggregate (Number_Dimensions (Typ), N);
920 else
921 Check_Qualified_Aggregate (1, N);
922 end if;
923 else
924 if Is_Array_Type (Typ)
925 and then Nkind (Parent (N)) = N_Assignment_Statement
926 and then not Is_Constrained (Etype (Name (Parent (N))))
927 then
928 if not Is_Others_Aggregate (N) then
929 Check_SPARK_05_Restriction
930 ("array aggregate should have only OTHERS", N);
931 end if;
933 elsif Is_Top_Level_Aggregate (N) then
934 Check_SPARK_05_Restriction ("aggregate should be qualified", N);
936 -- The legality of this unqualified aggregate is checked by calling
937 -- Check_Qualified_Aggregate from one of its enclosing aggregate,
938 -- unless one of these already causes an error to be issued.
940 else
941 null;
942 end if;
943 end if;
945 -- Check for aggregates not allowed in configurable run-time mode.
946 -- We allow all cases of aggregates that do not come from source, since
947 -- these are all assumed to be small (e.g. bounds of a string literal).
948 -- We also allow aggregates of types we know to be small.
950 if not Support_Aggregates_On_Target
951 and then Comes_From_Source (N)
952 and then (not Known_Static_Esize (Typ) or else Esize (Typ) > 64)
953 then
954 Error_Msg_CRT ("aggregate", N);
955 end if;
957 -- Ada 2005 (AI-287): Limited aggregates allowed
959 -- In an instance, ignore aggregate subcomponents tnat may be limited,
960 -- because they originate in view conflicts. If the original aggregate
961 -- is legal and the actuals are legal, the aggregate itself is legal.
963 if Is_Limited_Type (Typ)
964 and then Ada_Version < Ada_2005
965 and then not In_Instance
966 then
967 Error_Msg_N ("aggregate type cannot be limited", N);
968 Explain_Limited_Type (Typ, N);
970 elsif Is_Class_Wide_Type (Typ) then
971 Error_Msg_N ("type of aggregate cannot be class-wide", N);
973 elsif Typ = Any_String
974 or else Typ = Any_Composite
975 then
976 Error_Msg_N ("no unique type for aggregate", N);
977 Set_Etype (N, Any_Composite);
979 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
980 Error_Msg_N ("null record forbidden in array aggregate", N);
982 elsif Is_Record_Type (Typ) then
983 Resolve_Record_Aggregate (N, Typ);
985 elsif Is_Array_Type (Typ) then
987 -- First a special test, for the case of a positional aggregate
988 -- of characters which can be replaced by a string literal.
990 -- Do not perform this transformation if this was a string literal to
991 -- start with, whose components needed constraint checks, or if the
992 -- component type is non-static, because it will require those checks
993 -- and be transformed back into an aggregate.
995 if Number_Dimensions (Typ) = 1
996 and then Is_Standard_Character_Type (Component_Type (Typ))
997 and then No (Component_Associations (N))
998 and then not Is_Limited_Composite (Typ)
999 and then not Is_Private_Composite (Typ)
1000 and then not Is_Bit_Packed_Array (Typ)
1001 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
1002 and then Is_OK_Static_Subtype (Component_Type (Typ))
1003 then
1004 declare
1005 Expr : Node_Id;
1007 begin
1008 Expr := First (Expressions (N));
1009 while Present (Expr) loop
1010 exit when Nkind (Expr) /= N_Character_Literal;
1011 Next (Expr);
1012 end loop;
1014 if No (Expr) then
1015 Start_String;
1017 Expr := First (Expressions (N));
1018 while Present (Expr) loop
1019 Store_String_Char (UI_To_CC (Char_Literal_Value (Expr)));
1020 Next (Expr);
1021 end loop;
1023 Rewrite (N, Make_String_Literal (Loc, End_String));
1025 Analyze_And_Resolve (N, Typ);
1026 return;
1027 end if;
1028 end;
1029 end if;
1031 -- Here if we have a real aggregate to deal with
1033 Array_Aggregate : declare
1034 Aggr_Resolved : Boolean;
1036 Aggr_Typ : constant Entity_Id := Etype (Typ);
1037 -- This is the unconstrained array type, which is the type against
1038 -- which the aggregate is to be resolved. Typ itself is the array
1039 -- type of the context which may not be the same subtype as the
1040 -- subtype for the final aggregate.
1042 begin
1043 -- In the following we determine whether an OTHERS choice is
1044 -- allowed inside the array aggregate. The test checks the context
1045 -- in which the array aggregate occurs. If the context does not
1046 -- permit it, or the aggregate type is unconstrained, an OTHERS
1047 -- choice is not allowed (except that it is always allowed on the
1048 -- right-hand side of an assignment statement; in this case the
1049 -- constrainedness of the type doesn't matter).
1051 -- If expansion is disabled (generic context, or semantics-only
1052 -- mode) actual subtypes cannot be constructed, and the type of an
1053 -- object may be its unconstrained nominal type. However, if the
1054 -- context is an assignment, we assume that OTHERS is allowed,
1055 -- because the target of the assignment will have a constrained
1056 -- subtype when fully compiled.
1058 -- Note that there is no node for Explicit_Actual_Parameter.
1059 -- To test for this context we therefore have to test for node
1060 -- N_Parameter_Association which itself appears only if there is a
1061 -- formal parameter. Consequently we also need to test for
1062 -- N_Procedure_Call_Statement or N_Function_Call.
1064 -- The context may be an N_Reference node, created by expansion.
1065 -- Legality of the others clause was established in the source,
1066 -- so the context is legal.
1068 Set_Etype (N, Aggr_Typ); -- May be overridden later on
1070 if Pkind = N_Assignment_Statement
1071 or else (Is_Constrained (Typ)
1072 and then
1073 (Pkind = N_Parameter_Association or else
1074 Pkind = N_Function_Call or else
1075 Pkind = N_Procedure_Call_Statement or else
1076 Pkind = N_Generic_Association or else
1077 Pkind = N_Formal_Object_Declaration or else
1078 Pkind = N_Simple_Return_Statement or else
1079 Pkind = N_Object_Declaration or else
1080 Pkind = N_Component_Declaration or else
1081 Pkind = N_Parameter_Specification or else
1082 Pkind = N_Qualified_Expression or else
1083 Pkind = N_Reference or else
1084 Pkind = N_Aggregate or else
1085 Pkind = N_Extension_Aggregate or else
1086 Pkind = N_Component_Association))
1087 then
1088 Aggr_Resolved :=
1089 Resolve_Array_Aggregate
1091 Index => First_Index (Aggr_Typ),
1092 Index_Constr => First_Index (Typ),
1093 Component_Typ => Component_Type (Typ),
1094 Others_Allowed => True);
1096 elsif not Expander_Active
1097 and then Pkind = N_Assignment_Statement
1098 then
1099 Aggr_Resolved :=
1100 Resolve_Array_Aggregate
1102 Index => First_Index (Aggr_Typ),
1103 Index_Constr => First_Index (Typ),
1104 Component_Typ => Component_Type (Typ),
1105 Others_Allowed => True);
1107 else
1108 Aggr_Resolved :=
1109 Resolve_Array_Aggregate
1111 Index => First_Index (Aggr_Typ),
1112 Index_Constr => First_Index (Aggr_Typ),
1113 Component_Typ => Component_Type (Typ),
1114 Others_Allowed => False);
1115 end if;
1117 if not Aggr_Resolved then
1119 -- A parenthesized expression may have been intended as an
1120 -- aggregate, leading to a type error when analyzing the
1121 -- component. This can also happen for a nested component
1122 -- (see Analyze_Aggr_Expr).
1124 if Paren_Count (N) > 0 then
1125 Error_Msg_N
1126 ("positional aggregate cannot have one component", N);
1127 end if;
1129 Aggr_Subtyp := Any_Composite;
1131 else
1132 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1133 end if;
1135 Set_Etype (N, Aggr_Subtyp);
1136 end Array_Aggregate;
1138 elsif Is_Private_Type (Typ)
1139 and then Present (Full_View (Typ))
1140 and then (In_Inlined_Body or In_Instance_Body)
1141 and then Is_Composite_Type (Full_View (Typ))
1142 then
1143 Resolve (N, Full_View (Typ));
1145 else
1146 Error_Msg_N ("illegal context for aggregate", N);
1147 end if;
1149 -- If we can determine statically that the evaluation of the aggregate
1150 -- raises Constraint_Error, then replace the aggregate with an
1151 -- N_Raise_Constraint_Error node, but set the Etype to the right
1152 -- aggregate subtype. Gigi needs this.
1154 if Raises_Constraint_Error (N) then
1155 Aggr_Subtyp := Etype (N);
1156 Rewrite (N,
1157 Make_Raise_Constraint_Error (Loc, Reason => CE_Range_Check_Failed));
1158 Set_Raises_Constraint_Error (N);
1159 Set_Etype (N, Aggr_Subtyp);
1160 Set_Analyzed (N);
1161 end if;
1163 Check_Function_Writable_Actuals (N);
1164 end Resolve_Aggregate;
1166 -----------------------------
1167 -- Resolve_Array_Aggregate --
1168 -----------------------------
1170 function Resolve_Array_Aggregate
1171 (N : Node_Id;
1172 Index : Node_Id;
1173 Index_Constr : Node_Id;
1174 Component_Typ : Entity_Id;
1175 Others_Allowed : Boolean) return Boolean
1177 Loc : constant Source_Ptr := Sloc (N);
1179 Failure : constant Boolean := False;
1180 Success : constant Boolean := True;
1182 Index_Typ : constant Entity_Id := Etype (Index);
1183 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1184 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1185 -- The type of the index corresponding to the array sub-aggregate along
1186 -- with its low and upper bounds.
1188 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1189 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1190 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1191 -- Ditto for the base type
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 ---------
1235 -- Add --
1236 ---------
1238 function Add (Val : Uint; To : Node_Id) return Node_Id is
1239 Expr_Pos : Node_Id;
1240 Expr : Node_Id;
1241 To_Pos : Node_Id;
1243 begin
1244 if Raises_Constraint_Error (To) then
1245 return To;
1246 end if;
1248 -- First test if we can do constant folding
1250 if Compile_Time_Known_Value (To)
1251 or else Nkind (To) = N_Integer_Literal
1252 then
1253 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1254 Set_Is_Static_Expression (Expr_Pos);
1255 Set_Etype (Expr_Pos, Etype (To));
1256 Set_Analyzed (Expr_Pos, Analyzed (To));
1258 if not Is_Enumeration_Type (Index_Typ) then
1259 Expr := Expr_Pos;
1261 -- If we are dealing with enumeration return
1262 -- Index_Typ'Val (Expr_Pos)
1264 else
1265 Expr :=
1266 Make_Attribute_Reference
1267 (Loc,
1268 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1269 Attribute_Name => Name_Val,
1270 Expressions => New_List (Expr_Pos));
1271 end if;
1273 return Expr;
1274 end if;
1276 -- If we are here no constant folding possible
1278 if not Is_Enumeration_Type (Index_Base) then
1279 Expr :=
1280 Make_Op_Add (Loc,
1281 Left_Opnd => Duplicate_Subexpr (To),
1282 Right_Opnd => Make_Integer_Literal (Loc, Val));
1284 -- If we are dealing with enumeration return
1285 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1287 else
1288 To_Pos :=
1289 Make_Attribute_Reference
1290 (Loc,
1291 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1292 Attribute_Name => Name_Pos,
1293 Expressions => New_List (Duplicate_Subexpr (To)));
1295 Expr_Pos :=
1296 Make_Op_Add (Loc,
1297 Left_Opnd => To_Pos,
1298 Right_Opnd => Make_Integer_Literal (Loc, Val));
1300 Expr :=
1301 Make_Attribute_Reference
1302 (Loc,
1303 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1304 Attribute_Name => Name_Val,
1305 Expressions => New_List (Expr_Pos));
1307 -- If the index type has a non standard representation, the
1308 -- attributes 'Val and 'Pos expand into function calls and the
1309 -- resulting expression is considered non-safe for reevaluation
1310 -- by the backend. Relocate it into a constant temporary in order
1311 -- to make it safe for reevaluation.
1313 if Has_Non_Standard_Rep (Etype (N)) then
1314 declare
1315 Def_Id : Entity_Id;
1317 begin
1318 Def_Id := Make_Temporary (Loc, 'R', Expr);
1319 Set_Etype (Def_Id, Index_Typ);
1320 Insert_Action (N,
1321 Make_Object_Declaration (Loc,
1322 Defining_Identifier => Def_Id,
1323 Object_Definition =>
1324 New_Occurrence_Of (Index_Typ, Loc),
1325 Constant_Present => True,
1326 Expression => Relocate_Node (Expr)));
1328 Expr := New_Occurrence_Of (Def_Id, Loc);
1329 end;
1330 end if;
1331 end if;
1333 return Expr;
1334 end Add;
1336 -----------------
1337 -- Check_Bound --
1338 -----------------
1340 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1341 Val_BH : Uint;
1342 Val_AH : Uint;
1344 OK_BH : Boolean;
1345 OK_AH : Boolean;
1347 begin
1348 Get (Value => Val_BH, From => BH, OK => OK_BH);
1349 Get (Value => Val_AH, From => AH, OK => OK_AH);
1351 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1352 Set_Raises_Constraint_Error (N);
1353 Error_Msg_Warn := SPARK_Mode /= On;
1354 Error_Msg_N ("upper bound out of range<<", AH);
1355 Error_Msg_N ("\Constraint_Error [<<", AH);
1357 -- You need to set AH to BH or else in the case of enumerations
1358 -- indexes we will not be able to resolve the aggregate bounds.
1360 AH := Duplicate_Subexpr (BH);
1361 end if;
1362 end Check_Bound;
1364 ------------------
1365 -- Check_Bounds --
1366 ------------------
1368 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1369 Val_L : Uint;
1370 Val_H : Uint;
1371 Val_AL : Uint;
1372 Val_AH : Uint;
1374 OK_L : Boolean;
1375 OK_H : Boolean;
1377 OK_AL : Boolean;
1378 OK_AH : Boolean;
1379 pragma Warnings (Off, OK_AL);
1380 pragma Warnings (Off, OK_AH);
1382 begin
1383 if Raises_Constraint_Error (N)
1384 or else Dynamic_Or_Null_Range (AL, AH)
1385 then
1386 return;
1387 end if;
1389 Get (Value => Val_L, From => L, OK => OK_L);
1390 Get (Value => Val_H, From => H, OK => OK_H);
1392 Get (Value => Val_AL, From => AL, OK => OK_AL);
1393 Get (Value => Val_AH, From => AH, OK => OK_AH);
1395 if OK_L and then Val_L > Val_AL then
1396 Set_Raises_Constraint_Error (N);
1397 Error_Msg_Warn := SPARK_Mode /= On;
1398 Error_Msg_N ("lower bound of aggregate out of range<<", N);
1399 Error_Msg_N ("\Constraint_Error [<<", N);
1400 end if;
1402 if OK_H and then Val_H < Val_AH then
1403 Set_Raises_Constraint_Error (N);
1404 Error_Msg_Warn := SPARK_Mode /= On;
1405 Error_Msg_N ("upper bound of aggregate out of range<<", N);
1406 Error_Msg_N ("\Constraint_Error [<<", N);
1407 end if;
1408 end Check_Bounds;
1410 ------------------
1411 -- Check_Length --
1412 ------------------
1414 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1415 Val_L : Uint;
1416 Val_H : Uint;
1418 OK_L : Boolean;
1419 OK_H : Boolean;
1421 Range_Len : Uint;
1423 begin
1424 if Raises_Constraint_Error (N) then
1425 return;
1426 end if;
1428 Get (Value => Val_L, From => L, OK => OK_L);
1429 Get (Value => Val_H, From => H, OK => OK_H);
1431 if not OK_L or else not OK_H then
1432 return;
1433 end if;
1435 -- If null range length is zero
1437 if Val_L > Val_H then
1438 Range_Len := Uint_0;
1439 else
1440 Range_Len := Val_H - Val_L + 1;
1441 end if;
1443 if Range_Len < Len then
1444 Set_Raises_Constraint_Error (N);
1445 Error_Msg_Warn := SPARK_Mode /= On;
1446 Error_Msg_N ("too many elements<<", N);
1447 Error_Msg_N ("\Constraint_Error [<<", N);
1448 end if;
1449 end Check_Length;
1451 ---------------------------
1452 -- Dynamic_Or_Null_Range --
1453 ---------------------------
1455 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1456 Val_L : Uint;
1457 Val_H : Uint;
1459 OK_L : Boolean;
1460 OK_H : Boolean;
1462 begin
1463 Get (Value => Val_L, From => L, OK => OK_L);
1464 Get (Value => Val_H, From => H, OK => OK_H);
1466 return not OK_L or else not OK_H
1467 or else not Is_OK_Static_Expression (L)
1468 or else not Is_OK_Static_Expression (H)
1469 or else Val_L > Val_H;
1470 end Dynamic_Or_Null_Range;
1472 ---------
1473 -- Get --
1474 ---------
1476 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1477 begin
1478 OK := True;
1480 if Compile_Time_Known_Value (From) then
1481 Value := Expr_Value (From);
1483 -- If expression From is something like Some_Type'Val (10) then
1484 -- Value = 10.
1486 elsif Nkind (From) = N_Attribute_Reference
1487 and then Attribute_Name (From) = Name_Val
1488 and then Compile_Time_Known_Value (First (Expressions (From)))
1489 then
1490 Value := Expr_Value (First (Expressions (From)));
1492 else
1493 Value := Uint_0;
1494 OK := False;
1495 end if;
1496 end Get;
1498 -----------------------
1499 -- Resolve_Aggr_Expr --
1500 -----------------------
1502 function Resolve_Aggr_Expr
1503 (Expr : Node_Id;
1504 Single_Elmt : Boolean) return Boolean
1506 Nxt_Ind : constant Node_Id := Next_Index (Index);
1507 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
1508 -- Index is the current index corresponding to the expression
1510 Resolution_OK : Boolean := True;
1511 -- Set to False if resolution of the expression failed
1513 begin
1514 -- Defend against previous errors
1516 if Nkind (Expr) = N_Error
1517 or else Error_Posted (Expr)
1518 then
1519 return True;
1520 end if;
1522 -- If the array type against which we are resolving the aggregate
1523 -- has several dimensions, the expressions nested inside the
1524 -- aggregate must be further aggregates (or strings).
1526 if Present (Nxt_Ind) then
1527 if Nkind (Expr) /= N_Aggregate then
1529 -- A string literal can appear where a one-dimensional array
1530 -- of characters is expected. If the literal looks like an
1531 -- operator, it is still an operator symbol, which will be
1532 -- transformed into a string when analyzed.
1534 if Is_Character_Type (Component_Typ)
1535 and then No (Next_Index (Nxt_Ind))
1536 and then Nkind_In (Expr, N_String_Literal, N_Operator_Symbol)
1537 then
1538 -- A string literal used in a multidimensional array
1539 -- aggregate in place of the final one-dimensional
1540 -- aggregate must not be enclosed in parentheses.
1542 if Paren_Count (Expr) /= 0 then
1543 Error_Msg_N ("no parenthesis allowed here", Expr);
1544 end if;
1546 Make_String_Into_Aggregate (Expr);
1548 else
1549 Error_Msg_N ("nested array aggregate expected", Expr);
1551 -- If the expression is parenthesized, this may be
1552 -- a missing component association for a 1-aggregate.
1554 if Paren_Count (Expr) > 0 then
1555 Error_Msg_N
1556 ("\if single-component aggregate is intended,"
1557 & " write e.g. (1 ='> ...)", Expr);
1558 end if;
1560 return Failure;
1561 end if;
1562 end if;
1564 -- If it's "... => <>", nothing to resolve
1566 if Nkind (Expr) = N_Component_Association then
1567 pragma Assert (Box_Present (Expr));
1568 return Success;
1569 end if;
1571 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
1572 -- Required to check the null-exclusion attribute (if present).
1573 -- This value may be overridden later on.
1575 Set_Etype (Expr, Etype (N));
1577 Resolution_OK := Resolve_Array_Aggregate
1578 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1580 else
1581 -- If it's "... => <>", nothing to resolve
1583 if Nkind (Expr) = N_Component_Association then
1584 pragma Assert (Box_Present (Expr));
1585 return Success;
1586 end if;
1588 -- Do not resolve the expressions of discrete or others choices
1589 -- unless the expression covers a single component, or the
1590 -- expander is inactive.
1592 -- In SPARK mode, expressions that can perform side-effects will
1593 -- be recognized by the gnat2why back-end, and the whole
1594 -- subprogram will be ignored. So semantic analysis can be
1595 -- performed safely.
1597 if Single_Elmt
1598 or else not Expander_Active
1599 or else In_Spec_Expression
1600 then
1601 Analyze_And_Resolve (Expr, Component_Typ);
1602 Check_Expr_OK_In_Limited_Aggregate (Expr);
1603 Check_Non_Static_Context (Expr);
1604 Aggregate_Constraint_Checks (Expr, Component_Typ);
1605 Check_Unset_Reference (Expr);
1606 end if;
1607 end if;
1609 -- If an aggregate component has a type with predicates, an explicit
1610 -- predicate check must be applied, as for an assignment statement,
1611 -- because the aggegate might not be expanded into individual
1612 -- component assignments.
1614 if Present (Predicate_Function (Component_Typ)) then
1615 Apply_Predicate_Check (Expr, Component_Typ);
1616 end if;
1618 if Raises_Constraint_Error (Expr)
1619 and then Nkind (Parent (Expr)) /= N_Component_Association
1620 then
1621 Set_Raises_Constraint_Error (N);
1622 end if;
1624 -- If the expression has been marked as requiring a range check,
1625 -- then generate it here. It's a bit odd to be generating such
1626 -- checks in the analyzer, but harmless since Generate_Range_Check
1627 -- does nothing (other than making sure Do_Range_Check is set) if
1628 -- the expander is not active.
1630 if Do_Range_Check (Expr) then
1631 Generate_Range_Check (Expr, Component_Typ, CE_Range_Check_Failed);
1632 end if;
1634 return Resolution_OK;
1635 end Resolve_Aggr_Expr;
1637 -- Variables local to Resolve_Array_Aggregate
1639 Assoc : Node_Id;
1640 Choice : Node_Id;
1641 Expr : Node_Id;
1643 Discard : Node_Id;
1644 pragma Warnings (Off, Discard);
1646 Delete_Choice : Boolean;
1647 -- Used when replacing a subtype choice with predicate by a list
1649 Aggr_Low : Node_Id := Empty;
1650 Aggr_High : Node_Id := Empty;
1651 -- The actual low and high bounds of this sub-aggregate
1653 Choices_Low : Node_Id := Empty;
1654 Choices_High : Node_Id := Empty;
1655 -- The lowest and highest discrete choices values for a named aggregate
1657 Nb_Elements : Uint := Uint_0;
1658 -- The number of elements in a positional aggregate
1660 Others_Present : Boolean := False;
1662 Nb_Choices : Nat := 0;
1663 -- Contains the overall number of named choices in this sub-aggregate
1665 Nb_Discrete_Choices : Nat := 0;
1666 -- The overall number of discrete choices (not counting others choice)
1668 Case_Table_Size : Nat;
1669 -- Contains the size of the case table needed to sort aggregate choices
1671 -- Start of processing for Resolve_Array_Aggregate
1673 begin
1674 -- Ignore junk empty aggregate resulting from parser error
1676 if No (Expressions (N))
1677 and then No (Component_Associations (N))
1678 and then not Null_Record_Present (N)
1679 then
1680 return False;
1681 end if;
1683 -- STEP 1: make sure the aggregate is correctly formatted
1685 if Present (Component_Associations (N)) then
1686 Assoc := First (Component_Associations (N));
1687 while Present (Assoc) loop
1688 Choice := First (Choices (Assoc));
1689 Delete_Choice := False;
1691 while Present (Choice) loop
1692 if Nkind (Choice) = N_Others_Choice then
1693 Others_Present := True;
1695 if Choice /= First (Choices (Assoc))
1696 or else Present (Next (Choice))
1697 then
1698 Error_Msg_N
1699 ("OTHERS must appear alone in a choice list", Choice);
1700 return Failure;
1701 end if;
1703 if Present (Next (Assoc)) then
1704 Error_Msg_N
1705 ("OTHERS must appear last in an aggregate", Choice);
1706 return Failure;
1707 end if;
1709 if Ada_Version = Ada_83
1710 and then Assoc /= First (Component_Associations (N))
1711 and then Nkind_In (Parent (N), N_Assignment_Statement,
1712 N_Object_Declaration)
1713 then
1714 Error_Msg_N
1715 ("(Ada 83) illegal context for OTHERS choice", N);
1716 end if;
1718 elsif Is_Entity_Name (Choice) then
1719 Analyze (Choice);
1721 declare
1722 E : constant Entity_Id := Entity (Choice);
1723 New_Cs : List_Id;
1724 P : Node_Id;
1725 C : Node_Id;
1727 begin
1728 if Is_Type (E) and then Has_Predicates (E) then
1729 Freeze_Before (N, E);
1731 if Has_Dynamic_Predicate_Aspect (E) then
1732 Error_Msg_NE
1733 ("subtype& has dynamic predicate, not allowed "
1734 & "in aggregate choice", Choice, E);
1736 elsif not Is_OK_Static_Subtype (E) then
1737 Error_Msg_NE
1738 ("non-static subtype& has predicate, not allowed "
1739 & "in aggregate choice", Choice, E);
1740 end if;
1742 -- If the subtype has a static predicate, replace the
1743 -- original choice with the list of individual values
1744 -- covered by the predicate.
1746 if Present (Static_Discrete_Predicate (E)) then
1747 Delete_Choice := True;
1749 New_Cs := New_List;
1750 P := First (Static_Discrete_Predicate (E));
1751 while Present (P) loop
1752 C := New_Copy (P);
1753 Set_Sloc (C, Sloc (Choice));
1754 Append_To (New_Cs, C);
1755 Next (P);
1756 end loop;
1758 Insert_List_After (Choice, New_Cs);
1759 end if;
1760 end if;
1761 end;
1762 end if;
1764 Nb_Choices := Nb_Choices + 1;
1766 declare
1767 C : constant Node_Id := Choice;
1769 begin
1770 Next (Choice);
1772 if Delete_Choice then
1773 Remove (C);
1774 Nb_Choices := Nb_Choices - 1;
1775 Delete_Choice := False;
1776 end if;
1777 end;
1778 end loop;
1780 Next (Assoc);
1781 end loop;
1782 end if;
1784 -- At this point we know that the others choice, if present, is by
1785 -- itself and appears last in the aggregate. Check if we have mixed
1786 -- positional and discrete associations (other than the others choice).
1788 if Present (Expressions (N))
1789 and then (Nb_Choices > 1
1790 or else (Nb_Choices = 1 and then not Others_Present))
1791 then
1792 Error_Msg_N
1793 ("named association cannot follow positional association",
1794 First (Choices (First (Component_Associations (N)))));
1795 return Failure;
1796 end if;
1798 -- Test for the validity of an others choice if present
1800 if Others_Present and then not Others_Allowed then
1801 Error_Msg_N
1802 ("OTHERS choice not allowed here",
1803 First (Choices (First (Component_Associations (N)))));
1804 return Failure;
1805 end if;
1807 -- Protect against cascaded errors
1809 if Etype (Index_Typ) = Any_Type then
1810 return Failure;
1811 end if;
1813 -- STEP 2: Process named components
1815 if No (Expressions (N)) then
1816 if Others_Present then
1817 Case_Table_Size := Nb_Choices - 1;
1818 else
1819 Case_Table_Size := Nb_Choices;
1820 end if;
1822 Step_2 : declare
1823 Low : Node_Id;
1824 High : Node_Id;
1825 -- Denote the lowest and highest values in an aggregate choice
1827 S_Low : Node_Id := Empty;
1828 S_High : Node_Id := Empty;
1829 -- if a choice in an aggregate is a subtype indication these
1830 -- denote the lowest and highest values of the subtype
1832 Table : Case_Table_Type (0 .. Case_Table_Size);
1833 -- Used to sort all the different choice values. Entry zero is
1834 -- reserved for sorting purposes.
1836 Single_Choice : Boolean;
1837 -- Set to true every time there is a single discrete choice in a
1838 -- discrete association
1840 Prev_Nb_Discrete_Choices : Nat;
1841 -- Used to keep track of the number of discrete choices in the
1842 -- current association.
1844 Errors_Posted_On_Choices : Boolean := False;
1845 -- Keeps track of whether any choices have semantic errors
1847 function Empty_Range (A : Node_Id) return Boolean;
1848 -- If an association covers an empty range, some warnings on the
1849 -- expression of the association can be disabled.
1851 -----------------
1852 -- Empty_Range --
1853 -----------------
1855 function Empty_Range (A : Node_Id) return Boolean is
1856 R : constant Node_Id := First (Choices (A));
1857 begin
1858 return No (Next (R))
1859 and then Nkind (R) = N_Range
1860 and then Compile_Time_Compare
1861 (Low_Bound (R), High_Bound (R), False) = GT;
1862 end Empty_Range;
1864 -- Start of processing for Step_2
1866 begin
1867 -- STEP 2 (A): Check discrete choices validity
1869 Assoc := First (Component_Associations (N));
1870 while Present (Assoc) loop
1871 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
1872 Choice := First (Choices (Assoc));
1873 loop
1874 Analyze (Choice);
1876 if Nkind (Choice) = N_Others_Choice then
1877 Single_Choice := False;
1878 exit;
1880 -- Test for subtype mark without constraint
1882 elsif Is_Entity_Name (Choice) and then
1883 Is_Type (Entity (Choice))
1884 then
1885 if Base_Type (Entity (Choice)) /= Index_Base then
1886 Error_Msg_N
1887 ("invalid subtype mark in aggregate choice",
1888 Choice);
1889 return Failure;
1890 end if;
1892 -- Case of subtype indication
1894 elsif Nkind (Choice) = N_Subtype_Indication then
1895 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
1897 if Has_Dynamic_Predicate_Aspect
1898 (Entity (Subtype_Mark (Choice)))
1899 then
1900 Error_Msg_NE ("subtype& has dynamic predicate, "
1901 & "not allowed in aggregate choice",
1902 Choice, Entity (Subtype_Mark (Choice)));
1903 end if;
1905 -- Does the subtype indication evaluation raise CE?
1907 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
1908 Get_Index_Bounds (Choice, Low, High);
1909 Check_Bounds (S_Low, S_High, Low, High);
1911 -- Case of range or expression
1913 else
1914 Resolve (Choice, Index_Base);
1915 Check_Unset_Reference (Choice);
1916 Check_Non_Static_Context (Choice);
1918 -- If semantic errors were posted on the choice, then
1919 -- record that for possible early return from later
1920 -- processing (see handling of enumeration choices).
1922 if Error_Posted (Choice) then
1923 Errors_Posted_On_Choices := True;
1924 end if;
1926 -- Do not range check a choice. This check is redundant
1927 -- since this test is already done when we check that the
1928 -- bounds of the array aggregate are within range.
1930 Set_Do_Range_Check (Choice, False);
1932 -- In SPARK, the choice must be static
1934 if not (Is_OK_Static_Expression (Choice)
1935 or else (Nkind (Choice) = N_Range
1936 and then Is_OK_Static_Range (Choice)))
1937 then
1938 Check_SPARK_05_Restriction
1939 ("choice should be static", Choice);
1940 end if;
1941 end if;
1943 -- If we could not resolve the discrete choice stop here
1945 if Etype (Choice) = Any_Type then
1946 return Failure;
1948 -- If the discrete choice raises CE get its original bounds
1950 elsif Nkind (Choice) = N_Raise_Constraint_Error then
1951 Set_Raises_Constraint_Error (N);
1952 Get_Index_Bounds (Original_Node (Choice), Low, High);
1954 -- Otherwise get its bounds as usual
1956 else
1957 Get_Index_Bounds (Choice, Low, High);
1958 end if;
1960 if (Dynamic_Or_Null_Range (Low, High)
1961 or else (Nkind (Choice) = N_Subtype_Indication
1962 and then
1963 Dynamic_Or_Null_Range (S_Low, S_High)))
1964 and then Nb_Choices /= 1
1965 then
1966 Error_Msg_N
1967 ("dynamic or empty choice in aggregate " &
1968 "must be the only choice", Choice);
1969 return Failure;
1970 end if;
1972 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
1973 Table (Nb_Discrete_Choices).Lo := Low;
1974 Table (Nb_Discrete_Choices).Hi := High;
1975 Table (Nb_Discrete_Choices).Choice := Choice;
1977 Next (Choice);
1979 if No (Choice) then
1981 -- Check if we have a single discrete choice and whether
1982 -- this discrete choice specifies a single value.
1984 Single_Choice :=
1985 (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1)
1986 and then (Low = High);
1988 exit;
1989 end if;
1990 end loop;
1992 -- Ada 2005 (AI-231)
1994 if Ada_Version >= Ada_2005
1995 and then Known_Null (Expression (Assoc))
1996 and then not Empty_Range (Assoc)
1997 then
1998 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
1999 end if;
2001 -- Ada 2005 (AI-287): In case of default initialized component
2002 -- we delay the resolution to the expansion phase.
2004 if Box_Present (Assoc) then
2006 -- Ada 2005 (AI-287): In case of default initialization of a
2007 -- component the expander will generate calls to the
2008 -- corresponding initialization subprogram. We need to call
2009 -- Resolve_Aggr_Expr to check the rules about
2010 -- dimensionality.
2012 if not Resolve_Aggr_Expr
2013 (Assoc, Single_Elmt => Single_Choice)
2014 then
2015 return Failure;
2016 end if;
2018 elsif not Resolve_Aggr_Expr
2019 (Expression (Assoc), Single_Elmt => Single_Choice)
2020 then
2021 return Failure;
2023 -- Check incorrect use of dynamically tagged expression
2025 -- We differentiate here two cases because the expression may
2026 -- not be decorated. For example, the analysis and resolution
2027 -- of the expression associated with the others choice will be
2028 -- done later with the full aggregate. In such case we
2029 -- duplicate the expression tree to analyze the copy and
2030 -- perform the required check.
2032 elsif not Present (Etype (Expression (Assoc))) then
2033 declare
2034 Save_Analysis : constant Boolean := Full_Analysis;
2035 Expr : constant Node_Id :=
2036 New_Copy_Tree (Expression (Assoc));
2038 begin
2039 Expander_Mode_Save_And_Set (False);
2040 Full_Analysis := False;
2042 -- Analyze the expression, making sure it is properly
2043 -- attached to the tree before we do the analysis.
2045 Set_Parent (Expr, Parent (Expression (Assoc)));
2046 Analyze (Expr);
2048 -- If the expression is a literal, propagate this info
2049 -- to the expression in the association, to enable some
2050 -- optimizations downstream.
2052 if Is_Entity_Name (Expr)
2053 and then Present (Entity (Expr))
2054 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
2055 then
2056 Analyze_And_Resolve
2057 (Expression (Assoc), Component_Typ);
2058 end if;
2060 Full_Analysis := Save_Analysis;
2061 Expander_Mode_Restore;
2063 if Is_Tagged_Type (Etype (Expr)) then
2064 Check_Dynamically_Tagged_Expression
2065 (Expr => Expr,
2066 Typ => Component_Type (Etype (N)),
2067 Related_Nod => N);
2068 end if;
2069 end;
2071 elsif Is_Tagged_Type (Etype (Expression (Assoc))) then
2072 Check_Dynamically_Tagged_Expression
2073 (Expr => Expression (Assoc),
2074 Typ => Component_Type (Etype (N)),
2075 Related_Nod => N);
2076 end if;
2078 Next (Assoc);
2079 end loop;
2081 -- If aggregate contains more than one choice then these must be
2082 -- static. Check for duplicate and missing values.
2084 -- Note: there is duplicated code here wrt Check_Choice_Set in
2085 -- the body of Sem_Case, and it is possible we could just reuse
2086 -- that procedure. To be checked ???
2088 if Nb_Discrete_Choices > 1 then
2089 Check_Choices : declare
2090 Choice : Node_Id;
2091 -- Location of choice for messages
2093 Hi_Val : Uint;
2094 Lo_Val : Uint;
2095 -- High end of one range and Low end of the next. Should be
2096 -- contiguous if there is no hole in the list of values.
2098 Lo_Dup : Uint;
2099 Hi_Dup : Uint;
2100 -- End points of duplicated range
2102 Missing_Or_Duplicates : Boolean := False;
2103 -- Set True if missing or duplicate choices found
2105 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id);
2106 -- Output continuation message with a representation of the
2107 -- bounds (just Lo if Lo = Hi, else Lo .. Hi). C is the
2108 -- choice node where the message is to be posted.
2110 ------------------------
2111 -- Output_Bad_Choices --
2112 ------------------------
2114 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id) is
2115 begin
2116 -- Enumeration type case
2118 if Is_Enumeration_Type (Index_Typ) then
2119 Error_Msg_Name_1 :=
2120 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Lo, Loc));
2121 Error_Msg_Name_2 :=
2122 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Hi, Loc));
2124 if Lo = Hi then
2125 Error_Msg_N ("\\ %!", C);
2126 else
2127 Error_Msg_N ("\\ % .. %!", C);
2128 end if;
2130 -- Integer types case
2132 else
2133 Error_Msg_Uint_1 := Lo;
2134 Error_Msg_Uint_2 := Hi;
2136 if Lo = Hi then
2137 Error_Msg_N ("\\ ^!", C);
2138 else
2139 Error_Msg_N ("\\ ^ .. ^!", C);
2140 end if;
2141 end if;
2142 end Output_Bad_Choices;
2144 -- Start of processing for Check_Choices
2146 begin
2147 Sort_Case_Table (Table);
2149 -- First we do a quick linear loop to find out if we have
2150 -- any duplicates or missing entries (usually we have a
2151 -- legal aggregate, so this will get us out quickly).
2153 for J in 1 .. Nb_Discrete_Choices - 1 loop
2154 Hi_Val := Expr_Value (Table (J).Hi);
2155 Lo_Val := Expr_Value (Table (J + 1).Lo);
2157 if Lo_Val <= Hi_Val
2158 or else (Lo_Val > Hi_Val + 1
2159 and then not Others_Present)
2160 then
2161 Missing_Or_Duplicates := True;
2162 exit;
2163 end if;
2164 end loop;
2166 -- If we have missing or duplicate entries, first fill in
2167 -- the Highest entries to make life easier in the following
2168 -- loops to detect bad entries.
2170 if Missing_Or_Duplicates then
2171 Table (1).Highest := Expr_Value (Table (1).Hi);
2173 for J in 2 .. Nb_Discrete_Choices loop
2174 Table (J).Highest :=
2175 UI_Max
2176 (Table (J - 1).Highest, Expr_Value (Table (J).Hi));
2177 end loop;
2179 -- Loop through table entries to find duplicate indexes
2181 for J in 2 .. Nb_Discrete_Choices loop
2182 Lo_Val := Expr_Value (Table (J).Lo);
2183 Hi_Val := Expr_Value (Table (J).Hi);
2185 -- Case where we have duplicates (the lower bound of
2186 -- this choice is less than or equal to the highest
2187 -- high bound found so far).
2189 if Lo_Val <= Table (J - 1).Highest then
2191 -- We move backwards looking for duplicates. We can
2192 -- abandon this loop as soon as we reach a choice
2193 -- highest value that is less than Lo_Val.
2195 for K in reverse 1 .. J - 1 loop
2196 exit when Table (K).Highest < Lo_Val;
2198 -- Here we may have duplicates between entries
2199 -- for K and J. Get range of duplicates.
2201 Lo_Dup :=
2202 UI_Max (Lo_Val, Expr_Value (Table (K).Lo));
2203 Hi_Dup :=
2204 UI_Min (Hi_Val, Expr_Value (Table (K).Hi));
2206 -- Nothing to do if duplicate range is null
2208 if Lo_Dup > Hi_Dup then
2209 null;
2211 -- Otherwise place proper message
2213 else
2214 -- We place message on later choice, with a
2215 -- line reference to the earlier choice.
2217 if Sloc (Table (J).Choice) <
2218 Sloc (Table (K).Choice)
2219 then
2220 Choice := Table (K).Choice;
2221 Error_Msg_Sloc := Sloc (Table (J).Choice);
2222 else
2223 Choice := Table (J).Choice;
2224 Error_Msg_Sloc := Sloc (Table (K).Choice);
2225 end if;
2227 if Lo_Dup = Hi_Dup then
2228 Error_Msg_N
2229 ("index value in array aggregate "
2230 & "duplicates the one given#!", Choice);
2231 else
2232 Error_Msg_N
2233 ("index values in array aggregate "
2234 & "duplicate those given#!", Choice);
2235 end if;
2237 Output_Bad_Choices (Lo_Dup, Hi_Dup, Choice);
2238 end if;
2239 end loop;
2240 end if;
2241 end loop;
2243 -- Loop through entries in table to find missing indexes.
2244 -- Not needed if others, since missing impossible.
2246 if not Others_Present then
2247 for J in 2 .. Nb_Discrete_Choices loop
2248 Lo_Val := Expr_Value (Table (J).Lo);
2249 Hi_Val := Table (J - 1).Highest;
2251 if Lo_Val > Hi_Val + 1 then
2253 declare
2254 Error_Node : Node_Id;
2256 begin
2257 -- If the choice is the bound of a range in
2258 -- a subtype indication, it is not in the
2259 -- source lists for the aggregate itself, so
2260 -- post the error on the aggregate. Otherwise
2261 -- post it on choice itself.
2263 Choice := Table (J).Choice;
2265 if Is_List_Member (Choice) then
2266 Error_Node := Choice;
2267 else
2268 Error_Node := N;
2269 end if;
2271 if Hi_Val + 1 = Lo_Val - 1 then
2272 Error_Msg_N
2273 ("missing index value "
2274 & "in array aggregate!", Error_Node);
2275 else
2276 Error_Msg_N
2277 ("missing index values "
2278 & "in array aggregate!", Error_Node);
2279 end if;
2281 Output_Bad_Choices
2282 (Hi_Val + 1, Lo_Val - 1, Error_Node);
2283 end;
2284 end if;
2285 end loop;
2286 end if;
2288 -- If either missing or duplicate values, return failure
2290 Set_Etype (N, Any_Composite);
2291 return Failure;
2292 end if;
2293 end Check_Choices;
2294 end if;
2296 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
2298 if Nb_Discrete_Choices > 0 then
2299 Choices_Low := Table (1).Lo;
2300 Choices_High := Table (Nb_Discrete_Choices).Hi;
2301 end if;
2303 -- If Others is present, then bounds of aggregate come from the
2304 -- index constraint (not the choices in the aggregate itself).
2306 if Others_Present then
2307 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2309 -- No others clause present
2311 else
2312 -- Special processing if others allowed and not present. This
2313 -- means that the bounds of the aggregate come from the index
2314 -- constraint (and the length must match).
2316 if Others_Allowed then
2317 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2319 -- If others allowed, and no others present, then the array
2320 -- should cover all index values. If it does not, we will
2321 -- get a length check warning, but there is two cases where
2322 -- an additional warning is useful:
2324 -- If we have no positional components, and the length is
2325 -- wrong (which we can tell by others being allowed with
2326 -- missing components), and the index type is an enumeration
2327 -- type, then issue appropriate warnings about these missing
2328 -- components. They are only warnings, since the aggregate
2329 -- is fine, it's just the wrong length. We skip this check
2330 -- for standard character types (since there are no literals
2331 -- and it is too much trouble to concoct them), and also if
2332 -- any of the bounds have values that are not known at
2333 -- compile time.
2335 -- Another case warranting a warning is when the length is
2336 -- right, but as above we have an index type that is an
2337 -- enumeration, and the bounds do not match. This is a
2338 -- case where dubious sliding is allowed and we generate
2339 -- a warning that the bounds do not match.
2341 if No (Expressions (N))
2342 and then Nkind (Index) = N_Range
2343 and then Is_Enumeration_Type (Etype (Index))
2344 and then not Is_Standard_Character_Type (Etype (Index))
2345 and then Compile_Time_Known_Value (Aggr_Low)
2346 and then Compile_Time_Known_Value (Aggr_High)
2347 and then Compile_Time_Known_Value (Choices_Low)
2348 and then Compile_Time_Known_Value (Choices_High)
2349 then
2350 -- If any of the expressions or range bounds in choices
2351 -- have semantic errors, then do not attempt further
2352 -- resolution, to prevent cascaded errors.
2354 if Errors_Posted_On_Choices then
2355 return Failure;
2356 end if;
2358 declare
2359 ALo : constant Node_Id := Expr_Value_E (Aggr_Low);
2360 AHi : constant Node_Id := Expr_Value_E (Aggr_High);
2361 CLo : constant Node_Id := Expr_Value_E (Choices_Low);
2362 CHi : constant Node_Id := Expr_Value_E (Choices_High);
2364 Ent : Entity_Id;
2366 begin
2367 -- Warning case 1, missing values at start/end. Only
2368 -- do the check if the number of entries is too small.
2370 if (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2372 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2373 then
2374 Error_Msg_N
2375 ("missing index value(s) in array aggregate??",
2378 -- Output missing value(s) at start
2380 if Chars (ALo) /= Chars (CLo) then
2381 Ent := Prev (CLo);
2383 if Chars (ALo) = Chars (Ent) then
2384 Error_Msg_Name_1 := Chars (ALo);
2385 Error_Msg_N ("\ %??", N);
2386 else
2387 Error_Msg_Name_1 := Chars (ALo);
2388 Error_Msg_Name_2 := Chars (Ent);
2389 Error_Msg_N ("\ % .. %??", N);
2390 end if;
2391 end if;
2393 -- Output missing value(s) at end
2395 if Chars (AHi) /= Chars (CHi) then
2396 Ent := Next (CHi);
2398 if Chars (AHi) = Chars (Ent) then
2399 Error_Msg_Name_1 := Chars (Ent);
2400 Error_Msg_N ("\ %??", N);
2401 else
2402 Error_Msg_Name_1 := Chars (Ent);
2403 Error_Msg_Name_2 := Chars (AHi);
2404 Error_Msg_N ("\ % .. %??", N);
2405 end if;
2406 end if;
2408 -- Warning case 2, dubious sliding. The First_Subtype
2409 -- test distinguishes between a constrained type where
2410 -- sliding is not allowed (so we will get a warning
2411 -- later that Constraint_Error will be raised), and
2412 -- the unconstrained case where sliding is permitted.
2414 elsif (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2416 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2417 and then Chars (ALo) /= Chars (CLo)
2418 and then
2419 not Is_Constrained (First_Subtype (Etype (N)))
2420 then
2421 Error_Msg_N
2422 ("bounds of aggregate do not match target??", N);
2423 end if;
2424 end;
2425 end if;
2426 end if;
2428 -- If no others, aggregate bounds come from aggregate
2430 Aggr_Low := Choices_Low;
2431 Aggr_High := Choices_High;
2432 end if;
2433 end Step_2;
2435 -- STEP 3: Process positional components
2437 else
2438 -- STEP 3 (A): Process positional elements
2440 Expr := First (Expressions (N));
2441 Nb_Elements := Uint_0;
2442 while Present (Expr) loop
2443 Nb_Elements := Nb_Elements + 1;
2445 -- Ada 2005 (AI-231)
2447 if Ada_Version >= Ada_2005
2448 and then Known_Null (Expr)
2449 then
2450 Check_Can_Never_Be_Null (Etype (N), Expr);
2451 end if;
2453 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
2454 return Failure;
2455 end if;
2457 -- Check incorrect use of dynamically tagged expression
2459 if Is_Tagged_Type (Etype (Expr)) then
2460 Check_Dynamically_Tagged_Expression
2461 (Expr => Expr,
2462 Typ => Component_Type (Etype (N)),
2463 Related_Nod => N);
2464 end if;
2466 Next (Expr);
2467 end loop;
2469 if Others_Present then
2470 Assoc := Last (Component_Associations (N));
2472 -- Ada 2005 (AI-231)
2474 if Ada_Version >= Ada_2005
2475 and then Known_Null (Assoc)
2476 then
2477 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2478 end if;
2480 -- Ada 2005 (AI-287): In case of default initialized component,
2481 -- we delay the resolution to the expansion phase.
2483 if Box_Present (Assoc) then
2485 -- Ada 2005 (AI-287): In case of default initialization of a
2486 -- component the expander will generate calls to the
2487 -- corresponding initialization subprogram. We need to call
2488 -- Resolve_Aggr_Expr to check the rules about
2489 -- dimensionality.
2491 if not Resolve_Aggr_Expr (Assoc, Single_Elmt => False) then
2492 return Failure;
2493 end if;
2495 elsif not Resolve_Aggr_Expr (Expression (Assoc),
2496 Single_Elmt => False)
2497 then
2498 return Failure;
2500 -- Check incorrect use of dynamically tagged expression. The
2501 -- expression of the others choice has not been resolved yet.
2502 -- In order to diagnose the semantic error we create a duplicate
2503 -- tree to analyze it and perform the check.
2505 else
2506 declare
2507 Save_Analysis : constant Boolean := Full_Analysis;
2508 Expr : constant Node_Id :=
2509 New_Copy_Tree (Expression (Assoc));
2511 begin
2512 Expander_Mode_Save_And_Set (False);
2513 Full_Analysis := False;
2514 Analyze (Expr);
2515 Full_Analysis := Save_Analysis;
2516 Expander_Mode_Restore;
2518 if Is_Tagged_Type (Etype (Expr)) then
2519 Check_Dynamically_Tagged_Expression
2520 (Expr => Expr,
2521 Typ => Component_Type (Etype (N)),
2522 Related_Nod => N);
2523 end if;
2524 end;
2525 end if;
2526 end if;
2528 -- STEP 3 (B): Compute the aggregate bounds
2530 if Others_Present then
2531 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2533 else
2534 if Others_Allowed then
2535 Get_Index_Bounds (Index_Constr, Aggr_Low, Discard);
2536 else
2537 Aggr_Low := Index_Typ_Low;
2538 end if;
2540 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
2541 Check_Bound (Index_Base_High, Aggr_High);
2542 end if;
2543 end if;
2545 -- STEP 4: Perform static aggregate checks and save the bounds
2547 -- Check (A)
2549 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
2550 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
2552 -- Check (B)
2554 if Others_Present and then Nb_Discrete_Choices > 0 then
2555 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
2556 Check_Bounds (Index_Typ_Low, Index_Typ_High,
2557 Choices_Low, Choices_High);
2558 Check_Bounds (Index_Base_Low, Index_Base_High,
2559 Choices_Low, Choices_High);
2561 -- Check (C)
2563 elsif Others_Present and then Nb_Elements > 0 then
2564 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
2565 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
2566 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
2567 end if;
2569 if Raises_Constraint_Error (Aggr_Low)
2570 or else Raises_Constraint_Error (Aggr_High)
2571 then
2572 Set_Raises_Constraint_Error (N);
2573 end if;
2575 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
2577 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
2578 -- since the addition node returned by Add is not yet analyzed. Attach
2579 -- to tree and analyze first. Reset analyzed flag to ensure it will get
2580 -- analyzed when it is a literal bound whose type must be properly set.
2582 if Others_Present or else Nb_Discrete_Choices > 0 then
2583 Aggr_High := Duplicate_Subexpr (Aggr_High);
2585 if Etype (Aggr_High) = Universal_Integer then
2586 Set_Analyzed (Aggr_High, False);
2587 end if;
2588 end if;
2590 -- If the aggregate already has bounds attached to it, it means this is
2591 -- a positional aggregate created as an optimization by
2592 -- Exp_Aggr.Convert_To_Positional, so we don't want to change those
2593 -- bounds.
2595 if Present (Aggregate_Bounds (N)) and then not Others_Allowed then
2596 Aggr_Low := Low_Bound (Aggregate_Bounds (N));
2597 Aggr_High := High_Bound (Aggregate_Bounds (N));
2598 end if;
2600 Set_Aggregate_Bounds
2601 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
2603 -- The bounds may contain expressions that must be inserted upwards.
2604 -- Attach them fully to the tree. After analysis, remove side effects
2605 -- from upper bound, if still needed.
2607 Set_Parent (Aggregate_Bounds (N), N);
2608 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
2609 Check_Unset_Reference (Aggregate_Bounds (N));
2611 if not Others_Present and then Nb_Discrete_Choices = 0 then
2612 Set_High_Bound
2613 (Aggregate_Bounds (N),
2614 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
2615 end if;
2617 -- Check the dimensions of each component in the array aggregate
2619 Analyze_Dimension_Array_Aggregate (N, Component_Typ);
2621 return Success;
2622 end Resolve_Array_Aggregate;
2624 ---------------------------------
2625 -- Resolve_Extension_Aggregate --
2626 ---------------------------------
2628 -- There are two cases to consider:
2630 -- a) If the ancestor part is a type mark, the components needed are the
2631 -- difference between the components of the expected type and the
2632 -- components of the given type mark.
2634 -- b) If the ancestor part is an expression, it must be unambiguous, and
2635 -- once we have its type we can also compute the needed components as in
2636 -- the previous case. In both cases, if the ancestor type is not the
2637 -- immediate ancestor, we have to build this ancestor recursively.
2639 -- In both cases, discriminants of the ancestor type do not play a role in
2640 -- the resolution of the needed components, because inherited discriminants
2641 -- cannot be used in a type extension. As a result we can compute
2642 -- independently the list of components of the ancestor type and of the
2643 -- expected type.
2645 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
2646 A : constant Node_Id := Ancestor_Part (N);
2647 A_Type : Entity_Id;
2648 I : Interp_Index;
2649 It : Interp;
2651 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean;
2652 -- If the type is limited, verify that the ancestor part is a legal
2653 -- expression (aggregate or function call, including 'Input)) that does
2654 -- not require a copy, as specified in 7.5(2).
2656 function Valid_Ancestor_Type return Boolean;
2657 -- Verify that the type of the ancestor part is a non-private ancestor
2658 -- of the expected type, which must be a type extension.
2660 ----------------------------
2661 -- Valid_Limited_Ancestor --
2662 ----------------------------
2664 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean is
2665 begin
2666 if Is_Entity_Name (Anc) and then Is_Type (Entity (Anc)) then
2667 return True;
2669 -- The ancestor must be a call or an aggregate, but a call may
2670 -- have been expanded into a temporary, so check original node.
2672 elsif Nkind_In (Anc, N_Aggregate,
2673 N_Extension_Aggregate,
2674 N_Function_Call)
2675 then
2676 return True;
2678 elsif Nkind (Original_Node (Anc)) = N_Function_Call then
2679 return True;
2681 elsif Nkind (Anc) = N_Attribute_Reference
2682 and then Attribute_Name (Anc) = Name_Input
2683 then
2684 return True;
2686 elsif Nkind (Anc) = N_Qualified_Expression then
2687 return Valid_Limited_Ancestor (Expression (Anc));
2689 else
2690 return False;
2691 end if;
2692 end Valid_Limited_Ancestor;
2694 -------------------------
2695 -- Valid_Ancestor_Type --
2696 -------------------------
2698 function Valid_Ancestor_Type return Boolean is
2699 Imm_Type : Entity_Id;
2701 begin
2702 Imm_Type := Base_Type (Typ);
2703 while Is_Derived_Type (Imm_Type) loop
2704 if Etype (Imm_Type) = Base_Type (A_Type) then
2705 return True;
2707 -- The base type of the parent type may appear as a private
2708 -- extension if it is declared as such in a parent unit of the
2709 -- current one. For consistency of the subsequent analysis use
2710 -- the partial view for the ancestor part.
2712 elsif Is_Private_Type (Etype (Imm_Type))
2713 and then Present (Full_View (Etype (Imm_Type)))
2714 and then Base_Type (A_Type) = Full_View (Etype (Imm_Type))
2715 then
2716 A_Type := Etype (Imm_Type);
2717 return True;
2719 -- The parent type may be a private extension. The aggregate is
2720 -- legal if the type of the aggregate is an extension of it that
2721 -- is not a private extension.
2723 elsif Is_Private_Type (A_Type)
2724 and then not Is_Private_Type (Imm_Type)
2725 and then Present (Full_View (A_Type))
2726 and then Base_Type (Full_View (A_Type)) = Etype (Imm_Type)
2727 then
2728 return True;
2730 else
2731 Imm_Type := Etype (Base_Type (Imm_Type));
2732 end if;
2733 end loop;
2735 -- If previous loop did not find a proper ancestor, report error
2737 Error_Msg_NE ("expect ancestor type of &", A, Typ);
2738 return False;
2739 end Valid_Ancestor_Type;
2741 -- Start of processing for Resolve_Extension_Aggregate
2743 begin
2744 -- Analyze the ancestor part and account for the case where it is a
2745 -- parameterless function call.
2747 Analyze (A);
2748 Check_Parameterless_Call (A);
2750 -- In SPARK, the ancestor part cannot be a type mark
2752 if Is_Entity_Name (A)
2753 and then Is_Type (Entity (A))
2754 then
2755 Check_SPARK_05_Restriction ("ancestor part cannot be a type mark", A);
2757 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
2758 -- must not have unknown discriminants.
2760 if Has_Unknown_Discriminants (Root_Type (Typ)) then
2761 Error_Msg_NE
2762 ("aggregate not available for type& whose ancestor "
2763 & "has unknown discriminants", N, Typ);
2764 end if;
2765 end if;
2767 if not Is_Tagged_Type (Typ) then
2768 Error_Msg_N ("type of extension aggregate must be tagged", N);
2769 return;
2771 elsif Is_Limited_Type (Typ) then
2773 -- Ada 2005 (AI-287): Limited aggregates are allowed
2775 if Ada_Version < Ada_2005 then
2776 Error_Msg_N ("aggregate type cannot be limited", N);
2777 Explain_Limited_Type (Typ, N);
2778 return;
2780 elsif Valid_Limited_Ancestor (A) then
2781 null;
2783 else
2784 Error_Msg_N
2785 ("limited ancestor part must be aggregate or function call", A);
2786 end if;
2788 elsif Is_Class_Wide_Type (Typ) then
2789 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
2790 return;
2791 end if;
2793 if Is_Entity_Name (A)
2794 and then Is_Type (Entity (A))
2795 then
2796 A_Type := Get_Full_View (Entity (A));
2798 if Valid_Ancestor_Type then
2799 Set_Entity (A, A_Type);
2800 Set_Etype (A, A_Type);
2802 Validate_Ancestor_Part (N);
2803 Resolve_Record_Aggregate (N, Typ);
2804 end if;
2806 elsif Nkind (A) /= N_Aggregate then
2807 if Is_Overloaded (A) then
2808 A_Type := Any_Type;
2810 Get_First_Interp (A, I, It);
2811 while Present (It.Typ) loop
2812 -- Only consider limited interpretations in the Ada 2005 case
2814 if Is_Tagged_Type (It.Typ)
2815 and then (Ada_Version >= Ada_2005
2816 or else not Is_Limited_Type (It.Typ))
2817 then
2818 if A_Type /= Any_Type then
2819 Error_Msg_N ("cannot resolve expression", A);
2820 return;
2821 else
2822 A_Type := It.Typ;
2823 end if;
2824 end if;
2826 Get_Next_Interp (I, It);
2827 end loop;
2829 if A_Type = Any_Type then
2830 if Ada_Version >= Ada_2005 then
2831 Error_Msg_N ("ancestor part must be of a tagged type", A);
2832 else
2833 Error_Msg_N
2834 ("ancestor part must be of a nonlimited tagged type", A);
2835 end if;
2837 return;
2838 end if;
2840 else
2841 A_Type := Etype (A);
2842 end if;
2844 if Valid_Ancestor_Type then
2845 Resolve (A, A_Type);
2846 Check_Unset_Reference (A);
2847 Check_Non_Static_Context (A);
2849 -- The aggregate is illegal if the ancestor expression is a call
2850 -- to a function with a limited unconstrained result, unless the
2851 -- type of the aggregate is a null extension. This restriction
2852 -- was added in AI05-67 to simplify implementation.
2854 if Nkind (A) = N_Function_Call
2855 and then Is_Limited_Type (A_Type)
2856 and then not Is_Null_Extension (Typ)
2857 and then not Is_Constrained (A_Type)
2858 then
2859 Error_Msg_N
2860 ("type of limited ancestor part must be constrained", A);
2862 -- Reject the use of CPP constructors that leave objects partially
2863 -- initialized. For example:
2865 -- type CPP_Root is tagged limited record ...
2866 -- pragma Import (CPP, CPP_Root);
2868 -- type CPP_DT is new CPP_Root and Iface ...
2869 -- pragma Import (CPP, CPP_DT);
2871 -- type Ada_DT is new CPP_DT with ...
2873 -- Obj : Ada_DT := Ada_DT'(New_CPP_Root with others => <>);
2875 -- Using the constructor of CPP_Root the slots of the dispatch
2876 -- table of CPP_DT cannot be set, and the secondary tag of
2877 -- CPP_DT is unknown.
2879 elsif Nkind (A) = N_Function_Call
2880 and then Is_CPP_Constructor_Call (A)
2881 and then Enclosing_CPP_Parent (Typ) /= A_Type
2882 then
2883 Error_Msg_NE
2884 ("??must use 'C'P'P constructor for type &", A,
2885 Enclosing_CPP_Parent (Typ));
2887 -- The following call is not needed if the previous warning
2888 -- is promoted to an error.
2890 Resolve_Record_Aggregate (N, Typ);
2892 elsif Is_Class_Wide_Type (Etype (A))
2893 and then Nkind (Original_Node (A)) = N_Function_Call
2894 then
2895 -- If the ancestor part is a dispatching call, it appears
2896 -- statically to be a legal ancestor, but it yields any member
2897 -- of the class, and it is not possible to determine whether
2898 -- it is an ancestor of the extension aggregate (much less
2899 -- which ancestor). It is not possible to determine the
2900 -- components of the extension part.
2902 -- This check implements AI-306, which in fact was motivated by
2903 -- an AdaCore query to the ARG after this test was added.
2905 Error_Msg_N ("ancestor part must be statically tagged", A);
2906 else
2907 Resolve_Record_Aggregate (N, Typ);
2908 end if;
2909 end if;
2911 else
2912 Error_Msg_N ("no unique type for this aggregate", A);
2913 end if;
2915 Check_Function_Writable_Actuals (N);
2916 end Resolve_Extension_Aggregate;
2918 ------------------------------
2919 -- Resolve_Record_Aggregate --
2920 ------------------------------
2922 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
2923 Assoc : Node_Id;
2924 -- N_Component_Association node belonging to the input aggregate N
2926 Expr : Node_Id;
2927 Positional_Expr : Node_Id;
2928 Component : Entity_Id;
2929 Component_Elmt : Elmt_Id;
2931 Components : constant Elist_Id := New_Elmt_List;
2932 -- Components is the list of the record components whose value must be
2933 -- provided in the aggregate. This list does include discriminants.
2935 New_Assoc_List : constant List_Id := New_List;
2936 New_Assoc : Node_Id;
2937 -- New_Assoc_List is the newly built list of N_Component_Association
2938 -- nodes. New_Assoc is one such N_Component_Association node in it.
2939 -- Note that while Assoc and New_Assoc contain the same kind of nodes,
2940 -- they are used to iterate over two different N_Component_Association
2941 -- lists.
2943 Others_Etype : Entity_Id := Empty;
2944 -- This variable is used to save the Etype of the last record component
2945 -- that takes its value from the others choice. Its purpose is:
2947 -- (a) make sure the others choice is useful
2949 -- (b) make sure the type of all the components whose value is
2950 -- subsumed by the others choice are the same.
2952 -- This variable is updated as a side effect of function Get_Value.
2954 Is_Box_Present : Boolean := False;
2955 Others_Box : Boolean := False;
2956 -- Ada 2005 (AI-287): Variables used in case of default initialization
2957 -- to provide a functionality similar to Others_Etype. Box_Present
2958 -- indicates that the component takes its default initialization;
2959 -- Others_Box indicates that at least one component takes its default
2960 -- initialization. Similar to Others_Etype, they are also updated as a
2961 -- side effect of function Get_Value.
2963 procedure Add_Association
2964 (Component : Entity_Id;
2965 Expr : Node_Id;
2966 Assoc_List : List_Id;
2967 Is_Box_Present : Boolean := False);
2968 -- Builds a new N_Component_Association node which associates Component
2969 -- to expression Expr and adds it to the association list being built,
2970 -- either New_Assoc_List, or the association being built for an inner
2971 -- aggregate.
2973 function Discr_Present (Discr : Entity_Id) return Boolean;
2974 -- If aggregate N is a regular aggregate this routine will return True.
2975 -- Otherwise, if N is an extension aggregate, Discr is a discriminant
2976 -- whose value may already have been specified by N's ancestor part.
2977 -- This routine checks whether this is indeed the case and if so returns
2978 -- False, signaling that no value for Discr should appear in N's
2979 -- aggregate part. Also, in this case, the routine appends to
2980 -- New_Assoc_List the discriminant value specified in the ancestor part.
2982 -- If the aggregate is in a context with expansion delayed, it will be
2983 -- reanalyzed. The inherited discriminant values must not be reinserted
2984 -- in the component list to prevent spurious errors, but they must be
2985 -- present on first analysis to build the proper subtype indications.
2986 -- The flag Inherited_Discriminant is used to prevent the re-insertion.
2988 function Get_Value
2989 (Compon : Node_Id;
2990 From : List_Id;
2991 Consider_Others_Choice : Boolean := False)
2992 return Node_Id;
2993 -- Given a record component stored in parameter Compon, this function
2994 -- returns its value as it appears in the list From, which is a list
2995 -- of N_Component_Association nodes.
2997 -- If no component association has a choice for the searched component,
2998 -- the value provided by the others choice is returned, if there is one,
2999 -- and Consider_Others_Choice is set to true. Otherwise Empty is
3000 -- returned. If there is more than one component association giving a
3001 -- value for the searched record component, an error message is emitted
3002 -- and the first found value is returned.
3004 -- If Consider_Others_Choice is set and the returned expression comes
3005 -- from the others choice, then Others_Etype is set as a side effect.
3006 -- An error message is emitted if the components taking their value from
3007 -- the others choice do not have same type.
3009 function New_Copy_Tree_And_Copy_Dimensions
3010 (Source : Node_Id;
3011 Map : Elist_Id := No_Elist;
3012 New_Sloc : Source_Ptr := No_Location;
3013 New_Scope : Entity_Id := Empty) return Node_Id;
3014 -- Same as New_Copy_Tree (defined in Sem_Util), except that this routine
3015 -- also copies the dimensions of Source to the returned node.
3017 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id);
3018 -- Analyzes and resolves expression Expr against the Etype of the
3019 -- Component. This routine also applies all appropriate checks to Expr.
3020 -- It finally saves a Expr in the newly created association list that
3021 -- will be attached to the final record aggregate. Note that if the
3022 -- Parent pointer of Expr is not set then Expr was produced with a
3023 -- New_Copy_Tree or some such.
3025 ---------------------
3026 -- Add_Association --
3027 ---------------------
3029 procedure Add_Association
3030 (Component : Entity_Id;
3031 Expr : Node_Id;
3032 Assoc_List : List_Id;
3033 Is_Box_Present : Boolean := False)
3035 Loc : Source_Ptr;
3036 Choice_List : constant List_Id := New_List;
3037 New_Assoc : Node_Id;
3039 begin
3040 -- If this is a box association the expression is missing, so
3041 -- use the Sloc of the aggregate itself for the new association.
3043 if Present (Expr) then
3044 Loc := Sloc (Expr);
3045 else
3046 Loc := Sloc (N);
3047 end if;
3049 Append (New_Occurrence_Of (Component, Loc), Choice_List);
3050 New_Assoc :=
3051 Make_Component_Association (Loc,
3052 Choices => Choice_List,
3053 Expression => Expr,
3054 Box_Present => Is_Box_Present);
3055 Append (New_Assoc, Assoc_List);
3056 end Add_Association;
3058 -------------------
3059 -- Discr_Present --
3060 -------------------
3062 function Discr_Present (Discr : Entity_Id) return Boolean is
3063 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
3065 Loc : Source_Ptr;
3067 Ancestor : Node_Id;
3068 Comp_Assoc : Node_Id;
3069 Discr_Expr : Node_Id;
3071 Ancestor_Typ : Entity_Id;
3072 Orig_Discr : Entity_Id;
3073 D : Entity_Id;
3074 D_Val : Elmt_Id := No_Elmt; -- stop junk warning
3076 Ancestor_Is_Subtyp : Boolean;
3078 begin
3079 if Regular_Aggr then
3080 return True;
3081 end if;
3083 -- Check whether inherited discriminant values have already been
3084 -- inserted in the aggregate. This will be the case if we are
3085 -- re-analyzing an aggregate whose expansion was delayed.
3087 if Present (Component_Associations (N)) then
3088 Comp_Assoc := First (Component_Associations (N));
3089 while Present (Comp_Assoc) loop
3090 if Inherited_Discriminant (Comp_Assoc) then
3091 return True;
3092 end if;
3094 Next (Comp_Assoc);
3095 end loop;
3096 end if;
3098 Ancestor := Ancestor_Part (N);
3099 Ancestor_Typ := Etype (Ancestor);
3100 Loc := Sloc (Ancestor);
3102 -- For a private type with unknown discriminants, use the underlying
3103 -- record view if it is available.
3105 if Has_Unknown_Discriminants (Ancestor_Typ)
3106 and then Present (Full_View (Ancestor_Typ))
3107 and then Present (Underlying_Record_View (Full_View (Ancestor_Typ)))
3108 then
3109 Ancestor_Typ := Underlying_Record_View (Full_View (Ancestor_Typ));
3110 end if;
3112 Ancestor_Is_Subtyp :=
3113 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
3115 -- If the ancestor part has no discriminants clearly N's aggregate
3116 -- part must provide a value for Discr.
3118 if not Has_Discriminants (Ancestor_Typ) then
3119 return True;
3121 -- If the ancestor part is an unconstrained subtype mark then the
3122 -- Discr must be present in N's aggregate part.
3124 elsif Ancestor_Is_Subtyp
3125 and then not Is_Constrained (Entity (Ancestor))
3126 then
3127 return True;
3128 end if;
3130 -- Now look to see if Discr was specified in the ancestor part
3132 if Ancestor_Is_Subtyp then
3133 D_Val := First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
3134 end if;
3136 Orig_Discr := Original_Record_Component (Discr);
3138 D := First_Discriminant (Ancestor_Typ);
3139 while Present (D) loop
3141 -- If Ancestor has already specified Disc value then insert its
3142 -- value in the final aggregate.
3144 if Original_Record_Component (D) = Orig_Discr then
3145 if Ancestor_Is_Subtyp then
3146 Discr_Expr := New_Copy_Tree (Node (D_Val));
3147 else
3148 Discr_Expr :=
3149 Make_Selected_Component (Loc,
3150 Prefix => Duplicate_Subexpr (Ancestor),
3151 Selector_Name => New_Occurrence_Of (Discr, Loc));
3152 end if;
3154 Resolve_Aggr_Expr (Discr_Expr, Discr);
3155 Set_Inherited_Discriminant (Last (New_Assoc_List));
3156 return False;
3157 end if;
3159 Next_Discriminant (D);
3161 if Ancestor_Is_Subtyp then
3162 Next_Elmt (D_Val);
3163 end if;
3164 end loop;
3166 return True;
3167 end Discr_Present;
3169 ---------------
3170 -- Get_Value --
3171 ---------------
3173 function Get_Value
3174 (Compon : Node_Id;
3175 From : List_Id;
3176 Consider_Others_Choice : Boolean := False)
3177 return Node_Id
3179 Typ : constant Entity_Id := Etype (Compon);
3180 Assoc : Node_Id;
3181 Expr : Node_Id := Empty;
3182 Selector_Name : Node_Id;
3184 begin
3185 Is_Box_Present := False;
3187 if Present (From) then
3188 Assoc := First (From);
3189 else
3190 return Empty;
3191 end if;
3193 while Present (Assoc) loop
3194 Selector_Name := First (Choices (Assoc));
3195 while Present (Selector_Name) loop
3196 if Nkind (Selector_Name) = N_Others_Choice then
3197 if Consider_Others_Choice and then No (Expr) then
3199 -- We need to duplicate the expression for each
3200 -- successive component covered by the others choice.
3201 -- This is redundant if the others_choice covers only
3202 -- one component (small optimization possible???), but
3203 -- indispensable otherwise, because each one must be
3204 -- expanded individually to preserve side-effects.
3206 -- Ada 2005 (AI-287): In case of default initialization
3207 -- of components, we duplicate the corresponding default
3208 -- expression (from the record type declaration). The
3209 -- copy must carry the sloc of the association (not the
3210 -- original expression) to prevent spurious elaboration
3211 -- checks when the default includes function calls.
3213 if Box_Present (Assoc) then
3214 Others_Box := True;
3215 Is_Box_Present := True;
3217 if Expander_Active then
3218 return
3219 New_Copy_Tree_And_Copy_Dimensions
3220 (Expression (Parent (Compon)),
3221 New_Sloc => Sloc (Assoc));
3222 else
3223 return Expression (Parent (Compon));
3224 end if;
3226 else
3227 if Present (Others_Etype)
3228 and then Base_Type (Others_Etype) /= Base_Type (Typ)
3229 then
3230 -- If the components are of an anonymous access
3231 -- type they are distinct, but this is legal in
3232 -- Ada 2012 as long as designated types match.
3234 if (Ekind (Typ) = E_Anonymous_Access_Type
3235 or else Ekind (Typ) =
3236 E_Anonymous_Access_Subprogram_Type)
3237 and then Designated_Type (Typ) =
3238 Designated_Type (Others_Etype)
3239 then
3240 null;
3241 else
3242 Error_Msg_N
3243 ("components in OTHERS choice must "
3244 & "have same type", Selector_Name);
3245 end if;
3246 end if;
3248 Others_Etype := Typ;
3250 -- Copy expression so that it is resolved
3251 -- independently for each component, This is needed
3252 -- for accessibility checks on compoents of anonymous
3253 -- access types, even in compile_only mode.
3255 if not Inside_A_Generic then
3257 -- In ASIS mode, preanalyze the expression in an
3258 -- others association before making copies for
3259 -- separate resolution and accessibility checks.
3260 -- This ensures that the type of the expression is
3261 -- available to ASIS in all cases, in particular if
3262 -- the expression is itself an aggregate.
3264 if ASIS_Mode then
3265 Preanalyze_And_Resolve (Expression (Assoc), Typ);
3266 end if;
3268 return
3269 New_Copy_Tree_And_Copy_Dimensions
3270 (Expression (Assoc));
3272 else
3273 return Expression (Assoc);
3274 end if;
3275 end if;
3276 end if;
3278 elsif Chars (Compon) = Chars (Selector_Name) then
3279 if No (Expr) then
3281 -- Ada 2005 (AI-231)
3283 if Ada_Version >= Ada_2005
3284 and then Known_Null (Expression (Assoc))
3285 then
3286 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
3287 end if;
3289 -- We need to duplicate the expression when several
3290 -- components are grouped together with a "|" choice.
3291 -- For instance "filed1 | filed2 => Expr"
3293 -- Ada 2005 (AI-287)
3295 if Box_Present (Assoc) then
3296 Is_Box_Present := True;
3298 -- Duplicate the default expression of the component
3299 -- from the record type declaration, so a new copy
3300 -- can be attached to the association.
3302 -- Note that we always copy the default expression,
3303 -- even when the association has a single choice, in
3304 -- order to create a proper association for the
3305 -- expanded aggregate.
3307 -- Component may have no default, in which case the
3308 -- expression is empty and the component is default-
3309 -- initialized, but an association for the component
3310 -- exists, and it is not covered by an others clause.
3312 -- Scalar and private types have no initialization
3313 -- procedure, so they remain uninitialized. If the
3314 -- target of the aggregate is a constant this
3315 -- deserves a warning.
3317 if No (Expression (Parent (Compon)))
3318 and then not Has_Non_Null_Base_Init_Proc (Typ)
3319 and then not Has_Aspect (Typ, Aspect_Default_Value)
3320 and then not Is_Concurrent_Type (Typ)
3321 and then Nkind (Parent (N)) = N_Object_Declaration
3322 and then Constant_Present (Parent (N))
3323 then
3324 Error_Msg_Node_2 := Typ;
3325 Error_Msg_NE
3326 ("component&? of type& is uninitialized",
3327 Assoc, Selector_Name);
3329 -- An additional reminder if the component type
3330 -- is a generic formal.
3332 if Is_Generic_Type (Base_Type (Typ)) then
3333 Error_Msg_NE
3334 ("\instance should provide actual "
3335 & "type with initialization for&",
3336 Assoc, Typ);
3337 end if;
3338 end if;
3340 return
3341 New_Copy_Tree_And_Copy_Dimensions
3342 (Expression (Parent (Compon)));
3344 else
3345 if Present (Next (Selector_Name)) then
3346 Expr := New_Copy_Tree_And_Copy_Dimensions
3347 (Expression (Assoc));
3348 else
3349 Expr := Expression (Assoc);
3350 end if;
3351 end if;
3353 Generate_Reference (Compon, Selector_Name, 'm');
3355 else
3356 Error_Msg_NE
3357 ("more than one value supplied for &",
3358 Selector_Name, Compon);
3360 end if;
3361 end if;
3363 Next (Selector_Name);
3364 end loop;
3366 Next (Assoc);
3367 end loop;
3369 return Expr;
3370 end Get_Value;
3372 ---------------------------------------
3373 -- New_Copy_Tree_And_Copy_Dimensions --
3374 ---------------------------------------
3376 function New_Copy_Tree_And_Copy_Dimensions
3377 (Source : Node_Id;
3378 Map : Elist_Id := No_Elist;
3379 New_Sloc : Source_Ptr := No_Location;
3380 New_Scope : Entity_Id := Empty) return Node_Id
3382 New_Copy : constant Node_Id :=
3383 New_Copy_Tree (Source, Map, New_Sloc, New_Scope);
3384 begin
3385 -- Move the dimensions of Source to New_Copy
3387 Copy_Dimensions (Source, New_Copy);
3388 return New_Copy;
3389 end New_Copy_Tree_And_Copy_Dimensions;
3391 -----------------------
3392 -- Resolve_Aggr_Expr --
3393 -----------------------
3395 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id) is
3396 Expr_Type : Entity_Id := Empty;
3397 New_C : Entity_Id := Component;
3398 New_Expr : Node_Id;
3400 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
3401 -- If the expression is an aggregate (possibly qualified) then its
3402 -- expansion is delayed until the enclosing aggregate is expanded
3403 -- into assignments. In that case, do not generate checks on the
3404 -- expression, because they will be generated later, and will other-
3405 -- wise force a copy (to remove side-effects) that would leave a
3406 -- dynamic-sized aggregate in the code, something that gigi cannot
3407 -- handle.
3409 Relocate : Boolean;
3410 -- Set to True if the resolved Expr node needs to be relocated when
3411 -- attached to the newly created association list. This node need not
3412 -- be relocated if its parent pointer is not set. In fact in this
3413 -- case Expr is the output of a New_Copy_Tree call. If Relocate is
3414 -- True then we have analyzed the expression node in the original
3415 -- aggregate and hence it needs to be relocated when moved over to
3416 -- the new association list.
3418 ---------------------------
3419 -- Has_Expansion_Delayed --
3420 ---------------------------
3422 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
3423 Kind : constant Node_Kind := Nkind (Expr);
3424 begin
3425 return (Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate)
3426 and then Present (Etype (Expr))
3427 and then Is_Record_Type (Etype (Expr))
3428 and then Expansion_Delayed (Expr))
3429 or else (Kind = N_Qualified_Expression
3430 and then Has_Expansion_Delayed (Expression (Expr)));
3431 end Has_Expansion_Delayed;
3433 -- Start of processing for Resolve_Aggr_Expr
3435 begin
3436 -- If the type of the component is elementary or the type of the
3437 -- aggregate does not contain discriminants, use the type of the
3438 -- component to resolve Expr.
3440 if Is_Elementary_Type (Etype (Component))
3441 or else not Has_Discriminants (Etype (N))
3442 then
3443 Expr_Type := Etype (Component);
3445 -- Otherwise we have to pick up the new type of the component from
3446 -- the new constrained subtype of the aggregate. In fact components
3447 -- which are of a composite type might be constrained by a
3448 -- discriminant, and we want to resolve Expr against the subtype were
3449 -- all discriminant occurrences are replaced with their actual value.
3451 else
3452 New_C := First_Component (Etype (N));
3453 while Present (New_C) loop
3454 if Chars (New_C) = Chars (Component) then
3455 Expr_Type := Etype (New_C);
3456 exit;
3457 end if;
3459 Next_Component (New_C);
3460 end loop;
3462 pragma Assert (Present (Expr_Type));
3464 -- For each range in an array type where a discriminant has been
3465 -- replaced with the constraint, check that this range is within
3466 -- the range of the base type. This checks is done in the init
3467 -- proc for regular objects, but has to be done here for
3468 -- aggregates since no init proc is called for them.
3470 if Is_Array_Type (Expr_Type) then
3471 declare
3472 Index : Node_Id;
3473 -- Range of the current constrained index in the array
3475 Orig_Index : Node_Id := First_Index (Etype (Component));
3476 -- Range corresponding to the range Index above in the
3477 -- original unconstrained record type. The bounds of this
3478 -- range may be governed by discriminants.
3480 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
3481 -- Range corresponding to the range Index above for the
3482 -- unconstrained array type. This range is needed to apply
3483 -- range checks.
3485 begin
3486 Index := First_Index (Expr_Type);
3487 while Present (Index) loop
3488 if Depends_On_Discriminant (Orig_Index) then
3489 Apply_Range_Check (Index, Etype (Unconstr_Index));
3490 end if;
3492 Next_Index (Index);
3493 Next_Index (Orig_Index);
3494 Next_Index (Unconstr_Index);
3495 end loop;
3496 end;
3497 end if;
3498 end if;
3500 -- If the Parent pointer of Expr is not set, Expr is an expression
3501 -- duplicated by New_Tree_Copy (this happens for record aggregates
3502 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
3503 -- Such a duplicated expression must be attached to the tree
3504 -- before analysis and resolution to enforce the rule that a tree
3505 -- fragment should never be analyzed or resolved unless it is
3506 -- attached to the current compilation unit.
3508 if No (Parent (Expr)) then
3509 Set_Parent (Expr, N);
3510 Relocate := False;
3511 else
3512 Relocate := True;
3513 end if;
3515 Analyze_And_Resolve (Expr, Expr_Type);
3516 Check_Expr_OK_In_Limited_Aggregate (Expr);
3517 Check_Non_Static_Context (Expr);
3518 Check_Unset_Reference (Expr);
3520 -- Check wrong use of class-wide types
3522 if Is_Class_Wide_Type (Etype (Expr)) then
3523 Error_Msg_N ("dynamically tagged expression not allowed", Expr);
3524 end if;
3526 if not Has_Expansion_Delayed (Expr) then
3527 Aggregate_Constraint_Checks (Expr, Expr_Type);
3528 end if;
3530 -- If an aggregate component has a type with predicates, an explicit
3531 -- predicate check must be applied, as for an assignment statement,
3532 -- because the aggegate might not be expanded into individual
3533 -- component assignments.
3535 if Present (Predicate_Function (Expr_Type)) then
3536 Apply_Predicate_Check (Expr, Expr_Type);
3537 end if;
3539 if Raises_Constraint_Error (Expr) then
3540 Set_Raises_Constraint_Error (N);
3541 end if;
3543 -- If the expression has been marked as requiring a range check, then
3544 -- generate it here. It's a bit odd to be generating such checks in
3545 -- the analyzer, but harmless since Generate_Range_Check does nothing
3546 -- (other than making sure Do_Range_Check is set) if the expander is
3547 -- not active.
3549 if Do_Range_Check (Expr) then
3550 Generate_Range_Check (Expr, Expr_Type, CE_Range_Check_Failed);
3551 end if;
3553 if Relocate then
3554 New_Expr := Relocate_Node (Expr);
3556 -- Since New_Expr is not gonna be analyzed later on, we need to
3557 -- propagate here the dimensions form Expr to New_Expr.
3559 Copy_Dimensions (Expr, New_Expr);
3561 else
3562 New_Expr := Expr;
3563 end if;
3565 Add_Association (New_C, New_Expr, New_Assoc_List);
3566 end Resolve_Aggr_Expr;
3568 -- Start of processing for Resolve_Record_Aggregate
3570 begin
3571 -- A record aggregate is restricted in SPARK:
3573 -- Each named association can have only a single choice.
3574 -- OTHERS cannot be used.
3575 -- Positional and named associations cannot be mixed.
3577 if Present (Component_Associations (N))
3578 and then Present (First (Component_Associations (N)))
3579 then
3581 if Present (Expressions (N)) then
3582 Check_SPARK_05_Restriction
3583 ("named association cannot follow positional one",
3584 First (Choices (First (Component_Associations (N)))));
3585 end if;
3587 declare
3588 Assoc : Node_Id;
3590 begin
3591 Assoc := First (Component_Associations (N));
3592 while Present (Assoc) loop
3593 if List_Length (Choices (Assoc)) > 1 then
3594 Check_SPARK_05_Restriction
3595 ("component association in record aggregate must "
3596 & "contain a single choice", Assoc);
3597 end if;
3599 if Nkind (First (Choices (Assoc))) = N_Others_Choice then
3600 Check_SPARK_05_Restriction
3601 ("record aggregate cannot contain OTHERS", Assoc);
3602 end if;
3604 Assoc := Next (Assoc);
3605 end loop;
3606 end;
3607 end if;
3609 -- We may end up calling Duplicate_Subexpr on expressions that are
3610 -- attached to New_Assoc_List. For this reason we need to attach it
3611 -- to the tree by setting its parent pointer to N. This parent point
3612 -- will change in STEP 8 below.
3614 Set_Parent (New_Assoc_List, N);
3616 -- STEP 1: abstract type and null record verification
3618 if Is_Abstract_Type (Typ) then
3619 Error_Msg_N ("type of aggregate cannot be abstract", N);
3620 end if;
3622 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
3623 Set_Etype (N, Typ);
3624 return;
3626 elsif Present (First_Entity (Typ))
3627 and then Null_Record_Present (N)
3628 and then not Is_Tagged_Type (Typ)
3629 then
3630 Error_Msg_N ("record aggregate cannot be null", N);
3631 return;
3633 -- If the type has no components, then the aggregate should either
3634 -- have "null record", or in Ada 2005 it could instead have a single
3635 -- component association given by "others => <>". For Ada 95 we flag an
3636 -- error at this point, but for Ada 2005 we proceed with checking the
3637 -- associations below, which will catch the case where it's not an
3638 -- aggregate with "others => <>". Note that the legality of a <>
3639 -- aggregate for a null record type was established by AI05-016.
3641 elsif No (First_Entity (Typ))
3642 and then Ada_Version < Ada_2005
3643 then
3644 Error_Msg_N ("record aggregate must be null", N);
3645 return;
3646 end if;
3648 -- STEP 2: Verify aggregate structure
3650 Step_2 : declare
3651 Selector_Name : Node_Id;
3652 Bad_Aggregate : Boolean := False;
3654 begin
3655 if Present (Component_Associations (N)) then
3656 Assoc := First (Component_Associations (N));
3657 else
3658 Assoc := Empty;
3659 end if;
3661 while Present (Assoc) loop
3662 Selector_Name := First (Choices (Assoc));
3663 while Present (Selector_Name) loop
3664 if Nkind (Selector_Name) = N_Identifier then
3665 null;
3667 elsif Nkind (Selector_Name) = N_Others_Choice then
3668 if Selector_Name /= First (Choices (Assoc))
3669 or else Present (Next (Selector_Name))
3670 then
3671 Error_Msg_N
3672 ("OTHERS must appear alone in a choice list",
3673 Selector_Name);
3674 return;
3676 elsif Present (Next (Assoc)) then
3677 Error_Msg_N
3678 ("OTHERS must appear last in an aggregate",
3679 Selector_Name);
3680 return;
3682 -- (Ada 2005): If this is an association with a box,
3683 -- indicate that the association need not represent
3684 -- any component.
3686 elsif Box_Present (Assoc) then
3687 Others_Box := True;
3688 end if;
3690 else
3691 Error_Msg_N
3692 ("selector name should be identifier or OTHERS",
3693 Selector_Name);
3694 Bad_Aggregate := True;
3695 end if;
3697 Next (Selector_Name);
3698 end loop;
3700 Next (Assoc);
3701 end loop;
3703 if Bad_Aggregate then
3704 return;
3705 end if;
3706 end Step_2;
3708 -- STEP 3: Find discriminant Values
3710 Step_3 : declare
3711 Discrim : Entity_Id;
3712 Missing_Discriminants : Boolean := False;
3714 begin
3715 if Present (Expressions (N)) then
3716 Positional_Expr := First (Expressions (N));
3717 else
3718 Positional_Expr := Empty;
3719 end if;
3721 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
3722 -- must not have unknown discriminants.
3724 if Is_Derived_Type (Typ)
3725 and then Has_Unknown_Discriminants (Root_Type (Typ))
3726 and then Nkind (N) /= N_Extension_Aggregate
3727 then
3728 Error_Msg_NE
3729 ("aggregate not available for type& whose ancestor "
3730 & "has unknown discriminants ", N, Typ);
3731 end if;
3733 if Has_Unknown_Discriminants (Typ)
3734 and then Present (Underlying_Record_View (Typ))
3735 then
3736 Discrim := First_Discriminant (Underlying_Record_View (Typ));
3737 elsif Has_Discriminants (Typ) then
3738 Discrim := First_Discriminant (Typ);
3739 else
3740 Discrim := Empty;
3741 end if;
3743 -- First find the discriminant values in the positional components
3745 while Present (Discrim) and then Present (Positional_Expr) loop
3746 if Discr_Present (Discrim) then
3747 Resolve_Aggr_Expr (Positional_Expr, Discrim);
3749 -- Ada 2005 (AI-231)
3751 if Ada_Version >= Ada_2005
3752 and then Known_Null (Positional_Expr)
3753 then
3754 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
3755 end if;
3757 Next (Positional_Expr);
3758 end if;
3760 if Present (Get_Value (Discrim, Component_Associations (N))) then
3761 Error_Msg_NE
3762 ("more than one value supplied for discriminant&",
3763 N, Discrim);
3764 end if;
3766 Next_Discriminant (Discrim);
3767 end loop;
3769 -- Find remaining discriminant values if any among named components
3771 while Present (Discrim) loop
3772 Expr := Get_Value (Discrim, Component_Associations (N), True);
3774 if not Discr_Present (Discrim) then
3775 if Present (Expr) then
3776 Error_Msg_NE
3777 ("more than one value supplied for discriminant&",
3778 N, Discrim);
3779 end if;
3781 elsif No (Expr) then
3782 Error_Msg_NE
3783 ("no value supplied for discriminant &", N, Discrim);
3784 Missing_Discriminants := True;
3786 else
3787 Resolve_Aggr_Expr (Expr, Discrim);
3788 end if;
3790 Next_Discriminant (Discrim);
3791 end loop;
3793 if Missing_Discriminants then
3794 return;
3795 end if;
3797 -- At this point and until the beginning of STEP 6, New_Assoc_List
3798 -- contains only the discriminants and their values.
3800 end Step_3;
3802 -- STEP 4: Set the Etype of the record aggregate
3804 -- ??? This code is pretty much a copy of Sem_Ch3.Build_Subtype. That
3805 -- routine should really be exported in sem_util or some such and used
3806 -- in sem_ch3 and here rather than have a copy of the code which is a
3807 -- maintenance nightmare.
3809 -- ??? Performance WARNING. The current implementation creates a new
3810 -- itype for all aggregates whose base type is discriminated. This means
3811 -- that for record aggregates nested inside an array aggregate we will
3812 -- create a new itype for each record aggregate if the array component
3813 -- type has discriminants. For large aggregates this may be a problem.
3814 -- What should be done in this case is to reuse itypes as much as
3815 -- possible.
3817 if Has_Discriminants (Typ)
3818 or else (Has_Unknown_Discriminants (Typ)
3819 and then Present (Underlying_Record_View (Typ)))
3820 then
3821 Build_Constrained_Itype : declare
3822 Loc : constant Source_Ptr := Sloc (N);
3823 Indic : Node_Id;
3824 Subtyp_Decl : Node_Id;
3825 Def_Id : Entity_Id;
3827 C : constant List_Id := New_List;
3829 begin
3830 New_Assoc := First (New_Assoc_List);
3831 while Present (New_Assoc) loop
3832 Append (Duplicate_Subexpr (Expression (New_Assoc)), To => C);
3833 Next (New_Assoc);
3834 end loop;
3836 if Has_Unknown_Discriminants (Typ)
3837 and then Present (Underlying_Record_View (Typ))
3838 then
3839 Indic :=
3840 Make_Subtype_Indication (Loc,
3841 Subtype_Mark =>
3842 New_Occurrence_Of (Underlying_Record_View (Typ), Loc),
3843 Constraint =>
3844 Make_Index_Or_Discriminant_Constraint (Loc, C));
3845 else
3846 Indic :=
3847 Make_Subtype_Indication (Loc,
3848 Subtype_Mark =>
3849 New_Occurrence_Of (Base_Type (Typ), Loc),
3850 Constraint =>
3851 Make_Index_Or_Discriminant_Constraint (Loc, C));
3852 end if;
3854 Def_Id := Create_Itype (Ekind (Typ), N);
3856 Subtyp_Decl :=
3857 Make_Subtype_Declaration (Loc,
3858 Defining_Identifier => Def_Id,
3859 Subtype_Indication => Indic);
3860 Set_Parent (Subtyp_Decl, Parent (N));
3862 -- Itypes must be analyzed with checks off (see itypes.ads)
3864 Analyze (Subtyp_Decl, Suppress => All_Checks);
3866 Set_Etype (N, Def_Id);
3867 Check_Static_Discriminated_Subtype
3868 (Def_Id, Expression (First (New_Assoc_List)));
3869 end Build_Constrained_Itype;
3871 else
3872 Set_Etype (N, Typ);
3873 end if;
3875 -- STEP 5: Get remaining components according to discriminant values
3877 Step_5 : declare
3878 Record_Def : Node_Id;
3879 Parent_Typ : Entity_Id;
3880 Root_Typ : Entity_Id;
3881 Parent_Typ_List : Elist_Id;
3882 Parent_Elmt : Elmt_Id;
3883 Errors_Found : Boolean := False;
3884 Dnode : Node_Id;
3886 function Find_Private_Ancestor return Entity_Id;
3887 -- AI05-0115: Find earlier ancestor in the derivation chain that is
3888 -- derived from a private view. Whether the aggregate is legal
3889 -- depends on the current visibility of the type as well as that
3890 -- of the parent of the ancestor.
3892 ---------------------------
3893 -- Find_Private_Ancestor --
3894 ---------------------------
3896 function Find_Private_Ancestor return Entity_Id is
3897 Par : Entity_Id;
3898 begin
3899 Par := Typ;
3900 loop
3901 if Has_Private_Ancestor (Par)
3902 and then not Has_Private_Ancestor (Etype (Base_Type (Par)))
3903 then
3904 return Par;
3906 elsif not Is_Derived_Type (Par) then
3907 return Empty;
3909 else
3910 Par := Etype (Base_Type (Par));
3911 end if;
3912 end loop;
3913 end Find_Private_Ancestor;
3915 -- Start of processing for Step_5
3917 begin
3918 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
3919 Parent_Typ_List := New_Elmt_List;
3921 -- If this is an extension aggregate, the component list must
3922 -- include all components that are not in the given ancestor type.
3923 -- Otherwise, the component list must include components of all
3924 -- ancestors, starting with the root.
3926 if Nkind (N) = N_Extension_Aggregate then
3927 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
3929 else
3930 -- AI05-0115: check legality of aggregate for type with
3931 -- aa private ancestor.
3933 Root_Typ := Root_Type (Typ);
3934 if Has_Private_Ancestor (Typ) then
3935 declare
3936 Ancestor : constant Entity_Id :=
3937 Find_Private_Ancestor;
3938 Ancestor_Unit : constant Entity_Id :=
3939 Cunit_Entity (Get_Source_Unit (Ancestor));
3940 Parent_Unit : constant Entity_Id :=
3941 Cunit_Entity
3942 (Get_Source_Unit (Base_Type (Etype (Ancestor))));
3943 begin
3945 -- check whether we are in a scope that has full view
3946 -- over the private ancestor and its parent. This can
3947 -- only happen if the derivation takes place in a child
3948 -- unit of the unit that declares the parent, and we are
3949 -- in the private part or body of that child unit, else
3950 -- the aggregate is illegal.
3952 if Is_Child_Unit (Ancestor_Unit)
3953 and then Scope (Ancestor_Unit) = Parent_Unit
3954 and then In_Open_Scopes (Scope (Ancestor))
3955 and then
3956 (In_Private_Part (Scope (Ancestor))
3957 or else In_Package_Body (Scope (Ancestor)))
3958 then
3959 null;
3961 else
3962 Error_Msg_NE
3963 ("type of aggregate has private ancestor&!",
3964 N, Root_Typ);
3965 Error_Msg_N ("must use extension aggregate!", N);
3966 return;
3967 end if;
3968 end;
3969 end if;
3971 Dnode := Declaration_Node (Base_Type (Root_Typ));
3973 -- If we don't get a full declaration, then we have some error
3974 -- which will get signalled later so skip this part. Otherwise
3975 -- gather components of root that apply to the aggregate type.
3976 -- We use the base type in case there is an applicable stored
3977 -- constraint that renames the discriminants of the root.
3979 if Nkind (Dnode) = N_Full_Type_Declaration then
3980 Record_Def := Type_Definition (Dnode);
3981 Gather_Components
3982 (Base_Type (Typ),
3983 Component_List (Record_Def),
3984 Governed_By => New_Assoc_List,
3985 Into => Components,
3986 Report_Errors => Errors_Found);
3988 if Errors_Found then
3989 Error_Msg_N
3990 ("discriminant controlling variant part is not static",
3992 return;
3993 end if;
3994 end if;
3995 end if;
3997 Parent_Typ := Base_Type (Typ);
3998 while Parent_Typ /= Root_Typ loop
3999 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
4000 Parent_Typ := Etype (Parent_Typ);
4002 if Nkind (Parent (Base_Type (Parent_Typ))) =
4003 N_Private_Type_Declaration
4004 or else Nkind (Parent (Base_Type (Parent_Typ))) =
4005 N_Private_Extension_Declaration
4006 then
4007 if Nkind (N) /= N_Extension_Aggregate then
4008 Error_Msg_NE
4009 ("type of aggregate has private ancestor&!",
4010 N, Parent_Typ);
4011 Error_Msg_N ("must use extension aggregate!", N);
4012 return;
4014 elsif Parent_Typ /= Root_Typ then
4015 Error_Msg_NE
4016 ("ancestor part of aggregate must be private type&",
4017 Ancestor_Part (N), Parent_Typ);
4018 return;
4019 end if;
4021 -- The current view of ancestor part may be a private type,
4022 -- while the context type is always non-private.
4024 elsif Is_Private_Type (Root_Typ)
4025 and then Present (Full_View (Root_Typ))
4026 and then Nkind (N) = N_Extension_Aggregate
4027 then
4028 exit when Base_Type (Full_View (Root_Typ)) = Parent_Typ;
4029 end if;
4030 end loop;
4032 -- Now collect components from all other ancestors, beginning
4033 -- with the current type. If the type has unknown discriminants
4034 -- use the component list of the Underlying_Record_View, which
4035 -- needs to be used for the subsequent expansion of the aggregate
4036 -- into assignments.
4038 Parent_Elmt := First_Elmt (Parent_Typ_List);
4039 while Present (Parent_Elmt) loop
4040 Parent_Typ := Node (Parent_Elmt);
4042 if Has_Unknown_Discriminants (Parent_Typ)
4043 and then Present (Underlying_Record_View (Typ))
4044 then
4045 Parent_Typ := Underlying_Record_View (Parent_Typ);
4046 end if;
4048 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
4049 Gather_Components (Empty,
4050 Component_List (Record_Extension_Part (Record_Def)),
4051 Governed_By => New_Assoc_List,
4052 Into => Components,
4053 Report_Errors => Errors_Found);
4055 Next_Elmt (Parent_Elmt);
4056 end loop;
4058 -- Typ is not a derived tagged type
4060 else
4061 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
4063 if Null_Present (Record_Def) then
4064 null;
4066 elsif not Has_Unknown_Discriminants (Typ) then
4067 Gather_Components
4068 (Base_Type (Typ),
4069 Component_List (Record_Def),
4070 Governed_By => New_Assoc_List,
4071 Into => Components,
4072 Report_Errors => Errors_Found);
4074 else
4075 Gather_Components
4076 (Base_Type (Underlying_Record_View (Typ)),
4077 Component_List (Record_Def),
4078 Governed_By => New_Assoc_List,
4079 Into => Components,
4080 Report_Errors => Errors_Found);
4081 end if;
4082 end if;
4084 if Errors_Found then
4085 return;
4086 end if;
4087 end Step_5;
4089 -- STEP 6: Find component Values
4091 Component := Empty;
4092 Component_Elmt := First_Elmt (Components);
4094 -- First scan the remaining positional associations in the aggregate.
4095 -- Remember that at this point Positional_Expr contains the current
4096 -- positional association if any is left after looking for discriminant
4097 -- values in step 3.
4099 while Present (Positional_Expr) and then Present (Component_Elmt) loop
4100 Component := Node (Component_Elmt);
4101 Resolve_Aggr_Expr (Positional_Expr, Component);
4103 -- Ada 2005 (AI-231)
4105 if Ada_Version >= Ada_2005
4106 and then Known_Null (Positional_Expr)
4107 then
4108 Check_Can_Never_Be_Null (Component, Positional_Expr);
4109 end if;
4111 if Present (Get_Value (Component, Component_Associations (N))) then
4112 Error_Msg_NE
4113 ("more than one value supplied for Component &", N, Component);
4114 end if;
4116 Next (Positional_Expr);
4117 Next_Elmt (Component_Elmt);
4118 end loop;
4120 if Present (Positional_Expr) then
4121 Error_Msg_N
4122 ("too many components for record aggregate", Positional_Expr);
4123 end if;
4125 -- Now scan for the named arguments of the aggregate
4127 while Present (Component_Elmt) loop
4128 Component := Node (Component_Elmt);
4129 Expr := Get_Value (Component, Component_Associations (N), True);
4131 -- Note: The previous call to Get_Value sets the value of the
4132 -- variable Is_Box_Present.
4134 -- Ada 2005 (AI-287): Handle components with default initialization.
4135 -- Note: This feature was originally added to Ada 2005 for limited
4136 -- but it was finally allowed with any type.
4138 if Is_Box_Present then
4139 Check_Box_Component : declare
4140 Ctyp : constant Entity_Id := Etype (Component);
4142 begin
4143 -- If there is a default expression for the aggregate, copy
4144 -- it into a new association. This copy must modify the scopes
4145 -- of internal types that may be attached to the expression
4146 -- (e.g. index subtypes of arrays) because in general the type
4147 -- declaration and the aggregate appear in different scopes,
4148 -- and the backend requires the scope of the type to match the
4149 -- point at which it is elaborated.
4151 -- If the component has an initialization procedure (IP) we
4152 -- pass the component to the expander, which will generate
4153 -- the call to such IP.
4155 -- If the component has discriminants, their values must
4156 -- be taken from their subtype. This is indispensable for
4157 -- constraints that are given by the current instance of an
4158 -- enclosing type, to allow the expansion of the aggregate to
4159 -- replace the reference to the current instance by the target
4160 -- object of the aggregate.
4162 if Present (Parent (Component))
4163 and then
4164 Nkind (Parent (Component)) = N_Component_Declaration
4165 and then Present (Expression (Parent (Component)))
4166 then
4167 Expr :=
4168 New_Copy_Tree_And_Copy_Dimensions
4169 (Expression (Parent (Component)),
4170 New_Scope => Current_Scope,
4171 New_Sloc => Sloc (N));
4173 Add_Association
4174 (Component => Component,
4175 Expr => Expr,
4176 Assoc_List => New_Assoc_List);
4177 Set_Has_Self_Reference (N);
4179 -- A box-defaulted access component gets the value null. Also
4180 -- included are components of private types whose underlying
4181 -- type is an access type. In either case set the type of the
4182 -- literal, for subsequent use in semantic checks.
4184 elsif Present (Underlying_Type (Ctyp))
4185 and then Is_Access_Type (Underlying_Type (Ctyp))
4186 then
4187 if not Is_Private_Type (Ctyp) then
4188 Expr := Make_Null (Sloc (N));
4189 Set_Etype (Expr, Ctyp);
4190 Add_Association
4191 (Component => Component,
4192 Expr => Expr,
4193 Assoc_List => New_Assoc_List);
4195 -- If the component's type is private with an access type as
4196 -- its underlying type then we have to create an unchecked
4197 -- conversion to satisfy type checking.
4199 else
4200 declare
4201 Qual_Null : constant Node_Id :=
4202 Make_Qualified_Expression (Sloc (N),
4203 Subtype_Mark =>
4204 New_Occurrence_Of
4205 (Underlying_Type (Ctyp), Sloc (N)),
4206 Expression => Make_Null (Sloc (N)));
4208 Convert_Null : constant Node_Id :=
4209 Unchecked_Convert_To
4210 (Ctyp, Qual_Null);
4212 begin
4213 Analyze_And_Resolve (Convert_Null, Ctyp);
4214 Add_Association
4215 (Component => Component,
4216 Expr => Convert_Null,
4217 Assoc_List => New_Assoc_List);
4218 end;
4219 end if;
4221 -- Ada 2012: If component is scalar with default value, use it
4223 elsif Is_Scalar_Type (Ctyp)
4224 and then Has_Default_Aspect (Ctyp)
4225 then
4226 Add_Association
4227 (Component => Component,
4228 Expr => Default_Aspect_Value
4229 (First_Subtype (Underlying_Type (Ctyp))),
4230 Assoc_List => New_Assoc_List);
4232 elsif Has_Non_Null_Base_Init_Proc (Ctyp)
4233 or else not Expander_Active
4234 then
4235 if Is_Record_Type (Ctyp)
4236 and then Has_Discriminants (Ctyp)
4237 and then not Is_Private_Type (Ctyp)
4238 then
4239 -- We build a partially initialized aggregate with the
4240 -- values of the discriminants and box initialization
4241 -- for the rest, if other components are present.
4243 -- The type of the aggregate is the known subtype of
4244 -- the component. The capture of discriminants must
4245 -- be recursive because subcomponents may be constrained
4246 -- (transitively) by discriminants of enclosing types.
4247 -- For a private type with discriminants, a call to the
4248 -- initialization procedure will be generated, and no
4249 -- subaggregate is needed.
4251 Capture_Discriminants : declare
4252 Loc : constant Source_Ptr := Sloc (N);
4253 Expr : Node_Id;
4255 procedure Add_Discriminant_Values
4256 (New_Aggr : Node_Id;
4257 Assoc_List : List_Id);
4258 -- The constraint to a component may be given by a
4259 -- discriminant of the enclosing type, in which case
4260 -- we have to retrieve its value, which is part of the
4261 -- enclosing aggregate. Assoc_List provides the
4262 -- discriminant associations of the current type or
4263 -- of some enclosing record.
4265 procedure Propagate_Discriminants
4266 (Aggr : Node_Id;
4267 Assoc_List : List_Id);
4268 -- Nested components may themselves be discriminated
4269 -- types constrained by outer discriminants, whose
4270 -- values must be captured before the aggregate is
4271 -- expanded into assignments.
4273 -----------------------------
4274 -- Add_Discriminant_Values --
4275 -----------------------------
4277 procedure Add_Discriminant_Values
4278 (New_Aggr : Node_Id;
4279 Assoc_List : List_Id)
4281 Assoc : Node_Id;
4282 Discr : Entity_Id;
4283 Discr_Elmt : Elmt_Id;
4284 Discr_Val : Node_Id;
4285 Val : Entity_Id;
4287 begin
4288 Discr := First_Discriminant (Etype (New_Aggr));
4289 Discr_Elmt :=
4290 First_Elmt
4291 (Discriminant_Constraint (Etype (New_Aggr)));
4292 while Present (Discr_Elmt) loop
4293 Discr_Val := Node (Discr_Elmt);
4295 -- If the constraint is given by a discriminant
4296 -- it is a discriminant of an enclosing record,
4297 -- and its value has already been placed in the
4298 -- association list.
4300 if Is_Entity_Name (Discr_Val)
4301 and then
4302 Ekind (Entity (Discr_Val)) = E_Discriminant
4303 then
4304 Val := Entity (Discr_Val);
4306 Assoc := First (Assoc_List);
4307 while Present (Assoc) loop
4308 if Present
4309 (Entity (First (Choices (Assoc))))
4310 and then
4311 Entity (First (Choices (Assoc)))
4312 = Val
4313 then
4314 Discr_Val := Expression (Assoc);
4315 exit;
4316 end if;
4317 Next (Assoc);
4318 end loop;
4319 end if;
4321 Add_Association
4322 (Discr, New_Copy_Tree (Discr_Val),
4323 Component_Associations (New_Aggr));
4325 -- If the discriminant constraint is a current
4326 -- instance, mark the current aggregate so that
4327 -- the self-reference can be expanded later.
4329 if Nkind (Discr_Val) = N_Attribute_Reference
4330 and then Is_Entity_Name (Prefix (Discr_Val))
4331 and then Is_Type (Entity (Prefix (Discr_Val)))
4332 and then Etype (N) =
4333 Entity (Prefix (Discr_Val))
4334 then
4335 Set_Has_Self_Reference (N);
4336 end if;
4338 Next_Elmt (Discr_Elmt);
4339 Next_Discriminant (Discr);
4340 end loop;
4341 end Add_Discriminant_Values;
4343 ------------------------------
4344 -- Propagate_Discriminants --
4345 ------------------------------
4347 procedure Propagate_Discriminants
4348 (Aggr : Node_Id;
4349 Assoc_List : List_Id)
4351 Aggr_Type : constant Entity_Id :=
4352 Base_Type (Etype (Aggr));
4353 Def_Node : constant Node_Id :=
4354 Type_Definition
4355 (Declaration_Node (Aggr_Type));
4357 Comp : Node_Id;
4358 Comp_Elmt : Elmt_Id;
4359 Components : constant Elist_Id := New_Elmt_List;
4360 Needs_Box : Boolean := False;
4361 Errors : Boolean;
4363 procedure Process_Component (Comp : Entity_Id);
4364 -- Add one component with a box association to the
4365 -- inner aggregate, and recurse if component is
4366 -- itself composite.
4368 ------------------------
4369 -- Process_Component --
4370 ------------------------
4372 procedure Process_Component (Comp : Entity_Id) is
4373 T : constant Entity_Id := Etype (Comp);
4374 New_Aggr : Node_Id;
4376 begin
4377 if Is_Record_Type (T)
4378 and then Has_Discriminants (T)
4379 then
4380 New_Aggr :=
4381 Make_Aggregate (Loc, New_List, New_List);
4382 Set_Etype (New_Aggr, T);
4383 Add_Association
4384 (Comp, New_Aggr,
4385 Component_Associations (Aggr));
4387 -- Collect discriminant values and recurse
4389 Add_Discriminant_Values
4390 (New_Aggr, Assoc_List);
4391 Propagate_Discriminants
4392 (New_Aggr, Assoc_List);
4394 else
4395 Needs_Box := True;
4396 end if;
4397 end Process_Component;
4399 -- Start of processing for Propagate_Discriminants
4401 begin
4402 -- The component type may be a variant type, so
4403 -- collect the components that are ruled by the
4404 -- known values of the discriminants. Their values
4405 -- have already been inserted into the component
4406 -- list of the current aggregate.
4408 if Nkind (Def_Node) = N_Record_Definition
4409 and then
4410 Present (Component_List (Def_Node))
4411 and then
4412 Present
4413 (Variant_Part (Component_List (Def_Node)))
4414 then
4415 Gather_Components (Aggr_Type,
4416 Component_List (Def_Node),
4417 Governed_By => Component_Associations (Aggr),
4418 Into => Components,
4419 Report_Errors => Errors);
4421 Comp_Elmt := First_Elmt (Components);
4422 while Present (Comp_Elmt) loop
4424 Ekind (Node (Comp_Elmt)) /= E_Discriminant
4425 then
4426 Process_Component (Node (Comp_Elmt));
4427 end if;
4429 Next_Elmt (Comp_Elmt);
4430 end loop;
4432 -- No variant part, iterate over all components
4434 else
4435 Comp := First_Component (Etype (Aggr));
4436 while Present (Comp) loop
4437 Process_Component (Comp);
4438 Next_Component (Comp);
4439 end loop;
4440 end if;
4442 if Needs_Box then
4443 Append_To (Component_Associations (Aggr),
4444 Make_Component_Association (Loc,
4445 Choices =>
4446 New_List (Make_Others_Choice (Loc)),
4447 Expression => Empty,
4448 Box_Present => True));
4449 end if;
4450 end Propagate_Discriminants;
4452 -- Start of processing for Capture_Discriminants
4454 begin
4455 Expr := Make_Aggregate (Loc, New_List, New_List);
4456 Set_Etype (Expr, Ctyp);
4458 -- If the enclosing type has discriminants, they have
4459 -- been collected in the aggregate earlier, and they
4460 -- may appear as constraints of subcomponents.
4462 -- Similarly if this component has discriminants, they
4463 -- might in turn be propagated to their components.
4465 if Has_Discriminants (Typ) then
4466 Add_Discriminant_Values (Expr, New_Assoc_List);
4467 Propagate_Discriminants (Expr, New_Assoc_List);
4469 elsif Has_Discriminants (Ctyp) then
4470 Add_Discriminant_Values
4471 (Expr, Component_Associations (Expr));
4472 Propagate_Discriminants
4473 (Expr, Component_Associations (Expr));
4475 else
4476 declare
4477 Comp : Entity_Id;
4479 begin
4480 -- If the type has additional components, create
4481 -- an OTHERS box association for them.
4483 Comp := First_Component (Ctyp);
4484 while Present (Comp) loop
4485 if Ekind (Comp) = E_Component then
4486 if not Is_Record_Type (Etype (Comp)) then
4487 Append_To
4488 (Component_Associations (Expr),
4489 Make_Component_Association (Loc,
4490 Choices =>
4491 New_List
4492 (Make_Others_Choice (Loc)),
4493 Expression => Empty,
4494 Box_Present => True));
4495 end if;
4496 exit;
4497 end if;
4499 Next_Component (Comp);
4500 end loop;
4501 end;
4502 end if;
4504 Add_Association
4505 (Component => Component,
4506 Expr => Expr,
4507 Assoc_List => New_Assoc_List);
4508 end Capture_Discriminants;
4510 else
4511 Add_Association
4512 (Component => Component,
4513 Expr => Empty,
4514 Assoc_List => New_Assoc_List,
4515 Is_Box_Present => True);
4516 end if;
4518 -- Otherwise we only need to resolve the expression if the
4519 -- component has partially initialized values (required to
4520 -- expand the corresponding assignments and run-time checks).
4522 elsif Present (Expr)
4523 and then Is_Partially_Initialized_Type (Ctyp)
4524 then
4525 Resolve_Aggr_Expr (Expr, Component);
4526 end if;
4527 end Check_Box_Component;
4529 elsif No (Expr) then
4531 -- Ignore hidden components associated with the position of the
4532 -- interface tags: these are initialized dynamically.
4534 if not Present (Related_Type (Component)) then
4535 Error_Msg_NE
4536 ("no value supplied for component &!", N, Component);
4537 end if;
4539 else
4540 Resolve_Aggr_Expr (Expr, Component);
4541 end if;
4543 Next_Elmt (Component_Elmt);
4544 end loop;
4546 -- STEP 7: check for invalid components + check type in choice list
4548 Step_7 : declare
4549 Selectr : Node_Id;
4550 -- Selector name
4552 Typech : Entity_Id;
4553 -- Type of first component in choice list
4555 begin
4556 if Present (Component_Associations (N)) then
4557 Assoc := First (Component_Associations (N));
4558 else
4559 Assoc := Empty;
4560 end if;
4562 Verification : while Present (Assoc) loop
4563 Selectr := First (Choices (Assoc));
4564 Typech := Empty;
4566 if Nkind (Selectr) = N_Others_Choice then
4568 -- Ada 2005 (AI-287): others choice may have expression or box
4570 if No (Others_Etype)
4571 and then not Others_Box
4572 then
4573 Error_Msg_N
4574 ("OTHERS must represent at least one component", Selectr);
4575 end if;
4577 exit Verification;
4578 end if;
4580 while Present (Selectr) loop
4581 New_Assoc := First (New_Assoc_List);
4582 while Present (New_Assoc) loop
4583 Component := First (Choices (New_Assoc));
4585 if Chars (Selectr) = Chars (Component) then
4586 if Style_Check then
4587 Check_Identifier (Selectr, Entity (Component));
4588 end if;
4590 exit;
4591 end if;
4593 Next (New_Assoc);
4594 end loop;
4596 -- If no association, this is not a legal component of the type
4597 -- in question, unless its association is provided with a box.
4599 if No (New_Assoc) then
4600 if Box_Present (Parent (Selectr)) then
4602 -- This may still be a bogus component with a box. Scan
4603 -- list of components to verify that a component with
4604 -- that name exists.
4606 declare
4607 C : Entity_Id;
4609 begin
4610 C := First_Component (Typ);
4611 while Present (C) loop
4612 if Chars (C) = Chars (Selectr) then
4614 -- If the context is an extension aggregate,
4615 -- the component must not be inherited from
4616 -- the ancestor part of the aggregate.
4618 if Nkind (N) /= N_Extension_Aggregate
4619 or else
4620 Scope (Original_Record_Component (C)) /=
4621 Etype (Ancestor_Part (N))
4622 then
4623 exit;
4624 end if;
4625 end if;
4627 Next_Component (C);
4628 end loop;
4630 if No (C) then
4631 Error_Msg_Node_2 := Typ;
4632 Error_Msg_N ("& is not a component of}", Selectr);
4633 end if;
4634 end;
4636 elsif Chars (Selectr) /= Name_uTag
4637 and then Chars (Selectr) /= Name_uParent
4638 then
4639 if not Has_Discriminants (Typ) then
4640 Error_Msg_Node_2 := Typ;
4641 Error_Msg_N ("& is not a component of}", Selectr);
4642 else
4643 Error_Msg_N
4644 ("& is not a component of the aggregate subtype",
4645 Selectr);
4646 end if;
4648 Check_Misspelled_Component (Components, Selectr);
4649 end if;
4651 elsif No (Typech) then
4652 Typech := Base_Type (Etype (Component));
4654 -- AI05-0199: In Ada 2012, several components of anonymous
4655 -- access types can appear in a choice list, as long as the
4656 -- designated types match.
4658 elsif Typech /= Base_Type (Etype (Component)) then
4659 if Ada_Version >= Ada_2012
4660 and then Ekind (Typech) = E_Anonymous_Access_Type
4661 and then
4662 Ekind (Etype (Component)) = E_Anonymous_Access_Type
4663 and then Base_Type (Designated_Type (Typech)) =
4664 Base_Type (Designated_Type (Etype (Component)))
4665 and then
4666 Subtypes_Statically_Match (Typech, (Etype (Component)))
4667 then
4668 null;
4670 elsif not Box_Present (Parent (Selectr)) then
4671 Error_Msg_N
4672 ("components in choice list must have same type",
4673 Selectr);
4674 end if;
4675 end if;
4677 Next (Selectr);
4678 end loop;
4680 Next (Assoc);
4681 end loop Verification;
4682 end Step_7;
4684 -- STEP 8: replace the original aggregate
4686 Step_8 : declare
4687 New_Aggregate : constant Node_Id := New_Copy (N);
4689 begin
4690 Set_Expressions (New_Aggregate, No_List);
4691 Set_Etype (New_Aggregate, Etype (N));
4692 Set_Component_Associations (New_Aggregate, New_Assoc_List);
4694 Rewrite (N, New_Aggregate);
4695 end Step_8;
4697 -- Check the dimensions of the components in the record aggregate
4699 Analyze_Dimension_Extension_Or_Record_Aggregate (N);
4700 end Resolve_Record_Aggregate;
4702 -----------------------------
4703 -- Check_Can_Never_Be_Null --
4704 -----------------------------
4706 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
4707 Comp_Typ : Entity_Id;
4709 begin
4710 pragma Assert
4711 (Ada_Version >= Ada_2005
4712 and then Present (Expr)
4713 and then Known_Null (Expr));
4715 case Ekind (Typ) is
4716 when E_Array_Type =>
4717 Comp_Typ := Component_Type (Typ);
4719 when E_Component |
4720 E_Discriminant =>
4721 Comp_Typ := Etype (Typ);
4723 when others =>
4724 return;
4725 end case;
4727 if Can_Never_Be_Null (Comp_Typ) then
4729 -- Here we know we have a constraint error. Note that we do not use
4730 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
4731 -- seem the more natural approach. That's because in some cases the
4732 -- components are rewritten, and the replacement would be missed.
4733 -- We do not mark the whole aggregate as raising a constraint error,
4734 -- because the association may be a null array range.
4736 Error_Msg_N
4737 ("(Ada 2005) null not allowed in null-excluding component??", Expr);
4738 Error_Msg_N
4739 ("\Constraint_Error will be raised at run time??", Expr);
4741 Rewrite (Expr,
4742 Make_Raise_Constraint_Error
4743 (Sloc (Expr), Reason => CE_Access_Check_Failed));
4744 Set_Etype (Expr, Comp_Typ);
4745 Set_Analyzed (Expr);
4746 end if;
4747 end Check_Can_Never_Be_Null;
4749 ---------------------
4750 -- Sort_Case_Table --
4751 ---------------------
4753 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
4754 U : constant Int := Case_Table'Last;
4755 K : Int;
4756 J : Int;
4757 T : Case_Bounds;
4759 begin
4760 K := 1;
4761 while K < U loop
4762 T := Case_Table (K + 1);
4764 J := K + 1;
4765 while J > 1
4766 and then Expr_Value (Case_Table (J - 1).Lo) > Expr_Value (T.Lo)
4767 loop
4768 Case_Table (J) := Case_Table (J - 1);
4769 J := J - 1;
4770 end loop;
4772 Case_Table (J) := T;
4773 K := K + 1;
4774 end loop;
4775 end Sort_Case_Table;
4777 end Sem_Aggr;