hppa: Revise REG+D address support to allow long displacements before reload
[official-gcc.git] / gcc / ada / exp_ch3.adb
blobf88ac7e65426607278666f7d13ca6d94a239c09b
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ C H 3 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2023, 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 Accessibility; use Accessibility;
27 with Aspects; use Aspects;
28 with Atree; use Atree;
29 with Checks; use Checks;
30 with Contracts; use Contracts;
31 with Einfo; use Einfo;
32 with Einfo.Entities; use Einfo.Entities;
33 with Einfo.Utils; use Einfo.Utils;
34 with Errout; use Errout;
35 with Expander; use Expander;
36 with Exp_Aggr; use Exp_Aggr;
37 with Exp_Atag; use Exp_Atag;
38 with Exp_Ch4; use Exp_Ch4;
39 with Exp_Ch6; use Exp_Ch6;
40 with Exp_Ch7; use Exp_Ch7;
41 with Exp_Ch9; use Exp_Ch9;
42 with Exp_Dbug; use Exp_Dbug;
43 with Exp_Disp; use Exp_Disp;
44 with Exp_Dist; use Exp_Dist;
45 with Exp_Put_Image;
46 with Exp_Smem; use Exp_Smem;
47 with Exp_Strm; use Exp_Strm;
48 with Exp_Util; use Exp_Util;
49 with Freeze; use Freeze;
50 with Ghost; use Ghost;
51 with Lib; use Lib;
52 with Namet; use Namet;
53 with Nlists; use Nlists;
54 with Nmake; use Nmake;
55 with Opt; use Opt;
56 with Restrict; use Restrict;
57 with Rident; use Rident;
58 with Rtsfind; use Rtsfind;
59 with Sem; use Sem;
60 with Sem_Aux; use Sem_Aux;
61 with Sem_Attr; use Sem_Attr;
62 with Sem_Cat; use Sem_Cat;
63 with Sem_Ch3; use Sem_Ch3;
64 with Sem_Ch6; use Sem_Ch6;
65 with Sem_Ch8; use Sem_Ch8;
66 with Sem_Disp; use Sem_Disp;
67 with Sem_Eval; use Sem_Eval;
68 with Sem_Mech; use Sem_Mech;
69 with Sem_Res; use Sem_Res;
70 with Sem_SCIL; use Sem_SCIL;
71 with Sem_Type; use Sem_Type;
72 with Sem_Util; use Sem_Util;
73 with Sinfo; use Sinfo;
74 with Sinfo.Nodes; use Sinfo.Nodes;
75 with Sinfo.Utils; use Sinfo.Utils;
76 with Stand; use Stand;
77 with Snames; use Snames;
78 with Tbuild; use Tbuild;
79 with Ttypes; use Ttypes;
80 with Validsw; use Validsw;
82 package body Exp_Ch3 is
84 -----------------------
85 -- Local Subprograms --
86 -----------------------
88 procedure Adjust_Discriminants (Rtype : Entity_Id);
89 -- This is used when freezing a record type. It attempts to construct
90 -- more restrictive subtypes for discriminants so that the max size of
91 -- the record can be calculated more accurately. See the body of this
92 -- procedure for details.
94 procedure Build_Array_Init_Proc (A_Type : Entity_Id; Nod : Node_Id);
95 -- Build initialization procedure for given array type. Nod is a node
96 -- used for attachment of any actions required in its construction.
97 -- It also supplies the source location used for the procedure.
99 function Build_Discriminant_Formals
100 (Rec_Id : Entity_Id;
101 Use_Dl : Boolean) return List_Id;
102 -- This function uses the discriminants of a type to build a list of
103 -- formal parameters, used in Build_Init_Procedure among other places.
104 -- If the flag Use_Dl is set, the list is built using the already
105 -- defined discriminals of the type, as is the case for concurrent
106 -- types with discriminants. Otherwise new identifiers are created,
107 -- with the source names of the discriminants.
109 procedure Build_Discr_Checking_Funcs (N : Node_Id);
110 -- For each variant component, builds a function which checks whether
111 -- the component name is consistent with the current discriminants
112 -- and sets the component's Dcheck_Function attribute to refer to it.
113 -- N is the full type declaration node; the discriminant checking
114 -- functions are inserted after this node.
116 function Build_Equivalent_Array_Aggregate (T : Entity_Id) return Node_Id;
117 -- This function builds a static aggregate that can serve as the initial
118 -- value for an array type whose bounds are static, and whose component
119 -- type is a composite type that has a static equivalent aggregate.
120 -- The equivalent array aggregate is used both for object initialization
121 -- and for component initialization, when used in the following function.
123 function Build_Equivalent_Record_Aggregate (T : Entity_Id) return Node_Id;
124 -- This function builds a static aggregate that can serve as the initial
125 -- value for a record type whose components are scalar and initialized
126 -- with compile-time values, or arrays with similar initialization or
127 -- defaults. When possible, initialization of an object of the type can
128 -- be achieved by using a copy of the aggregate as an initial value, thus
129 -- removing the implicit call that would otherwise constitute elaboration
130 -- code.
132 procedure Build_Record_Init_Proc (N : Node_Id; Rec_Ent : Entity_Id);
133 -- Build record initialization procedure. N is the type declaration
134 -- node, and Rec_Ent is the corresponding entity for the record type.
136 procedure Build_Slice_Assignment (Typ : Entity_Id);
137 -- Build assignment procedure for one-dimensional arrays of controlled
138 -- types. Other array and slice assignments are expanded in-line, but
139 -- the code expansion for controlled components (when control actions
140 -- are active) can lead to very large blocks that GCC handles poorly.
142 procedure Build_Untagged_Record_Equality (Typ : Entity_Id);
143 -- AI05-0123: Equality on untagged records composes. This procedure
144 -- builds the equality routine for an untagged record that has components
145 -- of a record type that has user-defined primitive equality operations.
146 -- The resulting operation is a TSS subprogram.
148 procedure Check_Stream_Attributes (Typ : Entity_Id);
149 -- Check that if a limited extension has a parent with user-defined stream
150 -- attributes, and does not itself have user-defined stream-attributes,
151 -- then any limited component of the extension also has the corresponding
152 -- user-defined stream attributes.
154 procedure Clean_Task_Names
155 (Typ : Entity_Id;
156 Proc_Id : Entity_Id);
157 -- If an initialization procedure includes calls to generate names
158 -- for task subcomponents, indicate that secondary stack cleanup is
159 -- needed after an initialization. Typ is the component type, and Proc_Id
160 -- the initialization procedure for the enclosing composite type.
162 procedure Copy_Discr_Checking_Funcs (N : Node_Id);
163 -- For a derived untagged type, copy the attributes that were set
164 -- for the components of the parent type onto the components of the
165 -- derived type. No new subprograms are constructed.
166 -- N is the full type declaration node, as for Build_Discr_Checking_Funcs.
168 procedure Expand_Freeze_Array_Type (N : Node_Id);
169 -- Freeze an array type. Deals with building the initialization procedure,
170 -- creating the packed array type for a packed array and also with the
171 -- creation of the controlling procedures for the controlled case. The
172 -- argument N is the N_Freeze_Entity node for the type.
174 procedure Expand_Freeze_Class_Wide_Type (N : Node_Id);
175 -- Freeze a class-wide type. Build routine Finalize_Address for the purpose
176 -- of finalizing controlled derivations from the class-wide's root type.
178 procedure Expand_Freeze_Enumeration_Type (N : Node_Id);
179 -- Freeze enumeration type with non-standard representation. Builds the
180 -- array and function needed to convert between enumeration pos and
181 -- enumeration representation values. N is the N_Freeze_Entity node
182 -- for the type.
184 procedure Expand_Freeze_Record_Type (N : Node_Id);
185 -- Freeze record type. Builds all necessary discriminant checking
186 -- and other ancillary functions, and builds dispatch tables where
187 -- needed. The argument N is the N_Freeze_Entity node. This processing
188 -- applies only to E_Record_Type entities, not to class wide types,
189 -- record subtypes, or private types.
191 procedure Expand_Tagged_Root (T : Entity_Id);
192 -- Add a field _Tag at the beginning of the record. This field carries
193 -- the value of the access to the Dispatch table. This procedure is only
194 -- called on root type, the _Tag field being inherited by the descendants.
196 procedure Freeze_Stream_Operations (N : Node_Id; Typ : Entity_Id);
197 -- Treat user-defined stream operations as renaming_as_body if the
198 -- subprogram they rename is not frozen when the type is frozen.
200 package Initialization_Control is
202 function Requires_Late_Init
203 (Decl : Node_Id; Rec_Type : Entity_Id) return Boolean;
204 -- Return True iff the given component declaration requires late
205 -- initialization, as defined by 3.3.1 (8.1/5).
207 function Has_Late_Init_Component
208 (Tagged_Rec_Type : Entity_Id) return Boolean;
209 -- Return True iff the given tagged record type has at least one
210 -- component that requires late initialization; this includes
211 -- components of ancestor types.
213 type Initialization_Mode is
214 (Full_Init, Full_Init_Except_Tag, Early_Init_Only, Late_Init_Only);
215 -- The initialization routine for a tagged type is passed in a
216 -- formal parameter of this type, indicating what initialization
217 -- is to be performed. This parameter defaults to Full_Init in all
218 -- cases except when the init proc of a type extension (let's call
219 -- that type T2) calls the init proc of its parent (let's call that
220 -- type T1). In that case, one of the other 3 values will
221 -- be passed in. In all three of those cases, the Tag component has
222 -- already been initialized before the call and is therefore not to be
223 -- modified. T2's init proc will either call T1's init proc
224 -- once (with Full_Init_Except_Tag as the parameter value) or twice
225 -- (first with Early_Init_Only, then later with Late_Init_Only),
226 -- depending on the result returned by Has_Late_Init_Component (T1).
227 -- In the latter case, the first call does not initialize any
228 -- components that require late initialization and the second call
229 -- then performs that deferred initialization.
230 -- Strictly speaking, the formal parameter subtype is actually Natural
231 -- but calls will only pass in values corresponding to literals
232 -- of this enumeration type.
234 function Make_Mode_Literal
235 (Loc : Source_Ptr; Mode : Initialization_Mode) return Node_Id
236 is (Make_Integer_Literal (Loc, Initialization_Mode'Pos (Mode)));
237 -- Generate an integer literal for a given mode value.
239 function Tag_Init_Condition
240 (Loc : Source_Ptr;
241 Init_Control_Formal : Entity_Id) return Node_Id;
242 function Early_Init_Condition
243 (Loc : Source_Ptr;
244 Init_Control_Formal : Entity_Id) return Node_Id;
245 function Late_Init_Condition
246 (Loc : Source_Ptr;
247 Init_Control_Formal : Entity_Id) return Node_Id;
248 -- These three functions each return a Boolean expression that
249 -- can be used to determine whether a given call to the initialization
250 -- expression for a tagged type should initialize (respectively)
251 -- the Tag component, the non-Tag components that do not require late
252 -- initialization, and the components that do require late
253 -- initialization.
255 end Initialization_Control;
257 procedure Initialization_Warning (E : Entity_Id);
258 -- If static elaboration of the package is requested, indicate
259 -- when a type does meet the conditions for static initialization. If
260 -- E is a type, it has components that have no static initialization.
261 -- if E is an entity, its initial expression is not compile-time known.
263 function Init_Formals (Typ : Entity_Id; Proc_Id : Entity_Id) return List_Id;
264 -- This function builds the list of formals for an initialization routine.
265 -- The first formal is always _Init with the given type. For task value
266 -- record types and types containing tasks, three additional formals are
267 -- added and Proc_Id is decorated with attribute Has_Master_Entity:
269 -- _Master : Master_Id
270 -- _Chain : in out Activation_Chain
271 -- _Task_Name : String
273 -- The caller must append additional entries for discriminants if required.
275 function Inline_Init_Proc (Typ : Entity_Id) return Boolean;
276 -- Returns true if the initialization procedure of Typ should be inlined
278 function In_Runtime (E : Entity_Id) return Boolean;
279 -- Check if E is defined in the RTL (in a child of Ada or System). Used
280 -- to avoid to bring in the overhead of _Input, _Output for tagged types.
282 function Is_Null_Statement_List (Stmts : List_Id) return Boolean;
283 -- Returns true if Stmts is made of null statements only, possibly wrapped
284 -- in a case statement, recursively. This latter pattern may occur for the
285 -- initialization procedure of an unchecked union.
287 function Make_Eq_Body
288 (Typ : Entity_Id;
289 Eq_Name : Name_Id) return Node_Id;
290 -- Build the body of a primitive equality operation for a tagged record
291 -- type, or in Ada 2012 for any record type that has components with a
292 -- user-defined equality. Factored out of Predefined_Primitive_Bodies.
294 function Make_Eq_Case
295 (E : Entity_Id;
296 CL : Node_Id;
297 Discrs : Elist_Id := New_Elmt_List) return List_Id;
298 -- Building block for variant record equality. Defined to share the code
299 -- between the tagged and untagged case. Given a Component_List node CL,
300 -- it generates an 'if' followed by a 'case' statement that compares all
301 -- components of local temporaries named X and Y (that are declared as
302 -- formals at some upper level). E provides the Sloc to be used for the
303 -- generated code.
305 -- IF E is an unchecked_union, Discrs is the list of formals created for
306 -- the inferred discriminants of one operand. These formals are used in
307 -- the generated case statements for each variant of the unchecked union.
309 function Make_Eq_If
310 (E : Entity_Id;
311 L : List_Id) return Node_Id;
312 -- Building block for variant record equality. Defined to share the code
313 -- between the tagged and untagged case. Given the list of components
314 -- (or discriminants) L, it generates a return statement that compares all
315 -- components of local temporaries named X and Y (that are declared as
316 -- formals at some upper level). E provides the Sloc to be used for the
317 -- generated code.
319 function Make_Neq_Body (Tag_Typ : Entity_Id) return Node_Id;
320 -- Search for a renaming of the inequality dispatching primitive of
321 -- this tagged type. If found then build and return the corresponding
322 -- rename-as-body inequality subprogram; otherwise return Empty.
324 procedure Make_Predefined_Primitive_Specs
325 (Tag_Typ : Entity_Id;
326 Predef_List : out List_Id;
327 Renamed_Eq : out Entity_Id);
328 -- Create a list with the specs of the predefined primitive operations.
329 -- For tagged types that are interfaces all these primitives are defined
330 -- abstract.
332 -- The following entries are present for all tagged types, and provide
333 -- the results of the corresponding attribute applied to the object.
334 -- Dispatching is required in general, since the result of the attribute
335 -- will vary with the actual object subtype.
337 -- _size provides result of 'Size attribute
338 -- typSR provides result of 'Read attribute
339 -- typSW provides result of 'Write attribute
340 -- typSI provides result of 'Input attribute
341 -- typSO provides result of 'Output attribute
342 -- typPI provides result of 'Put_Image attribute
344 -- The following entries are additionally present for non-limited tagged
345 -- types, and implement additional dispatching operations for predefined
346 -- operations:
348 -- _equality implements "=" operator
349 -- _assign implements assignment operation
350 -- typDF implements deep finalization
351 -- typDA implements deep adjust
353 -- The latter two are empty procedures unless the type contains some
354 -- controlled components that require finalization actions (the deep
355 -- in the name refers to the fact that the action applies to components).
357 -- The list of specs is returned in Predef_List
359 function Has_New_Non_Standard_Rep (T : Entity_Id) return Boolean;
360 -- Returns True if there are representation clauses for type T that are not
361 -- inherited. If the result is false, the init_proc and the discriminant
362 -- checking functions of the parent can be reused by a derived type.
364 function Make_Null_Procedure_Specs (Tag_Typ : Entity_Id) return List_Id;
365 -- Ada 2005 (AI-251): Makes specs for null procedures associated with any
366 -- null procedures inherited from an interface type that have not been
367 -- overridden. Only one null procedure will be created for a given set of
368 -- inherited null procedures with homographic profiles.
370 function Predef_Spec_Or_Body
371 (Loc : Source_Ptr;
372 Tag_Typ : Entity_Id;
373 Name : Name_Id;
374 Profile : List_Id;
375 Ret_Type : Entity_Id := Empty;
376 For_Body : Boolean := False) return Node_Id;
377 -- This function generates the appropriate expansion for a predefined
378 -- primitive operation specified by its name, parameter profile and
379 -- return type (Empty means this is a procedure). If For_Body is false,
380 -- then the returned node is a subprogram declaration. If For_Body is
381 -- true, then the returned node is a empty subprogram body containing
382 -- no declarations and no statements.
384 function Predef_Stream_Attr_Spec
385 (Loc : Source_Ptr;
386 Tag_Typ : Entity_Id;
387 Name : TSS_Name_Type) return Node_Id;
388 -- Specialized version of Predef_Spec_Or_Body that apply to read, write,
389 -- input and output attribute whose specs are constructed in Exp_Strm.
391 function Predef_Deep_Spec
392 (Loc : Source_Ptr;
393 Tag_Typ : Entity_Id;
394 Name : TSS_Name_Type;
395 For_Body : Boolean := False) return Node_Id;
396 -- Specialized version of Predef_Spec_Or_Body that apply to _deep_adjust
397 -- and _deep_finalize
399 function Predefined_Primitive_Bodies
400 (Tag_Typ : Entity_Id;
401 Renamed_Eq : Entity_Id) return List_Id;
402 -- Create the bodies of the predefined primitives that are described in
403 -- Predefined_Primitive_Specs. When not empty, Renamed_Eq must denote
404 -- the defining unit name of the type's predefined equality as returned
405 -- by Make_Predefined_Primitive_Specs.
407 function Predefined_Primitive_Freeze (Tag_Typ : Entity_Id) return List_Id;
408 -- Freeze entities of all predefined primitive operations. This is needed
409 -- because the bodies of these operations do not normally do any freezing.
411 --------------------------
412 -- Adjust_Discriminants --
413 --------------------------
415 -- This procedure attempts to define subtypes for discriminants that are
416 -- more restrictive than those declared. Such a replacement is possible if
417 -- we can demonstrate that values outside the restricted range would cause
418 -- constraint errors in any case. The advantage of restricting the
419 -- discriminant types in this way is that the maximum size of the variant
420 -- record can be calculated more conservatively.
422 -- An example of a situation in which we can perform this type of
423 -- restriction is the following:
425 -- subtype B is range 1 .. 10;
426 -- type Q is array (B range <>) of Integer;
428 -- type V (N : Natural) is record
429 -- C : Q (1 .. N);
430 -- end record;
432 -- In this situation, we can restrict the upper bound of N to 10, since
433 -- any larger value would cause a constraint error in any case.
435 -- There are many situations in which such restriction is possible, but
436 -- for now, we just look for cases like the above, where the component
437 -- in question is a one dimensional array whose upper bound is one of
438 -- the record discriminants. Also the component must not be part of
439 -- any variant part, since then the component does not always exist.
441 procedure Adjust_Discriminants (Rtype : Entity_Id) is
442 Loc : constant Source_Ptr := Sloc (Rtype);
443 Comp : Entity_Id;
444 Ctyp : Entity_Id;
445 Ityp : Entity_Id;
446 Lo : Node_Id;
447 Hi : Node_Id;
448 P : Node_Id;
449 Loval : Uint;
450 Discr : Entity_Id;
451 Dtyp : Entity_Id;
452 Dhi : Node_Id;
453 Dhiv : Uint;
454 Ahi : Node_Id;
455 Ahiv : Uint;
456 Tnn : Entity_Id;
458 begin
459 Comp := First_Component (Rtype);
460 while Present (Comp) loop
462 -- If our parent is a variant, quit, we do not look at components
463 -- that are in variant parts, because they may not always exist.
465 P := Parent (Comp); -- component declaration
466 P := Parent (P); -- component list
468 exit when Nkind (Parent (P)) = N_Variant;
470 -- We are looking for a one dimensional array type
472 Ctyp := Etype (Comp);
474 if not Is_Array_Type (Ctyp) or else Number_Dimensions (Ctyp) > 1 then
475 goto Continue;
476 end if;
478 -- The lower bound must be constant, and the upper bound is a
479 -- discriminant (which is a discriminant of the current record).
481 Ityp := Etype (First_Index (Ctyp));
482 Lo := Type_Low_Bound (Ityp);
483 Hi := Type_High_Bound (Ityp);
485 if not Compile_Time_Known_Value (Lo)
486 or else Nkind (Hi) /= N_Identifier
487 or else No (Entity (Hi))
488 or else Ekind (Entity (Hi)) /= E_Discriminant
489 then
490 goto Continue;
491 end if;
493 -- We have an array with appropriate bounds
495 Loval := Expr_Value (Lo);
496 Discr := Entity (Hi);
497 Dtyp := Etype (Discr);
499 -- See if the discriminant has a known upper bound
501 Dhi := Type_High_Bound (Dtyp);
503 if not Compile_Time_Known_Value (Dhi) then
504 goto Continue;
505 end if;
507 Dhiv := Expr_Value (Dhi);
509 -- See if base type of component array has known upper bound
511 Ahi := Type_High_Bound (Etype (First_Index (Base_Type (Ctyp))));
513 if not Compile_Time_Known_Value (Ahi) then
514 goto Continue;
515 end if;
517 Ahiv := Expr_Value (Ahi);
519 -- The condition for doing the restriction is that the high bound
520 -- of the discriminant is greater than the low bound of the array,
521 -- and is also greater than the high bound of the base type index.
523 if Dhiv > Loval and then Dhiv > Ahiv then
525 -- We can reset the upper bound of the discriminant type to
526 -- whichever is larger, the low bound of the component, or
527 -- the high bound of the base type array index.
529 -- We build a subtype that is declared as
531 -- subtype Tnn is discr_type range discr_type'First .. max;
533 -- And insert this declaration into the tree. The type of the
534 -- discriminant is then reset to this more restricted subtype.
536 Tnn := Make_Temporary (Loc, 'T');
538 Insert_Action (Declaration_Node (Rtype),
539 Make_Subtype_Declaration (Loc,
540 Defining_Identifier => Tnn,
541 Subtype_Indication =>
542 Make_Subtype_Indication (Loc,
543 Subtype_Mark => New_Occurrence_Of (Dtyp, Loc),
544 Constraint =>
545 Make_Range_Constraint (Loc,
546 Range_Expression =>
547 Make_Range (Loc,
548 Low_Bound =>
549 Make_Attribute_Reference (Loc,
550 Attribute_Name => Name_First,
551 Prefix => New_Occurrence_Of (Dtyp, Loc)),
552 High_Bound =>
553 Make_Integer_Literal (Loc,
554 Intval => UI_Max (Loval, Ahiv)))))));
556 Set_Etype (Discr, Tnn);
557 end if;
559 <<Continue>>
560 Next_Component (Comp);
561 end loop;
562 end Adjust_Discriminants;
564 ------------------------------------------
565 -- Build_Access_Subprogram_Wrapper_Body --
566 ------------------------------------------
568 procedure Build_Access_Subprogram_Wrapper_Body
569 (Decl : Node_Id;
570 New_Decl : Node_Id)
572 Loc : constant Source_Ptr := Sloc (Decl);
573 Actuals : constant List_Id := New_List;
574 Type_Def : constant Node_Id := Type_Definition (Decl);
575 Type_Id : constant Entity_Id := Defining_Identifier (Decl);
576 Spec_Node : constant Node_Id :=
577 Copy_Subprogram_Spec (Specification (New_Decl));
578 -- This copy creates new identifiers for formals and subprogram.
580 Act : Node_Id;
581 Body_Node : Node_Id;
582 Call_Stmt : Node_Id;
583 Ptr : Entity_Id;
585 begin
586 -- Create List of actuals for indirect call. The last parameter of the
587 -- subprogram declaration is the access value for the indirect call.
589 Act := First (Parameter_Specifications (Spec_Node));
591 while Present (Act) loop
592 exit when Act = Last (Parameter_Specifications (Spec_Node));
593 Append_To (Actuals,
594 Make_Identifier (Loc, Chars (Defining_Identifier (Act))));
595 Next (Act);
596 end loop;
598 Ptr :=
599 Defining_Identifier
600 (Last (Parameter_Specifications (Specification (New_Decl))));
602 if Nkind (Type_Def) = N_Access_Procedure_Definition then
603 Call_Stmt := Make_Procedure_Call_Statement (Loc,
604 Name =>
605 Make_Explicit_Dereference
606 (Loc, New_Occurrence_Of (Ptr, Loc)),
607 Parameter_Associations => Actuals);
608 else
609 Call_Stmt := Make_Simple_Return_Statement (Loc,
610 Expression =>
611 Make_Function_Call (Loc,
612 Name => Make_Explicit_Dereference
613 (Loc, New_Occurrence_Of (Ptr, Loc)),
614 Parameter_Associations => Actuals));
615 end if;
617 Body_Node := Make_Subprogram_Body (Loc,
618 Specification => Spec_Node,
619 Declarations => New_List,
620 Handled_Statement_Sequence =>
621 Make_Handled_Sequence_Of_Statements (Loc,
622 Statements => New_List (Call_Stmt)));
624 -- Place body in list of freeze actions for the type.
626 Append_Freeze_Action (Type_Id, Body_Node);
627 end Build_Access_Subprogram_Wrapper_Body;
629 ---------------------------
630 -- Build_Array_Init_Proc --
631 ---------------------------
633 procedure Build_Array_Init_Proc (A_Type : Entity_Id; Nod : Node_Id) is
634 Comp_Type : constant Entity_Id := Component_Type (A_Type);
635 Comp_Simple_Init : constant Boolean :=
636 Needs_Simple_Initialization
637 (Typ => Comp_Type,
638 Consider_IS =>
639 not (Validity_Check_Copies and Is_Bit_Packed_Array (A_Type)));
640 -- True if the component needs simple initialization, based on its type,
641 -- plus the fact that we do not do simple initialization for components
642 -- of bit-packed arrays when validity checks are enabled, because the
643 -- initialization with deliberately out-of-range values would raise
644 -- Constraint_Error.
646 Body_Stmts : List_Id;
647 Has_Default_Init : Boolean;
648 Index_List : List_Id;
649 Loc : Source_Ptr;
650 Parameters : List_Id;
651 Proc_Id : Entity_Id;
653 function Init_Component return List_Id;
654 -- Create one statement to initialize one array component, designated
655 -- by a full set of indexes.
657 function Init_One_Dimension (N : Int) return List_Id;
658 -- Create loop to initialize one dimension of the array. The single
659 -- statement in the loop body initializes the inner dimensions if any,
660 -- or else the single component. Note that this procedure is called
661 -- recursively, with N being the dimension to be initialized. A call
662 -- with N greater than the number of dimensions simply generates the
663 -- component initialization, terminating the recursion.
665 --------------------
666 -- Init_Component --
667 --------------------
669 function Init_Component return List_Id is
670 Comp : Node_Id;
672 begin
673 Comp :=
674 Make_Indexed_Component (Loc,
675 Prefix => Make_Identifier (Loc, Name_uInit),
676 Expressions => Index_List);
678 if Has_Default_Aspect (A_Type) then
679 Set_Assignment_OK (Comp);
680 return New_List (
681 Make_Assignment_Statement (Loc,
682 Name => Comp,
683 Expression =>
684 Convert_To (Comp_Type,
685 Default_Aspect_Component_Value (First_Subtype (A_Type)))));
687 elsif Comp_Simple_Init then
688 Set_Assignment_OK (Comp);
689 return New_List (
690 Make_Assignment_Statement (Loc,
691 Name => Comp,
692 Expression =>
693 Get_Simple_Init_Val
694 (Typ => Comp_Type,
695 N => Nod,
696 Size => Component_Size (A_Type))));
698 else
699 Clean_Task_Names (Comp_Type, Proc_Id);
700 return
701 Build_Initialization_Call
702 (Loc => Loc,
703 Id_Ref => Comp,
704 Typ => Comp_Type,
705 In_Init_Proc => True,
706 Enclos_Type => A_Type);
707 end if;
708 end Init_Component;
710 ------------------------
711 -- Init_One_Dimension --
712 ------------------------
714 function Init_One_Dimension (N : Int) return List_Id is
715 Index : Entity_Id;
716 DIC_Call : Node_Id;
717 Result_List : List_Id;
719 function Possible_DIC_Call return Node_Id;
720 -- If the component type has Default_Initial_Conditions and a DIC
721 -- procedure that is not an empty body, then builds a call to the
722 -- DIC procedure and returns it.
724 -----------------------
725 -- Possible_DIC_Call --
726 -----------------------
728 function Possible_DIC_Call return Node_Id is
729 begin
730 -- When the component's type has a Default_Initial_Condition, then
731 -- create a call for the DIC check.
733 if Has_DIC (Comp_Type)
734 -- In GNATprove mode, the component DICs are checked by other
735 -- means. They should not be added to the record type DIC
736 -- procedure, so that the procedure can be used to check the
737 -- record type invariants or DICs if any.
739 and then not GNATprove_Mode
741 -- DIC checks for components of controlled types are done later
742 -- (see Exp_Ch7.Make_Deep_Array_Body).
744 and then not Is_Controlled (Comp_Type)
746 and then Present (DIC_Procedure (Comp_Type))
748 and then not Has_Null_Body (DIC_Procedure (Comp_Type))
749 then
750 return
751 Build_DIC_Call (Loc,
752 Make_Indexed_Component (Loc,
753 Prefix => Make_Identifier (Loc, Name_uInit),
754 Expressions => Index_List),
755 Comp_Type);
756 else
757 return Empty;
758 end if;
759 end Possible_DIC_Call;
761 -- Start of processing for Init_One_Dimension
763 begin
764 -- If the component does not need initializing, then there is nothing
765 -- to do here, so we return a null body. This occurs when generating
766 -- the dummy Init_Proc needed for Initialize_Scalars processing.
767 -- An exception is if component type has a Default_Initial_Condition,
768 -- in which case we generate a call to the type's DIC procedure.
770 if not Has_Non_Null_Base_Init_Proc (Comp_Type)
771 and then not Comp_Simple_Init
772 and then not Has_Task (Comp_Type)
773 and then not Has_Default_Aspect (A_Type)
774 and then (not Has_DIC (Comp_Type)
775 or else N > Number_Dimensions (A_Type))
776 then
777 DIC_Call := Possible_DIC_Call;
779 if Present (DIC_Call) then
780 return New_List (DIC_Call);
781 else
782 return New_List (Make_Null_Statement (Loc));
783 end if;
785 -- If all dimensions dealt with, we simply initialize the component
786 -- and append a call to component type's DIC procedure when needed.
788 elsif N > Number_Dimensions (A_Type) then
789 DIC_Call := Possible_DIC_Call;
791 if Present (DIC_Call) then
792 Result_List := Init_Component;
793 Append (DIC_Call, Result_List);
794 return Result_List;
796 else
797 return Init_Component;
798 end if;
800 -- Here we generate the required loop
802 else
803 Index :=
804 Make_Defining_Identifier (Loc, New_External_Name ('J', N));
806 Append (New_Occurrence_Of (Index, Loc), Index_List);
808 return New_List (
809 Make_Implicit_Loop_Statement (Nod,
810 Identifier => Empty,
811 Iteration_Scheme =>
812 Make_Iteration_Scheme (Loc,
813 Loop_Parameter_Specification =>
814 Make_Loop_Parameter_Specification (Loc,
815 Defining_Identifier => Index,
816 Discrete_Subtype_Definition =>
817 Make_Attribute_Reference (Loc,
818 Prefix =>
819 Make_Identifier (Loc, Name_uInit),
820 Attribute_Name => Name_Range,
821 Expressions => New_List (
822 Make_Integer_Literal (Loc, N))))),
823 Statements => Init_One_Dimension (N + 1)));
824 end if;
825 end Init_One_Dimension;
827 -- Start of processing for Build_Array_Init_Proc
829 begin
830 -- The init proc is created when analyzing the freeze node for the type,
831 -- but it properly belongs with the array type declaration. However, if
832 -- the freeze node is for a subtype of a type declared in another unit
833 -- it seems preferable to use the freeze node as the source location of
834 -- the init proc. In any case this is preferable for gcov usage, and
835 -- the Sloc is not otherwise used by the compiler.
837 if In_Open_Scopes (Scope (A_Type)) then
838 Loc := Sloc (A_Type);
839 else
840 Loc := Sloc (Nod);
841 end if;
843 -- Nothing to generate in the following cases:
845 -- 1. Initialization is suppressed for the type
846 -- 2. An initialization already exists for the base type
848 if Initialization_Suppressed (A_Type)
849 or else Present (Base_Init_Proc (A_Type))
850 then
851 return;
852 end if;
854 Index_List := New_List;
856 -- We need an initialization procedure if any of the following is true:
858 -- 1. The component type has an initialization procedure
859 -- 2. The component type needs simple initialization
860 -- 3. Tasks are present
861 -- 4. The type is marked as a public entity
862 -- 5. The array type has a Default_Component_Value aspect
863 -- 6. The array component type has a Default_Initialization_Condition
865 -- The reason for the public entity test is to deal properly with the
866 -- Initialize_Scalars pragma. This pragma can be set in the client and
867 -- not in the declaring package, this means the client will make a call
868 -- to the initialization procedure (because one of conditions 1-3 must
869 -- apply in this case), and we must generate a procedure (even if it is
870 -- null) to satisfy the call in this case.
872 -- Exception: do not build an array init_proc for a type whose root
873 -- type is Standard.String or Standard.Wide_[Wide_]String, since there
874 -- is no place to put the code, and in any case we handle initialization
875 -- of such types (in the Initialize_Scalars case, that's the only time
876 -- the issue arises) in a special manner anyway which does not need an
877 -- init_proc.
879 Has_Default_Init := Has_Non_Null_Base_Init_Proc (Comp_Type)
880 or else Comp_Simple_Init
881 or else Has_Task (Comp_Type)
882 or else Has_Default_Aspect (A_Type)
883 or else Has_DIC (Comp_Type);
885 if Has_Default_Init
886 or else (not Restriction_Active (No_Initialize_Scalars)
887 and then Is_Public (A_Type)
888 and then not Is_Standard_String_Type (A_Type))
889 then
890 Proc_Id :=
891 Make_Defining_Identifier (Loc,
892 Chars => Make_Init_Proc_Name (A_Type));
894 -- If No_Default_Initialization restriction is active, then we don't
895 -- want to build an init_proc, but we need to mark that an init_proc
896 -- would be needed if this restriction was not active (so that we can
897 -- detect attempts to call it), so set a dummy init_proc in place.
898 -- This is only done though when actual default initialization is
899 -- needed (and not done when only Is_Public is True), since otherwise
900 -- objects such as arrays of scalars could be wrongly flagged as
901 -- violating the restriction.
903 if Restriction_Active (No_Default_Initialization) then
904 if Has_Default_Init then
905 Set_Init_Proc (A_Type, Proc_Id);
906 end if;
908 return;
909 end if;
911 Body_Stmts := Init_One_Dimension (1);
912 Parameters := Init_Formals (A_Type, Proc_Id);
914 Discard_Node (
915 Make_Subprogram_Body (Loc,
916 Specification =>
917 Make_Procedure_Specification (Loc,
918 Defining_Unit_Name => Proc_Id,
919 Parameter_Specifications => Parameters),
920 Declarations => New_List,
921 Handled_Statement_Sequence =>
922 Make_Handled_Sequence_Of_Statements (Loc,
923 Statements => Body_Stmts)));
925 Mutate_Ekind (Proc_Id, E_Procedure);
926 Set_Is_Public (Proc_Id, Is_Public (A_Type));
927 Set_Is_Internal (Proc_Id);
928 Set_Has_Completion (Proc_Id);
930 if not Debug_Generated_Code then
931 Set_Debug_Info_Off (Proc_Id);
932 end if;
934 -- Set Inlined on Init_Proc if it is set on the Init_Proc of the
935 -- component type itself (see also Build_Record_Init_Proc).
937 Set_Is_Inlined (Proc_Id, Inline_Init_Proc (Comp_Type));
939 -- Associate Init_Proc with type, and determine if the procedure
940 -- is null (happens because of the Initialize_Scalars pragma case,
941 -- where we have to generate a null procedure in case it is called
942 -- by a client with Initialize_Scalars set). Such procedures have
943 -- to be generated, but do not have to be called, so we mark them
944 -- as null to suppress the call. Kill also warnings for the _Init
945 -- out parameter, which is left entirely uninitialized.
947 Set_Init_Proc (A_Type, Proc_Id);
949 if Is_Null_Statement_List (Body_Stmts) then
950 Set_Is_Null_Init_Proc (Proc_Id);
951 Set_Warnings_Off (Defining_Identifier (First (Parameters)));
953 else
954 -- Try to build a static aggregate to statically initialize
955 -- objects of the type. This can only be done for constrained
956 -- one-dimensional arrays with static bounds.
958 Set_Static_Initialization
959 (Proc_Id,
960 Build_Equivalent_Array_Aggregate (First_Subtype (A_Type)));
961 end if;
962 end if;
963 end Build_Array_Init_Proc;
965 --------------------------------
966 -- Build_Discr_Checking_Funcs --
967 --------------------------------
969 procedure Build_Discr_Checking_Funcs (N : Node_Id) is
970 Rec_Id : Entity_Id;
971 Loc : Source_Ptr;
972 Enclosing_Func_Id : Entity_Id;
973 Sequence : Nat := 1;
974 Type_Def : Node_Id;
975 V : Node_Id;
977 function Build_Case_Statement
978 (Case_Id : Entity_Id;
979 Variant : Node_Id) return Node_Id;
980 -- Build a case statement containing only two alternatives. The first
981 -- alternative corresponds to the discrete choices given on the variant
982 -- that contains the components that we are generating the checks
983 -- for. If the discriminant is one of these return False. The second
984 -- alternative is an OTHERS choice that returns True indicating the
985 -- discriminant did not match.
987 function Build_Dcheck_Function
988 (Case_Id : Entity_Id;
989 Variant : Node_Id) return Entity_Id;
990 -- Build the discriminant checking function for a given variant
992 procedure Build_Dcheck_Functions (Variant_Part_Node : Node_Id);
993 -- Builds the discriminant checking function for each variant of the
994 -- given variant part of the record type.
996 --------------------------
997 -- Build_Case_Statement --
998 --------------------------
1000 function Build_Case_Statement
1001 (Case_Id : Entity_Id;
1002 Variant : Node_Id) return Node_Id
1004 Alt_List : constant List_Id := New_List;
1005 Actuals_List : List_Id;
1006 Case_Node : Node_Id;
1007 Case_Alt_Node : Node_Id;
1008 Choice : Node_Id;
1009 Choice_List : List_Id;
1010 D : Entity_Id;
1011 Return_Node : Node_Id;
1013 begin
1014 Case_Node := New_Node (N_Case_Statement, Loc);
1015 Set_End_Span (Case_Node, Uint_0);
1017 -- Replace the discriminant which controls the variant with the name
1018 -- of the formal of the checking function.
1020 Set_Expression (Case_Node, Make_Identifier (Loc, Chars (Case_Id)));
1022 Choice := First (Discrete_Choices (Variant));
1024 if Nkind (Choice) = N_Others_Choice then
1025 Choice_List := New_Copy_List (Others_Discrete_Choices (Choice));
1026 else
1027 Choice_List := New_Copy_List (Discrete_Choices (Variant));
1028 end if;
1030 if not Is_Empty_List (Choice_List) then
1031 Case_Alt_Node := New_Node (N_Case_Statement_Alternative, Loc);
1032 Set_Discrete_Choices (Case_Alt_Node, Choice_List);
1034 -- In case this is a nested variant, we need to return the result
1035 -- of the discriminant checking function for the immediately
1036 -- enclosing variant.
1038 if Present (Enclosing_Func_Id) then
1039 Actuals_List := New_List;
1041 D := First_Discriminant (Rec_Id);
1042 while Present (D) loop
1043 Append (Make_Identifier (Loc, Chars (D)), Actuals_List);
1044 Next_Discriminant (D);
1045 end loop;
1047 Return_Node :=
1048 Make_Simple_Return_Statement (Loc,
1049 Expression =>
1050 Make_Function_Call (Loc,
1051 Name =>
1052 New_Occurrence_Of (Enclosing_Func_Id, Loc),
1053 Parameter_Associations =>
1054 Actuals_List));
1056 else
1057 Return_Node :=
1058 Make_Simple_Return_Statement (Loc,
1059 Expression =>
1060 New_Occurrence_Of (Standard_False, Loc));
1061 end if;
1063 Set_Statements (Case_Alt_Node, New_List (Return_Node));
1064 Append (Case_Alt_Node, Alt_List);
1065 end if;
1067 Case_Alt_Node := New_Node (N_Case_Statement_Alternative, Loc);
1068 Choice_List := New_List (New_Node (N_Others_Choice, Loc));
1069 Set_Discrete_Choices (Case_Alt_Node, Choice_List);
1071 Return_Node :=
1072 Make_Simple_Return_Statement (Loc,
1073 Expression =>
1074 New_Occurrence_Of (Standard_True, Loc));
1076 Set_Statements (Case_Alt_Node, New_List (Return_Node));
1077 Append (Case_Alt_Node, Alt_List);
1079 Set_Alternatives (Case_Node, Alt_List);
1080 return Case_Node;
1081 end Build_Case_Statement;
1083 ---------------------------
1084 -- Build_Dcheck_Function --
1085 ---------------------------
1087 function Build_Dcheck_Function
1088 (Case_Id : Entity_Id;
1089 Variant : Node_Id) return Entity_Id
1091 Body_Node : Node_Id;
1092 Func_Id : Entity_Id;
1093 Parameter_List : List_Id;
1094 Spec_Node : Node_Id;
1096 begin
1097 Body_Node := New_Node (N_Subprogram_Body, Loc);
1098 Sequence := Sequence + 1;
1100 Func_Id :=
1101 Make_Defining_Identifier (Loc,
1102 Chars => New_External_Name (Chars (Rec_Id), 'D', Sequence));
1103 Set_Is_Discriminant_Check_Function (Func_Id);
1105 Spec_Node := New_Node (N_Function_Specification, Loc);
1106 Set_Defining_Unit_Name (Spec_Node, Func_Id);
1108 Parameter_List := Build_Discriminant_Formals (Rec_Id, False);
1110 Set_Parameter_Specifications (Spec_Node, Parameter_List);
1111 Set_Result_Definition (Spec_Node,
1112 New_Occurrence_Of (Standard_Boolean, Loc));
1113 Set_Specification (Body_Node, Spec_Node);
1114 Set_Declarations (Body_Node, New_List);
1116 Set_Handled_Statement_Sequence (Body_Node,
1117 Make_Handled_Sequence_Of_Statements (Loc,
1118 Statements => New_List (
1119 Build_Case_Statement (Case_Id, Variant))));
1121 Mutate_Ekind (Func_Id, E_Function);
1122 Set_Mechanism (Func_Id, Default_Mechanism);
1123 Set_Is_Inlined (Func_Id, True);
1124 Set_Is_Pure (Func_Id, True);
1125 Set_Is_Public (Func_Id, Is_Public (Rec_Id));
1126 Set_Is_Internal (Func_Id, True);
1128 if not Debug_Generated_Code then
1129 Set_Debug_Info_Off (Func_Id);
1130 end if;
1132 Analyze (Body_Node);
1134 Append_Freeze_Action (Rec_Id, Body_Node);
1135 Set_Dcheck_Function (Variant, Func_Id);
1136 return Func_Id;
1137 end Build_Dcheck_Function;
1139 ----------------------------
1140 -- Build_Dcheck_Functions --
1141 ----------------------------
1143 procedure Build_Dcheck_Functions (Variant_Part_Node : Node_Id) is
1144 Component_List_Node : Node_Id;
1145 Decl : Entity_Id;
1146 Discr_Name : Entity_Id;
1147 Func_Id : Entity_Id;
1148 Variant : Node_Id;
1149 Saved_Enclosing_Func_Id : Entity_Id;
1151 begin
1152 -- Build the discriminant-checking function for each variant, and
1153 -- label all components of that variant with the function's name.
1154 -- We only Generate a discriminant-checking function when the
1155 -- variant is not empty, to prevent the creation of dead code.
1157 Discr_Name := Entity (Name (Variant_Part_Node));
1158 Variant := First_Non_Pragma (Variants (Variant_Part_Node));
1160 while Present (Variant) loop
1161 Component_List_Node := Component_List (Variant);
1163 if not Null_Present (Component_List_Node) then
1164 Func_Id := Build_Dcheck_Function (Discr_Name, Variant);
1166 Decl :=
1167 First_Non_Pragma (Component_Items (Component_List_Node));
1168 while Present (Decl) loop
1169 Set_Discriminant_Checking_Func
1170 (Defining_Identifier (Decl), Func_Id);
1171 Next_Non_Pragma (Decl);
1172 end loop;
1174 if Present (Variant_Part (Component_List_Node)) then
1175 Saved_Enclosing_Func_Id := Enclosing_Func_Id;
1176 Enclosing_Func_Id := Func_Id;
1177 Build_Dcheck_Functions (Variant_Part (Component_List_Node));
1178 Enclosing_Func_Id := Saved_Enclosing_Func_Id;
1179 end if;
1180 end if;
1182 Next_Non_Pragma (Variant);
1183 end loop;
1184 end Build_Dcheck_Functions;
1186 -- Start of processing for Build_Discr_Checking_Funcs
1188 begin
1189 -- Only build if not done already
1191 if not Discr_Check_Funcs_Built (N) then
1192 Type_Def := Type_Definition (N);
1194 if Nkind (Type_Def) = N_Record_Definition then
1195 if No (Component_List (Type_Def)) then -- null record.
1196 return;
1197 else
1198 V := Variant_Part (Component_List (Type_Def));
1199 end if;
1201 else pragma Assert (Nkind (Type_Def) = N_Derived_Type_Definition);
1202 if No (Component_List (Record_Extension_Part (Type_Def))) then
1203 return;
1204 else
1205 V := Variant_Part
1206 (Component_List (Record_Extension_Part (Type_Def)));
1207 end if;
1208 end if;
1210 Rec_Id := Defining_Identifier (N);
1212 if Present (V) and then not Is_Unchecked_Union (Rec_Id) then
1213 Loc := Sloc (N);
1214 Enclosing_Func_Id := Empty;
1215 Build_Dcheck_Functions (V);
1216 end if;
1218 Set_Discr_Check_Funcs_Built (N);
1219 end if;
1220 end Build_Discr_Checking_Funcs;
1222 ----------------------------------------
1223 -- Build_Or_Copy_Discr_Checking_Funcs --
1224 ----------------------------------------
1226 procedure Build_Or_Copy_Discr_Checking_Funcs (N : Node_Id) is
1227 Typ : constant Entity_Id := Defining_Identifier (N);
1228 begin
1229 if Is_Unchecked_Union (Typ) or else not Has_Discriminants (Typ) then
1230 null;
1231 elsif not Is_Derived_Type (Typ)
1232 or else Has_New_Non_Standard_Rep (Typ)
1233 or else Is_Tagged_Type (Typ)
1234 then
1235 Build_Discr_Checking_Funcs (N);
1236 else
1237 Copy_Discr_Checking_Funcs (N);
1238 end if;
1239 end Build_Or_Copy_Discr_Checking_Funcs;
1241 --------------------------------
1242 -- Build_Discriminant_Formals --
1243 --------------------------------
1245 function Build_Discriminant_Formals
1246 (Rec_Id : Entity_Id;
1247 Use_Dl : Boolean) return List_Id
1249 Loc : Source_Ptr := Sloc (Rec_Id);
1250 Parameter_List : constant List_Id := New_List;
1251 D : Entity_Id;
1252 Formal : Entity_Id;
1253 Formal_Type : Entity_Id;
1254 Param_Spec_Node : Node_Id;
1256 begin
1257 if Has_Discriminants (Rec_Id) then
1258 D := First_Discriminant (Rec_Id);
1259 while Present (D) loop
1260 Loc := Sloc (D);
1262 if Use_Dl then
1263 Formal := Discriminal (D);
1264 Formal_Type := Etype (Formal);
1265 else
1266 Formal := Make_Defining_Identifier (Loc, Chars (D));
1267 Formal_Type := Etype (D);
1268 end if;
1270 Param_Spec_Node :=
1271 Make_Parameter_Specification (Loc,
1272 Defining_Identifier => Formal,
1273 Parameter_Type =>
1274 New_Occurrence_Of (Formal_Type, Loc));
1275 Append (Param_Spec_Node, Parameter_List);
1276 Next_Discriminant (D);
1277 end loop;
1278 end if;
1280 return Parameter_List;
1281 end Build_Discriminant_Formals;
1283 --------------------------------------
1284 -- Build_Equivalent_Array_Aggregate --
1285 --------------------------------------
1287 function Build_Equivalent_Array_Aggregate (T : Entity_Id) return Node_Id is
1288 Loc : constant Source_Ptr := Sloc (T);
1289 Comp_Type : constant Entity_Id := Component_Type (T);
1290 Index_Type : constant Entity_Id := Etype (First_Index (T));
1291 Proc : constant Entity_Id := Base_Init_Proc (T);
1292 Lo, Hi : Node_Id;
1293 Aggr : Node_Id;
1294 Expr : Node_Id;
1296 begin
1297 if not Is_Constrained (T)
1298 or else Number_Dimensions (T) > 1
1299 or else No (Proc)
1300 then
1301 Initialization_Warning (T);
1302 return Empty;
1303 end if;
1305 Lo := Type_Low_Bound (Index_Type);
1306 Hi := Type_High_Bound (Index_Type);
1308 if not Compile_Time_Known_Value (Lo)
1309 or else not Compile_Time_Known_Value (Hi)
1310 then
1311 Initialization_Warning (T);
1312 return Empty;
1313 end if;
1315 if Is_Record_Type (Comp_Type)
1316 and then Present (Base_Init_Proc (Comp_Type))
1317 then
1318 Expr := Static_Initialization (Base_Init_Proc (Comp_Type));
1320 if No (Expr) then
1321 Initialization_Warning (T);
1322 return Empty;
1323 end if;
1325 else
1326 Initialization_Warning (T);
1327 return Empty;
1328 end if;
1330 Aggr := Make_Aggregate (Loc, No_List, New_List);
1331 Set_Etype (Aggr, T);
1332 Set_Aggregate_Bounds (Aggr,
1333 Make_Range (Loc,
1334 Low_Bound => New_Copy (Lo),
1335 High_Bound => New_Copy (Hi)));
1336 Set_Parent (Aggr, Parent (Proc));
1338 Append_To (Component_Associations (Aggr),
1339 Make_Component_Association (Loc,
1340 Choices =>
1341 New_List (
1342 Make_Range (Loc,
1343 Low_Bound => New_Copy (Lo),
1344 High_Bound => New_Copy (Hi))),
1345 Expression => Expr));
1347 if Static_Array_Aggregate (Aggr) then
1348 return Aggr;
1349 else
1350 Initialization_Warning (T);
1351 return Empty;
1352 end if;
1353 end Build_Equivalent_Array_Aggregate;
1355 ---------------------------------------
1356 -- Build_Equivalent_Record_Aggregate --
1357 ---------------------------------------
1359 function Build_Equivalent_Record_Aggregate (T : Entity_Id) return Node_Id is
1360 Agg : Node_Id;
1361 Comp : Entity_Id;
1362 Comp_Type : Entity_Id;
1364 begin
1365 if not Is_Record_Type (T)
1366 or else Has_Discriminants (T)
1367 or else Is_Limited_Type (T)
1368 or else Has_Non_Standard_Rep (T)
1369 then
1370 Initialization_Warning (T);
1371 return Empty;
1372 end if;
1374 Comp := First_Component (T);
1376 -- A null record needs no warning
1378 if No (Comp) then
1379 return Empty;
1380 end if;
1382 while Present (Comp) loop
1384 -- Array components are acceptable if initialized by a positional
1385 -- aggregate with static components.
1387 if Is_Array_Type (Etype (Comp)) then
1388 Comp_Type := Component_Type (Etype (Comp));
1390 if Nkind (Parent (Comp)) /= N_Component_Declaration
1391 or else No (Expression (Parent (Comp)))
1392 or else Nkind (Expression (Parent (Comp))) /= N_Aggregate
1393 then
1394 Initialization_Warning (T);
1395 return Empty;
1397 elsif Is_Scalar_Type (Component_Type (Etype (Comp)))
1398 and then
1399 (not Compile_Time_Known_Value (Type_Low_Bound (Comp_Type))
1400 or else
1401 not Compile_Time_Known_Value (Type_High_Bound (Comp_Type)))
1402 then
1403 Initialization_Warning (T);
1404 return Empty;
1406 elsif
1407 not Static_Array_Aggregate (Expression (Parent (Comp)))
1408 then
1409 Initialization_Warning (T);
1410 return Empty;
1412 -- We need to return empty if the type has predicates because
1413 -- this would otherwise duplicate calls to the predicate
1414 -- function. If the type hasn't been frozen before being
1415 -- referenced in the current record, the extraneous call to
1416 -- the predicate function would be inserted somewhere before
1417 -- the predicate function is elaborated, which would result in
1418 -- an invalid tree.
1420 elsif Has_Predicates (Etype (Comp)) then
1421 return Empty;
1422 end if;
1424 elsif Is_Scalar_Type (Etype (Comp)) then
1425 Comp_Type := Etype (Comp);
1427 if Nkind (Parent (Comp)) /= N_Component_Declaration
1428 or else No (Expression (Parent (Comp)))
1429 or else not Compile_Time_Known_Value (Expression (Parent (Comp)))
1430 or else not Compile_Time_Known_Value (Type_Low_Bound (Comp_Type))
1431 or else not
1432 Compile_Time_Known_Value (Type_High_Bound (Comp_Type))
1433 then
1434 Initialization_Warning (T);
1435 return Empty;
1436 end if;
1438 -- For now, other types are excluded
1440 else
1441 Initialization_Warning (T);
1442 return Empty;
1443 end if;
1445 Next_Component (Comp);
1446 end loop;
1448 -- All components have static initialization. Build positional aggregate
1449 -- from the given expressions or defaults.
1451 Agg := Make_Aggregate (Sloc (T), New_List, New_List);
1452 Set_Parent (Agg, Parent (T));
1454 Comp := First_Component (T);
1455 while Present (Comp) loop
1456 Append
1457 (New_Copy_Tree (Expression (Parent (Comp))), Expressions (Agg));
1458 Next_Component (Comp);
1459 end loop;
1461 Analyze_And_Resolve (Agg, T);
1462 return Agg;
1463 end Build_Equivalent_Record_Aggregate;
1465 ----------------------------
1466 -- Init_Proc_Level_Formal --
1467 ----------------------------
1469 function Init_Proc_Level_Formal (Proc : Entity_Id) return Entity_Id is
1470 Form : Entity_Id;
1471 begin
1472 -- Move through the formals of the initialization procedure Proc to find
1473 -- the extra accessibility level parameter associated with the object
1474 -- being initialized.
1476 Form := First_Formal (Proc);
1477 while Present (Form) loop
1478 if Chars (Form) = Name_uInit_Level then
1479 return Form;
1480 end if;
1482 Next_Formal (Form);
1483 end loop;
1485 -- No formal was found, return Empty
1487 return Empty;
1488 end Init_Proc_Level_Formal;
1490 -------------------------------
1491 -- Build_Initialization_Call --
1492 -------------------------------
1494 -- References to a discriminant inside the record type declaration can
1495 -- appear either in the subtype_indication to constrain a record or an
1496 -- array, or as part of a larger expression given for the initial value
1497 -- of a component. In both of these cases N appears in the record
1498 -- initialization procedure and needs to be replaced by the formal
1499 -- parameter of the initialization procedure which corresponds to that
1500 -- discriminant.
1502 -- In the example below, references to discriminants D1 and D2 in proc_1
1503 -- are replaced by references to formals with the same name
1504 -- (discriminals)
1506 -- A similar replacement is done for calls to any record initialization
1507 -- procedure for any components that are themselves of a record type.
1509 -- type R (D1, D2 : Integer) is record
1510 -- X : Integer := F * D1;
1511 -- Y : Integer := F * D2;
1512 -- end record;
1514 -- procedure proc_1 (Out_2 : out R; D1 : Integer; D2 : Integer) is
1515 -- begin
1516 -- Out_2.D1 := D1;
1517 -- Out_2.D2 := D2;
1518 -- Out_2.X := F * D1;
1519 -- Out_2.Y := F * D2;
1520 -- end;
1522 function Build_Initialization_Call
1523 (Loc : Source_Ptr;
1524 Id_Ref : Node_Id;
1525 Typ : Entity_Id;
1526 In_Init_Proc : Boolean := False;
1527 Enclos_Type : Entity_Id := Empty;
1528 Discr_Map : Elist_Id := New_Elmt_List;
1529 With_Default_Init : Boolean := False;
1530 Constructor_Ref : Node_Id := Empty;
1531 Init_Control_Actual : Entity_Id := Empty) return List_Id
1533 Res : constant List_Id := New_List;
1535 Full_Type : Entity_Id;
1537 procedure Check_Predicated_Discriminant
1538 (Val : Node_Id;
1539 Discr : Entity_Id);
1540 -- Discriminants whose subtypes have predicates are checked in two
1541 -- cases:
1542 -- a) When an object is default-initialized and assertions are enabled
1543 -- we check that the value of the discriminant obeys the predicate.
1545 -- b) In all cases, if the discriminant controls a variant and the
1546 -- variant has no others_choice, Constraint_Error must be raised if
1547 -- the predicate is violated, because there is no variant covered
1548 -- by the illegal discriminant value.
1550 -----------------------------------
1551 -- Check_Predicated_Discriminant --
1552 -----------------------------------
1554 procedure Check_Predicated_Discriminant
1555 (Val : Node_Id;
1556 Discr : Entity_Id)
1558 Typ : constant Entity_Id := Etype (Discr);
1560 procedure Check_Missing_Others (V : Node_Id);
1561 -- Check that a given variant and its nested variants have an others
1562 -- choice, and generate a constraint error raise when it does not.
1564 --------------------------
1565 -- Check_Missing_Others --
1566 --------------------------
1568 procedure Check_Missing_Others (V : Node_Id) is
1569 Alt : Node_Id;
1570 Choice : Node_Id;
1571 Last_Var : Node_Id;
1573 begin
1574 Last_Var := Last_Non_Pragma (Variants (V));
1575 Choice := First (Discrete_Choices (Last_Var));
1577 -- An others_choice is added during expansion for gcc use, but
1578 -- does not cover the illegality.
1580 if Entity (Name (V)) = Discr then
1581 if Present (Choice)
1582 and then (Nkind (Choice) /= N_Others_Choice
1583 or else not Comes_From_Source (Choice))
1584 then
1585 Check_Expression_Against_Static_Predicate (Val, Typ);
1587 if not Is_Static_Expression (Val) then
1588 Prepend_To (Res,
1589 Make_Raise_Constraint_Error (Loc,
1590 Condition =>
1591 Make_Op_Not (Loc,
1592 Right_Opnd => Make_Predicate_Call (Typ, Val)),
1593 Reason => CE_Invalid_Data));
1594 end if;
1595 end if;
1596 end if;
1598 -- Check whether some nested variant is ruled by the predicated
1599 -- discriminant.
1601 Alt := First (Variants (V));
1602 while Present (Alt) loop
1603 if Nkind (Alt) = N_Variant
1604 and then Present (Variant_Part (Component_List (Alt)))
1605 then
1606 Check_Missing_Others
1607 (Variant_Part (Component_List (Alt)));
1608 end if;
1610 Next (Alt);
1611 end loop;
1612 end Check_Missing_Others;
1614 -- Local variables
1616 Def : Node_Id;
1618 -- Start of processing for Check_Predicated_Discriminant
1620 begin
1621 if Ekind (Base_Type (Full_Type)) = E_Record_Type then
1622 Def := Type_Definition (Parent (Base_Type (Full_Type)));
1623 else
1624 return;
1625 end if;
1627 if Policy_In_Effect (Name_Assert) = Name_Check
1628 and then not Predicates_Ignored (Etype (Discr))
1629 then
1630 Prepend_To (Res, Make_Predicate_Check (Typ, Val));
1631 end if;
1633 -- If discriminant controls a variant, verify that predicate is
1634 -- obeyed or else an Others_Choice is present.
1636 if Nkind (Def) = N_Record_Definition
1637 and then Present (Variant_Part (Component_List (Def)))
1638 and then Policy_In_Effect (Name_Assert) = Name_Ignore
1639 then
1640 Check_Missing_Others (Variant_Part (Component_List (Def)));
1641 end if;
1642 end Check_Predicated_Discriminant;
1644 -- Local variables
1646 Arg : Node_Id;
1647 Args : List_Id;
1648 Decls : List_Id;
1649 Decl : Node_Id;
1650 Discr : Entity_Id;
1651 First_Arg : Node_Id;
1652 Full_Init_Type : Entity_Id;
1653 Init_Call : Node_Id;
1654 Init_Type : Entity_Id;
1655 Proc : Entity_Id;
1657 -- Start of processing for Build_Initialization_Call
1659 begin
1660 pragma Assert (Constructor_Ref = Empty
1661 or else Is_CPP_Constructor_Call (Constructor_Ref));
1663 if No (Constructor_Ref) then
1664 Proc := Base_Init_Proc (Typ);
1665 else
1666 Proc := Base_Init_Proc (Typ, Entity (Name (Constructor_Ref)));
1667 end if;
1669 pragma Assert (Present (Proc));
1670 Init_Type := Etype (First_Formal (Proc));
1671 Full_Init_Type := Underlying_Type (Init_Type);
1673 -- Nothing to do if the Init_Proc is null, unless Initialize_Scalars
1674 -- is active (in which case we make the call anyway, since in the
1675 -- actual compiled client it may be non null).
1677 if Is_Null_Init_Proc (Proc) and then not Init_Or_Norm_Scalars then
1678 return Empty_List;
1680 -- Nothing to do for an array of controlled components that have only
1681 -- the inherited Initialize primitive. This is a useful optimization
1682 -- for CodePeer.
1684 elsif Is_Trivial_Subprogram (Proc)
1685 and then Is_Array_Type (Full_Init_Type)
1686 then
1687 return New_List (Make_Null_Statement (Loc));
1688 end if;
1690 -- Use the [underlying] full view when dealing with a private type. This
1691 -- may require several steps depending on derivations.
1693 Full_Type := Typ;
1694 loop
1695 if Is_Private_Type (Full_Type) then
1696 if Present (Full_View (Full_Type)) then
1697 Full_Type := Full_View (Full_Type);
1699 elsif Present (Underlying_Full_View (Full_Type)) then
1700 Full_Type := Underlying_Full_View (Full_Type);
1702 -- When a private type acts as a generic actual and lacks a full
1703 -- view, use the base type.
1705 elsif Is_Generic_Actual_Type (Full_Type) then
1706 Full_Type := Base_Type (Full_Type);
1708 elsif Ekind (Full_Type) = E_Private_Subtype
1709 and then (not Has_Discriminants (Full_Type)
1710 or else No (Discriminant_Constraint (Full_Type)))
1711 then
1712 Full_Type := Etype (Full_Type);
1714 -- The loop has recovered the [underlying] full view, stop the
1715 -- traversal.
1717 else
1718 exit;
1719 end if;
1721 -- The type is not private, nothing to do
1723 else
1724 exit;
1725 end if;
1726 end loop;
1728 -- If Typ is derived, the procedure is the initialization procedure for
1729 -- the root type. Wrap the argument in an conversion to make it type
1730 -- honest. Actually it isn't quite type honest, because there can be
1731 -- conflicts of views in the private type case. That is why we set
1732 -- Conversion_OK in the conversion node.
1734 if (Is_Record_Type (Typ)
1735 or else Is_Array_Type (Typ)
1736 or else Is_Private_Type (Typ))
1737 and then Init_Type /= Base_Type (Typ)
1738 then
1739 First_Arg := OK_Convert_To (Etype (Init_Type), Id_Ref);
1740 Set_Etype (First_Arg, Init_Type);
1742 else
1743 First_Arg := Id_Ref;
1744 end if;
1746 Args := New_List (Convert_Concurrent (First_Arg, Typ));
1748 -- In the tasks case, add _Master as the value of the _Master parameter
1749 -- and _Chain as the value of the _Chain parameter. At the outer level,
1750 -- these will be variables holding the corresponding values obtained
1751 -- from GNARL. At inner levels, they will be the parameters passed down
1752 -- through the outer routines.
1754 if Has_Task (Full_Type) then
1755 if Restriction_Active (No_Task_Hierarchy) then
1756 Append_To (Args, Make_Integer_Literal (Loc, Library_Task_Level));
1757 else
1758 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
1759 end if;
1761 -- Add _Chain (not done for sequential elaboration policy, see
1762 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
1764 if Partition_Elaboration_Policy /= 'S' then
1765 Append_To (Args, Make_Identifier (Loc, Name_uChain));
1766 end if;
1768 -- Ada 2005 (AI-287): In case of default initialized components
1769 -- with tasks, we generate a null string actual parameter.
1770 -- This is just a workaround that must be improved later???
1772 if With_Default_Init then
1773 Append_To (Args,
1774 Make_String_Literal (Loc,
1775 Strval => ""));
1777 else
1778 Decls :=
1779 Build_Task_Image_Decls (Loc, Id_Ref, Enclos_Type, In_Init_Proc);
1780 Decl := Last (Decls);
1782 Append_To (Args,
1783 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
1784 Append_List (Decls, Res);
1785 end if;
1787 else
1788 Decls := No_List;
1789 Decl := Empty;
1790 end if;
1792 -- Handle the optionally generated formal *_skip_null_excluding_checks
1794 -- Look at the associated node for the object we are referencing and
1795 -- verify that we are expanding a call to an Init_Proc for an internally
1796 -- generated object declaration before passing True and skipping the
1797 -- relevant checks.
1799 if Needs_Conditional_Null_Excluding_Check (Full_Init_Type)
1800 and then Nkind (Id_Ref) in N_Has_Entity
1801 and then (Comes_From_Source (Id_Ref)
1802 or else (Present (Associated_Node (Id_Ref))
1803 and then Comes_From_Source
1804 (Associated_Node (Id_Ref))))
1805 then
1806 Append_To (Args, New_Occurrence_Of (Standard_True, Loc));
1807 end if;
1809 -- Add discriminant values if discriminants are present
1811 if Has_Discriminants (Full_Init_Type) then
1812 Discr := First_Discriminant (Full_Init_Type);
1813 while Present (Discr) loop
1815 -- If this is a discriminated concurrent type, the init_proc
1816 -- for the corresponding record is being called. Use that type
1817 -- directly to find the discriminant value, to handle properly
1818 -- intervening renamed discriminants.
1820 declare
1821 T : Entity_Id := Full_Type;
1823 begin
1824 if Is_Protected_Type (T) then
1825 T := Corresponding_Record_Type (T);
1826 end if;
1828 Arg :=
1829 Get_Discriminant_Value (
1830 Discr,
1832 Discriminant_Constraint (Full_Type));
1833 end;
1835 -- If the target has access discriminants, and is constrained by
1836 -- an access to the enclosing construct, i.e. a current instance,
1837 -- replace the reference to the type by a reference to the object.
1839 if Nkind (Arg) = N_Attribute_Reference
1840 and then Is_Access_Type (Etype (Arg))
1841 and then Is_Entity_Name (Prefix (Arg))
1842 and then Is_Type (Entity (Prefix (Arg)))
1843 then
1844 Arg :=
1845 Make_Attribute_Reference (Loc,
1846 Prefix => New_Copy (Prefix (Id_Ref)),
1847 Attribute_Name => Name_Unrestricted_Access);
1849 elsif In_Init_Proc then
1851 -- Replace any possible references to the discriminant in the
1852 -- call to the record initialization procedure with references
1853 -- to the appropriate formal parameter.
1855 if Nkind (Arg) = N_Identifier
1856 and then Ekind (Entity (Arg)) = E_Discriminant
1857 then
1858 Arg := New_Occurrence_Of (Discriminal (Entity (Arg)), Loc);
1860 -- Otherwise make a copy of the default expression. Note that
1861 -- we use the current Sloc for this, because we do not want the
1862 -- call to appear to be at the declaration point. Within the
1863 -- expression, replace discriminants with their discriminals.
1865 else
1866 Arg :=
1867 New_Copy_Tree (Arg, Map => Discr_Map, New_Sloc => Loc);
1868 end if;
1870 else
1871 if Is_Constrained (Full_Type) then
1872 Arg := Duplicate_Subexpr_No_Checks (Arg);
1873 else
1874 -- The constraints come from the discriminant default exps,
1875 -- they must be reevaluated, so we use New_Copy_Tree but we
1876 -- ensure the proper Sloc (for any embedded calls).
1877 -- In addition, if a predicate check is needed on the value
1878 -- of the discriminant, insert it ahead of the call.
1880 Arg := New_Copy_Tree (Arg, New_Sloc => Loc);
1881 end if;
1883 if Has_Predicates (Etype (Discr)) then
1884 Check_Predicated_Discriminant (Arg, Discr);
1885 end if;
1886 end if;
1888 -- Ada 2005 (AI-287): In case of default initialized components,
1889 -- if the component is constrained with a discriminant of the
1890 -- enclosing type, we need to generate the corresponding selected
1891 -- component node to access the discriminant value. In other cases
1892 -- this is not required, either because we are inside the init
1893 -- proc and we use the corresponding formal, or else because the
1894 -- component is constrained by an expression.
1896 if With_Default_Init
1897 and then Nkind (Id_Ref) = N_Selected_Component
1898 and then Nkind (Arg) = N_Identifier
1899 and then Ekind (Entity (Arg)) = E_Discriminant
1900 then
1901 Append_To (Args,
1902 Make_Selected_Component (Loc,
1903 Prefix => New_Copy_Tree (Prefix (Id_Ref)),
1904 Selector_Name => Arg));
1905 else
1906 Append_To (Args, Arg);
1907 end if;
1909 Next_Discriminant (Discr);
1910 end loop;
1911 end if;
1913 -- If this is a call to initialize the parent component of a derived
1914 -- tagged type, indicate that the tag should not be set in the parent.
1915 -- This is done via the actual parameter value for the Init_Control
1916 -- formal parameter, which is also used to deal with late initialization
1917 -- requirements.
1919 -- We pass in Full_Init_Except_Tag unless the caller tells us to do
1920 -- otherwise (by passing in a nonempty Init_Control_Actual parameter).
1922 if Is_Tagged_Type (Full_Init_Type)
1923 and then not Is_CPP_Class (Full_Init_Type)
1924 and then Nkind (Id_Ref) = N_Selected_Component
1925 and then Chars (Selector_Name (Id_Ref)) = Name_uParent
1926 then
1927 declare
1928 use Initialization_Control;
1929 begin
1930 Append_To (Args,
1931 (if Present (Init_Control_Actual)
1932 then Init_Control_Actual
1933 else Make_Mode_Literal (Loc, Full_Init_Except_Tag)));
1934 end;
1935 elsif Present (Constructor_Ref) then
1936 Append_List_To (Args,
1937 New_Copy_List (Parameter_Associations (Constructor_Ref)));
1938 end if;
1940 -- Pass the extra accessibility level parameter associated with the
1941 -- level of the object being initialized when required.
1943 if Is_Entity_Name (Id_Ref)
1944 and then Present (Init_Proc_Level_Formal (Proc))
1945 then
1946 Append_To (Args,
1947 Make_Parameter_Association (Loc,
1948 Selector_Name =>
1949 Make_Identifier (Loc, Name_uInit_Level),
1950 Explicit_Actual_Parameter =>
1951 Accessibility_Level (Id_Ref, Dynamic_Level)));
1952 end if;
1954 Append_To (Res,
1955 Make_Procedure_Call_Statement (Loc,
1956 Name => New_Occurrence_Of (Proc, Loc),
1957 Parameter_Associations => Args));
1959 if Needs_Finalization (Typ)
1960 and then Nkind (Id_Ref) = N_Selected_Component
1961 then
1962 if Chars (Selector_Name (Id_Ref)) /= Name_uParent then
1963 Init_Call :=
1964 Make_Init_Call
1965 (Obj_Ref => New_Copy_Tree (First_Arg),
1966 Typ => Typ);
1968 -- Guard against a missing [Deep_]Initialize when the type was not
1969 -- properly frozen.
1971 if Present (Init_Call) then
1972 Append_To (Res, Init_Call);
1973 end if;
1974 end if;
1975 end if;
1977 return Res;
1979 exception
1980 when RE_Not_Available =>
1981 return Empty_List;
1982 end Build_Initialization_Call;
1984 ----------------------------
1985 -- Build_Record_Init_Proc --
1986 ----------------------------
1988 procedure Build_Record_Init_Proc (N : Node_Id; Rec_Ent : Entity_Id) is
1989 Decls : constant List_Id := New_List;
1990 Discr_Map : constant Elist_Id := New_Elmt_List;
1991 Loc : constant Source_Ptr := Sloc (Rec_Ent);
1992 Counter : Nat := 0;
1993 Proc_Id : Entity_Id;
1994 Rec_Type : Entity_Id;
1996 Init_Control_Formal : Entity_Id := Empty; -- set in Build_Init_Statements
1997 Has_Late_Init_Comp : Boolean := False; -- set in Build_Init_Statements
1999 function Build_Assignment
2000 (Id : Entity_Id;
2001 Default : Node_Id) return List_Id;
2002 -- Build an assignment statement that assigns the default expression to
2003 -- its corresponding record component if defined. The left-hand side of
2004 -- the assignment is marked Assignment_OK so that initialization of
2005 -- limited private records works correctly. This routine may also build
2006 -- an adjustment call if the component is controlled.
2008 procedure Build_Discriminant_Assignments (Statement_List : List_Id);
2009 -- If the record has discriminants, add assignment statements to
2010 -- Statement_List to initialize the discriminant values from the
2011 -- arguments of the initialization procedure.
2013 function Build_Init_Statements (Comp_List : Node_Id) return List_Id;
2014 -- Build a list representing a sequence of statements which initialize
2015 -- components of the given component list. This may involve building
2016 -- case statements for the variant parts. Append any locally declared
2017 -- objects on list Decls.
2019 function Build_Init_Call_Thru (Parameters : List_Id) return List_Id;
2020 -- Given an untagged type-derivation that declares discriminants, e.g.
2022 -- type R (R1, R2 : Integer) is record ... end record;
2023 -- type D (D1 : Integer) is new R (1, D1);
2025 -- we make the _init_proc of D be
2027 -- procedure _init_proc (X : D; D1 : Integer) is
2028 -- begin
2029 -- _init_proc (R (X), 1, D1);
2030 -- end _init_proc;
2032 -- This function builds the call statement in this _init_proc.
2034 procedure Build_CPP_Init_Procedure;
2035 -- Build the tree corresponding to the procedure specification and body
2036 -- of the IC procedure that initializes the C++ part of the dispatch
2037 -- table of an Ada tagged type that is a derivation of a CPP type.
2038 -- Install it as the CPP_Init TSS.
2040 procedure Build_Init_Procedure;
2041 -- Build the tree corresponding to the procedure specification and body
2042 -- of the initialization procedure and install it as the _init TSS.
2044 procedure Build_Offset_To_Top_Functions;
2045 -- Ada 2005 (AI-251): Build the tree corresponding to the procedure spec
2046 -- and body of Offset_To_Top, a function used in conjuction with types
2047 -- having secondary dispatch tables.
2049 procedure Build_Record_Checks (S : Node_Id; Check_List : List_Id);
2050 -- Add range checks to components of discriminated records. S is a
2051 -- subtype indication of a record component. Check_List is a list
2052 -- to which the check actions are appended.
2054 function Component_Needs_Simple_Initialization
2055 (T : Entity_Id) return Boolean;
2056 -- Determine if a component needs simple initialization, given its type
2057 -- T. This routine is the same as Needs_Simple_Initialization except for
2058 -- components of type Tag and Interface_Tag. These two access types do
2059 -- not require initialization since they are explicitly initialized by
2060 -- other means.
2062 function Parent_Subtype_Renaming_Discrims return Boolean;
2063 -- Returns True for base types N that rename discriminants, else False
2065 function Requires_Init_Proc (Rec_Id : Entity_Id) return Boolean;
2066 -- Determine whether a record initialization procedure needs to be
2067 -- generated for the given record type.
2069 ----------------------
2070 -- Build_Assignment --
2071 ----------------------
2073 function Build_Assignment
2074 (Id : Entity_Id;
2075 Default : Node_Id) return List_Id
2077 Default_Loc : constant Source_Ptr := Sloc (Default);
2078 Typ : constant Entity_Id := Underlying_Type (Etype (Id));
2080 Adj_Call : Node_Id;
2081 Exp : Node_Id;
2082 Exp_Q : Node_Id;
2083 Lhs : Node_Id;
2084 Res : List_Id;
2086 begin
2087 Lhs :=
2088 Make_Selected_Component (Default_Loc,
2089 Prefix => Make_Identifier (Loc, Name_uInit),
2090 Selector_Name => New_Occurrence_Of (Id, Default_Loc));
2091 Set_Assignment_OK (Lhs);
2093 -- Take copy of Default to ensure that later copies of this component
2094 -- declaration in derived types see the original tree, not a node
2095 -- rewritten during expansion of the init_proc. If the copy contains
2096 -- itypes, the scope of the new itypes is the init_proc being built.
2098 declare
2099 Map : Elist_Id := No_Elist;
2101 begin
2102 if Has_Late_Init_Comp then
2103 -- Map the type to the _Init parameter in order to
2104 -- handle "current instance" references.
2106 Map := New_Elmt_List
2107 (Elmt1 => Rec_Type,
2108 Elmt2 => Defining_Identifier (First
2109 (Parameter_Specifications
2110 (Parent (Proc_Id)))));
2112 -- If the type has an incomplete view, a current instance
2113 -- may have an incomplete type. In that case, it must also be
2114 -- replaced by the formal of the Init_Proc.
2116 if Nkind (Parent (Rec_Type)) = N_Full_Type_Declaration
2117 and then Present (Incomplete_View (Parent (Rec_Type)))
2118 then
2119 Append_Elmt (
2120 N => Incomplete_View (Parent (Rec_Type)),
2121 To => Map);
2122 Append_Elmt (
2123 N => Defining_Identifier
2124 (First
2125 (Parameter_Specifications
2126 (Parent (Proc_Id)))),
2127 To => Map);
2128 end if;
2129 end if;
2131 Exp := New_Copy_Tree (Default, New_Scope => Proc_Id, Map => Map);
2132 end;
2134 Res := New_List (
2135 Make_Assignment_Statement (Loc,
2136 Name => Lhs,
2137 Expression => Exp));
2139 Set_No_Ctrl_Actions (First (Res));
2141 Exp_Q := Unqualify (Exp);
2143 -- Adjust the tag if tagged (because of possible view conversions).
2144 -- Suppress the tag adjustment when not Tagged_Type_Expansion because
2145 -- tags are represented implicitly in objects, and when the record is
2146 -- initialized with a raise expression.
2148 if Is_Tagged_Type (Typ)
2149 and then Tagged_Type_Expansion
2150 and then Nkind (Exp_Q) /= N_Raise_Expression
2151 then
2152 Append_To (Res,
2153 Make_Tag_Assignment_From_Type
2154 (Default_Loc,
2155 New_Copy_Tree (Lhs, New_Scope => Proc_Id),
2156 Underlying_Type (Typ)));
2157 end if;
2159 -- Adjust the component if controlled except if it is an aggregate
2160 -- that will be expanded inline.
2162 if Needs_Finalization (Typ)
2163 and then Nkind (Exp_Q) not in N_Aggregate | N_Extension_Aggregate
2164 and then not Is_Build_In_Place_Function_Call (Exp)
2165 then
2166 Adj_Call :=
2167 Make_Adjust_Call
2168 (Obj_Ref => New_Copy_Tree (Lhs),
2169 Typ => Etype (Id));
2171 -- Guard against a missing [Deep_]Adjust when the component type
2172 -- was not properly frozen.
2174 if Present (Adj_Call) then
2175 Append_To (Res, Adj_Call);
2176 end if;
2177 end if;
2179 return Res;
2181 exception
2182 when RE_Not_Available =>
2183 return Empty_List;
2184 end Build_Assignment;
2186 ------------------------------------
2187 -- Build_Discriminant_Assignments --
2188 ------------------------------------
2190 procedure Build_Discriminant_Assignments (Statement_List : List_Id) is
2191 Is_Tagged : constant Boolean := Is_Tagged_Type (Rec_Type);
2192 D : Entity_Id;
2193 D_Loc : Source_Ptr;
2195 begin
2196 if Has_Discriminants (Rec_Type)
2197 and then not Is_Unchecked_Union (Rec_Type)
2198 then
2199 D := First_Discriminant (Rec_Type);
2200 while Present (D) loop
2202 -- Don't generate the assignment for discriminants in derived
2203 -- tagged types if the discriminant is a renaming of some
2204 -- ancestor discriminant. This initialization will be done
2205 -- when initializing the _parent field of the derived record.
2207 if Is_Tagged
2208 and then Present (Corresponding_Discriminant (D))
2209 then
2210 null;
2212 else
2213 D_Loc := Sloc (D);
2214 Append_List_To (Statement_List,
2215 Build_Assignment (D,
2216 New_Occurrence_Of (Discriminal (D), D_Loc)));
2217 end if;
2219 Next_Discriminant (D);
2220 end loop;
2221 end if;
2222 end Build_Discriminant_Assignments;
2224 --------------------------
2225 -- Build_Init_Call_Thru --
2226 --------------------------
2228 function Build_Init_Call_Thru (Parameters : List_Id) return List_Id is
2229 Parent_Proc : constant Entity_Id :=
2230 Base_Init_Proc (Etype (Rec_Type));
2232 Parent_Type : constant Entity_Id :=
2233 Etype (First_Formal (Parent_Proc));
2235 Uparent_Type : constant Entity_Id :=
2236 Underlying_Type (Parent_Type);
2238 First_Discr_Param : Node_Id;
2240 Arg : Node_Id;
2241 Args : List_Id;
2242 First_Arg : Node_Id;
2243 Parent_Discr : Entity_Id;
2244 Res : List_Id;
2246 begin
2247 -- First argument (_Init) is the object to be initialized.
2248 -- ??? not sure where to get a reasonable Loc for First_Arg
2250 First_Arg :=
2251 OK_Convert_To (Parent_Type,
2252 New_Occurrence_Of
2253 (Defining_Identifier (First (Parameters)), Loc));
2255 Set_Etype (First_Arg, Parent_Type);
2257 Args := New_List (Convert_Concurrent (First_Arg, Rec_Type));
2259 -- In the tasks case,
2260 -- add _Master as the value of the _Master parameter
2261 -- add _Chain as the value of the _Chain parameter.
2262 -- add _Task_Name as the value of the _Task_Name parameter.
2263 -- At the outer level, these will be variables holding the
2264 -- corresponding values obtained from GNARL or the expander.
2266 -- At inner levels, they will be the parameters passed down through
2267 -- the outer routines.
2269 First_Discr_Param := Next (First (Parameters));
2271 if Has_Task (Rec_Type) then
2272 if Restriction_Active (No_Task_Hierarchy) then
2273 Append_To
2274 (Args, Make_Integer_Literal (Loc, Library_Task_Level));
2275 else
2276 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
2277 end if;
2279 -- Add _Chain (not done for sequential elaboration policy, see
2280 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
2282 if Partition_Elaboration_Policy /= 'S' then
2283 Append_To (Args, Make_Identifier (Loc, Name_uChain));
2284 end if;
2286 Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
2287 First_Discr_Param := Next (Next (Next (First_Discr_Param)));
2288 end if;
2290 -- Append discriminant values
2292 if Has_Discriminants (Uparent_Type) then
2293 pragma Assert (not Is_Tagged_Type (Uparent_Type));
2295 Parent_Discr := First_Discriminant (Uparent_Type);
2296 while Present (Parent_Discr) loop
2298 -- Get the initial value for this discriminant
2299 -- ??? needs to be cleaned up to use parent_Discr_Constr
2300 -- directly.
2302 declare
2303 Discr : Entity_Id :=
2304 First_Stored_Discriminant (Uparent_Type);
2306 Discr_Value : Elmt_Id :=
2307 First_Elmt (Stored_Constraint (Rec_Type));
2309 begin
2310 while Original_Record_Component (Parent_Discr) /= Discr loop
2311 Next_Stored_Discriminant (Discr);
2312 Next_Elmt (Discr_Value);
2313 end loop;
2315 Arg := Node (Discr_Value);
2316 end;
2318 -- Append it to the list
2320 if Nkind (Arg) = N_Identifier
2321 and then Ekind (Entity (Arg)) = E_Discriminant
2322 then
2323 Append_To (Args,
2324 New_Occurrence_Of (Discriminal (Entity (Arg)), Loc));
2326 -- Case of access discriminants. We replace the reference
2327 -- to the type by a reference to the actual object.
2329 -- Is above comment right??? Use of New_Copy below seems mighty
2330 -- suspicious ???
2332 else
2333 Append_To (Args, New_Copy (Arg));
2334 end if;
2336 Next_Discriminant (Parent_Discr);
2337 end loop;
2338 end if;
2340 Res :=
2341 New_List (
2342 Make_Procedure_Call_Statement (Loc,
2343 Name =>
2344 New_Occurrence_Of (Parent_Proc, Loc),
2345 Parameter_Associations => Args));
2347 return Res;
2348 end Build_Init_Call_Thru;
2350 -----------------------------------
2351 -- Build_Offset_To_Top_Functions --
2352 -----------------------------------
2354 procedure Build_Offset_To_Top_Functions is
2356 procedure Build_Offset_To_Top_Function (Iface_Comp : Entity_Id);
2357 -- Generate:
2358 -- function Fxx (O : Address) return Storage_Offset is
2359 -- type Acc is access all <Typ>;
2360 -- begin
2361 -- return Acc!(O).Iface_Comp'Position;
2362 -- end Fxx;
2364 ----------------------------------
2365 -- Build_Offset_To_Top_Function --
2366 ----------------------------------
2368 procedure Build_Offset_To_Top_Function (Iface_Comp : Entity_Id) is
2369 Body_Node : Node_Id;
2370 Func_Id : Entity_Id;
2371 Spec_Node : Node_Id;
2372 Acc_Type : Entity_Id;
2374 begin
2375 Func_Id := Make_Temporary (Loc, 'F');
2376 Set_DT_Offset_To_Top_Func (Iface_Comp, Func_Id);
2378 -- Generate
2379 -- function Fxx (O : in Rec_Typ) return Storage_Offset;
2381 Spec_Node := New_Node (N_Function_Specification, Loc);
2382 Set_Defining_Unit_Name (Spec_Node, Func_Id);
2383 Set_Parameter_Specifications (Spec_Node, New_List (
2384 Make_Parameter_Specification (Loc,
2385 Defining_Identifier =>
2386 Make_Defining_Identifier (Loc, Name_uO),
2387 In_Present => True,
2388 Parameter_Type =>
2389 New_Occurrence_Of (RTE (RE_Address), Loc))));
2390 Set_Result_Definition (Spec_Node,
2391 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
2393 -- Generate
2394 -- function Fxx (O : in Rec_Typ) return Storage_Offset is
2395 -- begin
2396 -- return -O.Iface_Comp'Position;
2397 -- end Fxx;
2399 Body_Node := New_Node (N_Subprogram_Body, Loc);
2400 Set_Specification (Body_Node, Spec_Node);
2402 Acc_Type := Make_Temporary (Loc, 'T');
2403 Set_Declarations (Body_Node, New_List (
2404 Make_Full_Type_Declaration (Loc,
2405 Defining_Identifier => Acc_Type,
2406 Type_Definition =>
2407 Make_Access_To_Object_Definition (Loc,
2408 All_Present => True,
2409 Null_Exclusion_Present => False,
2410 Constant_Present => False,
2411 Subtype_Indication =>
2412 New_Occurrence_Of (Rec_Type, Loc)))));
2414 Set_Handled_Statement_Sequence (Body_Node,
2415 Make_Handled_Sequence_Of_Statements (Loc,
2416 Statements => New_List (
2417 Make_Simple_Return_Statement (Loc,
2418 Expression =>
2419 Make_Op_Minus (Loc,
2420 Make_Attribute_Reference (Loc,
2421 Prefix =>
2422 Make_Selected_Component (Loc,
2423 Prefix =>
2424 Make_Explicit_Dereference (Loc,
2425 Unchecked_Convert_To (Acc_Type,
2426 Make_Identifier (Loc, Name_uO))),
2427 Selector_Name =>
2428 New_Occurrence_Of (Iface_Comp, Loc)),
2429 Attribute_Name => Name_Position))))));
2431 Mutate_Ekind (Func_Id, E_Function);
2432 Set_Mechanism (Func_Id, Default_Mechanism);
2433 Set_Is_Internal (Func_Id, True);
2435 if not Debug_Generated_Code then
2436 Set_Debug_Info_Off (Func_Id);
2437 end if;
2439 Analyze (Body_Node);
2441 Append_Freeze_Action (Rec_Type, Body_Node);
2442 end Build_Offset_To_Top_Function;
2444 -- Local variables
2446 Iface_Comp : Node_Id;
2447 Iface_Comp_Elmt : Elmt_Id;
2448 Ifaces_Comp_List : Elist_Id;
2450 -- Start of processing for Build_Offset_To_Top_Functions
2452 begin
2453 -- Offset_To_Top_Functions are built only for derivations of types
2454 -- with discriminants that cover interface types.
2455 -- Nothing is needed either in case of virtual targets, since
2456 -- interfaces are handled directly by the target.
2458 if not Is_Tagged_Type (Rec_Type)
2459 or else Etype (Rec_Type) = Rec_Type
2460 or else not Has_Discriminants (Etype (Rec_Type))
2461 or else not Tagged_Type_Expansion
2462 then
2463 return;
2464 end if;
2466 Collect_Interface_Components (Rec_Type, Ifaces_Comp_List);
2468 -- For each interface type with secondary dispatch table we generate
2469 -- the Offset_To_Top_Functions (required to displace the pointer in
2470 -- interface conversions)
2472 Iface_Comp_Elmt := First_Elmt (Ifaces_Comp_List);
2473 while Present (Iface_Comp_Elmt) loop
2474 Iface_Comp := Node (Iface_Comp_Elmt);
2475 pragma Assert (Is_Interface (Related_Type (Iface_Comp)));
2477 -- If the interface is a parent of Rec_Type it shares the primary
2478 -- dispatch table and hence there is no need to build the function
2480 if not Is_Ancestor (Related_Type (Iface_Comp), Rec_Type,
2481 Use_Full_View => True)
2482 then
2483 Build_Offset_To_Top_Function (Iface_Comp);
2484 end if;
2486 Next_Elmt (Iface_Comp_Elmt);
2487 end loop;
2488 end Build_Offset_To_Top_Functions;
2490 ------------------------------
2491 -- Build_CPP_Init_Procedure --
2492 ------------------------------
2494 procedure Build_CPP_Init_Procedure is
2495 Body_Node : Node_Id;
2496 Body_Stmts : List_Id;
2497 Flag_Id : Entity_Id;
2498 Handled_Stmt_Node : Node_Id;
2499 Init_Tags_List : List_Id;
2500 Proc_Id : Entity_Id;
2501 Proc_Spec_Node : Node_Id;
2503 begin
2504 -- Check cases requiring no IC routine
2506 if not Is_CPP_Class (Root_Type (Rec_Type))
2507 or else Is_CPP_Class (Rec_Type)
2508 or else CPP_Num_Prims (Rec_Type) = 0
2509 or else not Tagged_Type_Expansion
2510 or else No_Run_Time_Mode
2511 then
2512 return;
2513 end if;
2515 -- Generate:
2517 -- Flag : Boolean := False;
2519 -- procedure Typ_IC is
2520 -- begin
2521 -- if not Flag then
2522 -- Copy C++ dispatch table slots from parent
2523 -- Update C++ slots of overridden primitives
2524 -- end if;
2525 -- end;
2527 Flag_Id := Make_Temporary (Loc, 'F');
2529 Append_Freeze_Action (Rec_Type,
2530 Make_Object_Declaration (Loc,
2531 Defining_Identifier => Flag_Id,
2532 Object_Definition =>
2533 New_Occurrence_Of (Standard_Boolean, Loc),
2534 Expression =>
2535 New_Occurrence_Of (Standard_True, Loc)));
2537 Body_Stmts := New_List;
2538 Body_Node := New_Node (N_Subprogram_Body, Loc);
2540 Proc_Spec_Node := New_Node (N_Procedure_Specification, Loc);
2542 Proc_Id :=
2543 Make_Defining_Identifier (Loc,
2544 Chars => Make_TSS_Name (Rec_Type, TSS_CPP_Init_Proc));
2546 Mutate_Ekind (Proc_Id, E_Procedure);
2547 Set_Is_Internal (Proc_Id);
2549 Set_Defining_Unit_Name (Proc_Spec_Node, Proc_Id);
2551 Set_Parameter_Specifications (Proc_Spec_Node, New_List);
2552 Set_Specification (Body_Node, Proc_Spec_Node);
2553 Set_Declarations (Body_Node, New_List);
2555 Init_Tags_List := Build_Inherit_CPP_Prims (Rec_Type);
2557 Append_To (Init_Tags_List,
2558 Make_Assignment_Statement (Loc,
2559 Name =>
2560 New_Occurrence_Of (Flag_Id, Loc),
2561 Expression =>
2562 New_Occurrence_Of (Standard_False, Loc)));
2564 Append_To (Body_Stmts,
2565 Make_If_Statement (Loc,
2566 Condition => New_Occurrence_Of (Flag_Id, Loc),
2567 Then_Statements => Init_Tags_List));
2569 Handled_Stmt_Node :=
2570 New_Node (N_Handled_Sequence_Of_Statements, Loc);
2571 Set_Statements (Handled_Stmt_Node, Body_Stmts);
2572 Set_Exception_Handlers (Handled_Stmt_Node, No_List);
2573 Set_Handled_Statement_Sequence (Body_Node, Handled_Stmt_Node);
2575 if not Debug_Generated_Code then
2576 Set_Debug_Info_Off (Proc_Id);
2577 end if;
2579 -- Associate CPP_Init_Proc with type
2581 Set_Init_Proc (Rec_Type, Proc_Id);
2582 end Build_CPP_Init_Procedure;
2584 --------------------------
2585 -- Build_Init_Procedure --
2586 --------------------------
2588 procedure Build_Init_Procedure is
2589 Body_Stmts : List_Id;
2590 Body_Node : Node_Id;
2591 Handled_Stmt_Node : Node_Id;
2592 Init_Tags_List : List_Id;
2593 Parameters : List_Id;
2594 Proc_Spec_Node : Node_Id;
2595 Record_Extension_Node : Node_Id;
2597 use Initialization_Control;
2598 begin
2599 Body_Stmts := New_List;
2600 Body_Node := New_Node (N_Subprogram_Body, Loc);
2601 Mutate_Ekind (Proc_Id, E_Procedure);
2603 Proc_Spec_Node := New_Node (N_Procedure_Specification, Loc);
2604 Set_Defining_Unit_Name (Proc_Spec_Node, Proc_Id);
2606 Parameters := Init_Formals (Rec_Type, Proc_Id);
2607 Append_List_To (Parameters,
2608 Build_Discriminant_Formals (Rec_Type, True));
2610 -- For tagged types, we add a parameter to indicate what
2611 -- portion of the object's initialization is to be performed.
2612 -- This is used for two purposes:
2613 -- 1) When a type extension's initialization procedure calls
2614 -- the initialization procedure of the parent type, we do
2615 -- not want the parent to initialize the Tag component;
2616 -- it has been set already.
2617 -- 2) If an ancestor type has at least one component that requires
2618 -- late initialization, then we need to be able to initialize
2619 -- those components separately after initializing any other
2620 -- components.
2622 if Is_Tagged_Type (Rec_Type) then
2623 Init_Control_Formal := Make_Temporary (Loc, 'P');
2625 Append_To (Parameters,
2626 Make_Parameter_Specification (Loc,
2627 Defining_Identifier => Init_Control_Formal,
2628 Parameter_Type =>
2629 New_Occurrence_Of (Standard_Natural, Loc),
2630 Expression => Make_Mode_Literal (Loc, Full_Init)));
2631 end if;
2633 -- Create an extra accessibility parameter to capture the level of
2634 -- the object being initialized when its type is a limited record.
2636 if Is_Limited_Record (Rec_Type) then
2637 Append_To (Parameters,
2638 Make_Parameter_Specification (Loc,
2639 Defining_Identifier => Make_Defining_Identifier
2640 (Loc, Name_uInit_Level),
2641 Parameter_Type =>
2642 New_Occurrence_Of (Standard_Natural, Loc),
2643 Expression =>
2644 Make_Integer_Literal
2645 (Loc, Scope_Depth (Standard_Standard))));
2646 end if;
2648 Set_Parameter_Specifications (Proc_Spec_Node, Parameters);
2649 Set_Specification (Body_Node, Proc_Spec_Node);
2650 Set_Declarations (Body_Node, Decls);
2652 -- N is a Derived_Type_Definition that renames the parameters of the
2653 -- ancestor type. We initialize it by expanding our discriminants and
2654 -- call the ancestor _init_proc with a type-converted object.
2656 if Parent_Subtype_Renaming_Discrims then
2657 Append_List_To (Body_Stmts, Build_Init_Call_Thru (Parameters));
2659 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
2660 Build_Discriminant_Assignments (Body_Stmts);
2662 if not Null_Present (Type_Definition (N)) then
2663 Append_List_To (Body_Stmts,
2664 Build_Init_Statements (Component_List (Type_Definition (N))));
2665 end if;
2667 -- N is a Derived_Type_Definition with a possible non-empty
2668 -- extension. The initialization of a type extension consists in the
2669 -- initialization of the components in the extension.
2671 else
2672 Build_Discriminant_Assignments (Body_Stmts);
2674 Record_Extension_Node :=
2675 Record_Extension_Part (Type_Definition (N));
2677 if not Null_Present (Record_Extension_Node) then
2678 declare
2679 Stmts : constant List_Id :=
2680 Build_Init_Statements (
2681 Component_List (Record_Extension_Node));
2683 begin
2684 -- The parent field must be initialized first because the
2685 -- offset of the new discriminants may depend on it. This is
2686 -- not needed if the parent is an interface type because in
2687 -- such case the initialization of the _parent field was not
2688 -- generated.
2690 if not Is_Interface (Etype (Rec_Ent)) then
2691 declare
2692 Parent_IP : constant Name_Id :=
2693 Make_Init_Proc_Name (Etype (Rec_Ent));
2694 Stmt : Node_Id := First (Stmts);
2695 IP_Call : Node_Id := Empty;
2696 begin
2697 -- Look for a call to the parent IP associated with
2698 -- the record extension.
2699 -- The call will be inside not one but two
2700 -- if-statements (with the same condition). Testing
2701 -- the same Early_Init condition twice might seem
2702 -- redundant. However, as soon as we exit this loop,
2703 -- we are going to hoist the inner if-statement out
2704 -- of the outer one; the "redundant" test was built
2705 -- in anticipation of this hoisting.
2707 while Present (Stmt) loop
2708 if Nkind (Stmt) = N_If_Statement then
2709 declare
2710 Then_Stmt1 : Node_Id :=
2711 First (Then_Statements (Stmt));
2712 Then_Stmt2 : Node_Id;
2713 begin
2714 while Present (Then_Stmt1) loop
2715 if Nkind (Then_Stmt1) = N_If_Statement then
2716 Then_Stmt2 :=
2717 First (Then_Statements (Then_Stmt1));
2719 if Nkind (Then_Stmt2) =
2720 N_Procedure_Call_Statement
2721 and then Chars (Name (Then_Stmt2)) =
2722 Parent_IP
2723 then
2724 -- IP_Call is a call wrapped in an
2725 -- if statement.
2726 IP_Call := Then_Stmt1;
2727 exit;
2728 end if;
2729 end if;
2730 Next (Then_Stmt1);
2731 end loop;
2732 end;
2733 end if;
2735 Next (Stmt);
2736 end loop;
2738 -- If found then move it to the beginning of the
2739 -- statements of this IP routine
2741 if Present (IP_Call) then
2742 Remove (IP_Call);
2743 Prepend_List_To (Body_Stmts, New_List (IP_Call));
2744 end if;
2745 end;
2746 end if;
2748 Append_List_To (Body_Stmts, Stmts);
2749 end;
2750 end if;
2751 end if;
2753 -- Add here the assignment to instantiate the Tag
2755 -- The assignment corresponds to the code:
2757 -- _Init._Tag := Typ'Tag;
2759 -- Suppress the tag assignment when not Tagged_Type_Expansion because
2760 -- tags are represented implicitly in objects. It is also suppressed
2761 -- in case of CPP_Class types because in this case the tag is
2762 -- initialized in the C++ side.
2764 if Is_Tagged_Type (Rec_Type)
2765 and then Tagged_Type_Expansion
2766 and then not No_Run_Time_Mode
2767 then
2768 -- Case 1: Ada tagged types with no CPP ancestor. Set the tags of
2769 -- the actual object and invoke the IP of the parent (in this
2770 -- order). The tag must be initialized before the call to the IP
2771 -- of the parent and the assignments to other components because
2772 -- the initial value of the components may depend on the tag (eg.
2773 -- through a dispatching operation on an access to the current
2774 -- type). The tag assignment is not done when initializing the
2775 -- parent component of a type extension, because in that case the
2776 -- tag is set in the extension.
2778 if not Is_CPP_Class (Root_Type (Rec_Type)) then
2780 -- Initialize the primary tag component
2782 Init_Tags_List := New_List (
2783 Make_Tag_Assignment_From_Type
2784 (Loc, Make_Identifier (Loc, Name_uInit), Rec_Type));
2786 -- Ada 2005 (AI-251): Initialize the secondary tags components
2787 -- located at fixed positions (tags whose position depends on
2788 -- variable size components are initialized later ---see below)
2790 if Ada_Version >= Ada_2005
2791 and then not Is_Interface (Rec_Type)
2792 and then Has_Interfaces (Rec_Type)
2793 then
2794 declare
2795 Elab_Sec_DT_Stmts_List : constant List_Id := New_List;
2796 Elab_List : List_Id := New_List;
2798 begin
2799 Init_Secondary_Tags
2800 (Typ => Rec_Type,
2801 Target => Make_Identifier (Loc, Name_uInit),
2802 Init_Tags_List => Init_Tags_List,
2803 Stmts_List => Elab_Sec_DT_Stmts_List,
2804 Fixed_Comps => True,
2805 Variable_Comps => False);
2807 Elab_List := New_List (
2808 Make_If_Statement (Loc,
2809 Condition =>
2810 Tag_Init_Condition (Loc, Init_Control_Formal),
2811 Then_Statements => Init_Tags_List));
2813 if Elab_Flag_Needed (Rec_Type) then
2814 Append_To (Elab_Sec_DT_Stmts_List,
2815 Make_Assignment_Statement (Loc,
2816 Name =>
2817 New_Occurrence_Of
2818 (Access_Disp_Table_Elab_Flag (Rec_Type),
2819 Loc),
2820 Expression =>
2821 New_Occurrence_Of (Standard_False, Loc)));
2823 Append_To (Elab_List,
2824 Make_If_Statement (Loc,
2825 Condition =>
2826 New_Occurrence_Of
2827 (Access_Disp_Table_Elab_Flag (Rec_Type), Loc),
2828 Then_Statements => Elab_Sec_DT_Stmts_List));
2829 end if;
2831 Prepend_List_To (Body_Stmts, Elab_List);
2832 end;
2833 else
2834 Prepend_To (Body_Stmts,
2835 Make_If_Statement (Loc,
2836 Condition =>
2837 Tag_Init_Condition (Loc, Init_Control_Formal),
2838 Then_Statements => Init_Tags_List));
2839 end if;
2841 -- Case 2: CPP type. The imported C++ constructor takes care of
2842 -- tags initialization. No action needed here because the IP
2843 -- is built by Set_CPP_Constructors; in this case the IP is a
2844 -- wrapper that invokes the C++ constructor and copies the C++
2845 -- tags locally. Done to inherit the C++ slots in Ada derivations
2846 -- (see case 3).
2848 elsif Is_CPP_Class (Rec_Type) then
2849 pragma Assert (False);
2850 null;
2852 -- Case 3: Combined hierarchy containing C++ types and Ada tagged
2853 -- type derivations. Derivations of imported C++ classes add a
2854 -- complication, because we cannot inhibit tag setting in the
2855 -- constructor for the parent. Hence we initialize the tag after
2856 -- the call to the parent IP (that is, in reverse order compared
2857 -- with pure Ada hierarchies ---see comment on case 1).
2859 else
2860 -- Initialize the primary tag
2862 Init_Tags_List := New_List (
2863 Make_Tag_Assignment_From_Type
2864 (Loc, Make_Identifier (Loc, Name_uInit), Rec_Type));
2866 -- Ada 2005 (AI-251): Initialize the secondary tags components
2867 -- located at fixed positions (tags whose position depends on
2868 -- variable size components are initialized later ---see below)
2870 if Ada_Version >= Ada_2005
2871 and then not Is_Interface (Rec_Type)
2872 and then Has_Interfaces (Rec_Type)
2873 then
2874 Init_Secondary_Tags
2875 (Typ => Rec_Type,
2876 Target => Make_Identifier (Loc, Name_uInit),
2877 Init_Tags_List => Init_Tags_List,
2878 Stmts_List => Init_Tags_List,
2879 Fixed_Comps => True,
2880 Variable_Comps => False);
2881 end if;
2883 -- Initialize the tag component after invocation of parent IP.
2885 -- Generate:
2886 -- parent_IP(_init.parent); // Invokes the C++ constructor
2887 -- [ typIC; ] // Inherit C++ slots from parent
2888 -- init_tags
2890 declare
2891 Ins_Nod : Node_Id;
2893 begin
2894 -- Search for the call to the IP of the parent. We assume
2895 -- that the first init_proc call is for the parent.
2896 -- It is wrapped in an "if Early_Init_Condition"
2897 -- if-statement.
2899 Ins_Nod := First (Body_Stmts);
2900 while Present (Next (Ins_Nod))
2901 and then
2902 (Nkind (Ins_Nod) /= N_If_Statement
2903 or else Nkind (First (Then_Statements (Ins_Nod)))
2904 /= N_Procedure_Call_Statement
2905 or else not Is_Init_Proc
2906 (Name (First (Then_Statements
2907 (Ins_Nod)))))
2908 loop
2909 Next (Ins_Nod);
2910 end loop;
2912 -- The IC routine copies the inherited slots of the C+ part
2913 -- of the dispatch table from the parent and updates the
2914 -- overridden C++ slots.
2916 if CPP_Num_Prims (Rec_Type) > 0 then
2917 declare
2918 Init_DT : Entity_Id;
2919 New_Nod : Node_Id;
2921 begin
2922 Init_DT := CPP_Init_Proc (Rec_Type);
2923 pragma Assert (Present (Init_DT));
2925 New_Nod :=
2926 Make_Procedure_Call_Statement (Loc,
2927 New_Occurrence_Of (Init_DT, Loc));
2928 Insert_After (Ins_Nod, New_Nod);
2930 -- Update location of init tag statements
2932 Ins_Nod := New_Nod;
2933 end;
2934 end if;
2936 Insert_List_After (Ins_Nod, Init_Tags_List);
2937 end;
2938 end if;
2940 -- Ada 2005 (AI-251): Initialize the secondary tag components
2941 -- located at variable positions. We delay the generation of this
2942 -- code until here because the value of the attribute 'Position
2943 -- applied to variable size components of the parent type that
2944 -- depend on discriminants is only safely read at runtime after
2945 -- the parent components have been initialized.
2947 if Ada_Version >= Ada_2005
2948 and then not Is_Interface (Rec_Type)
2949 and then Has_Interfaces (Rec_Type)
2950 and then Has_Discriminants (Etype (Rec_Type))
2951 and then Is_Variable_Size_Record (Etype (Rec_Type))
2952 then
2953 Init_Tags_List := New_List;
2955 Init_Secondary_Tags
2956 (Typ => Rec_Type,
2957 Target => Make_Identifier (Loc, Name_uInit),
2958 Init_Tags_List => Init_Tags_List,
2959 Stmts_List => Init_Tags_List,
2960 Fixed_Comps => False,
2961 Variable_Comps => True);
2963 Append_List_To (Body_Stmts, Init_Tags_List);
2964 end if;
2965 end if;
2967 Handled_Stmt_Node := New_Node (N_Handled_Sequence_Of_Statements, Loc);
2968 Set_Statements (Handled_Stmt_Node, Body_Stmts);
2970 -- Generate:
2971 -- Deep_Finalize (_init, C1, ..., CN);
2972 -- raise;
2974 if Counter > 0
2975 and then Needs_Finalization (Rec_Type)
2976 and then not Is_Abstract_Type (Rec_Type)
2977 and then not Restriction_Active (No_Exception_Propagation)
2978 then
2979 declare
2980 DF_Call : Node_Id;
2981 DF_Id : Entity_Id;
2983 begin
2984 -- Create a local version of Deep_Finalize which has indication
2985 -- of partial initialization state.
2987 DF_Id :=
2988 Make_Defining_Identifier (Loc,
2989 Chars => New_External_Name (Name_uFinalizer));
2991 Append_To (Decls, Make_Local_Deep_Finalize (Rec_Type, DF_Id));
2993 DF_Call :=
2994 Make_Procedure_Call_Statement (Loc,
2995 Name => New_Occurrence_Of (DF_Id, Loc),
2996 Parameter_Associations => New_List (
2997 Make_Identifier (Loc, Name_uInit),
2998 New_Occurrence_Of (Standard_False, Loc)));
3000 -- Do not emit warnings related to the elaboration order when a
3001 -- controlled object is declared before the body of Finalize is
3002 -- seen.
3004 if Legacy_Elaboration_Checks then
3005 Set_No_Elaboration_Check (DF_Call);
3006 end if;
3008 Set_Exception_Handlers (Handled_Stmt_Node, New_List (
3009 Make_Exception_Handler (Loc,
3010 Exception_Choices => New_List (
3011 Make_Others_Choice (Loc)),
3012 Statements => New_List (
3013 DF_Call,
3014 Make_Raise_Statement (Loc)))));
3015 end;
3016 else
3017 Set_Exception_Handlers (Handled_Stmt_Node, No_List);
3018 end if;
3020 Set_Handled_Statement_Sequence (Body_Node, Handled_Stmt_Node);
3022 if not Debug_Generated_Code then
3023 Set_Debug_Info_Off (Proc_Id);
3024 end if;
3026 -- Associate Init_Proc with type, and determine if the procedure
3027 -- is null (happens because of the Initialize_Scalars pragma case,
3028 -- where we have to generate a null procedure in case it is called
3029 -- by a client with Initialize_Scalars set). Such procedures have
3030 -- to be generated, but do not have to be called, so we mark them
3031 -- as null to suppress the call. Kill also warnings for the _Init
3032 -- out parameter, which is left entirely uninitialized.
3034 Set_Init_Proc (Rec_Type, Proc_Id);
3036 if Is_Null_Statement_List (Body_Stmts) then
3037 Set_Is_Null_Init_Proc (Proc_Id);
3038 Set_Warnings_Off (Defining_Identifier (First (Parameters)));
3039 end if;
3040 end Build_Init_Procedure;
3042 ---------------------------
3043 -- Build_Init_Statements --
3044 ---------------------------
3046 function Build_Init_Statements (Comp_List : Node_Id) return List_Id is
3047 Checks : constant List_Id := New_List;
3048 Actions : List_Id := No_List;
3049 Counter_Id : Entity_Id := Empty;
3050 Comp_Loc : Source_Ptr;
3051 Decl : Node_Id;
3052 Id : Entity_Id;
3053 Parent_Stmts : List_Id;
3054 Parent_Id : Entity_Id := Empty;
3055 Stmts, Late_Stmts : List_Id := Empty_List;
3056 Typ : Entity_Id;
3058 procedure Increment_Counter
3059 (Loc : Source_Ptr; Late : Boolean := False);
3060 -- Generate an "increment by one" statement for the current counter
3061 -- and append it to the appropriate statement list.
3063 procedure Make_Counter (Loc : Source_Ptr);
3064 -- Create a new counter for the current component list. The routine
3065 -- creates a new defining Id, adds an object declaration and sets
3066 -- the Id generator for the next variant.
3068 -----------------------
3069 -- Increment_Counter --
3070 -----------------------
3072 procedure Increment_Counter
3073 (Loc : Source_Ptr; Late : Boolean := False) is
3074 begin
3075 -- Generate:
3076 -- Counter := Counter + 1;
3078 Append_To ((if Late then Late_Stmts else Stmts),
3079 Make_Assignment_Statement (Loc,
3080 Name => New_Occurrence_Of (Counter_Id, Loc),
3081 Expression =>
3082 Make_Op_Add (Loc,
3083 Left_Opnd => New_Occurrence_Of (Counter_Id, Loc),
3084 Right_Opnd => Make_Integer_Literal (Loc, 1))));
3085 end Increment_Counter;
3087 ------------------
3088 -- Make_Counter --
3089 ------------------
3091 procedure Make_Counter (Loc : Source_Ptr) is
3092 begin
3093 -- Increment the Id generator
3095 Counter := Counter + 1;
3097 -- Create the entity and declaration
3099 Counter_Id :=
3100 Make_Defining_Identifier (Loc,
3101 Chars => New_External_Name ('C', Counter));
3103 -- Generate:
3104 -- Cnn : Integer := 0;
3106 Append_To (Decls,
3107 Make_Object_Declaration (Loc,
3108 Defining_Identifier => Counter_Id,
3109 Object_Definition =>
3110 New_Occurrence_Of (Standard_Integer, Loc),
3111 Expression =>
3112 Make_Integer_Literal (Loc, 0)));
3113 end Make_Counter;
3115 -- Start of processing for Build_Init_Statements
3117 begin
3118 if Null_Present (Comp_List) then
3119 return New_List (Make_Null_Statement (Loc));
3120 end if;
3122 Parent_Stmts := New_List;
3123 Stmts := New_List;
3125 -- Loop through visible declarations of task types and protected
3126 -- types moving any expanded code from the spec to the body of the
3127 -- init procedure.
3129 if Is_Concurrent_Record_Type (Rec_Type) then
3130 declare
3131 Decl : constant Node_Id :=
3132 Parent (Corresponding_Concurrent_Type (Rec_Type));
3133 Def : Node_Id;
3134 N1 : Node_Id;
3135 N2 : Node_Id;
3137 begin
3138 if Is_Task_Record_Type (Rec_Type) then
3139 Def := Task_Definition (Decl);
3140 else
3141 Def := Protected_Definition (Decl);
3142 end if;
3144 if Present (Def) then
3145 N1 := First (Visible_Declarations (Def));
3146 while Present (N1) loop
3147 N2 := N1;
3148 N1 := Next (N1);
3150 if Nkind (N2) in N_Statement_Other_Than_Procedure_Call
3151 or else Nkind (N2) in N_Raise_xxx_Error
3152 or else Nkind (N2) = N_Procedure_Call_Statement
3153 then
3154 Append_To (Stmts,
3155 New_Copy_Tree (N2, New_Scope => Proc_Id));
3156 Rewrite (N2, Make_Null_Statement (Sloc (N2)));
3157 Analyze (N2);
3158 end if;
3159 end loop;
3160 end if;
3161 end;
3162 end if;
3164 -- Loop through components, skipping pragmas, in 2 steps. The first
3165 -- step deals with regular components. The second step deals with
3166 -- components that require late initialization.
3168 -- First pass : regular components
3170 Decl := First_Non_Pragma (Component_Items (Comp_List));
3171 while Present (Decl) loop
3172 Comp_Loc := Sloc (Decl);
3173 Build_Record_Checks
3174 (Subtype_Indication (Component_Definition (Decl)), Checks);
3176 Id := Defining_Identifier (Decl);
3177 Typ := Etype (Id);
3179 -- Leave any processing of component requiring late initialization
3180 -- for the second pass.
3182 if Initialization_Control.Requires_Late_Init (Decl, Rec_Type) then
3183 if not Has_Late_Init_Comp then
3184 Late_Stmts := New_List;
3185 end if;
3186 Has_Late_Init_Comp := True;
3188 -- Regular component cases
3190 else
3191 -- In the context of the init proc, references to discriminants
3192 -- resolve to denote the discriminals: this is where we can
3193 -- freeze discriminant dependent component subtypes.
3195 if not Is_Frozen (Typ) then
3196 Append_List_To (Stmts, Freeze_Entity (Typ, N));
3197 end if;
3199 -- Explicit initialization
3201 if Present (Expression (Decl)) then
3202 if Is_CPP_Constructor_Call (Expression (Decl)) then
3203 Actions :=
3204 Build_Initialization_Call
3205 (Comp_Loc,
3206 Id_Ref =>
3207 Make_Selected_Component (Comp_Loc,
3208 Prefix =>
3209 Make_Identifier (Comp_Loc, Name_uInit),
3210 Selector_Name =>
3211 New_Occurrence_Of (Id, Comp_Loc)),
3212 Typ => Typ,
3213 In_Init_Proc => True,
3214 Enclos_Type => Rec_Type,
3215 Discr_Map => Discr_Map,
3216 Constructor_Ref => Expression (Decl));
3217 else
3218 Actions := Build_Assignment (Id, Expression (Decl));
3219 end if;
3221 -- CPU, Dispatching_Domain, Priority, and Secondary_Stack_Size
3222 -- components are filled in with the corresponding rep-item
3223 -- expression of the concurrent type (if any).
3225 elsif Ekind (Scope (Id)) = E_Record_Type
3226 and then Present (Corresponding_Concurrent_Type (Scope (Id)))
3227 and then Chars (Id) in Name_uCPU
3228 | Name_uDispatching_Domain
3229 | Name_uPriority
3230 | Name_uSecondary_Stack_Size
3231 then
3232 declare
3233 Exp : Node_Id;
3234 Nam : Name_Id;
3235 pragma Warnings (Off, Nam);
3236 Ritem : Node_Id;
3238 begin
3239 if Chars (Id) = Name_uCPU then
3240 Nam := Name_CPU;
3242 elsif Chars (Id) = Name_uDispatching_Domain then
3243 Nam := Name_Dispatching_Domain;
3245 elsif Chars (Id) = Name_uPriority then
3246 Nam := Name_Priority;
3248 elsif Chars (Id) = Name_uSecondary_Stack_Size then
3249 Nam := Name_Secondary_Stack_Size;
3250 end if;
3252 -- Get the Rep Item (aspect specification, attribute
3253 -- definition clause or pragma) of the corresponding
3254 -- concurrent type.
3256 Ritem :=
3257 Get_Rep_Item
3258 (Corresponding_Concurrent_Type (Scope (Id)),
3259 Nam,
3260 Check_Parents => False);
3262 if Present (Ritem) then
3264 -- Pragma case
3266 if Nkind (Ritem) = N_Pragma then
3267 Exp :=
3268 Get_Pragma_Arg
3269 (First (Pragma_Argument_Associations (Ritem)));
3271 -- Conversion for Priority expression
3273 if Nam = Name_Priority then
3274 if Pragma_Name (Ritem) = Name_Priority
3275 and then not GNAT_Mode
3276 then
3277 Exp := Convert_To (RTE (RE_Priority), Exp);
3278 else
3279 Exp :=
3280 Convert_To (RTE (RE_Any_Priority), Exp);
3281 end if;
3282 end if;
3284 -- Aspect/Attribute definition clause case
3286 else
3287 Exp := Expression (Ritem);
3289 -- Conversion for Priority expression
3291 if Nam = Name_Priority then
3292 if Chars (Ritem) = Name_Priority
3293 and then not GNAT_Mode
3294 then
3295 Exp := Convert_To (RTE (RE_Priority), Exp);
3296 else
3297 Exp :=
3298 Convert_To (RTE (RE_Any_Priority), Exp);
3299 end if;
3300 end if;
3301 end if;
3303 -- Conversion for Dispatching_Domain value
3305 if Nam = Name_Dispatching_Domain then
3306 Exp :=
3307 Unchecked_Convert_To
3308 (RTE (RE_Dispatching_Domain_Access), Exp);
3310 -- Conversion for Secondary_Stack_Size value
3312 elsif Nam = Name_Secondary_Stack_Size then
3313 Exp := Convert_To (RTE (RE_Size_Type), Exp);
3314 end if;
3316 Actions := Build_Assignment (Id, Exp);
3318 -- Nothing needed if no Rep Item
3320 else
3321 Actions := No_List;
3322 end if;
3323 end;
3325 -- Composite component with its own Init_Proc
3327 elsif not Is_Interface (Typ)
3328 and then Has_Non_Null_Base_Init_Proc (Typ)
3329 then
3330 declare
3331 use Initialization_Control;
3332 Init_Control_Actual : Node_Id := Empty;
3333 Is_Parent : constant Boolean := Chars (Id) = Name_uParent;
3334 Init_Call_Stmts : List_Id;
3335 begin
3336 if Is_Parent and then Has_Late_Init_Component (Etype (Id))
3337 then
3338 Init_Control_Actual :=
3339 Make_Mode_Literal (Comp_Loc, Early_Init_Only);
3340 -- Parent_Id used later in second call to parent's
3341 -- init proc to initialize late-init components.
3342 Parent_Id := Id;
3343 end if;
3345 Init_Call_Stmts :=
3346 Build_Initialization_Call
3347 (Comp_Loc,
3348 Make_Selected_Component (Comp_Loc,
3349 Prefix =>
3350 Make_Identifier (Comp_Loc, Name_uInit),
3351 Selector_Name => New_Occurrence_Of (Id, Comp_Loc)),
3352 Typ,
3353 In_Init_Proc => True,
3354 Enclos_Type => Rec_Type,
3355 Discr_Map => Discr_Map,
3356 Init_Control_Actual => Init_Control_Actual);
3358 if Is_Parent then
3359 -- This is tricky. At first it looks like
3360 -- we are going to end up with nested
3361 -- if-statements with the same condition:
3362 -- if Early_Init_Condition then
3363 -- if Early_Init_Condition then
3364 -- Parent_TypeIP (...);
3365 -- end if;
3366 -- end if;
3367 -- But later we will hoist the inner if-statement
3368 -- out of the outer one; we do this because the
3369 -- init-proc call for the _Parent component of a type
3370 -- extension has to precede any other initialization.
3371 Actions :=
3372 New_List (Make_If_Statement (Loc,
3373 Condition =>
3374 Early_Init_Condition (Loc, Init_Control_Formal),
3375 Then_Statements => Init_Call_Stmts));
3376 else
3377 Actions := Init_Call_Stmts;
3378 end if;
3379 end;
3381 Clean_Task_Names (Typ, Proc_Id);
3383 -- Simple initialization. If the Esize is not yet set, we pass
3384 -- Uint_0 as expected by Get_Simple_Init_Val.
3386 elsif Component_Needs_Simple_Initialization (Typ) then
3387 Actions :=
3388 Build_Assignment
3389 (Id => Id,
3390 Default =>
3391 Get_Simple_Init_Val
3392 (Typ => Typ,
3393 N => N,
3394 Size =>
3395 (if Known_Esize (Id) then Esize (Id)
3396 else Uint_0)));
3398 -- Nothing needed for this case
3400 else
3401 Actions := No_List;
3402 end if;
3404 -- When the component's type has a Default_Initial_Condition,
3405 -- and the component is default initialized, then check the
3406 -- DIC here.
3408 if Has_DIC (Typ)
3409 and then No (Expression (Decl))
3410 and then Present (DIC_Procedure (Typ))
3411 and then not Has_Null_Body (DIC_Procedure (Typ))
3413 -- The DICs of ancestors are checked as part of the type's
3414 -- DIC procedure.
3416 and then Chars (Id) /= Name_uParent
3418 -- In GNATprove mode, the component DICs are checked by other
3419 -- means. They should not be added to the record type DIC
3420 -- procedure, so that the procedure can be used to check the
3421 -- record type invariants or DICs if any.
3423 and then not GNATprove_Mode
3424 then
3425 Append_New_To (Actions,
3426 Build_DIC_Call
3427 (Comp_Loc,
3428 Make_Selected_Component (Comp_Loc,
3429 Prefix =>
3430 Make_Identifier (Comp_Loc, Name_uInit),
3431 Selector_Name =>
3432 New_Occurrence_Of (Id, Comp_Loc)),
3433 Typ));
3434 end if;
3436 if Present (Checks) then
3437 if Chars (Id) = Name_uParent then
3438 Append_List_To (Parent_Stmts, Checks);
3439 else
3440 Append_List_To (Stmts, Checks);
3441 end if;
3442 end if;
3444 if Present (Actions) then
3445 if Chars (Id) = Name_uParent then
3446 Append_List_To (Parent_Stmts, Actions);
3447 else
3448 Append_List_To (Stmts, Actions);
3450 -- Preserve initialization state in the current counter
3452 if Needs_Finalization (Typ) then
3453 if No (Counter_Id) then
3454 Make_Counter (Comp_Loc);
3455 end if;
3457 Increment_Counter (Comp_Loc);
3458 end if;
3459 end if;
3460 end if;
3461 end if;
3463 Next_Non_Pragma (Decl);
3464 end loop;
3466 -- The parent field must be initialized first because variable
3467 -- size components of the parent affect the location of all the
3468 -- new components.
3470 Prepend_List_To (Stmts, Parent_Stmts);
3472 -- Set up tasks and protected object support. This needs to be done
3473 -- before any component with a per-object access discriminant
3474 -- constraint, or any variant part (which may contain such
3475 -- components) is initialized, because the initialization of these
3476 -- components may reference the enclosing concurrent object.
3478 -- For a task record type, add the task create call and calls to bind
3479 -- any interrupt (signal) entries.
3481 if Is_Task_Record_Type (Rec_Type) then
3483 -- In the case of the restricted run time the ATCB has already
3484 -- been preallocated.
3486 if Restricted_Profile then
3487 Append_To (Stmts,
3488 Make_Assignment_Statement (Loc,
3489 Name =>
3490 Make_Selected_Component (Loc,
3491 Prefix => Make_Identifier (Loc, Name_uInit),
3492 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)),
3493 Expression =>
3494 Make_Attribute_Reference (Loc,
3495 Prefix =>
3496 Make_Selected_Component (Loc,
3497 Prefix => Make_Identifier (Loc, Name_uInit),
3498 Selector_Name => Make_Identifier (Loc, Name_uATCB)),
3499 Attribute_Name => Name_Unchecked_Access)));
3500 end if;
3502 Append_To (Stmts, Make_Task_Create_Call (Rec_Type));
3504 declare
3505 Task_Type : constant Entity_Id :=
3506 Corresponding_Concurrent_Type (Rec_Type);
3507 Task_Decl : constant Node_Id := Parent (Task_Type);
3508 Task_Def : constant Node_Id := Task_Definition (Task_Decl);
3509 Decl_Loc : Source_Ptr;
3510 Ent : Entity_Id;
3511 Vis_Decl : Node_Id;
3513 begin
3514 if Present (Task_Def) then
3515 Vis_Decl := First (Visible_Declarations (Task_Def));
3516 while Present (Vis_Decl) loop
3517 Decl_Loc := Sloc (Vis_Decl);
3519 if Nkind (Vis_Decl) = N_Attribute_Definition_Clause then
3520 if Get_Attribute_Id (Chars (Vis_Decl)) =
3521 Attribute_Address
3522 then
3523 Ent := Entity (Name (Vis_Decl));
3525 if Ekind (Ent) = E_Entry then
3526 Append_To (Stmts,
3527 Make_Procedure_Call_Statement (Decl_Loc,
3528 Name =>
3529 New_Occurrence_Of (RTE (
3530 RE_Bind_Interrupt_To_Entry), Decl_Loc),
3531 Parameter_Associations => New_List (
3532 Make_Selected_Component (Decl_Loc,
3533 Prefix =>
3534 Make_Identifier (Decl_Loc, Name_uInit),
3535 Selector_Name =>
3536 Make_Identifier
3537 (Decl_Loc, Name_uTask_Id)),
3538 Entry_Index_Expression
3539 (Decl_Loc, Ent, Empty, Task_Type),
3540 Expression (Vis_Decl))));
3541 end if;
3542 end if;
3543 end if;
3545 Next (Vis_Decl);
3546 end loop;
3547 end if;
3548 end;
3550 -- For a protected type, add statements generated by
3551 -- Make_Initialize_Protection.
3553 elsif Is_Protected_Record_Type (Rec_Type) then
3554 Append_List_To (Stmts,
3555 Make_Initialize_Protection (Rec_Type));
3556 end if;
3558 -- Second pass: components that require late initialization
3560 if Present (Parent_Id) then
3561 declare
3562 Parent_Loc : constant Source_Ptr := Sloc (Parent (Parent_Id));
3563 use Initialization_Control;
3564 begin
3565 -- We are building the init proc for a type extension.
3566 -- Call the parent type's init proc a second time, this
3567 -- time to initialize the parent's components that require
3568 -- late initialization.
3570 Append_List_To (Late_Stmts,
3571 Build_Initialization_Call
3572 (Loc => Parent_Loc,
3573 Id_Ref =>
3574 Make_Selected_Component (Parent_Loc,
3575 Prefix => Make_Identifier
3576 (Parent_Loc, Name_uInit),
3577 Selector_Name => New_Occurrence_Of (Parent_Id,
3578 Parent_Loc)),
3579 Typ => Etype (Parent_Id),
3580 In_Init_Proc => True,
3581 Enclos_Type => Rec_Type,
3582 Discr_Map => Discr_Map,
3583 Init_Control_Actual => Make_Mode_Literal
3584 (Parent_Loc, Late_Init_Only)));
3585 end;
3586 end if;
3588 if Has_Late_Init_Comp then
3589 Decl := First_Non_Pragma (Component_Items (Comp_List));
3590 while Present (Decl) loop
3591 Comp_Loc := Sloc (Decl);
3592 Id := Defining_Identifier (Decl);
3593 Typ := Etype (Id);
3595 if Initialization_Control.Requires_Late_Init (Decl, Rec_Type)
3596 then
3597 if Present (Expression (Decl)) then
3598 Append_List_To (Late_Stmts,
3599 Build_Assignment (Id, Expression (Decl)));
3601 elsif Has_Non_Null_Base_Init_Proc (Typ) then
3602 Append_List_To (Late_Stmts,
3603 Build_Initialization_Call (Comp_Loc,
3604 Make_Selected_Component (Comp_Loc,
3605 Prefix =>
3606 Make_Identifier (Comp_Loc, Name_uInit),
3607 Selector_Name => New_Occurrence_Of (Id, Comp_Loc)),
3608 Typ,
3609 In_Init_Proc => True,
3610 Enclos_Type => Rec_Type,
3611 Discr_Map => Discr_Map));
3613 Clean_Task_Names (Typ, Proc_Id);
3615 -- Preserve initialization state in the current counter
3617 if Needs_Finalization (Typ) then
3618 if No (Counter_Id) then
3619 Make_Counter (Comp_Loc);
3620 end if;
3622 Increment_Counter (Comp_Loc, Late => True);
3623 end if;
3624 elsif Component_Needs_Simple_Initialization (Typ) then
3625 Append_List_To (Late_Stmts,
3626 Build_Assignment
3627 (Id => Id,
3628 Default =>
3629 Get_Simple_Init_Val
3630 (Typ => Typ,
3631 N => N,
3632 Size => Esize (Id))));
3633 end if;
3634 end if;
3636 Next_Non_Pragma (Decl);
3637 end loop;
3638 end if;
3640 -- Process the variant part (incorrectly ignoring late
3641 -- initialization requirements for components therein).
3643 if Present (Variant_Part (Comp_List)) then
3644 declare
3645 Variant_Alts : constant List_Id := New_List;
3646 Var_Loc : Source_Ptr := No_Location;
3647 Variant : Node_Id;
3649 begin
3650 Variant :=
3651 First_Non_Pragma (Variants (Variant_Part (Comp_List)));
3652 while Present (Variant) loop
3653 Var_Loc := Sloc (Variant);
3654 Append_To (Variant_Alts,
3655 Make_Case_Statement_Alternative (Var_Loc,
3656 Discrete_Choices =>
3657 New_Copy_List (Discrete_Choices (Variant)),
3658 Statements =>
3659 Build_Init_Statements (Component_List (Variant))));
3660 Next_Non_Pragma (Variant);
3661 end loop;
3663 -- The expression of the case statement which is a reference
3664 -- to one of the discriminants is replaced by the appropriate
3665 -- formal parameter of the initialization procedure.
3667 Append_To (Stmts,
3668 Make_Case_Statement (Var_Loc,
3669 Expression =>
3670 New_Occurrence_Of (Discriminal (
3671 Entity (Name (Variant_Part (Comp_List)))), Var_Loc),
3672 Alternatives => Variant_Alts));
3673 end;
3674 end if;
3676 if No (Init_Control_Formal) then
3677 Append_List_To (Stmts, Late_Stmts);
3679 -- If no initializations were generated for component declarations
3680 -- and included in Stmts, then append a null statement to Stmts
3681 -- to make it a valid Ada tree.
3683 if Is_Empty_List (Stmts) then
3684 Append (Make_Null_Statement (Loc), Stmts);
3685 end if;
3687 return Stmts;
3688 else
3689 declare
3690 use Initialization_Control;
3692 If_Early : constant Node_Id :=
3693 (if Is_Empty_List (Stmts) then
3694 Make_Null_Statement (Loc)
3695 else
3696 Make_If_Statement (Loc,
3697 Condition =>
3698 Early_Init_Condition (Loc, Init_Control_Formal),
3699 Then_Statements => Stmts));
3700 If_Late : constant Node_Id :=
3701 (if Is_Empty_List (Late_Stmts) then
3702 Make_Null_Statement (Loc)
3703 else
3704 Make_If_Statement (Loc,
3705 Condition =>
3706 Late_Init_Condition (Loc, Init_Control_Formal),
3707 Then_Statements => Late_Stmts));
3708 begin
3709 return New_List (If_Early, If_Late);
3710 end;
3711 end if;
3712 exception
3713 when RE_Not_Available =>
3714 return Empty_List;
3715 end Build_Init_Statements;
3717 -------------------------
3718 -- Build_Record_Checks --
3719 -------------------------
3721 procedure Build_Record_Checks (S : Node_Id; Check_List : List_Id) is
3722 Subtype_Mark_Id : Entity_Id;
3724 procedure Constrain_Array
3725 (SI : Node_Id;
3726 Check_List : List_Id);
3727 -- Apply a list of index constraints to an unconstrained array type.
3728 -- The first parameter is the entity for the resulting subtype.
3729 -- Check_List is a list to which the check actions are appended.
3731 ---------------------
3732 -- Constrain_Array --
3733 ---------------------
3735 procedure Constrain_Array
3736 (SI : Node_Id;
3737 Check_List : List_Id)
3739 C : constant Node_Id := Constraint (SI);
3740 Number_Of_Constraints : Nat := 0;
3741 Index : Node_Id;
3742 S, T : Entity_Id;
3744 procedure Constrain_Index
3745 (Index : Node_Id;
3746 S : Node_Id;
3747 Check_List : List_Id);
3748 -- Process an index constraint in a constrained array declaration.
3749 -- The constraint can be either a subtype name or a range with or
3750 -- without an explicit subtype mark. Index is the corresponding
3751 -- index of the unconstrained array. S is the range expression.
3752 -- Check_List is a list to which the check actions are appended.
3754 ---------------------
3755 -- Constrain_Index --
3756 ---------------------
3758 procedure Constrain_Index
3759 (Index : Node_Id;
3760 S : Node_Id;
3761 Check_List : List_Id)
3763 T : constant Entity_Id := Etype (Index);
3765 begin
3766 if Nkind (S) = N_Range then
3767 Process_Range_Expr_In_Decl (S, T, Check_List => Check_List);
3768 end if;
3769 end Constrain_Index;
3771 -- Start of processing for Constrain_Array
3773 begin
3774 T := Entity (Subtype_Mark (SI));
3776 if Is_Access_Type (T) then
3777 T := Designated_Type (T);
3778 end if;
3780 S := First (Constraints (C));
3781 while Present (S) loop
3782 Number_Of_Constraints := Number_Of_Constraints + 1;
3783 Next (S);
3784 end loop;
3786 -- In either case, the index constraint must provide a discrete
3787 -- range for each index of the array type and the type of each
3788 -- discrete range must be the same as that of the corresponding
3789 -- index. (RM 3.6.1)
3791 S := First (Constraints (C));
3792 Index := First_Index (T);
3793 Analyze (Index);
3795 -- Apply constraints to each index type
3797 for J in 1 .. Number_Of_Constraints loop
3798 Constrain_Index (Index, S, Check_List);
3799 Next (Index);
3800 Next (S);
3801 end loop;
3802 end Constrain_Array;
3804 -- Start of processing for Build_Record_Checks
3806 begin
3807 if Nkind (S) = N_Subtype_Indication then
3808 Find_Type (Subtype_Mark (S));
3809 Subtype_Mark_Id := Entity (Subtype_Mark (S));
3811 -- Remaining processing depends on type
3813 case Ekind (Subtype_Mark_Id) is
3814 when Array_Kind =>
3815 Constrain_Array (S, Check_List);
3817 when others =>
3818 null;
3819 end case;
3820 end if;
3821 end Build_Record_Checks;
3823 -------------------------------------------
3824 -- Component_Needs_Simple_Initialization --
3825 -------------------------------------------
3827 function Component_Needs_Simple_Initialization
3828 (T : Entity_Id) return Boolean
3830 begin
3831 return
3832 Needs_Simple_Initialization (T)
3833 and then not Is_RTE (T, RE_Tag)
3835 -- Ada 2005 (AI-251): Check also the tag of abstract interfaces
3837 and then not Is_RTE (T, RE_Interface_Tag);
3838 end Component_Needs_Simple_Initialization;
3840 --------------------------------------
3841 -- Parent_Subtype_Renaming_Discrims --
3842 --------------------------------------
3844 function Parent_Subtype_Renaming_Discrims return Boolean is
3845 De : Entity_Id;
3846 Dp : Entity_Id;
3848 begin
3849 if Base_Type (Rec_Ent) /= Rec_Ent then
3850 return False;
3851 end if;
3853 if Etype (Rec_Ent) = Rec_Ent
3854 or else not Has_Discriminants (Rec_Ent)
3855 or else Is_Constrained (Rec_Ent)
3856 or else Is_Tagged_Type (Rec_Ent)
3857 then
3858 return False;
3859 end if;
3861 -- If there are no explicit stored discriminants we have inherited
3862 -- the root type discriminants so far, so no renamings occurred.
3864 if First_Discriminant (Rec_Ent) =
3865 First_Stored_Discriminant (Rec_Ent)
3866 then
3867 return False;
3868 end if;
3870 -- Check if we have done some trivial renaming of the parent
3871 -- discriminants, i.e. something like
3873 -- type DT (X1, X2: int) is new PT (X1, X2);
3875 De := First_Discriminant (Rec_Ent);
3876 Dp := First_Discriminant (Etype (Rec_Ent));
3877 while Present (De) loop
3878 pragma Assert (Present (Dp));
3880 if Corresponding_Discriminant (De) /= Dp then
3881 return True;
3882 end if;
3884 Next_Discriminant (De);
3885 Next_Discriminant (Dp);
3886 end loop;
3888 return Present (Dp);
3889 end Parent_Subtype_Renaming_Discrims;
3891 ------------------------
3892 -- Requires_Init_Proc --
3893 ------------------------
3895 function Requires_Init_Proc (Rec_Id : Entity_Id) return Boolean is
3896 Comp_Decl : Node_Id;
3897 Id : Entity_Id;
3898 Typ : Entity_Id;
3900 begin
3901 -- Definitely do not need one if specifically suppressed
3903 if Initialization_Suppressed (Rec_Id) then
3904 return False;
3905 end if;
3907 -- If it is a type derived from a type with unknown discriminants,
3908 -- we cannot build an initialization procedure for it.
3910 if Has_Unknown_Discriminants (Rec_Id)
3911 or else Has_Unknown_Discriminants (Etype (Rec_Id))
3912 then
3913 return False;
3914 end if;
3916 -- Otherwise we need to generate an initialization procedure if
3917 -- Is_CPP_Class is False and at least one of the following applies:
3919 -- 1. Discriminants are present, since they need to be initialized
3920 -- with the appropriate discriminant constraint expressions.
3921 -- However, the discriminant of an unchecked union does not
3922 -- count, since the discriminant is not present.
3924 -- 2. The type is a tagged type, since the implicit Tag component
3925 -- needs to be initialized with a pointer to the dispatch table.
3927 -- 3. The type contains tasks
3929 -- 4. One or more components has an initial value
3931 -- 5. One or more components is for a type which itself requires
3932 -- an initialization procedure.
3934 -- 6. One or more components is a type that requires simple
3935 -- initialization (see Needs_Simple_Initialization), except
3936 -- that types Tag and Interface_Tag are excluded, since fields
3937 -- of these types are initialized by other means.
3939 -- 7. The type is the record type built for a task type (since at
3940 -- the very least, Create_Task must be called)
3942 -- 8. The type is the record type built for a protected type (since
3943 -- at least Initialize_Protection must be called)
3945 -- 9. The type is marked as a public entity. The reason we add this
3946 -- case (even if none of the above apply) is to properly handle
3947 -- Initialize_Scalars. If a package is compiled without an IS
3948 -- pragma, and the client is compiled with an IS pragma, then
3949 -- the client will think an initialization procedure is present
3950 -- and call it, when in fact no such procedure is required, but
3951 -- since the call is generated, there had better be a routine
3952 -- at the other end of the call, even if it does nothing).
3954 -- Note: the reason we exclude the CPP_Class case is because in this
3955 -- case the initialization is performed by the C++ constructors, and
3956 -- the IP is built by Set_CPP_Constructors.
3958 if Is_CPP_Class (Rec_Id) then
3959 return False;
3961 elsif Is_Interface (Rec_Id) then
3962 return False;
3964 elsif (Has_Discriminants (Rec_Id)
3965 and then not Is_Unchecked_Union (Rec_Id))
3966 or else Is_Tagged_Type (Rec_Id)
3967 or else Is_Concurrent_Record_Type (Rec_Id)
3968 or else Has_Task (Rec_Id)
3969 then
3970 return True;
3971 end if;
3973 Id := First_Component (Rec_Id);
3974 while Present (Id) loop
3975 Comp_Decl := Parent (Id);
3976 Typ := Etype (Id);
3978 if Present (Expression (Comp_Decl))
3979 or else Has_Non_Null_Base_Init_Proc (Typ)
3980 or else Component_Needs_Simple_Initialization (Typ)
3981 then
3982 return True;
3983 end if;
3985 Next_Component (Id);
3986 end loop;
3988 -- As explained above, a record initialization procedure is needed
3989 -- for public types in case Initialize_Scalars applies to a client.
3990 -- However, such a procedure is not needed in the case where either
3991 -- of restrictions No_Initialize_Scalars or No_Default_Initialization
3992 -- applies. No_Initialize_Scalars excludes the possibility of using
3993 -- Initialize_Scalars in any partition, and No_Default_Initialization
3994 -- implies that no initialization should ever be done for objects of
3995 -- the type, so is incompatible with Initialize_Scalars.
3997 if not Restriction_Active (No_Initialize_Scalars)
3998 and then not Restriction_Active (No_Default_Initialization)
3999 and then Is_Public (Rec_Id)
4000 then
4001 return True;
4002 end if;
4004 return False;
4005 end Requires_Init_Proc;
4007 -- Start of processing for Build_Record_Init_Proc
4009 begin
4010 Rec_Type := Defining_Identifier (N);
4012 -- This may be full declaration of a private type, in which case
4013 -- the visible entity is a record, and the private entity has been
4014 -- exchanged with it in the private part of the current package.
4015 -- The initialization procedure is built for the record type, which
4016 -- is retrievable from the private entity.
4018 if Is_Incomplete_Or_Private_Type (Rec_Type) then
4019 Rec_Type := Underlying_Type (Rec_Type);
4020 end if;
4022 -- If we have a variant record with restriction No_Implicit_Conditionals
4023 -- in effect, then we skip building the procedure. This is safe because
4024 -- if we can see the restriction, so can any caller, calls to initialize
4025 -- such records are not allowed for variant records if this restriction
4026 -- is active.
4028 if Has_Variant_Part (Rec_Type)
4029 and then Restriction_Active (No_Implicit_Conditionals)
4030 then
4031 return;
4032 end if;
4034 -- If there are discriminants, build the discriminant map to replace
4035 -- discriminants by their discriminals in complex bound expressions.
4036 -- These only arise for the corresponding records of synchronized types.
4038 if Is_Concurrent_Record_Type (Rec_Type)
4039 and then Has_Discriminants (Rec_Type)
4040 then
4041 declare
4042 Disc : Entity_Id;
4043 begin
4044 Disc := First_Discriminant (Rec_Type);
4045 while Present (Disc) loop
4046 Append_Elmt (Disc, Discr_Map);
4047 Append_Elmt (Discriminal (Disc), Discr_Map);
4048 Next_Discriminant (Disc);
4049 end loop;
4050 end;
4051 end if;
4053 -- Derived types that have no type extension can use the initialization
4054 -- procedure of their parent and do not need a procedure of their own.
4055 -- This is only correct if there are no representation clauses for the
4056 -- type or its parent, and if the parent has in fact been frozen so
4057 -- that its initialization procedure exists.
4059 if Is_Derived_Type (Rec_Type)
4060 and then not Is_Tagged_Type (Rec_Type)
4061 and then not Is_Unchecked_Union (Rec_Type)
4062 and then not Has_New_Non_Standard_Rep (Rec_Type)
4063 and then not Parent_Subtype_Renaming_Discrims
4064 and then Present (Base_Init_Proc (Etype (Rec_Type)))
4065 then
4066 Copy_TSS (Base_Init_Proc (Etype (Rec_Type)), Rec_Type);
4068 -- Otherwise if we need an initialization procedure, then build one,
4069 -- mark it as public and inlinable and as having a completion.
4071 elsif Requires_Init_Proc (Rec_Type)
4072 or else Is_Unchecked_Union (Rec_Type)
4073 then
4074 Proc_Id :=
4075 Make_Defining_Identifier (Loc,
4076 Chars => Make_Init_Proc_Name (Rec_Type));
4078 -- If No_Default_Initialization restriction is active, then we don't
4079 -- want to build an init_proc, but we need to mark that an init_proc
4080 -- would be needed if this restriction was not active (so that we can
4081 -- detect attempts to call it), so set a dummy init_proc in place.
4083 if Restriction_Active (No_Default_Initialization) then
4084 Set_Init_Proc (Rec_Type, Proc_Id);
4085 return;
4086 end if;
4088 Build_Offset_To_Top_Functions;
4089 Build_CPP_Init_Procedure;
4090 Build_Init_Procedure;
4092 Set_Is_Public (Proc_Id, Is_Public (Rec_Ent));
4093 Set_Is_Internal (Proc_Id);
4094 Set_Has_Completion (Proc_Id);
4096 if not Debug_Generated_Code then
4097 Set_Debug_Info_Off (Proc_Id);
4098 end if;
4100 Set_Is_Inlined (Proc_Id, Inline_Init_Proc (Rec_Type));
4102 -- Do not build an aggregate if Modify_Tree_For_C, this isn't
4103 -- needed and may generate early references to non frozen types
4104 -- since we expand aggregate much more systematically.
4106 if Modify_Tree_For_C then
4107 return;
4108 end if;
4110 declare
4111 Agg : constant Node_Id :=
4112 Build_Equivalent_Record_Aggregate (Rec_Type);
4114 procedure Collect_Itypes (Comp : Node_Id);
4115 -- Generate references to itypes in the aggregate, because
4116 -- the first use of the aggregate may be in a nested scope.
4118 --------------------
4119 -- Collect_Itypes --
4120 --------------------
4122 procedure Collect_Itypes (Comp : Node_Id) is
4123 Ref : Node_Id;
4124 Sub_Aggr : Node_Id;
4125 Typ : constant Entity_Id := Etype (Comp);
4127 begin
4128 if Is_Array_Type (Typ) and then Is_Itype (Typ) then
4129 Ref := Make_Itype_Reference (Loc);
4130 Set_Itype (Ref, Typ);
4131 Append_Freeze_Action (Rec_Type, Ref);
4133 Ref := Make_Itype_Reference (Loc);
4134 Set_Itype (Ref, Etype (First_Index (Typ)));
4135 Append_Freeze_Action (Rec_Type, Ref);
4137 -- Recurse on nested arrays
4139 Sub_Aggr := First (Expressions (Comp));
4140 while Present (Sub_Aggr) loop
4141 Collect_Itypes (Sub_Aggr);
4142 Next (Sub_Aggr);
4143 end loop;
4144 end if;
4145 end Collect_Itypes;
4147 begin
4148 -- If there is a static initialization aggregate for the type,
4149 -- generate itype references for the types of its (sub)components,
4150 -- to prevent out-of-scope errors in the resulting tree.
4151 -- The aggregate may have been rewritten as a Raise node, in which
4152 -- case there are no relevant itypes.
4154 if Present (Agg) and then Nkind (Agg) = N_Aggregate then
4155 Set_Static_Initialization (Proc_Id, Agg);
4157 declare
4158 Comp : Node_Id;
4159 begin
4160 Comp := First (Component_Associations (Agg));
4161 while Present (Comp) loop
4162 Collect_Itypes (Expression (Comp));
4163 Next (Comp);
4164 end loop;
4165 end;
4166 end if;
4167 end;
4168 end if;
4169 end Build_Record_Init_Proc;
4171 ----------------------------
4172 -- Build_Slice_Assignment --
4173 ----------------------------
4175 -- Generates the following subprogram:
4177 -- procedure array_typeSA
4178 -- (Source, Target : Array_Type,
4179 -- Left_Lo, Left_Hi : Index;
4180 -- Right_Lo, Right_Hi : Index;
4181 -- Rev : Boolean)
4182 -- is
4183 -- Li1 : Index;
4184 -- Ri1 : Index;
4186 -- begin
4187 -- if Left_Hi < Left_Lo then
4188 -- return;
4189 -- end if;
4191 -- if Rev then
4192 -- Li1 := Left_Hi;
4193 -- Ri1 := Right_Hi;
4194 -- else
4195 -- Li1 := Left_Lo;
4196 -- Ri1 := Right_Lo;
4197 -- end if;
4199 -- loop
4200 -- Target (Li1) := Source (Ri1);
4202 -- if Rev then
4203 -- exit when Li1 = Left_Lo;
4204 -- Li1 := Index'pred (Li1);
4205 -- Ri1 := Index'pred (Ri1);
4206 -- else
4207 -- exit when Li1 = Left_Hi;
4208 -- Li1 := Index'succ (Li1);
4209 -- Ri1 := Index'succ (Ri1);
4210 -- end if;
4211 -- end loop;
4212 -- end array_typeSA;
4214 procedure Build_Slice_Assignment (Typ : Entity_Id) is
4215 Loc : constant Source_Ptr := Sloc (Typ);
4216 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
4218 Larray : constant Entity_Id := Make_Temporary (Loc, 'A');
4219 Rarray : constant Entity_Id := Make_Temporary (Loc, 'R');
4220 Left_Lo : constant Entity_Id := Make_Temporary (Loc, 'L');
4221 Left_Hi : constant Entity_Id := Make_Temporary (Loc, 'L');
4222 Right_Lo : constant Entity_Id := Make_Temporary (Loc, 'R');
4223 Right_Hi : constant Entity_Id := Make_Temporary (Loc, 'R');
4224 Rev : constant Entity_Id := Make_Temporary (Loc, 'D');
4225 -- Formal parameters of procedure
4227 Proc_Name : constant Entity_Id :=
4228 Make_Defining_Identifier (Loc,
4229 Chars => Make_TSS_Name (Typ, TSS_Slice_Assign));
4231 Lnn : constant Entity_Id := Make_Temporary (Loc, 'L');
4232 Rnn : constant Entity_Id := Make_Temporary (Loc, 'R');
4233 -- Subscripts for left and right sides
4235 Decls : List_Id;
4236 Loops : Node_Id;
4237 Stats : List_Id;
4239 begin
4240 -- Build declarations for indexes
4242 Decls := New_List;
4244 Append_To (Decls,
4245 Make_Object_Declaration (Loc,
4246 Defining_Identifier => Lnn,
4247 Object_Definition =>
4248 New_Occurrence_Of (Index, Loc)));
4250 Append_To (Decls,
4251 Make_Object_Declaration (Loc,
4252 Defining_Identifier => Rnn,
4253 Object_Definition =>
4254 New_Occurrence_Of (Index, Loc)));
4256 Stats := New_List;
4258 -- Build test for empty slice case
4260 Append_To (Stats,
4261 Make_If_Statement (Loc,
4262 Condition =>
4263 Make_Op_Lt (Loc,
4264 Left_Opnd => New_Occurrence_Of (Left_Hi, Loc),
4265 Right_Opnd => New_Occurrence_Of (Left_Lo, Loc)),
4266 Then_Statements => New_List (Make_Simple_Return_Statement (Loc))));
4268 -- Build initializations for indexes
4270 declare
4271 F_Init : constant List_Id := New_List;
4272 B_Init : constant List_Id := New_List;
4274 begin
4275 Append_To (F_Init,
4276 Make_Assignment_Statement (Loc,
4277 Name => New_Occurrence_Of (Lnn, Loc),
4278 Expression => New_Occurrence_Of (Left_Lo, Loc)));
4280 Append_To (F_Init,
4281 Make_Assignment_Statement (Loc,
4282 Name => New_Occurrence_Of (Rnn, Loc),
4283 Expression => New_Occurrence_Of (Right_Lo, Loc)));
4285 Append_To (B_Init,
4286 Make_Assignment_Statement (Loc,
4287 Name => New_Occurrence_Of (Lnn, Loc),
4288 Expression => New_Occurrence_Of (Left_Hi, Loc)));
4290 Append_To (B_Init,
4291 Make_Assignment_Statement (Loc,
4292 Name => New_Occurrence_Of (Rnn, Loc),
4293 Expression => New_Occurrence_Of (Right_Hi, Loc)));
4295 Append_To (Stats,
4296 Make_If_Statement (Loc,
4297 Condition => New_Occurrence_Of (Rev, Loc),
4298 Then_Statements => B_Init,
4299 Else_Statements => F_Init));
4300 end;
4302 -- Now construct the assignment statement
4304 Loops :=
4305 Make_Loop_Statement (Loc,
4306 Statements => New_List (
4307 Make_Assignment_Statement (Loc,
4308 Name =>
4309 Make_Indexed_Component (Loc,
4310 Prefix => New_Occurrence_Of (Larray, Loc),
4311 Expressions => New_List (New_Occurrence_Of (Lnn, Loc))),
4312 Expression =>
4313 Make_Indexed_Component (Loc,
4314 Prefix => New_Occurrence_Of (Rarray, Loc),
4315 Expressions => New_List (New_Occurrence_Of (Rnn, Loc))))),
4316 End_Label => Empty);
4318 -- Build the exit condition and increment/decrement statements
4320 declare
4321 F_Ass : constant List_Id := New_List;
4322 B_Ass : constant List_Id := New_List;
4324 begin
4325 Append_To (F_Ass,
4326 Make_Exit_Statement (Loc,
4327 Condition =>
4328 Make_Op_Eq (Loc,
4329 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
4330 Right_Opnd => New_Occurrence_Of (Left_Hi, Loc))));
4332 Append_To (F_Ass,
4333 Make_Assignment_Statement (Loc,
4334 Name => New_Occurrence_Of (Lnn, Loc),
4335 Expression =>
4336 Make_Attribute_Reference (Loc,
4337 Prefix =>
4338 New_Occurrence_Of (Index, Loc),
4339 Attribute_Name => Name_Succ,
4340 Expressions => New_List (
4341 New_Occurrence_Of (Lnn, Loc)))));
4343 Append_To (F_Ass,
4344 Make_Assignment_Statement (Loc,
4345 Name => New_Occurrence_Of (Rnn, Loc),
4346 Expression =>
4347 Make_Attribute_Reference (Loc,
4348 Prefix =>
4349 New_Occurrence_Of (Index, Loc),
4350 Attribute_Name => Name_Succ,
4351 Expressions => New_List (
4352 New_Occurrence_Of (Rnn, Loc)))));
4354 Append_To (B_Ass,
4355 Make_Exit_Statement (Loc,
4356 Condition =>
4357 Make_Op_Eq (Loc,
4358 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
4359 Right_Opnd => New_Occurrence_Of (Left_Lo, Loc))));
4361 Append_To (B_Ass,
4362 Make_Assignment_Statement (Loc,
4363 Name => New_Occurrence_Of (Lnn, Loc),
4364 Expression =>
4365 Make_Attribute_Reference (Loc,
4366 Prefix =>
4367 New_Occurrence_Of (Index, Loc),
4368 Attribute_Name => Name_Pred,
4369 Expressions => New_List (
4370 New_Occurrence_Of (Lnn, Loc)))));
4372 Append_To (B_Ass,
4373 Make_Assignment_Statement (Loc,
4374 Name => New_Occurrence_Of (Rnn, Loc),
4375 Expression =>
4376 Make_Attribute_Reference (Loc,
4377 Prefix =>
4378 New_Occurrence_Of (Index, Loc),
4379 Attribute_Name => Name_Pred,
4380 Expressions => New_List (
4381 New_Occurrence_Of (Rnn, Loc)))));
4383 Append_To (Statements (Loops),
4384 Make_If_Statement (Loc,
4385 Condition => New_Occurrence_Of (Rev, Loc),
4386 Then_Statements => B_Ass,
4387 Else_Statements => F_Ass));
4388 end;
4390 Append_To (Stats, Loops);
4392 declare
4393 Spec : Node_Id;
4394 Formals : List_Id;
4396 begin
4397 Formals := New_List (
4398 Make_Parameter_Specification (Loc,
4399 Defining_Identifier => Larray,
4400 Out_Present => True,
4401 Parameter_Type =>
4402 New_Occurrence_Of (Base_Type (Typ), Loc)),
4404 Make_Parameter_Specification (Loc,
4405 Defining_Identifier => Rarray,
4406 Parameter_Type =>
4407 New_Occurrence_Of (Base_Type (Typ), Loc)),
4409 Make_Parameter_Specification (Loc,
4410 Defining_Identifier => Left_Lo,
4411 Parameter_Type =>
4412 New_Occurrence_Of (Index, Loc)),
4414 Make_Parameter_Specification (Loc,
4415 Defining_Identifier => Left_Hi,
4416 Parameter_Type =>
4417 New_Occurrence_Of (Index, Loc)),
4419 Make_Parameter_Specification (Loc,
4420 Defining_Identifier => Right_Lo,
4421 Parameter_Type =>
4422 New_Occurrence_Of (Index, Loc)),
4424 Make_Parameter_Specification (Loc,
4425 Defining_Identifier => Right_Hi,
4426 Parameter_Type =>
4427 New_Occurrence_Of (Index, Loc)));
4429 Append_To (Formals,
4430 Make_Parameter_Specification (Loc,
4431 Defining_Identifier => Rev,
4432 Parameter_Type =>
4433 New_Occurrence_Of (Standard_Boolean, Loc)));
4435 Spec :=
4436 Make_Procedure_Specification (Loc,
4437 Defining_Unit_Name => Proc_Name,
4438 Parameter_Specifications => Formals);
4440 Discard_Node (
4441 Make_Subprogram_Body (Loc,
4442 Specification => Spec,
4443 Declarations => Decls,
4444 Handled_Statement_Sequence =>
4445 Make_Handled_Sequence_Of_Statements (Loc,
4446 Statements => Stats)));
4447 end;
4449 Set_TSS (Typ, Proc_Name);
4450 Set_Is_Pure (Proc_Name);
4451 end Build_Slice_Assignment;
4453 ------------------------------------
4454 -- Build_Untagged_Record_Equality --
4455 ------------------------------------
4457 procedure Build_Untagged_Record_Equality (Typ : Entity_Id) is
4458 Build_Eq : Boolean;
4459 Comp : Entity_Id;
4460 Decl : Node_Id;
4461 Op : Entity_Id;
4462 Eq_Op : Entity_Id;
4464 function User_Defined_Eq (T : Entity_Id) return Entity_Id;
4465 -- Check whether the type T has a user-defined primitive equality. If so
4466 -- return it, else return Empty. If true for a component of Typ, we have
4467 -- to build the primitive equality for it.
4469 ---------------------
4470 -- User_Defined_Eq --
4471 ---------------------
4473 function User_Defined_Eq (T : Entity_Id) return Entity_Id is
4474 Op : constant Entity_Id := TSS (T, TSS_Composite_Equality);
4476 begin
4477 if Present (Op) then
4478 return Op;
4479 else
4480 return Get_User_Defined_Equality (T);
4481 end if;
4482 end User_Defined_Eq;
4484 -- Start of processing for Build_Untagged_Record_Equality
4486 begin
4487 -- If a record component has a primitive equality operation, we must
4488 -- build the corresponding one for the current type.
4490 Build_Eq := False;
4491 Comp := First_Component (Typ);
4492 while Present (Comp) loop
4493 if Is_Record_Type (Etype (Comp))
4494 and then Present (User_Defined_Eq (Etype (Comp)))
4495 then
4496 Build_Eq := True;
4497 exit;
4498 end if;
4500 Next_Component (Comp);
4501 end loop;
4503 -- If there is a user-defined equality for the type, we do not create
4504 -- the implicit one.
4506 Eq_Op := Get_User_Defined_Equality (Typ);
4507 if Present (Eq_Op) then
4508 if Comes_From_Source (Eq_Op) then
4509 Build_Eq := False;
4510 else
4511 Eq_Op := Empty;
4512 end if;
4513 end if;
4515 -- If the type is derived, inherit the operation, if present, from the
4516 -- parent type. It may have been declared after the type derivation. If
4517 -- the parent type itself is derived, it may have inherited an operation
4518 -- that has itself been overridden, so update its alias and related
4519 -- flags. Ditto for inequality.
4521 if No (Eq_Op) and then Is_Derived_Type (Typ) then
4522 Eq_Op := Get_User_Defined_Equality (Etype (Typ));
4523 if Present (Eq_Op) then
4524 Copy_TSS (Eq_Op, Typ);
4525 Build_Eq := False;
4527 declare
4528 Op : constant Entity_Id := User_Defined_Eq (Typ);
4529 NE_Op : constant Entity_Id := Next_Entity (Eq_Op);
4531 begin
4532 if Present (Op) then
4533 Set_Alias (Op, Eq_Op);
4534 Set_Is_Abstract_Subprogram
4535 (Op, Is_Abstract_Subprogram (Eq_Op));
4537 if Chars (Next_Entity (Op)) = Name_Op_Ne then
4538 Set_Is_Abstract_Subprogram
4539 (Next_Entity (Op), Is_Abstract_Subprogram (NE_Op));
4540 end if;
4541 end if;
4542 end;
4543 end if;
4544 end if;
4546 -- If not inherited and not user-defined, build body as for a type with
4547 -- components of record type (i.e. a type for which "=" composes when
4548 -- used as a component in an outer composite type).
4550 if Build_Eq then
4551 Decl :=
4552 Make_Eq_Body (Typ, Make_TSS_Name (Typ, TSS_Composite_Equality));
4553 Op := Defining_Entity (Decl);
4554 Set_TSS (Typ, Op);
4555 Set_Is_Pure (Op);
4557 if Is_Library_Level_Entity (Typ) then
4558 Set_Is_Public (Op);
4559 end if;
4560 end if;
4561 end Build_Untagged_Record_Equality;
4563 -----------------------------------
4564 -- Build_Variant_Record_Equality --
4565 -----------------------------------
4567 -- Generates:
4569 -- function <<Body_Id>> (Left, Right : T) return Boolean is
4570 -- [ X : T renames Left; ]
4571 -- [ Y : T renames Right; ]
4572 -- -- The above renamings are generated only if the parameters of
4573 -- -- this built function (which are passed by the caller) are not
4574 -- -- named 'X' and 'Y'; these names are required to reuse several
4575 -- -- expander routines when generating this body.
4577 -- begin
4578 -- -- Compare discriminants
4580 -- if X.D1 /= Y.D1 or else X.D2 /= Y.D2 or else ... then
4581 -- return False;
4582 -- end if;
4584 -- -- Compare components
4586 -- if X.C1 /= Y.C1 or else X.C2 /= Y.C2 or else ... then
4587 -- return False;
4588 -- end if;
4590 -- -- Compare variant part
4592 -- case X.D1 is
4593 -- when V1 =>
4594 -- if X.C2 /= Y.C2 or else X.C3 /= Y.C3 or else ... then
4595 -- return False;
4596 -- end if;
4597 -- ...
4598 -- when Vn =>
4599 -- if X.Cn /= Y.Cn or else ... then
4600 -- return False;
4601 -- end if;
4602 -- end case;
4604 -- return True;
4605 -- end _Equality;
4607 function Build_Variant_Record_Equality
4608 (Typ : Entity_Id;
4609 Spec_Id : Entity_Id;
4610 Body_Id : Entity_Id;
4611 Param_Specs : List_Id) return Node_Id
4613 Loc : constant Source_Ptr := Sloc (Typ);
4614 Def : constant Node_Id := Parent (Typ);
4615 Comps : constant Node_Id := Component_List (Type_Definition (Def));
4616 Left : constant Entity_Id := Defining_Identifier (First (Param_Specs));
4617 Right : constant Entity_Id :=
4618 Defining_Identifier (Next (First (Param_Specs)));
4619 Decls : constant List_Id := New_List;
4620 Stmts : constant List_Id := New_List;
4622 Subp_Body : Node_Id;
4624 begin
4625 pragma Assert (not Is_Tagged_Type (Typ));
4627 -- In order to reuse the expander routines Make_Eq_If and Make_Eq_Case
4628 -- the name of the formals must be X and Y; otherwise we generate two
4629 -- renaming declarations for such purpose.
4631 if Chars (Left) /= Name_X then
4632 Append_To (Decls,
4633 Make_Object_Renaming_Declaration (Loc,
4634 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
4635 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
4636 Name => Make_Identifier (Loc, Chars (Left))));
4637 end if;
4639 if Chars (Right) /= Name_Y then
4640 Append_To (Decls,
4641 Make_Object_Renaming_Declaration (Loc,
4642 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
4643 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
4644 Name => Make_Identifier (Loc, Chars (Right))));
4645 end if;
4647 -- Unchecked_Unions require additional machinery to support equality.
4648 -- Two extra parameters (A and B) are added to the equality function
4649 -- parameter list for each discriminant of the type, in order to
4650 -- capture the inferred values of the discriminants in equality calls.
4651 -- The names of the parameters match the names of the corresponding
4652 -- discriminant, with an added suffix.
4654 if Is_Unchecked_Union (Typ) then
4655 declare
4656 Right_Formal : constant Entity_Id :=
4657 (if Present (Spec_Id) then Last_Formal (Spec_Id) else Right);
4658 Scop : constant Entity_Id :=
4659 (if Present (Spec_Id) then Spec_Id else Body_Id);
4661 procedure Decorate_Extra_Formal (F, F_Typ : Entity_Id);
4662 -- Decorate extra formal F with type F_Typ
4664 ---------------------------
4665 -- Decorate_Extra_Formal --
4666 ---------------------------
4668 procedure Decorate_Extra_Formal (F, F_Typ : Entity_Id) is
4669 begin
4670 Mutate_Ekind (F, E_In_Parameter);
4671 Set_Etype (F, F_Typ);
4672 Set_Scope (F, Scop);
4673 Set_Mechanism (F, By_Copy);
4674 end Decorate_Extra_Formal;
4676 A : Entity_Id;
4677 B : Entity_Id;
4678 Discr : Entity_Id;
4679 Discr_Type : Entity_Id;
4680 Last_Extra : Entity_Id := Empty;
4681 New_Discrs : Elist_Id;
4683 begin
4684 Mutate_Ekind (Body_Id, E_Subprogram_Body);
4685 New_Discrs := New_Elmt_List;
4687 Discr := First_Discriminant (Typ);
4688 while Present (Discr) loop
4689 Discr_Type := Etype (Discr);
4691 -- Add the new parameters as extra formals
4693 A :=
4694 Make_Defining_Identifier (Loc,
4695 Chars => New_External_Name (Chars (Discr), 'A'));
4697 Decorate_Extra_Formal (A, Discr_Type);
4699 if Present (Last_Extra) then
4700 Set_Extra_Formal (Last_Extra, A);
4701 else
4702 Set_Extra_Formal (Right_Formal, A);
4703 Set_Extra_Formals (Scop, A);
4704 end if;
4706 Append_Elmt (A, New_Discrs);
4708 B :=
4709 Make_Defining_Identifier (Loc,
4710 Chars => New_External_Name (Chars (Discr), 'B'));
4712 Decorate_Extra_Formal (B, Discr_Type);
4714 Set_Extra_Formal (A, B);
4715 Last_Extra := B;
4717 -- Generate the following code to compare each of the inferred
4718 -- discriminants:
4720 -- if a /= b then
4721 -- return False;
4722 -- end if;
4724 Append_To (Stmts,
4725 Make_If_Statement (Loc,
4726 Condition =>
4727 Make_Op_Ne (Loc,
4728 Left_Opnd => New_Occurrence_Of (A, Loc),
4729 Right_Opnd => New_Occurrence_Of (B, Loc)),
4730 Then_Statements => New_List (
4731 Make_Simple_Return_Statement (Loc,
4732 Expression =>
4733 New_Occurrence_Of (Standard_False, Loc)))));
4735 Next_Discriminant (Discr);
4736 end loop;
4738 -- Generate component-by-component comparison. Note that we must
4739 -- propagate the inferred discriminants formals to act as the case
4740 -- statement switch. Their value is added when an equality call on
4741 -- unchecked unions is expanded.
4743 Append_List_To (Stmts, Make_Eq_Case (Typ, Comps, New_Discrs));
4744 end;
4746 -- Normal case (not unchecked union)
4748 else
4749 Append_To (Stmts,
4750 Make_Eq_If (Typ, Discriminant_Specifications (Def)));
4751 Append_List_To (Stmts, Make_Eq_Case (Typ, Comps));
4752 end if;
4754 Append_To (Stmts,
4755 Make_Simple_Return_Statement (Loc,
4756 Expression => New_Occurrence_Of (Standard_True, Loc)));
4758 Subp_Body :=
4759 Make_Subprogram_Body (Loc,
4760 Specification =>
4761 Make_Function_Specification (Loc,
4762 Defining_Unit_Name => Body_Id,
4763 Parameter_Specifications => Param_Specs,
4764 Result_Definition =>
4765 New_Occurrence_Of (Standard_Boolean, Loc)),
4766 Declarations => Decls,
4767 Handled_Statement_Sequence =>
4768 Make_Handled_Sequence_Of_Statements (Loc,
4769 Statements => Stmts));
4771 return Subp_Body;
4772 end Build_Variant_Record_Equality;
4774 -----------------------------
4775 -- Check_Stream_Attributes --
4776 -----------------------------
4778 procedure Check_Stream_Attributes (Typ : Entity_Id) is
4779 Comp : Entity_Id;
4780 Par_Read : constant Boolean :=
4781 Stream_Attribute_Available (Typ, TSS_Stream_Read)
4782 and then not Has_Specified_Stream_Read (Typ);
4783 Par_Write : constant Boolean :=
4784 Stream_Attribute_Available (Typ, TSS_Stream_Write)
4785 and then not Has_Specified_Stream_Write (Typ);
4787 procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type);
4788 -- Check that Comp has a user-specified Nam stream attribute
4790 ----------------
4791 -- Check_Attr --
4792 ----------------
4794 procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type) is
4795 begin
4796 -- Move this check to sem???
4798 if not Stream_Attribute_Available (Etype (Comp), TSS_Nam) then
4799 Error_Msg_Name_1 := Nam;
4800 Error_Msg_N
4801 ("|component& in limited extension must have% attribute", Comp);
4802 end if;
4803 end Check_Attr;
4805 -- Start of processing for Check_Stream_Attributes
4807 begin
4808 if Par_Read or else Par_Write then
4809 Comp := First_Component (Typ);
4810 while Present (Comp) loop
4811 if Comes_From_Source (Comp)
4812 and then Original_Record_Component (Comp) = Comp
4813 and then Is_Limited_Type (Etype (Comp))
4814 then
4815 if Par_Read then
4816 Check_Attr (Name_Read, TSS_Stream_Read);
4817 end if;
4819 if Par_Write then
4820 Check_Attr (Name_Write, TSS_Stream_Write);
4821 end if;
4822 end if;
4824 Next_Component (Comp);
4825 end loop;
4826 end if;
4827 end Check_Stream_Attributes;
4829 ----------------------
4830 -- Clean_Task_Names --
4831 ----------------------
4833 procedure Clean_Task_Names
4834 (Typ : Entity_Id;
4835 Proc_Id : Entity_Id)
4837 begin
4838 if Has_Task (Typ)
4839 and then not Restriction_Active (No_Implicit_Heap_Allocations)
4840 and then not Global_Discard_Names
4841 and then Tagged_Type_Expansion
4842 then
4843 Set_Uses_Sec_Stack (Proc_Id);
4844 end if;
4845 end Clean_Task_Names;
4847 -------------------------------
4848 -- Copy_Discr_Checking_Funcs --
4849 -------------------------------
4851 procedure Copy_Discr_Checking_Funcs (N : Node_Id) is
4852 Typ : constant Entity_Id := Defining_Identifier (N);
4853 Comp : Entity_Id := First_Component (Typ);
4854 Old_Comp : Entity_Id := First_Component
4855 (Base_Type (Underlying_Type (Etype (Typ))));
4856 begin
4857 while Present (Comp) loop
4858 if Chars (Comp) = Chars (Old_Comp) then
4859 Set_Discriminant_Checking_Func
4860 (Comp, Discriminant_Checking_Func (Old_Comp));
4861 end if;
4863 Next_Component (Old_Comp);
4864 Next_Component (Comp);
4865 end loop;
4866 end Copy_Discr_Checking_Funcs;
4868 ------------------------------
4869 -- Expand_Freeze_Array_Type --
4870 ------------------------------
4872 procedure Expand_Freeze_Array_Type (N : Node_Id) is
4873 Typ : constant Entity_Id := Entity (N);
4874 Base : constant Entity_Id := Base_Type (Typ);
4875 Comp_Typ : constant Entity_Id := Component_Type (Typ);
4877 begin
4878 if not Is_Bit_Packed_Array (Typ) then
4880 -- If the component contains tasks, so does the array type. This may
4881 -- not be indicated in the array type because the component may have
4882 -- been a private type at the point of definition. Same if component
4883 -- type is controlled or contains protected objects.
4885 Propagate_Concurrent_Flags (Base, Comp_Typ);
4886 Set_Has_Controlled_Component
4887 (Base, Has_Controlled_Component (Comp_Typ)
4888 or else Is_Controlled (Comp_Typ));
4890 if No (Init_Proc (Base)) then
4892 -- If this is an anonymous array created for a declaration with
4893 -- an initial value, its init_proc will never be called. The
4894 -- initial value itself may have been expanded into assignments,
4895 -- in which case the object declaration is carries the
4896 -- No_Initialization flag.
4898 if Is_Itype (Base)
4899 and then Nkind (Associated_Node_For_Itype (Base)) =
4900 N_Object_Declaration
4901 and then
4902 (Present (Expression (Associated_Node_For_Itype (Base)))
4903 or else No_Initialization (Associated_Node_For_Itype (Base)))
4904 then
4905 null;
4907 -- We do not need an init proc for string or wide [wide] string,
4908 -- since the only time these need initialization in normalize or
4909 -- initialize scalars mode, and these types are treated specially
4910 -- and do not need initialization procedures.
4912 elsif Is_Standard_String_Type (Base) then
4913 null;
4915 -- Otherwise we have to build an init proc for the subtype
4917 else
4918 Build_Array_Init_Proc (Base, N);
4919 end if;
4920 end if;
4922 if Typ = Base and then Has_Controlled_Component (Base) then
4923 Build_Controlling_Procs (Base);
4925 if not Is_Limited_Type (Comp_Typ)
4926 and then Number_Dimensions (Typ) = 1
4927 then
4928 Build_Slice_Assignment (Typ);
4929 end if;
4930 end if;
4932 -- For packed case, default initialization, except if the component type
4933 -- is itself a packed structure with an initialization procedure, or
4934 -- initialize/normalize scalars active, and we have a base type, or the
4935 -- type is public, because in that case a client might specify
4936 -- Normalize_Scalars and there better be a public Init_Proc for it.
4938 elsif (Present (Init_Proc (Component_Type (Base)))
4939 and then No (Base_Init_Proc (Base)))
4940 or else (Init_Or_Norm_Scalars and then Base = Typ)
4941 or else Is_Public (Typ)
4942 then
4943 Build_Array_Init_Proc (Base, N);
4944 end if;
4945 end Expand_Freeze_Array_Type;
4947 -----------------------------------
4948 -- Expand_Freeze_Class_Wide_Type --
4949 -----------------------------------
4951 procedure Expand_Freeze_Class_Wide_Type (N : Node_Id) is
4952 function Is_C_Derivation (Typ : Entity_Id) return Boolean;
4953 -- Given a type, determine whether it is derived from a C or C++ root
4955 ---------------------
4956 -- Is_C_Derivation --
4957 ---------------------
4959 function Is_C_Derivation (Typ : Entity_Id) return Boolean is
4960 T : Entity_Id;
4962 begin
4963 T := Typ;
4964 loop
4965 if Is_CPP_Class (T)
4966 or else Convention (T) = Convention_C
4967 or else Convention (T) = Convention_CPP
4968 then
4969 return True;
4970 end if;
4972 exit when T = Etype (T);
4974 T := Etype (T);
4975 end loop;
4977 return False;
4978 end Is_C_Derivation;
4980 -- Local variables
4982 Typ : constant Entity_Id := Entity (N);
4983 Root : constant Entity_Id := Root_Type (Typ);
4985 -- Start of processing for Expand_Freeze_Class_Wide_Type
4987 begin
4988 -- Certain run-time configurations and targets do not provide support
4989 -- for controlled types.
4991 if Restriction_Active (No_Finalization) then
4992 return;
4994 -- Do not create TSS routine Finalize_Address when dispatching calls are
4995 -- disabled since the core of the routine is a dispatching call.
4997 elsif Restriction_Active (No_Dispatching_Calls) then
4998 return;
5000 -- Do not create TSS routine Finalize_Address for concurrent class-wide
5001 -- types. Ignore C, C++, CIL and Java types since it is assumed that the
5002 -- non-Ada side will handle their destruction.
5004 -- Concurrent Ada types are functionally represented by an associated
5005 -- "corresponding record type" (typenameV), which owns the actual TSS
5006 -- finalize bodies for the type (and technically class-wide type).
5008 elsif Is_Concurrent_Type (Root)
5009 or else Is_C_Derivation (Root)
5010 or else Convention (Typ) = Convention_CPP
5011 then
5012 return;
5014 -- Do not create TSS routine Finalize_Address when compiling in CodePeer
5015 -- mode since the routine contains an Unchecked_Conversion.
5017 elsif CodePeer_Mode then
5018 return;
5019 end if;
5021 -- Create the body of TSS primitive Finalize_Address. This automatically
5022 -- sets the TSS entry for the class-wide type.
5024 Make_Finalize_Address_Body (Typ);
5025 end Expand_Freeze_Class_Wide_Type;
5027 ------------------------------------
5028 -- Expand_Freeze_Enumeration_Type --
5029 ------------------------------------
5031 procedure Expand_Freeze_Enumeration_Type (N : Node_Id) is
5032 Typ : constant Entity_Id := Entity (N);
5033 Loc : constant Source_Ptr := Sloc (Typ);
5035 Arr : Entity_Id;
5036 Ent : Entity_Id;
5037 Fent : Entity_Id;
5038 Is_Contiguous : Boolean;
5039 Index_Typ : Entity_Id;
5040 Ityp : Entity_Id;
5041 Last_Repval : Uint;
5042 Lst : List_Id;
5043 Num : Nat;
5044 Pos_Expr : Node_Id;
5046 Func : Entity_Id;
5047 pragma Warnings (Off, Func);
5049 begin
5050 -- Various optimizations possible if given representation is contiguous
5052 Is_Contiguous := True;
5054 Ent := First_Literal (Typ);
5055 Last_Repval := Enumeration_Rep (Ent);
5056 Num := 1;
5057 Next_Literal (Ent);
5059 while Present (Ent) loop
5060 if Enumeration_Rep (Ent) - Last_Repval /= 1 then
5061 Is_Contiguous := False;
5062 else
5063 Last_Repval := Enumeration_Rep (Ent);
5064 end if;
5066 Num := Num + 1;
5067 Next_Literal (Ent);
5068 end loop;
5070 if Is_Contiguous then
5071 Set_Has_Contiguous_Rep (Typ);
5073 -- Now build a subtype declaration
5075 -- subtype typI is new Natural range 0 .. num - 1
5077 Index_Typ :=
5078 Make_Defining_Identifier (Loc,
5079 Chars => New_External_Name (Chars (Typ), 'I'));
5081 Append_Freeze_Action (Typ,
5082 Make_Subtype_Declaration (Loc,
5083 Defining_Identifier => Index_Typ,
5084 Subtype_Indication =>
5085 Make_Subtype_Indication (Loc,
5086 Subtype_Mark =>
5087 New_Occurrence_Of (Standard_Natural, Loc),
5088 Constraint =>
5089 Make_Range_Constraint (Loc,
5090 Range_Expression =>
5091 Make_Range (Loc,
5092 Low_Bound =>
5093 Make_Integer_Literal (Loc, 0),
5094 High_Bound =>
5095 Make_Integer_Literal (Loc, Num - 1))))));
5097 Set_Enum_Pos_To_Rep (Typ, Index_Typ);
5099 else
5100 -- Build list of literal references
5102 Lst := New_List;
5103 Ent := First_Literal (Typ);
5104 while Present (Ent) loop
5105 Append_To (Lst, New_Occurrence_Of (Ent, Sloc (Ent)));
5106 Next_Literal (Ent);
5107 end loop;
5109 -- Now build an array declaration
5111 -- typA : constant array (Natural range 0 .. num - 1) of typ :=
5112 -- (v, v, v, v, v, ....)
5114 Arr :=
5115 Make_Defining_Identifier (Loc,
5116 Chars => New_External_Name (Chars (Typ), 'A'));
5118 Append_Freeze_Action (Typ,
5119 Make_Object_Declaration (Loc,
5120 Defining_Identifier => Arr,
5121 Constant_Present => True,
5123 Object_Definition =>
5124 Make_Constrained_Array_Definition (Loc,
5125 Discrete_Subtype_Definitions => New_List (
5126 Make_Subtype_Indication (Loc,
5127 Subtype_Mark =>
5128 New_Occurrence_Of (Standard_Natural, Loc),
5129 Constraint =>
5130 Make_Range_Constraint (Loc,
5131 Range_Expression =>
5132 Make_Range (Loc,
5133 Low_Bound =>
5134 Make_Integer_Literal (Loc, 0),
5135 High_Bound =>
5136 Make_Integer_Literal (Loc, Num - 1))))),
5138 Component_Definition =>
5139 Make_Component_Definition (Loc,
5140 Aliased_Present => False,
5141 Subtype_Indication => New_Occurrence_Of (Typ, Loc))),
5143 Expression =>
5144 Make_Aggregate (Loc,
5145 Expressions => Lst)));
5147 Set_Enum_Pos_To_Rep (Typ, Arr);
5148 end if;
5150 -- Now we build the function that converts representation values to
5151 -- position values. This function has the form:
5153 -- function _Rep_To_Pos (A : etype; F : Boolean) return Integer is
5154 -- begin
5155 -- case ityp!(A) is
5156 -- when enum-lit'Enum_Rep => return posval;
5157 -- when enum-lit'Enum_Rep => return posval;
5158 -- ...
5159 -- when others =>
5160 -- [raise Constraint_Error when F "invalid data"]
5161 -- return -1;
5162 -- end case;
5163 -- end;
5165 -- Note: the F parameter determines whether the others case (no valid
5166 -- representation) raises Constraint_Error or returns a unique value
5167 -- of minus one. The latter case is used, e.g. in 'Valid code.
5169 -- Note: the reason we use Enum_Rep values in the case here is to avoid
5170 -- the code generator making inappropriate assumptions about the range
5171 -- of the values in the case where the value is invalid. ityp is a
5172 -- signed or unsigned integer type of appropriate width.
5174 -- Note: if exceptions are not supported, then we suppress the raise
5175 -- and return -1 unconditionally (this is an erroneous program in any
5176 -- case and there is no obligation to raise Constraint_Error here). We
5177 -- also do this if pragma Restrictions (No_Exceptions) is active.
5179 -- Is this right??? What about No_Exception_Propagation???
5181 -- The underlying type is signed. Reset the Is_Unsigned_Type explicitly
5182 -- because it might have been inherited from the parent type.
5184 if Enumeration_Rep (First_Literal (Typ)) < 0 then
5185 Set_Is_Unsigned_Type (Typ, False);
5186 end if;
5188 Ityp := Integer_Type_For (Esize (Typ), Is_Unsigned_Type (Typ));
5190 -- The body of the function is a case statement. First collect case
5191 -- alternatives, or optimize the contiguous case.
5193 Lst := New_List;
5195 -- If representation is contiguous, Pos is computed by subtracting
5196 -- the representation of the first literal.
5198 if Is_Contiguous then
5199 Ent := First_Literal (Typ);
5201 if Enumeration_Rep (Ent) = Last_Repval then
5203 -- Another special case: for a single literal, Pos is zero
5205 Pos_Expr := Make_Integer_Literal (Loc, Uint_0);
5207 else
5208 Pos_Expr :=
5209 Convert_To (Standard_Integer,
5210 Make_Op_Subtract (Loc,
5211 Left_Opnd =>
5212 Unchecked_Convert_To
5213 (Ityp, Make_Identifier (Loc, Name_uA)),
5214 Right_Opnd =>
5215 Make_Integer_Literal (Loc,
5216 Intval => Enumeration_Rep (First_Literal (Typ)))));
5217 end if;
5219 Append_To (Lst,
5220 Make_Case_Statement_Alternative (Loc,
5221 Discrete_Choices => New_List (
5222 Make_Range (Sloc (Enumeration_Rep_Expr (Ent)),
5223 Low_Bound =>
5224 Make_Integer_Literal (Loc,
5225 Intval => Enumeration_Rep (Ent)),
5226 High_Bound =>
5227 Make_Integer_Literal (Loc, Intval => Last_Repval))),
5229 Statements => New_List (
5230 Make_Simple_Return_Statement (Loc,
5231 Expression => Pos_Expr))));
5233 else
5234 Ent := First_Literal (Typ);
5235 while Present (Ent) loop
5236 Append_To (Lst,
5237 Make_Case_Statement_Alternative (Loc,
5238 Discrete_Choices => New_List (
5239 Make_Integer_Literal (Sloc (Enumeration_Rep_Expr (Ent)),
5240 Intval => Enumeration_Rep (Ent))),
5242 Statements => New_List (
5243 Make_Simple_Return_Statement (Loc,
5244 Expression =>
5245 Make_Integer_Literal (Loc,
5246 Intval => Enumeration_Pos (Ent))))));
5248 Next_Literal (Ent);
5249 end loop;
5250 end if;
5252 -- In normal mode, add the others clause with the test.
5253 -- If Predicates_Ignored is True, validity checks do not apply to
5254 -- the subtype.
5256 if not No_Exception_Handlers_Set
5257 and then not Predicates_Ignored (Typ)
5258 then
5259 Append_To (Lst,
5260 Make_Case_Statement_Alternative (Loc,
5261 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
5262 Statements => New_List (
5263 Make_Raise_Constraint_Error (Loc,
5264 Condition => Make_Identifier (Loc, Name_uF),
5265 Reason => CE_Invalid_Data),
5266 Make_Simple_Return_Statement (Loc,
5267 Expression => Make_Integer_Literal (Loc, -1)))));
5269 -- If either of the restrictions No_Exceptions_Handlers/Propagation is
5270 -- active then return -1 (we cannot usefully raise Constraint_Error in
5271 -- this case). See description above for further details.
5273 else
5274 Append_To (Lst,
5275 Make_Case_Statement_Alternative (Loc,
5276 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
5277 Statements => New_List (
5278 Make_Simple_Return_Statement (Loc,
5279 Expression => Make_Integer_Literal (Loc, -1)))));
5280 end if;
5282 -- Now we can build the function body
5284 Fent :=
5285 Make_Defining_Identifier (Loc, Make_TSS_Name (Typ, TSS_Rep_To_Pos));
5287 Func :=
5288 Make_Subprogram_Body (Loc,
5289 Specification =>
5290 Make_Function_Specification (Loc,
5291 Defining_Unit_Name => Fent,
5292 Parameter_Specifications => New_List (
5293 Make_Parameter_Specification (Loc,
5294 Defining_Identifier =>
5295 Make_Defining_Identifier (Loc, Name_uA),
5296 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
5297 Make_Parameter_Specification (Loc,
5298 Defining_Identifier =>
5299 Make_Defining_Identifier (Loc, Name_uF),
5300 Parameter_Type =>
5301 New_Occurrence_Of (Standard_Boolean, Loc))),
5303 Result_Definition => New_Occurrence_Of (Standard_Integer, Loc)),
5305 Declarations => Empty_List,
5307 Handled_Statement_Sequence =>
5308 Make_Handled_Sequence_Of_Statements (Loc,
5309 Statements => New_List (
5310 Make_Case_Statement (Loc,
5311 Expression =>
5312 Unchecked_Convert_To
5313 (Ityp, Make_Identifier (Loc, Name_uA)),
5314 Alternatives => Lst))));
5316 Set_TSS (Typ, Fent);
5318 -- Set Pure flag (it will be reset if the current context is not Pure).
5319 -- We also pretend there was a pragma Pure_Function so that for purposes
5320 -- of optimization and constant-folding, we will consider the function
5321 -- Pure even if we are not in a Pure context).
5323 Set_Is_Pure (Fent);
5324 Set_Has_Pragma_Pure_Function (Fent);
5326 -- Unless we are in -gnatD mode, where we are debugging generated code,
5327 -- this is an internal entity for which we don't need debug info.
5329 if not Debug_Generated_Code then
5330 Set_Debug_Info_Off (Fent);
5331 end if;
5333 Set_Is_Inlined (Fent);
5335 exception
5336 when RE_Not_Available =>
5337 return;
5338 end Expand_Freeze_Enumeration_Type;
5340 -------------------------------
5341 -- Expand_Freeze_Record_Type --
5342 -------------------------------
5344 procedure Expand_Freeze_Record_Type (N : Node_Id) is
5346 procedure Build_Class_Condition_Subprograms (Typ : Entity_Id);
5347 -- Create internal subprograms of Typ primitives that have class-wide
5348 -- preconditions or postconditions; they are invoked by the caller to
5349 -- evaluate the conditions.
5351 procedure Build_Variant_Record_Equality (Typ : Entity_Id);
5352 -- Create an equality function for the untagged variant record Typ and
5353 -- attach it to the TSS list.
5355 procedure Register_Dispatch_Table_Wrappers (Typ : Entity_Id);
5356 -- Register dispatch-table wrappers in the dispatch table of Typ
5358 procedure Validate_Tagged_Type_Extra_Formals (Typ : Entity_Id);
5359 -- Check extra formals of dispatching primitives of tagged type Typ.
5360 -- Used in pragma Debug.
5362 ---------------------------------------
5363 -- Build_Class_Condition_Subprograms --
5364 ---------------------------------------
5366 procedure Build_Class_Condition_Subprograms (Typ : Entity_Id) is
5367 Prim_List : constant Elist_Id := Primitive_Operations (Typ);
5368 Prim_Elmt : Elmt_Id := First_Elmt (Prim_List);
5369 Prim : Entity_Id;
5371 begin
5372 while Present (Prim_Elmt) loop
5373 Prim := Node (Prim_Elmt);
5375 -- Primitive with class-wide preconditions
5377 if Comes_From_Source (Prim)
5378 and then Has_Significant_Contract (Prim)
5379 and then
5380 (Present (Class_Preconditions (Prim))
5381 or else Present (Ignored_Class_Preconditions (Prim)))
5382 then
5383 if Expander_Active then
5384 Make_Class_Precondition_Subps (Prim);
5385 end if;
5387 -- Wrapper of a primitive that has or inherits class-wide
5388 -- preconditions.
5390 elsif Is_Primitive_Wrapper (Prim)
5391 and then
5392 (Present (Nearest_Class_Condition_Subprogram
5393 (Spec_Id => Prim,
5394 Kind => Class_Precondition))
5395 or else
5396 Present (Nearest_Class_Condition_Subprogram
5397 (Spec_Id => Prim,
5398 Kind => Ignored_Class_Precondition)))
5399 then
5400 if Expander_Active then
5401 Make_Class_Precondition_Subps (Prim);
5402 end if;
5403 end if;
5405 Next_Elmt (Prim_Elmt);
5406 end loop;
5407 end Build_Class_Condition_Subprograms;
5409 -----------------------------------
5410 -- Build_Variant_Record_Equality --
5411 -----------------------------------
5413 procedure Build_Variant_Record_Equality (Typ : Entity_Id) is
5414 Loc : constant Source_Ptr := Sloc (Typ);
5415 F : constant Entity_Id :=
5416 Make_Defining_Identifier (Loc,
5417 Chars => Make_TSS_Name (Typ, TSS_Composite_Equality));
5418 begin
5419 -- For a variant record with restriction No_Implicit_Conditionals
5420 -- in effect we skip building the procedure. This is safe because
5421 -- if we can see the restriction, so can any caller, and calls to
5422 -- equality test routines are not allowed for variant records if
5423 -- this restriction is active.
5425 if Restriction_Active (No_Implicit_Conditionals) then
5426 return;
5427 end if;
5429 -- Derived Unchecked_Union types no longer inherit the equality
5430 -- function of their parent.
5432 if Is_Derived_Type (Typ)
5433 and then not Is_Unchecked_Union (Typ)
5434 and then not Has_New_Non_Standard_Rep (Typ)
5435 then
5436 declare
5437 Parent_Eq : constant Entity_Id :=
5438 TSS (Root_Type (Typ), TSS_Composite_Equality);
5439 begin
5440 if Present (Parent_Eq) then
5441 Copy_TSS (Parent_Eq, Typ);
5442 return;
5443 end if;
5444 end;
5445 end if;
5447 Discard_Node (
5448 Build_Variant_Record_Equality
5449 (Typ => Typ,
5450 Spec_Id => Empty,
5451 Body_Id => F,
5452 Param_Specs => New_List (
5453 Make_Parameter_Specification (Loc,
5454 Defining_Identifier =>
5455 Make_Defining_Identifier (Loc, Name_X),
5456 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
5458 Make_Parameter_Specification (Loc,
5459 Defining_Identifier =>
5460 Make_Defining_Identifier (Loc, Name_Y),
5461 Parameter_Type => New_Occurrence_Of (Typ, Loc)))));
5463 Set_TSS (Typ, F);
5464 Set_Is_Pure (F);
5466 if not Debug_Generated_Code then
5467 Set_Debug_Info_Off (F);
5468 end if;
5469 end Build_Variant_Record_Equality;
5471 --------------------------------------
5472 -- Register_Dispatch_Table_Wrappers --
5473 --------------------------------------
5475 procedure Register_Dispatch_Table_Wrappers (Typ : Entity_Id) is
5476 Elmt : Elmt_Id := First_Elmt (Primitive_Operations (Typ));
5477 Subp : Entity_Id;
5479 begin
5480 while Present (Elmt) loop
5481 Subp := Node (Elmt);
5483 if Is_Dispatch_Table_Wrapper (Subp) then
5484 Append_Freeze_Actions (Typ,
5485 Register_Primitive (Sloc (Subp), Subp));
5486 end if;
5488 Next_Elmt (Elmt);
5489 end loop;
5490 end Register_Dispatch_Table_Wrappers;
5492 ----------------------------------------
5493 -- Validate_Tagged_Type_Extra_Formals --
5494 ----------------------------------------
5496 procedure Validate_Tagged_Type_Extra_Formals (Typ : Entity_Id) is
5497 Ovr_Subp : Entity_Id;
5498 Elmt : Elmt_Id;
5499 Subp : Entity_Id;
5501 begin
5502 pragma Assert (not Is_Class_Wide_Type (Typ));
5504 -- No check required if expansion is not active since we never
5505 -- generate extra formals in such case.
5507 if not Expander_Active then
5508 return;
5509 end if;
5511 Elmt := First_Elmt (Primitive_Operations (Typ));
5512 while Present (Elmt) loop
5513 Subp := Node (Elmt);
5515 -- Extra formals of a dispatching primitive must match:
5517 -- 1) The extra formals of its covered interface primitive
5519 if Present (Interface_Alias (Subp)) then
5520 pragma Assert
5521 (Extra_Formals_Match_OK
5522 (E => Interface_Alias (Subp),
5523 Ref_E => Alias (Subp)));
5524 end if;
5526 -- 2) The extra formals of its renamed primitive
5528 if Present (Alias (Subp)) then
5529 pragma Assert
5530 (Extra_Formals_Match_OK
5531 (E => Subp,
5532 Ref_E => Ultimate_Alias (Subp)));
5533 end if;
5535 -- 3) The extra formals of its overridden primitive
5537 if Present (Overridden_Operation (Subp)) then
5538 Ovr_Subp := Overridden_Operation (Subp);
5540 -- Handle controlling function wrapper
5542 if Is_Wrapper (Subp)
5543 and then Ultimate_Alias (Ovr_Subp) = Subp
5544 then
5545 if Present (Overridden_Operation (Ovr_Subp)) then
5546 pragma Assert
5547 (Extra_Formals_Match_OK
5548 (E => Subp,
5549 Ref_E => Overridden_Operation (Ovr_Subp)));
5550 end if;
5552 else
5553 pragma Assert
5554 (Extra_Formals_Match_OK
5555 (E => Subp,
5556 Ref_E => Ovr_Subp));
5557 end if;
5558 end if;
5560 Next_Elmt (Elmt);
5561 end loop;
5562 end Validate_Tagged_Type_Extra_Formals;
5564 -- Local variables
5566 Typ : constant Node_Id := Entity (N);
5567 Typ_Decl : constant Node_Id := Parent (Typ);
5569 Comp : Entity_Id;
5570 Comp_Typ : Entity_Id;
5571 Predef_List : List_Id;
5573 Wrapper_Decl_List : List_Id;
5574 Wrapper_Body_List : List_Id := No_List;
5576 Renamed_Eq : Node_Id := Empty;
5577 -- Defining unit name for the predefined equality function in the case
5578 -- where the type has a primitive operation that is a renaming of
5579 -- predefined equality (but only if there is also an overriding
5580 -- user-defined equality function). Used to pass this entity from
5581 -- Make_Predefined_Primitive_Specs to Predefined_Primitive_Bodies.
5583 -- Start of processing for Expand_Freeze_Record_Type
5585 begin
5586 -- Build discriminant checking functions if not a derived type (for
5587 -- derived types that are not tagged types, always use the discriminant
5588 -- checking functions of the parent type). However, for untagged types
5589 -- the derivation may have taken place before the parent was frozen, so
5590 -- we copy explicitly the discriminant checking functions from the
5591 -- parent into the components of the derived type.
5593 Build_Or_Copy_Discr_Checking_Funcs (Typ_Decl);
5595 if Is_Derived_Type (Typ)
5596 and then Is_Limited_Type (Typ)
5597 and then Is_Tagged_Type (Typ)
5598 then
5599 Check_Stream_Attributes (Typ);
5600 end if;
5602 -- Update task, protected, and controlled component flags, because some
5603 -- of the component types may have been private at the point of the
5604 -- record declaration. Detect anonymous access-to-controlled components.
5606 Comp := First_Component (Typ);
5607 while Present (Comp) loop
5608 Comp_Typ := Etype (Comp);
5610 Propagate_Concurrent_Flags (Typ, Comp_Typ);
5612 -- Do not set Has_Controlled_Component on a class-wide equivalent
5613 -- type. See Make_CW_Equivalent_Type.
5615 if not Is_Class_Wide_Equivalent_Type (Typ)
5616 and then
5617 (Has_Controlled_Component (Comp_Typ)
5618 or else (Chars (Comp) /= Name_uParent
5619 and then Is_Controlled (Comp_Typ)))
5620 then
5621 Set_Has_Controlled_Component (Typ);
5622 end if;
5624 Next_Component (Comp);
5625 end loop;
5627 -- Handle constructors of untagged CPP_Class types
5629 if not Is_Tagged_Type (Typ) and then Is_CPP_Class (Typ) then
5630 Set_CPP_Constructors (Typ);
5631 end if;
5633 -- Creation of the Dispatch Table. Note that a Dispatch Table is built
5634 -- for regular tagged types as well as for Ada types deriving from a C++
5635 -- Class, but not for tagged types directly corresponding to C++ classes
5636 -- In the later case we assume that it is created in the C++ side and we
5637 -- just use it.
5639 if Is_Tagged_Type (Typ) then
5641 -- Add the _Tag component
5643 if Underlying_Type (Etype (Typ)) = Typ then
5644 Expand_Tagged_Root (Typ);
5645 end if;
5647 if Is_CPP_Class (Typ) then
5648 Set_All_DT_Position (Typ);
5650 -- Create the tag entities with a minimum decoration
5652 if Tagged_Type_Expansion then
5653 Append_Freeze_Actions (Typ, Make_Tags (Typ));
5654 end if;
5656 Set_CPP_Constructors (Typ);
5658 else
5659 if not Building_Static_DT (Typ) then
5661 -- Usually inherited primitives are not delayed but the first
5662 -- Ada extension of a CPP_Class is an exception since the
5663 -- address of the inherited subprogram has to be inserted in
5664 -- the new Ada Dispatch Table and this is a freezing action.
5666 -- Similarly, if this is an inherited operation whose parent is
5667 -- not frozen yet, it is not in the DT of the parent, and we
5668 -- generate an explicit freeze node for the inherited operation
5669 -- so it is properly inserted in the DT of the current type.
5671 declare
5672 Elmt : Elmt_Id;
5673 Subp : Entity_Id;
5675 begin
5676 Elmt := First_Elmt (Primitive_Operations (Typ));
5677 while Present (Elmt) loop
5678 Subp := Node (Elmt);
5680 if Present (Alias (Subp)) then
5681 if Is_CPP_Class (Etype (Typ)) then
5682 Set_Has_Delayed_Freeze (Subp);
5684 elsif Has_Delayed_Freeze (Alias (Subp))
5685 and then not Is_Frozen (Alias (Subp))
5686 then
5687 Set_Is_Frozen (Subp, False);
5688 Set_Has_Delayed_Freeze (Subp);
5689 end if;
5690 end if;
5692 Next_Elmt (Elmt);
5693 end loop;
5694 end;
5695 end if;
5697 -- Unfreeze momentarily the type to add the predefined primitives
5698 -- operations. The reason we unfreeze is so that these predefined
5699 -- operations will indeed end up as primitive operations (which
5700 -- must be before the freeze point).
5702 Set_Is_Frozen (Typ, False);
5704 -- Do not add the spec of predefined primitives in case of
5705 -- CPP tagged type derivations that have convention CPP.
5707 if Is_CPP_Class (Root_Type (Typ))
5708 and then Convention (Typ) = Convention_CPP
5709 then
5710 null;
5712 -- Do not add the spec of the predefined primitives if we are
5713 -- compiling under restriction No_Dispatching_Calls.
5715 elsif not Restriction_Active (No_Dispatching_Calls) then
5716 Make_Predefined_Primitive_Specs (Typ, Predef_List, Renamed_Eq);
5717 Insert_List_Before_And_Analyze (N, Predef_List);
5718 end if;
5720 -- Ada 2005 (AI-391): For a nonabstract null extension, create
5721 -- wrapper functions for each nonoverridden inherited function
5722 -- with a controlling result of the type. The wrapper for such
5723 -- a function returns an extension aggregate that invokes the
5724 -- parent function.
5726 if Ada_Version >= Ada_2005
5727 and then not Is_Abstract_Type (Typ)
5728 and then Is_Null_Extension (Typ)
5729 then
5730 Make_Controlling_Function_Wrappers
5731 (Typ, Wrapper_Decl_List, Wrapper_Body_List);
5732 Insert_List_Before_And_Analyze (N, Wrapper_Decl_List);
5733 end if;
5735 -- Ada 2005 (AI-251): For a nonabstract type extension, build
5736 -- null procedure declarations for each set of homographic null
5737 -- procedures that are inherited from interface types but not
5738 -- overridden. This is done to ensure that the dispatch table
5739 -- entry associated with such null primitives are properly filled.
5741 if Ada_Version >= Ada_2005
5742 and then Etype (Typ) /= Typ
5743 and then not Is_Abstract_Type (Typ)
5744 and then Has_Interfaces (Typ)
5745 then
5746 Insert_Actions (N, Make_Null_Procedure_Specs (Typ));
5747 end if;
5749 Set_Is_Frozen (Typ);
5751 if not Is_Derived_Type (Typ)
5752 or else Is_Tagged_Type (Etype (Typ))
5753 then
5754 Set_All_DT_Position (Typ);
5756 -- If this is a type derived from an untagged private type whose
5757 -- full view is tagged, the type is marked tagged for layout
5758 -- reasons, but it has no dispatch table.
5760 elsif Is_Derived_Type (Typ)
5761 and then Is_Private_Type (Etype (Typ))
5762 and then not Is_Tagged_Type (Etype (Typ))
5763 then
5764 return;
5765 end if;
5767 -- Create and decorate the tags. Suppress their creation when
5768 -- not Tagged_Type_Expansion because the dispatching mechanism is
5769 -- handled internally by the virtual target.
5771 if Tagged_Type_Expansion then
5772 Append_Freeze_Actions (Typ, Make_Tags (Typ));
5774 -- Generate dispatch table of locally defined tagged type.
5775 -- Dispatch tables of library level tagged types are built
5776 -- later (see Build_Static_Dispatch_Tables).
5778 if not Building_Static_DT (Typ) then
5779 Append_Freeze_Actions (Typ, Make_DT (Typ));
5781 -- Register dispatch table wrappers in the dispatch table.
5782 -- It could not be done when these wrappers were built
5783 -- because, at that stage, the dispatch table was not
5784 -- available.
5786 Register_Dispatch_Table_Wrappers (Typ);
5787 end if;
5788 end if;
5790 -- If the type has unknown discriminants, propagate dispatching
5791 -- information to its underlying record view, which does not get
5792 -- its own dispatch table.
5794 if Is_Derived_Type (Typ)
5795 and then Has_Unknown_Discriminants (Typ)
5796 and then Present (Underlying_Record_View (Typ))
5797 then
5798 declare
5799 Rep : constant Entity_Id := Underlying_Record_View (Typ);
5800 begin
5801 Set_Access_Disp_Table
5802 (Rep, Access_Disp_Table (Typ));
5803 Set_Dispatch_Table_Wrappers
5804 (Rep, Dispatch_Table_Wrappers (Typ));
5805 Set_Direct_Primitive_Operations
5806 (Rep, Direct_Primitive_Operations (Typ));
5807 end;
5808 end if;
5810 -- Make sure that the primitives Initialize, Adjust and Finalize
5811 -- are Frozen before other TSS subprograms. We don't want them
5812 -- Frozen inside.
5814 if Is_Controlled (Typ) then
5815 if not Is_Limited_Type (Typ) then
5816 Append_Freeze_Actions (Typ,
5817 Freeze_Entity (Find_Prim_Op (Typ, Name_Adjust), Typ));
5818 end if;
5820 Append_Freeze_Actions (Typ,
5821 Freeze_Entity (Find_Prim_Op (Typ, Name_Initialize), Typ));
5823 Append_Freeze_Actions (Typ,
5824 Freeze_Entity (Find_Prim_Op (Typ, Name_Finalize), Typ));
5825 end if;
5827 -- Freeze rest of primitive operations. There is no need to handle
5828 -- the predefined primitives if we are compiling under restriction
5829 -- No_Dispatching_Calls.
5831 if not Restriction_Active (No_Dispatching_Calls) then
5832 Append_Freeze_Actions (Typ, Predefined_Primitive_Freeze (Typ));
5833 end if;
5834 end if;
5836 -- In the untagged case, ever since Ada 83 an equality function must
5837 -- be provided for variant records that are not unchecked unions.
5839 elsif Has_Discriminants (Typ)
5840 and then not Is_Limited_Type (Typ)
5841 and then Present (Component_List (Type_Definition (Typ_Decl)))
5842 and then
5843 Present (Variant_Part (Component_List (Type_Definition (Typ_Decl))))
5844 then
5845 Build_Variant_Record_Equality (Typ);
5847 -- In Ada 2012 the equality function composes, and thus must be built
5848 -- explicitly just as for tagged records.
5850 -- This is done unconditionally to ensure that tools can be linked
5851 -- properly with user programs compiled with older language versions.
5852 -- In addition, this is needed because "=" composes for bounded strings
5853 -- in all language versions (see Exp_Ch4.Expand_Composite_Equality).
5855 elsif Comes_From_Source (Typ)
5856 and then Convention (Typ) = Convention_Ada
5857 and then not Is_Limited_Type (Typ)
5858 then
5859 Build_Untagged_Record_Equality (Typ);
5860 end if;
5862 -- Before building the record initialization procedure, if we are
5863 -- dealing with a concurrent record value type, then we must go through
5864 -- the discriminants, exchanging discriminals between the concurrent
5865 -- type and the concurrent record value type. See the section "Handling
5866 -- of Discriminants" in the Einfo spec for details.
5868 if Is_Concurrent_Record_Type (Typ) and then Has_Discriminants (Typ) then
5869 declare
5870 Ctyp : constant Entity_Id :=
5871 Corresponding_Concurrent_Type (Typ);
5872 Conc_Discr : Entity_Id;
5873 Rec_Discr : Entity_Id;
5874 Temp : Entity_Id;
5876 begin
5877 Conc_Discr := First_Discriminant (Ctyp);
5878 Rec_Discr := First_Discriminant (Typ);
5879 while Present (Conc_Discr) loop
5880 Temp := Discriminal (Conc_Discr);
5881 Set_Discriminal (Conc_Discr, Discriminal (Rec_Discr));
5882 Set_Discriminal (Rec_Discr, Temp);
5884 Set_Discriminal_Link (Discriminal (Conc_Discr), Conc_Discr);
5885 Set_Discriminal_Link (Discriminal (Rec_Discr), Rec_Discr);
5887 Next_Discriminant (Conc_Discr);
5888 Next_Discriminant (Rec_Discr);
5889 end loop;
5890 end;
5891 end if;
5893 if Has_Controlled_Component (Typ) then
5894 Build_Controlling_Procs (Typ);
5895 end if;
5897 Adjust_Discriminants (Typ);
5899 -- Do not need init for interfaces on virtual targets since they're
5900 -- abstract.
5902 if Tagged_Type_Expansion or else not Is_Interface (Typ) then
5903 Build_Record_Init_Proc (Typ_Decl, Typ);
5904 end if;
5906 -- For tagged type that are not interfaces, build bodies of primitive
5907 -- operations. Note: do this after building the record initialization
5908 -- procedure, since the primitive operations may need the initialization
5909 -- routine. There is no need to add predefined primitives of interfaces
5910 -- because all their predefined primitives are abstract.
5912 if Is_Tagged_Type (Typ) and then not Is_Interface (Typ) then
5914 -- Do not add the body of predefined primitives in case of CPP tagged
5915 -- type derivations that have convention CPP.
5917 if Is_CPP_Class (Root_Type (Typ))
5918 and then Convention (Typ) = Convention_CPP
5919 then
5920 null;
5922 -- Do not add the body of the predefined primitives if we are
5923 -- compiling under restriction No_Dispatching_Calls or if we are
5924 -- compiling a CPP tagged type.
5926 elsif not Restriction_Active (No_Dispatching_Calls) then
5928 -- Create the body of TSS primitive Finalize_Address. This must
5929 -- be done before the bodies of all predefined primitives are
5930 -- created. If Typ is limited, Stream_Input and Stream_Read may
5931 -- produce build-in-place allocations and for those the expander
5932 -- needs Finalize_Address.
5934 Make_Finalize_Address_Body (Typ);
5935 Predef_List := Predefined_Primitive_Bodies (Typ, Renamed_Eq);
5936 Append_Freeze_Actions (Typ, Predef_List);
5937 end if;
5939 -- Ada 2005 (AI-391): If any wrappers were created for nonoverridden
5940 -- inherited functions, then add their bodies to the freeze actions.
5942 Append_Freeze_Actions (Typ, Wrapper_Body_List);
5943 end if;
5945 -- Create extra formals for the primitive operations of the type.
5946 -- This must be done before analyzing the body of the initialization
5947 -- procedure, because a self-referential type might call one of these
5948 -- primitives in the body of the init_proc itself.
5950 -- This is not needed:
5951 -- 1) If expansion is disabled, because extra formals are only added
5952 -- when we are generating code.
5954 -- 2) For types with foreign convention since primitives with foreign
5955 -- convention don't have extra formals and AI95-117 requires that
5956 -- all primitives of a tagged type inherit the convention.
5958 if Expander_Active
5959 and then Is_Tagged_Type (Typ)
5960 and then not Has_Foreign_Convention (Typ)
5961 then
5962 declare
5963 Elmt : Elmt_Id;
5964 E : Entity_Id;
5966 begin
5967 -- Add extra formals to primitive operations
5969 Elmt := First_Elmt (Primitive_Operations (Typ));
5970 while Present (Elmt) loop
5971 Create_Extra_Formals (Node (Elmt));
5972 Next_Elmt (Elmt);
5973 end loop;
5975 -- Add extra formals to renamings of primitive operations. The
5976 -- addition of extra formals is done in two steps to minimize
5977 -- the compile time required for this action; the evaluation of
5978 -- Find_Dispatching_Type() and Contains() is only done here for
5979 -- renamings that are not primitive operations.
5981 E := First_Entity (Scope (Typ));
5982 while Present (E) loop
5983 if Is_Dispatching_Operation (E)
5984 and then Present (Alias (E))
5985 and then Find_Dispatching_Type (E) = Typ
5986 and then not Contains (Primitive_Operations (Typ), E)
5987 then
5988 Create_Extra_Formals (E);
5989 end if;
5991 Next_Entity (E);
5992 end loop;
5994 pragma Debug (Validate_Tagged_Type_Extra_Formals (Typ));
5995 end;
5996 end if;
5998 -- Build internal subprograms of primitives with class-wide
5999 -- pre/postconditions.
6001 if Is_Tagged_Type (Typ) then
6002 Build_Class_Condition_Subprograms (Typ);
6003 end if;
6004 end Expand_Freeze_Record_Type;
6006 ------------------------------------
6007 -- Expand_N_Full_Type_Declaration --
6008 ------------------------------------
6010 procedure Expand_N_Full_Type_Declaration (N : Node_Id) is
6011 procedure Build_Master (Ptr_Typ : Entity_Id);
6012 -- Create the master associated with Ptr_Typ
6014 ------------------
6015 -- Build_Master --
6016 ------------------
6018 procedure Build_Master (Ptr_Typ : Entity_Id) is
6019 Desig_Typ : Entity_Id := Designated_Type (Ptr_Typ);
6021 begin
6022 -- If the designated type is an incomplete view coming from a
6023 -- limited-with'ed package, we need to use the nonlimited view in
6024 -- case it has tasks.
6026 if Is_Incomplete_Type (Desig_Typ)
6027 and then Present (Non_Limited_View (Desig_Typ))
6028 then
6029 Desig_Typ := Non_Limited_View (Desig_Typ);
6030 end if;
6032 -- Anonymous access types are created for the components of the
6033 -- record parameter for an entry declaration. No master is created
6034 -- for such a type.
6036 if Has_Task (Desig_Typ) then
6037 Build_Master_Entity (Ptr_Typ);
6038 Build_Master_Renaming (Ptr_Typ);
6040 -- Create a class-wide master because a Master_Id must be generated
6041 -- for access-to-limited-class-wide types whose root may be extended
6042 -- with task components.
6044 -- Note: This code covers access-to-limited-interfaces because they
6045 -- can be used to reference tasks implementing them.
6047 -- Suppress the master creation for access types created for entry
6048 -- formal parameters (parameter block component types). Seems like
6049 -- suppression should be more general for compiler-generated types,
6050 -- but testing Comes_From_Source may be too general in this case
6051 -- (affects some test output)???
6053 elsif not Is_Param_Block_Component_Type (Ptr_Typ)
6054 and then Is_Limited_Class_Wide_Type (Desig_Typ)
6055 then
6056 Build_Class_Wide_Master (Ptr_Typ);
6057 end if;
6058 end Build_Master;
6060 -- Local declarations
6062 Def_Id : constant Entity_Id := Defining_Identifier (N);
6063 B_Id : constant Entity_Id := Base_Type (Def_Id);
6064 FN : Node_Id;
6065 Par_Id : Entity_Id;
6067 -- Start of processing for Expand_N_Full_Type_Declaration
6069 begin
6070 if Is_Access_Type (Def_Id) then
6071 Build_Master (Def_Id);
6073 if Ekind (Def_Id) = E_Access_Protected_Subprogram_Type then
6074 Expand_Access_Protected_Subprogram_Type (N);
6075 end if;
6077 -- Array of anonymous access-to-task pointers
6079 elsif Ada_Version >= Ada_2005
6080 and then Is_Array_Type (Def_Id)
6081 and then Is_Access_Type (Component_Type (Def_Id))
6082 and then Ekind (Component_Type (Def_Id)) = E_Anonymous_Access_Type
6083 then
6084 Build_Master (Component_Type (Def_Id));
6086 elsif Has_Task (Def_Id) then
6087 Expand_Previous_Access_Type (Def_Id);
6089 -- Check the components of a record type or array of records for
6090 -- anonymous access-to-task pointers.
6092 elsif Ada_Version >= Ada_2005
6093 and then (Is_Record_Type (Def_Id)
6094 or else
6095 (Is_Array_Type (Def_Id)
6096 and then Is_Record_Type (Component_Type (Def_Id))))
6097 then
6098 declare
6099 Comp : Entity_Id;
6100 First : Boolean;
6101 M_Id : Entity_Id := Empty;
6102 Typ : Entity_Id;
6104 begin
6105 if Is_Array_Type (Def_Id) then
6106 Comp := First_Entity (Component_Type (Def_Id));
6107 else
6108 Comp := First_Entity (Def_Id);
6109 end if;
6111 -- Examine all components looking for anonymous access-to-task
6112 -- types.
6114 First := True;
6115 while Present (Comp) loop
6116 Typ := Etype (Comp);
6118 if Ekind (Typ) = E_Anonymous_Access_Type
6119 and then Might_Have_Tasks
6120 (Available_View (Designated_Type (Typ)))
6121 and then No (Master_Id (Typ))
6122 then
6123 -- Ensure that the record or array type have a _master
6125 if First then
6126 Build_Master_Entity (Def_Id);
6127 Build_Master_Renaming (Typ);
6128 M_Id := Master_Id (Typ);
6130 First := False;
6132 -- Reuse the same master to service any additional types
6134 else
6135 pragma Assert (Present (M_Id));
6136 Set_Master_Id (Typ, M_Id);
6137 end if;
6138 end if;
6140 Next_Entity (Comp);
6141 end loop;
6142 end;
6143 end if;
6145 Par_Id := Etype (B_Id);
6147 -- The parent type is private then we need to inherit any TSS operations
6148 -- from the full view.
6150 if Is_Private_Type (Par_Id)
6151 and then Present (Full_View (Par_Id))
6152 then
6153 Par_Id := Base_Type (Full_View (Par_Id));
6154 end if;
6156 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
6157 and then not Is_Tagged_Type (Def_Id)
6158 and then Present (Freeze_Node (Par_Id))
6159 and then Present (TSS_Elist (Freeze_Node (Par_Id)))
6160 then
6161 Ensure_Freeze_Node (B_Id);
6162 FN := Freeze_Node (B_Id);
6164 if No (TSS_Elist (FN)) then
6165 Set_TSS_Elist (FN, New_Elmt_List);
6166 end if;
6168 declare
6169 T_E : constant Elist_Id := TSS_Elist (FN);
6170 Elmt : Elmt_Id;
6172 begin
6173 Elmt := First_Elmt (TSS_Elist (Freeze_Node (Par_Id)));
6174 while Present (Elmt) loop
6175 if Chars (Node (Elmt)) /= Name_uInit then
6176 Append_Elmt (Node (Elmt), T_E);
6177 end if;
6179 Next_Elmt (Elmt);
6180 end loop;
6182 -- If the derived type itself is private with a full view, then
6183 -- associate the full view with the inherited TSS_Elist as well.
6185 if Is_Private_Type (B_Id)
6186 and then Present (Full_View (B_Id))
6187 then
6188 Ensure_Freeze_Node (Base_Type (Full_View (B_Id)));
6189 Set_TSS_Elist
6190 (Freeze_Node (Base_Type (Full_View (B_Id))), TSS_Elist (FN));
6191 end if;
6192 end;
6193 end if;
6194 end Expand_N_Full_Type_Declaration;
6196 ---------------------------------
6197 -- Expand_N_Object_Declaration --
6198 ---------------------------------
6200 procedure Expand_N_Object_Declaration (N : Node_Id) is
6201 Loc : constant Source_Ptr := Sloc (N);
6202 Def_Id : constant Entity_Id := Defining_Identifier (N);
6203 Expr : constant Node_Id := Expression (N);
6204 Obj_Def : constant Node_Id := Object_Definition (N);
6205 Typ : constant Entity_Id := Etype (Def_Id);
6206 Base_Typ : constant Entity_Id := Base_Type (Typ);
6207 Next_N : constant Node_Id := Next (N);
6209 Special_Ret_Obj : constant Boolean := Is_Special_Return_Object (Def_Id);
6210 -- If this is a special return object, it will be allocated differently
6211 -- and ultimately rewritten as a renaming, so initialization activities
6212 -- need to be deferred until after that is done.
6214 Func_Id : constant Entity_Id :=
6215 (if Special_Ret_Obj then Return_Applies_To (Scope (Def_Id)) else Empty);
6216 -- The function if this is a special return object, otherwise Empty
6218 function Build_Equivalent_Aggregate return Boolean;
6219 -- If the object has a constrained discriminated type and no initial
6220 -- value, it may be possible to build an equivalent aggregate instead,
6221 -- and prevent an actual call to the initialization procedure.
6223 function Build_Heap_Or_Pool_Allocator
6224 (Temp_Id : Entity_Id;
6225 Temp_Typ : Entity_Id;
6226 Ret_Typ : Entity_Id;
6227 Alloc_Expr : Node_Id) return Node_Id;
6228 -- Create the statements necessary to allocate a return object on the
6229 -- heap or user-defined storage pool. The object may need finalization
6230 -- actions depending on the return type.
6232 -- * Controlled case
6234 -- if BIPfinalizationmaster = null then
6235 -- Temp_Id := <Alloc_Expr>;
6236 -- else
6237 -- declare
6238 -- type Ptr_Typ is access Ret_Typ;
6239 -- for Ptr_Typ'Storage_Pool use
6240 -- Base_Pool (BIPfinalizationmaster.all).all;
6241 -- Local : Ptr_Typ;
6243 -- begin
6244 -- procedure Allocate (...) is
6245 -- begin
6246 -- System.Storage_Pools.Subpools.Allocate_Any (...);
6247 -- end Allocate;
6249 -- Local := <Alloc_Expr>;
6250 -- Temp_Id := Temp_Typ (Local);
6251 -- end;
6252 -- end if;
6254 -- * Non-controlled case
6256 -- Temp_Id := <Alloc_Expr>;
6258 -- Temp_Id is the temporary which is used to reference the internally
6259 -- created object in all allocation forms. Temp_Typ is the type of the
6260 -- temporary. Func_Id is the enclosing function. Ret_Typ is the return
6261 -- type of Func_Id. Alloc_Expr is the actual allocator.
6263 function BIP_Function_Call_Id return Entity_Id;
6264 -- If the object initialization expression is a call to a build-in-place
6265 -- function, return the id of the called function; otherwise return
6266 -- Empty.
6268 procedure Count_Default_Sized_Task_Stacks
6269 (Typ : Entity_Id;
6270 Pri_Stacks : out Int;
6271 Sec_Stacks : out Int);
6272 -- Count the number of default-sized primary and secondary task stacks
6273 -- required for task objects contained within type Typ. If the number of
6274 -- task objects contained within the type is not known at compile time
6275 -- the procedure will return the stack counts of zero.
6277 procedure Default_Initialize_Object (After : Node_Id);
6278 -- Generate all default initialization actions for object Def_Id. Any
6279 -- new code is inserted after node After.
6281 procedure Initialize_Return_Object
6282 (Tag_Assign : Node_Id;
6283 Adj_Call : Node_Id;
6284 Expr : Node_Id;
6285 Init_Stmt : Node_Id;
6286 After : Node_Id);
6287 -- Generate all initialization actions for return object Def_Id. Any
6288 -- new code is inserted after node After.
6290 function Is_Renamable_Function_Call (Expr : Node_Id) return Boolean;
6291 -- If we are not at library level and the object declaration originally
6292 -- appears in the form:
6294 -- Obj : Typ := Func (...);
6296 -- and has been rewritten as the dereference of a captured reference
6297 -- to the function result built either on the primary or the secondary
6298 -- stack, then the declaration can be rewritten as the renaming of this
6299 -- dereference:
6301 -- type Ann is access all Typ;
6302 -- Rnn : constant Axx := Func (...)'reference;
6303 -- Obj : Typ renames Rnn.all;
6305 -- This will avoid making an extra copy and, in the case where Typ needs
6306 -- finalization, a pair of calls to the Adjust and Finalize primitives,
6307 -- or Deep_Adjust and Deep_Finalize routines, depending on whether Typ
6308 -- has components that themselves need finalization.
6310 -- However, in the case of a special return object, we need to make sure
6311 -- that the object Rnn is recognized by the Is_Related_To_Func_Return
6312 -- predicate; otherwise, if it is of a type that needs finalization,
6313 -- then Requires_Cleanup_Actions would return true because of this and
6314 -- Build_Finalizer would finalize it prematurely because of this (see
6315 -- also Expand_Simple_Function_Return for the same test in the case of
6316 -- a simple return).
6318 -- Finally, in the case of a special return object, we also need to make
6319 -- sure that the two functions return on the same stack, otherwise we
6320 -- would create a dangling reference.
6322 function Make_Allocator_For_Return (Expr : Node_Id) return Node_Id;
6323 -- Make an allocator for a return object initialized with Expr
6325 function OK_To_Rename_Ref (N : Node_Id) return Boolean;
6326 -- Return True if N denotes an entity with OK_To_Rename set
6328 --------------------------------
6329 -- Build_Equivalent_Aggregate --
6330 --------------------------------
6332 function Build_Equivalent_Aggregate return Boolean is
6333 Aggr : Node_Id;
6334 Comp : Entity_Id;
6335 Discr : Elmt_Id;
6336 Full_Type : Entity_Id;
6338 begin
6339 Full_Type := Typ;
6341 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
6342 Full_Type := Full_View (Typ);
6343 end if;
6345 -- Only perform this transformation if Elaboration_Code is forbidden
6346 -- or undesirable, and if this is a global entity of a constrained
6347 -- record type.
6349 -- If Initialize_Scalars might be active this transformation cannot
6350 -- be performed either, because it will lead to different semantics
6351 -- or because elaboration code will in fact be created.
6353 if Ekind (Full_Type) /= E_Record_Subtype
6354 or else not Has_Discriminants (Full_Type)
6355 or else not Is_Constrained (Full_Type)
6356 or else Is_Controlled (Full_Type)
6357 or else Is_Limited_Type (Full_Type)
6358 or else not Restriction_Active (No_Initialize_Scalars)
6359 then
6360 return False;
6361 end if;
6363 if Ekind (Current_Scope) = E_Package
6364 and then
6365 (Restriction_Active (No_Elaboration_Code)
6366 or else Is_Preelaborated (Current_Scope))
6367 then
6368 -- Building a static aggregate is possible if the discriminants
6369 -- have static values and the other components have static
6370 -- defaults or none.
6372 Discr := First_Elmt (Discriminant_Constraint (Full_Type));
6373 while Present (Discr) loop
6374 if not Is_OK_Static_Expression (Node (Discr)) then
6375 return False;
6376 end if;
6378 Next_Elmt (Discr);
6379 end loop;
6381 -- Check that initialized components are OK, and that non-
6382 -- initialized components do not require a call to their own
6383 -- initialization procedure.
6385 Comp := First_Component (Full_Type);
6386 while Present (Comp) loop
6387 if Present (Expression (Parent (Comp)))
6388 and then
6389 not Is_OK_Static_Expression (Expression (Parent (Comp)))
6390 then
6391 return False;
6393 elsif Has_Non_Null_Base_Init_Proc (Etype (Comp)) then
6394 return False;
6396 end if;
6398 Next_Component (Comp);
6399 end loop;
6401 -- Everything is static, assemble the aggregate, discriminant
6402 -- values first.
6404 Aggr :=
6405 Make_Aggregate (Loc,
6406 Expressions => New_List,
6407 Component_Associations => New_List);
6409 Discr := First_Elmt (Discriminant_Constraint (Full_Type));
6410 while Present (Discr) loop
6411 Append_To (Expressions (Aggr), New_Copy (Node (Discr)));
6412 Next_Elmt (Discr);
6413 end loop;
6415 -- Now collect values of initialized components
6417 Comp := First_Component (Full_Type);
6418 while Present (Comp) loop
6419 if Present (Expression (Parent (Comp))) then
6420 Append_To (Component_Associations (Aggr),
6421 Make_Component_Association (Loc,
6422 Choices => New_List (New_Occurrence_Of (Comp, Loc)),
6423 Expression => New_Copy_Tree
6424 (Expression (Parent (Comp)))));
6425 end if;
6427 Next_Component (Comp);
6428 end loop;
6430 -- Finally, box-initialize remaining components
6432 Append_To (Component_Associations (Aggr),
6433 Make_Component_Association (Loc,
6434 Choices => New_List (Make_Others_Choice (Loc)),
6435 Expression => Empty));
6436 Set_Box_Present (Last (Component_Associations (Aggr)));
6437 Set_Expression (N, Aggr);
6439 if Typ /= Full_Type then
6440 Analyze_And_Resolve (Aggr, Full_View (Base_Type (Full_Type)));
6441 Rewrite (Aggr, Unchecked_Convert_To (Typ, Aggr));
6442 Analyze_And_Resolve (Aggr, Typ);
6443 else
6444 Analyze_And_Resolve (Aggr, Full_Type);
6445 end if;
6447 return True;
6449 else
6450 return False;
6451 end if;
6452 end Build_Equivalent_Aggregate;
6454 ----------------------------------
6455 -- Build_Heap_Or_Pool_Allocator --
6456 ----------------------------------
6458 function Build_Heap_Or_Pool_Allocator
6459 (Temp_Id : Entity_Id;
6460 Temp_Typ : Entity_Id;
6461 Ret_Typ : Entity_Id;
6462 Alloc_Expr : Node_Id) return Node_Id
6464 begin
6465 pragma Assert (Is_Build_In_Place_Function (Func_Id));
6467 -- Processing for objects that require finalization actions
6469 if Needs_Finalization (Ret_Typ) then
6470 declare
6471 Decls : constant List_Id := New_List;
6472 Fin_Mas_Id : constant Entity_Id :=
6473 Build_In_Place_Formal (Func_Id, BIP_Finalization_Master);
6474 Orig_Expr : constant Node_Id := New_Copy_Tree (Alloc_Expr);
6475 Stmts : constant List_Id := New_List;
6476 Local_Id : Entity_Id;
6477 Pool_Id : Entity_Id;
6478 Ptr_Typ : Entity_Id;
6480 begin
6481 -- Generate:
6482 -- Pool_Id renames Base_Pool (BIPfinalizationmaster.all).all;
6484 Pool_Id := Make_Temporary (Loc, 'P');
6486 Append_To (Decls,
6487 Make_Object_Renaming_Declaration (Loc,
6488 Defining_Identifier => Pool_Id,
6489 Subtype_Mark =>
6490 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc),
6491 Name =>
6492 Make_Explicit_Dereference (Loc,
6493 Prefix =>
6494 Make_Function_Call (Loc,
6495 Name =>
6496 New_Occurrence_Of (RTE (RE_Base_Pool), Loc),
6497 Parameter_Associations => New_List (
6498 Make_Explicit_Dereference (Loc,
6499 Prefix =>
6500 New_Occurrence_Of (Fin_Mas_Id, Loc)))))));
6502 -- Create an access type which uses the storage pool of the
6503 -- caller's master. This additional type is necessary because
6504 -- the finalization master cannot be associated with the type
6505 -- of the temporary. Otherwise the secondary stack allocation
6506 -- will fail.
6508 -- Generate:
6509 -- type Ptr_Typ is access Ret_Typ;
6511 Ptr_Typ := Make_Temporary (Loc, 'P');
6513 Append_To (Decls,
6514 Make_Full_Type_Declaration (Loc,
6515 Defining_Identifier => Ptr_Typ,
6516 Type_Definition =>
6517 Make_Access_To_Object_Definition (Loc,
6518 Subtype_Indication =>
6519 New_Occurrence_Of (Ret_Typ, Loc))));
6521 -- Perform minor decoration in order to set the master and the
6522 -- storage pool attributes.
6524 Mutate_Ekind (Ptr_Typ, E_Access_Type);
6525 Set_Finalization_Master (Ptr_Typ, Fin_Mas_Id);
6526 Set_Associated_Storage_Pool (Ptr_Typ, Pool_Id);
6528 -- Create the temporary, generate:
6529 -- Local_Id : Ptr_Typ;
6531 Local_Id := Make_Temporary (Loc, 'T');
6533 Append_To (Decls,
6534 Make_Object_Declaration (Loc,
6535 Defining_Identifier => Local_Id,
6536 Object_Definition =>
6537 New_Occurrence_Of (Ptr_Typ, Loc)));
6539 -- Allocate the object, generate:
6540 -- Local_Id := <Alloc_Expr>;
6542 Append_To (Stmts,
6543 Make_Assignment_Statement (Loc,
6544 Name => New_Occurrence_Of (Local_Id, Loc),
6545 Expression => Alloc_Expr));
6547 -- Generate:
6548 -- Temp_Id := Temp_Typ (Local_Id);
6550 Append_To (Stmts,
6551 Make_Assignment_Statement (Loc,
6552 Name => New_Occurrence_Of (Temp_Id, Loc),
6553 Expression =>
6554 Unchecked_Convert_To (Temp_Typ,
6555 New_Occurrence_Of (Local_Id, Loc))));
6557 -- Wrap the allocation in a block. This is further conditioned
6558 -- by checking the caller finalization master at runtime. A
6559 -- null value indicates a non-existent master, most likely due
6560 -- to a Finalize_Storage_Only allocation.
6562 -- Generate:
6563 -- if BIPfinalizationmaster = null then
6564 -- Temp_Id := <Orig_Expr>;
6565 -- else
6566 -- declare
6567 -- <Decls>
6568 -- begin
6569 -- <Stmts>
6570 -- end;
6571 -- end if;
6573 return
6574 Make_If_Statement (Loc,
6575 Condition =>
6576 Make_Op_Eq (Loc,
6577 Left_Opnd => New_Occurrence_Of (Fin_Mas_Id, Loc),
6578 Right_Opnd => Make_Null (Loc)),
6580 Then_Statements => New_List (
6581 Make_Assignment_Statement (Loc,
6582 Name => New_Occurrence_Of (Temp_Id, Loc),
6583 Expression => Orig_Expr)),
6585 Else_Statements => New_List (
6586 Make_Block_Statement (Loc,
6587 Declarations => Decls,
6588 Handled_Statement_Sequence =>
6589 Make_Handled_Sequence_Of_Statements (Loc,
6590 Statements => Stmts))));
6591 end;
6593 -- For all other cases, generate:
6594 -- Temp_Id := <Alloc_Expr>;
6596 else
6597 return
6598 Make_Assignment_Statement (Loc,
6599 Name => New_Occurrence_Of (Temp_Id, Loc),
6600 Expression => Alloc_Expr);
6601 end if;
6602 end Build_Heap_Or_Pool_Allocator;
6604 --------------------------
6605 -- BIP_Function_Call_Id --
6606 --------------------------
6608 function BIP_Function_Call_Id return Entity_Id is
6610 function Func_Call_Id (Function_Call : Node_Id) return Entity_Id;
6611 -- Return the id of the called function.
6613 function Func_Call_Id (Function_Call : Node_Id) return Entity_Id is
6614 Call_Node : constant Node_Id := Unqual_Conv (Function_Call);
6616 begin
6617 if Is_Entity_Name (Name (Call_Node)) then
6618 return Entity (Name (Call_Node));
6620 elsif Nkind (Name (Call_Node)) = N_Explicit_Dereference then
6621 return Etype (Name (Call_Node));
6623 else
6624 pragma Assert (Nkind (Name (Call_Node)) = N_Selected_Component);
6625 return Etype (Entity (Selector_Name (Name (Call_Node))));
6626 end if;
6627 end Func_Call_Id;
6629 -- Local declarations
6631 BIP_Func_Call : Node_Id;
6632 Expr_Q : constant Node_Id := Unqual_Conv (Expr);
6634 -- Start of processing for BIP_Function_Call_Id
6636 begin
6637 if Is_Build_In_Place_Function_Call (Expr_Q) then
6638 return Func_Call_Id (Expr_Q);
6639 end if;
6641 BIP_Func_Call := Unqual_BIP_Iface_Function_Call (Expr_Q);
6643 if Present (BIP_Func_Call) then
6645 -- In the case of an explicitly dereferenced call, return the
6646 -- subprogram type.
6648 if Nkind (Name (BIP_Func_Call)) = N_Explicit_Dereference then
6649 return Etype (Name (BIP_Func_Call));
6650 else
6651 pragma Assert (Is_Entity_Name (Name (BIP_Func_Call)));
6652 return Entity (Name (BIP_Func_Call));
6653 end if;
6655 elsif Nkind (Expr_Q) = N_Reference
6656 and then Is_Build_In_Place_Function_Call (Prefix (Expr_Q))
6657 then
6658 return Func_Call_Id (Prefix (Expr_Q));
6660 else
6661 return Empty;
6662 end if;
6663 end BIP_Function_Call_Id;
6665 -------------------------------------
6666 -- Count_Default_Sized_Task_Stacks --
6667 -------------------------------------
6669 procedure Count_Default_Sized_Task_Stacks
6670 (Typ : Entity_Id;
6671 Pri_Stacks : out Int;
6672 Sec_Stacks : out Int)
6674 Component : Entity_Id;
6676 begin
6677 -- To calculate the number of default-sized task stacks required for
6678 -- an object of Typ, a depth-first recursive traversal of the AST
6679 -- from the Typ entity node is undertaken. Only type nodes containing
6680 -- task objects are visited.
6682 Pri_Stacks := 0;
6683 Sec_Stacks := 0;
6685 if not Has_Task (Typ) then
6686 return;
6687 end if;
6689 case Ekind (Typ) is
6690 when E_Task_Subtype
6691 | E_Task_Type
6693 -- A task type is found marking the bottom of the descent. If
6694 -- the type has no representation aspect for the corresponding
6695 -- stack then that stack is using the default size.
6697 if Present (Get_Rep_Item (Typ, Name_Storage_Size)) then
6698 Pri_Stacks := 0;
6699 else
6700 Pri_Stacks := 1;
6701 end if;
6703 if Present (Get_Rep_Item (Typ, Name_Secondary_Stack_Size)) then
6704 Sec_Stacks := 0;
6705 else
6706 Sec_Stacks := 1;
6707 end if;
6709 when E_Array_Subtype
6710 | E_Array_Type
6712 -- First find the number of default stacks contained within an
6713 -- array component.
6715 Count_Default_Sized_Task_Stacks
6716 (Component_Type (Typ),
6717 Pri_Stacks,
6718 Sec_Stacks);
6720 -- Then multiply the result by the size of the array
6722 declare
6723 Quantity : constant Int := Number_Of_Elements_In_Array (Typ);
6724 -- Number_Of_Elements_In_Array is non-trival, consequently
6725 -- its result is captured as an optimization.
6727 begin
6728 Pri_Stacks := Pri_Stacks * Quantity;
6729 Sec_Stacks := Sec_Stacks * Quantity;
6730 end;
6732 when E_Protected_Subtype
6733 | E_Protected_Type
6734 | E_Record_Subtype
6735 | E_Record_Type
6737 Component := First_Component_Or_Discriminant (Typ);
6739 -- Recursively descend each component of the composite type
6740 -- looking for tasks, but only if the component is marked as
6741 -- having a task.
6743 while Present (Component) loop
6744 if Has_Task (Etype (Component)) then
6745 declare
6746 P : Int;
6747 S : Int;
6749 begin
6750 Count_Default_Sized_Task_Stacks
6751 (Etype (Component), P, S);
6752 Pri_Stacks := Pri_Stacks + P;
6753 Sec_Stacks := Sec_Stacks + S;
6754 end;
6755 end if;
6757 Next_Component_Or_Discriminant (Component);
6758 end loop;
6760 when E_Limited_Private_Subtype
6761 | E_Limited_Private_Type
6762 | E_Record_Subtype_With_Private
6763 | E_Record_Type_With_Private
6765 -- Switch to the full view of the private type to continue
6766 -- search.
6768 Count_Default_Sized_Task_Stacks
6769 (Full_View (Typ), Pri_Stacks, Sec_Stacks);
6771 -- Other types should not contain tasks
6773 when others =>
6774 raise Program_Error;
6775 end case;
6776 end Count_Default_Sized_Task_Stacks;
6778 -------------------------------
6779 -- Default_Initialize_Object --
6780 -------------------------------
6782 procedure Default_Initialize_Object (After : Node_Id) is
6783 function New_Object_Reference return Node_Id;
6784 -- Return a new reference to Def_Id with attributes Assignment_OK and
6785 -- Must_Not_Freeze already set.
6787 function Simple_Initialization_OK
6788 (Init_Typ : Entity_Id) return Boolean;
6789 -- Determine whether object declaration N with entity Def_Id needs
6790 -- simple initialization, assuming that it is of type Init_Typ.
6792 --------------------------
6793 -- New_Object_Reference --
6794 --------------------------
6796 function New_Object_Reference return Node_Id is
6797 Obj_Ref : constant Node_Id := New_Occurrence_Of (Def_Id, Loc);
6799 begin
6800 -- The call to the type init proc or [Deep_]Finalize must not
6801 -- freeze the related object as the call is internally generated.
6802 -- This way legal rep clauses that apply to the object will not be
6803 -- flagged. Note that the initialization call may be removed if
6804 -- pragma Import is encountered or moved to the freeze actions of
6805 -- the object because of an address clause.
6807 Set_Assignment_OK (Obj_Ref);
6808 Set_Must_Not_Freeze (Obj_Ref);
6810 return Obj_Ref;
6811 end New_Object_Reference;
6813 ------------------------------
6814 -- Simple_Initialization_OK --
6815 ------------------------------
6817 function Simple_Initialization_OK
6818 (Init_Typ : Entity_Id) return Boolean
6820 begin
6821 -- Do not consider the object declaration if it comes with an
6822 -- initialization expression, or is internal in which case it
6823 -- will be assigned later.
6825 return
6826 not Is_Internal (Def_Id)
6827 and then not Has_Init_Expression (N)
6828 and then Needs_Simple_Initialization
6829 (Typ => Init_Typ,
6830 Consider_IS =>
6831 Initialize_Scalars
6832 and then No (Following_Address_Clause (N)));
6833 end Simple_Initialization_OK;
6835 -- Local variables
6837 Exceptions_OK : constant Boolean :=
6838 not Restriction_Active (No_Exception_Propagation);
6840 Aggr_Init : Node_Id;
6841 Comp_Init : List_Id := No_List;
6842 Fin_Block : Node_Id;
6843 Fin_Call : Node_Id;
6844 Init_Stmts : List_Id := No_List;
6845 Obj_Init : Node_Id := Empty;
6846 Obj_Ref : Node_Id;
6848 -- Start of processing for Default_Initialize_Object
6850 begin
6851 -- Default initialization is suppressed for objects that are already
6852 -- known to be imported (i.e. whose declaration specifies the Import
6853 -- aspect). Note that for objects with a pragma Import, we generate
6854 -- initialization here, and then remove it downstream when processing
6855 -- the pragma. It is also suppressed for variables for which a pragma
6856 -- Suppress_Initialization has been explicitly given
6858 if Is_Imported (Def_Id) or else Suppress_Initialization (Def_Id) then
6859 return;
6861 -- Nothing to do if the object being initialized is of a task type
6862 -- and restriction No_Tasking is in effect, because this is a direct
6863 -- violation of the restriction.
6865 elsif Is_Task_Type (Base_Typ)
6866 and then Restriction_Active (No_Tasking)
6867 then
6868 return;
6869 end if;
6871 -- The expansion performed by this routine is as follows:
6873 -- begin
6874 -- Abort_Defer;
6875 -- Type_Init_Proc (Obj);
6877 -- begin
6878 -- [Deep_]Initialize (Obj);
6880 -- exception
6881 -- when others =>
6882 -- [Deep_]Finalize (Obj, Self => False);
6883 -- raise;
6884 -- end;
6885 -- at end
6886 -- Abort_Undefer_Direct;
6887 -- end;
6889 -- Initialize the components of the object
6891 if Has_Non_Null_Base_Init_Proc (Typ)
6892 and then not No_Initialization (N)
6893 and then not Initialization_Suppressed (Typ)
6894 then
6895 -- Do not initialize the components if No_Default_Initialization
6896 -- applies as the actual restriction check will occur later when
6897 -- the object is frozen as it is not known yet whether the object
6898 -- is imported or not.
6900 if not Restriction_Active (No_Default_Initialization) then
6902 -- If the values of the components are compile-time known, use
6903 -- their prebuilt aggregate form directly.
6905 Aggr_Init := Static_Initialization (Base_Init_Proc (Typ));
6907 if Present (Aggr_Init) then
6908 Set_Expression (N,
6909 New_Copy_Tree (Aggr_Init, New_Scope => Current_Scope));
6911 -- If type has discriminants, try to build an equivalent
6912 -- aggregate using discriminant values from the declaration.
6913 -- This is a useful optimization, in particular if restriction
6914 -- No_Elaboration_Code is active.
6916 elsif Build_Equivalent_Aggregate then
6917 null;
6919 -- Optimize the default initialization of an array object when
6920 -- pragma Initialize_Scalars or Normalize_Scalars is in effect.
6921 -- Construct an in-place initialization aggregate which may be
6922 -- convert into a fast memset by the backend.
6924 elsif Init_Or_Norm_Scalars
6925 and then Is_Array_Type (Typ)
6927 -- The array must lack atomic components because they are
6928 -- treated as non-static, and as a result the backend will
6929 -- not initialize the memory in one go.
6931 and then not Has_Atomic_Components (Typ)
6933 -- The array must not be packed because the invalid values
6934 -- in System.Scalar_Values are multiples of Storage_Unit.
6936 and then not Is_Packed (Typ)
6938 -- The array must have static non-empty ranges, otherwise
6939 -- the backend cannot initialize the memory in one go.
6941 and then Has_Static_Non_Empty_Array_Bounds (Typ)
6943 -- The optimization is only relevant for arrays of scalar
6944 -- types.
6946 and then Is_Scalar_Type (Component_Type (Typ))
6948 -- Similar to regular array initialization using a type
6949 -- init proc, predicate checks are not performed because the
6950 -- initialization values are intentionally invalid, and may
6951 -- violate the predicate.
6953 and then not Has_Predicates (Component_Type (Typ))
6955 -- Array default component value takes precedence over
6956 -- Init_Or_Norm_Scalars.
6958 and then No (Find_Aspect (Typ,
6959 Aspect_Default_Component_Value))
6961 -- The component type must have a single initialization value
6963 and then Simple_Initialization_OK (Component_Type (Typ))
6964 then
6965 Set_No_Initialization (N, False);
6966 Set_Expression (N,
6967 Get_Simple_Init_Val
6968 (Typ => Typ,
6969 N => Obj_Def,
6970 Size => (if Known_Esize (Def_Id) then Esize (Def_Id)
6971 else Uint_0)));
6973 Analyze_And_Resolve
6974 (Expression (N), Typ, Suppress => All_Checks);
6976 -- Otherwise invoke the type init proc, generate:
6977 -- Type_Init_Proc (Obj);
6979 else
6980 Obj_Ref := New_Object_Reference;
6982 if Comes_From_Source (Def_Id) then
6983 Initialization_Warning (Obj_Ref);
6984 end if;
6986 Comp_Init := Build_Initialization_Call (Loc, Obj_Ref, Typ);
6987 end if;
6988 end if;
6990 -- Provide a default value if the object needs simple initialization
6992 elsif Simple_Initialization_OK (Typ) then
6993 Set_No_Initialization (N, False);
6994 Set_Expression (N,
6995 Get_Simple_Init_Val
6996 (Typ => Typ,
6997 N => Obj_Def,
6998 Size =>
6999 (if Known_Esize (Def_Id) then Esize (Def_Id) else Uint_0)));
7001 Analyze_And_Resolve (Expression (N), Typ);
7002 end if;
7004 -- Initialize the object, generate:
7005 -- [Deep_]Initialize (Obj);
7007 if Needs_Finalization (Typ) and then not No_Initialization (N) then
7008 Obj_Init :=
7009 Make_Init_Call
7010 (Obj_Ref => New_Object_Reference,
7011 Typ => Typ);
7012 end if;
7014 -- Build a special finalization block when both the object and its
7015 -- controlled components are to be initialized. The block finalizes
7016 -- the components if the object initialization fails. Generate:
7018 -- begin
7019 -- <Obj_Init>
7021 -- exception
7022 -- when others =>
7023 -- <Fin_Call>
7024 -- raise;
7025 -- end;
7027 if Has_Controlled_Component (Typ)
7028 and then Present (Comp_Init)
7029 and then Present (Obj_Init)
7030 and then Exceptions_OK
7031 then
7032 Init_Stmts := Comp_Init;
7034 Fin_Call :=
7035 Make_Final_Call
7036 (Obj_Ref => New_Object_Reference,
7037 Typ => Typ,
7038 Skip_Self => True);
7040 if Present (Fin_Call) then
7042 -- Do not emit warnings related to the elaboration order when a
7043 -- controlled object is declared before the body of Finalize is
7044 -- seen.
7046 if Legacy_Elaboration_Checks then
7047 Set_No_Elaboration_Check (Fin_Call);
7048 end if;
7050 Fin_Block :=
7051 Make_Block_Statement (Loc,
7052 Declarations => No_List,
7054 Handled_Statement_Sequence =>
7055 Make_Handled_Sequence_Of_Statements (Loc,
7056 Statements => New_List (Obj_Init),
7058 Exception_Handlers => New_List (
7059 Make_Exception_Handler (Loc,
7060 Exception_Choices => New_List (
7061 Make_Others_Choice (Loc)),
7063 Statements => New_List (
7064 Fin_Call,
7065 Make_Raise_Statement (Loc))))));
7067 -- Signal the ABE mechanism that the block carries out
7068 -- initialization actions.
7070 Set_Is_Initialization_Block (Fin_Block);
7072 Append_To (Init_Stmts, Fin_Block);
7073 end if;
7075 -- Otherwise finalization is not required, the initialization calls
7076 -- are passed to the abort block building circuitry, generate:
7078 -- Type_Init_Proc (Obj);
7079 -- [Deep_]Initialize (Obj);
7081 else
7082 if Present (Comp_Init) then
7083 Init_Stmts := Comp_Init;
7084 end if;
7086 if Present (Obj_Init) then
7087 if No (Init_Stmts) then
7088 Init_Stmts := New_List;
7089 end if;
7091 Append_To (Init_Stmts, Obj_Init);
7092 end if;
7093 end if;
7095 -- Build an abort block to protect the initialization calls
7097 if Abort_Allowed
7098 and then Present (Comp_Init)
7099 and then Present (Obj_Init)
7100 then
7101 -- Generate:
7102 -- Abort_Defer;
7104 Prepend_To (Init_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
7106 -- When exceptions are propagated, abort deferral must take place
7107 -- in the presence of initialization or finalization exceptions.
7108 -- Generate:
7110 -- begin
7111 -- Abort_Defer;
7112 -- <Init_Stmts>
7113 -- at end
7114 -- Abort_Undefer_Direct;
7115 -- end;
7117 if Exceptions_OK then
7118 Init_Stmts := New_List (
7119 Build_Abort_Undefer_Block (Loc,
7120 Stmts => Init_Stmts,
7121 Context => N));
7123 -- Otherwise exceptions are not propagated. Generate:
7125 -- Abort_Defer;
7126 -- <Init_Stmts>
7127 -- Abort_Undefer;
7129 else
7130 Append_To (Init_Stmts,
7131 Build_Runtime_Call (Loc, RE_Abort_Undefer));
7132 end if;
7133 end if;
7135 -- Insert the whole initialization sequence into the tree. If the
7136 -- object has a delayed freeze, as will be the case when it has
7137 -- aspect specifications, the initialization sequence is part of
7138 -- the freeze actions.
7140 if Present (Init_Stmts) then
7141 if Has_Delayed_Freeze (Def_Id) then
7142 Append_Freeze_Actions (Def_Id, Init_Stmts);
7143 else
7144 Insert_Actions_After (After, Init_Stmts);
7145 end if;
7146 end if;
7147 end Default_Initialize_Object;
7149 ------------------------------
7150 -- Initialize_Return_Object --
7151 ------------------------------
7153 procedure Initialize_Return_Object
7154 (Tag_Assign : Node_Id;
7155 Adj_Call : Node_Id;
7156 Expr : Node_Id;
7157 Init_Stmt : Node_Id;
7158 After : Node_Id)
7160 begin
7161 if Present (Tag_Assign) then
7162 Insert_Action_After (After, Tag_Assign);
7163 end if;
7165 if Present (Adj_Call) then
7166 Insert_Action_After (After, Adj_Call);
7167 end if;
7169 if No (Expr) then
7170 Default_Initialize_Object (After);
7172 elsif Is_Delayed_Aggregate (Expr)
7173 and then not No_Initialization (N)
7174 then
7175 Convert_Aggr_In_Object_Decl (N);
7177 elsif Present (Init_Stmt) then
7178 Insert_Action_After (After, Init_Stmt);
7179 Set_Expression (N, Empty);
7180 end if;
7181 end Initialize_Return_Object;
7183 --------------------------------
7184 -- Is_Renamable_Function_Call --
7185 --------------------------------
7187 function Is_Renamable_Function_Call (Expr : Node_Id) return Boolean is
7188 begin
7189 return not Is_Library_Level_Entity (Def_Id)
7190 and then Is_Captured_Function_Call (Expr)
7191 and then (not Special_Ret_Obj
7192 or else
7193 (Is_Related_To_Func_Return (Entity (Prefix (Expr)))
7194 and then Needs_Secondary_Stack (Etype (Expr)) =
7195 Needs_Secondary_Stack (Etype (Func_Id))));
7196 end Is_Renamable_Function_Call;
7198 -------------------------------
7199 -- Make_Allocator_For_Return --
7200 -------------------------------
7202 function Make_Allocator_For_Return (Expr : Node_Id) return Node_Id is
7203 Alloc : Node_Id;
7204 Alloc_Expr : Entity_Id;
7205 Alloc_Typ : Entity_Id;
7207 begin
7208 -- If the return object's declaration does not include an expression,
7209 -- then we use its subtype for the allocation. Likewise in the case
7210 -- of a degenerate expression like a raise expression.
7212 if No (Expr)
7213 or else Nkind (Original_Node (Expr)) = N_Raise_Expression
7214 then
7215 Alloc_Typ := Typ;
7217 -- If the return object's declaration includes an expression, then
7218 -- there are two cases: either the nominal subtype of the object is
7219 -- definite and we can use it for the allocation directly, or it is
7220 -- not and Analyze_Object_Declaration should have built an actual
7221 -- subtype from the expression.
7223 -- However, there are exceptions in the latter case for interfaces
7224 -- (see Analyze_Object_Declaration), as well as class-wide types and
7225 -- types with unknown discriminants if they are additionally limited
7226 -- (see Expand_Subtype_From_Expr), so we must cope with them.
7228 elsif Is_Interface (Typ) then
7229 pragma Assert (Is_Class_Wide_Type (Typ));
7231 -- For interfaces, we use the type of the expression, except if
7232 -- we need to put back a conversion that we have removed earlier
7233 -- in the processing.
7235 if Is_Class_Wide_Type (Etype (Expr)) then
7236 Alloc_Typ := Typ;
7237 else
7238 Alloc_Typ := Etype (Expr);
7239 end if;
7241 elsif Is_Class_Wide_Type (Typ) then
7243 -- For class-wide types, we have to make sure that we use the
7244 -- dynamic type of the expression for the allocation, either by
7245 -- means of its (static) subtype or through the actual subtype.
7247 if Has_Tag_Of_Type (Expr) then
7248 Alloc_Typ := Etype (Expr);
7250 else pragma Assert (Ekind (Typ) = E_Class_Wide_Subtype
7251 and then Present (Equivalent_Type (Typ)));
7253 Alloc_Typ := Typ;
7254 end if;
7256 else pragma Assert (Is_Definite_Subtype (Typ)
7257 or else (Has_Unknown_Discriminants (Typ)
7258 and then Is_Inherently_Limited_Type (Typ)));
7260 Alloc_Typ := Typ;
7261 end if;
7263 -- If the return object's declaration includes an expression and the
7264 -- declaration isn't marked as No_Initialization, then we generate an
7265 -- allocator with a qualified expression. Although this is necessary
7266 -- only in the case where the result type is an interface (or class-
7267 -- wide interface), we do it in all cases for the sake of consistency
7268 -- instead of subsequently generating a separate assignment.
7270 if Present (Expr)
7271 and then not Is_Delayed_Aggregate (Expr)
7272 and then not No_Initialization (N)
7273 then
7274 -- Ada 2005 (AI95-344): If the result type is class-wide, insert
7275 -- a check that the level of the return expression's underlying
7276 -- type is not deeper than the level of the master enclosing the
7277 -- function.
7279 -- AI12-043: The check is made immediately after the return object
7280 -- is created.
7282 if Is_Class_Wide_Type (Etype (Func_Id)) then
7283 Apply_CW_Accessibility_Check (Expr, Func_Id);
7284 end if;
7286 Alloc_Expr := New_Copy_Tree (Expr);
7288 if Etype (Alloc_Expr) /= Alloc_Typ then
7289 Alloc_Expr := Convert_To (Alloc_Typ, Alloc_Expr);
7290 end if;
7292 Alloc :=
7293 Make_Allocator (Loc,
7294 Expression =>
7295 Make_Qualified_Expression (Loc,
7296 Subtype_Mark =>
7297 New_Occurrence_Of (Alloc_Typ, Loc),
7298 Expression => Alloc_Expr));
7300 else
7301 Alloc :=
7302 Make_Allocator (Loc,
7303 Expression => New_Occurrence_Of (Alloc_Typ, Loc));
7305 -- If the return object requires default initialization, then it
7306 -- will happen later following the elaboration of the renaming.
7307 -- If we don't turn it off here, then the object will be default
7308 -- initialized twice.
7310 Set_No_Initialization (Alloc);
7311 end if;
7313 -- Set the flag indicating that the allocator is made for a special
7314 -- return object. This is used to bypass various legality checks as
7315 -- well as to make sure that the result is not adjusted twice.
7317 Set_For_Special_Return_Object (Alloc);
7319 return Alloc;
7320 end Make_Allocator_For_Return;
7322 ----------------------
7323 -- OK_To_Rename_Ref --
7324 ----------------------
7326 function OK_To_Rename_Ref (N : Node_Id) return Boolean is
7327 begin
7328 return Is_Entity_Name (N)
7329 and then Ekind (Entity (N)) = E_Variable
7330 and then OK_To_Rename (Entity (N));
7331 end OK_To_Rename_Ref;
7333 -- Local variables
7335 Adj_Call : Node_Id := Empty;
7336 Expr_Q : Node_Id := Empty;
7337 Tag_Assign : Node_Id := Empty;
7339 Init_After : Node_Id := N;
7340 -- Node after which the initialization actions are to be inserted. This
7341 -- is normally N, except for the case of a shared passive variable, in
7342 -- which case the init proc call must be inserted only after the bodies
7343 -- of the shared variable procedures have been seen.
7345 Has_BIP_Init_Expr : Boolean := False;
7346 -- Whether the object is initialized with a BIP function call
7348 Rewrite_As_Renaming : Boolean := False;
7349 -- Whether to turn the declaration into a renaming at the end
7351 -- Start of processing for Expand_N_Object_Declaration
7353 begin
7354 -- Don't do anything for deferred constants. All proper actions will be
7355 -- expanded during the full declaration.
7357 if No (Expr) and Constant_Present (N) then
7358 return;
7359 end if;
7361 -- The type of the object cannot be abstract. This is diagnosed at the
7362 -- point the object is frozen, which happens after the declaration is
7363 -- fully expanded, so simply return now.
7365 if Is_Abstract_Type (Typ) then
7366 return;
7367 end if;
7369 -- No action needed for the internal imported dummy object added by
7370 -- Make_DT to compute the offset of the components that reference
7371 -- secondary dispatch tables; required to avoid never-ending loop
7372 -- processing this internal object declaration.
7374 if Tagged_Type_Expansion
7375 and then Is_Internal (Def_Id)
7376 and then Is_Imported (Def_Id)
7377 and then Related_Type (Def_Id) = Implementation_Base_Type (Typ)
7378 then
7379 return;
7380 end if;
7382 -- Make shared memory routines for shared passive variable
7384 if Is_Shared_Passive (Def_Id) then
7385 Init_After := Make_Shared_Var_Procs (N);
7386 end if;
7388 -- Determine whether the object is initialized with a BIP function call
7390 if Present (Expr) then
7391 Expr_Q := Unqualify (Expr);
7393 Has_BIP_Init_Expr :=
7394 Is_Build_In_Place_Function_Call (Expr_Q)
7395 or else Present (Unqual_BIP_Iface_Function_Call (Expr_Q))
7396 or else (Nkind (Expr_Q) = N_Reference
7397 and then
7398 Is_Build_In_Place_Function_Call (Prefix (Expr_Q)));
7399 end if;
7401 -- If tasks are being declared, make sure we have an activation chain
7402 -- defined for the tasks (has no effect if we already have one), and
7403 -- also that a Master variable is established (and that the appropriate
7404 -- enclosing construct is established as a task master).
7406 if Has_Task (Typ)
7407 or else Might_Have_Tasks (Typ)
7408 or else (Has_BIP_Init_Expr
7409 and then Needs_BIP_Task_Actuals (BIP_Function_Call_Id))
7410 then
7411 Build_Activation_Chain_Entity (N);
7413 if Has_Task (Typ) then
7414 Build_Master_Entity (Def_Id);
7416 -- Handle objects initialized with BIP function calls
7418 elsif Has_BIP_Init_Expr then
7419 Build_Master_Entity (Def_Id);
7420 end if;
7421 end if;
7423 -- If No_Implicit_Heap_Allocations or No_Implicit_Task_Allocations
7424 -- restrictions are active then default-sized secondary stacks are
7425 -- generated by the binder and allocated by SS_Init. To provide the
7426 -- binder the number of stacks to generate, the number of default-sized
7427 -- stacks required for task objects contained within the object
7428 -- declaration N is calculated here as it is at this point where
7429 -- unconstrained types become constrained. The result is stored in the
7430 -- enclosing unit's Unit_Record.
7432 -- Note if N is an array object declaration that has an initialization
7433 -- expression, a second object declaration for the initialization
7434 -- expression is created by the compiler. To prevent double counting
7435 -- of the stacks in this scenario, the stacks of the first array are
7436 -- not counted.
7438 if Might_Have_Tasks (Typ)
7439 and then not Restriction_Active (No_Secondary_Stack)
7440 and then (Restriction_Active (No_Implicit_Heap_Allocations)
7441 or else Restriction_Active (No_Implicit_Task_Allocations))
7442 and then not (Ekind (Typ) in E_Array_Type | E_Array_Subtype
7443 and then Has_Init_Expression (N))
7444 then
7445 declare
7446 PS_Count, SS_Count : Int := 0;
7447 begin
7448 Count_Default_Sized_Task_Stacks (Typ, PS_Count, SS_Count);
7449 Increment_Primary_Stack_Count (PS_Count);
7450 Increment_Sec_Stack_Count (SS_Count);
7451 end;
7452 end if;
7454 -- Default initialization required, and no expression present
7456 if No (Expr) then
7457 -- If we have a type with a variant part, the initialization proc
7458 -- will contain implicit tests of the discriminant values, which
7459 -- counts as a violation of the restriction No_Implicit_Conditionals.
7461 if Has_Variant_Part (Typ) then
7462 declare
7463 Msg : Boolean;
7465 begin
7466 Check_Restriction (Msg, No_Implicit_Conditionals, Obj_Def);
7468 if Msg then
7469 Error_Msg_N
7470 ("\initialization of variant record tests discriminants",
7471 Obj_Def);
7472 return;
7473 end if;
7474 end;
7475 end if;
7477 -- For the default initialization case, if we have a private type
7478 -- with invariants, and invariant checks are enabled, then insert an
7479 -- invariant check after the object declaration. Note that it is OK
7480 -- to clobber the object with an invalid value since if the exception
7481 -- is raised, then the object will go out of scope. In the case where
7482 -- an array object is initialized with an aggregate, the expression
7483 -- is removed. Check flag Has_Init_Expression to avoid generating a
7484 -- junk invariant check and flag No_Initialization to avoid checking
7485 -- an uninitialized object such as a compiler temporary used for an
7486 -- aggregate.
7488 if Has_Invariants (Base_Typ)
7489 and then Present (Invariant_Procedure (Base_Typ))
7490 and then not Has_Init_Expression (N)
7491 and then not No_Initialization (N)
7492 then
7493 -- If entity has an address clause or aspect, make invariant
7494 -- call into a freeze action for the explicit freeze node for
7495 -- object. Otherwise insert invariant check after declaration.
7497 if Present (Following_Address_Clause (N))
7498 or else Has_Aspect (Def_Id, Aspect_Address)
7499 then
7500 Ensure_Freeze_Node (Def_Id);
7501 Set_Has_Delayed_Freeze (Def_Id);
7502 Set_Is_Frozen (Def_Id, False);
7504 if not Partial_View_Has_Unknown_Discr (Typ) then
7505 Append_Freeze_Action (Def_Id,
7506 Make_Invariant_Call (New_Occurrence_Of (Def_Id, Loc)));
7507 end if;
7509 elsif not Partial_View_Has_Unknown_Discr (Typ) then
7510 Insert_After (N,
7511 Make_Invariant_Call (New_Occurrence_Of (Def_Id, Loc)));
7512 end if;
7513 end if;
7515 if not Special_Ret_Obj then
7516 Default_Initialize_Object (Init_After);
7517 end if;
7519 -- Generate attribute for Persistent_BSS if needed
7521 if Persistent_BSS_Mode
7522 and then Comes_From_Source (N)
7523 and then Is_Potentially_Persistent_Type (Typ)
7524 and then not Has_Init_Expression (N)
7525 and then Is_Library_Level_Entity (Def_Id)
7526 then
7527 declare
7528 Prag : Node_Id;
7529 begin
7530 Prag :=
7531 Make_Linker_Section_Pragma
7532 (Def_Id, Sloc (N), ".persistent.bss");
7533 Insert_After (N, Prag);
7534 Analyze (Prag);
7535 end;
7536 end if;
7538 -- If access type, then we know it is null if not initialized
7540 if Is_Access_Type (Typ) then
7541 Set_Is_Known_Null (Def_Id);
7542 end if;
7544 -- Explicit initialization present
7546 else
7547 -- Obtain actual expression from qualified expression
7549 Expr_Q := Unqualify (Expr);
7551 -- When we have the appropriate type of aggregate in the expression
7552 -- (it has been determined during analysis of the aggregate by
7553 -- setting the delay flag), let's perform in place assignment and
7554 -- thus avoid creating a temporary.
7556 if Is_Delayed_Aggregate (Expr_Q) then
7558 -- An aggregate that must be built in place is not resolved and
7559 -- expanded until the enclosing construct is expanded. This will
7560 -- happen when the aggregate is limited and the declared object
7561 -- has a following address clause; it happens also when generating
7562 -- C code for an aggregate that has an alignment or address clause
7563 -- (see Analyze_Object_Declaration). Resolution is done without
7564 -- expansion because it will take place when the declaration
7565 -- itself is expanded.
7567 if (Is_Limited_Type (Typ) or else Modify_Tree_For_C)
7568 and then not Analyzed (Expr)
7569 then
7570 Expander_Mode_Save_And_Set (False);
7571 Resolve (Expr, Typ);
7572 Expander_Mode_Restore;
7573 end if;
7575 if not Special_Ret_Obj then
7576 Convert_Aggr_In_Object_Decl (N);
7577 end if;
7579 -- Ada 2005 (AI-318-02): If the initialization expression is a call
7580 -- to a build-in-place function, then access to the declared object
7581 -- must be passed to the function. Currently we limit such functions
7582 -- to those with constrained limited result subtypes, but eventually
7583 -- plan to expand the allowed forms of functions that are treated as
7584 -- build-in-place.
7586 elsif Is_Build_In_Place_Function_Call (Expr_Q) then
7587 Make_Build_In_Place_Call_In_Object_Declaration (N, Expr_Q);
7589 -- The previous call expands the expression initializing the
7590 -- built-in-place object into further code that will be analyzed
7591 -- later. No further expansion needed here.
7593 return;
7595 -- This is the same as the previous 'elsif', except that the call has
7596 -- been transformed by other expansion activities into something like
7597 -- F(...)'Reference.
7599 elsif Nkind (Expr_Q) = N_Reference
7600 and then Is_Build_In_Place_Function_Call (Prefix (Expr_Q))
7601 and then not Is_Expanded_Build_In_Place_Call
7602 (Unqual_Conv (Prefix (Expr_Q)))
7603 then
7604 Make_Build_In_Place_Call_In_Anonymous_Context (Prefix (Expr_Q));
7606 -- The previous call expands the expression initializing the
7607 -- built-in-place object into further code that will be analyzed
7608 -- later. No further expansion needed here.
7610 return;
7612 -- Ada 2005 (AI-318-02): Specialization of the previous case for
7613 -- expressions containing a build-in-place function call whose
7614 -- returned object covers interface types, and Expr_Q has calls to
7615 -- Ada.Tags.Displace to displace the pointer to the returned build-
7616 -- in-place object to reference the secondary dispatch table of a
7617 -- covered interface type.
7619 elsif Present (Unqual_BIP_Iface_Function_Call (Expr_Q)) then
7620 Make_Build_In_Place_Iface_Call_In_Object_Declaration (N, Expr_Q);
7622 -- The previous call expands the expression initializing the
7623 -- built-in-place object into further code that will be analyzed
7624 -- later. No further expansion needed here.
7626 return;
7628 -- Ada 2005 (AI-251): Rewrite the expression that initializes a
7629 -- class-wide interface object to ensure that we copy the full
7630 -- object, unless we are targetting a VM where interfaces are handled
7631 -- by VM itself. Note that if the root type of Typ is an ancestor of
7632 -- Expr's type, both types share the same dispatch table and there is
7633 -- no need to displace the pointer.
7635 elsif Is_Interface (Typ)
7637 -- Avoid never-ending recursion because if Equivalent_Type is set
7638 -- then we've done it already and must not do it again.
7640 and then not
7641 (Nkind (Obj_Def) = N_Identifier
7642 and then Present (Equivalent_Type (Entity (Obj_Def))))
7643 then
7644 pragma Assert (Is_Class_Wide_Type (Typ));
7646 -- If the original node of the expression was a conversion
7647 -- to this specific class-wide interface type then restore
7648 -- the original node because we must copy the object before
7649 -- displacing the pointer to reference the secondary tag
7650 -- component. This code must be kept synchronized with the
7651 -- expansion done by routine Expand_Interface_Conversion
7653 if not Comes_From_Source (Expr)
7654 and then Nkind (Expr) = N_Explicit_Dereference
7655 and then Nkind (Original_Node (Expr)) = N_Type_Conversion
7656 and then Etype (Original_Node (Expr)) = Typ
7657 then
7658 Rewrite (Expr, Original_Node (Expression (N)));
7659 end if;
7661 -- Avoid expansion of redundant interface conversion
7663 if Nkind (Expr) = N_Type_Conversion
7664 and then Etype (Expr) = Typ
7665 then
7666 Expr_Q := Expression (Expr);
7667 else
7668 Expr_Q := Expr;
7669 end if;
7671 -- We may use a renaming if the initialization expression is a
7672 -- captured function call that meets a few conditions.
7674 Rewrite_As_Renaming := Is_Renamable_Function_Call (Expr_Q);
7676 -- If the object is a special return object, then bypass special
7677 -- treatment of class-wide interface initialization below. In this
7678 -- case, the expansion of the return object will take care of this
7679 -- initialization via the expansion of the allocator.
7681 if Special_Ret_Obj and then not Rewrite_As_Renaming then
7683 -- If the type needs finalization and is not inherently
7684 -- limited, then the target is adjusted after the copy
7685 -- and attached to the finalization list.
7687 if Needs_Finalization (Typ)
7688 and then not Is_Inherently_Limited_Type (Typ)
7689 then
7690 Adj_Call :=
7691 Make_Adjust_Call (
7692 Obj_Ref => New_Occurrence_Of (Def_Id, Loc),
7693 Typ => Base_Typ);
7694 end if;
7696 -- Renaming an expression of the object's type is immediate
7698 elsif Rewrite_As_Renaming
7699 and then Base_Type (Etype (Expr_Q)) = Base_Type (Typ)
7700 then
7701 null;
7703 elsif Tagged_Type_Expansion then
7704 declare
7705 Iface : constant Entity_Id := Root_Type (Typ);
7707 Expr_Typ : Entity_Id;
7708 New_Expr : Node_Id;
7709 Obj_Id : Entity_Id;
7710 Ptr_Obj_Decl : Node_Id;
7711 Ptr_Obj_Id : Entity_Id;
7712 Tag_Comp : Node_Id;
7714 begin
7715 Expr_Typ := Base_Type (Etype (Expr_Q));
7716 if Is_Class_Wide_Type (Expr_Typ) then
7717 Expr_Typ := Root_Type (Expr_Typ);
7718 end if;
7720 -- Rename limited objects since they cannot be copied
7722 if Is_Limited_Record (Expr_Typ) then
7723 Rewrite_As_Renaming := True;
7724 end if;
7726 Obj_Id := Make_Temporary (Loc, 'D', Expr_Q);
7728 -- Replace
7729 -- IW : I'Class := Expr;
7730 -- by
7731 -- Dnn : Tag renames Tag_Ptr!(Expr'Address).all;
7732 -- type Ityp is not null access I'Class;
7733 -- Rnn : constant Ityp :=
7734 -- Ityp!(Displace (Dnn'Address, I'Tag));
7735 -- IW : I'Class renames Rnn.all;
7737 if Rewrite_As_Renaming then
7738 New_Expr :=
7739 Make_Explicit_Dereference (Loc,
7740 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
7741 Make_Attribute_Reference (Loc,
7742 Prefix => Relocate_Node (Expr_Q),
7743 Attribute_Name => Name_Address)));
7745 -- Suppress junk access checks on RE_Tag_Ptr
7747 Insert_Action (N,
7748 Make_Object_Renaming_Declaration (Loc,
7749 Defining_Identifier => Obj_Id,
7750 Subtype_Mark =>
7751 New_Occurrence_Of (RTE (RE_Tag), Loc),
7752 Name => New_Expr),
7753 Suppress => Access_Check);
7755 -- Dynamically reference the tag associated with the
7756 -- interface.
7758 Tag_Comp :=
7759 Make_Function_Call (Loc,
7760 Name => New_Occurrence_Of (RTE (RE_Displace), Loc),
7761 Parameter_Associations => New_List (
7762 Make_Attribute_Reference (Loc,
7763 Prefix => New_Occurrence_Of (Obj_Id, Loc),
7764 Attribute_Name => Name_Address),
7765 New_Occurrence_Of
7766 (Node (First_Elmt (Access_Disp_Table (Iface))),
7767 Loc)));
7769 -- Replace
7770 -- IW : I'Class := Expr;
7771 -- by
7772 -- Dnn : Typ := Expr;
7773 -- type Ityp is not null access I'Class;
7774 -- Rnn : constant Ityp := Ityp (Dnn.I_Tag'Address);
7775 -- IW : I'Class renames Rnn.all;
7777 elsif Has_Tag_Of_Type (Expr_Q)
7778 and then Interface_Present_In_Ancestor (Expr_Typ, Typ)
7779 and then (Expr_Typ = Etype (Expr_Typ)
7780 or else not
7781 Is_Variable_Size_Record (Etype (Expr_Typ)))
7782 then
7783 Insert_Action (N,
7784 Make_Object_Declaration (Loc,
7785 Defining_Identifier => Obj_Id,
7786 Object_Definition =>
7787 New_Occurrence_Of (Expr_Typ, Loc),
7788 Expression => Relocate_Node (Expr_Q)));
7790 -- Statically reference the tag associated with the
7791 -- interface
7793 Tag_Comp :=
7794 Make_Selected_Component (Loc,
7795 Prefix => New_Occurrence_Of (Obj_Id, Loc),
7796 Selector_Name =>
7797 New_Occurrence_Of
7798 (Find_Interface_Tag (Expr_Typ, Iface), Loc));
7800 -- Replace
7801 -- IW : I'Class := Expr;
7802 -- by
7803 -- type Equiv_Record is record ... end record;
7804 -- implicit subtype CW is <Class_Wide_Subtype>;
7805 -- Dnn : CW := CW!(Expr);
7806 -- type Ityp is not null access I'Class;
7807 -- Rnn : constant Ityp :=
7808 -- Ityp!(Displace (Dnn'Address, I'Tag));
7809 -- IW : I'Class renames Rnn.all;
7811 else
7812 -- Generate the equivalent record type and update the
7813 -- subtype indication to reference it.
7815 Expand_Subtype_From_Expr
7816 (N => N,
7817 Unc_Type => Typ,
7818 Subtype_Indic => Obj_Def,
7819 Exp => Expr_Q);
7821 -- For interface types we use 'Address which displaces
7822 -- the pointer to the base of the object (if required).
7824 if Is_Interface (Etype (Expr_Q)) then
7825 New_Expr :=
7826 Unchecked_Convert_To (Etype (Obj_Def),
7827 Make_Explicit_Dereference (Loc,
7828 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
7829 Make_Attribute_Reference (Loc,
7830 Prefix => Relocate_Node (Expr_Q),
7831 Attribute_Name => Name_Address))));
7833 -- For other types, no displacement is needed
7835 else
7836 New_Expr := Relocate_Node (Expr_Q);
7837 end if;
7839 -- Suppress junk access checks on RE_Tag_Ptr
7841 Insert_Action (N,
7842 Make_Object_Declaration (Loc,
7843 Defining_Identifier => Obj_Id,
7844 Object_Definition =>
7845 New_Occurrence_Of (Etype (Obj_Def), Loc),
7846 Expression => New_Expr),
7847 Suppress => Access_Check);
7849 -- Dynamically reference the tag associated with the
7850 -- interface.
7852 Tag_Comp :=
7853 Make_Function_Call (Loc,
7854 Name => New_Occurrence_Of (RTE (RE_Displace), Loc),
7855 Parameter_Associations => New_List (
7856 Make_Attribute_Reference (Loc,
7857 Prefix => New_Occurrence_Of (Obj_Id, Loc),
7858 Attribute_Name => Name_Address),
7859 New_Occurrence_Of
7860 (Node (First_Elmt (Access_Disp_Table (Iface))),
7861 Loc)));
7862 end if;
7864 -- As explained in Exp_Disp, we use Convert_Tag_To_Interface
7865 -- to do the final conversion, but we insert an intermediate
7866 -- temporary before the dereference so that we can process
7867 -- the expansion as part of the analysis of the declaration
7868 -- of this temporary, and then rewrite manually the original
7869 -- object as the simple renaming of this dereference.
7871 Tag_Comp := Convert_Tag_To_Interface (Typ, Tag_Comp);
7872 pragma Assert (Nkind (Tag_Comp) = N_Explicit_Dereference
7873 and then
7874 Nkind (Prefix (Tag_Comp)) = N_Unchecked_Type_Conversion);
7876 Ptr_Obj_Id := Make_Temporary (Loc, 'R');
7878 Ptr_Obj_Decl :=
7879 Make_Object_Declaration (Loc,
7880 Defining_Identifier => Ptr_Obj_Id,
7881 Constant_Present => True,
7882 Object_Definition =>
7883 New_Occurrence_Of
7884 (Entity (Subtype_Mark (Prefix (Tag_Comp))), Loc),
7885 Expression => Prefix (Tag_Comp));
7887 Insert_Action (N, Ptr_Obj_Decl, Suppress => All_Checks);
7889 Set_Prefix (Tag_Comp, New_Occurrence_Of (Ptr_Obj_Id, Loc));
7890 Expr_Q := Tag_Comp;
7891 Set_Etype (Expr_Q, Typ);
7892 Set_Parent (Expr_Q, N);
7894 Rewrite_As_Renaming := True;
7895 end;
7897 else
7898 return;
7899 end if;
7901 -- Common case of explicit object initialization
7903 else
7904 -- Small optimization: if the expression is a function call and
7905 -- the object is stand-alone, not declared at library level and of
7906 -- a class-wide type, then we capture the result of the call into
7907 -- a temporary, with the benefit that, if the result's type does
7908 -- not need finalization, nothing will be finalized and, if it
7909 -- does, the temporary only will be finalized by means of a direct
7910 -- call to the Finalize primitive if the result's type is not a
7911 -- class-wide type; whereas, in both cases, the stand-alone object
7912 -- itself would be finalized by means of a dispatching call to the
7913 -- Deep_Finalize routine.
7915 if Nkind (Expr_Q) = N_Function_Call
7916 and then not Special_Ret_Obj
7917 and then not Is_Library_Level_Entity (Def_Id)
7918 and then Is_Class_Wide_Type (Typ)
7919 then
7920 Remove_Side_Effects (Expr_Q);
7921 end if;
7923 -- In most cases, we must check that the initial value meets any
7924 -- constraint imposed by the declared type. However, there is one
7925 -- very important exception to this rule. If the entity has an
7926 -- unconstrained nominal subtype, then it acquired its constraints
7927 -- from the expression in the first place, and not only does this
7928 -- mean that the constraint check is not needed, but an attempt to
7929 -- perform the constraint check can cause order of elaboration
7930 -- problems.
7932 if not Is_Constr_Subt_For_U_Nominal (Typ) then
7934 -- If this is an allocator for an aggregate that has been
7935 -- allocated in place, delay checks until assignments are
7936 -- made, because the discriminants are not initialized.
7938 if Nkind (Expr) = N_Allocator
7939 and then No_Initialization (Expr)
7940 then
7941 null;
7943 -- Otherwise apply a constraint check now if no prev error
7945 elsif Nkind (Expr) /= N_Error then
7946 Apply_Constraint_Check (Expr, Typ);
7948 -- Deal with possible range check
7950 if Do_Range_Check (Expr) then
7952 -- If assignment checks are suppressed, turn off flag
7954 if Suppress_Assignment_Checks (N) then
7955 Set_Do_Range_Check (Expr, False);
7957 -- Otherwise generate the range check
7959 else
7960 Generate_Range_Check
7961 (Expr, Typ, CE_Range_Check_Failed);
7962 end if;
7963 end if;
7964 end if;
7965 end if;
7967 -- For tagged types, when an init value is given, the tag has to
7968 -- be re-initialized separately in order to avoid the propagation
7969 -- of a wrong tag coming from a view conversion unless the type
7970 -- is class wide (in this case the tag comes from the init value).
7971 -- Suppress the tag assignment when not Tagged_Type_Expansion
7972 -- because tags are represented implicitly in objects. Ditto for
7973 -- types that are CPP_CLASS, and for initializations that are
7974 -- aggregates, because they have to have the right tag.
7976 -- The re-assignment of the tag has to be done even if the object
7977 -- is a constant. The assignment must be analyzed after the
7978 -- declaration. If an address clause follows, this is handled as
7979 -- part of the freeze actions for the object, otherwise insert
7980 -- tag assignment here.
7982 Tag_Assign := Make_Tag_Assignment (N);
7984 if Present (Tag_Assign) then
7985 if Present (Following_Address_Clause (N)) then
7986 Ensure_Freeze_Node (Def_Id);
7987 elsif not Special_Ret_Obj then
7988 Insert_Action_After (Init_After, Tag_Assign);
7989 end if;
7991 -- Handle C++ constructor calls. Note that we do not check that
7992 -- Typ is a tagged type since the equivalent Ada type of a C++
7993 -- class that has no virtual methods is an untagged limited
7994 -- record type.
7996 elsif Is_CPP_Constructor_Call (Expr) then
7997 declare
7998 Id_Ref : constant Node_Id := New_Occurrence_Of (Def_Id, Loc);
8000 begin
8001 -- The call to the initialization procedure does NOT freeze
8002 -- the object being initialized.
8004 Set_Must_Not_Freeze (Id_Ref);
8005 Set_Assignment_OK (Id_Ref);
8007 Insert_Actions_After (Init_After,
8008 Build_Initialization_Call (Loc, Id_Ref, Typ,
8009 Constructor_Ref => Expr));
8011 -- We remove here the original call to the constructor
8012 -- to avoid its management in the backend
8014 Set_Expression (N, Empty);
8015 return;
8016 end;
8018 -- Handle initialization of limited tagged types
8020 elsif Is_Tagged_Type (Typ)
8021 and then Is_Class_Wide_Type (Typ)
8022 and then Is_Limited_Record (Typ)
8023 and then not Is_Limited_Interface (Typ)
8024 then
8025 -- Given that the type is limited we cannot perform a copy. If
8026 -- Expr_Q is the reference to a variable we mark the variable
8027 -- as OK_To_Rename to expand this declaration into a renaming
8028 -- declaration (see below).
8030 if Is_Entity_Name (Expr_Q) then
8031 Set_OK_To_Rename (Entity (Expr_Q));
8033 -- If we cannot convert the expression into a renaming we must
8034 -- consider it an internal error because the backend does not
8035 -- have support to handle it. But avoid crashing on a raise
8036 -- expression or conditional expression.
8038 elsif Nkind (Original_Node (Expr_Q)) not in
8039 N_Raise_Expression | N_If_Expression | N_Case_Expression
8040 then
8041 raise Program_Error;
8042 end if;
8044 -- For discrete types, set the Is_Known_Valid flag if the
8045 -- initializing value is known to be valid. Only do this for
8046 -- source assignments, since otherwise we can end up turning
8047 -- on the known valid flag prematurely from inserted code.
8049 elsif Comes_From_Source (N)
8050 and then Is_Discrete_Type (Typ)
8051 and then Expr_Known_Valid (Expr)
8052 and then Safe_To_Capture_Value (N, Def_Id)
8053 then
8054 Set_Is_Known_Valid (Def_Id);
8056 -- For access types, set the Is_Known_Non_Null flag if the
8057 -- initializing value is known to be non-null. We can also
8058 -- set Can_Never_Be_Null if this is a constant.
8060 elsif Is_Access_Type (Typ) and then Known_Non_Null (Expr) then
8061 Set_Is_Known_Non_Null (Def_Id, True);
8063 if Constant_Present (N) then
8064 Set_Can_Never_Be_Null (Def_Id);
8065 end if;
8066 end if;
8068 -- If validity checking on copies, validate initial expression.
8069 -- But skip this if declaration is for a generic type, since it
8070 -- makes no sense to validate generic types. Not clear if this
8071 -- can happen for legal programs, but it definitely can arise
8072 -- from previous instantiation errors.
8074 if Validity_Checks_On
8075 and then Comes_From_Source (N)
8076 and then Validity_Check_Copies
8077 and then not Is_Generic_Type (Typ)
8078 then
8079 Ensure_Valid (Expr);
8081 if Safe_To_Capture_Value (N, Def_Id) then
8082 Set_Is_Known_Valid (Def_Id);
8083 end if;
8084 end if;
8086 -- Now determine whether we will use a renaming
8088 Rewrite_As_Renaming :=
8090 -- The declaration cannot be rewritten if it has got constraints
8092 Is_Entity_Name (Original_Node (Obj_Def))
8094 -- If we have "X : S := ...;", and S is a constrained array
8095 -- subtype, then we cannot rename, because renamings ignore
8096 -- the constraints of S, so that would change the semantics
8097 -- (sliding would not occur on the initial value). This is
8098 -- only a problem for source objects though, the others have
8099 -- the correct bounds.
8101 and then not (Comes_From_Source (Obj_Def)
8102 and then Is_Array_Type (Typ)
8103 and then Is_Constrained (Typ))
8105 -- Moreover, if we have "X : aliased S := "...;" and S is an
8106 -- unconstrained array type, then we can rename only if the
8107 -- initialization expression has an unconstrained subtype too,
8108 -- because the bounds must be present within X.
8110 and then not (Is_Array_Type (Typ)
8111 and then Is_Constr_Subt_For_UN_Aliased (Typ)
8112 and then Is_Constrained (Etype (Expr_Q)))
8114 -- We may use a renaming if the initialization expression is a
8115 -- captured function call that meets a few conditions.
8117 and then
8118 (Is_Renamable_Function_Call (Expr_Q)
8120 -- Or else if it is a variable with OK_To_Rename set
8122 or else (OK_To_Rename_Ref (Expr_Q)
8123 and then not Special_Ret_Obj)
8125 -- Or else if it is a slice of such a variable
8127 or else (Nkind (Expr_Q) = N_Slice
8128 and then OK_To_Rename_Ref (Prefix (Expr_Q))
8129 and then not Special_Ret_Obj));
8131 -- If the type needs finalization and is not inherently limited,
8132 -- then the target is adjusted after the copy and attached to the
8133 -- finalization list. However, no adjustment is needed in the case
8134 -- where the object has been initialized by a call to a function
8135 -- returning on the primary stack (see Expand_Ctrl_Function_Call)
8136 -- since no copy occurred, given that the type is by-reference.
8137 -- Similarly, no adjustment is needed if we are going to rewrite
8138 -- the object declaration into a renaming declaration.
8140 if Needs_Finalization (Typ)
8141 and then not Is_Inherently_Limited_Type (Typ)
8142 and then Nkind (Expr_Q) /= N_Function_Call
8143 and then not Rewrite_As_Renaming
8144 then
8145 Adj_Call :=
8146 Make_Adjust_Call (
8147 Obj_Ref => New_Occurrence_Of (Def_Id, Loc),
8148 Typ => Base_Typ);
8150 if Present (Adj_Call) and then not Special_Ret_Obj then
8151 Insert_Action_After (Init_After, Adj_Call);
8152 end if;
8153 end if;
8154 end if;
8156 -- Cases where the back end cannot handle the initialization
8157 -- directly. In such cases, we expand an assignment that will
8158 -- be appropriately handled by Expand_N_Assignment_Statement.
8160 -- The exclusion of the unconstrained case is wrong, but for now it
8161 -- is too much trouble ???
8163 if (Is_Possibly_Unaligned_Slice (Expr)
8164 or else (Is_Possibly_Unaligned_Object (Expr)
8165 and then not Represented_As_Scalar (Etype (Expr))))
8166 and then not (Is_Array_Type (Etype (Expr))
8167 and then not Is_Constrained (Etype (Expr)))
8168 then
8169 declare
8170 Stat : constant Node_Id :=
8171 Make_Assignment_Statement (Loc,
8172 Name => New_Occurrence_Of (Def_Id, Loc),
8173 Expression => Relocate_Node (Expr));
8174 begin
8175 Set_Assignment_OK (Name (Stat));
8176 Set_No_Ctrl_Actions (Stat);
8177 Insert_Action_After (Init_After, Stat);
8178 Set_Expression (N, Empty);
8179 Set_No_Initialization (N);
8180 end;
8181 end if;
8182 end if;
8184 if Nkind (Obj_Def) = N_Access_Definition
8185 and then not Is_Local_Anonymous_Access (Typ)
8186 then
8187 -- An Ada 2012 stand-alone object of an anonymous access type
8189 declare
8190 Loc : constant Source_Ptr := Sloc (N);
8192 Level : constant Entity_Id :=
8193 Make_Defining_Identifier (Sloc (N),
8194 Chars =>
8195 New_External_Name (Chars (Def_Id), Suffix => "L"));
8197 Level_Decl : Node_Id;
8198 Level_Expr : Node_Id;
8200 begin
8201 Mutate_Ekind (Level, Ekind (Def_Id));
8202 Set_Etype (Level, Standard_Natural);
8203 Set_Scope (Level, Scope (Def_Id));
8205 -- Set accessibility level of null
8207 if No (Expr) then
8208 Level_Expr :=
8209 Make_Integer_Literal
8210 (Loc, Scope_Depth (Standard_Standard));
8212 -- When the expression of the object is a function which returns
8213 -- an anonymous access type the master of the call is the object
8214 -- being initialized instead of the type.
8216 elsif Nkind (Expr) = N_Function_Call
8217 and then Ekind (Etype (Name (Expr))) = E_Anonymous_Access_Type
8218 then
8219 Level_Expr := Accessibility_Level
8220 (Def_Id, Object_Decl_Level);
8222 -- General case
8224 else
8225 Level_Expr := Accessibility_Level (Expr, Dynamic_Level);
8226 end if;
8228 Level_Decl :=
8229 Make_Object_Declaration (Loc,
8230 Defining_Identifier => Level,
8231 Object_Definition =>
8232 New_Occurrence_Of (Standard_Natural, Loc),
8233 Expression => Level_Expr,
8234 Constant_Present => Constant_Present (N),
8235 Has_Init_Expression => True);
8237 Insert_Action_After (Init_After, Level_Decl);
8239 Set_Extra_Accessibility (Def_Id, Level);
8240 end;
8241 end if;
8243 -- If the object is default initialized and its type is subject to
8244 -- pragma Default_Initial_Condition, add a runtime check to verify
8245 -- the assumption of the pragma (SPARK RM 7.3.3). Generate:
8247 -- <Base_Typ>DIC (<Base_Typ> (Def_Id));
8249 -- Note that the check is generated for source objects only
8251 if Comes_From_Source (Def_Id)
8252 and then Has_DIC (Typ)
8253 and then Present (DIC_Procedure (Typ))
8254 and then not Has_Null_Body (DIC_Procedure (Typ))
8255 and then not Has_Init_Expression (N)
8256 and then No (Expr)
8257 and then not Is_Imported (Def_Id)
8258 then
8259 declare
8260 DIC_Call : constant Node_Id :=
8261 Build_DIC_Call
8262 (Loc, New_Occurrence_Of (Def_Id, Loc), Typ);
8263 begin
8264 if Present (Next_N) then
8265 Insert_Before_And_Analyze (Next_N, DIC_Call);
8267 -- The object declaration is the last node in a declarative or a
8268 -- statement list.
8270 else
8271 Append_To (List_Containing (N), DIC_Call);
8272 Analyze (DIC_Call);
8273 end if;
8274 end;
8275 end if;
8277 -- If this is the return object of a build-in-place function, locate the
8278 -- implicit BIPaccess parameter designating the caller-supplied return
8279 -- object and convert the declaration to a renaming of a dereference of
8280 -- this parameter. If the declaration includes an expression, add an
8281 -- assignment statement to ensure the return object gets initialized.
8283 -- Result : T [:= <expression>];
8285 -- is converted to
8287 -- Result : T renames BIPaccess.all;
8288 -- [Result := <expression>;]
8290 -- in the constrained case, or to
8292 -- type Txx is access all ...;
8293 -- Rxx : Txx := null;
8295 -- if BIPalloc = 1 then
8296 -- Rxx := BIPaccess;
8297 -- Rxx.all := <expression>;
8298 -- elsif BIPalloc = 2 then
8299 -- Rxx := new <expression-type>'(<expression>)[storage_pool =
8300 -- system__secondary_stack__ss_pool][procedure_to_call =
8301 -- system__secondary_stack__ss_allocate];
8302 -- elsif BIPalloc = 3 then
8303 -- Rxx := new <expression-type>'(<expression>)
8304 -- elsif BIPalloc = 4 then
8305 -- Pxx : system__storage_pools__root_storage_pool renames
8306 -- BIPstoragepool.all;
8307 -- Rxx := new <expression-type>'(<expression>)[storage_pool =
8308 -- Pxx][procedure_to_call =
8309 -- system__storage_pools__allocate_any];
8310 -- else
8311 -- [program_error "build in place mismatch"]
8312 -- end if;
8314 -- Result : T renames Rxx.all;
8316 -- in the unconstrained case.
8318 if Is_Build_In_Place_Return_Object (Def_Id) then
8319 declare
8320 Init_Stmt : Node_Id;
8321 Obj_Acc_Formal : Entity_Id;
8323 begin
8324 -- Retrieve the implicit access parameter passed by the caller
8326 Obj_Acc_Formal :=
8327 Build_In_Place_Formal (Func_Id, BIP_Object_Access);
8329 -- If the return object's declaration includes an expression
8330 -- and the declaration isn't marked as No_Initialization, then
8331 -- we need to generate an assignment to the object and insert
8332 -- it after the declaration before rewriting it as a renaming
8333 -- (otherwise we'll lose the initialization). The case where
8334 -- the result type is an interface (or class-wide interface)
8335 -- is also excluded because the context of the function call
8336 -- must be unconstrained, so the initialization will always
8337 -- be done as part of an allocator evaluation (storage pool
8338 -- or secondary stack), never to a constrained target object
8339 -- passed in by the caller. Besides the assignment being
8340 -- unneeded in this case, it avoids problems with trying to
8341 -- generate a dispatching assignment when the return expression
8342 -- is a nonlimited descendant of a limited interface (the
8343 -- interface has no assignment operation).
8345 if Present (Expr_Q)
8346 and then not Is_Delayed_Aggregate (Expr_Q)
8347 and then not No_Initialization (N)
8348 and then not Is_Interface (Typ)
8349 then
8350 if Is_Class_Wide_Type (Typ)
8351 and then not Is_Class_Wide_Type (Etype (Expr_Q))
8352 then
8353 Init_Stmt :=
8354 Make_Assignment_Statement (Loc,
8355 Name => New_Occurrence_Of (Def_Id, Loc),
8356 Expression =>
8357 Make_Type_Conversion (Loc,
8358 Subtype_Mark =>
8359 New_Occurrence_Of (Typ, Loc),
8360 Expression => New_Copy_Tree (Expr_Q)));
8362 else
8363 Init_Stmt :=
8364 Make_Assignment_Statement (Loc,
8365 Name => New_Occurrence_Of (Def_Id, Loc),
8366 Expression => New_Copy_Tree (Expr_Q));
8367 end if;
8369 Set_Assignment_OK (Name (Init_Stmt));
8370 Set_No_Ctrl_Actions (Init_Stmt);
8372 else
8373 Init_Stmt := Empty;
8374 end if;
8376 -- When the function's subtype is unconstrained, a run-time
8377 -- test may be needed to decide the form of allocation to use
8378 -- for the return object. The function has an implicit formal
8379 -- parameter indicating this. If the BIP_Alloc_Form formal has
8380 -- the value one, then the caller has passed access to an
8381 -- existing object for use as the return object. If the value
8382 -- is two, then the return object must be allocated on the
8383 -- secondary stack. If the value is three, then the return
8384 -- object must be allocated on the heap. Otherwise, the object
8385 -- must be allocated in a storage pool. We generate an if
8386 -- statement to test the BIP_Alloc_Form formal and initialize
8387 -- a local access value appropriately.
8389 if Needs_BIP_Alloc_Form (Func_Id) then
8390 declare
8391 Desig_Typ : constant Entity_Id :=
8392 (if Ekind (Typ) = E_Array_Subtype
8393 then Etype (Func_Id) else Typ);
8394 -- Ensure that the we use a fat pointer when allocating
8395 -- an unconstrained array on the heap. In this case the
8396 -- result object's type is a constrained array type even
8397 -- though the function's type is unconstrained.
8399 Obj_Alloc_Formal : constant Entity_Id :=
8400 Build_In_Place_Formal (Func_Id, BIP_Alloc_Form);
8401 Pool_Id : constant Entity_Id :=
8402 Make_Temporary (Loc, 'P');
8404 Acc_Typ : Entity_Id;
8405 Alloc_Obj_Decl : Node_Id;
8406 Alloc_Obj_Id : Entity_Id;
8407 Alloc_Stmt : Node_Id;
8408 Guard_Except : Node_Id;
8409 Heap_Allocator : Node_Id;
8410 Pool_Allocator : Node_Id;
8411 Pool_Decl : Node_Id;
8412 Ptr_Typ_Decl : Node_Id;
8413 SS_Allocator : Node_Id;
8415 begin
8416 -- Create an access type designating the function's
8417 -- result subtype.
8419 Acc_Typ := Make_Temporary (Loc, 'A');
8421 Ptr_Typ_Decl :=
8422 Make_Full_Type_Declaration (Loc,
8423 Defining_Identifier => Acc_Typ,
8424 Type_Definition =>
8425 Make_Access_To_Object_Definition (Loc,
8426 All_Present => True,
8427 Subtype_Indication =>
8428 New_Occurrence_Of (Desig_Typ, Loc)));
8430 Insert_Action (N, Ptr_Typ_Decl, Suppress => All_Checks);
8432 -- Create an access object that will be initialized to an
8433 -- access value denoting the return object, either coming
8434 -- from an implicit access value passed in by the caller
8435 -- or from the result of an allocator.
8437 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
8439 Alloc_Obj_Decl :=
8440 Make_Object_Declaration (Loc,
8441 Defining_Identifier => Alloc_Obj_Id,
8442 Object_Definition =>
8443 New_Occurrence_Of (Acc_Typ, Loc));
8445 Insert_Action (N, Alloc_Obj_Decl, Suppress => All_Checks);
8447 -- First create the Heap_Allocator
8449 Heap_Allocator := Make_Allocator_For_Return (Expr_Q);
8451 -- The Pool_Allocator is just like the Heap_Allocator,
8452 -- except we set Storage_Pool and Procedure_To_Call so
8453 -- it will use the user-defined storage pool.
8455 Pool_Allocator := Make_Allocator_For_Return (Expr_Q);
8457 -- Do not generate the renaming of the build-in-place
8458 -- pool parameter on ZFP because the parameter is not
8459 -- created in the first place.
8461 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
8462 Pool_Decl :=
8463 Make_Object_Renaming_Declaration (Loc,
8464 Defining_Identifier => Pool_Id,
8465 Subtype_Mark =>
8466 New_Occurrence_Of
8467 (RTE (RE_Root_Storage_Pool), Loc),
8468 Name =>
8469 Make_Explicit_Dereference (Loc,
8470 New_Occurrence_Of
8471 (Build_In_Place_Formal
8472 (Func_Id, BIP_Storage_Pool), Loc)));
8473 Set_Storage_Pool (Pool_Allocator, Pool_Id);
8474 Set_Procedure_To_Call
8475 (Pool_Allocator, RTE (RE_Allocate_Any));
8476 else
8477 Pool_Decl := Make_Null_Statement (Loc);
8478 end if;
8480 -- If the No_Allocators restriction is active, then only
8481 -- an allocator for secondary stack allocation is needed.
8482 -- It's OK for such allocators to have Comes_From_Source
8483 -- set to False, because gigi knows not to flag them as
8484 -- being a violation of No_Implicit_Heap_Allocations.
8486 if Restriction_Active (No_Allocators) then
8487 SS_Allocator := Heap_Allocator;
8488 Heap_Allocator := Make_Null (Loc);
8489 Pool_Allocator := Make_Null (Loc);
8491 -- Otherwise the heap and pool allocators may be needed,
8492 -- so we make another allocator for secondary stack
8493 -- allocation.
8495 else
8496 SS_Allocator := Make_Allocator_For_Return (Expr_Q);
8498 -- The heap and pool allocators are marked as
8499 -- Comes_From_Source since they correspond to an
8500 -- explicit user-written allocator (that is, it will
8501 -- only be executed on behalf of callers that call the
8502 -- function as initialization for such an allocator).
8503 -- Prevents errors when No_Implicit_Heap_Allocations
8504 -- is in force.
8506 Set_Comes_From_Source (Heap_Allocator, True);
8507 Set_Comes_From_Source (Pool_Allocator, True);
8508 end if;
8510 -- The allocator is returned on the secondary stack
8512 Check_Restriction (No_Secondary_Stack, N);
8513 Set_Storage_Pool (SS_Allocator, RTE (RE_SS_Pool));
8514 Set_Procedure_To_Call
8515 (SS_Allocator, RTE (RE_SS_Allocate));
8517 -- The allocator is returned on the secondary stack,
8518 -- so indicate that the function return, as well as
8519 -- all blocks that encloses the allocator, must not
8520 -- release it. The flags must be set now because
8521 -- the decision to use the secondary stack is done
8522 -- very late in the course of expanding the return
8523 -- statement, past the point where these flags are
8524 -- normally set.
8526 Set_Uses_Sec_Stack (Func_Id);
8527 Set_Uses_Sec_Stack (Scope (Def_Id));
8528 Set_Sec_Stack_Needed_For_Return (Scope (Def_Id));
8530 -- Guard against poor expansion on the caller side by
8531 -- using a raise statement to catch out-of-range values
8532 -- of formal parameter BIP_Alloc_Form.
8534 if Exceptions_OK then
8535 Guard_Except :=
8536 Make_Raise_Program_Error (Loc,
8537 Reason => PE_Build_In_Place_Mismatch);
8538 else
8539 Guard_Except := Make_Null_Statement (Loc);
8540 end if;
8542 -- Create an if statement to test the BIP_Alloc_Form
8543 -- formal and initialize the access object to either the
8544 -- BIP_Object_Access formal (BIP_Alloc_Form =
8545 -- Caller_Allocation), the result of allocating the
8546 -- object in the secondary stack (BIP_Alloc_Form =
8547 -- Secondary_Stack), or else an allocator to create the
8548 -- return object in the heap or user-defined pool
8549 -- (BIP_Alloc_Form = Global_Heap or User_Storage_Pool).
8551 -- ??? An unchecked type conversion must be made in the
8552 -- case of assigning the access object formal to the
8553 -- local access object, because a normal conversion would
8554 -- be illegal in some cases (such as converting access-
8555 -- to-unconstrained to access-to-constrained), but the
8556 -- the unchecked conversion will presumably fail to work
8557 -- right in just such cases. It's not clear at all how to
8558 -- handle this.
8560 Alloc_Stmt :=
8561 Make_If_Statement (Loc,
8562 Condition =>
8563 Make_Op_Eq (Loc,
8564 Left_Opnd =>
8565 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
8566 Right_Opnd =>
8567 Make_Integer_Literal (Loc,
8568 UI_From_Int (BIP_Allocation_Form'Pos
8569 (Caller_Allocation)))),
8571 Then_Statements => New_List (
8572 Make_Assignment_Statement (Loc,
8573 Name =>
8574 New_Occurrence_Of (Alloc_Obj_Id, Loc),
8575 Expression =>
8576 Unchecked_Convert_To
8577 (Acc_Typ,
8578 New_Occurrence_Of (Obj_Acc_Formal, Loc)))),
8580 Elsif_Parts => New_List (
8581 Make_Elsif_Part (Loc,
8582 Condition =>
8583 Make_Op_Eq (Loc,
8584 Left_Opnd =>
8585 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
8586 Right_Opnd =>
8587 Make_Integer_Literal (Loc,
8588 UI_From_Int (BIP_Allocation_Form'Pos
8589 (Secondary_Stack)))),
8591 Then_Statements => New_List (
8592 Make_Assignment_Statement (Loc,
8593 Name =>
8594 New_Occurrence_Of (Alloc_Obj_Id, Loc),
8595 Expression => SS_Allocator))),
8597 Make_Elsif_Part (Loc,
8598 Condition =>
8599 Make_Op_Eq (Loc,
8600 Left_Opnd =>
8601 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
8602 Right_Opnd =>
8603 Make_Integer_Literal (Loc,
8604 UI_From_Int (BIP_Allocation_Form'Pos
8605 (Global_Heap)))),
8607 Then_Statements => New_List (
8608 Build_Heap_Or_Pool_Allocator
8609 (Temp_Id => Alloc_Obj_Id,
8610 Temp_Typ => Acc_Typ,
8611 Ret_Typ => Desig_Typ,
8612 Alloc_Expr => Heap_Allocator))),
8614 -- ??? If all is well, we can put the following
8615 -- 'elsif' in the 'else', but this is a useful
8616 -- self-check in case caller and callee don't agree
8617 -- on whether BIPAlloc and so on should be passed.
8619 Make_Elsif_Part (Loc,
8620 Condition =>
8621 Make_Op_Eq (Loc,
8622 Left_Opnd =>
8623 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
8624 Right_Opnd =>
8625 Make_Integer_Literal (Loc,
8626 UI_From_Int (BIP_Allocation_Form'Pos
8627 (User_Storage_Pool)))),
8629 Then_Statements => New_List (
8630 Pool_Decl,
8631 Build_Heap_Or_Pool_Allocator
8632 (Temp_Id => Alloc_Obj_Id,
8633 Temp_Typ => Acc_Typ,
8634 Ret_Typ => Desig_Typ,
8635 Alloc_Expr => Pool_Allocator)))),
8637 -- Raise Program_Error if it's none of the above;
8638 -- this is a compiler bug.
8640 Else_Statements => New_List (Guard_Except));
8642 -- If a separate initialization assignment was created
8643 -- earlier, append that following the assignment of the
8644 -- implicit access formal to the access object, to ensure
8645 -- that the return object is initialized in that case. In
8646 -- this situation, the target of the assignment must be
8647 -- rewritten to denote a dereference of the access to the
8648 -- return object passed in by the caller.
8650 if Present (Init_Stmt) then
8651 Set_Name (Init_Stmt,
8652 Make_Explicit_Dereference (Loc,
8653 Prefix => New_Occurrence_Of (Alloc_Obj_Id, Loc)));
8654 Set_Assignment_OK (Name (Init_Stmt));
8656 Append_To (Then_Statements (Alloc_Stmt), Init_Stmt);
8657 Init_Stmt := Empty;
8658 end if;
8660 Insert_Action (N, Alloc_Stmt, Suppress => All_Checks);
8662 -- From now on, the type of the return object is the
8663 -- designated type.
8665 if Desig_Typ /= Typ then
8666 Set_Etype (Def_Id, Desig_Typ);
8667 Set_Actual_Subtype (Def_Id, Typ);
8668 end if;
8670 -- Remember the local access object for use in the
8671 -- dereference of the renaming created below.
8673 Obj_Acc_Formal := Alloc_Obj_Id;
8674 end;
8676 -- When the function's type is unconstrained and a run-time test
8677 -- is not needed, we nevertheless need to build the return using
8678 -- the return object's type.
8680 elsif not Is_Constrained (Underlying_Type (Etype (Func_Id))) then
8681 declare
8682 Acc_Typ : Entity_Id;
8683 Alloc_Obj_Decl : Node_Id;
8684 Alloc_Obj_Id : Entity_Id;
8685 Ptr_Typ_Decl : Node_Id;
8687 begin
8688 -- Create an access type designating the function's
8689 -- result subtype.
8691 Acc_Typ := Make_Temporary (Loc, 'A');
8693 Ptr_Typ_Decl :=
8694 Make_Full_Type_Declaration (Loc,
8695 Defining_Identifier => Acc_Typ,
8696 Type_Definition =>
8697 Make_Access_To_Object_Definition (Loc,
8698 All_Present => True,
8699 Subtype_Indication =>
8700 New_Occurrence_Of (Typ, Loc)));
8702 Insert_Action (N, Ptr_Typ_Decl, Suppress => All_Checks);
8704 -- Create an access object initialized to the conversion
8705 -- of the implicit access value passed in by the caller.
8707 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
8709 -- See the ??? comment a few lines above about the use of
8710 -- an unchecked conversion here.
8712 Alloc_Obj_Decl :=
8713 Make_Object_Declaration (Loc,
8714 Defining_Identifier => Alloc_Obj_Id,
8715 Constant_Present => True,
8716 Object_Definition =>
8717 New_Occurrence_Of (Acc_Typ, Loc),
8718 Expression =>
8719 Unchecked_Convert_To
8720 (Acc_Typ, New_Occurrence_Of (Obj_Acc_Formal, Loc)));
8722 Insert_Action (N, Alloc_Obj_Decl, Suppress => All_Checks);
8724 -- Remember the local access object for use in the
8725 -- dereference of the renaming created below.
8727 Obj_Acc_Formal := Alloc_Obj_Id;
8728 end;
8729 end if;
8731 -- Initialize the object now that it has got its final subtype,
8732 -- but before rewriting it as a renaming.
8734 Initialize_Return_Object
8735 (Tag_Assign, Adj_Call, Expr_Q, Init_Stmt, Init_After);
8737 -- Replace the return object declaration with a renaming of a
8738 -- dereference of the access value designating the return object.
8740 Expr_Q :=
8741 Make_Explicit_Dereference (Loc,
8742 Prefix => New_Occurrence_Of (Obj_Acc_Formal, Loc));
8743 Set_Etype (Expr_Q, Etype (Def_Id));
8745 Rewrite_As_Renaming := True;
8746 end;
8748 -- If we can rename the initialization expression, we need to make sure
8749 -- that we use the proper type in the case of a return object that lives
8750 -- on the secondary stack (see other cases below for a similar handling)
8751 -- and that the tag is assigned in the case of any return object.
8753 elsif Rewrite_As_Renaming then
8754 if Special_Ret_Obj then
8755 declare
8756 Desig_Typ : constant Entity_Id :=
8757 (if Ekind (Typ) = E_Array_Subtype
8758 then Etype (Func_Id) else Typ);
8760 begin
8761 -- From now on, the type of the return object is the
8762 -- designated type.
8764 if Desig_Typ /= Typ then
8765 Set_Etype (Def_Id, Desig_Typ);
8766 Set_Actual_Subtype (Def_Id, Typ);
8767 end if;
8769 if Present (Tag_Assign) then
8770 Insert_Action_After (Init_After, Tag_Assign);
8771 end if;
8773 -- Ada 2005 (AI95-344): If the result type is class-wide,
8774 -- insert a check that the level of the return expression's
8775 -- underlying type is not deeper than the level of the master
8776 -- enclosing the function.
8778 -- AI12-043: The check is made immediately after the return
8779 -- object is created.
8781 if Is_Class_Wide_Type (Etype (Func_Id)) then
8782 Apply_CW_Accessibility_Check (Expr_Q, Func_Id);
8783 end if;
8784 end;
8785 end if;
8787 -- If this is the return object of a function returning on the secondary
8788 -- stack, convert the declaration to a renaming of the dereference of ah
8789 -- allocator for the secondary stack.
8791 -- Result : T [:= <expression>];
8793 -- is converted to
8795 -- type Txx is access all ...;
8796 -- Rxx : constant Txx :=
8797 -- new <expression-type>['(<expression>)][storage_pool =
8798 -- system__secondary_stack__ss_pool][procedure_to_call =
8799 -- system__secondary_stack__ss_allocate];
8801 -- Result : T renames Rxx.all;
8803 elsif Is_Secondary_Stack_Return_Object (Def_Id) then
8804 declare
8805 Desig_Typ : constant Entity_Id :=
8806 (if Ekind (Typ) = E_Array_Subtype
8807 then Etype (Func_Id) else Typ);
8808 -- Ensure that the we use a fat pointer when allocating
8809 -- an unconstrained array on the heap. In this case the
8810 -- result object's type is a constrained array type even
8811 -- though the function's type is unconstrained.
8813 Acc_Typ : Entity_Id;
8814 Alloc_Obj_Decl : Node_Id;
8815 Alloc_Obj_Id : Entity_Id;
8816 Ptr_Type_Decl : Node_Id;
8818 begin
8819 -- Create an access type designating the function's
8820 -- result subtype.
8822 Acc_Typ := Make_Temporary (Loc, 'A');
8824 Ptr_Type_Decl :=
8825 Make_Full_Type_Declaration (Loc,
8826 Defining_Identifier => Acc_Typ,
8827 Type_Definition =>
8828 Make_Access_To_Object_Definition (Loc,
8829 All_Present => True,
8830 Subtype_Indication =>
8831 New_Occurrence_Of (Desig_Typ, Loc)));
8833 Insert_Action (N, Ptr_Type_Decl, Suppress => All_Checks);
8835 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
8837 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
8839 Alloc_Obj_Decl :=
8840 Make_Object_Declaration (Loc,
8841 Defining_Identifier => Alloc_Obj_Id,
8842 Constant_Present => True,
8843 Object_Definition =>
8844 New_Occurrence_Of (Acc_Typ, Loc),
8845 Expression => Make_Allocator_For_Return (Expr_Q));
8847 Insert_Action (N, Alloc_Obj_Decl, Suppress => All_Checks);
8849 Set_Uses_Sec_Stack (Func_Id);
8850 Set_Uses_Sec_Stack (Scope (Def_Id));
8851 Set_Sec_Stack_Needed_For_Return (Scope (Def_Id));
8853 -- From now on, the type of the return object is the
8854 -- designated type.
8856 if Desig_Typ /= Typ then
8857 Set_Etype (Def_Id, Desig_Typ);
8858 Set_Actual_Subtype (Def_Id, Typ);
8859 end if;
8861 -- Initialize the object now that it has got its final subtype,
8862 -- but before rewriting it as a renaming.
8864 Initialize_Return_Object
8865 (Tag_Assign, Adj_Call, Expr_Q, Empty, Init_After);
8867 -- Replace the return object declaration with a renaming of a
8868 -- dereference of the access value designating the return object.
8870 Expr_Q :=
8871 Make_Explicit_Dereference (Loc,
8872 Prefix => New_Occurrence_Of (Alloc_Obj_Id, Loc));
8873 Set_Etype (Expr_Q, Etype (Def_Id));
8875 Rewrite_As_Renaming := True;
8876 end;
8878 -- If this is the return object of a function returning a by-reference
8879 -- type, convert the declaration to a renaming of the dereference of ah
8880 -- allocator for the return stack.
8882 -- Result : T [:= <expression>];
8884 -- is converted to
8886 -- type Txx is access all ...;
8887 -- Rxx : constant Txx :=
8888 -- new <expression-type>['(<expression>)][storage_pool =
8889 -- system__return_stack__rs_pool][procedure_to_call =
8890 -- system__return_stack__rs_allocate];
8892 -- Result : T renames Rxx.all;
8894 elsif Back_End_Return_Slot
8895 and then Is_By_Reference_Return_Object (Def_Id)
8896 then
8897 declare
8898 Acc_Typ : Entity_Id;
8899 Alloc_Obj_Decl : Node_Id;
8900 Alloc_Obj_Id : Entity_Id;
8901 Ptr_Type_Decl : Node_Id;
8903 begin
8904 -- Create an access type designating the function's
8905 -- result subtype.
8907 Acc_Typ := Make_Temporary (Loc, 'A');
8909 Ptr_Type_Decl :=
8910 Make_Full_Type_Declaration (Loc,
8911 Defining_Identifier => Acc_Typ,
8912 Type_Definition =>
8913 Make_Access_To_Object_Definition (Loc,
8914 All_Present => True,
8915 Subtype_Indication =>
8916 New_Occurrence_Of (Typ, Loc)));
8918 Insert_Action (N, Ptr_Type_Decl, Suppress => All_Checks);
8920 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_RS_Pool));
8922 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
8924 Alloc_Obj_Decl :=
8925 Make_Object_Declaration (Loc,
8926 Defining_Identifier => Alloc_Obj_Id,
8927 Constant_Present => True,
8928 Object_Definition =>
8929 New_Occurrence_Of (Acc_Typ, Loc),
8930 Expression => Make_Allocator_For_Return (Expr_Q));
8932 Insert_Action (N, Alloc_Obj_Decl, Suppress => All_Checks);
8934 -- Initialize the object now that it has got its final subtype,
8935 -- but before rewriting it as a renaming.
8937 Initialize_Return_Object
8938 (Tag_Assign, Adj_Call, Expr_Q, Empty, Init_After);
8940 -- Replace the return object declaration with a renaming of a
8941 -- dereference of the access value designating the return object.
8943 Expr_Q :=
8944 Make_Explicit_Dereference (Loc,
8945 Prefix => New_Occurrence_Of (Alloc_Obj_Id, Loc));
8946 Set_Etype (Expr_Q, Etype (Def_Id));
8948 Rewrite_As_Renaming := True;
8949 end;
8950 end if;
8952 -- Final transformation - turn the object declaration into a renaming
8953 -- if appropriate. If this is the completion of a deferred constant
8954 -- declaration, then this transformation generates what would be
8955 -- illegal code if written by hand, but that's OK.
8957 if Rewrite_As_Renaming then
8958 Rewrite (N,
8959 Make_Object_Renaming_Declaration (Loc,
8960 Defining_Identifier => Def_Id,
8961 Subtype_Mark => New_Occurrence_Of (Etype (Def_Id), Loc),
8962 Name => Expr_Q));
8964 -- Keep original aspects
8966 Move_Aspects (Original_Node (N), N);
8968 -- We do not analyze this renaming declaration, because all its
8969 -- components have already been analyzed, and if we were to go
8970 -- ahead and analyze it, we would in effect be trying to generate
8971 -- another declaration of X, which won't do.
8973 Set_Renamed_Object (Def_Id, Expr_Q);
8974 Set_Analyzed (N);
8976 -- We do need to deal with debug issues for this renaming
8978 -- First, if entity comes from source, then mark it as needing
8979 -- debug information, even though it is defined by a generated
8980 -- renaming that does not come from source.
8982 Set_Debug_Info_Defining_Id (N);
8984 -- Now call the routine to generate debug info for the renaming
8986 Insert_Action (N, Debug_Renaming_Declaration (N));
8987 end if;
8989 -- Exception on library entity not available
8991 exception
8992 when RE_Not_Available =>
8993 return;
8994 end Expand_N_Object_Declaration;
8996 ---------------------------------
8997 -- Expand_N_Subtype_Indication --
8998 ---------------------------------
9000 -- Add a check on the range of the subtype and deal with validity checking
9002 procedure Expand_N_Subtype_Indication (N : Node_Id) is
9003 Ran : constant Node_Id := Range_Expression (Constraint (N));
9004 Typ : constant Entity_Id := Entity (Subtype_Mark (N));
9006 begin
9007 if Nkind (Constraint (N)) = N_Range_Constraint then
9008 Validity_Check_Range (Range_Expression (Constraint (N)));
9009 end if;
9011 -- Do not duplicate the work of Process_Range_Expr_In_Decl in Sem_Ch3
9013 if Nkind (Parent (N)) in N_Constrained_Array_Definition | N_Slice
9014 and then Nkind (Parent (Parent (N))) not in
9015 N_Full_Type_Declaration | N_Object_Declaration
9016 then
9017 Apply_Range_Check (Ran, Typ);
9018 end if;
9019 end Expand_N_Subtype_Indication;
9021 ---------------------------
9022 -- Expand_N_Variant_Part --
9023 ---------------------------
9025 -- Note: this procedure no longer has any effect. It used to be that we
9026 -- would replace the choices in the last variant by a when others, and
9027 -- also expanded static predicates in variant choices here, but both of
9028 -- those activities were being done too early, since we can't check the
9029 -- choices until the statically predicated subtypes are frozen, which can
9030 -- happen as late as the free point of the record, and we can't change the
9031 -- last choice to an others before checking the choices, which is now done
9032 -- at the freeze point of the record.
9034 procedure Expand_N_Variant_Part (N : Node_Id) is
9035 begin
9036 null;
9037 end Expand_N_Variant_Part;
9039 ---------------------------------
9040 -- Expand_Previous_Access_Type --
9041 ---------------------------------
9043 procedure Expand_Previous_Access_Type (Def_Id : Entity_Id) is
9044 Ptr_Typ : Entity_Id;
9046 begin
9047 -- Find all access types in the current scope whose designated type is
9048 -- Def_Id and build master renamings for them.
9050 Ptr_Typ := First_Entity (Current_Scope);
9051 while Present (Ptr_Typ) loop
9052 if Is_Access_Type (Ptr_Typ)
9053 and then Designated_Type (Ptr_Typ) = Def_Id
9054 and then No (Master_Id (Ptr_Typ))
9055 then
9056 -- Ensure that the designated type has a master
9058 Build_Master_Entity (Def_Id);
9060 -- Private and incomplete types complicate the insertion of master
9061 -- renamings because the access type may precede the full view of
9062 -- the designated type. For this reason, the master renamings are
9063 -- inserted relative to the designated type.
9065 Build_Master_Renaming (Ptr_Typ, Ins_Nod => Parent (Def_Id));
9066 end if;
9068 Next_Entity (Ptr_Typ);
9069 end loop;
9070 end Expand_Previous_Access_Type;
9072 -----------------------------
9073 -- Expand_Record_Extension --
9074 -----------------------------
9076 -- Add a field _parent at the beginning of the record extension. This is
9077 -- used to implement inheritance. Here are some examples of expansion:
9079 -- 1. no discriminants
9080 -- type T2 is new T1 with null record;
9081 -- gives
9082 -- type T2 is new T1 with record
9083 -- _Parent : T1;
9084 -- end record;
9086 -- 2. renamed discriminants
9087 -- type T2 (B, C : Int) is new T1 (A => B) with record
9088 -- _Parent : T1 (A => B);
9089 -- D : Int;
9090 -- end;
9092 -- 3. inherited discriminants
9093 -- type T2 is new T1 with record -- discriminant A inherited
9094 -- _Parent : T1 (A);
9095 -- D : Int;
9096 -- end;
9098 procedure Expand_Record_Extension (T : Entity_Id; Def : Node_Id) is
9099 Indic : constant Node_Id := Subtype_Indication (Def);
9100 Loc : constant Source_Ptr := Sloc (Def);
9101 Rec_Ext_Part : Node_Id := Record_Extension_Part (Def);
9102 Par_Subtype : Entity_Id;
9103 Comp_List : Node_Id;
9104 Comp_Decl : Node_Id;
9105 Parent_N : Node_Id;
9106 D : Entity_Id;
9107 List_Constr : constant List_Id := New_List;
9109 begin
9110 -- Expand_Record_Extension is called directly from the semantics, so
9111 -- we must check to see whether expansion is active before proceeding,
9112 -- because this affects the visibility of selected components in bodies
9113 -- of instances. Within a generic we still need to set Parent_Subtype
9114 -- link because the visibility of inherited components will have to be
9115 -- verified in subsequent instances.
9117 if not Expander_Active then
9118 if Inside_A_Generic and then Ekind (T) = E_Record_Type then
9119 Set_Parent_Subtype (T, Etype (T));
9120 end if;
9121 return;
9122 end if;
9124 -- This may be a derivation of an untagged private type whose full
9125 -- view is tagged, in which case the Derived_Type_Definition has no
9126 -- extension part. Build an empty one now.
9128 if No (Rec_Ext_Part) then
9129 Rec_Ext_Part :=
9130 Make_Record_Definition (Loc,
9131 End_Label => Empty,
9132 Component_List => Empty,
9133 Null_Present => True);
9135 Set_Record_Extension_Part (Def, Rec_Ext_Part);
9136 Mark_Rewrite_Insertion (Rec_Ext_Part);
9137 end if;
9139 Comp_List := Component_List (Rec_Ext_Part);
9141 Parent_N := Make_Defining_Identifier (Loc, Name_uParent);
9143 -- If the derived type inherits its discriminants the type of the
9144 -- _parent field must be constrained by the inherited discriminants
9146 if Has_Discriminants (T)
9147 and then Nkind (Indic) /= N_Subtype_Indication
9148 and then not Is_Constrained (Entity (Indic))
9149 then
9150 D := First_Discriminant (T);
9151 while Present (D) loop
9152 Append_To (List_Constr, New_Occurrence_Of (D, Loc));
9153 Next_Discriminant (D);
9154 end loop;
9156 Par_Subtype :=
9157 Process_Subtype (
9158 Make_Subtype_Indication (Loc,
9159 Subtype_Mark => New_Occurrence_Of (Entity (Indic), Loc),
9160 Constraint =>
9161 Make_Index_Or_Discriminant_Constraint (Loc,
9162 Constraints => List_Constr)),
9163 Def);
9165 -- Otherwise the original subtype_indication is just what is needed
9167 else
9168 Par_Subtype := Process_Subtype (New_Copy_Tree (Indic), Def);
9169 end if;
9171 Set_Parent_Subtype (T, Par_Subtype);
9173 Comp_Decl :=
9174 Make_Component_Declaration (Loc,
9175 Defining_Identifier => Parent_N,
9176 Component_Definition =>
9177 Make_Component_Definition (Loc,
9178 Aliased_Present => False,
9179 Subtype_Indication => New_Occurrence_Of (Par_Subtype, Loc)));
9181 if Null_Present (Rec_Ext_Part) then
9182 Set_Component_List (Rec_Ext_Part,
9183 Make_Component_List (Loc,
9184 Component_Items => New_List (Comp_Decl),
9185 Variant_Part => Empty,
9186 Null_Present => False));
9187 Set_Null_Present (Rec_Ext_Part, False);
9189 elsif Null_Present (Comp_List)
9190 or else Is_Empty_List (Component_Items (Comp_List))
9191 then
9192 Set_Component_Items (Comp_List, New_List (Comp_Decl));
9193 Set_Null_Present (Comp_List, False);
9195 else
9196 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
9197 end if;
9199 Analyze (Comp_Decl);
9200 end Expand_Record_Extension;
9202 ------------------------
9203 -- Expand_Tagged_Root --
9204 ------------------------
9206 procedure Expand_Tagged_Root (T : Entity_Id) is
9207 Def : constant Node_Id := Type_Definition (Parent (T));
9208 Comp_List : Node_Id;
9209 Comp_Decl : Node_Id;
9210 Sloc_N : Source_Ptr;
9212 begin
9213 if Null_Present (Def) then
9214 Set_Component_List (Def,
9215 Make_Component_List (Sloc (Def),
9216 Component_Items => Empty_List,
9217 Variant_Part => Empty,
9218 Null_Present => True));
9219 end if;
9221 Comp_List := Component_List (Def);
9223 if Null_Present (Comp_List)
9224 or else Is_Empty_List (Component_Items (Comp_List))
9225 then
9226 Sloc_N := Sloc (Comp_List);
9227 else
9228 Sloc_N := Sloc (First (Component_Items (Comp_List)));
9229 end if;
9231 Comp_Decl :=
9232 Make_Component_Declaration (Sloc_N,
9233 Defining_Identifier => First_Tag_Component (T),
9234 Component_Definition =>
9235 Make_Component_Definition (Sloc_N,
9236 Aliased_Present => False,
9237 Subtype_Indication => New_Occurrence_Of (RTE (RE_Tag), Sloc_N)));
9239 if Null_Present (Comp_List)
9240 or else Is_Empty_List (Component_Items (Comp_List))
9241 then
9242 Set_Component_Items (Comp_List, New_List (Comp_Decl));
9243 Set_Null_Present (Comp_List, False);
9245 else
9246 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
9247 end if;
9249 -- We don't Analyze the whole expansion because the tag component has
9250 -- already been analyzed previously. Here we just insure that the tree
9251 -- is coherent with the semantic decoration
9253 Find_Type (Subtype_Indication (Component_Definition (Comp_Decl)));
9255 exception
9256 when RE_Not_Available =>
9257 return;
9258 end Expand_Tagged_Root;
9260 ------------------------------
9261 -- Freeze_Stream_Operations --
9262 ------------------------------
9264 procedure Freeze_Stream_Operations (N : Node_Id; Typ : Entity_Id) is
9265 Names : constant array (1 .. 4) of TSS_Name_Type :=
9266 (TSS_Stream_Input,
9267 TSS_Stream_Output,
9268 TSS_Stream_Read,
9269 TSS_Stream_Write);
9270 Stream_Op : Entity_Id;
9272 begin
9273 -- Primitive operations of tagged types are frozen when the dispatch
9274 -- table is constructed.
9276 if not Comes_From_Source (Typ) or else Is_Tagged_Type (Typ) then
9277 return;
9278 end if;
9280 for J in Names'Range loop
9281 Stream_Op := TSS (Typ, Names (J));
9283 if Present (Stream_Op)
9284 and then Is_Subprogram (Stream_Op)
9285 and then Nkind (Unit_Declaration_Node (Stream_Op)) =
9286 N_Subprogram_Declaration
9287 and then not Is_Frozen (Stream_Op)
9288 then
9289 Append_Freeze_Actions (Typ, Freeze_Entity (Stream_Op, N));
9290 end if;
9291 end loop;
9292 end Freeze_Stream_Operations;
9294 -----------------
9295 -- Freeze_Type --
9296 -----------------
9298 -- Full type declarations are expanded at the point at which the type is
9299 -- frozen. The formal N is the Freeze_Node for the type. Any statements or
9300 -- declarations generated by the freezing (e.g. the procedure generated
9301 -- for initialization) are chained in the Actions field list of the freeze
9302 -- node using Append_Freeze_Actions.
9304 -- WARNING: This routine manages Ghost regions. Return statements must be
9305 -- replaced by gotos which jump to the end of the routine and restore the
9306 -- Ghost mode.
9308 function Freeze_Type (N : Node_Id) return Boolean is
9309 procedure Process_RACW_Types (Typ : Entity_Id);
9310 -- Validate and generate stubs for all RACW types associated with type
9311 -- Typ.
9313 procedure Process_Pending_Access_Types (Typ : Entity_Id);
9314 -- Associate type Typ's Finalize_Address primitive with the finalization
9315 -- masters of pending access-to-Typ types.
9317 ------------------------
9318 -- Process_RACW_Types --
9319 ------------------------
9321 procedure Process_RACW_Types (Typ : Entity_Id) is
9322 List : constant Elist_Id := Access_Types_To_Process (N);
9323 E : Elmt_Id;
9324 Seen : Boolean := False;
9326 begin
9327 if Present (List) then
9328 E := First_Elmt (List);
9329 while Present (E) loop
9330 if Is_Remote_Access_To_Class_Wide_Type (Node (E)) then
9331 Validate_RACW_Primitives (Node (E));
9332 Seen := True;
9333 end if;
9335 Next_Elmt (E);
9336 end loop;
9337 end if;
9339 -- If there are RACWs designating this type, make stubs now
9341 if Seen then
9342 Remote_Types_Tagged_Full_View_Encountered (Typ);
9343 end if;
9344 end Process_RACW_Types;
9346 ----------------------------------
9347 -- Process_Pending_Access_Types --
9348 ----------------------------------
9350 procedure Process_Pending_Access_Types (Typ : Entity_Id) is
9351 E : Elmt_Id;
9353 begin
9354 -- Finalize_Address is not generated in CodePeer mode because the
9355 -- body contains address arithmetic. This processing is disabled.
9357 if CodePeer_Mode then
9358 null;
9360 -- Certain itypes are generated for contexts that cannot allocate
9361 -- objects and should not set primitive Finalize_Address.
9363 elsif Is_Itype (Typ)
9364 and then Nkind (Associated_Node_For_Itype (Typ)) =
9365 N_Explicit_Dereference
9366 then
9367 null;
9369 -- When an access type is declared after the incomplete view of a
9370 -- Taft-amendment type, the access type is considered pending in
9371 -- case the full view of the Taft-amendment type is controlled. If
9372 -- this is indeed the case, associate the Finalize_Address routine
9373 -- of the full view with the finalization masters of all pending
9374 -- access types. This scenario applies to anonymous access types as
9375 -- well. But the Finalize_Address routine is missing if the type is
9376 -- class-wide and we are under restriction No_Dispatching_Calls, see
9377 -- Expand_Freeze_Class_Wide_Type above for the rationale.
9379 elsif Needs_Finalization (Typ)
9380 and then (not Is_Class_Wide_Type (Typ)
9381 or else not Restriction_Active (No_Dispatching_Calls))
9382 and then Present (Pending_Access_Types (Typ))
9383 then
9384 E := First_Elmt (Pending_Access_Types (Typ));
9385 while Present (E) loop
9387 -- Generate:
9388 -- Set_Finalize_Address
9389 -- (Ptr_Typ, <Typ>FD'Unrestricted_Access);
9391 Append_Freeze_Action (Typ,
9392 Make_Set_Finalize_Address_Call
9393 (Loc => Sloc (N),
9394 Ptr_Typ => Node (E)));
9396 Next_Elmt (E);
9397 end loop;
9398 end if;
9399 end Process_Pending_Access_Types;
9401 -- Local variables
9403 Def_Id : constant Entity_Id := Entity (N);
9405 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
9406 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
9407 -- Save the Ghost-related attributes to restore on exit
9409 Result : Boolean := False;
9411 -- Start of processing for Freeze_Type
9413 begin
9414 -- The type being frozen may be subject to pragma Ghost. Set the mode
9415 -- now to ensure that any nodes generated during freezing are properly
9416 -- marked as Ghost.
9418 Set_Ghost_Mode (Def_Id);
9420 -- Process any remote access-to-class-wide types designating the type
9421 -- being frozen.
9423 Process_RACW_Types (Def_Id);
9425 -- Freeze processing for record types
9427 if Is_Record_Type (Def_Id) then
9428 if Ekind (Def_Id) = E_Record_Type then
9429 Expand_Freeze_Record_Type (N);
9430 elsif Is_Class_Wide_Type (Def_Id) then
9431 Expand_Freeze_Class_Wide_Type (N);
9432 end if;
9434 -- Freeze processing for array types
9436 elsif Is_Array_Type (Def_Id) then
9437 Expand_Freeze_Array_Type (N);
9439 -- Freeze processing for access types
9441 -- For pool-specific access types, find out the pool object used for
9442 -- this type, needs actual expansion of it in some cases. Here are the
9443 -- different cases :
9445 -- 1. Rep Clause "for Def_Id'Storage_Size use 0;"
9446 -- ---> don't use any storage pool
9448 -- 2. Rep Clause : for Def_Id'Storage_Size use Expr.
9449 -- Expand:
9450 -- Def_Id__Pool : Stack_Bounded_Pool (Expr, DT'Size, DT'Alignment);
9452 -- 3. Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
9453 -- ---> Storage Pool is the specified one
9455 -- See GNAT Pool packages in the Run-Time for more details
9457 elsif Ekind (Def_Id) in E_Access_Type | E_General_Access_Type then
9458 declare
9459 Loc : constant Source_Ptr := Sloc (N);
9460 Desig_Type : constant Entity_Id := Designated_Type (Def_Id);
9462 Freeze_Action_Typ : Entity_Id;
9463 Pool_Object : Entity_Id;
9465 begin
9466 -- Case 1
9468 -- Rep Clause "for Def_Id'Storage_Size use 0;"
9469 -- ---> don't use any storage pool
9471 if No_Pool_Assigned (Def_Id) then
9472 null;
9474 -- Case 2
9476 -- Rep Clause : for Def_Id'Storage_Size use Expr.
9477 -- ---> Expand:
9478 -- Def_Id__Pool : Stack_Bounded_Pool
9479 -- (Expr, DT'Size, DT'Alignment);
9481 elsif Has_Storage_Size_Clause (Def_Id) then
9482 declare
9483 DT_Align : Node_Id;
9484 DT_Size : Node_Id;
9486 begin
9487 -- For unconstrained composite types we give a size of zero
9488 -- so that the pool knows that it needs a special algorithm
9489 -- for variable size object allocation.
9491 if Is_Composite_Type (Desig_Type)
9492 and then not Is_Constrained (Desig_Type)
9493 then
9494 DT_Size := Make_Integer_Literal (Loc, 0);
9495 DT_Align := Make_Integer_Literal (Loc, Maximum_Alignment);
9497 else
9498 DT_Size :=
9499 Make_Attribute_Reference (Loc,
9500 Prefix => New_Occurrence_Of (Desig_Type, Loc),
9501 Attribute_Name => Name_Max_Size_In_Storage_Elements);
9503 DT_Align :=
9504 Make_Attribute_Reference (Loc,
9505 Prefix => New_Occurrence_Of (Desig_Type, Loc),
9506 Attribute_Name => Name_Alignment);
9507 end if;
9509 Pool_Object :=
9510 Make_Defining_Identifier (Loc,
9511 Chars => New_External_Name (Chars (Def_Id), 'P'));
9513 -- We put the code associated with the pools in the entity
9514 -- that has the later freeze node, usually the access type
9515 -- but it can also be the designated_type; because the pool
9516 -- code requires both those types to be frozen
9518 if Is_Frozen (Desig_Type)
9519 and then (No (Freeze_Node (Desig_Type))
9520 or else Analyzed (Freeze_Node (Desig_Type)))
9521 then
9522 Freeze_Action_Typ := Def_Id;
9524 -- A Taft amendment type cannot get the freeze actions
9525 -- since the full view is not there.
9527 elsif Is_Incomplete_Or_Private_Type (Desig_Type)
9528 and then No (Full_View (Desig_Type))
9529 then
9530 Freeze_Action_Typ := Def_Id;
9532 else
9533 Freeze_Action_Typ := Desig_Type;
9534 end if;
9536 Append_Freeze_Action (Freeze_Action_Typ,
9537 Make_Object_Declaration (Loc,
9538 Defining_Identifier => Pool_Object,
9539 Object_Definition =>
9540 Make_Subtype_Indication (Loc,
9541 Subtype_Mark =>
9542 New_Occurrence_Of
9543 (RTE (RE_Stack_Bounded_Pool), Loc),
9545 Constraint =>
9546 Make_Index_Or_Discriminant_Constraint (Loc,
9547 Constraints => New_List (
9549 -- First discriminant is the Pool Size
9551 New_Occurrence_Of (
9552 Storage_Size_Variable (Def_Id), Loc),
9554 -- Second discriminant is the element size
9556 DT_Size,
9558 -- Third discriminant is the alignment
9560 DT_Align)))));
9561 end;
9563 Set_Associated_Storage_Pool (Def_Id, Pool_Object);
9565 -- Case 3
9567 -- Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
9568 -- ---> Storage Pool is the specified one
9570 -- When compiling in Ada 2012 mode, ensure that the accessibility
9571 -- level of the subpool access type is not deeper than that of the
9572 -- pool_with_subpools.
9574 elsif Ada_Version >= Ada_2012
9575 and then Present (Associated_Storage_Pool (Def_Id))
9576 and then RTU_Loaded (System_Storage_Pools_Subpools)
9577 then
9578 declare
9579 Loc : constant Source_Ptr := Sloc (Def_Id);
9580 Pool : constant Entity_Id :=
9581 Associated_Storage_Pool (Def_Id);
9583 begin
9584 -- It is known that the accessibility level of the access
9585 -- type is deeper than that of the pool.
9587 if Type_Access_Level (Def_Id)
9588 > Static_Accessibility_Level (Pool, Object_Decl_Level)
9589 and then Is_Class_Wide_Type (Etype (Pool))
9590 and then not Accessibility_Checks_Suppressed (Def_Id)
9591 and then not Accessibility_Checks_Suppressed (Pool)
9592 then
9593 -- When the pool is of a class-wide type, it may or may
9594 -- not support subpools depending on the path of
9595 -- derivation. Generate:
9597 -- if Def_Id in RSPWS'Class then
9598 -- raise Program_Error;
9599 -- end if;
9601 Append_Freeze_Action (Def_Id,
9602 Make_If_Statement (Loc,
9603 Condition =>
9604 Make_In (Loc,
9605 Left_Opnd => New_Occurrence_Of (Pool, Loc),
9606 Right_Opnd =>
9607 New_Occurrence_Of
9608 (Class_Wide_Type
9609 (RTE
9610 (RE_Root_Storage_Pool_With_Subpools)),
9611 Loc)),
9612 Then_Statements => New_List (
9613 Make_Raise_Program_Error (Loc,
9614 Reason => PE_Accessibility_Check_Failed))));
9615 end if;
9616 end;
9617 end if;
9619 -- For access-to-controlled types (including class-wide types and
9620 -- Taft-amendment types, which potentially have controlled
9621 -- components), expand the list controller object that will store
9622 -- the dynamically allocated objects. Don't do this transformation
9623 -- for expander-generated access types, except do it for types
9624 -- that are the full view of types derived from other private
9625 -- types and for access types used to implement indirect temps.
9626 -- Also suppress the list controller in the case of a designated
9627 -- type with convention Java, since this is used when binding to
9628 -- Java API specs, where there's no equivalent of a finalization
9629 -- list and we don't want to pull in the finalization support if
9630 -- not needed.
9632 if not Comes_From_Source (Def_Id)
9633 and then not Has_Private_Declaration (Def_Id)
9634 and then not Old_Attr_Util.Indirect_Temps
9635 .Is_Access_Type_For_Indirect_Temp (Def_Id)
9636 then
9637 null;
9639 -- An exception is made for types defined in the run-time because
9640 -- Ada.Tags.Tag itself is such a type and cannot afford this
9641 -- unnecessary overhead that would generates a loop in the
9642 -- expansion scheme. Another exception is if Restrictions
9643 -- (No_Finalization) is active, since then we know nothing is
9644 -- controlled.
9646 elsif Restriction_Active (No_Finalization)
9647 or else In_Runtime (Def_Id)
9648 then
9649 null;
9651 -- Create a finalization master for an access-to-controlled type
9652 -- or an access-to-incomplete type. It is assumed that the full
9653 -- view will be controlled.
9655 elsif Needs_Finalization (Desig_Type)
9656 or else (Is_Incomplete_Type (Desig_Type)
9657 and then No (Full_View (Desig_Type)))
9658 then
9659 Build_Finalization_Master (Def_Id);
9661 -- Create a finalization master when the designated type contains
9662 -- a private component. It is assumed that the full view will be
9663 -- controlled.
9665 elsif Has_Private_Component (Desig_Type) then
9666 Build_Finalization_Master
9667 (Typ => Def_Id,
9668 For_Private => True,
9669 Context_Scope => Scope (Def_Id),
9670 Insertion_Node => Declaration_Node (Desig_Type));
9671 end if;
9672 end;
9674 -- Freeze processing for enumeration types
9676 elsif Ekind (Def_Id) = E_Enumeration_Type then
9678 -- We only have something to do if we have a non-standard
9679 -- representation (i.e. at least one literal whose pos value
9680 -- is not the same as its representation)
9682 if Has_Non_Standard_Rep (Def_Id) then
9683 Expand_Freeze_Enumeration_Type (N);
9684 end if;
9686 -- Private types that are completed by a derivation from a private
9687 -- type have an internally generated full view, that needs to be
9688 -- frozen. This must be done explicitly because the two views share
9689 -- the freeze node, and the underlying full view is not visible when
9690 -- the freeze node is analyzed.
9692 elsif Is_Private_Type (Def_Id)
9693 and then Is_Derived_Type (Def_Id)
9694 and then Present (Full_View (Def_Id))
9695 and then Is_Itype (Full_View (Def_Id))
9696 and then Has_Private_Declaration (Full_View (Def_Id))
9697 and then Freeze_Node (Full_View (Def_Id)) = N
9698 then
9699 Set_Entity (N, Full_View (Def_Id));
9700 Result := Freeze_Type (N);
9701 Set_Entity (N, Def_Id);
9703 -- All other types require no expander action. There are such cases
9704 -- (e.g. task types and protected types). In such cases, the freeze
9705 -- nodes are there for use by Gigi.
9707 end if;
9709 -- Complete the initialization of all pending access types' finalization
9710 -- masters now that the designated type has been is frozen and primitive
9711 -- Finalize_Address generated.
9713 Process_Pending_Access_Types (Def_Id);
9714 Freeze_Stream_Operations (N, Def_Id);
9716 -- Generate the [spec and] body of the invariant procedure tasked with
9717 -- the runtime verification of all invariants that pertain to the type.
9718 -- This includes invariants on the partial and full view, inherited
9719 -- class-wide invariants from parent types or interfaces, and invariants
9720 -- on array elements or record components. But skip internal types.
9722 if Is_Itype (Def_Id) then
9723 null;
9725 elsif Is_Interface (Def_Id) then
9727 -- Interfaces are treated as the partial view of a private type in
9728 -- order to achieve uniformity with the general case. As a result, an
9729 -- interface receives only a "partial" invariant procedure which is
9730 -- never called.
9732 if Has_Own_Invariants (Def_Id) then
9733 Build_Invariant_Procedure_Body
9734 (Typ => Def_Id,
9735 Partial_Invariant => Is_Interface (Def_Id));
9736 end if;
9738 -- Non-interface types
9740 -- Do not generate invariant procedure within other assertion
9741 -- subprograms, which may involve local declarations of local
9742 -- subtypes to which these checks do not apply.
9744 else
9745 if Has_Invariants (Def_Id) then
9746 if not Predicate_Check_In_Scope (Def_Id)
9747 or else (Ekind (Current_Scope) = E_Function
9748 and then Is_Predicate_Function (Current_Scope))
9749 then
9750 null;
9751 else
9752 Build_Invariant_Procedure_Body (Def_Id);
9753 end if;
9754 end if;
9756 -- Generate the [spec and] body of the procedure tasked with the
9757 -- run-time verification of pragma Default_Initial_Condition's
9758 -- expression.
9760 if Has_DIC (Def_Id) then
9761 Build_DIC_Procedure_Body (Def_Id);
9762 end if;
9763 end if;
9765 Restore_Ghost_Region (Saved_GM, Saved_IGR);
9767 return Result;
9769 exception
9770 when RE_Not_Available =>
9771 Restore_Ghost_Region (Saved_GM, Saved_IGR);
9773 return False;
9774 end Freeze_Type;
9776 -------------------------
9777 -- Get_Simple_Init_Val --
9778 -------------------------
9780 function Get_Simple_Init_Val
9781 (Typ : Entity_Id;
9782 N : Node_Id;
9783 Size : Uint := No_Uint) return Node_Id
9785 IV_Attribute : constant Boolean :=
9786 Nkind (N) = N_Attribute_Reference
9787 and then Attribute_Name (N) = Name_Invalid_Value;
9789 Loc : constant Source_Ptr := Sloc (N);
9791 procedure Extract_Subtype_Bounds
9792 (Lo_Bound : out Uint;
9793 Hi_Bound : out Uint);
9794 -- Inspect subtype Typ as well its ancestor subtypes and derived types
9795 -- to determine the best known information about the bounds of the type.
9796 -- The output parameters are set as follows:
9798 -- * Lo_Bound - Set to No_Unit when there is no information available,
9799 -- or to the known low bound.
9801 -- * Hi_Bound - Set to No_Unit when there is no information available,
9802 -- or to the known high bound.
9804 function Simple_Init_Array_Type return Node_Id;
9805 -- Build an expression to initialize array type Typ
9807 function Simple_Init_Defaulted_Type return Node_Id;
9808 -- Build an expression to initialize type Typ which is subject to
9809 -- aspect Default_Value.
9811 function Simple_Init_Initialize_Scalars_Type
9812 (Size_To_Use : Uint) return Node_Id;
9813 -- Build an expression to initialize scalar type Typ which is subject to
9814 -- pragma Initialize_Scalars. Size_To_Use is the size of the object.
9816 function Simple_Init_Normalize_Scalars_Type
9817 (Size_To_Use : Uint) return Node_Id;
9818 -- Build an expression to initialize scalar type Typ which is subject to
9819 -- pragma Normalize_Scalars. Size_To_Use is the size of the object.
9821 function Simple_Init_Private_Type return Node_Id;
9822 -- Build an expression to initialize private type Typ
9824 function Simple_Init_Scalar_Type return Node_Id;
9825 -- Build an expression to initialize scalar type Typ
9827 ----------------------------
9828 -- Extract_Subtype_Bounds --
9829 ----------------------------
9831 procedure Extract_Subtype_Bounds
9832 (Lo_Bound : out Uint;
9833 Hi_Bound : out Uint)
9835 ST1 : Entity_Id;
9836 ST2 : Entity_Id;
9837 Lo : Node_Id;
9838 Hi : Node_Id;
9839 Lo_Val : Uint;
9840 Hi_Val : Uint;
9842 begin
9843 Lo_Bound := No_Uint;
9844 Hi_Bound := No_Uint;
9846 -- Loop to climb ancestor subtypes and derived types
9848 ST1 := Typ;
9849 loop
9850 if not Is_Discrete_Type (ST1) then
9851 return;
9852 end if;
9854 Lo := Type_Low_Bound (ST1);
9855 Hi := Type_High_Bound (ST1);
9857 if Compile_Time_Known_Value (Lo) then
9858 Lo_Val := Expr_Value (Lo);
9860 if No (Lo_Bound) or else Lo_Bound < Lo_Val then
9861 Lo_Bound := Lo_Val;
9862 end if;
9863 end if;
9865 if Compile_Time_Known_Value (Hi) then
9866 Hi_Val := Expr_Value (Hi);
9868 if No (Hi_Bound) or else Hi_Bound > Hi_Val then
9869 Hi_Bound := Hi_Val;
9870 end if;
9871 end if;
9873 ST2 := Ancestor_Subtype (ST1);
9875 if No (ST2) then
9876 ST2 := Etype (ST1);
9877 end if;
9879 exit when ST1 = ST2;
9880 ST1 := ST2;
9881 end loop;
9882 end Extract_Subtype_Bounds;
9884 ----------------------------
9885 -- Simple_Init_Array_Type --
9886 ----------------------------
9888 function Simple_Init_Array_Type return Node_Id is
9889 Comp_Typ : constant Entity_Id := Component_Type (Typ);
9891 function Simple_Init_Dimension (Index : Node_Id) return Node_Id;
9892 -- Initialize a single array dimension with index constraint Index
9894 --------------------
9895 -- Simple_Init_Dimension --
9896 --------------------
9898 function Simple_Init_Dimension (Index : Node_Id) return Node_Id is
9899 begin
9900 -- Process the current dimension
9902 if Present (Index) then
9904 -- Build a suitable "others" aggregate for the next dimension,
9905 -- or initialize the component itself. Generate:
9907 -- (others => ...)
9909 return
9910 Make_Aggregate (Loc,
9911 Component_Associations => New_List (
9912 Make_Component_Association (Loc,
9913 Choices => New_List (Make_Others_Choice (Loc)),
9914 Expression =>
9915 Simple_Init_Dimension (Next_Index (Index)))));
9917 -- Otherwise all dimensions have been processed. Initialize the
9918 -- component itself.
9920 else
9921 return
9922 Get_Simple_Init_Val
9923 (Typ => Comp_Typ,
9924 N => N,
9925 Size => Esize (Comp_Typ));
9926 end if;
9927 end Simple_Init_Dimension;
9929 -- Start of processing for Simple_Init_Array_Type
9931 begin
9932 return Simple_Init_Dimension (First_Index (Typ));
9933 end Simple_Init_Array_Type;
9935 --------------------------------
9936 -- Simple_Init_Defaulted_Type --
9937 --------------------------------
9939 function Simple_Init_Defaulted_Type return Node_Id is
9940 Subtyp : Entity_Id := First_Subtype (Typ);
9942 begin
9943 -- When the first subtype is private, retrieve the expression of the
9944 -- Default_Value from the underlying type.
9946 if Is_Private_Type (Subtyp) then
9947 Subtyp := Full_View (Subtyp);
9948 end if;
9950 -- Use the Sloc of the context node when constructing the initial
9951 -- value because the expression of Default_Value may come from a
9952 -- different unit. Updating the Sloc will result in accurate error
9953 -- diagnostics.
9955 return
9956 OK_Convert_To
9957 (Typ => Typ,
9958 Expr =>
9959 New_Copy_Tree
9960 (Source => Default_Aspect_Value (Subtyp),
9961 New_Sloc => Loc));
9962 end Simple_Init_Defaulted_Type;
9964 -----------------------------------------
9965 -- Simple_Init_Initialize_Scalars_Type --
9966 -----------------------------------------
9968 function Simple_Init_Initialize_Scalars_Type
9969 (Size_To_Use : Uint) return Node_Id
9971 Float_Typ : Entity_Id;
9972 Hi_Bound : Uint;
9973 Lo_Bound : Uint;
9974 Scal_Typ : Scalar_Id;
9976 begin
9977 Extract_Subtype_Bounds (Lo_Bound, Hi_Bound);
9979 -- Float types
9981 if Is_Floating_Point_Type (Typ) then
9982 Float_Typ := Root_Type (Typ);
9984 if Float_Typ = Standard_Short_Float then
9985 Scal_Typ := Name_Short_Float;
9986 elsif Float_Typ = Standard_Float then
9987 Scal_Typ := Name_Float;
9988 elsif Float_Typ = Standard_Long_Float then
9989 Scal_Typ := Name_Long_Float;
9990 else pragma Assert (Float_Typ = Standard_Long_Long_Float);
9991 Scal_Typ := Name_Long_Long_Float;
9992 end if;
9994 -- If zero is invalid, it is a convenient value to use that is for
9995 -- sure an appropriate invalid value in all situations.
9997 elsif Present (Lo_Bound) and then Lo_Bound > Uint_0 then
9998 return Make_Integer_Literal (Loc, 0);
10000 -- Unsigned types
10002 elsif Is_Unsigned_Type (Typ) then
10003 if Size_To_Use <= 8 then
10004 Scal_Typ := Name_Unsigned_8;
10005 elsif Size_To_Use <= 16 then
10006 Scal_Typ := Name_Unsigned_16;
10007 elsif Size_To_Use <= 32 then
10008 Scal_Typ := Name_Unsigned_32;
10009 elsif Size_To_Use <= 64 then
10010 Scal_Typ := Name_Unsigned_64;
10011 else
10012 Scal_Typ := Name_Unsigned_128;
10013 end if;
10015 -- Signed types
10017 else
10018 if Size_To_Use <= 8 then
10019 Scal_Typ := Name_Signed_8;
10020 elsif Size_To_Use <= 16 then
10021 Scal_Typ := Name_Signed_16;
10022 elsif Size_To_Use <= 32 then
10023 Scal_Typ := Name_Signed_32;
10024 elsif Size_To_Use <= 64 then
10025 Scal_Typ := Name_Signed_64;
10026 else
10027 Scal_Typ := Name_Signed_128;
10028 end if;
10029 end if;
10031 -- Use the values specified by pragma Initialize_Scalars or the ones
10032 -- provided by the binder. Higher precedence is given to the pragma.
10034 return Invalid_Scalar_Value (Loc, Scal_Typ);
10035 end Simple_Init_Initialize_Scalars_Type;
10037 ----------------------------------------
10038 -- Simple_Init_Normalize_Scalars_Type --
10039 ----------------------------------------
10041 function Simple_Init_Normalize_Scalars_Type
10042 (Size_To_Use : Uint) return Node_Id
10044 Signed_Size : constant Uint := UI_Min (Uint_63, Size_To_Use - 1);
10046 Expr : Node_Id;
10047 Hi_Bound : Uint;
10048 Lo_Bound : Uint;
10050 begin
10051 Extract_Subtype_Bounds (Lo_Bound, Hi_Bound);
10053 -- If zero is invalid, it is a convenient value to use that is for
10054 -- sure an appropriate invalid value in all situations.
10056 if Present (Lo_Bound) and then Lo_Bound > Uint_0 then
10057 Expr := Make_Integer_Literal (Loc, 0);
10059 -- Cases where all one bits is the appropriate invalid value
10061 -- For modular types, all 1 bits is either invalid or valid. If it
10062 -- is valid, then there is nothing that can be done since there are
10063 -- no invalid values (we ruled out zero already).
10065 -- For signed integer types that have no negative values, either
10066 -- there is room for negative values, or there is not. If there
10067 -- is, then all 1-bits may be interpreted as minus one, which is
10068 -- certainly invalid. Alternatively it is treated as the largest
10069 -- positive value, in which case the observation for modular types
10070 -- still applies.
10072 -- For float types, all 1-bits is a NaN (not a number), which is
10073 -- certainly an appropriately invalid value.
10075 elsif Is_Enumeration_Type (Typ)
10076 or else Is_Floating_Point_Type (Typ)
10077 or else Is_Unsigned_Type (Typ)
10078 then
10079 Expr := Make_Integer_Literal (Loc, 2 ** Size_To_Use - 1);
10081 -- Resolve as Long_Long_Long_Unsigned, because the largest number
10082 -- we can generate is out of range of universal integer.
10084 Analyze_And_Resolve (Expr, Standard_Long_Long_Long_Unsigned);
10086 -- Case of signed types
10088 else
10089 -- Normally we like to use the most negative number. The one
10090 -- exception is when this number is in the known subtype range and
10091 -- the largest positive number is not in the known subtype range.
10093 -- For this exceptional case, use largest positive value
10095 if Present (Lo_Bound) and then Present (Hi_Bound)
10096 and then Lo_Bound <= (-(2 ** Signed_Size))
10097 and then Hi_Bound < 2 ** Signed_Size
10098 then
10099 Expr := Make_Integer_Literal (Loc, 2 ** Signed_Size - 1);
10101 -- Normal case of largest negative value
10103 else
10104 Expr := Make_Integer_Literal (Loc, -(2 ** Signed_Size));
10105 end if;
10106 end if;
10108 return Expr;
10109 end Simple_Init_Normalize_Scalars_Type;
10111 ------------------------------
10112 -- Simple_Init_Private_Type --
10113 ------------------------------
10115 function Simple_Init_Private_Type return Node_Id is
10116 Under_Typ : constant Entity_Id := Underlying_Type (Typ);
10117 Expr : Node_Id;
10119 begin
10120 -- The availability of the underlying view must be checked by routine
10121 -- Needs_Simple_Initialization.
10123 pragma Assert (Present (Under_Typ));
10125 Expr := Get_Simple_Init_Val (Under_Typ, N, Size);
10127 -- If the initial value is null or an aggregate, qualify it with the
10128 -- underlying type in order to provide a proper context.
10130 if Nkind (Expr) in N_Aggregate | N_Null then
10131 Expr :=
10132 Make_Qualified_Expression (Loc,
10133 Subtype_Mark => New_Occurrence_Of (Under_Typ, Loc),
10134 Expression => Expr);
10135 end if;
10137 Expr := Unchecked_Convert_To (Typ, Expr);
10139 -- Do not truncate the result when scalar types are involved and
10140 -- Initialize/Normalize_Scalars is in effect.
10142 if Nkind (Expr) = N_Unchecked_Type_Conversion
10143 and then Is_Scalar_Type (Under_Typ)
10144 then
10145 Set_No_Truncation (Expr);
10146 end if;
10148 return Expr;
10149 end Simple_Init_Private_Type;
10151 -----------------------------
10152 -- Simple_Init_Scalar_Type --
10153 -----------------------------
10155 function Simple_Init_Scalar_Type return Node_Id is
10156 Expr : Node_Id;
10157 Size_To_Use : Uint;
10159 begin
10160 pragma Assert (Init_Or_Norm_Scalars or IV_Attribute);
10162 -- Determine the size of the object. This is either the size provided
10163 -- by the caller, or the Esize of the scalar type.
10165 if No (Size) or else Size <= Uint_0 then
10166 Size_To_Use := UI_Max (Uint_1, Esize (Typ));
10167 else
10168 Size_To_Use := Size;
10169 end if;
10171 -- The maximum size to use is System_Max_Integer_Size bits. This
10172 -- will create values of type Long_Long_Long_Unsigned and the range
10173 -- must fit this type.
10175 if Present (Size_To_Use)
10176 and then Size_To_Use > System_Max_Integer_Size
10177 then
10178 Size_To_Use := UI_From_Int (System_Max_Integer_Size);
10179 end if;
10181 if Normalize_Scalars and then not IV_Attribute then
10182 Expr := Simple_Init_Normalize_Scalars_Type (Size_To_Use);
10183 else
10184 Expr := Simple_Init_Initialize_Scalars_Type (Size_To_Use);
10185 end if;
10187 -- The final expression is obtained by doing an unchecked conversion
10188 -- of this result to the base type of the required subtype. Use the
10189 -- base type to prevent the unchecked conversion from chopping bits,
10190 -- and then we set Kill_Range_Check to preserve the "bad" value.
10192 Expr := Unchecked_Convert_To (Base_Type (Typ), Expr);
10194 -- Ensure that the expression is not truncated since the "bad" bits
10195 -- are desired, and also kill the range checks.
10197 if Nkind (Expr) = N_Unchecked_Type_Conversion then
10198 Set_Kill_Range_Check (Expr);
10199 Set_No_Truncation (Expr);
10200 end if;
10202 return Expr;
10203 end Simple_Init_Scalar_Type;
10205 -- Start of processing for Get_Simple_Init_Val
10207 begin
10208 if Is_Private_Type (Typ) then
10209 return Simple_Init_Private_Type;
10211 elsif Is_Scalar_Type (Typ) then
10212 if Has_Default_Aspect (Typ) then
10213 return Simple_Init_Defaulted_Type;
10214 else
10215 return Simple_Init_Scalar_Type;
10216 end if;
10218 -- Array type with Initialize or Normalize_Scalars
10220 elsif Is_Array_Type (Typ) then
10221 pragma Assert (Init_Or_Norm_Scalars);
10222 return Simple_Init_Array_Type;
10224 -- Access type is initialized to null
10226 elsif Is_Access_Type (Typ) then
10227 return Make_Null (Loc);
10229 -- No other possibilities should arise, since we should only be calling
10230 -- Get_Simple_Init_Val if Needs_Simple_Initialization returned True,
10231 -- indicating one of the above cases held.
10233 else
10234 raise Program_Error;
10235 end if;
10237 exception
10238 when RE_Not_Available =>
10239 return Empty;
10240 end Get_Simple_Init_Val;
10242 ------------------------------
10243 -- Has_New_Non_Standard_Rep --
10244 ------------------------------
10246 function Has_New_Non_Standard_Rep (T : Entity_Id) return Boolean is
10247 begin
10248 if not Is_Derived_Type (T) then
10249 return Has_Non_Standard_Rep (T)
10250 or else Has_Non_Standard_Rep (Root_Type (T));
10252 -- If Has_Non_Standard_Rep is not set on the derived type, the
10253 -- representation is fully inherited.
10255 elsif not Has_Non_Standard_Rep (T) then
10256 return False;
10258 else
10259 return First_Rep_Item (T) /= First_Rep_Item (Root_Type (T));
10261 -- May need a more precise check here: the First_Rep_Item may be a
10262 -- stream attribute, which does not affect the representation of the
10263 -- type ???
10265 end if;
10266 end Has_New_Non_Standard_Rep;
10268 ----------------------
10269 -- Inline_Init_Proc --
10270 ----------------------
10272 function Inline_Init_Proc (Typ : Entity_Id) return Boolean is
10273 begin
10274 -- The initialization proc of protected records is not worth inlining.
10275 -- In addition, when compiled for another unit for inlining purposes,
10276 -- it may make reference to entities that have not been elaborated yet.
10277 -- The initialization proc of records that need finalization contains
10278 -- a nested clean-up procedure that makes it impractical to inline as
10279 -- well, except for simple controlled types themselves. And similar
10280 -- considerations apply to task types.
10282 if Is_Concurrent_Type (Typ) then
10283 return False;
10285 elsif Needs_Finalization (Typ) and then not Is_Controlled (Typ) then
10286 return False;
10288 elsif Has_Task (Typ) then
10289 return False;
10291 else
10292 return True;
10293 end if;
10294 end Inline_Init_Proc;
10296 ----------------
10297 -- In_Runtime --
10298 ----------------
10300 function In_Runtime (E : Entity_Id) return Boolean is
10301 S1 : Entity_Id;
10303 begin
10304 S1 := Scope (E);
10305 while Scope (S1) /= Standard_Standard loop
10306 S1 := Scope (S1);
10307 end loop;
10309 return Is_RTU (S1, System) or else Is_RTU (S1, Ada);
10310 end In_Runtime;
10312 package body Initialization_Control is
10314 ------------------------
10315 -- Requires_Late_Init --
10316 ------------------------
10318 function Requires_Late_Init
10319 (Decl : Node_Id;
10320 Rec_Type : Entity_Id) return Boolean
10322 References_Current_Instance : Boolean := False;
10323 Has_Access_Discriminant : Boolean := False;
10324 Has_Internal_Call : Boolean := False;
10326 function Find_Access_Discriminant
10327 (N : Node_Id) return Traverse_Result;
10328 -- Look for a name denoting an access discriminant
10330 function Find_Current_Instance
10331 (N : Node_Id) return Traverse_Result;
10332 -- Look for a reference to the current instance of the type
10334 function Find_Internal_Call
10335 (N : Node_Id) return Traverse_Result;
10336 -- Look for an internal protected function call
10338 ------------------------------
10339 -- Find_Access_Discriminant --
10340 ------------------------------
10342 function Find_Access_Discriminant
10343 (N : Node_Id) return Traverse_Result is
10344 begin
10345 if Is_Entity_Name (N)
10346 and then Denotes_Discriminant (N)
10347 and then Is_Access_Type (Etype (N))
10348 then
10349 Has_Access_Discriminant := True;
10350 return Abandon;
10351 else
10352 return OK;
10353 end if;
10354 end Find_Access_Discriminant;
10356 ---------------------------
10357 -- Find_Current_Instance --
10358 ---------------------------
10360 function Find_Current_Instance
10361 (N : Node_Id) return Traverse_Result is
10362 begin
10363 if Is_Entity_Name (N)
10364 and then Present (Entity (N))
10365 and then Is_Current_Instance (N)
10366 then
10367 References_Current_Instance := True;
10368 return Abandon;
10369 else
10370 return OK;
10371 end if;
10372 end Find_Current_Instance;
10374 ------------------------
10375 -- Find_Internal_Call --
10376 ------------------------
10378 function Find_Internal_Call (N : Node_Id) return Traverse_Result is
10380 function Call_Scope (N : Node_Id) return Entity_Id;
10381 -- Return the scope enclosing a given call node N
10383 ----------------
10384 -- Call_Scope --
10385 ----------------
10387 function Call_Scope (N : Node_Id) return Entity_Id is
10388 Nam : constant Node_Id := Name (N);
10389 begin
10390 if Nkind (Nam) = N_Selected_Component then
10391 return Scope (Entity (Prefix (Nam)));
10392 else
10393 return Scope (Entity (Nam));
10394 end if;
10395 end Call_Scope;
10397 begin
10398 if Nkind (N) = N_Function_Call
10399 and then Call_Scope (N)
10400 = Corresponding_Concurrent_Type (Rec_Type)
10401 then
10402 Has_Internal_Call := True;
10403 return Abandon;
10404 else
10405 return OK;
10406 end if;
10407 end Find_Internal_Call;
10409 procedure Search_Access_Discriminant is new
10410 Traverse_Proc (Find_Access_Discriminant);
10412 procedure Search_Current_Instance is new
10413 Traverse_Proc (Find_Current_Instance);
10415 procedure Search_Internal_Call is new
10416 Traverse_Proc (Find_Internal_Call);
10418 -- Start of processing for Requires_Late_Init
10420 begin
10421 -- A component of an object is said to require late initialization
10422 -- if:
10424 -- it has an access discriminant value constrained by a per-object
10425 -- expression;
10427 if Has_Access_Constraint (Defining_Identifier (Decl))
10428 and then No (Expression (Decl))
10429 then
10430 return True;
10432 elsif Present (Expression (Decl)) then
10434 -- it has an initialization expression that includes a name
10435 -- denoting an access discriminant;
10437 Search_Access_Discriminant (Expression (Decl));
10439 if Has_Access_Discriminant then
10440 return True;
10441 end if;
10443 -- or it has an initialization expression that includes a
10444 -- reference to the current instance of the type either by
10445 -- name...
10447 Search_Current_Instance (Expression (Decl));
10449 if References_Current_Instance then
10450 return True;
10451 end if;
10453 -- ...or implicitly as the target object of a call.
10455 if Is_Protected_Record_Type (Rec_Type) then
10456 Search_Internal_Call (Expression (Decl));
10458 if Has_Internal_Call then
10459 return True;
10460 end if;
10461 end if;
10462 end if;
10464 return False;
10465 end Requires_Late_Init;
10467 -----------------------------
10468 -- Has_Late_Init_Component --
10469 -----------------------------
10471 function Has_Late_Init_Component
10472 (Tagged_Rec_Type : Entity_Id) return Boolean
10474 Comp_Id : Entity_Id :=
10475 First_Component (Implementation_Base_Type (Tagged_Rec_Type));
10476 begin
10477 while Present (Comp_Id) loop
10478 if Requires_Late_Init (Decl => Parent (Comp_Id),
10479 Rec_Type => Tagged_Rec_Type)
10480 then
10481 return True; -- found a component that requires late init
10483 elsif Chars (Comp_Id) = Name_uParent
10484 and then Has_Late_Init_Component (Etype (Comp_Id))
10485 then
10486 return True; -- an ancestor type has a late init component
10487 end if;
10489 Next_Component (Comp_Id);
10490 end loop;
10492 return False;
10493 end Has_Late_Init_Component;
10495 ------------------------
10496 -- Tag_Init_Condition --
10497 ------------------------
10499 function Tag_Init_Condition
10500 (Loc : Source_Ptr;
10501 Init_Control_Formal : Entity_Id) return Node_Id is
10502 begin
10503 return Make_Op_Eq (Loc,
10504 New_Occurrence_Of (Init_Control_Formal, Loc),
10505 Make_Mode_Literal (Loc, Full_Init));
10506 end Tag_Init_Condition;
10508 --------------------------
10509 -- Early_Init_Condition --
10510 --------------------------
10512 function Early_Init_Condition
10513 (Loc : Source_Ptr;
10514 Init_Control_Formal : Entity_Id) return Node_Id is
10515 begin
10516 return Make_Op_Ne (Loc,
10517 New_Occurrence_Of (Init_Control_Formal, Loc),
10518 Make_Mode_Literal (Loc, Late_Init_Only));
10519 end Early_Init_Condition;
10521 -------------------------
10522 -- Late_Init_Condition --
10523 -------------------------
10525 function Late_Init_Condition
10526 (Loc : Source_Ptr;
10527 Init_Control_Formal : Entity_Id) return Node_Id is
10528 begin
10529 return Make_Op_Ne (Loc,
10530 New_Occurrence_Of (Init_Control_Formal, Loc),
10531 Make_Mode_Literal (Loc, Early_Init_Only));
10532 end Late_Init_Condition;
10534 end Initialization_Control;
10536 ----------------------------
10537 -- Initialization_Warning --
10538 ----------------------------
10540 procedure Initialization_Warning (E : Entity_Id) is
10541 Warning_Needed : Boolean;
10543 begin
10544 Warning_Needed := False;
10546 if Ekind (Current_Scope) = E_Package
10547 and then Static_Elaboration_Desired (Current_Scope)
10548 then
10549 if Is_Type (E) then
10550 if Is_Record_Type (E) then
10551 if Has_Discriminants (E)
10552 or else Is_Limited_Type (E)
10553 or else Has_Non_Standard_Rep (E)
10554 then
10555 Warning_Needed := True;
10557 else
10558 -- Verify that at least one component has an initialization
10559 -- expression. No need for a warning on a type if all its
10560 -- components have no initialization.
10562 declare
10563 Comp : Entity_Id;
10565 begin
10566 Comp := First_Component (E);
10567 while Present (Comp) loop
10568 pragma Assert
10569 (Nkind (Parent (Comp)) = N_Component_Declaration);
10571 if Present (Expression (Parent (Comp))) then
10572 Warning_Needed := True;
10573 exit;
10574 end if;
10576 Next_Component (Comp);
10577 end loop;
10578 end;
10579 end if;
10581 if Warning_Needed then
10582 Error_Msg_N
10583 ("objects of the type cannot be initialized statically "
10584 & "by default??", Parent (E));
10585 end if;
10586 end if;
10588 else
10589 Error_Msg_N ("object cannot be initialized statically??", E);
10590 end if;
10591 end if;
10592 end Initialization_Warning;
10594 ------------------
10595 -- Init_Formals --
10596 ------------------
10598 function Init_Formals (Typ : Entity_Id; Proc_Id : Entity_Id) return List_Id
10600 Loc : constant Source_Ptr := Sloc (Typ);
10601 Unc_Arr : constant Boolean :=
10602 Is_Array_Type (Typ) and then not Is_Constrained (Typ);
10603 With_Prot : constant Boolean :=
10604 Has_Protected (Typ)
10605 or else (Is_Record_Type (Typ)
10606 and then Is_Protected_Record_Type (Typ));
10607 With_Task : constant Boolean :=
10608 not Global_No_Tasking
10609 and then
10610 (Has_Task (Typ)
10611 or else (Is_Record_Type (Typ)
10612 and then Is_Task_Record_Type (Typ)));
10613 Formals : List_Id;
10615 begin
10616 -- The first parameter is always _Init : [in] out Typ. Note that we need
10617 -- it to be in/out in the case of an unconstrained array, because of the
10618 -- need to have the bounds, and in the case of protected or task record
10619 -- value, because there are default record fields that may be referenced
10620 -- in the generated initialization routine.
10622 Formals := New_List (
10623 Make_Parameter_Specification (Loc,
10624 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uInit),
10625 In_Present => Unc_Arr or else With_Prot or else With_Task,
10626 Out_Present => True,
10627 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
10629 -- For task record value, or type that contains tasks, add two more
10630 -- formals, _Master : Master_Id and _Chain : in out Activation_Chain
10631 -- We also add these parameters for the task record type case.
10633 if With_Task then
10634 Append_To (Formals,
10635 Make_Parameter_Specification (Loc,
10636 Defining_Identifier =>
10637 Make_Defining_Identifier (Loc, Name_uMaster),
10638 Parameter_Type =>
10639 New_Occurrence_Of (Standard_Integer, Loc)));
10641 Set_Has_Master_Entity (Proc_Id);
10643 -- Add _Chain (not done for sequential elaboration policy, see
10644 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
10646 if Partition_Elaboration_Policy /= 'S' then
10647 Append_To (Formals,
10648 Make_Parameter_Specification (Loc,
10649 Defining_Identifier =>
10650 Make_Defining_Identifier (Loc, Name_uChain),
10651 In_Present => True,
10652 Out_Present => True,
10653 Parameter_Type =>
10654 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc)));
10655 end if;
10657 Append_To (Formals,
10658 Make_Parameter_Specification (Loc,
10659 Defining_Identifier =>
10660 Make_Defining_Identifier (Loc, Name_uTask_Name),
10661 In_Present => True,
10662 Parameter_Type => New_Occurrence_Of (Standard_String, Loc)));
10663 end if;
10665 -- Due to certain edge cases such as arrays with null-excluding
10666 -- components being built with the secondary stack it becomes necessary
10667 -- to add a formal to the Init_Proc which controls whether we raise
10668 -- Constraint_Errors on generated calls for internal object
10669 -- declarations.
10671 if Needs_Conditional_Null_Excluding_Check (Typ) then
10672 Append_To (Formals,
10673 Make_Parameter_Specification (Loc,
10674 Defining_Identifier =>
10675 Make_Defining_Identifier (Loc,
10676 New_External_Name (Chars
10677 (Component_Type (Typ)), "_skip_null_excluding_check")),
10678 Expression => New_Occurrence_Of (Standard_False, Loc),
10679 In_Present => True,
10680 Parameter_Type =>
10681 New_Occurrence_Of (Standard_Boolean, Loc)));
10682 end if;
10684 return Formals;
10686 exception
10687 when RE_Not_Available =>
10688 return Empty_List;
10689 end Init_Formals;
10691 -------------------------
10692 -- Init_Secondary_Tags --
10693 -------------------------
10695 procedure Init_Secondary_Tags
10696 (Typ : Entity_Id;
10697 Target : Node_Id;
10698 Init_Tags_List : List_Id;
10699 Stmts_List : List_Id;
10700 Fixed_Comps : Boolean := True;
10701 Variable_Comps : Boolean := True)
10703 Loc : constant Source_Ptr := Sloc (Target);
10705 -- Inherit the C++ tag of the secondary dispatch table of Typ associated
10706 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
10708 procedure Initialize_Tag
10709 (Typ : Entity_Id;
10710 Iface : Entity_Id;
10711 Tag_Comp : Entity_Id;
10712 Iface_Tag : Node_Id);
10713 -- Initialize the tag of the secondary dispatch table of Typ associated
10714 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
10715 -- Compiling under the CPP full ABI compatibility mode, if the ancestor
10716 -- of Typ CPP tagged type we generate code to inherit the contents of
10717 -- the dispatch table directly from the ancestor.
10719 --------------------
10720 -- Initialize_Tag --
10721 --------------------
10723 procedure Initialize_Tag
10724 (Typ : Entity_Id;
10725 Iface : Entity_Id;
10726 Tag_Comp : Entity_Id;
10727 Iface_Tag : Node_Id)
10729 Comp_Typ : Entity_Id;
10730 Offset_To_Top_Comp : Entity_Id := Empty;
10732 begin
10733 -- Initialize pointer to secondary DT associated with the interface
10735 if not Is_Ancestor (Iface, Typ, Use_Full_View => True) then
10736 Append_To (Init_Tags_List,
10737 Make_Assignment_Statement (Loc,
10738 Name =>
10739 Make_Selected_Component (Loc,
10740 Prefix => New_Copy_Tree (Target),
10741 Selector_Name => New_Occurrence_Of (Tag_Comp, Loc)),
10742 Expression =>
10743 New_Occurrence_Of (Iface_Tag, Loc)));
10744 end if;
10746 Comp_Typ := Scope (Tag_Comp);
10748 -- Initialize the entries of the table of interfaces. We generate a
10749 -- different call when the parent of the type has variable size
10750 -- components.
10752 if Comp_Typ /= Etype (Comp_Typ)
10753 and then Is_Variable_Size_Record (Etype (Comp_Typ))
10754 and then Chars (Tag_Comp) /= Name_uTag
10755 then
10756 pragma Assert (Present (DT_Offset_To_Top_Func (Tag_Comp)));
10758 -- Issue error if Set_Dynamic_Offset_To_Top is not available in a
10759 -- configurable run-time environment.
10761 if not RTE_Available (RE_Set_Dynamic_Offset_To_Top) then
10762 Error_Msg_CRT
10763 ("variable size record with interface types", Typ);
10764 return;
10765 end if;
10767 -- Generate:
10768 -- Set_Dynamic_Offset_To_Top
10769 -- (This => Init,
10770 -- Prim_T => Typ'Tag,
10771 -- Interface_T => Iface'Tag,
10772 -- Offset_Value => n,
10773 -- Offset_Func => Fn'Unrestricted_Access)
10775 Append_To (Stmts_List,
10776 Make_Procedure_Call_Statement (Loc,
10777 Name =>
10778 New_Occurrence_Of (RTE (RE_Set_Dynamic_Offset_To_Top), Loc),
10779 Parameter_Associations => New_List (
10780 Make_Attribute_Reference (Loc,
10781 Prefix => New_Copy_Tree (Target),
10782 Attribute_Name => Name_Address),
10784 Unchecked_Convert_To (RTE (RE_Tag),
10785 New_Occurrence_Of
10786 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc)),
10788 Unchecked_Convert_To (RTE (RE_Tag),
10789 New_Occurrence_Of
10790 (Node (First_Elmt (Access_Disp_Table (Iface))),
10791 Loc)),
10793 Unchecked_Convert_To
10794 (RTE (RE_Storage_Offset),
10795 Make_Op_Minus (Loc,
10796 Make_Attribute_Reference (Loc,
10797 Prefix =>
10798 Make_Selected_Component (Loc,
10799 Prefix => New_Copy_Tree (Target),
10800 Selector_Name =>
10801 New_Occurrence_Of (Tag_Comp, Loc)),
10802 Attribute_Name => Name_Position))),
10804 Unchecked_Convert_To (RTE (RE_Offset_To_Top_Function_Ptr),
10805 Make_Attribute_Reference (Loc,
10806 Prefix => New_Occurrence_Of
10807 (DT_Offset_To_Top_Func (Tag_Comp), Loc),
10808 Attribute_Name => Name_Unrestricted_Access)))));
10810 -- In this case the next component stores the value of the offset
10811 -- to the top.
10813 Offset_To_Top_Comp := Next_Entity (Tag_Comp);
10814 pragma Assert (Present (Offset_To_Top_Comp));
10816 Append_To (Init_Tags_List,
10817 Make_Assignment_Statement (Loc,
10818 Name =>
10819 Make_Selected_Component (Loc,
10820 Prefix => New_Copy_Tree (Target),
10821 Selector_Name =>
10822 New_Occurrence_Of (Offset_To_Top_Comp, Loc)),
10824 Expression =>
10825 Make_Op_Minus (Loc,
10826 Make_Attribute_Reference (Loc,
10827 Prefix =>
10828 Make_Selected_Component (Loc,
10829 Prefix => New_Copy_Tree (Target),
10830 Selector_Name => New_Occurrence_Of (Tag_Comp, Loc)),
10831 Attribute_Name => Name_Position))));
10833 -- Normal case: No discriminants in the parent type
10835 else
10836 -- Don't need to set any value if the offset-to-top field is
10837 -- statically set or if this interface shares the primary
10838 -- dispatch table.
10840 if not Building_Static_Secondary_DT (Typ)
10841 and then not Is_Ancestor (Iface, Typ, Use_Full_View => True)
10842 then
10843 Append_To (Stmts_List,
10844 Build_Set_Static_Offset_To_Top (Loc,
10845 Iface_Tag => New_Occurrence_Of (Iface_Tag, Loc),
10846 Offset_Value =>
10847 Unchecked_Convert_To (RTE (RE_Storage_Offset),
10848 Make_Op_Minus (Loc,
10849 Make_Attribute_Reference (Loc,
10850 Prefix =>
10851 Make_Selected_Component (Loc,
10852 Prefix => New_Copy_Tree (Target),
10853 Selector_Name =>
10854 New_Occurrence_Of (Tag_Comp, Loc)),
10855 Attribute_Name => Name_Position)))));
10856 end if;
10858 -- Generate:
10859 -- Register_Interface_Offset
10860 -- (Prim_T => Typ'Tag,
10861 -- Interface_T => Iface'Tag,
10862 -- Is_Constant => True,
10863 -- Offset_Value => n,
10864 -- Offset_Func => null);
10866 if not Building_Static_Secondary_DT (Typ)
10867 and then RTE_Available (RE_Register_Interface_Offset)
10868 then
10869 Append_To (Stmts_List,
10870 Make_Procedure_Call_Statement (Loc,
10871 Name =>
10872 New_Occurrence_Of
10873 (RTE (RE_Register_Interface_Offset), Loc),
10874 Parameter_Associations => New_List (
10875 Unchecked_Convert_To (RTE (RE_Tag),
10876 New_Occurrence_Of
10877 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc)),
10879 Unchecked_Convert_To (RTE (RE_Tag),
10880 New_Occurrence_Of
10881 (Node (First_Elmt (Access_Disp_Table (Iface))), Loc)),
10883 New_Occurrence_Of (Standard_True, Loc),
10885 Unchecked_Convert_To (RTE (RE_Storage_Offset),
10886 Make_Op_Minus (Loc,
10887 Make_Attribute_Reference (Loc,
10888 Prefix =>
10889 Make_Selected_Component (Loc,
10890 Prefix => New_Copy_Tree (Target),
10891 Selector_Name =>
10892 New_Occurrence_Of (Tag_Comp, Loc)),
10893 Attribute_Name => Name_Position))),
10895 Make_Null (Loc))));
10896 end if;
10897 end if;
10898 end Initialize_Tag;
10900 -- Local variables
10902 Full_Typ : Entity_Id;
10903 Ifaces_List : Elist_Id;
10904 Ifaces_Comp_List : Elist_Id;
10905 Ifaces_Tag_List : Elist_Id;
10906 Iface_Elmt : Elmt_Id;
10907 Iface_Comp_Elmt : Elmt_Id;
10908 Iface_Tag_Elmt : Elmt_Id;
10909 Tag_Comp : Node_Id;
10910 In_Variable_Pos : Boolean;
10912 -- Start of processing for Init_Secondary_Tags
10914 begin
10915 -- Handle private types
10917 if Present (Full_View (Typ)) then
10918 Full_Typ := Full_View (Typ);
10919 else
10920 Full_Typ := Typ;
10921 end if;
10923 Collect_Interfaces_Info
10924 (Full_Typ, Ifaces_List, Ifaces_Comp_List, Ifaces_Tag_List);
10926 Iface_Elmt := First_Elmt (Ifaces_List);
10927 Iface_Comp_Elmt := First_Elmt (Ifaces_Comp_List);
10928 Iface_Tag_Elmt := First_Elmt (Ifaces_Tag_List);
10929 while Present (Iface_Elmt) loop
10930 Tag_Comp := Node (Iface_Comp_Elmt);
10932 -- Check if parent of record type has variable size components
10934 In_Variable_Pos := Scope (Tag_Comp) /= Etype (Scope (Tag_Comp))
10935 and then Is_Variable_Size_Record (Etype (Scope (Tag_Comp)));
10937 -- If we are compiling under the CPP full ABI compatibility mode and
10938 -- the ancestor is a CPP_Pragma tagged type then we generate code to
10939 -- initialize the secondary tag components from tags that reference
10940 -- secondary tables filled with copy of parent slots.
10942 if Is_CPP_Class (Root_Type (Full_Typ)) then
10944 -- Reject interface components located at variable offset in
10945 -- C++ derivations. This is currently unsupported.
10947 if not Fixed_Comps and then In_Variable_Pos then
10949 -- Locate the first dynamic component of the record. Done to
10950 -- improve the text of the warning.
10952 declare
10953 Comp : Entity_Id;
10954 Comp_Typ : Entity_Id;
10956 begin
10957 Comp := First_Entity (Typ);
10958 while Present (Comp) loop
10959 Comp_Typ := Etype (Comp);
10961 if Ekind (Comp) /= E_Discriminant
10962 and then not Is_Tag (Comp)
10963 then
10964 exit when
10965 (Is_Record_Type (Comp_Typ)
10966 and then
10967 Is_Variable_Size_Record (Base_Type (Comp_Typ)))
10968 or else
10969 (Is_Array_Type (Comp_Typ)
10970 and then Is_Variable_Size_Array (Comp_Typ));
10971 end if;
10973 Next_Entity (Comp);
10974 end loop;
10976 pragma Assert (Present (Comp));
10978 -- Move this check to sem???
10979 Error_Msg_Node_2 := Comp;
10980 Error_Msg_NE
10981 ("parent type & with dynamic component & cannot be parent"
10982 & " of 'C'P'P derivation if new interfaces are present",
10983 Typ, Scope (Original_Record_Component (Comp)));
10985 Error_Msg_Sloc :=
10986 Sloc (Scope (Original_Record_Component (Comp)));
10987 Error_Msg_NE
10988 ("type derived from 'C'P'P type & defined #",
10989 Typ, Scope (Original_Record_Component (Comp)));
10991 -- Avoid duplicated warnings
10993 exit;
10994 end;
10996 -- Initialize secondary tags
10998 else
10999 Initialize_Tag
11000 (Typ => Full_Typ,
11001 Iface => Node (Iface_Elmt),
11002 Tag_Comp => Tag_Comp,
11003 Iface_Tag => Node (Iface_Tag_Elmt));
11004 end if;
11006 -- Otherwise generate code to initialize the tag
11008 else
11009 if (In_Variable_Pos and then Variable_Comps)
11010 or else (not In_Variable_Pos and then Fixed_Comps)
11011 then
11012 Initialize_Tag
11013 (Typ => Full_Typ,
11014 Iface => Node (Iface_Elmt),
11015 Tag_Comp => Tag_Comp,
11016 Iface_Tag => Node (Iface_Tag_Elmt));
11017 end if;
11018 end if;
11020 Next_Elmt (Iface_Elmt);
11021 Next_Elmt (Iface_Comp_Elmt);
11022 Next_Elmt (Iface_Tag_Elmt);
11023 end loop;
11024 end Init_Secondary_Tags;
11026 ----------------------------
11027 -- Is_Null_Statement_List --
11028 ----------------------------
11030 function Is_Null_Statement_List (Stmts : List_Id) return Boolean is
11031 Stmt : Node_Id;
11033 begin
11034 -- We must skip SCIL nodes because they may have been added to the list
11035 -- by Insert_Actions.
11037 Stmt := First_Non_SCIL_Node (Stmts);
11038 while Present (Stmt) loop
11039 if Nkind (Stmt) = N_Case_Statement then
11040 declare
11041 Alt : Node_Id;
11042 begin
11043 Alt := First (Alternatives (Stmt));
11044 while Present (Alt) loop
11045 if not Is_Null_Statement_List (Statements (Alt)) then
11046 return False;
11047 end if;
11049 Next (Alt);
11050 end loop;
11051 end;
11053 elsif Nkind (Stmt) /= N_Null_Statement then
11054 return False;
11055 end if;
11057 Stmt := Next_Non_SCIL_Node (Stmt);
11058 end loop;
11060 return True;
11061 end Is_Null_Statement_List;
11063 ----------------------------------------
11064 -- Make_Controlling_Function_Wrappers --
11065 ----------------------------------------
11067 procedure Make_Controlling_Function_Wrappers
11068 (Tag_Typ : Entity_Id;
11069 Decl_List : out List_Id;
11070 Body_List : out List_Id)
11072 Loc : constant Source_Ptr := Sloc (Tag_Typ);
11074 function Make_Wrapper_Specification (Subp : Entity_Id) return Node_Id;
11075 -- Returns a function specification with the same profile as Subp
11077 --------------------------------
11078 -- Make_Wrapper_Specification --
11079 --------------------------------
11081 function Make_Wrapper_Specification (Subp : Entity_Id) return Node_Id is
11082 begin
11083 return
11084 Make_Function_Specification (Loc,
11085 Defining_Unit_Name =>
11086 Make_Defining_Identifier (Loc,
11087 Chars => Chars (Subp)),
11088 Parameter_Specifications =>
11089 Copy_Parameter_List (Subp),
11090 Result_Definition =>
11091 New_Occurrence_Of (Etype (Subp), Loc));
11092 end Make_Wrapper_Specification;
11094 Prim_Elmt : Elmt_Id;
11095 Subp : Entity_Id;
11096 Actual_List : List_Id;
11097 Formal : Entity_Id;
11098 Par_Formal : Entity_Id;
11099 Ext_Aggr : Node_Id;
11100 Formal_Node : Node_Id;
11101 Func_Body : Node_Id;
11102 Func_Decl : Node_Id;
11103 Func_Id : Entity_Id;
11105 -- Start of processing for Make_Controlling_Function_Wrappers
11107 begin
11108 Decl_List := New_List;
11109 Body_List := New_List;
11111 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
11112 while Present (Prim_Elmt) loop
11113 Subp := Node (Prim_Elmt);
11115 -- If a primitive function with a controlling result of the type has
11116 -- not been overridden by the user, then we must create a wrapper
11117 -- function here that effectively overrides it and invokes the
11118 -- (non-abstract) parent function. This can only occur for a null
11119 -- extension. Note that functions with anonymous controlling access
11120 -- results don't qualify and must be overridden. We also exclude
11121 -- Input attributes, since each type will have its own version of
11122 -- Input constructed by the expander. The test for Comes_From_Source
11123 -- is needed to distinguish inherited operations from renamings
11124 -- (which also have Alias set). We exclude internal entities with
11125 -- Interface_Alias to avoid generating duplicated wrappers since
11126 -- the primitive which covers the interface is also available in
11127 -- the list of primitive operations.
11129 -- The function may be abstract, or require_Overriding may be set
11130 -- for it, because tests for null extensions may already have reset
11131 -- the Is_Abstract_Subprogram_Flag. If Requires_Overriding is not
11132 -- set, functions that need wrappers are recognized by having an
11133 -- alias that returns the parent type.
11135 if Comes_From_Source (Subp)
11136 or else No (Alias (Subp))
11137 or else Present (Interface_Alias (Subp))
11138 or else Ekind (Subp) /= E_Function
11139 or else not Has_Controlling_Result (Subp)
11140 or else Is_Access_Type (Etype (Subp))
11141 or else Is_Abstract_Subprogram (Alias (Subp))
11142 or else Is_TSS (Subp, TSS_Stream_Input)
11143 then
11144 goto Next_Prim;
11146 elsif Is_Abstract_Subprogram (Subp)
11147 or else Requires_Overriding (Subp)
11148 or else
11149 (Is_Null_Extension (Etype (Subp))
11150 and then Etype (Alias (Subp)) /= Etype (Subp))
11151 then
11152 -- If there is a non-overloadable homonym in the current
11153 -- scope, the implicit declaration remains invisible.
11154 -- We check the current entity with the same name, or its
11155 -- homonym in case the derivation takes place after the
11156 -- hiding object declaration.
11158 if Present (Current_Entity (Subp)) then
11159 declare
11160 Curr : constant Entity_Id := Current_Entity (Subp);
11161 Prev : constant Entity_Id := Homonym (Curr);
11162 begin
11163 if (Comes_From_Source (Curr)
11164 and then Scope (Curr) = Current_Scope
11165 and then not Is_Overloadable (Curr))
11166 or else
11167 (Present (Prev)
11168 and then Comes_From_Source (Prev)
11169 and then Scope (Prev) = Current_Scope
11170 and then not Is_Overloadable (Prev))
11171 then
11172 goto Next_Prim;
11173 end if;
11174 end;
11175 end if;
11177 Func_Decl :=
11178 Make_Subprogram_Declaration (Loc,
11179 Specification => Make_Wrapper_Specification (Subp));
11181 Append_To (Decl_List, Func_Decl);
11183 -- Build a wrapper body that calls the parent function. The body
11184 -- contains a single return statement that returns an extension
11185 -- aggregate whose ancestor part is a call to the parent function,
11186 -- passing the formals as actuals (with any controlling arguments
11187 -- converted to the types of the corresponding formals of the
11188 -- parent function, which might be anonymous access types), and
11189 -- having a null extension.
11191 Formal := First_Formal (Subp);
11192 Par_Formal := First_Formal (Alias (Subp));
11193 Formal_Node :=
11194 First (Parameter_Specifications (Specification (Func_Decl)));
11196 if Present (Formal) then
11197 Actual_List := New_List;
11199 while Present (Formal) loop
11200 if Is_Controlling_Formal (Formal) then
11201 Append_To (Actual_List,
11202 Make_Type_Conversion (Loc,
11203 Subtype_Mark =>
11204 New_Occurrence_Of (Etype (Par_Formal), Loc),
11205 Expression =>
11206 New_Occurrence_Of
11207 (Defining_Identifier (Formal_Node), Loc)));
11208 else
11209 Append_To
11210 (Actual_List,
11211 New_Occurrence_Of
11212 (Defining_Identifier (Formal_Node), Loc));
11213 end if;
11215 Next_Formal (Formal);
11216 Next_Formal (Par_Formal);
11217 Next (Formal_Node);
11218 end loop;
11219 else
11220 Actual_List := No_List;
11221 end if;
11223 Ext_Aggr :=
11224 Make_Extension_Aggregate (Loc,
11225 Ancestor_Part =>
11226 Make_Function_Call (Loc,
11227 Name =>
11228 New_Occurrence_Of (Alias (Subp), Loc),
11229 Parameter_Associations => Actual_List),
11230 Null_Record_Present => True);
11232 -- GNATprove will use expression of an expression function as an
11233 -- implicit postcondition. GNAT will also benefit from expression
11234 -- function to avoid premature freezing, but would struggle if we
11235 -- added an expression function to freezing actions, so we create
11236 -- the expanded form directly.
11238 if GNATprove_Mode then
11239 Func_Body :=
11240 Make_Expression_Function (Loc,
11241 Specification =>
11242 Make_Wrapper_Specification (Subp),
11243 Expression => Ext_Aggr);
11244 else
11245 Func_Body :=
11246 Make_Subprogram_Body (Loc,
11247 Specification =>
11248 Make_Wrapper_Specification (Subp),
11249 Declarations => Empty_List,
11250 Handled_Statement_Sequence =>
11251 Make_Handled_Sequence_Of_Statements (Loc,
11252 Statements => New_List (
11253 Make_Simple_Return_Statement (Loc,
11254 Expression => Ext_Aggr))));
11255 Set_Was_Expression_Function (Func_Body);
11256 end if;
11258 Append_To (Body_List, Func_Body);
11260 -- Replace the inherited function with the wrapper function in the
11261 -- primitive operations list. We add the minimum decoration needed
11262 -- to override interface primitives.
11264 Func_Id := Defining_Unit_Name (Specification (Func_Decl));
11266 Mutate_Ekind (Func_Id, E_Function);
11267 Set_Is_Wrapper (Func_Id);
11269 -- Corresponding_Spec will be set again to the same value during
11270 -- analysis, but we need this information earlier.
11271 -- Expand_N_Freeze_Entity needs to know whether a subprogram body
11272 -- is a wrapper's body in order to get check suppression right.
11274 Set_Corresponding_Spec (Func_Body, Func_Id);
11275 end if;
11277 <<Next_Prim>>
11278 Next_Elmt (Prim_Elmt);
11279 end loop;
11280 end Make_Controlling_Function_Wrappers;
11282 ------------------
11283 -- Make_Eq_Body --
11284 ------------------
11286 function Make_Eq_Body
11287 (Typ : Entity_Id;
11288 Eq_Name : Name_Id) return Node_Id
11290 Loc : constant Source_Ptr := Sloc (Parent (Typ));
11291 Decl : Node_Id;
11292 Def : constant Node_Id := Parent (Typ);
11293 Stmts : constant List_Id := New_List;
11294 Variant_Case : Boolean := Has_Discriminants (Typ);
11295 Comps : Node_Id := Empty;
11296 Typ_Def : Node_Id := Type_Definition (Def);
11298 begin
11299 Decl :=
11300 Predef_Spec_Or_Body (Loc,
11301 Tag_Typ => Typ,
11302 Name => Eq_Name,
11303 Profile => New_List (
11304 Make_Parameter_Specification (Loc,
11305 Defining_Identifier =>
11306 Make_Defining_Identifier (Loc, Name_X),
11307 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
11309 Make_Parameter_Specification (Loc,
11310 Defining_Identifier =>
11311 Make_Defining_Identifier (Loc, Name_Y),
11312 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
11314 Ret_Type => Standard_Boolean,
11315 For_Body => True);
11317 if Variant_Case then
11318 if Nkind (Typ_Def) = N_Derived_Type_Definition then
11319 Typ_Def := Record_Extension_Part (Typ_Def);
11320 end if;
11322 if Present (Typ_Def) then
11323 Comps := Component_List (Typ_Def);
11324 end if;
11326 Variant_Case :=
11327 Present (Comps) and then Present (Variant_Part (Comps));
11328 end if;
11330 if Variant_Case then
11331 Append_To (Stmts,
11332 Make_Eq_If (Typ, Discriminant_Specifications (Def)));
11333 Append_List_To (Stmts, Make_Eq_Case (Typ, Comps));
11334 Append_To (Stmts,
11335 Make_Simple_Return_Statement (Loc,
11336 Expression => New_Occurrence_Of (Standard_True, Loc)));
11338 else
11339 Append_To (Stmts,
11340 Make_Simple_Return_Statement (Loc,
11341 Expression =>
11342 Expand_Record_Equality
11343 (Typ,
11344 Typ => Typ,
11345 Lhs => Make_Identifier (Loc, Name_X),
11346 Rhs => Make_Identifier (Loc, Name_Y))));
11347 end if;
11349 Set_Handled_Statement_Sequence
11350 (Decl, Make_Handled_Sequence_Of_Statements (Loc, Stmts));
11351 return Decl;
11352 end Make_Eq_Body;
11354 ------------------
11355 -- Make_Eq_Case --
11356 ------------------
11358 -- <Make_Eq_If shared components>
11360 -- case X.D1 is
11361 -- when V1 => <Make_Eq_Case> on subcomponents
11362 -- ...
11363 -- when Vn => <Make_Eq_Case> on subcomponents
11364 -- end case;
11366 function Make_Eq_Case
11367 (E : Entity_Id;
11368 CL : Node_Id;
11369 Discrs : Elist_Id := New_Elmt_List) return List_Id
11371 Loc : constant Source_Ptr := Sloc (E);
11372 Result : constant List_Id := New_List;
11373 Variant : Node_Id;
11374 Alt_List : List_Id;
11376 function Corresponding_Formal (C : Node_Id) return Entity_Id;
11377 -- Given the discriminant that controls a given variant of an unchecked
11378 -- union, find the formal of the equality function that carries the
11379 -- inferred value of the discriminant.
11381 function External_Name (E : Entity_Id) return Name_Id;
11382 -- The value of a given discriminant is conveyed in the corresponding
11383 -- formal parameter of the equality routine. The name of this formal
11384 -- parameter carries a one-character suffix which is removed here.
11386 --------------------------
11387 -- Corresponding_Formal --
11388 --------------------------
11390 function Corresponding_Formal (C : Node_Id) return Entity_Id is
11391 Discr : constant Entity_Id := Entity (Name (Variant_Part (C)));
11392 Elm : Elmt_Id;
11394 begin
11395 Elm := First_Elmt (Discrs);
11396 while Present (Elm) loop
11397 if Chars (Discr) = External_Name (Node (Elm)) then
11398 return Node (Elm);
11399 end if;
11401 Next_Elmt (Elm);
11402 end loop;
11404 -- A formal of the proper name must be found
11406 raise Program_Error;
11407 end Corresponding_Formal;
11409 -------------------
11410 -- External_Name --
11411 -------------------
11413 function External_Name (E : Entity_Id) return Name_Id is
11414 begin
11415 Get_Name_String (Chars (E));
11416 Name_Len := Name_Len - 1;
11417 return Name_Find;
11418 end External_Name;
11420 -- Start of processing for Make_Eq_Case
11422 begin
11423 Append_To (Result, Make_Eq_If (E, Component_Items (CL)));
11425 if No (Variant_Part (CL)) then
11426 return Result;
11427 end if;
11429 Variant := First_Non_Pragma (Variants (Variant_Part (CL)));
11431 if No (Variant) then
11432 return Result;
11433 end if;
11435 Alt_List := New_List;
11436 while Present (Variant) loop
11437 Append_To (Alt_List,
11438 Make_Case_Statement_Alternative (Loc,
11439 Discrete_Choices => New_Copy_List (Discrete_Choices (Variant)),
11440 Statements =>
11441 Make_Eq_Case (E, Component_List (Variant), Discrs)));
11442 Next_Non_Pragma (Variant);
11443 end loop;
11445 -- If we have an Unchecked_Union, use one of the parameters of the
11446 -- enclosing equality routine that captures the discriminant, to use
11447 -- as the expression in the generated case statement.
11449 if Is_Unchecked_Union (E) then
11450 Append_To (Result,
11451 Make_Case_Statement (Loc,
11452 Expression =>
11453 New_Occurrence_Of (Corresponding_Formal (CL), Loc),
11454 Alternatives => Alt_List));
11456 else
11457 Append_To (Result,
11458 Make_Case_Statement (Loc,
11459 Expression =>
11460 Make_Selected_Component (Loc,
11461 Prefix => Make_Identifier (Loc, Name_X),
11462 Selector_Name => New_Copy (Name (Variant_Part (CL)))),
11463 Alternatives => Alt_List));
11464 end if;
11466 return Result;
11467 end Make_Eq_Case;
11469 ----------------
11470 -- Make_Eq_If --
11471 ----------------
11473 -- Generates:
11475 -- if
11476 -- X.C1 /= Y.C1
11477 -- or else
11478 -- X.C2 /= Y.C2
11479 -- ...
11480 -- then
11481 -- return False;
11482 -- end if;
11484 -- or a null statement if the list L is empty
11486 -- Equality may be user-defined for a given component type, in which case
11487 -- a function call is constructed instead of an operator node. This is an
11488 -- Ada 2012 change in the composability of equality for untagged composite
11489 -- types.
11491 function Make_Eq_If
11492 (E : Entity_Id;
11493 L : List_Id) return Node_Id
11495 Loc : constant Source_Ptr := Sloc (E);
11497 C : Node_Id;
11498 Cond : Node_Id;
11499 Field_Name : Name_Id;
11500 Next_Test : Node_Id;
11501 Typ : Entity_Id;
11503 begin
11504 if No (L) then
11505 return Make_Null_Statement (Loc);
11507 else
11508 Cond := Empty;
11510 C := First_Non_Pragma (L);
11511 while Present (C) loop
11512 Typ := Etype (Defining_Identifier (C));
11513 Field_Name := Chars (Defining_Identifier (C));
11515 -- The tags must not be compared: they are not part of the value.
11516 -- Ditto for parent interfaces because their equality operator is
11517 -- abstract.
11519 -- Note also that in the following, we use Make_Identifier for
11520 -- the component names. Use of New_Occurrence_Of to identify the
11521 -- components would be incorrect because the wrong entities for
11522 -- discriminants could be picked up in the private type case.
11524 if Field_Name = Name_uParent
11525 and then Is_Interface (Typ)
11526 then
11527 null;
11529 elsif Field_Name /= Name_uTag then
11530 declare
11531 Lhs : constant Node_Id :=
11532 Make_Selected_Component (Loc,
11533 Prefix => Make_Identifier (Loc, Name_X),
11534 Selector_Name => Make_Identifier (Loc, Field_Name));
11536 Rhs : constant Node_Id :=
11537 Make_Selected_Component (Loc,
11538 Prefix => Make_Identifier (Loc, Name_Y),
11539 Selector_Name => Make_Identifier (Loc, Field_Name));
11540 Eq_Call : Node_Id;
11542 begin
11543 -- Build equality code with a user-defined operator, if
11544 -- available, and with the predefined "=" otherwise. For
11545 -- compatibility with older Ada versions, we also use the
11546 -- predefined operation if the component-type equality is
11547 -- abstract, rather than raising Program_Error.
11549 if Ada_Version < Ada_2012 then
11550 Next_Test := Make_Op_Ne (Loc, Lhs, Rhs);
11552 else
11553 Eq_Call := Build_Eq_Call (Typ, Loc, Lhs, Rhs);
11555 if No (Eq_Call) then
11556 Next_Test := Make_Op_Ne (Loc, Lhs, Rhs);
11558 -- If a component has a defined abstract equality, its
11559 -- application raises Program_Error on that component
11560 -- and therefore on the current variant.
11562 elsif Nkind (Eq_Call) = N_Raise_Program_Error then
11563 Set_Etype (Eq_Call, Standard_Boolean);
11564 Next_Test := Make_Op_Not (Loc, Eq_Call);
11566 else
11567 Next_Test := Make_Op_Not (Loc, Eq_Call);
11568 end if;
11569 end if;
11570 end;
11572 Evolve_Or_Else (Cond, Next_Test);
11573 end if;
11575 Next_Non_Pragma (C);
11576 end loop;
11578 if No (Cond) then
11579 return Make_Null_Statement (Loc);
11581 else
11582 return
11583 Make_Implicit_If_Statement (E,
11584 Condition => Cond,
11585 Then_Statements => New_List (
11586 Make_Simple_Return_Statement (Loc,
11587 Expression => New_Occurrence_Of (Standard_False, Loc))));
11588 end if;
11589 end if;
11590 end Make_Eq_If;
11592 -------------------
11593 -- Make_Neq_Body --
11594 -------------------
11596 function Make_Neq_Body (Tag_Typ : Entity_Id) return Node_Id is
11598 function Is_Predefined_Neq_Renaming (Prim : Node_Id) return Boolean;
11599 -- Returns true if Prim is a renaming of an unresolved predefined
11600 -- inequality operation.
11602 --------------------------------
11603 -- Is_Predefined_Neq_Renaming --
11604 --------------------------------
11606 function Is_Predefined_Neq_Renaming (Prim : Node_Id) return Boolean is
11607 begin
11608 return Chars (Prim) /= Name_Op_Ne
11609 and then Present (Alias (Prim))
11610 and then Comes_From_Source (Prim)
11611 and then Is_Intrinsic_Subprogram (Alias (Prim))
11612 and then Chars (Alias (Prim)) = Name_Op_Ne;
11613 end Is_Predefined_Neq_Renaming;
11615 -- Local variables
11617 Loc : constant Source_Ptr := Sloc (Parent (Tag_Typ));
11618 Decl : Node_Id;
11619 Eq_Prim : Entity_Id;
11620 Left_Op : Entity_Id;
11621 Renaming_Prim : Entity_Id;
11622 Right_Op : Entity_Id;
11623 Target : Entity_Id;
11625 -- Start of processing for Make_Neq_Body
11627 begin
11628 -- For a call on a renaming of a dispatching subprogram that is
11629 -- overridden, if the overriding occurred before the renaming, then
11630 -- the body executed is that of the overriding declaration, even if the
11631 -- overriding declaration is not visible at the place of the renaming;
11632 -- otherwise, the inherited or predefined subprogram is called, see
11633 -- (RM 8.5.4(8)).
11635 -- Stage 1: Search for a renaming of the inequality primitive and also
11636 -- search for an overriding of the equality primitive located before the
11637 -- renaming declaration.
11639 declare
11640 Elmt : Elmt_Id;
11641 Prim : Node_Id;
11643 begin
11644 Eq_Prim := Empty;
11645 Renaming_Prim := Empty;
11647 Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
11648 while Present (Elmt) loop
11649 Prim := Node (Elmt);
11651 if Is_User_Defined_Equality (Prim) and then No (Alias (Prim)) then
11652 if No (Renaming_Prim) then
11653 pragma Assert (No (Eq_Prim));
11654 Eq_Prim := Prim;
11655 end if;
11657 elsif Is_Predefined_Neq_Renaming (Prim) then
11658 Renaming_Prim := Prim;
11659 end if;
11661 Next_Elmt (Elmt);
11662 end loop;
11663 end;
11665 -- No further action needed if no renaming was found
11667 if No (Renaming_Prim) then
11668 return Empty;
11669 end if;
11671 -- Stage 2: Replace the renaming declaration by a subprogram declaration
11672 -- (required to add its body)
11674 Decl := Parent (Parent (Renaming_Prim));
11675 Rewrite (Decl,
11676 Make_Subprogram_Declaration (Loc,
11677 Specification => Specification (Decl)));
11678 Set_Analyzed (Decl);
11680 -- Remove the decoration of intrinsic renaming subprogram
11682 Set_Is_Intrinsic_Subprogram (Renaming_Prim, False);
11683 Set_Convention (Renaming_Prim, Convention_Ada);
11684 Set_Alias (Renaming_Prim, Empty);
11685 Set_Has_Completion (Renaming_Prim, False);
11687 -- Stage 3: Build the corresponding body
11689 Left_Op := First_Formal (Renaming_Prim);
11690 Right_Op := Next_Formal (Left_Op);
11692 Decl :=
11693 Predef_Spec_Or_Body (Loc,
11694 Tag_Typ => Tag_Typ,
11695 Name => Chars (Renaming_Prim),
11696 Profile => New_List (
11697 Make_Parameter_Specification (Loc,
11698 Defining_Identifier =>
11699 Make_Defining_Identifier (Loc, Chars (Left_Op)),
11700 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
11702 Make_Parameter_Specification (Loc,
11703 Defining_Identifier =>
11704 Make_Defining_Identifier (Loc, Chars (Right_Op)),
11705 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
11707 Ret_Type => Standard_Boolean,
11708 For_Body => True);
11710 -- If the overriding of the equality primitive occurred before the
11711 -- renaming, then generate:
11713 -- function <Neq_Name> (X : Y : Typ) return Boolean is
11714 -- begin
11715 -- return not Oeq (X, Y);
11716 -- end;
11718 if Present (Eq_Prim) then
11719 Target := Eq_Prim;
11721 -- Otherwise build a nested subprogram which performs the predefined
11722 -- evaluation of the equality operator. That is, generate:
11724 -- function <Neq_Name> (X : Y : Typ) return Boolean is
11725 -- function Oeq (X : Y) return Boolean is
11726 -- begin
11727 -- <<body of default implementation>>
11728 -- end;
11729 -- begin
11730 -- return not Oeq (X, Y);
11731 -- end;
11733 else
11734 declare
11735 Local_Subp : Node_Id;
11736 begin
11737 Local_Subp := Make_Eq_Body (Tag_Typ, Name_Op_Eq);
11738 Set_Declarations (Decl, New_List (Local_Subp));
11739 Target := Defining_Entity (Local_Subp);
11740 end;
11741 end if;
11743 Set_Handled_Statement_Sequence
11744 (Decl,
11745 Make_Handled_Sequence_Of_Statements (Loc, New_List (
11746 Make_Simple_Return_Statement (Loc,
11747 Expression =>
11748 Make_Op_Not (Loc,
11749 Make_Function_Call (Loc,
11750 Name => New_Occurrence_Of (Target, Loc),
11751 Parameter_Associations => New_List (
11752 Make_Identifier (Loc, Chars (Left_Op)),
11753 Make_Identifier (Loc, Chars (Right_Op)))))))));
11755 return Decl;
11756 end Make_Neq_Body;
11758 -------------------------------
11759 -- Make_Null_Procedure_Specs --
11760 -------------------------------
11762 function Make_Null_Procedure_Specs (Tag_Typ : Entity_Id) return List_Id is
11763 Decl_List : constant List_Id := New_List;
11764 Loc : constant Source_Ptr := Sloc (Tag_Typ);
11765 Formal : Entity_Id;
11766 New_Param_Spec : Node_Id;
11767 New_Spec : Node_Id;
11768 Parent_Subp : Entity_Id;
11769 Prim_Elmt : Elmt_Id;
11770 Subp : Entity_Id;
11772 begin
11773 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
11774 while Present (Prim_Elmt) loop
11775 Subp := Node (Prim_Elmt);
11777 -- If a null procedure inherited from an interface has not been
11778 -- overridden, then we build a null procedure declaration to
11779 -- override the inherited procedure.
11781 Parent_Subp := Alias (Subp);
11783 if Present (Parent_Subp)
11784 and then Is_Null_Interface_Primitive (Parent_Subp)
11785 then
11786 -- The null procedure spec is copied from the inherited procedure,
11787 -- except for the IS NULL (which must be added) and the overriding
11788 -- indicators (which must be removed, if present).
11790 New_Spec :=
11791 Copy_Subprogram_Spec (Subprogram_Specification (Subp), Loc);
11793 Set_Null_Present (New_Spec, True);
11794 Set_Must_Override (New_Spec, False);
11795 Set_Must_Not_Override (New_Spec, False);
11797 Formal := First_Formal (Subp);
11798 New_Param_Spec := First (Parameter_Specifications (New_Spec));
11800 while Present (Formal) loop
11802 -- For controlling arguments we must change their parameter
11803 -- type to reference the tagged type (instead of the interface
11804 -- type).
11806 if Is_Controlling_Formal (Formal) then
11807 if Nkind (Parameter_Type (Parent (Formal))) = N_Identifier
11808 then
11809 Set_Parameter_Type (New_Param_Spec,
11810 New_Occurrence_Of (Tag_Typ, Loc));
11812 else pragma Assert
11813 (Nkind (Parameter_Type (Parent (Formal))) =
11814 N_Access_Definition);
11815 Set_Subtype_Mark (Parameter_Type (New_Param_Spec),
11816 New_Occurrence_Of (Tag_Typ, Loc));
11817 end if;
11818 end if;
11820 Next_Formal (Formal);
11821 Next (New_Param_Spec);
11822 end loop;
11824 Append_To (Decl_List,
11825 Make_Subprogram_Declaration (Loc,
11826 Specification => New_Spec));
11827 end if;
11829 Next_Elmt (Prim_Elmt);
11830 end loop;
11832 return Decl_List;
11833 end Make_Null_Procedure_Specs;
11835 ---------------------------------------
11836 -- Make_Predefined_Primitive_Eq_Spec --
11837 ---------------------------------------
11839 procedure Make_Predefined_Primitive_Eq_Spec
11840 (Tag_Typ : Entity_Id;
11841 Predef_List : List_Id;
11842 Renamed_Eq : out Entity_Id)
11844 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean;
11845 -- Returns true if Prim is a renaming of an unresolved predefined
11846 -- equality operation.
11848 -------------------------------
11849 -- Is_Predefined_Eq_Renaming --
11850 -------------------------------
11852 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean is
11853 begin
11854 return Chars (Prim) /= Name_Op_Eq
11855 and then Present (Alias (Prim))
11856 and then Comes_From_Source (Prim)
11857 and then Is_Intrinsic_Subprogram (Alias (Prim))
11858 and then Chars (Alias (Prim)) = Name_Op_Eq;
11859 end Is_Predefined_Eq_Renaming;
11861 -- Local variables
11863 Loc : constant Source_Ptr := Sloc (Tag_Typ);
11865 Eq_Name : Name_Id := Name_Op_Eq;
11866 Eq_Needed : Boolean := True;
11867 Eq_Spec : Node_Id;
11868 Prim : Elmt_Id;
11870 Has_Predef_Eq_Renaming : Boolean := False;
11871 -- Set to True if Tag_Typ has a primitive that renames the predefined
11872 -- equality operator. Used to implement (RM 8-5-4(8)).
11874 -- Start of processing for Make_Predefined_Primitive_Specs
11876 begin
11877 Renamed_Eq := Empty;
11879 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
11880 while Present (Prim) loop
11882 -- If a primitive is encountered that renames the predefined equality
11883 -- operator before reaching any explicit equality primitive, then we
11884 -- still need to create a predefined equality function, because calls
11885 -- to it can occur via the renaming. A new name is created for the
11886 -- equality to avoid conflicting with any user-defined equality.
11887 -- (Note that this doesn't account for renamings of equality nested
11888 -- within subpackages???)
11890 if Is_Predefined_Eq_Renaming (Node (Prim)) then
11891 Has_Predef_Eq_Renaming := True;
11892 Eq_Name := New_External_Name (Chars (Node (Prim)), 'E');
11894 -- User-defined equality
11896 elsif Is_User_Defined_Equality (Node (Prim)) then
11897 if No (Alias (Node (Prim)))
11898 or else Nkind (Unit_Declaration_Node (Node (Prim))) =
11899 N_Subprogram_Renaming_Declaration
11900 then
11901 Eq_Needed := False;
11902 exit;
11904 -- If the parent is not an interface type and has an abstract
11905 -- equality function explicitly defined in the sources, then the
11906 -- inherited equality is abstract as well, and no body can be
11907 -- created for it.
11909 elsif not Is_Interface (Etype (Tag_Typ))
11910 and then Present (Alias (Node (Prim)))
11911 and then Comes_From_Source (Alias (Node (Prim)))
11912 and then Is_Abstract_Subprogram (Alias (Node (Prim)))
11913 then
11914 Eq_Needed := False;
11915 exit;
11917 -- If the type has an equality function corresponding with a
11918 -- primitive defined in an interface type, the inherited equality
11919 -- is abstract as well, and no body can be created for it.
11921 elsif Present (Alias (Node (Prim)))
11922 and then Comes_From_Source (Ultimate_Alias (Node (Prim)))
11923 and then
11924 Is_Interface
11925 (Find_Dispatching_Type (Ultimate_Alias (Node (Prim))))
11926 then
11927 Eq_Needed := False;
11928 exit;
11929 end if;
11930 end if;
11932 Next_Elmt (Prim);
11933 end loop;
11935 -- If a renaming of predefined equality was found but there was no
11936 -- user-defined equality (so Eq_Needed is still true), then set the name
11937 -- back to Name_Op_Eq. But in the case where a user-defined equality was
11938 -- located after such a renaming, then the predefined equality function
11939 -- is still needed, so Eq_Needed must be set back to True.
11941 if Eq_Name /= Name_Op_Eq then
11942 if Eq_Needed then
11943 Eq_Name := Name_Op_Eq;
11944 else
11945 Eq_Needed := True;
11946 end if;
11947 end if;
11949 if Eq_Needed then
11950 Eq_Spec := Predef_Spec_Or_Body (Loc,
11951 Tag_Typ => Tag_Typ,
11952 Name => Eq_Name,
11953 Profile => New_List (
11954 Make_Parameter_Specification (Loc,
11955 Defining_Identifier =>
11956 Make_Defining_Identifier (Loc, Name_X),
11957 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
11959 Make_Parameter_Specification (Loc,
11960 Defining_Identifier =>
11961 Make_Defining_Identifier (Loc, Name_Y),
11962 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
11963 Ret_Type => Standard_Boolean);
11964 Append_To (Predef_List, Eq_Spec);
11966 if Has_Predef_Eq_Renaming then
11967 Renamed_Eq := Defining_Unit_Name (Specification (Eq_Spec));
11969 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
11970 while Present (Prim) loop
11972 -- Any renamings of equality that appeared before an overriding
11973 -- equality must be updated to refer to the entity for the
11974 -- predefined equality, otherwise calls via the renaming would
11975 -- get incorrectly resolved to call the user-defined equality
11976 -- function.
11978 if Is_Predefined_Eq_Renaming (Node (Prim)) then
11979 Set_Alias (Node (Prim), Renamed_Eq);
11981 -- Exit upon encountering a user-defined equality
11983 elsif Chars (Node (Prim)) = Name_Op_Eq
11984 and then No (Alias (Node (Prim)))
11985 then
11986 exit;
11987 end if;
11989 Next_Elmt (Prim);
11990 end loop;
11991 end if;
11992 end if;
11993 end Make_Predefined_Primitive_Eq_Spec;
11995 -------------------------------------
11996 -- Make_Predefined_Primitive_Specs --
11997 -------------------------------------
11999 procedure Make_Predefined_Primitive_Specs
12000 (Tag_Typ : Entity_Id;
12001 Predef_List : out List_Id;
12002 Renamed_Eq : out Entity_Id)
12004 Loc : constant Source_Ptr := Sloc (Tag_Typ);
12005 Res : constant List_Id := New_List;
12007 use Exp_Put_Image;
12009 begin
12010 Renamed_Eq := Empty;
12012 -- Spec of _Size
12014 Append_To (Res, Predef_Spec_Or_Body (Loc,
12015 Tag_Typ => Tag_Typ,
12016 Name => Name_uSize,
12017 Profile => New_List (
12018 Make_Parameter_Specification (Loc,
12019 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
12020 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
12022 Ret_Type => Standard_Long_Long_Integer));
12024 -- Spec of Put_Image
12026 if not No_Run_Time_Mode
12027 and then RTE_Available (RE_Root_Buffer_Type)
12028 then
12029 -- No_Run_Time_Mode implies that the declaration of Tag_Typ
12030 -- (like any tagged type) will be rejected. Given this, avoid
12031 -- cascading errors associated with the Tag_Typ's TSS_Put_Image
12032 -- procedure.
12034 Append_To (Res, Predef_Spec_Or_Body (Loc,
12035 Tag_Typ => Tag_Typ,
12036 Name => Make_TSS_Name (Tag_Typ, TSS_Put_Image),
12037 Profile => Build_Put_Image_Profile (Loc, Tag_Typ)));
12038 end if;
12040 -- Specs for dispatching stream attributes
12042 declare
12043 Stream_Op_TSS_Names :
12044 constant array (Positive range <>) of TSS_Name_Type :=
12045 (TSS_Stream_Read,
12046 TSS_Stream_Write,
12047 TSS_Stream_Input,
12048 TSS_Stream_Output);
12050 begin
12051 for Op in Stream_Op_TSS_Names'Range loop
12052 if Stream_Operation_OK (Tag_Typ, Stream_Op_TSS_Names (Op)) then
12053 Append_To (Res,
12054 Predef_Stream_Attr_Spec (Loc, Tag_Typ,
12055 Stream_Op_TSS_Names (Op)));
12056 end if;
12057 end loop;
12058 end;
12060 -- Spec of "=" is expanded if the type is not limited and if a user
12061 -- defined "=" was not already declared for the non-full view of a
12062 -- private extension.
12064 if not Is_Limited_Type (Tag_Typ) then
12065 Make_Predefined_Primitive_Eq_Spec (Tag_Typ, Res, Renamed_Eq);
12067 -- Spec for dispatching assignment
12069 Append_To (Res, Predef_Spec_Or_Body (Loc,
12070 Tag_Typ => Tag_Typ,
12071 Name => Name_uAssign,
12072 Profile => New_List (
12073 Make_Parameter_Specification (Loc,
12074 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
12075 Out_Present => True,
12076 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
12078 Make_Parameter_Specification (Loc,
12079 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
12080 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)))));
12081 end if;
12083 -- Ada 2005: Generate declarations for the following primitive
12084 -- operations for limited interfaces and synchronized types that
12085 -- implement a limited interface.
12087 -- Disp_Asynchronous_Select
12088 -- Disp_Conditional_Select
12089 -- Disp_Get_Prim_Op_Kind
12090 -- Disp_Get_Task_Id
12091 -- Disp_Requeue
12092 -- Disp_Timed_Select
12094 -- Disable the generation of these bodies if Ravenscar or ZFP is active
12096 if Ada_Version >= Ada_2005
12097 and then not Restriction_Active (No_Select_Statements)
12098 and then RTE_Available (RE_Select_Specific_Data)
12099 then
12100 -- These primitives are defined abstract in interface types
12102 if Is_Interface (Tag_Typ)
12103 and then Is_Limited_Record (Tag_Typ)
12104 then
12105 Append_To (Res,
12106 Make_Abstract_Subprogram_Declaration (Loc,
12107 Specification =>
12108 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
12110 Append_To (Res,
12111 Make_Abstract_Subprogram_Declaration (Loc,
12112 Specification =>
12113 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
12115 Append_To (Res,
12116 Make_Abstract_Subprogram_Declaration (Loc,
12117 Specification =>
12118 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
12120 Append_To (Res,
12121 Make_Abstract_Subprogram_Declaration (Loc,
12122 Specification =>
12123 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
12125 Append_To (Res,
12126 Make_Abstract_Subprogram_Declaration (Loc,
12127 Specification =>
12128 Make_Disp_Requeue_Spec (Tag_Typ)));
12130 Append_To (Res,
12131 Make_Abstract_Subprogram_Declaration (Loc,
12132 Specification =>
12133 Make_Disp_Timed_Select_Spec (Tag_Typ)));
12135 -- If ancestor is an interface type, declare non-abstract primitives
12136 -- to override the abstract primitives of the interface type.
12138 -- In VM targets we define these primitives in all root tagged types
12139 -- that are not interface types. Done because in VM targets we don't
12140 -- have secondary dispatch tables and any derivation of Tag_Typ may
12141 -- cover limited interfaces (which always have these primitives since
12142 -- they may be ancestors of synchronized interface types).
12144 elsif (not Is_Interface (Tag_Typ)
12145 and then Is_Interface (Etype (Tag_Typ))
12146 and then Is_Limited_Record (Etype (Tag_Typ)))
12147 or else
12148 (Is_Concurrent_Record_Type (Tag_Typ)
12149 and then Has_Interfaces (Tag_Typ))
12150 or else
12151 (not Tagged_Type_Expansion
12152 and then not Is_Interface (Tag_Typ)
12153 and then Tag_Typ = Root_Type (Tag_Typ))
12154 then
12155 Append_To (Res,
12156 Make_Subprogram_Declaration (Loc,
12157 Specification =>
12158 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
12160 Append_To (Res,
12161 Make_Subprogram_Declaration (Loc,
12162 Specification =>
12163 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
12165 Append_To (Res,
12166 Make_Subprogram_Declaration (Loc,
12167 Specification =>
12168 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
12170 Append_To (Res,
12171 Make_Subprogram_Declaration (Loc,
12172 Specification =>
12173 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
12175 Append_To (Res,
12176 Make_Subprogram_Declaration (Loc,
12177 Specification =>
12178 Make_Disp_Requeue_Spec (Tag_Typ)));
12180 Append_To (Res,
12181 Make_Subprogram_Declaration (Loc,
12182 Specification =>
12183 Make_Disp_Timed_Select_Spec (Tag_Typ)));
12184 end if;
12185 end if;
12187 -- All tagged types receive their own Deep_Adjust and Deep_Finalize
12188 -- regardless of whether they are controlled or may contain controlled
12189 -- components.
12191 -- Do not generate the routines if finalization is disabled
12193 if Restriction_Active (No_Finalization) then
12194 null;
12196 else
12197 if not Is_Limited_Type (Tag_Typ) then
12198 Append_To (Res, Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust));
12199 end if;
12201 Append_To (Res, Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize));
12202 end if;
12204 Predef_List := Res;
12205 end Make_Predefined_Primitive_Specs;
12207 -------------------------
12208 -- Make_Tag_Assignment --
12209 -------------------------
12211 function Make_Tag_Assignment (N : Node_Id) return Node_Id is
12212 Loc : constant Source_Ptr := Sloc (N);
12213 Def_Id : constant Entity_Id := Defining_Identifier (N);
12214 Expr : constant Node_Id := Expression (N);
12215 Typ : constant Entity_Id := Etype (Def_Id);
12216 Full_Typ : constant Entity_Id := Underlying_Type (Typ);
12218 begin
12219 -- This expansion activity is called during analysis
12221 if Is_Tagged_Type (Typ)
12222 and then not Is_Class_Wide_Type (Typ)
12223 and then not Is_CPP_Class (Typ)
12224 and then Tagged_Type_Expansion
12225 and then Nkind (Unqualify (Expr)) /= N_Aggregate
12226 then
12227 return
12228 Make_Tag_Assignment_From_Type
12229 (Loc, New_Occurrence_Of (Def_Id, Loc), Full_Typ);
12231 else
12232 return Empty;
12233 end if;
12234 end Make_Tag_Assignment;
12236 ----------------------
12237 -- Predef_Deep_Spec --
12238 ----------------------
12240 function Predef_Deep_Spec
12241 (Loc : Source_Ptr;
12242 Tag_Typ : Entity_Id;
12243 Name : TSS_Name_Type;
12244 For_Body : Boolean := False) return Node_Id
12246 Formals : List_Id;
12248 begin
12249 -- V : in out Tag_Typ
12251 Formals := New_List (
12252 Make_Parameter_Specification (Loc,
12253 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
12254 In_Present => True,
12255 Out_Present => True,
12256 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)));
12258 -- F : Boolean := True
12260 if Name = TSS_Deep_Adjust
12261 or else Name = TSS_Deep_Finalize
12262 then
12263 Append_To (Formals,
12264 Make_Parameter_Specification (Loc,
12265 Defining_Identifier => Make_Defining_Identifier (Loc, Name_F),
12266 Parameter_Type => New_Occurrence_Of (Standard_Boolean, Loc),
12267 Expression => New_Occurrence_Of (Standard_True, Loc)));
12268 end if;
12270 return
12271 Predef_Spec_Or_Body (Loc,
12272 Name => Make_TSS_Name (Tag_Typ, Name),
12273 Tag_Typ => Tag_Typ,
12274 Profile => Formals,
12275 For_Body => For_Body);
12277 exception
12278 when RE_Not_Available =>
12279 return Empty;
12280 end Predef_Deep_Spec;
12282 -------------------------
12283 -- Predef_Spec_Or_Body --
12284 -------------------------
12286 function Predef_Spec_Or_Body
12287 (Loc : Source_Ptr;
12288 Tag_Typ : Entity_Id;
12289 Name : Name_Id;
12290 Profile : List_Id;
12291 Ret_Type : Entity_Id := Empty;
12292 For_Body : Boolean := False) return Node_Id
12294 Id : constant Entity_Id := Make_Defining_Identifier (Loc, Name);
12295 Spec : Node_Id;
12297 begin
12298 Set_Is_Public (Id, Is_Public (Tag_Typ));
12300 -- The internal flag is set to mark these declarations because they have
12301 -- specific properties. First, they are primitives even if they are not
12302 -- defined in the type scope (the freezing point is not necessarily in
12303 -- the same scope). Second, the predefined equality can be overridden by
12304 -- a user-defined equality, no body will be generated in this case.
12306 Set_Is_Internal (Id);
12308 if not Debug_Generated_Code then
12309 Set_Debug_Info_Off (Id);
12310 end if;
12312 if No (Ret_Type) then
12313 Spec :=
12314 Make_Procedure_Specification (Loc,
12315 Defining_Unit_Name => Id,
12316 Parameter_Specifications => Profile);
12317 else
12318 Spec :=
12319 Make_Function_Specification (Loc,
12320 Defining_Unit_Name => Id,
12321 Parameter_Specifications => Profile,
12322 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
12323 end if;
12325 -- Declare an abstract subprogram for primitive subprograms of an
12326 -- interface type (except for "=").
12328 if Is_Interface (Tag_Typ) then
12329 if Name /= Name_Op_Eq then
12330 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
12332 -- The equality function (if any) for an interface type is defined
12333 -- to be nonabstract, so we create an expression function for it that
12334 -- always returns False. Note that the function can never actually be
12335 -- invoked because interface types are abstract, so there aren't any
12336 -- objects of such types (and their equality operation will always
12337 -- dispatch).
12339 else
12340 return Make_Expression_Function
12341 (Loc, Spec, New_Occurrence_Of (Standard_False, Loc));
12342 end if;
12344 -- If body case, return empty subprogram body. Note that this is ill-
12345 -- formed, because there is not even a null statement, and certainly not
12346 -- a return in the function case. The caller is expected to do surgery
12347 -- on the body to add the appropriate stuff.
12349 elsif For_Body then
12350 return Make_Subprogram_Body (Loc, Spec, Empty_List, Empty);
12352 -- For the case of an Input attribute predefined for an abstract type,
12353 -- generate an abstract specification. This will never be called, but we
12354 -- need the slot allocated in the dispatching table so that attributes
12355 -- typ'Class'Input and typ'Class'Output will work properly.
12357 elsif Is_TSS (Name, TSS_Stream_Input)
12358 and then Is_Abstract_Type (Tag_Typ)
12359 then
12360 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
12362 -- Normal spec case, where we return a subprogram declaration
12364 else
12365 return Make_Subprogram_Declaration (Loc, Spec);
12366 end if;
12367 end Predef_Spec_Or_Body;
12369 -----------------------------
12370 -- Predef_Stream_Attr_Spec --
12371 -----------------------------
12373 function Predef_Stream_Attr_Spec
12374 (Loc : Source_Ptr;
12375 Tag_Typ : Entity_Id;
12376 Name : TSS_Name_Type) return Node_Id
12378 Ret_Type : Entity_Id;
12380 begin
12381 if Name = TSS_Stream_Input then
12382 Ret_Type := Tag_Typ;
12383 else
12384 Ret_Type := Empty;
12385 end if;
12387 return
12388 Predef_Spec_Or_Body
12389 (Loc,
12390 Name => Make_TSS_Name (Tag_Typ, Name),
12391 Tag_Typ => Tag_Typ,
12392 Profile => Build_Stream_Attr_Profile (Loc, Tag_Typ, Name),
12393 Ret_Type => Ret_Type,
12394 For_Body => False);
12395 end Predef_Stream_Attr_Spec;
12397 ----------------------------------
12398 -- Predefined_Primitive_Eq_Body --
12399 ----------------------------------
12401 procedure Predefined_Primitive_Eq_Body
12402 (Tag_Typ : Entity_Id;
12403 Predef_List : List_Id;
12404 Renamed_Eq : Entity_Id)
12406 Decl : Node_Id;
12407 Eq_Needed : Boolean;
12408 Eq_Name : Name_Id;
12409 Prim : Elmt_Id;
12411 begin
12412 -- See if we have a predefined "=" operator
12414 if Present (Renamed_Eq) then
12415 Eq_Needed := True;
12416 Eq_Name := Chars (Renamed_Eq);
12418 -- If the parent is an interface type then it has defined all the
12419 -- predefined primitives abstract and we need to check if the type
12420 -- has some user defined "=" function which matches the profile of
12421 -- the Ada predefined equality operator to avoid generating it.
12423 elsif Is_Interface (Etype (Tag_Typ)) then
12424 Eq_Needed := True;
12425 Eq_Name := Name_Op_Eq;
12427 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
12428 while Present (Prim) loop
12429 if Is_User_Defined_Equality (Node (Prim))
12430 and then not Is_Internal (Node (Prim))
12431 then
12432 Eq_Needed := False;
12433 Eq_Name := No_Name;
12434 exit;
12435 end if;
12437 Next_Elmt (Prim);
12438 end loop;
12440 else
12441 Eq_Needed := False;
12442 Eq_Name := No_Name;
12444 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
12445 while Present (Prim) loop
12446 if Is_User_Defined_Equality (Node (Prim))
12447 and then Is_Internal (Node (Prim))
12448 then
12449 Eq_Needed := True;
12450 Eq_Name := Name_Op_Eq;
12451 exit;
12452 end if;
12454 Next_Elmt (Prim);
12455 end loop;
12456 end if;
12458 -- If equality is needed, we will have its name
12460 pragma Assert (Eq_Needed = Present (Eq_Name));
12462 -- Body for equality
12464 if Eq_Needed then
12465 Decl := Make_Eq_Body (Tag_Typ, Eq_Name);
12466 Append_To (Predef_List, Decl);
12467 end if;
12469 -- Body for inequality (if required)
12471 Decl := Make_Neq_Body (Tag_Typ);
12473 if Present (Decl) then
12474 Append_To (Predef_List, Decl);
12475 end if;
12476 end Predefined_Primitive_Eq_Body;
12478 ---------------------------------
12479 -- Predefined_Primitive_Bodies --
12480 ---------------------------------
12482 function Predefined_Primitive_Bodies
12483 (Tag_Typ : Entity_Id;
12484 Renamed_Eq : Entity_Id) return List_Id
12486 Loc : constant Source_Ptr := Sloc (Tag_Typ);
12487 Res : constant List_Id := New_List;
12488 Adj_Call : Node_Id;
12489 Decl : Node_Id;
12490 Fin_Call : Node_Id;
12491 Ent : Entity_Id;
12493 pragma Warnings (Off, Ent);
12495 use Exp_Put_Image;
12497 begin
12498 pragma Assert (not Is_Interface (Tag_Typ));
12500 -- Body of _Size
12502 Decl := Predef_Spec_Or_Body (Loc,
12503 Tag_Typ => Tag_Typ,
12504 Name => Name_uSize,
12505 Profile => New_List (
12506 Make_Parameter_Specification (Loc,
12507 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
12508 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
12510 Ret_Type => Standard_Long_Long_Integer,
12511 For_Body => True);
12513 Set_Handled_Statement_Sequence (Decl,
12514 Make_Handled_Sequence_Of_Statements (Loc, New_List (
12515 Make_Simple_Return_Statement (Loc,
12516 Expression =>
12517 Make_Attribute_Reference (Loc,
12518 Prefix => Make_Identifier (Loc, Name_X),
12519 Attribute_Name => Name_Size)))));
12521 Append_To (Res, Decl);
12523 -- Body of Put_Image
12525 if No (TSS (Tag_Typ, TSS_Put_Image))
12526 and then not No_Run_Time_Mode
12527 and then RTE_Available (RE_Root_Buffer_Type)
12528 then
12529 Build_Record_Put_Image_Procedure (Loc, Tag_Typ, Decl, Ent);
12530 Append_To (Res, Decl);
12531 end if;
12533 -- Bodies for Dispatching stream IO routines. We need these only for
12534 -- non-limited types (in the limited case there is no dispatching).
12535 -- We also skip them if dispatching or finalization are not available
12536 -- or if stream operations are prohibited by restriction No_Streams or
12537 -- from use of pragma/aspect No_Tagged_Streams.
12539 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Read)
12540 and then No (TSS (Tag_Typ, TSS_Stream_Read))
12541 then
12542 Build_Record_Read_Procedure (Tag_Typ, Decl, Ent);
12543 Append_To (Res, Decl);
12544 end if;
12546 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Write)
12547 and then No (TSS (Tag_Typ, TSS_Stream_Write))
12548 then
12549 Build_Record_Write_Procedure (Tag_Typ, Decl, Ent);
12550 Append_To (Res, Decl);
12551 end if;
12553 -- Skip body of _Input for the abstract case, since the corresponding
12554 -- spec is abstract (see Predef_Spec_Or_Body).
12556 if not Is_Abstract_Type (Tag_Typ)
12557 and then Stream_Operation_OK (Tag_Typ, TSS_Stream_Input)
12558 and then No (TSS (Tag_Typ, TSS_Stream_Input))
12559 then
12560 Build_Record_Or_Elementary_Input_Function
12561 (Tag_Typ, Decl, Ent);
12562 Append_To (Res, Decl);
12563 end if;
12565 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Output)
12566 and then No (TSS (Tag_Typ, TSS_Stream_Output))
12567 then
12568 Build_Record_Or_Elementary_Output_Procedure (Tag_Typ, Decl, Ent);
12569 Append_To (Res, Decl);
12570 end if;
12572 -- Ada 2005: Generate bodies for the following primitive operations for
12573 -- limited interfaces and synchronized types that implement a limited
12574 -- interface.
12576 -- disp_asynchronous_select
12577 -- disp_conditional_select
12578 -- disp_get_prim_op_kind
12579 -- disp_get_task_id
12580 -- disp_timed_select
12582 -- The interface versions will have null bodies
12584 -- Disable the generation of these bodies if Ravenscar or ZFP is active
12586 -- In VM targets we define these primitives in all root tagged types
12587 -- that are not interface types. Done because in VM targets we don't
12588 -- have secondary dispatch tables and any derivation of Tag_Typ may
12589 -- cover limited interfaces (which always have these primitives since
12590 -- they may be ancestors of synchronized interface types).
12592 if Ada_Version >= Ada_2005
12593 and then
12594 ((Is_Interface (Etype (Tag_Typ))
12595 and then Is_Limited_Record (Etype (Tag_Typ)))
12596 or else
12597 (Is_Concurrent_Record_Type (Tag_Typ)
12598 and then Has_Interfaces (Tag_Typ))
12599 or else
12600 (not Tagged_Type_Expansion
12601 and then Tag_Typ = Root_Type (Tag_Typ)))
12602 and then not Restriction_Active (No_Select_Statements)
12603 and then RTE_Available (RE_Select_Specific_Data)
12604 then
12605 Append_To (Res, Make_Disp_Asynchronous_Select_Body (Tag_Typ));
12606 Append_To (Res, Make_Disp_Conditional_Select_Body (Tag_Typ));
12607 Append_To (Res, Make_Disp_Get_Prim_Op_Kind_Body (Tag_Typ));
12608 Append_To (Res, Make_Disp_Get_Task_Id_Body (Tag_Typ));
12609 Append_To (Res, Make_Disp_Requeue_Body (Tag_Typ));
12610 Append_To (Res, Make_Disp_Timed_Select_Body (Tag_Typ));
12611 end if;
12613 if not Is_Limited_Type (Tag_Typ) then
12614 -- Body for equality and inequality
12616 Predefined_Primitive_Eq_Body (Tag_Typ, Res, Renamed_Eq);
12618 -- Body for dispatching assignment
12620 Decl :=
12621 Predef_Spec_Or_Body (Loc,
12622 Tag_Typ => Tag_Typ,
12623 Name => Name_uAssign,
12624 Profile => New_List (
12625 Make_Parameter_Specification (Loc,
12626 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
12627 Out_Present => True,
12628 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
12630 Make_Parameter_Specification (Loc,
12631 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
12632 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
12633 For_Body => True);
12635 Set_Handled_Statement_Sequence (Decl,
12636 Make_Handled_Sequence_Of_Statements (Loc, New_List (
12637 Make_Assignment_Statement (Loc,
12638 Name => Make_Identifier (Loc, Name_X),
12639 Expression => Make_Identifier (Loc, Name_Y)))));
12641 Append_To (Res, Decl);
12642 end if;
12644 -- Generate empty bodies of routines Deep_Adjust and Deep_Finalize for
12645 -- tagged types which do not contain controlled components.
12647 -- Do not generate the routines if finalization is disabled
12649 if Restriction_Active (No_Finalization) then
12650 null;
12652 elsif not Has_Controlled_Component (Tag_Typ) then
12653 if not Is_Limited_Type (Tag_Typ) then
12654 Adj_Call := Empty;
12655 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust, True);
12657 if Is_Controlled (Tag_Typ) then
12658 Adj_Call :=
12659 Make_Adjust_Call (
12660 Obj_Ref => Make_Identifier (Loc, Name_V),
12661 Typ => Tag_Typ);
12662 end if;
12664 if No (Adj_Call) then
12665 Adj_Call := Make_Null_Statement (Loc);
12666 end if;
12668 Set_Handled_Statement_Sequence (Decl,
12669 Make_Handled_Sequence_Of_Statements (Loc,
12670 Statements => New_List (Adj_Call)));
12672 Append_To (Res, Decl);
12673 end if;
12675 Fin_Call := Empty;
12676 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize, True);
12678 if Is_Controlled (Tag_Typ) then
12679 Fin_Call :=
12680 Make_Final_Call
12681 (Obj_Ref => Make_Identifier (Loc, Name_V),
12682 Typ => Tag_Typ);
12683 end if;
12685 if No (Fin_Call) then
12686 Fin_Call := Make_Null_Statement (Loc);
12687 end if;
12689 Set_Handled_Statement_Sequence (Decl,
12690 Make_Handled_Sequence_Of_Statements (Loc,
12691 Statements => New_List (Fin_Call)));
12693 Append_To (Res, Decl);
12694 end if;
12696 return Res;
12697 end Predefined_Primitive_Bodies;
12699 ---------------------------------
12700 -- Predefined_Primitive_Freeze --
12701 ---------------------------------
12703 function Predefined_Primitive_Freeze
12704 (Tag_Typ : Entity_Id) return List_Id
12706 Res : constant List_Id := New_List;
12707 Prim : Elmt_Id;
12708 Frnodes : List_Id;
12710 begin
12711 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
12712 while Present (Prim) loop
12713 if Is_Predefined_Dispatching_Operation (Node (Prim)) then
12714 Frnodes := Freeze_Entity (Node (Prim), Tag_Typ);
12716 if Present (Frnodes) then
12717 Append_List_To (Res, Frnodes);
12718 end if;
12719 end if;
12721 Next_Elmt (Prim);
12722 end loop;
12724 return Res;
12725 end Predefined_Primitive_Freeze;
12727 -------------------------
12728 -- Stream_Operation_OK --
12729 -------------------------
12731 function Stream_Operation_OK
12732 (Typ : Entity_Id;
12733 Operation : TSS_Name_Type) return Boolean
12735 Has_Predefined_Or_Specified_Stream_Attribute : Boolean := False;
12737 begin
12738 -- Special case of a limited type extension: a default implementation
12739 -- of the stream attributes Read or Write exists if that attribute
12740 -- has been specified or is available for an ancestor type; a default
12741 -- implementation of the attribute Output (resp. Input) exists if the
12742 -- attribute has been specified or Write (resp. Read) is available for
12743 -- an ancestor type. The last condition only applies under Ada 2005.
12745 if Is_Limited_Type (Typ) and then Is_Tagged_Type (Typ) then
12746 if Operation = TSS_Stream_Read then
12747 Has_Predefined_Or_Specified_Stream_Attribute :=
12748 Has_Specified_Stream_Read (Typ);
12750 elsif Operation = TSS_Stream_Write then
12751 Has_Predefined_Or_Specified_Stream_Attribute :=
12752 Has_Specified_Stream_Write (Typ);
12754 elsif Operation = TSS_Stream_Input then
12755 Has_Predefined_Or_Specified_Stream_Attribute :=
12756 Has_Specified_Stream_Input (Typ)
12757 or else
12758 (Ada_Version >= Ada_2005
12759 and then Stream_Operation_OK (Typ, TSS_Stream_Read));
12761 elsif Operation = TSS_Stream_Output then
12762 Has_Predefined_Or_Specified_Stream_Attribute :=
12763 Has_Specified_Stream_Output (Typ)
12764 or else
12765 (Ada_Version >= Ada_2005
12766 and then Stream_Operation_OK (Typ, TSS_Stream_Write));
12767 end if;
12769 -- Case of inherited TSS_Stream_Read or TSS_Stream_Write
12771 if not Has_Predefined_Or_Specified_Stream_Attribute
12772 and then Is_Derived_Type (Typ)
12773 and then (Operation = TSS_Stream_Read
12774 or else Operation = TSS_Stream_Write)
12775 then
12776 Has_Predefined_Or_Specified_Stream_Attribute :=
12777 Present
12778 (Find_Inherited_TSS (Base_Type (Etype (Typ)), Operation));
12779 end if;
12780 end if;
12782 -- If the type is not limited, or else is limited but the attribute is
12783 -- explicitly specified or is predefined for the type, then return True,
12784 -- unless other conditions prevail, such as restrictions prohibiting
12785 -- streams or dispatching operations. We also return True for limited
12786 -- interfaces, because they may be extended by nonlimited types and
12787 -- permit inheritance in this case (addresses cases where an abstract
12788 -- extension doesn't get 'Input declared, as per comments below, but
12789 -- 'Class'Input must still be allowed). Note that attempts to apply
12790 -- stream attributes to a limited interface or its class-wide type
12791 -- (or limited extensions thereof) will still get properly rejected
12792 -- by Check_Stream_Attribute.
12794 -- We exclude the Input operation from being a predefined subprogram in
12795 -- the case where the associated type is an abstract extension, because
12796 -- the attribute is not callable in that case, per 13.13.2(49/2). Also,
12797 -- we don't want an abstract version created because types derived from
12798 -- the abstract type may not even have Input available (for example if
12799 -- derived from a private view of the abstract type that doesn't have
12800 -- a visible Input).
12802 -- Do not generate stream routines for type Finalization_Master because
12803 -- a master may never appear in types and therefore cannot be read or
12804 -- written.
12806 return
12807 (not Is_Limited_Type (Typ)
12808 or else Is_Interface (Typ)
12809 or else Has_Predefined_Or_Specified_Stream_Attribute)
12810 and then
12811 (Operation /= TSS_Stream_Input
12812 or else not Is_Abstract_Type (Typ)
12813 or else not Is_Derived_Type (Typ))
12814 and then not Has_Unknown_Discriminants (Typ)
12815 and then not Is_Concurrent_Interface (Typ)
12816 and then not Restriction_Active (No_Streams)
12817 and then not Restriction_Active (No_Dispatch)
12818 and then No (No_Tagged_Streams_Pragma (Typ))
12819 and then not No_Run_Time_Mode
12820 and then RTE_Available (RE_Tag)
12821 and then No (Type_Without_Stream_Operation (Typ))
12822 and then RTE_Available (RE_Root_Stream_Type)
12823 and then not Is_RTE (Typ, RE_Finalization_Master);
12824 end Stream_Operation_OK;
12826 end Exp_Ch3;