Merge from mainline
[official-gcc.git] / gcc / ada / sem_aggr.adb
blob580dc29af450652c2d49316f8da75fa80b416924
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-2006, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Exp_Tss; use Exp_Tss;
33 with Exp_Util; use Exp_Util;
34 with Freeze; use Freeze;
35 with Itypes; use Itypes;
36 with Lib.Xref; use Lib.Xref;
37 with Namet; use Namet;
38 with Nmake; use Nmake;
39 with Nlists; use Nlists;
40 with Opt; use Opt;
41 with Sem; use Sem;
42 with Sem_Cat; use Sem_Cat;
43 with Sem_Ch8; use Sem_Ch8;
44 with Sem_Ch13; use Sem_Ch13;
45 with Sem_Eval; use Sem_Eval;
46 with Sem_Res; use Sem_Res;
47 with Sem_Util; use Sem_Util;
48 with Sem_Type; use Sem_Type;
49 with Sem_Warn; use Sem_Warn;
50 with Sinfo; use Sinfo;
51 with Snames; use Snames;
52 with Stringt; use Stringt;
53 with Stand; use Stand;
54 with Targparm; use Targparm;
55 with Tbuild; use Tbuild;
56 with Uintp; use Uintp;
58 with GNAT.Spelling_Checker; use GNAT.Spelling_Checker;
60 package body Sem_Aggr is
62 type Case_Bounds is record
63 Choice_Lo : Node_Id;
64 Choice_Hi : Node_Id;
65 Choice_Node : Node_Id;
66 end record;
68 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
69 -- Table type used by Check_Case_Choices procedure
71 -----------------------
72 -- Local Subprograms --
73 -----------------------
75 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
76 -- Sort the Case Table using the Lower Bound of each Choice as the key.
77 -- A simple insertion sort is used since the number of choices in a case
78 -- statement of variant part will usually be small and probably in near
79 -- sorted order.
81 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id);
82 -- Ada 2005 (AI-231): Check bad usage of null for a component for which
83 -- null exclusion (NOT NULL) is specified. Typ can be an E_Array_Type for
84 -- the array case (the component type of the array will be used) or an
85 -- E_Component/E_Discriminant entity in the record case, in which case the
86 -- type of the component will be used for the test. If Typ is any other
87 -- kind of entity, the call is ignored. Expr is the component node in the
88 -- aggregate which is an explicit occurrence of NULL. An error will be
89 -- issued if the component is null excluding.
91 -- It would be better to pass the proper type for Typ ???
93 ------------------------------------------------------
94 -- Subprograms used for RECORD AGGREGATE Processing --
95 ------------------------------------------------------
97 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
98 -- This procedure performs all the semantic checks required for record
99 -- aggregates. Note that for aggregates analysis and resolution go
100 -- hand in hand. Aggregate analysis has been delayed up to here and
101 -- it is done while resolving the aggregate.
103 -- N is the N_Aggregate node.
104 -- Typ is the record type for the aggregate resolution
106 -- While performing the semantic checks, this procedure builds a new
107 -- Component_Association_List where each record field appears alone in a
108 -- Component_Choice_List along with its corresponding expression. The
109 -- record fields in the Component_Association_List appear in the same order
110 -- in which they appear in the record type Typ.
112 -- Once this new Component_Association_List is built and all the semantic
113 -- checks performed, the original aggregate subtree is replaced with the
114 -- new named record aggregate just built. Note that subtree substitution is
115 -- performed with Rewrite so as to be able to retrieve the original
116 -- aggregate.
118 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
119 -- yields the aggregate format expected by Gigi. Typically, this kind of
120 -- tree manipulations are done in the expander. However, because the
121 -- semantic checks that need to be performed on record aggregates really go
122 -- hand in hand with the record aggregate normalization, the aggregate
123 -- subtree transformation is performed during resolution rather than
124 -- expansion. Had we decided otherwise we would have had to duplicate most
125 -- of the code in the expansion procedure Expand_Record_Aggregate. Note,
126 -- however, that all the expansion concerning aggegates for tagged records
127 -- is done in Expand_Record_Aggregate.
129 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
131 -- 1. Make sure that the record type against which the record aggregate
132 -- has to be resolved is not abstract. Furthermore if the type is
133 -- a null aggregate make sure the input aggregate N is also null.
135 -- 2. Verify that the structure of the aggregate is that of a record
136 -- aggregate. Specifically, look for component associations and ensure
137 -- that each choice list only has identifiers or the N_Others_Choice
138 -- node. Also make sure that if present, the N_Others_Choice occurs
139 -- last and by itself.
141 -- 3. If Typ contains discriminants, the values for each discriminant
142 -- is looked for. If the record type Typ has variants, we check
143 -- that the expressions corresponding to each discriminant ruling
144 -- the (possibly nested) variant parts of Typ, are static. This
145 -- allows us to determine the variant parts to which the rest of
146 -- the aggregate must conform. The names of discriminants with their
147 -- values are saved in a new association list, New_Assoc_List which
148 -- is later augmented with the names and values of the remaining
149 -- components in the record type.
151 -- During this phase we also make sure that every discriminant is
152 -- assigned exactly one value. Note that when several values
153 -- for a given discriminant are found, semantic processing continues
154 -- looking for further errors. In this case it's the first
155 -- discriminant value found which we will be recorded.
157 -- IMPORTANT NOTE: For derived tagged types this procedure expects
158 -- First_Discriminant and Next_Discriminant to give the correct list
159 -- of discriminants, in the correct order.
161 -- 4. After all the discriminant values have been gathered, we can
162 -- set the Etype of the record aggregate. If Typ contains no
163 -- discriminants this is straightforward: the Etype of N is just
164 -- Typ, otherwise a new implicit constrained subtype of Typ is
165 -- built to be the Etype of N.
167 -- 5. Gather the remaining record components according to the discriminant
168 -- values. This involves recursively traversing the record type
169 -- structure to see what variants are selected by the given discriminant
170 -- values. This processing is a little more convoluted if Typ is a
171 -- derived tagged types since we need to retrieve the record structure
172 -- of all the ancestors of Typ.
174 -- 6. After gathering the record components we look for their values
175 -- in the record aggregate and emit appropriate error messages
176 -- should we not find such values or should they be duplicated.
178 -- 7. We then make sure no illegal component names appear in the
179 -- record aggegate and make sure that the type of the record
180 -- components appearing in a same choice list is the same.
181 -- Finally we ensure that the others choice, if present, is
182 -- used to provide the value of at least a record component.
184 -- 8. The original aggregate node is replaced with the new named
185 -- aggregate built in steps 3 through 6, as explained earlier.
187 -- Given the complexity of record aggregate resolution, the primary
188 -- goal of this routine is clarity and simplicity rather than execution
189 -- and storage efficiency. If there are only positional components in the
190 -- aggregate the running time is linear. If there are associations
191 -- the running time is still linear as long as the order of the
192 -- associations is not too far off the order of the components in the
193 -- record type. If this is not the case the running time is at worst
194 -- quadratic in the size of the association list.
196 procedure Check_Misspelled_Component
197 (Elements : Elist_Id;
198 Component : Node_Id);
199 -- Give possible misspelling diagnostic if Component is likely to be
200 -- a misspelling of one of the components of the Assoc_List.
201 -- This is called by Resolv_Aggr_Expr after producing
202 -- an invalid component error message.
204 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id);
205 -- An optimization: determine whether a discriminated subtype has a
206 -- static constraint, and contains array components whose length is also
207 -- static, either because they are constrained by the discriminant, or
208 -- because the original component bounds are static.
210 -----------------------------------------------------
211 -- Subprograms used for ARRAY AGGREGATE Processing --
212 -----------------------------------------------------
214 function Resolve_Array_Aggregate
215 (N : Node_Id;
216 Index : Node_Id;
217 Index_Constr : Node_Id;
218 Component_Typ : Entity_Id;
219 Others_Allowed : Boolean)
220 return Boolean;
221 -- This procedure performs the semantic checks for an array aggregate.
222 -- True is returned if the aggregate resolution succeeds.
223 -- The procedure works by recursively checking each nested aggregate.
224 -- Specifically, after checking a sub-aggregate nested at the i-th level
225 -- we recursively check all the subaggregates at the i+1-st level (if any).
226 -- Note that for aggregates analysis and resolution go hand in hand.
227 -- Aggregate analysis has been delayed up to here and it is done while
228 -- resolving the aggregate.
230 -- N is the current N_Aggregate node to be checked.
232 -- Index is the index node corresponding to the array sub-aggregate that
233 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
234 -- corresponding index type (or subtype).
236 -- Index_Constr is the node giving the applicable index constraint if
237 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
238 -- contexts [...] that can be used to determine the bounds of the array
239 -- value specified by the aggregate". If Others_Allowed below is False
240 -- there is no applicable index constraint and this node is set to Index.
242 -- Component_Typ is the array component type.
244 -- Others_Allowed indicates whether an others choice is allowed
245 -- in the context where the top-level aggregate appeared.
247 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
249 -- 1. Make sure that the others choice, if present, is by itself and
250 -- appears last in the sub-aggregate. Check that we do not have
251 -- positional and named components in the array sub-aggregate (unless
252 -- the named association is an others choice). Finally if an others
253 -- choice is present, make sure it is allowed in the aggregate contex.
255 -- 2. If the array sub-aggregate contains discrete_choices:
257 -- (A) Verify their validity. Specifically verify that:
259 -- (a) If a null range is present it must be the only possible
260 -- choice in the array aggregate.
262 -- (b) Ditto for a non static range.
264 -- (c) Ditto for a non static expression.
266 -- In addition this step analyzes and resolves each discrete_choice,
267 -- making sure that its type is the type of the corresponding Index.
268 -- If we are not at the lowest array aggregate level (in the case of
269 -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
270 -- recursively on each component expression. Otherwise, resolve the
271 -- bottom level component expressions against the expected component
272 -- type ONLY IF the component corresponds to a single discrete choice
273 -- which is not an others choice (to see why read the DELAYED
274 -- COMPONENT RESOLUTION below).
276 -- (B) Determine the bounds of the sub-aggregate and lowest and
277 -- highest choice values.
279 -- 3. For positional aggregates:
281 -- (A) Loop over the component expressions either recursively invoking
282 -- Resolve_Array_Aggregate on each of these for multi-dimensional
283 -- array aggregates or resolving the bottom level component
284 -- expressions against the expected component type.
286 -- (B) Determine the bounds of the positional sub-aggregates.
288 -- 4. Try to determine statically whether the evaluation of the array
289 -- sub-aggregate raises Constraint_Error. If yes emit proper
290 -- warnings. The precise checks are the following:
292 -- (A) Check that the index range defined by aggregate bounds is
293 -- compatible with corresponding index subtype.
294 -- We also check against the base type. In fact it could be that
295 -- Low/High bounds of the base type are static whereas those of
296 -- the index subtype are not. Thus if we can statically catch
297 -- a problem with respect to the base type we are guaranteed
298 -- that the same problem will arise with the index subtype
300 -- (B) If we are dealing with a named aggregate containing an others
301 -- choice and at least one discrete choice then make sure the range
302 -- specified by the discrete choices does not overflow the
303 -- aggregate bounds. We also check against the index type and base
304 -- type bounds for the same reasons given in (A).
306 -- (C) If we are dealing with a positional aggregate with an others
307 -- choice make sure the number of positional elements specified
308 -- does not overflow the aggregate bounds. We also check against
309 -- the index type and base type bounds as mentioned in (A).
311 -- Finally construct an N_Range node giving the sub-aggregate bounds.
312 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
313 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
314 -- to build the appropriate aggregate subtype. Aggregate_Bounds
315 -- information is needed during expansion.
317 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
318 -- expressions in an array aggregate may call Duplicate_Subexpr or some
319 -- other routine that inserts code just outside the outermost aggregate.
320 -- If the array aggregate contains discrete choices or an others choice,
321 -- this may be wrong. Consider for instance the following example.
323 -- type Rec is record
324 -- V : Integer := 0;
325 -- end record;
327 -- type Acc_Rec is access Rec;
328 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
330 -- Then the transformation of "new Rec" that occurs during resolution
331 -- entails the following code modifications
333 -- P7b : constant Acc_Rec := new Rec;
334 -- RecIP (P7b.all);
335 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
337 -- This code transformation is clearly wrong, since we need to call
338 -- "new Rec" for each of the 3 array elements. To avoid this problem we
339 -- delay resolution of the components of non positional array aggregates
340 -- to the expansion phase. As an optimization, if the discrete choice
341 -- specifies a single value we do not delay resolution.
343 function Array_Aggr_Subtype (N : Node_Id; Typ : Node_Id) return Entity_Id;
344 -- This routine returns the type or subtype of an array aggregate.
346 -- N is the array aggregate node whose type we return.
348 -- Typ is the context type in which N occurs.
350 -- This routine creates an implicit array subtype whose bounds are
351 -- those defined by the aggregate. When this routine is invoked
352 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
353 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
354 -- sub-aggregate bounds. When building the aggegate itype, this function
355 -- traverses the array aggregate N collecting such Aggregate_Bounds and
356 -- constructs the proper array aggregate itype.
358 -- Note that in the case of multidimensional aggregates each inner
359 -- sub-aggregate corresponding to a given array dimension, may provide a
360 -- different bounds. If it is possible to determine statically that
361 -- some sub-aggregates corresponding to the same index do not have the
362 -- same bounds, then a warning is emitted. If such check is not possible
363 -- statically (because some sub-aggregate bounds are dynamic expressions)
364 -- then this job is left to the expander. In all cases the particular
365 -- bounds that this function will chose for a given dimension is the first
366 -- N_Range node for a sub-aggregate corresponding to that dimension.
368 -- Note that the Raises_Constraint_Error flag of an array aggregate
369 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
370 -- is set in Resolve_Array_Aggregate but the aggregate is not
371 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
372 -- first construct the proper itype for the aggregate (Gigi needs
373 -- this). After constructing the proper itype we will eventually replace
374 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
375 -- Of course in cases such as:
377 -- type Arr is array (integer range <>) of Integer;
378 -- A : Arr := (positive range -1 .. 2 => 0);
380 -- The bounds of the aggregate itype are cooked up to look reasonable
381 -- (in this particular case the bounds will be 1 .. 2).
383 procedure Aggregate_Constraint_Checks
384 (Exp : Node_Id;
385 Check_Typ : Entity_Id);
386 -- Checks expression Exp against subtype Check_Typ. If Exp is an
387 -- aggregate and Check_Typ a constrained record type with discriminants,
388 -- we generate the appropriate discriminant checks. If Exp is an array
389 -- aggregate then emit the appropriate length checks. If Exp is a scalar
390 -- type, or a string literal, Exp is changed into Check_Typ'(Exp) to
391 -- ensure that range checks are performed at run time.
393 procedure Make_String_Into_Aggregate (N : Node_Id);
394 -- A string literal can appear in a context in which a one dimensional
395 -- array of characters is expected. This procedure simply rewrites the
396 -- string as an aggregate, prior to resolution.
398 ---------------------------------
399 -- Aggregate_Constraint_Checks --
400 ---------------------------------
402 procedure Aggregate_Constraint_Checks
403 (Exp : Node_Id;
404 Check_Typ : Entity_Id)
406 Exp_Typ : constant Entity_Id := Etype (Exp);
408 begin
409 if Raises_Constraint_Error (Exp) then
410 return;
411 end if;
413 -- This is really expansion activity, so make sure that expansion
414 -- is on and is allowed.
416 if not Expander_Active or else In_Default_Expression then
417 return;
418 end if;
420 -- First check if we have to insert discriminant checks
422 if Has_Discriminants (Exp_Typ) then
423 Apply_Discriminant_Check (Exp, Check_Typ);
425 -- Next emit length checks for array aggregates
427 elsif Is_Array_Type (Exp_Typ) then
428 Apply_Length_Check (Exp, Check_Typ);
430 -- Finally emit scalar and string checks. If we are dealing with a
431 -- scalar literal we need to check by hand because the Etype of
432 -- literals is not necessarily correct.
434 elsif Is_Scalar_Type (Exp_Typ)
435 and then Compile_Time_Known_Value (Exp)
436 then
437 if Is_Out_Of_Range (Exp, Base_Type (Check_Typ)) then
438 Apply_Compile_Time_Constraint_Error
439 (Exp, "value not in range of}?", CE_Range_Check_Failed,
440 Ent => Base_Type (Check_Typ),
441 Typ => Base_Type (Check_Typ));
443 elsif Is_Out_Of_Range (Exp, Check_Typ) then
444 Apply_Compile_Time_Constraint_Error
445 (Exp, "value not in range of}?", CE_Range_Check_Failed,
446 Ent => Check_Typ,
447 Typ => Check_Typ);
449 elsif not Range_Checks_Suppressed (Check_Typ) then
450 Apply_Scalar_Range_Check (Exp, Check_Typ);
451 end if;
453 elsif (Is_Scalar_Type (Exp_Typ)
454 or else Nkind (Exp) = N_String_Literal)
455 and then Exp_Typ /= Check_Typ
456 then
457 if Is_Entity_Name (Exp)
458 and then Ekind (Entity (Exp)) = E_Constant
459 then
460 -- If expression is a constant, it is worthwhile checking whether
461 -- it is a bound of the type.
463 if (Is_Entity_Name (Type_Low_Bound (Check_Typ))
464 and then Entity (Exp) = Entity (Type_Low_Bound (Check_Typ)))
465 or else (Is_Entity_Name (Type_High_Bound (Check_Typ))
466 and then Entity (Exp) = Entity (Type_High_Bound (Check_Typ)))
467 then
468 return;
470 else
471 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
472 Analyze_And_Resolve (Exp, Check_Typ);
473 Check_Unset_Reference (Exp);
474 end if;
475 else
476 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
477 Analyze_And_Resolve (Exp, Check_Typ);
478 Check_Unset_Reference (Exp);
479 end if;
481 -- Ada 2005 (AI-230): Generate a conversion to an anonymous access
482 -- component's type to force the appropriate accessibility checks.
484 -- Ada 2005 (AI-231): Generate conversion to the null-excluding
485 -- type to force the corresponding run-time check
487 elsif Is_Access_Type (Check_Typ)
488 and then ((Is_Local_Anonymous_Access (Check_Typ))
489 or else (Can_Never_Be_Null (Check_Typ)
490 and then not Can_Never_Be_Null (Exp_Typ)))
491 then
492 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
493 Analyze_And_Resolve (Exp, Check_Typ);
494 Check_Unset_Reference (Exp);
495 end if;
496 end Aggregate_Constraint_Checks;
498 ------------------------
499 -- Array_Aggr_Subtype --
500 ------------------------
502 function Array_Aggr_Subtype
503 (N : Node_Id;
504 Typ : Entity_Id)
505 return Entity_Id
507 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
508 -- Number of aggregate index dimensions
510 Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
511 -- Constrained N_Range of each index dimension in our aggregate itype
513 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
514 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
515 -- Low and High bounds for each index dimension in our aggregate itype
517 Is_Fully_Positional : Boolean := True;
519 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
520 -- N is an array (sub-)aggregate. Dim is the dimension corresponding to
521 -- (sub-)aggregate N. This procedure collects the constrained N_Range
522 -- nodes corresponding to each index dimension of our aggregate itype.
523 -- These N_Range nodes are collected in Aggr_Range above.
525 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
526 -- bounds of each index dimension. If, when collecting, two bounds
527 -- corresponding to the same dimension are static and found to differ,
528 -- then emit a warning, and mark N as raising Constraint_Error.
530 -------------------------
531 -- Collect_Aggr_Bounds --
532 -------------------------
534 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
535 This_Range : constant Node_Id := Aggregate_Bounds (N);
536 -- The aggregate range node of this specific sub-aggregate
538 This_Low : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
539 This_High : constant Node_Id := High_Bound (Aggregate_Bounds (N));
540 -- The aggregate bounds of this specific sub-aggregate
542 Assoc : Node_Id;
543 Expr : Node_Id;
545 begin
546 -- Collect the first N_Range for a given dimension that you find.
547 -- For a given dimension they must be all equal anyway.
549 if No (Aggr_Range (Dim)) then
550 Aggr_Low (Dim) := This_Low;
551 Aggr_High (Dim) := This_High;
552 Aggr_Range (Dim) := This_Range;
554 else
555 if Compile_Time_Known_Value (This_Low) then
556 if not Compile_Time_Known_Value (Aggr_Low (Dim)) then
557 Aggr_Low (Dim) := This_Low;
559 elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
560 Set_Raises_Constraint_Error (N);
561 Error_Msg_N ("sub-aggregate low bound mismatch?", N);
562 Error_Msg_N
563 ("\Constraint_Error will be raised at run-time?", N);
564 end if;
565 end if;
567 if Compile_Time_Known_Value (This_High) then
568 if not Compile_Time_Known_Value (Aggr_High (Dim)) then
569 Aggr_High (Dim) := This_High;
571 elsif
572 Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
573 then
574 Set_Raises_Constraint_Error (N);
575 Error_Msg_N ("sub-aggregate high bound mismatch?", N);
576 Error_Msg_N
577 ("\Constraint_Error will be raised at run-time?", N);
578 end if;
579 end if;
580 end if;
582 if Dim < Aggr_Dimension then
584 -- Process positional components
586 if Present (Expressions (N)) then
587 Expr := First (Expressions (N));
588 while Present (Expr) loop
589 Collect_Aggr_Bounds (Expr, Dim + 1);
590 Next (Expr);
591 end loop;
592 end if;
594 -- Process component associations
596 if Present (Component_Associations (N)) then
597 Is_Fully_Positional := False;
599 Assoc := First (Component_Associations (N));
600 while Present (Assoc) loop
601 Expr := Expression (Assoc);
602 Collect_Aggr_Bounds (Expr, Dim + 1);
603 Next (Assoc);
604 end loop;
605 end if;
606 end if;
607 end Collect_Aggr_Bounds;
609 -- Array_Aggr_Subtype variables
611 Itype : Entity_Id;
612 -- the final itype of the overall aggregate
614 Index_Constraints : constant List_Id := New_List;
615 -- The list of index constraints of the aggregate itype
617 -- Start of processing for Array_Aggr_Subtype
619 begin
620 -- Make sure that the list of index constraints is properly attached
621 -- to the tree, and then collect the aggregate bounds.
623 Set_Parent (Index_Constraints, N);
624 Collect_Aggr_Bounds (N, 1);
626 -- Build the list of constrained indices of our aggregate itype
628 for J in 1 .. Aggr_Dimension loop
629 Create_Index : declare
630 Index_Base : constant Entity_Id :=
631 Base_Type (Etype (Aggr_Range (J)));
632 Index_Typ : Entity_Id;
634 begin
635 -- Construct the Index subtype
637 Index_Typ := Create_Itype (Subtype_Kind (Ekind (Index_Base)), N);
639 Set_Etype (Index_Typ, Index_Base);
641 if Is_Character_Type (Index_Base) then
642 Set_Is_Character_Type (Index_Typ);
643 end if;
645 Set_Size_Info (Index_Typ, (Index_Base));
646 Set_RM_Size (Index_Typ, RM_Size (Index_Base));
647 Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base));
648 Set_Scalar_Range (Index_Typ, Aggr_Range (J));
650 if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then
651 Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ)));
652 end if;
654 Set_Etype (Aggr_Range (J), Index_Typ);
656 Append (Aggr_Range (J), To => Index_Constraints);
657 end Create_Index;
658 end loop;
660 -- Now build the Itype
662 Itype := Create_Itype (E_Array_Subtype, N);
664 Set_First_Rep_Item (Itype, First_Rep_Item (Typ));
665 Set_Convention (Itype, Convention (Typ));
666 Set_Depends_On_Private (Itype, Has_Private_Component (Typ));
667 Set_Etype (Itype, Base_Type (Typ));
668 Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ));
669 Set_Is_Aliased (Itype, Is_Aliased (Typ));
670 Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
672 Copy_Suppress_Status (Index_Check, Typ, Itype);
673 Copy_Suppress_Status (Length_Check, Typ, Itype);
675 Set_First_Index (Itype, First (Index_Constraints));
676 Set_Is_Constrained (Itype, True);
677 Set_Is_Internal (Itype, True);
678 Init_Size_Align (Itype);
680 -- A simple optimization: purely positional aggregates of static
681 -- components should be passed to gigi unexpanded whenever possible,
682 -- and regardless of the staticness of the bounds themselves. Subse-
683 -- quent checks in exp_aggr verify that type is not packed, etc.
685 Set_Size_Known_At_Compile_Time (Itype,
686 Is_Fully_Positional
687 and then Comes_From_Source (N)
688 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
690 -- We always need a freeze node for a packed array subtype, so that
691 -- we can build the Packed_Array_Type corresponding to the subtype.
692 -- If expansion is disabled, the packed array subtype is not built,
693 -- and we must not generate a freeze node for the type, or else it
694 -- will appear incomplete to gigi.
696 if Is_Packed (Itype) and then not In_Default_Expression
697 and then Expander_Active
698 then
699 Freeze_Itype (Itype, N);
700 end if;
702 return Itype;
703 end Array_Aggr_Subtype;
705 --------------------------------
706 -- Check_Misspelled_Component --
707 --------------------------------
709 procedure Check_Misspelled_Component
710 (Elements : Elist_Id;
711 Component : Node_Id)
713 Max_Suggestions : constant := 2;
715 Nr_Of_Suggestions : Natural := 0;
716 Suggestion_1 : Entity_Id := Empty;
717 Suggestion_2 : Entity_Id := Empty;
718 Component_Elmt : Elmt_Id;
720 begin
721 -- All the components of List are matched against Component and
722 -- a count is maintained of possible misspellings. When at the
723 -- end of the analysis there are one or two (not more!) possible
724 -- misspellings, these misspellings will be suggested as
725 -- possible correction.
727 Get_Name_String (Chars (Component));
729 declare
730 S : constant String (1 .. Name_Len) :=
731 Name_Buffer (1 .. Name_Len);
733 begin
735 Component_Elmt := First_Elmt (Elements);
737 while Nr_Of_Suggestions <= Max_Suggestions
738 and then Present (Component_Elmt)
739 loop
741 Get_Name_String (Chars (Node (Component_Elmt)));
743 if Is_Bad_Spelling_Of (Name_Buffer (1 .. Name_Len), S) then
744 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
746 case Nr_Of_Suggestions is
747 when 1 => Suggestion_1 := Node (Component_Elmt);
748 when 2 => Suggestion_2 := Node (Component_Elmt);
749 when others => exit;
750 end case;
751 end if;
753 Next_Elmt (Component_Elmt);
754 end loop;
756 -- Report at most two suggestions
758 if Nr_Of_Suggestions = 1 then
759 Error_Msg_NE ("\possible misspelling of&",
760 Component, Suggestion_1);
762 elsif Nr_Of_Suggestions = 2 then
763 Error_Msg_Node_2 := Suggestion_2;
764 Error_Msg_NE ("\possible misspelling of& or&",
765 Component, Suggestion_1);
766 end if;
767 end;
768 end Check_Misspelled_Component;
770 ----------------------------------------
771 -- Check_Static_Discriminated_Subtype --
772 ----------------------------------------
774 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id) is
775 Disc : constant Entity_Id := First_Discriminant (T);
776 Comp : Entity_Id;
777 Ind : Entity_Id;
779 begin
780 if Has_Record_Rep_Clause (T) then
781 return;
783 elsif Present (Next_Discriminant (Disc)) then
784 return;
786 elsif Nkind (V) /= N_Integer_Literal then
787 return;
788 end if;
790 Comp := First_Component (T);
792 while Present (Comp) loop
794 if Is_Scalar_Type (Etype (Comp)) then
795 null;
797 elsif Is_Private_Type (Etype (Comp))
798 and then Present (Full_View (Etype (Comp)))
799 and then Is_Scalar_Type (Full_View (Etype (Comp)))
800 then
801 null;
803 elsif Is_Array_Type (Etype (Comp)) then
805 if Is_Bit_Packed_Array (Etype (Comp)) then
806 return;
807 end if;
809 Ind := First_Index (Etype (Comp));
811 while Present (Ind) loop
813 if Nkind (Ind) /= N_Range
814 or else Nkind (Low_Bound (Ind)) /= N_Integer_Literal
815 or else Nkind (High_Bound (Ind)) /= N_Integer_Literal
816 then
817 return;
818 end if;
820 Next_Index (Ind);
821 end loop;
823 else
824 return;
825 end if;
827 Next_Component (Comp);
828 end loop;
830 -- On exit, all components have statically known sizes
832 Set_Size_Known_At_Compile_Time (T);
833 end Check_Static_Discriminated_Subtype;
835 --------------------------------
836 -- Make_String_Into_Aggregate --
837 --------------------------------
839 procedure Make_String_Into_Aggregate (N : Node_Id) is
840 Exprs : constant List_Id := New_List;
841 Loc : constant Source_Ptr := Sloc (N);
842 Str : constant String_Id := Strval (N);
843 Strlen : constant Nat := String_Length (Str);
844 C : Char_Code;
845 C_Node : Node_Id;
846 New_N : Node_Id;
847 P : Source_Ptr;
849 begin
850 P := Loc + 1;
851 for J in 1 .. Strlen loop
852 C := Get_String_Char (Str, J);
853 Set_Character_Literal_Name (C);
855 C_Node :=
856 Make_Character_Literal (P,
857 Chars => Name_Find,
858 Char_Literal_Value => UI_From_CC (C));
859 Set_Etype (C_Node, Any_Character);
860 Append_To (Exprs, C_Node);
862 P := P + 1;
863 -- something special for wide strings ???
864 end loop;
866 New_N := Make_Aggregate (Loc, Expressions => Exprs);
867 Set_Analyzed (New_N);
868 Set_Etype (New_N, Any_Composite);
870 Rewrite (N, New_N);
871 end Make_String_Into_Aggregate;
873 -----------------------
874 -- Resolve_Aggregate --
875 -----------------------
877 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
878 Pkind : constant Node_Kind := Nkind (Parent (N));
880 Aggr_Subtyp : Entity_Id;
881 -- The actual aggregate subtype. This is not necessarily the same as Typ
882 -- which is the subtype of the context in which the aggregate was found.
884 begin
885 -- Check for aggregates not allowed in configurable run-time mode.
886 -- We allow all cases of aggregates that do not come from source,
887 -- since these are all assumed to be small (e.g. bounds of a string
888 -- literal). We also allow aggregates of types we know to be small.
890 if not Support_Aggregates_On_Target
891 and then Comes_From_Source (N)
892 and then (not Known_Static_Esize (Typ) or else Esize (Typ) > 64)
893 then
894 Error_Msg_CRT ("aggregate", N);
895 end if;
897 if Is_Limited_Composite (Typ) then
898 Error_Msg_N ("aggregate type cannot have limited component", N);
899 Explain_Limited_Type (Typ, N);
901 -- Ada 2005 (AI-287): Limited aggregates allowed
903 elsif Is_Limited_Type (Typ)
904 and Ada_Version < Ada_05
905 then
906 Error_Msg_N ("aggregate type cannot be limited", N);
907 Explain_Limited_Type (Typ, N);
909 elsif Is_Class_Wide_Type (Typ) then
910 Error_Msg_N ("type of aggregate cannot be class-wide", N);
912 elsif Typ = Any_String
913 or else Typ = Any_Composite
914 then
915 Error_Msg_N ("no unique type for aggregate", N);
916 Set_Etype (N, Any_Composite);
918 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
919 Error_Msg_N ("null record forbidden in array aggregate", N);
921 elsif Is_Record_Type (Typ) then
922 Resolve_Record_Aggregate (N, Typ);
924 elsif Is_Array_Type (Typ) then
926 -- First a special test, for the case of a positional aggregate
927 -- of characters which can be replaced by a string literal.
928 -- Do not perform this transformation if this was a string literal
929 -- to start with, whose components needed constraint checks, or if
930 -- the component type is non-static, because it will require those
931 -- checks and be transformed back into an aggregate.
933 if Number_Dimensions (Typ) = 1
934 and then
935 (Root_Type (Component_Type (Typ)) = Standard_Character
936 or else
937 Root_Type (Component_Type (Typ)) = Standard_Wide_Character
938 or else
939 Root_Type (Component_Type (Typ)) = Standard_Wide_Wide_Character)
940 and then No (Component_Associations (N))
941 and then not Is_Limited_Composite (Typ)
942 and then not Is_Private_Composite (Typ)
943 and then not Is_Bit_Packed_Array (Typ)
944 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
945 and then Is_Static_Subtype (Component_Type (Typ))
946 then
947 declare
948 Expr : Node_Id;
950 begin
951 Expr := First (Expressions (N));
952 while Present (Expr) loop
953 exit when Nkind (Expr) /= N_Character_Literal;
954 Next (Expr);
955 end loop;
957 if No (Expr) then
958 Start_String;
960 Expr := First (Expressions (N));
961 while Present (Expr) loop
962 Store_String_Char (UI_To_CC (Char_Literal_Value (Expr)));
963 Next (Expr);
964 end loop;
966 Rewrite (N,
967 Make_String_Literal (Sloc (N), End_String));
969 Analyze_And_Resolve (N, Typ);
970 return;
971 end if;
972 end;
973 end if;
975 -- Here if we have a real aggregate to deal with
977 Array_Aggregate : declare
978 Aggr_Resolved : Boolean;
980 Aggr_Typ : constant Entity_Id := Etype (Typ);
981 -- This is the unconstrained array type, which is the type
982 -- against which the aggregate is to be resolved. Typ itself
983 -- is the array type of the context which may not be the same
984 -- subtype as the subtype for the final aggregate.
986 begin
987 -- In the following we determine whether an others choice is
988 -- allowed inside the array aggregate. The test checks the context
989 -- in which the array aggregate occurs. If the context does not
990 -- permit it, or the aggregate type is unconstrained, an others
991 -- choice is not allowed.
993 -- If expansion is disabled (generic context, or semantics-only
994 -- mode) actual subtypes cannot be constructed, and the type of
995 -- an object may be its unconstrained nominal type. However, if
996 -- the context is an assignment, we assume that "others" is
997 -- allowed, because the target of the assignment will have a
998 -- constrained subtype when fully compiled.
1000 -- Note that there is no node for Explicit_Actual_Parameter.
1001 -- To test for this context we therefore have to test for node
1002 -- N_Parameter_Association which itself appears only if there is a
1003 -- formal parameter. Consequently we also need to test for
1004 -- N_Procedure_Call_Statement or N_Function_Call.
1006 Set_Etype (N, Aggr_Typ); -- may be overridden later on
1008 if Is_Constrained (Typ) and then
1009 (Pkind = N_Assignment_Statement or else
1010 Pkind = N_Parameter_Association or else
1011 Pkind = N_Function_Call or else
1012 Pkind = N_Procedure_Call_Statement or else
1013 Pkind = N_Generic_Association or else
1014 Pkind = N_Formal_Object_Declaration or else
1015 Pkind = N_Return_Statement or else
1016 Pkind = N_Object_Declaration or else
1017 Pkind = N_Component_Declaration or else
1018 Pkind = N_Parameter_Specification or else
1019 Pkind = N_Qualified_Expression or else
1020 Pkind = N_Aggregate or else
1021 Pkind = N_Extension_Aggregate or else
1022 Pkind = N_Component_Association)
1023 then
1024 Aggr_Resolved :=
1025 Resolve_Array_Aggregate
1027 Index => First_Index (Aggr_Typ),
1028 Index_Constr => First_Index (Typ),
1029 Component_Typ => Component_Type (Typ),
1030 Others_Allowed => True);
1032 elsif not Expander_Active
1033 and then Pkind = N_Assignment_Statement
1034 then
1035 Aggr_Resolved :=
1036 Resolve_Array_Aggregate
1038 Index => First_Index (Aggr_Typ),
1039 Index_Constr => First_Index (Typ),
1040 Component_Typ => Component_Type (Typ),
1041 Others_Allowed => True);
1042 else
1043 Aggr_Resolved :=
1044 Resolve_Array_Aggregate
1046 Index => First_Index (Aggr_Typ),
1047 Index_Constr => First_Index (Aggr_Typ),
1048 Component_Typ => Component_Type (Typ),
1049 Others_Allowed => False);
1050 end if;
1052 if not Aggr_Resolved then
1053 Aggr_Subtyp := Any_Composite;
1054 else
1055 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1056 end if;
1058 Set_Etype (N, Aggr_Subtyp);
1059 end Array_Aggregate;
1061 elsif Is_Private_Type (Typ)
1062 and then Present (Full_View (Typ))
1063 and then In_Inlined_Body
1064 and then Is_Composite_Type (Full_View (Typ))
1065 then
1066 Resolve (N, Full_View (Typ));
1068 else
1069 Error_Msg_N ("illegal context for aggregate", N);
1070 end if;
1072 -- If we can determine statically that the evaluation of the
1073 -- aggregate raises Constraint_Error, then replace the
1074 -- aggregate with an N_Raise_Constraint_Error node, but set the
1075 -- Etype to the right aggregate subtype. Gigi needs this.
1077 if Raises_Constraint_Error (N) then
1078 Aggr_Subtyp := Etype (N);
1079 Rewrite (N,
1080 Make_Raise_Constraint_Error (Sloc (N),
1081 Reason => CE_Range_Check_Failed));
1082 Set_Raises_Constraint_Error (N);
1083 Set_Etype (N, Aggr_Subtyp);
1084 Set_Analyzed (N);
1085 end if;
1086 end Resolve_Aggregate;
1088 -----------------------------
1089 -- Resolve_Array_Aggregate --
1090 -----------------------------
1092 function Resolve_Array_Aggregate
1093 (N : Node_Id;
1094 Index : Node_Id;
1095 Index_Constr : Node_Id;
1096 Component_Typ : Entity_Id;
1097 Others_Allowed : Boolean)
1098 return Boolean
1100 Loc : constant Source_Ptr := Sloc (N);
1102 Failure : constant Boolean := False;
1103 Success : constant Boolean := True;
1105 Index_Typ : constant Entity_Id := Etype (Index);
1106 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1107 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1108 -- The type of the index corresponding to the array sub-aggregate
1109 -- along with its low and upper bounds
1111 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1112 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1113 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1114 -- ditto for the base type
1116 function Add (Val : Uint; To : Node_Id) return Node_Id;
1117 -- Creates a new expression node where Val is added to expression To.
1118 -- Tries to constant fold whenever possible. To must be an already
1119 -- analyzed expression.
1121 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1122 -- Checks that AH (the upper bound of an array aggregate) is <= BH
1123 -- (the upper bound of the index base type). If the check fails a
1124 -- warning is emitted, the Raises_Constraint_Error Flag of N is set,
1125 -- and AH is replaced with a duplicate of BH.
1127 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1128 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1129 -- warning if not and sets the Raises_Constraint_Error Flag in N.
1131 procedure Check_Length (L, H : Node_Id; Len : Uint);
1132 -- Checks that range L .. H contains at least Len elements. Emits a
1133 -- warning if not and sets the Raises_Constraint_Error Flag in N.
1135 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1136 -- Returns True if range L .. H is dynamic or null
1138 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1139 -- Given expression node From, this routine sets OK to False if it
1140 -- cannot statically evaluate From. Otherwise it stores this static
1141 -- value into Value.
1143 function Resolve_Aggr_Expr
1144 (Expr : Node_Id;
1145 Single_Elmt : Boolean)
1146 return Boolean;
1147 -- Resolves aggregate expression Expr. Returs False if resolution
1148 -- fails. If Single_Elmt is set to False, the expression Expr may be
1149 -- used to initialize several array aggregate elements (this can
1150 -- happen for discrete choices such as "L .. H => Expr" or the others
1151 -- choice). In this event we do not resolve Expr unless expansion is
1152 -- disabled. To know why, see the DELAYED COMPONENT RESOLUTION
1153 -- note above.
1155 ---------
1156 -- Add --
1157 ---------
1159 function Add (Val : Uint; To : Node_Id) return Node_Id is
1160 Expr_Pos : Node_Id;
1161 Expr : Node_Id;
1162 To_Pos : Node_Id;
1164 begin
1165 if Raises_Constraint_Error (To) then
1166 return To;
1167 end if;
1169 -- First test if we can do constant folding
1171 if Compile_Time_Known_Value (To)
1172 or else Nkind (To) = N_Integer_Literal
1173 then
1174 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1175 Set_Is_Static_Expression (Expr_Pos);
1176 Set_Etype (Expr_Pos, Etype (To));
1177 Set_Analyzed (Expr_Pos, Analyzed (To));
1179 if not Is_Enumeration_Type (Index_Typ) then
1180 Expr := Expr_Pos;
1182 -- If we are dealing with enumeration return
1183 -- Index_Typ'Val (Expr_Pos)
1185 else
1186 Expr :=
1187 Make_Attribute_Reference
1188 (Loc,
1189 Prefix => New_Reference_To (Index_Typ, Loc),
1190 Attribute_Name => Name_Val,
1191 Expressions => New_List (Expr_Pos));
1192 end if;
1194 return Expr;
1195 end if;
1197 -- If we are here no constant folding possible
1199 if not Is_Enumeration_Type (Index_Base) then
1200 Expr :=
1201 Make_Op_Add (Loc,
1202 Left_Opnd => Duplicate_Subexpr (To),
1203 Right_Opnd => Make_Integer_Literal (Loc, Val));
1205 -- If we are dealing with enumeration return
1206 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1208 else
1209 To_Pos :=
1210 Make_Attribute_Reference
1211 (Loc,
1212 Prefix => New_Reference_To (Index_Typ, Loc),
1213 Attribute_Name => Name_Pos,
1214 Expressions => New_List (Duplicate_Subexpr (To)));
1216 Expr_Pos :=
1217 Make_Op_Add (Loc,
1218 Left_Opnd => To_Pos,
1219 Right_Opnd => Make_Integer_Literal (Loc, Val));
1221 Expr :=
1222 Make_Attribute_Reference
1223 (Loc,
1224 Prefix => New_Reference_To (Index_Typ, Loc),
1225 Attribute_Name => Name_Val,
1226 Expressions => New_List (Expr_Pos));
1227 end if;
1229 return Expr;
1230 end Add;
1232 -----------------
1233 -- Check_Bound --
1234 -----------------
1236 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1237 Val_BH : Uint;
1238 Val_AH : Uint;
1240 OK_BH : Boolean;
1241 OK_AH : Boolean;
1243 begin
1244 Get (Value => Val_BH, From => BH, OK => OK_BH);
1245 Get (Value => Val_AH, From => AH, OK => OK_AH);
1247 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1248 Set_Raises_Constraint_Error (N);
1249 Error_Msg_N ("upper bound out of range?", AH);
1250 Error_Msg_N ("\Constraint_Error will be raised at run-time?", AH);
1252 -- You need to set AH to BH or else in the case of enumerations
1253 -- indices we will not be able to resolve the aggregate bounds.
1255 AH := Duplicate_Subexpr (BH);
1256 end if;
1257 end Check_Bound;
1259 ------------------
1260 -- Check_Bounds --
1261 ------------------
1263 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1264 Val_L : Uint;
1265 Val_H : Uint;
1266 Val_AL : Uint;
1267 Val_AH : Uint;
1269 OK_L : Boolean;
1270 OK_H : Boolean;
1271 OK_AL : Boolean;
1272 OK_AH : Boolean;
1274 begin
1275 if Raises_Constraint_Error (N)
1276 or else Dynamic_Or_Null_Range (AL, AH)
1277 then
1278 return;
1279 end if;
1281 Get (Value => Val_L, From => L, OK => OK_L);
1282 Get (Value => Val_H, From => H, OK => OK_H);
1284 Get (Value => Val_AL, From => AL, OK => OK_AL);
1285 Get (Value => Val_AH, From => AH, OK => OK_AH);
1287 if OK_L and then Val_L > Val_AL then
1288 Set_Raises_Constraint_Error (N);
1289 Error_Msg_N ("lower bound of aggregate out of range?", N);
1290 Error_Msg_N ("\Constraint_Error will be raised at run-time?", N);
1291 end if;
1293 if OK_H and then Val_H < Val_AH then
1294 Set_Raises_Constraint_Error (N);
1295 Error_Msg_N ("upper bound of aggregate out of range?", N);
1296 Error_Msg_N ("\Constraint_Error will be raised at run-time?", N);
1297 end if;
1298 end Check_Bounds;
1300 ------------------
1301 -- Check_Length --
1302 ------------------
1304 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1305 Val_L : Uint;
1306 Val_H : Uint;
1308 OK_L : Boolean;
1309 OK_H : Boolean;
1311 Range_Len : Uint;
1313 begin
1314 if Raises_Constraint_Error (N) then
1315 return;
1316 end if;
1318 Get (Value => Val_L, From => L, OK => OK_L);
1319 Get (Value => Val_H, From => H, OK => OK_H);
1321 if not OK_L or else not OK_H then
1322 return;
1323 end if;
1325 -- If null range length is zero
1327 if Val_L > Val_H then
1328 Range_Len := Uint_0;
1329 else
1330 Range_Len := Val_H - Val_L + 1;
1331 end if;
1333 if Range_Len < Len then
1334 Set_Raises_Constraint_Error (N);
1335 Error_Msg_N ("too many elements?", N);
1336 Error_Msg_N ("\Constraint_Error will be raised at run-time?", N);
1337 end if;
1338 end Check_Length;
1340 ---------------------------
1341 -- Dynamic_Or_Null_Range --
1342 ---------------------------
1344 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1345 Val_L : Uint;
1346 Val_H : Uint;
1348 OK_L : Boolean;
1349 OK_H : Boolean;
1351 begin
1352 Get (Value => Val_L, From => L, OK => OK_L);
1353 Get (Value => Val_H, From => H, OK => OK_H);
1355 return not OK_L or else not OK_H
1356 or else not Is_OK_Static_Expression (L)
1357 or else not Is_OK_Static_Expression (H)
1358 or else Val_L > Val_H;
1359 end Dynamic_Or_Null_Range;
1361 ---------
1362 -- Get --
1363 ---------
1365 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1366 begin
1367 OK := True;
1369 if Compile_Time_Known_Value (From) then
1370 Value := Expr_Value (From);
1372 -- If expression From is something like Some_Type'Val (10) then
1373 -- Value = 10
1375 elsif Nkind (From) = N_Attribute_Reference
1376 and then Attribute_Name (From) = Name_Val
1377 and then Compile_Time_Known_Value (First (Expressions (From)))
1378 then
1379 Value := Expr_Value (First (Expressions (From)));
1381 else
1382 Value := Uint_0;
1383 OK := False;
1384 end if;
1385 end Get;
1387 -----------------------
1388 -- Resolve_Aggr_Expr --
1389 -----------------------
1391 function Resolve_Aggr_Expr
1392 (Expr : Node_Id;
1393 Single_Elmt : Boolean)
1394 return Boolean
1396 Nxt_Ind : constant Node_Id := Next_Index (Index);
1397 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
1398 -- Index is the current index corresponding to the expresion
1400 Resolution_OK : Boolean := True;
1401 -- Set to False if resolution of the expression failed
1403 begin
1404 -- If the array type against which we are resolving the aggregate
1405 -- has several dimensions, the expressions nested inside the
1406 -- aggregate must be further aggregates (or strings).
1408 if Present (Nxt_Ind) then
1409 if Nkind (Expr) /= N_Aggregate then
1411 -- A string literal can appear where a one-dimensional array
1412 -- of characters is expected. If the literal looks like an
1413 -- operator, it is still an operator symbol, which will be
1414 -- transformed into a string when analyzed.
1416 if Is_Character_Type (Component_Typ)
1417 and then No (Next_Index (Nxt_Ind))
1418 and then (Nkind (Expr) = N_String_Literal
1419 or else Nkind (Expr) = N_Operator_Symbol)
1420 then
1421 -- A string literal used in a multidimensional array
1422 -- aggregate in place of the final one-dimensional
1423 -- aggregate must not be enclosed in parentheses.
1425 if Paren_Count (Expr) /= 0 then
1426 Error_Msg_N ("no parenthesis allowed here", Expr);
1427 end if;
1429 Make_String_Into_Aggregate (Expr);
1431 else
1432 Error_Msg_N ("nested array aggregate expected", Expr);
1433 return Failure;
1434 end if;
1435 end if;
1437 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
1438 -- Required to check the null-exclusion attribute (if present).
1439 -- This value may be overridden later on.
1441 Set_Etype (Expr, Etype (N));
1443 Resolution_OK := Resolve_Array_Aggregate
1444 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1446 -- Do not resolve the expressions of discrete or others choices
1447 -- unless the expression covers a single component, or the expander
1448 -- is inactive.
1450 elsif Single_Elmt
1451 or else not Expander_Active
1452 or else In_Default_Expression
1453 then
1454 Analyze_And_Resolve (Expr, Component_Typ);
1455 Check_Non_Static_Context (Expr);
1456 Aggregate_Constraint_Checks (Expr, Component_Typ);
1457 Check_Unset_Reference (Expr);
1458 end if;
1460 if Raises_Constraint_Error (Expr)
1461 and then Nkind (Parent (Expr)) /= N_Component_Association
1462 then
1463 Set_Raises_Constraint_Error (N);
1464 end if;
1466 return Resolution_OK;
1467 end Resolve_Aggr_Expr;
1469 -- Variables local to Resolve_Array_Aggregate
1471 Assoc : Node_Id;
1472 Choice : Node_Id;
1473 Expr : Node_Id;
1475 Who_Cares : Node_Id;
1477 Aggr_Low : Node_Id := Empty;
1478 Aggr_High : Node_Id := Empty;
1479 -- The actual low and high bounds of this sub-aggegate
1481 Choices_Low : Node_Id := Empty;
1482 Choices_High : Node_Id := Empty;
1483 -- The lowest and highest discrete choices values for a named aggregate
1485 Nb_Elements : Uint := Uint_0;
1486 -- The number of elements in a positional aggegate
1488 Others_Present : Boolean := False;
1490 Nb_Choices : Nat := 0;
1491 -- Contains the overall number of named choices in this sub-aggregate
1493 Nb_Discrete_Choices : Nat := 0;
1494 -- The overall number of discrete choices (not counting others choice)
1496 Case_Table_Size : Nat;
1497 -- Contains the size of the case table needed to sort aggregate choices
1499 -- Start of processing for Resolve_Array_Aggregate
1501 begin
1502 -- STEP 1: make sure the aggregate is correctly formatted
1504 if Present (Component_Associations (N)) then
1505 Assoc := First (Component_Associations (N));
1506 while Present (Assoc) loop
1507 Choice := First (Choices (Assoc));
1508 while Present (Choice) loop
1509 if Nkind (Choice) = N_Others_Choice then
1510 Others_Present := True;
1512 if Choice /= First (Choices (Assoc))
1513 or else Present (Next (Choice))
1514 then
1515 Error_Msg_N
1516 ("OTHERS must appear alone in a choice list", Choice);
1517 return Failure;
1518 end if;
1520 if Present (Next (Assoc)) then
1521 Error_Msg_N
1522 ("OTHERS must appear last in an aggregate", Choice);
1523 return Failure;
1524 end if;
1526 if Ada_Version = Ada_83
1527 and then Assoc /= First (Component_Associations (N))
1528 and then (Nkind (Parent (N)) = N_Assignment_Statement
1529 or else
1530 Nkind (Parent (N)) = N_Object_Declaration)
1531 then
1532 Error_Msg_N
1533 ("(Ada 83) illegal context for OTHERS choice", N);
1534 end if;
1535 end if;
1537 Nb_Choices := Nb_Choices + 1;
1538 Next (Choice);
1539 end loop;
1541 Next (Assoc);
1542 end loop;
1543 end if;
1545 -- At this point we know that the others choice, if present, is by
1546 -- itself and appears last in the aggregate. Check if we have mixed
1547 -- positional and discrete associations (other than the others choice).
1549 if Present (Expressions (N))
1550 and then (Nb_Choices > 1
1551 or else (Nb_Choices = 1 and then not Others_Present))
1552 then
1553 Error_Msg_N
1554 ("named association cannot follow positional association",
1555 First (Choices (First (Component_Associations (N)))));
1556 return Failure;
1557 end if;
1559 -- Test for the validity of an others choice if present
1561 if Others_Present and then not Others_Allowed then
1562 Error_Msg_N
1563 ("OTHERS choice not allowed here",
1564 First (Choices (First (Component_Associations (N)))));
1565 return Failure;
1566 end if;
1568 -- Protect against cascaded errors
1570 if Etype (Index_Typ) = Any_Type then
1571 return Failure;
1572 end if;
1574 -- STEP 2: Process named components
1576 if No (Expressions (N)) then
1578 if Others_Present then
1579 Case_Table_Size := Nb_Choices - 1;
1580 else
1581 Case_Table_Size := Nb_Choices;
1582 end if;
1584 Step_2 : declare
1585 Low : Node_Id;
1586 High : Node_Id;
1587 -- Denote the lowest and highest values in an aggregate choice
1589 Hi_Val : Uint;
1590 Lo_Val : Uint;
1591 -- High end of one range and Low end of the next. Should be
1592 -- contiguous if there is no hole in the list of values.
1594 Missing_Values : Boolean;
1595 -- Set True if missing index values
1597 S_Low : Node_Id := Empty;
1598 S_High : Node_Id := Empty;
1599 -- if a choice in an aggregate is a subtype indication these
1600 -- denote the lowest and highest values of the subtype
1602 Table : Case_Table_Type (1 .. Case_Table_Size);
1603 -- Used to sort all the different choice values
1605 Single_Choice : Boolean;
1606 -- Set to true every time there is a single discrete choice in a
1607 -- discrete association
1609 Prev_Nb_Discrete_Choices : Nat;
1610 -- Used to keep track of the number of discrete choices
1611 -- in the current association.
1613 begin
1614 -- STEP 2 (A): Check discrete choices validity
1616 Assoc := First (Component_Associations (N));
1617 while Present (Assoc) loop
1619 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
1620 Choice := First (Choices (Assoc));
1621 loop
1622 Analyze (Choice);
1624 if Nkind (Choice) = N_Others_Choice then
1625 Single_Choice := False;
1626 exit;
1628 -- Test for subtype mark without constraint
1630 elsif Is_Entity_Name (Choice) and then
1631 Is_Type (Entity (Choice))
1632 then
1633 if Base_Type (Entity (Choice)) /= Index_Base then
1634 Error_Msg_N
1635 ("invalid subtype mark in aggregate choice",
1636 Choice);
1637 return Failure;
1638 end if;
1640 elsif Nkind (Choice) = N_Subtype_Indication then
1641 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
1643 -- Does the subtype indication evaluation raise CE ?
1645 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
1646 Get_Index_Bounds (Choice, Low, High);
1647 Check_Bounds (S_Low, S_High, Low, High);
1649 else -- Choice is a range or an expression
1650 Resolve (Choice, Index_Base);
1651 Check_Unset_Reference (Choice);
1652 Check_Non_Static_Context (Choice);
1654 -- Do not range check a choice. This check is redundant
1655 -- since this test is already performed when we check
1656 -- that the bounds of the array aggregate are within
1657 -- range.
1659 Set_Do_Range_Check (Choice, False);
1660 end if;
1662 -- If we could not resolve the discrete choice stop here
1664 if Etype (Choice) = Any_Type then
1665 return Failure;
1667 -- If the discrete choice raises CE get its original bounds
1669 elsif Nkind (Choice) = N_Raise_Constraint_Error then
1670 Set_Raises_Constraint_Error (N);
1671 Get_Index_Bounds (Original_Node (Choice), Low, High);
1673 -- Otherwise get its bounds as usual
1675 else
1676 Get_Index_Bounds (Choice, Low, High);
1677 end if;
1679 if (Dynamic_Or_Null_Range (Low, High)
1680 or else (Nkind (Choice) = N_Subtype_Indication
1681 and then
1682 Dynamic_Or_Null_Range (S_Low, S_High)))
1683 and then Nb_Choices /= 1
1684 then
1685 Error_Msg_N
1686 ("dynamic or empty choice in aggregate " &
1687 "must be the only choice", Choice);
1688 return Failure;
1689 end if;
1691 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
1692 Table (Nb_Discrete_Choices).Choice_Lo := Low;
1693 Table (Nb_Discrete_Choices).Choice_Hi := High;
1695 Next (Choice);
1697 if No (Choice) then
1699 -- Check if we have a single discrete choice and whether
1700 -- this discrete choice specifies a single value.
1702 Single_Choice :=
1703 (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1)
1704 and then (Low = High);
1706 exit;
1707 end if;
1708 end loop;
1710 -- Ada 2005 (AI-231)
1712 if Ada_Version >= Ada_05
1713 and then Nkind (Expression (Assoc)) = N_Null
1714 then
1715 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
1716 end if;
1718 -- Ada 2005 (AI-287): In case of default initialized component
1719 -- we delay the resolution to the expansion phase
1721 if Box_Present (Assoc) then
1723 -- Ada 2005 (AI-287): In case of default initialization
1724 -- of a component the expander will generate calls to
1725 -- the corresponding initialization subprogram.
1727 null;
1729 elsif not Resolve_Aggr_Expr (Expression (Assoc),
1730 Single_Elmt => Single_Choice)
1731 then
1732 return Failure;
1733 end if;
1735 Next (Assoc);
1736 end loop;
1738 -- If aggregate contains more than one choice then these must be
1739 -- static. Sort them and check that they are contiguous
1741 if Nb_Discrete_Choices > 1 then
1742 Sort_Case_Table (Table);
1743 Missing_Values := False;
1745 Outer : for J in 1 .. Nb_Discrete_Choices - 1 loop
1746 if Expr_Value (Table (J).Choice_Hi) >=
1747 Expr_Value (Table (J + 1).Choice_Lo)
1748 then
1749 Error_Msg_N
1750 ("duplicate choice values in array aggregate",
1751 Table (J).Choice_Hi);
1752 return Failure;
1754 elsif not Others_Present then
1756 Hi_Val := Expr_Value (Table (J).Choice_Hi);
1757 Lo_Val := Expr_Value (Table (J + 1).Choice_Lo);
1759 -- If missing values, output error messages
1761 if Lo_Val - Hi_Val > 1 then
1763 -- Header message if not first missing value
1765 if not Missing_Values then
1766 Error_Msg_N
1767 ("missing index value(s) in array aggregate", N);
1768 Missing_Values := True;
1769 end if;
1771 -- Output values of missing indexes
1773 Lo_Val := Lo_Val - 1;
1774 Hi_Val := Hi_Val + 1;
1776 -- Enumeration type case
1778 if Is_Enumeration_Type (Index_Typ) then
1779 Error_Msg_Name_1 :=
1780 Chars
1781 (Get_Enum_Lit_From_Pos
1782 (Index_Typ, Hi_Val, Loc));
1784 if Lo_Val = Hi_Val then
1785 Error_Msg_N ("\ %", N);
1786 else
1787 Error_Msg_Name_2 :=
1788 Chars
1789 (Get_Enum_Lit_From_Pos
1790 (Index_Typ, Lo_Val, Loc));
1791 Error_Msg_N ("\ % .. %", N);
1792 end if;
1794 -- Integer types case
1796 else
1797 Error_Msg_Uint_1 := Hi_Val;
1799 if Lo_Val = Hi_Val then
1800 Error_Msg_N ("\ ^", N);
1801 else
1802 Error_Msg_Uint_2 := Lo_Val;
1803 Error_Msg_N ("\ ^ .. ^", N);
1804 end if;
1805 end if;
1806 end if;
1807 end if;
1808 end loop Outer;
1810 if Missing_Values then
1811 Set_Etype (N, Any_Composite);
1812 return Failure;
1813 end if;
1814 end if;
1816 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
1818 if Nb_Discrete_Choices > 0 then
1819 Choices_Low := Table (1).Choice_Lo;
1820 Choices_High := Table (Nb_Discrete_Choices).Choice_Hi;
1821 end if;
1823 if Others_Present then
1824 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
1826 else
1827 Aggr_Low := Choices_Low;
1828 Aggr_High := Choices_High;
1829 end if;
1830 end Step_2;
1832 -- STEP 3: Process positional components
1834 else
1835 -- STEP 3 (A): Process positional elements
1837 Expr := First (Expressions (N));
1838 Nb_Elements := Uint_0;
1839 while Present (Expr) loop
1840 Nb_Elements := Nb_Elements + 1;
1842 -- Ada 2005 (AI-231)
1844 if Ada_Version >= Ada_05
1845 and then Nkind (Expr) = N_Null
1846 then
1847 Check_Can_Never_Be_Null (Etype (N), Expr);
1848 end if;
1850 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
1851 return Failure;
1852 end if;
1854 Next (Expr);
1855 end loop;
1857 if Others_Present then
1858 Assoc := Last (Component_Associations (N));
1860 -- Ada 2005 (AI-231)
1862 if Ada_Version >= Ada_05
1863 and then Nkind (Assoc) = N_Null
1864 then
1865 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
1866 end if;
1868 -- Ada 2005 (AI-287): In case of default initialized component
1869 -- we delay the resolution to the expansion phase.
1871 if Box_Present (Assoc) then
1873 -- Ada 2005 (AI-287): In case of default initialization
1874 -- of a component the expander will generate calls to
1875 -- the corresponding initialization subprogram.
1877 null;
1879 elsif not Resolve_Aggr_Expr (Expression (Assoc),
1880 Single_Elmt => False)
1881 then
1882 return Failure;
1883 end if;
1884 end if;
1886 -- STEP 3 (B): Compute the aggregate bounds
1888 if Others_Present then
1889 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
1891 else
1892 if Others_Allowed then
1893 Get_Index_Bounds (Index_Constr, Aggr_Low, Who_Cares);
1894 else
1895 Aggr_Low := Index_Typ_Low;
1896 end if;
1898 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
1899 Check_Bound (Index_Base_High, Aggr_High);
1900 end if;
1901 end if;
1903 -- STEP 4: Perform static aggregate checks and save the bounds
1905 -- Check (A)
1907 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
1908 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
1910 -- Check (B)
1912 if Others_Present and then Nb_Discrete_Choices > 0 then
1913 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
1914 Check_Bounds (Index_Typ_Low, Index_Typ_High,
1915 Choices_Low, Choices_High);
1916 Check_Bounds (Index_Base_Low, Index_Base_High,
1917 Choices_Low, Choices_High);
1919 -- Check (C)
1921 elsif Others_Present and then Nb_Elements > 0 then
1922 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
1923 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
1924 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
1925 end if;
1927 if Raises_Constraint_Error (Aggr_Low)
1928 or else Raises_Constraint_Error (Aggr_High)
1929 then
1930 Set_Raises_Constraint_Error (N);
1931 end if;
1933 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
1935 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
1936 -- since the addition node returned by Add is not yet analyzed. Attach
1937 -- to tree and analyze first. Reset analyzed flag to insure it will get
1938 -- analyzed when it is a literal bound whose type must be properly set.
1940 if Others_Present or else Nb_Discrete_Choices > 0 then
1941 Aggr_High := Duplicate_Subexpr (Aggr_High);
1943 if Etype (Aggr_High) = Universal_Integer then
1944 Set_Analyzed (Aggr_High, False);
1945 end if;
1946 end if;
1948 Set_Aggregate_Bounds
1949 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
1951 -- The bounds may contain expressions that must be inserted upwards.
1952 -- Attach them fully to the tree. After analysis, remove side effects
1953 -- from upper bound, if still needed.
1955 Set_Parent (Aggregate_Bounds (N), N);
1956 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
1957 Check_Unset_Reference (Aggregate_Bounds (N));
1959 if not Others_Present and then Nb_Discrete_Choices = 0 then
1960 Set_High_Bound (Aggregate_Bounds (N),
1961 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
1962 end if;
1964 return Success;
1965 end Resolve_Array_Aggregate;
1967 ---------------------------------
1968 -- Resolve_Extension_Aggregate --
1969 ---------------------------------
1971 -- There are two cases to consider:
1973 -- a) If the ancestor part is a type mark, the components needed are
1974 -- the difference between the components of the expected type and the
1975 -- components of the given type mark.
1977 -- b) If the ancestor part is an expression, it must be unambiguous,
1978 -- and once we have its type we can also compute the needed components
1979 -- as in the previous case. In both cases, if the ancestor type is not
1980 -- the immediate ancestor, we have to build this ancestor recursively.
1982 -- In both cases discriminants of the ancestor type do not play a
1983 -- role in the resolution of the needed components, because inherited
1984 -- discriminants cannot be used in a type extension. As a result we can
1985 -- compute independently the list of components of the ancestor type and
1986 -- of the expected type.
1988 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
1989 A : constant Node_Id := Ancestor_Part (N);
1990 A_Type : Entity_Id;
1991 I : Interp_Index;
1992 It : Interp;
1994 function Valid_Ancestor_Type return Boolean;
1995 -- Verify that the type of the ancestor part is a non-private ancestor
1996 -- of the expected type.
1998 -------------------------
1999 -- Valid_Ancestor_Type --
2000 -------------------------
2002 function Valid_Ancestor_Type return Boolean is
2003 Imm_Type : Entity_Id;
2005 begin
2006 Imm_Type := Base_Type (Typ);
2007 while Is_Derived_Type (Imm_Type)
2008 and then Etype (Imm_Type) /= Base_Type (A_Type)
2009 loop
2010 Imm_Type := Etype (Base_Type (Imm_Type));
2011 end loop;
2013 if Etype (Imm_Type) /= Base_Type (A_Type) then
2014 Error_Msg_NE ("expect ancestor type of &", A, Typ);
2015 return False;
2016 else
2017 return True;
2018 end if;
2019 end Valid_Ancestor_Type;
2021 -- Start of processing for Resolve_Extension_Aggregate
2023 begin
2024 Analyze (A);
2026 if not Is_Tagged_Type (Typ) then
2027 Error_Msg_N ("type of extension aggregate must be tagged", N);
2028 return;
2030 elsif Is_Limited_Type (Typ) then
2032 -- Ada 2005 (AI-287): Limited aggregates are allowed
2034 if Ada_Version < Ada_05 then
2035 Error_Msg_N ("aggregate type cannot be limited", N);
2036 Explain_Limited_Type (Typ, N);
2037 return;
2038 end if;
2040 elsif Is_Class_Wide_Type (Typ) then
2041 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
2042 return;
2043 end if;
2045 if Is_Entity_Name (A)
2046 and then Is_Type (Entity (A))
2047 then
2048 A_Type := Get_Full_View (Entity (A));
2050 if Valid_Ancestor_Type then
2051 Set_Entity (A, A_Type);
2052 Set_Etype (A, A_Type);
2054 Validate_Ancestor_Part (N);
2055 Resolve_Record_Aggregate (N, Typ);
2056 end if;
2058 elsif Nkind (A) /= N_Aggregate then
2059 if Is_Overloaded (A) then
2060 A_Type := Any_Type;
2061 Get_First_Interp (A, I, It);
2063 while Present (It.Typ) loop
2065 if Is_Tagged_Type (It.Typ)
2066 and then not Is_Limited_Type (It.Typ)
2067 then
2068 if A_Type /= Any_Type then
2069 Error_Msg_N ("cannot resolve expression", A);
2070 return;
2071 else
2072 A_Type := It.Typ;
2073 end if;
2074 end if;
2076 Get_Next_Interp (I, It);
2077 end loop;
2079 if A_Type = Any_Type then
2080 Error_Msg_N
2081 ("ancestor part must be non-limited tagged type", A);
2082 return;
2083 end if;
2085 else
2086 A_Type := Etype (A);
2087 end if;
2089 if Valid_Ancestor_Type then
2090 Resolve (A, A_Type);
2091 Check_Unset_Reference (A);
2092 Check_Non_Static_Context (A);
2094 if Is_Class_Wide_Type (Etype (A))
2095 and then Nkind (Original_Node (A)) = N_Function_Call
2096 then
2097 -- If the ancestor part is a dispatching call, it appears
2098 -- statically to be a legal ancestor, but it yields any
2099 -- member of the class, and it is not possible to determine
2100 -- whether it is an ancestor of the extension aggregate (much
2101 -- less which ancestor). It is not possible to determine the
2102 -- required components of the extension part.
2104 -- This check implements AI-306, which in fact was motivated
2105 -- by an ACT query to the ARG after this test was added.
2107 Error_Msg_N ("ancestor part must be statically tagged", A);
2108 else
2109 Resolve_Record_Aggregate (N, Typ);
2110 end if;
2111 end if;
2113 else
2114 Error_Msg_N (" No unique type for this aggregate", A);
2115 end if;
2116 end Resolve_Extension_Aggregate;
2118 ------------------------------
2119 -- Resolve_Record_Aggregate --
2120 ------------------------------
2122 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
2123 Assoc : Node_Id;
2124 -- N_Component_Association node belonging to the input aggregate N
2126 Expr : Node_Id;
2127 Positional_Expr : Node_Id;
2128 Component : Entity_Id;
2129 Component_Elmt : Elmt_Id;
2131 Components : constant Elist_Id := New_Elmt_List;
2132 -- Components is the list of the record components whose value must
2133 -- be provided in the aggregate. This list does include discriminants.
2135 New_Assoc_List : constant List_Id := New_List;
2136 New_Assoc : Node_Id;
2137 -- New_Assoc_List is the newly built list of N_Component_Association
2138 -- nodes. New_Assoc is one such N_Component_Association node in it.
2139 -- Please note that while Assoc and New_Assoc contain the same
2140 -- kind of nodes, they are used to iterate over two different
2141 -- N_Component_Association lists.
2143 Others_Etype : Entity_Id := Empty;
2144 -- This variable is used to save the Etype of the last record component
2145 -- that takes its value from the others choice. Its purpose is:
2147 -- (a) make sure the others choice is useful
2149 -- (b) make sure the type of all the components whose value is
2150 -- subsumed by the others choice are the same.
2152 -- This variable is updated as a side effect of function Get_Value
2154 Is_Box_Present : Boolean := False;
2155 Others_Box : Boolean := False;
2156 -- Ada 2005 (AI-287): Variables used in case of default initialization
2157 -- to provide a functionality similar to Others_Etype. Box_Present
2158 -- indicates that the component takes its default initialization;
2159 -- Others_Box indicates that at least one component takes its default
2160 -- initialization. Similar to Others_Etype, they are also updated as a
2161 -- side effect of function Get_Value.
2163 procedure Add_Association
2164 (Component : Entity_Id;
2165 Expr : Node_Id;
2166 Is_Box_Present : Boolean := False);
2167 -- Builds a new N_Component_Association node which associates
2168 -- Component to expression Expr and adds it to the new association
2169 -- list New_Assoc_List being built.
2171 function Discr_Present (Discr : Entity_Id) return Boolean;
2172 -- If aggregate N is a regular aggregate this routine will return True.
2173 -- Otherwise, if N is an extension aggregate, Discr is a discriminant
2174 -- whose value may already have been specified by N's ancestor part,
2175 -- this routine checks whether this is indeed the case and if so
2176 -- returns False, signaling that no value for Discr should appear in the
2177 -- N's aggregate part. Also, in this case, the routine appends to
2178 -- New_Assoc_List Discr the discriminant value specified in the ancestor
2179 -- part.
2181 function Get_Value
2182 (Compon : Node_Id;
2183 From : List_Id;
2184 Consider_Others_Choice : Boolean := False)
2185 return Node_Id;
2186 -- Given a record component stored in parameter Compon, the
2187 -- following function returns its value as it appears in the list
2188 -- From, which is a list of N_Component_Association nodes. If no
2189 -- component association has a choice for the searched component,
2190 -- the value provided by the others choice is returned, if there
2191 -- is one and Consider_Others_Choice is set to true. Otherwise
2192 -- Empty is returned. If there is more than one component association
2193 -- giving a value for the searched record component, an error message
2194 -- is emitted and the first found value is returned.
2196 -- If Consider_Others_Choice is set and the returned expression comes
2197 -- from the others choice, then Others_Etype is set as a side effect.
2198 -- An error message is emitted if the components taking their value
2199 -- from the others choice do not have same type.
2201 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id);
2202 -- Analyzes and resolves expression Expr against the Etype of the
2203 -- Component. This routine also applies all appropriate checks to Expr.
2204 -- It finally saves a Expr in the newly created association list that
2205 -- will be attached to the final record aggregate. Note that if the
2206 -- Parent pointer of Expr is not set then Expr was produced with a
2207 -- New_Copy_Tree or some such.
2209 ---------------------
2210 -- Add_Association --
2211 ---------------------
2213 procedure Add_Association
2214 (Component : Entity_Id;
2215 Expr : Node_Id;
2216 Is_Box_Present : Boolean := False)
2218 Choice_List : constant List_Id := New_List;
2219 New_Assoc : Node_Id;
2221 begin
2222 Append (New_Occurrence_Of (Component, Sloc (Expr)), Choice_List);
2223 New_Assoc :=
2224 Make_Component_Association (Sloc (Expr),
2225 Choices => Choice_List,
2226 Expression => Expr,
2227 Box_Present => Is_Box_Present);
2228 Append (New_Assoc, New_Assoc_List);
2229 end Add_Association;
2231 -------------------
2232 -- Discr_Present --
2233 -------------------
2235 function Discr_Present (Discr : Entity_Id) return Boolean is
2236 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
2238 Loc : Source_Ptr;
2240 Ancestor : Node_Id;
2241 Discr_Expr : Node_Id;
2243 Ancestor_Typ : Entity_Id;
2244 Orig_Discr : Entity_Id;
2245 D : Entity_Id;
2246 D_Val : Elmt_Id := No_Elmt; -- stop junk warning
2248 Ancestor_Is_Subtyp : Boolean;
2250 begin
2251 if Regular_Aggr then
2252 return True;
2253 end if;
2255 Ancestor := Ancestor_Part (N);
2256 Ancestor_Typ := Etype (Ancestor);
2257 Loc := Sloc (Ancestor);
2259 Ancestor_Is_Subtyp :=
2260 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
2262 -- If the ancestor part has no discriminants clearly N's aggregate
2263 -- part must provide a value for Discr.
2265 if not Has_Discriminants (Ancestor_Typ) then
2266 return True;
2268 -- If the ancestor part is an unconstrained subtype mark then the
2269 -- Discr must be present in N's aggregate part.
2271 elsif Ancestor_Is_Subtyp
2272 and then not Is_Constrained (Entity (Ancestor))
2273 then
2274 return True;
2275 end if;
2277 -- Now look to see if Discr was specified in the ancestor part
2279 if Ancestor_Is_Subtyp then
2280 D_Val := First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
2281 end if;
2283 Orig_Discr := Original_Record_Component (Discr);
2285 D := First_Discriminant (Ancestor_Typ);
2286 while Present (D) loop
2288 -- If Ancestor has already specified Disc value than insert its
2289 -- value in the final aggregate.
2291 if Original_Record_Component (D) = Orig_Discr then
2292 if Ancestor_Is_Subtyp then
2293 Discr_Expr := New_Copy_Tree (Node (D_Val));
2294 else
2295 Discr_Expr :=
2296 Make_Selected_Component (Loc,
2297 Prefix => Duplicate_Subexpr (Ancestor),
2298 Selector_Name => New_Occurrence_Of (Discr, Loc));
2299 end if;
2301 Resolve_Aggr_Expr (Discr_Expr, Discr);
2302 return False;
2303 end if;
2305 Next_Discriminant (D);
2307 if Ancestor_Is_Subtyp then
2308 Next_Elmt (D_Val);
2309 end if;
2310 end loop;
2312 return True;
2313 end Discr_Present;
2315 ---------------
2316 -- Get_Value --
2317 ---------------
2319 function Get_Value
2320 (Compon : Node_Id;
2321 From : List_Id;
2322 Consider_Others_Choice : Boolean := False)
2323 return Node_Id
2325 Assoc : Node_Id;
2326 Expr : Node_Id := Empty;
2327 Selector_Name : Node_Id;
2329 procedure Check_Non_Limited_Type;
2330 -- Relax check to allow the default initialization of limited types.
2331 -- For example:
2332 -- record
2333 -- C : Lim := (..., others => <>);
2334 -- end record;
2336 ----------------------------
2337 -- Check_Non_Limited_Type --
2338 ----------------------------
2340 procedure Check_Non_Limited_Type is
2341 begin
2342 if Is_Limited_Type (Etype (Compon))
2343 and then Comes_From_Source (Compon)
2344 and then not In_Instance_Body
2345 then
2346 -- Ada 2005 (AI-287): Limited aggregates are allowed
2348 if Ada_Version >= Ada_05
2349 and then Present (Expression (Assoc))
2350 and then Nkind (Expression (Assoc)) = N_Aggregate
2351 then
2352 null;
2353 else
2354 Error_Msg_N
2355 ("initialization not allowed for limited types", N);
2356 Explain_Limited_Type (Etype (Compon), Compon);
2357 end if;
2358 end if;
2359 end Check_Non_Limited_Type;
2361 -- Start of processing for Get_Value
2363 begin
2364 Is_Box_Present := False;
2366 if Present (From) then
2367 Assoc := First (From);
2368 else
2369 return Empty;
2370 end if;
2372 while Present (Assoc) loop
2373 Selector_Name := First (Choices (Assoc));
2374 while Present (Selector_Name) loop
2375 if Nkind (Selector_Name) = N_Others_Choice then
2376 if Consider_Others_Choice and then No (Expr) then
2378 -- We need to duplicate the expression for each
2379 -- successive component covered by the others choice.
2380 -- This is redundant if the others_choice covers only
2381 -- one component (small optimization possible???), but
2382 -- indispensable otherwise, because each one must be
2383 -- expanded individually to preserve side-effects.
2385 -- Ada 2005 (AI-287): In case of default initialization
2386 -- of components, we duplicate the corresponding default
2387 -- expression (from the record type declaration).
2389 if Box_Present (Assoc) then
2390 Others_Box := True;
2391 Is_Box_Present := True;
2393 if Expander_Active then
2394 return New_Copy_Tree (Expression (Parent (Compon)));
2395 else
2396 return Expression (Parent (Compon));
2397 end if;
2399 else
2400 Check_Non_Limited_Type;
2402 if Present (Others_Etype) and then
2403 Base_Type (Others_Etype) /= Base_Type (Etype
2404 (Compon))
2405 then
2406 Error_Msg_N ("components in OTHERS choice must " &
2407 "have same type", Selector_Name);
2408 end if;
2410 Others_Etype := Etype (Compon);
2412 if Expander_Active then
2413 return New_Copy_Tree (Expression (Assoc));
2414 else
2415 return Expression (Assoc);
2416 end if;
2417 end if;
2418 end if;
2420 elsif Chars (Compon) = Chars (Selector_Name) then
2421 if No (Expr) then
2423 -- Ada 2005 (AI-231)
2425 if Ada_Version >= Ada_05
2426 and then Nkind (Expression (Assoc)) = N_Null
2427 then
2428 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
2429 end if;
2431 -- We need to duplicate the expression when several
2432 -- components are grouped together with a "|" choice.
2433 -- For instance "filed1 | filed2 => Expr"
2435 -- Ada 2005 (AI-287)
2437 if Box_Present (Assoc) then
2438 Is_Box_Present := True;
2440 -- Duplicate the default expression of the component
2441 -- from the record type declaration
2443 if Present (Next (Selector_Name)) then
2444 Expr :=
2445 New_Copy_Tree (Expression (Parent (Compon)));
2446 else
2447 Expr := Expression (Parent (Compon));
2448 end if;
2450 else
2451 Check_Non_Limited_Type;
2453 if Present (Next (Selector_Name)) then
2454 Expr := New_Copy_Tree (Expression (Assoc));
2455 else
2456 Expr := Expression (Assoc);
2457 end if;
2458 end if;
2460 Generate_Reference (Compon, Selector_Name);
2462 else
2463 Error_Msg_NE
2464 ("more than one value supplied for &",
2465 Selector_Name, Compon);
2467 end if;
2468 end if;
2470 Next (Selector_Name);
2471 end loop;
2473 Next (Assoc);
2474 end loop;
2476 return Expr;
2477 end Get_Value;
2479 -----------------------
2480 -- Resolve_Aggr_Expr --
2481 -----------------------
2483 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id) is
2484 New_C : Entity_Id := Component;
2485 Expr_Type : Entity_Id := Empty;
2487 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
2488 -- If the expression is an aggregate (possibly qualified) then its
2489 -- expansion is delayed until the enclosing aggregate is expanded
2490 -- into assignments. In that case, do not generate checks on the
2491 -- expression, because they will be generated later, and will other-
2492 -- wise force a copy (to remove side-effects) that would leave a
2493 -- dynamic-sized aggregate in the code, something that gigi cannot
2494 -- handle.
2496 Relocate : Boolean;
2497 -- Set to True if the resolved Expr node needs to be relocated
2498 -- when attached to the newly created association list. This node
2499 -- need not be relocated if its parent pointer is not set.
2500 -- In fact in this case Expr is the output of a New_Copy_Tree call.
2501 -- if Relocate is True then we have analyzed the expression node
2502 -- in the original aggregate and hence it needs to be relocated
2503 -- when moved over the new association list.
2505 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
2506 Kind : constant Node_Kind := Nkind (Expr);
2508 begin
2509 return ((Kind = N_Aggregate
2510 or else Kind = N_Extension_Aggregate)
2511 and then Present (Etype (Expr))
2512 and then Is_Record_Type (Etype (Expr))
2513 and then Expansion_Delayed (Expr))
2515 or else (Kind = N_Qualified_Expression
2516 and then Has_Expansion_Delayed (Expression (Expr)));
2517 end Has_Expansion_Delayed;
2519 -- Start of processing for Resolve_Aggr_Expr
2521 begin
2522 -- If the type of the component is elementary or the type of the
2523 -- aggregate does not contain discriminants, use the type of the
2524 -- component to resolve Expr.
2526 if Is_Elementary_Type (Etype (Component))
2527 or else not Has_Discriminants (Etype (N))
2528 then
2529 Expr_Type := Etype (Component);
2531 -- Otherwise we have to pick up the new type of the component from
2532 -- the new costrained subtype of the aggregate. In fact components
2533 -- which are of a composite type might be constrained by a
2534 -- discriminant, and we want to resolve Expr against the subtype were
2535 -- all discriminant occurrences are replaced with their actual value.
2537 else
2538 New_C := First_Component (Etype (N));
2539 while Present (New_C) loop
2540 if Chars (New_C) = Chars (Component) then
2541 Expr_Type := Etype (New_C);
2542 exit;
2543 end if;
2545 Next_Component (New_C);
2546 end loop;
2548 pragma Assert (Present (Expr_Type));
2550 -- For each range in an array type where a discriminant has been
2551 -- replaced with the constraint, check that this range is within
2552 -- the range of the base type. This checks is done in the init
2553 -- proc for regular objects, but has to be done here for
2554 -- aggregates since no init proc is called for them.
2556 if Is_Array_Type (Expr_Type) then
2557 declare
2558 Index : Node_Id := First_Index (Expr_Type);
2559 -- Range of the current constrained index in the array
2561 Orig_Index : Node_Id := First_Index (Etype (Component));
2562 -- Range corresponding to the range Index above in the
2563 -- original unconstrained record type. The bounds of this
2564 -- range may be governed by discriminants.
2566 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
2567 -- Range corresponding to the range Index above for the
2568 -- unconstrained array type. This range is needed to apply
2569 -- range checks.
2571 begin
2572 while Present (Index) loop
2573 if Depends_On_Discriminant (Orig_Index) then
2574 Apply_Range_Check (Index, Etype (Unconstr_Index));
2575 end if;
2577 Next_Index (Index);
2578 Next_Index (Orig_Index);
2579 Next_Index (Unconstr_Index);
2580 end loop;
2581 end;
2582 end if;
2583 end if;
2585 -- If the Parent pointer of Expr is not set, Expr is an expression
2586 -- duplicated by New_Tree_Copy (this happens for record aggregates
2587 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
2588 -- Such a duplicated expression must be attached to the tree
2589 -- before analysis and resolution to enforce the rule that a tree
2590 -- fragment should never be analyzed or resolved unless it is
2591 -- attached to the current compilation unit.
2593 if No (Parent (Expr)) then
2594 Set_Parent (Expr, N);
2595 Relocate := False;
2596 else
2597 Relocate := True;
2598 end if;
2600 Analyze_And_Resolve (Expr, Expr_Type);
2601 Check_Non_Static_Context (Expr);
2602 Check_Unset_Reference (Expr);
2604 if not Has_Expansion_Delayed (Expr) then
2605 Aggregate_Constraint_Checks (Expr, Expr_Type);
2606 end if;
2608 if Raises_Constraint_Error (Expr) then
2609 Set_Raises_Constraint_Error (N);
2610 end if;
2612 if Relocate then
2613 Add_Association (New_C, Relocate_Node (Expr));
2614 else
2615 Add_Association (New_C, Expr);
2616 end if;
2617 end Resolve_Aggr_Expr;
2619 -- Start of processing for Resolve_Record_Aggregate
2621 begin
2622 -- We may end up calling Duplicate_Subexpr on expressions that are
2623 -- attached to New_Assoc_List. For this reason we need to attach it
2624 -- to the tree by setting its parent pointer to N. This parent point
2625 -- will change in STEP 8 below.
2627 Set_Parent (New_Assoc_List, N);
2629 -- STEP 1: abstract type and null record verification
2631 if Is_Abstract (Typ) then
2632 Error_Msg_N ("type of aggregate cannot be abstract", N);
2633 end if;
2635 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
2636 Set_Etype (N, Typ);
2637 return;
2639 elsif Present (First_Entity (Typ))
2640 and then Null_Record_Present (N)
2641 and then not Is_Tagged_Type (Typ)
2642 then
2643 Error_Msg_N ("record aggregate cannot be null", N);
2644 return;
2646 elsif No (First_Entity (Typ)) then
2647 Error_Msg_N ("record aggregate must be null", N);
2648 return;
2649 end if;
2651 -- STEP 2: Verify aggregate structure
2653 Step_2 : declare
2654 Selector_Name : Node_Id;
2655 Bad_Aggregate : Boolean := False;
2657 begin
2658 if Present (Component_Associations (N)) then
2659 Assoc := First (Component_Associations (N));
2660 else
2661 Assoc := Empty;
2662 end if;
2664 while Present (Assoc) loop
2665 Selector_Name := First (Choices (Assoc));
2666 while Present (Selector_Name) loop
2667 if Nkind (Selector_Name) = N_Identifier then
2668 null;
2670 elsif Nkind (Selector_Name) = N_Others_Choice then
2671 if Selector_Name /= First (Choices (Assoc))
2672 or else Present (Next (Selector_Name))
2673 then
2674 Error_Msg_N ("OTHERS must appear alone in a choice list",
2675 Selector_Name);
2676 return;
2678 elsif Present (Next (Assoc)) then
2679 Error_Msg_N ("OTHERS must appear last in an aggregate",
2680 Selector_Name);
2681 return;
2682 end if;
2684 else
2685 Error_Msg_N
2686 ("selector name should be identifier or OTHERS",
2687 Selector_Name);
2688 Bad_Aggregate := True;
2689 end if;
2691 Next (Selector_Name);
2692 end loop;
2694 Next (Assoc);
2695 end loop;
2697 if Bad_Aggregate then
2698 return;
2699 end if;
2700 end Step_2;
2702 -- STEP 3: Find discriminant Values
2704 Step_3 : declare
2705 Discrim : Entity_Id;
2706 Missing_Discriminants : Boolean := False;
2708 begin
2709 if Present (Expressions (N)) then
2710 Positional_Expr := First (Expressions (N));
2711 else
2712 Positional_Expr := Empty;
2713 end if;
2715 if Has_Discriminants (Typ) then
2716 Discrim := First_Discriminant (Typ);
2717 else
2718 Discrim := Empty;
2719 end if;
2721 -- First find the discriminant values in the positional components
2723 while Present (Discrim) and then Present (Positional_Expr) loop
2724 if Discr_Present (Discrim) then
2725 Resolve_Aggr_Expr (Positional_Expr, Discrim);
2727 -- Ada 2005 (AI-231)
2729 if Ada_Version >= Ada_05
2730 and then Nkind (Positional_Expr) = N_Null
2731 then
2732 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
2733 end if;
2735 Next (Positional_Expr);
2736 end if;
2738 if Present (Get_Value (Discrim, Component_Associations (N))) then
2739 Error_Msg_NE
2740 ("more than one value supplied for discriminant&",
2741 N, Discrim);
2742 end if;
2744 Next_Discriminant (Discrim);
2745 end loop;
2747 -- Find remaining discriminant values, if any, among named components
2749 while Present (Discrim) loop
2750 Expr := Get_Value (Discrim, Component_Associations (N), True);
2752 if not Discr_Present (Discrim) then
2753 if Present (Expr) then
2754 Error_Msg_NE
2755 ("more than one value supplied for discriminant&",
2756 N, Discrim);
2757 end if;
2759 elsif No (Expr) then
2760 Error_Msg_NE
2761 ("no value supplied for discriminant &", N, Discrim);
2762 Missing_Discriminants := True;
2764 else
2765 Resolve_Aggr_Expr (Expr, Discrim);
2766 end if;
2768 Next_Discriminant (Discrim);
2769 end loop;
2771 if Missing_Discriminants then
2772 return;
2773 end if;
2775 -- At this point and until the beginning of STEP 6, New_Assoc_List
2776 -- contains only the discriminants and their values.
2778 end Step_3;
2780 -- STEP 4: Set the Etype of the record aggregate
2782 -- ??? This code is pretty much a copy of Sem_Ch3.Build_Subtype. That
2783 -- routine should really be exported in sem_util or some such and used
2784 -- in sem_ch3 and here rather than have a copy of the code which is a
2785 -- maintenance nightmare.
2787 -- ??? Performace WARNING. The current implementation creates a new
2788 -- itype for all aggregates whose base type is discriminated.
2789 -- This means that for record aggregates nested inside an array
2790 -- aggregate we will create a new itype for each record aggregate
2791 -- if the array cmponent type has discriminants. For large aggregates
2792 -- this may be a problem. What should be done in this case is
2793 -- to reuse itypes as much as possible.
2795 if Has_Discriminants (Typ) then
2796 Build_Constrained_Itype : declare
2797 Loc : constant Source_Ptr := Sloc (N);
2798 Indic : Node_Id;
2799 Subtyp_Decl : Node_Id;
2800 Def_Id : Entity_Id;
2802 C : constant List_Id := New_List;
2804 begin
2805 New_Assoc := First (New_Assoc_List);
2806 while Present (New_Assoc) loop
2807 Append (Duplicate_Subexpr (Expression (New_Assoc)), To => C);
2808 Next (New_Assoc);
2809 end loop;
2811 Indic :=
2812 Make_Subtype_Indication (Loc,
2813 Subtype_Mark => New_Occurrence_Of (Base_Type (Typ), Loc),
2814 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
2816 Def_Id := Create_Itype (Ekind (Typ), N);
2818 Subtyp_Decl :=
2819 Make_Subtype_Declaration (Loc,
2820 Defining_Identifier => Def_Id,
2821 Subtype_Indication => Indic);
2822 Set_Parent (Subtyp_Decl, Parent (N));
2824 -- Itypes must be analyzed with checks off (see itypes.ads)
2826 Analyze (Subtyp_Decl, Suppress => All_Checks);
2828 Set_Etype (N, Def_Id);
2829 Check_Static_Discriminated_Subtype
2830 (Def_Id, Expression (First (New_Assoc_List)));
2831 end Build_Constrained_Itype;
2833 else
2834 Set_Etype (N, Typ);
2835 end if;
2837 -- STEP 5: Get remaining components according to discriminant values
2839 Step_5 : declare
2840 Record_Def : Node_Id;
2841 Parent_Typ : Entity_Id;
2842 Root_Typ : Entity_Id;
2843 Parent_Typ_List : Elist_Id;
2844 Parent_Elmt : Elmt_Id;
2845 Errors_Found : Boolean := False;
2846 Dnode : Node_Id;
2848 begin
2849 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
2850 Parent_Typ_List := New_Elmt_List;
2852 -- If this is an extension aggregate, the component list must
2853 -- include all components that are not in the given ancestor
2854 -- type. Otherwise, the component list must include components
2855 -- of all ancestors, starting with the root.
2857 if Nkind (N) = N_Extension_Aggregate then
2858 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
2859 else
2860 Root_Typ := Root_Type (Typ);
2862 if Nkind (Parent (Base_Type (Root_Typ)))
2863 = N_Private_Type_Declaration
2864 then
2865 Error_Msg_NE
2866 ("type of aggregate has private ancestor&!",
2867 N, Root_Typ);
2868 Error_Msg_N ("must use extension aggregate!", N);
2869 return;
2870 end if;
2872 Dnode := Declaration_Node (Base_Type (Root_Typ));
2874 -- If we don't get a full declaration, then we have some
2875 -- error which will get signalled later so skip this part.
2876 -- Otherwise, gather components of root that apply to the
2877 -- aggregate type. We use the base type in case there is an
2878 -- applicable stored constraint that renames the discriminants
2879 -- of the root.
2881 if Nkind (Dnode) = N_Full_Type_Declaration then
2882 Record_Def := Type_Definition (Dnode);
2883 Gather_Components (Base_Type (Typ),
2884 Component_List (Record_Def),
2885 Governed_By => New_Assoc_List,
2886 Into => Components,
2887 Report_Errors => Errors_Found);
2888 end if;
2889 end if;
2891 Parent_Typ := Base_Type (Typ);
2892 while Parent_Typ /= Root_Typ loop
2894 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
2895 Parent_Typ := Etype (Parent_Typ);
2897 if Nkind (Parent (Base_Type (Parent_Typ))) =
2898 N_Private_Type_Declaration
2899 or else Nkind (Parent (Base_Type (Parent_Typ))) =
2900 N_Private_Extension_Declaration
2901 then
2902 if Nkind (N) /= N_Extension_Aggregate then
2903 Error_Msg_NE
2904 ("type of aggregate has private ancestor&!",
2905 N, Parent_Typ);
2906 Error_Msg_N ("must use extension aggregate!", N);
2907 return;
2909 elsif Parent_Typ /= Root_Typ then
2910 Error_Msg_NE
2911 ("ancestor part of aggregate must be private type&",
2912 Ancestor_Part (N), Parent_Typ);
2913 return;
2914 end if;
2915 end if;
2916 end loop;
2918 -- Now collect components from all other ancestors
2920 Parent_Elmt := First_Elmt (Parent_Typ_List);
2921 while Present (Parent_Elmt) loop
2922 Parent_Typ := Node (Parent_Elmt);
2923 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
2924 Gather_Components (Empty,
2925 Component_List (Record_Extension_Part (Record_Def)),
2926 Governed_By => New_Assoc_List,
2927 Into => Components,
2928 Report_Errors => Errors_Found);
2930 Next_Elmt (Parent_Elmt);
2931 end loop;
2933 else
2934 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
2936 if Null_Present (Record_Def) then
2937 null;
2938 else
2939 Gather_Components (Base_Type (Typ),
2940 Component_List (Record_Def),
2941 Governed_By => New_Assoc_List,
2942 Into => Components,
2943 Report_Errors => Errors_Found);
2944 end if;
2945 end if;
2947 if Errors_Found then
2948 return;
2949 end if;
2950 end Step_5;
2952 -- STEP 6: Find component Values
2954 Component := Empty;
2955 Component_Elmt := First_Elmt (Components);
2957 -- First scan the remaining positional associations in the aggregate.
2958 -- Remember that at this point Positional_Expr contains the current
2959 -- positional association if any is left after looking for discriminant
2960 -- values in step 3.
2962 while Present (Positional_Expr) and then Present (Component_Elmt) loop
2963 Component := Node (Component_Elmt);
2964 Resolve_Aggr_Expr (Positional_Expr, Component);
2966 -- Ada 2005 (AI-231)
2968 if Ada_Version >= Ada_05
2969 and then Nkind (Positional_Expr) = N_Null
2970 then
2971 Check_Can_Never_Be_Null (Component, Positional_Expr);
2972 end if;
2974 if Present (Get_Value (Component, Component_Associations (N))) then
2975 Error_Msg_NE
2976 ("more than one value supplied for Component &", N, Component);
2977 end if;
2979 Next (Positional_Expr);
2980 Next_Elmt (Component_Elmt);
2981 end loop;
2983 if Present (Positional_Expr) then
2984 Error_Msg_N
2985 ("too many components for record aggregate", Positional_Expr);
2986 end if;
2988 -- Now scan for the named arguments of the aggregate
2990 while Present (Component_Elmt) loop
2991 Component := Node (Component_Elmt);
2992 Expr := Get_Value (Component, Component_Associations (N), True);
2994 -- Note: The previous call to Get_Value sets the value of the
2995 -- variable Is_Box_Present
2997 -- Ada 2005 (AI-287): Handle components with default initialization.
2998 -- Note: This feature was originally added to Ada 2005 for limited
2999 -- but it was finally allowed with any type.
3001 if Is_Box_Present then
3002 declare
3003 Is_Array_Subtype : constant Boolean :=
3004 Ekind (Etype (Component)) =
3005 E_Array_Subtype;
3007 Ctyp : Entity_Id;
3009 begin
3010 if Is_Array_Subtype then
3011 Ctyp := Component_Type (Base_Type (Etype (Component)));
3012 else
3013 Ctyp := Etype (Component);
3014 end if;
3016 -- If the component has an initialization procedure (IP) we
3017 -- pass the component to the expander, which will generate
3018 -- the call to such IP.
3020 if Has_Non_Null_Base_Init_Proc (Ctyp) then
3021 Add_Association
3022 (Component => Component,
3023 Expr => Empty,
3024 Is_Box_Present => True);
3026 -- Otherwise we only need to resolve the expression if the
3027 -- component has partially initialized values (required to
3028 -- expand the corresponding assignments and run-time checks).
3030 elsif Present (Expr)
3031 and then
3032 ((not Is_Array_Subtype
3033 and then Is_Partially_Initialized_Type (Component))
3034 or else
3035 (Is_Array_Subtype
3036 and then Is_Partially_Initialized_Type (Ctyp)))
3037 then
3038 Resolve_Aggr_Expr (Expr, Component);
3039 end if;
3040 end;
3042 elsif No (Expr) then
3043 Error_Msg_NE ("no value supplied for component &!", N, Component);
3045 else
3046 Resolve_Aggr_Expr (Expr, Component);
3047 end if;
3049 Next_Elmt (Component_Elmt);
3050 end loop;
3052 -- STEP 7: check for invalid components + check type in choice list
3054 Step_7 : declare
3055 Selectr : Node_Id;
3056 -- Selector name
3058 Typech : Entity_Id;
3059 -- Type of first component in choice list
3061 begin
3062 if Present (Component_Associations (N)) then
3063 Assoc := First (Component_Associations (N));
3064 else
3065 Assoc := Empty;
3066 end if;
3068 Verification : while Present (Assoc) loop
3069 Selectr := First (Choices (Assoc));
3070 Typech := Empty;
3072 if Nkind (Selectr) = N_Others_Choice then
3074 -- Ada 2005 (AI-287): others choice may have expression or box
3076 if No (Others_Etype)
3077 and then not Others_Box
3078 then
3079 Error_Msg_N
3080 ("OTHERS must represent at least one component", Selectr);
3081 end if;
3083 exit Verification;
3084 end if;
3086 while Present (Selectr) loop
3087 New_Assoc := First (New_Assoc_List);
3088 while Present (New_Assoc) loop
3089 Component := First (Choices (New_Assoc));
3090 exit when Chars (Selectr) = Chars (Component);
3091 Next (New_Assoc);
3092 end loop;
3094 -- If no association, this is not a legal component of
3095 -- of the type in question, except if this is an internal
3096 -- component supplied by a previous expansion.
3098 if No (New_Assoc) then
3099 if Box_Present (Parent (Selectr)) then
3100 null;
3102 elsif Chars (Selectr) /= Name_uTag
3103 and then Chars (Selectr) /= Name_uParent
3104 and then Chars (Selectr) /= Name_uController
3105 then
3106 if not Has_Discriminants (Typ) then
3107 Error_Msg_Node_2 := Typ;
3108 Error_Msg_N
3109 ("& is not a component of}",
3110 Selectr);
3111 else
3112 Error_Msg_N
3113 ("& is not a component of the aggregate subtype",
3114 Selectr);
3115 end if;
3117 Check_Misspelled_Component (Components, Selectr);
3118 end if;
3120 elsif No (Typech) then
3121 Typech := Base_Type (Etype (Component));
3123 elsif Typech /= Base_Type (Etype (Component)) then
3124 if not Box_Present (Parent (Selectr)) then
3125 Error_Msg_N
3126 ("components in choice list must have same type",
3127 Selectr);
3128 end if;
3129 end if;
3131 Next (Selectr);
3132 end loop;
3134 Next (Assoc);
3135 end loop Verification;
3136 end Step_7;
3138 -- STEP 8: replace the original aggregate
3140 Step_8 : declare
3141 New_Aggregate : constant Node_Id := New_Copy (N);
3143 begin
3144 Set_Expressions (New_Aggregate, No_List);
3145 Set_Etype (New_Aggregate, Etype (N));
3146 Set_Component_Associations (New_Aggregate, New_Assoc_List);
3148 Rewrite (N, New_Aggregate);
3149 end Step_8;
3150 end Resolve_Record_Aggregate;
3152 -----------------------------
3153 -- Check_Can_Never_Be_Null --
3154 -----------------------------
3156 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
3157 Comp_Typ : Entity_Id;
3159 begin
3160 pragma Assert
3161 (Ada_Version >= Ada_05
3162 and then Present (Expr)
3163 and then Nkind (Expr) = N_Null);
3165 case Ekind (Typ) is
3166 when E_Array_Type =>
3167 Comp_Typ := Component_Type (Typ);
3169 when E_Component |
3170 E_Discriminant =>
3171 Comp_Typ := Etype (Typ);
3173 when others =>
3174 return;
3175 end case;
3177 if Can_Never_Be_Null (Comp_Typ) then
3179 -- Here we know we have a constraint error. Note that we do not use
3180 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
3181 -- seem the more natural approach. That's because in some cases the
3182 -- components are rewritten, and the replacement would be missed.
3184 Insert_Action
3185 (Compile_Time_Constraint_Error
3186 (Expr,
3187 "(Ada 2005) NULL not allowed in null-excluding components?"),
3188 Make_Raise_Constraint_Error (Sloc (Expr),
3189 Reason => CE_Access_Check_Failed));
3191 -- Set proper type for bogus component (why is this needed???)
3193 Set_Etype (Expr, Comp_Typ);
3194 Set_Analyzed (Expr);
3195 end if;
3196 end Check_Can_Never_Be_Null;
3198 ---------------------
3199 -- Sort_Case_Table --
3200 ---------------------
3202 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
3203 L : constant Int := Case_Table'First;
3204 U : constant Int := Case_Table'Last;
3205 K : Int;
3206 J : Int;
3207 T : Case_Bounds;
3209 begin
3210 K := L;
3212 while K /= U loop
3213 T := Case_Table (K + 1);
3214 J := K + 1;
3216 while J /= L
3217 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
3218 Expr_Value (T.Choice_Lo)
3219 loop
3220 Case_Table (J) := Case_Table (J - 1);
3221 J := J - 1;
3222 end loop;
3224 Case_Table (J) := T;
3225 K := K + 1;
3226 end loop;
3227 end Sort_Case_Table;
3229 end Sem_Aggr;