PR target/58115
[official-gcc.git] / gcc / ada / sem_aggr.adb
blob5aec38a32d05f7fb65a8bdccaa5efcf48e6a37c6
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-2013, 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 Atree; use Atree;
27 with Checks; use Checks;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Expander; use Expander;
32 with Exp_Tss; use Exp_Tss;
33 with Exp_Util; use Exp_Util;
34 with Freeze; use Freeze;
35 with Itypes; use Itypes;
36 with Lib; use Lib;
37 with Lib.Xref; use Lib.Xref;
38 with Namet; use Namet;
39 with Namet.Sp; use Namet.Sp;
40 with Nmake; use Nmake;
41 with Nlists; use Nlists;
42 with Opt; use Opt;
43 with Restrict; use Restrict;
44 with Sem; use Sem;
45 with Sem_Aux; use Sem_Aux;
46 with Sem_Cat; use Sem_Cat;
47 with Sem_Ch3; use Sem_Ch3;
48 with Sem_Ch8; use Sem_Ch8;
49 with Sem_Ch13; use Sem_Ch13;
50 with Sem_Dim; use Sem_Dim;
51 with Sem_Eval; use Sem_Eval;
52 with Sem_Res; use Sem_Res;
53 with Sem_Util; use Sem_Util;
54 with Sem_Type; use Sem_Type;
55 with Sem_Warn; use Sem_Warn;
56 with Sinfo; use Sinfo;
57 with Snames; use Snames;
58 with Stringt; use Stringt;
59 with Stand; use Stand;
60 with Style; use Style;
61 with Targparm; use Targparm;
62 with Tbuild; use Tbuild;
63 with Uintp; use Uintp;
65 package body Sem_Aggr is
67 type Case_Bounds is record
68 Lo : Node_Id;
69 -- Low bound of choice. Once we sort the Case_Table, then entries
70 -- will be in order of ascending Choice_Lo values.
72 Hi : Node_Id;
73 -- High Bound of choice. The sort does not pay any attention to the
74 -- high bound, so choices 1 .. 4 and 1 .. 5 could be in either order.
76 Highest : Uint;
77 -- If there are duplicates or missing entries, then in the sorted
78 -- table, this records the highest value among Choice_Hi values
79 -- seen so far, including this entry.
81 Choice : Node_Id;
82 -- The node of the choice
83 end record;
85 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
86 -- Table type used by Check_Case_Choices procedure. Entry zero is not
87 -- used (reserved for the sort). Real entries start at one.
89 -----------------------
90 -- Local Subprograms --
91 -----------------------
93 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
94 -- Sort the Case Table using the Lower Bound of each Choice as the key. A
95 -- simple insertion sort is used since the choices in a case statement will
96 -- usually be in near sorted order.
98 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id);
99 -- Ada 2005 (AI-231): Check bad usage of null for a component for which
100 -- null exclusion (NOT NULL) is specified. Typ can be an E_Array_Type for
101 -- the array case (the component type of the array will be used) or an
102 -- E_Component/E_Discriminant entity in the record case, in which case the
103 -- type of the component will be used for the test. If Typ is any other
104 -- kind of entity, the call is ignored. Expr is the component node in the
105 -- aggregate which is known to have a null value. A warning message will be
106 -- issued if the component is null excluding.
108 -- It would be better to pass the proper type for Typ ???
110 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id);
111 -- Check that Expr is either not limited or else is one of the cases of
112 -- expressions allowed for a limited component association (namely, an
113 -- aggregate, function call, or <> notation). Report error for violations.
115 procedure Check_Qualified_Aggregate (Level : Nat; Expr : Node_Id);
116 -- Given aggregate Expr, check that sub-aggregates of Expr that are nested
117 -- at Level are qualified. If Level = 0, this applies to Expr directly.
118 -- Only issue errors in formal verification mode.
120 function Is_Top_Level_Aggregate (Expr : Node_Id) return Boolean;
121 -- Return True of Expr is an aggregate not contained directly in another
122 -- aggregate.
124 ------------------------------------------------------
125 -- Subprograms used for RECORD AGGREGATE Processing --
126 ------------------------------------------------------
128 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
129 -- This procedure performs all the semantic checks required for record
130 -- aggregates. Note that for aggregates analysis and resolution go
131 -- hand in hand. Aggregate analysis has been delayed up to here and
132 -- it is done while resolving the aggregate.
134 -- N is the N_Aggregate node.
135 -- Typ is the record type for the aggregate resolution
137 -- While performing the semantic checks, this procedure builds a new
138 -- Component_Association_List where each record field appears alone in a
139 -- Component_Choice_List along with its corresponding expression. The
140 -- record fields in the Component_Association_List appear in the same order
141 -- in which they appear in the record type Typ.
143 -- Once this new Component_Association_List is built and all the semantic
144 -- checks performed, the original aggregate subtree is replaced with the
145 -- new named record aggregate just built. Note that subtree substitution is
146 -- performed with Rewrite so as to be able to retrieve the original
147 -- aggregate.
149 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
150 -- yields the aggregate format expected by Gigi. Typically, this kind of
151 -- tree manipulations are done in the expander. However, because the
152 -- semantic checks that need to be performed on record aggregates really go
153 -- hand in hand with the record aggregate normalization, the aggregate
154 -- subtree transformation is performed during resolution rather than
155 -- expansion. Had we decided otherwise we would have had to duplicate most
156 -- of the code in the expansion procedure Expand_Record_Aggregate. Note,
157 -- however, that all the expansion concerning aggregates for tagged records
158 -- is done in Expand_Record_Aggregate.
160 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
162 -- 1. Make sure that the record type against which the record aggregate
163 -- has to be resolved is not abstract. Furthermore if the type is a
164 -- null aggregate make sure the input aggregate N is also null.
166 -- 2. Verify that the structure of the aggregate is that of a record
167 -- aggregate. Specifically, look for component associations and ensure
168 -- that each choice list only has identifiers or the N_Others_Choice
169 -- node. Also make sure that if present, the N_Others_Choice occurs
170 -- last and by itself.
172 -- 3. If Typ contains discriminants, the values for each discriminant is
173 -- looked for. If the record type Typ has variants, we check that the
174 -- expressions corresponding to each discriminant ruling the (possibly
175 -- nested) variant parts of Typ, are static. This allows us to determine
176 -- the variant parts to which the rest of the aggregate must conform.
177 -- The names of discriminants with their values are saved in a new
178 -- association list, New_Assoc_List which is later augmented with the
179 -- names and values of the remaining components in the record type.
181 -- During this phase we also make sure that every discriminant is
182 -- assigned exactly one value. Note that when several values for a given
183 -- discriminant are found, semantic processing continues looking for
184 -- further errors. In this case it's the first discriminant value found
185 -- which we will be recorded.
187 -- IMPORTANT NOTE: For derived tagged types this procedure expects
188 -- First_Discriminant and Next_Discriminant to give the correct list
189 -- of discriminants, in the correct order.
191 -- 4. After all the discriminant values have been gathered, we can set the
192 -- Etype of the record aggregate. If Typ contains no discriminants this
193 -- is straightforward: the Etype of N is just Typ, otherwise a new
194 -- implicit constrained subtype of Typ is built to be the Etype of N.
196 -- 5. Gather the remaining record components according to the discriminant
197 -- values. This involves recursively traversing the record type
198 -- structure to see what variants are selected by the given discriminant
199 -- values. This processing is a little more convoluted if Typ is a
200 -- derived tagged types since we need to retrieve the record structure
201 -- of all the ancestors of Typ.
203 -- 6. After gathering the record components we look for their values in the
204 -- record aggregate and emit appropriate error messages should we not
205 -- find such values or should they be duplicated.
207 -- 7. We then make sure no illegal component names appear in the record
208 -- aggregate and make sure that the type of the record components
209 -- appearing in a same choice list is the same. Finally we ensure that
210 -- the others choice, if present, is used to provide the value of at
211 -- least a record component.
213 -- 8. The original aggregate node is replaced with the new named aggregate
214 -- built in steps 3 through 6, as explained earlier.
216 -- Given the complexity of record aggregate resolution, the primary goal of
217 -- this routine is clarity and simplicity rather than execution and storage
218 -- efficiency. If there are only positional components in the aggregate the
219 -- running time is linear. If there are associations the running time is
220 -- still linear as long as the order of the associations is not too far off
221 -- the order of the components in the record type. If this is not the case
222 -- the running time is at worst quadratic in the size of the association
223 -- list.
225 procedure Check_Misspelled_Component
226 (Elements : Elist_Id;
227 Component : Node_Id);
228 -- Give possible misspelling diagnostic if Component is likely to be a
229 -- misspelling of one of the components of the Assoc_List. This is called
230 -- by Resolve_Aggr_Expr after producing an invalid component error message.
232 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id);
233 -- An optimization: determine whether a discriminated subtype has a static
234 -- constraint, and contains array components whose length is also static,
235 -- either because they are constrained by the discriminant, or because the
236 -- original component bounds are static.
238 -----------------------------------------------------
239 -- Subprograms used for ARRAY AGGREGATE Processing --
240 -----------------------------------------------------
242 function Resolve_Array_Aggregate
243 (N : Node_Id;
244 Index : Node_Id;
245 Index_Constr : Node_Id;
246 Component_Typ : Entity_Id;
247 Others_Allowed : Boolean) return Boolean;
248 -- This procedure performs the semantic checks for an array aggregate.
249 -- True is returned if the aggregate resolution succeeds.
251 -- The procedure works by recursively checking each nested aggregate.
252 -- Specifically, after checking a sub-aggregate nested at the i-th level
253 -- we recursively check all the subaggregates at the i+1-st level (if any).
254 -- Note that for aggregates analysis and resolution go hand in hand.
255 -- Aggregate analysis has been delayed up to here and it is done while
256 -- resolving the aggregate.
258 -- N is the current N_Aggregate node to be checked.
260 -- Index is the index node corresponding to the array sub-aggregate that
261 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
262 -- corresponding index type (or subtype).
264 -- Index_Constr is the node giving the applicable index constraint if
265 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
266 -- contexts [...] that can be used to determine the bounds of the array
267 -- value specified by the aggregate". If Others_Allowed below is False
268 -- there is no applicable index constraint and this node is set to Index.
270 -- Component_Typ is the array component type.
272 -- Others_Allowed indicates whether an others choice is allowed
273 -- in the context where the top-level aggregate appeared.
275 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
277 -- 1. Make sure that the others choice, if present, is by itself and
278 -- appears last in the sub-aggregate. Check that we do not have
279 -- positional and named components in the array sub-aggregate (unless
280 -- the named association is an others choice). Finally if an others
281 -- choice is present, make sure it is allowed in the aggregate context.
283 -- 2. If the array sub-aggregate contains discrete_choices:
285 -- (A) Verify their validity. Specifically verify that:
287 -- (a) If a null range is present it must be the only possible
288 -- choice in the array aggregate.
290 -- (b) Ditto for a non static range.
292 -- (c) Ditto for a non static expression.
294 -- In addition this step analyzes and resolves each discrete_choice,
295 -- making sure that its type is the type of the corresponding Index.
296 -- If we are not at the lowest array aggregate level (in the case of
297 -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
298 -- recursively on each component expression. Otherwise, resolve the
299 -- bottom level component expressions against the expected component
300 -- type ONLY IF the component corresponds to a single discrete choice
301 -- which is not an others choice (to see why read the DELAYED
302 -- COMPONENT RESOLUTION below).
304 -- (B) Determine the bounds of the sub-aggregate and lowest and
305 -- highest choice values.
307 -- 3. For positional aggregates:
309 -- (A) Loop over the component expressions either recursively invoking
310 -- Resolve_Array_Aggregate on each of these for multi-dimensional
311 -- array aggregates or resolving the bottom level component
312 -- expressions against the expected component type.
314 -- (B) Determine the bounds of the positional sub-aggregates.
316 -- 4. Try to determine statically whether the evaluation of the array
317 -- sub-aggregate raises Constraint_Error. If yes emit proper
318 -- warnings. The precise checks are the following:
320 -- (A) Check that the index range defined by aggregate bounds is
321 -- compatible with corresponding index subtype.
322 -- We also check against the base type. In fact it could be that
323 -- Low/High bounds of the base type are static whereas those of
324 -- the index subtype are not. Thus if we can statically catch
325 -- a problem with respect to the base type we are guaranteed
326 -- that the same problem will arise with the index subtype
328 -- (B) If we are dealing with a named aggregate containing an others
329 -- choice and at least one discrete choice then make sure the range
330 -- specified by the discrete choices does not overflow the
331 -- aggregate bounds. We also check against the index type and base
332 -- type bounds for the same reasons given in (A).
334 -- (C) If we are dealing with a positional aggregate with an others
335 -- choice make sure the number of positional elements specified
336 -- does not overflow the aggregate bounds. We also check against
337 -- the index type and base type bounds as mentioned in (A).
339 -- Finally construct an N_Range node giving the sub-aggregate bounds.
340 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
341 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
342 -- to build the appropriate aggregate subtype. Aggregate_Bounds
343 -- information is needed during expansion.
345 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
346 -- expressions in an array aggregate may call Duplicate_Subexpr or some
347 -- other routine that inserts code just outside the outermost aggregate.
348 -- If the array aggregate contains discrete choices or an others choice,
349 -- this may be wrong. Consider for instance the following example.
351 -- type Rec is record
352 -- V : Integer := 0;
353 -- end record;
355 -- type Acc_Rec is access Rec;
356 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
358 -- Then the transformation of "new Rec" that occurs during resolution
359 -- entails the following code modifications
361 -- P7b : constant Acc_Rec := new Rec;
362 -- RecIP (P7b.all);
363 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
365 -- This code transformation is clearly wrong, since we need to call
366 -- "new Rec" for each of the 3 array elements. To avoid this problem we
367 -- delay resolution of the components of non positional array aggregates
368 -- to the expansion phase. As an optimization, if the discrete choice
369 -- specifies a single value we do not delay resolution.
371 function Array_Aggr_Subtype (N : Node_Id; Typ : Node_Id) return Entity_Id;
372 -- This routine returns the type or subtype of an array aggregate.
374 -- N is the array aggregate node whose type we return.
376 -- Typ is the context type in which N occurs.
378 -- This routine creates an implicit array subtype whose bounds are
379 -- those defined by the aggregate. When this routine is invoked
380 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
381 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
382 -- sub-aggregate bounds. When building the aggregate itype, this function
383 -- traverses the array aggregate N collecting such Aggregate_Bounds and
384 -- constructs the proper array aggregate itype.
386 -- Note that in the case of multidimensional aggregates each inner
387 -- sub-aggregate corresponding to a given array dimension, may provide a
388 -- different bounds. If it is possible to determine statically that
389 -- some sub-aggregates corresponding to the same index do not have the
390 -- same bounds, then a warning is emitted. If such check is not possible
391 -- statically (because some sub-aggregate bounds are dynamic expressions)
392 -- then this job is left to the expander. In all cases the particular
393 -- bounds that this function will chose for a given dimension is the first
394 -- N_Range node for a sub-aggregate corresponding to that dimension.
396 -- Note that the Raises_Constraint_Error flag of an array aggregate
397 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
398 -- is set in Resolve_Array_Aggregate but the aggregate is not
399 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
400 -- first construct the proper itype for the aggregate (Gigi needs
401 -- this). After constructing the proper itype we will eventually replace
402 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
403 -- Of course in cases such as:
405 -- type Arr is array (integer range <>) of Integer;
406 -- A : Arr := (positive range -1 .. 2 => 0);
408 -- The bounds of the aggregate itype are cooked up to look reasonable
409 -- (in this particular case the bounds will be 1 .. 2).
411 procedure Aggregate_Constraint_Checks
412 (Exp : Node_Id;
413 Check_Typ : Entity_Id);
414 -- Checks expression Exp against subtype Check_Typ. If Exp is an
415 -- aggregate and Check_Typ a constrained record type with discriminants,
416 -- we generate the appropriate discriminant checks. If Exp is an array
417 -- aggregate then emit the appropriate length checks. If Exp is a scalar
418 -- type, or a string literal, Exp is changed into Check_Typ'(Exp) to
419 -- ensure that range checks are performed at run time.
421 procedure Make_String_Into_Aggregate (N : Node_Id);
422 -- A string literal can appear in a context in which a one dimensional
423 -- array of characters is expected. This procedure simply rewrites the
424 -- string as an aggregate, prior to resolution.
426 ---------------------------------
427 -- Aggregate_Constraint_Checks --
428 ---------------------------------
430 procedure Aggregate_Constraint_Checks
431 (Exp : Node_Id;
432 Check_Typ : Entity_Id)
434 Exp_Typ : constant Entity_Id := Etype (Exp);
436 begin
437 if Raises_Constraint_Error (Exp) then
438 return;
439 end if;
441 -- Ada 2005 (AI-230): Generate a conversion to an anonymous access
442 -- component's type to force the appropriate accessibility checks.
444 -- Ada 2005 (AI-231): Generate conversion to the null-excluding
445 -- type to force the corresponding run-time check
447 if Is_Access_Type (Check_Typ)
448 and then ((Is_Local_Anonymous_Access (Check_Typ))
449 or else (Can_Never_Be_Null (Check_Typ)
450 and then not Can_Never_Be_Null (Exp_Typ)))
451 then
452 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
453 Analyze_And_Resolve (Exp, Check_Typ);
454 Check_Unset_Reference (Exp);
455 end if;
457 -- This is really expansion activity, so make sure that expansion
458 -- is on and is allowed.
460 if not Expander_Active or else In_Spec_Expression then
461 return;
462 end if;
464 -- First check if we have to insert discriminant checks
466 if Has_Discriminants (Exp_Typ) then
467 Apply_Discriminant_Check (Exp, Check_Typ);
469 -- Next emit length checks for array aggregates
471 elsif Is_Array_Type (Exp_Typ) then
472 Apply_Length_Check (Exp, Check_Typ);
474 -- Finally emit scalar and string checks. If we are dealing with a
475 -- scalar literal we need to check by hand because the Etype of
476 -- literals is not necessarily correct.
478 elsif Is_Scalar_Type (Exp_Typ)
479 and then Compile_Time_Known_Value (Exp)
480 then
481 if Is_Out_Of_Range (Exp, Base_Type (Check_Typ)) then
482 Apply_Compile_Time_Constraint_Error
483 (Exp, "value not in range of}??", CE_Range_Check_Failed,
484 Ent => Base_Type (Check_Typ),
485 Typ => Base_Type (Check_Typ));
487 elsif Is_Out_Of_Range (Exp, Check_Typ) then
488 Apply_Compile_Time_Constraint_Error
489 (Exp, "value not in range of}??", CE_Range_Check_Failed,
490 Ent => Check_Typ,
491 Typ => Check_Typ);
493 elsif not Range_Checks_Suppressed (Check_Typ) then
494 Apply_Scalar_Range_Check (Exp, Check_Typ);
495 end if;
497 -- Verify that target type is also scalar, to prevent view anomalies
498 -- in instantiations.
500 elsif (Is_Scalar_Type (Exp_Typ)
501 or else Nkind (Exp) = N_String_Literal)
502 and then Is_Scalar_Type (Check_Typ)
503 and then Exp_Typ /= Check_Typ
504 then
505 if Is_Entity_Name (Exp)
506 and then Ekind (Entity (Exp)) = E_Constant
507 then
508 -- If expression is a constant, it is worthwhile checking whether
509 -- it is a bound of the type.
511 if (Is_Entity_Name (Type_Low_Bound (Check_Typ))
512 and then Entity (Exp) = Entity (Type_Low_Bound (Check_Typ)))
513 or else (Is_Entity_Name (Type_High_Bound (Check_Typ))
514 and then Entity (Exp) = Entity (Type_High_Bound (Check_Typ)))
515 then
516 return;
518 else
519 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
520 Analyze_And_Resolve (Exp, Check_Typ);
521 Check_Unset_Reference (Exp);
522 end if;
523 else
524 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
525 Analyze_And_Resolve (Exp, Check_Typ);
526 Check_Unset_Reference (Exp);
527 end if;
529 end if;
530 end Aggregate_Constraint_Checks;
532 ------------------------
533 -- Array_Aggr_Subtype --
534 ------------------------
536 function Array_Aggr_Subtype
537 (N : Node_Id;
538 Typ : Entity_Id) return Entity_Id
540 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
541 -- Number of aggregate index dimensions
543 Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
544 -- Constrained N_Range of each index dimension in our aggregate itype
546 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
547 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
548 -- Low and High bounds for each index dimension in our aggregate itype
550 Is_Fully_Positional : Boolean := True;
552 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
553 -- N is an array (sub-)aggregate. Dim is the dimension corresponding
554 -- to (sub-)aggregate N. This procedure collects and removes the side
555 -- effects of the constrained N_Range nodes corresponding to each index
556 -- dimension of our aggregate itype. These N_Range nodes are collected
557 -- in Aggr_Range above.
559 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
560 -- bounds of each index dimension. If, when collecting, two bounds
561 -- corresponding to the same dimension are static and found to differ,
562 -- then emit a warning, and mark N as raising Constraint_Error.
564 -------------------------
565 -- Collect_Aggr_Bounds --
566 -------------------------
568 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
569 This_Range : constant Node_Id := Aggregate_Bounds (N);
570 -- The aggregate range node of this specific sub-aggregate
572 This_Low : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
573 This_High : constant Node_Id := High_Bound (Aggregate_Bounds (N));
574 -- The aggregate bounds of this specific sub-aggregate
576 Assoc : Node_Id;
577 Expr : Node_Id;
579 begin
580 Remove_Side_Effects (This_Low, Variable_Ref => True);
581 Remove_Side_Effects (This_High, Variable_Ref => True);
583 -- Collect the first N_Range for a given dimension that you find.
584 -- For a given dimension they must be all equal anyway.
586 if No (Aggr_Range (Dim)) then
587 Aggr_Low (Dim) := This_Low;
588 Aggr_High (Dim) := This_High;
589 Aggr_Range (Dim) := This_Range;
591 else
592 if Compile_Time_Known_Value (This_Low) then
593 if not Compile_Time_Known_Value (Aggr_Low (Dim)) then
594 Aggr_Low (Dim) := This_Low;
596 elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
597 Set_Raises_Constraint_Error (N);
598 Error_Msg_N ("sub-aggregate low bound mismatch??", N);
599 Error_Msg_N
600 ("\Constraint_Error will be raised at run time??", N);
601 end if;
602 end if;
604 if Compile_Time_Known_Value (This_High) then
605 if not Compile_Time_Known_Value (Aggr_High (Dim)) then
606 Aggr_High (Dim) := This_High;
608 elsif
609 Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
610 then
611 Set_Raises_Constraint_Error (N);
612 Error_Msg_N ("sub-aggregate high bound mismatch??", N);
613 Error_Msg_N
614 ("\Constraint_Error will be raised at run time??", N);
615 end if;
616 end if;
617 end if;
619 if Dim < Aggr_Dimension then
621 -- Process positional components
623 if Present (Expressions (N)) then
624 Expr := First (Expressions (N));
625 while Present (Expr) loop
626 Collect_Aggr_Bounds (Expr, Dim + 1);
627 Next (Expr);
628 end loop;
629 end if;
631 -- Process component associations
633 if Present (Component_Associations (N)) then
634 Is_Fully_Positional := False;
636 Assoc := First (Component_Associations (N));
637 while Present (Assoc) loop
638 Expr := Expression (Assoc);
639 Collect_Aggr_Bounds (Expr, Dim + 1);
640 Next (Assoc);
641 end loop;
642 end if;
643 end if;
644 end Collect_Aggr_Bounds;
646 -- Array_Aggr_Subtype variables
648 Itype : Entity_Id;
649 -- The final itype of the overall aggregate
651 Index_Constraints : constant List_Id := New_List;
652 -- The list of index constraints of the aggregate itype
654 -- Start of processing for Array_Aggr_Subtype
656 begin
657 -- Make sure that the list of index constraints is properly attached to
658 -- the tree, and then collect the aggregate bounds.
660 Set_Parent (Index_Constraints, N);
661 Collect_Aggr_Bounds (N, 1);
663 -- Build the list of constrained indexes of our aggregate itype
665 for J in 1 .. Aggr_Dimension loop
666 Create_Index : declare
667 Index_Base : constant Entity_Id :=
668 Base_Type (Etype (Aggr_Range (J)));
669 Index_Typ : Entity_Id;
671 begin
672 -- Construct the Index subtype, and associate it with the range
673 -- construct that generates it.
675 Index_Typ :=
676 Create_Itype (Subtype_Kind (Ekind (Index_Base)), Aggr_Range (J));
678 Set_Etype (Index_Typ, Index_Base);
680 if Is_Character_Type (Index_Base) then
681 Set_Is_Character_Type (Index_Typ);
682 end if;
684 Set_Size_Info (Index_Typ, (Index_Base));
685 Set_RM_Size (Index_Typ, RM_Size (Index_Base));
686 Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base));
687 Set_Scalar_Range (Index_Typ, Aggr_Range (J));
689 if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then
690 Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ)));
691 end if;
693 Set_Etype (Aggr_Range (J), Index_Typ);
695 Append (Aggr_Range (J), To => Index_Constraints);
696 end Create_Index;
697 end loop;
699 -- Now build the Itype
701 Itype := Create_Itype (E_Array_Subtype, N);
703 Set_First_Rep_Item (Itype, First_Rep_Item (Typ));
704 Set_Convention (Itype, Convention (Typ));
705 Set_Depends_On_Private (Itype, Has_Private_Component (Typ));
706 Set_Etype (Itype, Base_Type (Typ));
707 Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ));
708 Set_Is_Aliased (Itype, Is_Aliased (Typ));
709 Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
711 Copy_Suppress_Status (Index_Check, Typ, Itype);
712 Copy_Suppress_Status (Length_Check, Typ, Itype);
714 Set_First_Index (Itype, First (Index_Constraints));
715 Set_Is_Constrained (Itype, True);
716 Set_Is_Internal (Itype, True);
718 -- A simple optimization: purely positional aggregates of static
719 -- components should be passed to gigi unexpanded whenever possible, and
720 -- regardless of the staticness of the bounds themselves. Subsequent
721 -- checks in exp_aggr verify that type is not packed, etc.
723 Set_Size_Known_At_Compile_Time (Itype,
724 Is_Fully_Positional
725 and then Comes_From_Source (N)
726 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
728 -- We always need a freeze node for a packed array subtype, so that we
729 -- can build the Packed_Array_Type corresponding to the subtype. If
730 -- expansion is disabled, the packed array subtype is not built, and we
731 -- must not generate a freeze node for the type, or else it will appear
732 -- incomplete to gigi.
734 if Is_Packed (Itype)
735 and then not In_Spec_Expression
736 and then Expander_Active
737 then
738 Freeze_Itype (Itype, N);
739 end if;
741 return Itype;
742 end Array_Aggr_Subtype;
744 --------------------------------
745 -- Check_Misspelled_Component --
746 --------------------------------
748 procedure Check_Misspelled_Component
749 (Elements : Elist_Id;
750 Component : Node_Id)
752 Max_Suggestions : constant := 2;
754 Nr_Of_Suggestions : Natural := 0;
755 Suggestion_1 : Entity_Id := Empty;
756 Suggestion_2 : Entity_Id := Empty;
757 Component_Elmt : Elmt_Id;
759 begin
760 -- All the components of List are matched against Component and a count
761 -- is maintained of possible misspellings. When at the end of the the
762 -- analysis there are one or two (not more!) possible misspellings,
763 -- these misspellings will be suggested as possible correction.
765 Component_Elmt := First_Elmt (Elements);
766 while Nr_Of_Suggestions <= Max_Suggestions
767 and then Present (Component_Elmt)
768 loop
769 if Is_Bad_Spelling_Of
770 (Chars (Node (Component_Elmt)),
771 Chars (Component))
772 then
773 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
775 case Nr_Of_Suggestions is
776 when 1 => Suggestion_1 := Node (Component_Elmt);
777 when 2 => Suggestion_2 := Node (Component_Elmt);
778 when others => exit;
779 end case;
780 end if;
782 Next_Elmt (Component_Elmt);
783 end loop;
785 -- Report at most two suggestions
787 if Nr_Of_Suggestions = 1 then
788 Error_Msg_NE -- CODEFIX
789 ("\possible misspelling of&", Component, Suggestion_1);
791 elsif Nr_Of_Suggestions = 2 then
792 Error_Msg_Node_2 := Suggestion_2;
793 Error_Msg_NE -- CODEFIX
794 ("\possible misspelling of& or&", Component, Suggestion_1);
795 end if;
796 end Check_Misspelled_Component;
798 ----------------------------------------
799 -- Check_Expr_OK_In_Limited_Aggregate --
800 ----------------------------------------
802 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id) is
803 begin
804 if Is_Limited_Type (Etype (Expr))
805 and then Comes_From_Source (Expr)
806 and then not In_Instance_Body
807 then
808 if not OK_For_Limited_Init (Etype (Expr), Expr) then
809 Error_Msg_N ("initialization not allowed for limited types", Expr);
810 Explain_Limited_Type (Etype (Expr), Expr);
811 end if;
812 end if;
813 end Check_Expr_OK_In_Limited_Aggregate;
815 -------------------------------
816 -- Check_Qualified_Aggregate --
817 -------------------------------
819 procedure Check_Qualified_Aggregate (Level : Nat; Expr : Node_Id) is
820 Comp_Expr : Node_Id;
821 Comp_Assn : Node_Id;
823 begin
824 if Level = 0 then
825 if Nkind (Parent (Expr)) /= N_Qualified_Expression then
826 Check_SPARK_Restriction ("aggregate should be qualified", Expr);
827 end if;
829 else
830 Comp_Expr := First (Expressions (Expr));
831 while Present (Comp_Expr) loop
832 if Nkind (Comp_Expr) = N_Aggregate then
833 Check_Qualified_Aggregate (Level - 1, Comp_Expr);
834 end if;
836 Comp_Expr := Next (Comp_Expr);
837 end loop;
839 Comp_Assn := First (Component_Associations (Expr));
840 while Present (Comp_Assn) loop
841 Comp_Expr := Expression (Comp_Assn);
843 if Nkind (Comp_Expr) = N_Aggregate then
844 Check_Qualified_Aggregate (Level - 1, Comp_Expr);
845 end if;
847 Comp_Assn := Next (Comp_Assn);
848 end loop;
849 end if;
850 end Check_Qualified_Aggregate;
852 ----------------------------------------
853 -- Check_Static_Discriminated_Subtype --
854 ----------------------------------------
856 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id) is
857 Disc : constant Entity_Id := First_Discriminant (T);
858 Comp : Entity_Id;
859 Ind : Entity_Id;
861 begin
862 if Has_Record_Rep_Clause (T) then
863 return;
865 elsif Present (Next_Discriminant (Disc)) then
866 return;
868 elsif Nkind (V) /= N_Integer_Literal then
869 return;
870 end if;
872 Comp := First_Component (T);
873 while Present (Comp) loop
874 if Is_Scalar_Type (Etype (Comp)) then
875 null;
877 elsif Is_Private_Type (Etype (Comp))
878 and then Present (Full_View (Etype (Comp)))
879 and then Is_Scalar_Type (Full_View (Etype (Comp)))
880 then
881 null;
883 elsif Is_Array_Type (Etype (Comp)) then
884 if Is_Bit_Packed_Array (Etype (Comp)) then
885 return;
886 end if;
888 Ind := First_Index (Etype (Comp));
889 while Present (Ind) loop
890 if Nkind (Ind) /= N_Range
891 or else Nkind (Low_Bound (Ind)) /= N_Integer_Literal
892 or else Nkind (High_Bound (Ind)) /= N_Integer_Literal
893 then
894 return;
895 end if;
897 Next_Index (Ind);
898 end loop;
900 else
901 return;
902 end if;
904 Next_Component (Comp);
905 end loop;
907 -- On exit, all components have statically known sizes
909 Set_Size_Known_At_Compile_Time (T);
910 end Check_Static_Discriminated_Subtype;
912 -------------------------
913 -- Is_Others_Aggregate --
914 -------------------------
916 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean is
917 begin
918 return No (Expressions (Aggr))
919 and then
920 Nkind (First (Choices (First (Component_Associations (Aggr)))))
921 = N_Others_Choice;
922 end Is_Others_Aggregate;
924 ----------------------------
925 -- Is_Top_Level_Aggregate --
926 ----------------------------
928 function Is_Top_Level_Aggregate (Expr : Node_Id) return Boolean is
929 begin
930 return Nkind (Parent (Expr)) /= N_Aggregate
931 and then (Nkind (Parent (Expr)) /= N_Component_Association
932 or else Nkind (Parent (Parent (Expr))) /= N_Aggregate);
933 end Is_Top_Level_Aggregate;
935 --------------------------------
936 -- Make_String_Into_Aggregate --
937 --------------------------------
939 procedure Make_String_Into_Aggregate (N : Node_Id) is
940 Exprs : constant List_Id := New_List;
941 Loc : constant Source_Ptr := Sloc (N);
942 Str : constant String_Id := Strval (N);
943 Strlen : constant Nat := String_Length (Str);
944 C : Char_Code;
945 C_Node : Node_Id;
946 New_N : Node_Id;
947 P : Source_Ptr;
949 begin
950 P := Loc + 1;
951 for J in 1 .. Strlen loop
952 C := Get_String_Char (Str, J);
953 Set_Character_Literal_Name (C);
955 C_Node :=
956 Make_Character_Literal (P,
957 Chars => Name_Find,
958 Char_Literal_Value => UI_From_CC (C));
959 Set_Etype (C_Node, Any_Character);
960 Append_To (Exprs, C_Node);
962 P := P + 1;
963 -- Something special for wide strings???
964 end loop;
966 New_N := Make_Aggregate (Loc, Expressions => Exprs);
967 Set_Analyzed (New_N);
968 Set_Etype (New_N, Any_Composite);
970 Rewrite (N, New_N);
971 end Make_String_Into_Aggregate;
973 -----------------------
974 -- Resolve_Aggregate --
975 -----------------------
977 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
978 Loc : constant Source_Ptr := Sloc (N);
979 Pkind : constant Node_Kind := Nkind (Parent (N));
981 Aggr_Subtyp : Entity_Id;
982 -- The actual aggregate subtype. This is not necessarily the same as Typ
983 -- which is the subtype of the context in which the aggregate was found.
985 begin
986 -- Ignore junk empty aggregate resulting from parser error
988 if No (Expressions (N))
989 and then No (Component_Associations (N))
990 and then not Null_Record_Present (N)
991 then
992 return;
993 end if;
995 -- If the aggregate has box-initialized components, its type must be
996 -- frozen so that initialization procedures can properly be called
997 -- in the resolution that follows. The replacement of boxes with
998 -- initialization calls is properly an expansion activity but it must
999 -- be done during revolution.
1001 if Expander_Active
1002 and then Present (Component_Associations (N))
1003 then
1004 declare
1005 Comp : Node_Id;
1007 begin
1008 Comp := First (Component_Associations (N));
1009 while Present (Comp) loop
1010 if Box_Present (Comp) then
1011 Insert_Actions (N, Freeze_Entity (Typ, N));
1012 exit;
1013 end if;
1015 Next (Comp);
1016 end loop;
1017 end;
1018 end if;
1020 -- An unqualified aggregate is restricted in SPARK to:
1022 -- An aggregate item inside an aggregate for a multi-dimensional array
1024 -- An expression being assigned to an unconstrained array, but only if
1025 -- the aggregate specifies a value for OTHERS only.
1027 if Nkind (Parent (N)) = N_Qualified_Expression then
1028 if Is_Array_Type (Typ) then
1029 Check_Qualified_Aggregate (Number_Dimensions (Typ), N);
1030 else
1031 Check_Qualified_Aggregate (1, N);
1032 end if;
1033 else
1034 if Is_Array_Type (Typ)
1035 and then Nkind (Parent (N)) = N_Assignment_Statement
1036 and then not Is_Constrained (Etype (Name (Parent (N))))
1037 then
1038 if not Is_Others_Aggregate (N) then
1039 Check_SPARK_Restriction
1040 ("array aggregate should have only OTHERS", N);
1041 end if;
1043 elsif Is_Top_Level_Aggregate (N) then
1044 Check_SPARK_Restriction ("aggregate should be qualified", N);
1046 -- The legality of this unqualified aggregate is checked by calling
1047 -- Check_Qualified_Aggregate from one of its enclosing aggregate,
1048 -- unless one of these already causes an error to be issued.
1050 else
1051 null;
1052 end if;
1053 end if;
1055 -- Check for aggregates not allowed in configurable run-time mode.
1056 -- We allow all cases of aggregates that do not come from source, since
1057 -- these are all assumed to be small (e.g. bounds of a string literal).
1058 -- We also allow aggregates of types we know to be small.
1060 if not Support_Aggregates_On_Target
1061 and then Comes_From_Source (N)
1062 and then (not Known_Static_Esize (Typ) or else Esize (Typ) > 64)
1063 then
1064 Error_Msg_CRT ("aggregate", N);
1065 end if;
1067 -- Ada 2005 (AI-287): Limited aggregates allowed
1069 -- In an instance, ignore aggregate subcomponents tnat may be limited,
1070 -- because they originate in view conflicts. If the original aggregate
1071 -- is legal and the actuals are legal, the aggregate itself is legal.
1073 if Is_Limited_Type (Typ)
1074 and then Ada_Version < Ada_2005
1075 and then not In_Instance
1076 then
1077 Error_Msg_N ("aggregate type cannot be limited", N);
1078 Explain_Limited_Type (Typ, N);
1080 elsif Is_Class_Wide_Type (Typ) then
1081 Error_Msg_N ("type of aggregate cannot be class-wide", N);
1083 elsif Typ = Any_String
1084 or else Typ = Any_Composite
1085 then
1086 Error_Msg_N ("no unique type for aggregate", N);
1087 Set_Etype (N, Any_Composite);
1089 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
1090 Error_Msg_N ("null record forbidden in array aggregate", N);
1092 elsif Is_Record_Type (Typ) then
1093 Resolve_Record_Aggregate (N, Typ);
1095 elsif Is_Array_Type (Typ) then
1097 -- First a special test, for the case of a positional aggregate
1098 -- of characters which can be replaced by a string literal.
1100 -- Do not perform this transformation if this was a string literal to
1101 -- start with, whose components needed constraint checks, or if the
1102 -- component type is non-static, because it will require those checks
1103 -- and be transformed back into an aggregate.
1105 if Number_Dimensions (Typ) = 1
1106 and then Is_Standard_Character_Type (Component_Type (Typ))
1107 and then No (Component_Associations (N))
1108 and then not Is_Limited_Composite (Typ)
1109 and then not Is_Private_Composite (Typ)
1110 and then not Is_Bit_Packed_Array (Typ)
1111 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
1112 and then Is_Static_Subtype (Component_Type (Typ))
1113 then
1114 declare
1115 Expr : Node_Id;
1117 begin
1118 Expr := First (Expressions (N));
1119 while Present (Expr) loop
1120 exit when Nkind (Expr) /= N_Character_Literal;
1121 Next (Expr);
1122 end loop;
1124 if No (Expr) then
1125 Start_String;
1127 Expr := First (Expressions (N));
1128 while Present (Expr) loop
1129 Store_String_Char (UI_To_CC (Char_Literal_Value (Expr)));
1130 Next (Expr);
1131 end loop;
1133 Rewrite (N, Make_String_Literal (Loc, End_String));
1135 Analyze_And_Resolve (N, Typ);
1136 return;
1137 end if;
1138 end;
1139 end if;
1141 -- Here if we have a real aggregate to deal with
1143 Array_Aggregate : declare
1144 Aggr_Resolved : Boolean;
1146 Aggr_Typ : constant Entity_Id := Etype (Typ);
1147 -- This is the unconstrained array type, which is the type against
1148 -- which the aggregate is to be resolved. Typ itself is the array
1149 -- type of the context which may not be the same subtype as the
1150 -- subtype for the final aggregate.
1152 begin
1153 -- In the following we determine whether an OTHERS choice is
1154 -- allowed inside the array aggregate. The test checks the context
1155 -- in which the array aggregate occurs. If the context does not
1156 -- permit it, or the aggregate type is unconstrained, an OTHERS
1157 -- choice is not allowed (except that it is always allowed on the
1158 -- right-hand side of an assignment statement; in this case the
1159 -- constrainedness of the type doesn't matter).
1161 -- If expansion is disabled (generic context, or semantics-only
1162 -- mode) actual subtypes cannot be constructed, and the type of an
1163 -- object may be its unconstrained nominal type. However, if the
1164 -- context is an assignment, we assume that OTHERS is allowed,
1165 -- because the target of the assignment will have a constrained
1166 -- subtype when fully compiled.
1168 -- Note that there is no node for Explicit_Actual_Parameter.
1169 -- To test for this context we therefore have to test for node
1170 -- N_Parameter_Association which itself appears only if there is a
1171 -- formal parameter. Consequently we also need to test for
1172 -- N_Procedure_Call_Statement or N_Function_Call.
1174 Set_Etype (N, Aggr_Typ); -- May be overridden later on
1176 if Pkind = N_Assignment_Statement
1177 or else (Is_Constrained (Typ)
1178 and then
1179 (Pkind = N_Parameter_Association or else
1180 Pkind = N_Function_Call or else
1181 Pkind = N_Procedure_Call_Statement or else
1182 Pkind = N_Generic_Association or else
1183 Pkind = N_Formal_Object_Declaration or else
1184 Pkind = N_Simple_Return_Statement or else
1185 Pkind = N_Object_Declaration or else
1186 Pkind = N_Component_Declaration or else
1187 Pkind = N_Parameter_Specification or else
1188 Pkind = N_Qualified_Expression or else
1189 Pkind = N_Aggregate or else
1190 Pkind = N_Extension_Aggregate or else
1191 Pkind = N_Component_Association))
1192 then
1193 Aggr_Resolved :=
1194 Resolve_Array_Aggregate
1196 Index => First_Index (Aggr_Typ),
1197 Index_Constr => First_Index (Typ),
1198 Component_Typ => Component_Type (Typ),
1199 Others_Allowed => True);
1201 elsif not Expander_Active
1202 and then Pkind = N_Assignment_Statement
1203 then
1204 Aggr_Resolved :=
1205 Resolve_Array_Aggregate
1207 Index => First_Index (Aggr_Typ),
1208 Index_Constr => First_Index (Typ),
1209 Component_Typ => Component_Type (Typ),
1210 Others_Allowed => True);
1212 else
1213 Aggr_Resolved :=
1214 Resolve_Array_Aggregate
1216 Index => First_Index (Aggr_Typ),
1217 Index_Constr => First_Index (Aggr_Typ),
1218 Component_Typ => Component_Type (Typ),
1219 Others_Allowed => False);
1220 end if;
1222 if not Aggr_Resolved then
1224 -- A parenthesized expression may have been intended as an
1225 -- aggregate, leading to a type error when analyzing the
1226 -- component. This can also happen for a nested component
1227 -- (see Analyze_Aggr_Expr).
1229 if Paren_Count (N) > 0 then
1230 Error_Msg_N
1231 ("positional aggregate cannot have one component", N);
1232 end if;
1234 Aggr_Subtyp := Any_Composite;
1236 else
1237 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1238 end if;
1240 Set_Etype (N, Aggr_Subtyp);
1241 end Array_Aggregate;
1243 elsif Is_Private_Type (Typ)
1244 and then Present (Full_View (Typ))
1245 and then (In_Inlined_Body or In_Instance_Body)
1246 and then Is_Composite_Type (Full_View (Typ))
1247 then
1248 Resolve (N, Full_View (Typ));
1250 else
1251 Error_Msg_N ("illegal context for aggregate", N);
1252 end if;
1254 -- If we can determine statically that the evaluation of the aggregate
1255 -- raises Constraint_Error, then replace the aggregate with an
1256 -- N_Raise_Constraint_Error node, but set the Etype to the right
1257 -- aggregate subtype. Gigi needs this.
1259 if Raises_Constraint_Error (N) then
1260 Aggr_Subtyp := Etype (N);
1261 Rewrite (N,
1262 Make_Raise_Constraint_Error (Loc, Reason => CE_Range_Check_Failed));
1263 Set_Raises_Constraint_Error (N);
1264 Set_Etype (N, Aggr_Subtyp);
1265 Set_Analyzed (N);
1266 end if;
1268 Check_Function_Writable_Actuals (N);
1269 end Resolve_Aggregate;
1271 -----------------------------
1272 -- Resolve_Array_Aggregate --
1273 -----------------------------
1275 function Resolve_Array_Aggregate
1276 (N : Node_Id;
1277 Index : Node_Id;
1278 Index_Constr : Node_Id;
1279 Component_Typ : Entity_Id;
1280 Others_Allowed : Boolean) return Boolean
1282 Loc : constant Source_Ptr := Sloc (N);
1284 Failure : constant Boolean := False;
1285 Success : constant Boolean := True;
1287 Index_Typ : constant Entity_Id := Etype (Index);
1288 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1289 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1290 -- The type of the index corresponding to the array sub-aggregate along
1291 -- with its low and upper bounds.
1293 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1294 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1295 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1296 -- Ditto for the base type
1298 function Add (Val : Uint; To : Node_Id) return Node_Id;
1299 -- Creates a new expression node where Val is added to expression To.
1300 -- Tries to constant fold whenever possible. To must be an already
1301 -- analyzed expression.
1303 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1304 -- Checks that AH (the upper bound of an array aggregate) is less than
1305 -- or equal to BH (the upper bound of the index base type). If the check
1306 -- fails, a warning is emitted, the Raises_Constraint_Error flag of N is
1307 -- set, and AH is replaced with a duplicate of BH.
1309 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1310 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1311 -- warning if not and sets the Raises_Constraint_Error flag in N.
1313 procedure Check_Length (L, H : Node_Id; Len : Uint);
1314 -- Checks that range L .. H contains at least Len elements. Emits a
1315 -- warning if not and sets the Raises_Constraint_Error flag in N.
1317 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1318 -- Returns True if range L .. H is dynamic or null
1320 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1321 -- Given expression node From, this routine sets OK to False if it
1322 -- cannot statically evaluate From. Otherwise it stores this static
1323 -- value into Value.
1325 function Resolve_Aggr_Expr
1326 (Expr : Node_Id;
1327 Single_Elmt : Boolean) return Boolean;
1328 -- Resolves aggregate expression Expr. Returns False if resolution
1329 -- fails. If Single_Elmt is set to False, the expression Expr may be
1330 -- used to initialize several array aggregate elements (this can happen
1331 -- for discrete choices such as "L .. H => Expr" or the OTHERS choice).
1332 -- In this event we do not resolve Expr unless expansion is disabled.
1333 -- To know why, see the DELAYED COMPONENT RESOLUTION note above.
1335 -- NOTE: In the case of "... => <>", we pass the in the
1336 -- N_Component_Association node as Expr, since there is no Expression in
1337 -- that case, and we need a Sloc for the error message.
1339 ---------
1340 -- Add --
1341 ---------
1343 function Add (Val : Uint; To : Node_Id) return Node_Id is
1344 Expr_Pos : Node_Id;
1345 Expr : Node_Id;
1346 To_Pos : Node_Id;
1348 begin
1349 if Raises_Constraint_Error (To) then
1350 return To;
1351 end if;
1353 -- First test if we can do constant folding
1355 if Compile_Time_Known_Value (To)
1356 or else Nkind (To) = N_Integer_Literal
1357 then
1358 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1359 Set_Is_Static_Expression (Expr_Pos);
1360 Set_Etype (Expr_Pos, Etype (To));
1361 Set_Analyzed (Expr_Pos, Analyzed (To));
1363 if not Is_Enumeration_Type (Index_Typ) then
1364 Expr := Expr_Pos;
1366 -- If we are dealing with enumeration return
1367 -- Index_Typ'Val (Expr_Pos)
1369 else
1370 Expr :=
1371 Make_Attribute_Reference
1372 (Loc,
1373 Prefix => New_Reference_To (Index_Typ, Loc),
1374 Attribute_Name => Name_Val,
1375 Expressions => New_List (Expr_Pos));
1376 end if;
1378 return Expr;
1379 end if;
1381 -- If we are here no constant folding possible
1383 if not Is_Enumeration_Type (Index_Base) then
1384 Expr :=
1385 Make_Op_Add (Loc,
1386 Left_Opnd => Duplicate_Subexpr (To),
1387 Right_Opnd => Make_Integer_Literal (Loc, Val));
1389 -- If we are dealing with enumeration return
1390 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1392 else
1393 To_Pos :=
1394 Make_Attribute_Reference
1395 (Loc,
1396 Prefix => New_Reference_To (Index_Typ, Loc),
1397 Attribute_Name => Name_Pos,
1398 Expressions => New_List (Duplicate_Subexpr (To)));
1400 Expr_Pos :=
1401 Make_Op_Add (Loc,
1402 Left_Opnd => To_Pos,
1403 Right_Opnd => Make_Integer_Literal (Loc, Val));
1405 Expr :=
1406 Make_Attribute_Reference
1407 (Loc,
1408 Prefix => New_Reference_To (Index_Typ, Loc),
1409 Attribute_Name => Name_Val,
1410 Expressions => New_List (Expr_Pos));
1412 -- If the index type has a non standard representation, the
1413 -- attributes 'Val and 'Pos expand into function calls and the
1414 -- resulting expression is considered non-safe for reevaluation
1415 -- by the backend. Relocate it into a constant temporary in order
1416 -- to make it safe for reevaluation.
1418 if Has_Non_Standard_Rep (Etype (N)) then
1419 declare
1420 Def_Id : Entity_Id;
1422 begin
1423 Def_Id := Make_Temporary (Loc, 'R', Expr);
1424 Set_Etype (Def_Id, Index_Typ);
1425 Insert_Action (N,
1426 Make_Object_Declaration (Loc,
1427 Defining_Identifier => Def_Id,
1428 Object_Definition => New_Reference_To (Index_Typ, Loc),
1429 Constant_Present => True,
1430 Expression => Relocate_Node (Expr)));
1432 Expr := New_Reference_To (Def_Id, Loc);
1433 end;
1434 end if;
1435 end if;
1437 return Expr;
1438 end Add;
1440 -----------------
1441 -- Check_Bound --
1442 -----------------
1444 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1445 Val_BH : Uint;
1446 Val_AH : Uint;
1448 OK_BH : Boolean;
1449 OK_AH : Boolean;
1451 begin
1452 Get (Value => Val_BH, From => BH, OK => OK_BH);
1453 Get (Value => Val_AH, From => AH, OK => OK_AH);
1455 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1456 Set_Raises_Constraint_Error (N);
1457 Error_Msg_N ("upper bound out of range??", AH);
1458 Error_Msg_N ("\Constraint_Error will be raised at run time??", AH);
1460 -- You need to set AH to BH or else in the case of enumerations
1461 -- indexes we will not be able to resolve the aggregate bounds.
1463 AH := Duplicate_Subexpr (BH);
1464 end if;
1465 end Check_Bound;
1467 ------------------
1468 -- Check_Bounds --
1469 ------------------
1471 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1472 Val_L : Uint;
1473 Val_H : Uint;
1474 Val_AL : Uint;
1475 Val_AH : Uint;
1477 OK_L : Boolean;
1478 OK_H : Boolean;
1480 OK_AL : Boolean;
1481 OK_AH : Boolean;
1482 pragma Warnings (Off, OK_AL);
1483 pragma Warnings (Off, OK_AH);
1485 begin
1486 if Raises_Constraint_Error (N)
1487 or else Dynamic_Or_Null_Range (AL, AH)
1488 then
1489 return;
1490 end if;
1492 Get (Value => Val_L, From => L, OK => OK_L);
1493 Get (Value => Val_H, From => H, OK => OK_H);
1495 Get (Value => Val_AL, From => AL, OK => OK_AL);
1496 Get (Value => Val_AH, From => AH, OK => OK_AH);
1498 if OK_L and then Val_L > Val_AL then
1499 Set_Raises_Constraint_Error (N);
1500 Error_Msg_N ("lower bound of aggregate out of range??", N);
1501 Error_Msg_N ("\Constraint_Error will be raised at run time??", N);
1502 end if;
1504 if OK_H and then Val_H < Val_AH then
1505 Set_Raises_Constraint_Error (N);
1506 Error_Msg_N ("upper bound of aggregate out of range??", N);
1507 Error_Msg_N ("\Constraint_Error will be raised at run time??", N);
1508 end if;
1509 end Check_Bounds;
1511 ------------------
1512 -- Check_Length --
1513 ------------------
1515 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1516 Val_L : Uint;
1517 Val_H : Uint;
1519 OK_L : Boolean;
1520 OK_H : Boolean;
1522 Range_Len : Uint;
1524 begin
1525 if Raises_Constraint_Error (N) then
1526 return;
1527 end if;
1529 Get (Value => Val_L, From => L, OK => OK_L);
1530 Get (Value => Val_H, From => H, OK => OK_H);
1532 if not OK_L or else not OK_H then
1533 return;
1534 end if;
1536 -- If null range length is zero
1538 if Val_L > Val_H then
1539 Range_Len := Uint_0;
1540 else
1541 Range_Len := Val_H - Val_L + 1;
1542 end if;
1544 if Range_Len < Len then
1545 Set_Raises_Constraint_Error (N);
1546 Error_Msg_N ("too many elements??", N);
1547 Error_Msg_N ("\Constraint_Error will be raised at run time??", N);
1548 end if;
1549 end Check_Length;
1551 ---------------------------
1552 -- Dynamic_Or_Null_Range --
1553 ---------------------------
1555 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1556 Val_L : Uint;
1557 Val_H : Uint;
1559 OK_L : Boolean;
1560 OK_H : Boolean;
1562 begin
1563 Get (Value => Val_L, From => L, OK => OK_L);
1564 Get (Value => Val_H, From => H, OK => OK_H);
1566 return not OK_L or else not OK_H
1567 or else not Is_OK_Static_Expression (L)
1568 or else not Is_OK_Static_Expression (H)
1569 or else Val_L > Val_H;
1570 end Dynamic_Or_Null_Range;
1572 ---------
1573 -- Get --
1574 ---------
1576 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1577 begin
1578 OK := True;
1580 if Compile_Time_Known_Value (From) then
1581 Value := Expr_Value (From);
1583 -- If expression From is something like Some_Type'Val (10) then
1584 -- Value = 10
1586 elsif Nkind (From) = N_Attribute_Reference
1587 and then Attribute_Name (From) = Name_Val
1588 and then Compile_Time_Known_Value (First (Expressions (From)))
1589 then
1590 Value := Expr_Value (First (Expressions (From)));
1592 else
1593 Value := Uint_0;
1594 OK := False;
1595 end if;
1596 end Get;
1598 -----------------------
1599 -- Resolve_Aggr_Expr --
1600 -----------------------
1602 function Resolve_Aggr_Expr
1603 (Expr : Node_Id;
1604 Single_Elmt : Boolean) return Boolean
1606 Nxt_Ind : constant Node_Id := Next_Index (Index);
1607 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
1608 -- Index is the current index corresponding to the expression
1610 Resolution_OK : Boolean := True;
1611 -- Set to False if resolution of the expression failed
1613 begin
1614 -- Defend against previous errors
1616 if Nkind (Expr) = N_Error
1617 or else Error_Posted (Expr)
1618 then
1619 return True;
1620 end if;
1622 -- If the array type against which we are resolving the aggregate
1623 -- has several dimensions, the expressions nested inside the
1624 -- aggregate must be further aggregates (or strings).
1626 if Present (Nxt_Ind) then
1627 if Nkind (Expr) /= N_Aggregate then
1629 -- A string literal can appear where a one-dimensional array
1630 -- of characters is expected. If the literal looks like an
1631 -- operator, it is still an operator symbol, which will be
1632 -- transformed into a string when analyzed.
1634 if Is_Character_Type (Component_Typ)
1635 and then No (Next_Index (Nxt_Ind))
1636 and then Nkind_In (Expr, N_String_Literal, N_Operator_Symbol)
1637 then
1638 -- A string literal used in a multidimensional array
1639 -- aggregate in place of the final one-dimensional
1640 -- aggregate must not be enclosed in parentheses.
1642 if Paren_Count (Expr) /= 0 then
1643 Error_Msg_N ("no parenthesis allowed here", Expr);
1644 end if;
1646 Make_String_Into_Aggregate (Expr);
1648 else
1649 Error_Msg_N ("nested array aggregate expected", Expr);
1651 -- If the expression is parenthesized, this may be
1652 -- a missing component association for a 1-aggregate.
1654 if Paren_Count (Expr) > 0 then
1655 Error_Msg_N
1656 ("\if single-component aggregate is intended,"
1657 & " write e.g. (1 ='> ...)", Expr);
1658 end if;
1660 return Failure;
1661 end if;
1662 end if;
1664 -- If it's "... => <>", nothing to resolve
1666 if Nkind (Expr) = N_Component_Association then
1667 pragma Assert (Box_Present (Expr));
1668 return Success;
1669 end if;
1671 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
1672 -- Required to check the null-exclusion attribute (if present).
1673 -- This value may be overridden later on.
1675 Set_Etype (Expr, Etype (N));
1677 Resolution_OK := Resolve_Array_Aggregate
1678 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1680 else
1682 -- If it's "... => <>", nothing to resolve
1684 if Nkind (Expr) = N_Component_Association then
1685 pragma Assert (Box_Present (Expr));
1686 return Success;
1687 end if;
1689 -- Do not resolve the expressions of discrete or others choices
1690 -- unless the expression covers a single component, or the
1691 -- expander is inactive.
1693 -- In SPARK mode, expressions that can perform side-effects will
1694 -- be recognized by the gnat2why back-end, and the whole
1695 -- subprogram will be ignored. So semantic analysis can be
1696 -- performed safely.
1698 if Single_Elmt
1699 or else not Full_Expander_Active
1700 or else In_Spec_Expression
1701 then
1702 Analyze_And_Resolve (Expr, Component_Typ);
1703 Check_Expr_OK_In_Limited_Aggregate (Expr);
1704 Check_Non_Static_Context (Expr);
1705 Aggregate_Constraint_Checks (Expr, Component_Typ);
1706 Check_Unset_Reference (Expr);
1707 end if;
1708 end if;
1710 -- If an aggregate component has a type with predicates, an explicit
1711 -- predicate check must be applied, as for an assignment statement,
1712 -- because the aggegate might not be expanded into individual
1713 -- component assignments.
1715 if Present (Predicate_Function (Component_Typ)) then
1716 Apply_Predicate_Check (Expr, Component_Typ);
1717 end if;
1719 if Raises_Constraint_Error (Expr)
1720 and then Nkind (Parent (Expr)) /= N_Component_Association
1721 then
1722 Set_Raises_Constraint_Error (N);
1723 end if;
1725 -- If the expression has been marked as requiring a range check,
1726 -- then generate it here.
1728 if Do_Range_Check (Expr) then
1729 Set_Do_Range_Check (Expr, False);
1730 Generate_Range_Check (Expr, Component_Typ, CE_Range_Check_Failed);
1731 end if;
1733 return Resolution_OK;
1734 end Resolve_Aggr_Expr;
1736 -- Variables local to Resolve_Array_Aggregate
1738 Assoc : Node_Id;
1739 Choice : Node_Id;
1740 Expr : Node_Id;
1742 Discard : Node_Id;
1743 pragma Warnings (Off, Discard);
1745 Delete_Choice : Boolean;
1746 -- Used when replacing a subtype choice with predicate by a list
1748 Aggr_Low : Node_Id := Empty;
1749 Aggr_High : Node_Id := Empty;
1750 -- The actual low and high bounds of this sub-aggregate
1752 Choices_Low : Node_Id := Empty;
1753 Choices_High : Node_Id := Empty;
1754 -- The lowest and highest discrete choices values for a named aggregate
1756 Nb_Elements : Uint := Uint_0;
1757 -- The number of elements in a positional aggregate
1759 Others_Present : Boolean := False;
1761 Nb_Choices : Nat := 0;
1762 -- Contains the overall number of named choices in this sub-aggregate
1764 Nb_Discrete_Choices : Nat := 0;
1765 -- The overall number of discrete choices (not counting others choice)
1767 Case_Table_Size : Nat;
1768 -- Contains the size of the case table needed to sort aggregate choices
1770 -- Start of processing for Resolve_Array_Aggregate
1772 begin
1773 -- Ignore junk empty aggregate resulting from parser error
1775 if No (Expressions (N))
1776 and then No (Component_Associations (N))
1777 and then not Null_Record_Present (N)
1778 then
1779 return False;
1780 end if;
1782 -- STEP 1: make sure the aggregate is correctly formatted
1784 if Present (Component_Associations (N)) then
1785 Assoc := First (Component_Associations (N));
1786 while Present (Assoc) loop
1787 Choice := First (Choices (Assoc));
1788 Delete_Choice := False;
1790 while Present (Choice) loop
1791 if Nkind (Choice) = N_Others_Choice then
1792 Others_Present := True;
1794 if Choice /= First (Choices (Assoc))
1795 or else Present (Next (Choice))
1796 then
1797 Error_Msg_N
1798 ("OTHERS must appear alone in a choice list", Choice);
1799 return Failure;
1800 end if;
1802 if Present (Next (Assoc)) then
1803 Error_Msg_N
1804 ("OTHERS must appear last in an aggregate", Choice);
1805 return Failure;
1806 end if;
1808 if Ada_Version = Ada_83
1809 and then Assoc /= First (Component_Associations (N))
1810 and then Nkind_In (Parent (N), N_Assignment_Statement,
1811 N_Object_Declaration)
1812 then
1813 Error_Msg_N
1814 ("(Ada 83) illegal context for OTHERS choice", N);
1815 end if;
1817 elsif Is_Entity_Name (Choice) then
1818 Analyze (Choice);
1820 declare
1821 E : constant Entity_Id := Entity (Choice);
1822 New_Cs : List_Id;
1823 P : Node_Id;
1824 C : Node_Id;
1826 begin
1827 if Is_Type (E) and then Has_Predicates (E) then
1828 Freeze_Before (N, E);
1830 -- If the subtype has a static predicate, replace the
1831 -- original choice with the list of individual values
1832 -- covered by the predicate.
1834 if Present (Static_Predicate (E)) then
1835 Delete_Choice := True;
1837 New_Cs := New_List;
1838 P := First (Static_Predicate (E));
1839 while Present (P) loop
1840 C := New_Copy (P);
1841 Set_Sloc (C, Sloc (Choice));
1842 Append_To (New_Cs, C);
1843 Next (P);
1844 end loop;
1846 Insert_List_After (Choice, New_Cs);
1847 end if;
1848 end if;
1849 end;
1850 end if;
1852 Nb_Choices := Nb_Choices + 1;
1854 declare
1855 C : constant Node_Id := Choice;
1857 begin
1858 Next (Choice);
1860 if Delete_Choice then
1861 Remove (C);
1862 Nb_Choices := Nb_Choices - 1;
1863 Delete_Choice := False;
1864 end if;
1865 end;
1866 end loop;
1868 Next (Assoc);
1869 end loop;
1870 end if;
1872 -- At this point we know that the others choice, if present, is by
1873 -- itself and appears last in the aggregate. Check if we have mixed
1874 -- positional and discrete associations (other than the others choice).
1876 if Present (Expressions (N))
1877 and then (Nb_Choices > 1
1878 or else (Nb_Choices = 1 and then not Others_Present))
1879 then
1880 Error_Msg_N
1881 ("named association cannot follow positional association",
1882 First (Choices (First (Component_Associations (N)))));
1883 return Failure;
1884 end if;
1886 -- Test for the validity of an others choice if present
1888 if Others_Present and then not Others_Allowed then
1889 Error_Msg_N
1890 ("OTHERS choice not allowed here",
1891 First (Choices (First (Component_Associations (N)))));
1892 return Failure;
1893 end if;
1895 -- Protect against cascaded errors
1897 if Etype (Index_Typ) = Any_Type then
1898 return Failure;
1899 end if;
1901 -- STEP 2: Process named components
1903 if No (Expressions (N)) then
1904 if Others_Present then
1905 Case_Table_Size := Nb_Choices - 1;
1906 else
1907 Case_Table_Size := Nb_Choices;
1908 end if;
1910 Step_2 : declare
1911 Low : Node_Id;
1912 High : Node_Id;
1913 -- Denote the lowest and highest values in an aggregate choice
1915 S_Low : Node_Id := Empty;
1916 S_High : Node_Id := Empty;
1917 -- if a choice in an aggregate is a subtype indication these
1918 -- denote the lowest and highest values of the subtype
1920 Table : Case_Table_Type (0 .. Case_Table_Size);
1921 -- Used to sort all the different choice values. Entry zero is
1922 -- reserved for sorting purposes.
1924 Single_Choice : Boolean;
1925 -- Set to true every time there is a single discrete choice in a
1926 -- discrete association
1928 Prev_Nb_Discrete_Choices : Nat;
1929 -- Used to keep track of the number of discrete choices in the
1930 -- current association.
1932 Errors_Posted_On_Choices : Boolean := False;
1933 -- Keeps track of whether any choices have semantic errors
1935 begin
1936 -- STEP 2 (A): Check discrete choices validity
1938 Assoc := First (Component_Associations (N));
1939 while Present (Assoc) loop
1940 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
1941 Choice := First (Choices (Assoc));
1942 loop
1943 Analyze (Choice);
1945 if Nkind (Choice) = N_Others_Choice then
1946 Single_Choice := False;
1947 exit;
1949 -- Test for subtype mark without constraint
1951 elsif Is_Entity_Name (Choice) and then
1952 Is_Type (Entity (Choice))
1953 then
1954 if Base_Type (Entity (Choice)) /= Index_Base then
1955 Error_Msg_N
1956 ("invalid subtype mark in aggregate choice",
1957 Choice);
1958 return Failure;
1959 end if;
1961 -- Case of subtype indication
1963 elsif Nkind (Choice) = N_Subtype_Indication then
1964 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
1966 -- Does the subtype indication evaluation raise CE?
1968 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
1969 Get_Index_Bounds (Choice, Low, High);
1970 Check_Bounds (S_Low, S_High, Low, High);
1972 -- Case of range or expression
1974 else
1975 Resolve (Choice, Index_Base);
1976 Check_Unset_Reference (Choice);
1977 Check_Non_Static_Context (Choice);
1979 -- If semantic errors were posted on the choice, then
1980 -- record that for possible early return from later
1981 -- processing (see handling of enumeration choices).
1983 if Error_Posted (Choice) then
1984 Errors_Posted_On_Choices := True;
1985 end if;
1987 -- Do not range check a choice. This check is redundant
1988 -- since this test is already done when we check that the
1989 -- bounds of the array aggregate are within range.
1991 Set_Do_Range_Check (Choice, False);
1993 -- In SPARK, the choice must be static
1995 if not (Is_Static_Expression (Choice)
1996 or else (Nkind (Choice) = N_Range
1997 and then Is_Static_Range (Choice)))
1998 then
1999 Check_SPARK_Restriction
2000 ("choice should be static", Choice);
2001 end if;
2002 end if;
2004 -- If we could not resolve the discrete choice stop here
2006 if Etype (Choice) = Any_Type then
2007 return Failure;
2009 -- If the discrete choice raises CE get its original bounds
2011 elsif Nkind (Choice) = N_Raise_Constraint_Error then
2012 Set_Raises_Constraint_Error (N);
2013 Get_Index_Bounds (Original_Node (Choice), Low, High);
2015 -- Otherwise get its bounds as usual
2017 else
2018 Get_Index_Bounds (Choice, Low, High);
2019 end if;
2021 if (Dynamic_Or_Null_Range (Low, High)
2022 or else (Nkind (Choice) = N_Subtype_Indication
2023 and then
2024 Dynamic_Or_Null_Range (S_Low, S_High)))
2025 and then Nb_Choices /= 1
2026 then
2027 Error_Msg_N
2028 ("dynamic or empty choice in aggregate " &
2029 "must be the only choice", Choice);
2030 return Failure;
2031 end if;
2033 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
2034 Table (Nb_Discrete_Choices).Lo := Low;
2035 Table (Nb_Discrete_Choices).Hi := High;
2036 Table (Nb_Discrete_Choices).Choice := Choice;
2038 Next (Choice);
2040 if No (Choice) then
2042 -- Check if we have a single discrete choice and whether
2043 -- this discrete choice specifies a single value.
2045 Single_Choice :=
2046 (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1)
2047 and then (Low = High);
2049 exit;
2050 end if;
2051 end loop;
2053 -- Ada 2005 (AI-231)
2055 if Ada_Version >= Ada_2005
2056 and then Known_Null (Expression (Assoc))
2057 then
2058 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2059 end if;
2061 -- Ada 2005 (AI-287): In case of default initialized component
2062 -- we delay the resolution to the expansion phase.
2064 if Box_Present (Assoc) then
2066 -- Ada 2005 (AI-287): In case of default initialization of a
2067 -- component the expander will generate calls to the
2068 -- corresponding initialization subprogram. We need to call
2069 -- Resolve_Aggr_Expr to check the rules about
2070 -- dimensionality.
2072 if not Resolve_Aggr_Expr
2073 (Assoc, Single_Elmt => Single_Choice)
2074 then
2075 return Failure;
2076 end if;
2078 elsif not Resolve_Aggr_Expr
2079 (Expression (Assoc), Single_Elmt => Single_Choice)
2080 then
2081 return Failure;
2083 -- Check incorrect use of dynamically tagged expression
2085 -- We differentiate here two cases because the expression may
2086 -- not be decorated. For example, the analysis and resolution
2087 -- of the expression associated with the others choice will be
2088 -- done later with the full aggregate. In such case we
2089 -- duplicate the expression tree to analyze the copy and
2090 -- perform the required check.
2092 elsif not Present (Etype (Expression (Assoc))) then
2093 declare
2094 Save_Analysis : constant Boolean := Full_Analysis;
2095 Expr : constant Node_Id :=
2096 New_Copy_Tree (Expression (Assoc));
2098 begin
2099 Expander_Mode_Save_And_Set (False);
2100 Full_Analysis := False;
2102 -- Analyze the expression, making sure it is properly
2103 -- attached to the tree before we do the analysis.
2105 Set_Parent (Expr, Parent (Expression (Assoc)));
2106 Analyze (Expr);
2108 -- If the expression is a literal, propagate this info
2109 -- to the expression in the association, to enable some
2110 -- optimizations downstream.
2112 if Is_Entity_Name (Expr)
2113 and then Present (Entity (Expr))
2114 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
2115 then
2116 Analyze_And_Resolve
2117 (Expression (Assoc), Component_Typ);
2118 end if;
2120 Full_Analysis := Save_Analysis;
2121 Expander_Mode_Restore;
2123 if Is_Tagged_Type (Etype (Expr)) then
2124 Check_Dynamically_Tagged_Expression
2125 (Expr => Expr,
2126 Typ => Component_Type (Etype (N)),
2127 Related_Nod => N);
2128 end if;
2129 end;
2131 elsif Is_Tagged_Type (Etype (Expression (Assoc))) then
2132 Check_Dynamically_Tagged_Expression
2133 (Expr => Expression (Assoc),
2134 Typ => Component_Type (Etype (N)),
2135 Related_Nod => N);
2136 end if;
2138 Next (Assoc);
2139 end loop;
2141 -- If aggregate contains more than one choice then these must be
2142 -- static. Check for duplicate and missing values.
2144 -- Note: there is duplicated code here wrt Check_Choice_Set in
2145 -- the body of Sem_Case, and it is possible we could just reuse
2146 -- that procedure. To be checked ???
2148 if Nb_Discrete_Choices > 1 then
2149 Check_Choices : declare
2150 Choice : Node_Id;
2151 -- Location of choice for messages
2153 Hi_Val : Uint;
2154 Lo_Val : Uint;
2155 -- High end of one range and Low end of the next. Should be
2156 -- contiguous if there is no hole in the list of values.
2158 Lo_Dup : Uint;
2159 Hi_Dup : Uint;
2160 -- End points of duplicated range
2162 Missing_Or_Duplicates : Boolean := False;
2163 -- Set True if missing or duplicate choices found
2165 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id);
2166 -- Output continuation message with a representation of the
2167 -- bounds (just Lo if Lo = Hi, else Lo .. Hi). C is the
2168 -- choice node where the message is to be posted.
2170 ------------------------
2171 -- Output_Bad_Choices --
2172 ------------------------
2174 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id) is
2175 begin
2176 -- Enumeration type case
2178 if Is_Enumeration_Type (Index_Typ) then
2179 Error_Msg_Name_1 :=
2180 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Lo, Loc));
2181 Error_Msg_Name_2 :=
2182 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Hi, Loc));
2184 if Lo = Hi then
2185 Error_Msg_N ("\\ %!", C);
2186 else
2187 Error_Msg_N ("\\ % .. %!", C);
2188 end if;
2190 -- Integer types case
2192 else
2193 Error_Msg_Uint_1 := Lo;
2194 Error_Msg_Uint_2 := Hi;
2196 if Lo = Hi then
2197 Error_Msg_N ("\\ ^!", C);
2198 else
2199 Error_Msg_N ("\\ ^ .. ^!", C);
2200 end if;
2201 end if;
2202 end Output_Bad_Choices;
2204 -- Start of processing for Check_Choices
2206 begin
2207 Sort_Case_Table (Table);
2209 -- First we do a quick linear loop to find out if we have
2210 -- any duplicates or missing entries (usually we have a
2211 -- legal aggregate, so this will get us out quickly).
2213 for J in 1 .. Nb_Discrete_Choices - 1 loop
2214 Hi_Val := Expr_Value (Table (J).Hi);
2215 Lo_Val := Expr_Value (Table (J + 1).Lo);
2217 if Lo_Val <= Hi_Val
2218 or else (Lo_Val > Hi_Val + 1
2219 and then not Others_Present)
2220 then
2221 Missing_Or_Duplicates := True;
2222 exit;
2223 end if;
2224 end loop;
2226 -- If we have missing or duplicate entries, first fill in
2227 -- the Highest entries to make life easier in the following
2228 -- loops to detect bad entries.
2230 if Missing_Or_Duplicates then
2231 Table (1).Highest := Expr_Value (Table (1).Hi);
2233 for J in 2 .. Nb_Discrete_Choices loop
2234 Table (J).Highest :=
2235 UI_Max
2236 (Table (J - 1).Highest, Expr_Value (Table (J).Hi));
2237 end loop;
2239 -- Loop through table entries to find duplicate indexes
2241 for J in 2 .. Nb_Discrete_Choices loop
2242 Lo_Val := Expr_Value (Table (J).Lo);
2243 Hi_Val := Expr_Value (Table (J).Hi);
2245 -- Case where we have duplicates (the lower bound of
2246 -- this choice is less than or equal to the highest
2247 -- high bound found so far).
2249 if Lo_Val <= Table (J - 1).Highest then
2251 -- We move backwards looking for duplicates. We can
2252 -- abandon this loop as soon as we reach a choice
2253 -- highest value that is less than Lo_Val.
2255 for K in reverse 1 .. J - 1 loop
2256 exit when Table (K).Highest < Lo_Val;
2258 -- Here we may have duplicates between entries
2259 -- for K and J. Get range of duplicates.
2261 Lo_Dup :=
2262 UI_Max (Lo_Val, Expr_Value (Table (K).Lo));
2263 Hi_Dup :=
2264 UI_Min (Hi_Val, Expr_Value (Table (K).Hi));
2266 -- Nothing to do if duplicate range is null
2268 if Lo_Dup > Hi_Dup then
2269 null;
2271 -- Otherwise place proper message
2273 else
2274 -- We place message on later choice, with a
2275 -- line reference to the earlier choice.
2277 if Sloc (Table (J).Choice) <
2278 Sloc (Table (K).Choice)
2279 then
2280 Choice := Table (K).Choice;
2281 Error_Msg_Sloc := Sloc (Table (J).Choice);
2282 else
2283 Choice := Table (J).Choice;
2284 Error_Msg_Sloc := Sloc (Table (K).Choice);
2285 end if;
2287 if Lo_Dup = Hi_Dup then
2288 Error_Msg_N
2289 ("index value in array aggregate "
2290 & "duplicates the one given#!", Choice);
2291 else
2292 Error_Msg_N
2293 ("index values in array aggregate "
2294 & "duplicate those given#!", Choice);
2295 end if;
2297 Output_Bad_Choices (Lo_Dup, Hi_Dup, Choice);
2298 end if;
2299 end loop;
2300 end if;
2301 end loop;
2303 -- Loop through entries in table to find missing indexes.
2304 -- Not needed if others, since missing impossible.
2306 if not Others_Present then
2307 for J in 2 .. Nb_Discrete_Choices loop
2308 Lo_Val := Expr_Value (Table (J).Lo);
2309 Hi_Val := Table (J - 1).Highest;
2311 if Lo_Val > Hi_Val + 1 then
2312 Choice := Table (J).Lo;
2314 if Hi_Val + 1 = Lo_Val - 1 then
2315 Error_Msg_N
2316 ("missing index value in array aggregate!",
2317 Choice);
2318 else
2319 Error_Msg_N
2320 ("missing index values in array aggregate!",
2321 Choice);
2322 end if;
2324 Output_Bad_Choices
2325 (Hi_Val + 1, Lo_Val - 1, Choice);
2326 end if;
2327 end loop;
2328 end if;
2330 -- If either missing or duplicate values, return failure
2332 Set_Etype (N, Any_Composite);
2333 return Failure;
2334 end if;
2335 end Check_Choices;
2336 end if;
2338 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
2340 if Nb_Discrete_Choices > 0 then
2341 Choices_Low := Table (1).Lo;
2342 Choices_High := Table (Nb_Discrete_Choices).Hi;
2343 end if;
2345 -- If Others is present, then bounds of aggregate come from the
2346 -- index constraint (not the choices in the aggregate itself).
2348 if Others_Present then
2349 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2351 -- No others clause present
2353 else
2354 -- Special processing if others allowed and not present. This
2355 -- means that the bounds of the aggregate come from the index
2356 -- constraint (and the length must match).
2358 if Others_Allowed then
2359 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2361 -- If others allowed, and no others present, then the array
2362 -- should cover all index values. If it does not, we will
2363 -- get a length check warning, but there is two cases where
2364 -- an additional warning is useful:
2366 -- If we have no positional components, and the length is
2367 -- wrong (which we can tell by others being allowed with
2368 -- missing components), and the index type is an enumeration
2369 -- type, then issue appropriate warnings about these missing
2370 -- components. They are only warnings, since the aggregate
2371 -- is fine, it's just the wrong length. We skip this check
2372 -- for standard character types (since there are no literals
2373 -- and it is too much trouble to concoct them), and also if
2374 -- any of the bounds have not-known-at-compile-time values.
2376 -- Another case warranting a warning is when the length is
2377 -- right, but as above we have an index type that is an
2378 -- enumeration, and the bounds do not match. This is a
2379 -- case where dubious sliding is allowed and we generate
2380 -- a warning that the bounds do not match.
2382 if No (Expressions (N))
2383 and then Nkind (Index) = N_Range
2384 and then Is_Enumeration_Type (Etype (Index))
2385 and then not Is_Standard_Character_Type (Etype (Index))
2386 and then Compile_Time_Known_Value (Aggr_Low)
2387 and then Compile_Time_Known_Value (Aggr_High)
2388 and then Compile_Time_Known_Value (Choices_Low)
2389 and then Compile_Time_Known_Value (Choices_High)
2390 then
2391 -- If any of the expressions or range bounds in choices
2392 -- have semantic errors, then do not attempt further
2393 -- resolution, to prevent cascaded errors.
2395 if Errors_Posted_On_Choices then
2396 return Failure;
2397 end if;
2399 declare
2400 ALo : constant Node_Id := Expr_Value_E (Aggr_Low);
2401 AHi : constant Node_Id := Expr_Value_E (Aggr_High);
2402 CLo : constant Node_Id := Expr_Value_E (Choices_Low);
2403 CHi : constant Node_Id := Expr_Value_E (Choices_High);
2405 Ent : Entity_Id;
2407 begin
2408 -- Warning case 1, missing values at start/end. Only
2409 -- do the check if the number of entries is too small.
2411 if (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2413 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2414 then
2415 Error_Msg_N
2416 ("missing index value(s) in array aggregate??",
2419 -- Output missing value(s) at start
2421 if Chars (ALo) /= Chars (CLo) then
2422 Ent := Prev (CLo);
2424 if Chars (ALo) = Chars (Ent) then
2425 Error_Msg_Name_1 := Chars (ALo);
2426 Error_Msg_N ("\ %??", N);
2427 else
2428 Error_Msg_Name_1 := Chars (ALo);
2429 Error_Msg_Name_2 := Chars (Ent);
2430 Error_Msg_N ("\ % .. %??", N);
2431 end if;
2432 end if;
2434 -- Output missing value(s) at end
2436 if Chars (AHi) /= Chars (CHi) then
2437 Ent := Next (CHi);
2439 if Chars (AHi) = Chars (Ent) then
2440 Error_Msg_Name_1 := Chars (Ent);
2441 Error_Msg_N ("\ %??", N);
2442 else
2443 Error_Msg_Name_1 := Chars (Ent);
2444 Error_Msg_Name_2 := Chars (AHi);
2445 Error_Msg_N ("\ % .. %??", N);
2446 end if;
2447 end if;
2449 -- Warning case 2, dubious sliding. The First_Subtype
2450 -- test distinguishes between a constrained type where
2451 -- sliding is not allowed (so we will get a warning
2452 -- later that Constraint_Error will be raised), and
2453 -- the unconstrained case where sliding is permitted.
2455 elsif (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2457 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2458 and then Chars (ALo) /= Chars (CLo)
2459 and then
2460 not Is_Constrained (First_Subtype (Etype (N)))
2461 then
2462 Error_Msg_N
2463 ("bounds of aggregate do not match target??", N);
2464 end if;
2465 end;
2466 end if;
2467 end if;
2469 -- If no others, aggregate bounds come from aggregate
2471 Aggr_Low := Choices_Low;
2472 Aggr_High := Choices_High;
2473 end if;
2474 end Step_2;
2476 -- STEP 3: Process positional components
2478 else
2479 -- STEP 3 (A): Process positional elements
2481 Expr := First (Expressions (N));
2482 Nb_Elements := Uint_0;
2483 while Present (Expr) loop
2484 Nb_Elements := Nb_Elements + 1;
2486 -- Ada 2005 (AI-231)
2488 if Ada_Version >= Ada_2005
2489 and then Known_Null (Expr)
2490 then
2491 Check_Can_Never_Be_Null (Etype (N), Expr);
2492 end if;
2494 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
2495 return Failure;
2496 end if;
2498 -- Check incorrect use of dynamically tagged expression
2500 if Is_Tagged_Type (Etype (Expr)) then
2501 Check_Dynamically_Tagged_Expression
2502 (Expr => Expr,
2503 Typ => Component_Type (Etype (N)),
2504 Related_Nod => N);
2505 end if;
2507 Next (Expr);
2508 end loop;
2510 if Others_Present then
2511 Assoc := Last (Component_Associations (N));
2513 -- Ada 2005 (AI-231)
2515 if Ada_Version >= Ada_2005
2516 and then Known_Null (Assoc)
2517 then
2518 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2519 end if;
2521 -- Ada 2005 (AI-287): In case of default initialized component,
2522 -- we delay the resolution to the expansion phase.
2524 if Box_Present (Assoc) then
2526 -- Ada 2005 (AI-287): In case of default initialization of a
2527 -- component the expander will generate calls to the
2528 -- corresponding initialization subprogram. We need to call
2529 -- Resolve_Aggr_Expr to check the rules about
2530 -- dimensionality.
2532 if not Resolve_Aggr_Expr (Assoc, Single_Elmt => False) then
2533 return Failure;
2534 end if;
2536 elsif not Resolve_Aggr_Expr (Expression (Assoc),
2537 Single_Elmt => False)
2538 then
2539 return Failure;
2541 -- Check incorrect use of dynamically tagged expression. The
2542 -- expression of the others choice has not been resolved yet.
2543 -- In order to diagnose the semantic error we create a duplicate
2544 -- tree to analyze it and perform the check.
2546 else
2547 declare
2548 Save_Analysis : constant Boolean := Full_Analysis;
2549 Expr : constant Node_Id :=
2550 New_Copy_Tree (Expression (Assoc));
2552 begin
2553 Expander_Mode_Save_And_Set (False);
2554 Full_Analysis := False;
2555 Analyze (Expr);
2556 Full_Analysis := Save_Analysis;
2557 Expander_Mode_Restore;
2559 if Is_Tagged_Type (Etype (Expr)) then
2560 Check_Dynamically_Tagged_Expression
2561 (Expr => Expr,
2562 Typ => Component_Type (Etype (N)),
2563 Related_Nod => N);
2564 end if;
2565 end;
2566 end if;
2567 end if;
2569 -- STEP 3 (B): Compute the aggregate bounds
2571 if Others_Present then
2572 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2574 else
2575 if Others_Allowed then
2576 Get_Index_Bounds (Index_Constr, Aggr_Low, Discard);
2577 else
2578 Aggr_Low := Index_Typ_Low;
2579 end if;
2581 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
2582 Check_Bound (Index_Base_High, Aggr_High);
2583 end if;
2584 end if;
2586 -- STEP 4: Perform static aggregate checks and save the bounds
2588 -- Check (A)
2590 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
2591 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
2593 -- Check (B)
2595 if Others_Present and then Nb_Discrete_Choices > 0 then
2596 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
2597 Check_Bounds (Index_Typ_Low, Index_Typ_High,
2598 Choices_Low, Choices_High);
2599 Check_Bounds (Index_Base_Low, Index_Base_High,
2600 Choices_Low, Choices_High);
2602 -- Check (C)
2604 elsif Others_Present and then Nb_Elements > 0 then
2605 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
2606 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
2607 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
2608 end if;
2610 if Raises_Constraint_Error (Aggr_Low)
2611 or else Raises_Constraint_Error (Aggr_High)
2612 then
2613 Set_Raises_Constraint_Error (N);
2614 end if;
2616 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
2618 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
2619 -- since the addition node returned by Add is not yet analyzed. Attach
2620 -- to tree and analyze first. Reset analyzed flag to ensure it will get
2621 -- analyzed when it is a literal bound whose type must be properly set.
2623 if Others_Present or else Nb_Discrete_Choices > 0 then
2624 Aggr_High := Duplicate_Subexpr (Aggr_High);
2626 if Etype (Aggr_High) = Universal_Integer then
2627 Set_Analyzed (Aggr_High, False);
2628 end if;
2629 end if;
2631 -- If the aggregate already has bounds attached to it, it means this is
2632 -- a positional aggregate created as an optimization by
2633 -- Exp_Aggr.Convert_To_Positional, so we don't want to change those
2634 -- bounds.
2636 if Present (Aggregate_Bounds (N)) and then not Others_Allowed then
2637 Aggr_Low := Low_Bound (Aggregate_Bounds (N));
2638 Aggr_High := High_Bound (Aggregate_Bounds (N));
2639 end if;
2641 Set_Aggregate_Bounds
2642 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
2644 -- The bounds may contain expressions that must be inserted upwards.
2645 -- Attach them fully to the tree. After analysis, remove side effects
2646 -- from upper bound, if still needed.
2648 Set_Parent (Aggregate_Bounds (N), N);
2649 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
2650 Check_Unset_Reference (Aggregate_Bounds (N));
2652 if not Others_Present and then Nb_Discrete_Choices = 0 then
2653 Set_High_Bound
2654 (Aggregate_Bounds (N),
2655 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
2656 end if;
2658 -- Check the dimensions of each component in the array aggregate
2660 Analyze_Dimension_Array_Aggregate (N, Component_Typ);
2662 return Success;
2663 end Resolve_Array_Aggregate;
2665 ---------------------------------
2666 -- Resolve_Extension_Aggregate --
2667 ---------------------------------
2669 -- There are two cases to consider:
2671 -- a) If the ancestor part is a type mark, the components needed are the
2672 -- difference between the components of the expected type and the
2673 -- components of the given type mark.
2675 -- b) If the ancestor part is an expression, it must be unambiguous, and
2676 -- once we have its type we can also compute the needed components as in
2677 -- the previous case. In both cases, if the ancestor type is not the
2678 -- immediate ancestor, we have to build this ancestor recursively.
2680 -- In both cases, discriminants of the ancestor type do not play a role in
2681 -- the resolution of the needed components, because inherited discriminants
2682 -- cannot be used in a type extension. As a result we can compute
2683 -- independently the list of components of the ancestor type and of the
2684 -- expected type.
2686 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
2687 A : constant Node_Id := Ancestor_Part (N);
2688 A_Type : Entity_Id;
2689 I : Interp_Index;
2690 It : Interp;
2692 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean;
2693 -- If the type is limited, verify that the ancestor part is a legal
2694 -- expression (aggregate or function call, including 'Input)) that does
2695 -- not require a copy, as specified in 7.5(2).
2697 function Valid_Ancestor_Type return Boolean;
2698 -- Verify that the type of the ancestor part is a non-private ancestor
2699 -- of the expected type, which must be a type extension.
2701 ----------------------------
2702 -- Valid_Limited_Ancestor --
2703 ----------------------------
2705 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean is
2706 begin
2707 if Is_Entity_Name (Anc)
2708 and then Is_Type (Entity (Anc))
2709 then
2710 return True;
2712 elsif Nkind_In (Anc, N_Aggregate, N_Function_Call) then
2713 return True;
2715 elsif Nkind (Anc) = N_Attribute_Reference
2716 and then Attribute_Name (Anc) = Name_Input
2717 then
2718 return True;
2720 elsif Nkind (Anc) = N_Qualified_Expression then
2721 return Valid_Limited_Ancestor (Expression (Anc));
2723 else
2724 return False;
2725 end if;
2726 end Valid_Limited_Ancestor;
2728 -------------------------
2729 -- Valid_Ancestor_Type --
2730 -------------------------
2732 function Valid_Ancestor_Type return Boolean is
2733 Imm_Type : Entity_Id;
2735 begin
2736 Imm_Type := Base_Type (Typ);
2737 while Is_Derived_Type (Imm_Type) loop
2738 if Etype (Imm_Type) = Base_Type (A_Type) then
2739 return True;
2741 -- The base type of the parent type may appear as a private
2742 -- extension if it is declared as such in a parent unit of the
2743 -- current one. For consistency of the subsequent analysis use
2744 -- the partial view for the ancestor part.
2746 elsif Is_Private_Type (Etype (Imm_Type))
2747 and then Present (Full_View (Etype (Imm_Type)))
2748 and then Base_Type (A_Type) = Full_View (Etype (Imm_Type))
2749 then
2750 A_Type := Etype (Imm_Type);
2751 return True;
2753 -- The parent type may be a private extension. The aggregate is
2754 -- legal if the type of the aggregate is an extension of it that
2755 -- is not a private extension.
2757 elsif Is_Private_Type (A_Type)
2758 and then not Is_Private_Type (Imm_Type)
2759 and then Present (Full_View (A_Type))
2760 and then Base_Type (Full_View (A_Type)) = Etype (Imm_Type)
2761 then
2762 return True;
2764 else
2765 Imm_Type := Etype (Base_Type (Imm_Type));
2766 end if;
2767 end loop;
2769 -- If previous loop did not find a proper ancestor, report error
2771 Error_Msg_NE ("expect ancestor type of &", A, Typ);
2772 return False;
2773 end Valid_Ancestor_Type;
2775 -- Start of processing for Resolve_Extension_Aggregate
2777 begin
2778 -- Analyze the ancestor part and account for the case where it is a
2779 -- parameterless function call.
2781 Analyze (A);
2782 Check_Parameterless_Call (A);
2784 -- In SPARK, the ancestor part cannot be a type mark
2786 if Is_Entity_Name (A)
2787 and then Is_Type (Entity (A))
2788 then
2789 Check_SPARK_Restriction ("ancestor part cannot be a type mark", A);
2791 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
2792 -- must not have unknown discriminants.
2794 if Has_Unknown_Discriminants (Root_Type (Typ)) then
2795 Error_Msg_NE
2796 ("aggregate not available for type& whose ancestor "
2797 & "has unknown discriminants", N, Typ);
2798 end if;
2799 end if;
2801 if not Is_Tagged_Type (Typ) then
2802 Error_Msg_N ("type of extension aggregate must be tagged", N);
2803 return;
2805 elsif Is_Limited_Type (Typ) then
2807 -- Ada 2005 (AI-287): Limited aggregates are allowed
2809 if Ada_Version < Ada_2005 then
2810 Error_Msg_N ("aggregate type cannot be limited", N);
2811 Explain_Limited_Type (Typ, N);
2812 return;
2814 elsif Valid_Limited_Ancestor (A) then
2815 null;
2817 else
2818 Error_Msg_N
2819 ("limited ancestor part must be aggregate or function call", A);
2820 end if;
2822 elsif Is_Class_Wide_Type (Typ) then
2823 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
2824 return;
2825 end if;
2827 if Is_Entity_Name (A)
2828 and then Is_Type (Entity (A))
2829 then
2830 A_Type := Get_Full_View (Entity (A));
2832 if Valid_Ancestor_Type then
2833 Set_Entity (A, A_Type);
2834 Set_Etype (A, A_Type);
2836 Validate_Ancestor_Part (N);
2837 Resolve_Record_Aggregate (N, Typ);
2838 end if;
2840 elsif Nkind (A) /= N_Aggregate then
2841 if Is_Overloaded (A) then
2842 A_Type := Any_Type;
2844 Get_First_Interp (A, I, It);
2845 while Present (It.Typ) loop
2846 -- Only consider limited interpretations in the Ada 2005 case
2848 if Is_Tagged_Type (It.Typ)
2849 and then (Ada_Version >= Ada_2005
2850 or else not Is_Limited_Type (It.Typ))
2851 then
2852 if A_Type /= Any_Type then
2853 Error_Msg_N ("cannot resolve expression", A);
2854 return;
2855 else
2856 A_Type := It.Typ;
2857 end if;
2858 end if;
2860 Get_Next_Interp (I, It);
2861 end loop;
2863 if A_Type = Any_Type then
2864 if Ada_Version >= Ada_2005 then
2865 Error_Msg_N ("ancestor part must be of a tagged type", A);
2866 else
2867 Error_Msg_N
2868 ("ancestor part must be of a nonlimited tagged type", A);
2869 end if;
2871 return;
2872 end if;
2874 else
2875 A_Type := Etype (A);
2876 end if;
2878 if Valid_Ancestor_Type then
2879 Resolve (A, A_Type);
2880 Check_Unset_Reference (A);
2881 Check_Non_Static_Context (A);
2883 -- The aggregate is illegal if the ancestor expression is a call
2884 -- to a function with a limited unconstrained result, unless the
2885 -- type of the aggregate is a null extension. This restriction
2886 -- was added in AI05-67 to simplify implementation.
2888 if Nkind (A) = N_Function_Call
2889 and then Is_Limited_Type (A_Type)
2890 and then not Is_Null_Extension (Typ)
2891 and then not Is_Constrained (A_Type)
2892 then
2893 Error_Msg_N
2894 ("type of limited ancestor part must be constrained", A);
2896 -- Reject the use of CPP constructors that leave objects partially
2897 -- initialized. For example:
2899 -- type CPP_Root is tagged limited record ...
2900 -- pragma Import (CPP, CPP_Root);
2902 -- type CPP_DT is new CPP_Root and Iface ...
2903 -- pragma Import (CPP, CPP_DT);
2905 -- type Ada_DT is new CPP_DT with ...
2907 -- Obj : Ada_DT := Ada_DT'(New_CPP_Root with others => <>);
2909 -- Using the constructor of CPP_Root the slots of the dispatch
2910 -- table of CPP_DT cannot be set, and the secondary tag of
2911 -- CPP_DT is unknown.
2913 elsif Nkind (A) = N_Function_Call
2914 and then Is_CPP_Constructor_Call (A)
2915 and then Enclosing_CPP_Parent (Typ) /= A_Type
2916 then
2917 Error_Msg_NE
2918 ("??must use 'C'P'P constructor for type &", A,
2919 Enclosing_CPP_Parent (Typ));
2921 -- The following call is not needed if the previous warning
2922 -- is promoted to an error.
2924 Resolve_Record_Aggregate (N, Typ);
2926 elsif Is_Class_Wide_Type (Etype (A))
2927 and then Nkind (Original_Node (A)) = N_Function_Call
2928 then
2929 -- If the ancestor part is a dispatching call, it appears
2930 -- statically to be a legal ancestor, but it yields any member
2931 -- of the class, and it is not possible to determine whether
2932 -- it is an ancestor of the extension aggregate (much less
2933 -- which ancestor). It is not possible to determine the
2934 -- components of the extension part.
2936 -- This check implements AI-306, which in fact was motivated by
2937 -- an AdaCore query to the ARG after this test was added.
2939 Error_Msg_N ("ancestor part must be statically tagged", A);
2940 else
2941 Resolve_Record_Aggregate (N, Typ);
2942 end if;
2943 end if;
2945 else
2946 Error_Msg_N ("no unique type for this aggregate", A);
2947 end if;
2949 Check_Function_Writable_Actuals (N);
2950 end Resolve_Extension_Aggregate;
2952 ------------------------------
2953 -- Resolve_Record_Aggregate --
2954 ------------------------------
2956 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
2957 Assoc : Node_Id;
2958 -- N_Component_Association node belonging to the input aggregate N
2960 Expr : Node_Id;
2961 Positional_Expr : Node_Id;
2962 Component : Entity_Id;
2963 Component_Elmt : Elmt_Id;
2965 Components : constant Elist_Id := New_Elmt_List;
2966 -- Components is the list of the record components whose value must be
2967 -- provided in the aggregate. This list does include discriminants.
2969 New_Assoc_List : constant List_Id := New_List;
2970 New_Assoc : Node_Id;
2971 -- New_Assoc_List is the newly built list of N_Component_Association
2972 -- nodes. New_Assoc is one such N_Component_Association node in it.
2973 -- Note that while Assoc and New_Assoc contain the same kind of nodes,
2974 -- they are used to iterate over two different N_Component_Association
2975 -- lists.
2977 Others_Etype : Entity_Id := Empty;
2978 -- This variable is used to save the Etype of the last record component
2979 -- that takes its value from the others choice. Its purpose is:
2981 -- (a) make sure the others choice is useful
2983 -- (b) make sure the type of all the components whose value is
2984 -- subsumed by the others choice are the same.
2986 -- This variable is updated as a side effect of function Get_Value.
2988 Is_Box_Present : Boolean := False;
2989 Others_Box : Boolean := False;
2990 -- Ada 2005 (AI-287): Variables used in case of default initialization
2991 -- to provide a functionality similar to Others_Etype. Box_Present
2992 -- indicates that the component takes its default initialization;
2993 -- Others_Box indicates that at least one component takes its default
2994 -- initialization. Similar to Others_Etype, they are also updated as a
2995 -- side effect of function Get_Value.
2997 procedure Add_Association
2998 (Component : Entity_Id;
2999 Expr : Node_Id;
3000 Assoc_List : List_Id;
3001 Is_Box_Present : Boolean := False);
3002 -- Builds a new N_Component_Association node which associates Component
3003 -- to expression Expr and adds it to the association list being built,
3004 -- either New_Assoc_List, or the association being built for an inner
3005 -- aggregate.
3007 function Discr_Present (Discr : Entity_Id) return Boolean;
3008 -- If aggregate N is a regular aggregate this routine will return True.
3009 -- Otherwise, if N is an extension aggregate, Discr is a discriminant
3010 -- whose value may already have been specified by N's ancestor part.
3011 -- This routine checks whether this is indeed the case and if so returns
3012 -- False, signaling that no value for Discr should appear in N's
3013 -- aggregate part. Also, in this case, the routine appends to
3014 -- New_Assoc_List the discriminant value specified in the ancestor part.
3016 -- If the aggregate is in a context with expansion delayed, it will be
3017 -- reanalyzed. The inherited discriminant values must not be reinserted
3018 -- in the component list to prevent spurious errors, but they must be
3019 -- present on first analysis to build the proper subtype indications.
3020 -- The flag Inherited_Discriminant is used to prevent the re-insertion.
3022 function Get_Value
3023 (Compon : Node_Id;
3024 From : List_Id;
3025 Consider_Others_Choice : Boolean := False)
3026 return Node_Id;
3027 -- Given a record component stored in parameter Compon, this function
3028 -- returns its value as it appears in the list From, which is a list
3029 -- of N_Component_Association nodes.
3031 -- If no component association has a choice for the searched component,
3032 -- the value provided by the others choice is returned, if there is one,
3033 -- and Consider_Others_Choice is set to true. Otherwise Empty is
3034 -- returned. If there is more than one component association giving a
3035 -- value for the searched record component, an error message is emitted
3036 -- and the first found value is returned.
3038 -- If Consider_Others_Choice is set and the returned expression comes
3039 -- from the others choice, then Others_Etype is set as a side effect.
3040 -- An error message is emitted if the components taking their value from
3041 -- the others choice do not have same type.
3043 function New_Copy_Tree_And_Copy_Dimensions
3044 (Source : Node_Id;
3045 Map : Elist_Id := No_Elist;
3046 New_Sloc : Source_Ptr := No_Location;
3047 New_Scope : Entity_Id := Empty) return Node_Id;
3048 -- Same as New_Copy_Tree (defined in Sem_Util), except that this routine
3049 -- also copies the dimensions of Source to the returned node.
3051 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id);
3052 -- Analyzes and resolves expression Expr against the Etype of the
3053 -- Component. This routine also applies all appropriate checks to Expr.
3054 -- It finally saves a Expr in the newly created association list that
3055 -- will be attached to the final record aggregate. Note that if the
3056 -- Parent pointer of Expr is not set then Expr was produced with a
3057 -- New_Copy_Tree or some such.
3059 ---------------------
3060 -- Add_Association --
3061 ---------------------
3063 procedure Add_Association
3064 (Component : Entity_Id;
3065 Expr : Node_Id;
3066 Assoc_List : List_Id;
3067 Is_Box_Present : Boolean := False)
3069 Loc : Source_Ptr;
3070 Choice_List : constant List_Id := New_List;
3071 New_Assoc : Node_Id;
3073 begin
3074 -- If this is a box association the expression is missing, so
3075 -- use the Sloc of the aggregate itself for the new association.
3077 if Present (Expr) then
3078 Loc := Sloc (Expr);
3079 else
3080 Loc := Sloc (N);
3081 end if;
3083 Append (New_Occurrence_Of (Component, Loc), Choice_List);
3084 New_Assoc :=
3085 Make_Component_Association (Loc,
3086 Choices => Choice_List,
3087 Expression => Expr,
3088 Box_Present => Is_Box_Present);
3089 Append (New_Assoc, Assoc_List);
3090 end Add_Association;
3092 -------------------
3093 -- Discr_Present --
3094 -------------------
3096 function Discr_Present (Discr : Entity_Id) return Boolean is
3097 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
3099 Loc : Source_Ptr;
3101 Ancestor : Node_Id;
3102 Comp_Assoc : Node_Id;
3103 Discr_Expr : Node_Id;
3105 Ancestor_Typ : Entity_Id;
3106 Orig_Discr : Entity_Id;
3107 D : Entity_Id;
3108 D_Val : Elmt_Id := No_Elmt; -- stop junk warning
3110 Ancestor_Is_Subtyp : Boolean;
3112 begin
3113 if Regular_Aggr then
3114 return True;
3115 end if;
3117 -- Check whether inherited discriminant values have already been
3118 -- inserted in the aggregate. This will be the case if we are
3119 -- re-analyzing an aggregate whose expansion was delayed.
3121 if Present (Component_Associations (N)) then
3122 Comp_Assoc := First (Component_Associations (N));
3123 while Present (Comp_Assoc) loop
3124 if Inherited_Discriminant (Comp_Assoc) then
3125 return True;
3126 end if;
3128 Next (Comp_Assoc);
3129 end loop;
3130 end if;
3132 Ancestor := Ancestor_Part (N);
3133 Ancestor_Typ := Etype (Ancestor);
3134 Loc := Sloc (Ancestor);
3136 -- For a private type with unknown discriminants, use the underlying
3137 -- record view if it is available.
3139 if Has_Unknown_Discriminants (Ancestor_Typ)
3140 and then Present (Full_View (Ancestor_Typ))
3141 and then Present (Underlying_Record_View (Full_View (Ancestor_Typ)))
3142 then
3143 Ancestor_Typ := Underlying_Record_View (Full_View (Ancestor_Typ));
3144 end if;
3146 Ancestor_Is_Subtyp :=
3147 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
3149 -- If the ancestor part has no discriminants clearly N's aggregate
3150 -- part must provide a value for Discr.
3152 if not Has_Discriminants (Ancestor_Typ) then
3153 return True;
3155 -- If the ancestor part is an unconstrained subtype mark then the
3156 -- Discr must be present in N's aggregate part.
3158 elsif Ancestor_Is_Subtyp
3159 and then not Is_Constrained (Entity (Ancestor))
3160 then
3161 return True;
3162 end if;
3164 -- Now look to see if Discr was specified in the ancestor part
3166 if Ancestor_Is_Subtyp then
3167 D_Val := First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
3168 end if;
3170 Orig_Discr := Original_Record_Component (Discr);
3172 D := First_Discriminant (Ancestor_Typ);
3173 while Present (D) loop
3175 -- If Ancestor has already specified Disc value then insert its
3176 -- value in the final aggregate.
3178 if Original_Record_Component (D) = Orig_Discr then
3179 if Ancestor_Is_Subtyp then
3180 Discr_Expr := New_Copy_Tree (Node (D_Val));
3181 else
3182 Discr_Expr :=
3183 Make_Selected_Component (Loc,
3184 Prefix => Duplicate_Subexpr (Ancestor),
3185 Selector_Name => New_Occurrence_Of (Discr, Loc));
3186 end if;
3188 Resolve_Aggr_Expr (Discr_Expr, Discr);
3189 Set_Inherited_Discriminant (Last (New_Assoc_List));
3190 return False;
3191 end if;
3193 Next_Discriminant (D);
3195 if Ancestor_Is_Subtyp then
3196 Next_Elmt (D_Val);
3197 end if;
3198 end loop;
3200 return True;
3201 end Discr_Present;
3203 ---------------
3204 -- Get_Value --
3205 ---------------
3207 function Get_Value
3208 (Compon : Node_Id;
3209 From : List_Id;
3210 Consider_Others_Choice : Boolean := False)
3211 return Node_Id
3213 Assoc : Node_Id;
3214 Expr : Node_Id := Empty;
3215 Selector_Name : Node_Id;
3217 begin
3218 Is_Box_Present := False;
3220 if Present (From) then
3221 Assoc := First (From);
3222 else
3223 return Empty;
3224 end if;
3226 while Present (Assoc) loop
3227 Selector_Name := First (Choices (Assoc));
3228 while Present (Selector_Name) loop
3229 if Nkind (Selector_Name) = N_Others_Choice then
3230 if Consider_Others_Choice and then No (Expr) then
3232 -- We need to duplicate the expression for each
3233 -- successive component covered by the others choice.
3234 -- This is redundant if the others_choice covers only
3235 -- one component (small optimization possible???), but
3236 -- indispensable otherwise, because each one must be
3237 -- expanded individually to preserve side-effects.
3239 -- Ada 2005 (AI-287): In case of default initialization
3240 -- of components, we duplicate the corresponding default
3241 -- expression (from the record type declaration). The
3242 -- copy must carry the sloc of the association (not the
3243 -- original expression) to prevent spurious elaboration
3244 -- checks when the default includes function calls.
3246 if Box_Present (Assoc) then
3247 Others_Box := True;
3248 Is_Box_Present := True;
3250 if Expander_Active then
3251 return
3252 New_Copy_Tree_And_Copy_Dimensions
3253 (Expression (Parent (Compon)),
3254 New_Sloc => Sloc (Assoc));
3255 else
3256 return Expression (Parent (Compon));
3257 end if;
3259 else
3260 if Present (Others_Etype) and then
3261 Base_Type (Others_Etype) /= Base_Type (Etype
3262 (Compon))
3263 then
3264 Error_Msg_N ("components in OTHERS choice must " &
3265 "have same type", Selector_Name);
3266 end if;
3268 Others_Etype := Etype (Compon);
3270 if Expander_Active then
3271 return
3272 New_Copy_Tree_And_Copy_Dimensions
3273 (Expression (Assoc));
3274 else
3275 return Expression (Assoc);
3276 end if;
3277 end if;
3278 end if;
3280 elsif Chars (Compon) = Chars (Selector_Name) then
3281 if No (Expr) then
3283 -- Ada 2005 (AI-231)
3285 if Ada_Version >= Ada_2005
3286 and then Known_Null (Expression (Assoc))
3287 then
3288 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
3289 end if;
3291 -- We need to duplicate the expression when several
3292 -- components are grouped together with a "|" choice.
3293 -- For instance "filed1 | filed2 => Expr"
3295 -- Ada 2005 (AI-287)
3297 if Box_Present (Assoc) then
3298 Is_Box_Present := True;
3300 -- Duplicate the default expression of the component
3301 -- from the record type declaration, so a new copy
3302 -- can be attached to the association.
3304 -- Note that we always copy the default expression,
3305 -- even when the association has a single choice, in
3306 -- order to create a proper association for the
3307 -- expanded aggregate.
3309 -- Component may have no default, in which case the
3310 -- expression is empty and the component is default-
3311 -- initialized, but an association for the component
3312 -- exists, and it is not covered by an others clause.
3314 return
3315 New_Copy_Tree_And_Copy_Dimensions
3316 (Expression (Parent (Compon)));
3318 else
3319 if Present (Next (Selector_Name)) then
3320 Expr :=
3321 New_Copy_Tree_And_Copy_Dimensions
3322 (Expression (Assoc));
3323 else
3324 Expr := Expression (Assoc);
3325 end if;
3326 end if;
3328 Generate_Reference (Compon, Selector_Name, 'm');
3330 else
3331 Error_Msg_NE
3332 ("more than one value supplied for &",
3333 Selector_Name, Compon);
3335 end if;
3336 end if;
3338 Next (Selector_Name);
3339 end loop;
3341 Next (Assoc);
3342 end loop;
3344 return Expr;
3345 end Get_Value;
3347 ---------------------------------------
3348 -- New_Copy_Tree_And_Copy_Dimensions --
3349 ---------------------------------------
3351 function New_Copy_Tree_And_Copy_Dimensions
3352 (Source : Node_Id;
3353 Map : Elist_Id := No_Elist;
3354 New_Sloc : Source_Ptr := No_Location;
3355 New_Scope : Entity_Id := Empty) return Node_Id
3357 New_Copy : constant Node_Id :=
3358 New_Copy_Tree (Source, Map, New_Sloc, New_Scope);
3359 begin
3360 -- Move the dimensions of Source to New_Copy
3362 Copy_Dimensions (Source, New_Copy);
3363 return New_Copy;
3364 end New_Copy_Tree_And_Copy_Dimensions;
3366 -----------------------
3367 -- Resolve_Aggr_Expr --
3368 -----------------------
3370 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id) is
3371 Expr_Type : Entity_Id := Empty;
3372 New_C : Entity_Id := Component;
3373 New_Expr : Node_Id;
3375 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
3376 -- If the expression is an aggregate (possibly qualified) then its
3377 -- expansion is delayed until the enclosing aggregate is expanded
3378 -- into assignments. In that case, do not generate checks on the
3379 -- expression, because they will be generated later, and will other-
3380 -- wise force a copy (to remove side-effects) that would leave a
3381 -- dynamic-sized aggregate in the code, something that gigi cannot
3382 -- handle.
3384 Relocate : Boolean;
3385 -- Set to True if the resolved Expr node needs to be relocated when
3386 -- attached to the newly created association list. This node need not
3387 -- be relocated if its parent pointer is not set. In fact in this
3388 -- case Expr is the output of a New_Copy_Tree call. If Relocate is
3389 -- True then we have analyzed the expression node in the original
3390 -- aggregate and hence it needs to be relocated when moved over to
3391 -- the new association list.
3393 ---------------------------
3394 -- Has_Expansion_Delayed --
3395 ---------------------------
3397 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
3398 Kind : constant Node_Kind := Nkind (Expr);
3399 begin
3400 return (Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate)
3401 and then Present (Etype (Expr))
3402 and then Is_Record_Type (Etype (Expr))
3403 and then Expansion_Delayed (Expr))
3404 or else (Kind = N_Qualified_Expression
3405 and then Has_Expansion_Delayed (Expression (Expr)));
3406 end Has_Expansion_Delayed;
3408 -- Start of processing for Resolve_Aggr_Expr
3410 begin
3411 -- If the type of the component is elementary or the type of the
3412 -- aggregate does not contain discriminants, use the type of the
3413 -- component to resolve Expr.
3415 if Is_Elementary_Type (Etype (Component))
3416 or else not Has_Discriminants (Etype (N))
3417 then
3418 Expr_Type := Etype (Component);
3420 -- Otherwise we have to pick up the new type of the component from
3421 -- the new constrained subtype of the aggregate. In fact components
3422 -- which are of a composite type might be constrained by a
3423 -- discriminant, and we want to resolve Expr against the subtype were
3424 -- all discriminant occurrences are replaced with their actual value.
3426 else
3427 New_C := First_Component (Etype (N));
3428 while Present (New_C) loop
3429 if Chars (New_C) = Chars (Component) then
3430 Expr_Type := Etype (New_C);
3431 exit;
3432 end if;
3434 Next_Component (New_C);
3435 end loop;
3437 pragma Assert (Present (Expr_Type));
3439 -- For each range in an array type where a discriminant has been
3440 -- replaced with the constraint, check that this range is within
3441 -- the range of the base type. This checks is done in the init
3442 -- proc for regular objects, but has to be done here for
3443 -- aggregates since no init proc is called for them.
3445 if Is_Array_Type (Expr_Type) then
3446 declare
3447 Index : Node_Id;
3448 -- Range of the current constrained index in the array
3450 Orig_Index : Node_Id := First_Index (Etype (Component));
3451 -- Range corresponding to the range Index above in the
3452 -- original unconstrained record type. The bounds of this
3453 -- range may be governed by discriminants.
3455 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
3456 -- Range corresponding to the range Index above for the
3457 -- unconstrained array type. This range is needed to apply
3458 -- range checks.
3460 begin
3461 Index := First_Index (Expr_Type);
3462 while Present (Index) loop
3463 if Depends_On_Discriminant (Orig_Index) then
3464 Apply_Range_Check (Index, Etype (Unconstr_Index));
3465 end if;
3467 Next_Index (Index);
3468 Next_Index (Orig_Index);
3469 Next_Index (Unconstr_Index);
3470 end loop;
3471 end;
3472 end if;
3473 end if;
3475 -- If the Parent pointer of Expr is not set, Expr is an expression
3476 -- duplicated by New_Tree_Copy (this happens for record aggregates
3477 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
3478 -- Such a duplicated expression must be attached to the tree
3479 -- before analysis and resolution to enforce the rule that a tree
3480 -- fragment should never be analyzed or resolved unless it is
3481 -- attached to the current compilation unit.
3483 if No (Parent (Expr)) then
3484 Set_Parent (Expr, N);
3485 Relocate := False;
3486 else
3487 Relocate := True;
3488 end if;
3490 Analyze_And_Resolve (Expr, Expr_Type);
3491 Check_Expr_OK_In_Limited_Aggregate (Expr);
3492 Check_Non_Static_Context (Expr);
3493 Check_Unset_Reference (Expr);
3495 -- Check wrong use of class-wide types
3497 if Is_Class_Wide_Type (Etype (Expr)) then
3498 Error_Msg_N ("dynamically tagged expression not allowed", Expr);
3499 end if;
3501 if not Has_Expansion_Delayed (Expr) then
3502 Aggregate_Constraint_Checks (Expr, Expr_Type);
3503 end if;
3505 -- If an aggregate component has a type with predicates, an explicit
3506 -- predicate check must be applied, as for an assignment statement,
3507 -- because the aggegate might not be expanded into individual
3508 -- component assignments.
3510 if Present (Predicate_Function (Expr_Type)) then
3511 Apply_Predicate_Check (Expr, Expr_Type);
3512 end if;
3514 if Raises_Constraint_Error (Expr) then
3515 Set_Raises_Constraint_Error (N);
3516 end if;
3518 -- If the expression has been marked as requiring a range check, then
3519 -- generate it here.
3521 if Do_Range_Check (Expr) then
3522 Set_Do_Range_Check (Expr, False);
3523 Generate_Range_Check (Expr, Expr_Type, CE_Range_Check_Failed);
3524 end if;
3526 if Relocate then
3527 New_Expr := Relocate_Node (Expr);
3529 -- Since New_Expr is not gonna be analyzed later on, we need to
3530 -- propagate here the dimensions form Expr to New_Expr.
3532 Copy_Dimensions (Expr, New_Expr);
3534 else
3535 New_Expr := Expr;
3536 end if;
3538 Add_Association (New_C, New_Expr, New_Assoc_List);
3539 end Resolve_Aggr_Expr;
3541 -- Start of processing for Resolve_Record_Aggregate
3543 begin
3544 -- A record aggregate is restricted in SPARK:
3546 -- Each named association can have only a single choice.
3547 -- OTHERS cannot be used.
3548 -- Positional and named associations cannot be mixed.
3550 if Present (Component_Associations (N))
3551 and then Present (First (Component_Associations (N)))
3552 then
3554 if Present (Expressions (N)) then
3555 Check_SPARK_Restriction
3556 ("named association cannot follow positional one",
3557 First (Choices (First (Component_Associations (N)))));
3558 end if;
3560 declare
3561 Assoc : Node_Id;
3563 begin
3564 Assoc := First (Component_Associations (N));
3565 while Present (Assoc) loop
3566 if List_Length (Choices (Assoc)) > 1 then
3567 Check_SPARK_Restriction
3568 ("component association in record aggregate must "
3569 & "contain a single choice", Assoc);
3570 end if;
3572 if Nkind (First (Choices (Assoc))) = N_Others_Choice then
3573 Check_SPARK_Restriction
3574 ("record aggregate cannot contain OTHERS", Assoc);
3575 end if;
3577 Assoc := Next (Assoc);
3578 end loop;
3579 end;
3580 end if;
3582 -- We may end up calling Duplicate_Subexpr on expressions that are
3583 -- attached to New_Assoc_List. For this reason we need to attach it
3584 -- to the tree by setting its parent pointer to N. This parent point
3585 -- will change in STEP 8 below.
3587 Set_Parent (New_Assoc_List, N);
3589 -- STEP 1: abstract type and null record verification
3591 if Is_Abstract_Type (Typ) then
3592 Error_Msg_N ("type of aggregate cannot be abstract", N);
3593 end if;
3595 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
3596 Set_Etype (N, Typ);
3597 return;
3599 elsif Present (First_Entity (Typ))
3600 and then Null_Record_Present (N)
3601 and then not Is_Tagged_Type (Typ)
3602 then
3603 Error_Msg_N ("record aggregate cannot be null", N);
3604 return;
3606 -- If the type has no components, then the aggregate should either
3607 -- have "null record", or in Ada 2005 it could instead have a single
3608 -- component association given by "others => <>". For Ada 95 we flag an
3609 -- error at this point, but for Ada 2005 we proceed with checking the
3610 -- associations below, which will catch the case where it's not an
3611 -- aggregate with "others => <>". Note that the legality of a <>
3612 -- aggregate for a null record type was established by AI05-016.
3614 elsif No (First_Entity (Typ))
3615 and then Ada_Version < Ada_2005
3616 then
3617 Error_Msg_N ("record aggregate must be null", N);
3618 return;
3619 end if;
3621 -- STEP 2: Verify aggregate structure
3623 Step_2 : declare
3624 Selector_Name : Node_Id;
3625 Bad_Aggregate : Boolean := False;
3627 begin
3628 if Present (Component_Associations (N)) then
3629 Assoc := First (Component_Associations (N));
3630 else
3631 Assoc := Empty;
3632 end if;
3634 while Present (Assoc) loop
3635 Selector_Name := First (Choices (Assoc));
3636 while Present (Selector_Name) loop
3637 if Nkind (Selector_Name) = N_Identifier then
3638 null;
3640 elsif Nkind (Selector_Name) = N_Others_Choice then
3641 if Selector_Name /= First (Choices (Assoc))
3642 or else Present (Next (Selector_Name))
3643 then
3644 Error_Msg_N
3645 ("OTHERS must appear alone in a choice list",
3646 Selector_Name);
3647 return;
3649 elsif Present (Next (Assoc)) then
3650 Error_Msg_N
3651 ("OTHERS must appear last in an aggregate",
3652 Selector_Name);
3653 return;
3655 -- (Ada 2005): If this is an association with a box,
3656 -- indicate that the association need not represent
3657 -- any component.
3659 elsif Box_Present (Assoc) then
3660 Others_Box := True;
3661 end if;
3663 else
3664 Error_Msg_N
3665 ("selector name should be identifier or OTHERS",
3666 Selector_Name);
3667 Bad_Aggregate := True;
3668 end if;
3670 Next (Selector_Name);
3671 end loop;
3673 Next (Assoc);
3674 end loop;
3676 if Bad_Aggregate then
3677 return;
3678 end if;
3679 end Step_2;
3681 -- STEP 3: Find discriminant Values
3683 Step_3 : declare
3684 Discrim : Entity_Id;
3685 Missing_Discriminants : Boolean := False;
3687 begin
3688 if Present (Expressions (N)) then
3689 Positional_Expr := First (Expressions (N));
3690 else
3691 Positional_Expr := Empty;
3692 end if;
3694 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
3695 -- must not have unknown discriminants.
3697 if Is_Derived_Type (Typ)
3698 and then Has_Unknown_Discriminants (Root_Type (Typ))
3699 and then Nkind (N) /= N_Extension_Aggregate
3700 then
3701 Error_Msg_NE
3702 ("aggregate not available for type& whose ancestor "
3703 & "has unknown discriminants ", N, Typ);
3704 end if;
3706 if Has_Unknown_Discriminants (Typ)
3707 and then Present (Underlying_Record_View (Typ))
3708 then
3709 Discrim := First_Discriminant (Underlying_Record_View (Typ));
3710 elsif Has_Discriminants (Typ) then
3711 Discrim := First_Discriminant (Typ);
3712 else
3713 Discrim := Empty;
3714 end if;
3716 -- First find the discriminant values in the positional components
3718 while Present (Discrim) and then Present (Positional_Expr) loop
3719 if Discr_Present (Discrim) then
3720 Resolve_Aggr_Expr (Positional_Expr, Discrim);
3722 -- Ada 2005 (AI-231)
3724 if Ada_Version >= Ada_2005
3725 and then Known_Null (Positional_Expr)
3726 then
3727 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
3728 end if;
3730 Next (Positional_Expr);
3731 end if;
3733 if Present (Get_Value (Discrim, Component_Associations (N))) then
3734 Error_Msg_NE
3735 ("more than one value supplied for discriminant&",
3736 N, Discrim);
3737 end if;
3739 Next_Discriminant (Discrim);
3740 end loop;
3742 -- Find remaining discriminant values if any among named components
3744 while Present (Discrim) loop
3745 Expr := Get_Value (Discrim, Component_Associations (N), True);
3747 if not Discr_Present (Discrim) then
3748 if Present (Expr) then
3749 Error_Msg_NE
3750 ("more than one value supplied for discriminant&",
3751 N, Discrim);
3752 end if;
3754 elsif No (Expr) then
3755 Error_Msg_NE
3756 ("no value supplied for discriminant &", N, Discrim);
3757 Missing_Discriminants := True;
3759 else
3760 Resolve_Aggr_Expr (Expr, Discrim);
3761 end if;
3763 Next_Discriminant (Discrim);
3764 end loop;
3766 if Missing_Discriminants then
3767 return;
3768 end if;
3770 -- At this point and until the beginning of STEP 6, New_Assoc_List
3771 -- contains only the discriminants and their values.
3773 end Step_3;
3775 -- STEP 4: Set the Etype of the record aggregate
3777 -- ??? This code is pretty much a copy of Sem_Ch3.Build_Subtype. That
3778 -- routine should really be exported in sem_util or some such and used
3779 -- in sem_ch3 and here rather than have a copy of the code which is a
3780 -- maintenance nightmare.
3782 -- ??? Performance WARNING. The current implementation creates a new
3783 -- itype for all aggregates whose base type is discriminated. This means
3784 -- that for record aggregates nested inside an array aggregate we will
3785 -- create a new itype for each record aggregate if the array component
3786 -- type has discriminants. For large aggregates this may be a problem.
3787 -- What should be done in this case is to reuse itypes as much as
3788 -- possible.
3790 if Has_Discriminants (Typ)
3791 or else (Has_Unknown_Discriminants (Typ)
3792 and then Present (Underlying_Record_View (Typ)))
3793 then
3794 Build_Constrained_Itype : declare
3795 Loc : constant Source_Ptr := Sloc (N);
3796 Indic : Node_Id;
3797 Subtyp_Decl : Node_Id;
3798 Def_Id : Entity_Id;
3800 C : constant List_Id := New_List;
3802 begin
3803 New_Assoc := First (New_Assoc_List);
3804 while Present (New_Assoc) loop
3805 Append (Duplicate_Subexpr (Expression (New_Assoc)), To => C);
3806 Next (New_Assoc);
3807 end loop;
3809 if Has_Unknown_Discriminants (Typ)
3810 and then Present (Underlying_Record_View (Typ))
3811 then
3812 Indic :=
3813 Make_Subtype_Indication (Loc,
3814 Subtype_Mark =>
3815 New_Occurrence_Of (Underlying_Record_View (Typ), Loc),
3816 Constraint =>
3817 Make_Index_Or_Discriminant_Constraint (Loc, C));
3818 else
3819 Indic :=
3820 Make_Subtype_Indication (Loc,
3821 Subtype_Mark =>
3822 New_Occurrence_Of (Base_Type (Typ), Loc),
3823 Constraint =>
3824 Make_Index_Or_Discriminant_Constraint (Loc, C));
3825 end if;
3827 Def_Id := Create_Itype (Ekind (Typ), N);
3829 Subtyp_Decl :=
3830 Make_Subtype_Declaration (Loc,
3831 Defining_Identifier => Def_Id,
3832 Subtype_Indication => Indic);
3833 Set_Parent (Subtyp_Decl, Parent (N));
3835 -- Itypes must be analyzed with checks off (see itypes.ads)
3837 Analyze (Subtyp_Decl, Suppress => All_Checks);
3839 Set_Etype (N, Def_Id);
3840 Check_Static_Discriminated_Subtype
3841 (Def_Id, Expression (First (New_Assoc_List)));
3842 end Build_Constrained_Itype;
3844 else
3845 Set_Etype (N, Typ);
3846 end if;
3848 -- STEP 5: Get remaining components according to discriminant values
3850 Step_5 : declare
3851 Record_Def : Node_Id;
3852 Parent_Typ : Entity_Id;
3853 Root_Typ : Entity_Id;
3854 Parent_Typ_List : Elist_Id;
3855 Parent_Elmt : Elmt_Id;
3856 Errors_Found : Boolean := False;
3857 Dnode : Node_Id;
3859 function Find_Private_Ancestor return Entity_Id;
3860 -- AI05-0115: Find earlier ancestor in the derivation chain that is
3861 -- derived from a private view. Whether the aggregate is legal
3862 -- depends on the current visibility of the type as well as that
3863 -- of the parent of the ancestor.
3865 ---------------------------
3866 -- Find_Private_Ancestor --
3867 ---------------------------
3869 function Find_Private_Ancestor return Entity_Id is
3870 Par : Entity_Id;
3871 begin
3872 Par := Typ;
3873 loop
3874 if Has_Private_Ancestor (Par)
3875 and then not Has_Private_Ancestor (Etype (Base_Type (Par)))
3876 then
3877 return Par;
3879 elsif not Is_Derived_Type (Par) then
3880 return Empty;
3882 else
3883 Par := Etype (Base_Type (Par));
3884 end if;
3885 end loop;
3886 end Find_Private_Ancestor;
3888 -- Start of processing for Step_5
3890 begin
3891 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
3892 Parent_Typ_List := New_Elmt_List;
3894 -- If this is an extension aggregate, the component list must
3895 -- include all components that are not in the given ancestor type.
3896 -- Otherwise, the component list must include components of all
3897 -- ancestors, starting with the root.
3899 if Nkind (N) = N_Extension_Aggregate then
3900 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
3902 else
3903 -- AI05-0115: check legality of aggregate for type with
3904 -- aa private ancestor.
3906 Root_Typ := Root_Type (Typ);
3907 if Has_Private_Ancestor (Typ) then
3908 declare
3909 Ancestor : constant Entity_Id :=
3910 Find_Private_Ancestor;
3911 Ancestor_Unit : constant Entity_Id :=
3912 Cunit_Entity (Get_Source_Unit (Ancestor));
3913 Parent_Unit : constant Entity_Id :=
3914 Cunit_Entity
3915 (Get_Source_Unit (Base_Type (Etype (Ancestor))));
3916 begin
3918 -- check whether we are in a scope that has full view
3919 -- over the private ancestor and its parent. This can
3920 -- only happen if the derivation takes place in a child
3921 -- unit of the unit that declares the parent, and we are
3922 -- in the private part or body of that child unit, else
3923 -- the aggregate is illegal.
3925 if Is_Child_Unit (Ancestor_Unit)
3926 and then Scope (Ancestor_Unit) = Parent_Unit
3927 and then In_Open_Scopes (Scope (Ancestor))
3928 and then
3929 (In_Private_Part (Scope (Ancestor))
3930 or else In_Package_Body (Scope (Ancestor)))
3931 then
3932 null;
3934 else
3935 Error_Msg_NE
3936 ("type of aggregate has private ancestor&!",
3937 N, Root_Typ);
3938 Error_Msg_N ("must use extension aggregate!", N);
3939 return;
3940 end if;
3941 end;
3942 end if;
3944 Dnode := Declaration_Node (Base_Type (Root_Typ));
3946 -- If we don't get a full declaration, then we have some error
3947 -- which will get signalled later so skip this part. Otherwise
3948 -- gather components of root that apply to the aggregate type.
3949 -- We use the base type in case there is an applicable stored
3950 -- constraint that renames the discriminants of the root.
3952 if Nkind (Dnode) = N_Full_Type_Declaration then
3953 Record_Def := Type_Definition (Dnode);
3954 Gather_Components
3955 (Base_Type (Typ),
3956 Component_List (Record_Def),
3957 Governed_By => New_Assoc_List,
3958 Into => Components,
3959 Report_Errors => Errors_Found);
3960 end if;
3961 end if;
3963 Parent_Typ := Base_Type (Typ);
3964 while Parent_Typ /= Root_Typ loop
3965 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
3966 Parent_Typ := Etype (Parent_Typ);
3968 if Nkind (Parent (Base_Type (Parent_Typ))) =
3969 N_Private_Type_Declaration
3970 or else Nkind (Parent (Base_Type (Parent_Typ))) =
3971 N_Private_Extension_Declaration
3972 then
3973 if Nkind (N) /= N_Extension_Aggregate then
3974 Error_Msg_NE
3975 ("type of aggregate has private ancestor&!",
3976 N, Parent_Typ);
3977 Error_Msg_N ("must use extension aggregate!", N);
3978 return;
3980 elsif Parent_Typ /= Root_Typ then
3981 Error_Msg_NE
3982 ("ancestor part of aggregate must be private type&",
3983 Ancestor_Part (N), Parent_Typ);
3984 return;
3985 end if;
3987 -- The current view of ancestor part may be a private type,
3988 -- while the context type is always non-private.
3990 elsif Is_Private_Type (Root_Typ)
3991 and then Present (Full_View (Root_Typ))
3992 and then Nkind (N) = N_Extension_Aggregate
3993 then
3994 exit when Base_Type (Full_View (Root_Typ)) = Parent_Typ;
3995 end if;
3996 end loop;
3998 -- Now collect components from all other ancestors, beginning
3999 -- with the current type. If the type has unknown discriminants
4000 -- use the component list of the Underlying_Record_View, which
4001 -- needs to be used for the subsequent expansion of the aggregate
4002 -- into assignments.
4004 Parent_Elmt := First_Elmt (Parent_Typ_List);
4005 while Present (Parent_Elmt) loop
4006 Parent_Typ := Node (Parent_Elmt);
4008 if Has_Unknown_Discriminants (Parent_Typ)
4009 and then Present (Underlying_Record_View (Typ))
4010 then
4011 Parent_Typ := Underlying_Record_View (Parent_Typ);
4012 end if;
4014 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
4015 Gather_Components (Empty,
4016 Component_List (Record_Extension_Part (Record_Def)),
4017 Governed_By => New_Assoc_List,
4018 Into => Components,
4019 Report_Errors => Errors_Found);
4021 Next_Elmt (Parent_Elmt);
4022 end loop;
4024 -- Typ is not a derived tagged type
4026 else
4027 -- A type derived from an untagged private type whose full view
4028 -- has discriminants is constructed as a record type but there
4029 -- are no legal aggregates for it.
4031 if Is_Derived_Type (Typ)
4032 and then Has_Private_Ancestor (Typ)
4033 and then Nkind (N) /= N_Extension_Aggregate
4034 then
4035 Error_Msg_Node_2 := Base_Type (Etype (Typ));
4036 Error_Msg_NE
4037 ("no aggregate available for type& derived from "
4038 & "private type&", N, Typ);
4039 return;
4040 end if;
4042 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
4044 if Null_Present (Record_Def) then
4045 null;
4047 elsif not Has_Unknown_Discriminants (Typ) then
4048 Gather_Components
4049 (Base_Type (Typ),
4050 Component_List (Record_Def),
4051 Governed_By => New_Assoc_List,
4052 Into => Components,
4053 Report_Errors => Errors_Found);
4055 else
4056 Gather_Components
4057 (Base_Type (Underlying_Record_View (Typ)),
4058 Component_List (Record_Def),
4059 Governed_By => New_Assoc_List,
4060 Into => Components,
4061 Report_Errors => Errors_Found);
4062 end if;
4063 end if;
4065 if Errors_Found then
4066 return;
4067 end if;
4068 end Step_5;
4070 -- STEP 6: Find component Values
4072 Component := Empty;
4073 Component_Elmt := First_Elmt (Components);
4075 -- First scan the remaining positional associations in the aggregate.
4076 -- Remember that at this point Positional_Expr contains the current
4077 -- positional association if any is left after looking for discriminant
4078 -- values in step 3.
4080 while Present (Positional_Expr) and then Present (Component_Elmt) loop
4081 Component := Node (Component_Elmt);
4082 Resolve_Aggr_Expr (Positional_Expr, Component);
4084 -- Ada 2005 (AI-231)
4086 if Ada_Version >= Ada_2005
4087 and then Known_Null (Positional_Expr)
4088 then
4089 Check_Can_Never_Be_Null (Component, Positional_Expr);
4090 end if;
4092 if Present (Get_Value (Component, Component_Associations (N))) then
4093 Error_Msg_NE
4094 ("more than one value supplied for Component &", N, Component);
4095 end if;
4097 Next (Positional_Expr);
4098 Next_Elmt (Component_Elmt);
4099 end loop;
4101 if Present (Positional_Expr) then
4102 Error_Msg_N
4103 ("too many components for record aggregate", Positional_Expr);
4104 end if;
4106 -- Now scan for the named arguments of the aggregate
4108 while Present (Component_Elmt) loop
4109 Component := Node (Component_Elmt);
4110 Expr := Get_Value (Component, Component_Associations (N), True);
4112 -- Note: The previous call to Get_Value sets the value of the
4113 -- variable Is_Box_Present.
4115 -- Ada 2005 (AI-287): Handle components with default initialization.
4116 -- Note: This feature was originally added to Ada 2005 for limited
4117 -- but it was finally allowed with any type.
4119 if Is_Box_Present then
4120 Check_Box_Component : declare
4121 Ctyp : constant Entity_Id := Etype (Component);
4123 begin
4124 -- If there is a default expression for the aggregate, copy
4125 -- it into a new association. This copy must modify the scopes
4126 -- of internal types that may be attached to the expression
4127 -- (e.g. index subtypes of arrays) because in general the type
4128 -- declaration and the aggregate appear in different scopes,
4129 -- and the backend requires the scope of the type to match the
4130 -- point at which it is elaborated.
4132 -- If the component has an initialization procedure (IP) we
4133 -- pass the component to the expander, which will generate
4134 -- the call to such IP.
4136 -- If the component has discriminants, their values must
4137 -- be taken from their subtype. This is indispensable for
4138 -- constraints that are given by the current instance of an
4139 -- enclosing type, to allow the expansion of the aggregate to
4140 -- replace the reference to the current instance by the target
4141 -- object of the aggregate.
4143 if Present (Parent (Component))
4144 and then
4145 Nkind (Parent (Component)) = N_Component_Declaration
4146 and then Present (Expression (Parent (Component)))
4147 then
4148 Expr :=
4149 New_Copy_Tree_And_Copy_Dimensions
4150 (Expression (Parent (Component)),
4151 New_Scope => Current_Scope,
4152 New_Sloc => Sloc (N));
4154 Add_Association
4155 (Component => Component,
4156 Expr => Expr,
4157 Assoc_List => New_Assoc_List);
4158 Set_Has_Self_Reference (N);
4160 -- A box-defaulted access component gets the value null. Also
4161 -- included are components of private types whose underlying
4162 -- type is an access type. In either case set the type of the
4163 -- literal, for subsequent use in semantic checks.
4165 elsif Present (Underlying_Type (Ctyp))
4166 and then Is_Access_Type (Underlying_Type (Ctyp))
4167 then
4168 if not Is_Private_Type (Ctyp) then
4169 Expr := Make_Null (Sloc (N));
4170 Set_Etype (Expr, Ctyp);
4171 Add_Association
4172 (Component => Component,
4173 Expr => Expr,
4174 Assoc_List => New_Assoc_List);
4176 -- If the component's type is private with an access type as
4177 -- its underlying type then we have to create an unchecked
4178 -- conversion to satisfy type checking.
4180 else
4181 declare
4182 Qual_Null : constant Node_Id :=
4183 Make_Qualified_Expression (Sloc (N),
4184 Subtype_Mark =>
4185 New_Occurrence_Of
4186 (Underlying_Type (Ctyp), Sloc (N)),
4187 Expression => Make_Null (Sloc (N)));
4189 Convert_Null : constant Node_Id :=
4190 Unchecked_Convert_To
4191 (Ctyp, Qual_Null);
4193 begin
4194 Analyze_And_Resolve (Convert_Null, Ctyp);
4195 Add_Association
4196 (Component => Component,
4197 Expr => Convert_Null,
4198 Assoc_List => New_Assoc_List);
4199 end;
4200 end if;
4202 elsif Has_Non_Null_Base_Init_Proc (Ctyp)
4203 or else not Expander_Active
4204 then
4205 if Is_Record_Type (Ctyp)
4206 and then Has_Discriminants (Ctyp)
4207 and then not Is_Private_Type (Ctyp)
4208 then
4209 -- We build a partially initialized aggregate with the
4210 -- values of the discriminants and box initialization
4211 -- for the rest, if other components are present.
4213 -- The type of the aggregate is the known subtype of
4214 -- the component. The capture of discriminants must
4215 -- be recursive because subcomponents may be constrained
4216 -- (transitively) by discriminants of enclosing types.
4217 -- For a private type with discriminants, a call to the
4218 -- initialization procedure will be generated, and no
4219 -- subaggregate is needed.
4221 Capture_Discriminants : declare
4222 Loc : constant Source_Ptr := Sloc (N);
4223 Expr : Node_Id;
4225 procedure Add_Discriminant_Values
4226 (New_Aggr : Node_Id;
4227 Assoc_List : List_Id);
4228 -- The constraint to a component may be given by a
4229 -- discriminant of the enclosing type, in which case
4230 -- we have to retrieve its value, which is part of the
4231 -- enclosing aggregate. Assoc_List provides the
4232 -- discriminant associations of the current type or
4233 -- of some enclosing record.
4235 procedure Propagate_Discriminants
4236 (Aggr : Node_Id;
4237 Assoc_List : List_Id);
4238 -- Nested components may themselves be discriminated
4239 -- types constrained by outer discriminants, whose
4240 -- values must be captured before the aggregate is
4241 -- expanded into assignments.
4243 -----------------------------
4244 -- Add_Discriminant_Values --
4245 -----------------------------
4247 procedure Add_Discriminant_Values
4248 (New_Aggr : Node_Id;
4249 Assoc_List : List_Id)
4251 Assoc : Node_Id;
4252 Discr : Entity_Id;
4253 Discr_Elmt : Elmt_Id;
4254 Discr_Val : Node_Id;
4255 Val : Entity_Id;
4257 begin
4258 Discr := First_Discriminant (Etype (New_Aggr));
4259 Discr_Elmt :=
4260 First_Elmt
4261 (Discriminant_Constraint (Etype (New_Aggr)));
4262 while Present (Discr_Elmt) loop
4263 Discr_Val := Node (Discr_Elmt);
4265 -- If the constraint is given by a discriminant
4266 -- it is a discriminant of an enclosing record,
4267 -- and its value has already been placed in the
4268 -- association list.
4270 if Is_Entity_Name (Discr_Val)
4271 and then
4272 Ekind (Entity (Discr_Val)) = E_Discriminant
4273 then
4274 Val := Entity (Discr_Val);
4276 Assoc := First (Assoc_List);
4277 while Present (Assoc) loop
4278 if Present
4279 (Entity (First (Choices (Assoc))))
4280 and then
4281 Entity (First (Choices (Assoc)))
4282 = Val
4283 then
4284 Discr_Val := Expression (Assoc);
4285 exit;
4286 end if;
4287 Next (Assoc);
4288 end loop;
4289 end if;
4291 Add_Association
4292 (Discr, New_Copy_Tree (Discr_Val),
4293 Component_Associations (New_Aggr));
4295 -- If the discriminant constraint is a current
4296 -- instance, mark the current aggregate so that
4297 -- the self-reference can be expanded later.
4299 if Nkind (Discr_Val) = N_Attribute_Reference
4300 and then Is_Entity_Name (Prefix (Discr_Val))
4301 and then Is_Type (Entity (Prefix (Discr_Val)))
4302 and then Etype (N) =
4303 Entity (Prefix (Discr_Val))
4304 then
4305 Set_Has_Self_Reference (N);
4306 end if;
4308 Next_Elmt (Discr_Elmt);
4309 Next_Discriminant (Discr);
4310 end loop;
4311 end Add_Discriminant_Values;
4313 ------------------------------
4314 -- Propagate_Discriminants --
4315 ------------------------------
4317 procedure Propagate_Discriminants
4318 (Aggr : Node_Id;
4319 Assoc_List : List_Id)
4321 Aggr_Type : constant Entity_Id :=
4322 Base_Type (Etype (Aggr));
4323 Def_Node : constant Node_Id :=
4324 Type_Definition
4325 (Declaration_Node (Aggr_Type));
4327 Comp : Node_Id;
4328 Comp_Elmt : Elmt_Id;
4329 Components : constant Elist_Id := New_Elmt_List;
4330 Needs_Box : Boolean := False;
4331 Errors : Boolean;
4333 procedure Process_Component (Comp : Entity_Id);
4334 -- Add one component with a box association to the
4335 -- inner aggregate, and recurse if component is
4336 -- itself composite.
4338 ------------------------
4339 -- Process_Component --
4340 ------------------------
4342 procedure Process_Component (Comp : Entity_Id) is
4343 T : constant Entity_Id := Etype (Comp);
4344 New_Aggr : Node_Id;
4346 begin
4347 if Is_Record_Type (T)
4348 and then Has_Discriminants (T)
4349 then
4350 New_Aggr :=
4351 Make_Aggregate (Loc, New_List, New_List);
4352 Set_Etype (New_Aggr, T);
4353 Add_Association
4354 (Comp, New_Aggr,
4355 Component_Associations (Aggr));
4357 -- Collect discriminant values and recurse
4359 Add_Discriminant_Values
4360 (New_Aggr, Assoc_List);
4361 Propagate_Discriminants
4362 (New_Aggr, Assoc_List);
4364 else
4365 Needs_Box := True;
4366 end if;
4367 end Process_Component;
4369 -- Start of processing for Propagate_Discriminants
4371 begin
4372 -- The component type may be a variant type, so
4373 -- collect the components that are ruled by the
4374 -- known values of the discriminants. Their values
4375 -- have already been inserted into the component
4376 -- list of the current aggregate.
4378 if Nkind (Def_Node) = N_Record_Definition
4379 and then
4380 Present (Component_List (Def_Node))
4381 and then
4382 Present
4383 (Variant_Part (Component_List (Def_Node)))
4384 then
4385 Gather_Components (Aggr_Type,
4386 Component_List (Def_Node),
4387 Governed_By => Component_Associations (Aggr),
4388 Into => Components,
4389 Report_Errors => Errors);
4391 Comp_Elmt := First_Elmt (Components);
4392 while Present (Comp_Elmt) loop
4394 Ekind (Node (Comp_Elmt)) /= E_Discriminant
4395 then
4396 Process_Component (Node (Comp_Elmt));
4397 end if;
4399 Next_Elmt (Comp_Elmt);
4400 end loop;
4402 -- No variant part, iterate over all components
4404 else
4405 Comp := First_Component (Etype (Aggr));
4406 while Present (Comp) loop
4407 Process_Component (Comp);
4408 Next_Component (Comp);
4409 end loop;
4410 end if;
4412 if Needs_Box then
4413 Append
4414 (Make_Component_Association (Loc,
4415 Choices =>
4416 New_List (Make_Others_Choice (Loc)),
4417 Expression => Empty,
4418 Box_Present => True),
4419 Component_Associations (Aggr));
4420 end if;
4421 end Propagate_Discriminants;
4423 -- Start of processing for Capture_Discriminants
4425 begin
4426 Expr := Make_Aggregate (Loc, New_List, New_List);
4427 Set_Etype (Expr, Ctyp);
4429 -- If the enclosing type has discriminants, they have
4430 -- been collected in the aggregate earlier, and they
4431 -- may appear as constraints of subcomponents.
4433 -- Similarly if this component has discriminants, they
4434 -- might in turn be propagated to their components.
4436 if Has_Discriminants (Typ) then
4437 Add_Discriminant_Values (Expr, New_Assoc_List);
4438 Propagate_Discriminants (Expr, New_Assoc_List);
4440 elsif Has_Discriminants (Ctyp) then
4441 Add_Discriminant_Values
4442 (Expr, Component_Associations (Expr));
4443 Propagate_Discriminants
4444 (Expr, Component_Associations (Expr));
4446 else
4447 declare
4448 Comp : Entity_Id;
4450 begin
4451 -- If the type has additional components, create
4452 -- an OTHERS box association for them.
4454 Comp := First_Component (Ctyp);
4455 while Present (Comp) loop
4456 if Ekind (Comp) = E_Component then
4457 if not Is_Record_Type (Etype (Comp)) then
4458 Append
4459 (Make_Component_Association (Loc,
4460 Choices =>
4461 New_List
4462 (Make_Others_Choice (Loc)),
4463 Expression => Empty,
4464 Box_Present => True),
4465 Component_Associations (Expr));
4466 end if;
4467 exit;
4468 end if;
4470 Next_Component (Comp);
4471 end loop;
4472 end;
4473 end if;
4475 Add_Association
4476 (Component => Component,
4477 Expr => Expr,
4478 Assoc_List => New_Assoc_List);
4479 end Capture_Discriminants;
4481 else
4482 Add_Association
4483 (Component => Component,
4484 Expr => Empty,
4485 Assoc_List => New_Assoc_List,
4486 Is_Box_Present => True);
4487 end if;
4489 -- Otherwise we only need to resolve the expression if the
4490 -- component has partially initialized values (required to
4491 -- expand the corresponding assignments and run-time checks).
4493 elsif Present (Expr)
4494 and then Is_Partially_Initialized_Type (Ctyp)
4495 then
4496 Resolve_Aggr_Expr (Expr, Component);
4497 end if;
4498 end Check_Box_Component;
4500 elsif No (Expr) then
4502 -- Ignore hidden components associated with the position of the
4503 -- interface tags: these are initialized dynamically.
4505 if not Present (Related_Type (Component)) then
4506 Error_Msg_NE
4507 ("no value supplied for component &!", N, Component);
4508 end if;
4510 else
4511 Resolve_Aggr_Expr (Expr, Component);
4512 end if;
4514 Next_Elmt (Component_Elmt);
4515 end loop;
4517 -- STEP 7: check for invalid components + check type in choice list
4519 Step_7 : declare
4520 Selectr : Node_Id;
4521 -- Selector name
4523 Typech : Entity_Id;
4524 -- Type of first component in choice list
4526 begin
4527 if Present (Component_Associations (N)) then
4528 Assoc := First (Component_Associations (N));
4529 else
4530 Assoc := Empty;
4531 end if;
4533 Verification : while Present (Assoc) loop
4534 Selectr := First (Choices (Assoc));
4535 Typech := Empty;
4537 if Nkind (Selectr) = N_Others_Choice then
4539 -- Ada 2005 (AI-287): others choice may have expression or box
4541 if No (Others_Etype)
4542 and then not Others_Box
4543 then
4544 Error_Msg_N
4545 ("OTHERS must represent at least one component", Selectr);
4546 end if;
4548 exit Verification;
4549 end if;
4551 while Present (Selectr) loop
4552 New_Assoc := First (New_Assoc_List);
4553 while Present (New_Assoc) loop
4554 Component := First (Choices (New_Assoc));
4556 if Chars (Selectr) = Chars (Component) then
4557 if Style_Check then
4558 Check_Identifier (Selectr, Entity (Component));
4559 end if;
4561 exit;
4562 end if;
4564 Next (New_Assoc);
4565 end loop;
4567 -- If no association, this is not a legal component of the type
4568 -- in question, unless its association is provided with a box.
4570 if No (New_Assoc) then
4571 if Box_Present (Parent (Selectr)) then
4573 -- This may still be a bogus component with a box. Scan
4574 -- list of components to verify that a component with
4575 -- that name exists.
4577 declare
4578 C : Entity_Id;
4580 begin
4581 C := First_Component (Typ);
4582 while Present (C) loop
4583 if Chars (C) = Chars (Selectr) then
4585 -- If the context is an extension aggregate,
4586 -- the component must not be inherited from
4587 -- the ancestor part of the aggregate.
4589 if Nkind (N) /= N_Extension_Aggregate
4590 or else
4591 Scope (Original_Record_Component (C)) /=
4592 Etype (Ancestor_Part (N))
4593 then
4594 exit;
4595 end if;
4596 end if;
4598 Next_Component (C);
4599 end loop;
4601 if No (C) then
4602 Error_Msg_Node_2 := Typ;
4603 Error_Msg_N ("& is not a component of}", Selectr);
4604 end if;
4605 end;
4607 elsif Chars (Selectr) /= Name_uTag
4608 and then Chars (Selectr) /= Name_uParent
4609 then
4610 if not Has_Discriminants (Typ) then
4611 Error_Msg_Node_2 := Typ;
4612 Error_Msg_N ("& is not a component of}", Selectr);
4613 else
4614 Error_Msg_N
4615 ("& is not a component of the aggregate subtype",
4616 Selectr);
4617 end if;
4619 Check_Misspelled_Component (Components, Selectr);
4620 end if;
4622 elsif No (Typech) then
4623 Typech := Base_Type (Etype (Component));
4625 -- AI05-0199: In Ada 2012, several components of anonymous
4626 -- access types can appear in a choice list, as long as the
4627 -- designated types match.
4629 elsif Typech /= Base_Type (Etype (Component)) then
4630 if Ada_Version >= Ada_2012
4631 and then Ekind (Typech) = E_Anonymous_Access_Type
4632 and then
4633 Ekind (Etype (Component)) = E_Anonymous_Access_Type
4634 and then Base_Type (Designated_Type (Typech)) =
4635 Base_Type (Designated_Type (Etype (Component)))
4636 and then
4637 Subtypes_Statically_Match (Typech, (Etype (Component)))
4638 then
4639 null;
4641 elsif not Box_Present (Parent (Selectr)) then
4642 Error_Msg_N
4643 ("components in choice list must have same type",
4644 Selectr);
4645 end if;
4646 end if;
4648 Next (Selectr);
4649 end loop;
4651 Next (Assoc);
4652 end loop Verification;
4653 end Step_7;
4655 -- STEP 8: replace the original aggregate
4657 Step_8 : declare
4658 New_Aggregate : constant Node_Id := New_Copy (N);
4660 begin
4661 Set_Expressions (New_Aggregate, No_List);
4662 Set_Etype (New_Aggregate, Etype (N));
4663 Set_Component_Associations (New_Aggregate, New_Assoc_List);
4665 Rewrite (N, New_Aggregate);
4666 end Step_8;
4668 -- Check the dimensions of the components in the record aggregate
4670 Analyze_Dimension_Extension_Or_Record_Aggregate (N);
4671 end Resolve_Record_Aggregate;
4673 -----------------------------
4674 -- Check_Can_Never_Be_Null --
4675 -----------------------------
4677 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
4678 Comp_Typ : Entity_Id;
4680 begin
4681 pragma Assert
4682 (Ada_Version >= Ada_2005
4683 and then Present (Expr)
4684 and then Known_Null (Expr));
4686 case Ekind (Typ) is
4687 when E_Array_Type =>
4688 Comp_Typ := Component_Type (Typ);
4690 when E_Component |
4691 E_Discriminant =>
4692 Comp_Typ := Etype (Typ);
4694 when others =>
4695 return;
4696 end case;
4698 if Can_Never_Be_Null (Comp_Typ) then
4700 -- Here we know we have a constraint error. Note that we do not use
4701 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
4702 -- seem the more natural approach. That's because in some cases the
4703 -- components are rewritten, and the replacement would be missed.
4705 Insert_Action
4706 (Compile_Time_Constraint_Error
4707 (Expr,
4708 "(Ada 2005) null not allowed in null-excluding component??"),
4709 Make_Raise_Constraint_Error
4710 (Sloc (Expr), Reason => CE_Access_Check_Failed));
4712 -- Set proper type for bogus component (why is this needed???)
4714 Set_Etype (Expr, Comp_Typ);
4715 Set_Analyzed (Expr);
4716 end if;
4717 end Check_Can_Never_Be_Null;
4719 ---------------------
4720 -- Sort_Case_Table --
4721 ---------------------
4723 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
4724 U : constant Int := Case_Table'Last;
4725 K : Int;
4726 J : Int;
4727 T : Case_Bounds;
4729 begin
4730 K := 1;
4731 while K < U loop
4732 T := Case_Table (K + 1);
4734 J := K + 1;
4735 while J > 1
4736 and then Expr_Value (Case_Table (J - 1).Lo) > Expr_Value (T.Lo)
4737 loop
4738 Case_Table (J) := Case_Table (J - 1);
4739 J := J - 1;
4740 end loop;
4742 Case_Table (J) := T;
4743 K := K + 1;
4744 end loop;
4745 end Sort_Case_Table;
4747 end Sem_Aggr;