testsuite: 32 bit AIX 2 byte wchar
[official-gcc.git] / gcc / ada / exp_ch3.adb
blobd616c5cba9fcb6d5612f409a1eb60afaf47247df
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_Constr_Array_Subt_With_Bounds (Typ)
8111 and then Is_Constrained (Etype (Expr_Q)))
8113 -- We may use a renaming if the initialization expression is a
8114 -- captured function call that meets a few conditions.
8116 and then
8117 (Is_Renamable_Function_Call (Expr_Q)
8119 -- Or else if it is a variable with OK_To_Rename set
8121 or else (OK_To_Rename_Ref (Expr_Q)
8122 and then not Special_Ret_Obj)
8124 -- Or else if it is a slice of such a variable
8126 or else (Nkind (Expr_Q) = N_Slice
8127 and then OK_To_Rename_Ref (Prefix (Expr_Q))
8128 and then not Special_Ret_Obj));
8130 -- If the type needs finalization and is not inherently limited,
8131 -- then the target is adjusted after the copy and attached to the
8132 -- finalization list. However, no adjustment is needed in the case
8133 -- where the object has been initialized by a call to a function
8134 -- returning on the primary stack (see Expand_Ctrl_Function_Call)
8135 -- since no copy occurred, given that the type is by-reference.
8136 -- Similarly, no adjustment is needed if we are going to rewrite
8137 -- the object declaration into a renaming declaration.
8139 if Needs_Finalization (Typ)
8140 and then not Is_Inherently_Limited_Type (Typ)
8141 and then Nkind (Expr_Q) /= N_Function_Call
8142 and then not Rewrite_As_Renaming
8143 then
8144 Adj_Call :=
8145 Make_Adjust_Call (
8146 Obj_Ref => New_Occurrence_Of (Def_Id, Loc),
8147 Typ => Base_Typ);
8149 if Present (Adj_Call) and then not Special_Ret_Obj then
8150 Insert_Action_After (Init_After, Adj_Call);
8151 end if;
8152 end if;
8153 end if;
8155 -- Cases where the back end cannot handle the initialization
8156 -- directly. In such cases, we expand an assignment that will
8157 -- be appropriately handled by Expand_N_Assignment_Statement.
8159 -- The exclusion of the unconstrained case is wrong, but for now it
8160 -- is too much trouble ???
8162 if (Is_Possibly_Unaligned_Slice (Expr)
8163 or else (Is_Possibly_Unaligned_Object (Expr)
8164 and then not Represented_As_Scalar (Etype (Expr))))
8165 and then not (Is_Array_Type (Etype (Expr))
8166 and then not Is_Constrained (Etype (Expr)))
8167 then
8168 declare
8169 Stat : constant Node_Id :=
8170 Make_Assignment_Statement (Loc,
8171 Name => New_Occurrence_Of (Def_Id, Loc),
8172 Expression => Relocate_Node (Expr));
8173 begin
8174 Set_Assignment_OK (Name (Stat));
8175 Set_No_Ctrl_Actions (Stat);
8176 Insert_Action_After (Init_After, Stat);
8177 Set_Expression (N, Empty);
8178 Set_No_Initialization (N);
8179 end;
8180 end if;
8181 end if;
8183 if Nkind (Obj_Def) = N_Access_Definition
8184 and then not Is_Local_Anonymous_Access (Typ)
8185 then
8186 -- An Ada 2012 stand-alone object of an anonymous access type
8188 declare
8189 Loc : constant Source_Ptr := Sloc (N);
8191 Level : constant Entity_Id :=
8192 Make_Defining_Identifier (Sloc (N),
8193 Chars =>
8194 New_External_Name (Chars (Def_Id), Suffix => "L"));
8196 Level_Decl : Node_Id;
8197 Level_Expr : Node_Id;
8199 begin
8200 Mutate_Ekind (Level, Ekind (Def_Id));
8201 Set_Etype (Level, Standard_Natural);
8202 Set_Scope (Level, Scope (Def_Id));
8204 -- Set accessibility level of null
8206 if No (Expr) then
8207 Level_Expr :=
8208 Make_Integer_Literal
8209 (Loc, Scope_Depth (Standard_Standard));
8211 -- When the expression of the object is a function which returns
8212 -- an anonymous access type the master of the call is the object
8213 -- being initialized instead of the type.
8215 elsif Nkind (Expr) = N_Function_Call
8216 and then Ekind (Etype (Name (Expr))) = E_Anonymous_Access_Type
8217 then
8218 Level_Expr := Accessibility_Level
8219 (Def_Id, Object_Decl_Level);
8221 -- General case
8223 else
8224 Level_Expr := Accessibility_Level (Expr, Dynamic_Level);
8225 end if;
8227 Level_Decl :=
8228 Make_Object_Declaration (Loc,
8229 Defining_Identifier => Level,
8230 Object_Definition =>
8231 New_Occurrence_Of (Standard_Natural, Loc),
8232 Expression => Level_Expr,
8233 Constant_Present => Constant_Present (N),
8234 Has_Init_Expression => True);
8236 Insert_Action_After (Init_After, Level_Decl);
8238 Set_Extra_Accessibility (Def_Id, Level);
8239 end;
8240 end if;
8242 -- If the object is default initialized and its type is subject to
8243 -- pragma Default_Initial_Condition, add a runtime check to verify
8244 -- the assumption of the pragma (SPARK RM 7.3.3). Generate:
8246 -- <Base_Typ>DIC (<Base_Typ> (Def_Id));
8248 -- Note that the check is generated for source objects only
8250 if Comes_From_Source (Def_Id)
8251 and then Has_DIC (Typ)
8252 and then Present (DIC_Procedure (Typ))
8253 and then not Has_Null_Body (DIC_Procedure (Typ))
8254 and then not Has_Init_Expression (N)
8255 and then No (Expr)
8256 and then not Is_Imported (Def_Id)
8257 then
8258 declare
8259 DIC_Call : constant Node_Id :=
8260 Build_DIC_Call
8261 (Loc, New_Occurrence_Of (Def_Id, Loc), Typ);
8262 begin
8263 if Present (Next_N) then
8264 Insert_Before_And_Analyze (Next_N, DIC_Call);
8266 -- The object declaration is the last node in a declarative or a
8267 -- statement list.
8269 else
8270 Append_To (List_Containing (N), DIC_Call);
8271 Analyze (DIC_Call);
8272 end if;
8273 end;
8274 end if;
8276 -- If this is the return object of a build-in-place function, locate the
8277 -- implicit BIPaccess parameter designating the caller-supplied return
8278 -- object and convert the declaration to a renaming of a dereference of
8279 -- this parameter. If the declaration includes an expression, add an
8280 -- assignment statement to ensure the return object gets initialized.
8282 -- Result : T [:= <expression>];
8284 -- is converted to
8286 -- Result : T renames BIPaccess.all;
8287 -- [Result := <expression>;]
8289 -- in the constrained case, or to
8291 -- type Txx is access all ...;
8292 -- Rxx : Txx := null;
8294 -- if BIPalloc = 1 then
8295 -- Rxx := BIPaccess;
8296 -- Rxx.all := <expression>;
8297 -- elsif BIPalloc = 2 then
8298 -- Rxx := new <expression-type>'(<expression>)[storage_pool =
8299 -- system__secondary_stack__ss_pool][procedure_to_call =
8300 -- system__secondary_stack__ss_allocate];
8301 -- elsif BIPalloc = 3 then
8302 -- Rxx := new <expression-type>'(<expression>)
8303 -- elsif BIPalloc = 4 then
8304 -- Pxx : system__storage_pools__root_storage_pool renames
8305 -- BIPstoragepool.all;
8306 -- Rxx := new <expression-type>'(<expression>)[storage_pool =
8307 -- Pxx][procedure_to_call =
8308 -- system__storage_pools__allocate_any];
8309 -- else
8310 -- [program_error "build in place mismatch"]
8311 -- end if;
8313 -- Result : T renames Rxx.all;
8315 -- in the unconstrained case.
8317 if Is_Build_In_Place_Return_Object (Def_Id) then
8318 declare
8319 Init_Stmt : Node_Id;
8320 Obj_Acc_Formal : Entity_Id;
8322 begin
8323 -- Retrieve the implicit access parameter passed by the caller
8325 Obj_Acc_Formal :=
8326 Build_In_Place_Formal (Func_Id, BIP_Object_Access);
8328 -- If the return object's declaration includes an expression
8329 -- and the declaration isn't marked as No_Initialization, then
8330 -- we need to generate an assignment to the object and insert
8331 -- it after the declaration before rewriting it as a renaming
8332 -- (otherwise we'll lose the initialization). The case where
8333 -- the result type is an interface (or class-wide interface)
8334 -- is also excluded because the context of the function call
8335 -- must be unconstrained, so the initialization will always
8336 -- be done as part of an allocator evaluation (storage pool
8337 -- or secondary stack), never to a constrained target object
8338 -- passed in by the caller. Besides the assignment being
8339 -- unneeded in this case, it avoids problems with trying to
8340 -- generate a dispatching assignment when the return expression
8341 -- is a nonlimited descendant of a limited interface (the
8342 -- interface has no assignment operation).
8344 if Present (Expr_Q)
8345 and then not Is_Delayed_Aggregate (Expr_Q)
8346 and then not No_Initialization (N)
8347 and then not Is_Interface (Typ)
8348 then
8349 if Is_Class_Wide_Type (Typ)
8350 and then not Is_Class_Wide_Type (Etype (Expr_Q))
8351 then
8352 Init_Stmt :=
8353 Make_Assignment_Statement (Loc,
8354 Name => New_Occurrence_Of (Def_Id, Loc),
8355 Expression =>
8356 Make_Type_Conversion (Loc,
8357 Subtype_Mark =>
8358 New_Occurrence_Of (Typ, Loc),
8359 Expression => New_Copy_Tree (Expr_Q)));
8361 else
8362 Init_Stmt :=
8363 Make_Assignment_Statement (Loc,
8364 Name => New_Occurrence_Of (Def_Id, Loc),
8365 Expression => New_Copy_Tree (Expr_Q));
8366 end if;
8368 Set_Assignment_OK (Name (Init_Stmt));
8369 Set_No_Ctrl_Actions (Init_Stmt);
8371 else
8372 Init_Stmt := Empty;
8373 end if;
8375 -- When the function's subtype is unconstrained, a run-time
8376 -- test may be needed to decide the form of allocation to use
8377 -- for the return object. The function has an implicit formal
8378 -- parameter indicating this. If the BIP_Alloc_Form formal has
8379 -- the value one, then the caller has passed access to an
8380 -- existing object for use as the return object. If the value
8381 -- is two, then the return object must be allocated on the
8382 -- secondary stack. If the value is three, then the return
8383 -- object must be allocated on the heap. Otherwise, the object
8384 -- must be allocated in a storage pool. We generate an if
8385 -- statement to test the BIP_Alloc_Form formal and initialize
8386 -- a local access value appropriately.
8388 if Needs_BIP_Alloc_Form (Func_Id) then
8389 declare
8390 Desig_Typ : constant Entity_Id :=
8391 (if Ekind (Typ) = E_Array_Subtype
8392 then Etype (Func_Id) else Typ);
8393 -- Ensure that the we use a fat pointer when allocating
8394 -- an unconstrained array on the heap. In this case the
8395 -- result object's type is a constrained array type even
8396 -- though the function's type is unconstrained.
8398 Obj_Alloc_Formal : constant Entity_Id :=
8399 Build_In_Place_Formal (Func_Id, BIP_Alloc_Form);
8400 Pool_Id : constant Entity_Id :=
8401 Make_Temporary (Loc, 'P');
8403 Acc_Typ : Entity_Id;
8404 Alloc_Obj_Decl : Node_Id;
8405 Alloc_Obj_Id : Entity_Id;
8406 Alloc_Stmt : Node_Id;
8407 Guard_Except : Node_Id;
8408 Heap_Allocator : Node_Id;
8409 Pool_Allocator : Node_Id;
8410 Pool_Decl : Node_Id;
8411 Ptr_Typ_Decl : Node_Id;
8412 SS_Allocator : Node_Id;
8414 begin
8415 -- Create an access type designating the function's
8416 -- result subtype.
8418 Acc_Typ := Make_Temporary (Loc, 'A');
8420 Ptr_Typ_Decl :=
8421 Make_Full_Type_Declaration (Loc,
8422 Defining_Identifier => Acc_Typ,
8423 Type_Definition =>
8424 Make_Access_To_Object_Definition (Loc,
8425 All_Present => True,
8426 Subtype_Indication =>
8427 New_Occurrence_Of (Desig_Typ, Loc)));
8429 Insert_Action (N, Ptr_Typ_Decl, Suppress => All_Checks);
8431 -- Create an access object that will be initialized to an
8432 -- access value denoting the return object, either coming
8433 -- from an implicit access value passed in by the caller
8434 -- or from the result of an allocator.
8436 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
8438 Alloc_Obj_Decl :=
8439 Make_Object_Declaration (Loc,
8440 Defining_Identifier => Alloc_Obj_Id,
8441 Object_Definition =>
8442 New_Occurrence_Of (Acc_Typ, Loc));
8444 Insert_Action (N, Alloc_Obj_Decl, Suppress => All_Checks);
8446 -- First create the Heap_Allocator
8448 Heap_Allocator := Make_Allocator_For_Return (Expr_Q);
8450 -- The Pool_Allocator is just like the Heap_Allocator,
8451 -- except we set Storage_Pool and Procedure_To_Call so
8452 -- it will use the user-defined storage pool.
8454 Pool_Allocator := Make_Allocator_For_Return (Expr_Q);
8456 -- Do not generate the renaming of the build-in-place
8457 -- pool parameter on ZFP because the parameter is not
8458 -- created in the first place.
8460 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
8461 Pool_Decl :=
8462 Make_Object_Renaming_Declaration (Loc,
8463 Defining_Identifier => Pool_Id,
8464 Subtype_Mark =>
8465 New_Occurrence_Of
8466 (RTE (RE_Root_Storage_Pool), Loc),
8467 Name =>
8468 Make_Explicit_Dereference (Loc,
8469 New_Occurrence_Of
8470 (Build_In_Place_Formal
8471 (Func_Id, BIP_Storage_Pool), Loc)));
8472 Set_Storage_Pool (Pool_Allocator, Pool_Id);
8473 Set_Procedure_To_Call
8474 (Pool_Allocator, RTE (RE_Allocate_Any));
8475 else
8476 Pool_Decl := Make_Null_Statement (Loc);
8477 end if;
8479 -- If the No_Allocators restriction is active, then only
8480 -- an allocator for secondary stack allocation is needed.
8481 -- It's OK for such allocators to have Comes_From_Source
8482 -- set to False, because gigi knows not to flag them as
8483 -- being a violation of No_Implicit_Heap_Allocations.
8485 if Restriction_Active (No_Allocators) then
8486 SS_Allocator := Heap_Allocator;
8487 Heap_Allocator := Make_Null (Loc);
8488 Pool_Allocator := Make_Null (Loc);
8490 -- Otherwise the heap and pool allocators may be needed,
8491 -- so we make another allocator for secondary stack
8492 -- allocation.
8494 else
8495 SS_Allocator := Make_Allocator_For_Return (Expr_Q);
8497 -- The heap and pool allocators are marked as
8498 -- Comes_From_Source since they correspond to an
8499 -- explicit user-written allocator (that is, it will
8500 -- only be executed on behalf of callers that call the
8501 -- function as initialization for such an allocator).
8502 -- Prevents errors when No_Implicit_Heap_Allocations
8503 -- is in force.
8505 Set_Comes_From_Source (Heap_Allocator, True);
8506 Set_Comes_From_Source (Pool_Allocator, True);
8507 end if;
8509 -- The allocator is returned on the secondary stack
8511 Check_Restriction (No_Secondary_Stack, N);
8512 Set_Storage_Pool (SS_Allocator, RTE (RE_SS_Pool));
8513 Set_Procedure_To_Call
8514 (SS_Allocator, RTE (RE_SS_Allocate));
8516 -- The allocator is returned on the secondary stack,
8517 -- so indicate that the function return, as well as
8518 -- all blocks that encloses the allocator, must not
8519 -- release it. The flags must be set now because
8520 -- the decision to use the secondary stack is done
8521 -- very late in the course of expanding the return
8522 -- statement, past the point where these flags are
8523 -- normally set.
8525 Set_Uses_Sec_Stack (Func_Id);
8526 Set_Uses_Sec_Stack (Scope (Def_Id));
8527 Set_Sec_Stack_Needed_For_Return (Scope (Def_Id));
8529 -- Guard against poor expansion on the caller side by
8530 -- using a raise statement to catch out-of-range values
8531 -- of formal parameter BIP_Alloc_Form.
8533 if Exceptions_OK then
8534 Guard_Except :=
8535 Make_Raise_Program_Error (Loc,
8536 Reason => PE_Build_In_Place_Mismatch);
8537 else
8538 Guard_Except := Make_Null_Statement (Loc);
8539 end if;
8541 -- Create an if statement to test the BIP_Alloc_Form
8542 -- formal and initialize the access object to either the
8543 -- BIP_Object_Access formal (BIP_Alloc_Form =
8544 -- Caller_Allocation), the result of allocating the
8545 -- object in the secondary stack (BIP_Alloc_Form =
8546 -- Secondary_Stack), or else an allocator to create the
8547 -- return object in the heap or user-defined pool
8548 -- (BIP_Alloc_Form = Global_Heap or User_Storage_Pool).
8550 -- ??? An unchecked type conversion must be made in the
8551 -- case of assigning the access object formal to the
8552 -- local access object, because a normal conversion would
8553 -- be illegal in some cases (such as converting access-
8554 -- to-unconstrained to access-to-constrained), but the
8555 -- the unchecked conversion will presumably fail to work
8556 -- right in just such cases. It's not clear at all how to
8557 -- handle this.
8559 Alloc_Stmt :=
8560 Make_If_Statement (Loc,
8561 Condition =>
8562 Make_Op_Eq (Loc,
8563 Left_Opnd =>
8564 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
8565 Right_Opnd =>
8566 Make_Integer_Literal (Loc,
8567 UI_From_Int (BIP_Allocation_Form'Pos
8568 (Caller_Allocation)))),
8570 Then_Statements => New_List (
8571 Make_Assignment_Statement (Loc,
8572 Name =>
8573 New_Occurrence_Of (Alloc_Obj_Id, Loc),
8574 Expression =>
8575 Unchecked_Convert_To
8576 (Acc_Typ,
8577 New_Occurrence_Of (Obj_Acc_Formal, Loc)))),
8579 Elsif_Parts => New_List (
8580 Make_Elsif_Part (Loc,
8581 Condition =>
8582 Make_Op_Eq (Loc,
8583 Left_Opnd =>
8584 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
8585 Right_Opnd =>
8586 Make_Integer_Literal (Loc,
8587 UI_From_Int (BIP_Allocation_Form'Pos
8588 (Secondary_Stack)))),
8590 Then_Statements => New_List (
8591 Make_Assignment_Statement (Loc,
8592 Name =>
8593 New_Occurrence_Of (Alloc_Obj_Id, Loc),
8594 Expression => SS_Allocator))),
8596 Make_Elsif_Part (Loc,
8597 Condition =>
8598 Make_Op_Eq (Loc,
8599 Left_Opnd =>
8600 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
8601 Right_Opnd =>
8602 Make_Integer_Literal (Loc,
8603 UI_From_Int (BIP_Allocation_Form'Pos
8604 (Global_Heap)))),
8606 Then_Statements => New_List (
8607 Build_Heap_Or_Pool_Allocator
8608 (Temp_Id => Alloc_Obj_Id,
8609 Temp_Typ => Acc_Typ,
8610 Ret_Typ => Desig_Typ,
8611 Alloc_Expr => Heap_Allocator))),
8613 -- ??? If all is well, we can put the following
8614 -- 'elsif' in the 'else', but this is a useful
8615 -- self-check in case caller and callee don't agree
8616 -- on whether BIPAlloc and so on should be passed.
8618 Make_Elsif_Part (Loc,
8619 Condition =>
8620 Make_Op_Eq (Loc,
8621 Left_Opnd =>
8622 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
8623 Right_Opnd =>
8624 Make_Integer_Literal (Loc,
8625 UI_From_Int (BIP_Allocation_Form'Pos
8626 (User_Storage_Pool)))),
8628 Then_Statements => New_List (
8629 Pool_Decl,
8630 Build_Heap_Or_Pool_Allocator
8631 (Temp_Id => Alloc_Obj_Id,
8632 Temp_Typ => Acc_Typ,
8633 Ret_Typ => Desig_Typ,
8634 Alloc_Expr => Pool_Allocator)))),
8636 -- Raise Program_Error if it's none of the above;
8637 -- this is a compiler bug.
8639 Else_Statements => New_List (Guard_Except));
8641 -- If a separate initialization assignment was created
8642 -- earlier, append that following the assignment of the
8643 -- implicit access formal to the access object, to ensure
8644 -- that the return object is initialized in that case. In
8645 -- this situation, the target of the assignment must be
8646 -- rewritten to denote a dereference of the access to the
8647 -- return object passed in by the caller.
8649 if Present (Init_Stmt) then
8650 Set_Name (Init_Stmt,
8651 Make_Explicit_Dereference (Loc,
8652 Prefix => New_Occurrence_Of (Alloc_Obj_Id, Loc)));
8653 Set_Assignment_OK (Name (Init_Stmt));
8655 Append_To (Then_Statements (Alloc_Stmt), Init_Stmt);
8656 Init_Stmt := Empty;
8657 end if;
8659 Insert_Action (N, Alloc_Stmt, Suppress => All_Checks);
8661 -- From now on, the type of the return object is the
8662 -- designated type.
8664 if Desig_Typ /= Typ then
8665 Set_Etype (Def_Id, Desig_Typ);
8666 Set_Actual_Subtype (Def_Id, Typ);
8667 end if;
8669 -- Remember the local access object for use in the
8670 -- dereference of the renaming created below.
8672 Obj_Acc_Formal := Alloc_Obj_Id;
8673 end;
8675 -- When the function's type is unconstrained and a run-time test
8676 -- is not needed, we nevertheless need to build the return using
8677 -- the return object's type.
8679 elsif not Is_Constrained (Underlying_Type (Etype (Func_Id))) then
8680 declare
8681 Acc_Typ : Entity_Id;
8682 Alloc_Obj_Decl : Node_Id;
8683 Alloc_Obj_Id : Entity_Id;
8684 Ptr_Typ_Decl : Node_Id;
8686 begin
8687 -- Create an access type designating the function's
8688 -- result subtype.
8690 Acc_Typ := Make_Temporary (Loc, 'A');
8692 Ptr_Typ_Decl :=
8693 Make_Full_Type_Declaration (Loc,
8694 Defining_Identifier => Acc_Typ,
8695 Type_Definition =>
8696 Make_Access_To_Object_Definition (Loc,
8697 All_Present => True,
8698 Subtype_Indication =>
8699 New_Occurrence_Of (Typ, Loc)));
8701 Insert_Action (N, Ptr_Typ_Decl, Suppress => All_Checks);
8703 -- Create an access object initialized to the conversion
8704 -- of the implicit access value passed in by the caller.
8706 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
8708 -- See the ??? comment a few lines above about the use of
8709 -- an unchecked conversion here.
8711 Alloc_Obj_Decl :=
8712 Make_Object_Declaration (Loc,
8713 Defining_Identifier => Alloc_Obj_Id,
8714 Constant_Present => True,
8715 Object_Definition =>
8716 New_Occurrence_Of (Acc_Typ, Loc),
8717 Expression =>
8718 Unchecked_Convert_To
8719 (Acc_Typ, New_Occurrence_Of (Obj_Acc_Formal, Loc)));
8721 Insert_Action (N, Alloc_Obj_Decl, Suppress => All_Checks);
8723 -- Remember the local access object for use in the
8724 -- dereference of the renaming created below.
8726 Obj_Acc_Formal := Alloc_Obj_Id;
8727 end;
8728 end if;
8730 -- Initialize the object now that it has got its final subtype,
8731 -- but before rewriting it as a renaming.
8733 Initialize_Return_Object
8734 (Tag_Assign, Adj_Call, Expr_Q, Init_Stmt, Init_After);
8736 -- Replace the return object declaration with a renaming of a
8737 -- dereference of the access value designating the return object.
8739 Expr_Q :=
8740 Make_Explicit_Dereference (Loc,
8741 Prefix => New_Occurrence_Of (Obj_Acc_Formal, Loc));
8742 Set_Etype (Expr_Q, Etype (Def_Id));
8744 Rewrite_As_Renaming := True;
8745 end;
8747 -- If we can rename the initialization expression, we need to make sure
8748 -- that we use the proper type in the case of a return object that lives
8749 -- on the secondary stack (see other cases below for a similar handling)
8750 -- and that the tag is assigned in the case of any return object.
8752 elsif Rewrite_As_Renaming then
8753 if Special_Ret_Obj then
8754 declare
8755 Desig_Typ : constant Entity_Id :=
8756 (if Ekind (Typ) = E_Array_Subtype
8757 then Etype (Func_Id) else Typ);
8759 begin
8760 -- From now on, the type of the return object is the
8761 -- designated type.
8763 if Desig_Typ /= Typ then
8764 Set_Etype (Def_Id, Desig_Typ);
8765 Set_Actual_Subtype (Def_Id, Typ);
8766 end if;
8768 if Present (Tag_Assign) then
8769 Insert_Action_After (Init_After, Tag_Assign);
8770 end if;
8772 -- Ada 2005 (AI95-344): If the result type is class-wide,
8773 -- insert a check that the level of the return expression's
8774 -- underlying type is not deeper than the level of the master
8775 -- enclosing the function.
8777 -- AI12-043: The check is made immediately after the return
8778 -- object is created.
8780 if Is_Class_Wide_Type (Etype (Func_Id)) then
8781 Apply_CW_Accessibility_Check (Expr_Q, Func_Id);
8782 end if;
8783 end;
8784 end if;
8786 -- If this is the return object of a function returning on the secondary
8787 -- stack, convert the declaration to a renaming of the dereference of ah
8788 -- allocator for the secondary stack.
8790 -- Result : T [:= <expression>];
8792 -- is converted to
8794 -- type Txx is access all ...;
8795 -- Rxx : constant Txx :=
8796 -- new <expression-type>['(<expression>)][storage_pool =
8797 -- system__secondary_stack__ss_pool][procedure_to_call =
8798 -- system__secondary_stack__ss_allocate];
8800 -- Result : T renames Rxx.all;
8802 elsif Is_Secondary_Stack_Return_Object (Def_Id) then
8803 declare
8804 Desig_Typ : constant Entity_Id :=
8805 (if Ekind (Typ) = E_Array_Subtype
8806 then Etype (Func_Id) else Typ);
8807 -- Ensure that the we use a fat pointer when allocating
8808 -- an unconstrained array on the heap. In this case the
8809 -- result object's type is a constrained array type even
8810 -- though the function's type is unconstrained.
8812 Acc_Typ : Entity_Id;
8813 Alloc_Obj_Decl : Node_Id;
8814 Alloc_Obj_Id : Entity_Id;
8815 Ptr_Type_Decl : Node_Id;
8817 begin
8818 -- Create an access type designating the function's
8819 -- result subtype.
8821 Acc_Typ := Make_Temporary (Loc, 'A');
8823 Ptr_Type_Decl :=
8824 Make_Full_Type_Declaration (Loc,
8825 Defining_Identifier => Acc_Typ,
8826 Type_Definition =>
8827 Make_Access_To_Object_Definition (Loc,
8828 All_Present => True,
8829 Subtype_Indication =>
8830 New_Occurrence_Of (Desig_Typ, Loc)));
8832 Insert_Action (N, Ptr_Type_Decl, Suppress => All_Checks);
8834 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
8836 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
8838 Alloc_Obj_Decl :=
8839 Make_Object_Declaration (Loc,
8840 Defining_Identifier => Alloc_Obj_Id,
8841 Constant_Present => True,
8842 Object_Definition =>
8843 New_Occurrence_Of (Acc_Typ, Loc),
8844 Expression => Make_Allocator_For_Return (Expr_Q));
8846 Insert_Action (N, Alloc_Obj_Decl, Suppress => All_Checks);
8848 Set_Uses_Sec_Stack (Func_Id);
8849 Set_Uses_Sec_Stack (Scope (Def_Id));
8850 Set_Sec_Stack_Needed_For_Return (Scope (Def_Id));
8852 -- From now on, the type of the return object is the
8853 -- designated type.
8855 if Desig_Typ /= Typ then
8856 Set_Etype (Def_Id, Desig_Typ);
8857 Set_Actual_Subtype (Def_Id, Typ);
8858 end if;
8860 -- Initialize the object now that it has got its final subtype,
8861 -- but before rewriting it as a renaming.
8863 Initialize_Return_Object
8864 (Tag_Assign, Adj_Call, Expr_Q, Empty, Init_After);
8866 -- Replace the return object declaration with a renaming of a
8867 -- dereference of the access value designating the return object.
8869 Expr_Q :=
8870 Make_Explicit_Dereference (Loc,
8871 Prefix => New_Occurrence_Of (Alloc_Obj_Id, Loc));
8872 Set_Etype (Expr_Q, Etype (Def_Id));
8874 Rewrite_As_Renaming := True;
8875 end;
8877 -- If this is the return object of a function returning a by-reference
8878 -- type, convert the declaration to a renaming of the dereference of ah
8879 -- allocator for the return stack.
8881 -- Result : T [:= <expression>];
8883 -- is converted to
8885 -- type Txx is access all ...;
8886 -- Rxx : constant Txx :=
8887 -- new <expression-type>['(<expression>)][storage_pool =
8888 -- system__return_stack__rs_pool][procedure_to_call =
8889 -- system__return_stack__rs_allocate];
8891 -- Result : T renames Rxx.all;
8893 elsif Back_End_Return_Slot
8894 and then Is_By_Reference_Return_Object (Def_Id)
8895 then
8896 declare
8897 Acc_Typ : Entity_Id;
8898 Alloc_Obj_Decl : Node_Id;
8899 Alloc_Obj_Id : Entity_Id;
8900 Ptr_Type_Decl : Node_Id;
8902 begin
8903 -- Create an access type designating the function's
8904 -- result subtype.
8906 Acc_Typ := Make_Temporary (Loc, 'A');
8908 Ptr_Type_Decl :=
8909 Make_Full_Type_Declaration (Loc,
8910 Defining_Identifier => Acc_Typ,
8911 Type_Definition =>
8912 Make_Access_To_Object_Definition (Loc,
8913 All_Present => True,
8914 Subtype_Indication =>
8915 New_Occurrence_Of (Typ, Loc)));
8917 Insert_Action (N, Ptr_Type_Decl, Suppress => All_Checks);
8919 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_RS_Pool));
8921 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
8923 Alloc_Obj_Decl :=
8924 Make_Object_Declaration (Loc,
8925 Defining_Identifier => Alloc_Obj_Id,
8926 Constant_Present => True,
8927 Object_Definition =>
8928 New_Occurrence_Of (Acc_Typ, Loc),
8929 Expression => Make_Allocator_For_Return (Expr_Q));
8931 Insert_Action (N, Alloc_Obj_Decl, Suppress => All_Checks);
8933 -- Initialize the object now that it has got its final subtype,
8934 -- but before rewriting it as a renaming.
8936 Initialize_Return_Object
8937 (Tag_Assign, Adj_Call, Expr_Q, Empty, Init_After);
8939 -- Replace the return object declaration with a renaming of a
8940 -- dereference of the access value designating the return object.
8942 Expr_Q :=
8943 Make_Explicit_Dereference (Loc,
8944 Prefix => New_Occurrence_Of (Alloc_Obj_Id, Loc));
8945 Set_Etype (Expr_Q, Etype (Def_Id));
8947 Rewrite_As_Renaming := True;
8948 end;
8949 end if;
8951 -- Final transformation - turn the object declaration into a renaming
8952 -- if appropriate. If this is the completion of a deferred constant
8953 -- declaration, then this transformation generates what would be
8954 -- illegal code if written by hand, but that's OK.
8956 if Rewrite_As_Renaming then
8957 Rewrite (N,
8958 Make_Object_Renaming_Declaration (Loc,
8959 Defining_Identifier => Def_Id,
8960 Subtype_Mark => New_Occurrence_Of (Etype (Def_Id), Loc),
8961 Name => Expr_Q));
8963 -- Keep original aspects
8965 Move_Aspects (Original_Node (N), N);
8967 -- We do not analyze this renaming declaration, because all its
8968 -- components have already been analyzed, and if we were to go
8969 -- ahead and analyze it, we would in effect be trying to generate
8970 -- another declaration of X, which won't do.
8972 Set_Renamed_Object (Def_Id, Expr_Q);
8973 Set_Analyzed (N);
8975 -- We do need to deal with debug issues for this renaming
8977 -- First, if entity comes from source, then mark it as needing
8978 -- debug information, even though it is defined by a generated
8979 -- renaming that does not come from source.
8981 Set_Debug_Info_Defining_Id (N);
8983 -- Now call the routine to generate debug info for the renaming
8985 Insert_Action (N, Debug_Renaming_Declaration (N));
8986 end if;
8988 -- Exception on library entity not available
8990 exception
8991 when RE_Not_Available =>
8992 return;
8993 end Expand_N_Object_Declaration;
8995 ---------------------------------
8996 -- Expand_N_Subtype_Indication --
8997 ---------------------------------
8999 -- Add a check on the range of the subtype and deal with validity checking
9001 procedure Expand_N_Subtype_Indication (N : Node_Id) is
9002 Ran : constant Node_Id := Range_Expression (Constraint (N));
9003 Typ : constant Entity_Id := Entity (Subtype_Mark (N));
9005 begin
9006 if Nkind (Constraint (N)) = N_Range_Constraint then
9007 Validity_Check_Range (Range_Expression (Constraint (N)));
9008 end if;
9010 -- Do not duplicate the work of Process_Range_Expr_In_Decl in Sem_Ch3
9012 if Nkind (Parent (N)) in N_Constrained_Array_Definition | N_Slice
9013 and then Nkind (Parent (Parent (N))) not in
9014 N_Full_Type_Declaration | N_Object_Declaration
9015 then
9016 Apply_Range_Check (Ran, Typ);
9017 end if;
9018 end Expand_N_Subtype_Indication;
9020 ---------------------------
9021 -- Expand_N_Variant_Part --
9022 ---------------------------
9024 -- Note: this procedure no longer has any effect. It used to be that we
9025 -- would replace the choices in the last variant by a when others, and
9026 -- also expanded static predicates in variant choices here, but both of
9027 -- those activities were being done too early, since we can't check the
9028 -- choices until the statically predicated subtypes are frozen, which can
9029 -- happen as late as the free point of the record, and we can't change the
9030 -- last choice to an others before checking the choices, which is now done
9031 -- at the freeze point of the record.
9033 procedure Expand_N_Variant_Part (N : Node_Id) is
9034 begin
9035 null;
9036 end Expand_N_Variant_Part;
9038 ---------------------------------
9039 -- Expand_Previous_Access_Type --
9040 ---------------------------------
9042 procedure Expand_Previous_Access_Type (Def_Id : Entity_Id) is
9043 Ptr_Typ : Entity_Id;
9045 begin
9046 -- Find all access types in the current scope whose designated type is
9047 -- Def_Id and build master renamings for them.
9049 Ptr_Typ := First_Entity (Current_Scope);
9050 while Present (Ptr_Typ) loop
9051 if Is_Access_Type (Ptr_Typ)
9052 and then Designated_Type (Ptr_Typ) = Def_Id
9053 and then No (Master_Id (Ptr_Typ))
9054 then
9055 -- Ensure that the designated type has a master
9057 Build_Master_Entity (Def_Id);
9059 -- Private and incomplete types complicate the insertion of master
9060 -- renamings because the access type may precede the full view of
9061 -- the designated type. For this reason, the master renamings are
9062 -- inserted relative to the designated type.
9064 Build_Master_Renaming (Ptr_Typ, Ins_Nod => Parent (Def_Id));
9065 end if;
9067 Next_Entity (Ptr_Typ);
9068 end loop;
9069 end Expand_Previous_Access_Type;
9071 -----------------------------
9072 -- Expand_Record_Extension --
9073 -----------------------------
9075 -- Add a field _parent at the beginning of the record extension. This is
9076 -- used to implement inheritance. Here are some examples of expansion:
9078 -- 1. no discriminants
9079 -- type T2 is new T1 with null record;
9080 -- gives
9081 -- type T2 is new T1 with record
9082 -- _Parent : T1;
9083 -- end record;
9085 -- 2. renamed discriminants
9086 -- type T2 (B, C : Int) is new T1 (A => B) with record
9087 -- _Parent : T1 (A => B);
9088 -- D : Int;
9089 -- end;
9091 -- 3. inherited discriminants
9092 -- type T2 is new T1 with record -- discriminant A inherited
9093 -- _Parent : T1 (A);
9094 -- D : Int;
9095 -- end;
9097 procedure Expand_Record_Extension (T : Entity_Id; Def : Node_Id) is
9098 Indic : constant Node_Id := Subtype_Indication (Def);
9099 Loc : constant Source_Ptr := Sloc (Def);
9100 Rec_Ext_Part : Node_Id := Record_Extension_Part (Def);
9101 Par_Subtype : Entity_Id;
9102 Comp_List : Node_Id;
9103 Comp_Decl : Node_Id;
9104 Parent_N : Node_Id;
9105 D : Entity_Id;
9106 List_Constr : constant List_Id := New_List;
9108 begin
9109 -- Expand_Record_Extension is called directly from the semantics, so
9110 -- we must check to see whether expansion is active before proceeding,
9111 -- because this affects the visibility of selected components in bodies
9112 -- of instances. Within a generic we still need to set Parent_Subtype
9113 -- link because the visibility of inherited components will have to be
9114 -- verified in subsequent instances.
9116 if not Expander_Active then
9117 if Inside_A_Generic and then Ekind (T) = E_Record_Type then
9118 Set_Parent_Subtype (T, Etype (T));
9119 end if;
9120 return;
9121 end if;
9123 -- This may be a derivation of an untagged private type whose full
9124 -- view is tagged, in which case the Derived_Type_Definition has no
9125 -- extension part. Build an empty one now.
9127 if No (Rec_Ext_Part) then
9128 Rec_Ext_Part :=
9129 Make_Record_Definition (Loc,
9130 End_Label => Empty,
9131 Component_List => Empty,
9132 Null_Present => True);
9134 Set_Record_Extension_Part (Def, Rec_Ext_Part);
9135 Mark_Rewrite_Insertion (Rec_Ext_Part);
9136 end if;
9138 Comp_List := Component_List (Rec_Ext_Part);
9140 Parent_N := Make_Defining_Identifier (Loc, Name_uParent);
9142 -- If the derived type inherits its discriminants the type of the
9143 -- _parent field must be constrained by the inherited discriminants
9145 if Has_Discriminants (T)
9146 and then Nkind (Indic) /= N_Subtype_Indication
9147 and then not Is_Constrained (Entity (Indic))
9148 then
9149 D := First_Discriminant (T);
9150 while Present (D) loop
9151 Append_To (List_Constr, New_Occurrence_Of (D, Loc));
9152 Next_Discriminant (D);
9153 end loop;
9155 Par_Subtype :=
9156 Process_Subtype (
9157 Make_Subtype_Indication (Loc,
9158 Subtype_Mark => New_Occurrence_Of (Entity (Indic), Loc),
9159 Constraint =>
9160 Make_Index_Or_Discriminant_Constraint (Loc,
9161 Constraints => List_Constr)),
9162 Def);
9164 -- Otherwise the original subtype_indication is just what is needed
9166 else
9167 Par_Subtype := Process_Subtype (New_Copy_Tree (Indic), Def);
9168 end if;
9170 Set_Parent_Subtype (T, Par_Subtype);
9172 Comp_Decl :=
9173 Make_Component_Declaration (Loc,
9174 Defining_Identifier => Parent_N,
9175 Component_Definition =>
9176 Make_Component_Definition (Loc,
9177 Aliased_Present => False,
9178 Subtype_Indication => New_Occurrence_Of (Par_Subtype, Loc)));
9180 if Null_Present (Rec_Ext_Part) then
9181 Set_Component_List (Rec_Ext_Part,
9182 Make_Component_List (Loc,
9183 Component_Items => New_List (Comp_Decl),
9184 Variant_Part => Empty,
9185 Null_Present => False));
9186 Set_Null_Present (Rec_Ext_Part, False);
9188 elsif Null_Present (Comp_List)
9189 or else Is_Empty_List (Component_Items (Comp_List))
9190 then
9191 Set_Component_Items (Comp_List, New_List (Comp_Decl));
9192 Set_Null_Present (Comp_List, False);
9194 else
9195 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
9196 end if;
9198 Analyze (Comp_Decl);
9199 end Expand_Record_Extension;
9201 ------------------------
9202 -- Expand_Tagged_Root --
9203 ------------------------
9205 procedure Expand_Tagged_Root (T : Entity_Id) is
9206 Def : constant Node_Id := Type_Definition (Parent (T));
9207 Comp_List : Node_Id;
9208 Comp_Decl : Node_Id;
9209 Sloc_N : Source_Ptr;
9211 begin
9212 if Null_Present (Def) then
9213 Set_Component_List (Def,
9214 Make_Component_List (Sloc (Def),
9215 Component_Items => Empty_List,
9216 Variant_Part => Empty,
9217 Null_Present => True));
9218 end if;
9220 Comp_List := Component_List (Def);
9222 if Null_Present (Comp_List)
9223 or else Is_Empty_List (Component_Items (Comp_List))
9224 then
9225 Sloc_N := Sloc (Comp_List);
9226 else
9227 Sloc_N := Sloc (First (Component_Items (Comp_List)));
9228 end if;
9230 Comp_Decl :=
9231 Make_Component_Declaration (Sloc_N,
9232 Defining_Identifier => First_Tag_Component (T),
9233 Component_Definition =>
9234 Make_Component_Definition (Sloc_N,
9235 Aliased_Present => False,
9236 Subtype_Indication => New_Occurrence_Of (RTE (RE_Tag), Sloc_N)));
9238 if Null_Present (Comp_List)
9239 or else Is_Empty_List (Component_Items (Comp_List))
9240 then
9241 Set_Component_Items (Comp_List, New_List (Comp_Decl));
9242 Set_Null_Present (Comp_List, False);
9244 else
9245 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
9246 end if;
9248 -- We don't Analyze the whole expansion because the tag component has
9249 -- already been analyzed previously. Here we just insure that the tree
9250 -- is coherent with the semantic decoration
9252 Find_Type (Subtype_Indication (Component_Definition (Comp_Decl)));
9254 exception
9255 when RE_Not_Available =>
9256 return;
9257 end Expand_Tagged_Root;
9259 ------------------------------
9260 -- Freeze_Stream_Operations --
9261 ------------------------------
9263 procedure Freeze_Stream_Operations (N : Node_Id; Typ : Entity_Id) is
9264 Names : constant array (1 .. 4) of TSS_Name_Type :=
9265 (TSS_Stream_Input,
9266 TSS_Stream_Output,
9267 TSS_Stream_Read,
9268 TSS_Stream_Write);
9269 Stream_Op : Entity_Id;
9271 begin
9272 -- Primitive operations of tagged types are frozen when the dispatch
9273 -- table is constructed.
9275 if not Comes_From_Source (Typ) or else Is_Tagged_Type (Typ) then
9276 return;
9277 end if;
9279 for J in Names'Range loop
9280 Stream_Op := TSS (Typ, Names (J));
9282 if Present (Stream_Op)
9283 and then Is_Subprogram (Stream_Op)
9284 and then Nkind (Unit_Declaration_Node (Stream_Op)) =
9285 N_Subprogram_Declaration
9286 and then not Is_Frozen (Stream_Op)
9287 then
9288 Append_Freeze_Actions (Typ, Freeze_Entity (Stream_Op, N));
9289 end if;
9290 end loop;
9291 end Freeze_Stream_Operations;
9293 -----------------
9294 -- Freeze_Type --
9295 -----------------
9297 -- Full type declarations are expanded at the point at which the type is
9298 -- frozen. The formal N is the Freeze_Node for the type. Any statements or
9299 -- declarations generated by the freezing (e.g. the procedure generated
9300 -- for initialization) are chained in the Actions field list of the freeze
9301 -- node using Append_Freeze_Actions.
9303 -- WARNING: This routine manages Ghost regions. Return statements must be
9304 -- replaced by gotos which jump to the end of the routine and restore the
9305 -- Ghost mode.
9307 function Freeze_Type (N : Node_Id) return Boolean is
9308 procedure Process_RACW_Types (Typ : Entity_Id);
9309 -- Validate and generate stubs for all RACW types associated with type
9310 -- Typ.
9312 procedure Process_Pending_Access_Types (Typ : Entity_Id);
9313 -- Associate type Typ's Finalize_Address primitive with the finalization
9314 -- masters of pending access-to-Typ types.
9316 ------------------------
9317 -- Process_RACW_Types --
9318 ------------------------
9320 procedure Process_RACW_Types (Typ : Entity_Id) is
9321 List : constant Elist_Id := Access_Types_To_Process (N);
9322 E : Elmt_Id;
9323 Seen : Boolean := False;
9325 begin
9326 if Present (List) then
9327 E := First_Elmt (List);
9328 while Present (E) loop
9329 if Is_Remote_Access_To_Class_Wide_Type (Node (E)) then
9330 Validate_RACW_Primitives (Node (E));
9331 Seen := True;
9332 end if;
9334 Next_Elmt (E);
9335 end loop;
9336 end if;
9338 -- If there are RACWs designating this type, make stubs now
9340 if Seen then
9341 Remote_Types_Tagged_Full_View_Encountered (Typ);
9342 end if;
9343 end Process_RACW_Types;
9345 ----------------------------------
9346 -- Process_Pending_Access_Types --
9347 ----------------------------------
9349 procedure Process_Pending_Access_Types (Typ : Entity_Id) is
9350 E : Elmt_Id;
9352 begin
9353 -- Finalize_Address is not generated in CodePeer mode because the
9354 -- body contains address arithmetic. This processing is disabled.
9356 if CodePeer_Mode then
9357 null;
9359 -- Certain itypes are generated for contexts that cannot allocate
9360 -- objects and should not set primitive Finalize_Address.
9362 elsif Is_Itype (Typ)
9363 and then Nkind (Associated_Node_For_Itype (Typ)) =
9364 N_Explicit_Dereference
9365 then
9366 null;
9368 -- When an access type is declared after the incomplete view of a
9369 -- Taft-amendment type, the access type is considered pending in
9370 -- case the full view of the Taft-amendment type is controlled. If
9371 -- this is indeed the case, associate the Finalize_Address routine
9372 -- of the full view with the finalization masters of all pending
9373 -- access types. This scenario applies to anonymous access types as
9374 -- well. But the Finalize_Address routine is missing if the type is
9375 -- class-wide and we are under restriction No_Dispatching_Calls, see
9376 -- Expand_Freeze_Class_Wide_Type above for the rationale.
9378 elsif Needs_Finalization (Typ)
9379 and then (not Is_Class_Wide_Type (Typ)
9380 or else not Restriction_Active (No_Dispatching_Calls))
9381 and then Present (Pending_Access_Types (Typ))
9382 then
9383 E := First_Elmt (Pending_Access_Types (Typ));
9384 while Present (E) loop
9386 -- Generate:
9387 -- Set_Finalize_Address
9388 -- (Ptr_Typ, <Typ>FD'Unrestricted_Access);
9390 Append_Freeze_Action (Typ,
9391 Make_Set_Finalize_Address_Call
9392 (Loc => Sloc (N),
9393 Ptr_Typ => Node (E)));
9395 Next_Elmt (E);
9396 end loop;
9397 end if;
9398 end Process_Pending_Access_Types;
9400 -- Local variables
9402 Def_Id : constant Entity_Id := Entity (N);
9404 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
9405 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
9406 -- Save the Ghost-related attributes to restore on exit
9408 Result : Boolean := False;
9410 -- Start of processing for Freeze_Type
9412 begin
9413 -- The type being frozen may be subject to pragma Ghost. Set the mode
9414 -- now to ensure that any nodes generated during freezing are properly
9415 -- marked as Ghost.
9417 Set_Ghost_Mode (Def_Id);
9419 -- Process any remote access-to-class-wide types designating the type
9420 -- being frozen.
9422 Process_RACW_Types (Def_Id);
9424 -- Freeze processing for record types
9426 if Is_Record_Type (Def_Id) then
9427 if Ekind (Def_Id) = E_Record_Type then
9428 Expand_Freeze_Record_Type (N);
9429 elsif Is_Class_Wide_Type (Def_Id) then
9430 Expand_Freeze_Class_Wide_Type (N);
9431 end if;
9433 -- Freeze processing for array types
9435 elsif Is_Array_Type (Def_Id) then
9436 Expand_Freeze_Array_Type (N);
9438 -- Freeze processing for access types
9440 -- For pool-specific access types, find out the pool object used for
9441 -- this type, needs actual expansion of it in some cases. Here are the
9442 -- different cases :
9444 -- 1. Rep Clause "for Def_Id'Storage_Size use 0;"
9445 -- ---> don't use any storage pool
9447 -- 2. Rep Clause : for Def_Id'Storage_Size use Expr.
9448 -- Expand:
9449 -- Def_Id__Pool : Stack_Bounded_Pool (Expr, DT'Size, DT'Alignment);
9451 -- 3. Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
9452 -- ---> Storage Pool is the specified one
9454 -- See GNAT Pool packages in the Run-Time for more details
9456 elsif Ekind (Def_Id) in E_Access_Type | E_General_Access_Type then
9457 declare
9458 Loc : constant Source_Ptr := Sloc (N);
9459 Desig_Type : constant Entity_Id := Designated_Type (Def_Id);
9461 Freeze_Action_Typ : Entity_Id;
9462 Pool_Object : Entity_Id;
9464 begin
9465 -- Case 1
9467 -- Rep Clause "for Def_Id'Storage_Size use 0;"
9468 -- ---> don't use any storage pool
9470 if No_Pool_Assigned (Def_Id) then
9471 null;
9473 -- Case 2
9475 -- Rep Clause : for Def_Id'Storage_Size use Expr.
9476 -- ---> Expand:
9477 -- Def_Id__Pool : Stack_Bounded_Pool
9478 -- (Expr, DT'Size, DT'Alignment);
9480 elsif Has_Storage_Size_Clause (Def_Id) then
9481 declare
9482 DT_Align : Node_Id;
9483 DT_Size : Node_Id;
9485 begin
9486 -- For unconstrained composite types we give a size of zero
9487 -- so that the pool knows that it needs a special algorithm
9488 -- for variable size object allocation.
9490 if Is_Composite_Type (Desig_Type)
9491 and then not Is_Constrained (Desig_Type)
9492 then
9493 DT_Size := Make_Integer_Literal (Loc, 0);
9494 DT_Align := Make_Integer_Literal (Loc, Maximum_Alignment);
9496 else
9497 DT_Size :=
9498 Make_Attribute_Reference (Loc,
9499 Prefix => New_Occurrence_Of (Desig_Type, Loc),
9500 Attribute_Name => Name_Max_Size_In_Storage_Elements);
9502 DT_Align :=
9503 Make_Attribute_Reference (Loc,
9504 Prefix => New_Occurrence_Of (Desig_Type, Loc),
9505 Attribute_Name => Name_Alignment);
9506 end if;
9508 Pool_Object :=
9509 Make_Defining_Identifier (Loc,
9510 Chars => New_External_Name (Chars (Def_Id), 'P'));
9512 -- We put the code associated with the pools in the entity
9513 -- that has the later freeze node, usually the access type
9514 -- but it can also be the designated_type; because the pool
9515 -- code requires both those types to be frozen
9517 if Is_Frozen (Desig_Type)
9518 and then (No (Freeze_Node (Desig_Type))
9519 or else Analyzed (Freeze_Node (Desig_Type)))
9520 then
9521 Freeze_Action_Typ := Def_Id;
9523 -- A Taft amendment type cannot get the freeze actions
9524 -- since the full view is not there.
9526 elsif Is_Incomplete_Or_Private_Type (Desig_Type)
9527 and then No (Full_View (Desig_Type))
9528 then
9529 Freeze_Action_Typ := Def_Id;
9531 else
9532 Freeze_Action_Typ := Desig_Type;
9533 end if;
9535 Append_Freeze_Action (Freeze_Action_Typ,
9536 Make_Object_Declaration (Loc,
9537 Defining_Identifier => Pool_Object,
9538 Object_Definition =>
9539 Make_Subtype_Indication (Loc,
9540 Subtype_Mark =>
9541 New_Occurrence_Of
9542 (RTE (RE_Stack_Bounded_Pool), Loc),
9544 Constraint =>
9545 Make_Index_Or_Discriminant_Constraint (Loc,
9546 Constraints => New_List (
9548 -- First discriminant is the Pool Size
9550 New_Occurrence_Of (
9551 Storage_Size_Variable (Def_Id), Loc),
9553 -- Second discriminant is the element size
9555 DT_Size,
9557 -- Third discriminant is the alignment
9559 DT_Align)))));
9560 end;
9562 Set_Associated_Storage_Pool (Def_Id, Pool_Object);
9564 -- Case 3
9566 -- Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
9567 -- ---> Storage Pool is the specified one
9569 -- When compiling in Ada 2012 mode, ensure that the accessibility
9570 -- level of the subpool access type is not deeper than that of the
9571 -- pool_with_subpools.
9573 elsif Ada_Version >= Ada_2012
9574 and then Present (Associated_Storage_Pool (Def_Id))
9575 and then RTU_Loaded (System_Storage_Pools_Subpools)
9576 then
9577 declare
9578 Loc : constant Source_Ptr := Sloc (Def_Id);
9579 Pool : constant Entity_Id :=
9580 Associated_Storage_Pool (Def_Id);
9582 begin
9583 -- It is known that the accessibility level of the access
9584 -- type is deeper than that of the pool.
9586 if Type_Access_Level (Def_Id)
9587 > Static_Accessibility_Level (Pool, Object_Decl_Level)
9588 and then Is_Class_Wide_Type (Etype (Pool))
9589 and then not Accessibility_Checks_Suppressed (Def_Id)
9590 and then not Accessibility_Checks_Suppressed (Pool)
9591 then
9592 -- When the pool is of a class-wide type, it may or may
9593 -- not support subpools depending on the path of
9594 -- derivation. Generate:
9596 -- if Def_Id in RSPWS'Class then
9597 -- raise Program_Error;
9598 -- end if;
9600 Append_Freeze_Action (Def_Id,
9601 Make_If_Statement (Loc,
9602 Condition =>
9603 Make_In (Loc,
9604 Left_Opnd => New_Occurrence_Of (Pool, Loc),
9605 Right_Opnd =>
9606 New_Occurrence_Of
9607 (Class_Wide_Type
9608 (RTE
9609 (RE_Root_Storage_Pool_With_Subpools)),
9610 Loc)),
9611 Then_Statements => New_List (
9612 Make_Raise_Program_Error (Loc,
9613 Reason => PE_Accessibility_Check_Failed))));
9614 end if;
9615 end;
9616 end if;
9618 -- For access-to-controlled types (including class-wide types and
9619 -- Taft-amendment types, which potentially have controlled
9620 -- components), expand the list controller object that will store
9621 -- the dynamically allocated objects. Don't do this transformation
9622 -- for expander-generated access types, except do it for types
9623 -- that are the full view of types derived from other private
9624 -- types and for access types used to implement indirect temps.
9625 -- Also suppress the list controller in the case of a designated
9626 -- type with convention Java, since this is used when binding to
9627 -- Java API specs, where there's no equivalent of a finalization
9628 -- list and we don't want to pull in the finalization support if
9629 -- not needed.
9631 if not Comes_From_Source (Def_Id)
9632 and then not Has_Private_Declaration (Def_Id)
9633 and then not Old_Attr_Util.Indirect_Temps
9634 .Is_Access_Type_For_Indirect_Temp (Def_Id)
9635 then
9636 null;
9638 -- An exception is made for types defined in the run-time because
9639 -- Ada.Tags.Tag itself is such a type and cannot afford this
9640 -- unnecessary overhead that would generates a loop in the
9641 -- expansion scheme. Another exception is if Restrictions
9642 -- (No_Finalization) is active, since then we know nothing is
9643 -- controlled.
9645 elsif Restriction_Active (No_Finalization)
9646 or else In_Runtime (Def_Id)
9647 then
9648 null;
9650 -- Create a finalization master for an access-to-controlled type
9651 -- or an access-to-incomplete type. It is assumed that the full
9652 -- view will be controlled.
9654 elsif Needs_Finalization (Desig_Type)
9655 or else (Is_Incomplete_Type (Desig_Type)
9656 and then No (Full_View (Desig_Type)))
9657 then
9658 Build_Finalization_Master (Def_Id);
9660 -- Create a finalization master when the designated type contains
9661 -- a private component. It is assumed that the full view will be
9662 -- controlled.
9664 elsif Has_Private_Component (Desig_Type) then
9665 Build_Finalization_Master
9666 (Typ => Def_Id,
9667 For_Private => True,
9668 Context_Scope => Scope (Def_Id),
9669 Insertion_Node => Declaration_Node (Desig_Type));
9670 end if;
9671 end;
9673 -- Freeze processing for enumeration types
9675 elsif Ekind (Def_Id) = E_Enumeration_Type then
9677 -- We only have something to do if we have a non-standard
9678 -- representation (i.e. at least one literal whose pos value
9679 -- is not the same as its representation)
9681 if Has_Non_Standard_Rep (Def_Id) then
9682 Expand_Freeze_Enumeration_Type (N);
9683 end if;
9685 -- Private types that are completed by a derivation from a private
9686 -- type have an internally generated full view, that needs to be
9687 -- frozen. This must be done explicitly because the two views share
9688 -- the freeze node, and the underlying full view is not visible when
9689 -- the freeze node is analyzed.
9691 elsif Is_Private_Type (Def_Id)
9692 and then Is_Derived_Type (Def_Id)
9693 and then Present (Full_View (Def_Id))
9694 and then Is_Itype (Full_View (Def_Id))
9695 and then Has_Private_Declaration (Full_View (Def_Id))
9696 and then Freeze_Node (Full_View (Def_Id)) = N
9697 then
9698 Set_Entity (N, Full_View (Def_Id));
9699 Result := Freeze_Type (N);
9700 Set_Entity (N, Def_Id);
9702 -- All other types require no expander action. There are such cases
9703 -- (e.g. task types and protected types). In such cases, the freeze
9704 -- nodes are there for use by Gigi.
9706 end if;
9708 -- Complete the initialization of all pending access types' finalization
9709 -- masters now that the designated type has been is frozen and primitive
9710 -- Finalize_Address generated.
9712 Process_Pending_Access_Types (Def_Id);
9713 Freeze_Stream_Operations (N, Def_Id);
9715 -- Generate the [spec and] body of the invariant procedure tasked with
9716 -- the runtime verification of all invariants that pertain to the type.
9717 -- This includes invariants on the partial and full view, inherited
9718 -- class-wide invariants from parent types or interfaces, and invariants
9719 -- on array elements or record components. But skip internal types.
9721 if Is_Itype (Def_Id) then
9722 null;
9724 elsif Is_Interface (Def_Id) then
9726 -- Interfaces are treated as the partial view of a private type in
9727 -- order to achieve uniformity with the general case. As a result, an
9728 -- interface receives only a "partial" invariant procedure which is
9729 -- never called.
9731 if Has_Own_Invariants (Def_Id) then
9732 Build_Invariant_Procedure_Body
9733 (Typ => Def_Id,
9734 Partial_Invariant => Is_Interface (Def_Id));
9735 end if;
9737 -- Non-interface types
9739 -- Do not generate invariant procedure within other assertion
9740 -- subprograms, which may involve local declarations of local
9741 -- subtypes to which these checks do not apply.
9743 else
9744 if Has_Invariants (Def_Id) then
9745 if not Predicate_Check_In_Scope (Def_Id)
9746 or else (Ekind (Current_Scope) = E_Function
9747 and then Is_Predicate_Function (Current_Scope))
9748 then
9749 null;
9750 else
9751 Build_Invariant_Procedure_Body (Def_Id);
9752 end if;
9753 end if;
9755 -- Generate the [spec and] body of the procedure tasked with the
9756 -- run-time verification of pragma Default_Initial_Condition's
9757 -- expression.
9759 if Has_DIC (Def_Id) then
9760 Build_DIC_Procedure_Body (Def_Id);
9761 end if;
9762 end if;
9764 Restore_Ghost_Region (Saved_GM, Saved_IGR);
9766 return Result;
9768 exception
9769 when RE_Not_Available =>
9770 Restore_Ghost_Region (Saved_GM, Saved_IGR);
9772 return False;
9773 end Freeze_Type;
9775 -------------------------
9776 -- Get_Simple_Init_Val --
9777 -------------------------
9779 function Get_Simple_Init_Val
9780 (Typ : Entity_Id;
9781 N : Node_Id;
9782 Size : Uint := No_Uint) return Node_Id
9784 IV_Attribute : constant Boolean :=
9785 Nkind (N) = N_Attribute_Reference
9786 and then Attribute_Name (N) = Name_Invalid_Value;
9788 Loc : constant Source_Ptr := Sloc (N);
9790 procedure Extract_Subtype_Bounds
9791 (Lo_Bound : out Uint;
9792 Hi_Bound : out Uint);
9793 -- Inspect subtype Typ as well its ancestor subtypes and derived types
9794 -- to determine the best known information about the bounds of the type.
9795 -- The output parameters are set as follows:
9797 -- * Lo_Bound - Set to No_Unit when there is no information available,
9798 -- or to the known low bound.
9800 -- * Hi_Bound - Set to No_Unit when there is no information available,
9801 -- or to the known high bound.
9803 function Simple_Init_Array_Type return Node_Id;
9804 -- Build an expression to initialize array type Typ
9806 function Simple_Init_Defaulted_Type return Node_Id;
9807 -- Build an expression to initialize type Typ which is subject to
9808 -- aspect Default_Value.
9810 function Simple_Init_Initialize_Scalars_Type
9811 (Size_To_Use : Uint) return Node_Id;
9812 -- Build an expression to initialize scalar type Typ which is subject to
9813 -- pragma Initialize_Scalars. Size_To_Use is the size of the object.
9815 function Simple_Init_Normalize_Scalars_Type
9816 (Size_To_Use : Uint) return Node_Id;
9817 -- Build an expression to initialize scalar type Typ which is subject to
9818 -- pragma Normalize_Scalars. Size_To_Use is the size of the object.
9820 function Simple_Init_Private_Type return Node_Id;
9821 -- Build an expression to initialize private type Typ
9823 function Simple_Init_Scalar_Type return Node_Id;
9824 -- Build an expression to initialize scalar type Typ
9826 ----------------------------
9827 -- Extract_Subtype_Bounds --
9828 ----------------------------
9830 procedure Extract_Subtype_Bounds
9831 (Lo_Bound : out Uint;
9832 Hi_Bound : out Uint)
9834 ST1 : Entity_Id;
9835 ST2 : Entity_Id;
9836 Lo : Node_Id;
9837 Hi : Node_Id;
9838 Lo_Val : Uint;
9839 Hi_Val : Uint;
9841 begin
9842 Lo_Bound := No_Uint;
9843 Hi_Bound := No_Uint;
9845 -- Loop to climb ancestor subtypes and derived types
9847 ST1 := Typ;
9848 loop
9849 if not Is_Discrete_Type (ST1) then
9850 return;
9851 end if;
9853 Lo := Type_Low_Bound (ST1);
9854 Hi := Type_High_Bound (ST1);
9856 if Compile_Time_Known_Value (Lo) then
9857 Lo_Val := Expr_Value (Lo);
9859 if No (Lo_Bound) or else Lo_Bound < Lo_Val then
9860 Lo_Bound := Lo_Val;
9861 end if;
9862 end if;
9864 if Compile_Time_Known_Value (Hi) then
9865 Hi_Val := Expr_Value (Hi);
9867 if No (Hi_Bound) or else Hi_Bound > Hi_Val then
9868 Hi_Bound := Hi_Val;
9869 end if;
9870 end if;
9872 ST2 := Ancestor_Subtype (ST1);
9874 if No (ST2) then
9875 ST2 := Etype (ST1);
9876 end if;
9878 exit when ST1 = ST2;
9879 ST1 := ST2;
9880 end loop;
9881 end Extract_Subtype_Bounds;
9883 ----------------------------
9884 -- Simple_Init_Array_Type --
9885 ----------------------------
9887 function Simple_Init_Array_Type return Node_Id is
9888 Comp_Typ : constant Entity_Id := Component_Type (Typ);
9890 function Simple_Init_Dimension (Index : Node_Id) return Node_Id;
9891 -- Initialize a single array dimension with index constraint Index
9893 --------------------
9894 -- Simple_Init_Dimension --
9895 --------------------
9897 function Simple_Init_Dimension (Index : Node_Id) return Node_Id is
9898 begin
9899 -- Process the current dimension
9901 if Present (Index) then
9903 -- Build a suitable "others" aggregate for the next dimension,
9904 -- or initialize the component itself. Generate:
9906 -- (others => ...)
9908 return
9909 Make_Aggregate (Loc,
9910 Component_Associations => New_List (
9911 Make_Component_Association (Loc,
9912 Choices => New_List (Make_Others_Choice (Loc)),
9913 Expression =>
9914 Simple_Init_Dimension (Next_Index (Index)))));
9916 -- Otherwise all dimensions have been processed. Initialize the
9917 -- component itself.
9919 else
9920 return
9921 Get_Simple_Init_Val
9922 (Typ => Comp_Typ,
9923 N => N,
9924 Size => Esize (Comp_Typ));
9925 end if;
9926 end Simple_Init_Dimension;
9928 -- Start of processing for Simple_Init_Array_Type
9930 begin
9931 return Simple_Init_Dimension (First_Index (Typ));
9932 end Simple_Init_Array_Type;
9934 --------------------------------
9935 -- Simple_Init_Defaulted_Type --
9936 --------------------------------
9938 function Simple_Init_Defaulted_Type return Node_Id is
9939 Subtyp : Entity_Id := First_Subtype (Typ);
9941 begin
9942 -- When the first subtype is private, retrieve the expression of the
9943 -- Default_Value from the underlying type.
9945 if Is_Private_Type (Subtyp) then
9946 Subtyp := Full_View (Subtyp);
9947 end if;
9949 -- Use the Sloc of the context node when constructing the initial
9950 -- value because the expression of Default_Value may come from a
9951 -- different unit. Updating the Sloc will result in accurate error
9952 -- diagnostics.
9954 return
9955 OK_Convert_To
9956 (Typ => Typ,
9957 Expr =>
9958 New_Copy_Tree
9959 (Source => Default_Aspect_Value (Subtyp),
9960 New_Sloc => Loc));
9961 end Simple_Init_Defaulted_Type;
9963 -----------------------------------------
9964 -- Simple_Init_Initialize_Scalars_Type --
9965 -----------------------------------------
9967 function Simple_Init_Initialize_Scalars_Type
9968 (Size_To_Use : Uint) return Node_Id
9970 Float_Typ : Entity_Id;
9971 Hi_Bound : Uint;
9972 Lo_Bound : Uint;
9973 Scal_Typ : Scalar_Id;
9975 begin
9976 Extract_Subtype_Bounds (Lo_Bound, Hi_Bound);
9978 -- Float types
9980 if Is_Floating_Point_Type (Typ) then
9981 Float_Typ := Root_Type (Typ);
9983 if Float_Typ = Standard_Short_Float then
9984 Scal_Typ := Name_Short_Float;
9985 elsif Float_Typ = Standard_Float then
9986 Scal_Typ := Name_Float;
9987 elsif Float_Typ = Standard_Long_Float then
9988 Scal_Typ := Name_Long_Float;
9989 else pragma Assert (Float_Typ = Standard_Long_Long_Float);
9990 Scal_Typ := Name_Long_Long_Float;
9991 end if;
9993 -- If zero is invalid, it is a convenient value to use that is for
9994 -- sure an appropriate invalid value in all situations.
9996 elsif Present (Lo_Bound) and then Lo_Bound > Uint_0 then
9997 return Make_Integer_Literal (Loc, 0);
9999 -- Unsigned types
10001 elsif Is_Unsigned_Type (Typ) then
10002 if Size_To_Use <= 8 then
10003 Scal_Typ := Name_Unsigned_8;
10004 elsif Size_To_Use <= 16 then
10005 Scal_Typ := Name_Unsigned_16;
10006 elsif Size_To_Use <= 32 then
10007 Scal_Typ := Name_Unsigned_32;
10008 elsif Size_To_Use <= 64 then
10009 Scal_Typ := Name_Unsigned_64;
10010 else
10011 Scal_Typ := Name_Unsigned_128;
10012 end if;
10014 -- Signed types
10016 else
10017 if Size_To_Use <= 8 then
10018 Scal_Typ := Name_Signed_8;
10019 elsif Size_To_Use <= 16 then
10020 Scal_Typ := Name_Signed_16;
10021 elsif Size_To_Use <= 32 then
10022 Scal_Typ := Name_Signed_32;
10023 elsif Size_To_Use <= 64 then
10024 Scal_Typ := Name_Signed_64;
10025 else
10026 Scal_Typ := Name_Signed_128;
10027 end if;
10028 end if;
10030 -- Use the values specified by pragma Initialize_Scalars or the ones
10031 -- provided by the binder. Higher precedence is given to the pragma.
10033 return Invalid_Scalar_Value (Loc, Scal_Typ);
10034 end Simple_Init_Initialize_Scalars_Type;
10036 ----------------------------------------
10037 -- Simple_Init_Normalize_Scalars_Type --
10038 ----------------------------------------
10040 function Simple_Init_Normalize_Scalars_Type
10041 (Size_To_Use : Uint) return Node_Id
10043 Signed_Size : constant Uint := UI_Min (Uint_63, Size_To_Use - 1);
10045 Expr : Node_Id;
10046 Hi_Bound : Uint;
10047 Lo_Bound : Uint;
10049 begin
10050 Extract_Subtype_Bounds (Lo_Bound, Hi_Bound);
10052 -- If zero is invalid, it is a convenient value to use that is for
10053 -- sure an appropriate invalid value in all situations.
10055 if Present (Lo_Bound) and then Lo_Bound > Uint_0 then
10056 Expr := Make_Integer_Literal (Loc, 0);
10058 -- Cases where all one bits is the appropriate invalid value
10060 -- For modular types, all 1 bits is either invalid or valid. If it
10061 -- is valid, then there is nothing that can be done since there are
10062 -- no invalid values (we ruled out zero already).
10064 -- For signed integer types that have no negative values, either
10065 -- there is room for negative values, or there is not. If there
10066 -- is, then all 1-bits may be interpreted as minus one, which is
10067 -- certainly invalid. Alternatively it is treated as the largest
10068 -- positive value, in which case the observation for modular types
10069 -- still applies.
10071 -- For float types, all 1-bits is a NaN (not a number), which is
10072 -- certainly an appropriately invalid value.
10074 elsif Is_Enumeration_Type (Typ)
10075 or else Is_Floating_Point_Type (Typ)
10076 or else Is_Unsigned_Type (Typ)
10077 then
10078 Expr := Make_Integer_Literal (Loc, 2 ** Size_To_Use - 1);
10080 -- Resolve as Long_Long_Long_Unsigned, because the largest number
10081 -- we can generate is out of range of universal integer.
10083 Analyze_And_Resolve (Expr, Standard_Long_Long_Long_Unsigned);
10085 -- Case of signed types
10087 else
10088 -- Normally we like to use the most negative number. The one
10089 -- exception is when this number is in the known subtype range and
10090 -- the largest positive number is not in the known subtype range.
10092 -- For this exceptional case, use largest positive value
10094 if Present (Lo_Bound) and then Present (Hi_Bound)
10095 and then Lo_Bound <= (-(2 ** Signed_Size))
10096 and then Hi_Bound < 2 ** Signed_Size
10097 then
10098 Expr := Make_Integer_Literal (Loc, 2 ** Signed_Size - 1);
10100 -- Normal case of largest negative value
10102 else
10103 Expr := Make_Integer_Literal (Loc, -(2 ** Signed_Size));
10104 end if;
10105 end if;
10107 return Expr;
10108 end Simple_Init_Normalize_Scalars_Type;
10110 ------------------------------
10111 -- Simple_Init_Private_Type --
10112 ------------------------------
10114 function Simple_Init_Private_Type return Node_Id is
10115 Under_Typ : constant Entity_Id := Underlying_Type (Typ);
10116 Expr : Node_Id;
10118 begin
10119 -- The availability of the underlying view must be checked by routine
10120 -- Needs_Simple_Initialization.
10122 pragma Assert (Present (Under_Typ));
10124 Expr := Get_Simple_Init_Val (Under_Typ, N, Size);
10126 -- If the initial value is null or an aggregate, qualify it with the
10127 -- underlying type in order to provide a proper context.
10129 if Nkind (Expr) in N_Aggregate | N_Null then
10130 Expr :=
10131 Make_Qualified_Expression (Loc,
10132 Subtype_Mark => New_Occurrence_Of (Under_Typ, Loc),
10133 Expression => Expr);
10134 end if;
10136 Expr := Unchecked_Convert_To (Typ, Expr);
10138 -- Do not truncate the result when scalar types are involved and
10139 -- Initialize/Normalize_Scalars is in effect.
10141 if Nkind (Expr) = N_Unchecked_Type_Conversion
10142 and then Is_Scalar_Type (Under_Typ)
10143 then
10144 Set_No_Truncation (Expr);
10145 end if;
10147 return Expr;
10148 end Simple_Init_Private_Type;
10150 -----------------------------
10151 -- Simple_Init_Scalar_Type --
10152 -----------------------------
10154 function Simple_Init_Scalar_Type return Node_Id is
10155 Expr : Node_Id;
10156 Size_To_Use : Uint;
10158 begin
10159 pragma Assert (Init_Or_Norm_Scalars or IV_Attribute);
10161 -- Determine the size of the object. This is either the size provided
10162 -- by the caller, or the Esize of the scalar type.
10164 if No (Size) or else Size <= Uint_0 then
10165 Size_To_Use := UI_Max (Uint_1, Esize (Typ));
10166 else
10167 Size_To_Use := Size;
10168 end if;
10170 -- The maximum size to use is System_Max_Integer_Size bits. This
10171 -- will create values of type Long_Long_Long_Unsigned and the range
10172 -- must fit this type.
10174 if Present (Size_To_Use)
10175 and then Size_To_Use > System_Max_Integer_Size
10176 then
10177 Size_To_Use := UI_From_Int (System_Max_Integer_Size);
10178 end if;
10180 if Normalize_Scalars and then not IV_Attribute then
10181 Expr := Simple_Init_Normalize_Scalars_Type (Size_To_Use);
10182 else
10183 Expr := Simple_Init_Initialize_Scalars_Type (Size_To_Use);
10184 end if;
10186 -- The final expression is obtained by doing an unchecked conversion
10187 -- of this result to the base type of the required subtype. Use the
10188 -- base type to prevent the unchecked conversion from chopping bits,
10189 -- and then we set Kill_Range_Check to preserve the "bad" value.
10191 Expr := Unchecked_Convert_To (Base_Type (Typ), Expr);
10193 -- Ensure that the expression is not truncated since the "bad" bits
10194 -- are desired, and also kill the range checks.
10196 if Nkind (Expr) = N_Unchecked_Type_Conversion then
10197 Set_Kill_Range_Check (Expr);
10198 Set_No_Truncation (Expr);
10199 end if;
10201 return Expr;
10202 end Simple_Init_Scalar_Type;
10204 -- Start of processing for Get_Simple_Init_Val
10206 begin
10207 if Is_Private_Type (Typ) then
10208 return Simple_Init_Private_Type;
10210 elsif Is_Scalar_Type (Typ) then
10211 if Has_Default_Aspect (Typ) then
10212 return Simple_Init_Defaulted_Type;
10213 else
10214 return Simple_Init_Scalar_Type;
10215 end if;
10217 -- Array type with Initialize or Normalize_Scalars
10219 elsif Is_Array_Type (Typ) then
10220 pragma Assert (Init_Or_Norm_Scalars);
10221 return Simple_Init_Array_Type;
10223 -- Access type is initialized to null
10225 elsif Is_Access_Type (Typ) then
10226 return Make_Null (Loc);
10228 -- No other possibilities should arise, since we should only be calling
10229 -- Get_Simple_Init_Val if Needs_Simple_Initialization returned True,
10230 -- indicating one of the above cases held.
10232 else
10233 raise Program_Error;
10234 end if;
10236 exception
10237 when RE_Not_Available =>
10238 return Empty;
10239 end Get_Simple_Init_Val;
10241 ------------------------------
10242 -- Has_New_Non_Standard_Rep --
10243 ------------------------------
10245 function Has_New_Non_Standard_Rep (T : Entity_Id) return Boolean is
10246 begin
10247 if not Is_Derived_Type (T) then
10248 return Has_Non_Standard_Rep (T)
10249 or else Has_Non_Standard_Rep (Root_Type (T));
10251 -- If Has_Non_Standard_Rep is not set on the derived type, the
10252 -- representation is fully inherited.
10254 elsif not Has_Non_Standard_Rep (T) then
10255 return False;
10257 else
10258 return First_Rep_Item (T) /= First_Rep_Item (Root_Type (T));
10260 -- May need a more precise check here: the First_Rep_Item may be a
10261 -- stream attribute, which does not affect the representation of the
10262 -- type ???
10264 end if;
10265 end Has_New_Non_Standard_Rep;
10267 ----------------------
10268 -- Inline_Init_Proc --
10269 ----------------------
10271 function Inline_Init_Proc (Typ : Entity_Id) return Boolean is
10272 begin
10273 -- The initialization proc of protected records is not worth inlining.
10274 -- In addition, when compiled for another unit for inlining purposes,
10275 -- it may make reference to entities that have not been elaborated yet.
10276 -- The initialization proc of records that need finalization contains
10277 -- a nested clean-up procedure that makes it impractical to inline as
10278 -- well, except for simple controlled types themselves. And similar
10279 -- considerations apply to task types.
10281 if Is_Concurrent_Type (Typ) then
10282 return False;
10284 elsif Needs_Finalization (Typ) and then not Is_Controlled (Typ) then
10285 return False;
10287 elsif Has_Task (Typ) then
10288 return False;
10290 else
10291 return True;
10292 end if;
10293 end Inline_Init_Proc;
10295 ----------------
10296 -- In_Runtime --
10297 ----------------
10299 function In_Runtime (E : Entity_Id) return Boolean is
10300 S1 : Entity_Id;
10302 begin
10303 S1 := Scope (E);
10304 while Scope (S1) /= Standard_Standard loop
10305 S1 := Scope (S1);
10306 end loop;
10308 return Is_RTU (S1, System) or else Is_RTU (S1, Ada);
10309 end In_Runtime;
10311 package body Initialization_Control is
10313 ------------------------
10314 -- Requires_Late_Init --
10315 ------------------------
10317 function Requires_Late_Init
10318 (Decl : Node_Id;
10319 Rec_Type : Entity_Id) return Boolean
10321 References_Current_Instance : Boolean := False;
10322 Has_Access_Discriminant : Boolean := False;
10323 Has_Internal_Call : Boolean := False;
10325 function Find_Access_Discriminant
10326 (N : Node_Id) return Traverse_Result;
10327 -- Look for a name denoting an access discriminant
10329 function Find_Current_Instance
10330 (N : Node_Id) return Traverse_Result;
10331 -- Look for a reference to the current instance of the type
10333 function Find_Internal_Call
10334 (N : Node_Id) return Traverse_Result;
10335 -- Look for an internal protected function call
10337 ------------------------------
10338 -- Find_Access_Discriminant --
10339 ------------------------------
10341 function Find_Access_Discriminant
10342 (N : Node_Id) return Traverse_Result is
10343 begin
10344 if Is_Entity_Name (N)
10345 and then Denotes_Discriminant (N)
10346 and then Is_Access_Type (Etype (N))
10347 then
10348 Has_Access_Discriminant := True;
10349 return Abandon;
10350 else
10351 return OK;
10352 end if;
10353 end Find_Access_Discriminant;
10355 ---------------------------
10356 -- Find_Current_Instance --
10357 ---------------------------
10359 function Find_Current_Instance
10360 (N : Node_Id) return Traverse_Result is
10361 begin
10362 if Is_Entity_Name (N)
10363 and then Present (Entity (N))
10364 and then Is_Current_Instance (N)
10365 then
10366 References_Current_Instance := True;
10367 return Abandon;
10368 else
10369 return OK;
10370 end if;
10371 end Find_Current_Instance;
10373 ------------------------
10374 -- Find_Internal_Call --
10375 ------------------------
10377 function Find_Internal_Call (N : Node_Id) return Traverse_Result is
10379 function Call_Scope (N : Node_Id) return Entity_Id;
10380 -- Return the scope enclosing a given call node N
10382 ----------------
10383 -- Call_Scope --
10384 ----------------
10386 function Call_Scope (N : Node_Id) return Entity_Id is
10387 Nam : constant Node_Id := Name (N);
10388 begin
10389 if Nkind (Nam) = N_Selected_Component then
10390 return Scope (Entity (Prefix (Nam)));
10391 else
10392 return Scope (Entity (Nam));
10393 end if;
10394 end Call_Scope;
10396 begin
10397 if Nkind (N) = N_Function_Call
10398 and then Call_Scope (N)
10399 = Corresponding_Concurrent_Type (Rec_Type)
10400 then
10401 Has_Internal_Call := True;
10402 return Abandon;
10403 else
10404 return OK;
10405 end if;
10406 end Find_Internal_Call;
10408 procedure Search_Access_Discriminant is new
10409 Traverse_Proc (Find_Access_Discriminant);
10411 procedure Search_Current_Instance is new
10412 Traverse_Proc (Find_Current_Instance);
10414 procedure Search_Internal_Call is new
10415 Traverse_Proc (Find_Internal_Call);
10417 -- Start of processing for Requires_Late_Init
10419 begin
10420 -- A component of an object is said to require late initialization
10421 -- if:
10423 -- it has an access discriminant value constrained by a per-object
10424 -- expression;
10426 if Has_Access_Constraint (Defining_Identifier (Decl))
10427 and then No (Expression (Decl))
10428 then
10429 return True;
10431 elsif Present (Expression (Decl)) then
10433 -- it has an initialization expression that includes a name
10434 -- denoting an access discriminant;
10436 Search_Access_Discriminant (Expression (Decl));
10438 if Has_Access_Discriminant then
10439 return True;
10440 end if;
10442 -- or it has an initialization expression that includes a
10443 -- reference to the current instance of the type either by
10444 -- name...
10446 Search_Current_Instance (Expression (Decl));
10448 if References_Current_Instance then
10449 return True;
10450 end if;
10452 -- ...or implicitly as the target object of a call.
10454 if Is_Protected_Record_Type (Rec_Type) then
10455 Search_Internal_Call (Expression (Decl));
10457 if Has_Internal_Call then
10458 return True;
10459 end if;
10460 end if;
10461 end if;
10463 return False;
10464 end Requires_Late_Init;
10466 -----------------------------
10467 -- Has_Late_Init_Component --
10468 -----------------------------
10470 function Has_Late_Init_Component
10471 (Tagged_Rec_Type : Entity_Id) return Boolean
10473 Comp_Id : Entity_Id :=
10474 First_Component (Implementation_Base_Type (Tagged_Rec_Type));
10475 begin
10476 while Present (Comp_Id) loop
10477 if Requires_Late_Init (Decl => Parent (Comp_Id),
10478 Rec_Type => Tagged_Rec_Type)
10479 then
10480 return True; -- found a component that requires late init
10482 elsif Chars (Comp_Id) = Name_uParent
10483 and then Has_Late_Init_Component (Etype (Comp_Id))
10484 then
10485 return True; -- an ancestor type has a late init component
10486 end if;
10488 Next_Component (Comp_Id);
10489 end loop;
10491 return False;
10492 end Has_Late_Init_Component;
10494 ------------------------
10495 -- Tag_Init_Condition --
10496 ------------------------
10498 function Tag_Init_Condition
10499 (Loc : Source_Ptr;
10500 Init_Control_Formal : Entity_Id) return Node_Id is
10501 begin
10502 return Make_Op_Eq (Loc,
10503 New_Occurrence_Of (Init_Control_Formal, Loc),
10504 Make_Mode_Literal (Loc, Full_Init));
10505 end Tag_Init_Condition;
10507 --------------------------
10508 -- Early_Init_Condition --
10509 --------------------------
10511 function Early_Init_Condition
10512 (Loc : Source_Ptr;
10513 Init_Control_Formal : Entity_Id) return Node_Id is
10514 begin
10515 return Make_Op_Ne (Loc,
10516 New_Occurrence_Of (Init_Control_Formal, Loc),
10517 Make_Mode_Literal (Loc, Late_Init_Only));
10518 end Early_Init_Condition;
10520 -------------------------
10521 -- Late_Init_Condition --
10522 -------------------------
10524 function Late_Init_Condition
10525 (Loc : Source_Ptr;
10526 Init_Control_Formal : Entity_Id) return Node_Id is
10527 begin
10528 return Make_Op_Ne (Loc,
10529 New_Occurrence_Of (Init_Control_Formal, Loc),
10530 Make_Mode_Literal (Loc, Early_Init_Only));
10531 end Late_Init_Condition;
10533 end Initialization_Control;
10535 ----------------------------
10536 -- Initialization_Warning --
10537 ----------------------------
10539 procedure Initialization_Warning (E : Entity_Id) is
10540 Warning_Needed : Boolean;
10542 begin
10543 Warning_Needed := False;
10545 if Ekind (Current_Scope) = E_Package
10546 and then Static_Elaboration_Desired (Current_Scope)
10547 then
10548 if Is_Type (E) then
10549 if Is_Record_Type (E) then
10550 if Has_Discriminants (E)
10551 or else Is_Limited_Type (E)
10552 or else Has_Non_Standard_Rep (E)
10553 then
10554 Warning_Needed := True;
10556 else
10557 -- Verify that at least one component has an initialization
10558 -- expression. No need for a warning on a type if all its
10559 -- components have no initialization.
10561 declare
10562 Comp : Entity_Id;
10564 begin
10565 Comp := First_Component (E);
10566 while Present (Comp) loop
10567 pragma Assert
10568 (Nkind (Parent (Comp)) = N_Component_Declaration);
10570 if Present (Expression (Parent (Comp))) then
10571 Warning_Needed := True;
10572 exit;
10573 end if;
10575 Next_Component (Comp);
10576 end loop;
10577 end;
10578 end if;
10580 if Warning_Needed then
10581 Error_Msg_N
10582 ("objects of the type cannot be initialized statically "
10583 & "by default??", Parent (E));
10584 end if;
10585 end if;
10587 else
10588 Error_Msg_N ("object cannot be initialized statically??", E);
10589 end if;
10590 end if;
10591 end Initialization_Warning;
10593 ------------------
10594 -- Init_Formals --
10595 ------------------
10597 function Init_Formals (Typ : Entity_Id; Proc_Id : Entity_Id) return List_Id
10599 Loc : constant Source_Ptr := Sloc (Typ);
10600 Unc_Arr : constant Boolean :=
10601 Is_Array_Type (Typ) and then not Is_Constrained (Typ);
10602 With_Prot : constant Boolean :=
10603 Has_Protected (Typ)
10604 or else (Is_Record_Type (Typ)
10605 and then Is_Protected_Record_Type (Typ));
10606 With_Task : constant Boolean :=
10607 not Global_No_Tasking
10608 and then
10609 (Has_Task (Typ)
10610 or else (Is_Record_Type (Typ)
10611 and then Is_Task_Record_Type (Typ)));
10612 Formals : List_Id;
10614 begin
10615 -- The first parameter is always _Init : [in] out Typ. Note that we need
10616 -- it to be in/out in the case of an unconstrained array, because of the
10617 -- need to have the bounds, and in the case of protected or task record
10618 -- value, because there are default record fields that may be referenced
10619 -- in the generated initialization routine.
10621 Formals := New_List (
10622 Make_Parameter_Specification (Loc,
10623 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uInit),
10624 In_Present => Unc_Arr or else With_Prot or else With_Task,
10625 Out_Present => True,
10626 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
10628 -- For task record value, or type that contains tasks, add two more
10629 -- formals, _Master : Master_Id and _Chain : in out Activation_Chain
10630 -- We also add these parameters for the task record type case.
10632 if With_Task then
10633 Append_To (Formals,
10634 Make_Parameter_Specification (Loc,
10635 Defining_Identifier =>
10636 Make_Defining_Identifier (Loc, Name_uMaster),
10637 Parameter_Type =>
10638 New_Occurrence_Of (Standard_Integer, Loc)));
10640 Set_Has_Master_Entity (Proc_Id);
10642 -- Add _Chain (not done for sequential elaboration policy, see
10643 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
10645 if Partition_Elaboration_Policy /= 'S' then
10646 Append_To (Formals,
10647 Make_Parameter_Specification (Loc,
10648 Defining_Identifier =>
10649 Make_Defining_Identifier (Loc, Name_uChain),
10650 In_Present => True,
10651 Out_Present => True,
10652 Parameter_Type =>
10653 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc)));
10654 end if;
10656 Append_To (Formals,
10657 Make_Parameter_Specification (Loc,
10658 Defining_Identifier =>
10659 Make_Defining_Identifier (Loc, Name_uTask_Name),
10660 In_Present => True,
10661 Parameter_Type => New_Occurrence_Of (Standard_String, Loc)));
10662 end if;
10664 -- Due to certain edge cases such as arrays with null-excluding
10665 -- components being built with the secondary stack it becomes necessary
10666 -- to add a formal to the Init_Proc which controls whether we raise
10667 -- Constraint_Errors on generated calls for internal object
10668 -- declarations.
10670 if Needs_Conditional_Null_Excluding_Check (Typ) then
10671 Append_To (Formals,
10672 Make_Parameter_Specification (Loc,
10673 Defining_Identifier =>
10674 Make_Defining_Identifier (Loc,
10675 New_External_Name (Chars
10676 (Component_Type (Typ)), "_skip_null_excluding_check")),
10677 Expression => New_Occurrence_Of (Standard_False, Loc),
10678 In_Present => True,
10679 Parameter_Type =>
10680 New_Occurrence_Of (Standard_Boolean, Loc)));
10681 end if;
10683 return Formals;
10685 exception
10686 when RE_Not_Available =>
10687 return Empty_List;
10688 end Init_Formals;
10690 -------------------------
10691 -- Init_Secondary_Tags --
10692 -------------------------
10694 procedure Init_Secondary_Tags
10695 (Typ : Entity_Id;
10696 Target : Node_Id;
10697 Init_Tags_List : List_Id;
10698 Stmts_List : List_Id;
10699 Fixed_Comps : Boolean := True;
10700 Variable_Comps : Boolean := True)
10702 Loc : constant Source_Ptr := Sloc (Target);
10704 -- Inherit the C++ tag of the secondary dispatch table of Typ associated
10705 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
10707 procedure Initialize_Tag
10708 (Typ : Entity_Id;
10709 Iface : Entity_Id;
10710 Tag_Comp : Entity_Id;
10711 Iface_Tag : Node_Id);
10712 -- Initialize the tag of the secondary dispatch table of Typ associated
10713 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
10714 -- Compiling under the CPP full ABI compatibility mode, if the ancestor
10715 -- of Typ CPP tagged type we generate code to inherit the contents of
10716 -- the dispatch table directly from the ancestor.
10718 --------------------
10719 -- Initialize_Tag --
10720 --------------------
10722 procedure Initialize_Tag
10723 (Typ : Entity_Id;
10724 Iface : Entity_Id;
10725 Tag_Comp : Entity_Id;
10726 Iface_Tag : Node_Id)
10728 Comp_Typ : Entity_Id;
10729 Offset_To_Top_Comp : Entity_Id := Empty;
10731 begin
10732 -- Initialize pointer to secondary DT associated with the interface
10734 if not Is_Ancestor (Iface, Typ, Use_Full_View => True) then
10735 Append_To (Init_Tags_List,
10736 Make_Assignment_Statement (Loc,
10737 Name =>
10738 Make_Selected_Component (Loc,
10739 Prefix => New_Copy_Tree (Target),
10740 Selector_Name => New_Occurrence_Of (Tag_Comp, Loc)),
10741 Expression =>
10742 New_Occurrence_Of (Iface_Tag, Loc)));
10743 end if;
10745 Comp_Typ := Scope (Tag_Comp);
10747 -- Initialize the entries of the table of interfaces. We generate a
10748 -- different call when the parent of the type has variable size
10749 -- components.
10751 if Comp_Typ /= Etype (Comp_Typ)
10752 and then Is_Variable_Size_Record (Etype (Comp_Typ))
10753 and then Chars (Tag_Comp) /= Name_uTag
10754 then
10755 pragma Assert (Present (DT_Offset_To_Top_Func (Tag_Comp)));
10757 -- Issue error if Set_Dynamic_Offset_To_Top is not available in a
10758 -- configurable run-time environment.
10760 if not RTE_Available (RE_Set_Dynamic_Offset_To_Top) then
10761 Error_Msg_CRT
10762 ("variable size record with interface types", Typ);
10763 return;
10764 end if;
10766 -- Generate:
10767 -- Set_Dynamic_Offset_To_Top
10768 -- (This => Init,
10769 -- Prim_T => Typ'Tag,
10770 -- Interface_T => Iface'Tag,
10771 -- Offset_Value => n,
10772 -- Offset_Func => Fn'Unrestricted_Access)
10774 Append_To (Stmts_List,
10775 Make_Procedure_Call_Statement (Loc,
10776 Name =>
10777 New_Occurrence_Of (RTE (RE_Set_Dynamic_Offset_To_Top), Loc),
10778 Parameter_Associations => New_List (
10779 Make_Attribute_Reference (Loc,
10780 Prefix => New_Copy_Tree (Target),
10781 Attribute_Name => Name_Address),
10783 Unchecked_Convert_To (RTE (RE_Tag),
10784 New_Occurrence_Of
10785 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc)),
10787 Unchecked_Convert_To (RTE (RE_Tag),
10788 New_Occurrence_Of
10789 (Node (First_Elmt (Access_Disp_Table (Iface))),
10790 Loc)),
10792 Unchecked_Convert_To
10793 (RTE (RE_Storage_Offset),
10794 Make_Op_Minus (Loc,
10795 Make_Attribute_Reference (Loc,
10796 Prefix =>
10797 Make_Selected_Component (Loc,
10798 Prefix => New_Copy_Tree (Target),
10799 Selector_Name =>
10800 New_Occurrence_Of (Tag_Comp, Loc)),
10801 Attribute_Name => Name_Position))),
10803 Unchecked_Convert_To (RTE (RE_Offset_To_Top_Function_Ptr),
10804 Make_Attribute_Reference (Loc,
10805 Prefix => New_Occurrence_Of
10806 (DT_Offset_To_Top_Func (Tag_Comp), Loc),
10807 Attribute_Name => Name_Unrestricted_Access)))));
10809 -- In this case the next component stores the value of the offset
10810 -- to the top.
10812 Offset_To_Top_Comp := Next_Entity (Tag_Comp);
10813 pragma Assert (Present (Offset_To_Top_Comp));
10815 Append_To (Init_Tags_List,
10816 Make_Assignment_Statement (Loc,
10817 Name =>
10818 Make_Selected_Component (Loc,
10819 Prefix => New_Copy_Tree (Target),
10820 Selector_Name =>
10821 New_Occurrence_Of (Offset_To_Top_Comp, Loc)),
10823 Expression =>
10824 Make_Op_Minus (Loc,
10825 Make_Attribute_Reference (Loc,
10826 Prefix =>
10827 Make_Selected_Component (Loc,
10828 Prefix => New_Copy_Tree (Target),
10829 Selector_Name => New_Occurrence_Of (Tag_Comp, Loc)),
10830 Attribute_Name => Name_Position))));
10832 -- Normal case: No discriminants in the parent type
10834 else
10835 -- Don't need to set any value if the offset-to-top field is
10836 -- statically set or if this interface shares the primary
10837 -- dispatch table.
10839 if not Building_Static_Secondary_DT (Typ)
10840 and then not Is_Ancestor (Iface, Typ, Use_Full_View => True)
10841 then
10842 Append_To (Stmts_List,
10843 Build_Set_Static_Offset_To_Top (Loc,
10844 Iface_Tag => New_Occurrence_Of (Iface_Tag, Loc),
10845 Offset_Value =>
10846 Unchecked_Convert_To (RTE (RE_Storage_Offset),
10847 Make_Op_Minus (Loc,
10848 Make_Attribute_Reference (Loc,
10849 Prefix =>
10850 Make_Selected_Component (Loc,
10851 Prefix => New_Copy_Tree (Target),
10852 Selector_Name =>
10853 New_Occurrence_Of (Tag_Comp, Loc)),
10854 Attribute_Name => Name_Position)))));
10855 end if;
10857 -- Generate:
10858 -- Register_Interface_Offset
10859 -- (Prim_T => Typ'Tag,
10860 -- Interface_T => Iface'Tag,
10861 -- Is_Constant => True,
10862 -- Offset_Value => n,
10863 -- Offset_Func => null);
10865 if not Building_Static_Secondary_DT (Typ)
10866 and then RTE_Available (RE_Register_Interface_Offset)
10867 then
10868 Append_To (Stmts_List,
10869 Make_Procedure_Call_Statement (Loc,
10870 Name =>
10871 New_Occurrence_Of
10872 (RTE (RE_Register_Interface_Offset), Loc),
10873 Parameter_Associations => New_List (
10874 Unchecked_Convert_To (RTE (RE_Tag),
10875 New_Occurrence_Of
10876 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc)),
10878 Unchecked_Convert_To (RTE (RE_Tag),
10879 New_Occurrence_Of
10880 (Node (First_Elmt (Access_Disp_Table (Iface))), Loc)),
10882 New_Occurrence_Of (Standard_True, Loc),
10884 Unchecked_Convert_To (RTE (RE_Storage_Offset),
10885 Make_Op_Minus (Loc,
10886 Make_Attribute_Reference (Loc,
10887 Prefix =>
10888 Make_Selected_Component (Loc,
10889 Prefix => New_Copy_Tree (Target),
10890 Selector_Name =>
10891 New_Occurrence_Of (Tag_Comp, Loc)),
10892 Attribute_Name => Name_Position))),
10894 Make_Null (Loc))));
10895 end if;
10896 end if;
10897 end Initialize_Tag;
10899 -- Local variables
10901 Full_Typ : Entity_Id;
10902 Ifaces_List : Elist_Id;
10903 Ifaces_Comp_List : Elist_Id;
10904 Ifaces_Tag_List : Elist_Id;
10905 Iface_Elmt : Elmt_Id;
10906 Iface_Comp_Elmt : Elmt_Id;
10907 Iface_Tag_Elmt : Elmt_Id;
10908 Tag_Comp : Node_Id;
10909 In_Variable_Pos : Boolean;
10911 -- Start of processing for Init_Secondary_Tags
10913 begin
10914 -- Handle private types
10916 if Present (Full_View (Typ)) then
10917 Full_Typ := Full_View (Typ);
10918 else
10919 Full_Typ := Typ;
10920 end if;
10922 Collect_Interfaces_Info
10923 (Full_Typ, Ifaces_List, Ifaces_Comp_List, Ifaces_Tag_List);
10925 Iface_Elmt := First_Elmt (Ifaces_List);
10926 Iface_Comp_Elmt := First_Elmt (Ifaces_Comp_List);
10927 Iface_Tag_Elmt := First_Elmt (Ifaces_Tag_List);
10928 while Present (Iface_Elmt) loop
10929 Tag_Comp := Node (Iface_Comp_Elmt);
10931 -- Check if parent of record type has variable size components
10933 In_Variable_Pos := Scope (Tag_Comp) /= Etype (Scope (Tag_Comp))
10934 and then Is_Variable_Size_Record (Etype (Scope (Tag_Comp)));
10936 -- If we are compiling under the CPP full ABI compatibility mode and
10937 -- the ancestor is a CPP_Pragma tagged type then we generate code to
10938 -- initialize the secondary tag components from tags that reference
10939 -- secondary tables filled with copy of parent slots.
10941 if Is_CPP_Class (Root_Type (Full_Typ)) then
10943 -- Reject interface components located at variable offset in
10944 -- C++ derivations. This is currently unsupported.
10946 if not Fixed_Comps and then In_Variable_Pos then
10948 -- Locate the first dynamic component of the record. Done to
10949 -- improve the text of the warning.
10951 declare
10952 Comp : Entity_Id;
10953 Comp_Typ : Entity_Id;
10955 begin
10956 Comp := First_Entity (Typ);
10957 while Present (Comp) loop
10958 Comp_Typ := Etype (Comp);
10960 if Ekind (Comp) /= E_Discriminant
10961 and then not Is_Tag (Comp)
10962 then
10963 exit when
10964 (Is_Record_Type (Comp_Typ)
10965 and then
10966 Is_Variable_Size_Record (Base_Type (Comp_Typ)))
10967 or else
10968 (Is_Array_Type (Comp_Typ)
10969 and then Is_Variable_Size_Array (Comp_Typ));
10970 end if;
10972 Next_Entity (Comp);
10973 end loop;
10975 pragma Assert (Present (Comp));
10977 -- Move this check to sem???
10978 Error_Msg_Node_2 := Comp;
10979 Error_Msg_NE
10980 ("parent type & with dynamic component & cannot be parent"
10981 & " of 'C'P'P derivation if new interfaces are present",
10982 Typ, Scope (Original_Record_Component (Comp)));
10984 Error_Msg_Sloc :=
10985 Sloc (Scope (Original_Record_Component (Comp)));
10986 Error_Msg_NE
10987 ("type derived from 'C'P'P type & defined #",
10988 Typ, Scope (Original_Record_Component (Comp)));
10990 -- Avoid duplicated warnings
10992 exit;
10993 end;
10995 -- Initialize secondary tags
10997 else
10998 Initialize_Tag
10999 (Typ => Full_Typ,
11000 Iface => Node (Iface_Elmt),
11001 Tag_Comp => Tag_Comp,
11002 Iface_Tag => Node (Iface_Tag_Elmt));
11003 end if;
11005 -- Otherwise generate code to initialize the tag
11007 else
11008 if (In_Variable_Pos and then Variable_Comps)
11009 or else (not In_Variable_Pos and then Fixed_Comps)
11010 then
11011 Initialize_Tag
11012 (Typ => Full_Typ,
11013 Iface => Node (Iface_Elmt),
11014 Tag_Comp => Tag_Comp,
11015 Iface_Tag => Node (Iface_Tag_Elmt));
11016 end if;
11017 end if;
11019 Next_Elmt (Iface_Elmt);
11020 Next_Elmt (Iface_Comp_Elmt);
11021 Next_Elmt (Iface_Tag_Elmt);
11022 end loop;
11023 end Init_Secondary_Tags;
11025 ----------------------------
11026 -- Is_Null_Statement_List --
11027 ----------------------------
11029 function Is_Null_Statement_List (Stmts : List_Id) return Boolean is
11030 Stmt : Node_Id;
11032 begin
11033 -- We must skip SCIL nodes because they may have been added to the list
11034 -- by Insert_Actions.
11036 Stmt := First_Non_SCIL_Node (Stmts);
11037 while Present (Stmt) loop
11038 if Nkind (Stmt) = N_Case_Statement then
11039 declare
11040 Alt : Node_Id;
11041 begin
11042 Alt := First (Alternatives (Stmt));
11043 while Present (Alt) loop
11044 if not Is_Null_Statement_List (Statements (Alt)) then
11045 return False;
11046 end if;
11048 Next (Alt);
11049 end loop;
11050 end;
11052 elsif Nkind (Stmt) /= N_Null_Statement then
11053 return False;
11054 end if;
11056 Stmt := Next_Non_SCIL_Node (Stmt);
11057 end loop;
11059 return True;
11060 end Is_Null_Statement_List;
11062 ----------------------------------------
11063 -- Make_Controlling_Function_Wrappers --
11064 ----------------------------------------
11066 procedure Make_Controlling_Function_Wrappers
11067 (Tag_Typ : Entity_Id;
11068 Decl_List : out List_Id;
11069 Body_List : out List_Id)
11071 Loc : constant Source_Ptr := Sloc (Tag_Typ);
11073 function Make_Wrapper_Specification (Subp : Entity_Id) return Node_Id;
11074 -- Returns a function specification with the same profile as Subp
11076 --------------------------------
11077 -- Make_Wrapper_Specification --
11078 --------------------------------
11080 function Make_Wrapper_Specification (Subp : Entity_Id) return Node_Id is
11081 begin
11082 return
11083 Make_Function_Specification (Loc,
11084 Defining_Unit_Name =>
11085 Make_Defining_Identifier (Loc,
11086 Chars => Chars (Subp)),
11087 Parameter_Specifications =>
11088 Copy_Parameter_List (Subp),
11089 Result_Definition =>
11090 New_Occurrence_Of (Etype (Subp), Loc));
11091 end Make_Wrapper_Specification;
11093 Prim_Elmt : Elmt_Id;
11094 Subp : Entity_Id;
11095 Actual_List : List_Id;
11096 Formal : Entity_Id;
11097 Par_Formal : Entity_Id;
11098 Ext_Aggr : Node_Id;
11099 Formal_Node : Node_Id;
11100 Func_Body : Node_Id;
11101 Func_Decl : Node_Id;
11102 Func_Id : Entity_Id;
11104 -- Start of processing for Make_Controlling_Function_Wrappers
11106 begin
11107 Decl_List := New_List;
11108 Body_List := New_List;
11110 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
11111 while Present (Prim_Elmt) loop
11112 Subp := Node (Prim_Elmt);
11114 -- If a primitive function with a controlling result of the type has
11115 -- not been overridden by the user, then we must create a wrapper
11116 -- function here that effectively overrides it and invokes the
11117 -- (non-abstract) parent function. This can only occur for a null
11118 -- extension. Note that functions with anonymous controlling access
11119 -- results don't qualify and must be overridden. We also exclude
11120 -- Input attributes, since each type will have its own version of
11121 -- Input constructed by the expander. The test for Comes_From_Source
11122 -- is needed to distinguish inherited operations from renamings
11123 -- (which also have Alias set). We exclude internal entities with
11124 -- Interface_Alias to avoid generating duplicated wrappers since
11125 -- the primitive which covers the interface is also available in
11126 -- the list of primitive operations.
11128 -- The function may be abstract, or require_Overriding may be set
11129 -- for it, because tests for null extensions may already have reset
11130 -- the Is_Abstract_Subprogram_Flag. If Requires_Overriding is not
11131 -- set, functions that need wrappers are recognized by having an
11132 -- alias that returns the parent type.
11134 if Comes_From_Source (Subp)
11135 or else No (Alias (Subp))
11136 or else Present (Interface_Alias (Subp))
11137 or else Ekind (Subp) /= E_Function
11138 or else not Has_Controlling_Result (Subp)
11139 or else Is_Access_Type (Etype (Subp))
11140 or else Is_Abstract_Subprogram (Alias (Subp))
11141 or else Is_TSS (Subp, TSS_Stream_Input)
11142 then
11143 goto Next_Prim;
11145 elsif Is_Abstract_Subprogram (Subp)
11146 or else Requires_Overriding (Subp)
11147 or else
11148 (Is_Null_Extension (Etype (Subp))
11149 and then Etype (Alias (Subp)) /= Etype (Subp))
11150 then
11151 -- If there is a non-overloadable homonym in the current
11152 -- scope, the implicit declaration remains invisible.
11153 -- We check the current entity with the same name, or its
11154 -- homonym in case the derivation takes place after the
11155 -- hiding object declaration.
11157 if Present (Current_Entity (Subp)) then
11158 declare
11159 Curr : constant Entity_Id := Current_Entity (Subp);
11160 Prev : constant Entity_Id := Homonym (Curr);
11161 begin
11162 if (Comes_From_Source (Curr)
11163 and then Scope (Curr) = Current_Scope
11164 and then not Is_Overloadable (Curr))
11165 or else
11166 (Present (Prev)
11167 and then Comes_From_Source (Prev)
11168 and then Scope (Prev) = Current_Scope
11169 and then not Is_Overloadable (Prev))
11170 then
11171 goto Next_Prim;
11172 end if;
11173 end;
11174 end if;
11176 Func_Decl :=
11177 Make_Subprogram_Declaration (Loc,
11178 Specification => Make_Wrapper_Specification (Subp));
11180 Append_To (Decl_List, Func_Decl);
11182 -- Build a wrapper body that calls the parent function. The body
11183 -- contains a single return statement that returns an extension
11184 -- aggregate whose ancestor part is a call to the parent function,
11185 -- passing the formals as actuals (with any controlling arguments
11186 -- converted to the types of the corresponding formals of the
11187 -- parent function, which might be anonymous access types), and
11188 -- having a null extension.
11190 Formal := First_Formal (Subp);
11191 Par_Formal := First_Formal (Alias (Subp));
11192 Formal_Node :=
11193 First (Parameter_Specifications (Specification (Func_Decl)));
11195 if Present (Formal) then
11196 Actual_List := New_List;
11198 while Present (Formal) loop
11199 if Is_Controlling_Formal (Formal) then
11200 Append_To (Actual_List,
11201 Make_Type_Conversion (Loc,
11202 Subtype_Mark =>
11203 New_Occurrence_Of (Etype (Par_Formal), Loc),
11204 Expression =>
11205 New_Occurrence_Of
11206 (Defining_Identifier (Formal_Node), Loc)));
11207 else
11208 Append_To
11209 (Actual_List,
11210 New_Occurrence_Of
11211 (Defining_Identifier (Formal_Node), Loc));
11212 end if;
11214 Next_Formal (Formal);
11215 Next_Formal (Par_Formal);
11216 Next (Formal_Node);
11217 end loop;
11218 else
11219 Actual_List := No_List;
11220 end if;
11222 Ext_Aggr :=
11223 Make_Extension_Aggregate (Loc,
11224 Ancestor_Part =>
11225 Make_Function_Call (Loc,
11226 Name =>
11227 New_Occurrence_Of (Alias (Subp), Loc),
11228 Parameter_Associations => Actual_List),
11229 Null_Record_Present => True);
11231 -- GNATprove will use expression of an expression function as an
11232 -- implicit postcondition. GNAT will also benefit from expression
11233 -- function to avoid premature freezing, but would struggle if we
11234 -- added an expression function to freezing actions, so we create
11235 -- the expanded form directly.
11237 if GNATprove_Mode then
11238 Func_Body :=
11239 Make_Expression_Function (Loc,
11240 Specification =>
11241 Make_Wrapper_Specification (Subp),
11242 Expression => Ext_Aggr);
11243 else
11244 Func_Body :=
11245 Make_Subprogram_Body (Loc,
11246 Specification =>
11247 Make_Wrapper_Specification (Subp),
11248 Declarations => Empty_List,
11249 Handled_Statement_Sequence =>
11250 Make_Handled_Sequence_Of_Statements (Loc,
11251 Statements => New_List (
11252 Make_Simple_Return_Statement (Loc,
11253 Expression => Ext_Aggr))));
11254 Set_Was_Expression_Function (Func_Body);
11255 end if;
11257 Append_To (Body_List, Func_Body);
11259 -- Replace the inherited function with the wrapper function in the
11260 -- primitive operations list. We add the minimum decoration needed
11261 -- to override interface primitives.
11263 Func_Id := Defining_Unit_Name (Specification (Func_Decl));
11265 Mutate_Ekind (Func_Id, E_Function);
11266 Set_Is_Wrapper (Func_Id);
11268 -- Corresponding_Spec will be set again to the same value during
11269 -- analysis, but we need this information earlier.
11270 -- Expand_N_Freeze_Entity needs to know whether a subprogram body
11271 -- is a wrapper's body in order to get check suppression right.
11273 Set_Corresponding_Spec (Func_Body, Func_Id);
11274 end if;
11276 <<Next_Prim>>
11277 Next_Elmt (Prim_Elmt);
11278 end loop;
11279 end Make_Controlling_Function_Wrappers;
11281 ------------------
11282 -- Make_Eq_Body --
11283 ------------------
11285 function Make_Eq_Body
11286 (Typ : Entity_Id;
11287 Eq_Name : Name_Id) return Node_Id
11289 Loc : constant Source_Ptr := Sloc (Parent (Typ));
11290 Decl : Node_Id;
11291 Def : constant Node_Id := Parent (Typ);
11292 Stmts : constant List_Id := New_List;
11293 Variant_Case : Boolean := Has_Discriminants (Typ);
11294 Comps : Node_Id := Empty;
11295 Typ_Def : Node_Id := Type_Definition (Def);
11297 begin
11298 Decl :=
11299 Predef_Spec_Or_Body (Loc,
11300 Tag_Typ => Typ,
11301 Name => Eq_Name,
11302 Profile => New_List (
11303 Make_Parameter_Specification (Loc,
11304 Defining_Identifier =>
11305 Make_Defining_Identifier (Loc, Name_X),
11306 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
11308 Make_Parameter_Specification (Loc,
11309 Defining_Identifier =>
11310 Make_Defining_Identifier (Loc, Name_Y),
11311 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
11313 Ret_Type => Standard_Boolean,
11314 For_Body => True);
11316 if Variant_Case then
11317 if Nkind (Typ_Def) = N_Derived_Type_Definition then
11318 Typ_Def := Record_Extension_Part (Typ_Def);
11319 end if;
11321 if Present (Typ_Def) then
11322 Comps := Component_List (Typ_Def);
11323 end if;
11325 Variant_Case :=
11326 Present (Comps) and then Present (Variant_Part (Comps));
11327 end if;
11329 if Variant_Case then
11330 Append_To (Stmts,
11331 Make_Eq_If (Typ, Discriminant_Specifications (Def)));
11332 Append_List_To (Stmts, Make_Eq_Case (Typ, Comps));
11333 Append_To (Stmts,
11334 Make_Simple_Return_Statement (Loc,
11335 Expression => New_Occurrence_Of (Standard_True, Loc)));
11337 else
11338 Append_To (Stmts,
11339 Make_Simple_Return_Statement (Loc,
11340 Expression =>
11341 Expand_Record_Equality
11342 (Typ,
11343 Typ => Typ,
11344 Lhs => Make_Identifier (Loc, Name_X),
11345 Rhs => Make_Identifier (Loc, Name_Y))));
11346 end if;
11348 Set_Handled_Statement_Sequence
11349 (Decl, Make_Handled_Sequence_Of_Statements (Loc, Stmts));
11350 return Decl;
11351 end Make_Eq_Body;
11353 ------------------
11354 -- Make_Eq_Case --
11355 ------------------
11357 -- <Make_Eq_If shared components>
11359 -- case X.D1 is
11360 -- when V1 => <Make_Eq_Case> on subcomponents
11361 -- ...
11362 -- when Vn => <Make_Eq_Case> on subcomponents
11363 -- end case;
11365 function Make_Eq_Case
11366 (E : Entity_Id;
11367 CL : Node_Id;
11368 Discrs : Elist_Id := New_Elmt_List) return List_Id
11370 Loc : constant Source_Ptr := Sloc (E);
11371 Result : constant List_Id := New_List;
11372 Variant : Node_Id;
11373 Alt_List : List_Id;
11375 function Corresponding_Formal (C : Node_Id) return Entity_Id;
11376 -- Given the discriminant that controls a given variant of an unchecked
11377 -- union, find the formal of the equality function that carries the
11378 -- inferred value of the discriminant.
11380 function External_Name (E : Entity_Id) return Name_Id;
11381 -- The value of a given discriminant is conveyed in the corresponding
11382 -- formal parameter of the equality routine. The name of this formal
11383 -- parameter carries a one-character suffix which is removed here.
11385 --------------------------
11386 -- Corresponding_Formal --
11387 --------------------------
11389 function Corresponding_Formal (C : Node_Id) return Entity_Id is
11390 Discr : constant Entity_Id := Entity (Name (Variant_Part (C)));
11391 Elm : Elmt_Id;
11393 begin
11394 Elm := First_Elmt (Discrs);
11395 while Present (Elm) loop
11396 if Chars (Discr) = External_Name (Node (Elm)) then
11397 return Node (Elm);
11398 end if;
11400 Next_Elmt (Elm);
11401 end loop;
11403 -- A formal of the proper name must be found
11405 raise Program_Error;
11406 end Corresponding_Formal;
11408 -------------------
11409 -- External_Name --
11410 -------------------
11412 function External_Name (E : Entity_Id) return Name_Id is
11413 begin
11414 Get_Name_String (Chars (E));
11415 Name_Len := Name_Len - 1;
11416 return Name_Find;
11417 end External_Name;
11419 -- Start of processing for Make_Eq_Case
11421 begin
11422 Append_To (Result, Make_Eq_If (E, Component_Items (CL)));
11424 if No (Variant_Part (CL)) then
11425 return Result;
11426 end if;
11428 Variant := First_Non_Pragma (Variants (Variant_Part (CL)));
11430 if No (Variant) then
11431 return Result;
11432 end if;
11434 Alt_List := New_List;
11435 while Present (Variant) loop
11436 Append_To (Alt_List,
11437 Make_Case_Statement_Alternative (Loc,
11438 Discrete_Choices => New_Copy_List (Discrete_Choices (Variant)),
11439 Statements =>
11440 Make_Eq_Case (E, Component_List (Variant), Discrs)));
11441 Next_Non_Pragma (Variant);
11442 end loop;
11444 -- If we have an Unchecked_Union, use one of the parameters of the
11445 -- enclosing equality routine that captures the discriminant, to use
11446 -- as the expression in the generated case statement.
11448 if Is_Unchecked_Union (E) then
11449 Append_To (Result,
11450 Make_Case_Statement (Loc,
11451 Expression =>
11452 New_Occurrence_Of (Corresponding_Formal (CL), Loc),
11453 Alternatives => Alt_List));
11455 else
11456 Append_To (Result,
11457 Make_Case_Statement (Loc,
11458 Expression =>
11459 Make_Selected_Component (Loc,
11460 Prefix => Make_Identifier (Loc, Name_X),
11461 Selector_Name => New_Copy (Name (Variant_Part (CL)))),
11462 Alternatives => Alt_List));
11463 end if;
11465 return Result;
11466 end Make_Eq_Case;
11468 ----------------
11469 -- Make_Eq_If --
11470 ----------------
11472 -- Generates:
11474 -- if
11475 -- X.C1 /= Y.C1
11476 -- or else
11477 -- X.C2 /= Y.C2
11478 -- ...
11479 -- then
11480 -- return False;
11481 -- end if;
11483 -- or a null statement if the list L is empty
11485 -- Equality may be user-defined for a given component type, in which case
11486 -- a function call is constructed instead of an operator node. This is an
11487 -- Ada 2012 change in the composability of equality for untagged composite
11488 -- types.
11490 function Make_Eq_If
11491 (E : Entity_Id;
11492 L : List_Id) return Node_Id
11494 Loc : constant Source_Ptr := Sloc (E);
11496 C : Node_Id;
11497 Cond : Node_Id;
11498 Field_Name : Name_Id;
11499 Next_Test : Node_Id;
11500 Typ : Entity_Id;
11502 begin
11503 if No (L) then
11504 return Make_Null_Statement (Loc);
11506 else
11507 Cond := Empty;
11509 C := First_Non_Pragma (L);
11510 while Present (C) loop
11511 Typ := Etype (Defining_Identifier (C));
11512 Field_Name := Chars (Defining_Identifier (C));
11514 -- The tags must not be compared: they are not part of the value.
11515 -- Ditto for parent interfaces because their equality operator is
11516 -- abstract.
11518 -- Note also that in the following, we use Make_Identifier for
11519 -- the component names. Use of New_Occurrence_Of to identify the
11520 -- components would be incorrect because the wrong entities for
11521 -- discriminants could be picked up in the private type case.
11523 if Field_Name = Name_uParent
11524 and then Is_Interface (Typ)
11525 then
11526 null;
11528 elsif Field_Name /= Name_uTag then
11529 declare
11530 Lhs : constant Node_Id :=
11531 Make_Selected_Component (Loc,
11532 Prefix => Make_Identifier (Loc, Name_X),
11533 Selector_Name => Make_Identifier (Loc, Field_Name));
11535 Rhs : constant Node_Id :=
11536 Make_Selected_Component (Loc,
11537 Prefix => Make_Identifier (Loc, Name_Y),
11538 Selector_Name => Make_Identifier (Loc, Field_Name));
11539 Eq_Call : Node_Id;
11541 begin
11542 -- Build equality code with a user-defined operator, if
11543 -- available, and with the predefined "=" otherwise. For
11544 -- compatibility with older Ada versions, we also use the
11545 -- predefined operation if the component-type equality is
11546 -- abstract, rather than raising Program_Error.
11548 if Ada_Version < Ada_2012 then
11549 Next_Test := Make_Op_Ne (Loc, Lhs, Rhs);
11551 else
11552 Eq_Call := Build_Eq_Call (Typ, Loc, Lhs, Rhs);
11554 if No (Eq_Call) then
11555 Next_Test := Make_Op_Ne (Loc, Lhs, Rhs);
11557 -- If a component has a defined abstract equality, its
11558 -- application raises Program_Error on that component
11559 -- and therefore on the current variant.
11561 elsif Nkind (Eq_Call) = N_Raise_Program_Error then
11562 Set_Etype (Eq_Call, Standard_Boolean);
11563 Next_Test := Make_Op_Not (Loc, Eq_Call);
11565 else
11566 Next_Test := Make_Op_Not (Loc, Eq_Call);
11567 end if;
11568 end if;
11569 end;
11571 Evolve_Or_Else (Cond, Next_Test);
11572 end if;
11574 Next_Non_Pragma (C);
11575 end loop;
11577 if No (Cond) then
11578 return Make_Null_Statement (Loc);
11580 else
11581 return
11582 Make_Implicit_If_Statement (E,
11583 Condition => Cond,
11584 Then_Statements => New_List (
11585 Make_Simple_Return_Statement (Loc,
11586 Expression => New_Occurrence_Of (Standard_False, Loc))));
11587 end if;
11588 end if;
11589 end Make_Eq_If;
11591 -------------------
11592 -- Make_Neq_Body --
11593 -------------------
11595 function Make_Neq_Body (Tag_Typ : Entity_Id) return Node_Id is
11597 function Is_Predefined_Neq_Renaming (Prim : Node_Id) return Boolean;
11598 -- Returns true if Prim is a renaming of an unresolved predefined
11599 -- inequality operation.
11601 --------------------------------
11602 -- Is_Predefined_Neq_Renaming --
11603 --------------------------------
11605 function Is_Predefined_Neq_Renaming (Prim : Node_Id) return Boolean is
11606 begin
11607 return Chars (Prim) /= Name_Op_Ne
11608 and then Present (Alias (Prim))
11609 and then Comes_From_Source (Prim)
11610 and then Is_Intrinsic_Subprogram (Alias (Prim))
11611 and then Chars (Alias (Prim)) = Name_Op_Ne;
11612 end Is_Predefined_Neq_Renaming;
11614 -- Local variables
11616 Loc : constant Source_Ptr := Sloc (Parent (Tag_Typ));
11617 Decl : Node_Id;
11618 Eq_Prim : Entity_Id;
11619 Left_Op : Entity_Id;
11620 Renaming_Prim : Entity_Id;
11621 Right_Op : Entity_Id;
11622 Target : Entity_Id;
11624 -- Start of processing for Make_Neq_Body
11626 begin
11627 -- For a call on a renaming of a dispatching subprogram that is
11628 -- overridden, if the overriding occurred before the renaming, then
11629 -- the body executed is that of the overriding declaration, even if the
11630 -- overriding declaration is not visible at the place of the renaming;
11631 -- otherwise, the inherited or predefined subprogram is called, see
11632 -- (RM 8.5.4(8)).
11634 -- Stage 1: Search for a renaming of the inequality primitive and also
11635 -- search for an overriding of the equality primitive located before the
11636 -- renaming declaration.
11638 declare
11639 Elmt : Elmt_Id;
11640 Prim : Node_Id;
11642 begin
11643 Eq_Prim := Empty;
11644 Renaming_Prim := Empty;
11646 Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
11647 while Present (Elmt) loop
11648 Prim := Node (Elmt);
11650 if Is_User_Defined_Equality (Prim) and then No (Alias (Prim)) then
11651 if No (Renaming_Prim) then
11652 pragma Assert (No (Eq_Prim));
11653 Eq_Prim := Prim;
11654 end if;
11656 elsif Is_Predefined_Neq_Renaming (Prim) then
11657 Renaming_Prim := Prim;
11658 end if;
11660 Next_Elmt (Elmt);
11661 end loop;
11662 end;
11664 -- No further action needed if no renaming was found
11666 if No (Renaming_Prim) then
11667 return Empty;
11668 end if;
11670 -- Stage 2: Replace the renaming declaration by a subprogram declaration
11671 -- (required to add its body)
11673 Decl := Parent (Parent (Renaming_Prim));
11674 Rewrite (Decl,
11675 Make_Subprogram_Declaration (Loc,
11676 Specification => Specification (Decl)));
11677 Set_Analyzed (Decl);
11679 -- Remove the decoration of intrinsic renaming subprogram
11681 Set_Is_Intrinsic_Subprogram (Renaming_Prim, False);
11682 Set_Convention (Renaming_Prim, Convention_Ada);
11683 Set_Alias (Renaming_Prim, Empty);
11684 Set_Has_Completion (Renaming_Prim, False);
11686 -- Stage 3: Build the corresponding body
11688 Left_Op := First_Formal (Renaming_Prim);
11689 Right_Op := Next_Formal (Left_Op);
11691 Decl :=
11692 Predef_Spec_Or_Body (Loc,
11693 Tag_Typ => Tag_Typ,
11694 Name => Chars (Renaming_Prim),
11695 Profile => New_List (
11696 Make_Parameter_Specification (Loc,
11697 Defining_Identifier =>
11698 Make_Defining_Identifier (Loc, Chars (Left_Op)),
11699 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
11701 Make_Parameter_Specification (Loc,
11702 Defining_Identifier =>
11703 Make_Defining_Identifier (Loc, Chars (Right_Op)),
11704 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
11706 Ret_Type => Standard_Boolean,
11707 For_Body => True);
11709 -- If the overriding of the equality primitive occurred before the
11710 -- renaming, then generate:
11712 -- function <Neq_Name> (X : Y : Typ) return Boolean is
11713 -- begin
11714 -- return not Oeq (X, Y);
11715 -- end;
11717 if Present (Eq_Prim) then
11718 Target := Eq_Prim;
11720 -- Otherwise build a nested subprogram which performs the predefined
11721 -- evaluation of the equality operator. That is, generate:
11723 -- function <Neq_Name> (X : Y : Typ) return Boolean is
11724 -- function Oeq (X : Y) return Boolean is
11725 -- begin
11726 -- <<body of default implementation>>
11727 -- end;
11728 -- begin
11729 -- return not Oeq (X, Y);
11730 -- end;
11732 else
11733 declare
11734 Local_Subp : Node_Id;
11735 begin
11736 Local_Subp := Make_Eq_Body (Tag_Typ, Name_Op_Eq);
11737 Set_Declarations (Decl, New_List (Local_Subp));
11738 Target := Defining_Entity (Local_Subp);
11739 end;
11740 end if;
11742 Set_Handled_Statement_Sequence
11743 (Decl,
11744 Make_Handled_Sequence_Of_Statements (Loc, New_List (
11745 Make_Simple_Return_Statement (Loc,
11746 Expression =>
11747 Make_Op_Not (Loc,
11748 Make_Function_Call (Loc,
11749 Name => New_Occurrence_Of (Target, Loc),
11750 Parameter_Associations => New_List (
11751 Make_Identifier (Loc, Chars (Left_Op)),
11752 Make_Identifier (Loc, Chars (Right_Op)))))))));
11754 return Decl;
11755 end Make_Neq_Body;
11757 -------------------------------
11758 -- Make_Null_Procedure_Specs --
11759 -------------------------------
11761 function Make_Null_Procedure_Specs (Tag_Typ : Entity_Id) return List_Id is
11762 Decl_List : constant List_Id := New_List;
11763 Loc : constant Source_Ptr := Sloc (Tag_Typ);
11764 Formal : Entity_Id;
11765 New_Param_Spec : Node_Id;
11766 New_Spec : Node_Id;
11767 Parent_Subp : Entity_Id;
11768 Prim_Elmt : Elmt_Id;
11769 Subp : Entity_Id;
11771 begin
11772 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
11773 while Present (Prim_Elmt) loop
11774 Subp := Node (Prim_Elmt);
11776 -- If a null procedure inherited from an interface has not been
11777 -- overridden, then we build a null procedure declaration to
11778 -- override the inherited procedure.
11780 Parent_Subp := Alias (Subp);
11782 if Present (Parent_Subp)
11783 and then Is_Null_Interface_Primitive (Parent_Subp)
11784 then
11785 -- The null procedure spec is copied from the inherited procedure,
11786 -- except for the IS NULL (which must be added) and the overriding
11787 -- indicators (which must be removed, if present).
11789 New_Spec :=
11790 Copy_Subprogram_Spec (Subprogram_Specification (Subp), Loc);
11792 Set_Null_Present (New_Spec, True);
11793 Set_Must_Override (New_Spec, False);
11794 Set_Must_Not_Override (New_Spec, False);
11796 Formal := First_Formal (Subp);
11797 New_Param_Spec := First (Parameter_Specifications (New_Spec));
11799 while Present (Formal) loop
11801 -- For controlling arguments we must change their parameter
11802 -- type to reference the tagged type (instead of the interface
11803 -- type).
11805 if Is_Controlling_Formal (Formal) then
11806 if Nkind (Parameter_Type (Parent (Formal))) = N_Identifier
11807 then
11808 Set_Parameter_Type (New_Param_Spec,
11809 New_Occurrence_Of (Tag_Typ, Loc));
11811 else pragma Assert
11812 (Nkind (Parameter_Type (Parent (Formal))) =
11813 N_Access_Definition);
11814 Set_Subtype_Mark (Parameter_Type (New_Param_Spec),
11815 New_Occurrence_Of (Tag_Typ, Loc));
11816 end if;
11817 end if;
11819 Next_Formal (Formal);
11820 Next (New_Param_Spec);
11821 end loop;
11823 Append_To (Decl_List,
11824 Make_Subprogram_Declaration (Loc,
11825 Specification => New_Spec));
11826 end if;
11828 Next_Elmt (Prim_Elmt);
11829 end loop;
11831 return Decl_List;
11832 end Make_Null_Procedure_Specs;
11834 ---------------------------------------
11835 -- Make_Predefined_Primitive_Eq_Spec --
11836 ---------------------------------------
11838 procedure Make_Predefined_Primitive_Eq_Spec
11839 (Tag_Typ : Entity_Id;
11840 Predef_List : List_Id;
11841 Renamed_Eq : out Entity_Id)
11843 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean;
11844 -- Returns true if Prim is a renaming of an unresolved predefined
11845 -- equality operation.
11847 -------------------------------
11848 -- Is_Predefined_Eq_Renaming --
11849 -------------------------------
11851 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean is
11852 begin
11853 return Chars (Prim) /= Name_Op_Eq
11854 and then Present (Alias (Prim))
11855 and then Comes_From_Source (Prim)
11856 and then Is_Intrinsic_Subprogram (Alias (Prim))
11857 and then Chars (Alias (Prim)) = Name_Op_Eq;
11858 end Is_Predefined_Eq_Renaming;
11860 -- Local variables
11862 Loc : constant Source_Ptr := Sloc (Tag_Typ);
11864 Eq_Name : Name_Id := Name_Op_Eq;
11865 Eq_Needed : Boolean := True;
11866 Eq_Spec : Node_Id;
11867 Prim : Elmt_Id;
11869 Has_Predef_Eq_Renaming : Boolean := False;
11870 -- Set to True if Tag_Typ has a primitive that renames the predefined
11871 -- equality operator. Used to implement (RM 8-5-4(8)).
11873 -- Start of processing for Make_Predefined_Primitive_Specs
11875 begin
11876 Renamed_Eq := Empty;
11878 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
11879 while Present (Prim) loop
11881 -- If a primitive is encountered that renames the predefined equality
11882 -- operator before reaching any explicit equality primitive, then we
11883 -- still need to create a predefined equality function, because calls
11884 -- to it can occur via the renaming. A new name is created for the
11885 -- equality to avoid conflicting with any user-defined equality.
11886 -- (Note that this doesn't account for renamings of equality nested
11887 -- within subpackages???)
11889 if Is_Predefined_Eq_Renaming (Node (Prim)) then
11890 Has_Predef_Eq_Renaming := True;
11891 Eq_Name := New_External_Name (Chars (Node (Prim)), 'E');
11893 -- User-defined equality
11895 elsif Is_User_Defined_Equality (Node (Prim)) then
11896 if No (Alias (Node (Prim)))
11897 or else Nkind (Unit_Declaration_Node (Node (Prim))) =
11898 N_Subprogram_Renaming_Declaration
11899 then
11900 Eq_Needed := False;
11901 exit;
11903 -- If the parent is not an interface type and has an abstract
11904 -- equality function explicitly defined in the sources, then the
11905 -- inherited equality is abstract as well, and no body can be
11906 -- created for it.
11908 elsif not Is_Interface (Etype (Tag_Typ))
11909 and then Present (Alias (Node (Prim)))
11910 and then Comes_From_Source (Alias (Node (Prim)))
11911 and then Is_Abstract_Subprogram (Alias (Node (Prim)))
11912 then
11913 Eq_Needed := False;
11914 exit;
11916 -- If the type has an equality function corresponding with a
11917 -- primitive defined in an interface type, the inherited equality
11918 -- is abstract as well, and no body can be created for it.
11920 elsif Present (Alias (Node (Prim)))
11921 and then Comes_From_Source (Ultimate_Alias (Node (Prim)))
11922 and then
11923 Is_Interface
11924 (Find_Dispatching_Type (Ultimate_Alias (Node (Prim))))
11925 then
11926 Eq_Needed := False;
11927 exit;
11928 end if;
11929 end if;
11931 Next_Elmt (Prim);
11932 end loop;
11934 -- If a renaming of predefined equality was found but there was no
11935 -- user-defined equality (so Eq_Needed is still true), then set the name
11936 -- back to Name_Op_Eq. But in the case where a user-defined equality was
11937 -- located after such a renaming, then the predefined equality function
11938 -- is still needed, so Eq_Needed must be set back to True.
11940 if Eq_Name /= Name_Op_Eq then
11941 if Eq_Needed then
11942 Eq_Name := Name_Op_Eq;
11943 else
11944 Eq_Needed := True;
11945 end if;
11946 end if;
11948 if Eq_Needed then
11949 Eq_Spec := Predef_Spec_Or_Body (Loc,
11950 Tag_Typ => Tag_Typ,
11951 Name => Eq_Name,
11952 Profile => New_List (
11953 Make_Parameter_Specification (Loc,
11954 Defining_Identifier =>
11955 Make_Defining_Identifier (Loc, Name_X),
11956 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
11958 Make_Parameter_Specification (Loc,
11959 Defining_Identifier =>
11960 Make_Defining_Identifier (Loc, Name_Y),
11961 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
11962 Ret_Type => Standard_Boolean);
11963 Append_To (Predef_List, Eq_Spec);
11965 if Has_Predef_Eq_Renaming then
11966 Renamed_Eq := Defining_Unit_Name (Specification (Eq_Spec));
11968 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
11969 while Present (Prim) loop
11971 -- Any renamings of equality that appeared before an overriding
11972 -- equality must be updated to refer to the entity for the
11973 -- predefined equality, otherwise calls via the renaming would
11974 -- get incorrectly resolved to call the user-defined equality
11975 -- function.
11977 if Is_Predefined_Eq_Renaming (Node (Prim)) then
11978 Set_Alias (Node (Prim), Renamed_Eq);
11980 -- Exit upon encountering a user-defined equality
11982 elsif Chars (Node (Prim)) = Name_Op_Eq
11983 and then No (Alias (Node (Prim)))
11984 then
11985 exit;
11986 end if;
11988 Next_Elmt (Prim);
11989 end loop;
11990 end if;
11991 end if;
11992 end Make_Predefined_Primitive_Eq_Spec;
11994 -------------------------------------
11995 -- Make_Predefined_Primitive_Specs --
11996 -------------------------------------
11998 procedure Make_Predefined_Primitive_Specs
11999 (Tag_Typ : Entity_Id;
12000 Predef_List : out List_Id;
12001 Renamed_Eq : out Entity_Id)
12003 Loc : constant Source_Ptr := Sloc (Tag_Typ);
12004 Res : constant List_Id := New_List;
12006 use Exp_Put_Image;
12008 begin
12009 Renamed_Eq := Empty;
12011 -- Spec of _Size
12013 Append_To (Res, Predef_Spec_Or_Body (Loc,
12014 Tag_Typ => Tag_Typ,
12015 Name => Name_uSize,
12016 Profile => New_List (
12017 Make_Parameter_Specification (Loc,
12018 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
12019 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
12021 Ret_Type => Standard_Long_Long_Integer));
12023 -- Spec of Put_Image
12025 if not No_Run_Time_Mode
12026 and then RTE_Available (RE_Root_Buffer_Type)
12027 then
12028 -- No_Run_Time_Mode implies that the declaration of Tag_Typ
12029 -- (like any tagged type) will be rejected. Given this, avoid
12030 -- cascading errors associated with the Tag_Typ's TSS_Put_Image
12031 -- procedure.
12033 Append_To (Res, Predef_Spec_Or_Body (Loc,
12034 Tag_Typ => Tag_Typ,
12035 Name => Make_TSS_Name (Tag_Typ, TSS_Put_Image),
12036 Profile => Build_Put_Image_Profile (Loc, Tag_Typ)));
12037 end if;
12039 -- Specs for dispatching stream attributes
12041 declare
12042 Stream_Op_TSS_Names :
12043 constant array (Positive range <>) of TSS_Name_Type :=
12044 (TSS_Stream_Read,
12045 TSS_Stream_Write,
12046 TSS_Stream_Input,
12047 TSS_Stream_Output);
12049 begin
12050 for Op in Stream_Op_TSS_Names'Range loop
12051 if Stream_Operation_OK (Tag_Typ, Stream_Op_TSS_Names (Op)) then
12052 Append_To (Res,
12053 Predef_Stream_Attr_Spec (Loc, Tag_Typ,
12054 Stream_Op_TSS_Names (Op)));
12055 end if;
12056 end loop;
12057 end;
12059 -- Spec of "=" is expanded if the type is not limited and if a user
12060 -- defined "=" was not already declared for the non-full view of a
12061 -- private extension.
12063 if not Is_Limited_Type (Tag_Typ) then
12064 Make_Predefined_Primitive_Eq_Spec (Tag_Typ, Res, Renamed_Eq);
12066 -- Spec for dispatching assignment
12068 Append_To (Res, Predef_Spec_Or_Body (Loc,
12069 Tag_Typ => Tag_Typ,
12070 Name => Name_uAssign,
12071 Profile => New_List (
12072 Make_Parameter_Specification (Loc,
12073 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
12074 Out_Present => True,
12075 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
12077 Make_Parameter_Specification (Loc,
12078 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
12079 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)))));
12080 end if;
12082 -- Ada 2005: Generate declarations for the following primitive
12083 -- operations for limited interfaces and synchronized types that
12084 -- implement a limited interface.
12086 -- Disp_Asynchronous_Select
12087 -- Disp_Conditional_Select
12088 -- Disp_Get_Prim_Op_Kind
12089 -- Disp_Get_Task_Id
12090 -- Disp_Requeue
12091 -- Disp_Timed_Select
12093 -- Disable the generation of these bodies if Ravenscar or ZFP is active
12095 if Ada_Version >= Ada_2005
12096 and then not Restriction_Active (No_Select_Statements)
12097 and then RTE_Available (RE_Select_Specific_Data)
12098 then
12099 -- These primitives are defined abstract in interface types
12101 if Is_Interface (Tag_Typ)
12102 and then Is_Limited_Record (Tag_Typ)
12103 then
12104 Append_To (Res,
12105 Make_Abstract_Subprogram_Declaration (Loc,
12106 Specification =>
12107 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
12109 Append_To (Res,
12110 Make_Abstract_Subprogram_Declaration (Loc,
12111 Specification =>
12112 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
12114 Append_To (Res,
12115 Make_Abstract_Subprogram_Declaration (Loc,
12116 Specification =>
12117 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
12119 Append_To (Res,
12120 Make_Abstract_Subprogram_Declaration (Loc,
12121 Specification =>
12122 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
12124 Append_To (Res,
12125 Make_Abstract_Subprogram_Declaration (Loc,
12126 Specification =>
12127 Make_Disp_Requeue_Spec (Tag_Typ)));
12129 Append_To (Res,
12130 Make_Abstract_Subprogram_Declaration (Loc,
12131 Specification =>
12132 Make_Disp_Timed_Select_Spec (Tag_Typ)));
12134 -- If ancestor is an interface type, declare non-abstract primitives
12135 -- to override the abstract primitives of the interface type.
12137 -- In VM targets we define these primitives in all root tagged types
12138 -- that are not interface types. Done because in VM targets we don't
12139 -- have secondary dispatch tables and any derivation of Tag_Typ may
12140 -- cover limited interfaces (which always have these primitives since
12141 -- they may be ancestors of synchronized interface types).
12143 elsif (not Is_Interface (Tag_Typ)
12144 and then Is_Interface (Etype (Tag_Typ))
12145 and then Is_Limited_Record (Etype (Tag_Typ)))
12146 or else
12147 (Is_Concurrent_Record_Type (Tag_Typ)
12148 and then Has_Interfaces (Tag_Typ))
12149 or else
12150 (not Tagged_Type_Expansion
12151 and then not Is_Interface (Tag_Typ)
12152 and then Tag_Typ = Root_Type (Tag_Typ))
12153 then
12154 Append_To (Res,
12155 Make_Subprogram_Declaration (Loc,
12156 Specification =>
12157 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
12159 Append_To (Res,
12160 Make_Subprogram_Declaration (Loc,
12161 Specification =>
12162 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
12164 Append_To (Res,
12165 Make_Subprogram_Declaration (Loc,
12166 Specification =>
12167 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
12169 Append_To (Res,
12170 Make_Subprogram_Declaration (Loc,
12171 Specification =>
12172 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
12174 Append_To (Res,
12175 Make_Subprogram_Declaration (Loc,
12176 Specification =>
12177 Make_Disp_Requeue_Spec (Tag_Typ)));
12179 Append_To (Res,
12180 Make_Subprogram_Declaration (Loc,
12181 Specification =>
12182 Make_Disp_Timed_Select_Spec (Tag_Typ)));
12183 end if;
12184 end if;
12186 -- All tagged types receive their own Deep_Adjust and Deep_Finalize
12187 -- regardless of whether they are controlled or may contain controlled
12188 -- components.
12190 -- Do not generate the routines if finalization is disabled
12192 if Restriction_Active (No_Finalization) then
12193 null;
12195 else
12196 if not Is_Limited_Type (Tag_Typ) then
12197 Append_To (Res, Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust));
12198 end if;
12200 Append_To (Res, Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize));
12201 end if;
12203 Predef_List := Res;
12204 end Make_Predefined_Primitive_Specs;
12206 -------------------------
12207 -- Make_Tag_Assignment --
12208 -------------------------
12210 function Make_Tag_Assignment (N : Node_Id) return Node_Id is
12211 Loc : constant Source_Ptr := Sloc (N);
12212 Def_Id : constant Entity_Id := Defining_Identifier (N);
12213 Expr : constant Node_Id := Expression (N);
12214 Typ : constant Entity_Id := Etype (Def_Id);
12215 Full_Typ : constant Entity_Id := Underlying_Type (Typ);
12217 begin
12218 -- This expansion activity is called during analysis
12220 if Is_Tagged_Type (Typ)
12221 and then not Is_Class_Wide_Type (Typ)
12222 and then not Is_CPP_Class (Typ)
12223 and then Tagged_Type_Expansion
12224 and then Nkind (Unqualify (Expr)) /= N_Aggregate
12225 then
12226 return
12227 Make_Tag_Assignment_From_Type
12228 (Loc, New_Occurrence_Of (Def_Id, Loc), Full_Typ);
12230 else
12231 return Empty;
12232 end if;
12233 end Make_Tag_Assignment;
12235 ----------------------
12236 -- Predef_Deep_Spec --
12237 ----------------------
12239 function Predef_Deep_Spec
12240 (Loc : Source_Ptr;
12241 Tag_Typ : Entity_Id;
12242 Name : TSS_Name_Type;
12243 For_Body : Boolean := False) return Node_Id
12245 Formals : List_Id;
12247 begin
12248 -- V : in out Tag_Typ
12250 Formals := New_List (
12251 Make_Parameter_Specification (Loc,
12252 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
12253 In_Present => True,
12254 Out_Present => True,
12255 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)));
12257 -- F : Boolean := True
12259 if Name = TSS_Deep_Adjust
12260 or else Name = TSS_Deep_Finalize
12261 then
12262 Append_To (Formals,
12263 Make_Parameter_Specification (Loc,
12264 Defining_Identifier => Make_Defining_Identifier (Loc, Name_F),
12265 Parameter_Type => New_Occurrence_Of (Standard_Boolean, Loc),
12266 Expression => New_Occurrence_Of (Standard_True, Loc)));
12267 end if;
12269 return
12270 Predef_Spec_Or_Body (Loc,
12271 Name => Make_TSS_Name (Tag_Typ, Name),
12272 Tag_Typ => Tag_Typ,
12273 Profile => Formals,
12274 For_Body => For_Body);
12276 exception
12277 when RE_Not_Available =>
12278 return Empty;
12279 end Predef_Deep_Spec;
12281 -------------------------
12282 -- Predef_Spec_Or_Body --
12283 -------------------------
12285 function Predef_Spec_Or_Body
12286 (Loc : Source_Ptr;
12287 Tag_Typ : Entity_Id;
12288 Name : Name_Id;
12289 Profile : List_Id;
12290 Ret_Type : Entity_Id := Empty;
12291 For_Body : Boolean := False) return Node_Id
12293 Id : constant Entity_Id := Make_Defining_Identifier (Loc, Name);
12294 Spec : Node_Id;
12296 begin
12297 Set_Is_Public (Id, Is_Public (Tag_Typ));
12299 -- The internal flag is set to mark these declarations because they have
12300 -- specific properties. First, they are primitives even if they are not
12301 -- defined in the type scope (the freezing point is not necessarily in
12302 -- the same scope). Second, the predefined equality can be overridden by
12303 -- a user-defined equality, no body will be generated in this case.
12305 Set_Is_Internal (Id);
12307 if not Debug_Generated_Code then
12308 Set_Debug_Info_Off (Id);
12309 end if;
12311 if No (Ret_Type) then
12312 Spec :=
12313 Make_Procedure_Specification (Loc,
12314 Defining_Unit_Name => Id,
12315 Parameter_Specifications => Profile);
12316 else
12317 Spec :=
12318 Make_Function_Specification (Loc,
12319 Defining_Unit_Name => Id,
12320 Parameter_Specifications => Profile,
12321 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
12322 end if;
12324 -- Declare an abstract subprogram for primitive subprograms of an
12325 -- interface type (except for "=").
12327 if Is_Interface (Tag_Typ) then
12328 if Name /= Name_Op_Eq then
12329 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
12331 -- The equality function (if any) for an interface type is defined
12332 -- to be nonabstract, so we create an expression function for it that
12333 -- always returns False. Note that the function can never actually be
12334 -- invoked because interface types are abstract, so there aren't any
12335 -- objects of such types (and their equality operation will always
12336 -- dispatch).
12338 else
12339 return Make_Expression_Function
12340 (Loc, Spec, New_Occurrence_Of (Standard_False, Loc));
12341 end if;
12343 -- If body case, return empty subprogram body. Note that this is ill-
12344 -- formed, because there is not even a null statement, and certainly not
12345 -- a return in the function case. The caller is expected to do surgery
12346 -- on the body to add the appropriate stuff.
12348 elsif For_Body then
12349 return Make_Subprogram_Body (Loc, Spec, Empty_List, Empty);
12351 -- For the case of an Input attribute predefined for an abstract type,
12352 -- generate an abstract specification. This will never be called, but we
12353 -- need the slot allocated in the dispatching table so that attributes
12354 -- typ'Class'Input and typ'Class'Output will work properly.
12356 elsif Is_TSS (Name, TSS_Stream_Input)
12357 and then Is_Abstract_Type (Tag_Typ)
12358 then
12359 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
12361 -- Normal spec case, where we return a subprogram declaration
12363 else
12364 return Make_Subprogram_Declaration (Loc, Spec);
12365 end if;
12366 end Predef_Spec_Or_Body;
12368 -----------------------------
12369 -- Predef_Stream_Attr_Spec --
12370 -----------------------------
12372 function Predef_Stream_Attr_Spec
12373 (Loc : Source_Ptr;
12374 Tag_Typ : Entity_Id;
12375 Name : TSS_Name_Type) return Node_Id
12377 Ret_Type : Entity_Id;
12379 begin
12380 if Name = TSS_Stream_Input then
12381 Ret_Type := Tag_Typ;
12382 else
12383 Ret_Type := Empty;
12384 end if;
12386 return
12387 Predef_Spec_Or_Body
12388 (Loc,
12389 Name => Make_TSS_Name (Tag_Typ, Name),
12390 Tag_Typ => Tag_Typ,
12391 Profile => Build_Stream_Attr_Profile (Loc, Tag_Typ, Name),
12392 Ret_Type => Ret_Type,
12393 For_Body => False);
12394 end Predef_Stream_Attr_Spec;
12396 ----------------------------------
12397 -- Predefined_Primitive_Eq_Body --
12398 ----------------------------------
12400 procedure Predefined_Primitive_Eq_Body
12401 (Tag_Typ : Entity_Id;
12402 Predef_List : List_Id;
12403 Renamed_Eq : Entity_Id)
12405 Decl : Node_Id;
12406 Eq_Needed : Boolean;
12407 Eq_Name : Name_Id;
12408 Prim : Elmt_Id;
12410 begin
12411 -- See if we have a predefined "=" operator
12413 if Present (Renamed_Eq) then
12414 Eq_Needed := True;
12415 Eq_Name := Chars (Renamed_Eq);
12417 -- If the parent is an interface type then it has defined all the
12418 -- predefined primitives abstract and we need to check if the type
12419 -- has some user defined "=" function which matches the profile of
12420 -- the Ada predefined equality operator to avoid generating it.
12422 elsif Is_Interface (Etype (Tag_Typ)) then
12423 Eq_Needed := True;
12424 Eq_Name := Name_Op_Eq;
12426 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
12427 while Present (Prim) loop
12428 if Is_User_Defined_Equality (Node (Prim))
12429 and then not Is_Internal (Node (Prim))
12430 then
12431 Eq_Needed := False;
12432 Eq_Name := No_Name;
12433 exit;
12434 end if;
12436 Next_Elmt (Prim);
12437 end loop;
12439 else
12440 Eq_Needed := False;
12441 Eq_Name := No_Name;
12443 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
12444 while Present (Prim) loop
12445 if Is_User_Defined_Equality (Node (Prim))
12446 and then Is_Internal (Node (Prim))
12447 then
12448 Eq_Needed := True;
12449 Eq_Name := Name_Op_Eq;
12450 exit;
12451 end if;
12453 Next_Elmt (Prim);
12454 end loop;
12455 end if;
12457 -- If equality is needed, we will have its name
12459 pragma Assert (Eq_Needed = Present (Eq_Name));
12461 -- Body for equality
12463 if Eq_Needed then
12464 Decl := Make_Eq_Body (Tag_Typ, Eq_Name);
12465 Append_To (Predef_List, Decl);
12466 end if;
12468 -- Body for inequality (if required)
12470 Decl := Make_Neq_Body (Tag_Typ);
12472 if Present (Decl) then
12473 Append_To (Predef_List, Decl);
12474 end if;
12475 end Predefined_Primitive_Eq_Body;
12477 ---------------------------------
12478 -- Predefined_Primitive_Bodies --
12479 ---------------------------------
12481 function Predefined_Primitive_Bodies
12482 (Tag_Typ : Entity_Id;
12483 Renamed_Eq : Entity_Id) return List_Id
12485 Loc : constant Source_Ptr := Sloc (Tag_Typ);
12486 Res : constant List_Id := New_List;
12487 Adj_Call : Node_Id;
12488 Decl : Node_Id;
12489 Fin_Call : Node_Id;
12490 Ent : Entity_Id;
12492 pragma Warnings (Off, Ent);
12494 use Exp_Put_Image;
12496 begin
12497 pragma Assert (not Is_Interface (Tag_Typ));
12499 -- Body of _Size
12501 Decl := Predef_Spec_Or_Body (Loc,
12502 Tag_Typ => Tag_Typ,
12503 Name => Name_uSize,
12504 Profile => New_List (
12505 Make_Parameter_Specification (Loc,
12506 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
12507 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
12509 Ret_Type => Standard_Long_Long_Integer,
12510 For_Body => True);
12512 Set_Handled_Statement_Sequence (Decl,
12513 Make_Handled_Sequence_Of_Statements (Loc, New_List (
12514 Make_Simple_Return_Statement (Loc,
12515 Expression =>
12516 Make_Attribute_Reference (Loc,
12517 Prefix => Make_Identifier (Loc, Name_X),
12518 Attribute_Name => Name_Size)))));
12520 Append_To (Res, Decl);
12522 -- Body of Put_Image
12524 if No (TSS (Tag_Typ, TSS_Put_Image))
12525 and then not No_Run_Time_Mode
12526 and then RTE_Available (RE_Root_Buffer_Type)
12527 then
12528 Build_Record_Put_Image_Procedure (Loc, Tag_Typ, Decl, Ent);
12529 Append_To (Res, Decl);
12530 end if;
12532 -- Bodies for Dispatching stream IO routines. We need these only for
12533 -- non-limited types (in the limited case there is no dispatching).
12534 -- We also skip them if dispatching or finalization are not available
12535 -- or if stream operations are prohibited by restriction No_Streams or
12536 -- from use of pragma/aspect No_Tagged_Streams.
12538 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Read)
12539 and then No (TSS (Tag_Typ, TSS_Stream_Read))
12540 then
12541 Build_Record_Read_Procedure (Tag_Typ, Decl, Ent);
12542 Append_To (Res, Decl);
12543 end if;
12545 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Write)
12546 and then No (TSS (Tag_Typ, TSS_Stream_Write))
12547 then
12548 Build_Record_Write_Procedure (Tag_Typ, Decl, Ent);
12549 Append_To (Res, Decl);
12550 end if;
12552 -- Skip body of _Input for the abstract case, since the corresponding
12553 -- spec is abstract (see Predef_Spec_Or_Body).
12555 if not Is_Abstract_Type (Tag_Typ)
12556 and then Stream_Operation_OK (Tag_Typ, TSS_Stream_Input)
12557 and then No (TSS (Tag_Typ, TSS_Stream_Input))
12558 then
12559 Build_Record_Or_Elementary_Input_Function
12560 (Tag_Typ, Decl, Ent);
12561 Append_To (Res, Decl);
12562 end if;
12564 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Output)
12565 and then No (TSS (Tag_Typ, TSS_Stream_Output))
12566 then
12567 Build_Record_Or_Elementary_Output_Procedure (Tag_Typ, Decl, Ent);
12568 Append_To (Res, Decl);
12569 end if;
12571 -- Ada 2005: Generate bodies for the following primitive operations for
12572 -- limited interfaces and synchronized types that implement a limited
12573 -- interface.
12575 -- disp_asynchronous_select
12576 -- disp_conditional_select
12577 -- disp_get_prim_op_kind
12578 -- disp_get_task_id
12579 -- disp_timed_select
12581 -- The interface versions will have null bodies
12583 -- Disable the generation of these bodies if Ravenscar or ZFP is active
12585 -- In VM targets we define these primitives in all root tagged types
12586 -- that are not interface types. Done because in VM targets we don't
12587 -- have secondary dispatch tables and any derivation of Tag_Typ may
12588 -- cover limited interfaces (which always have these primitives since
12589 -- they may be ancestors of synchronized interface types).
12591 if Ada_Version >= Ada_2005
12592 and then
12593 ((Is_Interface (Etype (Tag_Typ))
12594 and then Is_Limited_Record (Etype (Tag_Typ)))
12595 or else
12596 (Is_Concurrent_Record_Type (Tag_Typ)
12597 and then Has_Interfaces (Tag_Typ))
12598 or else
12599 (not Tagged_Type_Expansion
12600 and then Tag_Typ = Root_Type (Tag_Typ)))
12601 and then not Restriction_Active (No_Select_Statements)
12602 and then RTE_Available (RE_Select_Specific_Data)
12603 then
12604 Append_To (Res, Make_Disp_Asynchronous_Select_Body (Tag_Typ));
12605 Append_To (Res, Make_Disp_Conditional_Select_Body (Tag_Typ));
12606 Append_To (Res, Make_Disp_Get_Prim_Op_Kind_Body (Tag_Typ));
12607 Append_To (Res, Make_Disp_Get_Task_Id_Body (Tag_Typ));
12608 Append_To (Res, Make_Disp_Requeue_Body (Tag_Typ));
12609 Append_To (Res, Make_Disp_Timed_Select_Body (Tag_Typ));
12610 end if;
12612 if not Is_Limited_Type (Tag_Typ) then
12613 -- Body for equality and inequality
12615 Predefined_Primitive_Eq_Body (Tag_Typ, Res, Renamed_Eq);
12617 -- Body for dispatching assignment
12619 Decl :=
12620 Predef_Spec_Or_Body (Loc,
12621 Tag_Typ => Tag_Typ,
12622 Name => Name_uAssign,
12623 Profile => New_List (
12624 Make_Parameter_Specification (Loc,
12625 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
12626 Out_Present => True,
12627 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
12629 Make_Parameter_Specification (Loc,
12630 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
12631 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
12632 For_Body => True);
12634 Set_Handled_Statement_Sequence (Decl,
12635 Make_Handled_Sequence_Of_Statements (Loc, New_List (
12636 Make_Assignment_Statement (Loc,
12637 Name => Make_Identifier (Loc, Name_X),
12638 Expression => Make_Identifier (Loc, Name_Y)))));
12640 Append_To (Res, Decl);
12641 end if;
12643 -- Generate empty bodies of routines Deep_Adjust and Deep_Finalize for
12644 -- tagged types which do not contain controlled components.
12646 -- Do not generate the routines if finalization is disabled
12648 if Restriction_Active (No_Finalization) then
12649 null;
12651 elsif not Has_Controlled_Component (Tag_Typ) then
12652 if not Is_Limited_Type (Tag_Typ) then
12653 Adj_Call := Empty;
12654 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust, True);
12656 if Is_Controlled (Tag_Typ) then
12657 Adj_Call :=
12658 Make_Adjust_Call (
12659 Obj_Ref => Make_Identifier (Loc, Name_V),
12660 Typ => Tag_Typ);
12661 end if;
12663 if No (Adj_Call) then
12664 Adj_Call := Make_Null_Statement (Loc);
12665 end if;
12667 Set_Handled_Statement_Sequence (Decl,
12668 Make_Handled_Sequence_Of_Statements (Loc,
12669 Statements => New_List (Adj_Call)));
12671 Append_To (Res, Decl);
12672 end if;
12674 Fin_Call := Empty;
12675 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize, True);
12677 if Is_Controlled (Tag_Typ) then
12678 Fin_Call :=
12679 Make_Final_Call
12680 (Obj_Ref => Make_Identifier (Loc, Name_V),
12681 Typ => Tag_Typ);
12682 end if;
12684 if No (Fin_Call) then
12685 Fin_Call := Make_Null_Statement (Loc);
12686 end if;
12688 Set_Handled_Statement_Sequence (Decl,
12689 Make_Handled_Sequence_Of_Statements (Loc,
12690 Statements => New_List (Fin_Call)));
12692 Append_To (Res, Decl);
12693 end if;
12695 return Res;
12696 end Predefined_Primitive_Bodies;
12698 ---------------------------------
12699 -- Predefined_Primitive_Freeze --
12700 ---------------------------------
12702 function Predefined_Primitive_Freeze
12703 (Tag_Typ : Entity_Id) return List_Id
12705 Res : constant List_Id := New_List;
12706 Prim : Elmt_Id;
12707 Frnodes : List_Id;
12709 begin
12710 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
12711 while Present (Prim) loop
12712 if Is_Predefined_Dispatching_Operation (Node (Prim)) then
12713 Frnodes := Freeze_Entity (Node (Prim), Tag_Typ);
12715 if Present (Frnodes) then
12716 Append_List_To (Res, Frnodes);
12717 end if;
12718 end if;
12720 Next_Elmt (Prim);
12721 end loop;
12723 return Res;
12724 end Predefined_Primitive_Freeze;
12726 -------------------------
12727 -- Stream_Operation_OK --
12728 -------------------------
12730 function Stream_Operation_OK
12731 (Typ : Entity_Id;
12732 Operation : TSS_Name_Type) return Boolean
12734 Has_Predefined_Or_Specified_Stream_Attribute : Boolean := False;
12736 begin
12737 -- Special case of a limited type extension: a default implementation
12738 -- of the stream attributes Read or Write exists if that attribute
12739 -- has been specified or is available for an ancestor type; a default
12740 -- implementation of the attribute Output (resp. Input) exists if the
12741 -- attribute has been specified or Write (resp. Read) is available for
12742 -- an ancestor type. The last condition only applies under Ada 2005.
12744 if Is_Limited_Type (Typ) and then Is_Tagged_Type (Typ) then
12745 if Operation = TSS_Stream_Read then
12746 Has_Predefined_Or_Specified_Stream_Attribute :=
12747 Has_Specified_Stream_Read (Typ);
12749 elsif Operation = TSS_Stream_Write then
12750 Has_Predefined_Or_Specified_Stream_Attribute :=
12751 Has_Specified_Stream_Write (Typ);
12753 elsif Operation = TSS_Stream_Input then
12754 Has_Predefined_Or_Specified_Stream_Attribute :=
12755 Has_Specified_Stream_Input (Typ)
12756 or else
12757 (Ada_Version >= Ada_2005
12758 and then Stream_Operation_OK (Typ, TSS_Stream_Read));
12760 elsif Operation = TSS_Stream_Output then
12761 Has_Predefined_Or_Specified_Stream_Attribute :=
12762 Has_Specified_Stream_Output (Typ)
12763 or else
12764 (Ada_Version >= Ada_2005
12765 and then Stream_Operation_OK (Typ, TSS_Stream_Write));
12766 end if;
12768 -- Case of inherited TSS_Stream_Read or TSS_Stream_Write
12770 if not Has_Predefined_Or_Specified_Stream_Attribute
12771 and then Is_Derived_Type (Typ)
12772 and then (Operation = TSS_Stream_Read
12773 or else Operation = TSS_Stream_Write)
12774 then
12775 Has_Predefined_Or_Specified_Stream_Attribute :=
12776 Present
12777 (Find_Inherited_TSS (Base_Type (Etype (Typ)), Operation));
12778 end if;
12779 end if;
12781 -- If the type is not limited, or else is limited but the attribute is
12782 -- explicitly specified or is predefined for the type, then return True,
12783 -- unless other conditions prevail, such as restrictions prohibiting
12784 -- streams or dispatching operations. We also return True for limited
12785 -- interfaces, because they may be extended by nonlimited types and
12786 -- permit inheritance in this case (addresses cases where an abstract
12787 -- extension doesn't get 'Input declared, as per comments below, but
12788 -- 'Class'Input must still be allowed). Note that attempts to apply
12789 -- stream attributes to a limited interface or its class-wide type
12790 -- (or limited extensions thereof) will still get properly rejected
12791 -- by Check_Stream_Attribute.
12793 -- We exclude the Input operation from being a predefined subprogram in
12794 -- the case where the associated type is an abstract extension, because
12795 -- the attribute is not callable in that case, per 13.13.2(49/2). Also,
12796 -- we don't want an abstract version created because types derived from
12797 -- the abstract type may not even have Input available (for example if
12798 -- derived from a private view of the abstract type that doesn't have
12799 -- a visible Input).
12801 -- Do not generate stream routines for type Finalization_Master because
12802 -- a master may never appear in types and therefore cannot be read or
12803 -- written.
12805 return
12806 (not Is_Limited_Type (Typ)
12807 or else Is_Interface (Typ)
12808 or else Has_Predefined_Or_Specified_Stream_Attribute)
12809 and then
12810 (Operation /= TSS_Stream_Input
12811 or else not Is_Abstract_Type (Typ)
12812 or else not Is_Derived_Type (Typ))
12813 and then not Has_Unknown_Discriminants (Typ)
12814 and then not Is_Concurrent_Interface (Typ)
12815 and then not Restriction_Active (No_Streams)
12816 and then not Restriction_Active (No_Dispatch)
12817 and then No (No_Tagged_Streams_Pragma (Typ))
12818 and then not No_Run_Time_Mode
12819 and then RTE_Available (RE_Tag)
12820 and then No (Type_Without_Stream_Operation (Typ))
12821 and then RTE_Available (RE_Root_Stream_Type)
12822 and then not Is_RTE (Typ, RE_Finalization_Master);
12823 end Stream_Operation_OK;
12825 end Exp_Ch3;