sem_aggr.adb (Resolve_Record_Aggregate): In the case of a box association for an...
[official-gcc.git] / gcc / ada / sem_aggr.adb
blob3d5b62df4cf29bacf0360acdf8c0c5476c67e799
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-2007, 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 Exp_Tss; use Exp_Tss;
32 with Exp_Util; use Exp_Util;
33 with Freeze; use Freeze;
34 with Itypes; use Itypes;
35 with Lib; use Lib;
36 with Lib.Xref; use Lib.Xref;
37 with Namet; use Namet;
38 with Nmake; use Nmake;
39 with Nlists; use Nlists;
40 with Opt; use Opt;
41 with Sem; use Sem;
42 with Sem_Cat; use Sem_Cat;
43 with Sem_Ch3; use Sem_Ch3;
44 with Sem_Ch13; use Sem_Ch13;
45 with Sem_Eval; use Sem_Eval;
46 with Sem_Res; use Sem_Res;
47 with Sem_Util; use Sem_Util;
48 with Sem_Type; use Sem_Type;
49 with Sem_Warn; use Sem_Warn;
50 with Sinfo; use Sinfo;
51 with Snames; use Snames;
52 with Stringt; use Stringt;
53 with Stand; use Stand;
54 with Targparm; use Targparm;
55 with Tbuild; use Tbuild;
56 with Uintp; use Uintp;
58 with GNAT.Spelling_Checker; use GNAT.Spelling_Checker;
60 package body Sem_Aggr is
62 type Case_Bounds is record
63 Choice_Lo : Node_Id;
64 Choice_Hi : Node_Id;
65 Choice_Node : Node_Id;
66 end record;
68 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
69 -- Table type used by Check_Case_Choices procedure
71 -----------------------
72 -- Local Subprograms --
73 -----------------------
75 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
76 -- Sort the Case Table using the Lower Bound of each Choice as the key.
77 -- A simple insertion sort is used since the number of choices in a case
78 -- statement of variant part will usually be small and probably in near
79 -- sorted order.
81 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id);
82 -- Ada 2005 (AI-231): Check bad usage of null for a component for which
83 -- null exclusion (NOT NULL) is specified. Typ can be an E_Array_Type for
84 -- the array case (the component type of the array will be used) or an
85 -- E_Component/E_Discriminant entity in the record case, in which case the
86 -- type of the component will be used for the test. If Typ is any other
87 -- kind of entity, the call is ignored. Expr is the component node in the
88 -- aggregate which is known to have a null value. A warning message will be
89 -- issued if the component is null excluding.
91 -- It would be better to pass the proper type for Typ ???
93 ------------------------------------------------------
94 -- Subprograms used for RECORD AGGREGATE Processing --
95 ------------------------------------------------------
97 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
98 -- This procedure performs all the semantic checks required for record
99 -- aggregates. Note that for aggregates analysis and resolution go
100 -- hand in hand. Aggregate analysis has been delayed up to here and
101 -- it is done while resolving the aggregate.
103 -- N is the N_Aggregate node.
104 -- Typ is the record type for the aggregate resolution
106 -- While performing the semantic checks, this procedure builds a new
107 -- Component_Association_List where each record field appears alone in a
108 -- Component_Choice_List along with its corresponding expression. The
109 -- record fields in the Component_Association_List appear in the same order
110 -- in which they appear in the record type Typ.
112 -- Once this new Component_Association_List is built and all the semantic
113 -- checks performed, the original aggregate subtree is replaced with the
114 -- new named record aggregate just built. Note that subtree substitution is
115 -- performed with Rewrite so as to be able to retrieve the original
116 -- aggregate.
118 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
119 -- yields the aggregate format expected by Gigi. Typically, this kind of
120 -- tree manipulations are done in the expander. However, because the
121 -- semantic checks that need to be performed on record aggregates really go
122 -- hand in hand with the record aggregate normalization, the aggregate
123 -- subtree transformation is performed during resolution rather than
124 -- expansion. Had we decided otherwise we would have had to duplicate most
125 -- of the code in the expansion procedure Expand_Record_Aggregate. Note,
126 -- however, that all the expansion concerning aggregates for tagged records
127 -- is done in Expand_Record_Aggregate.
129 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
131 -- 1. Make sure that the record type against which the record aggregate
132 -- has to be resolved is not abstract. Furthermore if the type is
133 -- a null aggregate make sure the input aggregate N is also null.
135 -- 2. Verify that the structure of the aggregate is that of a record
136 -- aggregate. Specifically, look for component associations and ensure
137 -- that each choice list only has identifiers or the N_Others_Choice
138 -- node. Also make sure that if present, the N_Others_Choice occurs
139 -- last and by itself.
141 -- 3. If Typ contains discriminants, the values for each discriminant
142 -- is looked for. If the record type Typ has variants, we check
143 -- that the expressions corresponding to each discriminant ruling
144 -- the (possibly nested) variant parts of Typ, are static. This
145 -- allows us to determine the variant parts to which the rest of
146 -- the aggregate must conform. The names of discriminants with their
147 -- values are saved in a new association list, New_Assoc_List which
148 -- is later augmented with the names and values of the remaining
149 -- components in the record type.
151 -- During this phase we also make sure that every discriminant is
152 -- assigned exactly one value. Note that when several values
153 -- for a given discriminant are found, semantic processing continues
154 -- looking for further errors. In this case it's the first
155 -- discriminant value found which we will be recorded.
157 -- IMPORTANT NOTE: For derived tagged types this procedure expects
158 -- First_Discriminant and Next_Discriminant to give the correct list
159 -- of discriminants, in the correct order.
161 -- 4. After all the discriminant values have been gathered, we can
162 -- set the Etype of the record aggregate. If Typ contains no
163 -- discriminants this is straightforward: the Etype of N is just
164 -- Typ, otherwise a new implicit constrained subtype of Typ is
165 -- built to be the Etype of N.
167 -- 5. Gather the remaining record components according to the discriminant
168 -- values. This involves recursively traversing the record type
169 -- structure to see what variants are selected by the given discriminant
170 -- values. This processing is a little more convoluted if Typ is a
171 -- derived tagged types since we need to retrieve the record structure
172 -- of all the ancestors of Typ.
174 -- 6. After gathering the record components we look for their values
175 -- in the record aggregate and emit appropriate error messages
176 -- should we not find such values or should they be duplicated.
178 -- 7. We then make sure no illegal component names appear in the
179 -- record aggregate and make sure that the type of the record
180 -- components appearing in a same choice list is the same.
181 -- Finally we ensure that the others choice, if present, is
182 -- used to provide the value of at least a record component.
184 -- 8. The original aggregate node is replaced with the new named
185 -- aggregate built in steps 3 through 6, as explained earlier.
187 -- Given the complexity of record aggregate resolution, the primary
188 -- goal of this routine is clarity and simplicity rather than execution
189 -- and storage efficiency. If there are only positional components in the
190 -- aggregate the running time is linear. If there are associations
191 -- the running time is still linear as long as the order of the
192 -- associations is not too far off the order of the components in the
193 -- record type. If this is not the case the running time is at worst
194 -- quadratic in the size of the association list.
196 procedure Check_Misspelled_Component
197 (Elements : Elist_Id;
198 Component : Node_Id);
199 -- Give possible misspelling diagnostic if Component is likely to be
200 -- a misspelling of one of the components of the Assoc_List.
201 -- This is called by Resolv_Aggr_Expr after producing
202 -- an invalid component error message.
204 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id);
205 -- An optimization: determine whether a discriminated subtype has a
206 -- static constraint, and contains array components whose length is also
207 -- static, either because they are constrained by the discriminant, or
208 -- because the original component bounds are static.
210 -----------------------------------------------------
211 -- Subprograms used for ARRAY AGGREGATE Processing --
212 -----------------------------------------------------
214 function Resolve_Array_Aggregate
215 (N : Node_Id;
216 Index : Node_Id;
217 Index_Constr : Node_Id;
218 Component_Typ : Entity_Id;
219 Others_Allowed : Boolean)
220 return Boolean;
221 -- This procedure performs the semantic checks for an array aggregate.
222 -- True is returned if the aggregate resolution succeeds.
223 -- The procedure works by recursively checking each nested aggregate.
224 -- Specifically, after checking a sub-aggregate nested at the i-th level
225 -- we recursively check all the subaggregates at the i+1-st level (if any).
226 -- Note that for aggregates analysis and resolution go hand in hand.
227 -- Aggregate analysis has been delayed up to here and it is done while
228 -- resolving the aggregate.
230 -- N is the current N_Aggregate node to be checked.
232 -- Index is the index node corresponding to the array sub-aggregate that
233 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
234 -- corresponding index type (or subtype).
236 -- Index_Constr is the node giving the applicable index constraint if
237 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
238 -- contexts [...] that can be used to determine the bounds of the array
239 -- value specified by the aggregate". If Others_Allowed below is False
240 -- there is no applicable index constraint and this node is set to Index.
242 -- Component_Typ is the array component type.
244 -- Others_Allowed indicates whether an others choice is allowed
245 -- in the context where the top-level aggregate appeared.
247 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
249 -- 1. Make sure that the others choice, if present, is by itself and
250 -- appears last in the sub-aggregate. Check that we do not have
251 -- positional and named components in the array sub-aggregate (unless
252 -- the named association is an others choice). Finally if an others
253 -- choice is present, make sure it is allowed in the aggregate contex.
255 -- 2. If the array sub-aggregate contains discrete_choices:
257 -- (A) Verify their validity. Specifically verify that:
259 -- (a) If a null range is present it must be the only possible
260 -- choice in the array aggregate.
262 -- (b) Ditto for a non static range.
264 -- (c) Ditto for a non static expression.
266 -- In addition this step analyzes and resolves each discrete_choice,
267 -- making sure that its type is the type of the corresponding Index.
268 -- If we are not at the lowest array aggregate level (in the case of
269 -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
270 -- recursively on each component expression. Otherwise, resolve the
271 -- bottom level component expressions against the expected component
272 -- type ONLY IF the component corresponds to a single discrete choice
273 -- which is not an others choice (to see why read the DELAYED
274 -- COMPONENT RESOLUTION below).
276 -- (B) Determine the bounds of the sub-aggregate and lowest and
277 -- highest choice values.
279 -- 3. For positional aggregates:
281 -- (A) Loop over the component expressions either recursively invoking
282 -- Resolve_Array_Aggregate on each of these for multi-dimensional
283 -- array aggregates or resolving the bottom level component
284 -- expressions against the expected component type.
286 -- (B) Determine the bounds of the positional sub-aggregates.
288 -- 4. Try to determine statically whether the evaluation of the array
289 -- sub-aggregate raises Constraint_Error. If yes emit proper
290 -- warnings. The precise checks are the following:
292 -- (A) Check that the index range defined by aggregate bounds is
293 -- compatible with corresponding index subtype.
294 -- We also check against the base type. In fact it could be that
295 -- Low/High bounds of the base type are static whereas those of
296 -- the index subtype are not. Thus if we can statically catch
297 -- a problem with respect to the base type we are guaranteed
298 -- that the same problem will arise with the index subtype
300 -- (B) If we are dealing with a named aggregate containing an others
301 -- choice and at least one discrete choice then make sure the range
302 -- specified by the discrete choices does not overflow the
303 -- aggregate bounds. We also check against the index type and base
304 -- type bounds for the same reasons given in (A).
306 -- (C) If we are dealing with a positional aggregate with an others
307 -- choice make sure the number of positional elements specified
308 -- does not overflow the aggregate bounds. We also check against
309 -- the index type and base type bounds as mentioned in (A).
311 -- Finally construct an N_Range node giving the sub-aggregate bounds.
312 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
313 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
314 -- to build the appropriate aggregate subtype. Aggregate_Bounds
315 -- information is needed during expansion.
317 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
318 -- expressions in an array aggregate may call Duplicate_Subexpr or some
319 -- other routine that inserts code just outside the outermost aggregate.
320 -- If the array aggregate contains discrete choices or an others choice,
321 -- this may be wrong. Consider for instance the following example.
323 -- type Rec is record
324 -- V : Integer := 0;
325 -- end record;
327 -- type Acc_Rec is access Rec;
328 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
330 -- Then the transformation of "new Rec" that occurs during resolution
331 -- entails the following code modifications
333 -- P7b : constant Acc_Rec := new Rec;
334 -- RecIP (P7b.all);
335 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
337 -- This code transformation is clearly wrong, since we need to call
338 -- "new Rec" for each of the 3 array elements. To avoid this problem we
339 -- delay resolution of the components of non positional array aggregates
340 -- to the expansion phase. As an optimization, if the discrete choice
341 -- specifies a single value we do not delay resolution.
343 function Array_Aggr_Subtype (N : Node_Id; Typ : Node_Id) return Entity_Id;
344 -- This routine returns the type or subtype of an array aggregate.
346 -- N is the array aggregate node whose type we return.
348 -- Typ is the context type in which N occurs.
350 -- This routine creates an implicit array subtype whose bounds are
351 -- those defined by the aggregate. When this routine is invoked
352 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
353 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
354 -- sub-aggregate bounds. When building the aggregate itype, this function
355 -- traverses the array aggregate N collecting such Aggregate_Bounds and
356 -- constructs the proper array aggregate itype.
358 -- Note that in the case of multidimensional aggregates each inner
359 -- sub-aggregate corresponding to a given array dimension, may provide a
360 -- different bounds. If it is possible to determine statically that
361 -- some sub-aggregates corresponding to the same index do not have the
362 -- same bounds, then a warning is emitted. If such check is not possible
363 -- statically (because some sub-aggregate bounds are dynamic expressions)
364 -- then this job is left to the expander. In all cases the particular
365 -- bounds that this function will chose for a given dimension is the first
366 -- N_Range node for a sub-aggregate corresponding to that dimension.
368 -- Note that the Raises_Constraint_Error flag of an array aggregate
369 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
370 -- is set in Resolve_Array_Aggregate but the aggregate is not
371 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
372 -- first construct the proper itype for the aggregate (Gigi needs
373 -- this). After constructing the proper itype we will eventually replace
374 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
375 -- Of course in cases such as:
377 -- type Arr is array (integer range <>) of Integer;
378 -- A : Arr := (positive range -1 .. 2 => 0);
380 -- The bounds of the aggregate itype are cooked up to look reasonable
381 -- (in this particular case the bounds will be 1 .. 2).
383 procedure Aggregate_Constraint_Checks
384 (Exp : Node_Id;
385 Check_Typ : Entity_Id);
386 -- Checks expression Exp against subtype Check_Typ. If Exp is an
387 -- aggregate and Check_Typ a constrained record type with discriminants,
388 -- we generate the appropriate discriminant checks. If Exp is an array
389 -- aggregate then emit the appropriate length checks. If Exp is a scalar
390 -- type, or a string literal, Exp is changed into Check_Typ'(Exp) to
391 -- ensure that range checks are performed at run time.
393 procedure Make_String_Into_Aggregate (N : Node_Id);
394 -- A string literal can appear in a context in which a one dimensional
395 -- array of characters is expected. This procedure simply rewrites the
396 -- string as an aggregate, prior to resolution.
398 ---------------------------------
399 -- Aggregate_Constraint_Checks --
400 ---------------------------------
402 procedure Aggregate_Constraint_Checks
403 (Exp : Node_Id;
404 Check_Typ : Entity_Id)
406 Exp_Typ : constant Entity_Id := Etype (Exp);
408 begin
409 if Raises_Constraint_Error (Exp) then
410 return;
411 end if;
413 -- This is really expansion activity, so make sure that expansion
414 -- is on and is allowed.
416 if not Expander_Active or else In_Default_Expression then
417 return;
418 end if;
420 -- First check if we have to insert discriminant checks
422 if Has_Discriminants (Exp_Typ) then
423 Apply_Discriminant_Check (Exp, Check_Typ);
425 -- Next emit length checks for array aggregates
427 elsif Is_Array_Type (Exp_Typ) then
428 Apply_Length_Check (Exp, Check_Typ);
430 -- Finally emit scalar and string checks. If we are dealing with a
431 -- scalar literal we need to check by hand because the Etype of
432 -- literals is not necessarily correct.
434 elsif Is_Scalar_Type (Exp_Typ)
435 and then Compile_Time_Known_Value (Exp)
436 then
437 if Is_Out_Of_Range (Exp, Base_Type (Check_Typ)) then
438 Apply_Compile_Time_Constraint_Error
439 (Exp, "value not in range of}?", CE_Range_Check_Failed,
440 Ent => Base_Type (Check_Typ),
441 Typ => Base_Type (Check_Typ));
443 elsif Is_Out_Of_Range (Exp, Check_Typ) then
444 Apply_Compile_Time_Constraint_Error
445 (Exp, "value not in range of}?", CE_Range_Check_Failed,
446 Ent => Check_Typ,
447 Typ => Check_Typ);
449 elsif not Range_Checks_Suppressed (Check_Typ) then
450 Apply_Scalar_Range_Check (Exp, Check_Typ);
451 end if;
453 -- Verify that target type is also scalar, to prevent view anomalies
454 -- in instantiations.
456 elsif (Is_Scalar_Type (Exp_Typ)
457 or else Nkind (Exp) = N_String_Literal)
458 and then Is_Scalar_Type (Check_Typ)
459 and then Exp_Typ /= Check_Typ
460 then
461 if Is_Entity_Name (Exp)
462 and then Ekind (Entity (Exp)) = E_Constant
463 then
464 -- If expression is a constant, it is worthwhile checking whether
465 -- it is a bound of the type.
467 if (Is_Entity_Name (Type_Low_Bound (Check_Typ))
468 and then Entity (Exp) = Entity (Type_Low_Bound (Check_Typ)))
469 or else (Is_Entity_Name (Type_High_Bound (Check_Typ))
470 and then Entity (Exp) = Entity (Type_High_Bound (Check_Typ)))
471 then
472 return;
474 else
475 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
476 Analyze_And_Resolve (Exp, Check_Typ);
477 Check_Unset_Reference (Exp);
478 end if;
479 else
480 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
481 Analyze_And_Resolve (Exp, Check_Typ);
482 Check_Unset_Reference (Exp);
483 end if;
485 -- Ada 2005 (AI-230): Generate a conversion to an anonymous access
486 -- component's type to force the appropriate accessibility checks.
488 -- Ada 2005 (AI-231): Generate conversion to the null-excluding
489 -- type to force the corresponding run-time check
491 elsif Is_Access_Type (Check_Typ)
492 and then ((Is_Local_Anonymous_Access (Check_Typ))
493 or else (Can_Never_Be_Null (Check_Typ)
494 and then not Can_Never_Be_Null (Exp_Typ)))
495 then
496 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
497 Analyze_And_Resolve (Exp, Check_Typ);
498 Check_Unset_Reference (Exp);
499 end if;
500 end Aggregate_Constraint_Checks;
502 ------------------------
503 -- Array_Aggr_Subtype --
504 ------------------------
506 function Array_Aggr_Subtype
507 (N : Node_Id;
508 Typ : Entity_Id)
509 return Entity_Id
511 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
512 -- Number of aggregate index dimensions
514 Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
515 -- Constrained N_Range of each index dimension in our aggregate itype
517 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
518 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
519 -- Low and High bounds for each index dimension in our aggregate itype
521 Is_Fully_Positional : Boolean := True;
523 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
524 -- N is an array (sub-)aggregate. Dim is the dimension corresponding to
525 -- (sub-)aggregate N. This procedure collects the constrained N_Range
526 -- nodes corresponding to each index dimension of our aggregate itype.
527 -- These N_Range nodes are collected in Aggr_Range above.
529 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
530 -- bounds of each index dimension. If, when collecting, two bounds
531 -- corresponding to the same dimension are static and found to differ,
532 -- then emit a warning, and mark N as raising Constraint_Error.
534 -------------------------
535 -- Collect_Aggr_Bounds --
536 -------------------------
538 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
539 This_Range : constant Node_Id := Aggregate_Bounds (N);
540 -- The aggregate range node of this specific sub-aggregate
542 This_Low : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
543 This_High : constant Node_Id := High_Bound (Aggregate_Bounds (N));
544 -- The aggregate bounds of this specific sub-aggregate
546 Assoc : Node_Id;
547 Expr : Node_Id;
549 begin
550 -- Collect the first N_Range for a given dimension that you find.
551 -- For a given dimension they must be all equal anyway.
553 if No (Aggr_Range (Dim)) then
554 Aggr_Low (Dim) := This_Low;
555 Aggr_High (Dim) := This_High;
556 Aggr_Range (Dim) := This_Range;
558 else
559 if Compile_Time_Known_Value (This_Low) then
560 if not Compile_Time_Known_Value (Aggr_Low (Dim)) then
561 Aggr_Low (Dim) := This_Low;
563 elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
564 Set_Raises_Constraint_Error (N);
565 Error_Msg_N ("sub-aggregate low bound mismatch?", N);
566 Error_Msg_N
567 ("\Constraint_Error will be raised at run-time?", N);
568 end if;
569 end if;
571 if Compile_Time_Known_Value (This_High) then
572 if not Compile_Time_Known_Value (Aggr_High (Dim)) then
573 Aggr_High (Dim) := This_High;
575 elsif
576 Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
577 then
578 Set_Raises_Constraint_Error (N);
579 Error_Msg_N ("sub-aggregate high bound mismatch?", N);
580 Error_Msg_N
581 ("\Constraint_Error will be raised at run-time?", N);
582 end if;
583 end if;
584 end if;
586 if Dim < Aggr_Dimension then
588 -- Process positional components
590 if Present (Expressions (N)) then
591 Expr := First (Expressions (N));
592 while Present (Expr) loop
593 Collect_Aggr_Bounds (Expr, Dim + 1);
594 Next (Expr);
595 end loop;
596 end if;
598 -- Process component associations
600 if Present (Component_Associations (N)) then
601 Is_Fully_Positional := False;
603 Assoc := First (Component_Associations (N));
604 while Present (Assoc) loop
605 Expr := Expression (Assoc);
606 Collect_Aggr_Bounds (Expr, Dim + 1);
607 Next (Assoc);
608 end loop;
609 end if;
610 end if;
611 end Collect_Aggr_Bounds;
613 -- Array_Aggr_Subtype variables
615 Itype : Entity_Id;
616 -- the final itype of the overall aggregate
618 Index_Constraints : constant List_Id := New_List;
619 -- The list of index constraints of the aggregate itype
621 -- Start of processing for Array_Aggr_Subtype
623 begin
624 -- Make sure that the list of index constraints is properly attached
625 -- to the tree, and then collect the aggregate bounds.
627 Set_Parent (Index_Constraints, N);
628 Collect_Aggr_Bounds (N, 1);
630 -- Build the list of constrained indices of our aggregate itype
632 for J in 1 .. Aggr_Dimension loop
633 Create_Index : declare
634 Index_Base : constant Entity_Id :=
635 Base_Type (Etype (Aggr_Range (J)));
636 Index_Typ : Entity_Id;
638 begin
639 -- Construct the Index subtype, and associate it with the range
640 -- construct that generates it.
642 Index_Typ :=
643 Create_Itype (Subtype_Kind (Ekind (Index_Base)), Aggr_Range (J));
645 Set_Etype (Index_Typ, Index_Base);
647 if Is_Character_Type (Index_Base) then
648 Set_Is_Character_Type (Index_Typ);
649 end if;
651 Set_Size_Info (Index_Typ, (Index_Base));
652 Set_RM_Size (Index_Typ, RM_Size (Index_Base));
653 Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base));
654 Set_Scalar_Range (Index_Typ, Aggr_Range (J));
656 if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then
657 Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ)));
658 end if;
660 Set_Etype (Aggr_Range (J), Index_Typ);
662 Append (Aggr_Range (J), To => Index_Constraints);
663 end Create_Index;
664 end loop;
666 -- Now build the Itype
668 Itype := Create_Itype (E_Array_Subtype, N);
670 Set_First_Rep_Item (Itype, First_Rep_Item (Typ));
671 Set_Convention (Itype, Convention (Typ));
672 Set_Depends_On_Private (Itype, Has_Private_Component (Typ));
673 Set_Etype (Itype, Base_Type (Typ));
674 Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ));
675 Set_Is_Aliased (Itype, Is_Aliased (Typ));
676 Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
678 Copy_Suppress_Status (Index_Check, Typ, Itype);
679 Copy_Suppress_Status (Length_Check, Typ, Itype);
681 Set_First_Index (Itype, First (Index_Constraints));
682 Set_Is_Constrained (Itype, True);
683 Set_Is_Internal (Itype, True);
684 Init_Size_Align (Itype);
686 -- A simple optimization: purely positional aggregates of static
687 -- components should be passed to gigi unexpanded whenever possible,
688 -- and regardless of the staticness of the bounds themselves. Subse-
689 -- quent checks in exp_aggr verify that type is not packed, etc.
691 Set_Size_Known_At_Compile_Time (Itype,
692 Is_Fully_Positional
693 and then Comes_From_Source (N)
694 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
696 -- We always need a freeze node for a packed array subtype, so that
697 -- we can build the Packed_Array_Type corresponding to the subtype.
698 -- If expansion is disabled, the packed array subtype is not built,
699 -- and we must not generate a freeze node for the type, or else it
700 -- will appear incomplete to gigi.
702 if Is_Packed (Itype) and then not In_Default_Expression
703 and then Expander_Active
704 then
705 Freeze_Itype (Itype, N);
706 end if;
708 return Itype;
709 end Array_Aggr_Subtype;
711 --------------------------------
712 -- Check_Misspelled_Component --
713 --------------------------------
715 procedure Check_Misspelled_Component
716 (Elements : Elist_Id;
717 Component : Node_Id)
719 Max_Suggestions : constant := 2;
721 Nr_Of_Suggestions : Natural := 0;
722 Suggestion_1 : Entity_Id := Empty;
723 Suggestion_2 : Entity_Id := Empty;
724 Component_Elmt : Elmt_Id;
726 begin
727 -- All the components of List are matched against Component and
728 -- a count is maintained of possible misspellings. When at the
729 -- end of the analysis there are one or two (not more!) possible
730 -- misspellings, these misspellings will be suggested as
731 -- possible correction.
733 Get_Name_String (Chars (Component));
735 declare
736 S : constant String (1 .. Name_Len) :=
737 Name_Buffer (1 .. Name_Len);
739 begin
740 Component_Elmt := First_Elmt (Elements);
741 while Nr_Of_Suggestions <= Max_Suggestions
742 and then Present (Component_Elmt)
743 loop
744 Get_Name_String (Chars (Node (Component_Elmt)));
746 if Is_Bad_Spelling_Of (Name_Buffer (1 .. Name_Len), S) then
747 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
749 case Nr_Of_Suggestions is
750 when 1 => Suggestion_1 := Node (Component_Elmt);
751 when 2 => Suggestion_2 := Node (Component_Elmt);
752 when others => exit;
753 end case;
754 end if;
756 Next_Elmt (Component_Elmt);
757 end loop;
759 -- Report at most two suggestions
761 if Nr_Of_Suggestions = 1 then
762 Error_Msg_NE ("\possible misspelling of&",
763 Component, Suggestion_1);
765 elsif Nr_Of_Suggestions = 2 then
766 Error_Msg_Node_2 := Suggestion_2;
767 Error_Msg_NE ("\possible misspelling of& or&",
768 Component, Suggestion_1);
769 end if;
770 end;
771 end Check_Misspelled_Component;
773 ----------------------------------------
774 -- Check_Static_Discriminated_Subtype --
775 ----------------------------------------
777 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id) is
778 Disc : constant Entity_Id := First_Discriminant (T);
779 Comp : Entity_Id;
780 Ind : Entity_Id;
782 begin
783 if Has_Record_Rep_Clause (T) then
784 return;
786 elsif Present (Next_Discriminant (Disc)) then
787 return;
789 elsif Nkind (V) /= N_Integer_Literal then
790 return;
791 end if;
793 Comp := First_Component (T);
794 while Present (Comp) loop
795 if Is_Scalar_Type (Etype (Comp)) then
796 null;
798 elsif Is_Private_Type (Etype (Comp))
799 and then Present (Full_View (Etype (Comp)))
800 and then Is_Scalar_Type (Full_View (Etype (Comp)))
801 then
802 null;
804 elsif Is_Array_Type (Etype (Comp)) then
805 if Is_Bit_Packed_Array (Etype (Comp)) then
806 return;
807 end if;
809 Ind := First_Index (Etype (Comp));
810 while Present (Ind) loop
811 if Nkind (Ind) /= N_Range
812 or else Nkind (Low_Bound (Ind)) /= N_Integer_Literal
813 or else Nkind (High_Bound (Ind)) /= N_Integer_Literal
814 then
815 return;
816 end if;
818 Next_Index (Ind);
819 end loop;
821 else
822 return;
823 end if;
825 Next_Component (Comp);
826 end loop;
828 -- On exit, all components have statically known sizes
830 Set_Size_Known_At_Compile_Time (T);
831 end Check_Static_Discriminated_Subtype;
833 --------------------------------
834 -- Make_String_Into_Aggregate --
835 --------------------------------
837 procedure Make_String_Into_Aggregate (N : Node_Id) is
838 Exprs : constant List_Id := New_List;
839 Loc : constant Source_Ptr := Sloc (N);
840 Str : constant String_Id := Strval (N);
841 Strlen : constant Nat := String_Length (Str);
842 C : Char_Code;
843 C_Node : Node_Id;
844 New_N : Node_Id;
845 P : Source_Ptr;
847 begin
848 P := Loc + 1;
849 for J in 1 .. Strlen loop
850 C := Get_String_Char (Str, J);
851 Set_Character_Literal_Name (C);
853 C_Node :=
854 Make_Character_Literal (P,
855 Chars => Name_Find,
856 Char_Literal_Value => UI_From_CC (C));
857 Set_Etype (C_Node, Any_Character);
858 Append_To (Exprs, C_Node);
860 P := P + 1;
861 -- something special for wide strings ???
862 end loop;
864 New_N := Make_Aggregate (Loc, Expressions => Exprs);
865 Set_Analyzed (New_N);
866 Set_Etype (New_N, Any_Composite);
868 Rewrite (N, New_N);
869 end Make_String_Into_Aggregate;
871 -----------------------
872 -- Resolve_Aggregate --
873 -----------------------
875 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
876 Pkind : constant Node_Kind := Nkind (Parent (N));
878 Aggr_Subtyp : Entity_Id;
879 -- The actual aggregate subtype. This is not necessarily the same as Typ
880 -- which is the subtype of the context in which the aggregate was found.
882 begin
883 -- Check for aggregates not allowed in configurable run-time mode.
884 -- We allow all cases of aggregates that do not come from source,
885 -- since these are all assumed to be small (e.g. bounds of a string
886 -- literal). We also allow aggregates of types we know to be small.
888 if not Support_Aggregates_On_Target
889 and then Comes_From_Source (N)
890 and then (not Known_Static_Esize (Typ) or else Esize (Typ) > 64)
891 then
892 Error_Msg_CRT ("aggregate", N);
893 end if;
895 -- Ada 2005 (AI-287): Limited aggregates allowed
897 if Is_Limited_Type (Typ) and then Ada_Version < Ada_05 then
898 Error_Msg_N ("aggregate type cannot be limited", N);
899 Explain_Limited_Type (Typ, N);
901 elsif Is_Class_Wide_Type (Typ) then
902 Error_Msg_N ("type of aggregate cannot be class-wide", N);
904 elsif Typ = Any_String
905 or else Typ = Any_Composite
906 then
907 Error_Msg_N ("no unique type for aggregate", N);
908 Set_Etype (N, Any_Composite);
910 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
911 Error_Msg_N ("null record forbidden in array aggregate", N);
913 elsif Is_Record_Type (Typ) then
914 Resolve_Record_Aggregate (N, Typ);
916 elsif Is_Array_Type (Typ) then
918 -- First a special test, for the case of a positional aggregate
919 -- of characters which can be replaced by a string literal.
920 -- Do not perform this transformation if this was a string literal
921 -- to start with, whose components needed constraint checks, or if
922 -- the component type is non-static, because it will require those
923 -- checks and be transformed back into an aggregate.
925 if Number_Dimensions (Typ) = 1
926 and then
927 (Root_Type (Component_Type (Typ)) = Standard_Character
928 or else
929 Root_Type (Component_Type (Typ)) = Standard_Wide_Character
930 or else
931 Root_Type (Component_Type (Typ)) = Standard_Wide_Wide_Character)
932 and then No (Component_Associations (N))
933 and then not Is_Limited_Composite (Typ)
934 and then not Is_Private_Composite (Typ)
935 and then not Is_Bit_Packed_Array (Typ)
936 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
937 and then Is_Static_Subtype (Component_Type (Typ))
938 then
939 declare
940 Expr : Node_Id;
942 begin
943 Expr := First (Expressions (N));
944 while Present (Expr) loop
945 exit when Nkind (Expr) /= N_Character_Literal;
946 Next (Expr);
947 end loop;
949 if No (Expr) then
950 Start_String;
952 Expr := First (Expressions (N));
953 while Present (Expr) loop
954 Store_String_Char (UI_To_CC (Char_Literal_Value (Expr)));
955 Next (Expr);
956 end loop;
958 Rewrite (N,
959 Make_String_Literal (Sloc (N), End_String));
961 Analyze_And_Resolve (N, Typ);
962 return;
963 end if;
964 end;
965 end if;
967 -- Here if we have a real aggregate to deal with
969 Array_Aggregate : declare
970 Aggr_Resolved : Boolean;
972 Aggr_Typ : constant Entity_Id := Etype (Typ);
973 -- This is the unconstrained array type, which is the type
974 -- against which the aggregate is to be resolved. Typ itself
975 -- is the array type of the context which may not be the same
976 -- subtype as the subtype for the final aggregate.
978 begin
979 -- In the following we determine whether an others choice is
980 -- allowed inside the array aggregate. The test checks the context
981 -- in which the array aggregate occurs. If the context does not
982 -- permit it, or the aggregate type is unconstrained, an others
983 -- choice is not allowed.
985 -- If expansion is disabled (generic context, or semantics-only
986 -- mode) actual subtypes cannot be constructed, and the type of
987 -- an object may be its unconstrained nominal type. However, if
988 -- the context is an assignment, we assume that "others" is
989 -- allowed, because the target of the assignment will have a
990 -- constrained subtype when fully compiled.
992 -- Note that there is no node for Explicit_Actual_Parameter.
993 -- To test for this context we therefore have to test for node
994 -- N_Parameter_Association which itself appears only if there is a
995 -- formal parameter. Consequently we also need to test for
996 -- N_Procedure_Call_Statement or N_Function_Call.
998 Set_Etype (N, Aggr_Typ); -- may be overridden later on
1000 if Is_Constrained (Typ) and then
1001 (Pkind = N_Assignment_Statement or else
1002 Pkind = N_Parameter_Association or else
1003 Pkind = N_Function_Call or else
1004 Pkind = N_Procedure_Call_Statement or else
1005 Pkind = N_Generic_Association or else
1006 Pkind = N_Formal_Object_Declaration or else
1007 Pkind = N_Simple_Return_Statement or else
1008 Pkind = N_Object_Declaration or else
1009 Pkind = N_Component_Declaration or else
1010 Pkind = N_Parameter_Specification or else
1011 Pkind = N_Qualified_Expression or else
1012 Pkind = N_Aggregate or else
1013 Pkind = N_Extension_Aggregate or else
1014 Pkind = N_Component_Association)
1015 then
1016 Aggr_Resolved :=
1017 Resolve_Array_Aggregate
1019 Index => First_Index (Aggr_Typ),
1020 Index_Constr => First_Index (Typ),
1021 Component_Typ => Component_Type (Typ),
1022 Others_Allowed => True);
1024 elsif not Expander_Active
1025 and then Pkind = N_Assignment_Statement
1026 then
1027 Aggr_Resolved :=
1028 Resolve_Array_Aggregate
1030 Index => First_Index (Aggr_Typ),
1031 Index_Constr => First_Index (Typ),
1032 Component_Typ => Component_Type (Typ),
1033 Others_Allowed => True);
1034 else
1035 Aggr_Resolved :=
1036 Resolve_Array_Aggregate
1038 Index => First_Index (Aggr_Typ),
1039 Index_Constr => First_Index (Aggr_Typ),
1040 Component_Typ => Component_Type (Typ),
1041 Others_Allowed => False);
1042 end if;
1044 if not Aggr_Resolved then
1045 Aggr_Subtyp := Any_Composite;
1046 else
1047 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1048 end if;
1050 Set_Etype (N, Aggr_Subtyp);
1051 end Array_Aggregate;
1053 elsif Is_Private_Type (Typ)
1054 and then Present (Full_View (Typ))
1055 and then In_Inlined_Body
1056 and then Is_Composite_Type (Full_View (Typ))
1057 then
1058 Resolve (N, Full_View (Typ));
1060 else
1061 Error_Msg_N ("illegal context for aggregate", N);
1062 end if;
1064 -- If we can determine statically that the evaluation of the
1065 -- aggregate raises Constraint_Error, then replace the
1066 -- aggregate with an N_Raise_Constraint_Error node, but set the
1067 -- Etype to the right aggregate subtype. Gigi needs this.
1069 if Raises_Constraint_Error (N) then
1070 Aggr_Subtyp := Etype (N);
1071 Rewrite (N,
1072 Make_Raise_Constraint_Error (Sloc (N),
1073 Reason => CE_Range_Check_Failed));
1074 Set_Raises_Constraint_Error (N);
1075 Set_Etype (N, Aggr_Subtyp);
1076 Set_Analyzed (N);
1077 end if;
1078 end Resolve_Aggregate;
1080 -----------------------------
1081 -- Resolve_Array_Aggregate --
1082 -----------------------------
1084 function Resolve_Array_Aggregate
1085 (N : Node_Id;
1086 Index : Node_Id;
1087 Index_Constr : Node_Id;
1088 Component_Typ : Entity_Id;
1089 Others_Allowed : Boolean)
1090 return Boolean
1092 Loc : constant Source_Ptr := Sloc (N);
1094 Failure : constant Boolean := False;
1095 Success : constant Boolean := True;
1097 Index_Typ : constant Entity_Id := Etype (Index);
1098 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1099 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1100 -- The type of the index corresponding to the array sub-aggregate
1101 -- along with its low and upper bounds
1103 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1104 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1105 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1106 -- ditto for the base type
1108 function Add (Val : Uint; To : Node_Id) return Node_Id;
1109 -- Creates a new expression node where Val is added to expression To.
1110 -- Tries to constant fold whenever possible. To must be an already
1111 -- analyzed expression.
1113 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1114 -- Checks that AH (the upper bound of an array aggregate) is <= BH
1115 -- (the upper bound of the index base type). If the check fails a
1116 -- warning is emitted, the Raises_Constraint_Error Flag of N is set,
1117 -- and AH is replaced with a duplicate of BH.
1119 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1120 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1121 -- warning if not and sets the Raises_Constraint_Error Flag in N.
1123 procedure Check_Length (L, H : Node_Id; Len : Uint);
1124 -- Checks that range L .. H contains at least Len elements. Emits a
1125 -- warning if not and sets the Raises_Constraint_Error Flag in N.
1127 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1128 -- Returns True if range L .. H is dynamic or null
1130 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1131 -- Given expression node From, this routine sets OK to False if it
1132 -- cannot statically evaluate From. Otherwise it stores this static
1133 -- value into Value.
1135 function Resolve_Aggr_Expr
1136 (Expr : Node_Id;
1137 Single_Elmt : Boolean)
1138 return Boolean;
1139 -- Resolves aggregate expression Expr. Returs False if resolution
1140 -- fails. If Single_Elmt is set to False, the expression Expr may be
1141 -- used to initialize several array aggregate elements (this can
1142 -- happen for discrete choices such as "L .. H => Expr" or the others
1143 -- choice). In this event we do not resolve Expr unless expansion is
1144 -- disabled. To know why, see the DELAYED COMPONENT RESOLUTION
1145 -- note above.
1147 ---------
1148 -- Add --
1149 ---------
1151 function Add (Val : Uint; To : Node_Id) return Node_Id is
1152 Expr_Pos : Node_Id;
1153 Expr : Node_Id;
1154 To_Pos : Node_Id;
1156 begin
1157 if Raises_Constraint_Error (To) then
1158 return To;
1159 end if;
1161 -- First test if we can do constant folding
1163 if Compile_Time_Known_Value (To)
1164 or else Nkind (To) = N_Integer_Literal
1165 then
1166 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1167 Set_Is_Static_Expression (Expr_Pos);
1168 Set_Etype (Expr_Pos, Etype (To));
1169 Set_Analyzed (Expr_Pos, Analyzed (To));
1171 if not Is_Enumeration_Type (Index_Typ) then
1172 Expr := Expr_Pos;
1174 -- If we are dealing with enumeration return
1175 -- Index_Typ'Val (Expr_Pos)
1177 else
1178 Expr :=
1179 Make_Attribute_Reference
1180 (Loc,
1181 Prefix => New_Reference_To (Index_Typ, Loc),
1182 Attribute_Name => Name_Val,
1183 Expressions => New_List (Expr_Pos));
1184 end if;
1186 return Expr;
1187 end if;
1189 -- If we are here no constant folding possible
1191 if not Is_Enumeration_Type (Index_Base) then
1192 Expr :=
1193 Make_Op_Add (Loc,
1194 Left_Opnd => Duplicate_Subexpr (To),
1195 Right_Opnd => Make_Integer_Literal (Loc, Val));
1197 -- If we are dealing with enumeration return
1198 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1200 else
1201 To_Pos :=
1202 Make_Attribute_Reference
1203 (Loc,
1204 Prefix => New_Reference_To (Index_Typ, Loc),
1205 Attribute_Name => Name_Pos,
1206 Expressions => New_List (Duplicate_Subexpr (To)));
1208 Expr_Pos :=
1209 Make_Op_Add (Loc,
1210 Left_Opnd => To_Pos,
1211 Right_Opnd => Make_Integer_Literal (Loc, Val));
1213 Expr :=
1214 Make_Attribute_Reference
1215 (Loc,
1216 Prefix => New_Reference_To (Index_Typ, Loc),
1217 Attribute_Name => Name_Val,
1218 Expressions => New_List (Expr_Pos));
1219 end if;
1221 return Expr;
1222 end Add;
1224 -----------------
1225 -- Check_Bound --
1226 -----------------
1228 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1229 Val_BH : Uint;
1230 Val_AH : Uint;
1232 OK_BH : Boolean;
1233 OK_AH : Boolean;
1235 begin
1236 Get (Value => Val_BH, From => BH, OK => OK_BH);
1237 Get (Value => Val_AH, From => AH, OK => OK_AH);
1239 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1240 Set_Raises_Constraint_Error (N);
1241 Error_Msg_N ("upper bound out of range?", AH);
1242 Error_Msg_N ("\Constraint_Error will be raised at run-time?", AH);
1244 -- You need to set AH to BH or else in the case of enumerations
1245 -- indices we will not be able to resolve the aggregate bounds.
1247 AH := Duplicate_Subexpr (BH);
1248 end if;
1249 end Check_Bound;
1251 ------------------
1252 -- Check_Bounds --
1253 ------------------
1255 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1256 Val_L : Uint;
1257 Val_H : Uint;
1258 Val_AL : Uint;
1259 Val_AH : Uint;
1261 OK_L : Boolean;
1262 OK_H : Boolean;
1264 OK_AL : Boolean;
1265 OK_AH : Boolean;
1266 pragma Warnings (Off, OK_AL);
1267 pragma Warnings (Off, OK_AH);
1269 begin
1270 if Raises_Constraint_Error (N)
1271 or else Dynamic_Or_Null_Range (AL, AH)
1272 then
1273 return;
1274 end if;
1276 Get (Value => Val_L, From => L, OK => OK_L);
1277 Get (Value => Val_H, From => H, OK => OK_H);
1279 Get (Value => Val_AL, From => AL, OK => OK_AL);
1280 Get (Value => Val_AH, From => AH, OK => OK_AH);
1282 if OK_L and then Val_L > Val_AL then
1283 Set_Raises_Constraint_Error (N);
1284 Error_Msg_N ("lower bound of aggregate out of range?", N);
1285 Error_Msg_N ("\Constraint_Error will be raised at run-time?", N);
1286 end if;
1288 if OK_H and then Val_H < Val_AH then
1289 Set_Raises_Constraint_Error (N);
1290 Error_Msg_N ("upper bound of aggregate out of range?", N);
1291 Error_Msg_N ("\Constraint_Error will be raised at run-time?", N);
1292 end if;
1293 end Check_Bounds;
1295 ------------------
1296 -- Check_Length --
1297 ------------------
1299 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1300 Val_L : Uint;
1301 Val_H : Uint;
1303 OK_L : Boolean;
1304 OK_H : Boolean;
1306 Range_Len : Uint;
1308 begin
1309 if Raises_Constraint_Error (N) then
1310 return;
1311 end if;
1313 Get (Value => Val_L, From => L, OK => OK_L);
1314 Get (Value => Val_H, From => H, OK => OK_H);
1316 if not OK_L or else not OK_H then
1317 return;
1318 end if;
1320 -- If null range length is zero
1322 if Val_L > Val_H then
1323 Range_Len := Uint_0;
1324 else
1325 Range_Len := Val_H - Val_L + 1;
1326 end if;
1328 if Range_Len < Len then
1329 Set_Raises_Constraint_Error (N);
1330 Error_Msg_N ("too many elements?", N);
1331 Error_Msg_N ("\Constraint_Error will be raised at run-time?", N);
1332 end if;
1333 end Check_Length;
1335 ---------------------------
1336 -- Dynamic_Or_Null_Range --
1337 ---------------------------
1339 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1340 Val_L : Uint;
1341 Val_H : Uint;
1343 OK_L : Boolean;
1344 OK_H : Boolean;
1346 begin
1347 Get (Value => Val_L, From => L, OK => OK_L);
1348 Get (Value => Val_H, From => H, OK => OK_H);
1350 return not OK_L or else not OK_H
1351 or else not Is_OK_Static_Expression (L)
1352 or else not Is_OK_Static_Expression (H)
1353 or else Val_L > Val_H;
1354 end Dynamic_Or_Null_Range;
1356 ---------
1357 -- Get --
1358 ---------
1360 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1361 begin
1362 OK := True;
1364 if Compile_Time_Known_Value (From) then
1365 Value := Expr_Value (From);
1367 -- If expression From is something like Some_Type'Val (10) then
1368 -- Value = 10
1370 elsif Nkind (From) = N_Attribute_Reference
1371 and then Attribute_Name (From) = Name_Val
1372 and then Compile_Time_Known_Value (First (Expressions (From)))
1373 then
1374 Value := Expr_Value (First (Expressions (From)));
1376 else
1377 Value := Uint_0;
1378 OK := False;
1379 end if;
1380 end Get;
1382 -----------------------
1383 -- Resolve_Aggr_Expr --
1384 -----------------------
1386 function Resolve_Aggr_Expr
1387 (Expr : Node_Id;
1388 Single_Elmt : Boolean)
1389 return Boolean
1391 Nxt_Ind : constant Node_Id := Next_Index (Index);
1392 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
1393 -- Index is the current index corresponding to the expresion
1395 Resolution_OK : Boolean := True;
1396 -- Set to False if resolution of the expression failed
1398 begin
1399 -- If the array type against which we are resolving the aggregate
1400 -- has several dimensions, the expressions nested inside the
1401 -- aggregate must be further aggregates (or strings).
1403 if Present (Nxt_Ind) then
1404 if Nkind (Expr) /= N_Aggregate then
1406 -- A string literal can appear where a one-dimensional array
1407 -- of characters is expected. If the literal looks like an
1408 -- operator, it is still an operator symbol, which will be
1409 -- transformed into a string when analyzed.
1411 if Is_Character_Type (Component_Typ)
1412 and then No (Next_Index (Nxt_Ind))
1413 and then (Nkind (Expr) = N_String_Literal
1414 or else Nkind (Expr) = N_Operator_Symbol)
1415 then
1416 -- A string literal used in a multidimensional array
1417 -- aggregate in place of the final one-dimensional
1418 -- aggregate must not be enclosed in parentheses.
1420 if Paren_Count (Expr) /= 0 then
1421 Error_Msg_N ("no parenthesis allowed here", Expr);
1422 end if;
1424 Make_String_Into_Aggregate (Expr);
1426 else
1427 Error_Msg_N ("nested array aggregate expected", Expr);
1428 return Failure;
1429 end if;
1430 end if;
1432 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
1433 -- Required to check the null-exclusion attribute (if present).
1434 -- This value may be overridden later on.
1436 Set_Etype (Expr, Etype (N));
1438 Resolution_OK := Resolve_Array_Aggregate
1439 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1441 -- Do not resolve the expressions of discrete or others choices
1442 -- unless the expression covers a single component, or the expander
1443 -- is inactive.
1445 elsif Single_Elmt
1446 or else not Expander_Active
1447 or else In_Default_Expression
1448 then
1449 Analyze_And_Resolve (Expr, Component_Typ);
1450 Check_Non_Static_Context (Expr);
1451 Aggregate_Constraint_Checks (Expr, Component_Typ);
1452 Check_Unset_Reference (Expr);
1453 end if;
1455 if Raises_Constraint_Error (Expr)
1456 and then Nkind (Parent (Expr)) /= N_Component_Association
1457 then
1458 Set_Raises_Constraint_Error (N);
1459 end if;
1461 return Resolution_OK;
1462 end Resolve_Aggr_Expr;
1464 -- Variables local to Resolve_Array_Aggregate
1466 Assoc : Node_Id;
1467 Choice : Node_Id;
1468 Expr : Node_Id;
1470 Discard : Node_Id;
1471 pragma Warnings (Off, Discard);
1473 Aggr_Low : Node_Id := Empty;
1474 Aggr_High : Node_Id := Empty;
1475 -- The actual low and high bounds of this sub-aggregate
1477 Choices_Low : Node_Id := Empty;
1478 Choices_High : Node_Id := Empty;
1479 -- The lowest and highest discrete choices values for a named aggregate
1481 Nb_Elements : Uint := Uint_0;
1482 -- The number of elements in a positional aggregate
1484 Others_Present : Boolean := False;
1486 Nb_Choices : Nat := 0;
1487 -- Contains the overall number of named choices in this sub-aggregate
1489 Nb_Discrete_Choices : Nat := 0;
1490 -- The overall number of discrete choices (not counting others choice)
1492 Case_Table_Size : Nat;
1493 -- Contains the size of the case table needed to sort aggregate choices
1495 -- Start of processing for Resolve_Array_Aggregate
1497 begin
1498 -- STEP 1: make sure the aggregate is correctly formatted
1500 if Present (Component_Associations (N)) then
1501 Assoc := First (Component_Associations (N));
1502 while Present (Assoc) loop
1503 Choice := First (Choices (Assoc));
1504 while Present (Choice) loop
1505 if Nkind (Choice) = N_Others_Choice then
1506 Others_Present := True;
1508 if Choice /= First (Choices (Assoc))
1509 or else Present (Next (Choice))
1510 then
1511 Error_Msg_N
1512 ("OTHERS must appear alone in a choice list", Choice);
1513 return Failure;
1514 end if;
1516 if Present (Next (Assoc)) then
1517 Error_Msg_N
1518 ("OTHERS must appear last in an aggregate", Choice);
1519 return Failure;
1520 end if;
1522 if Ada_Version = Ada_83
1523 and then Assoc /= First (Component_Associations (N))
1524 and then (Nkind (Parent (N)) = N_Assignment_Statement
1525 or else
1526 Nkind (Parent (N)) = N_Object_Declaration)
1527 then
1528 Error_Msg_N
1529 ("(Ada 83) illegal context for OTHERS choice", N);
1530 end if;
1531 end if;
1533 Nb_Choices := Nb_Choices + 1;
1534 Next (Choice);
1535 end loop;
1537 Next (Assoc);
1538 end loop;
1539 end if;
1541 -- At this point we know that the others choice, if present, is by
1542 -- itself and appears last in the aggregate. Check if we have mixed
1543 -- positional and discrete associations (other than the others choice).
1545 if Present (Expressions (N))
1546 and then (Nb_Choices > 1
1547 or else (Nb_Choices = 1 and then not Others_Present))
1548 then
1549 Error_Msg_N
1550 ("named association cannot follow positional association",
1551 First (Choices (First (Component_Associations (N)))));
1552 return Failure;
1553 end if;
1555 -- Test for the validity of an others choice if present
1557 if Others_Present and then not Others_Allowed then
1558 Error_Msg_N
1559 ("OTHERS choice not allowed here",
1560 First (Choices (First (Component_Associations (N)))));
1561 return Failure;
1562 end if;
1564 -- Protect against cascaded errors
1566 if Etype (Index_Typ) = Any_Type then
1567 return Failure;
1568 end if;
1570 -- STEP 2: Process named components
1572 if No (Expressions (N)) then
1574 if Others_Present then
1575 Case_Table_Size := Nb_Choices - 1;
1576 else
1577 Case_Table_Size := Nb_Choices;
1578 end if;
1580 Step_2 : declare
1581 Low : Node_Id;
1582 High : Node_Id;
1583 -- Denote the lowest and highest values in an aggregate choice
1585 Hi_Val : Uint;
1586 Lo_Val : Uint;
1587 -- High end of one range and Low end of the next. Should be
1588 -- contiguous if there is no hole in the list of values.
1590 Missing_Values : Boolean;
1591 -- Set True if missing index values
1593 S_Low : Node_Id := Empty;
1594 S_High : Node_Id := Empty;
1595 -- if a choice in an aggregate is a subtype indication these
1596 -- denote the lowest and highest values of the subtype
1598 Table : Case_Table_Type (1 .. Case_Table_Size);
1599 -- Used to sort all the different choice values
1601 Single_Choice : Boolean;
1602 -- Set to true every time there is a single discrete choice in a
1603 -- discrete association
1605 Prev_Nb_Discrete_Choices : Nat;
1606 -- Used to keep track of the number of discrete choices
1607 -- in the current association.
1609 begin
1610 -- STEP 2 (A): Check discrete choices validity
1612 Assoc := First (Component_Associations (N));
1613 while Present (Assoc) loop
1614 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
1615 Choice := First (Choices (Assoc));
1616 loop
1617 Analyze (Choice);
1619 if Nkind (Choice) = N_Others_Choice then
1620 Single_Choice := False;
1621 exit;
1623 -- Test for subtype mark without constraint
1625 elsif Is_Entity_Name (Choice) and then
1626 Is_Type (Entity (Choice))
1627 then
1628 if Base_Type (Entity (Choice)) /= Index_Base then
1629 Error_Msg_N
1630 ("invalid subtype mark in aggregate choice",
1631 Choice);
1632 return Failure;
1633 end if;
1635 elsif Nkind (Choice) = N_Subtype_Indication then
1636 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
1638 -- Does the subtype indication evaluation raise CE ?
1640 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
1641 Get_Index_Bounds (Choice, Low, High);
1642 Check_Bounds (S_Low, S_High, Low, High);
1644 else -- Choice is a range or an expression
1645 Resolve (Choice, Index_Base);
1646 Check_Unset_Reference (Choice);
1647 Check_Non_Static_Context (Choice);
1649 -- Do not range check a choice. This check is redundant
1650 -- since this test is already performed when we check
1651 -- that the bounds of the array aggregate are within
1652 -- range.
1654 Set_Do_Range_Check (Choice, False);
1655 end if;
1657 -- If we could not resolve the discrete choice stop here
1659 if Etype (Choice) = Any_Type then
1660 return Failure;
1662 -- If the discrete choice raises CE get its original bounds
1664 elsif Nkind (Choice) = N_Raise_Constraint_Error then
1665 Set_Raises_Constraint_Error (N);
1666 Get_Index_Bounds (Original_Node (Choice), Low, High);
1668 -- Otherwise get its bounds as usual
1670 else
1671 Get_Index_Bounds (Choice, Low, High);
1672 end if;
1674 if (Dynamic_Or_Null_Range (Low, High)
1675 or else (Nkind (Choice) = N_Subtype_Indication
1676 and then
1677 Dynamic_Or_Null_Range (S_Low, S_High)))
1678 and then Nb_Choices /= 1
1679 then
1680 Error_Msg_N
1681 ("dynamic or empty choice in aggregate " &
1682 "must be the only choice", Choice);
1683 return Failure;
1684 end if;
1686 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
1687 Table (Nb_Discrete_Choices).Choice_Lo := Low;
1688 Table (Nb_Discrete_Choices).Choice_Hi := High;
1690 Next (Choice);
1692 if No (Choice) then
1694 -- Check if we have a single discrete choice and whether
1695 -- this discrete choice specifies a single value.
1697 Single_Choice :=
1698 (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1)
1699 and then (Low = High);
1701 exit;
1702 end if;
1703 end loop;
1705 -- Ada 2005 (AI-231)
1707 if Ada_Version >= Ada_05
1708 and then Known_Null (Expression (Assoc))
1709 then
1710 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
1711 end if;
1713 -- Ada 2005 (AI-287): In case of default initialized component
1714 -- we delay the resolution to the expansion phase
1716 if Box_Present (Assoc) then
1718 -- Ada 2005 (AI-287): In case of default initialization
1719 -- of a component the expander will generate calls to
1720 -- the corresponding initialization subprogram.
1722 null;
1724 elsif not Resolve_Aggr_Expr (Expression (Assoc),
1725 Single_Elmt => Single_Choice)
1726 then
1727 return Failure;
1728 end if;
1730 Next (Assoc);
1731 end loop;
1733 -- If aggregate contains more than one choice then these must be
1734 -- static. Sort them and check that they are contiguous
1736 if Nb_Discrete_Choices > 1 then
1737 Sort_Case_Table (Table);
1738 Missing_Values := False;
1740 Outer : for J in 1 .. Nb_Discrete_Choices - 1 loop
1741 if Expr_Value (Table (J).Choice_Hi) >=
1742 Expr_Value (Table (J + 1).Choice_Lo)
1743 then
1744 Error_Msg_N
1745 ("duplicate choice values in array aggregate",
1746 Table (J).Choice_Hi);
1747 return Failure;
1749 elsif not Others_Present then
1751 Hi_Val := Expr_Value (Table (J).Choice_Hi);
1752 Lo_Val := Expr_Value (Table (J + 1).Choice_Lo);
1754 -- If missing values, output error messages
1756 if Lo_Val - Hi_Val > 1 then
1758 -- Header message if not first missing value
1760 if not Missing_Values then
1761 Error_Msg_N
1762 ("missing index value(s) in array aggregate", N);
1763 Missing_Values := True;
1764 end if;
1766 -- Output values of missing indexes
1768 Lo_Val := Lo_Val - 1;
1769 Hi_Val := Hi_Val + 1;
1771 -- Enumeration type case
1773 if Is_Enumeration_Type (Index_Typ) then
1774 Error_Msg_Name_1 :=
1775 Chars
1776 (Get_Enum_Lit_From_Pos
1777 (Index_Typ, Hi_Val, Loc));
1779 if Lo_Val = Hi_Val then
1780 Error_Msg_N ("\ %", N);
1781 else
1782 Error_Msg_Name_2 :=
1783 Chars
1784 (Get_Enum_Lit_From_Pos
1785 (Index_Typ, Lo_Val, Loc));
1786 Error_Msg_N ("\ % .. %", N);
1787 end if;
1789 -- Integer types case
1791 else
1792 Error_Msg_Uint_1 := Hi_Val;
1794 if Lo_Val = Hi_Val then
1795 Error_Msg_N ("\ ^", N);
1796 else
1797 Error_Msg_Uint_2 := Lo_Val;
1798 Error_Msg_N ("\ ^ .. ^", N);
1799 end if;
1800 end if;
1801 end if;
1802 end if;
1803 end loop Outer;
1805 if Missing_Values then
1806 Set_Etype (N, Any_Composite);
1807 return Failure;
1808 end if;
1809 end if;
1811 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
1813 if Nb_Discrete_Choices > 0 then
1814 Choices_Low := Table (1).Choice_Lo;
1815 Choices_High := Table (Nb_Discrete_Choices).Choice_Hi;
1816 end if;
1818 if Others_Present then
1819 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
1821 else
1822 Aggr_Low := Choices_Low;
1823 Aggr_High := Choices_High;
1824 end if;
1825 end Step_2;
1827 -- STEP 3: Process positional components
1829 else
1830 -- STEP 3 (A): Process positional elements
1832 Expr := First (Expressions (N));
1833 Nb_Elements := Uint_0;
1834 while Present (Expr) loop
1835 Nb_Elements := Nb_Elements + 1;
1837 -- Ada 2005 (AI-231)
1839 if Ada_Version >= Ada_05
1840 and then Known_Null (Expr)
1841 then
1842 Check_Can_Never_Be_Null (Etype (N), Expr);
1843 end if;
1845 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
1846 return Failure;
1847 end if;
1849 Next (Expr);
1850 end loop;
1852 if Others_Present then
1853 Assoc := Last (Component_Associations (N));
1855 -- Ada 2005 (AI-231)
1857 if Ada_Version >= Ada_05
1858 and then Known_Null (Assoc)
1859 then
1860 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
1861 end if;
1863 -- Ada 2005 (AI-287): In case of default initialized component
1864 -- we delay the resolution to the expansion phase.
1866 if Box_Present (Assoc) then
1868 -- Ada 2005 (AI-287): In case of default initialization
1869 -- of a component the expander will generate calls to
1870 -- the corresponding initialization subprogram.
1872 null;
1874 elsif not Resolve_Aggr_Expr (Expression (Assoc),
1875 Single_Elmt => False)
1876 then
1877 return Failure;
1878 end if;
1879 end if;
1881 -- STEP 3 (B): Compute the aggregate bounds
1883 if Others_Present then
1884 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
1886 else
1887 if Others_Allowed then
1888 Get_Index_Bounds (Index_Constr, Aggr_Low, Discard);
1889 else
1890 Aggr_Low := Index_Typ_Low;
1891 end if;
1893 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
1894 Check_Bound (Index_Base_High, Aggr_High);
1895 end if;
1896 end if;
1898 -- STEP 4: Perform static aggregate checks and save the bounds
1900 -- Check (A)
1902 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
1903 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
1905 -- Check (B)
1907 if Others_Present and then Nb_Discrete_Choices > 0 then
1908 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
1909 Check_Bounds (Index_Typ_Low, Index_Typ_High,
1910 Choices_Low, Choices_High);
1911 Check_Bounds (Index_Base_Low, Index_Base_High,
1912 Choices_Low, Choices_High);
1914 -- Check (C)
1916 elsif Others_Present and then Nb_Elements > 0 then
1917 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
1918 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
1919 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
1920 end if;
1922 if Raises_Constraint_Error (Aggr_Low)
1923 or else Raises_Constraint_Error (Aggr_High)
1924 then
1925 Set_Raises_Constraint_Error (N);
1926 end if;
1928 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
1930 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
1931 -- since the addition node returned by Add is not yet analyzed. Attach
1932 -- to tree and analyze first. Reset analyzed flag to insure it will get
1933 -- analyzed when it is a literal bound whose type must be properly set.
1935 if Others_Present or else Nb_Discrete_Choices > 0 then
1936 Aggr_High := Duplicate_Subexpr (Aggr_High);
1938 if Etype (Aggr_High) = Universal_Integer then
1939 Set_Analyzed (Aggr_High, False);
1940 end if;
1941 end if;
1943 Set_Aggregate_Bounds
1944 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
1946 -- The bounds may contain expressions that must be inserted upwards.
1947 -- Attach them fully to the tree. After analysis, remove side effects
1948 -- from upper bound, if still needed.
1950 Set_Parent (Aggregate_Bounds (N), N);
1951 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
1952 Check_Unset_Reference (Aggregate_Bounds (N));
1954 if not Others_Present and then Nb_Discrete_Choices = 0 then
1955 Set_High_Bound (Aggregate_Bounds (N),
1956 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
1957 end if;
1959 return Success;
1960 end Resolve_Array_Aggregate;
1962 ---------------------------------
1963 -- Resolve_Extension_Aggregate --
1964 ---------------------------------
1966 -- There are two cases to consider:
1968 -- a) If the ancestor part is a type mark, the components needed are
1969 -- the difference between the components of the expected type and the
1970 -- components of the given type mark.
1972 -- b) If the ancestor part is an expression, it must be unambiguous,
1973 -- and once we have its type we can also compute the needed components
1974 -- as in the previous case. In both cases, if the ancestor type is not
1975 -- the immediate ancestor, we have to build this ancestor recursively.
1977 -- In both cases discriminants of the ancestor type do not play a
1978 -- role in the resolution of the needed components, because inherited
1979 -- discriminants cannot be used in a type extension. As a result we can
1980 -- compute independently the list of components of the ancestor type and
1981 -- of the expected type.
1983 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
1984 A : constant Node_Id := Ancestor_Part (N);
1985 A_Type : Entity_Id;
1986 I : Interp_Index;
1987 It : Interp;
1989 function Valid_Ancestor_Type return Boolean;
1990 -- Verify that the type of the ancestor part is a non-private ancestor
1991 -- of the expected type.
1993 -------------------------
1994 -- Valid_Ancestor_Type --
1995 -------------------------
1997 function Valid_Ancestor_Type return Boolean is
1998 Imm_Type : Entity_Id;
2000 begin
2001 Imm_Type := Base_Type (Typ);
2002 while Is_Derived_Type (Imm_Type)
2003 and then Etype (Imm_Type) /= Base_Type (A_Type)
2004 loop
2005 Imm_Type := Etype (Base_Type (Imm_Type));
2006 end loop;
2008 if Etype (Imm_Type) /= Base_Type (A_Type) then
2009 Error_Msg_NE ("expect ancestor type of &", A, Typ);
2010 return False;
2011 else
2012 return True;
2013 end if;
2014 end Valid_Ancestor_Type;
2016 -- Start of processing for Resolve_Extension_Aggregate
2018 begin
2019 Analyze (A);
2021 if not Is_Tagged_Type (Typ) then
2022 Error_Msg_N ("type of extension aggregate must be tagged", N);
2023 return;
2025 elsif Is_Limited_Type (Typ) then
2027 -- Ada 2005 (AI-287): Limited aggregates are allowed
2029 if Ada_Version < Ada_05 then
2030 Error_Msg_N ("aggregate type cannot be limited", N);
2031 Explain_Limited_Type (Typ, N);
2032 return;
2033 end if;
2035 elsif Is_Class_Wide_Type (Typ) then
2036 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
2037 return;
2038 end if;
2040 if Is_Entity_Name (A)
2041 and then Is_Type (Entity (A))
2042 then
2043 A_Type := Get_Full_View (Entity (A));
2045 if Valid_Ancestor_Type then
2046 Set_Entity (A, A_Type);
2047 Set_Etype (A, A_Type);
2049 Validate_Ancestor_Part (N);
2050 Resolve_Record_Aggregate (N, Typ);
2051 end if;
2053 elsif Nkind (A) /= N_Aggregate then
2054 if Is_Overloaded (A) then
2055 A_Type := Any_Type;
2057 Get_First_Interp (A, I, It);
2058 while Present (It.Typ) loop
2059 if Is_Tagged_Type (It.Typ)
2060 and then not Is_Limited_Type (It.Typ)
2061 then
2062 if A_Type /= Any_Type then
2063 Error_Msg_N ("cannot resolve expression", A);
2064 return;
2065 else
2066 A_Type := It.Typ;
2067 end if;
2068 end if;
2070 Get_Next_Interp (I, It);
2071 end loop;
2073 if A_Type = Any_Type then
2074 Error_Msg_N
2075 ("ancestor part must be non-limited tagged type", A);
2076 return;
2077 end if;
2079 else
2080 A_Type := Etype (A);
2081 end if;
2083 if Valid_Ancestor_Type then
2084 Resolve (A, A_Type);
2085 Check_Unset_Reference (A);
2086 Check_Non_Static_Context (A);
2088 if Is_Class_Wide_Type (Etype (A))
2089 and then Nkind (Original_Node (A)) = N_Function_Call
2090 then
2091 -- If the ancestor part is a dispatching call, it appears
2092 -- statically to be a legal ancestor, but it yields any
2093 -- member of the class, and it is not possible to determine
2094 -- whether it is an ancestor of the extension aggregate (much
2095 -- less which ancestor). It is not possible to determine the
2096 -- required components of the extension part.
2098 -- This check implements AI-306, which in fact was motivated
2099 -- by an ACT query to the ARG after this test was added.
2101 Error_Msg_N ("ancestor part must be statically tagged", A);
2102 else
2103 Resolve_Record_Aggregate (N, Typ);
2104 end if;
2105 end if;
2107 else
2108 Error_Msg_N ("no unique type for this aggregate", A);
2109 end if;
2110 end Resolve_Extension_Aggregate;
2112 ------------------------------
2113 -- Resolve_Record_Aggregate --
2114 ------------------------------
2116 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
2117 Assoc : Node_Id;
2118 -- N_Component_Association node belonging to the input aggregate N
2120 Expr : Node_Id;
2121 Positional_Expr : Node_Id;
2122 Component : Entity_Id;
2123 Component_Elmt : Elmt_Id;
2125 Components : constant Elist_Id := New_Elmt_List;
2126 -- Components is the list of the record components whose value must
2127 -- be provided in the aggregate. This list does include discriminants.
2129 New_Assoc_List : constant List_Id := New_List;
2130 New_Assoc : Node_Id;
2131 -- New_Assoc_List is the newly built list of N_Component_Association
2132 -- nodes. New_Assoc is one such N_Component_Association node in it.
2133 -- Please note that while Assoc and New_Assoc contain the same
2134 -- kind of nodes, they are used to iterate over two different
2135 -- N_Component_Association lists.
2137 Others_Etype : Entity_Id := Empty;
2138 -- This variable is used to save the Etype of the last record component
2139 -- that takes its value from the others choice. Its purpose is:
2141 -- (a) make sure the others choice is useful
2143 -- (b) make sure the type of all the components whose value is
2144 -- subsumed by the others choice are the same.
2146 -- This variable is updated as a side effect of function Get_Value
2148 Is_Box_Present : Boolean := False;
2149 Others_Box : Boolean := False;
2150 -- Ada 2005 (AI-287): Variables used in case of default initialization
2151 -- to provide a functionality similar to Others_Etype. Box_Present
2152 -- indicates that the component takes its default initialization;
2153 -- Others_Box indicates that at least one component takes its default
2154 -- initialization. Similar to Others_Etype, they are also updated as a
2155 -- side effect of function Get_Value.
2157 procedure Add_Association
2158 (Component : Entity_Id;
2159 Expr : Node_Id;
2160 Is_Box_Present : Boolean := False);
2161 -- Builds a new N_Component_Association node which associates
2162 -- Component to expression Expr and adds it to the new association
2163 -- list New_Assoc_List being built.
2165 function Discr_Present (Discr : Entity_Id) return Boolean;
2166 -- If aggregate N is a regular aggregate this routine will return True.
2167 -- Otherwise, if N is an extension aggregate, Discr is a discriminant
2168 -- whose value may already have been specified by N's ancestor part,
2169 -- this routine checks whether this is indeed the case and if so
2170 -- returns False, signaling that no value for Discr should appear in the
2171 -- N's aggregate part. Also, in this case, the routine appends to
2172 -- New_Assoc_List Discr the discriminant value specified in the ancestor
2173 -- part.
2175 function Get_Value
2176 (Compon : Node_Id;
2177 From : List_Id;
2178 Consider_Others_Choice : Boolean := False)
2179 return Node_Id;
2180 -- Given a record component stored in parameter Compon, the
2181 -- following function returns its value as it appears in the list
2182 -- From, which is a list of N_Component_Association nodes. If no
2183 -- component association has a choice for the searched component,
2184 -- the value provided by the others choice is returned, if there
2185 -- is one and Consider_Others_Choice is set to true. Otherwise
2186 -- Empty is returned. If there is more than one component association
2187 -- giving a value for the searched record component, an error message
2188 -- is emitted and the first found value is returned.
2190 -- If Consider_Others_Choice is set and the returned expression comes
2191 -- from the others choice, then Others_Etype is set as a side effect.
2192 -- An error message is emitted if the components taking their value
2193 -- from the others choice do not have same type.
2195 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id);
2196 -- Analyzes and resolves expression Expr against the Etype of the
2197 -- Component. This routine also applies all appropriate checks to Expr.
2198 -- It finally saves a Expr in the newly created association list that
2199 -- will be attached to the final record aggregate. Note that if the
2200 -- Parent pointer of Expr is not set then Expr was produced with a
2201 -- New_Copy_Tree or some such.
2203 ---------------------
2204 -- Add_Association --
2205 ---------------------
2207 procedure Add_Association
2208 (Component : Entity_Id;
2209 Expr : Node_Id;
2210 Is_Box_Present : Boolean := False)
2212 Choice_List : constant List_Id := New_List;
2213 New_Assoc : Node_Id;
2215 begin
2216 Append (New_Occurrence_Of (Component, Sloc (Expr)), Choice_List);
2217 New_Assoc :=
2218 Make_Component_Association (Sloc (Expr),
2219 Choices => Choice_List,
2220 Expression => Expr,
2221 Box_Present => Is_Box_Present);
2222 Append (New_Assoc, New_Assoc_List);
2223 end Add_Association;
2225 -------------------
2226 -- Discr_Present --
2227 -------------------
2229 function Discr_Present (Discr : Entity_Id) return Boolean is
2230 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
2232 Loc : Source_Ptr;
2234 Ancestor : Node_Id;
2235 Discr_Expr : Node_Id;
2237 Ancestor_Typ : Entity_Id;
2238 Orig_Discr : Entity_Id;
2239 D : Entity_Id;
2240 D_Val : Elmt_Id := No_Elmt; -- stop junk warning
2242 Ancestor_Is_Subtyp : Boolean;
2244 begin
2245 if Regular_Aggr then
2246 return True;
2247 end if;
2249 Ancestor := Ancestor_Part (N);
2250 Ancestor_Typ := Etype (Ancestor);
2251 Loc := Sloc (Ancestor);
2253 Ancestor_Is_Subtyp :=
2254 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
2256 -- If the ancestor part has no discriminants clearly N's aggregate
2257 -- part must provide a value for Discr.
2259 if not Has_Discriminants (Ancestor_Typ) then
2260 return True;
2262 -- If the ancestor part is an unconstrained subtype mark then the
2263 -- Discr must be present in N's aggregate part.
2265 elsif Ancestor_Is_Subtyp
2266 and then not Is_Constrained (Entity (Ancestor))
2267 then
2268 return True;
2269 end if;
2271 -- Now look to see if Discr was specified in the ancestor part
2273 if Ancestor_Is_Subtyp then
2274 D_Val := First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
2275 end if;
2277 Orig_Discr := Original_Record_Component (Discr);
2279 D := First_Discriminant (Ancestor_Typ);
2280 while Present (D) loop
2282 -- If Ancestor has already specified Disc value than insert its
2283 -- value in the final aggregate.
2285 if Original_Record_Component (D) = Orig_Discr then
2286 if Ancestor_Is_Subtyp then
2287 Discr_Expr := New_Copy_Tree (Node (D_Val));
2288 else
2289 Discr_Expr :=
2290 Make_Selected_Component (Loc,
2291 Prefix => Duplicate_Subexpr (Ancestor),
2292 Selector_Name => New_Occurrence_Of (Discr, Loc));
2293 end if;
2295 Resolve_Aggr_Expr (Discr_Expr, Discr);
2296 return False;
2297 end if;
2299 Next_Discriminant (D);
2301 if Ancestor_Is_Subtyp then
2302 Next_Elmt (D_Val);
2303 end if;
2304 end loop;
2306 return True;
2307 end Discr_Present;
2309 ---------------
2310 -- Get_Value --
2311 ---------------
2313 function Get_Value
2314 (Compon : Node_Id;
2315 From : List_Id;
2316 Consider_Others_Choice : Boolean := False)
2317 return Node_Id
2319 Assoc : Node_Id;
2320 Expr : Node_Id := Empty;
2321 Selector_Name : Node_Id;
2323 begin
2324 Is_Box_Present := False;
2326 if Present (From) then
2327 Assoc := First (From);
2328 else
2329 return Empty;
2330 end if;
2332 while Present (Assoc) loop
2333 Selector_Name := First (Choices (Assoc));
2334 while Present (Selector_Name) loop
2335 if Nkind (Selector_Name) = N_Others_Choice then
2336 if Consider_Others_Choice and then No (Expr) then
2338 -- We need to duplicate the expression for each
2339 -- successive component covered by the others choice.
2340 -- This is redundant if the others_choice covers only
2341 -- one component (small optimization possible???), but
2342 -- indispensable otherwise, because each one must be
2343 -- expanded individually to preserve side-effects.
2345 -- Ada 2005 (AI-287): In case of default initialization
2346 -- of components, we duplicate the corresponding default
2347 -- expression (from the record type declaration). The
2348 -- copy must carry the sloc of the association (not the
2349 -- original expression) to prevent spurious elaboration
2350 -- checks when the default includes function calls.
2352 if Box_Present (Assoc) then
2353 Others_Box := True;
2354 Is_Box_Present := True;
2356 if Expander_Active then
2357 return
2358 New_Copy_Tree
2359 (Expression (Parent (Compon)),
2360 New_Sloc => Sloc (Assoc));
2361 else
2362 return Expression (Parent (Compon));
2363 end if;
2365 else
2366 if Present (Others_Etype) and then
2367 Base_Type (Others_Etype) /= Base_Type (Etype
2368 (Compon))
2369 then
2370 Error_Msg_N ("components in OTHERS choice must " &
2371 "have same type", Selector_Name);
2372 end if;
2374 Others_Etype := Etype (Compon);
2376 if Expander_Active then
2377 return New_Copy_Tree (Expression (Assoc));
2378 else
2379 return Expression (Assoc);
2380 end if;
2381 end if;
2382 end if;
2384 elsif Chars (Compon) = Chars (Selector_Name) then
2385 if No (Expr) then
2387 -- Ada 2005 (AI-231)
2389 if Ada_Version >= Ada_05
2390 and then Known_Null (Expression (Assoc))
2391 then
2392 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
2393 end if;
2395 -- We need to duplicate the expression when several
2396 -- components are grouped together with a "|" choice.
2397 -- For instance "filed1 | filed2 => Expr"
2399 -- Ada 2005 (AI-287)
2401 if Box_Present (Assoc) then
2402 Is_Box_Present := True;
2404 -- Duplicate the default expression of the component
2405 -- from the record type declaration, so a new copy
2406 -- can be attached to the association.
2408 -- Note that we always copy the default expression,
2409 -- even when the association has a single choice, in
2410 -- order to create a proper association for the
2411 -- expanded aggregate.
2413 Expr := New_Copy_Tree (Expression (Parent (Compon)));
2415 else
2416 if Present (Next (Selector_Name)) then
2417 Expr := New_Copy_Tree (Expression (Assoc));
2418 else
2419 Expr := Expression (Assoc);
2420 end if;
2421 end if;
2423 Generate_Reference (Compon, Selector_Name);
2425 else
2426 Error_Msg_NE
2427 ("more than one value supplied for &",
2428 Selector_Name, Compon);
2430 end if;
2431 end if;
2433 Next (Selector_Name);
2434 end loop;
2436 Next (Assoc);
2437 end loop;
2439 return Expr;
2440 end Get_Value;
2442 procedure Check_Non_Limited_Type (Expr : Node_Id);
2443 -- Relax check to allow the default initialization of limited types.
2444 -- For example:
2445 -- record
2446 -- C : Lim := (..., others => <>);
2447 -- end record;
2449 ----------------------------
2450 -- Check_Non_Limited_Type --
2451 ----------------------------
2453 procedure Check_Non_Limited_Type (Expr : Node_Id) is
2454 begin
2455 if Is_Limited_Type (Etype (Expr))
2456 and then Comes_From_Source (Expr)
2457 and then not In_Instance_Body
2458 then
2459 if not OK_For_Limited_Init (Expr) then
2460 Error_Msg_N
2461 ("initialization not allowed for limited types", N);
2462 Explain_Limited_Type (Etype (Expr), Expr);
2463 end if;
2464 end if;
2465 end Check_Non_Limited_Type;
2467 -----------------------
2468 -- Resolve_Aggr_Expr --
2469 -----------------------
2471 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id) is
2472 New_C : Entity_Id := Component;
2473 Expr_Type : Entity_Id := Empty;
2475 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
2476 -- If the expression is an aggregate (possibly qualified) then its
2477 -- expansion is delayed until the enclosing aggregate is expanded
2478 -- into assignments. In that case, do not generate checks on the
2479 -- expression, because they will be generated later, and will other-
2480 -- wise force a copy (to remove side-effects) that would leave a
2481 -- dynamic-sized aggregate in the code, something that gigi cannot
2482 -- handle.
2484 Relocate : Boolean;
2485 -- Set to True if the resolved Expr node needs to be relocated
2486 -- when attached to the newly created association list. This node
2487 -- need not be relocated if its parent pointer is not set.
2488 -- In fact in this case Expr is the output of a New_Copy_Tree call.
2489 -- if Relocate is True then we have analyzed the expression node
2490 -- in the original aggregate and hence it needs to be relocated
2491 -- when moved over the new association list.
2493 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
2494 Kind : constant Node_Kind := Nkind (Expr);
2496 begin
2497 return ((Kind = N_Aggregate
2498 or else Kind = N_Extension_Aggregate)
2499 and then Present (Etype (Expr))
2500 and then Is_Record_Type (Etype (Expr))
2501 and then Expansion_Delayed (Expr))
2503 or else (Kind = N_Qualified_Expression
2504 and then Has_Expansion_Delayed (Expression (Expr)));
2505 end Has_Expansion_Delayed;
2507 -- Start of processing for Resolve_Aggr_Expr
2509 begin
2510 -- If the type of the component is elementary or the type of the
2511 -- aggregate does not contain discriminants, use the type of the
2512 -- component to resolve Expr.
2514 if Is_Elementary_Type (Etype (Component))
2515 or else not Has_Discriminants (Etype (N))
2516 then
2517 Expr_Type := Etype (Component);
2519 -- Otherwise we have to pick up the new type of the component from
2520 -- the new costrained subtype of the aggregate. In fact components
2521 -- which are of a composite type might be constrained by a
2522 -- discriminant, and we want to resolve Expr against the subtype were
2523 -- all discriminant occurrences are replaced with their actual value.
2525 else
2526 New_C := First_Component (Etype (N));
2527 while Present (New_C) loop
2528 if Chars (New_C) = Chars (Component) then
2529 Expr_Type := Etype (New_C);
2530 exit;
2531 end if;
2533 Next_Component (New_C);
2534 end loop;
2536 pragma Assert (Present (Expr_Type));
2538 -- For each range in an array type where a discriminant has been
2539 -- replaced with the constraint, check that this range is within
2540 -- the range of the base type. This checks is done in the init
2541 -- proc for regular objects, but has to be done here for
2542 -- aggregates since no init proc is called for them.
2544 if Is_Array_Type (Expr_Type) then
2545 declare
2546 Index : Node_Id;
2547 -- Range of the current constrained index in the array
2549 Orig_Index : Node_Id := First_Index (Etype (Component));
2550 -- Range corresponding to the range Index above in the
2551 -- original unconstrained record type. The bounds of this
2552 -- range may be governed by discriminants.
2554 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
2555 -- Range corresponding to the range Index above for the
2556 -- unconstrained array type. This range is needed to apply
2557 -- range checks.
2559 begin
2560 Index := First_Index (Expr_Type);
2561 while Present (Index) loop
2562 if Depends_On_Discriminant (Orig_Index) then
2563 Apply_Range_Check (Index, Etype (Unconstr_Index));
2564 end if;
2566 Next_Index (Index);
2567 Next_Index (Orig_Index);
2568 Next_Index (Unconstr_Index);
2569 end loop;
2570 end;
2571 end if;
2572 end if;
2574 -- If the Parent pointer of Expr is not set, Expr is an expression
2575 -- duplicated by New_Tree_Copy (this happens for record aggregates
2576 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
2577 -- Such a duplicated expression must be attached to the tree
2578 -- before analysis and resolution to enforce the rule that a tree
2579 -- fragment should never be analyzed or resolved unless it is
2580 -- attached to the current compilation unit.
2582 if No (Parent (Expr)) then
2583 Set_Parent (Expr, N);
2584 Relocate := False;
2585 else
2586 Relocate := True;
2587 end if;
2589 Analyze_And_Resolve (Expr, Expr_Type);
2590 Check_Non_Limited_Type (Expr);
2591 Check_Non_Static_Context (Expr);
2592 Check_Unset_Reference (Expr);
2594 if not Has_Expansion_Delayed (Expr) then
2595 Aggregate_Constraint_Checks (Expr, Expr_Type);
2596 end if;
2598 if Raises_Constraint_Error (Expr) then
2599 Set_Raises_Constraint_Error (N);
2600 end if;
2602 if Relocate then
2603 Add_Association (New_C, Relocate_Node (Expr));
2604 else
2605 Add_Association (New_C, Expr);
2606 end if;
2607 end Resolve_Aggr_Expr;
2609 -- Start of processing for Resolve_Record_Aggregate
2611 begin
2612 -- We may end up calling Duplicate_Subexpr on expressions that are
2613 -- attached to New_Assoc_List. For this reason we need to attach it
2614 -- to the tree by setting its parent pointer to N. This parent point
2615 -- will change in STEP 8 below.
2617 Set_Parent (New_Assoc_List, N);
2619 -- STEP 1: abstract type and null record verification
2621 if Is_Abstract_Type (Typ) then
2622 Error_Msg_N ("type of aggregate cannot be abstract", N);
2623 end if;
2625 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
2626 Set_Etype (N, Typ);
2627 return;
2629 elsif Present (First_Entity (Typ))
2630 and then Null_Record_Present (N)
2631 and then not Is_Tagged_Type (Typ)
2632 then
2633 Error_Msg_N ("record aggregate cannot be null", N);
2634 return;
2636 elsif No (First_Entity (Typ)) then
2637 Error_Msg_N ("record aggregate must be null", N);
2638 return;
2639 end if;
2641 -- STEP 2: Verify aggregate structure
2643 Step_2 : declare
2644 Selector_Name : Node_Id;
2645 Bad_Aggregate : Boolean := False;
2647 begin
2648 if Present (Component_Associations (N)) then
2649 Assoc := First (Component_Associations (N));
2650 else
2651 Assoc := Empty;
2652 end if;
2654 while Present (Assoc) loop
2655 Selector_Name := First (Choices (Assoc));
2656 while Present (Selector_Name) loop
2657 if Nkind (Selector_Name) = N_Identifier then
2658 null;
2660 elsif Nkind (Selector_Name) = N_Others_Choice then
2661 if Selector_Name /= First (Choices (Assoc))
2662 or else Present (Next (Selector_Name))
2663 then
2664 Error_Msg_N ("OTHERS must appear alone in a choice list",
2665 Selector_Name);
2666 return;
2668 elsif Present (Next (Assoc)) then
2669 Error_Msg_N ("OTHERS must appear last in an aggregate",
2670 Selector_Name);
2671 return;
2673 -- (Ada2005): If this is an association with a box,
2674 -- indicate that the association need not represent
2675 -- any component.
2677 elsif Box_Present (Assoc) then
2678 Others_Box := True;
2679 end if;
2681 else
2682 Error_Msg_N
2683 ("selector name should be identifier or OTHERS",
2684 Selector_Name);
2685 Bad_Aggregate := True;
2686 end if;
2688 Next (Selector_Name);
2689 end loop;
2691 Next (Assoc);
2692 end loop;
2694 if Bad_Aggregate then
2695 return;
2696 end if;
2697 end Step_2;
2699 -- STEP 3: Find discriminant Values
2701 Step_3 : declare
2702 Discrim : Entity_Id;
2703 Missing_Discriminants : Boolean := False;
2705 begin
2706 if Present (Expressions (N)) then
2707 Positional_Expr := First (Expressions (N));
2708 else
2709 Positional_Expr := Empty;
2710 end if;
2712 if Has_Discriminants (Typ) then
2713 Discrim := First_Discriminant (Typ);
2714 else
2715 Discrim := Empty;
2716 end if;
2718 -- First find the discriminant values in the positional components
2720 while Present (Discrim) and then Present (Positional_Expr) loop
2721 if Discr_Present (Discrim) then
2722 Resolve_Aggr_Expr (Positional_Expr, Discrim);
2724 -- Ada 2005 (AI-231)
2726 if Ada_Version >= Ada_05
2727 and then Known_Null (Positional_Expr)
2728 then
2729 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
2730 end if;
2732 Next (Positional_Expr);
2733 end if;
2735 if Present (Get_Value (Discrim, Component_Associations (N))) then
2736 Error_Msg_NE
2737 ("more than one value supplied for discriminant&",
2738 N, Discrim);
2739 end if;
2741 Next_Discriminant (Discrim);
2742 end loop;
2744 -- Find remaining discriminant values, if any, among named components
2746 while Present (Discrim) loop
2747 Expr := Get_Value (Discrim, Component_Associations (N), True);
2749 if not Discr_Present (Discrim) then
2750 if Present (Expr) then
2751 Error_Msg_NE
2752 ("more than one value supplied for discriminant&",
2753 N, Discrim);
2754 end if;
2756 elsif No (Expr) then
2757 Error_Msg_NE
2758 ("no value supplied for discriminant &", N, Discrim);
2759 Missing_Discriminants := True;
2761 else
2762 Resolve_Aggr_Expr (Expr, Discrim);
2763 end if;
2765 Next_Discriminant (Discrim);
2766 end loop;
2768 if Missing_Discriminants then
2769 return;
2770 end if;
2772 -- At this point and until the beginning of STEP 6, New_Assoc_List
2773 -- contains only the discriminants and their values.
2775 end Step_3;
2777 -- STEP 4: Set the Etype of the record aggregate
2779 -- ??? This code is pretty much a copy of Sem_Ch3.Build_Subtype. That
2780 -- routine should really be exported in sem_util or some such and used
2781 -- in sem_ch3 and here rather than have a copy of the code which is a
2782 -- maintenance nightmare.
2784 -- ??? Performace WARNING. The current implementation creates a new
2785 -- itype for all aggregates whose base type is discriminated.
2786 -- This means that for record aggregates nested inside an array
2787 -- aggregate we will create a new itype for each record aggregate
2788 -- if the array cmponent type has discriminants. For large aggregates
2789 -- this may be a problem. What should be done in this case is
2790 -- to reuse itypes as much as possible.
2792 if Has_Discriminants (Typ) then
2793 Build_Constrained_Itype : declare
2794 Loc : constant Source_Ptr := Sloc (N);
2795 Indic : Node_Id;
2796 Subtyp_Decl : Node_Id;
2797 Def_Id : Entity_Id;
2799 C : constant List_Id := New_List;
2801 begin
2802 New_Assoc := First (New_Assoc_List);
2803 while Present (New_Assoc) loop
2804 Append (Duplicate_Subexpr (Expression (New_Assoc)), To => C);
2805 Next (New_Assoc);
2806 end loop;
2808 Indic :=
2809 Make_Subtype_Indication (Loc,
2810 Subtype_Mark => New_Occurrence_Of (Base_Type (Typ), Loc),
2811 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
2813 Def_Id := Create_Itype (Ekind (Typ), N);
2815 Subtyp_Decl :=
2816 Make_Subtype_Declaration (Loc,
2817 Defining_Identifier => Def_Id,
2818 Subtype_Indication => Indic);
2819 Set_Parent (Subtyp_Decl, Parent (N));
2821 -- Itypes must be analyzed with checks off (see itypes.ads)
2823 Analyze (Subtyp_Decl, Suppress => All_Checks);
2825 Set_Etype (N, Def_Id);
2826 Check_Static_Discriminated_Subtype
2827 (Def_Id, Expression (First (New_Assoc_List)));
2828 end Build_Constrained_Itype;
2830 else
2831 Set_Etype (N, Typ);
2832 end if;
2834 -- STEP 5: Get remaining components according to discriminant values
2836 Step_5 : declare
2837 Record_Def : Node_Id;
2838 Parent_Typ : Entity_Id;
2839 Root_Typ : Entity_Id;
2840 Parent_Typ_List : Elist_Id;
2841 Parent_Elmt : Elmt_Id;
2842 Errors_Found : Boolean := False;
2843 Dnode : Node_Id;
2845 begin
2846 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
2847 Parent_Typ_List := New_Elmt_List;
2849 -- If this is an extension aggregate, the component list must
2850 -- include all components that are not in the given ancestor
2851 -- type. Otherwise, the component list must include components
2852 -- of all ancestors, starting with the root.
2854 if Nkind (N) = N_Extension_Aggregate then
2855 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
2856 else
2857 Root_Typ := Root_Type (Typ);
2859 if Nkind (Parent (Base_Type (Root_Typ)))
2860 = N_Private_Type_Declaration
2861 then
2862 Error_Msg_NE
2863 ("type of aggregate has private ancestor&!",
2864 N, Root_Typ);
2865 Error_Msg_N ("must use extension aggregate!", N);
2866 return;
2867 end if;
2869 Dnode := Declaration_Node (Base_Type (Root_Typ));
2871 -- If we don't get a full declaration, then we have some
2872 -- error which will get signalled later so skip this part.
2873 -- Otherwise, gather components of root that apply to the
2874 -- aggregate type. We use the base type in case there is an
2875 -- applicable stored constraint that renames the discriminants
2876 -- of the root.
2878 if Nkind (Dnode) = N_Full_Type_Declaration then
2879 Record_Def := Type_Definition (Dnode);
2880 Gather_Components (Base_Type (Typ),
2881 Component_List (Record_Def),
2882 Governed_By => New_Assoc_List,
2883 Into => Components,
2884 Report_Errors => Errors_Found);
2885 end if;
2886 end if;
2888 Parent_Typ := Base_Type (Typ);
2889 while Parent_Typ /= Root_Typ loop
2890 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
2891 Parent_Typ := Etype (Parent_Typ);
2893 if Nkind (Parent (Base_Type (Parent_Typ))) =
2894 N_Private_Type_Declaration
2895 or else Nkind (Parent (Base_Type (Parent_Typ))) =
2896 N_Private_Extension_Declaration
2897 then
2898 if Nkind (N) /= N_Extension_Aggregate then
2899 Error_Msg_NE
2900 ("type of aggregate has private ancestor&!",
2901 N, Parent_Typ);
2902 Error_Msg_N ("must use extension aggregate!", N);
2903 return;
2905 elsif Parent_Typ /= Root_Typ then
2906 Error_Msg_NE
2907 ("ancestor part of aggregate must be private type&",
2908 Ancestor_Part (N), Parent_Typ);
2909 return;
2910 end if;
2911 end if;
2912 end loop;
2914 -- Now collect components from all other ancestors
2916 Parent_Elmt := First_Elmt (Parent_Typ_List);
2917 while Present (Parent_Elmt) loop
2918 Parent_Typ := Node (Parent_Elmt);
2919 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
2920 Gather_Components (Empty,
2921 Component_List (Record_Extension_Part (Record_Def)),
2922 Governed_By => New_Assoc_List,
2923 Into => Components,
2924 Report_Errors => Errors_Found);
2926 Next_Elmt (Parent_Elmt);
2927 end loop;
2929 else
2930 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
2932 if Null_Present (Record_Def) then
2933 null;
2934 else
2935 Gather_Components (Base_Type (Typ),
2936 Component_List (Record_Def),
2937 Governed_By => New_Assoc_List,
2938 Into => Components,
2939 Report_Errors => Errors_Found);
2940 end if;
2941 end if;
2943 if Errors_Found then
2944 return;
2945 end if;
2946 end Step_5;
2948 -- STEP 6: Find component Values
2950 Component := Empty;
2951 Component_Elmt := First_Elmt (Components);
2953 -- First scan the remaining positional associations in the aggregate.
2954 -- Remember that at this point Positional_Expr contains the current
2955 -- positional association if any is left after looking for discriminant
2956 -- values in step 3.
2958 while Present (Positional_Expr) and then Present (Component_Elmt) loop
2959 Component := Node (Component_Elmt);
2960 Resolve_Aggr_Expr (Positional_Expr, Component);
2962 -- Ada 2005 (AI-231)
2964 if Ada_Version >= Ada_05
2965 and then Known_Null (Positional_Expr)
2966 then
2967 Check_Can_Never_Be_Null (Component, Positional_Expr);
2968 end if;
2970 if Present (Get_Value (Component, Component_Associations (N))) then
2971 Error_Msg_NE
2972 ("more than one value supplied for Component &", N, Component);
2973 end if;
2975 Next (Positional_Expr);
2976 Next_Elmt (Component_Elmt);
2977 end loop;
2979 if Present (Positional_Expr) then
2980 Error_Msg_N
2981 ("too many components for record aggregate", Positional_Expr);
2982 end if;
2984 -- Now scan for the named arguments of the aggregate
2986 while Present (Component_Elmt) loop
2987 Component := Node (Component_Elmt);
2988 Expr := Get_Value (Component, Component_Associations (N), True);
2990 -- Note: The previous call to Get_Value sets the value of the
2991 -- variable Is_Box_Present.
2993 -- Ada 2005 (AI-287): Handle components with default initialization.
2994 -- Note: This feature was originally added to Ada 2005 for limited
2995 -- but it was finally allowed with any type.
2997 if Is_Box_Present then
2998 Check_Box_Component : declare
2999 Ctyp : constant Entity_Id := Etype (Component);
3001 begin
3002 -- If there is a default expression for the aggregate, copy
3003 -- it into a new association.
3005 -- If the component has an initialization procedure (IP) we
3006 -- pass the component to the expander, which will generate
3007 -- the call to such IP.
3009 -- If the component has discriminants, their values must
3010 -- be taken from their subtype. This is indispensable for
3011 -- constraints that are given by the current instance of an
3012 -- enclosing type, to allow the expansion of the aggregate
3013 -- to replace the reference to the current instance by the
3014 -- target object of the aggregate.
3016 if Present (Parent (Component))
3017 and then
3018 Nkind (Parent (Component)) = N_Component_Declaration
3019 and then Present (Expression (Parent (Component)))
3020 then
3021 Expr :=
3022 New_Copy_Tree (Expression (Parent (Component)),
3023 New_Sloc => Sloc (N));
3025 Add_Association
3026 (Component => Component,
3027 Expr => Expr);
3028 Set_Has_Self_Reference (N);
3030 -- A box-defaulted access component gets the value null. Also
3031 -- included are components of private types whose underlying
3032 -- type is an access type.
3034 elsif Present (Underlying_Type (Ctyp))
3035 and then Is_Access_Type (Underlying_Type (Ctyp))
3036 then
3037 if not Is_Private_Type (Ctyp) then
3038 Add_Association
3039 (Component => Component,
3040 Expr => Make_Null (Sloc (N)));
3042 -- If the component's type is private with an access type as
3043 -- its underlying type then we have to create an unchecked
3044 -- conversion to satisfy type checking.
3046 else
3047 declare
3048 Qual_Null : constant Node_Id :=
3049 Make_Qualified_Expression (Sloc (N),
3050 Subtype_Mark =>
3051 New_Occurrence_Of
3052 (Underlying_Type (Ctyp), Sloc (N)),
3053 Expression => Make_Null (Sloc (N)));
3055 Convert_Null : constant Node_Id :=
3056 Unchecked_Convert_To
3057 (Ctyp, Qual_Null);
3059 begin
3060 Analyze_And_Resolve (Convert_Null, Ctyp);
3061 Add_Association
3062 (Component => Component, Expr => Convert_Null);
3063 end;
3064 end if;
3066 elsif Has_Non_Null_Base_Init_Proc (Ctyp)
3067 or else not Expander_Active
3068 then
3069 if Is_Record_Type (Ctyp)
3070 and then Has_Discriminants (Ctyp)
3071 then
3072 -- We build a partially initialized aggregate with the
3073 -- values of the discriminants and box initialization
3074 -- for the rest, if other components are present.
3076 declare
3077 Loc : constant Source_Ptr := Sloc (N);
3078 Assoc : Node_Id;
3079 Discr : Entity_Id;
3080 Discr_Elmt : Elmt_Id;
3081 Discr_Val : Node_Id;
3082 Expr : Node_Id;
3084 begin
3085 Expr := Make_Aggregate (Loc, New_List, New_List);
3087 Discr_Elmt :=
3088 First_Elmt (Discriminant_Constraint (Ctyp));
3089 while Present (Discr_Elmt) loop
3090 Discr_Val := Node (Discr_Elmt);
3092 -- The constraint may be given by a discriminant
3093 -- of the enclosing type, in which case we have
3094 -- to retrieve its value, which is part of the
3095 -- current aggregate.
3097 if Is_Entity_Name (Discr_Val)
3098 and then
3099 Ekind (Entity (Discr_Val)) = E_Discriminant
3100 then
3101 Discr := Entity (Discr_Val);
3103 Assoc := First (New_Assoc_List);
3104 while Present (Assoc) loop
3105 if Present
3106 (Entity (First (Choices (Assoc))))
3107 and then
3108 Entity (First (Choices (Assoc))) = Discr
3109 then
3110 Discr_Val := Expression (Assoc);
3111 exit;
3112 end if;
3113 Next (Assoc);
3114 end loop;
3115 end if;
3117 Append
3118 (New_Copy_Tree (Discr_Val), Expressions (Expr));
3120 -- If the discriminant constraint is a current
3121 -- instance, mark the current aggregate so that
3122 -- the self-reference can be expanded later.
3124 if Nkind (Discr_Val) = N_Attribute_Reference
3125 and then Is_Entity_Name (Prefix (Discr_Val))
3126 and then Is_Type (Entity (Prefix (Discr_Val)))
3127 and then Etype (N) = Entity (Prefix (Discr_Val))
3128 then
3129 Set_Has_Self_Reference (N);
3130 end if;
3132 Next_Elmt (Discr_Elmt);
3133 end loop;
3135 declare
3136 Comp : Entity_Id;
3138 begin
3139 -- Look for a component that is not a discriminant
3140 -- before creating an others box association.
3142 Comp := First_Component (Ctyp);
3143 while Present (Comp) loop
3144 if Ekind (Comp) = E_Component then
3145 Append
3146 (Make_Component_Association (Loc,
3147 Choices =>
3148 New_List (Make_Others_Choice (Loc)),
3149 Expression => Empty,
3150 Box_Present => True),
3151 Component_Associations (Expr));
3152 exit;
3153 end if;
3155 Next_Component (Comp);
3156 end loop;
3157 end;
3159 Add_Association
3160 (Component => Component,
3161 Expr => Expr);
3162 end;
3164 else
3165 Add_Association
3166 (Component => Component,
3167 Expr => Empty,
3168 Is_Box_Present => True);
3169 end if;
3171 -- Otherwise we only need to resolve the expression if the
3172 -- component has partially initialized values (required to
3173 -- expand the corresponding assignments and run-time checks).
3175 elsif Present (Expr)
3176 and then Is_Partially_Initialized_Type (Ctyp)
3177 then
3178 Resolve_Aggr_Expr (Expr, Component);
3179 end if;
3180 end Check_Box_Component;
3182 elsif No (Expr) then
3184 -- Ignore hidden components associated with the position of the
3185 -- interface tags: these are initialized dynamically.
3187 if Present (Related_Interface (Component)) then
3188 null;
3189 else
3190 Error_Msg_NE
3191 ("no value supplied for component &!", N, Component);
3192 end if;
3194 else
3195 Resolve_Aggr_Expr (Expr, Component);
3196 end if;
3198 Next_Elmt (Component_Elmt);
3199 end loop;
3201 -- STEP 7: check for invalid components + check type in choice list
3203 Step_7 : declare
3204 Selectr : Node_Id;
3205 -- Selector name
3207 Typech : Entity_Id;
3208 -- Type of first component in choice list
3210 begin
3211 if Present (Component_Associations (N)) then
3212 Assoc := First (Component_Associations (N));
3213 else
3214 Assoc := Empty;
3215 end if;
3217 Verification : while Present (Assoc) loop
3218 Selectr := First (Choices (Assoc));
3219 Typech := Empty;
3221 if Nkind (Selectr) = N_Others_Choice then
3223 -- Ada 2005 (AI-287): others choice may have expression or box
3225 if No (Others_Etype)
3226 and then not Others_Box
3227 then
3228 Error_Msg_N
3229 ("OTHERS must represent at least one component", Selectr);
3230 end if;
3232 exit Verification;
3233 end if;
3235 while Present (Selectr) loop
3236 New_Assoc := First (New_Assoc_List);
3237 while Present (New_Assoc) loop
3238 Component := First (Choices (New_Assoc));
3239 exit when Chars (Selectr) = Chars (Component);
3240 Next (New_Assoc);
3241 end loop;
3243 -- If no association, this is not a legal component of
3244 -- of the type in question, except if its association
3245 -- is provided with a box.
3247 if No (New_Assoc) then
3248 if Box_Present (Parent (Selectr)) then
3250 -- This may still be a bogus component with a box. Scan
3251 -- list of components to verify that a component with
3252 -- that name exists.
3254 declare
3255 C : Entity_Id;
3257 begin
3258 C := First_Component (Typ);
3259 while Present (C) loop
3260 if Chars (C) = Chars (Selectr) then
3261 exit;
3262 end if;
3264 Next_Component (C);
3265 end loop;
3267 if No (C) then
3268 Error_Msg_Node_2 := Typ;
3269 Error_Msg_N ("& is not a component of}", Selectr);
3270 end if;
3271 end;
3273 elsif Chars (Selectr) /= Name_uTag
3274 and then Chars (Selectr) /= Name_uParent
3275 and then Chars (Selectr) /= Name_uController
3276 then
3277 if not Has_Discriminants (Typ) then
3278 Error_Msg_Node_2 := Typ;
3279 Error_Msg_N ("& is not a component of}", Selectr);
3280 else
3281 Error_Msg_N
3282 ("& is not a component of the aggregate subtype",
3283 Selectr);
3284 end if;
3286 Check_Misspelled_Component (Components, Selectr);
3287 end if;
3289 elsif No (Typech) then
3290 Typech := Base_Type (Etype (Component));
3292 elsif Typech /= Base_Type (Etype (Component)) then
3293 if not Box_Present (Parent (Selectr)) then
3294 Error_Msg_N
3295 ("components in choice list must have same type",
3296 Selectr);
3297 end if;
3298 end if;
3300 Next (Selectr);
3301 end loop;
3303 Next (Assoc);
3304 end loop Verification;
3305 end Step_7;
3307 -- STEP 8: replace the original aggregate
3309 Step_8 : declare
3310 New_Aggregate : constant Node_Id := New_Copy (N);
3312 begin
3313 Set_Expressions (New_Aggregate, No_List);
3314 Set_Etype (New_Aggregate, Etype (N));
3315 Set_Component_Associations (New_Aggregate, New_Assoc_List);
3317 Rewrite (N, New_Aggregate);
3318 end Step_8;
3319 end Resolve_Record_Aggregate;
3321 -----------------------------
3322 -- Check_Can_Never_Be_Null --
3323 -----------------------------
3325 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
3326 Comp_Typ : Entity_Id;
3328 begin
3329 pragma Assert
3330 (Ada_Version >= Ada_05
3331 and then Present (Expr)
3332 and then Known_Null (Expr));
3334 case Ekind (Typ) is
3335 when E_Array_Type =>
3336 Comp_Typ := Component_Type (Typ);
3338 when E_Component |
3339 E_Discriminant =>
3340 Comp_Typ := Etype (Typ);
3342 when others =>
3343 return;
3344 end case;
3346 if Can_Never_Be_Null (Comp_Typ) then
3348 -- Here we know we have a constraint error. Note that we do not use
3349 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
3350 -- seem the more natural approach. That's because in some cases the
3351 -- components are rewritten, and the replacement would be missed.
3353 Insert_Action
3354 (Compile_Time_Constraint_Error
3355 (Expr,
3356 "(Ada 2005) null not allowed in null-excluding component?"),
3357 Make_Raise_Constraint_Error (Sloc (Expr),
3358 Reason => CE_Access_Check_Failed));
3360 -- Set proper type for bogus component (why is this needed???)
3362 Set_Etype (Expr, Comp_Typ);
3363 Set_Analyzed (Expr);
3364 end if;
3365 end Check_Can_Never_Be_Null;
3367 ---------------------
3368 -- Sort_Case_Table --
3369 ---------------------
3371 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
3372 L : constant Int := Case_Table'First;
3373 U : constant Int := Case_Table'Last;
3374 K : Int;
3375 J : Int;
3376 T : Case_Bounds;
3378 begin
3379 K := L;
3380 while K /= U loop
3381 T := Case_Table (K + 1);
3383 J := K + 1;
3384 while J /= L
3385 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
3386 Expr_Value (T.Choice_Lo)
3387 loop
3388 Case_Table (J) := Case_Table (J - 1);
3389 J := J - 1;
3390 end loop;
3392 Case_Table (J) := T;
3393 K := K + 1;
3394 end loop;
3395 end Sort_Case_Table;
3397 end Sem_Aggr;