./:
[official-gcc.git] / gcc / ada / sem_aggr.adb
blobb67a6a85fcb09febf0b581a30c37d15b3938fd8c
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_Util; use Exp_Util;
33 with Freeze; use Freeze;
34 with Itypes; use Itypes;
35 with Lib.Xref; use Lib.Xref;
36 with Namet; use Namet;
37 with Nmake; use Nmake;
38 with Nlists; use Nlists;
39 with Opt; use Opt;
40 with Sem; use Sem;
41 with Sem_Cat; use Sem_Cat;
42 with Sem_Ch8; use Sem_Ch8;
43 with Sem_Ch13; use Sem_Ch13;
44 with Sem_Eval; use Sem_Eval;
45 with Sem_Res; use Sem_Res;
46 with Sem_Util; use Sem_Util;
47 with Sem_Type; use Sem_Type;
48 with Sem_Warn; use Sem_Warn;
49 with Sinfo; use Sinfo;
50 with Snames; use Snames;
51 with Stringt; use Stringt;
52 with Stand; use Stand;
53 with Targparm; use Targparm;
54 with Tbuild; use Tbuild;
55 with Uintp; use Uintp;
57 with GNAT.Spelling_Checker; use GNAT.Spelling_Checker;
59 package body Sem_Aggr is
61 type Case_Bounds is record
62 Choice_Lo : Node_Id;
63 Choice_Hi : Node_Id;
64 Choice_Node : Node_Id;
65 end record;
67 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
68 -- Table type used by Check_Case_Choices procedure
70 -----------------------
71 -- Local Subprograms --
72 -----------------------
74 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
75 -- Sort the Case Table using the Lower Bound of each Choice as the key.
76 -- A simple insertion sort is used since the number of choices in a case
77 -- statement of variant part will usually be small and probably in near
78 -- sorted order.
80 procedure Check_Can_Never_Be_Null (Typ : Node_Id; Expr : Node_Id);
81 -- Ada 2005 (AI-231): Check bad usage of the null-exclusion issue
83 ------------------------------------------------------
84 -- Subprograms used for RECORD AGGREGATE Processing --
85 ------------------------------------------------------
87 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
88 -- This procedure performs all the semantic checks required for record
89 -- aggregates. Note that for aggregates analysis and resolution go
90 -- hand in hand. Aggregate analysis has been delayed up to here and
91 -- it is done while resolving the aggregate.
93 -- N is the N_Aggregate node.
94 -- Typ is the record type for the aggregate resolution
96 -- While performing the semantic checks, this procedure
97 -- builds a new Component_Association_List where each record field
98 -- appears alone in a Component_Choice_List along with its corresponding
99 -- expression. The record fields in the Component_Association_List
100 -- appear in the same order in which they appear in the record type Typ.
102 -- Once this new Component_Association_List is built and all the
103 -- semantic checks performed, the original aggregate subtree is replaced
104 -- with the new named record aggregate just built. Note that the subtree
105 -- substitution is performed with Rewrite so as to be
106 -- able to retrieve the original aggregate.
108 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
109 -- yields the aggregate format expected by Gigi. Typically, this kind of
110 -- tree manipulations are done in the expander. However, because the
111 -- semantic checks that need to be performed on record aggregates really
112 -- go hand in hand with the record aggregate normalization, the aggregate
113 -- subtree transformation is performed during resolution rather than
114 -- expansion. Had we decided otherwise we would have had to duplicate
115 -- most of the code in the expansion procedure Expand_Record_Aggregate.
116 -- Note, however, that all the expansion concerning aggegates for tagged
117 -- records is done in Expand_Record_Aggregate.
119 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
121 -- 1. Make sure that the record type against which the record aggregate
122 -- has to be resolved is not abstract. Furthermore if the type is
123 -- a null aggregate make sure the input aggregate N is also null.
125 -- 2. Verify that the structure of the aggregate is that of a record
126 -- aggregate. Specifically, look for component associations and ensure
127 -- that each choice list only has identifiers or the N_Others_Choice
128 -- node. Also make sure that if present, the N_Others_Choice occurs
129 -- last and by itself.
131 -- 3. If Typ contains discriminants, the values for each discriminant
132 -- is looked for. If the record type Typ has variants, we check
133 -- that the expressions corresponding to each discriminant ruling
134 -- the (possibly nested) variant parts of Typ, are static. This
135 -- allows us to determine the variant parts to which the rest of
136 -- the aggregate must conform. The names of discriminants with their
137 -- values are saved in a new association list, New_Assoc_List which
138 -- is later augmented with the names and values of the remaining
139 -- components in the record type.
141 -- During this phase we also make sure that every discriminant is
142 -- assigned exactly one value. Note that when several values
143 -- for a given discriminant are found, semantic processing continues
144 -- looking for further errors. In this case it's the first
145 -- discriminant value found which we will be recorded.
147 -- IMPORTANT NOTE: For derived tagged types this procedure expects
148 -- First_Discriminant and Next_Discriminant to give the correct list
149 -- of discriminants, in the correct order.
151 -- 4. After all the discriminant values have been gathered, we can
152 -- set the Etype of the record aggregate. If Typ contains no
153 -- discriminants this is straightforward: the Etype of N is just
154 -- Typ, otherwise a new implicit constrained subtype of Typ is
155 -- built to be the Etype of N.
157 -- 5. Gather the remaining record components according to the discriminant
158 -- values. This involves recursively traversing the record type
159 -- structure to see what variants are selected by the given discriminant
160 -- values. This processing is a little more convoluted if Typ is a
161 -- derived tagged types since we need to retrieve the record structure
162 -- of all the ancestors of Typ.
164 -- 6. After gathering the record components we look for their values
165 -- in the record aggregate and emit appropriate error messages
166 -- should we not find such values or should they be duplicated.
168 -- 7. We then make sure no illegal component names appear in the
169 -- record aggegate and make sure that the type of the record
170 -- components appearing in a same choice list is the same.
171 -- Finally we ensure that the others choice, if present, is
172 -- used to provide the value of at least a record component.
174 -- 8. The original aggregate node is replaced with the new named
175 -- aggregate built in steps 3 through 6, as explained earlier.
177 -- Given the complexity of record aggregate resolution, the primary
178 -- goal of this routine is clarity and simplicity rather than execution
179 -- and storage efficiency. If there are only positional components in the
180 -- aggregate the running time is linear. If there are associations
181 -- the running time is still linear as long as the order of the
182 -- associations is not too far off the order of the components in the
183 -- record type. If this is not the case the running time is at worst
184 -- quadratic in the size of the association list.
186 procedure Check_Misspelled_Component
187 (Elements : Elist_Id;
188 Component : Node_Id);
189 -- Give possible misspelling diagnostic if Component is likely to be
190 -- a misspelling of one of the components of the Assoc_List.
191 -- This is called by Resolv_Aggr_Expr after producing
192 -- an invalid component error message.
194 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id);
195 -- An optimization: determine whether a discriminated subtype has a
196 -- static constraint, and contains array components whose length is also
197 -- static, either because they are constrained by the discriminant, or
198 -- because the original component bounds are static.
200 -----------------------------------------------------
201 -- Subprograms used for ARRAY AGGREGATE Processing --
202 -----------------------------------------------------
204 function Resolve_Array_Aggregate
205 (N : Node_Id;
206 Index : Node_Id;
207 Index_Constr : Node_Id;
208 Component_Typ : Entity_Id;
209 Others_Allowed : Boolean)
210 return Boolean;
211 -- This procedure performs the semantic checks for an array aggregate.
212 -- True is returned if the aggregate resolution succeeds.
213 -- The procedure works by recursively checking each nested aggregate.
214 -- Specifically, after checking a sub-aggregate nested at the i-th level
215 -- we recursively check all the subaggregates at the i+1-st level (if any).
216 -- Note that for aggregates analysis and resolution go hand in hand.
217 -- Aggregate analysis has been delayed up to here and it is done while
218 -- resolving the aggregate.
220 -- N is the current N_Aggregate node to be checked.
222 -- Index is the index node corresponding to the array sub-aggregate that
223 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
224 -- corresponding index type (or subtype).
226 -- Index_Constr is the node giving the applicable index constraint if
227 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
228 -- contexts [...] that can be used to determine the bounds of the array
229 -- value specified by the aggregate". If Others_Allowed below is False
230 -- there is no applicable index constraint and this node is set to Index.
232 -- Component_Typ is the array component type.
234 -- Others_Allowed indicates whether an others choice is allowed
235 -- in the context where the top-level aggregate appeared.
237 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
239 -- 1. Make sure that the others choice, if present, is by itself and
240 -- appears last in the sub-aggregate. Check that we do not have
241 -- positional and named components in the array sub-aggregate (unless
242 -- the named association is an others choice). Finally if an others
243 -- choice is present, make sure it is allowed in the aggregate contex.
245 -- 2. If the array sub-aggregate contains discrete_choices:
247 -- (A) Verify their validity. Specifically verify that:
249 -- (a) If a null range is present it must be the only possible
250 -- choice in the array aggregate.
252 -- (b) Ditto for a non static range.
254 -- (c) Ditto for a non static expression.
256 -- In addition this step analyzes and resolves each discrete_choice,
257 -- making sure that its type is the type of the corresponding Index.
258 -- If we are not at the lowest array aggregate level (in the case of
259 -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
260 -- recursively on each component expression. Otherwise, resolve the
261 -- bottom level component expressions against the expected component
262 -- type ONLY IF the component corresponds to a single discrete choice
263 -- which is not an others choice (to see why read the DELAYED
264 -- COMPONENT RESOLUTION below).
266 -- (B) Determine the bounds of the sub-aggregate and lowest and
267 -- highest choice values.
269 -- 3. For positional aggregates:
271 -- (A) Loop over the component expressions either recursively invoking
272 -- Resolve_Array_Aggregate on each of these for multi-dimensional
273 -- array aggregates or resolving the bottom level component
274 -- expressions against the expected component type.
276 -- (B) Determine the bounds of the positional sub-aggregates.
278 -- 4. Try to determine statically whether the evaluation of the array
279 -- sub-aggregate raises Constraint_Error. If yes emit proper
280 -- warnings. The precise checks are the following:
282 -- (A) Check that the index range defined by aggregate bounds is
283 -- compatible with corresponding index subtype.
284 -- We also check against the base type. In fact it could be that
285 -- Low/High bounds of the base type are static whereas those of
286 -- the index subtype are not. Thus if we can statically catch
287 -- a problem with respect to the base type we are guaranteed
288 -- that the same problem will arise with the index subtype
290 -- (B) If we are dealing with a named aggregate containing an others
291 -- choice and at least one discrete choice then make sure the range
292 -- specified by the discrete choices does not overflow the
293 -- aggregate bounds. We also check against the index type and base
294 -- type bounds for the same reasons given in (A).
296 -- (C) If we are dealing with a positional aggregate with an others
297 -- choice make sure the number of positional elements specified
298 -- does not overflow the aggregate bounds. We also check against
299 -- the index type and base type bounds as mentioned in (A).
301 -- Finally construct an N_Range node giving the sub-aggregate bounds.
302 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
303 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
304 -- to build the appropriate aggregate subtype. Aggregate_Bounds
305 -- information is needed during expansion.
307 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
308 -- expressions in an array aggregate may call Duplicate_Subexpr or some
309 -- other routine that inserts code just outside the outermost aggregate.
310 -- If the array aggregate contains discrete choices or an others choice,
311 -- this may be wrong. Consider for instance the following example.
313 -- type Rec is record
314 -- V : Integer := 0;
315 -- end record;
317 -- type Acc_Rec is access Rec;
318 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
320 -- Then the transformation of "new Rec" that occurs during resolution
321 -- entails the following code modifications
323 -- P7b : constant Acc_Rec := new Rec;
324 -- RecIP (P7b.all);
325 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
327 -- This code transformation is clearly wrong, since we need to call
328 -- "new Rec" for each of the 3 array elements. To avoid this problem we
329 -- delay resolution of the components of non positional array aggregates
330 -- to the expansion phase. As an optimization, if the discrete choice
331 -- specifies a single value we do not delay resolution.
333 function Array_Aggr_Subtype (N : Node_Id; Typ : Node_Id) return Entity_Id;
334 -- This routine returns the type or subtype of an array aggregate.
336 -- N is the array aggregate node whose type we return.
338 -- Typ is the context type in which N occurs.
340 -- This routine creates an implicit array subtype whose bounds are
341 -- those defined by the aggregate. When this routine is invoked
342 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
343 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
344 -- sub-aggregate bounds. When building the aggegate itype, this function
345 -- traverses the array aggregate N collecting such Aggregate_Bounds and
346 -- constructs the proper array aggregate itype.
348 -- Note that in the case of multidimensional aggregates each inner
349 -- sub-aggregate corresponding to a given array dimension, may provide a
350 -- different bounds. If it is possible to determine statically that
351 -- some sub-aggregates corresponding to the same index do not have the
352 -- same bounds, then a warning is emitted. If such check is not possible
353 -- statically (because some sub-aggregate bounds are dynamic expressions)
354 -- then this job is left to the expander. In all cases the particular
355 -- bounds that this function will chose for a given dimension is the first
356 -- N_Range node for a sub-aggregate corresponding to that dimension.
358 -- Note that the Raises_Constraint_Error flag of an array aggregate
359 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
360 -- is set in Resolve_Array_Aggregate but the aggregate is not
361 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
362 -- first construct the proper itype for the aggregate (Gigi needs
363 -- this). After constructing the proper itype we will eventually replace
364 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
365 -- Of course in cases such as:
367 -- type Arr is array (integer range <>) of Integer;
368 -- A : Arr := (positive range -1 .. 2 => 0);
370 -- The bounds of the aggregate itype are cooked up to look reasonable
371 -- (in this particular case the bounds will be 1 .. 2).
373 procedure Aggregate_Constraint_Checks
374 (Exp : Node_Id;
375 Check_Typ : Entity_Id);
376 -- Checks expression Exp against subtype Check_Typ. If Exp is an
377 -- aggregate and Check_Typ a constrained record type with discriminants,
378 -- we generate the appropriate discriminant checks. If Exp is an array
379 -- aggregate then emit the appropriate length checks. If Exp is a scalar
380 -- type, or a string literal, Exp is changed into Check_Typ'(Exp) to
381 -- ensure that range checks are performed at run time.
383 procedure Make_String_Into_Aggregate (N : Node_Id);
384 -- A string literal can appear in a context in which a one dimensional
385 -- array of characters is expected. This procedure simply rewrites the
386 -- string as an aggregate, prior to resolution.
388 ---------------------------------
389 -- Aggregate_Constraint_Checks --
390 ---------------------------------
392 procedure Aggregate_Constraint_Checks
393 (Exp : Node_Id;
394 Check_Typ : Entity_Id)
396 Exp_Typ : constant Entity_Id := Etype (Exp);
398 begin
399 if Raises_Constraint_Error (Exp) then
400 return;
401 end if;
403 -- This is really expansion activity, so make sure that expansion
404 -- is on and is allowed.
406 if not Expander_Active or else In_Default_Expression then
407 return;
408 end if;
410 -- First check if we have to insert discriminant checks
412 if Has_Discriminants (Exp_Typ) then
413 Apply_Discriminant_Check (Exp, Check_Typ);
415 -- Next emit length checks for array aggregates
417 elsif Is_Array_Type (Exp_Typ) then
418 Apply_Length_Check (Exp, Check_Typ);
420 -- Finally emit scalar and string checks. If we are dealing with a
421 -- scalar literal we need to check by hand because the Etype of
422 -- literals is not necessarily correct.
424 elsif Is_Scalar_Type (Exp_Typ)
425 and then Compile_Time_Known_Value (Exp)
426 then
427 if Is_Out_Of_Range (Exp, Base_Type (Check_Typ)) then
428 Apply_Compile_Time_Constraint_Error
429 (Exp, "value not in range of}?", CE_Range_Check_Failed,
430 Ent => Base_Type (Check_Typ),
431 Typ => Base_Type (Check_Typ));
433 elsif Is_Out_Of_Range (Exp, Check_Typ) then
434 Apply_Compile_Time_Constraint_Error
435 (Exp, "value not in range of}?", CE_Range_Check_Failed,
436 Ent => Check_Typ,
437 Typ => Check_Typ);
439 elsif not Range_Checks_Suppressed (Check_Typ) then
440 Apply_Scalar_Range_Check (Exp, Check_Typ);
441 end if;
443 elsif (Is_Scalar_Type (Exp_Typ)
444 or else Nkind (Exp) = N_String_Literal)
445 and then Exp_Typ /= Check_Typ
446 then
447 if Is_Entity_Name (Exp)
448 and then Ekind (Entity (Exp)) = E_Constant
449 then
450 -- If expression is a constant, it is worthwhile checking whether
451 -- it is a bound of the type.
453 if (Is_Entity_Name (Type_Low_Bound (Check_Typ))
454 and then Entity (Exp) = Entity (Type_Low_Bound (Check_Typ)))
455 or else (Is_Entity_Name (Type_High_Bound (Check_Typ))
456 and then Entity (Exp) = Entity (Type_High_Bound (Check_Typ)))
457 then
458 return;
460 else
461 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
462 Analyze_And_Resolve (Exp, Check_Typ);
463 Check_Unset_Reference (Exp);
464 end if;
465 else
466 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
467 Analyze_And_Resolve (Exp, Check_Typ);
468 Check_Unset_Reference (Exp);
469 end if;
471 -- Ada 2005 (AI-230): Generate a conversion to an anonymous access
472 -- component's type to force the appropriate accessibility checks.
474 -- Ada 2005 (AI-231): Generate conversion to the null-excluding
475 -- type to force the corresponding run-time check
477 elsif Is_Access_Type (Check_Typ)
478 and then ((Is_Local_Anonymous_Access (Check_Typ))
479 or else (Can_Never_Be_Null (Check_Typ)
480 and then not Can_Never_Be_Null (Exp_Typ)))
481 then
482 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
483 Analyze_And_Resolve (Exp, Check_Typ);
484 Check_Unset_Reference (Exp);
485 end if;
486 end Aggregate_Constraint_Checks;
488 ------------------------
489 -- Array_Aggr_Subtype --
490 ------------------------
492 function Array_Aggr_Subtype
493 (N : Node_Id;
494 Typ : Entity_Id)
495 return Entity_Id
497 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
498 -- Number of aggregate index dimensions
500 Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
501 -- Constrained N_Range of each index dimension in our aggregate itype
503 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
504 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
505 -- Low and High bounds for each index dimension in our aggregate itype
507 Is_Fully_Positional : Boolean := True;
509 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
510 -- N is an array (sub-)aggregate. Dim is the dimension corresponding to
511 -- (sub-)aggregate N. This procedure collects the constrained N_Range
512 -- nodes corresponding to each index dimension of our aggregate itype.
513 -- These N_Range nodes are collected in Aggr_Range above.
515 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
516 -- bounds of each index dimension. If, when collecting, two bounds
517 -- corresponding to the same dimension are static and found to differ,
518 -- then emit a warning, and mark N as raising Constraint_Error.
520 -------------------------
521 -- Collect_Aggr_Bounds --
522 -------------------------
524 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
525 This_Range : constant Node_Id := Aggregate_Bounds (N);
526 -- The aggregate range node of this specific sub-aggregate
528 This_Low : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
529 This_High : constant Node_Id := High_Bound (Aggregate_Bounds (N));
530 -- The aggregate bounds of this specific sub-aggregate
532 Assoc : Node_Id;
533 Expr : Node_Id;
535 begin
536 -- Collect the first N_Range for a given dimension that you find.
537 -- For a given dimension they must be all equal anyway.
539 if No (Aggr_Range (Dim)) then
540 Aggr_Low (Dim) := This_Low;
541 Aggr_High (Dim) := This_High;
542 Aggr_Range (Dim) := This_Range;
544 else
545 if Compile_Time_Known_Value (This_Low) then
546 if not Compile_Time_Known_Value (Aggr_Low (Dim)) then
547 Aggr_Low (Dim) := This_Low;
549 elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
550 Set_Raises_Constraint_Error (N);
551 Error_Msg_N ("sub-aggregate low bound mismatch?", N);
552 Error_Msg_N ("Constraint_Error will be raised at run-time?",
554 end if;
555 end if;
557 if Compile_Time_Known_Value (This_High) then
558 if not Compile_Time_Known_Value (Aggr_High (Dim)) then
559 Aggr_High (Dim) := This_High;
561 elsif
562 Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
563 then
564 Set_Raises_Constraint_Error (N);
565 Error_Msg_N ("sub-aggregate high bound mismatch?", N);
566 Error_Msg_N ("Constraint_Error will be raised at run-time?",
568 end if;
569 end if;
570 end if;
572 if Dim < Aggr_Dimension then
574 -- Process positional components
576 if Present (Expressions (N)) then
577 Expr := First (Expressions (N));
578 while Present (Expr) loop
579 Collect_Aggr_Bounds (Expr, Dim + 1);
580 Next (Expr);
581 end loop;
582 end if;
584 -- Process component associations
586 if Present (Component_Associations (N)) then
587 Is_Fully_Positional := False;
589 Assoc := First (Component_Associations (N));
590 while Present (Assoc) loop
591 Expr := Expression (Assoc);
592 Collect_Aggr_Bounds (Expr, Dim + 1);
593 Next (Assoc);
594 end loop;
595 end if;
596 end if;
597 end Collect_Aggr_Bounds;
599 -- Array_Aggr_Subtype variables
601 Itype : Entity_Id;
602 -- the final itype of the overall aggregate
604 Index_Constraints : constant List_Id := New_List;
605 -- The list of index constraints of the aggregate itype
607 -- Start of processing for Array_Aggr_Subtype
609 begin
610 -- Make sure that the list of index constraints is properly attached
611 -- to the tree, and then collect the aggregate bounds.
613 Set_Parent (Index_Constraints, N);
614 Collect_Aggr_Bounds (N, 1);
616 -- Build the list of constrained indices of our aggregate itype
618 for J in 1 .. Aggr_Dimension loop
619 Create_Index : declare
620 Index_Base : constant Entity_Id :=
621 Base_Type (Etype (Aggr_Range (J)));
622 Index_Typ : Entity_Id;
624 begin
625 -- Construct the Index subtype
627 Index_Typ := Create_Itype (Subtype_Kind (Ekind (Index_Base)), N);
629 Set_Etype (Index_Typ, Index_Base);
631 if Is_Character_Type (Index_Base) then
632 Set_Is_Character_Type (Index_Typ);
633 end if;
635 Set_Size_Info (Index_Typ, (Index_Base));
636 Set_RM_Size (Index_Typ, RM_Size (Index_Base));
637 Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base));
638 Set_Scalar_Range (Index_Typ, Aggr_Range (J));
640 if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then
641 Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ)));
642 end if;
644 Set_Etype (Aggr_Range (J), Index_Typ);
646 Append (Aggr_Range (J), To => Index_Constraints);
647 end Create_Index;
648 end loop;
650 -- Now build the Itype
652 Itype := Create_Itype (E_Array_Subtype, N);
654 Set_First_Rep_Item (Itype, First_Rep_Item (Typ));
655 Set_Convention (Itype, Convention (Typ));
656 Set_Depends_On_Private (Itype, Has_Private_Component (Typ));
657 Set_Etype (Itype, Base_Type (Typ));
658 Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ));
659 Set_Is_Aliased (Itype, Is_Aliased (Typ));
660 Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
662 Copy_Suppress_Status (Index_Check, Typ, Itype);
663 Copy_Suppress_Status (Length_Check, Typ, Itype);
665 Set_First_Index (Itype, First (Index_Constraints));
666 Set_Is_Constrained (Itype, True);
667 Set_Is_Internal (Itype, True);
668 Init_Size_Align (Itype);
670 -- A simple optimization: purely positional aggregates of static
671 -- components should be passed to gigi unexpanded whenever possible,
672 -- and regardless of the staticness of the bounds themselves. Subse-
673 -- quent checks in exp_aggr verify that type is not packed, etc.
675 Set_Size_Known_At_Compile_Time (Itype,
676 Is_Fully_Positional
677 and then Comes_From_Source (N)
678 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
680 -- We always need a freeze node for a packed array subtype, so that
681 -- we can build the Packed_Array_Type corresponding to the subtype.
682 -- If expansion is disabled, the packed array subtype is not built,
683 -- and we must not generate a freeze node for the type, or else it
684 -- will appear incomplete to gigi.
686 if Is_Packed (Itype) and then not In_Default_Expression
687 and then Expander_Active
688 then
689 Freeze_Itype (Itype, N);
690 end if;
692 return Itype;
693 end Array_Aggr_Subtype;
695 --------------------------------
696 -- Check_Misspelled_Component --
697 --------------------------------
699 procedure Check_Misspelled_Component
700 (Elements : Elist_Id;
701 Component : Node_Id)
703 Max_Suggestions : constant := 2;
705 Nr_Of_Suggestions : Natural := 0;
706 Suggestion_1 : Entity_Id := Empty;
707 Suggestion_2 : Entity_Id := Empty;
708 Component_Elmt : Elmt_Id;
710 begin
711 -- All the components of List are matched against Component and
712 -- a count is maintained of possible misspellings. When at the
713 -- end of the analysis there are one or two (not more!) possible
714 -- misspellings, these misspellings will be suggested as
715 -- possible correction.
717 Get_Name_String (Chars (Component));
719 declare
720 S : constant String (1 .. Name_Len) :=
721 Name_Buffer (1 .. Name_Len);
723 begin
725 Component_Elmt := First_Elmt (Elements);
727 while Nr_Of_Suggestions <= Max_Suggestions
728 and then Present (Component_Elmt)
729 loop
731 Get_Name_String (Chars (Node (Component_Elmt)));
733 if Is_Bad_Spelling_Of (Name_Buffer (1 .. Name_Len), S) then
734 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
736 case Nr_Of_Suggestions is
737 when 1 => Suggestion_1 := Node (Component_Elmt);
738 when 2 => Suggestion_2 := Node (Component_Elmt);
739 when others => exit;
740 end case;
741 end if;
743 Next_Elmt (Component_Elmt);
744 end loop;
746 -- Report at most two suggestions
748 if Nr_Of_Suggestions = 1 then
749 Error_Msg_NE ("\possible misspelling of&",
750 Component, Suggestion_1);
752 elsif Nr_Of_Suggestions = 2 then
753 Error_Msg_Node_2 := Suggestion_2;
754 Error_Msg_NE ("\possible misspelling of& or&",
755 Component, Suggestion_1);
756 end if;
757 end;
758 end Check_Misspelled_Component;
760 ----------------------------------------
761 -- Check_Static_Discriminated_Subtype --
762 ----------------------------------------
764 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id) is
765 Disc : constant Entity_Id := First_Discriminant (T);
766 Comp : Entity_Id;
767 Ind : Entity_Id;
769 begin
770 if Has_Record_Rep_Clause (T) then
771 return;
773 elsif Present (Next_Discriminant (Disc)) then
774 return;
776 elsif Nkind (V) /= N_Integer_Literal then
777 return;
778 end if;
780 Comp := First_Component (T);
782 while Present (Comp) loop
784 if Is_Scalar_Type (Etype (Comp)) then
785 null;
787 elsif Is_Private_Type (Etype (Comp))
788 and then Present (Full_View (Etype (Comp)))
789 and then Is_Scalar_Type (Full_View (Etype (Comp)))
790 then
791 null;
793 elsif Is_Array_Type (Etype (Comp)) then
795 if Is_Bit_Packed_Array (Etype (Comp)) then
796 return;
797 end if;
799 Ind := First_Index (Etype (Comp));
801 while Present (Ind) loop
803 if Nkind (Ind) /= N_Range
804 or else Nkind (Low_Bound (Ind)) /= N_Integer_Literal
805 or else Nkind (High_Bound (Ind)) /= N_Integer_Literal
806 then
807 return;
808 end if;
810 Next_Index (Ind);
811 end loop;
813 else
814 return;
815 end if;
817 Next_Component (Comp);
818 end loop;
820 -- On exit, all components have statically known sizes
822 Set_Size_Known_At_Compile_Time (T);
823 end Check_Static_Discriminated_Subtype;
825 --------------------------------
826 -- Make_String_Into_Aggregate --
827 --------------------------------
829 procedure Make_String_Into_Aggregate (N : Node_Id) is
830 Exprs : constant List_Id := New_List;
831 Loc : constant Source_Ptr := Sloc (N);
832 Str : constant String_Id := Strval (N);
833 Strlen : constant Nat := String_Length (Str);
834 C : Char_Code;
835 C_Node : Node_Id;
836 New_N : Node_Id;
837 P : Source_Ptr;
839 begin
840 P := Loc + 1;
841 for J in 1 .. Strlen loop
842 C := Get_String_Char (Str, J);
843 Set_Character_Literal_Name (C);
845 C_Node :=
846 Make_Character_Literal (P,
847 Chars => Name_Find,
848 Char_Literal_Value => UI_From_CC (C));
849 Set_Etype (C_Node, Any_Character);
850 Append_To (Exprs, C_Node);
852 P := P + 1;
853 -- something special for wide strings ???
854 end loop;
856 New_N := Make_Aggregate (Loc, Expressions => Exprs);
857 Set_Analyzed (New_N);
858 Set_Etype (New_N, Any_Composite);
860 Rewrite (N, New_N);
861 end Make_String_Into_Aggregate;
863 -----------------------
864 -- Resolve_Aggregate --
865 -----------------------
867 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
868 Pkind : constant Node_Kind := Nkind (Parent (N));
870 Aggr_Subtyp : Entity_Id;
871 -- The actual aggregate subtype. This is not necessarily the same as Typ
872 -- which is the subtype of the context in which the aggregate was found.
874 begin
875 -- Check for aggregates not allowed in configurable run-time mode.
876 -- We allow all cases of aggregates that do not come from source,
877 -- since these are all assumed to be small (e.g. bounds of a string
878 -- literal). We also allow aggregates of types we know to be small.
880 if not Support_Aggregates_On_Target
881 and then Comes_From_Source (N)
882 and then (not Known_Static_Esize (Typ) or else Esize (Typ) > 64)
883 then
884 Error_Msg_CRT ("aggregate", N);
885 end if;
887 if Is_Limited_Composite (Typ) then
888 Error_Msg_N ("aggregate type cannot have limited component", N);
889 Explain_Limited_Type (Typ, N);
891 -- Ada 2005 (AI-287): Limited aggregates allowed
893 elsif Is_Limited_Type (Typ)
894 and Ada_Version < Ada_05
895 then
896 Error_Msg_N ("aggregate type cannot be limited", N);
897 Explain_Limited_Type (Typ, N);
899 elsif Is_Class_Wide_Type (Typ) then
900 Error_Msg_N ("type of aggregate cannot be class-wide", N);
902 elsif Typ = Any_String
903 or else Typ = Any_Composite
904 then
905 Error_Msg_N ("no unique type for aggregate", N);
906 Set_Etype (N, Any_Composite);
908 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
909 Error_Msg_N ("null record forbidden in array aggregate", N);
911 elsif Is_Record_Type (Typ) then
912 Resolve_Record_Aggregate (N, Typ);
914 elsif Is_Array_Type (Typ) then
916 -- First a special test, for the case of a positional aggregate
917 -- of characters which can be replaced by a string literal.
918 -- Do not perform this transformation if this was a string literal
919 -- to start with, whose components needed constraint checks, or if
920 -- the component type is non-static, because it will require those
921 -- checks and be transformed back into an aggregate.
923 if Number_Dimensions (Typ) = 1
924 and then
925 (Root_Type (Component_Type (Typ)) = Standard_Character
926 or else
927 Root_Type (Component_Type (Typ)) = Standard_Wide_Character
928 or else
929 Root_Type (Component_Type (Typ)) = Standard_Wide_Wide_Character)
930 and then No (Component_Associations (N))
931 and then not Is_Limited_Composite (Typ)
932 and then not Is_Private_Composite (Typ)
933 and then not Is_Bit_Packed_Array (Typ)
934 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
935 and then Is_Static_Subtype (Component_Type (Typ))
936 then
937 declare
938 Expr : Node_Id;
940 begin
941 Expr := First (Expressions (N));
942 while Present (Expr) loop
943 exit when Nkind (Expr) /= N_Character_Literal;
944 Next (Expr);
945 end loop;
947 if No (Expr) then
948 Start_String;
950 Expr := First (Expressions (N));
951 while Present (Expr) loop
952 Store_String_Char (UI_To_CC (Char_Literal_Value (Expr)));
953 Next (Expr);
954 end loop;
956 Rewrite (N,
957 Make_String_Literal (Sloc (N), End_String));
959 Analyze_And_Resolve (N, Typ);
960 return;
961 end if;
962 end;
963 end if;
965 -- Here if we have a real aggregate to deal with
967 Array_Aggregate : declare
968 Aggr_Resolved : Boolean;
970 Aggr_Typ : constant Entity_Id := Etype (Typ);
971 -- This is the unconstrained array type, which is the type
972 -- against which the aggregate is to be resolved. Typ itself
973 -- is the array type of the context which may not be the same
974 -- subtype as the subtype for the final aggregate.
976 begin
977 -- In the following we determine whether an others choice is
978 -- allowed inside the array aggregate. The test checks the context
979 -- in which the array aggregate occurs. If the context does not
980 -- permit it, or the aggregate type is unconstrained, an others
981 -- choice is not allowed.
983 -- If expansion is disabled (generic context, or semantics-only
984 -- mode) actual subtypes cannot be constructed, and the type of
985 -- an object may be its unconstrained nominal type. However, if
986 -- the context is an assignment, we assume that "others" is
987 -- allowed, because the target of the assignment will have a
988 -- constrained subtype when fully compiled.
990 -- Note that there is no node for Explicit_Actual_Parameter.
991 -- To test for this context we therefore have to test for node
992 -- N_Parameter_Association which itself appears only if there is a
993 -- formal parameter. Consequently we also need to test for
994 -- N_Procedure_Call_Statement or N_Function_Call.
996 Set_Etype (N, Aggr_Typ); -- may be overridden later on
998 if Is_Constrained (Typ) and then
999 (Pkind = N_Assignment_Statement or else
1000 Pkind = N_Parameter_Association or else
1001 Pkind = N_Function_Call or else
1002 Pkind = N_Procedure_Call_Statement or else
1003 Pkind = N_Generic_Association or else
1004 Pkind = N_Formal_Object_Declaration or else
1005 Pkind = N_Return_Statement or else
1006 Pkind = N_Object_Declaration or else
1007 Pkind = N_Component_Declaration or else
1008 Pkind = N_Parameter_Specification or else
1009 Pkind = N_Qualified_Expression or else
1010 Pkind = N_Aggregate or else
1011 Pkind = N_Extension_Aggregate or else
1012 Pkind = N_Component_Association)
1013 then
1014 Aggr_Resolved :=
1015 Resolve_Array_Aggregate
1017 Index => First_Index (Aggr_Typ),
1018 Index_Constr => First_Index (Typ),
1019 Component_Typ => Component_Type (Typ),
1020 Others_Allowed => True);
1022 elsif not Expander_Active
1023 and then Pkind = N_Assignment_Statement
1024 then
1025 Aggr_Resolved :=
1026 Resolve_Array_Aggregate
1028 Index => First_Index (Aggr_Typ),
1029 Index_Constr => First_Index (Typ),
1030 Component_Typ => Component_Type (Typ),
1031 Others_Allowed => True);
1032 else
1033 Aggr_Resolved :=
1034 Resolve_Array_Aggregate
1036 Index => First_Index (Aggr_Typ),
1037 Index_Constr => First_Index (Aggr_Typ),
1038 Component_Typ => Component_Type (Typ),
1039 Others_Allowed => False);
1040 end if;
1042 if not Aggr_Resolved then
1043 Aggr_Subtyp := Any_Composite;
1044 else
1045 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1046 end if;
1048 Set_Etype (N, Aggr_Subtyp);
1049 end Array_Aggregate;
1051 elsif Is_Private_Type (Typ)
1052 and then Present (Full_View (Typ))
1053 and then In_Inlined_Body
1054 and then Is_Composite_Type (Full_View (Typ))
1055 then
1056 Resolve (N, Full_View (Typ));
1058 else
1059 Error_Msg_N ("illegal context for aggregate", N);
1060 end if;
1062 -- If we can determine statically that the evaluation of the
1063 -- aggregate raises Constraint_Error, then replace the
1064 -- aggregate with an N_Raise_Constraint_Error node, but set the
1065 -- Etype to the right aggregate subtype. Gigi needs this.
1067 if Raises_Constraint_Error (N) then
1068 Aggr_Subtyp := Etype (N);
1069 Rewrite (N,
1070 Make_Raise_Constraint_Error (Sloc (N),
1071 Reason => CE_Range_Check_Failed));
1072 Set_Raises_Constraint_Error (N);
1073 Set_Etype (N, Aggr_Subtyp);
1074 Set_Analyzed (N);
1075 end if;
1076 end Resolve_Aggregate;
1078 -----------------------------
1079 -- Resolve_Array_Aggregate --
1080 -----------------------------
1082 function Resolve_Array_Aggregate
1083 (N : Node_Id;
1084 Index : Node_Id;
1085 Index_Constr : Node_Id;
1086 Component_Typ : Entity_Id;
1087 Others_Allowed : Boolean)
1088 return Boolean
1090 Loc : constant Source_Ptr := Sloc (N);
1092 Failure : constant Boolean := False;
1093 Success : constant Boolean := True;
1095 Index_Typ : constant Entity_Id := Etype (Index);
1096 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1097 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1098 -- The type of the index corresponding to the array sub-aggregate
1099 -- along with its low and upper bounds
1101 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1102 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1103 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1104 -- ditto for the base type
1106 function Add (Val : Uint; To : Node_Id) return Node_Id;
1107 -- Creates a new expression node where Val is added to expression To.
1108 -- Tries to constant fold whenever possible. To must be an already
1109 -- analyzed expression.
1111 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1112 -- Checks that AH (the upper bound of an array aggregate) is <= BH
1113 -- (the upper bound of the index base type). If the check fails a
1114 -- warning is emitted, the Raises_Constraint_Error Flag of N is set,
1115 -- and AH is replaced with a duplicate of BH.
1117 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1118 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1119 -- warning if not and sets the Raises_Constraint_Error Flag in N.
1121 procedure Check_Length (L, H : Node_Id; Len : Uint);
1122 -- Checks that range L .. H contains at least Len elements. Emits a
1123 -- warning if not and sets the Raises_Constraint_Error Flag in N.
1125 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1126 -- Returns True if range L .. H is dynamic or null
1128 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1129 -- Given expression node From, this routine sets OK to False if it
1130 -- cannot statically evaluate From. Otherwise it stores this static
1131 -- value into Value.
1133 function Resolve_Aggr_Expr
1134 (Expr : Node_Id;
1135 Single_Elmt : Boolean)
1136 return Boolean;
1137 -- Resolves aggregate expression Expr. Returs False if resolution
1138 -- fails. If Single_Elmt is set to False, the expression Expr may be
1139 -- used to initialize several array aggregate elements (this can
1140 -- happen for discrete choices such as "L .. H => Expr" or the others
1141 -- choice). In this event we do not resolve Expr unless expansion is
1142 -- disabled. To know why, see the DELAYED COMPONENT RESOLUTION
1143 -- note above.
1145 ---------
1146 -- Add --
1147 ---------
1149 function Add (Val : Uint; To : Node_Id) return Node_Id is
1150 Expr_Pos : Node_Id;
1151 Expr : Node_Id;
1152 To_Pos : Node_Id;
1154 begin
1155 if Raises_Constraint_Error (To) then
1156 return To;
1157 end if;
1159 -- First test if we can do constant folding
1161 if Compile_Time_Known_Value (To)
1162 or else Nkind (To) = N_Integer_Literal
1163 then
1164 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1165 Set_Is_Static_Expression (Expr_Pos);
1166 Set_Etype (Expr_Pos, Etype (To));
1167 Set_Analyzed (Expr_Pos, Analyzed (To));
1169 if not Is_Enumeration_Type (Index_Typ) then
1170 Expr := Expr_Pos;
1172 -- If we are dealing with enumeration return
1173 -- Index_Typ'Val (Expr_Pos)
1175 else
1176 Expr :=
1177 Make_Attribute_Reference
1178 (Loc,
1179 Prefix => New_Reference_To (Index_Typ, Loc),
1180 Attribute_Name => Name_Val,
1181 Expressions => New_List (Expr_Pos));
1182 end if;
1184 return Expr;
1185 end if;
1187 -- If we are here no constant folding possible
1189 if not Is_Enumeration_Type (Index_Base) then
1190 Expr :=
1191 Make_Op_Add (Loc,
1192 Left_Opnd => Duplicate_Subexpr (To),
1193 Right_Opnd => Make_Integer_Literal (Loc, Val));
1195 -- If we are dealing with enumeration return
1196 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1198 else
1199 To_Pos :=
1200 Make_Attribute_Reference
1201 (Loc,
1202 Prefix => New_Reference_To (Index_Typ, Loc),
1203 Attribute_Name => Name_Pos,
1204 Expressions => New_List (Duplicate_Subexpr (To)));
1206 Expr_Pos :=
1207 Make_Op_Add (Loc,
1208 Left_Opnd => To_Pos,
1209 Right_Opnd => Make_Integer_Literal (Loc, Val));
1211 Expr :=
1212 Make_Attribute_Reference
1213 (Loc,
1214 Prefix => New_Reference_To (Index_Typ, Loc),
1215 Attribute_Name => Name_Val,
1216 Expressions => New_List (Expr_Pos));
1217 end if;
1219 return Expr;
1220 end Add;
1222 -----------------
1223 -- Check_Bound --
1224 -----------------
1226 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1227 Val_BH : Uint;
1228 Val_AH : Uint;
1230 OK_BH : Boolean;
1231 OK_AH : Boolean;
1233 begin
1234 Get (Value => Val_BH, From => BH, OK => OK_BH);
1235 Get (Value => Val_AH, From => AH, OK => OK_AH);
1237 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1238 Set_Raises_Constraint_Error (N);
1239 Error_Msg_N ("upper bound out of range?", AH);
1240 Error_Msg_N ("Constraint_Error will be raised at run-time?", AH);
1242 -- You need to set AH to BH or else in the case of enumerations
1243 -- indices we will not be able to resolve the aggregate bounds.
1245 AH := Duplicate_Subexpr (BH);
1246 end if;
1247 end Check_Bound;
1249 ------------------
1250 -- Check_Bounds --
1251 ------------------
1253 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1254 Val_L : Uint;
1255 Val_H : Uint;
1256 Val_AL : Uint;
1257 Val_AH : Uint;
1259 OK_L : Boolean;
1260 OK_H : Boolean;
1261 OK_AL : Boolean;
1262 OK_AH : Boolean;
1264 begin
1265 if Raises_Constraint_Error (N)
1266 or else Dynamic_Or_Null_Range (AL, AH)
1267 then
1268 return;
1269 end if;
1271 Get (Value => Val_L, From => L, OK => OK_L);
1272 Get (Value => Val_H, From => H, OK => OK_H);
1274 Get (Value => Val_AL, From => AL, OK => OK_AL);
1275 Get (Value => Val_AH, From => AH, OK => OK_AH);
1277 if OK_L and then Val_L > Val_AL then
1278 Set_Raises_Constraint_Error (N);
1279 Error_Msg_N ("lower bound of aggregate out of range?", N);
1280 Error_Msg_N ("\Constraint_Error will be raised at run-time?", N);
1281 end if;
1283 if OK_H and then Val_H < Val_AH then
1284 Set_Raises_Constraint_Error (N);
1285 Error_Msg_N ("upper bound of aggregate out of range?", N);
1286 Error_Msg_N ("\Constraint_Error will be raised at run-time?", N);
1287 end if;
1288 end Check_Bounds;
1290 ------------------
1291 -- Check_Length --
1292 ------------------
1294 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1295 Val_L : Uint;
1296 Val_H : Uint;
1298 OK_L : Boolean;
1299 OK_H : Boolean;
1301 Range_Len : Uint;
1303 begin
1304 if Raises_Constraint_Error (N) then
1305 return;
1306 end if;
1308 Get (Value => Val_L, From => L, OK => OK_L);
1309 Get (Value => Val_H, From => H, OK => OK_H);
1311 if not OK_L or else not OK_H then
1312 return;
1313 end if;
1315 -- If null range length is zero
1317 if Val_L > Val_H then
1318 Range_Len := Uint_0;
1319 else
1320 Range_Len := Val_H - Val_L + 1;
1321 end if;
1323 if Range_Len < Len then
1324 Set_Raises_Constraint_Error (N);
1325 Error_Msg_N ("too many elements?", N);
1326 Error_Msg_N ("Constraint_Error will be raised at run-time?", N);
1327 end if;
1328 end Check_Length;
1330 ---------------------------
1331 -- Dynamic_Or_Null_Range --
1332 ---------------------------
1334 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1335 Val_L : Uint;
1336 Val_H : Uint;
1338 OK_L : Boolean;
1339 OK_H : Boolean;
1341 begin
1342 Get (Value => Val_L, From => L, OK => OK_L);
1343 Get (Value => Val_H, From => H, OK => OK_H);
1345 return not OK_L or else not OK_H
1346 or else not Is_OK_Static_Expression (L)
1347 or else not Is_OK_Static_Expression (H)
1348 or else Val_L > Val_H;
1349 end Dynamic_Or_Null_Range;
1351 ---------
1352 -- Get --
1353 ---------
1355 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1356 begin
1357 OK := True;
1359 if Compile_Time_Known_Value (From) then
1360 Value := Expr_Value (From);
1362 -- If expression From is something like Some_Type'Val (10) then
1363 -- Value = 10
1365 elsif Nkind (From) = N_Attribute_Reference
1366 and then Attribute_Name (From) = Name_Val
1367 and then Compile_Time_Known_Value (First (Expressions (From)))
1368 then
1369 Value := Expr_Value (First (Expressions (From)));
1371 else
1372 Value := Uint_0;
1373 OK := False;
1374 end if;
1375 end Get;
1377 -----------------------
1378 -- Resolve_Aggr_Expr --
1379 -----------------------
1381 function Resolve_Aggr_Expr
1382 (Expr : Node_Id;
1383 Single_Elmt : Boolean)
1384 return Boolean
1386 Nxt_Ind : constant Node_Id := Next_Index (Index);
1387 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
1388 -- Index is the current index corresponding to the expresion
1390 Resolution_OK : Boolean := True;
1391 -- Set to False if resolution of the expression failed
1393 begin
1394 -- If the array type against which we are resolving the aggregate
1395 -- has several dimensions, the expressions nested inside the
1396 -- aggregate must be further aggregates (or strings).
1398 if Present (Nxt_Ind) then
1399 if Nkind (Expr) /= N_Aggregate then
1401 -- A string literal can appear where a one-dimensional array
1402 -- of characters is expected. If the literal looks like an
1403 -- operator, it is still an operator symbol, which will be
1404 -- transformed into a string when analyzed.
1406 if Is_Character_Type (Component_Typ)
1407 and then No (Next_Index (Nxt_Ind))
1408 and then (Nkind (Expr) = N_String_Literal
1409 or else Nkind (Expr) = N_Operator_Symbol)
1410 then
1411 -- A string literal used in a multidimensional array
1412 -- aggregate in place of the final one-dimensional
1413 -- aggregate must not be enclosed in parentheses.
1415 if Paren_Count (Expr) /= 0 then
1416 Error_Msg_N ("no parenthesis allowed here", Expr);
1417 end if;
1419 Make_String_Into_Aggregate (Expr);
1421 else
1422 Error_Msg_N ("nested array aggregate expected", Expr);
1423 return Failure;
1424 end if;
1425 end if;
1427 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
1428 -- Required to check the null-exclusion attribute (if present).
1429 -- This value may be overridden later on.
1431 Set_Etype (Expr, Etype (N));
1433 Resolution_OK := Resolve_Array_Aggregate
1434 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1436 -- Do not resolve the expressions of discrete or others choices
1437 -- unless the expression covers a single component, or the expander
1438 -- is inactive.
1440 elsif Single_Elmt
1441 or else not Expander_Active
1442 or else In_Default_Expression
1443 then
1444 Analyze_And_Resolve (Expr, Component_Typ);
1445 Check_Non_Static_Context (Expr);
1446 Aggregate_Constraint_Checks (Expr, Component_Typ);
1447 Check_Unset_Reference (Expr);
1448 end if;
1450 if Raises_Constraint_Error (Expr)
1451 and then Nkind (Parent (Expr)) /= N_Component_Association
1452 then
1453 Set_Raises_Constraint_Error (N);
1454 end if;
1456 return Resolution_OK;
1457 end Resolve_Aggr_Expr;
1459 -- Variables local to Resolve_Array_Aggregate
1461 Assoc : Node_Id;
1462 Choice : Node_Id;
1463 Expr : Node_Id;
1465 Who_Cares : Node_Id;
1467 Aggr_Low : Node_Id := Empty;
1468 Aggr_High : Node_Id := Empty;
1469 -- The actual low and high bounds of this sub-aggegate
1471 Choices_Low : Node_Id := Empty;
1472 Choices_High : Node_Id := Empty;
1473 -- The lowest and highest discrete choices values for a named aggregate
1475 Nb_Elements : Uint := Uint_0;
1476 -- The number of elements in a positional aggegate
1478 Others_Present : Boolean := False;
1480 Nb_Choices : Nat := 0;
1481 -- Contains the overall number of named choices in this sub-aggregate
1483 Nb_Discrete_Choices : Nat := 0;
1484 -- The overall number of discrete choices (not counting others choice)
1486 Case_Table_Size : Nat;
1487 -- Contains the size of the case table needed to sort aggregate choices
1489 -- Start of processing for Resolve_Array_Aggregate
1491 begin
1492 -- STEP 1: make sure the aggregate is correctly formatted
1494 if Present (Component_Associations (N)) then
1495 Assoc := First (Component_Associations (N));
1496 while Present (Assoc) loop
1497 Choice := First (Choices (Assoc));
1498 while Present (Choice) loop
1499 if Nkind (Choice) = N_Others_Choice then
1500 Others_Present := True;
1502 if Choice /= First (Choices (Assoc))
1503 or else Present (Next (Choice))
1504 then
1505 Error_Msg_N
1506 ("OTHERS must appear alone in a choice list", Choice);
1507 return Failure;
1508 end if;
1510 if Present (Next (Assoc)) then
1511 Error_Msg_N
1512 ("OTHERS must appear last in an aggregate", Choice);
1513 return Failure;
1514 end if;
1516 if Ada_Version = Ada_83
1517 and then Assoc /= First (Component_Associations (N))
1518 and then (Nkind (Parent (N)) = N_Assignment_Statement
1519 or else
1520 Nkind (Parent (N)) = N_Object_Declaration)
1521 then
1522 Error_Msg_N
1523 ("(Ada 83) illegal context for OTHERS choice", N);
1524 end if;
1525 end if;
1527 Nb_Choices := Nb_Choices + 1;
1528 Next (Choice);
1529 end loop;
1531 Next (Assoc);
1532 end loop;
1533 end if;
1535 -- At this point we know that the others choice, if present, is by
1536 -- itself and appears last in the aggregate. Check if we have mixed
1537 -- positional and discrete associations (other than the others choice).
1539 if Present (Expressions (N))
1540 and then (Nb_Choices > 1
1541 or else (Nb_Choices = 1 and then not Others_Present))
1542 then
1543 Error_Msg_N
1544 ("named association cannot follow positional association",
1545 First (Choices (First (Component_Associations (N)))));
1546 return Failure;
1547 end if;
1549 -- Test for the validity of an others choice if present
1551 if Others_Present and then not Others_Allowed then
1552 Error_Msg_N
1553 ("OTHERS choice not allowed here",
1554 First (Choices (First (Component_Associations (N)))));
1555 return Failure;
1556 end if;
1558 -- Protect against cascaded errors
1560 if Etype (Index_Typ) = Any_Type then
1561 return Failure;
1562 end if;
1564 -- STEP 2: Process named components
1566 if No (Expressions (N)) then
1568 if Others_Present then
1569 Case_Table_Size := Nb_Choices - 1;
1570 else
1571 Case_Table_Size := Nb_Choices;
1572 end if;
1574 Step_2 : declare
1575 Low : Node_Id;
1576 High : Node_Id;
1577 -- Denote the lowest and highest values in an aggregate choice
1579 Hi_Val : Uint;
1580 Lo_Val : Uint;
1581 -- High end of one range and Low end of the next. Should be
1582 -- contiguous if there is no hole in the list of values.
1584 Missing_Values : Boolean;
1585 -- Set True if missing index values
1587 S_Low : Node_Id := Empty;
1588 S_High : Node_Id := Empty;
1589 -- if a choice in an aggregate is a subtype indication these
1590 -- denote the lowest and highest values of the subtype
1592 Table : Case_Table_Type (1 .. Case_Table_Size);
1593 -- Used to sort all the different choice values
1595 Single_Choice : Boolean;
1596 -- Set to true every time there is a single discrete choice in a
1597 -- discrete association
1599 Prev_Nb_Discrete_Choices : Nat;
1600 -- Used to keep track of the number of discrete choices
1601 -- in the current association.
1603 begin
1604 -- STEP 2 (A): Check discrete choices validity
1606 Assoc := First (Component_Associations (N));
1607 while Present (Assoc) loop
1609 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
1610 Choice := First (Choices (Assoc));
1611 loop
1612 Analyze (Choice);
1614 if Nkind (Choice) = N_Others_Choice then
1615 Single_Choice := False;
1616 exit;
1618 -- Test for subtype mark without constraint
1620 elsif Is_Entity_Name (Choice) and then
1621 Is_Type (Entity (Choice))
1622 then
1623 if Base_Type (Entity (Choice)) /= Index_Base then
1624 Error_Msg_N
1625 ("invalid subtype mark in aggregate choice",
1626 Choice);
1627 return Failure;
1628 end if;
1630 elsif Nkind (Choice) = N_Subtype_Indication then
1631 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
1633 -- Does the subtype indication evaluation raise CE ?
1635 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
1636 Get_Index_Bounds (Choice, Low, High);
1637 Check_Bounds (S_Low, S_High, Low, High);
1639 else -- Choice is a range or an expression
1640 Resolve (Choice, Index_Base);
1641 Check_Unset_Reference (Choice);
1642 Check_Non_Static_Context (Choice);
1644 -- Do not range check a choice. This check is redundant
1645 -- since this test is already performed when we check
1646 -- that the bounds of the array aggregate are within
1647 -- range.
1649 Set_Do_Range_Check (Choice, False);
1650 end if;
1652 -- If we could not resolve the discrete choice stop here
1654 if Etype (Choice) = Any_Type then
1655 return Failure;
1657 -- If the discrete choice raises CE get its original bounds
1659 elsif Nkind (Choice) = N_Raise_Constraint_Error then
1660 Set_Raises_Constraint_Error (N);
1661 Get_Index_Bounds (Original_Node (Choice), Low, High);
1663 -- Otherwise get its bounds as usual
1665 else
1666 Get_Index_Bounds (Choice, Low, High);
1667 end if;
1669 if (Dynamic_Or_Null_Range (Low, High)
1670 or else (Nkind (Choice) = N_Subtype_Indication
1671 and then
1672 Dynamic_Or_Null_Range (S_Low, S_High)))
1673 and then Nb_Choices /= 1
1674 then
1675 Error_Msg_N
1676 ("dynamic or empty choice in aggregate " &
1677 "must be the only choice", Choice);
1678 return Failure;
1679 end if;
1681 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
1682 Table (Nb_Discrete_Choices).Choice_Lo := Low;
1683 Table (Nb_Discrete_Choices).Choice_Hi := High;
1685 Next (Choice);
1687 if No (Choice) then
1688 -- Check if we have a single discrete choice and whether
1689 -- this discrete choice specifies a single value.
1691 Single_Choice :=
1692 (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1)
1693 and then (Low = High);
1695 exit;
1696 end if;
1697 end loop;
1699 -- Ada 2005 (AI-231)
1701 if Ada_Version >= Ada_05
1702 and then Nkind (Expression (Assoc)) = N_Null
1703 then
1704 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
1705 end if;
1707 -- Ada 2005 (AI-287): In case of default initialized component
1708 -- we delay the resolution to the expansion phase
1710 if Box_Present (Assoc) then
1712 -- Ada 2005 (AI-287): In case of default initialization
1713 -- of a component the expander will generate calls to
1714 -- the corresponding initialization subprogram.
1716 null;
1718 elsif not Resolve_Aggr_Expr (Expression (Assoc),
1719 Single_Elmt => Single_Choice)
1720 then
1721 return Failure;
1722 end if;
1724 Next (Assoc);
1725 end loop;
1727 -- If aggregate contains more than one choice then these must be
1728 -- static. Sort them and check that they are contiguous
1730 if Nb_Discrete_Choices > 1 then
1731 Sort_Case_Table (Table);
1732 Missing_Values := False;
1734 Outer : for J in 1 .. Nb_Discrete_Choices - 1 loop
1735 if Expr_Value (Table (J).Choice_Hi) >=
1736 Expr_Value (Table (J + 1).Choice_Lo)
1737 then
1738 Error_Msg_N
1739 ("duplicate choice values in array aggregate",
1740 Table (J).Choice_Hi);
1741 return Failure;
1743 elsif not Others_Present then
1745 Hi_Val := Expr_Value (Table (J).Choice_Hi);
1746 Lo_Val := Expr_Value (Table (J + 1).Choice_Lo);
1748 -- If missing values, output error messages
1750 if Lo_Val - Hi_Val > 1 then
1752 -- Header message if not first missing value
1754 if not Missing_Values then
1755 Error_Msg_N
1756 ("missing index value(s) in array aggregate", N);
1757 Missing_Values := True;
1758 end if;
1760 -- Output values of missing indexes
1762 Lo_Val := Lo_Val - 1;
1763 Hi_Val := Hi_Val + 1;
1765 -- Enumeration type case
1767 if Is_Enumeration_Type (Index_Typ) then
1768 Error_Msg_Name_1 :=
1769 Chars
1770 (Get_Enum_Lit_From_Pos
1771 (Index_Typ, Hi_Val, Loc));
1773 if Lo_Val = Hi_Val then
1774 Error_Msg_N ("\ %", N);
1775 else
1776 Error_Msg_Name_2 :=
1777 Chars
1778 (Get_Enum_Lit_From_Pos
1779 (Index_Typ, Lo_Val, Loc));
1780 Error_Msg_N ("\ % .. %", N);
1781 end if;
1783 -- Integer types case
1785 else
1786 Error_Msg_Uint_1 := Hi_Val;
1788 if Lo_Val = Hi_Val then
1789 Error_Msg_N ("\ ^", N);
1790 else
1791 Error_Msg_Uint_2 := Lo_Val;
1792 Error_Msg_N ("\ ^ .. ^", N);
1793 end if;
1794 end if;
1795 end if;
1796 end if;
1797 end loop Outer;
1799 if Missing_Values then
1800 Set_Etype (N, Any_Composite);
1801 return Failure;
1802 end if;
1803 end if;
1805 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
1807 if Nb_Discrete_Choices > 0 then
1808 Choices_Low := Table (1).Choice_Lo;
1809 Choices_High := Table (Nb_Discrete_Choices).Choice_Hi;
1810 end if;
1812 if Others_Present then
1813 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
1815 else
1816 Aggr_Low := Choices_Low;
1817 Aggr_High := Choices_High;
1818 end if;
1819 end Step_2;
1821 -- STEP 3: Process positional components
1823 else
1824 -- STEP 3 (A): Process positional elements
1826 Expr := First (Expressions (N));
1827 Nb_Elements := Uint_0;
1828 while Present (Expr) loop
1829 Nb_Elements := Nb_Elements + 1;
1831 -- Ada 2005 (AI-231)
1833 if Ada_Version >= Ada_05
1834 and then Nkind (Expr) = N_Null
1835 then
1836 Check_Can_Never_Be_Null (Etype (N), Expr);
1837 end if;
1839 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
1840 return Failure;
1841 end if;
1843 Next (Expr);
1844 end loop;
1846 if Others_Present then
1847 Assoc := Last (Component_Associations (N));
1849 -- Ada 2005 (AI-231)
1851 if Ada_Version >= Ada_05
1852 and then Nkind (Expression (Assoc)) = N_Null
1853 then
1854 Check_Can_Never_Be_Null
1855 (Etype (N), Expression (Assoc));
1856 end if;
1858 -- Ada 2005 (AI-287): In case of default initialized component
1859 -- we delay the resolution to the expansion phase.
1861 if Box_Present (Assoc) then
1863 -- Ada 2005 (AI-287): In case of default initialization
1864 -- of a component the expander will generate calls to
1865 -- the corresponding initialization subprogram.
1867 null;
1869 elsif not Resolve_Aggr_Expr (Expression (Assoc),
1870 Single_Elmt => False)
1871 then
1872 return Failure;
1873 end if;
1874 end if;
1876 -- STEP 3 (B): Compute the aggregate bounds
1878 if Others_Present then
1879 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
1881 else
1882 if Others_Allowed then
1883 Get_Index_Bounds (Index_Constr, Aggr_Low, Who_Cares);
1884 else
1885 Aggr_Low := Index_Typ_Low;
1886 end if;
1888 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
1889 Check_Bound (Index_Base_High, Aggr_High);
1890 end if;
1891 end if;
1893 -- STEP 4: Perform static aggregate checks and save the bounds
1895 -- Check (A)
1897 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
1898 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
1900 -- Check (B)
1902 if Others_Present and then Nb_Discrete_Choices > 0 then
1903 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
1904 Check_Bounds (Index_Typ_Low, Index_Typ_High,
1905 Choices_Low, Choices_High);
1906 Check_Bounds (Index_Base_Low, Index_Base_High,
1907 Choices_Low, Choices_High);
1909 -- Check (C)
1911 elsif Others_Present and then Nb_Elements > 0 then
1912 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
1913 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
1914 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
1915 end if;
1917 if Raises_Constraint_Error (Aggr_Low)
1918 or else Raises_Constraint_Error (Aggr_High)
1919 then
1920 Set_Raises_Constraint_Error (N);
1921 end if;
1923 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
1925 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
1926 -- since the addition node returned by Add is not yet analyzed. Attach
1927 -- to tree and analyze first. Reset analyzed flag to insure it will get
1928 -- analyzed when it is a literal bound whose type must be properly
1929 -- set.
1931 if Others_Present or else Nb_Discrete_Choices > 0 then
1932 Aggr_High := Duplicate_Subexpr (Aggr_High);
1934 if Etype (Aggr_High) = Universal_Integer then
1935 Set_Analyzed (Aggr_High, False);
1936 end if;
1937 end if;
1939 Set_Aggregate_Bounds
1940 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
1942 -- The bounds may contain expressions that must be inserted upwards.
1943 -- Attach them fully to the tree. After analysis, remove side effects
1944 -- from upper bound, if still needed.
1946 Set_Parent (Aggregate_Bounds (N), N);
1947 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
1948 Check_Unset_Reference (Aggregate_Bounds (N));
1950 if not Others_Present and then Nb_Discrete_Choices = 0 then
1951 Set_High_Bound (Aggregate_Bounds (N),
1952 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
1953 end if;
1955 return Success;
1956 end Resolve_Array_Aggregate;
1958 ---------------------------------
1959 -- Resolve_Extension_Aggregate --
1960 ---------------------------------
1962 -- There are two cases to consider:
1964 -- a) If the ancestor part is a type mark, the components needed are
1965 -- the difference between the components of the expected type and the
1966 -- components of the given type mark.
1968 -- b) If the ancestor part is an expression, it must be unambiguous,
1969 -- and once we have its type we can also compute the needed components
1970 -- as in the previous case. In both cases, if the ancestor type is not
1971 -- the immediate ancestor, we have to build this ancestor recursively.
1973 -- In both cases discriminants of the ancestor type do not play a
1974 -- role in the resolution of the needed components, because inherited
1975 -- discriminants cannot be used in a type extension. As a result we can
1976 -- compute independently the list of components of the ancestor type and
1977 -- of the expected type.
1979 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
1980 A : constant Node_Id := Ancestor_Part (N);
1981 A_Type : Entity_Id;
1982 I : Interp_Index;
1983 It : Interp;
1985 function Valid_Ancestor_Type return Boolean;
1986 -- Verify that the type of the ancestor part is a non-private ancestor
1987 -- of the expected type.
1989 -------------------------
1990 -- Valid_Ancestor_Type --
1991 -------------------------
1993 function Valid_Ancestor_Type return Boolean is
1994 Imm_Type : Entity_Id;
1996 begin
1997 Imm_Type := Base_Type (Typ);
1998 while Is_Derived_Type (Imm_Type)
1999 and then Etype (Imm_Type) /= Base_Type (A_Type)
2000 loop
2001 Imm_Type := Etype (Base_Type (Imm_Type));
2002 end loop;
2004 if Etype (Imm_Type) /= Base_Type (A_Type) then
2005 Error_Msg_NE ("expect ancestor type of &", A, Typ);
2006 return False;
2007 else
2008 return True;
2009 end if;
2010 end Valid_Ancestor_Type;
2012 -- Start of processing for Resolve_Extension_Aggregate
2014 begin
2015 Analyze (A);
2017 if not Is_Tagged_Type (Typ) then
2018 Error_Msg_N ("type of extension aggregate must be tagged", N);
2019 return;
2021 elsif Is_Limited_Type (Typ) then
2023 -- Ada 2005 (AI-287): Limited aggregates are allowed
2025 if Ada_Version < Ada_05 then
2026 Error_Msg_N ("aggregate type cannot be limited", N);
2027 Explain_Limited_Type (Typ, N);
2028 return;
2029 end if;
2031 elsif Is_Class_Wide_Type (Typ) then
2032 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
2033 return;
2034 end if;
2036 if Is_Entity_Name (A)
2037 and then Is_Type (Entity (A))
2038 then
2039 A_Type := Get_Full_View (Entity (A));
2041 if Valid_Ancestor_Type then
2042 Set_Entity (A, A_Type);
2043 Set_Etype (A, A_Type);
2045 Validate_Ancestor_Part (N);
2046 Resolve_Record_Aggregate (N, Typ);
2047 end if;
2049 elsif Nkind (A) /= N_Aggregate then
2050 if Is_Overloaded (A) then
2051 A_Type := Any_Type;
2052 Get_First_Interp (A, I, It);
2054 while Present (It.Typ) loop
2056 if Is_Tagged_Type (It.Typ)
2057 and then not Is_Limited_Type (It.Typ)
2058 then
2059 if A_Type /= Any_Type then
2060 Error_Msg_N ("cannot resolve expression", A);
2061 return;
2062 else
2063 A_Type := It.Typ;
2064 end if;
2065 end if;
2067 Get_Next_Interp (I, It);
2068 end loop;
2070 if A_Type = Any_Type then
2071 Error_Msg_N
2072 ("ancestor part must be non-limited tagged type", A);
2073 return;
2074 end if;
2076 else
2077 A_Type := Etype (A);
2078 end if;
2080 if Valid_Ancestor_Type then
2081 Resolve (A, A_Type);
2082 Check_Unset_Reference (A);
2083 Check_Non_Static_Context (A);
2085 if Is_Class_Wide_Type (Etype (A))
2086 and then Nkind (Original_Node (A)) = N_Function_Call
2087 then
2088 -- If the ancestor part is a dispatching call, it appears
2089 -- statically to be a legal ancestor, but it yields any
2090 -- member of the class, and it is not possible to determine
2091 -- whether it is an ancestor of the extension aggregate (much
2092 -- less which ancestor). It is not possible to determine the
2093 -- required components of the extension part.
2095 -- This check implements AI-306, which in fact was motivated
2096 -- by an ACT query to the ARG after this test was added.
2098 Error_Msg_N ("ancestor part must be statically tagged", A);
2099 else
2100 Resolve_Record_Aggregate (N, Typ);
2101 end if;
2102 end if;
2104 else
2105 Error_Msg_N (" No unique type for this aggregate", A);
2106 end if;
2107 end Resolve_Extension_Aggregate;
2109 ------------------------------
2110 -- Resolve_Record_Aggregate --
2111 ------------------------------
2113 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
2114 New_Assoc_List : constant List_Id := New_List;
2115 New_Assoc : Node_Id;
2116 -- New_Assoc_List is the newly built list of N_Component_Association
2117 -- nodes. New_Assoc is one such N_Component_Association node in it.
2118 -- Please note that while Assoc and New_Assoc contain the same
2119 -- kind of nodes, they are used to iterate over two different
2120 -- N_Component_Association lists.
2122 Others_Etype : Entity_Id := Empty;
2123 -- This variable is used to save the Etype of the last record component
2124 -- that takes its value from the others choice. Its purpose is:
2126 -- (a) make sure the others choice is useful
2128 -- (b) make sure the type of all the components whose value is
2129 -- subsumed by the others choice are the same.
2131 -- This variable is updated as a side effect of function Get_Value
2133 Mbox_Present : Boolean := False;
2134 Others_Mbox : Boolean := False;
2135 -- Ada 2005 (AI-287): Variables used in case of default initialization
2136 -- to provide a functionality similar to Others_Etype. Mbox_Present
2137 -- indicates that the component takes its default initialization;
2138 -- Others_Mbox indicates that at least one component takes its default
2139 -- initialization. Similar to Others_Etype, they are also updated as a
2140 -- side effect of function Get_Value.
2142 procedure Add_Association
2143 (Component : Entity_Id;
2144 Expr : Node_Id;
2145 Box_Present : Boolean := False);
2146 -- Builds a new N_Component_Association node which associates
2147 -- Component to expression Expr and adds it to the new association
2148 -- list New_Assoc_List being built.
2150 function Discr_Present (Discr : Entity_Id) return Boolean;
2151 -- If aggregate N is a regular aggregate this routine will return True.
2152 -- Otherwise, if N is an extension aggregate, Discr is a discriminant
2153 -- whose value may already have been specified by N's ancestor part,
2154 -- this routine checks whether this is indeed the case and if so
2155 -- returns False, signaling that no value for Discr should appear in the
2156 -- N's aggregate part. Also, in this case, the routine appends to
2157 -- New_Assoc_List Discr the discriminant value specified in the ancestor
2158 -- part.
2160 function Get_Value
2161 (Compon : Node_Id;
2162 From : List_Id;
2163 Consider_Others_Choice : Boolean := False)
2164 return Node_Id;
2165 -- Given a record component stored in parameter Compon, the
2166 -- following function returns its value as it appears in the list
2167 -- From, which is a list of N_Component_Association nodes. If no
2168 -- component association has a choice for the searched component,
2169 -- the value provided by the others choice is returned, if there
2170 -- is one and Consider_Others_Choice is set to true. Otherwise
2171 -- Empty is returned. If there is more than one component association
2172 -- giving a value for the searched record component, an error message
2173 -- is emitted and the first found value is returned.
2175 -- If Consider_Others_Choice is set and the returned expression comes
2176 -- from the others choice, then Others_Etype is set as a side effect.
2177 -- An error message is emitted if the components taking their value
2178 -- from the others choice do not have same type.
2180 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id);
2181 -- Analyzes and resolves expression Expr against the Etype of the
2182 -- Component. This routine also applies all appropriate checks to Expr.
2183 -- It finally saves a Expr in the newly created association list that
2184 -- will be attached to the final record aggregate. Note that if the
2185 -- Parent pointer of Expr is not set then Expr was produced with a
2186 -- New_Copy_Tree or some such.
2188 ---------------------
2189 -- Add_Association --
2190 ---------------------
2192 procedure Add_Association
2193 (Component : Entity_Id;
2194 Expr : Node_Id;
2195 Box_Present : Boolean := False)
2197 Choice_List : constant List_Id := New_List;
2198 New_Assoc : Node_Id;
2200 begin
2201 Append (New_Occurrence_Of (Component, Sloc (Expr)), Choice_List);
2202 New_Assoc :=
2203 Make_Component_Association (Sloc (Expr),
2204 Choices => Choice_List,
2205 Expression => Expr,
2206 Box_Present => Box_Present);
2207 Append (New_Assoc, New_Assoc_List);
2208 end Add_Association;
2210 -------------------
2211 -- Discr_Present --
2212 -------------------
2214 function Discr_Present (Discr : Entity_Id) return Boolean is
2215 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
2217 Loc : Source_Ptr;
2219 Ancestor : Node_Id;
2220 Discr_Expr : Node_Id;
2222 Ancestor_Typ : Entity_Id;
2223 Orig_Discr : Entity_Id;
2224 D : Entity_Id;
2225 D_Val : Elmt_Id := No_Elmt; -- stop junk warning
2227 Ancestor_Is_Subtyp : Boolean;
2229 begin
2230 if Regular_Aggr then
2231 return True;
2232 end if;
2234 Ancestor := Ancestor_Part (N);
2235 Ancestor_Typ := Etype (Ancestor);
2236 Loc := Sloc (Ancestor);
2238 Ancestor_Is_Subtyp :=
2239 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
2241 -- If the ancestor part has no discriminants clearly N's aggregate
2242 -- part must provide a value for Discr.
2244 if not Has_Discriminants (Ancestor_Typ) then
2245 return True;
2247 -- If the ancestor part is an unconstrained subtype mark then the
2248 -- Discr must be present in N's aggregate part.
2250 elsif Ancestor_Is_Subtyp
2251 and then not Is_Constrained (Entity (Ancestor))
2252 then
2253 return True;
2254 end if;
2256 -- Now look to see if Discr was specified in the ancestor part
2258 if Ancestor_Is_Subtyp then
2259 D_Val := First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
2260 end if;
2262 Orig_Discr := Original_Record_Component (Discr);
2264 D := First_Discriminant (Ancestor_Typ);
2265 while Present (D) loop
2267 -- If Ancestor has already specified Disc value than insert its
2268 -- value in the final aggregate.
2270 if Original_Record_Component (D) = Orig_Discr then
2271 if Ancestor_Is_Subtyp then
2272 Discr_Expr := New_Copy_Tree (Node (D_Val));
2273 else
2274 Discr_Expr :=
2275 Make_Selected_Component (Loc,
2276 Prefix => Duplicate_Subexpr (Ancestor),
2277 Selector_Name => New_Occurrence_Of (Discr, Loc));
2278 end if;
2280 Resolve_Aggr_Expr (Discr_Expr, Discr);
2281 return False;
2282 end if;
2284 Next_Discriminant (D);
2286 if Ancestor_Is_Subtyp then
2287 Next_Elmt (D_Val);
2288 end if;
2289 end loop;
2291 return True;
2292 end Discr_Present;
2294 ---------------
2295 -- Get_Value --
2296 ---------------
2298 function Get_Value
2299 (Compon : Node_Id;
2300 From : List_Id;
2301 Consider_Others_Choice : Boolean := False)
2302 return Node_Id
2304 Assoc : Node_Id;
2305 Expr : Node_Id := Empty;
2306 Selector_Name : Node_Id;
2308 procedure Check_Non_Limited_Type;
2309 -- Relax check to allow the default initialization of limited types.
2310 -- For example:
2311 -- record
2312 -- C : Lim := (..., others => <>);
2313 -- end record;
2315 ----------------------------
2316 -- Check_Non_Limited_Type --
2317 ----------------------------
2319 procedure Check_Non_Limited_Type is
2320 begin
2321 if Is_Limited_Type (Etype (Compon))
2322 and then Comes_From_Source (Compon)
2323 and then not In_Instance_Body
2324 then
2325 -- Ada 2005 (AI-287): Limited aggregates are allowed
2327 if Ada_Version >= Ada_05
2328 and then Present (Expression (Assoc))
2329 and then Nkind (Expression (Assoc)) = N_Aggregate
2330 then
2331 null;
2332 else
2333 Error_Msg_N
2334 ("initialization not allowed for limited types", N);
2335 Explain_Limited_Type (Etype (Compon), Compon);
2336 end if;
2337 end if;
2338 end Check_Non_Limited_Type;
2340 -- Start of processing for Get_Value
2342 begin
2343 Mbox_Present := False;
2345 if Present (From) then
2346 Assoc := First (From);
2347 else
2348 return Empty;
2349 end if;
2351 while Present (Assoc) loop
2352 Selector_Name := First (Choices (Assoc));
2353 while Present (Selector_Name) loop
2354 if Nkind (Selector_Name) = N_Others_Choice then
2355 if Consider_Others_Choice and then No (Expr) then
2357 -- We need to duplicate the expression for each
2358 -- successive component covered by the others choice.
2359 -- This is redundant if the others_choice covers only
2360 -- one component (small optimization possible???), but
2361 -- indispensable otherwise, because each one must be
2362 -- expanded individually to preserve side-effects.
2364 -- Ada 2005 (AI-287): In case of default initialization
2365 -- of components, we duplicate the corresponding default
2366 -- expression (from the record type declaration).
2368 if Box_Present (Assoc) then
2369 Others_Mbox := True;
2370 Mbox_Present := True;
2372 if Expander_Active then
2373 return New_Copy_Tree (Expression (Parent (Compon)));
2374 else
2375 return Expression (Parent (Compon));
2376 end if;
2378 else
2379 Check_Non_Limited_Type;
2381 if Present (Others_Etype) and then
2382 Base_Type (Others_Etype) /= Base_Type (Etype
2383 (Compon))
2384 then
2385 Error_Msg_N ("components in OTHERS choice must " &
2386 "have same type", Selector_Name);
2387 end if;
2389 Others_Etype := Etype (Compon);
2391 if Expander_Active then
2392 return New_Copy_Tree (Expression (Assoc));
2393 else
2394 return Expression (Assoc);
2395 end if;
2396 end if;
2397 end if;
2399 elsif Chars (Compon) = Chars (Selector_Name) then
2400 if No (Expr) then
2402 -- Ada 2005 (AI-231)
2404 if Ada_Version >= Ada_05
2405 and then Nkind (Expression (Assoc)) = N_Null
2406 then
2407 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
2408 end if;
2410 -- We need to duplicate the expression when several
2411 -- components are grouped together with a "|" choice.
2412 -- For instance "filed1 | filed2 => Expr"
2414 -- Ada 2005 (AI-287)
2416 if Box_Present (Assoc) then
2417 Mbox_Present := True;
2419 -- Duplicate the default expression of the component
2420 -- from the record type declaration
2422 if Present (Next (Selector_Name)) then
2423 Expr :=
2424 New_Copy_Tree (Expression (Parent (Compon)));
2425 else
2426 Expr := Expression (Parent (Compon));
2427 end if;
2429 else
2430 Check_Non_Limited_Type;
2432 if Present (Next (Selector_Name)) then
2433 Expr := New_Copy_Tree (Expression (Assoc));
2434 else
2435 Expr := Expression (Assoc);
2436 end if;
2437 end if;
2439 Generate_Reference (Compon, Selector_Name);
2441 else
2442 Error_Msg_NE
2443 ("more than one value supplied for &",
2444 Selector_Name, Compon);
2446 end if;
2447 end if;
2449 Next (Selector_Name);
2450 end loop;
2452 Next (Assoc);
2453 end loop;
2455 return Expr;
2456 end Get_Value;
2458 -----------------------
2459 -- Resolve_Aggr_Expr --
2460 -----------------------
2462 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id) is
2463 New_C : Entity_Id := Component;
2464 Expr_Type : Entity_Id := Empty;
2466 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
2467 -- If the expression is an aggregate (possibly qualified) then its
2468 -- expansion is delayed until the enclosing aggregate is expanded
2469 -- into assignments. In that case, do not generate checks on the
2470 -- expression, because they will be generated later, and will other-
2471 -- wise force a copy (to remove side-effects) that would leave a
2472 -- dynamic-sized aggregate in the code, something that gigi cannot
2473 -- handle.
2475 Relocate : Boolean;
2476 -- Set to True if the resolved Expr node needs to be relocated
2477 -- when attached to the newly created association list. This node
2478 -- need not be relocated if its parent pointer is not set.
2479 -- In fact in this case Expr is the output of a New_Copy_Tree call.
2480 -- if Relocate is True then we have analyzed the expression node
2481 -- in the original aggregate and hence it needs to be relocated
2482 -- when moved over the new association list.
2484 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
2485 Kind : constant Node_Kind := Nkind (Expr);
2487 begin
2488 return ((Kind = N_Aggregate
2489 or else Kind = N_Extension_Aggregate)
2490 and then Present (Etype (Expr))
2491 and then Is_Record_Type (Etype (Expr))
2492 and then Expansion_Delayed (Expr))
2494 or else (Kind = N_Qualified_Expression
2495 and then Has_Expansion_Delayed (Expression (Expr)));
2496 end Has_Expansion_Delayed;
2498 -- Start of processing for Resolve_Aggr_Expr
2500 begin
2501 -- If the type of the component is elementary or the type of the
2502 -- aggregate does not contain discriminants, use the type of the
2503 -- component to resolve Expr.
2505 if Is_Elementary_Type (Etype (Component))
2506 or else not Has_Discriminants (Etype (N))
2507 then
2508 Expr_Type := Etype (Component);
2510 -- Otherwise we have to pick up the new type of the component from
2511 -- the new costrained subtype of the aggregate. In fact components
2512 -- which are of a composite type might be constrained by a
2513 -- discriminant, and we want to resolve Expr against the subtype were
2514 -- all discriminant occurrences are replaced with their actual value.
2516 else
2517 New_C := First_Component (Etype (N));
2518 while Present (New_C) loop
2519 if Chars (New_C) = Chars (Component) then
2520 Expr_Type := Etype (New_C);
2521 exit;
2522 end if;
2524 Next_Component (New_C);
2525 end loop;
2527 pragma Assert (Present (Expr_Type));
2529 -- For each range in an array type where a discriminant has been
2530 -- replaced with the constraint, check that this range is within
2531 -- the range of the base type. This checks is done in the init
2532 -- proc for regular objects, but has to be done here for
2533 -- aggregates since no init proc is called for them.
2535 if Is_Array_Type (Expr_Type) then
2536 declare
2537 Index : Node_Id := First_Index (Expr_Type);
2538 -- Range of the current constrained index in the array
2540 Orig_Index : Node_Id := First_Index (Etype (Component));
2541 -- Range corresponding to the range Index above in the
2542 -- original unconstrained record type. The bounds of this
2543 -- range may be governed by discriminants.
2545 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
2546 -- Range corresponding to the range Index above for the
2547 -- unconstrained array type. This range is needed to apply
2548 -- range checks.
2550 begin
2551 while Present (Index) loop
2552 if Depends_On_Discriminant (Orig_Index) then
2553 Apply_Range_Check (Index, Etype (Unconstr_Index));
2554 end if;
2556 Next_Index (Index);
2557 Next_Index (Orig_Index);
2558 Next_Index (Unconstr_Index);
2559 end loop;
2560 end;
2561 end if;
2562 end if;
2564 -- If the Parent pointer of Expr is not set, Expr is an expression
2565 -- duplicated by New_Tree_Copy (this happens for record aggregates
2566 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
2567 -- Such a duplicated expression must be attached to the tree
2568 -- before analysis and resolution to enforce the rule that a tree
2569 -- fragment should never be analyzed or resolved unless it is
2570 -- attached to the current compilation unit.
2572 if No (Parent (Expr)) then
2573 Set_Parent (Expr, N);
2574 Relocate := False;
2575 else
2576 Relocate := True;
2577 end if;
2579 Analyze_And_Resolve (Expr, Expr_Type);
2580 Check_Non_Static_Context (Expr);
2581 Check_Unset_Reference (Expr);
2583 if not Has_Expansion_Delayed (Expr) then
2584 Aggregate_Constraint_Checks (Expr, Expr_Type);
2585 end if;
2587 if Raises_Constraint_Error (Expr) then
2588 Set_Raises_Constraint_Error (N);
2589 end if;
2591 if Relocate then
2592 Add_Association (New_C, Relocate_Node (Expr));
2593 else
2594 Add_Association (New_C, Expr);
2595 end if;
2596 end Resolve_Aggr_Expr;
2598 -- Resolve_Record_Aggregate local variables
2600 Assoc : Node_Id;
2601 -- N_Component_Association node belonging to the input aggregate N
2603 Expr : Node_Id;
2604 Positional_Expr : Node_Id;
2605 Component : Entity_Id;
2606 Component_Elmt : Elmt_Id;
2608 Components : constant Elist_Id := New_Elmt_List;
2609 -- Components is the list of the record components whose value must
2610 -- be provided in the aggregate. This list does include discriminants.
2612 -- Start of processing for Resolve_Record_Aggregate
2614 begin
2615 -- We may end up calling Duplicate_Subexpr on expressions that are
2616 -- attached to New_Assoc_List. For this reason we need to attach it
2617 -- to the tree by setting its parent pointer to N. This parent point
2618 -- will change in STEP 8 below.
2620 Set_Parent (New_Assoc_List, N);
2622 -- STEP 1: abstract type and null record verification
2624 if Is_Abstract (Typ) then
2625 Error_Msg_N ("type of aggregate cannot be abstract", N);
2626 end if;
2628 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
2629 Set_Etype (N, Typ);
2630 return;
2632 elsif Present (First_Entity (Typ))
2633 and then Null_Record_Present (N)
2634 and then not Is_Tagged_Type (Typ)
2635 then
2636 Error_Msg_N ("record aggregate cannot be null", N);
2637 return;
2639 elsif No (First_Entity (Typ)) then
2640 Error_Msg_N ("record aggregate must be null", N);
2641 return;
2642 end if;
2644 -- STEP 2: Verify aggregate structure
2646 Step_2 : declare
2647 Selector_Name : Node_Id;
2648 Bad_Aggregate : Boolean := False;
2650 begin
2651 if Present (Component_Associations (N)) then
2652 Assoc := First (Component_Associations (N));
2653 else
2654 Assoc := Empty;
2655 end if;
2657 while Present (Assoc) loop
2658 Selector_Name := First (Choices (Assoc));
2659 while Present (Selector_Name) loop
2660 if Nkind (Selector_Name) = N_Identifier then
2661 null;
2663 elsif Nkind (Selector_Name) = N_Others_Choice then
2664 if Selector_Name /= First (Choices (Assoc))
2665 or else Present (Next (Selector_Name))
2666 then
2667 Error_Msg_N ("OTHERS must appear alone in a choice list",
2668 Selector_Name);
2669 return;
2671 elsif Present (Next (Assoc)) then
2672 Error_Msg_N ("OTHERS must appear last in an aggregate",
2673 Selector_Name);
2674 return;
2675 end if;
2677 else
2678 Error_Msg_N
2679 ("selector name should be identifier or OTHERS",
2680 Selector_Name);
2681 Bad_Aggregate := True;
2682 end if;
2684 Next (Selector_Name);
2685 end loop;
2687 Next (Assoc);
2688 end loop;
2690 if Bad_Aggregate then
2691 return;
2692 end if;
2693 end Step_2;
2695 -- STEP 3: Find discriminant Values
2697 Step_3 : declare
2698 Discrim : Entity_Id;
2699 Missing_Discriminants : Boolean := False;
2701 begin
2702 if Present (Expressions (N)) then
2703 Positional_Expr := First (Expressions (N));
2704 else
2705 Positional_Expr := Empty;
2706 end if;
2708 if Has_Discriminants (Typ) then
2709 Discrim := First_Discriminant (Typ);
2710 else
2711 Discrim := Empty;
2712 end if;
2714 -- First find the discriminant values in the positional components
2716 while Present (Discrim) and then Present (Positional_Expr) loop
2717 if Discr_Present (Discrim) then
2718 Resolve_Aggr_Expr (Positional_Expr, Discrim);
2720 -- Ada 2005 (AI-231)
2722 if Ada_Version >= Ada_05
2723 and then Nkind (Positional_Expr) = N_Null
2724 then
2725 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
2726 end if;
2728 Next (Positional_Expr);
2729 end if;
2731 if Present (Get_Value (Discrim, Component_Associations (N))) then
2732 Error_Msg_NE
2733 ("more than one value supplied for discriminant&",
2734 N, Discrim);
2735 end if;
2737 Next_Discriminant (Discrim);
2738 end loop;
2740 -- Find remaining discriminant values, if any, among named components
2742 while Present (Discrim) loop
2743 Expr := Get_Value (Discrim, Component_Associations (N), True);
2745 if not Discr_Present (Discrim) then
2746 if Present (Expr) then
2747 Error_Msg_NE
2748 ("more than one value supplied for discriminant&",
2749 N, Discrim);
2750 end if;
2752 elsif No (Expr) then
2753 Error_Msg_NE
2754 ("no value supplied for discriminant &", N, Discrim);
2755 Missing_Discriminants := True;
2757 else
2758 Resolve_Aggr_Expr (Expr, Discrim);
2759 end if;
2761 Next_Discriminant (Discrim);
2762 end loop;
2764 if Missing_Discriminants then
2765 return;
2766 end if;
2768 -- At this point and until the beginning of STEP 6, New_Assoc_List
2769 -- contains only the discriminants and their values.
2771 end Step_3;
2773 -- STEP 4: Set the Etype of the record aggregate
2775 -- ??? This code is pretty much a copy of Sem_Ch3.Build_Subtype. That
2776 -- routine should really be exported in sem_util or some such and used
2777 -- in sem_ch3 and here rather than have a copy of the code which is a
2778 -- maintenance nightmare.
2780 -- ??? Performace WARNING. The current implementation creates a new
2781 -- itype for all aggregates whose base type is discriminated.
2782 -- This means that for record aggregates nested inside an array
2783 -- aggregate we will create a new itype for each record aggregate
2784 -- if the array cmponent type has discriminants. For large aggregates
2785 -- this may be a problem. What should be done in this case is
2786 -- to reuse itypes as much as possible.
2788 if Has_Discriminants (Typ) then
2789 Build_Constrained_Itype : declare
2790 Loc : constant Source_Ptr := Sloc (N);
2791 Indic : Node_Id;
2792 Subtyp_Decl : Node_Id;
2793 Def_Id : Entity_Id;
2795 C : constant List_Id := New_List;
2797 begin
2798 New_Assoc := First (New_Assoc_List);
2799 while Present (New_Assoc) loop
2800 Append (Duplicate_Subexpr (Expression (New_Assoc)), To => C);
2801 Next (New_Assoc);
2802 end loop;
2804 Indic :=
2805 Make_Subtype_Indication (Loc,
2806 Subtype_Mark => New_Occurrence_Of (Base_Type (Typ), Loc),
2807 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
2809 Def_Id := Create_Itype (Ekind (Typ), N);
2811 Subtyp_Decl :=
2812 Make_Subtype_Declaration (Loc,
2813 Defining_Identifier => Def_Id,
2814 Subtype_Indication => Indic);
2815 Set_Parent (Subtyp_Decl, Parent (N));
2817 -- Itypes must be analyzed with checks off (see itypes.ads)
2819 Analyze (Subtyp_Decl, Suppress => All_Checks);
2821 Set_Etype (N, Def_Id);
2822 Check_Static_Discriminated_Subtype
2823 (Def_Id, Expression (First (New_Assoc_List)));
2824 end Build_Constrained_Itype;
2826 else
2827 Set_Etype (N, Typ);
2828 end if;
2830 -- STEP 5: Get remaining components according to discriminant values
2832 Step_5 : declare
2833 Record_Def : Node_Id;
2834 Parent_Typ : Entity_Id;
2835 Root_Typ : Entity_Id;
2836 Parent_Typ_List : Elist_Id;
2837 Parent_Elmt : Elmt_Id;
2838 Errors_Found : Boolean := False;
2839 Dnode : Node_Id;
2841 begin
2842 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
2843 Parent_Typ_List := New_Elmt_List;
2845 -- If this is an extension aggregate, the component list must
2846 -- include all components that are not in the given ancestor
2847 -- type. Otherwise, the component list must include components
2848 -- of all ancestors, starting with the root.
2850 if Nkind (N) = N_Extension_Aggregate then
2851 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
2852 else
2853 Root_Typ := Root_Type (Typ);
2855 if Nkind (Parent (Base_Type (Root_Typ)))
2856 = N_Private_Type_Declaration
2857 then
2858 Error_Msg_NE
2859 ("type of aggregate has private ancestor&!",
2860 N, Root_Typ);
2861 Error_Msg_N ("must use extension aggregate!", N);
2862 return;
2863 end if;
2865 Dnode := Declaration_Node (Base_Type (Root_Typ));
2867 -- If we don't get a full declaration, then we have some
2868 -- error which will get signalled later so skip this part.
2869 -- Otherwise, gather components of root that apply to the
2870 -- aggregate type. We use the base type in case there is an
2871 -- applicable stored constraint that renames the discriminants
2872 -- of the root.
2874 if Nkind (Dnode) = N_Full_Type_Declaration then
2875 Record_Def := Type_Definition (Dnode);
2876 Gather_Components (Base_Type (Typ),
2877 Component_List (Record_Def),
2878 Governed_By => New_Assoc_List,
2879 Into => Components,
2880 Report_Errors => Errors_Found);
2881 end if;
2882 end if;
2884 Parent_Typ := Base_Type (Typ);
2885 while Parent_Typ /= Root_Typ loop
2887 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
2888 Parent_Typ := Etype (Parent_Typ);
2890 if Nkind (Parent (Base_Type (Parent_Typ))) =
2891 N_Private_Type_Declaration
2892 or else Nkind (Parent (Base_Type (Parent_Typ))) =
2893 N_Private_Extension_Declaration
2894 then
2895 if Nkind (N) /= N_Extension_Aggregate then
2896 Error_Msg_NE
2897 ("type of aggregate has private ancestor&!",
2898 N, Parent_Typ);
2899 Error_Msg_N ("must use extension aggregate!", N);
2900 return;
2902 elsif Parent_Typ /= Root_Typ then
2903 Error_Msg_NE
2904 ("ancestor part of aggregate must be private type&",
2905 Ancestor_Part (N), Parent_Typ);
2906 return;
2907 end if;
2908 end if;
2909 end loop;
2911 -- Now collect components from all other ancestors
2913 Parent_Elmt := First_Elmt (Parent_Typ_List);
2914 while Present (Parent_Elmt) loop
2915 Parent_Typ := Node (Parent_Elmt);
2916 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
2917 Gather_Components (Empty,
2918 Component_List (Record_Extension_Part (Record_Def)),
2919 Governed_By => New_Assoc_List,
2920 Into => Components,
2921 Report_Errors => Errors_Found);
2923 Next_Elmt (Parent_Elmt);
2924 end loop;
2926 else
2927 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
2929 if Null_Present (Record_Def) then
2930 null;
2931 else
2932 Gather_Components (Base_Type (Typ),
2933 Component_List (Record_Def),
2934 Governed_By => New_Assoc_List,
2935 Into => Components,
2936 Report_Errors => Errors_Found);
2937 end if;
2938 end if;
2940 if Errors_Found then
2941 return;
2942 end if;
2943 end Step_5;
2945 -- STEP 6: Find component Values
2947 Component := Empty;
2948 Component_Elmt := First_Elmt (Components);
2950 -- First scan the remaining positional associations in the aggregate.
2951 -- Remember that at this point Positional_Expr contains the current
2952 -- positional association if any is left after looking for discriminant
2953 -- values in step 3.
2955 while Present (Positional_Expr) and then Present (Component_Elmt) loop
2956 Component := Node (Component_Elmt);
2957 Resolve_Aggr_Expr (Positional_Expr, Component);
2959 -- Ada 2005 (AI-231)
2961 if Ada_Version >= Ada_05
2962 and then Nkind (Positional_Expr) = N_Null
2963 then
2964 Check_Can_Never_Be_Null (Component, Positional_Expr);
2965 end if;
2967 if Present (Get_Value (Component, Component_Associations (N))) then
2968 Error_Msg_NE
2969 ("more than one value supplied for Component &", N, Component);
2970 end if;
2972 Next (Positional_Expr);
2973 Next_Elmt (Component_Elmt);
2974 end loop;
2976 if Present (Positional_Expr) then
2977 Error_Msg_N
2978 ("too many components for record aggregate", Positional_Expr);
2979 end if;
2981 -- Now scan for the named arguments of the aggregate
2983 while Present (Component_Elmt) loop
2984 Component := Node (Component_Elmt);
2985 Expr := Get_Value (Component, Component_Associations (N), True);
2987 -- Ada 2005 (AI-287): Default initialized limited component are
2988 -- passed to the expander, that will generate calls to the
2989 -- corresponding IP.
2991 if Mbox_Present and then Is_Limited_Type (Etype (Component)) then
2992 Add_Association
2993 (Component => Component,
2994 Expr => Empty,
2995 Box_Present => True);
2997 -- Ada 2005 (AI-287): No value supplied for component
2999 elsif Mbox_Present and No (Expr) then
3000 null;
3002 elsif No (Expr) then
3003 Error_Msg_NE ("no value supplied for component &!", N, Component);
3005 else
3006 Resolve_Aggr_Expr (Expr, Component);
3007 end if;
3009 Next_Elmt (Component_Elmt);
3010 end loop;
3012 -- STEP 7: check for invalid components + check type in choice list
3014 Step_7 : declare
3015 Selectr : Node_Id;
3016 -- Selector name
3018 Typech : Entity_Id;
3019 -- Type of first component in choice list
3021 begin
3022 if Present (Component_Associations (N)) then
3023 Assoc := First (Component_Associations (N));
3024 else
3025 Assoc := Empty;
3026 end if;
3028 Verification : while Present (Assoc) loop
3029 Selectr := First (Choices (Assoc));
3030 Typech := Empty;
3032 if Nkind (Selectr) = N_Others_Choice then
3034 -- Ada 2005 (AI-287): others choice may have expression or mbox
3036 if No (Others_Etype)
3037 and then not Others_Mbox
3038 then
3039 Error_Msg_N
3040 ("OTHERS must represent at least one component", Selectr);
3041 end if;
3043 exit Verification;
3044 end if;
3046 while Present (Selectr) loop
3047 New_Assoc := First (New_Assoc_List);
3048 while Present (New_Assoc) loop
3049 Component := First (Choices (New_Assoc));
3050 exit when Chars (Selectr) = Chars (Component);
3051 Next (New_Assoc);
3052 end loop;
3054 -- If no association, this is not a legal component of
3055 -- of the type in question, except if this is an internal
3056 -- component supplied by a previous expansion.
3058 if No (New_Assoc) then
3059 if Box_Present (Parent (Selectr)) then
3060 null;
3062 elsif Chars (Selectr) /= Name_uTag
3063 and then Chars (Selectr) /= Name_uParent
3064 and then Chars (Selectr) /= Name_uController
3065 then
3066 if not Has_Discriminants (Typ) then
3067 Error_Msg_Node_2 := Typ;
3068 Error_Msg_N
3069 ("& is not a component of}",
3070 Selectr);
3071 else
3072 Error_Msg_N
3073 ("& is not a component of the aggregate subtype",
3074 Selectr);
3075 end if;
3077 Check_Misspelled_Component (Components, Selectr);
3078 end if;
3080 elsif No (Typech) then
3081 Typech := Base_Type (Etype (Component));
3083 elsif Typech /= Base_Type (Etype (Component)) then
3084 if not Box_Present (Parent (Selectr)) then
3085 Error_Msg_N
3086 ("components in choice list must have same type",
3087 Selectr);
3088 end if;
3089 end if;
3091 Next (Selectr);
3092 end loop;
3094 Next (Assoc);
3095 end loop Verification;
3096 end Step_7;
3098 -- STEP 8: replace the original aggregate
3100 Step_8 : declare
3101 New_Aggregate : constant Node_Id := New_Copy (N);
3103 begin
3104 Set_Expressions (New_Aggregate, No_List);
3105 Set_Etype (New_Aggregate, Etype (N));
3106 Set_Component_Associations (New_Aggregate, New_Assoc_List);
3108 Rewrite (N, New_Aggregate);
3109 end Step_8;
3110 end Resolve_Record_Aggregate;
3112 -----------------------------
3113 -- Check_Can_Never_Be_Null --
3114 -----------------------------
3116 procedure Check_Can_Never_Be_Null (Typ : Node_Id; Expr : Node_Id) is
3117 Comp_Typ : Entity_Id;
3119 begin
3120 pragma Assert (Ada_Version >= Ada_05
3121 and then Present (Expr)
3122 and then Nkind (Expr) = N_Null);
3124 case Ekind (Typ) is
3125 when E_Array_Type =>
3126 Comp_Typ := Component_Type (Typ);
3128 when E_Component |
3129 E_Discriminant =>
3130 Comp_Typ := Etype (Typ);
3132 when others =>
3133 return;
3134 end case;
3136 if Present (Expr)
3137 and then Can_Never_Be_Null (Comp_Typ)
3138 then
3139 Error_Msg_N
3140 ("(Ada 2005) NULL not allowed in null-excluding components?", Expr);
3141 Error_Msg_NEL
3142 ("\& will be raised at run time!?",
3143 Expr, Standard_Constraint_Error, Sloc (Expr));
3145 Set_Etype (Expr, Comp_Typ);
3146 Set_Analyzed (Expr);
3147 Install_Null_Excluding_Check (Expr);
3148 end if;
3149 end Check_Can_Never_Be_Null;
3151 ---------------------
3152 -- Sort_Case_Table --
3153 ---------------------
3155 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
3156 L : constant Int := Case_Table'First;
3157 U : constant Int := Case_Table'Last;
3158 K : Int;
3159 J : Int;
3160 T : Case_Bounds;
3162 begin
3163 K := L;
3165 while K /= U loop
3166 T := Case_Table (K + 1);
3167 J := K + 1;
3169 while J /= L
3170 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
3171 Expr_Value (T.Choice_Lo)
3172 loop
3173 Case_Table (J) := Case_Table (J - 1);
3174 J := J - 1;
3175 end loop;
3177 Case_Table (J) := T;
3178 K := K + 1;
3179 end loop;
3180 end Sort_Case_Table;
3182 end Sem_Aggr;