[RS6000] cost SLOW_UNALIGNED_ACCESS
[official-gcc.git] / gcc / ada / exp_ch3.adb
blob6f7ae0a002b6fed6035c9c4e4b59306d3e29415e
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-2016, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Errout; use Errout;
31 with Exp_Aggr; use Exp_Aggr;
32 with Exp_Atag; use Exp_Atag;
33 with Exp_Ch4; use Exp_Ch4;
34 with Exp_Ch6; use Exp_Ch6;
35 with Exp_Ch7; use Exp_Ch7;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Dbug; use Exp_Dbug;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Dist; use Exp_Dist;
40 with Exp_Smem; use Exp_Smem;
41 with Exp_Strm; use Exp_Strm;
42 with Exp_Tss; use Exp_Tss;
43 with Exp_Util; use Exp_Util;
44 with Freeze; use Freeze;
45 with Ghost; use Ghost;
46 with Namet; use Namet;
47 with Nlists; use Nlists;
48 with Nmake; use Nmake;
49 with Opt; use Opt;
50 with Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
53 with Sem; use Sem;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Attr; use Sem_Attr;
56 with Sem_Cat; use Sem_Cat;
57 with Sem_Ch3; use Sem_Ch3;
58 with Sem_Ch6; use Sem_Ch6;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Disp; use Sem_Disp;
61 with Sem_Eval; use Sem_Eval;
62 with Sem_Mech; use Sem_Mech;
63 with Sem_Res; use Sem_Res;
64 with Sem_SCIL; use Sem_SCIL;
65 with Sem_Type; use Sem_Type;
66 with Sem_Util; use Sem_Util;
67 with Sinfo; use Sinfo;
68 with Stand; use Stand;
69 with Snames; use Snames;
70 with Targparm; use Targparm;
71 with Tbuild; use Tbuild;
72 with Ttypes; use Ttypes;
73 with Validsw; use Validsw;
75 package body Exp_Ch3 is
77 -----------------------
78 -- Local Subprograms --
79 -----------------------
81 procedure Adjust_Discriminants (Rtype : Entity_Id);
82 -- This is used when freezing a record type. It attempts to construct
83 -- more restrictive subtypes for discriminants so that the max size of
84 -- the record can be calculated more accurately. See the body of this
85 -- procedure for details.
87 procedure Build_Array_Init_Proc (A_Type : Entity_Id; Nod : Node_Id);
88 -- Build initialization procedure for given array type. Nod is a node
89 -- used for attachment of any actions required in its construction.
90 -- It also supplies the source location used for the procedure.
92 function Build_Discriminant_Formals
93 (Rec_Id : Entity_Id;
94 Use_Dl : Boolean) return List_Id;
95 -- This function uses the discriminants of a type to build a list of
96 -- formal parameters, used in Build_Init_Procedure among other places.
97 -- If the flag Use_Dl is set, the list is built using the already
98 -- defined discriminals of the type, as is the case for concurrent
99 -- types with discriminants. Otherwise new identifiers are created,
100 -- with the source names of the discriminants.
102 function Build_Equivalent_Array_Aggregate (T : Entity_Id) return Node_Id;
103 -- This function builds a static aggregate that can serve as the initial
104 -- value for an array type whose bounds are static, and whose component
105 -- type is a composite type that has a static equivalent aggregate.
106 -- The equivalent array aggregate is used both for object initialization
107 -- and for component initialization, when used in the following function.
109 function Build_Equivalent_Record_Aggregate (T : Entity_Id) return Node_Id;
110 -- This function builds a static aggregate that can serve as the initial
111 -- value for a record type whose components are scalar and initialized
112 -- with compile-time values, or arrays with similar initialization or
113 -- defaults. When possible, initialization of an object of the type can
114 -- be achieved by using a copy of the aggregate as an initial value, thus
115 -- removing the implicit call that would otherwise constitute elaboration
116 -- code.
118 procedure Build_Record_Init_Proc (N : Node_Id; Rec_Ent : Entity_Id);
119 -- Build record initialization procedure. N is the type declaration
120 -- node, and Rec_Ent is the corresponding entity for the record type.
122 procedure Build_Slice_Assignment (Typ : Entity_Id);
123 -- Build assignment procedure for one-dimensional arrays of controlled
124 -- types. Other array and slice assignments are expanded in-line, but
125 -- the code expansion for controlled components (when control actions
126 -- are active) can lead to very large blocks that GCC3 handles poorly.
128 procedure Build_Untagged_Equality (Typ : Entity_Id);
129 -- AI05-0123: Equality on untagged records composes. This procedure
130 -- builds the equality routine for an untagged record that has components
131 -- of a record type that has user-defined primitive equality operations.
132 -- The resulting operation is a TSS subprogram.
134 procedure Build_Variant_Record_Equality (Typ : Entity_Id);
135 -- Create An Equality function for the untagged variant record Typ and
136 -- attach it to the TSS list
138 procedure Check_Stream_Attributes (Typ : Entity_Id);
139 -- Check that if a limited extension has a parent with user-defined stream
140 -- attributes, and does not itself have user-defined stream-attributes,
141 -- then any limited component of the extension also has the corresponding
142 -- user-defined stream attributes.
144 procedure Clean_Task_Names
145 (Typ : Entity_Id;
146 Proc_Id : Entity_Id);
147 -- If an initialization procedure includes calls to generate names
148 -- for task subcomponents, indicate that secondary stack cleanup is
149 -- needed after an initialization. Typ is the component type, and Proc_Id
150 -- the initialization procedure for the enclosing composite type.
152 procedure Expand_Freeze_Array_Type (N : Node_Id);
153 -- Freeze an array type. Deals with building the initialization procedure,
154 -- creating the packed array type for a packed array and also with the
155 -- creation of the controlling procedures for the controlled case. The
156 -- argument N is the N_Freeze_Entity node for the type.
158 procedure Expand_Freeze_Class_Wide_Type (N : Node_Id);
159 -- Freeze a class-wide type. Build routine Finalize_Address for the purpose
160 -- of finalizing controlled derivations from the class-wide's root type.
162 procedure Expand_Freeze_Enumeration_Type (N : Node_Id);
163 -- Freeze enumeration type with non-standard representation. Builds the
164 -- array and function needed to convert between enumeration pos and
165 -- enumeration representation values. N is the N_Freeze_Entity node
166 -- for the type.
168 procedure Expand_Freeze_Record_Type (N : Node_Id);
169 -- Freeze record type. Builds all necessary discriminant checking
170 -- and other ancillary functions, and builds dispatch tables where
171 -- needed. The argument N is the N_Freeze_Entity node. This processing
172 -- applies only to E_Record_Type entities, not to class wide types,
173 -- record subtypes, or private types.
175 procedure Expand_Tagged_Root (T : Entity_Id);
176 -- Add a field _Tag at the beginning of the record. This field carries
177 -- the value of the access to the Dispatch table. This procedure is only
178 -- called on root type, the _Tag field being inherited by the descendants.
180 procedure Freeze_Stream_Operations (N : Node_Id; Typ : Entity_Id);
181 -- Treat user-defined stream operations as renaming_as_body if the
182 -- subprogram they rename is not frozen when the type is frozen.
184 procedure Initialization_Warning (E : Entity_Id);
185 -- If static elaboration of the package is requested, indicate
186 -- when a type does meet the conditions for static initialization. If
187 -- E is a type, it has components that have no static initialization.
188 -- if E is an entity, its initial expression is not compile-time known.
190 function Init_Formals (Typ : Entity_Id) return List_Id;
191 -- This function builds the list of formals for an initialization routine.
192 -- The first formal is always _Init with the given type. For task value
193 -- record types and types containing tasks, three additional formals are
194 -- added:
196 -- _Master : Master_Id
197 -- _Chain : in out Activation_Chain
198 -- _Task_Name : String
200 -- The caller must append additional entries for discriminants if required.
202 function Inline_Init_Proc (Typ : Entity_Id) return Boolean;
203 -- Returns true if the initialization procedure of Typ should be inlined
205 function In_Runtime (E : Entity_Id) return Boolean;
206 -- Check if E is defined in the RTL (in a child of Ada or System). Used
207 -- to avoid to bring in the overhead of _Input, _Output for tagged types.
209 function Is_User_Defined_Equality (Prim : Node_Id) return Boolean;
210 -- Returns true if Prim is a user defined equality function
212 function Make_Eq_Body
213 (Typ : Entity_Id;
214 Eq_Name : Name_Id) return Node_Id;
215 -- Build the body of a primitive equality operation for a tagged record
216 -- type, or in Ada 2012 for any record type that has components with a
217 -- user-defined equality. Factored out of Predefined_Primitive_Bodies.
219 function Make_Eq_Case
220 (E : Entity_Id;
221 CL : Node_Id;
222 Discrs : Elist_Id := New_Elmt_List) return List_Id;
223 -- Building block for variant record equality. Defined to share the code
224 -- between the tagged and untagged case. Given a Component_List node CL,
225 -- it generates an 'if' followed by a 'case' statement that compares all
226 -- components of local temporaries named X and Y (that are declared as
227 -- formals at some upper level). E provides the Sloc to be used for the
228 -- generated code.
230 -- IF E is an unchecked_union, Discrs is the list of formals created for
231 -- the inferred discriminants of one operand. These formals are used in
232 -- the generated case statements for each variant of the unchecked union.
234 function Make_Eq_If
235 (E : Entity_Id;
236 L : List_Id) return Node_Id;
237 -- Building block for variant record equality. Defined to share the code
238 -- between the tagged and untagged case. Given the list of components
239 -- (or discriminants) L, it generates a return statement that compares all
240 -- components of local temporaries named X and Y (that are declared as
241 -- formals at some upper level). E provides the Sloc to be used for the
242 -- generated code.
244 function Make_Neq_Body (Tag_Typ : Entity_Id) return Node_Id;
245 -- Search for a renaming of the inequality dispatching primitive of
246 -- this tagged type. If found then build and return the corresponding
247 -- rename-as-body inequality subprogram; otherwise return Empty.
249 procedure Make_Predefined_Primitive_Specs
250 (Tag_Typ : Entity_Id;
251 Predef_List : out List_Id;
252 Renamed_Eq : out Entity_Id);
253 -- Create a list with the specs of the predefined primitive operations.
254 -- For tagged types that are interfaces all these primitives are defined
255 -- abstract.
257 -- The following entries are present for all tagged types, and provide
258 -- the results of the corresponding attribute applied to the object.
259 -- Dispatching is required in general, since the result of the attribute
260 -- will vary with the actual object subtype.
262 -- _size provides result of 'Size attribute
263 -- typSR provides result of 'Read attribute
264 -- typSW provides result of 'Write attribute
265 -- typSI provides result of 'Input attribute
266 -- typSO provides result of 'Output attribute
268 -- The following entries are additionally present for non-limited tagged
269 -- types, and implement additional dispatching operations for predefined
270 -- operations:
272 -- _equality implements "=" operator
273 -- _assign implements assignment operation
274 -- typDF implements deep finalization
275 -- typDA implements deep adjust
277 -- The latter two are empty procedures unless the type contains some
278 -- controlled components that require finalization actions (the deep
279 -- in the name refers to the fact that the action applies to components).
281 -- The list is returned in Predef_List. The Parameter Renamed_Eq either
282 -- returns the value Empty, or else the defining unit name for the
283 -- predefined equality function in the case where the type has a primitive
284 -- operation that is a renaming of predefined equality (but only if there
285 -- is also an overriding user-defined equality function). The returned
286 -- Renamed_Eq will be passed to the corresponding parameter of
287 -- Predefined_Primitive_Bodies.
289 function Has_New_Non_Standard_Rep (T : Entity_Id) return Boolean;
290 -- Returns True if there are representation clauses for type T that are not
291 -- inherited. If the result is false, the init_proc and the discriminant
292 -- checking functions of the parent can be reused by a derived type.
294 procedure Make_Controlling_Function_Wrappers
295 (Tag_Typ : Entity_Id;
296 Decl_List : out List_Id;
297 Body_List : out List_Id);
298 -- Ada 2005 (AI-391): Makes specs and bodies for the wrapper functions
299 -- associated with inherited functions with controlling results which
300 -- are not overridden. The body of each wrapper function consists solely
301 -- of a return statement whose expression is an extension aggregate
302 -- invoking the inherited subprogram's parent subprogram and extended
303 -- with a null association list.
305 function Make_Null_Procedure_Specs (Tag_Typ : Entity_Id) return List_Id;
306 -- Ada 2005 (AI-251): Makes specs for null procedures associated with any
307 -- null procedures inherited from an interface type that have not been
308 -- overridden. Only one null procedure will be created for a given set of
309 -- inherited null procedures with homographic profiles.
311 function Predef_Spec_Or_Body
312 (Loc : Source_Ptr;
313 Tag_Typ : Entity_Id;
314 Name : Name_Id;
315 Profile : List_Id;
316 Ret_Type : Entity_Id := Empty;
317 For_Body : Boolean := False) return Node_Id;
318 -- This function generates the appropriate expansion for a predefined
319 -- primitive operation specified by its name, parameter profile and
320 -- return type (Empty means this is a procedure). If For_Body is false,
321 -- then the returned node is a subprogram declaration. If For_Body is
322 -- true, then the returned node is a empty subprogram body containing
323 -- no declarations and no statements.
325 function Predef_Stream_Attr_Spec
326 (Loc : Source_Ptr;
327 Tag_Typ : Entity_Id;
328 Name : TSS_Name_Type;
329 For_Body : Boolean := False) return Node_Id;
330 -- Specialized version of Predef_Spec_Or_Body that apply to read, write,
331 -- input and output attribute whose specs are constructed in Exp_Strm.
333 function Predef_Deep_Spec
334 (Loc : Source_Ptr;
335 Tag_Typ : Entity_Id;
336 Name : TSS_Name_Type;
337 For_Body : Boolean := False) return Node_Id;
338 -- Specialized version of Predef_Spec_Or_Body that apply to _deep_adjust
339 -- and _deep_finalize
341 function Predefined_Primitive_Bodies
342 (Tag_Typ : Entity_Id;
343 Renamed_Eq : Entity_Id) return List_Id;
344 -- Create the bodies of the predefined primitives that are described in
345 -- Predefined_Primitive_Specs. When not empty, Renamed_Eq must denote
346 -- the defining unit name of the type's predefined equality as returned
347 -- by Make_Predefined_Primitive_Specs.
349 function Predefined_Primitive_Freeze (Tag_Typ : Entity_Id) return List_Id;
350 -- Freeze entities of all predefined primitive operations. This is needed
351 -- because the bodies of these operations do not normally do any freezing.
353 function Stream_Operation_OK
354 (Typ : Entity_Id;
355 Operation : TSS_Name_Type) return Boolean;
356 -- Check whether the named stream operation must be emitted for a given
357 -- type. The rules for inheritance of stream attributes by type extensions
358 -- are enforced by this function. Furthermore, various restrictions prevent
359 -- the generation of these operations, as a useful optimization or for
360 -- certification purposes and to save unnecessary generated code.
362 --------------------------
363 -- Adjust_Discriminants --
364 --------------------------
366 -- This procedure attempts to define subtypes for discriminants that are
367 -- more restrictive than those declared. Such a replacement is possible if
368 -- we can demonstrate that values outside the restricted range would cause
369 -- constraint errors in any case. The advantage of restricting the
370 -- discriminant types in this way is that the maximum size of the variant
371 -- record can be calculated more conservatively.
373 -- An example of a situation in which we can perform this type of
374 -- restriction is the following:
376 -- subtype B is range 1 .. 10;
377 -- type Q is array (B range <>) of Integer;
379 -- type V (N : Natural) is record
380 -- C : Q (1 .. N);
381 -- end record;
383 -- In this situation, we can restrict the upper bound of N to 10, since
384 -- any larger value would cause a constraint error in any case.
386 -- There are many situations in which such restriction is possible, but
387 -- for now, we just look for cases like the above, where the component
388 -- in question is a one dimensional array whose upper bound is one of
389 -- the record discriminants. Also the component must not be part of
390 -- any variant part, since then the component does not always exist.
392 procedure Adjust_Discriminants (Rtype : Entity_Id) is
393 Loc : constant Source_Ptr := Sloc (Rtype);
394 Comp : Entity_Id;
395 Ctyp : Entity_Id;
396 Ityp : Entity_Id;
397 Lo : Node_Id;
398 Hi : Node_Id;
399 P : Node_Id;
400 Loval : Uint;
401 Discr : Entity_Id;
402 Dtyp : Entity_Id;
403 Dhi : Node_Id;
404 Dhiv : Uint;
405 Ahi : Node_Id;
406 Ahiv : Uint;
407 Tnn : Entity_Id;
409 begin
410 Comp := First_Component (Rtype);
411 while Present (Comp) loop
413 -- If our parent is a variant, quit, we do not look at components
414 -- that are in variant parts, because they may not always exist.
416 P := Parent (Comp); -- component declaration
417 P := Parent (P); -- component list
419 exit when Nkind (Parent (P)) = N_Variant;
421 -- We are looking for a one dimensional array type
423 Ctyp := Etype (Comp);
425 if not Is_Array_Type (Ctyp) or else Number_Dimensions (Ctyp) > 1 then
426 goto Continue;
427 end if;
429 -- The lower bound must be constant, and the upper bound is a
430 -- discriminant (which is a discriminant of the current record).
432 Ityp := Etype (First_Index (Ctyp));
433 Lo := Type_Low_Bound (Ityp);
434 Hi := Type_High_Bound (Ityp);
436 if not Compile_Time_Known_Value (Lo)
437 or else Nkind (Hi) /= N_Identifier
438 or else No (Entity (Hi))
439 or else Ekind (Entity (Hi)) /= E_Discriminant
440 then
441 goto Continue;
442 end if;
444 -- We have an array with appropriate bounds
446 Loval := Expr_Value (Lo);
447 Discr := Entity (Hi);
448 Dtyp := Etype (Discr);
450 -- See if the discriminant has a known upper bound
452 Dhi := Type_High_Bound (Dtyp);
454 if not Compile_Time_Known_Value (Dhi) then
455 goto Continue;
456 end if;
458 Dhiv := Expr_Value (Dhi);
460 -- See if base type of component array has known upper bound
462 Ahi := Type_High_Bound (Etype (First_Index (Base_Type (Ctyp))));
464 if not Compile_Time_Known_Value (Ahi) then
465 goto Continue;
466 end if;
468 Ahiv := Expr_Value (Ahi);
470 -- The condition for doing the restriction is that the high bound
471 -- of the discriminant is greater than the low bound of the array,
472 -- and is also greater than the high bound of the base type index.
474 if Dhiv > Loval and then Dhiv > Ahiv then
476 -- We can reset the upper bound of the discriminant type to
477 -- whichever is larger, the low bound of the component, or
478 -- the high bound of the base type array index.
480 -- We build a subtype that is declared as
482 -- subtype Tnn is discr_type range discr_type'First .. max;
484 -- And insert this declaration into the tree. The type of the
485 -- discriminant is then reset to this more restricted subtype.
487 Tnn := Make_Temporary (Loc, 'T');
489 Insert_Action (Declaration_Node (Rtype),
490 Make_Subtype_Declaration (Loc,
491 Defining_Identifier => Tnn,
492 Subtype_Indication =>
493 Make_Subtype_Indication (Loc,
494 Subtype_Mark => New_Occurrence_Of (Dtyp, Loc),
495 Constraint =>
496 Make_Range_Constraint (Loc,
497 Range_Expression =>
498 Make_Range (Loc,
499 Low_Bound =>
500 Make_Attribute_Reference (Loc,
501 Attribute_Name => Name_First,
502 Prefix => New_Occurrence_Of (Dtyp, Loc)),
503 High_Bound =>
504 Make_Integer_Literal (Loc,
505 Intval => UI_Max (Loval, Ahiv)))))));
507 Set_Etype (Discr, Tnn);
508 end if;
510 <<Continue>>
511 Next_Component (Comp);
512 end loop;
513 end Adjust_Discriminants;
515 ---------------------------
516 -- Build_Array_Init_Proc --
517 ---------------------------
519 procedure Build_Array_Init_Proc (A_Type : Entity_Id; Nod : Node_Id) is
520 Comp_Type : constant Entity_Id := Component_Type (A_Type);
521 Body_Stmts : List_Id;
522 Has_Default_Init : Boolean;
523 Index_List : List_Id;
524 Loc : Source_Ptr;
525 Proc_Id : Entity_Id;
527 function Init_Component return List_Id;
528 -- Create one statement to initialize one array component, designated
529 -- by a full set of indexes.
531 function Init_One_Dimension (N : Int) return List_Id;
532 -- Create loop to initialize one dimension of the array. The single
533 -- statement in the loop body initializes the inner dimensions if any,
534 -- or else the single component. Note that this procedure is called
535 -- recursively, with N being the dimension to be initialized. A call
536 -- with N greater than the number of dimensions simply generates the
537 -- component initialization, terminating the recursion.
539 --------------------
540 -- Init_Component --
541 --------------------
543 function Init_Component return List_Id is
544 Comp : Node_Id;
546 begin
547 Comp :=
548 Make_Indexed_Component (Loc,
549 Prefix => Make_Identifier (Loc, Name_uInit),
550 Expressions => Index_List);
552 if Has_Default_Aspect (A_Type) then
553 Set_Assignment_OK (Comp);
554 return New_List (
555 Make_Assignment_Statement (Loc,
556 Name => Comp,
557 Expression =>
558 Convert_To (Comp_Type,
559 Default_Aspect_Component_Value (First_Subtype (A_Type)))));
561 elsif Needs_Simple_Initialization (Comp_Type) then
562 Set_Assignment_OK (Comp);
563 return New_List (
564 Make_Assignment_Statement (Loc,
565 Name => Comp,
566 Expression =>
567 Get_Simple_Init_Val
568 (Comp_Type, Nod, Component_Size (A_Type))));
570 else
571 Clean_Task_Names (Comp_Type, Proc_Id);
572 return
573 Build_Initialization_Call
574 (Loc, Comp, Comp_Type,
575 In_Init_Proc => True,
576 Enclos_Type => A_Type);
577 end if;
578 end Init_Component;
580 ------------------------
581 -- Init_One_Dimension --
582 ------------------------
584 function Init_One_Dimension (N : Int) return List_Id is
585 Index : Entity_Id;
587 begin
588 -- If the component does not need initializing, then there is nothing
589 -- to do here, so we return a null body. This occurs when generating
590 -- the dummy Init_Proc needed for Initialize_Scalars processing.
592 if not Has_Non_Null_Base_Init_Proc (Comp_Type)
593 and then not Needs_Simple_Initialization (Comp_Type)
594 and then not Has_Task (Comp_Type)
595 and then not Has_Default_Aspect (A_Type)
596 then
597 return New_List (Make_Null_Statement (Loc));
599 -- If all dimensions dealt with, we simply initialize the component
601 elsif N > Number_Dimensions (A_Type) then
602 return Init_Component;
604 -- Here we generate the required loop
606 else
607 Index :=
608 Make_Defining_Identifier (Loc, New_External_Name ('J', N));
610 Append (New_Occurrence_Of (Index, Loc), Index_List);
612 return New_List (
613 Make_Implicit_Loop_Statement (Nod,
614 Identifier => Empty,
615 Iteration_Scheme =>
616 Make_Iteration_Scheme (Loc,
617 Loop_Parameter_Specification =>
618 Make_Loop_Parameter_Specification (Loc,
619 Defining_Identifier => Index,
620 Discrete_Subtype_Definition =>
621 Make_Attribute_Reference (Loc,
622 Prefix =>
623 Make_Identifier (Loc, Name_uInit),
624 Attribute_Name => Name_Range,
625 Expressions => New_List (
626 Make_Integer_Literal (Loc, N))))),
627 Statements => Init_One_Dimension (N + 1)));
628 end if;
629 end Init_One_Dimension;
631 -- Start of processing for Build_Array_Init_Proc
633 begin
634 -- The init proc is created when analyzing the freeze node for the type,
635 -- but it properly belongs with the array type declaration. However, if
636 -- the freeze node is for a subtype of a type declared in another unit
637 -- it seems preferable to use the freeze node as the source location of
638 -- the init proc. In any case this is preferable for gcov usage, and
639 -- the Sloc is not otherwise used by the compiler.
641 if In_Open_Scopes (Scope (A_Type)) then
642 Loc := Sloc (A_Type);
643 else
644 Loc := Sloc (Nod);
645 end if;
647 -- Nothing to generate in the following cases:
649 -- 1. Initialization is suppressed for the type
650 -- 2. An initialization already exists for the base type
652 if Initialization_Suppressed (A_Type)
653 or else Present (Base_Init_Proc (A_Type))
654 then
655 return;
656 end if;
658 Index_List := New_List;
660 -- We need an initialization procedure if any of the following is true:
662 -- 1. The component type has an initialization procedure
663 -- 2. The component type needs simple initialization
664 -- 3. Tasks are present
665 -- 4. The type is marked as a public entity
666 -- 5. The array type has a Default_Component_Value aspect
668 -- The reason for the public entity test is to deal properly with the
669 -- Initialize_Scalars pragma. This pragma can be set in the client and
670 -- not in the declaring package, this means the client will make a call
671 -- to the initialization procedure (because one of conditions 1-3 must
672 -- apply in this case), and we must generate a procedure (even if it is
673 -- null) to satisfy the call in this case.
675 -- Exception: do not build an array init_proc for a type whose root
676 -- type is Standard.String or Standard.Wide_[Wide_]String, since there
677 -- is no place to put the code, and in any case we handle initialization
678 -- of such types (in the Initialize_Scalars case, that's the only time
679 -- the issue arises) in a special manner anyway which does not need an
680 -- init_proc.
682 Has_Default_Init := Has_Non_Null_Base_Init_Proc (Comp_Type)
683 or else Needs_Simple_Initialization (Comp_Type)
684 or else Has_Task (Comp_Type)
685 or else Has_Default_Aspect (A_Type);
687 if Has_Default_Init
688 or else (not Restriction_Active (No_Initialize_Scalars)
689 and then Is_Public (A_Type)
690 and then not Is_Standard_String_Type (A_Type))
691 then
692 Proc_Id :=
693 Make_Defining_Identifier (Loc,
694 Chars => Make_Init_Proc_Name (A_Type));
696 -- If No_Default_Initialization restriction is active, then we don't
697 -- want to build an init_proc, but we need to mark that an init_proc
698 -- would be needed if this restriction was not active (so that we can
699 -- detect attempts to call it), so set a dummy init_proc in place.
700 -- This is only done though when actual default initialization is
701 -- needed (and not done when only Is_Public is True), since otherwise
702 -- objects such as arrays of scalars could be wrongly flagged as
703 -- violating the restriction.
705 if Restriction_Active (No_Default_Initialization) then
706 if Has_Default_Init then
707 Set_Init_Proc (A_Type, Proc_Id);
708 end if;
710 return;
711 end if;
713 Body_Stmts := Init_One_Dimension (1);
715 Discard_Node (
716 Make_Subprogram_Body (Loc,
717 Specification =>
718 Make_Procedure_Specification (Loc,
719 Defining_Unit_Name => Proc_Id,
720 Parameter_Specifications => Init_Formals (A_Type)),
721 Declarations => New_List,
722 Handled_Statement_Sequence =>
723 Make_Handled_Sequence_Of_Statements (Loc,
724 Statements => Body_Stmts)));
726 Set_Ekind (Proc_Id, E_Procedure);
727 Set_Is_Public (Proc_Id, Is_Public (A_Type));
728 Set_Is_Internal (Proc_Id);
729 Set_Has_Completion (Proc_Id);
731 if not Debug_Generated_Code then
732 Set_Debug_Info_Off (Proc_Id);
733 end if;
735 -- Set Inlined on Init_Proc if it is set on the Init_Proc of the
736 -- component type itself (see also Build_Record_Init_Proc).
738 Set_Is_Inlined (Proc_Id, Inline_Init_Proc (Comp_Type));
740 -- Associate Init_Proc with type, and determine if the procedure
741 -- is null (happens because of the Initialize_Scalars pragma case,
742 -- where we have to generate a null procedure in case it is called
743 -- by a client with Initialize_Scalars set). Such procedures have
744 -- to be generated, but do not have to be called, so we mark them
745 -- as null to suppress the call.
747 Set_Init_Proc (A_Type, Proc_Id);
749 if List_Length (Body_Stmts) = 1
751 -- We must skip SCIL nodes because they may have been added to this
752 -- list by Insert_Actions.
754 and then Nkind (First_Non_SCIL_Node (Body_Stmts)) = N_Null_Statement
755 then
756 Set_Is_Null_Init_Proc (Proc_Id);
758 else
759 -- Try to build a static aggregate to statically initialize
760 -- objects of the type. This can only be done for constrained
761 -- one-dimensional arrays with static bounds.
763 Set_Static_Initialization
764 (Proc_Id,
765 Build_Equivalent_Array_Aggregate (First_Subtype (A_Type)));
766 end if;
767 end if;
768 end Build_Array_Init_Proc;
770 --------------------------------
771 -- Build_Discr_Checking_Funcs --
772 --------------------------------
774 procedure Build_Discr_Checking_Funcs (N : Node_Id) is
775 Rec_Id : Entity_Id;
776 Loc : Source_Ptr;
777 Enclosing_Func_Id : Entity_Id;
778 Sequence : Nat := 1;
779 Type_Def : Node_Id;
780 V : Node_Id;
782 function Build_Case_Statement
783 (Case_Id : Entity_Id;
784 Variant : Node_Id) return Node_Id;
785 -- Build a case statement containing only two alternatives. The first
786 -- alternative corresponds exactly to the discrete choices given on the
787 -- variant with contains the components that we are generating the
788 -- checks for. If the discriminant is one of these return False. The
789 -- second alternative is an OTHERS choice that will return True
790 -- indicating the discriminant did not match.
792 function Build_Dcheck_Function
793 (Case_Id : Entity_Id;
794 Variant : Node_Id) return Entity_Id;
795 -- Build the discriminant checking function for a given variant
797 procedure Build_Dcheck_Functions (Variant_Part_Node : Node_Id);
798 -- Builds the discriminant checking function for each variant of the
799 -- given variant part of the record type.
801 --------------------------
802 -- Build_Case_Statement --
803 --------------------------
805 function Build_Case_Statement
806 (Case_Id : Entity_Id;
807 Variant : Node_Id) return Node_Id
809 Alt_List : constant List_Id := New_List;
810 Actuals_List : List_Id;
811 Case_Node : Node_Id;
812 Case_Alt_Node : Node_Id;
813 Choice : Node_Id;
814 Choice_List : List_Id;
815 D : Entity_Id;
816 Return_Node : Node_Id;
818 begin
819 Case_Node := New_Node (N_Case_Statement, Loc);
821 -- Replace the discriminant which controls the variant with the name
822 -- of the formal of the checking function.
824 Set_Expression (Case_Node, Make_Identifier (Loc, Chars (Case_Id)));
826 Choice := First (Discrete_Choices (Variant));
828 if Nkind (Choice) = N_Others_Choice then
829 Choice_List := New_Copy_List (Others_Discrete_Choices (Choice));
830 else
831 Choice_List := New_Copy_List (Discrete_Choices (Variant));
832 end if;
834 if not Is_Empty_List (Choice_List) then
835 Case_Alt_Node := New_Node (N_Case_Statement_Alternative, Loc);
836 Set_Discrete_Choices (Case_Alt_Node, Choice_List);
838 -- In case this is a nested variant, we need to return the result
839 -- of the discriminant checking function for the immediately
840 -- enclosing variant.
842 if Present (Enclosing_Func_Id) then
843 Actuals_List := New_List;
845 D := First_Discriminant (Rec_Id);
846 while Present (D) loop
847 Append (Make_Identifier (Loc, Chars (D)), Actuals_List);
848 Next_Discriminant (D);
849 end loop;
851 Return_Node :=
852 Make_Simple_Return_Statement (Loc,
853 Expression =>
854 Make_Function_Call (Loc,
855 Name =>
856 New_Occurrence_Of (Enclosing_Func_Id, Loc),
857 Parameter_Associations =>
858 Actuals_List));
860 else
861 Return_Node :=
862 Make_Simple_Return_Statement (Loc,
863 Expression =>
864 New_Occurrence_Of (Standard_False, Loc));
865 end if;
867 Set_Statements (Case_Alt_Node, New_List (Return_Node));
868 Append (Case_Alt_Node, Alt_List);
869 end if;
871 Case_Alt_Node := New_Node (N_Case_Statement_Alternative, Loc);
872 Choice_List := New_List (New_Node (N_Others_Choice, Loc));
873 Set_Discrete_Choices (Case_Alt_Node, Choice_List);
875 Return_Node :=
876 Make_Simple_Return_Statement (Loc,
877 Expression =>
878 New_Occurrence_Of (Standard_True, Loc));
880 Set_Statements (Case_Alt_Node, New_List (Return_Node));
881 Append (Case_Alt_Node, Alt_List);
883 Set_Alternatives (Case_Node, Alt_List);
884 return Case_Node;
885 end Build_Case_Statement;
887 ---------------------------
888 -- Build_Dcheck_Function --
889 ---------------------------
891 function Build_Dcheck_Function
892 (Case_Id : Entity_Id;
893 Variant : Node_Id) return Entity_Id
895 Body_Node : Node_Id;
896 Func_Id : Entity_Id;
897 Parameter_List : List_Id;
898 Spec_Node : Node_Id;
900 begin
901 Body_Node := New_Node (N_Subprogram_Body, Loc);
902 Sequence := Sequence + 1;
904 Func_Id :=
905 Make_Defining_Identifier (Loc,
906 Chars => New_External_Name (Chars (Rec_Id), 'D', Sequence));
907 Set_Is_Discriminant_Check_Function (Func_Id);
909 Spec_Node := New_Node (N_Function_Specification, Loc);
910 Set_Defining_Unit_Name (Spec_Node, Func_Id);
912 Parameter_List := Build_Discriminant_Formals (Rec_Id, False);
914 Set_Parameter_Specifications (Spec_Node, Parameter_List);
915 Set_Result_Definition (Spec_Node,
916 New_Occurrence_Of (Standard_Boolean, Loc));
917 Set_Specification (Body_Node, Spec_Node);
918 Set_Declarations (Body_Node, New_List);
920 Set_Handled_Statement_Sequence (Body_Node,
921 Make_Handled_Sequence_Of_Statements (Loc,
922 Statements => New_List (
923 Build_Case_Statement (Case_Id, Variant))));
925 Set_Ekind (Func_Id, E_Function);
926 Set_Mechanism (Func_Id, Default_Mechanism);
927 Set_Is_Inlined (Func_Id, True);
928 Set_Is_Pure (Func_Id, True);
929 Set_Is_Public (Func_Id, Is_Public (Rec_Id));
930 Set_Is_Internal (Func_Id, True);
932 if not Debug_Generated_Code then
933 Set_Debug_Info_Off (Func_Id);
934 end if;
936 Analyze (Body_Node);
938 Append_Freeze_Action (Rec_Id, Body_Node);
939 Set_Dcheck_Function (Variant, Func_Id);
940 return Func_Id;
941 end Build_Dcheck_Function;
943 ----------------------------
944 -- Build_Dcheck_Functions --
945 ----------------------------
947 procedure Build_Dcheck_Functions (Variant_Part_Node : Node_Id) is
948 Component_List_Node : Node_Id;
949 Decl : Entity_Id;
950 Discr_Name : Entity_Id;
951 Func_Id : Entity_Id;
952 Variant : Node_Id;
953 Saved_Enclosing_Func_Id : Entity_Id;
955 begin
956 -- Build the discriminant-checking function for each variant, and
957 -- label all components of that variant with the function's name.
958 -- We only Generate a discriminant-checking function when the
959 -- variant is not empty, to prevent the creation of dead code.
960 -- The exception to that is when Frontend_Layout_On_Target is set,
961 -- because the variant record size function generated in package
962 -- Layout needs to generate calls to all discriminant-checking
963 -- functions, including those for empty variants.
965 Discr_Name := Entity (Name (Variant_Part_Node));
966 Variant := First_Non_Pragma (Variants (Variant_Part_Node));
968 while Present (Variant) loop
969 Component_List_Node := Component_List (Variant);
971 if not Null_Present (Component_List_Node)
972 or else Frontend_Layout_On_Target
973 then
974 Func_Id := Build_Dcheck_Function (Discr_Name, Variant);
976 Decl :=
977 First_Non_Pragma (Component_Items (Component_List_Node));
978 while Present (Decl) loop
979 Set_Discriminant_Checking_Func
980 (Defining_Identifier (Decl), Func_Id);
981 Next_Non_Pragma (Decl);
982 end loop;
984 if Present (Variant_Part (Component_List_Node)) then
985 Saved_Enclosing_Func_Id := Enclosing_Func_Id;
986 Enclosing_Func_Id := Func_Id;
987 Build_Dcheck_Functions (Variant_Part (Component_List_Node));
988 Enclosing_Func_Id := Saved_Enclosing_Func_Id;
989 end if;
990 end if;
992 Next_Non_Pragma (Variant);
993 end loop;
994 end Build_Dcheck_Functions;
996 -- Start of processing for Build_Discr_Checking_Funcs
998 begin
999 -- Only build if not done already
1001 if not Discr_Check_Funcs_Built (N) then
1002 Type_Def := Type_Definition (N);
1004 if Nkind (Type_Def) = N_Record_Definition then
1005 if No (Component_List (Type_Def)) then -- null record.
1006 return;
1007 else
1008 V := Variant_Part (Component_List (Type_Def));
1009 end if;
1011 else pragma Assert (Nkind (Type_Def) = N_Derived_Type_Definition);
1012 if No (Component_List (Record_Extension_Part (Type_Def))) then
1013 return;
1014 else
1015 V := Variant_Part
1016 (Component_List (Record_Extension_Part (Type_Def)));
1017 end if;
1018 end if;
1020 Rec_Id := Defining_Identifier (N);
1022 if Present (V) and then not Is_Unchecked_Union (Rec_Id) then
1023 Loc := Sloc (N);
1024 Enclosing_Func_Id := Empty;
1025 Build_Dcheck_Functions (V);
1026 end if;
1028 Set_Discr_Check_Funcs_Built (N);
1029 end if;
1030 end Build_Discr_Checking_Funcs;
1032 --------------------------------
1033 -- Build_Discriminant_Formals --
1034 --------------------------------
1036 function Build_Discriminant_Formals
1037 (Rec_Id : Entity_Id;
1038 Use_Dl : Boolean) return List_Id
1040 Loc : Source_Ptr := Sloc (Rec_Id);
1041 Parameter_List : constant List_Id := New_List;
1042 D : Entity_Id;
1043 Formal : Entity_Id;
1044 Formal_Type : Entity_Id;
1045 Param_Spec_Node : Node_Id;
1047 begin
1048 if Has_Discriminants (Rec_Id) then
1049 D := First_Discriminant (Rec_Id);
1050 while Present (D) loop
1051 Loc := Sloc (D);
1053 if Use_Dl then
1054 Formal := Discriminal (D);
1055 Formal_Type := Etype (Formal);
1056 else
1057 Formal := Make_Defining_Identifier (Loc, Chars (D));
1058 Formal_Type := Etype (D);
1059 end if;
1061 Param_Spec_Node :=
1062 Make_Parameter_Specification (Loc,
1063 Defining_Identifier => Formal,
1064 Parameter_Type =>
1065 New_Occurrence_Of (Formal_Type, Loc));
1066 Append (Param_Spec_Node, Parameter_List);
1067 Next_Discriminant (D);
1068 end loop;
1069 end if;
1071 return Parameter_List;
1072 end Build_Discriminant_Formals;
1074 --------------------------------------
1075 -- Build_Equivalent_Array_Aggregate --
1076 --------------------------------------
1078 function Build_Equivalent_Array_Aggregate (T : Entity_Id) return Node_Id is
1079 Loc : constant Source_Ptr := Sloc (T);
1080 Comp_Type : constant Entity_Id := Component_Type (T);
1081 Index_Type : constant Entity_Id := Etype (First_Index (T));
1082 Proc : constant Entity_Id := Base_Init_Proc (T);
1083 Lo, Hi : Node_Id;
1084 Aggr : Node_Id;
1085 Expr : Node_Id;
1087 begin
1088 if not Is_Constrained (T)
1089 or else Number_Dimensions (T) > 1
1090 or else No (Proc)
1091 then
1092 Initialization_Warning (T);
1093 return Empty;
1094 end if;
1096 Lo := Type_Low_Bound (Index_Type);
1097 Hi := Type_High_Bound (Index_Type);
1099 if not Compile_Time_Known_Value (Lo)
1100 or else not Compile_Time_Known_Value (Hi)
1101 then
1102 Initialization_Warning (T);
1103 return Empty;
1104 end if;
1106 if Is_Record_Type (Comp_Type)
1107 and then Present (Base_Init_Proc (Comp_Type))
1108 then
1109 Expr := Static_Initialization (Base_Init_Proc (Comp_Type));
1111 if No (Expr) then
1112 Initialization_Warning (T);
1113 return Empty;
1114 end if;
1116 else
1117 Initialization_Warning (T);
1118 return Empty;
1119 end if;
1121 Aggr := Make_Aggregate (Loc, No_List, New_List);
1122 Set_Etype (Aggr, T);
1123 Set_Aggregate_Bounds (Aggr,
1124 Make_Range (Loc,
1125 Low_Bound => New_Copy (Lo),
1126 High_Bound => New_Copy (Hi)));
1127 Set_Parent (Aggr, Parent (Proc));
1129 Append_To (Component_Associations (Aggr),
1130 Make_Component_Association (Loc,
1131 Choices =>
1132 New_List (
1133 Make_Range (Loc,
1134 Low_Bound => New_Copy (Lo),
1135 High_Bound => New_Copy (Hi))),
1136 Expression => Expr));
1138 if Static_Array_Aggregate (Aggr) then
1139 return Aggr;
1140 else
1141 Initialization_Warning (T);
1142 return Empty;
1143 end if;
1144 end Build_Equivalent_Array_Aggregate;
1146 ---------------------------------------
1147 -- Build_Equivalent_Record_Aggregate --
1148 ---------------------------------------
1150 function Build_Equivalent_Record_Aggregate (T : Entity_Id) return Node_Id is
1151 Agg : Node_Id;
1152 Comp : Entity_Id;
1153 Comp_Type : Entity_Id;
1155 -- Start of processing for Build_Equivalent_Record_Aggregate
1157 begin
1158 if not Is_Record_Type (T)
1159 or else Has_Discriminants (T)
1160 or else Is_Limited_Type (T)
1161 or else Has_Non_Standard_Rep (T)
1162 then
1163 Initialization_Warning (T);
1164 return Empty;
1165 end if;
1167 Comp := First_Component (T);
1169 -- A null record needs no warning
1171 if No (Comp) then
1172 return Empty;
1173 end if;
1175 while Present (Comp) loop
1177 -- Array components are acceptable if initialized by a positional
1178 -- aggregate with static components.
1180 if Is_Array_Type (Etype (Comp)) then
1181 Comp_Type := Component_Type (Etype (Comp));
1183 if Nkind (Parent (Comp)) /= N_Component_Declaration
1184 or else No (Expression (Parent (Comp)))
1185 or else Nkind (Expression (Parent (Comp))) /= N_Aggregate
1186 then
1187 Initialization_Warning (T);
1188 return Empty;
1190 elsif Is_Scalar_Type (Component_Type (Etype (Comp)))
1191 and then
1192 (not Compile_Time_Known_Value (Type_Low_Bound (Comp_Type))
1193 or else
1194 not Compile_Time_Known_Value (Type_High_Bound (Comp_Type)))
1195 then
1196 Initialization_Warning (T);
1197 return Empty;
1199 elsif
1200 not Static_Array_Aggregate (Expression (Parent (Comp)))
1201 then
1202 Initialization_Warning (T);
1203 return Empty;
1204 end if;
1206 elsif Is_Scalar_Type (Etype (Comp)) then
1207 Comp_Type := Etype (Comp);
1209 if Nkind (Parent (Comp)) /= N_Component_Declaration
1210 or else No (Expression (Parent (Comp)))
1211 or else not Compile_Time_Known_Value (Expression (Parent (Comp)))
1212 or else not Compile_Time_Known_Value (Type_Low_Bound (Comp_Type))
1213 or else not
1214 Compile_Time_Known_Value (Type_High_Bound (Comp_Type))
1215 then
1216 Initialization_Warning (T);
1217 return Empty;
1218 end if;
1220 -- For now, other types are excluded
1222 else
1223 Initialization_Warning (T);
1224 return Empty;
1225 end if;
1227 Next_Component (Comp);
1228 end loop;
1230 -- All components have static initialization. Build positional aggregate
1231 -- from the given expressions or defaults.
1233 Agg := Make_Aggregate (Sloc (T), New_List, New_List);
1234 Set_Parent (Agg, Parent (T));
1236 Comp := First_Component (T);
1237 while Present (Comp) loop
1238 Append
1239 (New_Copy_Tree (Expression (Parent (Comp))), Expressions (Agg));
1240 Next_Component (Comp);
1241 end loop;
1243 Analyze_And_Resolve (Agg, T);
1244 return Agg;
1245 end Build_Equivalent_Record_Aggregate;
1247 -------------------------------
1248 -- Build_Initialization_Call --
1249 -------------------------------
1251 -- References to a discriminant inside the record type declaration can
1252 -- appear either in the subtype_indication to constrain a record or an
1253 -- array, or as part of a larger expression given for the initial value
1254 -- of a component. In both of these cases N appears in the record
1255 -- initialization procedure and needs to be replaced by the formal
1256 -- parameter of the initialization procedure which corresponds to that
1257 -- discriminant.
1259 -- In the example below, references to discriminants D1 and D2 in proc_1
1260 -- are replaced by references to formals with the same name
1261 -- (discriminals)
1263 -- A similar replacement is done for calls to any record initialization
1264 -- procedure for any components that are themselves of a record type.
1266 -- type R (D1, D2 : Integer) is record
1267 -- X : Integer := F * D1;
1268 -- Y : Integer := F * D2;
1269 -- end record;
1271 -- procedure proc_1 (Out_2 : out R; D1 : Integer; D2 : Integer) is
1272 -- begin
1273 -- Out_2.D1 := D1;
1274 -- Out_2.D2 := D2;
1275 -- Out_2.X := F * D1;
1276 -- Out_2.Y := F * D2;
1277 -- end;
1279 function Build_Initialization_Call
1280 (Loc : Source_Ptr;
1281 Id_Ref : Node_Id;
1282 Typ : Entity_Id;
1283 In_Init_Proc : Boolean := False;
1284 Enclos_Type : Entity_Id := Empty;
1285 Discr_Map : Elist_Id := New_Elmt_List;
1286 With_Default_Init : Boolean := False;
1287 Constructor_Ref : Node_Id := Empty) return List_Id
1289 Res : constant List_Id := New_List;
1290 Arg : Node_Id;
1291 Args : List_Id;
1292 Decls : List_Id;
1293 Decl : Node_Id;
1294 Discr : Entity_Id;
1295 First_Arg : Node_Id;
1296 Full_Init_Type : Entity_Id;
1297 Full_Type : Entity_Id;
1298 Init_Type : Entity_Id;
1299 Proc : Entity_Id;
1301 begin
1302 pragma Assert (Constructor_Ref = Empty
1303 or else Is_CPP_Constructor_Call (Constructor_Ref));
1305 if No (Constructor_Ref) then
1306 Proc := Base_Init_Proc (Typ);
1307 else
1308 Proc := Base_Init_Proc (Typ, Entity (Name (Constructor_Ref)));
1309 end if;
1311 pragma Assert (Present (Proc));
1312 Init_Type := Etype (First_Formal (Proc));
1313 Full_Init_Type := Underlying_Type (Init_Type);
1315 -- Nothing to do if the Init_Proc is null, unless Initialize_Scalars
1316 -- is active (in which case we make the call anyway, since in the
1317 -- actual compiled client it may be non null).
1319 if Is_Null_Init_Proc (Proc) and then not Init_Or_Norm_Scalars then
1320 return Empty_List;
1321 end if;
1323 -- Use the [underlying] full view when dealing with a private type. This
1324 -- may require several steps depending on derivations.
1326 Full_Type := Typ;
1327 loop
1328 if Is_Private_Type (Full_Type) then
1329 if Present (Full_View (Full_Type)) then
1330 Full_Type := Full_View (Full_Type);
1332 elsif Present (Underlying_Full_View (Full_Type)) then
1333 Full_Type := Underlying_Full_View (Full_Type);
1335 -- When a private type acts as a generic actual and lacks a full
1336 -- view, use the base type.
1338 elsif Is_Generic_Actual_Type (Full_Type) then
1339 Full_Type := Base_Type (Full_Type);
1341 -- The loop has recovered the [underlying] full view, stop the
1342 -- traversal.
1344 else
1345 exit;
1346 end if;
1348 -- The type is not private, nothing to do
1350 else
1351 exit;
1352 end if;
1353 end loop;
1355 -- If Typ is derived, the procedure is the initialization procedure for
1356 -- the root type. Wrap the argument in an conversion to make it type
1357 -- honest. Actually it isn't quite type honest, because there can be
1358 -- conflicts of views in the private type case. That is why we set
1359 -- Conversion_OK in the conversion node.
1361 if (Is_Record_Type (Typ)
1362 or else Is_Array_Type (Typ)
1363 or else Is_Private_Type (Typ))
1364 and then Init_Type /= Base_Type (Typ)
1365 then
1366 First_Arg := OK_Convert_To (Etype (Init_Type), Id_Ref);
1367 Set_Etype (First_Arg, Init_Type);
1369 else
1370 First_Arg := Id_Ref;
1371 end if;
1373 Args := New_List (Convert_Concurrent (First_Arg, Typ));
1375 -- In the tasks case, add _Master as the value of the _Master parameter
1376 -- and _Chain as the value of the _Chain parameter. At the outer level,
1377 -- these will be variables holding the corresponding values obtained
1378 -- from GNARL. At inner levels, they will be the parameters passed down
1379 -- through the outer routines.
1381 if Has_Task (Full_Type) then
1382 if Restriction_Active (No_Task_Hierarchy) then
1383 Append_To (Args,
1384 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
1385 else
1386 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
1387 end if;
1389 -- Add _Chain (not done for sequential elaboration policy, see
1390 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
1392 if Partition_Elaboration_Policy /= 'S' then
1393 Append_To (Args, Make_Identifier (Loc, Name_uChain));
1394 end if;
1396 -- Ada 2005 (AI-287): In case of default initialized components
1397 -- with tasks, we generate a null string actual parameter.
1398 -- This is just a workaround that must be improved later???
1400 if With_Default_Init then
1401 Append_To (Args,
1402 Make_String_Literal (Loc,
1403 Strval => ""));
1405 else
1406 Decls :=
1407 Build_Task_Image_Decls (Loc, Id_Ref, Enclos_Type, In_Init_Proc);
1408 Decl := Last (Decls);
1410 Append_To (Args,
1411 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
1412 Append_List (Decls, Res);
1413 end if;
1415 else
1416 Decls := No_List;
1417 Decl := Empty;
1418 end if;
1420 -- Add discriminant values if discriminants are present
1422 if Has_Discriminants (Full_Init_Type) then
1423 Discr := First_Discriminant (Full_Init_Type);
1424 while Present (Discr) loop
1426 -- If this is a discriminated concurrent type, the init_proc
1427 -- for the corresponding record is being called. Use that type
1428 -- directly to find the discriminant value, to handle properly
1429 -- intervening renamed discriminants.
1431 declare
1432 T : Entity_Id := Full_Type;
1434 begin
1435 if Is_Protected_Type (T) then
1436 T := Corresponding_Record_Type (T);
1437 end if;
1439 Arg :=
1440 Get_Discriminant_Value (
1441 Discr,
1443 Discriminant_Constraint (Full_Type));
1444 end;
1446 -- If the target has access discriminants, and is constrained by
1447 -- an access to the enclosing construct, i.e. a current instance,
1448 -- replace the reference to the type by a reference to the object.
1450 if Nkind (Arg) = N_Attribute_Reference
1451 and then Is_Access_Type (Etype (Arg))
1452 and then Is_Entity_Name (Prefix (Arg))
1453 and then Is_Type (Entity (Prefix (Arg)))
1454 then
1455 Arg :=
1456 Make_Attribute_Reference (Loc,
1457 Prefix => New_Copy (Prefix (Id_Ref)),
1458 Attribute_Name => Name_Unrestricted_Access);
1460 elsif In_Init_Proc then
1462 -- Replace any possible references to the discriminant in the
1463 -- call to the record initialization procedure with references
1464 -- to the appropriate formal parameter.
1466 if Nkind (Arg) = N_Identifier
1467 and then Ekind (Entity (Arg)) = E_Discriminant
1468 then
1469 Arg := New_Occurrence_Of (Discriminal (Entity (Arg)), Loc);
1471 -- Otherwise make a copy of the default expression. Note that
1472 -- we use the current Sloc for this, because we do not want the
1473 -- call to appear to be at the declaration point. Within the
1474 -- expression, replace discriminants with their discriminals.
1476 else
1477 Arg :=
1478 New_Copy_Tree (Arg, Map => Discr_Map, New_Sloc => Loc);
1479 end if;
1481 else
1482 if Is_Constrained (Full_Type) then
1483 Arg := Duplicate_Subexpr_No_Checks (Arg);
1484 else
1485 -- The constraints come from the discriminant default exps,
1486 -- they must be reevaluated, so we use New_Copy_Tree but we
1487 -- ensure the proper Sloc (for any embedded calls).
1489 Arg := New_Copy_Tree (Arg, New_Sloc => Loc);
1490 end if;
1491 end if;
1493 -- Ada 2005 (AI-287): In case of default initialized components,
1494 -- if the component is constrained with a discriminant of the
1495 -- enclosing type, we need to generate the corresponding selected
1496 -- component node to access the discriminant value. In other cases
1497 -- this is not required, either because we are inside the init
1498 -- proc and we use the corresponding formal, or else because the
1499 -- component is constrained by an expression.
1501 if With_Default_Init
1502 and then Nkind (Id_Ref) = N_Selected_Component
1503 and then Nkind (Arg) = N_Identifier
1504 and then Ekind (Entity (Arg)) = E_Discriminant
1505 then
1506 Append_To (Args,
1507 Make_Selected_Component (Loc,
1508 Prefix => New_Copy_Tree (Prefix (Id_Ref)),
1509 Selector_Name => Arg));
1510 else
1511 Append_To (Args, Arg);
1512 end if;
1514 Next_Discriminant (Discr);
1515 end loop;
1516 end if;
1518 -- If this is a call to initialize the parent component of a derived
1519 -- tagged type, indicate that the tag should not be set in the parent.
1521 if Is_Tagged_Type (Full_Init_Type)
1522 and then not Is_CPP_Class (Full_Init_Type)
1523 and then Nkind (Id_Ref) = N_Selected_Component
1524 and then Chars (Selector_Name (Id_Ref)) = Name_uParent
1525 then
1526 Append_To (Args, New_Occurrence_Of (Standard_False, Loc));
1528 elsif Present (Constructor_Ref) then
1529 Append_List_To (Args,
1530 New_Copy_List (Parameter_Associations (Constructor_Ref)));
1531 end if;
1533 Append_To (Res,
1534 Make_Procedure_Call_Statement (Loc,
1535 Name => New_Occurrence_Of (Proc, Loc),
1536 Parameter_Associations => Args));
1538 if Needs_Finalization (Typ)
1539 and then Nkind (Id_Ref) = N_Selected_Component
1540 then
1541 if Chars (Selector_Name (Id_Ref)) /= Name_uParent then
1542 Append_To (Res,
1543 Make_Init_Call
1544 (Obj_Ref => New_Copy_Tree (First_Arg),
1545 Typ => Typ));
1546 end if;
1547 end if;
1549 return Res;
1551 exception
1552 when RE_Not_Available =>
1553 return Empty_List;
1554 end Build_Initialization_Call;
1556 ----------------------------
1557 -- Build_Record_Init_Proc --
1558 ----------------------------
1560 procedure Build_Record_Init_Proc (N : Node_Id; Rec_Ent : Entity_Id) is
1561 Decls : constant List_Id := New_List;
1562 Discr_Map : constant Elist_Id := New_Elmt_List;
1563 Loc : constant Source_Ptr := Sloc (Rec_Ent);
1564 Counter : Nat := 0;
1565 Proc_Id : Entity_Id;
1566 Rec_Type : Entity_Id;
1567 Set_Tag : Entity_Id := Empty;
1569 function Build_Assignment (Id : Entity_Id; N : Node_Id) return List_Id;
1570 -- Build an assignment statement which assigns the default expression
1571 -- to its corresponding record component if defined. The left hand side
1572 -- of the assignment is marked Assignment_OK so that initialization of
1573 -- limited private records works correctly. This routine may also build
1574 -- an adjustment call if the component is controlled.
1576 procedure Build_Discriminant_Assignments (Statement_List : List_Id);
1577 -- If the record has discriminants, add assignment statements to
1578 -- Statement_List to initialize the discriminant values from the
1579 -- arguments of the initialization procedure.
1581 function Build_Init_Statements (Comp_List : Node_Id) return List_Id;
1582 -- Build a list representing a sequence of statements which initialize
1583 -- components of the given component list. This may involve building
1584 -- case statements for the variant parts. Append any locally declared
1585 -- objects on list Decls.
1587 function Build_Init_Call_Thru (Parameters : List_Id) return List_Id;
1588 -- Given an untagged type-derivation that declares discriminants, e.g.
1590 -- type R (R1, R2 : Integer) is record ... end record;
1591 -- type D (D1 : Integer) is new R (1, D1);
1593 -- we make the _init_proc of D be
1595 -- procedure _init_proc (X : D; D1 : Integer) is
1596 -- begin
1597 -- _init_proc (R (X), 1, D1);
1598 -- end _init_proc;
1600 -- This function builds the call statement in this _init_proc.
1602 procedure Build_CPP_Init_Procedure;
1603 -- Build the tree corresponding to the procedure specification and body
1604 -- of the IC procedure that initializes the C++ part of the dispatch
1605 -- table of an Ada tagged type that is a derivation of a CPP type.
1606 -- Install it as the CPP_Init TSS.
1608 procedure Build_Init_Procedure;
1609 -- Build the tree corresponding to the procedure specification and body
1610 -- of the initialization procedure and install it as the _init TSS.
1612 procedure Build_Offset_To_Top_Functions;
1613 -- Ada 2005 (AI-251): Build the tree corresponding to the procedure spec
1614 -- and body of Offset_To_Top, a function used in conjuction with types
1615 -- having secondary dispatch tables.
1617 procedure Build_Record_Checks (S : Node_Id; Check_List : List_Id);
1618 -- Add range checks to components of discriminated records. S is a
1619 -- subtype indication of a record component. Check_List is a list
1620 -- to which the check actions are appended.
1622 function Component_Needs_Simple_Initialization
1623 (T : Entity_Id) return Boolean;
1624 -- Determine if a component needs simple initialization, given its type
1625 -- T. This routine is the same as Needs_Simple_Initialization except for
1626 -- components of type Tag and Interface_Tag. These two access types do
1627 -- not require initialization since they are explicitly initialized by
1628 -- other means.
1630 function Parent_Subtype_Renaming_Discrims return Boolean;
1631 -- Returns True for base types N that rename discriminants, else False
1633 function Requires_Init_Proc (Rec_Id : Entity_Id) return Boolean;
1634 -- Determine whether a record initialization procedure needs to be
1635 -- generated for the given record type.
1637 ----------------------
1638 -- Build_Assignment --
1639 ----------------------
1641 function Build_Assignment (Id : Entity_Id; N : Node_Id) return List_Id is
1642 N_Loc : constant Source_Ptr := Sloc (N);
1643 Typ : constant Entity_Id := Underlying_Type (Etype (Id));
1644 Exp : Node_Id := N;
1645 Kind : Node_Kind := Nkind (N);
1646 Lhs : Node_Id;
1647 Res : List_Id;
1649 begin
1650 Lhs :=
1651 Make_Selected_Component (N_Loc,
1652 Prefix => Make_Identifier (Loc, Name_uInit),
1653 Selector_Name => New_Occurrence_Of (Id, N_Loc));
1654 Set_Assignment_OK (Lhs);
1656 -- Case of an access attribute applied to the current instance.
1657 -- Replace the reference to the type by a reference to the actual
1658 -- object. (Note that this handles the case of the top level of
1659 -- the expression being given by such an attribute, but does not
1660 -- cover uses nested within an initial value expression. Nested
1661 -- uses are unlikely to occur in practice, but are theoretically
1662 -- possible.) It is not clear how to handle them without fully
1663 -- traversing the expression. ???
1665 if Kind = N_Attribute_Reference
1666 and then Nam_In (Attribute_Name (N), Name_Unchecked_Access,
1667 Name_Unrestricted_Access)
1668 and then Is_Entity_Name (Prefix (N))
1669 and then Is_Type (Entity (Prefix (N)))
1670 and then Entity (Prefix (N)) = Rec_Type
1671 then
1672 Exp :=
1673 Make_Attribute_Reference (N_Loc,
1674 Prefix =>
1675 Make_Identifier (N_Loc, Name_uInit),
1676 Attribute_Name => Name_Unrestricted_Access);
1677 end if;
1679 -- Take a copy of Exp to ensure that later copies of this component
1680 -- declaration in derived types see the original tree, not a node
1681 -- rewritten during expansion of the init_proc. If the copy contains
1682 -- itypes, the scope of the new itypes is the init_proc being built.
1684 Exp := New_Copy_Tree (Exp, New_Scope => Proc_Id);
1686 Res := New_List (
1687 Make_Assignment_Statement (Loc,
1688 Name => Lhs,
1689 Expression => Exp));
1691 Set_No_Ctrl_Actions (First (Res));
1693 -- Adjust the tag if tagged (because of possible view conversions).
1694 -- Suppress the tag adjustment when not Tagged_Type_Expansion because
1695 -- tags are represented implicitly in objects.
1697 if Is_Tagged_Type (Typ) and then Tagged_Type_Expansion then
1698 Append_To (Res,
1699 Make_Assignment_Statement (N_Loc,
1700 Name =>
1701 Make_Selected_Component (N_Loc,
1702 Prefix =>
1703 New_Copy_Tree (Lhs, New_Scope => Proc_Id),
1704 Selector_Name =>
1705 New_Occurrence_Of (First_Tag_Component (Typ), N_Loc)),
1707 Expression =>
1708 Unchecked_Convert_To (RTE (RE_Tag),
1709 New_Occurrence_Of
1710 (Node
1711 (First_Elmt
1712 (Access_Disp_Table (Underlying_Type (Typ)))),
1713 N_Loc))));
1714 end if;
1716 -- Adjust the component if controlled except if it is an aggregate
1717 -- that will be expanded inline.
1719 if Kind = N_Qualified_Expression then
1720 Kind := Nkind (Expression (N));
1721 end if;
1723 if Needs_Finalization (Typ)
1724 and then not (Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate))
1725 and then not Is_Limited_View (Typ)
1726 then
1727 Append_To (Res,
1728 Make_Adjust_Call
1729 (Obj_Ref => New_Copy_Tree (Lhs),
1730 Typ => Etype (Id)));
1731 end if;
1733 return Res;
1735 exception
1736 when RE_Not_Available =>
1737 return Empty_List;
1738 end Build_Assignment;
1740 ------------------------------------
1741 -- Build_Discriminant_Assignments --
1742 ------------------------------------
1744 procedure Build_Discriminant_Assignments (Statement_List : List_Id) is
1745 Is_Tagged : constant Boolean := Is_Tagged_Type (Rec_Type);
1746 D : Entity_Id;
1747 D_Loc : Source_Ptr;
1749 begin
1750 if Has_Discriminants (Rec_Type)
1751 and then not Is_Unchecked_Union (Rec_Type)
1752 then
1753 D := First_Discriminant (Rec_Type);
1754 while Present (D) loop
1756 -- Don't generate the assignment for discriminants in derived
1757 -- tagged types if the discriminant is a renaming of some
1758 -- ancestor discriminant. This initialization will be done
1759 -- when initializing the _parent field of the derived record.
1761 if Is_Tagged
1762 and then Present (Corresponding_Discriminant (D))
1763 then
1764 null;
1766 else
1767 D_Loc := Sloc (D);
1768 Append_List_To (Statement_List,
1769 Build_Assignment (D,
1770 New_Occurrence_Of (Discriminal (D), D_Loc)));
1771 end if;
1773 Next_Discriminant (D);
1774 end loop;
1775 end if;
1776 end Build_Discriminant_Assignments;
1778 --------------------------
1779 -- Build_Init_Call_Thru --
1780 --------------------------
1782 function Build_Init_Call_Thru (Parameters : List_Id) return List_Id is
1783 Parent_Proc : constant Entity_Id :=
1784 Base_Init_Proc (Etype (Rec_Type));
1786 Parent_Type : constant Entity_Id :=
1787 Etype (First_Formal (Parent_Proc));
1789 Uparent_Type : constant Entity_Id :=
1790 Underlying_Type (Parent_Type);
1792 First_Discr_Param : Node_Id;
1794 Arg : Node_Id;
1795 Args : List_Id;
1796 First_Arg : Node_Id;
1797 Parent_Discr : Entity_Id;
1798 Res : List_Id;
1800 begin
1801 -- First argument (_Init) is the object to be initialized.
1802 -- ??? not sure where to get a reasonable Loc for First_Arg
1804 First_Arg :=
1805 OK_Convert_To (Parent_Type,
1806 New_Occurrence_Of
1807 (Defining_Identifier (First (Parameters)), Loc));
1809 Set_Etype (First_Arg, Parent_Type);
1811 Args := New_List (Convert_Concurrent (First_Arg, Rec_Type));
1813 -- In the tasks case,
1814 -- add _Master as the value of the _Master parameter
1815 -- add _Chain as the value of the _Chain parameter.
1816 -- add _Task_Name as the value of the _Task_Name parameter.
1817 -- At the outer level, these will be variables holding the
1818 -- corresponding values obtained from GNARL or the expander.
1820 -- At inner levels, they will be the parameters passed down through
1821 -- the outer routines.
1823 First_Discr_Param := Next (First (Parameters));
1825 if Has_Task (Rec_Type) then
1826 if Restriction_Active (No_Task_Hierarchy) then
1827 Append_To (Args,
1828 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
1829 else
1830 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
1831 end if;
1833 -- Add _Chain (not done for sequential elaboration policy, see
1834 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
1836 if Partition_Elaboration_Policy /= 'S' then
1837 Append_To (Args, Make_Identifier (Loc, Name_uChain));
1838 end if;
1840 Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
1841 First_Discr_Param := Next (Next (Next (First_Discr_Param)));
1842 end if;
1844 -- Append discriminant values
1846 if Has_Discriminants (Uparent_Type) then
1847 pragma Assert (not Is_Tagged_Type (Uparent_Type));
1849 Parent_Discr := First_Discriminant (Uparent_Type);
1850 while Present (Parent_Discr) loop
1852 -- Get the initial value for this discriminant
1853 -- ??? needs to be cleaned up to use parent_Discr_Constr
1854 -- directly.
1856 declare
1857 Discr : Entity_Id :=
1858 First_Stored_Discriminant (Uparent_Type);
1860 Discr_Value : Elmt_Id :=
1861 First_Elmt (Stored_Constraint (Rec_Type));
1863 begin
1864 while Original_Record_Component (Parent_Discr) /= Discr loop
1865 Next_Stored_Discriminant (Discr);
1866 Next_Elmt (Discr_Value);
1867 end loop;
1869 Arg := Node (Discr_Value);
1870 end;
1872 -- Append it to the list
1874 if Nkind (Arg) = N_Identifier
1875 and then Ekind (Entity (Arg)) = E_Discriminant
1876 then
1877 Append_To (Args,
1878 New_Occurrence_Of (Discriminal (Entity (Arg)), Loc));
1880 -- Case of access discriminants. We replace the reference
1881 -- to the type by a reference to the actual object.
1883 -- Is above comment right??? Use of New_Copy below seems mighty
1884 -- suspicious ???
1886 else
1887 Append_To (Args, New_Copy (Arg));
1888 end if;
1890 Next_Discriminant (Parent_Discr);
1891 end loop;
1892 end if;
1894 Res :=
1895 New_List (
1896 Make_Procedure_Call_Statement (Loc,
1897 Name =>
1898 New_Occurrence_Of (Parent_Proc, Loc),
1899 Parameter_Associations => Args));
1901 return Res;
1902 end Build_Init_Call_Thru;
1904 -----------------------------------
1905 -- Build_Offset_To_Top_Functions --
1906 -----------------------------------
1908 procedure Build_Offset_To_Top_Functions is
1910 procedure Build_Offset_To_Top_Function (Iface_Comp : Entity_Id);
1911 -- Generate:
1912 -- function Fxx (O : Address) return Storage_Offset is
1913 -- type Acc is access all <Typ>;
1914 -- begin
1915 -- return Acc!(O).Iface_Comp'Position;
1916 -- end Fxx;
1918 ----------------------------------
1919 -- Build_Offset_To_Top_Function --
1920 ----------------------------------
1922 procedure Build_Offset_To_Top_Function (Iface_Comp : Entity_Id) is
1923 Body_Node : Node_Id;
1924 Func_Id : Entity_Id;
1925 Spec_Node : Node_Id;
1926 Acc_Type : Entity_Id;
1928 begin
1929 Func_Id := Make_Temporary (Loc, 'F');
1930 Set_DT_Offset_To_Top_Func (Iface_Comp, Func_Id);
1932 -- Generate
1933 -- function Fxx (O : in Rec_Typ) return Storage_Offset;
1935 Spec_Node := New_Node (N_Function_Specification, Loc);
1936 Set_Defining_Unit_Name (Spec_Node, Func_Id);
1937 Set_Parameter_Specifications (Spec_Node, New_List (
1938 Make_Parameter_Specification (Loc,
1939 Defining_Identifier =>
1940 Make_Defining_Identifier (Loc, Name_uO),
1941 In_Present => True,
1942 Parameter_Type =>
1943 New_Occurrence_Of (RTE (RE_Address), Loc))));
1944 Set_Result_Definition (Spec_Node,
1945 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1947 -- Generate
1948 -- function Fxx (O : in Rec_Typ) return Storage_Offset is
1949 -- begin
1950 -- return O.Iface_Comp'Position;
1951 -- end Fxx;
1953 Body_Node := New_Node (N_Subprogram_Body, Loc);
1954 Set_Specification (Body_Node, Spec_Node);
1956 Acc_Type := Make_Temporary (Loc, 'T');
1957 Set_Declarations (Body_Node, New_List (
1958 Make_Full_Type_Declaration (Loc,
1959 Defining_Identifier => Acc_Type,
1960 Type_Definition =>
1961 Make_Access_To_Object_Definition (Loc,
1962 All_Present => True,
1963 Null_Exclusion_Present => False,
1964 Constant_Present => False,
1965 Subtype_Indication =>
1966 New_Occurrence_Of (Rec_Type, Loc)))));
1968 Set_Handled_Statement_Sequence (Body_Node,
1969 Make_Handled_Sequence_Of_Statements (Loc,
1970 Statements => New_List (
1971 Make_Simple_Return_Statement (Loc,
1972 Expression =>
1973 Make_Attribute_Reference (Loc,
1974 Prefix =>
1975 Make_Selected_Component (Loc,
1976 Prefix =>
1977 Unchecked_Convert_To (Acc_Type,
1978 Make_Identifier (Loc, Name_uO)),
1979 Selector_Name =>
1980 New_Occurrence_Of (Iface_Comp, Loc)),
1981 Attribute_Name => Name_Position)))));
1983 Set_Ekind (Func_Id, E_Function);
1984 Set_Mechanism (Func_Id, Default_Mechanism);
1985 Set_Is_Internal (Func_Id, True);
1987 if not Debug_Generated_Code then
1988 Set_Debug_Info_Off (Func_Id);
1989 end if;
1991 Analyze (Body_Node);
1993 Append_Freeze_Action (Rec_Type, Body_Node);
1994 end Build_Offset_To_Top_Function;
1996 -- Local variables
1998 Iface_Comp : Node_Id;
1999 Iface_Comp_Elmt : Elmt_Id;
2000 Ifaces_Comp_List : Elist_Id;
2002 -- Start of processing for Build_Offset_To_Top_Functions
2004 begin
2005 -- Offset_To_Top_Functions are built only for derivations of types
2006 -- with discriminants that cover interface types.
2007 -- Nothing is needed either in case of virtual targets, since
2008 -- interfaces are handled directly by the target.
2010 if not Is_Tagged_Type (Rec_Type)
2011 or else Etype (Rec_Type) = Rec_Type
2012 or else not Has_Discriminants (Etype (Rec_Type))
2013 or else not Tagged_Type_Expansion
2014 then
2015 return;
2016 end if;
2018 Collect_Interface_Components (Rec_Type, Ifaces_Comp_List);
2020 -- For each interface type with secondary dispatch table we generate
2021 -- the Offset_To_Top_Functions (required to displace the pointer in
2022 -- interface conversions)
2024 Iface_Comp_Elmt := First_Elmt (Ifaces_Comp_List);
2025 while Present (Iface_Comp_Elmt) loop
2026 Iface_Comp := Node (Iface_Comp_Elmt);
2027 pragma Assert (Is_Interface (Related_Type (Iface_Comp)));
2029 -- If the interface is a parent of Rec_Type it shares the primary
2030 -- dispatch table and hence there is no need to build the function
2032 if not Is_Ancestor (Related_Type (Iface_Comp), Rec_Type,
2033 Use_Full_View => True)
2034 then
2035 Build_Offset_To_Top_Function (Iface_Comp);
2036 end if;
2038 Next_Elmt (Iface_Comp_Elmt);
2039 end loop;
2040 end Build_Offset_To_Top_Functions;
2042 ------------------------------
2043 -- Build_CPP_Init_Procedure --
2044 ------------------------------
2046 procedure Build_CPP_Init_Procedure is
2047 Body_Node : Node_Id;
2048 Body_Stmts : List_Id;
2049 Flag_Id : Entity_Id;
2050 Handled_Stmt_Node : Node_Id;
2051 Init_Tags_List : List_Id;
2052 Proc_Id : Entity_Id;
2053 Proc_Spec_Node : Node_Id;
2055 begin
2056 -- Check cases requiring no IC routine
2058 if not Is_CPP_Class (Root_Type (Rec_Type))
2059 or else Is_CPP_Class (Rec_Type)
2060 or else CPP_Num_Prims (Rec_Type) = 0
2061 or else not Tagged_Type_Expansion
2062 or else No_Run_Time_Mode
2063 then
2064 return;
2065 end if;
2067 -- Generate:
2069 -- Flag : Boolean := False;
2071 -- procedure Typ_IC is
2072 -- begin
2073 -- if not Flag then
2074 -- Copy C++ dispatch table slots from parent
2075 -- Update C++ slots of overridden primitives
2076 -- end if;
2077 -- end;
2079 Flag_Id := Make_Temporary (Loc, 'F');
2081 Append_Freeze_Action (Rec_Type,
2082 Make_Object_Declaration (Loc,
2083 Defining_Identifier => Flag_Id,
2084 Object_Definition =>
2085 New_Occurrence_Of (Standard_Boolean, Loc),
2086 Expression =>
2087 New_Occurrence_Of (Standard_True, Loc)));
2089 Body_Stmts := New_List;
2090 Body_Node := New_Node (N_Subprogram_Body, Loc);
2092 Proc_Spec_Node := New_Node (N_Procedure_Specification, Loc);
2094 Proc_Id :=
2095 Make_Defining_Identifier (Loc,
2096 Chars => Make_TSS_Name (Rec_Type, TSS_CPP_Init_Proc));
2098 Set_Ekind (Proc_Id, E_Procedure);
2099 Set_Is_Internal (Proc_Id);
2101 Set_Defining_Unit_Name (Proc_Spec_Node, Proc_Id);
2103 Set_Parameter_Specifications (Proc_Spec_Node, New_List);
2104 Set_Specification (Body_Node, Proc_Spec_Node);
2105 Set_Declarations (Body_Node, New_List);
2107 Init_Tags_List := Build_Inherit_CPP_Prims (Rec_Type);
2109 Append_To (Init_Tags_List,
2110 Make_Assignment_Statement (Loc,
2111 Name =>
2112 New_Occurrence_Of (Flag_Id, Loc),
2113 Expression =>
2114 New_Occurrence_Of (Standard_False, Loc)));
2116 Append_To (Body_Stmts,
2117 Make_If_Statement (Loc,
2118 Condition => New_Occurrence_Of (Flag_Id, Loc),
2119 Then_Statements => Init_Tags_List));
2121 Handled_Stmt_Node :=
2122 New_Node (N_Handled_Sequence_Of_Statements, Loc);
2123 Set_Statements (Handled_Stmt_Node, Body_Stmts);
2124 Set_Exception_Handlers (Handled_Stmt_Node, No_List);
2125 Set_Handled_Statement_Sequence (Body_Node, Handled_Stmt_Node);
2127 if not Debug_Generated_Code then
2128 Set_Debug_Info_Off (Proc_Id);
2129 end if;
2131 -- Associate CPP_Init_Proc with type
2133 Set_Init_Proc (Rec_Type, Proc_Id);
2134 end Build_CPP_Init_Procedure;
2136 --------------------------
2137 -- Build_Init_Procedure --
2138 --------------------------
2140 procedure Build_Init_Procedure is
2141 Body_Stmts : List_Id;
2142 Body_Node : Node_Id;
2143 Handled_Stmt_Node : Node_Id;
2144 Init_Tags_List : List_Id;
2145 Parameters : List_Id;
2146 Proc_Spec_Node : Node_Id;
2147 Record_Extension_Node : Node_Id;
2149 begin
2150 Body_Stmts := New_List;
2151 Body_Node := New_Node (N_Subprogram_Body, Loc);
2152 Set_Ekind (Proc_Id, E_Procedure);
2154 Proc_Spec_Node := New_Node (N_Procedure_Specification, Loc);
2155 Set_Defining_Unit_Name (Proc_Spec_Node, Proc_Id);
2157 Parameters := Init_Formals (Rec_Type);
2158 Append_List_To (Parameters,
2159 Build_Discriminant_Formals (Rec_Type, True));
2161 -- For tagged types, we add a flag to indicate whether the routine
2162 -- is called to initialize a parent component in the init_proc of
2163 -- a type extension. If the flag is false, we do not set the tag
2164 -- because it has been set already in the extension.
2166 if Is_Tagged_Type (Rec_Type) then
2167 Set_Tag := Make_Temporary (Loc, 'P');
2169 Append_To (Parameters,
2170 Make_Parameter_Specification (Loc,
2171 Defining_Identifier => Set_Tag,
2172 Parameter_Type =>
2173 New_Occurrence_Of (Standard_Boolean, Loc),
2174 Expression =>
2175 New_Occurrence_Of (Standard_True, Loc)));
2176 end if;
2178 Set_Parameter_Specifications (Proc_Spec_Node, Parameters);
2179 Set_Specification (Body_Node, Proc_Spec_Node);
2180 Set_Declarations (Body_Node, Decls);
2182 -- N is a Derived_Type_Definition that renames the parameters of the
2183 -- ancestor type. We initialize it by expanding our discriminants and
2184 -- call the ancestor _init_proc with a type-converted object.
2186 if Parent_Subtype_Renaming_Discrims then
2187 Append_List_To (Body_Stmts, Build_Init_Call_Thru (Parameters));
2189 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
2190 Build_Discriminant_Assignments (Body_Stmts);
2192 if not Null_Present (Type_Definition (N)) then
2193 Append_List_To (Body_Stmts,
2194 Build_Init_Statements (Component_List (Type_Definition (N))));
2195 end if;
2197 -- N is a Derived_Type_Definition with a possible non-empty
2198 -- extension. The initialization of a type extension consists in the
2199 -- initialization of the components in the extension.
2201 else
2202 Build_Discriminant_Assignments (Body_Stmts);
2204 Record_Extension_Node :=
2205 Record_Extension_Part (Type_Definition (N));
2207 if not Null_Present (Record_Extension_Node) then
2208 declare
2209 Stmts : constant List_Id :=
2210 Build_Init_Statements (
2211 Component_List (Record_Extension_Node));
2213 begin
2214 -- The parent field must be initialized first because the
2215 -- offset of the new discriminants may depend on it. This is
2216 -- not needed if the parent is an interface type because in
2217 -- such case the initialization of the _parent field was not
2218 -- generated.
2220 if not Is_Interface (Etype (Rec_Ent)) then
2221 declare
2222 Parent_IP : constant Name_Id :=
2223 Make_Init_Proc_Name (Etype (Rec_Ent));
2224 Stmt : Node_Id;
2225 IP_Call : Node_Id;
2226 IP_Stmts : List_Id;
2228 begin
2229 -- Look for a call to the parent IP at the beginning
2230 -- of Stmts associated with the record extension
2232 Stmt := First (Stmts);
2233 IP_Call := Empty;
2234 while Present (Stmt) loop
2235 if Nkind (Stmt) = N_Procedure_Call_Statement
2236 and then Chars (Name (Stmt)) = Parent_IP
2237 then
2238 IP_Call := Stmt;
2239 exit;
2240 end if;
2242 Next (Stmt);
2243 end loop;
2245 -- If found then move it to the beginning of the
2246 -- statements of this IP routine
2248 if Present (IP_Call) then
2249 IP_Stmts := New_List;
2250 loop
2251 Stmt := Remove_Head (Stmts);
2252 Append_To (IP_Stmts, Stmt);
2253 exit when Stmt = IP_Call;
2254 end loop;
2256 Prepend_List_To (Body_Stmts, IP_Stmts);
2257 end if;
2258 end;
2259 end if;
2261 Append_List_To (Body_Stmts, Stmts);
2262 end;
2263 end if;
2264 end if;
2266 -- Add here the assignment to instantiate the Tag
2268 -- The assignment corresponds to the code:
2270 -- _Init._Tag := Typ'Tag;
2272 -- Suppress the tag assignment when not Tagged_Type_Expansion because
2273 -- tags are represented implicitly in objects. It is also suppressed
2274 -- in case of CPP_Class types because in this case the tag is
2275 -- initialized in the C++ side.
2277 if Is_Tagged_Type (Rec_Type)
2278 and then Tagged_Type_Expansion
2279 and then not No_Run_Time_Mode
2280 then
2281 -- Case 1: Ada tagged types with no CPP ancestor. Set the tags of
2282 -- the actual object and invoke the IP of the parent (in this
2283 -- order). The tag must be initialized before the call to the IP
2284 -- of the parent and the assignments to other components because
2285 -- the initial value of the components may depend on the tag (eg.
2286 -- through a dispatching operation on an access to the current
2287 -- type). The tag assignment is not done when initializing the
2288 -- parent component of a type extension, because in that case the
2289 -- tag is set in the extension.
2291 if not Is_CPP_Class (Root_Type (Rec_Type)) then
2293 -- Initialize the primary tag component
2295 Init_Tags_List := New_List (
2296 Make_Assignment_Statement (Loc,
2297 Name =>
2298 Make_Selected_Component (Loc,
2299 Prefix => Make_Identifier (Loc, Name_uInit),
2300 Selector_Name =>
2301 New_Occurrence_Of
2302 (First_Tag_Component (Rec_Type), Loc)),
2303 Expression =>
2304 New_Occurrence_Of
2305 (Node
2306 (First_Elmt (Access_Disp_Table (Rec_Type))), Loc)));
2308 -- Ada 2005 (AI-251): Initialize the secondary tags components
2309 -- located at fixed positions (tags whose position depends on
2310 -- variable size components are initialized later ---see below)
2312 if Ada_Version >= Ada_2005
2313 and then not Is_Interface (Rec_Type)
2314 and then Has_Interfaces (Rec_Type)
2315 then
2316 Init_Secondary_Tags
2317 (Typ => Rec_Type,
2318 Target => Make_Identifier (Loc, Name_uInit),
2319 Stmts_List => Init_Tags_List,
2320 Fixed_Comps => True,
2321 Variable_Comps => False);
2322 end if;
2324 Prepend_To (Body_Stmts,
2325 Make_If_Statement (Loc,
2326 Condition => New_Occurrence_Of (Set_Tag, Loc),
2327 Then_Statements => Init_Tags_List));
2329 -- Case 2: CPP type. The imported C++ constructor takes care of
2330 -- tags initialization. No action needed here because the IP
2331 -- is built by Set_CPP_Constructors; in this case the IP is a
2332 -- wrapper that invokes the C++ constructor and copies the C++
2333 -- tags locally. Done to inherit the C++ slots in Ada derivations
2334 -- (see case 3).
2336 elsif Is_CPP_Class (Rec_Type) then
2337 pragma Assert (False);
2338 null;
2340 -- Case 3: Combined hierarchy containing C++ types and Ada tagged
2341 -- type derivations. Derivations of imported C++ classes add a
2342 -- complication, because we cannot inhibit tag setting in the
2343 -- constructor for the parent. Hence we initialize the tag after
2344 -- the call to the parent IP (that is, in reverse order compared
2345 -- with pure Ada hierarchies ---see comment on case 1).
2347 else
2348 -- Initialize the primary tag
2350 Init_Tags_List := New_List (
2351 Make_Assignment_Statement (Loc,
2352 Name =>
2353 Make_Selected_Component (Loc,
2354 Prefix => Make_Identifier (Loc, Name_uInit),
2355 Selector_Name =>
2356 New_Occurrence_Of
2357 (First_Tag_Component (Rec_Type), Loc)),
2358 Expression =>
2359 New_Occurrence_Of
2360 (Node
2361 (First_Elmt (Access_Disp_Table (Rec_Type))), Loc)));
2363 -- Ada 2005 (AI-251): Initialize the secondary tags components
2364 -- located at fixed positions (tags whose position depends on
2365 -- variable size components are initialized later ---see below)
2367 if Ada_Version >= Ada_2005
2368 and then not Is_Interface (Rec_Type)
2369 and then Has_Interfaces (Rec_Type)
2370 then
2371 Init_Secondary_Tags
2372 (Typ => Rec_Type,
2373 Target => Make_Identifier (Loc, Name_uInit),
2374 Stmts_List => Init_Tags_List,
2375 Fixed_Comps => True,
2376 Variable_Comps => False);
2377 end if;
2379 -- Initialize the tag component after invocation of parent IP.
2381 -- Generate:
2382 -- parent_IP(_init.parent); // Invokes the C++ constructor
2383 -- [ typIC; ] // Inherit C++ slots from parent
2384 -- init_tags
2386 declare
2387 Ins_Nod : Node_Id;
2389 begin
2390 -- Search for the call to the IP of the parent. We assume
2391 -- that the first init_proc call is for the parent.
2393 Ins_Nod := First (Body_Stmts);
2394 while Present (Next (Ins_Nod))
2395 and then (Nkind (Ins_Nod) /= N_Procedure_Call_Statement
2396 or else not Is_Init_Proc (Name (Ins_Nod)))
2397 loop
2398 Next (Ins_Nod);
2399 end loop;
2401 -- The IC routine copies the inherited slots of the C+ part
2402 -- of the dispatch table from the parent and updates the
2403 -- overridden C++ slots.
2405 if CPP_Num_Prims (Rec_Type) > 0 then
2406 declare
2407 Init_DT : Entity_Id;
2408 New_Nod : Node_Id;
2410 begin
2411 Init_DT := CPP_Init_Proc (Rec_Type);
2412 pragma Assert (Present (Init_DT));
2414 New_Nod :=
2415 Make_Procedure_Call_Statement (Loc,
2416 New_Occurrence_Of (Init_DT, Loc));
2417 Insert_After (Ins_Nod, New_Nod);
2419 -- Update location of init tag statements
2421 Ins_Nod := New_Nod;
2422 end;
2423 end if;
2425 Insert_List_After (Ins_Nod, Init_Tags_List);
2426 end;
2427 end if;
2429 -- Ada 2005 (AI-251): Initialize the secondary tag components
2430 -- located at variable positions. We delay the generation of this
2431 -- code until here because the value of the attribute 'Position
2432 -- applied to variable size components of the parent type that
2433 -- depend on discriminants is only safely read at runtime after
2434 -- the parent components have been initialized.
2436 if Ada_Version >= Ada_2005
2437 and then not Is_Interface (Rec_Type)
2438 and then Has_Interfaces (Rec_Type)
2439 and then Has_Discriminants (Etype (Rec_Type))
2440 and then Is_Variable_Size_Record (Etype (Rec_Type))
2441 then
2442 Init_Tags_List := New_List;
2444 Init_Secondary_Tags
2445 (Typ => Rec_Type,
2446 Target => Make_Identifier (Loc, Name_uInit),
2447 Stmts_List => Init_Tags_List,
2448 Fixed_Comps => False,
2449 Variable_Comps => True);
2451 if Is_Non_Empty_List (Init_Tags_List) then
2452 Append_List_To (Body_Stmts, Init_Tags_List);
2453 end if;
2454 end if;
2455 end if;
2457 Handled_Stmt_Node := New_Node (N_Handled_Sequence_Of_Statements, Loc);
2458 Set_Statements (Handled_Stmt_Node, Body_Stmts);
2460 -- Generate:
2461 -- Deep_Finalize (_init, C1, ..., CN);
2462 -- raise;
2464 if Counter > 0
2465 and then Needs_Finalization (Rec_Type)
2466 and then not Is_Abstract_Type (Rec_Type)
2467 and then not Restriction_Active (No_Exception_Propagation)
2468 then
2469 declare
2470 DF_Call : Node_Id;
2471 DF_Id : Entity_Id;
2473 begin
2474 -- Create a local version of Deep_Finalize which has indication
2475 -- of partial initialization state.
2477 DF_Id := Make_Temporary (Loc, 'F');
2479 Append_To (Decls, Make_Local_Deep_Finalize (Rec_Type, DF_Id));
2481 DF_Call :=
2482 Make_Procedure_Call_Statement (Loc,
2483 Name => New_Occurrence_Of (DF_Id, Loc),
2484 Parameter_Associations => New_List (
2485 Make_Identifier (Loc, Name_uInit),
2486 New_Occurrence_Of (Standard_False, Loc)));
2488 -- Do not emit warnings related to the elaboration order when a
2489 -- controlled object is declared before the body of Finalize is
2490 -- seen.
2492 Set_No_Elaboration_Check (DF_Call);
2494 Set_Exception_Handlers (Handled_Stmt_Node, New_List (
2495 Make_Exception_Handler (Loc,
2496 Exception_Choices => New_List (
2497 Make_Others_Choice (Loc)),
2498 Statements => New_List (
2499 DF_Call,
2500 Make_Raise_Statement (Loc)))));
2501 end;
2502 else
2503 Set_Exception_Handlers (Handled_Stmt_Node, No_List);
2504 end if;
2506 Set_Handled_Statement_Sequence (Body_Node, Handled_Stmt_Node);
2508 if not Debug_Generated_Code then
2509 Set_Debug_Info_Off (Proc_Id);
2510 end if;
2512 -- Associate Init_Proc with type, and determine if the procedure
2513 -- is null (happens because of the Initialize_Scalars pragma case,
2514 -- where we have to generate a null procedure in case it is called
2515 -- by a client with Initialize_Scalars set). Such procedures have
2516 -- to be generated, but do not have to be called, so we mark them
2517 -- as null to suppress the call.
2519 Set_Init_Proc (Rec_Type, Proc_Id);
2521 if List_Length (Body_Stmts) = 1
2523 -- We must skip SCIL nodes because they may have been added to this
2524 -- list by Insert_Actions.
2526 and then Nkind (First_Non_SCIL_Node (Body_Stmts)) = N_Null_Statement
2527 then
2528 Set_Is_Null_Init_Proc (Proc_Id);
2529 end if;
2530 end Build_Init_Procedure;
2532 ---------------------------
2533 -- Build_Init_Statements --
2534 ---------------------------
2536 function Build_Init_Statements (Comp_List : Node_Id) return List_Id is
2537 Checks : constant List_Id := New_List;
2538 Actions : List_Id := No_List;
2539 Counter_Id : Entity_Id := Empty;
2540 Comp_Loc : Source_Ptr;
2541 Decl : Node_Id;
2542 Has_POC : Boolean;
2543 Id : Entity_Id;
2544 Parent_Stmts : List_Id;
2545 Stmts : List_Id;
2546 Typ : Entity_Id;
2548 procedure Increment_Counter (Loc : Source_Ptr);
2549 -- Generate an "increment by one" statement for the current counter
2550 -- and append it to the list Stmts.
2552 procedure Make_Counter (Loc : Source_Ptr);
2553 -- Create a new counter for the current component list. The routine
2554 -- creates a new defining Id, adds an object declaration and sets
2555 -- the Id generator for the next variant.
2557 -----------------------
2558 -- Increment_Counter --
2559 -----------------------
2561 procedure Increment_Counter (Loc : Source_Ptr) is
2562 begin
2563 -- Generate:
2564 -- Counter := Counter + 1;
2566 Append_To (Stmts,
2567 Make_Assignment_Statement (Loc,
2568 Name => New_Occurrence_Of (Counter_Id, Loc),
2569 Expression =>
2570 Make_Op_Add (Loc,
2571 Left_Opnd => New_Occurrence_Of (Counter_Id, Loc),
2572 Right_Opnd => Make_Integer_Literal (Loc, 1))));
2573 end Increment_Counter;
2575 ------------------
2576 -- Make_Counter --
2577 ------------------
2579 procedure Make_Counter (Loc : Source_Ptr) is
2580 begin
2581 -- Increment the Id generator
2583 Counter := Counter + 1;
2585 -- Create the entity and declaration
2587 Counter_Id :=
2588 Make_Defining_Identifier (Loc,
2589 Chars => New_External_Name ('C', Counter));
2591 -- Generate:
2592 -- Cnn : Integer := 0;
2594 Append_To (Decls,
2595 Make_Object_Declaration (Loc,
2596 Defining_Identifier => Counter_Id,
2597 Object_Definition =>
2598 New_Occurrence_Of (Standard_Integer, Loc),
2599 Expression =>
2600 Make_Integer_Literal (Loc, 0)));
2601 end Make_Counter;
2603 -- Start of processing for Build_Init_Statements
2605 begin
2606 if Null_Present (Comp_List) then
2607 return New_List (Make_Null_Statement (Loc));
2608 end if;
2610 Parent_Stmts := New_List;
2611 Stmts := New_List;
2613 -- Loop through visible declarations of task types and protected
2614 -- types moving any expanded code from the spec to the body of the
2615 -- init procedure.
2617 if Is_Task_Record_Type (Rec_Type)
2618 or else Is_Protected_Record_Type (Rec_Type)
2619 then
2620 declare
2621 Decl : constant Node_Id :=
2622 Parent (Corresponding_Concurrent_Type (Rec_Type));
2623 Def : Node_Id;
2624 N1 : Node_Id;
2625 N2 : Node_Id;
2627 begin
2628 if Is_Task_Record_Type (Rec_Type) then
2629 Def := Task_Definition (Decl);
2630 else
2631 Def := Protected_Definition (Decl);
2632 end if;
2634 if Present (Def) then
2635 N1 := First (Visible_Declarations (Def));
2636 while Present (N1) loop
2637 N2 := N1;
2638 N1 := Next (N1);
2640 if Nkind (N2) in N_Statement_Other_Than_Procedure_Call
2641 or else Nkind (N2) in N_Raise_xxx_Error
2642 or else Nkind (N2) = N_Procedure_Call_Statement
2643 then
2644 Append_To (Stmts,
2645 New_Copy_Tree (N2, New_Scope => Proc_Id));
2646 Rewrite (N2, Make_Null_Statement (Sloc (N2)));
2647 Analyze (N2);
2648 end if;
2649 end loop;
2650 end if;
2651 end;
2652 end if;
2654 -- Loop through components, skipping pragmas, in 2 steps. The first
2655 -- step deals with regular components. The second step deals with
2656 -- components that have per object constraints and no explicit
2657 -- initialization.
2659 Has_POC := False;
2661 -- First pass : regular components
2663 Decl := First_Non_Pragma (Component_Items (Comp_List));
2664 while Present (Decl) loop
2665 Comp_Loc := Sloc (Decl);
2666 Build_Record_Checks
2667 (Subtype_Indication (Component_Definition (Decl)), Checks);
2669 Id := Defining_Identifier (Decl);
2670 Typ := Etype (Id);
2672 -- Leave any processing of per-object constrained component for
2673 -- the second pass.
2675 if Has_Access_Constraint (Id) and then No (Expression (Decl)) then
2676 Has_POC := True;
2678 -- Regular component cases
2680 else
2681 -- In the context of the init proc, references to discriminants
2682 -- resolve to denote the discriminals: this is where we can
2683 -- freeze discriminant dependent component subtypes.
2685 if not Is_Frozen (Typ) then
2686 Append_List_To (Stmts, Freeze_Entity (Typ, N));
2687 end if;
2689 -- Explicit initialization
2691 if Present (Expression (Decl)) then
2692 if Is_CPP_Constructor_Call (Expression (Decl)) then
2693 Actions :=
2694 Build_Initialization_Call
2695 (Comp_Loc,
2696 Id_Ref =>
2697 Make_Selected_Component (Comp_Loc,
2698 Prefix =>
2699 Make_Identifier (Comp_Loc, Name_uInit),
2700 Selector_Name =>
2701 New_Occurrence_Of (Id, Comp_Loc)),
2702 Typ => Typ,
2703 In_Init_Proc => True,
2704 Enclos_Type => Rec_Type,
2705 Discr_Map => Discr_Map,
2706 Constructor_Ref => Expression (Decl));
2707 else
2708 Actions := Build_Assignment (Id, Expression (Decl));
2709 end if;
2711 -- CPU, Dispatching_Domain, Priority and Size components are
2712 -- filled with the corresponding rep item expression of the
2713 -- concurrent type (if any).
2715 elsif Ekind (Scope (Id)) = E_Record_Type
2716 and then Present (Corresponding_Concurrent_Type (Scope (Id)))
2717 and then Nam_In (Chars (Id), Name_uCPU,
2718 Name_uDispatching_Domain,
2719 Name_uPriority)
2720 then
2721 declare
2722 Exp : Node_Id;
2723 Nam : Name_Id;
2724 Ritem : Node_Id;
2726 begin
2727 if Chars (Id) = Name_uCPU then
2728 Nam := Name_CPU;
2730 elsif Chars (Id) = Name_uDispatching_Domain then
2731 Nam := Name_Dispatching_Domain;
2733 elsif Chars (Id) = Name_uPriority then
2734 Nam := Name_Priority;
2735 end if;
2737 -- Get the Rep Item (aspect specification, attribute
2738 -- definition clause or pragma) of the corresponding
2739 -- concurrent type.
2741 Ritem :=
2742 Get_Rep_Item
2743 (Corresponding_Concurrent_Type (Scope (Id)),
2744 Nam,
2745 Check_Parents => False);
2747 if Present (Ritem) then
2749 -- Pragma case
2751 if Nkind (Ritem) = N_Pragma then
2752 Exp := First (Pragma_Argument_Associations (Ritem));
2754 if Nkind (Exp) = N_Pragma_Argument_Association then
2755 Exp := Expression (Exp);
2756 end if;
2758 -- Conversion for Priority expression
2760 if Nam = Name_Priority then
2761 if Pragma_Name (Ritem) = Name_Priority
2762 and then not GNAT_Mode
2763 then
2764 Exp := Convert_To (RTE (RE_Priority), Exp);
2765 else
2766 Exp :=
2767 Convert_To (RTE (RE_Any_Priority), Exp);
2768 end if;
2769 end if;
2771 -- Aspect/Attribute definition clause case
2773 else
2774 Exp := Expression (Ritem);
2776 -- Conversion for Priority expression
2778 if Nam = Name_Priority then
2779 if Chars (Ritem) = Name_Priority
2780 and then not GNAT_Mode
2781 then
2782 Exp := Convert_To (RTE (RE_Priority), Exp);
2783 else
2784 Exp :=
2785 Convert_To (RTE (RE_Any_Priority), Exp);
2786 end if;
2787 end if;
2788 end if;
2790 -- Conversion for Dispatching_Domain value
2792 if Nam = Name_Dispatching_Domain then
2793 Exp :=
2794 Unchecked_Convert_To
2795 (RTE (RE_Dispatching_Domain_Access), Exp);
2796 end if;
2798 Actions := Build_Assignment (Id, Exp);
2800 -- Nothing needed if no Rep Item
2802 else
2803 Actions := No_List;
2804 end if;
2805 end;
2807 -- Composite component with its own Init_Proc
2809 elsif not Is_Interface (Typ)
2810 and then Has_Non_Null_Base_Init_Proc (Typ)
2811 then
2812 Actions :=
2813 Build_Initialization_Call
2814 (Comp_Loc,
2815 Make_Selected_Component (Comp_Loc,
2816 Prefix =>
2817 Make_Identifier (Comp_Loc, Name_uInit),
2818 Selector_Name => New_Occurrence_Of (Id, Comp_Loc)),
2819 Typ,
2820 In_Init_Proc => True,
2821 Enclos_Type => Rec_Type,
2822 Discr_Map => Discr_Map);
2824 Clean_Task_Names (Typ, Proc_Id);
2826 -- Simple initialization
2828 elsif Component_Needs_Simple_Initialization (Typ) then
2829 Actions :=
2830 Build_Assignment
2831 (Id, Get_Simple_Init_Val (Typ, N, Esize (Id)));
2833 -- Nothing needed for this case
2835 else
2836 Actions := No_List;
2837 end if;
2839 if Present (Checks) then
2840 if Chars (Id) = Name_uParent then
2841 Append_List_To (Parent_Stmts, Checks);
2842 else
2843 Append_List_To (Stmts, Checks);
2844 end if;
2845 end if;
2847 if Present (Actions) then
2848 if Chars (Id) = Name_uParent then
2849 Append_List_To (Parent_Stmts, Actions);
2851 else
2852 Append_List_To (Stmts, Actions);
2854 -- Preserve initialization state in the current counter
2856 if Needs_Finalization (Typ) then
2857 if No (Counter_Id) then
2858 Make_Counter (Comp_Loc);
2859 end if;
2861 Increment_Counter (Comp_Loc);
2862 end if;
2863 end if;
2864 end if;
2865 end if;
2867 Next_Non_Pragma (Decl);
2868 end loop;
2870 -- The parent field must be initialized first because variable
2871 -- size components of the parent affect the location of all the
2872 -- new components.
2874 Prepend_List_To (Stmts, Parent_Stmts);
2876 -- Set up tasks and protected object support. This needs to be done
2877 -- before any component with a per-object access discriminant
2878 -- constraint, or any variant part (which may contain such
2879 -- components) is initialized, because the initialization of these
2880 -- components may reference the enclosing concurrent object.
2882 -- For a task record type, add the task create call and calls to bind
2883 -- any interrupt (signal) entries.
2885 if Is_Task_Record_Type (Rec_Type) then
2887 -- In the case of the restricted run time the ATCB has already
2888 -- been preallocated.
2890 if Restricted_Profile then
2891 Append_To (Stmts,
2892 Make_Assignment_Statement (Loc,
2893 Name =>
2894 Make_Selected_Component (Loc,
2895 Prefix => Make_Identifier (Loc, Name_uInit),
2896 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)),
2897 Expression =>
2898 Make_Attribute_Reference (Loc,
2899 Prefix =>
2900 Make_Selected_Component (Loc,
2901 Prefix => Make_Identifier (Loc, Name_uInit),
2902 Selector_Name => Make_Identifier (Loc, Name_uATCB)),
2903 Attribute_Name => Name_Unchecked_Access)));
2904 end if;
2906 Append_To (Stmts, Make_Task_Create_Call (Rec_Type));
2908 declare
2909 Task_Type : constant Entity_Id :=
2910 Corresponding_Concurrent_Type (Rec_Type);
2911 Task_Decl : constant Node_Id := Parent (Task_Type);
2912 Task_Def : constant Node_Id := Task_Definition (Task_Decl);
2913 Decl_Loc : Source_Ptr;
2914 Ent : Entity_Id;
2915 Vis_Decl : Node_Id;
2917 begin
2918 if Present (Task_Def) then
2919 Vis_Decl := First (Visible_Declarations (Task_Def));
2920 while Present (Vis_Decl) loop
2921 Decl_Loc := Sloc (Vis_Decl);
2923 if Nkind (Vis_Decl) = N_Attribute_Definition_Clause then
2924 if Get_Attribute_Id (Chars (Vis_Decl)) =
2925 Attribute_Address
2926 then
2927 Ent := Entity (Name (Vis_Decl));
2929 if Ekind (Ent) = E_Entry then
2930 Append_To (Stmts,
2931 Make_Procedure_Call_Statement (Decl_Loc,
2932 Name =>
2933 New_Occurrence_Of (RTE (
2934 RE_Bind_Interrupt_To_Entry), Decl_Loc),
2935 Parameter_Associations => New_List (
2936 Make_Selected_Component (Decl_Loc,
2937 Prefix =>
2938 Make_Identifier (Decl_Loc, Name_uInit),
2939 Selector_Name =>
2940 Make_Identifier
2941 (Decl_Loc, Name_uTask_Id)),
2942 Entry_Index_Expression
2943 (Decl_Loc, Ent, Empty, Task_Type),
2944 Expression (Vis_Decl))));
2945 end if;
2946 end if;
2947 end if;
2949 Next (Vis_Decl);
2950 end loop;
2951 end if;
2952 end;
2953 end if;
2955 -- For a protected type, add statements generated by
2956 -- Make_Initialize_Protection.
2958 if Is_Protected_Record_Type (Rec_Type) then
2959 Append_List_To (Stmts,
2960 Make_Initialize_Protection (Rec_Type));
2961 end if;
2963 -- Second pass: components with per-object constraints
2965 if Has_POC then
2966 Decl := First_Non_Pragma (Component_Items (Comp_List));
2967 while Present (Decl) loop
2968 Comp_Loc := Sloc (Decl);
2969 Id := Defining_Identifier (Decl);
2970 Typ := Etype (Id);
2972 if Has_Access_Constraint (Id)
2973 and then No (Expression (Decl))
2974 then
2975 if Has_Non_Null_Base_Init_Proc (Typ) then
2976 Append_List_To (Stmts,
2977 Build_Initialization_Call (Comp_Loc,
2978 Make_Selected_Component (Comp_Loc,
2979 Prefix =>
2980 Make_Identifier (Comp_Loc, Name_uInit),
2981 Selector_Name => New_Occurrence_Of (Id, Comp_Loc)),
2982 Typ,
2983 In_Init_Proc => True,
2984 Enclos_Type => Rec_Type,
2985 Discr_Map => Discr_Map));
2987 Clean_Task_Names (Typ, Proc_Id);
2989 -- Preserve initialization state in the current counter
2991 if Needs_Finalization (Typ) then
2992 if No (Counter_Id) then
2993 Make_Counter (Comp_Loc);
2994 end if;
2996 Increment_Counter (Comp_Loc);
2997 end if;
2999 elsif Component_Needs_Simple_Initialization (Typ) then
3000 Append_List_To (Stmts,
3001 Build_Assignment
3002 (Id, Get_Simple_Init_Val (Typ, N, Esize (Id))));
3003 end if;
3004 end if;
3006 Next_Non_Pragma (Decl);
3007 end loop;
3008 end if;
3010 -- Process the variant part
3012 if Present (Variant_Part (Comp_List)) then
3013 declare
3014 Variant_Alts : constant List_Id := New_List;
3015 Var_Loc : Source_Ptr;
3016 Variant : Node_Id;
3018 begin
3019 Variant :=
3020 First_Non_Pragma (Variants (Variant_Part (Comp_List)));
3021 while Present (Variant) loop
3022 Var_Loc := Sloc (Variant);
3023 Append_To (Variant_Alts,
3024 Make_Case_Statement_Alternative (Var_Loc,
3025 Discrete_Choices =>
3026 New_Copy_List (Discrete_Choices (Variant)),
3027 Statements =>
3028 Build_Init_Statements (Component_List (Variant))));
3029 Next_Non_Pragma (Variant);
3030 end loop;
3032 -- The expression of the case statement which is a reference
3033 -- to one of the discriminants is replaced by the appropriate
3034 -- formal parameter of the initialization procedure.
3036 Append_To (Stmts,
3037 Make_Case_Statement (Var_Loc,
3038 Expression =>
3039 New_Occurrence_Of (Discriminal (
3040 Entity (Name (Variant_Part (Comp_List)))), Var_Loc),
3041 Alternatives => Variant_Alts));
3042 end;
3043 end if;
3045 -- If no initializations when generated for component declarations
3046 -- corresponding to this Stmts, append a null statement to Stmts to
3047 -- to make it a valid Ada tree.
3049 if Is_Empty_List (Stmts) then
3050 Append (Make_Null_Statement (Loc), Stmts);
3051 end if;
3053 return Stmts;
3055 exception
3056 when RE_Not_Available =>
3057 return Empty_List;
3058 end Build_Init_Statements;
3060 -------------------------
3061 -- Build_Record_Checks --
3062 -------------------------
3064 procedure Build_Record_Checks (S : Node_Id; Check_List : List_Id) is
3065 Subtype_Mark_Id : Entity_Id;
3067 procedure Constrain_Array
3068 (SI : Node_Id;
3069 Check_List : List_Id);
3070 -- Apply a list of index constraints to an unconstrained array type.
3071 -- The first parameter is the entity for the resulting subtype.
3072 -- Check_List is a list to which the check actions are appended.
3074 ---------------------
3075 -- Constrain_Array --
3076 ---------------------
3078 procedure Constrain_Array
3079 (SI : Node_Id;
3080 Check_List : List_Id)
3082 C : constant Node_Id := Constraint (SI);
3083 Number_Of_Constraints : Nat := 0;
3084 Index : Node_Id;
3085 S, T : Entity_Id;
3087 procedure Constrain_Index
3088 (Index : Node_Id;
3089 S : Node_Id;
3090 Check_List : List_Id);
3091 -- Process an index constraint in a constrained array declaration.
3092 -- The constraint can be either a subtype name or a range with or
3093 -- without an explicit subtype mark. Index is the corresponding
3094 -- index of the unconstrained array. S is the range expression.
3095 -- Check_List is a list to which the check actions are appended.
3097 ---------------------
3098 -- Constrain_Index --
3099 ---------------------
3101 procedure Constrain_Index
3102 (Index : Node_Id;
3103 S : Node_Id;
3104 Check_List : List_Id)
3106 T : constant Entity_Id := Etype (Index);
3108 begin
3109 if Nkind (S) = N_Range then
3110 Process_Range_Expr_In_Decl (S, T, Check_List => Check_List);
3111 end if;
3112 end Constrain_Index;
3114 -- Start of processing for Constrain_Array
3116 begin
3117 T := Entity (Subtype_Mark (SI));
3119 if Is_Access_Type (T) then
3120 T := Designated_Type (T);
3121 end if;
3123 S := First (Constraints (C));
3124 while Present (S) loop
3125 Number_Of_Constraints := Number_Of_Constraints + 1;
3126 Next (S);
3127 end loop;
3129 -- In either case, the index constraint must provide a discrete
3130 -- range for each index of the array type and the type of each
3131 -- discrete range must be the same as that of the corresponding
3132 -- index. (RM 3.6.1)
3134 S := First (Constraints (C));
3135 Index := First_Index (T);
3136 Analyze (Index);
3138 -- Apply constraints to each index type
3140 for J in 1 .. Number_Of_Constraints loop
3141 Constrain_Index (Index, S, Check_List);
3142 Next (Index);
3143 Next (S);
3144 end loop;
3145 end Constrain_Array;
3147 -- Start of processing for Build_Record_Checks
3149 begin
3150 if Nkind (S) = N_Subtype_Indication then
3151 Find_Type (Subtype_Mark (S));
3152 Subtype_Mark_Id := Entity (Subtype_Mark (S));
3154 -- Remaining processing depends on type
3156 case Ekind (Subtype_Mark_Id) is
3158 when Array_Kind =>
3159 Constrain_Array (S, Check_List);
3161 when others =>
3162 null;
3163 end case;
3164 end if;
3165 end Build_Record_Checks;
3167 -------------------------------------------
3168 -- Component_Needs_Simple_Initialization --
3169 -------------------------------------------
3171 function Component_Needs_Simple_Initialization
3172 (T : Entity_Id) return Boolean
3174 begin
3175 return
3176 Needs_Simple_Initialization (T)
3177 and then not Is_RTE (T, RE_Tag)
3179 -- Ada 2005 (AI-251): Check also the tag of abstract interfaces
3181 and then not Is_RTE (T, RE_Interface_Tag);
3182 end Component_Needs_Simple_Initialization;
3184 --------------------------------------
3185 -- Parent_Subtype_Renaming_Discrims --
3186 --------------------------------------
3188 function Parent_Subtype_Renaming_Discrims return Boolean is
3189 De : Entity_Id;
3190 Dp : Entity_Id;
3192 begin
3193 if Base_Type (Rec_Ent) /= Rec_Ent then
3194 return False;
3195 end if;
3197 if Etype (Rec_Ent) = Rec_Ent
3198 or else not Has_Discriminants (Rec_Ent)
3199 or else Is_Constrained (Rec_Ent)
3200 or else Is_Tagged_Type (Rec_Ent)
3201 then
3202 return False;
3203 end if;
3205 -- If there are no explicit stored discriminants we have inherited
3206 -- the root type discriminants so far, so no renamings occurred.
3208 if First_Discriminant (Rec_Ent) =
3209 First_Stored_Discriminant (Rec_Ent)
3210 then
3211 return False;
3212 end if;
3214 -- Check if we have done some trivial renaming of the parent
3215 -- discriminants, i.e. something like
3217 -- type DT (X1, X2: int) is new PT (X1, X2);
3219 De := First_Discriminant (Rec_Ent);
3220 Dp := First_Discriminant (Etype (Rec_Ent));
3221 while Present (De) loop
3222 pragma Assert (Present (Dp));
3224 if Corresponding_Discriminant (De) /= Dp then
3225 return True;
3226 end if;
3228 Next_Discriminant (De);
3229 Next_Discriminant (Dp);
3230 end loop;
3232 return Present (Dp);
3233 end Parent_Subtype_Renaming_Discrims;
3235 ------------------------
3236 -- Requires_Init_Proc --
3237 ------------------------
3239 function Requires_Init_Proc (Rec_Id : Entity_Id) return Boolean is
3240 Comp_Decl : Node_Id;
3241 Id : Entity_Id;
3242 Typ : Entity_Id;
3244 begin
3245 -- Definitely do not need one if specifically suppressed
3247 if Initialization_Suppressed (Rec_Id) then
3248 return False;
3249 end if;
3251 -- If it is a type derived from a type with unknown discriminants,
3252 -- we cannot build an initialization procedure for it.
3254 if Has_Unknown_Discriminants (Rec_Id)
3255 or else Has_Unknown_Discriminants (Etype (Rec_Id))
3256 then
3257 return False;
3258 end if;
3260 -- Otherwise we need to generate an initialization procedure if
3261 -- Is_CPP_Class is False and at least one of the following applies:
3263 -- 1. Discriminants are present, since they need to be initialized
3264 -- with the appropriate discriminant constraint expressions.
3265 -- However, the discriminant of an unchecked union does not
3266 -- count, since the discriminant is not present.
3268 -- 2. The type is a tagged type, since the implicit Tag component
3269 -- needs to be initialized with a pointer to the dispatch table.
3271 -- 3. The type contains tasks
3273 -- 4. One or more components has an initial value
3275 -- 5. One or more components is for a type which itself requires
3276 -- an initialization procedure.
3278 -- 6. One or more components is a type that requires simple
3279 -- initialization (see Needs_Simple_Initialization), except
3280 -- that types Tag and Interface_Tag are excluded, since fields
3281 -- of these types are initialized by other means.
3283 -- 7. The type is the record type built for a task type (since at
3284 -- the very least, Create_Task must be called)
3286 -- 8. The type is the record type built for a protected type (since
3287 -- at least Initialize_Protection must be called)
3289 -- 9. The type is marked as a public entity. The reason we add this
3290 -- case (even if none of the above apply) is to properly handle
3291 -- Initialize_Scalars. If a package is compiled without an IS
3292 -- pragma, and the client is compiled with an IS pragma, then
3293 -- the client will think an initialization procedure is present
3294 -- and call it, when in fact no such procedure is required, but
3295 -- since the call is generated, there had better be a routine
3296 -- at the other end of the call, even if it does nothing).
3298 -- Note: the reason we exclude the CPP_Class case is because in this
3299 -- case the initialization is performed by the C++ constructors, and
3300 -- the IP is built by Set_CPP_Constructors.
3302 if Is_CPP_Class (Rec_Id) then
3303 return False;
3305 elsif Is_Interface (Rec_Id) then
3306 return False;
3308 elsif (Has_Discriminants (Rec_Id)
3309 and then not Is_Unchecked_Union (Rec_Id))
3310 or else Is_Tagged_Type (Rec_Id)
3311 or else Is_Concurrent_Record_Type (Rec_Id)
3312 or else Has_Task (Rec_Id)
3313 then
3314 return True;
3315 end if;
3317 Id := First_Component (Rec_Id);
3318 while Present (Id) loop
3319 Comp_Decl := Parent (Id);
3320 Typ := Etype (Id);
3322 if Present (Expression (Comp_Decl))
3323 or else Has_Non_Null_Base_Init_Proc (Typ)
3324 or else Component_Needs_Simple_Initialization (Typ)
3325 then
3326 return True;
3327 end if;
3329 Next_Component (Id);
3330 end loop;
3332 -- As explained above, a record initialization procedure is needed
3333 -- for public types in case Initialize_Scalars applies to a client.
3334 -- However, such a procedure is not needed in the case where either
3335 -- of restrictions No_Initialize_Scalars or No_Default_Initialization
3336 -- applies. No_Initialize_Scalars excludes the possibility of using
3337 -- Initialize_Scalars in any partition, and No_Default_Initialization
3338 -- implies that no initialization should ever be done for objects of
3339 -- the type, so is incompatible with Initialize_Scalars.
3341 if not Restriction_Active (No_Initialize_Scalars)
3342 and then not Restriction_Active (No_Default_Initialization)
3343 and then Is_Public (Rec_Id)
3344 then
3345 return True;
3346 end if;
3348 return False;
3349 end Requires_Init_Proc;
3351 -- Start of processing for Build_Record_Init_Proc
3353 begin
3354 Rec_Type := Defining_Identifier (N);
3356 -- This may be full declaration of a private type, in which case
3357 -- the visible entity is a record, and the private entity has been
3358 -- exchanged with it in the private part of the current package.
3359 -- The initialization procedure is built for the record type, which
3360 -- is retrievable from the private entity.
3362 if Is_Incomplete_Or_Private_Type (Rec_Type) then
3363 Rec_Type := Underlying_Type (Rec_Type);
3364 end if;
3366 -- If we have a variant record with restriction No_Implicit_Conditionals
3367 -- in effect, then we skip building the procedure. This is safe because
3368 -- if we can see the restriction, so can any caller, calls to initialize
3369 -- such records are not allowed for variant records if this restriction
3370 -- is active.
3372 if Has_Variant_Part (Rec_Type)
3373 and then Restriction_Active (No_Implicit_Conditionals)
3374 then
3375 return;
3376 end if;
3378 -- If there are discriminants, build the discriminant map to replace
3379 -- discriminants by their discriminals in complex bound expressions.
3380 -- These only arise for the corresponding records of synchronized types.
3382 if Is_Concurrent_Record_Type (Rec_Type)
3383 and then Has_Discriminants (Rec_Type)
3384 then
3385 declare
3386 Disc : Entity_Id;
3387 begin
3388 Disc := First_Discriminant (Rec_Type);
3389 while Present (Disc) loop
3390 Append_Elmt (Disc, Discr_Map);
3391 Append_Elmt (Discriminal (Disc), Discr_Map);
3392 Next_Discriminant (Disc);
3393 end loop;
3394 end;
3395 end if;
3397 -- Derived types that have no type extension can use the initialization
3398 -- procedure of their parent and do not need a procedure of their own.
3399 -- This is only correct if there are no representation clauses for the
3400 -- type or its parent, and if the parent has in fact been frozen so
3401 -- that its initialization procedure exists.
3403 if Is_Derived_Type (Rec_Type)
3404 and then not Is_Tagged_Type (Rec_Type)
3405 and then not Is_Unchecked_Union (Rec_Type)
3406 and then not Has_New_Non_Standard_Rep (Rec_Type)
3407 and then not Parent_Subtype_Renaming_Discrims
3408 and then Has_Non_Null_Base_Init_Proc (Etype (Rec_Type))
3409 then
3410 Copy_TSS (Base_Init_Proc (Etype (Rec_Type)), Rec_Type);
3412 -- Otherwise if we need an initialization procedure, then build one,
3413 -- mark it as public and inlinable and as having a completion.
3415 elsif Requires_Init_Proc (Rec_Type)
3416 or else Is_Unchecked_Union (Rec_Type)
3417 then
3418 Proc_Id :=
3419 Make_Defining_Identifier (Loc,
3420 Chars => Make_Init_Proc_Name (Rec_Type));
3422 -- If No_Default_Initialization restriction is active, then we don't
3423 -- want to build an init_proc, but we need to mark that an init_proc
3424 -- would be needed if this restriction was not active (so that we can
3425 -- detect attempts to call it), so set a dummy init_proc in place.
3427 if Restriction_Active (No_Default_Initialization) then
3428 Set_Init_Proc (Rec_Type, Proc_Id);
3429 return;
3430 end if;
3432 Build_Offset_To_Top_Functions;
3433 Build_CPP_Init_Procedure;
3434 Build_Init_Procedure;
3436 Set_Is_Public (Proc_Id, Is_Public (Rec_Ent));
3437 Set_Is_Internal (Proc_Id);
3438 Set_Has_Completion (Proc_Id);
3440 if not Debug_Generated_Code then
3441 Set_Debug_Info_Off (Proc_Id);
3442 end if;
3444 Set_Is_Inlined (Proc_Id, Inline_Init_Proc (Rec_Type));
3446 -- Do not build an aggregate if Modify_Tree_For_C, this isn't
3447 -- needed and may generate early references to non frozen types
3448 -- since we expand aggregate much more systematically.
3450 if Modify_Tree_For_C then
3451 return;
3452 end if;
3454 declare
3455 Agg : constant Node_Id :=
3456 Build_Equivalent_Record_Aggregate (Rec_Type);
3458 procedure Collect_Itypes (Comp : Node_Id);
3459 -- Generate references to itypes in the aggregate, because
3460 -- the first use of the aggregate may be in a nested scope.
3462 --------------------
3463 -- Collect_Itypes --
3464 --------------------
3466 procedure Collect_Itypes (Comp : Node_Id) is
3467 Ref : Node_Id;
3468 Sub_Aggr : Node_Id;
3469 Typ : constant Entity_Id := Etype (Comp);
3471 begin
3472 if Is_Array_Type (Typ) and then Is_Itype (Typ) then
3473 Ref := Make_Itype_Reference (Loc);
3474 Set_Itype (Ref, Typ);
3475 Append_Freeze_Action (Rec_Type, Ref);
3477 Ref := Make_Itype_Reference (Loc);
3478 Set_Itype (Ref, Etype (First_Index (Typ)));
3479 Append_Freeze_Action (Rec_Type, Ref);
3481 -- Recurse on nested arrays
3483 Sub_Aggr := First (Expressions (Comp));
3484 while Present (Sub_Aggr) loop
3485 Collect_Itypes (Sub_Aggr);
3486 Next (Sub_Aggr);
3487 end loop;
3488 end if;
3489 end Collect_Itypes;
3491 begin
3492 -- If there is a static initialization aggregate for the type,
3493 -- generate itype references for the types of its (sub)components,
3494 -- to prevent out-of-scope errors in the resulting tree.
3495 -- The aggregate may have been rewritten as a Raise node, in which
3496 -- case there are no relevant itypes.
3498 if Present (Agg) and then Nkind (Agg) = N_Aggregate then
3499 Set_Static_Initialization (Proc_Id, Agg);
3501 declare
3502 Comp : Node_Id;
3503 begin
3504 Comp := First (Component_Associations (Agg));
3505 while Present (Comp) loop
3506 Collect_Itypes (Expression (Comp));
3507 Next (Comp);
3508 end loop;
3509 end;
3510 end if;
3511 end;
3512 end if;
3513 end Build_Record_Init_Proc;
3515 ----------------------------
3516 -- Build_Slice_Assignment --
3517 ----------------------------
3519 -- Generates the following subprogram:
3521 -- procedure Assign
3522 -- (Source, Target : Array_Type,
3523 -- Left_Lo, Left_Hi : Index;
3524 -- Right_Lo, Right_Hi : Index;
3525 -- Rev : Boolean)
3526 -- is
3527 -- Li1 : Index;
3528 -- Ri1 : Index;
3530 -- begin
3532 -- if Left_Hi < Left_Lo then
3533 -- return;
3534 -- end if;
3536 -- if Rev then
3537 -- Li1 := Left_Hi;
3538 -- Ri1 := Right_Hi;
3539 -- else
3540 -- Li1 := Left_Lo;
3541 -- Ri1 := Right_Lo;
3542 -- end if;
3544 -- loop
3545 -- Target (Li1) := Source (Ri1);
3547 -- if Rev then
3548 -- exit when Li1 = Left_Lo;
3549 -- Li1 := Index'pred (Li1);
3550 -- Ri1 := Index'pred (Ri1);
3551 -- else
3552 -- exit when Li1 = Left_Hi;
3553 -- Li1 := Index'succ (Li1);
3554 -- Ri1 := Index'succ (Ri1);
3555 -- end if;
3556 -- end loop;
3557 -- end Assign;
3559 procedure Build_Slice_Assignment (Typ : Entity_Id) is
3560 Loc : constant Source_Ptr := Sloc (Typ);
3561 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
3563 Larray : constant Entity_Id := Make_Temporary (Loc, 'A');
3564 Rarray : constant Entity_Id := Make_Temporary (Loc, 'R');
3565 Left_Lo : constant Entity_Id := Make_Temporary (Loc, 'L');
3566 Left_Hi : constant Entity_Id := Make_Temporary (Loc, 'L');
3567 Right_Lo : constant Entity_Id := Make_Temporary (Loc, 'R');
3568 Right_Hi : constant Entity_Id := Make_Temporary (Loc, 'R');
3569 Rev : constant Entity_Id := Make_Temporary (Loc, 'D');
3570 -- Formal parameters of procedure
3572 Proc_Name : constant Entity_Id :=
3573 Make_Defining_Identifier (Loc,
3574 Chars => Make_TSS_Name (Typ, TSS_Slice_Assign));
3576 Lnn : constant Entity_Id := Make_Temporary (Loc, 'L');
3577 Rnn : constant Entity_Id := Make_Temporary (Loc, 'R');
3578 -- Subscripts for left and right sides
3580 Decls : List_Id;
3581 Loops : Node_Id;
3582 Stats : List_Id;
3584 begin
3585 -- Build declarations for indexes
3587 Decls := New_List;
3589 Append_To (Decls,
3590 Make_Object_Declaration (Loc,
3591 Defining_Identifier => Lnn,
3592 Object_Definition =>
3593 New_Occurrence_Of (Index, Loc)));
3595 Append_To (Decls,
3596 Make_Object_Declaration (Loc,
3597 Defining_Identifier => Rnn,
3598 Object_Definition =>
3599 New_Occurrence_Of (Index, Loc)));
3601 Stats := New_List;
3603 -- Build test for empty slice case
3605 Append_To (Stats,
3606 Make_If_Statement (Loc,
3607 Condition =>
3608 Make_Op_Lt (Loc,
3609 Left_Opnd => New_Occurrence_Of (Left_Hi, Loc),
3610 Right_Opnd => New_Occurrence_Of (Left_Lo, Loc)),
3611 Then_Statements => New_List (Make_Simple_Return_Statement (Loc))));
3613 -- Build initializations for indexes
3615 declare
3616 F_Init : constant List_Id := New_List;
3617 B_Init : constant List_Id := New_List;
3619 begin
3620 Append_To (F_Init,
3621 Make_Assignment_Statement (Loc,
3622 Name => New_Occurrence_Of (Lnn, Loc),
3623 Expression => New_Occurrence_Of (Left_Lo, Loc)));
3625 Append_To (F_Init,
3626 Make_Assignment_Statement (Loc,
3627 Name => New_Occurrence_Of (Rnn, Loc),
3628 Expression => New_Occurrence_Of (Right_Lo, Loc)));
3630 Append_To (B_Init,
3631 Make_Assignment_Statement (Loc,
3632 Name => New_Occurrence_Of (Lnn, Loc),
3633 Expression => New_Occurrence_Of (Left_Hi, Loc)));
3635 Append_To (B_Init,
3636 Make_Assignment_Statement (Loc,
3637 Name => New_Occurrence_Of (Rnn, Loc),
3638 Expression => New_Occurrence_Of (Right_Hi, Loc)));
3640 Append_To (Stats,
3641 Make_If_Statement (Loc,
3642 Condition => New_Occurrence_Of (Rev, Loc),
3643 Then_Statements => B_Init,
3644 Else_Statements => F_Init));
3645 end;
3647 -- Now construct the assignment statement
3649 Loops :=
3650 Make_Loop_Statement (Loc,
3651 Statements => New_List (
3652 Make_Assignment_Statement (Loc,
3653 Name =>
3654 Make_Indexed_Component (Loc,
3655 Prefix => New_Occurrence_Of (Larray, Loc),
3656 Expressions => New_List (New_Occurrence_Of (Lnn, Loc))),
3657 Expression =>
3658 Make_Indexed_Component (Loc,
3659 Prefix => New_Occurrence_Of (Rarray, Loc),
3660 Expressions => New_List (New_Occurrence_Of (Rnn, Loc))))),
3661 End_Label => Empty);
3663 -- Build the exit condition and increment/decrement statements
3665 declare
3666 F_Ass : constant List_Id := New_List;
3667 B_Ass : constant List_Id := New_List;
3669 begin
3670 Append_To (F_Ass,
3671 Make_Exit_Statement (Loc,
3672 Condition =>
3673 Make_Op_Eq (Loc,
3674 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
3675 Right_Opnd => New_Occurrence_Of (Left_Hi, Loc))));
3677 Append_To (F_Ass,
3678 Make_Assignment_Statement (Loc,
3679 Name => New_Occurrence_Of (Lnn, Loc),
3680 Expression =>
3681 Make_Attribute_Reference (Loc,
3682 Prefix =>
3683 New_Occurrence_Of (Index, Loc),
3684 Attribute_Name => Name_Succ,
3685 Expressions => New_List (
3686 New_Occurrence_Of (Lnn, Loc)))));
3688 Append_To (F_Ass,
3689 Make_Assignment_Statement (Loc,
3690 Name => New_Occurrence_Of (Rnn, Loc),
3691 Expression =>
3692 Make_Attribute_Reference (Loc,
3693 Prefix =>
3694 New_Occurrence_Of (Index, Loc),
3695 Attribute_Name => Name_Succ,
3696 Expressions => New_List (
3697 New_Occurrence_Of (Rnn, Loc)))));
3699 Append_To (B_Ass,
3700 Make_Exit_Statement (Loc,
3701 Condition =>
3702 Make_Op_Eq (Loc,
3703 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
3704 Right_Opnd => New_Occurrence_Of (Left_Lo, Loc))));
3706 Append_To (B_Ass,
3707 Make_Assignment_Statement (Loc,
3708 Name => New_Occurrence_Of (Lnn, Loc),
3709 Expression =>
3710 Make_Attribute_Reference (Loc,
3711 Prefix =>
3712 New_Occurrence_Of (Index, Loc),
3713 Attribute_Name => Name_Pred,
3714 Expressions => New_List (
3715 New_Occurrence_Of (Lnn, Loc)))));
3717 Append_To (B_Ass,
3718 Make_Assignment_Statement (Loc,
3719 Name => New_Occurrence_Of (Rnn, Loc),
3720 Expression =>
3721 Make_Attribute_Reference (Loc,
3722 Prefix =>
3723 New_Occurrence_Of (Index, Loc),
3724 Attribute_Name => Name_Pred,
3725 Expressions => New_List (
3726 New_Occurrence_Of (Rnn, Loc)))));
3728 Append_To (Statements (Loops),
3729 Make_If_Statement (Loc,
3730 Condition => New_Occurrence_Of (Rev, Loc),
3731 Then_Statements => B_Ass,
3732 Else_Statements => F_Ass));
3733 end;
3735 Append_To (Stats, Loops);
3737 declare
3738 Spec : Node_Id;
3739 Formals : List_Id := New_List;
3741 begin
3742 Formals := New_List (
3743 Make_Parameter_Specification (Loc,
3744 Defining_Identifier => Larray,
3745 Out_Present => True,
3746 Parameter_Type =>
3747 New_Occurrence_Of (Base_Type (Typ), Loc)),
3749 Make_Parameter_Specification (Loc,
3750 Defining_Identifier => Rarray,
3751 Parameter_Type =>
3752 New_Occurrence_Of (Base_Type (Typ), Loc)),
3754 Make_Parameter_Specification (Loc,
3755 Defining_Identifier => Left_Lo,
3756 Parameter_Type =>
3757 New_Occurrence_Of (Index, Loc)),
3759 Make_Parameter_Specification (Loc,
3760 Defining_Identifier => Left_Hi,
3761 Parameter_Type =>
3762 New_Occurrence_Of (Index, Loc)),
3764 Make_Parameter_Specification (Loc,
3765 Defining_Identifier => Right_Lo,
3766 Parameter_Type =>
3767 New_Occurrence_Of (Index, Loc)),
3769 Make_Parameter_Specification (Loc,
3770 Defining_Identifier => Right_Hi,
3771 Parameter_Type =>
3772 New_Occurrence_Of (Index, Loc)));
3774 Append_To (Formals,
3775 Make_Parameter_Specification (Loc,
3776 Defining_Identifier => Rev,
3777 Parameter_Type =>
3778 New_Occurrence_Of (Standard_Boolean, Loc)));
3780 Spec :=
3781 Make_Procedure_Specification (Loc,
3782 Defining_Unit_Name => Proc_Name,
3783 Parameter_Specifications => Formals);
3785 Discard_Node (
3786 Make_Subprogram_Body (Loc,
3787 Specification => Spec,
3788 Declarations => Decls,
3789 Handled_Statement_Sequence =>
3790 Make_Handled_Sequence_Of_Statements (Loc,
3791 Statements => Stats)));
3792 end;
3794 Set_TSS (Typ, Proc_Name);
3795 Set_Is_Pure (Proc_Name);
3796 end Build_Slice_Assignment;
3798 -----------------------------
3799 -- Build_Untagged_Equality --
3800 -----------------------------
3802 procedure Build_Untagged_Equality (Typ : Entity_Id) is
3803 Build_Eq : Boolean;
3804 Comp : Entity_Id;
3805 Decl : Node_Id;
3806 Op : Entity_Id;
3807 Prim : Elmt_Id;
3808 Eq_Op : Entity_Id;
3810 function User_Defined_Eq (T : Entity_Id) return Entity_Id;
3811 -- Check whether the type T has a user-defined primitive equality. If so
3812 -- return it, else return Empty. If true for a component of Typ, we have
3813 -- to build the primitive equality for it.
3815 ---------------------
3816 -- User_Defined_Eq --
3817 ---------------------
3819 function User_Defined_Eq (T : Entity_Id) return Entity_Id is
3820 Prim : Elmt_Id;
3821 Op : Entity_Id;
3823 begin
3824 Op := TSS (T, TSS_Composite_Equality);
3826 if Present (Op) then
3827 return Op;
3828 end if;
3830 Prim := First_Elmt (Collect_Primitive_Operations (T));
3831 while Present (Prim) loop
3832 Op := Node (Prim);
3834 if Chars (Op) = Name_Op_Eq
3835 and then Etype (Op) = Standard_Boolean
3836 and then Etype (First_Formal (Op)) = T
3837 and then Etype (Next_Formal (First_Formal (Op))) = T
3838 then
3839 return Op;
3840 end if;
3842 Next_Elmt (Prim);
3843 end loop;
3845 return Empty;
3846 end User_Defined_Eq;
3848 -- Start of processing for Build_Untagged_Equality
3850 begin
3851 -- If a record component has a primitive equality operation, we must
3852 -- build the corresponding one for the current type.
3854 Build_Eq := False;
3855 Comp := First_Component (Typ);
3856 while Present (Comp) loop
3857 if Is_Record_Type (Etype (Comp))
3858 and then Present (User_Defined_Eq (Etype (Comp)))
3859 then
3860 Build_Eq := True;
3861 end if;
3863 Next_Component (Comp);
3864 end loop;
3866 -- If there is a user-defined equality for the type, we do not create
3867 -- the implicit one.
3869 Prim := First_Elmt (Collect_Primitive_Operations (Typ));
3870 Eq_Op := Empty;
3871 while Present (Prim) loop
3872 if Chars (Node (Prim)) = Name_Op_Eq
3873 and then Comes_From_Source (Node (Prim))
3875 -- Don't we also need to check formal types and return type as in
3876 -- User_Defined_Eq above???
3878 then
3879 Eq_Op := Node (Prim);
3880 Build_Eq := False;
3881 exit;
3882 end if;
3884 Next_Elmt (Prim);
3885 end loop;
3887 -- If the type is derived, inherit the operation, if present, from the
3888 -- parent type. It may have been declared after the type derivation. If
3889 -- the parent type itself is derived, it may have inherited an operation
3890 -- that has itself been overridden, so update its alias and related
3891 -- flags. Ditto for inequality.
3893 if No (Eq_Op) and then Is_Derived_Type (Typ) then
3894 Prim := First_Elmt (Collect_Primitive_Operations (Etype (Typ)));
3895 while Present (Prim) loop
3896 if Chars (Node (Prim)) = Name_Op_Eq then
3897 Copy_TSS (Node (Prim), Typ);
3898 Build_Eq := False;
3900 declare
3901 Op : constant Entity_Id := User_Defined_Eq (Typ);
3902 Eq_Op : constant Entity_Id := Node (Prim);
3903 NE_Op : constant Entity_Id := Next_Entity (Eq_Op);
3905 begin
3906 if Present (Op) then
3907 Set_Alias (Op, Eq_Op);
3908 Set_Is_Abstract_Subprogram
3909 (Op, Is_Abstract_Subprogram (Eq_Op));
3911 if Chars (Next_Entity (Op)) = Name_Op_Ne then
3912 Set_Is_Abstract_Subprogram
3913 (Next_Entity (Op), Is_Abstract_Subprogram (NE_Op));
3914 end if;
3915 end if;
3916 end;
3918 exit;
3919 end if;
3921 Next_Elmt (Prim);
3922 end loop;
3923 end if;
3925 -- If not inherited and not user-defined, build body as for a type with
3926 -- tagged components.
3928 if Build_Eq then
3929 Decl :=
3930 Make_Eq_Body (Typ, Make_TSS_Name (Typ, TSS_Composite_Equality));
3931 Op := Defining_Entity (Decl);
3932 Set_TSS (Typ, Op);
3933 Set_Is_Pure (Op);
3935 if Is_Library_Level_Entity (Typ) then
3936 Set_Is_Public (Op);
3937 end if;
3938 end if;
3939 end Build_Untagged_Equality;
3941 -----------------------------------
3942 -- Build_Variant_Record_Equality --
3943 -----------------------------------
3945 -- Generates:
3947 -- function _Equality (X, Y : T) return Boolean is
3948 -- begin
3949 -- -- Compare discriminants
3951 -- if X.D1 /= Y.D1 or else X.D2 /= Y.D2 or else ... then
3952 -- return False;
3953 -- end if;
3955 -- -- Compare components
3957 -- if X.C1 /= Y.C1 or else X.C2 /= Y.C2 or else ... then
3958 -- return False;
3959 -- end if;
3961 -- -- Compare variant part
3963 -- case X.D1 is
3964 -- when V1 =>
3965 -- if X.C2 /= Y.C2 or else X.C3 /= Y.C3 or else ... then
3966 -- return False;
3967 -- end if;
3968 -- ...
3969 -- when Vn =>
3970 -- if X.Cn /= Y.Cn or else ... then
3971 -- return False;
3972 -- end if;
3973 -- end case;
3975 -- return True;
3976 -- end _Equality;
3978 procedure Build_Variant_Record_Equality (Typ : Entity_Id) is
3979 Loc : constant Source_Ptr := Sloc (Typ);
3981 F : constant Entity_Id :=
3982 Make_Defining_Identifier (Loc,
3983 Chars => Make_TSS_Name (Typ, TSS_Composite_Equality));
3985 X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_X);
3986 Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_Y);
3988 Def : constant Node_Id := Parent (Typ);
3989 Comps : constant Node_Id := Component_List (Type_Definition (Def));
3990 Stmts : constant List_Id := New_List;
3991 Pspecs : constant List_Id := New_List;
3993 begin
3994 -- If we have a variant record with restriction No_Implicit_Conditionals
3995 -- in effect, then we skip building the procedure. This is safe because
3996 -- if we can see the restriction, so can any caller, calls to equality
3997 -- test routines are not allowed for variant records if this restriction
3998 -- is active.
4000 if Restriction_Active (No_Implicit_Conditionals) then
4001 return;
4002 end if;
4004 -- Derived Unchecked_Union types no longer inherit the equality function
4005 -- of their parent.
4007 if Is_Derived_Type (Typ)
4008 and then not Is_Unchecked_Union (Typ)
4009 and then not Has_New_Non_Standard_Rep (Typ)
4010 then
4011 declare
4012 Parent_Eq : constant Entity_Id :=
4013 TSS (Root_Type (Typ), TSS_Composite_Equality);
4014 begin
4015 if Present (Parent_Eq) then
4016 Copy_TSS (Parent_Eq, Typ);
4017 return;
4018 end if;
4019 end;
4020 end if;
4022 Discard_Node (
4023 Make_Subprogram_Body (Loc,
4024 Specification =>
4025 Make_Function_Specification (Loc,
4026 Defining_Unit_Name => F,
4027 Parameter_Specifications => Pspecs,
4028 Result_Definition => New_Occurrence_Of (Standard_Boolean, Loc)),
4029 Declarations => New_List,
4030 Handled_Statement_Sequence =>
4031 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts)));
4033 Append_To (Pspecs,
4034 Make_Parameter_Specification (Loc,
4035 Defining_Identifier => X,
4036 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
4038 Append_To (Pspecs,
4039 Make_Parameter_Specification (Loc,
4040 Defining_Identifier => Y,
4041 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
4043 -- Unchecked_Unions require additional machinery to support equality.
4044 -- Two extra parameters (A and B) are added to the equality function
4045 -- parameter list for each discriminant of the type, in order to
4046 -- capture the inferred values of the discriminants in equality calls.
4047 -- The names of the parameters match the names of the corresponding
4048 -- discriminant, with an added suffix.
4050 if Is_Unchecked_Union (Typ) then
4051 declare
4052 Discr : Entity_Id;
4053 Discr_Type : Entity_Id;
4054 A, B : Entity_Id;
4055 New_Discrs : Elist_Id;
4057 begin
4058 New_Discrs := New_Elmt_List;
4060 Discr := First_Discriminant (Typ);
4061 while Present (Discr) loop
4062 Discr_Type := Etype (Discr);
4063 A := Make_Defining_Identifier (Loc,
4064 Chars => New_External_Name (Chars (Discr), 'A'));
4066 B := Make_Defining_Identifier (Loc,
4067 Chars => New_External_Name (Chars (Discr), 'B'));
4069 -- Add new parameters to the parameter list
4071 Append_To (Pspecs,
4072 Make_Parameter_Specification (Loc,
4073 Defining_Identifier => A,
4074 Parameter_Type =>
4075 New_Occurrence_Of (Discr_Type, Loc)));
4077 Append_To (Pspecs,
4078 Make_Parameter_Specification (Loc,
4079 Defining_Identifier => B,
4080 Parameter_Type =>
4081 New_Occurrence_Of (Discr_Type, Loc)));
4083 Append_Elmt (A, New_Discrs);
4085 -- Generate the following code to compare each of the inferred
4086 -- discriminants:
4088 -- if a /= b then
4089 -- return False;
4090 -- end if;
4092 Append_To (Stmts,
4093 Make_If_Statement (Loc,
4094 Condition =>
4095 Make_Op_Ne (Loc,
4096 Left_Opnd => New_Occurrence_Of (A, Loc),
4097 Right_Opnd => New_Occurrence_Of (B, Loc)),
4098 Then_Statements => New_List (
4099 Make_Simple_Return_Statement (Loc,
4100 Expression =>
4101 New_Occurrence_Of (Standard_False, Loc)))));
4102 Next_Discriminant (Discr);
4103 end loop;
4105 -- Generate component-by-component comparison. Note that we must
4106 -- propagate the inferred discriminants formals to act as
4107 -- the case statement switch. Their value is added when an
4108 -- equality call on unchecked unions is expanded.
4110 Append_List_To (Stmts, Make_Eq_Case (Typ, Comps, New_Discrs));
4111 end;
4113 -- Normal case (not unchecked union)
4115 else
4116 Append_To (Stmts,
4117 Make_Eq_If (Typ, Discriminant_Specifications (Def)));
4118 Append_List_To (Stmts, Make_Eq_Case (Typ, Comps));
4119 end if;
4121 Append_To (Stmts,
4122 Make_Simple_Return_Statement (Loc,
4123 Expression => New_Occurrence_Of (Standard_True, Loc)));
4125 Set_TSS (Typ, F);
4126 Set_Is_Pure (F);
4128 if not Debug_Generated_Code then
4129 Set_Debug_Info_Off (F);
4130 end if;
4131 end Build_Variant_Record_Equality;
4133 -----------------------------
4134 -- Check_Stream_Attributes --
4135 -----------------------------
4137 procedure Check_Stream_Attributes (Typ : Entity_Id) is
4138 Comp : Entity_Id;
4139 Par_Read : constant Boolean :=
4140 Stream_Attribute_Available (Typ, TSS_Stream_Read)
4141 and then not Has_Specified_Stream_Read (Typ);
4142 Par_Write : constant Boolean :=
4143 Stream_Attribute_Available (Typ, TSS_Stream_Write)
4144 and then not Has_Specified_Stream_Write (Typ);
4146 procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type);
4147 -- Check that Comp has a user-specified Nam stream attribute
4149 ----------------
4150 -- Check_Attr --
4151 ----------------
4153 procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type) is
4154 begin
4155 if not Stream_Attribute_Available (Etype (Comp), TSS_Nam) then
4156 Error_Msg_Name_1 := Nam;
4157 Error_Msg_N
4158 ("|component& in limited extension must have% attribute", Comp);
4159 end if;
4160 end Check_Attr;
4162 -- Start of processing for Check_Stream_Attributes
4164 begin
4165 if Par_Read or else Par_Write then
4166 Comp := First_Component (Typ);
4167 while Present (Comp) loop
4168 if Comes_From_Source (Comp)
4169 and then Original_Record_Component (Comp) = Comp
4170 and then Is_Limited_Type (Etype (Comp))
4171 then
4172 if Par_Read then
4173 Check_Attr (Name_Read, TSS_Stream_Read);
4174 end if;
4176 if Par_Write then
4177 Check_Attr (Name_Write, TSS_Stream_Write);
4178 end if;
4179 end if;
4181 Next_Component (Comp);
4182 end loop;
4183 end if;
4184 end Check_Stream_Attributes;
4186 ----------------------
4187 -- Clean_Task_Names --
4188 ----------------------
4190 procedure Clean_Task_Names
4191 (Typ : Entity_Id;
4192 Proc_Id : Entity_Id)
4194 begin
4195 if Has_Task (Typ)
4196 and then not Restriction_Active (No_Implicit_Heap_Allocations)
4197 and then not Global_Discard_Names
4198 and then Tagged_Type_Expansion
4199 then
4200 Set_Uses_Sec_Stack (Proc_Id);
4201 end if;
4202 end Clean_Task_Names;
4204 ------------------------------
4205 -- Expand_Freeze_Array_Type --
4206 ------------------------------
4208 procedure Expand_Freeze_Array_Type (N : Node_Id) is
4209 Typ : constant Entity_Id := Entity (N);
4210 Base : constant Entity_Id := Base_Type (Typ);
4211 Comp_Typ : constant Entity_Id := Component_Type (Typ);
4213 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
4215 begin
4216 -- Ensure that all freezing activities are properly flagged as Ghost
4218 Set_Ghost_Mode_From_Entity (Typ);
4220 if not Is_Bit_Packed_Array (Typ) then
4222 -- If the component contains tasks, so does the array type. This may
4223 -- not be indicated in the array type because the component may have
4224 -- been a private type at the point of definition. Same if component
4225 -- type is controlled or contains protected objects.
4227 Propagate_Concurrent_Flags (Base, Comp_Typ);
4228 Set_Has_Controlled_Component
4229 (Base, Has_Controlled_Component (Comp_Typ)
4230 or else Is_Controlled (Comp_Typ));
4232 if No (Init_Proc (Base)) then
4234 -- If this is an anonymous array created for a declaration with
4235 -- an initial value, its init_proc will never be called. The
4236 -- initial value itself may have been expanded into assignments,
4237 -- in which case the object declaration is carries the
4238 -- No_Initialization flag.
4240 if Is_Itype (Base)
4241 and then Nkind (Associated_Node_For_Itype (Base)) =
4242 N_Object_Declaration
4243 and then
4244 (Present (Expression (Associated_Node_For_Itype (Base)))
4245 or else No_Initialization (Associated_Node_For_Itype (Base)))
4246 then
4247 null;
4249 -- We do not need an init proc for string or wide [wide] string,
4250 -- since the only time these need initialization in normalize or
4251 -- initialize scalars mode, and these types are treated specially
4252 -- and do not need initialization procedures.
4254 elsif Is_Standard_String_Type (Base) then
4255 null;
4257 -- Otherwise we have to build an init proc for the subtype
4259 else
4260 Build_Array_Init_Proc (Base, N);
4261 end if;
4262 end if;
4264 if Typ = Base and then Has_Controlled_Component (Base) then
4265 Build_Controlling_Procs (Base);
4267 if not Is_Limited_Type (Comp_Typ)
4268 and then Number_Dimensions (Typ) = 1
4269 then
4270 Build_Slice_Assignment (Typ);
4271 end if;
4272 end if;
4274 -- For packed case, default initialization, except if the component type
4275 -- is itself a packed structure with an initialization procedure, or
4276 -- initialize/normalize scalars active, and we have a base type, or the
4277 -- type is public, because in that case a client might specify
4278 -- Normalize_Scalars and there better be a public Init_Proc for it.
4280 elsif (Present (Init_Proc (Component_Type (Base)))
4281 and then No (Base_Init_Proc (Base)))
4282 or else (Init_Or_Norm_Scalars and then Base = Typ)
4283 or else Is_Public (Typ)
4284 then
4285 Build_Array_Init_Proc (Base, N);
4286 end if;
4288 Ghost_Mode := Save_Ghost_Mode;
4289 end Expand_Freeze_Array_Type;
4291 -----------------------------------
4292 -- Expand_Freeze_Class_Wide_Type --
4293 -----------------------------------
4295 procedure Expand_Freeze_Class_Wide_Type (N : Node_Id) is
4296 function Is_C_Derivation (Typ : Entity_Id) return Boolean;
4297 -- Given a type, determine whether it is derived from a C or C++ root
4299 ---------------------
4300 -- Is_C_Derivation --
4301 ---------------------
4303 function Is_C_Derivation (Typ : Entity_Id) return Boolean is
4304 T : Entity_Id;
4306 begin
4307 T := Typ;
4308 loop
4309 if Is_CPP_Class (T)
4310 or else Convention (T) = Convention_C
4311 or else Convention (T) = Convention_CPP
4312 then
4313 return True;
4314 end if;
4316 exit when T = Etype (T);
4318 T := Etype (T);
4319 end loop;
4321 return False;
4322 end Is_C_Derivation;
4324 -- Local variables
4326 Typ : constant Entity_Id := Entity (N);
4327 Root : constant Entity_Id := Root_Type (Typ);
4329 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
4331 -- Start of processing for Expand_Freeze_Class_Wide_Type
4333 begin
4334 -- Certain run-time configurations and targets do not provide support
4335 -- for controlled types.
4337 if Restriction_Active (No_Finalization) then
4338 return;
4340 -- Do not create TSS routine Finalize_Address when dispatching calls are
4341 -- disabled since the core of the routine is a dispatching call.
4343 elsif Restriction_Active (No_Dispatching_Calls) then
4344 return;
4346 -- Do not create TSS routine Finalize_Address for concurrent class-wide
4347 -- types. Ignore C, C++, CIL and Java types since it is assumed that the
4348 -- non-Ada side will handle their destruction.
4350 elsif Is_Concurrent_Type (Root)
4351 or else Is_C_Derivation (Root)
4352 or else Convention (Typ) = Convention_CPP
4353 then
4354 return;
4356 -- Do not create TSS routine Finalize_Address when compiling in CodePeer
4357 -- mode since the routine contains an Unchecked_Conversion.
4359 elsif CodePeer_Mode then
4360 return;
4361 end if;
4363 -- Ensure that all freezing activities are properly flagged as Ghost
4365 Set_Ghost_Mode_From_Entity (Typ);
4367 -- Create the body of TSS primitive Finalize_Address. This automatically
4368 -- sets the TSS entry for the class-wide type.
4370 Make_Finalize_Address_Body (Typ);
4371 Ghost_Mode := Save_Ghost_Mode;
4372 end Expand_Freeze_Class_Wide_Type;
4374 ------------------------------------
4375 -- Expand_Freeze_Enumeration_Type --
4376 ------------------------------------
4378 procedure Expand_Freeze_Enumeration_Type (N : Node_Id) is
4379 Typ : constant Entity_Id := Entity (N);
4380 Loc : constant Source_Ptr := Sloc (Typ);
4382 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
4384 Arr : Entity_Id;
4385 Ent : Entity_Id;
4386 Fent : Entity_Id;
4387 Is_Contiguous : Boolean;
4388 Ityp : Entity_Id;
4389 Last_Repval : Uint;
4390 Lst : List_Id;
4391 Num : Nat;
4392 Pos_Expr : Node_Id;
4394 Func : Entity_Id;
4395 pragma Warnings (Off, Func);
4397 begin
4398 -- Ensure that all freezing activities are properly flagged as Ghost
4400 Set_Ghost_Mode_From_Entity (Typ);
4402 -- Various optimizations possible if given representation is contiguous
4404 Is_Contiguous := True;
4406 Ent := First_Literal (Typ);
4407 Last_Repval := Enumeration_Rep (Ent);
4409 Next_Literal (Ent);
4410 while Present (Ent) loop
4411 if Enumeration_Rep (Ent) - Last_Repval /= 1 then
4412 Is_Contiguous := False;
4413 exit;
4414 else
4415 Last_Repval := Enumeration_Rep (Ent);
4416 end if;
4418 Next_Literal (Ent);
4419 end loop;
4421 if Is_Contiguous then
4422 Set_Has_Contiguous_Rep (Typ);
4423 Ent := First_Literal (Typ);
4424 Num := 1;
4425 Lst := New_List (New_Occurrence_Of (Ent, Sloc (Ent)));
4427 else
4428 -- Build list of literal references
4430 Lst := New_List;
4431 Num := 0;
4433 Ent := First_Literal (Typ);
4434 while Present (Ent) loop
4435 Append_To (Lst, New_Occurrence_Of (Ent, Sloc (Ent)));
4436 Num := Num + 1;
4437 Next_Literal (Ent);
4438 end loop;
4439 end if;
4441 -- Now build an array declaration
4443 -- typA : array (Natural range 0 .. num - 1) of ctype :=
4444 -- (v, v, v, v, v, ....)
4446 -- where ctype is the corresponding integer type. If the representation
4447 -- is contiguous, we only keep the first literal, which provides the
4448 -- offset for Pos_To_Rep computations.
4450 Arr :=
4451 Make_Defining_Identifier (Loc,
4452 Chars => New_External_Name (Chars (Typ), 'A'));
4454 Append_Freeze_Action (Typ,
4455 Make_Object_Declaration (Loc,
4456 Defining_Identifier => Arr,
4457 Constant_Present => True,
4459 Object_Definition =>
4460 Make_Constrained_Array_Definition (Loc,
4461 Discrete_Subtype_Definitions => New_List (
4462 Make_Subtype_Indication (Loc,
4463 Subtype_Mark => New_Occurrence_Of (Standard_Natural, Loc),
4464 Constraint =>
4465 Make_Range_Constraint (Loc,
4466 Range_Expression =>
4467 Make_Range (Loc,
4468 Low_Bound =>
4469 Make_Integer_Literal (Loc, 0),
4470 High_Bound =>
4471 Make_Integer_Literal (Loc, Num - 1))))),
4473 Component_Definition =>
4474 Make_Component_Definition (Loc,
4475 Aliased_Present => False,
4476 Subtype_Indication => New_Occurrence_Of (Typ, Loc))),
4478 Expression =>
4479 Make_Aggregate (Loc,
4480 Expressions => Lst)));
4482 Set_Enum_Pos_To_Rep (Typ, Arr);
4484 -- Now we build the function that converts representation values to
4485 -- position values. This function has the form:
4487 -- function _Rep_To_Pos (A : etype; F : Boolean) return Integer is
4488 -- begin
4489 -- case ityp!(A) is
4490 -- when enum-lit'Enum_Rep => return posval;
4491 -- when enum-lit'Enum_Rep => return posval;
4492 -- ...
4493 -- when others =>
4494 -- [raise Constraint_Error when F "invalid data"]
4495 -- return -1;
4496 -- end case;
4497 -- end;
4499 -- Note: the F parameter determines whether the others case (no valid
4500 -- representation) raises Constraint_Error or returns a unique value
4501 -- of minus one. The latter case is used, e.g. in 'Valid code.
4503 -- Note: the reason we use Enum_Rep values in the case here is to avoid
4504 -- the code generator making inappropriate assumptions about the range
4505 -- of the values in the case where the value is invalid. ityp is a
4506 -- signed or unsigned integer type of appropriate width.
4508 -- Note: if exceptions are not supported, then we suppress the raise
4509 -- and return -1 unconditionally (this is an erroneous program in any
4510 -- case and there is no obligation to raise Constraint_Error here). We
4511 -- also do this if pragma Restrictions (No_Exceptions) is active.
4513 -- Is this right??? What about No_Exception_Propagation???
4515 -- Representations are signed
4517 if Enumeration_Rep (First_Literal (Typ)) < 0 then
4519 -- The underlying type is signed. Reset the Is_Unsigned_Type
4520 -- explicitly, because it might have been inherited from
4521 -- parent type.
4523 Set_Is_Unsigned_Type (Typ, False);
4525 if Esize (Typ) <= Standard_Integer_Size then
4526 Ityp := Standard_Integer;
4527 else
4528 Ityp := Universal_Integer;
4529 end if;
4531 -- Representations are unsigned
4533 else
4534 if Esize (Typ) <= Standard_Integer_Size then
4535 Ityp := RTE (RE_Unsigned);
4536 else
4537 Ityp := RTE (RE_Long_Long_Unsigned);
4538 end if;
4539 end if;
4541 -- The body of the function is a case statement. First collect case
4542 -- alternatives, or optimize the contiguous case.
4544 Lst := New_List;
4546 -- If representation is contiguous, Pos is computed by subtracting
4547 -- the representation of the first literal.
4549 if Is_Contiguous then
4550 Ent := First_Literal (Typ);
4552 if Enumeration_Rep (Ent) = Last_Repval then
4554 -- Another special case: for a single literal, Pos is zero
4556 Pos_Expr := Make_Integer_Literal (Loc, Uint_0);
4558 else
4559 Pos_Expr :=
4560 Convert_To (Standard_Integer,
4561 Make_Op_Subtract (Loc,
4562 Left_Opnd =>
4563 Unchecked_Convert_To
4564 (Ityp, Make_Identifier (Loc, Name_uA)),
4565 Right_Opnd =>
4566 Make_Integer_Literal (Loc,
4567 Intval => Enumeration_Rep (First_Literal (Typ)))));
4568 end if;
4570 Append_To (Lst,
4571 Make_Case_Statement_Alternative (Loc,
4572 Discrete_Choices => New_List (
4573 Make_Range (Sloc (Enumeration_Rep_Expr (Ent)),
4574 Low_Bound =>
4575 Make_Integer_Literal (Loc,
4576 Intval => Enumeration_Rep (Ent)),
4577 High_Bound =>
4578 Make_Integer_Literal (Loc, Intval => Last_Repval))),
4580 Statements => New_List (
4581 Make_Simple_Return_Statement (Loc,
4582 Expression => Pos_Expr))));
4584 else
4585 Ent := First_Literal (Typ);
4586 while Present (Ent) loop
4587 Append_To (Lst,
4588 Make_Case_Statement_Alternative (Loc,
4589 Discrete_Choices => New_List (
4590 Make_Integer_Literal (Sloc (Enumeration_Rep_Expr (Ent)),
4591 Intval => Enumeration_Rep (Ent))),
4593 Statements => New_List (
4594 Make_Simple_Return_Statement (Loc,
4595 Expression =>
4596 Make_Integer_Literal (Loc,
4597 Intval => Enumeration_Pos (Ent))))));
4599 Next_Literal (Ent);
4600 end loop;
4601 end if;
4603 -- In normal mode, add the others clause with the test.
4604 -- If Predicates_Ignored is True, validity checks do not apply to
4605 -- the subtype.
4607 if not No_Exception_Handlers_Set
4608 and then not Predicates_Ignored (Typ)
4609 then
4610 Append_To (Lst,
4611 Make_Case_Statement_Alternative (Loc,
4612 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
4613 Statements => New_List (
4614 Make_Raise_Constraint_Error (Loc,
4615 Condition => Make_Identifier (Loc, Name_uF),
4616 Reason => CE_Invalid_Data),
4617 Make_Simple_Return_Statement (Loc,
4618 Expression => Make_Integer_Literal (Loc, -1)))));
4620 -- If either of the restrictions No_Exceptions_Handlers/Propagation is
4621 -- active then return -1 (we cannot usefully raise Constraint_Error in
4622 -- this case). See description above for further details.
4624 else
4625 Append_To (Lst,
4626 Make_Case_Statement_Alternative (Loc,
4627 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
4628 Statements => New_List (
4629 Make_Simple_Return_Statement (Loc,
4630 Expression => Make_Integer_Literal (Loc, -1)))));
4631 end if;
4633 -- Now we can build the function body
4635 Fent :=
4636 Make_Defining_Identifier (Loc, Make_TSS_Name (Typ, TSS_Rep_To_Pos));
4638 Func :=
4639 Make_Subprogram_Body (Loc,
4640 Specification =>
4641 Make_Function_Specification (Loc,
4642 Defining_Unit_Name => Fent,
4643 Parameter_Specifications => New_List (
4644 Make_Parameter_Specification (Loc,
4645 Defining_Identifier =>
4646 Make_Defining_Identifier (Loc, Name_uA),
4647 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
4648 Make_Parameter_Specification (Loc,
4649 Defining_Identifier =>
4650 Make_Defining_Identifier (Loc, Name_uF),
4651 Parameter_Type =>
4652 New_Occurrence_Of (Standard_Boolean, Loc))),
4654 Result_Definition => New_Occurrence_Of (Standard_Integer, Loc)),
4656 Declarations => Empty_List,
4658 Handled_Statement_Sequence =>
4659 Make_Handled_Sequence_Of_Statements (Loc,
4660 Statements => New_List (
4661 Make_Case_Statement (Loc,
4662 Expression =>
4663 Unchecked_Convert_To
4664 (Ityp, Make_Identifier (Loc, Name_uA)),
4665 Alternatives => Lst))));
4667 Set_TSS (Typ, Fent);
4669 -- Set Pure flag (it will be reset if the current context is not Pure).
4670 -- We also pretend there was a pragma Pure_Function so that for purposes
4671 -- of optimization and constant-folding, we will consider the function
4672 -- Pure even if we are not in a Pure context).
4674 Set_Is_Pure (Fent);
4675 Set_Has_Pragma_Pure_Function (Fent);
4677 -- Unless we are in -gnatD mode, where we are debugging generated code,
4678 -- this is an internal entity for which we don't need debug info.
4680 if not Debug_Generated_Code then
4681 Set_Debug_Info_Off (Fent);
4682 end if;
4684 Ghost_Mode := Save_Ghost_Mode;
4686 exception
4687 when RE_Not_Available =>
4688 Ghost_Mode := Save_Ghost_Mode;
4689 return;
4690 end Expand_Freeze_Enumeration_Type;
4692 -------------------------------
4693 -- Expand_Freeze_Record_Type --
4694 -------------------------------
4696 procedure Expand_Freeze_Record_Type (N : Node_Id) is
4697 Typ : constant Node_Id := Entity (N);
4698 Typ_Decl : constant Node_Id := Parent (Typ);
4700 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
4702 Comp : Entity_Id;
4703 Comp_Typ : Entity_Id;
4704 Predef_List : List_Id;
4706 Wrapper_Decl_List : List_Id := No_List;
4707 Wrapper_Body_List : List_Id := No_List;
4709 Renamed_Eq : Node_Id := Empty;
4710 -- Defining unit name for the predefined equality function in the case
4711 -- where the type has a primitive operation that is a renaming of
4712 -- predefined equality (but only if there is also an overriding
4713 -- user-defined equality function). Used to pass this entity from
4714 -- Make_Predefined_Primitive_Specs to Predefined_Primitive_Bodies.
4716 -- Start of processing for Expand_Freeze_Record_Type
4718 begin
4719 -- Ensure that all freezing activities are properly flagged as Ghost
4721 Set_Ghost_Mode_From_Entity (Typ);
4723 -- Build discriminant checking functions if not a derived type (for
4724 -- derived types that are not tagged types, always use the discriminant
4725 -- checking functions of the parent type). However, for untagged types
4726 -- the derivation may have taken place before the parent was frozen, so
4727 -- we copy explicitly the discriminant checking functions from the
4728 -- parent into the components of the derived type.
4730 if not Is_Derived_Type (Typ)
4731 or else Has_New_Non_Standard_Rep (Typ)
4732 or else Is_Tagged_Type (Typ)
4733 then
4734 Build_Discr_Checking_Funcs (Typ_Decl);
4736 elsif Is_Derived_Type (Typ)
4737 and then not Is_Tagged_Type (Typ)
4739 -- If we have a derived Unchecked_Union, we do not inherit the
4740 -- discriminant checking functions from the parent type since the
4741 -- discriminants are non existent.
4743 and then not Is_Unchecked_Union (Typ)
4744 and then Has_Discriminants (Typ)
4745 then
4746 declare
4747 Old_Comp : Entity_Id;
4749 begin
4750 Old_Comp :=
4751 First_Component (Base_Type (Underlying_Type (Etype (Typ))));
4752 Comp := First_Component (Typ);
4753 while Present (Comp) loop
4754 if Ekind (Comp) = E_Component
4755 and then Chars (Comp) = Chars (Old_Comp)
4756 then
4757 Set_Discriminant_Checking_Func
4758 (Comp, Discriminant_Checking_Func (Old_Comp));
4759 end if;
4761 Next_Component (Old_Comp);
4762 Next_Component (Comp);
4763 end loop;
4764 end;
4765 end if;
4767 if Is_Derived_Type (Typ)
4768 and then Is_Limited_Type (Typ)
4769 and then Is_Tagged_Type (Typ)
4770 then
4771 Check_Stream_Attributes (Typ);
4772 end if;
4774 -- Update task, protected, and controlled component flags, because some
4775 -- of the component types may have been private at the point of the
4776 -- record declaration. Detect anonymous access-to-controlled components.
4778 Comp := First_Component (Typ);
4779 while Present (Comp) loop
4780 Comp_Typ := Etype (Comp);
4782 Propagate_Concurrent_Flags (Typ, Comp_Typ);
4784 -- Do not set Has_Controlled_Component on a class-wide equivalent
4785 -- type. See Make_CW_Equivalent_Type.
4787 if not Is_Class_Wide_Equivalent_Type (Typ)
4788 and then
4789 (Has_Controlled_Component (Comp_Typ)
4790 or else (Chars (Comp) /= Name_uParent
4791 and then (Is_Controlled_Active (Comp_Typ))))
4792 then
4793 Set_Has_Controlled_Component (Typ);
4794 end if;
4796 Next_Component (Comp);
4797 end loop;
4799 -- Handle constructors of untagged CPP_Class types
4801 if not Is_Tagged_Type (Typ) and then Is_CPP_Class (Typ) then
4802 Set_CPP_Constructors (Typ);
4803 end if;
4805 -- Creation of the Dispatch Table. Note that a Dispatch Table is built
4806 -- for regular tagged types as well as for Ada types deriving from a C++
4807 -- Class, but not for tagged types directly corresponding to C++ classes
4808 -- In the later case we assume that it is created in the C++ side and we
4809 -- just use it.
4811 if Is_Tagged_Type (Typ) then
4813 -- Add the _Tag component
4815 if Underlying_Type (Etype (Typ)) = Typ then
4816 Expand_Tagged_Root (Typ);
4817 end if;
4819 if Is_CPP_Class (Typ) then
4820 Set_All_DT_Position (Typ);
4822 -- Create the tag entities with a minimum decoration
4824 if Tagged_Type_Expansion then
4825 Append_Freeze_Actions (Typ, Make_Tags (Typ));
4826 end if;
4828 Set_CPP_Constructors (Typ);
4830 else
4831 if not Building_Static_DT (Typ) then
4833 -- Usually inherited primitives are not delayed but the first
4834 -- Ada extension of a CPP_Class is an exception since the
4835 -- address of the inherited subprogram has to be inserted in
4836 -- the new Ada Dispatch Table and this is a freezing action.
4838 -- Similarly, if this is an inherited operation whose parent is
4839 -- not frozen yet, it is not in the DT of the parent, and we
4840 -- generate an explicit freeze node for the inherited operation
4841 -- so it is properly inserted in the DT of the current type.
4843 declare
4844 Elmt : Elmt_Id;
4845 Subp : Entity_Id;
4847 begin
4848 Elmt := First_Elmt (Primitive_Operations (Typ));
4849 while Present (Elmt) loop
4850 Subp := Node (Elmt);
4852 if Present (Alias (Subp)) then
4853 if Is_CPP_Class (Etype (Typ)) then
4854 Set_Has_Delayed_Freeze (Subp);
4856 elsif Has_Delayed_Freeze (Alias (Subp))
4857 and then not Is_Frozen (Alias (Subp))
4858 then
4859 Set_Is_Frozen (Subp, False);
4860 Set_Has_Delayed_Freeze (Subp);
4861 end if;
4862 end if;
4864 Next_Elmt (Elmt);
4865 end loop;
4866 end;
4867 end if;
4869 -- Unfreeze momentarily the type to add the predefined primitives
4870 -- operations. The reason we unfreeze is so that these predefined
4871 -- operations will indeed end up as primitive operations (which
4872 -- must be before the freeze point).
4874 Set_Is_Frozen (Typ, False);
4876 -- Do not add the spec of predefined primitives in case of
4877 -- CPP tagged type derivations that have convention CPP.
4879 if Is_CPP_Class (Root_Type (Typ))
4880 and then Convention (Typ) = Convention_CPP
4881 then
4882 null;
4884 -- Do not add the spec of the predefined primitives if we are
4885 -- compiling under restriction No_Dispatching_Calls.
4887 elsif not Restriction_Active (No_Dispatching_Calls) then
4888 Make_Predefined_Primitive_Specs (Typ, Predef_List, Renamed_Eq);
4889 Insert_List_Before_And_Analyze (N, Predef_List);
4890 end if;
4892 -- Ada 2005 (AI-391): For a nonabstract null extension, create
4893 -- wrapper functions for each nonoverridden inherited function
4894 -- with a controlling result of the type. The wrapper for such
4895 -- a function returns an extension aggregate that invokes the
4896 -- parent function.
4898 if Ada_Version >= Ada_2005
4899 and then not Is_Abstract_Type (Typ)
4900 and then Is_Null_Extension (Typ)
4901 then
4902 Make_Controlling_Function_Wrappers
4903 (Typ, Wrapper_Decl_List, Wrapper_Body_List);
4904 Insert_List_Before_And_Analyze (N, Wrapper_Decl_List);
4905 end if;
4907 -- Ada 2005 (AI-251): For a nonabstract type extension, build
4908 -- null procedure declarations for each set of homographic null
4909 -- procedures that are inherited from interface types but not
4910 -- overridden. This is done to ensure that the dispatch table
4911 -- entry associated with such null primitives are properly filled.
4913 if Ada_Version >= Ada_2005
4914 and then Etype (Typ) /= Typ
4915 and then not Is_Abstract_Type (Typ)
4916 and then Has_Interfaces (Typ)
4917 then
4918 Insert_Actions (N, Make_Null_Procedure_Specs (Typ));
4919 end if;
4921 Set_Is_Frozen (Typ);
4923 if not Is_Derived_Type (Typ)
4924 or else Is_Tagged_Type (Etype (Typ))
4925 then
4926 Set_All_DT_Position (Typ);
4928 -- If this is a type derived from an untagged private type whose
4929 -- full view is tagged, the type is marked tagged for layout
4930 -- reasons, but it has no dispatch table.
4932 elsif Is_Derived_Type (Typ)
4933 and then Is_Private_Type (Etype (Typ))
4934 and then not Is_Tagged_Type (Etype (Typ))
4935 then
4936 return;
4937 end if;
4939 -- Create and decorate the tags. Suppress their creation when
4940 -- not Tagged_Type_Expansion because the dispatching mechanism is
4941 -- handled internally by the virtual target.
4943 if Tagged_Type_Expansion then
4944 Append_Freeze_Actions (Typ, Make_Tags (Typ));
4946 -- Generate dispatch table of locally defined tagged type.
4947 -- Dispatch tables of library level tagged types are built
4948 -- later (see Analyze_Declarations).
4950 if not Building_Static_DT (Typ) then
4951 Append_Freeze_Actions (Typ, Make_DT (Typ));
4952 end if;
4953 end if;
4955 -- If the type has unknown discriminants, propagate dispatching
4956 -- information to its underlying record view, which does not get
4957 -- its own dispatch table.
4959 if Is_Derived_Type (Typ)
4960 and then Has_Unknown_Discriminants (Typ)
4961 and then Present (Underlying_Record_View (Typ))
4962 then
4963 declare
4964 Rep : constant Entity_Id := Underlying_Record_View (Typ);
4965 begin
4966 Set_Access_Disp_Table
4967 (Rep, Access_Disp_Table (Typ));
4968 Set_Dispatch_Table_Wrappers
4969 (Rep, Dispatch_Table_Wrappers (Typ));
4970 Set_Direct_Primitive_Operations
4971 (Rep, Direct_Primitive_Operations (Typ));
4972 end;
4973 end if;
4975 -- Make sure that the primitives Initialize, Adjust and Finalize
4976 -- are Frozen before other TSS subprograms. We don't want them
4977 -- Frozen inside.
4979 if Is_Controlled (Typ) then
4980 if not Is_Limited_Type (Typ) then
4981 Append_Freeze_Actions (Typ,
4982 Freeze_Entity (Find_Prim_Op (Typ, Name_Adjust), Typ));
4983 end if;
4985 Append_Freeze_Actions (Typ,
4986 Freeze_Entity (Find_Prim_Op (Typ, Name_Initialize), Typ));
4988 Append_Freeze_Actions (Typ,
4989 Freeze_Entity (Find_Prim_Op (Typ, Name_Finalize), Typ));
4990 end if;
4992 -- Freeze rest of primitive operations. There is no need to handle
4993 -- the predefined primitives if we are compiling under restriction
4994 -- No_Dispatching_Calls.
4996 if not Restriction_Active (No_Dispatching_Calls) then
4997 Append_Freeze_Actions (Typ, Predefined_Primitive_Freeze (Typ));
4998 end if;
4999 end if;
5001 -- In the untagged case, ever since Ada 83 an equality function must
5002 -- be provided for variant records that are not unchecked unions.
5003 -- In Ada 2012 the equality function composes, and thus must be built
5004 -- explicitly just as for tagged records.
5006 elsif Has_Discriminants (Typ)
5007 and then not Is_Limited_Type (Typ)
5008 then
5009 declare
5010 Comps : constant Node_Id :=
5011 Component_List (Type_Definition (Typ_Decl));
5012 begin
5013 if Present (Comps)
5014 and then Present (Variant_Part (Comps))
5015 then
5016 Build_Variant_Record_Equality (Typ);
5017 end if;
5018 end;
5020 -- Otherwise create primitive equality operation (AI05-0123)
5022 -- This is done unconditionally to ensure that tools can be linked
5023 -- properly with user programs compiled with older language versions.
5024 -- In addition, this is needed because "=" composes for bounded strings
5025 -- in all language versions (see Exp_Ch4.Expand_Composite_Equality).
5027 elsif Comes_From_Source (Typ)
5028 and then Convention (Typ) = Convention_Ada
5029 and then not Is_Limited_Type (Typ)
5030 then
5031 Build_Untagged_Equality (Typ);
5032 end if;
5034 -- Before building the record initialization procedure, if we are
5035 -- dealing with a concurrent record value type, then we must go through
5036 -- the discriminants, exchanging discriminals between the concurrent
5037 -- type and the concurrent record value type. See the section "Handling
5038 -- of Discriminants" in the Einfo spec for details.
5040 if Is_Concurrent_Record_Type (Typ)
5041 and then Has_Discriminants (Typ)
5042 then
5043 declare
5044 Ctyp : constant Entity_Id :=
5045 Corresponding_Concurrent_Type (Typ);
5046 Conc_Discr : Entity_Id;
5047 Rec_Discr : Entity_Id;
5048 Temp : Entity_Id;
5050 begin
5051 Conc_Discr := First_Discriminant (Ctyp);
5052 Rec_Discr := First_Discriminant (Typ);
5053 while Present (Conc_Discr) loop
5054 Temp := Discriminal (Conc_Discr);
5055 Set_Discriminal (Conc_Discr, Discriminal (Rec_Discr));
5056 Set_Discriminal (Rec_Discr, Temp);
5058 Set_Discriminal_Link (Discriminal (Conc_Discr), Conc_Discr);
5059 Set_Discriminal_Link (Discriminal (Rec_Discr), Rec_Discr);
5061 Next_Discriminant (Conc_Discr);
5062 Next_Discriminant (Rec_Discr);
5063 end loop;
5064 end;
5065 end if;
5067 if Has_Controlled_Component (Typ) then
5068 Build_Controlling_Procs (Typ);
5069 end if;
5071 Adjust_Discriminants (Typ);
5073 -- Do not need init for interfaces on virtual targets since they're
5074 -- abstract.
5076 if Tagged_Type_Expansion or else not Is_Interface (Typ) then
5077 Build_Record_Init_Proc (Typ_Decl, Typ);
5078 end if;
5080 -- For tagged type that are not interfaces, build bodies of primitive
5081 -- operations. Note: do this after building the record initialization
5082 -- procedure, since the primitive operations may need the initialization
5083 -- routine. There is no need to add predefined primitives of interfaces
5084 -- because all their predefined primitives are abstract.
5086 if Is_Tagged_Type (Typ) and then not Is_Interface (Typ) then
5088 -- Do not add the body of predefined primitives in case of CPP tagged
5089 -- type derivations that have convention CPP.
5091 if Is_CPP_Class (Root_Type (Typ))
5092 and then Convention (Typ) = Convention_CPP
5093 then
5094 null;
5096 -- Do not add the body of the predefined primitives if we are
5097 -- compiling under restriction No_Dispatching_Calls or if we are
5098 -- compiling a CPP tagged type.
5100 elsif not Restriction_Active (No_Dispatching_Calls) then
5102 -- Create the body of TSS primitive Finalize_Address. This must
5103 -- be done before the bodies of all predefined primitives are
5104 -- created. If Typ is limited, Stream_Input and Stream_Read may
5105 -- produce build-in-place allocations and for those the expander
5106 -- needs Finalize_Address.
5108 Make_Finalize_Address_Body (Typ);
5109 Predef_List := Predefined_Primitive_Bodies (Typ, Renamed_Eq);
5110 Append_Freeze_Actions (Typ, Predef_List);
5111 end if;
5113 -- Ada 2005 (AI-391): If any wrappers were created for nonoverridden
5114 -- inherited functions, then add their bodies to the freeze actions.
5116 if Present (Wrapper_Body_List) then
5117 Append_Freeze_Actions (Typ, Wrapper_Body_List);
5118 end if;
5120 -- Create extra formals for the primitive operations of the type.
5121 -- This must be done before analyzing the body of the initialization
5122 -- procedure, because a self-referential type might call one of these
5123 -- primitives in the body of the init_proc itself.
5125 declare
5126 Elmt : Elmt_Id;
5127 Subp : Entity_Id;
5129 begin
5130 Elmt := First_Elmt (Primitive_Operations (Typ));
5131 while Present (Elmt) loop
5132 Subp := Node (Elmt);
5133 if not Has_Foreign_Convention (Subp)
5134 and then not Is_Predefined_Dispatching_Operation (Subp)
5135 then
5136 Create_Extra_Formals (Subp);
5137 end if;
5139 Next_Elmt (Elmt);
5140 end loop;
5141 end;
5142 end if;
5144 Ghost_Mode := Save_Ghost_Mode;
5145 end Expand_Freeze_Record_Type;
5147 ------------------------------------
5148 -- Expand_N_Full_Type_Declaration --
5149 ------------------------------------
5151 procedure Expand_N_Full_Type_Declaration (N : Node_Id) is
5152 procedure Build_Master (Ptr_Typ : Entity_Id);
5153 -- Create the master associated with Ptr_Typ
5155 ------------------
5156 -- Build_Master --
5157 ------------------
5159 procedure Build_Master (Ptr_Typ : Entity_Id) is
5160 Desig_Typ : Entity_Id := Designated_Type (Ptr_Typ);
5162 begin
5163 -- If the designated type is an incomplete view coming from a
5164 -- limited-with'ed package, we need to use the nonlimited view in
5165 -- case it has tasks.
5167 if Ekind (Desig_Typ) in Incomplete_Kind
5168 and then Present (Non_Limited_View (Desig_Typ))
5169 then
5170 Desig_Typ := Non_Limited_View (Desig_Typ);
5171 end if;
5173 -- Anonymous access types are created for the components of the
5174 -- record parameter for an entry declaration. No master is created
5175 -- for such a type.
5177 if Comes_From_Source (N) and then Has_Task (Desig_Typ) then
5178 Build_Master_Entity (Ptr_Typ);
5179 Build_Master_Renaming (Ptr_Typ);
5181 -- Create a class-wide master because a Master_Id must be generated
5182 -- for access-to-limited-class-wide types whose root may be extended
5183 -- with task components.
5185 -- Note: This code covers access-to-limited-interfaces because they
5186 -- can be used to reference tasks implementing them.
5188 elsif Is_Limited_Class_Wide_Type (Desig_Typ)
5189 and then Tasking_Allowed
5190 then
5191 Build_Class_Wide_Master (Ptr_Typ);
5192 end if;
5193 end Build_Master;
5195 -- Local declarations
5197 Def_Id : constant Entity_Id := Defining_Identifier (N);
5198 B_Id : constant Entity_Id := Base_Type (Def_Id);
5199 FN : Node_Id;
5200 Par_Id : Entity_Id;
5202 -- Start of processing for Expand_N_Full_Type_Declaration
5204 begin
5205 if Is_Access_Type (Def_Id) then
5206 Build_Master (Def_Id);
5208 if Ekind (Def_Id) = E_Access_Protected_Subprogram_Type then
5209 Expand_Access_Protected_Subprogram_Type (N);
5210 end if;
5212 -- Array of anonymous access-to-task pointers
5214 elsif Ada_Version >= Ada_2005
5215 and then Is_Array_Type (Def_Id)
5216 and then Is_Access_Type (Component_Type (Def_Id))
5217 and then Ekind (Component_Type (Def_Id)) = E_Anonymous_Access_Type
5218 then
5219 Build_Master (Component_Type (Def_Id));
5221 elsif Has_Task (Def_Id) then
5222 Expand_Previous_Access_Type (Def_Id);
5224 -- Check the components of a record type or array of records for
5225 -- anonymous access-to-task pointers.
5227 elsif Ada_Version >= Ada_2005
5228 and then (Is_Record_Type (Def_Id)
5229 or else
5230 (Is_Array_Type (Def_Id)
5231 and then Is_Record_Type (Component_Type (Def_Id))))
5232 then
5233 declare
5234 Comp : Entity_Id;
5235 First : Boolean;
5236 M_Id : Entity_Id;
5237 Typ : Entity_Id;
5239 begin
5240 if Is_Array_Type (Def_Id) then
5241 Comp := First_Entity (Component_Type (Def_Id));
5242 else
5243 Comp := First_Entity (Def_Id);
5244 end if;
5246 -- Examine all components looking for anonymous access-to-task
5247 -- types.
5249 First := True;
5250 while Present (Comp) loop
5251 Typ := Etype (Comp);
5253 if Ekind (Typ) = E_Anonymous_Access_Type
5254 and then Has_Task (Available_View (Designated_Type (Typ)))
5255 and then No (Master_Id (Typ))
5256 then
5257 -- Ensure that the record or array type have a _master
5259 if First then
5260 Build_Master_Entity (Def_Id);
5261 Build_Master_Renaming (Typ);
5262 M_Id := Master_Id (Typ);
5264 First := False;
5266 -- Reuse the same master to service any additional types
5268 else
5269 Set_Master_Id (Typ, M_Id);
5270 end if;
5271 end if;
5273 Next_Entity (Comp);
5274 end loop;
5275 end;
5276 end if;
5278 Par_Id := Etype (B_Id);
5280 -- The parent type is private then we need to inherit any TSS operations
5281 -- from the full view.
5283 if Ekind (Par_Id) in Private_Kind
5284 and then Present (Full_View (Par_Id))
5285 then
5286 Par_Id := Base_Type (Full_View (Par_Id));
5287 end if;
5289 if Nkind (Type_Definition (Original_Node (N))) =
5290 N_Derived_Type_Definition
5291 and then not Is_Tagged_Type (Def_Id)
5292 and then Present (Freeze_Node (Par_Id))
5293 and then Present (TSS_Elist (Freeze_Node (Par_Id)))
5294 then
5295 Ensure_Freeze_Node (B_Id);
5296 FN := Freeze_Node (B_Id);
5298 if No (TSS_Elist (FN)) then
5299 Set_TSS_Elist (FN, New_Elmt_List);
5300 end if;
5302 declare
5303 T_E : constant Elist_Id := TSS_Elist (FN);
5304 Elmt : Elmt_Id;
5306 begin
5307 Elmt := First_Elmt (TSS_Elist (Freeze_Node (Par_Id)));
5308 while Present (Elmt) loop
5309 if Chars (Node (Elmt)) /= Name_uInit then
5310 Append_Elmt (Node (Elmt), T_E);
5311 end if;
5313 Next_Elmt (Elmt);
5314 end loop;
5316 -- If the derived type itself is private with a full view, then
5317 -- associate the full view with the inherited TSS_Elist as well.
5319 if Ekind (B_Id) in Private_Kind
5320 and then Present (Full_View (B_Id))
5321 then
5322 Ensure_Freeze_Node (Base_Type (Full_View (B_Id)));
5323 Set_TSS_Elist
5324 (Freeze_Node (Base_Type (Full_View (B_Id))), TSS_Elist (FN));
5325 end if;
5326 end;
5327 end if;
5328 end Expand_N_Full_Type_Declaration;
5330 ---------------------------------
5331 -- Expand_N_Object_Declaration --
5332 ---------------------------------
5334 procedure Expand_N_Object_Declaration (N : Node_Id) is
5335 Loc : constant Source_Ptr := Sloc (N);
5336 Def_Id : constant Entity_Id := Defining_Identifier (N);
5337 Expr : constant Node_Id := Expression (N);
5338 Obj_Def : constant Node_Id := Object_Definition (N);
5339 Typ : constant Entity_Id := Etype (Def_Id);
5340 Base_Typ : constant Entity_Id := Base_Type (Typ);
5341 Expr_Q : Node_Id;
5343 function Build_Equivalent_Aggregate return Boolean;
5344 -- If the object has a constrained discriminated type and no initial
5345 -- value, it may be possible to build an equivalent aggregate instead,
5346 -- and prevent an actual call to the initialization procedure.
5348 procedure Default_Initialize_Object (After : Node_Id);
5349 -- Generate all default initialization actions for object Def_Id. Any
5350 -- new code is inserted after node After.
5352 function Rewrite_As_Renaming return Boolean;
5353 -- Indicate whether to rewrite a declaration with initialization into an
5354 -- object renaming declaration (see below).
5356 --------------------------------
5357 -- Build_Equivalent_Aggregate --
5358 --------------------------------
5360 function Build_Equivalent_Aggregate return Boolean is
5361 Aggr : Node_Id;
5362 Comp : Entity_Id;
5363 Discr : Elmt_Id;
5364 Full_Type : Entity_Id;
5366 begin
5367 Full_Type := Typ;
5369 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
5370 Full_Type := Full_View (Typ);
5371 end if;
5373 -- Only perform this transformation if Elaboration_Code is forbidden
5374 -- or undesirable, and if this is a global entity of a constrained
5375 -- record type.
5377 -- If Initialize_Scalars might be active this transformation cannot
5378 -- be performed either, because it will lead to different semantics
5379 -- or because elaboration code will in fact be created.
5381 if Ekind (Full_Type) /= E_Record_Subtype
5382 or else not Has_Discriminants (Full_Type)
5383 or else not Is_Constrained (Full_Type)
5384 or else Is_Controlled (Full_Type)
5385 or else Is_Limited_Type (Full_Type)
5386 or else not Restriction_Active (No_Initialize_Scalars)
5387 then
5388 return False;
5389 end if;
5391 if Ekind (Current_Scope) = E_Package
5392 and then
5393 (Restriction_Active (No_Elaboration_Code)
5394 or else Is_Preelaborated (Current_Scope))
5395 then
5396 -- Building a static aggregate is possible if the discriminants
5397 -- have static values and the other components have static
5398 -- defaults or none.
5400 Discr := First_Elmt (Discriminant_Constraint (Full_Type));
5401 while Present (Discr) loop
5402 if not Is_OK_Static_Expression (Node (Discr)) then
5403 return False;
5404 end if;
5406 Next_Elmt (Discr);
5407 end loop;
5409 -- Check that initialized components are OK, and that non-
5410 -- initialized components do not require a call to their own
5411 -- initialization procedure.
5413 Comp := First_Component (Full_Type);
5414 while Present (Comp) loop
5415 if Ekind (Comp) = E_Component
5416 and then Present (Expression (Parent (Comp)))
5417 and then
5418 not Is_OK_Static_Expression (Expression (Parent (Comp)))
5419 then
5420 return False;
5422 elsif Has_Non_Null_Base_Init_Proc (Etype (Comp)) then
5423 return False;
5425 end if;
5427 Next_Component (Comp);
5428 end loop;
5430 -- Everything is static, assemble the aggregate, discriminant
5431 -- values first.
5433 Aggr :=
5434 Make_Aggregate (Loc,
5435 Expressions => New_List,
5436 Component_Associations => New_List);
5438 Discr := First_Elmt (Discriminant_Constraint (Full_Type));
5439 while Present (Discr) loop
5440 Append_To (Expressions (Aggr), New_Copy (Node (Discr)));
5441 Next_Elmt (Discr);
5442 end loop;
5444 -- Now collect values of initialized components
5446 Comp := First_Component (Full_Type);
5447 while Present (Comp) loop
5448 if Ekind (Comp) = E_Component
5449 and then Present (Expression (Parent (Comp)))
5450 then
5451 Append_To (Component_Associations (Aggr),
5452 Make_Component_Association (Loc,
5453 Choices => New_List (New_Occurrence_Of (Comp, Loc)),
5454 Expression => New_Copy_Tree
5455 (Expression (Parent (Comp)))));
5456 end if;
5458 Next_Component (Comp);
5459 end loop;
5461 -- Finally, box-initialize remaining components
5463 Append_To (Component_Associations (Aggr),
5464 Make_Component_Association (Loc,
5465 Choices => New_List (Make_Others_Choice (Loc)),
5466 Expression => Empty));
5467 Set_Box_Present (Last (Component_Associations (Aggr)));
5468 Set_Expression (N, Aggr);
5470 if Typ /= Full_Type then
5471 Analyze_And_Resolve (Aggr, Full_View (Base_Type (Full_Type)));
5472 Rewrite (Aggr, Unchecked_Convert_To (Typ, Aggr));
5473 Analyze_And_Resolve (Aggr, Typ);
5474 else
5475 Analyze_And_Resolve (Aggr, Full_Type);
5476 end if;
5478 return True;
5480 else
5481 return False;
5482 end if;
5483 end Build_Equivalent_Aggregate;
5485 -------------------------------
5486 -- Default_Initialize_Object --
5487 -------------------------------
5489 procedure Default_Initialize_Object (After : Node_Id) is
5490 function New_Object_Reference return Node_Id;
5491 -- Return a new reference to Def_Id with attributes Assignment_OK and
5492 -- Must_Not_Freeze already set.
5494 --------------------------
5495 -- New_Object_Reference --
5496 --------------------------
5498 function New_Object_Reference return Node_Id is
5499 Obj_Ref : constant Node_Id := New_Occurrence_Of (Def_Id, Loc);
5501 begin
5502 -- The call to the type init proc or [Deep_]Finalize must not
5503 -- freeze the related object as the call is internally generated.
5504 -- This way legal rep clauses that apply to the object will not be
5505 -- flagged. Note that the initialization call may be removed if
5506 -- pragma Import is encountered or moved to the freeze actions of
5507 -- the object because of an address clause.
5509 Set_Assignment_OK (Obj_Ref);
5510 Set_Must_Not_Freeze (Obj_Ref);
5512 return Obj_Ref;
5513 end New_Object_Reference;
5515 -- Local variables
5517 Exceptions_OK : constant Boolean :=
5518 not Restriction_Active (No_Exception_Propagation);
5520 Aggr_Init : Node_Id;
5521 Comp_Init : List_Id := No_List;
5522 Fin_Call : Node_Id;
5523 Init_Stmts : List_Id := No_List;
5524 Obj_Init : Node_Id := Empty;
5525 Obj_Ref : Node_Id;
5527 -- Start of processing for Default_Initialize_Object
5529 begin
5530 -- Default initialization is suppressed for objects that are already
5531 -- known to be imported (i.e. whose declaration specifies the Import
5532 -- aspect). Note that for objects with a pragma Import, we generate
5533 -- initialization here, and then remove it downstream when processing
5534 -- the pragma. It is also suppressed for variables for which a pragma
5535 -- Suppress_Initialization has been explicitly given
5537 if Is_Imported (Def_Id) or else Suppress_Initialization (Def_Id) then
5538 return;
5539 end if;
5541 -- The expansion performed by this routine is as follows:
5543 -- begin
5544 -- Abort_Defer;
5545 -- Type_Init_Proc (Obj);
5547 -- begin
5548 -- [Deep_]Initialize (Obj);
5550 -- exception
5551 -- when others =>
5552 -- [Deep_]Finalize (Obj, Self => False);
5553 -- raise;
5554 -- end;
5555 -- at end
5556 -- Abort_Undefer_Direct;
5557 -- end;
5559 -- Initialize the components of the object
5561 if Has_Non_Null_Base_Init_Proc (Typ)
5562 and then not No_Initialization (N)
5563 and then not Initialization_Suppressed (Typ)
5564 then
5565 -- Do not initialize the components if No_Default_Initialization
5566 -- applies as the actual restriction check will occur later
5567 -- when the object is frozen as it is not known yet whether the
5568 -- object is imported or not.
5570 if not Restriction_Active (No_Default_Initialization) then
5572 -- If the values of the components are compile-time known, use
5573 -- their prebuilt aggregate form directly.
5575 Aggr_Init := Static_Initialization (Base_Init_Proc (Typ));
5577 if Present (Aggr_Init) then
5578 Set_Expression
5579 (N, New_Copy_Tree (Aggr_Init, New_Scope => Current_Scope));
5581 -- If type has discriminants, try to build an equivalent
5582 -- aggregate using discriminant values from the declaration.
5583 -- This is a useful optimization, in particular if restriction
5584 -- No_Elaboration_Code is active.
5586 elsif Build_Equivalent_Aggregate then
5587 null;
5589 -- Otherwise invoke the type init proc, generate:
5590 -- Type_Init_Proc (Obj);
5592 else
5593 Obj_Ref := New_Object_Reference;
5595 if Comes_From_Source (Def_Id) then
5596 Initialization_Warning (Obj_Ref);
5597 end if;
5599 Comp_Init := Build_Initialization_Call (Loc, Obj_Ref, Typ);
5600 end if;
5601 end if;
5603 -- Provide a default value if the object needs simple initialization
5604 -- and does not already have an initial value. A generated temporary
5605 -- does not require initialization because it will be assigned later.
5607 elsif Needs_Simple_Initialization
5608 (Typ, Initialize_Scalars
5609 and then No (Following_Address_Clause (N)))
5610 and then not Is_Internal (Def_Id)
5611 and then not Has_Init_Expression (N)
5612 then
5613 Set_No_Initialization (N, False);
5614 Set_Expression (N, Get_Simple_Init_Val (Typ, N, Esize (Def_Id)));
5615 Analyze_And_Resolve (Expression (N), Typ);
5616 end if;
5618 -- Initialize the object, generate:
5619 -- [Deep_]Initialize (Obj);
5621 if Needs_Finalization (Typ) and then not No_Initialization (N) then
5622 Obj_Init :=
5623 Make_Init_Call
5624 (Obj_Ref => New_Occurrence_Of (Def_Id, Loc),
5625 Typ => Typ);
5626 end if;
5628 -- Build a special finalization block when both the object and its
5629 -- controlled components are to be initialized. The block finalizes
5630 -- the components if the object initialization fails. Generate:
5632 -- begin
5633 -- <Obj_Init>
5635 -- exception
5636 -- when others =>
5637 -- <Fin_Call>
5638 -- raise;
5639 -- end;
5641 if Has_Controlled_Component (Typ)
5642 and then Present (Comp_Init)
5643 and then Present (Obj_Init)
5644 and then Exceptions_OK
5645 then
5646 Init_Stmts := Comp_Init;
5648 Fin_Call :=
5649 Make_Final_Call
5650 (Obj_Ref => New_Object_Reference,
5651 Typ => Typ,
5652 Skip_Self => True);
5654 if Present (Fin_Call) then
5656 -- Do not emit warnings related to the elaboration order when a
5657 -- controlled object is declared before the body of Finalize is
5658 -- seen.
5660 Set_No_Elaboration_Check (Fin_Call);
5662 Append_To (Init_Stmts,
5663 Make_Block_Statement (Loc,
5664 Declarations => No_List,
5666 Handled_Statement_Sequence =>
5667 Make_Handled_Sequence_Of_Statements (Loc,
5668 Statements => New_List (Obj_Init),
5670 Exception_Handlers => New_List (
5671 Make_Exception_Handler (Loc,
5672 Exception_Choices => New_List (
5673 Make_Others_Choice (Loc)),
5675 Statements => New_List (
5676 Fin_Call,
5677 Make_Raise_Statement (Loc)))))));
5678 end if;
5680 -- Otherwise finalization is not required, the initialization calls
5681 -- are passed to the abort block building circuitry, generate:
5683 -- Type_Init_Proc (Obj);
5684 -- [Deep_]Initialize (Obj);
5686 else
5687 if Present (Comp_Init) then
5688 Init_Stmts := Comp_Init;
5689 end if;
5691 if Present (Obj_Init) then
5692 if No (Init_Stmts) then
5693 Init_Stmts := New_List;
5694 end if;
5696 Append_To (Init_Stmts, Obj_Init);
5697 end if;
5698 end if;
5700 -- Build an abort block to protect the initialization calls
5702 if Abort_Allowed
5703 and then Present (Comp_Init)
5704 and then Present (Obj_Init)
5705 then
5706 -- Generate:
5707 -- Abort_Defer;
5709 Prepend_To (Init_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
5711 -- When exceptions are propagated, abort deferral must take place
5712 -- in the presence of initialization or finalization exceptions.
5713 -- Generate:
5715 -- begin
5716 -- Abort_Defer;
5717 -- <Init_Stmts>
5718 -- at end
5719 -- Abort_Undefer_Direct;
5720 -- end;
5722 if Exceptions_OK then
5723 Init_Stmts := New_List (
5724 Build_Abort_Undefer_Block (Loc,
5725 Stmts => Init_Stmts,
5726 Context => N));
5728 -- Otherwise exceptions are not propagated. Generate:
5730 -- Abort_Defer;
5731 -- <Init_Stmts>
5732 -- Abort_Undefer;
5734 else
5735 Append_To (Init_Stmts,
5736 Build_Runtime_Call (Loc, RE_Abort_Undefer));
5737 end if;
5738 end if;
5740 -- Insert the whole initialization sequence into the tree. If the
5741 -- object has a delayed freeze, as will be the case when it has
5742 -- aspect specifications, the initialization sequence is part of
5743 -- the freeze actions.
5745 if Present (Init_Stmts) then
5746 if Has_Delayed_Freeze (Def_Id) then
5747 Append_Freeze_Actions (Def_Id, Init_Stmts);
5748 else
5749 Insert_Actions_After (After, Init_Stmts);
5750 end if;
5751 end if;
5752 end Default_Initialize_Object;
5754 -------------------------
5755 -- Rewrite_As_Renaming --
5756 -------------------------
5758 function Rewrite_As_Renaming return Boolean is
5759 begin
5760 -- If the object declaration appears in the form
5762 -- Obj : Ctrl_Typ := Func (...);
5764 -- where Ctrl_Typ is controlled but not immutably limited type, then
5765 -- the expansion of the function call should use a dereference of the
5766 -- result to reference the value on the secondary stack.
5768 -- Obj : Ctrl_Typ renames Func (...).all;
5770 -- As a result, the call avoids an extra copy. This an optimization,
5771 -- but it is required for passing ACATS tests in some cases where it
5772 -- would otherwise make two copies. The RM allows removing redunant
5773 -- Adjust/Finalize calls, but does not allow insertion of extra ones.
5775 -- This part is disabled for now, because it breaks GPS builds
5777 return (False -- ???
5778 and then Nkind (Expr_Q) = N_Explicit_Dereference
5779 and then not Comes_From_Source (Expr_Q)
5780 and then Nkind (Original_Node (Expr_Q)) = N_Function_Call
5781 and then Nkind (Object_Definition (N)) in N_Has_Entity
5782 and then (Needs_Finalization (Entity (Object_Definition (N)))))
5784 -- If the initializing expression is for a variable with attribute
5785 -- OK_To_Rename set, then transform:
5787 -- Obj : Typ := Expr;
5789 -- into
5791 -- Obj : Typ renames Expr;
5793 -- provided that Obj is not aliased. The aliased case has to be
5794 -- excluded in general because Expr will not be aliased in
5795 -- general.
5797 or else
5798 (not Aliased_Present (N)
5799 and then Is_Entity_Name (Expr_Q)
5800 and then Ekind (Entity (Expr_Q)) = E_Variable
5801 and then OK_To_Rename (Entity (Expr_Q))
5802 and then Is_Entity_Name (Obj_Def));
5803 end Rewrite_As_Renaming;
5805 -- Local variables
5807 Next_N : constant Node_Id := Next (N);
5808 Id_Ref : Node_Id;
5809 Tag_Assign : Node_Id;
5811 Init_After : Node_Id := N;
5812 -- Node after which the initialization actions are to be inserted. This
5813 -- is normally N, except for the case of a shared passive variable, in
5814 -- which case the init proc call must be inserted only after the bodies
5815 -- of the shared variable procedures have been seen.
5817 -- Start of processing for Expand_N_Object_Declaration
5819 begin
5820 -- Don't do anything for deferred constants. All proper actions will be
5821 -- expanded during the full declaration.
5823 if No (Expr) and Constant_Present (N) then
5824 return;
5825 end if;
5827 -- The type of the object cannot be abstract. This is diagnosed at the
5828 -- point the object is frozen, which happens after the declaration is
5829 -- fully expanded, so simply return now.
5831 if Is_Abstract_Type (Typ) then
5832 return;
5833 end if;
5835 -- First we do special processing for objects of a tagged type where
5836 -- this is the point at which the type is frozen. The creation of the
5837 -- dispatch table and the initialization procedure have to be deferred
5838 -- to this point, since we reference previously declared primitive
5839 -- subprograms.
5841 -- Force construction of dispatch tables of library level tagged types
5843 if Tagged_Type_Expansion
5844 and then Static_Dispatch_Tables
5845 and then Is_Library_Level_Entity (Def_Id)
5846 and then Is_Library_Level_Tagged_Type (Base_Typ)
5847 and then Ekind_In (Base_Typ, E_Record_Type,
5848 E_Protected_Type,
5849 E_Task_Type)
5850 and then not Has_Dispatch_Table (Base_Typ)
5851 then
5852 declare
5853 New_Nodes : List_Id := No_List;
5855 begin
5856 if Is_Concurrent_Type (Base_Typ) then
5857 New_Nodes := Make_DT (Corresponding_Record_Type (Base_Typ), N);
5858 else
5859 New_Nodes := Make_DT (Base_Typ, N);
5860 end if;
5862 if not Is_Empty_List (New_Nodes) then
5863 Insert_List_Before (N, New_Nodes);
5864 end if;
5865 end;
5866 end if;
5868 -- Make shared memory routines for shared passive variable
5870 if Is_Shared_Passive (Def_Id) then
5871 Init_After := Make_Shared_Var_Procs (N);
5872 end if;
5874 -- If tasks being declared, make sure we have an activation chain
5875 -- defined for the tasks (has no effect if we already have one), and
5876 -- also that a Master variable is established and that the appropriate
5877 -- enclosing construct is established as a task master.
5879 if Has_Task (Typ) then
5880 Build_Activation_Chain_Entity (N);
5881 Build_Master_Entity (Def_Id);
5882 end if;
5884 -- Default initialization required, and no expression present
5886 if No (Expr) then
5888 -- If we have a type with a variant part, the initialization proc
5889 -- will contain implicit tests of the discriminant values, which
5890 -- counts as a violation of the restriction No_Implicit_Conditionals.
5892 if Has_Variant_Part (Typ) then
5893 declare
5894 Msg : Boolean;
5896 begin
5897 Check_Restriction (Msg, No_Implicit_Conditionals, Obj_Def);
5899 if Msg then
5900 Error_Msg_N
5901 ("\initialization of variant record tests discriminants",
5902 Obj_Def);
5903 return;
5904 end if;
5905 end;
5906 end if;
5908 -- For the default initialization case, if we have a private type
5909 -- with invariants, and invariant checks are enabled, then insert an
5910 -- invariant check after the object declaration. Note that it is OK
5911 -- to clobber the object with an invalid value since if the exception
5912 -- is raised, then the object will go out of scope. In the case where
5913 -- an array object is initialized with an aggregate, the expression
5914 -- is removed. Check flag Has_Init_Expression to avoid generating a
5915 -- junk invariant check and flag No_Initialization to avoid checking
5916 -- an uninitialized object such as a compiler temporary used for an
5917 -- aggregate.
5919 if Has_Invariants (Base_Typ)
5920 and then Present (Invariant_Procedure (Base_Typ))
5921 and then not Has_Init_Expression (N)
5922 and then not No_Initialization (N)
5923 then
5924 -- If entity has an address clause or aspect, make invariant
5925 -- call into a freeze action for the explicit freeze node for
5926 -- object. Otherwise insert invariant check after declaration.
5928 if Present (Following_Address_Clause (N))
5929 or else Has_Aspect (Def_Id, Aspect_Address)
5930 then
5931 Ensure_Freeze_Node (Def_Id);
5932 Set_Has_Delayed_Freeze (Def_Id);
5933 Set_Is_Frozen (Def_Id, False);
5935 if not Partial_View_Has_Unknown_Discr (Typ) then
5936 Append_Freeze_Action (Def_Id,
5937 Make_Invariant_Call (New_Occurrence_Of (Def_Id, Loc)));
5938 end if;
5940 elsif not Partial_View_Has_Unknown_Discr (Typ) then
5941 Insert_After (N,
5942 Make_Invariant_Call (New_Occurrence_Of (Def_Id, Loc)));
5943 end if;
5944 end if;
5946 Default_Initialize_Object (Init_After);
5948 -- Generate attribute for Persistent_BSS if needed
5950 if Persistent_BSS_Mode
5951 and then Comes_From_Source (N)
5952 and then Is_Potentially_Persistent_Type (Typ)
5953 and then not Has_Init_Expression (N)
5954 and then Is_Library_Level_Entity (Def_Id)
5955 then
5956 declare
5957 Prag : Node_Id;
5958 begin
5959 Prag :=
5960 Make_Linker_Section_Pragma
5961 (Def_Id, Sloc (N), ".persistent.bss");
5962 Insert_After (N, Prag);
5963 Analyze (Prag);
5964 end;
5965 end if;
5967 -- If access type, then we know it is null if not initialized
5969 if Is_Access_Type (Typ) then
5970 Set_Is_Known_Null (Def_Id);
5971 end if;
5973 -- Explicit initialization present
5975 else
5976 -- Obtain actual expression from qualified expression
5978 if Nkind (Expr) = N_Qualified_Expression then
5979 Expr_Q := Expression (Expr);
5980 else
5981 Expr_Q := Expr;
5982 end if;
5984 -- When we have the appropriate type of aggregate in the expression
5985 -- (it has been determined during analysis of the aggregate by
5986 -- setting the delay flag), let's perform in place assignment and
5987 -- thus avoid creating a temporary.
5989 if Is_Delayed_Aggregate (Expr_Q) then
5990 Convert_Aggr_In_Object_Decl (N);
5992 -- Ada 2005 (AI-318-02): If the initialization expression is a call
5993 -- to a build-in-place function, then access to the declared object
5994 -- must be passed to the function. Currently we limit such functions
5995 -- to those with constrained limited result subtypes, but eventually
5996 -- plan to expand the allowed forms of functions that are treated as
5997 -- build-in-place.
5999 elsif Ada_Version >= Ada_2005
6000 and then Is_Build_In_Place_Function_Call (Expr_Q)
6001 then
6002 Make_Build_In_Place_Call_In_Object_Declaration (N, Expr_Q);
6004 -- The previous call expands the expression initializing the
6005 -- built-in-place object into further code that will be analyzed
6006 -- later. No further expansion needed here.
6008 return;
6010 -- Ada 2005 (AI-251): Rewrite the expression that initializes a
6011 -- class-wide interface object to ensure that we copy the full
6012 -- object, unless we are targetting a VM where interfaces are handled
6013 -- by VM itself. Note that if the root type of Typ is an ancestor of
6014 -- Expr's type, both types share the same dispatch table and there is
6015 -- no need to displace the pointer.
6017 elsif Is_Interface (Typ)
6019 -- Avoid never-ending recursion because if Equivalent_Type is set
6020 -- then we've done it already and must not do it again.
6022 and then not
6023 (Nkind (Obj_Def) = N_Identifier
6024 and then Present (Equivalent_Type (Entity (Obj_Def))))
6025 then
6026 pragma Assert (Is_Class_Wide_Type (Typ));
6028 -- If the object is a return object of an inherently limited type,
6029 -- which implies build-in-place treatment, bypass the special
6030 -- treatment of class-wide interface initialization below. In this
6031 -- case, the expansion of the return statement will take care of
6032 -- creating the object (via allocator) and initializing it.
6034 if Is_Return_Object (Def_Id) and then Is_Limited_View (Typ) then
6035 null;
6037 elsif Tagged_Type_Expansion then
6038 declare
6039 Iface : constant Entity_Id := Root_Type (Typ);
6040 Expr_N : Node_Id := Expr;
6041 Expr_Typ : Entity_Id;
6042 New_Expr : Node_Id;
6043 Obj_Id : Entity_Id;
6044 Tag_Comp : Node_Id;
6046 begin
6047 -- If the original node of the expression was a conversion
6048 -- to this specific class-wide interface type then restore
6049 -- the original node because we must copy the object before
6050 -- displacing the pointer to reference the secondary tag
6051 -- component. This code must be kept synchronized with the
6052 -- expansion done by routine Expand_Interface_Conversion
6054 if not Comes_From_Source (Expr_N)
6055 and then Nkind (Expr_N) = N_Explicit_Dereference
6056 and then Nkind (Original_Node (Expr_N)) = N_Type_Conversion
6057 and then Etype (Original_Node (Expr_N)) = Typ
6058 then
6059 Rewrite (Expr_N, Original_Node (Expression (N)));
6060 end if;
6062 -- Avoid expansion of redundant interface conversion
6064 if Is_Interface (Etype (Expr_N))
6065 and then Nkind (Expr_N) = N_Type_Conversion
6066 and then Etype (Expr_N) = Typ
6067 then
6068 Expr_N := Expression (Expr_N);
6069 Set_Expression (N, Expr_N);
6070 end if;
6072 Obj_Id := Make_Temporary (Loc, 'D', Expr_N);
6073 Expr_Typ := Base_Type (Etype (Expr_N));
6075 if Is_Class_Wide_Type (Expr_Typ) then
6076 Expr_Typ := Root_Type (Expr_Typ);
6077 end if;
6079 -- Replace
6080 -- CW : I'Class := Obj;
6081 -- by
6082 -- Tmp : T := Obj;
6083 -- type Ityp is not null access I'Class;
6084 -- CW : I'Class renames Ityp (Tmp.I_Tag'Address).all;
6086 if Comes_From_Source (Expr_N)
6087 and then Nkind (Expr_N) = N_Identifier
6088 and then not Is_Interface (Expr_Typ)
6089 and then Interface_Present_In_Ancestor (Expr_Typ, Typ)
6090 and then (Expr_Typ = Etype (Expr_Typ)
6091 or else not
6092 Is_Variable_Size_Record (Etype (Expr_Typ)))
6093 then
6094 -- Copy the object
6096 Insert_Action (N,
6097 Make_Object_Declaration (Loc,
6098 Defining_Identifier => Obj_Id,
6099 Object_Definition =>
6100 New_Occurrence_Of (Expr_Typ, Loc),
6101 Expression => Relocate_Node (Expr_N)));
6103 -- Statically reference the tag associated with the
6104 -- interface
6106 Tag_Comp :=
6107 Make_Selected_Component (Loc,
6108 Prefix => New_Occurrence_Of (Obj_Id, Loc),
6109 Selector_Name =>
6110 New_Occurrence_Of
6111 (Find_Interface_Tag (Expr_Typ, Iface), Loc));
6113 -- Replace
6114 -- IW : I'Class := Obj;
6115 -- by
6116 -- type Equiv_Record is record ... end record;
6117 -- implicit subtype CW is <Class_Wide_Subtype>;
6118 -- Tmp : CW := CW!(Obj);
6119 -- type Ityp is not null access I'Class;
6120 -- IW : I'Class renames
6121 -- Ityp!(Displace (Temp'Address, I'Tag)).all;
6123 else
6124 -- Generate the equivalent record type and update the
6125 -- subtype indication to reference it.
6127 Expand_Subtype_From_Expr
6128 (N => N,
6129 Unc_Type => Typ,
6130 Subtype_Indic => Obj_Def,
6131 Exp => Expr_N);
6133 if not Is_Interface (Etype (Expr_N)) then
6134 New_Expr := Relocate_Node (Expr_N);
6136 -- For interface types we use 'Address which displaces
6137 -- the pointer to the base of the object (if required)
6139 else
6140 New_Expr :=
6141 Unchecked_Convert_To (Etype (Obj_Def),
6142 Make_Explicit_Dereference (Loc,
6143 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6144 Make_Attribute_Reference (Loc,
6145 Prefix => Relocate_Node (Expr_N),
6146 Attribute_Name => Name_Address))));
6147 end if;
6149 -- Copy the object
6151 if not Is_Limited_Record (Expr_Typ) then
6152 Insert_Action (N,
6153 Make_Object_Declaration (Loc,
6154 Defining_Identifier => Obj_Id,
6155 Object_Definition =>
6156 New_Occurrence_Of (Etype (Obj_Def), Loc),
6157 Expression => New_Expr));
6159 -- Rename limited type object since they cannot be copied
6160 -- This case occurs when the initialization expression
6161 -- has been previously expanded into a temporary object.
6163 else pragma Assert (not Comes_From_Source (Expr_Q));
6164 Insert_Action (N,
6165 Make_Object_Renaming_Declaration (Loc,
6166 Defining_Identifier => Obj_Id,
6167 Subtype_Mark =>
6168 New_Occurrence_Of (Etype (Obj_Def), Loc),
6169 Name =>
6170 Unchecked_Convert_To
6171 (Etype (Obj_Def), New_Expr)));
6172 end if;
6174 -- Dynamically reference the tag associated with the
6175 -- interface.
6177 Tag_Comp :=
6178 Make_Function_Call (Loc,
6179 Name => New_Occurrence_Of (RTE (RE_Displace), Loc),
6180 Parameter_Associations => New_List (
6181 Make_Attribute_Reference (Loc,
6182 Prefix => New_Occurrence_Of (Obj_Id, Loc),
6183 Attribute_Name => Name_Address),
6184 New_Occurrence_Of
6185 (Node (First_Elmt (Access_Disp_Table (Iface))),
6186 Loc)));
6187 end if;
6189 Rewrite (N,
6190 Make_Object_Renaming_Declaration (Loc,
6191 Defining_Identifier => Make_Temporary (Loc, 'D'),
6192 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
6193 Name =>
6194 Convert_Tag_To_Interface (Typ, Tag_Comp)));
6196 -- If the original entity comes from source, then mark the
6197 -- new entity as needing debug information, even though it's
6198 -- defined by a generated renaming that does not come from
6199 -- source, so that Materialize_Entity will be set on the
6200 -- entity when Debug_Renaming_Declaration is called during
6201 -- analysis.
6203 if Comes_From_Source (Def_Id) then
6204 Set_Debug_Info_Needed (Defining_Identifier (N));
6205 end if;
6207 Analyze (N, Suppress => All_Checks);
6209 -- Replace internal identifier of rewritten node by the
6210 -- identifier found in the sources. We also have to exchange
6211 -- entities containing their defining identifiers to ensure
6212 -- the correct replacement of the object declaration by this
6213 -- object renaming declaration because these identifiers
6214 -- were previously added by Enter_Name to the current scope.
6215 -- We must preserve the homonym chain of the source entity
6216 -- as well. We must also preserve the kind of the entity,
6217 -- which may be a constant. Preserve entity chain because
6218 -- itypes may have been generated already, and the full
6219 -- chain must be preserved for final freezing. Finally,
6220 -- preserve Comes_From_Source setting, so that debugging
6221 -- and cross-referencing information is properly kept, and
6222 -- preserve source location, to prevent spurious errors when
6223 -- entities are declared (they must have their own Sloc).
6225 declare
6226 New_Id : constant Entity_Id := Defining_Identifier (N);
6227 Next_Temp : constant Entity_Id := Next_Entity (New_Id);
6228 S_Flag : constant Boolean :=
6229 Comes_From_Source (Def_Id);
6231 begin
6232 Set_Next_Entity (New_Id, Next_Entity (Def_Id));
6233 Set_Next_Entity (Def_Id, Next_Temp);
6235 Set_Chars (Defining_Identifier (N), Chars (Def_Id));
6236 Set_Homonym (Defining_Identifier (N), Homonym (Def_Id));
6237 Set_Ekind (Defining_Identifier (N), Ekind (Def_Id));
6238 Set_Sloc (Defining_Identifier (N), Sloc (Def_Id));
6240 Set_Comes_From_Source (Def_Id, False);
6241 Exchange_Entities (Defining_Identifier (N), Def_Id);
6242 Set_Comes_From_Source (Def_Id, S_Flag);
6243 end;
6244 end;
6245 end if;
6247 return;
6249 -- Common case of explicit object initialization
6251 else
6252 -- In most cases, we must check that the initial value meets any
6253 -- constraint imposed by the declared type. However, there is one
6254 -- very important exception to this rule. If the entity has an
6255 -- unconstrained nominal subtype, then it acquired its constraints
6256 -- from the expression in the first place, and not only does this
6257 -- mean that the constraint check is not needed, but an attempt to
6258 -- perform the constraint check can cause order of elaboration
6259 -- problems.
6261 if not Is_Constr_Subt_For_U_Nominal (Typ) then
6263 -- If this is an allocator for an aggregate that has been
6264 -- allocated in place, delay checks until assignments are
6265 -- made, because the discriminants are not initialized.
6267 if Nkind (Expr) = N_Allocator and then No_Initialization (Expr)
6268 then
6269 null;
6271 -- Otherwise apply a constraint check now if no prev error
6273 elsif Nkind (Expr) /= N_Error then
6274 Apply_Constraint_Check (Expr, Typ);
6276 -- Deal with possible range check
6278 if Do_Range_Check (Expr) then
6280 -- If assignment checks are suppressed, turn off flag
6282 if Suppress_Assignment_Checks (N) then
6283 Set_Do_Range_Check (Expr, False);
6285 -- Otherwise generate the range check
6287 else
6288 Generate_Range_Check
6289 (Expr, Typ, CE_Range_Check_Failed);
6290 end if;
6291 end if;
6292 end if;
6293 end if;
6295 -- If the type is controlled and not inherently limited, then
6296 -- the target is adjusted after the copy and attached to the
6297 -- finalization list. However, no adjustment is done in the case
6298 -- where the object was initialized by a call to a function whose
6299 -- result is built in place, since no copy occurred. (Eventually
6300 -- we plan to support in-place function results for some cases
6301 -- of nonlimited types. ???) Similarly, no adjustment is required
6302 -- if we are going to rewrite the object declaration into a
6303 -- renaming declaration.
6305 if Needs_Finalization (Typ)
6306 and then not Is_Limited_View (Typ)
6307 and then not Rewrite_As_Renaming
6308 then
6309 Insert_Action_After (Init_After,
6310 Make_Adjust_Call (
6311 Obj_Ref => New_Occurrence_Of (Def_Id, Loc),
6312 Typ => Base_Typ));
6313 end if;
6315 -- For tagged types, when an init value is given, the tag has to
6316 -- be re-initialized separately in order to avoid the propagation
6317 -- of a wrong tag coming from a view conversion unless the type
6318 -- is class wide (in this case the tag comes from the init value).
6319 -- Suppress the tag assignment when not Tagged_Type_Expansion
6320 -- because tags are represented implicitly in objects. Ditto for
6321 -- types that are CPP_CLASS, and for initializations that are
6322 -- aggregates, because they have to have the right tag.
6324 -- The re-assignment of the tag has to be done even if the object
6325 -- is a constant. The assignment must be analyzed after the
6326 -- declaration. If an address clause follows, this is handled as
6327 -- part of the freeze actions for the object, otherwise insert
6328 -- tag assignment here.
6330 Tag_Assign := Make_Tag_Assignment (N);
6332 if Present (Tag_Assign) then
6333 if Present (Following_Address_Clause (N)) then
6334 Ensure_Freeze_Node (Def_Id);
6336 else
6337 Insert_Action_After (Init_After, Tag_Assign);
6338 end if;
6340 -- Handle C++ constructor calls. Note that we do not check that
6341 -- Typ is a tagged type since the equivalent Ada type of a C++
6342 -- class that has no virtual methods is an untagged limited
6343 -- record type.
6345 elsif Is_CPP_Constructor_Call (Expr) then
6347 -- The call to the initialization procedure does NOT freeze the
6348 -- object being initialized.
6350 Id_Ref := New_Occurrence_Of (Def_Id, Loc);
6351 Set_Must_Not_Freeze (Id_Ref);
6352 Set_Assignment_OK (Id_Ref);
6354 Insert_Actions_After (Init_After,
6355 Build_Initialization_Call (Loc, Id_Ref, Typ,
6356 Constructor_Ref => Expr));
6358 -- We remove here the original call to the constructor
6359 -- to avoid its management in the backend
6361 Set_Expression (N, Empty);
6362 return;
6364 -- Handle initialization of limited tagged types
6366 elsif Is_Tagged_Type (Typ)
6367 and then Is_Class_Wide_Type (Typ)
6368 and then Is_Limited_Record (Typ)
6369 and then not Is_Limited_Interface (Typ)
6370 then
6371 -- Given that the type is limited we cannot perform a copy. If
6372 -- Expr_Q is the reference to a variable we mark the variable
6373 -- as OK_To_Rename to expand this declaration into a renaming
6374 -- declaration (see bellow).
6376 if Is_Entity_Name (Expr_Q) then
6377 Set_OK_To_Rename (Entity (Expr_Q));
6379 -- If we cannot convert the expression into a renaming we must
6380 -- consider it an internal error because the backend does not
6381 -- have support to handle it.
6383 else
6384 pragma Assert (False);
6385 raise Program_Error;
6386 end if;
6388 -- For discrete types, set the Is_Known_Valid flag if the
6389 -- initializing value is known to be valid. Only do this for
6390 -- source assignments, since otherwise we can end up turning
6391 -- on the known valid flag prematurely from inserted code.
6393 elsif Comes_From_Source (N)
6394 and then Is_Discrete_Type (Typ)
6395 and then Expr_Known_Valid (Expr)
6396 then
6397 Set_Is_Known_Valid (Def_Id);
6399 elsif Is_Access_Type (Typ) then
6401 -- For access types set the Is_Known_Non_Null flag if the
6402 -- initializing value is known to be non-null. We can also set
6403 -- Can_Never_Be_Null if this is a constant.
6405 if Known_Non_Null (Expr) then
6406 Set_Is_Known_Non_Null (Def_Id, True);
6408 if Constant_Present (N) then
6409 Set_Can_Never_Be_Null (Def_Id);
6410 end if;
6411 end if;
6412 end if;
6414 -- If validity checking on copies, validate initial expression.
6415 -- But skip this if declaration is for a generic type, since it
6416 -- makes no sense to validate generic types. Not clear if this
6417 -- can happen for legal programs, but it definitely can arise
6418 -- from previous instantiation errors.
6420 if Validity_Checks_On
6421 and then Comes_From_Source (N)
6422 and then Validity_Check_Copies
6423 and then not Is_Generic_Type (Etype (Def_Id))
6424 then
6425 Ensure_Valid (Expr);
6426 Set_Is_Known_Valid (Def_Id);
6427 end if;
6428 end if;
6430 -- Cases where the back end cannot handle the initialization directly
6431 -- In such cases, we expand an assignment that will be appropriately
6432 -- handled by Expand_N_Assignment_Statement.
6434 -- The exclusion of the unconstrained case is wrong, but for now it
6435 -- is too much trouble ???
6437 if (Is_Possibly_Unaligned_Slice (Expr)
6438 or else (Is_Possibly_Unaligned_Object (Expr)
6439 and then not Represented_As_Scalar (Etype (Expr))))
6440 and then not (Is_Array_Type (Etype (Expr))
6441 and then not Is_Constrained (Etype (Expr)))
6442 then
6443 declare
6444 Stat : constant Node_Id :=
6445 Make_Assignment_Statement (Loc,
6446 Name => New_Occurrence_Of (Def_Id, Loc),
6447 Expression => Relocate_Node (Expr));
6448 begin
6449 Set_Expression (N, Empty);
6450 Set_No_Initialization (N);
6451 Set_Assignment_OK (Name (Stat));
6452 Set_No_Ctrl_Actions (Stat);
6453 Insert_After_And_Analyze (Init_After, Stat);
6454 end;
6455 end if;
6456 end if;
6458 if Nkind (Obj_Def) = N_Access_Definition
6459 and then not Is_Local_Anonymous_Access (Etype (Def_Id))
6460 then
6461 -- An Ada 2012 stand-alone object of an anonymous access type
6463 declare
6464 Loc : constant Source_Ptr := Sloc (N);
6466 Level : constant Entity_Id :=
6467 Make_Defining_Identifier (Sloc (N),
6468 Chars =>
6469 New_External_Name (Chars (Def_Id), Suffix => "L"));
6471 Level_Expr : Node_Id;
6472 Level_Decl : Node_Id;
6474 begin
6475 Set_Ekind (Level, Ekind (Def_Id));
6476 Set_Etype (Level, Standard_Natural);
6477 Set_Scope (Level, Scope (Def_Id));
6479 if No (Expr) then
6481 -- Set accessibility level of null
6483 Level_Expr :=
6484 Make_Integer_Literal (Loc, Scope_Depth (Standard_Standard));
6486 else
6487 Level_Expr := Dynamic_Accessibility_Level (Expr);
6488 end if;
6490 Level_Decl :=
6491 Make_Object_Declaration (Loc,
6492 Defining_Identifier => Level,
6493 Object_Definition =>
6494 New_Occurrence_Of (Standard_Natural, Loc),
6495 Expression => Level_Expr,
6496 Constant_Present => Constant_Present (N),
6497 Has_Init_Expression => True);
6499 Insert_Action_After (Init_After, Level_Decl);
6501 Set_Extra_Accessibility (Def_Id, Level);
6502 end;
6503 end if;
6505 -- If the object is default initialized and its type is subject to
6506 -- pragma Default_Initial_Condition, add a runtime check to verify
6507 -- the assumption of the pragma (SPARK RM 7.3.3). Generate:
6509 -- <Base_Typ>Default_Init_Cond (<Base_Typ> (Def_Id));
6511 -- Note that the check is generated for source objects only
6513 if Comes_From_Source (Def_Id)
6514 and then (Has_Default_Init_Cond (Typ)
6515 or else Has_Inherited_Default_Init_Cond (Typ))
6516 and then not Has_Init_Expression (N)
6517 and then Present (Default_Init_Cond_Procedure (Typ))
6518 then
6519 declare
6520 DIC_Call : constant Node_Id :=
6521 Build_Default_Init_Cond_Call (Loc, Def_Id, Typ);
6522 begin
6523 if Present (Next_N) then
6524 Insert_Before_And_Analyze (Next_N, DIC_Call);
6526 -- The object declaration is the last node in a declarative or a
6527 -- statement list.
6529 else
6530 Append_To (List_Containing (N), DIC_Call);
6531 Analyze (DIC_Call);
6532 end if;
6533 end;
6534 end if;
6536 -- Final transformation - turn the object declaration into a renaming
6537 -- if appropriate. If this is the completion of a deferred constant
6538 -- declaration, then this transformation generates what would be
6539 -- illegal code if written by hand, but that's OK.
6541 if Present (Expr) then
6542 if Rewrite_As_Renaming then
6543 Rewrite (N,
6544 Make_Object_Renaming_Declaration (Loc,
6545 Defining_Identifier => Defining_Identifier (N),
6546 Subtype_Mark => Obj_Def,
6547 Name => Expr_Q));
6549 -- We do not analyze this renaming declaration, because all its
6550 -- components have already been analyzed, and if we were to go
6551 -- ahead and analyze it, we would in effect be trying to generate
6552 -- another declaration of X, which won't do.
6554 Set_Renamed_Object (Defining_Identifier (N), Expr_Q);
6555 Set_Analyzed (N);
6557 -- We do need to deal with debug issues for this renaming
6559 -- First, if entity comes from source, then mark it as needing
6560 -- debug information, even though it is defined by a generated
6561 -- renaming that does not come from source.
6563 if Comes_From_Source (Defining_Identifier (N)) then
6564 Set_Debug_Info_Needed (Defining_Identifier (N));
6565 end if;
6567 -- Now call the routine to generate debug info for the renaming
6569 declare
6570 Decl : constant Node_Id := Debug_Renaming_Declaration (N);
6571 begin
6572 if Present (Decl) then
6573 Insert_Action (N, Decl);
6574 end if;
6575 end;
6576 end if;
6577 end if;
6579 -- Exception on library entity not available
6581 exception
6582 when RE_Not_Available =>
6583 return;
6584 end Expand_N_Object_Declaration;
6586 ---------------------------------
6587 -- Expand_N_Subtype_Indication --
6588 ---------------------------------
6590 -- Add a check on the range of the subtype. The static case is partially
6591 -- duplicated by Process_Range_Expr_In_Decl in Sem_Ch3, but we still need
6592 -- to check here for the static case in order to avoid generating
6593 -- extraneous expanded code. Also deal with validity checking.
6595 procedure Expand_N_Subtype_Indication (N : Node_Id) is
6596 Ran : constant Node_Id := Range_Expression (Constraint (N));
6597 Typ : constant Entity_Id := Entity (Subtype_Mark (N));
6599 begin
6600 if Nkind (Constraint (N)) = N_Range_Constraint then
6601 Validity_Check_Range (Range_Expression (Constraint (N)));
6602 end if;
6604 if Nkind_In (Parent (N), N_Constrained_Array_Definition, N_Slice) then
6605 Apply_Range_Check (Ran, Typ);
6606 end if;
6607 end Expand_N_Subtype_Indication;
6609 ---------------------------
6610 -- Expand_N_Variant_Part --
6611 ---------------------------
6613 -- Note: this procedure no longer has any effect. It used to be that we
6614 -- would replace the choices in the last variant by a when others, and
6615 -- also expanded static predicates in variant choices here, but both of
6616 -- those activities were being done too early, since we can't check the
6617 -- choices until the statically predicated subtypes are frozen, which can
6618 -- happen as late as the free point of the record, and we can't change the
6619 -- last choice to an others before checking the choices, which is now done
6620 -- at the freeze point of the record.
6622 procedure Expand_N_Variant_Part (N : Node_Id) is
6623 begin
6624 null;
6625 end Expand_N_Variant_Part;
6627 ---------------------------------
6628 -- Expand_Previous_Access_Type --
6629 ---------------------------------
6631 procedure Expand_Previous_Access_Type (Def_Id : Entity_Id) is
6632 Ptr_Typ : Entity_Id;
6634 begin
6635 -- Find all access types in the current scope whose designated type is
6636 -- Def_Id and build master renamings for them.
6638 Ptr_Typ := First_Entity (Current_Scope);
6639 while Present (Ptr_Typ) loop
6640 if Is_Access_Type (Ptr_Typ)
6641 and then Designated_Type (Ptr_Typ) = Def_Id
6642 and then No (Master_Id (Ptr_Typ))
6643 then
6644 -- Ensure that the designated type has a master
6646 Build_Master_Entity (Def_Id);
6648 -- Private and incomplete types complicate the insertion of master
6649 -- renamings because the access type may precede the full view of
6650 -- the designated type. For this reason, the master renamings are
6651 -- inserted relative to the designated type.
6653 Build_Master_Renaming (Ptr_Typ, Ins_Nod => Parent (Def_Id));
6654 end if;
6656 Next_Entity (Ptr_Typ);
6657 end loop;
6658 end Expand_Previous_Access_Type;
6660 -----------------------------
6661 -- Expand_Record_Extension --
6662 -----------------------------
6664 -- Add a field _parent at the beginning of the record extension. This is
6665 -- used to implement inheritance. Here are some examples of expansion:
6667 -- 1. no discriminants
6668 -- type T2 is new T1 with null record;
6669 -- gives
6670 -- type T2 is new T1 with record
6671 -- _Parent : T1;
6672 -- end record;
6674 -- 2. renamed discriminants
6675 -- type T2 (B, C : Int) is new T1 (A => B) with record
6676 -- _Parent : T1 (A => B);
6677 -- D : Int;
6678 -- end;
6680 -- 3. inherited discriminants
6681 -- type T2 is new T1 with record -- discriminant A inherited
6682 -- _Parent : T1 (A);
6683 -- D : Int;
6684 -- end;
6686 procedure Expand_Record_Extension (T : Entity_Id; Def : Node_Id) is
6687 Indic : constant Node_Id := Subtype_Indication (Def);
6688 Loc : constant Source_Ptr := Sloc (Def);
6689 Rec_Ext_Part : Node_Id := Record_Extension_Part (Def);
6690 Par_Subtype : Entity_Id;
6691 Comp_List : Node_Id;
6692 Comp_Decl : Node_Id;
6693 Parent_N : Node_Id;
6694 D : Entity_Id;
6695 List_Constr : constant List_Id := New_List;
6697 begin
6698 -- Expand_Record_Extension is called directly from the semantics, so
6699 -- we must check to see whether expansion is active before proceeding,
6700 -- because this affects the visibility of selected components in bodies
6701 -- of instances.
6703 if not Expander_Active then
6704 return;
6705 end if;
6707 -- This may be a derivation of an untagged private type whose full
6708 -- view is tagged, in which case the Derived_Type_Definition has no
6709 -- extension part. Build an empty one now.
6711 if No (Rec_Ext_Part) then
6712 Rec_Ext_Part :=
6713 Make_Record_Definition (Loc,
6714 End_Label => Empty,
6715 Component_List => Empty,
6716 Null_Present => True);
6718 Set_Record_Extension_Part (Def, Rec_Ext_Part);
6719 Mark_Rewrite_Insertion (Rec_Ext_Part);
6720 end if;
6722 Comp_List := Component_List (Rec_Ext_Part);
6724 Parent_N := Make_Defining_Identifier (Loc, Name_uParent);
6726 -- If the derived type inherits its discriminants the type of the
6727 -- _parent field must be constrained by the inherited discriminants
6729 if Has_Discriminants (T)
6730 and then Nkind (Indic) /= N_Subtype_Indication
6731 and then not Is_Constrained (Entity (Indic))
6732 then
6733 D := First_Discriminant (T);
6734 while Present (D) loop
6735 Append_To (List_Constr, New_Occurrence_Of (D, Loc));
6736 Next_Discriminant (D);
6737 end loop;
6739 Par_Subtype :=
6740 Process_Subtype (
6741 Make_Subtype_Indication (Loc,
6742 Subtype_Mark => New_Occurrence_Of (Entity (Indic), Loc),
6743 Constraint =>
6744 Make_Index_Or_Discriminant_Constraint (Loc,
6745 Constraints => List_Constr)),
6746 Def);
6748 -- Otherwise the original subtype_indication is just what is needed
6750 else
6751 Par_Subtype := Process_Subtype (New_Copy_Tree (Indic), Def);
6752 end if;
6754 Set_Parent_Subtype (T, Par_Subtype);
6756 Comp_Decl :=
6757 Make_Component_Declaration (Loc,
6758 Defining_Identifier => Parent_N,
6759 Component_Definition =>
6760 Make_Component_Definition (Loc,
6761 Aliased_Present => False,
6762 Subtype_Indication => New_Occurrence_Of (Par_Subtype, Loc)));
6764 if Null_Present (Rec_Ext_Part) then
6765 Set_Component_List (Rec_Ext_Part,
6766 Make_Component_List (Loc,
6767 Component_Items => New_List (Comp_Decl),
6768 Variant_Part => Empty,
6769 Null_Present => False));
6770 Set_Null_Present (Rec_Ext_Part, False);
6772 elsif Null_Present (Comp_List)
6773 or else Is_Empty_List (Component_Items (Comp_List))
6774 then
6775 Set_Component_Items (Comp_List, New_List (Comp_Decl));
6776 Set_Null_Present (Comp_List, False);
6778 else
6779 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
6780 end if;
6782 Analyze (Comp_Decl);
6783 end Expand_Record_Extension;
6785 ------------------------
6786 -- Expand_Tagged_Root --
6787 ------------------------
6789 procedure Expand_Tagged_Root (T : Entity_Id) is
6790 Def : constant Node_Id := Type_Definition (Parent (T));
6791 Comp_List : Node_Id;
6792 Comp_Decl : Node_Id;
6793 Sloc_N : Source_Ptr;
6795 begin
6796 if Null_Present (Def) then
6797 Set_Component_List (Def,
6798 Make_Component_List (Sloc (Def),
6799 Component_Items => Empty_List,
6800 Variant_Part => Empty,
6801 Null_Present => True));
6802 end if;
6804 Comp_List := Component_List (Def);
6806 if Null_Present (Comp_List)
6807 or else Is_Empty_List (Component_Items (Comp_List))
6808 then
6809 Sloc_N := Sloc (Comp_List);
6810 else
6811 Sloc_N := Sloc (First (Component_Items (Comp_List)));
6812 end if;
6814 Comp_Decl :=
6815 Make_Component_Declaration (Sloc_N,
6816 Defining_Identifier => First_Tag_Component (T),
6817 Component_Definition =>
6818 Make_Component_Definition (Sloc_N,
6819 Aliased_Present => False,
6820 Subtype_Indication => New_Occurrence_Of (RTE (RE_Tag), Sloc_N)));
6822 if Null_Present (Comp_List)
6823 or else Is_Empty_List (Component_Items (Comp_List))
6824 then
6825 Set_Component_Items (Comp_List, New_List (Comp_Decl));
6826 Set_Null_Present (Comp_List, False);
6828 else
6829 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
6830 end if;
6832 -- We don't Analyze the whole expansion because the tag component has
6833 -- already been analyzed previously. Here we just insure that the tree
6834 -- is coherent with the semantic decoration
6836 Find_Type (Subtype_Indication (Component_Definition (Comp_Decl)));
6838 exception
6839 when RE_Not_Available =>
6840 return;
6841 end Expand_Tagged_Root;
6843 ------------------------------
6844 -- Freeze_Stream_Operations --
6845 ------------------------------
6847 procedure Freeze_Stream_Operations (N : Node_Id; Typ : Entity_Id) is
6848 Names : constant array (1 .. 4) of TSS_Name_Type :=
6849 (TSS_Stream_Input,
6850 TSS_Stream_Output,
6851 TSS_Stream_Read,
6852 TSS_Stream_Write);
6853 Stream_Op : Entity_Id;
6855 begin
6856 -- Primitive operations of tagged types are frozen when the dispatch
6857 -- table is constructed.
6859 if not Comes_From_Source (Typ) or else Is_Tagged_Type (Typ) then
6860 return;
6861 end if;
6863 for J in Names'Range loop
6864 Stream_Op := TSS (Typ, Names (J));
6866 if Present (Stream_Op)
6867 and then Is_Subprogram (Stream_Op)
6868 and then Nkind (Unit_Declaration_Node (Stream_Op)) =
6869 N_Subprogram_Declaration
6870 and then not Is_Frozen (Stream_Op)
6871 then
6872 Append_Freeze_Actions (Typ, Freeze_Entity (Stream_Op, N));
6873 end if;
6874 end loop;
6875 end Freeze_Stream_Operations;
6877 -----------------
6878 -- Freeze_Type --
6879 -----------------
6881 -- Full type declarations are expanded at the point at which the type is
6882 -- frozen. The formal N is the Freeze_Node for the type. Any statements or
6883 -- declarations generated by the freezing (e.g. the procedure generated
6884 -- for initialization) are chained in the Actions field list of the freeze
6885 -- node using Append_Freeze_Actions.
6887 function Freeze_Type (N : Node_Id) return Boolean is
6888 procedure Process_RACW_Types (Typ : Entity_Id);
6889 -- Validate and generate stubs for all RACW types associated with type
6890 -- Typ.
6892 procedure Process_Pending_Access_Types (Typ : Entity_Id);
6893 -- Associate type Typ's Finalize_Address primitive with the finalization
6894 -- masters of pending access-to-Typ types.
6896 ------------------------
6897 -- Process_RACW_Types --
6898 ------------------------
6900 procedure Process_RACW_Types (Typ : Entity_Id) is
6901 List : constant Elist_Id := Access_Types_To_Process (N);
6902 E : Elmt_Id;
6903 Seen : Boolean := False;
6905 begin
6906 if Present (List) then
6907 E := First_Elmt (List);
6908 while Present (E) loop
6909 if Is_Remote_Access_To_Class_Wide_Type (Node (E)) then
6910 Validate_RACW_Primitives (Node (E));
6911 Seen := True;
6912 end if;
6914 Next_Elmt (E);
6915 end loop;
6916 end if;
6918 -- If there are RACWs designating this type, make stubs now
6920 if Seen then
6921 Remote_Types_Tagged_Full_View_Encountered (Typ);
6922 end if;
6923 end Process_RACW_Types;
6925 ----------------------------------
6926 -- Process_Pending_Access_Types --
6927 ----------------------------------
6929 procedure Process_Pending_Access_Types (Typ : Entity_Id) is
6930 E : Elmt_Id;
6932 begin
6933 -- Finalize_Address is not generated in CodePeer mode because the
6934 -- body contains address arithmetic. This processing is disabled.
6936 if CodePeer_Mode then
6937 null;
6939 -- Certain itypes are generated for contexts that cannot allocate
6940 -- objects and should not set primitive Finalize_Address.
6942 elsif Is_Itype (Typ)
6943 and then Nkind (Associated_Node_For_Itype (Typ)) =
6944 N_Explicit_Dereference
6945 then
6946 null;
6948 -- When an access type is declared after the incomplete view of a
6949 -- Taft-amendment type, the access type is considered pending in
6950 -- case the full view of the Taft-amendment type is controlled. If
6951 -- this is indeed the case, associate the Finalize_Address routine
6952 -- of the full view with the finalization masters of all pending
6953 -- access types. This scenario applies to anonymous access types as
6954 -- well.
6956 elsif Needs_Finalization (Typ)
6957 and then Present (Pending_Access_Types (Typ))
6958 then
6959 E := First_Elmt (Pending_Access_Types (Typ));
6960 while Present (E) loop
6962 -- Generate:
6963 -- Set_Finalize_Address
6964 -- (Ptr_Typ, <Typ>FD'Unrestricted_Access);
6966 Append_Freeze_Action (Typ,
6967 Make_Set_Finalize_Address_Call
6968 (Loc => Sloc (N),
6969 Ptr_Typ => Node (E)));
6971 Next_Elmt (E);
6972 end loop;
6973 end if;
6974 end Process_Pending_Access_Types;
6976 -- Local variables
6978 Def_Id : constant Entity_Id := Entity (N);
6979 Result : Boolean := False;
6981 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
6983 -- Start of processing for Freeze_Type
6985 begin
6986 -- The type being frozen may be subject to pragma Ghost. Set the mode
6987 -- now to ensure that any nodes generated during freezing are properly
6988 -- marked as Ghost.
6990 Set_Ghost_Mode (N, Def_Id);
6992 -- Process any remote access-to-class-wide types designating the type
6993 -- being frozen.
6995 Process_RACW_Types (Def_Id);
6997 -- Freeze processing for record types
6999 if Is_Record_Type (Def_Id) then
7000 if Ekind (Def_Id) = E_Record_Type then
7001 Expand_Freeze_Record_Type (N);
7002 elsif Is_Class_Wide_Type (Def_Id) then
7003 Expand_Freeze_Class_Wide_Type (N);
7004 end if;
7006 -- Freeze processing for array types
7008 elsif Is_Array_Type (Def_Id) then
7009 Expand_Freeze_Array_Type (N);
7011 -- Freeze processing for access types
7013 -- For pool-specific access types, find out the pool object used for
7014 -- this type, needs actual expansion of it in some cases. Here are the
7015 -- different cases :
7017 -- 1. Rep Clause "for Def_Id'Storage_Size use 0;"
7018 -- ---> don't use any storage pool
7020 -- 2. Rep Clause : for Def_Id'Storage_Size use Expr.
7021 -- Expand:
7022 -- Def_Id__Pool : Stack_Bounded_Pool (Expr, DT'Size, DT'Alignment);
7024 -- 3. Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
7025 -- ---> Storage Pool is the specified one
7027 -- See GNAT Pool packages in the Run-Time for more details
7029 elsif Ekind_In (Def_Id, E_Access_Type, E_General_Access_Type) then
7030 declare
7031 Loc : constant Source_Ptr := Sloc (N);
7032 Desig_Type : constant Entity_Id := Designated_Type (Def_Id);
7034 Freeze_Action_Typ : Entity_Id;
7035 Pool_Object : Entity_Id;
7037 begin
7038 -- Case 1
7040 -- Rep Clause "for Def_Id'Storage_Size use 0;"
7041 -- ---> don't use any storage pool
7043 if No_Pool_Assigned (Def_Id) then
7044 null;
7046 -- Case 2
7048 -- Rep Clause : for Def_Id'Storage_Size use Expr.
7049 -- ---> Expand:
7050 -- Def_Id__Pool : Stack_Bounded_Pool
7051 -- (Expr, DT'Size, DT'Alignment);
7053 elsif Has_Storage_Size_Clause (Def_Id) then
7054 declare
7055 DT_Align : Node_Id;
7056 DT_Size : Node_Id;
7058 begin
7059 -- For unconstrained composite types we give a size of zero
7060 -- so that the pool knows that it needs a special algorithm
7061 -- for variable size object allocation.
7063 if Is_Composite_Type (Desig_Type)
7064 and then not Is_Constrained (Desig_Type)
7065 then
7066 DT_Size := Make_Integer_Literal (Loc, 0);
7067 DT_Align := Make_Integer_Literal (Loc, Maximum_Alignment);
7069 else
7070 DT_Size :=
7071 Make_Attribute_Reference (Loc,
7072 Prefix => New_Occurrence_Of (Desig_Type, Loc),
7073 Attribute_Name => Name_Max_Size_In_Storage_Elements);
7075 DT_Align :=
7076 Make_Attribute_Reference (Loc,
7077 Prefix => New_Occurrence_Of (Desig_Type, Loc),
7078 Attribute_Name => Name_Alignment);
7079 end if;
7081 Pool_Object :=
7082 Make_Defining_Identifier (Loc,
7083 Chars => New_External_Name (Chars (Def_Id), 'P'));
7085 -- We put the code associated with the pools in the entity
7086 -- that has the later freeze node, usually the access type
7087 -- but it can also be the designated_type; because the pool
7088 -- code requires both those types to be frozen
7090 if Is_Frozen (Desig_Type)
7091 and then (No (Freeze_Node (Desig_Type))
7092 or else Analyzed (Freeze_Node (Desig_Type)))
7093 then
7094 Freeze_Action_Typ := Def_Id;
7096 -- A Taft amendment type cannot get the freeze actions
7097 -- since the full view is not there.
7099 elsif Is_Incomplete_Or_Private_Type (Desig_Type)
7100 and then No (Full_View (Desig_Type))
7101 then
7102 Freeze_Action_Typ := Def_Id;
7104 else
7105 Freeze_Action_Typ := Desig_Type;
7106 end if;
7108 Append_Freeze_Action (Freeze_Action_Typ,
7109 Make_Object_Declaration (Loc,
7110 Defining_Identifier => Pool_Object,
7111 Object_Definition =>
7112 Make_Subtype_Indication (Loc,
7113 Subtype_Mark =>
7114 New_Occurrence_Of
7115 (RTE (RE_Stack_Bounded_Pool), Loc),
7117 Constraint =>
7118 Make_Index_Or_Discriminant_Constraint (Loc,
7119 Constraints => New_List (
7121 -- First discriminant is the Pool Size
7123 New_Occurrence_Of (
7124 Storage_Size_Variable (Def_Id), Loc),
7126 -- Second discriminant is the element size
7128 DT_Size,
7130 -- Third discriminant is the alignment
7132 DT_Align)))));
7133 end;
7135 Set_Associated_Storage_Pool (Def_Id, Pool_Object);
7137 -- Case 3
7139 -- Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
7140 -- ---> Storage Pool is the specified one
7142 -- When compiling in Ada 2012 mode, ensure that the accessibility
7143 -- level of the subpool access type is not deeper than that of the
7144 -- pool_with_subpools.
7146 elsif Ada_Version >= Ada_2012
7147 and then Present (Associated_Storage_Pool (Def_Id))
7149 -- Omit this check for the case of a configurable run-time that
7150 -- does not provide package System.Storage_Pools.Subpools.
7152 and then RTE_Available (RE_Root_Storage_Pool_With_Subpools)
7153 then
7154 declare
7155 Loc : constant Source_Ptr := Sloc (Def_Id);
7156 Pool : constant Entity_Id :=
7157 Associated_Storage_Pool (Def_Id);
7158 RSPWS : constant Entity_Id :=
7159 RTE (RE_Root_Storage_Pool_With_Subpools);
7161 begin
7162 -- It is known that the accessibility level of the access
7163 -- type is deeper than that of the pool.
7165 if Type_Access_Level (Def_Id) > Object_Access_Level (Pool)
7166 and then not Accessibility_Checks_Suppressed (Def_Id)
7167 and then not Accessibility_Checks_Suppressed (Pool)
7168 then
7169 -- Static case: the pool is known to be a descendant of
7170 -- Root_Storage_Pool_With_Subpools.
7172 if Is_Ancestor (RSPWS, Etype (Pool)) then
7173 Error_Msg_N
7174 ("??subpool access type has deeper accessibility "
7175 & "level than pool", Def_Id);
7177 Append_Freeze_Action (Def_Id,
7178 Make_Raise_Program_Error (Loc,
7179 Reason => PE_Accessibility_Check_Failed));
7181 -- Dynamic case: when the pool is of a class-wide type,
7182 -- it may or may not support subpools depending on the
7183 -- path of derivation. Generate:
7185 -- if Def_Id in RSPWS'Class then
7186 -- raise Program_Error;
7187 -- end if;
7189 elsif Is_Class_Wide_Type (Etype (Pool)) then
7190 Append_Freeze_Action (Def_Id,
7191 Make_If_Statement (Loc,
7192 Condition =>
7193 Make_In (Loc,
7194 Left_Opnd => New_Occurrence_Of (Pool, Loc),
7195 Right_Opnd =>
7196 New_Occurrence_Of
7197 (Class_Wide_Type (RSPWS), Loc)),
7199 Then_Statements => New_List (
7200 Make_Raise_Program_Error (Loc,
7201 Reason => PE_Accessibility_Check_Failed))));
7202 end if;
7203 end if;
7204 end;
7205 end if;
7207 -- For access-to-controlled types (including class-wide types and
7208 -- Taft-amendment types, which potentially have controlled
7209 -- components), expand the list controller object that will store
7210 -- the dynamically allocated objects. Don't do this transformation
7211 -- for expander-generated access types, but do it for types that
7212 -- are the full view of types derived from other private types.
7213 -- Also suppress the list controller in the case of a designated
7214 -- type with convention Java, since this is used when binding to
7215 -- Java API specs, where there's no equivalent of a finalization
7216 -- list and we don't want to pull in the finalization support if
7217 -- not needed.
7219 if not Comes_From_Source (Def_Id)
7220 and then not Has_Private_Declaration (Def_Id)
7221 then
7222 null;
7224 -- An exception is made for types defined in the run-time because
7225 -- Ada.Tags.Tag itself is such a type and cannot afford this
7226 -- unnecessary overhead that would generates a loop in the
7227 -- expansion scheme. Another exception is if Restrictions
7228 -- (No_Finalization) is active, since then we know nothing is
7229 -- controlled.
7231 elsif Restriction_Active (No_Finalization)
7232 or else In_Runtime (Def_Id)
7233 then
7234 null;
7236 -- Create a finalization master for an access-to-controlled type
7237 -- or an access-to-incomplete type. It is assumed that the full
7238 -- view will be controlled.
7240 elsif Needs_Finalization (Desig_Type)
7241 or else (Is_Incomplete_Type (Desig_Type)
7242 and then No (Full_View (Desig_Type)))
7243 then
7244 Build_Finalization_Master (Def_Id);
7246 -- Create a finalization master when the designated type contains
7247 -- a private component. It is assumed that the full view will be
7248 -- controlled.
7250 elsif Has_Private_Component (Desig_Type) then
7251 Build_Finalization_Master
7252 (Typ => Def_Id,
7253 For_Private => True,
7254 Context_Scope => Scope (Def_Id),
7255 Insertion_Node => Declaration_Node (Desig_Type));
7256 end if;
7257 end;
7259 -- Freeze processing for enumeration types
7261 elsif Ekind (Def_Id) = E_Enumeration_Type then
7263 -- We only have something to do if we have a non-standard
7264 -- representation (i.e. at least one literal whose pos value
7265 -- is not the same as its representation)
7267 if Has_Non_Standard_Rep (Def_Id) then
7268 Expand_Freeze_Enumeration_Type (N);
7269 end if;
7271 -- Private types that are completed by a derivation from a private
7272 -- type have an internally generated full view, that needs to be
7273 -- frozen. This must be done explicitly because the two views share
7274 -- the freeze node, and the underlying full view is not visible when
7275 -- the freeze node is analyzed.
7277 elsif Is_Private_Type (Def_Id)
7278 and then Is_Derived_Type (Def_Id)
7279 and then Present (Full_View (Def_Id))
7280 and then Is_Itype (Full_View (Def_Id))
7281 and then Has_Private_Declaration (Full_View (Def_Id))
7282 and then Freeze_Node (Full_View (Def_Id)) = N
7283 then
7284 Set_Entity (N, Full_View (Def_Id));
7285 Result := Freeze_Type (N);
7286 Set_Entity (N, Def_Id);
7288 -- All other types require no expander action. There are such cases
7289 -- (e.g. task types and protected types). In such cases, the freeze
7290 -- nodes are there for use by Gigi.
7292 end if;
7294 -- Complete the initialization of all pending access types' finalization
7295 -- masters now that the designated type has been is frozen and primitive
7296 -- Finalize_Address generated.
7298 Process_Pending_Access_Types (Def_Id);
7299 Freeze_Stream_Operations (N, Def_Id);
7301 -- Generate the [spec and] body of the invariant procedure tasked with
7302 -- the runtime verification of all invariants that pertain to the type.
7303 -- This includes invariants on the partial and full view, inherited
7304 -- class-wide invariants from parent types or interfaces, and invariants
7305 -- on array elements or record components.
7307 if Has_Invariants (Def_Id) then
7308 Build_Invariant_Procedure_Body (Def_Id);
7309 end if;
7311 Ghost_Mode := Save_Ghost_Mode;
7312 return Result;
7314 exception
7315 when RE_Not_Available =>
7316 Ghost_Mode := Save_Ghost_Mode;
7317 return False;
7318 end Freeze_Type;
7320 -------------------------
7321 -- Get_Simple_Init_Val --
7322 -------------------------
7324 function Get_Simple_Init_Val
7325 (T : Entity_Id;
7326 N : Node_Id;
7327 Size : Uint := No_Uint) return Node_Id
7329 Loc : constant Source_Ptr := Sloc (N);
7330 Val : Node_Id;
7331 Result : Node_Id;
7332 Val_RE : RE_Id;
7334 Size_To_Use : Uint;
7335 -- This is the size to be used for computation of the appropriate
7336 -- initial value for the Normalize_Scalars and Initialize_Scalars case.
7338 IV_Attribute : constant Boolean :=
7339 Nkind (N) = N_Attribute_Reference
7340 and then Attribute_Name (N) = Name_Invalid_Value;
7342 Lo_Bound : Uint;
7343 Hi_Bound : Uint;
7344 -- These are the values computed by the procedure Check_Subtype_Bounds
7346 procedure Check_Subtype_Bounds;
7347 -- This procedure examines the subtype T, and its ancestor subtypes and
7348 -- derived types to determine the best known information about the
7349 -- bounds of the subtype. After the call Lo_Bound is set either to
7350 -- No_Uint if no information can be determined, or to a value which
7351 -- represents a known low bound, i.e. a valid value of the subtype can
7352 -- not be less than this value. Hi_Bound is similarly set to a known
7353 -- high bound (valid value cannot be greater than this).
7355 --------------------------
7356 -- Check_Subtype_Bounds --
7357 --------------------------
7359 procedure Check_Subtype_Bounds is
7360 ST1 : Entity_Id;
7361 ST2 : Entity_Id;
7362 Lo : Node_Id;
7363 Hi : Node_Id;
7364 Loval : Uint;
7365 Hival : Uint;
7367 begin
7368 Lo_Bound := No_Uint;
7369 Hi_Bound := No_Uint;
7371 -- Loop to climb ancestor subtypes and derived types
7373 ST1 := T;
7374 loop
7375 if not Is_Discrete_Type (ST1) then
7376 return;
7377 end if;
7379 Lo := Type_Low_Bound (ST1);
7380 Hi := Type_High_Bound (ST1);
7382 if Compile_Time_Known_Value (Lo) then
7383 Loval := Expr_Value (Lo);
7385 if Lo_Bound = No_Uint or else Lo_Bound < Loval then
7386 Lo_Bound := Loval;
7387 end if;
7388 end if;
7390 if Compile_Time_Known_Value (Hi) then
7391 Hival := Expr_Value (Hi);
7393 if Hi_Bound = No_Uint or else Hi_Bound > Hival then
7394 Hi_Bound := Hival;
7395 end if;
7396 end if;
7398 ST2 := Ancestor_Subtype (ST1);
7400 if No (ST2) then
7401 ST2 := Etype (ST1);
7402 end if;
7404 exit when ST1 = ST2;
7405 ST1 := ST2;
7406 end loop;
7407 end Check_Subtype_Bounds;
7409 -- Start of processing for Get_Simple_Init_Val
7411 begin
7412 -- For a private type, we should always have an underlying type (because
7413 -- this was already checked in Needs_Simple_Initialization). What we do
7414 -- is to get the value for the underlying type and then do an unchecked
7415 -- conversion to the private type.
7417 if Is_Private_Type (T) then
7418 Val := Get_Simple_Init_Val (Underlying_Type (T), N, Size);
7420 -- A special case, if the underlying value is null, then qualify it
7421 -- with the underlying type, so that the null is properly typed.
7422 -- Similarly, if it is an aggregate it must be qualified, because an
7423 -- unchecked conversion does not provide a context for it.
7425 if Nkind_In (Val, N_Null, N_Aggregate) then
7426 Val :=
7427 Make_Qualified_Expression (Loc,
7428 Subtype_Mark =>
7429 New_Occurrence_Of (Underlying_Type (T), Loc),
7430 Expression => Val);
7431 end if;
7433 Result := Unchecked_Convert_To (T, Val);
7435 -- Don't truncate result (important for Initialize/Normalize_Scalars)
7437 if Nkind (Result) = N_Unchecked_Type_Conversion
7438 and then Is_Scalar_Type (Underlying_Type (T))
7439 then
7440 Set_No_Truncation (Result);
7441 end if;
7443 return Result;
7445 -- Scalars with Default_Value aspect. The first subtype may now be
7446 -- private, so retrieve value from underlying type.
7448 elsif Is_Scalar_Type (T) and then Has_Default_Aspect (T) then
7449 if Is_Private_Type (First_Subtype (T)) then
7450 return Unchecked_Convert_To (T,
7451 Default_Aspect_Value (Full_View (First_Subtype (T))));
7452 else
7453 return
7454 Convert_To (T, Default_Aspect_Value (First_Subtype (T)));
7455 end if;
7457 -- Otherwise, for scalars, we must have normalize/initialize scalars
7458 -- case, or if the node N is an 'Invalid_Value attribute node.
7460 elsif Is_Scalar_Type (T) then
7461 pragma Assert (Init_Or_Norm_Scalars or IV_Attribute);
7463 -- Compute size of object. If it is given by the caller, we can use
7464 -- it directly, otherwise we use Esize (T) as an estimate. As far as
7465 -- we know this covers all cases correctly.
7467 if Size = No_Uint or else Size <= Uint_0 then
7468 Size_To_Use := UI_Max (Uint_1, Esize (T));
7469 else
7470 Size_To_Use := Size;
7471 end if;
7473 -- Maximum size to use is 64 bits, since we will create values of
7474 -- type Unsigned_64 and the range must fit this type.
7476 if Size_To_Use /= No_Uint and then Size_To_Use > Uint_64 then
7477 Size_To_Use := Uint_64;
7478 end if;
7480 -- Check known bounds of subtype
7482 Check_Subtype_Bounds;
7484 -- Processing for Normalize_Scalars case
7486 if Normalize_Scalars and then not IV_Attribute then
7488 -- If zero is invalid, it is a convenient value to use that is
7489 -- for sure an appropriate invalid value in all situations.
7491 if Lo_Bound /= No_Uint and then Lo_Bound > Uint_0 then
7492 Val := Make_Integer_Literal (Loc, 0);
7494 -- Cases where all one bits is the appropriate invalid value
7496 -- For modular types, all 1 bits is either invalid or valid. If
7497 -- it is valid, then there is nothing that can be done since there
7498 -- are no invalid values (we ruled out zero already).
7500 -- For signed integer types that have no negative values, either
7501 -- there is room for negative values, or there is not. If there
7502 -- is, then all 1-bits may be interpreted as minus one, which is
7503 -- certainly invalid. Alternatively it is treated as the largest
7504 -- positive value, in which case the observation for modular types
7505 -- still applies.
7507 -- For float types, all 1-bits is a NaN (not a number), which is
7508 -- certainly an appropriately invalid value.
7510 elsif Is_Unsigned_Type (T)
7511 or else Is_Floating_Point_Type (T)
7512 or else Is_Enumeration_Type (T)
7513 then
7514 Val := Make_Integer_Literal (Loc, 2 ** Size_To_Use - 1);
7516 -- Resolve as Unsigned_64, because the largest number we can
7517 -- generate is out of range of universal integer.
7519 Analyze_And_Resolve (Val, RTE (RE_Unsigned_64));
7521 -- Case of signed types
7523 else
7524 declare
7525 Signed_Size : constant Uint :=
7526 UI_Min (Uint_63, Size_To_Use - 1);
7528 begin
7529 -- Normally we like to use the most negative number. The one
7530 -- exception is when this number is in the known subtype
7531 -- range and the largest positive number is not in the known
7532 -- subtype range.
7534 -- For this exceptional case, use largest positive value
7536 if Lo_Bound /= No_Uint and then Hi_Bound /= No_Uint
7537 and then Lo_Bound <= (-(2 ** Signed_Size))
7538 and then Hi_Bound < 2 ** Signed_Size
7539 then
7540 Val := Make_Integer_Literal (Loc, 2 ** Signed_Size - 1);
7542 -- Normal case of largest negative value
7544 else
7545 Val := Make_Integer_Literal (Loc, -(2 ** Signed_Size));
7546 end if;
7547 end;
7548 end if;
7550 -- Here for Initialize_Scalars case (or Invalid_Value attribute used)
7552 else
7553 -- For float types, use float values from System.Scalar_Values
7555 if Is_Floating_Point_Type (T) then
7556 if Root_Type (T) = Standard_Short_Float then
7557 Val_RE := RE_IS_Isf;
7558 elsif Root_Type (T) = Standard_Float then
7559 Val_RE := RE_IS_Ifl;
7560 elsif Root_Type (T) = Standard_Long_Float then
7561 Val_RE := RE_IS_Ilf;
7562 else pragma Assert (Root_Type (T) = Standard_Long_Long_Float);
7563 Val_RE := RE_IS_Ill;
7564 end if;
7566 -- If zero is invalid, use zero values from System.Scalar_Values
7568 elsif Lo_Bound /= No_Uint and then Lo_Bound > Uint_0 then
7569 if Size_To_Use <= 8 then
7570 Val_RE := RE_IS_Iz1;
7571 elsif Size_To_Use <= 16 then
7572 Val_RE := RE_IS_Iz2;
7573 elsif Size_To_Use <= 32 then
7574 Val_RE := RE_IS_Iz4;
7575 else
7576 Val_RE := RE_IS_Iz8;
7577 end if;
7579 -- For unsigned, use unsigned values from System.Scalar_Values
7581 elsif Is_Unsigned_Type (T) then
7582 if Size_To_Use <= 8 then
7583 Val_RE := RE_IS_Iu1;
7584 elsif Size_To_Use <= 16 then
7585 Val_RE := RE_IS_Iu2;
7586 elsif Size_To_Use <= 32 then
7587 Val_RE := RE_IS_Iu4;
7588 else
7589 Val_RE := RE_IS_Iu8;
7590 end if;
7592 -- For signed, use signed values from System.Scalar_Values
7594 else
7595 if Size_To_Use <= 8 then
7596 Val_RE := RE_IS_Is1;
7597 elsif Size_To_Use <= 16 then
7598 Val_RE := RE_IS_Is2;
7599 elsif Size_To_Use <= 32 then
7600 Val_RE := RE_IS_Is4;
7601 else
7602 Val_RE := RE_IS_Is8;
7603 end if;
7604 end if;
7606 Val := New_Occurrence_Of (RTE (Val_RE), Loc);
7607 end if;
7609 -- The final expression is obtained by doing an unchecked conversion
7610 -- of this result to the base type of the required subtype. Use the
7611 -- base type to prevent the unchecked conversion from chopping bits,
7612 -- and then we set Kill_Range_Check to preserve the "bad" value.
7614 Result := Unchecked_Convert_To (Base_Type (T), Val);
7616 -- Ensure result is not truncated, since we want the "bad" bits, and
7617 -- also kill range check on result.
7619 if Nkind (Result) = N_Unchecked_Type_Conversion then
7620 Set_No_Truncation (Result);
7621 Set_Kill_Range_Check (Result, True);
7622 end if;
7624 return Result;
7626 -- String or Wide_[Wide]_String (must have Initialize_Scalars set)
7628 elsif Is_Standard_String_Type (T) then
7629 pragma Assert (Init_Or_Norm_Scalars);
7631 return
7632 Make_Aggregate (Loc,
7633 Component_Associations => New_List (
7634 Make_Component_Association (Loc,
7635 Choices => New_List (
7636 Make_Others_Choice (Loc)),
7637 Expression =>
7638 Get_Simple_Init_Val
7639 (Component_Type (T), N, Esize (Root_Type (T))))));
7641 -- Access type is initialized to null
7643 elsif Is_Access_Type (T) then
7644 return Make_Null (Loc);
7646 -- No other possibilities should arise, since we should only be calling
7647 -- Get_Simple_Init_Val if Needs_Simple_Initialization returned True,
7648 -- indicating one of the above cases held.
7650 else
7651 raise Program_Error;
7652 end if;
7654 exception
7655 when RE_Not_Available =>
7656 return Empty;
7657 end Get_Simple_Init_Val;
7659 ------------------------------
7660 -- Has_New_Non_Standard_Rep --
7661 ------------------------------
7663 function Has_New_Non_Standard_Rep (T : Entity_Id) return Boolean is
7664 begin
7665 if not Is_Derived_Type (T) then
7666 return Has_Non_Standard_Rep (T)
7667 or else Has_Non_Standard_Rep (Root_Type (T));
7669 -- If Has_Non_Standard_Rep is not set on the derived type, the
7670 -- representation is fully inherited.
7672 elsif not Has_Non_Standard_Rep (T) then
7673 return False;
7675 else
7676 return First_Rep_Item (T) /= First_Rep_Item (Root_Type (T));
7678 -- May need a more precise check here: the First_Rep_Item may be a
7679 -- stream attribute, which does not affect the representation of the
7680 -- type ???
7682 end if;
7683 end Has_New_Non_Standard_Rep;
7685 ----------------------
7686 -- Inline_Init_Proc --
7687 ----------------------
7689 function Inline_Init_Proc (Typ : Entity_Id) return Boolean is
7690 begin
7691 -- The initialization proc of protected records is not worth inlining.
7692 -- In addition, when compiled for another unit for inlining purposes,
7693 -- it may make reference to entities that have not been elaborated yet.
7694 -- The initialization proc of records that need finalization contains
7695 -- a nested clean-up procedure that makes it impractical to inline as
7696 -- well, except for simple controlled types themselves. And similar
7697 -- considerations apply to task types.
7699 if Is_Concurrent_Type (Typ) then
7700 return False;
7702 elsif Needs_Finalization (Typ) and then not Is_Controlled (Typ) then
7703 return False;
7705 elsif Has_Task (Typ) then
7706 return False;
7708 else
7709 return True;
7710 end if;
7711 end Inline_Init_Proc;
7713 ----------------
7714 -- In_Runtime --
7715 ----------------
7717 function In_Runtime (E : Entity_Id) return Boolean is
7718 S1 : Entity_Id;
7720 begin
7721 S1 := Scope (E);
7722 while Scope (S1) /= Standard_Standard loop
7723 S1 := Scope (S1);
7724 end loop;
7726 return Is_RTU (S1, System) or else Is_RTU (S1, Ada);
7727 end In_Runtime;
7729 ----------------------------
7730 -- Initialization_Warning --
7731 ----------------------------
7733 procedure Initialization_Warning (E : Entity_Id) is
7734 Warning_Needed : Boolean;
7736 begin
7737 Warning_Needed := False;
7739 if Ekind (Current_Scope) = E_Package
7740 and then Static_Elaboration_Desired (Current_Scope)
7741 then
7742 if Is_Type (E) then
7743 if Is_Record_Type (E) then
7744 if Has_Discriminants (E)
7745 or else Is_Limited_Type (E)
7746 or else Has_Non_Standard_Rep (E)
7747 then
7748 Warning_Needed := True;
7750 else
7751 -- Verify that at least one component has an initialization
7752 -- expression. No need for a warning on a type if all its
7753 -- components have no initialization.
7755 declare
7756 Comp : Entity_Id;
7758 begin
7759 Comp := First_Component (E);
7760 while Present (Comp) loop
7761 if Ekind (Comp) = E_Discriminant
7762 or else
7763 (Nkind (Parent (Comp)) = N_Component_Declaration
7764 and then Present (Expression (Parent (Comp))))
7765 then
7766 Warning_Needed := True;
7767 exit;
7768 end if;
7770 Next_Component (Comp);
7771 end loop;
7772 end;
7773 end if;
7775 if Warning_Needed then
7776 Error_Msg_N
7777 ("Objects of the type cannot be initialized statically "
7778 & "by default??", Parent (E));
7779 end if;
7780 end if;
7782 else
7783 Error_Msg_N ("Object cannot be initialized statically??", E);
7784 end if;
7785 end if;
7786 end Initialization_Warning;
7788 ------------------
7789 -- Init_Formals --
7790 ------------------
7792 function Init_Formals (Typ : Entity_Id) return List_Id is
7793 Loc : constant Source_Ptr := Sloc (Typ);
7794 Formals : List_Id;
7796 begin
7797 -- First parameter is always _Init : in out typ. Note that we need this
7798 -- to be in/out because in the case of the task record value, there
7799 -- are default record fields (_Priority, _Size, -Task_Info) that may
7800 -- be referenced in the generated initialization routine.
7802 Formals := New_List (
7803 Make_Parameter_Specification (Loc,
7804 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uInit),
7805 In_Present => True,
7806 Out_Present => True,
7807 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
7809 -- For task record value, or type that contains tasks, add two more
7810 -- formals, _Master : Master_Id and _Chain : in out Activation_Chain
7811 -- We also add these parameters for the task record type case.
7813 if Has_Task (Typ)
7814 or else (Is_Record_Type (Typ) and then Is_Task_Record_Type (Typ))
7815 then
7816 Append_To (Formals,
7817 Make_Parameter_Specification (Loc,
7818 Defining_Identifier =>
7819 Make_Defining_Identifier (Loc, Name_uMaster),
7820 Parameter_Type =>
7821 New_Occurrence_Of (RTE (RE_Master_Id), Loc)));
7823 -- Add _Chain (not done for sequential elaboration policy, see
7824 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
7826 if Partition_Elaboration_Policy /= 'S' then
7827 Append_To (Formals,
7828 Make_Parameter_Specification (Loc,
7829 Defining_Identifier =>
7830 Make_Defining_Identifier (Loc, Name_uChain),
7831 In_Present => True,
7832 Out_Present => True,
7833 Parameter_Type =>
7834 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc)));
7835 end if;
7837 Append_To (Formals,
7838 Make_Parameter_Specification (Loc,
7839 Defining_Identifier =>
7840 Make_Defining_Identifier (Loc, Name_uTask_Name),
7841 In_Present => True,
7842 Parameter_Type => New_Occurrence_Of (Standard_String, Loc)));
7843 end if;
7845 return Formals;
7847 exception
7848 when RE_Not_Available =>
7849 return Empty_List;
7850 end Init_Formals;
7852 -------------------------
7853 -- Init_Secondary_Tags --
7854 -------------------------
7856 procedure Init_Secondary_Tags
7857 (Typ : Entity_Id;
7858 Target : Node_Id;
7859 Stmts_List : List_Id;
7860 Fixed_Comps : Boolean := True;
7861 Variable_Comps : Boolean := True)
7863 Loc : constant Source_Ptr := Sloc (Target);
7865 -- Inherit the C++ tag of the secondary dispatch table of Typ associated
7866 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
7868 procedure Initialize_Tag
7869 (Typ : Entity_Id;
7870 Iface : Entity_Id;
7871 Tag_Comp : Entity_Id;
7872 Iface_Tag : Node_Id);
7873 -- Initialize the tag of the secondary dispatch table of Typ associated
7874 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
7875 -- Compiling under the CPP full ABI compatibility mode, if the ancestor
7876 -- of Typ CPP tagged type we generate code to inherit the contents of
7877 -- the dispatch table directly from the ancestor.
7879 --------------------
7880 -- Initialize_Tag --
7881 --------------------
7883 procedure Initialize_Tag
7884 (Typ : Entity_Id;
7885 Iface : Entity_Id;
7886 Tag_Comp : Entity_Id;
7887 Iface_Tag : Node_Id)
7889 Comp_Typ : Entity_Id;
7890 Offset_To_Top_Comp : Entity_Id := Empty;
7892 begin
7893 -- Initialize pointer to secondary DT associated with the interface
7895 if not Is_Ancestor (Iface, Typ, Use_Full_View => True) then
7896 Append_To (Stmts_List,
7897 Make_Assignment_Statement (Loc,
7898 Name =>
7899 Make_Selected_Component (Loc,
7900 Prefix => New_Copy_Tree (Target),
7901 Selector_Name => New_Occurrence_Of (Tag_Comp, Loc)),
7902 Expression =>
7903 New_Occurrence_Of (Iface_Tag, Loc)));
7904 end if;
7906 Comp_Typ := Scope (Tag_Comp);
7908 -- Initialize the entries of the table of interfaces. We generate a
7909 -- different call when the parent of the type has variable size
7910 -- components.
7912 if Comp_Typ /= Etype (Comp_Typ)
7913 and then Is_Variable_Size_Record (Etype (Comp_Typ))
7914 and then Chars (Tag_Comp) /= Name_uTag
7915 then
7916 pragma Assert (Present (DT_Offset_To_Top_Func (Tag_Comp)));
7918 -- Issue error if Set_Dynamic_Offset_To_Top is not available in a
7919 -- configurable run-time environment.
7921 if not RTE_Available (RE_Set_Dynamic_Offset_To_Top) then
7922 Error_Msg_CRT
7923 ("variable size record with interface types", Typ);
7924 return;
7925 end if;
7927 -- Generate:
7928 -- Set_Dynamic_Offset_To_Top
7929 -- (This => Init,
7930 -- Interface_T => Iface'Tag,
7931 -- Offset_Value => n,
7932 -- Offset_Func => Fn'Address)
7934 Append_To (Stmts_List,
7935 Make_Procedure_Call_Statement (Loc,
7936 Name =>
7937 New_Occurrence_Of (RTE (RE_Set_Dynamic_Offset_To_Top), Loc),
7938 Parameter_Associations => New_List (
7939 Make_Attribute_Reference (Loc,
7940 Prefix => New_Copy_Tree (Target),
7941 Attribute_Name => Name_Address),
7943 Unchecked_Convert_To (RTE (RE_Tag),
7944 New_Occurrence_Of
7945 (Node (First_Elmt (Access_Disp_Table (Iface))),
7946 Loc)),
7948 Unchecked_Convert_To
7949 (RTE (RE_Storage_Offset),
7950 Make_Attribute_Reference (Loc,
7951 Prefix =>
7952 Make_Selected_Component (Loc,
7953 Prefix => New_Copy_Tree (Target),
7954 Selector_Name =>
7955 New_Occurrence_Of (Tag_Comp, Loc)),
7956 Attribute_Name => Name_Position)),
7958 Unchecked_Convert_To (RTE (RE_Offset_To_Top_Function_Ptr),
7959 Make_Attribute_Reference (Loc,
7960 Prefix => New_Occurrence_Of
7961 (DT_Offset_To_Top_Func (Tag_Comp), Loc),
7962 Attribute_Name => Name_Address)))));
7964 -- In this case the next component stores the value of the offset
7965 -- to the top.
7967 Offset_To_Top_Comp := Next_Entity (Tag_Comp);
7968 pragma Assert (Present (Offset_To_Top_Comp));
7970 Append_To (Stmts_List,
7971 Make_Assignment_Statement (Loc,
7972 Name =>
7973 Make_Selected_Component (Loc,
7974 Prefix => New_Copy_Tree (Target),
7975 Selector_Name =>
7976 New_Occurrence_Of (Offset_To_Top_Comp, Loc)),
7978 Expression =>
7979 Make_Attribute_Reference (Loc,
7980 Prefix =>
7981 Make_Selected_Component (Loc,
7982 Prefix => New_Copy_Tree (Target),
7983 Selector_Name => New_Occurrence_Of (Tag_Comp, Loc)),
7984 Attribute_Name => Name_Position)));
7986 -- Normal case: No discriminants in the parent type
7988 else
7989 -- Don't need to set any value if this interface shares the
7990 -- primary dispatch table.
7992 if not Is_Ancestor (Iface, Typ, Use_Full_View => True) then
7993 Append_To (Stmts_List,
7994 Build_Set_Static_Offset_To_Top (Loc,
7995 Iface_Tag => New_Occurrence_Of (Iface_Tag, Loc),
7996 Offset_Value =>
7997 Unchecked_Convert_To (RTE (RE_Storage_Offset),
7998 Make_Attribute_Reference (Loc,
7999 Prefix =>
8000 Make_Selected_Component (Loc,
8001 Prefix => New_Copy_Tree (Target),
8002 Selector_Name =>
8003 New_Occurrence_Of (Tag_Comp, Loc)),
8004 Attribute_Name => Name_Position))));
8005 end if;
8007 -- Generate:
8008 -- Register_Interface_Offset
8009 -- (This => Init,
8010 -- Interface_T => Iface'Tag,
8011 -- Is_Constant => True,
8012 -- Offset_Value => n,
8013 -- Offset_Func => null);
8015 if RTE_Available (RE_Register_Interface_Offset) then
8016 Append_To (Stmts_List,
8017 Make_Procedure_Call_Statement (Loc,
8018 Name =>
8019 New_Occurrence_Of
8020 (RTE (RE_Register_Interface_Offset), Loc),
8021 Parameter_Associations => New_List (
8022 Make_Attribute_Reference (Loc,
8023 Prefix => New_Copy_Tree (Target),
8024 Attribute_Name => Name_Address),
8026 Unchecked_Convert_To (RTE (RE_Tag),
8027 New_Occurrence_Of
8028 (Node (First_Elmt (Access_Disp_Table (Iface))), Loc)),
8030 New_Occurrence_Of (Standard_True, Loc),
8032 Unchecked_Convert_To (RTE (RE_Storage_Offset),
8033 Make_Attribute_Reference (Loc,
8034 Prefix =>
8035 Make_Selected_Component (Loc,
8036 Prefix => New_Copy_Tree (Target),
8037 Selector_Name =>
8038 New_Occurrence_Of (Tag_Comp, Loc)),
8039 Attribute_Name => Name_Position)),
8041 Make_Null (Loc))));
8042 end if;
8043 end if;
8044 end Initialize_Tag;
8046 -- Local variables
8048 Full_Typ : Entity_Id;
8049 Ifaces_List : Elist_Id;
8050 Ifaces_Comp_List : Elist_Id;
8051 Ifaces_Tag_List : Elist_Id;
8052 Iface_Elmt : Elmt_Id;
8053 Iface_Comp_Elmt : Elmt_Id;
8054 Iface_Tag_Elmt : Elmt_Id;
8055 Tag_Comp : Node_Id;
8056 In_Variable_Pos : Boolean;
8058 -- Start of processing for Init_Secondary_Tags
8060 begin
8061 -- Handle private types
8063 if Present (Full_View (Typ)) then
8064 Full_Typ := Full_View (Typ);
8065 else
8066 Full_Typ := Typ;
8067 end if;
8069 Collect_Interfaces_Info
8070 (Full_Typ, Ifaces_List, Ifaces_Comp_List, Ifaces_Tag_List);
8072 Iface_Elmt := First_Elmt (Ifaces_List);
8073 Iface_Comp_Elmt := First_Elmt (Ifaces_Comp_List);
8074 Iface_Tag_Elmt := First_Elmt (Ifaces_Tag_List);
8075 while Present (Iface_Elmt) loop
8076 Tag_Comp := Node (Iface_Comp_Elmt);
8078 -- Check if parent of record type has variable size components
8080 In_Variable_Pos := Scope (Tag_Comp) /= Etype (Scope (Tag_Comp))
8081 and then Is_Variable_Size_Record (Etype (Scope (Tag_Comp)));
8083 -- If we are compiling under the CPP full ABI compatibility mode and
8084 -- the ancestor is a CPP_Pragma tagged type then we generate code to
8085 -- initialize the secondary tag components from tags that reference
8086 -- secondary tables filled with copy of parent slots.
8088 if Is_CPP_Class (Root_Type (Full_Typ)) then
8090 -- Reject interface components located at variable offset in
8091 -- C++ derivations. This is currently unsupported.
8093 if not Fixed_Comps and then In_Variable_Pos then
8095 -- Locate the first dynamic component of the record. Done to
8096 -- improve the text of the warning.
8098 declare
8099 Comp : Entity_Id;
8100 Comp_Typ : Entity_Id;
8102 begin
8103 Comp := First_Entity (Typ);
8104 while Present (Comp) loop
8105 Comp_Typ := Etype (Comp);
8107 if Ekind (Comp) /= E_Discriminant
8108 and then not Is_Tag (Comp)
8109 then
8110 exit when
8111 (Is_Record_Type (Comp_Typ)
8112 and then
8113 Is_Variable_Size_Record (Base_Type (Comp_Typ)))
8114 or else
8115 (Is_Array_Type (Comp_Typ)
8116 and then Is_Variable_Size_Array (Comp_Typ));
8117 end if;
8119 Next_Entity (Comp);
8120 end loop;
8122 pragma Assert (Present (Comp));
8123 Error_Msg_Node_2 := Comp;
8124 Error_Msg_NE
8125 ("parent type & with dynamic component & cannot be parent"
8126 & " of 'C'P'P derivation if new interfaces are present",
8127 Typ, Scope (Original_Record_Component (Comp)));
8129 Error_Msg_Sloc :=
8130 Sloc (Scope (Original_Record_Component (Comp)));
8131 Error_Msg_NE
8132 ("type derived from 'C'P'P type & defined #",
8133 Typ, Scope (Original_Record_Component (Comp)));
8135 -- Avoid duplicated warnings
8137 exit;
8138 end;
8140 -- Initialize secondary tags
8142 else
8143 Append_To (Stmts_List,
8144 Make_Assignment_Statement (Loc,
8145 Name =>
8146 Make_Selected_Component (Loc,
8147 Prefix => New_Copy_Tree (Target),
8148 Selector_Name =>
8149 New_Occurrence_Of (Node (Iface_Comp_Elmt), Loc)),
8150 Expression =>
8151 New_Occurrence_Of (Node (Iface_Tag_Elmt), Loc)));
8152 end if;
8154 -- Otherwise generate code to initialize the tag
8156 else
8157 if (In_Variable_Pos and then Variable_Comps)
8158 or else (not In_Variable_Pos and then Fixed_Comps)
8159 then
8160 Initialize_Tag (Full_Typ,
8161 Iface => Node (Iface_Elmt),
8162 Tag_Comp => Tag_Comp,
8163 Iface_Tag => Node (Iface_Tag_Elmt));
8164 end if;
8165 end if;
8167 Next_Elmt (Iface_Elmt);
8168 Next_Elmt (Iface_Comp_Elmt);
8169 Next_Elmt (Iface_Tag_Elmt);
8170 end loop;
8171 end Init_Secondary_Tags;
8173 ------------------------
8174 -- Is_User_Defined_Eq --
8175 ------------------------
8177 function Is_User_Defined_Equality (Prim : Node_Id) return Boolean is
8178 begin
8179 return Chars (Prim) = Name_Op_Eq
8180 and then Etype (First_Formal (Prim)) =
8181 Etype (Next_Formal (First_Formal (Prim)))
8182 and then Base_Type (Etype (Prim)) = Standard_Boolean;
8183 end Is_User_Defined_Equality;
8185 ----------------------------------------
8186 -- Make_Controlling_Function_Wrappers --
8187 ----------------------------------------
8189 procedure Make_Controlling_Function_Wrappers
8190 (Tag_Typ : Entity_Id;
8191 Decl_List : out List_Id;
8192 Body_List : out List_Id)
8194 Loc : constant Source_Ptr := Sloc (Tag_Typ);
8195 Prim_Elmt : Elmt_Id;
8196 Subp : Entity_Id;
8197 Actual_List : List_Id;
8198 Formal_List : List_Id;
8199 Formal : Entity_Id;
8200 Par_Formal : Entity_Id;
8201 Formal_Node : Node_Id;
8202 Func_Body : Node_Id;
8203 Func_Decl : Node_Id;
8204 Func_Spec : Node_Id;
8205 Return_Stmt : Node_Id;
8207 begin
8208 Decl_List := New_List;
8209 Body_List := New_List;
8211 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
8212 while Present (Prim_Elmt) loop
8213 Subp := Node (Prim_Elmt);
8215 -- If a primitive function with a controlling result of the type has
8216 -- not been overridden by the user, then we must create a wrapper
8217 -- function here that effectively overrides it and invokes the
8218 -- (non-abstract) parent function. This can only occur for a null
8219 -- extension. Note that functions with anonymous controlling access
8220 -- results don't qualify and must be overridden. We also exclude
8221 -- Input attributes, since each type will have its own version of
8222 -- Input constructed by the expander. The test for Comes_From_Source
8223 -- is needed to distinguish inherited operations from renamings
8224 -- (which also have Alias set). We exclude internal entities with
8225 -- Interface_Alias to avoid generating duplicated wrappers since
8226 -- the primitive which covers the interface is also available in
8227 -- the list of primitive operations.
8229 -- The function may be abstract, or require_Overriding may be set
8230 -- for it, because tests for null extensions may already have reset
8231 -- the Is_Abstract_Subprogram_Flag. If Requires_Overriding is not
8232 -- set, functions that need wrappers are recognized by having an
8233 -- alias that returns the parent type.
8235 if Comes_From_Source (Subp)
8236 or else No (Alias (Subp))
8237 or else Present (Interface_Alias (Subp))
8238 or else Ekind (Subp) /= E_Function
8239 or else not Has_Controlling_Result (Subp)
8240 or else Is_Access_Type (Etype (Subp))
8241 or else Is_Abstract_Subprogram (Alias (Subp))
8242 or else Is_TSS (Subp, TSS_Stream_Input)
8243 then
8244 goto Next_Prim;
8246 elsif Is_Abstract_Subprogram (Subp)
8247 or else Requires_Overriding (Subp)
8248 or else
8249 (Is_Null_Extension (Etype (Subp))
8250 and then Etype (Alias (Subp)) /= Etype (Subp))
8251 then
8252 Formal_List := No_List;
8253 Formal := First_Formal (Subp);
8255 if Present (Formal) then
8256 Formal_List := New_List;
8258 while Present (Formal) loop
8259 Append
8260 (Make_Parameter_Specification
8261 (Loc,
8262 Defining_Identifier =>
8263 Make_Defining_Identifier (Sloc (Formal),
8264 Chars => Chars (Formal)),
8265 In_Present => In_Present (Parent (Formal)),
8266 Out_Present => Out_Present (Parent (Formal)),
8267 Null_Exclusion_Present =>
8268 Null_Exclusion_Present (Parent (Formal)),
8269 Parameter_Type =>
8270 New_Occurrence_Of (Etype (Formal), Loc),
8271 Expression =>
8272 New_Copy_Tree (Expression (Parent (Formal)))),
8273 Formal_List);
8275 Next_Formal (Formal);
8276 end loop;
8277 end if;
8279 Func_Spec :=
8280 Make_Function_Specification (Loc,
8281 Defining_Unit_Name =>
8282 Make_Defining_Identifier (Loc,
8283 Chars => Chars (Subp)),
8284 Parameter_Specifications => Formal_List,
8285 Result_Definition =>
8286 New_Occurrence_Of (Etype (Subp), Loc));
8288 Func_Decl := Make_Subprogram_Declaration (Loc, Func_Spec);
8289 Append_To (Decl_List, Func_Decl);
8291 -- Build a wrapper body that calls the parent function. The body
8292 -- contains a single return statement that returns an extension
8293 -- aggregate whose ancestor part is a call to the parent function,
8294 -- passing the formals as actuals (with any controlling arguments
8295 -- converted to the types of the corresponding formals of the
8296 -- parent function, which might be anonymous access types), and
8297 -- having a null extension.
8299 Formal := First_Formal (Subp);
8300 Par_Formal := First_Formal (Alias (Subp));
8301 Formal_Node := First (Formal_List);
8303 if Present (Formal) then
8304 Actual_List := New_List;
8305 else
8306 Actual_List := No_List;
8307 end if;
8309 while Present (Formal) loop
8310 if Is_Controlling_Formal (Formal) then
8311 Append_To (Actual_List,
8312 Make_Type_Conversion (Loc,
8313 Subtype_Mark =>
8314 New_Occurrence_Of (Etype (Par_Formal), Loc),
8315 Expression =>
8316 New_Occurrence_Of
8317 (Defining_Identifier (Formal_Node), Loc)));
8318 else
8319 Append_To
8320 (Actual_List,
8321 New_Occurrence_Of
8322 (Defining_Identifier (Formal_Node), Loc));
8323 end if;
8325 Next_Formal (Formal);
8326 Next_Formal (Par_Formal);
8327 Next (Formal_Node);
8328 end loop;
8330 Return_Stmt :=
8331 Make_Simple_Return_Statement (Loc,
8332 Expression =>
8333 Make_Extension_Aggregate (Loc,
8334 Ancestor_Part =>
8335 Make_Function_Call (Loc,
8336 Name =>
8337 New_Occurrence_Of (Alias (Subp), Loc),
8338 Parameter_Associations => Actual_List),
8339 Null_Record_Present => True));
8341 Func_Body :=
8342 Make_Subprogram_Body (Loc,
8343 Specification => New_Copy_Tree (Func_Spec),
8344 Declarations => Empty_List,
8345 Handled_Statement_Sequence =>
8346 Make_Handled_Sequence_Of_Statements (Loc,
8347 Statements => New_List (Return_Stmt)));
8349 Set_Defining_Unit_Name
8350 (Specification (Func_Body),
8351 Make_Defining_Identifier (Loc, Chars (Subp)));
8353 Append_To (Body_List, Func_Body);
8355 -- Replace the inherited function with the wrapper function in the
8356 -- primitive operations list. We add the minimum decoration needed
8357 -- to override interface primitives.
8359 Set_Ekind (Defining_Unit_Name (Func_Spec), E_Function);
8361 Override_Dispatching_Operation
8362 (Tag_Typ, Subp, New_Op => Defining_Unit_Name (Func_Spec),
8363 Is_Wrapper => True);
8364 end if;
8366 <<Next_Prim>>
8367 Next_Elmt (Prim_Elmt);
8368 end loop;
8369 end Make_Controlling_Function_Wrappers;
8371 -------------------
8372 -- Make_Eq_Body --
8373 -------------------
8375 function Make_Eq_Body
8376 (Typ : Entity_Id;
8377 Eq_Name : Name_Id) return Node_Id
8379 Loc : constant Source_Ptr := Sloc (Parent (Typ));
8380 Decl : Node_Id;
8381 Def : constant Node_Id := Parent (Typ);
8382 Stmts : constant List_Id := New_List;
8383 Variant_Case : Boolean := Has_Discriminants (Typ);
8384 Comps : Node_Id := Empty;
8385 Typ_Def : Node_Id := Type_Definition (Def);
8387 begin
8388 Decl :=
8389 Predef_Spec_Or_Body (Loc,
8390 Tag_Typ => Typ,
8391 Name => Eq_Name,
8392 Profile => New_List (
8393 Make_Parameter_Specification (Loc,
8394 Defining_Identifier =>
8395 Make_Defining_Identifier (Loc, Name_X),
8396 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
8398 Make_Parameter_Specification (Loc,
8399 Defining_Identifier =>
8400 Make_Defining_Identifier (Loc, Name_Y),
8401 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8403 Ret_Type => Standard_Boolean,
8404 For_Body => True);
8406 if Variant_Case then
8407 if Nkind (Typ_Def) = N_Derived_Type_Definition then
8408 Typ_Def := Record_Extension_Part (Typ_Def);
8409 end if;
8411 if Present (Typ_Def) then
8412 Comps := Component_List (Typ_Def);
8413 end if;
8415 Variant_Case :=
8416 Present (Comps) and then Present (Variant_Part (Comps));
8417 end if;
8419 if Variant_Case then
8420 Append_To (Stmts,
8421 Make_Eq_If (Typ, Discriminant_Specifications (Def)));
8422 Append_List_To (Stmts, Make_Eq_Case (Typ, Comps));
8423 Append_To (Stmts,
8424 Make_Simple_Return_Statement (Loc,
8425 Expression => New_Occurrence_Of (Standard_True, Loc)));
8427 else
8428 Append_To (Stmts,
8429 Make_Simple_Return_Statement (Loc,
8430 Expression =>
8431 Expand_Record_Equality
8432 (Typ,
8433 Typ => Typ,
8434 Lhs => Make_Identifier (Loc, Name_X),
8435 Rhs => Make_Identifier (Loc, Name_Y),
8436 Bodies => Declarations (Decl))));
8437 end if;
8439 Set_Handled_Statement_Sequence
8440 (Decl, Make_Handled_Sequence_Of_Statements (Loc, Stmts));
8441 return Decl;
8442 end Make_Eq_Body;
8444 ------------------
8445 -- Make_Eq_Case --
8446 ------------------
8448 -- <Make_Eq_If shared components>
8450 -- case X.D1 is
8451 -- when V1 => <Make_Eq_Case> on subcomponents
8452 -- ...
8453 -- when Vn => <Make_Eq_Case> on subcomponents
8454 -- end case;
8456 function Make_Eq_Case
8457 (E : Entity_Id;
8458 CL : Node_Id;
8459 Discrs : Elist_Id := New_Elmt_List) return List_Id
8461 Loc : constant Source_Ptr := Sloc (E);
8462 Result : constant List_Id := New_List;
8463 Variant : Node_Id;
8464 Alt_List : List_Id;
8466 function Corresponding_Formal (C : Node_Id) return Entity_Id;
8467 -- Given the discriminant that controls a given variant of an unchecked
8468 -- union, find the formal of the equality function that carries the
8469 -- inferred value of the discriminant.
8471 function External_Name (E : Entity_Id) return Name_Id;
8472 -- The value of a given discriminant is conveyed in the corresponding
8473 -- formal parameter of the equality routine. The name of this formal
8474 -- parameter carries a one-character suffix which is removed here.
8476 --------------------------
8477 -- Corresponding_Formal --
8478 --------------------------
8480 function Corresponding_Formal (C : Node_Id) return Entity_Id is
8481 Discr : constant Entity_Id := Entity (Name (Variant_Part (C)));
8482 Elm : Elmt_Id;
8484 begin
8485 Elm := First_Elmt (Discrs);
8486 while Present (Elm) loop
8487 if Chars (Discr) = External_Name (Node (Elm)) then
8488 return Node (Elm);
8489 end if;
8491 Next_Elmt (Elm);
8492 end loop;
8494 -- A formal of the proper name must be found
8496 raise Program_Error;
8497 end Corresponding_Formal;
8499 -------------------
8500 -- External_Name --
8501 -------------------
8503 function External_Name (E : Entity_Id) return Name_Id is
8504 begin
8505 Get_Name_String (Chars (E));
8506 Name_Len := Name_Len - 1;
8507 return Name_Find;
8508 end External_Name;
8510 -- Start of processing for Make_Eq_Case
8512 begin
8513 Append_To (Result, Make_Eq_If (E, Component_Items (CL)));
8515 if No (Variant_Part (CL)) then
8516 return Result;
8517 end if;
8519 Variant := First_Non_Pragma (Variants (Variant_Part (CL)));
8521 if No (Variant) then
8522 return Result;
8523 end if;
8525 Alt_List := New_List;
8526 while Present (Variant) loop
8527 Append_To (Alt_List,
8528 Make_Case_Statement_Alternative (Loc,
8529 Discrete_Choices => New_Copy_List (Discrete_Choices (Variant)),
8530 Statements =>
8531 Make_Eq_Case (E, Component_List (Variant), Discrs)));
8532 Next_Non_Pragma (Variant);
8533 end loop;
8535 -- If we have an Unchecked_Union, use one of the parameters of the
8536 -- enclosing equality routine that captures the discriminant, to use
8537 -- as the expression in the generated case statement.
8539 if Is_Unchecked_Union (E) then
8540 Append_To (Result,
8541 Make_Case_Statement (Loc,
8542 Expression =>
8543 New_Occurrence_Of (Corresponding_Formal (CL), Loc),
8544 Alternatives => Alt_List));
8546 else
8547 Append_To (Result,
8548 Make_Case_Statement (Loc,
8549 Expression =>
8550 Make_Selected_Component (Loc,
8551 Prefix => Make_Identifier (Loc, Name_X),
8552 Selector_Name => New_Copy (Name (Variant_Part (CL)))),
8553 Alternatives => Alt_List));
8554 end if;
8556 return Result;
8557 end Make_Eq_Case;
8559 ----------------
8560 -- Make_Eq_If --
8561 ----------------
8563 -- Generates:
8565 -- if
8566 -- X.C1 /= Y.C1
8567 -- or else
8568 -- X.C2 /= Y.C2
8569 -- ...
8570 -- then
8571 -- return False;
8572 -- end if;
8574 -- or a null statement if the list L is empty
8576 function Make_Eq_If
8577 (E : Entity_Id;
8578 L : List_Id) return Node_Id
8580 Loc : constant Source_Ptr := Sloc (E);
8581 C : Node_Id;
8582 Field_Name : Name_Id;
8583 Cond : Node_Id;
8585 begin
8586 if No (L) then
8587 return Make_Null_Statement (Loc);
8589 else
8590 Cond := Empty;
8592 C := First_Non_Pragma (L);
8593 while Present (C) loop
8594 Field_Name := Chars (Defining_Identifier (C));
8596 -- The tags must not be compared: they are not part of the value.
8597 -- Ditto for parent interfaces because their equality operator is
8598 -- abstract.
8600 -- Note also that in the following, we use Make_Identifier for
8601 -- the component names. Use of New_Occurrence_Of to identify the
8602 -- components would be incorrect because the wrong entities for
8603 -- discriminants could be picked up in the private type case.
8605 if Field_Name = Name_uParent
8606 and then Is_Interface (Etype (Defining_Identifier (C)))
8607 then
8608 null;
8610 elsif Field_Name /= Name_uTag then
8611 Evolve_Or_Else (Cond,
8612 Make_Op_Ne (Loc,
8613 Left_Opnd =>
8614 Make_Selected_Component (Loc,
8615 Prefix => Make_Identifier (Loc, Name_X),
8616 Selector_Name => Make_Identifier (Loc, Field_Name)),
8618 Right_Opnd =>
8619 Make_Selected_Component (Loc,
8620 Prefix => Make_Identifier (Loc, Name_Y),
8621 Selector_Name => Make_Identifier (Loc, Field_Name))));
8622 end if;
8624 Next_Non_Pragma (C);
8625 end loop;
8627 if No (Cond) then
8628 return Make_Null_Statement (Loc);
8630 else
8631 return
8632 Make_Implicit_If_Statement (E,
8633 Condition => Cond,
8634 Then_Statements => New_List (
8635 Make_Simple_Return_Statement (Loc,
8636 Expression => New_Occurrence_Of (Standard_False, Loc))));
8637 end if;
8638 end if;
8639 end Make_Eq_If;
8641 -------------------
8642 -- Make_Neq_Body --
8643 -------------------
8645 function Make_Neq_Body (Tag_Typ : Entity_Id) return Node_Id is
8647 function Is_Predefined_Neq_Renaming (Prim : Node_Id) return Boolean;
8648 -- Returns true if Prim is a renaming of an unresolved predefined
8649 -- inequality operation.
8651 --------------------------------
8652 -- Is_Predefined_Neq_Renaming --
8653 --------------------------------
8655 function Is_Predefined_Neq_Renaming (Prim : Node_Id) return Boolean is
8656 begin
8657 return Chars (Prim) /= Name_Op_Ne
8658 and then Present (Alias (Prim))
8659 and then Comes_From_Source (Prim)
8660 and then Is_Intrinsic_Subprogram (Alias (Prim))
8661 and then Chars (Alias (Prim)) = Name_Op_Ne;
8662 end Is_Predefined_Neq_Renaming;
8664 -- Local variables
8666 Loc : constant Source_Ptr := Sloc (Parent (Tag_Typ));
8667 Stmts : constant List_Id := New_List;
8668 Decl : Node_Id;
8669 Eq_Prim : Entity_Id;
8670 Left_Op : Entity_Id;
8671 Renaming_Prim : Entity_Id;
8672 Right_Op : Entity_Id;
8673 Target : Entity_Id;
8675 -- Start of processing for Make_Neq_Body
8677 begin
8678 -- For a call on a renaming of a dispatching subprogram that is
8679 -- overridden, if the overriding occurred before the renaming, then
8680 -- the body executed is that of the overriding declaration, even if the
8681 -- overriding declaration is not visible at the place of the renaming;
8682 -- otherwise, the inherited or predefined subprogram is called, see
8683 -- (RM 8.5.4(8))
8685 -- Stage 1: Search for a renaming of the inequality primitive and also
8686 -- search for an overriding of the equality primitive located before the
8687 -- renaming declaration.
8689 declare
8690 Elmt : Elmt_Id;
8691 Prim : Node_Id;
8693 begin
8694 Eq_Prim := Empty;
8695 Renaming_Prim := Empty;
8697 Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
8698 while Present (Elmt) loop
8699 Prim := Node (Elmt);
8701 if Is_User_Defined_Equality (Prim) and then No (Alias (Prim)) then
8702 if No (Renaming_Prim) then
8703 pragma Assert (No (Eq_Prim));
8704 Eq_Prim := Prim;
8705 end if;
8707 elsif Is_Predefined_Neq_Renaming (Prim) then
8708 Renaming_Prim := Prim;
8709 end if;
8711 Next_Elmt (Elmt);
8712 end loop;
8713 end;
8715 -- No further action needed if no renaming was found
8717 if No (Renaming_Prim) then
8718 return Empty;
8719 end if;
8721 -- Stage 2: Replace the renaming declaration by a subprogram declaration
8722 -- (required to add its body)
8724 Decl := Parent (Parent (Renaming_Prim));
8725 Rewrite (Decl,
8726 Make_Subprogram_Declaration (Loc,
8727 Specification => Specification (Decl)));
8728 Set_Analyzed (Decl);
8730 -- Remove the decoration of intrinsic renaming subprogram
8732 Set_Is_Intrinsic_Subprogram (Renaming_Prim, False);
8733 Set_Convention (Renaming_Prim, Convention_Ada);
8734 Set_Alias (Renaming_Prim, Empty);
8735 Set_Has_Completion (Renaming_Prim, False);
8737 -- Stage 3: Build the corresponding body
8739 Left_Op := First_Formal (Renaming_Prim);
8740 Right_Op := Next_Formal (Left_Op);
8742 Decl :=
8743 Predef_Spec_Or_Body (Loc,
8744 Tag_Typ => Tag_Typ,
8745 Name => Chars (Renaming_Prim),
8746 Profile => New_List (
8747 Make_Parameter_Specification (Loc,
8748 Defining_Identifier =>
8749 Make_Defining_Identifier (Loc, Chars (Left_Op)),
8750 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
8752 Make_Parameter_Specification (Loc,
8753 Defining_Identifier =>
8754 Make_Defining_Identifier (Loc, Chars (Right_Op)),
8755 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
8757 Ret_Type => Standard_Boolean,
8758 For_Body => True);
8760 -- If the overriding of the equality primitive occurred before the
8761 -- renaming, then generate:
8763 -- function <Neq_Name> (X : Y : Typ) return Boolean is
8764 -- begin
8765 -- return not Oeq (X, Y);
8766 -- end;
8768 if Present (Eq_Prim) then
8769 Target := Eq_Prim;
8771 -- Otherwise build a nested subprogram which performs the predefined
8772 -- evaluation of the equality operator. That is, generate:
8774 -- function <Neq_Name> (X : Y : Typ) return Boolean is
8775 -- function Oeq (X : Y) return Boolean is
8776 -- begin
8777 -- <<body of default implementation>>
8778 -- end;
8779 -- begin
8780 -- return not Oeq (X, Y);
8781 -- end;
8783 else
8784 declare
8785 Local_Subp : Node_Id;
8786 begin
8787 Local_Subp := Make_Eq_Body (Tag_Typ, Name_Op_Eq);
8788 Set_Declarations (Decl, New_List (Local_Subp));
8789 Target := Defining_Entity (Local_Subp);
8790 end;
8791 end if;
8793 Append_To (Stmts,
8794 Make_Simple_Return_Statement (Loc,
8795 Expression =>
8796 Make_Op_Not (Loc,
8797 Make_Function_Call (Loc,
8798 Name => New_Occurrence_Of (Target, Loc),
8799 Parameter_Associations => New_List (
8800 Make_Identifier (Loc, Chars (Left_Op)),
8801 Make_Identifier (Loc, Chars (Right_Op)))))));
8803 Set_Handled_Statement_Sequence
8804 (Decl, Make_Handled_Sequence_Of_Statements (Loc, Stmts));
8805 return Decl;
8806 end Make_Neq_Body;
8808 -------------------------------
8809 -- Make_Null_Procedure_Specs --
8810 -------------------------------
8812 function Make_Null_Procedure_Specs (Tag_Typ : Entity_Id) return List_Id is
8813 Decl_List : constant List_Id := New_List;
8814 Loc : constant Source_Ptr := Sloc (Tag_Typ);
8815 Formal : Entity_Id;
8816 Formal_List : List_Id;
8817 New_Param_Spec : Node_Id;
8818 Parent_Subp : Entity_Id;
8819 Prim_Elmt : Elmt_Id;
8820 Subp : Entity_Id;
8822 begin
8823 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
8824 while Present (Prim_Elmt) loop
8825 Subp := Node (Prim_Elmt);
8827 -- If a null procedure inherited from an interface has not been
8828 -- overridden, then we build a null procedure declaration to
8829 -- override the inherited procedure.
8831 Parent_Subp := Alias (Subp);
8833 if Present (Parent_Subp)
8834 and then Is_Null_Interface_Primitive (Parent_Subp)
8835 then
8836 Formal_List := No_List;
8837 Formal := First_Formal (Subp);
8839 if Present (Formal) then
8840 Formal_List := New_List;
8842 while Present (Formal) loop
8844 -- Copy the parameter spec including default expressions
8846 New_Param_Spec :=
8847 New_Copy_Tree (Parent (Formal), New_Sloc => Loc);
8849 -- Generate a new defining identifier for the new formal.
8850 -- required because New_Copy_Tree does not duplicate
8851 -- semantic fields (except itypes).
8853 Set_Defining_Identifier (New_Param_Spec,
8854 Make_Defining_Identifier (Sloc (Formal),
8855 Chars => Chars (Formal)));
8857 -- For controlling arguments we must change their
8858 -- parameter type to reference the tagged type (instead
8859 -- of the interface type)
8861 if Is_Controlling_Formal (Formal) then
8862 if Nkind (Parameter_Type (Parent (Formal))) = N_Identifier
8863 then
8864 Set_Parameter_Type (New_Param_Spec,
8865 New_Occurrence_Of (Tag_Typ, Loc));
8867 else pragma Assert
8868 (Nkind (Parameter_Type (Parent (Formal))) =
8869 N_Access_Definition);
8870 Set_Subtype_Mark (Parameter_Type (New_Param_Spec),
8871 New_Occurrence_Of (Tag_Typ, Loc));
8872 end if;
8873 end if;
8875 Append (New_Param_Spec, Formal_List);
8877 Next_Formal (Formal);
8878 end loop;
8879 end if;
8881 Append_To (Decl_List,
8882 Make_Subprogram_Declaration (Loc,
8883 Make_Procedure_Specification (Loc,
8884 Defining_Unit_Name =>
8885 Make_Defining_Identifier (Loc, Chars (Subp)),
8886 Parameter_Specifications => Formal_List,
8887 Null_Present => True)));
8888 end if;
8890 Next_Elmt (Prim_Elmt);
8891 end loop;
8893 return Decl_List;
8894 end Make_Null_Procedure_Specs;
8896 -------------------------------------
8897 -- Make_Predefined_Primitive_Specs --
8898 -------------------------------------
8900 procedure Make_Predefined_Primitive_Specs
8901 (Tag_Typ : Entity_Id;
8902 Predef_List : out List_Id;
8903 Renamed_Eq : out Entity_Id)
8905 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean;
8906 -- Returns true if Prim is a renaming of an unresolved predefined
8907 -- equality operation.
8909 -------------------------------
8910 -- Is_Predefined_Eq_Renaming --
8911 -------------------------------
8913 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean is
8914 begin
8915 return Chars (Prim) /= Name_Op_Eq
8916 and then Present (Alias (Prim))
8917 and then Comes_From_Source (Prim)
8918 and then Is_Intrinsic_Subprogram (Alias (Prim))
8919 and then Chars (Alias (Prim)) = Name_Op_Eq;
8920 end Is_Predefined_Eq_Renaming;
8922 -- Local variables
8924 Loc : constant Source_Ptr := Sloc (Tag_Typ);
8925 Res : constant List_Id := New_List;
8926 Eq_Name : Name_Id := Name_Op_Eq;
8927 Eq_Needed : Boolean;
8928 Eq_Spec : Node_Id;
8929 Prim : Elmt_Id;
8931 Has_Predef_Eq_Renaming : Boolean := False;
8932 -- Set to True if Tag_Typ has a primitive that renames the predefined
8933 -- equality operator. Used to implement (RM 8-5-4(8)).
8935 -- Start of processing for Make_Predefined_Primitive_Specs
8937 begin
8938 Renamed_Eq := Empty;
8940 -- Spec of _Size
8942 Append_To (Res, Predef_Spec_Or_Body (Loc,
8943 Tag_Typ => Tag_Typ,
8944 Name => Name_uSize,
8945 Profile => New_List (
8946 Make_Parameter_Specification (Loc,
8947 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
8948 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
8950 Ret_Type => Standard_Long_Long_Integer));
8952 -- Specs for dispatching stream attributes
8954 declare
8955 Stream_Op_TSS_Names :
8956 constant array (Integer range <>) of TSS_Name_Type :=
8957 (TSS_Stream_Read,
8958 TSS_Stream_Write,
8959 TSS_Stream_Input,
8960 TSS_Stream_Output);
8962 begin
8963 for Op in Stream_Op_TSS_Names'Range loop
8964 if Stream_Operation_OK (Tag_Typ, Stream_Op_TSS_Names (Op)) then
8965 Append_To (Res,
8966 Predef_Stream_Attr_Spec (Loc, Tag_Typ,
8967 Stream_Op_TSS_Names (Op)));
8968 end if;
8969 end loop;
8970 end;
8972 -- Spec of "=" is expanded if the type is not limited and if a user
8973 -- defined "=" was not already declared for the non-full view of a
8974 -- private extension
8976 if not Is_Limited_Type (Tag_Typ) then
8977 Eq_Needed := True;
8978 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
8979 while Present (Prim) loop
8981 -- If a primitive is encountered that renames the predefined
8982 -- equality operator before reaching any explicit equality
8983 -- primitive, then we still need to create a predefined equality
8984 -- function, because calls to it can occur via the renaming. A
8985 -- new name is created for the equality to avoid conflicting with
8986 -- any user-defined equality. (Note that this doesn't account for
8987 -- renamings of equality nested within subpackages???)
8989 if Is_Predefined_Eq_Renaming (Node (Prim)) then
8990 Has_Predef_Eq_Renaming := True;
8991 Eq_Name := New_External_Name (Chars (Node (Prim)), 'E');
8993 -- User-defined equality
8995 elsif Is_User_Defined_Equality (Node (Prim)) then
8996 if No (Alias (Node (Prim)))
8997 or else Nkind (Unit_Declaration_Node (Node (Prim))) =
8998 N_Subprogram_Renaming_Declaration
8999 then
9000 Eq_Needed := False;
9001 exit;
9003 -- If the parent is not an interface type and has an abstract
9004 -- equality function explicitly defined in the sources, then
9005 -- the inherited equality is abstract as well, and no body can
9006 -- be created for it.
9008 elsif not Is_Interface (Etype (Tag_Typ))
9009 and then Present (Alias (Node (Prim)))
9010 and then Comes_From_Source (Alias (Node (Prim)))
9011 and then Is_Abstract_Subprogram (Alias (Node (Prim)))
9012 then
9013 Eq_Needed := False;
9014 exit;
9016 -- If the type has an equality function corresponding with
9017 -- a primitive defined in an interface type, the inherited
9018 -- equality is abstract as well, and no body can be created
9019 -- for it.
9021 elsif Present (Alias (Node (Prim)))
9022 and then Comes_From_Source (Ultimate_Alias (Node (Prim)))
9023 and then
9024 Is_Interface
9025 (Find_Dispatching_Type (Ultimate_Alias (Node (Prim))))
9026 then
9027 Eq_Needed := False;
9028 exit;
9029 end if;
9030 end if;
9032 Next_Elmt (Prim);
9033 end loop;
9035 -- If a renaming of predefined equality was found but there was no
9036 -- user-defined equality (so Eq_Needed is still true), then set the
9037 -- name back to Name_Op_Eq. But in the case where a user-defined
9038 -- equality was located after such a renaming, then the predefined
9039 -- equality function is still needed, so Eq_Needed must be set back
9040 -- to True.
9042 if Eq_Name /= Name_Op_Eq then
9043 if Eq_Needed then
9044 Eq_Name := Name_Op_Eq;
9045 else
9046 Eq_Needed := True;
9047 end if;
9048 end if;
9050 if Eq_Needed then
9051 Eq_Spec := Predef_Spec_Or_Body (Loc,
9052 Tag_Typ => Tag_Typ,
9053 Name => Eq_Name,
9054 Profile => New_List (
9055 Make_Parameter_Specification (Loc,
9056 Defining_Identifier =>
9057 Make_Defining_Identifier (Loc, Name_X),
9058 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
9060 Make_Parameter_Specification (Loc,
9061 Defining_Identifier =>
9062 Make_Defining_Identifier (Loc, Name_Y),
9063 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
9064 Ret_Type => Standard_Boolean);
9065 Append_To (Res, Eq_Spec);
9067 if Has_Predef_Eq_Renaming then
9068 Renamed_Eq := Defining_Unit_Name (Specification (Eq_Spec));
9070 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
9071 while Present (Prim) loop
9073 -- Any renamings of equality that appeared before an
9074 -- overriding equality must be updated to refer to the
9075 -- entity for the predefined equality, otherwise calls via
9076 -- the renaming would get incorrectly resolved to call the
9077 -- user-defined equality function.
9079 if Is_Predefined_Eq_Renaming (Node (Prim)) then
9080 Set_Alias (Node (Prim), Renamed_Eq);
9082 -- Exit upon encountering a user-defined equality
9084 elsif Chars (Node (Prim)) = Name_Op_Eq
9085 and then No (Alias (Node (Prim)))
9086 then
9087 exit;
9088 end if;
9090 Next_Elmt (Prim);
9091 end loop;
9092 end if;
9093 end if;
9095 -- Spec for dispatching assignment
9097 Append_To (Res, Predef_Spec_Or_Body (Loc,
9098 Tag_Typ => Tag_Typ,
9099 Name => Name_uAssign,
9100 Profile => New_List (
9101 Make_Parameter_Specification (Loc,
9102 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
9103 Out_Present => True,
9104 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
9106 Make_Parameter_Specification (Loc,
9107 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
9108 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)))));
9109 end if;
9111 -- Ada 2005: Generate declarations for the following primitive
9112 -- operations for limited interfaces and synchronized types that
9113 -- implement a limited interface.
9115 -- Disp_Asynchronous_Select
9116 -- Disp_Conditional_Select
9117 -- Disp_Get_Prim_Op_Kind
9118 -- Disp_Get_Task_Id
9119 -- Disp_Requeue
9120 -- Disp_Timed_Select
9122 -- Disable the generation of these bodies if No_Dispatching_Calls,
9123 -- Ravenscar or ZFP is active.
9125 if Ada_Version >= Ada_2005
9126 and then not Restriction_Active (No_Dispatching_Calls)
9127 and then not Restriction_Active (No_Select_Statements)
9128 and then RTE_Available (RE_Select_Specific_Data)
9129 then
9130 -- These primitives are defined abstract in interface types
9132 if Is_Interface (Tag_Typ)
9133 and then Is_Limited_Record (Tag_Typ)
9134 then
9135 Append_To (Res,
9136 Make_Abstract_Subprogram_Declaration (Loc,
9137 Specification =>
9138 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
9140 Append_To (Res,
9141 Make_Abstract_Subprogram_Declaration (Loc,
9142 Specification =>
9143 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
9145 Append_To (Res,
9146 Make_Abstract_Subprogram_Declaration (Loc,
9147 Specification =>
9148 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
9150 Append_To (Res,
9151 Make_Abstract_Subprogram_Declaration (Loc,
9152 Specification =>
9153 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
9155 Append_To (Res,
9156 Make_Abstract_Subprogram_Declaration (Loc,
9157 Specification =>
9158 Make_Disp_Requeue_Spec (Tag_Typ)));
9160 Append_To (Res,
9161 Make_Abstract_Subprogram_Declaration (Loc,
9162 Specification =>
9163 Make_Disp_Timed_Select_Spec (Tag_Typ)));
9165 -- If ancestor is an interface type, declare non-abstract primitives
9166 -- to override the abstract primitives of the interface type.
9168 -- In VM targets we define these primitives in all root tagged types
9169 -- that are not interface types. Done because in VM targets we don't
9170 -- have secondary dispatch tables and any derivation of Tag_Typ may
9171 -- cover limited interfaces (which always have these primitives since
9172 -- they may be ancestors of synchronized interface types).
9174 elsif (not Is_Interface (Tag_Typ)
9175 and then Is_Interface (Etype (Tag_Typ))
9176 and then Is_Limited_Record (Etype (Tag_Typ)))
9177 or else
9178 (Is_Concurrent_Record_Type (Tag_Typ)
9179 and then Has_Interfaces (Tag_Typ))
9180 or else
9181 (not Tagged_Type_Expansion
9182 and then not Is_Interface (Tag_Typ)
9183 and then Tag_Typ = Root_Type (Tag_Typ))
9184 then
9185 Append_To (Res,
9186 Make_Subprogram_Declaration (Loc,
9187 Specification =>
9188 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
9190 Append_To (Res,
9191 Make_Subprogram_Declaration (Loc,
9192 Specification =>
9193 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
9195 Append_To (Res,
9196 Make_Subprogram_Declaration (Loc,
9197 Specification =>
9198 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
9200 Append_To (Res,
9201 Make_Subprogram_Declaration (Loc,
9202 Specification =>
9203 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
9205 Append_To (Res,
9206 Make_Subprogram_Declaration (Loc,
9207 Specification =>
9208 Make_Disp_Requeue_Spec (Tag_Typ)));
9210 Append_To (Res,
9211 Make_Subprogram_Declaration (Loc,
9212 Specification =>
9213 Make_Disp_Timed_Select_Spec (Tag_Typ)));
9214 end if;
9215 end if;
9217 -- All tagged types receive their own Deep_Adjust and Deep_Finalize
9218 -- regardless of whether they are controlled or may contain controlled
9219 -- components.
9221 -- Do not generate the routines if finalization is disabled
9223 if Restriction_Active (No_Finalization) then
9224 null;
9226 else
9227 if not Is_Limited_Type (Tag_Typ) then
9228 Append_To (Res, Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust));
9229 end if;
9231 Append_To (Res, Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize));
9232 end if;
9234 Predef_List := Res;
9235 end Make_Predefined_Primitive_Specs;
9237 -------------------------
9238 -- Make_Tag_Assignment --
9239 -------------------------
9241 function Make_Tag_Assignment (N : Node_Id) return Node_Id is
9242 Loc : constant Source_Ptr := Sloc (N);
9243 Def_If : constant Entity_Id := Defining_Identifier (N);
9244 Expr : constant Node_Id := Expression (N);
9245 Typ : constant Entity_Id := Etype (Def_If);
9246 Full_Typ : constant Entity_Id := Underlying_Type (Typ);
9247 New_Ref : Node_Id;
9249 begin
9250 -- This expansion activity is called during analysis, but cannot
9251 -- be applied in ASIS mode when other expansion is disabled.
9253 if Is_Tagged_Type (Typ)
9254 and then not Is_Class_Wide_Type (Typ)
9255 and then not Is_CPP_Class (Typ)
9256 and then Tagged_Type_Expansion
9257 and then Nkind (Expr) /= N_Aggregate
9258 and then not ASIS_Mode
9259 and then (Nkind (Expr) /= N_Qualified_Expression
9260 or else Nkind (Expression (Expr)) /= N_Aggregate)
9261 then
9262 New_Ref :=
9263 Make_Selected_Component (Loc,
9264 Prefix => New_Occurrence_Of (Def_If, Loc),
9265 Selector_Name =>
9266 New_Occurrence_Of (First_Tag_Component (Full_Typ), Loc));
9267 Set_Assignment_OK (New_Ref);
9269 return
9270 Make_Assignment_Statement (Loc,
9271 Name => New_Ref,
9272 Expression =>
9273 Unchecked_Convert_To (RTE (RE_Tag),
9274 New_Occurrence_Of (Node
9275 (First_Elmt (Access_Disp_Table (Full_Typ))), Loc)));
9276 else
9277 return Empty;
9278 end if;
9279 end Make_Tag_Assignment;
9281 ---------------------------------
9282 -- Needs_Simple_Initialization --
9283 ---------------------------------
9285 function Needs_Simple_Initialization
9286 (T : Entity_Id;
9287 Consider_IS : Boolean := True) return Boolean
9289 Consider_IS_NS : constant Boolean :=
9290 Normalize_Scalars or (Initialize_Scalars and Consider_IS);
9292 begin
9293 -- Never need initialization if it is suppressed
9295 if Initialization_Suppressed (T) then
9296 return False;
9297 end if;
9299 -- Check for private type, in which case test applies to the underlying
9300 -- type of the private type.
9302 if Is_Private_Type (T) then
9303 declare
9304 RT : constant Entity_Id := Underlying_Type (T);
9305 begin
9306 if Present (RT) then
9307 return Needs_Simple_Initialization (RT);
9308 else
9309 return False;
9310 end if;
9311 end;
9313 -- Scalar type with Default_Value aspect requires initialization
9315 elsif Is_Scalar_Type (T) and then Has_Default_Aspect (T) then
9316 return True;
9318 -- Cases needing simple initialization are access types, and, if pragma
9319 -- Normalize_Scalars or Initialize_Scalars is in effect, then all scalar
9320 -- types.
9322 elsif Is_Access_Type (T)
9323 or else (Consider_IS_NS and then (Is_Scalar_Type (T)))
9324 then
9325 return True;
9327 -- If Initialize/Normalize_Scalars is in effect, string objects also
9328 -- need initialization, unless they are created in the course of
9329 -- expanding an aggregate (since in the latter case they will be
9330 -- filled with appropriate initializing values before they are used).
9332 elsif Consider_IS_NS
9333 and then Is_Standard_String_Type (T)
9334 and then
9335 (not Is_Itype (T)
9336 or else Nkind (Associated_Node_For_Itype (T)) /= N_Aggregate)
9337 then
9338 return True;
9340 else
9341 return False;
9342 end if;
9343 end Needs_Simple_Initialization;
9345 ----------------------
9346 -- Predef_Deep_Spec --
9347 ----------------------
9349 function Predef_Deep_Spec
9350 (Loc : Source_Ptr;
9351 Tag_Typ : Entity_Id;
9352 Name : TSS_Name_Type;
9353 For_Body : Boolean := False) return Node_Id
9355 Formals : List_Id;
9357 begin
9358 -- V : in out Tag_Typ
9360 Formals := New_List (
9361 Make_Parameter_Specification (Loc,
9362 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
9363 In_Present => True,
9364 Out_Present => True,
9365 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)));
9367 -- F : Boolean := True
9369 if Name = TSS_Deep_Adjust
9370 or else Name = TSS_Deep_Finalize
9371 then
9372 Append_To (Formals,
9373 Make_Parameter_Specification (Loc,
9374 Defining_Identifier => Make_Defining_Identifier (Loc, Name_F),
9375 Parameter_Type => New_Occurrence_Of (Standard_Boolean, Loc),
9376 Expression => New_Occurrence_Of (Standard_True, Loc)));
9377 end if;
9379 return
9380 Predef_Spec_Or_Body (Loc,
9381 Name => Make_TSS_Name (Tag_Typ, Name),
9382 Tag_Typ => Tag_Typ,
9383 Profile => Formals,
9384 For_Body => For_Body);
9386 exception
9387 when RE_Not_Available =>
9388 return Empty;
9389 end Predef_Deep_Spec;
9391 -------------------------
9392 -- Predef_Spec_Or_Body --
9393 -------------------------
9395 function Predef_Spec_Or_Body
9396 (Loc : Source_Ptr;
9397 Tag_Typ : Entity_Id;
9398 Name : Name_Id;
9399 Profile : List_Id;
9400 Ret_Type : Entity_Id := Empty;
9401 For_Body : Boolean := False) return Node_Id
9403 Id : constant Entity_Id := Make_Defining_Identifier (Loc, Name);
9404 Spec : Node_Id;
9406 begin
9407 Set_Is_Public (Id, Is_Public (Tag_Typ));
9409 -- The internal flag is set to mark these declarations because they have
9410 -- specific properties. First, they are primitives even if they are not
9411 -- defined in the type scope (the freezing point is not necessarily in
9412 -- the same scope). Second, the predefined equality can be overridden by
9413 -- a user-defined equality, no body will be generated in this case.
9415 Set_Is_Internal (Id);
9417 if not Debug_Generated_Code then
9418 Set_Debug_Info_Off (Id);
9419 end if;
9421 if No (Ret_Type) then
9422 Spec :=
9423 Make_Procedure_Specification (Loc,
9424 Defining_Unit_Name => Id,
9425 Parameter_Specifications => Profile);
9426 else
9427 Spec :=
9428 Make_Function_Specification (Loc,
9429 Defining_Unit_Name => Id,
9430 Parameter_Specifications => Profile,
9431 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
9432 end if;
9434 if Is_Interface (Tag_Typ) then
9435 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
9437 -- If body case, return empty subprogram body. Note that this is ill-
9438 -- formed, because there is not even a null statement, and certainly not
9439 -- a return in the function case. The caller is expected to do surgery
9440 -- on the body to add the appropriate stuff.
9442 elsif For_Body then
9443 return Make_Subprogram_Body (Loc, Spec, Empty_List, Empty);
9445 -- For the case of an Input attribute predefined for an abstract type,
9446 -- generate an abstract specification. This will never be called, but we
9447 -- need the slot allocated in the dispatching table so that attributes
9448 -- typ'Class'Input and typ'Class'Output will work properly.
9450 elsif Is_TSS (Name, TSS_Stream_Input)
9451 and then Is_Abstract_Type (Tag_Typ)
9452 then
9453 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
9455 -- Normal spec case, where we return a subprogram declaration
9457 else
9458 return Make_Subprogram_Declaration (Loc, Spec);
9459 end if;
9460 end Predef_Spec_Or_Body;
9462 -----------------------------
9463 -- Predef_Stream_Attr_Spec --
9464 -----------------------------
9466 function Predef_Stream_Attr_Spec
9467 (Loc : Source_Ptr;
9468 Tag_Typ : Entity_Id;
9469 Name : TSS_Name_Type;
9470 For_Body : Boolean := False) return Node_Id
9472 Ret_Type : Entity_Id;
9474 begin
9475 if Name = TSS_Stream_Input then
9476 Ret_Type := Tag_Typ;
9477 else
9478 Ret_Type := Empty;
9479 end if;
9481 return
9482 Predef_Spec_Or_Body
9483 (Loc,
9484 Name => Make_TSS_Name (Tag_Typ, Name),
9485 Tag_Typ => Tag_Typ,
9486 Profile => Build_Stream_Attr_Profile (Loc, Tag_Typ, Name),
9487 Ret_Type => Ret_Type,
9488 For_Body => For_Body);
9489 end Predef_Stream_Attr_Spec;
9491 ---------------------------------
9492 -- Predefined_Primitive_Bodies --
9493 ---------------------------------
9495 function Predefined_Primitive_Bodies
9496 (Tag_Typ : Entity_Id;
9497 Renamed_Eq : Entity_Id) return List_Id
9499 Loc : constant Source_Ptr := Sloc (Tag_Typ);
9500 Res : constant List_Id := New_List;
9501 Decl : Node_Id;
9502 Prim : Elmt_Id;
9503 Eq_Needed : Boolean;
9504 Eq_Name : Name_Id;
9505 Ent : Entity_Id;
9507 pragma Warnings (Off, Ent);
9509 begin
9510 pragma Assert (not Is_Interface (Tag_Typ));
9512 -- See if we have a predefined "=" operator
9514 if Present (Renamed_Eq) then
9515 Eq_Needed := True;
9516 Eq_Name := Chars (Renamed_Eq);
9518 -- If the parent is an interface type then it has defined all the
9519 -- predefined primitives abstract and we need to check if the type
9520 -- has some user defined "=" function which matches the profile of
9521 -- the Ada predefined equality operator to avoid generating it.
9523 elsif Is_Interface (Etype (Tag_Typ)) then
9524 Eq_Needed := True;
9525 Eq_Name := Name_Op_Eq;
9527 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
9528 while Present (Prim) loop
9529 if Chars (Node (Prim)) = Name_Op_Eq
9530 and then not Is_Internal (Node (Prim))
9531 and then Present (First_Entity (Node (Prim)))
9533 -- The predefined equality primitive must have exactly two
9534 -- formals whose type is this tagged type
9536 and then Present (Last_Entity (Node (Prim)))
9537 and then Next_Entity (First_Entity (Node (Prim)))
9538 = Last_Entity (Node (Prim))
9539 and then Etype (First_Entity (Node (Prim))) = Tag_Typ
9540 and then Etype (Last_Entity (Node (Prim))) = Tag_Typ
9541 then
9542 Eq_Needed := False;
9543 Eq_Name := No_Name;
9544 exit;
9545 end if;
9547 Next_Elmt (Prim);
9548 end loop;
9550 else
9551 Eq_Needed := False;
9552 Eq_Name := No_Name;
9554 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
9555 while Present (Prim) loop
9556 if Chars (Node (Prim)) = Name_Op_Eq
9557 and then Is_Internal (Node (Prim))
9558 then
9559 Eq_Needed := True;
9560 Eq_Name := Name_Op_Eq;
9561 exit;
9562 end if;
9564 Next_Elmt (Prim);
9565 end loop;
9566 end if;
9568 -- Body of _Size
9570 Decl := Predef_Spec_Or_Body (Loc,
9571 Tag_Typ => Tag_Typ,
9572 Name => Name_uSize,
9573 Profile => New_List (
9574 Make_Parameter_Specification (Loc,
9575 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
9576 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
9578 Ret_Type => Standard_Long_Long_Integer,
9579 For_Body => True);
9581 Set_Handled_Statement_Sequence (Decl,
9582 Make_Handled_Sequence_Of_Statements (Loc, New_List (
9583 Make_Simple_Return_Statement (Loc,
9584 Expression =>
9585 Make_Attribute_Reference (Loc,
9586 Prefix => Make_Identifier (Loc, Name_X),
9587 Attribute_Name => Name_Size)))));
9589 Append_To (Res, Decl);
9591 -- Bodies for Dispatching stream IO routines. We need these only for
9592 -- non-limited types (in the limited case there is no dispatching).
9593 -- We also skip them if dispatching or finalization are not available
9594 -- or if stream operations are prohibited by restriction No_Streams or
9595 -- from use of pragma/aspect No_Tagged_Streams.
9597 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Read)
9598 and then No (TSS (Tag_Typ, TSS_Stream_Read))
9599 then
9600 Build_Record_Read_Procedure (Loc, Tag_Typ, Decl, Ent);
9601 Append_To (Res, Decl);
9602 end if;
9604 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Write)
9605 and then No (TSS (Tag_Typ, TSS_Stream_Write))
9606 then
9607 Build_Record_Write_Procedure (Loc, Tag_Typ, Decl, Ent);
9608 Append_To (Res, Decl);
9609 end if;
9611 -- Skip body of _Input for the abstract case, since the corresponding
9612 -- spec is abstract (see Predef_Spec_Or_Body).
9614 if not Is_Abstract_Type (Tag_Typ)
9615 and then Stream_Operation_OK (Tag_Typ, TSS_Stream_Input)
9616 and then No (TSS (Tag_Typ, TSS_Stream_Input))
9617 then
9618 Build_Record_Or_Elementary_Input_Function
9619 (Loc, Tag_Typ, Decl, Ent);
9620 Append_To (Res, Decl);
9621 end if;
9623 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Output)
9624 and then No (TSS (Tag_Typ, TSS_Stream_Output))
9625 then
9626 Build_Record_Or_Elementary_Output_Procedure (Loc, Tag_Typ, Decl, Ent);
9627 Append_To (Res, Decl);
9628 end if;
9630 -- Ada 2005: Generate bodies for the following primitive operations for
9631 -- limited interfaces and synchronized types that implement a limited
9632 -- interface.
9634 -- disp_asynchronous_select
9635 -- disp_conditional_select
9636 -- disp_get_prim_op_kind
9637 -- disp_get_task_id
9638 -- disp_timed_select
9640 -- The interface versions will have null bodies
9642 -- Disable the generation of these bodies if No_Dispatching_Calls,
9643 -- Ravenscar or ZFP is active.
9645 -- In VM targets we define these primitives in all root tagged types
9646 -- that are not interface types. Done because in VM targets we don't
9647 -- have secondary dispatch tables and any derivation of Tag_Typ may
9648 -- cover limited interfaces (which always have these primitives since
9649 -- they may be ancestors of synchronized interface types).
9651 if Ada_Version >= Ada_2005
9652 and then not Is_Interface (Tag_Typ)
9653 and then
9654 ((Is_Interface (Etype (Tag_Typ))
9655 and then Is_Limited_Record (Etype (Tag_Typ)))
9656 or else
9657 (Is_Concurrent_Record_Type (Tag_Typ)
9658 and then Has_Interfaces (Tag_Typ))
9659 or else
9660 (not Tagged_Type_Expansion
9661 and then Tag_Typ = Root_Type (Tag_Typ)))
9662 and then not Restriction_Active (No_Dispatching_Calls)
9663 and then not Restriction_Active (No_Select_Statements)
9664 and then RTE_Available (RE_Select_Specific_Data)
9665 then
9666 Append_To (Res, Make_Disp_Asynchronous_Select_Body (Tag_Typ));
9667 Append_To (Res, Make_Disp_Conditional_Select_Body (Tag_Typ));
9668 Append_To (Res, Make_Disp_Get_Prim_Op_Kind_Body (Tag_Typ));
9669 Append_To (Res, Make_Disp_Get_Task_Id_Body (Tag_Typ));
9670 Append_To (Res, Make_Disp_Requeue_Body (Tag_Typ));
9671 Append_To (Res, Make_Disp_Timed_Select_Body (Tag_Typ));
9672 end if;
9674 if not Is_Limited_Type (Tag_Typ) and then not Is_Interface (Tag_Typ) then
9676 -- Body for equality
9678 if Eq_Needed then
9679 Decl := Make_Eq_Body (Tag_Typ, Eq_Name);
9680 Append_To (Res, Decl);
9681 end if;
9683 -- Body for inequality (if required)
9685 Decl := Make_Neq_Body (Tag_Typ);
9687 if Present (Decl) then
9688 Append_To (Res, Decl);
9689 end if;
9691 -- Body for dispatching assignment
9693 Decl :=
9694 Predef_Spec_Or_Body (Loc,
9695 Tag_Typ => Tag_Typ,
9696 Name => Name_uAssign,
9697 Profile => New_List (
9698 Make_Parameter_Specification (Loc,
9699 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
9700 Out_Present => True,
9701 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
9703 Make_Parameter_Specification (Loc,
9704 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
9705 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
9706 For_Body => True);
9708 Set_Handled_Statement_Sequence (Decl,
9709 Make_Handled_Sequence_Of_Statements (Loc, New_List (
9710 Make_Assignment_Statement (Loc,
9711 Name => Make_Identifier (Loc, Name_X),
9712 Expression => Make_Identifier (Loc, Name_Y)))));
9714 Append_To (Res, Decl);
9715 end if;
9717 -- Generate empty bodies of routines Deep_Adjust and Deep_Finalize for
9718 -- tagged types which do not contain controlled components.
9720 -- Do not generate the routines if finalization is disabled
9722 if Restriction_Active (No_Finalization) then
9723 null;
9725 elsif not Has_Controlled_Component (Tag_Typ) then
9726 if not Is_Limited_Type (Tag_Typ) then
9727 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust, True);
9729 if Is_Controlled (Tag_Typ) then
9730 Set_Handled_Statement_Sequence (Decl,
9731 Make_Handled_Sequence_Of_Statements (Loc,
9732 Statements => New_List (
9733 Make_Adjust_Call (
9734 Obj_Ref => Make_Identifier (Loc, Name_V),
9735 Typ => Tag_Typ))));
9737 else
9738 Set_Handled_Statement_Sequence (Decl,
9739 Make_Handled_Sequence_Of_Statements (Loc,
9740 Statements => New_List (
9741 Make_Null_Statement (Loc))));
9742 end if;
9744 Append_To (Res, Decl);
9745 end if;
9747 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize, True);
9749 if Is_Controlled (Tag_Typ) then
9750 Set_Handled_Statement_Sequence (Decl,
9751 Make_Handled_Sequence_Of_Statements (Loc,
9752 Statements => New_List (
9753 Make_Final_Call
9754 (Obj_Ref => Make_Identifier (Loc, Name_V),
9755 Typ => Tag_Typ))));
9757 else
9758 Set_Handled_Statement_Sequence (Decl,
9759 Make_Handled_Sequence_Of_Statements (Loc,
9760 Statements => New_List (Make_Null_Statement (Loc))));
9761 end if;
9763 Append_To (Res, Decl);
9764 end if;
9766 return Res;
9767 end Predefined_Primitive_Bodies;
9769 ---------------------------------
9770 -- Predefined_Primitive_Freeze --
9771 ---------------------------------
9773 function Predefined_Primitive_Freeze
9774 (Tag_Typ : Entity_Id) return List_Id
9776 Res : constant List_Id := New_List;
9777 Prim : Elmt_Id;
9778 Frnodes : List_Id;
9780 begin
9781 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
9782 while Present (Prim) loop
9783 if Is_Predefined_Dispatching_Operation (Node (Prim)) then
9784 Frnodes := Freeze_Entity (Node (Prim), Tag_Typ);
9786 if Present (Frnodes) then
9787 Append_List_To (Res, Frnodes);
9788 end if;
9789 end if;
9791 Next_Elmt (Prim);
9792 end loop;
9794 return Res;
9795 end Predefined_Primitive_Freeze;
9797 -------------------------
9798 -- Stream_Operation_OK --
9799 -------------------------
9801 function Stream_Operation_OK
9802 (Typ : Entity_Id;
9803 Operation : TSS_Name_Type) return Boolean
9805 Has_Predefined_Or_Specified_Stream_Attribute : Boolean := False;
9807 begin
9808 -- Special case of a limited type extension: a default implementation
9809 -- of the stream attributes Read or Write exists if that attribute
9810 -- has been specified or is available for an ancestor type; a default
9811 -- implementation of the attribute Output (resp. Input) exists if the
9812 -- attribute has been specified or Write (resp. Read) is available for
9813 -- an ancestor type. The last condition only applies under Ada 2005.
9815 if Is_Limited_Type (Typ) and then Is_Tagged_Type (Typ) then
9816 if Operation = TSS_Stream_Read then
9817 Has_Predefined_Or_Specified_Stream_Attribute :=
9818 Has_Specified_Stream_Read (Typ);
9820 elsif Operation = TSS_Stream_Write then
9821 Has_Predefined_Or_Specified_Stream_Attribute :=
9822 Has_Specified_Stream_Write (Typ);
9824 elsif Operation = TSS_Stream_Input then
9825 Has_Predefined_Or_Specified_Stream_Attribute :=
9826 Has_Specified_Stream_Input (Typ)
9827 or else
9828 (Ada_Version >= Ada_2005
9829 and then Stream_Operation_OK (Typ, TSS_Stream_Read));
9831 elsif Operation = TSS_Stream_Output then
9832 Has_Predefined_Or_Specified_Stream_Attribute :=
9833 Has_Specified_Stream_Output (Typ)
9834 or else
9835 (Ada_Version >= Ada_2005
9836 and then Stream_Operation_OK (Typ, TSS_Stream_Write));
9837 end if;
9839 -- Case of inherited TSS_Stream_Read or TSS_Stream_Write
9841 if not Has_Predefined_Or_Specified_Stream_Attribute
9842 and then Is_Derived_Type (Typ)
9843 and then (Operation = TSS_Stream_Read
9844 or else Operation = TSS_Stream_Write)
9845 then
9846 Has_Predefined_Or_Specified_Stream_Attribute :=
9847 Present
9848 (Find_Inherited_TSS (Base_Type (Etype (Typ)), Operation));
9849 end if;
9850 end if;
9852 -- If the type is not limited, or else is limited but the attribute is
9853 -- explicitly specified or is predefined for the type, then return True,
9854 -- unless other conditions prevail, such as restrictions prohibiting
9855 -- streams or dispatching operations. We also return True for limited
9856 -- interfaces, because they may be extended by nonlimited types and
9857 -- permit inheritance in this case (addresses cases where an abstract
9858 -- extension doesn't get 'Input declared, as per comments below, but
9859 -- 'Class'Input must still be allowed). Note that attempts to apply
9860 -- stream attributes to a limited interface or its class-wide type
9861 -- (or limited extensions thereof) will still get properly rejected
9862 -- by Check_Stream_Attribute.
9864 -- We exclude the Input operation from being a predefined subprogram in
9865 -- the case where the associated type is an abstract extension, because
9866 -- the attribute is not callable in that case, per 13.13.2(49/2). Also,
9867 -- we don't want an abstract version created because types derived from
9868 -- the abstract type may not even have Input available (for example if
9869 -- derived from a private view of the abstract type that doesn't have
9870 -- a visible Input).
9872 -- Do not generate stream routines for type Finalization_Master because
9873 -- a master may never appear in types and therefore cannot be read or
9874 -- written.
9876 return
9877 (not Is_Limited_Type (Typ)
9878 or else Is_Interface (Typ)
9879 or else Has_Predefined_Or_Specified_Stream_Attribute)
9880 and then
9881 (Operation /= TSS_Stream_Input
9882 or else not Is_Abstract_Type (Typ)
9883 or else not Is_Derived_Type (Typ))
9884 and then not Has_Unknown_Discriminants (Typ)
9885 and then not
9886 (Is_Interface (Typ)
9887 and then
9888 (Is_Task_Interface (Typ)
9889 or else Is_Protected_Interface (Typ)
9890 or else Is_Synchronized_Interface (Typ)))
9891 and then not Restriction_Active (No_Streams)
9892 and then not Restriction_Active (No_Dispatch)
9893 and then No (No_Tagged_Streams_Pragma (Typ))
9894 and then not No_Run_Time_Mode
9895 and then RTE_Available (RE_Tag)
9896 and then No (Type_Without_Stream_Operation (Typ))
9897 and then RTE_Available (RE_Root_Stream_Type)
9898 and then not Is_RTE (Typ, RE_Finalization_Master);
9899 end Stream_Operation_OK;
9901 end Exp_Ch3;