Merge from mainline (168000:168310).
[official-gcc/graphite-test-results.git] / gcc / ada / sem_aggr.adb
blob1d75a3c75a9a91085a8daae404a88cb04b1e8883
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-2010, 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 Sem; use Sem;
44 with Sem_Aux; use Sem_Aux;
45 with Sem_Cat; use Sem_Cat;
46 with Sem_Ch3; use Sem_Ch3;
47 with Sem_Ch13; use Sem_Ch13;
48 with Sem_Eval; use Sem_Eval;
49 with Sem_Res; use Sem_Res;
50 with Sem_Util; use Sem_Util;
51 with Sem_Type; use Sem_Type;
52 with Sem_Warn; use Sem_Warn;
53 with Sinfo; use Sinfo;
54 with Snames; use Snames;
55 with Stringt; use Stringt;
56 with Stand; use Stand;
57 with Style; use Style;
58 with Targparm; use Targparm;
59 with Tbuild; use Tbuild;
60 with Uintp; use Uintp;
62 package body Sem_Aggr is
64 type Case_Bounds is record
65 Choice_Lo : Node_Id;
66 Choice_Hi : Node_Id;
67 Choice_Node : Node_Id;
68 end record;
70 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
71 -- Table type used by Check_Case_Choices procedure
73 -----------------------
74 -- Local Subprograms --
75 -----------------------
77 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
78 -- Sort the Case Table using the Lower Bound of each Choice as the key.
79 -- A simple insertion sort is used since the number of choices in a case
80 -- statement of variant part will usually be small and probably in near
81 -- sorted order.
83 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id);
84 -- Ada 2005 (AI-231): Check bad usage of null for a component for which
85 -- null exclusion (NOT NULL) is specified. Typ can be an E_Array_Type for
86 -- the array case (the component type of the array will be used) or an
87 -- E_Component/E_Discriminant entity in the record case, in which case the
88 -- type of the component will be used for the test. If Typ is any other
89 -- kind of entity, the call is ignored. Expr is the component node in the
90 -- aggregate which is known to have a null value. A warning message will be
91 -- issued if the component is null excluding.
93 -- It would be better to pass the proper type for Typ ???
95 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id);
96 -- Check that Expr is either not limited or else is one of the cases of
97 -- expressions allowed for a limited component association (namely, an
98 -- aggregate, function call, or <> notation). Report error for violations.
100 ------------------------------------------------------
101 -- Subprograms used for RECORD AGGREGATE Processing --
102 ------------------------------------------------------
104 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
105 -- This procedure performs all the semantic checks required for record
106 -- aggregates. Note that for aggregates analysis and resolution go
107 -- hand in hand. Aggregate analysis has been delayed up to here and
108 -- it is done while resolving the aggregate.
110 -- N is the N_Aggregate node.
111 -- Typ is the record type for the aggregate resolution
113 -- While performing the semantic checks, this procedure builds a new
114 -- Component_Association_List where each record field appears alone in a
115 -- Component_Choice_List along with its corresponding expression. The
116 -- record fields in the Component_Association_List appear in the same order
117 -- in which they appear in the record type Typ.
119 -- Once this new Component_Association_List is built and all the semantic
120 -- checks performed, the original aggregate subtree is replaced with the
121 -- new named record aggregate just built. Note that subtree substitution is
122 -- performed with Rewrite so as to be able to retrieve the original
123 -- aggregate.
125 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
126 -- yields the aggregate format expected by Gigi. Typically, this kind of
127 -- tree manipulations are done in the expander. However, because the
128 -- semantic checks that need to be performed on record aggregates really go
129 -- hand in hand with the record aggregate normalization, the aggregate
130 -- subtree transformation is performed during resolution rather than
131 -- expansion. Had we decided otherwise we would have had to duplicate most
132 -- of the code in the expansion procedure Expand_Record_Aggregate. Note,
133 -- however, that all the expansion concerning aggregates for tagged records
134 -- is done in Expand_Record_Aggregate.
136 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
138 -- 1. Make sure that the record type against which the record aggregate
139 -- has to be resolved is not abstract. Furthermore if the type is a
140 -- null aggregate make sure the input aggregate N is also null.
142 -- 2. Verify that the structure of the aggregate is that of a record
143 -- aggregate. Specifically, look for component associations and ensure
144 -- that each choice list only has identifiers or the N_Others_Choice
145 -- node. Also make sure that if present, the N_Others_Choice occurs
146 -- last and by itself.
148 -- 3. If Typ contains discriminants, the values for each discriminant is
149 -- looked for. If the record type Typ has variants, we check that the
150 -- expressions corresponding to each discriminant ruling the (possibly
151 -- nested) variant parts of Typ, are static. This allows us to determine
152 -- the variant parts to which the rest of the aggregate must conform.
153 -- The names of discriminants with their values are saved in a new
154 -- association list, New_Assoc_List which is later augmented with the
155 -- names and values of the remaining components in the record type.
157 -- During this phase we also make sure that every discriminant is
158 -- assigned exactly one value. Note that when several values for a given
159 -- discriminant are found, semantic processing continues looking for
160 -- further errors. In this case it's the first discriminant value found
161 -- which we will be recorded.
163 -- IMPORTANT NOTE: For derived tagged types this procedure expects
164 -- First_Discriminant and Next_Discriminant to give the correct list
165 -- of discriminants, in the correct order.
167 -- 4. After all the discriminant values have been gathered, we can set the
168 -- Etype of the record aggregate. If Typ contains no discriminants this
169 -- is straightforward: the Etype of N is just Typ, otherwise a new
170 -- implicit constrained subtype of Typ is built to be the Etype of N.
172 -- 5. Gather the remaining record components according to the discriminant
173 -- values. This involves recursively traversing the record type
174 -- structure to see what variants are selected by the given discriminant
175 -- values. This processing is a little more convoluted if Typ is a
176 -- derived tagged types since we need to retrieve the record structure
177 -- of all the ancestors of Typ.
179 -- 6. After gathering the record components we look for their values in the
180 -- record aggregate and emit appropriate error messages should we not
181 -- find such values or should they be duplicated.
183 -- 7. We then make sure no illegal component names appear in the record
184 -- aggregate and make sure that the type of the record components
185 -- appearing in a same choice list is the same. Finally we ensure that
186 -- the others choice, if present, is used to provide the value of at
187 -- least a record component.
189 -- 8. The original aggregate node is replaced with the new named aggregate
190 -- built in steps 3 through 6, as explained earlier.
192 -- Given the complexity of record aggregate resolution, the primary goal of
193 -- this routine is clarity and simplicity rather than execution and storage
194 -- efficiency. If there are only positional components in the aggregate the
195 -- running time is linear. If there are associations the running time is
196 -- still linear as long as the order of the associations is not too far off
197 -- the order of the components in the record type. If this is not the case
198 -- the running time is at worst quadratic in the size of the association
199 -- list.
201 procedure Check_Misspelled_Component
202 (Elements : Elist_Id;
203 Component : Node_Id);
204 -- Give possible misspelling diagnostic if Component is likely to be a
205 -- misspelling of one of the components of the Assoc_List. This is called
206 -- by Resolve_Aggr_Expr after producing an invalid component error message.
208 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id);
209 -- An optimization: determine whether a discriminated subtype has a static
210 -- constraint, and contains array components whose length is also static,
211 -- either because they are constrained by the discriminant, or because the
212 -- original component bounds are static.
214 -----------------------------------------------------
215 -- Subprograms used for ARRAY AGGREGATE Processing --
216 -----------------------------------------------------
218 function Resolve_Array_Aggregate
219 (N : Node_Id;
220 Index : Node_Id;
221 Index_Constr : Node_Id;
222 Component_Typ : Entity_Id;
223 Others_Allowed : Boolean) return Boolean;
224 -- This procedure performs the semantic checks for an array aggregate.
225 -- True is returned if the aggregate resolution succeeds.
227 -- The procedure works by recursively checking each nested aggregate.
228 -- Specifically, after checking a sub-aggregate nested at the i-th level
229 -- we recursively check all the subaggregates at the i+1-st level (if any).
230 -- Note that for aggregates analysis and resolution go hand in hand.
231 -- Aggregate analysis has been delayed up to here and it is done while
232 -- resolving the aggregate.
234 -- N is the current N_Aggregate node to be checked.
236 -- Index is the index node corresponding to the array sub-aggregate that
237 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
238 -- corresponding index type (or subtype).
240 -- Index_Constr is the node giving the applicable index constraint if
241 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
242 -- contexts [...] that can be used to determine the bounds of the array
243 -- value specified by the aggregate". If Others_Allowed below is False
244 -- there is no applicable index constraint and this node is set to Index.
246 -- Component_Typ is the array component type.
248 -- Others_Allowed indicates whether an others choice is allowed
249 -- in the context where the top-level aggregate appeared.
251 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
253 -- 1. Make sure that the others choice, if present, is by itself and
254 -- appears last in the sub-aggregate. Check that we do not have
255 -- positional and named components in the array sub-aggregate (unless
256 -- the named association is an others choice). Finally if an others
257 -- choice is present, make sure it is allowed in the aggregate context.
259 -- 2. If the array sub-aggregate contains discrete_choices:
261 -- (A) Verify their validity. Specifically verify that:
263 -- (a) If a null range is present it must be the only possible
264 -- choice in the array aggregate.
266 -- (b) Ditto for a non static range.
268 -- (c) Ditto for a non static expression.
270 -- In addition this step analyzes and resolves each discrete_choice,
271 -- making sure that its type is the type of the corresponding Index.
272 -- If we are not at the lowest array aggregate level (in the case of
273 -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
274 -- recursively on each component expression. Otherwise, resolve the
275 -- bottom level component expressions against the expected component
276 -- type ONLY IF the component corresponds to a single discrete choice
277 -- which is not an others choice (to see why read the DELAYED
278 -- COMPONENT RESOLUTION below).
280 -- (B) Determine the bounds of the sub-aggregate and lowest and
281 -- highest choice values.
283 -- 3. For positional aggregates:
285 -- (A) Loop over the component expressions either recursively invoking
286 -- Resolve_Array_Aggregate on each of these for multi-dimensional
287 -- array aggregates or resolving the bottom level component
288 -- expressions against the expected component type.
290 -- (B) Determine the bounds of the positional sub-aggregates.
292 -- 4. Try to determine statically whether the evaluation of the array
293 -- sub-aggregate raises Constraint_Error. If yes emit proper
294 -- warnings. The precise checks are the following:
296 -- (A) Check that the index range defined by aggregate bounds is
297 -- compatible with corresponding index subtype.
298 -- We also check against the base type. In fact it could be that
299 -- Low/High bounds of the base type are static whereas those of
300 -- the index subtype are not. Thus if we can statically catch
301 -- a problem with respect to the base type we are guaranteed
302 -- that the same problem will arise with the index subtype
304 -- (B) If we are dealing with a named aggregate containing an others
305 -- choice and at least one discrete choice then make sure the range
306 -- specified by the discrete choices does not overflow the
307 -- aggregate bounds. We also check against the index type and base
308 -- type bounds for the same reasons given in (A).
310 -- (C) If we are dealing with a positional aggregate with an others
311 -- choice make sure the number of positional elements specified
312 -- does not overflow the aggregate bounds. We also check against
313 -- the index type and base type bounds as mentioned in (A).
315 -- Finally construct an N_Range node giving the sub-aggregate bounds.
316 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
317 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
318 -- to build the appropriate aggregate subtype. Aggregate_Bounds
319 -- information is needed during expansion.
321 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
322 -- expressions in an array aggregate may call Duplicate_Subexpr or some
323 -- other routine that inserts code just outside the outermost aggregate.
324 -- If the array aggregate contains discrete choices or an others choice,
325 -- this may be wrong. Consider for instance the following example.
327 -- type Rec is record
328 -- V : Integer := 0;
329 -- end record;
331 -- type Acc_Rec is access Rec;
332 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
334 -- Then the transformation of "new Rec" that occurs during resolution
335 -- entails the following code modifications
337 -- P7b : constant Acc_Rec := new Rec;
338 -- RecIP (P7b.all);
339 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
341 -- This code transformation is clearly wrong, since we need to call
342 -- "new Rec" for each of the 3 array elements. To avoid this problem we
343 -- delay resolution of the components of non positional array aggregates
344 -- to the expansion phase. As an optimization, if the discrete choice
345 -- specifies a single value we do not delay resolution.
347 function Array_Aggr_Subtype (N : Node_Id; Typ : Node_Id) return Entity_Id;
348 -- This routine returns the type or subtype of an array aggregate.
350 -- N is the array aggregate node whose type we return.
352 -- Typ is the context type in which N occurs.
354 -- This routine creates an implicit array subtype whose bounds are
355 -- those defined by the aggregate. When this routine is invoked
356 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
357 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
358 -- sub-aggregate bounds. When building the aggregate itype, this function
359 -- traverses the array aggregate N collecting such Aggregate_Bounds and
360 -- constructs the proper array aggregate itype.
362 -- Note that in the case of multidimensional aggregates each inner
363 -- sub-aggregate corresponding to a given array dimension, may provide a
364 -- different bounds. If it is possible to determine statically that
365 -- some sub-aggregates corresponding to the same index do not have the
366 -- same bounds, then a warning is emitted. If such check is not possible
367 -- statically (because some sub-aggregate bounds are dynamic expressions)
368 -- then this job is left to the expander. In all cases the particular
369 -- bounds that this function will chose for a given dimension is the first
370 -- N_Range node for a sub-aggregate corresponding to that dimension.
372 -- Note that the Raises_Constraint_Error flag of an array aggregate
373 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
374 -- is set in Resolve_Array_Aggregate but the aggregate is not
375 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
376 -- first construct the proper itype for the aggregate (Gigi needs
377 -- this). After constructing the proper itype we will eventually replace
378 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
379 -- Of course in cases such as:
381 -- type Arr is array (integer range <>) of Integer;
382 -- A : Arr := (positive range -1 .. 2 => 0);
384 -- The bounds of the aggregate itype are cooked up to look reasonable
385 -- (in this particular case the bounds will be 1 .. 2).
387 procedure Aggregate_Constraint_Checks
388 (Exp : Node_Id;
389 Check_Typ : Entity_Id);
390 -- Checks expression Exp against subtype Check_Typ. If Exp is an
391 -- aggregate and Check_Typ a constrained record type with discriminants,
392 -- we generate the appropriate discriminant checks. If Exp is an array
393 -- aggregate then emit the appropriate length checks. If Exp is a scalar
394 -- type, or a string literal, Exp is changed into Check_Typ'(Exp) to
395 -- ensure that range checks are performed at run time.
397 procedure Make_String_Into_Aggregate (N : Node_Id);
398 -- A string literal can appear in a context in which a one dimensional
399 -- array of characters is expected. This procedure simply rewrites the
400 -- string as an aggregate, prior to resolution.
402 ---------------------------------
403 -- Aggregate_Constraint_Checks --
404 ---------------------------------
406 procedure Aggregate_Constraint_Checks
407 (Exp : Node_Id;
408 Check_Typ : Entity_Id)
410 Exp_Typ : constant Entity_Id := Etype (Exp);
412 begin
413 if Raises_Constraint_Error (Exp) then
414 return;
415 end if;
417 -- Ada 2005 (AI-230): Generate a conversion to an anonymous access
418 -- component's type to force the appropriate accessibility checks.
420 -- Ada 2005 (AI-231): Generate conversion to the null-excluding
421 -- type to force the corresponding run-time check
423 if Is_Access_Type (Check_Typ)
424 and then ((Is_Local_Anonymous_Access (Check_Typ))
425 or else (Can_Never_Be_Null (Check_Typ)
426 and then not Can_Never_Be_Null (Exp_Typ)))
427 then
428 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
429 Analyze_And_Resolve (Exp, Check_Typ);
430 Check_Unset_Reference (Exp);
431 end if;
433 -- This is really expansion activity, so make sure that expansion
434 -- is on and is allowed.
436 if not Expander_Active or else In_Spec_Expression then
437 return;
438 end if;
440 -- First check if we have to insert discriminant checks
442 if Has_Discriminants (Exp_Typ) then
443 Apply_Discriminant_Check (Exp, Check_Typ);
445 -- Next emit length checks for array aggregates
447 elsif Is_Array_Type (Exp_Typ) then
448 Apply_Length_Check (Exp, Check_Typ);
450 -- Finally emit scalar and string checks. If we are dealing with a
451 -- scalar literal we need to check by hand because the Etype of
452 -- literals is not necessarily correct.
454 elsif Is_Scalar_Type (Exp_Typ)
455 and then Compile_Time_Known_Value (Exp)
456 then
457 if Is_Out_Of_Range (Exp, Base_Type (Check_Typ)) then
458 Apply_Compile_Time_Constraint_Error
459 (Exp, "value not in range of}?", CE_Range_Check_Failed,
460 Ent => Base_Type (Check_Typ),
461 Typ => Base_Type (Check_Typ));
463 elsif Is_Out_Of_Range (Exp, Check_Typ) then
464 Apply_Compile_Time_Constraint_Error
465 (Exp, "value not in range of}?", CE_Range_Check_Failed,
466 Ent => Check_Typ,
467 Typ => Check_Typ);
469 elsif not Range_Checks_Suppressed (Check_Typ) then
470 Apply_Scalar_Range_Check (Exp, Check_Typ);
471 end if;
473 -- Verify that target type is also scalar, to prevent view anomalies
474 -- in instantiations.
476 elsif (Is_Scalar_Type (Exp_Typ)
477 or else Nkind (Exp) = N_String_Literal)
478 and then Is_Scalar_Type (Check_Typ)
479 and then Exp_Typ /= Check_Typ
480 then
481 if Is_Entity_Name (Exp)
482 and then Ekind (Entity (Exp)) = E_Constant
483 then
484 -- If expression is a constant, it is worthwhile checking whether
485 -- it is a bound of the type.
487 if (Is_Entity_Name (Type_Low_Bound (Check_Typ))
488 and then Entity (Exp) = Entity (Type_Low_Bound (Check_Typ)))
489 or else (Is_Entity_Name (Type_High_Bound (Check_Typ))
490 and then Entity (Exp) = Entity (Type_High_Bound (Check_Typ)))
491 then
492 return;
494 else
495 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
496 Analyze_And_Resolve (Exp, Check_Typ);
497 Check_Unset_Reference (Exp);
498 end if;
499 else
500 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
501 Analyze_And_Resolve (Exp, Check_Typ);
502 Check_Unset_Reference (Exp);
503 end if;
505 end if;
506 end Aggregate_Constraint_Checks;
508 ------------------------
509 -- Array_Aggr_Subtype --
510 ------------------------
512 function Array_Aggr_Subtype
513 (N : Node_Id;
514 Typ : Entity_Id) return Entity_Id
516 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
517 -- Number of aggregate index dimensions
519 Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
520 -- Constrained N_Range of each index dimension in our aggregate itype
522 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
523 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
524 -- Low and High bounds for each index dimension in our aggregate itype
526 Is_Fully_Positional : Boolean := True;
528 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
529 -- N is an array (sub-)aggregate. Dim is the dimension corresponding
530 -- to (sub-)aggregate N. This procedure collects and removes the side
531 -- effects of the constrained N_Range nodes corresponding to each index
532 -- dimension of our aggregate itype. These N_Range nodes are collected
533 -- in Aggr_Range above.
535 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
536 -- bounds of each index dimension. If, when collecting, two bounds
537 -- corresponding to the same dimension are static and found to differ,
538 -- then emit a warning, and mark N as raising Constraint_Error.
540 -------------------------
541 -- Collect_Aggr_Bounds --
542 -------------------------
544 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
545 This_Range : constant Node_Id := Aggregate_Bounds (N);
546 -- The aggregate range node of this specific sub-aggregate
548 This_Low : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
549 This_High : constant Node_Id := High_Bound (Aggregate_Bounds (N));
550 -- The aggregate bounds of this specific sub-aggregate
552 Assoc : Node_Id;
553 Expr : Node_Id;
555 begin
556 Remove_Side_Effects (This_Low, Variable_Ref => True);
557 Remove_Side_Effects (This_High, Variable_Ref => True);
559 -- Collect the first N_Range for a given dimension that you find.
560 -- For a given dimension they must be all equal anyway.
562 if No (Aggr_Range (Dim)) then
563 Aggr_Low (Dim) := This_Low;
564 Aggr_High (Dim) := This_High;
565 Aggr_Range (Dim) := This_Range;
567 else
568 if Compile_Time_Known_Value (This_Low) then
569 if not Compile_Time_Known_Value (Aggr_Low (Dim)) then
570 Aggr_Low (Dim) := This_Low;
572 elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
573 Set_Raises_Constraint_Error (N);
574 Error_Msg_N ("sub-aggregate low bound mismatch?", N);
575 Error_Msg_N
576 ("\Constraint_Error will be raised at run time?", N);
577 end if;
578 end if;
580 if Compile_Time_Known_Value (This_High) then
581 if not Compile_Time_Known_Value (Aggr_High (Dim)) then
582 Aggr_High (Dim) := This_High;
584 elsif
585 Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
586 then
587 Set_Raises_Constraint_Error (N);
588 Error_Msg_N ("sub-aggregate high bound mismatch?", N);
589 Error_Msg_N
590 ("\Constraint_Error will be raised at run time?", N);
591 end if;
592 end if;
593 end if;
595 if Dim < Aggr_Dimension then
597 -- Process positional components
599 if Present (Expressions (N)) then
600 Expr := First (Expressions (N));
601 while Present (Expr) loop
602 Collect_Aggr_Bounds (Expr, Dim + 1);
603 Next (Expr);
604 end loop;
605 end if;
607 -- Process component associations
609 if Present (Component_Associations (N)) then
610 Is_Fully_Positional := False;
612 Assoc := First (Component_Associations (N));
613 while Present (Assoc) loop
614 Expr := Expression (Assoc);
615 Collect_Aggr_Bounds (Expr, Dim + 1);
616 Next (Assoc);
617 end loop;
618 end if;
619 end if;
620 end Collect_Aggr_Bounds;
622 -- Array_Aggr_Subtype variables
624 Itype : Entity_Id;
625 -- The final itype of the overall aggregate
627 Index_Constraints : constant List_Id := New_List;
628 -- The list of index constraints of the aggregate itype
630 -- Start of processing for Array_Aggr_Subtype
632 begin
633 -- Make sure that the list of index constraints is properly attached to
634 -- the tree, and then collect the aggregate bounds.
636 Set_Parent (Index_Constraints, N);
637 Collect_Aggr_Bounds (N, 1);
639 -- Build the list of constrained indexes of our aggregate itype
641 for J in 1 .. Aggr_Dimension loop
642 Create_Index : declare
643 Index_Base : constant Entity_Id :=
644 Base_Type (Etype (Aggr_Range (J)));
645 Index_Typ : Entity_Id;
647 begin
648 -- Construct the Index subtype, and associate it with the range
649 -- construct that generates it.
651 Index_Typ :=
652 Create_Itype (Subtype_Kind (Ekind (Index_Base)), Aggr_Range (J));
654 Set_Etype (Index_Typ, Index_Base);
656 if Is_Character_Type (Index_Base) then
657 Set_Is_Character_Type (Index_Typ);
658 end if;
660 Set_Size_Info (Index_Typ, (Index_Base));
661 Set_RM_Size (Index_Typ, RM_Size (Index_Base));
662 Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base));
663 Set_Scalar_Range (Index_Typ, Aggr_Range (J));
665 if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then
666 Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ)));
667 end if;
669 Set_Etype (Aggr_Range (J), Index_Typ);
671 Append (Aggr_Range (J), To => Index_Constraints);
672 end Create_Index;
673 end loop;
675 -- Now build the Itype
677 Itype := Create_Itype (E_Array_Subtype, N);
679 Set_First_Rep_Item (Itype, First_Rep_Item (Typ));
680 Set_Convention (Itype, Convention (Typ));
681 Set_Depends_On_Private (Itype, Has_Private_Component (Typ));
682 Set_Etype (Itype, Base_Type (Typ));
683 Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ));
684 Set_Is_Aliased (Itype, Is_Aliased (Typ));
685 Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
687 Copy_Suppress_Status (Index_Check, Typ, Itype);
688 Copy_Suppress_Status (Length_Check, Typ, Itype);
690 Set_First_Index (Itype, First (Index_Constraints));
691 Set_Is_Constrained (Itype, True);
692 Set_Is_Internal (Itype, True);
694 -- A simple optimization: purely positional aggregates of static
695 -- components should be passed to gigi unexpanded whenever possible, and
696 -- regardless of the staticness of the bounds themselves. Subsequent
697 -- checks in exp_aggr verify that type is not packed, etc.
699 Set_Size_Known_At_Compile_Time (Itype,
700 Is_Fully_Positional
701 and then Comes_From_Source (N)
702 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
704 -- We always need a freeze node for a packed array subtype, so that we
705 -- can build the Packed_Array_Type corresponding to the subtype. If
706 -- expansion is disabled, the packed array subtype is not built, and we
707 -- must not generate a freeze node for the type, or else it will appear
708 -- incomplete to gigi.
710 if Is_Packed (Itype)
711 and then not In_Spec_Expression
712 and then Expander_Active
713 then
714 Freeze_Itype (Itype, N);
715 end if;
717 return Itype;
718 end Array_Aggr_Subtype;
720 --------------------------------
721 -- Check_Misspelled_Component --
722 --------------------------------
724 procedure Check_Misspelled_Component
725 (Elements : Elist_Id;
726 Component : Node_Id)
728 Max_Suggestions : constant := 2;
730 Nr_Of_Suggestions : Natural := 0;
731 Suggestion_1 : Entity_Id := Empty;
732 Suggestion_2 : Entity_Id := Empty;
733 Component_Elmt : Elmt_Id;
735 begin
736 -- All the components of List are matched against Component and a count
737 -- is maintained of possible misspellings. When at the end of the
738 -- the analysis there are one or two (not more!) possible misspellings,
739 -- these misspellings will be suggested as possible correction.
741 Component_Elmt := First_Elmt (Elements);
742 while Nr_Of_Suggestions <= Max_Suggestions
743 and then Present (Component_Elmt)
744 loop
745 if Is_Bad_Spelling_Of
746 (Chars (Node (Component_Elmt)),
747 Chars (Component))
748 then
749 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
751 case Nr_Of_Suggestions is
752 when 1 => Suggestion_1 := Node (Component_Elmt);
753 when 2 => Suggestion_2 := Node (Component_Elmt);
754 when others => exit;
755 end case;
756 end if;
758 Next_Elmt (Component_Elmt);
759 end loop;
761 -- Report at most two suggestions
763 if Nr_Of_Suggestions = 1 then
764 Error_Msg_NE -- CODEFIX
765 ("\possible misspelling of&", Component, Suggestion_1);
767 elsif Nr_Of_Suggestions = 2 then
768 Error_Msg_Node_2 := Suggestion_2;
769 Error_Msg_NE -- CODEFIX
770 ("\possible misspelling of& or&", Component, Suggestion_1);
771 end if;
772 end Check_Misspelled_Component;
774 ----------------------------------------
775 -- Check_Expr_OK_In_Limited_Aggregate --
776 ----------------------------------------
778 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id) is
779 begin
780 if Is_Limited_Type (Etype (Expr))
781 and then Comes_From_Source (Expr)
782 and then not In_Instance_Body
783 then
784 if not OK_For_Limited_Init (Etype (Expr), Expr) then
785 Error_Msg_N ("initialization not allowed for limited types", Expr);
786 Explain_Limited_Type (Etype (Expr), Expr);
787 end if;
788 end if;
789 end Check_Expr_OK_In_Limited_Aggregate;
791 ----------------------------------------
792 -- Check_Static_Discriminated_Subtype --
793 ----------------------------------------
795 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id) is
796 Disc : constant Entity_Id := First_Discriminant (T);
797 Comp : Entity_Id;
798 Ind : Entity_Id;
800 begin
801 if Has_Record_Rep_Clause (T) then
802 return;
804 elsif Present (Next_Discriminant (Disc)) then
805 return;
807 elsif Nkind (V) /= N_Integer_Literal then
808 return;
809 end if;
811 Comp := First_Component (T);
812 while Present (Comp) loop
813 if Is_Scalar_Type (Etype (Comp)) then
814 null;
816 elsif Is_Private_Type (Etype (Comp))
817 and then Present (Full_View (Etype (Comp)))
818 and then Is_Scalar_Type (Full_View (Etype (Comp)))
819 then
820 null;
822 elsif Is_Array_Type (Etype (Comp)) then
823 if Is_Bit_Packed_Array (Etype (Comp)) then
824 return;
825 end if;
827 Ind := First_Index (Etype (Comp));
828 while Present (Ind) loop
829 if Nkind (Ind) /= N_Range
830 or else Nkind (Low_Bound (Ind)) /= N_Integer_Literal
831 or else Nkind (High_Bound (Ind)) /= N_Integer_Literal
832 then
833 return;
834 end if;
836 Next_Index (Ind);
837 end loop;
839 else
840 return;
841 end if;
843 Next_Component (Comp);
844 end loop;
846 -- On exit, all components have statically known sizes
848 Set_Size_Known_At_Compile_Time (T);
849 end Check_Static_Discriminated_Subtype;
851 --------------------------------
852 -- Make_String_Into_Aggregate --
853 --------------------------------
855 procedure Make_String_Into_Aggregate (N : Node_Id) is
856 Exprs : constant List_Id := New_List;
857 Loc : constant Source_Ptr := Sloc (N);
858 Str : constant String_Id := Strval (N);
859 Strlen : constant Nat := String_Length (Str);
860 C : Char_Code;
861 C_Node : Node_Id;
862 New_N : Node_Id;
863 P : Source_Ptr;
865 begin
866 P := Loc + 1;
867 for J in 1 .. Strlen loop
868 C := Get_String_Char (Str, J);
869 Set_Character_Literal_Name (C);
871 C_Node :=
872 Make_Character_Literal (P,
873 Chars => Name_Find,
874 Char_Literal_Value => UI_From_CC (C));
875 Set_Etype (C_Node, Any_Character);
876 Append_To (Exprs, C_Node);
878 P := P + 1;
879 -- Something special for wide strings???
880 end loop;
882 New_N := Make_Aggregate (Loc, Expressions => Exprs);
883 Set_Analyzed (New_N);
884 Set_Etype (New_N, Any_Composite);
886 Rewrite (N, New_N);
887 end Make_String_Into_Aggregate;
889 -----------------------
890 -- Resolve_Aggregate --
891 -----------------------
893 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
894 Loc : constant Source_Ptr := Sloc (N);
895 Pkind : constant Node_Kind := Nkind (Parent (N));
897 Aggr_Subtyp : Entity_Id;
898 -- The actual aggregate subtype. This is not necessarily the same as Typ
899 -- which is the subtype of the context in which the aggregate was found.
901 begin
902 -- Ignore junk empty aggregate resulting from parser error
904 if No (Expressions (N))
905 and then No (Component_Associations (N))
906 and then not Null_Record_Present (N)
907 then
908 return;
909 end if;
911 -- Check for aggregates not allowed in configurable run-time mode.
912 -- We allow all cases of aggregates that do not come from source, since
913 -- these are all assumed to be small (e.g. bounds of a string literal).
914 -- We also allow aggregates of types we know to be small.
916 if not Support_Aggregates_On_Target
917 and then Comes_From_Source (N)
918 and then (not Known_Static_Esize (Typ) or else Esize (Typ) > 64)
919 then
920 Error_Msg_CRT ("aggregate", N);
921 end if;
923 -- Ada 2005 (AI-287): Limited aggregates allowed
925 if Is_Limited_Type (Typ) and then Ada_Version < Ada_2005 then
926 Error_Msg_N ("aggregate type cannot be limited", N);
927 Explain_Limited_Type (Typ, N);
929 elsif Is_Class_Wide_Type (Typ) then
930 Error_Msg_N ("type of aggregate cannot be class-wide", N);
932 elsif Typ = Any_String
933 or else Typ = Any_Composite
934 then
935 Error_Msg_N ("no unique type for aggregate", N);
936 Set_Etype (N, Any_Composite);
938 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
939 Error_Msg_N ("null record forbidden in array aggregate", N);
941 elsif Is_Record_Type (Typ) then
942 Resolve_Record_Aggregate (N, Typ);
944 elsif Is_Array_Type (Typ) then
946 -- First a special test, for the case of a positional aggregate
947 -- of characters which can be replaced by a string literal.
949 -- Do not perform this transformation if this was a string literal to
950 -- start with, whose components needed constraint checks, or if the
951 -- component type is non-static, because it will require those checks
952 -- and be transformed back into an aggregate.
954 if Number_Dimensions (Typ) = 1
955 and then Is_Standard_Character_Type (Component_Type (Typ))
956 and then No (Component_Associations (N))
957 and then not Is_Limited_Composite (Typ)
958 and then not Is_Private_Composite (Typ)
959 and then not Is_Bit_Packed_Array (Typ)
960 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
961 and then Is_Static_Subtype (Component_Type (Typ))
962 then
963 declare
964 Expr : Node_Id;
966 begin
967 Expr := First (Expressions (N));
968 while Present (Expr) loop
969 exit when Nkind (Expr) /= N_Character_Literal;
970 Next (Expr);
971 end loop;
973 if No (Expr) then
974 Start_String;
976 Expr := First (Expressions (N));
977 while Present (Expr) loop
978 Store_String_Char (UI_To_CC (Char_Literal_Value (Expr)));
979 Next (Expr);
980 end loop;
982 Rewrite (N, Make_String_Literal (Loc, End_String));
984 Analyze_And_Resolve (N, Typ);
985 return;
986 end if;
987 end;
988 end if;
990 -- Here if we have a real aggregate to deal with
992 Array_Aggregate : declare
993 Aggr_Resolved : Boolean;
995 Aggr_Typ : constant Entity_Id := Etype (Typ);
996 -- This is the unconstrained array type, which is the type against
997 -- which the aggregate is to be resolved. Typ itself is the array
998 -- type of the context which may not be the same subtype as the
999 -- subtype for the final aggregate.
1001 begin
1002 -- In the following we determine whether an OTHERS choice is
1003 -- allowed inside the array aggregate. The test checks the context
1004 -- in which the array aggregate occurs. If the context does not
1005 -- permit it, or the aggregate type is unconstrained, an OTHERS
1006 -- choice is not allowed.
1008 -- If expansion is disabled (generic context, or semantics-only
1009 -- mode) actual subtypes cannot be constructed, and the type of an
1010 -- object may be its unconstrained nominal type. However, if the
1011 -- context is an assignment, we assume that OTHERS is allowed,
1012 -- because the target of the assignment will have a constrained
1013 -- subtype when fully compiled.
1015 -- Note that there is no node for Explicit_Actual_Parameter.
1016 -- To test for this context we therefore have to test for node
1017 -- N_Parameter_Association which itself appears only if there is a
1018 -- formal parameter. Consequently we also need to test for
1019 -- N_Procedure_Call_Statement or N_Function_Call.
1021 Set_Etype (N, Aggr_Typ); -- May be overridden later on
1023 if Is_Constrained (Typ) and then
1024 (Pkind = N_Assignment_Statement or else
1025 Pkind = N_Parameter_Association or else
1026 Pkind = N_Function_Call or else
1027 Pkind = N_Procedure_Call_Statement or else
1028 Pkind = N_Generic_Association or else
1029 Pkind = N_Formal_Object_Declaration or else
1030 Pkind = N_Simple_Return_Statement or else
1031 Pkind = N_Object_Declaration or else
1032 Pkind = N_Component_Declaration or else
1033 Pkind = N_Parameter_Specification or else
1034 Pkind = N_Qualified_Expression or else
1035 Pkind = N_Aggregate or else
1036 Pkind = N_Extension_Aggregate or else
1037 Pkind = N_Component_Association)
1038 then
1039 Aggr_Resolved :=
1040 Resolve_Array_Aggregate
1042 Index => First_Index (Aggr_Typ),
1043 Index_Constr => First_Index (Typ),
1044 Component_Typ => Component_Type (Typ),
1045 Others_Allowed => True);
1047 elsif not Expander_Active
1048 and then Pkind = N_Assignment_Statement
1049 then
1050 Aggr_Resolved :=
1051 Resolve_Array_Aggregate
1053 Index => First_Index (Aggr_Typ),
1054 Index_Constr => First_Index (Typ),
1055 Component_Typ => Component_Type (Typ),
1056 Others_Allowed => True);
1058 else
1059 Aggr_Resolved :=
1060 Resolve_Array_Aggregate
1062 Index => First_Index (Aggr_Typ),
1063 Index_Constr => First_Index (Aggr_Typ),
1064 Component_Typ => Component_Type (Typ),
1065 Others_Allowed => False);
1066 end if;
1068 if not Aggr_Resolved then
1069 Aggr_Subtyp := Any_Composite;
1070 else
1071 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1072 end if;
1074 Set_Etype (N, Aggr_Subtyp);
1075 end Array_Aggregate;
1077 elsif Is_Private_Type (Typ)
1078 and then Present (Full_View (Typ))
1079 and then In_Inlined_Body
1080 and then Is_Composite_Type (Full_View (Typ))
1081 then
1082 Resolve (N, Full_View (Typ));
1084 else
1085 Error_Msg_N ("illegal context for aggregate", N);
1086 end if;
1088 -- If we can determine statically that the evaluation of the aggregate
1089 -- raises Constraint_Error, then replace the aggregate with an
1090 -- N_Raise_Constraint_Error node, but set the Etype to the right
1091 -- aggregate subtype. Gigi needs this.
1093 if Raises_Constraint_Error (N) then
1094 Aggr_Subtyp := Etype (N);
1095 Rewrite (N,
1096 Make_Raise_Constraint_Error (Loc,
1097 Reason => CE_Range_Check_Failed));
1098 Set_Raises_Constraint_Error (N);
1099 Set_Etype (N, Aggr_Subtyp);
1100 Set_Analyzed (N);
1101 end if;
1102 end Resolve_Aggregate;
1104 -----------------------------
1105 -- Resolve_Array_Aggregate --
1106 -----------------------------
1108 function Resolve_Array_Aggregate
1109 (N : Node_Id;
1110 Index : Node_Id;
1111 Index_Constr : Node_Id;
1112 Component_Typ : Entity_Id;
1113 Others_Allowed : Boolean) return Boolean
1115 Loc : constant Source_Ptr := Sloc (N);
1117 Failure : constant Boolean := False;
1118 Success : constant Boolean := True;
1120 Index_Typ : constant Entity_Id := Etype (Index);
1121 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1122 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1123 -- The type of the index corresponding to the array sub-aggregate along
1124 -- with its low and upper bounds.
1126 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1127 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1128 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1129 -- Ditto for the base type
1131 function Add (Val : Uint; To : Node_Id) return Node_Id;
1132 -- Creates a new expression node where Val is added to expression To.
1133 -- Tries to constant fold whenever possible. To must be an already
1134 -- analyzed expression.
1136 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1137 -- Checks that AH (the upper bound of an array aggregate) is less than
1138 -- or equal to BH (the upper bound of the index base type). If the check
1139 -- fails, a warning is emitted, the Raises_Constraint_Error flag of N is
1140 -- set, and AH is replaced with a duplicate of BH.
1142 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1143 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1144 -- warning if not and sets the Raises_Constraint_Error flag in N.
1146 procedure Check_Length (L, H : Node_Id; Len : Uint);
1147 -- Checks that range L .. H contains at least Len elements. Emits a
1148 -- warning if not and sets the Raises_Constraint_Error flag in N.
1150 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1151 -- Returns True if range L .. H is dynamic or null
1153 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1154 -- Given expression node From, this routine sets OK to False if it
1155 -- cannot statically evaluate From. Otherwise it stores this static
1156 -- value into Value.
1158 function Resolve_Aggr_Expr
1159 (Expr : Node_Id;
1160 Single_Elmt : Boolean) return Boolean;
1161 -- Resolves aggregate expression Expr. Returns False if resolution
1162 -- fails. If Single_Elmt is set to False, the expression Expr may be
1163 -- used to initialize several array aggregate elements (this can happen
1164 -- for discrete choices such as "L .. H => Expr" or the OTHERS choice).
1165 -- In this event we do not resolve Expr unless expansion is disabled.
1166 -- To know why, see the DELAYED COMPONENT RESOLUTION note above.
1168 ---------
1169 -- Add --
1170 ---------
1172 function Add (Val : Uint; To : Node_Id) return Node_Id is
1173 Expr_Pos : Node_Id;
1174 Expr : Node_Id;
1175 To_Pos : Node_Id;
1177 begin
1178 if Raises_Constraint_Error (To) then
1179 return To;
1180 end if;
1182 -- First test if we can do constant folding
1184 if Compile_Time_Known_Value (To)
1185 or else Nkind (To) = N_Integer_Literal
1186 then
1187 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1188 Set_Is_Static_Expression (Expr_Pos);
1189 Set_Etype (Expr_Pos, Etype (To));
1190 Set_Analyzed (Expr_Pos, Analyzed (To));
1192 if not Is_Enumeration_Type (Index_Typ) then
1193 Expr := Expr_Pos;
1195 -- If we are dealing with enumeration return
1196 -- Index_Typ'Val (Expr_Pos)
1198 else
1199 Expr :=
1200 Make_Attribute_Reference
1201 (Loc,
1202 Prefix => New_Reference_To (Index_Typ, Loc),
1203 Attribute_Name => Name_Val,
1204 Expressions => New_List (Expr_Pos));
1205 end if;
1207 return Expr;
1208 end if;
1210 -- If we are here no constant folding possible
1212 if not Is_Enumeration_Type (Index_Base) then
1213 Expr :=
1214 Make_Op_Add (Loc,
1215 Left_Opnd => Duplicate_Subexpr (To),
1216 Right_Opnd => Make_Integer_Literal (Loc, Val));
1218 -- If we are dealing with enumeration return
1219 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1221 else
1222 To_Pos :=
1223 Make_Attribute_Reference
1224 (Loc,
1225 Prefix => New_Reference_To (Index_Typ, Loc),
1226 Attribute_Name => Name_Pos,
1227 Expressions => New_List (Duplicate_Subexpr (To)));
1229 Expr_Pos :=
1230 Make_Op_Add (Loc,
1231 Left_Opnd => To_Pos,
1232 Right_Opnd => Make_Integer_Literal (Loc, Val));
1234 Expr :=
1235 Make_Attribute_Reference
1236 (Loc,
1237 Prefix => New_Reference_To (Index_Typ, Loc),
1238 Attribute_Name => Name_Val,
1239 Expressions => New_List (Expr_Pos));
1241 -- If the index type has a non standard representation, the
1242 -- attributes 'Val and 'Pos expand into function calls and the
1243 -- resulting expression is considered non-safe for reevaluation
1244 -- by the backend. Relocate it into a constant temporary in order
1245 -- to make it safe for reevaluation.
1247 if Has_Non_Standard_Rep (Etype (N)) then
1248 declare
1249 Def_Id : Entity_Id;
1251 begin
1252 Def_Id := Make_Temporary (Loc, 'R', Expr);
1253 Set_Etype (Def_Id, Index_Typ);
1254 Insert_Action (N,
1255 Make_Object_Declaration (Loc,
1256 Defining_Identifier => Def_Id,
1257 Object_Definition => New_Reference_To (Index_Typ, Loc),
1258 Constant_Present => True,
1259 Expression => Relocate_Node (Expr)));
1261 Expr := New_Reference_To (Def_Id, Loc);
1262 end;
1263 end if;
1264 end if;
1266 return Expr;
1267 end Add;
1269 -----------------
1270 -- Check_Bound --
1271 -----------------
1273 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1274 Val_BH : Uint;
1275 Val_AH : Uint;
1277 OK_BH : Boolean;
1278 OK_AH : Boolean;
1280 begin
1281 Get (Value => Val_BH, From => BH, OK => OK_BH);
1282 Get (Value => Val_AH, From => AH, OK => OK_AH);
1284 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1285 Set_Raises_Constraint_Error (N);
1286 Error_Msg_N ("upper bound out of range?", AH);
1287 Error_Msg_N ("\Constraint_Error will be raised at run time?", AH);
1289 -- You need to set AH to BH or else in the case of enumerations
1290 -- indexes we will not be able to resolve the aggregate bounds.
1292 AH := Duplicate_Subexpr (BH);
1293 end if;
1294 end Check_Bound;
1296 ------------------
1297 -- Check_Bounds --
1298 ------------------
1300 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1301 Val_L : Uint;
1302 Val_H : Uint;
1303 Val_AL : Uint;
1304 Val_AH : Uint;
1306 OK_L : Boolean;
1307 OK_H : Boolean;
1309 OK_AL : Boolean;
1310 OK_AH : Boolean;
1311 pragma Warnings (Off, OK_AL);
1312 pragma Warnings (Off, OK_AH);
1314 begin
1315 if Raises_Constraint_Error (N)
1316 or else Dynamic_Or_Null_Range (AL, AH)
1317 then
1318 return;
1319 end if;
1321 Get (Value => Val_L, From => L, OK => OK_L);
1322 Get (Value => Val_H, From => H, OK => OK_H);
1324 Get (Value => Val_AL, From => AL, OK => OK_AL);
1325 Get (Value => Val_AH, From => AH, OK => OK_AH);
1327 if OK_L and then Val_L > Val_AL then
1328 Set_Raises_Constraint_Error (N);
1329 Error_Msg_N ("lower bound of aggregate out of range?", N);
1330 Error_Msg_N ("\Constraint_Error will be raised at run time?", N);
1331 end if;
1333 if OK_H and then Val_H < Val_AH then
1334 Set_Raises_Constraint_Error (N);
1335 Error_Msg_N ("upper bound of aggregate out of range?", N);
1336 Error_Msg_N ("\Constraint_Error will be raised at run time?", N);
1337 end if;
1338 end Check_Bounds;
1340 ------------------
1341 -- Check_Length --
1342 ------------------
1344 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1345 Val_L : Uint;
1346 Val_H : Uint;
1348 OK_L : Boolean;
1349 OK_H : Boolean;
1351 Range_Len : Uint;
1353 begin
1354 if Raises_Constraint_Error (N) then
1355 return;
1356 end if;
1358 Get (Value => Val_L, From => L, OK => OK_L);
1359 Get (Value => Val_H, From => H, OK => OK_H);
1361 if not OK_L or else not OK_H then
1362 return;
1363 end if;
1365 -- If null range length is zero
1367 if Val_L > Val_H then
1368 Range_Len := Uint_0;
1369 else
1370 Range_Len := Val_H - Val_L + 1;
1371 end if;
1373 if Range_Len < Len then
1374 Set_Raises_Constraint_Error (N);
1375 Error_Msg_N ("too many elements?", N);
1376 Error_Msg_N ("\Constraint_Error will be raised at run time?", N);
1377 end if;
1378 end Check_Length;
1380 ---------------------------
1381 -- Dynamic_Or_Null_Range --
1382 ---------------------------
1384 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1385 Val_L : Uint;
1386 Val_H : Uint;
1388 OK_L : Boolean;
1389 OK_H : Boolean;
1391 begin
1392 Get (Value => Val_L, From => L, OK => OK_L);
1393 Get (Value => Val_H, From => H, OK => OK_H);
1395 return not OK_L or else not OK_H
1396 or else not Is_OK_Static_Expression (L)
1397 or else not Is_OK_Static_Expression (H)
1398 or else Val_L > Val_H;
1399 end Dynamic_Or_Null_Range;
1401 ---------
1402 -- Get --
1403 ---------
1405 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1406 begin
1407 OK := True;
1409 if Compile_Time_Known_Value (From) then
1410 Value := Expr_Value (From);
1412 -- If expression From is something like Some_Type'Val (10) then
1413 -- Value = 10
1415 elsif Nkind (From) = N_Attribute_Reference
1416 and then Attribute_Name (From) = Name_Val
1417 and then Compile_Time_Known_Value (First (Expressions (From)))
1418 then
1419 Value := Expr_Value (First (Expressions (From)));
1421 else
1422 Value := Uint_0;
1423 OK := False;
1424 end if;
1425 end Get;
1427 -----------------------
1428 -- Resolve_Aggr_Expr --
1429 -----------------------
1431 function Resolve_Aggr_Expr
1432 (Expr : Node_Id;
1433 Single_Elmt : Boolean) return Boolean
1435 Nxt_Ind : constant Node_Id := Next_Index (Index);
1436 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
1437 -- Index is the current index corresponding to the expression
1439 Resolution_OK : Boolean := True;
1440 -- Set to False if resolution of the expression failed
1442 begin
1443 -- Defend against previous errors
1445 if Nkind (Expr) = N_Error
1446 or else Error_Posted (Expr)
1447 then
1448 return True;
1449 end if;
1451 -- If the array type against which we are resolving the aggregate
1452 -- has several dimensions, the expressions nested inside the
1453 -- aggregate must be further aggregates (or strings).
1455 if Present (Nxt_Ind) then
1456 if Nkind (Expr) /= N_Aggregate then
1458 -- A string literal can appear where a one-dimensional array
1459 -- of characters is expected. If the literal looks like an
1460 -- operator, it is still an operator symbol, which will be
1461 -- transformed into a string when analyzed.
1463 if Is_Character_Type (Component_Typ)
1464 and then No (Next_Index (Nxt_Ind))
1465 and then Nkind_In (Expr, N_String_Literal, N_Operator_Symbol)
1466 then
1467 -- A string literal used in a multidimensional array
1468 -- aggregate in place of the final one-dimensional
1469 -- aggregate must not be enclosed in parentheses.
1471 if Paren_Count (Expr) /= 0 then
1472 Error_Msg_N ("no parenthesis allowed here", Expr);
1473 end if;
1475 Make_String_Into_Aggregate (Expr);
1477 else
1478 Error_Msg_N ("nested array aggregate expected", Expr);
1480 -- If the expression is parenthesized, this may be
1481 -- a missing component association for a 1-aggregate.
1483 if Paren_Count (Expr) > 0 then
1484 Error_Msg_N
1485 ("\if single-component aggregate is intended,"
1486 & " write e.g. (1 ='> ...)", Expr);
1487 end if;
1488 return Failure;
1489 end if;
1490 end if;
1492 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
1493 -- Required to check the null-exclusion attribute (if present).
1494 -- This value may be overridden later on.
1496 Set_Etype (Expr, Etype (N));
1498 Resolution_OK := Resolve_Array_Aggregate
1499 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1501 -- Do not resolve the expressions of discrete or others choices
1502 -- unless the expression covers a single component, or the expander
1503 -- is inactive.
1505 elsif Single_Elmt
1506 or else not Expander_Active
1507 or else In_Spec_Expression
1508 then
1509 Analyze_And_Resolve (Expr, Component_Typ);
1510 Check_Expr_OK_In_Limited_Aggregate (Expr);
1511 Check_Non_Static_Context (Expr);
1512 Aggregate_Constraint_Checks (Expr, Component_Typ);
1513 Check_Unset_Reference (Expr);
1514 end if;
1516 if Raises_Constraint_Error (Expr)
1517 and then Nkind (Parent (Expr)) /= N_Component_Association
1518 then
1519 Set_Raises_Constraint_Error (N);
1520 end if;
1522 -- If the expression has been marked as requiring a range check,
1523 -- then generate it here.
1525 if Do_Range_Check (Expr) then
1526 Set_Do_Range_Check (Expr, False);
1527 Generate_Range_Check (Expr, Component_Typ, CE_Range_Check_Failed);
1528 end if;
1530 return Resolution_OK;
1531 end Resolve_Aggr_Expr;
1533 -- Variables local to Resolve_Array_Aggregate
1535 Assoc : Node_Id;
1536 Choice : Node_Id;
1537 Expr : Node_Id;
1539 Discard : Node_Id;
1540 pragma Warnings (Off, Discard);
1542 Aggr_Low : Node_Id := Empty;
1543 Aggr_High : Node_Id := Empty;
1544 -- The actual low and high bounds of this sub-aggregate
1546 Choices_Low : Node_Id := Empty;
1547 Choices_High : Node_Id := Empty;
1548 -- The lowest and highest discrete choices values for a named aggregate
1550 Nb_Elements : Uint := Uint_0;
1551 -- The number of elements in a positional aggregate
1553 Others_Present : Boolean := False;
1555 Nb_Choices : Nat := 0;
1556 -- Contains the overall number of named choices in this sub-aggregate
1558 Nb_Discrete_Choices : Nat := 0;
1559 -- The overall number of discrete choices (not counting others choice)
1561 Case_Table_Size : Nat;
1562 -- Contains the size of the case table needed to sort aggregate choices
1564 -- Start of processing for Resolve_Array_Aggregate
1566 begin
1567 -- Ignore junk empty aggregate resulting from parser error
1569 if No (Expressions (N))
1570 and then No (Component_Associations (N))
1571 and then not Null_Record_Present (N)
1572 then
1573 return False;
1574 end if;
1576 -- STEP 1: make sure the aggregate is correctly formatted
1578 if Present (Component_Associations (N)) then
1579 Assoc := First (Component_Associations (N));
1580 while Present (Assoc) loop
1581 Choice := First (Choices (Assoc));
1582 while Present (Choice) loop
1583 if Nkind (Choice) = N_Others_Choice then
1584 Others_Present := True;
1586 if Choice /= First (Choices (Assoc))
1587 or else Present (Next (Choice))
1588 then
1589 Error_Msg_N
1590 ("OTHERS must appear alone in a choice list", Choice);
1591 return Failure;
1592 end if;
1594 if Present (Next (Assoc)) then
1595 Error_Msg_N
1596 ("OTHERS must appear last in an aggregate", Choice);
1597 return Failure;
1598 end if;
1600 if Ada_Version = Ada_83
1601 and then Assoc /= First (Component_Associations (N))
1602 and then Nkind_In (Parent (N), N_Assignment_Statement,
1603 N_Object_Declaration)
1604 then
1605 Error_Msg_N
1606 ("(Ada 83) illegal context for OTHERS choice", N);
1607 end if;
1608 end if;
1610 Nb_Choices := Nb_Choices + 1;
1611 Next (Choice);
1612 end loop;
1614 Next (Assoc);
1615 end loop;
1616 end if;
1618 -- At this point we know that the others choice, if present, is by
1619 -- itself and appears last in the aggregate. Check if we have mixed
1620 -- positional and discrete associations (other than the others choice).
1622 if Present (Expressions (N))
1623 and then (Nb_Choices > 1
1624 or else (Nb_Choices = 1 and then not Others_Present))
1625 then
1626 Error_Msg_N
1627 ("named association cannot follow positional association",
1628 First (Choices (First (Component_Associations (N)))));
1629 return Failure;
1630 end if;
1632 -- Test for the validity of an others choice if present
1634 if Others_Present and then not Others_Allowed then
1635 Error_Msg_N
1636 ("OTHERS choice not allowed here",
1637 First (Choices (First (Component_Associations (N)))));
1638 return Failure;
1639 end if;
1641 -- Protect against cascaded errors
1643 if Etype (Index_Typ) = Any_Type then
1644 return Failure;
1645 end if;
1647 -- STEP 2: Process named components
1649 if No (Expressions (N)) then
1650 if Others_Present then
1651 Case_Table_Size := Nb_Choices - 1;
1652 else
1653 Case_Table_Size := Nb_Choices;
1654 end if;
1656 Step_2 : declare
1657 Low : Node_Id;
1658 High : Node_Id;
1659 -- Denote the lowest and highest values in an aggregate choice
1661 Hi_Val : Uint;
1662 Lo_Val : Uint;
1663 -- High end of one range and Low end of the next. Should be
1664 -- contiguous if there is no hole in the list of values.
1666 Missing_Values : Boolean;
1667 -- Set True if missing index values
1669 S_Low : Node_Id := Empty;
1670 S_High : Node_Id := Empty;
1671 -- if a choice in an aggregate is a subtype indication these
1672 -- denote the lowest and highest values of the subtype
1674 Table : Case_Table_Type (1 .. Case_Table_Size);
1675 -- Used to sort all the different choice values
1677 Single_Choice : Boolean;
1678 -- Set to true every time there is a single discrete choice in a
1679 -- discrete association
1681 Prev_Nb_Discrete_Choices : Nat;
1682 -- Used to keep track of the number of discrete choices in the
1683 -- current association.
1685 begin
1686 -- STEP 2 (A): Check discrete choices validity
1688 Assoc := First (Component_Associations (N));
1689 while Present (Assoc) loop
1690 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
1691 Choice := First (Choices (Assoc));
1692 loop
1693 Analyze (Choice);
1695 if Nkind (Choice) = N_Others_Choice then
1696 Single_Choice := False;
1697 exit;
1699 -- Test for subtype mark without constraint
1701 elsif Is_Entity_Name (Choice) and then
1702 Is_Type (Entity (Choice))
1703 then
1704 if Base_Type (Entity (Choice)) /= Index_Base then
1705 Error_Msg_N
1706 ("invalid subtype mark in aggregate choice",
1707 Choice);
1708 return Failure;
1709 end if;
1711 -- Case of subtype indication
1713 elsif Nkind (Choice) = N_Subtype_Indication then
1714 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
1716 -- Does the subtype indication evaluation raise CE ?
1718 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
1719 Get_Index_Bounds (Choice, Low, High);
1720 Check_Bounds (S_Low, S_High, Low, High);
1722 -- Case of range or expression
1724 else
1725 Resolve (Choice, Index_Base);
1726 Check_Unset_Reference (Choice);
1727 Check_Non_Static_Context (Choice);
1729 -- Do not range check a choice. This check is redundant
1730 -- since this test is already done when we check that the
1731 -- bounds of the array aggregate are within range.
1733 Set_Do_Range_Check (Choice, False);
1734 end if;
1736 -- If we could not resolve the discrete choice stop here
1738 if Etype (Choice) = Any_Type then
1739 return Failure;
1741 -- If the discrete choice raises CE get its original bounds
1743 elsif Nkind (Choice) = N_Raise_Constraint_Error then
1744 Set_Raises_Constraint_Error (N);
1745 Get_Index_Bounds (Original_Node (Choice), Low, High);
1747 -- Otherwise get its bounds as usual
1749 else
1750 Get_Index_Bounds (Choice, Low, High);
1751 end if;
1753 if (Dynamic_Or_Null_Range (Low, High)
1754 or else (Nkind (Choice) = N_Subtype_Indication
1755 and then
1756 Dynamic_Or_Null_Range (S_Low, S_High)))
1757 and then Nb_Choices /= 1
1758 then
1759 Error_Msg_N
1760 ("dynamic or empty choice in aggregate " &
1761 "must be the only choice", Choice);
1762 return Failure;
1763 end if;
1765 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
1766 Table (Nb_Discrete_Choices).Choice_Lo := Low;
1767 Table (Nb_Discrete_Choices).Choice_Hi := High;
1769 Next (Choice);
1771 if No (Choice) then
1773 -- Check if we have a single discrete choice and whether
1774 -- this discrete choice specifies a single value.
1776 Single_Choice :=
1777 (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1)
1778 and then (Low = High);
1780 exit;
1781 end if;
1782 end loop;
1784 -- Ada 2005 (AI-231)
1786 if Ada_Version >= Ada_2005
1787 and then Known_Null (Expression (Assoc))
1788 then
1789 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
1790 end if;
1792 -- Ada 2005 (AI-287): In case of default initialized component
1793 -- we delay the resolution to the expansion phase.
1795 if Box_Present (Assoc) then
1797 -- Ada 2005 (AI-287): In case of default initialization of a
1798 -- component the expander will generate calls to the
1799 -- corresponding initialization subprogram.
1801 null;
1803 elsif not Resolve_Aggr_Expr (Expression (Assoc),
1804 Single_Elmt => Single_Choice)
1805 then
1806 return Failure;
1808 -- Check incorrect use of dynamically tagged expression
1810 -- We differentiate here two cases because the expression may
1811 -- not be decorated. For example, the analysis and resolution
1812 -- of the expression associated with the others choice will be
1813 -- done later with the full aggregate. In such case we
1814 -- duplicate the expression tree to analyze the copy and
1815 -- perform the required check.
1817 elsif not Present (Etype (Expression (Assoc))) then
1818 declare
1819 Save_Analysis : constant Boolean := Full_Analysis;
1820 Expr : constant Node_Id :=
1821 New_Copy_Tree (Expression (Assoc));
1823 begin
1824 Expander_Mode_Save_And_Set (False);
1825 Full_Analysis := False;
1826 Analyze (Expr);
1828 -- If the expression is a literal, propagate this info
1829 -- to the expression in the association, to enable some
1830 -- optimizations downstream.
1832 if Is_Entity_Name (Expr)
1833 and then Present (Entity (Expr))
1834 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
1835 then
1836 Analyze_And_Resolve
1837 (Expression (Assoc), Component_Typ);
1838 end if;
1840 Full_Analysis := Save_Analysis;
1841 Expander_Mode_Restore;
1843 if Is_Tagged_Type (Etype (Expr)) then
1844 Check_Dynamically_Tagged_Expression
1845 (Expr => Expr,
1846 Typ => Component_Type (Etype (N)),
1847 Related_Nod => N);
1848 end if;
1849 end;
1851 elsif Is_Tagged_Type (Etype (Expression (Assoc))) then
1852 Check_Dynamically_Tagged_Expression
1853 (Expr => Expression (Assoc),
1854 Typ => Component_Type (Etype (N)),
1855 Related_Nod => N);
1856 end if;
1858 Next (Assoc);
1859 end loop;
1861 -- If aggregate contains more than one choice then these must be
1862 -- static. Sort them and check that they are contiguous.
1864 if Nb_Discrete_Choices > 1 then
1865 Sort_Case_Table (Table);
1866 Missing_Values := False;
1868 Outer : for J in 1 .. Nb_Discrete_Choices - 1 loop
1869 if Expr_Value (Table (J).Choice_Hi) >=
1870 Expr_Value (Table (J + 1).Choice_Lo)
1871 then
1872 Error_Msg_N
1873 ("duplicate choice values in array aggregate",
1874 Table (J).Choice_Hi);
1875 return Failure;
1877 elsif not Others_Present then
1878 Hi_Val := Expr_Value (Table (J).Choice_Hi);
1879 Lo_Val := Expr_Value (Table (J + 1).Choice_Lo);
1881 -- If missing values, output error messages
1883 if Lo_Val - Hi_Val > 1 then
1885 -- Header message if not first missing value
1887 if not Missing_Values then
1888 Error_Msg_N
1889 ("missing index value(s) in array aggregate", N);
1890 Missing_Values := True;
1891 end if;
1893 -- Output values of missing indexes
1895 Lo_Val := Lo_Val - 1;
1896 Hi_Val := Hi_Val + 1;
1898 -- Enumeration type case
1900 if Is_Enumeration_Type (Index_Typ) then
1901 Error_Msg_Name_1 :=
1902 Chars
1903 (Get_Enum_Lit_From_Pos
1904 (Index_Typ, Hi_Val, Loc));
1906 if Lo_Val = Hi_Val then
1907 Error_Msg_N ("\ %", N);
1908 else
1909 Error_Msg_Name_2 :=
1910 Chars
1911 (Get_Enum_Lit_From_Pos
1912 (Index_Typ, Lo_Val, Loc));
1913 Error_Msg_N ("\ % .. %", N);
1914 end if;
1916 -- Integer types case
1918 else
1919 Error_Msg_Uint_1 := Hi_Val;
1921 if Lo_Val = Hi_Val then
1922 Error_Msg_N ("\ ^", N);
1923 else
1924 Error_Msg_Uint_2 := Lo_Val;
1925 Error_Msg_N ("\ ^ .. ^", N);
1926 end if;
1927 end if;
1928 end if;
1929 end if;
1930 end loop Outer;
1932 if Missing_Values then
1933 Set_Etype (N, Any_Composite);
1934 return Failure;
1935 end if;
1936 end if;
1938 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
1940 if Nb_Discrete_Choices > 0 then
1941 Choices_Low := Table (1).Choice_Lo;
1942 Choices_High := Table (Nb_Discrete_Choices).Choice_Hi;
1943 end if;
1945 -- If Others is present, then bounds of aggregate come from the
1946 -- index constraint (not the choices in the aggregate itself).
1948 if Others_Present then
1949 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
1951 -- No others clause present
1953 else
1954 -- Special processing if others allowed and not present. This
1955 -- means that the bounds of the aggregate come from the index
1956 -- constraint (and the length must match).
1958 if Others_Allowed then
1959 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
1961 -- If others allowed, and no others present, then the array
1962 -- should cover all index values. If it does not, we will
1963 -- get a length check warning, but there is two cases where
1964 -- an additional warning is useful:
1966 -- If we have no positional components, and the length is
1967 -- wrong (which we can tell by others being allowed with
1968 -- missing components), and the index type is an enumeration
1969 -- type, then issue appropriate warnings about these missing
1970 -- components. They are only warnings, since the aggregate
1971 -- is fine, it's just the wrong length. We skip this check
1972 -- for standard character types (since there are no literals
1973 -- and it is too much trouble to concoct them), and also if
1974 -- any of the bounds have not-known-at-compile-time values.
1976 -- Another case warranting a warning is when the length is
1977 -- right, but as above we have an index type that is an
1978 -- enumeration, and the bounds do not match. This is a
1979 -- case where dubious sliding is allowed and we generate
1980 -- a warning that the bounds do not match.
1982 if No (Expressions (N))
1983 and then Nkind (Index) = N_Range
1984 and then Is_Enumeration_Type (Etype (Index))
1985 and then not Is_Standard_Character_Type (Etype (Index))
1986 and then Compile_Time_Known_Value (Aggr_Low)
1987 and then Compile_Time_Known_Value (Aggr_High)
1988 and then Compile_Time_Known_Value (Choices_Low)
1989 and then Compile_Time_Known_Value (Choices_High)
1990 then
1991 -- If the bounds have semantic errors, do not attempt
1992 -- further resolution to prevent cascaded errors.
1994 if Error_Posted (Choices_Low)
1995 or else Error_Posted (Choices_High)
1996 then
1997 return False;
1998 end if;
2000 declare
2001 ALo : constant Node_Id := Expr_Value_E (Aggr_Low);
2002 AHi : constant Node_Id := Expr_Value_E (Aggr_High);
2003 CLo : constant Node_Id := Expr_Value_E (Choices_Low);
2004 CHi : constant Node_Id := Expr_Value_E (Choices_High);
2006 Ent : Entity_Id;
2008 begin
2009 -- Warning case 1, missing values at start/end. Only
2010 -- do the check if the number of entries is too small.
2012 if (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2014 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2015 then
2016 Error_Msg_N
2017 ("missing index value(s) in array aggregate?", N);
2019 -- Output missing value(s) at start
2021 if Chars (ALo) /= Chars (CLo) then
2022 Ent := Prev (CLo);
2024 if Chars (ALo) = Chars (Ent) then
2025 Error_Msg_Name_1 := Chars (ALo);
2026 Error_Msg_N ("\ %?", N);
2027 else
2028 Error_Msg_Name_1 := Chars (ALo);
2029 Error_Msg_Name_2 := Chars (Ent);
2030 Error_Msg_N ("\ % .. %?", N);
2031 end if;
2032 end if;
2034 -- Output missing value(s) at end
2036 if Chars (AHi) /= Chars (CHi) then
2037 Ent := Next (CHi);
2039 if Chars (AHi) = Chars (Ent) then
2040 Error_Msg_Name_1 := Chars (Ent);
2041 Error_Msg_N ("\ %?", N);
2042 else
2043 Error_Msg_Name_1 := Chars (Ent);
2044 Error_Msg_Name_2 := Chars (AHi);
2045 Error_Msg_N ("\ % .. %?", N);
2046 end if;
2047 end if;
2049 -- Warning case 2, dubious sliding. The First_Subtype
2050 -- test distinguishes between a constrained type where
2051 -- sliding is not allowed (so we will get a warning
2052 -- later that Constraint_Error will be raised), and
2053 -- the unconstrained case where sliding is permitted.
2055 elsif (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2057 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2058 and then Chars (ALo) /= Chars (CLo)
2059 and then
2060 not Is_Constrained (First_Subtype (Etype (N)))
2061 then
2062 Error_Msg_N
2063 ("bounds of aggregate do not match target?", N);
2064 end if;
2065 end;
2066 end if;
2067 end if;
2069 -- If no others, aggregate bounds come from aggregate
2071 Aggr_Low := Choices_Low;
2072 Aggr_High := Choices_High;
2073 end if;
2074 end Step_2;
2076 -- STEP 3: Process positional components
2078 else
2079 -- STEP 3 (A): Process positional elements
2081 Expr := First (Expressions (N));
2082 Nb_Elements := Uint_0;
2083 while Present (Expr) loop
2084 Nb_Elements := Nb_Elements + 1;
2086 -- Ada 2005 (AI-231)
2088 if Ada_Version >= Ada_2005
2089 and then Known_Null (Expr)
2090 then
2091 Check_Can_Never_Be_Null (Etype (N), Expr);
2092 end if;
2094 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
2095 return Failure;
2096 end if;
2098 -- Check incorrect use of dynamically tagged expression
2100 if Is_Tagged_Type (Etype (Expr)) then
2101 Check_Dynamically_Tagged_Expression
2102 (Expr => Expr,
2103 Typ => Component_Type (Etype (N)),
2104 Related_Nod => N);
2105 end if;
2107 Next (Expr);
2108 end loop;
2110 if Others_Present then
2111 Assoc := Last (Component_Associations (N));
2113 -- Ada 2005 (AI-231)
2115 if Ada_Version >= Ada_2005
2116 and then Known_Null (Assoc)
2117 then
2118 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2119 end if;
2121 -- Ada 2005 (AI-287): In case of default initialized component,
2122 -- we delay the resolution to the expansion phase.
2124 if Box_Present (Assoc) then
2126 -- Ada 2005 (AI-287): In case of default initialization of a
2127 -- component the expander will generate calls to the
2128 -- corresponding initialization subprogram.
2130 null;
2132 elsif not Resolve_Aggr_Expr (Expression (Assoc),
2133 Single_Elmt => False)
2134 then
2135 return Failure;
2137 -- Check incorrect use of dynamically tagged expression. The
2138 -- expression of the others choice has not been resolved yet.
2139 -- In order to diagnose the semantic error we create a duplicate
2140 -- tree to analyze it and perform the check.
2142 else
2143 declare
2144 Save_Analysis : constant Boolean := Full_Analysis;
2145 Expr : constant Node_Id :=
2146 New_Copy_Tree (Expression (Assoc));
2148 begin
2149 Expander_Mode_Save_And_Set (False);
2150 Full_Analysis := False;
2151 Analyze (Expr);
2152 Full_Analysis := Save_Analysis;
2153 Expander_Mode_Restore;
2155 if Is_Tagged_Type (Etype (Expr)) then
2156 Check_Dynamically_Tagged_Expression
2157 (Expr => Expr,
2158 Typ => Component_Type (Etype (N)),
2159 Related_Nod => N);
2160 end if;
2161 end;
2162 end if;
2163 end if;
2165 -- STEP 3 (B): Compute the aggregate bounds
2167 if Others_Present then
2168 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2170 else
2171 if Others_Allowed then
2172 Get_Index_Bounds (Index_Constr, Aggr_Low, Discard);
2173 else
2174 Aggr_Low := Index_Typ_Low;
2175 end if;
2177 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
2178 Check_Bound (Index_Base_High, Aggr_High);
2179 end if;
2180 end if;
2182 -- STEP 4: Perform static aggregate checks and save the bounds
2184 -- Check (A)
2186 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
2187 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
2189 -- Check (B)
2191 if Others_Present and then Nb_Discrete_Choices > 0 then
2192 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
2193 Check_Bounds (Index_Typ_Low, Index_Typ_High,
2194 Choices_Low, Choices_High);
2195 Check_Bounds (Index_Base_Low, Index_Base_High,
2196 Choices_Low, Choices_High);
2198 -- Check (C)
2200 elsif Others_Present and then Nb_Elements > 0 then
2201 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
2202 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
2203 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
2204 end if;
2206 if Raises_Constraint_Error (Aggr_Low)
2207 or else Raises_Constraint_Error (Aggr_High)
2208 then
2209 Set_Raises_Constraint_Error (N);
2210 end if;
2212 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
2214 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
2215 -- since the addition node returned by Add is not yet analyzed. Attach
2216 -- to tree and analyze first. Reset analyzed flag to ensure it will get
2217 -- analyzed when it is a literal bound whose type must be properly set.
2219 if Others_Present or else Nb_Discrete_Choices > 0 then
2220 Aggr_High := Duplicate_Subexpr (Aggr_High);
2222 if Etype (Aggr_High) = Universal_Integer then
2223 Set_Analyzed (Aggr_High, False);
2224 end if;
2225 end if;
2227 -- If the aggregate already has bounds attached to it, it means this is
2228 -- a positional aggregate created as an optimization by
2229 -- Exp_Aggr.Convert_To_Positional, so we don't want to change those
2230 -- bounds.
2232 if Present (Aggregate_Bounds (N)) and then not Others_Allowed then
2233 Aggr_Low := Low_Bound (Aggregate_Bounds (N));
2234 Aggr_High := High_Bound (Aggregate_Bounds (N));
2235 end if;
2237 Set_Aggregate_Bounds
2238 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
2240 -- The bounds may contain expressions that must be inserted upwards.
2241 -- Attach them fully to the tree. After analysis, remove side effects
2242 -- from upper bound, if still needed.
2244 Set_Parent (Aggregate_Bounds (N), N);
2245 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
2246 Check_Unset_Reference (Aggregate_Bounds (N));
2248 if not Others_Present and then Nb_Discrete_Choices = 0 then
2249 Set_High_Bound (Aggregate_Bounds (N),
2250 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
2251 end if;
2253 return Success;
2254 end Resolve_Array_Aggregate;
2256 ---------------------------------
2257 -- Resolve_Extension_Aggregate --
2258 ---------------------------------
2260 -- There are two cases to consider:
2262 -- a) If the ancestor part is a type mark, the components needed are the
2263 -- difference between the components of the expected type and the
2264 -- components of the given type mark.
2266 -- b) If the ancestor part is an expression, it must be unambiguous, and
2267 -- once we have its type we can also compute the needed components as in
2268 -- the previous case. In both cases, if the ancestor type is not the
2269 -- immediate ancestor, we have to build this ancestor recursively.
2271 -- In both cases discriminants of the ancestor type do not play a role in
2272 -- the resolution of the needed components, because inherited discriminants
2273 -- cannot be used in a type extension. As a result we can compute
2274 -- independently the list of components of the ancestor type and of the
2275 -- expected type.
2277 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
2278 A : constant Node_Id := Ancestor_Part (N);
2279 A_Type : Entity_Id;
2280 I : Interp_Index;
2281 It : Interp;
2283 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean;
2284 -- If the type is limited, verify that the ancestor part is a legal
2285 -- expression (aggregate or function call, including 'Input)) that does
2286 -- not require a copy, as specified in 7.5(2).
2288 function Valid_Ancestor_Type return Boolean;
2289 -- Verify that the type of the ancestor part is a non-private ancestor
2290 -- of the expected type, which must be a type extension.
2292 ----------------------------
2293 -- Valid_Limited_Ancestor --
2294 ----------------------------
2296 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean is
2297 begin
2298 if Is_Entity_Name (Anc)
2299 and then Is_Type (Entity (Anc))
2300 then
2301 return True;
2303 elsif Nkind_In (Anc, N_Aggregate, N_Function_Call) then
2304 return True;
2306 elsif Nkind (Anc) = N_Attribute_Reference
2307 and then Attribute_Name (Anc) = Name_Input
2308 then
2309 return True;
2311 elsif Nkind (Anc) = N_Qualified_Expression then
2312 return Valid_Limited_Ancestor (Expression (Anc));
2314 else
2315 return False;
2316 end if;
2317 end Valid_Limited_Ancestor;
2319 -------------------------
2320 -- Valid_Ancestor_Type --
2321 -------------------------
2323 function Valid_Ancestor_Type return Boolean is
2324 Imm_Type : Entity_Id;
2326 begin
2327 Imm_Type := Base_Type (Typ);
2328 while Is_Derived_Type (Imm_Type) loop
2329 if Etype (Imm_Type) = Base_Type (A_Type) then
2330 return True;
2332 -- The base type of the parent type may appear as a private
2333 -- extension if it is declared as such in a parent unit of the
2334 -- current one. For consistency of the subsequent analysis use
2335 -- the partial view for the ancestor part.
2337 elsif Is_Private_Type (Etype (Imm_Type))
2338 and then Present (Full_View (Etype (Imm_Type)))
2339 and then Base_Type (A_Type) = Full_View (Etype (Imm_Type))
2340 then
2341 A_Type := Etype (Imm_Type);
2342 return True;
2344 -- The parent type may be a private extension. The aggregate is
2345 -- legal if the type of the aggregate is an extension of it that
2346 -- is not a private extension.
2348 elsif Is_Private_Type (A_Type)
2349 and then not Is_Private_Type (Imm_Type)
2350 and then Present (Full_View (A_Type))
2351 and then Base_Type (Full_View (A_Type)) = Etype (Imm_Type)
2352 then
2353 return True;
2355 else
2356 Imm_Type := Etype (Base_Type (Imm_Type));
2357 end if;
2358 end loop;
2360 -- If previous loop did not find a proper ancestor, report error
2362 Error_Msg_NE ("expect ancestor type of &", A, Typ);
2363 return False;
2364 end Valid_Ancestor_Type;
2366 -- Start of processing for Resolve_Extension_Aggregate
2368 begin
2369 -- Analyze the ancestor part and account for the case where it is a
2370 -- parameterless function call.
2372 Analyze (A);
2373 Check_Parameterless_Call (A);
2375 if not Is_Tagged_Type (Typ) then
2376 Error_Msg_N ("type of extension aggregate must be tagged", N);
2377 return;
2379 elsif Is_Limited_Type (Typ) then
2381 -- Ada 2005 (AI-287): Limited aggregates are allowed
2383 if Ada_Version < Ada_2005 then
2384 Error_Msg_N ("aggregate type cannot be limited", N);
2385 Explain_Limited_Type (Typ, N);
2386 return;
2388 elsif Valid_Limited_Ancestor (A) then
2389 null;
2391 else
2392 Error_Msg_N
2393 ("limited ancestor part must be aggregate or function call", A);
2394 end if;
2396 elsif Is_Class_Wide_Type (Typ) then
2397 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
2398 return;
2399 end if;
2401 if Is_Entity_Name (A)
2402 and then Is_Type (Entity (A))
2403 then
2404 A_Type := Get_Full_View (Entity (A));
2406 if Valid_Ancestor_Type then
2407 Set_Entity (A, A_Type);
2408 Set_Etype (A, A_Type);
2410 Validate_Ancestor_Part (N);
2411 Resolve_Record_Aggregate (N, Typ);
2412 end if;
2414 elsif Nkind (A) /= N_Aggregate then
2415 if Is_Overloaded (A) then
2416 A_Type := Any_Type;
2418 Get_First_Interp (A, I, It);
2419 while Present (It.Typ) loop
2420 -- Only consider limited interpretations in the Ada 2005 case
2422 if Is_Tagged_Type (It.Typ)
2423 and then (Ada_Version >= Ada_2005
2424 or else not Is_Limited_Type (It.Typ))
2425 then
2426 if A_Type /= Any_Type then
2427 Error_Msg_N ("cannot resolve expression", A);
2428 return;
2429 else
2430 A_Type := It.Typ;
2431 end if;
2432 end if;
2434 Get_Next_Interp (I, It);
2435 end loop;
2437 if A_Type = Any_Type then
2438 if Ada_Version >= Ada_2005 then
2439 Error_Msg_N ("ancestor part must be of a tagged type", A);
2440 else
2441 Error_Msg_N
2442 ("ancestor part must be of a nonlimited tagged type", A);
2443 end if;
2445 return;
2446 end if;
2448 else
2449 A_Type := Etype (A);
2450 end if;
2452 if Valid_Ancestor_Type then
2453 Resolve (A, A_Type);
2454 Check_Unset_Reference (A);
2455 Check_Non_Static_Context (A);
2457 -- The aggregate is illegal if the ancestor expression is a call
2458 -- to a function with a limited unconstrained result, unless the
2459 -- type of the aggregate is a null extension. This restriction
2460 -- was added in AI05-67 to simplify implementation.
2462 if Nkind (A) = N_Function_Call
2463 and then Is_Limited_Type (A_Type)
2464 and then not Is_Null_Extension (Typ)
2465 and then not Is_Constrained (A_Type)
2466 then
2467 Error_Msg_N
2468 ("type of limited ancestor part must be constrained", A);
2470 -- Reject the use of CPP constructors that leave objects partially
2471 -- initialized. For example:
2473 -- type CPP_Root is tagged limited record ...
2474 -- pragma Import (CPP, CPP_Root);
2476 -- type CPP_DT is new CPP_Root and Iface ...
2477 -- pragma Import (CPP, CPP_DT);
2479 -- type Ada_DT is new CPP_DT with ...
2481 -- Obj : Ada_DT := Ada_DT'(New_CPP_Root with others => <>);
2483 -- Using the constructor of CPP_Root the slots of the dispatch
2484 -- table of CPP_DT cannot be set, and the secondary tag of
2485 -- CPP_DT is unknown.
2487 elsif Nkind (A) = N_Function_Call
2488 and then Is_CPP_Constructor_Call (A)
2489 and then Enclosing_CPP_Parent (Typ) /= A_Type
2490 then
2491 Error_Msg_NE
2492 ("?must use 'C'P'P constructor for type &", A,
2493 Enclosing_CPP_Parent (Typ));
2495 -- The following call is not needed if the previous warning
2496 -- is promoted to an error.
2498 Resolve_Record_Aggregate (N, Typ);
2500 elsif Is_Class_Wide_Type (Etype (A))
2501 and then Nkind (Original_Node (A)) = N_Function_Call
2502 then
2503 -- If the ancestor part is a dispatching call, it appears
2504 -- statically to be a legal ancestor, but it yields any member
2505 -- of the class, and it is not possible to determine whether
2506 -- it is an ancestor of the extension aggregate (much less
2507 -- which ancestor). It is not possible to determine the
2508 -- components of the extension part.
2510 -- This check implements AI-306, which in fact was motivated by
2511 -- an AdaCore query to the ARG after this test was added.
2513 Error_Msg_N ("ancestor part must be statically tagged", A);
2514 else
2515 Resolve_Record_Aggregate (N, Typ);
2516 end if;
2517 end if;
2519 else
2520 Error_Msg_N ("no unique type for this aggregate", A);
2521 end if;
2522 end Resolve_Extension_Aggregate;
2524 ------------------------------
2525 -- Resolve_Record_Aggregate --
2526 ------------------------------
2528 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
2529 Assoc : Node_Id;
2530 -- N_Component_Association node belonging to the input aggregate N
2532 Expr : Node_Id;
2533 Positional_Expr : Node_Id;
2534 Component : Entity_Id;
2535 Component_Elmt : Elmt_Id;
2537 Components : constant Elist_Id := New_Elmt_List;
2538 -- Components is the list of the record components whose value must be
2539 -- provided in the aggregate. This list does include discriminants.
2541 New_Assoc_List : constant List_Id := New_List;
2542 New_Assoc : Node_Id;
2543 -- New_Assoc_List is the newly built list of N_Component_Association
2544 -- nodes. New_Assoc is one such N_Component_Association node in it.
2545 -- Note that while Assoc and New_Assoc contain the same kind of nodes,
2546 -- they are used to iterate over two different N_Component_Association
2547 -- lists.
2549 Others_Etype : Entity_Id := Empty;
2550 -- This variable is used to save the Etype of the last record component
2551 -- that takes its value from the others choice. Its purpose is:
2553 -- (a) make sure the others choice is useful
2555 -- (b) make sure the type of all the components whose value is
2556 -- subsumed by the others choice are the same.
2558 -- This variable is updated as a side effect of function Get_Value.
2560 Is_Box_Present : Boolean := False;
2561 Others_Box : Boolean := False;
2562 -- Ada 2005 (AI-287): Variables used in case of default initialization
2563 -- to provide a functionality similar to Others_Etype. Box_Present
2564 -- indicates that the component takes its default initialization;
2565 -- Others_Box indicates that at least one component takes its default
2566 -- initialization. Similar to Others_Etype, they are also updated as a
2567 -- side effect of function Get_Value.
2569 procedure Add_Association
2570 (Component : Entity_Id;
2571 Expr : Node_Id;
2572 Assoc_List : List_Id;
2573 Is_Box_Present : Boolean := False);
2574 -- Builds a new N_Component_Association node which associates Component
2575 -- to expression Expr and adds it to the association list being built,
2576 -- either New_Assoc_List, or the association being built for an inner
2577 -- aggregate.
2579 function Discr_Present (Discr : Entity_Id) return Boolean;
2580 -- If aggregate N is a regular aggregate this routine will return True.
2581 -- Otherwise, if N is an extension aggregate, Discr is a discriminant
2582 -- whose value may already have been specified by N's ancestor part.
2583 -- This routine checks whether this is indeed the case and if so returns
2584 -- False, signaling that no value for Discr should appear in N's
2585 -- aggregate part. Also, in this case, the routine appends to
2586 -- New_Assoc_List the discriminant value specified in the ancestor part.
2588 -- If the aggregate is in a context with expansion delayed, it will be
2589 -- reanalyzed. The inherited discriminant values must not be reinserted
2590 -- in the component list to prevent spurious errors, but they must be
2591 -- present on first analysis to build the proper subtype indications.
2592 -- The flag Inherited_Discriminant is used to prevent the re-insertion.
2594 function Get_Value
2595 (Compon : Node_Id;
2596 From : List_Id;
2597 Consider_Others_Choice : Boolean := False)
2598 return Node_Id;
2599 -- Given a record component stored in parameter Compon, this function
2600 -- returns its value as it appears in the list From, which is a list
2601 -- of N_Component_Association nodes.
2603 -- If no component association has a choice for the searched component,
2604 -- the value provided by the others choice is returned, if there is one,
2605 -- and Consider_Others_Choice is set to true. Otherwise Empty is
2606 -- returned. If there is more than one component association giving a
2607 -- value for the searched record component, an error message is emitted
2608 -- and the first found value is returned.
2610 -- If Consider_Others_Choice is set and the returned expression comes
2611 -- from the others choice, then Others_Etype is set as a side effect.
2612 -- An error message is emitted if the components taking their value from
2613 -- the others choice do not have same type.
2615 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id);
2616 -- Analyzes and resolves expression Expr against the Etype of the
2617 -- Component. This routine also applies all appropriate checks to Expr.
2618 -- It finally saves a Expr in the newly created association list that
2619 -- will be attached to the final record aggregate. Note that if the
2620 -- Parent pointer of Expr is not set then Expr was produced with a
2621 -- New_Copy_Tree or some such.
2623 ---------------------
2624 -- Add_Association --
2625 ---------------------
2627 procedure Add_Association
2628 (Component : Entity_Id;
2629 Expr : Node_Id;
2630 Assoc_List : List_Id;
2631 Is_Box_Present : Boolean := False)
2633 Choice_List : constant List_Id := New_List;
2634 New_Assoc : Node_Id;
2636 begin
2637 Append (New_Occurrence_Of (Component, Sloc (Expr)), Choice_List);
2638 New_Assoc :=
2639 Make_Component_Association (Sloc (Expr),
2640 Choices => Choice_List,
2641 Expression => Expr,
2642 Box_Present => Is_Box_Present);
2643 Append (New_Assoc, Assoc_List);
2644 end Add_Association;
2646 -------------------
2647 -- Discr_Present --
2648 -------------------
2650 function Discr_Present (Discr : Entity_Id) return Boolean is
2651 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
2653 Loc : Source_Ptr;
2655 Ancestor : Node_Id;
2656 Comp_Assoc : Node_Id;
2657 Discr_Expr : Node_Id;
2659 Ancestor_Typ : Entity_Id;
2660 Orig_Discr : Entity_Id;
2661 D : Entity_Id;
2662 D_Val : Elmt_Id := No_Elmt; -- stop junk warning
2664 Ancestor_Is_Subtyp : Boolean;
2666 begin
2667 if Regular_Aggr then
2668 return True;
2669 end if;
2671 -- Check whether inherited discriminant values have already been
2672 -- inserted in the aggregate. This will be the case if we are
2673 -- re-analyzing an aggregate whose expansion was delayed.
2675 if Present (Component_Associations (N)) then
2676 Comp_Assoc := First (Component_Associations (N));
2677 while Present (Comp_Assoc) loop
2678 if Inherited_Discriminant (Comp_Assoc) then
2679 return True;
2680 end if;
2682 Next (Comp_Assoc);
2683 end loop;
2684 end if;
2686 Ancestor := Ancestor_Part (N);
2687 Ancestor_Typ := Etype (Ancestor);
2688 Loc := Sloc (Ancestor);
2690 -- For a private type with unknown discriminants, use the underlying
2691 -- record view if it is available.
2693 if Has_Unknown_Discriminants (Ancestor_Typ)
2694 and then Present (Full_View (Ancestor_Typ))
2695 and then Present (Underlying_Record_View (Full_View (Ancestor_Typ)))
2696 then
2697 Ancestor_Typ := Underlying_Record_View (Full_View (Ancestor_Typ));
2698 end if;
2700 Ancestor_Is_Subtyp :=
2701 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
2703 -- If the ancestor part has no discriminants clearly N's aggregate
2704 -- part must provide a value for Discr.
2706 if not Has_Discriminants (Ancestor_Typ) then
2707 return True;
2709 -- If the ancestor part is an unconstrained subtype mark then the
2710 -- Discr must be present in N's aggregate part.
2712 elsif Ancestor_Is_Subtyp
2713 and then not Is_Constrained (Entity (Ancestor))
2714 then
2715 return True;
2716 end if;
2718 -- Now look to see if Discr was specified in the ancestor part
2720 if Ancestor_Is_Subtyp then
2721 D_Val := First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
2722 end if;
2724 Orig_Discr := Original_Record_Component (Discr);
2726 D := First_Discriminant (Ancestor_Typ);
2727 while Present (D) loop
2729 -- If Ancestor has already specified Disc value then insert its
2730 -- value in the final aggregate.
2732 if Original_Record_Component (D) = Orig_Discr then
2733 if Ancestor_Is_Subtyp then
2734 Discr_Expr := New_Copy_Tree (Node (D_Val));
2735 else
2736 Discr_Expr :=
2737 Make_Selected_Component (Loc,
2738 Prefix => Duplicate_Subexpr (Ancestor),
2739 Selector_Name => New_Occurrence_Of (Discr, Loc));
2740 end if;
2742 Resolve_Aggr_Expr (Discr_Expr, Discr);
2743 Set_Inherited_Discriminant (Last (New_Assoc_List));
2744 return False;
2745 end if;
2747 Next_Discriminant (D);
2749 if Ancestor_Is_Subtyp then
2750 Next_Elmt (D_Val);
2751 end if;
2752 end loop;
2754 return True;
2755 end Discr_Present;
2757 ---------------
2758 -- Get_Value --
2759 ---------------
2761 function Get_Value
2762 (Compon : Node_Id;
2763 From : List_Id;
2764 Consider_Others_Choice : Boolean := False)
2765 return Node_Id
2767 Assoc : Node_Id;
2768 Expr : Node_Id := Empty;
2769 Selector_Name : Node_Id;
2771 begin
2772 Is_Box_Present := False;
2774 if Present (From) then
2775 Assoc := First (From);
2776 else
2777 return Empty;
2778 end if;
2780 while Present (Assoc) loop
2781 Selector_Name := First (Choices (Assoc));
2782 while Present (Selector_Name) loop
2783 if Nkind (Selector_Name) = N_Others_Choice then
2784 if Consider_Others_Choice and then No (Expr) then
2786 -- We need to duplicate the expression for each
2787 -- successive component covered by the others choice.
2788 -- This is redundant if the others_choice covers only
2789 -- one component (small optimization possible???), but
2790 -- indispensable otherwise, because each one must be
2791 -- expanded individually to preserve side-effects.
2793 -- Ada 2005 (AI-287): In case of default initialization
2794 -- of components, we duplicate the corresponding default
2795 -- expression (from the record type declaration). The
2796 -- copy must carry the sloc of the association (not the
2797 -- original expression) to prevent spurious elaboration
2798 -- checks when the default includes function calls.
2800 if Box_Present (Assoc) then
2801 Others_Box := True;
2802 Is_Box_Present := True;
2804 if Expander_Active then
2805 return
2806 New_Copy_Tree
2807 (Expression (Parent (Compon)),
2808 New_Sloc => Sloc (Assoc));
2809 else
2810 return Expression (Parent (Compon));
2811 end if;
2813 else
2814 if Present (Others_Etype) and then
2815 Base_Type (Others_Etype) /= Base_Type (Etype
2816 (Compon))
2817 then
2818 Error_Msg_N ("components in OTHERS choice must " &
2819 "have same type", Selector_Name);
2820 end if;
2822 Others_Etype := Etype (Compon);
2824 if Expander_Active then
2825 return New_Copy_Tree (Expression (Assoc));
2826 else
2827 return Expression (Assoc);
2828 end if;
2829 end if;
2830 end if;
2832 elsif Chars (Compon) = Chars (Selector_Name) then
2833 if No (Expr) then
2835 -- Ada 2005 (AI-231)
2837 if Ada_Version >= Ada_2005
2838 and then Known_Null (Expression (Assoc))
2839 then
2840 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
2841 end if;
2843 -- We need to duplicate the expression when several
2844 -- components are grouped together with a "|" choice.
2845 -- For instance "filed1 | filed2 => Expr"
2847 -- Ada 2005 (AI-287)
2849 if Box_Present (Assoc) then
2850 Is_Box_Present := True;
2852 -- Duplicate the default expression of the component
2853 -- from the record type declaration, so a new copy
2854 -- can be attached to the association.
2856 -- Note that we always copy the default expression,
2857 -- even when the association has a single choice, in
2858 -- order to create a proper association for the
2859 -- expanded aggregate.
2861 Expr := New_Copy_Tree (Expression (Parent (Compon)));
2863 else
2864 if Present (Next (Selector_Name)) then
2865 Expr := New_Copy_Tree (Expression (Assoc));
2866 else
2867 Expr := Expression (Assoc);
2868 end if;
2869 end if;
2871 Generate_Reference (Compon, Selector_Name, 'm');
2873 else
2874 Error_Msg_NE
2875 ("more than one value supplied for &",
2876 Selector_Name, Compon);
2878 end if;
2879 end if;
2881 Next (Selector_Name);
2882 end loop;
2884 Next (Assoc);
2885 end loop;
2887 return Expr;
2888 end Get_Value;
2890 -----------------------
2891 -- Resolve_Aggr_Expr --
2892 -----------------------
2894 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id) is
2895 New_C : Entity_Id := Component;
2896 Expr_Type : Entity_Id := Empty;
2898 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
2899 -- If the expression is an aggregate (possibly qualified) then its
2900 -- expansion is delayed until the enclosing aggregate is expanded
2901 -- into assignments. In that case, do not generate checks on the
2902 -- expression, because they will be generated later, and will other-
2903 -- wise force a copy (to remove side-effects) that would leave a
2904 -- dynamic-sized aggregate in the code, something that gigi cannot
2905 -- handle.
2907 Relocate : Boolean;
2908 -- Set to True if the resolved Expr node needs to be relocated
2909 -- when attached to the newly created association list. This node
2910 -- need not be relocated if its parent pointer is not set.
2911 -- In fact in this case Expr is the output of a New_Copy_Tree call.
2912 -- if Relocate is True then we have analyzed the expression node
2913 -- in the original aggregate and hence it needs to be relocated
2914 -- when moved over the new association list.
2916 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
2917 Kind : constant Node_Kind := Nkind (Expr);
2918 begin
2919 return (Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate)
2920 and then Present (Etype (Expr))
2921 and then Is_Record_Type (Etype (Expr))
2922 and then Expansion_Delayed (Expr))
2923 or else (Kind = N_Qualified_Expression
2924 and then Has_Expansion_Delayed (Expression (Expr)));
2925 end Has_Expansion_Delayed;
2927 -- Start of processing for Resolve_Aggr_Expr
2929 begin
2930 -- If the type of the component is elementary or the type of the
2931 -- aggregate does not contain discriminants, use the type of the
2932 -- component to resolve Expr.
2934 if Is_Elementary_Type (Etype (Component))
2935 or else not Has_Discriminants (Etype (N))
2936 then
2937 Expr_Type := Etype (Component);
2939 -- Otherwise we have to pick up the new type of the component from
2940 -- the new constrained subtype of the aggregate. In fact components
2941 -- which are of a composite type might be constrained by a
2942 -- discriminant, and we want to resolve Expr against the subtype were
2943 -- all discriminant occurrences are replaced with their actual value.
2945 else
2946 New_C := First_Component (Etype (N));
2947 while Present (New_C) loop
2948 if Chars (New_C) = Chars (Component) then
2949 Expr_Type := Etype (New_C);
2950 exit;
2951 end if;
2953 Next_Component (New_C);
2954 end loop;
2956 pragma Assert (Present (Expr_Type));
2958 -- For each range in an array type where a discriminant has been
2959 -- replaced with the constraint, check that this range is within
2960 -- the range of the base type. This checks is done in the init
2961 -- proc for regular objects, but has to be done here for
2962 -- aggregates since no init proc is called for them.
2964 if Is_Array_Type (Expr_Type) then
2965 declare
2966 Index : Node_Id;
2967 -- Range of the current constrained index in the array
2969 Orig_Index : Node_Id := First_Index (Etype (Component));
2970 -- Range corresponding to the range Index above in the
2971 -- original unconstrained record type. The bounds of this
2972 -- range may be governed by discriminants.
2974 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
2975 -- Range corresponding to the range Index above for the
2976 -- unconstrained array type. This range is needed to apply
2977 -- range checks.
2979 begin
2980 Index := First_Index (Expr_Type);
2981 while Present (Index) loop
2982 if Depends_On_Discriminant (Orig_Index) then
2983 Apply_Range_Check (Index, Etype (Unconstr_Index));
2984 end if;
2986 Next_Index (Index);
2987 Next_Index (Orig_Index);
2988 Next_Index (Unconstr_Index);
2989 end loop;
2990 end;
2991 end if;
2992 end if;
2994 -- If the Parent pointer of Expr is not set, Expr is an expression
2995 -- duplicated by New_Tree_Copy (this happens for record aggregates
2996 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
2997 -- Such a duplicated expression must be attached to the tree
2998 -- before analysis and resolution to enforce the rule that a tree
2999 -- fragment should never be analyzed or resolved unless it is
3000 -- attached to the current compilation unit.
3002 if No (Parent (Expr)) then
3003 Set_Parent (Expr, N);
3004 Relocate := False;
3005 else
3006 Relocate := True;
3007 end if;
3009 Analyze_And_Resolve (Expr, Expr_Type);
3010 Check_Expr_OK_In_Limited_Aggregate (Expr);
3011 Check_Non_Static_Context (Expr);
3012 Check_Unset_Reference (Expr);
3014 -- Check wrong use of class-wide types
3016 if Is_Class_Wide_Type (Etype (Expr)) then
3017 Error_Msg_N ("dynamically tagged expression not allowed", Expr);
3018 end if;
3020 if not Has_Expansion_Delayed (Expr) then
3021 Aggregate_Constraint_Checks (Expr, Expr_Type);
3022 end if;
3024 if Raises_Constraint_Error (Expr) then
3025 Set_Raises_Constraint_Error (N);
3026 end if;
3028 -- If the expression has been marked as requiring a range check,
3029 -- then generate it here.
3031 if Do_Range_Check (Expr) then
3032 Set_Do_Range_Check (Expr, False);
3033 Generate_Range_Check (Expr, Expr_Type, CE_Range_Check_Failed);
3034 end if;
3036 if Relocate then
3037 Add_Association (New_C, Relocate_Node (Expr), New_Assoc_List);
3038 else
3039 Add_Association (New_C, Expr, New_Assoc_List);
3040 end if;
3041 end Resolve_Aggr_Expr;
3043 -- Start of processing for Resolve_Record_Aggregate
3045 begin
3046 -- We may end up calling Duplicate_Subexpr on expressions that are
3047 -- attached to New_Assoc_List. For this reason we need to attach it
3048 -- to the tree by setting its parent pointer to N. This parent point
3049 -- will change in STEP 8 below.
3051 Set_Parent (New_Assoc_List, N);
3053 -- STEP 1: abstract type and null record verification
3055 if Is_Abstract_Type (Typ) then
3056 Error_Msg_N ("type of aggregate cannot be abstract", N);
3057 end if;
3059 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
3060 Set_Etype (N, Typ);
3061 return;
3063 elsif Present (First_Entity (Typ))
3064 and then Null_Record_Present (N)
3065 and then not Is_Tagged_Type (Typ)
3066 then
3067 Error_Msg_N ("record aggregate cannot be null", N);
3068 return;
3070 -- If the type has no components, then the aggregate should either
3071 -- have "null record", or in Ada 2005 it could instead have a single
3072 -- component association given by "others => <>". For Ada 95 we flag
3073 -- an error at this point, but for Ada 2005 we proceed with checking
3074 -- the associations below, which will catch the case where it's not
3075 -- an aggregate with "others => <>". Note that the legality of a <>
3076 -- aggregate for a null record type was established by AI05-016.
3078 elsif No (First_Entity (Typ))
3079 and then Ada_Version < Ada_2005
3080 then
3081 Error_Msg_N ("record aggregate must be null", N);
3082 return;
3083 end if;
3085 -- STEP 2: Verify aggregate structure
3087 Step_2 : declare
3088 Selector_Name : Node_Id;
3089 Bad_Aggregate : Boolean := False;
3091 begin
3092 if Present (Component_Associations (N)) then
3093 Assoc := First (Component_Associations (N));
3094 else
3095 Assoc := Empty;
3096 end if;
3098 while Present (Assoc) loop
3099 Selector_Name := First (Choices (Assoc));
3100 while Present (Selector_Name) loop
3101 if Nkind (Selector_Name) = N_Identifier then
3102 null;
3104 elsif Nkind (Selector_Name) = N_Others_Choice then
3105 if Selector_Name /= First (Choices (Assoc))
3106 or else Present (Next (Selector_Name))
3107 then
3108 Error_Msg_N
3109 ("OTHERS must appear alone in a choice list",
3110 Selector_Name);
3111 return;
3113 elsif Present (Next (Assoc)) then
3114 Error_Msg_N
3115 ("OTHERS must appear last in an aggregate",
3116 Selector_Name);
3117 return;
3119 -- (Ada2005): If this is an association with a box,
3120 -- indicate that the association need not represent
3121 -- any component.
3123 elsif Box_Present (Assoc) then
3124 Others_Box := True;
3125 end if;
3127 else
3128 Error_Msg_N
3129 ("selector name should be identifier or OTHERS",
3130 Selector_Name);
3131 Bad_Aggregate := True;
3132 end if;
3134 Next (Selector_Name);
3135 end loop;
3137 Next (Assoc);
3138 end loop;
3140 if Bad_Aggregate then
3141 return;
3142 end if;
3143 end Step_2;
3145 -- STEP 3: Find discriminant Values
3147 Step_3 : declare
3148 Discrim : Entity_Id;
3149 Missing_Discriminants : Boolean := False;
3151 begin
3152 if Present (Expressions (N)) then
3153 Positional_Expr := First (Expressions (N));
3154 else
3155 Positional_Expr := Empty;
3156 end if;
3158 if Has_Unknown_Discriminants (Typ)
3159 and then Present (Underlying_Record_View (Typ))
3160 then
3161 Discrim := First_Discriminant (Underlying_Record_View (Typ));
3162 elsif Has_Discriminants (Typ) then
3163 Discrim := First_Discriminant (Typ);
3164 else
3165 Discrim := Empty;
3166 end if;
3168 -- First find the discriminant values in the positional components
3170 while Present (Discrim) and then Present (Positional_Expr) loop
3171 if Discr_Present (Discrim) then
3172 Resolve_Aggr_Expr (Positional_Expr, Discrim);
3174 -- Ada 2005 (AI-231)
3176 if Ada_Version >= Ada_2005
3177 and then Known_Null (Positional_Expr)
3178 then
3179 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
3180 end if;
3182 Next (Positional_Expr);
3183 end if;
3185 if Present (Get_Value (Discrim, Component_Associations (N))) then
3186 Error_Msg_NE
3187 ("more than one value supplied for discriminant&",
3188 N, Discrim);
3189 end if;
3191 Next_Discriminant (Discrim);
3192 end loop;
3194 -- Find remaining discriminant values, if any, among named components
3196 while Present (Discrim) loop
3197 Expr := Get_Value (Discrim, Component_Associations (N), True);
3199 if not Discr_Present (Discrim) then
3200 if Present (Expr) then
3201 Error_Msg_NE
3202 ("more than one value supplied for discriminant&",
3203 N, Discrim);
3204 end if;
3206 elsif No (Expr) then
3207 Error_Msg_NE
3208 ("no value supplied for discriminant &", N, Discrim);
3209 Missing_Discriminants := True;
3211 else
3212 Resolve_Aggr_Expr (Expr, Discrim);
3213 end if;
3215 Next_Discriminant (Discrim);
3216 end loop;
3218 if Missing_Discriminants then
3219 return;
3220 end if;
3222 -- At this point and until the beginning of STEP 6, New_Assoc_List
3223 -- contains only the discriminants and their values.
3225 end Step_3;
3227 -- STEP 4: Set the Etype of the record aggregate
3229 -- ??? This code is pretty much a copy of Sem_Ch3.Build_Subtype. That
3230 -- routine should really be exported in sem_util or some such and used
3231 -- in sem_ch3 and here rather than have a copy of the code which is a
3232 -- maintenance nightmare.
3234 -- ??? Performance WARNING. The current implementation creates a new
3235 -- itype for all aggregates whose base type is discriminated.
3236 -- This means that for record aggregates nested inside an array
3237 -- aggregate we will create a new itype for each record aggregate
3238 -- if the array component type has discriminants. For large aggregates
3239 -- this may be a problem. What should be done in this case is
3240 -- to reuse itypes as much as possible.
3242 if Has_Discriminants (Typ)
3243 or else (Has_Unknown_Discriminants (Typ)
3244 and then Present (Underlying_Record_View (Typ)))
3245 then
3246 Build_Constrained_Itype : declare
3247 Loc : constant Source_Ptr := Sloc (N);
3248 Indic : Node_Id;
3249 Subtyp_Decl : Node_Id;
3250 Def_Id : Entity_Id;
3252 C : constant List_Id := New_List;
3254 begin
3255 New_Assoc := First (New_Assoc_List);
3256 while Present (New_Assoc) loop
3257 Append (Duplicate_Subexpr (Expression (New_Assoc)), To => C);
3258 Next (New_Assoc);
3259 end loop;
3261 if Has_Unknown_Discriminants (Typ)
3262 and then Present (Underlying_Record_View (Typ))
3263 then
3264 Indic :=
3265 Make_Subtype_Indication (Loc,
3266 Subtype_Mark =>
3267 New_Occurrence_Of (Underlying_Record_View (Typ), Loc),
3268 Constraint =>
3269 Make_Index_Or_Discriminant_Constraint (Loc, C));
3270 else
3271 Indic :=
3272 Make_Subtype_Indication (Loc,
3273 Subtype_Mark =>
3274 New_Occurrence_Of (Base_Type (Typ), Loc),
3275 Constraint =>
3276 Make_Index_Or_Discriminant_Constraint (Loc, C));
3277 end if;
3279 Def_Id := Create_Itype (Ekind (Typ), N);
3281 Subtyp_Decl :=
3282 Make_Subtype_Declaration (Loc,
3283 Defining_Identifier => Def_Id,
3284 Subtype_Indication => Indic);
3285 Set_Parent (Subtyp_Decl, Parent (N));
3287 -- Itypes must be analyzed with checks off (see itypes.ads)
3289 Analyze (Subtyp_Decl, Suppress => All_Checks);
3291 Set_Etype (N, Def_Id);
3292 Check_Static_Discriminated_Subtype
3293 (Def_Id, Expression (First (New_Assoc_List)));
3294 end Build_Constrained_Itype;
3296 else
3297 Set_Etype (N, Typ);
3298 end if;
3300 -- STEP 5: Get remaining components according to discriminant values
3302 Step_5 : declare
3303 Record_Def : Node_Id;
3304 Parent_Typ : Entity_Id;
3305 Root_Typ : Entity_Id;
3306 Parent_Typ_List : Elist_Id;
3307 Parent_Elmt : Elmt_Id;
3308 Errors_Found : Boolean := False;
3309 Dnode : Node_Id;
3311 begin
3312 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
3313 Parent_Typ_List := New_Elmt_List;
3315 -- If this is an extension aggregate, the component list must
3316 -- include all components that are not in the given ancestor type.
3317 -- Otherwise, the component list must include components of all
3318 -- ancestors, starting with the root.
3320 if Nkind (N) = N_Extension_Aggregate then
3321 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
3323 else
3324 Root_Typ := Root_Type (Typ);
3326 if Nkind (Parent (Base_Type (Root_Typ))) =
3327 N_Private_Type_Declaration
3328 then
3329 Error_Msg_NE
3330 ("type of aggregate has private ancestor&!",
3331 N, Root_Typ);
3332 Error_Msg_N ("must use extension aggregate!", N);
3333 return;
3334 end if;
3336 Dnode := Declaration_Node (Base_Type (Root_Typ));
3338 -- If we don't get a full declaration, then we have some error
3339 -- which will get signalled later so skip this part. Otherwise
3340 -- gather components of root that apply to the aggregate type.
3341 -- We use the base type in case there is an applicable stored
3342 -- constraint that renames the discriminants of the root.
3344 if Nkind (Dnode) = N_Full_Type_Declaration then
3345 Record_Def := Type_Definition (Dnode);
3346 Gather_Components (Base_Type (Typ),
3347 Component_List (Record_Def),
3348 Governed_By => New_Assoc_List,
3349 Into => Components,
3350 Report_Errors => Errors_Found);
3351 end if;
3352 end if;
3354 Parent_Typ := Base_Type (Typ);
3355 while Parent_Typ /= Root_Typ loop
3356 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
3357 Parent_Typ := Etype (Parent_Typ);
3359 if Nkind (Parent (Base_Type (Parent_Typ))) =
3360 N_Private_Type_Declaration
3361 or else Nkind (Parent (Base_Type (Parent_Typ))) =
3362 N_Private_Extension_Declaration
3363 then
3364 if Nkind (N) /= N_Extension_Aggregate then
3365 Error_Msg_NE
3366 ("type of aggregate has private ancestor&!",
3367 N, Parent_Typ);
3368 Error_Msg_N ("must use extension aggregate!", N);
3369 return;
3371 elsif Parent_Typ /= Root_Typ then
3372 Error_Msg_NE
3373 ("ancestor part of aggregate must be private type&",
3374 Ancestor_Part (N), Parent_Typ);
3375 return;
3376 end if;
3378 -- The current view of ancestor part may be a private type,
3379 -- while the context type is always non-private.
3381 elsif Is_Private_Type (Root_Typ)
3382 and then Present (Full_View (Root_Typ))
3383 and then Nkind (N) = N_Extension_Aggregate
3384 then
3385 exit when Base_Type (Full_View (Root_Typ)) = Parent_Typ;
3386 end if;
3387 end loop;
3389 -- Now collect components from all other ancestors, beginning
3390 -- with the current type. If the type has unknown discriminants
3391 -- use the component list of the Underlying_Record_View, which
3392 -- needs to be used for the subsequent expansion of the aggregate
3393 -- into assignments.
3395 Parent_Elmt := First_Elmt (Parent_Typ_List);
3396 while Present (Parent_Elmt) loop
3397 Parent_Typ := Node (Parent_Elmt);
3399 if Has_Unknown_Discriminants (Parent_Typ)
3400 and then Present (Underlying_Record_View (Typ))
3401 then
3402 Parent_Typ := Underlying_Record_View (Parent_Typ);
3403 end if;
3405 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
3406 Gather_Components (Empty,
3407 Component_List (Record_Extension_Part (Record_Def)),
3408 Governed_By => New_Assoc_List,
3409 Into => Components,
3410 Report_Errors => Errors_Found);
3412 Next_Elmt (Parent_Elmt);
3413 end loop;
3415 else
3416 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
3418 if Null_Present (Record_Def) then
3419 null;
3421 elsif not Has_Unknown_Discriminants (Typ) then
3422 Gather_Components (Base_Type (Typ),
3423 Component_List (Record_Def),
3424 Governed_By => New_Assoc_List,
3425 Into => Components,
3426 Report_Errors => Errors_Found);
3428 else
3429 Gather_Components
3430 (Base_Type (Underlying_Record_View (Typ)),
3431 Component_List (Record_Def),
3432 Governed_By => New_Assoc_List,
3433 Into => Components,
3434 Report_Errors => Errors_Found);
3435 end if;
3436 end if;
3438 if Errors_Found then
3439 return;
3440 end if;
3441 end Step_5;
3443 -- STEP 6: Find component Values
3445 Component := Empty;
3446 Component_Elmt := First_Elmt (Components);
3448 -- First scan the remaining positional associations in the aggregate.
3449 -- Remember that at this point Positional_Expr contains the current
3450 -- positional association if any is left after looking for discriminant
3451 -- values in step 3.
3453 while Present (Positional_Expr) and then Present (Component_Elmt) loop
3454 Component := Node (Component_Elmt);
3455 Resolve_Aggr_Expr (Positional_Expr, Component);
3457 -- Ada 2005 (AI-231)
3459 if Ada_Version >= Ada_2005
3460 and then Known_Null (Positional_Expr)
3461 then
3462 Check_Can_Never_Be_Null (Component, Positional_Expr);
3463 end if;
3465 if Present (Get_Value (Component, Component_Associations (N))) then
3466 Error_Msg_NE
3467 ("more than one value supplied for Component &", N, Component);
3468 end if;
3470 Next (Positional_Expr);
3471 Next_Elmt (Component_Elmt);
3472 end loop;
3474 if Present (Positional_Expr) then
3475 Error_Msg_N
3476 ("too many components for record aggregate", Positional_Expr);
3477 end if;
3479 -- Now scan for the named arguments of the aggregate
3481 while Present (Component_Elmt) loop
3482 Component := Node (Component_Elmt);
3483 Expr := Get_Value (Component, Component_Associations (N), True);
3485 -- Note: The previous call to Get_Value sets the value of the
3486 -- variable Is_Box_Present.
3488 -- Ada 2005 (AI-287): Handle components with default initialization.
3489 -- Note: This feature was originally added to Ada 2005 for limited
3490 -- but it was finally allowed with any type.
3492 if Is_Box_Present then
3493 Check_Box_Component : declare
3494 Ctyp : constant Entity_Id := Etype (Component);
3496 begin
3497 -- If there is a default expression for the aggregate, copy
3498 -- it into a new association.
3500 -- If the component has an initialization procedure (IP) we
3501 -- pass the component to the expander, which will generate
3502 -- the call to such IP.
3504 -- If the component has discriminants, their values must
3505 -- be taken from their subtype. This is indispensable for
3506 -- constraints that are given by the current instance of an
3507 -- enclosing type, to allow the expansion of the aggregate
3508 -- to replace the reference to the current instance by the
3509 -- target object of the aggregate.
3511 if Present (Parent (Component))
3512 and then
3513 Nkind (Parent (Component)) = N_Component_Declaration
3514 and then Present (Expression (Parent (Component)))
3515 then
3516 Expr :=
3517 New_Copy_Tree (Expression (Parent (Component)),
3518 New_Sloc => Sloc (N));
3520 Add_Association
3521 (Component => Component,
3522 Expr => Expr,
3523 Assoc_List => New_Assoc_List);
3524 Set_Has_Self_Reference (N);
3526 -- A box-defaulted access component gets the value null. Also
3527 -- included are components of private types whose underlying
3528 -- type is an access type. In either case set the type of the
3529 -- literal, for subsequent use in semantic checks.
3531 elsif Present (Underlying_Type (Ctyp))
3532 and then Is_Access_Type (Underlying_Type (Ctyp))
3533 then
3534 if not Is_Private_Type (Ctyp) then
3535 Expr := Make_Null (Sloc (N));
3536 Set_Etype (Expr, Ctyp);
3537 Add_Association
3538 (Component => Component,
3539 Expr => Expr,
3540 Assoc_List => New_Assoc_List);
3542 -- If the component's type is private with an access type as
3543 -- its underlying type then we have to create an unchecked
3544 -- conversion to satisfy type checking.
3546 else
3547 declare
3548 Qual_Null : constant Node_Id :=
3549 Make_Qualified_Expression (Sloc (N),
3550 Subtype_Mark =>
3551 New_Occurrence_Of
3552 (Underlying_Type (Ctyp), Sloc (N)),
3553 Expression => Make_Null (Sloc (N)));
3555 Convert_Null : constant Node_Id :=
3556 Unchecked_Convert_To
3557 (Ctyp, Qual_Null);
3559 begin
3560 Analyze_And_Resolve (Convert_Null, Ctyp);
3561 Add_Association
3562 (Component => Component,
3563 Expr => Convert_Null,
3564 Assoc_List => New_Assoc_List);
3565 end;
3566 end if;
3568 elsif Has_Non_Null_Base_Init_Proc (Ctyp)
3569 or else not Expander_Active
3570 then
3571 if Is_Record_Type (Ctyp)
3572 and then Has_Discriminants (Ctyp)
3573 and then not Is_Private_Type (Ctyp)
3574 then
3575 -- We build a partially initialized aggregate with the
3576 -- values of the discriminants and box initialization
3577 -- for the rest, if other components are present.
3578 -- The type of the aggregate is the known subtype of
3579 -- the component. The capture of discriminants must
3580 -- be recursive because subcomponents may be constrained
3581 -- (transitively) by discriminants of enclosing types.
3582 -- For a private type with discriminants, a call to the
3583 -- initialization procedure will be generated, and no
3584 -- subaggregate is needed.
3586 Capture_Discriminants : declare
3587 Loc : constant Source_Ptr := Sloc (N);
3588 Expr : Node_Id;
3590 procedure Add_Discriminant_Values
3591 (New_Aggr : Node_Id;
3592 Assoc_List : List_Id);
3593 -- The constraint to a component may be given by a
3594 -- discriminant of the enclosing type, in which case
3595 -- we have to retrieve its value, which is part of the
3596 -- enclosing aggregate. Assoc_List provides the
3597 -- discriminant associations of the current type or
3598 -- of some enclosing record.
3600 procedure Propagate_Discriminants
3601 (Aggr : Node_Id;
3602 Assoc_List : List_Id);
3603 -- Nested components may themselves be discriminated
3604 -- types constrained by outer discriminants, whose
3605 -- values must be captured before the aggregate is
3606 -- expanded into assignments.
3608 -----------------------------
3609 -- Add_Discriminant_Values --
3610 -----------------------------
3612 procedure Add_Discriminant_Values
3613 (New_Aggr : Node_Id;
3614 Assoc_List : List_Id)
3616 Assoc : Node_Id;
3617 Discr : Entity_Id;
3618 Discr_Elmt : Elmt_Id;
3619 Discr_Val : Node_Id;
3620 Val : Entity_Id;
3622 begin
3623 Discr := First_Discriminant (Etype (New_Aggr));
3624 Discr_Elmt :=
3625 First_Elmt
3626 (Discriminant_Constraint (Etype (New_Aggr)));
3627 while Present (Discr_Elmt) loop
3628 Discr_Val := Node (Discr_Elmt);
3630 -- If the constraint is given by a discriminant
3631 -- it is a discriminant of an enclosing record,
3632 -- and its value has already been placed in the
3633 -- association list.
3635 if Is_Entity_Name (Discr_Val)
3636 and then
3637 Ekind (Entity (Discr_Val)) = E_Discriminant
3638 then
3639 Val := Entity (Discr_Val);
3641 Assoc := First (Assoc_List);
3642 while Present (Assoc) loop
3643 if Present
3644 (Entity (First (Choices (Assoc))))
3645 and then
3646 Entity (First (Choices (Assoc)))
3647 = Val
3648 then
3649 Discr_Val := Expression (Assoc);
3650 exit;
3651 end if;
3652 Next (Assoc);
3653 end loop;
3654 end if;
3656 Add_Association
3657 (Discr, New_Copy_Tree (Discr_Val),
3658 Component_Associations (New_Aggr));
3660 -- If the discriminant constraint is a current
3661 -- instance, mark the current aggregate so that
3662 -- the self-reference can be expanded later.
3664 if Nkind (Discr_Val) = N_Attribute_Reference
3665 and then Is_Entity_Name (Prefix (Discr_Val))
3666 and then Is_Type (Entity (Prefix (Discr_Val)))
3667 and then Etype (N) =
3668 Entity (Prefix (Discr_Val))
3669 then
3670 Set_Has_Self_Reference (N);
3671 end if;
3673 Next_Elmt (Discr_Elmt);
3674 Next_Discriminant (Discr);
3675 end loop;
3676 end Add_Discriminant_Values;
3678 ------------------------------
3679 -- Propagate_Discriminants --
3680 ------------------------------
3682 procedure Propagate_Discriminants
3683 (Aggr : Node_Id;
3684 Assoc_List : List_Id)
3686 Aggr_Type : constant Entity_Id :=
3687 Base_Type (Etype (Aggr));
3688 Def_Node : constant Node_Id :=
3689 Type_Definition
3690 (Declaration_Node (Aggr_Type));
3692 Comp : Node_Id;
3693 Comp_Elmt : Elmt_Id;
3694 Components : constant Elist_Id := New_Elmt_List;
3695 Needs_Box : Boolean := False;
3696 Errors : Boolean;
3698 procedure Process_Component (Comp : Entity_Id);
3699 -- Add one component with a box association to the
3700 -- inner aggregate, and recurse if component is
3701 -- itself composite.
3703 ------------------------
3704 -- Process_Component --
3705 ------------------------
3707 procedure Process_Component (Comp : Entity_Id) is
3708 T : constant Entity_Id := Etype (Comp);
3709 New_Aggr : Node_Id;
3711 begin
3712 if Is_Record_Type (T)
3713 and then Has_Discriminants (T)
3714 then
3715 New_Aggr :=
3716 Make_Aggregate (Loc, New_List, New_List);
3717 Set_Etype (New_Aggr, T);
3718 Add_Association
3719 (Comp, New_Aggr,
3720 Component_Associations (Aggr));
3722 -- Collect discriminant values and recurse
3724 Add_Discriminant_Values
3725 (New_Aggr, Assoc_List);
3726 Propagate_Discriminants
3727 (New_Aggr, Assoc_List);
3729 else
3730 Needs_Box := True;
3731 end if;
3732 end Process_Component;
3734 -- Start of processing for Propagate_Discriminants
3736 begin
3737 -- The component type may be a variant type, so
3738 -- collect the components that are ruled by the
3739 -- known values of the discriminants. Their values
3740 -- have already been inserted into the component
3741 -- list of the current aggregate.
3743 if Nkind (Def_Node) = N_Record_Definition
3744 and then
3745 Present (Component_List (Def_Node))
3746 and then
3747 Present
3748 (Variant_Part (Component_List (Def_Node)))
3749 then
3750 Gather_Components (Aggr_Type,
3751 Component_List (Def_Node),
3752 Governed_By => Component_Associations (Aggr),
3753 Into => Components,
3754 Report_Errors => Errors);
3756 Comp_Elmt := First_Elmt (Components);
3757 while Present (Comp_Elmt) loop
3759 Ekind (Node (Comp_Elmt)) /= E_Discriminant
3760 then
3761 Process_Component (Node (Comp_Elmt));
3762 end if;
3764 Next_Elmt (Comp_Elmt);
3765 end loop;
3767 -- No variant part, iterate over all components
3769 else
3770 Comp := First_Component (Etype (Aggr));
3771 while Present (Comp) loop
3772 Process_Component (Comp);
3773 Next_Component (Comp);
3774 end loop;
3775 end if;
3777 if Needs_Box then
3778 Append
3779 (Make_Component_Association (Loc,
3780 Choices =>
3781 New_List (Make_Others_Choice (Loc)),
3782 Expression => Empty,
3783 Box_Present => True),
3784 Component_Associations (Aggr));
3785 end if;
3786 end Propagate_Discriminants;
3788 -- Start of processing for Capture_Discriminants
3790 begin
3791 Expr := Make_Aggregate (Loc, New_List, New_List);
3792 Set_Etype (Expr, Ctyp);
3794 -- If the enclosing type has discriminants, they have
3795 -- been collected in the aggregate earlier, and they
3796 -- may appear as constraints of subcomponents.
3798 -- Similarly if this component has discriminants, they
3799 -- might in turn be propagated to their components.
3801 if Has_Discriminants (Typ) then
3802 Add_Discriminant_Values (Expr, New_Assoc_List);
3803 Propagate_Discriminants (Expr, New_Assoc_List);
3805 elsif Has_Discriminants (Ctyp) then
3806 Add_Discriminant_Values
3807 (Expr, Component_Associations (Expr));
3808 Propagate_Discriminants
3809 (Expr, Component_Associations (Expr));
3811 else
3812 declare
3813 Comp : Entity_Id;
3815 begin
3816 -- If the type has additional components, create
3817 -- an OTHERS box association for them.
3819 Comp := First_Component (Ctyp);
3820 while Present (Comp) loop
3821 if Ekind (Comp) = E_Component then
3822 if not Is_Record_Type (Etype (Comp)) then
3823 Append
3824 (Make_Component_Association (Loc,
3825 Choices =>
3826 New_List
3827 (Make_Others_Choice (Loc)),
3828 Expression => Empty,
3829 Box_Present => True),
3830 Component_Associations (Expr));
3831 end if;
3832 exit;
3833 end if;
3835 Next_Component (Comp);
3836 end loop;
3837 end;
3838 end if;
3840 Add_Association
3841 (Component => Component,
3842 Expr => Expr,
3843 Assoc_List => New_Assoc_List);
3844 end Capture_Discriminants;
3846 else
3847 Add_Association
3848 (Component => Component,
3849 Expr => Empty,
3850 Assoc_List => New_Assoc_List,
3851 Is_Box_Present => True);
3852 end if;
3854 -- Otherwise we only need to resolve the expression if the
3855 -- component has partially initialized values (required to
3856 -- expand the corresponding assignments and run-time checks).
3858 elsif Present (Expr)
3859 and then Is_Partially_Initialized_Type (Ctyp)
3860 then
3861 Resolve_Aggr_Expr (Expr, Component);
3862 end if;
3863 end Check_Box_Component;
3865 elsif No (Expr) then
3867 -- Ignore hidden components associated with the position of the
3868 -- interface tags: these are initialized dynamically.
3870 if not Present (Related_Type (Component)) then
3871 Error_Msg_NE
3872 ("no value supplied for component &!", N, Component);
3873 end if;
3875 else
3876 Resolve_Aggr_Expr (Expr, Component);
3877 end if;
3879 Next_Elmt (Component_Elmt);
3880 end loop;
3882 -- STEP 7: check for invalid components + check type in choice list
3884 Step_7 : declare
3885 Selectr : Node_Id;
3886 -- Selector name
3888 Typech : Entity_Id;
3889 -- Type of first component in choice list
3891 begin
3892 if Present (Component_Associations (N)) then
3893 Assoc := First (Component_Associations (N));
3894 else
3895 Assoc := Empty;
3896 end if;
3898 Verification : while Present (Assoc) loop
3899 Selectr := First (Choices (Assoc));
3900 Typech := Empty;
3902 if Nkind (Selectr) = N_Others_Choice then
3904 -- Ada 2005 (AI-287): others choice may have expression or box
3906 if No (Others_Etype)
3907 and then not Others_Box
3908 then
3909 Error_Msg_N
3910 ("OTHERS must represent at least one component", Selectr);
3911 end if;
3913 exit Verification;
3914 end if;
3916 while Present (Selectr) loop
3917 New_Assoc := First (New_Assoc_List);
3918 while Present (New_Assoc) loop
3919 Component := First (Choices (New_Assoc));
3921 if Chars (Selectr) = Chars (Component) then
3922 if Style_Check then
3923 Check_Identifier (Selectr, Entity (Component));
3924 end if;
3926 exit;
3927 end if;
3929 Next (New_Assoc);
3930 end loop;
3932 -- If no association, this is not a legal component of
3933 -- of the type in question, except if its association
3934 -- is provided with a box.
3936 if No (New_Assoc) then
3937 if Box_Present (Parent (Selectr)) then
3939 -- This may still be a bogus component with a box. Scan
3940 -- list of components to verify that a component with
3941 -- that name exists.
3943 declare
3944 C : Entity_Id;
3946 begin
3947 C := First_Component (Typ);
3948 while Present (C) loop
3949 if Chars (C) = Chars (Selectr) then
3951 -- If the context is an extension aggregate,
3952 -- the component must not be inherited from
3953 -- the ancestor part of the aggregate.
3955 if Nkind (N) /= N_Extension_Aggregate
3956 or else
3957 Scope (Original_Record_Component (C)) /=
3958 Etype (Ancestor_Part (N))
3959 then
3960 exit;
3961 end if;
3962 end if;
3964 Next_Component (C);
3965 end loop;
3967 if No (C) then
3968 Error_Msg_Node_2 := Typ;
3969 Error_Msg_N ("& is not a component of}", Selectr);
3970 end if;
3971 end;
3973 elsif Chars (Selectr) /= Name_uTag
3974 and then Chars (Selectr) /= Name_uParent
3975 and then Chars (Selectr) /= Name_uController
3976 then
3977 if not Has_Discriminants (Typ) then
3978 Error_Msg_Node_2 := Typ;
3979 Error_Msg_N ("& is not a component of}", Selectr);
3980 else
3981 Error_Msg_N
3982 ("& is not a component of the aggregate subtype",
3983 Selectr);
3984 end if;
3986 Check_Misspelled_Component (Components, Selectr);
3987 end if;
3989 elsif No (Typech) then
3990 Typech := Base_Type (Etype (Component));
3992 -- AI05-0199: In Ada 2012, several components of anonymous
3993 -- access types can appear in a choice list, as long as the
3994 -- designated types match.
3996 elsif Typech /= Base_Type (Etype (Component)) then
3997 if Ada_Version >= Ada_2012
3998 and then Ekind (Typech) = E_Anonymous_Access_Type
3999 and then
4000 Ekind (Etype (Component)) = E_Anonymous_Access_Type
4001 and then Base_Type (Designated_Type (Typech)) =
4002 Base_Type (Designated_Type (Etype (Component)))
4003 and then
4004 Subtypes_Statically_Match (Typech, (Etype (Component)))
4005 then
4006 null;
4008 elsif not Box_Present (Parent (Selectr)) then
4009 Error_Msg_N
4010 ("components in choice list must have same type",
4011 Selectr);
4012 end if;
4013 end if;
4015 Next (Selectr);
4016 end loop;
4018 Next (Assoc);
4019 end loop Verification;
4020 end Step_7;
4022 -- STEP 8: replace the original aggregate
4024 Step_8 : declare
4025 New_Aggregate : constant Node_Id := New_Copy (N);
4027 begin
4028 Set_Expressions (New_Aggregate, No_List);
4029 Set_Etype (New_Aggregate, Etype (N));
4030 Set_Component_Associations (New_Aggregate, New_Assoc_List);
4032 Rewrite (N, New_Aggregate);
4033 end Step_8;
4034 end Resolve_Record_Aggregate;
4036 -----------------------------
4037 -- Check_Can_Never_Be_Null --
4038 -----------------------------
4040 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
4041 Comp_Typ : Entity_Id;
4043 begin
4044 pragma Assert
4045 (Ada_Version >= Ada_2005
4046 and then Present (Expr)
4047 and then Known_Null (Expr));
4049 case Ekind (Typ) is
4050 when E_Array_Type =>
4051 Comp_Typ := Component_Type (Typ);
4053 when E_Component |
4054 E_Discriminant =>
4055 Comp_Typ := Etype (Typ);
4057 when others =>
4058 return;
4059 end case;
4061 if Can_Never_Be_Null (Comp_Typ) then
4063 -- Here we know we have a constraint error. Note that we do not use
4064 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
4065 -- seem the more natural approach. That's because in some cases the
4066 -- components are rewritten, and the replacement would be missed.
4068 Insert_Action
4069 (Compile_Time_Constraint_Error
4070 (Expr,
4071 "(Ada 2005) null not allowed in null-excluding component?"),
4072 Make_Raise_Constraint_Error (Sloc (Expr),
4073 Reason => CE_Access_Check_Failed));
4075 -- Set proper type for bogus component (why is this needed???)
4077 Set_Etype (Expr, Comp_Typ);
4078 Set_Analyzed (Expr);
4079 end if;
4080 end Check_Can_Never_Be_Null;
4082 ---------------------
4083 -- Sort_Case_Table --
4084 ---------------------
4086 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
4087 L : constant Int := Case_Table'First;
4088 U : constant Int := Case_Table'Last;
4089 K : Int;
4090 J : Int;
4091 T : Case_Bounds;
4093 begin
4094 K := L;
4095 while K /= U loop
4096 T := Case_Table (K + 1);
4098 J := K + 1;
4099 while J /= L
4100 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
4101 Expr_Value (T.Choice_Lo)
4102 loop
4103 Case_Table (J) := Case_Table (J - 1);
4104 J := J - 1;
4105 end loop;
4107 Case_Table (J) := T;
4108 K := K + 1;
4109 end loop;
4110 end Sort_Case_Table;
4112 end Sem_Aggr;