Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / gcc / ada / sem_aggr.adb
blob8890ffc43dc8b4d9aa86d75b448ca38cf525e718
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-2005, 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Exp_Tss; use Exp_Tss;
33 with Exp_Util; use Exp_Util;
34 with Freeze; use Freeze;
35 with Itypes; use Itypes;
36 with Lib.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_Ch8; use Sem_Ch8;
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 : Node_Id; Expr : Node_Id);
82 -- Ada 2005 (AI-231): Check bad usage of the null-exclusion issue
84 ------------------------------------------------------
85 -- Subprograms used for RECORD AGGREGATE Processing --
86 ------------------------------------------------------
88 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
89 -- This procedure performs all the semantic checks required for record
90 -- aggregates. Note that for aggregates analysis and resolution go
91 -- hand in hand. Aggregate analysis has been delayed up to here and
92 -- it is done while resolving the aggregate.
94 -- N is the N_Aggregate node.
95 -- Typ is the record type for the aggregate resolution
97 -- While performing the semantic checks, this procedure
98 -- builds a new Component_Association_List where each record field
99 -- appears alone in a Component_Choice_List along with its corresponding
100 -- expression. The record fields in the Component_Association_List
101 -- appear in the same order in which they appear in the record type Typ.
103 -- Once this new Component_Association_List is built and all the
104 -- semantic checks performed, the original aggregate subtree is replaced
105 -- with the new named record aggregate just built. Note that the subtree
106 -- substitution is performed with Rewrite so as to be
107 -- able to retrieve the original aggregate.
109 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
110 -- yields the aggregate format expected by Gigi. Typically, this kind of
111 -- tree manipulations are done in the expander. However, because the
112 -- semantic checks that need to be performed on record aggregates really
113 -- go hand in hand with the record aggregate normalization, the aggregate
114 -- subtree transformation is performed during resolution rather than
115 -- expansion. Had we decided otherwise we would have had to duplicate
116 -- most of the code in the expansion procedure Expand_Record_Aggregate.
117 -- Note, however, that all the expansion concerning aggegates for tagged
118 -- records is done in Expand_Record_Aggregate.
120 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
122 -- 1. Make sure that the record type against which the record aggregate
123 -- has to be resolved is not abstract. Furthermore if the type is
124 -- a null aggregate make sure the input aggregate N is also null.
126 -- 2. Verify that the structure of the aggregate is that of a record
127 -- aggregate. Specifically, look for component associations and ensure
128 -- that each choice list only has identifiers or the N_Others_Choice
129 -- node. Also make sure that if present, the N_Others_Choice occurs
130 -- last and by itself.
132 -- 3. If Typ contains discriminants, the values for each discriminant
133 -- is looked for. If the record type Typ has variants, we check
134 -- that the expressions corresponding to each discriminant ruling
135 -- the (possibly nested) variant parts of Typ, are static. This
136 -- allows us to determine the variant parts to which the rest of
137 -- the aggregate must conform. The names of discriminants with their
138 -- values are saved in a new association list, New_Assoc_List which
139 -- is later augmented with the names and values of the remaining
140 -- components in the record type.
142 -- During this phase we also make sure that every discriminant is
143 -- assigned exactly one value. Note that when several values
144 -- for a given discriminant are found, semantic processing continues
145 -- looking for further errors. In this case it's the first
146 -- discriminant value found which we will be recorded.
148 -- IMPORTANT NOTE: For derived tagged types this procedure expects
149 -- First_Discriminant and Next_Discriminant to give the correct list
150 -- of discriminants, in the correct order.
152 -- 4. After all the discriminant values have been gathered, we can
153 -- set the Etype of the record aggregate. If Typ contains no
154 -- discriminants this is straightforward: the Etype of N is just
155 -- Typ, otherwise a new implicit constrained subtype of Typ is
156 -- built to be the Etype of N.
158 -- 5. Gather the remaining record components according to the discriminant
159 -- values. This involves recursively traversing the record type
160 -- structure to see what variants are selected by the given discriminant
161 -- values. This processing is a little more convoluted if Typ is a
162 -- derived tagged types since we need to retrieve the record structure
163 -- of all the ancestors of Typ.
165 -- 6. After gathering the record components we look for their values
166 -- in the record aggregate and emit appropriate error messages
167 -- should we not find such values or should they be duplicated.
169 -- 7. We then make sure no illegal component names appear in the
170 -- record aggegate and make sure that the type of the record
171 -- components appearing in a same choice list is the same.
172 -- Finally we ensure that the others choice, if present, is
173 -- used to provide the value of at least a record component.
175 -- 8. The original aggregate node is replaced with the new named
176 -- aggregate built in steps 3 through 6, as explained earlier.
178 -- Given the complexity of record aggregate resolution, the primary
179 -- goal of this routine is clarity and simplicity rather than execution
180 -- and storage efficiency. If there are only positional components in the
181 -- aggregate the running time is linear. If there are associations
182 -- the running time is still linear as long as the order of the
183 -- associations is not too far off the order of the components in the
184 -- record type. If this is not the case the running time is at worst
185 -- quadratic in the size of the association list.
187 procedure Check_Misspelled_Component
188 (Elements : Elist_Id;
189 Component : Node_Id);
190 -- Give possible misspelling diagnostic if Component is likely to be
191 -- a misspelling of one of the components of the Assoc_List.
192 -- This is called by Resolv_Aggr_Expr after producing
193 -- an invalid component error message.
195 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id);
196 -- An optimization: determine whether a discriminated subtype has a
197 -- static constraint, and contains array components whose length is also
198 -- static, either because they are constrained by the discriminant, or
199 -- because the original component bounds are static.
201 -----------------------------------------------------
202 -- Subprograms used for ARRAY AGGREGATE Processing --
203 -----------------------------------------------------
205 function Resolve_Array_Aggregate
206 (N : Node_Id;
207 Index : Node_Id;
208 Index_Constr : Node_Id;
209 Component_Typ : Entity_Id;
210 Others_Allowed : Boolean)
211 return Boolean;
212 -- This procedure performs the semantic checks for an array aggregate.
213 -- True is returned if the aggregate resolution succeeds.
214 -- The procedure works by recursively checking each nested aggregate.
215 -- Specifically, after checking a sub-aggregate nested at the i-th level
216 -- we recursively check all the subaggregates at the i+1-st level (if any).
217 -- Note that for aggregates analysis and resolution go hand in hand.
218 -- Aggregate analysis has been delayed up to here and it is done while
219 -- resolving the aggregate.
221 -- N is the current N_Aggregate node to be checked.
223 -- Index is the index node corresponding to the array sub-aggregate that
224 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
225 -- corresponding index type (or subtype).
227 -- Index_Constr is the node giving the applicable index constraint if
228 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
229 -- contexts [...] that can be used to determine the bounds of the array
230 -- value specified by the aggregate". If Others_Allowed below is False
231 -- there is no applicable index constraint and this node is set to Index.
233 -- Component_Typ is the array component type.
235 -- Others_Allowed indicates whether an others choice is allowed
236 -- in the context where the top-level aggregate appeared.
238 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
240 -- 1. Make sure that the others choice, if present, is by itself and
241 -- appears last in the sub-aggregate. Check that we do not have
242 -- positional and named components in the array sub-aggregate (unless
243 -- the named association is an others choice). Finally if an others
244 -- choice is present, make sure it is allowed in the aggregate contex.
246 -- 2. If the array sub-aggregate contains discrete_choices:
248 -- (A) Verify their validity. Specifically verify that:
250 -- (a) If a null range is present it must be the only possible
251 -- choice in the array aggregate.
253 -- (b) Ditto for a non static range.
255 -- (c) Ditto for a non static expression.
257 -- In addition this step analyzes and resolves each discrete_choice,
258 -- making sure that its type is the type of the corresponding Index.
259 -- If we are not at the lowest array aggregate level (in the case of
260 -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
261 -- recursively on each component expression. Otherwise, resolve the
262 -- bottom level component expressions against the expected component
263 -- type ONLY IF the component corresponds to a single discrete choice
264 -- which is not an others choice (to see why read the DELAYED
265 -- COMPONENT RESOLUTION below).
267 -- (B) Determine the bounds of the sub-aggregate and lowest and
268 -- highest choice values.
270 -- 3. For positional aggregates:
272 -- (A) Loop over the component expressions either recursively invoking
273 -- Resolve_Array_Aggregate on each of these for multi-dimensional
274 -- array aggregates or resolving the bottom level component
275 -- expressions against the expected component type.
277 -- (B) Determine the bounds of the positional sub-aggregates.
279 -- 4. Try to determine statically whether the evaluation of the array
280 -- sub-aggregate raises Constraint_Error. If yes emit proper
281 -- warnings. The precise checks are the following:
283 -- (A) Check that the index range defined by aggregate bounds is
284 -- compatible with corresponding index subtype.
285 -- We also check against the base type. In fact it could be that
286 -- Low/High bounds of the base type are static whereas those of
287 -- the index subtype are not. Thus if we can statically catch
288 -- a problem with respect to the base type we are guaranteed
289 -- that the same problem will arise with the index subtype
291 -- (B) If we are dealing with a named aggregate containing an others
292 -- choice and at least one discrete choice then make sure the range
293 -- specified by the discrete choices does not overflow the
294 -- aggregate bounds. We also check against the index type and base
295 -- type bounds for the same reasons given in (A).
297 -- (C) If we are dealing with a positional aggregate with an others
298 -- choice make sure the number of positional elements specified
299 -- does not overflow the aggregate bounds. We also check against
300 -- the index type and base type bounds as mentioned in (A).
302 -- Finally construct an N_Range node giving the sub-aggregate bounds.
303 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
304 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
305 -- to build the appropriate aggregate subtype. Aggregate_Bounds
306 -- information is needed during expansion.
308 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
309 -- expressions in an array aggregate may call Duplicate_Subexpr or some
310 -- other routine that inserts code just outside the outermost aggregate.
311 -- If the array aggregate contains discrete choices or an others choice,
312 -- this may be wrong. Consider for instance the following example.
314 -- type Rec is record
315 -- V : Integer := 0;
316 -- end record;
318 -- type Acc_Rec is access Rec;
319 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
321 -- Then the transformation of "new Rec" that occurs during resolution
322 -- entails the following code modifications
324 -- P7b : constant Acc_Rec := new Rec;
325 -- RecIP (P7b.all);
326 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
328 -- This code transformation is clearly wrong, since we need to call
329 -- "new Rec" for each of the 3 array elements. To avoid this problem we
330 -- delay resolution of the components of non positional array aggregates
331 -- to the expansion phase. As an optimization, if the discrete choice
332 -- specifies a single value we do not delay resolution.
334 function Array_Aggr_Subtype (N : Node_Id; Typ : Node_Id) return Entity_Id;
335 -- This routine returns the type or subtype of an array aggregate.
337 -- N is the array aggregate node whose type we return.
339 -- Typ is the context type in which N occurs.
341 -- This routine creates an implicit array subtype whose bounds are
342 -- those defined by the aggregate. When this routine is invoked
343 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
344 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
345 -- sub-aggregate bounds. When building the aggegate itype, this function
346 -- traverses the array aggregate N collecting such Aggregate_Bounds and
347 -- constructs the proper array aggregate itype.
349 -- Note that in the case of multidimensional aggregates each inner
350 -- sub-aggregate corresponding to a given array dimension, may provide a
351 -- different bounds. If it is possible to determine statically that
352 -- some sub-aggregates corresponding to the same index do not have the
353 -- same bounds, then a warning is emitted. If such check is not possible
354 -- statically (because some sub-aggregate bounds are dynamic expressions)
355 -- then this job is left to the expander. In all cases the particular
356 -- bounds that this function will chose for a given dimension is the first
357 -- N_Range node for a sub-aggregate corresponding to that dimension.
359 -- Note that the Raises_Constraint_Error flag of an array aggregate
360 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
361 -- is set in Resolve_Array_Aggregate but the aggregate is not
362 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
363 -- first construct the proper itype for the aggregate (Gigi needs
364 -- this). After constructing the proper itype we will eventually replace
365 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
366 -- Of course in cases such as:
368 -- type Arr is array (integer range <>) of Integer;
369 -- A : Arr := (positive range -1 .. 2 => 0);
371 -- The bounds of the aggregate itype are cooked up to look reasonable
372 -- (in this particular case the bounds will be 1 .. 2).
374 procedure Aggregate_Constraint_Checks
375 (Exp : Node_Id;
376 Check_Typ : Entity_Id);
377 -- Checks expression Exp against subtype Check_Typ. If Exp is an
378 -- aggregate and Check_Typ a constrained record type with discriminants,
379 -- we generate the appropriate discriminant checks. If Exp is an array
380 -- aggregate then emit the appropriate length checks. If Exp is a scalar
381 -- type, or a string literal, Exp is changed into Check_Typ'(Exp) to
382 -- ensure that range checks are performed at run time.
384 procedure Make_String_Into_Aggregate (N : Node_Id);
385 -- A string literal can appear in a context in which a one dimensional
386 -- array of characters is expected. This procedure simply rewrites the
387 -- string as an aggregate, prior to resolution.
389 ---------------------------------
390 -- Aggregate_Constraint_Checks --
391 ---------------------------------
393 procedure Aggregate_Constraint_Checks
394 (Exp : Node_Id;
395 Check_Typ : Entity_Id)
397 Exp_Typ : constant Entity_Id := Etype (Exp);
399 begin
400 if Raises_Constraint_Error (Exp) then
401 return;
402 end if;
404 -- This is really expansion activity, so make sure that expansion
405 -- is on and is allowed.
407 if not Expander_Active or else In_Default_Expression then
408 return;
409 end if;
411 -- First check if we have to insert discriminant checks
413 if Has_Discriminants (Exp_Typ) then
414 Apply_Discriminant_Check (Exp, Check_Typ);
416 -- Next emit length checks for array aggregates
418 elsif Is_Array_Type (Exp_Typ) then
419 Apply_Length_Check (Exp, Check_Typ);
421 -- Finally emit scalar and string checks. If we are dealing with a
422 -- scalar literal we need to check by hand because the Etype of
423 -- literals is not necessarily correct.
425 elsif Is_Scalar_Type (Exp_Typ)
426 and then Compile_Time_Known_Value (Exp)
427 then
428 if Is_Out_Of_Range (Exp, Base_Type (Check_Typ)) then
429 Apply_Compile_Time_Constraint_Error
430 (Exp, "value not in range of}?", CE_Range_Check_Failed,
431 Ent => Base_Type (Check_Typ),
432 Typ => Base_Type (Check_Typ));
434 elsif Is_Out_Of_Range (Exp, Check_Typ) then
435 Apply_Compile_Time_Constraint_Error
436 (Exp, "value not in range of}?", CE_Range_Check_Failed,
437 Ent => Check_Typ,
438 Typ => Check_Typ);
440 elsif not Range_Checks_Suppressed (Check_Typ) then
441 Apply_Scalar_Range_Check (Exp, Check_Typ);
442 end if;
444 elsif (Is_Scalar_Type (Exp_Typ)
445 or else Nkind (Exp) = N_String_Literal)
446 and then Exp_Typ /= Check_Typ
447 then
448 if Is_Entity_Name (Exp)
449 and then Ekind (Entity (Exp)) = E_Constant
450 then
451 -- If expression is a constant, it is worthwhile checking whether
452 -- it is a bound of the type.
454 if (Is_Entity_Name (Type_Low_Bound (Check_Typ))
455 and then Entity (Exp) = Entity (Type_Low_Bound (Check_Typ)))
456 or else (Is_Entity_Name (Type_High_Bound (Check_Typ))
457 and then Entity (Exp) = Entity (Type_High_Bound (Check_Typ)))
458 then
459 return;
461 else
462 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
463 Analyze_And_Resolve (Exp, Check_Typ);
464 Check_Unset_Reference (Exp);
465 end if;
466 else
467 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
468 Analyze_And_Resolve (Exp, Check_Typ);
469 Check_Unset_Reference (Exp);
470 end if;
472 -- Ada 2005 (AI-230): Generate a conversion to an anonymous access
473 -- component's type to force the appropriate accessibility checks.
475 -- Ada 2005 (AI-231): Generate conversion to the null-excluding
476 -- type to force the corresponding run-time check
478 elsif Is_Access_Type (Check_Typ)
479 and then ((Is_Local_Anonymous_Access (Check_Typ))
480 or else (Can_Never_Be_Null (Check_Typ)
481 and then not Can_Never_Be_Null (Exp_Typ)))
482 then
483 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
484 Analyze_And_Resolve (Exp, Check_Typ);
485 Check_Unset_Reference (Exp);
486 end if;
487 end Aggregate_Constraint_Checks;
489 ------------------------
490 -- Array_Aggr_Subtype --
491 ------------------------
493 function Array_Aggr_Subtype
494 (N : Node_Id;
495 Typ : Entity_Id)
496 return Entity_Id
498 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
499 -- Number of aggregate index dimensions
501 Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
502 -- Constrained N_Range of each index dimension in our aggregate itype
504 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
505 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
506 -- Low and High bounds for each index dimension in our aggregate itype
508 Is_Fully_Positional : Boolean := True;
510 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
511 -- N is an array (sub-)aggregate. Dim is the dimension corresponding to
512 -- (sub-)aggregate N. This procedure collects the constrained N_Range
513 -- nodes corresponding to each index dimension of our aggregate itype.
514 -- These N_Range nodes are collected in Aggr_Range above.
516 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
517 -- bounds of each index dimension. If, when collecting, two bounds
518 -- corresponding to the same dimension are static and found to differ,
519 -- then emit a warning, and mark N as raising Constraint_Error.
521 -------------------------
522 -- Collect_Aggr_Bounds --
523 -------------------------
525 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
526 This_Range : constant Node_Id := Aggregate_Bounds (N);
527 -- The aggregate range node of this specific sub-aggregate
529 This_Low : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
530 This_High : constant Node_Id := High_Bound (Aggregate_Bounds (N));
531 -- The aggregate bounds of this specific sub-aggregate
533 Assoc : Node_Id;
534 Expr : Node_Id;
536 begin
537 -- Collect the first N_Range for a given dimension that you find.
538 -- For a given dimension they must be all equal anyway.
540 if No (Aggr_Range (Dim)) then
541 Aggr_Low (Dim) := This_Low;
542 Aggr_High (Dim) := This_High;
543 Aggr_Range (Dim) := This_Range;
545 else
546 if Compile_Time_Known_Value (This_Low) then
547 if not Compile_Time_Known_Value (Aggr_Low (Dim)) then
548 Aggr_Low (Dim) := This_Low;
550 elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
551 Set_Raises_Constraint_Error (N);
552 Error_Msg_N ("sub-aggregate low bound mismatch?", N);
553 Error_Msg_N ("Constraint_Error will be raised at run-time?",
555 end if;
556 end if;
558 if Compile_Time_Known_Value (This_High) then
559 if not Compile_Time_Known_Value (Aggr_High (Dim)) then
560 Aggr_High (Dim) := This_High;
562 elsif
563 Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
564 then
565 Set_Raises_Constraint_Error (N);
566 Error_Msg_N ("sub-aggregate high bound mismatch?", N);
567 Error_Msg_N ("Constraint_Error will be raised at run-time?",
569 end if;
570 end if;
571 end if;
573 if Dim < Aggr_Dimension then
575 -- Process positional components
577 if Present (Expressions (N)) then
578 Expr := First (Expressions (N));
579 while Present (Expr) loop
580 Collect_Aggr_Bounds (Expr, Dim + 1);
581 Next (Expr);
582 end loop;
583 end if;
585 -- Process component associations
587 if Present (Component_Associations (N)) then
588 Is_Fully_Positional := False;
590 Assoc := First (Component_Associations (N));
591 while Present (Assoc) loop
592 Expr := Expression (Assoc);
593 Collect_Aggr_Bounds (Expr, Dim + 1);
594 Next (Assoc);
595 end loop;
596 end if;
597 end if;
598 end Collect_Aggr_Bounds;
600 -- Array_Aggr_Subtype variables
602 Itype : Entity_Id;
603 -- the final itype of the overall aggregate
605 Index_Constraints : constant List_Id := New_List;
606 -- The list of index constraints of the aggregate itype
608 -- Start of processing for Array_Aggr_Subtype
610 begin
611 -- Make sure that the list of index constraints is properly attached
612 -- to the tree, and then collect the aggregate bounds.
614 Set_Parent (Index_Constraints, N);
615 Collect_Aggr_Bounds (N, 1);
617 -- Build the list of constrained indices of our aggregate itype
619 for J in 1 .. Aggr_Dimension loop
620 Create_Index : declare
621 Index_Base : constant Entity_Id :=
622 Base_Type (Etype (Aggr_Range (J)));
623 Index_Typ : Entity_Id;
625 begin
626 -- Construct the Index subtype
628 Index_Typ := Create_Itype (Subtype_Kind (Ekind (Index_Base)), N);
630 Set_Etype (Index_Typ, Index_Base);
632 if Is_Character_Type (Index_Base) then
633 Set_Is_Character_Type (Index_Typ);
634 end if;
636 Set_Size_Info (Index_Typ, (Index_Base));
637 Set_RM_Size (Index_Typ, RM_Size (Index_Base));
638 Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base));
639 Set_Scalar_Range (Index_Typ, Aggr_Range (J));
641 if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then
642 Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ)));
643 end if;
645 Set_Etype (Aggr_Range (J), Index_Typ);
647 Append (Aggr_Range (J), To => Index_Constraints);
648 end Create_Index;
649 end loop;
651 -- Now build the Itype
653 Itype := Create_Itype (E_Array_Subtype, N);
655 Set_First_Rep_Item (Itype, First_Rep_Item (Typ));
656 Set_Convention (Itype, Convention (Typ));
657 Set_Depends_On_Private (Itype, Has_Private_Component (Typ));
658 Set_Etype (Itype, Base_Type (Typ));
659 Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ));
660 Set_Is_Aliased (Itype, Is_Aliased (Typ));
661 Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
663 Copy_Suppress_Status (Index_Check, Typ, Itype);
664 Copy_Suppress_Status (Length_Check, Typ, Itype);
666 Set_First_Index (Itype, First (Index_Constraints));
667 Set_Is_Constrained (Itype, True);
668 Set_Is_Internal (Itype, True);
669 Init_Size_Align (Itype);
671 -- A simple optimization: purely positional aggregates of static
672 -- components should be passed to gigi unexpanded whenever possible,
673 -- and regardless of the staticness of the bounds themselves. Subse-
674 -- quent checks in exp_aggr verify that type is not packed, etc.
676 Set_Size_Known_At_Compile_Time (Itype,
677 Is_Fully_Positional
678 and then Comes_From_Source (N)
679 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
681 -- We always need a freeze node for a packed array subtype, so that
682 -- we can build the Packed_Array_Type corresponding to the subtype.
683 -- If expansion is disabled, the packed array subtype is not built,
684 -- and we must not generate a freeze node for the type, or else it
685 -- will appear incomplete to gigi.
687 if Is_Packed (Itype) and then not In_Default_Expression
688 and then Expander_Active
689 then
690 Freeze_Itype (Itype, N);
691 end if;
693 return Itype;
694 end Array_Aggr_Subtype;
696 --------------------------------
697 -- Check_Misspelled_Component --
698 --------------------------------
700 procedure Check_Misspelled_Component
701 (Elements : Elist_Id;
702 Component : Node_Id)
704 Max_Suggestions : constant := 2;
706 Nr_Of_Suggestions : Natural := 0;
707 Suggestion_1 : Entity_Id := Empty;
708 Suggestion_2 : Entity_Id := Empty;
709 Component_Elmt : Elmt_Id;
711 begin
712 -- All the components of List are matched against Component and
713 -- a count is maintained of possible misspellings. When at the
714 -- end of the analysis there are one or two (not more!) possible
715 -- misspellings, these misspellings will be suggested as
716 -- possible correction.
718 Get_Name_String (Chars (Component));
720 declare
721 S : constant String (1 .. Name_Len) :=
722 Name_Buffer (1 .. Name_Len);
724 begin
726 Component_Elmt := First_Elmt (Elements);
728 while Nr_Of_Suggestions <= Max_Suggestions
729 and then Present (Component_Elmt)
730 loop
732 Get_Name_String (Chars (Node (Component_Elmt)));
734 if Is_Bad_Spelling_Of (Name_Buffer (1 .. Name_Len), S) then
735 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
737 case Nr_Of_Suggestions is
738 when 1 => Suggestion_1 := Node (Component_Elmt);
739 when 2 => Suggestion_2 := Node (Component_Elmt);
740 when others => exit;
741 end case;
742 end if;
744 Next_Elmt (Component_Elmt);
745 end loop;
747 -- Report at most two suggestions
749 if Nr_Of_Suggestions = 1 then
750 Error_Msg_NE ("\possible misspelling of&",
751 Component, Suggestion_1);
753 elsif Nr_Of_Suggestions = 2 then
754 Error_Msg_Node_2 := Suggestion_2;
755 Error_Msg_NE ("\possible misspelling of& or&",
756 Component, Suggestion_1);
757 end if;
758 end;
759 end Check_Misspelled_Component;
761 ----------------------------------------
762 -- Check_Static_Discriminated_Subtype --
763 ----------------------------------------
765 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id) is
766 Disc : constant Entity_Id := First_Discriminant (T);
767 Comp : Entity_Id;
768 Ind : Entity_Id;
770 begin
771 if Has_Record_Rep_Clause (T) then
772 return;
774 elsif Present (Next_Discriminant (Disc)) then
775 return;
777 elsif Nkind (V) /= N_Integer_Literal then
778 return;
779 end if;
781 Comp := First_Component (T);
783 while Present (Comp) loop
785 if Is_Scalar_Type (Etype (Comp)) then
786 null;
788 elsif Is_Private_Type (Etype (Comp))
789 and then Present (Full_View (Etype (Comp)))
790 and then Is_Scalar_Type (Full_View (Etype (Comp)))
791 then
792 null;
794 elsif Is_Array_Type (Etype (Comp)) then
796 if Is_Bit_Packed_Array (Etype (Comp)) then
797 return;
798 end if;
800 Ind := First_Index (Etype (Comp));
802 while Present (Ind) loop
804 if Nkind (Ind) /= N_Range
805 or else Nkind (Low_Bound (Ind)) /= N_Integer_Literal
806 or else Nkind (High_Bound (Ind)) /= N_Integer_Literal
807 then
808 return;
809 end if;
811 Next_Index (Ind);
812 end loop;
814 else
815 return;
816 end if;
818 Next_Component (Comp);
819 end loop;
821 -- On exit, all components have statically known sizes
823 Set_Size_Known_At_Compile_Time (T);
824 end Check_Static_Discriminated_Subtype;
826 --------------------------------
827 -- Make_String_Into_Aggregate --
828 --------------------------------
830 procedure Make_String_Into_Aggregate (N : Node_Id) is
831 Exprs : constant List_Id := New_List;
832 Loc : constant Source_Ptr := Sloc (N);
833 Str : constant String_Id := Strval (N);
834 Strlen : constant Nat := String_Length (Str);
835 C : Char_Code;
836 C_Node : Node_Id;
837 New_N : Node_Id;
838 P : Source_Ptr;
840 begin
841 P := Loc + 1;
842 for J in 1 .. Strlen loop
843 C := Get_String_Char (Str, J);
844 Set_Character_Literal_Name (C);
846 C_Node :=
847 Make_Character_Literal (P,
848 Chars => Name_Find,
849 Char_Literal_Value => UI_From_CC (C));
850 Set_Etype (C_Node, Any_Character);
851 Append_To (Exprs, C_Node);
853 P := P + 1;
854 -- something special for wide strings ???
855 end loop;
857 New_N := Make_Aggregate (Loc, Expressions => Exprs);
858 Set_Analyzed (New_N);
859 Set_Etype (New_N, Any_Composite);
861 Rewrite (N, New_N);
862 end Make_String_Into_Aggregate;
864 -----------------------
865 -- Resolve_Aggregate --
866 -----------------------
868 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
869 Pkind : constant Node_Kind := Nkind (Parent (N));
871 Aggr_Subtyp : Entity_Id;
872 -- The actual aggregate subtype. This is not necessarily the same as Typ
873 -- which is the subtype of the context in which the aggregate was found.
875 begin
876 -- Check for aggregates not allowed in configurable run-time mode.
877 -- We allow all cases of aggregates that do not come from source,
878 -- since these are all assumed to be small (e.g. bounds of a string
879 -- literal). We also allow aggregates of types we know to be small.
881 if not Support_Aggregates_On_Target
882 and then Comes_From_Source (N)
883 and then (not Known_Static_Esize (Typ) or else Esize (Typ) > 64)
884 then
885 Error_Msg_CRT ("aggregate", N);
886 end if;
888 if Is_Limited_Composite (Typ) then
889 Error_Msg_N ("aggregate type cannot have limited component", N);
890 Explain_Limited_Type (Typ, N);
892 -- Ada 2005 (AI-287): Limited aggregates allowed
894 elsif Is_Limited_Type (Typ)
895 and Ada_Version < Ada_05
896 then
897 Error_Msg_N ("aggregate type cannot be limited", N);
898 Explain_Limited_Type (Typ, N);
900 elsif Is_Class_Wide_Type (Typ) then
901 Error_Msg_N ("type of aggregate cannot be class-wide", N);
903 elsif Typ = Any_String
904 or else Typ = Any_Composite
905 then
906 Error_Msg_N ("no unique type for aggregate", N);
907 Set_Etype (N, Any_Composite);
909 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
910 Error_Msg_N ("null record forbidden in array aggregate", N);
912 elsif Is_Record_Type (Typ) then
913 Resolve_Record_Aggregate (N, Typ);
915 elsif Is_Array_Type (Typ) then
917 -- First a special test, for the case of a positional aggregate
918 -- of characters which can be replaced by a string literal.
919 -- Do not perform this transformation if this was a string literal
920 -- to start with, whose components needed constraint checks, or if
921 -- the component type is non-static, because it will require those
922 -- checks and be transformed back into an aggregate.
924 if Number_Dimensions (Typ) = 1
925 and then
926 (Root_Type (Component_Type (Typ)) = Standard_Character
927 or else
928 Root_Type (Component_Type (Typ)) = Standard_Wide_Character
929 or else
930 Root_Type (Component_Type (Typ)) = Standard_Wide_Wide_Character)
931 and then No (Component_Associations (N))
932 and then not Is_Limited_Composite (Typ)
933 and then not Is_Private_Composite (Typ)
934 and then not Is_Bit_Packed_Array (Typ)
935 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
936 and then Is_Static_Subtype (Component_Type (Typ))
937 then
938 declare
939 Expr : Node_Id;
941 begin
942 Expr := First (Expressions (N));
943 while Present (Expr) loop
944 exit when Nkind (Expr) /= N_Character_Literal;
945 Next (Expr);
946 end loop;
948 if No (Expr) then
949 Start_String;
951 Expr := First (Expressions (N));
952 while Present (Expr) loop
953 Store_String_Char (UI_To_CC (Char_Literal_Value (Expr)));
954 Next (Expr);
955 end loop;
957 Rewrite (N,
958 Make_String_Literal (Sloc (N), End_String));
960 Analyze_And_Resolve (N, Typ);
961 return;
962 end if;
963 end;
964 end if;
966 -- Here if we have a real aggregate to deal with
968 Array_Aggregate : declare
969 Aggr_Resolved : Boolean;
971 Aggr_Typ : constant Entity_Id := Etype (Typ);
972 -- This is the unconstrained array type, which is the type
973 -- against which the aggregate is to be resolved. Typ itself
974 -- is the array type of the context which may not be the same
975 -- subtype as the subtype for the final aggregate.
977 begin
978 -- In the following we determine whether an others choice is
979 -- allowed inside the array aggregate. The test checks the context
980 -- in which the array aggregate occurs. If the context does not
981 -- permit it, or the aggregate type is unconstrained, an others
982 -- choice is not allowed.
984 -- If expansion is disabled (generic context, or semantics-only
985 -- mode) actual subtypes cannot be constructed, and the type of
986 -- an object may be its unconstrained nominal type. However, if
987 -- the context is an assignment, we assume that "others" is
988 -- allowed, because the target of the assignment will have a
989 -- constrained subtype when fully compiled.
991 -- Note that there is no node for Explicit_Actual_Parameter.
992 -- To test for this context we therefore have to test for node
993 -- N_Parameter_Association which itself appears only if there is a
994 -- formal parameter. Consequently we also need to test for
995 -- N_Procedure_Call_Statement or N_Function_Call.
997 Set_Etype (N, Aggr_Typ); -- may be overridden later on
999 if Is_Constrained (Typ) and then
1000 (Pkind = N_Assignment_Statement or else
1001 Pkind = N_Parameter_Association or else
1002 Pkind = N_Function_Call or else
1003 Pkind = N_Procedure_Call_Statement or else
1004 Pkind = N_Generic_Association or else
1005 Pkind = N_Formal_Object_Declaration or else
1006 Pkind = N_Return_Statement or else
1007 Pkind = N_Object_Declaration or else
1008 Pkind = N_Component_Declaration or else
1009 Pkind = N_Parameter_Specification or else
1010 Pkind = N_Qualified_Expression or else
1011 Pkind = N_Aggregate or else
1012 Pkind = N_Extension_Aggregate or else
1013 Pkind = N_Component_Association)
1014 then
1015 Aggr_Resolved :=
1016 Resolve_Array_Aggregate
1018 Index => First_Index (Aggr_Typ),
1019 Index_Constr => First_Index (Typ),
1020 Component_Typ => Component_Type (Typ),
1021 Others_Allowed => True);
1023 elsif not Expander_Active
1024 and then Pkind = N_Assignment_Statement
1025 then
1026 Aggr_Resolved :=
1027 Resolve_Array_Aggregate
1029 Index => First_Index (Aggr_Typ),
1030 Index_Constr => First_Index (Typ),
1031 Component_Typ => Component_Type (Typ),
1032 Others_Allowed => True);
1033 else
1034 Aggr_Resolved :=
1035 Resolve_Array_Aggregate
1037 Index => First_Index (Aggr_Typ),
1038 Index_Constr => First_Index (Aggr_Typ),
1039 Component_Typ => Component_Type (Typ),
1040 Others_Allowed => False);
1041 end if;
1043 if not Aggr_Resolved then
1044 Aggr_Subtyp := Any_Composite;
1045 else
1046 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1047 end if;
1049 Set_Etype (N, Aggr_Subtyp);
1050 end Array_Aggregate;
1052 elsif Is_Private_Type (Typ)
1053 and then Present (Full_View (Typ))
1054 and then In_Inlined_Body
1055 and then Is_Composite_Type (Full_View (Typ))
1056 then
1057 Resolve (N, Full_View (Typ));
1059 else
1060 Error_Msg_N ("illegal context for aggregate", N);
1061 end if;
1063 -- If we can determine statically that the evaluation of the
1064 -- aggregate raises Constraint_Error, then replace the
1065 -- aggregate with an N_Raise_Constraint_Error node, but set the
1066 -- Etype to the right aggregate subtype. Gigi needs this.
1068 if Raises_Constraint_Error (N) then
1069 Aggr_Subtyp := Etype (N);
1070 Rewrite (N,
1071 Make_Raise_Constraint_Error (Sloc (N),
1072 Reason => CE_Range_Check_Failed));
1073 Set_Raises_Constraint_Error (N);
1074 Set_Etype (N, Aggr_Subtyp);
1075 Set_Analyzed (N);
1076 end if;
1077 end Resolve_Aggregate;
1079 -----------------------------
1080 -- Resolve_Array_Aggregate --
1081 -----------------------------
1083 function Resolve_Array_Aggregate
1084 (N : Node_Id;
1085 Index : Node_Id;
1086 Index_Constr : Node_Id;
1087 Component_Typ : Entity_Id;
1088 Others_Allowed : Boolean)
1089 return Boolean
1091 Loc : constant Source_Ptr := Sloc (N);
1093 Failure : constant Boolean := False;
1094 Success : constant Boolean := True;
1096 Index_Typ : constant Entity_Id := Etype (Index);
1097 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1098 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1099 -- The type of the index corresponding to the array sub-aggregate
1100 -- along with its low and upper bounds
1102 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1103 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1104 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1105 -- ditto for the base type
1107 function Add (Val : Uint; To : Node_Id) return Node_Id;
1108 -- Creates a new expression node where Val is added to expression To.
1109 -- Tries to constant fold whenever possible. To must be an already
1110 -- analyzed expression.
1112 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1113 -- Checks that AH (the upper bound of an array aggregate) is <= BH
1114 -- (the upper bound of the index base type). If the check fails a
1115 -- warning is emitted, the Raises_Constraint_Error Flag of N is set,
1116 -- and AH is replaced with a duplicate of BH.
1118 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1119 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1120 -- warning if not and sets the Raises_Constraint_Error Flag in N.
1122 procedure Check_Length (L, H : Node_Id; Len : Uint);
1123 -- Checks that range L .. H contains at least Len elements. Emits a
1124 -- warning if not and sets the Raises_Constraint_Error Flag in N.
1126 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1127 -- Returns True if range L .. H is dynamic or null
1129 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1130 -- Given expression node From, this routine sets OK to False if it
1131 -- cannot statically evaluate From. Otherwise it stores this static
1132 -- value into Value.
1134 function Resolve_Aggr_Expr
1135 (Expr : Node_Id;
1136 Single_Elmt : Boolean)
1137 return Boolean;
1138 -- Resolves aggregate expression Expr. Returs False if resolution
1139 -- fails. If Single_Elmt is set to False, the expression Expr may be
1140 -- used to initialize several array aggregate elements (this can
1141 -- happen for discrete choices such as "L .. H => Expr" or the others
1142 -- choice). In this event we do not resolve Expr unless expansion is
1143 -- disabled. To know why, see the DELAYED COMPONENT RESOLUTION
1144 -- note above.
1146 ---------
1147 -- Add --
1148 ---------
1150 function Add (Val : Uint; To : Node_Id) return Node_Id is
1151 Expr_Pos : Node_Id;
1152 Expr : Node_Id;
1153 To_Pos : Node_Id;
1155 begin
1156 if Raises_Constraint_Error (To) then
1157 return To;
1158 end if;
1160 -- First test if we can do constant folding
1162 if Compile_Time_Known_Value (To)
1163 or else Nkind (To) = N_Integer_Literal
1164 then
1165 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1166 Set_Is_Static_Expression (Expr_Pos);
1167 Set_Etype (Expr_Pos, Etype (To));
1168 Set_Analyzed (Expr_Pos, Analyzed (To));
1170 if not Is_Enumeration_Type (Index_Typ) then
1171 Expr := Expr_Pos;
1173 -- If we are dealing with enumeration return
1174 -- Index_Typ'Val (Expr_Pos)
1176 else
1177 Expr :=
1178 Make_Attribute_Reference
1179 (Loc,
1180 Prefix => New_Reference_To (Index_Typ, Loc),
1181 Attribute_Name => Name_Val,
1182 Expressions => New_List (Expr_Pos));
1183 end if;
1185 return Expr;
1186 end if;
1188 -- If we are here no constant folding possible
1190 if not Is_Enumeration_Type (Index_Base) then
1191 Expr :=
1192 Make_Op_Add (Loc,
1193 Left_Opnd => Duplicate_Subexpr (To),
1194 Right_Opnd => Make_Integer_Literal (Loc, Val));
1196 -- If we are dealing with enumeration return
1197 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1199 else
1200 To_Pos :=
1201 Make_Attribute_Reference
1202 (Loc,
1203 Prefix => New_Reference_To (Index_Typ, Loc),
1204 Attribute_Name => Name_Pos,
1205 Expressions => New_List (Duplicate_Subexpr (To)));
1207 Expr_Pos :=
1208 Make_Op_Add (Loc,
1209 Left_Opnd => To_Pos,
1210 Right_Opnd => Make_Integer_Literal (Loc, Val));
1212 Expr :=
1213 Make_Attribute_Reference
1214 (Loc,
1215 Prefix => New_Reference_To (Index_Typ, Loc),
1216 Attribute_Name => Name_Val,
1217 Expressions => New_List (Expr_Pos));
1218 end if;
1220 return Expr;
1221 end Add;
1223 -----------------
1224 -- Check_Bound --
1225 -----------------
1227 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1228 Val_BH : Uint;
1229 Val_AH : Uint;
1231 OK_BH : Boolean;
1232 OK_AH : Boolean;
1234 begin
1235 Get (Value => Val_BH, From => BH, OK => OK_BH);
1236 Get (Value => Val_AH, From => AH, OK => OK_AH);
1238 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1239 Set_Raises_Constraint_Error (N);
1240 Error_Msg_N ("upper bound out of range?", AH);
1241 Error_Msg_N ("Constraint_Error will be raised at run-time?", AH);
1243 -- You need to set AH to BH or else in the case of enumerations
1244 -- indices we will not be able to resolve the aggregate bounds.
1246 AH := Duplicate_Subexpr (BH);
1247 end if;
1248 end Check_Bound;
1250 ------------------
1251 -- Check_Bounds --
1252 ------------------
1254 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1255 Val_L : Uint;
1256 Val_H : Uint;
1257 Val_AL : Uint;
1258 Val_AH : Uint;
1260 OK_L : Boolean;
1261 OK_H : Boolean;
1262 OK_AL : Boolean;
1263 OK_AH : Boolean;
1265 begin
1266 if Raises_Constraint_Error (N)
1267 or else Dynamic_Or_Null_Range (AL, AH)
1268 then
1269 return;
1270 end if;
1272 Get (Value => Val_L, From => L, OK => OK_L);
1273 Get (Value => Val_H, From => H, OK => OK_H);
1275 Get (Value => Val_AL, From => AL, OK => OK_AL);
1276 Get (Value => Val_AH, From => AH, OK => OK_AH);
1278 if OK_L and then Val_L > Val_AL then
1279 Set_Raises_Constraint_Error (N);
1280 Error_Msg_N ("lower bound of aggregate out of range?", N);
1281 Error_Msg_N ("\Constraint_Error will be raised at run-time?", N);
1282 end if;
1284 if OK_H and then Val_H < Val_AH then
1285 Set_Raises_Constraint_Error (N);
1286 Error_Msg_N ("upper bound of aggregate out of range?", N);
1287 Error_Msg_N ("\Constraint_Error will be raised at run-time?", N);
1288 end if;
1289 end Check_Bounds;
1291 ------------------
1292 -- Check_Length --
1293 ------------------
1295 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1296 Val_L : Uint;
1297 Val_H : Uint;
1299 OK_L : Boolean;
1300 OK_H : Boolean;
1302 Range_Len : Uint;
1304 begin
1305 if Raises_Constraint_Error (N) then
1306 return;
1307 end if;
1309 Get (Value => Val_L, From => L, OK => OK_L);
1310 Get (Value => Val_H, From => H, OK => OK_H);
1312 if not OK_L or else not OK_H then
1313 return;
1314 end if;
1316 -- If null range length is zero
1318 if Val_L > Val_H then
1319 Range_Len := Uint_0;
1320 else
1321 Range_Len := Val_H - Val_L + 1;
1322 end if;
1324 if Range_Len < Len then
1325 Set_Raises_Constraint_Error (N);
1326 Error_Msg_N ("too many elements?", N);
1327 Error_Msg_N ("Constraint_Error will be raised at run-time?", N);
1328 end if;
1329 end Check_Length;
1331 ---------------------------
1332 -- Dynamic_Or_Null_Range --
1333 ---------------------------
1335 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1336 Val_L : Uint;
1337 Val_H : Uint;
1339 OK_L : Boolean;
1340 OK_H : Boolean;
1342 begin
1343 Get (Value => Val_L, From => L, OK => OK_L);
1344 Get (Value => Val_H, From => H, OK => OK_H);
1346 return not OK_L or else not OK_H
1347 or else not Is_OK_Static_Expression (L)
1348 or else not Is_OK_Static_Expression (H)
1349 or else Val_L > Val_H;
1350 end Dynamic_Or_Null_Range;
1352 ---------
1353 -- Get --
1354 ---------
1356 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1357 begin
1358 OK := True;
1360 if Compile_Time_Known_Value (From) then
1361 Value := Expr_Value (From);
1363 -- If expression From is something like Some_Type'Val (10) then
1364 -- Value = 10
1366 elsif Nkind (From) = N_Attribute_Reference
1367 and then Attribute_Name (From) = Name_Val
1368 and then Compile_Time_Known_Value (First (Expressions (From)))
1369 then
1370 Value := Expr_Value (First (Expressions (From)));
1372 else
1373 Value := Uint_0;
1374 OK := False;
1375 end if;
1376 end Get;
1378 -----------------------
1379 -- Resolve_Aggr_Expr --
1380 -----------------------
1382 function Resolve_Aggr_Expr
1383 (Expr : Node_Id;
1384 Single_Elmt : Boolean)
1385 return Boolean
1387 Nxt_Ind : constant Node_Id := Next_Index (Index);
1388 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
1389 -- Index is the current index corresponding to the expresion
1391 Resolution_OK : Boolean := True;
1392 -- Set to False if resolution of the expression failed
1394 begin
1395 -- If the array type against which we are resolving the aggregate
1396 -- has several dimensions, the expressions nested inside the
1397 -- aggregate must be further aggregates (or strings).
1399 if Present (Nxt_Ind) then
1400 if Nkind (Expr) /= N_Aggregate then
1402 -- A string literal can appear where a one-dimensional array
1403 -- of characters is expected. If the literal looks like an
1404 -- operator, it is still an operator symbol, which will be
1405 -- transformed into a string when analyzed.
1407 if Is_Character_Type (Component_Typ)
1408 and then No (Next_Index (Nxt_Ind))
1409 and then (Nkind (Expr) = N_String_Literal
1410 or else Nkind (Expr) = N_Operator_Symbol)
1411 then
1412 -- A string literal used in a multidimensional array
1413 -- aggregate in place of the final one-dimensional
1414 -- aggregate must not be enclosed in parentheses.
1416 if Paren_Count (Expr) /= 0 then
1417 Error_Msg_N ("no parenthesis allowed here", Expr);
1418 end if;
1420 Make_String_Into_Aggregate (Expr);
1422 else
1423 Error_Msg_N ("nested array aggregate expected", Expr);
1424 return Failure;
1425 end if;
1426 end if;
1428 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
1429 -- Required to check the null-exclusion attribute (if present).
1430 -- This value may be overridden later on.
1432 Set_Etype (Expr, Etype (N));
1434 Resolution_OK := Resolve_Array_Aggregate
1435 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1437 -- Do not resolve the expressions of discrete or others choices
1438 -- unless the expression covers a single component, or the expander
1439 -- is inactive.
1441 elsif Single_Elmt
1442 or else not Expander_Active
1443 or else In_Default_Expression
1444 then
1445 Analyze_And_Resolve (Expr, Component_Typ);
1446 Check_Non_Static_Context (Expr);
1447 Aggregate_Constraint_Checks (Expr, Component_Typ);
1448 Check_Unset_Reference (Expr);
1449 end if;
1451 if Raises_Constraint_Error (Expr)
1452 and then Nkind (Parent (Expr)) /= N_Component_Association
1453 then
1454 Set_Raises_Constraint_Error (N);
1455 end if;
1457 return Resolution_OK;
1458 end Resolve_Aggr_Expr;
1460 -- Variables local to Resolve_Array_Aggregate
1462 Assoc : Node_Id;
1463 Choice : Node_Id;
1464 Expr : Node_Id;
1466 Who_Cares : Node_Id;
1468 Aggr_Low : Node_Id := Empty;
1469 Aggr_High : Node_Id := Empty;
1470 -- The actual low and high bounds of this sub-aggegate
1472 Choices_Low : Node_Id := Empty;
1473 Choices_High : Node_Id := Empty;
1474 -- The lowest and highest discrete choices values for a named aggregate
1476 Nb_Elements : Uint := Uint_0;
1477 -- The number of elements in a positional aggegate
1479 Others_Present : Boolean := False;
1481 Nb_Choices : Nat := 0;
1482 -- Contains the overall number of named choices in this sub-aggregate
1484 Nb_Discrete_Choices : Nat := 0;
1485 -- The overall number of discrete choices (not counting others choice)
1487 Case_Table_Size : Nat;
1488 -- Contains the size of the case table needed to sort aggregate choices
1490 -- Start of processing for Resolve_Array_Aggregate
1492 begin
1493 -- STEP 1: make sure the aggregate is correctly formatted
1495 if Present (Component_Associations (N)) then
1496 Assoc := First (Component_Associations (N));
1497 while Present (Assoc) loop
1498 Choice := First (Choices (Assoc));
1499 while Present (Choice) loop
1500 if Nkind (Choice) = N_Others_Choice then
1501 Others_Present := True;
1503 if Choice /= First (Choices (Assoc))
1504 or else Present (Next (Choice))
1505 then
1506 Error_Msg_N
1507 ("OTHERS must appear alone in a choice list", Choice);
1508 return Failure;
1509 end if;
1511 if Present (Next (Assoc)) then
1512 Error_Msg_N
1513 ("OTHERS must appear last in an aggregate", Choice);
1514 return Failure;
1515 end if;
1517 if Ada_Version = Ada_83
1518 and then Assoc /= First (Component_Associations (N))
1519 and then (Nkind (Parent (N)) = N_Assignment_Statement
1520 or else
1521 Nkind (Parent (N)) = N_Object_Declaration)
1522 then
1523 Error_Msg_N
1524 ("(Ada 83) illegal context for OTHERS choice", N);
1525 end if;
1526 end if;
1528 Nb_Choices := Nb_Choices + 1;
1529 Next (Choice);
1530 end loop;
1532 Next (Assoc);
1533 end loop;
1534 end if;
1536 -- At this point we know that the others choice, if present, is by
1537 -- itself and appears last in the aggregate. Check if we have mixed
1538 -- positional and discrete associations (other than the others choice).
1540 if Present (Expressions (N))
1541 and then (Nb_Choices > 1
1542 or else (Nb_Choices = 1 and then not Others_Present))
1543 then
1544 Error_Msg_N
1545 ("named association cannot follow positional association",
1546 First (Choices (First (Component_Associations (N)))));
1547 return Failure;
1548 end if;
1550 -- Test for the validity of an others choice if present
1552 if Others_Present and then not Others_Allowed then
1553 Error_Msg_N
1554 ("OTHERS choice not allowed here",
1555 First (Choices (First (Component_Associations (N)))));
1556 return Failure;
1557 end if;
1559 -- Protect against cascaded errors
1561 if Etype (Index_Typ) = Any_Type then
1562 return Failure;
1563 end if;
1565 -- STEP 2: Process named components
1567 if No (Expressions (N)) then
1569 if Others_Present then
1570 Case_Table_Size := Nb_Choices - 1;
1571 else
1572 Case_Table_Size := Nb_Choices;
1573 end if;
1575 Step_2 : declare
1576 Low : Node_Id;
1577 High : Node_Id;
1578 -- Denote the lowest and highest values in an aggregate choice
1580 Hi_Val : Uint;
1581 Lo_Val : Uint;
1582 -- High end of one range and Low end of the next. Should be
1583 -- contiguous if there is no hole in the list of values.
1585 Missing_Values : Boolean;
1586 -- Set True if missing index values
1588 S_Low : Node_Id := Empty;
1589 S_High : Node_Id := Empty;
1590 -- if a choice in an aggregate is a subtype indication these
1591 -- denote the lowest and highest values of the subtype
1593 Table : Case_Table_Type (1 .. Case_Table_Size);
1594 -- Used to sort all the different choice values
1596 Single_Choice : Boolean;
1597 -- Set to true every time there is a single discrete choice in a
1598 -- discrete association
1600 Prev_Nb_Discrete_Choices : Nat;
1601 -- Used to keep track of the number of discrete choices
1602 -- in the current association.
1604 begin
1605 -- STEP 2 (A): Check discrete choices validity
1607 Assoc := First (Component_Associations (N));
1608 while Present (Assoc) loop
1610 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
1611 Choice := First (Choices (Assoc));
1612 loop
1613 Analyze (Choice);
1615 if Nkind (Choice) = N_Others_Choice then
1616 Single_Choice := False;
1617 exit;
1619 -- Test for subtype mark without constraint
1621 elsif Is_Entity_Name (Choice) and then
1622 Is_Type (Entity (Choice))
1623 then
1624 if Base_Type (Entity (Choice)) /= Index_Base then
1625 Error_Msg_N
1626 ("invalid subtype mark in aggregate choice",
1627 Choice);
1628 return Failure;
1629 end if;
1631 elsif Nkind (Choice) = N_Subtype_Indication then
1632 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
1634 -- Does the subtype indication evaluation raise CE ?
1636 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
1637 Get_Index_Bounds (Choice, Low, High);
1638 Check_Bounds (S_Low, S_High, Low, High);
1640 else -- Choice is a range or an expression
1641 Resolve (Choice, Index_Base);
1642 Check_Unset_Reference (Choice);
1643 Check_Non_Static_Context (Choice);
1645 -- Do not range check a choice. This check is redundant
1646 -- since this test is already performed when we check
1647 -- that the bounds of the array aggregate are within
1648 -- range.
1650 Set_Do_Range_Check (Choice, False);
1651 end if;
1653 -- If we could not resolve the discrete choice stop here
1655 if Etype (Choice) = Any_Type then
1656 return Failure;
1658 -- If the discrete choice raises CE get its original bounds
1660 elsif Nkind (Choice) = N_Raise_Constraint_Error then
1661 Set_Raises_Constraint_Error (N);
1662 Get_Index_Bounds (Original_Node (Choice), Low, High);
1664 -- Otherwise get its bounds as usual
1666 else
1667 Get_Index_Bounds (Choice, Low, High);
1668 end if;
1670 if (Dynamic_Or_Null_Range (Low, High)
1671 or else (Nkind (Choice) = N_Subtype_Indication
1672 and then
1673 Dynamic_Or_Null_Range (S_Low, S_High)))
1674 and then Nb_Choices /= 1
1675 then
1676 Error_Msg_N
1677 ("dynamic or empty choice in aggregate " &
1678 "must be the only choice", Choice);
1679 return Failure;
1680 end if;
1682 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
1683 Table (Nb_Discrete_Choices).Choice_Lo := Low;
1684 Table (Nb_Discrete_Choices).Choice_Hi := High;
1686 Next (Choice);
1688 if No (Choice) then
1689 -- Check if we have a single discrete choice and whether
1690 -- this discrete choice specifies a single value.
1692 Single_Choice :=
1693 (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1)
1694 and then (Low = High);
1696 exit;
1697 end if;
1698 end loop;
1700 -- Ada 2005 (AI-231)
1702 if Ada_Version >= Ada_05
1703 and then Nkind (Expression (Assoc)) = N_Null
1704 then
1705 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
1706 end if;
1708 -- Ada 2005 (AI-287): In case of default initialized component
1709 -- we delay the resolution to the expansion phase
1711 if Box_Present (Assoc) then
1713 -- Ada 2005 (AI-287): In case of default initialization
1714 -- of a component the expander will generate calls to
1715 -- the corresponding initialization subprogram.
1717 null;
1719 elsif not Resolve_Aggr_Expr (Expression (Assoc),
1720 Single_Elmt => Single_Choice)
1721 then
1722 return Failure;
1723 end if;
1725 Next (Assoc);
1726 end loop;
1728 -- If aggregate contains more than one choice then these must be
1729 -- static. Sort them and check that they are contiguous
1731 if Nb_Discrete_Choices > 1 then
1732 Sort_Case_Table (Table);
1733 Missing_Values := False;
1735 Outer : for J in 1 .. Nb_Discrete_Choices - 1 loop
1736 if Expr_Value (Table (J).Choice_Hi) >=
1737 Expr_Value (Table (J + 1).Choice_Lo)
1738 then
1739 Error_Msg_N
1740 ("duplicate choice values in array aggregate",
1741 Table (J).Choice_Hi);
1742 return Failure;
1744 elsif not Others_Present then
1746 Hi_Val := Expr_Value (Table (J).Choice_Hi);
1747 Lo_Val := Expr_Value (Table (J + 1).Choice_Lo);
1749 -- If missing values, output error messages
1751 if Lo_Val - Hi_Val > 1 then
1753 -- Header message if not first missing value
1755 if not Missing_Values then
1756 Error_Msg_N
1757 ("missing index value(s) in array aggregate", N);
1758 Missing_Values := True;
1759 end if;
1761 -- Output values of missing indexes
1763 Lo_Val := Lo_Val - 1;
1764 Hi_Val := Hi_Val + 1;
1766 -- Enumeration type case
1768 if Is_Enumeration_Type (Index_Typ) then
1769 Error_Msg_Name_1 :=
1770 Chars
1771 (Get_Enum_Lit_From_Pos
1772 (Index_Typ, Hi_Val, Loc));
1774 if Lo_Val = Hi_Val then
1775 Error_Msg_N ("\ %", N);
1776 else
1777 Error_Msg_Name_2 :=
1778 Chars
1779 (Get_Enum_Lit_From_Pos
1780 (Index_Typ, Lo_Val, Loc));
1781 Error_Msg_N ("\ % .. %", N);
1782 end if;
1784 -- Integer types case
1786 else
1787 Error_Msg_Uint_1 := Hi_Val;
1789 if Lo_Val = Hi_Val then
1790 Error_Msg_N ("\ ^", N);
1791 else
1792 Error_Msg_Uint_2 := Lo_Val;
1793 Error_Msg_N ("\ ^ .. ^", N);
1794 end if;
1795 end if;
1796 end if;
1797 end if;
1798 end loop Outer;
1800 if Missing_Values then
1801 Set_Etype (N, Any_Composite);
1802 return Failure;
1803 end if;
1804 end if;
1806 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
1808 if Nb_Discrete_Choices > 0 then
1809 Choices_Low := Table (1).Choice_Lo;
1810 Choices_High := Table (Nb_Discrete_Choices).Choice_Hi;
1811 end if;
1813 if Others_Present then
1814 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
1816 else
1817 Aggr_Low := Choices_Low;
1818 Aggr_High := Choices_High;
1819 end if;
1820 end Step_2;
1822 -- STEP 3: Process positional components
1824 else
1825 -- STEP 3 (A): Process positional elements
1827 Expr := First (Expressions (N));
1828 Nb_Elements := Uint_0;
1829 while Present (Expr) loop
1830 Nb_Elements := Nb_Elements + 1;
1832 -- Ada 2005 (AI-231)
1834 if Ada_Version >= Ada_05
1835 and then Nkind (Expr) = N_Null
1836 then
1837 Check_Can_Never_Be_Null (Etype (N), Expr);
1838 end if;
1840 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
1841 return Failure;
1842 end if;
1844 Next (Expr);
1845 end loop;
1847 if Others_Present then
1848 Assoc := Last (Component_Associations (N));
1850 -- Ada 2005 (AI-231)
1852 if Ada_Version >= Ada_05
1853 and then Nkind (Expression (Assoc)) = N_Null
1854 then
1855 Check_Can_Never_Be_Null
1856 (Etype (N), Expression (Assoc));
1857 end if;
1859 -- Ada 2005 (AI-287): In case of default initialized component
1860 -- we delay the resolution to the expansion phase.
1862 if Box_Present (Assoc) then
1864 -- Ada 2005 (AI-287): In case of default initialization
1865 -- of a component the expander will generate calls to
1866 -- the corresponding initialization subprogram.
1868 null;
1870 elsif not Resolve_Aggr_Expr (Expression (Assoc),
1871 Single_Elmt => False)
1872 then
1873 return Failure;
1874 end if;
1875 end if;
1877 -- STEP 3 (B): Compute the aggregate bounds
1879 if Others_Present then
1880 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
1882 else
1883 if Others_Allowed then
1884 Get_Index_Bounds (Index_Constr, Aggr_Low, Who_Cares);
1885 else
1886 Aggr_Low := Index_Typ_Low;
1887 end if;
1889 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
1890 Check_Bound (Index_Base_High, Aggr_High);
1891 end if;
1892 end if;
1894 -- STEP 4: Perform static aggregate checks and save the bounds
1896 -- Check (A)
1898 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
1899 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
1901 -- Check (B)
1903 if Others_Present and then Nb_Discrete_Choices > 0 then
1904 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
1905 Check_Bounds (Index_Typ_Low, Index_Typ_High,
1906 Choices_Low, Choices_High);
1907 Check_Bounds (Index_Base_Low, Index_Base_High,
1908 Choices_Low, Choices_High);
1910 -- Check (C)
1912 elsif Others_Present and then Nb_Elements > 0 then
1913 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
1914 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
1915 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
1916 end if;
1918 if Raises_Constraint_Error (Aggr_Low)
1919 or else Raises_Constraint_Error (Aggr_High)
1920 then
1921 Set_Raises_Constraint_Error (N);
1922 end if;
1924 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
1926 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
1927 -- since the addition node returned by Add is not yet analyzed. Attach
1928 -- to tree and analyze first. Reset analyzed flag to insure it will get
1929 -- analyzed when it is a literal bound whose type must be properly
1930 -- set.
1932 if Others_Present or else Nb_Discrete_Choices > 0 then
1933 Aggr_High := Duplicate_Subexpr (Aggr_High);
1935 if Etype (Aggr_High) = Universal_Integer then
1936 Set_Analyzed (Aggr_High, False);
1937 end if;
1938 end if;
1940 Set_Aggregate_Bounds
1941 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
1943 -- The bounds may contain expressions that must be inserted upwards.
1944 -- Attach them fully to the tree. After analysis, remove side effects
1945 -- from upper bound, if still needed.
1947 Set_Parent (Aggregate_Bounds (N), N);
1948 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
1949 Check_Unset_Reference (Aggregate_Bounds (N));
1951 if not Others_Present and then Nb_Discrete_Choices = 0 then
1952 Set_High_Bound (Aggregate_Bounds (N),
1953 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
1954 end if;
1956 return Success;
1957 end Resolve_Array_Aggregate;
1959 ---------------------------------
1960 -- Resolve_Extension_Aggregate --
1961 ---------------------------------
1963 -- There are two cases to consider:
1965 -- a) If the ancestor part is a type mark, the components needed are
1966 -- the difference between the components of the expected type and the
1967 -- components of the given type mark.
1969 -- b) If the ancestor part is an expression, it must be unambiguous,
1970 -- and once we have its type we can also compute the needed components
1971 -- as in the previous case. In both cases, if the ancestor type is not
1972 -- the immediate ancestor, we have to build this ancestor recursively.
1974 -- In both cases discriminants of the ancestor type do not play a
1975 -- role in the resolution of the needed components, because inherited
1976 -- discriminants cannot be used in a type extension. As a result we can
1977 -- compute independently the list of components of the ancestor type and
1978 -- of the expected type.
1980 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
1981 A : constant Node_Id := Ancestor_Part (N);
1982 A_Type : Entity_Id;
1983 I : Interp_Index;
1984 It : Interp;
1986 function Valid_Ancestor_Type return Boolean;
1987 -- Verify that the type of the ancestor part is a non-private ancestor
1988 -- of the expected type.
1990 -------------------------
1991 -- Valid_Ancestor_Type --
1992 -------------------------
1994 function Valid_Ancestor_Type return Boolean is
1995 Imm_Type : Entity_Id;
1997 begin
1998 Imm_Type := Base_Type (Typ);
1999 while Is_Derived_Type (Imm_Type)
2000 and then Etype (Imm_Type) /= Base_Type (A_Type)
2001 loop
2002 Imm_Type := Etype (Base_Type (Imm_Type));
2003 end loop;
2005 if Etype (Imm_Type) /= Base_Type (A_Type) then
2006 Error_Msg_NE ("expect ancestor type of &", A, Typ);
2007 return False;
2008 else
2009 return True;
2010 end if;
2011 end Valid_Ancestor_Type;
2013 -- Start of processing for Resolve_Extension_Aggregate
2015 begin
2016 Analyze (A);
2018 if not Is_Tagged_Type (Typ) then
2019 Error_Msg_N ("type of extension aggregate must be tagged", N);
2020 return;
2022 elsif Is_Limited_Type (Typ) then
2024 -- Ada 2005 (AI-287): Limited aggregates are allowed
2026 if Ada_Version < Ada_05 then
2027 Error_Msg_N ("aggregate type cannot be limited", N);
2028 Explain_Limited_Type (Typ, N);
2029 return;
2030 end if;
2032 elsif Is_Class_Wide_Type (Typ) then
2033 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
2034 return;
2035 end if;
2037 if Is_Entity_Name (A)
2038 and then Is_Type (Entity (A))
2039 then
2040 A_Type := Get_Full_View (Entity (A));
2042 if Valid_Ancestor_Type then
2043 Set_Entity (A, A_Type);
2044 Set_Etype (A, A_Type);
2046 Validate_Ancestor_Part (N);
2047 Resolve_Record_Aggregate (N, Typ);
2048 end if;
2050 elsif Nkind (A) /= N_Aggregate then
2051 if Is_Overloaded (A) then
2052 A_Type := Any_Type;
2053 Get_First_Interp (A, I, It);
2055 while Present (It.Typ) loop
2057 if Is_Tagged_Type (It.Typ)
2058 and then not Is_Limited_Type (It.Typ)
2059 then
2060 if A_Type /= Any_Type then
2061 Error_Msg_N ("cannot resolve expression", A);
2062 return;
2063 else
2064 A_Type := It.Typ;
2065 end if;
2066 end if;
2068 Get_Next_Interp (I, It);
2069 end loop;
2071 if A_Type = Any_Type then
2072 Error_Msg_N
2073 ("ancestor part must be non-limited tagged type", A);
2074 return;
2075 end if;
2077 else
2078 A_Type := Etype (A);
2079 end if;
2081 if Valid_Ancestor_Type then
2082 Resolve (A, A_Type);
2083 Check_Unset_Reference (A);
2084 Check_Non_Static_Context (A);
2086 if Is_Class_Wide_Type (Etype (A))
2087 and then Nkind (Original_Node (A)) = N_Function_Call
2088 then
2089 -- If the ancestor part is a dispatching call, it appears
2090 -- statically to be a legal ancestor, but it yields any
2091 -- member of the class, and it is not possible to determine
2092 -- whether it is an ancestor of the extension aggregate (much
2093 -- less which ancestor). It is not possible to determine the
2094 -- required components of the extension part.
2096 -- This check implements AI-306, which in fact was motivated
2097 -- by an ACT query to the ARG after this test was added.
2099 Error_Msg_N ("ancestor part must be statically tagged", A);
2100 else
2101 Resolve_Record_Aggregate (N, Typ);
2102 end if;
2103 end if;
2105 else
2106 Error_Msg_N (" No unique type for this aggregate", A);
2107 end if;
2108 end Resolve_Extension_Aggregate;
2110 ------------------------------
2111 -- Resolve_Record_Aggregate --
2112 ------------------------------
2114 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
2115 New_Assoc_List : constant List_Id := New_List;
2116 New_Assoc : Node_Id;
2117 -- New_Assoc_List is the newly built list of N_Component_Association
2118 -- nodes. New_Assoc is one such N_Component_Association node in it.
2119 -- Please note that while Assoc and New_Assoc contain the same
2120 -- kind of nodes, they are used to iterate over two different
2121 -- N_Component_Association lists.
2123 Others_Etype : Entity_Id := Empty;
2124 -- This variable is used to save the Etype of the last record component
2125 -- that takes its value from the others choice. Its purpose is:
2127 -- (a) make sure the others choice is useful
2129 -- (b) make sure the type of all the components whose value is
2130 -- subsumed by the others choice are the same.
2132 -- This variable is updated as a side effect of function Get_Value
2134 Mbox_Present : Boolean := False;
2135 Others_Mbox : Boolean := False;
2136 -- Ada 2005 (AI-287): Variables used in case of default initialization
2137 -- to provide a functionality similar to Others_Etype. Mbox_Present
2138 -- indicates that the component takes its default initialization;
2139 -- Others_Mbox indicates that at least one component takes its default
2140 -- initialization. Similar to Others_Etype, they are also updated as a
2141 -- side effect of function Get_Value.
2143 procedure Add_Association
2144 (Component : Entity_Id;
2145 Expr : Node_Id;
2146 Box_Present : Boolean := False);
2147 -- Builds a new N_Component_Association node which associates
2148 -- Component to expression Expr and adds it to the new association
2149 -- list New_Assoc_List being built.
2151 function Discr_Present (Discr : Entity_Id) return Boolean;
2152 -- If aggregate N is a regular aggregate this routine will return True.
2153 -- Otherwise, if N is an extension aggregate, Discr is a discriminant
2154 -- whose value may already have been specified by N's ancestor part,
2155 -- this routine checks whether this is indeed the case and if so
2156 -- returns False, signaling that no value for Discr should appear in the
2157 -- N's aggregate part. Also, in this case, the routine appends to
2158 -- New_Assoc_List Discr the discriminant value specified in the ancestor
2159 -- part.
2161 function Get_Value
2162 (Compon : Node_Id;
2163 From : List_Id;
2164 Consider_Others_Choice : Boolean := False)
2165 return Node_Id;
2166 -- Given a record component stored in parameter Compon, the
2167 -- following function returns its value as it appears in the list
2168 -- From, which is a list of N_Component_Association nodes. If no
2169 -- component association has a choice for the searched component,
2170 -- the value provided by the others choice is returned, if there
2171 -- is one and Consider_Others_Choice is set to true. Otherwise
2172 -- Empty is returned. If there is more than one component association
2173 -- giving a value for the searched record component, an error message
2174 -- is emitted and the first found value is returned.
2176 -- If Consider_Others_Choice is set and the returned expression comes
2177 -- from the others choice, then Others_Etype is set as a side effect.
2178 -- An error message is emitted if the components taking their value
2179 -- from the others choice do not have same type.
2181 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id);
2182 -- Analyzes and resolves expression Expr against the Etype of the
2183 -- Component. This routine also applies all appropriate checks to Expr.
2184 -- It finally saves a Expr in the newly created association list that
2185 -- will be attached to the final record aggregate. Note that if the
2186 -- Parent pointer of Expr is not set then Expr was produced with a
2187 -- New_Copy_Tree or some such.
2189 ---------------------
2190 -- Add_Association --
2191 ---------------------
2193 procedure Add_Association
2194 (Component : Entity_Id;
2195 Expr : Node_Id;
2196 Box_Present : Boolean := False)
2198 Choice_List : constant List_Id := New_List;
2199 New_Assoc : Node_Id;
2201 begin
2202 Append (New_Occurrence_Of (Component, Sloc (Expr)), Choice_List);
2203 New_Assoc :=
2204 Make_Component_Association (Sloc (Expr),
2205 Choices => Choice_List,
2206 Expression => Expr,
2207 Box_Present => Box_Present);
2208 Append (New_Assoc, New_Assoc_List);
2209 end Add_Association;
2211 -------------------
2212 -- Discr_Present --
2213 -------------------
2215 function Discr_Present (Discr : Entity_Id) return Boolean is
2216 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
2218 Loc : Source_Ptr;
2220 Ancestor : Node_Id;
2221 Discr_Expr : Node_Id;
2223 Ancestor_Typ : Entity_Id;
2224 Orig_Discr : Entity_Id;
2225 D : Entity_Id;
2226 D_Val : Elmt_Id := No_Elmt; -- stop junk warning
2228 Ancestor_Is_Subtyp : Boolean;
2230 begin
2231 if Regular_Aggr then
2232 return True;
2233 end if;
2235 Ancestor := Ancestor_Part (N);
2236 Ancestor_Typ := Etype (Ancestor);
2237 Loc := Sloc (Ancestor);
2239 Ancestor_Is_Subtyp :=
2240 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
2242 -- If the ancestor part has no discriminants clearly N's aggregate
2243 -- part must provide a value for Discr.
2245 if not Has_Discriminants (Ancestor_Typ) then
2246 return True;
2248 -- If the ancestor part is an unconstrained subtype mark then the
2249 -- Discr must be present in N's aggregate part.
2251 elsif Ancestor_Is_Subtyp
2252 and then not Is_Constrained (Entity (Ancestor))
2253 then
2254 return True;
2255 end if;
2257 -- Now look to see if Discr was specified in the ancestor part
2259 if Ancestor_Is_Subtyp then
2260 D_Val := First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
2261 end if;
2263 Orig_Discr := Original_Record_Component (Discr);
2265 D := First_Discriminant (Ancestor_Typ);
2266 while Present (D) loop
2268 -- If Ancestor has already specified Disc value than insert its
2269 -- value in the final aggregate.
2271 if Original_Record_Component (D) = Orig_Discr then
2272 if Ancestor_Is_Subtyp then
2273 Discr_Expr := New_Copy_Tree (Node (D_Val));
2274 else
2275 Discr_Expr :=
2276 Make_Selected_Component (Loc,
2277 Prefix => Duplicate_Subexpr (Ancestor),
2278 Selector_Name => New_Occurrence_Of (Discr, Loc));
2279 end if;
2281 Resolve_Aggr_Expr (Discr_Expr, Discr);
2282 return False;
2283 end if;
2285 Next_Discriminant (D);
2287 if Ancestor_Is_Subtyp then
2288 Next_Elmt (D_Val);
2289 end if;
2290 end loop;
2292 return True;
2293 end Discr_Present;
2295 ---------------
2296 -- Get_Value --
2297 ---------------
2299 function Get_Value
2300 (Compon : Node_Id;
2301 From : List_Id;
2302 Consider_Others_Choice : Boolean := False)
2303 return Node_Id
2305 Assoc : Node_Id;
2306 Expr : Node_Id := Empty;
2307 Selector_Name : Node_Id;
2309 procedure Check_Non_Limited_Type;
2310 -- Relax check to allow the default initialization of limited types.
2311 -- For example:
2312 -- record
2313 -- C : Lim := (..., others => <>);
2314 -- end record;
2316 ----------------------------
2317 -- Check_Non_Limited_Type --
2318 ----------------------------
2320 procedure Check_Non_Limited_Type is
2321 begin
2322 if Is_Limited_Type (Etype (Compon))
2323 and then Comes_From_Source (Compon)
2324 and then not In_Instance_Body
2325 then
2326 -- Ada 2005 (AI-287): Limited aggregates are allowed
2328 if Ada_Version >= Ada_05
2329 and then Present (Expression (Assoc))
2330 and then Nkind (Expression (Assoc)) = N_Aggregate
2331 then
2332 null;
2333 else
2334 Error_Msg_N
2335 ("initialization not allowed for limited types", N);
2336 Explain_Limited_Type (Etype (Compon), Compon);
2337 end if;
2338 end if;
2339 end Check_Non_Limited_Type;
2341 -- Start of processing for Get_Value
2343 begin
2344 Mbox_Present := False;
2346 if Present (From) then
2347 Assoc := First (From);
2348 else
2349 return Empty;
2350 end if;
2352 while Present (Assoc) loop
2353 Selector_Name := First (Choices (Assoc));
2354 while Present (Selector_Name) loop
2355 if Nkind (Selector_Name) = N_Others_Choice then
2356 if Consider_Others_Choice and then No (Expr) then
2358 -- We need to duplicate the expression for each
2359 -- successive component covered by the others choice.
2360 -- This is redundant if the others_choice covers only
2361 -- one component (small optimization possible???), but
2362 -- indispensable otherwise, because each one must be
2363 -- expanded individually to preserve side-effects.
2365 -- Ada 2005 (AI-287): In case of default initialization
2366 -- of components, we duplicate the corresponding default
2367 -- expression (from the record type declaration).
2369 if Box_Present (Assoc) then
2370 Others_Mbox := True;
2371 Mbox_Present := True;
2373 if Expander_Active then
2374 return New_Copy_Tree (Expression (Parent (Compon)));
2375 else
2376 return Expression (Parent (Compon));
2377 end if;
2379 else
2380 Check_Non_Limited_Type;
2382 if Present (Others_Etype) and then
2383 Base_Type (Others_Etype) /= Base_Type (Etype
2384 (Compon))
2385 then
2386 Error_Msg_N ("components in OTHERS choice must " &
2387 "have same type", Selector_Name);
2388 end if;
2390 Others_Etype := Etype (Compon);
2392 if Expander_Active then
2393 return New_Copy_Tree (Expression (Assoc));
2394 else
2395 return Expression (Assoc);
2396 end if;
2397 end if;
2398 end if;
2400 elsif Chars (Compon) = Chars (Selector_Name) then
2401 if No (Expr) then
2403 -- Ada 2005 (AI-231)
2405 if Ada_Version >= Ada_05
2406 and then Nkind (Expression (Assoc)) = N_Null
2407 then
2408 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
2409 end if;
2411 -- We need to duplicate the expression when several
2412 -- components are grouped together with a "|" choice.
2413 -- For instance "filed1 | filed2 => Expr"
2415 -- Ada 2005 (AI-287)
2417 if Box_Present (Assoc) then
2418 Mbox_Present := True;
2420 -- Duplicate the default expression of the component
2421 -- from the record type declaration
2423 if Present (Next (Selector_Name)) then
2424 Expr :=
2425 New_Copy_Tree (Expression (Parent (Compon)));
2426 else
2427 Expr := Expression (Parent (Compon));
2428 end if;
2430 else
2431 Check_Non_Limited_Type;
2433 if Present (Next (Selector_Name)) then
2434 Expr := New_Copy_Tree (Expression (Assoc));
2435 else
2436 Expr := Expression (Assoc);
2437 end if;
2438 end if;
2440 Generate_Reference (Compon, Selector_Name);
2442 else
2443 Error_Msg_NE
2444 ("more than one value supplied for &",
2445 Selector_Name, Compon);
2447 end if;
2448 end if;
2450 Next (Selector_Name);
2451 end loop;
2453 Next (Assoc);
2454 end loop;
2456 return Expr;
2457 end Get_Value;
2459 -----------------------
2460 -- Resolve_Aggr_Expr --
2461 -----------------------
2463 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id) is
2464 New_C : Entity_Id := Component;
2465 Expr_Type : Entity_Id := Empty;
2467 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
2468 -- If the expression is an aggregate (possibly qualified) then its
2469 -- expansion is delayed until the enclosing aggregate is expanded
2470 -- into assignments. In that case, do not generate checks on the
2471 -- expression, because they will be generated later, and will other-
2472 -- wise force a copy (to remove side-effects) that would leave a
2473 -- dynamic-sized aggregate in the code, something that gigi cannot
2474 -- handle.
2476 Relocate : Boolean;
2477 -- Set to True if the resolved Expr node needs to be relocated
2478 -- when attached to the newly created association list. This node
2479 -- need not be relocated if its parent pointer is not set.
2480 -- In fact in this case Expr is the output of a New_Copy_Tree call.
2481 -- if Relocate is True then we have analyzed the expression node
2482 -- in the original aggregate and hence it needs to be relocated
2483 -- when moved over the new association list.
2485 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
2486 Kind : constant Node_Kind := Nkind (Expr);
2488 begin
2489 return ((Kind = N_Aggregate
2490 or else Kind = N_Extension_Aggregate)
2491 and then Present (Etype (Expr))
2492 and then Is_Record_Type (Etype (Expr))
2493 and then Expansion_Delayed (Expr))
2495 or else (Kind = N_Qualified_Expression
2496 and then Has_Expansion_Delayed (Expression (Expr)));
2497 end Has_Expansion_Delayed;
2499 -- Start of processing for Resolve_Aggr_Expr
2501 begin
2502 -- If the type of the component is elementary or the type of the
2503 -- aggregate does not contain discriminants, use the type of the
2504 -- component to resolve Expr.
2506 if Is_Elementary_Type (Etype (Component))
2507 or else not Has_Discriminants (Etype (N))
2508 then
2509 Expr_Type := Etype (Component);
2511 -- Otherwise we have to pick up the new type of the component from
2512 -- the new costrained subtype of the aggregate. In fact components
2513 -- which are of a composite type might be constrained by a
2514 -- discriminant, and we want to resolve Expr against the subtype were
2515 -- all discriminant occurrences are replaced with their actual value.
2517 else
2518 New_C := First_Component (Etype (N));
2519 while Present (New_C) loop
2520 if Chars (New_C) = Chars (Component) then
2521 Expr_Type := Etype (New_C);
2522 exit;
2523 end if;
2525 Next_Component (New_C);
2526 end loop;
2528 pragma Assert (Present (Expr_Type));
2530 -- For each range in an array type where a discriminant has been
2531 -- replaced with the constraint, check that this range is within
2532 -- the range of the base type. This checks is done in the init
2533 -- proc for regular objects, but has to be done here for
2534 -- aggregates since no init proc is called for them.
2536 if Is_Array_Type (Expr_Type) then
2537 declare
2538 Index : Node_Id := First_Index (Expr_Type);
2539 -- Range of the current constrained index in the array
2541 Orig_Index : Node_Id := First_Index (Etype (Component));
2542 -- Range corresponding to the range Index above in the
2543 -- original unconstrained record type. The bounds of this
2544 -- range may be governed by discriminants.
2546 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
2547 -- Range corresponding to the range Index above for the
2548 -- unconstrained array type. This range is needed to apply
2549 -- range checks.
2551 begin
2552 while Present (Index) loop
2553 if Depends_On_Discriminant (Orig_Index) then
2554 Apply_Range_Check (Index, Etype (Unconstr_Index));
2555 end if;
2557 Next_Index (Index);
2558 Next_Index (Orig_Index);
2559 Next_Index (Unconstr_Index);
2560 end loop;
2561 end;
2562 end if;
2563 end if;
2565 -- If the Parent pointer of Expr is not set, Expr is an expression
2566 -- duplicated by New_Tree_Copy (this happens for record aggregates
2567 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
2568 -- Such a duplicated expression must be attached to the tree
2569 -- before analysis and resolution to enforce the rule that a tree
2570 -- fragment should never be analyzed or resolved unless it is
2571 -- attached to the current compilation unit.
2573 if No (Parent (Expr)) then
2574 Set_Parent (Expr, N);
2575 Relocate := False;
2576 else
2577 Relocate := True;
2578 end if;
2580 Analyze_And_Resolve (Expr, Expr_Type);
2581 Check_Non_Static_Context (Expr);
2582 Check_Unset_Reference (Expr);
2584 if not Has_Expansion_Delayed (Expr) then
2585 Aggregate_Constraint_Checks (Expr, Expr_Type);
2586 end if;
2588 if Raises_Constraint_Error (Expr) then
2589 Set_Raises_Constraint_Error (N);
2590 end if;
2592 if Relocate then
2593 Add_Association (New_C, Relocate_Node (Expr));
2594 else
2595 Add_Association (New_C, Expr);
2596 end if;
2597 end Resolve_Aggr_Expr;
2599 -- Resolve_Record_Aggregate local variables
2601 Assoc : Node_Id;
2602 -- N_Component_Association node belonging to the input aggregate N
2604 Expr : Node_Id;
2605 Positional_Expr : Node_Id;
2606 Component : Entity_Id;
2607 Component_Elmt : Elmt_Id;
2609 Components : constant Elist_Id := New_Elmt_List;
2610 -- Components is the list of the record components whose value must
2611 -- be provided in the aggregate. This list does include discriminants.
2613 -- Start of processing for Resolve_Record_Aggregate
2615 begin
2616 -- We may end up calling Duplicate_Subexpr on expressions that are
2617 -- attached to New_Assoc_List. For this reason we need to attach it
2618 -- to the tree by setting its parent pointer to N. This parent point
2619 -- will change in STEP 8 below.
2621 Set_Parent (New_Assoc_List, N);
2623 -- STEP 1: abstract type and null record verification
2625 if Is_Abstract (Typ) then
2626 Error_Msg_N ("type of aggregate cannot be abstract", N);
2627 end if;
2629 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
2630 Set_Etype (N, Typ);
2631 return;
2633 elsif Present (First_Entity (Typ))
2634 and then Null_Record_Present (N)
2635 and then not Is_Tagged_Type (Typ)
2636 then
2637 Error_Msg_N ("record aggregate cannot be null", N);
2638 return;
2640 elsif No (First_Entity (Typ)) then
2641 Error_Msg_N ("record aggregate must be null", N);
2642 return;
2643 end if;
2645 -- STEP 2: Verify aggregate structure
2647 Step_2 : declare
2648 Selector_Name : Node_Id;
2649 Bad_Aggregate : Boolean := False;
2651 begin
2652 if Present (Component_Associations (N)) then
2653 Assoc := First (Component_Associations (N));
2654 else
2655 Assoc := Empty;
2656 end if;
2658 while Present (Assoc) loop
2659 Selector_Name := First (Choices (Assoc));
2660 while Present (Selector_Name) loop
2661 if Nkind (Selector_Name) = N_Identifier then
2662 null;
2664 elsif Nkind (Selector_Name) = N_Others_Choice then
2665 if Selector_Name /= First (Choices (Assoc))
2666 or else Present (Next (Selector_Name))
2667 then
2668 Error_Msg_N ("OTHERS must appear alone in a choice list",
2669 Selector_Name);
2670 return;
2672 elsif Present (Next (Assoc)) then
2673 Error_Msg_N ("OTHERS must appear last in an aggregate",
2674 Selector_Name);
2675 return;
2676 end if;
2678 else
2679 Error_Msg_N
2680 ("selector name should be identifier or OTHERS",
2681 Selector_Name);
2682 Bad_Aggregate := True;
2683 end if;
2685 Next (Selector_Name);
2686 end loop;
2688 Next (Assoc);
2689 end loop;
2691 if Bad_Aggregate then
2692 return;
2693 end if;
2694 end Step_2;
2696 -- STEP 3: Find discriminant Values
2698 Step_3 : declare
2699 Discrim : Entity_Id;
2700 Missing_Discriminants : Boolean := False;
2702 begin
2703 if Present (Expressions (N)) then
2704 Positional_Expr := First (Expressions (N));
2705 else
2706 Positional_Expr := Empty;
2707 end if;
2709 if Has_Discriminants (Typ) then
2710 Discrim := First_Discriminant (Typ);
2711 else
2712 Discrim := Empty;
2713 end if;
2715 -- First find the discriminant values in the positional components
2717 while Present (Discrim) and then Present (Positional_Expr) loop
2718 if Discr_Present (Discrim) then
2719 Resolve_Aggr_Expr (Positional_Expr, Discrim);
2721 -- Ada 2005 (AI-231)
2723 if Ada_Version >= Ada_05
2724 and then Nkind (Positional_Expr) = N_Null
2725 then
2726 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
2727 end if;
2729 Next (Positional_Expr);
2730 end if;
2732 if Present (Get_Value (Discrim, Component_Associations (N))) then
2733 Error_Msg_NE
2734 ("more than one value supplied for discriminant&",
2735 N, Discrim);
2736 end if;
2738 Next_Discriminant (Discrim);
2739 end loop;
2741 -- Find remaining discriminant values, if any, among named components
2743 while Present (Discrim) loop
2744 Expr := Get_Value (Discrim, Component_Associations (N), True);
2746 if not Discr_Present (Discrim) then
2747 if Present (Expr) then
2748 Error_Msg_NE
2749 ("more than one value supplied for discriminant&",
2750 N, Discrim);
2751 end if;
2753 elsif No (Expr) then
2754 Error_Msg_NE
2755 ("no value supplied for discriminant &", N, Discrim);
2756 Missing_Discriminants := True;
2758 else
2759 Resolve_Aggr_Expr (Expr, Discrim);
2760 end if;
2762 Next_Discriminant (Discrim);
2763 end loop;
2765 if Missing_Discriminants then
2766 return;
2767 end if;
2769 -- At this point and until the beginning of STEP 6, New_Assoc_List
2770 -- contains only the discriminants and their values.
2772 end Step_3;
2774 -- STEP 4: Set the Etype of the record aggregate
2776 -- ??? This code is pretty much a copy of Sem_Ch3.Build_Subtype. That
2777 -- routine should really be exported in sem_util or some such and used
2778 -- in sem_ch3 and here rather than have a copy of the code which is a
2779 -- maintenance nightmare.
2781 -- ??? Performace WARNING. The current implementation creates a new
2782 -- itype for all aggregates whose base type is discriminated.
2783 -- This means that for record aggregates nested inside an array
2784 -- aggregate we will create a new itype for each record aggregate
2785 -- if the array cmponent type has discriminants. For large aggregates
2786 -- this may be a problem. What should be done in this case is
2787 -- to reuse itypes as much as possible.
2789 if Has_Discriminants (Typ) then
2790 Build_Constrained_Itype : declare
2791 Loc : constant Source_Ptr := Sloc (N);
2792 Indic : Node_Id;
2793 Subtyp_Decl : Node_Id;
2794 Def_Id : Entity_Id;
2796 C : constant List_Id := New_List;
2798 begin
2799 New_Assoc := First (New_Assoc_List);
2800 while Present (New_Assoc) loop
2801 Append (Duplicate_Subexpr (Expression (New_Assoc)), To => C);
2802 Next (New_Assoc);
2803 end loop;
2805 Indic :=
2806 Make_Subtype_Indication (Loc,
2807 Subtype_Mark => New_Occurrence_Of (Base_Type (Typ), Loc),
2808 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
2810 Def_Id := Create_Itype (Ekind (Typ), N);
2812 Subtyp_Decl :=
2813 Make_Subtype_Declaration (Loc,
2814 Defining_Identifier => Def_Id,
2815 Subtype_Indication => Indic);
2816 Set_Parent (Subtyp_Decl, Parent (N));
2818 -- Itypes must be analyzed with checks off (see itypes.ads)
2820 Analyze (Subtyp_Decl, Suppress => All_Checks);
2822 Set_Etype (N, Def_Id);
2823 Check_Static_Discriminated_Subtype
2824 (Def_Id, Expression (First (New_Assoc_List)));
2825 end Build_Constrained_Itype;
2827 else
2828 Set_Etype (N, Typ);
2829 end if;
2831 -- STEP 5: Get remaining components according to discriminant values
2833 Step_5 : declare
2834 Record_Def : Node_Id;
2835 Parent_Typ : Entity_Id;
2836 Root_Typ : Entity_Id;
2837 Parent_Typ_List : Elist_Id;
2838 Parent_Elmt : Elmt_Id;
2839 Errors_Found : Boolean := False;
2840 Dnode : Node_Id;
2842 begin
2843 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
2844 Parent_Typ_List := New_Elmt_List;
2846 -- If this is an extension aggregate, the component list must
2847 -- include all components that are not in the given ancestor
2848 -- type. Otherwise, the component list must include components
2849 -- of all ancestors, starting with the root.
2851 if Nkind (N) = N_Extension_Aggregate then
2852 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
2853 else
2854 Root_Typ := Root_Type (Typ);
2856 if Nkind (Parent (Base_Type (Root_Typ)))
2857 = N_Private_Type_Declaration
2858 then
2859 Error_Msg_NE
2860 ("type of aggregate has private ancestor&!",
2861 N, Root_Typ);
2862 Error_Msg_N ("must use extension aggregate!", N);
2863 return;
2864 end if;
2866 Dnode := Declaration_Node (Base_Type (Root_Typ));
2868 -- If we don't get a full declaration, then we have some
2869 -- error which will get signalled later so skip this part.
2870 -- Otherwise, gather components of root that apply to the
2871 -- aggregate type. We use the base type in case there is an
2872 -- applicable stored constraint that renames the discriminants
2873 -- of the root.
2875 if Nkind (Dnode) = N_Full_Type_Declaration then
2876 Record_Def := Type_Definition (Dnode);
2877 Gather_Components (Base_Type (Typ),
2878 Component_List (Record_Def),
2879 Governed_By => New_Assoc_List,
2880 Into => Components,
2881 Report_Errors => Errors_Found);
2882 end if;
2883 end if;
2885 Parent_Typ := Base_Type (Typ);
2886 while Parent_Typ /= Root_Typ loop
2888 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
2889 Parent_Typ := Etype (Parent_Typ);
2891 if Nkind (Parent (Base_Type (Parent_Typ))) =
2892 N_Private_Type_Declaration
2893 or else Nkind (Parent (Base_Type (Parent_Typ))) =
2894 N_Private_Extension_Declaration
2895 then
2896 if Nkind (N) /= N_Extension_Aggregate then
2897 Error_Msg_NE
2898 ("type of aggregate has private ancestor&!",
2899 N, Parent_Typ);
2900 Error_Msg_N ("must use extension aggregate!", N);
2901 return;
2903 elsif Parent_Typ /= Root_Typ then
2904 Error_Msg_NE
2905 ("ancestor part of aggregate must be private type&",
2906 Ancestor_Part (N), Parent_Typ);
2907 return;
2908 end if;
2909 end if;
2910 end loop;
2912 -- Now collect components from all other ancestors
2914 Parent_Elmt := First_Elmt (Parent_Typ_List);
2915 while Present (Parent_Elmt) loop
2916 Parent_Typ := Node (Parent_Elmt);
2917 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
2918 Gather_Components (Empty,
2919 Component_List (Record_Extension_Part (Record_Def)),
2920 Governed_By => New_Assoc_List,
2921 Into => Components,
2922 Report_Errors => Errors_Found);
2924 Next_Elmt (Parent_Elmt);
2925 end loop;
2927 else
2928 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
2930 if Null_Present (Record_Def) then
2931 null;
2932 else
2933 Gather_Components (Base_Type (Typ),
2934 Component_List (Record_Def),
2935 Governed_By => New_Assoc_List,
2936 Into => Components,
2937 Report_Errors => Errors_Found);
2938 end if;
2939 end if;
2941 if Errors_Found then
2942 return;
2943 end if;
2944 end Step_5;
2946 -- STEP 6: Find component Values
2948 Component := Empty;
2949 Component_Elmt := First_Elmt (Components);
2951 -- First scan the remaining positional associations in the aggregate.
2952 -- Remember that at this point Positional_Expr contains the current
2953 -- positional association if any is left after looking for discriminant
2954 -- values in step 3.
2956 while Present (Positional_Expr) and then Present (Component_Elmt) loop
2957 Component := Node (Component_Elmt);
2958 Resolve_Aggr_Expr (Positional_Expr, Component);
2960 -- Ada 2005 (AI-231)
2962 if Ada_Version >= Ada_05
2963 and then Nkind (Positional_Expr) = N_Null
2964 then
2965 Check_Can_Never_Be_Null (Component, Positional_Expr);
2966 end if;
2968 if Present (Get_Value (Component, Component_Associations (N))) then
2969 Error_Msg_NE
2970 ("more than one value supplied for Component &", N, Component);
2971 end if;
2973 Next (Positional_Expr);
2974 Next_Elmt (Component_Elmt);
2975 end loop;
2977 if Present (Positional_Expr) then
2978 Error_Msg_N
2979 ("too many components for record aggregate", Positional_Expr);
2980 end if;
2982 -- Now scan for the named arguments of the aggregate
2984 while Present (Component_Elmt) loop
2985 Component := Node (Component_Elmt);
2986 Expr := Get_Value (Component, Component_Associations (N), True);
2988 -- Ada 2005 (AI-287): Although the default initialization by means
2989 -- of the mbox was initially added to Ada 2005 for limited types, it
2990 -- is not constrained to limited types. Therefore if the component
2991 -- has some initialization procedure (IP) we pass the component to
2992 -- the expander, which will generate the call to such IP.
2994 if Mbox_Present
2995 and then Has_Non_Null_Base_Init_Proc (Etype (Component))
2996 then
2997 Add_Association
2998 (Component => Component,
2999 Expr => Empty,
3000 Box_Present => True);
3002 -- Ada 2005 (AI-287): No value supplied for component
3004 elsif Mbox_Present and No (Expr) then
3005 null;
3007 elsif No (Expr) then
3008 Error_Msg_NE ("no value supplied for component &!", N, Component);
3010 else
3011 Resolve_Aggr_Expr (Expr, Component);
3012 end if;
3014 Next_Elmt (Component_Elmt);
3015 end loop;
3017 -- STEP 7: check for invalid components + check type in choice list
3019 Step_7 : declare
3020 Selectr : Node_Id;
3021 -- Selector name
3023 Typech : Entity_Id;
3024 -- Type of first component in choice list
3026 begin
3027 if Present (Component_Associations (N)) then
3028 Assoc := First (Component_Associations (N));
3029 else
3030 Assoc := Empty;
3031 end if;
3033 Verification : while Present (Assoc) loop
3034 Selectr := First (Choices (Assoc));
3035 Typech := Empty;
3037 if Nkind (Selectr) = N_Others_Choice then
3039 -- Ada 2005 (AI-287): others choice may have expression or mbox
3041 if No (Others_Etype)
3042 and then not Others_Mbox
3043 then
3044 Error_Msg_N
3045 ("OTHERS must represent at least one component", Selectr);
3046 end if;
3048 exit Verification;
3049 end if;
3051 while Present (Selectr) loop
3052 New_Assoc := First (New_Assoc_List);
3053 while Present (New_Assoc) loop
3054 Component := First (Choices (New_Assoc));
3055 exit when Chars (Selectr) = Chars (Component);
3056 Next (New_Assoc);
3057 end loop;
3059 -- If no association, this is not a legal component of
3060 -- of the type in question, except if this is an internal
3061 -- component supplied by a previous expansion.
3063 if No (New_Assoc) then
3064 if Box_Present (Parent (Selectr)) then
3065 null;
3067 elsif Chars (Selectr) /= Name_uTag
3068 and then Chars (Selectr) /= Name_uParent
3069 and then Chars (Selectr) /= Name_uController
3070 then
3071 if not Has_Discriminants (Typ) then
3072 Error_Msg_Node_2 := Typ;
3073 Error_Msg_N
3074 ("& is not a component of}",
3075 Selectr);
3076 else
3077 Error_Msg_N
3078 ("& is not a component of the aggregate subtype",
3079 Selectr);
3080 end if;
3082 Check_Misspelled_Component (Components, Selectr);
3083 end if;
3085 elsif No (Typech) then
3086 Typech := Base_Type (Etype (Component));
3088 elsif Typech /= Base_Type (Etype (Component)) then
3089 if not Box_Present (Parent (Selectr)) then
3090 Error_Msg_N
3091 ("components in choice list must have same type",
3092 Selectr);
3093 end if;
3094 end if;
3096 Next (Selectr);
3097 end loop;
3099 Next (Assoc);
3100 end loop Verification;
3101 end Step_7;
3103 -- STEP 8: replace the original aggregate
3105 Step_8 : declare
3106 New_Aggregate : constant Node_Id := New_Copy (N);
3108 begin
3109 Set_Expressions (New_Aggregate, No_List);
3110 Set_Etype (New_Aggregate, Etype (N));
3111 Set_Component_Associations (New_Aggregate, New_Assoc_List);
3113 Rewrite (N, New_Aggregate);
3114 end Step_8;
3115 end Resolve_Record_Aggregate;
3117 -----------------------------
3118 -- Check_Can_Never_Be_Null --
3119 -----------------------------
3121 procedure Check_Can_Never_Be_Null (Typ : Node_Id; Expr : Node_Id) is
3122 Comp_Typ : Entity_Id;
3124 begin
3125 pragma Assert (Ada_Version >= Ada_05
3126 and then Present (Expr)
3127 and then Nkind (Expr) = N_Null);
3129 case Ekind (Typ) is
3130 when E_Array_Type =>
3131 Comp_Typ := Component_Type (Typ);
3133 when E_Component |
3134 E_Discriminant =>
3135 Comp_Typ := Etype (Typ);
3137 when others =>
3138 return;
3139 end case;
3141 if Present (Expr)
3142 and then Can_Never_Be_Null (Comp_Typ)
3143 then
3144 Error_Msg_N
3145 ("(Ada 2005) NULL not allowed in null-excluding components?", Expr);
3146 Error_Msg_NEL
3147 ("\& will be raised at run time!?",
3148 Expr, Standard_Constraint_Error, Sloc (Expr));
3150 Set_Etype (Expr, Comp_Typ);
3151 Set_Analyzed (Expr);
3152 Install_Null_Excluding_Check (Expr);
3153 end if;
3154 end Check_Can_Never_Be_Null;
3156 ---------------------
3157 -- Sort_Case_Table --
3158 ---------------------
3160 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
3161 L : constant Int := Case_Table'First;
3162 U : constant Int := Case_Table'Last;
3163 K : Int;
3164 J : Int;
3165 T : Case_Bounds;
3167 begin
3168 K := L;
3170 while K /= U loop
3171 T := Case_Table (K + 1);
3172 J := K + 1;
3174 while J /= L
3175 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
3176 Expr_Value (T.Choice_Lo)
3177 loop
3178 Case_Table (J) := Case_Table (J - 1);
3179 J := J - 1;
3180 end loop;
3182 Case_Table (J) := T;
3183 K := K + 1;
3184 end loop;
3185 end Sort_Case_Table;
3187 end Sem_Aggr;