2016-04-21 Javier Miranda <miranda@adacore.com>
[official-gcc.git] / gcc / ada / exp_aggr.adb
blob6d6e1a2526312e2c45c5e6108304052c1c84a2bb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ A G G R --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2016, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Exp_Util; use Exp_Util;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Ch9; use Exp_Ch9;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Tss; use Exp_Tss;
40 with Fname; use Fname;
41 with Freeze; use Freeze;
42 with Itypes; use Itypes;
43 with Lib; use Lib;
44 with Namet; use Namet;
45 with Nmake; use Nmake;
46 with Nlists; use Nlists;
47 with Opt; use Opt;
48 with Restrict; use Restrict;
49 with Rident; use Rident;
50 with Rtsfind; use Rtsfind;
51 with Ttypes; use Ttypes;
52 with Sem; use Sem;
53 with Sem_Aggr; use Sem_Aggr;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Ch3; use Sem_Ch3;
56 with Sem_Eval; use Sem_Eval;
57 with Sem_Res; use Sem_Res;
58 with Sem_Util; use Sem_Util;
59 with Sinfo; use Sinfo;
60 with Snames; use Snames;
61 with Stand; use Stand;
62 with Stringt; use Stringt;
63 with Targparm; use Targparm;
64 with Tbuild; use Tbuild;
65 with Uintp; use Uintp;
67 package body Exp_Aggr is
69 type Case_Bounds is record
70 Choice_Lo : Node_Id;
71 Choice_Hi : Node_Id;
72 Choice_Node : Node_Id;
73 end record;
75 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
76 -- Table type used by Check_Case_Choices procedure
78 procedure Collect_Initialization_Statements
79 (Obj : Entity_Id;
80 N : Node_Id;
81 Node_After : Node_Id);
82 -- If Obj is not frozen, collect actions inserted after N until, but not
83 -- including, Node_After, for initialization of Obj, and move them to an
84 -- expression with actions, which becomes the Initialization_Statements for
85 -- Obj.
87 function Has_Default_Init_Comps (N : Node_Id) return Boolean;
88 -- N is an aggregate (record or array). Checks the presence of default
89 -- initialization (<>) in any component (Ada 2005: AI-287).
91 function In_Object_Declaration (N : Node_Id) return Boolean;
92 -- Return True if N is part of an object declaration, False otherwise
94 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean;
95 -- Returns true if N is an aggregate used to initialize the components
96 -- of a statically allocated dispatch table.
98 function Must_Slide
99 (Obj_Type : Entity_Id;
100 Typ : Entity_Id) return Boolean;
101 -- A static array aggregate in an object declaration can in most cases be
102 -- expanded in place. The one exception is when the aggregate is given
103 -- with component associations that specify different bounds from those of
104 -- the type definition in the object declaration. In this pathological
105 -- case the aggregate must slide, and we must introduce an intermediate
106 -- temporary to hold it.
108 -- The same holds in an assignment to one-dimensional array of arrays,
109 -- when a component may be given with bounds that differ from those of the
110 -- component type.
112 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
113 -- Sort the Case Table using the Lower Bound of each Choice as the key.
114 -- A simple insertion sort is used since the number of choices in a case
115 -- statement of variant part will usually be small and probably in near
116 -- sorted order.
118 ------------------------------------------------------
119 -- Local subprograms for Record Aggregate Expansion --
120 ------------------------------------------------------
122 function Build_Record_Aggr_Code
123 (N : Node_Id;
124 Typ : Entity_Id;
125 Lhs : Node_Id) return List_Id;
126 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
127 -- aggregate. Target is an expression containing the location on which the
128 -- component by component assignments will take place. Returns the list of
129 -- assignments plus all other adjustments needed for tagged and controlled
130 -- types.
132 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id);
133 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
134 -- aggregate (which can only be a record type, this procedure is only used
135 -- for record types). Transform the given aggregate into a sequence of
136 -- assignments performed component by component.
138 procedure Expand_Record_Aggregate
139 (N : Node_Id;
140 Orig_Tag : Node_Id := Empty;
141 Parent_Expr : Node_Id := Empty);
142 -- This is the top level procedure for record aggregate expansion.
143 -- Expansion for record aggregates needs expand aggregates for tagged
144 -- record types. Specifically Expand_Record_Aggregate adds the Tag
145 -- field in front of the Component_Association list that was created
146 -- during resolution by Resolve_Record_Aggregate.
148 -- N is the record aggregate node.
149 -- Orig_Tag is the value of the Tag that has to be provided for this
150 -- specific aggregate. It carries the tag corresponding to the type
151 -- of the outermost aggregate during the recursive expansion
152 -- Parent_Expr is the ancestor part of the original extension
153 -- aggregate
155 function Has_Mutable_Components (Typ : Entity_Id) return Boolean;
156 -- Return true if one of the components is of a discriminated type with
157 -- defaults. An aggregate for a type with mutable components must be
158 -- expanded into individual assignments.
160 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id);
161 -- If the type of the aggregate is a type extension with renamed discrimi-
162 -- nants, we must initialize the hidden discriminants of the parent.
163 -- Otherwise, the target object must not be initialized. The discriminants
164 -- are initialized by calling the initialization procedure for the type.
165 -- This is incorrect if the initialization of other components has any
166 -- side effects. We restrict this call to the case where the parent type
167 -- has a variant part, because this is the only case where the hidden
168 -- discriminants are accessed, namely when calling discriminant checking
169 -- functions of the parent type, and when applying a stream attribute to
170 -- an object of the derived type.
172 -----------------------------------------------------
173 -- Local Subprograms for Array Aggregate Expansion --
174 -----------------------------------------------------
176 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean;
177 -- Very large static aggregates present problems to the back-end, and are
178 -- transformed into assignments and loops. This function verifies that the
179 -- total number of components of an aggregate is acceptable for rewriting
180 -- into a purely positional static form. Aggr_Size_OK must be called before
181 -- calling Flatten.
183 -- This function also detects and warns about one-component aggregates that
184 -- appear in a non-static context. Even if the component value is static,
185 -- such an aggregate must be expanded into an assignment.
187 function Backend_Processing_Possible (N : Node_Id) return Boolean;
188 -- This function checks if array aggregate N can be processed directly
189 -- by the backend. If this is the case, True is returned.
191 function Build_Array_Aggr_Code
192 (N : Node_Id;
193 Ctype : Entity_Id;
194 Index : Node_Id;
195 Into : Node_Id;
196 Scalar_Comp : Boolean;
197 Indexes : List_Id := No_List) return List_Id;
198 -- This recursive routine returns a list of statements containing the
199 -- loops and assignments that are needed for the expansion of the array
200 -- aggregate N.
202 -- N is the (sub-)aggregate node to be expanded into code. This node has
203 -- been fully analyzed, and its Etype is properly set.
205 -- Index is the index node corresponding to the array subaggregate N
207 -- Into is the target expression into which we are copying the aggregate.
208 -- Note that this node may not have been analyzed yet, and so the Etype
209 -- field may not be set.
211 -- Scalar_Comp is True if the component type of the aggregate is scalar
213 -- Indexes is the current list of expressions used to index the object we
214 -- are writing into.
216 procedure Convert_Array_Aggr_In_Allocator
217 (Decl : Node_Id;
218 Aggr : Node_Id;
219 Target : Node_Id);
220 -- If the aggregate appears within an allocator and can be expanded in
221 -- place, this routine generates the individual assignments to components
222 -- of the designated object. This is an optimization over the general
223 -- case, where a temporary is first created on the stack and then used to
224 -- construct the allocated object on the heap.
226 procedure Convert_To_Positional
227 (N : Node_Id;
228 Max_Others_Replicate : Nat := 5;
229 Handle_Bit_Packed : Boolean := False);
230 -- If possible, convert named notation to positional notation. This
231 -- conversion is possible only in some static cases. If the conversion is
232 -- possible, then N is rewritten with the analyzed converted aggregate.
233 -- The parameter Max_Others_Replicate controls the maximum number of
234 -- values corresponding to an others choice that will be converted to
235 -- positional notation (the default of 5 is the normal limit, and reflects
236 -- the fact that normally the loop is better than a lot of separate
237 -- assignments). Note that this limit gets overridden in any case if
238 -- either of the restrictions No_Elaboration_Code or No_Implicit_Loops is
239 -- set. The parameter Handle_Bit_Packed is usually set False (since we do
240 -- not expect the back end to handle bit packed arrays, so the normal case
241 -- of conversion is pointless), but in the special case of a call from
242 -- Packed_Array_Aggregate_Handled, we set this parameter to True, since
243 -- these are cases we handle in there.
245 -- It would seem useful to have a higher default for Max_Others_Replicate,
246 -- but aggregates in the compiler make this impossible: the compiler
247 -- bootstrap fails if Max_Others_Replicate is greater than 25. This
248 -- is unexpected ???
250 procedure Expand_Array_Aggregate (N : Node_Id);
251 -- This is the top-level routine to perform array aggregate expansion.
252 -- N is the N_Aggregate node to be expanded.
254 function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean;
255 -- For two-dimensional packed aggregates with constant bounds and constant
256 -- components, it is preferable to pack the inner aggregates because the
257 -- whole matrix can then be presented to the back-end as a one-dimensional
258 -- list of literals. This is much more efficient than expanding into single
259 -- component assignments. This function determines if the type Typ is for
260 -- an array that is suitable for this optimization: it returns True if Typ
261 -- is a two dimensional bit packed array with component size 1, 2, or 4.
263 function Late_Expansion
264 (N : Node_Id;
265 Typ : Entity_Id;
266 Target : Node_Id) return List_Id;
267 -- This routine implements top-down expansion of nested aggregates. In
268 -- doing so, it avoids the generation of temporaries at each level. N is
269 -- a nested record or array aggregate with the Expansion_Delayed flag.
270 -- Typ is the expected type of the aggregate. Target is a (duplicatable)
271 -- expression that will hold the result of the aggregate expansion.
273 function Make_OK_Assignment_Statement
274 (Sloc : Source_Ptr;
275 Name : Node_Id;
276 Expression : Node_Id) return Node_Id;
277 -- This is like Make_Assignment_Statement, except that Assignment_OK
278 -- is set in the left operand. All assignments built by this unit use
279 -- this routine. This is needed to deal with assignments to initialized
280 -- constants that are done in place.
282 function Number_Of_Choices (N : Node_Id) return Nat;
283 -- Returns the number of discrete choices (not including the others choice
284 -- if present) contained in (sub-)aggregate N.
286 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean;
287 -- Given an array aggregate, this function handles the case of a packed
288 -- array aggregate with all constant values, where the aggregate can be
289 -- evaluated at compile time. If this is possible, then N is rewritten
290 -- to be its proper compile time value with all the components properly
291 -- assembled. The expression is analyzed and resolved and True is returned.
292 -- If this transformation is not possible, N is unchanged and False is
293 -- returned.
295 function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean;
296 -- If the type of the aggregate is a two-dimensional bit_packed array
297 -- it may be transformed into an array of bytes with constant values,
298 -- and presented to the back-end as a static value. The function returns
299 -- false if this transformation cannot be performed. THis is similar to,
300 -- and reuses part of the machinery in Packed_Array_Aggregate_Handled.
302 ------------------
303 -- Aggr_Size_OK --
304 ------------------
306 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean is
307 Lo : Node_Id;
308 Hi : Node_Id;
309 Indx : Node_Id;
310 Siz : Int;
311 Lov : Uint;
312 Hiv : Uint;
314 Max_Aggr_Size : Nat;
315 -- Determines the maximum size of an array aggregate produced by
316 -- converting named to positional notation (e.g. from others clauses).
317 -- This avoids running away with attempts to convert huge aggregates,
318 -- which hit memory limits in the backend.
320 function Component_Count (T : Entity_Id) return Nat;
321 -- The limit is applied to the total number of components that the
322 -- aggregate will have, which is the number of static expressions
323 -- that will appear in the flattened array. This requires a recursive
324 -- computation of the number of scalar components of the structure.
326 ---------------------
327 -- Component_Count --
328 ---------------------
330 function Component_Count (T : Entity_Id) return Nat is
331 Res : Nat := 0;
332 Comp : Entity_Id;
334 begin
335 if Is_Scalar_Type (T) then
336 return 1;
338 elsif Is_Record_Type (T) then
339 Comp := First_Component (T);
340 while Present (Comp) loop
341 Res := Res + Component_Count (Etype (Comp));
342 Next_Component (Comp);
343 end loop;
345 return Res;
347 elsif Is_Array_Type (T) then
348 declare
349 Lo : constant Node_Id :=
350 Type_Low_Bound (Etype (First_Index (T)));
351 Hi : constant Node_Id :=
352 Type_High_Bound (Etype (First_Index (T)));
354 Siz : constant Nat := Component_Count (Component_Type (T));
356 begin
357 -- Check for superflat arrays, i.e. arrays with such bounds
358 -- as 4 .. 2, to insure that this function never returns a
359 -- meaningless negative value.
361 if not Compile_Time_Known_Value (Lo)
362 or else not Compile_Time_Known_Value (Hi)
363 or else Expr_Value (Hi) < Expr_Value (Lo)
364 then
365 return 0;
367 else
368 return
369 Siz * UI_To_Int (Expr_Value (Hi) - Expr_Value (Lo) + 1);
370 end if;
371 end;
373 else
374 -- Can only be a null for an access type
376 return 1;
377 end if;
378 end Component_Count;
380 -- Start of processing for Aggr_Size_OK
382 begin
383 -- The normal aggregate limit is 50000, but we increase this limit to
384 -- 2**24 (about 16 million) if Restrictions (No_Elaboration_Code) or
385 -- Restrictions (No_Implicit_Loops) is specified, since in either case
386 -- we are at risk of declaring the program illegal because of this
387 -- limit. We also increase the limit when Static_Elaboration_Desired,
388 -- given that this means that objects are intended to be placed in data
389 -- memory.
391 -- We also increase the limit if the aggregate is for a packed two-
392 -- dimensional array, because if components are static it is much more
393 -- efficient to construct a one-dimensional equivalent array with static
394 -- components.
396 -- Conversely, we decrease the maximum size if none of the above
397 -- requirements apply, and if the aggregate has a single component
398 -- association, which will be more efficient if implemented with a loop.
400 -- Finally, we use a small limit in CodePeer mode where we favor loops
401 -- instead of thousands of single assignments (from large aggregates).
403 Max_Aggr_Size := 50000;
405 if CodePeer_Mode then
406 Max_Aggr_Size := 100;
408 elsif Restriction_Active (No_Elaboration_Code)
409 or else Restriction_Active (No_Implicit_Loops)
410 or else Is_Two_Dim_Packed_Array (Typ)
411 or else (Ekind (Current_Scope) = E_Package
412 and then Static_Elaboration_Desired (Current_Scope))
413 then
414 Max_Aggr_Size := 2 ** 24;
416 elsif No (Expressions (N))
417 and then No (Next (First (Component_Associations (N))))
418 then
419 Max_Aggr_Size := 5000;
420 end if;
422 Siz := Component_Count (Component_Type (Typ));
424 Indx := First_Index (Typ);
425 while Present (Indx) loop
426 Lo := Type_Low_Bound (Etype (Indx));
427 Hi := Type_High_Bound (Etype (Indx));
429 -- Bounds need to be known at compile time
431 if not Compile_Time_Known_Value (Lo)
432 or else not Compile_Time_Known_Value (Hi)
433 then
434 return False;
435 end if;
437 Lov := Expr_Value (Lo);
438 Hiv := Expr_Value (Hi);
440 -- A flat array is always safe
442 if Hiv < Lov then
443 return True;
444 end if;
446 -- One-component aggregates are suspicious, and if the context type
447 -- is an object declaration with non-static bounds it will trip gcc;
448 -- such an aggregate must be expanded into a single assignment.
450 if Hiv = Lov and then Nkind (Parent (N)) = N_Object_Declaration then
451 declare
452 Index_Type : constant Entity_Id :=
453 Etype
454 (First_Index (Etype (Defining_Identifier (Parent (N)))));
455 Indx : Node_Id;
457 begin
458 if not Compile_Time_Known_Value (Type_Low_Bound (Index_Type))
459 or else not Compile_Time_Known_Value
460 (Type_High_Bound (Index_Type))
461 then
462 if Present (Component_Associations (N)) then
463 Indx :=
464 First (Choices (First (Component_Associations (N))));
466 if Is_Entity_Name (Indx)
467 and then not Is_Type (Entity (Indx))
468 then
469 Error_Msg_N
470 ("single component aggregate in "
471 & "non-static context??", Indx);
472 Error_Msg_N ("\maybe subtype name was meant??", Indx);
473 end if;
474 end if;
476 return False;
477 end if;
478 end;
479 end if;
481 declare
482 Rng : constant Uint := Hiv - Lov + 1;
484 begin
485 -- Check if size is too large
487 if not UI_Is_In_Int_Range (Rng) then
488 return False;
489 end if;
491 Siz := Siz * UI_To_Int (Rng);
492 end;
494 if Siz <= 0
495 or else Siz > Max_Aggr_Size
496 then
497 return False;
498 end if;
500 -- Bounds must be in integer range, for later array construction
502 if not UI_Is_In_Int_Range (Lov)
503 or else
504 not UI_Is_In_Int_Range (Hiv)
505 then
506 return False;
507 end if;
509 Next_Index (Indx);
510 end loop;
512 return True;
513 end Aggr_Size_OK;
515 ---------------------------------
516 -- Backend_Processing_Possible --
517 ---------------------------------
519 -- Backend processing by Gigi/gcc is possible only if all the following
520 -- conditions are met:
522 -- 1. N is fully positional
524 -- 2. N is not a bit-packed array aggregate;
526 -- 3. The size of N's array type must be known at compile time. Note
527 -- that this implies that the component size is also known
529 -- 4. The array type of N does not follow the Fortran layout convention
530 -- or if it does it must be 1 dimensional.
532 -- 5. The array component type may not be tagged (which could necessitate
533 -- reassignment of proper tags).
535 -- 6. The array component type must not have unaligned bit components
537 -- 7. None of the components of the aggregate may be bit unaligned
538 -- components.
540 -- 8. There cannot be delayed components, since we do not know enough
541 -- at this stage to know if back end processing is possible.
543 -- 9. There cannot be any discriminated record components, since the
544 -- back end cannot handle this complex case.
546 -- 10. No controlled actions need to be generated for components
548 -- 11. When generating C code, N must be part of a N_Object_Declaration
550 -- 12. When generating C code, N must not include function calls
552 function Backend_Processing_Possible (N : Node_Id) return Boolean is
553 Typ : constant Entity_Id := Etype (N);
554 -- Typ is the correct constrained array subtype of the aggregate
556 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean;
557 -- This routine checks components of aggregate N, enforcing checks
558 -- 1, 7, 8, 9, 11, and 12. In the multidimensional case, these checks
559 -- are performed on subaggregates. The Index value is the current index
560 -- being checked in the multidimensional case.
562 ---------------------
563 -- Component_Check --
564 ---------------------
566 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean is
568 function Ultimate_Original_Expression (N : Node_Id) return Node_Id;
569 -- Given a type conversion or an unchecked type conversion N, return
570 -- its innermost original expression.
572 ----------------------------------
573 -- Ultimate_Original_Expression --
574 ----------------------------------
576 function Ultimate_Original_Expression (N : Node_Id) return Node_Id is
577 Expr : Node_Id := Original_Node (N);
579 begin
580 while Nkind_In (Expr, N_Type_Conversion,
581 N_Unchecked_Type_Conversion)
582 loop
583 Expr := Original_Node (Expression (Expr));
584 end loop;
586 return Expr;
587 end Ultimate_Original_Expression;
589 -- Local variables
591 Expr : Node_Id;
593 begin
594 -- Checks 1: (no component associations)
596 if Present (Component_Associations (N)) then
597 return False;
598 end if;
600 -- Checks 11: (part of an object declaration)
602 if Modify_Tree_For_C
603 and then Nkind (Parent (N)) /= N_Object_Declaration
604 and then
605 (Nkind (Parent (N)) /= N_Qualified_Expression
606 or else Nkind (Parent (Parent (N))) /= N_Object_Declaration)
607 then
608 return False;
609 end if;
611 -- Checks on components
613 -- Recurse to check subaggregates, which may appear in qualified
614 -- expressions. If delayed, the front-end will have to expand.
615 -- If the component is a discriminated record, treat as non-static,
616 -- as the back-end cannot handle this properly.
618 Expr := First (Expressions (N));
619 while Present (Expr) loop
621 -- Checks 8: (no delayed components)
623 if Is_Delayed_Aggregate (Expr) then
624 return False;
625 end if;
627 -- Checks 9: (no discriminated records)
629 if Present (Etype (Expr))
630 and then Is_Record_Type (Etype (Expr))
631 and then Has_Discriminants (Etype (Expr))
632 then
633 return False;
634 end if;
636 -- Checks 7. Component must not be bit aligned component
638 if Possible_Bit_Aligned_Component (Expr) then
639 return False;
640 end if;
642 -- Checks 12: (no function call)
644 if Modify_Tree_For_C
645 and then
646 Nkind (Ultimate_Original_Expression (Expr)) = N_Function_Call
647 then
648 return False;
649 end if;
651 -- Recursion to following indexes for multiple dimension case
653 if Present (Next_Index (Index))
654 and then not Component_Check (Expr, Next_Index (Index))
655 then
656 return False;
657 end if;
659 -- All checks for that component finished, on to next
661 Next (Expr);
662 end loop;
664 return True;
665 end Component_Check;
667 -- Start of processing for Backend_Processing_Possible
669 begin
670 -- Checks 2 (array not bit packed) and 10 (no controlled actions)
672 if Is_Bit_Packed_Array (Typ) or else Needs_Finalization (Typ) then
673 return False;
674 end if;
676 -- If component is limited, aggregate must be expanded because each
677 -- component assignment must be built in place.
679 if Is_Limited_View (Component_Type (Typ)) then
680 return False;
681 end if;
683 -- Checks 4 (array must not be multidimensional Fortran case)
685 if Convention (Typ) = Convention_Fortran
686 and then Number_Dimensions (Typ) > 1
687 then
688 return False;
689 end if;
691 -- Checks 3 (size of array must be known at compile time)
693 if not Size_Known_At_Compile_Time (Typ) then
694 return False;
695 end if;
697 -- Checks on components
699 if not Component_Check (N, First_Index (Typ)) then
700 return False;
701 end if;
703 -- Checks 5 (if the component type is tagged, then we may need to do
704 -- tag adjustments. Perhaps this should be refined to check for any
705 -- component associations that actually need tag adjustment, similar
706 -- to the test in Component_Not_OK_For_Backend for record aggregates
707 -- with tagged components, but not clear whether it's worthwhile ???;
708 -- in the case of virtual machines (no Tagged_Type_Expansion), object
709 -- tags are handled implicitly).
711 if Is_Tagged_Type (Component_Type (Typ))
712 and then Tagged_Type_Expansion
713 then
714 return False;
715 end if;
717 -- Checks 6 (component type must not have bit aligned components)
719 if Type_May_Have_Bit_Aligned_Components (Component_Type (Typ)) then
720 return False;
721 end if;
723 -- Backend processing is possible
725 Set_Size_Known_At_Compile_Time (Etype (N), True);
726 return True;
727 end Backend_Processing_Possible;
729 ---------------------------
730 -- Build_Array_Aggr_Code --
731 ---------------------------
733 -- The code that we generate from a one dimensional aggregate is
735 -- 1. If the subaggregate contains discrete choices we
737 -- (a) Sort the discrete choices
739 -- (b) Otherwise for each discrete choice that specifies a range we
740 -- emit a loop. If a range specifies a maximum of three values, or
741 -- we are dealing with an expression we emit a sequence of
742 -- assignments instead of a loop.
744 -- (c) Generate the remaining loops to cover the others choice if any
746 -- 2. If the aggregate contains positional elements we
748 -- (a) translate the positional elements in a series of assignments
750 -- (b) Generate a final loop to cover the others choice if any.
751 -- Note that this final loop has to be a while loop since the case
753 -- L : Integer := Integer'Last;
754 -- H : Integer := Integer'Last;
755 -- A : array (L .. H) := (1, others =>0);
757 -- cannot be handled by a for loop. Thus for the following
759 -- array (L .. H) := (.. positional elements.., others =>E);
761 -- we always generate something like:
763 -- J : Index_Type := Index_Of_Last_Positional_Element;
764 -- while J < H loop
765 -- J := Index_Base'Succ (J)
766 -- Tmp (J) := E;
767 -- end loop;
769 function Build_Array_Aggr_Code
770 (N : Node_Id;
771 Ctype : Entity_Id;
772 Index : Node_Id;
773 Into : Node_Id;
774 Scalar_Comp : Boolean;
775 Indexes : List_Id := No_List) return List_Id
777 Loc : constant Source_Ptr := Sloc (N);
778 Index_Base : constant Entity_Id := Base_Type (Etype (Index));
779 Index_Base_L : constant Node_Id := Type_Low_Bound (Index_Base);
780 Index_Base_H : constant Node_Id := Type_High_Bound (Index_Base);
782 function Add (Val : Int; To : Node_Id) return Node_Id;
783 -- Returns an expression where Val is added to expression To, unless
784 -- To+Val is provably out of To's base type range. To must be an
785 -- already analyzed expression.
787 function Empty_Range (L, H : Node_Id) return Boolean;
788 -- Returns True if the range defined by L .. H is certainly empty
790 function Equal (L, H : Node_Id) return Boolean;
791 -- Returns True if L = H for sure
793 function Index_Base_Name return Node_Id;
794 -- Returns a new reference to the index type name
796 function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id;
797 -- Ind must be a side-effect-free expression. If the input aggregate N
798 -- to Build_Loop contains no subaggregates, then this function returns
799 -- the assignment statement:
801 -- Into (Indexes, Ind) := Expr;
803 -- Otherwise we call Build_Code recursively
805 -- Ada 2005 (AI-287): In case of default initialized component, Expr
806 -- is empty and we generate a call to the corresponding IP subprogram.
808 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id;
809 -- Nodes L and H must be side-effect-free expressions. If the input
810 -- aggregate N to Build_Loop contains no subaggregates, this routine
811 -- returns the for loop statement:
813 -- for J in Index_Base'(L) .. Index_Base'(H) loop
814 -- Into (Indexes, J) := Expr;
815 -- end loop;
817 -- Otherwise we call Build_Code recursively.
818 -- As an optimization if the loop covers 3 or fewer scalar elements we
819 -- generate a sequence of assignments.
821 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id;
822 -- Nodes L and H must be side-effect-free expressions. If the input
823 -- aggregate N to Build_Loop contains no subaggregates, this routine
824 -- returns the while loop statement:
826 -- J : Index_Base := L;
827 -- while J < H loop
828 -- J := Index_Base'Succ (J);
829 -- Into (Indexes, J) := Expr;
830 -- end loop;
832 -- Otherwise we call Build_Code recursively
834 function Get_Assoc_Expr (Assoc : Node_Id) return Node_Id;
835 -- For an association with a box, use value given by aspect
836 -- Default_Component_Value of array type if specified, else use
837 -- value given by aspect Default_Value for component type itself
838 -- if specified, else return Empty.
840 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean;
841 function Local_Expr_Value (E : Node_Id) return Uint;
842 -- These two Local routines are used to replace the corresponding ones
843 -- in sem_eval because while processing the bounds of an aggregate with
844 -- discrete choices whose index type is an enumeration, we build static
845 -- expressions not recognized by Compile_Time_Known_Value as such since
846 -- they have not yet been analyzed and resolved. All the expressions in
847 -- question are things like Index_Base_Name'Val (Const) which we can
848 -- easily recognize as being constant.
850 ---------
851 -- Add --
852 ---------
854 function Add (Val : Int; To : Node_Id) return Node_Id is
855 Expr_Pos : Node_Id;
856 Expr : Node_Id;
857 To_Pos : Node_Id;
858 U_To : Uint;
859 U_Val : constant Uint := UI_From_Int (Val);
861 begin
862 -- Note: do not try to optimize the case of Val = 0, because
863 -- we need to build a new node with the proper Sloc value anyway.
865 -- First test if we can do constant folding
867 if Local_Compile_Time_Known_Value (To) then
868 U_To := Local_Expr_Value (To) + Val;
870 -- Determine if our constant is outside the range of the index.
871 -- If so return an Empty node. This empty node will be caught
872 -- by Empty_Range below.
874 if Compile_Time_Known_Value (Index_Base_L)
875 and then U_To < Expr_Value (Index_Base_L)
876 then
877 return Empty;
879 elsif Compile_Time_Known_Value (Index_Base_H)
880 and then U_To > Expr_Value (Index_Base_H)
881 then
882 return Empty;
883 end if;
885 Expr_Pos := Make_Integer_Literal (Loc, U_To);
886 Set_Is_Static_Expression (Expr_Pos);
888 if not Is_Enumeration_Type (Index_Base) then
889 Expr := Expr_Pos;
891 -- If we are dealing with enumeration return
892 -- Index_Base'Val (Expr_Pos)
894 else
895 Expr :=
896 Make_Attribute_Reference
897 (Loc,
898 Prefix => Index_Base_Name,
899 Attribute_Name => Name_Val,
900 Expressions => New_List (Expr_Pos));
901 end if;
903 return Expr;
904 end if;
906 -- If we are here no constant folding possible
908 if not Is_Enumeration_Type (Index_Base) then
909 Expr :=
910 Make_Op_Add (Loc,
911 Left_Opnd => Duplicate_Subexpr (To),
912 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
914 -- If we are dealing with enumeration return
915 -- Index_Base'Val (Index_Base'Pos (To) + Val)
917 else
918 To_Pos :=
919 Make_Attribute_Reference
920 (Loc,
921 Prefix => Index_Base_Name,
922 Attribute_Name => Name_Pos,
923 Expressions => New_List (Duplicate_Subexpr (To)));
925 Expr_Pos :=
926 Make_Op_Add (Loc,
927 Left_Opnd => To_Pos,
928 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
930 Expr :=
931 Make_Attribute_Reference
932 (Loc,
933 Prefix => Index_Base_Name,
934 Attribute_Name => Name_Val,
935 Expressions => New_List (Expr_Pos));
936 end if;
938 return Expr;
939 end Add;
941 -----------------
942 -- Empty_Range --
943 -----------------
945 function Empty_Range (L, H : Node_Id) return Boolean is
946 Is_Empty : Boolean := False;
947 Low : Node_Id;
948 High : Node_Id;
950 begin
951 -- First check if L or H were already detected as overflowing the
952 -- index base range type by function Add above. If this is so Add
953 -- returns the empty node.
955 if No (L) or else No (H) then
956 return True;
957 end if;
959 for J in 1 .. 3 loop
960 case J is
962 -- L > H range is empty
964 when 1 =>
965 Low := L;
966 High := H;
968 -- B_L > H range must be empty
970 when 2 =>
971 Low := Index_Base_L;
972 High := H;
974 -- L > B_H range must be empty
976 when 3 =>
977 Low := L;
978 High := Index_Base_H;
979 end case;
981 if Local_Compile_Time_Known_Value (Low)
982 and then
983 Local_Compile_Time_Known_Value (High)
984 then
985 Is_Empty :=
986 UI_Gt (Local_Expr_Value (Low), Local_Expr_Value (High));
987 end if;
989 exit when Is_Empty;
990 end loop;
992 return Is_Empty;
993 end Empty_Range;
995 -----------
996 -- Equal --
997 -----------
999 function Equal (L, H : Node_Id) return Boolean is
1000 begin
1001 if L = H then
1002 return True;
1004 elsif Local_Compile_Time_Known_Value (L)
1005 and then
1006 Local_Compile_Time_Known_Value (H)
1007 then
1008 return UI_Eq (Local_Expr_Value (L), Local_Expr_Value (H));
1009 end if;
1011 return False;
1012 end Equal;
1014 ----------------
1015 -- Gen_Assign --
1016 ----------------
1018 function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id is
1019 L : constant List_Id := New_List;
1020 A : Node_Id;
1022 New_Indexes : List_Id;
1023 Indexed_Comp : Node_Id;
1024 Expr_Q : Node_Id;
1025 Comp_Type : Entity_Id := Empty;
1027 function Add_Loop_Actions (Lis : List_Id) return List_Id;
1028 -- Collect insert_actions generated in the construction of a
1029 -- loop, and prepend them to the sequence of assignments to
1030 -- complete the eventual body of the loop.
1032 ----------------------
1033 -- Add_Loop_Actions --
1034 ----------------------
1036 function Add_Loop_Actions (Lis : List_Id) return List_Id is
1037 Res : List_Id;
1039 begin
1040 -- Ada 2005 (AI-287): Do nothing else in case of default
1041 -- initialized component.
1043 if No (Expr) then
1044 return Lis;
1046 elsif Nkind (Parent (Expr)) = N_Component_Association
1047 and then Present (Loop_Actions (Parent (Expr)))
1048 then
1049 Append_List (Lis, Loop_Actions (Parent (Expr)));
1050 Res := Loop_Actions (Parent (Expr));
1051 Set_Loop_Actions (Parent (Expr), No_List);
1052 return Res;
1054 else
1055 return Lis;
1056 end if;
1057 end Add_Loop_Actions;
1059 -- Start of processing for Gen_Assign
1061 begin
1062 if No (Indexes) then
1063 New_Indexes := New_List;
1064 else
1065 New_Indexes := New_Copy_List_Tree (Indexes);
1066 end if;
1068 Append_To (New_Indexes, Ind);
1070 if Present (Next_Index (Index)) then
1071 return
1072 Add_Loop_Actions (
1073 Build_Array_Aggr_Code
1074 (N => Expr,
1075 Ctype => Ctype,
1076 Index => Next_Index (Index),
1077 Into => Into,
1078 Scalar_Comp => Scalar_Comp,
1079 Indexes => New_Indexes));
1080 end if;
1082 -- If we get here then we are at a bottom-level (sub-)aggregate
1084 Indexed_Comp :=
1085 Checks_Off
1086 (Make_Indexed_Component (Loc,
1087 Prefix => New_Copy_Tree (Into),
1088 Expressions => New_Indexes));
1090 Set_Assignment_OK (Indexed_Comp);
1092 -- Ada 2005 (AI-287): In case of default initialized component, Expr
1093 -- is not present (and therefore we also initialize Expr_Q to empty).
1095 if No (Expr) then
1096 Expr_Q := Empty;
1097 elsif Nkind (Expr) = N_Qualified_Expression then
1098 Expr_Q := Expression (Expr);
1099 else
1100 Expr_Q := Expr;
1101 end if;
1103 if Present (Etype (N)) and then Etype (N) /= Any_Composite then
1104 Comp_Type := Component_Type (Etype (N));
1105 pragma Assert (Comp_Type = Ctype); -- AI-287
1107 elsif Present (Next (First (New_Indexes))) then
1109 -- Ada 2005 (AI-287): Do nothing in case of default initialized
1110 -- component because we have received the component type in
1111 -- the formal parameter Ctype.
1113 -- ??? Some assert pragmas have been added to check if this new
1114 -- formal can be used to replace this code in all cases.
1116 if Present (Expr) then
1118 -- This is a multidimensional array. Recover the component type
1119 -- from the outermost aggregate, because subaggregates do not
1120 -- have an assigned type.
1122 declare
1123 P : Node_Id;
1125 begin
1126 P := Parent (Expr);
1127 while Present (P) loop
1128 if Nkind (P) = N_Aggregate
1129 and then Present (Etype (P))
1130 then
1131 Comp_Type := Component_Type (Etype (P));
1132 exit;
1134 else
1135 P := Parent (P);
1136 end if;
1137 end loop;
1139 pragma Assert (Comp_Type = Ctype); -- AI-287
1140 end;
1141 end if;
1142 end if;
1144 -- Ada 2005 (AI-287): We only analyze the expression in case of non-
1145 -- default initialized components (otherwise Expr_Q is not present).
1147 if Present (Expr_Q)
1148 and then Nkind_In (Expr_Q, N_Aggregate, N_Extension_Aggregate)
1149 then
1150 -- At this stage the Expression may not have been analyzed yet
1151 -- because the array aggregate code has not been updated to use
1152 -- the Expansion_Delayed flag and avoid analysis altogether to
1153 -- solve the same problem (see Resolve_Aggr_Expr). So let us do
1154 -- the analysis of non-array aggregates now in order to get the
1155 -- value of Expansion_Delayed flag for the inner aggregate ???
1157 if Present (Comp_Type) and then not Is_Array_Type (Comp_Type) then
1158 Analyze_And_Resolve (Expr_Q, Comp_Type);
1159 end if;
1161 if Is_Delayed_Aggregate (Expr_Q) then
1163 -- This is either a subaggregate of a multidimensional array,
1164 -- or a component of an array type whose component type is
1165 -- also an array. In the latter case, the expression may have
1166 -- component associations that provide different bounds from
1167 -- those of the component type, and sliding must occur. Instead
1168 -- of decomposing the current aggregate assignment, force the
1169 -- re-analysis of the assignment, so that a temporary will be
1170 -- generated in the usual fashion, and sliding will take place.
1172 if Nkind (Parent (N)) = N_Assignment_Statement
1173 and then Is_Array_Type (Comp_Type)
1174 and then Present (Component_Associations (Expr_Q))
1175 and then Must_Slide (Comp_Type, Etype (Expr_Q))
1176 then
1177 Set_Expansion_Delayed (Expr_Q, False);
1178 Set_Analyzed (Expr_Q, False);
1180 else
1181 return
1182 Add_Loop_Actions (
1183 Late_Expansion (Expr_Q, Etype (Expr_Q), Indexed_Comp));
1184 end if;
1185 end if;
1186 end if;
1188 -- Ada 2005 (AI-287): In case of default initialized component, call
1189 -- the initialization subprogram associated with the component type.
1190 -- If the component type is an access type, add an explicit null
1191 -- assignment, because for the back-end there is an initialization
1192 -- present for the whole aggregate, and no default initialization
1193 -- will take place.
1195 -- In addition, if the component type is controlled, we must call
1196 -- its Initialize procedure explicitly, because there is no explicit
1197 -- object creation that will invoke it otherwise.
1199 if No (Expr) then
1200 if Present (Base_Init_Proc (Base_Type (Ctype)))
1201 or else Has_Task (Base_Type (Ctype))
1202 then
1203 Append_List_To (L,
1204 Build_Initialization_Call (Loc,
1205 Id_Ref => Indexed_Comp,
1206 Typ => Ctype,
1207 With_Default_Init => True));
1209 -- If the component type has invariants, add an invariant
1210 -- check after the component is default-initialized. It will
1211 -- be analyzed and resolved before the code for initialization
1212 -- of other components.
1214 if Has_Invariants (Ctype) then
1215 Set_Etype (Indexed_Comp, Ctype);
1216 Append_To (L, Make_Invariant_Call (Indexed_Comp));
1217 end if;
1219 elsif Is_Access_Type (Ctype) then
1220 Append_To (L,
1221 Make_Assignment_Statement (Loc,
1222 Name => Indexed_Comp,
1223 Expression => Make_Null (Loc)));
1224 end if;
1226 if Needs_Finalization (Ctype) then
1227 Append_To (L,
1228 Make_Init_Call
1229 (Obj_Ref => New_Copy_Tree (Indexed_Comp),
1230 Typ => Ctype));
1231 end if;
1233 else
1234 A :=
1235 Make_OK_Assignment_Statement (Loc,
1236 Name => Indexed_Comp,
1237 Expression => New_Copy_Tree (Expr));
1239 -- The target of the assignment may not have been initialized,
1240 -- so it is not possible to call Finalize as expected in normal
1241 -- controlled assignments. We must also avoid using the primitive
1242 -- _assign (which depends on a valid target, and may for example
1243 -- perform discriminant checks on it).
1245 -- Both Finalize and usage of _assign are disabled by setting
1246 -- No_Ctrl_Actions on the assignment. The rest of the controlled
1247 -- actions are done manually with the proper finalization list
1248 -- coming from the context.
1250 Set_No_Ctrl_Actions (A);
1252 -- If this is an aggregate for an array of arrays, each
1253 -- subaggregate will be expanded as well, and even with
1254 -- No_Ctrl_Actions the assignments of inner components will
1255 -- require attachment in their assignments to temporaries. These
1256 -- temporaries must be finalized for each subaggregate, to prevent
1257 -- multiple attachments of the same temporary location to same
1258 -- finalization chain (and consequently circular lists). To ensure
1259 -- that finalization takes place for each subaggregate we wrap the
1260 -- assignment in a block.
1262 if Present (Comp_Type)
1263 and then Needs_Finalization (Comp_Type)
1264 and then Is_Array_Type (Comp_Type)
1265 and then Present (Expr)
1266 then
1267 A :=
1268 Make_Block_Statement (Loc,
1269 Handled_Statement_Sequence =>
1270 Make_Handled_Sequence_Of_Statements (Loc,
1271 Statements => New_List (A)));
1272 end if;
1274 Append_To (L, A);
1276 -- Adjust the tag if tagged (because of possible view
1277 -- conversions), unless compiling for a VM where tags
1278 -- are implicit.
1280 if Present (Comp_Type)
1281 and then Is_Tagged_Type (Comp_Type)
1282 and then Tagged_Type_Expansion
1283 then
1284 declare
1285 Full_Typ : constant Entity_Id := Underlying_Type (Comp_Type);
1287 begin
1288 A :=
1289 Make_OK_Assignment_Statement (Loc,
1290 Name =>
1291 Make_Selected_Component (Loc,
1292 Prefix => New_Copy_Tree (Indexed_Comp),
1293 Selector_Name =>
1294 New_Occurrence_Of
1295 (First_Tag_Component (Full_Typ), Loc)),
1297 Expression =>
1298 Unchecked_Convert_To (RTE (RE_Tag),
1299 New_Occurrence_Of
1300 (Node (First_Elmt (Access_Disp_Table (Full_Typ))),
1301 Loc)));
1303 Append_To (L, A);
1304 end;
1305 end if;
1307 -- Adjust and attach the component to the proper final list, which
1308 -- can be the controller of the outer record object or the final
1309 -- list associated with the scope.
1311 -- If the component is itself an array of controlled types, whose
1312 -- value is given by a subaggregate, then the attach calls have
1313 -- been generated when individual subcomponent are assigned, and
1314 -- must not be done again to prevent malformed finalization chains
1315 -- (see comments above, concerning the creation of a block to hold
1316 -- inner finalization actions).
1318 if Present (Comp_Type)
1319 and then Needs_Finalization (Comp_Type)
1320 and then not Is_Limited_Type (Comp_Type)
1321 and then not
1322 (Is_Array_Type (Comp_Type)
1323 and then Is_Controlled (Component_Type (Comp_Type))
1324 and then Nkind (Expr) = N_Aggregate)
1325 then
1326 Append_To (L,
1327 Make_Adjust_Call
1328 (Obj_Ref => New_Copy_Tree (Indexed_Comp),
1329 Typ => Comp_Type));
1330 end if;
1331 end if;
1333 return Add_Loop_Actions (L);
1334 end Gen_Assign;
1336 --------------
1337 -- Gen_Loop --
1338 --------------
1340 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id is
1341 L_J : Node_Id;
1343 L_L : Node_Id;
1344 -- Index_Base'(L)
1346 L_H : Node_Id;
1347 -- Index_Base'(H)
1349 L_Range : Node_Id;
1350 -- Index_Base'(L) .. Index_Base'(H)
1352 L_Iteration_Scheme : Node_Id;
1353 -- L_J in Index_Base'(L) .. Index_Base'(H)
1355 L_Body : List_Id;
1356 -- The statements to execute in the loop
1358 S : constant List_Id := New_List;
1359 -- List of statements
1361 Tcopy : Node_Id;
1362 -- Copy of expression tree, used for checking purposes
1364 begin
1365 -- If loop bounds define an empty range return the null statement
1367 if Empty_Range (L, H) then
1368 Append_To (S, Make_Null_Statement (Loc));
1370 -- Ada 2005 (AI-287): Nothing else need to be done in case of
1371 -- default initialized component.
1373 if No (Expr) then
1374 null;
1376 else
1377 -- The expression must be type-checked even though no component
1378 -- of the aggregate will have this value. This is done only for
1379 -- actual components of the array, not for subaggregates. Do
1380 -- the check on a copy, because the expression may be shared
1381 -- among several choices, some of which might be non-null.
1383 if Present (Etype (N))
1384 and then Is_Array_Type (Etype (N))
1385 and then No (Next_Index (Index))
1386 then
1387 Expander_Mode_Save_And_Set (False);
1388 Tcopy := New_Copy_Tree (Expr);
1389 Set_Parent (Tcopy, N);
1390 Analyze_And_Resolve (Tcopy, Component_Type (Etype (N)));
1391 Expander_Mode_Restore;
1392 end if;
1393 end if;
1395 return S;
1397 -- If loop bounds are the same then generate an assignment
1399 elsif Equal (L, H) then
1400 return Gen_Assign (New_Copy_Tree (L), Expr);
1402 -- If H - L <= 2 then generate a sequence of assignments when we are
1403 -- processing the bottom most aggregate and it contains scalar
1404 -- components.
1406 elsif No (Next_Index (Index))
1407 and then Scalar_Comp
1408 and then Local_Compile_Time_Known_Value (L)
1409 and then Local_Compile_Time_Known_Value (H)
1410 and then Local_Expr_Value (H) - Local_Expr_Value (L) <= 2
1411 then
1413 Append_List_To (S, Gen_Assign (New_Copy_Tree (L), Expr));
1414 Append_List_To (S, Gen_Assign (Add (1, To => L), Expr));
1416 if Local_Expr_Value (H) - Local_Expr_Value (L) = 2 then
1417 Append_List_To (S, Gen_Assign (Add (2, To => L), Expr));
1418 end if;
1420 return S;
1421 end if;
1423 -- Otherwise construct the loop, starting with the loop index L_J
1425 L_J := Make_Temporary (Loc, 'J', L);
1427 -- Construct "L .. H" in Index_Base. We use a qualified expression
1428 -- for the bound to convert to the index base, but we don't need
1429 -- to do that if we already have the base type at hand.
1431 if Etype (L) = Index_Base then
1432 L_L := L;
1433 else
1434 L_L :=
1435 Make_Qualified_Expression (Loc,
1436 Subtype_Mark => Index_Base_Name,
1437 Expression => L);
1438 end if;
1440 if Etype (H) = Index_Base then
1441 L_H := H;
1442 else
1443 L_H :=
1444 Make_Qualified_Expression (Loc,
1445 Subtype_Mark => Index_Base_Name,
1446 Expression => H);
1447 end if;
1449 L_Range :=
1450 Make_Range (Loc,
1451 Low_Bound => L_L,
1452 High_Bound => L_H);
1454 -- Construct "for L_J in Index_Base range L .. H"
1456 L_Iteration_Scheme :=
1457 Make_Iteration_Scheme
1458 (Loc,
1459 Loop_Parameter_Specification =>
1460 Make_Loop_Parameter_Specification
1461 (Loc,
1462 Defining_Identifier => L_J,
1463 Discrete_Subtype_Definition => L_Range));
1465 -- Construct the statements to execute in the loop body
1467 L_Body := Gen_Assign (New_Occurrence_Of (L_J, Loc), Expr);
1469 -- Construct the final loop
1471 Append_To (S,
1472 Make_Implicit_Loop_Statement
1473 (Node => N,
1474 Identifier => Empty,
1475 Iteration_Scheme => L_Iteration_Scheme,
1476 Statements => L_Body));
1478 -- A small optimization: if the aggregate is initialized with a box
1479 -- and the component type has no initialization procedure, remove the
1480 -- useless empty loop.
1482 if Nkind (First (S)) = N_Loop_Statement
1483 and then Is_Empty_List (Statements (First (S)))
1484 then
1485 return New_List (Make_Null_Statement (Loc));
1486 else
1487 return S;
1488 end if;
1489 end Gen_Loop;
1491 ---------------
1492 -- Gen_While --
1493 ---------------
1495 -- The code built is
1497 -- W_J : Index_Base := L;
1498 -- while W_J < H loop
1499 -- W_J := Index_Base'Succ (W);
1500 -- L_Body;
1501 -- end loop;
1503 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id is
1504 W_J : Node_Id;
1506 W_Decl : Node_Id;
1507 -- W_J : Base_Type := L;
1509 W_Iteration_Scheme : Node_Id;
1510 -- while W_J < H
1512 W_Index_Succ : Node_Id;
1513 -- Index_Base'Succ (J)
1515 W_Increment : Node_Id;
1516 -- W_J := Index_Base'Succ (W)
1518 W_Body : constant List_Id := New_List;
1519 -- The statements to execute in the loop
1521 S : constant List_Id := New_List;
1522 -- list of statement
1524 begin
1525 -- If loop bounds define an empty range or are equal return null
1527 if Empty_Range (L, H) or else Equal (L, H) then
1528 Append_To (S, Make_Null_Statement (Loc));
1529 return S;
1530 end if;
1532 -- Build the decl of W_J
1534 W_J := Make_Temporary (Loc, 'J', L);
1535 W_Decl :=
1536 Make_Object_Declaration
1537 (Loc,
1538 Defining_Identifier => W_J,
1539 Object_Definition => Index_Base_Name,
1540 Expression => L);
1542 -- Theoretically we should do a New_Copy_Tree (L) here, but we know
1543 -- that in this particular case L is a fresh Expr generated by
1544 -- Add which we are the only ones to use.
1546 Append_To (S, W_Decl);
1548 -- Construct " while W_J < H"
1550 W_Iteration_Scheme :=
1551 Make_Iteration_Scheme
1552 (Loc,
1553 Condition => Make_Op_Lt
1554 (Loc,
1555 Left_Opnd => New_Occurrence_Of (W_J, Loc),
1556 Right_Opnd => New_Copy_Tree (H)));
1558 -- Construct the statements to execute in the loop body
1560 W_Index_Succ :=
1561 Make_Attribute_Reference
1562 (Loc,
1563 Prefix => Index_Base_Name,
1564 Attribute_Name => Name_Succ,
1565 Expressions => New_List (New_Occurrence_Of (W_J, Loc)));
1567 W_Increment :=
1568 Make_OK_Assignment_Statement
1569 (Loc,
1570 Name => New_Occurrence_Of (W_J, Loc),
1571 Expression => W_Index_Succ);
1573 Append_To (W_Body, W_Increment);
1574 Append_List_To (W_Body,
1575 Gen_Assign (New_Occurrence_Of (W_J, Loc), Expr));
1577 -- Construct the final loop
1579 Append_To (S,
1580 Make_Implicit_Loop_Statement
1581 (Node => N,
1582 Identifier => Empty,
1583 Iteration_Scheme => W_Iteration_Scheme,
1584 Statements => W_Body));
1586 return S;
1587 end Gen_While;
1589 --------------------
1590 -- Get_Assoc_Expr --
1591 --------------------
1593 function Get_Assoc_Expr (Assoc : Node_Id) return Node_Id is
1594 Typ : constant Entity_Id := Base_Type (Etype (N));
1596 begin
1597 if Box_Present (Assoc) then
1598 if Is_Scalar_Type (Ctype) then
1599 if Present (Default_Aspect_Component_Value (Typ)) then
1600 return Default_Aspect_Component_Value (Typ);
1601 elsif Present (Default_Aspect_Value (Ctype)) then
1602 return Default_Aspect_Value (Ctype);
1603 else
1604 return Empty;
1605 end if;
1607 else
1608 return Empty;
1609 end if;
1611 else
1612 return Expression (Assoc);
1613 end if;
1614 end Get_Assoc_Expr;
1616 ---------------------
1617 -- Index_Base_Name --
1618 ---------------------
1620 function Index_Base_Name return Node_Id is
1621 begin
1622 return New_Occurrence_Of (Index_Base, Sloc (N));
1623 end Index_Base_Name;
1625 ------------------------------------
1626 -- Local_Compile_Time_Known_Value --
1627 ------------------------------------
1629 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean is
1630 begin
1631 return Compile_Time_Known_Value (E)
1632 or else
1633 (Nkind (E) = N_Attribute_Reference
1634 and then Attribute_Name (E) = Name_Val
1635 and then Compile_Time_Known_Value (First (Expressions (E))));
1636 end Local_Compile_Time_Known_Value;
1638 ----------------------
1639 -- Local_Expr_Value --
1640 ----------------------
1642 function Local_Expr_Value (E : Node_Id) return Uint is
1643 begin
1644 if Compile_Time_Known_Value (E) then
1645 return Expr_Value (E);
1646 else
1647 return Expr_Value (First (Expressions (E)));
1648 end if;
1649 end Local_Expr_Value;
1651 -- Build_Array_Aggr_Code Variables
1653 Assoc : Node_Id;
1654 Choice : Node_Id;
1655 Expr : Node_Id;
1656 Typ : Entity_Id;
1658 Others_Assoc : Node_Id := Empty;
1660 Aggr_L : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
1661 Aggr_H : constant Node_Id := High_Bound (Aggregate_Bounds (N));
1662 -- The aggregate bounds of this specific subaggregate. Note that if the
1663 -- code generated by Build_Array_Aggr_Code is executed then these bounds
1664 -- are OK. Otherwise a Constraint_Error would have been raised.
1666 Aggr_Low : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_L);
1667 Aggr_High : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_H);
1668 -- After Duplicate_Subexpr these are side-effect free
1670 Low : Node_Id;
1671 High : Node_Id;
1673 Nb_Choices : Nat := 0;
1674 Table : Case_Table_Type (1 .. Number_Of_Choices (N));
1675 -- Used to sort all the different choice values
1677 Nb_Elements : Int;
1678 -- Number of elements in the positional aggregate
1680 New_Code : constant List_Id := New_List;
1682 -- Start of processing for Build_Array_Aggr_Code
1684 begin
1685 -- First before we start, a special case. if we have a bit packed
1686 -- array represented as a modular type, then clear the value to
1687 -- zero first, to ensure that unused bits are properly cleared.
1689 Typ := Etype (N);
1691 if Present (Typ)
1692 and then Is_Bit_Packed_Array (Typ)
1693 and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ))
1694 then
1695 Append_To (New_Code,
1696 Make_Assignment_Statement (Loc,
1697 Name => New_Copy_Tree (Into),
1698 Expression =>
1699 Unchecked_Convert_To (Typ,
1700 Make_Integer_Literal (Loc, Uint_0))));
1701 end if;
1703 -- If the component type contains tasks, we need to build a Master
1704 -- entity in the current scope, because it will be needed if build-
1705 -- in-place functions are called in the expanded code.
1707 if Nkind (Parent (N)) = N_Object_Declaration and then Has_Task (Typ) then
1708 Build_Master_Entity (Defining_Identifier (Parent (N)));
1709 end if;
1711 -- STEP 1: Process component associations
1713 -- For those associations that may generate a loop, initialize
1714 -- Loop_Actions to collect inserted actions that may be crated.
1716 -- Skip this if no component associations
1718 if No (Expressions (N)) then
1720 -- STEP 1 (a): Sort the discrete choices
1722 Assoc := First (Component_Associations (N));
1723 while Present (Assoc) loop
1724 Choice := First (Choices (Assoc));
1725 while Present (Choice) loop
1726 if Nkind (Choice) = N_Others_Choice then
1727 Set_Loop_Actions (Assoc, New_List);
1728 Others_Assoc := Assoc;
1729 exit;
1730 end if;
1732 Get_Index_Bounds (Choice, Low, High);
1734 if Low /= High then
1735 Set_Loop_Actions (Assoc, New_List);
1736 end if;
1738 Nb_Choices := Nb_Choices + 1;
1740 Table (Nb_Choices) :=
1741 (Choice_Lo => Low,
1742 Choice_Hi => High,
1743 Choice_Node => Get_Assoc_Expr (Assoc));
1745 Next (Choice);
1746 end loop;
1748 Next (Assoc);
1749 end loop;
1751 -- If there is more than one set of choices these must be static
1752 -- and we can therefore sort them. Remember that Nb_Choices does not
1753 -- account for an others choice.
1755 if Nb_Choices > 1 then
1756 Sort_Case_Table (Table);
1757 end if;
1759 -- STEP 1 (b): take care of the whole set of discrete choices
1761 for J in 1 .. Nb_Choices loop
1762 Low := Table (J).Choice_Lo;
1763 High := Table (J).Choice_Hi;
1764 Expr := Table (J).Choice_Node;
1765 Append_List (Gen_Loop (Low, High, Expr), To => New_Code);
1766 end loop;
1768 -- STEP 1 (c): generate the remaining loops to cover others choice
1769 -- We don't need to generate loops over empty gaps, but if there is
1770 -- a single empty range we must analyze the expression for semantics
1772 if Present (Others_Assoc) then
1773 declare
1774 First : Boolean := True;
1776 begin
1777 for J in 0 .. Nb_Choices loop
1778 if J = 0 then
1779 Low := Aggr_Low;
1780 else
1781 Low := Add (1, To => Table (J).Choice_Hi);
1782 end if;
1784 if J = Nb_Choices then
1785 High := Aggr_High;
1786 else
1787 High := Add (-1, To => Table (J + 1).Choice_Lo);
1788 end if;
1790 -- If this is an expansion within an init proc, make
1791 -- sure that discriminant references are replaced by
1792 -- the corresponding discriminal.
1794 if Inside_Init_Proc then
1795 if Is_Entity_Name (Low)
1796 and then Ekind (Entity (Low)) = E_Discriminant
1797 then
1798 Set_Entity (Low, Discriminal (Entity (Low)));
1799 end if;
1801 if Is_Entity_Name (High)
1802 and then Ekind (Entity (High)) = E_Discriminant
1803 then
1804 Set_Entity (High, Discriminal (Entity (High)));
1805 end if;
1806 end if;
1808 if First
1809 or else not Empty_Range (Low, High)
1810 then
1811 First := False;
1812 Append_List
1813 (Gen_Loop (Low, High,
1814 Get_Assoc_Expr (Others_Assoc)), To => New_Code);
1815 end if;
1816 end loop;
1817 end;
1818 end if;
1820 -- STEP 2: Process positional components
1822 else
1823 -- STEP 2 (a): Generate the assignments for each positional element
1824 -- Note that here we have to use Aggr_L rather than Aggr_Low because
1825 -- Aggr_L is analyzed and Add wants an analyzed expression.
1827 Expr := First (Expressions (N));
1828 Nb_Elements := -1;
1829 while Present (Expr) loop
1830 Nb_Elements := Nb_Elements + 1;
1831 Append_List (Gen_Assign (Add (Nb_Elements, To => Aggr_L), Expr),
1832 To => New_Code);
1833 Next (Expr);
1834 end loop;
1836 -- STEP 2 (b): Generate final loop if an others choice is present
1837 -- Here Nb_Elements gives the offset of the last positional element.
1839 if Present (Component_Associations (N)) then
1840 Assoc := Last (Component_Associations (N));
1842 -- Ada 2005 (AI-287)
1844 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
1845 Aggr_High,
1846 Get_Assoc_Expr (Assoc)), -- AI-287
1847 To => New_Code);
1848 end if;
1849 end if;
1851 return New_Code;
1852 end Build_Array_Aggr_Code;
1854 ----------------------------
1855 -- Build_Record_Aggr_Code --
1856 ----------------------------
1858 function Build_Record_Aggr_Code
1859 (N : Node_Id;
1860 Typ : Entity_Id;
1861 Lhs : Node_Id) return List_Id
1863 Loc : constant Source_Ptr := Sloc (N);
1864 L : constant List_Id := New_List;
1865 N_Typ : constant Entity_Id := Etype (N);
1867 Comp : Node_Id;
1868 Instr : Node_Id;
1869 Ref : Node_Id;
1870 Target : Entity_Id;
1871 Comp_Type : Entity_Id;
1872 Selector : Entity_Id;
1873 Comp_Expr : Node_Id;
1874 Expr_Q : Node_Id;
1876 -- If this is an internal aggregate, the External_Final_List is an
1877 -- expression for the controller record of the enclosing type.
1879 -- If the current aggregate has several controlled components, this
1880 -- expression will appear in several calls to attach to the finali-
1881 -- zation list, and it must not be shared.
1883 Ancestor_Is_Expression : Boolean := False;
1884 Ancestor_Is_Subtype_Mark : Boolean := False;
1886 Init_Typ : Entity_Id := Empty;
1888 Finalization_Done : Boolean := False;
1889 -- True if Generate_Finalization_Actions has already been called; calls
1890 -- after the first do nothing.
1892 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id;
1893 -- Returns the value that the given discriminant of an ancestor type
1894 -- should receive (in the absence of a conflict with the value provided
1895 -- by an ancestor part of an extension aggregate).
1897 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id);
1898 -- Check that each of the discriminant values defined by the ancestor
1899 -- part of an extension aggregate match the corresponding values
1900 -- provided by either an association of the aggregate or by the
1901 -- constraint imposed by a parent type (RM95-4.3.2(8)).
1903 function Compatible_Int_Bounds
1904 (Agg_Bounds : Node_Id;
1905 Typ_Bounds : Node_Id) return Boolean;
1906 -- Return true if Agg_Bounds are equal or within Typ_Bounds. It is
1907 -- assumed that both bounds are integer ranges.
1909 procedure Generate_Finalization_Actions;
1910 -- Deal with the various controlled type data structure initializations
1911 -- (but only if it hasn't been done already).
1913 function Get_Constraint_Association (T : Entity_Id) return Node_Id;
1914 -- Returns the first discriminant association in the constraint
1915 -- associated with T, if any, otherwise returns Empty.
1917 function Get_Explicit_Discriminant_Value (D : Entity_Id) return Node_Id;
1918 -- If the ancestor part is an unconstrained type and further ancestors
1919 -- do not provide discriminants for it, check aggregate components for
1920 -- values of the discriminants.
1922 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id);
1923 -- If Typ is derived, and constrains discriminants of the parent type,
1924 -- these discriminants are not components of the aggregate, and must be
1925 -- initialized. The assignments are appended to List. The same is done
1926 -- if Typ derives fron an already constrained subtype of a discriminated
1927 -- parent type.
1929 procedure Init_Stored_Discriminants;
1930 -- If the type is derived and has inherited discriminants, generate
1931 -- explicit assignments for each, using the store constraint of the
1932 -- type. Note that both visible and stored discriminants must be
1933 -- initialized in case the derived type has some renamed and some
1934 -- constrained discriminants.
1936 procedure Init_Visible_Discriminants;
1937 -- If type has discriminants, retrieve their values from aggregate,
1938 -- and generate explicit assignments for each. This does not include
1939 -- discriminants inherited from ancestor, which are handled above.
1940 -- The type of the aggregate is a subtype created ealier using the
1941 -- given values of the discriminant components of the aggregate.
1943 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean;
1944 -- Check whether Bounds is a range node and its lower and higher bounds
1945 -- are integers literals.
1947 ---------------------------------
1948 -- Ancestor_Discriminant_Value --
1949 ---------------------------------
1951 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id is
1952 Assoc : Node_Id;
1953 Assoc_Elmt : Elmt_Id;
1954 Aggr_Comp : Entity_Id;
1955 Corresp_Disc : Entity_Id;
1956 Current_Typ : Entity_Id := Base_Type (Typ);
1957 Parent_Typ : Entity_Id;
1958 Parent_Disc : Entity_Id;
1959 Save_Assoc : Node_Id := Empty;
1961 begin
1962 -- First check any discriminant associations to see if any of them
1963 -- provide a value for the discriminant.
1965 if Present (Discriminant_Specifications (Parent (Current_Typ))) then
1966 Assoc := First (Component_Associations (N));
1967 while Present (Assoc) loop
1968 Aggr_Comp := Entity (First (Choices (Assoc)));
1970 if Ekind (Aggr_Comp) = E_Discriminant then
1971 Save_Assoc := Expression (Assoc);
1973 Corresp_Disc := Corresponding_Discriminant (Aggr_Comp);
1974 while Present (Corresp_Disc) loop
1976 -- If found a corresponding discriminant then return the
1977 -- value given in the aggregate. (Note: this is not
1978 -- correct in the presence of side effects. ???)
1980 if Disc = Corresp_Disc then
1981 return Duplicate_Subexpr (Expression (Assoc));
1982 end if;
1984 Corresp_Disc := Corresponding_Discriminant (Corresp_Disc);
1985 end loop;
1986 end if;
1988 Next (Assoc);
1989 end loop;
1990 end if;
1992 -- No match found in aggregate, so chain up parent types to find
1993 -- a constraint that defines the value of the discriminant.
1995 Parent_Typ := Etype (Current_Typ);
1996 while Current_Typ /= Parent_Typ loop
1997 if Has_Discriminants (Parent_Typ)
1998 and then not Has_Unknown_Discriminants (Parent_Typ)
1999 then
2000 Parent_Disc := First_Discriminant (Parent_Typ);
2002 -- We either get the association from the subtype indication
2003 -- of the type definition itself, or from the discriminant
2004 -- constraint associated with the type entity (which is
2005 -- preferable, but it's not always present ???)
2007 if Is_Empty_Elmt_List (Discriminant_Constraint (Current_Typ))
2008 then
2009 Assoc := Get_Constraint_Association (Current_Typ);
2010 Assoc_Elmt := No_Elmt;
2011 else
2012 Assoc_Elmt :=
2013 First_Elmt (Discriminant_Constraint (Current_Typ));
2014 Assoc := Node (Assoc_Elmt);
2015 end if;
2017 -- Traverse the discriminants of the parent type looking
2018 -- for one that corresponds.
2020 while Present (Parent_Disc) and then Present (Assoc) loop
2021 Corresp_Disc := Parent_Disc;
2022 while Present (Corresp_Disc)
2023 and then Disc /= Corresp_Disc
2024 loop
2025 Corresp_Disc := Corresponding_Discriminant (Corresp_Disc);
2026 end loop;
2028 if Disc = Corresp_Disc then
2029 if Nkind (Assoc) = N_Discriminant_Association then
2030 Assoc := Expression (Assoc);
2031 end if;
2033 -- If the located association directly denotes
2034 -- a discriminant, then use the value of a saved
2035 -- association of the aggregate. This is an approach
2036 -- used to handle certain cases involving multiple
2037 -- discriminants mapped to a single discriminant of
2038 -- a descendant. It's not clear how to locate the
2039 -- appropriate discriminant value for such cases. ???
2041 if Is_Entity_Name (Assoc)
2042 and then Ekind (Entity (Assoc)) = E_Discriminant
2043 then
2044 Assoc := Save_Assoc;
2045 end if;
2047 return Duplicate_Subexpr (Assoc);
2048 end if;
2050 Next_Discriminant (Parent_Disc);
2052 if No (Assoc_Elmt) then
2053 Next (Assoc);
2055 else
2056 Next_Elmt (Assoc_Elmt);
2058 if Present (Assoc_Elmt) then
2059 Assoc := Node (Assoc_Elmt);
2060 else
2061 Assoc := Empty;
2062 end if;
2063 end if;
2064 end loop;
2065 end if;
2067 Current_Typ := Parent_Typ;
2068 Parent_Typ := Etype (Current_Typ);
2069 end loop;
2071 -- In some cases there's no ancestor value to locate (such as
2072 -- when an ancestor part given by an expression defines the
2073 -- discriminant value).
2075 return Empty;
2076 end Ancestor_Discriminant_Value;
2078 ----------------------------------
2079 -- Check_Ancestor_Discriminants --
2080 ----------------------------------
2082 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id) is
2083 Discr : Entity_Id;
2084 Disc_Value : Node_Id;
2085 Cond : Node_Id;
2087 begin
2088 Discr := First_Discriminant (Base_Type (Anc_Typ));
2089 while Present (Discr) loop
2090 Disc_Value := Ancestor_Discriminant_Value (Discr);
2092 if Present (Disc_Value) then
2093 Cond := Make_Op_Ne (Loc,
2094 Left_Opnd =>
2095 Make_Selected_Component (Loc,
2096 Prefix => New_Copy_Tree (Target),
2097 Selector_Name => New_Occurrence_Of (Discr, Loc)),
2098 Right_Opnd => Disc_Value);
2100 Append_To (L,
2101 Make_Raise_Constraint_Error (Loc,
2102 Condition => Cond,
2103 Reason => CE_Discriminant_Check_Failed));
2104 end if;
2106 Next_Discriminant (Discr);
2107 end loop;
2108 end Check_Ancestor_Discriminants;
2110 ---------------------------
2111 -- Compatible_Int_Bounds --
2112 ---------------------------
2114 function Compatible_Int_Bounds
2115 (Agg_Bounds : Node_Id;
2116 Typ_Bounds : Node_Id) return Boolean
2118 Agg_Lo : constant Uint := Intval (Low_Bound (Agg_Bounds));
2119 Agg_Hi : constant Uint := Intval (High_Bound (Agg_Bounds));
2120 Typ_Lo : constant Uint := Intval (Low_Bound (Typ_Bounds));
2121 Typ_Hi : constant Uint := Intval (High_Bound (Typ_Bounds));
2122 begin
2123 return Typ_Lo <= Agg_Lo and then Agg_Hi <= Typ_Hi;
2124 end Compatible_Int_Bounds;
2126 --------------------------------
2127 -- Get_Constraint_Association --
2128 --------------------------------
2130 function Get_Constraint_Association (T : Entity_Id) return Node_Id is
2131 Indic : Node_Id;
2132 Typ : Entity_Id;
2134 begin
2135 Typ := T;
2137 -- If type is private, get constraint from full view. This was
2138 -- previously done in an instance context, but is needed whenever
2139 -- the ancestor part has a discriminant, possibly inherited through
2140 -- multiple derivations.
2142 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
2143 Typ := Full_View (Typ);
2144 end if;
2146 Indic := Subtype_Indication (Type_Definition (Parent (Typ)));
2148 -- Verify that the subtype indication carries a constraint
2150 if Nkind (Indic) = N_Subtype_Indication
2151 and then Present (Constraint (Indic))
2152 then
2153 return First (Constraints (Constraint (Indic)));
2154 end if;
2156 return Empty;
2157 end Get_Constraint_Association;
2159 -------------------------------------
2160 -- Get_Explicit_Discriminant_Value --
2161 -------------------------------------
2163 function Get_Explicit_Discriminant_Value
2164 (D : Entity_Id) return Node_Id
2166 Assoc : Node_Id;
2167 Choice : Node_Id;
2168 Val : Node_Id;
2170 begin
2171 -- The aggregate has been normalized and all associations have a
2172 -- single choice.
2174 Assoc := First (Component_Associations (N));
2175 while Present (Assoc) loop
2176 Choice := First (Choices (Assoc));
2178 if Chars (Choice) = Chars (D) then
2179 Val := Expression (Assoc);
2180 Remove (Assoc);
2181 return Val;
2182 end if;
2184 Next (Assoc);
2185 end loop;
2187 return Empty;
2188 end Get_Explicit_Discriminant_Value;
2190 -------------------------------
2191 -- Init_Hidden_Discriminants --
2192 -------------------------------
2194 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id) is
2195 function Is_Completely_Hidden_Discriminant
2196 (Discr : Entity_Id) return Boolean;
2197 -- Determine whether Discr is a completely hidden discriminant of
2198 -- type Typ.
2200 ---------------------------------------
2201 -- Is_Completely_Hidden_Discriminant --
2202 ---------------------------------------
2204 function Is_Completely_Hidden_Discriminant
2205 (Discr : Entity_Id) return Boolean
2207 Item : Entity_Id;
2209 begin
2210 -- Use First/Next_Entity as First/Next_Discriminant do not yield
2211 -- completely hidden discriminants.
2213 Item := First_Entity (Typ);
2214 while Present (Item) loop
2215 if Ekind (Item) = E_Discriminant
2216 and then Is_Completely_Hidden (Item)
2217 and then Chars (Original_Record_Component (Item)) =
2218 Chars (Discr)
2219 then
2220 return True;
2221 end if;
2223 Next_Entity (Item);
2224 end loop;
2226 return False;
2227 end Is_Completely_Hidden_Discriminant;
2229 -- Local variables
2231 Base_Typ : Entity_Id;
2232 Discr : Entity_Id;
2233 Discr_Constr : Elmt_Id;
2234 Discr_Init : Node_Id;
2235 Discr_Val : Node_Id;
2236 In_Aggr_Type : Boolean;
2237 Par_Typ : Entity_Id;
2239 -- Start of processing for Init_Hidden_Discriminants
2241 begin
2242 -- The constraints on the hidden discriminants, if present, are kept
2243 -- in the Stored_Constraint list of the type itself, or in that of
2244 -- the base type. If not in the constraints of the aggregate itself,
2245 -- we examine ancestors to find discriminants that are not renamed
2246 -- by other discriminants but constrained explicitly.
2248 In_Aggr_Type := True;
2250 Base_Typ := Base_Type (Typ);
2251 while Is_Derived_Type (Base_Typ)
2252 and then
2253 (Present (Stored_Constraint (Base_Typ))
2254 or else
2255 (In_Aggr_Type and then Present (Stored_Constraint (Typ))))
2256 loop
2257 Par_Typ := Etype (Base_Typ);
2259 if not Has_Discriminants (Par_Typ) then
2260 return;
2261 end if;
2263 Discr := First_Discriminant (Par_Typ);
2265 -- We know that one of the stored-constraint lists is present
2267 if Present (Stored_Constraint (Base_Typ)) then
2268 Discr_Constr := First_Elmt (Stored_Constraint (Base_Typ));
2270 -- For private extension, stored constraint may be on full view
2272 elsif Is_Private_Type (Base_Typ)
2273 and then Present (Full_View (Base_Typ))
2274 and then Present (Stored_Constraint (Full_View (Base_Typ)))
2275 then
2276 Discr_Constr :=
2277 First_Elmt (Stored_Constraint (Full_View (Base_Typ)));
2279 else
2280 Discr_Constr := First_Elmt (Stored_Constraint (Typ));
2281 end if;
2283 while Present (Discr) and then Present (Discr_Constr) loop
2284 Discr_Val := Node (Discr_Constr);
2286 -- The parent discriminant is renamed in the derived type,
2287 -- nothing to initialize.
2289 -- type Deriv_Typ (Discr : ...)
2290 -- is new Parent_Typ (Discr => Discr);
2292 if Is_Entity_Name (Discr_Val)
2293 and then Ekind (Entity (Discr_Val)) = E_Discriminant
2294 then
2295 null;
2297 -- When the parent discriminant is constrained at the type
2298 -- extension level, it does not appear in the derived type.
2300 -- type Deriv_Typ (Discr : ...)
2301 -- is new Parent_Typ (Discr => Discr,
2302 -- Hidden_Discr => Expression);
2304 elsif Is_Completely_Hidden_Discriminant (Discr) then
2305 null;
2307 -- Otherwise initialize the discriminant
2309 else
2310 Discr_Init :=
2311 Make_OK_Assignment_Statement (Loc,
2312 Name =>
2313 Make_Selected_Component (Loc,
2314 Prefix => New_Copy_Tree (Target),
2315 Selector_Name => New_Occurrence_Of (Discr, Loc)),
2316 Expression => New_Copy_Tree (Discr_Val));
2318 Set_No_Ctrl_Actions (Discr_Init);
2319 Append_To (List, Discr_Init);
2320 end if;
2322 Next_Elmt (Discr_Constr);
2323 Next_Discriminant (Discr);
2324 end loop;
2326 In_Aggr_Type := False;
2327 Base_Typ := Base_Type (Par_Typ);
2328 end loop;
2329 end Init_Hidden_Discriminants;
2331 --------------------------------
2332 -- Init_Visible_Discriminants --
2333 --------------------------------
2335 procedure Init_Visible_Discriminants is
2336 Discriminant : Entity_Id;
2337 Discriminant_Value : Node_Id;
2339 begin
2340 Discriminant := First_Discriminant (Typ);
2341 while Present (Discriminant) loop
2342 Comp_Expr :=
2343 Make_Selected_Component (Loc,
2344 Prefix => New_Copy_Tree (Target),
2345 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2347 Discriminant_Value :=
2348 Get_Discriminant_Value
2349 (Discriminant, Typ, Discriminant_Constraint (N_Typ));
2351 Instr :=
2352 Make_OK_Assignment_Statement (Loc,
2353 Name => Comp_Expr,
2354 Expression => New_Copy_Tree (Discriminant_Value));
2356 Set_No_Ctrl_Actions (Instr);
2357 Append_To (L, Instr);
2359 Next_Discriminant (Discriminant);
2360 end loop;
2361 end Init_Visible_Discriminants;
2363 -------------------------------
2364 -- Init_Stored_Discriminants --
2365 -------------------------------
2367 procedure Init_Stored_Discriminants is
2368 Discriminant : Entity_Id;
2369 Discriminant_Value : Node_Id;
2371 begin
2372 Discriminant := First_Stored_Discriminant (Typ);
2373 while Present (Discriminant) loop
2374 Comp_Expr :=
2375 Make_Selected_Component (Loc,
2376 Prefix => New_Copy_Tree (Target),
2377 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2379 Discriminant_Value :=
2380 Get_Discriminant_Value
2381 (Discriminant, N_Typ, Discriminant_Constraint (N_Typ));
2383 Instr :=
2384 Make_OK_Assignment_Statement (Loc,
2385 Name => Comp_Expr,
2386 Expression => New_Copy_Tree (Discriminant_Value));
2388 Set_No_Ctrl_Actions (Instr);
2389 Append_To (L, Instr);
2391 Next_Stored_Discriminant (Discriminant);
2392 end loop;
2393 end Init_Stored_Discriminants;
2395 -------------------------
2396 -- Is_Int_Range_Bounds --
2397 -------------------------
2399 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean is
2400 begin
2401 return Nkind (Bounds) = N_Range
2402 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
2403 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal;
2404 end Is_Int_Range_Bounds;
2406 -----------------------------------
2407 -- Generate_Finalization_Actions --
2408 -----------------------------------
2410 procedure Generate_Finalization_Actions is
2411 begin
2412 -- Do the work only the first time this is called
2414 if Finalization_Done then
2415 return;
2416 end if;
2418 Finalization_Done := True;
2420 -- Determine the external finalization list. It is either the
2421 -- finalization list of the outer-scope or the one coming from an
2422 -- outer aggregate. When the target is not a temporary, the proper
2423 -- scope is the scope of the target rather than the potentially
2424 -- transient current scope.
2426 if Is_Controlled (Typ) and then Ancestor_Is_Subtype_Mark then
2427 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2428 Set_Assignment_OK (Ref);
2430 Append_To (L,
2431 Make_Procedure_Call_Statement (Loc,
2432 Name =>
2433 New_Occurrence_Of
2434 (Find_Prim_Op (Init_Typ, Name_Initialize), Loc),
2435 Parameter_Associations => New_List (New_Copy_Tree (Ref))));
2436 end if;
2437 end Generate_Finalization_Actions;
2439 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result;
2440 -- If default expression of a component mentions a discriminant of the
2441 -- type, it must be rewritten as the discriminant of the target object.
2443 function Replace_Type (Expr : Node_Id) return Traverse_Result;
2444 -- If the aggregate contains a self-reference, traverse each expression
2445 -- to replace a possible self-reference with a reference to the proper
2446 -- component of the target of the assignment.
2448 --------------------------
2449 -- Rewrite_Discriminant --
2450 --------------------------
2452 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result is
2453 begin
2454 if Is_Entity_Name (Expr)
2455 and then Present (Entity (Expr))
2456 and then Ekind (Entity (Expr)) = E_In_Parameter
2457 and then Present (Discriminal_Link (Entity (Expr)))
2458 and then Scope (Discriminal_Link (Entity (Expr))) =
2459 Base_Type (Etype (N))
2460 then
2461 Rewrite (Expr,
2462 Make_Selected_Component (Loc,
2463 Prefix => New_Copy_Tree (Lhs),
2464 Selector_Name => Make_Identifier (Loc, Chars (Expr))));
2465 end if;
2467 return OK;
2468 end Rewrite_Discriminant;
2470 ------------------
2471 -- Replace_Type --
2472 ------------------
2474 function Replace_Type (Expr : Node_Id) return Traverse_Result is
2475 begin
2476 -- Note regarding the Root_Type test below: Aggregate components for
2477 -- self-referential types include attribute references to the current
2478 -- instance, of the form: Typ'access, etc.. These references are
2479 -- rewritten as references to the target of the aggregate: the
2480 -- left-hand side of an assignment, the entity in a declaration,
2481 -- or a temporary. Without this test, we would improperly extended
2482 -- this rewriting to attribute references whose prefix was not the
2483 -- type of the aggregate.
2485 if Nkind (Expr) = N_Attribute_Reference
2486 and then Is_Entity_Name (Prefix (Expr))
2487 and then Is_Type (Entity (Prefix (Expr)))
2488 and then Root_Type (Etype (N)) = Root_Type (Entity (Prefix (Expr)))
2489 then
2490 if Is_Entity_Name (Lhs) then
2491 Rewrite (Prefix (Expr),
2492 New_Occurrence_Of (Entity (Lhs), Loc));
2494 elsif Nkind (Lhs) = N_Selected_Component then
2495 Rewrite (Expr,
2496 Make_Attribute_Reference (Loc,
2497 Attribute_Name => Name_Unrestricted_Access,
2498 Prefix => New_Copy_Tree (Lhs)));
2499 Set_Analyzed (Parent (Expr), False);
2501 else
2502 Rewrite (Expr,
2503 Make_Attribute_Reference (Loc,
2504 Attribute_Name => Name_Unrestricted_Access,
2505 Prefix => New_Copy_Tree (Lhs)));
2506 Set_Analyzed (Parent (Expr), False);
2507 end if;
2508 end if;
2510 return OK;
2511 end Replace_Type;
2513 procedure Replace_Self_Reference is
2514 new Traverse_Proc (Replace_Type);
2516 procedure Replace_Discriminants is
2517 new Traverse_Proc (Rewrite_Discriminant);
2519 -- Start of processing for Build_Record_Aggr_Code
2521 begin
2522 if Has_Self_Reference (N) then
2523 Replace_Self_Reference (N);
2524 end if;
2526 -- If the target of the aggregate is class-wide, we must convert it
2527 -- to the actual type of the aggregate, so that the proper components
2528 -- are visible. We know already that the types are compatible.
2530 if Present (Etype (Lhs))
2531 and then Is_Class_Wide_Type (Etype (Lhs))
2532 then
2533 Target := Unchecked_Convert_To (Typ, Lhs);
2534 else
2535 Target := Lhs;
2536 end if;
2538 -- Deal with the ancestor part of extension aggregates or with the
2539 -- discriminants of the root type.
2541 if Nkind (N) = N_Extension_Aggregate then
2542 declare
2543 Ancestor : constant Node_Id := Ancestor_Part (N);
2544 Assign : List_Id;
2546 begin
2547 -- If the ancestor part is a subtype mark "T", we generate
2549 -- init-proc (T (tmp)); if T is constrained and
2550 -- init-proc (S (tmp)); where S applies an appropriate
2551 -- constraint if T is unconstrained
2553 if Is_Entity_Name (Ancestor)
2554 and then Is_Type (Entity (Ancestor))
2555 then
2556 Ancestor_Is_Subtype_Mark := True;
2558 if Is_Constrained (Entity (Ancestor)) then
2559 Init_Typ := Entity (Ancestor);
2561 -- For an ancestor part given by an unconstrained type mark,
2562 -- create a subtype constrained by appropriate corresponding
2563 -- discriminant values coming from either associations of the
2564 -- aggregate or a constraint on a parent type. The subtype will
2565 -- be used to generate the correct default value for the
2566 -- ancestor part.
2568 elsif Has_Discriminants (Entity (Ancestor)) then
2569 declare
2570 Anc_Typ : constant Entity_Id := Entity (Ancestor);
2571 Anc_Constr : constant List_Id := New_List;
2572 Discrim : Entity_Id;
2573 Disc_Value : Node_Id;
2574 New_Indic : Node_Id;
2575 Subt_Decl : Node_Id;
2577 begin
2578 Discrim := First_Discriminant (Anc_Typ);
2579 while Present (Discrim) loop
2580 Disc_Value := Ancestor_Discriminant_Value (Discrim);
2582 -- If no usable discriminant in ancestors, check
2583 -- whether aggregate has an explicit value for it.
2585 if No (Disc_Value) then
2586 Disc_Value :=
2587 Get_Explicit_Discriminant_Value (Discrim);
2588 end if;
2590 Append_To (Anc_Constr, Disc_Value);
2591 Next_Discriminant (Discrim);
2592 end loop;
2594 New_Indic :=
2595 Make_Subtype_Indication (Loc,
2596 Subtype_Mark => New_Occurrence_Of (Anc_Typ, Loc),
2597 Constraint =>
2598 Make_Index_Or_Discriminant_Constraint (Loc,
2599 Constraints => Anc_Constr));
2601 Init_Typ := Create_Itype (Ekind (Anc_Typ), N);
2603 Subt_Decl :=
2604 Make_Subtype_Declaration (Loc,
2605 Defining_Identifier => Init_Typ,
2606 Subtype_Indication => New_Indic);
2608 -- Itypes must be analyzed with checks off Declaration
2609 -- must have a parent for proper handling of subsidiary
2610 -- actions.
2612 Set_Parent (Subt_Decl, N);
2613 Analyze (Subt_Decl, Suppress => All_Checks);
2614 end;
2615 end if;
2617 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2618 Set_Assignment_OK (Ref);
2620 if not Is_Interface (Init_Typ) then
2621 Append_List_To (L,
2622 Build_Initialization_Call (Loc,
2623 Id_Ref => Ref,
2624 Typ => Init_Typ,
2625 In_Init_Proc => Within_Init_Proc,
2626 With_Default_Init => Has_Default_Init_Comps (N)
2627 or else
2628 Has_Task (Base_Type (Init_Typ))));
2630 if Is_Constrained (Entity (Ancestor))
2631 and then Has_Discriminants (Entity (Ancestor))
2632 then
2633 Check_Ancestor_Discriminants (Entity (Ancestor));
2634 end if;
2635 end if;
2637 -- Handle calls to C++ constructors
2639 elsif Is_CPP_Constructor_Call (Ancestor) then
2640 Init_Typ := Etype (Ancestor);
2641 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2642 Set_Assignment_OK (Ref);
2644 Append_List_To (L,
2645 Build_Initialization_Call (Loc,
2646 Id_Ref => Ref,
2647 Typ => Init_Typ,
2648 In_Init_Proc => Within_Init_Proc,
2649 With_Default_Init => Has_Default_Init_Comps (N),
2650 Constructor_Ref => Ancestor));
2652 -- Ada 2005 (AI-287): If the ancestor part is an aggregate of
2653 -- limited type, a recursive call expands the ancestor. Note that
2654 -- in the limited case, the ancestor part must be either a
2655 -- function call (possibly qualified, or wrapped in an unchecked
2656 -- conversion) or aggregate (definitely qualified).
2658 -- The ancestor part can also be a function call (that may be
2659 -- transformed into an explicit dereference) or a qualification
2660 -- of one such.
2662 elsif Is_Limited_Type (Etype (Ancestor))
2663 and then Nkind_In (Unqualify (Ancestor), N_Aggregate,
2664 N_Extension_Aggregate)
2665 then
2666 Ancestor_Is_Expression := True;
2668 -- Set up finalization data for enclosing record, because
2669 -- controlled subcomponents of the ancestor part will be
2670 -- attached to it.
2672 Generate_Finalization_Actions;
2674 Append_List_To (L,
2675 Build_Record_Aggr_Code
2676 (N => Unqualify (Ancestor),
2677 Typ => Etype (Unqualify (Ancestor)),
2678 Lhs => Target));
2680 -- If the ancestor part is an expression "E", we generate
2682 -- T (tmp) := E;
2684 -- In Ada 2005, this includes the case of a (possibly qualified)
2685 -- limited function call. The assignment will turn into a
2686 -- build-in-place function call (for further details, see
2687 -- Make_Build_In_Place_Call_In_Assignment).
2689 else
2690 Ancestor_Is_Expression := True;
2691 Init_Typ := Etype (Ancestor);
2693 -- If the ancestor part is an aggregate, force its full
2694 -- expansion, which was delayed.
2696 if Nkind_In (Unqualify (Ancestor), N_Aggregate,
2697 N_Extension_Aggregate)
2698 then
2699 Set_Analyzed (Ancestor, False);
2700 Set_Analyzed (Expression (Ancestor), False);
2701 end if;
2703 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2704 Set_Assignment_OK (Ref);
2706 -- Make the assignment without usual controlled actions, since
2707 -- we only want to Adjust afterwards, but not to Finalize
2708 -- beforehand. Add manual Adjust when necessary.
2710 Assign := New_List (
2711 Make_OK_Assignment_Statement (Loc,
2712 Name => Ref,
2713 Expression => Ancestor));
2714 Set_No_Ctrl_Actions (First (Assign));
2716 -- Assign the tag now to make sure that the dispatching call in
2717 -- the subsequent deep_adjust works properly (unless
2718 -- Tagged_Type_Expansion where tags are implicit).
2720 if Tagged_Type_Expansion then
2721 Instr :=
2722 Make_OK_Assignment_Statement (Loc,
2723 Name =>
2724 Make_Selected_Component (Loc,
2725 Prefix => New_Copy_Tree (Target),
2726 Selector_Name =>
2727 New_Occurrence_Of
2728 (First_Tag_Component (Base_Type (Typ)), Loc)),
2730 Expression =>
2731 Unchecked_Convert_To (RTE (RE_Tag),
2732 New_Occurrence_Of
2733 (Node (First_Elmt
2734 (Access_Disp_Table (Base_Type (Typ)))),
2735 Loc)));
2737 Set_Assignment_OK (Name (Instr));
2738 Append_To (Assign, Instr);
2740 -- Ada 2005 (AI-251): If tagged type has progenitors we must
2741 -- also initialize tags of the secondary dispatch tables.
2743 if Has_Interfaces (Base_Type (Typ)) then
2744 Init_Secondary_Tags
2745 (Typ => Base_Type (Typ),
2746 Target => Target,
2747 Stmts_List => Assign);
2748 end if;
2749 end if;
2751 -- Call Adjust manually
2753 if Needs_Finalization (Etype (Ancestor))
2754 and then not Is_Limited_Type (Etype (Ancestor))
2755 then
2756 Append_To (Assign,
2757 Make_Adjust_Call
2758 (Obj_Ref => New_Copy_Tree (Ref),
2759 Typ => Etype (Ancestor)));
2760 end if;
2762 Append_To (L,
2763 Make_Unsuppress_Block (Loc, Name_Discriminant_Check, Assign));
2765 if Has_Discriminants (Init_Typ) then
2766 Check_Ancestor_Discriminants (Init_Typ);
2767 end if;
2768 end if;
2769 end;
2771 -- Generate assignments of hidden discriminants. If the base type is
2772 -- an unchecked union, the discriminants are unknown to the back-end
2773 -- and absent from a value of the type, so assignments for them are
2774 -- not emitted.
2776 if Has_Discriminants (Typ)
2777 and then not Is_Unchecked_Union (Base_Type (Typ))
2778 then
2779 Init_Hidden_Discriminants (Typ, L);
2780 end if;
2782 -- Normal case (not an extension aggregate)
2784 else
2785 -- Generate the discriminant expressions, component by component.
2786 -- If the base type is an unchecked union, the discriminants are
2787 -- unknown to the back-end and absent from a value of the type, so
2788 -- assignments for them are not emitted.
2790 if Has_Discriminants (Typ)
2791 and then not Is_Unchecked_Union (Base_Type (Typ))
2792 then
2793 Init_Hidden_Discriminants (Typ, L);
2795 -- Generate discriminant init values for the visible discriminants
2797 Init_Visible_Discriminants;
2799 if Is_Derived_Type (N_Typ) then
2800 Init_Stored_Discriminants;
2801 end if;
2802 end if;
2803 end if;
2805 -- For CPP types we generate an implicit call to the C++ default
2806 -- constructor to ensure the proper initialization of the _Tag
2807 -- component.
2809 if Is_CPP_Class (Root_Type (Typ)) and then CPP_Num_Prims (Typ) > 0 then
2810 Invoke_Constructor : declare
2811 CPP_Parent : constant Entity_Id := Enclosing_CPP_Parent (Typ);
2813 procedure Invoke_IC_Proc (T : Entity_Id);
2814 -- Recursive routine used to climb to parents. Required because
2815 -- parents must be initialized before descendants to ensure
2816 -- propagation of inherited C++ slots.
2818 --------------------
2819 -- Invoke_IC_Proc --
2820 --------------------
2822 procedure Invoke_IC_Proc (T : Entity_Id) is
2823 begin
2824 -- Avoid generating extra calls. Initialization required
2825 -- only for types defined from the level of derivation of
2826 -- type of the constructor and the type of the aggregate.
2828 if T = CPP_Parent then
2829 return;
2830 end if;
2832 Invoke_IC_Proc (Etype (T));
2834 -- Generate call to the IC routine
2836 if Present (CPP_Init_Proc (T)) then
2837 Append_To (L,
2838 Make_Procedure_Call_Statement (Loc,
2839 Name => New_Occurrence_Of (CPP_Init_Proc (T), Loc)));
2840 end if;
2841 end Invoke_IC_Proc;
2843 -- Start of processing for Invoke_Constructor
2845 begin
2846 -- Implicit invocation of the C++ constructor
2848 if Nkind (N) = N_Aggregate then
2849 Append_To (L,
2850 Make_Procedure_Call_Statement (Loc,
2851 Name =>
2852 New_Occurrence_Of (Base_Init_Proc (CPP_Parent), Loc),
2853 Parameter_Associations => New_List (
2854 Unchecked_Convert_To (CPP_Parent,
2855 New_Copy_Tree (Lhs)))));
2856 end if;
2858 Invoke_IC_Proc (Typ);
2859 end Invoke_Constructor;
2860 end if;
2862 -- Generate the assignments, component by component
2864 -- tmp.comp1 := Expr1_From_Aggr;
2865 -- tmp.comp2 := Expr2_From_Aggr;
2866 -- ....
2868 Comp := First (Component_Associations (N));
2869 while Present (Comp) loop
2870 Selector := Entity (First (Choices (Comp)));
2872 -- C++ constructors
2874 if Is_CPP_Constructor_Call (Expression (Comp)) then
2875 Append_List_To (L,
2876 Build_Initialization_Call (Loc,
2877 Id_Ref =>
2878 Make_Selected_Component (Loc,
2879 Prefix => New_Copy_Tree (Target),
2880 Selector_Name => New_Occurrence_Of (Selector, Loc)),
2881 Typ => Etype (Selector),
2882 Enclos_Type => Typ,
2883 With_Default_Init => True,
2884 Constructor_Ref => Expression (Comp)));
2886 -- Ada 2005 (AI-287): For each default-initialized component generate
2887 -- a call to the corresponding IP subprogram if available.
2889 elsif Box_Present (Comp)
2890 and then Has_Non_Null_Base_Init_Proc (Etype (Selector))
2891 then
2892 if Ekind (Selector) /= E_Discriminant then
2893 Generate_Finalization_Actions;
2894 end if;
2896 -- Ada 2005 (AI-287): If the component type has tasks then
2897 -- generate the activation chain and master entities (except
2898 -- in case of an allocator because in that case these entities
2899 -- are generated by Build_Task_Allocate_Block_With_Init_Stmts).
2901 declare
2902 Ctype : constant Entity_Id := Etype (Selector);
2903 Inside_Allocator : Boolean := False;
2904 P : Node_Id := Parent (N);
2906 begin
2907 if Is_Task_Type (Ctype) or else Has_Task (Ctype) then
2908 while Present (P) loop
2909 if Nkind (P) = N_Allocator then
2910 Inside_Allocator := True;
2911 exit;
2912 end if;
2914 P := Parent (P);
2915 end loop;
2917 if not Inside_Init_Proc and not Inside_Allocator then
2918 Build_Activation_Chain_Entity (N);
2919 end if;
2920 end if;
2921 end;
2923 Append_List_To (L,
2924 Build_Initialization_Call (Loc,
2925 Id_Ref => Make_Selected_Component (Loc,
2926 Prefix => New_Copy_Tree (Target),
2927 Selector_Name =>
2928 New_Occurrence_Of (Selector, Loc)),
2929 Typ => Etype (Selector),
2930 Enclos_Type => Typ,
2931 With_Default_Init => True));
2933 -- Prepare for component assignment
2935 elsif Ekind (Selector) /= E_Discriminant
2936 or else Nkind (N) = N_Extension_Aggregate
2937 then
2938 -- All the discriminants have now been assigned
2940 -- This is now a good moment to initialize and attach all the
2941 -- controllers. Their position may depend on the discriminants.
2943 if Ekind (Selector) /= E_Discriminant then
2944 Generate_Finalization_Actions;
2945 end if;
2947 Comp_Type := Underlying_Type (Etype (Selector));
2948 Comp_Expr :=
2949 Make_Selected_Component (Loc,
2950 Prefix => New_Copy_Tree (Target),
2951 Selector_Name => New_Occurrence_Of (Selector, Loc));
2953 if Nkind (Expression (Comp)) = N_Qualified_Expression then
2954 Expr_Q := Expression (Expression (Comp));
2955 else
2956 Expr_Q := Expression (Comp);
2957 end if;
2959 -- Now either create the assignment or generate the code for the
2960 -- inner aggregate top-down.
2962 if Is_Delayed_Aggregate (Expr_Q) then
2964 -- We have the following case of aggregate nesting inside
2965 -- an object declaration:
2967 -- type Arr_Typ is array (Integer range <>) of ...;
2969 -- type Rec_Typ (...) is record
2970 -- Obj_Arr_Typ : Arr_Typ (A .. B);
2971 -- end record;
2973 -- Obj_Rec_Typ : Rec_Typ := (...,
2974 -- Obj_Arr_Typ => (X => (...), Y => (...)));
2976 -- The length of the ranges of the aggregate and Obj_Add_Typ
2977 -- are equal (B - A = Y - X), but they do not coincide (X /=
2978 -- A and B /= Y). This case requires array sliding which is
2979 -- performed in the following manner:
2981 -- subtype Arr_Sub is Arr_Typ (X .. Y);
2982 -- Temp : Arr_Sub;
2983 -- Temp (X) := (...);
2984 -- ...
2985 -- Temp (Y) := (...);
2986 -- Obj_Rec_Typ.Obj_Arr_Typ := Temp;
2988 if Ekind (Comp_Type) = E_Array_Subtype
2989 and then Is_Int_Range_Bounds (Aggregate_Bounds (Expr_Q))
2990 and then Is_Int_Range_Bounds (First_Index (Comp_Type))
2991 and then not
2992 Compatible_Int_Bounds
2993 (Agg_Bounds => Aggregate_Bounds (Expr_Q),
2994 Typ_Bounds => First_Index (Comp_Type))
2995 then
2996 -- Create the array subtype with bounds equal to those of
2997 -- the corresponding aggregate.
2999 declare
3000 SubE : constant Entity_Id := Make_Temporary (Loc, 'T');
3002 SubD : constant Node_Id :=
3003 Make_Subtype_Declaration (Loc,
3004 Defining_Identifier => SubE,
3005 Subtype_Indication =>
3006 Make_Subtype_Indication (Loc,
3007 Subtype_Mark =>
3008 New_Occurrence_Of (Etype (Comp_Type), Loc),
3009 Constraint =>
3010 Make_Index_Or_Discriminant_Constraint
3011 (Loc,
3012 Constraints => New_List (
3013 New_Copy_Tree
3014 (Aggregate_Bounds (Expr_Q))))));
3016 -- Create a temporary array of the above subtype which
3017 -- will be used to capture the aggregate assignments.
3019 TmpE : constant Entity_Id := Make_Temporary (Loc, 'A', N);
3021 TmpD : constant Node_Id :=
3022 Make_Object_Declaration (Loc,
3023 Defining_Identifier => TmpE,
3024 Object_Definition => New_Occurrence_Of (SubE, Loc));
3026 begin
3027 Set_No_Initialization (TmpD);
3028 Append_To (L, SubD);
3029 Append_To (L, TmpD);
3031 -- Expand aggregate into assignments to the temp array
3033 Append_List_To (L,
3034 Late_Expansion (Expr_Q, Comp_Type,
3035 New_Occurrence_Of (TmpE, Loc)));
3037 -- Slide
3039 Append_To (L,
3040 Make_Assignment_Statement (Loc,
3041 Name => New_Copy_Tree (Comp_Expr),
3042 Expression => New_Occurrence_Of (TmpE, Loc)));
3043 end;
3045 -- Normal case (sliding not required)
3047 else
3048 Append_List_To (L,
3049 Late_Expansion (Expr_Q, Comp_Type, Comp_Expr));
3050 end if;
3052 -- Expr_Q is not delayed aggregate
3054 else
3055 if Has_Discriminants (Typ) then
3056 Replace_Discriminants (Expr_Q);
3058 -- If the component is an array type that depends on
3059 -- discriminants, and the expression is a single Others
3060 -- clause, create an explicit subtype for it because the
3061 -- backend has troubles recovering the actual bounds.
3063 if Nkind (Expr_Q) = N_Aggregate
3064 and then Is_Array_Type (Comp_Type)
3065 and then Present (Component_Associations (Expr_Q))
3066 then
3067 declare
3068 Assoc : constant Node_Id :=
3069 First (Component_Associations (Expr_Q));
3070 Decl : Node_Id;
3072 begin
3073 if Nkind (First (Choices (Assoc))) = N_Others_Choice
3074 then
3075 Decl :=
3076 Build_Actual_Subtype_Of_Component
3077 (Comp_Type, Comp_Expr);
3079 -- If the component type does not in fact depend on
3080 -- discriminants, the subtype declaration is empty.
3082 if Present (Decl) then
3083 Append_To (L, Decl);
3084 Set_Etype (Comp_Expr, Defining_Entity (Decl));
3085 end if;
3086 end if;
3087 end;
3088 end if;
3089 end if;
3091 if Generate_C_Code
3092 and then Nkind (Expr_Q) = N_Aggregate
3093 and then Is_Array_Type (Etype (Expr_Q))
3094 and then Present (First_Index (Etype (Expr_Q)))
3095 then
3096 declare
3097 Expr_Q_Type : constant Node_Id := Etype (Expr_Q);
3098 begin
3099 Append_List_To (L,
3100 Build_Array_Aggr_Code
3101 (N => Expr_Q,
3102 Ctype => Component_Type (Expr_Q_Type),
3103 Index => First_Index (Expr_Q_Type),
3104 Into => Comp_Expr,
3105 Scalar_Comp => Is_Scalar_Type
3106 (Component_Type (Expr_Q_Type))));
3107 end;
3109 else
3110 Instr :=
3111 Make_OK_Assignment_Statement (Loc,
3112 Name => Comp_Expr,
3113 Expression => Expr_Q);
3115 Set_No_Ctrl_Actions (Instr);
3116 Append_To (L, Instr);
3117 end if;
3119 -- Adjust the tag if tagged (because of possible view
3120 -- conversions), unless compiling for a VM where tags are
3121 -- implicit.
3123 -- tmp.comp._tag := comp_typ'tag;
3125 if Is_Tagged_Type (Comp_Type)
3126 and then Tagged_Type_Expansion
3127 then
3128 Instr :=
3129 Make_OK_Assignment_Statement (Loc,
3130 Name =>
3131 Make_Selected_Component (Loc,
3132 Prefix => New_Copy_Tree (Comp_Expr),
3133 Selector_Name =>
3134 New_Occurrence_Of
3135 (First_Tag_Component (Comp_Type), Loc)),
3137 Expression =>
3138 Unchecked_Convert_To (RTE (RE_Tag),
3139 New_Occurrence_Of
3140 (Node (First_Elmt (Access_Disp_Table (Comp_Type))),
3141 Loc)));
3143 Append_To (L, Instr);
3144 end if;
3146 -- Generate:
3147 -- Adjust (tmp.comp);
3149 if Needs_Finalization (Comp_Type)
3150 and then not Is_Limited_Type (Comp_Type)
3151 then
3152 Append_To (L,
3153 Make_Adjust_Call
3154 (Obj_Ref => New_Copy_Tree (Comp_Expr),
3155 Typ => Comp_Type));
3156 end if;
3157 end if;
3159 -- comment would be good here ???
3161 elsif Ekind (Selector) = E_Discriminant
3162 and then Nkind (N) /= N_Extension_Aggregate
3163 and then Nkind (Parent (N)) = N_Component_Association
3164 and then Is_Constrained (Typ)
3165 then
3166 -- We must check that the discriminant value imposed by the
3167 -- context is the same as the value given in the subaggregate,
3168 -- because after the expansion into assignments there is no
3169 -- record on which to perform a regular discriminant check.
3171 declare
3172 D_Val : Elmt_Id;
3173 Disc : Entity_Id;
3175 begin
3176 D_Val := First_Elmt (Discriminant_Constraint (Typ));
3177 Disc := First_Discriminant (Typ);
3178 while Chars (Disc) /= Chars (Selector) loop
3179 Next_Discriminant (Disc);
3180 Next_Elmt (D_Val);
3181 end loop;
3183 pragma Assert (Present (D_Val));
3185 -- This check cannot performed for components that are
3186 -- constrained by a current instance, because this is not a
3187 -- value that can be compared with the actual constraint.
3189 if Nkind (Node (D_Val)) /= N_Attribute_Reference
3190 or else not Is_Entity_Name (Prefix (Node (D_Val)))
3191 or else not Is_Type (Entity (Prefix (Node (D_Val))))
3192 then
3193 Append_To (L,
3194 Make_Raise_Constraint_Error (Loc,
3195 Condition =>
3196 Make_Op_Ne (Loc,
3197 Left_Opnd => New_Copy_Tree (Node (D_Val)),
3198 Right_Opnd => Expression (Comp)),
3199 Reason => CE_Discriminant_Check_Failed));
3201 else
3202 -- Find self-reference in previous discriminant assignment,
3203 -- and replace with proper expression.
3205 declare
3206 Ass : Node_Id;
3208 begin
3209 Ass := First (L);
3210 while Present (Ass) loop
3211 if Nkind (Ass) = N_Assignment_Statement
3212 and then Nkind (Name (Ass)) = N_Selected_Component
3213 and then Chars (Selector_Name (Name (Ass))) =
3214 Chars (Disc)
3215 then
3216 Set_Expression
3217 (Ass, New_Copy_Tree (Expression (Comp)));
3218 exit;
3219 end if;
3220 Next (Ass);
3221 end loop;
3222 end;
3223 end if;
3224 end;
3225 end if;
3227 Next (Comp);
3228 end loop;
3230 -- If the type is tagged, the tag needs to be initialized (unless we
3231 -- are in VM-mode where tags are implicit). It is done late in the
3232 -- initialization process because in some cases, we call the init
3233 -- proc of an ancestor which will not leave out the right tag.
3235 if Ancestor_Is_Expression then
3236 null;
3238 -- For CPP types we generated a call to the C++ default constructor
3239 -- before the components have been initialized to ensure the proper
3240 -- initialization of the _Tag component (see above).
3242 elsif Is_CPP_Class (Typ) then
3243 null;
3245 elsif Is_Tagged_Type (Typ) and then Tagged_Type_Expansion then
3246 Instr :=
3247 Make_OK_Assignment_Statement (Loc,
3248 Name =>
3249 Make_Selected_Component (Loc,
3250 Prefix => New_Copy_Tree (Target),
3251 Selector_Name =>
3252 New_Occurrence_Of
3253 (First_Tag_Component (Base_Type (Typ)), Loc)),
3255 Expression =>
3256 Unchecked_Convert_To (RTE (RE_Tag),
3257 New_Occurrence_Of
3258 (Node (First_Elmt (Access_Disp_Table (Base_Type (Typ)))),
3259 Loc)));
3261 Append_To (L, Instr);
3263 -- Ada 2005 (AI-251): If the tagged type has been derived from an
3264 -- abstract interfaces we must also initialize the tags of the
3265 -- secondary dispatch tables.
3267 if Has_Interfaces (Base_Type (Typ)) then
3268 Init_Secondary_Tags
3269 (Typ => Base_Type (Typ),
3270 Target => Target,
3271 Stmts_List => L);
3272 end if;
3273 end if;
3275 -- If the controllers have not been initialized yet (by lack of non-
3276 -- discriminant components), let's do it now.
3278 Generate_Finalization_Actions;
3280 return L;
3281 end Build_Record_Aggr_Code;
3283 ---------------------------------------
3284 -- Collect_Initialization_Statements --
3285 ---------------------------------------
3287 procedure Collect_Initialization_Statements
3288 (Obj : Entity_Id;
3289 N : Node_Id;
3290 Node_After : Node_Id)
3292 Loc : constant Source_Ptr := Sloc (N);
3293 Init_Actions : constant List_Id := New_List;
3294 Init_Node : Node_Id;
3295 Comp_Stmt : Node_Id;
3297 begin
3298 -- Nothing to do if Obj is already frozen, as in this case we known we
3299 -- won't need to move the initialization statements about later on.
3301 if Is_Frozen (Obj) then
3302 return;
3303 end if;
3305 Init_Node := N;
3306 while Next (Init_Node) /= Node_After loop
3307 Append_To (Init_Actions, Remove_Next (Init_Node));
3308 end loop;
3310 if not Is_Empty_List (Init_Actions) then
3311 Comp_Stmt := Make_Compound_Statement (Loc, Actions => Init_Actions);
3312 Insert_Action_After (Init_Node, Comp_Stmt);
3313 Set_Initialization_Statements (Obj, Comp_Stmt);
3314 end if;
3315 end Collect_Initialization_Statements;
3317 -------------------------------
3318 -- Convert_Aggr_In_Allocator --
3319 -------------------------------
3321 procedure Convert_Aggr_In_Allocator
3322 (Alloc : Node_Id;
3323 Decl : Node_Id;
3324 Aggr : Node_Id)
3326 Loc : constant Source_Ptr := Sloc (Aggr);
3327 Typ : constant Entity_Id := Etype (Aggr);
3328 Temp : constant Entity_Id := Defining_Identifier (Decl);
3330 Occ : constant Node_Id :=
3331 Unchecked_Convert_To (Typ,
3332 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Temp, Loc)));
3334 begin
3335 if Is_Array_Type (Typ) then
3336 Convert_Array_Aggr_In_Allocator (Decl, Aggr, Occ);
3338 elsif Has_Default_Init_Comps (Aggr) then
3339 declare
3340 L : constant List_Id := New_List;
3341 Init_Stmts : List_Id;
3343 begin
3344 Init_Stmts := Late_Expansion (Aggr, Typ, Occ);
3346 if Has_Task (Typ) then
3347 Build_Task_Allocate_Block_With_Init_Stmts (L, Aggr, Init_Stmts);
3348 Insert_Actions (Alloc, L);
3349 else
3350 Insert_Actions (Alloc, Init_Stmts);
3351 end if;
3352 end;
3354 else
3355 Insert_Actions (Alloc, Late_Expansion (Aggr, Typ, Occ));
3356 end if;
3357 end Convert_Aggr_In_Allocator;
3359 --------------------------------
3360 -- Convert_Aggr_In_Assignment --
3361 --------------------------------
3363 procedure Convert_Aggr_In_Assignment (N : Node_Id) is
3364 Aggr : Node_Id := Expression (N);
3365 Typ : constant Entity_Id := Etype (Aggr);
3366 Occ : constant Node_Id := New_Copy_Tree (Name (N));
3368 begin
3369 if Nkind (Aggr) = N_Qualified_Expression then
3370 Aggr := Expression (Aggr);
3371 end if;
3373 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
3374 end Convert_Aggr_In_Assignment;
3376 ---------------------------------
3377 -- Convert_Aggr_In_Object_Decl --
3378 ---------------------------------
3380 procedure Convert_Aggr_In_Object_Decl (N : Node_Id) is
3381 Obj : constant Entity_Id := Defining_Identifier (N);
3382 Aggr : Node_Id := Expression (N);
3383 Loc : constant Source_Ptr := Sloc (Aggr);
3384 Typ : constant Entity_Id := Etype (Aggr);
3385 Occ : constant Node_Id := New_Occurrence_Of (Obj, Loc);
3387 function Discriminants_Ok return Boolean;
3388 -- If the object type is constrained, the discriminants in the
3389 -- aggregate must be checked against the discriminants of the subtype.
3390 -- This cannot be done using Apply_Discriminant_Checks because after
3391 -- expansion there is no aggregate left to check.
3393 ----------------------
3394 -- Discriminants_Ok --
3395 ----------------------
3397 function Discriminants_Ok return Boolean is
3398 Cond : Node_Id := Empty;
3399 Check : Node_Id;
3400 D : Entity_Id;
3401 Disc1 : Elmt_Id;
3402 Disc2 : Elmt_Id;
3403 Val1 : Node_Id;
3404 Val2 : Node_Id;
3406 begin
3407 D := First_Discriminant (Typ);
3408 Disc1 := First_Elmt (Discriminant_Constraint (Typ));
3409 Disc2 := First_Elmt (Discriminant_Constraint (Etype (Obj)));
3410 while Present (Disc1) and then Present (Disc2) loop
3411 Val1 := Node (Disc1);
3412 Val2 := Node (Disc2);
3414 if not Is_OK_Static_Expression (Val1)
3415 or else not Is_OK_Static_Expression (Val2)
3416 then
3417 Check := Make_Op_Ne (Loc,
3418 Left_Opnd => Duplicate_Subexpr (Val1),
3419 Right_Opnd => Duplicate_Subexpr (Val2));
3421 if No (Cond) then
3422 Cond := Check;
3424 else
3425 Cond := Make_Or_Else (Loc,
3426 Left_Opnd => Cond,
3427 Right_Opnd => Check);
3428 end if;
3430 elsif Expr_Value (Val1) /= Expr_Value (Val2) then
3431 Apply_Compile_Time_Constraint_Error (Aggr,
3432 Msg => "incorrect value for discriminant&??",
3433 Reason => CE_Discriminant_Check_Failed,
3434 Ent => D);
3435 return False;
3436 end if;
3438 Next_Discriminant (D);
3439 Next_Elmt (Disc1);
3440 Next_Elmt (Disc2);
3441 end loop;
3443 -- If any discriminant constraint is non-static, emit a check
3445 if Present (Cond) then
3446 Insert_Action (N,
3447 Make_Raise_Constraint_Error (Loc,
3448 Condition => Cond,
3449 Reason => CE_Discriminant_Check_Failed));
3450 end if;
3452 return True;
3453 end Discriminants_Ok;
3455 -- Start of processing for Convert_Aggr_In_Object_Decl
3457 begin
3458 Set_Assignment_OK (Occ);
3460 if Nkind (Aggr) = N_Qualified_Expression then
3461 Aggr := Expression (Aggr);
3462 end if;
3464 if Has_Discriminants (Typ)
3465 and then Typ /= Etype (Obj)
3466 and then Is_Constrained (Etype (Obj))
3467 and then not Discriminants_Ok
3468 then
3469 return;
3470 end if;
3472 -- If the context is an extended return statement, it has its own
3473 -- finalization machinery (i.e. works like a transient scope) and
3474 -- we do not want to create an additional one, because objects on
3475 -- the finalization list of the return must be moved to the caller's
3476 -- finalization list to complete the return.
3478 -- However, if the aggregate is limited, it is built in place, and the
3479 -- controlled components are not assigned to intermediate temporaries
3480 -- so there is no need for a transient scope in this case either.
3482 if Requires_Transient_Scope (Typ)
3483 and then Ekind (Current_Scope) /= E_Return_Statement
3484 and then not Is_Limited_Type (Typ)
3485 then
3486 Establish_Transient_Scope
3487 (Aggr,
3488 Sec_Stack =>
3489 Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
3490 end if;
3492 declare
3493 Node_After : constant Node_Id := Next (N);
3494 begin
3495 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
3496 Collect_Initialization_Statements (Obj, N, Node_After);
3497 end;
3498 Set_No_Initialization (N);
3499 Initialize_Discriminants (N, Typ);
3500 end Convert_Aggr_In_Object_Decl;
3502 -------------------------------------
3503 -- Convert_Array_Aggr_In_Allocator --
3504 -------------------------------------
3506 procedure Convert_Array_Aggr_In_Allocator
3507 (Decl : Node_Id;
3508 Aggr : Node_Id;
3509 Target : Node_Id)
3511 Aggr_Code : List_Id;
3512 Typ : constant Entity_Id := Etype (Aggr);
3513 Ctyp : constant Entity_Id := Component_Type (Typ);
3515 begin
3516 -- The target is an explicit dereference of the allocated object.
3517 -- Generate component assignments to it, as for an aggregate that
3518 -- appears on the right-hand side of an assignment statement.
3520 Aggr_Code :=
3521 Build_Array_Aggr_Code (Aggr,
3522 Ctype => Ctyp,
3523 Index => First_Index (Typ),
3524 Into => Target,
3525 Scalar_Comp => Is_Scalar_Type (Ctyp));
3527 Insert_Actions_After (Decl, Aggr_Code);
3528 end Convert_Array_Aggr_In_Allocator;
3530 ----------------------------
3531 -- Convert_To_Assignments --
3532 ----------------------------
3534 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id) is
3535 Loc : constant Source_Ptr := Sloc (N);
3536 T : Entity_Id;
3537 Temp : Entity_Id;
3539 Aggr_Code : List_Id;
3540 Instr : Node_Id;
3541 Target_Expr : Node_Id;
3542 Parent_Kind : Node_Kind;
3543 Unc_Decl : Boolean := False;
3544 Parent_Node : Node_Id;
3546 begin
3547 pragma Assert (not Is_Static_Dispatch_Table_Aggregate (N));
3548 pragma Assert (Is_Record_Type (Typ));
3550 Parent_Node := Parent (N);
3551 Parent_Kind := Nkind (Parent_Node);
3553 if Parent_Kind = N_Qualified_Expression then
3555 -- Check if we are in a unconstrained declaration because in this
3556 -- case the current delayed expansion mechanism doesn't work when
3557 -- the declared object size depend on the initializing expr.
3559 begin
3560 Parent_Node := Parent (Parent_Node);
3561 Parent_Kind := Nkind (Parent_Node);
3563 if Parent_Kind = N_Object_Declaration then
3564 Unc_Decl :=
3565 not Is_Entity_Name (Object_Definition (Parent_Node))
3566 or else Has_Discriminants
3567 (Entity (Object_Definition (Parent_Node)))
3568 or else Is_Class_Wide_Type
3569 (Entity (Object_Definition (Parent_Node)));
3570 end if;
3571 end;
3572 end if;
3574 -- Just set the Delay flag in the cases where the transformation will be
3575 -- done top down from above.
3577 if False
3579 -- Internal aggregate (transformed when expanding the parent)
3581 or else Parent_Kind = N_Aggregate
3582 or else Parent_Kind = N_Extension_Aggregate
3583 or else Parent_Kind = N_Component_Association
3585 -- Allocator (see Convert_Aggr_In_Allocator)
3587 or else Parent_Kind = N_Allocator
3589 -- Object declaration (see Convert_Aggr_In_Object_Decl)
3591 or else (Parent_Kind = N_Object_Declaration and then not Unc_Decl)
3593 -- Safe assignment (see Convert_Aggr_Assignments). So far only the
3594 -- assignments in init procs are taken into account.
3596 or else (Parent_Kind = N_Assignment_Statement
3597 and then Inside_Init_Proc)
3599 -- (Ada 2005) An inherently limited type in a return statement, which
3600 -- will be handled in a build-in-place fashion, and may be rewritten
3601 -- as an extended return and have its own finalization machinery.
3602 -- In the case of a simple return, the aggregate needs to be delayed
3603 -- until the scope for the return statement has been created, so
3604 -- that any finalization chain will be associated with that scope.
3605 -- For extended returns, we delay expansion to avoid the creation
3606 -- of an unwanted transient scope that could result in premature
3607 -- finalization of the return object (which is built in place
3608 -- within the caller's scope).
3610 or else
3611 (Is_Limited_View (Typ)
3612 and then
3613 (Nkind (Parent (Parent_Node)) = N_Extended_Return_Statement
3614 or else Nkind (Parent_Node) = N_Simple_Return_Statement))
3615 then
3616 Set_Expansion_Delayed (N);
3617 return;
3618 end if;
3620 -- Otherwise, if a transient scope is required, create it now. If we
3621 -- are within an initialization procedure do not create such, because
3622 -- the target of the assignment must not be declared within a local
3623 -- block, and because cleanup will take place on return from the
3624 -- initialization procedure.
3625 -- Should the condition be more restrictive ???
3627 if Requires_Transient_Scope (Typ) and then not Inside_Init_Proc then
3628 Establish_Transient_Scope (N, Sec_Stack => Needs_Finalization (Typ));
3629 end if;
3631 -- If the aggregate is non-limited, create a temporary. If it is limited
3632 -- and context is an assignment, this is a subaggregate for an enclosing
3633 -- aggregate being expanded. It must be built in place, so use target of
3634 -- the current assignment.
3636 if Is_Limited_Type (Typ)
3637 and then Nkind (Parent (N)) = N_Assignment_Statement
3638 then
3639 Target_Expr := New_Copy_Tree (Name (Parent (N)));
3640 Insert_Actions (Parent (N),
3641 Build_Record_Aggr_Code (N, Typ, Target_Expr));
3642 Rewrite (Parent (N), Make_Null_Statement (Loc));
3644 else
3645 Temp := Make_Temporary (Loc, 'A', N);
3647 -- If the type inherits unknown discriminants, use the view with
3648 -- known discriminants if available.
3650 if Has_Unknown_Discriminants (Typ)
3651 and then Present (Underlying_Record_View (Typ))
3652 then
3653 T := Underlying_Record_View (Typ);
3654 else
3655 T := Typ;
3656 end if;
3658 Instr :=
3659 Make_Object_Declaration (Loc,
3660 Defining_Identifier => Temp,
3661 Object_Definition => New_Occurrence_Of (T, Loc));
3663 Set_No_Initialization (Instr);
3664 Insert_Action (N, Instr);
3665 Initialize_Discriminants (Instr, T);
3667 Target_Expr := New_Occurrence_Of (Temp, Loc);
3668 Aggr_Code := Build_Record_Aggr_Code (N, T, Target_Expr);
3670 -- Save the last assignment statement associated with the aggregate
3671 -- when building a controlled object. This reference is utilized by
3672 -- the finalization machinery when marking an object as successfully
3673 -- initialized.
3675 if Needs_Finalization (T) then
3676 Set_Last_Aggregate_Assignment (Temp, Last (Aggr_Code));
3677 end if;
3679 Insert_Actions (N, Aggr_Code);
3680 Rewrite (N, New_Occurrence_Of (Temp, Loc));
3681 Analyze_And_Resolve (N, T);
3682 end if;
3683 end Convert_To_Assignments;
3685 ---------------------------
3686 -- Convert_To_Positional --
3687 ---------------------------
3689 procedure Convert_To_Positional
3690 (N : Node_Id;
3691 Max_Others_Replicate : Nat := 5;
3692 Handle_Bit_Packed : Boolean := False)
3694 Typ : constant Entity_Id := Etype (N);
3696 Static_Components : Boolean := True;
3698 procedure Check_Static_Components;
3699 -- Check whether all components of the aggregate are compile-time known
3700 -- values, and can be passed as is to the back-end without further
3701 -- expansion.
3703 function Flatten
3704 (N : Node_Id;
3705 Ix : Node_Id;
3706 Ixb : Node_Id) return Boolean;
3707 -- Convert the aggregate into a purely positional form if possible. On
3708 -- entry the bounds of all dimensions are known to be static, and the
3709 -- total number of components is safe enough to expand.
3711 function Is_Flat (N : Node_Id; Dims : Int) return Boolean;
3712 -- Return True iff the array N is flat (which is not trivial in the case
3713 -- of multidimensional aggregates).
3715 -----------------------------
3716 -- Check_Static_Components --
3717 -----------------------------
3719 -- Could use some comments in this body ???
3721 procedure Check_Static_Components is
3722 Expr : Node_Id;
3724 begin
3725 Static_Components := True;
3727 if Nkind (N) = N_String_Literal then
3728 null;
3730 elsif Present (Expressions (N)) then
3731 Expr := First (Expressions (N));
3732 while Present (Expr) loop
3733 if Nkind (Expr) /= N_Aggregate
3734 or else not Compile_Time_Known_Aggregate (Expr)
3735 or else Expansion_Delayed (Expr)
3736 then
3737 Static_Components := False;
3738 exit;
3739 end if;
3741 Next (Expr);
3742 end loop;
3743 end if;
3745 if Nkind (N) = N_Aggregate
3746 and then Present (Component_Associations (N))
3747 then
3748 Expr := First (Component_Associations (N));
3749 while Present (Expr) loop
3750 if Nkind_In (Expression (Expr), N_Integer_Literal,
3751 N_Real_Literal)
3752 then
3753 null;
3755 elsif Is_Entity_Name (Expression (Expr))
3756 and then Present (Entity (Expression (Expr)))
3757 and then Ekind (Entity (Expression (Expr))) =
3758 E_Enumeration_Literal
3759 then
3760 null;
3762 elsif Nkind (Expression (Expr)) /= N_Aggregate
3763 or else not Compile_Time_Known_Aggregate (Expression (Expr))
3764 or else Expansion_Delayed (Expression (Expr))
3765 then
3766 Static_Components := False;
3767 exit;
3768 end if;
3770 Next (Expr);
3771 end loop;
3772 end if;
3773 end Check_Static_Components;
3775 -------------
3776 -- Flatten --
3777 -------------
3779 function Flatten
3780 (N : Node_Id;
3781 Ix : Node_Id;
3782 Ixb : Node_Id) return Boolean
3784 Loc : constant Source_Ptr := Sloc (N);
3785 Blo : constant Node_Id := Type_Low_Bound (Etype (Ixb));
3786 Lo : constant Node_Id := Type_Low_Bound (Etype (Ix));
3787 Hi : constant Node_Id := Type_High_Bound (Etype (Ix));
3788 Lov : Uint;
3789 Hiv : Uint;
3791 Others_Present : Boolean := False;
3793 begin
3794 if Nkind (Original_Node (N)) = N_String_Literal then
3795 return True;
3796 end if;
3798 if not Compile_Time_Known_Value (Lo)
3799 or else not Compile_Time_Known_Value (Hi)
3800 then
3801 return False;
3802 end if;
3804 Lov := Expr_Value (Lo);
3805 Hiv := Expr_Value (Hi);
3807 -- Check if there is an others choice
3809 if Present (Component_Associations (N)) then
3810 declare
3811 Assoc : Node_Id;
3812 Choice : Node_Id;
3814 begin
3815 Assoc := First (Component_Associations (N));
3816 while Present (Assoc) loop
3818 -- If this is a box association, flattening is in general
3819 -- not possible because at this point we cannot tell if the
3820 -- default is static or even exists.
3822 if Box_Present (Assoc) then
3823 return False;
3824 end if;
3826 Choice := First (Choices (Assoc));
3828 while Present (Choice) loop
3829 if Nkind (Choice) = N_Others_Choice then
3830 Others_Present := True;
3831 end if;
3833 Next (Choice);
3834 end loop;
3836 Next (Assoc);
3837 end loop;
3838 end;
3839 end if;
3841 -- If the low bound is not known at compile time and others is not
3842 -- present we can proceed since the bounds can be obtained from the
3843 -- aggregate.
3845 if Hiv < Lov
3846 or else (not Compile_Time_Known_Value (Blo) and then Others_Present)
3847 then
3848 return False;
3849 end if;
3851 -- Determine if set of alternatives is suitable for conversion and
3852 -- build an array containing the values in sequence.
3854 declare
3855 Vals : array (UI_To_Int (Lov) .. UI_To_Int (Hiv))
3856 of Node_Id := (others => Empty);
3857 -- The values in the aggregate sorted appropriately
3859 Vlist : List_Id;
3860 -- Same data as Vals in list form
3862 Rep_Count : Nat;
3863 -- Used to validate Max_Others_Replicate limit
3865 Elmt : Node_Id;
3866 Num : Int := UI_To_Int (Lov);
3867 Choice_Index : Int;
3868 Choice : Node_Id;
3869 Lo, Hi : Node_Id;
3871 begin
3872 if Present (Expressions (N)) then
3873 Elmt := First (Expressions (N));
3874 while Present (Elmt) loop
3875 if Nkind (Elmt) = N_Aggregate
3876 and then Present (Next_Index (Ix))
3877 and then
3878 not Flatten (Elmt, Next_Index (Ix), Next_Index (Ixb))
3879 then
3880 return False;
3881 end if;
3883 Vals (Num) := Relocate_Node (Elmt);
3884 Num := Num + 1;
3886 Next (Elmt);
3887 end loop;
3888 end if;
3890 if No (Component_Associations (N)) then
3891 return True;
3892 end if;
3894 Elmt := First (Component_Associations (N));
3896 if Nkind (Expression (Elmt)) = N_Aggregate then
3897 if Present (Next_Index (Ix))
3898 and then
3899 not Flatten
3900 (Expression (Elmt), Next_Index (Ix), Next_Index (Ixb))
3901 then
3902 return False;
3903 end if;
3904 end if;
3906 Component_Loop : while Present (Elmt) loop
3907 Choice := First (Choices (Elmt));
3908 Choice_Loop : while Present (Choice) loop
3910 -- If we have an others choice, fill in the missing elements
3911 -- subject to the limit established by Max_Others_Replicate.
3913 if Nkind (Choice) = N_Others_Choice then
3914 Rep_Count := 0;
3916 for J in Vals'Range loop
3917 if No (Vals (J)) then
3918 Vals (J) := New_Copy_Tree (Expression (Elmt));
3919 Rep_Count := Rep_Count + 1;
3921 -- Check for maximum others replication. Note that
3922 -- we skip this test if either of the restrictions
3923 -- No_Elaboration_Code or No_Implicit_Loops is
3924 -- active, if this is a preelaborable unit or
3925 -- a predefined unit, or if the unit must be
3926 -- placed in data memory. This also ensures that
3927 -- predefined units get the same level of constant
3928 -- folding in Ada 95 and Ada 2005, where their
3929 -- categorization has changed.
3931 declare
3932 P : constant Entity_Id :=
3933 Cunit_Entity (Current_Sem_Unit);
3935 begin
3936 -- Check if duplication OK and if so continue
3937 -- processing.
3939 if Restriction_Active (No_Elaboration_Code)
3940 or else Restriction_Active (No_Implicit_Loops)
3941 or else
3942 (Ekind (Current_Scope) = E_Package
3943 and then Static_Elaboration_Desired
3944 (Current_Scope))
3945 or else Is_Preelaborated (P)
3946 or else (Ekind (P) = E_Package_Body
3947 and then
3948 Is_Preelaborated (Spec_Entity (P)))
3949 or else
3950 Is_Predefined_File_Name
3951 (Unit_File_Name (Get_Source_Unit (P)))
3952 then
3953 null;
3955 -- If duplication not OK, then we return False
3956 -- if the replication count is too high
3958 elsif Rep_Count > Max_Others_Replicate then
3959 return False;
3961 -- Continue on if duplication not OK, but the
3962 -- replication count is not excessive.
3964 else
3965 null;
3966 end if;
3967 end;
3968 end if;
3969 end loop;
3971 exit Component_Loop;
3973 -- Case of a subtype mark, identifier or expanded name
3975 elsif Is_Entity_Name (Choice)
3976 and then Is_Type (Entity (Choice))
3977 then
3978 Lo := Type_Low_Bound (Etype (Choice));
3979 Hi := Type_High_Bound (Etype (Choice));
3981 -- Case of subtype indication
3983 elsif Nkind (Choice) = N_Subtype_Indication then
3984 Lo := Low_Bound (Range_Expression (Constraint (Choice)));
3985 Hi := High_Bound (Range_Expression (Constraint (Choice)));
3987 -- Case of a range
3989 elsif Nkind (Choice) = N_Range then
3990 Lo := Low_Bound (Choice);
3991 Hi := High_Bound (Choice);
3993 -- Normal subexpression case
3995 else pragma Assert (Nkind (Choice) in N_Subexpr);
3996 if not Compile_Time_Known_Value (Choice) then
3997 return False;
3999 else
4000 Choice_Index := UI_To_Int (Expr_Value (Choice));
4002 if Choice_Index in Vals'Range then
4003 Vals (Choice_Index) :=
4004 New_Copy_Tree (Expression (Elmt));
4005 goto Continue;
4007 -- Choice is statically out-of-range, will be
4008 -- rewritten to raise Constraint_Error.
4010 else
4011 return False;
4012 end if;
4013 end if;
4014 end if;
4016 -- Range cases merge with Lo,Hi set
4018 if not Compile_Time_Known_Value (Lo)
4019 or else
4020 not Compile_Time_Known_Value (Hi)
4021 then
4022 return False;
4024 else
4025 for J in UI_To_Int (Expr_Value (Lo)) ..
4026 UI_To_Int (Expr_Value (Hi))
4027 loop
4028 Vals (J) := New_Copy_Tree (Expression (Elmt));
4029 end loop;
4030 end if;
4032 <<Continue>>
4033 Next (Choice);
4034 end loop Choice_Loop;
4036 Next (Elmt);
4037 end loop Component_Loop;
4039 -- If we get here the conversion is possible
4041 Vlist := New_List;
4042 for J in Vals'Range loop
4043 Append (Vals (J), Vlist);
4044 end loop;
4046 Rewrite (N, Make_Aggregate (Loc, Expressions => Vlist));
4047 Set_Aggregate_Bounds (N, Aggregate_Bounds (Original_Node (N)));
4048 return True;
4049 end;
4050 end Flatten;
4052 -------------
4053 -- Is_Flat --
4054 -------------
4056 function Is_Flat (N : Node_Id; Dims : Int) return Boolean is
4057 Elmt : Node_Id;
4059 begin
4060 if Dims = 0 then
4061 return True;
4063 elsif Nkind (N) = N_Aggregate then
4064 if Present (Component_Associations (N)) then
4065 return False;
4067 else
4068 Elmt := First (Expressions (N));
4069 while Present (Elmt) loop
4070 if not Is_Flat (Elmt, Dims - 1) then
4071 return False;
4072 end if;
4074 Next (Elmt);
4075 end loop;
4077 return True;
4078 end if;
4079 else
4080 return True;
4081 end if;
4082 end Is_Flat;
4084 -- Start of processing for Convert_To_Positional
4086 begin
4087 -- Only convert to positional when generating C in case of an
4088 -- object declaration, this is the only case where aggregates are
4089 -- supported in C.
4091 if Modify_Tree_For_C and then not In_Object_Declaration (N) then
4092 return;
4093 end if;
4095 -- Ada 2005 (AI-287): Do not convert in case of default initialized
4096 -- components because in this case will need to call the corresponding
4097 -- IP procedure.
4099 if Has_Default_Init_Comps (N) then
4100 return;
4101 end if;
4103 if Is_Flat (N, Number_Dimensions (Typ)) then
4104 return;
4105 end if;
4107 if Is_Bit_Packed_Array (Typ) and then not Handle_Bit_Packed then
4108 return;
4109 end if;
4111 -- Do not convert to positional if controlled components are involved
4112 -- since these require special processing
4114 if Has_Controlled_Component (Typ) then
4115 return;
4116 end if;
4118 Check_Static_Components;
4120 -- If the size is known, or all the components are static, try to
4121 -- build a fully positional aggregate.
4123 -- The size of the type may not be known for an aggregate with
4124 -- discriminated array components, but if the components are static
4125 -- it is still possible to verify statically that the length is
4126 -- compatible with the upper bound of the type, and therefore it is
4127 -- worth flattening such aggregates as well.
4129 -- For now the back-end expands these aggregates into individual
4130 -- assignments to the target anyway, but it is conceivable that
4131 -- it will eventually be able to treat such aggregates statically???
4133 if Aggr_Size_OK (N, Typ)
4134 and then Flatten (N, First_Index (Typ), First_Index (Base_Type (Typ)))
4135 then
4136 if Static_Components then
4137 Set_Compile_Time_Known_Aggregate (N);
4138 Set_Expansion_Delayed (N, False);
4139 end if;
4141 Analyze_And_Resolve (N, Typ);
4142 end if;
4144 -- If Static_Elaboration_Desired has been specified, diagnose aggregates
4145 -- that will still require initialization code.
4147 if (Ekind (Current_Scope) = E_Package
4148 and then Static_Elaboration_Desired (Current_Scope))
4149 and then Nkind (Parent (N)) = N_Object_Declaration
4150 then
4151 declare
4152 Expr : Node_Id;
4154 begin
4155 if Nkind (N) = N_Aggregate and then Present (Expressions (N)) then
4156 Expr := First (Expressions (N));
4157 while Present (Expr) loop
4158 if Nkind_In (Expr, N_Integer_Literal, N_Real_Literal)
4159 or else
4160 (Is_Entity_Name (Expr)
4161 and then Ekind (Entity (Expr)) = E_Enumeration_Literal)
4162 then
4163 null;
4165 else
4166 Error_Msg_N
4167 ("non-static object requires elaboration code??", N);
4168 exit;
4169 end if;
4171 Next (Expr);
4172 end loop;
4174 if Present (Component_Associations (N)) then
4175 Error_Msg_N ("object requires elaboration code??", N);
4176 end if;
4177 end if;
4178 end;
4179 end if;
4180 end Convert_To_Positional;
4182 ----------------------------
4183 -- Expand_Array_Aggregate --
4184 ----------------------------
4186 -- Array aggregate expansion proceeds as follows:
4188 -- 1. If requested we generate code to perform all the array aggregate
4189 -- bound checks, specifically
4191 -- (a) Check that the index range defined by aggregate bounds is
4192 -- compatible with corresponding index subtype.
4194 -- (b) If an others choice is present check that no aggregate
4195 -- index is outside the bounds of the index constraint.
4197 -- (c) For multidimensional arrays make sure that all subaggregates
4198 -- corresponding to the same dimension have the same bounds.
4200 -- 2. Check for packed array aggregate which can be converted to a
4201 -- constant so that the aggregate disappears completely.
4203 -- 3. Check case of nested aggregate. Generally nested aggregates are
4204 -- handled during the processing of the parent aggregate.
4206 -- 4. Check if the aggregate can be statically processed. If this is the
4207 -- case pass it as is to Gigi. Note that a necessary condition for
4208 -- static processing is that the aggregate be fully positional.
4210 -- 5. If in place aggregate expansion is possible (i.e. no need to create
4211 -- a temporary) then mark the aggregate as such and return. Otherwise
4212 -- create a new temporary and generate the appropriate initialization
4213 -- code.
4215 procedure Expand_Array_Aggregate (N : Node_Id) is
4216 Loc : constant Source_Ptr := Sloc (N);
4218 Typ : constant Entity_Id := Etype (N);
4219 Ctyp : constant Entity_Id := Component_Type (Typ);
4220 -- Typ is the correct constrained array subtype of the aggregate
4221 -- Ctyp is the corresponding component type.
4223 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
4224 -- Number of aggregate index dimensions
4226 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id;
4227 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id;
4228 -- Low and High bounds of the constraint for each aggregate index
4230 Aggr_Index_Typ : array (1 .. Aggr_Dimension) of Entity_Id;
4231 -- The type of each index
4233 In_Place_Assign_OK_For_Declaration : Boolean := False;
4234 -- True if we are to generate an in place assignment for a declaration
4236 Maybe_In_Place_OK : Boolean;
4237 -- If the type is neither controlled nor packed and the aggregate
4238 -- is the expression in an assignment, assignment in place may be
4239 -- possible, provided other conditions are met on the LHS.
4241 Others_Present : array (1 .. Aggr_Dimension) of Boolean :=
4242 (others => False);
4243 -- If Others_Present (J) is True, then there is an others choice in one
4244 -- of the subaggregates of N at dimension J.
4246 function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean;
4247 -- Returns true if an aggregate assignment can be done by the back end
4249 procedure Build_Constrained_Type (Positional : Boolean);
4250 -- If the subtype is not static or unconstrained, build a constrained
4251 -- type using the computable sizes of the aggregate and its sub-
4252 -- aggregates.
4254 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id);
4255 -- Checks that the bounds of Aggr_Bounds are within the bounds defined
4256 -- by Index_Bounds.
4258 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos);
4259 -- Checks that in a multidimensional array aggregate all subaggregates
4260 -- corresponding to the same dimension have the same bounds. Sub_Aggr is
4261 -- an array subaggregate. Dim is the dimension corresponding to the
4262 -- subaggregate.
4264 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos);
4265 -- Computes the values of array Others_Present. Sub_Aggr is the array
4266 -- subaggregate we start the computation from. Dim is the dimension
4267 -- corresponding to the subaggregate.
4269 function In_Place_Assign_OK return Boolean;
4270 -- Simple predicate to determine whether an aggregate assignment can
4271 -- be done in place, because none of the new values can depend on the
4272 -- components of the target of the assignment.
4274 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos);
4275 -- Checks that if an others choice is present in any subaggregate, no
4276 -- aggregate index is outside the bounds of the index constraint.
4277 -- Sub_Aggr is an array subaggregate. Dim is the dimension corresponding
4278 -- to the subaggregate.
4280 function Safe_Left_Hand_Side (N : Node_Id) return Boolean;
4281 -- In addition to Maybe_In_Place_OK, in order for an aggregate to be
4282 -- built directly into the target of the assignment it must be free
4283 -- of side effects.
4285 ------------------------------------
4286 -- Aggr_Assignment_OK_For_Backend --
4287 ------------------------------------
4289 -- Backend processing by Gigi/gcc is possible only if all the following
4290 -- conditions are met:
4292 -- 1. N consists of a single OTHERS choice, possibly recursively
4294 -- 2. The array type is not packed
4296 -- 3. The array type has no atomic components
4298 -- 4. The array type has no null ranges (the purpose of this is to
4299 -- avoid a bogus warning for an out-of-range value).
4301 -- 5. The component type is discrete
4303 -- 6. The component size is Storage_Unit or the value is of the form
4304 -- M * (1 + A**1 + A**2 + .. A**(K-1)) where A = 2**(Storage_Unit)
4305 -- and M in 1 .. A-1. This can also be viewed as K occurrences of
4306 -- the 8-bit value M, concatenated together.
4308 -- The ultimate goal is to generate a call to a fast memset routine
4309 -- specifically optimized for the target.
4311 function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean is
4312 Ctyp : Entity_Id;
4313 Index : Entity_Id;
4314 Expr : Node_Id := N;
4315 Low : Node_Id;
4316 High : Node_Id;
4317 Remainder : Uint;
4318 Value : Uint;
4319 Nunits : Nat;
4321 begin
4322 -- Recurse as far as possible to find the innermost component type
4324 Ctyp := Etype (N);
4325 while Is_Array_Type (Ctyp) loop
4326 if Nkind (Expr) /= N_Aggregate
4327 or else not Is_Others_Aggregate (Expr)
4328 then
4329 return False;
4330 end if;
4332 if Present (Packed_Array_Impl_Type (Ctyp)) then
4333 return False;
4334 end if;
4336 if Has_Atomic_Components (Ctyp) then
4337 return False;
4338 end if;
4340 Index := First_Index (Ctyp);
4341 while Present (Index) loop
4342 Get_Index_Bounds (Index, Low, High);
4344 if Is_Null_Range (Low, High) then
4345 return False;
4346 end if;
4348 Next_Index (Index);
4349 end loop;
4351 Expr := Expression (First (Component_Associations (Expr)));
4353 for J in 1 .. Number_Dimensions (Ctyp) - 1 loop
4354 if Nkind (Expr) /= N_Aggregate
4355 or else not Is_Others_Aggregate (Expr)
4356 then
4357 return False;
4358 end if;
4360 Expr := Expression (First (Component_Associations (Expr)));
4361 end loop;
4363 Ctyp := Component_Type (Ctyp);
4365 if Is_Atomic_Or_VFA (Ctyp) then
4366 return False;
4367 end if;
4368 end loop;
4370 if not Is_Discrete_Type (Ctyp) then
4371 return False;
4372 end if;
4374 -- The expression needs to be analyzed if True is returned
4376 Analyze_And_Resolve (Expr, Ctyp);
4378 -- The back end uses the Esize as the precision of the type
4380 Nunits := UI_To_Int (Esize (Ctyp)) / System_Storage_Unit;
4382 if Nunits = 1 then
4383 return True;
4384 end if;
4386 if not Compile_Time_Known_Value (Expr) then
4387 return False;
4388 end if;
4390 Value := Expr_Value (Expr);
4392 if Has_Biased_Representation (Ctyp) then
4393 Value := Value - Expr_Value (Type_Low_Bound (Ctyp));
4394 end if;
4396 -- Values 0 and -1 immediately satisfy the last check
4398 if Value = Uint_0 or else Value = Uint_Minus_1 then
4399 return True;
4400 end if;
4402 -- We need to work with an unsigned value
4404 if Value < 0 then
4405 Value := Value + 2**(System_Storage_Unit * Nunits);
4406 end if;
4408 Remainder := Value rem 2**System_Storage_Unit;
4410 for J in 1 .. Nunits - 1 loop
4411 Value := Value / 2**System_Storage_Unit;
4413 if Value rem 2**System_Storage_Unit /= Remainder then
4414 return False;
4415 end if;
4416 end loop;
4418 return True;
4419 end Aggr_Assignment_OK_For_Backend;
4421 ----------------------------
4422 -- Build_Constrained_Type --
4423 ----------------------------
4425 procedure Build_Constrained_Type (Positional : Boolean) is
4426 Loc : constant Source_Ptr := Sloc (N);
4427 Agg_Type : constant Entity_Id := Make_Temporary (Loc, 'A');
4428 Comp : Node_Id;
4429 Decl : Node_Id;
4430 Typ : constant Entity_Id := Etype (N);
4431 Indexes : constant List_Id := New_List;
4432 Num : Nat;
4433 Sub_Agg : Node_Id;
4435 begin
4436 -- If the aggregate is purely positional, all its subaggregates
4437 -- have the same size. We collect the dimensions from the first
4438 -- subaggregate at each level.
4440 if Positional then
4441 Sub_Agg := N;
4443 for D in 1 .. Number_Dimensions (Typ) loop
4444 Sub_Agg := First (Expressions (Sub_Agg));
4446 Comp := Sub_Agg;
4447 Num := 0;
4448 while Present (Comp) loop
4449 Num := Num + 1;
4450 Next (Comp);
4451 end loop;
4453 Append_To (Indexes,
4454 Make_Range (Loc,
4455 Low_Bound => Make_Integer_Literal (Loc, 1),
4456 High_Bound => Make_Integer_Literal (Loc, Num)));
4457 end loop;
4459 else
4460 -- We know the aggregate type is unconstrained and the aggregate
4461 -- is not processable by the back end, therefore not necessarily
4462 -- positional. Retrieve each dimension bounds (computed earlier).
4464 for D in 1 .. Number_Dimensions (Typ) loop
4465 Append_To (Indexes,
4466 Make_Range (Loc,
4467 Low_Bound => Aggr_Low (D),
4468 High_Bound => Aggr_High (D)));
4469 end loop;
4470 end if;
4472 Decl :=
4473 Make_Full_Type_Declaration (Loc,
4474 Defining_Identifier => Agg_Type,
4475 Type_Definition =>
4476 Make_Constrained_Array_Definition (Loc,
4477 Discrete_Subtype_Definitions => Indexes,
4478 Component_Definition =>
4479 Make_Component_Definition (Loc,
4480 Aliased_Present => False,
4481 Subtype_Indication =>
4482 New_Occurrence_Of (Component_Type (Typ), Loc))));
4484 Insert_Action (N, Decl);
4485 Analyze (Decl);
4486 Set_Etype (N, Agg_Type);
4487 Set_Is_Itype (Agg_Type);
4488 Freeze_Itype (Agg_Type, N);
4489 end Build_Constrained_Type;
4491 ------------------
4492 -- Check_Bounds --
4493 ------------------
4495 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id) is
4496 Aggr_Lo : Node_Id;
4497 Aggr_Hi : Node_Id;
4499 Ind_Lo : Node_Id;
4500 Ind_Hi : Node_Id;
4502 Cond : Node_Id := Empty;
4504 begin
4505 Get_Index_Bounds (Aggr_Bounds, Aggr_Lo, Aggr_Hi);
4506 Get_Index_Bounds (Index_Bounds, Ind_Lo, Ind_Hi);
4508 -- Generate the following test:
4510 -- [constraint_error when
4511 -- Aggr_Lo <= Aggr_Hi and then
4512 -- (Aggr_Lo < Ind_Lo or else Aggr_Hi > Ind_Hi)]
4514 -- As an optimization try to see if some tests are trivially vacuous
4515 -- because we are comparing an expression against itself.
4517 if Aggr_Lo = Ind_Lo and then Aggr_Hi = Ind_Hi then
4518 Cond := Empty;
4520 elsif Aggr_Hi = Ind_Hi then
4521 Cond :=
4522 Make_Op_Lt (Loc,
4523 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4524 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo));
4526 elsif Aggr_Lo = Ind_Lo then
4527 Cond :=
4528 Make_Op_Gt (Loc,
4529 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
4530 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Hi));
4532 else
4533 Cond :=
4534 Make_Or_Else (Loc,
4535 Left_Opnd =>
4536 Make_Op_Lt (Loc,
4537 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4538 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo)),
4540 Right_Opnd =>
4541 Make_Op_Gt (Loc,
4542 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
4543 Right_Opnd => Duplicate_Subexpr (Ind_Hi)));
4544 end if;
4546 if Present (Cond) then
4547 Cond :=
4548 Make_And_Then (Loc,
4549 Left_Opnd =>
4550 Make_Op_Le (Loc,
4551 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4552 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi)),
4554 Right_Opnd => Cond);
4556 Set_Analyzed (Left_Opnd (Left_Opnd (Cond)), False);
4557 Set_Analyzed (Right_Opnd (Left_Opnd (Cond)), False);
4558 Insert_Action (N,
4559 Make_Raise_Constraint_Error (Loc,
4560 Condition => Cond,
4561 Reason => CE_Range_Check_Failed));
4562 end if;
4563 end Check_Bounds;
4565 ----------------------------
4566 -- Check_Same_Aggr_Bounds --
4567 ----------------------------
4569 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos) is
4570 Sub_Lo : constant Node_Id := Low_Bound (Aggregate_Bounds (Sub_Aggr));
4571 Sub_Hi : constant Node_Id := High_Bound (Aggregate_Bounds (Sub_Aggr));
4572 -- The bounds of this specific subaggregate
4574 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
4575 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
4576 -- The bounds of the aggregate for this dimension
4578 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
4579 -- The index type for this dimension.xxx
4581 Cond : Node_Id := Empty;
4582 Assoc : Node_Id;
4583 Expr : Node_Id;
4585 begin
4586 -- If index checks are on generate the test
4588 -- [constraint_error when
4589 -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
4591 -- As an optimization try to see if some tests are trivially vacuos
4592 -- because we are comparing an expression against itself. Also for
4593 -- the first dimension the test is trivially vacuous because there
4594 -- is just one aggregate for dimension 1.
4596 if Index_Checks_Suppressed (Ind_Typ) then
4597 Cond := Empty;
4599 elsif Dim = 1 or else (Aggr_Lo = Sub_Lo and then Aggr_Hi = Sub_Hi)
4600 then
4601 Cond := Empty;
4603 elsif Aggr_Hi = Sub_Hi then
4604 Cond :=
4605 Make_Op_Ne (Loc,
4606 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4607 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo));
4609 elsif Aggr_Lo = Sub_Lo then
4610 Cond :=
4611 Make_Op_Ne (Loc,
4612 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
4613 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Hi));
4615 else
4616 Cond :=
4617 Make_Or_Else (Loc,
4618 Left_Opnd =>
4619 Make_Op_Ne (Loc,
4620 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4621 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo)),
4623 Right_Opnd =>
4624 Make_Op_Ne (Loc,
4625 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
4626 Right_Opnd => Duplicate_Subexpr (Sub_Hi)));
4627 end if;
4629 if Present (Cond) then
4630 Insert_Action (N,
4631 Make_Raise_Constraint_Error (Loc,
4632 Condition => Cond,
4633 Reason => CE_Length_Check_Failed));
4634 end if;
4636 -- Now look inside the subaggregate to see if there is more work
4638 if Dim < Aggr_Dimension then
4640 -- Process positional components
4642 if Present (Expressions (Sub_Aggr)) then
4643 Expr := First (Expressions (Sub_Aggr));
4644 while Present (Expr) loop
4645 Check_Same_Aggr_Bounds (Expr, Dim + 1);
4646 Next (Expr);
4647 end loop;
4648 end if;
4650 -- Process component associations
4652 if Present (Component_Associations (Sub_Aggr)) then
4653 Assoc := First (Component_Associations (Sub_Aggr));
4654 while Present (Assoc) loop
4655 Expr := Expression (Assoc);
4656 Check_Same_Aggr_Bounds (Expr, Dim + 1);
4657 Next (Assoc);
4658 end loop;
4659 end if;
4660 end if;
4661 end Check_Same_Aggr_Bounds;
4663 ----------------------------
4664 -- Compute_Others_Present --
4665 ----------------------------
4667 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos) is
4668 Assoc : Node_Id;
4669 Expr : Node_Id;
4671 begin
4672 if Present (Component_Associations (Sub_Aggr)) then
4673 Assoc := Last (Component_Associations (Sub_Aggr));
4675 if Nkind (First (Choices (Assoc))) = N_Others_Choice then
4676 Others_Present (Dim) := True;
4677 end if;
4678 end if;
4680 -- Now look inside the subaggregate to see if there is more work
4682 if Dim < Aggr_Dimension then
4684 -- Process positional components
4686 if Present (Expressions (Sub_Aggr)) then
4687 Expr := First (Expressions (Sub_Aggr));
4688 while Present (Expr) loop
4689 Compute_Others_Present (Expr, Dim + 1);
4690 Next (Expr);
4691 end loop;
4692 end if;
4694 -- Process component associations
4696 if Present (Component_Associations (Sub_Aggr)) then
4697 Assoc := First (Component_Associations (Sub_Aggr));
4698 while Present (Assoc) loop
4699 Expr := Expression (Assoc);
4700 Compute_Others_Present (Expr, Dim + 1);
4701 Next (Assoc);
4702 end loop;
4703 end if;
4704 end if;
4705 end Compute_Others_Present;
4707 ------------------------
4708 -- In_Place_Assign_OK --
4709 ------------------------
4711 function In_Place_Assign_OK return Boolean is
4712 Aggr_In : Node_Id;
4713 Aggr_Lo : Node_Id;
4714 Aggr_Hi : Node_Id;
4715 Obj_In : Node_Id;
4716 Obj_Lo : Node_Id;
4717 Obj_Hi : Node_Id;
4719 function Safe_Aggregate (Aggr : Node_Id) return Boolean;
4720 -- Check recursively that each component of a (sub)aggregate does not
4721 -- depend on the variable being assigned to.
4723 function Safe_Component (Expr : Node_Id) return Boolean;
4724 -- Verify that an expression cannot depend on the variable being
4725 -- assigned to. Room for improvement here (but less than before).
4727 --------------------
4728 -- Safe_Aggregate --
4729 --------------------
4731 function Safe_Aggregate (Aggr : Node_Id) return Boolean is
4732 Expr : Node_Id;
4734 begin
4735 if Present (Expressions (Aggr)) then
4736 Expr := First (Expressions (Aggr));
4737 while Present (Expr) loop
4738 if Nkind (Expr) = N_Aggregate then
4739 if not Safe_Aggregate (Expr) then
4740 return False;
4741 end if;
4743 elsif not Safe_Component (Expr) then
4744 return False;
4745 end if;
4747 Next (Expr);
4748 end loop;
4749 end if;
4751 if Present (Component_Associations (Aggr)) then
4752 Expr := First (Component_Associations (Aggr));
4753 while Present (Expr) loop
4754 if Nkind (Expression (Expr)) = N_Aggregate then
4755 if not Safe_Aggregate (Expression (Expr)) then
4756 return False;
4757 end if;
4759 -- If association has a box, no way to determine yet
4760 -- whether default can be assigned in place.
4762 elsif Box_Present (Expr) then
4763 return False;
4765 elsif not Safe_Component (Expression (Expr)) then
4766 return False;
4767 end if;
4769 Next (Expr);
4770 end loop;
4771 end if;
4773 return True;
4774 end Safe_Aggregate;
4776 --------------------
4777 -- Safe_Component --
4778 --------------------
4780 function Safe_Component (Expr : Node_Id) return Boolean is
4781 Comp : Node_Id := Expr;
4783 function Check_Component (Comp : Node_Id) return Boolean;
4784 -- Do the recursive traversal, after copy
4786 ---------------------
4787 -- Check_Component --
4788 ---------------------
4790 function Check_Component (Comp : Node_Id) return Boolean is
4791 begin
4792 if Is_Overloaded (Comp) then
4793 return False;
4794 end if;
4796 return Compile_Time_Known_Value (Comp)
4798 or else (Is_Entity_Name (Comp)
4799 and then Present (Entity (Comp))
4800 and then No (Renamed_Object (Entity (Comp))))
4802 or else (Nkind (Comp) = N_Attribute_Reference
4803 and then Check_Component (Prefix (Comp)))
4805 or else (Nkind (Comp) in N_Binary_Op
4806 and then Check_Component (Left_Opnd (Comp))
4807 and then Check_Component (Right_Opnd (Comp)))
4809 or else (Nkind (Comp) in N_Unary_Op
4810 and then Check_Component (Right_Opnd (Comp)))
4812 or else (Nkind (Comp) = N_Selected_Component
4813 and then Check_Component (Prefix (Comp)))
4815 or else (Nkind (Comp) = N_Unchecked_Type_Conversion
4816 and then Check_Component (Expression (Comp)));
4817 end Check_Component;
4819 -- Start of processing for Safe_Component
4821 begin
4822 -- If the component appears in an association that may correspond
4823 -- to more than one element, it is not analyzed before expansion
4824 -- into assignments, to avoid side effects. We analyze, but do not
4825 -- resolve the copy, to obtain sufficient entity information for
4826 -- the checks that follow. If component is overloaded we assume
4827 -- an unsafe function call.
4829 if not Analyzed (Comp) then
4830 if Is_Overloaded (Expr) then
4831 return False;
4833 elsif Nkind (Expr) = N_Aggregate
4834 and then not Is_Others_Aggregate (Expr)
4835 then
4836 return False;
4838 elsif Nkind (Expr) = N_Allocator then
4840 -- For now, too complex to analyze
4842 return False;
4843 end if;
4845 Comp := New_Copy_Tree (Expr);
4846 Set_Parent (Comp, Parent (Expr));
4847 Analyze (Comp);
4848 end if;
4850 if Nkind (Comp) = N_Aggregate then
4851 return Safe_Aggregate (Comp);
4852 else
4853 return Check_Component (Comp);
4854 end if;
4855 end Safe_Component;
4857 -- Start of processing for In_Place_Assign_OK
4859 begin
4860 if Present (Component_Associations (N)) then
4862 -- On assignment, sliding can take place, so we cannot do the
4863 -- assignment in place unless the bounds of the aggregate are
4864 -- statically equal to those of the target.
4866 -- If the aggregate is given by an others choice, the bounds are
4867 -- derived from the left-hand side, and the assignment is safe if
4868 -- the expression is.
4870 if Is_Others_Aggregate (N) then
4871 return
4872 Safe_Component
4873 (Expression (First (Component_Associations (N))));
4874 end if;
4876 Aggr_In := First_Index (Etype (N));
4878 if Nkind (Parent (N)) = N_Assignment_Statement then
4879 Obj_In := First_Index (Etype (Name (Parent (N))));
4881 else
4882 -- Context is an allocator. Check bounds of aggregate against
4883 -- given type in qualified expression.
4885 pragma Assert (Nkind (Parent (Parent (N))) = N_Allocator);
4886 Obj_In :=
4887 First_Index (Etype (Entity (Subtype_Mark (Parent (N)))));
4888 end if;
4890 while Present (Aggr_In) loop
4891 Get_Index_Bounds (Aggr_In, Aggr_Lo, Aggr_Hi);
4892 Get_Index_Bounds (Obj_In, Obj_Lo, Obj_Hi);
4894 if not Compile_Time_Known_Value (Aggr_Lo)
4895 or else not Compile_Time_Known_Value (Aggr_Hi)
4896 or else not Compile_Time_Known_Value (Obj_Lo)
4897 or else not Compile_Time_Known_Value (Obj_Hi)
4898 or else Expr_Value (Aggr_Lo) /= Expr_Value (Obj_Lo)
4899 or else Expr_Value (Aggr_Hi) /= Expr_Value (Obj_Hi)
4900 then
4901 return False;
4902 end if;
4904 Next_Index (Aggr_In);
4905 Next_Index (Obj_In);
4906 end loop;
4907 end if;
4909 -- Now check the component values themselves
4911 return Safe_Aggregate (N);
4912 end In_Place_Assign_OK;
4914 ------------------
4915 -- Others_Check --
4916 ------------------
4918 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos) is
4919 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
4920 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
4921 -- The bounds of the aggregate for this dimension
4923 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
4924 -- The index type for this dimension
4926 Need_To_Check : Boolean := False;
4928 Choices_Lo : Node_Id := Empty;
4929 Choices_Hi : Node_Id := Empty;
4930 -- The lowest and highest discrete choices for a named subaggregate
4932 Nb_Choices : Int := -1;
4933 -- The number of discrete non-others choices in this subaggregate
4935 Nb_Elements : Uint := Uint_0;
4936 -- The number of elements in a positional aggregate
4938 Cond : Node_Id := Empty;
4940 Assoc : Node_Id;
4941 Choice : Node_Id;
4942 Expr : Node_Id;
4944 begin
4945 -- Check if we have an others choice. If we do make sure that this
4946 -- subaggregate contains at least one element in addition to the
4947 -- others choice.
4949 if Range_Checks_Suppressed (Ind_Typ) then
4950 Need_To_Check := False;
4952 elsif Present (Expressions (Sub_Aggr))
4953 and then Present (Component_Associations (Sub_Aggr))
4954 then
4955 Need_To_Check := True;
4957 elsif Present (Component_Associations (Sub_Aggr)) then
4958 Assoc := Last (Component_Associations (Sub_Aggr));
4960 if Nkind (First (Choices (Assoc))) /= N_Others_Choice then
4961 Need_To_Check := False;
4963 else
4964 -- Count the number of discrete choices. Start with -1 because
4965 -- the others choice does not count.
4967 -- Is there some reason we do not use List_Length here ???
4969 Nb_Choices := -1;
4970 Assoc := First (Component_Associations (Sub_Aggr));
4971 while Present (Assoc) loop
4972 Choice := First (Choices (Assoc));
4973 while Present (Choice) loop
4974 Nb_Choices := Nb_Choices + 1;
4975 Next (Choice);
4976 end loop;
4978 Next (Assoc);
4979 end loop;
4981 -- If there is only an others choice nothing to do
4983 Need_To_Check := (Nb_Choices > 0);
4984 end if;
4986 else
4987 Need_To_Check := False;
4988 end if;
4990 -- If we are dealing with a positional subaggregate with an others
4991 -- choice then compute the number or positional elements.
4993 if Need_To_Check and then Present (Expressions (Sub_Aggr)) then
4994 Expr := First (Expressions (Sub_Aggr));
4995 Nb_Elements := Uint_0;
4996 while Present (Expr) loop
4997 Nb_Elements := Nb_Elements + 1;
4998 Next (Expr);
4999 end loop;
5001 -- If the aggregate contains discrete choices and an others choice
5002 -- compute the smallest and largest discrete choice values.
5004 elsif Need_To_Check then
5005 Compute_Choices_Lo_And_Choices_Hi : declare
5007 Table : Case_Table_Type (1 .. Nb_Choices);
5008 -- Used to sort all the different choice values
5010 J : Pos := 1;
5011 Low : Node_Id;
5012 High : Node_Id;
5014 begin
5015 Assoc := First (Component_Associations (Sub_Aggr));
5016 while Present (Assoc) loop
5017 Choice := First (Choices (Assoc));
5018 while Present (Choice) loop
5019 if Nkind (Choice) = N_Others_Choice then
5020 exit;
5021 end if;
5023 Get_Index_Bounds (Choice, Low, High);
5024 Table (J).Choice_Lo := Low;
5025 Table (J).Choice_Hi := High;
5027 J := J + 1;
5028 Next (Choice);
5029 end loop;
5031 Next (Assoc);
5032 end loop;
5034 -- Sort the discrete choices
5036 Sort_Case_Table (Table);
5038 Choices_Lo := Table (1).Choice_Lo;
5039 Choices_Hi := Table (Nb_Choices).Choice_Hi;
5040 end Compute_Choices_Lo_And_Choices_Hi;
5041 end if;
5043 -- If no others choice in this subaggregate, or the aggregate
5044 -- comprises only an others choice, nothing to do.
5046 if not Need_To_Check then
5047 Cond := Empty;
5049 -- If we are dealing with an aggregate containing an others choice
5050 -- and positional components, we generate the following test:
5052 -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) >
5053 -- Ind_Typ'Pos (Aggr_Hi)
5054 -- then
5055 -- raise Constraint_Error;
5056 -- end if;
5058 elsif Nb_Elements > Uint_0 then
5059 Cond :=
5060 Make_Op_Gt (Loc,
5061 Left_Opnd =>
5062 Make_Op_Add (Loc,
5063 Left_Opnd =>
5064 Make_Attribute_Reference (Loc,
5065 Prefix => New_Occurrence_Of (Ind_Typ, Loc),
5066 Attribute_Name => Name_Pos,
5067 Expressions =>
5068 New_List
5069 (Duplicate_Subexpr_Move_Checks (Aggr_Lo))),
5070 Right_Opnd => Make_Integer_Literal (Loc, Nb_Elements - 1)),
5072 Right_Opnd =>
5073 Make_Attribute_Reference (Loc,
5074 Prefix => New_Occurrence_Of (Ind_Typ, Loc),
5075 Attribute_Name => Name_Pos,
5076 Expressions => New_List (
5077 Duplicate_Subexpr_Move_Checks (Aggr_Hi))));
5079 -- If we are dealing with an aggregate containing an others choice
5080 -- and discrete choices we generate the following test:
5082 -- [constraint_error when
5083 -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi];
5085 else
5086 Cond :=
5087 Make_Or_Else (Loc,
5088 Left_Opnd =>
5089 Make_Op_Lt (Loc,
5090 Left_Opnd => Duplicate_Subexpr_Move_Checks (Choices_Lo),
5091 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo)),
5093 Right_Opnd =>
5094 Make_Op_Gt (Loc,
5095 Left_Opnd => Duplicate_Subexpr (Choices_Hi),
5096 Right_Opnd => Duplicate_Subexpr (Aggr_Hi)));
5097 end if;
5099 if Present (Cond) then
5100 Insert_Action (N,
5101 Make_Raise_Constraint_Error (Loc,
5102 Condition => Cond,
5103 Reason => CE_Length_Check_Failed));
5104 -- Questionable reason code, shouldn't that be a
5105 -- CE_Range_Check_Failed ???
5106 end if;
5108 -- Now look inside the subaggregate to see if there is more work
5110 if Dim < Aggr_Dimension then
5112 -- Process positional components
5114 if Present (Expressions (Sub_Aggr)) then
5115 Expr := First (Expressions (Sub_Aggr));
5116 while Present (Expr) loop
5117 Others_Check (Expr, Dim + 1);
5118 Next (Expr);
5119 end loop;
5120 end if;
5122 -- Process component associations
5124 if Present (Component_Associations (Sub_Aggr)) then
5125 Assoc := First (Component_Associations (Sub_Aggr));
5126 while Present (Assoc) loop
5127 Expr := Expression (Assoc);
5128 Others_Check (Expr, Dim + 1);
5129 Next (Assoc);
5130 end loop;
5131 end if;
5132 end if;
5133 end Others_Check;
5135 -------------------------
5136 -- Safe_Left_Hand_Side --
5137 -------------------------
5139 function Safe_Left_Hand_Side (N : Node_Id) return Boolean is
5140 function Is_Safe_Index (Indx : Node_Id) return Boolean;
5141 -- If the left-hand side includes an indexed component, check that
5142 -- the indexes are free of side effects.
5144 -------------------
5145 -- Is_Safe_Index --
5146 -------------------
5148 function Is_Safe_Index (Indx : Node_Id) return Boolean is
5149 begin
5150 if Is_Entity_Name (Indx) then
5151 return True;
5153 elsif Nkind (Indx) = N_Integer_Literal then
5154 return True;
5156 elsif Nkind (Indx) = N_Function_Call
5157 and then Is_Entity_Name (Name (Indx))
5158 and then Has_Pragma_Pure_Function (Entity (Name (Indx)))
5159 then
5160 return True;
5162 elsif Nkind (Indx) = N_Type_Conversion
5163 and then Is_Safe_Index (Expression (Indx))
5164 then
5165 return True;
5167 else
5168 return False;
5169 end if;
5170 end Is_Safe_Index;
5172 -- Start of processing for Safe_Left_Hand_Side
5174 begin
5175 if Is_Entity_Name (N) then
5176 return True;
5178 elsif Nkind_In (N, N_Explicit_Dereference, N_Selected_Component)
5179 and then Safe_Left_Hand_Side (Prefix (N))
5180 then
5181 return True;
5183 elsif Nkind (N) = N_Indexed_Component
5184 and then Safe_Left_Hand_Side (Prefix (N))
5185 and then Is_Safe_Index (First (Expressions (N)))
5186 then
5187 return True;
5189 elsif Nkind (N) = N_Unchecked_Type_Conversion then
5190 return Safe_Left_Hand_Side (Expression (N));
5192 else
5193 return False;
5194 end if;
5195 end Safe_Left_Hand_Side;
5197 -- Local variables
5199 Tmp : Entity_Id;
5200 -- Holds the temporary aggregate value
5202 Tmp_Decl : Node_Id;
5203 -- Holds the declaration of Tmp
5205 Aggr_Code : List_Id;
5206 Parent_Node : Node_Id;
5207 Parent_Kind : Node_Kind;
5209 -- Start of processing for Expand_Array_Aggregate
5211 begin
5212 -- Do not touch the special aggregates of attributes used for Asm calls
5214 if Is_RTE (Ctyp, RE_Asm_Input_Operand)
5215 or else Is_RTE (Ctyp, RE_Asm_Output_Operand)
5216 then
5217 return;
5219 -- Do not expand an aggregate for an array type which contains tasks if
5220 -- the aggregate is associated with an unexpanded return statement of a
5221 -- build-in-place function. The aggregate is expanded when the related
5222 -- return statement (rewritten into an extended return) is processed.
5223 -- This delay ensures that any temporaries and initialization code
5224 -- generated for the aggregate appear in the proper return block and
5225 -- use the correct _chain and _master.
5227 elsif Has_Task (Base_Type (Etype (N)))
5228 and then Nkind (Parent (N)) = N_Simple_Return_Statement
5229 and then Is_Build_In_Place_Function
5230 (Return_Applies_To (Return_Statement_Entity (Parent (N))))
5231 then
5232 return;
5234 -- Do not attempt expansion if error already detected. We may reach this
5235 -- point in spite of previous errors when compiling with -gnatq, to
5236 -- force all possible errors (this is the usual ACATS mode).
5238 elsif Error_Posted (N) then
5239 return;
5240 end if;
5242 -- If the semantic analyzer has determined that aggregate N will raise
5243 -- Constraint_Error at run time, then the aggregate node has been
5244 -- replaced with an N_Raise_Constraint_Error node and we should
5245 -- never get here.
5247 pragma Assert (not Raises_Constraint_Error (N));
5249 -- STEP 1a
5251 -- Check that the index range defined by aggregate bounds is
5252 -- compatible with corresponding index subtype.
5254 Index_Compatibility_Check : declare
5255 Aggr_Index_Range : Node_Id := First_Index (Typ);
5256 -- The current aggregate index range
5258 Index_Constraint : Node_Id := First_Index (Etype (Typ));
5259 -- The corresponding index constraint against which we have to
5260 -- check the above aggregate index range.
5262 begin
5263 Compute_Others_Present (N, 1);
5265 for J in 1 .. Aggr_Dimension loop
5266 -- There is no need to emit a check if an others choice is present
5267 -- for this array aggregate dimension since in this case one of
5268 -- N's subaggregates has taken its bounds from the context and
5269 -- these bounds must have been checked already. In addition all
5270 -- subaggregates corresponding to the same dimension must all have
5271 -- the same bounds (checked in (c) below).
5273 if not Range_Checks_Suppressed (Etype (Index_Constraint))
5274 and then not Others_Present (J)
5275 then
5276 -- We don't use Checks.Apply_Range_Check here because it emits
5277 -- a spurious check. Namely it checks that the range defined by
5278 -- the aggregate bounds is nonempty. But we know this already
5279 -- if we get here.
5281 Check_Bounds (Aggr_Index_Range, Index_Constraint);
5282 end if;
5284 -- Save the low and high bounds of the aggregate index as well as
5285 -- the index type for later use in checks (b) and (c) below.
5287 Aggr_Low (J) := Low_Bound (Aggr_Index_Range);
5288 Aggr_High (J) := High_Bound (Aggr_Index_Range);
5290 Aggr_Index_Typ (J) := Etype (Index_Constraint);
5292 Next_Index (Aggr_Index_Range);
5293 Next_Index (Index_Constraint);
5294 end loop;
5295 end Index_Compatibility_Check;
5297 -- STEP 1b
5299 -- If an others choice is present check that no aggregate index is
5300 -- outside the bounds of the index constraint.
5302 Others_Check (N, 1);
5304 -- STEP 1c
5306 -- For multidimensional arrays make sure that all subaggregates
5307 -- corresponding to the same dimension have the same bounds.
5309 if Aggr_Dimension > 1 then
5310 Check_Same_Aggr_Bounds (N, 1);
5311 end if;
5313 -- STEP 1d
5315 -- If we have a default component value, or simple initialization is
5316 -- required for the component type, then we replace <> in component
5317 -- associations by the required default value.
5319 declare
5320 Default_Val : Node_Id;
5321 Assoc : Node_Id;
5323 begin
5324 if (Present (Default_Aspect_Component_Value (Typ))
5325 or else Needs_Simple_Initialization (Ctyp))
5326 and then Present (Component_Associations (N))
5327 then
5328 Assoc := First (Component_Associations (N));
5329 while Present (Assoc) loop
5330 if Nkind (Assoc) = N_Component_Association
5331 and then Box_Present (Assoc)
5332 then
5333 Set_Box_Present (Assoc, False);
5335 if Present (Default_Aspect_Component_Value (Typ)) then
5336 Default_Val := Default_Aspect_Component_Value (Typ);
5337 else
5338 Default_Val := Get_Simple_Init_Val (Ctyp, N);
5339 end if;
5341 Set_Expression (Assoc, New_Copy_Tree (Default_Val));
5342 Analyze_And_Resolve (Expression (Assoc), Ctyp);
5343 end if;
5345 Next (Assoc);
5346 end loop;
5347 end if;
5348 end;
5350 -- STEP 2
5352 -- Here we test for is packed array aggregate that we can handle at
5353 -- compile time. If so, return with transformation done. Note that we do
5354 -- this even if the aggregate is nested, because once we have done this
5355 -- processing, there is no more nested aggregate.
5357 if Packed_Array_Aggregate_Handled (N) then
5358 return;
5359 end if;
5361 -- At this point we try to convert to positional form
5363 if Ekind (Current_Scope) = E_Package
5364 and then Static_Elaboration_Desired (Current_Scope)
5365 then
5366 Convert_To_Positional (N, Max_Others_Replicate => 100);
5367 else
5368 Convert_To_Positional (N);
5369 end if;
5371 -- if the result is no longer an aggregate (e.g. it may be a string
5372 -- literal, or a temporary which has the needed value), then we are
5373 -- done, since there is no longer a nested aggregate.
5375 if Nkind (N) /= N_Aggregate then
5376 return;
5378 -- We are also done if the result is an analyzed aggregate, indicating
5379 -- that Convert_To_Positional succeeded and reanalyzed the rewritten
5380 -- aggregate.
5382 elsif Analyzed (N) and then N /= Original_Node (N) then
5383 return;
5384 end if;
5386 -- If all aggregate components are compile-time known and the aggregate
5387 -- has been flattened, nothing left to do. The same occurs if the
5388 -- aggregate is used to initialize the components of a statically
5389 -- allocated dispatch table.
5391 if Compile_Time_Known_Aggregate (N)
5392 or else Is_Static_Dispatch_Table_Aggregate (N)
5393 then
5394 Set_Expansion_Delayed (N, False);
5395 return;
5396 end if;
5398 -- Now see if back end processing is possible
5400 if Backend_Processing_Possible (N) then
5402 -- If the aggregate is static but the constraints are not, build
5403 -- a static subtype for the aggregate, so that Gigi can place it
5404 -- in static memory. Perform an unchecked_conversion to the non-
5405 -- static type imposed by the context.
5407 declare
5408 Itype : constant Entity_Id := Etype (N);
5409 Index : Node_Id;
5410 Needs_Type : Boolean := False;
5412 begin
5413 Index := First_Index (Itype);
5414 while Present (Index) loop
5415 if not Is_OK_Static_Subtype (Etype (Index)) then
5416 Needs_Type := True;
5417 exit;
5418 else
5419 Next_Index (Index);
5420 end if;
5421 end loop;
5423 if Needs_Type then
5424 Build_Constrained_Type (Positional => True);
5425 Rewrite (N, Unchecked_Convert_To (Itype, N));
5426 Analyze (N);
5427 end if;
5428 end;
5430 return;
5431 end if;
5433 -- STEP 3
5435 -- Delay expansion for nested aggregates: it will be taken care of
5436 -- when the parent aggregate is expanded.
5438 Parent_Node := Parent (N);
5439 Parent_Kind := Nkind (Parent_Node);
5441 if Parent_Kind = N_Qualified_Expression then
5442 Parent_Node := Parent (Parent_Node);
5443 Parent_Kind := Nkind (Parent_Node);
5444 end if;
5446 if Parent_Kind = N_Aggregate
5447 or else Parent_Kind = N_Extension_Aggregate
5448 or else Parent_Kind = N_Component_Association
5449 or else (Parent_Kind = N_Object_Declaration
5450 and then Needs_Finalization (Typ))
5451 or else (Parent_Kind = N_Assignment_Statement
5452 and then Inside_Init_Proc)
5453 then
5454 if Static_Array_Aggregate (N)
5455 or else Compile_Time_Known_Aggregate (N)
5456 then
5457 Set_Expansion_Delayed (N, False);
5458 return;
5459 else
5460 Set_Expansion_Delayed (N);
5461 return;
5462 end if;
5463 end if;
5465 -- STEP 4
5467 -- Look if in place aggregate expansion is possible
5469 -- For object declarations we build the aggregate in place, unless
5470 -- the array is bit-packed or the component is controlled.
5472 -- For assignments we do the assignment in place if all the component
5473 -- associations have compile-time known values. For other cases we
5474 -- create a temporary. The analysis for safety of on-line assignment
5475 -- is delicate, i.e. we don't know how to do it fully yet ???
5477 -- For allocators we assign to the designated object in place if the
5478 -- aggregate meets the same conditions as other in-place assignments.
5479 -- In this case the aggregate may not come from source but was created
5480 -- for default initialization, e.g. with Initialize_Scalars.
5482 if Requires_Transient_Scope (Typ) then
5483 Establish_Transient_Scope
5484 (N, Sec_Stack => Has_Controlled_Component (Typ));
5485 end if;
5487 if Has_Default_Init_Comps (N) then
5488 Maybe_In_Place_OK := False;
5490 elsif Is_Bit_Packed_Array (Typ)
5491 or else Has_Controlled_Component (Typ)
5492 then
5493 Maybe_In_Place_OK := False;
5495 else
5496 Maybe_In_Place_OK :=
5497 (Nkind (Parent (N)) = N_Assignment_Statement
5498 and then In_Place_Assign_OK)
5500 or else
5501 (Nkind (Parent (Parent (N))) = N_Allocator
5502 and then In_Place_Assign_OK);
5503 end if;
5505 -- If this is an array of tasks, it will be expanded into build-in-place
5506 -- assignments. Build an activation chain for the tasks now.
5508 if Has_Task (Etype (N)) then
5509 Build_Activation_Chain_Entity (N);
5510 end if;
5512 -- Perform in-place expansion of aggregate in an object declaration.
5513 -- Note: actions generated for the aggregate will be captured in an
5514 -- expression-with-actions statement so that they can be transferred
5515 -- to freeze actions later if there is an address clause for the
5516 -- object. (Note: we don't use a block statement because this would
5517 -- cause generated freeze nodes to be elaborated in the wrong scope).
5519 -- Should document these individual tests ???
5521 if not Has_Default_Init_Comps (N)
5522 and then Comes_From_Source (Parent_Node)
5523 and then Parent_Kind = N_Object_Declaration
5524 and then not
5525 Must_Slide (Etype (Defining_Identifier (Parent_Node)), Typ)
5526 and then N = Expression (Parent_Node)
5527 and then not Is_Bit_Packed_Array (Typ)
5528 and then not Has_Controlled_Component (Typ)
5529 then
5530 In_Place_Assign_OK_For_Declaration := True;
5531 Tmp := Defining_Identifier (Parent (N));
5532 Set_No_Initialization (Parent (N));
5533 Set_Expression (Parent (N), Empty);
5535 -- Set kind and type of the entity, for use in the analysis
5536 -- of the subsequent assignments. If the nominal type is not
5537 -- constrained, build a subtype from the known bounds of the
5538 -- aggregate. If the declaration has a subtype mark, use it,
5539 -- otherwise use the itype of the aggregate.
5541 Set_Ekind (Tmp, E_Variable);
5543 if not Is_Constrained (Typ) then
5544 Build_Constrained_Type (Positional => False);
5546 elsif Is_Entity_Name (Object_Definition (Parent (N)))
5547 and then Is_Constrained (Entity (Object_Definition (Parent (N))))
5548 then
5549 Set_Etype (Tmp, Entity (Object_Definition (Parent (N))));
5551 else
5552 Set_Size_Known_At_Compile_Time (Typ, False);
5553 Set_Etype (Tmp, Typ);
5554 end if;
5556 elsif Maybe_In_Place_OK
5557 and then Nkind (Parent (N)) = N_Qualified_Expression
5558 and then Nkind (Parent (Parent (N))) = N_Allocator
5559 then
5560 Set_Expansion_Delayed (N);
5561 return;
5563 -- In the remaining cases the aggregate is the RHS of an assignment
5565 elsif Maybe_In_Place_OK
5566 and then Safe_Left_Hand_Side (Name (Parent (N)))
5567 then
5568 Tmp := Name (Parent (N));
5570 if Etype (Tmp) /= Etype (N) then
5571 Apply_Length_Check (N, Etype (Tmp));
5573 if Nkind (N) = N_Raise_Constraint_Error then
5575 -- Static error, nothing further to expand
5577 return;
5578 end if;
5579 end if;
5581 -- If a slice assignment has an aggregate with a single others_choice,
5582 -- the assignment can be done in place even if bounds are not static,
5583 -- by converting it into a loop over the discrete range of the slice.
5585 elsif Maybe_In_Place_OK
5586 and then Nkind (Name (Parent (N))) = N_Slice
5587 and then Is_Others_Aggregate (N)
5588 then
5589 Tmp := Name (Parent (N));
5591 -- Set type of aggregate to be type of lhs in assignment, in order
5592 -- to suppress redundant length checks.
5594 Set_Etype (N, Etype (Tmp));
5596 -- Step 5
5598 -- In place aggregate expansion is not possible
5600 else
5601 Maybe_In_Place_OK := False;
5602 Tmp := Make_Temporary (Loc, 'A', N);
5603 Tmp_Decl :=
5604 Make_Object_Declaration (Loc,
5605 Defining_Identifier => Tmp,
5606 Object_Definition => New_Occurrence_Of (Typ, Loc));
5607 Set_No_Initialization (Tmp_Decl, True);
5609 -- If we are within a loop, the temporary will be pushed on the
5610 -- stack at each iteration. If the aggregate is the expression for an
5611 -- allocator, it will be immediately copied to the heap and can
5612 -- be reclaimed at once. We create a transient scope around the
5613 -- aggregate for this purpose.
5615 if Ekind (Current_Scope) = E_Loop
5616 and then Nkind (Parent (Parent (N))) = N_Allocator
5617 then
5618 Establish_Transient_Scope (N, False);
5619 end if;
5621 Insert_Action (N, Tmp_Decl);
5622 end if;
5624 -- Construct and insert the aggregate code. We can safely suppress index
5625 -- checks because this code is guaranteed not to raise CE on index
5626 -- checks. However we should *not* suppress all checks.
5628 declare
5629 Target : Node_Id;
5631 begin
5632 if Nkind (Tmp) = N_Defining_Identifier then
5633 Target := New_Occurrence_Of (Tmp, Loc);
5635 else
5636 if Has_Default_Init_Comps (N) then
5638 -- Ada 2005 (AI-287): This case has not been analyzed???
5640 raise Program_Error;
5641 end if;
5643 -- Name in assignment is explicit dereference
5645 Target := New_Copy (Tmp);
5646 end if;
5648 -- If we are to generate an in place assignment for a declaration or
5649 -- an assignment statement, and the assignment can be done directly
5650 -- by the back end, then do not expand further.
5652 -- ??? We can also do that if in place expansion is not possible but
5653 -- then we could go into an infinite recursion.
5655 if (In_Place_Assign_OK_For_Declaration or else Maybe_In_Place_OK)
5656 and then not AAMP_On_Target
5657 and then not CodePeer_Mode
5658 and then not Generate_C_Code
5659 and then not Possible_Bit_Aligned_Component (Target)
5660 and then not Is_Possibly_Unaligned_Slice (Target)
5661 and then Aggr_Assignment_OK_For_Backend (N)
5662 then
5663 if Maybe_In_Place_OK then
5664 return;
5665 end if;
5667 Aggr_Code :=
5668 New_List (
5669 Make_Assignment_Statement (Loc,
5670 Name => Target,
5671 Expression => New_Copy (N)));
5673 else
5674 Aggr_Code :=
5675 Build_Array_Aggr_Code (N,
5676 Ctype => Ctyp,
5677 Index => First_Index (Typ),
5678 Into => Target,
5679 Scalar_Comp => Is_Scalar_Type (Ctyp));
5680 end if;
5682 -- Save the last assignment statement associated with the aggregate
5683 -- when building a controlled object. This reference is utilized by
5684 -- the finalization machinery when marking an object as successfully
5685 -- initialized.
5687 if Needs_Finalization (Typ)
5688 and then Is_Entity_Name (Target)
5689 and then Present (Entity (Target))
5690 and then Ekind_In (Entity (Target), E_Constant, E_Variable)
5691 then
5692 Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code));
5693 end if;
5694 end;
5696 -- If the aggregate is the expression in a declaration, the expanded
5697 -- code must be inserted after it. The defining entity might not come
5698 -- from source if this is part of an inlined body, but the declaration
5699 -- itself will.
5701 if Comes_From_Source (Tmp)
5702 or else
5703 (Nkind (Parent (N)) = N_Object_Declaration
5704 and then Comes_From_Source (Parent (N))
5705 and then Tmp = Defining_Entity (Parent (N)))
5706 then
5707 declare
5708 Node_After : constant Node_Id := Next (Parent_Node);
5710 begin
5711 Insert_Actions_After (Parent_Node, Aggr_Code);
5713 if Parent_Kind = N_Object_Declaration then
5714 Collect_Initialization_Statements
5715 (Obj => Tmp, N => Parent_Node, Node_After => Node_After);
5716 end if;
5717 end;
5719 else
5720 Insert_Actions (N, Aggr_Code);
5721 end if;
5723 -- If the aggregate has been assigned in place, remove the original
5724 -- assignment.
5726 if Nkind (Parent (N)) = N_Assignment_Statement
5727 and then Maybe_In_Place_OK
5728 then
5729 Rewrite (Parent (N), Make_Null_Statement (Loc));
5731 elsif Nkind (Parent (N)) /= N_Object_Declaration
5732 or else Tmp /= Defining_Identifier (Parent (N))
5733 then
5734 Rewrite (N, New_Occurrence_Of (Tmp, Loc));
5735 Analyze_And_Resolve (N, Typ);
5736 end if;
5737 end Expand_Array_Aggregate;
5739 ------------------------
5740 -- Expand_N_Aggregate --
5741 ------------------------
5743 procedure Expand_N_Aggregate (N : Node_Id) is
5744 begin
5745 -- Record aggregate case
5747 if Is_Record_Type (Etype (N)) then
5748 Expand_Record_Aggregate (N);
5750 -- Array aggregate case
5752 else
5753 -- A special case, if we have a string subtype with bounds 1 .. N,
5754 -- where N is known at compile time, and the aggregate is of the
5755 -- form (others => 'x'), with a single choice and no expressions,
5756 -- and N is less than 80 (an arbitrary limit for now), then replace
5757 -- the aggregate by the equivalent string literal (but do not mark
5758 -- it as static since it is not).
5760 -- Note: this entire circuit is redundant with respect to code in
5761 -- Expand_Array_Aggregate that collapses others choices to positional
5762 -- form, but there are two problems with that circuit:
5764 -- a) It is limited to very small cases due to ill-understood
5765 -- interactions with bootstrapping. That limit is removed by
5766 -- use of the No_Implicit_Loops restriction.
5768 -- b) It incorrectly ends up with the resulting expressions being
5769 -- considered static when they are not. For example, the
5770 -- following test should fail:
5772 -- pragma Restrictions (No_Implicit_Loops);
5773 -- package NonSOthers4 is
5774 -- B : constant String (1 .. 6) := (others => 'A');
5775 -- DH : constant String (1 .. 8) := B & "BB";
5776 -- X : Integer;
5777 -- pragma Export (C, X, Link_Name => DH);
5778 -- end;
5780 -- But it succeeds (DH looks static to pragma Export)
5782 -- To be sorted out ???
5784 if Present (Component_Associations (N)) then
5785 declare
5786 CA : constant Node_Id := First (Component_Associations (N));
5787 MX : constant := 80;
5789 begin
5790 if Nkind (First (Choices (CA))) = N_Others_Choice
5791 and then Nkind (Expression (CA)) = N_Character_Literal
5792 and then No (Expressions (N))
5793 then
5794 declare
5795 T : constant Entity_Id := Etype (N);
5796 X : constant Node_Id := First_Index (T);
5797 EC : constant Node_Id := Expression (CA);
5798 CV : constant Uint := Char_Literal_Value (EC);
5799 CC : constant Int := UI_To_Int (CV);
5801 begin
5802 if Nkind (X) = N_Range
5803 and then Compile_Time_Known_Value (Low_Bound (X))
5804 and then Expr_Value (Low_Bound (X)) = 1
5805 and then Compile_Time_Known_Value (High_Bound (X))
5806 then
5807 declare
5808 Hi : constant Uint := Expr_Value (High_Bound (X));
5810 begin
5811 if Hi <= MX then
5812 Start_String;
5814 for J in 1 .. UI_To_Int (Hi) loop
5815 Store_String_Char (Char_Code (CC));
5816 end loop;
5818 Rewrite (N,
5819 Make_String_Literal (Sloc (N),
5820 Strval => End_String));
5822 if CC >= Int (2 ** 16) then
5823 Set_Has_Wide_Wide_Character (N);
5824 elsif CC >= Int (2 ** 8) then
5825 Set_Has_Wide_Character (N);
5826 end if;
5828 Analyze_And_Resolve (N, T);
5829 Set_Is_Static_Expression (N, False);
5830 return;
5831 end if;
5832 end;
5833 end if;
5834 end;
5835 end if;
5836 end;
5837 end if;
5839 -- Not that special case, so normal expansion of array aggregate
5841 Expand_Array_Aggregate (N);
5842 end if;
5844 exception
5845 when RE_Not_Available =>
5846 return;
5847 end Expand_N_Aggregate;
5849 ----------------------------------
5850 -- Expand_N_Extension_Aggregate --
5851 ----------------------------------
5853 -- If the ancestor part is an expression, add a component association for
5854 -- the parent field. If the type of the ancestor part is not the direct
5855 -- parent of the expected type, build recursively the needed ancestors.
5856 -- If the ancestor part is a subtype_mark, replace aggregate with a decla-
5857 -- ration for a temporary of the expected type, followed by individual
5858 -- assignments to the given components.
5860 procedure Expand_N_Extension_Aggregate (N : Node_Id) is
5861 Loc : constant Source_Ptr := Sloc (N);
5862 A : constant Node_Id := Ancestor_Part (N);
5863 Typ : constant Entity_Id := Etype (N);
5865 begin
5866 -- If the ancestor is a subtype mark, an init proc must be called
5867 -- on the resulting object which thus has to be materialized in
5868 -- the front-end
5870 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
5871 Convert_To_Assignments (N, Typ);
5873 -- The extension aggregate is transformed into a record aggregate
5874 -- of the following form (c1 and c2 are inherited components)
5876 -- (Exp with c3 => a, c4 => b)
5877 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c3 => a, c4 => b)
5879 else
5880 Set_Etype (N, Typ);
5882 if Tagged_Type_Expansion then
5883 Expand_Record_Aggregate (N,
5884 Orig_Tag =>
5885 New_Occurrence_Of
5886 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc),
5887 Parent_Expr => A);
5889 -- No tag is needed in the case of a VM
5891 else
5892 Expand_Record_Aggregate (N, Parent_Expr => A);
5893 end if;
5894 end if;
5896 exception
5897 when RE_Not_Available =>
5898 return;
5899 end Expand_N_Extension_Aggregate;
5901 -----------------------------
5902 -- Expand_Record_Aggregate --
5903 -----------------------------
5905 procedure Expand_Record_Aggregate
5906 (N : Node_Id;
5907 Orig_Tag : Node_Id := Empty;
5908 Parent_Expr : Node_Id := Empty)
5910 Loc : constant Source_Ptr := Sloc (N);
5911 Comps : constant List_Id := Component_Associations (N);
5912 Typ : constant Entity_Id := Etype (N);
5913 Base_Typ : constant Entity_Id := Base_Type (Typ);
5915 Static_Components : Boolean := True;
5916 -- Flag to indicate whether all components are compile-time known,
5917 -- and the aggregate can be constructed statically and handled by
5918 -- the back-end.
5920 function Compile_Time_Known_Composite_Value (N : Node_Id) return Boolean;
5921 -- Returns true if N is an expression of composite type which can be
5922 -- fully evaluated at compile time without raising constraint error.
5923 -- Such expressions can be passed as is to Gigi without any expansion.
5925 -- This returns true for N_Aggregate with Compile_Time_Known_Aggregate
5926 -- set and constants whose expression is such an aggregate, recursively.
5928 function Component_Not_OK_For_Backend return Boolean;
5929 -- Check for presence of a component which makes it impossible for the
5930 -- backend to process the aggregate, thus requiring the use of a series
5931 -- of assignment statements. Cases checked for are a nested aggregate
5932 -- needing Late_Expansion, the presence of a tagged component which may
5933 -- need tag adjustment, and a bit unaligned component reference.
5935 -- We also force expansion into assignments if a component is of a
5936 -- mutable type (including a private type with discriminants) because
5937 -- in that case the size of the component to be copied may be smaller
5938 -- than the side of the target, and there is no simple way for gigi
5939 -- to compute the size of the object to be copied.
5941 -- NOTE: This is part of the ongoing work to define precisely the
5942 -- interface between front-end and back-end handling of aggregates.
5943 -- In general it is desirable to pass aggregates as they are to gigi,
5944 -- in order to minimize elaboration code. This is one case where the
5945 -- semantics of Ada complicate the analysis and lead to anomalies in
5946 -- the gcc back-end if the aggregate is not expanded into assignments.
5948 function Has_Per_Object_Constraint (L : List_Id) return Boolean;
5949 -- Return True if any element of L has Has_Per_Object_Constraint set.
5950 -- L should be the Choices component of an N_Component_Association.
5952 function Has_Visible_Private_Ancestor (Id : E) return Boolean;
5953 -- If any ancestor of the current type is private, the aggregate
5954 -- cannot be built in place. We cannot rely on Has_Private_Ancestor,
5955 -- because it will not be set when type and its parent are in the
5956 -- same scope, and the parent component needs expansion.
5958 function Top_Level_Aggregate (N : Node_Id) return Node_Id;
5959 -- For nested aggregates return the ultimate enclosing aggregate; for
5960 -- non-nested aggregates return N.
5962 ----------------------------------------
5963 -- Compile_Time_Known_Composite_Value --
5964 ----------------------------------------
5966 function Compile_Time_Known_Composite_Value
5967 (N : Node_Id) return Boolean
5969 begin
5970 -- If we have an entity name, then see if it is the name of a
5971 -- constant and if so, test the corresponding constant value.
5973 if Is_Entity_Name (N) then
5974 declare
5975 E : constant Entity_Id := Entity (N);
5976 V : Node_Id;
5977 begin
5978 if Ekind (E) /= E_Constant then
5979 return False;
5980 else
5981 V := Constant_Value (E);
5982 return Present (V)
5983 and then Compile_Time_Known_Composite_Value (V);
5984 end if;
5985 end;
5987 -- We have a value, see if it is compile time known
5989 else
5990 if Nkind (N) = N_Aggregate then
5991 return Compile_Time_Known_Aggregate (N);
5992 end if;
5994 -- All other types of values are not known at compile time
5996 return False;
5997 end if;
5999 end Compile_Time_Known_Composite_Value;
6001 ----------------------------------
6002 -- Component_Not_OK_For_Backend --
6003 ----------------------------------
6005 function Component_Not_OK_For_Backend return Boolean is
6006 C : Node_Id;
6007 Expr_Q : Node_Id;
6009 begin
6010 if No (Comps) then
6011 return False;
6012 end if;
6014 C := First (Comps);
6015 while Present (C) loop
6017 -- If the component has box initialization, expansion is needed
6018 -- and component is not ready for backend.
6020 if Box_Present (C) then
6021 return True;
6022 end if;
6024 if Nkind (Expression (C)) = N_Qualified_Expression then
6025 Expr_Q := Expression (Expression (C));
6026 else
6027 Expr_Q := Expression (C);
6028 end if;
6030 -- Return true if the aggregate has any associations for tagged
6031 -- components that may require tag adjustment.
6033 -- These are cases where the source expression may have a tag that
6034 -- could differ from the component tag (e.g., can occur for type
6035 -- conversions and formal parameters). (Tag adjustment not needed
6036 -- if Tagged_Type_Expansion because object tags are implicit in
6037 -- the machine.)
6039 if Is_Tagged_Type (Etype (Expr_Q))
6040 and then (Nkind (Expr_Q) = N_Type_Conversion
6041 or else (Is_Entity_Name (Expr_Q)
6042 and then
6043 Ekind (Entity (Expr_Q)) in Formal_Kind))
6044 and then Tagged_Type_Expansion
6045 then
6046 Static_Components := False;
6047 return True;
6049 elsif Is_Delayed_Aggregate (Expr_Q) then
6050 Static_Components := False;
6051 return True;
6053 elsif Possible_Bit_Aligned_Component (Expr_Q) then
6054 Static_Components := False;
6055 return True;
6057 elsif Modify_Tree_For_C
6058 and then Nkind (C) = N_Component_Association
6059 and then Has_Per_Object_Constraint (Choices (C))
6060 then
6061 Static_Components := False;
6062 return True;
6063 end if;
6065 if Is_Elementary_Type (Etype (Expr_Q)) then
6066 if not Compile_Time_Known_Value (Expr_Q) then
6067 Static_Components := False;
6068 end if;
6070 elsif not Compile_Time_Known_Composite_Value (Expr_Q) then
6071 Static_Components := False;
6073 if Is_Private_Type (Etype (Expr_Q))
6074 and then Has_Discriminants (Etype (Expr_Q))
6075 then
6076 return True;
6077 end if;
6078 end if;
6080 Next (C);
6081 end loop;
6083 return False;
6084 end Component_Not_OK_For_Backend;
6086 -------------------------------
6087 -- Has_Per_Object_Constraint --
6088 -------------------------------
6090 function Has_Per_Object_Constraint (L : List_Id) return Boolean is
6091 N : Node_Id := First (L);
6092 begin
6093 while Present (N) loop
6094 if Has_Per_Object_Constraint (Associated_Node (N)) then
6095 return True;
6096 end if;
6098 Next (N);
6099 end loop;
6101 return False;
6102 end Has_Per_Object_Constraint;
6104 -----------------------------------
6105 -- Has_Visible_Private_Ancestor --
6106 -----------------------------------
6108 function Has_Visible_Private_Ancestor (Id : E) return Boolean is
6109 R : constant Entity_Id := Root_Type (Id);
6110 T1 : Entity_Id := Id;
6112 begin
6113 loop
6114 if Is_Private_Type (T1) then
6115 return True;
6117 elsif T1 = R then
6118 return False;
6120 else
6121 T1 := Etype (T1);
6122 end if;
6123 end loop;
6124 end Has_Visible_Private_Ancestor;
6126 -------------------------
6127 -- Top_Level_Aggregate --
6128 -------------------------
6130 function Top_Level_Aggregate (N : Node_Id) return Node_Id is
6131 Aggr : Node_Id;
6133 begin
6134 Aggr := N;
6135 while Present (Parent (Aggr))
6136 and then Nkind_In (Parent (Aggr), N_Component_Association,
6137 N_Aggregate)
6138 loop
6139 Aggr := Parent (Aggr);
6140 end loop;
6142 return Aggr;
6143 end Top_Level_Aggregate;
6145 -- Local variables
6147 Top_Level_Aggr : constant Node_Id := Top_Level_Aggregate (N);
6148 Tag_Value : Node_Id;
6149 Comp : Entity_Id;
6150 New_Comp : Node_Id;
6152 -- Start of processing for Expand_Record_Aggregate
6154 begin
6155 -- If the aggregate is to be assigned to an atomic/VFA variable, we have
6156 -- to prevent a piecemeal assignment even if the aggregate is to be
6157 -- expanded. We create a temporary for the aggregate, and assign the
6158 -- temporary instead, so that the back end can generate an atomic move
6159 -- for it.
6161 if Is_Atomic_VFA_Aggregate (N) then
6162 return;
6164 -- No special management required for aggregates used to initialize
6165 -- statically allocated dispatch tables
6167 elsif Is_Static_Dispatch_Table_Aggregate (N) then
6168 return;
6169 end if;
6171 -- Ada 2005 (AI-318-2): We need to convert to assignments if components
6172 -- are build-in-place function calls. The assignments will each turn
6173 -- into a build-in-place function call. If components are all static,
6174 -- we can pass the aggregate to the backend regardless of limitedness.
6176 -- Extension aggregates, aggregates in extended return statements, and
6177 -- aggregates for C++ imported types must be expanded.
6179 if Ada_Version >= Ada_2005 and then Is_Limited_View (Typ) then
6180 if not Nkind_In (Parent (N), N_Object_Declaration,
6181 N_Component_Association)
6182 then
6183 Convert_To_Assignments (N, Typ);
6185 elsif Nkind (N) = N_Extension_Aggregate
6186 or else Convention (Typ) = Convention_CPP
6187 then
6188 Convert_To_Assignments (N, Typ);
6190 elsif not Size_Known_At_Compile_Time (Typ)
6191 or else Component_Not_OK_For_Backend
6192 or else not Static_Components
6193 then
6194 Convert_To_Assignments (N, Typ);
6196 else
6197 Set_Compile_Time_Known_Aggregate (N);
6198 Set_Expansion_Delayed (N, False);
6199 end if;
6201 -- Gigi doesn't properly handle temporaries of variable size so we
6202 -- generate it in the front-end
6204 elsif not Size_Known_At_Compile_Time (Typ)
6205 and then Tagged_Type_Expansion
6206 then
6207 Convert_To_Assignments (N, Typ);
6209 -- An aggregate used to initialize a controlled object must be turned
6210 -- into component assignments as the components themselves may require
6211 -- finalization actions such as adjustment.
6213 elsif Needs_Finalization (Typ) then
6214 Convert_To_Assignments (N, Typ);
6216 -- Ada 2005 (AI-287): In case of default initialized components we
6217 -- convert the aggregate into assignments.
6219 elsif Has_Default_Init_Comps (N) then
6220 Convert_To_Assignments (N, Typ);
6222 -- Check components
6224 elsif Component_Not_OK_For_Backend then
6225 Convert_To_Assignments (N, Typ);
6227 -- If an ancestor is private, some components are not inherited and we
6228 -- cannot expand into a record aggregate.
6230 elsif Has_Visible_Private_Ancestor (Typ) then
6231 Convert_To_Assignments (N, Typ);
6233 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
6234 -- is not able to handle the aggregate for Late_Request.
6236 elsif Is_Tagged_Type (Typ) and then Has_Discriminants (Typ) then
6237 Convert_To_Assignments (N, Typ);
6239 -- If the tagged types covers interface types we need to initialize all
6240 -- hidden components containing pointers to secondary dispatch tables.
6242 elsif Is_Tagged_Type (Typ) and then Has_Interfaces (Typ) then
6243 Convert_To_Assignments (N, Typ);
6245 -- If some components are mutable, the size of the aggregate component
6246 -- may be distinct from the default size of the type component, so
6247 -- we need to expand to insure that the back-end copies the proper
6248 -- size of the data. However, if the aggregate is the initial value of
6249 -- a constant, the target is immutable and might be built statically
6250 -- if components are appropriate.
6252 elsif Has_Mutable_Components (Typ)
6253 and then
6254 (Nkind (Parent (Top_Level_Aggr)) /= N_Object_Declaration
6255 or else not Constant_Present (Parent (Top_Level_Aggr))
6256 or else not Static_Components)
6257 then
6258 Convert_To_Assignments (N, Typ);
6260 -- If the type involved has bit aligned components, then we are not sure
6261 -- that the back end can handle this case correctly.
6263 elsif Type_May_Have_Bit_Aligned_Components (Typ) then
6264 Convert_To_Assignments (N, Typ);
6266 -- When generating C, only generate an aggregate when declaring objects
6267 -- since C does not support aggregates in e.g. assignment statements.
6269 elsif Modify_Tree_For_C and then not In_Object_Declaration (N) then
6270 Convert_To_Assignments (N, Typ);
6272 -- In all other cases, build a proper aggregate to be handled by gigi
6274 else
6275 if Nkind (N) = N_Aggregate then
6277 -- If the aggregate is static and can be handled by the back-end,
6278 -- nothing left to do.
6280 if Static_Components then
6281 Set_Compile_Time_Known_Aggregate (N);
6282 Set_Expansion_Delayed (N, False);
6283 end if;
6284 end if;
6286 -- If no discriminants, nothing special to do
6288 if not Has_Discriminants (Typ) then
6289 null;
6291 -- Case of discriminants present
6293 elsif Is_Derived_Type (Typ) then
6295 -- For untagged types, non-stored discriminants are replaced
6296 -- with stored discriminants, which are the ones that gigi uses
6297 -- to describe the type and its components.
6299 Generate_Aggregate_For_Derived_Type : declare
6300 Constraints : constant List_Id := New_List;
6301 First_Comp : Node_Id;
6302 Discriminant : Entity_Id;
6303 Decl : Node_Id;
6304 Num_Disc : Nat := 0;
6305 Num_Gird : Nat := 0;
6307 procedure Prepend_Stored_Values (T : Entity_Id);
6308 -- Scan the list of stored discriminants of the type, and add
6309 -- their values to the aggregate being built.
6311 ---------------------------
6312 -- Prepend_Stored_Values --
6313 ---------------------------
6315 procedure Prepend_Stored_Values (T : Entity_Id) is
6316 begin
6317 Discriminant := First_Stored_Discriminant (T);
6318 while Present (Discriminant) loop
6319 New_Comp :=
6320 Make_Component_Association (Loc,
6321 Choices =>
6322 New_List (New_Occurrence_Of (Discriminant, Loc)),
6324 Expression =>
6325 New_Copy_Tree
6326 (Get_Discriminant_Value
6327 (Discriminant,
6328 Typ,
6329 Discriminant_Constraint (Typ))));
6331 if No (First_Comp) then
6332 Prepend_To (Component_Associations (N), New_Comp);
6333 else
6334 Insert_After (First_Comp, New_Comp);
6335 end if;
6337 First_Comp := New_Comp;
6338 Next_Stored_Discriminant (Discriminant);
6339 end loop;
6340 end Prepend_Stored_Values;
6342 -- Start of processing for Generate_Aggregate_For_Derived_Type
6344 begin
6345 -- Remove the associations for the discriminant of derived type
6347 First_Comp := First (Component_Associations (N));
6348 while Present (First_Comp) loop
6349 Comp := First_Comp;
6350 Next (First_Comp);
6352 if Ekind (Entity (First (Choices (Comp)))) = E_Discriminant
6353 then
6354 Remove (Comp);
6355 Num_Disc := Num_Disc + 1;
6356 end if;
6357 end loop;
6359 -- Insert stored discriminant associations in the correct
6360 -- order. If there are more stored discriminants than new
6361 -- discriminants, there is at least one new discriminant that
6362 -- constrains more than one of the stored discriminants. In
6363 -- this case we need to construct a proper subtype of the
6364 -- parent type, in order to supply values to all the
6365 -- components. Otherwise there is one-one correspondence
6366 -- between the constraints and the stored discriminants.
6368 First_Comp := Empty;
6370 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
6371 while Present (Discriminant) loop
6372 Num_Gird := Num_Gird + 1;
6373 Next_Stored_Discriminant (Discriminant);
6374 end loop;
6376 -- Case of more stored discriminants than new discriminants
6378 if Num_Gird > Num_Disc then
6380 -- Create a proper subtype of the parent type, which is the
6381 -- proper implementation type for the aggregate, and convert
6382 -- it to the intended target type.
6384 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
6385 while Present (Discriminant) loop
6386 New_Comp :=
6387 New_Copy_Tree
6388 (Get_Discriminant_Value
6389 (Discriminant,
6390 Typ,
6391 Discriminant_Constraint (Typ)));
6392 Append (New_Comp, Constraints);
6393 Next_Stored_Discriminant (Discriminant);
6394 end loop;
6396 Decl :=
6397 Make_Subtype_Declaration (Loc,
6398 Defining_Identifier => Make_Temporary (Loc, 'T'),
6399 Subtype_Indication =>
6400 Make_Subtype_Indication (Loc,
6401 Subtype_Mark =>
6402 New_Occurrence_Of (Etype (Base_Type (Typ)), Loc),
6403 Constraint =>
6404 Make_Index_Or_Discriminant_Constraint
6405 (Loc, Constraints)));
6407 Insert_Action (N, Decl);
6408 Prepend_Stored_Values (Base_Type (Typ));
6410 Set_Etype (N, Defining_Identifier (Decl));
6411 Set_Analyzed (N);
6413 Rewrite (N, Unchecked_Convert_To (Typ, N));
6414 Analyze (N);
6416 -- Case where we do not have fewer new discriminants than
6417 -- stored discriminants, so in this case we can simply use the
6418 -- stored discriminants of the subtype.
6420 else
6421 Prepend_Stored_Values (Typ);
6422 end if;
6423 end Generate_Aggregate_For_Derived_Type;
6424 end if;
6426 if Is_Tagged_Type (Typ) then
6428 -- In the tagged case, _parent and _tag component must be created
6430 -- Reset Null_Present unconditionally. Tagged records always have
6431 -- at least one field (the tag or the parent).
6433 Set_Null_Record_Present (N, False);
6435 -- When the current aggregate comes from the expansion of an
6436 -- extension aggregate, the parent expr is replaced by an
6437 -- aggregate formed by selected components of this expr.
6439 if Present (Parent_Expr) and then Is_Empty_List (Comps) then
6440 Comp := First_Component_Or_Discriminant (Typ);
6441 while Present (Comp) loop
6443 -- Skip all expander-generated components
6445 if not Comes_From_Source (Original_Record_Component (Comp))
6446 then
6447 null;
6449 else
6450 New_Comp :=
6451 Make_Selected_Component (Loc,
6452 Prefix =>
6453 Unchecked_Convert_To (Typ,
6454 Duplicate_Subexpr (Parent_Expr, True)),
6455 Selector_Name => New_Occurrence_Of (Comp, Loc));
6457 Append_To (Comps,
6458 Make_Component_Association (Loc,
6459 Choices =>
6460 New_List (New_Occurrence_Of (Comp, Loc)),
6461 Expression => New_Comp));
6463 Analyze_And_Resolve (New_Comp, Etype (Comp));
6464 end if;
6466 Next_Component_Or_Discriminant (Comp);
6467 end loop;
6468 end if;
6470 -- Compute the value for the Tag now, if the type is a root it
6471 -- will be included in the aggregate right away, otherwise it will
6472 -- be propagated to the parent aggregate.
6474 if Present (Orig_Tag) then
6475 Tag_Value := Orig_Tag;
6476 elsif not Tagged_Type_Expansion then
6477 Tag_Value := Empty;
6478 else
6479 Tag_Value :=
6480 New_Occurrence_Of
6481 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc);
6482 end if;
6484 -- For a derived type, an aggregate for the parent is formed with
6485 -- all the inherited components.
6487 if Is_Derived_Type (Typ) then
6489 declare
6490 First_Comp : Node_Id;
6491 Parent_Comps : List_Id;
6492 Parent_Aggr : Node_Id;
6493 Parent_Name : Node_Id;
6495 begin
6496 -- Remove the inherited component association from the
6497 -- aggregate and store them in the parent aggregate
6499 First_Comp := First (Component_Associations (N));
6500 Parent_Comps := New_List;
6501 while Present (First_Comp)
6502 and then
6503 Scope (Original_Record_Component
6504 (Entity (First (Choices (First_Comp))))) /=
6505 Base_Typ
6506 loop
6507 Comp := First_Comp;
6508 Next (First_Comp);
6509 Remove (Comp);
6510 Append (Comp, Parent_Comps);
6511 end loop;
6513 Parent_Aggr :=
6514 Make_Aggregate (Loc,
6515 Component_Associations => Parent_Comps);
6516 Set_Etype (Parent_Aggr, Etype (Base_Type (Typ)));
6518 -- Find the _parent component
6520 Comp := First_Component (Typ);
6521 while Chars (Comp) /= Name_uParent loop
6522 Comp := Next_Component (Comp);
6523 end loop;
6525 Parent_Name := New_Occurrence_Of (Comp, Loc);
6527 -- Insert the parent aggregate
6529 Prepend_To (Component_Associations (N),
6530 Make_Component_Association (Loc,
6531 Choices => New_List (Parent_Name),
6532 Expression => Parent_Aggr));
6534 -- Expand recursively the parent propagating the right Tag
6536 Expand_Record_Aggregate
6537 (Parent_Aggr, Tag_Value, Parent_Expr);
6539 -- The ancestor part may be a nested aggregate that has
6540 -- delayed expansion: recheck now.
6542 if Component_Not_OK_For_Backend then
6543 Convert_To_Assignments (N, Typ);
6544 end if;
6545 end;
6547 -- For a root type, the tag component is added (unless compiling
6548 -- for the VMs, where tags are implicit).
6550 elsif Tagged_Type_Expansion then
6551 declare
6552 Tag_Name : constant Node_Id :=
6553 New_Occurrence_Of (First_Tag_Component (Typ), Loc);
6554 Typ_Tag : constant Entity_Id := RTE (RE_Tag);
6555 Conv_Node : constant Node_Id :=
6556 Unchecked_Convert_To (Typ_Tag, Tag_Value);
6558 begin
6559 Set_Etype (Conv_Node, Typ_Tag);
6560 Prepend_To (Component_Associations (N),
6561 Make_Component_Association (Loc,
6562 Choices => New_List (Tag_Name),
6563 Expression => Conv_Node));
6564 end;
6565 end if;
6566 end if;
6567 end if;
6569 end Expand_Record_Aggregate;
6571 ----------------------------
6572 -- Has_Default_Init_Comps --
6573 ----------------------------
6575 function Has_Default_Init_Comps (N : Node_Id) return Boolean is
6576 Comps : constant List_Id := Component_Associations (N);
6577 C : Node_Id;
6578 Expr : Node_Id;
6580 begin
6581 pragma Assert (Nkind_In (N, N_Aggregate, N_Extension_Aggregate));
6583 if No (Comps) then
6584 return False;
6585 end if;
6587 if Has_Self_Reference (N) then
6588 return True;
6589 end if;
6591 -- Check if any direct component has default initialized components
6593 C := First (Comps);
6594 while Present (C) loop
6595 if Box_Present (C) then
6596 return True;
6597 end if;
6599 Next (C);
6600 end loop;
6602 -- Recursive call in case of aggregate expression
6604 C := First (Comps);
6605 while Present (C) loop
6606 Expr := Expression (C);
6608 if Present (Expr)
6609 and then Nkind_In (Expr, N_Aggregate, N_Extension_Aggregate)
6610 and then Has_Default_Init_Comps (Expr)
6611 then
6612 return True;
6613 end if;
6615 Next (C);
6616 end loop;
6618 return False;
6619 end Has_Default_Init_Comps;
6621 --------------------------
6622 -- Is_Delayed_Aggregate --
6623 --------------------------
6625 function Is_Delayed_Aggregate (N : Node_Id) return Boolean is
6626 Node : Node_Id := N;
6627 Kind : Node_Kind := Nkind (Node);
6629 begin
6630 if Kind = N_Qualified_Expression then
6631 Node := Expression (Node);
6632 Kind := Nkind (Node);
6633 end if;
6635 if not Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate) then
6636 return False;
6637 else
6638 return Expansion_Delayed (Node);
6639 end if;
6640 end Is_Delayed_Aggregate;
6642 ---------------------------
6643 -- In_Object_Declaration --
6644 ---------------------------
6646 function In_Object_Declaration (N : Node_Id) return Boolean is
6647 P : Node_Id := Parent (N);
6648 begin
6649 while Present (P) loop
6650 if Nkind (P) = N_Object_Declaration then
6651 return True;
6652 end if;
6654 P := Parent (P);
6655 end loop;
6657 return False;
6658 end In_Object_Declaration;
6660 ----------------------------------------
6661 -- Is_Static_Dispatch_Table_Aggregate --
6662 ----------------------------------------
6664 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean is
6665 Typ : constant Entity_Id := Base_Type (Etype (N));
6667 begin
6668 return Static_Dispatch_Tables
6669 and then Tagged_Type_Expansion
6670 and then RTU_Loaded (Ada_Tags)
6672 -- Avoid circularity when rebuilding the compiler
6674 and then Cunit_Entity (Get_Source_Unit (N)) /= RTU_Entity (Ada_Tags)
6675 and then (Typ = RTE (RE_Dispatch_Table_Wrapper)
6676 or else
6677 Typ = RTE (RE_Address_Array)
6678 or else
6679 Typ = RTE (RE_Type_Specific_Data)
6680 or else
6681 Typ = RTE (RE_Tag_Table)
6682 or else
6683 (RTE_Available (RE_Interface_Data)
6684 and then Typ = RTE (RE_Interface_Data))
6685 or else
6686 (RTE_Available (RE_Interfaces_Array)
6687 and then Typ = RTE (RE_Interfaces_Array))
6688 or else
6689 (RTE_Available (RE_Interface_Data_Element)
6690 and then Typ = RTE (RE_Interface_Data_Element)));
6691 end Is_Static_Dispatch_Table_Aggregate;
6693 -----------------------------
6694 -- Is_Two_Dim_Packed_Array --
6695 -----------------------------
6697 function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean is
6698 C : constant Int := UI_To_Int (Component_Size (Typ));
6699 begin
6700 return Number_Dimensions (Typ) = 2
6701 and then Is_Bit_Packed_Array (Typ)
6702 and then (C = 1 or else C = 2 or else C = 4);
6703 end Is_Two_Dim_Packed_Array;
6705 --------------------
6706 -- Late_Expansion --
6707 --------------------
6709 function Late_Expansion
6710 (N : Node_Id;
6711 Typ : Entity_Id;
6712 Target : Node_Id) return List_Id
6714 Aggr_Code : List_Id;
6716 begin
6717 if Is_Array_Type (Etype (N)) then
6718 Aggr_Code :=
6719 Build_Array_Aggr_Code
6720 (N => N,
6721 Ctype => Component_Type (Etype (N)),
6722 Index => First_Index (Typ),
6723 Into => Target,
6724 Scalar_Comp => Is_Scalar_Type (Component_Type (Typ)),
6725 Indexes => No_List);
6727 -- Directly or indirectly (e.g. access protected procedure) a record
6729 else
6730 Aggr_Code := Build_Record_Aggr_Code (N, Typ, Target);
6731 end if;
6733 -- Save the last assignment statement associated with the aggregate
6734 -- when building a controlled object. This reference is utilized by
6735 -- the finalization machinery when marking an object as successfully
6736 -- initialized.
6738 if Needs_Finalization (Typ)
6739 and then Is_Entity_Name (Target)
6740 and then Present (Entity (Target))
6741 and then Ekind_In (Entity (Target), E_Constant, E_Variable)
6742 then
6743 Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code));
6744 end if;
6746 return Aggr_Code;
6747 end Late_Expansion;
6749 ----------------------------------
6750 -- Make_OK_Assignment_Statement --
6751 ----------------------------------
6753 function Make_OK_Assignment_Statement
6754 (Sloc : Source_Ptr;
6755 Name : Node_Id;
6756 Expression : Node_Id) return Node_Id
6758 begin
6759 Set_Assignment_OK (Name);
6760 return Make_Assignment_Statement (Sloc, Name, Expression);
6761 end Make_OK_Assignment_Statement;
6763 -----------------------
6764 -- Number_Of_Choices --
6765 -----------------------
6767 function Number_Of_Choices (N : Node_Id) return Nat is
6768 Assoc : Node_Id;
6769 Choice : Node_Id;
6771 Nb_Choices : Nat := 0;
6773 begin
6774 if Present (Expressions (N)) then
6775 return 0;
6776 end if;
6778 Assoc := First (Component_Associations (N));
6779 while Present (Assoc) loop
6780 Choice := First (Choices (Assoc));
6781 while Present (Choice) loop
6782 if Nkind (Choice) /= N_Others_Choice then
6783 Nb_Choices := Nb_Choices + 1;
6784 end if;
6786 Next (Choice);
6787 end loop;
6789 Next (Assoc);
6790 end loop;
6792 return Nb_Choices;
6793 end Number_Of_Choices;
6795 ------------------------------------
6796 -- Packed_Array_Aggregate_Handled --
6797 ------------------------------------
6799 -- The current version of this procedure will handle at compile time
6800 -- any array aggregate that meets these conditions:
6802 -- One and two dimensional, bit packed
6803 -- Underlying packed type is modular type
6804 -- Bounds are within 32-bit Int range
6805 -- All bounds and values are static
6807 -- Note: for now, in the 2-D case, we only handle component sizes of
6808 -- 1, 2, 4 (cases where an integral number of elements occupies a byte).
6810 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean is
6811 Loc : constant Source_Ptr := Sloc (N);
6812 Typ : constant Entity_Id := Etype (N);
6813 Ctyp : constant Entity_Id := Component_Type (Typ);
6815 Not_Handled : exception;
6816 -- Exception raised if this aggregate cannot be handled
6818 begin
6819 -- Handle one- or two dimensional bit packed array
6821 if not Is_Bit_Packed_Array (Typ)
6822 or else Number_Dimensions (Typ) > 2
6823 then
6824 return False;
6825 end if;
6827 -- If two-dimensional, check whether it can be folded, and transformed
6828 -- into a one-dimensional aggregate for the Packed_Array_Impl_Type of
6829 -- the original type.
6831 if Number_Dimensions (Typ) = 2 then
6832 return Two_Dim_Packed_Array_Handled (N);
6833 end if;
6835 if not Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)) then
6836 return False;
6837 end if;
6839 if not Is_Scalar_Type (Component_Type (Typ))
6840 and then Has_Non_Standard_Rep (Component_Type (Typ))
6841 then
6842 return False;
6843 end if;
6845 declare
6846 Csiz : constant Nat := UI_To_Int (Component_Size (Typ));
6848 Lo : Node_Id;
6849 Hi : Node_Id;
6850 -- Bounds of index type
6852 Lob : Uint;
6853 Hib : Uint;
6854 -- Values of bounds if compile time known
6856 function Get_Component_Val (N : Node_Id) return Uint;
6857 -- Given a expression value N of the component type Ctyp, returns a
6858 -- value of Csiz (component size) bits representing this value. If
6859 -- the value is non-static or any other reason exists why the value
6860 -- cannot be returned, then Not_Handled is raised.
6862 -----------------------
6863 -- Get_Component_Val --
6864 -----------------------
6866 function Get_Component_Val (N : Node_Id) return Uint is
6867 Val : Uint;
6869 begin
6870 -- We have to analyze the expression here before doing any further
6871 -- processing here. The analysis of such expressions is deferred
6872 -- till expansion to prevent some problems of premature analysis.
6874 Analyze_And_Resolve (N, Ctyp);
6876 -- Must have a compile time value. String literals have to be
6877 -- converted into temporaries as well, because they cannot easily
6878 -- be converted into their bit representation.
6880 if not Compile_Time_Known_Value (N)
6881 or else Nkind (N) = N_String_Literal
6882 then
6883 raise Not_Handled;
6884 end if;
6886 Val := Expr_Rep_Value (N);
6888 -- Adjust for bias, and strip proper number of bits
6890 if Has_Biased_Representation (Ctyp) then
6891 Val := Val - Expr_Value (Type_Low_Bound (Ctyp));
6892 end if;
6894 return Val mod Uint_2 ** Csiz;
6895 end Get_Component_Val;
6897 -- Here we know we have a one dimensional bit packed array
6899 begin
6900 Get_Index_Bounds (First_Index (Typ), Lo, Hi);
6902 -- Cannot do anything if bounds are dynamic
6904 if not Compile_Time_Known_Value (Lo)
6905 or else
6906 not Compile_Time_Known_Value (Hi)
6907 then
6908 return False;
6909 end if;
6911 -- Or are silly out of range of int bounds
6913 Lob := Expr_Value (Lo);
6914 Hib := Expr_Value (Hi);
6916 if not UI_Is_In_Int_Range (Lob)
6917 or else
6918 not UI_Is_In_Int_Range (Hib)
6919 then
6920 return False;
6921 end if;
6923 -- At this stage we have a suitable aggregate for handling at compile
6924 -- time. The only remaining checks are that the values of expressions
6925 -- in the aggregate are compile-time known (checks are performed by
6926 -- Get_Component_Val), and that any subtypes or ranges are statically
6927 -- known.
6929 -- If the aggregate is not fully positional at this stage, then
6930 -- convert it to positional form. Either this will fail, in which
6931 -- case we can do nothing, or it will succeed, in which case we have
6932 -- succeeded in handling the aggregate and transforming it into a
6933 -- modular value, or it will stay an aggregate, in which case we
6934 -- have failed to create a packed value for it.
6936 if Present (Component_Associations (N)) then
6937 Convert_To_Positional
6938 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
6939 return Nkind (N) /= N_Aggregate;
6940 end if;
6942 -- Otherwise we are all positional, so convert to proper value
6944 declare
6945 Lov : constant Int := UI_To_Int (Lob);
6946 Hiv : constant Int := UI_To_Int (Hib);
6948 Len : constant Nat := Int'Max (0, Hiv - Lov + 1);
6949 -- The length of the array (number of elements)
6951 Aggregate_Val : Uint;
6952 -- Value of aggregate. The value is set in the low order bits of
6953 -- this value. For the little-endian case, the values are stored
6954 -- from low-order to high-order and for the big-endian case the
6955 -- values are stored from high-order to low-order. Note that gigi
6956 -- will take care of the conversions to left justify the value in
6957 -- the big endian case (because of left justified modular type
6958 -- processing), so we do not have to worry about that here.
6960 Lit : Node_Id;
6961 -- Integer literal for resulting constructed value
6963 Shift : Nat;
6964 -- Shift count from low order for next value
6966 Incr : Int;
6967 -- Shift increment for loop
6969 Expr : Node_Id;
6970 -- Next expression from positional parameters of aggregate
6972 Left_Justified : Boolean;
6973 -- Set True if we are filling the high order bits of the target
6974 -- value (i.e. the value is left justified).
6976 begin
6977 -- For little endian, we fill up the low order bits of the target
6978 -- value. For big endian we fill up the high order bits of the
6979 -- target value (which is a left justified modular value).
6981 Left_Justified := Bytes_Big_Endian;
6983 -- Switch justification if using -gnatd8
6985 if Debug_Flag_8 then
6986 Left_Justified := not Left_Justified;
6987 end if;
6989 -- Switch justfification if reverse storage order
6991 if Reverse_Storage_Order (Base_Type (Typ)) then
6992 Left_Justified := not Left_Justified;
6993 end if;
6995 if Left_Justified then
6996 Shift := Csiz * (Len - 1);
6997 Incr := -Csiz;
6998 else
6999 Shift := 0;
7000 Incr := +Csiz;
7001 end if;
7003 -- Loop to set the values
7005 if Len = 0 then
7006 Aggregate_Val := Uint_0;
7007 else
7008 Expr := First (Expressions (N));
7009 Aggregate_Val := Get_Component_Val (Expr) * Uint_2 ** Shift;
7011 for J in 2 .. Len loop
7012 Shift := Shift + Incr;
7013 Next (Expr);
7014 Aggregate_Val :=
7015 Aggregate_Val + Get_Component_Val (Expr) * Uint_2 ** Shift;
7016 end loop;
7017 end if;
7019 -- Now we can rewrite with the proper value
7021 Lit := Make_Integer_Literal (Loc, Intval => Aggregate_Val);
7022 Set_Print_In_Hex (Lit);
7024 -- Construct the expression using this literal. Note that it is
7025 -- important to qualify the literal with its proper modular type
7026 -- since universal integer does not have the required range and
7027 -- also this is a left justified modular type, which is important
7028 -- in the big-endian case.
7030 Rewrite (N,
7031 Unchecked_Convert_To (Typ,
7032 Make_Qualified_Expression (Loc,
7033 Subtype_Mark =>
7034 New_Occurrence_Of (Packed_Array_Impl_Type (Typ), Loc),
7035 Expression => Lit)));
7037 Analyze_And_Resolve (N, Typ);
7038 return True;
7039 end;
7040 end;
7042 exception
7043 when Not_Handled =>
7044 return False;
7045 end Packed_Array_Aggregate_Handled;
7047 ----------------------------
7048 -- Has_Mutable_Components --
7049 ----------------------------
7051 function Has_Mutable_Components (Typ : Entity_Id) return Boolean is
7052 Comp : Entity_Id;
7054 begin
7055 Comp := First_Component (Typ);
7056 while Present (Comp) loop
7057 if Is_Record_Type (Etype (Comp))
7058 and then Has_Discriminants (Etype (Comp))
7059 and then not Is_Constrained (Etype (Comp))
7060 then
7061 return True;
7062 end if;
7064 Next_Component (Comp);
7065 end loop;
7067 return False;
7068 end Has_Mutable_Components;
7070 ------------------------------
7071 -- Initialize_Discriminants --
7072 ------------------------------
7074 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id) is
7075 Loc : constant Source_Ptr := Sloc (N);
7076 Bas : constant Entity_Id := Base_Type (Typ);
7077 Par : constant Entity_Id := Etype (Bas);
7078 Decl : constant Node_Id := Parent (Par);
7079 Ref : Node_Id;
7081 begin
7082 if Is_Tagged_Type (Bas)
7083 and then Is_Derived_Type (Bas)
7084 and then Has_Discriminants (Par)
7085 and then Has_Discriminants (Bas)
7086 and then Number_Discriminants (Bas) /= Number_Discriminants (Par)
7087 and then Nkind (Decl) = N_Full_Type_Declaration
7088 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
7089 and then
7090 Present (Variant_Part (Component_List (Type_Definition (Decl))))
7091 and then Nkind (N) /= N_Extension_Aggregate
7092 then
7094 -- Call init proc to set discriminants.
7095 -- There should eventually be a special procedure for this ???
7097 Ref := New_Occurrence_Of (Defining_Identifier (N), Loc);
7098 Insert_Actions_After (N,
7099 Build_Initialization_Call (Sloc (N), Ref, Typ));
7100 end if;
7101 end Initialize_Discriminants;
7103 ----------------
7104 -- Must_Slide --
7105 ----------------
7107 function Must_Slide
7108 (Obj_Type : Entity_Id;
7109 Typ : Entity_Id) return Boolean
7111 L1, L2, H1, H2 : Node_Id;
7113 begin
7114 -- No sliding if the type of the object is not established yet, if it is
7115 -- an unconstrained type whose actual subtype comes from the aggregate,
7116 -- or if the two types are identical.
7118 if not Is_Array_Type (Obj_Type) then
7119 return False;
7121 elsif not Is_Constrained (Obj_Type) then
7122 return False;
7124 elsif Typ = Obj_Type then
7125 return False;
7127 else
7128 -- Sliding can only occur along the first dimension
7130 Get_Index_Bounds (First_Index (Typ), L1, H1);
7131 Get_Index_Bounds (First_Index (Obj_Type), L2, H2);
7133 if not Is_OK_Static_Expression (L1) or else
7134 not Is_OK_Static_Expression (L2) or else
7135 not Is_OK_Static_Expression (H1) or else
7136 not Is_OK_Static_Expression (H2)
7137 then
7138 return False;
7139 else
7140 return Expr_Value (L1) /= Expr_Value (L2)
7141 or else
7142 Expr_Value (H1) /= Expr_Value (H2);
7143 end if;
7144 end if;
7145 end Must_Slide;
7147 ----------------------------------
7148 -- Two_Dim_Packed_Array_Handled --
7149 ----------------------------------
7151 function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean is
7152 Loc : constant Source_Ptr := Sloc (N);
7153 Typ : constant Entity_Id := Etype (N);
7154 Ctyp : constant Entity_Id := Component_Type (Typ);
7155 Comp_Size : constant Int := UI_To_Int (Component_Size (Typ));
7156 Packed_Array : constant Entity_Id :=
7157 Packed_Array_Impl_Type (Base_Type (Typ));
7159 One_Comp : Node_Id;
7160 -- Expression in original aggregate
7162 One_Dim : Node_Id;
7163 -- One-dimensional subaggregate
7165 begin
7167 -- For now, only deal with cases where an integral number of elements
7168 -- fit in a single byte. This includes the most common boolean case.
7170 if not (Comp_Size = 1 or else
7171 Comp_Size = 2 or else
7172 Comp_Size = 4)
7173 then
7174 return False;
7175 end if;
7177 Convert_To_Positional
7178 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
7180 -- Verify that all components are static
7182 if Nkind (N) = N_Aggregate
7183 and then Compile_Time_Known_Aggregate (N)
7184 then
7185 null;
7187 -- The aggregate may have been re-analyzed and converted already
7189 elsif Nkind (N) /= N_Aggregate then
7190 return True;
7192 -- If component associations remain, the aggregate is not static
7194 elsif Present (Component_Associations (N)) then
7195 return False;
7197 else
7198 One_Dim := First (Expressions (N));
7199 while Present (One_Dim) loop
7200 if Present (Component_Associations (One_Dim)) then
7201 return False;
7202 end if;
7204 One_Comp := First (Expressions (One_Dim));
7205 while Present (One_Comp) loop
7206 if not Is_OK_Static_Expression (One_Comp) then
7207 return False;
7208 end if;
7210 Next (One_Comp);
7211 end loop;
7213 Next (One_Dim);
7214 end loop;
7215 end if;
7217 -- Two-dimensional aggregate is now fully positional so pack one
7218 -- dimension to create a static one-dimensional array, and rewrite
7219 -- as an unchecked conversion to the original type.
7221 declare
7222 Byte_Size : constant Int := UI_To_Int (Component_Size (Packed_Array));
7223 -- The packed array type is a byte array
7225 Packed_Num : Nat;
7226 -- Number of components accumulated in current byte
7228 Comps : List_Id;
7229 -- Assembled list of packed values for equivalent aggregate
7231 Comp_Val : Uint;
7232 -- integer value of component
7234 Incr : Int;
7235 -- Step size for packing
7237 Init_Shift : Int;
7238 -- Endian-dependent start position for packing
7240 Shift : Int;
7241 -- Current insertion position
7243 Val : Int;
7244 -- Component of packed array being assembled.
7246 begin
7247 Comps := New_List;
7248 Val := 0;
7249 Packed_Num := 0;
7251 -- Account for endianness. See corresponding comment in
7252 -- Packed_Array_Aggregate_Handled concerning the following.
7254 if Bytes_Big_Endian
7255 xor Debug_Flag_8
7256 xor Reverse_Storage_Order (Base_Type (Typ))
7257 then
7258 Init_Shift := Byte_Size - Comp_Size;
7259 Incr := -Comp_Size;
7260 else
7261 Init_Shift := 0;
7262 Incr := +Comp_Size;
7263 end if;
7265 -- Iterate over each subaggregate
7267 Shift := Init_Shift;
7268 One_Dim := First (Expressions (N));
7269 while Present (One_Dim) loop
7270 One_Comp := First (Expressions (One_Dim));
7271 while Present (One_Comp) loop
7272 if Packed_Num = Byte_Size / Comp_Size then
7274 -- Byte is complete, add to list of expressions
7276 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
7277 Val := 0;
7278 Shift := Init_Shift;
7279 Packed_Num := 0;
7281 else
7282 Comp_Val := Expr_Rep_Value (One_Comp);
7284 -- Adjust for bias, and strip proper number of bits
7286 if Has_Biased_Representation (Ctyp) then
7287 Comp_Val := Comp_Val - Expr_Value (Type_Low_Bound (Ctyp));
7288 end if;
7290 Comp_Val := Comp_Val mod Uint_2 ** Comp_Size;
7291 Val := UI_To_Int (Val + Comp_Val * Uint_2 ** Shift);
7292 Shift := Shift + Incr;
7293 One_Comp := Next (One_Comp);
7294 Packed_Num := Packed_Num + 1;
7295 end if;
7296 end loop;
7298 One_Dim := Next (One_Dim);
7299 end loop;
7301 if Packed_Num > 0 then
7303 -- Add final incomplete byte if present
7305 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
7306 end if;
7308 Rewrite (N,
7309 Unchecked_Convert_To (Typ,
7310 Make_Qualified_Expression (Loc,
7311 Subtype_Mark => New_Occurrence_Of (Packed_Array, Loc),
7312 Expression => Make_Aggregate (Loc, Expressions => Comps))));
7313 Analyze_And_Resolve (N);
7314 return True;
7315 end;
7316 end Two_Dim_Packed_Array_Handled;
7318 ---------------------
7319 -- Sort_Case_Table --
7320 ---------------------
7322 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
7323 L : constant Int := Case_Table'First;
7324 U : constant Int := Case_Table'Last;
7325 K : Int;
7326 J : Int;
7327 T : Case_Bounds;
7329 begin
7330 K := L;
7331 while K /= U loop
7332 T := Case_Table (K + 1);
7334 J := K + 1;
7335 while J /= L
7336 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
7337 Expr_Value (T.Choice_Lo)
7338 loop
7339 Case_Table (J) := Case_Table (J - 1);
7340 J := J - 1;
7341 end loop;
7343 Case_Table (J) := T;
7344 K := K + 1;
7345 end loop;
7346 end Sort_Case_Table;
7348 ----------------------------
7349 -- Static_Array_Aggregate --
7350 ----------------------------
7352 function Static_Array_Aggregate (N : Node_Id) return Boolean is
7353 Bounds : constant Node_Id := Aggregate_Bounds (N);
7355 Typ : constant Entity_Id := Etype (N);
7356 Comp_Type : constant Entity_Id := Component_Type (Typ);
7357 Agg : Node_Id;
7358 Expr : Node_Id;
7359 Lo : Node_Id;
7360 Hi : Node_Id;
7362 begin
7363 if Is_Tagged_Type (Typ)
7364 or else Is_Controlled (Typ)
7365 or else Is_Packed (Typ)
7366 then
7367 return False;
7368 end if;
7370 if Present (Bounds)
7371 and then Nkind (Bounds) = N_Range
7372 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
7373 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal
7374 then
7375 Lo := Low_Bound (Bounds);
7376 Hi := High_Bound (Bounds);
7378 if No (Component_Associations (N)) then
7380 -- Verify that all components are static integers
7382 Expr := First (Expressions (N));
7383 while Present (Expr) loop
7384 if Nkind (Expr) /= N_Integer_Literal then
7385 return False;
7386 end if;
7388 Next (Expr);
7389 end loop;
7391 return True;
7393 else
7394 -- We allow only a single named association, either a static
7395 -- range or an others_clause, with a static expression.
7397 Expr := First (Component_Associations (N));
7399 if Present (Expressions (N)) then
7400 return False;
7402 elsif Present (Next (Expr)) then
7403 return False;
7405 elsif Present (Next (First (Choices (Expr)))) then
7406 return False;
7408 else
7409 -- The aggregate is static if all components are literals,
7410 -- or else all its components are static aggregates for the
7411 -- component type. We also limit the size of a static aggregate
7412 -- to prevent runaway static expressions.
7414 if Is_Array_Type (Comp_Type)
7415 or else Is_Record_Type (Comp_Type)
7416 then
7417 if Nkind (Expression (Expr)) /= N_Aggregate
7418 or else
7419 not Compile_Time_Known_Aggregate (Expression (Expr))
7420 then
7421 return False;
7422 end if;
7424 elsif Nkind (Expression (Expr)) /= N_Integer_Literal then
7425 return False;
7426 end if;
7428 if not Aggr_Size_OK (N, Typ) then
7429 return False;
7430 end if;
7432 -- Create a positional aggregate with the right number of
7433 -- copies of the expression.
7435 Agg := Make_Aggregate (Sloc (N), New_List, No_List);
7437 for I in UI_To_Int (Intval (Lo)) .. UI_To_Int (Intval (Hi))
7438 loop
7439 Append_To (Expressions (Agg), New_Copy (Expression (Expr)));
7441 -- The copied expression must be analyzed and resolved.
7442 -- Besides setting the type, this ensures that static
7443 -- expressions are appropriately marked as such.
7445 Analyze_And_Resolve
7446 (Last (Expressions (Agg)), Component_Type (Typ));
7447 end loop;
7449 Set_Aggregate_Bounds (Agg, Bounds);
7450 Set_Etype (Agg, Typ);
7451 Set_Analyzed (Agg);
7452 Rewrite (N, Agg);
7453 Set_Compile_Time_Known_Aggregate (N);
7455 return True;
7456 end if;
7457 end if;
7459 else
7460 return False;
7461 end if;
7462 end Static_Array_Aggregate;
7464 end Exp_Aggr;