[NDS32] Implement bswapsi2 and bswaphi2 patterns.
[official-gcc.git] / gcc / ada / exp_ch3.adb
blobdb93b6453afecf45cb70654abb62bb4693e47117
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-2018, 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 Lib; use Lib;
47 with Namet; use Namet;
48 with Nlists; use Nlists;
49 with Nmake; use Nmake;
50 with Opt; use Opt;
51 with Restrict; use Restrict;
52 with Rident; use Rident;
53 with Rtsfind; use Rtsfind;
54 with Sem; use Sem;
55 with Sem_Aux; use Sem_Aux;
56 with Sem_Attr; use Sem_Attr;
57 with Sem_Cat; use Sem_Cat;
58 with Sem_Ch3; use Sem_Ch3;
59 with Sem_Ch6; use Sem_Ch6;
60 with Sem_Ch8; use Sem_Ch8;
61 with Sem_Disp; use Sem_Disp;
62 with Sem_Eval; use Sem_Eval;
63 with Sem_Mech; use Sem_Mech;
64 with Sem_Res; use Sem_Res;
65 with Sem_SCIL; use Sem_SCIL;
66 with Sem_Type; use Sem_Type;
67 with Sem_Util; use Sem_Util;
68 with Sinfo; use Sinfo;
69 with Stand; use Stand;
70 with Snames; use Snames;
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 Check_Stream_Attributes (Typ : Entity_Id);
135 -- Check that if a limited extension has a parent with user-defined stream
136 -- attributes, and does not itself have user-defined stream-attributes,
137 -- then any limited component of the extension also has the corresponding
138 -- user-defined stream attributes.
140 procedure Clean_Task_Names
141 (Typ : Entity_Id;
142 Proc_Id : Entity_Id);
143 -- If an initialization procedure includes calls to generate names
144 -- for task subcomponents, indicate that secondary stack cleanup is
145 -- needed after an initialization. Typ is the component type, and Proc_Id
146 -- the initialization procedure for the enclosing composite type.
148 procedure Expand_Freeze_Array_Type (N : Node_Id);
149 -- Freeze an array type. Deals with building the initialization procedure,
150 -- creating the packed array type for a packed array and also with the
151 -- creation of the controlling procedures for the controlled case. The
152 -- argument N is the N_Freeze_Entity node for the type.
154 procedure Expand_Freeze_Class_Wide_Type (N : Node_Id);
155 -- Freeze a class-wide type. Build routine Finalize_Address for the purpose
156 -- of finalizing controlled derivations from the class-wide's root type.
158 procedure Expand_Freeze_Enumeration_Type (N : Node_Id);
159 -- Freeze enumeration type with non-standard representation. Builds the
160 -- array and function needed to convert between enumeration pos and
161 -- enumeration representation values. N is the N_Freeze_Entity node
162 -- for the type.
164 procedure Expand_Freeze_Record_Type (N : Node_Id);
165 -- Freeze record type. Builds all necessary discriminant checking
166 -- and other ancillary functions, and builds dispatch tables where
167 -- needed. The argument N is the N_Freeze_Entity node. This processing
168 -- applies only to E_Record_Type entities, not to class wide types,
169 -- record subtypes, or private types.
171 procedure Expand_Tagged_Root (T : Entity_Id);
172 -- Add a field _Tag at the beginning of the record. This field carries
173 -- the value of the access to the Dispatch table. This procedure is only
174 -- called on root type, the _Tag field being inherited by the descendants.
176 procedure Freeze_Stream_Operations (N : Node_Id; Typ : Entity_Id);
177 -- Treat user-defined stream operations as renaming_as_body if the
178 -- subprogram they rename is not frozen when the type is frozen.
180 procedure Initialization_Warning (E : Entity_Id);
181 -- If static elaboration of the package is requested, indicate
182 -- when a type does meet the conditions for static initialization. If
183 -- E is a type, it has components that have no static initialization.
184 -- if E is an entity, its initial expression is not compile-time known.
186 function Init_Formals (Typ : Entity_Id) return List_Id;
187 -- This function builds the list of formals for an initialization routine.
188 -- The first formal is always _Init with the given type. For task value
189 -- record types and types containing tasks, three additional formals are
190 -- added:
192 -- _Master : Master_Id
193 -- _Chain : in out Activation_Chain
194 -- _Task_Name : String
196 -- The caller must append additional entries for discriminants if required.
198 function Inline_Init_Proc (Typ : Entity_Id) return Boolean;
199 -- Returns true if the initialization procedure of Typ should be inlined
201 function In_Runtime (E : Entity_Id) return Boolean;
202 -- Check if E is defined in the RTL (in a child of Ada or System). Used
203 -- to avoid to bring in the overhead of _Input, _Output for tagged types.
205 function Is_User_Defined_Equality (Prim : Node_Id) return Boolean;
206 -- Returns true if Prim is a user defined equality function
208 function Make_Eq_Body
209 (Typ : Entity_Id;
210 Eq_Name : Name_Id) return Node_Id;
211 -- Build the body of a primitive equality operation for a tagged record
212 -- type, or in Ada 2012 for any record type that has components with a
213 -- user-defined equality. Factored out of Predefined_Primitive_Bodies.
215 function Make_Eq_Case
216 (E : Entity_Id;
217 CL : Node_Id;
218 Discrs : Elist_Id := New_Elmt_List) return List_Id;
219 -- Building block for variant record equality. Defined to share the code
220 -- between the tagged and untagged case. Given a Component_List node CL,
221 -- it generates an 'if' followed by a 'case' statement that compares all
222 -- components of local temporaries named X and Y (that are declared as
223 -- formals at some upper level). E provides the Sloc to be used for the
224 -- generated code.
226 -- IF E is an unchecked_union, Discrs is the list of formals created for
227 -- the inferred discriminants of one operand. These formals are used in
228 -- the generated case statements for each variant of the unchecked union.
230 function Make_Eq_If
231 (E : Entity_Id;
232 L : List_Id) return Node_Id;
233 -- Building block for variant record equality. Defined to share the code
234 -- between the tagged and untagged case. Given the list of components
235 -- (or discriminants) L, it generates a return statement that compares all
236 -- components of local temporaries named X and Y (that are declared as
237 -- formals at some upper level). E provides the Sloc to be used for the
238 -- generated code.
240 function Make_Neq_Body (Tag_Typ : Entity_Id) return Node_Id;
241 -- Search for a renaming of the inequality dispatching primitive of
242 -- this tagged type. If found then build and return the corresponding
243 -- rename-as-body inequality subprogram; otherwise return Empty.
245 procedure Make_Predefined_Primitive_Specs
246 (Tag_Typ : Entity_Id;
247 Predef_List : out List_Id;
248 Renamed_Eq : out Entity_Id);
249 -- Create a list with the specs of the predefined primitive operations.
250 -- For tagged types that are interfaces all these primitives are defined
251 -- abstract.
253 -- The following entries are present for all tagged types, and provide
254 -- the results of the corresponding attribute applied to the object.
255 -- Dispatching is required in general, since the result of the attribute
256 -- will vary with the actual object subtype.
258 -- _size provides result of 'Size attribute
259 -- typSR provides result of 'Read attribute
260 -- typSW provides result of 'Write attribute
261 -- typSI provides result of 'Input attribute
262 -- typSO provides result of 'Output attribute
264 -- The following entries are additionally present for non-limited tagged
265 -- types, and implement additional dispatching operations for predefined
266 -- operations:
268 -- _equality implements "=" operator
269 -- _assign implements assignment operation
270 -- typDF implements deep finalization
271 -- typDA implements deep adjust
273 -- The latter two are empty procedures unless the type contains some
274 -- controlled components that require finalization actions (the deep
275 -- in the name refers to the fact that the action applies to components).
277 -- The list is returned in Predef_List. The Parameter Renamed_Eq either
278 -- returns the value Empty, or else the defining unit name for the
279 -- predefined equality function in the case where the type has a primitive
280 -- operation that is a renaming of predefined equality (but only if there
281 -- is also an overriding user-defined equality function). The returned
282 -- Renamed_Eq will be passed to the corresponding parameter of
283 -- Predefined_Primitive_Bodies.
285 function Has_New_Non_Standard_Rep (T : Entity_Id) return Boolean;
286 -- Returns True if there are representation clauses for type T that are not
287 -- inherited. If the result is false, the init_proc and the discriminant
288 -- checking functions of the parent can be reused by a derived type.
290 procedure Make_Controlling_Function_Wrappers
291 (Tag_Typ : Entity_Id;
292 Decl_List : out List_Id;
293 Body_List : out List_Id);
294 -- Ada 2005 (AI-391): Makes specs and bodies for the wrapper functions
295 -- associated with inherited functions with controlling results which
296 -- are not overridden. The body of each wrapper function consists solely
297 -- of a return statement whose expression is an extension aggregate
298 -- invoking the inherited subprogram's parent subprogram and extended
299 -- with a null association list.
301 function Make_Null_Procedure_Specs (Tag_Typ : Entity_Id) return List_Id;
302 -- Ada 2005 (AI-251): Makes specs for null procedures associated with any
303 -- null procedures inherited from an interface type that have not been
304 -- overridden. Only one null procedure will be created for a given set of
305 -- inherited null procedures with homographic profiles.
307 function Predef_Spec_Or_Body
308 (Loc : Source_Ptr;
309 Tag_Typ : Entity_Id;
310 Name : Name_Id;
311 Profile : List_Id;
312 Ret_Type : Entity_Id := Empty;
313 For_Body : Boolean := False) return Node_Id;
314 -- This function generates the appropriate expansion for a predefined
315 -- primitive operation specified by its name, parameter profile and
316 -- return type (Empty means this is a procedure). If For_Body is false,
317 -- then the returned node is a subprogram declaration. If For_Body is
318 -- true, then the returned node is a empty subprogram body containing
319 -- no declarations and no statements.
321 function Predef_Stream_Attr_Spec
322 (Loc : Source_Ptr;
323 Tag_Typ : Entity_Id;
324 Name : TSS_Name_Type;
325 For_Body : Boolean := False) return Node_Id;
326 -- Specialized version of Predef_Spec_Or_Body that apply to read, write,
327 -- input and output attribute whose specs are constructed in Exp_Strm.
329 function Predef_Deep_Spec
330 (Loc : Source_Ptr;
331 Tag_Typ : Entity_Id;
332 Name : TSS_Name_Type;
333 For_Body : Boolean := False) return Node_Id;
334 -- Specialized version of Predef_Spec_Or_Body that apply to _deep_adjust
335 -- and _deep_finalize
337 function Predefined_Primitive_Bodies
338 (Tag_Typ : Entity_Id;
339 Renamed_Eq : Entity_Id) return List_Id;
340 -- Create the bodies of the predefined primitives that are described in
341 -- Predefined_Primitive_Specs. When not empty, Renamed_Eq must denote
342 -- the defining unit name of the type's predefined equality as returned
343 -- by Make_Predefined_Primitive_Specs.
345 function Predefined_Primitive_Freeze (Tag_Typ : Entity_Id) return List_Id;
346 -- Freeze entities of all predefined primitive operations. This is needed
347 -- because the bodies of these operations do not normally do any freezing.
349 function Stream_Operation_OK
350 (Typ : Entity_Id;
351 Operation : TSS_Name_Type) return Boolean;
352 -- Check whether the named stream operation must be emitted for a given
353 -- type. The rules for inheritance of stream attributes by type extensions
354 -- are enforced by this function. Furthermore, various restrictions prevent
355 -- the generation of these operations, as a useful optimization or for
356 -- certification purposes and to save unnecessary generated code.
358 --------------------------
359 -- Adjust_Discriminants --
360 --------------------------
362 -- This procedure attempts to define subtypes for discriminants that are
363 -- more restrictive than those declared. Such a replacement is possible if
364 -- we can demonstrate that values outside the restricted range would cause
365 -- constraint errors in any case. The advantage of restricting the
366 -- discriminant types in this way is that the maximum size of the variant
367 -- record can be calculated more conservatively.
369 -- An example of a situation in which we can perform this type of
370 -- restriction is the following:
372 -- subtype B is range 1 .. 10;
373 -- type Q is array (B range <>) of Integer;
375 -- type V (N : Natural) is record
376 -- C : Q (1 .. N);
377 -- end record;
379 -- In this situation, we can restrict the upper bound of N to 10, since
380 -- any larger value would cause a constraint error in any case.
382 -- There are many situations in which such restriction is possible, but
383 -- for now, we just look for cases like the above, where the component
384 -- in question is a one dimensional array whose upper bound is one of
385 -- the record discriminants. Also the component must not be part of
386 -- any variant part, since then the component does not always exist.
388 procedure Adjust_Discriminants (Rtype : Entity_Id) is
389 Loc : constant Source_Ptr := Sloc (Rtype);
390 Comp : Entity_Id;
391 Ctyp : Entity_Id;
392 Ityp : Entity_Id;
393 Lo : Node_Id;
394 Hi : Node_Id;
395 P : Node_Id;
396 Loval : Uint;
397 Discr : Entity_Id;
398 Dtyp : Entity_Id;
399 Dhi : Node_Id;
400 Dhiv : Uint;
401 Ahi : Node_Id;
402 Ahiv : Uint;
403 Tnn : Entity_Id;
405 begin
406 Comp := First_Component (Rtype);
407 while Present (Comp) loop
409 -- If our parent is a variant, quit, we do not look at components
410 -- that are in variant parts, because they may not always exist.
412 P := Parent (Comp); -- component declaration
413 P := Parent (P); -- component list
415 exit when Nkind (Parent (P)) = N_Variant;
417 -- We are looking for a one dimensional array type
419 Ctyp := Etype (Comp);
421 if not Is_Array_Type (Ctyp) or else Number_Dimensions (Ctyp) > 1 then
422 goto Continue;
423 end if;
425 -- The lower bound must be constant, and the upper bound is a
426 -- discriminant (which is a discriminant of the current record).
428 Ityp := Etype (First_Index (Ctyp));
429 Lo := Type_Low_Bound (Ityp);
430 Hi := Type_High_Bound (Ityp);
432 if not Compile_Time_Known_Value (Lo)
433 or else Nkind (Hi) /= N_Identifier
434 or else No (Entity (Hi))
435 or else Ekind (Entity (Hi)) /= E_Discriminant
436 then
437 goto Continue;
438 end if;
440 -- We have an array with appropriate bounds
442 Loval := Expr_Value (Lo);
443 Discr := Entity (Hi);
444 Dtyp := Etype (Discr);
446 -- See if the discriminant has a known upper bound
448 Dhi := Type_High_Bound (Dtyp);
450 if not Compile_Time_Known_Value (Dhi) then
451 goto Continue;
452 end if;
454 Dhiv := Expr_Value (Dhi);
456 -- See if base type of component array has known upper bound
458 Ahi := Type_High_Bound (Etype (First_Index (Base_Type (Ctyp))));
460 if not Compile_Time_Known_Value (Ahi) then
461 goto Continue;
462 end if;
464 Ahiv := Expr_Value (Ahi);
466 -- The condition for doing the restriction is that the high bound
467 -- of the discriminant is greater than the low bound of the array,
468 -- and is also greater than the high bound of the base type index.
470 if Dhiv > Loval and then Dhiv > Ahiv then
472 -- We can reset the upper bound of the discriminant type to
473 -- whichever is larger, the low bound of the component, or
474 -- the high bound of the base type array index.
476 -- We build a subtype that is declared as
478 -- subtype Tnn is discr_type range discr_type'First .. max;
480 -- And insert this declaration into the tree. The type of the
481 -- discriminant is then reset to this more restricted subtype.
483 Tnn := Make_Temporary (Loc, 'T');
485 Insert_Action (Declaration_Node (Rtype),
486 Make_Subtype_Declaration (Loc,
487 Defining_Identifier => Tnn,
488 Subtype_Indication =>
489 Make_Subtype_Indication (Loc,
490 Subtype_Mark => New_Occurrence_Of (Dtyp, Loc),
491 Constraint =>
492 Make_Range_Constraint (Loc,
493 Range_Expression =>
494 Make_Range (Loc,
495 Low_Bound =>
496 Make_Attribute_Reference (Loc,
497 Attribute_Name => Name_First,
498 Prefix => New_Occurrence_Of (Dtyp, Loc)),
499 High_Bound =>
500 Make_Integer_Literal (Loc,
501 Intval => UI_Max (Loval, Ahiv)))))));
503 Set_Etype (Discr, Tnn);
504 end if;
506 <<Continue>>
507 Next_Component (Comp);
508 end loop;
509 end Adjust_Discriminants;
511 ---------------------------
512 -- Build_Array_Init_Proc --
513 ---------------------------
515 procedure Build_Array_Init_Proc (A_Type : Entity_Id; Nod : Node_Id) is
516 Comp_Type : constant Entity_Id := Component_Type (A_Type);
517 Comp_Simple_Init : constant Boolean :=
518 Needs_Simple_Initialization
519 (Typ => Comp_Type,
520 Consider_IS =>
521 not (Validity_Check_Copies and Is_Bit_Packed_Array (A_Type)));
522 -- True if the component needs simple initialization, based on its type,
523 -- plus the fact that we do not do simple initialization for components
524 -- of bit-packed arrays when validity checks are enabled, because the
525 -- initialization with deliberately out-of-range values would raise
526 -- Constraint_Error.
528 Body_Stmts : List_Id;
529 Has_Default_Init : Boolean;
530 Index_List : List_Id;
531 Loc : Source_Ptr;
532 Proc_Id : Entity_Id;
534 function Init_Component return List_Id;
535 -- Create one statement to initialize one array component, designated
536 -- by a full set of indexes.
538 function Init_One_Dimension (N : Int) return List_Id;
539 -- Create loop to initialize one dimension of the array. The single
540 -- statement in the loop body initializes the inner dimensions if any,
541 -- or else the single component. Note that this procedure is called
542 -- recursively, with N being the dimension to be initialized. A call
543 -- with N greater than the number of dimensions simply generates the
544 -- component initialization, terminating the recursion.
546 --------------------
547 -- Init_Component --
548 --------------------
550 function Init_Component return List_Id is
551 Comp : Node_Id;
553 begin
554 Comp :=
555 Make_Indexed_Component (Loc,
556 Prefix => Make_Identifier (Loc, Name_uInit),
557 Expressions => Index_List);
559 if Has_Default_Aspect (A_Type) then
560 Set_Assignment_OK (Comp);
561 return New_List (
562 Make_Assignment_Statement (Loc,
563 Name => Comp,
564 Expression =>
565 Convert_To (Comp_Type,
566 Default_Aspect_Component_Value (First_Subtype (A_Type)))));
568 elsif Comp_Simple_Init then
569 Set_Assignment_OK (Comp);
570 return New_List (
571 Make_Assignment_Statement (Loc,
572 Name => Comp,
573 Expression =>
574 Get_Simple_Init_Val
575 (Typ => Comp_Type,
576 N => Nod,
577 Size => Component_Size (A_Type))));
579 else
580 Clean_Task_Names (Comp_Type, Proc_Id);
581 return
582 Build_Initialization_Call
583 (Loc => Loc,
584 Id_Ref => Comp,
585 Typ => Comp_Type,
586 In_Init_Proc => True,
587 Enclos_Type => A_Type);
588 end if;
589 end Init_Component;
591 ------------------------
592 -- Init_One_Dimension --
593 ------------------------
595 function Init_One_Dimension (N : Int) return List_Id is
596 Index : Entity_Id;
598 begin
599 -- If the component does not need initializing, then there is nothing
600 -- to do here, so we return a null body. This occurs when generating
601 -- the dummy Init_Proc needed for Initialize_Scalars processing.
603 if not Has_Non_Null_Base_Init_Proc (Comp_Type)
604 and then not Comp_Simple_Init
605 and then not Has_Task (Comp_Type)
606 and then not Has_Default_Aspect (A_Type)
607 then
608 return New_List (Make_Null_Statement (Loc));
610 -- If all dimensions dealt with, we simply initialize the component
612 elsif N > Number_Dimensions (A_Type) then
613 return Init_Component;
615 -- Here we generate the required loop
617 else
618 Index :=
619 Make_Defining_Identifier (Loc, New_External_Name ('J', N));
621 Append (New_Occurrence_Of (Index, Loc), Index_List);
623 return New_List (
624 Make_Implicit_Loop_Statement (Nod,
625 Identifier => Empty,
626 Iteration_Scheme =>
627 Make_Iteration_Scheme (Loc,
628 Loop_Parameter_Specification =>
629 Make_Loop_Parameter_Specification (Loc,
630 Defining_Identifier => Index,
631 Discrete_Subtype_Definition =>
632 Make_Attribute_Reference (Loc,
633 Prefix =>
634 Make_Identifier (Loc, Name_uInit),
635 Attribute_Name => Name_Range,
636 Expressions => New_List (
637 Make_Integer_Literal (Loc, N))))),
638 Statements => Init_One_Dimension (N + 1)));
639 end if;
640 end Init_One_Dimension;
642 -- Start of processing for Build_Array_Init_Proc
644 begin
645 -- The init proc is created when analyzing the freeze node for the type,
646 -- but it properly belongs with the array type declaration. However, if
647 -- the freeze node is for a subtype of a type declared in another unit
648 -- it seems preferable to use the freeze node as the source location of
649 -- the init proc. In any case this is preferable for gcov usage, and
650 -- the Sloc is not otherwise used by the compiler.
652 if In_Open_Scopes (Scope (A_Type)) then
653 Loc := Sloc (A_Type);
654 else
655 Loc := Sloc (Nod);
656 end if;
658 -- Nothing to generate in the following cases:
660 -- 1. Initialization is suppressed for the type
661 -- 2. An initialization already exists for the base type
663 if Initialization_Suppressed (A_Type)
664 or else Present (Base_Init_Proc (A_Type))
665 then
666 return;
667 end if;
669 Index_List := New_List;
671 -- We need an initialization procedure if any of the following is true:
673 -- 1. The component type has an initialization procedure
674 -- 2. The component type needs simple initialization
675 -- 3. Tasks are present
676 -- 4. The type is marked as a public entity
677 -- 5. The array type has a Default_Component_Value aspect
679 -- The reason for the public entity test is to deal properly with the
680 -- Initialize_Scalars pragma. This pragma can be set in the client and
681 -- not in the declaring package, this means the client will make a call
682 -- to the initialization procedure (because one of conditions 1-3 must
683 -- apply in this case), and we must generate a procedure (even if it is
684 -- null) to satisfy the call in this case.
686 -- Exception: do not build an array init_proc for a type whose root
687 -- type is Standard.String or Standard.Wide_[Wide_]String, since there
688 -- is no place to put the code, and in any case we handle initialization
689 -- of such types (in the Initialize_Scalars case, that's the only time
690 -- the issue arises) in a special manner anyway which does not need an
691 -- init_proc.
693 Has_Default_Init := Has_Non_Null_Base_Init_Proc (Comp_Type)
694 or else Comp_Simple_Init
695 or else Has_Task (Comp_Type)
696 or else Has_Default_Aspect (A_Type);
698 if Has_Default_Init
699 or else (not Restriction_Active (No_Initialize_Scalars)
700 and then Is_Public (A_Type)
701 and then not Is_Standard_String_Type (A_Type))
702 then
703 Proc_Id :=
704 Make_Defining_Identifier (Loc,
705 Chars => Make_Init_Proc_Name (A_Type));
707 -- If No_Default_Initialization restriction is active, then we don't
708 -- want to build an init_proc, but we need to mark that an init_proc
709 -- would be needed if this restriction was not active (so that we can
710 -- detect attempts to call it), so set a dummy init_proc in place.
711 -- This is only done though when actual default initialization is
712 -- needed (and not done when only Is_Public is True), since otherwise
713 -- objects such as arrays of scalars could be wrongly flagged as
714 -- violating the restriction.
716 if Restriction_Active (No_Default_Initialization) then
717 if Has_Default_Init then
718 Set_Init_Proc (A_Type, Proc_Id);
719 end if;
721 return;
722 end if;
724 Body_Stmts := Init_One_Dimension (1);
726 Discard_Node (
727 Make_Subprogram_Body (Loc,
728 Specification =>
729 Make_Procedure_Specification (Loc,
730 Defining_Unit_Name => Proc_Id,
731 Parameter_Specifications => Init_Formals (A_Type)),
732 Declarations => New_List,
733 Handled_Statement_Sequence =>
734 Make_Handled_Sequence_Of_Statements (Loc,
735 Statements => Body_Stmts)));
737 Set_Ekind (Proc_Id, E_Procedure);
738 Set_Is_Public (Proc_Id, Is_Public (A_Type));
739 Set_Is_Internal (Proc_Id);
740 Set_Has_Completion (Proc_Id);
742 if not Debug_Generated_Code then
743 Set_Debug_Info_Off (Proc_Id);
744 end if;
746 -- Set Inlined on Init_Proc if it is set on the Init_Proc of the
747 -- component type itself (see also Build_Record_Init_Proc).
749 Set_Is_Inlined (Proc_Id, Inline_Init_Proc (Comp_Type));
751 -- Associate Init_Proc with type, and determine if the procedure
752 -- is null (happens because of the Initialize_Scalars pragma case,
753 -- where we have to generate a null procedure in case it is called
754 -- by a client with Initialize_Scalars set). Such procedures have
755 -- to be generated, but do not have to be called, so we mark them
756 -- as null to suppress the call.
758 Set_Init_Proc (A_Type, Proc_Id);
760 if List_Length (Body_Stmts) = 1
762 -- We must skip SCIL nodes because they may have been added to this
763 -- list by Insert_Actions.
765 and then Nkind (First_Non_SCIL_Node (Body_Stmts)) = N_Null_Statement
766 then
767 Set_Is_Null_Init_Proc (Proc_Id);
769 else
770 -- Try to build a static aggregate to statically initialize
771 -- objects of the type. This can only be done for constrained
772 -- one-dimensional arrays with static bounds.
774 Set_Static_Initialization
775 (Proc_Id,
776 Build_Equivalent_Array_Aggregate (First_Subtype (A_Type)));
777 end if;
778 end if;
779 end Build_Array_Init_Proc;
781 --------------------------------
782 -- Build_Discr_Checking_Funcs --
783 --------------------------------
785 procedure Build_Discr_Checking_Funcs (N : Node_Id) is
786 Rec_Id : Entity_Id;
787 Loc : Source_Ptr;
788 Enclosing_Func_Id : Entity_Id;
789 Sequence : Nat := 1;
790 Type_Def : Node_Id;
791 V : Node_Id;
793 function Build_Case_Statement
794 (Case_Id : Entity_Id;
795 Variant : Node_Id) return Node_Id;
796 -- Build a case statement containing only two alternatives. The first
797 -- alternative corresponds exactly to the discrete choices given on the
798 -- variant with contains the components that we are generating the
799 -- checks for. If the discriminant is one of these return False. The
800 -- second alternative is an OTHERS choice that will return True
801 -- indicating the discriminant did not match.
803 function Build_Dcheck_Function
804 (Case_Id : Entity_Id;
805 Variant : Node_Id) return Entity_Id;
806 -- Build the discriminant checking function for a given variant
808 procedure Build_Dcheck_Functions (Variant_Part_Node : Node_Id);
809 -- Builds the discriminant checking function for each variant of the
810 -- given variant part of the record type.
812 --------------------------
813 -- Build_Case_Statement --
814 --------------------------
816 function Build_Case_Statement
817 (Case_Id : Entity_Id;
818 Variant : Node_Id) return Node_Id
820 Alt_List : constant List_Id := New_List;
821 Actuals_List : List_Id;
822 Case_Node : Node_Id;
823 Case_Alt_Node : Node_Id;
824 Choice : Node_Id;
825 Choice_List : List_Id;
826 D : Entity_Id;
827 Return_Node : Node_Id;
829 begin
830 Case_Node := New_Node (N_Case_Statement, Loc);
832 -- Replace the discriminant which controls the variant with the name
833 -- of the formal of the checking function.
835 Set_Expression (Case_Node, Make_Identifier (Loc, Chars (Case_Id)));
837 Choice := First (Discrete_Choices (Variant));
839 if Nkind (Choice) = N_Others_Choice then
840 Choice_List := New_Copy_List (Others_Discrete_Choices (Choice));
841 else
842 Choice_List := New_Copy_List (Discrete_Choices (Variant));
843 end if;
845 if not Is_Empty_List (Choice_List) then
846 Case_Alt_Node := New_Node (N_Case_Statement_Alternative, Loc);
847 Set_Discrete_Choices (Case_Alt_Node, Choice_List);
849 -- In case this is a nested variant, we need to return the result
850 -- of the discriminant checking function for the immediately
851 -- enclosing variant.
853 if Present (Enclosing_Func_Id) then
854 Actuals_List := New_List;
856 D := First_Discriminant (Rec_Id);
857 while Present (D) loop
858 Append (Make_Identifier (Loc, Chars (D)), Actuals_List);
859 Next_Discriminant (D);
860 end loop;
862 Return_Node :=
863 Make_Simple_Return_Statement (Loc,
864 Expression =>
865 Make_Function_Call (Loc,
866 Name =>
867 New_Occurrence_Of (Enclosing_Func_Id, Loc),
868 Parameter_Associations =>
869 Actuals_List));
871 else
872 Return_Node :=
873 Make_Simple_Return_Statement (Loc,
874 Expression =>
875 New_Occurrence_Of (Standard_False, Loc));
876 end if;
878 Set_Statements (Case_Alt_Node, New_List (Return_Node));
879 Append (Case_Alt_Node, Alt_List);
880 end if;
882 Case_Alt_Node := New_Node (N_Case_Statement_Alternative, Loc);
883 Choice_List := New_List (New_Node (N_Others_Choice, Loc));
884 Set_Discrete_Choices (Case_Alt_Node, Choice_List);
886 Return_Node :=
887 Make_Simple_Return_Statement (Loc,
888 Expression =>
889 New_Occurrence_Of (Standard_True, Loc));
891 Set_Statements (Case_Alt_Node, New_List (Return_Node));
892 Append (Case_Alt_Node, Alt_List);
894 Set_Alternatives (Case_Node, Alt_List);
895 return Case_Node;
896 end Build_Case_Statement;
898 ---------------------------
899 -- Build_Dcheck_Function --
900 ---------------------------
902 function Build_Dcheck_Function
903 (Case_Id : Entity_Id;
904 Variant : Node_Id) return Entity_Id
906 Body_Node : Node_Id;
907 Func_Id : Entity_Id;
908 Parameter_List : List_Id;
909 Spec_Node : Node_Id;
911 begin
912 Body_Node := New_Node (N_Subprogram_Body, Loc);
913 Sequence := Sequence + 1;
915 Func_Id :=
916 Make_Defining_Identifier (Loc,
917 Chars => New_External_Name (Chars (Rec_Id), 'D', Sequence));
918 Set_Is_Discriminant_Check_Function (Func_Id);
920 Spec_Node := New_Node (N_Function_Specification, Loc);
921 Set_Defining_Unit_Name (Spec_Node, Func_Id);
923 Parameter_List := Build_Discriminant_Formals (Rec_Id, False);
925 Set_Parameter_Specifications (Spec_Node, Parameter_List);
926 Set_Result_Definition (Spec_Node,
927 New_Occurrence_Of (Standard_Boolean, Loc));
928 Set_Specification (Body_Node, Spec_Node);
929 Set_Declarations (Body_Node, New_List);
931 Set_Handled_Statement_Sequence (Body_Node,
932 Make_Handled_Sequence_Of_Statements (Loc,
933 Statements => New_List (
934 Build_Case_Statement (Case_Id, Variant))));
936 Set_Ekind (Func_Id, E_Function);
937 Set_Mechanism (Func_Id, Default_Mechanism);
938 Set_Is_Inlined (Func_Id, True);
939 Set_Is_Pure (Func_Id, True);
940 Set_Is_Public (Func_Id, Is_Public (Rec_Id));
941 Set_Is_Internal (Func_Id, True);
943 if not Debug_Generated_Code then
944 Set_Debug_Info_Off (Func_Id);
945 end if;
947 Analyze (Body_Node);
949 Append_Freeze_Action (Rec_Id, Body_Node);
950 Set_Dcheck_Function (Variant, Func_Id);
951 return Func_Id;
952 end Build_Dcheck_Function;
954 ----------------------------
955 -- Build_Dcheck_Functions --
956 ----------------------------
958 procedure Build_Dcheck_Functions (Variant_Part_Node : Node_Id) is
959 Component_List_Node : Node_Id;
960 Decl : Entity_Id;
961 Discr_Name : Entity_Id;
962 Func_Id : Entity_Id;
963 Variant : Node_Id;
964 Saved_Enclosing_Func_Id : Entity_Id;
966 begin
967 -- Build the discriminant-checking function for each variant, and
968 -- label all components of that variant with the function's name.
969 -- We only Generate a discriminant-checking function when the
970 -- variant is not empty, to prevent the creation of dead code.
972 Discr_Name := Entity (Name (Variant_Part_Node));
973 Variant := First_Non_Pragma (Variants (Variant_Part_Node));
975 while Present (Variant) loop
976 Component_List_Node := Component_List (Variant);
978 if not Null_Present (Component_List_Node) then
979 Func_Id := Build_Dcheck_Function (Discr_Name, Variant);
981 Decl :=
982 First_Non_Pragma (Component_Items (Component_List_Node));
983 while Present (Decl) loop
984 Set_Discriminant_Checking_Func
985 (Defining_Identifier (Decl), Func_Id);
986 Next_Non_Pragma (Decl);
987 end loop;
989 if Present (Variant_Part (Component_List_Node)) then
990 Saved_Enclosing_Func_Id := Enclosing_Func_Id;
991 Enclosing_Func_Id := Func_Id;
992 Build_Dcheck_Functions (Variant_Part (Component_List_Node));
993 Enclosing_Func_Id := Saved_Enclosing_Func_Id;
994 end if;
995 end if;
997 Next_Non_Pragma (Variant);
998 end loop;
999 end Build_Dcheck_Functions;
1001 -- Start of processing for Build_Discr_Checking_Funcs
1003 begin
1004 -- Only build if not done already
1006 if not Discr_Check_Funcs_Built (N) then
1007 Type_Def := Type_Definition (N);
1009 if Nkind (Type_Def) = N_Record_Definition then
1010 if No (Component_List (Type_Def)) then -- null record.
1011 return;
1012 else
1013 V := Variant_Part (Component_List (Type_Def));
1014 end if;
1016 else pragma Assert (Nkind (Type_Def) = N_Derived_Type_Definition);
1017 if No (Component_List (Record_Extension_Part (Type_Def))) then
1018 return;
1019 else
1020 V := Variant_Part
1021 (Component_List (Record_Extension_Part (Type_Def)));
1022 end if;
1023 end if;
1025 Rec_Id := Defining_Identifier (N);
1027 if Present (V) and then not Is_Unchecked_Union (Rec_Id) then
1028 Loc := Sloc (N);
1029 Enclosing_Func_Id := Empty;
1030 Build_Dcheck_Functions (V);
1031 end if;
1033 Set_Discr_Check_Funcs_Built (N);
1034 end if;
1035 end Build_Discr_Checking_Funcs;
1037 --------------------------------
1038 -- Build_Discriminant_Formals --
1039 --------------------------------
1041 function Build_Discriminant_Formals
1042 (Rec_Id : Entity_Id;
1043 Use_Dl : Boolean) return List_Id
1045 Loc : Source_Ptr := Sloc (Rec_Id);
1046 Parameter_List : constant List_Id := New_List;
1047 D : Entity_Id;
1048 Formal : Entity_Id;
1049 Formal_Type : Entity_Id;
1050 Param_Spec_Node : Node_Id;
1052 begin
1053 if Has_Discriminants (Rec_Id) then
1054 D := First_Discriminant (Rec_Id);
1055 while Present (D) loop
1056 Loc := Sloc (D);
1058 if Use_Dl then
1059 Formal := Discriminal (D);
1060 Formal_Type := Etype (Formal);
1061 else
1062 Formal := Make_Defining_Identifier (Loc, Chars (D));
1063 Formal_Type := Etype (D);
1064 end if;
1066 Param_Spec_Node :=
1067 Make_Parameter_Specification (Loc,
1068 Defining_Identifier => Formal,
1069 Parameter_Type =>
1070 New_Occurrence_Of (Formal_Type, Loc));
1071 Append (Param_Spec_Node, Parameter_List);
1072 Next_Discriminant (D);
1073 end loop;
1074 end if;
1076 return Parameter_List;
1077 end Build_Discriminant_Formals;
1079 --------------------------------------
1080 -- Build_Equivalent_Array_Aggregate --
1081 --------------------------------------
1083 function Build_Equivalent_Array_Aggregate (T : Entity_Id) return Node_Id is
1084 Loc : constant Source_Ptr := Sloc (T);
1085 Comp_Type : constant Entity_Id := Component_Type (T);
1086 Index_Type : constant Entity_Id := Etype (First_Index (T));
1087 Proc : constant Entity_Id := Base_Init_Proc (T);
1088 Lo, Hi : Node_Id;
1089 Aggr : Node_Id;
1090 Expr : Node_Id;
1092 begin
1093 if not Is_Constrained (T)
1094 or else Number_Dimensions (T) > 1
1095 or else No (Proc)
1096 then
1097 Initialization_Warning (T);
1098 return Empty;
1099 end if;
1101 Lo := Type_Low_Bound (Index_Type);
1102 Hi := Type_High_Bound (Index_Type);
1104 if not Compile_Time_Known_Value (Lo)
1105 or else not Compile_Time_Known_Value (Hi)
1106 then
1107 Initialization_Warning (T);
1108 return Empty;
1109 end if;
1111 if Is_Record_Type (Comp_Type)
1112 and then Present (Base_Init_Proc (Comp_Type))
1113 then
1114 Expr := Static_Initialization (Base_Init_Proc (Comp_Type));
1116 if No (Expr) then
1117 Initialization_Warning (T);
1118 return Empty;
1119 end if;
1121 else
1122 Initialization_Warning (T);
1123 return Empty;
1124 end if;
1126 Aggr := Make_Aggregate (Loc, No_List, New_List);
1127 Set_Etype (Aggr, T);
1128 Set_Aggregate_Bounds (Aggr,
1129 Make_Range (Loc,
1130 Low_Bound => New_Copy (Lo),
1131 High_Bound => New_Copy (Hi)));
1132 Set_Parent (Aggr, Parent (Proc));
1134 Append_To (Component_Associations (Aggr),
1135 Make_Component_Association (Loc,
1136 Choices =>
1137 New_List (
1138 Make_Range (Loc,
1139 Low_Bound => New_Copy (Lo),
1140 High_Bound => New_Copy (Hi))),
1141 Expression => Expr));
1143 if Static_Array_Aggregate (Aggr) then
1144 return Aggr;
1145 else
1146 Initialization_Warning (T);
1147 return Empty;
1148 end if;
1149 end Build_Equivalent_Array_Aggregate;
1151 ---------------------------------------
1152 -- Build_Equivalent_Record_Aggregate --
1153 ---------------------------------------
1155 function Build_Equivalent_Record_Aggregate (T : Entity_Id) return Node_Id is
1156 Agg : Node_Id;
1157 Comp : Entity_Id;
1158 Comp_Type : Entity_Id;
1160 -- Start of processing for Build_Equivalent_Record_Aggregate
1162 begin
1163 if not Is_Record_Type (T)
1164 or else Has_Discriminants (T)
1165 or else Is_Limited_Type (T)
1166 or else Has_Non_Standard_Rep (T)
1167 then
1168 Initialization_Warning (T);
1169 return Empty;
1170 end if;
1172 Comp := First_Component (T);
1174 -- A null record needs no warning
1176 if No (Comp) then
1177 return Empty;
1178 end if;
1180 while Present (Comp) loop
1182 -- Array components are acceptable if initialized by a positional
1183 -- aggregate with static components.
1185 if Is_Array_Type (Etype (Comp)) then
1186 Comp_Type := Component_Type (Etype (Comp));
1188 if Nkind (Parent (Comp)) /= N_Component_Declaration
1189 or else No (Expression (Parent (Comp)))
1190 or else Nkind (Expression (Parent (Comp))) /= N_Aggregate
1191 then
1192 Initialization_Warning (T);
1193 return Empty;
1195 elsif Is_Scalar_Type (Component_Type (Etype (Comp)))
1196 and then
1197 (not Compile_Time_Known_Value (Type_Low_Bound (Comp_Type))
1198 or else
1199 not Compile_Time_Known_Value (Type_High_Bound (Comp_Type)))
1200 then
1201 Initialization_Warning (T);
1202 return Empty;
1204 elsif
1205 not Static_Array_Aggregate (Expression (Parent (Comp)))
1206 then
1207 Initialization_Warning (T);
1208 return Empty;
1209 end if;
1211 elsif Is_Scalar_Type (Etype (Comp)) then
1212 Comp_Type := Etype (Comp);
1214 if Nkind (Parent (Comp)) /= N_Component_Declaration
1215 or else No (Expression (Parent (Comp)))
1216 or else not Compile_Time_Known_Value (Expression (Parent (Comp)))
1217 or else not Compile_Time_Known_Value (Type_Low_Bound (Comp_Type))
1218 or else not
1219 Compile_Time_Known_Value (Type_High_Bound (Comp_Type))
1220 then
1221 Initialization_Warning (T);
1222 return Empty;
1223 end if;
1225 -- For now, other types are excluded
1227 else
1228 Initialization_Warning (T);
1229 return Empty;
1230 end if;
1232 Next_Component (Comp);
1233 end loop;
1235 -- All components have static initialization. Build positional aggregate
1236 -- from the given expressions or defaults.
1238 Agg := Make_Aggregate (Sloc (T), New_List, New_List);
1239 Set_Parent (Agg, Parent (T));
1241 Comp := First_Component (T);
1242 while Present (Comp) loop
1243 Append
1244 (New_Copy_Tree (Expression (Parent (Comp))), Expressions (Agg));
1245 Next_Component (Comp);
1246 end loop;
1248 Analyze_And_Resolve (Agg, T);
1249 return Agg;
1250 end Build_Equivalent_Record_Aggregate;
1252 -------------------------------
1253 -- Build_Initialization_Call --
1254 -------------------------------
1256 -- References to a discriminant inside the record type declaration can
1257 -- appear either in the subtype_indication to constrain a record or an
1258 -- array, or as part of a larger expression given for the initial value
1259 -- of a component. In both of these cases N appears in the record
1260 -- initialization procedure and needs to be replaced by the formal
1261 -- parameter of the initialization procedure which corresponds to that
1262 -- discriminant.
1264 -- In the example below, references to discriminants D1 and D2 in proc_1
1265 -- are replaced by references to formals with the same name
1266 -- (discriminals)
1268 -- A similar replacement is done for calls to any record initialization
1269 -- procedure for any components that are themselves of a record type.
1271 -- type R (D1, D2 : Integer) is record
1272 -- X : Integer := F * D1;
1273 -- Y : Integer := F * D2;
1274 -- end record;
1276 -- procedure proc_1 (Out_2 : out R; D1 : Integer; D2 : Integer) is
1277 -- begin
1278 -- Out_2.D1 := D1;
1279 -- Out_2.D2 := D2;
1280 -- Out_2.X := F * D1;
1281 -- Out_2.Y := F * D2;
1282 -- end;
1284 function Build_Initialization_Call
1285 (Loc : Source_Ptr;
1286 Id_Ref : Node_Id;
1287 Typ : Entity_Id;
1288 In_Init_Proc : Boolean := False;
1289 Enclos_Type : Entity_Id := Empty;
1290 Discr_Map : Elist_Id := New_Elmt_List;
1291 With_Default_Init : Boolean := False;
1292 Constructor_Ref : Node_Id := Empty) return List_Id
1294 Res : constant List_Id := New_List;
1296 Full_Type : Entity_Id;
1298 procedure Check_Predicated_Discriminant
1299 (Val : Node_Id;
1300 Discr : Entity_Id);
1301 -- Discriminants whose subtypes have predicates are checked in two
1302 -- cases:
1303 -- a) When an object is default-initialized and assertions are enabled
1304 -- we check that the value of the discriminant obeys the predicate.
1306 -- b) In all cases, if the discriminant controls a variant and the
1307 -- variant has no others_choice, Constraint_Error must be raised if
1308 -- the predicate is violated, because there is no variant covered
1309 -- by the illegal discriminant value.
1311 -----------------------------------
1312 -- Check_Predicated_Discriminant --
1313 -----------------------------------
1315 procedure Check_Predicated_Discriminant
1316 (Val : Node_Id;
1317 Discr : Entity_Id)
1319 Typ : constant Entity_Id := Etype (Discr);
1321 procedure Check_Missing_Others (V : Node_Id);
1322 -- ???
1324 --------------------------
1325 -- Check_Missing_Others --
1326 --------------------------
1328 procedure Check_Missing_Others (V : Node_Id) is
1329 Alt : Node_Id;
1330 Choice : Node_Id;
1331 Last_Var : Node_Id;
1333 begin
1334 Last_Var := Last_Non_Pragma (Variants (V));
1335 Choice := First (Discrete_Choices (Last_Var));
1337 -- An others_choice is added during expansion for gcc use, but
1338 -- does not cover the illegality.
1340 if Entity (Name (V)) = Discr then
1341 if Present (Choice)
1342 and then (Nkind (Choice) /= N_Others_Choice
1343 or else not Comes_From_Source (Choice))
1344 then
1345 Check_Expression_Against_Static_Predicate (Val, Typ);
1347 if not Is_Static_Expression (Val) then
1348 Prepend_To (Res,
1349 Make_Raise_Constraint_Error (Loc,
1350 Condition =>
1351 Make_Op_Not (Loc,
1352 Right_Opnd => Make_Predicate_Call (Typ, Val)),
1353 Reason => CE_Invalid_Data));
1354 end if;
1355 end if;
1356 end if;
1358 -- Check whether some nested variant is ruled by the predicated
1359 -- discriminant.
1361 Alt := First (Variants (V));
1362 while Present (Alt) loop
1363 if Nkind (Alt) = N_Variant
1364 and then Present (Variant_Part (Component_List (Alt)))
1365 then
1366 Check_Missing_Others
1367 (Variant_Part (Component_List (Alt)));
1368 end if;
1370 Next (Alt);
1371 end loop;
1372 end Check_Missing_Others;
1374 -- Local variables
1376 Def : Node_Id;
1378 -- Start of processing for Check_Predicated_Discriminant
1380 begin
1381 if Ekind (Base_Type (Full_Type)) = E_Record_Type then
1382 Def := Type_Definition (Parent (Base_Type (Full_Type)));
1383 else
1384 return;
1385 end if;
1387 if Policy_In_Effect (Name_Assert) = Name_Check
1388 and then not Predicates_Ignored (Etype (Discr))
1389 then
1390 Prepend_To (Res, Make_Predicate_Check (Typ, Val));
1391 end if;
1393 -- If discriminant controls a variant, verify that predicate is
1394 -- obeyed or else an Others_Choice is present.
1396 if Nkind (Def) = N_Record_Definition
1397 and then Present (Variant_Part (Component_List (Def)))
1398 and then Policy_In_Effect (Name_Assert) = Name_Ignore
1399 then
1400 Check_Missing_Others (Variant_Part (Component_List (Def)));
1401 end if;
1402 end Check_Predicated_Discriminant;
1404 -- Local variables
1406 Arg : Node_Id;
1407 Args : List_Id;
1408 Decls : List_Id;
1409 Decl : Node_Id;
1410 Discr : Entity_Id;
1411 First_Arg : Node_Id;
1412 Full_Init_Type : Entity_Id;
1413 Init_Call : Node_Id;
1414 Init_Type : Entity_Id;
1415 Proc : Entity_Id;
1417 -- Start of processing for Build_Initialization_Call
1419 begin
1420 pragma Assert (Constructor_Ref = Empty
1421 or else Is_CPP_Constructor_Call (Constructor_Ref));
1423 if No (Constructor_Ref) then
1424 Proc := Base_Init_Proc (Typ);
1425 else
1426 Proc := Base_Init_Proc (Typ, Entity (Name (Constructor_Ref)));
1427 end if;
1429 pragma Assert (Present (Proc));
1430 Init_Type := Etype (First_Formal (Proc));
1431 Full_Init_Type := Underlying_Type (Init_Type);
1433 -- Nothing to do if the Init_Proc is null, unless Initialize_Scalars
1434 -- is active (in which case we make the call anyway, since in the
1435 -- actual compiled client it may be non null).
1437 if Is_Null_Init_Proc (Proc) and then not Init_Or_Norm_Scalars then
1438 return Empty_List;
1440 -- Nothing to do for an array of controlled components that have only
1441 -- the inherited Initialize primitive. This is a useful optimization
1442 -- for CodePeer.
1444 elsif Is_Trivial_Subprogram (Proc)
1445 and then Is_Array_Type (Full_Init_Type)
1446 then
1447 return New_List (Make_Null_Statement (Loc));
1448 end if;
1450 -- Use the [underlying] full view when dealing with a private type. This
1451 -- may require several steps depending on derivations.
1453 Full_Type := Typ;
1454 loop
1455 if Is_Private_Type (Full_Type) then
1456 if Present (Full_View (Full_Type)) then
1457 Full_Type := Full_View (Full_Type);
1459 elsif Present (Underlying_Full_View (Full_Type)) then
1460 Full_Type := Underlying_Full_View (Full_Type);
1462 -- When a private type acts as a generic actual and lacks a full
1463 -- view, use the base type.
1465 elsif Is_Generic_Actual_Type (Full_Type) then
1466 Full_Type := Base_Type (Full_Type);
1468 elsif Ekind (Full_Type) = E_Private_Subtype
1469 and then (not Has_Discriminants (Full_Type)
1470 or else No (Discriminant_Constraint (Full_Type)))
1471 then
1472 Full_Type := Etype (Full_Type);
1474 -- The loop has recovered the [underlying] full view, stop the
1475 -- traversal.
1477 else
1478 exit;
1479 end if;
1481 -- The type is not private, nothing to do
1483 else
1484 exit;
1485 end if;
1486 end loop;
1488 -- If Typ is derived, the procedure is the initialization procedure for
1489 -- the root type. Wrap the argument in an conversion to make it type
1490 -- honest. Actually it isn't quite type honest, because there can be
1491 -- conflicts of views in the private type case. That is why we set
1492 -- Conversion_OK in the conversion node.
1494 if (Is_Record_Type (Typ)
1495 or else Is_Array_Type (Typ)
1496 or else Is_Private_Type (Typ))
1497 and then Init_Type /= Base_Type (Typ)
1498 then
1499 First_Arg := OK_Convert_To (Etype (Init_Type), Id_Ref);
1500 Set_Etype (First_Arg, Init_Type);
1502 else
1503 First_Arg := Id_Ref;
1504 end if;
1506 Args := New_List (Convert_Concurrent (First_Arg, Typ));
1508 -- In the tasks case, add _Master as the value of the _Master parameter
1509 -- and _Chain as the value of the _Chain parameter. At the outer level,
1510 -- these will be variables holding the corresponding values obtained
1511 -- from GNARL. At inner levels, they will be the parameters passed down
1512 -- through the outer routines.
1514 if Has_Task (Full_Type) then
1515 if Restriction_Active (No_Task_Hierarchy) then
1516 Append_To (Args,
1517 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
1518 else
1519 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
1520 end if;
1522 -- Add _Chain (not done for sequential elaboration policy, see
1523 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
1525 if Partition_Elaboration_Policy /= 'S' then
1526 Append_To (Args, Make_Identifier (Loc, Name_uChain));
1527 end if;
1529 -- Ada 2005 (AI-287): In case of default initialized components
1530 -- with tasks, we generate a null string actual parameter.
1531 -- This is just a workaround that must be improved later???
1533 if With_Default_Init then
1534 Append_To (Args,
1535 Make_String_Literal (Loc,
1536 Strval => ""));
1538 else
1539 Decls :=
1540 Build_Task_Image_Decls (Loc, Id_Ref, Enclos_Type, In_Init_Proc);
1541 Decl := Last (Decls);
1543 Append_To (Args,
1544 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
1545 Append_List (Decls, Res);
1546 end if;
1548 else
1549 Decls := No_List;
1550 Decl := Empty;
1551 end if;
1553 -- Add discriminant values if discriminants are present
1555 if Has_Discriminants (Full_Init_Type) then
1556 Discr := First_Discriminant (Full_Init_Type);
1557 while Present (Discr) loop
1559 -- If this is a discriminated concurrent type, the init_proc
1560 -- for the corresponding record is being called. Use that type
1561 -- directly to find the discriminant value, to handle properly
1562 -- intervening renamed discriminants.
1564 declare
1565 T : Entity_Id := Full_Type;
1567 begin
1568 if Is_Protected_Type (T) then
1569 T := Corresponding_Record_Type (T);
1570 end if;
1572 Arg :=
1573 Get_Discriminant_Value (
1574 Discr,
1576 Discriminant_Constraint (Full_Type));
1577 end;
1579 -- If the target has access discriminants, and is constrained by
1580 -- an access to the enclosing construct, i.e. a current instance,
1581 -- replace the reference to the type by a reference to the object.
1583 if Nkind (Arg) = N_Attribute_Reference
1584 and then Is_Access_Type (Etype (Arg))
1585 and then Is_Entity_Name (Prefix (Arg))
1586 and then Is_Type (Entity (Prefix (Arg)))
1587 then
1588 Arg :=
1589 Make_Attribute_Reference (Loc,
1590 Prefix => New_Copy (Prefix (Id_Ref)),
1591 Attribute_Name => Name_Unrestricted_Access);
1593 elsif In_Init_Proc then
1595 -- Replace any possible references to the discriminant in the
1596 -- call to the record initialization procedure with references
1597 -- to the appropriate formal parameter.
1599 if Nkind (Arg) = N_Identifier
1600 and then Ekind (Entity (Arg)) = E_Discriminant
1601 then
1602 Arg := New_Occurrence_Of (Discriminal (Entity (Arg)), Loc);
1604 -- Otherwise make a copy of the default expression. Note that
1605 -- we use the current Sloc for this, because we do not want the
1606 -- call to appear to be at the declaration point. Within the
1607 -- expression, replace discriminants with their discriminals.
1609 else
1610 Arg :=
1611 New_Copy_Tree (Arg, Map => Discr_Map, New_Sloc => Loc);
1612 end if;
1614 else
1615 if Is_Constrained (Full_Type) then
1616 Arg := Duplicate_Subexpr_No_Checks (Arg);
1617 else
1618 -- The constraints come from the discriminant default exps,
1619 -- they must be reevaluated, so we use New_Copy_Tree but we
1620 -- ensure the proper Sloc (for any embedded calls).
1621 -- In addition, if a predicate check is needed on the value
1622 -- of the discriminant, insert it ahead of the call.
1624 Arg := New_Copy_Tree (Arg, New_Sloc => Loc);
1625 end if;
1627 if Has_Predicates (Etype (Discr)) then
1628 Check_Predicated_Discriminant (Arg, Discr);
1629 end if;
1630 end if;
1632 -- Ada 2005 (AI-287): In case of default initialized components,
1633 -- if the component is constrained with a discriminant of the
1634 -- enclosing type, we need to generate the corresponding selected
1635 -- component node to access the discriminant value. In other cases
1636 -- this is not required, either because we are inside the init
1637 -- proc and we use the corresponding formal, or else because the
1638 -- component is constrained by an expression.
1640 if With_Default_Init
1641 and then Nkind (Id_Ref) = N_Selected_Component
1642 and then Nkind (Arg) = N_Identifier
1643 and then Ekind (Entity (Arg)) = E_Discriminant
1644 then
1645 Append_To (Args,
1646 Make_Selected_Component (Loc,
1647 Prefix => New_Copy_Tree (Prefix (Id_Ref)),
1648 Selector_Name => Arg));
1649 else
1650 Append_To (Args, Arg);
1651 end if;
1653 Next_Discriminant (Discr);
1654 end loop;
1655 end if;
1657 -- If this is a call to initialize the parent component of a derived
1658 -- tagged type, indicate that the tag should not be set in the parent.
1660 if Is_Tagged_Type (Full_Init_Type)
1661 and then not Is_CPP_Class (Full_Init_Type)
1662 and then Nkind (Id_Ref) = N_Selected_Component
1663 and then Chars (Selector_Name (Id_Ref)) = Name_uParent
1664 then
1665 Append_To (Args, New_Occurrence_Of (Standard_False, Loc));
1667 elsif Present (Constructor_Ref) then
1668 Append_List_To (Args,
1669 New_Copy_List (Parameter_Associations (Constructor_Ref)));
1670 end if;
1672 Append_To (Res,
1673 Make_Procedure_Call_Statement (Loc,
1674 Name => New_Occurrence_Of (Proc, Loc),
1675 Parameter_Associations => Args));
1677 if Needs_Finalization (Typ)
1678 and then Nkind (Id_Ref) = N_Selected_Component
1679 then
1680 if Chars (Selector_Name (Id_Ref)) /= Name_uParent then
1681 Init_Call :=
1682 Make_Init_Call
1683 (Obj_Ref => New_Copy_Tree (First_Arg),
1684 Typ => Typ);
1686 -- Guard against a missing [Deep_]Initialize when the type was not
1687 -- properly frozen.
1689 if Present (Init_Call) then
1690 Append_To (Res, Init_Call);
1691 end if;
1692 end if;
1693 end if;
1695 return Res;
1697 exception
1698 when RE_Not_Available =>
1699 return Empty_List;
1700 end Build_Initialization_Call;
1702 ----------------------------
1703 -- Build_Record_Init_Proc --
1704 ----------------------------
1706 procedure Build_Record_Init_Proc (N : Node_Id; Rec_Ent : Entity_Id) is
1707 Decls : constant List_Id := New_List;
1708 Discr_Map : constant Elist_Id := New_Elmt_List;
1709 Loc : constant Source_Ptr := Sloc (Rec_Ent);
1710 Counter : Nat := 0;
1711 Proc_Id : Entity_Id;
1712 Rec_Type : Entity_Id;
1713 Set_Tag : Entity_Id := Empty;
1715 function Build_Assignment
1716 (Id : Entity_Id;
1717 Default : Node_Id) return List_Id;
1718 -- Build an assignment statement that assigns the default expression to
1719 -- its corresponding record component if defined. The left-hand side of
1720 -- the assignment is marked Assignment_OK so that initialization of
1721 -- limited private records works correctly. This routine may also build
1722 -- an adjustment call if the component is controlled.
1724 procedure Build_Discriminant_Assignments (Statement_List : List_Id);
1725 -- If the record has discriminants, add assignment statements to
1726 -- Statement_List to initialize the discriminant values from the
1727 -- arguments of the initialization procedure.
1729 function Build_Init_Statements (Comp_List : Node_Id) return List_Id;
1730 -- Build a list representing a sequence of statements which initialize
1731 -- components of the given component list. This may involve building
1732 -- case statements for the variant parts. Append any locally declared
1733 -- objects on list Decls.
1735 function Build_Init_Call_Thru (Parameters : List_Id) return List_Id;
1736 -- Given an untagged type-derivation that declares discriminants, e.g.
1738 -- type R (R1, R2 : Integer) is record ... end record;
1739 -- type D (D1 : Integer) is new R (1, D1);
1741 -- we make the _init_proc of D be
1743 -- procedure _init_proc (X : D; D1 : Integer) is
1744 -- begin
1745 -- _init_proc (R (X), 1, D1);
1746 -- end _init_proc;
1748 -- This function builds the call statement in this _init_proc.
1750 procedure Build_CPP_Init_Procedure;
1751 -- Build the tree corresponding to the procedure specification and body
1752 -- of the IC procedure that initializes the C++ part of the dispatch
1753 -- table of an Ada tagged type that is a derivation of a CPP type.
1754 -- Install it as the CPP_Init TSS.
1756 procedure Build_Init_Procedure;
1757 -- Build the tree corresponding to the procedure specification and body
1758 -- of the initialization procedure and install it as the _init TSS.
1760 procedure Build_Offset_To_Top_Functions;
1761 -- Ada 2005 (AI-251): Build the tree corresponding to the procedure spec
1762 -- and body of Offset_To_Top, a function used in conjuction with types
1763 -- having secondary dispatch tables.
1765 procedure Build_Record_Checks (S : Node_Id; Check_List : List_Id);
1766 -- Add range checks to components of discriminated records. S is a
1767 -- subtype indication of a record component. Check_List is a list
1768 -- to which the check actions are appended.
1770 function Component_Needs_Simple_Initialization
1771 (T : Entity_Id) return Boolean;
1772 -- Determine if a component needs simple initialization, given its type
1773 -- T. This routine is the same as Needs_Simple_Initialization except for
1774 -- components of type Tag and Interface_Tag. These two access types do
1775 -- not require initialization since they are explicitly initialized by
1776 -- other means.
1778 function Parent_Subtype_Renaming_Discrims return Boolean;
1779 -- Returns True for base types N that rename discriminants, else False
1781 function Requires_Init_Proc (Rec_Id : Entity_Id) return Boolean;
1782 -- Determine whether a record initialization procedure needs to be
1783 -- generated for the given record type.
1785 ----------------------
1786 -- Build_Assignment --
1787 ----------------------
1789 function Build_Assignment
1790 (Id : Entity_Id;
1791 Default : Node_Id) return List_Id
1793 Default_Loc : constant Source_Ptr := Sloc (Default);
1794 Typ : constant Entity_Id := Underlying_Type (Etype (Id));
1796 Adj_Call : Node_Id;
1797 Exp : Node_Id := Default;
1798 Kind : Node_Kind := Nkind (Default);
1799 Lhs : Node_Id;
1800 Res : List_Id;
1802 function Replace_Discr_Ref (N : Node_Id) return Traverse_Result;
1803 -- Analysis of the aggregate has replaced discriminants by their
1804 -- corresponding discriminals, but these are irrelevant when the
1805 -- component has a mutable type and is initialized with an aggregate.
1806 -- Instead, they must be replaced by the values supplied in the
1807 -- aggregate, that will be assigned during the expansion of the
1808 -- assignment.
1810 -----------------------
1811 -- Replace_Discr_Ref --
1812 -----------------------
1814 function Replace_Discr_Ref (N : Node_Id) return Traverse_Result is
1815 Val : Node_Id;
1817 begin
1818 if Is_Entity_Name (N)
1819 and then Present (Entity (N))
1820 and then Is_Formal (Entity (N))
1821 and then Present (Discriminal_Link (Entity (N)))
1822 then
1823 Val :=
1824 Make_Selected_Component (Default_Loc,
1825 Prefix => New_Copy_Tree (Lhs),
1826 Selector_Name =>
1827 New_Occurrence_Of
1828 (Discriminal_Link (Entity (N)), Default_Loc));
1830 if Present (Val) then
1831 Rewrite (N, New_Copy_Tree (Val));
1832 end if;
1833 end if;
1835 return OK;
1836 end Replace_Discr_Ref;
1838 procedure Replace_Discriminant_References is
1839 new Traverse_Proc (Replace_Discr_Ref);
1841 -- Start of processing for Build_Assignment
1843 begin
1844 Lhs :=
1845 Make_Selected_Component (Default_Loc,
1846 Prefix => Make_Identifier (Loc, Name_uInit),
1847 Selector_Name => New_Occurrence_Of (Id, Default_Loc));
1848 Set_Assignment_OK (Lhs);
1850 if Nkind (Exp) = N_Aggregate
1851 and then Has_Discriminants (Typ)
1852 and then not Is_Constrained (Base_Type (Typ))
1853 then
1854 -- The aggregate may provide new values for the discriminants
1855 -- of the component, and other components may depend on those
1856 -- discriminants. Previous analysis of those expressions have
1857 -- replaced the discriminants by the formals of the initialization
1858 -- procedure for the type, but these are irrelevant in the
1859 -- enclosing initialization procedure: those discriminant
1860 -- references must be replaced by the values provided in the
1861 -- aggregate.
1863 Replace_Discriminant_References (Exp);
1864 end if;
1866 -- Case of an access attribute applied to the current instance.
1867 -- Replace the reference to the type by a reference to the actual
1868 -- object. (Note that this handles the case of the top level of
1869 -- the expression being given by such an attribute, but does not
1870 -- cover uses nested within an initial value expression. Nested
1871 -- uses are unlikely to occur in practice, but are theoretically
1872 -- possible.) It is not clear how to handle them without fully
1873 -- traversing the expression. ???
1875 if Kind = N_Attribute_Reference
1876 and then Nam_In (Attribute_Name (Default), Name_Unchecked_Access,
1877 Name_Unrestricted_Access)
1878 and then Is_Entity_Name (Prefix (Default))
1879 and then Is_Type (Entity (Prefix (Default)))
1880 and then Entity (Prefix (Default)) = Rec_Type
1881 then
1882 Exp :=
1883 Make_Attribute_Reference (Default_Loc,
1884 Prefix =>
1885 Make_Identifier (Default_Loc, Name_uInit),
1886 Attribute_Name => Name_Unrestricted_Access);
1887 end if;
1889 -- Take a copy of Exp to ensure that later copies of this component
1890 -- declaration in derived types see the original tree, not a node
1891 -- rewritten during expansion of the init_proc. If the copy contains
1892 -- itypes, the scope of the new itypes is the init_proc being built.
1894 Exp := New_Copy_Tree (Exp, New_Scope => Proc_Id);
1896 Res := New_List (
1897 Make_Assignment_Statement (Loc,
1898 Name => Lhs,
1899 Expression => Exp));
1901 Set_No_Ctrl_Actions (First (Res));
1903 -- Adjust the tag if tagged (because of possible view conversions).
1904 -- Suppress the tag adjustment when not Tagged_Type_Expansion because
1905 -- tags are represented implicitly in objects.
1907 if Is_Tagged_Type (Typ) and then Tagged_Type_Expansion then
1908 Append_To (Res,
1909 Make_Assignment_Statement (Default_Loc,
1910 Name =>
1911 Make_Selected_Component (Default_Loc,
1912 Prefix =>
1913 New_Copy_Tree (Lhs, New_Scope => Proc_Id),
1914 Selector_Name =>
1915 New_Occurrence_Of
1916 (First_Tag_Component (Typ), Default_Loc)),
1918 Expression =>
1919 Unchecked_Convert_To (RTE (RE_Tag),
1920 New_Occurrence_Of
1921 (Node (First_Elmt (Access_Disp_Table (Underlying_Type
1922 (Typ)))),
1923 Default_Loc))));
1924 end if;
1926 -- Adjust the component if controlled except if it is an aggregate
1927 -- that will be expanded inline.
1929 if Kind = N_Qualified_Expression then
1930 Kind := Nkind (Expression (Default));
1931 end if;
1933 if Needs_Finalization (Typ)
1934 and then not (Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate))
1935 and then not Is_Build_In_Place_Function_Call (Exp)
1936 then
1937 Adj_Call :=
1938 Make_Adjust_Call
1939 (Obj_Ref => New_Copy_Tree (Lhs),
1940 Typ => Etype (Id));
1942 -- Guard against a missing [Deep_]Adjust when the component type
1943 -- was not properly frozen.
1945 if Present (Adj_Call) then
1946 Append_To (Res, Adj_Call);
1947 end if;
1948 end if;
1950 -- If a component type has a predicate, add check to the component
1951 -- assignment. Discriminants are handled at the point of the call,
1952 -- which provides for a better error message.
1954 if Comes_From_Source (Exp)
1955 and then Has_Predicates (Typ)
1956 and then not Predicate_Checks_Suppressed (Empty)
1957 and then not Predicates_Ignored (Typ)
1958 then
1959 Append (Make_Predicate_Check (Typ, Exp), Res);
1960 end if;
1962 return Res;
1964 exception
1965 when RE_Not_Available =>
1966 return Empty_List;
1967 end Build_Assignment;
1969 ------------------------------------
1970 -- Build_Discriminant_Assignments --
1971 ------------------------------------
1973 procedure Build_Discriminant_Assignments (Statement_List : List_Id) is
1974 Is_Tagged : constant Boolean := Is_Tagged_Type (Rec_Type);
1975 D : Entity_Id;
1976 D_Loc : Source_Ptr;
1978 begin
1979 if Has_Discriminants (Rec_Type)
1980 and then not Is_Unchecked_Union (Rec_Type)
1981 then
1982 D := First_Discriminant (Rec_Type);
1983 while Present (D) loop
1985 -- Don't generate the assignment for discriminants in derived
1986 -- tagged types if the discriminant is a renaming of some
1987 -- ancestor discriminant. This initialization will be done
1988 -- when initializing the _parent field of the derived record.
1990 if Is_Tagged
1991 and then Present (Corresponding_Discriminant (D))
1992 then
1993 null;
1995 else
1996 D_Loc := Sloc (D);
1997 Append_List_To (Statement_List,
1998 Build_Assignment (D,
1999 New_Occurrence_Of (Discriminal (D), D_Loc)));
2000 end if;
2002 Next_Discriminant (D);
2003 end loop;
2004 end if;
2005 end Build_Discriminant_Assignments;
2007 --------------------------
2008 -- Build_Init_Call_Thru --
2009 --------------------------
2011 function Build_Init_Call_Thru (Parameters : List_Id) return List_Id is
2012 Parent_Proc : constant Entity_Id :=
2013 Base_Init_Proc (Etype (Rec_Type));
2015 Parent_Type : constant Entity_Id :=
2016 Etype (First_Formal (Parent_Proc));
2018 Uparent_Type : constant Entity_Id :=
2019 Underlying_Type (Parent_Type);
2021 First_Discr_Param : Node_Id;
2023 Arg : Node_Id;
2024 Args : List_Id;
2025 First_Arg : Node_Id;
2026 Parent_Discr : Entity_Id;
2027 Res : List_Id;
2029 begin
2030 -- First argument (_Init) is the object to be initialized.
2031 -- ??? not sure where to get a reasonable Loc for First_Arg
2033 First_Arg :=
2034 OK_Convert_To (Parent_Type,
2035 New_Occurrence_Of
2036 (Defining_Identifier (First (Parameters)), Loc));
2038 Set_Etype (First_Arg, Parent_Type);
2040 Args := New_List (Convert_Concurrent (First_Arg, Rec_Type));
2042 -- In the tasks case,
2043 -- add _Master as the value of the _Master parameter
2044 -- add _Chain as the value of the _Chain parameter.
2045 -- add _Task_Name as the value of the _Task_Name parameter.
2046 -- At the outer level, these will be variables holding the
2047 -- corresponding values obtained from GNARL or the expander.
2049 -- At inner levels, they will be the parameters passed down through
2050 -- the outer routines.
2052 First_Discr_Param := Next (First (Parameters));
2054 if Has_Task (Rec_Type) then
2055 if Restriction_Active (No_Task_Hierarchy) then
2056 Append_To (Args,
2057 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
2058 else
2059 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
2060 end if;
2062 -- Add _Chain (not done for sequential elaboration policy, see
2063 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
2065 if Partition_Elaboration_Policy /= 'S' then
2066 Append_To (Args, Make_Identifier (Loc, Name_uChain));
2067 end if;
2069 Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
2070 First_Discr_Param := Next (Next (Next (First_Discr_Param)));
2071 end if;
2073 -- Append discriminant values
2075 if Has_Discriminants (Uparent_Type) then
2076 pragma Assert (not Is_Tagged_Type (Uparent_Type));
2078 Parent_Discr := First_Discriminant (Uparent_Type);
2079 while Present (Parent_Discr) loop
2081 -- Get the initial value for this discriminant
2082 -- ??? needs to be cleaned up to use parent_Discr_Constr
2083 -- directly.
2085 declare
2086 Discr : Entity_Id :=
2087 First_Stored_Discriminant (Uparent_Type);
2089 Discr_Value : Elmt_Id :=
2090 First_Elmt (Stored_Constraint (Rec_Type));
2092 begin
2093 while Original_Record_Component (Parent_Discr) /= Discr loop
2094 Next_Stored_Discriminant (Discr);
2095 Next_Elmt (Discr_Value);
2096 end loop;
2098 Arg := Node (Discr_Value);
2099 end;
2101 -- Append it to the list
2103 if Nkind (Arg) = N_Identifier
2104 and then Ekind (Entity (Arg)) = E_Discriminant
2105 then
2106 Append_To (Args,
2107 New_Occurrence_Of (Discriminal (Entity (Arg)), Loc));
2109 -- Case of access discriminants. We replace the reference
2110 -- to the type by a reference to the actual object.
2112 -- Is above comment right??? Use of New_Copy below seems mighty
2113 -- suspicious ???
2115 else
2116 Append_To (Args, New_Copy (Arg));
2117 end if;
2119 Next_Discriminant (Parent_Discr);
2120 end loop;
2121 end if;
2123 Res :=
2124 New_List (
2125 Make_Procedure_Call_Statement (Loc,
2126 Name =>
2127 New_Occurrence_Of (Parent_Proc, Loc),
2128 Parameter_Associations => Args));
2130 return Res;
2131 end Build_Init_Call_Thru;
2133 -----------------------------------
2134 -- Build_Offset_To_Top_Functions --
2135 -----------------------------------
2137 procedure Build_Offset_To_Top_Functions is
2139 procedure Build_Offset_To_Top_Function (Iface_Comp : Entity_Id);
2140 -- Generate:
2141 -- function Fxx (O : Address) return Storage_Offset is
2142 -- type Acc is access all <Typ>;
2143 -- begin
2144 -- return Acc!(O).Iface_Comp'Position;
2145 -- end Fxx;
2147 ----------------------------------
2148 -- Build_Offset_To_Top_Function --
2149 ----------------------------------
2151 procedure Build_Offset_To_Top_Function (Iface_Comp : Entity_Id) is
2152 Body_Node : Node_Id;
2153 Func_Id : Entity_Id;
2154 Spec_Node : Node_Id;
2155 Acc_Type : Entity_Id;
2157 begin
2158 Func_Id := Make_Temporary (Loc, 'F');
2159 Set_DT_Offset_To_Top_Func (Iface_Comp, Func_Id);
2161 -- Generate
2162 -- function Fxx (O : in Rec_Typ) return Storage_Offset;
2164 Spec_Node := New_Node (N_Function_Specification, Loc);
2165 Set_Defining_Unit_Name (Spec_Node, Func_Id);
2166 Set_Parameter_Specifications (Spec_Node, New_List (
2167 Make_Parameter_Specification (Loc,
2168 Defining_Identifier =>
2169 Make_Defining_Identifier (Loc, Name_uO),
2170 In_Present => True,
2171 Parameter_Type =>
2172 New_Occurrence_Of (RTE (RE_Address), Loc))));
2173 Set_Result_Definition (Spec_Node,
2174 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
2176 -- Generate
2177 -- function Fxx (O : in Rec_Typ) return Storage_Offset is
2178 -- begin
2179 -- return -O.Iface_Comp'Position;
2180 -- end Fxx;
2182 Body_Node := New_Node (N_Subprogram_Body, Loc);
2183 Set_Specification (Body_Node, Spec_Node);
2185 Acc_Type := Make_Temporary (Loc, 'T');
2186 Set_Declarations (Body_Node, New_List (
2187 Make_Full_Type_Declaration (Loc,
2188 Defining_Identifier => Acc_Type,
2189 Type_Definition =>
2190 Make_Access_To_Object_Definition (Loc,
2191 All_Present => True,
2192 Null_Exclusion_Present => False,
2193 Constant_Present => False,
2194 Subtype_Indication =>
2195 New_Occurrence_Of (Rec_Type, Loc)))));
2197 Set_Handled_Statement_Sequence (Body_Node,
2198 Make_Handled_Sequence_Of_Statements (Loc,
2199 Statements => New_List (
2200 Make_Simple_Return_Statement (Loc,
2201 Expression =>
2202 Make_Op_Minus (Loc,
2203 Make_Attribute_Reference (Loc,
2204 Prefix =>
2205 Make_Selected_Component (Loc,
2206 Prefix =>
2207 Unchecked_Convert_To (Acc_Type,
2208 Make_Identifier (Loc, Name_uO)),
2209 Selector_Name =>
2210 New_Occurrence_Of (Iface_Comp, Loc)),
2211 Attribute_Name => Name_Position))))));
2213 Set_Ekind (Func_Id, E_Function);
2214 Set_Mechanism (Func_Id, Default_Mechanism);
2215 Set_Is_Internal (Func_Id, True);
2217 if not Debug_Generated_Code then
2218 Set_Debug_Info_Off (Func_Id);
2219 end if;
2221 Analyze (Body_Node);
2223 Append_Freeze_Action (Rec_Type, Body_Node);
2224 end Build_Offset_To_Top_Function;
2226 -- Local variables
2228 Iface_Comp : Node_Id;
2229 Iface_Comp_Elmt : Elmt_Id;
2230 Ifaces_Comp_List : Elist_Id;
2232 -- Start of processing for Build_Offset_To_Top_Functions
2234 begin
2235 -- Offset_To_Top_Functions are built only for derivations of types
2236 -- with discriminants that cover interface types.
2237 -- Nothing is needed either in case of virtual targets, since
2238 -- interfaces are handled directly by the target.
2240 if not Is_Tagged_Type (Rec_Type)
2241 or else Etype (Rec_Type) = Rec_Type
2242 or else not Has_Discriminants (Etype (Rec_Type))
2243 or else not Tagged_Type_Expansion
2244 then
2245 return;
2246 end if;
2248 Collect_Interface_Components (Rec_Type, Ifaces_Comp_List);
2250 -- For each interface type with secondary dispatch table we generate
2251 -- the Offset_To_Top_Functions (required to displace the pointer in
2252 -- interface conversions)
2254 Iface_Comp_Elmt := First_Elmt (Ifaces_Comp_List);
2255 while Present (Iface_Comp_Elmt) loop
2256 Iface_Comp := Node (Iface_Comp_Elmt);
2257 pragma Assert (Is_Interface (Related_Type (Iface_Comp)));
2259 -- If the interface is a parent of Rec_Type it shares the primary
2260 -- dispatch table and hence there is no need to build the function
2262 if not Is_Ancestor (Related_Type (Iface_Comp), Rec_Type,
2263 Use_Full_View => True)
2264 then
2265 Build_Offset_To_Top_Function (Iface_Comp);
2266 end if;
2268 Next_Elmt (Iface_Comp_Elmt);
2269 end loop;
2270 end Build_Offset_To_Top_Functions;
2272 ------------------------------
2273 -- Build_CPP_Init_Procedure --
2274 ------------------------------
2276 procedure Build_CPP_Init_Procedure is
2277 Body_Node : Node_Id;
2278 Body_Stmts : List_Id;
2279 Flag_Id : Entity_Id;
2280 Handled_Stmt_Node : Node_Id;
2281 Init_Tags_List : List_Id;
2282 Proc_Id : Entity_Id;
2283 Proc_Spec_Node : Node_Id;
2285 begin
2286 -- Check cases requiring no IC routine
2288 if not Is_CPP_Class (Root_Type (Rec_Type))
2289 or else Is_CPP_Class (Rec_Type)
2290 or else CPP_Num_Prims (Rec_Type) = 0
2291 or else not Tagged_Type_Expansion
2292 or else No_Run_Time_Mode
2293 then
2294 return;
2295 end if;
2297 -- Generate:
2299 -- Flag : Boolean := False;
2301 -- procedure Typ_IC is
2302 -- begin
2303 -- if not Flag then
2304 -- Copy C++ dispatch table slots from parent
2305 -- Update C++ slots of overridden primitives
2306 -- end if;
2307 -- end;
2309 Flag_Id := Make_Temporary (Loc, 'F');
2311 Append_Freeze_Action (Rec_Type,
2312 Make_Object_Declaration (Loc,
2313 Defining_Identifier => Flag_Id,
2314 Object_Definition =>
2315 New_Occurrence_Of (Standard_Boolean, Loc),
2316 Expression =>
2317 New_Occurrence_Of (Standard_True, Loc)));
2319 Body_Stmts := New_List;
2320 Body_Node := New_Node (N_Subprogram_Body, Loc);
2322 Proc_Spec_Node := New_Node (N_Procedure_Specification, Loc);
2324 Proc_Id :=
2325 Make_Defining_Identifier (Loc,
2326 Chars => Make_TSS_Name (Rec_Type, TSS_CPP_Init_Proc));
2328 Set_Ekind (Proc_Id, E_Procedure);
2329 Set_Is_Internal (Proc_Id);
2331 Set_Defining_Unit_Name (Proc_Spec_Node, Proc_Id);
2333 Set_Parameter_Specifications (Proc_Spec_Node, New_List);
2334 Set_Specification (Body_Node, Proc_Spec_Node);
2335 Set_Declarations (Body_Node, New_List);
2337 Init_Tags_List := Build_Inherit_CPP_Prims (Rec_Type);
2339 Append_To (Init_Tags_List,
2340 Make_Assignment_Statement (Loc,
2341 Name =>
2342 New_Occurrence_Of (Flag_Id, Loc),
2343 Expression =>
2344 New_Occurrence_Of (Standard_False, Loc)));
2346 Append_To (Body_Stmts,
2347 Make_If_Statement (Loc,
2348 Condition => New_Occurrence_Of (Flag_Id, Loc),
2349 Then_Statements => Init_Tags_List));
2351 Handled_Stmt_Node :=
2352 New_Node (N_Handled_Sequence_Of_Statements, Loc);
2353 Set_Statements (Handled_Stmt_Node, Body_Stmts);
2354 Set_Exception_Handlers (Handled_Stmt_Node, No_List);
2355 Set_Handled_Statement_Sequence (Body_Node, Handled_Stmt_Node);
2357 if not Debug_Generated_Code then
2358 Set_Debug_Info_Off (Proc_Id);
2359 end if;
2361 -- Associate CPP_Init_Proc with type
2363 Set_Init_Proc (Rec_Type, Proc_Id);
2364 end Build_CPP_Init_Procedure;
2366 --------------------------
2367 -- Build_Init_Procedure --
2368 --------------------------
2370 procedure Build_Init_Procedure is
2371 Body_Stmts : List_Id;
2372 Body_Node : Node_Id;
2373 Handled_Stmt_Node : Node_Id;
2374 Init_Tags_List : List_Id;
2375 Parameters : List_Id;
2376 Proc_Spec_Node : Node_Id;
2377 Record_Extension_Node : Node_Id;
2379 begin
2380 Body_Stmts := New_List;
2381 Body_Node := New_Node (N_Subprogram_Body, Loc);
2382 Set_Ekind (Proc_Id, E_Procedure);
2384 Proc_Spec_Node := New_Node (N_Procedure_Specification, Loc);
2385 Set_Defining_Unit_Name (Proc_Spec_Node, Proc_Id);
2387 Parameters := Init_Formals (Rec_Type);
2388 Append_List_To (Parameters,
2389 Build_Discriminant_Formals (Rec_Type, True));
2391 -- For tagged types, we add a flag to indicate whether the routine
2392 -- is called to initialize a parent component in the init_proc of
2393 -- a type extension. If the flag is false, we do not set the tag
2394 -- because it has been set already in the extension.
2396 if Is_Tagged_Type (Rec_Type) then
2397 Set_Tag := Make_Temporary (Loc, 'P');
2399 Append_To (Parameters,
2400 Make_Parameter_Specification (Loc,
2401 Defining_Identifier => Set_Tag,
2402 Parameter_Type =>
2403 New_Occurrence_Of (Standard_Boolean, Loc),
2404 Expression =>
2405 New_Occurrence_Of (Standard_True, Loc)));
2406 end if;
2408 Set_Parameter_Specifications (Proc_Spec_Node, Parameters);
2409 Set_Specification (Body_Node, Proc_Spec_Node);
2410 Set_Declarations (Body_Node, Decls);
2412 -- N is a Derived_Type_Definition that renames the parameters of the
2413 -- ancestor type. We initialize it by expanding our discriminants and
2414 -- call the ancestor _init_proc with a type-converted object.
2416 if Parent_Subtype_Renaming_Discrims then
2417 Append_List_To (Body_Stmts, Build_Init_Call_Thru (Parameters));
2419 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
2420 Build_Discriminant_Assignments (Body_Stmts);
2422 if not Null_Present (Type_Definition (N)) then
2423 Append_List_To (Body_Stmts,
2424 Build_Init_Statements (Component_List (Type_Definition (N))));
2425 end if;
2427 -- N is a Derived_Type_Definition with a possible non-empty
2428 -- extension. The initialization of a type extension consists in the
2429 -- initialization of the components in the extension.
2431 else
2432 Build_Discriminant_Assignments (Body_Stmts);
2434 Record_Extension_Node :=
2435 Record_Extension_Part (Type_Definition (N));
2437 if not Null_Present (Record_Extension_Node) then
2438 declare
2439 Stmts : constant List_Id :=
2440 Build_Init_Statements (
2441 Component_List (Record_Extension_Node));
2443 begin
2444 -- The parent field must be initialized first because the
2445 -- offset of the new discriminants may depend on it. This is
2446 -- not needed if the parent is an interface type because in
2447 -- such case the initialization of the _parent field was not
2448 -- generated.
2450 if not Is_Interface (Etype (Rec_Ent)) then
2451 declare
2452 Parent_IP : constant Name_Id :=
2453 Make_Init_Proc_Name (Etype (Rec_Ent));
2454 Stmt : Node_Id;
2455 IP_Call : Node_Id;
2456 IP_Stmts : List_Id;
2458 begin
2459 -- Look for a call to the parent IP at the beginning
2460 -- of Stmts associated with the record extension
2462 Stmt := First (Stmts);
2463 IP_Call := Empty;
2464 while Present (Stmt) loop
2465 if Nkind (Stmt) = N_Procedure_Call_Statement
2466 and then Chars (Name (Stmt)) = Parent_IP
2467 then
2468 IP_Call := Stmt;
2469 exit;
2470 end if;
2472 Next (Stmt);
2473 end loop;
2475 -- If found then move it to the beginning of the
2476 -- statements of this IP routine
2478 if Present (IP_Call) then
2479 IP_Stmts := New_List;
2480 loop
2481 Stmt := Remove_Head (Stmts);
2482 Append_To (IP_Stmts, Stmt);
2483 exit when Stmt = IP_Call;
2484 end loop;
2486 Prepend_List_To (Body_Stmts, IP_Stmts);
2487 end if;
2488 end;
2489 end if;
2491 Append_List_To (Body_Stmts, Stmts);
2492 end;
2493 end if;
2494 end if;
2496 -- Add here the assignment to instantiate the Tag
2498 -- The assignment corresponds to the code:
2500 -- _Init._Tag := Typ'Tag;
2502 -- Suppress the tag assignment when not Tagged_Type_Expansion because
2503 -- tags are represented implicitly in objects. It is also suppressed
2504 -- in case of CPP_Class types because in this case the tag is
2505 -- initialized in the C++ side.
2507 if Is_Tagged_Type (Rec_Type)
2508 and then Tagged_Type_Expansion
2509 and then not No_Run_Time_Mode
2510 then
2511 -- Case 1: Ada tagged types with no CPP ancestor. Set the tags of
2512 -- the actual object and invoke the IP of the parent (in this
2513 -- order). The tag must be initialized before the call to the IP
2514 -- of the parent and the assignments to other components because
2515 -- the initial value of the components may depend on the tag (eg.
2516 -- through a dispatching operation on an access to the current
2517 -- type). The tag assignment is not done when initializing the
2518 -- parent component of a type extension, because in that case the
2519 -- tag is set in the extension.
2521 if not Is_CPP_Class (Root_Type (Rec_Type)) then
2523 -- Initialize the primary tag component
2525 Init_Tags_List := New_List (
2526 Make_Assignment_Statement (Loc,
2527 Name =>
2528 Make_Selected_Component (Loc,
2529 Prefix => Make_Identifier (Loc, Name_uInit),
2530 Selector_Name =>
2531 New_Occurrence_Of
2532 (First_Tag_Component (Rec_Type), Loc)),
2533 Expression =>
2534 New_Occurrence_Of
2535 (Node
2536 (First_Elmt (Access_Disp_Table (Rec_Type))), Loc)));
2538 -- Ada 2005 (AI-251): Initialize the secondary tags components
2539 -- located at fixed positions (tags whose position depends on
2540 -- variable size components are initialized later ---see below)
2542 if Ada_Version >= Ada_2005
2543 and then not Is_Interface (Rec_Type)
2544 and then Has_Interfaces (Rec_Type)
2545 then
2546 declare
2547 Elab_Sec_DT_Stmts_List : constant List_Id := New_List;
2548 Elab_List : List_Id := New_List;
2550 begin
2551 Init_Secondary_Tags
2552 (Typ => Rec_Type,
2553 Target => Make_Identifier (Loc, Name_uInit),
2554 Init_Tags_List => Init_Tags_List,
2555 Stmts_List => Elab_Sec_DT_Stmts_List,
2556 Fixed_Comps => True,
2557 Variable_Comps => False);
2559 Elab_List := New_List (
2560 Make_If_Statement (Loc,
2561 Condition => New_Occurrence_Of (Set_Tag, Loc),
2562 Then_Statements => Init_Tags_List));
2564 if Elab_Flag_Needed (Rec_Type) then
2565 Append_To (Elab_Sec_DT_Stmts_List,
2566 Make_Assignment_Statement (Loc,
2567 Name =>
2568 New_Occurrence_Of
2569 (Access_Disp_Table_Elab_Flag (Rec_Type),
2570 Loc),
2571 Expression =>
2572 New_Occurrence_Of (Standard_False, Loc)));
2574 Append_To (Elab_List,
2575 Make_If_Statement (Loc,
2576 Condition =>
2577 New_Occurrence_Of
2578 (Access_Disp_Table_Elab_Flag (Rec_Type), Loc),
2579 Then_Statements => Elab_Sec_DT_Stmts_List));
2580 end if;
2582 Prepend_List_To (Body_Stmts, Elab_List);
2583 end;
2584 else
2585 Prepend_To (Body_Stmts,
2586 Make_If_Statement (Loc,
2587 Condition => New_Occurrence_Of (Set_Tag, Loc),
2588 Then_Statements => Init_Tags_List));
2589 end if;
2591 -- Case 2: CPP type. The imported C++ constructor takes care of
2592 -- tags initialization. No action needed here because the IP
2593 -- is built by Set_CPP_Constructors; in this case the IP is a
2594 -- wrapper that invokes the C++ constructor and copies the C++
2595 -- tags locally. Done to inherit the C++ slots in Ada derivations
2596 -- (see case 3).
2598 elsif Is_CPP_Class (Rec_Type) then
2599 pragma Assert (False);
2600 null;
2602 -- Case 3: Combined hierarchy containing C++ types and Ada tagged
2603 -- type derivations. Derivations of imported C++ classes add a
2604 -- complication, because we cannot inhibit tag setting in the
2605 -- constructor for the parent. Hence we initialize the tag after
2606 -- the call to the parent IP (that is, in reverse order compared
2607 -- with pure Ada hierarchies ---see comment on case 1).
2609 else
2610 -- Initialize the primary tag
2612 Init_Tags_List := New_List (
2613 Make_Assignment_Statement (Loc,
2614 Name =>
2615 Make_Selected_Component (Loc,
2616 Prefix => Make_Identifier (Loc, Name_uInit),
2617 Selector_Name =>
2618 New_Occurrence_Of
2619 (First_Tag_Component (Rec_Type), Loc)),
2620 Expression =>
2621 New_Occurrence_Of
2622 (Node
2623 (First_Elmt (Access_Disp_Table (Rec_Type))), Loc)));
2625 -- Ada 2005 (AI-251): Initialize the secondary tags components
2626 -- located at fixed positions (tags whose position depends on
2627 -- variable size components are initialized later ---see below)
2629 if Ada_Version >= Ada_2005
2630 and then not Is_Interface (Rec_Type)
2631 and then Has_Interfaces (Rec_Type)
2632 then
2633 Init_Secondary_Tags
2634 (Typ => Rec_Type,
2635 Target => Make_Identifier (Loc, Name_uInit),
2636 Init_Tags_List => Init_Tags_List,
2637 Stmts_List => Init_Tags_List,
2638 Fixed_Comps => True,
2639 Variable_Comps => False);
2640 end if;
2642 -- Initialize the tag component after invocation of parent IP.
2644 -- Generate:
2645 -- parent_IP(_init.parent); // Invokes the C++ constructor
2646 -- [ typIC; ] // Inherit C++ slots from parent
2647 -- init_tags
2649 declare
2650 Ins_Nod : Node_Id;
2652 begin
2653 -- Search for the call to the IP of the parent. We assume
2654 -- that the first init_proc call is for the parent.
2656 Ins_Nod := First (Body_Stmts);
2657 while Present (Next (Ins_Nod))
2658 and then (Nkind (Ins_Nod) /= N_Procedure_Call_Statement
2659 or else not Is_Init_Proc (Name (Ins_Nod)))
2660 loop
2661 Next (Ins_Nod);
2662 end loop;
2664 -- The IC routine copies the inherited slots of the C+ part
2665 -- of the dispatch table from the parent and updates the
2666 -- overridden C++ slots.
2668 if CPP_Num_Prims (Rec_Type) > 0 then
2669 declare
2670 Init_DT : Entity_Id;
2671 New_Nod : Node_Id;
2673 begin
2674 Init_DT := CPP_Init_Proc (Rec_Type);
2675 pragma Assert (Present (Init_DT));
2677 New_Nod :=
2678 Make_Procedure_Call_Statement (Loc,
2679 New_Occurrence_Of (Init_DT, Loc));
2680 Insert_After (Ins_Nod, New_Nod);
2682 -- Update location of init tag statements
2684 Ins_Nod := New_Nod;
2685 end;
2686 end if;
2688 Insert_List_After (Ins_Nod, Init_Tags_List);
2689 end;
2690 end if;
2692 -- Ada 2005 (AI-251): Initialize the secondary tag components
2693 -- located at variable positions. We delay the generation of this
2694 -- code until here because the value of the attribute 'Position
2695 -- applied to variable size components of the parent type that
2696 -- depend on discriminants is only safely read at runtime after
2697 -- the parent components have been initialized.
2699 if Ada_Version >= Ada_2005
2700 and then not Is_Interface (Rec_Type)
2701 and then Has_Interfaces (Rec_Type)
2702 and then Has_Discriminants (Etype (Rec_Type))
2703 and then Is_Variable_Size_Record (Etype (Rec_Type))
2704 then
2705 Init_Tags_List := New_List;
2707 Init_Secondary_Tags
2708 (Typ => Rec_Type,
2709 Target => Make_Identifier (Loc, Name_uInit),
2710 Init_Tags_List => Init_Tags_List,
2711 Stmts_List => Init_Tags_List,
2712 Fixed_Comps => False,
2713 Variable_Comps => True);
2715 if Is_Non_Empty_List (Init_Tags_List) then
2716 Append_List_To (Body_Stmts, Init_Tags_List);
2717 end if;
2718 end if;
2719 end if;
2721 Handled_Stmt_Node := New_Node (N_Handled_Sequence_Of_Statements, Loc);
2722 Set_Statements (Handled_Stmt_Node, Body_Stmts);
2724 -- Generate:
2725 -- Deep_Finalize (_init, C1, ..., CN);
2726 -- raise;
2728 if Counter > 0
2729 and then Needs_Finalization (Rec_Type)
2730 and then not Is_Abstract_Type (Rec_Type)
2731 and then not Restriction_Active (No_Exception_Propagation)
2732 then
2733 declare
2734 DF_Call : Node_Id;
2735 DF_Id : Entity_Id;
2737 begin
2738 -- Create a local version of Deep_Finalize which has indication
2739 -- of partial initialization state.
2741 DF_Id :=
2742 Make_Defining_Identifier (Loc,
2743 Chars => New_External_Name (Name_uFinalizer));
2745 Append_To (Decls, Make_Local_Deep_Finalize (Rec_Type, DF_Id));
2747 DF_Call :=
2748 Make_Procedure_Call_Statement (Loc,
2749 Name => New_Occurrence_Of (DF_Id, Loc),
2750 Parameter_Associations => New_List (
2751 Make_Identifier (Loc, Name_uInit),
2752 New_Occurrence_Of (Standard_False, Loc)));
2754 -- Do not emit warnings related to the elaboration order when a
2755 -- controlled object is declared before the body of Finalize is
2756 -- seen.
2758 if Legacy_Elaboration_Checks then
2759 Set_No_Elaboration_Check (DF_Call);
2760 end if;
2762 Set_Exception_Handlers (Handled_Stmt_Node, New_List (
2763 Make_Exception_Handler (Loc,
2764 Exception_Choices => New_List (
2765 Make_Others_Choice (Loc)),
2766 Statements => New_List (
2767 DF_Call,
2768 Make_Raise_Statement (Loc)))));
2769 end;
2770 else
2771 Set_Exception_Handlers (Handled_Stmt_Node, No_List);
2772 end if;
2774 Set_Handled_Statement_Sequence (Body_Node, Handled_Stmt_Node);
2776 if not Debug_Generated_Code then
2777 Set_Debug_Info_Off (Proc_Id);
2778 end if;
2780 -- Associate Init_Proc with type, and determine if the procedure
2781 -- is null (happens because of the Initialize_Scalars pragma case,
2782 -- where we have to generate a null procedure in case it is called
2783 -- by a client with Initialize_Scalars set). Such procedures have
2784 -- to be generated, but do not have to be called, so we mark them
2785 -- as null to suppress the call.
2787 Set_Init_Proc (Rec_Type, Proc_Id);
2789 if List_Length (Body_Stmts) = 1
2791 -- We must skip SCIL nodes because they may have been added to this
2792 -- list by Insert_Actions.
2794 and then Nkind (First_Non_SCIL_Node (Body_Stmts)) = N_Null_Statement
2795 then
2796 Set_Is_Null_Init_Proc (Proc_Id);
2797 end if;
2798 end Build_Init_Procedure;
2800 ---------------------------
2801 -- Build_Init_Statements --
2802 ---------------------------
2804 function Build_Init_Statements (Comp_List : Node_Id) return List_Id is
2805 Checks : constant List_Id := New_List;
2806 Actions : List_Id := No_List;
2807 Counter_Id : Entity_Id := Empty;
2808 Comp_Loc : Source_Ptr;
2809 Decl : Node_Id;
2810 Has_POC : Boolean;
2811 Id : Entity_Id;
2812 Parent_Stmts : List_Id;
2813 Stmts : List_Id;
2814 Typ : Entity_Id;
2816 procedure Increment_Counter (Loc : Source_Ptr);
2817 -- Generate an "increment by one" statement for the current counter
2818 -- and append it to the list Stmts.
2820 procedure Make_Counter (Loc : Source_Ptr);
2821 -- Create a new counter for the current component list. The routine
2822 -- creates a new defining Id, adds an object declaration and sets
2823 -- the Id generator for the next variant.
2825 -----------------------
2826 -- Increment_Counter --
2827 -----------------------
2829 procedure Increment_Counter (Loc : Source_Ptr) is
2830 begin
2831 -- Generate:
2832 -- Counter := Counter + 1;
2834 Append_To (Stmts,
2835 Make_Assignment_Statement (Loc,
2836 Name => New_Occurrence_Of (Counter_Id, Loc),
2837 Expression =>
2838 Make_Op_Add (Loc,
2839 Left_Opnd => New_Occurrence_Of (Counter_Id, Loc),
2840 Right_Opnd => Make_Integer_Literal (Loc, 1))));
2841 end Increment_Counter;
2843 ------------------
2844 -- Make_Counter --
2845 ------------------
2847 procedure Make_Counter (Loc : Source_Ptr) is
2848 begin
2849 -- Increment the Id generator
2851 Counter := Counter + 1;
2853 -- Create the entity and declaration
2855 Counter_Id :=
2856 Make_Defining_Identifier (Loc,
2857 Chars => New_External_Name ('C', Counter));
2859 -- Generate:
2860 -- Cnn : Integer := 0;
2862 Append_To (Decls,
2863 Make_Object_Declaration (Loc,
2864 Defining_Identifier => Counter_Id,
2865 Object_Definition =>
2866 New_Occurrence_Of (Standard_Integer, Loc),
2867 Expression =>
2868 Make_Integer_Literal (Loc, 0)));
2869 end Make_Counter;
2871 -- Start of processing for Build_Init_Statements
2873 begin
2874 if Null_Present (Comp_List) then
2875 return New_List (Make_Null_Statement (Loc));
2876 end if;
2878 Parent_Stmts := New_List;
2879 Stmts := New_List;
2881 -- Loop through visible declarations of task types and protected
2882 -- types moving any expanded code from the spec to the body of the
2883 -- init procedure.
2885 if Is_Task_Record_Type (Rec_Type)
2886 or else Is_Protected_Record_Type (Rec_Type)
2887 then
2888 declare
2889 Decl : constant Node_Id :=
2890 Parent (Corresponding_Concurrent_Type (Rec_Type));
2891 Def : Node_Id;
2892 N1 : Node_Id;
2893 N2 : Node_Id;
2895 begin
2896 if Is_Task_Record_Type (Rec_Type) then
2897 Def := Task_Definition (Decl);
2898 else
2899 Def := Protected_Definition (Decl);
2900 end if;
2902 if Present (Def) then
2903 N1 := First (Visible_Declarations (Def));
2904 while Present (N1) loop
2905 N2 := N1;
2906 N1 := Next (N1);
2908 if Nkind (N2) in N_Statement_Other_Than_Procedure_Call
2909 or else Nkind (N2) in N_Raise_xxx_Error
2910 or else Nkind (N2) = N_Procedure_Call_Statement
2911 then
2912 Append_To (Stmts,
2913 New_Copy_Tree (N2, New_Scope => Proc_Id));
2914 Rewrite (N2, Make_Null_Statement (Sloc (N2)));
2915 Analyze (N2);
2916 end if;
2917 end loop;
2918 end if;
2919 end;
2920 end if;
2922 -- Loop through components, skipping pragmas, in 2 steps. The first
2923 -- step deals with regular components. The second step deals with
2924 -- components that have per object constraints and no explicit
2925 -- initialization.
2927 Has_POC := False;
2929 -- First pass : regular components
2931 Decl := First_Non_Pragma (Component_Items (Comp_List));
2932 while Present (Decl) loop
2933 Comp_Loc := Sloc (Decl);
2934 Build_Record_Checks
2935 (Subtype_Indication (Component_Definition (Decl)), Checks);
2937 Id := Defining_Identifier (Decl);
2938 Typ := Etype (Id);
2940 -- Leave any processing of per-object constrained component for
2941 -- the second pass.
2943 if Has_Access_Constraint (Id) and then No (Expression (Decl)) then
2944 Has_POC := True;
2946 -- Regular component cases
2948 else
2949 -- In the context of the init proc, references to discriminants
2950 -- resolve to denote the discriminals: this is where we can
2951 -- freeze discriminant dependent component subtypes.
2953 if not Is_Frozen (Typ) then
2954 Append_List_To (Stmts, Freeze_Entity (Typ, N));
2955 end if;
2957 -- Explicit initialization
2959 if Present (Expression (Decl)) then
2960 if Is_CPP_Constructor_Call (Expression (Decl)) then
2961 Actions :=
2962 Build_Initialization_Call
2963 (Comp_Loc,
2964 Id_Ref =>
2965 Make_Selected_Component (Comp_Loc,
2966 Prefix =>
2967 Make_Identifier (Comp_Loc, Name_uInit),
2968 Selector_Name =>
2969 New_Occurrence_Of (Id, Comp_Loc)),
2970 Typ => Typ,
2971 In_Init_Proc => True,
2972 Enclos_Type => Rec_Type,
2973 Discr_Map => Discr_Map,
2974 Constructor_Ref => Expression (Decl));
2975 else
2976 Actions := Build_Assignment (Id, Expression (Decl));
2977 end if;
2979 -- CPU, Dispatching_Domain, Priority, and Secondary_Stack_Size
2980 -- components are filled in with the corresponding rep-item
2981 -- expression of the concurrent type (if any).
2983 elsif Ekind (Scope (Id)) = E_Record_Type
2984 and then Present (Corresponding_Concurrent_Type (Scope (Id)))
2985 and then Nam_In (Chars (Id), Name_uCPU,
2986 Name_uDispatching_Domain,
2987 Name_uPriority,
2988 Name_uSecondary_Stack_Size)
2989 then
2990 declare
2991 Exp : Node_Id;
2992 Nam : Name_Id;
2993 pragma Warnings (Off, Nam);
2994 Ritem : Node_Id;
2996 begin
2997 if Chars (Id) = Name_uCPU then
2998 Nam := Name_CPU;
3000 elsif Chars (Id) = Name_uDispatching_Domain then
3001 Nam := Name_Dispatching_Domain;
3003 elsif Chars (Id) = Name_uPriority then
3004 Nam := Name_Priority;
3006 elsif Chars (Id) = Name_uSecondary_Stack_Size then
3007 Nam := Name_Secondary_Stack_Size;
3008 end if;
3010 -- Get the Rep Item (aspect specification, attribute
3011 -- definition clause or pragma) of the corresponding
3012 -- concurrent type.
3014 Ritem :=
3015 Get_Rep_Item
3016 (Corresponding_Concurrent_Type (Scope (Id)),
3017 Nam,
3018 Check_Parents => False);
3020 if Present (Ritem) then
3022 -- Pragma case
3024 if Nkind (Ritem) = N_Pragma then
3025 Exp := First (Pragma_Argument_Associations (Ritem));
3027 if Nkind (Exp) = N_Pragma_Argument_Association then
3028 Exp := Expression (Exp);
3029 end if;
3031 -- Conversion for Priority expression
3033 if Nam = Name_Priority then
3034 if Pragma_Name (Ritem) = Name_Priority
3035 and then not GNAT_Mode
3036 then
3037 Exp := Convert_To (RTE (RE_Priority), Exp);
3038 else
3039 Exp :=
3040 Convert_To (RTE (RE_Any_Priority), Exp);
3041 end if;
3042 end if;
3044 -- Aspect/Attribute definition clause case
3046 else
3047 Exp := Expression (Ritem);
3049 -- Conversion for Priority expression
3051 if Nam = Name_Priority then
3052 if Chars (Ritem) = Name_Priority
3053 and then not GNAT_Mode
3054 then
3055 Exp := Convert_To (RTE (RE_Priority), Exp);
3056 else
3057 Exp :=
3058 Convert_To (RTE (RE_Any_Priority), Exp);
3059 end if;
3060 end if;
3061 end if;
3063 -- Conversion for Dispatching_Domain value
3065 if Nam = Name_Dispatching_Domain then
3066 Exp :=
3067 Unchecked_Convert_To
3068 (RTE (RE_Dispatching_Domain_Access), Exp);
3070 -- Conversion for Secondary_Stack_Size value
3072 elsif Nam = Name_Secondary_Stack_Size then
3073 Exp := Convert_To (RTE (RE_Size_Type), Exp);
3074 end if;
3076 Actions := Build_Assignment (Id, Exp);
3078 -- Nothing needed if no Rep Item
3080 else
3081 Actions := No_List;
3082 end if;
3083 end;
3085 -- Composite component with its own Init_Proc
3087 elsif not Is_Interface (Typ)
3088 and then Has_Non_Null_Base_Init_Proc (Typ)
3089 then
3090 Actions :=
3091 Build_Initialization_Call
3092 (Comp_Loc,
3093 Make_Selected_Component (Comp_Loc,
3094 Prefix =>
3095 Make_Identifier (Comp_Loc, Name_uInit),
3096 Selector_Name => New_Occurrence_Of (Id, Comp_Loc)),
3097 Typ,
3098 In_Init_Proc => True,
3099 Enclos_Type => Rec_Type,
3100 Discr_Map => Discr_Map);
3102 Clean_Task_Names (Typ, Proc_Id);
3104 -- Simple initialization
3106 elsif Component_Needs_Simple_Initialization (Typ) then
3107 Actions :=
3108 Build_Assignment
3109 (Id => Id,
3110 Default =>
3111 Get_Simple_Init_Val
3112 (Typ => Typ,
3113 N => N,
3114 Size => Esize (Id)));
3116 -- Nothing needed for this case
3118 else
3119 Actions := No_List;
3120 end if;
3122 if Present (Checks) then
3123 if Chars (Id) = Name_uParent then
3124 Append_List_To (Parent_Stmts, Checks);
3125 else
3126 Append_List_To (Stmts, Checks);
3127 end if;
3128 end if;
3130 if Present (Actions) then
3131 if Chars (Id) = Name_uParent then
3132 Append_List_To (Parent_Stmts, Actions);
3134 else
3135 Append_List_To (Stmts, Actions);
3137 -- Preserve initialization state in the current counter
3139 if Needs_Finalization (Typ) then
3140 if No (Counter_Id) then
3141 Make_Counter (Comp_Loc);
3142 end if;
3144 Increment_Counter (Comp_Loc);
3145 end if;
3146 end if;
3147 end if;
3148 end if;
3150 Next_Non_Pragma (Decl);
3151 end loop;
3153 -- The parent field must be initialized first because variable
3154 -- size components of the parent affect the location of all the
3155 -- new components.
3157 Prepend_List_To (Stmts, Parent_Stmts);
3159 -- Set up tasks and protected object support. This needs to be done
3160 -- before any component with a per-object access discriminant
3161 -- constraint, or any variant part (which may contain such
3162 -- components) is initialized, because the initialization of these
3163 -- components may reference the enclosing concurrent object.
3165 -- For a task record type, add the task create call and calls to bind
3166 -- any interrupt (signal) entries.
3168 if Is_Task_Record_Type (Rec_Type) then
3170 -- In the case of the restricted run time the ATCB has already
3171 -- been preallocated.
3173 if Restricted_Profile then
3174 Append_To (Stmts,
3175 Make_Assignment_Statement (Loc,
3176 Name =>
3177 Make_Selected_Component (Loc,
3178 Prefix => Make_Identifier (Loc, Name_uInit),
3179 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)),
3180 Expression =>
3181 Make_Attribute_Reference (Loc,
3182 Prefix =>
3183 Make_Selected_Component (Loc,
3184 Prefix => Make_Identifier (Loc, Name_uInit),
3185 Selector_Name => Make_Identifier (Loc, Name_uATCB)),
3186 Attribute_Name => Name_Unchecked_Access)));
3187 end if;
3189 Append_To (Stmts, Make_Task_Create_Call (Rec_Type));
3191 declare
3192 Task_Type : constant Entity_Id :=
3193 Corresponding_Concurrent_Type (Rec_Type);
3194 Task_Decl : constant Node_Id := Parent (Task_Type);
3195 Task_Def : constant Node_Id := Task_Definition (Task_Decl);
3196 Decl_Loc : Source_Ptr;
3197 Ent : Entity_Id;
3198 Vis_Decl : Node_Id;
3200 begin
3201 if Present (Task_Def) then
3202 Vis_Decl := First (Visible_Declarations (Task_Def));
3203 while Present (Vis_Decl) loop
3204 Decl_Loc := Sloc (Vis_Decl);
3206 if Nkind (Vis_Decl) = N_Attribute_Definition_Clause then
3207 if Get_Attribute_Id (Chars (Vis_Decl)) =
3208 Attribute_Address
3209 then
3210 Ent := Entity (Name (Vis_Decl));
3212 if Ekind (Ent) = E_Entry then
3213 Append_To (Stmts,
3214 Make_Procedure_Call_Statement (Decl_Loc,
3215 Name =>
3216 New_Occurrence_Of (RTE (
3217 RE_Bind_Interrupt_To_Entry), Decl_Loc),
3218 Parameter_Associations => New_List (
3219 Make_Selected_Component (Decl_Loc,
3220 Prefix =>
3221 Make_Identifier (Decl_Loc, Name_uInit),
3222 Selector_Name =>
3223 Make_Identifier
3224 (Decl_Loc, Name_uTask_Id)),
3225 Entry_Index_Expression
3226 (Decl_Loc, Ent, Empty, Task_Type),
3227 Expression (Vis_Decl))));
3228 end if;
3229 end if;
3230 end if;
3232 Next (Vis_Decl);
3233 end loop;
3234 end if;
3235 end;
3236 end if;
3238 -- For a protected type, add statements generated by
3239 -- Make_Initialize_Protection.
3241 if Is_Protected_Record_Type (Rec_Type) then
3242 Append_List_To (Stmts,
3243 Make_Initialize_Protection (Rec_Type));
3244 end if;
3246 -- Second pass: components with per-object constraints
3248 if Has_POC then
3249 Decl := First_Non_Pragma (Component_Items (Comp_List));
3250 while Present (Decl) loop
3251 Comp_Loc := Sloc (Decl);
3252 Id := Defining_Identifier (Decl);
3253 Typ := Etype (Id);
3255 if Has_Access_Constraint (Id)
3256 and then No (Expression (Decl))
3257 then
3258 if Has_Non_Null_Base_Init_Proc (Typ) then
3259 Append_List_To (Stmts,
3260 Build_Initialization_Call (Comp_Loc,
3261 Make_Selected_Component (Comp_Loc,
3262 Prefix =>
3263 Make_Identifier (Comp_Loc, Name_uInit),
3264 Selector_Name => New_Occurrence_Of (Id, Comp_Loc)),
3265 Typ,
3266 In_Init_Proc => True,
3267 Enclos_Type => Rec_Type,
3268 Discr_Map => Discr_Map));
3270 Clean_Task_Names (Typ, Proc_Id);
3272 -- Preserve initialization state in the current counter
3274 if Needs_Finalization (Typ) then
3275 if No (Counter_Id) then
3276 Make_Counter (Comp_Loc);
3277 end if;
3279 Increment_Counter (Comp_Loc);
3280 end if;
3282 elsif Component_Needs_Simple_Initialization (Typ) then
3283 Append_List_To (Stmts,
3284 Build_Assignment
3285 (Id => Id,
3286 Default =>
3287 Get_Simple_Init_Val
3288 (Typ => Typ,
3289 N => N,
3290 Size => Esize (Id))));
3291 end if;
3292 end if;
3294 Next_Non_Pragma (Decl);
3295 end loop;
3296 end if;
3298 -- Process the variant part
3300 if Present (Variant_Part (Comp_List)) then
3301 declare
3302 Variant_Alts : constant List_Id := New_List;
3303 Var_Loc : Source_Ptr := No_Location;
3304 Variant : Node_Id;
3306 begin
3307 Variant :=
3308 First_Non_Pragma (Variants (Variant_Part (Comp_List)));
3309 while Present (Variant) loop
3310 Var_Loc := Sloc (Variant);
3311 Append_To (Variant_Alts,
3312 Make_Case_Statement_Alternative (Var_Loc,
3313 Discrete_Choices =>
3314 New_Copy_List (Discrete_Choices (Variant)),
3315 Statements =>
3316 Build_Init_Statements (Component_List (Variant))));
3317 Next_Non_Pragma (Variant);
3318 end loop;
3320 -- The expression of the case statement which is a reference
3321 -- to one of the discriminants is replaced by the appropriate
3322 -- formal parameter of the initialization procedure.
3324 Append_To (Stmts,
3325 Make_Case_Statement (Var_Loc,
3326 Expression =>
3327 New_Occurrence_Of (Discriminal (
3328 Entity (Name (Variant_Part (Comp_List)))), Var_Loc),
3329 Alternatives => Variant_Alts));
3330 end;
3331 end if;
3333 -- If no initializations when generated for component declarations
3334 -- corresponding to this Stmts, append a null statement to Stmts to
3335 -- to make it a valid Ada tree.
3337 if Is_Empty_List (Stmts) then
3338 Append (Make_Null_Statement (Loc), Stmts);
3339 end if;
3341 return Stmts;
3343 exception
3344 when RE_Not_Available =>
3345 return Empty_List;
3346 end Build_Init_Statements;
3348 -------------------------
3349 -- Build_Record_Checks --
3350 -------------------------
3352 procedure Build_Record_Checks (S : Node_Id; Check_List : List_Id) is
3353 Subtype_Mark_Id : Entity_Id;
3355 procedure Constrain_Array
3356 (SI : Node_Id;
3357 Check_List : List_Id);
3358 -- Apply a list of index constraints to an unconstrained array type.
3359 -- The first parameter is the entity for the resulting subtype.
3360 -- Check_List is a list to which the check actions are appended.
3362 ---------------------
3363 -- Constrain_Array --
3364 ---------------------
3366 procedure Constrain_Array
3367 (SI : Node_Id;
3368 Check_List : List_Id)
3370 C : constant Node_Id := Constraint (SI);
3371 Number_Of_Constraints : Nat := 0;
3372 Index : Node_Id;
3373 S, T : Entity_Id;
3375 procedure Constrain_Index
3376 (Index : Node_Id;
3377 S : Node_Id;
3378 Check_List : List_Id);
3379 -- Process an index constraint in a constrained array declaration.
3380 -- The constraint can be either a subtype name or a range with or
3381 -- without an explicit subtype mark. Index is the corresponding
3382 -- index of the unconstrained array. S is the range expression.
3383 -- Check_List is a list to which the check actions are appended.
3385 ---------------------
3386 -- Constrain_Index --
3387 ---------------------
3389 procedure Constrain_Index
3390 (Index : Node_Id;
3391 S : Node_Id;
3392 Check_List : List_Id)
3394 T : constant Entity_Id := Etype (Index);
3396 begin
3397 if Nkind (S) = N_Range then
3398 Process_Range_Expr_In_Decl (S, T, Check_List => Check_List);
3399 end if;
3400 end Constrain_Index;
3402 -- Start of processing for Constrain_Array
3404 begin
3405 T := Entity (Subtype_Mark (SI));
3407 if Is_Access_Type (T) then
3408 T := Designated_Type (T);
3409 end if;
3411 S := First (Constraints (C));
3412 while Present (S) loop
3413 Number_Of_Constraints := Number_Of_Constraints + 1;
3414 Next (S);
3415 end loop;
3417 -- In either case, the index constraint must provide a discrete
3418 -- range for each index of the array type and the type of each
3419 -- discrete range must be the same as that of the corresponding
3420 -- index. (RM 3.6.1)
3422 S := First (Constraints (C));
3423 Index := First_Index (T);
3424 Analyze (Index);
3426 -- Apply constraints to each index type
3428 for J in 1 .. Number_Of_Constraints loop
3429 Constrain_Index (Index, S, Check_List);
3430 Next (Index);
3431 Next (S);
3432 end loop;
3433 end Constrain_Array;
3435 -- Start of processing for Build_Record_Checks
3437 begin
3438 if Nkind (S) = N_Subtype_Indication then
3439 Find_Type (Subtype_Mark (S));
3440 Subtype_Mark_Id := Entity (Subtype_Mark (S));
3442 -- Remaining processing depends on type
3444 case Ekind (Subtype_Mark_Id) is
3445 when Array_Kind =>
3446 Constrain_Array (S, Check_List);
3448 when others =>
3449 null;
3450 end case;
3451 end if;
3452 end Build_Record_Checks;
3454 -------------------------------------------
3455 -- Component_Needs_Simple_Initialization --
3456 -------------------------------------------
3458 function Component_Needs_Simple_Initialization
3459 (T : Entity_Id) return Boolean
3461 begin
3462 return
3463 Needs_Simple_Initialization (T)
3464 and then not Is_RTE (T, RE_Tag)
3466 -- Ada 2005 (AI-251): Check also the tag of abstract interfaces
3468 and then not Is_RTE (T, RE_Interface_Tag);
3469 end Component_Needs_Simple_Initialization;
3471 --------------------------------------
3472 -- Parent_Subtype_Renaming_Discrims --
3473 --------------------------------------
3475 function Parent_Subtype_Renaming_Discrims return Boolean is
3476 De : Entity_Id;
3477 Dp : Entity_Id;
3479 begin
3480 if Base_Type (Rec_Ent) /= Rec_Ent then
3481 return False;
3482 end if;
3484 if Etype (Rec_Ent) = Rec_Ent
3485 or else not Has_Discriminants (Rec_Ent)
3486 or else Is_Constrained (Rec_Ent)
3487 or else Is_Tagged_Type (Rec_Ent)
3488 then
3489 return False;
3490 end if;
3492 -- If there are no explicit stored discriminants we have inherited
3493 -- the root type discriminants so far, so no renamings occurred.
3495 if First_Discriminant (Rec_Ent) =
3496 First_Stored_Discriminant (Rec_Ent)
3497 then
3498 return False;
3499 end if;
3501 -- Check if we have done some trivial renaming of the parent
3502 -- discriminants, i.e. something like
3504 -- type DT (X1, X2: int) is new PT (X1, X2);
3506 De := First_Discriminant (Rec_Ent);
3507 Dp := First_Discriminant (Etype (Rec_Ent));
3508 while Present (De) loop
3509 pragma Assert (Present (Dp));
3511 if Corresponding_Discriminant (De) /= Dp then
3512 return True;
3513 end if;
3515 Next_Discriminant (De);
3516 Next_Discriminant (Dp);
3517 end loop;
3519 return Present (Dp);
3520 end Parent_Subtype_Renaming_Discrims;
3522 ------------------------
3523 -- Requires_Init_Proc --
3524 ------------------------
3526 function Requires_Init_Proc (Rec_Id : Entity_Id) return Boolean is
3527 Comp_Decl : Node_Id;
3528 Id : Entity_Id;
3529 Typ : Entity_Id;
3531 begin
3532 -- Definitely do not need one if specifically suppressed
3534 if Initialization_Suppressed (Rec_Id) then
3535 return False;
3536 end if;
3538 -- If it is a type derived from a type with unknown discriminants,
3539 -- we cannot build an initialization procedure for it.
3541 if Has_Unknown_Discriminants (Rec_Id)
3542 or else Has_Unknown_Discriminants (Etype (Rec_Id))
3543 then
3544 return False;
3545 end if;
3547 -- Otherwise we need to generate an initialization procedure if
3548 -- Is_CPP_Class is False and at least one of the following applies:
3550 -- 1. Discriminants are present, since they need to be initialized
3551 -- with the appropriate discriminant constraint expressions.
3552 -- However, the discriminant of an unchecked union does not
3553 -- count, since the discriminant is not present.
3555 -- 2. The type is a tagged type, since the implicit Tag component
3556 -- needs to be initialized with a pointer to the dispatch table.
3558 -- 3. The type contains tasks
3560 -- 4. One or more components has an initial value
3562 -- 5. One or more components is for a type which itself requires
3563 -- an initialization procedure.
3565 -- 6. One or more components is a type that requires simple
3566 -- initialization (see Needs_Simple_Initialization), except
3567 -- that types Tag and Interface_Tag are excluded, since fields
3568 -- of these types are initialized by other means.
3570 -- 7. The type is the record type built for a task type (since at
3571 -- the very least, Create_Task must be called)
3573 -- 8. The type is the record type built for a protected type (since
3574 -- at least Initialize_Protection must be called)
3576 -- 9. The type is marked as a public entity. The reason we add this
3577 -- case (even if none of the above apply) is to properly handle
3578 -- Initialize_Scalars. If a package is compiled without an IS
3579 -- pragma, and the client is compiled with an IS pragma, then
3580 -- the client will think an initialization procedure is present
3581 -- and call it, when in fact no such procedure is required, but
3582 -- since the call is generated, there had better be a routine
3583 -- at the other end of the call, even if it does nothing).
3585 -- Note: the reason we exclude the CPP_Class case is because in this
3586 -- case the initialization is performed by the C++ constructors, and
3587 -- the IP is built by Set_CPP_Constructors.
3589 if Is_CPP_Class (Rec_Id) then
3590 return False;
3592 elsif Is_Interface (Rec_Id) then
3593 return False;
3595 elsif (Has_Discriminants (Rec_Id)
3596 and then not Is_Unchecked_Union (Rec_Id))
3597 or else Is_Tagged_Type (Rec_Id)
3598 or else Is_Concurrent_Record_Type (Rec_Id)
3599 or else Has_Task (Rec_Id)
3600 then
3601 return True;
3602 end if;
3604 Id := First_Component (Rec_Id);
3605 while Present (Id) loop
3606 Comp_Decl := Parent (Id);
3607 Typ := Etype (Id);
3609 if Present (Expression (Comp_Decl))
3610 or else Has_Non_Null_Base_Init_Proc (Typ)
3611 or else Component_Needs_Simple_Initialization (Typ)
3612 then
3613 return True;
3614 end if;
3616 Next_Component (Id);
3617 end loop;
3619 -- As explained above, a record initialization procedure is needed
3620 -- for public types in case Initialize_Scalars applies to a client.
3621 -- However, such a procedure is not needed in the case where either
3622 -- of restrictions No_Initialize_Scalars or No_Default_Initialization
3623 -- applies. No_Initialize_Scalars excludes the possibility of using
3624 -- Initialize_Scalars in any partition, and No_Default_Initialization
3625 -- implies that no initialization should ever be done for objects of
3626 -- the type, so is incompatible with Initialize_Scalars.
3628 if not Restriction_Active (No_Initialize_Scalars)
3629 and then not Restriction_Active (No_Default_Initialization)
3630 and then Is_Public (Rec_Id)
3631 then
3632 return True;
3633 end if;
3635 return False;
3636 end Requires_Init_Proc;
3638 -- Start of processing for Build_Record_Init_Proc
3640 begin
3641 Rec_Type := Defining_Identifier (N);
3643 -- This may be full declaration of a private type, in which case
3644 -- the visible entity is a record, and the private entity has been
3645 -- exchanged with it in the private part of the current package.
3646 -- The initialization procedure is built for the record type, which
3647 -- is retrievable from the private entity.
3649 if Is_Incomplete_Or_Private_Type (Rec_Type) then
3650 Rec_Type := Underlying_Type (Rec_Type);
3651 end if;
3653 -- If we have a variant record with restriction No_Implicit_Conditionals
3654 -- in effect, then we skip building the procedure. This is safe because
3655 -- if we can see the restriction, so can any caller, calls to initialize
3656 -- such records are not allowed for variant records if this restriction
3657 -- is active.
3659 if Has_Variant_Part (Rec_Type)
3660 and then Restriction_Active (No_Implicit_Conditionals)
3661 then
3662 return;
3663 end if;
3665 -- If there are discriminants, build the discriminant map to replace
3666 -- discriminants by their discriminals in complex bound expressions.
3667 -- These only arise for the corresponding records of synchronized types.
3669 if Is_Concurrent_Record_Type (Rec_Type)
3670 and then Has_Discriminants (Rec_Type)
3671 then
3672 declare
3673 Disc : Entity_Id;
3674 begin
3675 Disc := First_Discriminant (Rec_Type);
3676 while Present (Disc) loop
3677 Append_Elmt (Disc, Discr_Map);
3678 Append_Elmt (Discriminal (Disc), Discr_Map);
3679 Next_Discriminant (Disc);
3680 end loop;
3681 end;
3682 end if;
3684 -- Derived types that have no type extension can use the initialization
3685 -- procedure of their parent and do not need a procedure of their own.
3686 -- This is only correct if there are no representation clauses for the
3687 -- type or its parent, and if the parent has in fact been frozen so
3688 -- that its initialization procedure exists.
3690 if Is_Derived_Type (Rec_Type)
3691 and then not Is_Tagged_Type (Rec_Type)
3692 and then not Is_Unchecked_Union (Rec_Type)
3693 and then not Has_New_Non_Standard_Rep (Rec_Type)
3694 and then not Parent_Subtype_Renaming_Discrims
3695 and then Has_Non_Null_Base_Init_Proc (Etype (Rec_Type))
3696 then
3697 Copy_TSS (Base_Init_Proc (Etype (Rec_Type)), Rec_Type);
3699 -- Otherwise if we need an initialization procedure, then build one,
3700 -- mark it as public and inlinable and as having a completion.
3702 elsif Requires_Init_Proc (Rec_Type)
3703 or else Is_Unchecked_Union (Rec_Type)
3704 then
3705 Proc_Id :=
3706 Make_Defining_Identifier (Loc,
3707 Chars => Make_Init_Proc_Name (Rec_Type));
3709 -- If No_Default_Initialization restriction is active, then we don't
3710 -- want to build an init_proc, but we need to mark that an init_proc
3711 -- would be needed if this restriction was not active (so that we can
3712 -- detect attempts to call it), so set a dummy init_proc in place.
3714 if Restriction_Active (No_Default_Initialization) then
3715 Set_Init_Proc (Rec_Type, Proc_Id);
3716 return;
3717 end if;
3719 Build_Offset_To_Top_Functions;
3720 Build_CPP_Init_Procedure;
3721 Build_Init_Procedure;
3723 Set_Is_Public (Proc_Id, Is_Public (Rec_Ent));
3724 Set_Is_Internal (Proc_Id);
3725 Set_Has_Completion (Proc_Id);
3727 if not Debug_Generated_Code then
3728 Set_Debug_Info_Off (Proc_Id);
3729 end if;
3731 Set_Is_Inlined (Proc_Id, Inline_Init_Proc (Rec_Type));
3733 -- Do not build an aggregate if Modify_Tree_For_C, this isn't
3734 -- needed and may generate early references to non frozen types
3735 -- since we expand aggregate much more systematically.
3737 if Modify_Tree_For_C then
3738 return;
3739 end if;
3741 declare
3742 Agg : constant Node_Id :=
3743 Build_Equivalent_Record_Aggregate (Rec_Type);
3745 procedure Collect_Itypes (Comp : Node_Id);
3746 -- Generate references to itypes in the aggregate, because
3747 -- the first use of the aggregate may be in a nested scope.
3749 --------------------
3750 -- Collect_Itypes --
3751 --------------------
3753 procedure Collect_Itypes (Comp : Node_Id) is
3754 Ref : Node_Id;
3755 Sub_Aggr : Node_Id;
3756 Typ : constant Entity_Id := Etype (Comp);
3758 begin
3759 if Is_Array_Type (Typ) and then Is_Itype (Typ) then
3760 Ref := Make_Itype_Reference (Loc);
3761 Set_Itype (Ref, Typ);
3762 Append_Freeze_Action (Rec_Type, Ref);
3764 Ref := Make_Itype_Reference (Loc);
3765 Set_Itype (Ref, Etype (First_Index (Typ)));
3766 Append_Freeze_Action (Rec_Type, Ref);
3768 -- Recurse on nested arrays
3770 Sub_Aggr := First (Expressions (Comp));
3771 while Present (Sub_Aggr) loop
3772 Collect_Itypes (Sub_Aggr);
3773 Next (Sub_Aggr);
3774 end loop;
3775 end if;
3776 end Collect_Itypes;
3778 begin
3779 -- If there is a static initialization aggregate for the type,
3780 -- generate itype references for the types of its (sub)components,
3781 -- to prevent out-of-scope errors in the resulting tree.
3782 -- The aggregate may have been rewritten as a Raise node, in which
3783 -- case there are no relevant itypes.
3785 if Present (Agg) and then Nkind (Agg) = N_Aggregate then
3786 Set_Static_Initialization (Proc_Id, Agg);
3788 declare
3789 Comp : Node_Id;
3790 begin
3791 Comp := First (Component_Associations (Agg));
3792 while Present (Comp) loop
3793 Collect_Itypes (Expression (Comp));
3794 Next (Comp);
3795 end loop;
3796 end;
3797 end if;
3798 end;
3799 end if;
3800 end Build_Record_Init_Proc;
3802 ----------------------------
3803 -- Build_Slice_Assignment --
3804 ----------------------------
3806 -- Generates the following subprogram:
3808 -- procedure Assign
3809 -- (Source, Target : Array_Type,
3810 -- Left_Lo, Left_Hi : Index;
3811 -- Right_Lo, Right_Hi : Index;
3812 -- Rev : Boolean)
3813 -- is
3814 -- Li1 : Index;
3815 -- Ri1 : Index;
3817 -- begin
3819 -- if Left_Hi < Left_Lo then
3820 -- return;
3821 -- end if;
3823 -- if Rev then
3824 -- Li1 := Left_Hi;
3825 -- Ri1 := Right_Hi;
3826 -- else
3827 -- Li1 := Left_Lo;
3828 -- Ri1 := Right_Lo;
3829 -- end if;
3831 -- loop
3832 -- Target (Li1) := Source (Ri1);
3834 -- if Rev then
3835 -- exit when Li1 = Left_Lo;
3836 -- Li1 := Index'pred (Li1);
3837 -- Ri1 := Index'pred (Ri1);
3838 -- else
3839 -- exit when Li1 = Left_Hi;
3840 -- Li1 := Index'succ (Li1);
3841 -- Ri1 := Index'succ (Ri1);
3842 -- end if;
3843 -- end loop;
3844 -- end Assign;
3846 procedure Build_Slice_Assignment (Typ : Entity_Id) is
3847 Loc : constant Source_Ptr := Sloc (Typ);
3848 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
3850 Larray : constant Entity_Id := Make_Temporary (Loc, 'A');
3851 Rarray : constant Entity_Id := Make_Temporary (Loc, 'R');
3852 Left_Lo : constant Entity_Id := Make_Temporary (Loc, 'L');
3853 Left_Hi : constant Entity_Id := Make_Temporary (Loc, 'L');
3854 Right_Lo : constant Entity_Id := Make_Temporary (Loc, 'R');
3855 Right_Hi : constant Entity_Id := Make_Temporary (Loc, 'R');
3856 Rev : constant Entity_Id := Make_Temporary (Loc, 'D');
3857 -- Formal parameters of procedure
3859 Proc_Name : constant Entity_Id :=
3860 Make_Defining_Identifier (Loc,
3861 Chars => Make_TSS_Name (Typ, TSS_Slice_Assign));
3863 Lnn : constant Entity_Id := Make_Temporary (Loc, 'L');
3864 Rnn : constant Entity_Id := Make_Temporary (Loc, 'R');
3865 -- Subscripts for left and right sides
3867 Decls : List_Id;
3868 Loops : Node_Id;
3869 Stats : List_Id;
3871 begin
3872 -- Build declarations for indexes
3874 Decls := New_List;
3876 Append_To (Decls,
3877 Make_Object_Declaration (Loc,
3878 Defining_Identifier => Lnn,
3879 Object_Definition =>
3880 New_Occurrence_Of (Index, Loc)));
3882 Append_To (Decls,
3883 Make_Object_Declaration (Loc,
3884 Defining_Identifier => Rnn,
3885 Object_Definition =>
3886 New_Occurrence_Of (Index, Loc)));
3888 Stats := New_List;
3890 -- Build test for empty slice case
3892 Append_To (Stats,
3893 Make_If_Statement (Loc,
3894 Condition =>
3895 Make_Op_Lt (Loc,
3896 Left_Opnd => New_Occurrence_Of (Left_Hi, Loc),
3897 Right_Opnd => New_Occurrence_Of (Left_Lo, Loc)),
3898 Then_Statements => New_List (Make_Simple_Return_Statement (Loc))));
3900 -- Build initializations for indexes
3902 declare
3903 F_Init : constant List_Id := New_List;
3904 B_Init : constant List_Id := New_List;
3906 begin
3907 Append_To (F_Init,
3908 Make_Assignment_Statement (Loc,
3909 Name => New_Occurrence_Of (Lnn, Loc),
3910 Expression => New_Occurrence_Of (Left_Lo, Loc)));
3912 Append_To (F_Init,
3913 Make_Assignment_Statement (Loc,
3914 Name => New_Occurrence_Of (Rnn, Loc),
3915 Expression => New_Occurrence_Of (Right_Lo, Loc)));
3917 Append_To (B_Init,
3918 Make_Assignment_Statement (Loc,
3919 Name => New_Occurrence_Of (Lnn, Loc),
3920 Expression => New_Occurrence_Of (Left_Hi, Loc)));
3922 Append_To (B_Init,
3923 Make_Assignment_Statement (Loc,
3924 Name => New_Occurrence_Of (Rnn, Loc),
3925 Expression => New_Occurrence_Of (Right_Hi, Loc)));
3927 Append_To (Stats,
3928 Make_If_Statement (Loc,
3929 Condition => New_Occurrence_Of (Rev, Loc),
3930 Then_Statements => B_Init,
3931 Else_Statements => F_Init));
3932 end;
3934 -- Now construct the assignment statement
3936 Loops :=
3937 Make_Loop_Statement (Loc,
3938 Statements => New_List (
3939 Make_Assignment_Statement (Loc,
3940 Name =>
3941 Make_Indexed_Component (Loc,
3942 Prefix => New_Occurrence_Of (Larray, Loc),
3943 Expressions => New_List (New_Occurrence_Of (Lnn, Loc))),
3944 Expression =>
3945 Make_Indexed_Component (Loc,
3946 Prefix => New_Occurrence_Of (Rarray, Loc),
3947 Expressions => New_List (New_Occurrence_Of (Rnn, Loc))))),
3948 End_Label => Empty);
3950 -- Build the exit condition and increment/decrement statements
3952 declare
3953 F_Ass : constant List_Id := New_List;
3954 B_Ass : constant List_Id := New_List;
3956 begin
3957 Append_To (F_Ass,
3958 Make_Exit_Statement (Loc,
3959 Condition =>
3960 Make_Op_Eq (Loc,
3961 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
3962 Right_Opnd => New_Occurrence_Of (Left_Hi, Loc))));
3964 Append_To (F_Ass,
3965 Make_Assignment_Statement (Loc,
3966 Name => New_Occurrence_Of (Lnn, Loc),
3967 Expression =>
3968 Make_Attribute_Reference (Loc,
3969 Prefix =>
3970 New_Occurrence_Of (Index, Loc),
3971 Attribute_Name => Name_Succ,
3972 Expressions => New_List (
3973 New_Occurrence_Of (Lnn, Loc)))));
3975 Append_To (F_Ass,
3976 Make_Assignment_Statement (Loc,
3977 Name => New_Occurrence_Of (Rnn, Loc),
3978 Expression =>
3979 Make_Attribute_Reference (Loc,
3980 Prefix =>
3981 New_Occurrence_Of (Index, Loc),
3982 Attribute_Name => Name_Succ,
3983 Expressions => New_List (
3984 New_Occurrence_Of (Rnn, Loc)))));
3986 Append_To (B_Ass,
3987 Make_Exit_Statement (Loc,
3988 Condition =>
3989 Make_Op_Eq (Loc,
3990 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
3991 Right_Opnd => New_Occurrence_Of (Left_Lo, Loc))));
3993 Append_To (B_Ass,
3994 Make_Assignment_Statement (Loc,
3995 Name => New_Occurrence_Of (Lnn, Loc),
3996 Expression =>
3997 Make_Attribute_Reference (Loc,
3998 Prefix =>
3999 New_Occurrence_Of (Index, Loc),
4000 Attribute_Name => Name_Pred,
4001 Expressions => New_List (
4002 New_Occurrence_Of (Lnn, Loc)))));
4004 Append_To (B_Ass,
4005 Make_Assignment_Statement (Loc,
4006 Name => New_Occurrence_Of (Rnn, Loc),
4007 Expression =>
4008 Make_Attribute_Reference (Loc,
4009 Prefix =>
4010 New_Occurrence_Of (Index, Loc),
4011 Attribute_Name => Name_Pred,
4012 Expressions => New_List (
4013 New_Occurrence_Of (Rnn, Loc)))));
4015 Append_To (Statements (Loops),
4016 Make_If_Statement (Loc,
4017 Condition => New_Occurrence_Of (Rev, Loc),
4018 Then_Statements => B_Ass,
4019 Else_Statements => F_Ass));
4020 end;
4022 Append_To (Stats, Loops);
4024 declare
4025 Spec : Node_Id;
4026 Formals : List_Id := New_List;
4028 begin
4029 Formals := New_List (
4030 Make_Parameter_Specification (Loc,
4031 Defining_Identifier => Larray,
4032 Out_Present => True,
4033 Parameter_Type =>
4034 New_Occurrence_Of (Base_Type (Typ), Loc)),
4036 Make_Parameter_Specification (Loc,
4037 Defining_Identifier => Rarray,
4038 Parameter_Type =>
4039 New_Occurrence_Of (Base_Type (Typ), Loc)),
4041 Make_Parameter_Specification (Loc,
4042 Defining_Identifier => Left_Lo,
4043 Parameter_Type =>
4044 New_Occurrence_Of (Index, Loc)),
4046 Make_Parameter_Specification (Loc,
4047 Defining_Identifier => Left_Hi,
4048 Parameter_Type =>
4049 New_Occurrence_Of (Index, Loc)),
4051 Make_Parameter_Specification (Loc,
4052 Defining_Identifier => Right_Lo,
4053 Parameter_Type =>
4054 New_Occurrence_Of (Index, Loc)),
4056 Make_Parameter_Specification (Loc,
4057 Defining_Identifier => Right_Hi,
4058 Parameter_Type =>
4059 New_Occurrence_Of (Index, Loc)));
4061 Append_To (Formals,
4062 Make_Parameter_Specification (Loc,
4063 Defining_Identifier => Rev,
4064 Parameter_Type =>
4065 New_Occurrence_Of (Standard_Boolean, Loc)));
4067 Spec :=
4068 Make_Procedure_Specification (Loc,
4069 Defining_Unit_Name => Proc_Name,
4070 Parameter_Specifications => Formals);
4072 Discard_Node (
4073 Make_Subprogram_Body (Loc,
4074 Specification => Spec,
4075 Declarations => Decls,
4076 Handled_Statement_Sequence =>
4077 Make_Handled_Sequence_Of_Statements (Loc,
4078 Statements => Stats)));
4079 end;
4081 Set_TSS (Typ, Proc_Name);
4082 Set_Is_Pure (Proc_Name);
4083 end Build_Slice_Assignment;
4085 -----------------------------
4086 -- Build_Untagged_Equality --
4087 -----------------------------
4089 procedure Build_Untagged_Equality (Typ : Entity_Id) is
4090 Build_Eq : Boolean;
4091 Comp : Entity_Id;
4092 Decl : Node_Id;
4093 Op : Entity_Id;
4094 Prim : Elmt_Id;
4095 Eq_Op : Entity_Id;
4097 function User_Defined_Eq (T : Entity_Id) return Entity_Id;
4098 -- Check whether the type T has a user-defined primitive equality. If so
4099 -- return it, else return Empty. If true for a component of Typ, we have
4100 -- to build the primitive equality for it.
4102 ---------------------
4103 -- User_Defined_Eq --
4104 ---------------------
4106 function User_Defined_Eq (T : Entity_Id) return Entity_Id is
4107 Prim : Elmt_Id;
4108 Op : Entity_Id;
4110 begin
4111 Op := TSS (T, TSS_Composite_Equality);
4113 if Present (Op) then
4114 return Op;
4115 end if;
4117 Prim := First_Elmt (Collect_Primitive_Operations (T));
4118 while Present (Prim) loop
4119 Op := Node (Prim);
4121 if Chars (Op) = Name_Op_Eq
4122 and then Etype (Op) = Standard_Boolean
4123 and then Etype (First_Formal (Op)) = T
4124 and then Etype (Next_Formal (First_Formal (Op))) = T
4125 then
4126 return Op;
4127 end if;
4129 Next_Elmt (Prim);
4130 end loop;
4132 return Empty;
4133 end User_Defined_Eq;
4135 -- Start of processing for Build_Untagged_Equality
4137 begin
4138 -- If a record component has a primitive equality operation, we must
4139 -- build the corresponding one for the current type.
4141 Build_Eq := False;
4142 Comp := First_Component (Typ);
4143 while Present (Comp) loop
4144 if Is_Record_Type (Etype (Comp))
4145 and then Present (User_Defined_Eq (Etype (Comp)))
4146 then
4147 Build_Eq := True;
4148 end if;
4150 Next_Component (Comp);
4151 end loop;
4153 -- If there is a user-defined equality for the type, we do not create
4154 -- the implicit one.
4156 Prim := First_Elmt (Collect_Primitive_Operations (Typ));
4157 Eq_Op := Empty;
4158 while Present (Prim) loop
4159 if Chars (Node (Prim)) = Name_Op_Eq
4160 and then Comes_From_Source (Node (Prim))
4162 -- Don't we also need to check formal types and return type as in
4163 -- User_Defined_Eq above???
4165 then
4166 Eq_Op := Node (Prim);
4167 Build_Eq := False;
4168 exit;
4169 end if;
4171 Next_Elmt (Prim);
4172 end loop;
4174 -- If the type is derived, inherit the operation, if present, from the
4175 -- parent type. It may have been declared after the type derivation. If
4176 -- the parent type itself is derived, it may have inherited an operation
4177 -- that has itself been overridden, so update its alias and related
4178 -- flags. Ditto for inequality.
4180 if No (Eq_Op) and then Is_Derived_Type (Typ) then
4181 Prim := First_Elmt (Collect_Primitive_Operations (Etype (Typ)));
4182 while Present (Prim) loop
4183 if Chars (Node (Prim)) = Name_Op_Eq then
4184 Copy_TSS (Node (Prim), Typ);
4185 Build_Eq := False;
4187 declare
4188 Op : constant Entity_Id := User_Defined_Eq (Typ);
4189 Eq_Op : constant Entity_Id := Node (Prim);
4190 NE_Op : constant Entity_Id := Next_Entity (Eq_Op);
4192 begin
4193 if Present (Op) then
4194 Set_Alias (Op, Eq_Op);
4195 Set_Is_Abstract_Subprogram
4196 (Op, Is_Abstract_Subprogram (Eq_Op));
4198 if Chars (Next_Entity (Op)) = Name_Op_Ne then
4199 Set_Is_Abstract_Subprogram
4200 (Next_Entity (Op), Is_Abstract_Subprogram (NE_Op));
4201 end if;
4202 end if;
4203 end;
4205 exit;
4206 end if;
4208 Next_Elmt (Prim);
4209 end loop;
4210 end if;
4212 -- If not inherited and not user-defined, build body as for a type with
4213 -- tagged components.
4215 if Build_Eq then
4216 Decl :=
4217 Make_Eq_Body (Typ, Make_TSS_Name (Typ, TSS_Composite_Equality));
4218 Op := Defining_Entity (Decl);
4219 Set_TSS (Typ, Op);
4220 Set_Is_Pure (Op);
4222 if Is_Library_Level_Entity (Typ) then
4223 Set_Is_Public (Op);
4224 end if;
4225 end if;
4226 end Build_Untagged_Equality;
4228 -----------------------------------
4229 -- Build_Variant_Record_Equality --
4230 -----------------------------------
4232 -- Generates:
4234 -- function <<Body_Id>> (Left, Right : T) return Boolean is
4235 -- [ X : T renames Left; ]
4236 -- [ Y : T renames Right; ]
4237 -- -- The above renamings are generated only if the parameters of
4238 -- -- this built function (which are passed by the caller) are not
4239 -- -- named 'X' and 'Y'; these names are required to reuse several
4240 -- -- expander routines when generating this body.
4242 -- begin
4243 -- -- Compare discriminants
4245 -- if X.D1 /= Y.D1 or else X.D2 /= Y.D2 or else ... then
4246 -- return False;
4247 -- end if;
4249 -- -- Compare components
4251 -- if X.C1 /= Y.C1 or else X.C2 /= Y.C2 or else ... then
4252 -- return False;
4253 -- end if;
4255 -- -- Compare variant part
4257 -- case X.D1 is
4258 -- when V1 =>
4259 -- if X.C2 /= Y.C2 or else X.C3 /= Y.C3 or else ... then
4260 -- return False;
4261 -- end if;
4262 -- ...
4263 -- when Vn =>
4264 -- if X.Cn /= Y.Cn or else ... then
4265 -- return False;
4266 -- end if;
4267 -- end case;
4269 -- return True;
4270 -- end _Equality;
4272 function Build_Variant_Record_Equality
4273 (Typ : Entity_Id;
4274 Body_Id : Entity_Id;
4275 Param_Specs : List_Id) return Node_Id
4277 Loc : constant Source_Ptr := Sloc (Typ);
4278 Def : constant Node_Id := Parent (Typ);
4279 Comps : constant Node_Id := Component_List (Type_Definition (Def));
4280 Left : constant Entity_Id := Defining_Identifier (First (Param_Specs));
4281 Right : constant Entity_Id :=
4282 Defining_Identifier (Next (First (Param_Specs)));
4283 Decls : constant List_Id := New_List;
4284 Stmts : constant List_Id := New_List;
4286 Subp_Body : Node_Id;
4288 begin
4289 pragma Assert (not Is_Tagged_Type (Typ));
4291 -- In order to reuse the expander routines Make_Eq_If and Make_Eq_Case
4292 -- the name of the formals must be X and Y; otherwise we generate two
4293 -- renaming declarations for such purpose.
4295 if Chars (Left) /= Name_X then
4296 Append_To (Decls,
4297 Make_Object_Renaming_Declaration (Loc,
4298 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
4299 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
4300 Name => Make_Identifier (Loc, Chars (Left))));
4301 end if;
4303 if Chars (Right) /= Name_Y then
4304 Append_To (Decls,
4305 Make_Object_Renaming_Declaration (Loc,
4306 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
4307 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
4308 Name => Make_Identifier (Loc, Chars (Right))));
4309 end if;
4311 -- Unchecked_Unions require additional machinery to support equality.
4312 -- Two extra parameters (A and B) are added to the equality function
4313 -- parameter list for each discriminant of the type, in order to
4314 -- capture the inferred values of the discriminants in equality calls.
4315 -- The names of the parameters match the names of the corresponding
4316 -- discriminant, with an added suffix.
4318 if Is_Unchecked_Union (Typ) then
4319 declare
4320 A : Entity_Id;
4321 B : Entity_Id;
4322 Discr : Entity_Id;
4323 Discr_Type : Entity_Id;
4324 New_Discrs : Elist_Id;
4326 begin
4327 New_Discrs := New_Elmt_List;
4329 Discr := First_Discriminant (Typ);
4330 while Present (Discr) loop
4331 Discr_Type := Etype (Discr);
4333 A :=
4334 Make_Defining_Identifier (Loc,
4335 Chars => New_External_Name (Chars (Discr), 'A'));
4337 B :=
4338 Make_Defining_Identifier (Loc,
4339 Chars => New_External_Name (Chars (Discr), 'B'));
4341 -- Add new parameters to the parameter list
4343 Append_To (Param_Specs,
4344 Make_Parameter_Specification (Loc,
4345 Defining_Identifier => A,
4346 Parameter_Type =>
4347 New_Occurrence_Of (Discr_Type, Loc)));
4349 Append_To (Param_Specs,
4350 Make_Parameter_Specification (Loc,
4351 Defining_Identifier => B,
4352 Parameter_Type =>
4353 New_Occurrence_Of (Discr_Type, Loc)));
4355 Append_Elmt (A, New_Discrs);
4357 -- Generate the following code to compare each of the inferred
4358 -- discriminants:
4360 -- if a /= b then
4361 -- return False;
4362 -- end if;
4364 Append_To (Stmts,
4365 Make_If_Statement (Loc,
4366 Condition =>
4367 Make_Op_Ne (Loc,
4368 Left_Opnd => New_Occurrence_Of (A, Loc),
4369 Right_Opnd => New_Occurrence_Of (B, Loc)),
4370 Then_Statements => New_List (
4371 Make_Simple_Return_Statement (Loc,
4372 Expression =>
4373 New_Occurrence_Of (Standard_False, Loc)))));
4374 Next_Discriminant (Discr);
4375 end loop;
4377 -- Generate component-by-component comparison. Note that we must
4378 -- propagate the inferred discriminants formals to act as the case
4379 -- statement switch. Their value is added when an equality call on
4380 -- unchecked unions is expanded.
4382 Append_List_To (Stmts, Make_Eq_Case (Typ, Comps, New_Discrs));
4383 end;
4385 -- Normal case (not unchecked union)
4387 else
4388 Append_To (Stmts,
4389 Make_Eq_If (Typ, Discriminant_Specifications (Def)));
4390 Append_List_To (Stmts, Make_Eq_Case (Typ, Comps));
4391 end if;
4393 Append_To (Stmts,
4394 Make_Simple_Return_Statement (Loc,
4395 Expression => New_Occurrence_Of (Standard_True, Loc)));
4397 Subp_Body :=
4398 Make_Subprogram_Body (Loc,
4399 Specification =>
4400 Make_Function_Specification (Loc,
4401 Defining_Unit_Name => Body_Id,
4402 Parameter_Specifications => Param_Specs,
4403 Result_Definition =>
4404 New_Occurrence_Of (Standard_Boolean, Loc)),
4405 Declarations => Decls,
4406 Handled_Statement_Sequence =>
4407 Make_Handled_Sequence_Of_Statements (Loc,
4408 Statements => Stmts));
4410 return Subp_Body;
4411 end Build_Variant_Record_Equality;
4413 -----------------------------
4414 -- Check_Stream_Attributes --
4415 -----------------------------
4417 procedure Check_Stream_Attributes (Typ : Entity_Id) is
4418 Comp : Entity_Id;
4419 Par_Read : constant Boolean :=
4420 Stream_Attribute_Available (Typ, TSS_Stream_Read)
4421 and then not Has_Specified_Stream_Read (Typ);
4422 Par_Write : constant Boolean :=
4423 Stream_Attribute_Available (Typ, TSS_Stream_Write)
4424 and then not Has_Specified_Stream_Write (Typ);
4426 procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type);
4427 -- Check that Comp has a user-specified Nam stream attribute
4429 ----------------
4430 -- Check_Attr --
4431 ----------------
4433 procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type) is
4434 begin
4435 if not Stream_Attribute_Available (Etype (Comp), TSS_Nam) then
4436 Error_Msg_Name_1 := Nam;
4437 Error_Msg_N
4438 ("|component& in limited extension must have% attribute", Comp);
4439 end if;
4440 end Check_Attr;
4442 -- Start of processing for Check_Stream_Attributes
4444 begin
4445 if Par_Read or else Par_Write then
4446 Comp := First_Component (Typ);
4447 while Present (Comp) loop
4448 if Comes_From_Source (Comp)
4449 and then Original_Record_Component (Comp) = Comp
4450 and then Is_Limited_Type (Etype (Comp))
4451 then
4452 if Par_Read then
4453 Check_Attr (Name_Read, TSS_Stream_Read);
4454 end if;
4456 if Par_Write then
4457 Check_Attr (Name_Write, TSS_Stream_Write);
4458 end if;
4459 end if;
4461 Next_Component (Comp);
4462 end loop;
4463 end if;
4464 end Check_Stream_Attributes;
4466 ----------------------
4467 -- Clean_Task_Names --
4468 ----------------------
4470 procedure Clean_Task_Names
4471 (Typ : Entity_Id;
4472 Proc_Id : Entity_Id)
4474 begin
4475 if Has_Task (Typ)
4476 and then not Restriction_Active (No_Implicit_Heap_Allocations)
4477 and then not Global_Discard_Names
4478 and then Tagged_Type_Expansion
4479 then
4480 Set_Uses_Sec_Stack (Proc_Id);
4481 end if;
4482 end Clean_Task_Names;
4484 ------------------------------
4485 -- Expand_Freeze_Array_Type --
4486 ------------------------------
4488 procedure Expand_Freeze_Array_Type (N : Node_Id) is
4489 Typ : constant Entity_Id := Entity (N);
4490 Base : constant Entity_Id := Base_Type (Typ);
4491 Comp_Typ : constant Entity_Id := Component_Type (Typ);
4493 begin
4494 if not Is_Bit_Packed_Array (Typ) then
4496 -- If the component contains tasks, so does the array type. This may
4497 -- not be indicated in the array type because the component may have
4498 -- been a private type at the point of definition. Same if component
4499 -- type is controlled or contains protected objects.
4501 Propagate_Concurrent_Flags (Base, Comp_Typ);
4502 Set_Has_Controlled_Component
4503 (Base, Has_Controlled_Component (Comp_Typ)
4504 or else Is_Controlled (Comp_Typ));
4506 if No (Init_Proc (Base)) then
4508 -- If this is an anonymous array created for a declaration with
4509 -- an initial value, its init_proc will never be called. The
4510 -- initial value itself may have been expanded into assignments,
4511 -- in which case the object declaration is carries the
4512 -- No_Initialization flag.
4514 if Is_Itype (Base)
4515 and then Nkind (Associated_Node_For_Itype (Base)) =
4516 N_Object_Declaration
4517 and then
4518 (Present (Expression (Associated_Node_For_Itype (Base)))
4519 or else No_Initialization (Associated_Node_For_Itype (Base)))
4520 then
4521 null;
4523 -- We do not need an init proc for string or wide [wide] string,
4524 -- since the only time these need initialization in normalize or
4525 -- initialize scalars mode, and these types are treated specially
4526 -- and do not need initialization procedures.
4528 elsif Is_Standard_String_Type (Base) then
4529 null;
4531 -- Otherwise we have to build an init proc for the subtype
4533 else
4534 Build_Array_Init_Proc (Base, N);
4535 end if;
4536 end if;
4538 if Typ = Base and then Has_Controlled_Component (Base) then
4539 Build_Controlling_Procs (Base);
4541 if not Is_Limited_Type (Comp_Typ)
4542 and then Number_Dimensions (Typ) = 1
4543 then
4544 Build_Slice_Assignment (Typ);
4545 end if;
4546 end if;
4548 -- For packed case, default initialization, except if the component type
4549 -- is itself a packed structure with an initialization procedure, or
4550 -- initialize/normalize scalars active, and we have a base type, or the
4551 -- type is public, because in that case a client might specify
4552 -- Normalize_Scalars and there better be a public Init_Proc for it.
4554 elsif (Present (Init_Proc (Component_Type (Base)))
4555 and then No (Base_Init_Proc (Base)))
4556 or else (Init_Or_Norm_Scalars and then Base = Typ)
4557 or else Is_Public (Typ)
4558 then
4559 Build_Array_Init_Proc (Base, N);
4560 end if;
4561 end Expand_Freeze_Array_Type;
4563 -----------------------------------
4564 -- Expand_Freeze_Class_Wide_Type --
4565 -----------------------------------
4567 procedure Expand_Freeze_Class_Wide_Type (N : Node_Id) is
4568 function Is_C_Derivation (Typ : Entity_Id) return Boolean;
4569 -- Given a type, determine whether it is derived from a C or C++ root
4571 ---------------------
4572 -- Is_C_Derivation --
4573 ---------------------
4575 function Is_C_Derivation (Typ : Entity_Id) return Boolean is
4576 T : Entity_Id;
4578 begin
4579 T := Typ;
4580 loop
4581 if Is_CPP_Class (T)
4582 or else Convention (T) = Convention_C
4583 or else Convention (T) = Convention_CPP
4584 then
4585 return True;
4586 end if;
4588 exit when T = Etype (T);
4590 T := Etype (T);
4591 end loop;
4593 return False;
4594 end Is_C_Derivation;
4596 -- Local variables
4598 Typ : constant Entity_Id := Entity (N);
4599 Root : constant Entity_Id := Root_Type (Typ);
4601 -- Start of processing for Expand_Freeze_Class_Wide_Type
4603 begin
4604 -- Certain run-time configurations and targets do not provide support
4605 -- for controlled types.
4607 if Restriction_Active (No_Finalization) then
4608 return;
4610 -- Do not create TSS routine Finalize_Address when dispatching calls are
4611 -- disabled since the core of the routine is a dispatching call.
4613 elsif Restriction_Active (No_Dispatching_Calls) then
4614 return;
4616 -- Do not create TSS routine Finalize_Address for concurrent class-wide
4617 -- types. Ignore C, C++, CIL and Java types since it is assumed that the
4618 -- non-Ada side will handle their destruction.
4620 elsif Is_Concurrent_Type (Root)
4621 or else Is_C_Derivation (Root)
4622 or else Convention (Typ) = Convention_CPP
4623 then
4624 return;
4626 -- Do not create TSS routine Finalize_Address when compiling in CodePeer
4627 -- mode since the routine contains an Unchecked_Conversion.
4629 elsif CodePeer_Mode then
4630 return;
4631 end if;
4633 -- Create the body of TSS primitive Finalize_Address. This automatically
4634 -- sets the TSS entry for the class-wide type.
4636 Make_Finalize_Address_Body (Typ);
4637 end Expand_Freeze_Class_Wide_Type;
4639 ------------------------------------
4640 -- Expand_Freeze_Enumeration_Type --
4641 ------------------------------------
4643 procedure Expand_Freeze_Enumeration_Type (N : Node_Id) is
4644 Typ : constant Entity_Id := Entity (N);
4645 Loc : constant Source_Ptr := Sloc (Typ);
4647 Arr : Entity_Id;
4648 Ent : Entity_Id;
4649 Fent : Entity_Id;
4650 Is_Contiguous : Boolean;
4651 Ityp : Entity_Id;
4652 Last_Repval : Uint;
4653 Lst : List_Id;
4654 Num : Nat;
4655 Pos_Expr : Node_Id;
4657 Func : Entity_Id;
4658 pragma Warnings (Off, Func);
4660 begin
4661 -- Various optimizations possible if given representation is contiguous
4663 Is_Contiguous := True;
4665 Ent := First_Literal (Typ);
4666 Last_Repval := Enumeration_Rep (Ent);
4668 Next_Literal (Ent);
4669 while Present (Ent) loop
4670 if Enumeration_Rep (Ent) - Last_Repval /= 1 then
4671 Is_Contiguous := False;
4672 exit;
4673 else
4674 Last_Repval := Enumeration_Rep (Ent);
4675 end if;
4677 Next_Literal (Ent);
4678 end loop;
4680 if Is_Contiguous then
4681 Set_Has_Contiguous_Rep (Typ);
4682 Ent := First_Literal (Typ);
4683 Num := 1;
4684 Lst := New_List (New_Occurrence_Of (Ent, Sloc (Ent)));
4686 else
4687 -- Build list of literal references
4689 Lst := New_List;
4690 Num := 0;
4692 Ent := First_Literal (Typ);
4693 while Present (Ent) loop
4694 Append_To (Lst, New_Occurrence_Of (Ent, Sloc (Ent)));
4695 Num := Num + 1;
4696 Next_Literal (Ent);
4697 end loop;
4698 end if;
4700 -- Now build an array declaration
4702 -- typA : array (Natural range 0 .. num - 1) of ctype :=
4703 -- (v, v, v, v, v, ....)
4705 -- where ctype is the corresponding integer type. If the representation
4706 -- is contiguous, we only keep the first literal, which provides the
4707 -- offset for Pos_To_Rep computations.
4709 Arr :=
4710 Make_Defining_Identifier (Loc,
4711 Chars => New_External_Name (Chars (Typ), 'A'));
4713 Append_Freeze_Action (Typ,
4714 Make_Object_Declaration (Loc,
4715 Defining_Identifier => Arr,
4716 Constant_Present => True,
4718 Object_Definition =>
4719 Make_Constrained_Array_Definition (Loc,
4720 Discrete_Subtype_Definitions => New_List (
4721 Make_Subtype_Indication (Loc,
4722 Subtype_Mark => New_Occurrence_Of (Standard_Natural, Loc),
4723 Constraint =>
4724 Make_Range_Constraint (Loc,
4725 Range_Expression =>
4726 Make_Range (Loc,
4727 Low_Bound =>
4728 Make_Integer_Literal (Loc, 0),
4729 High_Bound =>
4730 Make_Integer_Literal (Loc, Num - 1))))),
4732 Component_Definition =>
4733 Make_Component_Definition (Loc,
4734 Aliased_Present => False,
4735 Subtype_Indication => New_Occurrence_Of (Typ, Loc))),
4737 Expression =>
4738 Make_Aggregate (Loc,
4739 Expressions => Lst)));
4741 Set_Enum_Pos_To_Rep (Typ, Arr);
4743 -- Now we build the function that converts representation values to
4744 -- position values. This function has the form:
4746 -- function _Rep_To_Pos (A : etype; F : Boolean) return Integer is
4747 -- begin
4748 -- case ityp!(A) is
4749 -- when enum-lit'Enum_Rep => return posval;
4750 -- when enum-lit'Enum_Rep => return posval;
4751 -- ...
4752 -- when others =>
4753 -- [raise Constraint_Error when F "invalid data"]
4754 -- return -1;
4755 -- end case;
4756 -- end;
4758 -- Note: the F parameter determines whether the others case (no valid
4759 -- representation) raises Constraint_Error or returns a unique value
4760 -- of minus one. The latter case is used, e.g. in 'Valid code.
4762 -- Note: the reason we use Enum_Rep values in the case here is to avoid
4763 -- the code generator making inappropriate assumptions about the range
4764 -- of the values in the case where the value is invalid. ityp is a
4765 -- signed or unsigned integer type of appropriate width.
4767 -- Note: if exceptions are not supported, then we suppress the raise
4768 -- and return -1 unconditionally (this is an erroneous program in any
4769 -- case and there is no obligation to raise Constraint_Error here). We
4770 -- also do this if pragma Restrictions (No_Exceptions) is active.
4772 -- Is this right??? What about No_Exception_Propagation???
4774 -- Representations are signed
4776 if Enumeration_Rep (First_Literal (Typ)) < 0 then
4778 -- The underlying type is signed. Reset the Is_Unsigned_Type
4779 -- explicitly, because it might have been inherited from
4780 -- parent type.
4782 Set_Is_Unsigned_Type (Typ, False);
4784 if Esize (Typ) <= Standard_Integer_Size then
4785 Ityp := Standard_Integer;
4786 else
4787 Ityp := Universal_Integer;
4788 end if;
4790 -- Representations are unsigned
4792 else
4793 if Esize (Typ) <= Standard_Integer_Size then
4794 Ityp := RTE (RE_Unsigned);
4795 else
4796 Ityp := RTE (RE_Long_Long_Unsigned);
4797 end if;
4798 end if;
4800 -- The body of the function is a case statement. First collect case
4801 -- alternatives, or optimize the contiguous case.
4803 Lst := New_List;
4805 -- If representation is contiguous, Pos is computed by subtracting
4806 -- the representation of the first literal.
4808 if Is_Contiguous then
4809 Ent := First_Literal (Typ);
4811 if Enumeration_Rep (Ent) = Last_Repval then
4813 -- Another special case: for a single literal, Pos is zero
4815 Pos_Expr := Make_Integer_Literal (Loc, Uint_0);
4817 else
4818 Pos_Expr :=
4819 Convert_To (Standard_Integer,
4820 Make_Op_Subtract (Loc,
4821 Left_Opnd =>
4822 Unchecked_Convert_To
4823 (Ityp, Make_Identifier (Loc, Name_uA)),
4824 Right_Opnd =>
4825 Make_Integer_Literal (Loc,
4826 Intval => Enumeration_Rep (First_Literal (Typ)))));
4827 end if;
4829 Append_To (Lst,
4830 Make_Case_Statement_Alternative (Loc,
4831 Discrete_Choices => New_List (
4832 Make_Range (Sloc (Enumeration_Rep_Expr (Ent)),
4833 Low_Bound =>
4834 Make_Integer_Literal (Loc,
4835 Intval => Enumeration_Rep (Ent)),
4836 High_Bound =>
4837 Make_Integer_Literal (Loc, Intval => Last_Repval))),
4839 Statements => New_List (
4840 Make_Simple_Return_Statement (Loc,
4841 Expression => Pos_Expr))));
4843 else
4844 Ent := First_Literal (Typ);
4845 while Present (Ent) loop
4846 Append_To (Lst,
4847 Make_Case_Statement_Alternative (Loc,
4848 Discrete_Choices => New_List (
4849 Make_Integer_Literal (Sloc (Enumeration_Rep_Expr (Ent)),
4850 Intval => Enumeration_Rep (Ent))),
4852 Statements => New_List (
4853 Make_Simple_Return_Statement (Loc,
4854 Expression =>
4855 Make_Integer_Literal (Loc,
4856 Intval => Enumeration_Pos (Ent))))));
4858 Next_Literal (Ent);
4859 end loop;
4860 end if;
4862 -- In normal mode, add the others clause with the test.
4863 -- If Predicates_Ignored is True, validity checks do not apply to
4864 -- the subtype.
4866 if not No_Exception_Handlers_Set
4867 and then not Predicates_Ignored (Typ)
4868 then
4869 Append_To (Lst,
4870 Make_Case_Statement_Alternative (Loc,
4871 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
4872 Statements => New_List (
4873 Make_Raise_Constraint_Error (Loc,
4874 Condition => Make_Identifier (Loc, Name_uF),
4875 Reason => CE_Invalid_Data),
4876 Make_Simple_Return_Statement (Loc,
4877 Expression => Make_Integer_Literal (Loc, -1)))));
4879 -- If either of the restrictions No_Exceptions_Handlers/Propagation is
4880 -- active then return -1 (we cannot usefully raise Constraint_Error in
4881 -- this case). See description above for further details.
4883 else
4884 Append_To (Lst,
4885 Make_Case_Statement_Alternative (Loc,
4886 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
4887 Statements => New_List (
4888 Make_Simple_Return_Statement (Loc,
4889 Expression => Make_Integer_Literal (Loc, -1)))));
4890 end if;
4892 -- Now we can build the function body
4894 Fent :=
4895 Make_Defining_Identifier (Loc, Make_TSS_Name (Typ, TSS_Rep_To_Pos));
4897 Func :=
4898 Make_Subprogram_Body (Loc,
4899 Specification =>
4900 Make_Function_Specification (Loc,
4901 Defining_Unit_Name => Fent,
4902 Parameter_Specifications => New_List (
4903 Make_Parameter_Specification (Loc,
4904 Defining_Identifier =>
4905 Make_Defining_Identifier (Loc, Name_uA),
4906 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
4907 Make_Parameter_Specification (Loc,
4908 Defining_Identifier =>
4909 Make_Defining_Identifier (Loc, Name_uF),
4910 Parameter_Type =>
4911 New_Occurrence_Of (Standard_Boolean, Loc))),
4913 Result_Definition => New_Occurrence_Of (Standard_Integer, Loc)),
4915 Declarations => Empty_List,
4917 Handled_Statement_Sequence =>
4918 Make_Handled_Sequence_Of_Statements (Loc,
4919 Statements => New_List (
4920 Make_Case_Statement (Loc,
4921 Expression =>
4922 Unchecked_Convert_To
4923 (Ityp, Make_Identifier (Loc, Name_uA)),
4924 Alternatives => Lst))));
4926 Set_TSS (Typ, Fent);
4928 -- Set Pure flag (it will be reset if the current context is not Pure).
4929 -- We also pretend there was a pragma Pure_Function so that for purposes
4930 -- of optimization and constant-folding, we will consider the function
4931 -- Pure even if we are not in a Pure context).
4933 Set_Is_Pure (Fent);
4934 Set_Has_Pragma_Pure_Function (Fent);
4936 -- Unless we are in -gnatD mode, where we are debugging generated code,
4937 -- this is an internal entity for which we don't need debug info.
4939 if not Debug_Generated_Code then
4940 Set_Debug_Info_Off (Fent);
4941 end if;
4943 Set_Is_Inlined (Fent);
4945 exception
4946 when RE_Not_Available =>
4947 return;
4948 end Expand_Freeze_Enumeration_Type;
4950 -------------------------------
4951 -- Expand_Freeze_Record_Type --
4952 -------------------------------
4954 procedure Expand_Freeze_Record_Type (N : Node_Id) is
4955 procedure Build_Variant_Record_Equality (Typ : Entity_Id);
4956 -- Create An Equality function for the untagged variant record Typ and
4957 -- attach it to the TSS list.
4959 -----------------------------------
4960 -- Build_Variant_Record_Equality --
4961 -----------------------------------
4963 procedure Build_Variant_Record_Equality (Typ : Entity_Id) is
4964 Loc : constant Source_Ptr := Sloc (Typ);
4965 F : constant Entity_Id :=
4966 Make_Defining_Identifier (Loc,
4967 Chars => Make_TSS_Name (Typ, TSS_Composite_Equality));
4968 begin
4969 -- For a variant record with restriction No_Implicit_Conditionals
4970 -- in effect we skip building the procedure. This is safe because
4971 -- if we can see the restriction, so can any caller, and calls to
4972 -- equality test routines are not allowed for variant records if
4973 -- this restriction is active.
4975 if Restriction_Active (No_Implicit_Conditionals) then
4976 return;
4977 end if;
4979 -- Derived Unchecked_Union types no longer inherit the equality
4980 -- function of their parent.
4982 if Is_Derived_Type (Typ)
4983 and then not Is_Unchecked_Union (Typ)
4984 and then not Has_New_Non_Standard_Rep (Typ)
4985 then
4986 declare
4987 Parent_Eq : constant Entity_Id :=
4988 TSS (Root_Type (Typ), TSS_Composite_Equality);
4989 begin
4990 if Present (Parent_Eq) then
4991 Copy_TSS (Parent_Eq, Typ);
4992 return;
4993 end if;
4994 end;
4995 end if;
4997 Discard_Node (
4998 Build_Variant_Record_Equality
4999 (Typ => Typ,
5000 Body_Id => F,
5001 Param_Specs => New_List (
5002 Make_Parameter_Specification (Loc,
5003 Defining_Identifier =>
5004 Make_Defining_Identifier (Loc, Name_X),
5005 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
5007 Make_Parameter_Specification (Loc,
5008 Defining_Identifier =>
5009 Make_Defining_Identifier (Loc, Name_Y),
5010 Parameter_Type => New_Occurrence_Of (Typ, Loc)))));
5012 Set_TSS (Typ, F);
5013 Set_Is_Pure (F);
5015 if not Debug_Generated_Code then
5016 Set_Debug_Info_Off (F);
5017 end if;
5018 end Build_Variant_Record_Equality;
5020 -- Local variables
5022 Typ : constant Node_Id := Entity (N);
5023 Typ_Decl : constant Node_Id := Parent (Typ);
5025 Comp : Entity_Id;
5026 Comp_Typ : Entity_Id;
5027 Predef_List : List_Id;
5029 Wrapper_Decl_List : List_Id := No_List;
5030 Wrapper_Body_List : List_Id := No_List;
5032 Renamed_Eq : Node_Id := Empty;
5033 -- Defining unit name for the predefined equality function in the case
5034 -- where the type has a primitive operation that is a renaming of
5035 -- predefined equality (but only if there is also an overriding
5036 -- user-defined equality function). Used to pass this entity from
5037 -- Make_Predefined_Primitive_Specs to Predefined_Primitive_Bodies.
5039 -- Start of processing for Expand_Freeze_Record_Type
5041 begin
5042 -- Build discriminant checking functions if not a derived type (for
5043 -- derived types that are not tagged types, always use the discriminant
5044 -- checking functions of the parent type). However, for untagged types
5045 -- the derivation may have taken place before the parent was frozen, so
5046 -- we copy explicitly the discriminant checking functions from the
5047 -- parent into the components of the derived type.
5049 if not Is_Derived_Type (Typ)
5050 or else Has_New_Non_Standard_Rep (Typ)
5051 or else Is_Tagged_Type (Typ)
5052 then
5053 Build_Discr_Checking_Funcs (Typ_Decl);
5055 elsif Is_Derived_Type (Typ)
5056 and then not Is_Tagged_Type (Typ)
5058 -- If we have a derived Unchecked_Union, we do not inherit the
5059 -- discriminant checking functions from the parent type since the
5060 -- discriminants are non existent.
5062 and then not Is_Unchecked_Union (Typ)
5063 and then Has_Discriminants (Typ)
5064 then
5065 declare
5066 Old_Comp : Entity_Id;
5068 begin
5069 Old_Comp :=
5070 First_Component (Base_Type (Underlying_Type (Etype (Typ))));
5071 Comp := First_Component (Typ);
5072 while Present (Comp) loop
5073 if Ekind (Comp) = E_Component
5074 and then Chars (Comp) = Chars (Old_Comp)
5075 then
5076 Set_Discriminant_Checking_Func
5077 (Comp, Discriminant_Checking_Func (Old_Comp));
5078 end if;
5080 Next_Component (Old_Comp);
5081 Next_Component (Comp);
5082 end loop;
5083 end;
5084 end if;
5086 if Is_Derived_Type (Typ)
5087 and then Is_Limited_Type (Typ)
5088 and then Is_Tagged_Type (Typ)
5089 then
5090 Check_Stream_Attributes (Typ);
5091 end if;
5093 -- Update task, protected, and controlled component flags, because some
5094 -- of the component types may have been private at the point of the
5095 -- record declaration. Detect anonymous access-to-controlled components.
5097 Comp := First_Component (Typ);
5098 while Present (Comp) loop
5099 Comp_Typ := Etype (Comp);
5101 Propagate_Concurrent_Flags (Typ, Comp_Typ);
5103 -- Do not set Has_Controlled_Component on a class-wide equivalent
5104 -- type. See Make_CW_Equivalent_Type.
5106 if not Is_Class_Wide_Equivalent_Type (Typ)
5107 and then
5108 (Has_Controlled_Component (Comp_Typ)
5109 or else (Chars (Comp) /= Name_uParent
5110 and then Is_Controlled (Comp_Typ)))
5111 then
5112 Set_Has_Controlled_Component (Typ);
5113 end if;
5115 Next_Component (Comp);
5116 end loop;
5118 -- Handle constructors of untagged CPP_Class types
5120 if not Is_Tagged_Type (Typ) and then Is_CPP_Class (Typ) then
5121 Set_CPP_Constructors (Typ);
5122 end if;
5124 -- Creation of the Dispatch Table. Note that a Dispatch Table is built
5125 -- for regular tagged types as well as for Ada types deriving from a C++
5126 -- Class, but not for tagged types directly corresponding to C++ classes
5127 -- In the later case we assume that it is created in the C++ side and we
5128 -- just use it.
5130 if Is_Tagged_Type (Typ) then
5132 -- Add the _Tag component
5134 if Underlying_Type (Etype (Typ)) = Typ then
5135 Expand_Tagged_Root (Typ);
5136 end if;
5138 if Is_CPP_Class (Typ) then
5139 Set_All_DT_Position (Typ);
5141 -- Create the tag entities with a minimum decoration
5143 if Tagged_Type_Expansion then
5144 Append_Freeze_Actions (Typ, Make_Tags (Typ));
5145 end if;
5147 Set_CPP_Constructors (Typ);
5149 else
5150 if not Building_Static_DT (Typ) then
5152 -- Usually inherited primitives are not delayed but the first
5153 -- Ada extension of a CPP_Class is an exception since the
5154 -- address of the inherited subprogram has to be inserted in
5155 -- the new Ada Dispatch Table and this is a freezing action.
5157 -- Similarly, if this is an inherited operation whose parent is
5158 -- not frozen yet, it is not in the DT of the parent, and we
5159 -- generate an explicit freeze node for the inherited operation
5160 -- so it is properly inserted in the DT of the current type.
5162 declare
5163 Elmt : Elmt_Id;
5164 Subp : Entity_Id;
5166 begin
5167 Elmt := First_Elmt (Primitive_Operations (Typ));
5168 while Present (Elmt) loop
5169 Subp := Node (Elmt);
5171 if Present (Alias (Subp)) then
5172 if Is_CPP_Class (Etype (Typ)) then
5173 Set_Has_Delayed_Freeze (Subp);
5175 elsif Has_Delayed_Freeze (Alias (Subp))
5176 and then not Is_Frozen (Alias (Subp))
5177 then
5178 Set_Is_Frozen (Subp, False);
5179 Set_Has_Delayed_Freeze (Subp);
5180 end if;
5181 end if;
5183 Next_Elmt (Elmt);
5184 end loop;
5185 end;
5186 end if;
5188 -- Unfreeze momentarily the type to add the predefined primitives
5189 -- operations. The reason we unfreeze is so that these predefined
5190 -- operations will indeed end up as primitive operations (which
5191 -- must be before the freeze point).
5193 Set_Is_Frozen (Typ, False);
5195 -- Do not add the spec of predefined primitives in case of
5196 -- CPP tagged type derivations that have convention CPP.
5198 if Is_CPP_Class (Root_Type (Typ))
5199 and then Convention (Typ) = Convention_CPP
5200 then
5201 null;
5203 -- Do not add the spec of the predefined primitives if we are
5204 -- compiling under restriction No_Dispatching_Calls.
5206 elsif not Restriction_Active (No_Dispatching_Calls) then
5207 Make_Predefined_Primitive_Specs (Typ, Predef_List, Renamed_Eq);
5208 Insert_List_Before_And_Analyze (N, Predef_List);
5209 end if;
5211 -- Ada 2005 (AI-391): For a nonabstract null extension, create
5212 -- wrapper functions for each nonoverridden inherited function
5213 -- with a controlling result of the type. The wrapper for such
5214 -- a function returns an extension aggregate that invokes the
5215 -- parent function.
5217 if Ada_Version >= Ada_2005
5218 and then not Is_Abstract_Type (Typ)
5219 and then Is_Null_Extension (Typ)
5220 then
5221 Make_Controlling_Function_Wrappers
5222 (Typ, Wrapper_Decl_List, Wrapper_Body_List);
5223 Insert_List_Before_And_Analyze (N, Wrapper_Decl_List);
5224 end if;
5226 -- Ada 2005 (AI-251): For a nonabstract type extension, build
5227 -- null procedure declarations for each set of homographic null
5228 -- procedures that are inherited from interface types but not
5229 -- overridden. This is done to ensure that the dispatch table
5230 -- entry associated with such null primitives are properly filled.
5232 if Ada_Version >= Ada_2005
5233 and then Etype (Typ) /= Typ
5234 and then not Is_Abstract_Type (Typ)
5235 and then Has_Interfaces (Typ)
5236 then
5237 Insert_Actions (N, Make_Null_Procedure_Specs (Typ));
5238 end if;
5240 Set_Is_Frozen (Typ);
5242 if not Is_Derived_Type (Typ)
5243 or else Is_Tagged_Type (Etype (Typ))
5244 then
5245 Set_All_DT_Position (Typ);
5247 -- If this is a type derived from an untagged private type whose
5248 -- full view is tagged, the type is marked tagged for layout
5249 -- reasons, but it has no dispatch table.
5251 elsif Is_Derived_Type (Typ)
5252 and then Is_Private_Type (Etype (Typ))
5253 and then not Is_Tagged_Type (Etype (Typ))
5254 then
5255 return;
5256 end if;
5258 -- Create and decorate the tags. Suppress their creation when
5259 -- not Tagged_Type_Expansion because the dispatching mechanism is
5260 -- handled internally by the virtual target.
5262 if Tagged_Type_Expansion then
5263 Append_Freeze_Actions (Typ, Make_Tags (Typ));
5265 -- Generate dispatch table of locally defined tagged type.
5266 -- Dispatch tables of library level tagged types are built
5267 -- later (see Analyze_Declarations).
5269 if not Building_Static_DT (Typ) then
5270 Append_Freeze_Actions (Typ, Make_DT (Typ));
5271 end if;
5272 end if;
5274 -- If the type has unknown discriminants, propagate dispatching
5275 -- information to its underlying record view, which does not get
5276 -- its own dispatch table.
5278 if Is_Derived_Type (Typ)
5279 and then Has_Unknown_Discriminants (Typ)
5280 and then Present (Underlying_Record_View (Typ))
5281 then
5282 declare
5283 Rep : constant Entity_Id := Underlying_Record_View (Typ);
5284 begin
5285 Set_Access_Disp_Table
5286 (Rep, Access_Disp_Table (Typ));
5287 Set_Dispatch_Table_Wrappers
5288 (Rep, Dispatch_Table_Wrappers (Typ));
5289 Set_Direct_Primitive_Operations
5290 (Rep, Direct_Primitive_Operations (Typ));
5291 end;
5292 end if;
5294 -- Make sure that the primitives Initialize, Adjust and Finalize
5295 -- are Frozen before other TSS subprograms. We don't want them
5296 -- Frozen inside.
5298 if Is_Controlled (Typ) then
5299 if not Is_Limited_Type (Typ) then
5300 Append_Freeze_Actions (Typ,
5301 Freeze_Entity (Find_Prim_Op (Typ, Name_Adjust), Typ));
5302 end if;
5304 Append_Freeze_Actions (Typ,
5305 Freeze_Entity (Find_Prim_Op (Typ, Name_Initialize), Typ));
5307 Append_Freeze_Actions (Typ,
5308 Freeze_Entity (Find_Prim_Op (Typ, Name_Finalize), Typ));
5309 end if;
5311 -- Freeze rest of primitive operations. There is no need to handle
5312 -- the predefined primitives if we are compiling under restriction
5313 -- No_Dispatching_Calls.
5315 if not Restriction_Active (No_Dispatching_Calls) then
5316 Append_Freeze_Actions (Typ, Predefined_Primitive_Freeze (Typ));
5317 end if;
5318 end if;
5320 -- In the untagged case, ever since Ada 83 an equality function must
5321 -- be provided for variant records that are not unchecked unions.
5322 -- In Ada 2012 the equality function composes, and thus must be built
5323 -- explicitly just as for tagged records.
5325 elsif Has_Discriminants (Typ)
5326 and then not Is_Limited_Type (Typ)
5327 then
5328 declare
5329 Comps : constant Node_Id :=
5330 Component_List (Type_Definition (Typ_Decl));
5331 begin
5332 if Present (Comps)
5333 and then Present (Variant_Part (Comps))
5334 then
5335 Build_Variant_Record_Equality (Typ);
5336 end if;
5337 end;
5339 -- Otherwise create primitive equality operation (AI05-0123)
5341 -- This is done unconditionally to ensure that tools can be linked
5342 -- properly with user programs compiled with older language versions.
5343 -- In addition, this is needed because "=" composes for bounded strings
5344 -- in all language versions (see Exp_Ch4.Expand_Composite_Equality).
5346 elsif Comes_From_Source (Typ)
5347 and then Convention (Typ) = Convention_Ada
5348 and then not Is_Limited_Type (Typ)
5349 then
5350 Build_Untagged_Equality (Typ);
5351 end if;
5353 -- Before building the record initialization procedure, if we are
5354 -- dealing with a concurrent record value type, then we must go through
5355 -- the discriminants, exchanging discriminals between the concurrent
5356 -- type and the concurrent record value type. See the section "Handling
5357 -- of Discriminants" in the Einfo spec for details.
5359 if Is_Concurrent_Record_Type (Typ)
5360 and then Has_Discriminants (Typ)
5361 then
5362 declare
5363 Ctyp : constant Entity_Id :=
5364 Corresponding_Concurrent_Type (Typ);
5365 Conc_Discr : Entity_Id;
5366 Rec_Discr : Entity_Id;
5367 Temp : Entity_Id;
5369 begin
5370 Conc_Discr := First_Discriminant (Ctyp);
5371 Rec_Discr := First_Discriminant (Typ);
5372 while Present (Conc_Discr) loop
5373 Temp := Discriminal (Conc_Discr);
5374 Set_Discriminal (Conc_Discr, Discriminal (Rec_Discr));
5375 Set_Discriminal (Rec_Discr, Temp);
5377 Set_Discriminal_Link (Discriminal (Conc_Discr), Conc_Discr);
5378 Set_Discriminal_Link (Discriminal (Rec_Discr), Rec_Discr);
5380 Next_Discriminant (Conc_Discr);
5381 Next_Discriminant (Rec_Discr);
5382 end loop;
5383 end;
5384 end if;
5386 if Has_Controlled_Component (Typ) then
5387 Build_Controlling_Procs (Typ);
5388 end if;
5390 Adjust_Discriminants (Typ);
5392 -- Do not need init for interfaces on virtual targets since they're
5393 -- abstract.
5395 if Tagged_Type_Expansion or else not Is_Interface (Typ) then
5396 Build_Record_Init_Proc (Typ_Decl, Typ);
5397 end if;
5399 -- For tagged type that are not interfaces, build bodies of primitive
5400 -- operations. Note: do this after building the record initialization
5401 -- procedure, since the primitive operations may need the initialization
5402 -- routine. There is no need to add predefined primitives of interfaces
5403 -- because all their predefined primitives are abstract.
5405 if Is_Tagged_Type (Typ) and then not Is_Interface (Typ) then
5407 -- Do not add the body of predefined primitives in case of CPP tagged
5408 -- type derivations that have convention CPP.
5410 if Is_CPP_Class (Root_Type (Typ))
5411 and then Convention (Typ) = Convention_CPP
5412 then
5413 null;
5415 -- Do not add the body of the predefined primitives if we are
5416 -- compiling under restriction No_Dispatching_Calls or if we are
5417 -- compiling a CPP tagged type.
5419 elsif not Restriction_Active (No_Dispatching_Calls) then
5421 -- Create the body of TSS primitive Finalize_Address. This must
5422 -- be done before the bodies of all predefined primitives are
5423 -- created. If Typ is limited, Stream_Input and Stream_Read may
5424 -- produce build-in-place allocations and for those the expander
5425 -- needs Finalize_Address.
5427 Make_Finalize_Address_Body (Typ);
5428 Predef_List := Predefined_Primitive_Bodies (Typ, Renamed_Eq);
5429 Append_Freeze_Actions (Typ, Predef_List);
5430 end if;
5432 -- Ada 2005 (AI-391): If any wrappers were created for nonoverridden
5433 -- inherited functions, then add their bodies to the freeze actions.
5435 if Present (Wrapper_Body_List) then
5436 Append_Freeze_Actions (Typ, Wrapper_Body_List);
5437 end if;
5439 -- Create extra formals for the primitive operations of the type.
5440 -- This must be done before analyzing the body of the initialization
5441 -- procedure, because a self-referential type might call one of these
5442 -- primitives in the body of the init_proc itself.
5444 declare
5445 Elmt : Elmt_Id;
5446 Subp : Entity_Id;
5448 begin
5449 Elmt := First_Elmt (Primitive_Operations (Typ));
5450 while Present (Elmt) loop
5451 Subp := Node (Elmt);
5452 if not Has_Foreign_Convention (Subp)
5453 and then not Is_Predefined_Dispatching_Operation (Subp)
5454 then
5455 Create_Extra_Formals (Subp);
5456 end if;
5458 Next_Elmt (Elmt);
5459 end loop;
5460 end;
5461 end if;
5462 end Expand_Freeze_Record_Type;
5464 ------------------------------------
5465 -- Expand_N_Full_Type_Declaration --
5466 ------------------------------------
5468 procedure Expand_N_Full_Type_Declaration (N : Node_Id) is
5469 procedure Build_Master (Ptr_Typ : Entity_Id);
5470 -- Create the master associated with Ptr_Typ
5472 ------------------
5473 -- Build_Master --
5474 ------------------
5476 procedure Build_Master (Ptr_Typ : Entity_Id) is
5477 Desig_Typ : Entity_Id := Designated_Type (Ptr_Typ);
5479 begin
5480 -- If the designated type is an incomplete view coming from a
5481 -- limited-with'ed package, we need to use the nonlimited view in
5482 -- case it has tasks.
5484 if Ekind (Desig_Typ) in Incomplete_Kind
5485 and then Present (Non_Limited_View (Desig_Typ))
5486 then
5487 Desig_Typ := Non_Limited_View (Desig_Typ);
5488 end if;
5490 -- Anonymous access types are created for the components of the
5491 -- record parameter for an entry declaration. No master is created
5492 -- for such a type.
5494 if Comes_From_Source (N) and then Has_Task (Desig_Typ) then
5495 Build_Master_Entity (Ptr_Typ);
5496 Build_Master_Renaming (Ptr_Typ);
5498 -- Create a class-wide master because a Master_Id must be generated
5499 -- for access-to-limited-class-wide types whose root may be extended
5500 -- with task components.
5502 -- Note: This code covers access-to-limited-interfaces because they
5503 -- can be used to reference tasks implementing them.
5505 elsif Is_Limited_Class_Wide_Type (Desig_Typ)
5506 and then Tasking_Allowed
5507 then
5508 Build_Class_Wide_Master (Ptr_Typ);
5509 end if;
5510 end Build_Master;
5512 -- Local declarations
5514 Def_Id : constant Entity_Id := Defining_Identifier (N);
5515 B_Id : constant Entity_Id := Base_Type (Def_Id);
5516 FN : Node_Id;
5517 Par_Id : Entity_Id;
5519 -- Start of processing for Expand_N_Full_Type_Declaration
5521 begin
5522 if Is_Access_Type (Def_Id) then
5523 Build_Master (Def_Id);
5525 if Ekind (Def_Id) = E_Access_Protected_Subprogram_Type then
5526 Expand_Access_Protected_Subprogram_Type (N);
5527 end if;
5529 -- Array of anonymous access-to-task pointers
5531 elsif Ada_Version >= Ada_2005
5532 and then Is_Array_Type (Def_Id)
5533 and then Is_Access_Type (Component_Type (Def_Id))
5534 and then Ekind (Component_Type (Def_Id)) = E_Anonymous_Access_Type
5535 then
5536 Build_Master (Component_Type (Def_Id));
5538 elsif Has_Task (Def_Id) then
5539 Expand_Previous_Access_Type (Def_Id);
5541 -- Check the components of a record type or array of records for
5542 -- anonymous access-to-task pointers.
5544 elsif Ada_Version >= Ada_2005
5545 and then (Is_Record_Type (Def_Id)
5546 or else
5547 (Is_Array_Type (Def_Id)
5548 and then Is_Record_Type (Component_Type (Def_Id))))
5549 then
5550 declare
5551 Comp : Entity_Id;
5552 First : Boolean;
5553 M_Id : Entity_Id;
5554 Typ : Entity_Id;
5556 begin
5557 if Is_Array_Type (Def_Id) then
5558 Comp := First_Entity (Component_Type (Def_Id));
5559 else
5560 Comp := First_Entity (Def_Id);
5561 end if;
5563 -- Examine all components looking for anonymous access-to-task
5564 -- types.
5566 First := True;
5567 while Present (Comp) loop
5568 Typ := Etype (Comp);
5570 if Ekind (Typ) = E_Anonymous_Access_Type
5571 and then Has_Task (Available_View (Designated_Type (Typ)))
5572 and then No (Master_Id (Typ))
5573 then
5574 -- Ensure that the record or array type have a _master
5576 if First then
5577 Build_Master_Entity (Def_Id);
5578 Build_Master_Renaming (Typ);
5579 M_Id := Master_Id (Typ);
5581 First := False;
5583 -- Reuse the same master to service any additional types
5585 else
5586 Set_Master_Id (Typ, M_Id);
5587 end if;
5588 end if;
5590 Next_Entity (Comp);
5591 end loop;
5592 end;
5593 end if;
5595 Par_Id := Etype (B_Id);
5597 -- The parent type is private then we need to inherit any TSS operations
5598 -- from the full view.
5600 if Ekind (Par_Id) in Private_Kind
5601 and then Present (Full_View (Par_Id))
5602 then
5603 Par_Id := Base_Type (Full_View (Par_Id));
5604 end if;
5606 if Nkind (Type_Definition (Original_Node (N))) =
5607 N_Derived_Type_Definition
5608 and then not Is_Tagged_Type (Def_Id)
5609 and then Present (Freeze_Node (Par_Id))
5610 and then Present (TSS_Elist (Freeze_Node (Par_Id)))
5611 then
5612 Ensure_Freeze_Node (B_Id);
5613 FN := Freeze_Node (B_Id);
5615 if No (TSS_Elist (FN)) then
5616 Set_TSS_Elist (FN, New_Elmt_List);
5617 end if;
5619 declare
5620 T_E : constant Elist_Id := TSS_Elist (FN);
5621 Elmt : Elmt_Id;
5623 begin
5624 Elmt := First_Elmt (TSS_Elist (Freeze_Node (Par_Id)));
5625 while Present (Elmt) loop
5626 if Chars (Node (Elmt)) /= Name_uInit then
5627 Append_Elmt (Node (Elmt), T_E);
5628 end if;
5630 Next_Elmt (Elmt);
5631 end loop;
5633 -- If the derived type itself is private with a full view, then
5634 -- associate the full view with the inherited TSS_Elist as well.
5636 if Ekind (B_Id) in Private_Kind
5637 and then Present (Full_View (B_Id))
5638 then
5639 Ensure_Freeze_Node (Base_Type (Full_View (B_Id)));
5640 Set_TSS_Elist
5641 (Freeze_Node (Base_Type (Full_View (B_Id))), TSS_Elist (FN));
5642 end if;
5643 end;
5644 end if;
5645 end Expand_N_Full_Type_Declaration;
5647 ---------------------------------
5648 -- Expand_N_Object_Declaration --
5649 ---------------------------------
5651 procedure Expand_N_Object_Declaration (N : Node_Id) is
5652 Loc : constant Source_Ptr := Sloc (N);
5653 Def_Id : constant Entity_Id := Defining_Identifier (N);
5654 Expr : constant Node_Id := Expression (N);
5655 Obj_Def : constant Node_Id := Object_Definition (N);
5656 Typ : constant Entity_Id := Etype (Def_Id);
5657 Base_Typ : constant Entity_Id := Base_Type (Typ);
5658 Expr_Q : Node_Id;
5660 function Build_Equivalent_Aggregate return Boolean;
5661 -- If the object has a constrained discriminated type and no initial
5662 -- value, it may be possible to build an equivalent aggregate instead,
5663 -- and prevent an actual call to the initialization procedure.
5665 procedure Count_Default_Sized_Task_Stacks
5666 (Typ : Entity_Id;
5667 Pri_Stacks : out Int;
5668 Sec_Stacks : out Int);
5669 -- Count the number of default-sized primary and secondary task stacks
5670 -- required for task objects contained within type Typ. If the number of
5671 -- task objects contained within the type is not known at compile time
5672 -- the procedure will return the stack counts of zero.
5674 procedure Default_Initialize_Object (After : Node_Id);
5675 -- Generate all default initialization actions for object Def_Id. Any
5676 -- new code is inserted after node After.
5678 function Rewrite_As_Renaming return Boolean;
5679 -- Indicate whether to rewrite a declaration with initialization into an
5680 -- object renaming declaration (see below).
5682 --------------------------------
5683 -- Build_Equivalent_Aggregate --
5684 --------------------------------
5686 function Build_Equivalent_Aggregate return Boolean is
5687 Aggr : Node_Id;
5688 Comp : Entity_Id;
5689 Discr : Elmt_Id;
5690 Full_Type : Entity_Id;
5692 begin
5693 Full_Type := Typ;
5695 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
5696 Full_Type := Full_View (Typ);
5697 end if;
5699 -- Only perform this transformation if Elaboration_Code is forbidden
5700 -- or undesirable, and if this is a global entity of a constrained
5701 -- record type.
5703 -- If Initialize_Scalars might be active this transformation cannot
5704 -- be performed either, because it will lead to different semantics
5705 -- or because elaboration code will in fact be created.
5707 if Ekind (Full_Type) /= E_Record_Subtype
5708 or else not Has_Discriminants (Full_Type)
5709 or else not Is_Constrained (Full_Type)
5710 or else Is_Controlled (Full_Type)
5711 or else Is_Limited_Type (Full_Type)
5712 or else not Restriction_Active (No_Initialize_Scalars)
5713 then
5714 return False;
5715 end if;
5717 if Ekind (Current_Scope) = E_Package
5718 and then
5719 (Restriction_Active (No_Elaboration_Code)
5720 or else Is_Preelaborated (Current_Scope))
5721 then
5722 -- Building a static aggregate is possible if the discriminants
5723 -- have static values and the other components have static
5724 -- defaults or none.
5726 Discr := First_Elmt (Discriminant_Constraint (Full_Type));
5727 while Present (Discr) loop
5728 if not Is_OK_Static_Expression (Node (Discr)) then
5729 return False;
5730 end if;
5732 Next_Elmt (Discr);
5733 end loop;
5735 -- Check that initialized components are OK, and that non-
5736 -- initialized components do not require a call to their own
5737 -- initialization procedure.
5739 Comp := First_Component (Full_Type);
5740 while Present (Comp) loop
5741 if Ekind (Comp) = E_Component
5742 and then Present (Expression (Parent (Comp)))
5743 and then
5744 not Is_OK_Static_Expression (Expression (Parent (Comp)))
5745 then
5746 return False;
5748 elsif Has_Non_Null_Base_Init_Proc (Etype (Comp)) then
5749 return False;
5751 end if;
5753 Next_Component (Comp);
5754 end loop;
5756 -- Everything is static, assemble the aggregate, discriminant
5757 -- values first.
5759 Aggr :=
5760 Make_Aggregate (Loc,
5761 Expressions => New_List,
5762 Component_Associations => New_List);
5764 Discr := First_Elmt (Discriminant_Constraint (Full_Type));
5765 while Present (Discr) loop
5766 Append_To (Expressions (Aggr), New_Copy (Node (Discr)));
5767 Next_Elmt (Discr);
5768 end loop;
5770 -- Now collect values of initialized components
5772 Comp := First_Component (Full_Type);
5773 while Present (Comp) loop
5774 if Ekind (Comp) = E_Component
5775 and then Present (Expression (Parent (Comp)))
5776 then
5777 Append_To (Component_Associations (Aggr),
5778 Make_Component_Association (Loc,
5779 Choices => New_List (New_Occurrence_Of (Comp, Loc)),
5780 Expression => New_Copy_Tree
5781 (Expression (Parent (Comp)))));
5782 end if;
5784 Next_Component (Comp);
5785 end loop;
5787 -- Finally, box-initialize remaining components
5789 Append_To (Component_Associations (Aggr),
5790 Make_Component_Association (Loc,
5791 Choices => New_List (Make_Others_Choice (Loc)),
5792 Expression => Empty));
5793 Set_Box_Present (Last (Component_Associations (Aggr)));
5794 Set_Expression (N, Aggr);
5796 if Typ /= Full_Type then
5797 Analyze_And_Resolve (Aggr, Full_View (Base_Type (Full_Type)));
5798 Rewrite (Aggr, Unchecked_Convert_To (Typ, Aggr));
5799 Analyze_And_Resolve (Aggr, Typ);
5800 else
5801 Analyze_And_Resolve (Aggr, Full_Type);
5802 end if;
5804 return True;
5806 else
5807 return False;
5808 end if;
5809 end Build_Equivalent_Aggregate;
5811 -------------------------------------
5812 -- Count_Default_Sized_Task_Stacks --
5813 -------------------------------------
5815 procedure Count_Default_Sized_Task_Stacks
5816 (Typ : Entity_Id;
5817 Pri_Stacks : out Int;
5818 Sec_Stacks : out Int)
5820 Component : Entity_Id;
5822 begin
5823 -- To calculate the number of default-sized task stacks required for
5824 -- an object of Typ, a depth-first recursive traversal of the AST
5825 -- from the Typ entity node is undertaken. Only type nodes containing
5826 -- task objects are visited.
5828 Pri_Stacks := 0;
5829 Sec_Stacks := 0;
5831 if not Has_Task (Typ) then
5832 return;
5833 end if;
5835 case Ekind (Typ) is
5836 when E_Task_Subtype
5837 | E_Task_Type
5839 -- A task type is found marking the bottom of the descent. If
5840 -- the type has no representation aspect for the corresponding
5841 -- stack then that stack is using the default size.
5843 if Present (Get_Rep_Item (Typ, Name_Storage_Size)) then
5844 Pri_Stacks := 0;
5845 else
5846 Pri_Stacks := 1;
5847 end if;
5849 if Present (Get_Rep_Item (Typ, Name_Secondary_Stack_Size)) then
5850 Sec_Stacks := 0;
5851 else
5852 Sec_Stacks := 1;
5853 end if;
5855 when E_Array_Subtype
5856 | E_Array_Type
5858 -- First find the number of default stacks contained within an
5859 -- array component.
5861 Count_Default_Sized_Task_Stacks
5862 (Component_Type (Typ),
5863 Pri_Stacks,
5864 Sec_Stacks);
5866 -- Then multiply the result by the size of the array
5868 declare
5869 Quantity : constant Int := Number_Of_Elements_In_Array (Typ);
5870 -- Number_Of_Elements_In_Array is non-trival, consequently
5871 -- its result is captured as an optimization.
5873 begin
5874 Pri_Stacks := Pri_Stacks * Quantity;
5875 Sec_Stacks := Sec_Stacks * Quantity;
5876 end;
5878 when E_Protected_Subtype
5879 | E_Protected_Type
5880 | E_Record_Subtype
5881 | E_Record_Type
5883 Component := First_Component_Or_Discriminant (Typ);
5885 -- Recursively descend each component of the composite type
5886 -- looking for tasks, but only if the component is marked as
5887 -- having a task.
5889 while Present (Component) loop
5890 if Has_Task (Etype (Component)) then
5891 declare
5892 P : Int;
5893 S : Int;
5895 begin
5896 Count_Default_Sized_Task_Stacks
5897 (Etype (Component), P, S);
5898 Pri_Stacks := Pri_Stacks + P;
5899 Sec_Stacks := Sec_Stacks + S;
5900 end;
5901 end if;
5903 Next_Component_Or_Discriminant (Component);
5904 end loop;
5906 when E_Limited_Private_Subtype
5907 | E_Limited_Private_Type
5908 | E_Record_Subtype_With_Private
5909 | E_Record_Type_With_Private
5911 -- Switch to the full view of the private type to continue
5912 -- search.
5914 Count_Default_Sized_Task_Stacks
5915 (Full_View (Typ), Pri_Stacks, Sec_Stacks);
5917 -- Other types should not contain tasks
5919 when others =>
5920 raise Program_Error;
5921 end case;
5922 end Count_Default_Sized_Task_Stacks;
5924 -------------------------------
5925 -- Default_Initialize_Object --
5926 -------------------------------
5928 procedure Default_Initialize_Object (After : Node_Id) is
5929 function New_Object_Reference return Node_Id;
5930 -- Return a new reference to Def_Id with attributes Assignment_OK and
5931 -- Must_Not_Freeze already set.
5933 function Simple_Initialization_OK
5934 (Init_Typ : Entity_Id) return Boolean;
5935 -- Determine whether object declaration N with entity Def_Id needs
5936 -- simple initialization, assuming that it is of type Init_Typ.
5938 --------------------------
5939 -- New_Object_Reference --
5940 --------------------------
5942 function New_Object_Reference return Node_Id is
5943 Obj_Ref : constant Node_Id := New_Occurrence_Of (Def_Id, Loc);
5945 begin
5946 -- The call to the type init proc or [Deep_]Finalize must not
5947 -- freeze the related object as the call is internally generated.
5948 -- This way legal rep clauses that apply to the object will not be
5949 -- flagged. Note that the initialization call may be removed if
5950 -- pragma Import is encountered or moved to the freeze actions of
5951 -- the object because of an address clause.
5953 Set_Assignment_OK (Obj_Ref);
5954 Set_Must_Not_Freeze (Obj_Ref);
5956 return Obj_Ref;
5957 end New_Object_Reference;
5959 ------------------------------
5960 -- Simple_Initialization_OK --
5961 ------------------------------
5963 function Simple_Initialization_OK
5964 (Init_Typ : Entity_Id) return Boolean
5966 begin
5967 -- Do not consider the object declaration if it comes with an
5968 -- initialization expression, or is internal in which case it
5969 -- will be assigned later.
5971 return
5972 not Is_Internal (Def_Id)
5973 and then not Has_Init_Expression (N)
5974 and then Needs_Simple_Initialization
5975 (Typ => Init_Typ,
5976 Consider_IS =>
5977 Initialize_Scalars
5978 and then No (Following_Address_Clause (N)));
5979 end Simple_Initialization_OK;
5981 -- Local variables
5983 Exceptions_OK : constant Boolean :=
5984 not Restriction_Active (No_Exception_Propagation);
5986 Aggr_Init : Node_Id;
5987 Comp_Init : List_Id := No_List;
5988 Fin_Block : Node_Id;
5989 Fin_Call : Node_Id;
5990 Init_Stmts : List_Id := No_List;
5991 Obj_Init : Node_Id := Empty;
5992 Obj_Ref : Node_Id;
5994 -- Start of processing for Default_Initialize_Object
5996 begin
5997 -- Default initialization is suppressed for objects that are already
5998 -- known to be imported (i.e. whose declaration specifies the Import
5999 -- aspect). Note that for objects with a pragma Import, we generate
6000 -- initialization here, and then remove it downstream when processing
6001 -- the pragma. It is also suppressed for variables for which a pragma
6002 -- Suppress_Initialization has been explicitly given
6004 if Is_Imported (Def_Id) or else Suppress_Initialization (Def_Id) then
6005 return;
6007 -- Nothing to do if the object being initialized is of a task type
6008 -- and restriction No_Tasking is in effect, because this is a direct
6009 -- violation of the restriction.
6011 elsif Is_Task_Type (Base_Typ)
6012 and then Restriction_Active (No_Tasking)
6013 then
6014 return;
6015 end if;
6017 -- The expansion performed by this routine is as follows:
6019 -- begin
6020 -- Abort_Defer;
6021 -- Type_Init_Proc (Obj);
6023 -- begin
6024 -- [Deep_]Initialize (Obj);
6026 -- exception
6027 -- when others =>
6028 -- [Deep_]Finalize (Obj, Self => False);
6029 -- raise;
6030 -- end;
6031 -- at end
6032 -- Abort_Undefer_Direct;
6033 -- end;
6035 -- Initialize the components of the object
6037 if Has_Non_Null_Base_Init_Proc (Typ)
6038 and then not No_Initialization (N)
6039 and then not Initialization_Suppressed (Typ)
6040 then
6041 -- Do not initialize the components if No_Default_Initialization
6042 -- applies as the actual restriction check will occur later when
6043 -- the object is frozen as it is not known yet whether the object
6044 -- is imported or not.
6046 if not Restriction_Active (No_Default_Initialization) then
6048 -- If the values of the components are compile-time known, use
6049 -- their prebuilt aggregate form directly.
6051 Aggr_Init := Static_Initialization (Base_Init_Proc (Typ));
6053 if Present (Aggr_Init) then
6054 Set_Expression (N,
6055 New_Copy_Tree (Aggr_Init, New_Scope => Current_Scope));
6057 -- If type has discriminants, try to build an equivalent
6058 -- aggregate using discriminant values from the declaration.
6059 -- This is a useful optimization, in particular if restriction
6060 -- No_Elaboration_Code is active.
6062 elsif Build_Equivalent_Aggregate then
6063 null;
6065 -- Optimize the default initialization of an array object when
6066 -- pragma Initialize_Scalars or Normalize_Scalars is in effect.
6067 -- Construct an in-place initialization aggregate which may be
6068 -- convert into a fast memset by the backend.
6070 elsif Init_Or_Norm_Scalars
6071 and then Is_Array_Type (Typ)
6073 -- The array must lack atomic components because they are
6074 -- treated as non-static, and as a result the backend will
6075 -- not initialize the memory in one go.
6077 and then not Has_Atomic_Components (Typ)
6079 -- The array must not be packed because the invalid values
6080 -- in System.Scalar_Values are multiples of Storage_Unit.
6082 and then not Is_Packed (Typ)
6084 -- The array must have static non-empty ranges, otherwise
6085 -- the backend cannot initialize the memory in one go.
6087 and then Has_Static_Non_Empty_Array_Bounds (Typ)
6089 -- The optimization is only relevant for arrays of scalar
6090 -- types.
6092 and then Is_Scalar_Type (Component_Type (Typ))
6094 -- Similar to regular array initialization using a type
6095 -- init proc, predicate checks are not performed because the
6096 -- initialization values are intentionally invalid, and may
6097 -- violate the predicate.
6099 and then not Has_Predicates (Component_Type (Typ))
6101 -- The component type must have a single initialization value
6103 and then Simple_Initialization_OK (Component_Type (Typ))
6104 then
6105 Set_No_Initialization (N, False);
6106 Set_Expression (N,
6107 Get_Simple_Init_Val
6108 (Typ => Typ,
6109 N => Obj_Def,
6110 Size => Esize (Def_Id)));
6112 Analyze_And_Resolve
6113 (Expression (N), Typ, Suppress => All_Checks);
6115 -- Otherwise invoke the type init proc, generate:
6116 -- Type_Init_Proc (Obj);
6118 else
6119 Obj_Ref := New_Object_Reference;
6121 if Comes_From_Source (Def_Id) then
6122 Initialization_Warning (Obj_Ref);
6123 end if;
6125 Comp_Init := Build_Initialization_Call (Loc, Obj_Ref, Typ);
6126 end if;
6127 end if;
6129 -- Provide a default value if the object needs simple initialization
6131 elsif Simple_Initialization_OK (Typ) then
6132 Set_No_Initialization (N, False);
6133 Set_Expression (N,
6134 Get_Simple_Init_Val
6135 (Typ => Typ,
6136 N => Obj_Def,
6137 Size => Esize (Def_Id)));
6139 Analyze_And_Resolve (Expression (N), Typ);
6140 end if;
6142 -- Initialize the object, generate:
6143 -- [Deep_]Initialize (Obj);
6145 if Needs_Finalization (Typ) and then not No_Initialization (N) then
6146 Obj_Init :=
6147 Make_Init_Call
6148 (Obj_Ref => New_Occurrence_Of (Def_Id, Loc),
6149 Typ => Typ);
6150 end if;
6152 -- Build a special finalization block when both the object and its
6153 -- controlled components are to be initialized. The block finalizes
6154 -- the components if the object initialization fails. Generate:
6156 -- begin
6157 -- <Obj_Init>
6159 -- exception
6160 -- when others =>
6161 -- <Fin_Call>
6162 -- raise;
6163 -- end;
6165 if Has_Controlled_Component (Typ)
6166 and then Present (Comp_Init)
6167 and then Present (Obj_Init)
6168 and then Exceptions_OK
6169 then
6170 Init_Stmts := Comp_Init;
6172 Fin_Call :=
6173 Make_Final_Call
6174 (Obj_Ref => New_Object_Reference,
6175 Typ => Typ,
6176 Skip_Self => True);
6178 if Present (Fin_Call) then
6180 -- Do not emit warnings related to the elaboration order when a
6181 -- controlled object is declared before the body of Finalize is
6182 -- seen.
6184 if Legacy_Elaboration_Checks then
6185 Set_No_Elaboration_Check (Fin_Call);
6186 end if;
6188 Fin_Block :=
6189 Make_Block_Statement (Loc,
6190 Declarations => No_List,
6192 Handled_Statement_Sequence =>
6193 Make_Handled_Sequence_Of_Statements (Loc,
6194 Statements => New_List (Obj_Init),
6196 Exception_Handlers => New_List (
6197 Make_Exception_Handler (Loc,
6198 Exception_Choices => New_List (
6199 Make_Others_Choice (Loc)),
6201 Statements => New_List (
6202 Fin_Call,
6203 Make_Raise_Statement (Loc))))));
6205 -- Signal the ABE mechanism that the block carries out
6206 -- initialization actions.
6208 Set_Is_Initialization_Block (Fin_Block);
6210 Append_To (Init_Stmts, Fin_Block);
6211 end if;
6213 -- Otherwise finalization is not required, the initialization calls
6214 -- are passed to the abort block building circuitry, generate:
6216 -- Type_Init_Proc (Obj);
6217 -- [Deep_]Initialize (Obj);
6219 else
6220 if Present (Comp_Init) then
6221 Init_Stmts := Comp_Init;
6222 end if;
6224 if Present (Obj_Init) then
6225 if No (Init_Stmts) then
6226 Init_Stmts := New_List;
6227 end if;
6229 Append_To (Init_Stmts, Obj_Init);
6230 end if;
6231 end if;
6233 -- Build an abort block to protect the initialization calls
6235 if Abort_Allowed
6236 and then Present (Comp_Init)
6237 and then Present (Obj_Init)
6238 then
6239 -- Generate:
6240 -- Abort_Defer;
6242 Prepend_To (Init_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
6244 -- When exceptions are propagated, abort deferral must take place
6245 -- in the presence of initialization or finalization exceptions.
6246 -- Generate:
6248 -- begin
6249 -- Abort_Defer;
6250 -- <Init_Stmts>
6251 -- at end
6252 -- Abort_Undefer_Direct;
6253 -- end;
6255 if Exceptions_OK then
6256 Init_Stmts := New_List (
6257 Build_Abort_Undefer_Block (Loc,
6258 Stmts => Init_Stmts,
6259 Context => N));
6261 -- Otherwise exceptions are not propagated. Generate:
6263 -- Abort_Defer;
6264 -- <Init_Stmts>
6265 -- Abort_Undefer;
6267 else
6268 Append_To (Init_Stmts,
6269 Build_Runtime_Call (Loc, RE_Abort_Undefer));
6270 end if;
6271 end if;
6273 -- Insert the whole initialization sequence into the tree. If the
6274 -- object has a delayed freeze, as will be the case when it has
6275 -- aspect specifications, the initialization sequence is part of
6276 -- the freeze actions.
6278 if Present (Init_Stmts) then
6279 if Has_Delayed_Freeze (Def_Id) then
6280 Append_Freeze_Actions (Def_Id, Init_Stmts);
6281 else
6282 Insert_Actions_After (After, Init_Stmts);
6283 end if;
6284 end if;
6285 end Default_Initialize_Object;
6287 -------------------------
6288 -- Rewrite_As_Renaming --
6289 -------------------------
6291 function Rewrite_As_Renaming return Boolean is
6292 begin
6293 -- If the object declaration appears in the form
6295 -- Obj : Ctrl_Typ := Func (...);
6297 -- where Ctrl_Typ is controlled but not immutably limited type, then
6298 -- the expansion of the function call should use a dereference of the
6299 -- result to reference the value on the secondary stack.
6301 -- Obj : Ctrl_Typ renames Func (...).all;
6303 -- As a result, the call avoids an extra copy. This an optimization,
6304 -- but it is required for passing ACATS tests in some cases where it
6305 -- would otherwise make two copies. The RM allows removing redunant
6306 -- Adjust/Finalize calls, but does not allow insertion of extra ones.
6308 -- This part is disabled for now, because it breaks GPS builds
6310 return (False -- ???
6311 and then Nkind (Expr_Q) = N_Explicit_Dereference
6312 and then not Comes_From_Source (Expr_Q)
6313 and then Nkind (Original_Node (Expr_Q)) = N_Function_Call
6314 and then Nkind (Object_Definition (N)) in N_Has_Entity
6315 and then (Needs_Finalization (Entity (Object_Definition (N)))))
6317 -- If the initializing expression is for a variable with attribute
6318 -- OK_To_Rename set, then transform:
6320 -- Obj : Typ := Expr;
6322 -- into
6324 -- Obj : Typ renames Expr;
6326 -- provided that Obj is not aliased. The aliased case has to be
6327 -- excluded in general because Expr will not be aliased in
6328 -- general.
6330 or else
6331 (not Aliased_Present (N)
6332 and then Is_Entity_Name (Expr_Q)
6333 and then Ekind (Entity (Expr_Q)) = E_Variable
6334 and then OK_To_Rename (Entity (Expr_Q))
6335 and then Is_Entity_Name (Obj_Def));
6336 end Rewrite_As_Renaming;
6338 -- Local variables
6340 Next_N : constant Node_Id := Next (N);
6342 Adj_Call : Node_Id;
6343 Id_Ref : Node_Id;
6344 Tag_Assign : Node_Id;
6346 Init_After : Node_Id := N;
6347 -- Node after which the initialization actions are to be inserted. This
6348 -- is normally N, except for the case of a shared passive variable, in
6349 -- which case the init proc call must be inserted only after the bodies
6350 -- of the shared variable procedures have been seen.
6352 -- Start of processing for Expand_N_Object_Declaration
6354 begin
6355 -- Don't do anything for deferred constants. All proper actions will be
6356 -- expanded during the full declaration.
6358 if No (Expr) and Constant_Present (N) then
6359 return;
6360 end if;
6362 -- The type of the object cannot be abstract. This is diagnosed at the
6363 -- point the object is frozen, which happens after the declaration is
6364 -- fully expanded, so simply return now.
6366 if Is_Abstract_Type (Typ) then
6367 return;
6368 end if;
6370 -- No action needed for the internal imported dummy object added by
6371 -- Make_DT to compute the offset of the components that reference
6372 -- secondary dispatch tables; required to avoid never-ending loop
6373 -- processing this internal object declaration.
6375 if Tagged_Type_Expansion
6376 and then Is_Internal (Def_Id)
6377 and then Is_Imported (Def_Id)
6378 and then Related_Type (Def_Id) = Implementation_Base_Type (Typ)
6379 then
6380 return;
6381 end if;
6383 -- First we do special processing for objects of a tagged type where
6384 -- this is the point at which the type is frozen. The creation of the
6385 -- dispatch table and the initialization procedure have to be deferred
6386 -- to this point, since we reference previously declared primitive
6387 -- subprograms.
6389 -- Force construction of dispatch tables of library level tagged types
6391 if Tagged_Type_Expansion
6392 and then Building_Static_Dispatch_Tables
6393 and then Is_Library_Level_Entity (Def_Id)
6394 and then Is_Library_Level_Tagged_Type (Base_Typ)
6395 and then Ekind_In (Base_Typ, E_Record_Type,
6396 E_Protected_Type,
6397 E_Task_Type)
6398 and then not Has_Dispatch_Table (Base_Typ)
6399 then
6400 declare
6401 New_Nodes : List_Id := No_List;
6403 begin
6404 if Is_Concurrent_Type (Base_Typ) then
6405 New_Nodes := Make_DT (Corresponding_Record_Type (Base_Typ), N);
6406 else
6407 New_Nodes := Make_DT (Base_Typ, N);
6408 end if;
6410 if not Is_Empty_List (New_Nodes) then
6411 Insert_List_Before (N, New_Nodes);
6412 end if;
6413 end;
6414 end if;
6416 -- Make shared memory routines for shared passive variable
6418 if Is_Shared_Passive (Def_Id) then
6419 Init_After := Make_Shared_Var_Procs (N);
6420 end if;
6422 -- If tasks being declared, make sure we have an activation chain
6423 -- defined for the tasks (has no effect if we already have one), and
6424 -- also that a Master variable is established and that the appropriate
6425 -- enclosing construct is established as a task master.
6427 if Has_Task (Typ) then
6428 Build_Activation_Chain_Entity (N);
6429 Build_Master_Entity (Def_Id);
6430 end if;
6432 -- If No_Implicit_Heap_Allocations or No_Implicit_Task_Allocations
6433 -- restrictions are active then default-sized secondary stacks are
6434 -- generated by the binder and allocated by SS_Init. To provide the
6435 -- binder the number of stacks to generate, the number of default-sized
6436 -- stacks required for task objects contained within the object
6437 -- declaration N is calculated here as it is at this point where
6438 -- unconstrained types become constrained. The result is stored in the
6439 -- enclosing unit's Unit_Record.
6441 -- Note if N is an array object declaration that has an initialization
6442 -- expression, a second object declaration for the initialization
6443 -- expression is created by the compiler. To prevent double counting
6444 -- of the stacks in this scenario, the stacks of the first array are
6445 -- not counted.
6447 if Has_Task (Typ)
6448 and then not Restriction_Active (No_Secondary_Stack)
6449 and then (Restriction_Active (No_Implicit_Heap_Allocations)
6450 or else Restriction_Active (No_Implicit_Task_Allocations))
6451 and then not (Ekind_In (Ekind (Typ), E_Array_Type, E_Array_Subtype)
6452 and then (Has_Init_Expression (N)))
6453 then
6454 declare
6455 PS_Count, SS_Count : Int := 0;
6456 begin
6457 Count_Default_Sized_Task_Stacks (Typ, PS_Count, SS_Count);
6458 Increment_Primary_Stack_Count (PS_Count);
6459 Increment_Sec_Stack_Count (SS_Count);
6460 end;
6461 end if;
6463 -- Default initialization required, and no expression present
6465 if No (Expr) then
6467 -- If we have a type with a variant part, the initialization proc
6468 -- will contain implicit tests of the discriminant values, which
6469 -- counts as a violation of the restriction No_Implicit_Conditionals.
6471 if Has_Variant_Part (Typ) then
6472 declare
6473 Msg : Boolean;
6475 begin
6476 Check_Restriction (Msg, No_Implicit_Conditionals, Obj_Def);
6478 if Msg then
6479 Error_Msg_N
6480 ("\initialization of variant record tests discriminants",
6481 Obj_Def);
6482 return;
6483 end if;
6484 end;
6485 end if;
6487 -- For the default initialization case, if we have a private type
6488 -- with invariants, and invariant checks are enabled, then insert an
6489 -- invariant check after the object declaration. Note that it is OK
6490 -- to clobber the object with an invalid value since if the exception
6491 -- is raised, then the object will go out of scope. In the case where
6492 -- an array object is initialized with an aggregate, the expression
6493 -- is removed. Check flag Has_Init_Expression to avoid generating a
6494 -- junk invariant check and flag No_Initialization to avoid checking
6495 -- an uninitialized object such as a compiler temporary used for an
6496 -- aggregate.
6498 if Has_Invariants (Base_Typ)
6499 and then Present (Invariant_Procedure (Base_Typ))
6500 and then not Has_Init_Expression (N)
6501 and then not No_Initialization (N)
6502 then
6503 -- If entity has an address clause or aspect, make invariant
6504 -- call into a freeze action for the explicit freeze node for
6505 -- object. Otherwise insert invariant check after declaration.
6507 if Present (Following_Address_Clause (N))
6508 or else Has_Aspect (Def_Id, Aspect_Address)
6509 then
6510 Ensure_Freeze_Node (Def_Id);
6511 Set_Has_Delayed_Freeze (Def_Id);
6512 Set_Is_Frozen (Def_Id, False);
6514 if not Partial_View_Has_Unknown_Discr (Typ) then
6515 Append_Freeze_Action (Def_Id,
6516 Make_Invariant_Call (New_Occurrence_Of (Def_Id, Loc)));
6517 end if;
6519 elsif not Partial_View_Has_Unknown_Discr (Typ) then
6520 Insert_After (N,
6521 Make_Invariant_Call (New_Occurrence_Of (Def_Id, Loc)));
6522 end if;
6523 end if;
6525 Default_Initialize_Object (Init_After);
6527 -- Generate attribute for Persistent_BSS if needed
6529 if Persistent_BSS_Mode
6530 and then Comes_From_Source (N)
6531 and then Is_Potentially_Persistent_Type (Typ)
6532 and then not Has_Init_Expression (N)
6533 and then Is_Library_Level_Entity (Def_Id)
6534 then
6535 declare
6536 Prag : Node_Id;
6537 begin
6538 Prag :=
6539 Make_Linker_Section_Pragma
6540 (Def_Id, Sloc (N), ".persistent.bss");
6541 Insert_After (N, Prag);
6542 Analyze (Prag);
6543 end;
6544 end if;
6546 -- If access type, then we know it is null if not initialized
6548 if Is_Access_Type (Typ) then
6549 Set_Is_Known_Null (Def_Id);
6550 end if;
6552 -- Explicit initialization present
6554 else
6555 -- Obtain actual expression from qualified expression
6557 if Nkind (Expr) = N_Qualified_Expression then
6558 Expr_Q := Expression (Expr);
6559 else
6560 Expr_Q := Expr;
6561 end if;
6563 -- When we have the appropriate type of aggregate in the expression
6564 -- (it has been determined during analysis of the aggregate by
6565 -- setting the delay flag), let's perform in place assignment and
6566 -- thus avoid creating a temporary.
6568 if Is_Delayed_Aggregate (Expr_Q) then
6569 Convert_Aggr_In_Object_Decl (N);
6571 -- Ada 2005 (AI-318-02): If the initialization expression is a call
6572 -- to a build-in-place function, then access to the declared object
6573 -- must be passed to the function. Currently we limit such functions
6574 -- to those with constrained limited result subtypes, but eventually
6575 -- plan to expand the allowed forms of functions that are treated as
6576 -- build-in-place.
6578 elsif Is_Build_In_Place_Function_Call (Expr_Q) then
6579 Make_Build_In_Place_Call_In_Object_Declaration (N, Expr_Q);
6581 -- The previous call expands the expression initializing the
6582 -- built-in-place object into further code that will be analyzed
6583 -- later. No further expansion needed here.
6585 return;
6587 -- This is the same as the previous 'elsif', except that the call has
6588 -- been transformed by other expansion activities into something like
6589 -- F(...)'Reference.
6591 elsif Nkind (Expr_Q) = N_Reference
6592 and then Is_Build_In_Place_Function_Call (Prefix (Expr_Q))
6593 and then not Is_Expanded_Build_In_Place_Call
6594 (Unqual_Conv (Prefix (Expr_Q)))
6595 then
6596 Make_Build_In_Place_Call_In_Anonymous_Context (Prefix (Expr_Q));
6598 -- The previous call expands the expression initializing the
6599 -- built-in-place object into further code that will be analyzed
6600 -- later. No further expansion needed here.
6602 return;
6604 -- Ada 2005 (AI-318-02): Specialization of the previous case for
6605 -- expressions containing a build-in-place function call whose
6606 -- returned object covers interface types, and Expr_Q has calls to
6607 -- Ada.Tags.Displace to displace the pointer to the returned build-
6608 -- in-place object to reference the secondary dispatch table of a
6609 -- covered interface type.
6611 elsif Present (Unqual_BIP_Iface_Function_Call (Expr_Q)) then
6612 Make_Build_In_Place_Iface_Call_In_Object_Declaration (N, Expr_Q);
6614 -- The previous call expands the expression initializing the
6615 -- built-in-place object into further code that will be analyzed
6616 -- later. No further expansion needed here.
6618 return;
6620 -- Ada 2005 (AI-251): Rewrite the expression that initializes a
6621 -- class-wide interface object to ensure that we copy the full
6622 -- object, unless we are targetting a VM where interfaces are handled
6623 -- by VM itself. Note that if the root type of Typ is an ancestor of
6624 -- Expr's type, both types share the same dispatch table and there is
6625 -- no need to displace the pointer.
6627 elsif Is_Interface (Typ)
6629 -- Avoid never-ending recursion because if Equivalent_Type is set
6630 -- then we've done it already and must not do it again.
6632 and then not
6633 (Nkind (Obj_Def) = N_Identifier
6634 and then Present (Equivalent_Type (Entity (Obj_Def))))
6635 then
6636 pragma Assert (Is_Class_Wide_Type (Typ));
6638 -- If the object is a return object of an inherently limited type,
6639 -- which implies build-in-place treatment, bypass the special
6640 -- treatment of class-wide interface initialization below. In this
6641 -- case, the expansion of the return statement will take care of
6642 -- creating the object (via allocator) and initializing it.
6644 if Is_Return_Object (Def_Id) and then Is_Limited_View (Typ) then
6645 null;
6647 elsif Tagged_Type_Expansion then
6648 declare
6649 Iface : constant Entity_Id := Root_Type (Typ);
6650 Expr_N : Node_Id := Expr;
6651 Expr_Typ : Entity_Id;
6652 New_Expr : Node_Id;
6653 Obj_Id : Entity_Id;
6654 Tag_Comp : Node_Id;
6656 begin
6657 -- If the original node of the expression was a conversion
6658 -- to this specific class-wide interface type then restore
6659 -- the original node because we must copy the object before
6660 -- displacing the pointer to reference the secondary tag
6661 -- component. This code must be kept synchronized with the
6662 -- expansion done by routine Expand_Interface_Conversion
6664 if not Comes_From_Source (Expr_N)
6665 and then Nkind (Expr_N) = N_Explicit_Dereference
6666 and then Nkind (Original_Node (Expr_N)) = N_Type_Conversion
6667 and then Etype (Original_Node (Expr_N)) = Typ
6668 then
6669 Rewrite (Expr_N, Original_Node (Expression (N)));
6670 end if;
6672 -- Avoid expansion of redundant interface conversion
6674 if Is_Interface (Etype (Expr_N))
6675 and then Nkind (Expr_N) = N_Type_Conversion
6676 and then Etype (Expr_N) = Typ
6677 then
6678 Expr_N := Expression (Expr_N);
6679 Set_Expression (N, Expr_N);
6680 end if;
6682 Obj_Id := Make_Temporary (Loc, 'D', Expr_N);
6683 Expr_Typ := Base_Type (Etype (Expr_N));
6685 if Is_Class_Wide_Type (Expr_Typ) then
6686 Expr_Typ := Root_Type (Expr_Typ);
6687 end if;
6689 -- Replace
6690 -- CW : I'Class := Obj;
6691 -- by
6692 -- Tmp : T := Obj;
6693 -- type Ityp is not null access I'Class;
6694 -- CW : I'Class renames Ityp (Tmp.I_Tag'Address).all;
6696 if Comes_From_Source (Expr_N)
6697 and then Nkind (Expr_N) = N_Identifier
6698 and then not Is_Interface (Expr_Typ)
6699 and then Interface_Present_In_Ancestor (Expr_Typ, Typ)
6700 and then (Expr_Typ = Etype (Expr_Typ)
6701 or else not
6702 Is_Variable_Size_Record (Etype (Expr_Typ)))
6703 then
6704 -- Copy the object
6706 Insert_Action (N,
6707 Make_Object_Declaration (Loc,
6708 Defining_Identifier => Obj_Id,
6709 Object_Definition =>
6710 New_Occurrence_Of (Expr_Typ, Loc),
6711 Expression => Relocate_Node (Expr_N)));
6713 -- Statically reference the tag associated with the
6714 -- interface
6716 Tag_Comp :=
6717 Make_Selected_Component (Loc,
6718 Prefix => New_Occurrence_Of (Obj_Id, Loc),
6719 Selector_Name =>
6720 New_Occurrence_Of
6721 (Find_Interface_Tag (Expr_Typ, Iface), Loc));
6723 -- Replace
6724 -- IW : I'Class := Obj;
6725 -- by
6726 -- type Equiv_Record is record ... end record;
6727 -- implicit subtype CW is <Class_Wide_Subtype>;
6728 -- Tmp : CW := CW!(Obj);
6729 -- type Ityp is not null access I'Class;
6730 -- IW : I'Class renames
6731 -- Ityp!(Displace (Temp'Address, I'Tag)).all;
6733 else
6734 -- Generate the equivalent record type and update the
6735 -- subtype indication to reference it.
6737 Expand_Subtype_From_Expr
6738 (N => N,
6739 Unc_Type => Typ,
6740 Subtype_Indic => Obj_Def,
6741 Exp => Expr_N);
6743 if not Is_Interface (Etype (Expr_N)) then
6744 New_Expr := Relocate_Node (Expr_N);
6746 -- For interface types we use 'Address which displaces
6747 -- the pointer to the base of the object (if required)
6749 else
6750 New_Expr :=
6751 Unchecked_Convert_To (Etype (Obj_Def),
6752 Make_Explicit_Dereference (Loc,
6753 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6754 Make_Attribute_Reference (Loc,
6755 Prefix => Relocate_Node (Expr_N),
6756 Attribute_Name => Name_Address))));
6757 end if;
6759 -- Copy the object
6761 if not Is_Limited_Record (Expr_Typ) then
6762 Insert_Action (N,
6763 Make_Object_Declaration (Loc,
6764 Defining_Identifier => Obj_Id,
6765 Object_Definition =>
6766 New_Occurrence_Of (Etype (Obj_Def), Loc),
6767 Expression => New_Expr));
6769 -- Rename limited type object since they cannot be copied
6770 -- This case occurs when the initialization expression
6771 -- has been previously expanded into a temporary object.
6773 else pragma Assert (not Comes_From_Source (Expr_Q));
6774 Insert_Action (N,
6775 Make_Object_Renaming_Declaration (Loc,
6776 Defining_Identifier => Obj_Id,
6777 Subtype_Mark =>
6778 New_Occurrence_Of (Etype (Obj_Def), Loc),
6779 Name =>
6780 Unchecked_Convert_To
6781 (Etype (Obj_Def), New_Expr)));
6782 end if;
6784 -- Dynamically reference the tag associated with the
6785 -- interface.
6787 Tag_Comp :=
6788 Make_Function_Call (Loc,
6789 Name => New_Occurrence_Of (RTE (RE_Displace), Loc),
6790 Parameter_Associations => New_List (
6791 Make_Attribute_Reference (Loc,
6792 Prefix => New_Occurrence_Of (Obj_Id, Loc),
6793 Attribute_Name => Name_Address),
6794 New_Occurrence_Of
6795 (Node (First_Elmt (Access_Disp_Table (Iface))),
6796 Loc)));
6797 end if;
6799 Rewrite (N,
6800 Make_Object_Renaming_Declaration (Loc,
6801 Defining_Identifier => Make_Temporary (Loc, 'D'),
6802 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
6803 Name =>
6804 Convert_Tag_To_Interface (Typ, Tag_Comp)));
6806 -- If the original entity comes from source, then mark the
6807 -- new entity as needing debug information, even though it's
6808 -- defined by a generated renaming that does not come from
6809 -- source, so that Materialize_Entity will be set on the
6810 -- entity when Debug_Renaming_Declaration is called during
6811 -- analysis.
6813 if Comes_From_Source (Def_Id) then
6814 Set_Debug_Info_Needed (Defining_Identifier (N));
6815 end if;
6817 Analyze (N, Suppress => All_Checks);
6819 -- Replace internal identifier of rewritten node by the
6820 -- identifier found in the sources. We also have to exchange
6821 -- entities containing their defining identifiers to ensure
6822 -- the correct replacement of the object declaration by this
6823 -- object renaming declaration because these identifiers
6824 -- were previously added by Enter_Name to the current scope.
6825 -- We must preserve the homonym chain of the source entity
6826 -- as well. We must also preserve the kind of the entity,
6827 -- which may be a constant. Preserve entity chain because
6828 -- itypes may have been generated already, and the full
6829 -- chain must be preserved for final freezing. Finally,
6830 -- preserve Comes_From_Source setting, so that debugging
6831 -- and cross-referencing information is properly kept, and
6832 -- preserve source location, to prevent spurious errors when
6833 -- entities are declared (they must have their own Sloc).
6835 declare
6836 New_Id : constant Entity_Id := Defining_Identifier (N);
6837 Next_Temp : constant Entity_Id := Next_Entity (New_Id);
6838 Save_CFS : constant Boolean :=
6839 Comes_From_Source (Def_Id);
6840 Save_SP : constant Node_Id := SPARK_Pragma (Def_Id);
6841 Save_SPI : constant Boolean :=
6842 SPARK_Pragma_Inherited (Def_Id);
6844 begin
6845 Link_Entities (New_Id, Next_Entity (Def_Id));
6846 Link_Entities (Def_Id, Next_Temp);
6848 Set_Chars (Defining_Identifier (N), Chars (Def_Id));
6849 Set_Homonym (Defining_Identifier (N), Homonym (Def_Id));
6850 Set_Ekind (Defining_Identifier (N), Ekind (Def_Id));
6851 Set_Sloc (Defining_Identifier (N), Sloc (Def_Id));
6853 Set_Comes_From_Source (Def_Id, False);
6855 -- ??? This is extremely dangerous!!! Exchanging entities
6856 -- is very low level, and as a result it resets flags and
6857 -- fields which belong to the original Def_Id. Several of
6858 -- these attributes are saved and restored, but there may
6859 -- be many more that need to be preserverd.
6861 Exchange_Entities (Defining_Identifier (N), Def_Id);
6863 -- Restore clobbered attributes
6865 Set_Comes_From_Source (Def_Id, Save_CFS);
6866 Set_SPARK_Pragma (Def_Id, Save_SP);
6867 Set_SPARK_Pragma_Inherited (Def_Id, Save_SPI);
6868 end;
6869 end;
6870 end if;
6872 return;
6874 -- Common case of explicit object initialization
6876 else
6877 -- In most cases, we must check that the initial value meets any
6878 -- constraint imposed by the declared type. However, there is one
6879 -- very important exception to this rule. If the entity has an
6880 -- unconstrained nominal subtype, then it acquired its constraints
6881 -- from the expression in the first place, and not only does this
6882 -- mean that the constraint check is not needed, but an attempt to
6883 -- perform the constraint check can cause order of elaboration
6884 -- problems.
6886 if not Is_Constr_Subt_For_U_Nominal (Typ) then
6888 -- If this is an allocator for an aggregate that has been
6889 -- allocated in place, delay checks until assignments are
6890 -- made, because the discriminants are not initialized.
6892 if Nkind (Expr) = N_Allocator
6893 and then No_Initialization (Expr)
6894 then
6895 null;
6897 -- Otherwise apply a constraint check now if no prev error
6899 elsif Nkind (Expr) /= N_Error then
6900 Apply_Constraint_Check (Expr, Typ);
6902 -- Deal with possible range check
6904 if Do_Range_Check (Expr) then
6906 -- If assignment checks are suppressed, turn off flag
6908 if Suppress_Assignment_Checks (N) then
6909 Set_Do_Range_Check (Expr, False);
6911 -- Otherwise generate the range check
6913 else
6914 Generate_Range_Check
6915 (Expr, Typ, CE_Range_Check_Failed);
6916 end if;
6917 end if;
6918 end if;
6919 end if;
6921 -- If the type is controlled and not inherently limited, then
6922 -- the target is adjusted after the copy and attached to the
6923 -- finalization list. However, no adjustment is done in the case
6924 -- where the object was initialized by a call to a function whose
6925 -- result is built in place, since no copy occurred. Similarly, no
6926 -- adjustment is required if we are going to rewrite the object
6927 -- declaration into a renaming declaration.
6929 if Needs_Finalization (Typ)
6930 and then not Is_Limited_View (Typ)
6931 and then not Rewrite_As_Renaming
6932 then
6933 Adj_Call :=
6934 Make_Adjust_Call (
6935 Obj_Ref => New_Occurrence_Of (Def_Id, Loc),
6936 Typ => Base_Typ);
6938 -- Guard against a missing [Deep_]Adjust when the base type
6939 -- was not properly frozen.
6941 if Present (Adj_Call) then
6942 Insert_Action_After (Init_After, Adj_Call);
6943 end if;
6944 end if;
6946 -- For tagged types, when an init value is given, the tag has to
6947 -- be re-initialized separately in order to avoid the propagation
6948 -- of a wrong tag coming from a view conversion unless the type
6949 -- is class wide (in this case the tag comes from the init value).
6950 -- Suppress the tag assignment when not Tagged_Type_Expansion
6951 -- because tags are represented implicitly in objects. Ditto for
6952 -- types that are CPP_CLASS, and for initializations that are
6953 -- aggregates, because they have to have the right tag.
6955 -- The re-assignment of the tag has to be done even if the object
6956 -- is a constant. The assignment must be analyzed after the
6957 -- declaration. If an address clause follows, this is handled as
6958 -- part of the freeze actions for the object, otherwise insert
6959 -- tag assignment here.
6961 Tag_Assign := Make_Tag_Assignment (N);
6963 if Present (Tag_Assign) then
6964 if Present (Following_Address_Clause (N)) then
6965 Ensure_Freeze_Node (Def_Id);
6967 else
6968 Insert_Action_After (Init_After, Tag_Assign);
6969 end if;
6971 -- Handle C++ constructor calls. Note that we do not check that
6972 -- Typ is a tagged type since the equivalent Ada type of a C++
6973 -- class that has no virtual methods is an untagged limited
6974 -- record type.
6976 elsif Is_CPP_Constructor_Call (Expr) then
6978 -- The call to the initialization procedure does NOT freeze the
6979 -- object being initialized.
6981 Id_Ref := New_Occurrence_Of (Def_Id, Loc);
6982 Set_Must_Not_Freeze (Id_Ref);
6983 Set_Assignment_OK (Id_Ref);
6985 Insert_Actions_After (Init_After,
6986 Build_Initialization_Call (Loc, Id_Ref, Typ,
6987 Constructor_Ref => Expr));
6989 -- We remove here the original call to the constructor
6990 -- to avoid its management in the backend
6992 Set_Expression (N, Empty);
6993 return;
6995 -- Handle initialization of limited tagged types
6997 elsif Is_Tagged_Type (Typ)
6998 and then Is_Class_Wide_Type (Typ)
6999 and then Is_Limited_Record (Typ)
7000 and then not Is_Limited_Interface (Typ)
7001 then
7002 -- Given that the type is limited we cannot perform a copy. If
7003 -- Expr_Q is the reference to a variable we mark the variable
7004 -- as OK_To_Rename to expand this declaration into a renaming
7005 -- declaration (see bellow).
7007 if Is_Entity_Name (Expr_Q) then
7008 Set_OK_To_Rename (Entity (Expr_Q));
7010 -- If we cannot convert the expression into a renaming we must
7011 -- consider it an internal error because the backend does not
7012 -- have support to handle it. Also, when a raise expression is
7013 -- encountered we ignore it since it doesn't return a value and
7014 -- thus cannot trigger a copy.
7016 elsif Nkind (Original_Node (Expr_Q)) /= N_Raise_Expression then
7017 pragma Assert (False);
7018 raise Program_Error;
7019 end if;
7021 -- For discrete types, set the Is_Known_Valid flag if the
7022 -- initializing value is known to be valid. Only do this for
7023 -- source assignments, since otherwise we can end up turning
7024 -- on the known valid flag prematurely from inserted code.
7026 elsif Comes_From_Source (N)
7027 and then Is_Discrete_Type (Typ)
7028 and then Expr_Known_Valid (Expr)
7029 then
7030 Set_Is_Known_Valid (Def_Id);
7032 elsif Is_Access_Type (Typ) then
7034 -- For access types set the Is_Known_Non_Null flag if the
7035 -- initializing value is known to be non-null. We can also set
7036 -- Can_Never_Be_Null if this is a constant.
7038 if Known_Non_Null (Expr) then
7039 Set_Is_Known_Non_Null (Def_Id, True);
7041 if Constant_Present (N) then
7042 Set_Can_Never_Be_Null (Def_Id);
7043 end if;
7044 end if;
7045 end if;
7047 -- If validity checking on copies, validate initial expression.
7048 -- But skip this if declaration is for a generic type, since it
7049 -- makes no sense to validate generic types. Not clear if this
7050 -- can happen for legal programs, but it definitely can arise
7051 -- from previous instantiation errors.
7053 if Validity_Checks_On
7054 and then Comes_From_Source (N)
7055 and then Validity_Check_Copies
7056 and then not Is_Generic_Type (Etype (Def_Id))
7057 then
7058 Ensure_Valid (Expr);
7059 Set_Is_Known_Valid (Def_Id);
7060 end if;
7061 end if;
7063 -- Cases where the back end cannot handle the initialization
7064 -- directly. In such cases, we expand an assignment that will
7065 -- be appropriately handled by Expand_N_Assignment_Statement.
7067 -- The exclusion of the unconstrained case is wrong, but for now it
7068 -- is too much trouble ???
7070 if (Is_Possibly_Unaligned_Slice (Expr)
7071 or else (Is_Possibly_Unaligned_Object (Expr)
7072 and then not Represented_As_Scalar (Etype (Expr))))
7073 and then not (Is_Array_Type (Etype (Expr))
7074 and then not Is_Constrained (Etype (Expr)))
7075 then
7076 declare
7077 Stat : constant Node_Id :=
7078 Make_Assignment_Statement (Loc,
7079 Name => New_Occurrence_Of (Def_Id, Loc),
7080 Expression => Relocate_Node (Expr));
7081 begin
7082 Set_Expression (N, Empty);
7083 Set_No_Initialization (N);
7084 Set_Assignment_OK (Name (Stat));
7085 Set_No_Ctrl_Actions (Stat);
7086 Insert_After_And_Analyze (Init_After, Stat);
7087 end;
7088 end if;
7089 end if;
7091 if Nkind (Obj_Def) = N_Access_Definition
7092 and then not Is_Local_Anonymous_Access (Etype (Def_Id))
7093 then
7094 -- An Ada 2012 stand-alone object of an anonymous access type
7096 declare
7097 Loc : constant Source_Ptr := Sloc (N);
7099 Level : constant Entity_Id :=
7100 Make_Defining_Identifier (Sloc (N),
7101 Chars =>
7102 New_External_Name (Chars (Def_Id), Suffix => "L"));
7104 Level_Expr : Node_Id;
7105 Level_Decl : Node_Id;
7107 begin
7108 Set_Ekind (Level, Ekind (Def_Id));
7109 Set_Etype (Level, Standard_Natural);
7110 Set_Scope (Level, Scope (Def_Id));
7112 if No (Expr) then
7114 -- Set accessibility level of null
7116 Level_Expr :=
7117 Make_Integer_Literal (Loc, Scope_Depth (Standard_Standard));
7119 else
7120 Level_Expr := Dynamic_Accessibility_Level (Expr);
7121 end if;
7123 Level_Decl :=
7124 Make_Object_Declaration (Loc,
7125 Defining_Identifier => Level,
7126 Object_Definition =>
7127 New_Occurrence_Of (Standard_Natural, Loc),
7128 Expression => Level_Expr,
7129 Constant_Present => Constant_Present (N),
7130 Has_Init_Expression => True);
7132 Insert_Action_After (Init_After, Level_Decl);
7134 Set_Extra_Accessibility (Def_Id, Level);
7135 end;
7136 end if;
7138 -- If the object is default initialized and its type is subject to
7139 -- pragma Default_Initial_Condition, add a runtime check to verify
7140 -- the assumption of the pragma (SPARK RM 7.3.3). Generate:
7142 -- <Base_Typ>DIC (<Base_Typ> (Def_Id));
7144 -- Note that the check is generated for source objects only
7146 if Comes_From_Source (Def_Id)
7147 and then Has_DIC (Typ)
7148 and then Present (DIC_Procedure (Typ))
7149 and then not Has_Init_Expression (N)
7150 then
7151 declare
7152 DIC_Call : constant Node_Id := Build_DIC_Call (Loc, Def_Id, Typ);
7154 begin
7155 if Present (Next_N) then
7156 Insert_Before_And_Analyze (Next_N, DIC_Call);
7158 -- The object declaration is the last node in a declarative or a
7159 -- statement list.
7161 else
7162 Append_To (List_Containing (N), DIC_Call);
7163 Analyze (DIC_Call);
7164 end if;
7165 end;
7166 end if;
7168 -- Final transformation - turn the object declaration into a renaming
7169 -- if appropriate. If this is the completion of a deferred constant
7170 -- declaration, then this transformation generates what would be
7171 -- illegal code if written by hand, but that's OK.
7173 if Present (Expr) then
7174 if Rewrite_As_Renaming then
7175 Rewrite (N,
7176 Make_Object_Renaming_Declaration (Loc,
7177 Defining_Identifier => Defining_Identifier (N),
7178 Subtype_Mark => Obj_Def,
7179 Name => Expr_Q));
7181 -- We do not analyze this renaming declaration, because all its
7182 -- components have already been analyzed, and if we were to go
7183 -- ahead and analyze it, we would in effect be trying to generate
7184 -- another declaration of X, which won't do.
7186 Set_Renamed_Object (Defining_Identifier (N), Expr_Q);
7187 Set_Analyzed (N);
7189 -- We do need to deal with debug issues for this renaming
7191 -- First, if entity comes from source, then mark it as needing
7192 -- debug information, even though it is defined by a generated
7193 -- renaming that does not come from source.
7195 if Comes_From_Source (Defining_Identifier (N)) then
7196 Set_Debug_Info_Needed (Defining_Identifier (N));
7197 end if;
7199 -- Now call the routine to generate debug info for the renaming
7201 declare
7202 Decl : constant Node_Id := Debug_Renaming_Declaration (N);
7203 begin
7204 if Present (Decl) then
7205 Insert_Action (N, Decl);
7206 end if;
7207 end;
7208 end if;
7209 end if;
7211 -- Exception on library entity not available
7213 exception
7214 when RE_Not_Available =>
7215 return;
7216 end Expand_N_Object_Declaration;
7218 ---------------------------------
7219 -- Expand_N_Subtype_Indication --
7220 ---------------------------------
7222 -- Add a check on the range of the subtype. The static case is partially
7223 -- duplicated by Process_Range_Expr_In_Decl in Sem_Ch3, but we still need
7224 -- to check here for the static case in order to avoid generating
7225 -- extraneous expanded code. Also deal with validity checking.
7227 procedure Expand_N_Subtype_Indication (N : Node_Id) is
7228 Ran : constant Node_Id := Range_Expression (Constraint (N));
7229 Typ : constant Entity_Id := Entity (Subtype_Mark (N));
7231 begin
7232 if Nkind (Constraint (N)) = N_Range_Constraint then
7233 Validity_Check_Range (Range_Expression (Constraint (N)));
7234 end if;
7236 if Nkind_In (Parent (N), N_Constrained_Array_Definition, N_Slice) then
7237 Apply_Range_Check (Ran, Typ);
7238 end if;
7239 end Expand_N_Subtype_Indication;
7241 ---------------------------
7242 -- Expand_N_Variant_Part --
7243 ---------------------------
7245 -- Note: this procedure no longer has any effect. It used to be that we
7246 -- would replace the choices in the last variant by a when others, and
7247 -- also expanded static predicates in variant choices here, but both of
7248 -- those activities were being done too early, since we can't check the
7249 -- choices until the statically predicated subtypes are frozen, which can
7250 -- happen as late as the free point of the record, and we can't change the
7251 -- last choice to an others before checking the choices, which is now done
7252 -- at the freeze point of the record.
7254 procedure Expand_N_Variant_Part (N : Node_Id) is
7255 begin
7256 null;
7257 end Expand_N_Variant_Part;
7259 ---------------------------------
7260 -- Expand_Previous_Access_Type --
7261 ---------------------------------
7263 procedure Expand_Previous_Access_Type (Def_Id : Entity_Id) is
7264 Ptr_Typ : Entity_Id;
7266 begin
7267 -- Find all access types in the current scope whose designated type is
7268 -- Def_Id and build master renamings for them.
7270 Ptr_Typ := First_Entity (Current_Scope);
7271 while Present (Ptr_Typ) loop
7272 if Is_Access_Type (Ptr_Typ)
7273 and then Designated_Type (Ptr_Typ) = Def_Id
7274 and then No (Master_Id (Ptr_Typ))
7275 then
7276 -- Ensure that the designated type has a master
7278 Build_Master_Entity (Def_Id);
7280 -- Private and incomplete types complicate the insertion of master
7281 -- renamings because the access type may precede the full view of
7282 -- the designated type. For this reason, the master renamings are
7283 -- inserted relative to the designated type.
7285 Build_Master_Renaming (Ptr_Typ, Ins_Nod => Parent (Def_Id));
7286 end if;
7288 Next_Entity (Ptr_Typ);
7289 end loop;
7290 end Expand_Previous_Access_Type;
7292 -----------------------------
7293 -- Expand_Record_Extension --
7294 -----------------------------
7296 -- Add a field _parent at the beginning of the record extension. This is
7297 -- used to implement inheritance. Here are some examples of expansion:
7299 -- 1. no discriminants
7300 -- type T2 is new T1 with null record;
7301 -- gives
7302 -- type T2 is new T1 with record
7303 -- _Parent : T1;
7304 -- end record;
7306 -- 2. renamed discriminants
7307 -- type T2 (B, C : Int) is new T1 (A => B) with record
7308 -- _Parent : T1 (A => B);
7309 -- D : Int;
7310 -- end;
7312 -- 3. inherited discriminants
7313 -- type T2 is new T1 with record -- discriminant A inherited
7314 -- _Parent : T1 (A);
7315 -- D : Int;
7316 -- end;
7318 procedure Expand_Record_Extension (T : Entity_Id; Def : Node_Id) is
7319 Indic : constant Node_Id := Subtype_Indication (Def);
7320 Loc : constant Source_Ptr := Sloc (Def);
7321 Rec_Ext_Part : Node_Id := Record_Extension_Part (Def);
7322 Par_Subtype : Entity_Id;
7323 Comp_List : Node_Id;
7324 Comp_Decl : Node_Id;
7325 Parent_N : Node_Id;
7326 D : Entity_Id;
7327 List_Constr : constant List_Id := New_List;
7329 begin
7330 -- Expand_Record_Extension is called directly from the semantics, so
7331 -- we must check to see whether expansion is active before proceeding,
7332 -- because this affects the visibility of selected components in bodies
7333 -- of instances.
7335 if not Expander_Active then
7336 return;
7337 end if;
7339 -- This may be a derivation of an untagged private type whose full
7340 -- view is tagged, in which case the Derived_Type_Definition has no
7341 -- extension part. Build an empty one now.
7343 if No (Rec_Ext_Part) then
7344 Rec_Ext_Part :=
7345 Make_Record_Definition (Loc,
7346 End_Label => Empty,
7347 Component_List => Empty,
7348 Null_Present => True);
7350 Set_Record_Extension_Part (Def, Rec_Ext_Part);
7351 Mark_Rewrite_Insertion (Rec_Ext_Part);
7352 end if;
7354 Comp_List := Component_List (Rec_Ext_Part);
7356 Parent_N := Make_Defining_Identifier (Loc, Name_uParent);
7358 -- If the derived type inherits its discriminants the type of the
7359 -- _parent field must be constrained by the inherited discriminants
7361 if Has_Discriminants (T)
7362 and then Nkind (Indic) /= N_Subtype_Indication
7363 and then not Is_Constrained (Entity (Indic))
7364 then
7365 D := First_Discriminant (T);
7366 while Present (D) loop
7367 Append_To (List_Constr, New_Occurrence_Of (D, Loc));
7368 Next_Discriminant (D);
7369 end loop;
7371 Par_Subtype :=
7372 Process_Subtype (
7373 Make_Subtype_Indication (Loc,
7374 Subtype_Mark => New_Occurrence_Of (Entity (Indic), Loc),
7375 Constraint =>
7376 Make_Index_Or_Discriminant_Constraint (Loc,
7377 Constraints => List_Constr)),
7378 Def);
7380 -- Otherwise the original subtype_indication is just what is needed
7382 else
7383 Par_Subtype := Process_Subtype (New_Copy_Tree (Indic), Def);
7384 end if;
7386 Set_Parent_Subtype (T, Par_Subtype);
7388 Comp_Decl :=
7389 Make_Component_Declaration (Loc,
7390 Defining_Identifier => Parent_N,
7391 Component_Definition =>
7392 Make_Component_Definition (Loc,
7393 Aliased_Present => False,
7394 Subtype_Indication => New_Occurrence_Of (Par_Subtype, Loc)));
7396 if Null_Present (Rec_Ext_Part) then
7397 Set_Component_List (Rec_Ext_Part,
7398 Make_Component_List (Loc,
7399 Component_Items => New_List (Comp_Decl),
7400 Variant_Part => Empty,
7401 Null_Present => False));
7402 Set_Null_Present (Rec_Ext_Part, False);
7404 elsif Null_Present (Comp_List)
7405 or else Is_Empty_List (Component_Items (Comp_List))
7406 then
7407 Set_Component_Items (Comp_List, New_List (Comp_Decl));
7408 Set_Null_Present (Comp_List, False);
7410 else
7411 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
7412 end if;
7414 Analyze (Comp_Decl);
7415 end Expand_Record_Extension;
7417 ------------------------
7418 -- Expand_Tagged_Root --
7419 ------------------------
7421 procedure Expand_Tagged_Root (T : Entity_Id) is
7422 Def : constant Node_Id := Type_Definition (Parent (T));
7423 Comp_List : Node_Id;
7424 Comp_Decl : Node_Id;
7425 Sloc_N : Source_Ptr;
7427 begin
7428 if Null_Present (Def) then
7429 Set_Component_List (Def,
7430 Make_Component_List (Sloc (Def),
7431 Component_Items => Empty_List,
7432 Variant_Part => Empty,
7433 Null_Present => True));
7434 end if;
7436 Comp_List := Component_List (Def);
7438 if Null_Present (Comp_List)
7439 or else Is_Empty_List (Component_Items (Comp_List))
7440 then
7441 Sloc_N := Sloc (Comp_List);
7442 else
7443 Sloc_N := Sloc (First (Component_Items (Comp_List)));
7444 end if;
7446 Comp_Decl :=
7447 Make_Component_Declaration (Sloc_N,
7448 Defining_Identifier => First_Tag_Component (T),
7449 Component_Definition =>
7450 Make_Component_Definition (Sloc_N,
7451 Aliased_Present => False,
7452 Subtype_Indication => New_Occurrence_Of (RTE (RE_Tag), Sloc_N)));
7454 if Null_Present (Comp_List)
7455 or else Is_Empty_List (Component_Items (Comp_List))
7456 then
7457 Set_Component_Items (Comp_List, New_List (Comp_Decl));
7458 Set_Null_Present (Comp_List, False);
7460 else
7461 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
7462 end if;
7464 -- We don't Analyze the whole expansion because the tag component has
7465 -- already been analyzed previously. Here we just insure that the tree
7466 -- is coherent with the semantic decoration
7468 Find_Type (Subtype_Indication (Component_Definition (Comp_Decl)));
7470 exception
7471 when RE_Not_Available =>
7472 return;
7473 end Expand_Tagged_Root;
7475 ------------------------------
7476 -- Freeze_Stream_Operations --
7477 ------------------------------
7479 procedure Freeze_Stream_Operations (N : Node_Id; Typ : Entity_Id) is
7480 Names : constant array (1 .. 4) of TSS_Name_Type :=
7481 (TSS_Stream_Input,
7482 TSS_Stream_Output,
7483 TSS_Stream_Read,
7484 TSS_Stream_Write);
7485 Stream_Op : Entity_Id;
7487 begin
7488 -- Primitive operations of tagged types are frozen when the dispatch
7489 -- table is constructed.
7491 if not Comes_From_Source (Typ) or else Is_Tagged_Type (Typ) then
7492 return;
7493 end if;
7495 for J in Names'Range loop
7496 Stream_Op := TSS (Typ, Names (J));
7498 if Present (Stream_Op)
7499 and then Is_Subprogram (Stream_Op)
7500 and then Nkind (Unit_Declaration_Node (Stream_Op)) =
7501 N_Subprogram_Declaration
7502 and then not Is_Frozen (Stream_Op)
7503 then
7504 Append_Freeze_Actions (Typ, Freeze_Entity (Stream_Op, N));
7505 end if;
7506 end loop;
7507 end Freeze_Stream_Operations;
7509 -----------------
7510 -- Freeze_Type --
7511 -----------------
7513 -- Full type declarations are expanded at the point at which the type is
7514 -- frozen. The formal N is the Freeze_Node for the type. Any statements or
7515 -- declarations generated by the freezing (e.g. the procedure generated
7516 -- for initialization) are chained in the Actions field list of the freeze
7517 -- node using Append_Freeze_Actions.
7519 -- WARNING: This routine manages Ghost regions. Return statements must be
7520 -- replaced by gotos which jump to the end of the routine and restore the
7521 -- Ghost mode.
7523 function Freeze_Type (N : Node_Id) return Boolean is
7524 procedure Process_RACW_Types (Typ : Entity_Id);
7525 -- Validate and generate stubs for all RACW types associated with type
7526 -- Typ.
7528 procedure Process_Pending_Access_Types (Typ : Entity_Id);
7529 -- Associate type Typ's Finalize_Address primitive with the finalization
7530 -- masters of pending access-to-Typ types.
7532 ------------------------
7533 -- Process_RACW_Types --
7534 ------------------------
7536 procedure Process_RACW_Types (Typ : Entity_Id) is
7537 List : constant Elist_Id := Access_Types_To_Process (N);
7538 E : Elmt_Id;
7539 Seen : Boolean := False;
7541 begin
7542 if Present (List) then
7543 E := First_Elmt (List);
7544 while Present (E) loop
7545 if Is_Remote_Access_To_Class_Wide_Type (Node (E)) then
7546 Validate_RACW_Primitives (Node (E));
7547 Seen := True;
7548 end if;
7550 Next_Elmt (E);
7551 end loop;
7552 end if;
7554 -- If there are RACWs designating this type, make stubs now
7556 if Seen then
7557 Remote_Types_Tagged_Full_View_Encountered (Typ);
7558 end if;
7559 end Process_RACW_Types;
7561 ----------------------------------
7562 -- Process_Pending_Access_Types --
7563 ----------------------------------
7565 procedure Process_Pending_Access_Types (Typ : Entity_Id) is
7566 E : Elmt_Id;
7568 begin
7569 -- Finalize_Address is not generated in CodePeer mode because the
7570 -- body contains address arithmetic. This processing is disabled.
7572 if CodePeer_Mode then
7573 null;
7575 -- Certain itypes are generated for contexts that cannot allocate
7576 -- objects and should not set primitive Finalize_Address.
7578 elsif Is_Itype (Typ)
7579 and then Nkind (Associated_Node_For_Itype (Typ)) =
7580 N_Explicit_Dereference
7581 then
7582 null;
7584 -- When an access type is declared after the incomplete view of a
7585 -- Taft-amendment type, the access type is considered pending in
7586 -- case the full view of the Taft-amendment type is controlled. If
7587 -- this is indeed the case, associate the Finalize_Address routine
7588 -- of the full view with the finalization masters of all pending
7589 -- access types. This scenario applies to anonymous access types as
7590 -- well.
7592 elsif Needs_Finalization (Typ)
7593 and then Present (Pending_Access_Types (Typ))
7594 then
7595 E := First_Elmt (Pending_Access_Types (Typ));
7596 while Present (E) loop
7598 -- Generate:
7599 -- Set_Finalize_Address
7600 -- (Ptr_Typ, <Typ>FD'Unrestricted_Access);
7602 Append_Freeze_Action (Typ,
7603 Make_Set_Finalize_Address_Call
7604 (Loc => Sloc (N),
7605 Ptr_Typ => Node (E)));
7607 Next_Elmt (E);
7608 end loop;
7609 end if;
7610 end Process_Pending_Access_Types;
7612 -- Local variables
7614 Def_Id : constant Entity_Id := Entity (N);
7616 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
7617 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
7618 -- Save the Ghost-related attributes to restore on exit
7620 Result : Boolean := False;
7622 -- Start of processing for Freeze_Type
7624 begin
7625 -- The type being frozen may be subject to pragma Ghost. Set the mode
7626 -- now to ensure that any nodes generated during freezing are properly
7627 -- marked as Ghost.
7629 Set_Ghost_Mode (Def_Id);
7631 -- Process any remote access-to-class-wide types designating the type
7632 -- being frozen.
7634 Process_RACW_Types (Def_Id);
7636 -- Freeze processing for record types
7638 if Is_Record_Type (Def_Id) then
7639 if Ekind (Def_Id) = E_Record_Type then
7640 Expand_Freeze_Record_Type (N);
7641 elsif Is_Class_Wide_Type (Def_Id) then
7642 Expand_Freeze_Class_Wide_Type (N);
7643 end if;
7645 -- Freeze processing for array types
7647 elsif Is_Array_Type (Def_Id) then
7648 Expand_Freeze_Array_Type (N);
7650 -- Freeze processing for access types
7652 -- For pool-specific access types, find out the pool object used for
7653 -- this type, needs actual expansion of it in some cases. Here are the
7654 -- different cases :
7656 -- 1. Rep Clause "for Def_Id'Storage_Size use 0;"
7657 -- ---> don't use any storage pool
7659 -- 2. Rep Clause : for Def_Id'Storage_Size use Expr.
7660 -- Expand:
7661 -- Def_Id__Pool : Stack_Bounded_Pool (Expr, DT'Size, DT'Alignment);
7663 -- 3. Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
7664 -- ---> Storage Pool is the specified one
7666 -- See GNAT Pool packages in the Run-Time for more details
7668 elsif Ekind_In (Def_Id, E_Access_Type, E_General_Access_Type) then
7669 declare
7670 Loc : constant Source_Ptr := Sloc (N);
7671 Desig_Type : constant Entity_Id := Designated_Type (Def_Id);
7673 Freeze_Action_Typ : Entity_Id;
7674 Pool_Object : Entity_Id;
7676 begin
7677 -- Case 1
7679 -- Rep Clause "for Def_Id'Storage_Size use 0;"
7680 -- ---> don't use any storage pool
7682 if No_Pool_Assigned (Def_Id) then
7683 null;
7685 -- Case 2
7687 -- Rep Clause : for Def_Id'Storage_Size use Expr.
7688 -- ---> Expand:
7689 -- Def_Id__Pool : Stack_Bounded_Pool
7690 -- (Expr, DT'Size, DT'Alignment);
7692 elsif Has_Storage_Size_Clause (Def_Id) then
7693 declare
7694 DT_Align : Node_Id;
7695 DT_Size : Node_Id;
7697 begin
7698 -- For unconstrained composite types we give a size of zero
7699 -- so that the pool knows that it needs a special algorithm
7700 -- for variable size object allocation.
7702 if Is_Composite_Type (Desig_Type)
7703 and then not Is_Constrained (Desig_Type)
7704 then
7705 DT_Size := Make_Integer_Literal (Loc, 0);
7706 DT_Align := Make_Integer_Literal (Loc, Maximum_Alignment);
7708 else
7709 DT_Size :=
7710 Make_Attribute_Reference (Loc,
7711 Prefix => New_Occurrence_Of (Desig_Type, Loc),
7712 Attribute_Name => Name_Max_Size_In_Storage_Elements);
7714 DT_Align :=
7715 Make_Attribute_Reference (Loc,
7716 Prefix => New_Occurrence_Of (Desig_Type, Loc),
7717 Attribute_Name => Name_Alignment);
7718 end if;
7720 Pool_Object :=
7721 Make_Defining_Identifier (Loc,
7722 Chars => New_External_Name (Chars (Def_Id), 'P'));
7724 -- We put the code associated with the pools in the entity
7725 -- that has the later freeze node, usually the access type
7726 -- but it can also be the designated_type; because the pool
7727 -- code requires both those types to be frozen
7729 if Is_Frozen (Desig_Type)
7730 and then (No (Freeze_Node (Desig_Type))
7731 or else Analyzed (Freeze_Node (Desig_Type)))
7732 then
7733 Freeze_Action_Typ := Def_Id;
7735 -- A Taft amendment type cannot get the freeze actions
7736 -- since the full view is not there.
7738 elsif Is_Incomplete_Or_Private_Type (Desig_Type)
7739 and then No (Full_View (Desig_Type))
7740 then
7741 Freeze_Action_Typ := Def_Id;
7743 else
7744 Freeze_Action_Typ := Desig_Type;
7745 end if;
7747 Append_Freeze_Action (Freeze_Action_Typ,
7748 Make_Object_Declaration (Loc,
7749 Defining_Identifier => Pool_Object,
7750 Object_Definition =>
7751 Make_Subtype_Indication (Loc,
7752 Subtype_Mark =>
7753 New_Occurrence_Of
7754 (RTE (RE_Stack_Bounded_Pool), Loc),
7756 Constraint =>
7757 Make_Index_Or_Discriminant_Constraint (Loc,
7758 Constraints => New_List (
7760 -- First discriminant is the Pool Size
7762 New_Occurrence_Of (
7763 Storage_Size_Variable (Def_Id), Loc),
7765 -- Second discriminant is the element size
7767 DT_Size,
7769 -- Third discriminant is the alignment
7771 DT_Align)))));
7772 end;
7774 Set_Associated_Storage_Pool (Def_Id, Pool_Object);
7776 -- Case 3
7778 -- Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
7779 -- ---> Storage Pool is the specified one
7781 -- When compiling in Ada 2012 mode, ensure that the accessibility
7782 -- level of the subpool access type is not deeper than that of the
7783 -- pool_with_subpools.
7785 elsif Ada_Version >= Ada_2012
7786 and then Present (Associated_Storage_Pool (Def_Id))
7788 -- Omit this check for the case of a configurable run-time that
7789 -- does not provide package System.Storage_Pools.Subpools.
7791 and then RTE_Available (RE_Root_Storage_Pool_With_Subpools)
7792 then
7793 declare
7794 Loc : constant Source_Ptr := Sloc (Def_Id);
7795 Pool : constant Entity_Id :=
7796 Associated_Storage_Pool (Def_Id);
7797 RSPWS : constant Entity_Id :=
7798 RTE (RE_Root_Storage_Pool_With_Subpools);
7800 begin
7801 -- It is known that the accessibility level of the access
7802 -- type is deeper than that of the pool.
7804 if Type_Access_Level (Def_Id) > Object_Access_Level (Pool)
7805 and then not Accessibility_Checks_Suppressed (Def_Id)
7806 and then not Accessibility_Checks_Suppressed (Pool)
7807 then
7808 -- Static case: the pool is known to be a descendant of
7809 -- Root_Storage_Pool_With_Subpools.
7811 if Is_Ancestor (RSPWS, Etype (Pool)) then
7812 Error_Msg_N
7813 ("??subpool access type has deeper accessibility "
7814 & "level than pool", Def_Id);
7816 Append_Freeze_Action (Def_Id,
7817 Make_Raise_Program_Error (Loc,
7818 Reason => PE_Accessibility_Check_Failed));
7820 -- Dynamic case: when the pool is of a class-wide type,
7821 -- it may or may not support subpools depending on the
7822 -- path of derivation. Generate:
7824 -- if Def_Id in RSPWS'Class then
7825 -- raise Program_Error;
7826 -- end if;
7828 elsif Is_Class_Wide_Type (Etype (Pool)) then
7829 Append_Freeze_Action (Def_Id,
7830 Make_If_Statement (Loc,
7831 Condition =>
7832 Make_In (Loc,
7833 Left_Opnd => New_Occurrence_Of (Pool, Loc),
7834 Right_Opnd =>
7835 New_Occurrence_Of
7836 (Class_Wide_Type (RSPWS), Loc)),
7838 Then_Statements => New_List (
7839 Make_Raise_Program_Error (Loc,
7840 Reason => PE_Accessibility_Check_Failed))));
7841 end if;
7842 end if;
7843 end;
7844 end if;
7846 -- For access-to-controlled types (including class-wide types and
7847 -- Taft-amendment types, which potentially have controlled
7848 -- components), expand the list controller object that will store
7849 -- the dynamically allocated objects. Don't do this transformation
7850 -- for expander-generated access types, but do it for types that
7851 -- are the full view of types derived from other private types.
7852 -- Also suppress the list controller in the case of a designated
7853 -- type with convention Java, since this is used when binding to
7854 -- Java API specs, where there's no equivalent of a finalization
7855 -- list and we don't want to pull in the finalization support if
7856 -- not needed.
7858 if not Comes_From_Source (Def_Id)
7859 and then not Has_Private_Declaration (Def_Id)
7860 then
7861 null;
7863 -- An exception is made for types defined in the run-time because
7864 -- Ada.Tags.Tag itself is such a type and cannot afford this
7865 -- unnecessary overhead that would generates a loop in the
7866 -- expansion scheme. Another exception is if Restrictions
7867 -- (No_Finalization) is active, since then we know nothing is
7868 -- controlled.
7870 elsif Restriction_Active (No_Finalization)
7871 or else In_Runtime (Def_Id)
7872 then
7873 null;
7875 -- Create a finalization master for an access-to-controlled type
7876 -- or an access-to-incomplete type. It is assumed that the full
7877 -- view will be controlled.
7879 elsif Needs_Finalization (Desig_Type)
7880 or else (Is_Incomplete_Type (Desig_Type)
7881 and then No (Full_View (Desig_Type)))
7882 then
7883 Build_Finalization_Master (Def_Id);
7885 -- Create a finalization master when the designated type contains
7886 -- a private component. It is assumed that the full view will be
7887 -- controlled.
7889 elsif Has_Private_Component (Desig_Type) then
7890 Build_Finalization_Master
7891 (Typ => Def_Id,
7892 For_Private => True,
7893 Context_Scope => Scope (Def_Id),
7894 Insertion_Node => Declaration_Node (Desig_Type));
7895 end if;
7896 end;
7898 -- Freeze processing for enumeration types
7900 elsif Ekind (Def_Id) = E_Enumeration_Type then
7902 -- We only have something to do if we have a non-standard
7903 -- representation (i.e. at least one literal whose pos value
7904 -- is not the same as its representation)
7906 if Has_Non_Standard_Rep (Def_Id) then
7907 Expand_Freeze_Enumeration_Type (N);
7908 end if;
7910 -- Private types that are completed by a derivation from a private
7911 -- type have an internally generated full view, that needs to be
7912 -- frozen. This must be done explicitly because the two views share
7913 -- the freeze node, and the underlying full view is not visible when
7914 -- the freeze node is analyzed.
7916 elsif Is_Private_Type (Def_Id)
7917 and then Is_Derived_Type (Def_Id)
7918 and then Present (Full_View (Def_Id))
7919 and then Is_Itype (Full_View (Def_Id))
7920 and then Has_Private_Declaration (Full_View (Def_Id))
7921 and then Freeze_Node (Full_View (Def_Id)) = N
7922 then
7923 Set_Entity (N, Full_View (Def_Id));
7924 Result := Freeze_Type (N);
7925 Set_Entity (N, Def_Id);
7927 -- All other types require no expander action. There are such cases
7928 -- (e.g. task types and protected types). In such cases, the freeze
7929 -- nodes are there for use by Gigi.
7931 end if;
7933 -- Complete the initialization of all pending access types' finalization
7934 -- masters now that the designated type has been is frozen and primitive
7935 -- Finalize_Address generated.
7937 Process_Pending_Access_Types (Def_Id);
7938 Freeze_Stream_Operations (N, Def_Id);
7940 -- Generate the [spec and] body of the procedure tasked with the runtime
7941 -- verification of pragma Default_Initial_Condition's expression.
7943 if Has_DIC (Def_Id) then
7944 Build_DIC_Procedure_Body (Def_Id, For_Freeze => True);
7945 end if;
7947 -- Generate the [spec and] body of the invariant procedure tasked with
7948 -- the runtime verification of all invariants that pertain to the type.
7949 -- This includes invariants on the partial and full view, inherited
7950 -- class-wide invariants from parent types or interfaces, and invariants
7951 -- on array elements or record components.
7953 if Is_Interface (Def_Id) then
7955 -- Interfaces are treated as the partial view of a private type in
7956 -- order to achieve uniformity with the general case. As a result, an
7957 -- interface receives only a "partial" invariant procedure which is
7958 -- never called.
7960 if Has_Own_Invariants (Def_Id) then
7961 Build_Invariant_Procedure_Body
7962 (Typ => Def_Id,
7963 Partial_Invariant => Is_Interface (Def_Id));
7964 end if;
7966 -- Non-interface types
7968 -- Do not generate invariant procedure within other assertion
7969 -- subprograms, which may involve local declarations of local
7970 -- subtypes to which these checks do not apply.
7972 elsif Has_Invariants (Def_Id) then
7973 if Within_Internal_Subprogram
7974 or else (Ekind (Current_Scope) = E_Function
7975 and then Is_Predicate_Function (Current_Scope))
7976 then
7977 null;
7978 else
7979 Build_Invariant_Procedure_Body (Def_Id);
7980 end if;
7981 end if;
7983 Restore_Ghost_Region (Saved_GM, Saved_IGR);
7985 return Result;
7987 exception
7988 when RE_Not_Available =>
7989 Restore_Ghost_Region (Saved_GM, Saved_IGR);
7991 return False;
7992 end Freeze_Type;
7994 -------------------------
7995 -- Get_Simple_Init_Val --
7996 -------------------------
7998 function Get_Simple_Init_Val
7999 (Typ : Entity_Id;
8000 N : Node_Id;
8001 Size : Uint := No_Uint) return Node_Id
8003 IV_Attribute : constant Boolean :=
8004 Nkind (N) = N_Attribute_Reference
8005 and then Attribute_Name (N) = Name_Invalid_Value;
8007 Loc : constant Source_Ptr := Sloc (N);
8009 procedure Extract_Subtype_Bounds
8010 (Lo_Bound : out Uint;
8011 Hi_Bound : out Uint);
8012 -- Inspect subtype Typ as well its ancestor subtypes and derived types
8013 -- to determine the best known information about the bounds of the type.
8014 -- The output parameters are set as follows:
8016 -- * Lo_Bound - Set to No_Unit when there is no information available,
8017 -- or to the known low bound.
8019 -- * Hi_Bound - Set to No_Unit when there is no information available,
8020 -- or to the known high bound.
8022 function Simple_Init_Array_Type return Node_Id;
8023 -- Build an expression to initialize array type Typ
8025 function Simple_Init_Defaulted_Type return Node_Id;
8026 -- Build an expression to initialize type Typ which is subject to
8027 -- aspect Default_Value.
8029 function Simple_Init_Initialize_Scalars_Type
8030 (Size_To_Use : Uint) return Node_Id;
8031 -- Build an expression to initialize scalar type Typ which is subject to
8032 -- pragma Initialize_Scalars. Size_To_Use is the size of the object.
8034 function Simple_Init_Normalize_Scalars_Type
8035 (Size_To_Use : Uint) return Node_Id;
8036 -- Build an expression to initialize scalar type Typ which is subject to
8037 -- pragma Normalize_Scalars. Size_To_Use is the size of the object.
8039 function Simple_Init_Private_Type return Node_Id;
8040 -- Build an expression to initialize private type Typ
8042 function Simple_Init_Scalar_Type return Node_Id;
8043 -- Build an expression to initialize scalar type Typ
8045 ----------------------------
8046 -- Extract_Subtype_Bounds --
8047 ----------------------------
8049 procedure Extract_Subtype_Bounds
8050 (Lo_Bound : out Uint;
8051 Hi_Bound : out Uint)
8053 ST1 : Entity_Id;
8054 ST2 : Entity_Id;
8055 Lo : Node_Id;
8056 Hi : Node_Id;
8057 Lo_Val : Uint;
8058 Hi_Val : Uint;
8060 begin
8061 Lo_Bound := No_Uint;
8062 Hi_Bound := No_Uint;
8064 -- Loop to climb ancestor subtypes and derived types
8066 ST1 := Typ;
8067 loop
8068 if not Is_Discrete_Type (ST1) then
8069 return;
8070 end if;
8072 Lo := Type_Low_Bound (ST1);
8073 Hi := Type_High_Bound (ST1);
8075 if Compile_Time_Known_Value (Lo) then
8076 Lo_Val := Expr_Value (Lo);
8078 if Lo_Bound = No_Uint or else Lo_Bound < Lo_Val then
8079 Lo_Bound := Lo_Val;
8080 end if;
8081 end if;
8083 if Compile_Time_Known_Value (Hi) then
8084 Hi_Val := Expr_Value (Hi);
8086 if Hi_Bound = No_Uint or else Hi_Bound > Hi_Val then
8087 Hi_Bound := Hi_Val;
8088 end if;
8089 end if;
8091 ST2 := Ancestor_Subtype (ST1);
8093 if No (ST2) then
8094 ST2 := Etype (ST1);
8095 end if;
8097 exit when ST1 = ST2;
8098 ST1 := ST2;
8099 end loop;
8100 end Extract_Subtype_Bounds;
8102 ----------------------------
8103 -- Simple_Init_Array_Type --
8104 ----------------------------
8106 function Simple_Init_Array_Type return Node_Id is
8107 Comp_Typ : constant Entity_Id := Component_Type (Typ);
8109 function Simple_Init_Dimension (Index : Node_Id) return Node_Id;
8110 -- Initialize a single array dimension with index constraint Index
8112 --------------------
8113 -- Simple_Init_Dimension --
8114 --------------------
8116 function Simple_Init_Dimension (Index : Node_Id) return Node_Id is
8117 begin
8118 -- Process the current dimension
8120 if Present (Index) then
8122 -- Build a suitable "others" aggregate for the next dimension,
8123 -- or initialize the component itself. Generate:
8125 -- (others => ...)
8127 return
8128 Make_Aggregate (Loc,
8129 Component_Associations => New_List (
8130 Make_Component_Association (Loc,
8131 Choices => New_List (Make_Others_Choice (Loc)),
8132 Expression =>
8133 Simple_Init_Dimension (Next_Index (Index)))));
8135 -- Otherwise all dimensions have been processed. Initialize the
8136 -- component itself.
8138 else
8139 return
8140 Get_Simple_Init_Val
8141 (Typ => Comp_Typ,
8142 N => N,
8143 Size => Esize (Comp_Typ));
8144 end if;
8145 end Simple_Init_Dimension;
8147 -- Start of processing for Simple_Init_Array_Type
8149 begin
8150 return Simple_Init_Dimension (First_Index (Typ));
8151 end Simple_Init_Array_Type;
8153 --------------------------------
8154 -- Simple_Init_Defaulted_Type --
8155 --------------------------------
8157 function Simple_Init_Defaulted_Type return Node_Id is
8158 Subtyp : constant Entity_Id := First_Subtype (Typ);
8160 begin
8161 -- Use the Sloc of the context node when constructing the initial
8162 -- value because the expression of Default_Value may come from a
8163 -- different unit. Updating the Sloc will result in accurate error
8164 -- diagnostics.
8166 -- When the first subtype is private, retrieve the expression of the
8167 -- Default_Value from the underlying type.
8169 if Is_Private_Type (Subtyp) then
8170 return
8171 Unchecked_Convert_To
8172 (Typ => Typ,
8173 Expr =>
8174 New_Copy_Tree
8175 (Source => Default_Aspect_Value (Full_View (Subtyp)),
8176 New_Sloc => Loc));
8178 else
8179 return
8180 Convert_To
8181 (Typ => Typ,
8182 Expr =>
8183 New_Copy_Tree
8184 (Source => Default_Aspect_Value (Subtyp),
8185 New_Sloc => Loc));
8186 end if;
8187 end Simple_Init_Defaulted_Type;
8189 -----------------------------------------
8190 -- Simple_Init_Initialize_Scalars_Type --
8191 -----------------------------------------
8193 function Simple_Init_Initialize_Scalars_Type
8194 (Size_To_Use : Uint) return Node_Id
8196 Float_Typ : Entity_Id;
8197 Hi_Bound : Uint;
8198 Lo_Bound : Uint;
8199 Scal_Typ : Scalar_Id;
8201 begin
8202 Extract_Subtype_Bounds (Lo_Bound, Hi_Bound);
8204 -- Float types
8206 if Is_Floating_Point_Type (Typ) then
8207 Float_Typ := Root_Type (Typ);
8209 if Float_Typ = Standard_Short_Float then
8210 Scal_Typ := Name_Short_Float;
8211 elsif Float_Typ = Standard_Float then
8212 Scal_Typ := Name_Float;
8213 elsif Float_Typ = Standard_Long_Float then
8214 Scal_Typ := Name_Long_Float;
8215 else pragma Assert (Float_Typ = Standard_Long_Long_Float);
8216 Scal_Typ := Name_Long_Long_Float;
8217 end if;
8219 -- If zero is invalid, it is a convenient value to use that is for
8220 -- sure an appropriate invalid value in all situations.
8222 elsif Lo_Bound /= No_Uint and then Lo_Bound > Uint_0 then
8223 return Make_Integer_Literal (Loc, 0);
8225 -- Unsigned types
8227 elsif Is_Unsigned_Type (Typ) then
8228 if Size_To_Use <= 8 then
8229 Scal_Typ := Name_Unsigned_8;
8230 elsif Size_To_Use <= 16 then
8231 Scal_Typ := Name_Unsigned_16;
8232 elsif Size_To_Use <= 32 then
8233 Scal_Typ := Name_Unsigned_32;
8234 else
8235 Scal_Typ := Name_Unsigned_64;
8236 end if;
8238 -- Signed types
8240 else
8241 if Size_To_Use <= 8 then
8242 Scal_Typ := Name_Signed_8;
8243 elsif Size_To_Use <= 16 then
8244 Scal_Typ := Name_Signed_16;
8245 elsif Size_To_Use <= 32 then
8246 Scal_Typ := Name_Signed_32;
8247 else
8248 Scal_Typ := Name_Signed_64;
8249 end if;
8250 end if;
8252 -- Use the values specified by pragma Initialize_Scalars or the ones
8253 -- provided by the binder. Higher precedence is given to the pragma.
8255 return Invalid_Scalar_Value (Loc, Scal_Typ);
8256 end Simple_Init_Initialize_Scalars_Type;
8258 ----------------------------------------
8259 -- Simple_Init_Normalize_Scalars_Type --
8260 ----------------------------------------
8262 function Simple_Init_Normalize_Scalars_Type
8263 (Size_To_Use : Uint) return Node_Id
8265 Signed_Size : constant Uint := UI_Min (Uint_63, Size_To_Use - 1);
8267 Expr : Node_Id;
8268 Hi_Bound : Uint;
8269 Lo_Bound : Uint;
8271 begin
8272 Extract_Subtype_Bounds (Lo_Bound, Hi_Bound);
8274 -- If zero is invalid, it is a convenient value to use that is for
8275 -- sure an appropriate invalid value in all situations.
8277 if Lo_Bound /= No_Uint and then Lo_Bound > Uint_0 then
8278 Expr := Make_Integer_Literal (Loc, 0);
8280 -- Cases where all one bits is the appropriate invalid value
8282 -- For modular types, all 1 bits is either invalid or valid. If it
8283 -- is valid, then there is nothing that can be done since there are
8284 -- no invalid values (we ruled out zero already).
8286 -- For signed integer types that have no negative values, either
8287 -- there is room for negative values, or there is not. If there
8288 -- is, then all 1-bits may be interpreted as minus one, which is
8289 -- certainly invalid. Alternatively it is treated as the largest
8290 -- positive value, in which case the observation for modular types
8291 -- still applies.
8293 -- For float types, all 1-bits is a NaN (not a number), which is
8294 -- certainly an appropriately invalid value.
8296 elsif Is_Enumeration_Type (Typ)
8297 or else Is_Floating_Point_Type (Typ)
8298 or else Is_Unsigned_Type (Typ)
8299 then
8300 Expr := Make_Integer_Literal (Loc, 2 ** Size_To_Use - 1);
8302 -- Resolve as Unsigned_64, because the largest number we can
8303 -- generate is out of range of universal integer.
8305 Analyze_And_Resolve (Expr, RTE (RE_Unsigned_64));
8307 -- Case of signed types
8309 else
8310 -- Normally we like to use the most negative number. The one
8311 -- exception is when this number is in the known subtype range and
8312 -- the largest positive number is not in the known subtype range.
8314 -- For this exceptional case, use largest positive value
8316 if Lo_Bound /= No_Uint and then Hi_Bound /= No_Uint
8317 and then Lo_Bound <= (-(2 ** Signed_Size))
8318 and then Hi_Bound < 2 ** Signed_Size
8319 then
8320 Expr := Make_Integer_Literal (Loc, 2 ** Signed_Size - 1);
8322 -- Normal case of largest negative value
8324 else
8325 Expr := Make_Integer_Literal (Loc, -(2 ** Signed_Size));
8326 end if;
8327 end if;
8329 return Expr;
8330 end Simple_Init_Normalize_Scalars_Type;
8332 ------------------------------
8333 -- Simple_Init_Private_Type --
8334 ------------------------------
8336 function Simple_Init_Private_Type return Node_Id is
8337 Under_Typ : constant Entity_Id := Underlying_Type (Typ);
8338 Expr : Node_Id;
8340 begin
8341 -- The availability of the underlying view must be checked by routine
8342 -- Needs_Simple_Initialization.
8344 pragma Assert (Present (Under_Typ));
8346 Expr := Get_Simple_Init_Val (Under_Typ, N, Size);
8348 -- If the initial value is null or an aggregate, qualify it with the
8349 -- underlying type in order to provide a proper context.
8351 if Nkind_In (Expr, N_Aggregate, N_Null) then
8352 Expr :=
8353 Make_Qualified_Expression (Loc,
8354 Subtype_Mark => New_Occurrence_Of (Under_Typ, Loc),
8355 Expression => Expr);
8356 end if;
8358 Expr := Unchecked_Convert_To (Typ, Expr);
8360 -- Do not truncate the result when scalar types are involved and
8361 -- Initialize/Normalize_Scalars is in effect.
8363 if Nkind (Expr) = N_Unchecked_Type_Conversion
8364 and then Is_Scalar_Type (Under_Typ)
8365 then
8366 Set_No_Truncation (Expr);
8367 end if;
8369 return Expr;
8370 end Simple_Init_Private_Type;
8372 -----------------------------
8373 -- Simple_Init_Scalar_Type --
8374 -----------------------------
8376 function Simple_Init_Scalar_Type return Node_Id is
8377 Expr : Node_Id;
8378 Size_To_Use : Uint;
8380 begin
8381 pragma Assert (Init_Or_Norm_Scalars or IV_Attribute);
8383 -- Determine the size of the object. This is either the size provided
8384 -- by the caller, or the Esize of the scalar type.
8386 if Size = No_Uint or else Size <= Uint_0 then
8387 Size_To_Use := UI_Max (Uint_1, Esize (Typ));
8388 else
8389 Size_To_Use := Size;
8390 end if;
8392 -- The maximum size to use is 64 bits. This will create values of
8393 -- type Unsigned_64 and the range must fit this type.
8395 if Size_To_Use /= No_Uint and then Size_To_Use > Uint_64 then
8396 Size_To_Use := Uint_64;
8397 end if;
8399 if Normalize_Scalars and then not IV_Attribute then
8400 Expr := Simple_Init_Normalize_Scalars_Type (Size_To_Use);
8401 else
8402 Expr := Simple_Init_Initialize_Scalars_Type (Size_To_Use);
8403 end if;
8405 -- The final expression is obtained by doing an unchecked conversion
8406 -- of this result to the base type of the required subtype. Use the
8407 -- base type to prevent the unchecked conversion from chopping bits,
8408 -- and then we set Kill_Range_Check to preserve the "bad" value.
8410 Expr := Unchecked_Convert_To (Base_Type (Typ), Expr);
8412 -- Ensure that the expression is not truncated since the "bad" bits
8413 -- are desired, and also kill the range checks.
8415 if Nkind (Expr) = N_Unchecked_Type_Conversion then
8416 Set_Kill_Range_Check (Expr);
8417 Set_No_Truncation (Expr);
8418 end if;
8420 return Expr;
8421 end Simple_Init_Scalar_Type;
8423 -- Start of processing for Get_Simple_Init_Val
8425 begin
8426 if Is_Private_Type (Typ) then
8427 return Simple_Init_Private_Type;
8429 elsif Is_Scalar_Type (Typ) then
8430 if Has_Default_Aspect (Typ) then
8431 return Simple_Init_Defaulted_Type;
8432 else
8433 return Simple_Init_Scalar_Type;
8434 end if;
8436 -- Array type with Initialize or Normalize_Scalars
8438 elsif Is_Array_Type (Typ) then
8439 pragma Assert (Init_Or_Norm_Scalars);
8440 return Simple_Init_Array_Type;
8442 -- Access type is initialized to null
8444 elsif Is_Access_Type (Typ) then
8445 return Make_Null (Loc);
8447 -- No other possibilities should arise, since we should only be calling
8448 -- Get_Simple_Init_Val if Needs_Simple_Initialization returned True,
8449 -- indicating one of the above cases held.
8451 else
8452 raise Program_Error;
8453 end if;
8455 exception
8456 when RE_Not_Available =>
8457 return Empty;
8458 end Get_Simple_Init_Val;
8460 ------------------------------
8461 -- Has_New_Non_Standard_Rep --
8462 ------------------------------
8464 function Has_New_Non_Standard_Rep (T : Entity_Id) return Boolean is
8465 begin
8466 if not Is_Derived_Type (T) then
8467 return Has_Non_Standard_Rep (T)
8468 or else Has_Non_Standard_Rep (Root_Type (T));
8470 -- If Has_Non_Standard_Rep is not set on the derived type, the
8471 -- representation is fully inherited.
8473 elsif not Has_Non_Standard_Rep (T) then
8474 return False;
8476 else
8477 return First_Rep_Item (T) /= First_Rep_Item (Root_Type (T));
8479 -- May need a more precise check here: the First_Rep_Item may be a
8480 -- stream attribute, which does not affect the representation of the
8481 -- type ???
8483 end if;
8484 end Has_New_Non_Standard_Rep;
8486 ----------------------
8487 -- Inline_Init_Proc --
8488 ----------------------
8490 function Inline_Init_Proc (Typ : Entity_Id) return Boolean is
8491 begin
8492 -- The initialization proc of protected records is not worth inlining.
8493 -- In addition, when compiled for another unit for inlining purposes,
8494 -- it may make reference to entities that have not been elaborated yet.
8495 -- The initialization proc of records that need finalization contains
8496 -- a nested clean-up procedure that makes it impractical to inline as
8497 -- well, except for simple controlled types themselves. And similar
8498 -- considerations apply to task types.
8500 if Is_Concurrent_Type (Typ) then
8501 return False;
8503 elsif Needs_Finalization (Typ) and then not Is_Controlled (Typ) then
8504 return False;
8506 elsif Has_Task (Typ) then
8507 return False;
8509 else
8510 return True;
8511 end if;
8512 end Inline_Init_Proc;
8514 ----------------
8515 -- In_Runtime --
8516 ----------------
8518 function In_Runtime (E : Entity_Id) return Boolean is
8519 S1 : Entity_Id;
8521 begin
8522 S1 := Scope (E);
8523 while Scope (S1) /= Standard_Standard loop
8524 S1 := Scope (S1);
8525 end loop;
8527 return Is_RTU (S1, System) or else Is_RTU (S1, Ada);
8528 end In_Runtime;
8530 ----------------------------
8531 -- Initialization_Warning --
8532 ----------------------------
8534 procedure Initialization_Warning (E : Entity_Id) is
8535 Warning_Needed : Boolean;
8537 begin
8538 Warning_Needed := False;
8540 if Ekind (Current_Scope) = E_Package
8541 and then Static_Elaboration_Desired (Current_Scope)
8542 then
8543 if Is_Type (E) then
8544 if Is_Record_Type (E) then
8545 if Has_Discriminants (E)
8546 or else Is_Limited_Type (E)
8547 or else Has_Non_Standard_Rep (E)
8548 then
8549 Warning_Needed := True;
8551 else
8552 -- Verify that at least one component has an initialization
8553 -- expression. No need for a warning on a type if all its
8554 -- components have no initialization.
8556 declare
8557 Comp : Entity_Id;
8559 begin
8560 Comp := First_Component (E);
8561 while Present (Comp) loop
8562 if Ekind (Comp) = E_Discriminant
8563 or else
8564 (Nkind (Parent (Comp)) = N_Component_Declaration
8565 and then Present (Expression (Parent (Comp))))
8566 then
8567 Warning_Needed := True;
8568 exit;
8569 end if;
8571 Next_Component (Comp);
8572 end loop;
8573 end;
8574 end if;
8576 if Warning_Needed then
8577 Error_Msg_N
8578 ("Objects of the type cannot be initialized statically "
8579 & "by default??", Parent (E));
8580 end if;
8581 end if;
8583 else
8584 Error_Msg_N ("Object cannot be initialized statically??", E);
8585 end if;
8586 end if;
8587 end Initialization_Warning;
8589 ------------------
8590 -- Init_Formals --
8591 ------------------
8593 function Init_Formals (Typ : Entity_Id) return List_Id is
8594 Loc : constant Source_Ptr := Sloc (Typ);
8595 Formals : List_Id;
8597 begin
8598 -- First parameter is always _Init : in out typ. Note that we need this
8599 -- to be in/out because in the case of the task record value, there
8600 -- are default record fields (_Priority, _Size, -Task_Info) that may
8601 -- be referenced in the generated initialization routine.
8603 Formals := New_List (
8604 Make_Parameter_Specification (Loc,
8605 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uInit),
8606 In_Present => True,
8607 Out_Present => True,
8608 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
8610 -- For task record value, or type that contains tasks, add two more
8611 -- formals, _Master : Master_Id and _Chain : in out Activation_Chain
8612 -- We also add these parameters for the task record type case.
8614 if Has_Task (Typ)
8615 or else (Is_Record_Type (Typ) and then Is_Task_Record_Type (Typ))
8616 then
8617 Append_To (Formals,
8618 Make_Parameter_Specification (Loc,
8619 Defining_Identifier =>
8620 Make_Defining_Identifier (Loc, Name_uMaster),
8621 Parameter_Type =>
8622 New_Occurrence_Of (RTE (RE_Master_Id), Loc)));
8624 -- Add _Chain (not done for sequential elaboration policy, see
8625 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
8627 if Partition_Elaboration_Policy /= 'S' then
8628 Append_To (Formals,
8629 Make_Parameter_Specification (Loc,
8630 Defining_Identifier =>
8631 Make_Defining_Identifier (Loc, Name_uChain),
8632 In_Present => True,
8633 Out_Present => True,
8634 Parameter_Type =>
8635 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc)));
8636 end if;
8638 Append_To (Formals,
8639 Make_Parameter_Specification (Loc,
8640 Defining_Identifier =>
8641 Make_Defining_Identifier (Loc, Name_uTask_Name),
8642 In_Present => True,
8643 Parameter_Type => New_Occurrence_Of (Standard_String, Loc)));
8644 end if;
8646 return Formals;
8648 exception
8649 when RE_Not_Available =>
8650 return Empty_List;
8651 end Init_Formals;
8653 -------------------------
8654 -- Init_Secondary_Tags --
8655 -------------------------
8657 procedure Init_Secondary_Tags
8658 (Typ : Entity_Id;
8659 Target : Node_Id;
8660 Init_Tags_List : List_Id;
8661 Stmts_List : List_Id;
8662 Fixed_Comps : Boolean := True;
8663 Variable_Comps : Boolean := True)
8665 Loc : constant Source_Ptr := Sloc (Target);
8667 -- Inherit the C++ tag of the secondary dispatch table of Typ associated
8668 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
8670 procedure Initialize_Tag
8671 (Typ : Entity_Id;
8672 Iface : Entity_Id;
8673 Tag_Comp : Entity_Id;
8674 Iface_Tag : Node_Id);
8675 -- Initialize the tag of the secondary dispatch table of Typ associated
8676 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
8677 -- Compiling under the CPP full ABI compatibility mode, if the ancestor
8678 -- of Typ CPP tagged type we generate code to inherit the contents of
8679 -- the dispatch table directly from the ancestor.
8681 --------------------
8682 -- Initialize_Tag --
8683 --------------------
8685 procedure Initialize_Tag
8686 (Typ : Entity_Id;
8687 Iface : Entity_Id;
8688 Tag_Comp : Entity_Id;
8689 Iface_Tag : Node_Id)
8691 Comp_Typ : Entity_Id;
8692 Offset_To_Top_Comp : Entity_Id := Empty;
8694 begin
8695 -- Initialize pointer to secondary DT associated with the interface
8697 if not Is_Ancestor (Iface, Typ, Use_Full_View => True) then
8698 Append_To (Init_Tags_List,
8699 Make_Assignment_Statement (Loc,
8700 Name =>
8701 Make_Selected_Component (Loc,
8702 Prefix => New_Copy_Tree (Target),
8703 Selector_Name => New_Occurrence_Of (Tag_Comp, Loc)),
8704 Expression =>
8705 New_Occurrence_Of (Iface_Tag, Loc)));
8706 end if;
8708 Comp_Typ := Scope (Tag_Comp);
8710 -- Initialize the entries of the table of interfaces. We generate a
8711 -- different call when the parent of the type has variable size
8712 -- components.
8714 if Comp_Typ /= Etype (Comp_Typ)
8715 and then Is_Variable_Size_Record (Etype (Comp_Typ))
8716 and then Chars (Tag_Comp) /= Name_uTag
8717 then
8718 pragma Assert (Present (DT_Offset_To_Top_Func (Tag_Comp)));
8720 -- Issue error if Set_Dynamic_Offset_To_Top is not available in a
8721 -- configurable run-time environment.
8723 if not RTE_Available (RE_Set_Dynamic_Offset_To_Top) then
8724 Error_Msg_CRT
8725 ("variable size record with interface types", Typ);
8726 return;
8727 end if;
8729 -- Generate:
8730 -- Set_Dynamic_Offset_To_Top
8731 -- (This => Init,
8732 -- Prim_T => Typ'Tag,
8733 -- Interface_T => Iface'Tag,
8734 -- Offset_Value => n,
8735 -- Offset_Func => Fn'Address)
8737 Append_To (Stmts_List,
8738 Make_Procedure_Call_Statement (Loc,
8739 Name =>
8740 New_Occurrence_Of (RTE (RE_Set_Dynamic_Offset_To_Top), Loc),
8741 Parameter_Associations => New_List (
8742 Make_Attribute_Reference (Loc,
8743 Prefix => New_Copy_Tree (Target),
8744 Attribute_Name => Name_Address),
8746 Unchecked_Convert_To (RTE (RE_Tag),
8747 New_Occurrence_Of
8748 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc)),
8750 Unchecked_Convert_To (RTE (RE_Tag),
8751 New_Occurrence_Of
8752 (Node (First_Elmt (Access_Disp_Table (Iface))),
8753 Loc)),
8755 Unchecked_Convert_To
8756 (RTE (RE_Storage_Offset),
8757 Make_Op_Minus (Loc,
8758 Make_Attribute_Reference (Loc,
8759 Prefix =>
8760 Make_Selected_Component (Loc,
8761 Prefix => New_Copy_Tree (Target),
8762 Selector_Name =>
8763 New_Occurrence_Of (Tag_Comp, Loc)),
8764 Attribute_Name => Name_Position))),
8766 Unchecked_Convert_To (RTE (RE_Offset_To_Top_Function_Ptr),
8767 Make_Attribute_Reference (Loc,
8768 Prefix => New_Occurrence_Of
8769 (DT_Offset_To_Top_Func (Tag_Comp), Loc),
8770 Attribute_Name => Name_Address)))));
8772 -- In this case the next component stores the value of the offset
8773 -- to the top.
8775 Offset_To_Top_Comp := Next_Entity (Tag_Comp);
8776 pragma Assert (Present (Offset_To_Top_Comp));
8778 Append_To (Init_Tags_List,
8779 Make_Assignment_Statement (Loc,
8780 Name =>
8781 Make_Selected_Component (Loc,
8782 Prefix => New_Copy_Tree (Target),
8783 Selector_Name =>
8784 New_Occurrence_Of (Offset_To_Top_Comp, Loc)),
8786 Expression =>
8787 Make_Op_Minus (Loc,
8788 Make_Attribute_Reference (Loc,
8789 Prefix =>
8790 Make_Selected_Component (Loc,
8791 Prefix => New_Copy_Tree (Target),
8792 Selector_Name => New_Occurrence_Of (Tag_Comp, Loc)),
8793 Attribute_Name => Name_Position))));
8795 -- Normal case: No discriminants in the parent type
8797 else
8798 -- Don't need to set any value if the offset-to-top field is
8799 -- statically set or if this interface shares the primary
8800 -- dispatch table.
8802 if not Building_Static_Secondary_DT (Typ)
8803 and then not Is_Ancestor (Iface, Typ, Use_Full_View => True)
8804 then
8805 Append_To (Stmts_List,
8806 Build_Set_Static_Offset_To_Top (Loc,
8807 Iface_Tag => New_Occurrence_Of (Iface_Tag, Loc),
8808 Offset_Value =>
8809 Unchecked_Convert_To (RTE (RE_Storage_Offset),
8810 Make_Op_Minus (Loc,
8811 Make_Attribute_Reference (Loc,
8812 Prefix =>
8813 Make_Selected_Component (Loc,
8814 Prefix => New_Copy_Tree (Target),
8815 Selector_Name =>
8816 New_Occurrence_Of (Tag_Comp, Loc)),
8817 Attribute_Name => Name_Position)))));
8818 end if;
8820 -- Generate:
8821 -- Register_Interface_Offset
8822 -- (Prim_T => Typ'Tag,
8823 -- Interface_T => Iface'Tag,
8824 -- Is_Constant => True,
8825 -- Offset_Value => n,
8826 -- Offset_Func => null);
8828 if not Building_Static_Secondary_DT (Typ)
8829 and then RTE_Available (RE_Register_Interface_Offset)
8830 then
8831 Append_To (Stmts_List,
8832 Make_Procedure_Call_Statement (Loc,
8833 Name =>
8834 New_Occurrence_Of
8835 (RTE (RE_Register_Interface_Offset), Loc),
8836 Parameter_Associations => New_List (
8837 Unchecked_Convert_To (RTE (RE_Tag),
8838 New_Occurrence_Of
8839 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc)),
8841 Unchecked_Convert_To (RTE (RE_Tag),
8842 New_Occurrence_Of
8843 (Node (First_Elmt (Access_Disp_Table (Iface))), Loc)),
8845 New_Occurrence_Of (Standard_True, Loc),
8847 Unchecked_Convert_To (RTE (RE_Storage_Offset),
8848 Make_Op_Minus (Loc,
8849 Make_Attribute_Reference (Loc,
8850 Prefix =>
8851 Make_Selected_Component (Loc,
8852 Prefix => New_Copy_Tree (Target),
8853 Selector_Name =>
8854 New_Occurrence_Of (Tag_Comp, Loc)),
8855 Attribute_Name => Name_Position))),
8857 Make_Null (Loc))));
8858 end if;
8859 end if;
8860 end Initialize_Tag;
8862 -- Local variables
8864 Full_Typ : Entity_Id;
8865 Ifaces_List : Elist_Id;
8866 Ifaces_Comp_List : Elist_Id;
8867 Ifaces_Tag_List : Elist_Id;
8868 Iface_Elmt : Elmt_Id;
8869 Iface_Comp_Elmt : Elmt_Id;
8870 Iface_Tag_Elmt : Elmt_Id;
8871 Tag_Comp : Node_Id;
8872 In_Variable_Pos : Boolean;
8874 -- Start of processing for Init_Secondary_Tags
8876 begin
8877 -- Handle private types
8879 if Present (Full_View (Typ)) then
8880 Full_Typ := Full_View (Typ);
8881 else
8882 Full_Typ := Typ;
8883 end if;
8885 Collect_Interfaces_Info
8886 (Full_Typ, Ifaces_List, Ifaces_Comp_List, Ifaces_Tag_List);
8888 Iface_Elmt := First_Elmt (Ifaces_List);
8889 Iface_Comp_Elmt := First_Elmt (Ifaces_Comp_List);
8890 Iface_Tag_Elmt := First_Elmt (Ifaces_Tag_List);
8891 while Present (Iface_Elmt) loop
8892 Tag_Comp := Node (Iface_Comp_Elmt);
8894 -- Check if parent of record type has variable size components
8896 In_Variable_Pos := Scope (Tag_Comp) /= Etype (Scope (Tag_Comp))
8897 and then Is_Variable_Size_Record (Etype (Scope (Tag_Comp)));
8899 -- If we are compiling under the CPP full ABI compatibility mode and
8900 -- the ancestor is a CPP_Pragma tagged type then we generate code to
8901 -- initialize the secondary tag components from tags that reference
8902 -- secondary tables filled with copy of parent slots.
8904 if Is_CPP_Class (Root_Type (Full_Typ)) then
8906 -- Reject interface components located at variable offset in
8907 -- C++ derivations. This is currently unsupported.
8909 if not Fixed_Comps and then In_Variable_Pos then
8911 -- Locate the first dynamic component of the record. Done to
8912 -- improve the text of the warning.
8914 declare
8915 Comp : Entity_Id;
8916 Comp_Typ : Entity_Id;
8918 begin
8919 Comp := First_Entity (Typ);
8920 while Present (Comp) loop
8921 Comp_Typ := Etype (Comp);
8923 if Ekind (Comp) /= E_Discriminant
8924 and then not Is_Tag (Comp)
8925 then
8926 exit when
8927 (Is_Record_Type (Comp_Typ)
8928 and then
8929 Is_Variable_Size_Record (Base_Type (Comp_Typ)))
8930 or else
8931 (Is_Array_Type (Comp_Typ)
8932 and then Is_Variable_Size_Array (Comp_Typ));
8933 end if;
8935 Next_Entity (Comp);
8936 end loop;
8938 pragma Assert (Present (Comp));
8939 Error_Msg_Node_2 := Comp;
8940 Error_Msg_NE
8941 ("parent type & with dynamic component & cannot be parent"
8942 & " of 'C'P'P derivation if new interfaces are present",
8943 Typ, Scope (Original_Record_Component (Comp)));
8945 Error_Msg_Sloc :=
8946 Sloc (Scope (Original_Record_Component (Comp)));
8947 Error_Msg_NE
8948 ("type derived from 'C'P'P type & defined #",
8949 Typ, Scope (Original_Record_Component (Comp)));
8951 -- Avoid duplicated warnings
8953 exit;
8954 end;
8956 -- Initialize secondary tags
8958 else
8959 Initialize_Tag
8960 (Typ => Full_Typ,
8961 Iface => Node (Iface_Elmt),
8962 Tag_Comp => Tag_Comp,
8963 Iface_Tag => Node (Iface_Tag_Elmt));
8964 end if;
8966 -- Otherwise generate code to initialize the tag
8968 else
8969 if (In_Variable_Pos and then Variable_Comps)
8970 or else (not In_Variable_Pos and then Fixed_Comps)
8971 then
8972 Initialize_Tag
8973 (Typ => Full_Typ,
8974 Iface => Node (Iface_Elmt),
8975 Tag_Comp => Tag_Comp,
8976 Iface_Tag => Node (Iface_Tag_Elmt));
8977 end if;
8978 end if;
8980 Next_Elmt (Iface_Elmt);
8981 Next_Elmt (Iface_Comp_Elmt);
8982 Next_Elmt (Iface_Tag_Elmt);
8983 end loop;
8984 end Init_Secondary_Tags;
8986 ------------------------------
8987 -- Is_User_Defined_Equality --
8988 ------------------------------
8990 function Is_User_Defined_Equality (Prim : Node_Id) return Boolean is
8991 begin
8992 return Chars (Prim) = Name_Op_Eq
8993 and then Etype (First_Formal (Prim)) =
8994 Etype (Next_Formal (First_Formal (Prim)))
8995 and then Base_Type (Etype (Prim)) = Standard_Boolean;
8996 end Is_User_Defined_Equality;
8998 ----------------------------------------
8999 -- Make_Controlling_Function_Wrappers --
9000 ----------------------------------------
9002 procedure Make_Controlling_Function_Wrappers
9003 (Tag_Typ : Entity_Id;
9004 Decl_List : out List_Id;
9005 Body_List : out List_Id)
9007 Loc : constant Source_Ptr := Sloc (Tag_Typ);
9008 Prim_Elmt : Elmt_Id;
9009 Subp : Entity_Id;
9010 Actual_List : List_Id;
9011 Formal_List : List_Id;
9012 Formal : Entity_Id;
9013 Par_Formal : Entity_Id;
9014 Formal_Node : Node_Id;
9015 Func_Body : Node_Id;
9016 Func_Decl : Node_Id;
9017 Func_Spec : Node_Id;
9018 Return_Stmt : Node_Id;
9020 begin
9021 Decl_List := New_List;
9022 Body_List := New_List;
9024 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
9025 while Present (Prim_Elmt) loop
9026 Subp := Node (Prim_Elmt);
9028 -- If a primitive function with a controlling result of the type has
9029 -- not been overridden by the user, then we must create a wrapper
9030 -- function here that effectively overrides it and invokes the
9031 -- (non-abstract) parent function. This can only occur for a null
9032 -- extension. Note that functions with anonymous controlling access
9033 -- results don't qualify and must be overridden. We also exclude
9034 -- Input attributes, since each type will have its own version of
9035 -- Input constructed by the expander. The test for Comes_From_Source
9036 -- is needed to distinguish inherited operations from renamings
9037 -- (which also have Alias set). We exclude internal entities with
9038 -- Interface_Alias to avoid generating duplicated wrappers since
9039 -- the primitive which covers the interface is also available in
9040 -- the list of primitive operations.
9042 -- The function may be abstract, or require_Overriding may be set
9043 -- for it, because tests for null extensions may already have reset
9044 -- the Is_Abstract_Subprogram_Flag. If Requires_Overriding is not
9045 -- set, functions that need wrappers are recognized by having an
9046 -- alias that returns the parent type.
9048 if Comes_From_Source (Subp)
9049 or else No (Alias (Subp))
9050 or else Present (Interface_Alias (Subp))
9051 or else Ekind (Subp) /= E_Function
9052 or else not Has_Controlling_Result (Subp)
9053 or else Is_Access_Type (Etype (Subp))
9054 or else Is_Abstract_Subprogram (Alias (Subp))
9055 or else Is_TSS (Subp, TSS_Stream_Input)
9056 then
9057 goto Next_Prim;
9059 elsif Is_Abstract_Subprogram (Subp)
9060 or else Requires_Overriding (Subp)
9061 or else
9062 (Is_Null_Extension (Etype (Subp))
9063 and then Etype (Alias (Subp)) /= Etype (Subp))
9064 then
9065 Formal_List := No_List;
9066 Formal := First_Formal (Subp);
9068 if Present (Formal) then
9069 Formal_List := New_List;
9071 while Present (Formal) loop
9072 Append
9073 (Make_Parameter_Specification
9074 (Loc,
9075 Defining_Identifier =>
9076 Make_Defining_Identifier (Sloc (Formal),
9077 Chars => Chars (Formal)),
9078 In_Present => In_Present (Parent (Formal)),
9079 Out_Present => Out_Present (Parent (Formal)),
9080 Null_Exclusion_Present =>
9081 Null_Exclusion_Present (Parent (Formal)),
9082 Parameter_Type =>
9083 New_Occurrence_Of (Etype (Formal), Loc),
9084 Expression =>
9085 New_Copy_Tree (Expression (Parent (Formal)))),
9086 Formal_List);
9088 Next_Formal (Formal);
9089 end loop;
9090 end if;
9092 Func_Spec :=
9093 Make_Function_Specification (Loc,
9094 Defining_Unit_Name =>
9095 Make_Defining_Identifier (Loc,
9096 Chars => Chars (Subp)),
9097 Parameter_Specifications => Formal_List,
9098 Result_Definition =>
9099 New_Occurrence_Of (Etype (Subp), Loc));
9101 Func_Decl := Make_Subprogram_Declaration (Loc, Func_Spec);
9102 Append_To (Decl_List, Func_Decl);
9104 -- Build a wrapper body that calls the parent function. The body
9105 -- contains a single return statement that returns an extension
9106 -- aggregate whose ancestor part is a call to the parent function,
9107 -- passing the formals as actuals (with any controlling arguments
9108 -- converted to the types of the corresponding formals of the
9109 -- parent function, which might be anonymous access types), and
9110 -- having a null extension.
9112 Formal := First_Formal (Subp);
9113 Par_Formal := First_Formal (Alias (Subp));
9114 Formal_Node := First (Formal_List);
9116 if Present (Formal) then
9117 Actual_List := New_List;
9118 else
9119 Actual_List := No_List;
9120 end if;
9122 while Present (Formal) loop
9123 if Is_Controlling_Formal (Formal) then
9124 Append_To (Actual_List,
9125 Make_Type_Conversion (Loc,
9126 Subtype_Mark =>
9127 New_Occurrence_Of (Etype (Par_Formal), Loc),
9128 Expression =>
9129 New_Occurrence_Of
9130 (Defining_Identifier (Formal_Node), Loc)));
9131 else
9132 Append_To
9133 (Actual_List,
9134 New_Occurrence_Of
9135 (Defining_Identifier (Formal_Node), Loc));
9136 end if;
9138 Next_Formal (Formal);
9139 Next_Formal (Par_Formal);
9140 Next (Formal_Node);
9141 end loop;
9143 Return_Stmt :=
9144 Make_Simple_Return_Statement (Loc,
9145 Expression =>
9146 Make_Extension_Aggregate (Loc,
9147 Ancestor_Part =>
9148 Make_Function_Call (Loc,
9149 Name =>
9150 New_Occurrence_Of (Alias (Subp), Loc),
9151 Parameter_Associations => Actual_List),
9152 Null_Record_Present => True));
9154 Func_Body :=
9155 Make_Subprogram_Body (Loc,
9156 Specification => New_Copy_Tree (Func_Spec),
9157 Declarations => Empty_List,
9158 Handled_Statement_Sequence =>
9159 Make_Handled_Sequence_Of_Statements (Loc,
9160 Statements => New_List (Return_Stmt)));
9162 Set_Defining_Unit_Name
9163 (Specification (Func_Body),
9164 Make_Defining_Identifier (Loc, Chars (Subp)));
9166 Append_To (Body_List, Func_Body);
9168 -- Replace the inherited function with the wrapper function in the
9169 -- primitive operations list. We add the minimum decoration needed
9170 -- to override interface primitives.
9172 Set_Ekind (Defining_Unit_Name (Func_Spec), E_Function);
9174 Override_Dispatching_Operation
9175 (Tag_Typ, Subp, New_Op => Defining_Unit_Name (Func_Spec),
9176 Is_Wrapper => True);
9177 end if;
9179 <<Next_Prim>>
9180 Next_Elmt (Prim_Elmt);
9181 end loop;
9182 end Make_Controlling_Function_Wrappers;
9184 -------------------
9185 -- Make_Eq_Body --
9186 -------------------
9188 function Make_Eq_Body
9189 (Typ : Entity_Id;
9190 Eq_Name : Name_Id) return Node_Id
9192 Loc : constant Source_Ptr := Sloc (Parent (Typ));
9193 Decl : Node_Id;
9194 Def : constant Node_Id := Parent (Typ);
9195 Stmts : constant List_Id := New_List;
9196 Variant_Case : Boolean := Has_Discriminants (Typ);
9197 Comps : Node_Id := Empty;
9198 Typ_Def : Node_Id := Type_Definition (Def);
9200 begin
9201 Decl :=
9202 Predef_Spec_Or_Body (Loc,
9203 Tag_Typ => Typ,
9204 Name => Eq_Name,
9205 Profile => New_List (
9206 Make_Parameter_Specification (Loc,
9207 Defining_Identifier =>
9208 Make_Defining_Identifier (Loc, Name_X),
9209 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
9211 Make_Parameter_Specification (Loc,
9212 Defining_Identifier =>
9213 Make_Defining_Identifier (Loc, Name_Y),
9214 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
9216 Ret_Type => Standard_Boolean,
9217 For_Body => True);
9219 if Variant_Case then
9220 if Nkind (Typ_Def) = N_Derived_Type_Definition then
9221 Typ_Def := Record_Extension_Part (Typ_Def);
9222 end if;
9224 if Present (Typ_Def) then
9225 Comps := Component_List (Typ_Def);
9226 end if;
9228 Variant_Case :=
9229 Present (Comps) and then Present (Variant_Part (Comps));
9230 end if;
9232 if Variant_Case then
9233 Append_To (Stmts,
9234 Make_Eq_If (Typ, Discriminant_Specifications (Def)));
9235 Append_List_To (Stmts, Make_Eq_Case (Typ, Comps));
9236 Append_To (Stmts,
9237 Make_Simple_Return_Statement (Loc,
9238 Expression => New_Occurrence_Of (Standard_True, Loc)));
9240 else
9241 Append_To (Stmts,
9242 Make_Simple_Return_Statement (Loc,
9243 Expression =>
9244 Expand_Record_Equality
9245 (Typ,
9246 Typ => Typ,
9247 Lhs => Make_Identifier (Loc, Name_X),
9248 Rhs => Make_Identifier (Loc, Name_Y),
9249 Bodies => Declarations (Decl))));
9250 end if;
9252 Set_Handled_Statement_Sequence
9253 (Decl, Make_Handled_Sequence_Of_Statements (Loc, Stmts));
9254 return Decl;
9255 end Make_Eq_Body;
9257 ------------------
9258 -- Make_Eq_Case --
9259 ------------------
9261 -- <Make_Eq_If shared components>
9263 -- case X.D1 is
9264 -- when V1 => <Make_Eq_Case> on subcomponents
9265 -- ...
9266 -- when Vn => <Make_Eq_Case> on subcomponents
9267 -- end case;
9269 function Make_Eq_Case
9270 (E : Entity_Id;
9271 CL : Node_Id;
9272 Discrs : Elist_Id := New_Elmt_List) return List_Id
9274 Loc : constant Source_Ptr := Sloc (E);
9275 Result : constant List_Id := New_List;
9276 Variant : Node_Id;
9277 Alt_List : List_Id;
9279 function Corresponding_Formal (C : Node_Id) return Entity_Id;
9280 -- Given the discriminant that controls a given variant of an unchecked
9281 -- union, find the formal of the equality function that carries the
9282 -- inferred value of the discriminant.
9284 function External_Name (E : Entity_Id) return Name_Id;
9285 -- The value of a given discriminant is conveyed in the corresponding
9286 -- formal parameter of the equality routine. The name of this formal
9287 -- parameter carries a one-character suffix which is removed here.
9289 --------------------------
9290 -- Corresponding_Formal --
9291 --------------------------
9293 function Corresponding_Formal (C : Node_Id) return Entity_Id is
9294 Discr : constant Entity_Id := Entity (Name (Variant_Part (C)));
9295 Elm : Elmt_Id;
9297 begin
9298 Elm := First_Elmt (Discrs);
9299 while Present (Elm) loop
9300 if Chars (Discr) = External_Name (Node (Elm)) then
9301 return Node (Elm);
9302 end if;
9304 Next_Elmt (Elm);
9305 end loop;
9307 -- A formal of the proper name must be found
9309 raise Program_Error;
9310 end Corresponding_Formal;
9312 -------------------
9313 -- External_Name --
9314 -------------------
9316 function External_Name (E : Entity_Id) return Name_Id is
9317 begin
9318 Get_Name_String (Chars (E));
9319 Name_Len := Name_Len - 1;
9320 return Name_Find;
9321 end External_Name;
9323 -- Start of processing for Make_Eq_Case
9325 begin
9326 Append_To (Result, Make_Eq_If (E, Component_Items (CL)));
9328 if No (Variant_Part (CL)) then
9329 return Result;
9330 end if;
9332 Variant := First_Non_Pragma (Variants (Variant_Part (CL)));
9334 if No (Variant) then
9335 return Result;
9336 end if;
9338 Alt_List := New_List;
9339 while Present (Variant) loop
9340 Append_To (Alt_List,
9341 Make_Case_Statement_Alternative (Loc,
9342 Discrete_Choices => New_Copy_List (Discrete_Choices (Variant)),
9343 Statements =>
9344 Make_Eq_Case (E, Component_List (Variant), Discrs)));
9345 Next_Non_Pragma (Variant);
9346 end loop;
9348 -- If we have an Unchecked_Union, use one of the parameters of the
9349 -- enclosing equality routine that captures the discriminant, to use
9350 -- as the expression in the generated case statement.
9352 if Is_Unchecked_Union (E) then
9353 Append_To (Result,
9354 Make_Case_Statement (Loc,
9355 Expression =>
9356 New_Occurrence_Of (Corresponding_Formal (CL), Loc),
9357 Alternatives => Alt_List));
9359 else
9360 Append_To (Result,
9361 Make_Case_Statement (Loc,
9362 Expression =>
9363 Make_Selected_Component (Loc,
9364 Prefix => Make_Identifier (Loc, Name_X),
9365 Selector_Name => New_Copy (Name (Variant_Part (CL)))),
9366 Alternatives => Alt_List));
9367 end if;
9369 return Result;
9370 end Make_Eq_Case;
9372 ----------------
9373 -- Make_Eq_If --
9374 ----------------
9376 -- Generates:
9378 -- if
9379 -- X.C1 /= Y.C1
9380 -- or else
9381 -- X.C2 /= Y.C2
9382 -- ...
9383 -- then
9384 -- return False;
9385 -- end if;
9387 -- or a null statement if the list L is empty
9389 function Make_Eq_If
9390 (E : Entity_Id;
9391 L : List_Id) return Node_Id
9393 Loc : constant Source_Ptr := Sloc (E);
9394 C : Node_Id;
9395 Field_Name : Name_Id;
9396 Cond : Node_Id;
9398 begin
9399 if No (L) then
9400 return Make_Null_Statement (Loc);
9402 else
9403 Cond := Empty;
9405 C := First_Non_Pragma (L);
9406 while Present (C) loop
9407 Field_Name := Chars (Defining_Identifier (C));
9409 -- The tags must not be compared: they are not part of the value.
9410 -- Ditto for parent interfaces because their equality operator is
9411 -- abstract.
9413 -- Note also that in the following, we use Make_Identifier for
9414 -- the component names. Use of New_Occurrence_Of to identify the
9415 -- components would be incorrect because the wrong entities for
9416 -- discriminants could be picked up in the private type case.
9418 if Field_Name = Name_uParent
9419 and then Is_Interface (Etype (Defining_Identifier (C)))
9420 then
9421 null;
9423 elsif Field_Name /= Name_uTag then
9424 Evolve_Or_Else (Cond,
9425 Make_Op_Ne (Loc,
9426 Left_Opnd =>
9427 Make_Selected_Component (Loc,
9428 Prefix => Make_Identifier (Loc, Name_X),
9429 Selector_Name => Make_Identifier (Loc, Field_Name)),
9431 Right_Opnd =>
9432 Make_Selected_Component (Loc,
9433 Prefix => Make_Identifier (Loc, Name_Y),
9434 Selector_Name => Make_Identifier (Loc, Field_Name))));
9435 end if;
9437 Next_Non_Pragma (C);
9438 end loop;
9440 if No (Cond) then
9441 return Make_Null_Statement (Loc);
9443 else
9444 return
9445 Make_Implicit_If_Statement (E,
9446 Condition => Cond,
9447 Then_Statements => New_List (
9448 Make_Simple_Return_Statement (Loc,
9449 Expression => New_Occurrence_Of (Standard_False, Loc))));
9450 end if;
9451 end if;
9452 end Make_Eq_If;
9454 -------------------
9455 -- Make_Neq_Body --
9456 -------------------
9458 function Make_Neq_Body (Tag_Typ : Entity_Id) return Node_Id is
9460 function Is_Predefined_Neq_Renaming (Prim : Node_Id) return Boolean;
9461 -- Returns true if Prim is a renaming of an unresolved predefined
9462 -- inequality operation.
9464 --------------------------------
9465 -- Is_Predefined_Neq_Renaming --
9466 --------------------------------
9468 function Is_Predefined_Neq_Renaming (Prim : Node_Id) return Boolean is
9469 begin
9470 return Chars (Prim) /= Name_Op_Ne
9471 and then Present (Alias (Prim))
9472 and then Comes_From_Source (Prim)
9473 and then Is_Intrinsic_Subprogram (Alias (Prim))
9474 and then Chars (Alias (Prim)) = Name_Op_Ne;
9475 end Is_Predefined_Neq_Renaming;
9477 -- Local variables
9479 Loc : constant Source_Ptr := Sloc (Parent (Tag_Typ));
9480 Stmts : constant List_Id := New_List;
9481 Decl : Node_Id;
9482 Eq_Prim : Entity_Id;
9483 Left_Op : Entity_Id;
9484 Renaming_Prim : Entity_Id;
9485 Right_Op : Entity_Id;
9486 Target : Entity_Id;
9488 -- Start of processing for Make_Neq_Body
9490 begin
9491 -- For a call on a renaming of a dispatching subprogram that is
9492 -- overridden, if the overriding occurred before the renaming, then
9493 -- the body executed is that of the overriding declaration, even if the
9494 -- overriding declaration is not visible at the place of the renaming;
9495 -- otherwise, the inherited or predefined subprogram is called, see
9496 -- (RM 8.5.4(8))
9498 -- Stage 1: Search for a renaming of the inequality primitive and also
9499 -- search for an overriding of the equality primitive located before the
9500 -- renaming declaration.
9502 declare
9503 Elmt : Elmt_Id;
9504 Prim : Node_Id;
9506 begin
9507 Eq_Prim := Empty;
9508 Renaming_Prim := Empty;
9510 Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
9511 while Present (Elmt) loop
9512 Prim := Node (Elmt);
9514 if Is_User_Defined_Equality (Prim) and then No (Alias (Prim)) then
9515 if No (Renaming_Prim) then
9516 pragma Assert (No (Eq_Prim));
9517 Eq_Prim := Prim;
9518 end if;
9520 elsif Is_Predefined_Neq_Renaming (Prim) then
9521 Renaming_Prim := Prim;
9522 end if;
9524 Next_Elmt (Elmt);
9525 end loop;
9526 end;
9528 -- No further action needed if no renaming was found
9530 if No (Renaming_Prim) then
9531 return Empty;
9532 end if;
9534 -- Stage 2: Replace the renaming declaration by a subprogram declaration
9535 -- (required to add its body)
9537 Decl := Parent (Parent (Renaming_Prim));
9538 Rewrite (Decl,
9539 Make_Subprogram_Declaration (Loc,
9540 Specification => Specification (Decl)));
9541 Set_Analyzed (Decl);
9543 -- Remove the decoration of intrinsic renaming subprogram
9545 Set_Is_Intrinsic_Subprogram (Renaming_Prim, False);
9546 Set_Convention (Renaming_Prim, Convention_Ada);
9547 Set_Alias (Renaming_Prim, Empty);
9548 Set_Has_Completion (Renaming_Prim, False);
9550 -- Stage 3: Build the corresponding body
9552 Left_Op := First_Formal (Renaming_Prim);
9553 Right_Op := Next_Formal (Left_Op);
9555 Decl :=
9556 Predef_Spec_Or_Body (Loc,
9557 Tag_Typ => Tag_Typ,
9558 Name => Chars (Renaming_Prim),
9559 Profile => New_List (
9560 Make_Parameter_Specification (Loc,
9561 Defining_Identifier =>
9562 Make_Defining_Identifier (Loc, Chars (Left_Op)),
9563 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
9565 Make_Parameter_Specification (Loc,
9566 Defining_Identifier =>
9567 Make_Defining_Identifier (Loc, Chars (Right_Op)),
9568 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
9570 Ret_Type => Standard_Boolean,
9571 For_Body => True);
9573 -- If the overriding of the equality primitive occurred before the
9574 -- renaming, then generate:
9576 -- function <Neq_Name> (X : Y : Typ) return Boolean is
9577 -- begin
9578 -- return not Oeq (X, Y);
9579 -- end;
9581 if Present (Eq_Prim) then
9582 Target := Eq_Prim;
9584 -- Otherwise build a nested subprogram which performs the predefined
9585 -- evaluation of the equality operator. That is, generate:
9587 -- function <Neq_Name> (X : Y : Typ) return Boolean is
9588 -- function Oeq (X : Y) return Boolean is
9589 -- begin
9590 -- <<body of default implementation>>
9591 -- end;
9592 -- begin
9593 -- return not Oeq (X, Y);
9594 -- end;
9596 else
9597 declare
9598 Local_Subp : Node_Id;
9599 begin
9600 Local_Subp := Make_Eq_Body (Tag_Typ, Name_Op_Eq);
9601 Set_Declarations (Decl, New_List (Local_Subp));
9602 Target := Defining_Entity (Local_Subp);
9603 end;
9604 end if;
9606 Append_To (Stmts,
9607 Make_Simple_Return_Statement (Loc,
9608 Expression =>
9609 Make_Op_Not (Loc,
9610 Make_Function_Call (Loc,
9611 Name => New_Occurrence_Of (Target, Loc),
9612 Parameter_Associations => New_List (
9613 Make_Identifier (Loc, Chars (Left_Op)),
9614 Make_Identifier (Loc, Chars (Right_Op)))))));
9616 Set_Handled_Statement_Sequence
9617 (Decl, Make_Handled_Sequence_Of_Statements (Loc, Stmts));
9618 return Decl;
9619 end Make_Neq_Body;
9621 -------------------------------
9622 -- Make_Null_Procedure_Specs --
9623 -------------------------------
9625 function Make_Null_Procedure_Specs (Tag_Typ : Entity_Id) return List_Id is
9626 Decl_List : constant List_Id := New_List;
9627 Loc : constant Source_Ptr := Sloc (Tag_Typ);
9628 Formal : Entity_Id;
9629 Formal_List : List_Id;
9630 New_Param_Spec : Node_Id;
9631 Parent_Subp : Entity_Id;
9632 Prim_Elmt : Elmt_Id;
9633 Subp : Entity_Id;
9635 begin
9636 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
9637 while Present (Prim_Elmt) loop
9638 Subp := Node (Prim_Elmt);
9640 -- If a null procedure inherited from an interface has not been
9641 -- overridden, then we build a null procedure declaration to
9642 -- override the inherited procedure.
9644 Parent_Subp := Alias (Subp);
9646 if Present (Parent_Subp)
9647 and then Is_Null_Interface_Primitive (Parent_Subp)
9648 then
9649 Formal_List := No_List;
9650 Formal := First_Formal (Subp);
9652 if Present (Formal) then
9653 Formal_List := New_List;
9655 while Present (Formal) loop
9657 -- Copy the parameter spec including default expressions
9659 New_Param_Spec :=
9660 New_Copy_Tree (Parent (Formal), New_Sloc => Loc);
9662 -- Generate a new defining identifier for the new formal.
9663 -- required because New_Copy_Tree does not duplicate
9664 -- semantic fields (except itypes).
9666 Set_Defining_Identifier (New_Param_Spec,
9667 Make_Defining_Identifier (Sloc (Formal),
9668 Chars => Chars (Formal)));
9670 -- For controlling arguments we must change their
9671 -- parameter type to reference the tagged type (instead
9672 -- of the interface type)
9674 if Is_Controlling_Formal (Formal) then
9675 if Nkind (Parameter_Type (Parent (Formal))) = N_Identifier
9676 then
9677 Set_Parameter_Type (New_Param_Spec,
9678 New_Occurrence_Of (Tag_Typ, Loc));
9680 else pragma Assert
9681 (Nkind (Parameter_Type (Parent (Formal))) =
9682 N_Access_Definition);
9683 Set_Subtype_Mark (Parameter_Type (New_Param_Spec),
9684 New_Occurrence_Of (Tag_Typ, Loc));
9685 end if;
9686 end if;
9688 Append (New_Param_Spec, Formal_List);
9690 Next_Formal (Formal);
9691 end loop;
9692 end if;
9694 Append_To (Decl_List,
9695 Make_Subprogram_Declaration (Loc,
9696 Make_Procedure_Specification (Loc,
9697 Defining_Unit_Name =>
9698 Make_Defining_Identifier (Loc, Chars (Subp)),
9699 Parameter_Specifications => Formal_List,
9700 Null_Present => True)));
9701 end if;
9703 Next_Elmt (Prim_Elmt);
9704 end loop;
9706 return Decl_List;
9707 end Make_Null_Procedure_Specs;
9709 -------------------------------------
9710 -- Make_Predefined_Primitive_Specs --
9711 -------------------------------------
9713 procedure Make_Predefined_Primitive_Specs
9714 (Tag_Typ : Entity_Id;
9715 Predef_List : out List_Id;
9716 Renamed_Eq : out Entity_Id)
9718 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean;
9719 -- Returns true if Prim is a renaming of an unresolved predefined
9720 -- equality operation.
9722 -------------------------------
9723 -- Is_Predefined_Eq_Renaming --
9724 -------------------------------
9726 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean is
9727 begin
9728 return Chars (Prim) /= Name_Op_Eq
9729 and then Present (Alias (Prim))
9730 and then Comes_From_Source (Prim)
9731 and then Is_Intrinsic_Subprogram (Alias (Prim))
9732 and then Chars (Alias (Prim)) = Name_Op_Eq;
9733 end Is_Predefined_Eq_Renaming;
9735 -- Local variables
9737 Loc : constant Source_Ptr := Sloc (Tag_Typ);
9738 Res : constant List_Id := New_List;
9739 Eq_Name : Name_Id := Name_Op_Eq;
9740 Eq_Needed : Boolean;
9741 Eq_Spec : Node_Id;
9742 Prim : Elmt_Id;
9744 Has_Predef_Eq_Renaming : Boolean := False;
9745 -- Set to True if Tag_Typ has a primitive that renames the predefined
9746 -- equality operator. Used to implement (RM 8-5-4(8)).
9748 -- Start of processing for Make_Predefined_Primitive_Specs
9750 begin
9751 Renamed_Eq := Empty;
9753 -- Spec of _Size
9755 Append_To (Res, Predef_Spec_Or_Body (Loc,
9756 Tag_Typ => Tag_Typ,
9757 Name => Name_uSize,
9758 Profile => New_List (
9759 Make_Parameter_Specification (Loc,
9760 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
9761 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
9763 Ret_Type => Standard_Long_Long_Integer));
9765 -- Specs for dispatching stream attributes
9767 declare
9768 Stream_Op_TSS_Names :
9769 constant array (Positive range <>) of TSS_Name_Type :=
9770 (TSS_Stream_Read,
9771 TSS_Stream_Write,
9772 TSS_Stream_Input,
9773 TSS_Stream_Output);
9775 begin
9776 for Op in Stream_Op_TSS_Names'Range loop
9777 if Stream_Operation_OK (Tag_Typ, Stream_Op_TSS_Names (Op)) then
9778 Append_To (Res,
9779 Predef_Stream_Attr_Spec (Loc, Tag_Typ,
9780 Stream_Op_TSS_Names (Op)));
9781 end if;
9782 end loop;
9783 end;
9785 -- Spec of "=" is expanded if the type is not limited and if a user
9786 -- defined "=" was not already declared for the non-full view of a
9787 -- private extension
9789 if not Is_Limited_Type (Tag_Typ) then
9790 Eq_Needed := True;
9791 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
9792 while Present (Prim) loop
9794 -- If a primitive is encountered that renames the predefined
9795 -- equality operator before reaching any explicit equality
9796 -- primitive, then we still need to create a predefined equality
9797 -- function, because calls to it can occur via the renaming. A
9798 -- new name is created for the equality to avoid conflicting with
9799 -- any user-defined equality. (Note that this doesn't account for
9800 -- renamings of equality nested within subpackages???)
9802 if Is_Predefined_Eq_Renaming (Node (Prim)) then
9803 Has_Predef_Eq_Renaming := True;
9804 Eq_Name := New_External_Name (Chars (Node (Prim)), 'E');
9806 -- User-defined equality
9808 elsif Is_User_Defined_Equality (Node (Prim)) then
9809 if No (Alias (Node (Prim)))
9810 or else Nkind (Unit_Declaration_Node (Node (Prim))) =
9811 N_Subprogram_Renaming_Declaration
9812 then
9813 Eq_Needed := False;
9814 exit;
9816 -- If the parent is not an interface type and has an abstract
9817 -- equality function explicitly defined in the sources, then
9818 -- the inherited equality is abstract as well, and no body can
9819 -- be created for it.
9821 elsif not Is_Interface (Etype (Tag_Typ))
9822 and then Present (Alias (Node (Prim)))
9823 and then Comes_From_Source (Alias (Node (Prim)))
9824 and then Is_Abstract_Subprogram (Alias (Node (Prim)))
9825 then
9826 Eq_Needed := False;
9827 exit;
9829 -- If the type has an equality function corresponding with
9830 -- a primitive defined in an interface type, the inherited
9831 -- equality is abstract as well, and no body can be created
9832 -- for it.
9834 elsif Present (Alias (Node (Prim)))
9835 and then Comes_From_Source (Ultimate_Alias (Node (Prim)))
9836 and then
9837 Is_Interface
9838 (Find_Dispatching_Type (Ultimate_Alias (Node (Prim))))
9839 then
9840 Eq_Needed := False;
9841 exit;
9842 end if;
9843 end if;
9845 Next_Elmt (Prim);
9846 end loop;
9848 -- If a renaming of predefined equality was found but there was no
9849 -- user-defined equality (so Eq_Needed is still true), then set the
9850 -- name back to Name_Op_Eq. But in the case where a user-defined
9851 -- equality was located after such a renaming, then the predefined
9852 -- equality function is still needed, so Eq_Needed must be set back
9853 -- to True.
9855 if Eq_Name /= Name_Op_Eq then
9856 if Eq_Needed then
9857 Eq_Name := Name_Op_Eq;
9858 else
9859 Eq_Needed := True;
9860 end if;
9861 end if;
9863 if Eq_Needed then
9864 Eq_Spec := Predef_Spec_Or_Body (Loc,
9865 Tag_Typ => Tag_Typ,
9866 Name => Eq_Name,
9867 Profile => New_List (
9868 Make_Parameter_Specification (Loc,
9869 Defining_Identifier =>
9870 Make_Defining_Identifier (Loc, Name_X),
9871 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
9873 Make_Parameter_Specification (Loc,
9874 Defining_Identifier =>
9875 Make_Defining_Identifier (Loc, Name_Y),
9876 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
9877 Ret_Type => Standard_Boolean);
9878 Append_To (Res, Eq_Spec);
9880 if Has_Predef_Eq_Renaming then
9881 Renamed_Eq := Defining_Unit_Name (Specification (Eq_Spec));
9883 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
9884 while Present (Prim) loop
9886 -- Any renamings of equality that appeared before an
9887 -- overriding equality must be updated to refer to the
9888 -- entity for the predefined equality, otherwise calls via
9889 -- the renaming would get incorrectly resolved to call the
9890 -- user-defined equality function.
9892 if Is_Predefined_Eq_Renaming (Node (Prim)) then
9893 Set_Alias (Node (Prim), Renamed_Eq);
9895 -- Exit upon encountering a user-defined equality
9897 elsif Chars (Node (Prim)) = Name_Op_Eq
9898 and then No (Alias (Node (Prim)))
9899 then
9900 exit;
9901 end if;
9903 Next_Elmt (Prim);
9904 end loop;
9905 end if;
9906 end if;
9908 -- Spec for dispatching assignment
9910 Append_To (Res, Predef_Spec_Or_Body (Loc,
9911 Tag_Typ => Tag_Typ,
9912 Name => Name_uAssign,
9913 Profile => New_List (
9914 Make_Parameter_Specification (Loc,
9915 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
9916 Out_Present => True,
9917 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
9919 Make_Parameter_Specification (Loc,
9920 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
9921 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)))));
9922 end if;
9924 -- Ada 2005: Generate declarations for the following primitive
9925 -- operations for limited interfaces and synchronized types that
9926 -- implement a limited interface.
9928 -- Disp_Asynchronous_Select
9929 -- Disp_Conditional_Select
9930 -- Disp_Get_Prim_Op_Kind
9931 -- Disp_Get_Task_Id
9932 -- Disp_Requeue
9933 -- Disp_Timed_Select
9935 -- Disable the generation of these bodies if No_Dispatching_Calls,
9936 -- Ravenscar or ZFP is active.
9938 if Ada_Version >= Ada_2005
9939 and then not Restriction_Active (No_Dispatching_Calls)
9940 and then not Restriction_Active (No_Select_Statements)
9941 and then RTE_Available (RE_Select_Specific_Data)
9942 then
9943 -- These primitives are defined abstract in interface types
9945 if Is_Interface (Tag_Typ)
9946 and then Is_Limited_Record (Tag_Typ)
9947 then
9948 Append_To (Res,
9949 Make_Abstract_Subprogram_Declaration (Loc,
9950 Specification =>
9951 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
9953 Append_To (Res,
9954 Make_Abstract_Subprogram_Declaration (Loc,
9955 Specification =>
9956 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
9958 Append_To (Res,
9959 Make_Abstract_Subprogram_Declaration (Loc,
9960 Specification =>
9961 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
9963 Append_To (Res,
9964 Make_Abstract_Subprogram_Declaration (Loc,
9965 Specification =>
9966 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
9968 Append_To (Res,
9969 Make_Abstract_Subprogram_Declaration (Loc,
9970 Specification =>
9971 Make_Disp_Requeue_Spec (Tag_Typ)));
9973 Append_To (Res,
9974 Make_Abstract_Subprogram_Declaration (Loc,
9975 Specification =>
9976 Make_Disp_Timed_Select_Spec (Tag_Typ)));
9978 -- If ancestor is an interface type, declare non-abstract primitives
9979 -- to override the abstract primitives of the interface type.
9981 -- In VM targets we define these primitives in all root tagged types
9982 -- that are not interface types. Done because in VM targets we don't
9983 -- have secondary dispatch tables and any derivation of Tag_Typ may
9984 -- cover limited interfaces (which always have these primitives since
9985 -- they may be ancestors of synchronized interface types).
9987 elsif (not Is_Interface (Tag_Typ)
9988 and then Is_Interface (Etype (Tag_Typ))
9989 and then Is_Limited_Record (Etype (Tag_Typ)))
9990 or else
9991 (Is_Concurrent_Record_Type (Tag_Typ)
9992 and then Has_Interfaces (Tag_Typ))
9993 or else
9994 (not Tagged_Type_Expansion
9995 and then not Is_Interface (Tag_Typ)
9996 and then Tag_Typ = Root_Type (Tag_Typ))
9997 then
9998 Append_To (Res,
9999 Make_Subprogram_Declaration (Loc,
10000 Specification =>
10001 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
10003 Append_To (Res,
10004 Make_Subprogram_Declaration (Loc,
10005 Specification =>
10006 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
10008 Append_To (Res,
10009 Make_Subprogram_Declaration (Loc,
10010 Specification =>
10011 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
10013 Append_To (Res,
10014 Make_Subprogram_Declaration (Loc,
10015 Specification =>
10016 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
10018 Append_To (Res,
10019 Make_Subprogram_Declaration (Loc,
10020 Specification =>
10021 Make_Disp_Requeue_Spec (Tag_Typ)));
10023 Append_To (Res,
10024 Make_Subprogram_Declaration (Loc,
10025 Specification =>
10026 Make_Disp_Timed_Select_Spec (Tag_Typ)));
10027 end if;
10028 end if;
10030 -- All tagged types receive their own Deep_Adjust and Deep_Finalize
10031 -- regardless of whether they are controlled or may contain controlled
10032 -- components.
10034 -- Do not generate the routines if finalization is disabled
10036 if Restriction_Active (No_Finalization) then
10037 null;
10039 else
10040 if not Is_Limited_Type (Tag_Typ) then
10041 Append_To (Res, Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust));
10042 end if;
10044 Append_To (Res, Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize));
10045 end if;
10047 Predef_List := Res;
10048 end Make_Predefined_Primitive_Specs;
10050 -------------------------
10051 -- Make_Tag_Assignment --
10052 -------------------------
10054 function Make_Tag_Assignment (N : Node_Id) return Node_Id is
10055 Loc : constant Source_Ptr := Sloc (N);
10056 Def_If : constant Entity_Id := Defining_Identifier (N);
10057 Expr : constant Node_Id := Expression (N);
10058 Typ : constant Entity_Id := Etype (Def_If);
10059 Full_Typ : constant Entity_Id := Underlying_Type (Typ);
10060 New_Ref : Node_Id;
10062 begin
10063 -- This expansion activity is called during analysis, but cannot
10064 -- be applied in ASIS mode when other expansion is disabled.
10066 if Is_Tagged_Type (Typ)
10067 and then not Is_Class_Wide_Type (Typ)
10068 and then not Is_CPP_Class (Typ)
10069 and then Tagged_Type_Expansion
10070 and then Nkind (Expr) /= N_Aggregate
10071 and then not ASIS_Mode
10072 and then (Nkind (Expr) /= N_Qualified_Expression
10073 or else Nkind (Expression (Expr)) /= N_Aggregate)
10074 then
10075 New_Ref :=
10076 Make_Selected_Component (Loc,
10077 Prefix => New_Occurrence_Of (Def_If, Loc),
10078 Selector_Name =>
10079 New_Occurrence_Of (First_Tag_Component (Full_Typ), Loc));
10080 Set_Assignment_OK (New_Ref);
10082 return
10083 Make_Assignment_Statement (Loc,
10084 Name => New_Ref,
10085 Expression =>
10086 Unchecked_Convert_To (RTE (RE_Tag),
10087 New_Occurrence_Of (Node
10088 (First_Elmt (Access_Disp_Table (Full_Typ))), Loc)));
10089 else
10090 return Empty;
10091 end if;
10092 end Make_Tag_Assignment;
10094 ----------------------
10095 -- Predef_Deep_Spec --
10096 ----------------------
10098 function Predef_Deep_Spec
10099 (Loc : Source_Ptr;
10100 Tag_Typ : Entity_Id;
10101 Name : TSS_Name_Type;
10102 For_Body : Boolean := False) return Node_Id
10104 Formals : List_Id;
10106 begin
10107 -- V : in out Tag_Typ
10109 Formals := New_List (
10110 Make_Parameter_Specification (Loc,
10111 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
10112 In_Present => True,
10113 Out_Present => True,
10114 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)));
10116 -- F : Boolean := True
10118 if Name = TSS_Deep_Adjust
10119 or else Name = TSS_Deep_Finalize
10120 then
10121 Append_To (Formals,
10122 Make_Parameter_Specification (Loc,
10123 Defining_Identifier => Make_Defining_Identifier (Loc, Name_F),
10124 Parameter_Type => New_Occurrence_Of (Standard_Boolean, Loc),
10125 Expression => New_Occurrence_Of (Standard_True, Loc)));
10126 end if;
10128 return
10129 Predef_Spec_Or_Body (Loc,
10130 Name => Make_TSS_Name (Tag_Typ, Name),
10131 Tag_Typ => Tag_Typ,
10132 Profile => Formals,
10133 For_Body => For_Body);
10135 exception
10136 when RE_Not_Available =>
10137 return Empty;
10138 end Predef_Deep_Spec;
10140 -------------------------
10141 -- Predef_Spec_Or_Body --
10142 -------------------------
10144 function Predef_Spec_Or_Body
10145 (Loc : Source_Ptr;
10146 Tag_Typ : Entity_Id;
10147 Name : Name_Id;
10148 Profile : List_Id;
10149 Ret_Type : Entity_Id := Empty;
10150 For_Body : Boolean := False) return Node_Id
10152 Id : constant Entity_Id := Make_Defining_Identifier (Loc, Name);
10153 Spec : Node_Id;
10155 begin
10156 Set_Is_Public (Id, Is_Public (Tag_Typ));
10158 -- The internal flag is set to mark these declarations because they have
10159 -- specific properties. First, they are primitives even if they are not
10160 -- defined in the type scope (the freezing point is not necessarily in
10161 -- the same scope). Second, the predefined equality can be overridden by
10162 -- a user-defined equality, no body will be generated in this case.
10164 Set_Is_Internal (Id);
10166 if not Debug_Generated_Code then
10167 Set_Debug_Info_Off (Id);
10168 end if;
10170 if No (Ret_Type) then
10171 Spec :=
10172 Make_Procedure_Specification (Loc,
10173 Defining_Unit_Name => Id,
10174 Parameter_Specifications => Profile);
10175 else
10176 Spec :=
10177 Make_Function_Specification (Loc,
10178 Defining_Unit_Name => Id,
10179 Parameter_Specifications => Profile,
10180 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
10181 end if;
10183 if Is_Interface (Tag_Typ) then
10184 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
10186 -- If body case, return empty subprogram body. Note that this is ill-
10187 -- formed, because there is not even a null statement, and certainly not
10188 -- a return in the function case. The caller is expected to do surgery
10189 -- on the body to add the appropriate stuff.
10191 elsif For_Body then
10192 return Make_Subprogram_Body (Loc, Spec, Empty_List, Empty);
10194 -- For the case of an Input attribute predefined for an abstract type,
10195 -- generate an abstract specification. This will never be called, but we
10196 -- need the slot allocated in the dispatching table so that attributes
10197 -- typ'Class'Input and typ'Class'Output will work properly.
10199 elsif Is_TSS (Name, TSS_Stream_Input)
10200 and then Is_Abstract_Type (Tag_Typ)
10201 then
10202 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
10204 -- Normal spec case, where we return a subprogram declaration
10206 else
10207 return Make_Subprogram_Declaration (Loc, Spec);
10208 end if;
10209 end Predef_Spec_Or_Body;
10211 -----------------------------
10212 -- Predef_Stream_Attr_Spec --
10213 -----------------------------
10215 function Predef_Stream_Attr_Spec
10216 (Loc : Source_Ptr;
10217 Tag_Typ : Entity_Id;
10218 Name : TSS_Name_Type;
10219 For_Body : Boolean := False) return Node_Id
10221 Ret_Type : Entity_Id;
10223 begin
10224 if Name = TSS_Stream_Input then
10225 Ret_Type := Tag_Typ;
10226 else
10227 Ret_Type := Empty;
10228 end if;
10230 return
10231 Predef_Spec_Or_Body
10232 (Loc,
10233 Name => Make_TSS_Name (Tag_Typ, Name),
10234 Tag_Typ => Tag_Typ,
10235 Profile => Build_Stream_Attr_Profile (Loc, Tag_Typ, Name),
10236 Ret_Type => Ret_Type,
10237 For_Body => For_Body);
10238 end Predef_Stream_Attr_Spec;
10240 ---------------------------------
10241 -- Predefined_Primitive_Bodies --
10242 ---------------------------------
10244 function Predefined_Primitive_Bodies
10245 (Tag_Typ : Entity_Id;
10246 Renamed_Eq : Entity_Id) return List_Id
10248 Loc : constant Source_Ptr := Sloc (Tag_Typ);
10249 Res : constant List_Id := New_List;
10250 Adj_Call : Node_Id;
10251 Decl : Node_Id;
10252 Fin_Call : Node_Id;
10253 Prim : Elmt_Id;
10254 Eq_Needed : Boolean;
10255 Eq_Name : Name_Id;
10256 Ent : Entity_Id;
10258 pragma Warnings (Off, Ent);
10260 begin
10261 pragma Assert (not Is_Interface (Tag_Typ));
10263 -- See if we have a predefined "=" operator
10265 if Present (Renamed_Eq) then
10266 Eq_Needed := True;
10267 Eq_Name := Chars (Renamed_Eq);
10269 -- If the parent is an interface type then it has defined all the
10270 -- predefined primitives abstract and we need to check if the type
10271 -- has some user defined "=" function which matches the profile of
10272 -- the Ada predefined equality operator to avoid generating it.
10274 elsif Is_Interface (Etype (Tag_Typ)) then
10275 Eq_Needed := True;
10276 Eq_Name := Name_Op_Eq;
10278 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
10279 while Present (Prim) loop
10280 if Chars (Node (Prim)) = Name_Op_Eq
10281 and then not Is_Internal (Node (Prim))
10282 and then Present (First_Entity (Node (Prim)))
10284 -- The predefined equality primitive must have exactly two
10285 -- formals whose type is this tagged type
10287 and then Present (Last_Entity (Node (Prim)))
10288 and then Next_Entity (First_Entity (Node (Prim)))
10289 = Last_Entity (Node (Prim))
10290 and then Etype (First_Entity (Node (Prim))) = Tag_Typ
10291 and then Etype (Last_Entity (Node (Prim))) = Tag_Typ
10292 then
10293 Eq_Needed := False;
10294 Eq_Name := No_Name;
10295 exit;
10296 end if;
10298 Next_Elmt (Prim);
10299 end loop;
10301 else
10302 Eq_Needed := False;
10303 Eq_Name := No_Name;
10305 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
10306 while Present (Prim) loop
10307 if Chars (Node (Prim)) = Name_Op_Eq
10308 and then Is_Internal (Node (Prim))
10309 then
10310 Eq_Needed := True;
10311 Eq_Name := Name_Op_Eq;
10312 exit;
10313 end if;
10315 Next_Elmt (Prim);
10316 end loop;
10317 end if;
10319 -- Body of _Size
10321 Decl := Predef_Spec_Or_Body (Loc,
10322 Tag_Typ => Tag_Typ,
10323 Name => Name_uSize,
10324 Profile => New_List (
10325 Make_Parameter_Specification (Loc,
10326 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
10327 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
10329 Ret_Type => Standard_Long_Long_Integer,
10330 For_Body => True);
10332 Set_Handled_Statement_Sequence (Decl,
10333 Make_Handled_Sequence_Of_Statements (Loc, New_List (
10334 Make_Simple_Return_Statement (Loc,
10335 Expression =>
10336 Make_Attribute_Reference (Loc,
10337 Prefix => Make_Identifier (Loc, Name_X),
10338 Attribute_Name => Name_Size)))));
10340 Append_To (Res, Decl);
10342 -- Bodies for Dispatching stream IO routines. We need these only for
10343 -- non-limited types (in the limited case there is no dispatching).
10344 -- We also skip them if dispatching or finalization are not available
10345 -- or if stream operations are prohibited by restriction No_Streams or
10346 -- from use of pragma/aspect No_Tagged_Streams.
10348 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Read)
10349 and then No (TSS (Tag_Typ, TSS_Stream_Read))
10350 then
10351 Build_Record_Read_Procedure (Loc, Tag_Typ, Decl, Ent);
10352 Append_To (Res, Decl);
10353 end if;
10355 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Write)
10356 and then No (TSS (Tag_Typ, TSS_Stream_Write))
10357 then
10358 Build_Record_Write_Procedure (Loc, Tag_Typ, Decl, Ent);
10359 Append_To (Res, Decl);
10360 end if;
10362 -- Skip body of _Input for the abstract case, since the corresponding
10363 -- spec is abstract (see Predef_Spec_Or_Body).
10365 if not Is_Abstract_Type (Tag_Typ)
10366 and then Stream_Operation_OK (Tag_Typ, TSS_Stream_Input)
10367 and then No (TSS (Tag_Typ, TSS_Stream_Input))
10368 then
10369 Build_Record_Or_Elementary_Input_Function
10370 (Loc, Tag_Typ, Decl, Ent);
10371 Append_To (Res, Decl);
10372 end if;
10374 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Output)
10375 and then No (TSS (Tag_Typ, TSS_Stream_Output))
10376 then
10377 Build_Record_Or_Elementary_Output_Procedure (Loc, Tag_Typ, Decl, Ent);
10378 Append_To (Res, Decl);
10379 end if;
10381 -- Ada 2005: Generate bodies for the following primitive operations for
10382 -- limited interfaces and synchronized types that implement a limited
10383 -- interface.
10385 -- disp_asynchronous_select
10386 -- disp_conditional_select
10387 -- disp_get_prim_op_kind
10388 -- disp_get_task_id
10389 -- disp_timed_select
10391 -- The interface versions will have null bodies
10393 -- Disable the generation of these bodies if No_Dispatching_Calls,
10394 -- Ravenscar or ZFP is active.
10396 -- In VM targets we define these primitives in all root tagged types
10397 -- that are not interface types. Done because in VM targets we don't
10398 -- have secondary dispatch tables and any derivation of Tag_Typ may
10399 -- cover limited interfaces (which always have these primitives since
10400 -- they may be ancestors of synchronized interface types).
10402 if Ada_Version >= Ada_2005
10403 and then not Is_Interface (Tag_Typ)
10404 and then
10405 ((Is_Interface (Etype (Tag_Typ))
10406 and then Is_Limited_Record (Etype (Tag_Typ)))
10407 or else
10408 (Is_Concurrent_Record_Type (Tag_Typ)
10409 and then Has_Interfaces (Tag_Typ))
10410 or else
10411 (not Tagged_Type_Expansion
10412 and then Tag_Typ = Root_Type (Tag_Typ)))
10413 and then not Restriction_Active (No_Dispatching_Calls)
10414 and then not Restriction_Active (No_Select_Statements)
10415 and then RTE_Available (RE_Select_Specific_Data)
10416 then
10417 Append_To (Res, Make_Disp_Asynchronous_Select_Body (Tag_Typ));
10418 Append_To (Res, Make_Disp_Conditional_Select_Body (Tag_Typ));
10419 Append_To (Res, Make_Disp_Get_Prim_Op_Kind_Body (Tag_Typ));
10420 Append_To (Res, Make_Disp_Get_Task_Id_Body (Tag_Typ));
10421 Append_To (Res, Make_Disp_Requeue_Body (Tag_Typ));
10422 Append_To (Res, Make_Disp_Timed_Select_Body (Tag_Typ));
10423 end if;
10425 if not Is_Limited_Type (Tag_Typ) and then not Is_Interface (Tag_Typ) then
10427 -- Body for equality
10429 if Eq_Needed then
10430 Decl := Make_Eq_Body (Tag_Typ, Eq_Name);
10431 Append_To (Res, Decl);
10432 end if;
10434 -- Body for inequality (if required)
10436 Decl := Make_Neq_Body (Tag_Typ);
10438 if Present (Decl) then
10439 Append_To (Res, Decl);
10440 end if;
10442 -- Body for dispatching assignment
10444 Decl :=
10445 Predef_Spec_Or_Body (Loc,
10446 Tag_Typ => Tag_Typ,
10447 Name => Name_uAssign,
10448 Profile => New_List (
10449 Make_Parameter_Specification (Loc,
10450 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
10451 Out_Present => True,
10452 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
10454 Make_Parameter_Specification (Loc,
10455 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
10456 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
10457 For_Body => True);
10459 Set_Handled_Statement_Sequence (Decl,
10460 Make_Handled_Sequence_Of_Statements (Loc, New_List (
10461 Make_Assignment_Statement (Loc,
10462 Name => Make_Identifier (Loc, Name_X),
10463 Expression => Make_Identifier (Loc, Name_Y)))));
10465 Append_To (Res, Decl);
10466 end if;
10468 -- Generate empty bodies of routines Deep_Adjust and Deep_Finalize for
10469 -- tagged types which do not contain controlled components.
10471 -- Do not generate the routines if finalization is disabled
10473 if Restriction_Active (No_Finalization) then
10474 null;
10476 elsif not Has_Controlled_Component (Tag_Typ) then
10477 if not Is_Limited_Type (Tag_Typ) then
10478 Adj_Call := Empty;
10479 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust, True);
10481 if Is_Controlled (Tag_Typ) then
10482 Adj_Call :=
10483 Make_Adjust_Call (
10484 Obj_Ref => Make_Identifier (Loc, Name_V),
10485 Typ => Tag_Typ);
10486 end if;
10488 if No (Adj_Call) then
10489 Adj_Call := Make_Null_Statement (Loc);
10490 end if;
10492 Set_Handled_Statement_Sequence (Decl,
10493 Make_Handled_Sequence_Of_Statements (Loc,
10494 Statements => New_List (Adj_Call)));
10496 Append_To (Res, Decl);
10497 end if;
10499 Fin_Call := Empty;
10500 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize, True);
10502 if Is_Controlled (Tag_Typ) then
10503 Fin_Call :=
10504 Make_Final_Call
10505 (Obj_Ref => Make_Identifier (Loc, Name_V),
10506 Typ => Tag_Typ);
10507 end if;
10509 if No (Fin_Call) then
10510 Fin_Call := Make_Null_Statement (Loc);
10511 end if;
10513 Set_Handled_Statement_Sequence (Decl,
10514 Make_Handled_Sequence_Of_Statements (Loc,
10515 Statements => New_List (Fin_Call)));
10517 Append_To (Res, Decl);
10518 end if;
10520 return Res;
10521 end Predefined_Primitive_Bodies;
10523 ---------------------------------
10524 -- Predefined_Primitive_Freeze --
10525 ---------------------------------
10527 function Predefined_Primitive_Freeze
10528 (Tag_Typ : Entity_Id) return List_Id
10530 Res : constant List_Id := New_List;
10531 Prim : Elmt_Id;
10532 Frnodes : List_Id;
10534 begin
10535 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
10536 while Present (Prim) loop
10537 if Is_Predefined_Dispatching_Operation (Node (Prim)) then
10538 Frnodes := Freeze_Entity (Node (Prim), Tag_Typ);
10540 if Present (Frnodes) then
10541 Append_List_To (Res, Frnodes);
10542 end if;
10543 end if;
10545 Next_Elmt (Prim);
10546 end loop;
10548 return Res;
10549 end Predefined_Primitive_Freeze;
10551 -------------------------
10552 -- Stream_Operation_OK --
10553 -------------------------
10555 function Stream_Operation_OK
10556 (Typ : Entity_Id;
10557 Operation : TSS_Name_Type) return Boolean
10559 Has_Predefined_Or_Specified_Stream_Attribute : Boolean := False;
10561 begin
10562 -- Special case of a limited type extension: a default implementation
10563 -- of the stream attributes Read or Write exists if that attribute
10564 -- has been specified or is available for an ancestor type; a default
10565 -- implementation of the attribute Output (resp. Input) exists if the
10566 -- attribute has been specified or Write (resp. Read) is available for
10567 -- an ancestor type. The last condition only applies under Ada 2005.
10569 if Is_Limited_Type (Typ) and then Is_Tagged_Type (Typ) then
10570 if Operation = TSS_Stream_Read then
10571 Has_Predefined_Or_Specified_Stream_Attribute :=
10572 Has_Specified_Stream_Read (Typ);
10574 elsif Operation = TSS_Stream_Write then
10575 Has_Predefined_Or_Specified_Stream_Attribute :=
10576 Has_Specified_Stream_Write (Typ);
10578 elsif Operation = TSS_Stream_Input then
10579 Has_Predefined_Or_Specified_Stream_Attribute :=
10580 Has_Specified_Stream_Input (Typ)
10581 or else
10582 (Ada_Version >= Ada_2005
10583 and then Stream_Operation_OK (Typ, TSS_Stream_Read));
10585 elsif Operation = TSS_Stream_Output then
10586 Has_Predefined_Or_Specified_Stream_Attribute :=
10587 Has_Specified_Stream_Output (Typ)
10588 or else
10589 (Ada_Version >= Ada_2005
10590 and then Stream_Operation_OK (Typ, TSS_Stream_Write));
10591 end if;
10593 -- Case of inherited TSS_Stream_Read or TSS_Stream_Write
10595 if not Has_Predefined_Or_Specified_Stream_Attribute
10596 and then Is_Derived_Type (Typ)
10597 and then (Operation = TSS_Stream_Read
10598 or else Operation = TSS_Stream_Write)
10599 then
10600 Has_Predefined_Or_Specified_Stream_Attribute :=
10601 Present
10602 (Find_Inherited_TSS (Base_Type (Etype (Typ)), Operation));
10603 end if;
10604 end if;
10606 -- If the type is not limited, or else is limited but the attribute is
10607 -- explicitly specified or is predefined for the type, then return True,
10608 -- unless other conditions prevail, such as restrictions prohibiting
10609 -- streams or dispatching operations. We also return True for limited
10610 -- interfaces, because they may be extended by nonlimited types and
10611 -- permit inheritance in this case (addresses cases where an abstract
10612 -- extension doesn't get 'Input declared, as per comments below, but
10613 -- 'Class'Input must still be allowed). Note that attempts to apply
10614 -- stream attributes to a limited interface or its class-wide type
10615 -- (or limited extensions thereof) will still get properly rejected
10616 -- by Check_Stream_Attribute.
10618 -- We exclude the Input operation from being a predefined subprogram in
10619 -- the case where the associated type is an abstract extension, because
10620 -- the attribute is not callable in that case, per 13.13.2(49/2). Also,
10621 -- we don't want an abstract version created because types derived from
10622 -- the abstract type may not even have Input available (for example if
10623 -- derived from a private view of the abstract type that doesn't have
10624 -- a visible Input).
10626 -- Do not generate stream routines for type Finalization_Master because
10627 -- a master may never appear in types and therefore cannot be read or
10628 -- written.
10630 return
10631 (not Is_Limited_Type (Typ)
10632 or else Is_Interface (Typ)
10633 or else Has_Predefined_Or_Specified_Stream_Attribute)
10634 and then
10635 (Operation /= TSS_Stream_Input
10636 or else not Is_Abstract_Type (Typ)
10637 or else not Is_Derived_Type (Typ))
10638 and then not Has_Unknown_Discriminants (Typ)
10639 and then not
10640 (Is_Interface (Typ)
10641 and then
10642 (Is_Task_Interface (Typ)
10643 or else Is_Protected_Interface (Typ)
10644 or else Is_Synchronized_Interface (Typ)))
10645 and then not Restriction_Active (No_Streams)
10646 and then not Restriction_Active (No_Dispatch)
10647 and then No (No_Tagged_Streams_Pragma (Typ))
10648 and then not No_Run_Time_Mode
10649 and then RTE_Available (RE_Tag)
10650 and then No (Type_Without_Stream_Operation (Typ))
10651 and then RTE_Available (RE_Root_Stream_Type)
10652 and then not Is_RTE (Typ, RE_Finalization_Master);
10653 end Stream_Operation_OK;
10655 end Exp_Ch3;