* config/rs6000/predicates.md (branch_comparison_operator): Change
[official-gcc.git] / gcc / ada / sem_aggr.adb
blobaa7cddff6a17100da162dbd74e8f05edc4bda746
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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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 (N : 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-231): Generate conversion to the null-excluding
472 -- type to force the corresponding run-time check
474 elsif Is_Access_Type (Check_Typ)
475 and then Can_Never_Be_Null (Check_Typ)
476 and then not Can_Never_Be_Null (Exp_Typ)
477 then
478 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
479 Analyze_And_Resolve (Exp, Check_Typ);
480 Check_Unset_Reference (Exp);
481 end if;
482 end Aggregate_Constraint_Checks;
484 ------------------------
485 -- Array_Aggr_Subtype --
486 ------------------------
488 function Array_Aggr_Subtype
489 (N : Node_Id;
490 Typ : Entity_Id)
491 return Entity_Id
493 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
494 -- Number of aggregate index dimensions.
496 Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
497 -- Constrained N_Range of each index dimension in our aggregate itype.
499 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
500 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
501 -- Low and High bounds for each index dimension in our aggregate itype.
503 Is_Fully_Positional : Boolean := True;
505 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
506 -- N is an array (sub-)aggregate. Dim is the dimension corresponding to
507 -- (sub-)aggregate N. This procedure collects the constrained N_Range
508 -- nodes corresponding to each index dimension of our aggregate itype.
509 -- These N_Range nodes are collected in Aggr_Range above.
510 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
511 -- bounds of each index dimension. If, when collecting, two bounds
512 -- corresponding to the same dimension are static and found to differ,
513 -- then emit a warning, and mark N as raising Constraint_Error.
515 -------------------------
516 -- Collect_Aggr_Bounds --
517 -------------------------
519 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
520 This_Range : constant Node_Id := Aggregate_Bounds (N);
521 -- The aggregate range node of this specific sub-aggregate.
523 This_Low : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
524 This_High : constant Node_Id := High_Bound (Aggregate_Bounds (N));
525 -- The aggregate bounds of this specific sub-aggregate.
527 Assoc : Node_Id;
528 Expr : Node_Id;
530 begin
531 -- Collect the first N_Range for a given dimension that you find.
532 -- For a given dimension they must be all equal anyway.
534 if No (Aggr_Range (Dim)) then
535 Aggr_Low (Dim) := This_Low;
536 Aggr_High (Dim) := This_High;
537 Aggr_Range (Dim) := This_Range;
539 else
540 if Compile_Time_Known_Value (This_Low) then
541 if not Compile_Time_Known_Value (Aggr_Low (Dim)) then
542 Aggr_Low (Dim) := This_Low;
544 elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
545 Set_Raises_Constraint_Error (N);
546 Error_Msg_N ("Sub-aggregate low bound mismatch?", N);
547 Error_Msg_N ("Constraint_Error will be raised at run-time?",
549 end if;
550 end if;
552 if Compile_Time_Known_Value (This_High) then
553 if not Compile_Time_Known_Value (Aggr_High (Dim)) then
554 Aggr_High (Dim) := This_High;
556 elsif
557 Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
558 then
559 Set_Raises_Constraint_Error (N);
560 Error_Msg_N ("Sub-aggregate high bound mismatch?", N);
561 Error_Msg_N ("Constraint_Error will be raised at run-time?",
563 end if;
564 end if;
565 end if;
567 if Dim < Aggr_Dimension then
569 -- Process positional components
571 if Present (Expressions (N)) then
572 Expr := First (Expressions (N));
573 while Present (Expr) loop
574 Collect_Aggr_Bounds (Expr, Dim + 1);
575 Next (Expr);
576 end loop;
577 end if;
579 -- Process component associations
581 if Present (Component_Associations (N)) then
582 Is_Fully_Positional := False;
584 Assoc := First (Component_Associations (N));
585 while Present (Assoc) loop
586 Expr := Expression (Assoc);
587 Collect_Aggr_Bounds (Expr, Dim + 1);
588 Next (Assoc);
589 end loop;
590 end if;
591 end if;
592 end Collect_Aggr_Bounds;
594 -- Array_Aggr_Subtype variables
596 Itype : Entity_Id;
597 -- the final itype of the overall aggregate
599 Index_Constraints : constant List_Id := New_List;
600 -- The list of index constraints of the aggregate itype.
602 -- Start of processing for Array_Aggr_Subtype
604 begin
605 -- Make sure that the list of index constraints is properly attached
606 -- to the tree, and then collect the aggregate bounds.
608 Set_Parent (Index_Constraints, N);
609 Collect_Aggr_Bounds (N, 1);
611 -- Build the list of constrained indices of our aggregate itype.
613 for J in 1 .. Aggr_Dimension loop
614 Create_Index : declare
615 Index_Base : constant Entity_Id :=
616 Base_Type (Etype (Aggr_Range (J)));
617 Index_Typ : Entity_Id;
619 begin
620 -- Construct the Index subtype
622 Index_Typ := Create_Itype (Subtype_Kind (Ekind (Index_Base)), N);
624 Set_Etype (Index_Typ, Index_Base);
626 if Is_Character_Type (Index_Base) then
627 Set_Is_Character_Type (Index_Typ);
628 end if;
630 Set_Size_Info (Index_Typ, (Index_Base));
631 Set_RM_Size (Index_Typ, RM_Size (Index_Base));
632 Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base));
633 Set_Scalar_Range (Index_Typ, Aggr_Range (J));
635 if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then
636 Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ)));
637 end if;
639 Set_Etype (Aggr_Range (J), Index_Typ);
641 Append (Aggr_Range (J), To => Index_Constraints);
642 end Create_Index;
643 end loop;
645 -- Now build the Itype
647 Itype := Create_Itype (E_Array_Subtype, N);
649 Set_First_Rep_Item (Itype, First_Rep_Item (Typ));
650 Set_Convention (Itype, Convention (Typ));
651 Set_Depends_On_Private (Itype, Has_Private_Component (Typ));
652 Set_Etype (Itype, Base_Type (Typ));
653 Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ));
654 Set_Is_Aliased (Itype, Is_Aliased (Typ));
655 Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
657 Copy_Suppress_Status (Index_Check, Typ, Itype);
658 Copy_Suppress_Status (Length_Check, Typ, Itype);
660 Set_First_Index (Itype, First (Index_Constraints));
661 Set_Is_Constrained (Itype, True);
662 Set_Is_Internal (Itype, True);
663 Init_Size_Align (Itype);
665 -- A simple optimization: purely positional aggregates of static
666 -- components should be passed to gigi unexpanded whenever possible,
667 -- and regardless of the staticness of the bounds themselves. Subse-
668 -- quent checks in exp_aggr verify that type is not packed, etc.
670 Set_Size_Known_At_Compile_Time (Itype,
671 Is_Fully_Positional
672 and then Comes_From_Source (N)
673 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
675 -- We always need a freeze node for a packed array subtype, so that
676 -- we can build the Packed_Array_Type corresponding to the subtype.
677 -- If expansion is disabled, the packed array subtype is not built,
678 -- and we must not generate a freeze node for the type, or else it
679 -- will appear incomplete to gigi.
681 if Is_Packed (Itype) and then not In_Default_Expression
682 and then Expander_Active
683 then
684 Freeze_Itype (Itype, N);
685 end if;
687 return Itype;
688 end Array_Aggr_Subtype;
690 --------------------------------
691 -- Check_Misspelled_Component --
692 --------------------------------
694 procedure Check_Misspelled_Component
695 (Elements : Elist_Id;
696 Component : Node_Id)
698 Max_Suggestions : constant := 2;
700 Nr_Of_Suggestions : Natural := 0;
701 Suggestion_1 : Entity_Id := Empty;
702 Suggestion_2 : Entity_Id := Empty;
703 Component_Elmt : Elmt_Id;
705 begin
706 -- All the components of List are matched against Component and
707 -- a count is maintained of possible misspellings. When at the
708 -- end of the analysis there are one or two (not more!) possible
709 -- misspellings, these misspellings will be suggested as
710 -- possible correction.
712 Get_Name_String (Chars (Component));
714 declare
715 S : constant String (1 .. Name_Len) :=
716 Name_Buffer (1 .. Name_Len);
718 begin
720 Component_Elmt := First_Elmt (Elements);
722 while Nr_Of_Suggestions <= Max_Suggestions
723 and then Present (Component_Elmt)
724 loop
726 Get_Name_String (Chars (Node (Component_Elmt)));
728 if Is_Bad_Spelling_Of (Name_Buffer (1 .. Name_Len), S) then
729 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
731 case Nr_Of_Suggestions is
732 when 1 => Suggestion_1 := Node (Component_Elmt);
733 when 2 => Suggestion_2 := Node (Component_Elmt);
734 when others => exit;
735 end case;
736 end if;
738 Next_Elmt (Component_Elmt);
739 end loop;
741 -- Report at most two suggestions
743 if Nr_Of_Suggestions = 1 then
744 Error_Msg_NE ("\possible misspelling of&",
745 Component, Suggestion_1);
747 elsif Nr_Of_Suggestions = 2 then
748 Error_Msg_Node_2 := Suggestion_2;
749 Error_Msg_NE ("\possible misspelling of& or&",
750 Component, Suggestion_1);
751 end if;
752 end;
753 end Check_Misspelled_Component;
755 ----------------------------------------
756 -- Check_Static_Discriminated_Subtype --
757 ----------------------------------------
759 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id) is
760 Disc : constant Entity_Id := First_Discriminant (T);
761 Comp : Entity_Id;
762 Ind : Entity_Id;
764 begin
765 if Has_Record_Rep_Clause (T) then
766 return;
768 elsif Present (Next_Discriminant (Disc)) then
769 return;
771 elsif Nkind (V) /= N_Integer_Literal then
772 return;
773 end if;
775 Comp := First_Component (T);
777 while Present (Comp) loop
779 if Is_Scalar_Type (Etype (Comp)) then
780 null;
782 elsif Is_Private_Type (Etype (Comp))
783 and then Present (Full_View (Etype (Comp)))
784 and then Is_Scalar_Type (Full_View (Etype (Comp)))
785 then
786 null;
788 elsif Is_Array_Type (Etype (Comp)) then
790 if Is_Bit_Packed_Array (Etype (Comp)) then
791 return;
792 end if;
794 Ind := First_Index (Etype (Comp));
796 while Present (Ind) loop
798 if Nkind (Ind) /= N_Range
799 or else Nkind (Low_Bound (Ind)) /= N_Integer_Literal
800 or else Nkind (High_Bound (Ind)) /= N_Integer_Literal
801 then
802 return;
803 end if;
805 Next_Index (Ind);
806 end loop;
808 else
809 return;
810 end if;
812 Next_Component (Comp);
813 end loop;
815 -- On exit, all components have statically known sizes.
817 Set_Size_Known_At_Compile_Time (T);
818 end Check_Static_Discriminated_Subtype;
820 --------------------------------
821 -- Make_String_Into_Aggregate --
822 --------------------------------
824 procedure Make_String_Into_Aggregate (N : Node_Id) is
825 Exprs : constant List_Id := New_List;
826 Loc : constant Source_Ptr := Sloc (N);
827 Str : constant String_Id := Strval (N);
828 Strlen : constant Nat := String_Length (Str);
829 C : Char_Code;
830 C_Node : Node_Id;
831 New_N : Node_Id;
832 P : Source_Ptr;
834 begin
835 P := Loc + 1;
836 for J in 1 .. Strlen loop
837 C := Get_String_Char (Str, J);
838 Set_Character_Literal_Name (C);
840 C_Node :=
841 Make_Character_Literal (P,
842 Chars => Name_Find,
843 Char_Literal_Value => UI_From_CC (C));
844 Set_Etype (C_Node, Any_Character);
845 Append_To (Exprs, C_Node);
847 P := P + 1;
848 -- something special for wide strings ???
849 end loop;
851 New_N := Make_Aggregate (Loc, Expressions => Exprs);
852 Set_Analyzed (New_N);
853 Set_Etype (New_N, Any_Composite);
855 Rewrite (N, New_N);
856 end Make_String_Into_Aggregate;
858 -----------------------
859 -- Resolve_Aggregate --
860 -----------------------
862 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
863 Pkind : constant Node_Kind := Nkind (Parent (N));
865 Aggr_Subtyp : Entity_Id;
866 -- The actual aggregate subtype. This is not necessarily the same as Typ
867 -- which is the subtype of the context in which the aggregate was found.
869 begin
870 -- Check for aggregates not allowed in configurable run-time mode.
871 -- We allow all cases of aggregates that do not come from source,
872 -- since these are all assumed to be small (e.g. bounds of a string
873 -- literal). We also allow aggregates of types we know to be small.
875 if not Support_Aggregates_On_Target
876 and then Comes_From_Source (N)
877 and then (not Known_Static_Esize (Typ) or else Esize (Typ) > 64)
878 then
879 Error_Msg_CRT ("aggregate", N);
880 end if;
882 if Is_Limited_Composite (Typ) then
883 Error_Msg_N ("aggregate type cannot have limited component", N);
884 Explain_Limited_Type (Typ, N);
886 -- Ada 2005 (AI-287): Limited aggregates allowed
888 elsif Is_Limited_Type (Typ)
889 and Ada_Version < Ada_05
890 then
891 Error_Msg_N ("aggregate type cannot be limited", N);
892 Explain_Limited_Type (Typ, N);
894 elsif Is_Class_Wide_Type (Typ) then
895 Error_Msg_N ("type of aggregate cannot be class-wide", N);
897 elsif Typ = Any_String
898 or else Typ = Any_Composite
899 then
900 Error_Msg_N ("no unique type for aggregate", N);
901 Set_Etype (N, Any_Composite);
903 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
904 Error_Msg_N ("null record forbidden in array aggregate", N);
906 elsif Is_Record_Type (Typ) then
907 Resolve_Record_Aggregate (N, Typ);
909 elsif Is_Array_Type (Typ) then
911 -- First a special test, for the case of a positional aggregate
912 -- of characters which can be replaced by a string literal.
913 -- Do not perform this transformation if this was a string literal
914 -- to start with, whose components needed constraint checks, or if
915 -- the component type is non-static, because it will require those
916 -- checks and be transformed back into an aggregate.
918 if Number_Dimensions (Typ) = 1
919 and then
920 (Root_Type (Component_Type (Typ)) = Standard_Character
921 or else
922 Root_Type (Component_Type (Typ)) = Standard_Wide_Character
923 or else
924 Root_Type (Component_Type (Typ)) = Standard_Wide_Wide_Character)
925 and then No (Component_Associations (N))
926 and then not Is_Limited_Composite (Typ)
927 and then not Is_Private_Composite (Typ)
928 and then not Is_Bit_Packed_Array (Typ)
929 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
930 and then Is_Static_Subtype (Component_Type (Typ))
931 then
932 declare
933 Expr : Node_Id;
935 begin
936 Expr := First (Expressions (N));
937 while Present (Expr) loop
938 exit when Nkind (Expr) /= N_Character_Literal;
939 Next (Expr);
940 end loop;
942 if No (Expr) then
943 Start_String;
945 Expr := First (Expressions (N));
946 while Present (Expr) loop
947 Store_String_Char (UI_To_CC (Char_Literal_Value (Expr)));
948 Next (Expr);
949 end loop;
951 Rewrite (N,
952 Make_String_Literal (Sloc (N), End_String));
954 Analyze_And_Resolve (N, Typ);
955 return;
956 end if;
957 end;
958 end if;
960 -- Here if we have a real aggregate to deal with
962 Array_Aggregate : declare
963 Aggr_Resolved : Boolean;
965 Aggr_Typ : constant Entity_Id := Etype (Typ);
966 -- This is the unconstrained array type, which is the type
967 -- against which the aggregate is to be resolved. Typ itself
968 -- is the array type of the context which may not be the same
969 -- subtype as the subtype for the final aggregate.
971 begin
972 -- In the following we determine whether an others choice is
973 -- allowed inside the array aggregate. The test checks the context
974 -- in which the array aggregate occurs. If the context does not
975 -- permit it, or the aggregate type is unconstrained, an others
976 -- choice is not allowed.
978 -- Note that there is no node for Explicit_Actual_Parameter.
979 -- To test for this context we therefore have to test for node
980 -- N_Parameter_Association which itself appears only if there is a
981 -- formal parameter. Consequently we also need to test for
982 -- N_Procedure_Call_Statement or N_Function_Call.
984 Set_Etype (N, Aggr_Typ); -- may be overridden later on
986 -- Ada 2005 (AI-231): Propagate the null_exclusion attribute to
987 -- the components of the array aggregate
989 if Ada_Version >= Ada_05 then
990 Set_Can_Never_Be_Null (Aggr_Typ, Can_Never_Be_Null (Typ));
991 end if;
993 if Is_Constrained (Typ) and then
994 (Pkind = N_Assignment_Statement or else
995 Pkind = N_Parameter_Association or else
996 Pkind = N_Function_Call or else
997 Pkind = N_Procedure_Call_Statement or else
998 Pkind = N_Generic_Association or else
999 Pkind = N_Formal_Object_Declaration or else
1000 Pkind = N_Return_Statement or else
1001 Pkind = N_Object_Declaration or else
1002 Pkind = N_Component_Declaration or else
1003 Pkind = N_Parameter_Specification or else
1004 Pkind = N_Qualified_Expression or else
1005 Pkind = N_Aggregate or else
1006 Pkind = N_Extension_Aggregate or else
1007 Pkind = N_Component_Association)
1008 then
1009 Aggr_Resolved :=
1010 Resolve_Array_Aggregate
1012 Index => First_Index (Aggr_Typ),
1013 Index_Constr => First_Index (Typ),
1014 Component_Typ => Component_Type (Typ),
1015 Others_Allowed => True);
1017 else
1018 Aggr_Resolved :=
1019 Resolve_Array_Aggregate
1021 Index => First_Index (Aggr_Typ),
1022 Index_Constr => First_Index (Aggr_Typ),
1023 Component_Typ => Component_Type (Typ),
1024 Others_Allowed => False);
1025 end if;
1027 if not Aggr_Resolved then
1028 Aggr_Subtyp := Any_Composite;
1029 else
1030 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1031 end if;
1033 Set_Etype (N, Aggr_Subtyp);
1034 end Array_Aggregate;
1036 else
1037 Error_Msg_N ("illegal context for aggregate", N);
1039 end if;
1041 -- If we can determine statically that the evaluation of the
1042 -- aggregate raises Constraint_Error, then replace the
1043 -- aggregate with an N_Raise_Constraint_Error node, but set the
1044 -- Etype to the right aggregate subtype. Gigi needs this.
1046 if Raises_Constraint_Error (N) then
1047 Aggr_Subtyp := Etype (N);
1048 Rewrite (N,
1049 Make_Raise_Constraint_Error (Sloc (N),
1050 Reason => CE_Range_Check_Failed));
1051 Set_Raises_Constraint_Error (N);
1052 Set_Etype (N, Aggr_Subtyp);
1053 Set_Analyzed (N);
1054 end if;
1055 end Resolve_Aggregate;
1057 -----------------------------
1058 -- Resolve_Array_Aggregate --
1059 -----------------------------
1061 function Resolve_Array_Aggregate
1062 (N : Node_Id;
1063 Index : Node_Id;
1064 Index_Constr : Node_Id;
1065 Component_Typ : Entity_Id;
1066 Others_Allowed : Boolean)
1067 return Boolean
1069 Loc : constant Source_Ptr := Sloc (N);
1071 Failure : constant Boolean := False;
1072 Success : constant Boolean := True;
1074 Index_Typ : constant Entity_Id := Etype (Index);
1075 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1076 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1077 -- The type of the index corresponding to the array sub-aggregate
1078 -- along with its low and upper bounds
1080 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1081 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1082 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1083 -- ditto for the base type
1085 function Add (Val : Uint; To : Node_Id) return Node_Id;
1086 -- Creates a new expression node where Val is added to expression To.
1087 -- Tries to constant fold whenever possible. To must be an already
1088 -- analyzed expression.
1090 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1091 -- Checks that AH (the upper bound of an array aggregate) is <= BH
1092 -- (the upper bound of the index base type). If the check fails a
1093 -- warning is emitted, the Raises_Constraint_Error Flag of N is set,
1094 -- and AH is replaced with a duplicate of BH.
1096 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1097 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1098 -- warning if not and sets the Raises_Constraint_Error Flag in N.
1100 procedure Check_Length (L, H : Node_Id; Len : Uint);
1101 -- Checks that range L .. H contains at least Len elements. Emits a
1102 -- warning if not and sets the Raises_Constraint_Error Flag in N.
1104 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1105 -- Returns True if range L .. H is dynamic or null.
1107 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1108 -- Given expression node From, this routine sets OK to False if it
1109 -- cannot statically evaluate From. Otherwise it stores this static
1110 -- value into Value.
1112 function Resolve_Aggr_Expr
1113 (Expr : Node_Id;
1114 Single_Elmt : Boolean)
1115 return Boolean;
1116 -- Resolves aggregate expression Expr. Returs False if resolution
1117 -- fails. If Single_Elmt is set to False, the expression Expr may be
1118 -- used to initialize several array aggregate elements (this can
1119 -- happen for discrete choices such as "L .. H => Expr" or the others
1120 -- choice). In this event we do not resolve Expr unless expansion is
1121 -- disabled. To know why, see the DELAYED COMPONENT RESOLUTION
1122 -- note above.
1124 ---------
1125 -- Add --
1126 ---------
1128 function Add (Val : Uint; To : Node_Id) return Node_Id is
1129 Expr_Pos : Node_Id;
1130 Expr : Node_Id;
1131 To_Pos : Node_Id;
1133 begin
1134 if Raises_Constraint_Error (To) then
1135 return To;
1136 end if;
1138 -- First test if we can do constant folding
1140 if Compile_Time_Known_Value (To)
1141 or else Nkind (To) = N_Integer_Literal
1142 then
1143 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1144 Set_Is_Static_Expression (Expr_Pos);
1145 Set_Etype (Expr_Pos, Etype (To));
1146 Set_Analyzed (Expr_Pos, Analyzed (To));
1148 if not Is_Enumeration_Type (Index_Typ) then
1149 Expr := Expr_Pos;
1151 -- If we are dealing with enumeration return
1152 -- Index_Typ'Val (Expr_Pos)
1154 else
1155 Expr :=
1156 Make_Attribute_Reference
1157 (Loc,
1158 Prefix => New_Reference_To (Index_Typ, Loc),
1159 Attribute_Name => Name_Val,
1160 Expressions => New_List (Expr_Pos));
1161 end if;
1163 return Expr;
1164 end if;
1166 -- If we are here no constant folding possible
1168 if not Is_Enumeration_Type (Index_Base) then
1169 Expr :=
1170 Make_Op_Add (Loc,
1171 Left_Opnd => Duplicate_Subexpr (To),
1172 Right_Opnd => Make_Integer_Literal (Loc, Val));
1174 -- If we are dealing with enumeration return
1175 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1177 else
1178 To_Pos :=
1179 Make_Attribute_Reference
1180 (Loc,
1181 Prefix => New_Reference_To (Index_Typ, Loc),
1182 Attribute_Name => Name_Pos,
1183 Expressions => New_List (Duplicate_Subexpr (To)));
1185 Expr_Pos :=
1186 Make_Op_Add (Loc,
1187 Left_Opnd => To_Pos,
1188 Right_Opnd => Make_Integer_Literal (Loc, Val));
1190 Expr :=
1191 Make_Attribute_Reference
1192 (Loc,
1193 Prefix => New_Reference_To (Index_Typ, Loc),
1194 Attribute_Name => Name_Val,
1195 Expressions => New_List (Expr_Pos));
1196 end if;
1198 return Expr;
1199 end Add;
1201 -----------------
1202 -- Check_Bound --
1203 -----------------
1205 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1206 Val_BH : Uint;
1207 Val_AH : Uint;
1209 OK_BH : Boolean;
1210 OK_AH : Boolean;
1212 begin
1213 Get (Value => Val_BH, From => BH, OK => OK_BH);
1214 Get (Value => Val_AH, From => AH, OK => OK_AH);
1216 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1217 Set_Raises_Constraint_Error (N);
1218 Error_Msg_N ("upper bound out of range?", AH);
1219 Error_Msg_N ("Constraint_Error will be raised at run-time?", AH);
1221 -- You need to set AH to BH or else in the case of enumerations
1222 -- indices we will not be able to resolve the aggregate bounds.
1224 AH := Duplicate_Subexpr (BH);
1225 end if;
1226 end Check_Bound;
1228 ------------------
1229 -- Check_Bounds --
1230 ------------------
1232 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1233 Val_L : Uint;
1234 Val_H : Uint;
1235 Val_AL : Uint;
1236 Val_AH : Uint;
1238 OK_L : Boolean;
1239 OK_H : Boolean;
1240 OK_AL : Boolean;
1241 OK_AH : Boolean;
1243 begin
1244 if Raises_Constraint_Error (N)
1245 or else Dynamic_Or_Null_Range (AL, AH)
1246 then
1247 return;
1248 end if;
1250 Get (Value => Val_L, From => L, OK => OK_L);
1251 Get (Value => Val_H, From => H, OK => OK_H);
1253 Get (Value => Val_AL, From => AL, OK => OK_AL);
1254 Get (Value => Val_AH, From => AH, OK => OK_AH);
1256 if OK_L and then Val_L > Val_AL then
1257 Set_Raises_Constraint_Error (N);
1258 Error_Msg_N ("lower bound of aggregate out of range?", N);
1259 Error_Msg_N ("\Constraint_Error will be raised at run-time?", N);
1260 end if;
1262 if OK_H and then Val_H < Val_AH then
1263 Set_Raises_Constraint_Error (N);
1264 Error_Msg_N ("upper bound of aggregate out of range?", N);
1265 Error_Msg_N ("\Constraint_Error will be raised at run-time?", N);
1266 end if;
1267 end Check_Bounds;
1269 ------------------
1270 -- Check_Length --
1271 ------------------
1273 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1274 Val_L : Uint;
1275 Val_H : Uint;
1277 OK_L : Boolean;
1278 OK_H : Boolean;
1280 Range_Len : Uint;
1282 begin
1283 if Raises_Constraint_Error (N) then
1284 return;
1285 end if;
1287 Get (Value => Val_L, From => L, OK => OK_L);
1288 Get (Value => Val_H, From => H, OK => OK_H);
1290 if not OK_L or else not OK_H then
1291 return;
1292 end if;
1294 -- If null range length is zero
1296 if Val_L > Val_H then
1297 Range_Len := Uint_0;
1298 else
1299 Range_Len := Val_H - Val_L + 1;
1300 end if;
1302 if Range_Len < Len then
1303 Set_Raises_Constraint_Error (N);
1304 Error_Msg_N ("Too many elements?", N);
1305 Error_Msg_N ("Constraint_Error will be raised at run-time?", N);
1306 end if;
1307 end Check_Length;
1309 ---------------------------
1310 -- Dynamic_Or_Null_Range --
1311 ---------------------------
1313 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1314 Val_L : Uint;
1315 Val_H : Uint;
1317 OK_L : Boolean;
1318 OK_H : Boolean;
1320 begin
1321 Get (Value => Val_L, From => L, OK => OK_L);
1322 Get (Value => Val_H, From => H, OK => OK_H);
1324 return not OK_L or else not OK_H
1325 or else not Is_OK_Static_Expression (L)
1326 or else not Is_OK_Static_Expression (H)
1327 or else Val_L > Val_H;
1328 end Dynamic_Or_Null_Range;
1330 ---------
1331 -- Get --
1332 ---------
1334 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1335 begin
1336 OK := True;
1338 if Compile_Time_Known_Value (From) then
1339 Value := Expr_Value (From);
1341 -- If expression From is something like Some_Type'Val (10) then
1342 -- Value = 10
1344 elsif Nkind (From) = N_Attribute_Reference
1345 and then Attribute_Name (From) = Name_Val
1346 and then Compile_Time_Known_Value (First (Expressions (From)))
1347 then
1348 Value := Expr_Value (First (Expressions (From)));
1350 else
1351 Value := Uint_0;
1352 OK := False;
1353 end if;
1354 end Get;
1356 -----------------------
1357 -- Resolve_Aggr_Expr --
1358 -----------------------
1360 function Resolve_Aggr_Expr
1361 (Expr : Node_Id;
1362 Single_Elmt : Boolean)
1363 return Boolean
1365 Nxt_Ind : constant Node_Id := Next_Index (Index);
1366 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
1367 -- Index is the current index corresponding to the expresion.
1369 Resolution_OK : Boolean := True;
1370 -- Set to False if resolution of the expression failed.
1372 begin
1373 -- If the array type against which we are resolving the aggregate
1374 -- has several dimensions, the expressions nested inside the
1375 -- aggregate must be further aggregates (or strings).
1377 if Present (Nxt_Ind) then
1378 if Nkind (Expr) /= N_Aggregate then
1380 -- A string literal can appear where a one-dimensional array
1381 -- of characters is expected. If the literal looks like an
1382 -- operator, it is still an operator symbol, which will be
1383 -- transformed into a string when analyzed.
1385 if Is_Character_Type (Component_Typ)
1386 and then No (Next_Index (Nxt_Ind))
1387 and then (Nkind (Expr) = N_String_Literal
1388 or else Nkind (Expr) = N_Operator_Symbol)
1389 then
1390 -- A string literal used in a multidimensional array
1391 -- aggregate in place of the final one-dimensional
1392 -- aggregate must not be enclosed in parentheses.
1394 if Paren_Count (Expr) /= 0 then
1395 Error_Msg_N ("No parenthesis allowed here", Expr);
1396 end if;
1398 Make_String_Into_Aggregate (Expr);
1400 else
1401 Error_Msg_N ("nested array aggregate expected", Expr);
1402 return Failure;
1403 end if;
1404 end if;
1406 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
1407 -- Required to check the null-exclusion attribute (if present).
1408 -- This value may be overridden later on.
1410 Set_Etype (Expr, Etype (N));
1412 Resolution_OK := Resolve_Array_Aggregate
1413 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1415 -- Do not resolve the expressions of discrete or others choices
1416 -- unless the expression covers a single component, or the expander
1417 -- is inactive.
1419 elsif Single_Elmt
1420 or else not Expander_Active
1421 or else In_Default_Expression
1422 then
1423 Analyze_And_Resolve (Expr, Component_Typ);
1424 Check_Non_Static_Context (Expr);
1425 Aggregate_Constraint_Checks (Expr, Component_Typ);
1426 Check_Unset_Reference (Expr);
1427 end if;
1429 if Raises_Constraint_Error (Expr)
1430 and then Nkind (Parent (Expr)) /= N_Component_Association
1431 then
1432 Set_Raises_Constraint_Error (N);
1433 end if;
1435 return Resolution_OK;
1436 end Resolve_Aggr_Expr;
1438 -- Variables local to Resolve_Array_Aggregate
1440 Assoc : Node_Id;
1441 Choice : Node_Id;
1442 Expr : Node_Id;
1444 Who_Cares : Node_Id;
1446 Aggr_Low : Node_Id := Empty;
1447 Aggr_High : Node_Id := Empty;
1448 -- The actual low and high bounds of this sub-aggegate
1450 Choices_Low : Node_Id := Empty;
1451 Choices_High : Node_Id := Empty;
1452 -- The lowest and highest discrete choices values for a named aggregate
1454 Nb_Elements : Uint := Uint_0;
1455 -- The number of elements in a positional aggegate
1457 Others_Present : Boolean := False;
1459 Nb_Choices : Nat := 0;
1460 -- Contains the overall number of named choices in this sub-aggregate
1462 Nb_Discrete_Choices : Nat := 0;
1463 -- The overall number of discrete choices (not counting others choice)
1465 Case_Table_Size : Nat;
1466 -- Contains the size of the case table needed to sort aggregate choices
1468 -- Start of processing for Resolve_Array_Aggregate
1470 begin
1471 -- STEP 1: make sure the aggregate is correctly formatted
1473 if Present (Component_Associations (N)) then
1474 Assoc := First (Component_Associations (N));
1475 while Present (Assoc) loop
1476 Choice := First (Choices (Assoc));
1477 while Present (Choice) loop
1478 if Nkind (Choice) = N_Others_Choice then
1479 Others_Present := True;
1481 if Choice /= First (Choices (Assoc))
1482 or else Present (Next (Choice))
1483 then
1484 Error_Msg_N
1485 ("OTHERS must appear alone in a choice list", Choice);
1486 return Failure;
1487 end if;
1489 if Present (Next (Assoc)) then
1490 Error_Msg_N
1491 ("OTHERS must appear last in an aggregate", Choice);
1492 return Failure;
1493 end if;
1495 if Ada_Version = Ada_83
1496 and then Assoc /= First (Component_Associations (N))
1497 and then (Nkind (Parent (N)) = N_Assignment_Statement
1498 or else
1499 Nkind (Parent (N)) = N_Object_Declaration)
1500 then
1501 Error_Msg_N
1502 ("(Ada 83) illegal context for OTHERS choice", N);
1503 end if;
1504 end if;
1506 Nb_Choices := Nb_Choices + 1;
1507 Next (Choice);
1508 end loop;
1510 Next (Assoc);
1511 end loop;
1512 end if;
1514 -- At this point we know that the others choice, if present, is by
1515 -- itself and appears last in the aggregate. Check if we have mixed
1516 -- positional and discrete associations (other than the others choice).
1518 if Present (Expressions (N))
1519 and then (Nb_Choices > 1
1520 or else (Nb_Choices = 1 and then not Others_Present))
1521 then
1522 Error_Msg_N
1523 ("named association cannot follow positional association",
1524 First (Choices (First (Component_Associations (N)))));
1525 return Failure;
1526 end if;
1528 -- Test for the validity of an others choice if present
1530 if Others_Present and then not Others_Allowed then
1531 Error_Msg_N
1532 ("OTHERS choice not allowed here",
1533 First (Choices (First (Component_Associations (N)))));
1534 return Failure;
1535 end if;
1537 -- Protect against cascaded errors
1539 if Etype (Index_Typ) = Any_Type then
1540 return Failure;
1541 end if;
1543 -- STEP 2: Process named components
1545 if No (Expressions (N)) then
1547 if Others_Present then
1548 Case_Table_Size := Nb_Choices - 1;
1549 else
1550 Case_Table_Size := Nb_Choices;
1551 end if;
1553 Step_2 : declare
1554 Low : Node_Id;
1555 High : Node_Id;
1556 -- Denote the lowest and highest values in an aggregate choice
1558 Hi_Val : Uint;
1559 Lo_Val : Uint;
1560 -- High end of one range and Low end of the next. Should be
1561 -- contiguous if there is no hole in the list of values.
1563 Missing_Values : Boolean;
1564 -- Set True if missing index values
1566 S_Low : Node_Id := Empty;
1567 S_High : Node_Id := Empty;
1568 -- if a choice in an aggregate is a subtype indication these
1569 -- denote the lowest and highest values of the subtype
1571 Table : Case_Table_Type (1 .. Case_Table_Size);
1572 -- Used to sort all the different choice values
1574 Single_Choice : Boolean;
1575 -- Set to true every time there is a single discrete choice in a
1576 -- discrete association
1578 Prev_Nb_Discrete_Choices : Nat;
1579 -- Used to keep track of the number of discrete choices
1580 -- in the current association.
1582 begin
1583 -- STEP 2 (A): Check discrete choices validity.
1585 Assoc := First (Component_Associations (N));
1586 while Present (Assoc) loop
1588 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
1589 Choice := First (Choices (Assoc));
1590 loop
1591 Analyze (Choice);
1593 if Nkind (Choice) = N_Others_Choice then
1594 Single_Choice := False;
1595 exit;
1597 -- Test for subtype mark without constraint
1599 elsif Is_Entity_Name (Choice) and then
1600 Is_Type (Entity (Choice))
1601 then
1602 if Base_Type (Entity (Choice)) /= Index_Base then
1603 Error_Msg_N
1604 ("invalid subtype mark in aggregate choice",
1605 Choice);
1606 return Failure;
1607 end if;
1609 elsif Nkind (Choice) = N_Subtype_Indication then
1610 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
1612 -- Does the subtype indication evaluation raise CE ?
1614 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
1615 Get_Index_Bounds (Choice, Low, High);
1616 Check_Bounds (S_Low, S_High, Low, High);
1618 else -- Choice is a range or an expression
1619 Resolve (Choice, Index_Base);
1620 Check_Unset_Reference (Choice);
1621 Check_Non_Static_Context (Choice);
1623 -- Do not range check a choice. This check is redundant
1624 -- since this test is already performed when we check
1625 -- that the bounds of the array aggregate are within
1626 -- range.
1628 Set_Do_Range_Check (Choice, False);
1629 end if;
1631 -- If we could not resolve the discrete choice stop here
1633 if Etype (Choice) = Any_Type then
1634 return Failure;
1636 -- If the discrete choice raises CE get its original bounds.
1638 elsif Nkind (Choice) = N_Raise_Constraint_Error then
1639 Set_Raises_Constraint_Error (N);
1640 Get_Index_Bounds (Original_Node (Choice), Low, High);
1642 -- Otherwise get its bounds as usual
1644 else
1645 Get_Index_Bounds (Choice, Low, High);
1646 end if;
1648 if (Dynamic_Or_Null_Range (Low, High)
1649 or else (Nkind (Choice) = N_Subtype_Indication
1650 and then
1651 Dynamic_Or_Null_Range (S_Low, S_High)))
1652 and then Nb_Choices /= 1
1653 then
1654 Error_Msg_N
1655 ("dynamic or empty choice in aggregate " &
1656 "must be the only choice", Choice);
1657 return Failure;
1658 end if;
1660 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
1661 Table (Nb_Discrete_Choices).Choice_Lo := Low;
1662 Table (Nb_Discrete_Choices).Choice_Hi := High;
1664 Next (Choice);
1666 if No (Choice) then
1667 -- Check if we have a single discrete choice and whether
1668 -- this discrete choice specifies a single value.
1670 Single_Choice :=
1671 (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1)
1672 and then (Low = High);
1674 exit;
1675 end if;
1676 end loop;
1678 -- Ada 2005 (AI-231)
1680 if Ada_Version >= Ada_05 then
1681 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
1682 end if;
1684 -- Ada 2005 (AI-287): In case of default initialized component
1685 -- we delay the resolution to the expansion phase
1687 if Box_Present (Assoc) then
1689 -- Ada 2005 (AI-287): In case of default initialization
1690 -- of a component the expander will generate calls to
1691 -- the corresponding initialization subprogram.
1693 null;
1695 elsif not Resolve_Aggr_Expr (Expression (Assoc),
1696 Single_Elmt => Single_Choice)
1697 then
1698 return Failure;
1699 end if;
1701 Next (Assoc);
1702 end loop;
1704 -- If aggregate contains more than one choice then these must be
1705 -- static. Sort them and check that they are contiguous
1707 if Nb_Discrete_Choices > 1 then
1708 Sort_Case_Table (Table);
1709 Missing_Values := False;
1711 Outer : for J in 1 .. Nb_Discrete_Choices - 1 loop
1712 if Expr_Value (Table (J).Choice_Hi) >=
1713 Expr_Value (Table (J + 1).Choice_Lo)
1714 then
1715 Error_Msg_N
1716 ("duplicate choice values in array aggregate",
1717 Table (J).Choice_Hi);
1718 return Failure;
1720 elsif not Others_Present then
1722 Hi_Val := Expr_Value (Table (J).Choice_Hi);
1723 Lo_Val := Expr_Value (Table (J + 1).Choice_Lo);
1725 -- If missing values, output error messages
1727 if Lo_Val - Hi_Val > 1 then
1729 -- Header message if not first missing value
1731 if not Missing_Values then
1732 Error_Msg_N
1733 ("missing index value(s) in array aggregate", N);
1734 Missing_Values := True;
1735 end if;
1737 -- Output values of missing indexes
1739 Lo_Val := Lo_Val - 1;
1740 Hi_Val := Hi_Val + 1;
1742 -- Enumeration type case
1744 if Is_Enumeration_Type (Index_Typ) then
1745 Error_Msg_Name_1 :=
1746 Chars
1747 (Get_Enum_Lit_From_Pos
1748 (Index_Typ, Hi_Val, Loc));
1750 if Lo_Val = Hi_Val then
1751 Error_Msg_N ("\ %", N);
1752 else
1753 Error_Msg_Name_2 :=
1754 Chars
1755 (Get_Enum_Lit_From_Pos
1756 (Index_Typ, Lo_Val, Loc));
1757 Error_Msg_N ("\ % .. %", N);
1758 end if;
1760 -- Integer types case
1762 else
1763 Error_Msg_Uint_1 := Hi_Val;
1765 if Lo_Val = Hi_Val then
1766 Error_Msg_N ("\ ^", N);
1767 else
1768 Error_Msg_Uint_2 := Lo_Val;
1769 Error_Msg_N ("\ ^ .. ^", N);
1770 end if;
1771 end if;
1772 end if;
1773 end if;
1774 end loop Outer;
1776 if Missing_Values then
1777 Set_Etype (N, Any_Composite);
1778 return Failure;
1779 end if;
1780 end if;
1782 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
1784 if Nb_Discrete_Choices > 0 then
1785 Choices_Low := Table (1).Choice_Lo;
1786 Choices_High := Table (Nb_Discrete_Choices).Choice_Hi;
1787 end if;
1789 if Others_Present then
1790 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
1792 else
1793 Aggr_Low := Choices_Low;
1794 Aggr_High := Choices_High;
1795 end if;
1796 end Step_2;
1798 -- STEP 3: Process positional components
1800 else
1801 -- STEP 3 (A): Process positional elements
1803 Expr := First (Expressions (N));
1804 Nb_Elements := Uint_0;
1805 while Present (Expr) loop
1806 Nb_Elements := Nb_Elements + 1;
1808 -- Ada 2005 (AI-231)
1810 if Ada_Version >= Ada_05 then
1811 Check_Can_Never_Be_Null (Etype (N), Expr);
1812 end if;
1814 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
1815 return Failure;
1816 end if;
1818 Next (Expr);
1819 end loop;
1821 if Others_Present then
1822 Assoc := Last (Component_Associations (N));
1824 -- Ada 2005 (AI-231)
1826 if Ada_Version >= Ada_05 then
1827 Check_Can_Never_Be_Null
1828 (Etype (N), Expression (Assoc));
1829 end if;
1831 -- Ada 2005 (AI-287): In case of default initialized component
1832 -- we delay the resolution to the expansion phase.
1834 if Box_Present (Assoc) then
1836 -- Ada 2005 (AI-287): In case of default initialization
1837 -- of a component the expander will generate calls to
1838 -- the corresponding initialization subprogram.
1840 null;
1842 elsif not Resolve_Aggr_Expr (Expression (Assoc),
1843 Single_Elmt => False)
1844 then
1845 return Failure;
1846 end if;
1847 end if;
1849 -- STEP 3 (B): Compute the aggregate bounds
1851 if Others_Present then
1852 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
1854 else
1855 if Others_Allowed then
1856 Get_Index_Bounds (Index_Constr, Aggr_Low, Who_Cares);
1857 else
1858 Aggr_Low := Index_Typ_Low;
1859 end if;
1861 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
1862 Check_Bound (Index_Base_High, Aggr_High);
1863 end if;
1864 end if;
1866 -- STEP 4: Perform static aggregate checks and save the bounds
1868 -- Check (A)
1870 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
1871 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
1873 -- Check (B)
1875 if Others_Present and then Nb_Discrete_Choices > 0 then
1876 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
1877 Check_Bounds (Index_Typ_Low, Index_Typ_High,
1878 Choices_Low, Choices_High);
1879 Check_Bounds (Index_Base_Low, Index_Base_High,
1880 Choices_Low, Choices_High);
1882 -- Check (C)
1884 elsif Others_Present and then Nb_Elements > 0 then
1885 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
1886 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
1887 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
1889 end if;
1891 if Raises_Constraint_Error (Aggr_Low)
1892 or else Raises_Constraint_Error (Aggr_High)
1893 then
1894 Set_Raises_Constraint_Error (N);
1895 end if;
1897 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
1899 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
1900 -- since the addition node returned by Add is not yet analyzed. Attach
1901 -- to tree and analyze first. Reset analyzed flag to insure it will get
1902 -- analyzed when it is a literal bound whose type must be properly
1903 -- set.
1905 if Others_Present or else Nb_Discrete_Choices > 0 then
1906 Aggr_High := Duplicate_Subexpr (Aggr_High);
1908 if Etype (Aggr_High) = Universal_Integer then
1909 Set_Analyzed (Aggr_High, False);
1910 end if;
1911 end if;
1913 Set_Aggregate_Bounds
1914 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
1916 -- The bounds may contain expressions that must be inserted upwards.
1917 -- Attach them fully to the tree. After analysis, remove side effects
1918 -- from upper bound, if still needed.
1920 Set_Parent (Aggregate_Bounds (N), N);
1921 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
1922 Check_Unset_Reference (Aggregate_Bounds (N));
1924 if not Others_Present and then Nb_Discrete_Choices = 0 then
1925 Set_High_Bound (Aggregate_Bounds (N),
1926 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
1927 end if;
1929 return Success;
1930 end Resolve_Array_Aggregate;
1932 ---------------------------------
1933 -- Resolve_Extension_Aggregate --
1934 ---------------------------------
1936 -- There are two cases to consider:
1938 -- a) If the ancestor part is a type mark, the components needed are
1939 -- the difference between the components of the expected type and the
1940 -- components of the given type mark.
1942 -- b) If the ancestor part is an expression, it must be unambiguous,
1943 -- and once we have its type we can also compute the needed components
1944 -- as in the previous case. In both cases, if the ancestor type is not
1945 -- the immediate ancestor, we have to build this ancestor recursively.
1947 -- In both cases discriminants of the ancestor type do not play a
1948 -- role in the resolution of the needed components, because inherited
1949 -- discriminants cannot be used in a type extension. As a result we can
1950 -- compute independently the list of components of the ancestor type and
1951 -- of the expected type.
1953 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
1954 A : constant Node_Id := Ancestor_Part (N);
1955 A_Type : Entity_Id;
1956 I : Interp_Index;
1957 It : Interp;
1959 function Valid_Ancestor_Type return Boolean;
1960 -- Verify that the type of the ancestor part is a non-private ancestor
1961 -- of the expected type.
1963 -------------------------
1964 -- Valid_Ancestor_Type --
1965 -------------------------
1967 function Valid_Ancestor_Type return Boolean is
1968 Imm_Type : Entity_Id;
1970 begin
1971 Imm_Type := Base_Type (Typ);
1972 while Is_Derived_Type (Imm_Type)
1973 and then Etype (Imm_Type) /= Base_Type (A_Type)
1974 loop
1975 Imm_Type := Etype (Base_Type (Imm_Type));
1976 end loop;
1978 if Etype (Imm_Type) /= Base_Type (A_Type) then
1979 Error_Msg_NE ("expect ancestor type of &", A, Typ);
1980 return False;
1981 else
1982 return True;
1983 end if;
1984 end Valid_Ancestor_Type;
1986 -- Start of processing for Resolve_Extension_Aggregate
1988 begin
1989 Analyze (A);
1991 if not Is_Tagged_Type (Typ) then
1992 Error_Msg_N ("type of extension aggregate must be tagged", N);
1993 return;
1995 elsif Is_Limited_Type (Typ) then
1997 -- Ada 2005 (AI-287): Limited aggregates are allowed
1999 if Ada_Version < Ada_05 then
2000 Error_Msg_N ("aggregate type cannot be limited", N);
2001 Explain_Limited_Type (Typ, N);
2002 return;
2003 end if;
2005 elsif Is_Class_Wide_Type (Typ) then
2006 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
2007 return;
2008 end if;
2010 if Is_Entity_Name (A)
2011 and then Is_Type (Entity (A))
2012 then
2013 A_Type := Get_Full_View (Entity (A));
2015 if Valid_Ancestor_Type then
2016 Set_Entity (A, A_Type);
2017 Set_Etype (A, A_Type);
2019 Validate_Ancestor_Part (N);
2020 Resolve_Record_Aggregate (N, Typ);
2021 end if;
2023 elsif Nkind (A) /= N_Aggregate then
2024 if Is_Overloaded (A) then
2025 A_Type := Any_Type;
2026 Get_First_Interp (A, I, It);
2028 while Present (It.Typ) loop
2030 if Is_Tagged_Type (It.Typ)
2031 and then not Is_Limited_Type (It.Typ)
2032 then
2033 if A_Type /= Any_Type then
2034 Error_Msg_N ("cannot resolve expression", A);
2035 return;
2036 else
2037 A_Type := It.Typ;
2038 end if;
2039 end if;
2041 Get_Next_Interp (I, It);
2042 end loop;
2044 if A_Type = Any_Type then
2045 Error_Msg_N
2046 ("ancestor part must be non-limited tagged type", A);
2047 return;
2048 end if;
2050 else
2051 A_Type := Etype (A);
2052 end if;
2054 if Valid_Ancestor_Type then
2055 Resolve (A, A_Type);
2056 Check_Unset_Reference (A);
2057 Check_Non_Static_Context (A);
2059 if Is_Class_Wide_Type (Etype (A))
2060 and then Nkind (Original_Node (A)) = N_Function_Call
2061 then
2062 -- If the ancestor part is a dispatching call, it appears
2063 -- statically to be a legal ancestor, but it yields any
2064 -- member of the class, and it is not possible to determine
2065 -- whether it is an ancestor of the extension aggregate (much
2066 -- less which ancestor). It is not possible to determine the
2067 -- required components of the extension part.
2069 -- This check implements AI-306, which in fact was motivated
2070 -- by an ACT query to the ARG after this test was added.
2072 Error_Msg_N ("ancestor part must be statically tagged", A);
2073 else
2074 Resolve_Record_Aggregate (N, Typ);
2075 end if;
2076 end if;
2078 else
2079 Error_Msg_N (" No unique type for this aggregate", A);
2080 end if;
2081 end Resolve_Extension_Aggregate;
2083 ------------------------------
2084 -- Resolve_Record_Aggregate --
2085 ------------------------------
2087 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
2088 New_Assoc_List : constant List_Id := New_List;
2089 New_Assoc : Node_Id;
2090 -- New_Assoc_List is the newly built list of N_Component_Association
2091 -- nodes. New_Assoc is one such N_Component_Association node in it.
2092 -- Please note that while Assoc and New_Assoc contain the same
2093 -- kind of nodes, they are used to iterate over two different
2094 -- N_Component_Association lists.
2096 Others_Etype : Entity_Id := Empty;
2097 -- This variable is used to save the Etype of the last record component
2098 -- that takes its value from the others choice. Its purpose is:
2100 -- (a) make sure the others choice is useful
2102 -- (b) make sure the type of all the components whose value is
2103 -- subsumed by the others choice are the same.
2105 -- This variable is updated as a side effect of function Get_Value
2107 Mbox_Present : Boolean := False;
2108 Others_Mbox : Boolean := False;
2109 -- Ada 2005 (AI-287): Variables used in case of default initialization
2110 -- to provide a functionality similar to Others_Etype. Mbox_Present
2111 -- indicates that the component takes its default initialization;
2112 -- Others_Mbox indicates that at least one component takes its default
2113 -- initialization. Similar to Others_Etype, they are also updated as a
2114 -- side effect of function Get_Value.
2116 procedure Add_Association
2117 (Component : Entity_Id;
2118 Expr : Node_Id;
2119 Box_Present : Boolean := False);
2120 -- Builds a new N_Component_Association node which associates
2121 -- Component to expression Expr and adds it to the new association
2122 -- list New_Assoc_List being built.
2124 function Discr_Present (Discr : Entity_Id) return Boolean;
2125 -- If aggregate N is a regular aggregate this routine will return True.
2126 -- Otherwise, if N is an extension aggregate, Discr is a discriminant
2127 -- whose value may already have been specified by N's ancestor part,
2128 -- this routine checks whether this is indeed the case and if so
2129 -- returns False, signaling that no value for Discr should appear in the
2130 -- N's aggregate part. Also, in this case, the routine appends to
2131 -- New_Assoc_List Discr the discriminant value specified in the ancestor
2132 -- part.
2134 function Get_Value
2135 (Compon : Node_Id;
2136 From : List_Id;
2137 Consider_Others_Choice : Boolean := False)
2138 return Node_Id;
2139 -- Given a record component stored in parameter Compon, the
2140 -- following function returns its value as it appears in the list
2141 -- From, which is a list of N_Component_Association nodes. If no
2142 -- component association has a choice for the searched component,
2143 -- the value provided by the others choice is returned, if there
2144 -- is one and Consider_Others_Choice is set to true. Otherwise
2145 -- Empty is returned. If there is more than one component association
2146 -- giving a value for the searched record component, an error message
2147 -- is emitted and the first found value is returned.
2149 -- If Consider_Others_Choice is set and the returned expression comes
2150 -- from the others choice, then Others_Etype is set as a side effect.
2151 -- An error message is emitted if the components taking their value
2152 -- from the others choice do not have same type.
2154 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id);
2155 -- Analyzes and resolves expression Expr against the Etype of the
2156 -- Component. This routine also applies all appropriate checks to Expr.
2157 -- It finally saves a Expr in the newly created association list that
2158 -- will be attached to the final record aggregate. Note that if the
2159 -- Parent pointer of Expr is not set then Expr was produced with a
2160 -- New_Copy_Tree or some such.
2162 ---------------------
2163 -- Add_Association --
2164 ---------------------
2166 procedure Add_Association
2167 (Component : Entity_Id;
2168 Expr : Node_Id;
2169 Box_Present : Boolean := False)
2171 Choice_List : constant List_Id := New_List;
2172 New_Assoc : Node_Id;
2174 begin
2175 Append (New_Occurrence_Of (Component, Sloc (Expr)), Choice_List);
2176 New_Assoc :=
2177 Make_Component_Association (Sloc (Expr),
2178 Choices => Choice_List,
2179 Expression => Expr,
2180 Box_Present => Box_Present);
2181 Append (New_Assoc, New_Assoc_List);
2182 end Add_Association;
2184 -------------------
2185 -- Discr_Present --
2186 -------------------
2188 function Discr_Present (Discr : Entity_Id) return Boolean is
2189 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
2191 Loc : Source_Ptr;
2193 Ancestor : Node_Id;
2194 Discr_Expr : Node_Id;
2196 Ancestor_Typ : Entity_Id;
2197 Orig_Discr : Entity_Id;
2198 D : Entity_Id;
2199 D_Val : Elmt_Id := No_Elmt; -- stop junk warning
2201 Ancestor_Is_Subtyp : Boolean;
2203 begin
2204 if Regular_Aggr then
2205 return True;
2206 end if;
2208 Ancestor := Ancestor_Part (N);
2209 Ancestor_Typ := Etype (Ancestor);
2210 Loc := Sloc (Ancestor);
2212 Ancestor_Is_Subtyp :=
2213 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
2215 -- If the ancestor part has no discriminants clearly N's aggregate
2216 -- part must provide a value for Discr.
2218 if not Has_Discriminants (Ancestor_Typ) then
2219 return True;
2221 -- If the ancestor part is an unconstrained subtype mark then the
2222 -- Discr must be present in N's aggregate part.
2224 elsif Ancestor_Is_Subtyp
2225 and then not Is_Constrained (Entity (Ancestor))
2226 then
2227 return True;
2228 end if;
2230 -- Now look to see if Discr was specified in the ancestor part.
2232 Orig_Discr := Original_Record_Component (Discr);
2233 D := First_Discriminant (Ancestor_Typ);
2235 if Ancestor_Is_Subtyp then
2236 D_Val := First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
2237 end if;
2239 while Present (D) loop
2240 -- If Ancestor has already specified Disc value than
2241 -- insert its value in the final aggregate.
2243 if Original_Record_Component (D) = Orig_Discr then
2244 if Ancestor_Is_Subtyp then
2245 Discr_Expr := New_Copy_Tree (Node (D_Val));
2246 else
2247 Discr_Expr :=
2248 Make_Selected_Component (Loc,
2249 Prefix => Duplicate_Subexpr (Ancestor),
2250 Selector_Name => New_Occurrence_Of (Discr, Loc));
2251 end if;
2253 Resolve_Aggr_Expr (Discr_Expr, Discr);
2254 return False;
2255 end if;
2257 Next_Discriminant (D);
2259 if Ancestor_Is_Subtyp then
2260 Next_Elmt (D_Val);
2261 end if;
2262 end loop;
2264 return True;
2265 end Discr_Present;
2267 ---------------
2268 -- Get_Value --
2269 ---------------
2271 function Get_Value
2272 (Compon : Node_Id;
2273 From : List_Id;
2274 Consider_Others_Choice : Boolean := False)
2275 return Node_Id
2277 Assoc : Node_Id;
2278 Expr : Node_Id := Empty;
2279 Selector_Name : Node_Id;
2281 procedure Check_Non_Limited_Type;
2282 -- Relax check to allow the default initialization of limited types.
2283 -- For example:
2284 -- record
2285 -- C : Lim := (..., others => <>);
2286 -- end record;
2288 ----------------------------
2289 -- Check_Non_Limited_Type --
2290 ----------------------------
2292 procedure Check_Non_Limited_Type is
2293 begin
2294 if Is_Limited_Type (Etype (Compon))
2295 and then Comes_From_Source (Compon)
2296 and then not In_Instance_Body
2297 then
2298 -- Ada 2005 (AI-287): Limited aggregates are allowed
2300 if Ada_Version >= Ada_05
2301 and then Present (Expression (Assoc))
2302 and then Nkind (Expression (Assoc)) = N_Aggregate
2303 then
2304 null;
2305 else
2306 Error_Msg_N
2307 ("initialization not allowed for limited types", N);
2308 Explain_Limited_Type (Etype (Compon), Compon);
2309 end if;
2311 end if;
2312 end Check_Non_Limited_Type;
2314 -- Start of processing for Get_Value
2316 begin
2317 Mbox_Present := False;
2319 if Present (From) then
2320 Assoc := First (From);
2321 else
2322 return Empty;
2323 end if;
2325 while Present (Assoc) loop
2326 Selector_Name := First (Choices (Assoc));
2327 while Present (Selector_Name) loop
2328 if Nkind (Selector_Name) = N_Others_Choice then
2329 if Consider_Others_Choice and then No (Expr) then
2331 -- We need to duplicate the expression for each
2332 -- successive component covered by the others choice.
2333 -- This is redundant if the others_choice covers only
2334 -- one component (small optimization possible???), but
2335 -- indispensable otherwise, because each one must be
2336 -- expanded individually to preserve side-effects.
2338 -- Ada 2005 (AI-287): In case of default initialization
2339 -- of components, we duplicate the corresponding default
2340 -- expression (from the record type declaration).
2342 if Box_Present (Assoc) then
2343 Others_Mbox := True;
2344 Mbox_Present := True;
2346 if Expander_Active then
2347 return New_Copy_Tree (Expression (Parent (Compon)));
2348 else
2349 return Expression (Parent (Compon));
2350 end if;
2352 else
2353 Check_Non_Limited_Type;
2355 if Present (Others_Etype) and then
2356 Base_Type (Others_Etype) /= Base_Type (Etype
2357 (Compon))
2358 then
2359 Error_Msg_N ("components in OTHERS choice must " &
2360 "have same type", Selector_Name);
2361 end if;
2363 Others_Etype := Etype (Compon);
2365 if Expander_Active then
2366 return New_Copy_Tree (Expression (Assoc));
2367 else
2368 return Expression (Assoc);
2369 end if;
2370 end if;
2371 end if;
2373 elsif Chars (Compon) = Chars (Selector_Name) then
2374 if No (Expr) then
2376 -- Ada 2005 (AI-231)
2378 if Ada_Version >= Ada_05
2379 and then Nkind (Expression (Assoc)) = N_Null
2380 then
2381 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
2382 end if;
2384 -- We need to duplicate the expression when several
2385 -- components are grouped together with a "|" choice.
2386 -- For instance "filed1 | filed2 => Expr"
2388 -- Ada 2005 (AI-287)
2390 if Box_Present (Assoc) then
2391 Mbox_Present := True;
2393 -- Duplicate the default expression of the component
2394 -- from the record type declaration
2396 if Present (Next (Selector_Name)) then
2397 Expr :=
2398 New_Copy_Tree (Expression (Parent (Compon)));
2399 else
2400 Expr := Expression (Parent (Compon));
2401 end if;
2403 else
2404 Check_Non_Limited_Type;
2406 if Present (Next (Selector_Name)) then
2407 Expr := New_Copy_Tree (Expression (Assoc));
2408 else
2409 Expr := Expression (Assoc);
2410 end if;
2411 end if;
2413 Generate_Reference (Compon, Selector_Name);
2415 else
2416 Error_Msg_NE
2417 ("more than one value supplied for &",
2418 Selector_Name, Compon);
2420 end if;
2421 end if;
2423 Next (Selector_Name);
2424 end loop;
2426 Next (Assoc);
2427 end loop;
2429 return Expr;
2430 end Get_Value;
2432 -----------------------
2433 -- Resolve_Aggr_Expr --
2434 -----------------------
2436 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id) is
2437 New_C : Entity_Id := Component;
2438 Expr_Type : Entity_Id := Empty;
2440 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
2441 -- If the expression is an aggregate (possibly qualified) then its
2442 -- expansion is delayed until the enclosing aggregate is expanded
2443 -- into assignments. In that case, do not generate checks on the
2444 -- expression, because they will be generated later, and will other-
2445 -- wise force a copy (to remove side-effects) that would leave a
2446 -- dynamic-sized aggregate in the code, something that gigi cannot
2447 -- handle.
2449 Relocate : Boolean;
2450 -- Set to True if the resolved Expr node needs to be relocated
2451 -- when attached to the newly created association list. This node
2452 -- need not be relocated if its parent pointer is not set.
2453 -- In fact in this case Expr is the output of a New_Copy_Tree call.
2454 -- if Relocate is True then we have analyzed the expression node
2455 -- in the original aggregate and hence it needs to be relocated
2456 -- when moved over the new association list.
2458 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
2459 Kind : constant Node_Kind := Nkind (Expr);
2461 begin
2462 return ((Kind = N_Aggregate
2463 or else Kind = N_Extension_Aggregate)
2464 and then Present (Etype (Expr))
2465 and then Is_Record_Type (Etype (Expr))
2466 and then Expansion_Delayed (Expr))
2468 or else (Kind = N_Qualified_Expression
2469 and then Has_Expansion_Delayed (Expression (Expr)));
2470 end Has_Expansion_Delayed;
2472 -- Start of processing for Resolve_Aggr_Expr
2474 begin
2475 -- If the type of the component is elementary or the type of the
2476 -- aggregate does not contain discriminants, use the type of the
2477 -- component to resolve Expr.
2479 if Is_Elementary_Type (Etype (Component))
2480 or else not Has_Discriminants (Etype (N))
2481 then
2482 Expr_Type := Etype (Component);
2484 -- Otherwise we have to pick up the new type of the component from
2485 -- the new costrained subtype of the aggregate. In fact components
2486 -- which are of a composite type might be constrained by a
2487 -- discriminant, and we want to resolve Expr against the subtype were
2488 -- all discriminant occurrences are replaced with their actual value.
2490 else
2491 New_C := First_Component (Etype (N));
2492 while Present (New_C) loop
2493 if Chars (New_C) = Chars (Component) then
2494 Expr_Type := Etype (New_C);
2495 exit;
2496 end if;
2498 Next_Component (New_C);
2499 end loop;
2501 pragma Assert (Present (Expr_Type));
2503 -- For each range in an array type where a discriminant has been
2504 -- replaced with the constraint, check that this range is within
2505 -- the range of the base type. This checks is done in the
2506 -- init proc for regular objects, but has to be done here for
2507 -- aggregates since no init proc is called for them.
2509 if Is_Array_Type (Expr_Type) then
2510 declare
2511 Index : Node_Id := First_Index (Expr_Type);
2512 -- Range of the current constrained index in the array.
2514 Orig_Index : Node_Id := First_Index (Etype (Component));
2515 -- Range corresponding to the range Index above in the
2516 -- original unconstrained record type. The bounds of this
2517 -- range may be governed by discriminants.
2519 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
2520 -- Range corresponding to the range Index above for the
2521 -- unconstrained array type. This range is needed to apply
2522 -- range checks.
2524 begin
2525 while Present (Index) loop
2526 if Depends_On_Discriminant (Orig_Index) then
2527 Apply_Range_Check (Index, Etype (Unconstr_Index));
2528 end if;
2530 Next_Index (Index);
2531 Next_Index (Orig_Index);
2532 Next_Index (Unconstr_Index);
2533 end loop;
2534 end;
2535 end if;
2536 end if;
2538 -- If the Parent pointer of Expr is not set, Expr is an expression
2539 -- duplicated by New_Tree_Copy (this happens for record aggregates
2540 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
2541 -- Such a duplicated expression must be attached to the tree
2542 -- before analysis and resolution to enforce the rule that a tree
2543 -- fragment should never be analyzed or resolved unless it is
2544 -- attached to the current compilation unit.
2546 if No (Parent (Expr)) then
2547 Set_Parent (Expr, N);
2548 Relocate := False;
2549 else
2550 Relocate := True;
2551 end if;
2553 Analyze_And_Resolve (Expr, Expr_Type);
2554 Check_Non_Static_Context (Expr);
2555 Check_Unset_Reference (Expr);
2557 if not Has_Expansion_Delayed (Expr) then
2558 Aggregate_Constraint_Checks (Expr, Expr_Type);
2559 end if;
2561 if Raises_Constraint_Error (Expr) then
2562 Set_Raises_Constraint_Error (N);
2563 end if;
2565 if Relocate then
2566 Add_Association (New_C, Relocate_Node (Expr));
2567 else
2568 Add_Association (New_C, Expr);
2569 end if;
2570 end Resolve_Aggr_Expr;
2572 -- Resolve_Record_Aggregate local variables
2574 Assoc : Node_Id;
2575 -- N_Component_Association node belonging to the input aggregate N
2577 Expr : Node_Id;
2578 Positional_Expr : Node_Id;
2579 Component : Entity_Id;
2580 Component_Elmt : Elmt_Id;
2582 Components : constant Elist_Id := New_Elmt_List;
2583 -- Components is the list of the record components whose value must
2584 -- be provided in the aggregate. This list does include discriminants.
2586 -- Start of processing for Resolve_Record_Aggregate
2588 begin
2589 -- We may end up calling Duplicate_Subexpr on expressions that are
2590 -- attached to New_Assoc_List. For this reason we need to attach it
2591 -- to the tree by setting its parent pointer to N. This parent point
2592 -- will change in STEP 8 below.
2594 Set_Parent (New_Assoc_List, N);
2596 -- STEP 1: abstract type and null record verification
2598 if Is_Abstract (Typ) then
2599 Error_Msg_N ("type of aggregate cannot be abstract", N);
2600 end if;
2602 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
2603 Set_Etype (N, Typ);
2604 return;
2606 elsif Present (First_Entity (Typ))
2607 and then Null_Record_Present (N)
2608 and then not Is_Tagged_Type (Typ)
2609 then
2610 Error_Msg_N ("record aggregate cannot be null", N);
2611 return;
2613 elsif No (First_Entity (Typ)) then
2614 Error_Msg_N ("record aggregate must be null", N);
2615 return;
2616 end if;
2618 -- STEP 2: Verify aggregate structure
2620 Step_2 : declare
2621 Selector_Name : Node_Id;
2622 Bad_Aggregate : Boolean := False;
2624 begin
2625 if Present (Component_Associations (N)) then
2626 Assoc := First (Component_Associations (N));
2627 else
2628 Assoc := Empty;
2629 end if;
2631 while Present (Assoc) loop
2632 Selector_Name := First (Choices (Assoc));
2633 while Present (Selector_Name) loop
2634 if Nkind (Selector_Name) = N_Identifier then
2635 null;
2637 elsif Nkind (Selector_Name) = N_Others_Choice then
2638 if Selector_Name /= First (Choices (Assoc))
2639 or else Present (Next (Selector_Name))
2640 then
2641 Error_Msg_N ("OTHERS must appear alone in a choice list",
2642 Selector_Name);
2643 return;
2645 elsif Present (Next (Assoc)) then
2646 Error_Msg_N ("OTHERS must appear last in an aggregate",
2647 Selector_Name);
2648 return;
2649 end if;
2651 else
2652 Error_Msg_N
2653 ("selector name should be identifier or OTHERS",
2654 Selector_Name);
2655 Bad_Aggregate := True;
2656 end if;
2658 Next (Selector_Name);
2659 end loop;
2661 Next (Assoc);
2662 end loop;
2664 if Bad_Aggregate then
2665 return;
2666 end if;
2667 end Step_2;
2669 -- STEP 3: Find discriminant Values
2671 Step_3 : declare
2672 Discrim : Entity_Id;
2673 Missing_Discriminants : Boolean := False;
2675 begin
2676 if Present (Expressions (N)) then
2677 Positional_Expr := First (Expressions (N));
2678 else
2679 Positional_Expr := Empty;
2680 end if;
2682 if Has_Discriminants (Typ) then
2683 Discrim := First_Discriminant (Typ);
2684 else
2685 Discrim := Empty;
2686 end if;
2688 -- First find the discriminant values in the positional components
2690 while Present (Discrim) and then Present (Positional_Expr) loop
2691 if Discr_Present (Discrim) then
2692 Resolve_Aggr_Expr (Positional_Expr, Discrim);
2694 -- Ada 2005 (AI-231)
2696 if Ada_Version >= Ada_05 then
2697 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
2698 end if;
2700 Next (Positional_Expr);
2701 end if;
2703 if Present (Get_Value (Discrim, Component_Associations (N))) then
2704 Error_Msg_NE
2705 ("more than one value supplied for discriminant&",
2706 N, Discrim);
2707 end if;
2709 Next_Discriminant (Discrim);
2710 end loop;
2712 -- Find remaining discriminant values, if any, among named components
2714 while Present (Discrim) loop
2715 Expr := Get_Value (Discrim, Component_Associations (N), True);
2717 if not Discr_Present (Discrim) then
2718 if Present (Expr) then
2719 Error_Msg_NE
2720 ("more than one value supplied for discriminant&",
2721 N, Discrim);
2722 end if;
2724 elsif No (Expr) then
2725 Error_Msg_NE
2726 ("no value supplied for discriminant &", N, Discrim);
2727 Missing_Discriminants := True;
2729 else
2730 Resolve_Aggr_Expr (Expr, Discrim);
2731 end if;
2733 Next_Discriminant (Discrim);
2734 end loop;
2736 if Missing_Discriminants then
2737 return;
2738 end if;
2740 -- At this point and until the beginning of STEP 6, New_Assoc_List
2741 -- contains only the discriminants and their values.
2743 end Step_3;
2745 -- STEP 4: Set the Etype of the record aggregate
2747 -- ??? This code is pretty much a copy of Sem_Ch3.Build_Subtype. That
2748 -- routine should really be exported in sem_util or some such and used
2749 -- in sem_ch3 and here rather than have a copy of the code which is a
2750 -- maintenance nightmare.
2752 -- ??? Performace WARNING. The current implementation creates a new
2753 -- itype for all aggregates whose base type is discriminated.
2754 -- This means that for record aggregates nested inside an array
2755 -- aggregate we will create a new itype for each record aggregate
2756 -- if the array cmponent type has discriminants. For large aggregates
2757 -- this may be a problem. What should be done in this case is
2758 -- to reuse itypes as much as possible.
2760 if Has_Discriminants (Typ) then
2761 Build_Constrained_Itype : declare
2762 Loc : constant Source_Ptr := Sloc (N);
2763 Indic : Node_Id;
2764 Subtyp_Decl : Node_Id;
2765 Def_Id : Entity_Id;
2767 C : constant List_Id := New_List;
2769 begin
2770 New_Assoc := First (New_Assoc_List);
2771 while Present (New_Assoc) loop
2772 Append (Duplicate_Subexpr (Expression (New_Assoc)), To => C);
2773 Next (New_Assoc);
2774 end loop;
2776 Indic :=
2777 Make_Subtype_Indication (Loc,
2778 Subtype_Mark => New_Occurrence_Of (Base_Type (Typ), Loc),
2779 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
2781 Def_Id := Create_Itype (Ekind (Typ), N);
2783 Subtyp_Decl :=
2784 Make_Subtype_Declaration (Loc,
2785 Defining_Identifier => Def_Id,
2786 Subtype_Indication => Indic);
2787 Set_Parent (Subtyp_Decl, Parent (N));
2789 -- Itypes must be analyzed with checks off (see itypes.ads).
2791 Analyze (Subtyp_Decl, Suppress => All_Checks);
2793 Set_Etype (N, Def_Id);
2794 Check_Static_Discriminated_Subtype
2795 (Def_Id, Expression (First (New_Assoc_List)));
2796 end Build_Constrained_Itype;
2798 else
2799 Set_Etype (N, Typ);
2800 end if;
2802 -- STEP 5: Get remaining components according to discriminant values
2804 Step_5 : declare
2805 Record_Def : Node_Id;
2806 Parent_Typ : Entity_Id;
2807 Root_Typ : Entity_Id;
2808 Parent_Typ_List : Elist_Id;
2809 Parent_Elmt : Elmt_Id;
2810 Errors_Found : Boolean := False;
2811 Dnode : Node_Id;
2813 begin
2814 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
2815 Parent_Typ_List := New_Elmt_List;
2817 -- If this is an extension aggregate, the component list must
2818 -- include all components that are not in the given ancestor
2819 -- type. Otherwise, the component list must include components
2820 -- of all ancestors, starting with the root.
2822 if Nkind (N) = N_Extension_Aggregate then
2823 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
2824 else
2825 Root_Typ := Root_Type (Typ);
2827 if Nkind (Parent (Base_Type (Root_Typ)))
2828 = N_Private_Type_Declaration
2829 then
2830 Error_Msg_NE
2831 ("type of aggregate has private ancestor&!",
2832 N, Root_Typ);
2833 Error_Msg_N ("must use extension aggregate!", N);
2834 return;
2835 end if;
2837 Dnode := Declaration_Node (Base_Type (Root_Typ));
2839 -- If we don't get a full declaration, then we have some
2840 -- error which will get signalled later so skip this part.
2841 -- Otherwise, gather components of root that apply to the
2842 -- aggregate type. We use the base type in case there is an
2843 -- applicable stored constraint that renames the discriminants
2844 -- of the root.
2846 if Nkind (Dnode) = N_Full_Type_Declaration then
2847 Record_Def := Type_Definition (Dnode);
2848 Gather_Components (Base_Type (Typ),
2849 Component_List (Record_Def),
2850 Governed_By => New_Assoc_List,
2851 Into => Components,
2852 Report_Errors => Errors_Found);
2853 end if;
2854 end if;
2856 Parent_Typ := Base_Type (Typ);
2857 while Parent_Typ /= Root_Typ loop
2859 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
2860 Parent_Typ := Etype (Parent_Typ);
2862 if Nkind (Parent (Base_Type (Parent_Typ))) =
2863 N_Private_Type_Declaration
2864 or else Nkind (Parent (Base_Type (Parent_Typ))) =
2865 N_Private_Extension_Declaration
2866 then
2867 if Nkind (N) /= N_Extension_Aggregate then
2868 Error_Msg_NE
2869 ("type of aggregate has private ancestor&!",
2870 N, Parent_Typ);
2871 Error_Msg_N ("must use extension aggregate!", N);
2872 return;
2874 elsif Parent_Typ /= Root_Typ then
2875 Error_Msg_NE
2876 ("ancestor part of aggregate must be private type&",
2877 Ancestor_Part (N), Parent_Typ);
2878 return;
2879 end if;
2880 end if;
2881 end loop;
2883 -- Now collect components from all other ancestors.
2885 Parent_Elmt := First_Elmt (Parent_Typ_List);
2886 while Present (Parent_Elmt) loop
2887 Parent_Typ := Node (Parent_Elmt);
2888 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
2889 Gather_Components (Empty,
2890 Component_List (Record_Extension_Part (Record_Def)),
2891 Governed_By => New_Assoc_List,
2892 Into => Components,
2893 Report_Errors => Errors_Found);
2895 Next_Elmt (Parent_Elmt);
2896 end loop;
2898 else
2899 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
2901 if Null_Present (Record_Def) then
2902 null;
2903 else
2904 Gather_Components (Base_Type (Typ),
2905 Component_List (Record_Def),
2906 Governed_By => New_Assoc_List,
2907 Into => Components,
2908 Report_Errors => Errors_Found);
2909 end if;
2910 end if;
2912 if Errors_Found then
2913 return;
2914 end if;
2915 end Step_5;
2917 -- STEP 6: Find component Values
2919 Component := Empty;
2920 Component_Elmt := First_Elmt (Components);
2922 -- First scan the remaining positional associations in the aggregate.
2923 -- Remember that at this point Positional_Expr contains the current
2924 -- positional association if any is left after looking for discriminant
2925 -- values in step 3.
2927 while Present (Positional_Expr) and then Present (Component_Elmt) loop
2928 Component := Node (Component_Elmt);
2929 Resolve_Aggr_Expr (Positional_Expr, Component);
2931 -- Ada 2005 (AI-231)
2933 if Ada_Version >= Ada_05 then
2934 Check_Can_Never_Be_Null (Component, Positional_Expr);
2935 end if;
2937 if Present (Get_Value (Component, Component_Associations (N))) then
2938 Error_Msg_NE
2939 ("more than one value supplied for Component &", N, Component);
2940 end if;
2942 Next (Positional_Expr);
2943 Next_Elmt (Component_Elmt);
2944 end loop;
2946 if Present (Positional_Expr) then
2947 Error_Msg_N
2948 ("too many components for record aggregate", Positional_Expr);
2949 end if;
2951 -- Now scan for the named arguments of the aggregate
2953 while Present (Component_Elmt) loop
2954 Component := Node (Component_Elmt);
2955 Expr := Get_Value (Component, Component_Associations (N), True);
2957 -- Ada 2005 (AI-287): Default initialized limited component are
2958 -- passed to the expander, that will generate calls to the
2959 -- corresponding IP.
2961 if Mbox_Present and then Is_Limited_Type (Etype (Component)) then
2962 Add_Association
2963 (Component => Component,
2964 Expr => Empty,
2965 Box_Present => True);
2967 -- Ada 2005 (AI-287): No value supplied for component
2969 elsif Mbox_Present and No (Expr) then
2970 null;
2972 elsif No (Expr) then
2973 Error_Msg_NE ("no value supplied for component &!", N, Component);
2975 else
2976 Resolve_Aggr_Expr (Expr, Component);
2977 end if;
2979 Next_Elmt (Component_Elmt);
2980 end loop;
2982 -- STEP 7: check for invalid components + check type in choice list
2984 Step_7 : declare
2985 Selectr : Node_Id;
2986 -- Selector name
2988 Typech : Entity_Id;
2989 -- Type of first component in choice list
2991 begin
2992 if Present (Component_Associations (N)) then
2993 Assoc := First (Component_Associations (N));
2994 else
2995 Assoc := Empty;
2996 end if;
2998 Verification : while Present (Assoc) loop
2999 Selectr := First (Choices (Assoc));
3000 Typech := Empty;
3002 if Nkind (Selectr) = N_Others_Choice then
3004 -- Ada 2005 (AI-287): others choice may have expression or mbox
3006 if No (Others_Etype)
3007 and then not Others_Mbox
3008 then
3009 Error_Msg_N
3010 ("OTHERS must represent at least one component", Selectr);
3011 end if;
3013 exit Verification;
3014 end if;
3016 while Present (Selectr) loop
3017 New_Assoc := First (New_Assoc_List);
3018 while Present (New_Assoc) loop
3019 Component := First (Choices (New_Assoc));
3020 exit when Chars (Selectr) = Chars (Component);
3021 Next (New_Assoc);
3022 end loop;
3024 -- If no association, this is not a legal component of
3025 -- of the type in question, except if this is an internal
3026 -- component supplied by a previous expansion.
3028 if No (New_Assoc) then
3029 if Box_Present (Parent (Selectr)) then
3030 null;
3032 elsif Chars (Selectr) /= Name_uTag
3033 and then Chars (Selectr) /= Name_uParent
3034 and then Chars (Selectr) /= Name_uController
3035 then
3036 if not Has_Discriminants (Typ) then
3037 Error_Msg_Node_2 := Typ;
3038 Error_Msg_N
3039 ("& is not a component of}",
3040 Selectr);
3041 else
3042 Error_Msg_N
3043 ("& is not a component of the aggregate subtype",
3044 Selectr);
3045 end if;
3047 Check_Misspelled_Component (Components, Selectr);
3048 end if;
3050 elsif No (Typech) then
3051 Typech := Base_Type (Etype (Component));
3053 elsif Typech /= Base_Type (Etype (Component)) then
3054 if not Box_Present (Parent (Selectr)) then
3055 Error_Msg_N
3056 ("components in choice list must have same type",
3057 Selectr);
3058 end if;
3059 end if;
3061 Next (Selectr);
3062 end loop;
3064 Next (Assoc);
3065 end loop Verification;
3066 end Step_7;
3068 -- STEP 8: replace the original aggregate
3070 Step_8 : declare
3071 New_Aggregate : constant Node_Id := New_Copy (N);
3073 begin
3074 Set_Expressions (New_Aggregate, No_List);
3075 Set_Etype (New_Aggregate, Etype (N));
3076 Set_Component_Associations (New_Aggregate, New_Assoc_List);
3078 Rewrite (N, New_Aggregate);
3079 end Step_8;
3080 end Resolve_Record_Aggregate;
3082 -----------------------------
3083 -- Check_Can_Never_Be_Null --
3084 -----------------------------
3086 procedure Check_Can_Never_Be_Null (N : Node_Id; Expr : Node_Id) is
3087 begin
3088 pragma Assert (Ada_Version >= Ada_05);
3090 if Nkind (Expr) = N_Null
3091 and then Can_Never_Be_Null (N)
3092 then
3093 Apply_Compile_Time_Constraint_Error
3094 (N => Expr,
3095 Msg => "(Ada 2005) NULL not allowed in"
3096 & " null-excluding components?",
3097 Reason => CE_Null_Not_Allowed,
3098 Rep => False);
3099 end if;
3100 end Check_Can_Never_Be_Null;
3102 ---------------------
3103 -- Sort_Case_Table --
3104 ---------------------
3106 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
3107 L : constant Int := Case_Table'First;
3108 U : constant Int := Case_Table'Last;
3109 K : Int;
3110 J : Int;
3111 T : Case_Bounds;
3113 begin
3114 K := L;
3116 while K /= U loop
3117 T := Case_Table (K + 1);
3118 J := K + 1;
3120 while J /= L
3121 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
3122 Expr_Value (T.Choice_Lo)
3123 loop
3124 Case_Table (J) := Case_Table (J - 1);
3125 J := J - 1;
3126 end loop;
3128 Case_Table (J) := T;
3129 K := K + 1;
3130 end loop;
3131 end Sort_Case_Table;
3133 end Sem_Aggr;