PR27116, Spelling errors found by Debian style checker
[official-gcc.git] / gcc / ada / exp_ch3.adb
blob04c3ad8c6317fd4cb7fbcb67e58e668c9d772166
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 elsif Is_Concurrent_Type (Root)
5005 or else Is_C_Derivation (Root)
5006 or else Convention (Typ) = Convention_CPP
5007 then
5008 return;
5010 -- Do not create TSS routine Finalize_Address when compiling in CodePeer
5011 -- mode since the routine contains an Unchecked_Conversion.
5013 elsif CodePeer_Mode then
5014 return;
5015 end if;
5017 -- Create the body of TSS primitive Finalize_Address. This automatically
5018 -- sets the TSS entry for the class-wide type.
5020 Make_Finalize_Address_Body (Typ);
5021 end Expand_Freeze_Class_Wide_Type;
5023 ------------------------------------
5024 -- Expand_Freeze_Enumeration_Type --
5025 ------------------------------------
5027 procedure Expand_Freeze_Enumeration_Type (N : Node_Id) is
5028 Typ : constant Entity_Id := Entity (N);
5029 Loc : constant Source_Ptr := Sloc (Typ);
5031 Arr : Entity_Id;
5032 Ent : Entity_Id;
5033 Fent : Entity_Id;
5034 Is_Contiguous : Boolean;
5035 Index_Typ : Entity_Id;
5036 Ityp : Entity_Id;
5037 Last_Repval : Uint;
5038 Lst : List_Id;
5039 Num : Nat;
5040 Pos_Expr : Node_Id;
5042 Func : Entity_Id;
5043 pragma Warnings (Off, Func);
5045 begin
5046 -- Various optimizations possible if given representation is contiguous
5048 Is_Contiguous := True;
5050 Ent := First_Literal (Typ);
5051 Last_Repval := Enumeration_Rep (Ent);
5052 Num := 1;
5053 Next_Literal (Ent);
5055 while Present (Ent) loop
5056 if Enumeration_Rep (Ent) - Last_Repval /= 1 then
5057 Is_Contiguous := False;
5058 else
5059 Last_Repval := Enumeration_Rep (Ent);
5060 end if;
5062 Num := Num + 1;
5063 Next_Literal (Ent);
5064 end loop;
5066 if Is_Contiguous then
5067 Set_Has_Contiguous_Rep (Typ);
5069 -- Now build a subtype declaration
5071 -- subtype typI is new Natural range 0 .. num - 1
5073 Index_Typ :=
5074 Make_Defining_Identifier (Loc,
5075 Chars => New_External_Name (Chars (Typ), 'I'));
5077 Append_Freeze_Action (Typ,
5078 Make_Subtype_Declaration (Loc,
5079 Defining_Identifier => Index_Typ,
5080 Subtype_Indication =>
5081 Make_Subtype_Indication (Loc,
5082 Subtype_Mark =>
5083 New_Occurrence_Of (Standard_Natural, Loc),
5084 Constraint =>
5085 Make_Range_Constraint (Loc,
5086 Range_Expression =>
5087 Make_Range (Loc,
5088 Low_Bound =>
5089 Make_Integer_Literal (Loc, 0),
5090 High_Bound =>
5091 Make_Integer_Literal (Loc, Num - 1))))));
5093 Set_Enum_Pos_To_Rep (Typ, Index_Typ);
5095 else
5096 -- Build list of literal references
5098 Lst := New_List;
5099 Ent := First_Literal (Typ);
5100 while Present (Ent) loop
5101 Append_To (Lst, New_Occurrence_Of (Ent, Sloc (Ent)));
5102 Next_Literal (Ent);
5103 end loop;
5105 -- Now build an array declaration
5107 -- typA : constant array (Natural range 0 .. num - 1) of typ :=
5108 -- (v, v, v, v, v, ....)
5110 Arr :=
5111 Make_Defining_Identifier (Loc,
5112 Chars => New_External_Name (Chars (Typ), 'A'));
5114 Append_Freeze_Action (Typ,
5115 Make_Object_Declaration (Loc,
5116 Defining_Identifier => Arr,
5117 Constant_Present => True,
5119 Object_Definition =>
5120 Make_Constrained_Array_Definition (Loc,
5121 Discrete_Subtype_Definitions => New_List (
5122 Make_Subtype_Indication (Loc,
5123 Subtype_Mark =>
5124 New_Occurrence_Of (Standard_Natural, Loc),
5125 Constraint =>
5126 Make_Range_Constraint (Loc,
5127 Range_Expression =>
5128 Make_Range (Loc,
5129 Low_Bound =>
5130 Make_Integer_Literal (Loc, 0),
5131 High_Bound =>
5132 Make_Integer_Literal (Loc, Num - 1))))),
5134 Component_Definition =>
5135 Make_Component_Definition (Loc,
5136 Aliased_Present => False,
5137 Subtype_Indication => New_Occurrence_Of (Typ, Loc))),
5139 Expression =>
5140 Make_Aggregate (Loc,
5141 Expressions => Lst)));
5143 Set_Enum_Pos_To_Rep (Typ, Arr);
5144 end if;
5146 -- Now we build the function that converts representation values to
5147 -- position values. This function has the form:
5149 -- function _Rep_To_Pos (A : etype; F : Boolean) return Integer is
5150 -- begin
5151 -- case ityp!(A) is
5152 -- when enum-lit'Enum_Rep => return posval;
5153 -- when enum-lit'Enum_Rep => return posval;
5154 -- ...
5155 -- when others =>
5156 -- [raise Constraint_Error when F "invalid data"]
5157 -- return -1;
5158 -- end case;
5159 -- end;
5161 -- Note: the F parameter determines whether the others case (no valid
5162 -- representation) raises Constraint_Error or returns a unique value
5163 -- of minus one. The latter case is used, e.g. in 'Valid code.
5165 -- Note: the reason we use Enum_Rep values in the case here is to avoid
5166 -- the code generator making inappropriate assumptions about the range
5167 -- of the values in the case where the value is invalid. ityp is a
5168 -- signed or unsigned integer type of appropriate width.
5170 -- Note: if exceptions are not supported, then we suppress the raise
5171 -- and return -1 unconditionally (this is an erroneous program in any
5172 -- case and there is no obligation to raise Constraint_Error here). We
5173 -- also do this if pragma Restrictions (No_Exceptions) is active.
5175 -- Is this right??? What about No_Exception_Propagation???
5177 -- The underlying type is signed. Reset the Is_Unsigned_Type explicitly
5178 -- because it might have been inherited from the parent type.
5180 if Enumeration_Rep (First_Literal (Typ)) < 0 then
5181 Set_Is_Unsigned_Type (Typ, False);
5182 end if;
5184 Ityp := Integer_Type_For (Esize (Typ), Is_Unsigned_Type (Typ));
5186 -- The body of the function is a case statement. First collect case
5187 -- alternatives, or optimize the contiguous case.
5189 Lst := New_List;
5191 -- If representation is contiguous, Pos is computed by subtracting
5192 -- the representation of the first literal.
5194 if Is_Contiguous then
5195 Ent := First_Literal (Typ);
5197 if Enumeration_Rep (Ent) = Last_Repval then
5199 -- Another special case: for a single literal, Pos is zero
5201 Pos_Expr := Make_Integer_Literal (Loc, Uint_0);
5203 else
5204 Pos_Expr :=
5205 Convert_To (Standard_Integer,
5206 Make_Op_Subtract (Loc,
5207 Left_Opnd =>
5208 Unchecked_Convert_To
5209 (Ityp, Make_Identifier (Loc, Name_uA)),
5210 Right_Opnd =>
5211 Make_Integer_Literal (Loc,
5212 Intval => Enumeration_Rep (First_Literal (Typ)))));
5213 end if;
5215 Append_To (Lst,
5216 Make_Case_Statement_Alternative (Loc,
5217 Discrete_Choices => New_List (
5218 Make_Range (Sloc (Enumeration_Rep_Expr (Ent)),
5219 Low_Bound =>
5220 Make_Integer_Literal (Loc,
5221 Intval => Enumeration_Rep (Ent)),
5222 High_Bound =>
5223 Make_Integer_Literal (Loc, Intval => Last_Repval))),
5225 Statements => New_List (
5226 Make_Simple_Return_Statement (Loc,
5227 Expression => Pos_Expr))));
5229 else
5230 Ent := First_Literal (Typ);
5231 while Present (Ent) loop
5232 Append_To (Lst,
5233 Make_Case_Statement_Alternative (Loc,
5234 Discrete_Choices => New_List (
5235 Make_Integer_Literal (Sloc (Enumeration_Rep_Expr (Ent)),
5236 Intval => Enumeration_Rep (Ent))),
5238 Statements => New_List (
5239 Make_Simple_Return_Statement (Loc,
5240 Expression =>
5241 Make_Integer_Literal (Loc,
5242 Intval => Enumeration_Pos (Ent))))));
5244 Next_Literal (Ent);
5245 end loop;
5246 end if;
5248 -- In normal mode, add the others clause with the test.
5249 -- If Predicates_Ignored is True, validity checks do not apply to
5250 -- the subtype.
5252 if not No_Exception_Handlers_Set
5253 and then not Predicates_Ignored (Typ)
5254 then
5255 Append_To (Lst,
5256 Make_Case_Statement_Alternative (Loc,
5257 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
5258 Statements => New_List (
5259 Make_Raise_Constraint_Error (Loc,
5260 Condition => Make_Identifier (Loc, Name_uF),
5261 Reason => CE_Invalid_Data),
5262 Make_Simple_Return_Statement (Loc,
5263 Expression => Make_Integer_Literal (Loc, -1)))));
5265 -- If either of the restrictions No_Exceptions_Handlers/Propagation is
5266 -- active then return -1 (we cannot usefully raise Constraint_Error in
5267 -- this case). See description above for further details.
5269 else
5270 Append_To (Lst,
5271 Make_Case_Statement_Alternative (Loc,
5272 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
5273 Statements => New_List (
5274 Make_Simple_Return_Statement (Loc,
5275 Expression => Make_Integer_Literal (Loc, -1)))));
5276 end if;
5278 -- Now we can build the function body
5280 Fent :=
5281 Make_Defining_Identifier (Loc, Make_TSS_Name (Typ, TSS_Rep_To_Pos));
5283 Func :=
5284 Make_Subprogram_Body (Loc,
5285 Specification =>
5286 Make_Function_Specification (Loc,
5287 Defining_Unit_Name => Fent,
5288 Parameter_Specifications => New_List (
5289 Make_Parameter_Specification (Loc,
5290 Defining_Identifier =>
5291 Make_Defining_Identifier (Loc, Name_uA),
5292 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
5293 Make_Parameter_Specification (Loc,
5294 Defining_Identifier =>
5295 Make_Defining_Identifier (Loc, Name_uF),
5296 Parameter_Type =>
5297 New_Occurrence_Of (Standard_Boolean, Loc))),
5299 Result_Definition => New_Occurrence_Of (Standard_Integer, Loc)),
5301 Declarations => Empty_List,
5303 Handled_Statement_Sequence =>
5304 Make_Handled_Sequence_Of_Statements (Loc,
5305 Statements => New_List (
5306 Make_Case_Statement (Loc,
5307 Expression =>
5308 Unchecked_Convert_To
5309 (Ityp, Make_Identifier (Loc, Name_uA)),
5310 Alternatives => Lst))));
5312 Set_TSS (Typ, Fent);
5314 -- Set Pure flag (it will be reset if the current context is not Pure).
5315 -- We also pretend there was a pragma Pure_Function so that for purposes
5316 -- of optimization and constant-folding, we will consider the function
5317 -- Pure even if we are not in a Pure context).
5319 Set_Is_Pure (Fent);
5320 Set_Has_Pragma_Pure_Function (Fent);
5322 -- Unless we are in -gnatD mode, where we are debugging generated code,
5323 -- this is an internal entity for which we don't need debug info.
5325 if not Debug_Generated_Code then
5326 Set_Debug_Info_Off (Fent);
5327 end if;
5329 Set_Is_Inlined (Fent);
5331 exception
5332 when RE_Not_Available =>
5333 return;
5334 end Expand_Freeze_Enumeration_Type;
5336 -------------------------------
5337 -- Expand_Freeze_Record_Type --
5338 -------------------------------
5340 procedure Expand_Freeze_Record_Type (N : Node_Id) is
5342 procedure Build_Class_Condition_Subprograms (Typ : Entity_Id);
5343 -- Create internal subprograms of Typ primitives that have class-wide
5344 -- preconditions or postconditions; they are invoked by the caller to
5345 -- evaluate the conditions.
5347 procedure Build_Variant_Record_Equality (Typ : Entity_Id);
5348 -- Create an equality function for the untagged variant record Typ and
5349 -- attach it to the TSS list.
5351 procedure Register_Dispatch_Table_Wrappers (Typ : Entity_Id);
5352 -- Register dispatch-table wrappers in the dispatch table of Typ
5354 procedure Validate_Tagged_Type_Extra_Formals (Typ : Entity_Id);
5355 -- Check extra formals of dispatching primitives of tagged type Typ.
5356 -- Used in pragma Debug.
5358 ---------------------------------------
5359 -- Build_Class_Condition_Subprograms --
5360 ---------------------------------------
5362 procedure Build_Class_Condition_Subprograms (Typ : Entity_Id) is
5363 Prim_List : constant Elist_Id := Primitive_Operations (Typ);
5364 Prim_Elmt : Elmt_Id := First_Elmt (Prim_List);
5365 Prim : Entity_Id;
5367 begin
5368 while Present (Prim_Elmt) loop
5369 Prim := Node (Prim_Elmt);
5371 -- Primitive with class-wide preconditions
5373 if Comes_From_Source (Prim)
5374 and then Has_Significant_Contract (Prim)
5375 and then
5376 (Present (Class_Preconditions (Prim))
5377 or else Present (Ignored_Class_Preconditions (Prim)))
5378 then
5379 if Expander_Active then
5380 Make_Class_Precondition_Subps (Prim);
5381 end if;
5383 -- Wrapper of a primitive that has or inherits class-wide
5384 -- preconditions.
5386 elsif Is_Primitive_Wrapper (Prim)
5387 and then
5388 (Present (Nearest_Class_Condition_Subprogram
5389 (Spec_Id => Prim,
5390 Kind => Class_Precondition))
5391 or else
5392 Present (Nearest_Class_Condition_Subprogram
5393 (Spec_Id => Prim,
5394 Kind => Ignored_Class_Precondition)))
5395 then
5396 if Expander_Active then
5397 Make_Class_Precondition_Subps (Prim);
5398 end if;
5399 end if;
5401 Next_Elmt (Prim_Elmt);
5402 end loop;
5403 end Build_Class_Condition_Subprograms;
5405 -----------------------------------
5406 -- Build_Variant_Record_Equality --
5407 -----------------------------------
5409 procedure Build_Variant_Record_Equality (Typ : Entity_Id) is
5410 Loc : constant Source_Ptr := Sloc (Typ);
5411 F : constant Entity_Id :=
5412 Make_Defining_Identifier (Loc,
5413 Chars => Make_TSS_Name (Typ, TSS_Composite_Equality));
5414 begin
5415 -- For a variant record with restriction No_Implicit_Conditionals
5416 -- in effect we skip building the procedure. This is safe because
5417 -- if we can see the restriction, so can any caller, and calls to
5418 -- equality test routines are not allowed for variant records if
5419 -- this restriction is active.
5421 if Restriction_Active (No_Implicit_Conditionals) then
5422 return;
5423 end if;
5425 -- Derived Unchecked_Union types no longer inherit the equality
5426 -- function of their parent.
5428 if Is_Derived_Type (Typ)
5429 and then not Is_Unchecked_Union (Typ)
5430 and then not Has_New_Non_Standard_Rep (Typ)
5431 then
5432 declare
5433 Parent_Eq : constant Entity_Id :=
5434 TSS (Root_Type (Typ), TSS_Composite_Equality);
5435 begin
5436 if Present (Parent_Eq) then
5437 Copy_TSS (Parent_Eq, Typ);
5438 return;
5439 end if;
5440 end;
5441 end if;
5443 Discard_Node (
5444 Build_Variant_Record_Equality
5445 (Typ => Typ,
5446 Spec_Id => Empty,
5447 Body_Id => F,
5448 Param_Specs => New_List (
5449 Make_Parameter_Specification (Loc,
5450 Defining_Identifier =>
5451 Make_Defining_Identifier (Loc, Name_X),
5452 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
5454 Make_Parameter_Specification (Loc,
5455 Defining_Identifier =>
5456 Make_Defining_Identifier (Loc, Name_Y),
5457 Parameter_Type => New_Occurrence_Of (Typ, Loc)))));
5459 Set_TSS (Typ, F);
5460 Set_Is_Pure (F);
5462 if not Debug_Generated_Code then
5463 Set_Debug_Info_Off (F);
5464 end if;
5465 end Build_Variant_Record_Equality;
5467 --------------------------------------
5468 -- Register_Dispatch_Table_Wrappers --
5469 --------------------------------------
5471 procedure Register_Dispatch_Table_Wrappers (Typ : Entity_Id) is
5472 Elmt : Elmt_Id := First_Elmt (Primitive_Operations (Typ));
5473 Subp : Entity_Id;
5475 begin
5476 while Present (Elmt) loop
5477 Subp := Node (Elmt);
5479 if Is_Dispatch_Table_Wrapper (Subp) then
5480 Append_Freeze_Actions (Typ,
5481 Register_Primitive (Sloc (Subp), Subp));
5482 end if;
5484 Next_Elmt (Elmt);
5485 end loop;
5486 end Register_Dispatch_Table_Wrappers;
5488 ----------------------------------------
5489 -- Validate_Tagged_Type_Extra_Formals --
5490 ----------------------------------------
5492 procedure Validate_Tagged_Type_Extra_Formals (Typ : Entity_Id) is
5493 Ovr_Subp : Entity_Id;
5494 Elmt : Elmt_Id;
5495 Subp : Entity_Id;
5497 begin
5498 pragma Assert (not Is_Class_Wide_Type (Typ));
5500 -- No check required if expansion is not active since we never
5501 -- generate extra formals in such case.
5503 if not Expander_Active then
5504 return;
5505 end if;
5507 Elmt := First_Elmt (Primitive_Operations (Typ));
5508 while Present (Elmt) loop
5509 Subp := Node (Elmt);
5511 -- Extra formals of a dispatching primitive must match:
5513 -- 1) The extra formals of its covered interface primitive
5515 if Present (Interface_Alias (Subp)) then
5516 pragma Assert
5517 (Extra_Formals_Match_OK
5518 (E => Interface_Alias (Subp),
5519 Ref_E => Alias (Subp)));
5520 end if;
5522 -- 2) The extra formals of its renamed primitive
5524 if Present (Alias (Subp)) then
5525 pragma Assert
5526 (Extra_Formals_Match_OK
5527 (E => Subp,
5528 Ref_E => Ultimate_Alias (Subp)));
5529 end if;
5531 -- 3) The extra formals of its overridden primitive
5533 if Present (Overridden_Operation (Subp)) then
5534 Ovr_Subp := Overridden_Operation (Subp);
5536 -- Handle controlling function wrapper
5538 if Is_Wrapper (Subp)
5539 and then Ultimate_Alias (Ovr_Subp) = Subp
5540 then
5541 if Present (Overridden_Operation (Ovr_Subp)) then
5542 pragma Assert
5543 (Extra_Formals_Match_OK
5544 (E => Subp,
5545 Ref_E => Overridden_Operation (Ovr_Subp)));
5546 end if;
5548 else
5549 pragma Assert
5550 (Extra_Formals_Match_OK
5551 (E => Subp,
5552 Ref_E => Ovr_Subp));
5553 end if;
5554 end if;
5556 Next_Elmt (Elmt);
5557 end loop;
5558 end Validate_Tagged_Type_Extra_Formals;
5560 -- Local variables
5562 Typ : constant Node_Id := Entity (N);
5563 Typ_Decl : constant Node_Id := Parent (Typ);
5565 Comp : Entity_Id;
5566 Comp_Typ : Entity_Id;
5567 Predef_List : List_Id;
5569 Wrapper_Decl_List : List_Id;
5570 Wrapper_Body_List : List_Id := No_List;
5572 Renamed_Eq : Node_Id := Empty;
5573 -- Defining unit name for the predefined equality function in the case
5574 -- where the type has a primitive operation that is a renaming of
5575 -- predefined equality (but only if there is also an overriding
5576 -- user-defined equality function). Used to pass this entity from
5577 -- Make_Predefined_Primitive_Specs to Predefined_Primitive_Bodies.
5579 -- Start of processing for Expand_Freeze_Record_Type
5581 begin
5582 -- Build discriminant checking functions if not a derived type (for
5583 -- derived types that are not tagged types, always use the discriminant
5584 -- checking functions of the parent type). However, for untagged types
5585 -- the derivation may have taken place before the parent was frozen, so
5586 -- we copy explicitly the discriminant checking functions from the
5587 -- parent into the components of the derived type.
5589 Build_Or_Copy_Discr_Checking_Funcs (Typ_Decl);
5591 if Is_Derived_Type (Typ)
5592 and then Is_Limited_Type (Typ)
5593 and then Is_Tagged_Type (Typ)
5594 then
5595 Check_Stream_Attributes (Typ);
5596 end if;
5598 -- Update task, protected, and controlled component flags, because some
5599 -- of the component types may have been private at the point of the
5600 -- record declaration. Detect anonymous access-to-controlled components.
5602 Comp := First_Component (Typ);
5603 while Present (Comp) loop
5604 Comp_Typ := Etype (Comp);
5606 Propagate_Concurrent_Flags (Typ, Comp_Typ);
5608 -- Do not set Has_Controlled_Component on a class-wide equivalent
5609 -- type. See Make_CW_Equivalent_Type.
5611 if not Is_Class_Wide_Equivalent_Type (Typ)
5612 and then
5613 (Has_Controlled_Component (Comp_Typ)
5614 or else (Chars (Comp) /= Name_uParent
5615 and then Is_Controlled (Comp_Typ)))
5616 then
5617 Set_Has_Controlled_Component (Typ);
5618 end if;
5620 Next_Component (Comp);
5621 end loop;
5623 -- Handle constructors of untagged CPP_Class types
5625 if not Is_Tagged_Type (Typ) and then Is_CPP_Class (Typ) then
5626 Set_CPP_Constructors (Typ);
5627 end if;
5629 -- Creation of the Dispatch Table. Note that a Dispatch Table is built
5630 -- for regular tagged types as well as for Ada types deriving from a C++
5631 -- Class, but not for tagged types directly corresponding to C++ classes
5632 -- In the later case we assume that it is created in the C++ side and we
5633 -- just use it.
5635 if Is_Tagged_Type (Typ) then
5637 -- Add the _Tag component
5639 if Underlying_Type (Etype (Typ)) = Typ then
5640 Expand_Tagged_Root (Typ);
5641 end if;
5643 if Is_CPP_Class (Typ) then
5644 Set_All_DT_Position (Typ);
5646 -- Create the tag entities with a minimum decoration
5648 if Tagged_Type_Expansion then
5649 Append_Freeze_Actions (Typ, Make_Tags (Typ));
5650 end if;
5652 Set_CPP_Constructors (Typ);
5654 else
5655 if not Building_Static_DT (Typ) then
5657 -- Usually inherited primitives are not delayed but the first
5658 -- Ada extension of a CPP_Class is an exception since the
5659 -- address of the inherited subprogram has to be inserted in
5660 -- the new Ada Dispatch Table and this is a freezing action.
5662 -- Similarly, if this is an inherited operation whose parent is
5663 -- not frozen yet, it is not in the DT of the parent, and we
5664 -- generate an explicit freeze node for the inherited operation
5665 -- so it is properly inserted in the DT of the current type.
5667 declare
5668 Elmt : Elmt_Id;
5669 Subp : Entity_Id;
5671 begin
5672 Elmt := First_Elmt (Primitive_Operations (Typ));
5673 while Present (Elmt) loop
5674 Subp := Node (Elmt);
5676 if Present (Alias (Subp)) then
5677 if Is_CPP_Class (Etype (Typ)) then
5678 Set_Has_Delayed_Freeze (Subp);
5680 elsif Has_Delayed_Freeze (Alias (Subp))
5681 and then not Is_Frozen (Alias (Subp))
5682 then
5683 Set_Is_Frozen (Subp, False);
5684 Set_Has_Delayed_Freeze (Subp);
5685 end if;
5686 end if;
5688 Next_Elmt (Elmt);
5689 end loop;
5690 end;
5691 end if;
5693 -- Unfreeze momentarily the type to add the predefined primitives
5694 -- operations. The reason we unfreeze is so that these predefined
5695 -- operations will indeed end up as primitive operations (which
5696 -- must be before the freeze point).
5698 Set_Is_Frozen (Typ, False);
5700 -- Do not add the spec of predefined primitives in case of
5701 -- CPP tagged type derivations that have convention CPP.
5703 if Is_CPP_Class (Root_Type (Typ))
5704 and then Convention (Typ) = Convention_CPP
5705 then
5706 null;
5708 -- Do not add the spec of the predefined primitives if we are
5709 -- compiling under restriction No_Dispatching_Calls.
5711 elsif not Restriction_Active (No_Dispatching_Calls) then
5712 Make_Predefined_Primitive_Specs (Typ, Predef_List, Renamed_Eq);
5713 Insert_List_Before_And_Analyze (N, Predef_List);
5714 end if;
5716 -- Ada 2005 (AI-391): For a nonabstract null extension, create
5717 -- wrapper functions for each nonoverridden inherited function
5718 -- with a controlling result of the type. The wrapper for such
5719 -- a function returns an extension aggregate that invokes the
5720 -- parent function.
5722 if Ada_Version >= Ada_2005
5723 and then not Is_Abstract_Type (Typ)
5724 and then Is_Null_Extension (Typ)
5725 then
5726 Make_Controlling_Function_Wrappers
5727 (Typ, Wrapper_Decl_List, Wrapper_Body_List);
5728 Insert_List_Before_And_Analyze (N, Wrapper_Decl_List);
5729 end if;
5731 -- Ada 2005 (AI-251): For a nonabstract type extension, build
5732 -- null procedure declarations for each set of homographic null
5733 -- procedures that are inherited from interface types but not
5734 -- overridden. This is done to ensure that the dispatch table
5735 -- entry associated with such null primitives are properly filled.
5737 if Ada_Version >= Ada_2005
5738 and then Etype (Typ) /= Typ
5739 and then not Is_Abstract_Type (Typ)
5740 and then Has_Interfaces (Typ)
5741 then
5742 Insert_Actions (N, Make_Null_Procedure_Specs (Typ));
5743 end if;
5745 Set_Is_Frozen (Typ);
5747 if not Is_Derived_Type (Typ)
5748 or else Is_Tagged_Type (Etype (Typ))
5749 then
5750 Set_All_DT_Position (Typ);
5752 -- If this is a type derived from an untagged private type whose
5753 -- full view is tagged, the type is marked tagged for layout
5754 -- reasons, but it has no dispatch table.
5756 elsif Is_Derived_Type (Typ)
5757 and then Is_Private_Type (Etype (Typ))
5758 and then not Is_Tagged_Type (Etype (Typ))
5759 then
5760 return;
5761 end if;
5763 -- Create and decorate the tags. Suppress their creation when
5764 -- not Tagged_Type_Expansion because the dispatching mechanism is
5765 -- handled internally by the virtual target.
5767 if Tagged_Type_Expansion then
5768 Append_Freeze_Actions (Typ, Make_Tags (Typ));
5770 -- Generate dispatch table of locally defined tagged type.
5771 -- Dispatch tables of library level tagged types are built
5772 -- later (see Build_Static_Dispatch_Tables).
5774 if not Building_Static_DT (Typ) then
5775 Append_Freeze_Actions (Typ, Make_DT (Typ));
5777 -- Register dispatch table wrappers in the dispatch table.
5778 -- It could not be done when these wrappers were built
5779 -- because, at that stage, the dispatch table was not
5780 -- available.
5782 Register_Dispatch_Table_Wrappers (Typ);
5783 end if;
5784 end if;
5786 -- If the type has unknown discriminants, propagate dispatching
5787 -- information to its underlying record view, which does not get
5788 -- its own dispatch table.
5790 if Is_Derived_Type (Typ)
5791 and then Has_Unknown_Discriminants (Typ)
5792 and then Present (Underlying_Record_View (Typ))
5793 then
5794 declare
5795 Rep : constant Entity_Id := Underlying_Record_View (Typ);
5796 begin
5797 Set_Access_Disp_Table
5798 (Rep, Access_Disp_Table (Typ));
5799 Set_Dispatch_Table_Wrappers
5800 (Rep, Dispatch_Table_Wrappers (Typ));
5801 Set_Direct_Primitive_Operations
5802 (Rep, Direct_Primitive_Operations (Typ));
5803 end;
5804 end if;
5806 -- Make sure that the primitives Initialize, Adjust and Finalize
5807 -- are Frozen before other TSS subprograms. We don't want them
5808 -- Frozen inside.
5810 if Is_Controlled (Typ) then
5811 if not Is_Limited_Type (Typ) then
5812 Append_Freeze_Actions (Typ,
5813 Freeze_Entity (Find_Prim_Op (Typ, Name_Adjust), Typ));
5814 end if;
5816 Append_Freeze_Actions (Typ,
5817 Freeze_Entity (Find_Prim_Op (Typ, Name_Initialize), Typ));
5819 Append_Freeze_Actions (Typ,
5820 Freeze_Entity (Find_Prim_Op (Typ, Name_Finalize), Typ));
5821 end if;
5823 -- Freeze rest of primitive operations. There is no need to handle
5824 -- the predefined primitives if we are compiling under restriction
5825 -- No_Dispatching_Calls.
5827 if not Restriction_Active (No_Dispatching_Calls) then
5828 Append_Freeze_Actions (Typ, Predefined_Primitive_Freeze (Typ));
5829 end if;
5830 end if;
5832 -- In the untagged case, ever since Ada 83 an equality function must
5833 -- be provided for variant records that are not unchecked unions.
5835 elsif Has_Discriminants (Typ)
5836 and then not Is_Limited_Type (Typ)
5837 and then Present (Component_List (Type_Definition (Typ_Decl)))
5838 and then
5839 Present (Variant_Part (Component_List (Type_Definition (Typ_Decl))))
5840 then
5841 Build_Variant_Record_Equality (Typ);
5843 -- In Ada 2012 the equality function composes, and thus must be built
5844 -- explicitly just as for tagged records.
5846 -- This is done unconditionally to ensure that tools can be linked
5847 -- properly with user programs compiled with older language versions.
5848 -- In addition, this is needed because "=" composes for bounded strings
5849 -- in all language versions (see Exp_Ch4.Expand_Composite_Equality).
5851 elsif Comes_From_Source (Typ)
5852 and then Convention (Typ) = Convention_Ada
5853 and then not Is_Limited_Type (Typ)
5854 then
5855 Build_Untagged_Record_Equality (Typ);
5856 end if;
5858 -- Before building the record initialization procedure, if we are
5859 -- dealing with a concurrent record value type, then we must go through
5860 -- the discriminants, exchanging discriminals between the concurrent
5861 -- type and the concurrent record value type. See the section "Handling
5862 -- of Discriminants" in the Einfo spec for details.
5864 if Is_Concurrent_Record_Type (Typ) and then Has_Discriminants (Typ) then
5865 declare
5866 Ctyp : constant Entity_Id :=
5867 Corresponding_Concurrent_Type (Typ);
5868 Conc_Discr : Entity_Id;
5869 Rec_Discr : Entity_Id;
5870 Temp : Entity_Id;
5872 begin
5873 Conc_Discr := First_Discriminant (Ctyp);
5874 Rec_Discr := First_Discriminant (Typ);
5875 while Present (Conc_Discr) loop
5876 Temp := Discriminal (Conc_Discr);
5877 Set_Discriminal (Conc_Discr, Discriminal (Rec_Discr));
5878 Set_Discriminal (Rec_Discr, Temp);
5880 Set_Discriminal_Link (Discriminal (Conc_Discr), Conc_Discr);
5881 Set_Discriminal_Link (Discriminal (Rec_Discr), Rec_Discr);
5883 Next_Discriminant (Conc_Discr);
5884 Next_Discriminant (Rec_Discr);
5885 end loop;
5886 end;
5887 end if;
5889 if Has_Controlled_Component (Typ) then
5890 Build_Controlling_Procs (Typ);
5891 end if;
5893 Adjust_Discriminants (Typ);
5895 -- Do not need init for interfaces on virtual targets since they're
5896 -- abstract.
5898 if Tagged_Type_Expansion or else not Is_Interface (Typ) then
5899 Build_Record_Init_Proc (Typ_Decl, Typ);
5900 end if;
5902 -- For tagged type that are not interfaces, build bodies of primitive
5903 -- operations. Note: do this after building the record initialization
5904 -- procedure, since the primitive operations may need the initialization
5905 -- routine. There is no need to add predefined primitives of interfaces
5906 -- because all their predefined primitives are abstract.
5908 if Is_Tagged_Type (Typ) and then not Is_Interface (Typ) then
5910 -- Do not add the body of predefined primitives in case of CPP tagged
5911 -- type derivations that have convention CPP.
5913 if Is_CPP_Class (Root_Type (Typ))
5914 and then Convention (Typ) = Convention_CPP
5915 then
5916 null;
5918 -- Do not add the body of the predefined primitives if we are
5919 -- compiling under restriction No_Dispatching_Calls or if we are
5920 -- compiling a CPP tagged type.
5922 elsif not Restriction_Active (No_Dispatching_Calls) then
5924 -- Create the body of TSS primitive Finalize_Address. This must
5925 -- be done before the bodies of all predefined primitives are
5926 -- created. If Typ is limited, Stream_Input and Stream_Read may
5927 -- produce build-in-place allocations and for those the expander
5928 -- needs Finalize_Address.
5930 Make_Finalize_Address_Body (Typ);
5931 Predef_List := Predefined_Primitive_Bodies (Typ, Renamed_Eq);
5932 Append_Freeze_Actions (Typ, Predef_List);
5933 end if;
5935 -- Ada 2005 (AI-391): If any wrappers were created for nonoverridden
5936 -- inherited functions, then add their bodies to the freeze actions.
5938 Append_Freeze_Actions (Typ, Wrapper_Body_List);
5939 end if;
5941 -- Create extra formals for the primitive operations of the type.
5942 -- This must be done before analyzing the body of the initialization
5943 -- procedure, because a self-referential type might call one of these
5944 -- primitives in the body of the init_proc itself.
5946 -- This is not needed:
5947 -- 1) If expansion is disabled, because extra formals are only added
5948 -- when we are generating code.
5950 -- 2) For types with foreign convention since primitives with foreign
5951 -- convention don't have extra formals and AI95-117 requires that
5952 -- all primitives of a tagged type inherit the convention.
5954 if Expander_Active
5955 and then Is_Tagged_Type (Typ)
5956 and then not Has_Foreign_Convention (Typ)
5957 then
5958 declare
5959 Elmt : Elmt_Id;
5960 E : Entity_Id;
5962 begin
5963 -- Add extra formals to primitive operations
5965 Elmt := First_Elmt (Primitive_Operations (Typ));
5966 while Present (Elmt) loop
5967 Create_Extra_Formals (Node (Elmt));
5968 Next_Elmt (Elmt);
5969 end loop;
5971 -- Add extra formals to renamings of primitive operations. The
5972 -- addition of extra formals is done in two steps to minimize
5973 -- the compile time required for this action; the evaluation of
5974 -- Find_Dispatching_Type() and Contains() is only done here for
5975 -- renamings that are not primitive operations.
5977 E := First_Entity (Scope (Typ));
5978 while Present (E) loop
5979 if Is_Dispatching_Operation (E)
5980 and then Present (Alias (E))
5981 and then Find_Dispatching_Type (E) = Typ
5982 and then not Contains (Primitive_Operations (Typ), E)
5983 then
5984 Create_Extra_Formals (E);
5985 end if;
5987 Next_Entity (E);
5988 end loop;
5990 pragma Debug (Validate_Tagged_Type_Extra_Formals (Typ));
5991 end;
5992 end if;
5994 -- Build internal subprograms of primitives with class-wide
5995 -- pre/postconditions.
5997 if Is_Tagged_Type (Typ) then
5998 Build_Class_Condition_Subprograms (Typ);
5999 end if;
6000 end Expand_Freeze_Record_Type;
6002 ------------------------------------
6003 -- Expand_N_Full_Type_Declaration --
6004 ------------------------------------
6006 procedure Expand_N_Full_Type_Declaration (N : Node_Id) is
6007 procedure Build_Master (Ptr_Typ : Entity_Id);
6008 -- Create the master associated with Ptr_Typ
6010 ------------------
6011 -- Build_Master --
6012 ------------------
6014 procedure Build_Master (Ptr_Typ : Entity_Id) is
6015 Desig_Typ : Entity_Id := Designated_Type (Ptr_Typ);
6017 begin
6018 -- If the designated type is an incomplete view coming from a
6019 -- limited-with'ed package, we need to use the nonlimited view in
6020 -- case it has tasks.
6022 if Is_Incomplete_Type (Desig_Typ)
6023 and then Present (Non_Limited_View (Desig_Typ))
6024 then
6025 Desig_Typ := Non_Limited_View (Desig_Typ);
6026 end if;
6028 -- Anonymous access types are created for the components of the
6029 -- record parameter for an entry declaration. No master is created
6030 -- for such a type.
6032 if Has_Task (Desig_Typ) then
6033 Build_Master_Entity (Ptr_Typ);
6034 Build_Master_Renaming (Ptr_Typ);
6036 -- Create a class-wide master because a Master_Id must be generated
6037 -- for access-to-limited-class-wide types whose root may be extended
6038 -- with task components.
6040 -- Note: This code covers access-to-limited-interfaces because they
6041 -- can be used to reference tasks implementing them.
6043 -- Suppress the master creation for access types created for entry
6044 -- formal parameters (parameter block component types). Seems like
6045 -- suppression should be more general for compiler-generated types,
6046 -- but testing Comes_From_Source may be too general in this case
6047 -- (affects some test output)???
6049 elsif not Is_Param_Block_Component_Type (Ptr_Typ)
6050 and then Is_Limited_Class_Wide_Type (Desig_Typ)
6051 then
6052 Build_Class_Wide_Master (Ptr_Typ);
6053 end if;
6054 end Build_Master;
6056 -- Local declarations
6058 Def_Id : constant Entity_Id := Defining_Identifier (N);
6059 B_Id : constant Entity_Id := Base_Type (Def_Id);
6060 FN : Node_Id;
6061 Par_Id : Entity_Id;
6063 -- Start of processing for Expand_N_Full_Type_Declaration
6065 begin
6066 if Is_Access_Type (Def_Id) then
6067 Build_Master (Def_Id);
6069 if Ekind (Def_Id) = E_Access_Protected_Subprogram_Type then
6070 Expand_Access_Protected_Subprogram_Type (N);
6071 end if;
6073 -- Array of anonymous access-to-task pointers
6075 elsif Ada_Version >= Ada_2005
6076 and then Is_Array_Type (Def_Id)
6077 and then Is_Access_Type (Component_Type (Def_Id))
6078 and then Ekind (Component_Type (Def_Id)) = E_Anonymous_Access_Type
6079 then
6080 Build_Master (Component_Type (Def_Id));
6082 elsif Has_Task (Def_Id) then
6083 Expand_Previous_Access_Type (Def_Id);
6085 -- Check the components of a record type or array of records for
6086 -- anonymous access-to-task pointers.
6088 elsif Ada_Version >= Ada_2005
6089 and then (Is_Record_Type (Def_Id)
6090 or else
6091 (Is_Array_Type (Def_Id)
6092 and then Is_Record_Type (Component_Type (Def_Id))))
6093 then
6094 declare
6095 Comp : Entity_Id;
6096 First : Boolean;
6097 M_Id : Entity_Id := Empty;
6098 Typ : Entity_Id;
6100 begin
6101 if Is_Array_Type (Def_Id) then
6102 Comp := First_Entity (Component_Type (Def_Id));
6103 else
6104 Comp := First_Entity (Def_Id);
6105 end if;
6107 -- Examine all components looking for anonymous access-to-task
6108 -- types.
6110 First := True;
6111 while Present (Comp) loop
6112 Typ := Etype (Comp);
6114 if Ekind (Typ) = E_Anonymous_Access_Type
6115 and then Might_Have_Tasks
6116 (Available_View (Designated_Type (Typ)))
6117 and then No (Master_Id (Typ))
6118 then
6119 -- Ensure that the record or array type have a _master
6121 if First then
6122 Build_Master_Entity (Def_Id);
6123 Build_Master_Renaming (Typ);
6124 M_Id := Master_Id (Typ);
6126 First := False;
6128 -- Reuse the same master to service any additional types
6130 else
6131 pragma Assert (Present (M_Id));
6132 Set_Master_Id (Typ, M_Id);
6133 end if;
6134 end if;
6136 Next_Entity (Comp);
6137 end loop;
6138 end;
6139 end if;
6141 Par_Id := Etype (B_Id);
6143 -- The parent type is private then we need to inherit any TSS operations
6144 -- from the full view.
6146 if Is_Private_Type (Par_Id)
6147 and then Present (Full_View (Par_Id))
6148 then
6149 Par_Id := Base_Type (Full_View (Par_Id));
6150 end if;
6152 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
6153 and then not Is_Tagged_Type (Def_Id)
6154 and then Present (Freeze_Node (Par_Id))
6155 and then Present (TSS_Elist (Freeze_Node (Par_Id)))
6156 then
6157 Ensure_Freeze_Node (B_Id);
6158 FN := Freeze_Node (B_Id);
6160 if No (TSS_Elist (FN)) then
6161 Set_TSS_Elist (FN, New_Elmt_List);
6162 end if;
6164 declare
6165 T_E : constant Elist_Id := TSS_Elist (FN);
6166 Elmt : Elmt_Id;
6168 begin
6169 Elmt := First_Elmt (TSS_Elist (Freeze_Node (Par_Id)));
6170 while Present (Elmt) loop
6171 if Chars (Node (Elmt)) /= Name_uInit then
6172 Append_Elmt (Node (Elmt), T_E);
6173 end if;
6175 Next_Elmt (Elmt);
6176 end loop;
6178 -- If the derived type itself is private with a full view, then
6179 -- associate the full view with the inherited TSS_Elist as well.
6181 if Is_Private_Type (B_Id)
6182 and then Present (Full_View (B_Id))
6183 then
6184 Ensure_Freeze_Node (Base_Type (Full_View (B_Id)));
6185 Set_TSS_Elist
6186 (Freeze_Node (Base_Type (Full_View (B_Id))), TSS_Elist (FN));
6187 end if;
6188 end;
6189 end if;
6190 end Expand_N_Full_Type_Declaration;
6192 ---------------------------------
6193 -- Expand_N_Object_Declaration --
6194 ---------------------------------
6196 procedure Expand_N_Object_Declaration (N : Node_Id) is
6197 Loc : constant Source_Ptr := Sloc (N);
6198 Def_Id : constant Entity_Id := Defining_Identifier (N);
6199 Expr : constant Node_Id := Expression (N);
6200 Obj_Def : constant Node_Id := Object_Definition (N);
6201 Typ : constant Entity_Id := Etype (Def_Id);
6202 Base_Typ : constant Entity_Id := Base_Type (Typ);
6203 Next_N : constant Node_Id := Next (N);
6205 Special_Ret_Obj : constant Boolean := Is_Special_Return_Object (Def_Id);
6206 -- If this is a special return object, it will be allocated differently
6207 -- and ultimately rewritten as a renaming, so initialization activities
6208 -- need to be deferred until after that is done.
6210 Func_Id : constant Entity_Id :=
6211 (if Special_Ret_Obj then Return_Applies_To (Scope (Def_Id)) else Empty);
6212 -- The function if this is a special return object, otherwise Empty
6214 function Build_Equivalent_Aggregate return Boolean;
6215 -- If the object has a constrained discriminated type and no initial
6216 -- value, it may be possible to build an equivalent aggregate instead,
6217 -- and prevent an actual call to the initialization procedure.
6219 function Build_Heap_Or_Pool_Allocator
6220 (Temp_Id : Entity_Id;
6221 Temp_Typ : Entity_Id;
6222 Ret_Typ : Entity_Id;
6223 Alloc_Expr : Node_Id) return Node_Id;
6224 -- Create the statements necessary to allocate a return object on the
6225 -- heap or user-defined storage pool. The object may need finalization
6226 -- actions depending on the return type.
6228 -- * Controlled case
6230 -- if BIPfinalizationmaster = null then
6231 -- Temp_Id := <Alloc_Expr>;
6232 -- else
6233 -- declare
6234 -- type Ptr_Typ is access Ret_Typ;
6235 -- for Ptr_Typ'Storage_Pool use
6236 -- Base_Pool (BIPfinalizationmaster.all).all;
6237 -- Local : Ptr_Typ;
6239 -- begin
6240 -- procedure Allocate (...) is
6241 -- begin
6242 -- System.Storage_Pools.Subpools.Allocate_Any (...);
6243 -- end Allocate;
6245 -- Local := <Alloc_Expr>;
6246 -- Temp_Id := Temp_Typ (Local);
6247 -- end;
6248 -- end if;
6250 -- * Non-controlled case
6252 -- Temp_Id := <Alloc_Expr>;
6254 -- Temp_Id is the temporary which is used to reference the internally
6255 -- created object in all allocation forms. Temp_Typ is the type of the
6256 -- temporary. Func_Id is the enclosing function. Ret_Typ is the return
6257 -- type of Func_Id. Alloc_Expr is the actual allocator.
6259 function BIP_Function_Call_Id return Entity_Id;
6260 -- If the object initialization expression is a call to a build-in-place
6261 -- function, return the id of the called function; otherwise return
6262 -- Empty.
6264 procedure Count_Default_Sized_Task_Stacks
6265 (Typ : Entity_Id;
6266 Pri_Stacks : out Int;
6267 Sec_Stacks : out Int);
6268 -- Count the number of default-sized primary and secondary task stacks
6269 -- required for task objects contained within type Typ. If the number of
6270 -- task objects contained within the type is not known at compile time
6271 -- the procedure will return the stack counts of zero.
6273 procedure Default_Initialize_Object (After : Node_Id);
6274 -- Generate all default initialization actions for object Def_Id. Any
6275 -- new code is inserted after node After.
6277 procedure Initialize_Return_Object
6278 (Tag_Assign : Node_Id;
6279 Adj_Call : Node_Id;
6280 Expr : Node_Id;
6281 Init_Stmt : Node_Id;
6282 After : Node_Id);
6283 -- Generate all initialization actions for return object Def_Id. Any
6284 -- new code is inserted after node After.
6286 function Is_Renamable_Function_Call (Expr : Node_Id) return Boolean;
6287 -- If we are not at library level and the object declaration originally
6288 -- appears in the form:
6290 -- Obj : Typ := Func (...);
6292 -- and has been rewritten as the dereference of a captured reference
6293 -- to the function result built either on the primary or the secondary
6294 -- stack, then the declaration can be rewritten as the renaming of this
6295 -- dereference:
6297 -- type Ann is access all Typ;
6298 -- Rnn : constant Axx := Func (...)'reference;
6299 -- Obj : Typ renames Rnn.all;
6301 -- This will avoid making an extra copy and, in the case where Typ needs
6302 -- finalization, a pair of calls to the Adjust and Finalize primitives,
6303 -- or Deep_Adjust and Deep_Finalize routines, depending on whether Typ
6304 -- has components that themselves need finalization.
6306 -- However, in the case of a special return object, we need to make sure
6307 -- that the object Rnn is recognized by the Is_Related_To_Func_Return
6308 -- predicate; otherwise, if it is of a type that needs finalization,
6309 -- then Requires_Cleanup_Actions would return true because of this and
6310 -- Build_Finalizer would finalize it prematurely because of this (see
6311 -- also Expand_Simple_Function_Return for the same test in the case of
6312 -- a simple return).
6314 -- Finally, in the case of a special return object, we also need to make
6315 -- sure that the two functions return on the same stack, otherwise we
6316 -- would create a dangling reference.
6318 function Make_Allocator_For_Return (Expr : Node_Id) return Node_Id;
6319 -- Make an allocator for a return object initialized with Expr
6321 function OK_To_Rename_Ref (N : Node_Id) return Boolean;
6322 -- Return True if N denotes an entity with OK_To_Rename set
6324 --------------------------------
6325 -- Build_Equivalent_Aggregate --
6326 --------------------------------
6328 function Build_Equivalent_Aggregate return Boolean is
6329 Aggr : Node_Id;
6330 Comp : Entity_Id;
6331 Discr : Elmt_Id;
6332 Full_Type : Entity_Id;
6334 begin
6335 Full_Type := Typ;
6337 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
6338 Full_Type := Full_View (Typ);
6339 end if;
6341 -- Only perform this transformation if Elaboration_Code is forbidden
6342 -- or undesirable, and if this is a global entity of a constrained
6343 -- record type.
6345 -- If Initialize_Scalars might be active this transformation cannot
6346 -- be performed either, because it will lead to different semantics
6347 -- or because elaboration code will in fact be created.
6349 if Ekind (Full_Type) /= E_Record_Subtype
6350 or else not Has_Discriminants (Full_Type)
6351 or else not Is_Constrained (Full_Type)
6352 or else Is_Controlled (Full_Type)
6353 or else Is_Limited_Type (Full_Type)
6354 or else not Restriction_Active (No_Initialize_Scalars)
6355 then
6356 return False;
6357 end if;
6359 if Ekind (Current_Scope) = E_Package
6360 and then
6361 (Restriction_Active (No_Elaboration_Code)
6362 or else Is_Preelaborated (Current_Scope))
6363 then
6364 -- Building a static aggregate is possible if the discriminants
6365 -- have static values and the other components have static
6366 -- defaults or none.
6368 Discr := First_Elmt (Discriminant_Constraint (Full_Type));
6369 while Present (Discr) loop
6370 if not Is_OK_Static_Expression (Node (Discr)) then
6371 return False;
6372 end if;
6374 Next_Elmt (Discr);
6375 end loop;
6377 -- Check that initialized components are OK, and that non-
6378 -- initialized components do not require a call to their own
6379 -- initialization procedure.
6381 Comp := First_Component (Full_Type);
6382 while Present (Comp) loop
6383 if Present (Expression (Parent (Comp)))
6384 and then
6385 not Is_OK_Static_Expression (Expression (Parent (Comp)))
6386 then
6387 return False;
6389 elsif Has_Non_Null_Base_Init_Proc (Etype (Comp)) then
6390 return False;
6392 end if;
6394 Next_Component (Comp);
6395 end loop;
6397 -- Everything is static, assemble the aggregate, discriminant
6398 -- values first.
6400 Aggr :=
6401 Make_Aggregate (Loc,
6402 Expressions => New_List,
6403 Component_Associations => New_List);
6405 Discr := First_Elmt (Discriminant_Constraint (Full_Type));
6406 while Present (Discr) loop
6407 Append_To (Expressions (Aggr), New_Copy (Node (Discr)));
6408 Next_Elmt (Discr);
6409 end loop;
6411 -- Now collect values of initialized components
6413 Comp := First_Component (Full_Type);
6414 while Present (Comp) loop
6415 if Present (Expression (Parent (Comp))) then
6416 Append_To (Component_Associations (Aggr),
6417 Make_Component_Association (Loc,
6418 Choices => New_List (New_Occurrence_Of (Comp, Loc)),
6419 Expression => New_Copy_Tree
6420 (Expression (Parent (Comp)))));
6421 end if;
6423 Next_Component (Comp);
6424 end loop;
6426 -- Finally, box-initialize remaining components
6428 Append_To (Component_Associations (Aggr),
6429 Make_Component_Association (Loc,
6430 Choices => New_List (Make_Others_Choice (Loc)),
6431 Expression => Empty));
6432 Set_Box_Present (Last (Component_Associations (Aggr)));
6433 Set_Expression (N, Aggr);
6435 if Typ /= Full_Type then
6436 Analyze_And_Resolve (Aggr, Full_View (Base_Type (Full_Type)));
6437 Rewrite (Aggr, Unchecked_Convert_To (Typ, Aggr));
6438 Analyze_And_Resolve (Aggr, Typ);
6439 else
6440 Analyze_And_Resolve (Aggr, Full_Type);
6441 end if;
6443 return True;
6445 else
6446 return False;
6447 end if;
6448 end Build_Equivalent_Aggregate;
6450 ----------------------------------
6451 -- Build_Heap_Or_Pool_Allocator --
6452 ----------------------------------
6454 function Build_Heap_Or_Pool_Allocator
6455 (Temp_Id : Entity_Id;
6456 Temp_Typ : Entity_Id;
6457 Ret_Typ : Entity_Id;
6458 Alloc_Expr : Node_Id) return Node_Id
6460 begin
6461 pragma Assert (Is_Build_In_Place_Function (Func_Id));
6463 -- Processing for objects that require finalization actions
6465 if Needs_Finalization (Ret_Typ) then
6466 declare
6467 Decls : constant List_Id := New_List;
6468 Fin_Mas_Id : constant Entity_Id :=
6469 Build_In_Place_Formal (Func_Id, BIP_Finalization_Master);
6470 Orig_Expr : constant Node_Id := New_Copy_Tree (Alloc_Expr);
6471 Stmts : constant List_Id := New_List;
6472 Local_Id : Entity_Id;
6473 Pool_Id : Entity_Id;
6474 Ptr_Typ : Entity_Id;
6476 begin
6477 -- Generate:
6478 -- Pool_Id renames Base_Pool (BIPfinalizationmaster.all).all;
6480 Pool_Id := Make_Temporary (Loc, 'P');
6482 Append_To (Decls,
6483 Make_Object_Renaming_Declaration (Loc,
6484 Defining_Identifier => Pool_Id,
6485 Subtype_Mark =>
6486 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc),
6487 Name =>
6488 Make_Explicit_Dereference (Loc,
6489 Prefix =>
6490 Make_Function_Call (Loc,
6491 Name =>
6492 New_Occurrence_Of (RTE (RE_Base_Pool), Loc),
6493 Parameter_Associations => New_List (
6494 Make_Explicit_Dereference (Loc,
6495 Prefix =>
6496 New_Occurrence_Of (Fin_Mas_Id, Loc)))))));
6498 -- Create an access type which uses the storage pool of the
6499 -- caller's master. This additional type is necessary because
6500 -- the finalization master cannot be associated with the type
6501 -- of the temporary. Otherwise the secondary stack allocation
6502 -- will fail.
6504 -- Generate:
6505 -- type Ptr_Typ is access Ret_Typ;
6507 Ptr_Typ := Make_Temporary (Loc, 'P');
6509 Append_To (Decls,
6510 Make_Full_Type_Declaration (Loc,
6511 Defining_Identifier => Ptr_Typ,
6512 Type_Definition =>
6513 Make_Access_To_Object_Definition (Loc,
6514 Subtype_Indication =>
6515 New_Occurrence_Of (Ret_Typ, Loc))));
6517 -- Perform minor decoration in order to set the master and the
6518 -- storage pool attributes.
6520 Mutate_Ekind (Ptr_Typ, E_Access_Type);
6521 Set_Finalization_Master (Ptr_Typ, Fin_Mas_Id);
6522 Set_Associated_Storage_Pool (Ptr_Typ, Pool_Id);
6524 -- Create the temporary, generate:
6525 -- Local_Id : Ptr_Typ;
6527 Local_Id := Make_Temporary (Loc, 'T');
6529 Append_To (Decls,
6530 Make_Object_Declaration (Loc,
6531 Defining_Identifier => Local_Id,
6532 Object_Definition =>
6533 New_Occurrence_Of (Ptr_Typ, Loc)));
6535 -- Allocate the object, generate:
6536 -- Local_Id := <Alloc_Expr>;
6538 Append_To (Stmts,
6539 Make_Assignment_Statement (Loc,
6540 Name => New_Occurrence_Of (Local_Id, Loc),
6541 Expression => Alloc_Expr));
6543 -- Generate:
6544 -- Temp_Id := Temp_Typ (Local_Id);
6546 Append_To (Stmts,
6547 Make_Assignment_Statement (Loc,
6548 Name => New_Occurrence_Of (Temp_Id, Loc),
6549 Expression =>
6550 Unchecked_Convert_To (Temp_Typ,
6551 New_Occurrence_Of (Local_Id, Loc))));
6553 -- Wrap the allocation in a block. This is further conditioned
6554 -- by checking the caller finalization master at runtime. A
6555 -- null value indicates a non-existent master, most likely due
6556 -- to a Finalize_Storage_Only allocation.
6558 -- Generate:
6559 -- if BIPfinalizationmaster = null then
6560 -- Temp_Id := <Orig_Expr>;
6561 -- else
6562 -- declare
6563 -- <Decls>
6564 -- begin
6565 -- <Stmts>
6566 -- end;
6567 -- end if;
6569 return
6570 Make_If_Statement (Loc,
6571 Condition =>
6572 Make_Op_Eq (Loc,
6573 Left_Opnd => New_Occurrence_Of (Fin_Mas_Id, Loc),
6574 Right_Opnd => Make_Null (Loc)),
6576 Then_Statements => New_List (
6577 Make_Assignment_Statement (Loc,
6578 Name => New_Occurrence_Of (Temp_Id, Loc),
6579 Expression => Orig_Expr)),
6581 Else_Statements => New_List (
6582 Make_Block_Statement (Loc,
6583 Declarations => Decls,
6584 Handled_Statement_Sequence =>
6585 Make_Handled_Sequence_Of_Statements (Loc,
6586 Statements => Stmts))));
6587 end;
6589 -- For all other cases, generate:
6590 -- Temp_Id := <Alloc_Expr>;
6592 else
6593 return
6594 Make_Assignment_Statement (Loc,
6595 Name => New_Occurrence_Of (Temp_Id, Loc),
6596 Expression => Alloc_Expr);
6597 end if;
6598 end Build_Heap_Or_Pool_Allocator;
6600 --------------------------
6601 -- BIP_Function_Call_Id --
6602 --------------------------
6604 function BIP_Function_Call_Id return Entity_Id is
6606 function Func_Call_Id (Function_Call : Node_Id) return Entity_Id;
6607 -- Return the id of the called function.
6609 function Func_Call_Id (Function_Call : Node_Id) return Entity_Id is
6610 Call_Node : constant Node_Id := Unqual_Conv (Function_Call);
6612 begin
6613 if Is_Entity_Name (Name (Call_Node)) then
6614 return Entity (Name (Call_Node));
6616 elsif Nkind (Name (Call_Node)) = N_Explicit_Dereference then
6617 return Etype (Name (Call_Node));
6619 else
6620 pragma Assert (Nkind (Name (Call_Node)) = N_Selected_Component);
6621 return Etype (Entity (Selector_Name (Name (Call_Node))));
6622 end if;
6623 end Func_Call_Id;
6625 -- Local declarations
6627 BIP_Func_Call : Node_Id;
6628 Expr_Q : constant Node_Id := Unqual_Conv (Expr);
6630 -- Start of processing for BIP_Function_Call_Id
6632 begin
6633 if Is_Build_In_Place_Function_Call (Expr_Q) then
6634 return Func_Call_Id (Expr_Q);
6635 end if;
6637 BIP_Func_Call := Unqual_BIP_Iface_Function_Call (Expr_Q);
6639 if Present (BIP_Func_Call) then
6641 -- In the case of an explicitly dereferenced call, return the
6642 -- subprogram type.
6644 if Nkind (Name (BIP_Func_Call)) = N_Explicit_Dereference then
6645 return Etype (Name (BIP_Func_Call));
6646 else
6647 pragma Assert (Is_Entity_Name (Name (BIP_Func_Call)));
6648 return Entity (Name (BIP_Func_Call));
6649 end if;
6651 elsif Nkind (Expr_Q) = N_Reference
6652 and then Is_Build_In_Place_Function_Call (Prefix (Expr_Q))
6653 then
6654 return Func_Call_Id (Prefix (Expr_Q));
6656 else
6657 return Empty;
6658 end if;
6659 end BIP_Function_Call_Id;
6661 -------------------------------------
6662 -- Count_Default_Sized_Task_Stacks --
6663 -------------------------------------
6665 procedure Count_Default_Sized_Task_Stacks
6666 (Typ : Entity_Id;
6667 Pri_Stacks : out Int;
6668 Sec_Stacks : out Int)
6670 Component : Entity_Id;
6672 begin
6673 -- To calculate the number of default-sized task stacks required for
6674 -- an object of Typ, a depth-first recursive traversal of the AST
6675 -- from the Typ entity node is undertaken. Only type nodes containing
6676 -- task objects are visited.
6678 Pri_Stacks := 0;
6679 Sec_Stacks := 0;
6681 if not Has_Task (Typ) then
6682 return;
6683 end if;
6685 case Ekind (Typ) is
6686 when E_Task_Subtype
6687 | E_Task_Type
6689 -- A task type is found marking the bottom of the descent. If
6690 -- the type has no representation aspect for the corresponding
6691 -- stack then that stack is using the default size.
6693 if Present (Get_Rep_Item (Typ, Name_Storage_Size)) then
6694 Pri_Stacks := 0;
6695 else
6696 Pri_Stacks := 1;
6697 end if;
6699 if Present (Get_Rep_Item (Typ, Name_Secondary_Stack_Size)) then
6700 Sec_Stacks := 0;
6701 else
6702 Sec_Stacks := 1;
6703 end if;
6705 when E_Array_Subtype
6706 | E_Array_Type
6708 -- First find the number of default stacks contained within an
6709 -- array component.
6711 Count_Default_Sized_Task_Stacks
6712 (Component_Type (Typ),
6713 Pri_Stacks,
6714 Sec_Stacks);
6716 -- Then multiply the result by the size of the array
6718 declare
6719 Quantity : constant Int := Number_Of_Elements_In_Array (Typ);
6720 -- Number_Of_Elements_In_Array is non-trival, consequently
6721 -- its result is captured as an optimization.
6723 begin
6724 Pri_Stacks := Pri_Stacks * Quantity;
6725 Sec_Stacks := Sec_Stacks * Quantity;
6726 end;
6728 when E_Protected_Subtype
6729 | E_Protected_Type
6730 | E_Record_Subtype
6731 | E_Record_Type
6733 Component := First_Component_Or_Discriminant (Typ);
6735 -- Recursively descend each component of the composite type
6736 -- looking for tasks, but only if the component is marked as
6737 -- having a task.
6739 while Present (Component) loop
6740 if Has_Task (Etype (Component)) then
6741 declare
6742 P : Int;
6743 S : Int;
6745 begin
6746 Count_Default_Sized_Task_Stacks
6747 (Etype (Component), P, S);
6748 Pri_Stacks := Pri_Stacks + P;
6749 Sec_Stacks := Sec_Stacks + S;
6750 end;
6751 end if;
6753 Next_Component_Or_Discriminant (Component);
6754 end loop;
6756 when E_Limited_Private_Subtype
6757 | E_Limited_Private_Type
6758 | E_Record_Subtype_With_Private
6759 | E_Record_Type_With_Private
6761 -- Switch to the full view of the private type to continue
6762 -- search.
6764 Count_Default_Sized_Task_Stacks
6765 (Full_View (Typ), Pri_Stacks, Sec_Stacks);
6767 -- Other types should not contain tasks
6769 when others =>
6770 raise Program_Error;
6771 end case;
6772 end Count_Default_Sized_Task_Stacks;
6774 -------------------------------
6775 -- Default_Initialize_Object --
6776 -------------------------------
6778 procedure Default_Initialize_Object (After : Node_Id) is
6779 function New_Object_Reference return Node_Id;
6780 -- Return a new reference to Def_Id with attributes Assignment_OK and
6781 -- Must_Not_Freeze already set.
6783 function Simple_Initialization_OK
6784 (Init_Typ : Entity_Id) return Boolean;
6785 -- Determine whether object declaration N with entity Def_Id needs
6786 -- simple initialization, assuming that it is of type Init_Typ.
6788 --------------------------
6789 -- New_Object_Reference --
6790 --------------------------
6792 function New_Object_Reference return Node_Id is
6793 Obj_Ref : constant Node_Id := New_Occurrence_Of (Def_Id, Loc);
6795 begin
6796 -- The call to the type init proc or [Deep_]Finalize must not
6797 -- freeze the related object as the call is internally generated.
6798 -- This way legal rep clauses that apply to the object will not be
6799 -- flagged. Note that the initialization call may be removed if
6800 -- pragma Import is encountered or moved to the freeze actions of
6801 -- the object because of an address clause.
6803 Set_Assignment_OK (Obj_Ref);
6804 Set_Must_Not_Freeze (Obj_Ref);
6806 return Obj_Ref;
6807 end New_Object_Reference;
6809 ------------------------------
6810 -- Simple_Initialization_OK --
6811 ------------------------------
6813 function Simple_Initialization_OK
6814 (Init_Typ : Entity_Id) return Boolean
6816 begin
6817 -- Do not consider the object declaration if it comes with an
6818 -- initialization expression, or is internal in which case it
6819 -- will be assigned later.
6821 return
6822 not Is_Internal (Def_Id)
6823 and then not Has_Init_Expression (N)
6824 and then Needs_Simple_Initialization
6825 (Typ => Init_Typ,
6826 Consider_IS =>
6827 Initialize_Scalars
6828 and then No (Following_Address_Clause (N)));
6829 end Simple_Initialization_OK;
6831 -- Local variables
6833 Exceptions_OK : constant Boolean :=
6834 not Restriction_Active (No_Exception_Propagation);
6836 Aggr_Init : Node_Id;
6837 Comp_Init : List_Id := No_List;
6838 Fin_Block : Node_Id;
6839 Fin_Call : Node_Id;
6840 Init_Stmts : List_Id := No_List;
6841 Obj_Init : Node_Id := Empty;
6842 Obj_Ref : Node_Id;
6844 -- Start of processing for Default_Initialize_Object
6846 begin
6847 -- Default initialization is suppressed for objects that are already
6848 -- known to be imported (i.e. whose declaration specifies the Import
6849 -- aspect). Note that for objects with a pragma Import, we generate
6850 -- initialization here, and then remove it downstream when processing
6851 -- the pragma. It is also suppressed for variables for which a pragma
6852 -- Suppress_Initialization has been explicitly given
6854 if Is_Imported (Def_Id) or else Suppress_Initialization (Def_Id) then
6855 return;
6857 -- Nothing to do if the object being initialized is of a task type
6858 -- and restriction No_Tasking is in effect, because this is a direct
6859 -- violation of the restriction.
6861 elsif Is_Task_Type (Base_Typ)
6862 and then Restriction_Active (No_Tasking)
6863 then
6864 return;
6865 end if;
6867 -- The expansion performed by this routine is as follows:
6869 -- begin
6870 -- Abort_Defer;
6871 -- Type_Init_Proc (Obj);
6873 -- begin
6874 -- [Deep_]Initialize (Obj);
6876 -- exception
6877 -- when others =>
6878 -- [Deep_]Finalize (Obj, Self => False);
6879 -- raise;
6880 -- end;
6881 -- at end
6882 -- Abort_Undefer_Direct;
6883 -- end;
6885 -- Initialize the components of the object
6887 if Has_Non_Null_Base_Init_Proc (Typ)
6888 and then not No_Initialization (N)
6889 and then not Initialization_Suppressed (Typ)
6890 then
6891 -- Do not initialize the components if No_Default_Initialization
6892 -- applies as the actual restriction check will occur later when
6893 -- the object is frozen as it is not known yet whether the object
6894 -- is imported or not.
6896 if not Restriction_Active (No_Default_Initialization) then
6898 -- If the values of the components are compile-time known, use
6899 -- their prebuilt aggregate form directly.
6901 Aggr_Init := Static_Initialization (Base_Init_Proc (Typ));
6903 if Present (Aggr_Init) then
6904 Set_Expression (N,
6905 New_Copy_Tree (Aggr_Init, New_Scope => Current_Scope));
6907 -- If type has discriminants, try to build an equivalent
6908 -- aggregate using discriminant values from the declaration.
6909 -- This is a useful optimization, in particular if restriction
6910 -- No_Elaboration_Code is active.
6912 elsif Build_Equivalent_Aggregate then
6913 null;
6915 -- Optimize the default initialization of an array object when
6916 -- pragma Initialize_Scalars or Normalize_Scalars is in effect.
6917 -- Construct an in-place initialization aggregate which may be
6918 -- convert into a fast memset by the backend.
6920 elsif Init_Or_Norm_Scalars
6921 and then Is_Array_Type (Typ)
6923 -- The array must lack atomic components because they are
6924 -- treated as non-static, and as a result the backend will
6925 -- not initialize the memory in one go.
6927 and then not Has_Atomic_Components (Typ)
6929 -- The array must not be packed because the invalid values
6930 -- in System.Scalar_Values are multiples of Storage_Unit.
6932 and then not Is_Packed (Typ)
6934 -- The array must have static non-empty ranges, otherwise
6935 -- the backend cannot initialize the memory in one go.
6937 and then Has_Static_Non_Empty_Array_Bounds (Typ)
6939 -- The optimization is only relevant for arrays of scalar
6940 -- types.
6942 and then Is_Scalar_Type (Component_Type (Typ))
6944 -- Similar to regular array initialization using a type
6945 -- init proc, predicate checks are not performed because the
6946 -- initialization values are intentionally invalid, and may
6947 -- violate the predicate.
6949 and then not Has_Predicates (Component_Type (Typ))
6951 -- Array default component value takes precedence over
6952 -- Init_Or_Norm_Scalars.
6954 and then No (Find_Aspect (Typ,
6955 Aspect_Default_Component_Value))
6957 -- The component type must have a single initialization value
6959 and then Simple_Initialization_OK (Component_Type (Typ))
6960 then
6961 Set_No_Initialization (N, False);
6962 Set_Expression (N,
6963 Get_Simple_Init_Val
6964 (Typ => Typ,
6965 N => Obj_Def,
6966 Size => (if Known_Esize (Def_Id) then Esize (Def_Id)
6967 else Uint_0)));
6969 Analyze_And_Resolve
6970 (Expression (N), Typ, Suppress => All_Checks);
6972 -- Otherwise invoke the type init proc, generate:
6973 -- Type_Init_Proc (Obj);
6975 else
6976 Obj_Ref := New_Object_Reference;
6978 if Comes_From_Source (Def_Id) then
6979 Initialization_Warning (Obj_Ref);
6980 end if;
6982 Comp_Init := Build_Initialization_Call (Loc, Obj_Ref, Typ);
6983 end if;
6984 end if;
6986 -- Provide a default value if the object needs simple initialization
6988 elsif Simple_Initialization_OK (Typ) then
6989 Set_No_Initialization (N, False);
6990 Set_Expression (N,
6991 Get_Simple_Init_Val
6992 (Typ => Typ,
6993 N => Obj_Def,
6994 Size =>
6995 (if Known_Esize (Def_Id) then Esize (Def_Id) else Uint_0)));
6997 Analyze_And_Resolve (Expression (N), Typ);
6998 end if;
7000 -- Initialize the object, generate:
7001 -- [Deep_]Initialize (Obj);
7003 if Needs_Finalization (Typ) and then not No_Initialization (N) then
7004 Obj_Init :=
7005 Make_Init_Call
7006 (Obj_Ref => New_Object_Reference,
7007 Typ => Typ);
7008 end if;
7010 -- Build a special finalization block when both the object and its
7011 -- controlled components are to be initialized. The block finalizes
7012 -- the components if the object initialization fails. Generate:
7014 -- begin
7015 -- <Obj_Init>
7017 -- exception
7018 -- when others =>
7019 -- <Fin_Call>
7020 -- raise;
7021 -- end;
7023 if Has_Controlled_Component (Typ)
7024 and then Present (Comp_Init)
7025 and then Present (Obj_Init)
7026 and then Exceptions_OK
7027 then
7028 Init_Stmts := Comp_Init;
7030 Fin_Call :=
7031 Make_Final_Call
7032 (Obj_Ref => New_Object_Reference,
7033 Typ => Typ,
7034 Skip_Self => True);
7036 if Present (Fin_Call) then
7038 -- Do not emit warnings related to the elaboration order when a
7039 -- controlled object is declared before the body of Finalize is
7040 -- seen.
7042 if Legacy_Elaboration_Checks then
7043 Set_No_Elaboration_Check (Fin_Call);
7044 end if;
7046 Fin_Block :=
7047 Make_Block_Statement (Loc,
7048 Declarations => No_List,
7050 Handled_Statement_Sequence =>
7051 Make_Handled_Sequence_Of_Statements (Loc,
7052 Statements => New_List (Obj_Init),
7054 Exception_Handlers => New_List (
7055 Make_Exception_Handler (Loc,
7056 Exception_Choices => New_List (
7057 Make_Others_Choice (Loc)),
7059 Statements => New_List (
7060 Fin_Call,
7061 Make_Raise_Statement (Loc))))));
7063 -- Signal the ABE mechanism that the block carries out
7064 -- initialization actions.
7066 Set_Is_Initialization_Block (Fin_Block);
7068 Append_To (Init_Stmts, Fin_Block);
7069 end if;
7071 -- Otherwise finalization is not required, the initialization calls
7072 -- are passed to the abort block building circuitry, generate:
7074 -- Type_Init_Proc (Obj);
7075 -- [Deep_]Initialize (Obj);
7077 else
7078 if Present (Comp_Init) then
7079 Init_Stmts := Comp_Init;
7080 end if;
7082 if Present (Obj_Init) then
7083 if No (Init_Stmts) then
7084 Init_Stmts := New_List;
7085 end if;
7087 Append_To (Init_Stmts, Obj_Init);
7088 end if;
7089 end if;
7091 -- Build an abort block to protect the initialization calls
7093 if Abort_Allowed
7094 and then Present (Comp_Init)
7095 and then Present (Obj_Init)
7096 then
7097 -- Generate:
7098 -- Abort_Defer;
7100 Prepend_To (Init_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
7102 -- When exceptions are propagated, abort deferral must take place
7103 -- in the presence of initialization or finalization exceptions.
7104 -- Generate:
7106 -- begin
7107 -- Abort_Defer;
7108 -- <Init_Stmts>
7109 -- at end
7110 -- Abort_Undefer_Direct;
7111 -- end;
7113 if Exceptions_OK then
7114 Init_Stmts := New_List (
7115 Build_Abort_Undefer_Block (Loc,
7116 Stmts => Init_Stmts,
7117 Context => N));
7119 -- Otherwise exceptions are not propagated. Generate:
7121 -- Abort_Defer;
7122 -- <Init_Stmts>
7123 -- Abort_Undefer;
7125 else
7126 Append_To (Init_Stmts,
7127 Build_Runtime_Call (Loc, RE_Abort_Undefer));
7128 end if;
7129 end if;
7131 -- Insert the whole initialization sequence into the tree. If the
7132 -- object has a delayed freeze, as will be the case when it has
7133 -- aspect specifications, the initialization sequence is part of
7134 -- the freeze actions.
7136 if Present (Init_Stmts) then
7137 if Has_Delayed_Freeze (Def_Id) then
7138 Append_Freeze_Actions (Def_Id, Init_Stmts);
7139 else
7140 Insert_Actions_After (After, Init_Stmts);
7141 end if;
7142 end if;
7143 end Default_Initialize_Object;
7145 ------------------------------
7146 -- Initialize_Return_Object --
7147 ------------------------------
7149 procedure Initialize_Return_Object
7150 (Tag_Assign : Node_Id;
7151 Adj_Call : Node_Id;
7152 Expr : Node_Id;
7153 Init_Stmt : Node_Id;
7154 After : Node_Id)
7156 begin
7157 if Present (Tag_Assign) then
7158 Insert_Action_After (After, Tag_Assign);
7159 end if;
7161 if Present (Adj_Call) then
7162 Insert_Action_After (After, Adj_Call);
7163 end if;
7165 if No (Expr) then
7166 Default_Initialize_Object (After);
7168 elsif Is_Delayed_Aggregate (Expr)
7169 and then not No_Initialization (N)
7170 then
7171 Convert_Aggr_In_Object_Decl (N);
7173 elsif Present (Init_Stmt) then
7174 Insert_Action_After (After, Init_Stmt);
7175 Set_Expression (N, Empty);
7176 end if;
7177 end Initialize_Return_Object;
7179 --------------------------------
7180 -- Is_Renamable_Function_Call --
7181 --------------------------------
7183 function Is_Renamable_Function_Call (Expr : Node_Id) return Boolean is
7184 begin
7185 return not Is_Library_Level_Entity (Def_Id)
7186 and then Is_Captured_Function_Call (Expr)
7187 and then (not Special_Ret_Obj
7188 or else
7189 (Is_Related_To_Func_Return (Entity (Prefix (Expr)))
7190 and then Needs_Secondary_Stack (Etype (Expr)) =
7191 Needs_Secondary_Stack (Etype (Func_Id))));
7192 end Is_Renamable_Function_Call;
7194 -------------------------------
7195 -- Make_Allocator_For_Return --
7196 -------------------------------
7198 function Make_Allocator_For_Return (Expr : Node_Id) return Node_Id is
7199 Alloc : Node_Id;
7200 Alloc_Expr : Entity_Id;
7201 Alloc_Typ : Entity_Id;
7203 begin
7204 -- If the return object's declaration does not include an expression,
7205 -- then we use its subtype for the allocation. Likewise in the case
7206 -- of a degenerate expression like a raise expression.
7208 if No (Expr)
7209 or else Nkind (Original_Node (Expr)) = N_Raise_Expression
7210 then
7211 Alloc_Typ := Typ;
7213 -- If the return object's declaration includes an expression, then
7214 -- there are two cases: either the nominal subtype of the object is
7215 -- definite and we can use it for the allocation directly, or it is
7216 -- not and Analyze_Object_Declaration should have built an actual
7217 -- subtype from the expression.
7219 -- However, there are exceptions in the latter case for interfaces
7220 -- (see Analyze_Object_Declaration), as well as class-wide types and
7221 -- types with unknown discriminants if they are additionally limited
7222 -- (see Expand_Subtype_From_Expr), so we must cope with them.
7224 elsif Is_Interface (Typ) then
7225 pragma Assert (Is_Class_Wide_Type (Typ));
7227 -- For interfaces, we use the type of the expression, except if
7228 -- we need to put back a conversion that we have removed earlier
7229 -- in the processing.
7231 if Is_Class_Wide_Type (Etype (Expr)) then
7232 Alloc_Typ := Typ;
7233 else
7234 Alloc_Typ := Etype (Expr);
7235 end if;
7237 elsif Is_Class_Wide_Type (Typ) then
7239 -- For class-wide types, we have to make sure that we use the
7240 -- dynamic type of the expression for the allocation, either by
7241 -- means of its (static) subtype or through the actual subtype.
7243 if Has_Tag_Of_Type (Expr) then
7244 Alloc_Typ := Etype (Expr);
7246 else pragma Assert (Ekind (Typ) = E_Class_Wide_Subtype
7247 and then Present (Equivalent_Type (Typ)));
7249 Alloc_Typ := Typ;
7250 end if;
7252 else pragma Assert (Is_Definite_Subtype (Typ)
7253 or else (Has_Unknown_Discriminants (Typ)
7254 and then Is_Limited_View (Typ)));
7256 Alloc_Typ := Typ;
7257 end if;
7259 -- If the return object's declaration includes an expression and the
7260 -- declaration isn't marked as No_Initialization, then we generate an
7261 -- allocator with a qualified expression. Although this is necessary
7262 -- only in the case where the result type is an interface (or class-
7263 -- wide interface), we do it in all cases for the sake of consistency
7264 -- instead of subsequently generating a separate assignment.
7266 if Present (Expr)
7267 and then not Is_Delayed_Aggregate (Expr)
7268 and then not No_Initialization (N)
7269 then
7270 -- Ada 2005 (AI95-344): If the result type is class-wide, insert
7271 -- a check that the level of the return expression's underlying
7272 -- type is not deeper than the level of the master enclosing the
7273 -- function.
7275 -- AI12-043: The check is made immediately after the return object
7276 -- is created.
7278 if Is_Class_Wide_Type (Etype (Func_Id)) then
7279 Apply_CW_Accessibility_Check (Expr, Func_Id);
7280 end if;
7282 Alloc_Expr := New_Copy_Tree (Expr);
7284 if Etype (Alloc_Expr) /= Alloc_Typ then
7285 Alloc_Expr := Convert_To (Alloc_Typ, Alloc_Expr);
7286 end if;
7288 Alloc :=
7289 Make_Allocator (Loc,
7290 Expression =>
7291 Make_Qualified_Expression (Loc,
7292 Subtype_Mark =>
7293 New_Occurrence_Of (Alloc_Typ, Loc),
7294 Expression => Alloc_Expr));
7296 else
7297 Alloc :=
7298 Make_Allocator (Loc,
7299 Expression => New_Occurrence_Of (Alloc_Typ, Loc));
7301 -- If the return object requires default initialization, then it
7302 -- will happen later following the elaboration of the renaming.
7303 -- If we don't turn it off here, then the object will be default
7304 -- initialized twice.
7306 Set_No_Initialization (Alloc);
7307 end if;
7309 -- Set the flag indicating that the allocator is made for a special
7310 -- return object. This is used to bypass various legality checks as
7311 -- well as to make sure that the result is not adjusted twice.
7313 Set_For_Special_Return_Object (Alloc);
7315 return Alloc;
7316 end Make_Allocator_For_Return;
7318 ----------------------
7319 -- OK_To_Rename_Ref --
7320 ----------------------
7322 function OK_To_Rename_Ref (N : Node_Id) return Boolean is
7323 begin
7324 return Is_Entity_Name (N)
7325 and then Ekind (Entity (N)) = E_Variable
7326 and then OK_To_Rename (Entity (N));
7327 end OK_To_Rename_Ref;
7329 -- Local variables
7331 Adj_Call : Node_Id := Empty;
7332 Expr_Q : Node_Id := Empty;
7333 Tag_Assign : Node_Id := Empty;
7335 Init_After : Node_Id := N;
7336 -- Node after which the initialization actions are to be inserted. This
7337 -- is normally N, except for the case of a shared passive variable, in
7338 -- which case the init proc call must be inserted only after the bodies
7339 -- of the shared variable procedures have been seen.
7341 Has_BIP_Init_Expr : Boolean := False;
7342 -- Whether the object is initialized with a BIP function call
7344 Rewrite_As_Renaming : Boolean := False;
7345 -- Whether to turn the declaration into a renaming at the end
7347 Nominal_Subtype_Is_Constrained_Array : constant Boolean :=
7348 Comes_From_Source (Obj_Def)
7349 and then Is_Array_Type (Typ) and then Is_Constrained (Typ);
7350 -- Used to avoid rewriting as a renaming for constrained arrays,
7351 -- which is only a problem for source arrays; others have the
7352 -- correct bounds (see below).
7354 -- Start of processing for Expand_N_Object_Declaration
7356 begin
7357 -- Don't do anything for deferred constants. All proper actions will be
7358 -- expanded during the full declaration.
7360 if No (Expr) and Constant_Present (N) then
7361 return;
7362 end if;
7364 -- The type of the object cannot be abstract. This is diagnosed at the
7365 -- point the object is frozen, which happens after the declaration is
7366 -- fully expanded, so simply return now.
7368 if Is_Abstract_Type (Typ) then
7369 return;
7370 end if;
7372 -- No action needed for the internal imported dummy object added by
7373 -- Make_DT to compute the offset of the components that reference
7374 -- secondary dispatch tables; required to avoid never-ending loop
7375 -- processing this internal object declaration.
7377 if Tagged_Type_Expansion
7378 and then Is_Internal (Def_Id)
7379 and then Is_Imported (Def_Id)
7380 and then Related_Type (Def_Id) = Implementation_Base_Type (Typ)
7381 then
7382 return;
7383 end if;
7385 -- Make shared memory routines for shared passive variable
7387 if Is_Shared_Passive (Def_Id) then
7388 Init_After := Make_Shared_Var_Procs (N);
7389 end if;
7391 -- Determine whether the object is initialized with a BIP function call
7393 if Present (Expr) then
7394 Expr_Q := Unqualify (Expr);
7396 Has_BIP_Init_Expr :=
7397 Is_Build_In_Place_Function_Call (Expr_Q)
7398 or else Present (Unqual_BIP_Iface_Function_Call (Expr_Q))
7399 or else (Nkind (Expr_Q) = N_Reference
7400 and then
7401 Is_Build_In_Place_Function_Call (Prefix (Expr_Q)));
7402 end if;
7404 -- If tasks are being declared, make sure we have an activation chain
7405 -- defined for the tasks (has no effect if we already have one), and
7406 -- also that a Master variable is established (and that the appropriate
7407 -- enclosing construct is established as a task master).
7409 if Has_Task (Typ)
7410 or else Might_Have_Tasks (Typ)
7411 or else (Has_BIP_Init_Expr
7412 and then Needs_BIP_Task_Actuals (BIP_Function_Call_Id))
7413 then
7414 Build_Activation_Chain_Entity (N);
7416 if Has_Task (Typ) then
7417 Build_Master_Entity (Def_Id);
7419 -- Handle objects initialized with BIP function calls
7421 elsif Has_BIP_Init_Expr then
7422 Build_Master_Entity (Def_Id);
7423 end if;
7424 end if;
7426 -- If No_Implicit_Heap_Allocations or No_Implicit_Task_Allocations
7427 -- restrictions are active then default-sized secondary stacks are
7428 -- generated by the binder and allocated by SS_Init. To provide the
7429 -- binder the number of stacks to generate, the number of default-sized
7430 -- stacks required for task objects contained within the object
7431 -- declaration N is calculated here as it is at this point where
7432 -- unconstrained types become constrained. The result is stored in the
7433 -- enclosing unit's Unit_Record.
7435 -- Note if N is an array object declaration that has an initialization
7436 -- expression, a second object declaration for the initialization
7437 -- expression is created by the compiler. To prevent double counting
7438 -- of the stacks in this scenario, the stacks of the first array are
7439 -- not counted.
7441 if Might_Have_Tasks (Typ)
7442 and then not Restriction_Active (No_Secondary_Stack)
7443 and then (Restriction_Active (No_Implicit_Heap_Allocations)
7444 or else Restriction_Active (No_Implicit_Task_Allocations))
7445 and then not (Ekind (Typ) in E_Array_Type | E_Array_Subtype
7446 and then Has_Init_Expression (N))
7447 then
7448 declare
7449 PS_Count, SS_Count : Int := 0;
7450 begin
7451 Count_Default_Sized_Task_Stacks (Typ, PS_Count, SS_Count);
7452 Increment_Primary_Stack_Count (PS_Count);
7453 Increment_Sec_Stack_Count (SS_Count);
7454 end;
7455 end if;
7457 -- Default initialization required, and no expression present
7459 if No (Expr) then
7460 -- If we have a type with a variant part, the initialization proc
7461 -- will contain implicit tests of the discriminant values, which
7462 -- counts as a violation of the restriction No_Implicit_Conditionals.
7464 if Has_Variant_Part (Typ) then
7465 declare
7466 Msg : Boolean;
7468 begin
7469 Check_Restriction (Msg, No_Implicit_Conditionals, Obj_Def);
7471 if Msg then
7472 Error_Msg_N
7473 ("\initialization of variant record tests discriminants",
7474 Obj_Def);
7475 return;
7476 end if;
7477 end;
7478 end if;
7480 -- For the default initialization case, if we have a private type
7481 -- with invariants, and invariant checks are enabled, then insert an
7482 -- invariant check after the object declaration. Note that it is OK
7483 -- to clobber the object with an invalid value since if the exception
7484 -- is raised, then the object will go out of scope. In the case where
7485 -- an array object is initialized with an aggregate, the expression
7486 -- is removed. Check flag Has_Init_Expression to avoid generating a
7487 -- junk invariant check and flag No_Initialization to avoid checking
7488 -- an uninitialized object such as a compiler temporary used for an
7489 -- aggregate.
7491 if Has_Invariants (Base_Typ)
7492 and then Present (Invariant_Procedure (Base_Typ))
7493 and then not Has_Init_Expression (N)
7494 and then not No_Initialization (N)
7495 then
7496 -- If entity has an address clause or aspect, make invariant
7497 -- call into a freeze action for the explicit freeze node for
7498 -- object. Otherwise insert invariant check after declaration.
7500 if Present (Following_Address_Clause (N))
7501 or else Has_Aspect (Def_Id, Aspect_Address)
7502 then
7503 Ensure_Freeze_Node (Def_Id);
7504 Set_Has_Delayed_Freeze (Def_Id);
7505 Set_Is_Frozen (Def_Id, False);
7507 if not Partial_View_Has_Unknown_Discr (Typ) then
7508 Append_Freeze_Action (Def_Id,
7509 Make_Invariant_Call (New_Occurrence_Of (Def_Id, Loc)));
7510 end if;
7512 elsif not Partial_View_Has_Unknown_Discr (Typ) then
7513 Insert_After (N,
7514 Make_Invariant_Call (New_Occurrence_Of (Def_Id, Loc)));
7515 end if;
7516 end if;
7518 if not Special_Ret_Obj then
7519 Default_Initialize_Object (Init_After);
7520 end if;
7522 -- Generate attribute for Persistent_BSS if needed
7524 if Persistent_BSS_Mode
7525 and then Comes_From_Source (N)
7526 and then Is_Potentially_Persistent_Type (Typ)
7527 and then not Has_Init_Expression (N)
7528 and then Is_Library_Level_Entity (Def_Id)
7529 then
7530 declare
7531 Prag : Node_Id;
7532 begin
7533 Prag :=
7534 Make_Linker_Section_Pragma
7535 (Def_Id, Sloc (N), ".persistent.bss");
7536 Insert_After (N, Prag);
7537 Analyze (Prag);
7538 end;
7539 end if;
7541 -- If access type, then we know it is null if not initialized
7543 if Is_Access_Type (Typ) then
7544 Set_Is_Known_Null (Def_Id);
7545 end if;
7547 -- Explicit initialization present
7549 else
7550 -- Obtain actual expression from qualified expression
7552 Expr_Q := Unqualify (Expr);
7554 -- When we have the appropriate type of aggregate in the expression
7555 -- (it has been determined during analysis of the aggregate by
7556 -- setting the delay flag), let's perform in place assignment and
7557 -- thus avoid creating a temporary.
7559 if Is_Delayed_Aggregate (Expr_Q) then
7561 -- An aggregate that must be built in place is not resolved and
7562 -- expanded until the enclosing construct is expanded. This will
7563 -- happen when the aggregate is limited and the declared object
7564 -- has a following address clause; it happens also when generating
7565 -- C code for an aggregate that has an alignment or address clause
7566 -- (see Analyze_Object_Declaration). Resolution is done without
7567 -- expansion because it will take place when the declaration
7568 -- itself is expanded.
7570 if (Is_Limited_Type (Typ) or else Modify_Tree_For_C)
7571 and then not Analyzed (Expr)
7572 then
7573 Expander_Mode_Save_And_Set (False);
7574 Resolve (Expr, Typ);
7575 Expander_Mode_Restore;
7576 end if;
7578 if not Special_Ret_Obj then
7579 Convert_Aggr_In_Object_Decl (N);
7580 end if;
7582 -- Ada 2005 (AI-318-02): If the initialization expression is a call
7583 -- to a build-in-place function, then access to the declared object
7584 -- must be passed to the function. Currently we limit such functions
7585 -- to those with constrained limited result subtypes, but eventually
7586 -- plan to expand the allowed forms of functions that are treated as
7587 -- build-in-place.
7589 elsif Is_Build_In_Place_Function_Call (Expr_Q) then
7590 Make_Build_In_Place_Call_In_Object_Declaration (N, Expr_Q);
7592 -- The previous call expands the expression initializing the
7593 -- built-in-place object into further code that will be analyzed
7594 -- later. No further expansion needed here.
7596 return;
7598 -- This is the same as the previous 'elsif', except that the call has
7599 -- been transformed by other expansion activities into something like
7600 -- F(...)'Reference.
7602 elsif Nkind (Expr_Q) = N_Reference
7603 and then Is_Build_In_Place_Function_Call (Prefix (Expr_Q))
7604 and then not Is_Expanded_Build_In_Place_Call
7605 (Unqual_Conv (Prefix (Expr_Q)))
7606 then
7607 Make_Build_In_Place_Call_In_Anonymous_Context (Prefix (Expr_Q));
7609 -- The previous call expands the expression initializing the
7610 -- built-in-place object into further code that will be analyzed
7611 -- later. No further expansion needed here.
7613 return;
7615 -- Ada 2005 (AI-318-02): Specialization of the previous case for
7616 -- expressions containing a build-in-place function call whose
7617 -- returned object covers interface types, and Expr_Q has calls to
7618 -- Ada.Tags.Displace to displace the pointer to the returned build-
7619 -- in-place object to reference the secondary dispatch table of a
7620 -- covered interface type.
7622 elsif Present (Unqual_BIP_Iface_Function_Call (Expr_Q)) then
7623 Make_Build_In_Place_Iface_Call_In_Object_Declaration (N, Expr_Q);
7625 -- The previous call expands the expression initializing the
7626 -- built-in-place object into further code that will be analyzed
7627 -- later. No further expansion needed here.
7629 return;
7631 -- Ada 2005 (AI-251): Rewrite the expression that initializes a
7632 -- class-wide interface object to ensure that we copy the full
7633 -- object, unless we are targetting a VM where interfaces are handled
7634 -- by VM itself. Note that if the root type of Typ is an ancestor of
7635 -- Expr's type, both types share the same dispatch table and there is
7636 -- no need to displace the pointer.
7638 elsif Is_Interface (Typ)
7640 -- Avoid never-ending recursion because if Equivalent_Type is set
7641 -- then we've done it already and must not do it again.
7643 and then not
7644 (Nkind (Obj_Def) = N_Identifier
7645 and then Present (Equivalent_Type (Entity (Obj_Def))))
7646 then
7647 pragma Assert (Is_Class_Wide_Type (Typ));
7649 -- If the original node of the expression was a conversion
7650 -- to this specific class-wide interface type then restore
7651 -- the original node because we must copy the object before
7652 -- displacing the pointer to reference the secondary tag
7653 -- component. This code must be kept synchronized with the
7654 -- expansion done by routine Expand_Interface_Conversion
7656 if not Comes_From_Source (Expr)
7657 and then Nkind (Expr) = N_Explicit_Dereference
7658 and then Nkind (Original_Node (Expr)) = N_Type_Conversion
7659 and then Etype (Original_Node (Expr)) = Typ
7660 then
7661 Rewrite (Expr, Original_Node (Expression (N)));
7662 end if;
7664 -- Avoid expansion of redundant interface conversion
7666 if Nkind (Expr) = N_Type_Conversion
7667 and then Etype (Expr) = Typ
7668 then
7669 Expr_Q := Expression (Expr);
7670 else
7671 Expr_Q := Expr;
7672 end if;
7674 -- We may use a renaming if the initialization expression is a
7675 -- captured function call that meets a few conditions.
7677 Rewrite_As_Renaming := Is_Renamable_Function_Call (Expr_Q);
7679 -- If the object is a special return object, then bypass special
7680 -- treatment of class-wide interface initialization below. In this
7681 -- case, the expansion of the return object will take care of this
7682 -- initialization via the expansion of the allocator.
7684 if Special_Ret_Obj and then not Rewrite_As_Renaming then
7686 -- If the type needs finalization and is not inherently
7687 -- limited, then the target is adjusted after the copy
7688 -- and attached to the finalization list.
7690 if Needs_Finalization (Typ)
7691 and then not Is_Limited_View (Typ)
7692 then
7693 Adj_Call :=
7694 Make_Adjust_Call (
7695 Obj_Ref => New_Occurrence_Of (Def_Id, Loc),
7696 Typ => Base_Typ);
7697 end if;
7699 -- Renaming an expression of the object's type is immediate
7701 elsif Rewrite_As_Renaming
7702 and then Base_Type (Etype (Expr_Q)) = Base_Type (Typ)
7703 then
7704 null;
7706 elsif Tagged_Type_Expansion then
7707 declare
7708 Iface : constant Entity_Id := Root_Type (Typ);
7710 Expr_Typ : Entity_Id;
7711 New_Expr : Node_Id;
7712 Obj_Id : Entity_Id;
7713 Ptr_Obj_Decl : Node_Id;
7714 Ptr_Obj_Id : Entity_Id;
7715 Tag_Comp : Node_Id;
7717 begin
7718 Expr_Typ := Base_Type (Etype (Expr_Q));
7719 if Is_Class_Wide_Type (Expr_Typ) then
7720 Expr_Typ := Root_Type (Expr_Typ);
7721 end if;
7723 -- Rename limited objects since they cannot be copied
7725 if Is_Limited_Record (Expr_Typ) then
7726 Rewrite_As_Renaming := True;
7727 end if;
7729 Obj_Id := Make_Temporary (Loc, 'D', Expr_Q);
7731 -- Replace
7732 -- IW : I'Class := Expr;
7733 -- by
7734 -- Dnn : Tag renames Tag_Ptr!(Expr'Address).all;
7735 -- type Ityp is not null access I'Class;
7736 -- Rnn : constant Ityp :=
7737 -- Ityp!(Displace (Dnn'Address, I'Tag));
7738 -- IW : I'Class renames Rnn.all;
7740 if Rewrite_As_Renaming then
7741 New_Expr :=
7742 Make_Explicit_Dereference (Loc,
7743 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
7744 Make_Attribute_Reference (Loc,
7745 Prefix => Relocate_Node (Expr_Q),
7746 Attribute_Name => Name_Address)));
7748 -- Suppress junk access checks on RE_Tag_Ptr
7750 Insert_Action (N,
7751 Make_Object_Renaming_Declaration (Loc,
7752 Defining_Identifier => Obj_Id,
7753 Subtype_Mark =>
7754 New_Occurrence_Of (RTE (RE_Tag), Loc),
7755 Name => New_Expr),
7756 Suppress => Access_Check);
7758 -- Dynamically reference the tag associated with the
7759 -- interface.
7761 Tag_Comp :=
7762 Make_Function_Call (Loc,
7763 Name => New_Occurrence_Of (RTE (RE_Displace), Loc),
7764 Parameter_Associations => New_List (
7765 Make_Attribute_Reference (Loc,
7766 Prefix => New_Occurrence_Of (Obj_Id, Loc),
7767 Attribute_Name => Name_Address),
7768 New_Occurrence_Of
7769 (Node (First_Elmt (Access_Disp_Table (Iface))),
7770 Loc)));
7772 -- Replace
7773 -- IW : I'Class := Expr;
7774 -- by
7775 -- Dnn : Typ := Expr;
7776 -- type Ityp is not null access I'Class;
7777 -- Rnn : constant Ityp := Ityp (Dnn.I_Tag'Address);
7778 -- IW : I'Class renames Rnn.all;
7780 elsif Has_Tag_Of_Type (Expr_Q)
7781 and then Interface_Present_In_Ancestor (Expr_Typ, Typ)
7782 and then (Expr_Typ = Etype (Expr_Typ)
7783 or else not
7784 Is_Variable_Size_Record (Etype (Expr_Typ)))
7785 then
7786 Insert_Action (N,
7787 Make_Object_Declaration (Loc,
7788 Defining_Identifier => Obj_Id,
7789 Object_Definition =>
7790 New_Occurrence_Of (Expr_Typ, Loc),
7791 Expression => Relocate_Node (Expr_Q)));
7793 -- Statically reference the tag associated with the
7794 -- interface
7796 Tag_Comp :=
7797 Make_Selected_Component (Loc,
7798 Prefix => New_Occurrence_Of (Obj_Id, Loc),
7799 Selector_Name =>
7800 New_Occurrence_Of
7801 (Find_Interface_Tag (Expr_Typ, Iface), Loc));
7803 -- Replace
7804 -- IW : I'Class := Expr;
7805 -- by
7806 -- type Equiv_Record is record ... end record;
7807 -- implicit subtype CW is <Class_Wide_Subtype>;
7808 -- Dnn : CW := CW!(Expr);
7809 -- type Ityp is not null access I'Class;
7810 -- Rnn : constant Ityp :=
7811 -- Ityp!(Displace (Dnn'Address, I'Tag));
7812 -- IW : I'Class renames Rnn.all;
7814 else
7815 -- Generate the equivalent record type and update the
7816 -- subtype indication to reference it.
7818 Expand_Subtype_From_Expr
7819 (N => N,
7820 Unc_Type => Typ,
7821 Subtype_Indic => Obj_Def,
7822 Exp => Expr_Q);
7824 -- For interface types we use 'Address which displaces
7825 -- the pointer to the base of the object (if required).
7827 if Is_Interface (Etype (Expr_Q)) then
7828 New_Expr :=
7829 Unchecked_Convert_To (Etype (Obj_Def),
7830 Make_Explicit_Dereference (Loc,
7831 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
7832 Make_Attribute_Reference (Loc,
7833 Prefix => Relocate_Node (Expr_Q),
7834 Attribute_Name => Name_Address))));
7836 -- For other types, no displacement is needed
7838 else
7839 New_Expr := Relocate_Node (Expr_Q);
7840 end if;
7842 -- Suppress junk access checks on RE_Tag_Ptr
7844 Insert_Action (N,
7845 Make_Object_Declaration (Loc,
7846 Defining_Identifier => Obj_Id,
7847 Object_Definition =>
7848 New_Occurrence_Of (Etype (Obj_Def), Loc),
7849 Expression => New_Expr),
7850 Suppress => Access_Check);
7852 -- Dynamically reference the tag associated with the
7853 -- interface.
7855 Tag_Comp :=
7856 Make_Function_Call (Loc,
7857 Name => New_Occurrence_Of (RTE (RE_Displace), Loc),
7858 Parameter_Associations => New_List (
7859 Make_Attribute_Reference (Loc,
7860 Prefix => New_Occurrence_Of (Obj_Id, Loc),
7861 Attribute_Name => Name_Address),
7862 New_Occurrence_Of
7863 (Node (First_Elmt (Access_Disp_Table (Iface))),
7864 Loc)));
7865 end if;
7867 -- As explained in Exp_Disp, we use Convert_Tag_To_Interface
7868 -- to do the final conversion, but we insert an intermediate
7869 -- temporary before the dereference so that we can process
7870 -- the expansion as part of the analysis of the declaration
7871 -- of this temporary, and then rewrite manually the original
7872 -- object as the simple renaming of this dereference.
7874 Tag_Comp := Convert_Tag_To_Interface (Typ, Tag_Comp);
7875 pragma Assert (Nkind (Tag_Comp) = N_Explicit_Dereference
7876 and then
7877 Nkind (Prefix (Tag_Comp)) = N_Unchecked_Type_Conversion);
7879 Ptr_Obj_Id := Make_Temporary (Loc, 'R');
7881 Ptr_Obj_Decl :=
7882 Make_Object_Declaration (Loc,
7883 Defining_Identifier => Ptr_Obj_Id,
7884 Constant_Present => True,
7885 Object_Definition =>
7886 New_Occurrence_Of
7887 (Entity (Subtype_Mark (Prefix (Tag_Comp))), Loc),
7888 Expression => Prefix (Tag_Comp));
7890 Insert_Action (N, Ptr_Obj_Decl, Suppress => All_Checks);
7892 Set_Prefix (Tag_Comp, New_Occurrence_Of (Ptr_Obj_Id, Loc));
7893 Expr_Q := Tag_Comp;
7894 Set_Etype (Expr_Q, Typ);
7895 Set_Parent (Expr_Q, N);
7897 Rewrite_As_Renaming := True;
7898 end;
7900 else
7901 return;
7902 end if;
7904 -- Common case of explicit object initialization
7906 else
7907 -- Small optimization: if the expression is a function call and
7908 -- the object is stand-alone, not declared at library level and of
7909 -- a class-wide type, then we capture the result of the call into
7910 -- a temporary, with the benefit that, if the result's type does
7911 -- not need finalization, nothing will be finalized and, if it
7912 -- does, the temporary only will be finalized by means of a direct
7913 -- call to the Finalize primitive if the result's type is not a
7914 -- class-wide type; whereas, in both cases, the stand-alone object
7915 -- itself would be finalized by means of a dispatching call to the
7916 -- Deep_Finalize routine.
7918 if Nkind (Expr_Q) = N_Function_Call
7919 and then not Special_Ret_Obj
7920 and then not Is_Library_Level_Entity (Def_Id)
7921 and then Is_Class_Wide_Type (Typ)
7922 then
7923 Remove_Side_Effects (Expr_Q);
7924 end if;
7926 -- In most cases, we must check that the initial value meets any
7927 -- constraint imposed by the declared type. However, there is one
7928 -- very important exception to this rule. If the entity has an
7929 -- unconstrained nominal subtype, then it acquired its constraints
7930 -- from the expression in the first place, and not only does this
7931 -- mean that the constraint check is not needed, but an attempt to
7932 -- perform the constraint check can cause order of elaboration
7933 -- problems.
7935 if not Is_Constr_Subt_For_U_Nominal (Typ) then
7937 -- If this is an allocator for an aggregate that has been
7938 -- allocated in place, delay checks until assignments are
7939 -- made, because the discriminants are not initialized.
7941 if Nkind (Expr) = N_Allocator
7942 and then No_Initialization (Expr)
7943 then
7944 null;
7946 -- Otherwise apply a constraint check now if no prev error
7948 elsif Nkind (Expr) /= N_Error then
7949 Apply_Constraint_Check (Expr, Typ);
7951 -- Deal with possible range check
7953 if Do_Range_Check (Expr) then
7955 -- If assignment checks are suppressed, turn off flag
7957 if Suppress_Assignment_Checks (N) then
7958 Set_Do_Range_Check (Expr, False);
7960 -- Otherwise generate the range check
7962 else
7963 Generate_Range_Check
7964 (Expr, Typ, CE_Range_Check_Failed);
7965 end if;
7966 end if;
7967 end if;
7968 end if;
7970 -- For tagged types, when an init value is given, the tag has to
7971 -- be re-initialized separately in order to avoid the propagation
7972 -- of a wrong tag coming from a view conversion unless the type
7973 -- is class wide (in this case the tag comes from the init value).
7974 -- Suppress the tag assignment when not Tagged_Type_Expansion
7975 -- because tags are represented implicitly in objects. Ditto for
7976 -- types that are CPP_CLASS, and for initializations that are
7977 -- aggregates, because they have to have the right tag.
7979 -- The re-assignment of the tag has to be done even if the object
7980 -- is a constant. The assignment must be analyzed after the
7981 -- declaration. If an address clause follows, this is handled as
7982 -- part of the freeze actions for the object, otherwise insert
7983 -- tag assignment here.
7985 Tag_Assign := Make_Tag_Assignment (N);
7987 if Present (Tag_Assign) then
7988 if Present (Following_Address_Clause (N)) then
7989 Ensure_Freeze_Node (Def_Id);
7990 elsif not Special_Ret_Obj then
7991 Insert_Action_After (Init_After, Tag_Assign);
7992 end if;
7994 -- Handle C++ constructor calls. Note that we do not check that
7995 -- Typ is a tagged type since the equivalent Ada type of a C++
7996 -- class that has no virtual methods is an untagged limited
7997 -- record type.
7999 elsif Is_CPP_Constructor_Call (Expr) then
8000 declare
8001 Id_Ref : constant Node_Id := New_Occurrence_Of (Def_Id, Loc);
8003 begin
8004 -- The call to the initialization procedure does NOT freeze
8005 -- the object being initialized.
8007 Set_Must_Not_Freeze (Id_Ref);
8008 Set_Assignment_OK (Id_Ref);
8010 Insert_Actions_After (Init_After,
8011 Build_Initialization_Call (Loc, Id_Ref, Typ,
8012 Constructor_Ref => Expr));
8014 -- We remove here the original call to the constructor
8015 -- to avoid its management in the backend
8017 Set_Expression (N, Empty);
8018 return;
8019 end;
8021 -- Handle initialization of limited tagged types
8023 elsif Is_Tagged_Type (Typ)
8024 and then Is_Class_Wide_Type (Typ)
8025 and then Is_Limited_Record (Typ)
8026 and then not Is_Limited_Interface (Typ)
8027 then
8028 -- Given that the type is limited we cannot perform a copy. If
8029 -- Expr_Q is the reference to a variable we mark the variable
8030 -- as OK_To_Rename to expand this declaration into a renaming
8031 -- declaration (see below).
8033 if Is_Entity_Name (Expr_Q) then
8034 Set_OK_To_Rename (Entity (Expr_Q));
8036 -- If we cannot convert the expression into a renaming we must
8037 -- consider it an internal error because the backend does not
8038 -- have support to handle it. But avoid crashing on a raise
8039 -- expression or conditional expression.
8041 elsif Nkind (Original_Node (Expr_Q)) not in
8042 N_Raise_Expression | N_If_Expression | N_Case_Expression
8043 then
8044 raise Program_Error;
8045 end if;
8047 -- For discrete types, set the Is_Known_Valid flag if the
8048 -- initializing value is known to be valid. Only do this for
8049 -- source assignments, since otherwise we can end up turning
8050 -- on the known valid flag prematurely from inserted code.
8052 elsif Comes_From_Source (N)
8053 and then Is_Discrete_Type (Typ)
8054 and then Expr_Known_Valid (Expr)
8055 and then Safe_To_Capture_Value (N, Def_Id)
8056 then
8057 Set_Is_Known_Valid (Def_Id);
8059 -- For access types, set the Is_Known_Non_Null flag if the
8060 -- initializing value is known to be non-null. We can also
8061 -- set Can_Never_Be_Null if this is a constant.
8063 elsif Is_Access_Type (Typ) and then Known_Non_Null (Expr) then
8064 Set_Is_Known_Non_Null (Def_Id, True);
8066 if Constant_Present (N) then
8067 Set_Can_Never_Be_Null (Def_Id);
8068 end if;
8069 end if;
8071 -- If validity checking on copies, validate initial expression.
8072 -- But skip this if declaration is for a generic type, since it
8073 -- makes no sense to validate generic types. Not clear if this
8074 -- can happen for legal programs, but it definitely can arise
8075 -- from previous instantiation errors.
8077 if Validity_Checks_On
8078 and then Comes_From_Source (N)
8079 and then Validity_Check_Copies
8080 and then not Is_Generic_Type (Typ)
8081 then
8082 Ensure_Valid (Expr);
8084 if Safe_To_Capture_Value (N, Def_Id) then
8085 Set_Is_Known_Valid (Def_Id);
8086 end if;
8087 end if;
8089 -- Now determine whether we will use a renaming
8091 Rewrite_As_Renaming :=
8093 -- The declaration cannot be rewritten if it has got constraints
8095 Is_Entity_Name (Original_Node (Obj_Def))
8097 -- Nor if it is effectively an unconstrained declaration
8099 and then not (Is_Array_Type (Typ)
8100 and then Is_Constr_Subt_For_UN_Aliased (Typ))
8102 -- We may use a renaming if the initialization expression is a
8103 -- captured function call that meets a few conditions.
8105 and then
8106 (Is_Renamable_Function_Call (Expr_Q)
8108 -- Or else if it is a variable with OK_To_Rename set
8110 or else (OK_To_Rename_Ref (Expr_Q)
8111 and then not Special_Ret_Obj)
8113 -- Or else if it is a slice of such a variable
8115 or else (Nkind (Expr_Q) = N_Slice
8116 and then OK_To_Rename_Ref (Prefix (Expr_Q))
8117 and then not Special_Ret_Obj))
8119 -- If we have "X : S := ...;", and S is a constrained array
8120 -- subtype, then we cannot rename, because renamings ignore
8121 -- the constraints of S, so that would change the semantics
8122 -- (sliding would not occur on the initial value).
8124 and then not Nominal_Subtype_Is_Constrained_Array;
8126 -- If the type needs finalization and is not inherently limited,
8127 -- then the target is adjusted after the copy and attached to the
8128 -- finalization list. However, no adjustment is needed in the case
8129 -- where the object has been initialized by a call to a function
8130 -- returning on the primary stack (see Expand_Ctrl_Function_Call)
8131 -- since no copy occurred, given that the type is by-reference.
8132 -- Similarly, no adjustment is needed if we are going to rewrite
8133 -- the object declaration into a renaming declaration.
8135 if Needs_Finalization (Typ)
8136 and then not Is_Limited_View (Typ)
8137 and then Nkind (Expr_Q) /= N_Function_Call
8138 and then not Rewrite_As_Renaming
8139 then
8140 Adj_Call :=
8141 Make_Adjust_Call (
8142 Obj_Ref => New_Occurrence_Of (Def_Id, Loc),
8143 Typ => Base_Typ);
8145 if Present (Adj_Call) and then not Special_Ret_Obj then
8146 Insert_Action_After (Init_After, Adj_Call);
8147 end if;
8148 end if;
8149 end if;
8151 -- Cases where the back end cannot handle the initialization
8152 -- directly. In such cases, we expand an assignment that will
8153 -- be appropriately handled by Expand_N_Assignment_Statement.
8155 -- The exclusion of the unconstrained case is wrong, but for now it
8156 -- is too much trouble ???
8158 if (Is_Possibly_Unaligned_Slice (Expr)
8159 or else (Is_Possibly_Unaligned_Object (Expr)
8160 and then not Represented_As_Scalar (Etype (Expr))))
8161 and then not (Is_Array_Type (Etype (Expr))
8162 and then not Is_Constrained (Etype (Expr)))
8163 then
8164 declare
8165 Stat : constant Node_Id :=
8166 Make_Assignment_Statement (Loc,
8167 Name => New_Occurrence_Of (Def_Id, Loc),
8168 Expression => Relocate_Node (Expr));
8169 begin
8170 Set_Assignment_OK (Name (Stat));
8171 Set_No_Ctrl_Actions (Stat);
8172 Insert_Action_After (Init_After, Stat);
8173 Set_Expression (N, Empty);
8174 Set_No_Initialization (N);
8175 end;
8176 end if;
8177 end if;
8179 if Nkind (Obj_Def) = N_Access_Definition
8180 and then not Is_Local_Anonymous_Access (Typ)
8181 then
8182 -- An Ada 2012 stand-alone object of an anonymous access type
8184 declare
8185 Loc : constant Source_Ptr := Sloc (N);
8187 Level : constant Entity_Id :=
8188 Make_Defining_Identifier (Sloc (N),
8189 Chars =>
8190 New_External_Name (Chars (Def_Id), Suffix => "L"));
8192 Level_Decl : Node_Id;
8193 Level_Expr : Node_Id;
8195 begin
8196 Mutate_Ekind (Level, Ekind (Def_Id));
8197 Set_Etype (Level, Standard_Natural);
8198 Set_Scope (Level, Scope (Def_Id));
8200 -- Set accessibility level of null
8202 if No (Expr) then
8203 Level_Expr :=
8204 Make_Integer_Literal
8205 (Loc, Scope_Depth (Standard_Standard));
8207 -- When the expression of the object is a function which returns
8208 -- an anonymous access type the master of the call is the object
8209 -- being initialized instead of the type.
8211 elsif Nkind (Expr) = N_Function_Call
8212 and then Ekind (Etype (Name (Expr))) = E_Anonymous_Access_Type
8213 then
8214 Level_Expr := Accessibility_Level
8215 (Def_Id, Object_Decl_Level);
8217 -- General case
8219 else
8220 Level_Expr := Accessibility_Level (Expr, Dynamic_Level);
8221 end if;
8223 Level_Decl :=
8224 Make_Object_Declaration (Loc,
8225 Defining_Identifier => Level,
8226 Object_Definition =>
8227 New_Occurrence_Of (Standard_Natural, Loc),
8228 Expression => Level_Expr,
8229 Constant_Present => Constant_Present (N),
8230 Has_Init_Expression => True);
8232 Insert_Action_After (Init_After, Level_Decl);
8234 Set_Extra_Accessibility (Def_Id, Level);
8235 end;
8236 end if;
8238 -- If the object is default initialized and its type is subject to
8239 -- pragma Default_Initial_Condition, add a runtime check to verify
8240 -- the assumption of the pragma (SPARK RM 7.3.3). Generate:
8242 -- <Base_Typ>DIC (<Base_Typ> (Def_Id));
8244 -- Note that the check is generated for source objects only
8246 if Comes_From_Source (Def_Id)
8247 and then Has_DIC (Typ)
8248 and then Present (DIC_Procedure (Typ))
8249 and then not Has_Null_Body (DIC_Procedure (Typ))
8250 and then not Has_Init_Expression (N)
8251 and then No (Expr)
8252 and then not Is_Imported (Def_Id)
8253 then
8254 declare
8255 DIC_Call : constant Node_Id :=
8256 Build_DIC_Call
8257 (Loc, New_Occurrence_Of (Def_Id, Loc), Typ);
8258 begin
8259 if Present (Next_N) then
8260 Insert_Before_And_Analyze (Next_N, DIC_Call);
8262 -- The object declaration is the last node in a declarative or a
8263 -- statement list.
8265 else
8266 Append_To (List_Containing (N), DIC_Call);
8267 Analyze (DIC_Call);
8268 end if;
8269 end;
8270 end if;
8272 -- If this is the return object of a build-in-place function, locate the
8273 -- implicit BIPaccess parameter designating the caller-supplied return
8274 -- object and convert the declaration to a renaming of a dereference of
8275 -- this parameter. If the declaration includes an expression, add an
8276 -- assignment statement to ensure the return object gets initialized.
8278 -- Result : T [:= <expression>];
8280 -- is converted to
8282 -- Result : T renames BIPaccess.all;
8283 -- [Result := <expression>;]
8285 -- in the constrained case, or to
8287 -- type Txx is access all ...;
8288 -- Rxx : Txx := null;
8290 -- if BIPalloc = 1 then
8291 -- Rxx := BIPaccess;
8292 -- Rxx.all := <expression>;
8293 -- elsif BIPalloc = 2 then
8294 -- Rxx := new <expression-type>'(<expression>)[storage_pool =
8295 -- system__secondary_stack__ss_pool][procedure_to_call =
8296 -- system__secondary_stack__ss_allocate];
8297 -- elsif BIPalloc = 3 then
8298 -- Rxx := new <expression-type>'(<expression>)
8299 -- elsif BIPalloc = 4 then
8300 -- Pxx : system__storage_pools__root_storage_pool renames
8301 -- BIPstoragepool.all;
8302 -- Rxx := new <expression-type>'(<expression>)[storage_pool =
8303 -- Pxx][procedure_to_call =
8304 -- system__storage_pools__allocate_any];
8305 -- else
8306 -- [program_error "build in place mismatch"]
8307 -- end if;
8309 -- Result : T renames Rxx.all;
8311 -- in the unconstrained case.
8313 if Is_Build_In_Place_Return_Object (Def_Id) then
8314 declare
8315 Init_Stmt : Node_Id;
8316 Obj_Acc_Formal : Entity_Id;
8318 begin
8319 -- Retrieve the implicit access parameter passed by the caller
8321 Obj_Acc_Formal :=
8322 Build_In_Place_Formal (Func_Id, BIP_Object_Access);
8324 -- If the return object's declaration includes an expression
8325 -- and the declaration isn't marked as No_Initialization, then
8326 -- we need to generate an assignment to the object and insert
8327 -- it after the declaration before rewriting it as a renaming
8328 -- (otherwise we'll lose the initialization). The case where
8329 -- the result type is an interface (or class-wide interface)
8330 -- is also excluded because the context of the function call
8331 -- must be unconstrained, so the initialization will always
8332 -- be done as part of an allocator evaluation (storage pool
8333 -- or secondary stack), never to a constrained target object
8334 -- passed in by the caller. Besides the assignment being
8335 -- unneeded in this case, it avoids problems with trying to
8336 -- generate a dispatching assignment when the return expression
8337 -- is a nonlimited descendant of a limited interface (the
8338 -- interface has no assignment operation).
8340 if Present (Expr_Q)
8341 and then not Is_Delayed_Aggregate (Expr_Q)
8342 and then not No_Initialization (N)
8343 and then not Is_Interface (Typ)
8344 then
8345 if Is_Class_Wide_Type (Typ)
8346 and then not Is_Class_Wide_Type (Etype (Expr_Q))
8347 then
8348 Init_Stmt :=
8349 Make_Assignment_Statement (Loc,
8350 Name => New_Occurrence_Of (Def_Id, Loc),
8351 Expression =>
8352 Make_Type_Conversion (Loc,
8353 Subtype_Mark =>
8354 New_Occurrence_Of (Typ, Loc),
8355 Expression => New_Copy_Tree (Expr_Q)));
8357 else
8358 Init_Stmt :=
8359 Make_Assignment_Statement (Loc,
8360 Name => New_Occurrence_Of (Def_Id, Loc),
8361 Expression => New_Copy_Tree (Expr_Q));
8362 end if;
8364 Set_Assignment_OK (Name (Init_Stmt));
8365 Set_No_Ctrl_Actions (Init_Stmt);
8367 else
8368 Init_Stmt := Empty;
8369 end if;
8371 -- When the function's subtype is unconstrained, a run-time
8372 -- test may be needed to decide the form of allocation to use
8373 -- for the return object. The function has an implicit formal
8374 -- parameter indicating this. If the BIP_Alloc_Form formal has
8375 -- the value one, then the caller has passed access to an
8376 -- existing object for use as the return object. If the value
8377 -- is two, then the return object must be allocated on the
8378 -- secondary stack. If the value is three, then the return
8379 -- object must be allocated on the heap. Otherwise, the object
8380 -- must be allocated in a storage pool. We generate an if
8381 -- statement to test the BIP_Alloc_Form formal and initialize
8382 -- a local access value appropriately.
8384 if Needs_BIP_Alloc_Form (Func_Id) then
8385 declare
8386 Desig_Typ : constant Entity_Id :=
8387 (if Ekind (Typ) = E_Array_Subtype
8388 then Etype (Func_Id) else Typ);
8389 -- Ensure that the we use a fat pointer when allocating
8390 -- an unconstrained array on the heap. In this case the
8391 -- result object's type is a constrained array type even
8392 -- though the function's type is unconstrained.
8394 Obj_Alloc_Formal : constant Entity_Id :=
8395 Build_In_Place_Formal (Func_Id, BIP_Alloc_Form);
8396 Pool_Id : constant Entity_Id :=
8397 Make_Temporary (Loc, 'P');
8399 Acc_Typ : Entity_Id;
8400 Alloc_Obj_Decl : Node_Id;
8401 Alloc_Obj_Id : Entity_Id;
8402 Alloc_Stmt : Node_Id;
8403 Guard_Except : Node_Id;
8404 Heap_Allocator : Node_Id;
8405 Pool_Allocator : Node_Id;
8406 Pool_Decl : Node_Id;
8407 Ptr_Typ_Decl : Node_Id;
8408 SS_Allocator : Node_Id;
8410 begin
8411 -- Create an access type designating the function's
8412 -- result subtype.
8414 Acc_Typ := Make_Temporary (Loc, 'A');
8416 Ptr_Typ_Decl :=
8417 Make_Full_Type_Declaration (Loc,
8418 Defining_Identifier => Acc_Typ,
8419 Type_Definition =>
8420 Make_Access_To_Object_Definition (Loc,
8421 All_Present => True,
8422 Subtype_Indication =>
8423 New_Occurrence_Of (Desig_Typ, Loc)));
8425 Insert_Action (N, Ptr_Typ_Decl, Suppress => All_Checks);
8427 -- Create an access object that will be initialized to an
8428 -- access value denoting the return object, either coming
8429 -- from an implicit access value passed in by the caller
8430 -- or from the result of an allocator.
8432 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
8434 Alloc_Obj_Decl :=
8435 Make_Object_Declaration (Loc,
8436 Defining_Identifier => Alloc_Obj_Id,
8437 Object_Definition =>
8438 New_Occurrence_Of (Acc_Typ, Loc));
8440 Insert_Action (N, Alloc_Obj_Decl, Suppress => All_Checks);
8442 -- First create the Heap_Allocator
8444 Heap_Allocator := Make_Allocator_For_Return (Expr_Q);
8446 -- The Pool_Allocator is just like the Heap_Allocator,
8447 -- except we set Storage_Pool and Procedure_To_Call so
8448 -- it will use the user-defined storage pool.
8450 Pool_Allocator := Make_Allocator_For_Return (Expr_Q);
8452 -- Do not generate the renaming of the build-in-place
8453 -- pool parameter on ZFP because the parameter is not
8454 -- created in the first place.
8456 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
8457 Pool_Decl :=
8458 Make_Object_Renaming_Declaration (Loc,
8459 Defining_Identifier => Pool_Id,
8460 Subtype_Mark =>
8461 New_Occurrence_Of
8462 (RTE (RE_Root_Storage_Pool), Loc),
8463 Name =>
8464 Make_Explicit_Dereference (Loc,
8465 New_Occurrence_Of
8466 (Build_In_Place_Formal
8467 (Func_Id, BIP_Storage_Pool), Loc)));
8468 Set_Storage_Pool (Pool_Allocator, Pool_Id);
8469 Set_Procedure_To_Call
8470 (Pool_Allocator, RTE (RE_Allocate_Any));
8471 else
8472 Pool_Decl := Make_Null_Statement (Loc);
8473 end if;
8475 -- If the No_Allocators restriction is active, then only
8476 -- an allocator for secondary stack allocation is needed.
8477 -- It's OK for such allocators to have Comes_From_Source
8478 -- set to False, because gigi knows not to flag them as
8479 -- being a violation of No_Implicit_Heap_Allocations.
8481 if Restriction_Active (No_Allocators) then
8482 SS_Allocator := Heap_Allocator;
8483 Heap_Allocator := Make_Null (Loc);
8484 Pool_Allocator := Make_Null (Loc);
8486 -- Otherwise the heap and pool allocators may be needed,
8487 -- so we make another allocator for secondary stack
8488 -- allocation.
8490 else
8491 SS_Allocator := Make_Allocator_For_Return (Expr_Q);
8493 -- The heap and pool allocators are marked as
8494 -- Comes_From_Source since they correspond to an
8495 -- explicit user-written allocator (that is, it will
8496 -- only be executed on behalf of callers that call the
8497 -- function as initialization for such an allocator).
8498 -- Prevents errors when No_Implicit_Heap_Allocations
8499 -- is in force.
8501 Set_Comes_From_Source (Heap_Allocator, True);
8502 Set_Comes_From_Source (Pool_Allocator, True);
8503 end if;
8505 -- The allocator is returned on the secondary stack
8507 Check_Restriction (No_Secondary_Stack, N);
8508 Set_Storage_Pool (SS_Allocator, RTE (RE_SS_Pool));
8509 Set_Procedure_To_Call
8510 (SS_Allocator, RTE (RE_SS_Allocate));
8512 -- The allocator is returned on the secondary stack,
8513 -- so indicate that the function return, as well as
8514 -- all blocks that encloses the allocator, must not
8515 -- release it. The flags must be set now because
8516 -- the decision to use the secondary stack is done
8517 -- very late in the course of expanding the return
8518 -- statement, past the point where these flags are
8519 -- normally set.
8521 Set_Uses_Sec_Stack (Func_Id);
8522 Set_Uses_Sec_Stack (Scope (Def_Id));
8523 Set_Sec_Stack_Needed_For_Return (Scope (Def_Id));
8525 -- Guard against poor expansion on the caller side by
8526 -- using a raise statement to catch out-of-range values
8527 -- of formal parameter BIP_Alloc_Form.
8529 if Exceptions_OK then
8530 Guard_Except :=
8531 Make_Raise_Program_Error (Loc,
8532 Reason => PE_Build_In_Place_Mismatch);
8533 else
8534 Guard_Except := Make_Null_Statement (Loc);
8535 end if;
8537 -- Create an if statement to test the BIP_Alloc_Form
8538 -- formal and initialize the access object to either the
8539 -- BIP_Object_Access formal (BIP_Alloc_Form =
8540 -- Caller_Allocation), the result of allocating the
8541 -- object in the secondary stack (BIP_Alloc_Form =
8542 -- Secondary_Stack), or else an allocator to create the
8543 -- return object in the heap or user-defined pool
8544 -- (BIP_Alloc_Form = Global_Heap or User_Storage_Pool).
8546 -- ??? An unchecked type conversion must be made in the
8547 -- case of assigning the access object formal to the
8548 -- local access object, because a normal conversion would
8549 -- be illegal in some cases (such as converting access-
8550 -- to-unconstrained to access-to-constrained), but the
8551 -- the unchecked conversion will presumably fail to work
8552 -- right in just such cases. It's not clear at all how to
8553 -- handle this.
8555 Alloc_Stmt :=
8556 Make_If_Statement (Loc,
8557 Condition =>
8558 Make_Op_Eq (Loc,
8559 Left_Opnd =>
8560 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
8561 Right_Opnd =>
8562 Make_Integer_Literal (Loc,
8563 UI_From_Int (BIP_Allocation_Form'Pos
8564 (Caller_Allocation)))),
8566 Then_Statements => New_List (
8567 Make_Assignment_Statement (Loc,
8568 Name =>
8569 New_Occurrence_Of (Alloc_Obj_Id, Loc),
8570 Expression =>
8571 Unchecked_Convert_To
8572 (Acc_Typ,
8573 New_Occurrence_Of (Obj_Acc_Formal, Loc)))),
8575 Elsif_Parts => New_List (
8576 Make_Elsif_Part (Loc,
8577 Condition =>
8578 Make_Op_Eq (Loc,
8579 Left_Opnd =>
8580 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
8581 Right_Opnd =>
8582 Make_Integer_Literal (Loc,
8583 UI_From_Int (BIP_Allocation_Form'Pos
8584 (Secondary_Stack)))),
8586 Then_Statements => New_List (
8587 Make_Assignment_Statement (Loc,
8588 Name =>
8589 New_Occurrence_Of (Alloc_Obj_Id, Loc),
8590 Expression => SS_Allocator))),
8592 Make_Elsif_Part (Loc,
8593 Condition =>
8594 Make_Op_Eq (Loc,
8595 Left_Opnd =>
8596 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
8597 Right_Opnd =>
8598 Make_Integer_Literal (Loc,
8599 UI_From_Int (BIP_Allocation_Form'Pos
8600 (Global_Heap)))),
8602 Then_Statements => New_List (
8603 Build_Heap_Or_Pool_Allocator
8604 (Temp_Id => Alloc_Obj_Id,
8605 Temp_Typ => Acc_Typ,
8606 Ret_Typ => Desig_Typ,
8607 Alloc_Expr => Heap_Allocator))),
8609 -- ??? If all is well, we can put the following
8610 -- 'elsif' in the 'else', but this is a useful
8611 -- self-check in case caller and callee don't agree
8612 -- on whether BIPAlloc and so on should be passed.
8614 Make_Elsif_Part (Loc,
8615 Condition =>
8616 Make_Op_Eq (Loc,
8617 Left_Opnd =>
8618 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
8619 Right_Opnd =>
8620 Make_Integer_Literal (Loc,
8621 UI_From_Int (BIP_Allocation_Form'Pos
8622 (User_Storage_Pool)))),
8624 Then_Statements => New_List (
8625 Pool_Decl,
8626 Build_Heap_Or_Pool_Allocator
8627 (Temp_Id => Alloc_Obj_Id,
8628 Temp_Typ => Acc_Typ,
8629 Ret_Typ => Desig_Typ,
8630 Alloc_Expr => Pool_Allocator)))),
8632 -- Raise Program_Error if it's none of the above;
8633 -- this is a compiler bug.
8635 Else_Statements => New_List (Guard_Except));
8637 -- If a separate initialization assignment was created
8638 -- earlier, append that following the assignment of the
8639 -- implicit access formal to the access object, to ensure
8640 -- that the return object is initialized in that case. In
8641 -- this situation, the target of the assignment must be
8642 -- rewritten to denote a dereference of the access to the
8643 -- return object passed in by the caller.
8645 if Present (Init_Stmt) then
8646 Set_Name (Init_Stmt,
8647 Make_Explicit_Dereference (Loc,
8648 Prefix => New_Occurrence_Of (Alloc_Obj_Id, Loc)));
8649 Set_Assignment_OK (Name (Init_Stmt));
8651 Append_To (Then_Statements (Alloc_Stmt), Init_Stmt);
8652 Init_Stmt := Empty;
8653 end if;
8655 Insert_Action (N, Alloc_Stmt, Suppress => All_Checks);
8657 -- From now on, the type of the return object is the
8658 -- designated type.
8660 if Desig_Typ /= Typ then
8661 Set_Etype (Def_Id, Desig_Typ);
8662 Set_Actual_Subtype (Def_Id, Typ);
8663 end if;
8665 -- Remember the local access object for use in the
8666 -- dereference of the renaming created below.
8668 Obj_Acc_Formal := Alloc_Obj_Id;
8669 end;
8671 -- When the function's type is unconstrained and a run-time test
8672 -- is not needed, we nevertheless need to build the return using
8673 -- the return object's type.
8675 elsif not Is_Constrained (Underlying_Type (Etype (Func_Id))) then
8676 declare
8677 Acc_Typ : Entity_Id;
8678 Alloc_Obj_Decl : Node_Id;
8679 Alloc_Obj_Id : Entity_Id;
8680 Ptr_Typ_Decl : Node_Id;
8682 begin
8683 -- Create an access type designating the function's
8684 -- result subtype.
8686 Acc_Typ := Make_Temporary (Loc, 'A');
8688 Ptr_Typ_Decl :=
8689 Make_Full_Type_Declaration (Loc,
8690 Defining_Identifier => Acc_Typ,
8691 Type_Definition =>
8692 Make_Access_To_Object_Definition (Loc,
8693 All_Present => True,
8694 Subtype_Indication =>
8695 New_Occurrence_Of (Typ, Loc)));
8697 Insert_Action (N, Ptr_Typ_Decl, Suppress => All_Checks);
8699 -- Create an access object initialized to the conversion
8700 -- of the implicit access value passed in by the caller.
8702 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
8704 -- See the ??? comment a few lines above about the use of
8705 -- an unchecked conversion here.
8707 Alloc_Obj_Decl :=
8708 Make_Object_Declaration (Loc,
8709 Defining_Identifier => Alloc_Obj_Id,
8710 Constant_Present => True,
8711 Object_Definition =>
8712 New_Occurrence_Of (Acc_Typ, Loc),
8713 Expression =>
8714 Unchecked_Convert_To
8715 (Acc_Typ, New_Occurrence_Of (Obj_Acc_Formal, Loc)));
8717 Insert_Action (N, Alloc_Obj_Decl, Suppress => All_Checks);
8719 -- Remember the local access object for use in the
8720 -- dereference of the renaming created below.
8722 Obj_Acc_Formal := Alloc_Obj_Id;
8723 end;
8724 end if;
8726 -- Initialize the object now that it has got its final subtype,
8727 -- but before rewriting it as a renaming.
8729 Initialize_Return_Object
8730 (Tag_Assign, Adj_Call, Expr_Q, Init_Stmt, Init_After);
8732 -- Replace the return object declaration with a renaming of a
8733 -- dereference of the access value designating the return object.
8735 Expr_Q :=
8736 Make_Explicit_Dereference (Loc,
8737 Prefix => New_Occurrence_Of (Obj_Acc_Formal, Loc));
8738 Set_Etype (Expr_Q, Etype (Def_Id));
8740 Rewrite_As_Renaming := True;
8741 end;
8743 -- If we can rename the initialization expression, we need to make sure
8744 -- that we use the proper type in the case of a return object that lives
8745 -- on the secondary stack (see other cases below for a similar handling)
8746 -- and that the tag is assigned in the case of any return object.
8748 elsif Rewrite_As_Renaming then
8749 if Special_Ret_Obj then
8750 declare
8751 Desig_Typ : constant Entity_Id :=
8752 (if Ekind (Typ) = E_Array_Subtype
8753 then Etype (Func_Id) else Typ);
8755 begin
8756 -- From now on, the type of the return object is the
8757 -- designated type.
8759 if Desig_Typ /= Typ then
8760 Set_Etype (Def_Id, Desig_Typ);
8761 Set_Actual_Subtype (Def_Id, Typ);
8762 end if;
8764 if Present (Tag_Assign) then
8765 Insert_Action_After (Init_After, Tag_Assign);
8766 end if;
8768 -- Ada 2005 (AI95-344): If the result type is class-wide,
8769 -- insert a check that the level of the return expression's
8770 -- underlying type is not deeper than the level of the master
8771 -- enclosing the function.
8773 -- AI12-043: The check is made immediately after the return
8774 -- object is created.
8776 if Is_Class_Wide_Type (Etype (Func_Id)) then
8777 Apply_CW_Accessibility_Check (Expr_Q, Func_Id);
8778 end if;
8779 end;
8780 end if;
8782 -- If this is the return object of a function returning on the secondary
8783 -- stack, convert the declaration to a renaming of the dereference of ah
8784 -- allocator for the secondary stack.
8786 -- Result : T [:= <expression>];
8788 -- is converted to
8790 -- type Txx is access all ...;
8791 -- Rxx : constant Txx :=
8792 -- new <expression-type>['(<expression>)][storage_pool =
8793 -- system__secondary_stack__ss_pool][procedure_to_call =
8794 -- system__secondary_stack__ss_allocate];
8796 -- Result : T renames Rxx.all;
8798 elsif Is_Secondary_Stack_Return_Object (Def_Id) then
8799 declare
8800 Desig_Typ : constant Entity_Id :=
8801 (if Ekind (Typ) = E_Array_Subtype
8802 then Etype (Func_Id) else Typ);
8803 -- Ensure that the we use a fat pointer when allocating
8804 -- an unconstrained array on the heap. In this case the
8805 -- result object's type is a constrained array type even
8806 -- though the function's type is unconstrained.
8808 Acc_Typ : Entity_Id;
8809 Alloc_Obj_Decl : Node_Id;
8810 Alloc_Obj_Id : Entity_Id;
8811 Ptr_Type_Decl : Node_Id;
8813 begin
8814 -- Create an access type designating the function's
8815 -- result subtype.
8817 Acc_Typ := Make_Temporary (Loc, 'A');
8819 Ptr_Type_Decl :=
8820 Make_Full_Type_Declaration (Loc,
8821 Defining_Identifier => Acc_Typ,
8822 Type_Definition =>
8823 Make_Access_To_Object_Definition (Loc,
8824 All_Present => True,
8825 Subtype_Indication =>
8826 New_Occurrence_Of (Desig_Typ, Loc)));
8828 Insert_Action (N, Ptr_Type_Decl, Suppress => All_Checks);
8830 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
8832 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
8834 Alloc_Obj_Decl :=
8835 Make_Object_Declaration (Loc,
8836 Defining_Identifier => Alloc_Obj_Id,
8837 Constant_Present => True,
8838 Object_Definition =>
8839 New_Occurrence_Of (Acc_Typ, Loc),
8840 Expression => Make_Allocator_For_Return (Expr_Q));
8842 Insert_Action (N, Alloc_Obj_Decl, Suppress => All_Checks);
8844 Set_Uses_Sec_Stack (Func_Id);
8845 Set_Uses_Sec_Stack (Scope (Def_Id));
8846 Set_Sec_Stack_Needed_For_Return (Scope (Def_Id));
8848 -- From now on, the type of the return object is the
8849 -- designated type.
8851 if Desig_Typ /= Typ then
8852 Set_Etype (Def_Id, Desig_Typ);
8853 Set_Actual_Subtype (Def_Id, Typ);
8854 end if;
8856 -- Initialize the object now that it has got its final subtype,
8857 -- but before rewriting it as a renaming.
8859 Initialize_Return_Object
8860 (Tag_Assign, Adj_Call, Expr_Q, Empty, Init_After);
8862 -- Replace the return object declaration with a renaming of a
8863 -- dereference of the access value designating the return object.
8865 Expr_Q :=
8866 Make_Explicit_Dereference (Loc,
8867 Prefix => New_Occurrence_Of (Alloc_Obj_Id, Loc));
8868 Set_Etype (Expr_Q, Etype (Def_Id));
8870 Rewrite_As_Renaming := True;
8871 end;
8873 -- If this is the return object of a function returning a by-reference
8874 -- type, convert the declaration to a renaming of the dereference of ah
8875 -- allocator for the return stack.
8877 -- Result : T [:= <expression>];
8879 -- is converted to
8881 -- type Txx is access all ...;
8882 -- Rxx : constant Txx :=
8883 -- new <expression-type>['(<expression>)][storage_pool =
8884 -- system__return_stack__rs_pool][procedure_to_call =
8885 -- system__return_stack__rs_allocate];
8887 -- Result : T renames Rxx.all;
8889 elsif Back_End_Return_Slot
8890 and then Is_By_Reference_Return_Object (Def_Id)
8891 then
8892 declare
8893 Acc_Typ : Entity_Id;
8894 Alloc_Obj_Decl : Node_Id;
8895 Alloc_Obj_Id : Entity_Id;
8896 Ptr_Type_Decl : Node_Id;
8898 begin
8899 -- Create an access type designating the function's
8900 -- result subtype.
8902 Acc_Typ := Make_Temporary (Loc, 'A');
8904 Ptr_Type_Decl :=
8905 Make_Full_Type_Declaration (Loc,
8906 Defining_Identifier => Acc_Typ,
8907 Type_Definition =>
8908 Make_Access_To_Object_Definition (Loc,
8909 All_Present => True,
8910 Subtype_Indication =>
8911 New_Occurrence_Of (Typ, Loc)));
8913 Insert_Action (N, Ptr_Type_Decl, Suppress => All_Checks);
8915 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_RS_Pool));
8917 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
8919 Alloc_Obj_Decl :=
8920 Make_Object_Declaration (Loc,
8921 Defining_Identifier => Alloc_Obj_Id,
8922 Constant_Present => True,
8923 Object_Definition =>
8924 New_Occurrence_Of (Acc_Typ, Loc),
8925 Expression => Make_Allocator_For_Return (Expr_Q));
8927 Insert_Action (N, Alloc_Obj_Decl, Suppress => All_Checks);
8929 -- Initialize the object now that it has got its final subtype,
8930 -- but before rewriting it as a renaming.
8932 Initialize_Return_Object
8933 (Tag_Assign, Adj_Call, Expr_Q, Empty, Init_After);
8935 -- Replace the return object declaration with a renaming of a
8936 -- dereference of the access value designating the return object.
8938 Expr_Q :=
8939 Make_Explicit_Dereference (Loc,
8940 Prefix => New_Occurrence_Of (Alloc_Obj_Id, Loc));
8941 Set_Etype (Expr_Q, Etype (Def_Id));
8943 Rewrite_As_Renaming := True;
8944 end;
8945 end if;
8947 -- Final transformation - turn the object declaration into a renaming
8948 -- if appropriate. If this is the completion of a deferred constant
8949 -- declaration, then this transformation generates what would be
8950 -- illegal code if written by hand, but that's OK.
8952 if Rewrite_As_Renaming then
8953 Rewrite (N,
8954 Make_Object_Renaming_Declaration (Loc,
8955 Defining_Identifier => Def_Id,
8956 Subtype_Mark => New_Occurrence_Of (Etype (Def_Id), Loc),
8957 Name => Expr_Q));
8959 -- We do not analyze this renaming declaration, because all its
8960 -- components have already been analyzed, and if we were to go
8961 -- ahead and analyze it, we would in effect be trying to generate
8962 -- another declaration of X, which won't do.
8964 Set_Renamed_Object (Def_Id, Expr_Q);
8965 Set_Analyzed (N);
8967 -- We do need to deal with debug issues for this renaming
8969 -- First, if entity comes from source, then mark it as needing
8970 -- debug information, even though it is defined by a generated
8971 -- renaming that does not come from source.
8973 Set_Debug_Info_Defining_Id (N);
8975 -- Now call the routine to generate debug info for the renaming
8977 Insert_Action (N, Debug_Renaming_Declaration (N));
8978 end if;
8980 -- Exception on library entity not available
8982 exception
8983 when RE_Not_Available =>
8984 return;
8985 end Expand_N_Object_Declaration;
8987 ---------------------------------
8988 -- Expand_N_Subtype_Indication --
8989 ---------------------------------
8991 -- Add a check on the range of the subtype and deal with validity checking
8993 procedure Expand_N_Subtype_Indication (N : Node_Id) is
8994 Ran : constant Node_Id := Range_Expression (Constraint (N));
8995 Typ : constant Entity_Id := Entity (Subtype_Mark (N));
8997 begin
8998 if Nkind (Constraint (N)) = N_Range_Constraint then
8999 Validity_Check_Range (Range_Expression (Constraint (N)));
9000 end if;
9002 -- Do not duplicate the work of Process_Range_Expr_In_Decl in Sem_Ch3
9004 if Nkind (Parent (N)) in N_Constrained_Array_Definition | N_Slice
9005 and then Nkind (Parent (Parent (N))) not in
9006 N_Full_Type_Declaration | N_Object_Declaration
9007 then
9008 Apply_Range_Check (Ran, Typ);
9009 end if;
9010 end Expand_N_Subtype_Indication;
9012 ---------------------------
9013 -- Expand_N_Variant_Part --
9014 ---------------------------
9016 -- Note: this procedure no longer has any effect. It used to be that we
9017 -- would replace the choices in the last variant by a when others, and
9018 -- also expanded static predicates in variant choices here, but both of
9019 -- those activities were being done too early, since we can't check the
9020 -- choices until the statically predicated subtypes are frozen, which can
9021 -- happen as late as the free point of the record, and we can't change the
9022 -- last choice to an others before checking the choices, which is now done
9023 -- at the freeze point of the record.
9025 procedure Expand_N_Variant_Part (N : Node_Id) is
9026 begin
9027 null;
9028 end Expand_N_Variant_Part;
9030 ---------------------------------
9031 -- Expand_Previous_Access_Type --
9032 ---------------------------------
9034 procedure Expand_Previous_Access_Type (Def_Id : Entity_Id) is
9035 Ptr_Typ : Entity_Id;
9037 begin
9038 -- Find all access types in the current scope whose designated type is
9039 -- Def_Id and build master renamings for them.
9041 Ptr_Typ := First_Entity (Current_Scope);
9042 while Present (Ptr_Typ) loop
9043 if Is_Access_Type (Ptr_Typ)
9044 and then Designated_Type (Ptr_Typ) = Def_Id
9045 and then No (Master_Id (Ptr_Typ))
9046 then
9047 -- Ensure that the designated type has a master
9049 Build_Master_Entity (Def_Id);
9051 -- Private and incomplete types complicate the insertion of master
9052 -- renamings because the access type may precede the full view of
9053 -- the designated type. For this reason, the master renamings are
9054 -- inserted relative to the designated type.
9056 Build_Master_Renaming (Ptr_Typ, Ins_Nod => Parent (Def_Id));
9057 end if;
9059 Next_Entity (Ptr_Typ);
9060 end loop;
9061 end Expand_Previous_Access_Type;
9063 -----------------------------
9064 -- Expand_Record_Extension --
9065 -----------------------------
9067 -- Add a field _parent at the beginning of the record extension. This is
9068 -- used to implement inheritance. Here are some examples of expansion:
9070 -- 1. no discriminants
9071 -- type T2 is new T1 with null record;
9072 -- gives
9073 -- type T2 is new T1 with record
9074 -- _Parent : T1;
9075 -- end record;
9077 -- 2. renamed discriminants
9078 -- type T2 (B, C : Int) is new T1 (A => B) with record
9079 -- _Parent : T1 (A => B);
9080 -- D : Int;
9081 -- end;
9083 -- 3. inherited discriminants
9084 -- type T2 is new T1 with record -- discriminant A inherited
9085 -- _Parent : T1 (A);
9086 -- D : Int;
9087 -- end;
9089 procedure Expand_Record_Extension (T : Entity_Id; Def : Node_Id) is
9090 Indic : constant Node_Id := Subtype_Indication (Def);
9091 Loc : constant Source_Ptr := Sloc (Def);
9092 Rec_Ext_Part : Node_Id := Record_Extension_Part (Def);
9093 Par_Subtype : Entity_Id;
9094 Comp_List : Node_Id;
9095 Comp_Decl : Node_Id;
9096 Parent_N : Node_Id;
9097 D : Entity_Id;
9098 List_Constr : constant List_Id := New_List;
9100 begin
9101 -- Expand_Record_Extension is called directly from the semantics, so
9102 -- we must check to see whether expansion is active before proceeding,
9103 -- because this affects the visibility of selected components in bodies
9104 -- of instances. Within a generic we still need to set Parent_Subtype
9105 -- link because the visibility of inherited components will have to be
9106 -- verified in subsequent instances.
9108 if not Expander_Active then
9109 if Inside_A_Generic and then Ekind (T) = E_Record_Type then
9110 Set_Parent_Subtype (T, Etype (T));
9111 end if;
9112 return;
9113 end if;
9115 -- This may be a derivation of an untagged private type whose full
9116 -- view is tagged, in which case the Derived_Type_Definition has no
9117 -- extension part. Build an empty one now.
9119 if No (Rec_Ext_Part) then
9120 Rec_Ext_Part :=
9121 Make_Record_Definition (Loc,
9122 End_Label => Empty,
9123 Component_List => Empty,
9124 Null_Present => True);
9126 Set_Record_Extension_Part (Def, Rec_Ext_Part);
9127 Mark_Rewrite_Insertion (Rec_Ext_Part);
9128 end if;
9130 Comp_List := Component_List (Rec_Ext_Part);
9132 Parent_N := Make_Defining_Identifier (Loc, Name_uParent);
9134 -- If the derived type inherits its discriminants the type of the
9135 -- _parent field must be constrained by the inherited discriminants
9137 if Has_Discriminants (T)
9138 and then Nkind (Indic) /= N_Subtype_Indication
9139 and then not Is_Constrained (Entity (Indic))
9140 then
9141 D := First_Discriminant (T);
9142 while Present (D) loop
9143 Append_To (List_Constr, New_Occurrence_Of (D, Loc));
9144 Next_Discriminant (D);
9145 end loop;
9147 Par_Subtype :=
9148 Process_Subtype (
9149 Make_Subtype_Indication (Loc,
9150 Subtype_Mark => New_Occurrence_Of (Entity (Indic), Loc),
9151 Constraint =>
9152 Make_Index_Or_Discriminant_Constraint (Loc,
9153 Constraints => List_Constr)),
9154 Def);
9156 -- Otherwise the original subtype_indication is just what is needed
9158 else
9159 Par_Subtype := Process_Subtype (New_Copy_Tree (Indic), Def);
9160 end if;
9162 Set_Parent_Subtype (T, Par_Subtype);
9164 Comp_Decl :=
9165 Make_Component_Declaration (Loc,
9166 Defining_Identifier => Parent_N,
9167 Component_Definition =>
9168 Make_Component_Definition (Loc,
9169 Aliased_Present => False,
9170 Subtype_Indication => New_Occurrence_Of (Par_Subtype, Loc)));
9172 if Null_Present (Rec_Ext_Part) then
9173 Set_Component_List (Rec_Ext_Part,
9174 Make_Component_List (Loc,
9175 Component_Items => New_List (Comp_Decl),
9176 Variant_Part => Empty,
9177 Null_Present => False));
9178 Set_Null_Present (Rec_Ext_Part, False);
9180 elsif Null_Present (Comp_List)
9181 or else Is_Empty_List (Component_Items (Comp_List))
9182 then
9183 Set_Component_Items (Comp_List, New_List (Comp_Decl));
9184 Set_Null_Present (Comp_List, False);
9186 else
9187 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
9188 end if;
9190 Analyze (Comp_Decl);
9191 end Expand_Record_Extension;
9193 ------------------------
9194 -- Expand_Tagged_Root --
9195 ------------------------
9197 procedure Expand_Tagged_Root (T : Entity_Id) is
9198 Def : constant Node_Id := Type_Definition (Parent (T));
9199 Comp_List : Node_Id;
9200 Comp_Decl : Node_Id;
9201 Sloc_N : Source_Ptr;
9203 begin
9204 if Null_Present (Def) then
9205 Set_Component_List (Def,
9206 Make_Component_List (Sloc (Def),
9207 Component_Items => Empty_List,
9208 Variant_Part => Empty,
9209 Null_Present => True));
9210 end if;
9212 Comp_List := Component_List (Def);
9214 if Null_Present (Comp_List)
9215 or else Is_Empty_List (Component_Items (Comp_List))
9216 then
9217 Sloc_N := Sloc (Comp_List);
9218 else
9219 Sloc_N := Sloc (First (Component_Items (Comp_List)));
9220 end if;
9222 Comp_Decl :=
9223 Make_Component_Declaration (Sloc_N,
9224 Defining_Identifier => First_Tag_Component (T),
9225 Component_Definition =>
9226 Make_Component_Definition (Sloc_N,
9227 Aliased_Present => False,
9228 Subtype_Indication => New_Occurrence_Of (RTE (RE_Tag), Sloc_N)));
9230 if Null_Present (Comp_List)
9231 or else Is_Empty_List (Component_Items (Comp_List))
9232 then
9233 Set_Component_Items (Comp_List, New_List (Comp_Decl));
9234 Set_Null_Present (Comp_List, False);
9236 else
9237 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
9238 end if;
9240 -- We don't Analyze the whole expansion because the tag component has
9241 -- already been analyzed previously. Here we just insure that the tree
9242 -- is coherent with the semantic decoration
9244 Find_Type (Subtype_Indication (Component_Definition (Comp_Decl)));
9246 exception
9247 when RE_Not_Available =>
9248 return;
9249 end Expand_Tagged_Root;
9251 ------------------------------
9252 -- Freeze_Stream_Operations --
9253 ------------------------------
9255 procedure Freeze_Stream_Operations (N : Node_Id; Typ : Entity_Id) is
9256 Names : constant array (1 .. 4) of TSS_Name_Type :=
9257 (TSS_Stream_Input,
9258 TSS_Stream_Output,
9259 TSS_Stream_Read,
9260 TSS_Stream_Write);
9261 Stream_Op : Entity_Id;
9263 begin
9264 -- Primitive operations of tagged types are frozen when the dispatch
9265 -- table is constructed.
9267 if not Comes_From_Source (Typ) or else Is_Tagged_Type (Typ) then
9268 return;
9269 end if;
9271 for J in Names'Range loop
9272 Stream_Op := TSS (Typ, Names (J));
9274 if Present (Stream_Op)
9275 and then Is_Subprogram (Stream_Op)
9276 and then Nkind (Unit_Declaration_Node (Stream_Op)) =
9277 N_Subprogram_Declaration
9278 and then not Is_Frozen (Stream_Op)
9279 then
9280 Append_Freeze_Actions (Typ, Freeze_Entity (Stream_Op, N));
9281 end if;
9282 end loop;
9283 end Freeze_Stream_Operations;
9285 -----------------
9286 -- Freeze_Type --
9287 -----------------
9289 -- Full type declarations are expanded at the point at which the type is
9290 -- frozen. The formal N is the Freeze_Node for the type. Any statements or
9291 -- declarations generated by the freezing (e.g. the procedure generated
9292 -- for initialization) are chained in the Actions field list of the freeze
9293 -- node using Append_Freeze_Actions.
9295 -- WARNING: This routine manages Ghost regions. Return statements must be
9296 -- replaced by gotos which jump to the end of the routine and restore the
9297 -- Ghost mode.
9299 function Freeze_Type (N : Node_Id) return Boolean is
9300 procedure Process_RACW_Types (Typ : Entity_Id);
9301 -- Validate and generate stubs for all RACW types associated with type
9302 -- Typ.
9304 procedure Process_Pending_Access_Types (Typ : Entity_Id);
9305 -- Associate type Typ's Finalize_Address primitive with the finalization
9306 -- masters of pending access-to-Typ types.
9308 ------------------------
9309 -- Process_RACW_Types --
9310 ------------------------
9312 procedure Process_RACW_Types (Typ : Entity_Id) is
9313 List : constant Elist_Id := Access_Types_To_Process (N);
9314 E : Elmt_Id;
9315 Seen : Boolean := False;
9317 begin
9318 if Present (List) then
9319 E := First_Elmt (List);
9320 while Present (E) loop
9321 if Is_Remote_Access_To_Class_Wide_Type (Node (E)) then
9322 Validate_RACW_Primitives (Node (E));
9323 Seen := True;
9324 end if;
9326 Next_Elmt (E);
9327 end loop;
9328 end if;
9330 -- If there are RACWs designating this type, make stubs now
9332 if Seen then
9333 Remote_Types_Tagged_Full_View_Encountered (Typ);
9334 end if;
9335 end Process_RACW_Types;
9337 ----------------------------------
9338 -- Process_Pending_Access_Types --
9339 ----------------------------------
9341 procedure Process_Pending_Access_Types (Typ : Entity_Id) is
9342 E : Elmt_Id;
9344 begin
9345 -- Finalize_Address is not generated in CodePeer mode because the
9346 -- body contains address arithmetic. This processing is disabled.
9348 if CodePeer_Mode then
9349 null;
9351 -- Certain itypes are generated for contexts that cannot allocate
9352 -- objects and should not set primitive Finalize_Address.
9354 elsif Is_Itype (Typ)
9355 and then Nkind (Associated_Node_For_Itype (Typ)) =
9356 N_Explicit_Dereference
9357 then
9358 null;
9360 -- When an access type is declared after the incomplete view of a
9361 -- Taft-amendment type, the access type is considered pending in
9362 -- case the full view of the Taft-amendment type is controlled. If
9363 -- this is indeed the case, associate the Finalize_Address routine
9364 -- of the full view with the finalization masters of all pending
9365 -- access types. This scenario applies to anonymous access types as
9366 -- well. But the Finalize_Address routine is missing if the type is
9367 -- class-wide and we are under restriction No_Dispatching_Calls, see
9368 -- Expand_Freeze_Class_Wide_Type above for the rationale.
9370 elsif Needs_Finalization (Typ)
9371 and then (not Is_Class_Wide_Type (Typ)
9372 or else not Restriction_Active (No_Dispatching_Calls))
9373 and then Present (Pending_Access_Types (Typ))
9374 then
9375 E := First_Elmt (Pending_Access_Types (Typ));
9376 while Present (E) loop
9378 -- Generate:
9379 -- Set_Finalize_Address
9380 -- (Ptr_Typ, <Typ>FD'Unrestricted_Access);
9382 Append_Freeze_Action (Typ,
9383 Make_Set_Finalize_Address_Call
9384 (Loc => Sloc (N),
9385 Ptr_Typ => Node (E)));
9387 Next_Elmt (E);
9388 end loop;
9389 end if;
9390 end Process_Pending_Access_Types;
9392 -- Local variables
9394 Def_Id : constant Entity_Id := Entity (N);
9396 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
9397 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
9398 -- Save the Ghost-related attributes to restore on exit
9400 Result : Boolean := False;
9402 -- Start of processing for Freeze_Type
9404 begin
9405 -- The type being frozen may be subject to pragma Ghost. Set the mode
9406 -- now to ensure that any nodes generated during freezing are properly
9407 -- marked as Ghost.
9409 Set_Ghost_Mode (Def_Id);
9411 -- Process any remote access-to-class-wide types designating the type
9412 -- being frozen.
9414 Process_RACW_Types (Def_Id);
9416 -- Freeze processing for record types
9418 if Is_Record_Type (Def_Id) then
9419 if Ekind (Def_Id) = E_Record_Type then
9420 Expand_Freeze_Record_Type (N);
9421 elsif Is_Class_Wide_Type (Def_Id) then
9422 Expand_Freeze_Class_Wide_Type (N);
9423 end if;
9425 -- Freeze processing for array types
9427 elsif Is_Array_Type (Def_Id) then
9428 Expand_Freeze_Array_Type (N);
9430 -- Freeze processing for access types
9432 -- For pool-specific access types, find out the pool object used for
9433 -- this type, needs actual expansion of it in some cases. Here are the
9434 -- different cases :
9436 -- 1. Rep Clause "for Def_Id'Storage_Size use 0;"
9437 -- ---> don't use any storage pool
9439 -- 2. Rep Clause : for Def_Id'Storage_Size use Expr.
9440 -- Expand:
9441 -- Def_Id__Pool : Stack_Bounded_Pool (Expr, DT'Size, DT'Alignment);
9443 -- 3. Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
9444 -- ---> Storage Pool is the specified one
9446 -- See GNAT Pool packages in the Run-Time for more details
9448 elsif Ekind (Def_Id) in E_Access_Type | E_General_Access_Type then
9449 declare
9450 Loc : constant Source_Ptr := Sloc (N);
9451 Desig_Type : constant Entity_Id := Designated_Type (Def_Id);
9453 Freeze_Action_Typ : Entity_Id;
9454 Pool_Object : Entity_Id;
9456 begin
9457 -- Case 1
9459 -- Rep Clause "for Def_Id'Storage_Size use 0;"
9460 -- ---> don't use any storage pool
9462 if No_Pool_Assigned (Def_Id) then
9463 null;
9465 -- Case 2
9467 -- Rep Clause : for Def_Id'Storage_Size use Expr.
9468 -- ---> Expand:
9469 -- Def_Id__Pool : Stack_Bounded_Pool
9470 -- (Expr, DT'Size, DT'Alignment);
9472 elsif Has_Storage_Size_Clause (Def_Id) then
9473 declare
9474 DT_Align : Node_Id;
9475 DT_Size : Node_Id;
9477 begin
9478 -- For unconstrained composite types we give a size of zero
9479 -- so that the pool knows that it needs a special algorithm
9480 -- for variable size object allocation.
9482 if Is_Composite_Type (Desig_Type)
9483 and then not Is_Constrained (Desig_Type)
9484 then
9485 DT_Size := Make_Integer_Literal (Loc, 0);
9486 DT_Align := Make_Integer_Literal (Loc, Maximum_Alignment);
9488 else
9489 DT_Size :=
9490 Make_Attribute_Reference (Loc,
9491 Prefix => New_Occurrence_Of (Desig_Type, Loc),
9492 Attribute_Name => Name_Max_Size_In_Storage_Elements);
9494 DT_Align :=
9495 Make_Attribute_Reference (Loc,
9496 Prefix => New_Occurrence_Of (Desig_Type, Loc),
9497 Attribute_Name => Name_Alignment);
9498 end if;
9500 Pool_Object :=
9501 Make_Defining_Identifier (Loc,
9502 Chars => New_External_Name (Chars (Def_Id), 'P'));
9504 -- We put the code associated with the pools in the entity
9505 -- that has the later freeze node, usually the access type
9506 -- but it can also be the designated_type; because the pool
9507 -- code requires both those types to be frozen
9509 if Is_Frozen (Desig_Type)
9510 and then (No (Freeze_Node (Desig_Type))
9511 or else Analyzed (Freeze_Node (Desig_Type)))
9512 then
9513 Freeze_Action_Typ := Def_Id;
9515 -- A Taft amendment type cannot get the freeze actions
9516 -- since the full view is not there.
9518 elsif Is_Incomplete_Or_Private_Type (Desig_Type)
9519 and then No (Full_View (Desig_Type))
9520 then
9521 Freeze_Action_Typ := Def_Id;
9523 else
9524 Freeze_Action_Typ := Desig_Type;
9525 end if;
9527 Append_Freeze_Action (Freeze_Action_Typ,
9528 Make_Object_Declaration (Loc,
9529 Defining_Identifier => Pool_Object,
9530 Object_Definition =>
9531 Make_Subtype_Indication (Loc,
9532 Subtype_Mark =>
9533 New_Occurrence_Of
9534 (RTE (RE_Stack_Bounded_Pool), Loc),
9536 Constraint =>
9537 Make_Index_Or_Discriminant_Constraint (Loc,
9538 Constraints => New_List (
9540 -- First discriminant is the Pool Size
9542 New_Occurrence_Of (
9543 Storage_Size_Variable (Def_Id), Loc),
9545 -- Second discriminant is the element size
9547 DT_Size,
9549 -- Third discriminant is the alignment
9551 DT_Align)))));
9552 end;
9554 Set_Associated_Storage_Pool (Def_Id, Pool_Object);
9556 -- Case 3
9558 -- Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
9559 -- ---> Storage Pool is the specified one
9561 -- When compiling in Ada 2012 mode, ensure that the accessibility
9562 -- level of the subpool access type is not deeper than that of the
9563 -- pool_with_subpools.
9565 elsif Ada_Version >= Ada_2012
9566 and then Present (Associated_Storage_Pool (Def_Id))
9567 and then RTU_Loaded (System_Storage_Pools_Subpools)
9568 then
9569 declare
9570 Loc : constant Source_Ptr := Sloc (Def_Id);
9571 Pool : constant Entity_Id :=
9572 Associated_Storage_Pool (Def_Id);
9574 begin
9575 -- It is known that the accessibility level of the access
9576 -- type is deeper than that of the pool.
9578 if Type_Access_Level (Def_Id)
9579 > Static_Accessibility_Level (Pool, Object_Decl_Level)
9580 and then Is_Class_Wide_Type (Etype (Pool))
9581 and then not Accessibility_Checks_Suppressed (Def_Id)
9582 and then not Accessibility_Checks_Suppressed (Pool)
9583 then
9584 -- When the pool is of a class-wide type, it may or may
9585 -- not support subpools depending on the path of
9586 -- derivation. Generate:
9588 -- if Def_Id in RSPWS'Class then
9589 -- raise Program_Error;
9590 -- end if;
9592 Append_Freeze_Action (Def_Id,
9593 Make_If_Statement (Loc,
9594 Condition =>
9595 Make_In (Loc,
9596 Left_Opnd => New_Occurrence_Of (Pool, Loc),
9597 Right_Opnd =>
9598 New_Occurrence_Of
9599 (Class_Wide_Type
9600 (RTE
9601 (RE_Root_Storage_Pool_With_Subpools)),
9602 Loc)),
9603 Then_Statements => New_List (
9604 Make_Raise_Program_Error (Loc,
9605 Reason => PE_Accessibility_Check_Failed))));
9606 end if;
9607 end;
9608 end if;
9610 -- For access-to-controlled types (including class-wide types and
9611 -- Taft-amendment types, which potentially have controlled
9612 -- components), expand the list controller object that will store
9613 -- the dynamically allocated objects. Don't do this transformation
9614 -- for expander-generated access types, except do it for types
9615 -- that are the full view of types derived from other private
9616 -- types and for access types used to implement indirect temps.
9617 -- Also suppress the list controller in the case of a designated
9618 -- type with convention Java, since this is used when binding to
9619 -- Java API specs, where there's no equivalent of a finalization
9620 -- list and we don't want to pull in the finalization support if
9621 -- not needed.
9623 if not Comes_From_Source (Def_Id)
9624 and then not Has_Private_Declaration (Def_Id)
9625 and then not Old_Attr_Util.Indirect_Temps
9626 .Is_Access_Type_For_Indirect_Temp (Def_Id)
9627 then
9628 null;
9630 -- An exception is made for types defined in the run-time because
9631 -- Ada.Tags.Tag itself is such a type and cannot afford this
9632 -- unnecessary overhead that would generates a loop in the
9633 -- expansion scheme. Another exception is if Restrictions
9634 -- (No_Finalization) is active, since then we know nothing is
9635 -- controlled.
9637 elsif Restriction_Active (No_Finalization)
9638 or else In_Runtime (Def_Id)
9639 then
9640 null;
9642 -- Create a finalization master for an access-to-controlled type
9643 -- or an access-to-incomplete type. It is assumed that the full
9644 -- view will be controlled.
9646 elsif Needs_Finalization (Desig_Type)
9647 or else (Is_Incomplete_Type (Desig_Type)
9648 and then No (Full_View (Desig_Type)))
9649 then
9650 Build_Finalization_Master (Def_Id);
9652 -- Create a finalization master when the designated type contains
9653 -- a private component. It is assumed that the full view will be
9654 -- controlled.
9656 elsif Has_Private_Component (Desig_Type) then
9657 Build_Finalization_Master
9658 (Typ => Def_Id,
9659 For_Private => True,
9660 Context_Scope => Scope (Def_Id),
9661 Insertion_Node => Declaration_Node (Desig_Type));
9662 end if;
9663 end;
9665 -- Freeze processing for enumeration types
9667 elsif Ekind (Def_Id) = E_Enumeration_Type then
9669 -- We only have something to do if we have a non-standard
9670 -- representation (i.e. at least one literal whose pos value
9671 -- is not the same as its representation)
9673 if Has_Non_Standard_Rep (Def_Id) then
9674 Expand_Freeze_Enumeration_Type (N);
9675 end if;
9677 -- Private types that are completed by a derivation from a private
9678 -- type have an internally generated full view, that needs to be
9679 -- frozen. This must be done explicitly because the two views share
9680 -- the freeze node, and the underlying full view is not visible when
9681 -- the freeze node is analyzed.
9683 elsif Is_Private_Type (Def_Id)
9684 and then Is_Derived_Type (Def_Id)
9685 and then Present (Full_View (Def_Id))
9686 and then Is_Itype (Full_View (Def_Id))
9687 and then Has_Private_Declaration (Full_View (Def_Id))
9688 and then Freeze_Node (Full_View (Def_Id)) = N
9689 then
9690 Set_Entity (N, Full_View (Def_Id));
9691 Result := Freeze_Type (N);
9692 Set_Entity (N, Def_Id);
9694 -- All other types require no expander action. There are such cases
9695 -- (e.g. task types and protected types). In such cases, the freeze
9696 -- nodes are there for use by Gigi.
9698 end if;
9700 -- Complete the initialization of all pending access types' finalization
9701 -- masters now that the designated type has been is frozen and primitive
9702 -- Finalize_Address generated.
9704 Process_Pending_Access_Types (Def_Id);
9705 Freeze_Stream_Operations (N, Def_Id);
9707 -- Generate the [spec and] body of the invariant procedure tasked with
9708 -- the runtime verification of all invariants that pertain to the type.
9709 -- This includes invariants on the partial and full view, inherited
9710 -- class-wide invariants from parent types or interfaces, and invariants
9711 -- on array elements or record components. But skip internal types.
9713 if Is_Itype (Def_Id) then
9714 null;
9716 elsif Is_Interface (Def_Id) then
9718 -- Interfaces are treated as the partial view of a private type in
9719 -- order to achieve uniformity with the general case. As a result, an
9720 -- interface receives only a "partial" invariant procedure which is
9721 -- never called.
9723 if Has_Own_Invariants (Def_Id) then
9724 Build_Invariant_Procedure_Body
9725 (Typ => Def_Id,
9726 Partial_Invariant => Is_Interface (Def_Id));
9727 end if;
9729 -- Non-interface types
9731 -- Do not generate invariant procedure within other assertion
9732 -- subprograms, which may involve local declarations of local
9733 -- subtypes to which these checks do not apply.
9735 else
9736 if Has_Invariants (Def_Id) then
9737 if not Predicate_Check_In_Scope (Def_Id)
9738 or else (Ekind (Current_Scope) = E_Function
9739 and then Is_Predicate_Function (Current_Scope))
9740 then
9741 null;
9742 else
9743 Build_Invariant_Procedure_Body (Def_Id);
9744 end if;
9745 end if;
9747 -- Generate the [spec and] body of the procedure tasked with the
9748 -- run-time verification of pragma Default_Initial_Condition's
9749 -- expression.
9751 if Has_DIC (Def_Id) then
9752 Build_DIC_Procedure_Body (Def_Id);
9753 end if;
9754 end if;
9756 Restore_Ghost_Region (Saved_GM, Saved_IGR);
9758 return Result;
9760 exception
9761 when RE_Not_Available =>
9762 Restore_Ghost_Region (Saved_GM, Saved_IGR);
9764 return False;
9765 end Freeze_Type;
9767 -------------------------
9768 -- Get_Simple_Init_Val --
9769 -------------------------
9771 function Get_Simple_Init_Val
9772 (Typ : Entity_Id;
9773 N : Node_Id;
9774 Size : Uint := No_Uint) return Node_Id
9776 IV_Attribute : constant Boolean :=
9777 Nkind (N) = N_Attribute_Reference
9778 and then Attribute_Name (N) = Name_Invalid_Value;
9780 Loc : constant Source_Ptr := Sloc (N);
9782 procedure Extract_Subtype_Bounds
9783 (Lo_Bound : out Uint;
9784 Hi_Bound : out Uint);
9785 -- Inspect subtype Typ as well its ancestor subtypes and derived types
9786 -- to determine the best known information about the bounds of the type.
9787 -- The output parameters are set as follows:
9789 -- * Lo_Bound - Set to No_Unit when there is no information available,
9790 -- or to the known low bound.
9792 -- * Hi_Bound - Set to No_Unit when there is no information available,
9793 -- or to the known high bound.
9795 function Simple_Init_Array_Type return Node_Id;
9796 -- Build an expression to initialize array type Typ
9798 function Simple_Init_Defaulted_Type return Node_Id;
9799 -- Build an expression to initialize type Typ which is subject to
9800 -- aspect Default_Value.
9802 function Simple_Init_Initialize_Scalars_Type
9803 (Size_To_Use : Uint) return Node_Id;
9804 -- Build an expression to initialize scalar type Typ which is subject to
9805 -- pragma Initialize_Scalars. Size_To_Use is the size of the object.
9807 function Simple_Init_Normalize_Scalars_Type
9808 (Size_To_Use : Uint) return Node_Id;
9809 -- Build an expression to initialize scalar type Typ which is subject to
9810 -- pragma Normalize_Scalars. Size_To_Use is the size of the object.
9812 function Simple_Init_Private_Type return Node_Id;
9813 -- Build an expression to initialize private type Typ
9815 function Simple_Init_Scalar_Type return Node_Id;
9816 -- Build an expression to initialize scalar type Typ
9818 ----------------------------
9819 -- Extract_Subtype_Bounds --
9820 ----------------------------
9822 procedure Extract_Subtype_Bounds
9823 (Lo_Bound : out Uint;
9824 Hi_Bound : out Uint)
9826 ST1 : Entity_Id;
9827 ST2 : Entity_Id;
9828 Lo : Node_Id;
9829 Hi : Node_Id;
9830 Lo_Val : Uint;
9831 Hi_Val : Uint;
9833 begin
9834 Lo_Bound := No_Uint;
9835 Hi_Bound := No_Uint;
9837 -- Loop to climb ancestor subtypes and derived types
9839 ST1 := Typ;
9840 loop
9841 if not Is_Discrete_Type (ST1) then
9842 return;
9843 end if;
9845 Lo := Type_Low_Bound (ST1);
9846 Hi := Type_High_Bound (ST1);
9848 if Compile_Time_Known_Value (Lo) then
9849 Lo_Val := Expr_Value (Lo);
9851 if No (Lo_Bound) or else Lo_Bound < Lo_Val then
9852 Lo_Bound := Lo_Val;
9853 end if;
9854 end if;
9856 if Compile_Time_Known_Value (Hi) then
9857 Hi_Val := Expr_Value (Hi);
9859 if No (Hi_Bound) or else Hi_Bound > Hi_Val then
9860 Hi_Bound := Hi_Val;
9861 end if;
9862 end if;
9864 ST2 := Ancestor_Subtype (ST1);
9866 if No (ST2) then
9867 ST2 := Etype (ST1);
9868 end if;
9870 exit when ST1 = ST2;
9871 ST1 := ST2;
9872 end loop;
9873 end Extract_Subtype_Bounds;
9875 ----------------------------
9876 -- Simple_Init_Array_Type --
9877 ----------------------------
9879 function Simple_Init_Array_Type return Node_Id is
9880 Comp_Typ : constant Entity_Id := Component_Type (Typ);
9882 function Simple_Init_Dimension (Index : Node_Id) return Node_Id;
9883 -- Initialize a single array dimension with index constraint Index
9885 --------------------
9886 -- Simple_Init_Dimension --
9887 --------------------
9889 function Simple_Init_Dimension (Index : Node_Id) return Node_Id is
9890 begin
9891 -- Process the current dimension
9893 if Present (Index) then
9895 -- Build a suitable "others" aggregate for the next dimension,
9896 -- or initialize the component itself. Generate:
9898 -- (others => ...)
9900 return
9901 Make_Aggregate (Loc,
9902 Component_Associations => New_List (
9903 Make_Component_Association (Loc,
9904 Choices => New_List (Make_Others_Choice (Loc)),
9905 Expression =>
9906 Simple_Init_Dimension (Next_Index (Index)))));
9908 -- Otherwise all dimensions have been processed. Initialize the
9909 -- component itself.
9911 else
9912 return
9913 Get_Simple_Init_Val
9914 (Typ => Comp_Typ,
9915 N => N,
9916 Size => Esize (Comp_Typ));
9917 end if;
9918 end Simple_Init_Dimension;
9920 -- Start of processing for Simple_Init_Array_Type
9922 begin
9923 return Simple_Init_Dimension (First_Index (Typ));
9924 end Simple_Init_Array_Type;
9926 --------------------------------
9927 -- Simple_Init_Defaulted_Type --
9928 --------------------------------
9930 function Simple_Init_Defaulted_Type return Node_Id is
9931 Subtyp : Entity_Id := First_Subtype (Typ);
9933 begin
9934 -- When the first subtype is private, retrieve the expression of the
9935 -- Default_Value from the underlying type.
9937 if Is_Private_Type (Subtyp) then
9938 Subtyp := Full_View (Subtyp);
9939 end if;
9941 -- Use the Sloc of the context node when constructing the initial
9942 -- value because the expression of Default_Value may come from a
9943 -- different unit. Updating the Sloc will result in accurate error
9944 -- diagnostics.
9946 return
9947 OK_Convert_To
9948 (Typ => Typ,
9949 Expr =>
9950 New_Copy_Tree
9951 (Source => Default_Aspect_Value (Subtyp),
9952 New_Sloc => Loc));
9953 end Simple_Init_Defaulted_Type;
9955 -----------------------------------------
9956 -- Simple_Init_Initialize_Scalars_Type --
9957 -----------------------------------------
9959 function Simple_Init_Initialize_Scalars_Type
9960 (Size_To_Use : Uint) return Node_Id
9962 Float_Typ : Entity_Id;
9963 Hi_Bound : Uint;
9964 Lo_Bound : Uint;
9965 Scal_Typ : Scalar_Id;
9967 begin
9968 Extract_Subtype_Bounds (Lo_Bound, Hi_Bound);
9970 -- Float types
9972 if Is_Floating_Point_Type (Typ) then
9973 Float_Typ := Root_Type (Typ);
9975 if Float_Typ = Standard_Short_Float then
9976 Scal_Typ := Name_Short_Float;
9977 elsif Float_Typ = Standard_Float then
9978 Scal_Typ := Name_Float;
9979 elsif Float_Typ = Standard_Long_Float then
9980 Scal_Typ := Name_Long_Float;
9981 else pragma Assert (Float_Typ = Standard_Long_Long_Float);
9982 Scal_Typ := Name_Long_Long_Float;
9983 end if;
9985 -- If zero is invalid, it is a convenient value to use that is for
9986 -- sure an appropriate invalid value in all situations.
9988 elsif Present (Lo_Bound) and then Lo_Bound > Uint_0 then
9989 return Make_Integer_Literal (Loc, 0);
9991 -- Unsigned types
9993 elsif Is_Unsigned_Type (Typ) then
9994 if Size_To_Use <= 8 then
9995 Scal_Typ := Name_Unsigned_8;
9996 elsif Size_To_Use <= 16 then
9997 Scal_Typ := Name_Unsigned_16;
9998 elsif Size_To_Use <= 32 then
9999 Scal_Typ := Name_Unsigned_32;
10000 elsif Size_To_Use <= 64 then
10001 Scal_Typ := Name_Unsigned_64;
10002 else
10003 Scal_Typ := Name_Unsigned_128;
10004 end if;
10006 -- Signed types
10008 else
10009 if Size_To_Use <= 8 then
10010 Scal_Typ := Name_Signed_8;
10011 elsif Size_To_Use <= 16 then
10012 Scal_Typ := Name_Signed_16;
10013 elsif Size_To_Use <= 32 then
10014 Scal_Typ := Name_Signed_32;
10015 elsif Size_To_Use <= 64 then
10016 Scal_Typ := Name_Signed_64;
10017 else
10018 Scal_Typ := Name_Signed_128;
10019 end if;
10020 end if;
10022 -- Use the values specified by pragma Initialize_Scalars or the ones
10023 -- provided by the binder. Higher precedence is given to the pragma.
10025 return Invalid_Scalar_Value (Loc, Scal_Typ);
10026 end Simple_Init_Initialize_Scalars_Type;
10028 ----------------------------------------
10029 -- Simple_Init_Normalize_Scalars_Type --
10030 ----------------------------------------
10032 function Simple_Init_Normalize_Scalars_Type
10033 (Size_To_Use : Uint) return Node_Id
10035 Signed_Size : constant Uint := UI_Min (Uint_63, Size_To_Use - 1);
10037 Expr : Node_Id;
10038 Hi_Bound : Uint;
10039 Lo_Bound : Uint;
10041 begin
10042 Extract_Subtype_Bounds (Lo_Bound, Hi_Bound);
10044 -- If zero is invalid, it is a convenient value to use that is for
10045 -- sure an appropriate invalid value in all situations.
10047 if Present (Lo_Bound) and then Lo_Bound > Uint_0 then
10048 Expr := Make_Integer_Literal (Loc, 0);
10050 -- Cases where all one bits is the appropriate invalid value
10052 -- For modular types, all 1 bits is either invalid or valid. If it
10053 -- is valid, then there is nothing that can be done since there are
10054 -- no invalid values (we ruled out zero already).
10056 -- For signed integer types that have no negative values, either
10057 -- there is room for negative values, or there is not. If there
10058 -- is, then all 1-bits may be interpreted as minus one, which is
10059 -- certainly invalid. Alternatively it is treated as the largest
10060 -- positive value, in which case the observation for modular types
10061 -- still applies.
10063 -- For float types, all 1-bits is a NaN (not a number), which is
10064 -- certainly an appropriately invalid value.
10066 elsif Is_Enumeration_Type (Typ)
10067 or else Is_Floating_Point_Type (Typ)
10068 or else Is_Unsigned_Type (Typ)
10069 then
10070 Expr := Make_Integer_Literal (Loc, 2 ** Size_To_Use - 1);
10072 -- Resolve as Long_Long_Long_Unsigned, because the largest number
10073 -- we can generate is out of range of universal integer.
10075 Analyze_And_Resolve (Expr, Standard_Long_Long_Long_Unsigned);
10077 -- Case of signed types
10079 else
10080 -- Normally we like to use the most negative number. The one
10081 -- exception is when this number is in the known subtype range and
10082 -- the largest positive number is not in the known subtype range.
10084 -- For this exceptional case, use largest positive value
10086 if Present (Lo_Bound) and then Present (Hi_Bound)
10087 and then Lo_Bound <= (-(2 ** Signed_Size))
10088 and then Hi_Bound < 2 ** Signed_Size
10089 then
10090 Expr := Make_Integer_Literal (Loc, 2 ** Signed_Size - 1);
10092 -- Normal case of largest negative value
10094 else
10095 Expr := Make_Integer_Literal (Loc, -(2 ** Signed_Size));
10096 end if;
10097 end if;
10099 return Expr;
10100 end Simple_Init_Normalize_Scalars_Type;
10102 ------------------------------
10103 -- Simple_Init_Private_Type --
10104 ------------------------------
10106 function Simple_Init_Private_Type return Node_Id is
10107 Under_Typ : constant Entity_Id := Underlying_Type (Typ);
10108 Expr : Node_Id;
10110 begin
10111 -- The availability of the underlying view must be checked by routine
10112 -- Needs_Simple_Initialization.
10114 pragma Assert (Present (Under_Typ));
10116 Expr := Get_Simple_Init_Val (Under_Typ, N, Size);
10118 -- If the initial value is null or an aggregate, qualify it with the
10119 -- underlying type in order to provide a proper context.
10121 if Nkind (Expr) in N_Aggregate | N_Null then
10122 Expr :=
10123 Make_Qualified_Expression (Loc,
10124 Subtype_Mark => New_Occurrence_Of (Under_Typ, Loc),
10125 Expression => Expr);
10126 end if;
10128 Expr := Unchecked_Convert_To (Typ, Expr);
10130 -- Do not truncate the result when scalar types are involved and
10131 -- Initialize/Normalize_Scalars is in effect.
10133 if Nkind (Expr) = N_Unchecked_Type_Conversion
10134 and then Is_Scalar_Type (Under_Typ)
10135 then
10136 Set_No_Truncation (Expr);
10137 end if;
10139 return Expr;
10140 end Simple_Init_Private_Type;
10142 -----------------------------
10143 -- Simple_Init_Scalar_Type --
10144 -----------------------------
10146 function Simple_Init_Scalar_Type return Node_Id is
10147 Expr : Node_Id;
10148 Size_To_Use : Uint;
10150 begin
10151 pragma Assert (Init_Or_Norm_Scalars or IV_Attribute);
10153 -- Determine the size of the object. This is either the size provided
10154 -- by the caller, or the Esize of the scalar type.
10156 if No (Size) or else Size <= Uint_0 then
10157 Size_To_Use := UI_Max (Uint_1, Esize (Typ));
10158 else
10159 Size_To_Use := Size;
10160 end if;
10162 -- The maximum size to use is System_Max_Integer_Size bits. This
10163 -- will create values of type Long_Long_Long_Unsigned and the range
10164 -- must fit this type.
10166 if Present (Size_To_Use)
10167 and then Size_To_Use > System_Max_Integer_Size
10168 then
10169 Size_To_Use := UI_From_Int (System_Max_Integer_Size);
10170 end if;
10172 if Normalize_Scalars and then not IV_Attribute then
10173 Expr := Simple_Init_Normalize_Scalars_Type (Size_To_Use);
10174 else
10175 Expr := Simple_Init_Initialize_Scalars_Type (Size_To_Use);
10176 end if;
10178 -- The final expression is obtained by doing an unchecked conversion
10179 -- of this result to the base type of the required subtype. Use the
10180 -- base type to prevent the unchecked conversion from chopping bits,
10181 -- and then we set Kill_Range_Check to preserve the "bad" value.
10183 Expr := Unchecked_Convert_To (Base_Type (Typ), Expr);
10185 -- Ensure that the expression is not truncated since the "bad" bits
10186 -- are desired, and also kill the range checks.
10188 if Nkind (Expr) = N_Unchecked_Type_Conversion then
10189 Set_Kill_Range_Check (Expr);
10190 Set_No_Truncation (Expr);
10191 end if;
10193 return Expr;
10194 end Simple_Init_Scalar_Type;
10196 -- Start of processing for Get_Simple_Init_Val
10198 begin
10199 if Is_Private_Type (Typ) then
10200 return Simple_Init_Private_Type;
10202 elsif Is_Scalar_Type (Typ) then
10203 if Has_Default_Aspect (Typ) then
10204 return Simple_Init_Defaulted_Type;
10205 else
10206 return Simple_Init_Scalar_Type;
10207 end if;
10209 -- Array type with Initialize or Normalize_Scalars
10211 elsif Is_Array_Type (Typ) then
10212 pragma Assert (Init_Or_Norm_Scalars);
10213 return Simple_Init_Array_Type;
10215 -- Access type is initialized to null
10217 elsif Is_Access_Type (Typ) then
10218 return Make_Null (Loc);
10220 -- No other possibilities should arise, since we should only be calling
10221 -- Get_Simple_Init_Val if Needs_Simple_Initialization returned True,
10222 -- indicating one of the above cases held.
10224 else
10225 raise Program_Error;
10226 end if;
10228 exception
10229 when RE_Not_Available =>
10230 return Empty;
10231 end Get_Simple_Init_Val;
10233 ------------------------------
10234 -- Has_New_Non_Standard_Rep --
10235 ------------------------------
10237 function Has_New_Non_Standard_Rep (T : Entity_Id) return Boolean is
10238 begin
10239 if not Is_Derived_Type (T) then
10240 return Has_Non_Standard_Rep (T)
10241 or else Has_Non_Standard_Rep (Root_Type (T));
10243 -- If Has_Non_Standard_Rep is not set on the derived type, the
10244 -- representation is fully inherited.
10246 elsif not Has_Non_Standard_Rep (T) then
10247 return False;
10249 else
10250 return First_Rep_Item (T) /= First_Rep_Item (Root_Type (T));
10252 -- May need a more precise check here: the First_Rep_Item may be a
10253 -- stream attribute, which does not affect the representation of the
10254 -- type ???
10256 end if;
10257 end Has_New_Non_Standard_Rep;
10259 ----------------------
10260 -- Inline_Init_Proc --
10261 ----------------------
10263 function Inline_Init_Proc (Typ : Entity_Id) return Boolean is
10264 begin
10265 -- The initialization proc of protected records is not worth inlining.
10266 -- In addition, when compiled for another unit for inlining purposes,
10267 -- it may make reference to entities that have not been elaborated yet.
10268 -- The initialization proc of records that need finalization contains
10269 -- a nested clean-up procedure that makes it impractical to inline as
10270 -- well, except for simple controlled types themselves. And similar
10271 -- considerations apply to task types.
10273 if Is_Concurrent_Type (Typ) then
10274 return False;
10276 elsif Needs_Finalization (Typ) and then not Is_Controlled (Typ) then
10277 return False;
10279 elsif Has_Task (Typ) then
10280 return False;
10282 else
10283 return True;
10284 end if;
10285 end Inline_Init_Proc;
10287 ----------------
10288 -- In_Runtime --
10289 ----------------
10291 function In_Runtime (E : Entity_Id) return Boolean is
10292 S1 : Entity_Id;
10294 begin
10295 S1 := Scope (E);
10296 while Scope (S1) /= Standard_Standard loop
10297 S1 := Scope (S1);
10298 end loop;
10300 return Is_RTU (S1, System) or else Is_RTU (S1, Ada);
10301 end In_Runtime;
10303 package body Initialization_Control is
10305 ------------------------
10306 -- Requires_Late_Init --
10307 ------------------------
10309 function Requires_Late_Init
10310 (Decl : Node_Id;
10311 Rec_Type : Entity_Id) return Boolean
10313 References_Current_Instance : Boolean := False;
10314 Has_Access_Discriminant : Boolean := False;
10315 Has_Internal_Call : Boolean := False;
10317 function Find_Access_Discriminant
10318 (N : Node_Id) return Traverse_Result;
10319 -- Look for a name denoting an access discriminant
10321 function Find_Current_Instance
10322 (N : Node_Id) return Traverse_Result;
10323 -- Look for a reference to the current instance of the type
10325 function Find_Internal_Call
10326 (N : Node_Id) return Traverse_Result;
10327 -- Look for an internal protected function call
10329 ------------------------------
10330 -- Find_Access_Discriminant --
10331 ------------------------------
10333 function Find_Access_Discriminant
10334 (N : Node_Id) return Traverse_Result is
10335 begin
10336 if Is_Entity_Name (N)
10337 and then Denotes_Discriminant (N)
10338 and then Is_Access_Type (Etype (N))
10339 then
10340 Has_Access_Discriminant := True;
10341 return Abandon;
10342 else
10343 return OK;
10344 end if;
10345 end Find_Access_Discriminant;
10347 ---------------------------
10348 -- Find_Current_Instance --
10349 ---------------------------
10351 function Find_Current_Instance
10352 (N : Node_Id) return Traverse_Result is
10353 begin
10354 if Is_Entity_Name (N)
10355 and then Present (Entity (N))
10356 and then Is_Current_Instance (N)
10357 then
10358 References_Current_Instance := True;
10359 return Abandon;
10360 else
10361 return OK;
10362 end if;
10363 end Find_Current_Instance;
10365 ------------------------
10366 -- Find_Internal_Call --
10367 ------------------------
10369 function Find_Internal_Call (N : Node_Id) return Traverse_Result is
10371 function Call_Scope (N : Node_Id) return Entity_Id;
10372 -- Return the scope enclosing a given call node N
10374 ----------------
10375 -- Call_Scope --
10376 ----------------
10378 function Call_Scope (N : Node_Id) return Entity_Id is
10379 Nam : constant Node_Id := Name (N);
10380 begin
10381 if Nkind (Nam) = N_Selected_Component then
10382 return Scope (Entity (Prefix (Nam)));
10383 else
10384 return Scope (Entity (Nam));
10385 end if;
10386 end Call_Scope;
10388 begin
10389 if Nkind (N) = N_Function_Call
10390 and then Call_Scope (N)
10391 = Corresponding_Concurrent_Type (Rec_Type)
10392 then
10393 Has_Internal_Call := True;
10394 return Abandon;
10395 else
10396 return OK;
10397 end if;
10398 end Find_Internal_Call;
10400 procedure Search_Access_Discriminant is new
10401 Traverse_Proc (Find_Access_Discriminant);
10403 procedure Search_Current_Instance is new
10404 Traverse_Proc (Find_Current_Instance);
10406 procedure Search_Internal_Call is new
10407 Traverse_Proc (Find_Internal_Call);
10409 -- Start of processing for Requires_Late_Init
10411 begin
10412 -- A component of an object is said to require late initialization
10413 -- if:
10415 -- it has an access discriminant value constrained by a per-object
10416 -- expression;
10418 if Has_Access_Constraint (Defining_Identifier (Decl))
10419 and then No (Expression (Decl))
10420 then
10421 return True;
10423 elsif Present (Expression (Decl)) then
10425 -- it has an initialization expression that includes a name
10426 -- denoting an access discriminant;
10428 Search_Access_Discriminant (Expression (Decl));
10430 if Has_Access_Discriminant then
10431 return True;
10432 end if;
10434 -- or it has an initialization expression that includes a
10435 -- reference to the current instance of the type either by
10436 -- name...
10438 Search_Current_Instance (Expression (Decl));
10440 if References_Current_Instance then
10441 return True;
10442 end if;
10444 -- ...or implicitly as the target object of a call.
10446 if Is_Protected_Record_Type (Rec_Type) then
10447 Search_Internal_Call (Expression (Decl));
10449 if Has_Internal_Call then
10450 return True;
10451 end if;
10452 end if;
10453 end if;
10455 return False;
10456 end Requires_Late_Init;
10458 -----------------------------
10459 -- Has_Late_Init_Component --
10460 -----------------------------
10462 function Has_Late_Init_Component
10463 (Tagged_Rec_Type : Entity_Id) return Boolean
10465 Comp_Id : Entity_Id :=
10466 First_Component (Implementation_Base_Type (Tagged_Rec_Type));
10467 begin
10468 while Present (Comp_Id) loop
10469 if Requires_Late_Init (Decl => Parent (Comp_Id),
10470 Rec_Type => Tagged_Rec_Type)
10471 then
10472 return True; -- found a component that requires late init
10474 elsif Chars (Comp_Id) = Name_uParent
10475 and then Has_Late_Init_Component (Etype (Comp_Id))
10476 then
10477 return True; -- an ancestor type has a late init component
10478 end if;
10480 Next_Component (Comp_Id);
10481 end loop;
10483 return False;
10484 end Has_Late_Init_Component;
10486 ------------------------
10487 -- Tag_Init_Condition --
10488 ------------------------
10490 function Tag_Init_Condition
10491 (Loc : Source_Ptr;
10492 Init_Control_Formal : Entity_Id) return Node_Id is
10493 begin
10494 return Make_Op_Eq (Loc,
10495 New_Occurrence_Of (Init_Control_Formal, Loc),
10496 Make_Mode_Literal (Loc, Full_Init));
10497 end Tag_Init_Condition;
10499 --------------------------
10500 -- Early_Init_Condition --
10501 --------------------------
10503 function Early_Init_Condition
10504 (Loc : Source_Ptr;
10505 Init_Control_Formal : Entity_Id) return Node_Id is
10506 begin
10507 return Make_Op_Ne (Loc,
10508 New_Occurrence_Of (Init_Control_Formal, Loc),
10509 Make_Mode_Literal (Loc, Late_Init_Only));
10510 end Early_Init_Condition;
10512 -------------------------
10513 -- Late_Init_Condition --
10514 -------------------------
10516 function Late_Init_Condition
10517 (Loc : Source_Ptr;
10518 Init_Control_Formal : Entity_Id) return Node_Id is
10519 begin
10520 return Make_Op_Ne (Loc,
10521 New_Occurrence_Of (Init_Control_Formal, Loc),
10522 Make_Mode_Literal (Loc, Early_Init_Only));
10523 end Late_Init_Condition;
10525 end Initialization_Control;
10527 ----------------------------
10528 -- Initialization_Warning --
10529 ----------------------------
10531 procedure Initialization_Warning (E : Entity_Id) is
10532 Warning_Needed : Boolean;
10534 begin
10535 Warning_Needed := False;
10537 if Ekind (Current_Scope) = E_Package
10538 and then Static_Elaboration_Desired (Current_Scope)
10539 then
10540 if Is_Type (E) then
10541 if Is_Record_Type (E) then
10542 if Has_Discriminants (E)
10543 or else Is_Limited_Type (E)
10544 or else Has_Non_Standard_Rep (E)
10545 then
10546 Warning_Needed := True;
10548 else
10549 -- Verify that at least one component has an initialization
10550 -- expression. No need for a warning on a type if all its
10551 -- components have no initialization.
10553 declare
10554 Comp : Entity_Id;
10556 begin
10557 Comp := First_Component (E);
10558 while Present (Comp) loop
10559 pragma Assert
10560 (Nkind (Parent (Comp)) = N_Component_Declaration);
10562 if Present (Expression (Parent (Comp))) then
10563 Warning_Needed := True;
10564 exit;
10565 end if;
10567 Next_Component (Comp);
10568 end loop;
10569 end;
10570 end if;
10572 if Warning_Needed then
10573 Error_Msg_N
10574 ("objects of the type cannot be initialized statically "
10575 & "by default??", Parent (E));
10576 end if;
10577 end if;
10579 else
10580 Error_Msg_N ("object cannot be initialized statically??", E);
10581 end if;
10582 end if;
10583 end Initialization_Warning;
10585 ------------------
10586 -- Init_Formals --
10587 ------------------
10589 function Init_Formals (Typ : Entity_Id; Proc_Id : Entity_Id) return List_Id
10591 Loc : constant Source_Ptr := Sloc (Typ);
10592 Unc_Arr : constant Boolean :=
10593 Is_Array_Type (Typ) and then not Is_Constrained (Typ);
10594 With_Prot : constant Boolean :=
10595 Has_Protected (Typ)
10596 or else (Is_Record_Type (Typ)
10597 and then Is_Protected_Record_Type (Typ));
10598 With_Task : constant Boolean :=
10599 not Global_No_Tasking
10600 and then
10601 (Has_Task (Typ)
10602 or else (Is_Record_Type (Typ)
10603 and then Is_Task_Record_Type (Typ)));
10604 Formals : List_Id;
10606 begin
10607 -- The first parameter is always _Init : [in] out Typ. Note that we need
10608 -- it to be in/out in the case of an unconstrained array, because of the
10609 -- need to have the bounds, and in the case of protected or task record
10610 -- value, because there are default record fields that may be referenced
10611 -- in the generated initialization routine.
10613 Formals := New_List (
10614 Make_Parameter_Specification (Loc,
10615 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uInit),
10616 In_Present => Unc_Arr or else With_Prot or else With_Task,
10617 Out_Present => True,
10618 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
10620 -- For task record value, or type that contains tasks, add two more
10621 -- formals, _Master : Master_Id and _Chain : in out Activation_Chain
10622 -- We also add these parameters for the task record type case.
10624 if With_Task then
10625 Append_To (Formals,
10626 Make_Parameter_Specification (Loc,
10627 Defining_Identifier =>
10628 Make_Defining_Identifier (Loc, Name_uMaster),
10629 Parameter_Type =>
10630 New_Occurrence_Of (Standard_Integer, Loc)));
10632 Set_Has_Master_Entity (Proc_Id);
10634 -- Add _Chain (not done for sequential elaboration policy, see
10635 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
10637 if Partition_Elaboration_Policy /= 'S' then
10638 Append_To (Formals,
10639 Make_Parameter_Specification (Loc,
10640 Defining_Identifier =>
10641 Make_Defining_Identifier (Loc, Name_uChain),
10642 In_Present => True,
10643 Out_Present => True,
10644 Parameter_Type =>
10645 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc)));
10646 end if;
10648 Append_To (Formals,
10649 Make_Parameter_Specification (Loc,
10650 Defining_Identifier =>
10651 Make_Defining_Identifier (Loc, Name_uTask_Name),
10652 In_Present => True,
10653 Parameter_Type => New_Occurrence_Of (Standard_String, Loc)));
10654 end if;
10656 -- Due to certain edge cases such as arrays with null-excluding
10657 -- components being built with the secondary stack it becomes necessary
10658 -- to add a formal to the Init_Proc which controls whether we raise
10659 -- Constraint_Errors on generated calls for internal object
10660 -- declarations.
10662 if Needs_Conditional_Null_Excluding_Check (Typ) then
10663 Append_To (Formals,
10664 Make_Parameter_Specification (Loc,
10665 Defining_Identifier =>
10666 Make_Defining_Identifier (Loc,
10667 New_External_Name (Chars
10668 (Component_Type (Typ)), "_skip_null_excluding_check")),
10669 Expression => New_Occurrence_Of (Standard_False, Loc),
10670 In_Present => True,
10671 Parameter_Type =>
10672 New_Occurrence_Of (Standard_Boolean, Loc)));
10673 end if;
10675 return Formals;
10677 exception
10678 when RE_Not_Available =>
10679 return Empty_List;
10680 end Init_Formals;
10682 -------------------------
10683 -- Init_Secondary_Tags --
10684 -------------------------
10686 procedure Init_Secondary_Tags
10687 (Typ : Entity_Id;
10688 Target : Node_Id;
10689 Init_Tags_List : List_Id;
10690 Stmts_List : List_Id;
10691 Fixed_Comps : Boolean := True;
10692 Variable_Comps : Boolean := True)
10694 Loc : constant Source_Ptr := Sloc (Target);
10696 -- Inherit the C++ tag of the secondary dispatch table of Typ associated
10697 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
10699 procedure Initialize_Tag
10700 (Typ : Entity_Id;
10701 Iface : Entity_Id;
10702 Tag_Comp : Entity_Id;
10703 Iface_Tag : Node_Id);
10704 -- Initialize the tag of the secondary dispatch table of Typ associated
10705 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
10706 -- Compiling under the CPP full ABI compatibility mode, if the ancestor
10707 -- of Typ CPP tagged type we generate code to inherit the contents of
10708 -- the dispatch table directly from the ancestor.
10710 --------------------
10711 -- Initialize_Tag --
10712 --------------------
10714 procedure Initialize_Tag
10715 (Typ : Entity_Id;
10716 Iface : Entity_Id;
10717 Tag_Comp : Entity_Id;
10718 Iface_Tag : Node_Id)
10720 Comp_Typ : Entity_Id;
10721 Offset_To_Top_Comp : Entity_Id := Empty;
10723 begin
10724 -- Initialize pointer to secondary DT associated with the interface
10726 if not Is_Ancestor (Iface, Typ, Use_Full_View => True) then
10727 Append_To (Init_Tags_List,
10728 Make_Assignment_Statement (Loc,
10729 Name =>
10730 Make_Selected_Component (Loc,
10731 Prefix => New_Copy_Tree (Target),
10732 Selector_Name => New_Occurrence_Of (Tag_Comp, Loc)),
10733 Expression =>
10734 New_Occurrence_Of (Iface_Tag, Loc)));
10735 end if;
10737 Comp_Typ := Scope (Tag_Comp);
10739 -- Initialize the entries of the table of interfaces. We generate a
10740 -- different call when the parent of the type has variable size
10741 -- components.
10743 if Comp_Typ /= Etype (Comp_Typ)
10744 and then Is_Variable_Size_Record (Etype (Comp_Typ))
10745 and then Chars (Tag_Comp) /= Name_uTag
10746 then
10747 pragma Assert (Present (DT_Offset_To_Top_Func (Tag_Comp)));
10749 -- Issue error if Set_Dynamic_Offset_To_Top is not available in a
10750 -- configurable run-time environment.
10752 if not RTE_Available (RE_Set_Dynamic_Offset_To_Top) then
10753 Error_Msg_CRT
10754 ("variable size record with interface types", Typ);
10755 return;
10756 end if;
10758 -- Generate:
10759 -- Set_Dynamic_Offset_To_Top
10760 -- (This => Init,
10761 -- Prim_T => Typ'Tag,
10762 -- Interface_T => Iface'Tag,
10763 -- Offset_Value => n,
10764 -- Offset_Func => Fn'Unrestricted_Access)
10766 Append_To (Stmts_List,
10767 Make_Procedure_Call_Statement (Loc,
10768 Name =>
10769 New_Occurrence_Of (RTE (RE_Set_Dynamic_Offset_To_Top), Loc),
10770 Parameter_Associations => New_List (
10771 Make_Attribute_Reference (Loc,
10772 Prefix => New_Copy_Tree (Target),
10773 Attribute_Name => Name_Address),
10775 Unchecked_Convert_To (RTE (RE_Tag),
10776 New_Occurrence_Of
10777 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc)),
10779 Unchecked_Convert_To (RTE (RE_Tag),
10780 New_Occurrence_Of
10781 (Node (First_Elmt (Access_Disp_Table (Iface))),
10782 Loc)),
10784 Unchecked_Convert_To
10785 (RTE (RE_Storage_Offset),
10786 Make_Op_Minus (Loc,
10787 Make_Attribute_Reference (Loc,
10788 Prefix =>
10789 Make_Selected_Component (Loc,
10790 Prefix => New_Copy_Tree (Target),
10791 Selector_Name =>
10792 New_Occurrence_Of (Tag_Comp, Loc)),
10793 Attribute_Name => Name_Position))),
10795 Unchecked_Convert_To (RTE (RE_Offset_To_Top_Function_Ptr),
10796 Make_Attribute_Reference (Loc,
10797 Prefix => New_Occurrence_Of
10798 (DT_Offset_To_Top_Func (Tag_Comp), Loc),
10799 Attribute_Name => Name_Unrestricted_Access)))));
10801 -- In this case the next component stores the value of the offset
10802 -- to the top.
10804 Offset_To_Top_Comp := Next_Entity (Tag_Comp);
10805 pragma Assert (Present (Offset_To_Top_Comp));
10807 Append_To (Init_Tags_List,
10808 Make_Assignment_Statement (Loc,
10809 Name =>
10810 Make_Selected_Component (Loc,
10811 Prefix => New_Copy_Tree (Target),
10812 Selector_Name =>
10813 New_Occurrence_Of (Offset_To_Top_Comp, Loc)),
10815 Expression =>
10816 Make_Op_Minus (Loc,
10817 Make_Attribute_Reference (Loc,
10818 Prefix =>
10819 Make_Selected_Component (Loc,
10820 Prefix => New_Copy_Tree (Target),
10821 Selector_Name => New_Occurrence_Of (Tag_Comp, Loc)),
10822 Attribute_Name => Name_Position))));
10824 -- Normal case: No discriminants in the parent type
10826 else
10827 -- Don't need to set any value if the offset-to-top field is
10828 -- statically set or if this interface shares the primary
10829 -- dispatch table.
10831 if not Building_Static_Secondary_DT (Typ)
10832 and then not Is_Ancestor (Iface, Typ, Use_Full_View => True)
10833 then
10834 Append_To (Stmts_List,
10835 Build_Set_Static_Offset_To_Top (Loc,
10836 Iface_Tag => New_Occurrence_Of (Iface_Tag, Loc),
10837 Offset_Value =>
10838 Unchecked_Convert_To (RTE (RE_Storage_Offset),
10839 Make_Op_Minus (Loc,
10840 Make_Attribute_Reference (Loc,
10841 Prefix =>
10842 Make_Selected_Component (Loc,
10843 Prefix => New_Copy_Tree (Target),
10844 Selector_Name =>
10845 New_Occurrence_Of (Tag_Comp, Loc)),
10846 Attribute_Name => Name_Position)))));
10847 end if;
10849 -- Generate:
10850 -- Register_Interface_Offset
10851 -- (Prim_T => Typ'Tag,
10852 -- Interface_T => Iface'Tag,
10853 -- Is_Constant => True,
10854 -- Offset_Value => n,
10855 -- Offset_Func => null);
10857 if not Building_Static_Secondary_DT (Typ)
10858 and then RTE_Available (RE_Register_Interface_Offset)
10859 then
10860 Append_To (Stmts_List,
10861 Make_Procedure_Call_Statement (Loc,
10862 Name =>
10863 New_Occurrence_Of
10864 (RTE (RE_Register_Interface_Offset), Loc),
10865 Parameter_Associations => New_List (
10866 Unchecked_Convert_To (RTE (RE_Tag),
10867 New_Occurrence_Of
10868 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc)),
10870 Unchecked_Convert_To (RTE (RE_Tag),
10871 New_Occurrence_Of
10872 (Node (First_Elmt (Access_Disp_Table (Iface))), Loc)),
10874 New_Occurrence_Of (Standard_True, Loc),
10876 Unchecked_Convert_To (RTE (RE_Storage_Offset),
10877 Make_Op_Minus (Loc,
10878 Make_Attribute_Reference (Loc,
10879 Prefix =>
10880 Make_Selected_Component (Loc,
10881 Prefix => New_Copy_Tree (Target),
10882 Selector_Name =>
10883 New_Occurrence_Of (Tag_Comp, Loc)),
10884 Attribute_Name => Name_Position))),
10886 Make_Null (Loc))));
10887 end if;
10888 end if;
10889 end Initialize_Tag;
10891 -- Local variables
10893 Full_Typ : Entity_Id;
10894 Ifaces_List : Elist_Id;
10895 Ifaces_Comp_List : Elist_Id;
10896 Ifaces_Tag_List : Elist_Id;
10897 Iface_Elmt : Elmt_Id;
10898 Iface_Comp_Elmt : Elmt_Id;
10899 Iface_Tag_Elmt : Elmt_Id;
10900 Tag_Comp : Node_Id;
10901 In_Variable_Pos : Boolean;
10903 -- Start of processing for Init_Secondary_Tags
10905 begin
10906 -- Handle private types
10908 if Present (Full_View (Typ)) then
10909 Full_Typ := Full_View (Typ);
10910 else
10911 Full_Typ := Typ;
10912 end if;
10914 Collect_Interfaces_Info
10915 (Full_Typ, Ifaces_List, Ifaces_Comp_List, Ifaces_Tag_List);
10917 Iface_Elmt := First_Elmt (Ifaces_List);
10918 Iface_Comp_Elmt := First_Elmt (Ifaces_Comp_List);
10919 Iface_Tag_Elmt := First_Elmt (Ifaces_Tag_List);
10920 while Present (Iface_Elmt) loop
10921 Tag_Comp := Node (Iface_Comp_Elmt);
10923 -- Check if parent of record type has variable size components
10925 In_Variable_Pos := Scope (Tag_Comp) /= Etype (Scope (Tag_Comp))
10926 and then Is_Variable_Size_Record (Etype (Scope (Tag_Comp)));
10928 -- If we are compiling under the CPP full ABI compatibility mode and
10929 -- the ancestor is a CPP_Pragma tagged type then we generate code to
10930 -- initialize the secondary tag components from tags that reference
10931 -- secondary tables filled with copy of parent slots.
10933 if Is_CPP_Class (Root_Type (Full_Typ)) then
10935 -- Reject interface components located at variable offset in
10936 -- C++ derivations. This is currently unsupported.
10938 if not Fixed_Comps and then In_Variable_Pos then
10940 -- Locate the first dynamic component of the record. Done to
10941 -- improve the text of the warning.
10943 declare
10944 Comp : Entity_Id;
10945 Comp_Typ : Entity_Id;
10947 begin
10948 Comp := First_Entity (Typ);
10949 while Present (Comp) loop
10950 Comp_Typ := Etype (Comp);
10952 if Ekind (Comp) /= E_Discriminant
10953 and then not Is_Tag (Comp)
10954 then
10955 exit when
10956 (Is_Record_Type (Comp_Typ)
10957 and then
10958 Is_Variable_Size_Record (Base_Type (Comp_Typ)))
10959 or else
10960 (Is_Array_Type (Comp_Typ)
10961 and then Is_Variable_Size_Array (Comp_Typ));
10962 end if;
10964 Next_Entity (Comp);
10965 end loop;
10967 pragma Assert (Present (Comp));
10969 -- Move this check to sem???
10970 Error_Msg_Node_2 := Comp;
10971 Error_Msg_NE
10972 ("parent type & with dynamic component & cannot be parent"
10973 & " of 'C'P'P derivation if new interfaces are present",
10974 Typ, Scope (Original_Record_Component (Comp)));
10976 Error_Msg_Sloc :=
10977 Sloc (Scope (Original_Record_Component (Comp)));
10978 Error_Msg_NE
10979 ("type derived from 'C'P'P type & defined #",
10980 Typ, Scope (Original_Record_Component (Comp)));
10982 -- Avoid duplicated warnings
10984 exit;
10985 end;
10987 -- Initialize secondary tags
10989 else
10990 Initialize_Tag
10991 (Typ => Full_Typ,
10992 Iface => Node (Iface_Elmt),
10993 Tag_Comp => Tag_Comp,
10994 Iface_Tag => Node (Iface_Tag_Elmt));
10995 end if;
10997 -- Otherwise generate code to initialize the tag
10999 else
11000 if (In_Variable_Pos and then Variable_Comps)
11001 or else (not In_Variable_Pos and then Fixed_Comps)
11002 then
11003 Initialize_Tag
11004 (Typ => Full_Typ,
11005 Iface => Node (Iface_Elmt),
11006 Tag_Comp => Tag_Comp,
11007 Iface_Tag => Node (Iface_Tag_Elmt));
11008 end if;
11009 end if;
11011 Next_Elmt (Iface_Elmt);
11012 Next_Elmt (Iface_Comp_Elmt);
11013 Next_Elmt (Iface_Tag_Elmt);
11014 end loop;
11015 end Init_Secondary_Tags;
11017 ----------------------------
11018 -- Is_Null_Statement_List --
11019 ----------------------------
11021 function Is_Null_Statement_List (Stmts : List_Id) return Boolean is
11022 Stmt : Node_Id;
11024 begin
11025 -- We must skip SCIL nodes because they may have been added to the list
11026 -- by Insert_Actions.
11028 Stmt := First_Non_SCIL_Node (Stmts);
11029 while Present (Stmt) loop
11030 if Nkind (Stmt) = N_Case_Statement then
11031 declare
11032 Alt : Node_Id;
11033 begin
11034 Alt := First (Alternatives (Stmt));
11035 while Present (Alt) loop
11036 if not Is_Null_Statement_List (Statements (Alt)) then
11037 return False;
11038 end if;
11040 Next (Alt);
11041 end loop;
11042 end;
11044 elsif Nkind (Stmt) /= N_Null_Statement then
11045 return False;
11046 end if;
11048 Stmt := Next_Non_SCIL_Node (Stmt);
11049 end loop;
11051 return True;
11052 end Is_Null_Statement_List;
11054 ----------------------------------------
11055 -- Make_Controlling_Function_Wrappers --
11056 ----------------------------------------
11058 procedure Make_Controlling_Function_Wrappers
11059 (Tag_Typ : Entity_Id;
11060 Decl_List : out List_Id;
11061 Body_List : out List_Id)
11063 Loc : constant Source_Ptr := Sloc (Tag_Typ);
11065 function Make_Wrapper_Specification (Subp : Entity_Id) return Node_Id;
11066 -- Returns a function specification with the same profile as Subp
11068 --------------------------------
11069 -- Make_Wrapper_Specification --
11070 --------------------------------
11072 function Make_Wrapper_Specification (Subp : Entity_Id) return Node_Id is
11073 begin
11074 return
11075 Make_Function_Specification (Loc,
11076 Defining_Unit_Name =>
11077 Make_Defining_Identifier (Loc,
11078 Chars => Chars (Subp)),
11079 Parameter_Specifications =>
11080 Copy_Parameter_List (Subp),
11081 Result_Definition =>
11082 New_Occurrence_Of (Etype (Subp), Loc));
11083 end Make_Wrapper_Specification;
11085 Prim_Elmt : Elmt_Id;
11086 Subp : Entity_Id;
11087 Actual_List : List_Id;
11088 Formal : Entity_Id;
11089 Par_Formal : Entity_Id;
11090 Ext_Aggr : Node_Id;
11091 Formal_Node : Node_Id;
11092 Func_Body : Node_Id;
11093 Func_Decl : Node_Id;
11094 Func_Id : Entity_Id;
11096 -- Start of processing for Make_Controlling_Function_Wrappers
11098 begin
11099 Decl_List := New_List;
11100 Body_List := New_List;
11102 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
11103 while Present (Prim_Elmt) loop
11104 Subp := Node (Prim_Elmt);
11106 -- If a primitive function with a controlling result of the type has
11107 -- not been overridden by the user, then we must create a wrapper
11108 -- function here that effectively overrides it and invokes the
11109 -- (non-abstract) parent function. This can only occur for a null
11110 -- extension. Note that functions with anonymous controlling access
11111 -- results don't qualify and must be overridden. We also exclude
11112 -- Input attributes, since each type will have its own version of
11113 -- Input constructed by the expander. The test for Comes_From_Source
11114 -- is needed to distinguish inherited operations from renamings
11115 -- (which also have Alias set). We exclude internal entities with
11116 -- Interface_Alias to avoid generating duplicated wrappers since
11117 -- the primitive which covers the interface is also available in
11118 -- the list of primitive operations.
11120 -- The function may be abstract, or require_Overriding may be set
11121 -- for it, because tests for null extensions may already have reset
11122 -- the Is_Abstract_Subprogram_Flag. If Requires_Overriding is not
11123 -- set, functions that need wrappers are recognized by having an
11124 -- alias that returns the parent type.
11126 if Comes_From_Source (Subp)
11127 or else No (Alias (Subp))
11128 or else Present (Interface_Alias (Subp))
11129 or else Ekind (Subp) /= E_Function
11130 or else not Has_Controlling_Result (Subp)
11131 or else Is_Access_Type (Etype (Subp))
11132 or else Is_Abstract_Subprogram (Alias (Subp))
11133 or else Is_TSS (Subp, TSS_Stream_Input)
11134 then
11135 goto Next_Prim;
11137 elsif Is_Abstract_Subprogram (Subp)
11138 or else Requires_Overriding (Subp)
11139 or else
11140 (Is_Null_Extension (Etype (Subp))
11141 and then Etype (Alias (Subp)) /= Etype (Subp))
11142 then
11143 -- If there is a non-overloadable homonym in the current
11144 -- scope, the implicit declaration remains invisible.
11145 -- We check the current entity with the same name, or its
11146 -- homonym in case the derivation takes place after the
11147 -- hiding object declaration.
11149 if Present (Current_Entity (Subp)) then
11150 declare
11151 Curr : constant Entity_Id := Current_Entity (Subp);
11152 Prev : constant Entity_Id := Homonym (Curr);
11153 begin
11154 if (Comes_From_Source (Curr)
11155 and then Scope (Curr) = Current_Scope
11156 and then not Is_Overloadable (Curr))
11157 or else
11158 (Present (Prev)
11159 and then Comes_From_Source (Prev)
11160 and then Scope (Prev) = Current_Scope
11161 and then not Is_Overloadable (Prev))
11162 then
11163 goto Next_Prim;
11164 end if;
11165 end;
11166 end if;
11168 Func_Decl :=
11169 Make_Subprogram_Declaration (Loc,
11170 Specification => Make_Wrapper_Specification (Subp));
11172 Append_To (Decl_List, Func_Decl);
11174 -- Build a wrapper body that calls the parent function. The body
11175 -- contains a single return statement that returns an extension
11176 -- aggregate whose ancestor part is a call to the parent function,
11177 -- passing the formals as actuals (with any controlling arguments
11178 -- converted to the types of the corresponding formals of the
11179 -- parent function, which might be anonymous access types), and
11180 -- having a null extension.
11182 Formal := First_Formal (Subp);
11183 Par_Formal := First_Formal (Alias (Subp));
11184 Formal_Node :=
11185 First (Parameter_Specifications (Specification (Func_Decl)));
11187 if Present (Formal) then
11188 Actual_List := New_List;
11190 while Present (Formal) loop
11191 if Is_Controlling_Formal (Formal) then
11192 Append_To (Actual_List,
11193 Make_Type_Conversion (Loc,
11194 Subtype_Mark =>
11195 New_Occurrence_Of (Etype (Par_Formal), Loc),
11196 Expression =>
11197 New_Occurrence_Of
11198 (Defining_Identifier (Formal_Node), Loc)));
11199 else
11200 Append_To
11201 (Actual_List,
11202 New_Occurrence_Of
11203 (Defining_Identifier (Formal_Node), Loc));
11204 end if;
11206 Next_Formal (Formal);
11207 Next_Formal (Par_Formal);
11208 Next (Formal_Node);
11209 end loop;
11210 else
11211 Actual_List := No_List;
11212 end if;
11214 Ext_Aggr :=
11215 Make_Extension_Aggregate (Loc,
11216 Ancestor_Part =>
11217 Make_Function_Call (Loc,
11218 Name =>
11219 New_Occurrence_Of (Alias (Subp), Loc),
11220 Parameter_Associations => Actual_List),
11221 Null_Record_Present => True);
11223 -- GNATprove will use expression of an expression function as an
11224 -- implicit postcondition. GNAT will also benefit from expression
11225 -- function to avoid premature freezing, but would struggle if we
11226 -- added an expression function to freezing actions, so we create
11227 -- the expanded form directly.
11229 if GNATprove_Mode then
11230 Func_Body :=
11231 Make_Expression_Function (Loc,
11232 Specification =>
11233 Make_Wrapper_Specification (Subp),
11234 Expression => Ext_Aggr);
11235 else
11236 Func_Body :=
11237 Make_Subprogram_Body (Loc,
11238 Specification =>
11239 Make_Wrapper_Specification (Subp),
11240 Declarations => Empty_List,
11241 Handled_Statement_Sequence =>
11242 Make_Handled_Sequence_Of_Statements (Loc,
11243 Statements => New_List (
11244 Make_Simple_Return_Statement (Loc,
11245 Expression => Ext_Aggr))));
11246 Set_Was_Expression_Function (Func_Body);
11247 end if;
11249 Append_To (Body_List, Func_Body);
11251 -- Replace the inherited function with the wrapper function in the
11252 -- primitive operations list. We add the minimum decoration needed
11253 -- to override interface primitives.
11255 Func_Id := Defining_Unit_Name (Specification (Func_Decl));
11257 Mutate_Ekind (Func_Id, E_Function);
11258 Set_Is_Wrapper (Func_Id);
11260 -- Corresponding_Spec will be set again to the same value during
11261 -- analysis, but we need this information earlier.
11262 -- Expand_N_Freeze_Entity needs to know whether a subprogram body
11263 -- is a wrapper's body in order to get check suppression right.
11265 Set_Corresponding_Spec (Func_Body, Func_Id);
11266 end if;
11268 <<Next_Prim>>
11269 Next_Elmt (Prim_Elmt);
11270 end loop;
11271 end Make_Controlling_Function_Wrappers;
11273 ------------------
11274 -- Make_Eq_Body --
11275 ------------------
11277 function Make_Eq_Body
11278 (Typ : Entity_Id;
11279 Eq_Name : Name_Id) return Node_Id
11281 Loc : constant Source_Ptr := Sloc (Parent (Typ));
11282 Decl : Node_Id;
11283 Def : constant Node_Id := Parent (Typ);
11284 Stmts : constant List_Id := New_List;
11285 Variant_Case : Boolean := Has_Discriminants (Typ);
11286 Comps : Node_Id := Empty;
11287 Typ_Def : Node_Id := Type_Definition (Def);
11289 begin
11290 Decl :=
11291 Predef_Spec_Or_Body (Loc,
11292 Tag_Typ => Typ,
11293 Name => Eq_Name,
11294 Profile => New_List (
11295 Make_Parameter_Specification (Loc,
11296 Defining_Identifier =>
11297 Make_Defining_Identifier (Loc, Name_X),
11298 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
11300 Make_Parameter_Specification (Loc,
11301 Defining_Identifier =>
11302 Make_Defining_Identifier (Loc, Name_Y),
11303 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
11305 Ret_Type => Standard_Boolean,
11306 For_Body => True);
11308 if Variant_Case then
11309 if Nkind (Typ_Def) = N_Derived_Type_Definition then
11310 Typ_Def := Record_Extension_Part (Typ_Def);
11311 end if;
11313 if Present (Typ_Def) then
11314 Comps := Component_List (Typ_Def);
11315 end if;
11317 Variant_Case :=
11318 Present (Comps) and then Present (Variant_Part (Comps));
11319 end if;
11321 if Variant_Case then
11322 Append_To (Stmts,
11323 Make_Eq_If (Typ, Discriminant_Specifications (Def)));
11324 Append_List_To (Stmts, Make_Eq_Case (Typ, Comps));
11325 Append_To (Stmts,
11326 Make_Simple_Return_Statement (Loc,
11327 Expression => New_Occurrence_Of (Standard_True, Loc)));
11329 else
11330 Append_To (Stmts,
11331 Make_Simple_Return_Statement (Loc,
11332 Expression =>
11333 Expand_Record_Equality
11334 (Typ,
11335 Typ => Typ,
11336 Lhs => Make_Identifier (Loc, Name_X),
11337 Rhs => Make_Identifier (Loc, Name_Y))));
11338 end if;
11340 Set_Handled_Statement_Sequence
11341 (Decl, Make_Handled_Sequence_Of_Statements (Loc, Stmts));
11342 return Decl;
11343 end Make_Eq_Body;
11345 ------------------
11346 -- Make_Eq_Case --
11347 ------------------
11349 -- <Make_Eq_If shared components>
11351 -- case X.D1 is
11352 -- when V1 => <Make_Eq_Case> on subcomponents
11353 -- ...
11354 -- when Vn => <Make_Eq_Case> on subcomponents
11355 -- end case;
11357 function Make_Eq_Case
11358 (E : Entity_Id;
11359 CL : Node_Id;
11360 Discrs : Elist_Id := New_Elmt_List) return List_Id
11362 Loc : constant Source_Ptr := Sloc (E);
11363 Result : constant List_Id := New_List;
11364 Variant : Node_Id;
11365 Alt_List : List_Id;
11367 function Corresponding_Formal (C : Node_Id) return Entity_Id;
11368 -- Given the discriminant that controls a given variant of an unchecked
11369 -- union, find the formal of the equality function that carries the
11370 -- inferred value of the discriminant.
11372 function External_Name (E : Entity_Id) return Name_Id;
11373 -- The value of a given discriminant is conveyed in the corresponding
11374 -- formal parameter of the equality routine. The name of this formal
11375 -- parameter carries a one-character suffix which is removed here.
11377 --------------------------
11378 -- Corresponding_Formal --
11379 --------------------------
11381 function Corresponding_Formal (C : Node_Id) return Entity_Id is
11382 Discr : constant Entity_Id := Entity (Name (Variant_Part (C)));
11383 Elm : Elmt_Id;
11385 begin
11386 Elm := First_Elmt (Discrs);
11387 while Present (Elm) loop
11388 if Chars (Discr) = External_Name (Node (Elm)) then
11389 return Node (Elm);
11390 end if;
11392 Next_Elmt (Elm);
11393 end loop;
11395 -- A formal of the proper name must be found
11397 raise Program_Error;
11398 end Corresponding_Formal;
11400 -------------------
11401 -- External_Name --
11402 -------------------
11404 function External_Name (E : Entity_Id) return Name_Id is
11405 begin
11406 Get_Name_String (Chars (E));
11407 Name_Len := Name_Len - 1;
11408 return Name_Find;
11409 end External_Name;
11411 -- Start of processing for Make_Eq_Case
11413 begin
11414 Append_To (Result, Make_Eq_If (E, Component_Items (CL)));
11416 if No (Variant_Part (CL)) then
11417 return Result;
11418 end if;
11420 Variant := First_Non_Pragma (Variants (Variant_Part (CL)));
11422 if No (Variant) then
11423 return Result;
11424 end if;
11426 Alt_List := New_List;
11427 while Present (Variant) loop
11428 Append_To (Alt_List,
11429 Make_Case_Statement_Alternative (Loc,
11430 Discrete_Choices => New_Copy_List (Discrete_Choices (Variant)),
11431 Statements =>
11432 Make_Eq_Case (E, Component_List (Variant), Discrs)));
11433 Next_Non_Pragma (Variant);
11434 end loop;
11436 -- If we have an Unchecked_Union, use one of the parameters of the
11437 -- enclosing equality routine that captures the discriminant, to use
11438 -- as the expression in the generated case statement.
11440 if Is_Unchecked_Union (E) then
11441 Append_To (Result,
11442 Make_Case_Statement (Loc,
11443 Expression =>
11444 New_Occurrence_Of (Corresponding_Formal (CL), Loc),
11445 Alternatives => Alt_List));
11447 else
11448 Append_To (Result,
11449 Make_Case_Statement (Loc,
11450 Expression =>
11451 Make_Selected_Component (Loc,
11452 Prefix => Make_Identifier (Loc, Name_X),
11453 Selector_Name => New_Copy (Name (Variant_Part (CL)))),
11454 Alternatives => Alt_List));
11455 end if;
11457 return Result;
11458 end Make_Eq_Case;
11460 ----------------
11461 -- Make_Eq_If --
11462 ----------------
11464 -- Generates:
11466 -- if
11467 -- X.C1 /= Y.C1
11468 -- or else
11469 -- X.C2 /= Y.C2
11470 -- ...
11471 -- then
11472 -- return False;
11473 -- end if;
11475 -- or a null statement if the list L is empty
11477 -- Equality may be user-defined for a given component type, in which case
11478 -- a function call is constructed instead of an operator node. This is an
11479 -- Ada 2012 change in the composability of equality for untagged composite
11480 -- types.
11482 function Make_Eq_If
11483 (E : Entity_Id;
11484 L : List_Id) return Node_Id
11486 Loc : constant Source_Ptr := Sloc (E);
11488 C : Node_Id;
11489 Cond : Node_Id;
11490 Field_Name : Name_Id;
11491 Next_Test : Node_Id;
11492 Typ : Entity_Id;
11494 begin
11495 if No (L) then
11496 return Make_Null_Statement (Loc);
11498 else
11499 Cond := Empty;
11501 C := First_Non_Pragma (L);
11502 while Present (C) loop
11503 Typ := Etype (Defining_Identifier (C));
11504 Field_Name := Chars (Defining_Identifier (C));
11506 -- The tags must not be compared: they are not part of the value.
11507 -- Ditto for parent interfaces because their equality operator is
11508 -- abstract.
11510 -- Note also that in the following, we use Make_Identifier for
11511 -- the component names. Use of New_Occurrence_Of to identify the
11512 -- components would be incorrect because the wrong entities for
11513 -- discriminants could be picked up in the private type case.
11515 if Field_Name = Name_uParent
11516 and then Is_Interface (Typ)
11517 then
11518 null;
11520 elsif Field_Name /= Name_uTag then
11521 declare
11522 Lhs : constant Node_Id :=
11523 Make_Selected_Component (Loc,
11524 Prefix => Make_Identifier (Loc, Name_X),
11525 Selector_Name => Make_Identifier (Loc, Field_Name));
11527 Rhs : constant Node_Id :=
11528 Make_Selected_Component (Loc,
11529 Prefix => Make_Identifier (Loc, Name_Y),
11530 Selector_Name => Make_Identifier (Loc, Field_Name));
11531 Eq_Call : Node_Id;
11533 begin
11534 -- Build equality code with a user-defined operator, if
11535 -- available, and with the predefined "=" otherwise. For
11536 -- compatibility with older Ada versions, we also use the
11537 -- predefined operation if the component-type equality is
11538 -- abstract, rather than raising Program_Error.
11540 if Ada_Version < Ada_2012 then
11541 Next_Test := Make_Op_Ne (Loc, Lhs, Rhs);
11543 else
11544 Eq_Call := Build_Eq_Call (Typ, Loc, Lhs, Rhs);
11546 if No (Eq_Call) then
11547 Next_Test := Make_Op_Ne (Loc, Lhs, Rhs);
11549 -- If a component has a defined abstract equality, its
11550 -- application raises Program_Error on that component
11551 -- and therefore on the current variant.
11553 elsif Nkind (Eq_Call) = N_Raise_Program_Error then
11554 Set_Etype (Eq_Call, Standard_Boolean);
11555 Next_Test := Make_Op_Not (Loc, Eq_Call);
11557 else
11558 Next_Test := Make_Op_Not (Loc, Eq_Call);
11559 end if;
11560 end if;
11561 end;
11563 Evolve_Or_Else (Cond, Next_Test);
11564 end if;
11566 Next_Non_Pragma (C);
11567 end loop;
11569 if No (Cond) then
11570 return Make_Null_Statement (Loc);
11572 else
11573 return
11574 Make_Implicit_If_Statement (E,
11575 Condition => Cond,
11576 Then_Statements => New_List (
11577 Make_Simple_Return_Statement (Loc,
11578 Expression => New_Occurrence_Of (Standard_False, Loc))));
11579 end if;
11580 end if;
11581 end Make_Eq_If;
11583 -------------------
11584 -- Make_Neq_Body --
11585 -------------------
11587 function Make_Neq_Body (Tag_Typ : Entity_Id) return Node_Id is
11589 function Is_Predefined_Neq_Renaming (Prim : Node_Id) return Boolean;
11590 -- Returns true if Prim is a renaming of an unresolved predefined
11591 -- inequality operation.
11593 --------------------------------
11594 -- Is_Predefined_Neq_Renaming --
11595 --------------------------------
11597 function Is_Predefined_Neq_Renaming (Prim : Node_Id) return Boolean is
11598 begin
11599 return Chars (Prim) /= Name_Op_Ne
11600 and then Present (Alias (Prim))
11601 and then Comes_From_Source (Prim)
11602 and then Is_Intrinsic_Subprogram (Alias (Prim))
11603 and then Chars (Alias (Prim)) = Name_Op_Ne;
11604 end Is_Predefined_Neq_Renaming;
11606 -- Local variables
11608 Loc : constant Source_Ptr := Sloc (Parent (Tag_Typ));
11609 Decl : Node_Id;
11610 Eq_Prim : Entity_Id;
11611 Left_Op : Entity_Id;
11612 Renaming_Prim : Entity_Id;
11613 Right_Op : Entity_Id;
11614 Target : Entity_Id;
11616 -- Start of processing for Make_Neq_Body
11618 begin
11619 -- For a call on a renaming of a dispatching subprogram that is
11620 -- overridden, if the overriding occurred before the renaming, then
11621 -- the body executed is that of the overriding declaration, even if the
11622 -- overriding declaration is not visible at the place of the renaming;
11623 -- otherwise, the inherited or predefined subprogram is called, see
11624 -- (RM 8.5.4(8)).
11626 -- Stage 1: Search for a renaming of the inequality primitive and also
11627 -- search for an overriding of the equality primitive located before the
11628 -- renaming declaration.
11630 declare
11631 Elmt : Elmt_Id;
11632 Prim : Node_Id;
11634 begin
11635 Eq_Prim := Empty;
11636 Renaming_Prim := Empty;
11638 Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
11639 while Present (Elmt) loop
11640 Prim := Node (Elmt);
11642 if Is_User_Defined_Equality (Prim) and then No (Alias (Prim)) then
11643 if No (Renaming_Prim) then
11644 pragma Assert (No (Eq_Prim));
11645 Eq_Prim := Prim;
11646 end if;
11648 elsif Is_Predefined_Neq_Renaming (Prim) then
11649 Renaming_Prim := Prim;
11650 end if;
11652 Next_Elmt (Elmt);
11653 end loop;
11654 end;
11656 -- No further action needed if no renaming was found
11658 if No (Renaming_Prim) then
11659 return Empty;
11660 end if;
11662 -- Stage 2: Replace the renaming declaration by a subprogram declaration
11663 -- (required to add its body)
11665 Decl := Parent (Parent (Renaming_Prim));
11666 Rewrite (Decl,
11667 Make_Subprogram_Declaration (Loc,
11668 Specification => Specification (Decl)));
11669 Set_Analyzed (Decl);
11671 -- Remove the decoration of intrinsic renaming subprogram
11673 Set_Is_Intrinsic_Subprogram (Renaming_Prim, False);
11674 Set_Convention (Renaming_Prim, Convention_Ada);
11675 Set_Alias (Renaming_Prim, Empty);
11676 Set_Has_Completion (Renaming_Prim, False);
11678 -- Stage 3: Build the corresponding body
11680 Left_Op := First_Formal (Renaming_Prim);
11681 Right_Op := Next_Formal (Left_Op);
11683 Decl :=
11684 Predef_Spec_Or_Body (Loc,
11685 Tag_Typ => Tag_Typ,
11686 Name => Chars (Renaming_Prim),
11687 Profile => New_List (
11688 Make_Parameter_Specification (Loc,
11689 Defining_Identifier =>
11690 Make_Defining_Identifier (Loc, Chars (Left_Op)),
11691 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
11693 Make_Parameter_Specification (Loc,
11694 Defining_Identifier =>
11695 Make_Defining_Identifier (Loc, Chars (Right_Op)),
11696 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
11698 Ret_Type => Standard_Boolean,
11699 For_Body => True);
11701 -- If the overriding of the equality primitive occurred before the
11702 -- renaming, then generate:
11704 -- function <Neq_Name> (X : Y : Typ) return Boolean is
11705 -- begin
11706 -- return not Oeq (X, Y);
11707 -- end;
11709 if Present (Eq_Prim) then
11710 Target := Eq_Prim;
11712 -- Otherwise build a nested subprogram which performs the predefined
11713 -- evaluation of the equality operator. That is, generate:
11715 -- function <Neq_Name> (X : Y : Typ) return Boolean is
11716 -- function Oeq (X : Y) return Boolean is
11717 -- begin
11718 -- <<body of default implementation>>
11719 -- end;
11720 -- begin
11721 -- return not Oeq (X, Y);
11722 -- end;
11724 else
11725 declare
11726 Local_Subp : Node_Id;
11727 begin
11728 Local_Subp := Make_Eq_Body (Tag_Typ, Name_Op_Eq);
11729 Set_Declarations (Decl, New_List (Local_Subp));
11730 Target := Defining_Entity (Local_Subp);
11731 end;
11732 end if;
11734 Set_Handled_Statement_Sequence
11735 (Decl,
11736 Make_Handled_Sequence_Of_Statements (Loc, New_List (
11737 Make_Simple_Return_Statement (Loc,
11738 Expression =>
11739 Make_Op_Not (Loc,
11740 Make_Function_Call (Loc,
11741 Name => New_Occurrence_Of (Target, Loc),
11742 Parameter_Associations => New_List (
11743 Make_Identifier (Loc, Chars (Left_Op)),
11744 Make_Identifier (Loc, Chars (Right_Op)))))))));
11746 return Decl;
11747 end Make_Neq_Body;
11749 -------------------------------
11750 -- Make_Null_Procedure_Specs --
11751 -------------------------------
11753 function Make_Null_Procedure_Specs (Tag_Typ : Entity_Id) return List_Id is
11754 Decl_List : constant List_Id := New_List;
11755 Loc : constant Source_Ptr := Sloc (Tag_Typ);
11756 Formal : Entity_Id;
11757 New_Param_Spec : Node_Id;
11758 New_Spec : Node_Id;
11759 Parent_Subp : Entity_Id;
11760 Prim_Elmt : Elmt_Id;
11761 Subp : Entity_Id;
11763 begin
11764 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
11765 while Present (Prim_Elmt) loop
11766 Subp := Node (Prim_Elmt);
11768 -- If a null procedure inherited from an interface has not been
11769 -- overridden, then we build a null procedure declaration to
11770 -- override the inherited procedure.
11772 Parent_Subp := Alias (Subp);
11774 if Present (Parent_Subp)
11775 and then Is_Null_Interface_Primitive (Parent_Subp)
11776 then
11777 -- The null procedure spec is copied from the inherited procedure,
11778 -- except for the IS NULL (which must be added) and the overriding
11779 -- indicators (which must be removed, if present).
11781 New_Spec :=
11782 Copy_Subprogram_Spec (Subprogram_Specification (Subp), Loc);
11784 Set_Null_Present (New_Spec, True);
11785 Set_Must_Override (New_Spec, False);
11786 Set_Must_Not_Override (New_Spec, False);
11788 Formal := First_Formal (Subp);
11789 New_Param_Spec := First (Parameter_Specifications (New_Spec));
11791 while Present (Formal) loop
11793 -- For controlling arguments we must change their parameter
11794 -- type to reference the tagged type (instead of the interface
11795 -- type).
11797 if Is_Controlling_Formal (Formal) then
11798 if Nkind (Parameter_Type (Parent (Formal))) = N_Identifier
11799 then
11800 Set_Parameter_Type (New_Param_Spec,
11801 New_Occurrence_Of (Tag_Typ, Loc));
11803 else pragma Assert
11804 (Nkind (Parameter_Type (Parent (Formal))) =
11805 N_Access_Definition);
11806 Set_Subtype_Mark (Parameter_Type (New_Param_Spec),
11807 New_Occurrence_Of (Tag_Typ, Loc));
11808 end if;
11809 end if;
11811 Next_Formal (Formal);
11812 Next (New_Param_Spec);
11813 end loop;
11815 Append_To (Decl_List,
11816 Make_Subprogram_Declaration (Loc,
11817 Specification => New_Spec));
11818 end if;
11820 Next_Elmt (Prim_Elmt);
11821 end loop;
11823 return Decl_List;
11824 end Make_Null_Procedure_Specs;
11826 ---------------------------------------
11827 -- Make_Predefined_Primitive_Eq_Spec --
11828 ---------------------------------------
11830 procedure Make_Predefined_Primitive_Eq_Spec
11831 (Tag_Typ : Entity_Id;
11832 Predef_List : List_Id;
11833 Renamed_Eq : out Entity_Id)
11835 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean;
11836 -- Returns true if Prim is a renaming of an unresolved predefined
11837 -- equality operation.
11839 -------------------------------
11840 -- Is_Predefined_Eq_Renaming --
11841 -------------------------------
11843 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean is
11844 begin
11845 return Chars (Prim) /= Name_Op_Eq
11846 and then Present (Alias (Prim))
11847 and then Comes_From_Source (Prim)
11848 and then Is_Intrinsic_Subprogram (Alias (Prim))
11849 and then Chars (Alias (Prim)) = Name_Op_Eq;
11850 end Is_Predefined_Eq_Renaming;
11852 -- Local variables
11854 Loc : constant Source_Ptr := Sloc (Tag_Typ);
11856 Eq_Name : Name_Id := Name_Op_Eq;
11857 Eq_Needed : Boolean := True;
11858 Eq_Spec : Node_Id;
11859 Prim : Elmt_Id;
11861 Has_Predef_Eq_Renaming : Boolean := False;
11862 -- Set to True if Tag_Typ has a primitive that renames the predefined
11863 -- equality operator. Used to implement (RM 8-5-4(8)).
11865 -- Start of processing for Make_Predefined_Primitive_Specs
11867 begin
11868 Renamed_Eq := Empty;
11870 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
11871 while Present (Prim) loop
11873 -- If a primitive is encountered that renames the predefined equality
11874 -- operator before reaching any explicit equality primitive, then we
11875 -- still need to create a predefined equality function, because calls
11876 -- to it can occur via the renaming. A new name is created for the
11877 -- equality to avoid conflicting with any user-defined equality.
11878 -- (Note that this doesn't account for renamings of equality nested
11879 -- within subpackages???)
11881 if Is_Predefined_Eq_Renaming (Node (Prim)) then
11882 Has_Predef_Eq_Renaming := True;
11883 Eq_Name := New_External_Name (Chars (Node (Prim)), 'E');
11885 -- User-defined equality
11887 elsif Is_User_Defined_Equality (Node (Prim)) then
11888 if No (Alias (Node (Prim)))
11889 or else Nkind (Unit_Declaration_Node (Node (Prim))) =
11890 N_Subprogram_Renaming_Declaration
11891 then
11892 Eq_Needed := False;
11893 exit;
11895 -- If the parent is not an interface type and has an abstract
11896 -- equality function explicitly defined in the sources, then the
11897 -- inherited equality is abstract as well, and no body can be
11898 -- created for it.
11900 elsif not Is_Interface (Etype (Tag_Typ))
11901 and then Present (Alias (Node (Prim)))
11902 and then Comes_From_Source (Alias (Node (Prim)))
11903 and then Is_Abstract_Subprogram (Alias (Node (Prim)))
11904 then
11905 Eq_Needed := False;
11906 exit;
11908 -- If the type has an equality function corresponding with a
11909 -- primitive defined in an interface type, the inherited equality
11910 -- is abstract as well, and no body can be created for it.
11912 elsif Present (Alias (Node (Prim)))
11913 and then Comes_From_Source (Ultimate_Alias (Node (Prim)))
11914 and then
11915 Is_Interface
11916 (Find_Dispatching_Type (Ultimate_Alias (Node (Prim))))
11917 then
11918 Eq_Needed := False;
11919 exit;
11920 end if;
11921 end if;
11923 Next_Elmt (Prim);
11924 end loop;
11926 -- If a renaming of predefined equality was found but there was no
11927 -- user-defined equality (so Eq_Needed is still true), then set the name
11928 -- back to Name_Op_Eq. But in the case where a user-defined equality was
11929 -- located after such a renaming, then the predefined equality function
11930 -- is still needed, so Eq_Needed must be set back to True.
11932 if Eq_Name /= Name_Op_Eq then
11933 if Eq_Needed then
11934 Eq_Name := Name_Op_Eq;
11935 else
11936 Eq_Needed := True;
11937 end if;
11938 end if;
11940 if Eq_Needed then
11941 Eq_Spec := Predef_Spec_Or_Body (Loc,
11942 Tag_Typ => Tag_Typ,
11943 Name => Eq_Name,
11944 Profile => New_List (
11945 Make_Parameter_Specification (Loc,
11946 Defining_Identifier =>
11947 Make_Defining_Identifier (Loc, Name_X),
11948 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
11950 Make_Parameter_Specification (Loc,
11951 Defining_Identifier =>
11952 Make_Defining_Identifier (Loc, Name_Y),
11953 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
11954 Ret_Type => Standard_Boolean);
11955 Append_To (Predef_List, Eq_Spec);
11957 if Has_Predef_Eq_Renaming then
11958 Renamed_Eq := Defining_Unit_Name (Specification (Eq_Spec));
11960 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
11961 while Present (Prim) loop
11963 -- Any renamings of equality that appeared before an overriding
11964 -- equality must be updated to refer to the entity for the
11965 -- predefined equality, otherwise calls via the renaming would
11966 -- get incorrectly resolved to call the user-defined equality
11967 -- function.
11969 if Is_Predefined_Eq_Renaming (Node (Prim)) then
11970 Set_Alias (Node (Prim), Renamed_Eq);
11972 -- Exit upon encountering a user-defined equality
11974 elsif Chars (Node (Prim)) = Name_Op_Eq
11975 and then No (Alias (Node (Prim)))
11976 then
11977 exit;
11978 end if;
11980 Next_Elmt (Prim);
11981 end loop;
11982 end if;
11983 end if;
11984 end Make_Predefined_Primitive_Eq_Spec;
11986 -------------------------------------
11987 -- Make_Predefined_Primitive_Specs --
11988 -------------------------------------
11990 procedure Make_Predefined_Primitive_Specs
11991 (Tag_Typ : Entity_Id;
11992 Predef_List : out List_Id;
11993 Renamed_Eq : out Entity_Id)
11995 Loc : constant Source_Ptr := Sloc (Tag_Typ);
11996 Res : constant List_Id := New_List;
11998 use Exp_Put_Image;
12000 begin
12001 Renamed_Eq := Empty;
12003 -- Spec of _Size
12005 Append_To (Res, Predef_Spec_Or_Body (Loc,
12006 Tag_Typ => Tag_Typ,
12007 Name => Name_uSize,
12008 Profile => New_List (
12009 Make_Parameter_Specification (Loc,
12010 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
12011 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
12013 Ret_Type => Standard_Long_Long_Integer));
12015 -- Spec of Put_Image
12017 if not No_Run_Time_Mode
12018 and then RTE_Available (RE_Root_Buffer_Type)
12019 then
12020 -- No_Run_Time_Mode implies that the declaration of Tag_Typ
12021 -- (like any tagged type) will be rejected. Given this, avoid
12022 -- cascading errors associated with the Tag_Typ's TSS_Put_Image
12023 -- procedure.
12025 Append_To (Res, Predef_Spec_Or_Body (Loc,
12026 Tag_Typ => Tag_Typ,
12027 Name => Make_TSS_Name (Tag_Typ, TSS_Put_Image),
12028 Profile => Build_Put_Image_Profile (Loc, Tag_Typ)));
12029 end if;
12031 -- Specs for dispatching stream attributes
12033 declare
12034 Stream_Op_TSS_Names :
12035 constant array (Positive range <>) of TSS_Name_Type :=
12036 (TSS_Stream_Read,
12037 TSS_Stream_Write,
12038 TSS_Stream_Input,
12039 TSS_Stream_Output);
12041 begin
12042 for Op in Stream_Op_TSS_Names'Range loop
12043 if Stream_Operation_OK (Tag_Typ, Stream_Op_TSS_Names (Op)) then
12044 Append_To (Res,
12045 Predef_Stream_Attr_Spec (Loc, Tag_Typ,
12046 Stream_Op_TSS_Names (Op)));
12047 end if;
12048 end loop;
12049 end;
12051 -- Spec of "=" is expanded if the type is not limited and if a user
12052 -- defined "=" was not already declared for the non-full view of a
12053 -- private extension.
12055 if not Is_Limited_Type (Tag_Typ) then
12056 Make_Predefined_Primitive_Eq_Spec (Tag_Typ, Res, Renamed_Eq);
12058 -- Spec for dispatching assignment
12060 Append_To (Res, Predef_Spec_Or_Body (Loc,
12061 Tag_Typ => Tag_Typ,
12062 Name => Name_uAssign,
12063 Profile => New_List (
12064 Make_Parameter_Specification (Loc,
12065 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
12066 Out_Present => True,
12067 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
12069 Make_Parameter_Specification (Loc,
12070 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
12071 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)))));
12072 end if;
12074 -- Ada 2005: Generate declarations for the following primitive
12075 -- operations for limited interfaces and synchronized types that
12076 -- implement a limited interface.
12078 -- Disp_Asynchronous_Select
12079 -- Disp_Conditional_Select
12080 -- Disp_Get_Prim_Op_Kind
12081 -- Disp_Get_Task_Id
12082 -- Disp_Requeue
12083 -- Disp_Timed_Select
12085 -- Disable the generation of these bodies if Ravenscar or ZFP is active
12087 if Ada_Version >= Ada_2005
12088 and then not Restriction_Active (No_Select_Statements)
12089 and then RTE_Available (RE_Select_Specific_Data)
12090 then
12091 -- These primitives are defined abstract in interface types
12093 if Is_Interface (Tag_Typ)
12094 and then Is_Limited_Record (Tag_Typ)
12095 then
12096 Append_To (Res,
12097 Make_Abstract_Subprogram_Declaration (Loc,
12098 Specification =>
12099 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
12101 Append_To (Res,
12102 Make_Abstract_Subprogram_Declaration (Loc,
12103 Specification =>
12104 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
12106 Append_To (Res,
12107 Make_Abstract_Subprogram_Declaration (Loc,
12108 Specification =>
12109 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
12111 Append_To (Res,
12112 Make_Abstract_Subprogram_Declaration (Loc,
12113 Specification =>
12114 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
12116 Append_To (Res,
12117 Make_Abstract_Subprogram_Declaration (Loc,
12118 Specification =>
12119 Make_Disp_Requeue_Spec (Tag_Typ)));
12121 Append_To (Res,
12122 Make_Abstract_Subprogram_Declaration (Loc,
12123 Specification =>
12124 Make_Disp_Timed_Select_Spec (Tag_Typ)));
12126 -- If ancestor is an interface type, declare non-abstract primitives
12127 -- to override the abstract primitives of the interface type.
12129 -- In VM targets we define these primitives in all root tagged types
12130 -- that are not interface types. Done because in VM targets we don't
12131 -- have secondary dispatch tables and any derivation of Tag_Typ may
12132 -- cover limited interfaces (which always have these primitives since
12133 -- they may be ancestors of synchronized interface types).
12135 elsif (not Is_Interface (Tag_Typ)
12136 and then Is_Interface (Etype (Tag_Typ))
12137 and then Is_Limited_Record (Etype (Tag_Typ)))
12138 or else
12139 (Is_Concurrent_Record_Type (Tag_Typ)
12140 and then Has_Interfaces (Tag_Typ))
12141 or else
12142 (not Tagged_Type_Expansion
12143 and then not Is_Interface (Tag_Typ)
12144 and then Tag_Typ = Root_Type (Tag_Typ))
12145 then
12146 Append_To (Res,
12147 Make_Subprogram_Declaration (Loc,
12148 Specification =>
12149 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
12151 Append_To (Res,
12152 Make_Subprogram_Declaration (Loc,
12153 Specification =>
12154 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
12156 Append_To (Res,
12157 Make_Subprogram_Declaration (Loc,
12158 Specification =>
12159 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
12161 Append_To (Res,
12162 Make_Subprogram_Declaration (Loc,
12163 Specification =>
12164 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
12166 Append_To (Res,
12167 Make_Subprogram_Declaration (Loc,
12168 Specification =>
12169 Make_Disp_Requeue_Spec (Tag_Typ)));
12171 Append_To (Res,
12172 Make_Subprogram_Declaration (Loc,
12173 Specification =>
12174 Make_Disp_Timed_Select_Spec (Tag_Typ)));
12175 end if;
12176 end if;
12178 -- All tagged types receive their own Deep_Adjust and Deep_Finalize
12179 -- regardless of whether they are controlled or may contain controlled
12180 -- components.
12182 -- Do not generate the routines if finalization is disabled
12184 if Restriction_Active (No_Finalization) then
12185 null;
12187 else
12188 if not Is_Limited_Type (Tag_Typ) then
12189 Append_To (Res, Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust));
12190 end if;
12192 Append_To (Res, Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize));
12193 end if;
12195 Predef_List := Res;
12196 end Make_Predefined_Primitive_Specs;
12198 -------------------------
12199 -- Make_Tag_Assignment --
12200 -------------------------
12202 function Make_Tag_Assignment (N : Node_Id) return Node_Id is
12203 Loc : constant Source_Ptr := Sloc (N);
12204 Def_Id : constant Entity_Id := Defining_Identifier (N);
12205 Expr : constant Node_Id := Expression (N);
12206 Typ : constant Entity_Id := Etype (Def_Id);
12207 Full_Typ : constant Entity_Id := Underlying_Type (Typ);
12209 begin
12210 -- This expansion activity is called during analysis
12212 if Is_Tagged_Type (Typ)
12213 and then not Is_Class_Wide_Type (Typ)
12214 and then not Is_CPP_Class (Typ)
12215 and then Tagged_Type_Expansion
12216 and then Nkind (Unqualify (Expr)) /= N_Aggregate
12217 then
12218 return
12219 Make_Tag_Assignment_From_Type
12220 (Loc, New_Occurrence_Of (Def_Id, Loc), Full_Typ);
12222 else
12223 return Empty;
12224 end if;
12225 end Make_Tag_Assignment;
12227 ----------------------
12228 -- Predef_Deep_Spec --
12229 ----------------------
12231 function Predef_Deep_Spec
12232 (Loc : Source_Ptr;
12233 Tag_Typ : Entity_Id;
12234 Name : TSS_Name_Type;
12235 For_Body : Boolean := False) return Node_Id
12237 Formals : List_Id;
12239 begin
12240 -- V : in out Tag_Typ
12242 Formals := New_List (
12243 Make_Parameter_Specification (Loc,
12244 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
12245 In_Present => True,
12246 Out_Present => True,
12247 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)));
12249 -- F : Boolean := True
12251 if Name = TSS_Deep_Adjust
12252 or else Name = TSS_Deep_Finalize
12253 then
12254 Append_To (Formals,
12255 Make_Parameter_Specification (Loc,
12256 Defining_Identifier => Make_Defining_Identifier (Loc, Name_F),
12257 Parameter_Type => New_Occurrence_Of (Standard_Boolean, Loc),
12258 Expression => New_Occurrence_Of (Standard_True, Loc)));
12259 end if;
12261 return
12262 Predef_Spec_Or_Body (Loc,
12263 Name => Make_TSS_Name (Tag_Typ, Name),
12264 Tag_Typ => Tag_Typ,
12265 Profile => Formals,
12266 For_Body => For_Body);
12268 exception
12269 when RE_Not_Available =>
12270 return Empty;
12271 end Predef_Deep_Spec;
12273 -------------------------
12274 -- Predef_Spec_Or_Body --
12275 -------------------------
12277 function Predef_Spec_Or_Body
12278 (Loc : Source_Ptr;
12279 Tag_Typ : Entity_Id;
12280 Name : Name_Id;
12281 Profile : List_Id;
12282 Ret_Type : Entity_Id := Empty;
12283 For_Body : Boolean := False) return Node_Id
12285 Id : constant Entity_Id := Make_Defining_Identifier (Loc, Name);
12286 Spec : Node_Id;
12288 begin
12289 Set_Is_Public (Id, Is_Public (Tag_Typ));
12291 -- The internal flag is set to mark these declarations because they have
12292 -- specific properties. First, they are primitives even if they are not
12293 -- defined in the type scope (the freezing point is not necessarily in
12294 -- the same scope). Second, the predefined equality can be overridden by
12295 -- a user-defined equality, no body will be generated in this case.
12297 Set_Is_Internal (Id);
12299 if not Debug_Generated_Code then
12300 Set_Debug_Info_Off (Id);
12301 end if;
12303 if No (Ret_Type) then
12304 Spec :=
12305 Make_Procedure_Specification (Loc,
12306 Defining_Unit_Name => Id,
12307 Parameter_Specifications => Profile);
12308 else
12309 Spec :=
12310 Make_Function_Specification (Loc,
12311 Defining_Unit_Name => Id,
12312 Parameter_Specifications => Profile,
12313 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
12314 end if;
12316 -- Declare an abstract subprogram for primitive subprograms of an
12317 -- interface type (except for "=").
12319 if Is_Interface (Tag_Typ) then
12320 if Name /= Name_Op_Eq then
12321 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
12323 -- The equality function (if any) for an interface type is defined
12324 -- to be nonabstract, so we create an expression function for it that
12325 -- always returns False. Note that the function can never actually be
12326 -- invoked because interface types are abstract, so there aren't any
12327 -- objects of such types (and their equality operation will always
12328 -- dispatch).
12330 else
12331 return Make_Expression_Function
12332 (Loc, Spec, New_Occurrence_Of (Standard_False, Loc));
12333 end if;
12335 -- If body case, return empty subprogram body. Note that this is ill-
12336 -- formed, because there is not even a null statement, and certainly not
12337 -- a return in the function case. The caller is expected to do surgery
12338 -- on the body to add the appropriate stuff.
12340 elsif For_Body then
12341 return Make_Subprogram_Body (Loc, Spec, Empty_List, Empty);
12343 -- For the case of an Input attribute predefined for an abstract type,
12344 -- generate an abstract specification. This will never be called, but we
12345 -- need the slot allocated in the dispatching table so that attributes
12346 -- typ'Class'Input and typ'Class'Output will work properly.
12348 elsif Is_TSS (Name, TSS_Stream_Input)
12349 and then Is_Abstract_Type (Tag_Typ)
12350 then
12351 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
12353 -- Normal spec case, where we return a subprogram declaration
12355 else
12356 return Make_Subprogram_Declaration (Loc, Spec);
12357 end if;
12358 end Predef_Spec_Or_Body;
12360 -----------------------------
12361 -- Predef_Stream_Attr_Spec --
12362 -----------------------------
12364 function Predef_Stream_Attr_Spec
12365 (Loc : Source_Ptr;
12366 Tag_Typ : Entity_Id;
12367 Name : TSS_Name_Type) return Node_Id
12369 Ret_Type : Entity_Id;
12371 begin
12372 if Name = TSS_Stream_Input then
12373 Ret_Type := Tag_Typ;
12374 else
12375 Ret_Type := Empty;
12376 end if;
12378 return
12379 Predef_Spec_Or_Body
12380 (Loc,
12381 Name => Make_TSS_Name (Tag_Typ, Name),
12382 Tag_Typ => Tag_Typ,
12383 Profile => Build_Stream_Attr_Profile (Loc, Tag_Typ, Name),
12384 Ret_Type => Ret_Type,
12385 For_Body => False);
12386 end Predef_Stream_Attr_Spec;
12388 ----------------------------------
12389 -- Predefined_Primitive_Eq_Body --
12390 ----------------------------------
12392 procedure Predefined_Primitive_Eq_Body
12393 (Tag_Typ : Entity_Id;
12394 Predef_List : List_Id;
12395 Renamed_Eq : Entity_Id)
12397 Decl : Node_Id;
12398 Eq_Needed : Boolean;
12399 Eq_Name : Name_Id;
12400 Prim : Elmt_Id;
12402 begin
12403 -- See if we have a predefined "=" operator
12405 if Present (Renamed_Eq) then
12406 Eq_Needed := True;
12407 Eq_Name := Chars (Renamed_Eq);
12409 -- If the parent is an interface type then it has defined all the
12410 -- predefined primitives abstract and we need to check if the type
12411 -- has some user defined "=" function which matches the profile of
12412 -- the Ada predefined equality operator to avoid generating it.
12414 elsif Is_Interface (Etype (Tag_Typ)) then
12415 Eq_Needed := True;
12416 Eq_Name := Name_Op_Eq;
12418 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
12419 while Present (Prim) loop
12420 if Is_User_Defined_Equality (Node (Prim))
12421 and then not Is_Internal (Node (Prim))
12422 then
12423 Eq_Needed := False;
12424 Eq_Name := No_Name;
12425 exit;
12426 end if;
12428 Next_Elmt (Prim);
12429 end loop;
12431 else
12432 Eq_Needed := False;
12433 Eq_Name := No_Name;
12435 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
12436 while Present (Prim) loop
12437 if Is_User_Defined_Equality (Node (Prim))
12438 and then Is_Internal (Node (Prim))
12439 then
12440 Eq_Needed := True;
12441 Eq_Name := Name_Op_Eq;
12442 exit;
12443 end if;
12445 Next_Elmt (Prim);
12446 end loop;
12447 end if;
12449 -- If equality is needed, we will have its name
12451 pragma Assert (Eq_Needed = Present (Eq_Name));
12453 -- Body for equality
12455 if Eq_Needed then
12456 Decl := Make_Eq_Body (Tag_Typ, Eq_Name);
12457 Append_To (Predef_List, Decl);
12458 end if;
12460 -- Body for inequality (if required)
12462 Decl := Make_Neq_Body (Tag_Typ);
12464 if Present (Decl) then
12465 Append_To (Predef_List, Decl);
12466 end if;
12467 end Predefined_Primitive_Eq_Body;
12469 ---------------------------------
12470 -- Predefined_Primitive_Bodies --
12471 ---------------------------------
12473 function Predefined_Primitive_Bodies
12474 (Tag_Typ : Entity_Id;
12475 Renamed_Eq : Entity_Id) return List_Id
12477 Loc : constant Source_Ptr := Sloc (Tag_Typ);
12478 Res : constant List_Id := New_List;
12479 Adj_Call : Node_Id;
12480 Decl : Node_Id;
12481 Fin_Call : Node_Id;
12482 Ent : Entity_Id;
12484 pragma Warnings (Off, Ent);
12486 use Exp_Put_Image;
12488 begin
12489 pragma Assert (not Is_Interface (Tag_Typ));
12491 -- Body of _Size
12493 Decl := Predef_Spec_Or_Body (Loc,
12494 Tag_Typ => Tag_Typ,
12495 Name => Name_uSize,
12496 Profile => New_List (
12497 Make_Parameter_Specification (Loc,
12498 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
12499 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
12501 Ret_Type => Standard_Long_Long_Integer,
12502 For_Body => True);
12504 Set_Handled_Statement_Sequence (Decl,
12505 Make_Handled_Sequence_Of_Statements (Loc, New_List (
12506 Make_Simple_Return_Statement (Loc,
12507 Expression =>
12508 Make_Attribute_Reference (Loc,
12509 Prefix => Make_Identifier (Loc, Name_X),
12510 Attribute_Name => Name_Size)))));
12512 Append_To (Res, Decl);
12514 -- Body of Put_Image
12516 if No (TSS (Tag_Typ, TSS_Put_Image))
12517 and then not No_Run_Time_Mode
12518 and then RTE_Available (RE_Root_Buffer_Type)
12519 then
12520 Build_Record_Put_Image_Procedure (Loc, Tag_Typ, Decl, Ent);
12521 Append_To (Res, Decl);
12522 end if;
12524 -- Bodies for Dispatching stream IO routines. We need these only for
12525 -- non-limited types (in the limited case there is no dispatching).
12526 -- We also skip them if dispatching or finalization are not available
12527 -- or if stream operations are prohibited by restriction No_Streams or
12528 -- from use of pragma/aspect No_Tagged_Streams.
12530 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Read)
12531 and then No (TSS (Tag_Typ, TSS_Stream_Read))
12532 then
12533 Build_Record_Read_Procedure (Tag_Typ, Decl, Ent);
12534 Append_To (Res, Decl);
12535 end if;
12537 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Write)
12538 and then No (TSS (Tag_Typ, TSS_Stream_Write))
12539 then
12540 Build_Record_Write_Procedure (Tag_Typ, Decl, Ent);
12541 Append_To (Res, Decl);
12542 end if;
12544 -- Skip body of _Input for the abstract case, since the corresponding
12545 -- spec is abstract (see Predef_Spec_Or_Body).
12547 if not Is_Abstract_Type (Tag_Typ)
12548 and then Stream_Operation_OK (Tag_Typ, TSS_Stream_Input)
12549 and then No (TSS (Tag_Typ, TSS_Stream_Input))
12550 then
12551 Build_Record_Or_Elementary_Input_Function
12552 (Tag_Typ, Decl, Ent);
12553 Append_To (Res, Decl);
12554 end if;
12556 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Output)
12557 and then No (TSS (Tag_Typ, TSS_Stream_Output))
12558 then
12559 Build_Record_Or_Elementary_Output_Procedure (Tag_Typ, Decl, Ent);
12560 Append_To (Res, Decl);
12561 end if;
12563 -- Ada 2005: Generate bodies for the following primitive operations for
12564 -- limited interfaces and synchronized types that implement a limited
12565 -- interface.
12567 -- disp_asynchronous_select
12568 -- disp_conditional_select
12569 -- disp_get_prim_op_kind
12570 -- disp_get_task_id
12571 -- disp_timed_select
12573 -- The interface versions will have null bodies
12575 -- Disable the generation of these bodies if Ravenscar or ZFP is active
12577 -- In VM targets we define these primitives in all root tagged types
12578 -- that are not interface types. Done because in VM targets we don't
12579 -- have secondary dispatch tables and any derivation of Tag_Typ may
12580 -- cover limited interfaces (which always have these primitives since
12581 -- they may be ancestors of synchronized interface types).
12583 if Ada_Version >= Ada_2005
12584 and then
12585 ((Is_Interface (Etype (Tag_Typ))
12586 and then Is_Limited_Record (Etype (Tag_Typ)))
12587 or else
12588 (Is_Concurrent_Record_Type (Tag_Typ)
12589 and then Has_Interfaces (Tag_Typ))
12590 or else
12591 (not Tagged_Type_Expansion
12592 and then Tag_Typ = Root_Type (Tag_Typ)))
12593 and then not Restriction_Active (No_Select_Statements)
12594 and then RTE_Available (RE_Select_Specific_Data)
12595 then
12596 Append_To (Res, Make_Disp_Asynchronous_Select_Body (Tag_Typ));
12597 Append_To (Res, Make_Disp_Conditional_Select_Body (Tag_Typ));
12598 Append_To (Res, Make_Disp_Get_Prim_Op_Kind_Body (Tag_Typ));
12599 Append_To (Res, Make_Disp_Get_Task_Id_Body (Tag_Typ));
12600 Append_To (Res, Make_Disp_Requeue_Body (Tag_Typ));
12601 Append_To (Res, Make_Disp_Timed_Select_Body (Tag_Typ));
12602 end if;
12604 if not Is_Limited_Type (Tag_Typ) then
12605 -- Body for equality and inequality
12607 Predefined_Primitive_Eq_Body (Tag_Typ, Res, Renamed_Eq);
12609 -- Body for dispatching assignment
12611 Decl :=
12612 Predef_Spec_Or_Body (Loc,
12613 Tag_Typ => Tag_Typ,
12614 Name => Name_uAssign,
12615 Profile => New_List (
12616 Make_Parameter_Specification (Loc,
12617 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
12618 Out_Present => True,
12619 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
12621 Make_Parameter_Specification (Loc,
12622 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
12623 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
12624 For_Body => True);
12626 Set_Handled_Statement_Sequence (Decl,
12627 Make_Handled_Sequence_Of_Statements (Loc, New_List (
12628 Make_Assignment_Statement (Loc,
12629 Name => Make_Identifier (Loc, Name_X),
12630 Expression => Make_Identifier (Loc, Name_Y)))));
12632 Append_To (Res, Decl);
12633 end if;
12635 -- Generate empty bodies of routines Deep_Adjust and Deep_Finalize for
12636 -- tagged types which do not contain controlled components.
12638 -- Do not generate the routines if finalization is disabled
12640 if Restriction_Active (No_Finalization) then
12641 null;
12643 elsif not Has_Controlled_Component (Tag_Typ) then
12644 if not Is_Limited_Type (Tag_Typ) then
12645 Adj_Call := Empty;
12646 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust, True);
12648 if Is_Controlled (Tag_Typ) then
12649 Adj_Call :=
12650 Make_Adjust_Call (
12651 Obj_Ref => Make_Identifier (Loc, Name_V),
12652 Typ => Tag_Typ);
12653 end if;
12655 if No (Adj_Call) then
12656 Adj_Call := Make_Null_Statement (Loc);
12657 end if;
12659 Set_Handled_Statement_Sequence (Decl,
12660 Make_Handled_Sequence_Of_Statements (Loc,
12661 Statements => New_List (Adj_Call)));
12663 Append_To (Res, Decl);
12664 end if;
12666 Fin_Call := Empty;
12667 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize, True);
12669 if Is_Controlled (Tag_Typ) then
12670 Fin_Call :=
12671 Make_Final_Call
12672 (Obj_Ref => Make_Identifier (Loc, Name_V),
12673 Typ => Tag_Typ);
12674 end if;
12676 if No (Fin_Call) then
12677 Fin_Call := Make_Null_Statement (Loc);
12678 end if;
12680 Set_Handled_Statement_Sequence (Decl,
12681 Make_Handled_Sequence_Of_Statements (Loc,
12682 Statements => New_List (Fin_Call)));
12684 Append_To (Res, Decl);
12685 end if;
12687 return Res;
12688 end Predefined_Primitive_Bodies;
12690 ---------------------------------
12691 -- Predefined_Primitive_Freeze --
12692 ---------------------------------
12694 function Predefined_Primitive_Freeze
12695 (Tag_Typ : Entity_Id) return List_Id
12697 Res : constant List_Id := New_List;
12698 Prim : Elmt_Id;
12699 Frnodes : List_Id;
12701 begin
12702 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
12703 while Present (Prim) loop
12704 if Is_Predefined_Dispatching_Operation (Node (Prim)) then
12705 Frnodes := Freeze_Entity (Node (Prim), Tag_Typ);
12707 if Present (Frnodes) then
12708 Append_List_To (Res, Frnodes);
12709 end if;
12710 end if;
12712 Next_Elmt (Prim);
12713 end loop;
12715 return Res;
12716 end Predefined_Primitive_Freeze;
12718 -------------------------
12719 -- Stream_Operation_OK --
12720 -------------------------
12722 function Stream_Operation_OK
12723 (Typ : Entity_Id;
12724 Operation : TSS_Name_Type) return Boolean
12726 Has_Predefined_Or_Specified_Stream_Attribute : Boolean := False;
12728 begin
12729 -- Special case of a limited type extension: a default implementation
12730 -- of the stream attributes Read or Write exists if that attribute
12731 -- has been specified or is available for an ancestor type; a default
12732 -- implementation of the attribute Output (resp. Input) exists if the
12733 -- attribute has been specified or Write (resp. Read) is available for
12734 -- an ancestor type. The last condition only applies under Ada 2005.
12736 if Is_Limited_Type (Typ) and then Is_Tagged_Type (Typ) then
12737 if Operation = TSS_Stream_Read then
12738 Has_Predefined_Or_Specified_Stream_Attribute :=
12739 Has_Specified_Stream_Read (Typ);
12741 elsif Operation = TSS_Stream_Write then
12742 Has_Predefined_Or_Specified_Stream_Attribute :=
12743 Has_Specified_Stream_Write (Typ);
12745 elsif Operation = TSS_Stream_Input then
12746 Has_Predefined_Or_Specified_Stream_Attribute :=
12747 Has_Specified_Stream_Input (Typ)
12748 or else
12749 (Ada_Version >= Ada_2005
12750 and then Stream_Operation_OK (Typ, TSS_Stream_Read));
12752 elsif Operation = TSS_Stream_Output then
12753 Has_Predefined_Or_Specified_Stream_Attribute :=
12754 Has_Specified_Stream_Output (Typ)
12755 or else
12756 (Ada_Version >= Ada_2005
12757 and then Stream_Operation_OK (Typ, TSS_Stream_Write));
12758 end if;
12760 -- Case of inherited TSS_Stream_Read or TSS_Stream_Write
12762 if not Has_Predefined_Or_Specified_Stream_Attribute
12763 and then Is_Derived_Type (Typ)
12764 and then (Operation = TSS_Stream_Read
12765 or else Operation = TSS_Stream_Write)
12766 then
12767 Has_Predefined_Or_Specified_Stream_Attribute :=
12768 Present
12769 (Find_Inherited_TSS (Base_Type (Etype (Typ)), Operation));
12770 end if;
12771 end if;
12773 -- If the type is not limited, or else is limited but the attribute is
12774 -- explicitly specified or is predefined for the type, then return True,
12775 -- unless other conditions prevail, such as restrictions prohibiting
12776 -- streams or dispatching operations. We also return True for limited
12777 -- interfaces, because they may be extended by nonlimited types and
12778 -- permit inheritance in this case (addresses cases where an abstract
12779 -- extension doesn't get 'Input declared, as per comments below, but
12780 -- 'Class'Input must still be allowed). Note that attempts to apply
12781 -- stream attributes to a limited interface or its class-wide type
12782 -- (or limited extensions thereof) will still get properly rejected
12783 -- by Check_Stream_Attribute.
12785 -- We exclude the Input operation from being a predefined subprogram in
12786 -- the case where the associated type is an abstract extension, because
12787 -- the attribute is not callable in that case, per 13.13.2(49/2). Also,
12788 -- we don't want an abstract version created because types derived from
12789 -- the abstract type may not even have Input available (for example if
12790 -- derived from a private view of the abstract type that doesn't have
12791 -- a visible Input).
12793 -- Do not generate stream routines for type Finalization_Master because
12794 -- a master may never appear in types and therefore cannot be read or
12795 -- written.
12797 return
12798 (not Is_Limited_Type (Typ)
12799 or else Is_Interface (Typ)
12800 or else Has_Predefined_Or_Specified_Stream_Attribute)
12801 and then
12802 (Operation /= TSS_Stream_Input
12803 or else not Is_Abstract_Type (Typ)
12804 or else not Is_Derived_Type (Typ))
12805 and then not Has_Unknown_Discriminants (Typ)
12806 and then not Is_Concurrent_Interface (Typ)
12807 and then not Restriction_Active (No_Streams)
12808 and then not Restriction_Active (No_Dispatch)
12809 and then No (No_Tagged_Streams_Pragma (Typ))
12810 and then not No_Run_Time_Mode
12811 and then RTE_Available (RE_Tag)
12812 and then No (Type_Without_Stream_Operation (Typ))
12813 and then RTE_Available (RE_Root_Stream_Type)
12814 and then not Is_RTE (Typ, RE_Finalization_Master);
12815 end Stream_Operation_OK;
12817 end Exp_Ch3;