Implement -mmemcpy-strategy= and -mmemset-strategy= options
[official-gcc.git] / gcc / ada / sem_aggr.adb
blob9d7d7b7e4b1e1fde770a04e3551462c530f8c6cd
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 Choice_Lo : Node_Id;
69 Choice_Hi : Node_Id;
70 Choice_Node : Node_Id;
71 end record;
73 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
74 -- Table type used by Check_Case_Choices procedure
76 -----------------------
77 -- Local Subprograms --
78 -----------------------
80 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
81 -- Sort the Case Table using the Lower Bound of each Choice as the key.
82 -- A simple insertion sort is used since the number of choices in a case
83 -- statement of variant part will usually be small and probably in near
84 -- sorted order.
86 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id);
87 -- Ada 2005 (AI-231): Check bad usage of null for a component for which
88 -- null exclusion (NOT NULL) is specified. Typ can be an E_Array_Type for
89 -- the array case (the component type of the array will be used) or an
90 -- E_Component/E_Discriminant entity in the record case, in which case the
91 -- type of the component will be used for the test. If Typ is any other
92 -- kind of entity, the call is ignored. Expr is the component node in the
93 -- aggregate which is known to have a null value. A warning message will be
94 -- issued if the component is null excluding.
96 -- It would be better to pass the proper type for Typ ???
98 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id);
99 -- Check that Expr is either not limited or else is one of the cases of
100 -- expressions allowed for a limited component association (namely, an
101 -- aggregate, function call, or <> notation). Report error for violations.
103 procedure Check_Qualified_Aggregate (Level : Nat; Expr : Node_Id);
104 -- Given aggregate Expr, check that sub-aggregates of Expr that are nested
105 -- at Level are qualified. If Level = 0, this applies to Expr directly.
106 -- Only issue errors in formal verification mode.
108 function Is_Top_Level_Aggregate (Expr : Node_Id) return Boolean;
109 -- Return True of Expr is an aggregate not contained directly in another
110 -- aggregate.
112 ------------------------------------------------------
113 -- Subprograms used for RECORD AGGREGATE Processing --
114 ------------------------------------------------------
116 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
117 -- This procedure performs all the semantic checks required for record
118 -- aggregates. Note that for aggregates analysis and resolution go
119 -- hand in hand. Aggregate analysis has been delayed up to here and
120 -- it is done while resolving the aggregate.
122 -- N is the N_Aggregate node.
123 -- Typ is the record type for the aggregate resolution
125 -- While performing the semantic checks, this procedure builds a new
126 -- Component_Association_List where each record field appears alone in a
127 -- Component_Choice_List along with its corresponding expression. The
128 -- record fields in the Component_Association_List appear in the same order
129 -- in which they appear in the record type Typ.
131 -- Once this new Component_Association_List is built and all the semantic
132 -- checks performed, the original aggregate subtree is replaced with the
133 -- new named record aggregate just built. Note that subtree substitution is
134 -- performed with Rewrite so as to be able to retrieve the original
135 -- aggregate.
137 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
138 -- yields the aggregate format expected by Gigi. Typically, this kind of
139 -- tree manipulations are done in the expander. However, because the
140 -- semantic checks that need to be performed on record aggregates really go
141 -- hand in hand with the record aggregate normalization, the aggregate
142 -- subtree transformation is performed during resolution rather than
143 -- expansion. Had we decided otherwise we would have had to duplicate most
144 -- of the code in the expansion procedure Expand_Record_Aggregate. Note,
145 -- however, that all the expansion concerning aggregates for tagged records
146 -- is done in Expand_Record_Aggregate.
148 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
150 -- 1. Make sure that the record type against which the record aggregate
151 -- has to be resolved is not abstract. Furthermore if the type is a
152 -- null aggregate make sure the input aggregate N is also null.
154 -- 2. Verify that the structure of the aggregate is that of a record
155 -- aggregate. Specifically, look for component associations and ensure
156 -- that each choice list only has identifiers or the N_Others_Choice
157 -- node. Also make sure that if present, the N_Others_Choice occurs
158 -- last and by itself.
160 -- 3. If Typ contains discriminants, the values for each discriminant is
161 -- looked for. If the record type Typ has variants, we check that the
162 -- expressions corresponding to each discriminant ruling the (possibly
163 -- nested) variant parts of Typ, are static. This allows us to determine
164 -- the variant parts to which the rest of the aggregate must conform.
165 -- The names of discriminants with their values are saved in a new
166 -- association list, New_Assoc_List which is later augmented with the
167 -- names and values of the remaining components in the record type.
169 -- During this phase we also make sure that every discriminant is
170 -- assigned exactly one value. Note that when several values for a given
171 -- discriminant are found, semantic processing continues looking for
172 -- further errors. In this case it's the first discriminant value found
173 -- which we will be recorded.
175 -- IMPORTANT NOTE: For derived tagged types this procedure expects
176 -- First_Discriminant and Next_Discriminant to give the correct list
177 -- of discriminants, in the correct order.
179 -- 4. After all the discriminant values have been gathered, we can set the
180 -- Etype of the record aggregate. If Typ contains no discriminants this
181 -- is straightforward: the Etype of N is just Typ, otherwise a new
182 -- implicit constrained subtype of Typ is built to be the Etype of N.
184 -- 5. Gather the remaining record components according to the discriminant
185 -- values. This involves recursively traversing the record type
186 -- structure to see what variants are selected by the given discriminant
187 -- values. This processing is a little more convoluted if Typ is a
188 -- derived tagged types since we need to retrieve the record structure
189 -- of all the ancestors of Typ.
191 -- 6. After gathering the record components we look for their values in the
192 -- record aggregate and emit appropriate error messages should we not
193 -- find such values or should they be duplicated.
195 -- 7. We then make sure no illegal component names appear in the record
196 -- aggregate and make sure that the type of the record components
197 -- appearing in a same choice list is the same. Finally we ensure that
198 -- the others choice, if present, is used to provide the value of at
199 -- least a record component.
201 -- 8. The original aggregate node is replaced with the new named aggregate
202 -- built in steps 3 through 6, as explained earlier.
204 -- Given the complexity of record aggregate resolution, the primary goal of
205 -- this routine is clarity and simplicity rather than execution and storage
206 -- efficiency. If there are only positional components in the aggregate the
207 -- running time is linear. If there are associations the running time is
208 -- still linear as long as the order of the associations is not too far off
209 -- the order of the components in the record type. If this is not the case
210 -- the running time is at worst quadratic in the size of the association
211 -- list.
213 procedure Check_Misspelled_Component
214 (Elements : Elist_Id;
215 Component : Node_Id);
216 -- Give possible misspelling diagnostic if Component is likely to be a
217 -- misspelling of one of the components of the Assoc_List. This is called
218 -- by Resolve_Aggr_Expr after producing an invalid component error message.
220 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id);
221 -- An optimization: determine whether a discriminated subtype has a static
222 -- constraint, and contains array components whose length is also static,
223 -- either because they are constrained by the discriminant, or because the
224 -- original component bounds are static.
226 -----------------------------------------------------
227 -- Subprograms used for ARRAY AGGREGATE Processing --
228 -----------------------------------------------------
230 function Resolve_Array_Aggregate
231 (N : Node_Id;
232 Index : Node_Id;
233 Index_Constr : Node_Id;
234 Component_Typ : Entity_Id;
235 Others_Allowed : Boolean) return Boolean;
236 -- This procedure performs the semantic checks for an array aggregate.
237 -- True is returned if the aggregate resolution succeeds.
239 -- The procedure works by recursively checking each nested aggregate.
240 -- Specifically, after checking a sub-aggregate nested at the i-th level
241 -- we recursively check all the subaggregates at the i+1-st level (if any).
242 -- Note that for aggregates analysis and resolution go hand in hand.
243 -- Aggregate analysis has been delayed up to here and it is done while
244 -- resolving the aggregate.
246 -- N is the current N_Aggregate node to be checked.
248 -- Index is the index node corresponding to the array sub-aggregate that
249 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
250 -- corresponding index type (or subtype).
252 -- Index_Constr is the node giving the applicable index constraint if
253 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
254 -- contexts [...] that can be used to determine the bounds of the array
255 -- value specified by the aggregate". If Others_Allowed below is False
256 -- there is no applicable index constraint and this node is set to Index.
258 -- Component_Typ is the array component type.
260 -- Others_Allowed indicates whether an others choice is allowed
261 -- in the context where the top-level aggregate appeared.
263 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
265 -- 1. Make sure that the others choice, if present, is by itself and
266 -- appears last in the sub-aggregate. Check that we do not have
267 -- positional and named components in the array sub-aggregate (unless
268 -- the named association is an others choice). Finally if an others
269 -- choice is present, make sure it is allowed in the aggregate context.
271 -- 2. If the array sub-aggregate contains discrete_choices:
273 -- (A) Verify their validity. Specifically verify that:
275 -- (a) If a null range is present it must be the only possible
276 -- choice in the array aggregate.
278 -- (b) Ditto for a non static range.
280 -- (c) Ditto for a non static expression.
282 -- In addition this step analyzes and resolves each discrete_choice,
283 -- making sure that its type is the type of the corresponding Index.
284 -- If we are not at the lowest array aggregate level (in the case of
285 -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
286 -- recursively on each component expression. Otherwise, resolve the
287 -- bottom level component expressions against the expected component
288 -- type ONLY IF the component corresponds to a single discrete choice
289 -- which is not an others choice (to see why read the DELAYED
290 -- COMPONENT RESOLUTION below).
292 -- (B) Determine the bounds of the sub-aggregate and lowest and
293 -- highest choice values.
295 -- 3. For positional aggregates:
297 -- (A) Loop over the component expressions either recursively invoking
298 -- Resolve_Array_Aggregate on each of these for multi-dimensional
299 -- array aggregates or resolving the bottom level component
300 -- expressions against the expected component type.
302 -- (B) Determine the bounds of the positional sub-aggregates.
304 -- 4. Try to determine statically whether the evaluation of the array
305 -- sub-aggregate raises Constraint_Error. If yes emit proper
306 -- warnings. The precise checks are the following:
308 -- (A) Check that the index range defined by aggregate bounds is
309 -- compatible with corresponding index subtype.
310 -- We also check against the base type. In fact it could be that
311 -- Low/High bounds of the base type are static whereas those of
312 -- the index subtype are not. Thus if we can statically catch
313 -- a problem with respect to the base type we are guaranteed
314 -- that the same problem will arise with the index subtype
316 -- (B) If we are dealing with a named aggregate containing an others
317 -- choice and at least one discrete choice then make sure the range
318 -- specified by the discrete choices does not overflow the
319 -- aggregate bounds. We also check against the index type and base
320 -- type bounds for the same reasons given in (A).
322 -- (C) If we are dealing with a positional aggregate with an others
323 -- choice make sure the number of positional elements specified
324 -- does not overflow the aggregate bounds. We also check against
325 -- the index type and base type bounds as mentioned in (A).
327 -- Finally construct an N_Range node giving the sub-aggregate bounds.
328 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
329 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
330 -- to build the appropriate aggregate subtype. Aggregate_Bounds
331 -- information is needed during expansion.
333 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
334 -- expressions in an array aggregate may call Duplicate_Subexpr or some
335 -- other routine that inserts code just outside the outermost aggregate.
336 -- If the array aggregate contains discrete choices or an others choice,
337 -- this may be wrong. Consider for instance the following example.
339 -- type Rec is record
340 -- V : Integer := 0;
341 -- end record;
343 -- type Acc_Rec is access Rec;
344 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
346 -- Then the transformation of "new Rec" that occurs during resolution
347 -- entails the following code modifications
349 -- P7b : constant Acc_Rec := new Rec;
350 -- RecIP (P7b.all);
351 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
353 -- This code transformation is clearly wrong, since we need to call
354 -- "new Rec" for each of the 3 array elements. To avoid this problem we
355 -- delay resolution of the components of non positional array aggregates
356 -- to the expansion phase. As an optimization, if the discrete choice
357 -- specifies a single value we do not delay resolution.
359 function Array_Aggr_Subtype (N : Node_Id; Typ : Node_Id) return Entity_Id;
360 -- This routine returns the type or subtype of an array aggregate.
362 -- N is the array aggregate node whose type we return.
364 -- Typ is the context type in which N occurs.
366 -- This routine creates an implicit array subtype whose bounds are
367 -- those defined by the aggregate. When this routine is invoked
368 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
369 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
370 -- sub-aggregate bounds. When building the aggregate itype, this function
371 -- traverses the array aggregate N collecting such Aggregate_Bounds and
372 -- constructs the proper array aggregate itype.
374 -- Note that in the case of multidimensional aggregates each inner
375 -- sub-aggregate corresponding to a given array dimension, may provide a
376 -- different bounds. If it is possible to determine statically that
377 -- some sub-aggregates corresponding to the same index do not have the
378 -- same bounds, then a warning is emitted. If such check is not possible
379 -- statically (because some sub-aggregate bounds are dynamic expressions)
380 -- then this job is left to the expander. In all cases the particular
381 -- bounds that this function will chose for a given dimension is the first
382 -- N_Range node for a sub-aggregate corresponding to that dimension.
384 -- Note that the Raises_Constraint_Error flag of an array aggregate
385 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
386 -- is set in Resolve_Array_Aggregate but the aggregate is not
387 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
388 -- first construct the proper itype for the aggregate (Gigi needs
389 -- this). After constructing the proper itype we will eventually replace
390 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
391 -- Of course in cases such as:
393 -- type Arr is array (integer range <>) of Integer;
394 -- A : Arr := (positive range -1 .. 2 => 0);
396 -- The bounds of the aggregate itype are cooked up to look reasonable
397 -- (in this particular case the bounds will be 1 .. 2).
399 procedure Aggregate_Constraint_Checks
400 (Exp : Node_Id;
401 Check_Typ : Entity_Id);
402 -- Checks expression Exp against subtype Check_Typ. If Exp is an
403 -- aggregate and Check_Typ a constrained record type with discriminants,
404 -- we generate the appropriate discriminant checks. If Exp is an array
405 -- aggregate then emit the appropriate length checks. If Exp is a scalar
406 -- type, or a string literal, Exp is changed into Check_Typ'(Exp) to
407 -- ensure that range checks are performed at run time.
409 procedure Make_String_Into_Aggregate (N : Node_Id);
410 -- A string literal can appear in a context in which a one dimensional
411 -- array of characters is expected. This procedure simply rewrites the
412 -- string as an aggregate, prior to resolution.
414 ---------------------------------
415 -- Aggregate_Constraint_Checks --
416 ---------------------------------
418 procedure Aggregate_Constraint_Checks
419 (Exp : Node_Id;
420 Check_Typ : Entity_Id)
422 Exp_Typ : constant Entity_Id := Etype (Exp);
424 begin
425 if Raises_Constraint_Error (Exp) then
426 return;
427 end if;
429 -- Ada 2005 (AI-230): Generate a conversion to an anonymous access
430 -- component's type to force the appropriate accessibility checks.
432 -- Ada 2005 (AI-231): Generate conversion to the null-excluding
433 -- type to force the corresponding run-time check
435 if Is_Access_Type (Check_Typ)
436 and then ((Is_Local_Anonymous_Access (Check_Typ))
437 or else (Can_Never_Be_Null (Check_Typ)
438 and then not Can_Never_Be_Null (Exp_Typ)))
439 then
440 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
441 Analyze_And_Resolve (Exp, Check_Typ);
442 Check_Unset_Reference (Exp);
443 end if;
445 -- This is really expansion activity, so make sure that expansion
446 -- is on and is allowed.
448 if not Expander_Active or else In_Spec_Expression then
449 return;
450 end if;
452 -- First check if we have to insert discriminant checks
454 if Has_Discriminants (Exp_Typ) then
455 Apply_Discriminant_Check (Exp, Check_Typ);
457 -- Next emit length checks for array aggregates
459 elsif Is_Array_Type (Exp_Typ) then
460 Apply_Length_Check (Exp, Check_Typ);
462 -- Finally emit scalar and string checks. If we are dealing with a
463 -- scalar literal we need to check by hand because the Etype of
464 -- literals is not necessarily correct.
466 elsif Is_Scalar_Type (Exp_Typ)
467 and then Compile_Time_Known_Value (Exp)
468 then
469 if Is_Out_Of_Range (Exp, Base_Type (Check_Typ)) then
470 Apply_Compile_Time_Constraint_Error
471 (Exp, "value not in range of}??", CE_Range_Check_Failed,
472 Ent => Base_Type (Check_Typ),
473 Typ => Base_Type (Check_Typ));
475 elsif Is_Out_Of_Range (Exp, Check_Typ) then
476 Apply_Compile_Time_Constraint_Error
477 (Exp, "value not in range of}??", CE_Range_Check_Failed,
478 Ent => Check_Typ,
479 Typ => Check_Typ);
481 elsif not Range_Checks_Suppressed (Check_Typ) then
482 Apply_Scalar_Range_Check (Exp, Check_Typ);
483 end if;
485 -- Verify that target type is also scalar, to prevent view anomalies
486 -- in instantiations.
488 elsif (Is_Scalar_Type (Exp_Typ)
489 or else Nkind (Exp) = N_String_Literal)
490 and then Is_Scalar_Type (Check_Typ)
491 and then Exp_Typ /= Check_Typ
492 then
493 if Is_Entity_Name (Exp)
494 and then Ekind (Entity (Exp)) = E_Constant
495 then
496 -- If expression is a constant, it is worthwhile checking whether
497 -- it is a bound of the type.
499 if (Is_Entity_Name (Type_Low_Bound (Check_Typ))
500 and then Entity (Exp) = Entity (Type_Low_Bound (Check_Typ)))
501 or else (Is_Entity_Name (Type_High_Bound (Check_Typ))
502 and then Entity (Exp) = Entity (Type_High_Bound (Check_Typ)))
503 then
504 return;
506 else
507 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
508 Analyze_And_Resolve (Exp, Check_Typ);
509 Check_Unset_Reference (Exp);
510 end if;
511 else
512 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
513 Analyze_And_Resolve (Exp, Check_Typ);
514 Check_Unset_Reference (Exp);
515 end if;
517 end if;
518 end Aggregate_Constraint_Checks;
520 ------------------------
521 -- Array_Aggr_Subtype --
522 ------------------------
524 function Array_Aggr_Subtype
525 (N : Node_Id;
526 Typ : Entity_Id) return Entity_Id
528 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
529 -- Number of aggregate index dimensions
531 Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
532 -- Constrained N_Range of each index dimension in our aggregate itype
534 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
535 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
536 -- Low and High bounds for each index dimension in our aggregate itype
538 Is_Fully_Positional : Boolean := True;
540 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
541 -- N is an array (sub-)aggregate. Dim is the dimension corresponding
542 -- to (sub-)aggregate N. This procedure collects and removes the side
543 -- effects of the constrained N_Range nodes corresponding to each index
544 -- dimension of our aggregate itype. These N_Range nodes are collected
545 -- in Aggr_Range above.
547 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
548 -- bounds of each index dimension. If, when collecting, two bounds
549 -- corresponding to the same dimension are static and found to differ,
550 -- then emit a warning, and mark N as raising Constraint_Error.
552 -------------------------
553 -- Collect_Aggr_Bounds --
554 -------------------------
556 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
557 This_Range : constant Node_Id := Aggregate_Bounds (N);
558 -- The aggregate range node of this specific sub-aggregate
560 This_Low : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
561 This_High : constant Node_Id := High_Bound (Aggregate_Bounds (N));
562 -- The aggregate bounds of this specific sub-aggregate
564 Assoc : Node_Id;
565 Expr : Node_Id;
567 begin
568 Remove_Side_Effects (This_Low, Variable_Ref => True);
569 Remove_Side_Effects (This_High, Variable_Ref => True);
571 -- Collect the first N_Range for a given dimension that you find.
572 -- For a given dimension they must be all equal anyway.
574 if No (Aggr_Range (Dim)) then
575 Aggr_Low (Dim) := This_Low;
576 Aggr_High (Dim) := This_High;
577 Aggr_Range (Dim) := This_Range;
579 else
580 if Compile_Time_Known_Value (This_Low) then
581 if not Compile_Time_Known_Value (Aggr_Low (Dim)) then
582 Aggr_Low (Dim) := This_Low;
584 elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
585 Set_Raises_Constraint_Error (N);
586 Error_Msg_N ("sub-aggregate low bound mismatch??", N);
587 Error_Msg_N
588 ("\Constraint_Error will be raised at run time??", N);
589 end if;
590 end if;
592 if Compile_Time_Known_Value (This_High) then
593 if not Compile_Time_Known_Value (Aggr_High (Dim)) then
594 Aggr_High (Dim) := This_High;
596 elsif
597 Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
598 then
599 Set_Raises_Constraint_Error (N);
600 Error_Msg_N ("sub-aggregate high bound mismatch??", N);
601 Error_Msg_N
602 ("\Constraint_Error will be raised at run time??", N);
603 end if;
604 end if;
605 end if;
607 if Dim < Aggr_Dimension then
609 -- Process positional components
611 if Present (Expressions (N)) then
612 Expr := First (Expressions (N));
613 while Present (Expr) loop
614 Collect_Aggr_Bounds (Expr, Dim + 1);
615 Next (Expr);
616 end loop;
617 end if;
619 -- Process component associations
621 if Present (Component_Associations (N)) then
622 Is_Fully_Positional := False;
624 Assoc := First (Component_Associations (N));
625 while Present (Assoc) loop
626 Expr := Expression (Assoc);
627 Collect_Aggr_Bounds (Expr, Dim + 1);
628 Next (Assoc);
629 end loop;
630 end if;
631 end if;
632 end Collect_Aggr_Bounds;
634 -- Array_Aggr_Subtype variables
636 Itype : Entity_Id;
637 -- The final itype of the overall aggregate
639 Index_Constraints : constant List_Id := New_List;
640 -- The list of index constraints of the aggregate itype
642 -- Start of processing for Array_Aggr_Subtype
644 begin
645 -- Make sure that the list of index constraints is properly attached to
646 -- the tree, and then collect the aggregate bounds.
648 Set_Parent (Index_Constraints, N);
649 Collect_Aggr_Bounds (N, 1);
651 -- Build the list of constrained indexes of our aggregate itype
653 for J in 1 .. Aggr_Dimension loop
654 Create_Index : declare
655 Index_Base : constant Entity_Id :=
656 Base_Type (Etype (Aggr_Range (J)));
657 Index_Typ : Entity_Id;
659 begin
660 -- Construct the Index subtype, and associate it with the range
661 -- construct that generates it.
663 Index_Typ :=
664 Create_Itype (Subtype_Kind (Ekind (Index_Base)), Aggr_Range (J));
666 Set_Etype (Index_Typ, Index_Base);
668 if Is_Character_Type (Index_Base) then
669 Set_Is_Character_Type (Index_Typ);
670 end if;
672 Set_Size_Info (Index_Typ, (Index_Base));
673 Set_RM_Size (Index_Typ, RM_Size (Index_Base));
674 Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base));
675 Set_Scalar_Range (Index_Typ, Aggr_Range (J));
677 if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then
678 Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ)));
679 end if;
681 Set_Etype (Aggr_Range (J), Index_Typ);
683 Append (Aggr_Range (J), To => Index_Constraints);
684 end Create_Index;
685 end loop;
687 -- Now build the Itype
689 Itype := Create_Itype (E_Array_Subtype, N);
691 Set_First_Rep_Item (Itype, First_Rep_Item (Typ));
692 Set_Convention (Itype, Convention (Typ));
693 Set_Depends_On_Private (Itype, Has_Private_Component (Typ));
694 Set_Etype (Itype, Base_Type (Typ));
695 Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ));
696 Set_Is_Aliased (Itype, Is_Aliased (Typ));
697 Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
699 Copy_Suppress_Status (Index_Check, Typ, Itype);
700 Copy_Suppress_Status (Length_Check, Typ, Itype);
702 Set_First_Index (Itype, First (Index_Constraints));
703 Set_Is_Constrained (Itype, True);
704 Set_Is_Internal (Itype, True);
706 -- A simple optimization: purely positional aggregates of static
707 -- components should be passed to gigi unexpanded whenever possible, and
708 -- regardless of the staticness of the bounds themselves. Subsequent
709 -- checks in exp_aggr verify that type is not packed, etc.
711 Set_Size_Known_At_Compile_Time (Itype,
712 Is_Fully_Positional
713 and then Comes_From_Source (N)
714 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
716 -- We always need a freeze node for a packed array subtype, so that we
717 -- can build the Packed_Array_Type corresponding to the subtype. If
718 -- expansion is disabled, the packed array subtype is not built, and we
719 -- must not generate a freeze node for the type, or else it will appear
720 -- incomplete to gigi.
722 if Is_Packed (Itype)
723 and then not In_Spec_Expression
724 and then Expander_Active
725 then
726 Freeze_Itype (Itype, N);
727 end if;
729 return Itype;
730 end Array_Aggr_Subtype;
732 --------------------------------
733 -- Check_Misspelled_Component --
734 --------------------------------
736 procedure Check_Misspelled_Component
737 (Elements : Elist_Id;
738 Component : Node_Id)
740 Max_Suggestions : constant := 2;
742 Nr_Of_Suggestions : Natural := 0;
743 Suggestion_1 : Entity_Id := Empty;
744 Suggestion_2 : Entity_Id := Empty;
745 Component_Elmt : Elmt_Id;
747 begin
748 -- All the components of List are matched against Component and a count
749 -- is maintained of possible misspellings. When at the end of the the
750 -- analysis there are one or two (not more!) possible misspellings,
751 -- these misspellings will be suggested as possible correction.
753 Component_Elmt := First_Elmt (Elements);
754 while Nr_Of_Suggestions <= Max_Suggestions
755 and then Present (Component_Elmt)
756 loop
757 if Is_Bad_Spelling_Of
758 (Chars (Node (Component_Elmt)),
759 Chars (Component))
760 then
761 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
763 case Nr_Of_Suggestions is
764 when 1 => Suggestion_1 := Node (Component_Elmt);
765 when 2 => Suggestion_2 := Node (Component_Elmt);
766 when others => exit;
767 end case;
768 end if;
770 Next_Elmt (Component_Elmt);
771 end loop;
773 -- Report at most two suggestions
775 if Nr_Of_Suggestions = 1 then
776 Error_Msg_NE -- CODEFIX
777 ("\possible misspelling of&", Component, Suggestion_1);
779 elsif Nr_Of_Suggestions = 2 then
780 Error_Msg_Node_2 := Suggestion_2;
781 Error_Msg_NE -- CODEFIX
782 ("\possible misspelling of& or&", Component, Suggestion_1);
783 end if;
784 end Check_Misspelled_Component;
786 ----------------------------------------
787 -- Check_Expr_OK_In_Limited_Aggregate --
788 ----------------------------------------
790 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id) is
791 begin
792 if Is_Limited_Type (Etype (Expr))
793 and then Comes_From_Source (Expr)
794 and then not In_Instance_Body
795 then
796 if not OK_For_Limited_Init (Etype (Expr), Expr) then
797 Error_Msg_N ("initialization not allowed for limited types", Expr);
798 Explain_Limited_Type (Etype (Expr), Expr);
799 end if;
800 end if;
801 end Check_Expr_OK_In_Limited_Aggregate;
803 -------------------------------
804 -- Check_Qualified_Aggregate --
805 -------------------------------
807 procedure Check_Qualified_Aggregate (Level : Nat; Expr : Node_Id) is
808 Comp_Expr : Node_Id;
809 Comp_Assn : Node_Id;
811 begin
812 if Level = 0 then
813 if Nkind (Parent (Expr)) /= N_Qualified_Expression then
814 Check_SPARK_Restriction ("aggregate should be qualified", Expr);
815 end if;
817 else
818 Comp_Expr := First (Expressions (Expr));
819 while Present (Comp_Expr) loop
820 if Nkind (Comp_Expr) = N_Aggregate then
821 Check_Qualified_Aggregate (Level - 1, Comp_Expr);
822 end if;
824 Comp_Expr := Next (Comp_Expr);
825 end loop;
827 Comp_Assn := First (Component_Associations (Expr));
828 while Present (Comp_Assn) loop
829 Comp_Expr := Expression (Comp_Assn);
831 if Nkind (Comp_Expr) = N_Aggregate then
832 Check_Qualified_Aggregate (Level - 1, Comp_Expr);
833 end if;
835 Comp_Assn := Next (Comp_Assn);
836 end loop;
837 end if;
838 end Check_Qualified_Aggregate;
840 ----------------------------------------
841 -- Check_Static_Discriminated_Subtype --
842 ----------------------------------------
844 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id) is
845 Disc : constant Entity_Id := First_Discriminant (T);
846 Comp : Entity_Id;
847 Ind : Entity_Id;
849 begin
850 if Has_Record_Rep_Clause (T) then
851 return;
853 elsif Present (Next_Discriminant (Disc)) then
854 return;
856 elsif Nkind (V) /= N_Integer_Literal then
857 return;
858 end if;
860 Comp := First_Component (T);
861 while Present (Comp) loop
862 if Is_Scalar_Type (Etype (Comp)) then
863 null;
865 elsif Is_Private_Type (Etype (Comp))
866 and then Present (Full_View (Etype (Comp)))
867 and then Is_Scalar_Type (Full_View (Etype (Comp)))
868 then
869 null;
871 elsif Is_Array_Type (Etype (Comp)) then
872 if Is_Bit_Packed_Array (Etype (Comp)) then
873 return;
874 end if;
876 Ind := First_Index (Etype (Comp));
877 while Present (Ind) loop
878 if Nkind (Ind) /= N_Range
879 or else Nkind (Low_Bound (Ind)) /= N_Integer_Literal
880 or else Nkind (High_Bound (Ind)) /= N_Integer_Literal
881 then
882 return;
883 end if;
885 Next_Index (Ind);
886 end loop;
888 else
889 return;
890 end if;
892 Next_Component (Comp);
893 end loop;
895 -- On exit, all components have statically known sizes
897 Set_Size_Known_At_Compile_Time (T);
898 end Check_Static_Discriminated_Subtype;
900 -------------------------
901 -- Is_Others_Aggregate --
902 -------------------------
904 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean is
905 begin
906 return No (Expressions (Aggr))
907 and then
908 Nkind (First (Choices (First (Component_Associations (Aggr)))))
909 = N_Others_Choice;
910 end Is_Others_Aggregate;
912 ----------------------------
913 -- Is_Top_Level_Aggregate --
914 ----------------------------
916 function Is_Top_Level_Aggregate (Expr : Node_Id) return Boolean is
917 begin
918 return Nkind (Parent (Expr)) /= N_Aggregate
919 and then (Nkind (Parent (Expr)) /= N_Component_Association
920 or else Nkind (Parent (Parent (Expr))) /= N_Aggregate);
921 end Is_Top_Level_Aggregate;
923 --------------------------------
924 -- Make_String_Into_Aggregate --
925 --------------------------------
927 procedure Make_String_Into_Aggregate (N : Node_Id) is
928 Exprs : constant List_Id := New_List;
929 Loc : constant Source_Ptr := Sloc (N);
930 Str : constant String_Id := Strval (N);
931 Strlen : constant Nat := String_Length (Str);
932 C : Char_Code;
933 C_Node : Node_Id;
934 New_N : Node_Id;
935 P : Source_Ptr;
937 begin
938 P := Loc + 1;
939 for J in 1 .. Strlen loop
940 C := Get_String_Char (Str, J);
941 Set_Character_Literal_Name (C);
943 C_Node :=
944 Make_Character_Literal (P,
945 Chars => Name_Find,
946 Char_Literal_Value => UI_From_CC (C));
947 Set_Etype (C_Node, Any_Character);
948 Append_To (Exprs, C_Node);
950 P := P + 1;
951 -- Something special for wide strings???
952 end loop;
954 New_N := Make_Aggregate (Loc, Expressions => Exprs);
955 Set_Analyzed (New_N);
956 Set_Etype (New_N, Any_Composite);
958 Rewrite (N, New_N);
959 end Make_String_Into_Aggregate;
961 -----------------------
962 -- Resolve_Aggregate --
963 -----------------------
965 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
966 Loc : constant Source_Ptr := Sloc (N);
967 Pkind : constant Node_Kind := Nkind (Parent (N));
969 Aggr_Subtyp : Entity_Id;
970 -- The actual aggregate subtype. This is not necessarily the same as Typ
971 -- which is the subtype of the context in which the aggregate was found.
973 begin
974 -- Ignore junk empty aggregate resulting from parser error
976 if No (Expressions (N))
977 and then No (Component_Associations (N))
978 and then not Null_Record_Present (N)
979 then
980 return;
981 end if;
983 -- If the aggregate has box-initialized components, its type must be
984 -- frozen so that initialization procedures can properly be called
985 -- in the resolution that follows. The replacement of boxes with
986 -- initialization calls is properly an expansion activity but it must
987 -- be done during revolution.
989 if Expander_Active
990 and then Present (Component_Associations (N))
991 then
992 declare
993 Comp : Node_Id;
995 begin
996 Comp := First (Component_Associations (N));
997 while Present (Comp) loop
998 if Box_Present (Comp) then
999 Insert_Actions (N, Freeze_Entity (Typ, N));
1000 exit;
1001 end if;
1003 Next (Comp);
1004 end loop;
1005 end;
1006 end if;
1008 -- An unqualified aggregate is restricted in SPARK to:
1010 -- An aggregate item inside an aggregate for a multi-dimensional array
1012 -- An expression being assigned to an unconstrained array, but only if
1013 -- the aggregate specifies a value for OTHERS only.
1015 if Nkind (Parent (N)) = N_Qualified_Expression then
1016 if Is_Array_Type (Typ) then
1017 Check_Qualified_Aggregate (Number_Dimensions (Typ), N);
1018 else
1019 Check_Qualified_Aggregate (1, N);
1020 end if;
1021 else
1022 if Is_Array_Type (Typ)
1023 and then Nkind (Parent (N)) = N_Assignment_Statement
1024 and then not Is_Constrained (Etype (Name (Parent (N))))
1025 then
1026 if not Is_Others_Aggregate (N) then
1027 Check_SPARK_Restriction
1028 ("array aggregate should have only OTHERS", N);
1029 end if;
1031 elsif Is_Top_Level_Aggregate (N) then
1032 Check_SPARK_Restriction ("aggregate should be qualified", N);
1034 -- The legality of this unqualified aggregate is checked by calling
1035 -- Check_Qualified_Aggregate from one of its enclosing aggregate,
1036 -- unless one of these already causes an error to be issued.
1038 else
1039 null;
1040 end if;
1041 end if;
1043 -- Check for aggregates not allowed in configurable run-time mode.
1044 -- We allow all cases of aggregates that do not come from source, since
1045 -- these are all assumed to be small (e.g. bounds of a string literal).
1046 -- We also allow aggregates of types we know to be small.
1048 if not Support_Aggregates_On_Target
1049 and then Comes_From_Source (N)
1050 and then (not Known_Static_Esize (Typ) or else Esize (Typ) > 64)
1051 then
1052 Error_Msg_CRT ("aggregate", N);
1053 end if;
1055 -- Ada 2005 (AI-287): Limited aggregates allowed
1057 -- In an instance, ignore aggregate subcomponents tnat may be limited,
1058 -- because they originate in view conflicts. If the original aggregate
1059 -- is legal and the actuals are legal, the aggregate itself is legal.
1061 if Is_Limited_Type (Typ)
1062 and then Ada_Version < Ada_2005
1063 and then not In_Instance
1064 then
1065 Error_Msg_N ("aggregate type cannot be limited", N);
1066 Explain_Limited_Type (Typ, N);
1068 elsif Is_Class_Wide_Type (Typ) then
1069 Error_Msg_N ("type of aggregate cannot be class-wide", N);
1071 elsif Typ = Any_String
1072 or else Typ = Any_Composite
1073 then
1074 Error_Msg_N ("no unique type for aggregate", N);
1075 Set_Etype (N, Any_Composite);
1077 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
1078 Error_Msg_N ("null record forbidden in array aggregate", N);
1080 elsif Is_Record_Type (Typ) then
1081 Resolve_Record_Aggregate (N, Typ);
1083 elsif Is_Array_Type (Typ) then
1085 -- First a special test, for the case of a positional aggregate
1086 -- of characters which can be replaced by a string literal.
1088 -- Do not perform this transformation if this was a string literal to
1089 -- start with, whose components needed constraint checks, or if the
1090 -- component type is non-static, because it will require those checks
1091 -- and be transformed back into an aggregate.
1093 if Number_Dimensions (Typ) = 1
1094 and then Is_Standard_Character_Type (Component_Type (Typ))
1095 and then No (Component_Associations (N))
1096 and then not Is_Limited_Composite (Typ)
1097 and then not Is_Private_Composite (Typ)
1098 and then not Is_Bit_Packed_Array (Typ)
1099 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
1100 and then Is_Static_Subtype (Component_Type (Typ))
1101 then
1102 declare
1103 Expr : Node_Id;
1105 begin
1106 Expr := First (Expressions (N));
1107 while Present (Expr) loop
1108 exit when Nkind (Expr) /= N_Character_Literal;
1109 Next (Expr);
1110 end loop;
1112 if No (Expr) then
1113 Start_String;
1115 Expr := First (Expressions (N));
1116 while Present (Expr) loop
1117 Store_String_Char (UI_To_CC (Char_Literal_Value (Expr)));
1118 Next (Expr);
1119 end loop;
1121 Rewrite (N, Make_String_Literal (Loc, End_String));
1123 Analyze_And_Resolve (N, Typ);
1124 return;
1125 end if;
1126 end;
1127 end if;
1129 -- Here if we have a real aggregate to deal with
1131 Array_Aggregate : declare
1132 Aggr_Resolved : Boolean;
1134 Aggr_Typ : constant Entity_Id := Etype (Typ);
1135 -- This is the unconstrained array type, which is the type against
1136 -- which the aggregate is to be resolved. Typ itself is the array
1137 -- type of the context which may not be the same subtype as the
1138 -- subtype for the final aggregate.
1140 begin
1141 -- In the following we determine whether an OTHERS choice is
1142 -- allowed inside the array aggregate. The test checks the context
1143 -- in which the array aggregate occurs. If the context does not
1144 -- permit it, or the aggregate type is unconstrained, an OTHERS
1145 -- choice is not allowed (except that it is always allowed on the
1146 -- right-hand side of an assignment statement; in this case the
1147 -- constrainedness of the type doesn't matter).
1149 -- If expansion is disabled (generic context, or semantics-only
1150 -- mode) actual subtypes cannot be constructed, and the type of an
1151 -- object may be its unconstrained nominal type. However, if the
1152 -- context is an assignment, we assume that OTHERS is allowed,
1153 -- because the target of the assignment will have a constrained
1154 -- subtype when fully compiled.
1156 -- Note that there is no node for Explicit_Actual_Parameter.
1157 -- To test for this context we therefore have to test for node
1158 -- N_Parameter_Association which itself appears only if there is a
1159 -- formal parameter. Consequently we also need to test for
1160 -- N_Procedure_Call_Statement or N_Function_Call.
1162 Set_Etype (N, Aggr_Typ); -- May be overridden later on
1164 if Pkind = N_Assignment_Statement
1165 or else (Is_Constrained (Typ)
1166 and then
1167 (Pkind = N_Parameter_Association or else
1168 Pkind = N_Function_Call or else
1169 Pkind = N_Procedure_Call_Statement or else
1170 Pkind = N_Generic_Association or else
1171 Pkind = N_Formal_Object_Declaration or else
1172 Pkind = N_Simple_Return_Statement or else
1173 Pkind = N_Object_Declaration or else
1174 Pkind = N_Component_Declaration or else
1175 Pkind = N_Parameter_Specification or else
1176 Pkind = N_Qualified_Expression or else
1177 Pkind = N_Aggregate or else
1178 Pkind = N_Extension_Aggregate or else
1179 Pkind = N_Component_Association))
1180 then
1181 Aggr_Resolved :=
1182 Resolve_Array_Aggregate
1184 Index => First_Index (Aggr_Typ),
1185 Index_Constr => First_Index (Typ),
1186 Component_Typ => Component_Type (Typ),
1187 Others_Allowed => True);
1189 elsif not Expander_Active
1190 and then Pkind = N_Assignment_Statement
1191 then
1192 Aggr_Resolved :=
1193 Resolve_Array_Aggregate
1195 Index => First_Index (Aggr_Typ),
1196 Index_Constr => First_Index (Typ),
1197 Component_Typ => Component_Type (Typ),
1198 Others_Allowed => True);
1200 else
1201 Aggr_Resolved :=
1202 Resolve_Array_Aggregate
1204 Index => First_Index (Aggr_Typ),
1205 Index_Constr => First_Index (Aggr_Typ),
1206 Component_Typ => Component_Type (Typ),
1207 Others_Allowed => False);
1208 end if;
1210 if not Aggr_Resolved then
1212 -- A parenthesized expression may have been intended as an
1213 -- aggregate, leading to a type error when analyzing the
1214 -- component. This can also happen for a nested component
1215 -- (see Analyze_Aggr_Expr).
1217 if Paren_Count (N) > 0 then
1218 Error_Msg_N
1219 ("positional aggregate cannot have one component", N);
1220 end if;
1222 Aggr_Subtyp := Any_Composite;
1224 else
1225 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1226 end if;
1228 Set_Etype (N, Aggr_Subtyp);
1229 end Array_Aggregate;
1231 elsif Is_Private_Type (Typ)
1232 and then Present (Full_View (Typ))
1233 and then (In_Inlined_Body or In_Instance_Body)
1234 and then Is_Composite_Type (Full_View (Typ))
1235 then
1236 Resolve (N, Full_View (Typ));
1238 else
1239 Error_Msg_N ("illegal context for aggregate", N);
1240 end if;
1242 -- If we can determine statically that the evaluation of the aggregate
1243 -- raises Constraint_Error, then replace the aggregate with an
1244 -- N_Raise_Constraint_Error node, but set the Etype to the right
1245 -- aggregate subtype. Gigi needs this.
1247 if Raises_Constraint_Error (N) then
1248 Aggr_Subtyp := Etype (N);
1249 Rewrite (N,
1250 Make_Raise_Constraint_Error (Loc, Reason => CE_Range_Check_Failed));
1251 Set_Raises_Constraint_Error (N);
1252 Set_Etype (N, Aggr_Subtyp);
1253 Set_Analyzed (N);
1254 end if;
1256 Check_Function_Writable_Actuals (N);
1257 end Resolve_Aggregate;
1259 -----------------------------
1260 -- Resolve_Array_Aggregate --
1261 -----------------------------
1263 function Resolve_Array_Aggregate
1264 (N : Node_Id;
1265 Index : Node_Id;
1266 Index_Constr : Node_Id;
1267 Component_Typ : Entity_Id;
1268 Others_Allowed : Boolean) return Boolean
1270 Loc : constant Source_Ptr := Sloc (N);
1272 Failure : constant Boolean := False;
1273 Success : constant Boolean := True;
1275 Index_Typ : constant Entity_Id := Etype (Index);
1276 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1277 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1278 -- The type of the index corresponding to the array sub-aggregate along
1279 -- with its low and upper bounds.
1281 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1282 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1283 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1284 -- Ditto for the base type
1286 function Add (Val : Uint; To : Node_Id) return Node_Id;
1287 -- Creates a new expression node where Val is added to expression To.
1288 -- Tries to constant fold whenever possible. To must be an already
1289 -- analyzed expression.
1291 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1292 -- Checks that AH (the upper bound of an array aggregate) is less than
1293 -- or equal to BH (the upper bound of the index base type). If the check
1294 -- fails, a warning is emitted, the Raises_Constraint_Error flag of N is
1295 -- set, and AH is replaced with a duplicate of BH.
1297 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1298 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1299 -- warning if not and sets the Raises_Constraint_Error flag in N.
1301 procedure Check_Length (L, H : Node_Id; Len : Uint);
1302 -- Checks that range L .. H contains at least Len elements. Emits a
1303 -- warning if not and sets the Raises_Constraint_Error flag in N.
1305 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1306 -- Returns True if range L .. H is dynamic or null
1308 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1309 -- Given expression node From, this routine sets OK to False if it
1310 -- cannot statically evaluate From. Otherwise it stores this static
1311 -- value into Value.
1313 function Resolve_Aggr_Expr
1314 (Expr : Node_Id;
1315 Single_Elmt : Boolean) return Boolean;
1316 -- Resolves aggregate expression Expr. Returns False if resolution
1317 -- fails. If Single_Elmt is set to False, the expression Expr may be
1318 -- used to initialize several array aggregate elements (this can happen
1319 -- for discrete choices such as "L .. H => Expr" or the OTHERS choice).
1320 -- In this event we do not resolve Expr unless expansion is disabled.
1321 -- To know why, see the DELAYED COMPONENT RESOLUTION note above.
1323 -- NOTE: In the case of "... => <>", we pass the in the
1324 -- N_Component_Association node as Expr, since there is no Expression in
1325 -- that case, and we need a Sloc for the error message.
1327 ---------
1328 -- Add --
1329 ---------
1331 function Add (Val : Uint; To : Node_Id) return Node_Id is
1332 Expr_Pos : Node_Id;
1333 Expr : Node_Id;
1334 To_Pos : Node_Id;
1336 begin
1337 if Raises_Constraint_Error (To) then
1338 return To;
1339 end if;
1341 -- First test if we can do constant folding
1343 if Compile_Time_Known_Value (To)
1344 or else Nkind (To) = N_Integer_Literal
1345 then
1346 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1347 Set_Is_Static_Expression (Expr_Pos);
1348 Set_Etype (Expr_Pos, Etype (To));
1349 Set_Analyzed (Expr_Pos, Analyzed (To));
1351 if not Is_Enumeration_Type (Index_Typ) then
1352 Expr := Expr_Pos;
1354 -- If we are dealing with enumeration return
1355 -- Index_Typ'Val (Expr_Pos)
1357 else
1358 Expr :=
1359 Make_Attribute_Reference
1360 (Loc,
1361 Prefix => New_Reference_To (Index_Typ, Loc),
1362 Attribute_Name => Name_Val,
1363 Expressions => New_List (Expr_Pos));
1364 end if;
1366 return Expr;
1367 end if;
1369 -- If we are here no constant folding possible
1371 if not Is_Enumeration_Type (Index_Base) then
1372 Expr :=
1373 Make_Op_Add (Loc,
1374 Left_Opnd => Duplicate_Subexpr (To),
1375 Right_Opnd => Make_Integer_Literal (Loc, Val));
1377 -- If we are dealing with enumeration return
1378 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1380 else
1381 To_Pos :=
1382 Make_Attribute_Reference
1383 (Loc,
1384 Prefix => New_Reference_To (Index_Typ, Loc),
1385 Attribute_Name => Name_Pos,
1386 Expressions => New_List (Duplicate_Subexpr (To)));
1388 Expr_Pos :=
1389 Make_Op_Add (Loc,
1390 Left_Opnd => To_Pos,
1391 Right_Opnd => Make_Integer_Literal (Loc, Val));
1393 Expr :=
1394 Make_Attribute_Reference
1395 (Loc,
1396 Prefix => New_Reference_To (Index_Typ, Loc),
1397 Attribute_Name => Name_Val,
1398 Expressions => New_List (Expr_Pos));
1400 -- If the index type has a non standard representation, the
1401 -- attributes 'Val and 'Pos expand into function calls and the
1402 -- resulting expression is considered non-safe for reevaluation
1403 -- by the backend. Relocate it into a constant temporary in order
1404 -- to make it safe for reevaluation.
1406 if Has_Non_Standard_Rep (Etype (N)) then
1407 declare
1408 Def_Id : Entity_Id;
1410 begin
1411 Def_Id := Make_Temporary (Loc, 'R', Expr);
1412 Set_Etype (Def_Id, Index_Typ);
1413 Insert_Action (N,
1414 Make_Object_Declaration (Loc,
1415 Defining_Identifier => Def_Id,
1416 Object_Definition => New_Reference_To (Index_Typ, Loc),
1417 Constant_Present => True,
1418 Expression => Relocate_Node (Expr)));
1420 Expr := New_Reference_To (Def_Id, Loc);
1421 end;
1422 end if;
1423 end if;
1425 return Expr;
1426 end Add;
1428 -----------------
1429 -- Check_Bound --
1430 -----------------
1432 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1433 Val_BH : Uint;
1434 Val_AH : Uint;
1436 OK_BH : Boolean;
1437 OK_AH : Boolean;
1439 begin
1440 Get (Value => Val_BH, From => BH, OK => OK_BH);
1441 Get (Value => Val_AH, From => AH, OK => OK_AH);
1443 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1444 Set_Raises_Constraint_Error (N);
1445 Error_Msg_N ("upper bound out of range??", AH);
1446 Error_Msg_N ("\Constraint_Error will be raised at run time??", AH);
1448 -- You need to set AH to BH or else in the case of enumerations
1449 -- indexes we will not be able to resolve the aggregate bounds.
1451 AH := Duplicate_Subexpr (BH);
1452 end if;
1453 end Check_Bound;
1455 ------------------
1456 -- Check_Bounds --
1457 ------------------
1459 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1460 Val_L : Uint;
1461 Val_H : Uint;
1462 Val_AL : Uint;
1463 Val_AH : Uint;
1465 OK_L : Boolean;
1466 OK_H : Boolean;
1468 OK_AL : Boolean;
1469 OK_AH : Boolean;
1470 pragma Warnings (Off, OK_AL);
1471 pragma Warnings (Off, OK_AH);
1473 begin
1474 if Raises_Constraint_Error (N)
1475 or else Dynamic_Or_Null_Range (AL, AH)
1476 then
1477 return;
1478 end if;
1480 Get (Value => Val_L, From => L, OK => OK_L);
1481 Get (Value => Val_H, From => H, OK => OK_H);
1483 Get (Value => Val_AL, From => AL, OK => OK_AL);
1484 Get (Value => Val_AH, From => AH, OK => OK_AH);
1486 if OK_L and then Val_L > Val_AL then
1487 Set_Raises_Constraint_Error (N);
1488 Error_Msg_N ("lower bound of aggregate out of range??", N);
1489 Error_Msg_N ("\Constraint_Error will be raised at run time??", N);
1490 end if;
1492 if OK_H and then Val_H < Val_AH then
1493 Set_Raises_Constraint_Error (N);
1494 Error_Msg_N ("upper bound of aggregate out of range??", N);
1495 Error_Msg_N ("\Constraint_Error will be raised at run time??", N);
1496 end if;
1497 end Check_Bounds;
1499 ------------------
1500 -- Check_Length --
1501 ------------------
1503 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1504 Val_L : Uint;
1505 Val_H : Uint;
1507 OK_L : Boolean;
1508 OK_H : Boolean;
1510 Range_Len : Uint;
1512 begin
1513 if Raises_Constraint_Error (N) then
1514 return;
1515 end if;
1517 Get (Value => Val_L, From => L, OK => OK_L);
1518 Get (Value => Val_H, From => H, OK => OK_H);
1520 if not OK_L or else not OK_H then
1521 return;
1522 end if;
1524 -- If null range length is zero
1526 if Val_L > Val_H then
1527 Range_Len := Uint_0;
1528 else
1529 Range_Len := Val_H - Val_L + 1;
1530 end if;
1532 if Range_Len < Len then
1533 Set_Raises_Constraint_Error (N);
1534 Error_Msg_N ("too many elements??", N);
1535 Error_Msg_N ("\Constraint_Error will be raised at run time??", N);
1536 end if;
1537 end Check_Length;
1539 ---------------------------
1540 -- Dynamic_Or_Null_Range --
1541 ---------------------------
1543 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1544 Val_L : Uint;
1545 Val_H : Uint;
1547 OK_L : Boolean;
1548 OK_H : Boolean;
1550 begin
1551 Get (Value => Val_L, From => L, OK => OK_L);
1552 Get (Value => Val_H, From => H, OK => OK_H);
1554 return not OK_L or else not OK_H
1555 or else not Is_OK_Static_Expression (L)
1556 or else not Is_OK_Static_Expression (H)
1557 or else Val_L > Val_H;
1558 end Dynamic_Or_Null_Range;
1560 ---------
1561 -- Get --
1562 ---------
1564 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1565 begin
1566 OK := True;
1568 if Compile_Time_Known_Value (From) then
1569 Value := Expr_Value (From);
1571 -- If expression From is something like Some_Type'Val (10) then
1572 -- Value = 10
1574 elsif Nkind (From) = N_Attribute_Reference
1575 and then Attribute_Name (From) = Name_Val
1576 and then Compile_Time_Known_Value (First (Expressions (From)))
1577 then
1578 Value := Expr_Value (First (Expressions (From)));
1580 else
1581 Value := Uint_0;
1582 OK := False;
1583 end if;
1584 end Get;
1586 -----------------------
1587 -- Resolve_Aggr_Expr --
1588 -----------------------
1590 function Resolve_Aggr_Expr
1591 (Expr : Node_Id;
1592 Single_Elmt : Boolean) return Boolean
1594 Nxt_Ind : constant Node_Id := Next_Index (Index);
1595 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
1596 -- Index is the current index corresponding to the expression
1598 Resolution_OK : Boolean := True;
1599 -- Set to False if resolution of the expression failed
1601 begin
1602 -- Defend against previous errors
1604 if Nkind (Expr) = N_Error
1605 or else Error_Posted (Expr)
1606 then
1607 return True;
1608 end if;
1610 -- If the array type against which we are resolving the aggregate
1611 -- has several dimensions, the expressions nested inside the
1612 -- aggregate must be further aggregates (or strings).
1614 if Present (Nxt_Ind) then
1615 if Nkind (Expr) /= N_Aggregate then
1617 -- A string literal can appear where a one-dimensional array
1618 -- of characters is expected. If the literal looks like an
1619 -- operator, it is still an operator symbol, which will be
1620 -- transformed into a string when analyzed.
1622 if Is_Character_Type (Component_Typ)
1623 and then No (Next_Index (Nxt_Ind))
1624 and then Nkind_In (Expr, N_String_Literal, N_Operator_Symbol)
1625 then
1626 -- A string literal used in a multidimensional array
1627 -- aggregate in place of the final one-dimensional
1628 -- aggregate must not be enclosed in parentheses.
1630 if Paren_Count (Expr) /= 0 then
1631 Error_Msg_N ("no parenthesis allowed here", Expr);
1632 end if;
1634 Make_String_Into_Aggregate (Expr);
1636 else
1637 Error_Msg_N ("nested array aggregate expected", Expr);
1639 -- If the expression is parenthesized, this may be
1640 -- a missing component association for a 1-aggregate.
1642 if Paren_Count (Expr) > 0 then
1643 Error_Msg_N
1644 ("\if single-component aggregate is intended,"
1645 & " write e.g. (1 ='> ...)", Expr);
1646 end if;
1648 return Failure;
1649 end if;
1650 end if;
1652 -- If it's "... => <>", nothing to resolve
1654 if Nkind (Expr) = N_Component_Association then
1655 pragma Assert (Box_Present (Expr));
1656 return Success;
1657 end if;
1659 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
1660 -- Required to check the null-exclusion attribute (if present).
1661 -- This value may be overridden later on.
1663 Set_Etype (Expr, Etype (N));
1665 Resolution_OK := Resolve_Array_Aggregate
1666 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1668 else
1670 -- If it's "... => <>", nothing to resolve
1672 if Nkind (Expr) = N_Component_Association then
1673 pragma Assert (Box_Present (Expr));
1674 return Success;
1675 end if;
1677 -- Do not resolve the expressions of discrete or others choices
1678 -- unless the expression covers a single component, or the
1679 -- expander is inactive.
1681 -- In SPARK mode, expressions that can perform side-effects will
1682 -- be recognized by the gnat2why back-end, and the whole
1683 -- subprogram will be ignored. So semantic analysis can be
1684 -- performed safely.
1686 if Single_Elmt
1687 or else not Full_Expander_Active
1688 or else In_Spec_Expression
1689 then
1690 Analyze_And_Resolve (Expr, Component_Typ);
1691 Check_Expr_OK_In_Limited_Aggregate (Expr);
1692 Check_Non_Static_Context (Expr);
1693 Aggregate_Constraint_Checks (Expr, Component_Typ);
1694 Check_Unset_Reference (Expr);
1695 end if;
1696 end if;
1698 -- If an aggregate component has a type with predicates, an explicit
1699 -- predicate check must be applied, as for an assignment statement,
1700 -- because the aggegate might not be expanded into individual
1701 -- component assignments.
1703 if Present (Predicate_Function (Component_Typ)) then
1704 Apply_Predicate_Check (Expr, Component_Typ);
1705 end if;
1707 if Raises_Constraint_Error (Expr)
1708 and then Nkind (Parent (Expr)) /= N_Component_Association
1709 then
1710 Set_Raises_Constraint_Error (N);
1711 end if;
1713 -- If the expression has been marked as requiring a range check,
1714 -- then generate it here.
1716 if Do_Range_Check (Expr) then
1717 Set_Do_Range_Check (Expr, False);
1718 Generate_Range_Check (Expr, Component_Typ, CE_Range_Check_Failed);
1719 end if;
1721 return Resolution_OK;
1722 end Resolve_Aggr_Expr;
1724 -- Variables local to Resolve_Array_Aggregate
1726 Assoc : Node_Id;
1727 Choice : Node_Id;
1728 Expr : Node_Id;
1730 Discard : Node_Id;
1731 pragma Warnings (Off, Discard);
1733 Delete_Choice : Boolean;
1734 -- Used when replacing a subtype choice with predicate by a list
1736 Aggr_Low : Node_Id := Empty;
1737 Aggr_High : Node_Id := Empty;
1738 -- The actual low and high bounds of this sub-aggregate
1740 Choices_Low : Node_Id := Empty;
1741 Choices_High : Node_Id := Empty;
1742 -- The lowest and highest discrete choices values for a named aggregate
1744 Nb_Elements : Uint := Uint_0;
1745 -- The number of elements in a positional aggregate
1747 Others_Present : Boolean := False;
1749 Nb_Choices : Nat := 0;
1750 -- Contains the overall number of named choices in this sub-aggregate
1752 Nb_Discrete_Choices : Nat := 0;
1753 -- The overall number of discrete choices (not counting others choice)
1755 Case_Table_Size : Nat;
1756 -- Contains the size of the case table needed to sort aggregate choices
1758 -- Start of processing for Resolve_Array_Aggregate
1760 begin
1761 -- Ignore junk empty aggregate resulting from parser error
1763 if No (Expressions (N))
1764 and then No (Component_Associations (N))
1765 and then not Null_Record_Present (N)
1766 then
1767 return False;
1768 end if;
1770 -- STEP 1: make sure the aggregate is correctly formatted
1772 if Present (Component_Associations (N)) then
1773 Assoc := First (Component_Associations (N));
1774 while Present (Assoc) loop
1775 Choice := First (Choices (Assoc));
1776 Delete_Choice := False;
1778 while Present (Choice) loop
1779 if Nkind (Choice) = N_Others_Choice then
1780 Others_Present := True;
1782 if Choice /= First (Choices (Assoc))
1783 or else Present (Next (Choice))
1784 then
1785 Error_Msg_N
1786 ("OTHERS must appear alone in a choice list", Choice);
1787 return Failure;
1788 end if;
1790 if Present (Next (Assoc)) then
1791 Error_Msg_N
1792 ("OTHERS must appear last in an aggregate", Choice);
1793 return Failure;
1794 end if;
1796 if Ada_Version = Ada_83
1797 and then Assoc /= First (Component_Associations (N))
1798 and then Nkind_In (Parent (N), N_Assignment_Statement,
1799 N_Object_Declaration)
1800 then
1801 Error_Msg_N
1802 ("(Ada 83) illegal context for OTHERS choice", N);
1803 end if;
1805 elsif Is_Entity_Name (Choice) then
1806 Analyze (Choice);
1808 declare
1809 E : constant Entity_Id := Entity (Choice);
1810 New_Cs : List_Id;
1811 P : Node_Id;
1812 C : Node_Id;
1814 begin
1815 if Is_Type (E) and then Has_Predicates (E) then
1816 Freeze_Before (N, E);
1818 -- If the subtype has a static predicate, replace the
1819 -- original choice with the list of individual values
1820 -- covered by the predicate.
1822 if Present (Static_Predicate (E)) then
1823 Delete_Choice := True;
1825 New_Cs := New_List;
1826 P := First (Static_Predicate (E));
1827 while Present (P) loop
1828 C := New_Copy (P);
1829 Set_Sloc (C, Sloc (Choice));
1830 Append_To (New_Cs, C);
1831 Next (P);
1832 end loop;
1834 Insert_List_After (Choice, New_Cs);
1835 end if;
1836 end if;
1837 end;
1838 end if;
1840 Nb_Choices := Nb_Choices + 1;
1842 declare
1843 C : constant Node_Id := Choice;
1845 begin
1846 Next (Choice);
1848 if Delete_Choice then
1849 Remove (C);
1850 Nb_Choices := Nb_Choices - 1;
1851 Delete_Choice := False;
1852 end if;
1853 end;
1854 end loop;
1856 Next (Assoc);
1857 end loop;
1858 end if;
1860 -- At this point we know that the others choice, if present, is by
1861 -- itself and appears last in the aggregate. Check if we have mixed
1862 -- positional and discrete associations (other than the others choice).
1864 if Present (Expressions (N))
1865 and then (Nb_Choices > 1
1866 or else (Nb_Choices = 1 and then not Others_Present))
1867 then
1868 Error_Msg_N
1869 ("named association cannot follow positional association",
1870 First (Choices (First (Component_Associations (N)))));
1871 return Failure;
1872 end if;
1874 -- Test for the validity of an others choice if present
1876 if Others_Present and then not Others_Allowed then
1877 Error_Msg_N
1878 ("OTHERS choice not allowed here",
1879 First (Choices (First (Component_Associations (N)))));
1880 return Failure;
1881 end if;
1883 -- Protect against cascaded errors
1885 if Etype (Index_Typ) = Any_Type then
1886 return Failure;
1887 end if;
1889 -- STEP 2: Process named components
1891 if No (Expressions (N)) then
1892 if Others_Present then
1893 Case_Table_Size := Nb_Choices - 1;
1894 else
1895 Case_Table_Size := Nb_Choices;
1896 end if;
1898 Step_2 : declare
1899 Low : Node_Id;
1900 High : Node_Id;
1901 -- Denote the lowest and highest values in an aggregate choice
1903 Hi_Val : Uint;
1904 Lo_Val : Uint;
1905 -- High end of one range and Low end of the next. Should be
1906 -- contiguous if there is no hole in the list of values.
1908 Missing_Values : Boolean;
1909 -- Set True if missing index values
1911 S_Low : Node_Id := Empty;
1912 S_High : Node_Id := Empty;
1913 -- if a choice in an aggregate is a subtype indication these
1914 -- denote the lowest and highest values of the subtype
1916 Table : Case_Table_Type (1 .. Case_Table_Size);
1917 -- Used to sort all the different choice values
1919 Single_Choice : Boolean;
1920 -- Set to true every time there is a single discrete choice in a
1921 -- discrete association
1923 Prev_Nb_Discrete_Choices : Nat;
1924 -- Used to keep track of the number of discrete choices in the
1925 -- current association.
1927 Errors_Posted_On_Choices : Boolean := False;
1928 -- Keeps track of whether any choices have semantic errors
1930 begin
1931 -- STEP 2 (A): Check discrete choices validity
1933 Assoc := First (Component_Associations (N));
1934 while Present (Assoc) loop
1935 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
1936 Choice := First (Choices (Assoc));
1937 loop
1938 Analyze (Choice);
1940 if Nkind (Choice) = N_Others_Choice then
1941 Single_Choice := False;
1942 exit;
1944 -- Test for subtype mark without constraint
1946 elsif Is_Entity_Name (Choice) and then
1947 Is_Type (Entity (Choice))
1948 then
1949 if Base_Type (Entity (Choice)) /= Index_Base then
1950 Error_Msg_N
1951 ("invalid subtype mark in aggregate choice",
1952 Choice);
1953 return Failure;
1954 end if;
1956 -- Case of subtype indication
1958 elsif Nkind (Choice) = N_Subtype_Indication then
1959 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
1961 -- Does the subtype indication evaluation raise CE?
1963 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
1964 Get_Index_Bounds (Choice, Low, High);
1965 Check_Bounds (S_Low, S_High, Low, High);
1967 -- Case of range or expression
1969 else
1970 Resolve (Choice, Index_Base);
1971 Check_Unset_Reference (Choice);
1972 Check_Non_Static_Context (Choice);
1974 -- If semantic errors were posted on the choice, then
1975 -- record that for possible early return from later
1976 -- processing (see handling of enumeration choices).
1978 if Error_Posted (Choice) then
1979 Errors_Posted_On_Choices := True;
1980 end if;
1982 -- Do not range check a choice. This check is redundant
1983 -- since this test is already done when we check that the
1984 -- bounds of the array aggregate are within range.
1986 Set_Do_Range_Check (Choice, False);
1988 -- In SPARK, the choice must be static
1990 if not (Is_Static_Expression (Choice)
1991 or else (Nkind (Choice) = N_Range
1992 and then Is_Static_Range (Choice)))
1993 then
1994 Check_SPARK_Restriction
1995 ("choice should be static", Choice);
1996 end if;
1997 end if;
1999 -- If we could not resolve the discrete choice stop here
2001 if Etype (Choice) = Any_Type then
2002 return Failure;
2004 -- If the discrete choice raises CE get its original bounds
2006 elsif Nkind (Choice) = N_Raise_Constraint_Error then
2007 Set_Raises_Constraint_Error (N);
2008 Get_Index_Bounds (Original_Node (Choice), Low, High);
2010 -- Otherwise get its bounds as usual
2012 else
2013 Get_Index_Bounds (Choice, Low, High);
2014 end if;
2016 if (Dynamic_Or_Null_Range (Low, High)
2017 or else (Nkind (Choice) = N_Subtype_Indication
2018 and then
2019 Dynamic_Or_Null_Range (S_Low, S_High)))
2020 and then Nb_Choices /= 1
2021 then
2022 Error_Msg_N
2023 ("dynamic or empty choice in aggregate " &
2024 "must be the only choice", Choice);
2025 return Failure;
2026 end if;
2028 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
2029 Table (Nb_Discrete_Choices).Choice_Lo := Low;
2030 Table (Nb_Discrete_Choices).Choice_Hi := High;
2031 Table (Nb_Discrete_Choices).Choice_Node := Choice;
2033 Next (Choice);
2035 if No (Choice) then
2037 -- Check if we have a single discrete choice and whether
2038 -- this discrete choice specifies a single value.
2040 Single_Choice :=
2041 (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1)
2042 and then (Low = High);
2044 exit;
2045 end if;
2046 end loop;
2048 -- Ada 2005 (AI-231)
2050 if Ada_Version >= Ada_2005
2051 and then Known_Null (Expression (Assoc))
2052 then
2053 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2054 end if;
2056 -- Ada 2005 (AI-287): In case of default initialized component
2057 -- we delay the resolution to the expansion phase.
2059 if Box_Present (Assoc) then
2061 -- Ada 2005 (AI-287): In case of default initialization of a
2062 -- component the expander will generate calls to the
2063 -- corresponding initialization subprogram. We need to call
2064 -- Resolve_Aggr_Expr to check the rules about
2065 -- dimensionality.
2067 if not Resolve_Aggr_Expr (Assoc,
2068 Single_Elmt => Single_Choice)
2069 then
2070 return Failure;
2071 end if;
2073 elsif not Resolve_Aggr_Expr (Expression (Assoc),
2074 Single_Elmt => Single_Choice)
2075 then
2076 return Failure;
2078 -- Check incorrect use of dynamically tagged expression
2080 -- We differentiate here two cases because the expression may
2081 -- not be decorated. For example, the analysis and resolution
2082 -- of the expression associated with the others choice will be
2083 -- done later with the full aggregate. In such case we
2084 -- duplicate the expression tree to analyze the copy and
2085 -- perform the required check.
2087 elsif not Present (Etype (Expression (Assoc))) then
2088 declare
2089 Save_Analysis : constant Boolean := Full_Analysis;
2090 Expr : constant Node_Id :=
2091 New_Copy_Tree (Expression (Assoc));
2093 begin
2094 Expander_Mode_Save_And_Set (False);
2095 Full_Analysis := False;
2097 -- Analyze the expression, making sure it is properly
2098 -- attached to the tree before we do the analysis.
2100 Set_Parent (Expr, Parent (Expression (Assoc)));
2101 Analyze (Expr);
2103 -- If the expression is a literal, propagate this info
2104 -- to the expression in the association, to enable some
2105 -- optimizations downstream.
2107 if Is_Entity_Name (Expr)
2108 and then Present (Entity (Expr))
2109 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
2110 then
2111 Analyze_And_Resolve
2112 (Expression (Assoc), Component_Typ);
2113 end if;
2115 Full_Analysis := Save_Analysis;
2116 Expander_Mode_Restore;
2118 if Is_Tagged_Type (Etype (Expr)) then
2119 Check_Dynamically_Tagged_Expression
2120 (Expr => Expr,
2121 Typ => Component_Type (Etype (N)),
2122 Related_Nod => N);
2123 end if;
2124 end;
2126 elsif Is_Tagged_Type (Etype (Expression (Assoc))) then
2127 Check_Dynamically_Tagged_Expression
2128 (Expr => Expression (Assoc),
2129 Typ => Component_Type (Etype (N)),
2130 Related_Nod => N);
2131 end if;
2133 Next (Assoc);
2134 end loop;
2136 -- If aggregate contains more than one choice then these must be
2137 -- static. Sort them and check that they are contiguous.
2139 if Nb_Discrete_Choices > 1 then
2140 Sort_Case_Table (Table);
2141 Missing_Values := False;
2143 Outer : for J in 1 .. Nb_Discrete_Choices - 1 loop
2144 if Expr_Value (Table (J).Choice_Hi) >=
2145 Expr_Value (Table (J + 1).Choice_Lo)
2146 then
2147 Error_Msg_N
2148 ("duplicate choice values in array aggregate",
2149 Table (J).Choice_Node);
2150 return Failure;
2152 elsif not Others_Present then
2153 Hi_Val := Expr_Value (Table (J).Choice_Hi);
2154 Lo_Val := Expr_Value (Table (J + 1).Choice_Lo);
2156 -- If missing values, output error messages
2158 if Lo_Val - Hi_Val > 1 then
2160 -- Header message if not first missing value
2162 if not Missing_Values then
2163 Error_Msg_N
2164 ("missing index value(s) in array aggregate", N);
2165 Missing_Values := True;
2166 end if;
2168 -- Output values of missing indexes
2170 Lo_Val := Lo_Val - 1;
2171 Hi_Val := Hi_Val + 1;
2173 -- Enumeration type case
2175 if Is_Enumeration_Type (Index_Typ) then
2176 Error_Msg_Name_1 :=
2177 Chars
2178 (Get_Enum_Lit_From_Pos
2179 (Index_Typ, Hi_Val, Loc));
2181 if Lo_Val = Hi_Val then
2182 Error_Msg_N ("\ %", N);
2183 else
2184 Error_Msg_Name_2 :=
2185 Chars
2186 (Get_Enum_Lit_From_Pos
2187 (Index_Typ, Lo_Val, Loc));
2188 Error_Msg_N ("\ % .. %", N);
2189 end if;
2191 -- Integer types case
2193 else
2194 Error_Msg_Uint_1 := Hi_Val;
2196 if Lo_Val = Hi_Val then
2197 Error_Msg_N ("\ ^", N);
2198 else
2199 Error_Msg_Uint_2 := Lo_Val;
2200 Error_Msg_N ("\ ^ .. ^", N);
2201 end if;
2202 end if;
2203 end if;
2204 end if;
2205 end loop Outer;
2207 if Missing_Values then
2208 Set_Etype (N, Any_Composite);
2209 return Failure;
2210 end if;
2211 end if;
2213 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
2215 if Nb_Discrete_Choices > 0 then
2216 Choices_Low := Table (1).Choice_Lo;
2217 Choices_High := Table (Nb_Discrete_Choices).Choice_Hi;
2218 end if;
2220 -- If Others is present, then bounds of aggregate come from the
2221 -- index constraint (not the choices in the aggregate itself).
2223 if Others_Present then
2224 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2226 -- No others clause present
2228 else
2229 -- Special processing if others allowed and not present. This
2230 -- means that the bounds of the aggregate come from the index
2231 -- constraint (and the length must match).
2233 if Others_Allowed then
2234 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2236 -- If others allowed, and no others present, then the array
2237 -- should cover all index values. If it does not, we will
2238 -- get a length check warning, but there is two cases where
2239 -- an additional warning is useful:
2241 -- If we have no positional components, and the length is
2242 -- wrong (which we can tell by others being allowed with
2243 -- missing components), and the index type is an enumeration
2244 -- type, then issue appropriate warnings about these missing
2245 -- components. They are only warnings, since the aggregate
2246 -- is fine, it's just the wrong length. We skip this check
2247 -- for standard character types (since there are no literals
2248 -- and it is too much trouble to concoct them), and also if
2249 -- any of the bounds have not-known-at-compile-time values.
2251 -- Another case warranting a warning is when the length is
2252 -- right, but as above we have an index type that is an
2253 -- enumeration, and the bounds do not match. This is a
2254 -- case where dubious sliding is allowed and we generate
2255 -- a warning that the bounds do not match.
2257 if No (Expressions (N))
2258 and then Nkind (Index) = N_Range
2259 and then Is_Enumeration_Type (Etype (Index))
2260 and then not Is_Standard_Character_Type (Etype (Index))
2261 and then Compile_Time_Known_Value (Aggr_Low)
2262 and then Compile_Time_Known_Value (Aggr_High)
2263 and then Compile_Time_Known_Value (Choices_Low)
2264 and then Compile_Time_Known_Value (Choices_High)
2265 then
2266 -- If any of the expressions or range bounds in choices
2267 -- have semantic errors, then do not attempt further
2268 -- resolution, to prevent cascaded errors.
2270 if Errors_Posted_On_Choices then
2271 return Failure;
2272 end if;
2274 declare
2275 ALo : constant Node_Id := Expr_Value_E (Aggr_Low);
2276 AHi : constant Node_Id := Expr_Value_E (Aggr_High);
2277 CLo : constant Node_Id := Expr_Value_E (Choices_Low);
2278 CHi : constant Node_Id := Expr_Value_E (Choices_High);
2280 Ent : Entity_Id;
2282 begin
2283 -- Warning case 1, missing values at start/end. Only
2284 -- do the check if the number of entries is too small.
2286 if (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2288 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2289 then
2290 Error_Msg_N
2291 ("missing index value(s) in array aggregate??",
2294 -- Output missing value(s) at start
2296 if Chars (ALo) /= Chars (CLo) then
2297 Ent := Prev (CLo);
2299 if Chars (ALo) = Chars (Ent) then
2300 Error_Msg_Name_1 := Chars (ALo);
2301 Error_Msg_N ("\ %??", N);
2302 else
2303 Error_Msg_Name_1 := Chars (ALo);
2304 Error_Msg_Name_2 := Chars (Ent);
2305 Error_Msg_N ("\ % .. %??", N);
2306 end if;
2307 end if;
2309 -- Output missing value(s) at end
2311 if Chars (AHi) /= Chars (CHi) then
2312 Ent := Next (CHi);
2314 if Chars (AHi) = Chars (Ent) then
2315 Error_Msg_Name_1 := Chars (Ent);
2316 Error_Msg_N ("\ %??", N);
2317 else
2318 Error_Msg_Name_1 := Chars (Ent);
2319 Error_Msg_Name_2 := Chars (AHi);
2320 Error_Msg_N ("\ % .. %??", N);
2321 end if;
2322 end if;
2324 -- Warning case 2, dubious sliding. The First_Subtype
2325 -- test distinguishes between a constrained type where
2326 -- sliding is not allowed (so we will get a warning
2327 -- later that Constraint_Error will be raised), and
2328 -- the unconstrained case where sliding is permitted.
2330 elsif (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2332 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2333 and then Chars (ALo) /= Chars (CLo)
2334 and then
2335 not Is_Constrained (First_Subtype (Etype (N)))
2336 then
2337 Error_Msg_N
2338 ("bounds of aggregate do not match target??", N);
2339 end if;
2340 end;
2341 end if;
2342 end if;
2344 -- If no others, aggregate bounds come from aggregate
2346 Aggr_Low := Choices_Low;
2347 Aggr_High := Choices_High;
2348 end if;
2349 end Step_2;
2351 -- STEP 3: Process positional components
2353 else
2354 -- STEP 3 (A): Process positional elements
2356 Expr := First (Expressions (N));
2357 Nb_Elements := Uint_0;
2358 while Present (Expr) loop
2359 Nb_Elements := Nb_Elements + 1;
2361 -- Ada 2005 (AI-231)
2363 if Ada_Version >= Ada_2005
2364 and then Known_Null (Expr)
2365 then
2366 Check_Can_Never_Be_Null (Etype (N), Expr);
2367 end if;
2369 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
2370 return Failure;
2371 end if;
2373 -- Check incorrect use of dynamically tagged expression
2375 if Is_Tagged_Type (Etype (Expr)) then
2376 Check_Dynamically_Tagged_Expression
2377 (Expr => Expr,
2378 Typ => Component_Type (Etype (N)),
2379 Related_Nod => N);
2380 end if;
2382 Next (Expr);
2383 end loop;
2385 if Others_Present then
2386 Assoc := Last (Component_Associations (N));
2388 -- Ada 2005 (AI-231)
2390 if Ada_Version >= Ada_2005
2391 and then Known_Null (Assoc)
2392 then
2393 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2394 end if;
2396 -- Ada 2005 (AI-287): In case of default initialized component,
2397 -- we delay the resolution to the expansion phase.
2399 if Box_Present (Assoc) then
2401 -- Ada 2005 (AI-287): In case of default initialization of a
2402 -- component the expander will generate calls to the
2403 -- corresponding initialization subprogram. We need to call
2404 -- Resolve_Aggr_Expr to check the rules about
2405 -- dimensionality.
2407 if not Resolve_Aggr_Expr (Assoc, Single_Elmt => False) then
2408 return Failure;
2409 end if;
2411 elsif not Resolve_Aggr_Expr (Expression (Assoc),
2412 Single_Elmt => False)
2413 then
2414 return Failure;
2416 -- Check incorrect use of dynamically tagged expression. The
2417 -- expression of the others choice has not been resolved yet.
2418 -- In order to diagnose the semantic error we create a duplicate
2419 -- tree to analyze it and perform the check.
2421 else
2422 declare
2423 Save_Analysis : constant Boolean := Full_Analysis;
2424 Expr : constant Node_Id :=
2425 New_Copy_Tree (Expression (Assoc));
2427 begin
2428 Expander_Mode_Save_And_Set (False);
2429 Full_Analysis := False;
2430 Analyze (Expr);
2431 Full_Analysis := Save_Analysis;
2432 Expander_Mode_Restore;
2434 if Is_Tagged_Type (Etype (Expr)) then
2435 Check_Dynamically_Tagged_Expression
2436 (Expr => Expr,
2437 Typ => Component_Type (Etype (N)),
2438 Related_Nod => N);
2439 end if;
2440 end;
2441 end if;
2442 end if;
2444 -- STEP 3 (B): Compute the aggregate bounds
2446 if Others_Present then
2447 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2449 else
2450 if Others_Allowed then
2451 Get_Index_Bounds (Index_Constr, Aggr_Low, Discard);
2452 else
2453 Aggr_Low := Index_Typ_Low;
2454 end if;
2456 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
2457 Check_Bound (Index_Base_High, Aggr_High);
2458 end if;
2459 end if;
2461 -- STEP 4: Perform static aggregate checks and save the bounds
2463 -- Check (A)
2465 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
2466 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
2468 -- Check (B)
2470 if Others_Present and then Nb_Discrete_Choices > 0 then
2471 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
2472 Check_Bounds (Index_Typ_Low, Index_Typ_High,
2473 Choices_Low, Choices_High);
2474 Check_Bounds (Index_Base_Low, Index_Base_High,
2475 Choices_Low, Choices_High);
2477 -- Check (C)
2479 elsif Others_Present and then Nb_Elements > 0 then
2480 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
2481 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
2482 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
2483 end if;
2485 if Raises_Constraint_Error (Aggr_Low)
2486 or else Raises_Constraint_Error (Aggr_High)
2487 then
2488 Set_Raises_Constraint_Error (N);
2489 end if;
2491 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
2493 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
2494 -- since the addition node returned by Add is not yet analyzed. Attach
2495 -- to tree and analyze first. Reset analyzed flag to ensure it will get
2496 -- analyzed when it is a literal bound whose type must be properly set.
2498 if Others_Present or else Nb_Discrete_Choices > 0 then
2499 Aggr_High := Duplicate_Subexpr (Aggr_High);
2501 if Etype (Aggr_High) = Universal_Integer then
2502 Set_Analyzed (Aggr_High, False);
2503 end if;
2504 end if;
2506 -- If the aggregate already has bounds attached to it, it means this is
2507 -- a positional aggregate created as an optimization by
2508 -- Exp_Aggr.Convert_To_Positional, so we don't want to change those
2509 -- bounds.
2511 if Present (Aggregate_Bounds (N)) and then not Others_Allowed then
2512 Aggr_Low := Low_Bound (Aggregate_Bounds (N));
2513 Aggr_High := High_Bound (Aggregate_Bounds (N));
2514 end if;
2516 Set_Aggregate_Bounds
2517 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
2519 -- The bounds may contain expressions that must be inserted upwards.
2520 -- Attach them fully to the tree. After analysis, remove side effects
2521 -- from upper bound, if still needed.
2523 Set_Parent (Aggregate_Bounds (N), N);
2524 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
2525 Check_Unset_Reference (Aggregate_Bounds (N));
2527 if not Others_Present and then Nb_Discrete_Choices = 0 then
2528 Set_High_Bound (Aggregate_Bounds (N),
2529 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
2530 end if;
2532 -- Check the dimensions of each component in the array aggregate
2534 Analyze_Dimension_Array_Aggregate (N, Component_Typ);
2536 return Success;
2537 end Resolve_Array_Aggregate;
2539 ---------------------------------
2540 -- Resolve_Extension_Aggregate --
2541 ---------------------------------
2543 -- There are two cases to consider:
2545 -- a) If the ancestor part is a type mark, the components needed are the
2546 -- difference between the components of the expected type and the
2547 -- components of the given type mark.
2549 -- b) If the ancestor part is an expression, it must be unambiguous, and
2550 -- once we have its type we can also compute the needed components as in
2551 -- the previous case. In both cases, if the ancestor type is not the
2552 -- immediate ancestor, we have to build this ancestor recursively.
2554 -- In both cases, discriminants of the ancestor type do not play a role in
2555 -- the resolution of the needed components, because inherited discriminants
2556 -- cannot be used in a type extension. As a result we can compute
2557 -- independently the list of components of the ancestor type and of the
2558 -- expected type.
2560 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
2561 A : constant Node_Id := Ancestor_Part (N);
2562 A_Type : Entity_Id;
2563 I : Interp_Index;
2564 It : Interp;
2566 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean;
2567 -- If the type is limited, verify that the ancestor part is a legal
2568 -- expression (aggregate or function call, including 'Input)) that does
2569 -- not require a copy, as specified in 7.5(2).
2571 function Valid_Ancestor_Type return Boolean;
2572 -- Verify that the type of the ancestor part is a non-private ancestor
2573 -- of the expected type, which must be a type extension.
2575 ----------------------------
2576 -- Valid_Limited_Ancestor --
2577 ----------------------------
2579 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean is
2580 begin
2581 if Is_Entity_Name (Anc)
2582 and then Is_Type (Entity (Anc))
2583 then
2584 return True;
2586 elsif Nkind_In (Anc, N_Aggregate, N_Function_Call) then
2587 return True;
2589 elsif Nkind (Anc) = N_Attribute_Reference
2590 and then Attribute_Name (Anc) = Name_Input
2591 then
2592 return True;
2594 elsif Nkind (Anc) = N_Qualified_Expression then
2595 return Valid_Limited_Ancestor (Expression (Anc));
2597 else
2598 return False;
2599 end if;
2600 end Valid_Limited_Ancestor;
2602 -------------------------
2603 -- Valid_Ancestor_Type --
2604 -------------------------
2606 function Valid_Ancestor_Type return Boolean is
2607 Imm_Type : Entity_Id;
2609 begin
2610 Imm_Type := Base_Type (Typ);
2611 while Is_Derived_Type (Imm_Type) loop
2612 if Etype (Imm_Type) = Base_Type (A_Type) then
2613 return True;
2615 -- The base type of the parent type may appear as a private
2616 -- extension if it is declared as such in a parent unit of the
2617 -- current one. For consistency of the subsequent analysis use
2618 -- the partial view for the ancestor part.
2620 elsif Is_Private_Type (Etype (Imm_Type))
2621 and then Present (Full_View (Etype (Imm_Type)))
2622 and then Base_Type (A_Type) = Full_View (Etype (Imm_Type))
2623 then
2624 A_Type := Etype (Imm_Type);
2625 return True;
2627 -- The parent type may be a private extension. The aggregate is
2628 -- legal if the type of the aggregate is an extension of it that
2629 -- is not a private extension.
2631 elsif Is_Private_Type (A_Type)
2632 and then not Is_Private_Type (Imm_Type)
2633 and then Present (Full_View (A_Type))
2634 and then Base_Type (Full_View (A_Type)) = Etype (Imm_Type)
2635 then
2636 return True;
2638 else
2639 Imm_Type := Etype (Base_Type (Imm_Type));
2640 end if;
2641 end loop;
2643 -- If previous loop did not find a proper ancestor, report error
2645 Error_Msg_NE ("expect ancestor type of &", A, Typ);
2646 return False;
2647 end Valid_Ancestor_Type;
2649 -- Start of processing for Resolve_Extension_Aggregate
2651 begin
2652 -- Analyze the ancestor part and account for the case where it is a
2653 -- parameterless function call.
2655 Analyze (A);
2656 Check_Parameterless_Call (A);
2658 -- In SPARK, the ancestor part cannot be a type mark
2660 if Is_Entity_Name (A)
2661 and then Is_Type (Entity (A))
2662 then
2663 Check_SPARK_Restriction ("ancestor part cannot be a type mark", A);
2665 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
2666 -- must not have unknown discriminants.
2668 if Has_Unknown_Discriminants (Root_Type (Typ)) then
2669 Error_Msg_NE
2670 ("aggregate not available for type& whose ancestor "
2671 & "has unknown discriminants", N, Typ);
2672 end if;
2673 end if;
2675 if not Is_Tagged_Type (Typ) then
2676 Error_Msg_N ("type of extension aggregate must be tagged", N);
2677 return;
2679 elsif Is_Limited_Type (Typ) then
2681 -- Ada 2005 (AI-287): Limited aggregates are allowed
2683 if Ada_Version < Ada_2005 then
2684 Error_Msg_N ("aggregate type cannot be limited", N);
2685 Explain_Limited_Type (Typ, N);
2686 return;
2688 elsif Valid_Limited_Ancestor (A) then
2689 null;
2691 else
2692 Error_Msg_N
2693 ("limited ancestor part must be aggregate or function call", A);
2694 end if;
2696 elsif Is_Class_Wide_Type (Typ) then
2697 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
2698 return;
2699 end if;
2701 if Is_Entity_Name (A)
2702 and then Is_Type (Entity (A))
2703 then
2704 A_Type := Get_Full_View (Entity (A));
2706 if Valid_Ancestor_Type then
2707 Set_Entity (A, A_Type);
2708 Set_Etype (A, A_Type);
2710 Validate_Ancestor_Part (N);
2711 Resolve_Record_Aggregate (N, Typ);
2712 end if;
2714 elsif Nkind (A) /= N_Aggregate then
2715 if Is_Overloaded (A) then
2716 A_Type := Any_Type;
2718 Get_First_Interp (A, I, It);
2719 while Present (It.Typ) loop
2720 -- Only consider limited interpretations in the Ada 2005 case
2722 if Is_Tagged_Type (It.Typ)
2723 and then (Ada_Version >= Ada_2005
2724 or else not Is_Limited_Type (It.Typ))
2725 then
2726 if A_Type /= Any_Type then
2727 Error_Msg_N ("cannot resolve expression", A);
2728 return;
2729 else
2730 A_Type := It.Typ;
2731 end if;
2732 end if;
2734 Get_Next_Interp (I, It);
2735 end loop;
2737 if A_Type = Any_Type then
2738 if Ada_Version >= Ada_2005 then
2739 Error_Msg_N ("ancestor part must be of a tagged type", A);
2740 else
2741 Error_Msg_N
2742 ("ancestor part must be of a nonlimited tagged type", A);
2743 end if;
2745 return;
2746 end if;
2748 else
2749 A_Type := Etype (A);
2750 end if;
2752 if Valid_Ancestor_Type then
2753 Resolve (A, A_Type);
2754 Check_Unset_Reference (A);
2755 Check_Non_Static_Context (A);
2757 -- The aggregate is illegal if the ancestor expression is a call
2758 -- to a function with a limited unconstrained result, unless the
2759 -- type of the aggregate is a null extension. This restriction
2760 -- was added in AI05-67 to simplify implementation.
2762 if Nkind (A) = N_Function_Call
2763 and then Is_Limited_Type (A_Type)
2764 and then not Is_Null_Extension (Typ)
2765 and then not Is_Constrained (A_Type)
2766 then
2767 Error_Msg_N
2768 ("type of limited ancestor part must be constrained", A);
2770 -- Reject the use of CPP constructors that leave objects partially
2771 -- initialized. For example:
2773 -- type CPP_Root is tagged limited record ...
2774 -- pragma Import (CPP, CPP_Root);
2776 -- type CPP_DT is new CPP_Root and Iface ...
2777 -- pragma Import (CPP, CPP_DT);
2779 -- type Ada_DT is new CPP_DT with ...
2781 -- Obj : Ada_DT := Ada_DT'(New_CPP_Root with others => <>);
2783 -- Using the constructor of CPP_Root the slots of the dispatch
2784 -- table of CPP_DT cannot be set, and the secondary tag of
2785 -- CPP_DT is unknown.
2787 elsif Nkind (A) = N_Function_Call
2788 and then Is_CPP_Constructor_Call (A)
2789 and then Enclosing_CPP_Parent (Typ) /= A_Type
2790 then
2791 Error_Msg_NE
2792 ("??must use 'C'P'P constructor for type &", A,
2793 Enclosing_CPP_Parent (Typ));
2795 -- The following call is not needed if the previous warning
2796 -- is promoted to an error.
2798 Resolve_Record_Aggregate (N, Typ);
2800 elsif Is_Class_Wide_Type (Etype (A))
2801 and then Nkind (Original_Node (A)) = N_Function_Call
2802 then
2803 -- If the ancestor part is a dispatching call, it appears
2804 -- statically to be a legal ancestor, but it yields any member
2805 -- of the class, and it is not possible to determine whether
2806 -- it is an ancestor of the extension aggregate (much less
2807 -- which ancestor). It is not possible to determine the
2808 -- components of the extension part.
2810 -- This check implements AI-306, which in fact was motivated by
2811 -- an AdaCore query to the ARG after this test was added.
2813 Error_Msg_N ("ancestor part must be statically tagged", A);
2814 else
2815 Resolve_Record_Aggregate (N, Typ);
2816 end if;
2817 end if;
2819 else
2820 Error_Msg_N ("no unique type for this aggregate", A);
2821 end if;
2823 Check_Function_Writable_Actuals (N);
2824 end Resolve_Extension_Aggregate;
2826 ------------------------------
2827 -- Resolve_Record_Aggregate --
2828 ------------------------------
2830 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
2831 Assoc : Node_Id;
2832 -- N_Component_Association node belonging to the input aggregate N
2834 Expr : Node_Id;
2835 Positional_Expr : Node_Id;
2836 Component : Entity_Id;
2837 Component_Elmt : Elmt_Id;
2839 Components : constant Elist_Id := New_Elmt_List;
2840 -- Components is the list of the record components whose value must be
2841 -- provided in the aggregate. This list does include discriminants.
2843 New_Assoc_List : constant List_Id := New_List;
2844 New_Assoc : Node_Id;
2845 -- New_Assoc_List is the newly built list of N_Component_Association
2846 -- nodes. New_Assoc is one such N_Component_Association node in it.
2847 -- Note that while Assoc and New_Assoc contain the same kind of nodes,
2848 -- they are used to iterate over two different N_Component_Association
2849 -- lists.
2851 Others_Etype : Entity_Id := Empty;
2852 -- This variable is used to save the Etype of the last record component
2853 -- that takes its value from the others choice. Its purpose is:
2855 -- (a) make sure the others choice is useful
2857 -- (b) make sure the type of all the components whose value is
2858 -- subsumed by the others choice are the same.
2860 -- This variable is updated as a side effect of function Get_Value.
2862 Is_Box_Present : Boolean := False;
2863 Others_Box : Boolean := False;
2864 -- Ada 2005 (AI-287): Variables used in case of default initialization
2865 -- to provide a functionality similar to Others_Etype. Box_Present
2866 -- indicates that the component takes its default initialization;
2867 -- Others_Box indicates that at least one component takes its default
2868 -- initialization. Similar to Others_Etype, they are also updated as a
2869 -- side effect of function Get_Value.
2871 procedure Add_Association
2872 (Component : Entity_Id;
2873 Expr : Node_Id;
2874 Assoc_List : List_Id;
2875 Is_Box_Present : Boolean := False);
2876 -- Builds a new N_Component_Association node which associates Component
2877 -- to expression Expr and adds it to the association list being built,
2878 -- either New_Assoc_List, or the association being built for an inner
2879 -- aggregate.
2881 function Discr_Present (Discr : Entity_Id) return Boolean;
2882 -- If aggregate N is a regular aggregate this routine will return True.
2883 -- Otherwise, if N is an extension aggregate, Discr is a discriminant
2884 -- whose value may already have been specified by N's ancestor part.
2885 -- This routine checks whether this is indeed the case and if so returns
2886 -- False, signaling that no value for Discr should appear in N's
2887 -- aggregate part. Also, in this case, the routine appends to
2888 -- New_Assoc_List the discriminant value specified in the ancestor part.
2890 -- If the aggregate is in a context with expansion delayed, it will be
2891 -- reanalyzed. The inherited discriminant values must not be reinserted
2892 -- in the component list to prevent spurious errors, but they must be
2893 -- present on first analysis to build the proper subtype indications.
2894 -- The flag Inherited_Discriminant is used to prevent the re-insertion.
2896 function Get_Value
2897 (Compon : Node_Id;
2898 From : List_Id;
2899 Consider_Others_Choice : Boolean := False)
2900 return Node_Id;
2901 -- Given a record component stored in parameter Compon, this function
2902 -- returns its value as it appears in the list From, which is a list
2903 -- of N_Component_Association nodes.
2905 -- If no component association has a choice for the searched component,
2906 -- the value provided by the others choice is returned, if there is one,
2907 -- and Consider_Others_Choice is set to true. Otherwise Empty is
2908 -- returned. If there is more than one component association giving a
2909 -- value for the searched record component, an error message is emitted
2910 -- and the first found value is returned.
2912 -- If Consider_Others_Choice is set and the returned expression comes
2913 -- from the others choice, then Others_Etype is set as a side effect.
2914 -- An error message is emitted if the components taking their value from
2915 -- the others choice do not have same type.
2917 function New_Copy_Tree_And_Copy_Dimensions
2918 (Source : Node_Id;
2919 Map : Elist_Id := No_Elist;
2920 New_Sloc : Source_Ptr := No_Location;
2921 New_Scope : Entity_Id := Empty) return Node_Id;
2922 -- Same as New_Copy_Tree (defined in Sem_Util), except that this routine
2923 -- also copies the dimensions of Source to the returned node.
2925 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id);
2926 -- Analyzes and resolves expression Expr against the Etype of the
2927 -- Component. This routine also applies all appropriate checks to Expr.
2928 -- It finally saves a Expr in the newly created association list that
2929 -- will be attached to the final record aggregate. Note that if the
2930 -- Parent pointer of Expr is not set then Expr was produced with a
2931 -- New_Copy_Tree or some such.
2933 ---------------------
2934 -- Add_Association --
2935 ---------------------
2937 procedure Add_Association
2938 (Component : Entity_Id;
2939 Expr : Node_Id;
2940 Assoc_List : List_Id;
2941 Is_Box_Present : Boolean := False)
2943 Loc : Source_Ptr;
2944 Choice_List : constant List_Id := New_List;
2945 New_Assoc : Node_Id;
2947 begin
2948 -- If this is a box association the expression is missing, so
2949 -- use the Sloc of the aggregate itself for the new association.
2951 if Present (Expr) then
2952 Loc := Sloc (Expr);
2953 else
2954 Loc := Sloc (N);
2955 end if;
2957 Append (New_Occurrence_Of (Component, Loc), Choice_List);
2958 New_Assoc :=
2959 Make_Component_Association (Loc,
2960 Choices => Choice_List,
2961 Expression => Expr,
2962 Box_Present => Is_Box_Present);
2963 Append (New_Assoc, Assoc_List);
2964 end Add_Association;
2966 -------------------
2967 -- Discr_Present --
2968 -------------------
2970 function Discr_Present (Discr : Entity_Id) return Boolean is
2971 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
2973 Loc : Source_Ptr;
2975 Ancestor : Node_Id;
2976 Comp_Assoc : Node_Id;
2977 Discr_Expr : Node_Id;
2979 Ancestor_Typ : Entity_Id;
2980 Orig_Discr : Entity_Id;
2981 D : Entity_Id;
2982 D_Val : Elmt_Id := No_Elmt; -- stop junk warning
2984 Ancestor_Is_Subtyp : Boolean;
2986 begin
2987 if Regular_Aggr then
2988 return True;
2989 end if;
2991 -- Check whether inherited discriminant values have already been
2992 -- inserted in the aggregate. This will be the case if we are
2993 -- re-analyzing an aggregate whose expansion was delayed.
2995 if Present (Component_Associations (N)) then
2996 Comp_Assoc := First (Component_Associations (N));
2997 while Present (Comp_Assoc) loop
2998 if Inherited_Discriminant (Comp_Assoc) then
2999 return True;
3000 end if;
3002 Next (Comp_Assoc);
3003 end loop;
3004 end if;
3006 Ancestor := Ancestor_Part (N);
3007 Ancestor_Typ := Etype (Ancestor);
3008 Loc := Sloc (Ancestor);
3010 -- For a private type with unknown discriminants, use the underlying
3011 -- record view if it is available.
3013 if Has_Unknown_Discriminants (Ancestor_Typ)
3014 and then Present (Full_View (Ancestor_Typ))
3015 and then Present (Underlying_Record_View (Full_View (Ancestor_Typ)))
3016 then
3017 Ancestor_Typ := Underlying_Record_View (Full_View (Ancestor_Typ));
3018 end if;
3020 Ancestor_Is_Subtyp :=
3021 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
3023 -- If the ancestor part has no discriminants clearly N's aggregate
3024 -- part must provide a value for Discr.
3026 if not Has_Discriminants (Ancestor_Typ) then
3027 return True;
3029 -- If the ancestor part is an unconstrained subtype mark then the
3030 -- Discr must be present in N's aggregate part.
3032 elsif Ancestor_Is_Subtyp
3033 and then not Is_Constrained (Entity (Ancestor))
3034 then
3035 return True;
3036 end if;
3038 -- Now look to see if Discr was specified in the ancestor part
3040 if Ancestor_Is_Subtyp then
3041 D_Val := First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
3042 end if;
3044 Orig_Discr := Original_Record_Component (Discr);
3046 D := First_Discriminant (Ancestor_Typ);
3047 while Present (D) loop
3049 -- If Ancestor has already specified Disc value then insert its
3050 -- value in the final aggregate.
3052 if Original_Record_Component (D) = Orig_Discr then
3053 if Ancestor_Is_Subtyp then
3054 Discr_Expr := New_Copy_Tree (Node (D_Val));
3055 else
3056 Discr_Expr :=
3057 Make_Selected_Component (Loc,
3058 Prefix => Duplicate_Subexpr (Ancestor),
3059 Selector_Name => New_Occurrence_Of (Discr, Loc));
3060 end if;
3062 Resolve_Aggr_Expr (Discr_Expr, Discr);
3063 Set_Inherited_Discriminant (Last (New_Assoc_List));
3064 return False;
3065 end if;
3067 Next_Discriminant (D);
3069 if Ancestor_Is_Subtyp then
3070 Next_Elmt (D_Val);
3071 end if;
3072 end loop;
3074 return True;
3075 end Discr_Present;
3077 ---------------
3078 -- Get_Value --
3079 ---------------
3081 function Get_Value
3082 (Compon : Node_Id;
3083 From : List_Id;
3084 Consider_Others_Choice : Boolean := False)
3085 return Node_Id
3087 Assoc : Node_Id;
3088 Expr : Node_Id := Empty;
3089 Selector_Name : Node_Id;
3091 begin
3092 Is_Box_Present := False;
3094 if Present (From) then
3095 Assoc := First (From);
3096 else
3097 return Empty;
3098 end if;
3100 while Present (Assoc) loop
3101 Selector_Name := First (Choices (Assoc));
3102 while Present (Selector_Name) loop
3103 if Nkind (Selector_Name) = N_Others_Choice then
3104 if Consider_Others_Choice and then No (Expr) then
3106 -- We need to duplicate the expression for each
3107 -- successive component covered by the others choice.
3108 -- This is redundant if the others_choice covers only
3109 -- one component (small optimization possible???), but
3110 -- indispensable otherwise, because each one must be
3111 -- expanded individually to preserve side-effects.
3113 -- Ada 2005 (AI-287): In case of default initialization
3114 -- of components, we duplicate the corresponding default
3115 -- expression (from the record type declaration). The
3116 -- copy must carry the sloc of the association (not the
3117 -- original expression) to prevent spurious elaboration
3118 -- checks when the default includes function calls.
3120 if Box_Present (Assoc) then
3121 Others_Box := True;
3122 Is_Box_Present := True;
3124 if Expander_Active then
3125 return
3126 New_Copy_Tree_And_Copy_Dimensions
3127 (Expression (Parent (Compon)),
3128 New_Sloc => Sloc (Assoc));
3129 else
3130 return Expression (Parent (Compon));
3131 end if;
3133 else
3134 if Present (Others_Etype) and then
3135 Base_Type (Others_Etype) /= Base_Type (Etype
3136 (Compon))
3137 then
3138 Error_Msg_N ("components in OTHERS choice must " &
3139 "have same type", Selector_Name);
3140 end if;
3142 Others_Etype := Etype (Compon);
3144 if Expander_Active then
3145 return
3146 New_Copy_Tree_And_Copy_Dimensions
3147 (Expression (Assoc));
3148 else
3149 return Expression (Assoc);
3150 end if;
3151 end if;
3152 end if;
3154 elsif Chars (Compon) = Chars (Selector_Name) then
3155 if No (Expr) then
3157 -- Ada 2005 (AI-231)
3159 if Ada_Version >= Ada_2005
3160 and then Known_Null (Expression (Assoc))
3161 then
3162 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
3163 end if;
3165 -- We need to duplicate the expression when several
3166 -- components are grouped together with a "|" choice.
3167 -- For instance "filed1 | filed2 => Expr"
3169 -- Ada 2005 (AI-287)
3171 if Box_Present (Assoc) then
3172 Is_Box_Present := True;
3174 -- Duplicate the default expression of the component
3175 -- from the record type declaration, so a new copy
3176 -- can be attached to the association.
3178 -- Note that we always copy the default expression,
3179 -- even when the association has a single choice, in
3180 -- order to create a proper association for the
3181 -- expanded aggregate.
3183 -- Component may have no default, in which case the
3184 -- expression is empty and the component is default-
3185 -- initialized, but an association for the component
3186 -- exists, and it is not covered by an others clause.
3188 return
3189 New_Copy_Tree_And_Copy_Dimensions
3190 (Expression (Parent (Compon)));
3192 else
3193 if Present (Next (Selector_Name)) then
3194 Expr :=
3195 New_Copy_Tree_And_Copy_Dimensions
3196 (Expression (Assoc));
3197 else
3198 Expr := Expression (Assoc);
3199 end if;
3200 end if;
3202 Generate_Reference (Compon, Selector_Name, 'm');
3204 else
3205 Error_Msg_NE
3206 ("more than one value supplied for &",
3207 Selector_Name, Compon);
3209 end if;
3210 end if;
3212 Next (Selector_Name);
3213 end loop;
3215 Next (Assoc);
3216 end loop;
3218 return Expr;
3219 end Get_Value;
3221 ---------------------------------------
3222 -- New_Copy_Tree_And_Copy_Dimensions --
3223 ---------------------------------------
3225 function New_Copy_Tree_And_Copy_Dimensions
3226 (Source : Node_Id;
3227 Map : Elist_Id := No_Elist;
3228 New_Sloc : Source_Ptr := No_Location;
3229 New_Scope : Entity_Id := Empty) return Node_Id
3231 New_Copy : constant Node_Id :=
3232 New_Copy_Tree (Source, Map, New_Sloc, New_Scope);
3233 begin
3234 -- Move the dimensions of Source to New_Copy
3236 Copy_Dimensions (Source, New_Copy);
3237 return New_Copy;
3238 end New_Copy_Tree_And_Copy_Dimensions;
3240 -----------------------
3241 -- Resolve_Aggr_Expr --
3242 -----------------------
3244 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id) is
3245 Expr_Type : Entity_Id := Empty;
3246 New_C : Entity_Id := Component;
3247 New_Expr : Node_Id;
3249 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
3250 -- If the expression is an aggregate (possibly qualified) then its
3251 -- expansion is delayed until the enclosing aggregate is expanded
3252 -- into assignments. In that case, do not generate checks on the
3253 -- expression, because they will be generated later, and will other-
3254 -- wise force a copy (to remove side-effects) that would leave a
3255 -- dynamic-sized aggregate in the code, something that gigi cannot
3256 -- handle.
3258 Relocate : Boolean;
3259 -- Set to True if the resolved Expr node needs to be relocated when
3260 -- attached to the newly created association list. This node need not
3261 -- be relocated if its parent pointer is not set. In fact in this
3262 -- case Expr is the output of a New_Copy_Tree call. If Relocate is
3263 -- True then we have analyzed the expression node in the original
3264 -- aggregate and hence it needs to be relocated when moved over to
3265 -- the new association list.
3267 ---------------------------
3268 -- Has_Expansion_Delayed --
3269 ---------------------------
3271 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
3272 Kind : constant Node_Kind := Nkind (Expr);
3273 begin
3274 return (Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate)
3275 and then Present (Etype (Expr))
3276 and then Is_Record_Type (Etype (Expr))
3277 and then Expansion_Delayed (Expr))
3278 or else (Kind = N_Qualified_Expression
3279 and then Has_Expansion_Delayed (Expression (Expr)));
3280 end Has_Expansion_Delayed;
3282 -- Start of processing for Resolve_Aggr_Expr
3284 begin
3285 -- If the type of the component is elementary or the type of the
3286 -- aggregate does not contain discriminants, use the type of the
3287 -- component to resolve Expr.
3289 if Is_Elementary_Type (Etype (Component))
3290 or else not Has_Discriminants (Etype (N))
3291 then
3292 Expr_Type := Etype (Component);
3294 -- Otherwise we have to pick up the new type of the component from
3295 -- the new constrained subtype of the aggregate. In fact components
3296 -- which are of a composite type might be constrained by a
3297 -- discriminant, and we want to resolve Expr against the subtype were
3298 -- all discriminant occurrences are replaced with their actual value.
3300 else
3301 New_C := First_Component (Etype (N));
3302 while Present (New_C) loop
3303 if Chars (New_C) = Chars (Component) then
3304 Expr_Type := Etype (New_C);
3305 exit;
3306 end if;
3308 Next_Component (New_C);
3309 end loop;
3311 pragma Assert (Present (Expr_Type));
3313 -- For each range in an array type where a discriminant has been
3314 -- replaced with the constraint, check that this range is within
3315 -- the range of the base type. This checks is done in the init
3316 -- proc for regular objects, but has to be done here for
3317 -- aggregates since no init proc is called for them.
3319 if Is_Array_Type (Expr_Type) then
3320 declare
3321 Index : Node_Id;
3322 -- Range of the current constrained index in the array
3324 Orig_Index : Node_Id := First_Index (Etype (Component));
3325 -- Range corresponding to the range Index above in the
3326 -- original unconstrained record type. The bounds of this
3327 -- range may be governed by discriminants.
3329 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
3330 -- Range corresponding to the range Index above for the
3331 -- unconstrained array type. This range is needed to apply
3332 -- range checks.
3334 begin
3335 Index := First_Index (Expr_Type);
3336 while Present (Index) loop
3337 if Depends_On_Discriminant (Orig_Index) then
3338 Apply_Range_Check (Index, Etype (Unconstr_Index));
3339 end if;
3341 Next_Index (Index);
3342 Next_Index (Orig_Index);
3343 Next_Index (Unconstr_Index);
3344 end loop;
3345 end;
3346 end if;
3347 end if;
3349 -- If the Parent pointer of Expr is not set, Expr is an expression
3350 -- duplicated by New_Tree_Copy (this happens for record aggregates
3351 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
3352 -- Such a duplicated expression must be attached to the tree
3353 -- before analysis and resolution to enforce the rule that a tree
3354 -- fragment should never be analyzed or resolved unless it is
3355 -- attached to the current compilation unit.
3357 if No (Parent (Expr)) then
3358 Set_Parent (Expr, N);
3359 Relocate := False;
3360 else
3361 Relocate := True;
3362 end if;
3364 Analyze_And_Resolve (Expr, Expr_Type);
3365 Check_Expr_OK_In_Limited_Aggregate (Expr);
3366 Check_Non_Static_Context (Expr);
3367 Check_Unset_Reference (Expr);
3369 -- Check wrong use of class-wide types
3371 if Is_Class_Wide_Type (Etype (Expr)) then
3372 Error_Msg_N ("dynamically tagged expression not allowed", Expr);
3373 end if;
3375 if not Has_Expansion_Delayed (Expr) then
3376 Aggregate_Constraint_Checks (Expr, Expr_Type);
3377 end if;
3379 -- If an aggregate component has a type with predicates, an explicit
3380 -- predicate check must be applied, as for an assignment statement,
3381 -- because the aggegate might not be expanded into individual
3382 -- component assignments.
3384 if Present (Predicate_Function (Expr_Type)) then
3385 Apply_Predicate_Check (Expr, Expr_Type);
3386 end if;
3388 if Raises_Constraint_Error (Expr) then
3389 Set_Raises_Constraint_Error (N);
3390 end if;
3392 -- If the expression has been marked as requiring a range check, then
3393 -- generate it here.
3395 if Do_Range_Check (Expr) then
3396 Set_Do_Range_Check (Expr, False);
3397 Generate_Range_Check (Expr, Expr_Type, CE_Range_Check_Failed);
3398 end if;
3400 if Relocate then
3401 New_Expr := Relocate_Node (Expr);
3403 -- Since New_Expr is not gonna be analyzed later on, we need to
3404 -- propagate here the dimensions form Expr to New_Expr.
3406 Copy_Dimensions (Expr, New_Expr);
3408 else
3409 New_Expr := Expr;
3410 end if;
3412 Add_Association (New_C, New_Expr, New_Assoc_List);
3413 end Resolve_Aggr_Expr;
3415 -- Start of processing for Resolve_Record_Aggregate
3417 begin
3418 -- A record aggregate is restricted in SPARK:
3419 -- Each named association can have only a single choice.
3420 -- OTHERS cannot be used.
3421 -- Positional and named associations cannot be mixed.
3423 if Present (Component_Associations (N))
3424 and then Present (First (Component_Associations (N)))
3425 then
3427 if Present (Expressions (N)) then
3428 Check_SPARK_Restriction
3429 ("named association cannot follow positional one",
3430 First (Choices (First (Component_Associations (N)))));
3431 end if;
3433 declare
3434 Assoc : Node_Id;
3436 begin
3437 Assoc := First (Component_Associations (N));
3438 while Present (Assoc) loop
3439 if List_Length (Choices (Assoc)) > 1 then
3440 Check_SPARK_Restriction
3441 ("component association in record aggregate must "
3442 & "contain a single choice", Assoc);
3443 end if;
3445 if Nkind (First (Choices (Assoc))) = N_Others_Choice then
3446 Check_SPARK_Restriction
3447 ("record aggregate cannot contain OTHERS", Assoc);
3448 end if;
3450 Assoc := Next (Assoc);
3451 end loop;
3452 end;
3453 end if;
3455 -- We may end up calling Duplicate_Subexpr on expressions that are
3456 -- attached to New_Assoc_List. For this reason we need to attach it
3457 -- to the tree by setting its parent pointer to N. This parent point
3458 -- will change in STEP 8 below.
3460 Set_Parent (New_Assoc_List, N);
3462 -- STEP 1: abstract type and null record verification
3464 if Is_Abstract_Type (Typ) then
3465 Error_Msg_N ("type of aggregate cannot be abstract", N);
3466 end if;
3468 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
3469 Set_Etype (N, Typ);
3470 return;
3472 elsif Present (First_Entity (Typ))
3473 and then Null_Record_Present (N)
3474 and then not Is_Tagged_Type (Typ)
3475 then
3476 Error_Msg_N ("record aggregate cannot be null", N);
3477 return;
3479 -- If the type has no components, then the aggregate should either
3480 -- have "null record", or in Ada 2005 it could instead have a single
3481 -- component association given by "others => <>". For Ada 95 we flag an
3482 -- error at this point, but for Ada 2005 we proceed with checking the
3483 -- associations below, which will catch the case where it's not an
3484 -- aggregate with "others => <>". Note that the legality of a <>
3485 -- aggregate for a null record type was established by AI05-016.
3487 elsif No (First_Entity (Typ))
3488 and then Ada_Version < Ada_2005
3489 then
3490 Error_Msg_N ("record aggregate must be null", N);
3491 return;
3492 end if;
3494 -- STEP 2: Verify aggregate structure
3496 Step_2 : declare
3497 Selector_Name : Node_Id;
3498 Bad_Aggregate : Boolean := False;
3500 begin
3501 if Present (Component_Associations (N)) then
3502 Assoc := First (Component_Associations (N));
3503 else
3504 Assoc := Empty;
3505 end if;
3507 while Present (Assoc) loop
3508 Selector_Name := First (Choices (Assoc));
3509 while Present (Selector_Name) loop
3510 if Nkind (Selector_Name) = N_Identifier then
3511 null;
3513 elsif Nkind (Selector_Name) = N_Others_Choice then
3514 if Selector_Name /= First (Choices (Assoc))
3515 or else Present (Next (Selector_Name))
3516 then
3517 Error_Msg_N
3518 ("OTHERS must appear alone in a choice list",
3519 Selector_Name);
3520 return;
3522 elsif Present (Next (Assoc)) then
3523 Error_Msg_N
3524 ("OTHERS must appear last in an aggregate",
3525 Selector_Name);
3526 return;
3528 -- (Ada 2005): If this is an association with a box,
3529 -- indicate that the association need not represent
3530 -- any component.
3532 elsif Box_Present (Assoc) then
3533 Others_Box := True;
3534 end if;
3536 else
3537 Error_Msg_N
3538 ("selector name should be identifier or OTHERS",
3539 Selector_Name);
3540 Bad_Aggregate := True;
3541 end if;
3543 Next (Selector_Name);
3544 end loop;
3546 Next (Assoc);
3547 end loop;
3549 if Bad_Aggregate then
3550 return;
3551 end if;
3552 end Step_2;
3554 -- STEP 3: Find discriminant Values
3556 Step_3 : declare
3557 Discrim : Entity_Id;
3558 Missing_Discriminants : Boolean := False;
3560 begin
3561 if Present (Expressions (N)) then
3562 Positional_Expr := First (Expressions (N));
3563 else
3564 Positional_Expr := Empty;
3565 end if;
3567 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
3568 -- must not have unknown discriminants.
3570 if Is_Derived_Type (Typ)
3571 and then Has_Unknown_Discriminants (Root_Type (Typ))
3572 and then Nkind (N) /= N_Extension_Aggregate
3573 then
3574 Error_Msg_NE
3575 ("aggregate not available for type& whose ancestor "
3576 & "has unknown discriminants ", N, Typ);
3577 end if;
3579 if Has_Unknown_Discriminants (Typ)
3580 and then Present (Underlying_Record_View (Typ))
3581 then
3582 Discrim := First_Discriminant (Underlying_Record_View (Typ));
3583 elsif Has_Discriminants (Typ) then
3584 Discrim := First_Discriminant (Typ);
3585 else
3586 Discrim := Empty;
3587 end if;
3589 -- First find the discriminant values in the positional components
3591 while Present (Discrim) and then Present (Positional_Expr) loop
3592 if Discr_Present (Discrim) then
3593 Resolve_Aggr_Expr (Positional_Expr, Discrim);
3595 -- Ada 2005 (AI-231)
3597 if Ada_Version >= Ada_2005
3598 and then Known_Null (Positional_Expr)
3599 then
3600 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
3601 end if;
3603 Next (Positional_Expr);
3604 end if;
3606 if Present (Get_Value (Discrim, Component_Associations (N))) then
3607 Error_Msg_NE
3608 ("more than one value supplied for discriminant&",
3609 N, Discrim);
3610 end if;
3612 Next_Discriminant (Discrim);
3613 end loop;
3615 -- Find remaining discriminant values if any among named components
3617 while Present (Discrim) loop
3618 Expr := Get_Value (Discrim, Component_Associations (N), True);
3620 if not Discr_Present (Discrim) then
3621 if Present (Expr) then
3622 Error_Msg_NE
3623 ("more than one value supplied for discriminant&",
3624 N, Discrim);
3625 end if;
3627 elsif No (Expr) then
3628 Error_Msg_NE
3629 ("no value supplied for discriminant &", N, Discrim);
3630 Missing_Discriminants := True;
3632 else
3633 Resolve_Aggr_Expr (Expr, Discrim);
3634 end if;
3636 Next_Discriminant (Discrim);
3637 end loop;
3639 if Missing_Discriminants then
3640 return;
3641 end if;
3643 -- At this point and until the beginning of STEP 6, New_Assoc_List
3644 -- contains only the discriminants and their values.
3646 end Step_3;
3648 -- STEP 4: Set the Etype of the record aggregate
3650 -- ??? This code is pretty much a copy of Sem_Ch3.Build_Subtype. That
3651 -- routine should really be exported in sem_util or some such and used
3652 -- in sem_ch3 and here rather than have a copy of the code which is a
3653 -- maintenance nightmare.
3655 -- ??? Performance WARNING. The current implementation creates a new
3656 -- itype for all aggregates whose base type is discriminated. This means
3657 -- that for record aggregates nested inside an array aggregate we will
3658 -- create a new itype for each record aggregate if the array component
3659 -- type has discriminants. For large aggregates this may be a problem.
3660 -- What should be done in this case is to reuse itypes as much as
3661 -- possible.
3663 if Has_Discriminants (Typ)
3664 or else (Has_Unknown_Discriminants (Typ)
3665 and then Present (Underlying_Record_View (Typ)))
3666 then
3667 Build_Constrained_Itype : declare
3668 Loc : constant Source_Ptr := Sloc (N);
3669 Indic : Node_Id;
3670 Subtyp_Decl : Node_Id;
3671 Def_Id : Entity_Id;
3673 C : constant List_Id := New_List;
3675 begin
3676 New_Assoc := First (New_Assoc_List);
3677 while Present (New_Assoc) loop
3678 Append (Duplicate_Subexpr (Expression (New_Assoc)), To => C);
3679 Next (New_Assoc);
3680 end loop;
3682 if Has_Unknown_Discriminants (Typ)
3683 and then Present (Underlying_Record_View (Typ))
3684 then
3685 Indic :=
3686 Make_Subtype_Indication (Loc,
3687 Subtype_Mark =>
3688 New_Occurrence_Of (Underlying_Record_View (Typ), Loc),
3689 Constraint =>
3690 Make_Index_Or_Discriminant_Constraint (Loc, C));
3691 else
3692 Indic :=
3693 Make_Subtype_Indication (Loc,
3694 Subtype_Mark =>
3695 New_Occurrence_Of (Base_Type (Typ), Loc),
3696 Constraint =>
3697 Make_Index_Or_Discriminant_Constraint (Loc, C));
3698 end if;
3700 Def_Id := Create_Itype (Ekind (Typ), N);
3702 Subtyp_Decl :=
3703 Make_Subtype_Declaration (Loc,
3704 Defining_Identifier => Def_Id,
3705 Subtype_Indication => Indic);
3706 Set_Parent (Subtyp_Decl, Parent (N));
3708 -- Itypes must be analyzed with checks off (see itypes.ads)
3710 Analyze (Subtyp_Decl, Suppress => All_Checks);
3712 Set_Etype (N, Def_Id);
3713 Check_Static_Discriminated_Subtype
3714 (Def_Id, Expression (First (New_Assoc_List)));
3715 end Build_Constrained_Itype;
3717 else
3718 Set_Etype (N, Typ);
3719 end if;
3721 -- STEP 5: Get remaining components according to discriminant values
3723 Step_5 : declare
3724 Record_Def : Node_Id;
3725 Parent_Typ : Entity_Id;
3726 Root_Typ : Entity_Id;
3727 Parent_Typ_List : Elist_Id;
3728 Parent_Elmt : Elmt_Id;
3729 Errors_Found : Boolean := False;
3730 Dnode : Node_Id;
3732 function Find_Private_Ancestor return Entity_Id;
3733 -- AI05-0115: Find earlier ancestor in the derivation chain that is
3734 -- derived from a private view. Whether the aggregate is legal
3735 -- depends on the current visibility of the type as well as that
3736 -- of the parent of the ancestor.
3738 ---------------------------
3739 -- Find_Private_Ancestor --
3740 ---------------------------
3742 function Find_Private_Ancestor return Entity_Id is
3743 Par : Entity_Id;
3744 begin
3745 Par := Typ;
3746 loop
3747 if Has_Private_Ancestor (Par)
3748 and then not Has_Private_Ancestor (Etype (Base_Type (Par)))
3749 then
3750 return Par;
3752 elsif not Is_Derived_Type (Par) then
3753 return Empty;
3755 else
3756 Par := Etype (Base_Type (Par));
3757 end if;
3758 end loop;
3759 end Find_Private_Ancestor;
3761 begin
3762 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
3763 Parent_Typ_List := New_Elmt_List;
3765 -- If this is an extension aggregate, the component list must
3766 -- include all components that are not in the given ancestor type.
3767 -- Otherwise, the component list must include components of all
3768 -- ancestors, starting with the root.
3770 if Nkind (N) = N_Extension_Aggregate then
3771 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
3773 else
3774 -- AI05-0115: check legality of aggregate for type with
3775 -- aa private ancestor.
3777 Root_Typ := Root_Type (Typ);
3778 if Has_Private_Ancestor (Typ) then
3779 declare
3780 Ancestor : constant Entity_Id :=
3781 Find_Private_Ancestor;
3782 Ancestor_Unit : constant Entity_Id :=
3783 Cunit_Entity (Get_Source_Unit (Ancestor));
3784 Parent_Unit : constant Entity_Id :=
3785 Cunit_Entity
3786 (Get_Source_Unit (Base_Type (Etype (Ancestor))));
3787 begin
3789 -- check whether we are in a scope that has full view
3790 -- over the private ancestor and its parent. This can
3791 -- only happen if the derivation takes place in a child
3792 -- unit of the unit that declares the parent, and we are
3793 -- in the private part or body of that child unit, else
3794 -- the aggregate is illegal.
3796 if Is_Child_Unit (Ancestor_Unit)
3797 and then Scope (Ancestor_Unit) = Parent_Unit
3798 and then In_Open_Scopes (Scope (Ancestor))
3799 and then
3800 (In_Private_Part (Scope (Ancestor))
3801 or else In_Package_Body (Scope (Ancestor)))
3802 then
3803 null;
3805 else
3806 Error_Msg_NE
3807 ("type of aggregate has private ancestor&!",
3808 N, Root_Typ);
3809 Error_Msg_N ("must use extension aggregate!", N);
3810 return;
3811 end if;
3812 end;
3813 end if;
3815 Dnode := Declaration_Node (Base_Type (Root_Typ));
3817 -- If we don't get a full declaration, then we have some error
3818 -- which will get signalled later so skip this part. Otherwise
3819 -- gather components of root that apply to the aggregate type.
3820 -- We use the base type in case there is an applicable stored
3821 -- constraint that renames the discriminants of the root.
3823 if Nkind (Dnode) = N_Full_Type_Declaration then
3824 Record_Def := Type_Definition (Dnode);
3825 Gather_Components (Base_Type (Typ),
3826 Component_List (Record_Def),
3827 Governed_By => New_Assoc_List,
3828 Into => Components,
3829 Report_Errors => Errors_Found);
3830 end if;
3831 end if;
3833 Parent_Typ := Base_Type (Typ);
3834 while Parent_Typ /= Root_Typ loop
3835 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
3836 Parent_Typ := Etype (Parent_Typ);
3838 if Nkind (Parent (Base_Type (Parent_Typ))) =
3839 N_Private_Type_Declaration
3840 or else Nkind (Parent (Base_Type (Parent_Typ))) =
3841 N_Private_Extension_Declaration
3842 then
3843 if Nkind (N) /= N_Extension_Aggregate then
3844 Error_Msg_NE
3845 ("type of aggregate has private ancestor&!",
3846 N, Parent_Typ);
3847 Error_Msg_N ("must use extension aggregate!", N);
3848 return;
3850 elsif Parent_Typ /= Root_Typ then
3851 Error_Msg_NE
3852 ("ancestor part of aggregate must be private type&",
3853 Ancestor_Part (N), Parent_Typ);
3854 return;
3855 end if;
3857 -- The current view of ancestor part may be a private type,
3858 -- while the context type is always non-private.
3860 elsif Is_Private_Type (Root_Typ)
3861 and then Present (Full_View (Root_Typ))
3862 and then Nkind (N) = N_Extension_Aggregate
3863 then
3864 exit when Base_Type (Full_View (Root_Typ)) = Parent_Typ;
3865 end if;
3866 end loop;
3868 -- Now collect components from all other ancestors, beginning
3869 -- with the current type. If the type has unknown discriminants
3870 -- use the component list of the Underlying_Record_View, which
3871 -- needs to be used for the subsequent expansion of the aggregate
3872 -- into assignments.
3874 Parent_Elmt := First_Elmt (Parent_Typ_List);
3875 while Present (Parent_Elmt) loop
3876 Parent_Typ := Node (Parent_Elmt);
3878 if Has_Unknown_Discriminants (Parent_Typ)
3879 and then Present (Underlying_Record_View (Typ))
3880 then
3881 Parent_Typ := Underlying_Record_View (Parent_Typ);
3882 end if;
3884 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
3885 Gather_Components (Empty,
3886 Component_List (Record_Extension_Part (Record_Def)),
3887 Governed_By => New_Assoc_List,
3888 Into => Components,
3889 Report_Errors => Errors_Found);
3891 Next_Elmt (Parent_Elmt);
3892 end loop;
3894 -- Typ is not a derived tagged type
3896 else
3897 -- A type derived from an untagged private type whose full view
3898 -- has discriminants is constructed as a record type but there
3899 -- are no legal aggregates for it.
3901 if Is_Derived_Type (Typ)
3902 and then Has_Private_Ancestor (Typ)
3903 and then Nkind (N) /= N_Extension_Aggregate
3904 then
3905 Error_Msg_Node_2 := Base_Type (Etype (Typ));
3906 Error_Msg_NE
3907 ("no aggregate available for type& derived from "
3908 & "private type&", N, Typ);
3909 return;
3910 end if;
3912 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
3914 if Null_Present (Record_Def) then
3915 null;
3917 elsif not Has_Unknown_Discriminants (Typ) then
3918 Gather_Components (Base_Type (Typ),
3919 Component_List (Record_Def),
3920 Governed_By => New_Assoc_List,
3921 Into => Components,
3922 Report_Errors => Errors_Found);
3924 else
3925 Gather_Components
3926 (Base_Type (Underlying_Record_View (Typ)),
3927 Component_List (Record_Def),
3928 Governed_By => New_Assoc_List,
3929 Into => Components,
3930 Report_Errors => Errors_Found);
3931 end if;
3932 end if;
3934 if Errors_Found then
3935 return;
3936 end if;
3937 end Step_5;
3939 -- STEP 6: Find component Values
3941 Component := Empty;
3942 Component_Elmt := First_Elmt (Components);
3944 -- First scan the remaining positional associations in the aggregate.
3945 -- Remember that at this point Positional_Expr contains the current
3946 -- positional association if any is left after looking for discriminant
3947 -- values in step 3.
3949 while Present (Positional_Expr) and then Present (Component_Elmt) loop
3950 Component := Node (Component_Elmt);
3951 Resolve_Aggr_Expr (Positional_Expr, Component);
3953 -- Ada 2005 (AI-231)
3955 if Ada_Version >= Ada_2005
3956 and then Known_Null (Positional_Expr)
3957 then
3958 Check_Can_Never_Be_Null (Component, Positional_Expr);
3959 end if;
3961 if Present (Get_Value (Component, Component_Associations (N))) then
3962 Error_Msg_NE
3963 ("more than one value supplied for Component &", N, Component);
3964 end if;
3966 Next (Positional_Expr);
3967 Next_Elmt (Component_Elmt);
3968 end loop;
3970 if Present (Positional_Expr) then
3971 Error_Msg_N
3972 ("too many components for record aggregate", Positional_Expr);
3973 end if;
3975 -- Now scan for the named arguments of the aggregate
3977 while Present (Component_Elmt) loop
3978 Component := Node (Component_Elmt);
3979 Expr := Get_Value (Component, Component_Associations (N), True);
3981 -- Note: The previous call to Get_Value sets the value of the
3982 -- variable Is_Box_Present.
3984 -- Ada 2005 (AI-287): Handle components with default initialization.
3985 -- Note: This feature was originally added to Ada 2005 for limited
3986 -- but it was finally allowed with any type.
3988 if Is_Box_Present then
3989 Check_Box_Component : declare
3990 Ctyp : constant Entity_Id := Etype (Component);
3992 begin
3993 -- If there is a default expression for the aggregate, copy
3994 -- it into a new association. This copy must modify the scopes
3995 -- of internal types that may be attached to the expression
3996 -- (e.g. index subtypes of arrays) because in general the type
3997 -- declaration and the aggregate appear in different scopes,
3998 -- and the backend requires the scope of the type to match the
3999 -- point at which it is elaborated.
4001 -- If the component has an initialization procedure (IP) we
4002 -- pass the component to the expander, which will generate
4003 -- the call to such IP.
4005 -- If the component has discriminants, their values must
4006 -- be taken from their subtype. This is indispensable for
4007 -- constraints that are given by the current instance of an
4008 -- enclosing type, to allow the expansion of the aggregate to
4009 -- replace the reference to the current instance by the target
4010 -- object of the aggregate.
4012 if Present (Parent (Component))
4013 and then
4014 Nkind (Parent (Component)) = N_Component_Declaration
4015 and then Present (Expression (Parent (Component)))
4016 then
4017 Expr :=
4018 New_Copy_Tree_And_Copy_Dimensions
4019 (Expression (Parent (Component)),
4020 New_Scope => Current_Scope,
4021 New_Sloc => Sloc (N));
4023 Add_Association
4024 (Component => Component,
4025 Expr => Expr,
4026 Assoc_List => New_Assoc_List);
4027 Set_Has_Self_Reference (N);
4029 -- A box-defaulted access component gets the value null. Also
4030 -- included are components of private types whose underlying
4031 -- type is an access type. In either case set the type of the
4032 -- literal, for subsequent use in semantic checks.
4034 elsif Present (Underlying_Type (Ctyp))
4035 and then Is_Access_Type (Underlying_Type (Ctyp))
4036 then
4037 if not Is_Private_Type (Ctyp) then
4038 Expr := Make_Null (Sloc (N));
4039 Set_Etype (Expr, Ctyp);
4040 Add_Association
4041 (Component => Component,
4042 Expr => Expr,
4043 Assoc_List => New_Assoc_List);
4045 -- If the component's type is private with an access type as
4046 -- its underlying type then we have to create an unchecked
4047 -- conversion to satisfy type checking.
4049 else
4050 declare
4051 Qual_Null : constant Node_Id :=
4052 Make_Qualified_Expression (Sloc (N),
4053 Subtype_Mark =>
4054 New_Occurrence_Of
4055 (Underlying_Type (Ctyp), Sloc (N)),
4056 Expression => Make_Null (Sloc (N)));
4058 Convert_Null : constant Node_Id :=
4059 Unchecked_Convert_To
4060 (Ctyp, Qual_Null);
4062 begin
4063 Analyze_And_Resolve (Convert_Null, Ctyp);
4064 Add_Association
4065 (Component => Component,
4066 Expr => Convert_Null,
4067 Assoc_List => New_Assoc_List);
4068 end;
4069 end if;
4071 elsif Has_Non_Null_Base_Init_Proc (Ctyp)
4072 or else not Expander_Active
4073 then
4074 if Is_Record_Type (Ctyp)
4075 and then Has_Discriminants (Ctyp)
4076 and then not Is_Private_Type (Ctyp)
4077 then
4078 -- We build a partially initialized aggregate with the
4079 -- values of the discriminants and box initialization
4080 -- for the rest, if other components are present.
4082 -- The type of the aggregate is the known subtype of
4083 -- the component. The capture of discriminants must
4084 -- be recursive because subcomponents may be constrained
4085 -- (transitively) by discriminants of enclosing types.
4086 -- For a private type with discriminants, a call to the
4087 -- initialization procedure will be generated, and no
4088 -- subaggregate is needed.
4090 Capture_Discriminants : declare
4091 Loc : constant Source_Ptr := Sloc (N);
4092 Expr : Node_Id;
4094 procedure Add_Discriminant_Values
4095 (New_Aggr : Node_Id;
4096 Assoc_List : List_Id);
4097 -- The constraint to a component may be given by a
4098 -- discriminant of the enclosing type, in which case
4099 -- we have to retrieve its value, which is part of the
4100 -- enclosing aggregate. Assoc_List provides the
4101 -- discriminant associations of the current type or
4102 -- of some enclosing record.
4104 procedure Propagate_Discriminants
4105 (Aggr : Node_Id;
4106 Assoc_List : List_Id);
4107 -- Nested components may themselves be discriminated
4108 -- types constrained by outer discriminants, whose
4109 -- values must be captured before the aggregate is
4110 -- expanded into assignments.
4112 -----------------------------
4113 -- Add_Discriminant_Values --
4114 -----------------------------
4116 procedure Add_Discriminant_Values
4117 (New_Aggr : Node_Id;
4118 Assoc_List : List_Id)
4120 Assoc : Node_Id;
4121 Discr : Entity_Id;
4122 Discr_Elmt : Elmt_Id;
4123 Discr_Val : Node_Id;
4124 Val : Entity_Id;
4126 begin
4127 Discr := First_Discriminant (Etype (New_Aggr));
4128 Discr_Elmt :=
4129 First_Elmt
4130 (Discriminant_Constraint (Etype (New_Aggr)));
4131 while Present (Discr_Elmt) loop
4132 Discr_Val := Node (Discr_Elmt);
4134 -- If the constraint is given by a discriminant
4135 -- it is a discriminant of an enclosing record,
4136 -- and its value has already been placed in the
4137 -- association list.
4139 if Is_Entity_Name (Discr_Val)
4140 and then
4141 Ekind (Entity (Discr_Val)) = E_Discriminant
4142 then
4143 Val := Entity (Discr_Val);
4145 Assoc := First (Assoc_List);
4146 while Present (Assoc) loop
4147 if Present
4148 (Entity (First (Choices (Assoc))))
4149 and then
4150 Entity (First (Choices (Assoc)))
4151 = Val
4152 then
4153 Discr_Val := Expression (Assoc);
4154 exit;
4155 end if;
4156 Next (Assoc);
4157 end loop;
4158 end if;
4160 Add_Association
4161 (Discr, New_Copy_Tree (Discr_Val),
4162 Component_Associations (New_Aggr));
4164 -- If the discriminant constraint is a current
4165 -- instance, mark the current aggregate so that
4166 -- the self-reference can be expanded later.
4168 if Nkind (Discr_Val) = N_Attribute_Reference
4169 and then Is_Entity_Name (Prefix (Discr_Val))
4170 and then Is_Type (Entity (Prefix (Discr_Val)))
4171 and then Etype (N) =
4172 Entity (Prefix (Discr_Val))
4173 then
4174 Set_Has_Self_Reference (N);
4175 end if;
4177 Next_Elmt (Discr_Elmt);
4178 Next_Discriminant (Discr);
4179 end loop;
4180 end Add_Discriminant_Values;
4182 ------------------------------
4183 -- Propagate_Discriminants --
4184 ------------------------------
4186 procedure Propagate_Discriminants
4187 (Aggr : Node_Id;
4188 Assoc_List : List_Id)
4190 Aggr_Type : constant Entity_Id :=
4191 Base_Type (Etype (Aggr));
4192 Def_Node : constant Node_Id :=
4193 Type_Definition
4194 (Declaration_Node (Aggr_Type));
4196 Comp : Node_Id;
4197 Comp_Elmt : Elmt_Id;
4198 Components : constant Elist_Id := New_Elmt_List;
4199 Needs_Box : Boolean := False;
4200 Errors : Boolean;
4202 procedure Process_Component (Comp : Entity_Id);
4203 -- Add one component with a box association to the
4204 -- inner aggregate, and recurse if component is
4205 -- itself composite.
4207 ------------------------
4208 -- Process_Component --
4209 ------------------------
4211 procedure Process_Component (Comp : Entity_Id) is
4212 T : constant Entity_Id := Etype (Comp);
4213 New_Aggr : Node_Id;
4215 begin
4216 if Is_Record_Type (T)
4217 and then Has_Discriminants (T)
4218 then
4219 New_Aggr :=
4220 Make_Aggregate (Loc, New_List, New_List);
4221 Set_Etype (New_Aggr, T);
4222 Add_Association
4223 (Comp, New_Aggr,
4224 Component_Associations (Aggr));
4226 -- Collect discriminant values and recurse
4228 Add_Discriminant_Values
4229 (New_Aggr, Assoc_List);
4230 Propagate_Discriminants
4231 (New_Aggr, Assoc_List);
4233 else
4234 Needs_Box := True;
4235 end if;
4236 end Process_Component;
4238 -- Start of processing for Propagate_Discriminants
4240 begin
4241 -- The component type may be a variant type, so
4242 -- collect the components that are ruled by the
4243 -- known values of the discriminants. Their values
4244 -- have already been inserted into the component
4245 -- list of the current aggregate.
4247 if Nkind (Def_Node) = N_Record_Definition
4248 and then
4249 Present (Component_List (Def_Node))
4250 and then
4251 Present
4252 (Variant_Part (Component_List (Def_Node)))
4253 then
4254 Gather_Components (Aggr_Type,
4255 Component_List (Def_Node),
4256 Governed_By => Component_Associations (Aggr),
4257 Into => Components,
4258 Report_Errors => Errors);
4260 Comp_Elmt := First_Elmt (Components);
4261 while Present (Comp_Elmt) loop
4263 Ekind (Node (Comp_Elmt)) /= E_Discriminant
4264 then
4265 Process_Component (Node (Comp_Elmt));
4266 end if;
4268 Next_Elmt (Comp_Elmt);
4269 end loop;
4271 -- No variant part, iterate over all components
4273 else
4274 Comp := First_Component (Etype (Aggr));
4275 while Present (Comp) loop
4276 Process_Component (Comp);
4277 Next_Component (Comp);
4278 end loop;
4279 end if;
4281 if Needs_Box then
4282 Append
4283 (Make_Component_Association (Loc,
4284 Choices =>
4285 New_List (Make_Others_Choice (Loc)),
4286 Expression => Empty,
4287 Box_Present => True),
4288 Component_Associations (Aggr));
4289 end if;
4290 end Propagate_Discriminants;
4292 -- Start of processing for Capture_Discriminants
4294 begin
4295 Expr := Make_Aggregate (Loc, New_List, New_List);
4296 Set_Etype (Expr, Ctyp);
4298 -- If the enclosing type has discriminants, they have
4299 -- been collected in the aggregate earlier, and they
4300 -- may appear as constraints of subcomponents.
4302 -- Similarly if this component has discriminants, they
4303 -- might in turn be propagated to their components.
4305 if Has_Discriminants (Typ) then
4306 Add_Discriminant_Values (Expr, New_Assoc_List);
4307 Propagate_Discriminants (Expr, New_Assoc_List);
4309 elsif Has_Discriminants (Ctyp) then
4310 Add_Discriminant_Values
4311 (Expr, Component_Associations (Expr));
4312 Propagate_Discriminants
4313 (Expr, Component_Associations (Expr));
4315 else
4316 declare
4317 Comp : Entity_Id;
4319 begin
4320 -- If the type has additional components, create
4321 -- an OTHERS box association for them.
4323 Comp := First_Component (Ctyp);
4324 while Present (Comp) loop
4325 if Ekind (Comp) = E_Component then
4326 if not Is_Record_Type (Etype (Comp)) then
4327 Append
4328 (Make_Component_Association (Loc,
4329 Choices =>
4330 New_List
4331 (Make_Others_Choice (Loc)),
4332 Expression => Empty,
4333 Box_Present => True),
4334 Component_Associations (Expr));
4335 end if;
4336 exit;
4337 end if;
4339 Next_Component (Comp);
4340 end loop;
4341 end;
4342 end if;
4344 Add_Association
4345 (Component => Component,
4346 Expr => Expr,
4347 Assoc_List => New_Assoc_List);
4348 end Capture_Discriminants;
4350 else
4351 Add_Association
4352 (Component => Component,
4353 Expr => Empty,
4354 Assoc_List => New_Assoc_List,
4355 Is_Box_Present => True);
4356 end if;
4358 -- Otherwise we only need to resolve the expression if the
4359 -- component has partially initialized values (required to
4360 -- expand the corresponding assignments and run-time checks).
4362 elsif Present (Expr)
4363 and then Is_Partially_Initialized_Type (Ctyp)
4364 then
4365 Resolve_Aggr_Expr (Expr, Component);
4366 end if;
4367 end Check_Box_Component;
4369 elsif No (Expr) then
4371 -- Ignore hidden components associated with the position of the
4372 -- interface tags: these are initialized dynamically.
4374 if not Present (Related_Type (Component)) then
4375 Error_Msg_NE
4376 ("no value supplied for component &!", N, Component);
4377 end if;
4379 else
4380 Resolve_Aggr_Expr (Expr, Component);
4381 end if;
4383 Next_Elmt (Component_Elmt);
4384 end loop;
4386 -- STEP 7: check for invalid components + check type in choice list
4388 Step_7 : declare
4389 Selectr : Node_Id;
4390 -- Selector name
4392 Typech : Entity_Id;
4393 -- Type of first component in choice list
4395 begin
4396 if Present (Component_Associations (N)) then
4397 Assoc := First (Component_Associations (N));
4398 else
4399 Assoc := Empty;
4400 end if;
4402 Verification : while Present (Assoc) loop
4403 Selectr := First (Choices (Assoc));
4404 Typech := Empty;
4406 if Nkind (Selectr) = N_Others_Choice then
4408 -- Ada 2005 (AI-287): others choice may have expression or box
4410 if No (Others_Etype)
4411 and then not Others_Box
4412 then
4413 Error_Msg_N
4414 ("OTHERS must represent at least one component", Selectr);
4415 end if;
4417 exit Verification;
4418 end if;
4420 while Present (Selectr) loop
4421 New_Assoc := First (New_Assoc_List);
4422 while Present (New_Assoc) loop
4423 Component := First (Choices (New_Assoc));
4425 if Chars (Selectr) = Chars (Component) then
4426 if Style_Check then
4427 Check_Identifier (Selectr, Entity (Component));
4428 end if;
4430 exit;
4431 end if;
4433 Next (New_Assoc);
4434 end loop;
4436 -- If no association, this is not a legal component of the type
4437 -- in question, unless its association is provided with a box.
4439 if No (New_Assoc) then
4440 if Box_Present (Parent (Selectr)) then
4442 -- This may still be a bogus component with a box. Scan
4443 -- list of components to verify that a component with
4444 -- that name exists.
4446 declare
4447 C : Entity_Id;
4449 begin
4450 C := First_Component (Typ);
4451 while Present (C) loop
4452 if Chars (C) = Chars (Selectr) then
4454 -- If the context is an extension aggregate,
4455 -- the component must not be inherited from
4456 -- the ancestor part of the aggregate.
4458 if Nkind (N) /= N_Extension_Aggregate
4459 or else
4460 Scope (Original_Record_Component (C)) /=
4461 Etype (Ancestor_Part (N))
4462 then
4463 exit;
4464 end if;
4465 end if;
4467 Next_Component (C);
4468 end loop;
4470 if No (C) then
4471 Error_Msg_Node_2 := Typ;
4472 Error_Msg_N ("& is not a component of}", Selectr);
4473 end if;
4474 end;
4476 elsif Chars (Selectr) /= Name_uTag
4477 and then Chars (Selectr) /= Name_uParent
4478 then
4479 if not Has_Discriminants (Typ) then
4480 Error_Msg_Node_2 := Typ;
4481 Error_Msg_N ("& is not a component of}", Selectr);
4482 else
4483 Error_Msg_N
4484 ("& is not a component of the aggregate subtype",
4485 Selectr);
4486 end if;
4488 Check_Misspelled_Component (Components, Selectr);
4489 end if;
4491 elsif No (Typech) then
4492 Typech := Base_Type (Etype (Component));
4494 -- AI05-0199: In Ada 2012, several components of anonymous
4495 -- access types can appear in a choice list, as long as the
4496 -- designated types match.
4498 elsif Typech /= Base_Type (Etype (Component)) then
4499 if Ada_Version >= Ada_2012
4500 and then Ekind (Typech) = E_Anonymous_Access_Type
4501 and then
4502 Ekind (Etype (Component)) = E_Anonymous_Access_Type
4503 and then Base_Type (Designated_Type (Typech)) =
4504 Base_Type (Designated_Type (Etype (Component)))
4505 and then
4506 Subtypes_Statically_Match (Typech, (Etype (Component)))
4507 then
4508 null;
4510 elsif not Box_Present (Parent (Selectr)) then
4511 Error_Msg_N
4512 ("components in choice list must have same type",
4513 Selectr);
4514 end if;
4515 end if;
4517 Next (Selectr);
4518 end loop;
4520 Next (Assoc);
4521 end loop Verification;
4522 end Step_7;
4524 -- STEP 8: replace the original aggregate
4526 Step_8 : declare
4527 New_Aggregate : constant Node_Id := New_Copy (N);
4529 begin
4530 Set_Expressions (New_Aggregate, No_List);
4531 Set_Etype (New_Aggregate, Etype (N));
4532 Set_Component_Associations (New_Aggregate, New_Assoc_List);
4534 Rewrite (N, New_Aggregate);
4535 end Step_8;
4537 -- Check the dimensions of the components in the record aggregate
4539 Analyze_Dimension_Extension_Or_Record_Aggregate (N);
4540 end Resolve_Record_Aggregate;
4542 -----------------------------
4543 -- Check_Can_Never_Be_Null --
4544 -----------------------------
4546 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
4547 Comp_Typ : Entity_Id;
4549 begin
4550 pragma Assert
4551 (Ada_Version >= Ada_2005
4552 and then Present (Expr)
4553 and then Known_Null (Expr));
4555 case Ekind (Typ) is
4556 when E_Array_Type =>
4557 Comp_Typ := Component_Type (Typ);
4559 when E_Component |
4560 E_Discriminant =>
4561 Comp_Typ := Etype (Typ);
4563 when others =>
4564 return;
4565 end case;
4567 if Can_Never_Be_Null (Comp_Typ) then
4569 -- Here we know we have a constraint error. Note that we do not use
4570 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
4571 -- seem the more natural approach. That's because in some cases the
4572 -- components are rewritten, and the replacement would be missed.
4574 Insert_Action
4575 (Compile_Time_Constraint_Error
4576 (Expr,
4577 "(Ada 2005) null not allowed in null-excluding component??"),
4578 Make_Raise_Constraint_Error
4579 (Sloc (Expr), Reason => CE_Access_Check_Failed));
4581 -- Set proper type for bogus component (why is this needed???)
4583 Set_Etype (Expr, Comp_Typ);
4584 Set_Analyzed (Expr);
4585 end if;
4586 end Check_Can_Never_Be_Null;
4588 ---------------------
4589 -- Sort_Case_Table --
4590 ---------------------
4592 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
4593 L : constant Int := Case_Table'First;
4594 U : constant Int := Case_Table'Last;
4595 K : Int;
4596 J : Int;
4597 T : Case_Bounds;
4599 begin
4600 K := L;
4601 while K /= U loop
4602 T := Case_Table (K + 1);
4604 J := K + 1;
4605 while J /= L
4606 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
4607 Expr_Value (T.Choice_Lo)
4608 loop
4609 Case_Table (J) := Case_Table (J - 1);
4610 J := J - 1;
4611 end loop;
4613 Case_Table (J) := T;
4614 K := K + 1;
4615 end loop;
4616 end Sort_Case_Table;
4618 end Sem_Aggr;