gcc/testsuite/ChangeLog:
[official-gcc.git] / gcc / ada / exp_ch3.adb
blob9281896d1312d19e9be986097be48852cc8c3092
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 -- Handle the optionally generated formal *_skip_null_excluding_checks
1555 if Needs_Conditional_Null_Excluding_Check (Full_Init_Type) then
1557 -- Look at the associated node for the object we are referencing
1558 -- and verify that we are expanding a call to an Init_Proc for an
1559 -- internally generated object declaration before passing True and
1560 -- skipping the relevant checks.
1562 if Nkind (Id_Ref) in N_Has_Entity
1563 and then Comes_From_Source (Associated_Node (Id_Ref))
1564 then
1565 Append_To (Args, New_Occurrence_Of (Standard_True, Loc));
1567 -- Otherwise, we pass False to perform null-excluding checks
1569 else
1570 Append_To (Args, New_Occurrence_Of (Standard_False, Loc));
1571 end if;
1572 end if;
1574 -- Add discriminant values if discriminants are present
1576 if Has_Discriminants (Full_Init_Type) then
1577 Discr := First_Discriminant (Full_Init_Type);
1578 while Present (Discr) loop
1580 -- If this is a discriminated concurrent type, the init_proc
1581 -- for the corresponding record is being called. Use that type
1582 -- directly to find the discriminant value, to handle properly
1583 -- intervening renamed discriminants.
1585 declare
1586 T : Entity_Id := Full_Type;
1588 begin
1589 if Is_Protected_Type (T) then
1590 T := Corresponding_Record_Type (T);
1591 end if;
1593 Arg :=
1594 Get_Discriminant_Value (
1595 Discr,
1597 Discriminant_Constraint (Full_Type));
1598 end;
1600 -- If the target has access discriminants, and is constrained by
1601 -- an access to the enclosing construct, i.e. a current instance,
1602 -- replace the reference to the type by a reference to the object.
1604 if Nkind (Arg) = N_Attribute_Reference
1605 and then Is_Access_Type (Etype (Arg))
1606 and then Is_Entity_Name (Prefix (Arg))
1607 and then Is_Type (Entity (Prefix (Arg)))
1608 then
1609 Arg :=
1610 Make_Attribute_Reference (Loc,
1611 Prefix => New_Copy (Prefix (Id_Ref)),
1612 Attribute_Name => Name_Unrestricted_Access);
1614 elsif In_Init_Proc then
1616 -- Replace any possible references to the discriminant in the
1617 -- call to the record initialization procedure with references
1618 -- to the appropriate formal parameter.
1620 if Nkind (Arg) = N_Identifier
1621 and then Ekind (Entity (Arg)) = E_Discriminant
1622 then
1623 Arg := New_Occurrence_Of (Discriminal (Entity (Arg)), Loc);
1625 -- Otherwise make a copy of the default expression. Note that
1626 -- we use the current Sloc for this, because we do not want the
1627 -- call to appear to be at the declaration point. Within the
1628 -- expression, replace discriminants with their discriminals.
1630 else
1631 Arg :=
1632 New_Copy_Tree (Arg, Map => Discr_Map, New_Sloc => Loc);
1633 end if;
1635 else
1636 if Is_Constrained (Full_Type) then
1637 Arg := Duplicate_Subexpr_No_Checks (Arg);
1638 else
1639 -- The constraints come from the discriminant default exps,
1640 -- they must be reevaluated, so we use New_Copy_Tree but we
1641 -- ensure the proper Sloc (for any embedded calls).
1642 -- In addition, if a predicate check is needed on the value
1643 -- of the discriminant, insert it ahead of the call.
1645 Arg := New_Copy_Tree (Arg, New_Sloc => Loc);
1646 end if;
1648 if Has_Predicates (Etype (Discr)) then
1649 Check_Predicated_Discriminant (Arg, Discr);
1650 end if;
1651 end if;
1653 -- Ada 2005 (AI-287): In case of default initialized components,
1654 -- if the component is constrained with a discriminant of the
1655 -- enclosing type, we need to generate the corresponding selected
1656 -- component node to access the discriminant value. In other cases
1657 -- this is not required, either because we are inside the init
1658 -- proc and we use the corresponding formal, or else because the
1659 -- component is constrained by an expression.
1661 if With_Default_Init
1662 and then Nkind (Id_Ref) = N_Selected_Component
1663 and then Nkind (Arg) = N_Identifier
1664 and then Ekind (Entity (Arg)) = E_Discriminant
1665 then
1666 Append_To (Args,
1667 Make_Selected_Component (Loc,
1668 Prefix => New_Copy_Tree (Prefix (Id_Ref)),
1669 Selector_Name => Arg));
1670 else
1671 Append_To (Args, Arg);
1672 end if;
1674 Next_Discriminant (Discr);
1675 end loop;
1676 end if;
1678 -- If this is a call to initialize the parent component of a derived
1679 -- tagged type, indicate that the tag should not be set in the parent.
1681 if Is_Tagged_Type (Full_Init_Type)
1682 and then not Is_CPP_Class (Full_Init_Type)
1683 and then Nkind (Id_Ref) = N_Selected_Component
1684 and then Chars (Selector_Name (Id_Ref)) = Name_uParent
1685 then
1686 Append_To (Args, New_Occurrence_Of (Standard_False, Loc));
1688 elsif Present (Constructor_Ref) then
1689 Append_List_To (Args,
1690 New_Copy_List (Parameter_Associations (Constructor_Ref)));
1691 end if;
1693 Append_To (Res,
1694 Make_Procedure_Call_Statement (Loc,
1695 Name => New_Occurrence_Of (Proc, Loc),
1696 Parameter_Associations => Args));
1698 if Needs_Finalization (Typ)
1699 and then Nkind (Id_Ref) = N_Selected_Component
1700 then
1701 if Chars (Selector_Name (Id_Ref)) /= Name_uParent then
1702 Init_Call :=
1703 Make_Init_Call
1704 (Obj_Ref => New_Copy_Tree (First_Arg),
1705 Typ => Typ);
1707 -- Guard against a missing [Deep_]Initialize when the type was not
1708 -- properly frozen.
1710 if Present (Init_Call) then
1711 Append_To (Res, Init_Call);
1712 end if;
1713 end if;
1714 end if;
1716 return Res;
1718 exception
1719 when RE_Not_Available =>
1720 return Empty_List;
1721 end Build_Initialization_Call;
1723 ----------------------------
1724 -- Build_Record_Init_Proc --
1725 ----------------------------
1727 procedure Build_Record_Init_Proc (N : Node_Id; Rec_Ent : Entity_Id) is
1728 Decls : constant List_Id := New_List;
1729 Discr_Map : constant Elist_Id := New_Elmt_List;
1730 Loc : constant Source_Ptr := Sloc (Rec_Ent);
1731 Counter : Nat := 0;
1732 Proc_Id : Entity_Id;
1733 Rec_Type : Entity_Id;
1734 Set_Tag : Entity_Id := Empty;
1736 function Build_Assignment
1737 (Id : Entity_Id;
1738 Default : Node_Id) return List_Id;
1739 -- Build an assignment statement that assigns the default expression to
1740 -- its corresponding record component if defined. The left-hand side of
1741 -- the assignment is marked Assignment_OK so that initialization of
1742 -- limited private records works correctly. This routine may also build
1743 -- an adjustment call if the component is controlled.
1745 procedure Build_Discriminant_Assignments (Statement_List : List_Id);
1746 -- If the record has discriminants, add assignment statements to
1747 -- Statement_List to initialize the discriminant values from the
1748 -- arguments of the initialization procedure.
1750 function Build_Init_Statements (Comp_List : Node_Id) return List_Id;
1751 -- Build a list representing a sequence of statements which initialize
1752 -- components of the given component list. This may involve building
1753 -- case statements for the variant parts. Append any locally declared
1754 -- objects on list Decls.
1756 function Build_Init_Call_Thru (Parameters : List_Id) return List_Id;
1757 -- Given an untagged type-derivation that declares discriminants, e.g.
1759 -- type R (R1, R2 : Integer) is record ... end record;
1760 -- type D (D1 : Integer) is new R (1, D1);
1762 -- we make the _init_proc of D be
1764 -- procedure _init_proc (X : D; D1 : Integer) is
1765 -- begin
1766 -- _init_proc (R (X), 1, D1);
1767 -- end _init_proc;
1769 -- This function builds the call statement in this _init_proc.
1771 procedure Build_CPP_Init_Procedure;
1772 -- Build the tree corresponding to the procedure specification and body
1773 -- of the IC procedure that initializes the C++ part of the dispatch
1774 -- table of an Ada tagged type that is a derivation of a CPP type.
1775 -- Install it as the CPP_Init TSS.
1777 procedure Build_Init_Procedure;
1778 -- Build the tree corresponding to the procedure specification and body
1779 -- of the initialization procedure and install it as the _init TSS.
1781 procedure Build_Offset_To_Top_Functions;
1782 -- Ada 2005 (AI-251): Build the tree corresponding to the procedure spec
1783 -- and body of Offset_To_Top, a function used in conjuction with types
1784 -- having secondary dispatch tables.
1786 procedure Build_Record_Checks (S : Node_Id; Check_List : List_Id);
1787 -- Add range checks to components of discriminated records. S is a
1788 -- subtype indication of a record component. Check_List is a list
1789 -- to which the check actions are appended.
1791 function Component_Needs_Simple_Initialization
1792 (T : Entity_Id) return Boolean;
1793 -- Determine if a component needs simple initialization, given its type
1794 -- T. This routine is the same as Needs_Simple_Initialization except for
1795 -- components of type Tag and Interface_Tag. These two access types do
1796 -- not require initialization since they are explicitly initialized by
1797 -- other means.
1799 function Parent_Subtype_Renaming_Discrims return Boolean;
1800 -- Returns True for base types N that rename discriminants, else False
1802 function Requires_Init_Proc (Rec_Id : Entity_Id) return Boolean;
1803 -- Determine whether a record initialization procedure needs to be
1804 -- generated for the given record type.
1806 ----------------------
1807 -- Build_Assignment --
1808 ----------------------
1810 function Build_Assignment
1811 (Id : Entity_Id;
1812 Default : Node_Id) return List_Id
1814 Default_Loc : constant Source_Ptr := Sloc (Default);
1815 Typ : constant Entity_Id := Underlying_Type (Etype (Id));
1817 Adj_Call : Node_Id;
1818 Exp : Node_Id := Default;
1819 Kind : Node_Kind := Nkind (Default);
1820 Lhs : Node_Id;
1821 Res : List_Id;
1823 function Replace_Discr_Ref (N : Node_Id) return Traverse_Result;
1824 -- Analysis of the aggregate has replaced discriminants by their
1825 -- corresponding discriminals, but these are irrelevant when the
1826 -- component has a mutable type and is initialized with an aggregate.
1827 -- Instead, they must be replaced by the values supplied in the
1828 -- aggregate, that will be assigned during the expansion of the
1829 -- assignment.
1831 -----------------------
1832 -- Replace_Discr_Ref --
1833 -----------------------
1835 function Replace_Discr_Ref (N : Node_Id) return Traverse_Result is
1836 Val : Node_Id;
1838 begin
1839 if Is_Entity_Name (N)
1840 and then Present (Entity (N))
1841 and then Is_Formal (Entity (N))
1842 and then Present (Discriminal_Link (Entity (N)))
1843 then
1844 Val :=
1845 Make_Selected_Component (Default_Loc,
1846 Prefix => New_Copy_Tree (Lhs),
1847 Selector_Name =>
1848 New_Occurrence_Of
1849 (Discriminal_Link (Entity (N)), Default_Loc));
1851 if Present (Val) then
1852 Rewrite (N, New_Copy_Tree (Val));
1853 end if;
1854 end if;
1856 return OK;
1857 end Replace_Discr_Ref;
1859 procedure Replace_Discriminant_References is
1860 new Traverse_Proc (Replace_Discr_Ref);
1862 -- Start of processing for Build_Assignment
1864 begin
1865 Lhs :=
1866 Make_Selected_Component (Default_Loc,
1867 Prefix => Make_Identifier (Loc, Name_uInit),
1868 Selector_Name => New_Occurrence_Of (Id, Default_Loc));
1869 Set_Assignment_OK (Lhs);
1871 if Nkind (Exp) = N_Aggregate
1872 and then Has_Discriminants (Typ)
1873 and then not Is_Constrained (Base_Type (Typ))
1874 then
1875 -- The aggregate may provide new values for the discriminants
1876 -- of the component, and other components may depend on those
1877 -- discriminants. Previous analysis of those expressions have
1878 -- replaced the discriminants by the formals of the initialization
1879 -- procedure for the type, but these are irrelevant in the
1880 -- enclosing initialization procedure: those discriminant
1881 -- references must be replaced by the values provided in the
1882 -- aggregate.
1884 Replace_Discriminant_References (Exp);
1885 end if;
1887 -- Case of an access attribute applied to the current instance.
1888 -- Replace the reference to the type by a reference to the actual
1889 -- object. (Note that this handles the case of the top level of
1890 -- the expression being given by such an attribute, but does not
1891 -- cover uses nested within an initial value expression. Nested
1892 -- uses are unlikely to occur in practice, but are theoretically
1893 -- possible.) It is not clear how to handle them without fully
1894 -- traversing the expression. ???
1896 if Kind = N_Attribute_Reference
1897 and then Nam_In (Attribute_Name (Default), Name_Unchecked_Access,
1898 Name_Unrestricted_Access)
1899 and then Is_Entity_Name (Prefix (Default))
1900 and then Is_Type (Entity (Prefix (Default)))
1901 and then Entity (Prefix (Default)) = Rec_Type
1902 then
1903 Exp :=
1904 Make_Attribute_Reference (Default_Loc,
1905 Prefix =>
1906 Make_Identifier (Default_Loc, Name_uInit),
1907 Attribute_Name => Name_Unrestricted_Access);
1908 end if;
1910 -- Take a copy of Exp to ensure that later copies of this component
1911 -- declaration in derived types see the original tree, not a node
1912 -- rewritten during expansion of the init_proc. If the copy contains
1913 -- itypes, the scope of the new itypes is the init_proc being built.
1915 Exp := New_Copy_Tree (Exp, New_Scope => Proc_Id);
1917 Res := New_List (
1918 Make_Assignment_Statement (Loc,
1919 Name => Lhs,
1920 Expression => Exp));
1922 Set_No_Ctrl_Actions (First (Res));
1924 -- Adjust the tag if tagged (because of possible view conversions).
1925 -- Suppress the tag adjustment when not Tagged_Type_Expansion because
1926 -- tags are represented implicitly in objects.
1928 if Is_Tagged_Type (Typ) and then Tagged_Type_Expansion then
1929 Append_To (Res,
1930 Make_Assignment_Statement (Default_Loc,
1931 Name =>
1932 Make_Selected_Component (Default_Loc,
1933 Prefix =>
1934 New_Copy_Tree (Lhs, New_Scope => Proc_Id),
1935 Selector_Name =>
1936 New_Occurrence_Of
1937 (First_Tag_Component (Typ), Default_Loc)),
1939 Expression =>
1940 Unchecked_Convert_To (RTE (RE_Tag),
1941 New_Occurrence_Of
1942 (Node (First_Elmt (Access_Disp_Table (Underlying_Type
1943 (Typ)))),
1944 Default_Loc))));
1945 end if;
1947 -- Adjust the component if controlled except if it is an aggregate
1948 -- that will be expanded inline.
1950 if Kind = N_Qualified_Expression then
1951 Kind := Nkind (Expression (Default));
1952 end if;
1954 if Needs_Finalization (Typ)
1955 and then not (Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate))
1956 and then not Is_Build_In_Place_Function_Call (Exp)
1957 then
1958 Adj_Call :=
1959 Make_Adjust_Call
1960 (Obj_Ref => New_Copy_Tree (Lhs),
1961 Typ => Etype (Id));
1963 -- Guard against a missing [Deep_]Adjust when the component type
1964 -- was not properly frozen.
1966 if Present (Adj_Call) then
1967 Append_To (Res, Adj_Call);
1968 end if;
1969 end if;
1971 -- If a component type has a predicate, add check to the component
1972 -- assignment. Discriminants are handled at the point of the call,
1973 -- which provides for a better error message.
1975 if Comes_From_Source (Exp)
1976 and then Has_Predicates (Typ)
1977 and then not Predicate_Checks_Suppressed (Empty)
1978 and then not Predicates_Ignored (Typ)
1979 then
1980 Append (Make_Predicate_Check (Typ, Exp), Res);
1981 end if;
1983 return Res;
1985 exception
1986 when RE_Not_Available =>
1987 return Empty_List;
1988 end Build_Assignment;
1990 ------------------------------------
1991 -- Build_Discriminant_Assignments --
1992 ------------------------------------
1994 procedure Build_Discriminant_Assignments (Statement_List : List_Id) is
1995 Is_Tagged : constant Boolean := Is_Tagged_Type (Rec_Type);
1996 D : Entity_Id;
1997 D_Loc : Source_Ptr;
1999 begin
2000 if Has_Discriminants (Rec_Type)
2001 and then not Is_Unchecked_Union (Rec_Type)
2002 then
2003 D := First_Discriminant (Rec_Type);
2004 while Present (D) loop
2006 -- Don't generate the assignment for discriminants in derived
2007 -- tagged types if the discriminant is a renaming of some
2008 -- ancestor discriminant. This initialization will be done
2009 -- when initializing the _parent field of the derived record.
2011 if Is_Tagged
2012 and then Present (Corresponding_Discriminant (D))
2013 then
2014 null;
2016 else
2017 D_Loc := Sloc (D);
2018 Append_List_To (Statement_List,
2019 Build_Assignment (D,
2020 New_Occurrence_Of (Discriminal (D), D_Loc)));
2021 end if;
2023 Next_Discriminant (D);
2024 end loop;
2025 end if;
2026 end Build_Discriminant_Assignments;
2028 --------------------------
2029 -- Build_Init_Call_Thru --
2030 --------------------------
2032 function Build_Init_Call_Thru (Parameters : List_Id) return List_Id is
2033 Parent_Proc : constant Entity_Id :=
2034 Base_Init_Proc (Etype (Rec_Type));
2036 Parent_Type : constant Entity_Id :=
2037 Etype (First_Formal (Parent_Proc));
2039 Uparent_Type : constant Entity_Id :=
2040 Underlying_Type (Parent_Type);
2042 First_Discr_Param : Node_Id;
2044 Arg : Node_Id;
2045 Args : List_Id;
2046 First_Arg : Node_Id;
2047 Parent_Discr : Entity_Id;
2048 Res : List_Id;
2050 begin
2051 -- First argument (_Init) is the object to be initialized.
2052 -- ??? not sure where to get a reasonable Loc for First_Arg
2054 First_Arg :=
2055 OK_Convert_To (Parent_Type,
2056 New_Occurrence_Of
2057 (Defining_Identifier (First (Parameters)), Loc));
2059 Set_Etype (First_Arg, Parent_Type);
2061 Args := New_List (Convert_Concurrent (First_Arg, Rec_Type));
2063 -- In the tasks case,
2064 -- add _Master as the value of the _Master parameter
2065 -- add _Chain as the value of the _Chain parameter.
2066 -- add _Task_Name as the value of the _Task_Name parameter.
2067 -- At the outer level, these will be variables holding the
2068 -- corresponding values obtained from GNARL or the expander.
2070 -- At inner levels, they will be the parameters passed down through
2071 -- the outer routines.
2073 First_Discr_Param := Next (First (Parameters));
2075 if Has_Task (Rec_Type) then
2076 if Restriction_Active (No_Task_Hierarchy) then
2077 Append_To (Args,
2078 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
2079 else
2080 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
2081 end if;
2083 -- Add _Chain (not done for sequential elaboration policy, see
2084 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
2086 if Partition_Elaboration_Policy /= 'S' then
2087 Append_To (Args, Make_Identifier (Loc, Name_uChain));
2088 end if;
2090 Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
2091 First_Discr_Param := Next (Next (Next (First_Discr_Param)));
2092 end if;
2094 -- Append discriminant values
2096 if Has_Discriminants (Uparent_Type) then
2097 pragma Assert (not Is_Tagged_Type (Uparent_Type));
2099 Parent_Discr := First_Discriminant (Uparent_Type);
2100 while Present (Parent_Discr) loop
2102 -- Get the initial value for this discriminant
2103 -- ??? needs to be cleaned up to use parent_Discr_Constr
2104 -- directly.
2106 declare
2107 Discr : Entity_Id :=
2108 First_Stored_Discriminant (Uparent_Type);
2110 Discr_Value : Elmt_Id :=
2111 First_Elmt (Stored_Constraint (Rec_Type));
2113 begin
2114 while Original_Record_Component (Parent_Discr) /= Discr loop
2115 Next_Stored_Discriminant (Discr);
2116 Next_Elmt (Discr_Value);
2117 end loop;
2119 Arg := Node (Discr_Value);
2120 end;
2122 -- Append it to the list
2124 if Nkind (Arg) = N_Identifier
2125 and then Ekind (Entity (Arg)) = E_Discriminant
2126 then
2127 Append_To (Args,
2128 New_Occurrence_Of (Discriminal (Entity (Arg)), Loc));
2130 -- Case of access discriminants. We replace the reference
2131 -- to the type by a reference to the actual object.
2133 -- Is above comment right??? Use of New_Copy below seems mighty
2134 -- suspicious ???
2136 else
2137 Append_To (Args, New_Copy (Arg));
2138 end if;
2140 Next_Discriminant (Parent_Discr);
2141 end loop;
2142 end if;
2144 Res :=
2145 New_List (
2146 Make_Procedure_Call_Statement (Loc,
2147 Name =>
2148 New_Occurrence_Of (Parent_Proc, Loc),
2149 Parameter_Associations => Args));
2151 return Res;
2152 end Build_Init_Call_Thru;
2154 -----------------------------------
2155 -- Build_Offset_To_Top_Functions --
2156 -----------------------------------
2158 procedure Build_Offset_To_Top_Functions is
2160 procedure Build_Offset_To_Top_Function (Iface_Comp : Entity_Id);
2161 -- Generate:
2162 -- function Fxx (O : Address) return Storage_Offset is
2163 -- type Acc is access all <Typ>;
2164 -- begin
2165 -- return Acc!(O).Iface_Comp'Position;
2166 -- end Fxx;
2168 ----------------------------------
2169 -- Build_Offset_To_Top_Function --
2170 ----------------------------------
2172 procedure Build_Offset_To_Top_Function (Iface_Comp : Entity_Id) is
2173 Body_Node : Node_Id;
2174 Func_Id : Entity_Id;
2175 Spec_Node : Node_Id;
2176 Acc_Type : Entity_Id;
2178 begin
2179 Func_Id := Make_Temporary (Loc, 'F');
2180 Set_DT_Offset_To_Top_Func (Iface_Comp, Func_Id);
2182 -- Generate
2183 -- function Fxx (O : in Rec_Typ) return Storage_Offset;
2185 Spec_Node := New_Node (N_Function_Specification, Loc);
2186 Set_Defining_Unit_Name (Spec_Node, Func_Id);
2187 Set_Parameter_Specifications (Spec_Node, New_List (
2188 Make_Parameter_Specification (Loc,
2189 Defining_Identifier =>
2190 Make_Defining_Identifier (Loc, Name_uO),
2191 In_Present => True,
2192 Parameter_Type =>
2193 New_Occurrence_Of (RTE (RE_Address), Loc))));
2194 Set_Result_Definition (Spec_Node,
2195 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
2197 -- Generate
2198 -- function Fxx (O : in Rec_Typ) return Storage_Offset is
2199 -- begin
2200 -- return -O.Iface_Comp'Position;
2201 -- end Fxx;
2203 Body_Node := New_Node (N_Subprogram_Body, Loc);
2204 Set_Specification (Body_Node, Spec_Node);
2206 Acc_Type := Make_Temporary (Loc, 'T');
2207 Set_Declarations (Body_Node, New_List (
2208 Make_Full_Type_Declaration (Loc,
2209 Defining_Identifier => Acc_Type,
2210 Type_Definition =>
2211 Make_Access_To_Object_Definition (Loc,
2212 All_Present => True,
2213 Null_Exclusion_Present => False,
2214 Constant_Present => False,
2215 Subtype_Indication =>
2216 New_Occurrence_Of (Rec_Type, Loc)))));
2218 Set_Handled_Statement_Sequence (Body_Node,
2219 Make_Handled_Sequence_Of_Statements (Loc,
2220 Statements => New_List (
2221 Make_Simple_Return_Statement (Loc,
2222 Expression =>
2223 Make_Op_Minus (Loc,
2224 Make_Attribute_Reference (Loc,
2225 Prefix =>
2226 Make_Selected_Component (Loc,
2227 Prefix =>
2228 Unchecked_Convert_To (Acc_Type,
2229 Make_Identifier (Loc, Name_uO)),
2230 Selector_Name =>
2231 New_Occurrence_Of (Iface_Comp, Loc)),
2232 Attribute_Name => Name_Position))))));
2234 Set_Ekind (Func_Id, E_Function);
2235 Set_Mechanism (Func_Id, Default_Mechanism);
2236 Set_Is_Internal (Func_Id, True);
2238 if not Debug_Generated_Code then
2239 Set_Debug_Info_Off (Func_Id);
2240 end if;
2242 Analyze (Body_Node);
2244 Append_Freeze_Action (Rec_Type, Body_Node);
2245 end Build_Offset_To_Top_Function;
2247 -- Local variables
2249 Iface_Comp : Node_Id;
2250 Iface_Comp_Elmt : Elmt_Id;
2251 Ifaces_Comp_List : Elist_Id;
2253 -- Start of processing for Build_Offset_To_Top_Functions
2255 begin
2256 -- Offset_To_Top_Functions are built only for derivations of types
2257 -- with discriminants that cover interface types.
2258 -- Nothing is needed either in case of virtual targets, since
2259 -- interfaces are handled directly by the target.
2261 if not Is_Tagged_Type (Rec_Type)
2262 or else Etype (Rec_Type) = Rec_Type
2263 or else not Has_Discriminants (Etype (Rec_Type))
2264 or else not Tagged_Type_Expansion
2265 then
2266 return;
2267 end if;
2269 Collect_Interface_Components (Rec_Type, Ifaces_Comp_List);
2271 -- For each interface type with secondary dispatch table we generate
2272 -- the Offset_To_Top_Functions (required to displace the pointer in
2273 -- interface conversions)
2275 Iface_Comp_Elmt := First_Elmt (Ifaces_Comp_List);
2276 while Present (Iface_Comp_Elmt) loop
2277 Iface_Comp := Node (Iface_Comp_Elmt);
2278 pragma Assert (Is_Interface (Related_Type (Iface_Comp)));
2280 -- If the interface is a parent of Rec_Type it shares the primary
2281 -- dispatch table and hence there is no need to build the function
2283 if not Is_Ancestor (Related_Type (Iface_Comp), Rec_Type,
2284 Use_Full_View => True)
2285 then
2286 Build_Offset_To_Top_Function (Iface_Comp);
2287 end if;
2289 Next_Elmt (Iface_Comp_Elmt);
2290 end loop;
2291 end Build_Offset_To_Top_Functions;
2293 ------------------------------
2294 -- Build_CPP_Init_Procedure --
2295 ------------------------------
2297 procedure Build_CPP_Init_Procedure is
2298 Body_Node : Node_Id;
2299 Body_Stmts : List_Id;
2300 Flag_Id : Entity_Id;
2301 Handled_Stmt_Node : Node_Id;
2302 Init_Tags_List : List_Id;
2303 Proc_Id : Entity_Id;
2304 Proc_Spec_Node : Node_Id;
2306 begin
2307 -- Check cases requiring no IC routine
2309 if not Is_CPP_Class (Root_Type (Rec_Type))
2310 or else Is_CPP_Class (Rec_Type)
2311 or else CPP_Num_Prims (Rec_Type) = 0
2312 or else not Tagged_Type_Expansion
2313 or else No_Run_Time_Mode
2314 then
2315 return;
2316 end if;
2318 -- Generate:
2320 -- Flag : Boolean := False;
2322 -- procedure Typ_IC is
2323 -- begin
2324 -- if not Flag then
2325 -- Copy C++ dispatch table slots from parent
2326 -- Update C++ slots of overridden primitives
2327 -- end if;
2328 -- end;
2330 Flag_Id := Make_Temporary (Loc, 'F');
2332 Append_Freeze_Action (Rec_Type,
2333 Make_Object_Declaration (Loc,
2334 Defining_Identifier => Flag_Id,
2335 Object_Definition =>
2336 New_Occurrence_Of (Standard_Boolean, Loc),
2337 Expression =>
2338 New_Occurrence_Of (Standard_True, Loc)));
2340 Body_Stmts := New_List;
2341 Body_Node := New_Node (N_Subprogram_Body, Loc);
2343 Proc_Spec_Node := New_Node (N_Procedure_Specification, Loc);
2345 Proc_Id :=
2346 Make_Defining_Identifier (Loc,
2347 Chars => Make_TSS_Name (Rec_Type, TSS_CPP_Init_Proc));
2349 Set_Ekind (Proc_Id, E_Procedure);
2350 Set_Is_Internal (Proc_Id);
2352 Set_Defining_Unit_Name (Proc_Spec_Node, Proc_Id);
2354 Set_Parameter_Specifications (Proc_Spec_Node, New_List);
2355 Set_Specification (Body_Node, Proc_Spec_Node);
2356 Set_Declarations (Body_Node, New_List);
2358 Init_Tags_List := Build_Inherit_CPP_Prims (Rec_Type);
2360 Append_To (Init_Tags_List,
2361 Make_Assignment_Statement (Loc,
2362 Name =>
2363 New_Occurrence_Of (Flag_Id, Loc),
2364 Expression =>
2365 New_Occurrence_Of (Standard_False, Loc)));
2367 Append_To (Body_Stmts,
2368 Make_If_Statement (Loc,
2369 Condition => New_Occurrence_Of (Flag_Id, Loc),
2370 Then_Statements => Init_Tags_List));
2372 Handled_Stmt_Node :=
2373 New_Node (N_Handled_Sequence_Of_Statements, Loc);
2374 Set_Statements (Handled_Stmt_Node, Body_Stmts);
2375 Set_Exception_Handlers (Handled_Stmt_Node, No_List);
2376 Set_Handled_Statement_Sequence (Body_Node, Handled_Stmt_Node);
2378 if not Debug_Generated_Code then
2379 Set_Debug_Info_Off (Proc_Id);
2380 end if;
2382 -- Associate CPP_Init_Proc with type
2384 Set_Init_Proc (Rec_Type, Proc_Id);
2385 end Build_CPP_Init_Procedure;
2387 --------------------------
2388 -- Build_Init_Procedure --
2389 --------------------------
2391 procedure Build_Init_Procedure is
2392 Body_Stmts : List_Id;
2393 Body_Node : Node_Id;
2394 Handled_Stmt_Node : Node_Id;
2395 Init_Tags_List : List_Id;
2396 Parameters : List_Id;
2397 Proc_Spec_Node : Node_Id;
2398 Record_Extension_Node : Node_Id;
2400 begin
2401 Body_Stmts := New_List;
2402 Body_Node := New_Node (N_Subprogram_Body, Loc);
2403 Set_Ekind (Proc_Id, E_Procedure);
2405 Proc_Spec_Node := New_Node (N_Procedure_Specification, Loc);
2406 Set_Defining_Unit_Name (Proc_Spec_Node, Proc_Id);
2408 Parameters := Init_Formals (Rec_Type);
2409 Append_List_To (Parameters,
2410 Build_Discriminant_Formals (Rec_Type, True));
2412 -- For tagged types, we add a flag to indicate whether the routine
2413 -- is called to initialize a parent component in the init_proc of
2414 -- a type extension. If the flag is false, we do not set the tag
2415 -- because it has been set already in the extension.
2417 if Is_Tagged_Type (Rec_Type) then
2418 Set_Tag := Make_Temporary (Loc, 'P');
2420 Append_To (Parameters,
2421 Make_Parameter_Specification (Loc,
2422 Defining_Identifier => Set_Tag,
2423 Parameter_Type =>
2424 New_Occurrence_Of (Standard_Boolean, Loc),
2425 Expression =>
2426 New_Occurrence_Of (Standard_True, Loc)));
2427 end if;
2429 Set_Parameter_Specifications (Proc_Spec_Node, Parameters);
2430 Set_Specification (Body_Node, Proc_Spec_Node);
2431 Set_Declarations (Body_Node, Decls);
2433 -- N is a Derived_Type_Definition that renames the parameters of the
2434 -- ancestor type. We initialize it by expanding our discriminants and
2435 -- call the ancestor _init_proc with a type-converted object.
2437 if Parent_Subtype_Renaming_Discrims then
2438 Append_List_To (Body_Stmts, Build_Init_Call_Thru (Parameters));
2440 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
2441 Build_Discriminant_Assignments (Body_Stmts);
2443 if not Null_Present (Type_Definition (N)) then
2444 Append_List_To (Body_Stmts,
2445 Build_Init_Statements (Component_List (Type_Definition (N))));
2446 end if;
2448 -- N is a Derived_Type_Definition with a possible non-empty
2449 -- extension. The initialization of a type extension consists in the
2450 -- initialization of the components in the extension.
2452 else
2453 Build_Discriminant_Assignments (Body_Stmts);
2455 Record_Extension_Node :=
2456 Record_Extension_Part (Type_Definition (N));
2458 if not Null_Present (Record_Extension_Node) then
2459 declare
2460 Stmts : constant List_Id :=
2461 Build_Init_Statements (
2462 Component_List (Record_Extension_Node));
2464 begin
2465 -- The parent field must be initialized first because the
2466 -- offset of the new discriminants may depend on it. This is
2467 -- not needed if the parent is an interface type because in
2468 -- such case the initialization of the _parent field was not
2469 -- generated.
2471 if not Is_Interface (Etype (Rec_Ent)) then
2472 declare
2473 Parent_IP : constant Name_Id :=
2474 Make_Init_Proc_Name (Etype (Rec_Ent));
2475 Stmt : Node_Id;
2476 IP_Call : Node_Id;
2477 IP_Stmts : List_Id;
2479 begin
2480 -- Look for a call to the parent IP at the beginning
2481 -- of Stmts associated with the record extension
2483 Stmt := First (Stmts);
2484 IP_Call := Empty;
2485 while Present (Stmt) loop
2486 if Nkind (Stmt) = N_Procedure_Call_Statement
2487 and then Chars (Name (Stmt)) = Parent_IP
2488 then
2489 IP_Call := Stmt;
2490 exit;
2491 end if;
2493 Next (Stmt);
2494 end loop;
2496 -- If found then move it to the beginning of the
2497 -- statements of this IP routine
2499 if Present (IP_Call) then
2500 IP_Stmts := New_List;
2501 loop
2502 Stmt := Remove_Head (Stmts);
2503 Append_To (IP_Stmts, Stmt);
2504 exit when Stmt = IP_Call;
2505 end loop;
2507 Prepend_List_To (Body_Stmts, IP_Stmts);
2508 end if;
2509 end;
2510 end if;
2512 Append_List_To (Body_Stmts, Stmts);
2513 end;
2514 end if;
2515 end if;
2517 -- Add here the assignment to instantiate the Tag
2519 -- The assignment corresponds to the code:
2521 -- _Init._Tag := Typ'Tag;
2523 -- Suppress the tag assignment when not Tagged_Type_Expansion because
2524 -- tags are represented implicitly in objects. It is also suppressed
2525 -- in case of CPP_Class types because in this case the tag is
2526 -- initialized in the C++ side.
2528 if Is_Tagged_Type (Rec_Type)
2529 and then Tagged_Type_Expansion
2530 and then not No_Run_Time_Mode
2531 then
2532 -- Case 1: Ada tagged types with no CPP ancestor. Set the tags of
2533 -- the actual object and invoke the IP of the parent (in this
2534 -- order). The tag must be initialized before the call to the IP
2535 -- of the parent and the assignments to other components because
2536 -- the initial value of the components may depend on the tag (eg.
2537 -- through a dispatching operation on an access to the current
2538 -- type). The tag assignment is not done when initializing the
2539 -- parent component of a type extension, because in that case the
2540 -- tag is set in the extension.
2542 if not Is_CPP_Class (Root_Type (Rec_Type)) then
2544 -- Initialize the primary tag component
2546 Init_Tags_List := New_List (
2547 Make_Assignment_Statement (Loc,
2548 Name =>
2549 Make_Selected_Component (Loc,
2550 Prefix => Make_Identifier (Loc, Name_uInit),
2551 Selector_Name =>
2552 New_Occurrence_Of
2553 (First_Tag_Component (Rec_Type), Loc)),
2554 Expression =>
2555 New_Occurrence_Of
2556 (Node
2557 (First_Elmt (Access_Disp_Table (Rec_Type))), Loc)));
2559 -- Ada 2005 (AI-251): Initialize the secondary tags components
2560 -- located at fixed positions (tags whose position depends on
2561 -- variable size components are initialized later ---see below)
2563 if Ada_Version >= Ada_2005
2564 and then not Is_Interface (Rec_Type)
2565 and then Has_Interfaces (Rec_Type)
2566 then
2567 declare
2568 Elab_Sec_DT_Stmts_List : constant List_Id := New_List;
2569 Elab_List : List_Id := New_List;
2571 begin
2572 Init_Secondary_Tags
2573 (Typ => Rec_Type,
2574 Target => Make_Identifier (Loc, Name_uInit),
2575 Init_Tags_List => Init_Tags_List,
2576 Stmts_List => Elab_Sec_DT_Stmts_List,
2577 Fixed_Comps => True,
2578 Variable_Comps => False);
2580 Elab_List := New_List (
2581 Make_If_Statement (Loc,
2582 Condition => New_Occurrence_Of (Set_Tag, Loc),
2583 Then_Statements => Init_Tags_List));
2585 if Elab_Flag_Needed (Rec_Type) then
2586 Append_To (Elab_Sec_DT_Stmts_List,
2587 Make_Assignment_Statement (Loc,
2588 Name =>
2589 New_Occurrence_Of
2590 (Access_Disp_Table_Elab_Flag (Rec_Type),
2591 Loc),
2592 Expression =>
2593 New_Occurrence_Of (Standard_False, Loc)));
2595 Append_To (Elab_List,
2596 Make_If_Statement (Loc,
2597 Condition =>
2598 New_Occurrence_Of
2599 (Access_Disp_Table_Elab_Flag (Rec_Type), Loc),
2600 Then_Statements => Elab_Sec_DT_Stmts_List));
2601 end if;
2603 Prepend_List_To (Body_Stmts, Elab_List);
2604 end;
2605 else
2606 Prepend_To (Body_Stmts,
2607 Make_If_Statement (Loc,
2608 Condition => New_Occurrence_Of (Set_Tag, Loc),
2609 Then_Statements => Init_Tags_List));
2610 end if;
2612 -- Case 2: CPP type. The imported C++ constructor takes care of
2613 -- tags initialization. No action needed here because the IP
2614 -- is built by Set_CPP_Constructors; in this case the IP is a
2615 -- wrapper that invokes the C++ constructor and copies the C++
2616 -- tags locally. Done to inherit the C++ slots in Ada derivations
2617 -- (see case 3).
2619 elsif Is_CPP_Class (Rec_Type) then
2620 pragma Assert (False);
2621 null;
2623 -- Case 3: Combined hierarchy containing C++ types and Ada tagged
2624 -- type derivations. Derivations of imported C++ classes add a
2625 -- complication, because we cannot inhibit tag setting in the
2626 -- constructor for the parent. Hence we initialize the tag after
2627 -- the call to the parent IP (that is, in reverse order compared
2628 -- with pure Ada hierarchies ---see comment on case 1).
2630 else
2631 -- Initialize the primary tag
2633 Init_Tags_List := New_List (
2634 Make_Assignment_Statement (Loc,
2635 Name =>
2636 Make_Selected_Component (Loc,
2637 Prefix => Make_Identifier (Loc, Name_uInit),
2638 Selector_Name =>
2639 New_Occurrence_Of
2640 (First_Tag_Component (Rec_Type), Loc)),
2641 Expression =>
2642 New_Occurrence_Of
2643 (Node
2644 (First_Elmt (Access_Disp_Table (Rec_Type))), Loc)));
2646 -- Ada 2005 (AI-251): Initialize the secondary tags components
2647 -- located at fixed positions (tags whose position depends on
2648 -- variable size components are initialized later ---see below)
2650 if Ada_Version >= Ada_2005
2651 and then not Is_Interface (Rec_Type)
2652 and then Has_Interfaces (Rec_Type)
2653 then
2654 Init_Secondary_Tags
2655 (Typ => Rec_Type,
2656 Target => Make_Identifier (Loc, Name_uInit),
2657 Init_Tags_List => Init_Tags_List,
2658 Stmts_List => Init_Tags_List,
2659 Fixed_Comps => True,
2660 Variable_Comps => False);
2661 end if;
2663 -- Initialize the tag component after invocation of parent IP.
2665 -- Generate:
2666 -- parent_IP(_init.parent); // Invokes the C++ constructor
2667 -- [ typIC; ] // Inherit C++ slots from parent
2668 -- init_tags
2670 declare
2671 Ins_Nod : Node_Id;
2673 begin
2674 -- Search for the call to the IP of the parent. We assume
2675 -- that the first init_proc call is for the parent.
2677 Ins_Nod := First (Body_Stmts);
2678 while Present (Next (Ins_Nod))
2679 and then (Nkind (Ins_Nod) /= N_Procedure_Call_Statement
2680 or else not Is_Init_Proc (Name (Ins_Nod)))
2681 loop
2682 Next (Ins_Nod);
2683 end loop;
2685 -- The IC routine copies the inherited slots of the C+ part
2686 -- of the dispatch table from the parent and updates the
2687 -- overridden C++ slots.
2689 if CPP_Num_Prims (Rec_Type) > 0 then
2690 declare
2691 Init_DT : Entity_Id;
2692 New_Nod : Node_Id;
2694 begin
2695 Init_DT := CPP_Init_Proc (Rec_Type);
2696 pragma Assert (Present (Init_DT));
2698 New_Nod :=
2699 Make_Procedure_Call_Statement (Loc,
2700 New_Occurrence_Of (Init_DT, Loc));
2701 Insert_After (Ins_Nod, New_Nod);
2703 -- Update location of init tag statements
2705 Ins_Nod := New_Nod;
2706 end;
2707 end if;
2709 Insert_List_After (Ins_Nod, Init_Tags_List);
2710 end;
2711 end if;
2713 -- Ada 2005 (AI-251): Initialize the secondary tag components
2714 -- located at variable positions. We delay the generation of this
2715 -- code until here because the value of the attribute 'Position
2716 -- applied to variable size components of the parent type that
2717 -- depend on discriminants is only safely read at runtime after
2718 -- the parent components have been initialized.
2720 if Ada_Version >= Ada_2005
2721 and then not Is_Interface (Rec_Type)
2722 and then Has_Interfaces (Rec_Type)
2723 and then Has_Discriminants (Etype (Rec_Type))
2724 and then Is_Variable_Size_Record (Etype (Rec_Type))
2725 then
2726 Init_Tags_List := New_List;
2728 Init_Secondary_Tags
2729 (Typ => Rec_Type,
2730 Target => Make_Identifier (Loc, Name_uInit),
2731 Init_Tags_List => Init_Tags_List,
2732 Stmts_List => Init_Tags_List,
2733 Fixed_Comps => False,
2734 Variable_Comps => True);
2736 if Is_Non_Empty_List (Init_Tags_List) then
2737 Append_List_To (Body_Stmts, Init_Tags_List);
2738 end if;
2739 end if;
2740 end if;
2742 Handled_Stmt_Node := New_Node (N_Handled_Sequence_Of_Statements, Loc);
2743 Set_Statements (Handled_Stmt_Node, Body_Stmts);
2745 -- Generate:
2746 -- Deep_Finalize (_init, C1, ..., CN);
2747 -- raise;
2749 if Counter > 0
2750 and then Needs_Finalization (Rec_Type)
2751 and then not Is_Abstract_Type (Rec_Type)
2752 and then not Restriction_Active (No_Exception_Propagation)
2753 then
2754 declare
2755 DF_Call : Node_Id;
2756 DF_Id : Entity_Id;
2758 begin
2759 -- Create a local version of Deep_Finalize which has indication
2760 -- of partial initialization state.
2762 DF_Id :=
2763 Make_Defining_Identifier (Loc,
2764 Chars => New_External_Name (Name_uFinalizer));
2766 Append_To (Decls, Make_Local_Deep_Finalize (Rec_Type, DF_Id));
2768 DF_Call :=
2769 Make_Procedure_Call_Statement (Loc,
2770 Name => New_Occurrence_Of (DF_Id, Loc),
2771 Parameter_Associations => New_List (
2772 Make_Identifier (Loc, Name_uInit),
2773 New_Occurrence_Of (Standard_False, Loc)));
2775 -- Do not emit warnings related to the elaboration order when a
2776 -- controlled object is declared before the body of Finalize is
2777 -- seen.
2779 if Legacy_Elaboration_Checks then
2780 Set_No_Elaboration_Check (DF_Call);
2781 end if;
2783 Set_Exception_Handlers (Handled_Stmt_Node, New_List (
2784 Make_Exception_Handler (Loc,
2785 Exception_Choices => New_List (
2786 Make_Others_Choice (Loc)),
2787 Statements => New_List (
2788 DF_Call,
2789 Make_Raise_Statement (Loc)))));
2790 end;
2791 else
2792 Set_Exception_Handlers (Handled_Stmt_Node, No_List);
2793 end if;
2795 Set_Handled_Statement_Sequence (Body_Node, Handled_Stmt_Node);
2797 if not Debug_Generated_Code then
2798 Set_Debug_Info_Off (Proc_Id);
2799 end if;
2801 -- Associate Init_Proc with type, and determine if the procedure
2802 -- is null (happens because of the Initialize_Scalars pragma case,
2803 -- where we have to generate a null procedure in case it is called
2804 -- by a client with Initialize_Scalars set). Such procedures have
2805 -- to be generated, but do not have to be called, so we mark them
2806 -- as null to suppress the call.
2808 Set_Init_Proc (Rec_Type, Proc_Id);
2810 if List_Length (Body_Stmts) = 1
2812 -- We must skip SCIL nodes because they may have been added to this
2813 -- list by Insert_Actions.
2815 and then Nkind (First_Non_SCIL_Node (Body_Stmts)) = N_Null_Statement
2816 then
2817 Set_Is_Null_Init_Proc (Proc_Id);
2818 end if;
2819 end Build_Init_Procedure;
2821 ---------------------------
2822 -- Build_Init_Statements --
2823 ---------------------------
2825 function Build_Init_Statements (Comp_List : Node_Id) return List_Id is
2826 Checks : constant List_Id := New_List;
2827 Actions : List_Id := No_List;
2828 Counter_Id : Entity_Id := Empty;
2829 Comp_Loc : Source_Ptr;
2830 Decl : Node_Id;
2831 Has_POC : Boolean;
2832 Id : Entity_Id;
2833 Parent_Stmts : List_Id;
2834 Stmts : List_Id;
2835 Typ : Entity_Id;
2837 procedure Increment_Counter (Loc : Source_Ptr);
2838 -- Generate an "increment by one" statement for the current counter
2839 -- and append it to the list Stmts.
2841 procedure Make_Counter (Loc : Source_Ptr);
2842 -- Create a new counter for the current component list. The routine
2843 -- creates a new defining Id, adds an object declaration and sets
2844 -- the Id generator for the next variant.
2846 -----------------------
2847 -- Increment_Counter --
2848 -----------------------
2850 procedure Increment_Counter (Loc : Source_Ptr) is
2851 begin
2852 -- Generate:
2853 -- Counter := Counter + 1;
2855 Append_To (Stmts,
2856 Make_Assignment_Statement (Loc,
2857 Name => New_Occurrence_Of (Counter_Id, Loc),
2858 Expression =>
2859 Make_Op_Add (Loc,
2860 Left_Opnd => New_Occurrence_Of (Counter_Id, Loc),
2861 Right_Opnd => Make_Integer_Literal (Loc, 1))));
2862 end Increment_Counter;
2864 ------------------
2865 -- Make_Counter --
2866 ------------------
2868 procedure Make_Counter (Loc : Source_Ptr) is
2869 begin
2870 -- Increment the Id generator
2872 Counter := Counter + 1;
2874 -- Create the entity and declaration
2876 Counter_Id :=
2877 Make_Defining_Identifier (Loc,
2878 Chars => New_External_Name ('C', Counter));
2880 -- Generate:
2881 -- Cnn : Integer := 0;
2883 Append_To (Decls,
2884 Make_Object_Declaration (Loc,
2885 Defining_Identifier => Counter_Id,
2886 Object_Definition =>
2887 New_Occurrence_Of (Standard_Integer, Loc),
2888 Expression =>
2889 Make_Integer_Literal (Loc, 0)));
2890 end Make_Counter;
2892 -- Start of processing for Build_Init_Statements
2894 begin
2895 if Null_Present (Comp_List) then
2896 return New_List (Make_Null_Statement (Loc));
2897 end if;
2899 Parent_Stmts := New_List;
2900 Stmts := New_List;
2902 -- Loop through visible declarations of task types and protected
2903 -- types moving any expanded code from the spec to the body of the
2904 -- init procedure.
2906 if Is_Task_Record_Type (Rec_Type)
2907 or else Is_Protected_Record_Type (Rec_Type)
2908 then
2909 declare
2910 Decl : constant Node_Id :=
2911 Parent (Corresponding_Concurrent_Type (Rec_Type));
2912 Def : Node_Id;
2913 N1 : Node_Id;
2914 N2 : Node_Id;
2916 begin
2917 if Is_Task_Record_Type (Rec_Type) then
2918 Def := Task_Definition (Decl);
2919 else
2920 Def := Protected_Definition (Decl);
2921 end if;
2923 if Present (Def) then
2924 N1 := First (Visible_Declarations (Def));
2925 while Present (N1) loop
2926 N2 := N1;
2927 N1 := Next (N1);
2929 if Nkind (N2) in N_Statement_Other_Than_Procedure_Call
2930 or else Nkind (N2) in N_Raise_xxx_Error
2931 or else Nkind (N2) = N_Procedure_Call_Statement
2932 then
2933 Append_To (Stmts,
2934 New_Copy_Tree (N2, New_Scope => Proc_Id));
2935 Rewrite (N2, Make_Null_Statement (Sloc (N2)));
2936 Analyze (N2);
2937 end if;
2938 end loop;
2939 end if;
2940 end;
2941 end if;
2943 -- Loop through components, skipping pragmas, in 2 steps. The first
2944 -- step deals with regular components. The second step deals with
2945 -- components that have per object constraints and no explicit
2946 -- initialization.
2948 Has_POC := False;
2950 -- First pass : regular components
2952 Decl := First_Non_Pragma (Component_Items (Comp_List));
2953 while Present (Decl) loop
2954 Comp_Loc := Sloc (Decl);
2955 Build_Record_Checks
2956 (Subtype_Indication (Component_Definition (Decl)), Checks);
2958 Id := Defining_Identifier (Decl);
2959 Typ := Etype (Id);
2961 -- Leave any processing of per-object constrained component for
2962 -- the second pass.
2964 if Has_Access_Constraint (Id) and then No (Expression (Decl)) then
2965 Has_POC := True;
2967 -- Regular component cases
2969 else
2970 -- In the context of the init proc, references to discriminants
2971 -- resolve to denote the discriminals: this is where we can
2972 -- freeze discriminant dependent component subtypes.
2974 if not Is_Frozen (Typ) then
2975 Append_List_To (Stmts, Freeze_Entity (Typ, N));
2976 end if;
2978 -- Explicit initialization
2980 if Present (Expression (Decl)) then
2981 if Is_CPP_Constructor_Call (Expression (Decl)) then
2982 Actions :=
2983 Build_Initialization_Call
2984 (Comp_Loc,
2985 Id_Ref =>
2986 Make_Selected_Component (Comp_Loc,
2987 Prefix =>
2988 Make_Identifier (Comp_Loc, Name_uInit),
2989 Selector_Name =>
2990 New_Occurrence_Of (Id, Comp_Loc)),
2991 Typ => Typ,
2992 In_Init_Proc => True,
2993 Enclos_Type => Rec_Type,
2994 Discr_Map => Discr_Map,
2995 Constructor_Ref => Expression (Decl));
2996 else
2997 Actions := Build_Assignment (Id, Expression (Decl));
2998 end if;
3000 -- CPU, Dispatching_Domain, Priority, and Secondary_Stack_Size
3001 -- components are filled in with the corresponding rep-item
3002 -- expression of the concurrent type (if any).
3004 elsif Ekind (Scope (Id)) = E_Record_Type
3005 and then Present (Corresponding_Concurrent_Type (Scope (Id)))
3006 and then Nam_In (Chars (Id), Name_uCPU,
3007 Name_uDispatching_Domain,
3008 Name_uPriority,
3009 Name_uSecondary_Stack_Size)
3010 then
3011 declare
3012 Exp : Node_Id;
3013 Nam : Name_Id;
3014 pragma Warnings (Off, Nam);
3015 Ritem : Node_Id;
3017 begin
3018 if Chars (Id) = Name_uCPU then
3019 Nam := Name_CPU;
3021 elsif Chars (Id) = Name_uDispatching_Domain then
3022 Nam := Name_Dispatching_Domain;
3024 elsif Chars (Id) = Name_uPriority then
3025 Nam := Name_Priority;
3027 elsif Chars (Id) = Name_uSecondary_Stack_Size then
3028 Nam := Name_Secondary_Stack_Size;
3029 end if;
3031 -- Get the Rep Item (aspect specification, attribute
3032 -- definition clause or pragma) of the corresponding
3033 -- concurrent type.
3035 Ritem :=
3036 Get_Rep_Item
3037 (Corresponding_Concurrent_Type (Scope (Id)),
3038 Nam,
3039 Check_Parents => False);
3041 if Present (Ritem) then
3043 -- Pragma case
3045 if Nkind (Ritem) = N_Pragma then
3046 Exp := First (Pragma_Argument_Associations (Ritem));
3048 if Nkind (Exp) = N_Pragma_Argument_Association then
3049 Exp := Expression (Exp);
3050 end if;
3052 -- Conversion for Priority expression
3054 if Nam = Name_Priority then
3055 if Pragma_Name (Ritem) = Name_Priority
3056 and then not GNAT_Mode
3057 then
3058 Exp := Convert_To (RTE (RE_Priority), Exp);
3059 else
3060 Exp :=
3061 Convert_To (RTE (RE_Any_Priority), Exp);
3062 end if;
3063 end if;
3065 -- Aspect/Attribute definition clause case
3067 else
3068 Exp := Expression (Ritem);
3070 -- Conversion for Priority expression
3072 if Nam = Name_Priority then
3073 if Chars (Ritem) = Name_Priority
3074 and then not GNAT_Mode
3075 then
3076 Exp := Convert_To (RTE (RE_Priority), Exp);
3077 else
3078 Exp :=
3079 Convert_To (RTE (RE_Any_Priority), Exp);
3080 end if;
3081 end if;
3082 end if;
3084 -- Conversion for Dispatching_Domain value
3086 if Nam = Name_Dispatching_Domain then
3087 Exp :=
3088 Unchecked_Convert_To
3089 (RTE (RE_Dispatching_Domain_Access), Exp);
3091 -- Conversion for Secondary_Stack_Size value
3093 elsif Nam = Name_Secondary_Stack_Size then
3094 Exp := Convert_To (RTE (RE_Size_Type), Exp);
3095 end if;
3097 Actions := Build_Assignment (Id, Exp);
3099 -- Nothing needed if no Rep Item
3101 else
3102 Actions := No_List;
3103 end if;
3104 end;
3106 -- Composite component with its own Init_Proc
3108 elsif not Is_Interface (Typ)
3109 and then Has_Non_Null_Base_Init_Proc (Typ)
3110 then
3111 Actions :=
3112 Build_Initialization_Call
3113 (Comp_Loc,
3114 Make_Selected_Component (Comp_Loc,
3115 Prefix =>
3116 Make_Identifier (Comp_Loc, Name_uInit),
3117 Selector_Name => New_Occurrence_Of (Id, Comp_Loc)),
3118 Typ,
3119 In_Init_Proc => True,
3120 Enclos_Type => Rec_Type,
3121 Discr_Map => Discr_Map);
3123 Clean_Task_Names (Typ, Proc_Id);
3125 -- Simple initialization
3127 elsif Component_Needs_Simple_Initialization (Typ) then
3128 Actions :=
3129 Build_Assignment
3130 (Id => Id,
3131 Default =>
3132 Get_Simple_Init_Val
3133 (Typ => Typ,
3134 N => N,
3135 Size => Esize (Id)));
3137 -- Nothing needed for this case
3139 else
3140 Actions := No_List;
3141 end if;
3143 if Present (Checks) then
3144 if Chars (Id) = Name_uParent then
3145 Append_List_To (Parent_Stmts, Checks);
3146 else
3147 Append_List_To (Stmts, Checks);
3148 end if;
3149 end if;
3151 if Present (Actions) then
3152 if Chars (Id) = Name_uParent then
3153 Append_List_To (Parent_Stmts, Actions);
3155 else
3156 Append_List_To (Stmts, Actions);
3158 -- Preserve initialization state in the current counter
3160 if Needs_Finalization (Typ) then
3161 if No (Counter_Id) then
3162 Make_Counter (Comp_Loc);
3163 end if;
3165 Increment_Counter (Comp_Loc);
3166 end if;
3167 end if;
3168 end if;
3169 end if;
3171 Next_Non_Pragma (Decl);
3172 end loop;
3174 -- The parent field must be initialized first because variable
3175 -- size components of the parent affect the location of all the
3176 -- new components.
3178 Prepend_List_To (Stmts, Parent_Stmts);
3180 -- Set up tasks and protected object support. This needs to be done
3181 -- before any component with a per-object access discriminant
3182 -- constraint, or any variant part (which may contain such
3183 -- components) is initialized, because the initialization of these
3184 -- components may reference the enclosing concurrent object.
3186 -- For a task record type, add the task create call and calls to bind
3187 -- any interrupt (signal) entries.
3189 if Is_Task_Record_Type (Rec_Type) then
3191 -- In the case of the restricted run time the ATCB has already
3192 -- been preallocated.
3194 if Restricted_Profile then
3195 Append_To (Stmts,
3196 Make_Assignment_Statement (Loc,
3197 Name =>
3198 Make_Selected_Component (Loc,
3199 Prefix => Make_Identifier (Loc, Name_uInit),
3200 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)),
3201 Expression =>
3202 Make_Attribute_Reference (Loc,
3203 Prefix =>
3204 Make_Selected_Component (Loc,
3205 Prefix => Make_Identifier (Loc, Name_uInit),
3206 Selector_Name => Make_Identifier (Loc, Name_uATCB)),
3207 Attribute_Name => Name_Unchecked_Access)));
3208 end if;
3210 Append_To (Stmts, Make_Task_Create_Call (Rec_Type));
3212 declare
3213 Task_Type : constant Entity_Id :=
3214 Corresponding_Concurrent_Type (Rec_Type);
3215 Task_Decl : constant Node_Id := Parent (Task_Type);
3216 Task_Def : constant Node_Id := Task_Definition (Task_Decl);
3217 Decl_Loc : Source_Ptr;
3218 Ent : Entity_Id;
3219 Vis_Decl : Node_Id;
3221 begin
3222 if Present (Task_Def) then
3223 Vis_Decl := First (Visible_Declarations (Task_Def));
3224 while Present (Vis_Decl) loop
3225 Decl_Loc := Sloc (Vis_Decl);
3227 if Nkind (Vis_Decl) = N_Attribute_Definition_Clause then
3228 if Get_Attribute_Id (Chars (Vis_Decl)) =
3229 Attribute_Address
3230 then
3231 Ent := Entity (Name (Vis_Decl));
3233 if Ekind (Ent) = E_Entry then
3234 Append_To (Stmts,
3235 Make_Procedure_Call_Statement (Decl_Loc,
3236 Name =>
3237 New_Occurrence_Of (RTE (
3238 RE_Bind_Interrupt_To_Entry), Decl_Loc),
3239 Parameter_Associations => New_List (
3240 Make_Selected_Component (Decl_Loc,
3241 Prefix =>
3242 Make_Identifier (Decl_Loc, Name_uInit),
3243 Selector_Name =>
3244 Make_Identifier
3245 (Decl_Loc, Name_uTask_Id)),
3246 Entry_Index_Expression
3247 (Decl_Loc, Ent, Empty, Task_Type),
3248 Expression (Vis_Decl))));
3249 end if;
3250 end if;
3251 end if;
3253 Next (Vis_Decl);
3254 end loop;
3255 end if;
3256 end;
3257 end if;
3259 -- For a protected type, add statements generated by
3260 -- Make_Initialize_Protection.
3262 if Is_Protected_Record_Type (Rec_Type) then
3263 Append_List_To (Stmts,
3264 Make_Initialize_Protection (Rec_Type));
3265 end if;
3267 -- Second pass: components with per-object constraints
3269 if Has_POC then
3270 Decl := First_Non_Pragma (Component_Items (Comp_List));
3271 while Present (Decl) loop
3272 Comp_Loc := Sloc (Decl);
3273 Id := Defining_Identifier (Decl);
3274 Typ := Etype (Id);
3276 if Has_Access_Constraint (Id)
3277 and then No (Expression (Decl))
3278 then
3279 if Has_Non_Null_Base_Init_Proc (Typ) then
3280 Append_List_To (Stmts,
3281 Build_Initialization_Call (Comp_Loc,
3282 Make_Selected_Component (Comp_Loc,
3283 Prefix =>
3284 Make_Identifier (Comp_Loc, Name_uInit),
3285 Selector_Name => New_Occurrence_Of (Id, Comp_Loc)),
3286 Typ,
3287 In_Init_Proc => True,
3288 Enclos_Type => Rec_Type,
3289 Discr_Map => Discr_Map));
3291 Clean_Task_Names (Typ, Proc_Id);
3293 -- Preserve initialization state in the current counter
3295 if Needs_Finalization (Typ) then
3296 if No (Counter_Id) then
3297 Make_Counter (Comp_Loc);
3298 end if;
3300 Increment_Counter (Comp_Loc);
3301 end if;
3303 elsif Component_Needs_Simple_Initialization (Typ) then
3304 Append_List_To (Stmts,
3305 Build_Assignment
3306 (Id => Id,
3307 Default =>
3308 Get_Simple_Init_Val
3309 (Typ => Typ,
3310 N => N,
3311 Size => Esize (Id))));
3312 end if;
3313 end if;
3315 Next_Non_Pragma (Decl);
3316 end loop;
3317 end if;
3319 -- Process the variant part
3321 if Present (Variant_Part (Comp_List)) then
3322 declare
3323 Variant_Alts : constant List_Id := New_List;
3324 Var_Loc : Source_Ptr := No_Location;
3325 Variant : Node_Id;
3327 begin
3328 Variant :=
3329 First_Non_Pragma (Variants (Variant_Part (Comp_List)));
3330 while Present (Variant) loop
3331 Var_Loc := Sloc (Variant);
3332 Append_To (Variant_Alts,
3333 Make_Case_Statement_Alternative (Var_Loc,
3334 Discrete_Choices =>
3335 New_Copy_List (Discrete_Choices (Variant)),
3336 Statements =>
3337 Build_Init_Statements (Component_List (Variant))));
3338 Next_Non_Pragma (Variant);
3339 end loop;
3341 -- The expression of the case statement which is a reference
3342 -- to one of the discriminants is replaced by the appropriate
3343 -- formal parameter of the initialization procedure.
3345 Append_To (Stmts,
3346 Make_Case_Statement (Var_Loc,
3347 Expression =>
3348 New_Occurrence_Of (Discriminal (
3349 Entity (Name (Variant_Part (Comp_List)))), Var_Loc),
3350 Alternatives => Variant_Alts));
3351 end;
3352 end if;
3354 -- If no initializations when generated for component declarations
3355 -- corresponding to this Stmts, append a null statement to Stmts to
3356 -- to make it a valid Ada tree.
3358 if Is_Empty_List (Stmts) then
3359 Append (Make_Null_Statement (Loc), Stmts);
3360 end if;
3362 return Stmts;
3364 exception
3365 when RE_Not_Available =>
3366 return Empty_List;
3367 end Build_Init_Statements;
3369 -------------------------
3370 -- Build_Record_Checks --
3371 -------------------------
3373 procedure Build_Record_Checks (S : Node_Id; Check_List : List_Id) is
3374 Subtype_Mark_Id : Entity_Id;
3376 procedure Constrain_Array
3377 (SI : Node_Id;
3378 Check_List : List_Id);
3379 -- Apply a list of index constraints to an unconstrained array type.
3380 -- The first parameter is the entity for the resulting subtype.
3381 -- Check_List is a list to which the check actions are appended.
3383 ---------------------
3384 -- Constrain_Array --
3385 ---------------------
3387 procedure Constrain_Array
3388 (SI : Node_Id;
3389 Check_List : List_Id)
3391 C : constant Node_Id := Constraint (SI);
3392 Number_Of_Constraints : Nat := 0;
3393 Index : Node_Id;
3394 S, T : Entity_Id;
3396 procedure Constrain_Index
3397 (Index : Node_Id;
3398 S : Node_Id;
3399 Check_List : List_Id);
3400 -- Process an index constraint in a constrained array declaration.
3401 -- The constraint can be either a subtype name or a range with or
3402 -- without an explicit subtype mark. Index is the corresponding
3403 -- index of the unconstrained array. S is the range expression.
3404 -- Check_List is a list to which the check actions are appended.
3406 ---------------------
3407 -- Constrain_Index --
3408 ---------------------
3410 procedure Constrain_Index
3411 (Index : Node_Id;
3412 S : Node_Id;
3413 Check_List : List_Id)
3415 T : constant Entity_Id := Etype (Index);
3417 begin
3418 if Nkind (S) = N_Range then
3419 Process_Range_Expr_In_Decl (S, T, Check_List => Check_List);
3420 end if;
3421 end Constrain_Index;
3423 -- Start of processing for Constrain_Array
3425 begin
3426 T := Entity (Subtype_Mark (SI));
3428 if Is_Access_Type (T) then
3429 T := Designated_Type (T);
3430 end if;
3432 S := First (Constraints (C));
3433 while Present (S) loop
3434 Number_Of_Constraints := Number_Of_Constraints + 1;
3435 Next (S);
3436 end loop;
3438 -- In either case, the index constraint must provide a discrete
3439 -- range for each index of the array type and the type of each
3440 -- discrete range must be the same as that of the corresponding
3441 -- index. (RM 3.6.1)
3443 S := First (Constraints (C));
3444 Index := First_Index (T);
3445 Analyze (Index);
3447 -- Apply constraints to each index type
3449 for J in 1 .. Number_Of_Constraints loop
3450 Constrain_Index (Index, S, Check_List);
3451 Next (Index);
3452 Next (S);
3453 end loop;
3454 end Constrain_Array;
3456 -- Start of processing for Build_Record_Checks
3458 begin
3459 if Nkind (S) = N_Subtype_Indication then
3460 Find_Type (Subtype_Mark (S));
3461 Subtype_Mark_Id := Entity (Subtype_Mark (S));
3463 -- Remaining processing depends on type
3465 case Ekind (Subtype_Mark_Id) is
3466 when Array_Kind =>
3467 Constrain_Array (S, Check_List);
3469 when others =>
3470 null;
3471 end case;
3472 end if;
3473 end Build_Record_Checks;
3475 -------------------------------------------
3476 -- Component_Needs_Simple_Initialization --
3477 -------------------------------------------
3479 function Component_Needs_Simple_Initialization
3480 (T : Entity_Id) return Boolean
3482 begin
3483 return
3484 Needs_Simple_Initialization (T)
3485 and then not Is_RTE (T, RE_Tag)
3487 -- Ada 2005 (AI-251): Check also the tag of abstract interfaces
3489 and then not Is_RTE (T, RE_Interface_Tag);
3490 end Component_Needs_Simple_Initialization;
3492 --------------------------------------
3493 -- Parent_Subtype_Renaming_Discrims --
3494 --------------------------------------
3496 function Parent_Subtype_Renaming_Discrims return Boolean is
3497 De : Entity_Id;
3498 Dp : Entity_Id;
3500 begin
3501 if Base_Type (Rec_Ent) /= Rec_Ent then
3502 return False;
3503 end if;
3505 if Etype (Rec_Ent) = Rec_Ent
3506 or else not Has_Discriminants (Rec_Ent)
3507 or else Is_Constrained (Rec_Ent)
3508 or else Is_Tagged_Type (Rec_Ent)
3509 then
3510 return False;
3511 end if;
3513 -- If there are no explicit stored discriminants we have inherited
3514 -- the root type discriminants so far, so no renamings occurred.
3516 if First_Discriminant (Rec_Ent) =
3517 First_Stored_Discriminant (Rec_Ent)
3518 then
3519 return False;
3520 end if;
3522 -- Check if we have done some trivial renaming of the parent
3523 -- discriminants, i.e. something like
3525 -- type DT (X1, X2: int) is new PT (X1, X2);
3527 De := First_Discriminant (Rec_Ent);
3528 Dp := First_Discriminant (Etype (Rec_Ent));
3529 while Present (De) loop
3530 pragma Assert (Present (Dp));
3532 if Corresponding_Discriminant (De) /= Dp then
3533 return True;
3534 end if;
3536 Next_Discriminant (De);
3537 Next_Discriminant (Dp);
3538 end loop;
3540 return Present (Dp);
3541 end Parent_Subtype_Renaming_Discrims;
3543 ------------------------
3544 -- Requires_Init_Proc --
3545 ------------------------
3547 function Requires_Init_Proc (Rec_Id : Entity_Id) return Boolean is
3548 Comp_Decl : Node_Id;
3549 Id : Entity_Id;
3550 Typ : Entity_Id;
3552 begin
3553 -- Definitely do not need one if specifically suppressed
3555 if Initialization_Suppressed (Rec_Id) then
3556 return False;
3557 end if;
3559 -- If it is a type derived from a type with unknown discriminants,
3560 -- we cannot build an initialization procedure for it.
3562 if Has_Unknown_Discriminants (Rec_Id)
3563 or else Has_Unknown_Discriminants (Etype (Rec_Id))
3564 then
3565 return False;
3566 end if;
3568 -- Otherwise we need to generate an initialization procedure if
3569 -- Is_CPP_Class is False and at least one of the following applies:
3571 -- 1. Discriminants are present, since they need to be initialized
3572 -- with the appropriate discriminant constraint expressions.
3573 -- However, the discriminant of an unchecked union does not
3574 -- count, since the discriminant is not present.
3576 -- 2. The type is a tagged type, since the implicit Tag component
3577 -- needs to be initialized with a pointer to the dispatch table.
3579 -- 3. The type contains tasks
3581 -- 4. One or more components has an initial value
3583 -- 5. One or more components is for a type which itself requires
3584 -- an initialization procedure.
3586 -- 6. One or more components is a type that requires simple
3587 -- initialization (see Needs_Simple_Initialization), except
3588 -- that types Tag and Interface_Tag are excluded, since fields
3589 -- of these types are initialized by other means.
3591 -- 7. The type is the record type built for a task type (since at
3592 -- the very least, Create_Task must be called)
3594 -- 8. The type is the record type built for a protected type (since
3595 -- at least Initialize_Protection must be called)
3597 -- 9. The type is marked as a public entity. The reason we add this
3598 -- case (even if none of the above apply) is to properly handle
3599 -- Initialize_Scalars. If a package is compiled without an IS
3600 -- pragma, and the client is compiled with an IS pragma, then
3601 -- the client will think an initialization procedure is present
3602 -- and call it, when in fact no such procedure is required, but
3603 -- since the call is generated, there had better be a routine
3604 -- at the other end of the call, even if it does nothing).
3606 -- Note: the reason we exclude the CPP_Class case is because in this
3607 -- case the initialization is performed by the C++ constructors, and
3608 -- the IP is built by Set_CPP_Constructors.
3610 if Is_CPP_Class (Rec_Id) then
3611 return False;
3613 elsif Is_Interface (Rec_Id) then
3614 return False;
3616 elsif (Has_Discriminants (Rec_Id)
3617 and then not Is_Unchecked_Union (Rec_Id))
3618 or else Is_Tagged_Type (Rec_Id)
3619 or else Is_Concurrent_Record_Type (Rec_Id)
3620 or else Has_Task (Rec_Id)
3621 then
3622 return True;
3623 end if;
3625 Id := First_Component (Rec_Id);
3626 while Present (Id) loop
3627 Comp_Decl := Parent (Id);
3628 Typ := Etype (Id);
3630 if Present (Expression (Comp_Decl))
3631 or else Has_Non_Null_Base_Init_Proc (Typ)
3632 or else Component_Needs_Simple_Initialization (Typ)
3633 then
3634 return True;
3635 end if;
3637 Next_Component (Id);
3638 end loop;
3640 -- As explained above, a record initialization procedure is needed
3641 -- for public types in case Initialize_Scalars applies to a client.
3642 -- However, such a procedure is not needed in the case where either
3643 -- of restrictions No_Initialize_Scalars or No_Default_Initialization
3644 -- applies. No_Initialize_Scalars excludes the possibility of using
3645 -- Initialize_Scalars in any partition, and No_Default_Initialization
3646 -- implies that no initialization should ever be done for objects of
3647 -- the type, so is incompatible with Initialize_Scalars.
3649 if not Restriction_Active (No_Initialize_Scalars)
3650 and then not Restriction_Active (No_Default_Initialization)
3651 and then Is_Public (Rec_Id)
3652 then
3653 return True;
3654 end if;
3656 return False;
3657 end Requires_Init_Proc;
3659 -- Start of processing for Build_Record_Init_Proc
3661 begin
3662 Rec_Type := Defining_Identifier (N);
3664 -- This may be full declaration of a private type, in which case
3665 -- the visible entity is a record, and the private entity has been
3666 -- exchanged with it in the private part of the current package.
3667 -- The initialization procedure is built for the record type, which
3668 -- is retrievable from the private entity.
3670 if Is_Incomplete_Or_Private_Type (Rec_Type) then
3671 Rec_Type := Underlying_Type (Rec_Type);
3672 end if;
3674 -- If we have a variant record with restriction No_Implicit_Conditionals
3675 -- in effect, then we skip building the procedure. This is safe because
3676 -- if we can see the restriction, so can any caller, calls to initialize
3677 -- such records are not allowed for variant records if this restriction
3678 -- is active.
3680 if Has_Variant_Part (Rec_Type)
3681 and then Restriction_Active (No_Implicit_Conditionals)
3682 then
3683 return;
3684 end if;
3686 -- If there are discriminants, build the discriminant map to replace
3687 -- discriminants by their discriminals in complex bound expressions.
3688 -- These only arise for the corresponding records of synchronized types.
3690 if Is_Concurrent_Record_Type (Rec_Type)
3691 and then Has_Discriminants (Rec_Type)
3692 then
3693 declare
3694 Disc : Entity_Id;
3695 begin
3696 Disc := First_Discriminant (Rec_Type);
3697 while Present (Disc) loop
3698 Append_Elmt (Disc, Discr_Map);
3699 Append_Elmt (Discriminal (Disc), Discr_Map);
3700 Next_Discriminant (Disc);
3701 end loop;
3702 end;
3703 end if;
3705 -- Derived types that have no type extension can use the initialization
3706 -- procedure of their parent and do not need a procedure of their own.
3707 -- This is only correct if there are no representation clauses for the
3708 -- type or its parent, and if the parent has in fact been frozen so
3709 -- that its initialization procedure exists.
3711 if Is_Derived_Type (Rec_Type)
3712 and then not Is_Tagged_Type (Rec_Type)
3713 and then not Is_Unchecked_Union (Rec_Type)
3714 and then not Has_New_Non_Standard_Rep (Rec_Type)
3715 and then not Parent_Subtype_Renaming_Discrims
3716 and then Has_Non_Null_Base_Init_Proc (Etype (Rec_Type))
3717 then
3718 Copy_TSS (Base_Init_Proc (Etype (Rec_Type)), Rec_Type);
3720 -- Otherwise if we need an initialization procedure, then build one,
3721 -- mark it as public and inlinable and as having a completion.
3723 elsif Requires_Init_Proc (Rec_Type)
3724 or else Is_Unchecked_Union (Rec_Type)
3725 then
3726 Proc_Id :=
3727 Make_Defining_Identifier (Loc,
3728 Chars => Make_Init_Proc_Name (Rec_Type));
3730 -- If No_Default_Initialization restriction is active, then we don't
3731 -- want to build an init_proc, but we need to mark that an init_proc
3732 -- would be needed if this restriction was not active (so that we can
3733 -- detect attempts to call it), so set a dummy init_proc in place.
3735 if Restriction_Active (No_Default_Initialization) then
3736 Set_Init_Proc (Rec_Type, Proc_Id);
3737 return;
3738 end if;
3740 Build_Offset_To_Top_Functions;
3741 Build_CPP_Init_Procedure;
3742 Build_Init_Procedure;
3744 Set_Is_Public (Proc_Id, Is_Public (Rec_Ent));
3745 Set_Is_Internal (Proc_Id);
3746 Set_Has_Completion (Proc_Id);
3748 if not Debug_Generated_Code then
3749 Set_Debug_Info_Off (Proc_Id);
3750 end if;
3752 Set_Is_Inlined (Proc_Id, Inline_Init_Proc (Rec_Type));
3754 -- Do not build an aggregate if Modify_Tree_For_C, this isn't
3755 -- needed and may generate early references to non frozen types
3756 -- since we expand aggregate much more systematically.
3758 if Modify_Tree_For_C then
3759 return;
3760 end if;
3762 declare
3763 Agg : constant Node_Id :=
3764 Build_Equivalent_Record_Aggregate (Rec_Type);
3766 procedure Collect_Itypes (Comp : Node_Id);
3767 -- Generate references to itypes in the aggregate, because
3768 -- the first use of the aggregate may be in a nested scope.
3770 --------------------
3771 -- Collect_Itypes --
3772 --------------------
3774 procedure Collect_Itypes (Comp : Node_Id) is
3775 Ref : Node_Id;
3776 Sub_Aggr : Node_Id;
3777 Typ : constant Entity_Id := Etype (Comp);
3779 begin
3780 if Is_Array_Type (Typ) and then Is_Itype (Typ) then
3781 Ref := Make_Itype_Reference (Loc);
3782 Set_Itype (Ref, Typ);
3783 Append_Freeze_Action (Rec_Type, Ref);
3785 Ref := Make_Itype_Reference (Loc);
3786 Set_Itype (Ref, Etype (First_Index (Typ)));
3787 Append_Freeze_Action (Rec_Type, Ref);
3789 -- Recurse on nested arrays
3791 Sub_Aggr := First (Expressions (Comp));
3792 while Present (Sub_Aggr) loop
3793 Collect_Itypes (Sub_Aggr);
3794 Next (Sub_Aggr);
3795 end loop;
3796 end if;
3797 end Collect_Itypes;
3799 begin
3800 -- If there is a static initialization aggregate for the type,
3801 -- generate itype references for the types of its (sub)components,
3802 -- to prevent out-of-scope errors in the resulting tree.
3803 -- The aggregate may have been rewritten as a Raise node, in which
3804 -- case there are no relevant itypes.
3806 if Present (Agg) and then Nkind (Agg) = N_Aggregate then
3807 Set_Static_Initialization (Proc_Id, Agg);
3809 declare
3810 Comp : Node_Id;
3811 begin
3812 Comp := First (Component_Associations (Agg));
3813 while Present (Comp) loop
3814 Collect_Itypes (Expression (Comp));
3815 Next (Comp);
3816 end loop;
3817 end;
3818 end if;
3819 end;
3820 end if;
3821 end Build_Record_Init_Proc;
3823 ----------------------------
3824 -- Build_Slice_Assignment --
3825 ----------------------------
3827 -- Generates the following subprogram:
3829 -- procedure Assign
3830 -- (Source, Target : Array_Type,
3831 -- Left_Lo, Left_Hi : Index;
3832 -- Right_Lo, Right_Hi : Index;
3833 -- Rev : Boolean)
3834 -- is
3835 -- Li1 : Index;
3836 -- Ri1 : Index;
3838 -- begin
3840 -- if Left_Hi < Left_Lo then
3841 -- return;
3842 -- end if;
3844 -- if Rev then
3845 -- Li1 := Left_Hi;
3846 -- Ri1 := Right_Hi;
3847 -- else
3848 -- Li1 := Left_Lo;
3849 -- Ri1 := Right_Lo;
3850 -- end if;
3852 -- loop
3853 -- Target (Li1) := Source (Ri1);
3855 -- if Rev then
3856 -- exit when Li1 = Left_Lo;
3857 -- Li1 := Index'pred (Li1);
3858 -- Ri1 := Index'pred (Ri1);
3859 -- else
3860 -- exit when Li1 = Left_Hi;
3861 -- Li1 := Index'succ (Li1);
3862 -- Ri1 := Index'succ (Ri1);
3863 -- end if;
3864 -- end loop;
3865 -- end Assign;
3867 procedure Build_Slice_Assignment (Typ : Entity_Id) is
3868 Loc : constant Source_Ptr := Sloc (Typ);
3869 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
3871 Larray : constant Entity_Id := Make_Temporary (Loc, 'A');
3872 Rarray : constant Entity_Id := Make_Temporary (Loc, 'R');
3873 Left_Lo : constant Entity_Id := Make_Temporary (Loc, 'L');
3874 Left_Hi : constant Entity_Id := Make_Temporary (Loc, 'L');
3875 Right_Lo : constant Entity_Id := Make_Temporary (Loc, 'R');
3876 Right_Hi : constant Entity_Id := Make_Temporary (Loc, 'R');
3877 Rev : constant Entity_Id := Make_Temporary (Loc, 'D');
3878 -- Formal parameters of procedure
3880 Proc_Name : constant Entity_Id :=
3881 Make_Defining_Identifier (Loc,
3882 Chars => Make_TSS_Name (Typ, TSS_Slice_Assign));
3884 Lnn : constant Entity_Id := Make_Temporary (Loc, 'L');
3885 Rnn : constant Entity_Id := Make_Temporary (Loc, 'R');
3886 -- Subscripts for left and right sides
3888 Decls : List_Id;
3889 Loops : Node_Id;
3890 Stats : List_Id;
3892 begin
3893 -- Build declarations for indexes
3895 Decls := New_List;
3897 Append_To (Decls,
3898 Make_Object_Declaration (Loc,
3899 Defining_Identifier => Lnn,
3900 Object_Definition =>
3901 New_Occurrence_Of (Index, Loc)));
3903 Append_To (Decls,
3904 Make_Object_Declaration (Loc,
3905 Defining_Identifier => Rnn,
3906 Object_Definition =>
3907 New_Occurrence_Of (Index, Loc)));
3909 Stats := New_List;
3911 -- Build test for empty slice case
3913 Append_To (Stats,
3914 Make_If_Statement (Loc,
3915 Condition =>
3916 Make_Op_Lt (Loc,
3917 Left_Opnd => New_Occurrence_Of (Left_Hi, Loc),
3918 Right_Opnd => New_Occurrence_Of (Left_Lo, Loc)),
3919 Then_Statements => New_List (Make_Simple_Return_Statement (Loc))));
3921 -- Build initializations for indexes
3923 declare
3924 F_Init : constant List_Id := New_List;
3925 B_Init : constant List_Id := New_List;
3927 begin
3928 Append_To (F_Init,
3929 Make_Assignment_Statement (Loc,
3930 Name => New_Occurrence_Of (Lnn, Loc),
3931 Expression => New_Occurrence_Of (Left_Lo, Loc)));
3933 Append_To (F_Init,
3934 Make_Assignment_Statement (Loc,
3935 Name => New_Occurrence_Of (Rnn, Loc),
3936 Expression => New_Occurrence_Of (Right_Lo, Loc)));
3938 Append_To (B_Init,
3939 Make_Assignment_Statement (Loc,
3940 Name => New_Occurrence_Of (Lnn, Loc),
3941 Expression => New_Occurrence_Of (Left_Hi, Loc)));
3943 Append_To (B_Init,
3944 Make_Assignment_Statement (Loc,
3945 Name => New_Occurrence_Of (Rnn, Loc),
3946 Expression => New_Occurrence_Of (Right_Hi, Loc)));
3948 Append_To (Stats,
3949 Make_If_Statement (Loc,
3950 Condition => New_Occurrence_Of (Rev, Loc),
3951 Then_Statements => B_Init,
3952 Else_Statements => F_Init));
3953 end;
3955 -- Now construct the assignment statement
3957 Loops :=
3958 Make_Loop_Statement (Loc,
3959 Statements => New_List (
3960 Make_Assignment_Statement (Loc,
3961 Name =>
3962 Make_Indexed_Component (Loc,
3963 Prefix => New_Occurrence_Of (Larray, Loc),
3964 Expressions => New_List (New_Occurrence_Of (Lnn, Loc))),
3965 Expression =>
3966 Make_Indexed_Component (Loc,
3967 Prefix => New_Occurrence_Of (Rarray, Loc),
3968 Expressions => New_List (New_Occurrence_Of (Rnn, Loc))))),
3969 End_Label => Empty);
3971 -- Build the exit condition and increment/decrement statements
3973 declare
3974 F_Ass : constant List_Id := New_List;
3975 B_Ass : constant List_Id := New_List;
3977 begin
3978 Append_To (F_Ass,
3979 Make_Exit_Statement (Loc,
3980 Condition =>
3981 Make_Op_Eq (Loc,
3982 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
3983 Right_Opnd => New_Occurrence_Of (Left_Hi, Loc))));
3985 Append_To (F_Ass,
3986 Make_Assignment_Statement (Loc,
3987 Name => New_Occurrence_Of (Lnn, Loc),
3988 Expression =>
3989 Make_Attribute_Reference (Loc,
3990 Prefix =>
3991 New_Occurrence_Of (Index, Loc),
3992 Attribute_Name => Name_Succ,
3993 Expressions => New_List (
3994 New_Occurrence_Of (Lnn, Loc)))));
3996 Append_To (F_Ass,
3997 Make_Assignment_Statement (Loc,
3998 Name => New_Occurrence_Of (Rnn, Loc),
3999 Expression =>
4000 Make_Attribute_Reference (Loc,
4001 Prefix =>
4002 New_Occurrence_Of (Index, Loc),
4003 Attribute_Name => Name_Succ,
4004 Expressions => New_List (
4005 New_Occurrence_Of (Rnn, Loc)))));
4007 Append_To (B_Ass,
4008 Make_Exit_Statement (Loc,
4009 Condition =>
4010 Make_Op_Eq (Loc,
4011 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
4012 Right_Opnd => New_Occurrence_Of (Left_Lo, Loc))));
4014 Append_To (B_Ass,
4015 Make_Assignment_Statement (Loc,
4016 Name => New_Occurrence_Of (Lnn, Loc),
4017 Expression =>
4018 Make_Attribute_Reference (Loc,
4019 Prefix =>
4020 New_Occurrence_Of (Index, Loc),
4021 Attribute_Name => Name_Pred,
4022 Expressions => New_List (
4023 New_Occurrence_Of (Lnn, Loc)))));
4025 Append_To (B_Ass,
4026 Make_Assignment_Statement (Loc,
4027 Name => New_Occurrence_Of (Rnn, Loc),
4028 Expression =>
4029 Make_Attribute_Reference (Loc,
4030 Prefix =>
4031 New_Occurrence_Of (Index, Loc),
4032 Attribute_Name => Name_Pred,
4033 Expressions => New_List (
4034 New_Occurrence_Of (Rnn, Loc)))));
4036 Append_To (Statements (Loops),
4037 Make_If_Statement (Loc,
4038 Condition => New_Occurrence_Of (Rev, Loc),
4039 Then_Statements => B_Ass,
4040 Else_Statements => F_Ass));
4041 end;
4043 Append_To (Stats, Loops);
4045 declare
4046 Spec : Node_Id;
4047 Formals : List_Id := New_List;
4049 begin
4050 Formals := New_List (
4051 Make_Parameter_Specification (Loc,
4052 Defining_Identifier => Larray,
4053 Out_Present => True,
4054 Parameter_Type =>
4055 New_Occurrence_Of (Base_Type (Typ), Loc)),
4057 Make_Parameter_Specification (Loc,
4058 Defining_Identifier => Rarray,
4059 Parameter_Type =>
4060 New_Occurrence_Of (Base_Type (Typ), Loc)),
4062 Make_Parameter_Specification (Loc,
4063 Defining_Identifier => Left_Lo,
4064 Parameter_Type =>
4065 New_Occurrence_Of (Index, Loc)),
4067 Make_Parameter_Specification (Loc,
4068 Defining_Identifier => Left_Hi,
4069 Parameter_Type =>
4070 New_Occurrence_Of (Index, Loc)),
4072 Make_Parameter_Specification (Loc,
4073 Defining_Identifier => Right_Lo,
4074 Parameter_Type =>
4075 New_Occurrence_Of (Index, Loc)),
4077 Make_Parameter_Specification (Loc,
4078 Defining_Identifier => Right_Hi,
4079 Parameter_Type =>
4080 New_Occurrence_Of (Index, Loc)));
4082 Append_To (Formals,
4083 Make_Parameter_Specification (Loc,
4084 Defining_Identifier => Rev,
4085 Parameter_Type =>
4086 New_Occurrence_Of (Standard_Boolean, Loc)));
4088 Spec :=
4089 Make_Procedure_Specification (Loc,
4090 Defining_Unit_Name => Proc_Name,
4091 Parameter_Specifications => Formals);
4093 Discard_Node (
4094 Make_Subprogram_Body (Loc,
4095 Specification => Spec,
4096 Declarations => Decls,
4097 Handled_Statement_Sequence =>
4098 Make_Handled_Sequence_Of_Statements (Loc,
4099 Statements => Stats)));
4100 end;
4102 Set_TSS (Typ, Proc_Name);
4103 Set_Is_Pure (Proc_Name);
4104 end Build_Slice_Assignment;
4106 -----------------------------
4107 -- Build_Untagged_Equality --
4108 -----------------------------
4110 procedure Build_Untagged_Equality (Typ : Entity_Id) is
4111 Build_Eq : Boolean;
4112 Comp : Entity_Id;
4113 Decl : Node_Id;
4114 Op : Entity_Id;
4115 Prim : Elmt_Id;
4116 Eq_Op : Entity_Id;
4118 function User_Defined_Eq (T : Entity_Id) return Entity_Id;
4119 -- Check whether the type T has a user-defined primitive equality. If so
4120 -- return it, else return Empty. If true for a component of Typ, we have
4121 -- to build the primitive equality for it.
4123 ---------------------
4124 -- User_Defined_Eq --
4125 ---------------------
4127 function User_Defined_Eq (T : Entity_Id) return Entity_Id is
4128 Prim : Elmt_Id;
4129 Op : Entity_Id;
4131 begin
4132 Op := TSS (T, TSS_Composite_Equality);
4134 if Present (Op) then
4135 return Op;
4136 end if;
4138 Prim := First_Elmt (Collect_Primitive_Operations (T));
4139 while Present (Prim) loop
4140 Op := Node (Prim);
4142 if Chars (Op) = Name_Op_Eq
4143 and then Etype (Op) = Standard_Boolean
4144 and then Etype (First_Formal (Op)) = T
4145 and then Etype (Next_Formal (First_Formal (Op))) = T
4146 then
4147 return Op;
4148 end if;
4150 Next_Elmt (Prim);
4151 end loop;
4153 return Empty;
4154 end User_Defined_Eq;
4156 -- Start of processing for Build_Untagged_Equality
4158 begin
4159 -- If a record component has a primitive equality operation, we must
4160 -- build the corresponding one for the current type.
4162 Build_Eq := False;
4163 Comp := First_Component (Typ);
4164 while Present (Comp) loop
4165 if Is_Record_Type (Etype (Comp))
4166 and then Present (User_Defined_Eq (Etype (Comp)))
4167 then
4168 Build_Eq := True;
4169 end if;
4171 Next_Component (Comp);
4172 end loop;
4174 -- If there is a user-defined equality for the type, we do not create
4175 -- the implicit one.
4177 Prim := First_Elmt (Collect_Primitive_Operations (Typ));
4178 Eq_Op := Empty;
4179 while Present (Prim) loop
4180 if Chars (Node (Prim)) = Name_Op_Eq
4181 and then Comes_From_Source (Node (Prim))
4183 -- Don't we also need to check formal types and return type as in
4184 -- User_Defined_Eq above???
4186 then
4187 Eq_Op := Node (Prim);
4188 Build_Eq := False;
4189 exit;
4190 end if;
4192 Next_Elmt (Prim);
4193 end loop;
4195 -- If the type is derived, inherit the operation, if present, from the
4196 -- parent type. It may have been declared after the type derivation. If
4197 -- the parent type itself is derived, it may have inherited an operation
4198 -- that has itself been overridden, so update its alias and related
4199 -- flags. Ditto for inequality.
4201 if No (Eq_Op) and then Is_Derived_Type (Typ) then
4202 Prim := First_Elmt (Collect_Primitive_Operations (Etype (Typ)));
4203 while Present (Prim) loop
4204 if Chars (Node (Prim)) = Name_Op_Eq then
4205 Copy_TSS (Node (Prim), Typ);
4206 Build_Eq := False;
4208 declare
4209 Op : constant Entity_Id := User_Defined_Eq (Typ);
4210 Eq_Op : constant Entity_Id := Node (Prim);
4211 NE_Op : constant Entity_Id := Next_Entity (Eq_Op);
4213 begin
4214 if Present (Op) then
4215 Set_Alias (Op, Eq_Op);
4216 Set_Is_Abstract_Subprogram
4217 (Op, Is_Abstract_Subprogram (Eq_Op));
4219 if Chars (Next_Entity (Op)) = Name_Op_Ne then
4220 Set_Is_Abstract_Subprogram
4221 (Next_Entity (Op), Is_Abstract_Subprogram (NE_Op));
4222 end if;
4223 end if;
4224 end;
4226 exit;
4227 end if;
4229 Next_Elmt (Prim);
4230 end loop;
4231 end if;
4233 -- If not inherited and not user-defined, build body as for a type with
4234 -- tagged components.
4236 if Build_Eq then
4237 Decl :=
4238 Make_Eq_Body (Typ, Make_TSS_Name (Typ, TSS_Composite_Equality));
4239 Op := Defining_Entity (Decl);
4240 Set_TSS (Typ, Op);
4241 Set_Is_Pure (Op);
4243 if Is_Library_Level_Entity (Typ) then
4244 Set_Is_Public (Op);
4245 end if;
4246 end if;
4247 end Build_Untagged_Equality;
4249 -----------------------------------
4250 -- Build_Variant_Record_Equality --
4251 -----------------------------------
4253 -- Generates:
4255 -- function <<Body_Id>> (Left, Right : T) return Boolean is
4256 -- [ X : T renames Left; ]
4257 -- [ Y : T renames Right; ]
4258 -- -- The above renamings are generated only if the parameters of
4259 -- -- this built function (which are passed by the caller) are not
4260 -- -- named 'X' and 'Y'; these names are required to reuse several
4261 -- -- expander routines when generating this body.
4263 -- begin
4264 -- -- Compare discriminants
4266 -- if X.D1 /= Y.D1 or else X.D2 /= Y.D2 or else ... then
4267 -- return False;
4268 -- end if;
4270 -- -- Compare components
4272 -- if X.C1 /= Y.C1 or else X.C2 /= Y.C2 or else ... then
4273 -- return False;
4274 -- end if;
4276 -- -- Compare variant part
4278 -- case X.D1 is
4279 -- when V1 =>
4280 -- if X.C2 /= Y.C2 or else X.C3 /= Y.C3 or else ... then
4281 -- return False;
4282 -- end if;
4283 -- ...
4284 -- when Vn =>
4285 -- if X.Cn /= Y.Cn or else ... then
4286 -- return False;
4287 -- end if;
4288 -- end case;
4290 -- return True;
4291 -- end _Equality;
4293 function Build_Variant_Record_Equality
4294 (Typ : Entity_Id;
4295 Body_Id : Entity_Id;
4296 Param_Specs : List_Id) return Node_Id
4298 Loc : constant Source_Ptr := Sloc (Typ);
4299 Def : constant Node_Id := Parent (Typ);
4300 Comps : constant Node_Id := Component_List (Type_Definition (Def));
4301 Left : constant Entity_Id := Defining_Identifier (First (Param_Specs));
4302 Right : constant Entity_Id :=
4303 Defining_Identifier (Next (First (Param_Specs)));
4304 Decls : constant List_Id := New_List;
4305 Stmts : constant List_Id := New_List;
4307 Subp_Body : Node_Id;
4309 begin
4310 pragma Assert (not Is_Tagged_Type (Typ));
4312 -- In order to reuse the expander routines Make_Eq_If and Make_Eq_Case
4313 -- the name of the formals must be X and Y; otherwise we generate two
4314 -- renaming declarations for such purpose.
4316 if Chars (Left) /= Name_X then
4317 Append_To (Decls,
4318 Make_Object_Renaming_Declaration (Loc,
4319 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
4320 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
4321 Name => Make_Identifier (Loc, Chars (Left))));
4322 end if;
4324 if Chars (Right) /= Name_Y then
4325 Append_To (Decls,
4326 Make_Object_Renaming_Declaration (Loc,
4327 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
4328 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
4329 Name => Make_Identifier (Loc, Chars (Right))));
4330 end if;
4332 -- Unchecked_Unions require additional machinery to support equality.
4333 -- Two extra parameters (A and B) are added to the equality function
4334 -- parameter list for each discriminant of the type, in order to
4335 -- capture the inferred values of the discriminants in equality calls.
4336 -- The names of the parameters match the names of the corresponding
4337 -- discriminant, with an added suffix.
4339 if Is_Unchecked_Union (Typ) then
4340 declare
4341 A : Entity_Id;
4342 B : Entity_Id;
4343 Discr : Entity_Id;
4344 Discr_Type : Entity_Id;
4345 New_Discrs : Elist_Id;
4347 begin
4348 New_Discrs := New_Elmt_List;
4350 Discr := First_Discriminant (Typ);
4351 while Present (Discr) loop
4352 Discr_Type := Etype (Discr);
4354 A :=
4355 Make_Defining_Identifier (Loc,
4356 Chars => New_External_Name (Chars (Discr), 'A'));
4358 B :=
4359 Make_Defining_Identifier (Loc,
4360 Chars => New_External_Name (Chars (Discr), 'B'));
4362 -- Add new parameters to the parameter list
4364 Append_To (Param_Specs,
4365 Make_Parameter_Specification (Loc,
4366 Defining_Identifier => A,
4367 Parameter_Type =>
4368 New_Occurrence_Of (Discr_Type, Loc)));
4370 Append_To (Param_Specs,
4371 Make_Parameter_Specification (Loc,
4372 Defining_Identifier => B,
4373 Parameter_Type =>
4374 New_Occurrence_Of (Discr_Type, Loc)));
4376 Append_Elmt (A, New_Discrs);
4378 -- Generate the following code to compare each of the inferred
4379 -- discriminants:
4381 -- if a /= b then
4382 -- return False;
4383 -- end if;
4385 Append_To (Stmts,
4386 Make_If_Statement (Loc,
4387 Condition =>
4388 Make_Op_Ne (Loc,
4389 Left_Opnd => New_Occurrence_Of (A, Loc),
4390 Right_Opnd => New_Occurrence_Of (B, Loc)),
4391 Then_Statements => New_List (
4392 Make_Simple_Return_Statement (Loc,
4393 Expression =>
4394 New_Occurrence_Of (Standard_False, Loc)))));
4395 Next_Discriminant (Discr);
4396 end loop;
4398 -- Generate component-by-component comparison. Note that we must
4399 -- propagate the inferred discriminants formals to act as the case
4400 -- statement switch. Their value is added when an equality call on
4401 -- unchecked unions is expanded.
4403 Append_List_To (Stmts, Make_Eq_Case (Typ, Comps, New_Discrs));
4404 end;
4406 -- Normal case (not unchecked union)
4408 else
4409 Append_To (Stmts,
4410 Make_Eq_If (Typ, Discriminant_Specifications (Def)));
4411 Append_List_To (Stmts, Make_Eq_Case (Typ, Comps));
4412 end if;
4414 Append_To (Stmts,
4415 Make_Simple_Return_Statement (Loc,
4416 Expression => New_Occurrence_Of (Standard_True, Loc)));
4418 Subp_Body :=
4419 Make_Subprogram_Body (Loc,
4420 Specification =>
4421 Make_Function_Specification (Loc,
4422 Defining_Unit_Name => Body_Id,
4423 Parameter_Specifications => Param_Specs,
4424 Result_Definition =>
4425 New_Occurrence_Of (Standard_Boolean, Loc)),
4426 Declarations => Decls,
4427 Handled_Statement_Sequence =>
4428 Make_Handled_Sequence_Of_Statements (Loc,
4429 Statements => Stmts));
4431 return Subp_Body;
4432 end Build_Variant_Record_Equality;
4434 -----------------------------
4435 -- Check_Stream_Attributes --
4436 -----------------------------
4438 procedure Check_Stream_Attributes (Typ : Entity_Id) is
4439 Comp : Entity_Id;
4440 Par_Read : constant Boolean :=
4441 Stream_Attribute_Available (Typ, TSS_Stream_Read)
4442 and then not Has_Specified_Stream_Read (Typ);
4443 Par_Write : constant Boolean :=
4444 Stream_Attribute_Available (Typ, TSS_Stream_Write)
4445 and then not Has_Specified_Stream_Write (Typ);
4447 procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type);
4448 -- Check that Comp has a user-specified Nam stream attribute
4450 ----------------
4451 -- Check_Attr --
4452 ----------------
4454 procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type) is
4455 begin
4456 if not Stream_Attribute_Available (Etype (Comp), TSS_Nam) then
4457 Error_Msg_Name_1 := Nam;
4458 Error_Msg_N
4459 ("|component& in limited extension must have% attribute", Comp);
4460 end if;
4461 end Check_Attr;
4463 -- Start of processing for Check_Stream_Attributes
4465 begin
4466 if Par_Read or else Par_Write then
4467 Comp := First_Component (Typ);
4468 while Present (Comp) loop
4469 if Comes_From_Source (Comp)
4470 and then Original_Record_Component (Comp) = Comp
4471 and then Is_Limited_Type (Etype (Comp))
4472 then
4473 if Par_Read then
4474 Check_Attr (Name_Read, TSS_Stream_Read);
4475 end if;
4477 if Par_Write then
4478 Check_Attr (Name_Write, TSS_Stream_Write);
4479 end if;
4480 end if;
4482 Next_Component (Comp);
4483 end loop;
4484 end if;
4485 end Check_Stream_Attributes;
4487 ----------------------
4488 -- Clean_Task_Names --
4489 ----------------------
4491 procedure Clean_Task_Names
4492 (Typ : Entity_Id;
4493 Proc_Id : Entity_Id)
4495 begin
4496 if Has_Task (Typ)
4497 and then not Restriction_Active (No_Implicit_Heap_Allocations)
4498 and then not Global_Discard_Names
4499 and then Tagged_Type_Expansion
4500 then
4501 Set_Uses_Sec_Stack (Proc_Id);
4502 end if;
4503 end Clean_Task_Names;
4505 ------------------------------
4506 -- Expand_Freeze_Array_Type --
4507 ------------------------------
4509 procedure Expand_Freeze_Array_Type (N : Node_Id) is
4510 Typ : constant Entity_Id := Entity (N);
4511 Base : constant Entity_Id := Base_Type (Typ);
4512 Comp_Typ : constant Entity_Id := Component_Type (Typ);
4514 begin
4515 if not Is_Bit_Packed_Array (Typ) then
4517 -- If the component contains tasks, so does the array type. This may
4518 -- not be indicated in the array type because the component may have
4519 -- been a private type at the point of definition. Same if component
4520 -- type is controlled or contains protected objects.
4522 Propagate_Concurrent_Flags (Base, Comp_Typ);
4523 Set_Has_Controlled_Component
4524 (Base, Has_Controlled_Component (Comp_Typ)
4525 or else Is_Controlled (Comp_Typ));
4527 if No (Init_Proc (Base)) then
4529 -- If this is an anonymous array created for a declaration with
4530 -- an initial value, its init_proc will never be called. The
4531 -- initial value itself may have been expanded into assignments,
4532 -- in which case the object declaration is carries the
4533 -- No_Initialization flag.
4535 if Is_Itype (Base)
4536 and then Nkind (Associated_Node_For_Itype (Base)) =
4537 N_Object_Declaration
4538 and then
4539 (Present (Expression (Associated_Node_For_Itype (Base)))
4540 or else No_Initialization (Associated_Node_For_Itype (Base)))
4541 then
4542 null;
4544 -- We do not need an init proc for string or wide [wide] string,
4545 -- since the only time these need initialization in normalize or
4546 -- initialize scalars mode, and these types are treated specially
4547 -- and do not need initialization procedures.
4549 elsif Is_Standard_String_Type (Base) then
4550 null;
4552 -- Otherwise we have to build an init proc for the subtype
4554 else
4555 Build_Array_Init_Proc (Base, N);
4556 end if;
4557 end if;
4559 if Typ = Base and then Has_Controlled_Component (Base) then
4560 Build_Controlling_Procs (Base);
4562 if not Is_Limited_Type (Comp_Typ)
4563 and then Number_Dimensions (Typ) = 1
4564 then
4565 Build_Slice_Assignment (Typ);
4566 end if;
4567 end if;
4569 -- For packed case, default initialization, except if the component type
4570 -- is itself a packed structure with an initialization procedure, or
4571 -- initialize/normalize scalars active, and we have a base type, or the
4572 -- type is public, because in that case a client might specify
4573 -- Normalize_Scalars and there better be a public Init_Proc for it.
4575 elsif (Present (Init_Proc (Component_Type (Base)))
4576 and then No (Base_Init_Proc (Base)))
4577 or else (Init_Or_Norm_Scalars and then Base = Typ)
4578 or else Is_Public (Typ)
4579 then
4580 Build_Array_Init_Proc (Base, N);
4581 end if;
4582 end Expand_Freeze_Array_Type;
4584 -----------------------------------
4585 -- Expand_Freeze_Class_Wide_Type --
4586 -----------------------------------
4588 procedure Expand_Freeze_Class_Wide_Type (N : Node_Id) is
4589 function Is_C_Derivation (Typ : Entity_Id) return Boolean;
4590 -- Given a type, determine whether it is derived from a C or C++ root
4592 ---------------------
4593 -- Is_C_Derivation --
4594 ---------------------
4596 function Is_C_Derivation (Typ : Entity_Id) return Boolean is
4597 T : Entity_Id;
4599 begin
4600 T := Typ;
4601 loop
4602 if Is_CPP_Class (T)
4603 or else Convention (T) = Convention_C
4604 or else Convention (T) = Convention_CPP
4605 then
4606 return True;
4607 end if;
4609 exit when T = Etype (T);
4611 T := Etype (T);
4612 end loop;
4614 return False;
4615 end Is_C_Derivation;
4617 -- Local variables
4619 Typ : constant Entity_Id := Entity (N);
4620 Root : constant Entity_Id := Root_Type (Typ);
4622 -- Start of processing for Expand_Freeze_Class_Wide_Type
4624 begin
4625 -- Certain run-time configurations and targets do not provide support
4626 -- for controlled types.
4628 if Restriction_Active (No_Finalization) then
4629 return;
4631 -- Do not create TSS routine Finalize_Address when dispatching calls are
4632 -- disabled since the core of the routine is a dispatching call.
4634 elsif Restriction_Active (No_Dispatching_Calls) then
4635 return;
4637 -- Do not create TSS routine Finalize_Address for concurrent class-wide
4638 -- types. Ignore C, C++, CIL and Java types since it is assumed that the
4639 -- non-Ada side will handle their destruction.
4641 elsif Is_Concurrent_Type (Root)
4642 or else Is_C_Derivation (Root)
4643 or else Convention (Typ) = Convention_CPP
4644 then
4645 return;
4647 -- Do not create TSS routine Finalize_Address when compiling in CodePeer
4648 -- mode since the routine contains an Unchecked_Conversion.
4650 elsif CodePeer_Mode then
4651 return;
4652 end if;
4654 -- Create the body of TSS primitive Finalize_Address. This automatically
4655 -- sets the TSS entry for the class-wide type.
4657 Make_Finalize_Address_Body (Typ);
4658 end Expand_Freeze_Class_Wide_Type;
4660 ------------------------------------
4661 -- Expand_Freeze_Enumeration_Type --
4662 ------------------------------------
4664 procedure Expand_Freeze_Enumeration_Type (N : Node_Id) is
4665 Typ : constant Entity_Id := Entity (N);
4666 Loc : constant Source_Ptr := Sloc (Typ);
4668 Arr : Entity_Id;
4669 Ent : Entity_Id;
4670 Fent : Entity_Id;
4671 Is_Contiguous : Boolean;
4672 Ityp : Entity_Id;
4673 Last_Repval : Uint;
4674 Lst : List_Id;
4675 Num : Nat;
4676 Pos_Expr : Node_Id;
4678 Func : Entity_Id;
4679 pragma Warnings (Off, Func);
4681 begin
4682 -- Various optimizations possible if given representation is contiguous
4684 Is_Contiguous := True;
4686 Ent := First_Literal (Typ);
4687 Last_Repval := Enumeration_Rep (Ent);
4689 Next_Literal (Ent);
4690 while Present (Ent) loop
4691 if Enumeration_Rep (Ent) - Last_Repval /= 1 then
4692 Is_Contiguous := False;
4693 exit;
4694 else
4695 Last_Repval := Enumeration_Rep (Ent);
4696 end if;
4698 Next_Literal (Ent);
4699 end loop;
4701 if Is_Contiguous then
4702 Set_Has_Contiguous_Rep (Typ);
4703 Ent := First_Literal (Typ);
4704 Num := 1;
4705 Lst := New_List (New_Occurrence_Of (Ent, Sloc (Ent)));
4707 else
4708 -- Build list of literal references
4710 Lst := New_List;
4711 Num := 0;
4713 Ent := First_Literal (Typ);
4714 while Present (Ent) loop
4715 Append_To (Lst, New_Occurrence_Of (Ent, Sloc (Ent)));
4716 Num := Num + 1;
4717 Next_Literal (Ent);
4718 end loop;
4719 end if;
4721 -- Now build an array declaration
4723 -- typA : array (Natural range 0 .. num - 1) of ctype :=
4724 -- (v, v, v, v, v, ....)
4726 -- where ctype is the corresponding integer type. If the representation
4727 -- is contiguous, we only keep the first literal, which provides the
4728 -- offset for Pos_To_Rep computations.
4730 Arr :=
4731 Make_Defining_Identifier (Loc,
4732 Chars => New_External_Name (Chars (Typ), 'A'));
4734 Append_Freeze_Action (Typ,
4735 Make_Object_Declaration (Loc,
4736 Defining_Identifier => Arr,
4737 Constant_Present => True,
4739 Object_Definition =>
4740 Make_Constrained_Array_Definition (Loc,
4741 Discrete_Subtype_Definitions => New_List (
4742 Make_Subtype_Indication (Loc,
4743 Subtype_Mark => New_Occurrence_Of (Standard_Natural, Loc),
4744 Constraint =>
4745 Make_Range_Constraint (Loc,
4746 Range_Expression =>
4747 Make_Range (Loc,
4748 Low_Bound =>
4749 Make_Integer_Literal (Loc, 0),
4750 High_Bound =>
4751 Make_Integer_Literal (Loc, Num - 1))))),
4753 Component_Definition =>
4754 Make_Component_Definition (Loc,
4755 Aliased_Present => False,
4756 Subtype_Indication => New_Occurrence_Of (Typ, Loc))),
4758 Expression =>
4759 Make_Aggregate (Loc,
4760 Expressions => Lst)));
4762 Set_Enum_Pos_To_Rep (Typ, Arr);
4764 -- Now we build the function that converts representation values to
4765 -- position values. This function has the form:
4767 -- function _Rep_To_Pos (A : etype; F : Boolean) return Integer is
4768 -- begin
4769 -- case ityp!(A) is
4770 -- when enum-lit'Enum_Rep => return posval;
4771 -- when enum-lit'Enum_Rep => return posval;
4772 -- ...
4773 -- when others =>
4774 -- [raise Constraint_Error when F "invalid data"]
4775 -- return -1;
4776 -- end case;
4777 -- end;
4779 -- Note: the F parameter determines whether the others case (no valid
4780 -- representation) raises Constraint_Error or returns a unique value
4781 -- of minus one. The latter case is used, e.g. in 'Valid code.
4783 -- Note: the reason we use Enum_Rep values in the case here is to avoid
4784 -- the code generator making inappropriate assumptions about the range
4785 -- of the values in the case where the value is invalid. ityp is a
4786 -- signed or unsigned integer type of appropriate width.
4788 -- Note: if exceptions are not supported, then we suppress the raise
4789 -- and return -1 unconditionally (this is an erroneous program in any
4790 -- case and there is no obligation to raise Constraint_Error here). We
4791 -- also do this if pragma Restrictions (No_Exceptions) is active.
4793 -- Is this right??? What about No_Exception_Propagation???
4795 -- Representations are signed
4797 if Enumeration_Rep (First_Literal (Typ)) < 0 then
4799 -- The underlying type is signed. Reset the Is_Unsigned_Type
4800 -- explicitly, because it might have been inherited from
4801 -- parent type.
4803 Set_Is_Unsigned_Type (Typ, False);
4805 if Esize (Typ) <= Standard_Integer_Size then
4806 Ityp := Standard_Integer;
4807 else
4808 Ityp := Universal_Integer;
4809 end if;
4811 -- Representations are unsigned
4813 else
4814 if Esize (Typ) <= Standard_Integer_Size then
4815 Ityp := RTE (RE_Unsigned);
4816 else
4817 Ityp := RTE (RE_Long_Long_Unsigned);
4818 end if;
4819 end if;
4821 -- The body of the function is a case statement. First collect case
4822 -- alternatives, or optimize the contiguous case.
4824 Lst := New_List;
4826 -- If representation is contiguous, Pos is computed by subtracting
4827 -- the representation of the first literal.
4829 if Is_Contiguous then
4830 Ent := First_Literal (Typ);
4832 if Enumeration_Rep (Ent) = Last_Repval then
4834 -- Another special case: for a single literal, Pos is zero
4836 Pos_Expr := Make_Integer_Literal (Loc, Uint_0);
4838 else
4839 Pos_Expr :=
4840 Convert_To (Standard_Integer,
4841 Make_Op_Subtract (Loc,
4842 Left_Opnd =>
4843 Unchecked_Convert_To
4844 (Ityp, Make_Identifier (Loc, Name_uA)),
4845 Right_Opnd =>
4846 Make_Integer_Literal (Loc,
4847 Intval => Enumeration_Rep (First_Literal (Typ)))));
4848 end if;
4850 Append_To (Lst,
4851 Make_Case_Statement_Alternative (Loc,
4852 Discrete_Choices => New_List (
4853 Make_Range (Sloc (Enumeration_Rep_Expr (Ent)),
4854 Low_Bound =>
4855 Make_Integer_Literal (Loc,
4856 Intval => Enumeration_Rep (Ent)),
4857 High_Bound =>
4858 Make_Integer_Literal (Loc, Intval => Last_Repval))),
4860 Statements => New_List (
4861 Make_Simple_Return_Statement (Loc,
4862 Expression => Pos_Expr))));
4864 else
4865 Ent := First_Literal (Typ);
4866 while Present (Ent) loop
4867 Append_To (Lst,
4868 Make_Case_Statement_Alternative (Loc,
4869 Discrete_Choices => New_List (
4870 Make_Integer_Literal (Sloc (Enumeration_Rep_Expr (Ent)),
4871 Intval => Enumeration_Rep (Ent))),
4873 Statements => New_List (
4874 Make_Simple_Return_Statement (Loc,
4875 Expression =>
4876 Make_Integer_Literal (Loc,
4877 Intval => Enumeration_Pos (Ent))))));
4879 Next_Literal (Ent);
4880 end loop;
4881 end if;
4883 -- In normal mode, add the others clause with the test.
4884 -- If Predicates_Ignored is True, validity checks do not apply to
4885 -- the subtype.
4887 if not No_Exception_Handlers_Set
4888 and then not Predicates_Ignored (Typ)
4889 then
4890 Append_To (Lst,
4891 Make_Case_Statement_Alternative (Loc,
4892 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
4893 Statements => New_List (
4894 Make_Raise_Constraint_Error (Loc,
4895 Condition => Make_Identifier (Loc, Name_uF),
4896 Reason => CE_Invalid_Data),
4897 Make_Simple_Return_Statement (Loc,
4898 Expression => Make_Integer_Literal (Loc, -1)))));
4900 -- If either of the restrictions No_Exceptions_Handlers/Propagation is
4901 -- active then return -1 (we cannot usefully raise Constraint_Error in
4902 -- this case). See description above for further details.
4904 else
4905 Append_To (Lst,
4906 Make_Case_Statement_Alternative (Loc,
4907 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
4908 Statements => New_List (
4909 Make_Simple_Return_Statement (Loc,
4910 Expression => Make_Integer_Literal (Loc, -1)))));
4911 end if;
4913 -- Now we can build the function body
4915 Fent :=
4916 Make_Defining_Identifier (Loc, Make_TSS_Name (Typ, TSS_Rep_To_Pos));
4918 Func :=
4919 Make_Subprogram_Body (Loc,
4920 Specification =>
4921 Make_Function_Specification (Loc,
4922 Defining_Unit_Name => Fent,
4923 Parameter_Specifications => New_List (
4924 Make_Parameter_Specification (Loc,
4925 Defining_Identifier =>
4926 Make_Defining_Identifier (Loc, Name_uA),
4927 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
4928 Make_Parameter_Specification (Loc,
4929 Defining_Identifier =>
4930 Make_Defining_Identifier (Loc, Name_uF),
4931 Parameter_Type =>
4932 New_Occurrence_Of (Standard_Boolean, Loc))),
4934 Result_Definition => New_Occurrence_Of (Standard_Integer, Loc)),
4936 Declarations => Empty_List,
4938 Handled_Statement_Sequence =>
4939 Make_Handled_Sequence_Of_Statements (Loc,
4940 Statements => New_List (
4941 Make_Case_Statement (Loc,
4942 Expression =>
4943 Unchecked_Convert_To
4944 (Ityp, Make_Identifier (Loc, Name_uA)),
4945 Alternatives => Lst))));
4947 Set_TSS (Typ, Fent);
4949 -- Set Pure flag (it will be reset if the current context is not Pure).
4950 -- We also pretend there was a pragma Pure_Function so that for purposes
4951 -- of optimization and constant-folding, we will consider the function
4952 -- Pure even if we are not in a Pure context).
4954 Set_Is_Pure (Fent);
4955 Set_Has_Pragma_Pure_Function (Fent);
4957 -- Unless we are in -gnatD mode, where we are debugging generated code,
4958 -- this is an internal entity for which we don't need debug info.
4960 if not Debug_Generated_Code then
4961 Set_Debug_Info_Off (Fent);
4962 end if;
4964 Set_Is_Inlined (Fent);
4966 exception
4967 when RE_Not_Available =>
4968 return;
4969 end Expand_Freeze_Enumeration_Type;
4971 -------------------------------
4972 -- Expand_Freeze_Record_Type --
4973 -------------------------------
4975 procedure Expand_Freeze_Record_Type (N : Node_Id) is
4976 procedure Build_Variant_Record_Equality (Typ : Entity_Id);
4977 -- Create An Equality function for the untagged variant record Typ and
4978 -- attach it to the TSS list.
4980 -----------------------------------
4981 -- Build_Variant_Record_Equality --
4982 -----------------------------------
4984 procedure Build_Variant_Record_Equality (Typ : Entity_Id) is
4985 Loc : constant Source_Ptr := Sloc (Typ);
4986 F : constant Entity_Id :=
4987 Make_Defining_Identifier (Loc,
4988 Chars => Make_TSS_Name (Typ, TSS_Composite_Equality));
4989 begin
4990 -- For a variant record with restriction No_Implicit_Conditionals
4991 -- in effect we skip building the procedure. This is safe because
4992 -- if we can see the restriction, so can any caller, and calls to
4993 -- equality test routines are not allowed for variant records if
4994 -- this restriction is active.
4996 if Restriction_Active (No_Implicit_Conditionals) then
4997 return;
4998 end if;
5000 -- Derived Unchecked_Union types no longer inherit the equality
5001 -- function of their parent.
5003 if Is_Derived_Type (Typ)
5004 and then not Is_Unchecked_Union (Typ)
5005 and then not Has_New_Non_Standard_Rep (Typ)
5006 then
5007 declare
5008 Parent_Eq : constant Entity_Id :=
5009 TSS (Root_Type (Typ), TSS_Composite_Equality);
5010 begin
5011 if Present (Parent_Eq) then
5012 Copy_TSS (Parent_Eq, Typ);
5013 return;
5014 end if;
5015 end;
5016 end if;
5018 Discard_Node (
5019 Build_Variant_Record_Equality
5020 (Typ => Typ,
5021 Body_Id => F,
5022 Param_Specs => New_List (
5023 Make_Parameter_Specification (Loc,
5024 Defining_Identifier =>
5025 Make_Defining_Identifier (Loc, Name_X),
5026 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
5028 Make_Parameter_Specification (Loc,
5029 Defining_Identifier =>
5030 Make_Defining_Identifier (Loc, Name_Y),
5031 Parameter_Type => New_Occurrence_Of (Typ, Loc)))));
5033 Set_TSS (Typ, F);
5034 Set_Is_Pure (F);
5036 if not Debug_Generated_Code then
5037 Set_Debug_Info_Off (F);
5038 end if;
5039 end Build_Variant_Record_Equality;
5041 -- Local variables
5043 Typ : constant Node_Id := Entity (N);
5044 Typ_Decl : constant Node_Id := Parent (Typ);
5046 Comp : Entity_Id;
5047 Comp_Typ : Entity_Id;
5048 Predef_List : List_Id;
5050 Wrapper_Decl_List : List_Id := No_List;
5051 Wrapper_Body_List : List_Id := No_List;
5053 Renamed_Eq : Node_Id := Empty;
5054 -- Defining unit name for the predefined equality function in the case
5055 -- where the type has a primitive operation that is a renaming of
5056 -- predefined equality (but only if there is also an overriding
5057 -- user-defined equality function). Used to pass this entity from
5058 -- Make_Predefined_Primitive_Specs to Predefined_Primitive_Bodies.
5060 -- Start of processing for Expand_Freeze_Record_Type
5062 begin
5063 -- Build discriminant checking functions if not a derived type (for
5064 -- derived types that are not tagged types, always use the discriminant
5065 -- checking functions of the parent type). However, for untagged types
5066 -- the derivation may have taken place before the parent was frozen, so
5067 -- we copy explicitly the discriminant checking functions from the
5068 -- parent into the components of the derived type.
5070 if not Is_Derived_Type (Typ)
5071 or else Has_New_Non_Standard_Rep (Typ)
5072 or else Is_Tagged_Type (Typ)
5073 then
5074 Build_Discr_Checking_Funcs (Typ_Decl);
5076 elsif Is_Derived_Type (Typ)
5077 and then not Is_Tagged_Type (Typ)
5079 -- If we have a derived Unchecked_Union, we do not inherit the
5080 -- discriminant checking functions from the parent type since the
5081 -- discriminants are non existent.
5083 and then not Is_Unchecked_Union (Typ)
5084 and then Has_Discriminants (Typ)
5085 then
5086 declare
5087 Old_Comp : Entity_Id;
5089 begin
5090 Old_Comp :=
5091 First_Component (Base_Type (Underlying_Type (Etype (Typ))));
5092 Comp := First_Component (Typ);
5093 while Present (Comp) loop
5094 if Ekind (Comp) = E_Component
5095 and then Chars (Comp) = Chars (Old_Comp)
5096 then
5097 Set_Discriminant_Checking_Func
5098 (Comp, Discriminant_Checking_Func (Old_Comp));
5099 end if;
5101 Next_Component (Old_Comp);
5102 Next_Component (Comp);
5103 end loop;
5104 end;
5105 end if;
5107 if Is_Derived_Type (Typ)
5108 and then Is_Limited_Type (Typ)
5109 and then Is_Tagged_Type (Typ)
5110 then
5111 Check_Stream_Attributes (Typ);
5112 end if;
5114 -- Update task, protected, and controlled component flags, because some
5115 -- of the component types may have been private at the point of the
5116 -- record declaration. Detect anonymous access-to-controlled components.
5118 Comp := First_Component (Typ);
5119 while Present (Comp) loop
5120 Comp_Typ := Etype (Comp);
5122 Propagate_Concurrent_Flags (Typ, Comp_Typ);
5124 -- Do not set Has_Controlled_Component on a class-wide equivalent
5125 -- type. See Make_CW_Equivalent_Type.
5127 if not Is_Class_Wide_Equivalent_Type (Typ)
5128 and then
5129 (Has_Controlled_Component (Comp_Typ)
5130 or else (Chars (Comp) /= Name_uParent
5131 and then Is_Controlled (Comp_Typ)))
5132 then
5133 Set_Has_Controlled_Component (Typ);
5134 end if;
5136 Next_Component (Comp);
5137 end loop;
5139 -- Handle constructors of untagged CPP_Class types
5141 if not Is_Tagged_Type (Typ) and then Is_CPP_Class (Typ) then
5142 Set_CPP_Constructors (Typ);
5143 end if;
5145 -- Creation of the Dispatch Table. Note that a Dispatch Table is built
5146 -- for regular tagged types as well as for Ada types deriving from a C++
5147 -- Class, but not for tagged types directly corresponding to C++ classes
5148 -- In the later case we assume that it is created in the C++ side and we
5149 -- just use it.
5151 if Is_Tagged_Type (Typ) then
5153 -- Add the _Tag component
5155 if Underlying_Type (Etype (Typ)) = Typ then
5156 Expand_Tagged_Root (Typ);
5157 end if;
5159 if Is_CPP_Class (Typ) then
5160 Set_All_DT_Position (Typ);
5162 -- Create the tag entities with a minimum decoration
5164 if Tagged_Type_Expansion then
5165 Append_Freeze_Actions (Typ, Make_Tags (Typ));
5166 end if;
5168 Set_CPP_Constructors (Typ);
5170 else
5171 if not Building_Static_DT (Typ) then
5173 -- Usually inherited primitives are not delayed but the first
5174 -- Ada extension of a CPP_Class is an exception since the
5175 -- address of the inherited subprogram has to be inserted in
5176 -- the new Ada Dispatch Table and this is a freezing action.
5178 -- Similarly, if this is an inherited operation whose parent is
5179 -- not frozen yet, it is not in the DT of the parent, and we
5180 -- generate an explicit freeze node for the inherited operation
5181 -- so it is properly inserted in the DT of the current type.
5183 declare
5184 Elmt : Elmt_Id;
5185 Subp : Entity_Id;
5187 begin
5188 Elmt := First_Elmt (Primitive_Operations (Typ));
5189 while Present (Elmt) loop
5190 Subp := Node (Elmt);
5192 if Present (Alias (Subp)) then
5193 if Is_CPP_Class (Etype (Typ)) then
5194 Set_Has_Delayed_Freeze (Subp);
5196 elsif Has_Delayed_Freeze (Alias (Subp))
5197 and then not Is_Frozen (Alias (Subp))
5198 then
5199 Set_Is_Frozen (Subp, False);
5200 Set_Has_Delayed_Freeze (Subp);
5201 end if;
5202 end if;
5204 Next_Elmt (Elmt);
5205 end loop;
5206 end;
5207 end if;
5209 -- Unfreeze momentarily the type to add the predefined primitives
5210 -- operations. The reason we unfreeze is so that these predefined
5211 -- operations will indeed end up as primitive operations (which
5212 -- must be before the freeze point).
5214 Set_Is_Frozen (Typ, False);
5216 -- Do not add the spec of predefined primitives in case of
5217 -- CPP tagged type derivations that have convention CPP.
5219 if Is_CPP_Class (Root_Type (Typ))
5220 and then Convention (Typ) = Convention_CPP
5221 then
5222 null;
5224 -- Do not add the spec of the predefined primitives if we are
5225 -- compiling under restriction No_Dispatching_Calls.
5227 elsif not Restriction_Active (No_Dispatching_Calls) then
5228 Make_Predefined_Primitive_Specs (Typ, Predef_List, Renamed_Eq);
5229 Insert_List_Before_And_Analyze (N, Predef_List);
5230 end if;
5232 -- Ada 2005 (AI-391): For a nonabstract null extension, create
5233 -- wrapper functions for each nonoverridden inherited function
5234 -- with a controlling result of the type. The wrapper for such
5235 -- a function returns an extension aggregate that invokes the
5236 -- parent function.
5238 if Ada_Version >= Ada_2005
5239 and then not Is_Abstract_Type (Typ)
5240 and then Is_Null_Extension (Typ)
5241 then
5242 Make_Controlling_Function_Wrappers
5243 (Typ, Wrapper_Decl_List, Wrapper_Body_List);
5244 Insert_List_Before_And_Analyze (N, Wrapper_Decl_List);
5245 end if;
5247 -- Ada 2005 (AI-251): For a nonabstract type extension, build
5248 -- null procedure declarations for each set of homographic null
5249 -- procedures that are inherited from interface types but not
5250 -- overridden. This is done to ensure that the dispatch table
5251 -- entry associated with such null primitives are properly filled.
5253 if Ada_Version >= Ada_2005
5254 and then Etype (Typ) /= Typ
5255 and then not Is_Abstract_Type (Typ)
5256 and then Has_Interfaces (Typ)
5257 then
5258 Insert_Actions (N, Make_Null_Procedure_Specs (Typ));
5259 end if;
5261 Set_Is_Frozen (Typ);
5263 if not Is_Derived_Type (Typ)
5264 or else Is_Tagged_Type (Etype (Typ))
5265 then
5266 Set_All_DT_Position (Typ);
5268 -- If this is a type derived from an untagged private type whose
5269 -- full view is tagged, the type is marked tagged for layout
5270 -- reasons, but it has no dispatch table.
5272 elsif Is_Derived_Type (Typ)
5273 and then Is_Private_Type (Etype (Typ))
5274 and then not Is_Tagged_Type (Etype (Typ))
5275 then
5276 return;
5277 end if;
5279 -- Create and decorate the tags. Suppress their creation when
5280 -- not Tagged_Type_Expansion because the dispatching mechanism is
5281 -- handled internally by the virtual target.
5283 if Tagged_Type_Expansion then
5284 Append_Freeze_Actions (Typ, Make_Tags (Typ));
5286 -- Generate dispatch table of locally defined tagged type.
5287 -- Dispatch tables of library level tagged types are built
5288 -- later (see Analyze_Declarations).
5290 if not Building_Static_DT (Typ) then
5291 Append_Freeze_Actions (Typ, Make_DT (Typ));
5292 end if;
5293 end if;
5295 -- If the type has unknown discriminants, propagate dispatching
5296 -- information to its underlying record view, which does not get
5297 -- its own dispatch table.
5299 if Is_Derived_Type (Typ)
5300 and then Has_Unknown_Discriminants (Typ)
5301 and then Present (Underlying_Record_View (Typ))
5302 then
5303 declare
5304 Rep : constant Entity_Id := Underlying_Record_View (Typ);
5305 begin
5306 Set_Access_Disp_Table
5307 (Rep, Access_Disp_Table (Typ));
5308 Set_Dispatch_Table_Wrappers
5309 (Rep, Dispatch_Table_Wrappers (Typ));
5310 Set_Direct_Primitive_Operations
5311 (Rep, Direct_Primitive_Operations (Typ));
5312 end;
5313 end if;
5315 -- Make sure that the primitives Initialize, Adjust and Finalize
5316 -- are Frozen before other TSS subprograms. We don't want them
5317 -- Frozen inside.
5319 if Is_Controlled (Typ) then
5320 if not Is_Limited_Type (Typ) then
5321 Append_Freeze_Actions (Typ,
5322 Freeze_Entity (Find_Prim_Op (Typ, Name_Adjust), Typ));
5323 end if;
5325 Append_Freeze_Actions (Typ,
5326 Freeze_Entity (Find_Prim_Op (Typ, Name_Initialize), Typ));
5328 Append_Freeze_Actions (Typ,
5329 Freeze_Entity (Find_Prim_Op (Typ, Name_Finalize), Typ));
5330 end if;
5332 -- Freeze rest of primitive operations. There is no need to handle
5333 -- the predefined primitives if we are compiling under restriction
5334 -- No_Dispatching_Calls.
5336 if not Restriction_Active (No_Dispatching_Calls) then
5337 Append_Freeze_Actions (Typ, Predefined_Primitive_Freeze (Typ));
5338 end if;
5339 end if;
5341 -- In the untagged case, ever since Ada 83 an equality function must
5342 -- be provided for variant records that are not unchecked unions.
5343 -- In Ada 2012 the equality function composes, and thus must be built
5344 -- explicitly just as for tagged records.
5346 elsif Has_Discriminants (Typ)
5347 and then not Is_Limited_Type (Typ)
5348 then
5349 declare
5350 Comps : constant Node_Id :=
5351 Component_List (Type_Definition (Typ_Decl));
5352 begin
5353 if Present (Comps)
5354 and then Present (Variant_Part (Comps))
5355 then
5356 Build_Variant_Record_Equality (Typ);
5357 end if;
5358 end;
5360 -- Otherwise create primitive equality operation (AI05-0123)
5362 -- This is done unconditionally to ensure that tools can be linked
5363 -- properly with user programs compiled with older language versions.
5364 -- In addition, this is needed because "=" composes for bounded strings
5365 -- in all language versions (see Exp_Ch4.Expand_Composite_Equality).
5367 elsif Comes_From_Source (Typ)
5368 and then Convention (Typ) = Convention_Ada
5369 and then not Is_Limited_Type (Typ)
5370 then
5371 Build_Untagged_Equality (Typ);
5372 end if;
5374 -- Before building the record initialization procedure, if we are
5375 -- dealing with a concurrent record value type, then we must go through
5376 -- the discriminants, exchanging discriminals between the concurrent
5377 -- type and the concurrent record value type. See the section "Handling
5378 -- of Discriminants" in the Einfo spec for details.
5380 if Is_Concurrent_Record_Type (Typ)
5381 and then Has_Discriminants (Typ)
5382 then
5383 declare
5384 Ctyp : constant Entity_Id :=
5385 Corresponding_Concurrent_Type (Typ);
5386 Conc_Discr : Entity_Id;
5387 Rec_Discr : Entity_Id;
5388 Temp : Entity_Id;
5390 begin
5391 Conc_Discr := First_Discriminant (Ctyp);
5392 Rec_Discr := First_Discriminant (Typ);
5393 while Present (Conc_Discr) loop
5394 Temp := Discriminal (Conc_Discr);
5395 Set_Discriminal (Conc_Discr, Discriminal (Rec_Discr));
5396 Set_Discriminal (Rec_Discr, Temp);
5398 Set_Discriminal_Link (Discriminal (Conc_Discr), Conc_Discr);
5399 Set_Discriminal_Link (Discriminal (Rec_Discr), Rec_Discr);
5401 Next_Discriminant (Conc_Discr);
5402 Next_Discriminant (Rec_Discr);
5403 end loop;
5404 end;
5405 end if;
5407 if Has_Controlled_Component (Typ) then
5408 Build_Controlling_Procs (Typ);
5409 end if;
5411 Adjust_Discriminants (Typ);
5413 -- Do not need init for interfaces on virtual targets since they're
5414 -- abstract.
5416 if Tagged_Type_Expansion or else not Is_Interface (Typ) then
5417 Build_Record_Init_Proc (Typ_Decl, Typ);
5418 end if;
5420 -- For tagged type that are not interfaces, build bodies of primitive
5421 -- operations. Note: do this after building the record initialization
5422 -- procedure, since the primitive operations may need the initialization
5423 -- routine. There is no need to add predefined primitives of interfaces
5424 -- because all their predefined primitives are abstract.
5426 if Is_Tagged_Type (Typ) and then not Is_Interface (Typ) then
5428 -- Do not add the body of predefined primitives in case of CPP tagged
5429 -- type derivations that have convention CPP.
5431 if Is_CPP_Class (Root_Type (Typ))
5432 and then Convention (Typ) = Convention_CPP
5433 then
5434 null;
5436 -- Do not add the body of the predefined primitives if we are
5437 -- compiling under restriction No_Dispatching_Calls or if we are
5438 -- compiling a CPP tagged type.
5440 elsif not Restriction_Active (No_Dispatching_Calls) then
5442 -- Create the body of TSS primitive Finalize_Address. This must
5443 -- be done before the bodies of all predefined primitives are
5444 -- created. If Typ is limited, Stream_Input and Stream_Read may
5445 -- produce build-in-place allocations and for those the expander
5446 -- needs Finalize_Address.
5448 Make_Finalize_Address_Body (Typ);
5449 Predef_List := Predefined_Primitive_Bodies (Typ, Renamed_Eq);
5450 Append_Freeze_Actions (Typ, Predef_List);
5451 end if;
5453 -- Ada 2005 (AI-391): If any wrappers were created for nonoverridden
5454 -- inherited functions, then add their bodies to the freeze actions.
5456 if Present (Wrapper_Body_List) then
5457 Append_Freeze_Actions (Typ, Wrapper_Body_List);
5458 end if;
5460 -- Create extra formals for the primitive operations of the type.
5461 -- This must be done before analyzing the body of the initialization
5462 -- procedure, because a self-referential type might call one of these
5463 -- primitives in the body of the init_proc itself.
5465 declare
5466 Elmt : Elmt_Id;
5467 Subp : Entity_Id;
5469 begin
5470 Elmt := First_Elmt (Primitive_Operations (Typ));
5471 while Present (Elmt) loop
5472 Subp := Node (Elmt);
5473 if not Has_Foreign_Convention (Subp)
5474 and then not Is_Predefined_Dispatching_Operation (Subp)
5475 then
5476 Create_Extra_Formals (Subp);
5477 end if;
5479 Next_Elmt (Elmt);
5480 end loop;
5481 end;
5482 end if;
5483 end Expand_Freeze_Record_Type;
5485 ------------------------------------
5486 -- Expand_N_Full_Type_Declaration --
5487 ------------------------------------
5489 procedure Expand_N_Full_Type_Declaration (N : Node_Id) is
5490 procedure Build_Master (Ptr_Typ : Entity_Id);
5491 -- Create the master associated with Ptr_Typ
5493 ------------------
5494 -- Build_Master --
5495 ------------------
5497 procedure Build_Master (Ptr_Typ : Entity_Id) is
5498 Desig_Typ : Entity_Id := Designated_Type (Ptr_Typ);
5500 begin
5501 -- If the designated type is an incomplete view coming from a
5502 -- limited-with'ed package, we need to use the nonlimited view in
5503 -- case it has tasks.
5505 if Ekind (Desig_Typ) in Incomplete_Kind
5506 and then Present (Non_Limited_View (Desig_Typ))
5507 then
5508 Desig_Typ := Non_Limited_View (Desig_Typ);
5509 end if;
5511 -- Anonymous access types are created for the components of the
5512 -- record parameter for an entry declaration. No master is created
5513 -- for such a type.
5515 if Comes_From_Source (N) and then Has_Task (Desig_Typ) then
5516 Build_Master_Entity (Ptr_Typ);
5517 Build_Master_Renaming (Ptr_Typ);
5519 -- Create a class-wide master because a Master_Id must be generated
5520 -- for access-to-limited-class-wide types whose root may be extended
5521 -- with task components.
5523 -- Note: This code covers access-to-limited-interfaces because they
5524 -- can be used to reference tasks implementing them.
5526 elsif Is_Limited_Class_Wide_Type (Desig_Typ)
5527 and then Tasking_Allowed
5528 then
5529 Build_Class_Wide_Master (Ptr_Typ);
5530 end if;
5531 end Build_Master;
5533 -- Local declarations
5535 Def_Id : constant Entity_Id := Defining_Identifier (N);
5536 B_Id : constant Entity_Id := Base_Type (Def_Id);
5537 FN : Node_Id;
5538 Par_Id : Entity_Id;
5540 -- Start of processing for Expand_N_Full_Type_Declaration
5542 begin
5543 if Is_Access_Type (Def_Id) then
5544 Build_Master (Def_Id);
5546 if Ekind (Def_Id) = E_Access_Protected_Subprogram_Type then
5547 Expand_Access_Protected_Subprogram_Type (N);
5548 end if;
5550 -- Array of anonymous access-to-task pointers
5552 elsif Ada_Version >= Ada_2005
5553 and then Is_Array_Type (Def_Id)
5554 and then Is_Access_Type (Component_Type (Def_Id))
5555 and then Ekind (Component_Type (Def_Id)) = E_Anonymous_Access_Type
5556 then
5557 Build_Master (Component_Type (Def_Id));
5559 elsif Has_Task (Def_Id) then
5560 Expand_Previous_Access_Type (Def_Id);
5562 -- Check the components of a record type or array of records for
5563 -- anonymous access-to-task pointers.
5565 elsif Ada_Version >= Ada_2005
5566 and then (Is_Record_Type (Def_Id)
5567 or else
5568 (Is_Array_Type (Def_Id)
5569 and then Is_Record_Type (Component_Type (Def_Id))))
5570 then
5571 declare
5572 Comp : Entity_Id;
5573 First : Boolean;
5574 M_Id : Entity_Id;
5575 Typ : Entity_Id;
5577 begin
5578 if Is_Array_Type (Def_Id) then
5579 Comp := First_Entity (Component_Type (Def_Id));
5580 else
5581 Comp := First_Entity (Def_Id);
5582 end if;
5584 -- Examine all components looking for anonymous access-to-task
5585 -- types.
5587 First := True;
5588 while Present (Comp) loop
5589 Typ := Etype (Comp);
5591 if Ekind (Typ) = E_Anonymous_Access_Type
5592 and then Has_Task (Available_View (Designated_Type (Typ)))
5593 and then No (Master_Id (Typ))
5594 then
5595 -- Ensure that the record or array type have a _master
5597 if First then
5598 Build_Master_Entity (Def_Id);
5599 Build_Master_Renaming (Typ);
5600 M_Id := Master_Id (Typ);
5602 First := False;
5604 -- Reuse the same master to service any additional types
5606 else
5607 Set_Master_Id (Typ, M_Id);
5608 end if;
5609 end if;
5611 Next_Entity (Comp);
5612 end loop;
5613 end;
5614 end if;
5616 Par_Id := Etype (B_Id);
5618 -- The parent type is private then we need to inherit any TSS operations
5619 -- from the full view.
5621 if Ekind (Par_Id) in Private_Kind
5622 and then Present (Full_View (Par_Id))
5623 then
5624 Par_Id := Base_Type (Full_View (Par_Id));
5625 end if;
5627 if Nkind (Type_Definition (Original_Node (N))) =
5628 N_Derived_Type_Definition
5629 and then not Is_Tagged_Type (Def_Id)
5630 and then Present (Freeze_Node (Par_Id))
5631 and then Present (TSS_Elist (Freeze_Node (Par_Id)))
5632 then
5633 Ensure_Freeze_Node (B_Id);
5634 FN := Freeze_Node (B_Id);
5636 if No (TSS_Elist (FN)) then
5637 Set_TSS_Elist (FN, New_Elmt_List);
5638 end if;
5640 declare
5641 T_E : constant Elist_Id := TSS_Elist (FN);
5642 Elmt : Elmt_Id;
5644 begin
5645 Elmt := First_Elmt (TSS_Elist (Freeze_Node (Par_Id)));
5646 while Present (Elmt) loop
5647 if Chars (Node (Elmt)) /= Name_uInit then
5648 Append_Elmt (Node (Elmt), T_E);
5649 end if;
5651 Next_Elmt (Elmt);
5652 end loop;
5654 -- If the derived type itself is private with a full view, then
5655 -- associate the full view with the inherited TSS_Elist as well.
5657 if Ekind (B_Id) in Private_Kind
5658 and then Present (Full_View (B_Id))
5659 then
5660 Ensure_Freeze_Node (Base_Type (Full_View (B_Id)));
5661 Set_TSS_Elist
5662 (Freeze_Node (Base_Type (Full_View (B_Id))), TSS_Elist (FN));
5663 end if;
5664 end;
5665 end if;
5666 end Expand_N_Full_Type_Declaration;
5668 ---------------------------------
5669 -- Expand_N_Object_Declaration --
5670 ---------------------------------
5672 procedure Expand_N_Object_Declaration (N : Node_Id) is
5673 Loc : constant Source_Ptr := Sloc (N);
5674 Def_Id : constant Entity_Id := Defining_Identifier (N);
5675 Expr : constant Node_Id := Expression (N);
5676 Obj_Def : constant Node_Id := Object_Definition (N);
5677 Typ : constant Entity_Id := Etype (Def_Id);
5678 Base_Typ : constant Entity_Id := Base_Type (Typ);
5679 Expr_Q : Node_Id;
5681 function Build_Equivalent_Aggregate return Boolean;
5682 -- If the object has a constrained discriminated type and no initial
5683 -- value, it may be possible to build an equivalent aggregate instead,
5684 -- and prevent an actual call to the initialization procedure.
5686 procedure Count_Default_Sized_Task_Stacks
5687 (Typ : Entity_Id;
5688 Pri_Stacks : out Int;
5689 Sec_Stacks : out Int);
5690 -- Count the number of default-sized primary and secondary task stacks
5691 -- required for task objects contained within type Typ. If the number of
5692 -- task objects contained within the type is not known at compile time
5693 -- the procedure will return the stack counts of zero.
5695 procedure Default_Initialize_Object (After : Node_Id);
5696 -- Generate all default initialization actions for object Def_Id. Any
5697 -- new code is inserted after node After.
5699 function Rewrite_As_Renaming return Boolean;
5700 -- Indicate whether to rewrite a declaration with initialization into an
5701 -- object renaming declaration (see below).
5703 --------------------------------
5704 -- Build_Equivalent_Aggregate --
5705 --------------------------------
5707 function Build_Equivalent_Aggregate return Boolean is
5708 Aggr : Node_Id;
5709 Comp : Entity_Id;
5710 Discr : Elmt_Id;
5711 Full_Type : Entity_Id;
5713 begin
5714 Full_Type := Typ;
5716 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
5717 Full_Type := Full_View (Typ);
5718 end if;
5720 -- Only perform this transformation if Elaboration_Code is forbidden
5721 -- or undesirable, and if this is a global entity of a constrained
5722 -- record type.
5724 -- If Initialize_Scalars might be active this transformation cannot
5725 -- be performed either, because it will lead to different semantics
5726 -- or because elaboration code will in fact be created.
5728 if Ekind (Full_Type) /= E_Record_Subtype
5729 or else not Has_Discriminants (Full_Type)
5730 or else not Is_Constrained (Full_Type)
5731 or else Is_Controlled (Full_Type)
5732 or else Is_Limited_Type (Full_Type)
5733 or else not Restriction_Active (No_Initialize_Scalars)
5734 then
5735 return False;
5736 end if;
5738 if Ekind (Current_Scope) = E_Package
5739 and then
5740 (Restriction_Active (No_Elaboration_Code)
5741 or else Is_Preelaborated (Current_Scope))
5742 then
5743 -- Building a static aggregate is possible if the discriminants
5744 -- have static values and the other components have static
5745 -- defaults or none.
5747 Discr := First_Elmt (Discriminant_Constraint (Full_Type));
5748 while Present (Discr) loop
5749 if not Is_OK_Static_Expression (Node (Discr)) then
5750 return False;
5751 end if;
5753 Next_Elmt (Discr);
5754 end loop;
5756 -- Check that initialized components are OK, and that non-
5757 -- initialized components do not require a call to their own
5758 -- initialization procedure.
5760 Comp := First_Component (Full_Type);
5761 while Present (Comp) loop
5762 if Ekind (Comp) = E_Component
5763 and then Present (Expression (Parent (Comp)))
5764 and then
5765 not Is_OK_Static_Expression (Expression (Parent (Comp)))
5766 then
5767 return False;
5769 elsif Has_Non_Null_Base_Init_Proc (Etype (Comp)) then
5770 return False;
5772 end if;
5774 Next_Component (Comp);
5775 end loop;
5777 -- Everything is static, assemble the aggregate, discriminant
5778 -- values first.
5780 Aggr :=
5781 Make_Aggregate (Loc,
5782 Expressions => New_List,
5783 Component_Associations => New_List);
5785 Discr := First_Elmt (Discriminant_Constraint (Full_Type));
5786 while Present (Discr) loop
5787 Append_To (Expressions (Aggr), New_Copy (Node (Discr)));
5788 Next_Elmt (Discr);
5789 end loop;
5791 -- Now collect values of initialized components
5793 Comp := First_Component (Full_Type);
5794 while Present (Comp) loop
5795 if Ekind (Comp) = E_Component
5796 and then Present (Expression (Parent (Comp)))
5797 then
5798 Append_To (Component_Associations (Aggr),
5799 Make_Component_Association (Loc,
5800 Choices => New_List (New_Occurrence_Of (Comp, Loc)),
5801 Expression => New_Copy_Tree
5802 (Expression (Parent (Comp)))));
5803 end if;
5805 Next_Component (Comp);
5806 end loop;
5808 -- Finally, box-initialize remaining components
5810 Append_To (Component_Associations (Aggr),
5811 Make_Component_Association (Loc,
5812 Choices => New_List (Make_Others_Choice (Loc)),
5813 Expression => Empty));
5814 Set_Box_Present (Last (Component_Associations (Aggr)));
5815 Set_Expression (N, Aggr);
5817 if Typ /= Full_Type then
5818 Analyze_And_Resolve (Aggr, Full_View (Base_Type (Full_Type)));
5819 Rewrite (Aggr, Unchecked_Convert_To (Typ, Aggr));
5820 Analyze_And_Resolve (Aggr, Typ);
5821 else
5822 Analyze_And_Resolve (Aggr, Full_Type);
5823 end if;
5825 return True;
5827 else
5828 return False;
5829 end if;
5830 end Build_Equivalent_Aggregate;
5832 -------------------------------------
5833 -- Count_Default_Sized_Task_Stacks --
5834 -------------------------------------
5836 procedure Count_Default_Sized_Task_Stacks
5837 (Typ : Entity_Id;
5838 Pri_Stacks : out Int;
5839 Sec_Stacks : out Int)
5841 Component : Entity_Id;
5843 begin
5844 -- To calculate the number of default-sized task stacks required for
5845 -- an object of Typ, a depth-first recursive traversal of the AST
5846 -- from the Typ entity node is undertaken. Only type nodes containing
5847 -- task objects are visited.
5849 Pri_Stacks := 0;
5850 Sec_Stacks := 0;
5852 if not Has_Task (Typ) then
5853 return;
5854 end if;
5856 case Ekind (Typ) is
5857 when E_Task_Subtype
5858 | E_Task_Type
5860 -- A task type is found marking the bottom of the descent. If
5861 -- the type has no representation aspect for the corresponding
5862 -- stack then that stack is using the default size.
5864 if Present (Get_Rep_Item (Typ, Name_Storage_Size)) then
5865 Pri_Stacks := 0;
5866 else
5867 Pri_Stacks := 1;
5868 end if;
5870 if Present (Get_Rep_Item (Typ, Name_Secondary_Stack_Size)) then
5871 Sec_Stacks := 0;
5872 else
5873 Sec_Stacks := 1;
5874 end if;
5876 when E_Array_Subtype
5877 | E_Array_Type
5879 -- First find the number of default stacks contained within an
5880 -- array component.
5882 Count_Default_Sized_Task_Stacks
5883 (Component_Type (Typ),
5884 Pri_Stacks,
5885 Sec_Stacks);
5887 -- Then multiply the result by the size of the array
5889 declare
5890 Quantity : constant Int := Number_Of_Elements_In_Array (Typ);
5891 -- Number_Of_Elements_In_Array is non-trival, consequently
5892 -- its result is captured as an optimization.
5894 begin
5895 Pri_Stacks := Pri_Stacks * Quantity;
5896 Sec_Stacks := Sec_Stacks * Quantity;
5897 end;
5899 when E_Protected_Subtype
5900 | E_Protected_Type
5901 | E_Record_Subtype
5902 | E_Record_Type
5904 Component := First_Component_Or_Discriminant (Typ);
5906 -- Recursively descend each component of the composite type
5907 -- looking for tasks, but only if the component is marked as
5908 -- having a task.
5910 while Present (Component) loop
5911 if Has_Task (Etype (Component)) then
5912 declare
5913 P : Int;
5914 S : Int;
5916 begin
5917 Count_Default_Sized_Task_Stacks
5918 (Etype (Component), P, S);
5919 Pri_Stacks := Pri_Stacks + P;
5920 Sec_Stacks := Sec_Stacks + S;
5921 end;
5922 end if;
5924 Next_Component_Or_Discriminant (Component);
5925 end loop;
5927 when E_Limited_Private_Subtype
5928 | E_Limited_Private_Type
5929 | E_Record_Subtype_With_Private
5930 | E_Record_Type_With_Private
5932 -- Switch to the full view of the private type to continue
5933 -- search.
5935 Count_Default_Sized_Task_Stacks
5936 (Full_View (Typ), Pri_Stacks, Sec_Stacks);
5938 -- Other types should not contain tasks
5940 when others =>
5941 raise Program_Error;
5942 end case;
5943 end Count_Default_Sized_Task_Stacks;
5945 -------------------------------
5946 -- Default_Initialize_Object --
5947 -------------------------------
5949 procedure Default_Initialize_Object (After : Node_Id) is
5950 function New_Object_Reference return Node_Id;
5951 -- Return a new reference to Def_Id with attributes Assignment_OK and
5952 -- Must_Not_Freeze already set.
5954 function Simple_Initialization_OK
5955 (Init_Typ : Entity_Id) return Boolean;
5956 -- Determine whether object declaration N with entity Def_Id needs
5957 -- simple initialization, assuming that it is of type Init_Typ.
5959 --------------------------
5960 -- New_Object_Reference --
5961 --------------------------
5963 function New_Object_Reference return Node_Id is
5964 Obj_Ref : constant Node_Id := New_Occurrence_Of (Def_Id, Loc);
5966 begin
5967 -- The call to the type init proc or [Deep_]Finalize must not
5968 -- freeze the related object as the call is internally generated.
5969 -- This way legal rep clauses that apply to the object will not be
5970 -- flagged. Note that the initialization call may be removed if
5971 -- pragma Import is encountered or moved to the freeze actions of
5972 -- the object because of an address clause.
5974 Set_Assignment_OK (Obj_Ref);
5975 Set_Must_Not_Freeze (Obj_Ref);
5977 return Obj_Ref;
5978 end New_Object_Reference;
5980 ------------------------------
5981 -- Simple_Initialization_OK --
5982 ------------------------------
5984 function Simple_Initialization_OK
5985 (Init_Typ : Entity_Id) return Boolean
5987 begin
5988 -- Do not consider the object declaration if it comes with an
5989 -- initialization expression, or is internal in which case it
5990 -- will be assigned later.
5992 return
5993 not Is_Internal (Def_Id)
5994 and then not Has_Init_Expression (N)
5995 and then Needs_Simple_Initialization
5996 (Typ => Init_Typ,
5997 Consider_IS =>
5998 Initialize_Scalars
5999 and then No (Following_Address_Clause (N)));
6000 end Simple_Initialization_OK;
6002 -- Local variables
6004 Exceptions_OK : constant Boolean :=
6005 not Restriction_Active (No_Exception_Propagation);
6007 Aggr_Init : Node_Id;
6008 Comp_Init : List_Id := No_List;
6009 Fin_Block : Node_Id;
6010 Fin_Call : Node_Id;
6011 Init_Stmts : List_Id := No_List;
6012 Obj_Init : Node_Id := Empty;
6013 Obj_Ref : Node_Id;
6015 -- Start of processing for Default_Initialize_Object
6017 begin
6018 -- Default initialization is suppressed for objects that are already
6019 -- known to be imported (i.e. whose declaration specifies the Import
6020 -- aspect). Note that for objects with a pragma Import, we generate
6021 -- initialization here, and then remove it downstream when processing
6022 -- the pragma. It is also suppressed for variables for which a pragma
6023 -- Suppress_Initialization has been explicitly given
6025 if Is_Imported (Def_Id) or else Suppress_Initialization (Def_Id) then
6026 return;
6028 -- Nothing to do if the object being initialized is of a task type
6029 -- and restriction No_Tasking is in effect, because this is a direct
6030 -- violation of the restriction.
6032 elsif Is_Task_Type (Base_Typ)
6033 and then Restriction_Active (No_Tasking)
6034 then
6035 return;
6036 end if;
6038 -- The expansion performed by this routine is as follows:
6040 -- begin
6041 -- Abort_Defer;
6042 -- Type_Init_Proc (Obj);
6044 -- begin
6045 -- [Deep_]Initialize (Obj);
6047 -- exception
6048 -- when others =>
6049 -- [Deep_]Finalize (Obj, Self => False);
6050 -- raise;
6051 -- end;
6052 -- at end
6053 -- Abort_Undefer_Direct;
6054 -- end;
6056 -- Initialize the components of the object
6058 if Has_Non_Null_Base_Init_Proc (Typ)
6059 and then not No_Initialization (N)
6060 and then not Initialization_Suppressed (Typ)
6061 then
6062 -- Do not initialize the components if No_Default_Initialization
6063 -- applies as the actual restriction check will occur later when
6064 -- the object is frozen as it is not known yet whether the object
6065 -- is imported or not.
6067 if not Restriction_Active (No_Default_Initialization) then
6069 -- If the values of the components are compile-time known, use
6070 -- their prebuilt aggregate form directly.
6072 Aggr_Init := Static_Initialization (Base_Init_Proc (Typ));
6074 if Present (Aggr_Init) then
6075 Set_Expression (N,
6076 New_Copy_Tree (Aggr_Init, New_Scope => Current_Scope));
6078 -- If type has discriminants, try to build an equivalent
6079 -- aggregate using discriminant values from the declaration.
6080 -- This is a useful optimization, in particular if restriction
6081 -- No_Elaboration_Code is active.
6083 elsif Build_Equivalent_Aggregate then
6084 null;
6086 -- Optimize the default initialization of an array object when
6087 -- pragma Initialize_Scalars or Normalize_Scalars is in effect.
6088 -- Construct an in-place initialization aggregate which may be
6089 -- convert into a fast memset by the backend.
6091 elsif Init_Or_Norm_Scalars
6092 and then Is_Array_Type (Typ)
6094 -- The array must lack atomic components because they are
6095 -- treated as non-static, and as a result the backend will
6096 -- not initialize the memory in one go.
6098 and then not Has_Atomic_Components (Typ)
6100 -- The array must not be packed because the invalid values
6101 -- in System.Scalar_Values are multiples of Storage_Unit.
6103 and then not Is_Packed (Typ)
6105 -- The array must have static non-empty ranges, otherwise
6106 -- the backend cannot initialize the memory in one go.
6108 and then Has_Static_Non_Empty_Array_Bounds (Typ)
6110 -- The optimization is only relevant for arrays of scalar
6111 -- types.
6113 and then Is_Scalar_Type (Component_Type (Typ))
6115 -- Similar to regular array initialization using a type
6116 -- init proc, predicate checks are not performed because the
6117 -- initialization values are intentionally invalid, and may
6118 -- violate the predicate.
6120 and then not Has_Predicates (Component_Type (Typ))
6122 -- The component type must have a single initialization value
6124 and then Simple_Initialization_OK (Component_Type (Typ))
6125 then
6126 Set_No_Initialization (N, False);
6127 Set_Expression (N,
6128 Get_Simple_Init_Val
6129 (Typ => Typ,
6130 N => Obj_Def,
6131 Size => Esize (Def_Id)));
6133 Analyze_And_Resolve
6134 (Expression (N), Typ, Suppress => All_Checks);
6136 -- Otherwise invoke the type init proc, generate:
6137 -- Type_Init_Proc (Obj);
6139 else
6140 Obj_Ref := New_Object_Reference;
6142 if Comes_From_Source (Def_Id) then
6143 Initialization_Warning (Obj_Ref);
6144 end if;
6146 Comp_Init := Build_Initialization_Call (Loc, Obj_Ref, Typ);
6147 end if;
6148 end if;
6150 -- Provide a default value if the object needs simple initialization
6152 elsif Simple_Initialization_OK (Typ) then
6153 Set_No_Initialization (N, False);
6154 Set_Expression (N,
6155 Get_Simple_Init_Val
6156 (Typ => Typ,
6157 N => Obj_Def,
6158 Size => Esize (Def_Id)));
6160 Analyze_And_Resolve (Expression (N), Typ);
6161 end if;
6163 -- Initialize the object, generate:
6164 -- [Deep_]Initialize (Obj);
6166 if Needs_Finalization (Typ) and then not No_Initialization (N) then
6167 Obj_Init :=
6168 Make_Init_Call
6169 (Obj_Ref => New_Occurrence_Of (Def_Id, Loc),
6170 Typ => Typ);
6171 end if;
6173 -- Build a special finalization block when both the object and its
6174 -- controlled components are to be initialized. The block finalizes
6175 -- the components if the object initialization fails. Generate:
6177 -- begin
6178 -- <Obj_Init>
6180 -- exception
6181 -- when others =>
6182 -- <Fin_Call>
6183 -- raise;
6184 -- end;
6186 if Has_Controlled_Component (Typ)
6187 and then Present (Comp_Init)
6188 and then Present (Obj_Init)
6189 and then Exceptions_OK
6190 then
6191 Init_Stmts := Comp_Init;
6193 Fin_Call :=
6194 Make_Final_Call
6195 (Obj_Ref => New_Object_Reference,
6196 Typ => Typ,
6197 Skip_Self => True);
6199 if Present (Fin_Call) then
6201 -- Do not emit warnings related to the elaboration order when a
6202 -- controlled object is declared before the body of Finalize is
6203 -- seen.
6205 if Legacy_Elaboration_Checks then
6206 Set_No_Elaboration_Check (Fin_Call);
6207 end if;
6209 Fin_Block :=
6210 Make_Block_Statement (Loc,
6211 Declarations => No_List,
6213 Handled_Statement_Sequence =>
6214 Make_Handled_Sequence_Of_Statements (Loc,
6215 Statements => New_List (Obj_Init),
6217 Exception_Handlers => New_List (
6218 Make_Exception_Handler (Loc,
6219 Exception_Choices => New_List (
6220 Make_Others_Choice (Loc)),
6222 Statements => New_List (
6223 Fin_Call,
6224 Make_Raise_Statement (Loc))))));
6226 -- Signal the ABE mechanism that the block carries out
6227 -- initialization actions.
6229 Set_Is_Initialization_Block (Fin_Block);
6231 Append_To (Init_Stmts, Fin_Block);
6232 end if;
6234 -- Otherwise finalization is not required, the initialization calls
6235 -- are passed to the abort block building circuitry, generate:
6237 -- Type_Init_Proc (Obj);
6238 -- [Deep_]Initialize (Obj);
6240 else
6241 if Present (Comp_Init) then
6242 Init_Stmts := Comp_Init;
6243 end if;
6245 if Present (Obj_Init) then
6246 if No (Init_Stmts) then
6247 Init_Stmts := New_List;
6248 end if;
6250 Append_To (Init_Stmts, Obj_Init);
6251 end if;
6252 end if;
6254 -- Build an abort block to protect the initialization calls
6256 if Abort_Allowed
6257 and then Present (Comp_Init)
6258 and then Present (Obj_Init)
6259 then
6260 -- Generate:
6261 -- Abort_Defer;
6263 Prepend_To (Init_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
6265 -- When exceptions are propagated, abort deferral must take place
6266 -- in the presence of initialization or finalization exceptions.
6267 -- Generate:
6269 -- begin
6270 -- Abort_Defer;
6271 -- <Init_Stmts>
6272 -- at end
6273 -- Abort_Undefer_Direct;
6274 -- end;
6276 if Exceptions_OK then
6277 Init_Stmts := New_List (
6278 Build_Abort_Undefer_Block (Loc,
6279 Stmts => Init_Stmts,
6280 Context => N));
6282 -- Otherwise exceptions are not propagated. Generate:
6284 -- Abort_Defer;
6285 -- <Init_Stmts>
6286 -- Abort_Undefer;
6288 else
6289 Append_To (Init_Stmts,
6290 Build_Runtime_Call (Loc, RE_Abort_Undefer));
6291 end if;
6292 end if;
6294 -- Insert the whole initialization sequence into the tree. If the
6295 -- object has a delayed freeze, as will be the case when it has
6296 -- aspect specifications, the initialization sequence is part of
6297 -- the freeze actions.
6299 if Present (Init_Stmts) then
6300 if Has_Delayed_Freeze (Def_Id) then
6301 Append_Freeze_Actions (Def_Id, Init_Stmts);
6302 else
6303 Insert_Actions_After (After, Init_Stmts);
6304 end if;
6305 end if;
6306 end Default_Initialize_Object;
6308 -------------------------
6309 -- Rewrite_As_Renaming --
6310 -------------------------
6312 function Rewrite_As_Renaming return Boolean is
6313 begin
6314 -- If the object declaration appears in the form
6316 -- Obj : Ctrl_Typ := Func (...);
6318 -- where Ctrl_Typ is controlled but not immutably limited type, then
6319 -- the expansion of the function call should use a dereference of the
6320 -- result to reference the value on the secondary stack.
6322 -- Obj : Ctrl_Typ renames Func (...).all;
6324 -- As a result, the call avoids an extra copy. This an optimization,
6325 -- but it is required for passing ACATS tests in some cases where it
6326 -- would otherwise make two copies. The RM allows removing redunant
6327 -- Adjust/Finalize calls, but does not allow insertion of extra ones.
6329 -- This part is disabled for now, because it breaks GPS builds
6331 return (False -- ???
6332 and then Nkind (Expr_Q) = N_Explicit_Dereference
6333 and then not Comes_From_Source (Expr_Q)
6334 and then Nkind (Original_Node (Expr_Q)) = N_Function_Call
6335 and then Nkind (Object_Definition (N)) in N_Has_Entity
6336 and then (Needs_Finalization (Entity (Object_Definition (N)))))
6338 -- If the initializing expression is for a variable with attribute
6339 -- OK_To_Rename set, then transform:
6341 -- Obj : Typ := Expr;
6343 -- into
6345 -- Obj : Typ renames Expr;
6347 -- provided that Obj is not aliased. The aliased case has to be
6348 -- excluded in general because Expr will not be aliased in
6349 -- general.
6351 or else
6352 (not Aliased_Present (N)
6353 and then Is_Entity_Name (Expr_Q)
6354 and then Ekind (Entity (Expr_Q)) = E_Variable
6355 and then OK_To_Rename (Entity (Expr_Q))
6356 and then Is_Entity_Name (Obj_Def));
6357 end Rewrite_As_Renaming;
6359 -- Local variables
6361 Next_N : constant Node_Id := Next (N);
6363 Adj_Call : Node_Id;
6364 Id_Ref : Node_Id;
6365 Tag_Assign : Node_Id;
6367 Init_After : Node_Id := N;
6368 -- Node after which the initialization actions are to be inserted. This
6369 -- is normally N, except for the case of a shared passive variable, in
6370 -- which case the init proc call must be inserted only after the bodies
6371 -- of the shared variable procedures have been seen.
6373 -- Start of processing for Expand_N_Object_Declaration
6375 begin
6376 -- Don't do anything for deferred constants. All proper actions will be
6377 -- expanded during the full declaration.
6379 if No (Expr) and Constant_Present (N) then
6380 return;
6381 end if;
6383 -- The type of the object cannot be abstract. This is diagnosed at the
6384 -- point the object is frozen, which happens after the declaration is
6385 -- fully expanded, so simply return now.
6387 if Is_Abstract_Type (Typ) then
6388 return;
6389 end if;
6391 -- No action needed for the internal imported dummy object added by
6392 -- Make_DT to compute the offset of the components that reference
6393 -- secondary dispatch tables; required to avoid never-ending loop
6394 -- processing this internal object declaration.
6396 if Tagged_Type_Expansion
6397 and then Is_Internal (Def_Id)
6398 and then Is_Imported (Def_Id)
6399 and then Related_Type (Def_Id) = Implementation_Base_Type (Typ)
6400 then
6401 return;
6402 end if;
6404 -- First we do special processing for objects of a tagged type where
6405 -- this is the point at which the type is frozen. The creation of the
6406 -- dispatch table and the initialization procedure have to be deferred
6407 -- to this point, since we reference previously declared primitive
6408 -- subprograms.
6410 -- Force construction of dispatch tables of library level tagged types
6412 if Tagged_Type_Expansion
6413 and then Building_Static_Dispatch_Tables
6414 and then Is_Library_Level_Entity (Def_Id)
6415 and then Is_Library_Level_Tagged_Type (Base_Typ)
6416 and then Ekind_In (Base_Typ, E_Record_Type,
6417 E_Protected_Type,
6418 E_Task_Type)
6419 and then not Has_Dispatch_Table (Base_Typ)
6420 then
6421 declare
6422 New_Nodes : List_Id := No_List;
6424 begin
6425 if Is_Concurrent_Type (Base_Typ) then
6426 New_Nodes := Make_DT (Corresponding_Record_Type (Base_Typ), N);
6427 else
6428 New_Nodes := Make_DT (Base_Typ, N);
6429 end if;
6431 if not Is_Empty_List (New_Nodes) then
6432 Insert_List_Before (N, New_Nodes);
6433 end if;
6434 end;
6435 end if;
6437 -- Make shared memory routines for shared passive variable
6439 if Is_Shared_Passive (Def_Id) then
6440 Init_After := Make_Shared_Var_Procs (N);
6441 end if;
6443 -- If tasks being declared, make sure we have an activation chain
6444 -- defined for the tasks (has no effect if we already have one), and
6445 -- also that a Master variable is established and that the appropriate
6446 -- enclosing construct is established as a task master.
6448 if Has_Task (Typ) then
6449 Build_Activation_Chain_Entity (N);
6450 Build_Master_Entity (Def_Id);
6451 end if;
6453 -- If No_Implicit_Heap_Allocations or No_Implicit_Task_Allocations
6454 -- restrictions are active then default-sized secondary stacks are
6455 -- generated by the binder and allocated by SS_Init. To provide the
6456 -- binder the number of stacks to generate, the number of default-sized
6457 -- stacks required for task objects contained within the object
6458 -- declaration N is calculated here as it is at this point where
6459 -- unconstrained types become constrained. The result is stored in the
6460 -- enclosing unit's Unit_Record.
6462 -- Note if N is an array object declaration that has an initialization
6463 -- expression, a second object declaration for the initialization
6464 -- expression is created by the compiler. To prevent double counting
6465 -- of the stacks in this scenario, the stacks of the first array are
6466 -- not counted.
6468 if Has_Task (Typ)
6469 and then not Restriction_Active (No_Secondary_Stack)
6470 and then (Restriction_Active (No_Implicit_Heap_Allocations)
6471 or else Restriction_Active (No_Implicit_Task_Allocations))
6472 and then not (Ekind_In (Ekind (Typ), E_Array_Type, E_Array_Subtype)
6473 and then (Has_Init_Expression (N)))
6474 then
6475 declare
6476 PS_Count, SS_Count : Int := 0;
6477 begin
6478 Count_Default_Sized_Task_Stacks (Typ, PS_Count, SS_Count);
6479 Increment_Primary_Stack_Count (PS_Count);
6480 Increment_Sec_Stack_Count (SS_Count);
6481 end;
6482 end if;
6484 -- Default initialization required, and no expression present
6486 if No (Expr) then
6488 -- If we have a type with a variant part, the initialization proc
6489 -- will contain implicit tests of the discriminant values, which
6490 -- counts as a violation of the restriction No_Implicit_Conditionals.
6492 if Has_Variant_Part (Typ) then
6493 declare
6494 Msg : Boolean;
6496 begin
6497 Check_Restriction (Msg, No_Implicit_Conditionals, Obj_Def);
6499 if Msg then
6500 Error_Msg_N
6501 ("\initialization of variant record tests discriminants",
6502 Obj_Def);
6503 return;
6504 end if;
6505 end;
6506 end if;
6508 -- For the default initialization case, if we have a private type
6509 -- with invariants, and invariant checks are enabled, then insert an
6510 -- invariant check after the object declaration. Note that it is OK
6511 -- to clobber the object with an invalid value since if the exception
6512 -- is raised, then the object will go out of scope. In the case where
6513 -- an array object is initialized with an aggregate, the expression
6514 -- is removed. Check flag Has_Init_Expression to avoid generating a
6515 -- junk invariant check and flag No_Initialization to avoid checking
6516 -- an uninitialized object such as a compiler temporary used for an
6517 -- aggregate.
6519 if Has_Invariants (Base_Typ)
6520 and then Present (Invariant_Procedure (Base_Typ))
6521 and then not Has_Init_Expression (N)
6522 and then not No_Initialization (N)
6523 then
6524 -- If entity has an address clause or aspect, make invariant
6525 -- call into a freeze action for the explicit freeze node for
6526 -- object. Otherwise insert invariant check after declaration.
6528 if Present (Following_Address_Clause (N))
6529 or else Has_Aspect (Def_Id, Aspect_Address)
6530 then
6531 Ensure_Freeze_Node (Def_Id);
6532 Set_Has_Delayed_Freeze (Def_Id);
6533 Set_Is_Frozen (Def_Id, False);
6535 if not Partial_View_Has_Unknown_Discr (Typ) then
6536 Append_Freeze_Action (Def_Id,
6537 Make_Invariant_Call (New_Occurrence_Of (Def_Id, Loc)));
6538 end if;
6540 elsif not Partial_View_Has_Unknown_Discr (Typ) then
6541 Insert_After (N,
6542 Make_Invariant_Call (New_Occurrence_Of (Def_Id, Loc)));
6543 end if;
6544 end if;
6546 Default_Initialize_Object (Init_After);
6548 -- Generate attribute for Persistent_BSS if needed
6550 if Persistent_BSS_Mode
6551 and then Comes_From_Source (N)
6552 and then Is_Potentially_Persistent_Type (Typ)
6553 and then not Has_Init_Expression (N)
6554 and then Is_Library_Level_Entity (Def_Id)
6555 then
6556 declare
6557 Prag : Node_Id;
6558 begin
6559 Prag :=
6560 Make_Linker_Section_Pragma
6561 (Def_Id, Sloc (N), ".persistent.bss");
6562 Insert_After (N, Prag);
6563 Analyze (Prag);
6564 end;
6565 end if;
6567 -- If access type, then we know it is null if not initialized
6569 if Is_Access_Type (Typ) then
6570 Set_Is_Known_Null (Def_Id);
6571 end if;
6573 -- Explicit initialization present
6575 else
6576 -- Obtain actual expression from qualified expression
6578 if Nkind (Expr) = N_Qualified_Expression then
6579 Expr_Q := Expression (Expr);
6580 else
6581 Expr_Q := Expr;
6582 end if;
6584 -- When we have the appropriate type of aggregate in the expression
6585 -- (it has been determined during analysis of the aggregate by
6586 -- setting the delay flag), let's perform in place assignment and
6587 -- thus avoid creating a temporary.
6589 if Is_Delayed_Aggregate (Expr_Q) then
6590 Convert_Aggr_In_Object_Decl (N);
6592 -- Ada 2005 (AI-318-02): If the initialization expression is a call
6593 -- to a build-in-place function, then access to the declared object
6594 -- must be passed to the function. Currently we limit such functions
6595 -- to those with constrained limited result subtypes, but eventually
6596 -- plan to expand the allowed forms of functions that are treated as
6597 -- build-in-place.
6599 elsif Is_Build_In_Place_Function_Call (Expr_Q) then
6600 Make_Build_In_Place_Call_In_Object_Declaration (N, Expr_Q);
6602 -- The previous call expands the expression initializing the
6603 -- built-in-place object into further code that will be analyzed
6604 -- later. No further expansion needed here.
6606 return;
6608 -- This is the same as the previous 'elsif', except that the call has
6609 -- been transformed by other expansion activities into something like
6610 -- F(...)'Reference.
6612 elsif Nkind (Expr_Q) = N_Reference
6613 and then Is_Build_In_Place_Function_Call (Prefix (Expr_Q))
6614 and then not Is_Expanded_Build_In_Place_Call
6615 (Unqual_Conv (Prefix (Expr_Q)))
6616 then
6617 Make_Build_In_Place_Call_In_Anonymous_Context (Prefix (Expr_Q));
6619 -- The previous call expands the expression initializing the
6620 -- built-in-place object into further code that will be analyzed
6621 -- later. No further expansion needed here.
6623 return;
6625 -- Ada 2005 (AI-318-02): Specialization of the previous case for
6626 -- expressions containing a build-in-place function call whose
6627 -- returned object covers interface types, and Expr_Q has calls to
6628 -- Ada.Tags.Displace to displace the pointer to the returned build-
6629 -- in-place object to reference the secondary dispatch table of a
6630 -- covered interface type.
6632 elsif Present (Unqual_BIP_Iface_Function_Call (Expr_Q)) then
6633 Make_Build_In_Place_Iface_Call_In_Object_Declaration (N, Expr_Q);
6635 -- The previous call expands the expression initializing the
6636 -- built-in-place object into further code that will be analyzed
6637 -- later. No further expansion needed here.
6639 return;
6641 -- Ada 2005 (AI-251): Rewrite the expression that initializes a
6642 -- class-wide interface object to ensure that we copy the full
6643 -- object, unless we are targetting a VM where interfaces are handled
6644 -- by VM itself. Note that if the root type of Typ is an ancestor of
6645 -- Expr's type, both types share the same dispatch table and there is
6646 -- no need to displace the pointer.
6648 elsif Is_Interface (Typ)
6650 -- Avoid never-ending recursion because if Equivalent_Type is set
6651 -- then we've done it already and must not do it again.
6653 and then not
6654 (Nkind (Obj_Def) = N_Identifier
6655 and then Present (Equivalent_Type (Entity (Obj_Def))))
6656 then
6657 pragma Assert (Is_Class_Wide_Type (Typ));
6659 -- If the object is a return object of an inherently limited type,
6660 -- which implies build-in-place treatment, bypass the special
6661 -- treatment of class-wide interface initialization below. In this
6662 -- case, the expansion of the return statement will take care of
6663 -- creating the object (via allocator) and initializing it.
6665 if Is_Return_Object (Def_Id) and then Is_Limited_View (Typ) then
6666 null;
6668 elsif Tagged_Type_Expansion then
6669 declare
6670 Iface : constant Entity_Id := Root_Type (Typ);
6671 Expr_N : Node_Id := Expr;
6672 Expr_Typ : Entity_Id;
6673 New_Expr : Node_Id;
6674 Obj_Id : Entity_Id;
6675 Tag_Comp : Node_Id;
6677 begin
6678 -- If the original node of the expression was a conversion
6679 -- to this specific class-wide interface type then restore
6680 -- the original node because we must copy the object before
6681 -- displacing the pointer to reference the secondary tag
6682 -- component. This code must be kept synchronized with the
6683 -- expansion done by routine Expand_Interface_Conversion
6685 if not Comes_From_Source (Expr_N)
6686 and then Nkind (Expr_N) = N_Explicit_Dereference
6687 and then Nkind (Original_Node (Expr_N)) = N_Type_Conversion
6688 and then Etype (Original_Node (Expr_N)) = Typ
6689 then
6690 Rewrite (Expr_N, Original_Node (Expression (N)));
6691 end if;
6693 -- Avoid expansion of redundant interface conversion
6695 if Is_Interface (Etype (Expr_N))
6696 and then Nkind (Expr_N) = N_Type_Conversion
6697 and then Etype (Expr_N) = Typ
6698 then
6699 Expr_N := Expression (Expr_N);
6700 Set_Expression (N, Expr_N);
6701 end if;
6703 Obj_Id := Make_Temporary (Loc, 'D', Expr_N);
6704 Expr_Typ := Base_Type (Etype (Expr_N));
6706 if Is_Class_Wide_Type (Expr_Typ) then
6707 Expr_Typ := Root_Type (Expr_Typ);
6708 end if;
6710 -- Replace
6711 -- CW : I'Class := Obj;
6712 -- by
6713 -- Tmp : T := Obj;
6714 -- type Ityp is not null access I'Class;
6715 -- CW : I'Class renames Ityp (Tmp.I_Tag'Address).all;
6717 if Comes_From_Source (Expr_N)
6718 and then Nkind (Expr_N) = N_Identifier
6719 and then not Is_Interface (Expr_Typ)
6720 and then Interface_Present_In_Ancestor (Expr_Typ, Typ)
6721 and then (Expr_Typ = Etype (Expr_Typ)
6722 or else not
6723 Is_Variable_Size_Record (Etype (Expr_Typ)))
6724 then
6725 -- Copy the object
6727 Insert_Action (N,
6728 Make_Object_Declaration (Loc,
6729 Defining_Identifier => Obj_Id,
6730 Object_Definition =>
6731 New_Occurrence_Of (Expr_Typ, Loc),
6732 Expression => Relocate_Node (Expr_N)));
6734 -- Statically reference the tag associated with the
6735 -- interface
6737 Tag_Comp :=
6738 Make_Selected_Component (Loc,
6739 Prefix => New_Occurrence_Of (Obj_Id, Loc),
6740 Selector_Name =>
6741 New_Occurrence_Of
6742 (Find_Interface_Tag (Expr_Typ, Iface), Loc));
6744 -- Replace
6745 -- IW : I'Class := Obj;
6746 -- by
6747 -- type Equiv_Record is record ... end record;
6748 -- implicit subtype CW is <Class_Wide_Subtype>;
6749 -- Tmp : CW := CW!(Obj);
6750 -- type Ityp is not null access I'Class;
6751 -- IW : I'Class renames
6752 -- Ityp!(Displace (Temp'Address, I'Tag)).all;
6754 else
6755 -- Generate the equivalent record type and update the
6756 -- subtype indication to reference it.
6758 Expand_Subtype_From_Expr
6759 (N => N,
6760 Unc_Type => Typ,
6761 Subtype_Indic => Obj_Def,
6762 Exp => Expr_N);
6764 if not Is_Interface (Etype (Expr_N)) then
6765 New_Expr := Relocate_Node (Expr_N);
6767 -- For interface types we use 'Address which displaces
6768 -- the pointer to the base of the object (if required)
6770 else
6771 New_Expr :=
6772 Unchecked_Convert_To (Etype (Obj_Def),
6773 Make_Explicit_Dereference (Loc,
6774 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6775 Make_Attribute_Reference (Loc,
6776 Prefix => Relocate_Node (Expr_N),
6777 Attribute_Name => Name_Address))));
6778 end if;
6780 -- Copy the object
6782 if not Is_Limited_Record (Expr_Typ) then
6783 Insert_Action (N,
6784 Make_Object_Declaration (Loc,
6785 Defining_Identifier => Obj_Id,
6786 Object_Definition =>
6787 New_Occurrence_Of (Etype (Obj_Def), Loc),
6788 Expression => New_Expr));
6790 -- Rename limited type object since they cannot be copied
6791 -- This case occurs when the initialization expression
6792 -- has been previously expanded into a temporary object.
6794 else pragma Assert (not Comes_From_Source (Expr_Q));
6795 Insert_Action (N,
6796 Make_Object_Renaming_Declaration (Loc,
6797 Defining_Identifier => Obj_Id,
6798 Subtype_Mark =>
6799 New_Occurrence_Of (Etype (Obj_Def), Loc),
6800 Name =>
6801 Unchecked_Convert_To
6802 (Etype (Obj_Def), New_Expr)));
6803 end if;
6805 -- Dynamically reference the tag associated with the
6806 -- interface.
6808 Tag_Comp :=
6809 Make_Function_Call (Loc,
6810 Name => New_Occurrence_Of (RTE (RE_Displace), Loc),
6811 Parameter_Associations => New_List (
6812 Make_Attribute_Reference (Loc,
6813 Prefix => New_Occurrence_Of (Obj_Id, Loc),
6814 Attribute_Name => Name_Address),
6815 New_Occurrence_Of
6816 (Node (First_Elmt (Access_Disp_Table (Iface))),
6817 Loc)));
6818 end if;
6820 Rewrite (N,
6821 Make_Object_Renaming_Declaration (Loc,
6822 Defining_Identifier => Make_Temporary (Loc, 'D'),
6823 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
6824 Name =>
6825 Convert_Tag_To_Interface (Typ, Tag_Comp)));
6827 -- If the original entity comes from source, then mark the
6828 -- new entity as needing debug information, even though it's
6829 -- defined by a generated renaming that does not come from
6830 -- source, so that Materialize_Entity will be set on the
6831 -- entity when Debug_Renaming_Declaration is called during
6832 -- analysis.
6834 if Comes_From_Source (Def_Id) then
6835 Set_Debug_Info_Needed (Defining_Identifier (N));
6836 end if;
6838 Analyze (N, Suppress => All_Checks);
6840 -- Replace internal identifier of rewritten node by the
6841 -- identifier found in the sources. We also have to exchange
6842 -- entities containing their defining identifiers to ensure
6843 -- the correct replacement of the object declaration by this
6844 -- object renaming declaration because these identifiers
6845 -- were previously added by Enter_Name to the current scope.
6846 -- We must preserve the homonym chain of the source entity
6847 -- as well. We must also preserve the kind of the entity,
6848 -- which may be a constant. Preserve entity chain because
6849 -- itypes may have been generated already, and the full
6850 -- chain must be preserved for final freezing. Finally,
6851 -- preserve Comes_From_Source setting, so that debugging
6852 -- and cross-referencing information is properly kept, and
6853 -- preserve source location, to prevent spurious errors when
6854 -- entities are declared (they must have their own Sloc).
6856 declare
6857 New_Id : constant Entity_Id := Defining_Identifier (N);
6858 Next_Temp : constant Entity_Id := Next_Entity (New_Id);
6859 Save_CFS : constant Boolean :=
6860 Comes_From_Source (Def_Id);
6861 Save_SP : constant Node_Id := SPARK_Pragma (Def_Id);
6862 Save_SPI : constant Boolean :=
6863 SPARK_Pragma_Inherited (Def_Id);
6865 begin
6866 Link_Entities (New_Id, Next_Entity (Def_Id));
6867 Link_Entities (Def_Id, Next_Temp);
6869 Set_Chars (Defining_Identifier (N), Chars (Def_Id));
6870 Set_Homonym (Defining_Identifier (N), Homonym (Def_Id));
6871 Set_Ekind (Defining_Identifier (N), Ekind (Def_Id));
6872 Set_Sloc (Defining_Identifier (N), Sloc (Def_Id));
6874 Set_Comes_From_Source (Def_Id, False);
6876 -- ??? This is extremely dangerous!!! Exchanging entities
6877 -- is very low level, and as a result it resets flags and
6878 -- fields which belong to the original Def_Id. Several of
6879 -- these attributes are saved and restored, but there may
6880 -- be many more that need to be preserverd.
6882 Exchange_Entities (Defining_Identifier (N), Def_Id);
6884 -- Restore clobbered attributes
6886 Set_Comes_From_Source (Def_Id, Save_CFS);
6887 Set_SPARK_Pragma (Def_Id, Save_SP);
6888 Set_SPARK_Pragma_Inherited (Def_Id, Save_SPI);
6889 end;
6890 end;
6891 end if;
6893 return;
6895 -- Common case of explicit object initialization
6897 else
6898 -- In most cases, we must check that the initial value meets any
6899 -- constraint imposed by the declared type. However, there is one
6900 -- very important exception to this rule. If the entity has an
6901 -- unconstrained nominal subtype, then it acquired its constraints
6902 -- from the expression in the first place, and not only does this
6903 -- mean that the constraint check is not needed, but an attempt to
6904 -- perform the constraint check can cause order of elaboration
6905 -- problems.
6907 if not Is_Constr_Subt_For_U_Nominal (Typ) then
6909 -- If this is an allocator for an aggregate that has been
6910 -- allocated in place, delay checks until assignments are
6911 -- made, because the discriminants are not initialized.
6913 if Nkind (Expr) = N_Allocator
6914 and then No_Initialization (Expr)
6915 then
6916 null;
6918 -- Otherwise apply a constraint check now if no prev error
6920 elsif Nkind (Expr) /= N_Error then
6921 Apply_Constraint_Check (Expr, Typ);
6923 -- Deal with possible range check
6925 if Do_Range_Check (Expr) then
6927 -- If assignment checks are suppressed, turn off flag
6929 if Suppress_Assignment_Checks (N) then
6930 Set_Do_Range_Check (Expr, False);
6932 -- Otherwise generate the range check
6934 else
6935 Generate_Range_Check
6936 (Expr, Typ, CE_Range_Check_Failed);
6937 end if;
6938 end if;
6939 end if;
6940 end if;
6942 -- If the type is controlled and not inherently limited, then
6943 -- the target is adjusted after the copy and attached to the
6944 -- finalization list. However, no adjustment is done in the case
6945 -- where the object was initialized by a call to a function whose
6946 -- result is built in place, since no copy occurred. Similarly, no
6947 -- adjustment is required if we are going to rewrite the object
6948 -- declaration into a renaming declaration.
6950 if Needs_Finalization (Typ)
6951 and then not Is_Limited_View (Typ)
6952 and then not Rewrite_As_Renaming
6953 then
6954 Adj_Call :=
6955 Make_Adjust_Call (
6956 Obj_Ref => New_Occurrence_Of (Def_Id, Loc),
6957 Typ => Base_Typ);
6959 -- Guard against a missing [Deep_]Adjust when the base type
6960 -- was not properly frozen.
6962 if Present (Adj_Call) then
6963 Insert_Action_After (Init_After, Adj_Call);
6964 end if;
6965 end if;
6967 -- For tagged types, when an init value is given, the tag has to
6968 -- be re-initialized separately in order to avoid the propagation
6969 -- of a wrong tag coming from a view conversion unless the type
6970 -- is class wide (in this case the tag comes from the init value).
6971 -- Suppress the tag assignment when not Tagged_Type_Expansion
6972 -- because tags are represented implicitly in objects. Ditto for
6973 -- types that are CPP_CLASS, and for initializations that are
6974 -- aggregates, because they have to have the right tag.
6976 -- The re-assignment of the tag has to be done even if the object
6977 -- is a constant. The assignment must be analyzed after the
6978 -- declaration. If an address clause follows, this is handled as
6979 -- part of the freeze actions for the object, otherwise insert
6980 -- tag assignment here.
6982 Tag_Assign := Make_Tag_Assignment (N);
6984 if Present (Tag_Assign) then
6985 if Present (Following_Address_Clause (N)) then
6986 Ensure_Freeze_Node (Def_Id);
6988 else
6989 Insert_Action_After (Init_After, Tag_Assign);
6990 end if;
6992 -- Handle C++ constructor calls. Note that we do not check that
6993 -- Typ is a tagged type since the equivalent Ada type of a C++
6994 -- class that has no virtual methods is an untagged limited
6995 -- record type.
6997 elsif Is_CPP_Constructor_Call (Expr) then
6999 -- The call to the initialization procedure does NOT freeze the
7000 -- object being initialized.
7002 Id_Ref := New_Occurrence_Of (Def_Id, Loc);
7003 Set_Must_Not_Freeze (Id_Ref);
7004 Set_Assignment_OK (Id_Ref);
7006 Insert_Actions_After (Init_After,
7007 Build_Initialization_Call (Loc, Id_Ref, Typ,
7008 Constructor_Ref => Expr));
7010 -- We remove here the original call to the constructor
7011 -- to avoid its management in the backend
7013 Set_Expression (N, Empty);
7014 return;
7016 -- Handle initialization of limited tagged types
7018 elsif Is_Tagged_Type (Typ)
7019 and then Is_Class_Wide_Type (Typ)
7020 and then Is_Limited_Record (Typ)
7021 and then not Is_Limited_Interface (Typ)
7022 then
7023 -- Given that the type is limited we cannot perform a copy. If
7024 -- Expr_Q is the reference to a variable we mark the variable
7025 -- as OK_To_Rename to expand this declaration into a renaming
7026 -- declaration (see bellow).
7028 if Is_Entity_Name (Expr_Q) then
7029 Set_OK_To_Rename (Entity (Expr_Q));
7031 -- If we cannot convert the expression into a renaming we must
7032 -- consider it an internal error because the backend does not
7033 -- have support to handle it. Also, when a raise expression is
7034 -- encountered we ignore it since it doesn't return a value and
7035 -- thus cannot trigger a copy.
7037 elsif Nkind (Original_Node (Expr_Q)) /= N_Raise_Expression then
7038 pragma Assert (False);
7039 raise Program_Error;
7040 end if;
7042 -- For discrete types, set the Is_Known_Valid flag if the
7043 -- initializing value is known to be valid. Only do this for
7044 -- source assignments, since otherwise we can end up turning
7045 -- on the known valid flag prematurely from inserted code.
7047 elsif Comes_From_Source (N)
7048 and then Is_Discrete_Type (Typ)
7049 and then Expr_Known_Valid (Expr)
7050 then
7051 Set_Is_Known_Valid (Def_Id);
7053 elsif Is_Access_Type (Typ) then
7055 -- For access types set the Is_Known_Non_Null flag if the
7056 -- initializing value is known to be non-null. We can also set
7057 -- Can_Never_Be_Null if this is a constant.
7059 if Known_Non_Null (Expr) then
7060 Set_Is_Known_Non_Null (Def_Id, True);
7062 if Constant_Present (N) then
7063 Set_Can_Never_Be_Null (Def_Id);
7064 end if;
7065 end if;
7066 end if;
7068 -- If validity checking on copies, validate initial expression.
7069 -- But skip this if declaration is for a generic type, since it
7070 -- makes no sense to validate generic types. Not clear if this
7071 -- can happen for legal programs, but it definitely can arise
7072 -- from previous instantiation errors.
7074 if Validity_Checks_On
7075 and then Comes_From_Source (N)
7076 and then Validity_Check_Copies
7077 and then not Is_Generic_Type (Etype (Def_Id))
7078 then
7079 Ensure_Valid (Expr);
7080 Set_Is_Known_Valid (Def_Id);
7081 end if;
7082 end if;
7084 -- Cases where the back end cannot handle the initialization
7085 -- directly. In such cases, we expand an assignment that will
7086 -- be appropriately handled by Expand_N_Assignment_Statement.
7088 -- The exclusion of the unconstrained case is wrong, but for now it
7089 -- is too much trouble ???
7091 if (Is_Possibly_Unaligned_Slice (Expr)
7092 or else (Is_Possibly_Unaligned_Object (Expr)
7093 and then not Represented_As_Scalar (Etype (Expr))))
7094 and then not (Is_Array_Type (Etype (Expr))
7095 and then not Is_Constrained (Etype (Expr)))
7096 then
7097 declare
7098 Stat : constant Node_Id :=
7099 Make_Assignment_Statement (Loc,
7100 Name => New_Occurrence_Of (Def_Id, Loc),
7101 Expression => Relocate_Node (Expr));
7102 begin
7103 Set_Expression (N, Empty);
7104 Set_No_Initialization (N);
7105 Set_Assignment_OK (Name (Stat));
7106 Set_No_Ctrl_Actions (Stat);
7107 Insert_After_And_Analyze (Init_After, Stat);
7108 end;
7109 end if;
7110 end if;
7112 if Nkind (Obj_Def) = N_Access_Definition
7113 and then not Is_Local_Anonymous_Access (Etype (Def_Id))
7114 then
7115 -- An Ada 2012 stand-alone object of an anonymous access type
7117 declare
7118 Loc : constant Source_Ptr := Sloc (N);
7120 Level : constant Entity_Id :=
7121 Make_Defining_Identifier (Sloc (N),
7122 Chars =>
7123 New_External_Name (Chars (Def_Id), Suffix => "L"));
7125 Level_Expr : Node_Id;
7126 Level_Decl : Node_Id;
7128 begin
7129 Set_Ekind (Level, Ekind (Def_Id));
7130 Set_Etype (Level, Standard_Natural);
7131 Set_Scope (Level, Scope (Def_Id));
7133 if No (Expr) then
7135 -- Set accessibility level of null
7137 Level_Expr :=
7138 Make_Integer_Literal (Loc, Scope_Depth (Standard_Standard));
7140 else
7141 Level_Expr := Dynamic_Accessibility_Level (Expr);
7142 end if;
7144 Level_Decl :=
7145 Make_Object_Declaration (Loc,
7146 Defining_Identifier => Level,
7147 Object_Definition =>
7148 New_Occurrence_Of (Standard_Natural, Loc),
7149 Expression => Level_Expr,
7150 Constant_Present => Constant_Present (N),
7151 Has_Init_Expression => True);
7153 Insert_Action_After (Init_After, Level_Decl);
7155 Set_Extra_Accessibility (Def_Id, Level);
7156 end;
7157 end if;
7159 -- If the object is default initialized and its type is subject to
7160 -- pragma Default_Initial_Condition, add a runtime check to verify
7161 -- the assumption of the pragma (SPARK RM 7.3.3). Generate:
7163 -- <Base_Typ>DIC (<Base_Typ> (Def_Id));
7165 -- Note that the check is generated for source objects only
7167 if Comes_From_Source (Def_Id)
7168 and then Has_DIC (Typ)
7169 and then Present (DIC_Procedure (Typ))
7170 and then not Has_Init_Expression (N)
7171 then
7172 declare
7173 DIC_Call : constant Node_Id := Build_DIC_Call (Loc, Def_Id, Typ);
7175 begin
7176 if Present (Next_N) then
7177 Insert_Before_And_Analyze (Next_N, DIC_Call);
7179 -- The object declaration is the last node in a declarative or a
7180 -- statement list.
7182 else
7183 Append_To (List_Containing (N), DIC_Call);
7184 Analyze (DIC_Call);
7185 end if;
7186 end;
7187 end if;
7189 -- Final transformation - turn the object declaration into a renaming
7190 -- if appropriate. If this is the completion of a deferred constant
7191 -- declaration, then this transformation generates what would be
7192 -- illegal code if written by hand, but that's OK.
7194 if Present (Expr) then
7195 if Rewrite_As_Renaming then
7196 Rewrite (N,
7197 Make_Object_Renaming_Declaration (Loc,
7198 Defining_Identifier => Defining_Identifier (N),
7199 Subtype_Mark => Obj_Def,
7200 Name => Expr_Q));
7202 -- We do not analyze this renaming declaration, because all its
7203 -- components have already been analyzed, and if we were to go
7204 -- ahead and analyze it, we would in effect be trying to generate
7205 -- another declaration of X, which won't do.
7207 Set_Renamed_Object (Defining_Identifier (N), Expr_Q);
7208 Set_Analyzed (N);
7210 -- We do need to deal with debug issues for this renaming
7212 -- First, if entity comes from source, then mark it as needing
7213 -- debug information, even though it is defined by a generated
7214 -- renaming that does not come from source.
7216 if Comes_From_Source (Defining_Identifier (N)) then
7217 Set_Debug_Info_Needed (Defining_Identifier (N));
7218 end if;
7220 -- Now call the routine to generate debug info for the renaming
7222 declare
7223 Decl : constant Node_Id := Debug_Renaming_Declaration (N);
7224 begin
7225 if Present (Decl) then
7226 Insert_Action (N, Decl);
7227 end if;
7228 end;
7229 end if;
7230 end if;
7232 -- Exception on library entity not available
7234 exception
7235 when RE_Not_Available =>
7236 return;
7237 end Expand_N_Object_Declaration;
7239 ---------------------------------
7240 -- Expand_N_Subtype_Indication --
7241 ---------------------------------
7243 -- Add a check on the range of the subtype. The static case is partially
7244 -- duplicated by Process_Range_Expr_In_Decl in Sem_Ch3, but we still need
7245 -- to check here for the static case in order to avoid generating
7246 -- extraneous expanded code. Also deal with validity checking.
7248 procedure Expand_N_Subtype_Indication (N : Node_Id) is
7249 Ran : constant Node_Id := Range_Expression (Constraint (N));
7250 Typ : constant Entity_Id := Entity (Subtype_Mark (N));
7252 begin
7253 if Nkind (Constraint (N)) = N_Range_Constraint then
7254 Validity_Check_Range (Range_Expression (Constraint (N)));
7255 end if;
7257 if Nkind_In (Parent (N), N_Constrained_Array_Definition, N_Slice) then
7258 Apply_Range_Check (Ran, Typ);
7259 end if;
7260 end Expand_N_Subtype_Indication;
7262 ---------------------------
7263 -- Expand_N_Variant_Part --
7264 ---------------------------
7266 -- Note: this procedure no longer has any effect. It used to be that we
7267 -- would replace the choices in the last variant by a when others, and
7268 -- also expanded static predicates in variant choices here, but both of
7269 -- those activities were being done too early, since we can't check the
7270 -- choices until the statically predicated subtypes are frozen, which can
7271 -- happen as late as the free point of the record, and we can't change the
7272 -- last choice to an others before checking the choices, which is now done
7273 -- at the freeze point of the record.
7275 procedure Expand_N_Variant_Part (N : Node_Id) is
7276 begin
7277 null;
7278 end Expand_N_Variant_Part;
7280 ---------------------------------
7281 -- Expand_Previous_Access_Type --
7282 ---------------------------------
7284 procedure Expand_Previous_Access_Type (Def_Id : Entity_Id) is
7285 Ptr_Typ : Entity_Id;
7287 begin
7288 -- Find all access types in the current scope whose designated type is
7289 -- Def_Id and build master renamings for them.
7291 Ptr_Typ := First_Entity (Current_Scope);
7292 while Present (Ptr_Typ) loop
7293 if Is_Access_Type (Ptr_Typ)
7294 and then Designated_Type (Ptr_Typ) = Def_Id
7295 and then No (Master_Id (Ptr_Typ))
7296 then
7297 -- Ensure that the designated type has a master
7299 Build_Master_Entity (Def_Id);
7301 -- Private and incomplete types complicate the insertion of master
7302 -- renamings because the access type may precede the full view of
7303 -- the designated type. For this reason, the master renamings are
7304 -- inserted relative to the designated type.
7306 Build_Master_Renaming (Ptr_Typ, Ins_Nod => Parent (Def_Id));
7307 end if;
7309 Next_Entity (Ptr_Typ);
7310 end loop;
7311 end Expand_Previous_Access_Type;
7313 -----------------------------
7314 -- Expand_Record_Extension --
7315 -----------------------------
7317 -- Add a field _parent at the beginning of the record extension. This is
7318 -- used to implement inheritance. Here are some examples of expansion:
7320 -- 1. no discriminants
7321 -- type T2 is new T1 with null record;
7322 -- gives
7323 -- type T2 is new T1 with record
7324 -- _Parent : T1;
7325 -- end record;
7327 -- 2. renamed discriminants
7328 -- type T2 (B, C : Int) is new T1 (A => B) with record
7329 -- _Parent : T1 (A => B);
7330 -- D : Int;
7331 -- end;
7333 -- 3. inherited discriminants
7334 -- type T2 is new T1 with record -- discriminant A inherited
7335 -- _Parent : T1 (A);
7336 -- D : Int;
7337 -- end;
7339 procedure Expand_Record_Extension (T : Entity_Id; Def : Node_Id) is
7340 Indic : constant Node_Id := Subtype_Indication (Def);
7341 Loc : constant Source_Ptr := Sloc (Def);
7342 Rec_Ext_Part : Node_Id := Record_Extension_Part (Def);
7343 Par_Subtype : Entity_Id;
7344 Comp_List : Node_Id;
7345 Comp_Decl : Node_Id;
7346 Parent_N : Node_Id;
7347 D : Entity_Id;
7348 List_Constr : constant List_Id := New_List;
7350 begin
7351 -- Expand_Record_Extension is called directly from the semantics, so
7352 -- we must check to see whether expansion is active before proceeding,
7353 -- because this affects the visibility of selected components in bodies
7354 -- of instances.
7356 if not Expander_Active then
7357 return;
7358 end if;
7360 -- This may be a derivation of an untagged private type whose full
7361 -- view is tagged, in which case the Derived_Type_Definition has no
7362 -- extension part. Build an empty one now.
7364 if No (Rec_Ext_Part) then
7365 Rec_Ext_Part :=
7366 Make_Record_Definition (Loc,
7367 End_Label => Empty,
7368 Component_List => Empty,
7369 Null_Present => True);
7371 Set_Record_Extension_Part (Def, Rec_Ext_Part);
7372 Mark_Rewrite_Insertion (Rec_Ext_Part);
7373 end if;
7375 Comp_List := Component_List (Rec_Ext_Part);
7377 Parent_N := Make_Defining_Identifier (Loc, Name_uParent);
7379 -- If the derived type inherits its discriminants the type of the
7380 -- _parent field must be constrained by the inherited discriminants
7382 if Has_Discriminants (T)
7383 and then Nkind (Indic) /= N_Subtype_Indication
7384 and then not Is_Constrained (Entity (Indic))
7385 then
7386 D := First_Discriminant (T);
7387 while Present (D) loop
7388 Append_To (List_Constr, New_Occurrence_Of (D, Loc));
7389 Next_Discriminant (D);
7390 end loop;
7392 Par_Subtype :=
7393 Process_Subtype (
7394 Make_Subtype_Indication (Loc,
7395 Subtype_Mark => New_Occurrence_Of (Entity (Indic), Loc),
7396 Constraint =>
7397 Make_Index_Or_Discriminant_Constraint (Loc,
7398 Constraints => List_Constr)),
7399 Def);
7401 -- Otherwise the original subtype_indication is just what is needed
7403 else
7404 Par_Subtype := Process_Subtype (New_Copy_Tree (Indic), Def);
7405 end if;
7407 Set_Parent_Subtype (T, Par_Subtype);
7409 Comp_Decl :=
7410 Make_Component_Declaration (Loc,
7411 Defining_Identifier => Parent_N,
7412 Component_Definition =>
7413 Make_Component_Definition (Loc,
7414 Aliased_Present => False,
7415 Subtype_Indication => New_Occurrence_Of (Par_Subtype, Loc)));
7417 if Null_Present (Rec_Ext_Part) then
7418 Set_Component_List (Rec_Ext_Part,
7419 Make_Component_List (Loc,
7420 Component_Items => New_List (Comp_Decl),
7421 Variant_Part => Empty,
7422 Null_Present => False));
7423 Set_Null_Present (Rec_Ext_Part, False);
7425 elsif Null_Present (Comp_List)
7426 or else Is_Empty_List (Component_Items (Comp_List))
7427 then
7428 Set_Component_Items (Comp_List, New_List (Comp_Decl));
7429 Set_Null_Present (Comp_List, False);
7431 else
7432 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
7433 end if;
7435 Analyze (Comp_Decl);
7436 end Expand_Record_Extension;
7438 ------------------------
7439 -- Expand_Tagged_Root --
7440 ------------------------
7442 procedure Expand_Tagged_Root (T : Entity_Id) is
7443 Def : constant Node_Id := Type_Definition (Parent (T));
7444 Comp_List : Node_Id;
7445 Comp_Decl : Node_Id;
7446 Sloc_N : Source_Ptr;
7448 begin
7449 if Null_Present (Def) then
7450 Set_Component_List (Def,
7451 Make_Component_List (Sloc (Def),
7452 Component_Items => Empty_List,
7453 Variant_Part => Empty,
7454 Null_Present => True));
7455 end if;
7457 Comp_List := Component_List (Def);
7459 if Null_Present (Comp_List)
7460 or else Is_Empty_List (Component_Items (Comp_List))
7461 then
7462 Sloc_N := Sloc (Comp_List);
7463 else
7464 Sloc_N := Sloc (First (Component_Items (Comp_List)));
7465 end if;
7467 Comp_Decl :=
7468 Make_Component_Declaration (Sloc_N,
7469 Defining_Identifier => First_Tag_Component (T),
7470 Component_Definition =>
7471 Make_Component_Definition (Sloc_N,
7472 Aliased_Present => False,
7473 Subtype_Indication => New_Occurrence_Of (RTE (RE_Tag), Sloc_N)));
7475 if Null_Present (Comp_List)
7476 or else Is_Empty_List (Component_Items (Comp_List))
7477 then
7478 Set_Component_Items (Comp_List, New_List (Comp_Decl));
7479 Set_Null_Present (Comp_List, False);
7481 else
7482 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
7483 end if;
7485 -- We don't Analyze the whole expansion because the tag component has
7486 -- already been analyzed previously. Here we just insure that the tree
7487 -- is coherent with the semantic decoration
7489 Find_Type (Subtype_Indication (Component_Definition (Comp_Decl)));
7491 exception
7492 when RE_Not_Available =>
7493 return;
7494 end Expand_Tagged_Root;
7496 ------------------------------
7497 -- Freeze_Stream_Operations --
7498 ------------------------------
7500 procedure Freeze_Stream_Operations (N : Node_Id; Typ : Entity_Id) is
7501 Names : constant array (1 .. 4) of TSS_Name_Type :=
7502 (TSS_Stream_Input,
7503 TSS_Stream_Output,
7504 TSS_Stream_Read,
7505 TSS_Stream_Write);
7506 Stream_Op : Entity_Id;
7508 begin
7509 -- Primitive operations of tagged types are frozen when the dispatch
7510 -- table is constructed.
7512 if not Comes_From_Source (Typ) or else Is_Tagged_Type (Typ) then
7513 return;
7514 end if;
7516 for J in Names'Range loop
7517 Stream_Op := TSS (Typ, Names (J));
7519 if Present (Stream_Op)
7520 and then Is_Subprogram (Stream_Op)
7521 and then Nkind (Unit_Declaration_Node (Stream_Op)) =
7522 N_Subprogram_Declaration
7523 and then not Is_Frozen (Stream_Op)
7524 then
7525 Append_Freeze_Actions (Typ, Freeze_Entity (Stream_Op, N));
7526 end if;
7527 end loop;
7528 end Freeze_Stream_Operations;
7530 -----------------
7531 -- Freeze_Type --
7532 -----------------
7534 -- Full type declarations are expanded at the point at which the type is
7535 -- frozen. The formal N is the Freeze_Node for the type. Any statements or
7536 -- declarations generated by the freezing (e.g. the procedure generated
7537 -- for initialization) are chained in the Actions field list of the freeze
7538 -- node using Append_Freeze_Actions.
7540 -- WARNING: This routine manages Ghost regions. Return statements must be
7541 -- replaced by gotos which jump to the end of the routine and restore the
7542 -- Ghost mode.
7544 function Freeze_Type (N : Node_Id) return Boolean is
7545 procedure Process_RACW_Types (Typ : Entity_Id);
7546 -- Validate and generate stubs for all RACW types associated with type
7547 -- Typ.
7549 procedure Process_Pending_Access_Types (Typ : Entity_Id);
7550 -- Associate type Typ's Finalize_Address primitive with the finalization
7551 -- masters of pending access-to-Typ types.
7553 ------------------------
7554 -- Process_RACW_Types --
7555 ------------------------
7557 procedure Process_RACW_Types (Typ : Entity_Id) is
7558 List : constant Elist_Id := Access_Types_To_Process (N);
7559 E : Elmt_Id;
7560 Seen : Boolean := False;
7562 begin
7563 if Present (List) then
7564 E := First_Elmt (List);
7565 while Present (E) loop
7566 if Is_Remote_Access_To_Class_Wide_Type (Node (E)) then
7567 Validate_RACW_Primitives (Node (E));
7568 Seen := True;
7569 end if;
7571 Next_Elmt (E);
7572 end loop;
7573 end if;
7575 -- If there are RACWs designating this type, make stubs now
7577 if Seen then
7578 Remote_Types_Tagged_Full_View_Encountered (Typ);
7579 end if;
7580 end Process_RACW_Types;
7582 ----------------------------------
7583 -- Process_Pending_Access_Types --
7584 ----------------------------------
7586 procedure Process_Pending_Access_Types (Typ : Entity_Id) is
7587 E : Elmt_Id;
7589 begin
7590 -- Finalize_Address is not generated in CodePeer mode because the
7591 -- body contains address arithmetic. This processing is disabled.
7593 if CodePeer_Mode then
7594 null;
7596 -- Certain itypes are generated for contexts that cannot allocate
7597 -- objects and should not set primitive Finalize_Address.
7599 elsif Is_Itype (Typ)
7600 and then Nkind (Associated_Node_For_Itype (Typ)) =
7601 N_Explicit_Dereference
7602 then
7603 null;
7605 -- When an access type is declared after the incomplete view of a
7606 -- Taft-amendment type, the access type is considered pending in
7607 -- case the full view of the Taft-amendment type is controlled. If
7608 -- this is indeed the case, associate the Finalize_Address routine
7609 -- of the full view with the finalization masters of all pending
7610 -- access types. This scenario applies to anonymous access types as
7611 -- well.
7613 elsif Needs_Finalization (Typ)
7614 and then Present (Pending_Access_Types (Typ))
7615 then
7616 E := First_Elmt (Pending_Access_Types (Typ));
7617 while Present (E) loop
7619 -- Generate:
7620 -- Set_Finalize_Address
7621 -- (Ptr_Typ, <Typ>FD'Unrestricted_Access);
7623 Append_Freeze_Action (Typ,
7624 Make_Set_Finalize_Address_Call
7625 (Loc => Sloc (N),
7626 Ptr_Typ => Node (E)));
7628 Next_Elmt (E);
7629 end loop;
7630 end if;
7631 end Process_Pending_Access_Types;
7633 -- Local variables
7635 Def_Id : constant Entity_Id := Entity (N);
7637 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
7638 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
7639 -- Save the Ghost-related attributes to restore on exit
7641 Result : Boolean := False;
7643 -- Start of processing for Freeze_Type
7645 begin
7646 -- The type being frozen may be subject to pragma Ghost. Set the mode
7647 -- now to ensure that any nodes generated during freezing are properly
7648 -- marked as Ghost.
7650 Set_Ghost_Mode (Def_Id);
7652 -- Process any remote access-to-class-wide types designating the type
7653 -- being frozen.
7655 Process_RACW_Types (Def_Id);
7657 -- Freeze processing for record types
7659 if Is_Record_Type (Def_Id) then
7660 if Ekind (Def_Id) = E_Record_Type then
7661 Expand_Freeze_Record_Type (N);
7662 elsif Is_Class_Wide_Type (Def_Id) then
7663 Expand_Freeze_Class_Wide_Type (N);
7664 end if;
7666 -- Freeze processing for array types
7668 elsif Is_Array_Type (Def_Id) then
7669 Expand_Freeze_Array_Type (N);
7671 -- Freeze processing for access types
7673 -- For pool-specific access types, find out the pool object used for
7674 -- this type, needs actual expansion of it in some cases. Here are the
7675 -- different cases :
7677 -- 1. Rep Clause "for Def_Id'Storage_Size use 0;"
7678 -- ---> don't use any storage pool
7680 -- 2. Rep Clause : for Def_Id'Storage_Size use Expr.
7681 -- Expand:
7682 -- Def_Id__Pool : Stack_Bounded_Pool (Expr, DT'Size, DT'Alignment);
7684 -- 3. Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
7685 -- ---> Storage Pool is the specified one
7687 -- See GNAT Pool packages in the Run-Time for more details
7689 elsif Ekind_In (Def_Id, E_Access_Type, E_General_Access_Type) then
7690 declare
7691 Loc : constant Source_Ptr := Sloc (N);
7692 Desig_Type : constant Entity_Id := Designated_Type (Def_Id);
7694 Freeze_Action_Typ : Entity_Id;
7695 Pool_Object : Entity_Id;
7697 begin
7698 -- Case 1
7700 -- Rep Clause "for Def_Id'Storage_Size use 0;"
7701 -- ---> don't use any storage pool
7703 if No_Pool_Assigned (Def_Id) then
7704 null;
7706 -- Case 2
7708 -- Rep Clause : for Def_Id'Storage_Size use Expr.
7709 -- ---> Expand:
7710 -- Def_Id__Pool : Stack_Bounded_Pool
7711 -- (Expr, DT'Size, DT'Alignment);
7713 elsif Has_Storage_Size_Clause (Def_Id) then
7714 declare
7715 DT_Align : Node_Id;
7716 DT_Size : Node_Id;
7718 begin
7719 -- For unconstrained composite types we give a size of zero
7720 -- so that the pool knows that it needs a special algorithm
7721 -- for variable size object allocation.
7723 if Is_Composite_Type (Desig_Type)
7724 and then not Is_Constrained (Desig_Type)
7725 then
7726 DT_Size := Make_Integer_Literal (Loc, 0);
7727 DT_Align := Make_Integer_Literal (Loc, Maximum_Alignment);
7729 else
7730 DT_Size :=
7731 Make_Attribute_Reference (Loc,
7732 Prefix => New_Occurrence_Of (Desig_Type, Loc),
7733 Attribute_Name => Name_Max_Size_In_Storage_Elements);
7735 DT_Align :=
7736 Make_Attribute_Reference (Loc,
7737 Prefix => New_Occurrence_Of (Desig_Type, Loc),
7738 Attribute_Name => Name_Alignment);
7739 end if;
7741 Pool_Object :=
7742 Make_Defining_Identifier (Loc,
7743 Chars => New_External_Name (Chars (Def_Id), 'P'));
7745 -- We put the code associated with the pools in the entity
7746 -- that has the later freeze node, usually the access type
7747 -- but it can also be the designated_type; because the pool
7748 -- code requires both those types to be frozen
7750 if Is_Frozen (Desig_Type)
7751 and then (No (Freeze_Node (Desig_Type))
7752 or else Analyzed (Freeze_Node (Desig_Type)))
7753 then
7754 Freeze_Action_Typ := Def_Id;
7756 -- A Taft amendment type cannot get the freeze actions
7757 -- since the full view is not there.
7759 elsif Is_Incomplete_Or_Private_Type (Desig_Type)
7760 and then No (Full_View (Desig_Type))
7761 then
7762 Freeze_Action_Typ := Def_Id;
7764 else
7765 Freeze_Action_Typ := Desig_Type;
7766 end if;
7768 Append_Freeze_Action (Freeze_Action_Typ,
7769 Make_Object_Declaration (Loc,
7770 Defining_Identifier => Pool_Object,
7771 Object_Definition =>
7772 Make_Subtype_Indication (Loc,
7773 Subtype_Mark =>
7774 New_Occurrence_Of
7775 (RTE (RE_Stack_Bounded_Pool), Loc),
7777 Constraint =>
7778 Make_Index_Or_Discriminant_Constraint (Loc,
7779 Constraints => New_List (
7781 -- First discriminant is the Pool Size
7783 New_Occurrence_Of (
7784 Storage_Size_Variable (Def_Id), Loc),
7786 -- Second discriminant is the element size
7788 DT_Size,
7790 -- Third discriminant is the alignment
7792 DT_Align)))));
7793 end;
7795 Set_Associated_Storage_Pool (Def_Id, Pool_Object);
7797 -- Case 3
7799 -- Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
7800 -- ---> Storage Pool is the specified one
7802 -- When compiling in Ada 2012 mode, ensure that the accessibility
7803 -- level of the subpool access type is not deeper than that of the
7804 -- pool_with_subpools.
7806 elsif Ada_Version >= Ada_2012
7807 and then Present (Associated_Storage_Pool (Def_Id))
7809 -- Omit this check for the case of a configurable run-time that
7810 -- does not provide package System.Storage_Pools.Subpools.
7812 and then RTE_Available (RE_Root_Storage_Pool_With_Subpools)
7813 then
7814 declare
7815 Loc : constant Source_Ptr := Sloc (Def_Id);
7816 Pool : constant Entity_Id :=
7817 Associated_Storage_Pool (Def_Id);
7818 RSPWS : constant Entity_Id :=
7819 RTE (RE_Root_Storage_Pool_With_Subpools);
7821 begin
7822 -- It is known that the accessibility level of the access
7823 -- type is deeper than that of the pool.
7825 if Type_Access_Level (Def_Id) > Object_Access_Level (Pool)
7826 and then not Accessibility_Checks_Suppressed (Def_Id)
7827 and then not Accessibility_Checks_Suppressed (Pool)
7828 then
7829 -- Static case: the pool is known to be a descendant of
7830 -- Root_Storage_Pool_With_Subpools.
7832 if Is_Ancestor (RSPWS, Etype (Pool)) then
7833 Error_Msg_N
7834 ("??subpool access type has deeper accessibility "
7835 & "level than pool", Def_Id);
7837 Append_Freeze_Action (Def_Id,
7838 Make_Raise_Program_Error (Loc,
7839 Reason => PE_Accessibility_Check_Failed));
7841 -- Dynamic case: when the pool is of a class-wide type,
7842 -- it may or may not support subpools depending on the
7843 -- path of derivation. Generate:
7845 -- if Def_Id in RSPWS'Class then
7846 -- raise Program_Error;
7847 -- end if;
7849 elsif Is_Class_Wide_Type (Etype (Pool)) then
7850 Append_Freeze_Action (Def_Id,
7851 Make_If_Statement (Loc,
7852 Condition =>
7853 Make_In (Loc,
7854 Left_Opnd => New_Occurrence_Of (Pool, Loc),
7855 Right_Opnd =>
7856 New_Occurrence_Of
7857 (Class_Wide_Type (RSPWS), Loc)),
7859 Then_Statements => New_List (
7860 Make_Raise_Program_Error (Loc,
7861 Reason => PE_Accessibility_Check_Failed))));
7862 end if;
7863 end if;
7864 end;
7865 end if;
7867 -- For access-to-controlled types (including class-wide types and
7868 -- Taft-amendment types, which potentially have controlled
7869 -- components), expand the list controller object that will store
7870 -- the dynamically allocated objects. Don't do this transformation
7871 -- for expander-generated access types, but do it for types that
7872 -- are the full view of types derived from other private types.
7873 -- Also suppress the list controller in the case of a designated
7874 -- type with convention Java, since this is used when binding to
7875 -- Java API specs, where there's no equivalent of a finalization
7876 -- list and we don't want to pull in the finalization support if
7877 -- not needed.
7879 if not Comes_From_Source (Def_Id)
7880 and then not Has_Private_Declaration (Def_Id)
7881 then
7882 null;
7884 -- An exception is made for types defined in the run-time because
7885 -- Ada.Tags.Tag itself is such a type and cannot afford this
7886 -- unnecessary overhead that would generates a loop in the
7887 -- expansion scheme. Another exception is if Restrictions
7888 -- (No_Finalization) is active, since then we know nothing is
7889 -- controlled.
7891 elsif Restriction_Active (No_Finalization)
7892 or else In_Runtime (Def_Id)
7893 then
7894 null;
7896 -- Create a finalization master for an access-to-controlled type
7897 -- or an access-to-incomplete type. It is assumed that the full
7898 -- view will be controlled.
7900 elsif Needs_Finalization (Desig_Type)
7901 or else (Is_Incomplete_Type (Desig_Type)
7902 and then No (Full_View (Desig_Type)))
7903 then
7904 Build_Finalization_Master (Def_Id);
7906 -- Create a finalization master when the designated type contains
7907 -- a private component. It is assumed that the full view will be
7908 -- controlled.
7910 elsif Has_Private_Component (Desig_Type) then
7911 Build_Finalization_Master
7912 (Typ => Def_Id,
7913 For_Private => True,
7914 Context_Scope => Scope (Def_Id),
7915 Insertion_Node => Declaration_Node (Desig_Type));
7916 end if;
7917 end;
7919 -- Freeze processing for enumeration types
7921 elsif Ekind (Def_Id) = E_Enumeration_Type then
7923 -- We only have something to do if we have a non-standard
7924 -- representation (i.e. at least one literal whose pos value
7925 -- is not the same as its representation)
7927 if Has_Non_Standard_Rep (Def_Id) then
7928 Expand_Freeze_Enumeration_Type (N);
7929 end if;
7931 -- Private types that are completed by a derivation from a private
7932 -- type have an internally generated full view, that needs to be
7933 -- frozen. This must be done explicitly because the two views share
7934 -- the freeze node, and the underlying full view is not visible when
7935 -- the freeze node is analyzed.
7937 elsif Is_Private_Type (Def_Id)
7938 and then Is_Derived_Type (Def_Id)
7939 and then Present (Full_View (Def_Id))
7940 and then Is_Itype (Full_View (Def_Id))
7941 and then Has_Private_Declaration (Full_View (Def_Id))
7942 and then Freeze_Node (Full_View (Def_Id)) = N
7943 then
7944 Set_Entity (N, Full_View (Def_Id));
7945 Result := Freeze_Type (N);
7946 Set_Entity (N, Def_Id);
7948 -- All other types require no expander action. There are such cases
7949 -- (e.g. task types and protected types). In such cases, the freeze
7950 -- nodes are there for use by Gigi.
7952 end if;
7954 -- Complete the initialization of all pending access types' finalization
7955 -- masters now that the designated type has been is frozen and primitive
7956 -- Finalize_Address generated.
7958 Process_Pending_Access_Types (Def_Id);
7959 Freeze_Stream_Operations (N, Def_Id);
7961 -- Generate the [spec and] body of the procedure tasked with the runtime
7962 -- verification of pragma Default_Initial_Condition's expression.
7964 if Has_DIC (Def_Id) then
7965 Build_DIC_Procedure_Body (Def_Id, For_Freeze => True);
7966 end if;
7968 -- Generate the [spec and] body of the invariant procedure tasked with
7969 -- the runtime verification of all invariants that pertain to the type.
7970 -- This includes invariants on the partial and full view, inherited
7971 -- class-wide invariants from parent types or interfaces, and invariants
7972 -- on array elements or record components.
7974 if Is_Interface (Def_Id) then
7976 -- Interfaces are treated as the partial view of a private type in
7977 -- order to achieve uniformity with the general case. As a result, an
7978 -- interface receives only a "partial" invariant procedure which is
7979 -- never called.
7981 if Has_Own_Invariants (Def_Id) then
7982 Build_Invariant_Procedure_Body
7983 (Typ => Def_Id,
7984 Partial_Invariant => Is_Interface (Def_Id));
7985 end if;
7987 -- Non-interface types
7989 -- Do not generate invariant procedure within other assertion
7990 -- subprograms, which may involve local declarations of local
7991 -- subtypes to which these checks do not apply.
7993 elsif Has_Invariants (Def_Id) then
7994 if Within_Internal_Subprogram
7995 or else (Ekind (Current_Scope) = E_Function
7996 and then Is_Predicate_Function (Current_Scope))
7997 then
7998 null;
7999 else
8000 Build_Invariant_Procedure_Body (Def_Id);
8001 end if;
8002 end if;
8004 Restore_Ghost_Region (Saved_GM, Saved_IGR);
8006 return Result;
8008 exception
8009 when RE_Not_Available =>
8010 Restore_Ghost_Region (Saved_GM, Saved_IGR);
8012 return False;
8013 end Freeze_Type;
8015 -------------------------
8016 -- Get_Simple_Init_Val --
8017 -------------------------
8019 function Get_Simple_Init_Val
8020 (Typ : Entity_Id;
8021 N : Node_Id;
8022 Size : Uint := No_Uint) return Node_Id
8024 IV_Attribute : constant Boolean :=
8025 Nkind (N) = N_Attribute_Reference
8026 and then Attribute_Name (N) = Name_Invalid_Value;
8028 Loc : constant Source_Ptr := Sloc (N);
8030 procedure Extract_Subtype_Bounds
8031 (Lo_Bound : out Uint;
8032 Hi_Bound : out Uint);
8033 -- Inspect subtype Typ as well its ancestor subtypes and derived types
8034 -- to determine the best known information about the bounds of the type.
8035 -- The output parameters are set as follows:
8037 -- * Lo_Bound - Set to No_Unit when there is no information available,
8038 -- or to the known low bound.
8040 -- * Hi_Bound - Set to No_Unit when there is no information available,
8041 -- or to the known high bound.
8043 function Simple_Init_Array_Type return Node_Id;
8044 -- Build an expression to initialize array type Typ
8046 function Simple_Init_Defaulted_Type return Node_Id;
8047 -- Build an expression to initialize type Typ which is subject to
8048 -- aspect Default_Value.
8050 function Simple_Init_Initialize_Scalars_Type
8051 (Size_To_Use : Uint) return Node_Id;
8052 -- Build an expression to initialize scalar type Typ which is subject to
8053 -- pragma Initialize_Scalars. Size_To_Use is the size of the object.
8055 function Simple_Init_Normalize_Scalars_Type
8056 (Size_To_Use : Uint) return Node_Id;
8057 -- Build an expression to initialize scalar type Typ which is subject to
8058 -- pragma Normalize_Scalars. Size_To_Use is the size of the object.
8060 function Simple_Init_Private_Type return Node_Id;
8061 -- Build an expression to initialize private type Typ
8063 function Simple_Init_Scalar_Type return Node_Id;
8064 -- Build an expression to initialize scalar type Typ
8066 ----------------------------
8067 -- Extract_Subtype_Bounds --
8068 ----------------------------
8070 procedure Extract_Subtype_Bounds
8071 (Lo_Bound : out Uint;
8072 Hi_Bound : out Uint)
8074 ST1 : Entity_Id;
8075 ST2 : Entity_Id;
8076 Lo : Node_Id;
8077 Hi : Node_Id;
8078 Lo_Val : Uint;
8079 Hi_Val : Uint;
8081 begin
8082 Lo_Bound := No_Uint;
8083 Hi_Bound := No_Uint;
8085 -- Loop to climb ancestor subtypes and derived types
8087 ST1 := Typ;
8088 loop
8089 if not Is_Discrete_Type (ST1) then
8090 return;
8091 end if;
8093 Lo := Type_Low_Bound (ST1);
8094 Hi := Type_High_Bound (ST1);
8096 if Compile_Time_Known_Value (Lo) then
8097 Lo_Val := Expr_Value (Lo);
8099 if Lo_Bound = No_Uint or else Lo_Bound < Lo_Val then
8100 Lo_Bound := Lo_Val;
8101 end if;
8102 end if;
8104 if Compile_Time_Known_Value (Hi) then
8105 Hi_Val := Expr_Value (Hi);
8107 if Hi_Bound = No_Uint or else Hi_Bound > Hi_Val then
8108 Hi_Bound := Hi_Val;
8109 end if;
8110 end if;
8112 ST2 := Ancestor_Subtype (ST1);
8114 if No (ST2) then
8115 ST2 := Etype (ST1);
8116 end if;
8118 exit when ST1 = ST2;
8119 ST1 := ST2;
8120 end loop;
8121 end Extract_Subtype_Bounds;
8123 ----------------------------
8124 -- Simple_Init_Array_Type --
8125 ----------------------------
8127 function Simple_Init_Array_Type return Node_Id is
8128 Comp_Typ : constant Entity_Id := Component_Type (Typ);
8130 function Simple_Init_Dimension (Index : Node_Id) return Node_Id;
8131 -- Initialize a single array dimension with index constraint Index
8133 --------------------
8134 -- Simple_Init_Dimension --
8135 --------------------
8137 function Simple_Init_Dimension (Index : Node_Id) return Node_Id is
8138 begin
8139 -- Process the current dimension
8141 if Present (Index) then
8143 -- Build a suitable "others" aggregate for the next dimension,
8144 -- or initialize the component itself. Generate:
8146 -- (others => ...)
8148 return
8149 Make_Aggregate (Loc,
8150 Component_Associations => New_List (
8151 Make_Component_Association (Loc,
8152 Choices => New_List (Make_Others_Choice (Loc)),
8153 Expression =>
8154 Simple_Init_Dimension (Next_Index (Index)))));
8156 -- Otherwise all dimensions have been processed. Initialize the
8157 -- component itself.
8159 else
8160 return
8161 Get_Simple_Init_Val
8162 (Typ => Comp_Typ,
8163 N => N,
8164 Size => Esize (Comp_Typ));
8165 end if;
8166 end Simple_Init_Dimension;
8168 -- Start of processing for Simple_Init_Array_Type
8170 begin
8171 return Simple_Init_Dimension (First_Index (Typ));
8172 end Simple_Init_Array_Type;
8174 --------------------------------
8175 -- Simple_Init_Defaulted_Type --
8176 --------------------------------
8178 function Simple_Init_Defaulted_Type return Node_Id is
8179 Subtyp : constant Entity_Id := First_Subtype (Typ);
8181 begin
8182 -- Use the Sloc of the context node when constructing the initial
8183 -- value because the expression of Default_Value may come from a
8184 -- different unit. Updating the Sloc will result in accurate error
8185 -- diagnostics.
8187 -- When the first subtype is private, retrieve the expression of the
8188 -- Default_Value from the underlying type.
8190 if Is_Private_Type (Subtyp) then
8191 return
8192 Unchecked_Convert_To
8193 (Typ => Typ,
8194 Expr =>
8195 New_Copy_Tree
8196 (Source => Default_Aspect_Value (Full_View (Subtyp)),
8197 New_Sloc => Loc));
8199 else
8200 return
8201 Convert_To
8202 (Typ => Typ,
8203 Expr =>
8204 New_Copy_Tree
8205 (Source => Default_Aspect_Value (Subtyp),
8206 New_Sloc => Loc));
8207 end if;
8208 end Simple_Init_Defaulted_Type;
8210 -----------------------------------------
8211 -- Simple_Init_Initialize_Scalars_Type --
8212 -----------------------------------------
8214 function Simple_Init_Initialize_Scalars_Type
8215 (Size_To_Use : Uint) return Node_Id
8217 Float_Typ : Entity_Id;
8218 Hi_Bound : Uint;
8219 Lo_Bound : Uint;
8220 Scal_Typ : Scalar_Id;
8222 begin
8223 Extract_Subtype_Bounds (Lo_Bound, Hi_Bound);
8225 -- Float types
8227 if Is_Floating_Point_Type (Typ) then
8228 Float_Typ := Root_Type (Typ);
8230 if Float_Typ = Standard_Short_Float then
8231 Scal_Typ := Name_Short_Float;
8232 elsif Float_Typ = Standard_Float then
8233 Scal_Typ := Name_Float;
8234 elsif Float_Typ = Standard_Long_Float then
8235 Scal_Typ := Name_Long_Float;
8236 else pragma Assert (Float_Typ = Standard_Long_Long_Float);
8237 Scal_Typ := Name_Long_Long_Float;
8238 end if;
8240 -- If zero is invalid, it is a convenient value to use that is for
8241 -- sure an appropriate invalid value in all situations.
8243 elsif Lo_Bound /= No_Uint and then Lo_Bound > Uint_0 then
8244 return Make_Integer_Literal (Loc, 0);
8246 -- Unsigned types
8248 elsif Is_Unsigned_Type (Typ) then
8249 if Size_To_Use <= 8 then
8250 Scal_Typ := Name_Unsigned_8;
8251 elsif Size_To_Use <= 16 then
8252 Scal_Typ := Name_Unsigned_16;
8253 elsif Size_To_Use <= 32 then
8254 Scal_Typ := Name_Unsigned_32;
8255 else
8256 Scal_Typ := Name_Unsigned_64;
8257 end if;
8259 -- Signed types
8261 else
8262 if Size_To_Use <= 8 then
8263 Scal_Typ := Name_Signed_8;
8264 elsif Size_To_Use <= 16 then
8265 Scal_Typ := Name_Signed_16;
8266 elsif Size_To_Use <= 32 then
8267 Scal_Typ := Name_Signed_32;
8268 else
8269 Scal_Typ := Name_Signed_64;
8270 end if;
8271 end if;
8273 -- Use the values specified by pragma Initialize_Scalars or the ones
8274 -- provided by the binder. Higher precedence is given to the pragma.
8276 return Invalid_Scalar_Value (Loc, Scal_Typ);
8277 end Simple_Init_Initialize_Scalars_Type;
8279 ----------------------------------------
8280 -- Simple_Init_Normalize_Scalars_Type --
8281 ----------------------------------------
8283 function Simple_Init_Normalize_Scalars_Type
8284 (Size_To_Use : Uint) return Node_Id
8286 Signed_Size : constant Uint := UI_Min (Uint_63, Size_To_Use - 1);
8288 Expr : Node_Id;
8289 Hi_Bound : Uint;
8290 Lo_Bound : Uint;
8292 begin
8293 Extract_Subtype_Bounds (Lo_Bound, Hi_Bound);
8295 -- If zero is invalid, it is a convenient value to use that is for
8296 -- sure an appropriate invalid value in all situations.
8298 if Lo_Bound /= No_Uint and then Lo_Bound > Uint_0 then
8299 Expr := Make_Integer_Literal (Loc, 0);
8301 -- Cases where all one bits is the appropriate invalid value
8303 -- For modular types, all 1 bits is either invalid or valid. If it
8304 -- is valid, then there is nothing that can be done since there are
8305 -- no invalid values (we ruled out zero already).
8307 -- For signed integer types that have no negative values, either
8308 -- there is room for negative values, or there is not. If there
8309 -- is, then all 1-bits may be interpreted as minus one, which is
8310 -- certainly invalid. Alternatively it is treated as the largest
8311 -- positive value, in which case the observation for modular types
8312 -- still applies.
8314 -- For float types, all 1-bits is a NaN (not a number), which is
8315 -- certainly an appropriately invalid value.
8317 elsif Is_Enumeration_Type (Typ)
8318 or else Is_Floating_Point_Type (Typ)
8319 or else Is_Unsigned_Type (Typ)
8320 then
8321 Expr := Make_Integer_Literal (Loc, 2 ** Size_To_Use - 1);
8323 -- Resolve as Unsigned_64, because the largest number we can
8324 -- generate is out of range of universal integer.
8326 Analyze_And_Resolve (Expr, RTE (RE_Unsigned_64));
8328 -- Case of signed types
8330 else
8331 -- Normally we like to use the most negative number. The one
8332 -- exception is when this number is in the known subtype range and
8333 -- the largest positive number is not in the known subtype range.
8335 -- For this exceptional case, use largest positive value
8337 if Lo_Bound /= No_Uint and then Hi_Bound /= No_Uint
8338 and then Lo_Bound <= (-(2 ** Signed_Size))
8339 and then Hi_Bound < 2 ** Signed_Size
8340 then
8341 Expr := Make_Integer_Literal (Loc, 2 ** Signed_Size - 1);
8343 -- Normal case of largest negative value
8345 else
8346 Expr := Make_Integer_Literal (Loc, -(2 ** Signed_Size));
8347 end if;
8348 end if;
8350 return Expr;
8351 end Simple_Init_Normalize_Scalars_Type;
8353 ------------------------------
8354 -- Simple_Init_Private_Type --
8355 ------------------------------
8357 function Simple_Init_Private_Type return Node_Id is
8358 Under_Typ : constant Entity_Id := Underlying_Type (Typ);
8359 Expr : Node_Id;
8361 begin
8362 -- The availability of the underlying view must be checked by routine
8363 -- Needs_Simple_Initialization.
8365 pragma Assert (Present (Under_Typ));
8367 Expr := Get_Simple_Init_Val (Under_Typ, N, Size);
8369 -- If the initial value is null or an aggregate, qualify it with the
8370 -- underlying type in order to provide a proper context.
8372 if Nkind_In (Expr, N_Aggregate, N_Null) then
8373 Expr :=
8374 Make_Qualified_Expression (Loc,
8375 Subtype_Mark => New_Occurrence_Of (Under_Typ, Loc),
8376 Expression => Expr);
8377 end if;
8379 Expr := Unchecked_Convert_To (Typ, Expr);
8381 -- Do not truncate the result when scalar types are involved and
8382 -- Initialize/Normalize_Scalars is in effect.
8384 if Nkind (Expr) = N_Unchecked_Type_Conversion
8385 and then Is_Scalar_Type (Under_Typ)
8386 then
8387 Set_No_Truncation (Expr);
8388 end if;
8390 return Expr;
8391 end Simple_Init_Private_Type;
8393 -----------------------------
8394 -- Simple_Init_Scalar_Type --
8395 -----------------------------
8397 function Simple_Init_Scalar_Type return Node_Id is
8398 Expr : Node_Id;
8399 Size_To_Use : Uint;
8401 begin
8402 pragma Assert (Init_Or_Norm_Scalars or IV_Attribute);
8404 -- Determine the size of the object. This is either the size provided
8405 -- by the caller, or the Esize of the scalar type.
8407 if Size = No_Uint or else Size <= Uint_0 then
8408 Size_To_Use := UI_Max (Uint_1, Esize (Typ));
8409 else
8410 Size_To_Use := Size;
8411 end if;
8413 -- The maximum size to use is 64 bits. This will create values of
8414 -- type Unsigned_64 and the range must fit this type.
8416 if Size_To_Use /= No_Uint and then Size_To_Use > Uint_64 then
8417 Size_To_Use := Uint_64;
8418 end if;
8420 if Normalize_Scalars and then not IV_Attribute then
8421 Expr := Simple_Init_Normalize_Scalars_Type (Size_To_Use);
8422 else
8423 Expr := Simple_Init_Initialize_Scalars_Type (Size_To_Use);
8424 end if;
8426 -- The final expression is obtained by doing an unchecked conversion
8427 -- of this result to the base type of the required subtype. Use the
8428 -- base type to prevent the unchecked conversion from chopping bits,
8429 -- and then we set Kill_Range_Check to preserve the "bad" value.
8431 Expr := Unchecked_Convert_To (Base_Type (Typ), Expr);
8433 -- Ensure that the expression is not truncated since the "bad" bits
8434 -- are desired, and also kill the range checks.
8436 if Nkind (Expr) = N_Unchecked_Type_Conversion then
8437 Set_Kill_Range_Check (Expr);
8438 Set_No_Truncation (Expr);
8439 end if;
8441 return Expr;
8442 end Simple_Init_Scalar_Type;
8444 -- Start of processing for Get_Simple_Init_Val
8446 begin
8447 if Is_Private_Type (Typ) then
8448 return Simple_Init_Private_Type;
8450 elsif Is_Scalar_Type (Typ) then
8451 if Has_Default_Aspect (Typ) then
8452 return Simple_Init_Defaulted_Type;
8453 else
8454 return Simple_Init_Scalar_Type;
8455 end if;
8457 -- Array type with Initialize or Normalize_Scalars
8459 elsif Is_Array_Type (Typ) then
8460 pragma Assert (Init_Or_Norm_Scalars);
8461 return Simple_Init_Array_Type;
8463 -- Access type is initialized to null
8465 elsif Is_Access_Type (Typ) then
8466 return Make_Null (Loc);
8468 -- No other possibilities should arise, since we should only be calling
8469 -- Get_Simple_Init_Val if Needs_Simple_Initialization returned True,
8470 -- indicating one of the above cases held.
8472 else
8473 raise Program_Error;
8474 end if;
8476 exception
8477 when RE_Not_Available =>
8478 return Empty;
8479 end Get_Simple_Init_Val;
8481 ------------------------------
8482 -- Has_New_Non_Standard_Rep --
8483 ------------------------------
8485 function Has_New_Non_Standard_Rep (T : Entity_Id) return Boolean is
8486 begin
8487 if not Is_Derived_Type (T) then
8488 return Has_Non_Standard_Rep (T)
8489 or else Has_Non_Standard_Rep (Root_Type (T));
8491 -- If Has_Non_Standard_Rep is not set on the derived type, the
8492 -- representation is fully inherited.
8494 elsif not Has_Non_Standard_Rep (T) then
8495 return False;
8497 else
8498 return First_Rep_Item (T) /= First_Rep_Item (Root_Type (T));
8500 -- May need a more precise check here: the First_Rep_Item may be a
8501 -- stream attribute, which does not affect the representation of the
8502 -- type ???
8504 end if;
8505 end Has_New_Non_Standard_Rep;
8507 ----------------------
8508 -- Inline_Init_Proc --
8509 ----------------------
8511 function Inline_Init_Proc (Typ : Entity_Id) return Boolean is
8512 begin
8513 -- The initialization proc of protected records is not worth inlining.
8514 -- In addition, when compiled for another unit for inlining purposes,
8515 -- it may make reference to entities that have not been elaborated yet.
8516 -- The initialization proc of records that need finalization contains
8517 -- a nested clean-up procedure that makes it impractical to inline as
8518 -- well, except for simple controlled types themselves. And similar
8519 -- considerations apply to task types.
8521 if Is_Concurrent_Type (Typ) then
8522 return False;
8524 elsif Needs_Finalization (Typ) and then not Is_Controlled (Typ) then
8525 return False;
8527 elsif Has_Task (Typ) then
8528 return False;
8530 else
8531 return True;
8532 end if;
8533 end Inline_Init_Proc;
8535 ----------------
8536 -- In_Runtime --
8537 ----------------
8539 function In_Runtime (E : Entity_Id) return Boolean is
8540 S1 : Entity_Id;
8542 begin
8543 S1 := Scope (E);
8544 while Scope (S1) /= Standard_Standard loop
8545 S1 := Scope (S1);
8546 end loop;
8548 return Is_RTU (S1, System) or else Is_RTU (S1, Ada);
8549 end In_Runtime;
8551 ----------------------------
8552 -- Initialization_Warning --
8553 ----------------------------
8555 procedure Initialization_Warning (E : Entity_Id) is
8556 Warning_Needed : Boolean;
8558 begin
8559 Warning_Needed := False;
8561 if Ekind (Current_Scope) = E_Package
8562 and then Static_Elaboration_Desired (Current_Scope)
8563 then
8564 if Is_Type (E) then
8565 if Is_Record_Type (E) then
8566 if Has_Discriminants (E)
8567 or else Is_Limited_Type (E)
8568 or else Has_Non_Standard_Rep (E)
8569 then
8570 Warning_Needed := True;
8572 else
8573 -- Verify that at least one component has an initialization
8574 -- expression. No need for a warning on a type if all its
8575 -- components have no initialization.
8577 declare
8578 Comp : Entity_Id;
8580 begin
8581 Comp := First_Component (E);
8582 while Present (Comp) loop
8583 if Ekind (Comp) = E_Discriminant
8584 or else
8585 (Nkind (Parent (Comp)) = N_Component_Declaration
8586 and then Present (Expression (Parent (Comp))))
8587 then
8588 Warning_Needed := True;
8589 exit;
8590 end if;
8592 Next_Component (Comp);
8593 end loop;
8594 end;
8595 end if;
8597 if Warning_Needed then
8598 Error_Msg_N
8599 ("Objects of the type cannot be initialized statically "
8600 & "by default??", Parent (E));
8601 end if;
8602 end if;
8604 else
8605 Error_Msg_N ("Object cannot be initialized statically??", E);
8606 end if;
8607 end if;
8608 end Initialization_Warning;
8610 ------------------
8611 -- Init_Formals --
8612 ------------------
8614 function Init_Formals (Typ : Entity_Id) return List_Id is
8615 Loc : constant Source_Ptr := Sloc (Typ);
8616 Formals : List_Id;
8618 begin
8619 -- First parameter is always _Init : in out typ. Note that we need this
8620 -- to be in/out because in the case of the task record value, there
8621 -- are default record fields (_Priority, _Size, -Task_Info) that may
8622 -- be referenced in the generated initialization routine.
8624 Formals := New_List (
8625 Make_Parameter_Specification (Loc,
8626 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uInit),
8627 In_Present => True,
8628 Out_Present => True,
8629 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
8631 -- For task record value, or type that contains tasks, add two more
8632 -- formals, _Master : Master_Id and _Chain : in out Activation_Chain
8633 -- We also add these parameters for the task record type case.
8635 if Has_Task (Typ)
8636 or else (Is_Record_Type (Typ) and then Is_Task_Record_Type (Typ))
8637 then
8638 Append_To (Formals,
8639 Make_Parameter_Specification (Loc,
8640 Defining_Identifier =>
8641 Make_Defining_Identifier (Loc, Name_uMaster),
8642 Parameter_Type =>
8643 New_Occurrence_Of (RTE (RE_Master_Id), Loc)));
8645 -- Add _Chain (not done for sequential elaboration policy, see
8646 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
8648 if Partition_Elaboration_Policy /= 'S' then
8649 Append_To (Formals,
8650 Make_Parameter_Specification (Loc,
8651 Defining_Identifier =>
8652 Make_Defining_Identifier (Loc, Name_uChain),
8653 In_Present => True,
8654 Out_Present => True,
8655 Parameter_Type =>
8656 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc)));
8657 end if;
8659 Append_To (Formals,
8660 Make_Parameter_Specification (Loc,
8661 Defining_Identifier =>
8662 Make_Defining_Identifier (Loc, Name_uTask_Name),
8663 In_Present => True,
8664 Parameter_Type => New_Occurrence_Of (Standard_String, Loc)));
8665 end if;
8667 -- Due to certain edge cases such as arrays with null-excluding
8668 -- components being built with the secondary stack it becomes necessary
8669 -- to add a formal to the Init_Proc which controls whether we raise
8670 -- Constraint_Errors on generated calls for internal object
8671 -- declarations.
8673 if Needs_Conditional_Null_Excluding_Check (Typ) then
8674 Append_To (Formals,
8675 Make_Parameter_Specification (Loc,
8676 Defining_Identifier =>
8677 Make_Defining_Identifier (Loc,
8678 New_External_Name (Chars
8679 (Component_Type (Typ)), "_skip_null_excluding_check")),
8680 In_Present => True,
8681 Parameter_Type =>
8682 New_Occurrence_Of (Standard_Boolean, Loc)));
8683 end if;
8685 return Formals;
8687 exception
8688 when RE_Not_Available =>
8689 return Empty_List;
8690 end Init_Formals;
8692 -------------------------
8693 -- Init_Secondary_Tags --
8694 -------------------------
8696 procedure Init_Secondary_Tags
8697 (Typ : Entity_Id;
8698 Target : Node_Id;
8699 Init_Tags_List : List_Id;
8700 Stmts_List : List_Id;
8701 Fixed_Comps : Boolean := True;
8702 Variable_Comps : Boolean := True)
8704 Loc : constant Source_Ptr := Sloc (Target);
8706 -- Inherit the C++ tag of the secondary dispatch table of Typ associated
8707 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
8709 procedure Initialize_Tag
8710 (Typ : Entity_Id;
8711 Iface : Entity_Id;
8712 Tag_Comp : Entity_Id;
8713 Iface_Tag : Node_Id);
8714 -- Initialize the tag of the secondary dispatch table of Typ associated
8715 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
8716 -- Compiling under the CPP full ABI compatibility mode, if the ancestor
8717 -- of Typ CPP tagged type we generate code to inherit the contents of
8718 -- the dispatch table directly from the ancestor.
8720 --------------------
8721 -- Initialize_Tag --
8722 --------------------
8724 procedure Initialize_Tag
8725 (Typ : Entity_Id;
8726 Iface : Entity_Id;
8727 Tag_Comp : Entity_Id;
8728 Iface_Tag : Node_Id)
8730 Comp_Typ : Entity_Id;
8731 Offset_To_Top_Comp : Entity_Id := Empty;
8733 begin
8734 -- Initialize pointer to secondary DT associated with the interface
8736 if not Is_Ancestor (Iface, Typ, Use_Full_View => True) then
8737 Append_To (Init_Tags_List,
8738 Make_Assignment_Statement (Loc,
8739 Name =>
8740 Make_Selected_Component (Loc,
8741 Prefix => New_Copy_Tree (Target),
8742 Selector_Name => New_Occurrence_Of (Tag_Comp, Loc)),
8743 Expression =>
8744 New_Occurrence_Of (Iface_Tag, Loc)));
8745 end if;
8747 Comp_Typ := Scope (Tag_Comp);
8749 -- Initialize the entries of the table of interfaces. We generate a
8750 -- different call when the parent of the type has variable size
8751 -- components.
8753 if Comp_Typ /= Etype (Comp_Typ)
8754 and then Is_Variable_Size_Record (Etype (Comp_Typ))
8755 and then Chars (Tag_Comp) /= Name_uTag
8756 then
8757 pragma Assert (Present (DT_Offset_To_Top_Func (Tag_Comp)));
8759 -- Issue error if Set_Dynamic_Offset_To_Top is not available in a
8760 -- configurable run-time environment.
8762 if not RTE_Available (RE_Set_Dynamic_Offset_To_Top) then
8763 Error_Msg_CRT
8764 ("variable size record with interface types", Typ);
8765 return;
8766 end if;
8768 -- Generate:
8769 -- Set_Dynamic_Offset_To_Top
8770 -- (This => Init,
8771 -- Prim_T => Typ'Tag,
8772 -- Interface_T => Iface'Tag,
8773 -- Offset_Value => n,
8774 -- Offset_Func => Fn'Address)
8776 Append_To (Stmts_List,
8777 Make_Procedure_Call_Statement (Loc,
8778 Name =>
8779 New_Occurrence_Of (RTE (RE_Set_Dynamic_Offset_To_Top), Loc),
8780 Parameter_Associations => New_List (
8781 Make_Attribute_Reference (Loc,
8782 Prefix => New_Copy_Tree (Target),
8783 Attribute_Name => Name_Address),
8785 Unchecked_Convert_To (RTE (RE_Tag),
8786 New_Occurrence_Of
8787 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc)),
8789 Unchecked_Convert_To (RTE (RE_Tag),
8790 New_Occurrence_Of
8791 (Node (First_Elmt (Access_Disp_Table (Iface))),
8792 Loc)),
8794 Unchecked_Convert_To
8795 (RTE (RE_Storage_Offset),
8796 Make_Op_Minus (Loc,
8797 Make_Attribute_Reference (Loc,
8798 Prefix =>
8799 Make_Selected_Component (Loc,
8800 Prefix => New_Copy_Tree (Target),
8801 Selector_Name =>
8802 New_Occurrence_Of (Tag_Comp, Loc)),
8803 Attribute_Name => Name_Position))),
8805 Unchecked_Convert_To (RTE (RE_Offset_To_Top_Function_Ptr),
8806 Make_Attribute_Reference (Loc,
8807 Prefix => New_Occurrence_Of
8808 (DT_Offset_To_Top_Func (Tag_Comp), Loc),
8809 Attribute_Name => Name_Address)))));
8811 -- In this case the next component stores the value of the offset
8812 -- to the top.
8814 Offset_To_Top_Comp := Next_Entity (Tag_Comp);
8815 pragma Assert (Present (Offset_To_Top_Comp));
8817 Append_To (Init_Tags_List,
8818 Make_Assignment_Statement (Loc,
8819 Name =>
8820 Make_Selected_Component (Loc,
8821 Prefix => New_Copy_Tree (Target),
8822 Selector_Name =>
8823 New_Occurrence_Of (Offset_To_Top_Comp, Loc)),
8825 Expression =>
8826 Make_Op_Minus (Loc,
8827 Make_Attribute_Reference (Loc,
8828 Prefix =>
8829 Make_Selected_Component (Loc,
8830 Prefix => New_Copy_Tree (Target),
8831 Selector_Name => New_Occurrence_Of (Tag_Comp, Loc)),
8832 Attribute_Name => Name_Position))));
8834 -- Normal case: No discriminants in the parent type
8836 else
8837 -- Don't need to set any value if the offset-to-top field is
8838 -- statically set or if this interface shares the primary
8839 -- dispatch table.
8841 if not Building_Static_Secondary_DT (Typ)
8842 and then not Is_Ancestor (Iface, Typ, Use_Full_View => True)
8843 then
8844 Append_To (Stmts_List,
8845 Build_Set_Static_Offset_To_Top (Loc,
8846 Iface_Tag => New_Occurrence_Of (Iface_Tag, Loc),
8847 Offset_Value =>
8848 Unchecked_Convert_To (RTE (RE_Storage_Offset),
8849 Make_Op_Minus (Loc,
8850 Make_Attribute_Reference (Loc,
8851 Prefix =>
8852 Make_Selected_Component (Loc,
8853 Prefix => New_Copy_Tree (Target),
8854 Selector_Name =>
8855 New_Occurrence_Of (Tag_Comp, Loc)),
8856 Attribute_Name => Name_Position)))));
8857 end if;
8859 -- Generate:
8860 -- Register_Interface_Offset
8861 -- (Prim_T => Typ'Tag,
8862 -- Interface_T => Iface'Tag,
8863 -- Is_Constant => True,
8864 -- Offset_Value => n,
8865 -- Offset_Func => null);
8867 if not Building_Static_Secondary_DT (Typ)
8868 and then RTE_Available (RE_Register_Interface_Offset)
8869 then
8870 Append_To (Stmts_List,
8871 Make_Procedure_Call_Statement (Loc,
8872 Name =>
8873 New_Occurrence_Of
8874 (RTE (RE_Register_Interface_Offset), Loc),
8875 Parameter_Associations => New_List (
8876 Unchecked_Convert_To (RTE (RE_Tag),
8877 New_Occurrence_Of
8878 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc)),
8880 Unchecked_Convert_To (RTE (RE_Tag),
8881 New_Occurrence_Of
8882 (Node (First_Elmt (Access_Disp_Table (Iface))), Loc)),
8884 New_Occurrence_Of (Standard_True, Loc),
8886 Unchecked_Convert_To (RTE (RE_Storage_Offset),
8887 Make_Op_Minus (Loc,
8888 Make_Attribute_Reference (Loc,
8889 Prefix =>
8890 Make_Selected_Component (Loc,
8891 Prefix => New_Copy_Tree (Target),
8892 Selector_Name =>
8893 New_Occurrence_Of (Tag_Comp, Loc)),
8894 Attribute_Name => Name_Position))),
8896 Make_Null (Loc))));
8897 end if;
8898 end if;
8899 end Initialize_Tag;
8901 -- Local variables
8903 Full_Typ : Entity_Id;
8904 Ifaces_List : Elist_Id;
8905 Ifaces_Comp_List : Elist_Id;
8906 Ifaces_Tag_List : Elist_Id;
8907 Iface_Elmt : Elmt_Id;
8908 Iface_Comp_Elmt : Elmt_Id;
8909 Iface_Tag_Elmt : Elmt_Id;
8910 Tag_Comp : Node_Id;
8911 In_Variable_Pos : Boolean;
8913 -- Start of processing for Init_Secondary_Tags
8915 begin
8916 -- Handle private types
8918 if Present (Full_View (Typ)) then
8919 Full_Typ := Full_View (Typ);
8920 else
8921 Full_Typ := Typ;
8922 end if;
8924 Collect_Interfaces_Info
8925 (Full_Typ, Ifaces_List, Ifaces_Comp_List, Ifaces_Tag_List);
8927 Iface_Elmt := First_Elmt (Ifaces_List);
8928 Iface_Comp_Elmt := First_Elmt (Ifaces_Comp_List);
8929 Iface_Tag_Elmt := First_Elmt (Ifaces_Tag_List);
8930 while Present (Iface_Elmt) loop
8931 Tag_Comp := Node (Iface_Comp_Elmt);
8933 -- Check if parent of record type has variable size components
8935 In_Variable_Pos := Scope (Tag_Comp) /= Etype (Scope (Tag_Comp))
8936 and then Is_Variable_Size_Record (Etype (Scope (Tag_Comp)));
8938 -- If we are compiling under the CPP full ABI compatibility mode and
8939 -- the ancestor is a CPP_Pragma tagged type then we generate code to
8940 -- initialize the secondary tag components from tags that reference
8941 -- secondary tables filled with copy of parent slots.
8943 if Is_CPP_Class (Root_Type (Full_Typ)) then
8945 -- Reject interface components located at variable offset in
8946 -- C++ derivations. This is currently unsupported.
8948 if not Fixed_Comps and then In_Variable_Pos then
8950 -- Locate the first dynamic component of the record. Done to
8951 -- improve the text of the warning.
8953 declare
8954 Comp : Entity_Id;
8955 Comp_Typ : Entity_Id;
8957 begin
8958 Comp := First_Entity (Typ);
8959 while Present (Comp) loop
8960 Comp_Typ := Etype (Comp);
8962 if Ekind (Comp) /= E_Discriminant
8963 and then not Is_Tag (Comp)
8964 then
8965 exit when
8966 (Is_Record_Type (Comp_Typ)
8967 and then
8968 Is_Variable_Size_Record (Base_Type (Comp_Typ)))
8969 or else
8970 (Is_Array_Type (Comp_Typ)
8971 and then Is_Variable_Size_Array (Comp_Typ));
8972 end if;
8974 Next_Entity (Comp);
8975 end loop;
8977 pragma Assert (Present (Comp));
8978 Error_Msg_Node_2 := Comp;
8979 Error_Msg_NE
8980 ("parent type & with dynamic component & cannot be parent"
8981 & " of 'C'P'P derivation if new interfaces are present",
8982 Typ, Scope (Original_Record_Component (Comp)));
8984 Error_Msg_Sloc :=
8985 Sloc (Scope (Original_Record_Component (Comp)));
8986 Error_Msg_NE
8987 ("type derived from 'C'P'P type & defined #",
8988 Typ, Scope (Original_Record_Component (Comp)));
8990 -- Avoid duplicated warnings
8992 exit;
8993 end;
8995 -- Initialize secondary tags
8997 else
8998 Initialize_Tag
8999 (Typ => Full_Typ,
9000 Iface => Node (Iface_Elmt),
9001 Tag_Comp => Tag_Comp,
9002 Iface_Tag => Node (Iface_Tag_Elmt));
9003 end if;
9005 -- Otherwise generate code to initialize the tag
9007 else
9008 if (In_Variable_Pos and then Variable_Comps)
9009 or else (not In_Variable_Pos and then Fixed_Comps)
9010 then
9011 Initialize_Tag
9012 (Typ => Full_Typ,
9013 Iface => Node (Iface_Elmt),
9014 Tag_Comp => Tag_Comp,
9015 Iface_Tag => Node (Iface_Tag_Elmt));
9016 end if;
9017 end if;
9019 Next_Elmt (Iface_Elmt);
9020 Next_Elmt (Iface_Comp_Elmt);
9021 Next_Elmt (Iface_Tag_Elmt);
9022 end loop;
9023 end Init_Secondary_Tags;
9025 ------------------------------
9026 -- Is_User_Defined_Equality --
9027 ------------------------------
9029 function Is_User_Defined_Equality (Prim : Node_Id) return Boolean is
9030 begin
9031 return Chars (Prim) = Name_Op_Eq
9032 and then Etype (First_Formal (Prim)) =
9033 Etype (Next_Formal (First_Formal (Prim)))
9034 and then Base_Type (Etype (Prim)) = Standard_Boolean;
9035 end Is_User_Defined_Equality;
9037 ----------------------------------------
9038 -- Make_Controlling_Function_Wrappers --
9039 ----------------------------------------
9041 procedure Make_Controlling_Function_Wrappers
9042 (Tag_Typ : Entity_Id;
9043 Decl_List : out List_Id;
9044 Body_List : out List_Id)
9046 Loc : constant Source_Ptr := Sloc (Tag_Typ);
9047 Prim_Elmt : Elmt_Id;
9048 Subp : Entity_Id;
9049 Actual_List : List_Id;
9050 Formal_List : List_Id;
9051 Formal : Entity_Id;
9052 Par_Formal : Entity_Id;
9053 Formal_Node : Node_Id;
9054 Func_Body : Node_Id;
9055 Func_Decl : Node_Id;
9056 Func_Spec : Node_Id;
9057 Return_Stmt : Node_Id;
9059 begin
9060 Decl_List := New_List;
9061 Body_List := New_List;
9063 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
9064 while Present (Prim_Elmt) loop
9065 Subp := Node (Prim_Elmt);
9067 -- If a primitive function with a controlling result of the type has
9068 -- not been overridden by the user, then we must create a wrapper
9069 -- function here that effectively overrides it and invokes the
9070 -- (non-abstract) parent function. This can only occur for a null
9071 -- extension. Note that functions with anonymous controlling access
9072 -- results don't qualify and must be overridden. We also exclude
9073 -- Input attributes, since each type will have its own version of
9074 -- Input constructed by the expander. The test for Comes_From_Source
9075 -- is needed to distinguish inherited operations from renamings
9076 -- (which also have Alias set). We exclude internal entities with
9077 -- Interface_Alias to avoid generating duplicated wrappers since
9078 -- the primitive which covers the interface is also available in
9079 -- the list of primitive operations.
9081 -- The function may be abstract, or require_Overriding may be set
9082 -- for it, because tests for null extensions may already have reset
9083 -- the Is_Abstract_Subprogram_Flag. If Requires_Overriding is not
9084 -- set, functions that need wrappers are recognized by having an
9085 -- alias that returns the parent type.
9087 if Comes_From_Source (Subp)
9088 or else No (Alias (Subp))
9089 or else Present (Interface_Alias (Subp))
9090 or else Ekind (Subp) /= E_Function
9091 or else not Has_Controlling_Result (Subp)
9092 or else Is_Access_Type (Etype (Subp))
9093 or else Is_Abstract_Subprogram (Alias (Subp))
9094 or else Is_TSS (Subp, TSS_Stream_Input)
9095 then
9096 goto Next_Prim;
9098 elsif Is_Abstract_Subprogram (Subp)
9099 or else Requires_Overriding (Subp)
9100 or else
9101 (Is_Null_Extension (Etype (Subp))
9102 and then Etype (Alias (Subp)) /= Etype (Subp))
9103 then
9104 Formal_List := No_List;
9105 Formal := First_Formal (Subp);
9107 if Present (Formal) then
9108 Formal_List := New_List;
9110 while Present (Formal) loop
9111 Append
9112 (Make_Parameter_Specification
9113 (Loc,
9114 Defining_Identifier =>
9115 Make_Defining_Identifier (Sloc (Formal),
9116 Chars => Chars (Formal)),
9117 In_Present => In_Present (Parent (Formal)),
9118 Out_Present => Out_Present (Parent (Formal)),
9119 Null_Exclusion_Present =>
9120 Null_Exclusion_Present (Parent (Formal)),
9121 Parameter_Type =>
9122 New_Occurrence_Of (Etype (Formal), Loc),
9123 Expression =>
9124 New_Copy_Tree (Expression (Parent (Formal)))),
9125 Formal_List);
9127 Next_Formal (Formal);
9128 end loop;
9129 end if;
9131 Func_Spec :=
9132 Make_Function_Specification (Loc,
9133 Defining_Unit_Name =>
9134 Make_Defining_Identifier (Loc,
9135 Chars => Chars (Subp)),
9136 Parameter_Specifications => Formal_List,
9137 Result_Definition =>
9138 New_Occurrence_Of (Etype (Subp), Loc));
9140 Func_Decl := Make_Subprogram_Declaration (Loc, Func_Spec);
9141 Append_To (Decl_List, Func_Decl);
9143 -- Build a wrapper body that calls the parent function. The body
9144 -- contains a single return statement that returns an extension
9145 -- aggregate whose ancestor part is a call to the parent function,
9146 -- passing the formals as actuals (with any controlling arguments
9147 -- converted to the types of the corresponding formals of the
9148 -- parent function, which might be anonymous access types), and
9149 -- having a null extension.
9151 Formal := First_Formal (Subp);
9152 Par_Formal := First_Formal (Alias (Subp));
9153 Formal_Node := First (Formal_List);
9155 if Present (Formal) then
9156 Actual_List := New_List;
9157 else
9158 Actual_List := No_List;
9159 end if;
9161 while Present (Formal) loop
9162 if Is_Controlling_Formal (Formal) then
9163 Append_To (Actual_List,
9164 Make_Type_Conversion (Loc,
9165 Subtype_Mark =>
9166 New_Occurrence_Of (Etype (Par_Formal), Loc),
9167 Expression =>
9168 New_Occurrence_Of
9169 (Defining_Identifier (Formal_Node), Loc)));
9170 else
9171 Append_To
9172 (Actual_List,
9173 New_Occurrence_Of
9174 (Defining_Identifier (Formal_Node), Loc));
9175 end if;
9177 Next_Formal (Formal);
9178 Next_Formal (Par_Formal);
9179 Next (Formal_Node);
9180 end loop;
9182 Return_Stmt :=
9183 Make_Simple_Return_Statement (Loc,
9184 Expression =>
9185 Make_Extension_Aggregate (Loc,
9186 Ancestor_Part =>
9187 Make_Function_Call (Loc,
9188 Name =>
9189 New_Occurrence_Of (Alias (Subp), Loc),
9190 Parameter_Associations => Actual_List),
9191 Null_Record_Present => True));
9193 Func_Body :=
9194 Make_Subprogram_Body (Loc,
9195 Specification => New_Copy_Tree (Func_Spec),
9196 Declarations => Empty_List,
9197 Handled_Statement_Sequence =>
9198 Make_Handled_Sequence_Of_Statements (Loc,
9199 Statements => New_List (Return_Stmt)));
9201 Set_Defining_Unit_Name
9202 (Specification (Func_Body),
9203 Make_Defining_Identifier (Loc, Chars (Subp)));
9205 Append_To (Body_List, Func_Body);
9207 -- Replace the inherited function with the wrapper function in the
9208 -- primitive operations list. We add the minimum decoration needed
9209 -- to override interface primitives.
9211 Set_Ekind (Defining_Unit_Name (Func_Spec), E_Function);
9213 Override_Dispatching_Operation
9214 (Tag_Typ, Subp, New_Op => Defining_Unit_Name (Func_Spec),
9215 Is_Wrapper => True);
9216 end if;
9218 <<Next_Prim>>
9219 Next_Elmt (Prim_Elmt);
9220 end loop;
9221 end Make_Controlling_Function_Wrappers;
9223 ------------------
9224 -- Make_Eq_Body --
9225 ------------------
9227 function Make_Eq_Body
9228 (Typ : Entity_Id;
9229 Eq_Name : Name_Id) return Node_Id
9231 Loc : constant Source_Ptr := Sloc (Parent (Typ));
9232 Decl : Node_Id;
9233 Def : constant Node_Id := Parent (Typ);
9234 Stmts : constant List_Id := New_List;
9235 Variant_Case : Boolean := Has_Discriminants (Typ);
9236 Comps : Node_Id := Empty;
9237 Typ_Def : Node_Id := Type_Definition (Def);
9239 begin
9240 Decl :=
9241 Predef_Spec_Or_Body (Loc,
9242 Tag_Typ => Typ,
9243 Name => Eq_Name,
9244 Profile => New_List (
9245 Make_Parameter_Specification (Loc,
9246 Defining_Identifier =>
9247 Make_Defining_Identifier (Loc, Name_X),
9248 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
9250 Make_Parameter_Specification (Loc,
9251 Defining_Identifier =>
9252 Make_Defining_Identifier (Loc, Name_Y),
9253 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
9255 Ret_Type => Standard_Boolean,
9256 For_Body => True);
9258 if Variant_Case then
9259 if Nkind (Typ_Def) = N_Derived_Type_Definition then
9260 Typ_Def := Record_Extension_Part (Typ_Def);
9261 end if;
9263 if Present (Typ_Def) then
9264 Comps := Component_List (Typ_Def);
9265 end if;
9267 Variant_Case :=
9268 Present (Comps) and then Present (Variant_Part (Comps));
9269 end if;
9271 if Variant_Case then
9272 Append_To (Stmts,
9273 Make_Eq_If (Typ, Discriminant_Specifications (Def)));
9274 Append_List_To (Stmts, Make_Eq_Case (Typ, Comps));
9275 Append_To (Stmts,
9276 Make_Simple_Return_Statement (Loc,
9277 Expression => New_Occurrence_Of (Standard_True, Loc)));
9279 else
9280 Append_To (Stmts,
9281 Make_Simple_Return_Statement (Loc,
9282 Expression =>
9283 Expand_Record_Equality
9284 (Typ,
9285 Typ => Typ,
9286 Lhs => Make_Identifier (Loc, Name_X),
9287 Rhs => Make_Identifier (Loc, Name_Y),
9288 Bodies => Declarations (Decl))));
9289 end if;
9291 Set_Handled_Statement_Sequence
9292 (Decl, Make_Handled_Sequence_Of_Statements (Loc, Stmts));
9293 return Decl;
9294 end Make_Eq_Body;
9296 ------------------
9297 -- Make_Eq_Case --
9298 ------------------
9300 -- <Make_Eq_If shared components>
9302 -- case X.D1 is
9303 -- when V1 => <Make_Eq_Case> on subcomponents
9304 -- ...
9305 -- when Vn => <Make_Eq_Case> on subcomponents
9306 -- end case;
9308 function Make_Eq_Case
9309 (E : Entity_Id;
9310 CL : Node_Id;
9311 Discrs : Elist_Id := New_Elmt_List) return List_Id
9313 Loc : constant Source_Ptr := Sloc (E);
9314 Result : constant List_Id := New_List;
9315 Variant : Node_Id;
9316 Alt_List : List_Id;
9318 function Corresponding_Formal (C : Node_Id) return Entity_Id;
9319 -- Given the discriminant that controls a given variant of an unchecked
9320 -- union, find the formal of the equality function that carries the
9321 -- inferred value of the discriminant.
9323 function External_Name (E : Entity_Id) return Name_Id;
9324 -- The value of a given discriminant is conveyed in the corresponding
9325 -- formal parameter of the equality routine. The name of this formal
9326 -- parameter carries a one-character suffix which is removed here.
9328 --------------------------
9329 -- Corresponding_Formal --
9330 --------------------------
9332 function Corresponding_Formal (C : Node_Id) return Entity_Id is
9333 Discr : constant Entity_Id := Entity (Name (Variant_Part (C)));
9334 Elm : Elmt_Id;
9336 begin
9337 Elm := First_Elmt (Discrs);
9338 while Present (Elm) loop
9339 if Chars (Discr) = External_Name (Node (Elm)) then
9340 return Node (Elm);
9341 end if;
9343 Next_Elmt (Elm);
9344 end loop;
9346 -- A formal of the proper name must be found
9348 raise Program_Error;
9349 end Corresponding_Formal;
9351 -------------------
9352 -- External_Name --
9353 -------------------
9355 function External_Name (E : Entity_Id) return Name_Id is
9356 begin
9357 Get_Name_String (Chars (E));
9358 Name_Len := Name_Len - 1;
9359 return Name_Find;
9360 end External_Name;
9362 -- Start of processing for Make_Eq_Case
9364 begin
9365 Append_To (Result, Make_Eq_If (E, Component_Items (CL)));
9367 if No (Variant_Part (CL)) then
9368 return Result;
9369 end if;
9371 Variant := First_Non_Pragma (Variants (Variant_Part (CL)));
9373 if No (Variant) then
9374 return Result;
9375 end if;
9377 Alt_List := New_List;
9378 while Present (Variant) loop
9379 Append_To (Alt_List,
9380 Make_Case_Statement_Alternative (Loc,
9381 Discrete_Choices => New_Copy_List (Discrete_Choices (Variant)),
9382 Statements =>
9383 Make_Eq_Case (E, Component_List (Variant), Discrs)));
9384 Next_Non_Pragma (Variant);
9385 end loop;
9387 -- If we have an Unchecked_Union, use one of the parameters of the
9388 -- enclosing equality routine that captures the discriminant, to use
9389 -- as the expression in the generated case statement.
9391 if Is_Unchecked_Union (E) then
9392 Append_To (Result,
9393 Make_Case_Statement (Loc,
9394 Expression =>
9395 New_Occurrence_Of (Corresponding_Formal (CL), Loc),
9396 Alternatives => Alt_List));
9398 else
9399 Append_To (Result,
9400 Make_Case_Statement (Loc,
9401 Expression =>
9402 Make_Selected_Component (Loc,
9403 Prefix => Make_Identifier (Loc, Name_X),
9404 Selector_Name => New_Copy (Name (Variant_Part (CL)))),
9405 Alternatives => Alt_List));
9406 end if;
9408 return Result;
9409 end Make_Eq_Case;
9411 ----------------
9412 -- Make_Eq_If --
9413 ----------------
9415 -- Generates:
9417 -- if
9418 -- X.C1 /= Y.C1
9419 -- or else
9420 -- X.C2 /= Y.C2
9421 -- ...
9422 -- then
9423 -- return False;
9424 -- end if;
9426 -- or a null statement if the list L is empty
9428 function Make_Eq_If
9429 (E : Entity_Id;
9430 L : List_Id) return Node_Id
9432 Loc : constant Source_Ptr := Sloc (E);
9433 C : Node_Id;
9434 Field_Name : Name_Id;
9435 Cond : Node_Id;
9437 begin
9438 if No (L) then
9439 return Make_Null_Statement (Loc);
9441 else
9442 Cond := Empty;
9444 C := First_Non_Pragma (L);
9445 while Present (C) loop
9446 Field_Name := Chars (Defining_Identifier (C));
9448 -- The tags must not be compared: they are not part of the value.
9449 -- Ditto for parent interfaces because their equality operator is
9450 -- abstract.
9452 -- Note also that in the following, we use Make_Identifier for
9453 -- the component names. Use of New_Occurrence_Of to identify the
9454 -- components would be incorrect because the wrong entities for
9455 -- discriminants could be picked up in the private type case.
9457 if Field_Name = Name_uParent
9458 and then Is_Interface (Etype (Defining_Identifier (C)))
9459 then
9460 null;
9462 elsif Field_Name /= Name_uTag then
9463 Evolve_Or_Else (Cond,
9464 Make_Op_Ne (Loc,
9465 Left_Opnd =>
9466 Make_Selected_Component (Loc,
9467 Prefix => Make_Identifier (Loc, Name_X),
9468 Selector_Name => Make_Identifier (Loc, Field_Name)),
9470 Right_Opnd =>
9471 Make_Selected_Component (Loc,
9472 Prefix => Make_Identifier (Loc, Name_Y),
9473 Selector_Name => Make_Identifier (Loc, Field_Name))));
9474 end if;
9476 Next_Non_Pragma (C);
9477 end loop;
9479 if No (Cond) then
9480 return Make_Null_Statement (Loc);
9482 else
9483 return
9484 Make_Implicit_If_Statement (E,
9485 Condition => Cond,
9486 Then_Statements => New_List (
9487 Make_Simple_Return_Statement (Loc,
9488 Expression => New_Occurrence_Of (Standard_False, Loc))));
9489 end if;
9490 end if;
9491 end Make_Eq_If;
9493 -------------------
9494 -- Make_Neq_Body --
9495 -------------------
9497 function Make_Neq_Body (Tag_Typ : Entity_Id) return Node_Id is
9499 function Is_Predefined_Neq_Renaming (Prim : Node_Id) return Boolean;
9500 -- Returns true if Prim is a renaming of an unresolved predefined
9501 -- inequality operation.
9503 --------------------------------
9504 -- Is_Predefined_Neq_Renaming --
9505 --------------------------------
9507 function Is_Predefined_Neq_Renaming (Prim : Node_Id) return Boolean is
9508 begin
9509 return Chars (Prim) /= Name_Op_Ne
9510 and then Present (Alias (Prim))
9511 and then Comes_From_Source (Prim)
9512 and then Is_Intrinsic_Subprogram (Alias (Prim))
9513 and then Chars (Alias (Prim)) = Name_Op_Ne;
9514 end Is_Predefined_Neq_Renaming;
9516 -- Local variables
9518 Loc : constant Source_Ptr := Sloc (Parent (Tag_Typ));
9519 Stmts : constant List_Id := New_List;
9520 Decl : Node_Id;
9521 Eq_Prim : Entity_Id;
9522 Left_Op : Entity_Id;
9523 Renaming_Prim : Entity_Id;
9524 Right_Op : Entity_Id;
9525 Target : Entity_Id;
9527 -- Start of processing for Make_Neq_Body
9529 begin
9530 -- For a call on a renaming of a dispatching subprogram that is
9531 -- overridden, if the overriding occurred before the renaming, then
9532 -- the body executed is that of the overriding declaration, even if the
9533 -- overriding declaration is not visible at the place of the renaming;
9534 -- otherwise, the inherited or predefined subprogram is called, see
9535 -- (RM 8.5.4(8))
9537 -- Stage 1: Search for a renaming of the inequality primitive and also
9538 -- search for an overriding of the equality primitive located before the
9539 -- renaming declaration.
9541 declare
9542 Elmt : Elmt_Id;
9543 Prim : Node_Id;
9545 begin
9546 Eq_Prim := Empty;
9547 Renaming_Prim := Empty;
9549 Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
9550 while Present (Elmt) loop
9551 Prim := Node (Elmt);
9553 if Is_User_Defined_Equality (Prim) and then No (Alias (Prim)) then
9554 if No (Renaming_Prim) then
9555 pragma Assert (No (Eq_Prim));
9556 Eq_Prim := Prim;
9557 end if;
9559 elsif Is_Predefined_Neq_Renaming (Prim) then
9560 Renaming_Prim := Prim;
9561 end if;
9563 Next_Elmt (Elmt);
9564 end loop;
9565 end;
9567 -- No further action needed if no renaming was found
9569 if No (Renaming_Prim) then
9570 return Empty;
9571 end if;
9573 -- Stage 2: Replace the renaming declaration by a subprogram declaration
9574 -- (required to add its body)
9576 Decl := Parent (Parent (Renaming_Prim));
9577 Rewrite (Decl,
9578 Make_Subprogram_Declaration (Loc,
9579 Specification => Specification (Decl)));
9580 Set_Analyzed (Decl);
9582 -- Remove the decoration of intrinsic renaming subprogram
9584 Set_Is_Intrinsic_Subprogram (Renaming_Prim, False);
9585 Set_Convention (Renaming_Prim, Convention_Ada);
9586 Set_Alias (Renaming_Prim, Empty);
9587 Set_Has_Completion (Renaming_Prim, False);
9589 -- Stage 3: Build the corresponding body
9591 Left_Op := First_Formal (Renaming_Prim);
9592 Right_Op := Next_Formal (Left_Op);
9594 Decl :=
9595 Predef_Spec_Or_Body (Loc,
9596 Tag_Typ => Tag_Typ,
9597 Name => Chars (Renaming_Prim),
9598 Profile => New_List (
9599 Make_Parameter_Specification (Loc,
9600 Defining_Identifier =>
9601 Make_Defining_Identifier (Loc, Chars (Left_Op)),
9602 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
9604 Make_Parameter_Specification (Loc,
9605 Defining_Identifier =>
9606 Make_Defining_Identifier (Loc, Chars (Right_Op)),
9607 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
9609 Ret_Type => Standard_Boolean,
9610 For_Body => True);
9612 -- If the overriding of the equality primitive occurred before the
9613 -- renaming, then generate:
9615 -- function <Neq_Name> (X : Y : Typ) return Boolean is
9616 -- begin
9617 -- return not Oeq (X, Y);
9618 -- end;
9620 if Present (Eq_Prim) then
9621 Target := Eq_Prim;
9623 -- Otherwise build a nested subprogram which performs the predefined
9624 -- evaluation of the equality operator. That is, generate:
9626 -- function <Neq_Name> (X : Y : Typ) return Boolean is
9627 -- function Oeq (X : Y) return Boolean is
9628 -- begin
9629 -- <<body of default implementation>>
9630 -- end;
9631 -- begin
9632 -- return not Oeq (X, Y);
9633 -- end;
9635 else
9636 declare
9637 Local_Subp : Node_Id;
9638 begin
9639 Local_Subp := Make_Eq_Body (Tag_Typ, Name_Op_Eq);
9640 Set_Declarations (Decl, New_List (Local_Subp));
9641 Target := Defining_Entity (Local_Subp);
9642 end;
9643 end if;
9645 Append_To (Stmts,
9646 Make_Simple_Return_Statement (Loc,
9647 Expression =>
9648 Make_Op_Not (Loc,
9649 Make_Function_Call (Loc,
9650 Name => New_Occurrence_Of (Target, Loc),
9651 Parameter_Associations => New_List (
9652 Make_Identifier (Loc, Chars (Left_Op)),
9653 Make_Identifier (Loc, Chars (Right_Op)))))));
9655 Set_Handled_Statement_Sequence
9656 (Decl, Make_Handled_Sequence_Of_Statements (Loc, Stmts));
9657 return Decl;
9658 end Make_Neq_Body;
9660 -------------------------------
9661 -- Make_Null_Procedure_Specs --
9662 -------------------------------
9664 function Make_Null_Procedure_Specs (Tag_Typ : Entity_Id) return List_Id is
9665 Decl_List : constant List_Id := New_List;
9666 Loc : constant Source_Ptr := Sloc (Tag_Typ);
9667 Formal : Entity_Id;
9668 Formal_List : List_Id;
9669 New_Param_Spec : Node_Id;
9670 Parent_Subp : Entity_Id;
9671 Prim_Elmt : Elmt_Id;
9672 Subp : Entity_Id;
9674 begin
9675 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
9676 while Present (Prim_Elmt) loop
9677 Subp := Node (Prim_Elmt);
9679 -- If a null procedure inherited from an interface has not been
9680 -- overridden, then we build a null procedure declaration to
9681 -- override the inherited procedure.
9683 Parent_Subp := Alias (Subp);
9685 if Present (Parent_Subp)
9686 and then Is_Null_Interface_Primitive (Parent_Subp)
9687 then
9688 Formal_List := No_List;
9689 Formal := First_Formal (Subp);
9691 if Present (Formal) then
9692 Formal_List := New_List;
9694 while Present (Formal) loop
9696 -- Copy the parameter spec including default expressions
9698 New_Param_Spec :=
9699 New_Copy_Tree (Parent (Formal), New_Sloc => Loc);
9701 -- Generate a new defining identifier for the new formal.
9702 -- required because New_Copy_Tree does not duplicate
9703 -- semantic fields (except itypes).
9705 Set_Defining_Identifier (New_Param_Spec,
9706 Make_Defining_Identifier (Sloc (Formal),
9707 Chars => Chars (Formal)));
9709 -- For controlling arguments we must change their
9710 -- parameter type to reference the tagged type (instead
9711 -- of the interface type)
9713 if Is_Controlling_Formal (Formal) then
9714 if Nkind (Parameter_Type (Parent (Formal))) = N_Identifier
9715 then
9716 Set_Parameter_Type (New_Param_Spec,
9717 New_Occurrence_Of (Tag_Typ, Loc));
9719 else pragma Assert
9720 (Nkind (Parameter_Type (Parent (Formal))) =
9721 N_Access_Definition);
9722 Set_Subtype_Mark (Parameter_Type (New_Param_Spec),
9723 New_Occurrence_Of (Tag_Typ, Loc));
9724 end if;
9725 end if;
9727 Append (New_Param_Spec, Formal_List);
9729 Next_Formal (Formal);
9730 end loop;
9731 end if;
9733 Append_To (Decl_List,
9734 Make_Subprogram_Declaration (Loc,
9735 Make_Procedure_Specification (Loc,
9736 Defining_Unit_Name =>
9737 Make_Defining_Identifier (Loc, Chars (Subp)),
9738 Parameter_Specifications => Formal_List,
9739 Null_Present => True)));
9740 end if;
9742 Next_Elmt (Prim_Elmt);
9743 end loop;
9745 return Decl_List;
9746 end Make_Null_Procedure_Specs;
9748 -------------------------------------
9749 -- Make_Predefined_Primitive_Specs --
9750 -------------------------------------
9752 procedure Make_Predefined_Primitive_Specs
9753 (Tag_Typ : Entity_Id;
9754 Predef_List : out List_Id;
9755 Renamed_Eq : out Entity_Id)
9757 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean;
9758 -- Returns true if Prim is a renaming of an unresolved predefined
9759 -- equality operation.
9761 -------------------------------
9762 -- Is_Predefined_Eq_Renaming --
9763 -------------------------------
9765 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean is
9766 begin
9767 return Chars (Prim) /= Name_Op_Eq
9768 and then Present (Alias (Prim))
9769 and then Comes_From_Source (Prim)
9770 and then Is_Intrinsic_Subprogram (Alias (Prim))
9771 and then Chars (Alias (Prim)) = Name_Op_Eq;
9772 end Is_Predefined_Eq_Renaming;
9774 -- Local variables
9776 Loc : constant Source_Ptr := Sloc (Tag_Typ);
9777 Res : constant List_Id := New_List;
9778 Eq_Name : Name_Id := Name_Op_Eq;
9779 Eq_Needed : Boolean;
9780 Eq_Spec : Node_Id;
9781 Prim : Elmt_Id;
9783 Has_Predef_Eq_Renaming : Boolean := False;
9784 -- Set to True if Tag_Typ has a primitive that renames the predefined
9785 -- equality operator. Used to implement (RM 8-5-4(8)).
9787 -- Start of processing for Make_Predefined_Primitive_Specs
9789 begin
9790 Renamed_Eq := Empty;
9792 -- Spec of _Size
9794 Append_To (Res, Predef_Spec_Or_Body (Loc,
9795 Tag_Typ => Tag_Typ,
9796 Name => Name_uSize,
9797 Profile => New_List (
9798 Make_Parameter_Specification (Loc,
9799 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
9800 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
9802 Ret_Type => Standard_Long_Long_Integer));
9804 -- Specs for dispatching stream attributes
9806 declare
9807 Stream_Op_TSS_Names :
9808 constant array (Positive range <>) of TSS_Name_Type :=
9809 (TSS_Stream_Read,
9810 TSS_Stream_Write,
9811 TSS_Stream_Input,
9812 TSS_Stream_Output);
9814 begin
9815 for Op in Stream_Op_TSS_Names'Range loop
9816 if Stream_Operation_OK (Tag_Typ, Stream_Op_TSS_Names (Op)) then
9817 Append_To (Res,
9818 Predef_Stream_Attr_Spec (Loc, Tag_Typ,
9819 Stream_Op_TSS_Names (Op)));
9820 end if;
9821 end loop;
9822 end;
9824 -- Spec of "=" is expanded if the type is not limited and if a user
9825 -- defined "=" was not already declared for the non-full view of a
9826 -- private extension
9828 if not Is_Limited_Type (Tag_Typ) then
9829 Eq_Needed := True;
9830 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
9831 while Present (Prim) loop
9833 -- If a primitive is encountered that renames the predefined
9834 -- equality operator before reaching any explicit equality
9835 -- primitive, then we still need to create a predefined equality
9836 -- function, because calls to it can occur via the renaming. A
9837 -- new name is created for the equality to avoid conflicting with
9838 -- any user-defined equality. (Note that this doesn't account for
9839 -- renamings of equality nested within subpackages???)
9841 if Is_Predefined_Eq_Renaming (Node (Prim)) then
9842 Has_Predef_Eq_Renaming := True;
9843 Eq_Name := New_External_Name (Chars (Node (Prim)), 'E');
9845 -- User-defined equality
9847 elsif Is_User_Defined_Equality (Node (Prim)) then
9848 if No (Alias (Node (Prim)))
9849 or else Nkind (Unit_Declaration_Node (Node (Prim))) =
9850 N_Subprogram_Renaming_Declaration
9851 then
9852 Eq_Needed := False;
9853 exit;
9855 -- If the parent is not an interface type and has an abstract
9856 -- equality function explicitly defined in the sources, then
9857 -- the inherited equality is abstract as well, and no body can
9858 -- be created for it.
9860 elsif not Is_Interface (Etype (Tag_Typ))
9861 and then Present (Alias (Node (Prim)))
9862 and then Comes_From_Source (Alias (Node (Prim)))
9863 and then Is_Abstract_Subprogram (Alias (Node (Prim)))
9864 then
9865 Eq_Needed := False;
9866 exit;
9868 -- If the type has an equality function corresponding with
9869 -- a primitive defined in an interface type, the inherited
9870 -- equality is abstract as well, and no body can be created
9871 -- for it.
9873 elsif Present (Alias (Node (Prim)))
9874 and then Comes_From_Source (Ultimate_Alias (Node (Prim)))
9875 and then
9876 Is_Interface
9877 (Find_Dispatching_Type (Ultimate_Alias (Node (Prim))))
9878 then
9879 Eq_Needed := False;
9880 exit;
9881 end if;
9882 end if;
9884 Next_Elmt (Prim);
9885 end loop;
9887 -- If a renaming of predefined equality was found but there was no
9888 -- user-defined equality (so Eq_Needed is still true), then set the
9889 -- name back to Name_Op_Eq. But in the case where a user-defined
9890 -- equality was located after such a renaming, then the predefined
9891 -- equality function is still needed, so Eq_Needed must be set back
9892 -- to True.
9894 if Eq_Name /= Name_Op_Eq then
9895 if Eq_Needed then
9896 Eq_Name := Name_Op_Eq;
9897 else
9898 Eq_Needed := True;
9899 end if;
9900 end if;
9902 if Eq_Needed then
9903 Eq_Spec := Predef_Spec_Or_Body (Loc,
9904 Tag_Typ => Tag_Typ,
9905 Name => Eq_Name,
9906 Profile => New_List (
9907 Make_Parameter_Specification (Loc,
9908 Defining_Identifier =>
9909 Make_Defining_Identifier (Loc, Name_X),
9910 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
9912 Make_Parameter_Specification (Loc,
9913 Defining_Identifier =>
9914 Make_Defining_Identifier (Loc, Name_Y),
9915 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
9916 Ret_Type => Standard_Boolean);
9917 Append_To (Res, Eq_Spec);
9919 if Has_Predef_Eq_Renaming then
9920 Renamed_Eq := Defining_Unit_Name (Specification (Eq_Spec));
9922 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
9923 while Present (Prim) loop
9925 -- Any renamings of equality that appeared before an
9926 -- overriding equality must be updated to refer to the
9927 -- entity for the predefined equality, otherwise calls via
9928 -- the renaming would get incorrectly resolved to call the
9929 -- user-defined equality function.
9931 if Is_Predefined_Eq_Renaming (Node (Prim)) then
9932 Set_Alias (Node (Prim), Renamed_Eq);
9934 -- Exit upon encountering a user-defined equality
9936 elsif Chars (Node (Prim)) = Name_Op_Eq
9937 and then No (Alias (Node (Prim)))
9938 then
9939 exit;
9940 end if;
9942 Next_Elmt (Prim);
9943 end loop;
9944 end if;
9945 end if;
9947 -- Spec for dispatching assignment
9949 Append_To (Res, Predef_Spec_Or_Body (Loc,
9950 Tag_Typ => Tag_Typ,
9951 Name => Name_uAssign,
9952 Profile => New_List (
9953 Make_Parameter_Specification (Loc,
9954 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
9955 Out_Present => True,
9956 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
9958 Make_Parameter_Specification (Loc,
9959 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
9960 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)))));
9961 end if;
9963 -- Ada 2005: Generate declarations for the following primitive
9964 -- operations for limited interfaces and synchronized types that
9965 -- implement a limited interface.
9967 -- Disp_Asynchronous_Select
9968 -- Disp_Conditional_Select
9969 -- Disp_Get_Prim_Op_Kind
9970 -- Disp_Get_Task_Id
9971 -- Disp_Requeue
9972 -- Disp_Timed_Select
9974 -- Disable the generation of these bodies if No_Dispatching_Calls,
9975 -- Ravenscar or ZFP is active.
9977 if Ada_Version >= Ada_2005
9978 and then not Restriction_Active (No_Dispatching_Calls)
9979 and then not Restriction_Active (No_Select_Statements)
9980 and then RTE_Available (RE_Select_Specific_Data)
9981 then
9982 -- These primitives are defined abstract in interface types
9984 if Is_Interface (Tag_Typ)
9985 and then Is_Limited_Record (Tag_Typ)
9986 then
9987 Append_To (Res,
9988 Make_Abstract_Subprogram_Declaration (Loc,
9989 Specification =>
9990 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
9992 Append_To (Res,
9993 Make_Abstract_Subprogram_Declaration (Loc,
9994 Specification =>
9995 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
9997 Append_To (Res,
9998 Make_Abstract_Subprogram_Declaration (Loc,
9999 Specification =>
10000 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
10002 Append_To (Res,
10003 Make_Abstract_Subprogram_Declaration (Loc,
10004 Specification =>
10005 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
10007 Append_To (Res,
10008 Make_Abstract_Subprogram_Declaration (Loc,
10009 Specification =>
10010 Make_Disp_Requeue_Spec (Tag_Typ)));
10012 Append_To (Res,
10013 Make_Abstract_Subprogram_Declaration (Loc,
10014 Specification =>
10015 Make_Disp_Timed_Select_Spec (Tag_Typ)));
10017 -- If ancestor is an interface type, declare non-abstract primitives
10018 -- to override the abstract primitives of the interface type.
10020 -- In VM targets we define these primitives in all root tagged types
10021 -- that are not interface types. Done because in VM targets we don't
10022 -- have secondary dispatch tables and any derivation of Tag_Typ may
10023 -- cover limited interfaces (which always have these primitives since
10024 -- they may be ancestors of synchronized interface types).
10026 elsif (not Is_Interface (Tag_Typ)
10027 and then Is_Interface (Etype (Tag_Typ))
10028 and then Is_Limited_Record (Etype (Tag_Typ)))
10029 or else
10030 (Is_Concurrent_Record_Type (Tag_Typ)
10031 and then Has_Interfaces (Tag_Typ))
10032 or else
10033 (not Tagged_Type_Expansion
10034 and then not Is_Interface (Tag_Typ)
10035 and then Tag_Typ = Root_Type (Tag_Typ))
10036 then
10037 Append_To (Res,
10038 Make_Subprogram_Declaration (Loc,
10039 Specification =>
10040 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
10042 Append_To (Res,
10043 Make_Subprogram_Declaration (Loc,
10044 Specification =>
10045 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
10047 Append_To (Res,
10048 Make_Subprogram_Declaration (Loc,
10049 Specification =>
10050 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
10052 Append_To (Res,
10053 Make_Subprogram_Declaration (Loc,
10054 Specification =>
10055 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
10057 Append_To (Res,
10058 Make_Subprogram_Declaration (Loc,
10059 Specification =>
10060 Make_Disp_Requeue_Spec (Tag_Typ)));
10062 Append_To (Res,
10063 Make_Subprogram_Declaration (Loc,
10064 Specification =>
10065 Make_Disp_Timed_Select_Spec (Tag_Typ)));
10066 end if;
10067 end if;
10069 -- All tagged types receive their own Deep_Adjust and Deep_Finalize
10070 -- regardless of whether they are controlled or may contain controlled
10071 -- components.
10073 -- Do not generate the routines if finalization is disabled
10075 if Restriction_Active (No_Finalization) then
10076 null;
10078 else
10079 if not Is_Limited_Type (Tag_Typ) then
10080 Append_To (Res, Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust));
10081 end if;
10083 Append_To (Res, Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize));
10084 end if;
10086 Predef_List := Res;
10087 end Make_Predefined_Primitive_Specs;
10089 -------------------------
10090 -- Make_Tag_Assignment --
10091 -------------------------
10093 function Make_Tag_Assignment (N : Node_Id) return Node_Id is
10094 Loc : constant Source_Ptr := Sloc (N);
10095 Def_If : constant Entity_Id := Defining_Identifier (N);
10096 Expr : constant Node_Id := Expression (N);
10097 Typ : constant Entity_Id := Etype (Def_If);
10098 Full_Typ : constant Entity_Id := Underlying_Type (Typ);
10099 New_Ref : Node_Id;
10101 begin
10102 -- This expansion activity is called during analysis, but cannot
10103 -- be applied in ASIS mode when other expansion is disabled.
10105 if Is_Tagged_Type (Typ)
10106 and then not Is_Class_Wide_Type (Typ)
10107 and then not Is_CPP_Class (Typ)
10108 and then Tagged_Type_Expansion
10109 and then Nkind (Expr) /= N_Aggregate
10110 and then not ASIS_Mode
10111 and then (Nkind (Expr) /= N_Qualified_Expression
10112 or else Nkind (Expression (Expr)) /= N_Aggregate)
10113 then
10114 New_Ref :=
10115 Make_Selected_Component (Loc,
10116 Prefix => New_Occurrence_Of (Def_If, Loc),
10117 Selector_Name =>
10118 New_Occurrence_Of (First_Tag_Component (Full_Typ), Loc));
10119 Set_Assignment_OK (New_Ref);
10121 return
10122 Make_Assignment_Statement (Loc,
10123 Name => New_Ref,
10124 Expression =>
10125 Unchecked_Convert_To (RTE (RE_Tag),
10126 New_Occurrence_Of (Node
10127 (First_Elmt (Access_Disp_Table (Full_Typ))), Loc)));
10128 else
10129 return Empty;
10130 end if;
10131 end Make_Tag_Assignment;
10133 ----------------------
10134 -- Predef_Deep_Spec --
10135 ----------------------
10137 function Predef_Deep_Spec
10138 (Loc : Source_Ptr;
10139 Tag_Typ : Entity_Id;
10140 Name : TSS_Name_Type;
10141 For_Body : Boolean := False) return Node_Id
10143 Formals : List_Id;
10145 begin
10146 -- V : in out Tag_Typ
10148 Formals := New_List (
10149 Make_Parameter_Specification (Loc,
10150 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
10151 In_Present => True,
10152 Out_Present => True,
10153 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)));
10155 -- F : Boolean := True
10157 if Name = TSS_Deep_Adjust
10158 or else Name = TSS_Deep_Finalize
10159 then
10160 Append_To (Formals,
10161 Make_Parameter_Specification (Loc,
10162 Defining_Identifier => Make_Defining_Identifier (Loc, Name_F),
10163 Parameter_Type => New_Occurrence_Of (Standard_Boolean, Loc),
10164 Expression => New_Occurrence_Of (Standard_True, Loc)));
10165 end if;
10167 return
10168 Predef_Spec_Or_Body (Loc,
10169 Name => Make_TSS_Name (Tag_Typ, Name),
10170 Tag_Typ => Tag_Typ,
10171 Profile => Formals,
10172 For_Body => For_Body);
10174 exception
10175 when RE_Not_Available =>
10176 return Empty;
10177 end Predef_Deep_Spec;
10179 -------------------------
10180 -- Predef_Spec_Or_Body --
10181 -------------------------
10183 function Predef_Spec_Or_Body
10184 (Loc : Source_Ptr;
10185 Tag_Typ : Entity_Id;
10186 Name : Name_Id;
10187 Profile : List_Id;
10188 Ret_Type : Entity_Id := Empty;
10189 For_Body : Boolean := False) return Node_Id
10191 Id : constant Entity_Id := Make_Defining_Identifier (Loc, Name);
10192 Spec : Node_Id;
10194 begin
10195 Set_Is_Public (Id, Is_Public (Tag_Typ));
10197 -- The internal flag is set to mark these declarations because they have
10198 -- specific properties. First, they are primitives even if they are not
10199 -- defined in the type scope (the freezing point is not necessarily in
10200 -- the same scope). Second, the predefined equality can be overridden by
10201 -- a user-defined equality, no body will be generated in this case.
10203 Set_Is_Internal (Id);
10205 if not Debug_Generated_Code then
10206 Set_Debug_Info_Off (Id);
10207 end if;
10209 if No (Ret_Type) then
10210 Spec :=
10211 Make_Procedure_Specification (Loc,
10212 Defining_Unit_Name => Id,
10213 Parameter_Specifications => Profile);
10214 else
10215 Spec :=
10216 Make_Function_Specification (Loc,
10217 Defining_Unit_Name => Id,
10218 Parameter_Specifications => Profile,
10219 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
10220 end if;
10222 if Is_Interface (Tag_Typ) then
10223 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
10225 -- If body case, return empty subprogram body. Note that this is ill-
10226 -- formed, because there is not even a null statement, and certainly not
10227 -- a return in the function case. The caller is expected to do surgery
10228 -- on the body to add the appropriate stuff.
10230 elsif For_Body then
10231 return Make_Subprogram_Body (Loc, Spec, Empty_List, Empty);
10233 -- For the case of an Input attribute predefined for an abstract type,
10234 -- generate an abstract specification. This will never be called, but we
10235 -- need the slot allocated in the dispatching table so that attributes
10236 -- typ'Class'Input and typ'Class'Output will work properly.
10238 elsif Is_TSS (Name, TSS_Stream_Input)
10239 and then Is_Abstract_Type (Tag_Typ)
10240 then
10241 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
10243 -- Normal spec case, where we return a subprogram declaration
10245 else
10246 return Make_Subprogram_Declaration (Loc, Spec);
10247 end if;
10248 end Predef_Spec_Or_Body;
10250 -----------------------------
10251 -- Predef_Stream_Attr_Spec --
10252 -----------------------------
10254 function Predef_Stream_Attr_Spec
10255 (Loc : Source_Ptr;
10256 Tag_Typ : Entity_Id;
10257 Name : TSS_Name_Type;
10258 For_Body : Boolean := False) return Node_Id
10260 Ret_Type : Entity_Id;
10262 begin
10263 if Name = TSS_Stream_Input then
10264 Ret_Type := Tag_Typ;
10265 else
10266 Ret_Type := Empty;
10267 end if;
10269 return
10270 Predef_Spec_Or_Body
10271 (Loc,
10272 Name => Make_TSS_Name (Tag_Typ, Name),
10273 Tag_Typ => Tag_Typ,
10274 Profile => Build_Stream_Attr_Profile (Loc, Tag_Typ, Name),
10275 Ret_Type => Ret_Type,
10276 For_Body => For_Body);
10277 end Predef_Stream_Attr_Spec;
10279 ---------------------------------
10280 -- Predefined_Primitive_Bodies --
10281 ---------------------------------
10283 function Predefined_Primitive_Bodies
10284 (Tag_Typ : Entity_Id;
10285 Renamed_Eq : Entity_Id) return List_Id
10287 Loc : constant Source_Ptr := Sloc (Tag_Typ);
10288 Res : constant List_Id := New_List;
10289 Adj_Call : Node_Id;
10290 Decl : Node_Id;
10291 Fin_Call : Node_Id;
10292 Prim : Elmt_Id;
10293 Eq_Needed : Boolean;
10294 Eq_Name : Name_Id;
10295 Ent : Entity_Id;
10297 pragma Warnings (Off, Ent);
10299 begin
10300 pragma Assert (not Is_Interface (Tag_Typ));
10302 -- See if we have a predefined "=" operator
10304 if Present (Renamed_Eq) then
10305 Eq_Needed := True;
10306 Eq_Name := Chars (Renamed_Eq);
10308 -- If the parent is an interface type then it has defined all the
10309 -- predefined primitives abstract and we need to check if the type
10310 -- has some user defined "=" function which matches the profile of
10311 -- the Ada predefined equality operator to avoid generating it.
10313 elsif Is_Interface (Etype (Tag_Typ)) then
10314 Eq_Needed := True;
10315 Eq_Name := Name_Op_Eq;
10317 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
10318 while Present (Prim) loop
10319 if Chars (Node (Prim)) = Name_Op_Eq
10320 and then not Is_Internal (Node (Prim))
10321 and then Present (First_Entity (Node (Prim)))
10323 -- The predefined equality primitive must have exactly two
10324 -- formals whose type is this tagged type
10326 and then Present (Last_Entity (Node (Prim)))
10327 and then Next_Entity (First_Entity (Node (Prim)))
10328 = Last_Entity (Node (Prim))
10329 and then Etype (First_Entity (Node (Prim))) = Tag_Typ
10330 and then Etype (Last_Entity (Node (Prim))) = Tag_Typ
10331 then
10332 Eq_Needed := False;
10333 Eq_Name := No_Name;
10334 exit;
10335 end if;
10337 Next_Elmt (Prim);
10338 end loop;
10340 else
10341 Eq_Needed := False;
10342 Eq_Name := No_Name;
10344 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
10345 while Present (Prim) loop
10346 if Chars (Node (Prim)) = Name_Op_Eq
10347 and then Is_Internal (Node (Prim))
10348 then
10349 Eq_Needed := True;
10350 Eq_Name := Name_Op_Eq;
10351 exit;
10352 end if;
10354 Next_Elmt (Prim);
10355 end loop;
10356 end if;
10358 -- Body of _Size
10360 Decl := Predef_Spec_Or_Body (Loc,
10361 Tag_Typ => Tag_Typ,
10362 Name => Name_uSize,
10363 Profile => New_List (
10364 Make_Parameter_Specification (Loc,
10365 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
10366 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
10368 Ret_Type => Standard_Long_Long_Integer,
10369 For_Body => True);
10371 Set_Handled_Statement_Sequence (Decl,
10372 Make_Handled_Sequence_Of_Statements (Loc, New_List (
10373 Make_Simple_Return_Statement (Loc,
10374 Expression =>
10375 Make_Attribute_Reference (Loc,
10376 Prefix => Make_Identifier (Loc, Name_X),
10377 Attribute_Name => Name_Size)))));
10379 Append_To (Res, Decl);
10381 -- Bodies for Dispatching stream IO routines. We need these only for
10382 -- non-limited types (in the limited case there is no dispatching).
10383 -- We also skip them if dispatching or finalization are not available
10384 -- or if stream operations are prohibited by restriction No_Streams or
10385 -- from use of pragma/aspect No_Tagged_Streams.
10387 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Read)
10388 and then No (TSS (Tag_Typ, TSS_Stream_Read))
10389 then
10390 Build_Record_Read_Procedure (Loc, Tag_Typ, Decl, Ent);
10391 Append_To (Res, Decl);
10392 end if;
10394 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Write)
10395 and then No (TSS (Tag_Typ, TSS_Stream_Write))
10396 then
10397 Build_Record_Write_Procedure (Loc, Tag_Typ, Decl, Ent);
10398 Append_To (Res, Decl);
10399 end if;
10401 -- Skip body of _Input for the abstract case, since the corresponding
10402 -- spec is abstract (see Predef_Spec_Or_Body).
10404 if not Is_Abstract_Type (Tag_Typ)
10405 and then Stream_Operation_OK (Tag_Typ, TSS_Stream_Input)
10406 and then No (TSS (Tag_Typ, TSS_Stream_Input))
10407 then
10408 Build_Record_Or_Elementary_Input_Function
10409 (Loc, Tag_Typ, Decl, Ent);
10410 Append_To (Res, Decl);
10411 end if;
10413 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Output)
10414 and then No (TSS (Tag_Typ, TSS_Stream_Output))
10415 then
10416 Build_Record_Or_Elementary_Output_Procedure (Loc, Tag_Typ, Decl, Ent);
10417 Append_To (Res, Decl);
10418 end if;
10420 -- Ada 2005: Generate bodies for the following primitive operations for
10421 -- limited interfaces and synchronized types that implement a limited
10422 -- interface.
10424 -- disp_asynchronous_select
10425 -- disp_conditional_select
10426 -- disp_get_prim_op_kind
10427 -- disp_get_task_id
10428 -- disp_timed_select
10430 -- The interface versions will have null bodies
10432 -- Disable the generation of these bodies if No_Dispatching_Calls,
10433 -- Ravenscar or ZFP is active.
10435 -- In VM targets we define these primitives in all root tagged types
10436 -- that are not interface types. Done because in VM targets we don't
10437 -- have secondary dispatch tables and any derivation of Tag_Typ may
10438 -- cover limited interfaces (which always have these primitives since
10439 -- they may be ancestors of synchronized interface types).
10441 if Ada_Version >= Ada_2005
10442 and then not Is_Interface (Tag_Typ)
10443 and then
10444 ((Is_Interface (Etype (Tag_Typ))
10445 and then Is_Limited_Record (Etype (Tag_Typ)))
10446 or else
10447 (Is_Concurrent_Record_Type (Tag_Typ)
10448 and then Has_Interfaces (Tag_Typ))
10449 or else
10450 (not Tagged_Type_Expansion
10451 and then Tag_Typ = Root_Type (Tag_Typ)))
10452 and then not Restriction_Active (No_Dispatching_Calls)
10453 and then not Restriction_Active (No_Select_Statements)
10454 and then RTE_Available (RE_Select_Specific_Data)
10455 then
10456 Append_To (Res, Make_Disp_Asynchronous_Select_Body (Tag_Typ));
10457 Append_To (Res, Make_Disp_Conditional_Select_Body (Tag_Typ));
10458 Append_To (Res, Make_Disp_Get_Prim_Op_Kind_Body (Tag_Typ));
10459 Append_To (Res, Make_Disp_Get_Task_Id_Body (Tag_Typ));
10460 Append_To (Res, Make_Disp_Requeue_Body (Tag_Typ));
10461 Append_To (Res, Make_Disp_Timed_Select_Body (Tag_Typ));
10462 end if;
10464 if not Is_Limited_Type (Tag_Typ) and then not Is_Interface (Tag_Typ) then
10466 -- Body for equality
10468 if Eq_Needed then
10469 Decl := Make_Eq_Body (Tag_Typ, Eq_Name);
10470 Append_To (Res, Decl);
10471 end if;
10473 -- Body for inequality (if required)
10475 Decl := Make_Neq_Body (Tag_Typ);
10477 if Present (Decl) then
10478 Append_To (Res, Decl);
10479 end if;
10481 -- Body for dispatching assignment
10483 Decl :=
10484 Predef_Spec_Or_Body (Loc,
10485 Tag_Typ => Tag_Typ,
10486 Name => Name_uAssign,
10487 Profile => New_List (
10488 Make_Parameter_Specification (Loc,
10489 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
10490 Out_Present => True,
10491 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
10493 Make_Parameter_Specification (Loc,
10494 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
10495 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
10496 For_Body => True);
10498 Set_Handled_Statement_Sequence (Decl,
10499 Make_Handled_Sequence_Of_Statements (Loc, New_List (
10500 Make_Assignment_Statement (Loc,
10501 Name => Make_Identifier (Loc, Name_X),
10502 Expression => Make_Identifier (Loc, Name_Y)))));
10504 Append_To (Res, Decl);
10505 end if;
10507 -- Generate empty bodies of routines Deep_Adjust and Deep_Finalize for
10508 -- tagged types which do not contain controlled components.
10510 -- Do not generate the routines if finalization is disabled
10512 if Restriction_Active (No_Finalization) then
10513 null;
10515 elsif not Has_Controlled_Component (Tag_Typ) then
10516 if not Is_Limited_Type (Tag_Typ) then
10517 Adj_Call := Empty;
10518 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust, True);
10520 if Is_Controlled (Tag_Typ) then
10521 Adj_Call :=
10522 Make_Adjust_Call (
10523 Obj_Ref => Make_Identifier (Loc, Name_V),
10524 Typ => Tag_Typ);
10525 end if;
10527 if No (Adj_Call) then
10528 Adj_Call := Make_Null_Statement (Loc);
10529 end if;
10531 Set_Handled_Statement_Sequence (Decl,
10532 Make_Handled_Sequence_Of_Statements (Loc,
10533 Statements => New_List (Adj_Call)));
10535 Append_To (Res, Decl);
10536 end if;
10538 Fin_Call := Empty;
10539 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize, True);
10541 if Is_Controlled (Tag_Typ) then
10542 Fin_Call :=
10543 Make_Final_Call
10544 (Obj_Ref => Make_Identifier (Loc, Name_V),
10545 Typ => Tag_Typ);
10546 end if;
10548 if No (Fin_Call) then
10549 Fin_Call := Make_Null_Statement (Loc);
10550 end if;
10552 Set_Handled_Statement_Sequence (Decl,
10553 Make_Handled_Sequence_Of_Statements (Loc,
10554 Statements => New_List (Fin_Call)));
10556 Append_To (Res, Decl);
10557 end if;
10559 return Res;
10560 end Predefined_Primitive_Bodies;
10562 ---------------------------------
10563 -- Predefined_Primitive_Freeze --
10564 ---------------------------------
10566 function Predefined_Primitive_Freeze
10567 (Tag_Typ : Entity_Id) return List_Id
10569 Res : constant List_Id := New_List;
10570 Prim : Elmt_Id;
10571 Frnodes : List_Id;
10573 begin
10574 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
10575 while Present (Prim) loop
10576 if Is_Predefined_Dispatching_Operation (Node (Prim)) then
10577 Frnodes := Freeze_Entity (Node (Prim), Tag_Typ);
10579 if Present (Frnodes) then
10580 Append_List_To (Res, Frnodes);
10581 end if;
10582 end if;
10584 Next_Elmt (Prim);
10585 end loop;
10587 return Res;
10588 end Predefined_Primitive_Freeze;
10590 -------------------------
10591 -- Stream_Operation_OK --
10592 -------------------------
10594 function Stream_Operation_OK
10595 (Typ : Entity_Id;
10596 Operation : TSS_Name_Type) return Boolean
10598 Has_Predefined_Or_Specified_Stream_Attribute : Boolean := False;
10600 begin
10601 -- Special case of a limited type extension: a default implementation
10602 -- of the stream attributes Read or Write exists if that attribute
10603 -- has been specified or is available for an ancestor type; a default
10604 -- implementation of the attribute Output (resp. Input) exists if the
10605 -- attribute has been specified or Write (resp. Read) is available for
10606 -- an ancestor type. The last condition only applies under Ada 2005.
10608 if Is_Limited_Type (Typ) and then Is_Tagged_Type (Typ) then
10609 if Operation = TSS_Stream_Read then
10610 Has_Predefined_Or_Specified_Stream_Attribute :=
10611 Has_Specified_Stream_Read (Typ);
10613 elsif Operation = TSS_Stream_Write then
10614 Has_Predefined_Or_Specified_Stream_Attribute :=
10615 Has_Specified_Stream_Write (Typ);
10617 elsif Operation = TSS_Stream_Input then
10618 Has_Predefined_Or_Specified_Stream_Attribute :=
10619 Has_Specified_Stream_Input (Typ)
10620 or else
10621 (Ada_Version >= Ada_2005
10622 and then Stream_Operation_OK (Typ, TSS_Stream_Read));
10624 elsif Operation = TSS_Stream_Output then
10625 Has_Predefined_Or_Specified_Stream_Attribute :=
10626 Has_Specified_Stream_Output (Typ)
10627 or else
10628 (Ada_Version >= Ada_2005
10629 and then Stream_Operation_OK (Typ, TSS_Stream_Write));
10630 end if;
10632 -- Case of inherited TSS_Stream_Read or TSS_Stream_Write
10634 if not Has_Predefined_Or_Specified_Stream_Attribute
10635 and then Is_Derived_Type (Typ)
10636 and then (Operation = TSS_Stream_Read
10637 or else Operation = TSS_Stream_Write)
10638 then
10639 Has_Predefined_Or_Specified_Stream_Attribute :=
10640 Present
10641 (Find_Inherited_TSS (Base_Type (Etype (Typ)), Operation));
10642 end if;
10643 end if;
10645 -- If the type is not limited, or else is limited but the attribute is
10646 -- explicitly specified or is predefined for the type, then return True,
10647 -- unless other conditions prevail, such as restrictions prohibiting
10648 -- streams or dispatching operations. We also return True for limited
10649 -- interfaces, because they may be extended by nonlimited types and
10650 -- permit inheritance in this case (addresses cases where an abstract
10651 -- extension doesn't get 'Input declared, as per comments below, but
10652 -- 'Class'Input must still be allowed). Note that attempts to apply
10653 -- stream attributes to a limited interface or its class-wide type
10654 -- (or limited extensions thereof) will still get properly rejected
10655 -- by Check_Stream_Attribute.
10657 -- We exclude the Input operation from being a predefined subprogram in
10658 -- the case where the associated type is an abstract extension, because
10659 -- the attribute is not callable in that case, per 13.13.2(49/2). Also,
10660 -- we don't want an abstract version created because types derived from
10661 -- the abstract type may not even have Input available (for example if
10662 -- derived from a private view of the abstract type that doesn't have
10663 -- a visible Input).
10665 -- Do not generate stream routines for type Finalization_Master because
10666 -- a master may never appear in types and therefore cannot be read or
10667 -- written.
10669 return
10670 (not Is_Limited_Type (Typ)
10671 or else Is_Interface (Typ)
10672 or else Has_Predefined_Or_Specified_Stream_Attribute)
10673 and then
10674 (Operation /= TSS_Stream_Input
10675 or else not Is_Abstract_Type (Typ)
10676 or else not Is_Derived_Type (Typ))
10677 and then not Has_Unknown_Discriminants (Typ)
10678 and then not
10679 (Is_Interface (Typ)
10680 and then
10681 (Is_Task_Interface (Typ)
10682 or else Is_Protected_Interface (Typ)
10683 or else Is_Synchronized_Interface (Typ)))
10684 and then not Restriction_Active (No_Streams)
10685 and then not Restriction_Active (No_Dispatch)
10686 and then No (No_Tagged_Streams_Pragma (Typ))
10687 and then not No_Run_Time_Mode
10688 and then RTE_Available (RE_Tag)
10689 and then No (Type_Without_Stream_Operation (Typ))
10690 and then RTE_Available (RE_Root_Stream_Type)
10691 and then not Is_RTE (Typ, RE_Finalization_Master);
10692 end Stream_Operation_OK;
10694 end Exp_Ch3;