Add Knights Landing support to __builtin_cpu_is
[official-gcc.git] / gcc / ada / sem_aggr.adb
blob5300d3ab87fb939cc48dc7937a40feb359e15cd5
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-2015, 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
611 (Itype,
612 Is_Fully_Positional
613 and then Comes_From_Source (N)
614 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
616 -- We always need a freeze node for a packed array subtype, so that we
617 -- can build the Packed_Array_Impl_Type corresponding to the subtype. If
618 -- expansion is disabled, the packed array subtype is not built, and we
619 -- must not generate a freeze node for the type, or else it will appear
620 -- incomplete to gigi.
622 if Is_Packed (Itype)
623 and then not In_Spec_Expression
624 and then Expander_Active
625 then
626 Freeze_Itype (Itype, N);
627 end if;
629 return Itype;
630 end Array_Aggr_Subtype;
632 --------------------------------
633 -- Check_Misspelled_Component --
634 --------------------------------
636 procedure Check_Misspelled_Component
637 (Elements : Elist_Id;
638 Component : Node_Id)
640 Max_Suggestions : constant := 2;
642 Nr_Of_Suggestions : Natural := 0;
643 Suggestion_1 : Entity_Id := Empty;
644 Suggestion_2 : Entity_Id := Empty;
645 Component_Elmt : Elmt_Id;
647 begin
648 -- All the components of List are matched against Component and a count
649 -- is maintained of possible misspellings. When at the end of the the
650 -- analysis there are one or two (not more) possible misspellings,
651 -- these misspellings will be suggested as possible correction.
653 Component_Elmt := First_Elmt (Elements);
654 while Nr_Of_Suggestions <= Max_Suggestions
655 and then Present (Component_Elmt)
656 loop
657 if Is_Bad_Spelling_Of
658 (Chars (Node (Component_Elmt)),
659 Chars (Component))
660 then
661 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
663 case Nr_Of_Suggestions is
664 when 1 => Suggestion_1 := Node (Component_Elmt);
665 when 2 => Suggestion_2 := Node (Component_Elmt);
666 when others => exit;
667 end case;
668 end if;
670 Next_Elmt (Component_Elmt);
671 end loop;
673 -- Report at most two suggestions
675 if Nr_Of_Suggestions = 1 then
676 Error_Msg_NE -- CODEFIX
677 ("\possible misspelling of&", Component, Suggestion_1);
679 elsif Nr_Of_Suggestions = 2 then
680 Error_Msg_Node_2 := Suggestion_2;
681 Error_Msg_NE -- CODEFIX
682 ("\possible misspelling of& or&", Component, Suggestion_1);
683 end if;
684 end Check_Misspelled_Component;
686 ----------------------------------------
687 -- Check_Expr_OK_In_Limited_Aggregate --
688 ----------------------------------------
690 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id) is
691 begin
692 if Is_Limited_Type (Etype (Expr))
693 and then Comes_From_Source (Expr)
694 then
695 if In_Instance_Body or else In_Inlined_Body then
696 null;
698 elsif not OK_For_Limited_Init (Etype (Expr), Expr) then
699 Error_Msg_N
700 ("initialization not allowed for limited types", Expr);
701 Explain_Limited_Type (Etype (Expr), Expr);
702 end if;
703 end if;
704 end Check_Expr_OK_In_Limited_Aggregate;
706 -------------------------------
707 -- Check_Qualified_Aggregate --
708 -------------------------------
710 procedure Check_Qualified_Aggregate (Level : Nat; Expr : Node_Id) is
711 Comp_Expr : Node_Id;
712 Comp_Assn : Node_Id;
714 begin
715 if Level = 0 then
716 if Nkind (Parent (Expr)) /= N_Qualified_Expression then
717 Check_SPARK_05_Restriction ("aggregate should be qualified", Expr);
718 end if;
720 else
721 Comp_Expr := First (Expressions (Expr));
722 while Present (Comp_Expr) loop
723 if Nkind (Comp_Expr) = N_Aggregate then
724 Check_Qualified_Aggregate (Level - 1, Comp_Expr);
725 end if;
727 Comp_Expr := Next (Comp_Expr);
728 end loop;
730 Comp_Assn := First (Component_Associations (Expr));
731 while Present (Comp_Assn) loop
732 Comp_Expr := Expression (Comp_Assn);
734 if Nkind (Comp_Expr) = N_Aggregate then
735 Check_Qualified_Aggregate (Level - 1, Comp_Expr);
736 end if;
738 Comp_Assn := Next (Comp_Assn);
739 end loop;
740 end if;
741 end Check_Qualified_Aggregate;
743 ----------------------------------------
744 -- Check_Static_Discriminated_Subtype --
745 ----------------------------------------
747 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id) is
748 Disc : constant Entity_Id := First_Discriminant (T);
749 Comp : Entity_Id;
750 Ind : Entity_Id;
752 begin
753 if Has_Record_Rep_Clause (T) then
754 return;
756 elsif Present (Next_Discriminant (Disc)) then
757 return;
759 elsif Nkind (V) /= N_Integer_Literal then
760 return;
761 end if;
763 Comp := First_Component (T);
764 while Present (Comp) loop
765 if Is_Scalar_Type (Etype (Comp)) then
766 null;
768 elsif Is_Private_Type (Etype (Comp))
769 and then Present (Full_View (Etype (Comp)))
770 and then Is_Scalar_Type (Full_View (Etype (Comp)))
771 then
772 null;
774 elsif Is_Array_Type (Etype (Comp)) then
775 if Is_Bit_Packed_Array (Etype (Comp)) then
776 return;
777 end if;
779 Ind := First_Index (Etype (Comp));
780 while Present (Ind) loop
781 if Nkind (Ind) /= N_Range
782 or else Nkind (Low_Bound (Ind)) /= N_Integer_Literal
783 or else Nkind (High_Bound (Ind)) /= N_Integer_Literal
784 then
785 return;
786 end if;
788 Next_Index (Ind);
789 end loop;
791 else
792 return;
793 end if;
795 Next_Component (Comp);
796 end loop;
798 -- On exit, all components have statically known sizes
800 Set_Size_Known_At_Compile_Time (T);
801 end Check_Static_Discriminated_Subtype;
803 -------------------------
804 -- Is_Others_Aggregate --
805 -------------------------
807 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean is
808 begin
809 return No (Expressions (Aggr))
810 and then
811 Nkind (First (Choices (First (Component_Associations (Aggr))))) =
812 N_Others_Choice;
813 end Is_Others_Aggregate;
815 ----------------------------
816 -- Is_Top_Level_Aggregate --
817 ----------------------------
819 function Is_Top_Level_Aggregate (Expr : Node_Id) return Boolean is
820 begin
821 return Nkind (Parent (Expr)) /= N_Aggregate
822 and then (Nkind (Parent (Expr)) /= N_Component_Association
823 or else Nkind (Parent (Parent (Expr))) /= N_Aggregate);
824 end Is_Top_Level_Aggregate;
826 --------------------------------
827 -- Make_String_Into_Aggregate --
828 --------------------------------
830 procedure Make_String_Into_Aggregate (N : Node_Id) is
831 Exprs : constant List_Id := New_List;
832 Loc : constant Source_Ptr := Sloc (N);
833 Str : constant String_Id := Strval (N);
834 Strlen : constant Nat := String_Length (Str);
835 C : Char_Code;
836 C_Node : Node_Id;
837 New_N : Node_Id;
838 P : Source_Ptr;
840 begin
841 P := Loc + 1;
842 for J in 1 .. Strlen loop
843 C := Get_String_Char (Str, J);
844 Set_Character_Literal_Name (C);
846 C_Node :=
847 Make_Character_Literal (P,
848 Chars => Name_Find,
849 Char_Literal_Value => UI_From_CC (C));
850 Set_Etype (C_Node, Any_Character);
851 Append_To (Exprs, C_Node);
853 P := P + 1;
854 -- Something special for wide strings???
855 end loop;
857 New_N := Make_Aggregate (Loc, Expressions => Exprs);
858 Set_Analyzed (New_N);
859 Set_Etype (New_N, Any_Composite);
861 Rewrite (N, New_N);
862 end Make_String_Into_Aggregate;
864 -----------------------
865 -- Resolve_Aggregate --
866 -----------------------
868 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
869 Loc : constant Source_Ptr := Sloc (N);
870 Pkind : constant Node_Kind := Nkind (Parent (N));
872 Aggr_Subtyp : Entity_Id;
873 -- The actual aggregate subtype. This is not necessarily the same as Typ
874 -- which is the subtype of the context in which the aggregate was found.
876 begin
877 -- Ignore junk empty aggregate resulting from parser error
879 if No (Expressions (N))
880 and then No (Component_Associations (N))
881 and then not Null_Record_Present (N)
882 then
883 return;
884 end if;
886 -- If the aggregate has box-initialized components, its type must be
887 -- frozen so that initialization procedures can properly be called
888 -- in the resolution that follows. The replacement of boxes with
889 -- initialization calls is properly an expansion activity but it must
890 -- be done during resolution.
892 if Expander_Active
893 and then Present (Component_Associations (N))
894 then
895 declare
896 Comp : Node_Id;
898 begin
899 Comp := First (Component_Associations (N));
900 while Present (Comp) loop
901 if Box_Present (Comp) then
902 Insert_Actions (N, Freeze_Entity (Typ, N));
903 exit;
904 end if;
906 Next (Comp);
907 end loop;
908 end;
909 end if;
911 -- An unqualified aggregate is restricted in SPARK to:
913 -- An aggregate item inside an aggregate for a multi-dimensional array
915 -- An expression being assigned to an unconstrained array, but only if
916 -- the aggregate specifies a value for OTHERS only.
918 if Nkind (Parent (N)) = N_Qualified_Expression then
919 if Is_Array_Type (Typ) then
920 Check_Qualified_Aggregate (Number_Dimensions (Typ), N);
921 else
922 Check_Qualified_Aggregate (1, N);
923 end if;
924 else
925 if Is_Array_Type (Typ)
926 and then Nkind (Parent (N)) = N_Assignment_Statement
927 and then not Is_Constrained (Etype (Name (Parent (N))))
928 then
929 if not Is_Others_Aggregate (N) then
930 Check_SPARK_05_Restriction
931 ("array aggregate should have only OTHERS", N);
932 end if;
934 elsif Is_Top_Level_Aggregate (N) then
935 Check_SPARK_05_Restriction ("aggregate should be qualified", N);
937 -- The legality of this unqualified aggregate is checked by calling
938 -- Check_Qualified_Aggregate from one of its enclosing aggregate,
939 -- unless one of these already causes an error to be issued.
941 else
942 null;
943 end if;
944 end if;
946 -- Check for aggregates not allowed in configurable run-time mode.
947 -- We allow all cases of aggregates that do not come from source, since
948 -- these are all assumed to be small (e.g. bounds of a string literal).
949 -- We also allow aggregates of types we know to be small.
951 if not Support_Aggregates_On_Target
952 and then Comes_From_Source (N)
953 and then (not Known_Static_Esize (Typ) or else Esize (Typ) > 64)
954 then
955 Error_Msg_CRT ("aggregate", N);
956 end if;
958 -- Ada 2005 (AI-287): Limited aggregates allowed
960 -- In an instance, ignore aggregate subcomponents tnat may be limited,
961 -- because they originate in view conflicts. If the original aggregate
962 -- is legal and the actuals are legal, the aggregate itself is legal.
964 if Is_Limited_Type (Typ)
965 and then Ada_Version < Ada_2005
966 and then not In_Instance
967 then
968 Error_Msg_N ("aggregate type cannot be limited", N);
969 Explain_Limited_Type (Typ, N);
971 elsif Is_Class_Wide_Type (Typ) then
972 Error_Msg_N ("type of aggregate cannot be class-wide", N);
974 elsif Typ = Any_String
975 or else Typ = Any_Composite
976 then
977 Error_Msg_N ("no unique type for aggregate", N);
978 Set_Etype (N, Any_Composite);
980 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
981 Error_Msg_N ("null record forbidden in array aggregate", N);
983 elsif Is_Record_Type (Typ) then
984 Resolve_Record_Aggregate (N, Typ);
986 elsif Is_Array_Type (Typ) then
988 -- First a special test, for the case of a positional aggregate
989 -- of characters which can be replaced by a string literal.
991 -- Do not perform this transformation if this was a string literal to
992 -- start with, whose components needed constraint checks, or if the
993 -- component type is non-static, because it will require those checks
994 -- and be transformed back into an aggregate.
996 if Number_Dimensions (Typ) = 1
997 and then Is_Standard_Character_Type (Component_Type (Typ))
998 and then No (Component_Associations (N))
999 and then not Is_Limited_Composite (Typ)
1000 and then not Is_Private_Composite (Typ)
1001 and then not Is_Bit_Packed_Array (Typ)
1002 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
1003 and then Is_OK_Static_Subtype (Component_Type (Typ))
1004 then
1005 declare
1006 Expr : Node_Id;
1008 begin
1009 Expr := First (Expressions (N));
1010 while Present (Expr) loop
1011 exit when Nkind (Expr) /= N_Character_Literal;
1012 Next (Expr);
1013 end loop;
1015 if No (Expr) then
1016 Start_String;
1018 Expr := First (Expressions (N));
1019 while Present (Expr) loop
1020 Store_String_Char (UI_To_CC (Char_Literal_Value (Expr)));
1021 Next (Expr);
1022 end loop;
1024 Rewrite (N, Make_String_Literal (Loc, End_String));
1026 Analyze_And_Resolve (N, Typ);
1027 return;
1028 end if;
1029 end;
1030 end if;
1032 -- Here if we have a real aggregate to deal with
1034 Array_Aggregate : declare
1035 Aggr_Resolved : Boolean;
1037 Aggr_Typ : constant Entity_Id := Etype (Typ);
1038 -- This is the unconstrained array type, which is the type against
1039 -- which the aggregate is to be resolved. Typ itself is the array
1040 -- type of the context which may not be the same subtype as the
1041 -- subtype for the final aggregate.
1043 begin
1044 -- In the following we determine whether an OTHERS choice is
1045 -- allowed inside the array aggregate. The test checks the context
1046 -- in which the array aggregate occurs. If the context does not
1047 -- permit it, or the aggregate type is unconstrained, an OTHERS
1048 -- choice is not allowed (except that it is always allowed on the
1049 -- right-hand side of an assignment statement; in this case the
1050 -- constrainedness of the type doesn't matter).
1052 -- If expansion is disabled (generic context, or semantics-only
1053 -- mode) actual subtypes cannot be constructed, and the type of an
1054 -- object may be its unconstrained nominal type. However, if the
1055 -- context is an assignment, we assume that OTHERS is allowed,
1056 -- because the target of the assignment will have a constrained
1057 -- subtype when fully compiled.
1059 -- Note that there is no node for Explicit_Actual_Parameter.
1060 -- To test for this context we therefore have to test for node
1061 -- N_Parameter_Association which itself appears only if there is a
1062 -- formal parameter. Consequently we also need to test for
1063 -- N_Procedure_Call_Statement or N_Function_Call.
1065 -- The context may be an N_Reference node, created by expansion.
1066 -- Legality of the others clause was established in the source,
1067 -- so the context is legal.
1069 Set_Etype (N, Aggr_Typ); -- May be overridden later on
1071 if Pkind = N_Assignment_Statement
1072 or else (Is_Constrained (Typ)
1073 and then
1074 (Pkind = N_Parameter_Association or else
1075 Pkind = N_Function_Call or else
1076 Pkind = N_Procedure_Call_Statement or else
1077 Pkind = N_Generic_Association or else
1078 Pkind = N_Formal_Object_Declaration or else
1079 Pkind = N_Simple_Return_Statement or else
1080 Pkind = N_Object_Declaration or else
1081 Pkind = N_Component_Declaration or else
1082 Pkind = N_Parameter_Specification or else
1083 Pkind = N_Qualified_Expression or else
1084 Pkind = N_Reference or else
1085 Pkind = N_Aggregate or else
1086 Pkind = N_Extension_Aggregate or else
1087 Pkind = N_Component_Association))
1088 then
1089 Aggr_Resolved :=
1090 Resolve_Array_Aggregate
1092 Index => First_Index (Aggr_Typ),
1093 Index_Constr => First_Index (Typ),
1094 Component_Typ => Component_Type (Typ),
1095 Others_Allowed => True);
1097 elsif not Expander_Active
1098 and then Pkind = N_Assignment_Statement
1099 then
1100 Aggr_Resolved :=
1101 Resolve_Array_Aggregate
1103 Index => First_Index (Aggr_Typ),
1104 Index_Constr => First_Index (Typ),
1105 Component_Typ => Component_Type (Typ),
1106 Others_Allowed => True);
1108 else
1109 Aggr_Resolved :=
1110 Resolve_Array_Aggregate
1112 Index => First_Index (Aggr_Typ),
1113 Index_Constr => First_Index (Aggr_Typ),
1114 Component_Typ => Component_Type (Typ),
1115 Others_Allowed => False);
1116 end if;
1118 if not Aggr_Resolved then
1120 -- A parenthesized expression may have been intended as an
1121 -- aggregate, leading to a type error when analyzing the
1122 -- component. This can also happen for a nested component
1123 -- (see Analyze_Aggr_Expr).
1125 if Paren_Count (N) > 0 then
1126 Error_Msg_N
1127 ("positional aggregate cannot have one component", N);
1128 end if;
1130 Aggr_Subtyp := Any_Composite;
1132 else
1133 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1134 end if;
1136 Set_Etype (N, Aggr_Subtyp);
1137 end Array_Aggregate;
1139 elsif Is_Private_Type (Typ)
1140 and then Present (Full_View (Typ))
1141 and then (In_Inlined_Body or In_Instance_Body)
1142 and then Is_Composite_Type (Full_View (Typ))
1143 then
1144 Resolve (N, Full_View (Typ));
1146 else
1147 Error_Msg_N ("illegal context for aggregate", N);
1148 end if;
1150 -- If we can determine statically that the evaluation of the aggregate
1151 -- raises Constraint_Error, then replace the aggregate with an
1152 -- N_Raise_Constraint_Error node, but set the Etype to the right
1153 -- aggregate subtype. Gigi needs this.
1155 if Raises_Constraint_Error (N) then
1156 Aggr_Subtyp := Etype (N);
1157 Rewrite (N,
1158 Make_Raise_Constraint_Error (Loc, Reason => CE_Range_Check_Failed));
1159 Set_Raises_Constraint_Error (N);
1160 Set_Etype (N, Aggr_Subtyp);
1161 Set_Analyzed (N);
1162 end if;
1164 Check_Function_Writable_Actuals (N);
1165 end Resolve_Aggregate;
1167 -----------------------------
1168 -- Resolve_Array_Aggregate --
1169 -----------------------------
1171 function Resolve_Array_Aggregate
1172 (N : Node_Id;
1173 Index : Node_Id;
1174 Index_Constr : Node_Id;
1175 Component_Typ : Entity_Id;
1176 Others_Allowed : Boolean) return Boolean
1178 Loc : constant Source_Ptr := Sloc (N);
1180 Failure : constant Boolean := False;
1181 Success : constant Boolean := True;
1183 Index_Typ : constant Entity_Id := Etype (Index);
1184 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1185 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1186 -- The type of the index corresponding to the array sub-aggregate along
1187 -- with its low and upper bounds.
1189 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1190 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1191 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1192 -- Ditto for the base type
1194 function Add (Val : Uint; To : Node_Id) return Node_Id;
1195 -- Creates a new expression node where Val is added to expression To.
1196 -- Tries to constant fold whenever possible. To must be an already
1197 -- analyzed expression.
1199 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1200 -- Checks that AH (the upper bound of an array aggregate) is less than
1201 -- or equal to BH (the upper bound of the index base type). If the check
1202 -- fails, a warning is emitted, the Raises_Constraint_Error flag of N is
1203 -- set, and AH is replaced with a duplicate of BH.
1205 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1206 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1207 -- warning if not and sets the Raises_Constraint_Error flag in N.
1209 procedure Check_Length (L, H : Node_Id; Len : Uint);
1210 -- Checks that range L .. H contains at least Len elements. Emits a
1211 -- warning if not and sets the Raises_Constraint_Error flag in N.
1213 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1214 -- Returns True if range L .. H is dynamic or null
1216 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1217 -- Given expression node From, this routine sets OK to False if it
1218 -- cannot statically evaluate From. Otherwise it stores this static
1219 -- value into Value.
1221 function Resolve_Aggr_Expr
1222 (Expr : Node_Id;
1223 Single_Elmt : Boolean) return Boolean;
1224 -- Resolves aggregate expression Expr. Returns False if resolution
1225 -- fails. If Single_Elmt is set to False, the expression Expr may be
1226 -- used to initialize several array aggregate elements (this can happen
1227 -- for discrete choices such as "L .. H => Expr" or the OTHERS choice).
1228 -- In this event we do not resolve Expr unless expansion is disabled.
1229 -- To know why, see the DELAYED COMPONENT RESOLUTION note above.
1231 -- NOTE: In the case of "... => <>", we pass the in the
1232 -- N_Component_Association node as Expr, since there is no Expression in
1233 -- that case, and we need a Sloc for the error message.
1235 ---------
1236 -- Add --
1237 ---------
1239 function Add (Val : Uint; To : Node_Id) return Node_Id is
1240 Expr_Pos : Node_Id;
1241 Expr : Node_Id;
1242 To_Pos : Node_Id;
1244 begin
1245 if Raises_Constraint_Error (To) then
1246 return To;
1247 end if;
1249 -- First test if we can do constant folding
1251 if Compile_Time_Known_Value (To)
1252 or else Nkind (To) = N_Integer_Literal
1253 then
1254 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1255 Set_Is_Static_Expression (Expr_Pos);
1256 Set_Etype (Expr_Pos, Etype (To));
1257 Set_Analyzed (Expr_Pos, Analyzed (To));
1259 if not Is_Enumeration_Type (Index_Typ) then
1260 Expr := Expr_Pos;
1262 -- If we are dealing with enumeration return
1263 -- Index_Typ'Val (Expr_Pos)
1265 else
1266 Expr :=
1267 Make_Attribute_Reference
1268 (Loc,
1269 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1270 Attribute_Name => Name_Val,
1271 Expressions => New_List (Expr_Pos));
1272 end if;
1274 return Expr;
1275 end if;
1277 -- If we are here no constant folding possible
1279 if not Is_Enumeration_Type (Index_Base) then
1280 Expr :=
1281 Make_Op_Add (Loc,
1282 Left_Opnd => Duplicate_Subexpr (To),
1283 Right_Opnd => Make_Integer_Literal (Loc, Val));
1285 -- If we are dealing with enumeration return
1286 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1288 else
1289 To_Pos :=
1290 Make_Attribute_Reference
1291 (Loc,
1292 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1293 Attribute_Name => Name_Pos,
1294 Expressions => New_List (Duplicate_Subexpr (To)));
1296 Expr_Pos :=
1297 Make_Op_Add (Loc,
1298 Left_Opnd => To_Pos,
1299 Right_Opnd => Make_Integer_Literal (Loc, Val));
1301 Expr :=
1302 Make_Attribute_Reference
1303 (Loc,
1304 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1305 Attribute_Name => Name_Val,
1306 Expressions => New_List (Expr_Pos));
1308 -- If the index type has a non standard representation, the
1309 -- attributes 'Val and 'Pos expand into function calls and the
1310 -- resulting expression is considered non-safe for reevaluation
1311 -- by the backend. Relocate it into a constant temporary in order
1312 -- to make it safe for reevaluation.
1314 if Has_Non_Standard_Rep (Etype (N)) then
1315 declare
1316 Def_Id : Entity_Id;
1318 begin
1319 Def_Id := Make_Temporary (Loc, 'R', Expr);
1320 Set_Etype (Def_Id, Index_Typ);
1321 Insert_Action (N,
1322 Make_Object_Declaration (Loc,
1323 Defining_Identifier => Def_Id,
1324 Object_Definition =>
1325 New_Occurrence_Of (Index_Typ, Loc),
1326 Constant_Present => True,
1327 Expression => Relocate_Node (Expr)));
1329 Expr := New_Occurrence_Of (Def_Id, Loc);
1330 end;
1331 end if;
1332 end if;
1334 return Expr;
1335 end Add;
1337 -----------------
1338 -- Check_Bound --
1339 -----------------
1341 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1342 Val_BH : Uint;
1343 Val_AH : Uint;
1345 OK_BH : Boolean;
1346 OK_AH : Boolean;
1348 begin
1349 Get (Value => Val_BH, From => BH, OK => OK_BH);
1350 Get (Value => Val_AH, From => AH, OK => OK_AH);
1352 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1353 Set_Raises_Constraint_Error (N);
1354 Error_Msg_Warn := SPARK_Mode /= On;
1355 Error_Msg_N ("upper bound out of range<<", AH);
1356 Error_Msg_N ("\Constraint_Error [<<", AH);
1358 -- You need to set AH to BH or else in the case of enumerations
1359 -- indexes we will not be able to resolve the aggregate bounds.
1361 AH := Duplicate_Subexpr (BH);
1362 end if;
1363 end Check_Bound;
1365 ------------------
1366 -- Check_Bounds --
1367 ------------------
1369 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1370 Val_L : Uint;
1371 Val_H : Uint;
1372 Val_AL : Uint;
1373 Val_AH : Uint;
1375 OK_L : Boolean;
1376 OK_H : Boolean;
1378 OK_AL : Boolean;
1379 OK_AH : Boolean;
1380 pragma Warnings (Off, OK_AL);
1381 pragma Warnings (Off, OK_AH);
1383 begin
1384 if Raises_Constraint_Error (N)
1385 or else Dynamic_Or_Null_Range (AL, AH)
1386 then
1387 return;
1388 end if;
1390 Get (Value => Val_L, From => L, OK => OK_L);
1391 Get (Value => Val_H, From => H, OK => OK_H);
1393 Get (Value => Val_AL, From => AL, OK => OK_AL);
1394 Get (Value => Val_AH, From => AH, OK => OK_AH);
1396 if OK_L and then Val_L > Val_AL then
1397 Set_Raises_Constraint_Error (N);
1398 Error_Msg_Warn := SPARK_Mode /= On;
1399 Error_Msg_N ("lower bound of aggregate out of range<<", N);
1400 Error_Msg_N ("\Constraint_Error [<<", N);
1401 end if;
1403 if OK_H and then Val_H < Val_AH then
1404 Set_Raises_Constraint_Error (N);
1405 Error_Msg_Warn := SPARK_Mode /= On;
1406 Error_Msg_N ("upper bound of aggregate out of range<<", N);
1407 Error_Msg_N ("\Constraint_Error [<<", N);
1408 end if;
1409 end Check_Bounds;
1411 ------------------
1412 -- Check_Length --
1413 ------------------
1415 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1416 Val_L : Uint;
1417 Val_H : Uint;
1419 OK_L : Boolean;
1420 OK_H : Boolean;
1422 Range_Len : Uint;
1424 begin
1425 if Raises_Constraint_Error (N) then
1426 return;
1427 end if;
1429 Get (Value => Val_L, From => L, OK => OK_L);
1430 Get (Value => Val_H, From => H, OK => OK_H);
1432 if not OK_L or else not OK_H then
1433 return;
1434 end if;
1436 -- If null range length is zero
1438 if Val_L > Val_H then
1439 Range_Len := Uint_0;
1440 else
1441 Range_Len := Val_H - Val_L + 1;
1442 end if;
1444 if Range_Len < Len then
1445 Set_Raises_Constraint_Error (N);
1446 Error_Msg_Warn := SPARK_Mode /= On;
1447 Error_Msg_N ("too many elements<<", N);
1448 Error_Msg_N ("\Constraint_Error [<<", N);
1449 end if;
1450 end Check_Length;
1452 ---------------------------
1453 -- Dynamic_Or_Null_Range --
1454 ---------------------------
1456 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1457 Val_L : Uint;
1458 Val_H : Uint;
1460 OK_L : Boolean;
1461 OK_H : Boolean;
1463 begin
1464 Get (Value => Val_L, From => L, OK => OK_L);
1465 Get (Value => Val_H, From => H, OK => OK_H);
1467 return not OK_L or else not OK_H
1468 or else not Is_OK_Static_Expression (L)
1469 or else not Is_OK_Static_Expression (H)
1470 or else Val_L > Val_H;
1471 end Dynamic_Or_Null_Range;
1473 ---------
1474 -- Get --
1475 ---------
1477 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1478 begin
1479 OK := True;
1481 if Compile_Time_Known_Value (From) then
1482 Value := Expr_Value (From);
1484 -- If expression From is something like Some_Type'Val (10) then
1485 -- Value = 10.
1487 elsif Nkind (From) = N_Attribute_Reference
1488 and then Attribute_Name (From) = Name_Val
1489 and then Compile_Time_Known_Value (First (Expressions (From)))
1490 then
1491 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;
1642 Discard : Node_Id;
1644 Delete_Choice : Boolean;
1645 -- Used when replacing a subtype choice with predicate by a list
1647 Aggr_Low : Node_Id := Empty;
1648 Aggr_High : Node_Id := Empty;
1649 -- The actual low and high bounds of this sub-aggregate
1651 Choices_Low : Node_Id := Empty;
1652 Choices_High : Node_Id := Empty;
1653 -- The lowest and highest discrete choices values for a named aggregate
1655 Nb_Elements : Uint := Uint_0;
1656 -- The number of elements in a positional aggregate
1658 Others_Present : Boolean := False;
1660 Nb_Choices : Nat := 0;
1661 -- Contains the overall number of named choices in this sub-aggregate
1663 Nb_Discrete_Choices : Nat := 0;
1664 -- The overall number of discrete choices (not counting others choice)
1666 Case_Table_Size : Nat;
1667 -- Contains the size of the case table needed to sort aggregate choices
1669 -- Start of processing for Resolve_Array_Aggregate
1671 begin
1672 -- Ignore junk empty aggregate resulting from parser error
1674 if No (Expressions (N))
1675 and then No (Component_Associations (N))
1676 and then not Null_Record_Present (N)
1677 then
1678 return False;
1679 end if;
1681 -- STEP 1: make sure the aggregate is correctly formatted
1683 if Present (Component_Associations (N)) then
1684 Assoc := First (Component_Associations (N));
1685 while Present (Assoc) loop
1686 Choice := First (Choices (Assoc));
1687 Delete_Choice := False;
1688 while Present (Choice) loop
1689 if Nkind (Choice) = N_Others_Choice then
1690 Others_Present := True;
1692 if Choice /= First (Choices (Assoc))
1693 or else Present (Next (Choice))
1694 then
1695 Error_Msg_N
1696 ("OTHERS must appear alone in a choice list", Choice);
1697 return Failure;
1698 end if;
1700 if Present (Next (Assoc)) then
1701 Error_Msg_N
1702 ("OTHERS must appear last in an aggregate", Choice);
1703 return Failure;
1704 end if;
1706 if Ada_Version = Ada_83
1707 and then Assoc /= First (Component_Associations (N))
1708 and then Nkind_In (Parent (N), N_Assignment_Statement,
1709 N_Object_Declaration)
1710 then
1711 Error_Msg_N
1712 ("(Ada 83) illegal context for OTHERS choice", N);
1713 end if;
1715 elsif Is_Entity_Name (Choice) then
1716 Analyze (Choice);
1718 declare
1719 E : constant Entity_Id := Entity (Choice);
1720 New_Cs : List_Id;
1721 P : Node_Id;
1722 C : Node_Id;
1724 begin
1725 if Is_Type (E) and then Has_Predicates (E) then
1726 Freeze_Before (N, E);
1728 if Has_Dynamic_Predicate_Aspect (E) then
1729 Error_Msg_NE
1730 ("subtype& has dynamic predicate, not allowed "
1731 & "in aggregate choice", Choice, E);
1733 elsif not Is_OK_Static_Subtype (E) then
1734 Error_Msg_NE
1735 ("non-static subtype& has predicate, not allowed "
1736 & "in aggregate choice", Choice, E);
1737 end if;
1739 -- If the subtype has a static predicate, replace the
1740 -- original choice with the list of individual values
1741 -- covered by the predicate.
1743 if Present (Static_Discrete_Predicate (E)) then
1744 Delete_Choice := True;
1746 New_Cs := New_List;
1747 P := First (Static_Discrete_Predicate (E));
1748 while Present (P) loop
1749 C := New_Copy (P);
1750 Set_Sloc (C, Sloc (Choice));
1751 Append_To (New_Cs, C);
1752 Next (P);
1753 end loop;
1755 Insert_List_After (Choice, New_Cs);
1756 end if;
1757 end if;
1758 end;
1759 end if;
1761 Nb_Choices := Nb_Choices + 1;
1763 declare
1764 C : constant Node_Id := Choice;
1766 begin
1767 Next (Choice);
1769 if Delete_Choice then
1770 Remove (C);
1771 Nb_Choices := Nb_Choices - 1;
1772 Delete_Choice := False;
1773 end if;
1774 end;
1775 end loop;
1777 Next (Assoc);
1778 end loop;
1779 end if;
1781 -- At this point we know that the others choice, if present, is by
1782 -- itself and appears last in the aggregate. Check if we have mixed
1783 -- positional and discrete associations (other than the others choice).
1785 if Present (Expressions (N))
1786 and then (Nb_Choices > 1
1787 or else (Nb_Choices = 1 and then not Others_Present))
1788 then
1789 Error_Msg_N
1790 ("named association cannot follow positional association",
1791 First (Choices (First (Component_Associations (N)))));
1792 return Failure;
1793 end if;
1795 -- Test for the validity of an others choice if present
1797 if Others_Present and then not Others_Allowed then
1798 Error_Msg_N
1799 ("OTHERS choice not allowed here",
1800 First (Choices (First (Component_Associations (N)))));
1801 return Failure;
1802 end if;
1804 -- Protect against cascaded errors
1806 if Etype (Index_Typ) = Any_Type then
1807 return Failure;
1808 end if;
1810 -- STEP 2: Process named components
1812 if No (Expressions (N)) then
1813 if Others_Present then
1814 Case_Table_Size := Nb_Choices - 1;
1815 else
1816 Case_Table_Size := Nb_Choices;
1817 end if;
1819 Step_2 : declare
1820 Low : Node_Id;
1821 High : Node_Id;
1822 -- Denote the lowest and highest values in an aggregate choice
1824 S_Low : Node_Id := Empty;
1825 S_High : Node_Id := Empty;
1826 -- if a choice in an aggregate is a subtype indication these
1827 -- denote the lowest and highest values of the subtype
1829 Table : Case_Table_Type (0 .. Case_Table_Size);
1830 -- Used to sort all the different choice values. Entry zero is
1831 -- reserved for sorting purposes.
1833 Single_Choice : Boolean;
1834 -- Set to true every time there is a single discrete choice in a
1835 -- discrete association
1837 Prev_Nb_Discrete_Choices : Nat;
1838 -- Used to keep track of the number of discrete choices in the
1839 -- current association.
1841 Errors_Posted_On_Choices : Boolean := False;
1842 -- Keeps track of whether any choices have semantic errors
1844 function Empty_Range (A : Node_Id) return Boolean;
1845 -- If an association covers an empty range, some warnings on the
1846 -- expression of the association can be disabled.
1848 -----------------
1849 -- Empty_Range --
1850 -----------------
1852 function Empty_Range (A : Node_Id) return Boolean is
1853 R : constant Node_Id := First (Choices (A));
1854 begin
1855 return No (Next (R))
1856 and then Nkind (R) = N_Range
1857 and then Compile_Time_Compare
1858 (Low_Bound (R), High_Bound (R), False) = GT;
1859 end Empty_Range;
1861 -- Start of processing for Step_2
1863 begin
1864 -- STEP 2 (A): Check discrete choices validity
1866 Assoc := First (Component_Associations (N));
1867 while Present (Assoc) loop
1868 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
1869 Choice := First (Choices (Assoc));
1870 loop
1871 Analyze (Choice);
1873 if Nkind (Choice) = N_Others_Choice then
1874 Single_Choice := False;
1875 exit;
1877 -- Test for subtype mark without constraint
1879 elsif Is_Entity_Name (Choice) and then
1880 Is_Type (Entity (Choice))
1881 then
1882 if Base_Type (Entity (Choice)) /= Index_Base then
1883 Error_Msg_N
1884 ("invalid subtype mark in aggregate choice",
1885 Choice);
1886 return Failure;
1887 end if;
1889 -- Case of subtype indication
1891 elsif Nkind (Choice) = N_Subtype_Indication then
1892 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
1894 if Has_Dynamic_Predicate_Aspect
1895 (Entity (Subtype_Mark (Choice)))
1896 then
1897 Error_Msg_NE
1898 ("subtype& has dynamic predicate, "
1899 & "not allowed in aggregate choice",
1900 Choice, Entity (Subtype_Mark (Choice)));
1901 end if;
1903 -- Does the subtype indication evaluation raise CE?
1905 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
1906 Get_Index_Bounds (Choice, Low, High);
1907 Check_Bounds (S_Low, S_High, Low, High);
1909 -- Case of range or expression
1911 else
1912 Resolve (Choice, Index_Base);
1913 Check_Unset_Reference (Choice);
1914 Check_Non_Static_Context (Choice);
1916 -- If semantic errors were posted on the choice, then
1917 -- record that for possible early return from later
1918 -- processing (see handling of enumeration choices).
1920 if Error_Posted (Choice) then
1921 Errors_Posted_On_Choices := True;
1922 end if;
1924 -- Do not range check a choice. This check is redundant
1925 -- since this test is already done when we check that the
1926 -- bounds of the array aggregate are within range.
1928 Set_Do_Range_Check (Choice, False);
1930 -- In SPARK, the choice must be static
1932 if not (Is_OK_Static_Expression (Choice)
1933 or else (Nkind (Choice) = N_Range
1934 and then Is_OK_Static_Range (Choice)))
1935 then
1936 Check_SPARK_05_Restriction
1937 ("choice should be static", Choice);
1938 end if;
1939 end if;
1941 -- If we could not resolve the discrete choice stop here
1943 if Etype (Choice) = Any_Type then
1944 return Failure;
1946 -- If the discrete choice raises CE get its original bounds
1948 elsif Nkind (Choice) = N_Raise_Constraint_Error then
1949 Set_Raises_Constraint_Error (N);
1950 Get_Index_Bounds (Original_Node (Choice), Low, High);
1952 -- Otherwise get its bounds as usual
1954 else
1955 Get_Index_Bounds (Choice, Low, High);
1956 end if;
1958 if (Dynamic_Or_Null_Range (Low, High)
1959 or else (Nkind (Choice) = N_Subtype_Indication
1960 and then
1961 Dynamic_Or_Null_Range (S_Low, S_High)))
1962 and then Nb_Choices /= 1
1963 then
1964 Error_Msg_N
1965 ("dynamic or empty choice in aggregate "
1966 & "must be the only choice", Choice);
1967 return Failure;
1968 end if;
1970 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
1971 Table (Nb_Discrete_Choices).Lo := Low;
1972 Table (Nb_Discrete_Choices).Hi := High;
1973 Table (Nb_Discrete_Choices).Choice := Choice;
1975 Next (Choice);
1977 if No (Choice) then
1979 -- Check if we have a single discrete choice and whether
1980 -- this discrete choice specifies a single value.
1982 Single_Choice :=
1983 (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1)
1984 and then (Low = High);
1986 exit;
1987 end if;
1988 end loop;
1990 -- Ada 2005 (AI-231)
1992 if Ada_Version >= Ada_2005
1993 and then Known_Null (Expression (Assoc))
1994 and then not Empty_Range (Assoc)
1995 then
1996 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
1997 end if;
1999 -- Ada 2005 (AI-287): In case of default initialized component
2000 -- we delay the resolution to the expansion phase.
2002 if Box_Present (Assoc) then
2004 -- Ada 2005 (AI-287): In case of default initialization of a
2005 -- component the expander will generate calls to the
2006 -- corresponding initialization subprogram. We need to call
2007 -- Resolve_Aggr_Expr to check the rules about
2008 -- dimensionality.
2010 if not Resolve_Aggr_Expr
2011 (Assoc, Single_Elmt => Single_Choice)
2012 then
2013 return Failure;
2014 end if;
2016 elsif not Resolve_Aggr_Expr
2017 (Expression (Assoc), Single_Elmt => Single_Choice)
2018 then
2019 return Failure;
2021 -- Check incorrect use of dynamically tagged expression
2023 -- We differentiate here two cases because the expression may
2024 -- not be decorated. For example, the analysis and resolution
2025 -- of the expression associated with the others choice will be
2026 -- done later with the full aggregate. In such case we
2027 -- duplicate the expression tree to analyze the copy and
2028 -- perform the required check.
2030 elsif not Present (Etype (Expression (Assoc))) then
2031 declare
2032 Save_Analysis : constant Boolean := Full_Analysis;
2033 Expr : constant Node_Id :=
2034 New_Copy_Tree (Expression (Assoc));
2036 begin
2037 Expander_Mode_Save_And_Set (False);
2038 Full_Analysis := False;
2040 -- Analyze the expression, making sure it is properly
2041 -- attached to the tree before we do the analysis.
2043 Set_Parent (Expr, Parent (Expression (Assoc)));
2044 Analyze (Expr);
2046 -- If the expression is a literal, propagate this info
2047 -- to the expression in the association, to enable some
2048 -- optimizations downstream.
2050 if Is_Entity_Name (Expr)
2051 and then Present (Entity (Expr))
2052 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
2053 then
2054 Analyze_And_Resolve
2055 (Expression (Assoc), Component_Typ);
2056 end if;
2058 Full_Analysis := Save_Analysis;
2059 Expander_Mode_Restore;
2061 if Is_Tagged_Type (Etype (Expr)) then
2062 Check_Dynamically_Tagged_Expression
2063 (Expr => Expr,
2064 Typ => Component_Type (Etype (N)),
2065 Related_Nod => N);
2066 end if;
2067 end;
2069 elsif Is_Tagged_Type (Etype (Expression (Assoc))) then
2070 Check_Dynamically_Tagged_Expression
2071 (Expr => Expression (Assoc),
2072 Typ => Component_Type (Etype (N)),
2073 Related_Nod => N);
2074 end if;
2076 Next (Assoc);
2077 end loop;
2079 -- If aggregate contains more than one choice then these must be
2080 -- static. Check for duplicate and missing values.
2082 -- Note: there is duplicated code here wrt Check_Choice_Set in
2083 -- the body of Sem_Case, and it is possible we could just reuse
2084 -- that procedure. To be checked ???
2086 if Nb_Discrete_Choices > 1 then
2087 Check_Choices : declare
2088 Choice : Node_Id;
2089 -- Location of choice for messages
2091 Hi_Val : Uint;
2092 Lo_Val : Uint;
2093 -- High end of one range and Low end of the next. Should be
2094 -- contiguous if there is no hole in the list of values.
2096 Lo_Dup : Uint;
2097 Hi_Dup : Uint;
2098 -- End points of duplicated range
2100 Missing_Or_Duplicates : Boolean := False;
2101 -- Set True if missing or duplicate choices found
2103 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id);
2104 -- Output continuation message with a representation of the
2105 -- bounds (just Lo if Lo = Hi, else Lo .. Hi). C is the
2106 -- choice node where the message is to be posted.
2108 ------------------------
2109 -- Output_Bad_Choices --
2110 ------------------------
2112 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id) is
2113 begin
2114 -- Enumeration type case
2116 if Is_Enumeration_Type (Index_Typ) then
2117 Error_Msg_Name_1 :=
2118 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Lo, Loc));
2119 Error_Msg_Name_2 :=
2120 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Hi, Loc));
2122 if Lo = Hi then
2123 Error_Msg_N ("\\ %!", C);
2124 else
2125 Error_Msg_N ("\\ % .. %!", C);
2126 end if;
2128 -- Integer types case
2130 else
2131 Error_Msg_Uint_1 := Lo;
2132 Error_Msg_Uint_2 := Hi;
2134 if Lo = Hi then
2135 Error_Msg_N ("\\ ^!", C);
2136 else
2137 Error_Msg_N ("\\ ^ .. ^!", C);
2138 end if;
2139 end if;
2140 end Output_Bad_Choices;
2142 -- Start of processing for Check_Choices
2144 begin
2145 Sort_Case_Table (Table);
2147 -- First we do a quick linear loop to find out if we have
2148 -- any duplicates or missing entries (usually we have a
2149 -- legal aggregate, so this will get us out quickly).
2151 for J in 1 .. Nb_Discrete_Choices - 1 loop
2152 Hi_Val := Expr_Value (Table (J).Hi);
2153 Lo_Val := Expr_Value (Table (J + 1).Lo);
2155 if Lo_Val <= Hi_Val
2156 or else (Lo_Val > Hi_Val + 1
2157 and then not Others_Present)
2158 then
2159 Missing_Or_Duplicates := True;
2160 exit;
2161 end if;
2162 end loop;
2164 -- If we have missing or duplicate entries, first fill in
2165 -- the Highest entries to make life easier in the following
2166 -- loops to detect bad entries.
2168 if Missing_Or_Duplicates then
2169 Table (1).Highest := Expr_Value (Table (1).Hi);
2171 for J in 2 .. Nb_Discrete_Choices loop
2172 Table (J).Highest :=
2173 UI_Max
2174 (Table (J - 1).Highest, Expr_Value (Table (J).Hi));
2175 end loop;
2177 -- Loop through table entries to find duplicate indexes
2179 for J in 2 .. Nb_Discrete_Choices loop
2180 Lo_Val := Expr_Value (Table (J).Lo);
2181 Hi_Val := Expr_Value (Table (J).Hi);
2183 -- Case where we have duplicates (the lower bound of
2184 -- this choice is less than or equal to the highest
2185 -- high bound found so far).
2187 if Lo_Val <= Table (J - 1).Highest then
2189 -- We move backwards looking for duplicates. We can
2190 -- abandon this loop as soon as we reach a choice
2191 -- highest value that is less than Lo_Val.
2193 for K in reverse 1 .. J - 1 loop
2194 exit when Table (K).Highest < Lo_Val;
2196 -- Here we may have duplicates between entries
2197 -- for K and J. Get range of duplicates.
2199 Lo_Dup :=
2200 UI_Max (Lo_Val, Expr_Value (Table (K).Lo));
2201 Hi_Dup :=
2202 UI_Min (Hi_Val, Expr_Value (Table (K).Hi));
2204 -- Nothing to do if duplicate range is null
2206 if Lo_Dup > Hi_Dup then
2207 null;
2209 -- Otherwise place proper message
2211 else
2212 -- We place message on later choice, with a
2213 -- line reference to the earlier choice.
2215 if Sloc (Table (J).Choice) <
2216 Sloc (Table (K).Choice)
2217 then
2218 Choice := Table (K).Choice;
2219 Error_Msg_Sloc := Sloc (Table (J).Choice);
2220 else
2221 Choice := Table (J).Choice;
2222 Error_Msg_Sloc := Sloc (Table (K).Choice);
2223 end if;
2225 if Lo_Dup = Hi_Dup then
2226 Error_Msg_N
2227 ("index value in array aggregate "
2228 & "duplicates the one given#!", Choice);
2229 else
2230 Error_Msg_N
2231 ("index values in array aggregate "
2232 & "duplicate those given#!", Choice);
2233 end if;
2235 Output_Bad_Choices (Lo_Dup, Hi_Dup, Choice);
2236 end if;
2237 end loop;
2238 end if;
2239 end loop;
2241 -- Loop through entries in table to find missing indexes.
2242 -- Not needed if others, since missing impossible.
2244 if not Others_Present then
2245 for J in 2 .. Nb_Discrete_Choices loop
2246 Lo_Val := Expr_Value (Table (J).Lo);
2247 Hi_Val := Table (J - 1).Highest;
2249 if Lo_Val > Hi_Val + 1 then
2251 declare
2252 Error_Node : Node_Id;
2254 begin
2255 -- If the choice is the bound of a range in
2256 -- a subtype indication, it is not in the
2257 -- source lists for the aggregate itself, so
2258 -- post the error on the aggregate. Otherwise
2259 -- post it on choice itself.
2261 Choice := Table (J).Choice;
2263 if Is_List_Member (Choice) then
2264 Error_Node := Choice;
2265 else
2266 Error_Node := N;
2267 end if;
2269 if Hi_Val + 1 = Lo_Val - 1 then
2270 Error_Msg_N
2271 ("missing index value "
2272 & "in array aggregate!", Error_Node);
2273 else
2274 Error_Msg_N
2275 ("missing index values "
2276 & "in array aggregate!", Error_Node);
2277 end if;
2279 Output_Bad_Choices
2280 (Hi_Val + 1, Lo_Val - 1, Error_Node);
2281 end;
2282 end if;
2283 end loop;
2284 end if;
2286 -- If either missing or duplicate values, return failure
2288 Set_Etype (N, Any_Composite);
2289 return Failure;
2290 end if;
2291 end Check_Choices;
2292 end if;
2294 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
2296 if Nb_Discrete_Choices > 0 then
2297 Choices_Low := Table (1).Lo;
2298 Choices_High := Table (Nb_Discrete_Choices).Hi;
2299 end if;
2301 -- If Others is present, then bounds of aggregate come from the
2302 -- index constraint (not the choices in the aggregate itself).
2304 if Others_Present then
2305 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2307 -- Abandon processing if either bound is already signalled as
2308 -- an error (prevents junk cascaded messages and blow ups).
2310 if Nkind (Aggr_Low) = N_Error
2311 or else
2312 Nkind (Aggr_High) = N_Error
2313 then
2314 return False;
2315 end if;
2317 -- No others clause present
2319 else
2320 -- Special processing if others allowed and not present. This
2321 -- means that the bounds of the aggregate come from the index
2322 -- constraint (and the length must match).
2324 if Others_Allowed then
2325 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2327 -- Abandon processing if either bound is already signalled
2328 -- as an error (stop junk cascaded messages and blow ups).
2330 if Nkind (Aggr_Low) = N_Error
2331 or else
2332 Nkind (Aggr_High) = N_Error
2333 then
2334 return False;
2335 end if;
2337 -- If others allowed, and no others present, then the array
2338 -- should cover all index values. If it does not, we will
2339 -- get a length check warning, but there is two cases where
2340 -- an additional warning is useful:
2342 -- If we have no positional components, and the length is
2343 -- wrong (which we can tell by others being allowed with
2344 -- missing components), and the index type is an enumeration
2345 -- type, then issue appropriate warnings about these missing
2346 -- components. They are only warnings, since the aggregate
2347 -- is fine, it's just the wrong length. We skip this check
2348 -- for standard character types (since there are no literals
2349 -- and it is too much trouble to concoct them), and also if
2350 -- any of the bounds have values that are not known at
2351 -- compile time.
2353 -- Another case warranting a warning is when the length
2354 -- is right, but as above we have an index type that is
2355 -- an enumeration, and the bounds do not match. This is a
2356 -- case where dubious sliding is allowed and we generate a
2357 -- warning that the bounds do not match.
2359 if No (Expressions (N))
2360 and then Nkind (Index) = N_Range
2361 and then Is_Enumeration_Type (Etype (Index))
2362 and then not Is_Standard_Character_Type (Etype (Index))
2363 and then Compile_Time_Known_Value (Aggr_Low)
2364 and then Compile_Time_Known_Value (Aggr_High)
2365 and then Compile_Time_Known_Value (Choices_Low)
2366 and then Compile_Time_Known_Value (Choices_High)
2367 then
2368 -- If any of the expressions or range bounds in choices
2369 -- have semantic errors, then do not attempt further
2370 -- resolution, to prevent cascaded errors.
2372 if Errors_Posted_On_Choices then
2373 return Failure;
2374 end if;
2376 declare
2377 ALo : constant Node_Id := Expr_Value_E (Aggr_Low);
2378 AHi : constant Node_Id := Expr_Value_E (Aggr_High);
2379 CLo : constant Node_Id := Expr_Value_E (Choices_Low);
2380 CHi : constant Node_Id := Expr_Value_E (Choices_High);
2382 Ent : Entity_Id;
2384 begin
2385 -- Warning case 1, missing values at start/end. Only
2386 -- do the check if the number of entries is too small.
2388 if (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2390 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2391 then
2392 Error_Msg_N
2393 ("missing index value(s) in array aggregate??",
2396 -- Output missing value(s) at start
2398 if Chars (ALo) /= Chars (CLo) then
2399 Ent := Prev (CLo);
2401 if Chars (ALo) = Chars (Ent) then
2402 Error_Msg_Name_1 := Chars (ALo);
2403 Error_Msg_N ("\ %??", N);
2404 else
2405 Error_Msg_Name_1 := Chars (ALo);
2406 Error_Msg_Name_2 := Chars (Ent);
2407 Error_Msg_N ("\ % .. %??", N);
2408 end if;
2409 end if;
2411 -- Output missing value(s) at end
2413 if Chars (AHi) /= Chars (CHi) then
2414 Ent := Next (CHi);
2416 if Chars (AHi) = Chars (Ent) then
2417 Error_Msg_Name_1 := Chars (Ent);
2418 Error_Msg_N ("\ %??", N);
2419 else
2420 Error_Msg_Name_1 := Chars (Ent);
2421 Error_Msg_Name_2 := Chars (AHi);
2422 Error_Msg_N ("\ % .. %??", N);
2423 end if;
2424 end if;
2426 -- Warning case 2, dubious sliding. The First_Subtype
2427 -- test distinguishes between a constrained type where
2428 -- sliding is not allowed (so we will get a warning
2429 -- later that Constraint_Error will be raised), and
2430 -- the unconstrained case where sliding is permitted.
2432 elsif (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2434 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2435 and then Chars (ALo) /= Chars (CLo)
2436 and then
2437 not Is_Constrained (First_Subtype (Etype (N)))
2438 then
2439 Error_Msg_N
2440 ("bounds of aggregate do not match target??", N);
2441 end if;
2442 end;
2443 end if;
2444 end if;
2446 -- If no others, aggregate bounds come from aggregate
2448 Aggr_Low := Choices_Low;
2449 Aggr_High := Choices_High;
2450 end if;
2451 end Step_2;
2453 -- STEP 3: Process positional components
2455 else
2456 -- STEP 3 (A): Process positional elements
2458 Expr := First (Expressions (N));
2459 Nb_Elements := Uint_0;
2460 while Present (Expr) loop
2461 Nb_Elements := Nb_Elements + 1;
2463 -- Ada 2005 (AI-231)
2465 if Ada_Version >= Ada_2005 and then Known_Null (Expr) then
2466 Check_Can_Never_Be_Null (Etype (N), Expr);
2467 end if;
2469 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
2470 return Failure;
2471 end if;
2473 -- Check incorrect use of dynamically tagged expression
2475 if Is_Tagged_Type (Etype (Expr)) then
2476 Check_Dynamically_Tagged_Expression
2477 (Expr => Expr,
2478 Typ => Component_Type (Etype (N)),
2479 Related_Nod => N);
2480 end if;
2482 Next (Expr);
2483 end loop;
2485 if Others_Present then
2486 Assoc := Last (Component_Associations (N));
2488 -- Ada 2005 (AI-231)
2490 if Ada_Version >= Ada_2005 and then Known_Null (Assoc) then
2491 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2492 end if;
2494 -- Ada 2005 (AI-287): In case of default initialized component,
2495 -- we delay the resolution to the expansion phase.
2497 if Box_Present (Assoc) then
2499 -- Ada 2005 (AI-287): In case of default initialization of a
2500 -- component the expander will generate calls to the
2501 -- corresponding initialization subprogram. We need to call
2502 -- Resolve_Aggr_Expr to check the rules about
2503 -- dimensionality.
2505 if not Resolve_Aggr_Expr (Assoc, Single_Elmt => False) then
2506 return Failure;
2507 end if;
2509 elsif not Resolve_Aggr_Expr (Expression (Assoc),
2510 Single_Elmt => False)
2511 then
2512 return Failure;
2514 -- Check incorrect use of dynamically tagged expression. The
2515 -- expression of the others choice has not been resolved yet.
2516 -- In order to diagnose the semantic error we create a duplicate
2517 -- tree to analyze it and perform the check.
2519 else
2520 declare
2521 Save_Analysis : constant Boolean := Full_Analysis;
2522 Expr : constant Node_Id :=
2523 New_Copy_Tree (Expression (Assoc));
2525 begin
2526 Expander_Mode_Save_And_Set (False);
2527 Full_Analysis := False;
2528 Analyze (Expr);
2529 Full_Analysis := Save_Analysis;
2530 Expander_Mode_Restore;
2532 if Is_Tagged_Type (Etype (Expr)) then
2533 Check_Dynamically_Tagged_Expression
2534 (Expr => Expr,
2535 Typ => Component_Type (Etype (N)),
2536 Related_Nod => N);
2537 end if;
2538 end;
2539 end if;
2540 end if;
2542 -- STEP 3 (B): Compute the aggregate bounds
2544 if Others_Present then
2545 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2547 else
2548 if Others_Allowed then
2549 Get_Index_Bounds (Index_Constr, Aggr_Low, Discard);
2550 else
2551 Aggr_Low := Index_Typ_Low;
2552 end if;
2554 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
2555 Check_Bound (Index_Base_High, Aggr_High);
2556 end if;
2557 end if;
2559 -- STEP 4: Perform static aggregate checks and save the bounds
2561 -- Check (A)
2563 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
2564 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
2566 -- Check (B)
2568 if Others_Present and then Nb_Discrete_Choices > 0 then
2569 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
2570 Check_Bounds (Index_Typ_Low, Index_Typ_High,
2571 Choices_Low, Choices_High);
2572 Check_Bounds (Index_Base_Low, Index_Base_High,
2573 Choices_Low, Choices_High);
2575 -- Check (C)
2577 elsif Others_Present and then Nb_Elements > 0 then
2578 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
2579 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
2580 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
2581 end if;
2583 if Raises_Constraint_Error (Aggr_Low)
2584 or else Raises_Constraint_Error (Aggr_High)
2585 then
2586 Set_Raises_Constraint_Error (N);
2587 end if;
2589 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
2591 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
2592 -- since the addition node returned by Add is not yet analyzed. Attach
2593 -- to tree and analyze first. Reset analyzed flag to ensure it will get
2594 -- analyzed when it is a literal bound whose type must be properly set.
2596 if Others_Present or else Nb_Discrete_Choices > 0 then
2597 Aggr_High := Duplicate_Subexpr (Aggr_High);
2599 if Etype (Aggr_High) = Universal_Integer then
2600 Set_Analyzed (Aggr_High, False);
2601 end if;
2602 end if;
2604 -- If the aggregate already has bounds attached to it, it means this is
2605 -- a positional aggregate created as an optimization by
2606 -- Exp_Aggr.Convert_To_Positional, so we don't want to change those
2607 -- bounds.
2609 if Present (Aggregate_Bounds (N)) and then not Others_Allowed then
2610 Aggr_Low := Low_Bound (Aggregate_Bounds (N));
2611 Aggr_High := High_Bound (Aggregate_Bounds (N));
2612 end if;
2614 Set_Aggregate_Bounds
2615 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
2617 -- The bounds may contain expressions that must be inserted upwards.
2618 -- Attach them fully to the tree. After analysis, remove side effects
2619 -- from upper bound, if still needed.
2621 Set_Parent (Aggregate_Bounds (N), N);
2622 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
2623 Check_Unset_Reference (Aggregate_Bounds (N));
2625 if not Others_Present and then Nb_Discrete_Choices = 0 then
2626 Set_High_Bound
2627 (Aggregate_Bounds (N),
2628 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
2629 end if;
2631 -- Check the dimensions of each component in the array aggregate
2633 Analyze_Dimension_Array_Aggregate (N, Component_Typ);
2635 return Success;
2636 end Resolve_Array_Aggregate;
2638 ---------------------------------
2639 -- Resolve_Extension_Aggregate --
2640 ---------------------------------
2642 -- There are two cases to consider:
2644 -- a) If the ancestor part is a type mark, the components needed are the
2645 -- difference between the components of the expected type and the
2646 -- components of the given type mark.
2648 -- b) If the ancestor part is an expression, it must be unambiguous, and
2649 -- once we have its type we can also compute the needed components as in
2650 -- the previous case. In both cases, if the ancestor type is not the
2651 -- immediate ancestor, we have to build this ancestor recursively.
2653 -- In both cases, discriminants of the ancestor type do not play a role in
2654 -- the resolution of the needed components, because inherited discriminants
2655 -- cannot be used in a type extension. As a result we can compute
2656 -- independently the list of components of the ancestor type and of the
2657 -- expected type.
2659 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
2660 A : constant Node_Id := Ancestor_Part (N);
2661 A_Type : Entity_Id;
2662 I : Interp_Index;
2663 It : Interp;
2665 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean;
2666 -- If the type is limited, verify that the ancestor part is a legal
2667 -- expression (aggregate or function call, including 'Input)) that does
2668 -- not require a copy, as specified in 7.5(2).
2670 function Valid_Ancestor_Type return Boolean;
2671 -- Verify that the type of the ancestor part is a non-private ancestor
2672 -- of the expected type, which must be a type extension.
2674 ----------------------------
2675 -- Valid_Limited_Ancestor --
2676 ----------------------------
2678 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean is
2679 begin
2680 if Is_Entity_Name (Anc) and then Is_Type (Entity (Anc)) then
2681 return True;
2683 -- The ancestor must be a call or an aggregate, but a call may
2684 -- have been expanded into a temporary, so check original node.
2686 elsif Nkind_In (Anc, N_Aggregate,
2687 N_Extension_Aggregate,
2688 N_Function_Call)
2689 then
2690 return True;
2692 elsif Nkind (Original_Node (Anc)) = N_Function_Call then
2693 return True;
2695 elsif Nkind (Anc) = N_Attribute_Reference
2696 and then Attribute_Name (Anc) = Name_Input
2697 then
2698 return True;
2700 elsif Nkind (Anc) = N_Qualified_Expression then
2701 return Valid_Limited_Ancestor (Expression (Anc));
2703 else
2704 return False;
2705 end if;
2706 end Valid_Limited_Ancestor;
2708 -------------------------
2709 -- Valid_Ancestor_Type --
2710 -------------------------
2712 function Valid_Ancestor_Type return Boolean is
2713 Imm_Type : Entity_Id;
2715 begin
2716 Imm_Type := Base_Type (Typ);
2717 while Is_Derived_Type (Imm_Type) loop
2718 if Etype (Imm_Type) = Base_Type (A_Type) then
2719 return True;
2721 -- The base type of the parent type may appear as a private
2722 -- extension if it is declared as such in a parent unit of the
2723 -- current one. For consistency of the subsequent analysis use
2724 -- the partial view for the ancestor part.
2726 elsif Is_Private_Type (Etype (Imm_Type))
2727 and then Present (Full_View (Etype (Imm_Type)))
2728 and then Base_Type (A_Type) = Full_View (Etype (Imm_Type))
2729 then
2730 A_Type := Etype (Imm_Type);
2731 return True;
2733 -- The parent type may be a private extension. The aggregate is
2734 -- legal if the type of the aggregate is an extension of it that
2735 -- is not a private extension.
2737 elsif Is_Private_Type (A_Type)
2738 and then not Is_Private_Type (Imm_Type)
2739 and then Present (Full_View (A_Type))
2740 and then Base_Type (Full_View (A_Type)) = Etype (Imm_Type)
2741 then
2742 return True;
2744 else
2745 Imm_Type := Etype (Base_Type (Imm_Type));
2746 end if;
2747 end loop;
2749 -- If previous loop did not find a proper ancestor, report error
2751 Error_Msg_NE ("expect ancestor type of &", A, Typ);
2752 return False;
2753 end Valid_Ancestor_Type;
2755 -- Start of processing for Resolve_Extension_Aggregate
2757 begin
2758 -- Analyze the ancestor part and account for the case where it is a
2759 -- parameterless function call.
2761 Analyze (A);
2762 Check_Parameterless_Call (A);
2764 -- In SPARK, the ancestor part cannot be a type mark
2766 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
2767 Check_SPARK_05_Restriction ("ancestor part cannot be a type mark", A);
2769 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
2770 -- must not have unknown discriminants.
2772 if Has_Unknown_Discriminants (Root_Type (Typ)) then
2773 Error_Msg_NE
2774 ("aggregate not available for type& whose ancestor "
2775 & "has unknown discriminants", N, Typ);
2776 end if;
2777 end if;
2779 if not Is_Tagged_Type (Typ) then
2780 Error_Msg_N ("type of extension aggregate must be tagged", N);
2781 return;
2783 elsif Is_Limited_Type (Typ) then
2785 -- Ada 2005 (AI-287): Limited aggregates are allowed
2787 if Ada_Version < Ada_2005 then
2788 Error_Msg_N ("aggregate type cannot be limited", N);
2789 Explain_Limited_Type (Typ, N);
2790 return;
2792 elsif Valid_Limited_Ancestor (A) then
2793 null;
2795 else
2796 Error_Msg_N
2797 ("limited ancestor part must be aggregate or function call", A);
2798 end if;
2800 elsif Is_Class_Wide_Type (Typ) then
2801 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
2802 return;
2803 end if;
2805 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
2806 A_Type := Get_Full_View (Entity (A));
2808 if Valid_Ancestor_Type then
2809 Set_Entity (A, A_Type);
2810 Set_Etype (A, A_Type);
2812 Validate_Ancestor_Part (N);
2813 Resolve_Record_Aggregate (N, Typ);
2814 end if;
2816 elsif Nkind (A) /= N_Aggregate then
2817 if Is_Overloaded (A) then
2818 A_Type := Any_Type;
2820 Get_First_Interp (A, I, It);
2821 while Present (It.Typ) loop
2823 -- Only consider limited interpretations in the Ada 2005 case
2825 if Is_Tagged_Type (It.Typ)
2826 and then (Ada_Version >= Ada_2005
2827 or else not Is_Limited_Type (It.Typ))
2828 then
2829 if A_Type /= Any_Type then
2830 Error_Msg_N ("cannot resolve expression", A);
2831 return;
2832 else
2833 A_Type := It.Typ;
2834 end if;
2835 end if;
2837 Get_Next_Interp (I, It);
2838 end loop;
2840 if A_Type = Any_Type then
2841 if Ada_Version >= Ada_2005 then
2842 Error_Msg_N
2843 ("ancestor part must be of a tagged type", A);
2844 else
2845 Error_Msg_N
2846 ("ancestor part must be of a nonlimited tagged type", A);
2847 end if;
2849 return;
2850 end if;
2852 else
2853 A_Type := Etype (A);
2854 end if;
2856 if Valid_Ancestor_Type then
2857 Resolve (A, A_Type);
2858 Check_Unset_Reference (A);
2859 Check_Non_Static_Context (A);
2861 -- The aggregate is illegal if the ancestor expression is a call
2862 -- to a function with a limited unconstrained result, unless the
2863 -- type of the aggregate is a null extension. This restriction
2864 -- was added in AI05-67 to simplify implementation.
2866 if Nkind (A) = N_Function_Call
2867 and then Is_Limited_Type (A_Type)
2868 and then not Is_Null_Extension (Typ)
2869 and then not Is_Constrained (A_Type)
2870 then
2871 Error_Msg_N
2872 ("type of limited ancestor part must be constrained", A);
2874 -- Reject the use of CPP constructors that leave objects partially
2875 -- initialized. For example:
2877 -- type CPP_Root is tagged limited record ...
2878 -- pragma Import (CPP, CPP_Root);
2880 -- type CPP_DT is new CPP_Root and Iface ...
2881 -- pragma Import (CPP, CPP_DT);
2883 -- type Ada_DT is new CPP_DT with ...
2885 -- Obj : Ada_DT := Ada_DT'(New_CPP_Root with others => <>);
2887 -- Using the constructor of CPP_Root the slots of the dispatch
2888 -- table of CPP_DT cannot be set, and the secondary tag of
2889 -- CPP_DT is unknown.
2891 elsif Nkind (A) = N_Function_Call
2892 and then Is_CPP_Constructor_Call (A)
2893 and then Enclosing_CPP_Parent (Typ) /= A_Type
2894 then
2895 Error_Msg_NE
2896 ("??must use 'C'P'P constructor for type &", A,
2897 Enclosing_CPP_Parent (Typ));
2899 -- The following call is not needed if the previous warning
2900 -- is promoted to an error.
2902 Resolve_Record_Aggregate (N, Typ);
2904 elsif Is_Class_Wide_Type (Etype (A))
2905 and then Nkind (Original_Node (A)) = N_Function_Call
2906 then
2907 -- If the ancestor part is a dispatching call, it appears
2908 -- statically to be a legal ancestor, but it yields any member
2909 -- of the class, and it is not possible to determine whether
2910 -- it is an ancestor of the extension aggregate (much less
2911 -- which ancestor). It is not possible to determine the
2912 -- components of the extension part.
2914 -- This check implements AI-306, which in fact was motivated by
2915 -- an AdaCore query to the ARG after this test was added.
2917 Error_Msg_N ("ancestor part must be statically tagged", A);
2918 else
2919 Resolve_Record_Aggregate (N, Typ);
2920 end if;
2921 end if;
2923 else
2924 Error_Msg_N ("no unique type for this aggregate", A);
2925 end if;
2927 Check_Function_Writable_Actuals (N);
2928 end Resolve_Extension_Aggregate;
2930 ------------------------------
2931 -- Resolve_Record_Aggregate --
2932 ------------------------------
2934 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
2935 Assoc : Node_Id;
2936 -- N_Component_Association node belonging to the input aggregate N
2938 Expr : Node_Id;
2939 Positional_Expr : Node_Id;
2940 Component : Entity_Id;
2941 Component_Elmt : Elmt_Id;
2943 Components : constant Elist_Id := New_Elmt_List;
2944 -- Components is the list of the record components whose value must be
2945 -- provided in the aggregate. This list does include discriminants.
2947 New_Assoc_List : constant List_Id := New_List;
2948 New_Assoc : Node_Id;
2949 -- New_Assoc_List is the newly built list of N_Component_Association
2950 -- nodes. New_Assoc is one such N_Component_Association node in it.
2951 -- Note that while Assoc and New_Assoc contain the same kind of nodes,
2952 -- they are used to iterate over two different N_Component_Association
2953 -- lists.
2955 Others_Etype : Entity_Id := Empty;
2956 -- This variable is used to save the Etype of the last record component
2957 -- that takes its value from the others choice. Its purpose is:
2959 -- (a) make sure the others choice is useful
2961 -- (b) make sure the type of all the components whose value is
2962 -- subsumed by the others choice are the same.
2964 -- This variable is updated as a side effect of function Get_Value.
2966 Is_Box_Present : Boolean := False;
2967 Others_Box : Boolean := False;
2968 -- Ada 2005 (AI-287): Variables used in case of default initialization
2969 -- to provide a functionality similar to Others_Etype. Box_Present
2970 -- indicates that the component takes its default initialization;
2971 -- Others_Box indicates that at least one component takes its default
2972 -- initialization. Similar to Others_Etype, they are also updated as a
2973 -- side effect of function Get_Value.
2975 procedure Add_Association
2976 (Component : Entity_Id;
2977 Expr : Node_Id;
2978 Assoc_List : List_Id;
2979 Is_Box_Present : Boolean := False);
2980 -- Builds a new N_Component_Association node which associates Component
2981 -- to expression Expr and adds it to the association list being built,
2982 -- either New_Assoc_List, or the association being built for an inner
2983 -- aggregate.
2985 function Discr_Present (Discr : Entity_Id) return Boolean;
2986 -- If aggregate N is a regular aggregate this routine will return True.
2987 -- Otherwise, if N is an extension aggregate, Discr is a discriminant
2988 -- whose value may already have been specified by N's ancestor part.
2989 -- This routine checks whether this is indeed the case and if so returns
2990 -- False, signaling that no value for Discr should appear in N's
2991 -- aggregate part. Also, in this case, the routine appends to
2992 -- New_Assoc_List the discriminant value specified in the ancestor part.
2994 -- If the aggregate is in a context with expansion delayed, it will be
2995 -- reanalyzed. The inherited discriminant values must not be reinserted
2996 -- in the component list to prevent spurious errors, but they must be
2997 -- present on first analysis to build the proper subtype indications.
2998 -- The flag Inherited_Discriminant is used to prevent the re-insertion.
3000 function Get_Value
3001 (Compon : Node_Id;
3002 From : List_Id;
3003 Consider_Others_Choice : Boolean := False)
3004 return Node_Id;
3005 -- Given a record component stored in parameter Compon, this function
3006 -- returns its value as it appears in the list From, which is a list
3007 -- of N_Component_Association nodes.
3009 -- If no component association has a choice for the searched component,
3010 -- the value provided by the others choice is returned, if there is one,
3011 -- and Consider_Others_Choice is set to true. Otherwise Empty is
3012 -- returned. If there is more than one component association giving a
3013 -- value for the searched record component, an error message is emitted
3014 -- and the first found value is returned.
3016 -- If Consider_Others_Choice is set and the returned expression comes
3017 -- from the others choice, then Others_Etype is set as a side effect.
3018 -- An error message is emitted if the components taking their value from
3019 -- the others choice do not have same type.
3021 function New_Copy_Tree_And_Copy_Dimensions
3022 (Source : Node_Id;
3023 Map : Elist_Id := No_Elist;
3024 New_Sloc : Source_Ptr := No_Location;
3025 New_Scope : Entity_Id := Empty) return Node_Id;
3026 -- Same as New_Copy_Tree (defined in Sem_Util), except that this routine
3027 -- also copies the dimensions of Source to the returned node.
3029 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id);
3030 -- Analyzes and resolves expression Expr against the Etype of the
3031 -- Component. This routine also applies all appropriate checks to Expr.
3032 -- It finally saves a Expr in the newly created association list that
3033 -- will be attached to the final record aggregate. Note that if the
3034 -- Parent pointer of Expr is not set then Expr was produced with a
3035 -- New_Copy_Tree or some such.
3037 ---------------------
3038 -- Add_Association --
3039 ---------------------
3041 procedure Add_Association
3042 (Component : Entity_Id;
3043 Expr : Node_Id;
3044 Assoc_List : List_Id;
3045 Is_Box_Present : Boolean := False)
3047 Loc : Source_Ptr;
3048 Choice_List : constant List_Id := New_List;
3049 New_Assoc : Node_Id;
3051 begin
3052 -- If this is a box association the expression is missing, so
3053 -- use the Sloc of the aggregate itself for the new association.
3055 if Present (Expr) then
3056 Loc := Sloc (Expr);
3057 else
3058 Loc := Sloc (N);
3059 end if;
3061 Append (New_Occurrence_Of (Component, Loc), Choice_List);
3062 New_Assoc :=
3063 Make_Component_Association (Loc,
3064 Choices => Choice_List,
3065 Expression => Expr,
3066 Box_Present => Is_Box_Present);
3067 Append (New_Assoc, Assoc_List);
3068 end Add_Association;
3070 -------------------
3071 -- Discr_Present --
3072 -------------------
3074 function Discr_Present (Discr : Entity_Id) return Boolean is
3075 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
3077 Loc : Source_Ptr;
3079 Ancestor : Node_Id;
3080 Comp_Assoc : Node_Id;
3081 Discr_Expr : Node_Id;
3083 Ancestor_Typ : Entity_Id;
3084 Orig_Discr : Entity_Id;
3085 D : Entity_Id;
3086 D_Val : Elmt_Id := No_Elmt; -- stop junk warning
3088 Ancestor_Is_Subtyp : Boolean;
3090 begin
3091 if Regular_Aggr then
3092 return True;
3093 end if;
3095 -- Check whether inherited discriminant values have already been
3096 -- inserted in the aggregate. This will be the case if we are
3097 -- re-analyzing an aggregate whose expansion was delayed.
3099 if Present (Component_Associations (N)) then
3100 Comp_Assoc := First (Component_Associations (N));
3101 while Present (Comp_Assoc) loop
3102 if Inherited_Discriminant (Comp_Assoc) then
3103 return True;
3104 end if;
3106 Next (Comp_Assoc);
3107 end loop;
3108 end if;
3110 Ancestor := Ancestor_Part (N);
3111 Ancestor_Typ := Etype (Ancestor);
3112 Loc := Sloc (Ancestor);
3114 -- For a private type with unknown discriminants, use the underlying
3115 -- record view if it is available.
3117 if Has_Unknown_Discriminants (Ancestor_Typ)
3118 and then Present (Full_View (Ancestor_Typ))
3119 and then Present (Underlying_Record_View (Full_View (Ancestor_Typ)))
3120 then
3121 Ancestor_Typ := Underlying_Record_View (Full_View (Ancestor_Typ));
3122 end if;
3124 Ancestor_Is_Subtyp :=
3125 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
3127 -- If the ancestor part has no discriminants clearly N's aggregate
3128 -- part must provide a value for Discr.
3130 if not Has_Discriminants (Ancestor_Typ) then
3131 return True;
3133 -- If the ancestor part is an unconstrained subtype mark then the
3134 -- Discr must be present in N's aggregate part.
3136 elsif Ancestor_Is_Subtyp
3137 and then not Is_Constrained (Entity (Ancestor))
3138 then
3139 return True;
3140 end if;
3142 -- Now look to see if Discr was specified in the ancestor part
3144 if Ancestor_Is_Subtyp then
3145 D_Val := First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
3146 end if;
3148 Orig_Discr := Original_Record_Component (Discr);
3150 D := First_Discriminant (Ancestor_Typ);
3151 while Present (D) loop
3153 -- If Ancestor has already specified Disc value then insert its
3154 -- value in the final aggregate.
3156 if Original_Record_Component (D) = Orig_Discr then
3157 if Ancestor_Is_Subtyp then
3158 Discr_Expr := New_Copy_Tree (Node (D_Val));
3159 else
3160 Discr_Expr :=
3161 Make_Selected_Component (Loc,
3162 Prefix => Duplicate_Subexpr (Ancestor),
3163 Selector_Name => New_Occurrence_Of (Discr, Loc));
3164 end if;
3166 Resolve_Aggr_Expr (Discr_Expr, Discr);
3167 Set_Inherited_Discriminant (Last (New_Assoc_List));
3168 return False;
3169 end if;
3171 Next_Discriminant (D);
3173 if Ancestor_Is_Subtyp then
3174 Next_Elmt (D_Val);
3175 end if;
3176 end loop;
3178 return True;
3179 end Discr_Present;
3181 ---------------
3182 -- Get_Value --
3183 ---------------
3185 function Get_Value
3186 (Compon : Node_Id;
3187 From : List_Id;
3188 Consider_Others_Choice : Boolean := False)
3189 return Node_Id
3191 Typ : constant Entity_Id := Etype (Compon);
3192 Assoc : Node_Id;
3193 Expr : Node_Id := Empty;
3194 Selector_Name : Node_Id;
3196 begin
3197 Is_Box_Present := False;
3199 if No (From) then
3200 return Empty;
3201 end if;
3203 Assoc := First (From);
3204 while Present (Assoc) loop
3205 Selector_Name := First (Choices (Assoc));
3206 while Present (Selector_Name) loop
3207 if Nkind (Selector_Name) = N_Others_Choice then
3208 if Consider_Others_Choice and then No (Expr) then
3210 -- We need to duplicate the expression for each
3211 -- successive component covered by the others choice.
3212 -- This is redundant if the others_choice covers only
3213 -- one component (small optimization possible???), but
3214 -- indispensable otherwise, because each one must be
3215 -- expanded individually to preserve side-effects.
3217 -- Ada 2005 (AI-287): In case of default initialization
3218 -- of components, we duplicate the corresponding default
3219 -- expression (from the record type declaration). The
3220 -- copy must carry the sloc of the association (not the
3221 -- original expression) to prevent spurious elaboration
3222 -- checks when the default includes function calls.
3224 if Box_Present (Assoc) then
3225 Others_Box := True;
3226 Is_Box_Present := True;
3228 if Expander_Active then
3229 return
3230 New_Copy_Tree_And_Copy_Dimensions
3231 (Expression (Parent (Compon)),
3232 New_Sloc => Sloc (Assoc));
3233 else
3234 return Expression (Parent (Compon));
3235 end if;
3237 else
3238 if Present (Others_Etype)
3239 and then Base_Type (Others_Etype) /= Base_Type (Typ)
3240 then
3241 -- If the components are of an anonymous access
3242 -- type they are distinct, but this is legal in
3243 -- Ada 2012 as long as designated types match.
3245 if (Ekind (Typ) = E_Anonymous_Access_Type
3246 or else Ekind (Typ) =
3247 E_Anonymous_Access_Subprogram_Type)
3248 and then Designated_Type (Typ) =
3249 Designated_Type (Others_Etype)
3250 then
3251 null;
3252 else
3253 Error_Msg_N
3254 ("components in OTHERS choice must "
3255 & "have same type", Selector_Name);
3256 end if;
3257 end if;
3259 Others_Etype := Typ;
3261 -- Copy expression so that it is resolved
3262 -- independently for each component, This is needed
3263 -- for accessibility checks on compoents of anonymous
3264 -- access types, even in compile_only mode.
3266 if not Inside_A_Generic then
3268 -- In ASIS mode, preanalyze the expression in an
3269 -- others association before making copies for
3270 -- separate resolution and accessibility checks.
3271 -- This ensures that the type of the expression is
3272 -- available to ASIS in all cases, in particular if
3273 -- the expression is itself an aggregate.
3275 if ASIS_Mode then
3276 Preanalyze_And_Resolve (Expression (Assoc), Typ);
3277 end if;
3279 return
3280 New_Copy_Tree_And_Copy_Dimensions
3281 (Expression (Assoc));
3283 else
3284 return Expression (Assoc);
3285 end if;
3286 end if;
3287 end if;
3289 elsif Chars (Compon) = Chars (Selector_Name) then
3290 if No (Expr) then
3292 -- Ada 2005 (AI-231)
3294 if Ada_Version >= Ada_2005
3295 and then Known_Null (Expression (Assoc))
3296 then
3297 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
3298 end if;
3300 -- We need to duplicate the expression when several
3301 -- components are grouped together with a "|" choice.
3302 -- For instance "filed1 | filed2 => Expr"
3304 -- Ada 2005 (AI-287)
3306 if Box_Present (Assoc) then
3307 Is_Box_Present := True;
3309 -- Duplicate the default expression of the component
3310 -- from the record type declaration, so a new copy
3311 -- can be attached to the association.
3313 -- Note that we always copy the default expression,
3314 -- even when the association has a single choice, in
3315 -- order to create a proper association for the
3316 -- expanded aggregate.
3318 -- Component may have no default, in which case the
3319 -- expression is empty and the component is default-
3320 -- initialized, but an association for the component
3321 -- exists, and it is not covered by an others clause.
3323 -- Scalar and private types have no initialization
3324 -- procedure, so they remain uninitialized. If the
3325 -- target of the aggregate is a constant this
3326 -- deserves a warning.
3328 if No (Expression (Parent (Compon)))
3329 and then not Has_Non_Null_Base_Init_Proc (Typ)
3330 and then not Has_Aspect (Typ, Aspect_Default_Value)
3331 and then not Is_Concurrent_Type (Typ)
3332 and then Nkind (Parent (N)) = N_Object_Declaration
3333 and then Constant_Present (Parent (N))
3334 then
3335 Error_Msg_Node_2 := Typ;
3336 Error_Msg_NE
3337 ("component&? of type& is uninitialized",
3338 Assoc, Selector_Name);
3340 -- An additional reminder if the component type
3341 -- is a generic formal.
3343 if Is_Generic_Type (Base_Type (Typ)) then
3344 Error_Msg_NE
3345 ("\instance should provide actual type with "
3346 & "initialization for&", Assoc, Typ);
3347 end if;
3348 end if;
3350 return
3351 New_Copy_Tree_And_Copy_Dimensions
3352 (Expression (Parent (Compon)));
3354 else
3355 if Present (Next (Selector_Name)) then
3356 Expr := New_Copy_Tree_And_Copy_Dimensions
3357 (Expression (Assoc));
3358 else
3359 Expr := Expression (Assoc);
3360 end if;
3361 end if;
3363 Generate_Reference (Compon, Selector_Name, 'm');
3365 else
3366 Error_Msg_NE
3367 ("more than one value supplied for &",
3368 Selector_Name, Compon);
3370 end if;
3371 end if;
3373 Next (Selector_Name);
3374 end loop;
3376 Next (Assoc);
3377 end loop;
3379 return Expr;
3380 end Get_Value;
3382 ---------------------------------------
3383 -- New_Copy_Tree_And_Copy_Dimensions --
3384 ---------------------------------------
3386 function New_Copy_Tree_And_Copy_Dimensions
3387 (Source : Node_Id;
3388 Map : Elist_Id := No_Elist;
3389 New_Sloc : Source_Ptr := No_Location;
3390 New_Scope : Entity_Id := Empty) return Node_Id
3392 New_Copy : constant Node_Id :=
3393 New_Copy_Tree (Source, Map, New_Sloc, New_Scope);
3395 begin
3396 -- Move the dimensions of Source to New_Copy
3398 Copy_Dimensions (Source, New_Copy);
3399 return New_Copy;
3400 end New_Copy_Tree_And_Copy_Dimensions;
3402 -----------------------
3403 -- Resolve_Aggr_Expr --
3404 -----------------------
3406 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id) is
3407 Expr_Type : Entity_Id := Empty;
3408 New_C : Entity_Id := Component;
3409 New_Expr : Node_Id;
3411 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
3412 -- If the expression is an aggregate (possibly qualified) then its
3413 -- expansion is delayed until the enclosing aggregate is expanded
3414 -- into assignments. In that case, do not generate checks on the
3415 -- expression, because they will be generated later, and will other-
3416 -- wise force a copy (to remove side-effects) that would leave a
3417 -- dynamic-sized aggregate in the code, something that gigi cannot
3418 -- handle.
3420 Relocate : Boolean;
3421 -- Set to True if the resolved Expr node needs to be relocated when
3422 -- attached to the newly created association list. This node need not
3423 -- be relocated if its parent pointer is not set. In fact in this
3424 -- case Expr is the output of a New_Copy_Tree call. If Relocate is
3425 -- True then we have analyzed the expression node in the original
3426 -- aggregate and hence it needs to be relocated when moved over to
3427 -- the new association list.
3429 ---------------------------
3430 -- Has_Expansion_Delayed --
3431 ---------------------------
3433 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
3434 Kind : constant Node_Kind := Nkind (Expr);
3435 begin
3436 return (Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate)
3437 and then Present (Etype (Expr))
3438 and then Is_Record_Type (Etype (Expr))
3439 and then Expansion_Delayed (Expr))
3440 or else (Kind = N_Qualified_Expression
3441 and then Has_Expansion_Delayed (Expression (Expr)));
3442 end Has_Expansion_Delayed;
3444 -- Start of processing for Resolve_Aggr_Expr
3446 begin
3447 -- If the type of the component is elementary or the type of the
3448 -- aggregate does not contain discriminants, use the type of the
3449 -- component to resolve Expr.
3451 if Is_Elementary_Type (Etype (Component))
3452 or else not Has_Discriminants (Etype (N))
3453 then
3454 Expr_Type := Etype (Component);
3456 -- Otherwise we have to pick up the new type of the component from
3457 -- the new constrained subtype of the aggregate. In fact components
3458 -- which are of a composite type might be constrained by a
3459 -- discriminant, and we want to resolve Expr against the subtype were
3460 -- all discriminant occurrences are replaced with their actual value.
3462 else
3463 New_C := First_Component (Etype (N));
3464 while Present (New_C) loop
3465 if Chars (New_C) = Chars (Component) then
3466 Expr_Type := Etype (New_C);
3467 exit;
3468 end if;
3470 Next_Component (New_C);
3471 end loop;
3473 pragma Assert (Present (Expr_Type));
3475 -- For each range in an array type where a discriminant has been
3476 -- replaced with the constraint, check that this range is within
3477 -- the range of the base type. This checks is done in the init
3478 -- proc for regular objects, but has to be done here for
3479 -- aggregates since no init proc is called for them.
3481 if Is_Array_Type (Expr_Type) then
3482 declare
3483 Index : Node_Id;
3484 -- Range of the current constrained index in the array
3486 Orig_Index : Node_Id := First_Index (Etype (Component));
3487 -- Range corresponding to the range Index above in the
3488 -- original unconstrained record type. The bounds of this
3489 -- range may be governed by discriminants.
3491 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
3492 -- Range corresponding to the range Index above for the
3493 -- unconstrained array type. This range is needed to apply
3494 -- range checks.
3496 begin
3497 Index := First_Index (Expr_Type);
3498 while Present (Index) loop
3499 if Depends_On_Discriminant (Orig_Index) then
3500 Apply_Range_Check (Index, Etype (Unconstr_Index));
3501 end if;
3503 Next_Index (Index);
3504 Next_Index (Orig_Index);
3505 Next_Index (Unconstr_Index);
3506 end loop;
3507 end;
3508 end if;
3509 end if;
3511 -- If the Parent pointer of Expr is not set, Expr is an expression
3512 -- duplicated by New_Tree_Copy (this happens for record aggregates
3513 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
3514 -- Such a duplicated expression must be attached to the tree
3515 -- before analysis and resolution to enforce the rule that a tree
3516 -- fragment should never be analyzed or resolved unless it is
3517 -- attached to the current compilation unit.
3519 if No (Parent (Expr)) then
3520 Set_Parent (Expr, N);
3521 Relocate := False;
3522 else
3523 Relocate := True;
3524 end if;
3526 Analyze_And_Resolve (Expr, Expr_Type);
3527 Check_Expr_OK_In_Limited_Aggregate (Expr);
3528 Check_Non_Static_Context (Expr);
3529 Check_Unset_Reference (Expr);
3531 -- Check wrong use of class-wide types
3533 if Is_Class_Wide_Type (Etype (Expr)) then
3534 Error_Msg_N ("dynamically tagged expression not allowed", Expr);
3535 end if;
3537 if not Has_Expansion_Delayed (Expr) then
3538 Aggregate_Constraint_Checks (Expr, Expr_Type);
3539 end if;
3541 -- If an aggregate component has a type with predicates, an explicit
3542 -- predicate check must be applied, as for an assignment statement,
3543 -- because the aggegate might not be expanded into individual
3544 -- component assignments.
3546 if Present (Predicate_Function (Expr_Type)) then
3547 Apply_Predicate_Check (Expr, Expr_Type);
3548 end if;
3550 if Raises_Constraint_Error (Expr) then
3551 Set_Raises_Constraint_Error (N);
3552 end if;
3554 -- If the expression has been marked as requiring a range check, then
3555 -- generate it here. It's a bit odd to be generating such checks in
3556 -- the analyzer, but harmless since Generate_Range_Check does nothing
3557 -- (other than making sure Do_Range_Check is set) if the expander is
3558 -- not active.
3560 if Do_Range_Check (Expr) then
3561 Generate_Range_Check (Expr, Expr_Type, CE_Range_Check_Failed);
3562 end if;
3564 if Relocate then
3565 New_Expr := Relocate_Node (Expr);
3567 -- Since New_Expr is not gonna be analyzed later on, we need to
3568 -- propagate here the dimensions form Expr to New_Expr.
3570 Copy_Dimensions (Expr, New_Expr);
3572 else
3573 New_Expr := Expr;
3574 end if;
3576 Add_Association (New_C, New_Expr, New_Assoc_List);
3577 end Resolve_Aggr_Expr;
3579 -- Start of processing for Resolve_Record_Aggregate
3581 begin
3582 -- A record aggregate is restricted in SPARK:
3584 -- Each named association can have only a single choice.
3585 -- OTHERS cannot be used.
3586 -- Positional and named associations cannot be mixed.
3588 if Present (Component_Associations (N))
3589 and then Present (First (Component_Associations (N)))
3590 then
3592 if Present (Expressions (N)) then
3593 Check_SPARK_05_Restriction
3594 ("named association cannot follow positional one",
3595 First (Choices (First (Component_Associations (N)))));
3596 end if;
3598 declare
3599 Assoc : Node_Id;
3601 begin
3602 Assoc := First (Component_Associations (N));
3603 while Present (Assoc) loop
3604 if List_Length (Choices (Assoc)) > 1 then
3605 Check_SPARK_05_Restriction
3606 ("component association in record aggregate must "
3607 & "contain a single choice", Assoc);
3608 end if;
3610 if Nkind (First (Choices (Assoc))) = N_Others_Choice then
3611 Check_SPARK_05_Restriction
3612 ("record aggregate cannot contain OTHERS", Assoc);
3613 end if;
3615 Assoc := Next (Assoc);
3616 end loop;
3617 end;
3618 end if;
3620 -- We may end up calling Duplicate_Subexpr on expressions that are
3621 -- attached to New_Assoc_List. For this reason we need to attach it
3622 -- to the tree by setting its parent pointer to N. This parent point
3623 -- will change in STEP 8 below.
3625 Set_Parent (New_Assoc_List, N);
3627 -- STEP 1: abstract type and null record verification
3629 if Is_Abstract_Type (Typ) then
3630 Error_Msg_N ("type of aggregate cannot be abstract", N);
3631 end if;
3633 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
3634 Set_Etype (N, Typ);
3635 return;
3637 elsif Present (First_Entity (Typ))
3638 and then Null_Record_Present (N)
3639 and then not Is_Tagged_Type (Typ)
3640 then
3641 Error_Msg_N ("record aggregate cannot be null", N);
3642 return;
3644 -- If the type has no components, then the aggregate should either
3645 -- have "null record", or in Ada 2005 it could instead have a single
3646 -- component association given by "others => <>". For Ada 95 we flag an
3647 -- error at this point, but for Ada 2005 we proceed with checking the
3648 -- associations below, which will catch the case where it's not an
3649 -- aggregate with "others => <>". Note that the legality of a <>
3650 -- aggregate for a null record type was established by AI05-016.
3652 elsif No (First_Entity (Typ))
3653 and then Ada_Version < Ada_2005
3654 then
3655 Error_Msg_N ("record aggregate must be null", N);
3656 return;
3657 end if;
3659 -- STEP 2: Verify aggregate structure
3661 Step_2 : declare
3662 Selector_Name : Node_Id;
3663 Bad_Aggregate : Boolean := False;
3665 begin
3666 if Present (Component_Associations (N)) then
3667 Assoc := First (Component_Associations (N));
3668 else
3669 Assoc := Empty;
3670 end if;
3672 while Present (Assoc) loop
3673 Selector_Name := First (Choices (Assoc));
3674 while Present (Selector_Name) loop
3675 if Nkind (Selector_Name) = N_Identifier then
3676 null;
3678 elsif Nkind (Selector_Name) = N_Others_Choice then
3679 if Selector_Name /= First (Choices (Assoc))
3680 or else Present (Next (Selector_Name))
3681 then
3682 Error_Msg_N
3683 ("OTHERS must appear alone in a choice list",
3684 Selector_Name);
3685 return;
3687 elsif Present (Next (Assoc)) then
3688 Error_Msg_N
3689 ("OTHERS must appear last in an aggregate",
3690 Selector_Name);
3691 return;
3693 -- (Ada 2005): If this is an association with a box,
3694 -- indicate that the association need not represent
3695 -- any component.
3697 elsif Box_Present (Assoc) then
3698 Others_Box := True;
3699 end if;
3701 else
3702 Error_Msg_N
3703 ("selector name should be identifier or OTHERS",
3704 Selector_Name);
3705 Bad_Aggregate := True;
3706 end if;
3708 Next (Selector_Name);
3709 end loop;
3711 Next (Assoc);
3712 end loop;
3714 if Bad_Aggregate then
3715 return;
3716 end if;
3717 end Step_2;
3719 -- STEP 3: Find discriminant Values
3721 Step_3 : declare
3722 Discrim : Entity_Id;
3723 Missing_Discriminants : Boolean := False;
3725 begin
3726 if Present (Expressions (N)) then
3727 Positional_Expr := First (Expressions (N));
3728 else
3729 Positional_Expr := Empty;
3730 end if;
3732 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
3733 -- must not have unknown discriminants.
3735 if Is_Derived_Type (Typ)
3736 and then Has_Unknown_Discriminants (Root_Type (Typ))
3737 and then Nkind (N) /= N_Extension_Aggregate
3738 then
3739 Error_Msg_NE
3740 ("aggregate not available for type& whose ancestor "
3741 & "has unknown discriminants ", N, Typ);
3742 end if;
3744 if Has_Unknown_Discriminants (Typ)
3745 and then Present (Underlying_Record_View (Typ))
3746 then
3747 Discrim := First_Discriminant (Underlying_Record_View (Typ));
3748 elsif Has_Discriminants (Typ) then
3749 Discrim := First_Discriminant (Typ);
3750 else
3751 Discrim := Empty;
3752 end if;
3754 -- First find the discriminant values in the positional components
3756 while Present (Discrim) and then Present (Positional_Expr) loop
3757 if Discr_Present (Discrim) then
3758 Resolve_Aggr_Expr (Positional_Expr, Discrim);
3760 -- Ada 2005 (AI-231)
3762 if Ada_Version >= Ada_2005
3763 and then Known_Null (Positional_Expr)
3764 then
3765 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
3766 end if;
3768 Next (Positional_Expr);
3769 end if;
3771 if Present (Get_Value (Discrim, Component_Associations (N))) then
3772 Error_Msg_NE
3773 ("more than one value supplied for discriminant&",
3774 N, Discrim);
3775 end if;
3777 Next_Discriminant (Discrim);
3778 end loop;
3780 -- Find remaining discriminant values if any among named components
3782 while Present (Discrim) loop
3783 Expr := Get_Value (Discrim, Component_Associations (N), True);
3785 if not Discr_Present (Discrim) then
3786 if Present (Expr) then
3787 Error_Msg_NE
3788 ("more than one value supplied for discriminant &",
3789 N, Discrim);
3790 end if;
3792 elsif No (Expr) then
3793 Error_Msg_NE
3794 ("no value supplied for discriminant &", N, Discrim);
3795 Missing_Discriminants := True;
3797 else
3798 Resolve_Aggr_Expr (Expr, Discrim);
3799 end if;
3801 Next_Discriminant (Discrim);
3802 end loop;
3804 if Missing_Discriminants then
3805 return;
3806 end if;
3808 -- At this point and until the beginning of STEP 6, New_Assoc_List
3809 -- contains only the discriminants and their values.
3811 end Step_3;
3813 -- STEP 4: Set the Etype of the record aggregate
3815 -- ??? This code is pretty much a copy of Sem_Ch3.Build_Subtype. That
3816 -- routine should really be exported in sem_util or some such and used
3817 -- in sem_ch3 and here rather than have a copy of the code which is a
3818 -- maintenance nightmare.
3820 -- ??? Performance WARNING. The current implementation creates a new
3821 -- itype for all aggregates whose base type is discriminated. This means
3822 -- that for record aggregates nested inside an array aggregate we will
3823 -- create a new itype for each record aggregate if the array component
3824 -- type has discriminants. For large aggregates this may be a problem.
3825 -- What should be done in this case is to reuse itypes as much as
3826 -- possible.
3828 if Has_Discriminants (Typ)
3829 or else (Has_Unknown_Discriminants (Typ)
3830 and then Present (Underlying_Record_View (Typ)))
3831 then
3832 Build_Constrained_Itype : declare
3833 Loc : constant Source_Ptr := Sloc (N);
3834 Indic : Node_Id;
3835 Subtyp_Decl : Node_Id;
3836 Def_Id : Entity_Id;
3838 C : constant List_Id := New_List;
3840 begin
3841 New_Assoc := First (New_Assoc_List);
3842 while Present (New_Assoc) loop
3843 Append (Duplicate_Subexpr (Expression (New_Assoc)), To => C);
3844 Next (New_Assoc);
3845 end loop;
3847 if Has_Unknown_Discriminants (Typ)
3848 and then Present (Underlying_Record_View (Typ))
3849 then
3850 Indic :=
3851 Make_Subtype_Indication (Loc,
3852 Subtype_Mark =>
3853 New_Occurrence_Of (Underlying_Record_View (Typ), Loc),
3854 Constraint =>
3855 Make_Index_Or_Discriminant_Constraint (Loc, C));
3856 else
3857 Indic :=
3858 Make_Subtype_Indication (Loc,
3859 Subtype_Mark =>
3860 New_Occurrence_Of (Base_Type (Typ), Loc),
3861 Constraint =>
3862 Make_Index_Or_Discriminant_Constraint (Loc, C));
3863 end if;
3865 Def_Id := Create_Itype (Ekind (Typ), N);
3867 Subtyp_Decl :=
3868 Make_Subtype_Declaration (Loc,
3869 Defining_Identifier => Def_Id,
3870 Subtype_Indication => Indic);
3871 Set_Parent (Subtyp_Decl, Parent (N));
3873 -- Itypes must be analyzed with checks off (see itypes.ads)
3875 Analyze (Subtyp_Decl, Suppress => All_Checks);
3877 Set_Etype (N, Def_Id);
3878 Check_Static_Discriminated_Subtype
3879 (Def_Id, Expression (First (New_Assoc_List)));
3880 end Build_Constrained_Itype;
3882 else
3883 Set_Etype (N, Typ);
3884 end if;
3886 -- STEP 5: Get remaining components according to discriminant values
3888 Step_5 : declare
3889 Record_Def : Node_Id;
3890 Parent_Typ : Entity_Id;
3891 Root_Typ : Entity_Id;
3892 Parent_Typ_List : Elist_Id;
3893 Parent_Elmt : Elmt_Id;
3894 Errors_Found : Boolean := False;
3895 Dnode : Node_Id;
3897 function Find_Private_Ancestor return Entity_Id;
3898 -- AI05-0115: Find earlier ancestor in the derivation chain that is
3899 -- derived from a private view. Whether the aggregate is legal
3900 -- depends on the current visibility of the type as well as that
3901 -- of the parent of the ancestor.
3903 ---------------------------
3904 -- Find_Private_Ancestor --
3905 ---------------------------
3907 function Find_Private_Ancestor return Entity_Id is
3908 Par : Entity_Id;
3910 begin
3911 Par := Typ;
3912 loop
3913 if Has_Private_Ancestor (Par)
3914 and then not Has_Private_Ancestor (Etype (Base_Type (Par)))
3915 then
3916 return Par;
3918 elsif not Is_Derived_Type (Par) then
3919 return Empty;
3921 else
3922 Par := Etype (Base_Type (Par));
3923 end if;
3924 end loop;
3925 end Find_Private_Ancestor;
3927 -- Start of processing for Step_5
3929 begin
3930 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
3931 Parent_Typ_List := New_Elmt_List;
3933 -- If this is an extension aggregate, the component list must
3934 -- include all components that are not in the given ancestor type.
3935 -- Otherwise, the component list must include components of all
3936 -- ancestors, starting with the root.
3938 if Nkind (N) = N_Extension_Aggregate then
3939 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
3941 else
3942 -- AI05-0115: check legality of aggregate for type with
3943 -- aa private ancestor.
3945 Root_Typ := Root_Type (Typ);
3946 if Has_Private_Ancestor (Typ) then
3947 declare
3948 Ancestor : constant Entity_Id :=
3949 Find_Private_Ancestor;
3950 Ancestor_Unit : constant Entity_Id :=
3951 Cunit_Entity (Get_Source_Unit (Ancestor));
3952 Parent_Unit : constant Entity_Id :=
3953 Cunit_Entity
3954 (Get_Source_Unit (Base_Type (Etype (Ancestor))));
3955 begin
3956 -- Check whether we are in a scope that has full view
3957 -- over the private ancestor and its parent. This can
3958 -- only happen if the derivation takes place in a child
3959 -- unit of the unit that declares the parent, and we are
3960 -- in the private part or body of that child unit, else
3961 -- the aggregate is illegal.
3963 if Is_Child_Unit (Ancestor_Unit)
3964 and then Scope (Ancestor_Unit) = Parent_Unit
3965 and then In_Open_Scopes (Scope (Ancestor))
3966 and then
3967 (In_Private_Part (Scope (Ancestor))
3968 or else In_Package_Body (Scope (Ancestor)))
3969 then
3970 null;
3972 else
3973 Error_Msg_NE
3974 ("type of aggregate has private ancestor&!",
3975 N, Root_Typ);
3976 Error_Msg_N ("must use extension aggregate!", N);
3977 return;
3978 end if;
3979 end;
3980 end if;
3982 Dnode := Declaration_Node (Base_Type (Root_Typ));
3984 -- If we don't get a full declaration, then we have some error
3985 -- which will get signalled later so skip this part. Otherwise
3986 -- gather components of root that apply to the aggregate type.
3987 -- We use the base type in case there is an applicable stored
3988 -- constraint that renames the discriminants of the root.
3990 if Nkind (Dnode) = N_Full_Type_Declaration then
3991 Record_Def := Type_Definition (Dnode);
3992 Gather_Components
3993 (Base_Type (Typ),
3994 Component_List (Record_Def),
3995 Governed_By => New_Assoc_List,
3996 Into => Components,
3997 Report_Errors => Errors_Found);
3999 if Errors_Found then
4000 Error_Msg_N
4001 ("discriminant controlling variant part is not static",
4003 return;
4004 end if;
4005 end if;
4006 end if;
4008 Parent_Typ := Base_Type (Typ);
4009 while Parent_Typ /= Root_Typ loop
4010 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
4011 Parent_Typ := Etype (Parent_Typ);
4013 if Nkind (Parent (Base_Type (Parent_Typ))) =
4014 N_Private_Type_Declaration
4015 or else Nkind (Parent (Base_Type (Parent_Typ))) =
4016 N_Private_Extension_Declaration
4017 then
4018 if Nkind (N) /= N_Extension_Aggregate then
4019 Error_Msg_NE
4020 ("type of aggregate has private ancestor&!",
4021 N, Parent_Typ);
4022 Error_Msg_N ("must use extension aggregate!", N);
4023 return;
4025 elsif Parent_Typ /= Root_Typ then
4026 Error_Msg_NE
4027 ("ancestor part of aggregate must be private type&",
4028 Ancestor_Part (N), Parent_Typ);
4029 return;
4030 end if;
4032 -- The current view of ancestor part may be a private type,
4033 -- while the context type is always non-private.
4035 elsif Is_Private_Type (Root_Typ)
4036 and then Present (Full_View (Root_Typ))
4037 and then Nkind (N) = N_Extension_Aggregate
4038 then
4039 exit when Base_Type (Full_View (Root_Typ)) = Parent_Typ;
4040 end if;
4041 end loop;
4043 -- Now collect components from all other ancestors, beginning
4044 -- with the current type. If the type has unknown discriminants
4045 -- use the component list of the Underlying_Record_View, which
4046 -- needs to be used for the subsequent expansion of the aggregate
4047 -- into assignments.
4049 Parent_Elmt := First_Elmt (Parent_Typ_List);
4050 while Present (Parent_Elmt) loop
4051 Parent_Typ := Node (Parent_Elmt);
4053 if Has_Unknown_Discriminants (Parent_Typ)
4054 and then Present (Underlying_Record_View (Typ))
4055 then
4056 Parent_Typ := Underlying_Record_View (Parent_Typ);
4057 end if;
4059 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
4060 Gather_Components (Empty,
4061 Component_List (Record_Extension_Part (Record_Def)),
4062 Governed_By => New_Assoc_List,
4063 Into => Components,
4064 Report_Errors => Errors_Found);
4066 Next_Elmt (Parent_Elmt);
4067 end loop;
4069 -- Typ is not a derived tagged type
4071 else
4072 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
4074 if Null_Present (Record_Def) then
4075 null;
4077 elsif not Has_Unknown_Discriminants (Typ) then
4078 Gather_Components
4079 (Base_Type (Typ),
4080 Component_List (Record_Def),
4081 Governed_By => New_Assoc_List,
4082 Into => Components,
4083 Report_Errors => Errors_Found);
4085 else
4086 Gather_Components
4087 (Base_Type (Underlying_Record_View (Typ)),
4088 Component_List (Record_Def),
4089 Governed_By => New_Assoc_List,
4090 Into => Components,
4091 Report_Errors => Errors_Found);
4092 end if;
4093 end if;
4095 if Errors_Found then
4096 return;
4097 end if;
4098 end Step_5;
4100 -- STEP 6: Find component Values
4102 Component := Empty;
4103 Component_Elmt := First_Elmt (Components);
4105 -- First scan the remaining positional associations in the aggregate.
4106 -- Remember that at this point Positional_Expr contains the current
4107 -- positional association if any is left after looking for discriminant
4108 -- values in step 3.
4110 while Present (Positional_Expr) and then Present (Component_Elmt) loop
4111 Component := Node (Component_Elmt);
4112 Resolve_Aggr_Expr (Positional_Expr, Component);
4114 -- Ada 2005 (AI-231)
4116 if Ada_Version >= Ada_2005 and then Known_Null (Positional_Expr) then
4117 Check_Can_Never_Be_Null (Component, Positional_Expr);
4118 end if;
4120 if Present (Get_Value (Component, Component_Associations (N))) then
4121 Error_Msg_NE
4122 ("more than one value supplied for Component &", N, Component);
4123 end if;
4125 Next (Positional_Expr);
4126 Next_Elmt (Component_Elmt);
4127 end loop;
4129 if Present (Positional_Expr) then
4130 Error_Msg_N
4131 ("too many components for record aggregate", Positional_Expr);
4132 end if;
4134 -- Now scan for the named arguments of the aggregate
4136 while Present (Component_Elmt) loop
4137 Component := Node (Component_Elmt);
4138 Expr := Get_Value (Component, Component_Associations (N), True);
4140 -- Note: The previous call to Get_Value sets the value of the
4141 -- variable Is_Box_Present.
4143 -- Ada 2005 (AI-287): Handle components with default initialization.
4144 -- Note: This feature was originally added to Ada 2005 for limited
4145 -- but it was finally allowed with any type.
4147 if Is_Box_Present then
4148 Check_Box_Component : declare
4149 Ctyp : constant Entity_Id := Etype (Component);
4151 begin
4152 -- If there is a default expression for the aggregate, copy
4153 -- it into a new association. This copy must modify the scopes
4154 -- of internal types that may be attached to the expression
4155 -- (e.g. index subtypes of arrays) because in general the type
4156 -- declaration and the aggregate appear in different scopes,
4157 -- and the backend requires the scope of the type to match the
4158 -- point at which it is elaborated.
4160 -- If the component has an initialization procedure (IP) we
4161 -- pass the component to the expander, which will generate
4162 -- the call to such IP.
4164 -- If the component has discriminants, their values must
4165 -- be taken from their subtype. This is indispensable for
4166 -- constraints that are given by the current instance of an
4167 -- enclosing type, to allow the expansion of the aggregate to
4168 -- replace the reference to the current instance by the target
4169 -- object of the aggregate.
4171 if Present (Parent (Component))
4172 and then
4173 Nkind (Parent (Component)) = N_Component_Declaration
4174 and then Present (Expression (Parent (Component)))
4175 then
4176 Expr :=
4177 New_Copy_Tree_And_Copy_Dimensions
4178 (Expression (Parent (Component)),
4179 New_Scope => Current_Scope,
4180 New_Sloc => Sloc (N));
4182 Add_Association
4183 (Component => Component,
4184 Expr => Expr,
4185 Assoc_List => New_Assoc_List);
4186 Set_Has_Self_Reference (N);
4188 -- A box-defaulted access component gets the value null. Also
4189 -- included are components of private types whose underlying
4190 -- type is an access type. In either case set the type of the
4191 -- literal, for subsequent use in semantic checks.
4193 elsif Present (Underlying_Type (Ctyp))
4194 and then Is_Access_Type (Underlying_Type (Ctyp))
4195 then
4196 if not Is_Private_Type (Ctyp) then
4197 Expr := Make_Null (Sloc (N));
4198 Set_Etype (Expr, Ctyp);
4199 Add_Association
4200 (Component => Component,
4201 Expr => Expr,
4202 Assoc_List => New_Assoc_List);
4204 -- If the component's type is private with an access type as
4205 -- its underlying type then we have to create an unchecked
4206 -- conversion to satisfy type checking.
4208 else
4209 declare
4210 Qual_Null : constant Node_Id :=
4211 Make_Qualified_Expression (Sloc (N),
4212 Subtype_Mark =>
4213 New_Occurrence_Of
4214 (Underlying_Type (Ctyp), Sloc (N)),
4215 Expression => Make_Null (Sloc (N)));
4217 Convert_Null : constant Node_Id :=
4218 Unchecked_Convert_To
4219 (Ctyp, Qual_Null);
4221 begin
4222 Analyze_And_Resolve (Convert_Null, Ctyp);
4223 Add_Association
4224 (Component => Component,
4225 Expr => Convert_Null,
4226 Assoc_List => New_Assoc_List);
4227 end;
4228 end if;
4230 -- Ada 2012: If component is scalar with default value, use it
4232 elsif Is_Scalar_Type (Ctyp)
4233 and then Has_Default_Aspect (Ctyp)
4234 then
4235 Add_Association
4236 (Component => Component,
4237 Expr => Default_Aspect_Value
4238 (First_Subtype (Underlying_Type (Ctyp))),
4239 Assoc_List => New_Assoc_List);
4241 elsif Has_Non_Null_Base_Init_Proc (Ctyp)
4242 or else not Expander_Active
4243 then
4244 if Is_Record_Type (Ctyp)
4245 and then Has_Discriminants (Ctyp)
4246 and then not Is_Private_Type (Ctyp)
4247 then
4248 -- We build a partially initialized aggregate with the
4249 -- values of the discriminants and box initialization
4250 -- for the rest, if other components are present.
4252 -- The type of the aggregate is the known subtype of
4253 -- the component. The capture of discriminants must
4254 -- be recursive because subcomponents may be constrained
4255 -- (transitively) by discriminants of enclosing types.
4256 -- For a private type with discriminants, a call to the
4257 -- initialization procedure will be generated, and no
4258 -- subaggregate is needed.
4260 Capture_Discriminants : declare
4261 Loc : constant Source_Ptr := Sloc (N);
4262 Expr : Node_Id;
4264 procedure Add_Discriminant_Values
4265 (New_Aggr : Node_Id;
4266 Assoc_List : List_Id);
4267 -- The constraint to a component may be given by a
4268 -- discriminant of the enclosing type, in which case
4269 -- we have to retrieve its value, which is part of the
4270 -- enclosing aggregate. Assoc_List provides the
4271 -- discriminant associations of the current type or
4272 -- of some enclosing record.
4274 procedure Propagate_Discriminants
4275 (Aggr : Node_Id;
4276 Assoc_List : List_Id);
4277 -- Nested components may themselves be discriminated
4278 -- types constrained by outer discriminants, whose
4279 -- values must be captured before the aggregate is
4280 -- expanded into assignments.
4282 -----------------------------
4283 -- Add_Discriminant_Values --
4284 -----------------------------
4286 procedure Add_Discriminant_Values
4287 (New_Aggr : Node_Id;
4288 Assoc_List : List_Id)
4290 Assoc : Node_Id;
4291 Discr : Entity_Id;
4292 Discr_Elmt : Elmt_Id;
4293 Discr_Val : Node_Id;
4294 Val : Entity_Id;
4296 begin
4297 Discr := First_Discriminant (Etype (New_Aggr));
4298 Discr_Elmt :=
4299 First_Elmt
4300 (Discriminant_Constraint (Etype (New_Aggr)));
4301 while Present (Discr_Elmt) loop
4302 Discr_Val := Node (Discr_Elmt);
4304 -- If the constraint is given by a discriminant
4305 -- it is a discriminant of an enclosing record,
4306 -- and its value has already been placed in the
4307 -- association list.
4309 if Is_Entity_Name (Discr_Val)
4310 and then
4311 Ekind (Entity (Discr_Val)) = E_Discriminant
4312 then
4313 Val := Entity (Discr_Val);
4315 Assoc := First (Assoc_List);
4316 while Present (Assoc) loop
4317 if Present
4318 (Entity (First (Choices (Assoc))))
4319 and then
4320 Entity (First (Choices (Assoc))) = Val
4321 then
4322 Discr_Val := Expression (Assoc);
4323 exit;
4324 end if;
4326 Next (Assoc);
4327 end loop;
4328 end if;
4330 Add_Association
4331 (Discr, New_Copy_Tree (Discr_Val),
4332 Component_Associations (New_Aggr));
4334 -- If the discriminant constraint is a current
4335 -- instance, mark the current aggregate so that
4336 -- the self-reference can be expanded later.
4337 -- The constraint may refer to the subtype of
4338 -- aggregate, so use base type for comparison.
4340 if Nkind (Discr_Val) = N_Attribute_Reference
4341 and then Is_Entity_Name (Prefix (Discr_Val))
4342 and then Is_Type (Entity (Prefix (Discr_Val)))
4343 and then Base_Type (Etype (N)) =
4344 Entity (Prefix (Discr_Val))
4345 then
4346 Set_Has_Self_Reference (N);
4347 end if;
4349 Next_Elmt (Discr_Elmt);
4350 Next_Discriminant (Discr);
4351 end loop;
4352 end Add_Discriminant_Values;
4354 -----------------------------
4355 -- Propagate_Discriminants --
4356 -----------------------------
4358 procedure Propagate_Discriminants
4359 (Aggr : Node_Id;
4360 Assoc_List : List_Id)
4362 Aggr_Type : constant Entity_Id :=
4363 Base_Type (Etype (Aggr));
4364 Def_Node : constant Node_Id :=
4365 Type_Definition
4366 (Declaration_Node (Aggr_Type));
4368 Comp : Node_Id;
4369 Comp_Elmt : Elmt_Id;
4370 Components : constant Elist_Id := New_Elmt_List;
4371 Needs_Box : Boolean := False;
4372 Errors : Boolean;
4374 procedure Process_Component (Comp : Entity_Id);
4375 -- Add one component with a box association to the
4376 -- inner aggregate, and recurse if component is
4377 -- itself composite.
4379 -----------------------
4380 -- Process_Component --
4381 -----------------------
4383 procedure Process_Component (Comp : Entity_Id) is
4384 T : constant Entity_Id := Etype (Comp);
4385 New_Aggr : Node_Id;
4387 begin
4388 if Is_Record_Type (T)
4389 and then Has_Discriminants (T)
4390 then
4391 New_Aggr :=
4392 Make_Aggregate (Loc, New_List, New_List);
4393 Set_Etype (New_Aggr, T);
4394 Add_Association
4395 (Comp, New_Aggr,
4396 Component_Associations (Aggr));
4398 -- Collect discriminant values and recurse
4400 Add_Discriminant_Values
4401 (New_Aggr, Assoc_List);
4402 Propagate_Discriminants
4403 (New_Aggr, Assoc_List);
4405 else
4406 Needs_Box := True;
4407 end if;
4408 end Process_Component;
4410 -- Start of processing for Propagate_Discriminants
4412 begin
4413 -- The component type may be a variant type, so
4414 -- collect the components that are ruled by the
4415 -- known values of the discriminants. Their values
4416 -- have already been inserted into the component
4417 -- list of the current aggregate.
4419 if Nkind (Def_Node) = N_Record_Definition
4420 and then Present (Component_List (Def_Node))
4421 and then
4422 Present
4423 (Variant_Part (Component_List (Def_Node)))
4424 then
4425 Gather_Components (Aggr_Type,
4426 Component_List (Def_Node),
4427 Governed_By => Component_Associations (Aggr),
4428 Into => Components,
4429 Report_Errors => Errors);
4431 Comp_Elmt := First_Elmt (Components);
4432 while Present (Comp_Elmt) loop
4433 if Ekind (Node (Comp_Elmt)) /= E_Discriminant
4434 then
4435 Process_Component (Node (Comp_Elmt));
4436 end if;
4438 Next_Elmt (Comp_Elmt);
4439 end loop;
4441 -- No variant part, iterate over all components
4443 else
4444 Comp := First_Component (Etype (Aggr));
4445 while Present (Comp) loop
4446 Process_Component (Comp);
4447 Next_Component (Comp);
4448 end loop;
4449 end if;
4451 if Needs_Box then
4452 Append_To (Component_Associations (Aggr),
4453 Make_Component_Association (Loc,
4454 Choices =>
4455 New_List (Make_Others_Choice (Loc)),
4456 Expression => Empty,
4457 Box_Present => True));
4458 end if;
4459 end Propagate_Discriminants;
4461 -- Start of processing for Capture_Discriminants
4463 begin
4464 Expr := Make_Aggregate (Loc, New_List, New_List);
4465 Set_Etype (Expr, Ctyp);
4467 -- If the enclosing type has discriminants, they have
4468 -- been collected in the aggregate earlier, and they
4469 -- may appear as constraints of subcomponents.
4471 -- Similarly if this component has discriminants, they
4472 -- might in turn be propagated to their components.
4474 if Has_Discriminants (Typ) then
4475 Add_Discriminant_Values (Expr, New_Assoc_List);
4476 Propagate_Discriminants (Expr, New_Assoc_List);
4478 elsif Has_Discriminants (Ctyp) then
4479 Add_Discriminant_Values
4480 (Expr, Component_Associations (Expr));
4481 Propagate_Discriminants
4482 (Expr, Component_Associations (Expr));
4484 else
4485 declare
4486 Comp : Entity_Id;
4488 begin
4489 -- If the type has additional components, create
4490 -- an OTHERS box association for them.
4492 Comp := First_Component (Ctyp);
4493 while Present (Comp) loop
4494 if Ekind (Comp) = E_Component then
4495 if not Is_Record_Type (Etype (Comp)) then
4496 Append_To
4497 (Component_Associations (Expr),
4498 Make_Component_Association (Loc,
4499 Choices =>
4500 New_List (
4501 Make_Others_Choice (Loc)),
4502 Expression => Empty,
4503 Box_Present => True));
4504 end if;
4505 exit;
4506 end if;
4508 Next_Component (Comp);
4509 end loop;
4510 end;
4511 end if;
4513 Add_Association
4514 (Component => Component,
4515 Expr => Expr,
4516 Assoc_List => New_Assoc_List);
4517 end Capture_Discriminants;
4519 else
4520 Add_Association
4521 (Component => Component,
4522 Expr => Empty,
4523 Assoc_List => New_Assoc_List,
4524 Is_Box_Present => True);
4525 end if;
4527 -- Otherwise we only need to resolve the expression if the
4528 -- component has partially initialized values (required to
4529 -- expand the corresponding assignments and run-time checks).
4531 elsif Present (Expr)
4532 and then Is_Partially_Initialized_Type (Ctyp)
4533 then
4534 Resolve_Aggr_Expr (Expr, Component);
4535 end if;
4536 end Check_Box_Component;
4538 elsif No (Expr) then
4540 -- Ignore hidden components associated with the position of the
4541 -- interface tags: these are initialized dynamically.
4543 if not Present (Related_Type (Component)) then
4544 Error_Msg_NE
4545 ("no value supplied for component &!", N, Component);
4546 end if;
4548 else
4549 Resolve_Aggr_Expr (Expr, Component);
4550 end if;
4552 Next_Elmt (Component_Elmt);
4553 end loop;
4555 -- STEP 7: check for invalid components + check type in choice list
4557 Step_7 : declare
4558 Selectr : Node_Id;
4559 -- Selector name
4561 Typech : Entity_Id;
4562 -- Type of first component in choice list
4564 begin
4565 if Present (Component_Associations (N)) then
4566 Assoc := First (Component_Associations (N));
4567 else
4568 Assoc := Empty;
4569 end if;
4571 Verification : while Present (Assoc) loop
4572 Selectr := First (Choices (Assoc));
4573 Typech := Empty;
4575 if Nkind (Selectr) = N_Others_Choice then
4577 -- Ada 2005 (AI-287): others choice may have expression or box
4579 if No (Others_Etype) and then not Others_Box then
4580 Error_Msg_N
4581 ("OTHERS must represent at least one component", Selectr);
4582 end if;
4584 exit Verification;
4585 end if;
4587 while Present (Selectr) loop
4588 New_Assoc := First (New_Assoc_List);
4589 while Present (New_Assoc) loop
4590 Component := First (Choices (New_Assoc));
4592 if Chars (Selectr) = Chars (Component) then
4593 if Style_Check then
4594 Check_Identifier (Selectr, Entity (Component));
4595 end if;
4597 exit;
4598 end if;
4600 Next (New_Assoc);
4601 end loop;
4603 -- If no association, this is not a legal component of the type
4604 -- in question, unless its association is provided with a box.
4606 if No (New_Assoc) then
4607 if Box_Present (Parent (Selectr)) then
4609 -- This may still be a bogus component with a box. Scan
4610 -- list of components to verify that a component with
4611 -- that name exists.
4613 declare
4614 C : Entity_Id;
4616 begin
4617 C := First_Component (Typ);
4618 while Present (C) loop
4619 if Chars (C) = Chars (Selectr) then
4621 -- If the context is an extension aggregate,
4622 -- the component must not be inherited from
4623 -- the ancestor part of the aggregate.
4625 if Nkind (N) /= N_Extension_Aggregate
4626 or else
4627 Scope (Original_Record_Component (C)) /=
4628 Etype (Ancestor_Part (N))
4629 then
4630 exit;
4631 end if;
4632 end if;
4634 Next_Component (C);
4635 end loop;
4637 if No (C) then
4638 Error_Msg_Node_2 := Typ;
4639 Error_Msg_N ("& is not a component of}", Selectr);
4640 end if;
4641 end;
4643 elsif Chars (Selectr) /= Name_uTag
4644 and then Chars (Selectr) /= Name_uParent
4645 then
4646 if not Has_Discriminants (Typ) then
4647 Error_Msg_Node_2 := Typ;
4648 Error_Msg_N ("& is not a component of}", Selectr);
4649 else
4650 Error_Msg_N
4651 ("& is not a component of the aggregate subtype",
4652 Selectr);
4653 end if;
4655 Check_Misspelled_Component (Components, Selectr);
4656 end if;
4658 elsif No (Typech) then
4659 Typech := Base_Type (Etype (Component));
4661 -- AI05-0199: In Ada 2012, several components of anonymous
4662 -- access types can appear in a choice list, as long as the
4663 -- designated types match.
4665 elsif Typech /= Base_Type (Etype (Component)) then
4666 if Ada_Version >= Ada_2012
4667 and then Ekind (Typech) = E_Anonymous_Access_Type
4668 and then
4669 Ekind (Etype (Component)) = E_Anonymous_Access_Type
4670 and then Base_Type (Designated_Type (Typech)) =
4671 Base_Type (Designated_Type (Etype (Component)))
4672 and then
4673 Subtypes_Statically_Match (Typech, (Etype (Component)))
4674 then
4675 null;
4677 elsif not Box_Present (Parent (Selectr)) then
4678 Error_Msg_N
4679 ("components in choice list must have same type",
4680 Selectr);
4681 end if;
4682 end if;
4684 Next (Selectr);
4685 end loop;
4687 Next (Assoc);
4688 end loop Verification;
4689 end Step_7;
4691 -- STEP 8: replace the original aggregate
4693 Step_8 : declare
4694 New_Aggregate : constant Node_Id := New_Copy (N);
4696 begin
4697 Set_Expressions (New_Aggregate, No_List);
4698 Set_Etype (New_Aggregate, Etype (N));
4699 Set_Component_Associations (New_Aggregate, New_Assoc_List);
4700 Set_Check_Actuals (New_Aggregate, Check_Actuals (N));
4702 Rewrite (N, New_Aggregate);
4703 end Step_8;
4705 -- Check the dimensions of the components in the record aggregate
4707 Analyze_Dimension_Extension_Or_Record_Aggregate (N);
4708 end Resolve_Record_Aggregate;
4710 -----------------------------
4711 -- Check_Can_Never_Be_Null --
4712 -----------------------------
4714 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
4715 Comp_Typ : Entity_Id;
4717 begin
4718 pragma Assert
4719 (Ada_Version >= Ada_2005
4720 and then Present (Expr)
4721 and then Known_Null (Expr));
4723 case Ekind (Typ) is
4724 when E_Array_Type =>
4725 Comp_Typ := Component_Type (Typ);
4727 when E_Component |
4728 E_Discriminant =>
4729 Comp_Typ := Etype (Typ);
4731 when others =>
4732 return;
4733 end case;
4735 if Can_Never_Be_Null (Comp_Typ) then
4737 -- Here we know we have a constraint error. Note that we do not use
4738 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
4739 -- seem the more natural approach. That's because in some cases the
4740 -- components are rewritten, and the replacement would be missed.
4741 -- We do not mark the whole aggregate as raising a constraint error,
4742 -- because the association may be a null array range.
4744 Error_Msg_N
4745 ("(Ada 2005) null not allowed in null-excluding component??", Expr);
4746 Error_Msg_N
4747 ("\Constraint_Error will be raised at run time??", Expr);
4749 Rewrite (Expr,
4750 Make_Raise_Constraint_Error
4751 (Sloc (Expr), Reason => CE_Access_Check_Failed));
4752 Set_Etype (Expr, Comp_Typ);
4753 Set_Analyzed (Expr);
4754 end if;
4755 end Check_Can_Never_Be_Null;
4757 ---------------------
4758 -- Sort_Case_Table --
4759 ---------------------
4761 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
4762 U : constant Int := Case_Table'Last;
4763 K : Int;
4764 J : Int;
4765 T : Case_Bounds;
4767 begin
4768 K := 1;
4769 while K < U loop
4770 T := Case_Table (K + 1);
4772 J := K + 1;
4773 while J > 1
4774 and then Expr_Value (Case_Table (J - 1).Lo) > Expr_Value (T.Lo)
4775 loop
4776 Case_Table (J) := Case_Table (J - 1);
4777 J := J - 1;
4778 end loop;
4780 Case_Table (J) := T;
4781 K := K + 1;
4782 end loop;
4783 end Sort_Case_Table;
4785 end Sem_Aggr;