PR tree-optimization/82929
[official-gcc.git] / gcc / ada / exp_ch3.adb
blobf21806923da3099c3179c9dac64831c58cb9c4f3
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-2017, 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 Build_Variant_Record_Equality (Typ : Entity_Id);
135 -- Create An Equality function for the untagged variant record Typ and
136 -- attach it to the TSS list
138 procedure Check_Stream_Attributes (Typ : Entity_Id);
139 -- Check that if a limited extension has a parent with user-defined stream
140 -- attributes, and does not itself have user-defined stream-attributes,
141 -- then any limited component of the extension also has the corresponding
142 -- user-defined stream attributes.
144 procedure Clean_Task_Names
145 (Typ : Entity_Id;
146 Proc_Id : Entity_Id);
147 -- If an initialization procedure includes calls to generate names
148 -- for task subcomponents, indicate that secondary stack cleanup is
149 -- needed after an initialization. Typ is the component type, and Proc_Id
150 -- the initialization procedure for the enclosing composite type.
152 procedure Expand_Freeze_Array_Type (N : Node_Id);
153 -- Freeze an array type. Deals with building the initialization procedure,
154 -- creating the packed array type for a packed array and also with the
155 -- creation of the controlling procedures for the controlled case. The
156 -- argument N is the N_Freeze_Entity node for the type.
158 procedure Expand_Freeze_Class_Wide_Type (N : Node_Id);
159 -- Freeze a class-wide type. Build routine Finalize_Address for the purpose
160 -- of finalizing controlled derivations from the class-wide's root type.
162 procedure Expand_Freeze_Enumeration_Type (N : Node_Id);
163 -- Freeze enumeration type with non-standard representation. Builds the
164 -- array and function needed to convert between enumeration pos and
165 -- enumeration representation values. N is the N_Freeze_Entity node
166 -- for the type.
168 procedure Expand_Freeze_Record_Type (N : Node_Id);
169 -- Freeze record type. Builds all necessary discriminant checking
170 -- and other ancillary functions, and builds dispatch tables where
171 -- needed. The argument N is the N_Freeze_Entity node. This processing
172 -- applies only to E_Record_Type entities, not to class wide types,
173 -- record subtypes, or private types.
175 procedure Expand_Tagged_Root (T : Entity_Id);
176 -- Add a field _Tag at the beginning of the record. This field carries
177 -- the value of the access to the Dispatch table. This procedure is only
178 -- called on root type, the _Tag field being inherited by the descendants.
180 procedure Freeze_Stream_Operations (N : Node_Id; Typ : Entity_Id);
181 -- Treat user-defined stream operations as renaming_as_body if the
182 -- subprogram they rename is not frozen when the type is frozen.
184 procedure Initialization_Warning (E : Entity_Id);
185 -- If static elaboration of the package is requested, indicate
186 -- when a type does meet the conditions for static initialization. If
187 -- E is a type, it has components that have no static initialization.
188 -- if E is an entity, its initial expression is not compile-time known.
190 function Init_Formals (Typ : Entity_Id) return List_Id;
191 -- This function builds the list of formals for an initialization routine.
192 -- The first formal is always _Init with the given type. For task value
193 -- record types and types containing tasks, three additional formals are
194 -- added:
196 -- _Master : Master_Id
197 -- _Chain : in out Activation_Chain
198 -- _Task_Name : String
200 -- The caller must append additional entries for discriminants if required.
202 function Inline_Init_Proc (Typ : Entity_Id) return Boolean;
203 -- Returns true if the initialization procedure of Typ should be inlined
205 function In_Runtime (E : Entity_Id) return Boolean;
206 -- Check if E is defined in the RTL (in a child of Ada or System). Used
207 -- to avoid to bring in the overhead of _Input, _Output for tagged types.
209 function Is_User_Defined_Equality (Prim : Node_Id) return Boolean;
210 -- Returns true if Prim is a user defined equality function
212 function Make_Eq_Body
213 (Typ : Entity_Id;
214 Eq_Name : Name_Id) return Node_Id;
215 -- Build the body of a primitive equality operation for a tagged record
216 -- type, or in Ada 2012 for any record type that has components with a
217 -- user-defined equality. Factored out of Predefined_Primitive_Bodies.
219 function Make_Eq_Case
220 (E : Entity_Id;
221 CL : Node_Id;
222 Discrs : Elist_Id := New_Elmt_List) return List_Id;
223 -- Building block for variant record equality. Defined to share the code
224 -- between the tagged and untagged case. Given a Component_List node CL,
225 -- it generates an 'if' followed by a 'case' statement that compares all
226 -- components of local temporaries named X and Y (that are declared as
227 -- formals at some upper level). E provides the Sloc to be used for the
228 -- generated code.
230 -- IF E is an unchecked_union, Discrs is the list of formals created for
231 -- the inferred discriminants of one operand. These formals are used in
232 -- the generated case statements for each variant of the unchecked union.
234 function Make_Eq_If
235 (E : Entity_Id;
236 L : List_Id) return Node_Id;
237 -- Building block for variant record equality. Defined to share the code
238 -- between the tagged and untagged case. Given the list of components
239 -- (or discriminants) L, it generates a return statement that compares all
240 -- components of local temporaries named X and Y (that are declared as
241 -- formals at some upper level). E provides the Sloc to be used for the
242 -- generated code.
244 function Make_Neq_Body (Tag_Typ : Entity_Id) return Node_Id;
245 -- Search for a renaming of the inequality dispatching primitive of
246 -- this tagged type. If found then build and return the corresponding
247 -- rename-as-body inequality subprogram; otherwise return Empty.
249 procedure Make_Predefined_Primitive_Specs
250 (Tag_Typ : Entity_Id;
251 Predef_List : out List_Id;
252 Renamed_Eq : out Entity_Id);
253 -- Create a list with the specs of the predefined primitive operations.
254 -- For tagged types that are interfaces all these primitives are defined
255 -- abstract.
257 -- The following entries are present for all tagged types, and provide
258 -- the results of the corresponding attribute applied to the object.
259 -- Dispatching is required in general, since the result of the attribute
260 -- will vary with the actual object subtype.
262 -- _size provides result of 'Size attribute
263 -- typSR provides result of 'Read attribute
264 -- typSW provides result of 'Write attribute
265 -- typSI provides result of 'Input attribute
266 -- typSO provides result of 'Output attribute
268 -- The following entries are additionally present for non-limited tagged
269 -- types, and implement additional dispatching operations for predefined
270 -- operations:
272 -- _equality implements "=" operator
273 -- _assign implements assignment operation
274 -- typDF implements deep finalization
275 -- typDA implements deep adjust
277 -- The latter two are empty procedures unless the type contains some
278 -- controlled components that require finalization actions (the deep
279 -- in the name refers to the fact that the action applies to components).
281 -- The list is returned in Predef_List. The Parameter Renamed_Eq either
282 -- returns the value Empty, or else the defining unit name for the
283 -- predefined equality function in the case where the type has a primitive
284 -- operation that is a renaming of predefined equality (but only if there
285 -- is also an overriding user-defined equality function). The returned
286 -- Renamed_Eq will be passed to the corresponding parameter of
287 -- Predefined_Primitive_Bodies.
289 function Has_New_Non_Standard_Rep (T : Entity_Id) return Boolean;
290 -- Returns True if there are representation clauses for type T that are not
291 -- inherited. If the result is false, the init_proc and the discriminant
292 -- checking functions of the parent can be reused by a derived type.
294 procedure Make_Controlling_Function_Wrappers
295 (Tag_Typ : Entity_Id;
296 Decl_List : out List_Id;
297 Body_List : out List_Id);
298 -- Ada 2005 (AI-391): Makes specs and bodies for the wrapper functions
299 -- associated with inherited functions with controlling results which
300 -- are not overridden. The body of each wrapper function consists solely
301 -- of a return statement whose expression is an extension aggregate
302 -- invoking the inherited subprogram's parent subprogram and extended
303 -- with a null association list.
305 function Make_Null_Procedure_Specs (Tag_Typ : Entity_Id) return List_Id;
306 -- Ada 2005 (AI-251): Makes specs for null procedures associated with any
307 -- null procedures inherited from an interface type that have not been
308 -- overridden. Only one null procedure will be created for a given set of
309 -- inherited null procedures with homographic profiles.
311 function Predef_Spec_Or_Body
312 (Loc : Source_Ptr;
313 Tag_Typ : Entity_Id;
314 Name : Name_Id;
315 Profile : List_Id;
316 Ret_Type : Entity_Id := Empty;
317 For_Body : Boolean := False) return Node_Id;
318 -- This function generates the appropriate expansion for a predefined
319 -- primitive operation specified by its name, parameter profile and
320 -- return type (Empty means this is a procedure). If For_Body is false,
321 -- then the returned node is a subprogram declaration. If For_Body is
322 -- true, then the returned node is a empty subprogram body containing
323 -- no declarations and no statements.
325 function Predef_Stream_Attr_Spec
326 (Loc : Source_Ptr;
327 Tag_Typ : Entity_Id;
328 Name : TSS_Name_Type;
329 For_Body : Boolean := False) return Node_Id;
330 -- Specialized version of Predef_Spec_Or_Body that apply to read, write,
331 -- input and output attribute whose specs are constructed in Exp_Strm.
333 function Predef_Deep_Spec
334 (Loc : Source_Ptr;
335 Tag_Typ : Entity_Id;
336 Name : TSS_Name_Type;
337 For_Body : Boolean := False) return Node_Id;
338 -- Specialized version of Predef_Spec_Or_Body that apply to _deep_adjust
339 -- and _deep_finalize
341 function Predefined_Primitive_Bodies
342 (Tag_Typ : Entity_Id;
343 Renamed_Eq : Entity_Id) return List_Id;
344 -- Create the bodies of the predefined primitives that are described in
345 -- Predefined_Primitive_Specs. When not empty, Renamed_Eq must denote
346 -- the defining unit name of the type's predefined equality as returned
347 -- by Make_Predefined_Primitive_Specs.
349 function Predefined_Primitive_Freeze (Tag_Typ : Entity_Id) return List_Id;
350 -- Freeze entities of all predefined primitive operations. This is needed
351 -- because the bodies of these operations do not normally do any freezing.
353 function Stream_Operation_OK
354 (Typ : Entity_Id;
355 Operation : TSS_Name_Type) return Boolean;
356 -- Check whether the named stream operation must be emitted for a given
357 -- type. The rules for inheritance of stream attributes by type extensions
358 -- are enforced by this function. Furthermore, various restrictions prevent
359 -- the generation of these operations, as a useful optimization or for
360 -- certification purposes and to save unnecessary generated code.
362 --------------------------
363 -- Adjust_Discriminants --
364 --------------------------
366 -- This procedure attempts to define subtypes for discriminants that are
367 -- more restrictive than those declared. Such a replacement is possible if
368 -- we can demonstrate that values outside the restricted range would cause
369 -- constraint errors in any case. The advantage of restricting the
370 -- discriminant types in this way is that the maximum size of the variant
371 -- record can be calculated more conservatively.
373 -- An example of a situation in which we can perform this type of
374 -- restriction is the following:
376 -- subtype B is range 1 .. 10;
377 -- type Q is array (B range <>) of Integer;
379 -- type V (N : Natural) is record
380 -- C : Q (1 .. N);
381 -- end record;
383 -- In this situation, we can restrict the upper bound of N to 10, since
384 -- any larger value would cause a constraint error in any case.
386 -- There are many situations in which such restriction is possible, but
387 -- for now, we just look for cases like the above, where the component
388 -- in question is a one dimensional array whose upper bound is one of
389 -- the record discriminants. Also the component must not be part of
390 -- any variant part, since then the component does not always exist.
392 procedure Adjust_Discriminants (Rtype : Entity_Id) is
393 Loc : constant Source_Ptr := Sloc (Rtype);
394 Comp : Entity_Id;
395 Ctyp : Entity_Id;
396 Ityp : Entity_Id;
397 Lo : Node_Id;
398 Hi : Node_Id;
399 P : Node_Id;
400 Loval : Uint;
401 Discr : Entity_Id;
402 Dtyp : Entity_Id;
403 Dhi : Node_Id;
404 Dhiv : Uint;
405 Ahi : Node_Id;
406 Ahiv : Uint;
407 Tnn : Entity_Id;
409 begin
410 Comp := First_Component (Rtype);
411 while Present (Comp) loop
413 -- If our parent is a variant, quit, we do not look at components
414 -- that are in variant parts, because they may not always exist.
416 P := Parent (Comp); -- component declaration
417 P := Parent (P); -- component list
419 exit when Nkind (Parent (P)) = N_Variant;
421 -- We are looking for a one dimensional array type
423 Ctyp := Etype (Comp);
425 if not Is_Array_Type (Ctyp) or else Number_Dimensions (Ctyp) > 1 then
426 goto Continue;
427 end if;
429 -- The lower bound must be constant, and the upper bound is a
430 -- discriminant (which is a discriminant of the current record).
432 Ityp := Etype (First_Index (Ctyp));
433 Lo := Type_Low_Bound (Ityp);
434 Hi := Type_High_Bound (Ityp);
436 if not Compile_Time_Known_Value (Lo)
437 or else Nkind (Hi) /= N_Identifier
438 or else No (Entity (Hi))
439 or else Ekind (Entity (Hi)) /= E_Discriminant
440 then
441 goto Continue;
442 end if;
444 -- We have an array with appropriate bounds
446 Loval := Expr_Value (Lo);
447 Discr := Entity (Hi);
448 Dtyp := Etype (Discr);
450 -- See if the discriminant has a known upper bound
452 Dhi := Type_High_Bound (Dtyp);
454 if not Compile_Time_Known_Value (Dhi) then
455 goto Continue;
456 end if;
458 Dhiv := Expr_Value (Dhi);
460 -- See if base type of component array has known upper bound
462 Ahi := Type_High_Bound (Etype (First_Index (Base_Type (Ctyp))));
464 if not Compile_Time_Known_Value (Ahi) then
465 goto Continue;
466 end if;
468 Ahiv := Expr_Value (Ahi);
470 -- The condition for doing the restriction is that the high bound
471 -- of the discriminant is greater than the low bound of the array,
472 -- and is also greater than the high bound of the base type index.
474 if Dhiv > Loval and then Dhiv > Ahiv then
476 -- We can reset the upper bound of the discriminant type to
477 -- whichever is larger, the low bound of the component, or
478 -- the high bound of the base type array index.
480 -- We build a subtype that is declared as
482 -- subtype Tnn is discr_type range discr_type'First .. max;
484 -- And insert this declaration into the tree. The type of the
485 -- discriminant is then reset to this more restricted subtype.
487 Tnn := Make_Temporary (Loc, 'T');
489 Insert_Action (Declaration_Node (Rtype),
490 Make_Subtype_Declaration (Loc,
491 Defining_Identifier => Tnn,
492 Subtype_Indication =>
493 Make_Subtype_Indication (Loc,
494 Subtype_Mark => New_Occurrence_Of (Dtyp, Loc),
495 Constraint =>
496 Make_Range_Constraint (Loc,
497 Range_Expression =>
498 Make_Range (Loc,
499 Low_Bound =>
500 Make_Attribute_Reference (Loc,
501 Attribute_Name => Name_First,
502 Prefix => New_Occurrence_Of (Dtyp, Loc)),
503 High_Bound =>
504 Make_Integer_Literal (Loc,
505 Intval => UI_Max (Loval, Ahiv)))))));
507 Set_Etype (Discr, Tnn);
508 end if;
510 <<Continue>>
511 Next_Component (Comp);
512 end loop;
513 end Adjust_Discriminants;
515 ---------------------------
516 -- Build_Array_Init_Proc --
517 ---------------------------
519 procedure Build_Array_Init_Proc (A_Type : Entity_Id; Nod : Node_Id) is
520 Comp_Type : constant Entity_Id := Component_Type (A_Type);
521 Comp_Simple_Init : constant Boolean :=
522 Needs_Simple_Initialization
523 (T => Comp_Type,
524 Consider_IS =>
525 not (Validity_Check_Copies and Is_Bit_Packed_Array (A_Type)));
526 -- True if the component needs simple initialization, based on its type,
527 -- plus the fact that we do not do simple initialization for components
528 -- of bit-packed arrays when validity checks are enabled, because the
529 -- initialization with deliberately out-of-range values would raise
530 -- Constraint_Error.
532 Body_Stmts : List_Id;
533 Has_Default_Init : Boolean;
534 Index_List : List_Id;
535 Loc : Source_Ptr;
536 Proc_Id : Entity_Id;
538 function Init_Component return List_Id;
539 -- Create one statement to initialize one array component, designated
540 -- by a full set of indexes.
542 function Init_One_Dimension (N : Int) return List_Id;
543 -- Create loop to initialize one dimension of the array. The single
544 -- statement in the loop body initializes the inner dimensions if any,
545 -- or else the single component. Note that this procedure is called
546 -- recursively, with N being the dimension to be initialized. A call
547 -- with N greater than the number of dimensions simply generates the
548 -- component initialization, terminating the recursion.
550 --------------------
551 -- Init_Component --
552 --------------------
554 function Init_Component return List_Id is
555 Comp : Node_Id;
557 begin
558 Comp :=
559 Make_Indexed_Component (Loc,
560 Prefix => Make_Identifier (Loc, Name_uInit),
561 Expressions => Index_List);
563 if Has_Default_Aspect (A_Type) then
564 Set_Assignment_OK (Comp);
565 return New_List (
566 Make_Assignment_Statement (Loc,
567 Name => Comp,
568 Expression =>
569 Convert_To (Comp_Type,
570 Default_Aspect_Component_Value (First_Subtype (A_Type)))));
572 elsif Comp_Simple_Init then
573 Set_Assignment_OK (Comp);
574 return New_List (
575 Make_Assignment_Statement (Loc,
576 Name => Comp,
577 Expression =>
578 Get_Simple_Init_Val
579 (Comp_Type, Nod, Component_Size (A_Type))));
581 else
582 Clean_Task_Names (Comp_Type, Proc_Id);
583 return
584 Build_Initialization_Call
585 (Loc, Comp, Comp_Type,
586 In_Init_Proc => True,
587 Enclos_Type => A_Type);
588 end if;
589 end Init_Component;
591 ------------------------
592 -- Init_One_Dimension --
593 ------------------------
595 function Init_One_Dimension (N : Int) return List_Id is
596 Index : Entity_Id;
598 begin
599 -- If the component does not need initializing, then there is nothing
600 -- to do here, so we return a null body. This occurs when generating
601 -- the dummy Init_Proc needed for Initialize_Scalars processing.
603 if not Has_Non_Null_Base_Init_Proc (Comp_Type)
604 and then not Comp_Simple_Init
605 and then not Has_Task (Comp_Type)
606 and then not Has_Default_Aspect (A_Type)
607 then
608 return New_List (Make_Null_Statement (Loc));
610 -- If all dimensions dealt with, we simply initialize the component
612 elsif N > Number_Dimensions (A_Type) then
613 return Init_Component;
615 -- Here we generate the required loop
617 else
618 Index :=
619 Make_Defining_Identifier (Loc, New_External_Name ('J', N));
621 Append (New_Occurrence_Of (Index, Loc), Index_List);
623 return New_List (
624 Make_Implicit_Loop_Statement (Nod,
625 Identifier => Empty,
626 Iteration_Scheme =>
627 Make_Iteration_Scheme (Loc,
628 Loop_Parameter_Specification =>
629 Make_Loop_Parameter_Specification (Loc,
630 Defining_Identifier => Index,
631 Discrete_Subtype_Definition =>
632 Make_Attribute_Reference (Loc,
633 Prefix =>
634 Make_Identifier (Loc, Name_uInit),
635 Attribute_Name => Name_Range,
636 Expressions => New_List (
637 Make_Integer_Literal (Loc, N))))),
638 Statements => Init_One_Dimension (N + 1)));
639 end if;
640 end Init_One_Dimension;
642 -- Start of processing for Build_Array_Init_Proc
644 begin
645 -- The init proc is created when analyzing the freeze node for the type,
646 -- but it properly belongs with the array type declaration. However, if
647 -- the freeze node is for a subtype of a type declared in another unit
648 -- it seems preferable to use the freeze node as the source location of
649 -- the init proc. In any case this is preferable for gcov usage, and
650 -- the Sloc is not otherwise used by the compiler.
652 if In_Open_Scopes (Scope (A_Type)) then
653 Loc := Sloc (A_Type);
654 else
655 Loc := Sloc (Nod);
656 end if;
658 -- Nothing to generate in the following cases:
660 -- 1. Initialization is suppressed for the type
661 -- 2. An initialization already exists for the base type
663 if Initialization_Suppressed (A_Type)
664 or else Present (Base_Init_Proc (A_Type))
665 then
666 return;
667 end if;
669 Index_List := New_List;
671 -- We need an initialization procedure if any of the following is true:
673 -- 1. The component type has an initialization procedure
674 -- 2. The component type needs simple initialization
675 -- 3. Tasks are present
676 -- 4. The type is marked as a public entity
677 -- 5. The array type has a Default_Component_Value aspect
679 -- The reason for the public entity test is to deal properly with the
680 -- Initialize_Scalars pragma. This pragma can be set in the client and
681 -- not in the declaring package, this means the client will make a call
682 -- to the initialization procedure (because one of conditions 1-3 must
683 -- apply in this case), and we must generate a procedure (even if it is
684 -- null) to satisfy the call in this case.
686 -- Exception: do not build an array init_proc for a type whose root
687 -- type is Standard.String or Standard.Wide_[Wide_]String, since there
688 -- is no place to put the code, and in any case we handle initialization
689 -- of such types (in the Initialize_Scalars case, that's the only time
690 -- the issue arises) in a special manner anyway which does not need an
691 -- init_proc.
693 Has_Default_Init := Has_Non_Null_Base_Init_Proc (Comp_Type)
694 or else Comp_Simple_Init
695 or else Has_Task (Comp_Type)
696 or else Has_Default_Aspect (A_Type);
698 if Has_Default_Init
699 or else (not Restriction_Active (No_Initialize_Scalars)
700 and then Is_Public (A_Type)
701 and then not Is_Standard_String_Type (A_Type))
702 then
703 Proc_Id :=
704 Make_Defining_Identifier (Loc,
705 Chars => Make_Init_Proc_Name (A_Type));
707 -- If No_Default_Initialization restriction is active, then we don't
708 -- want to build an init_proc, but we need to mark that an init_proc
709 -- would be needed if this restriction was not active (so that we can
710 -- detect attempts to call it), so set a dummy init_proc in place.
711 -- This is only done though when actual default initialization is
712 -- needed (and not done when only Is_Public is True), since otherwise
713 -- objects such as arrays of scalars could be wrongly flagged as
714 -- violating the restriction.
716 if Restriction_Active (No_Default_Initialization) then
717 if Has_Default_Init then
718 Set_Init_Proc (A_Type, Proc_Id);
719 end if;
721 return;
722 end if;
724 Body_Stmts := Init_One_Dimension (1);
726 Discard_Node (
727 Make_Subprogram_Body (Loc,
728 Specification =>
729 Make_Procedure_Specification (Loc,
730 Defining_Unit_Name => Proc_Id,
731 Parameter_Specifications => Init_Formals (A_Type)),
732 Declarations => New_List,
733 Handled_Statement_Sequence =>
734 Make_Handled_Sequence_Of_Statements (Loc,
735 Statements => Body_Stmts)));
737 Set_Ekind (Proc_Id, E_Procedure);
738 Set_Is_Public (Proc_Id, Is_Public (A_Type));
739 Set_Is_Internal (Proc_Id);
740 Set_Has_Completion (Proc_Id);
742 if not Debug_Generated_Code then
743 Set_Debug_Info_Off (Proc_Id);
744 end if;
746 -- Set Inlined on Init_Proc if it is set on the Init_Proc of the
747 -- component type itself (see also Build_Record_Init_Proc).
749 Set_Is_Inlined (Proc_Id, Inline_Init_Proc (Comp_Type));
751 -- Associate Init_Proc with type, and determine if the procedure
752 -- is null (happens because of the Initialize_Scalars pragma case,
753 -- where we have to generate a null procedure in case it is called
754 -- by a client with Initialize_Scalars set). Such procedures have
755 -- to be generated, but do not have to be called, so we mark them
756 -- as null to suppress the call.
758 Set_Init_Proc (A_Type, Proc_Id);
760 if List_Length (Body_Stmts) = 1
762 -- We must skip SCIL nodes because they may have been added to this
763 -- list by Insert_Actions.
765 and then Nkind (First_Non_SCIL_Node (Body_Stmts)) = N_Null_Statement
766 then
767 Set_Is_Null_Init_Proc (Proc_Id);
769 else
770 -- Try to build a static aggregate to statically initialize
771 -- objects of the type. This can only be done for constrained
772 -- one-dimensional arrays with static bounds.
774 Set_Static_Initialization
775 (Proc_Id,
776 Build_Equivalent_Array_Aggregate (First_Subtype (A_Type)));
777 end if;
778 end if;
779 end Build_Array_Init_Proc;
781 --------------------------------
782 -- Build_Discr_Checking_Funcs --
783 --------------------------------
785 procedure Build_Discr_Checking_Funcs (N : Node_Id) is
786 Rec_Id : Entity_Id;
787 Loc : Source_Ptr;
788 Enclosing_Func_Id : Entity_Id;
789 Sequence : Nat := 1;
790 Type_Def : Node_Id;
791 V : Node_Id;
793 function Build_Case_Statement
794 (Case_Id : Entity_Id;
795 Variant : Node_Id) return Node_Id;
796 -- Build a case statement containing only two alternatives. The first
797 -- alternative corresponds exactly to the discrete choices given on the
798 -- variant with contains the components that we are generating the
799 -- checks for. If the discriminant is one of these return False. The
800 -- second alternative is an OTHERS choice that will return True
801 -- indicating the discriminant did not match.
803 function Build_Dcheck_Function
804 (Case_Id : Entity_Id;
805 Variant : Node_Id) return Entity_Id;
806 -- Build the discriminant checking function for a given variant
808 procedure Build_Dcheck_Functions (Variant_Part_Node : Node_Id);
809 -- Builds the discriminant checking function for each variant of the
810 -- given variant part of the record type.
812 --------------------------
813 -- Build_Case_Statement --
814 --------------------------
816 function Build_Case_Statement
817 (Case_Id : Entity_Id;
818 Variant : Node_Id) return Node_Id
820 Alt_List : constant List_Id := New_List;
821 Actuals_List : List_Id;
822 Case_Node : Node_Id;
823 Case_Alt_Node : Node_Id;
824 Choice : Node_Id;
825 Choice_List : List_Id;
826 D : Entity_Id;
827 Return_Node : Node_Id;
829 begin
830 Case_Node := New_Node (N_Case_Statement, Loc);
832 -- Replace the discriminant which controls the variant with the name
833 -- of the formal of the checking function.
835 Set_Expression (Case_Node, Make_Identifier (Loc, Chars (Case_Id)));
837 Choice := First (Discrete_Choices (Variant));
839 if Nkind (Choice) = N_Others_Choice then
840 Choice_List := New_Copy_List (Others_Discrete_Choices (Choice));
841 else
842 Choice_List := New_Copy_List (Discrete_Choices (Variant));
843 end if;
845 if not Is_Empty_List (Choice_List) then
846 Case_Alt_Node := New_Node (N_Case_Statement_Alternative, Loc);
847 Set_Discrete_Choices (Case_Alt_Node, Choice_List);
849 -- In case this is a nested variant, we need to return the result
850 -- of the discriminant checking function for the immediately
851 -- enclosing variant.
853 if Present (Enclosing_Func_Id) then
854 Actuals_List := New_List;
856 D := First_Discriminant (Rec_Id);
857 while Present (D) loop
858 Append (Make_Identifier (Loc, Chars (D)), Actuals_List);
859 Next_Discriminant (D);
860 end loop;
862 Return_Node :=
863 Make_Simple_Return_Statement (Loc,
864 Expression =>
865 Make_Function_Call (Loc,
866 Name =>
867 New_Occurrence_Of (Enclosing_Func_Id, Loc),
868 Parameter_Associations =>
869 Actuals_List));
871 else
872 Return_Node :=
873 Make_Simple_Return_Statement (Loc,
874 Expression =>
875 New_Occurrence_Of (Standard_False, Loc));
876 end if;
878 Set_Statements (Case_Alt_Node, New_List (Return_Node));
879 Append (Case_Alt_Node, Alt_List);
880 end if;
882 Case_Alt_Node := New_Node (N_Case_Statement_Alternative, Loc);
883 Choice_List := New_List (New_Node (N_Others_Choice, Loc));
884 Set_Discrete_Choices (Case_Alt_Node, Choice_List);
886 Return_Node :=
887 Make_Simple_Return_Statement (Loc,
888 Expression =>
889 New_Occurrence_Of (Standard_True, Loc));
891 Set_Statements (Case_Alt_Node, New_List (Return_Node));
892 Append (Case_Alt_Node, Alt_List);
894 Set_Alternatives (Case_Node, Alt_List);
895 return Case_Node;
896 end Build_Case_Statement;
898 ---------------------------
899 -- Build_Dcheck_Function --
900 ---------------------------
902 function Build_Dcheck_Function
903 (Case_Id : Entity_Id;
904 Variant : Node_Id) return Entity_Id
906 Body_Node : Node_Id;
907 Func_Id : Entity_Id;
908 Parameter_List : List_Id;
909 Spec_Node : Node_Id;
911 begin
912 Body_Node := New_Node (N_Subprogram_Body, Loc);
913 Sequence := Sequence + 1;
915 Func_Id :=
916 Make_Defining_Identifier (Loc,
917 Chars => New_External_Name (Chars (Rec_Id), 'D', Sequence));
918 Set_Is_Discriminant_Check_Function (Func_Id);
920 Spec_Node := New_Node (N_Function_Specification, Loc);
921 Set_Defining_Unit_Name (Spec_Node, Func_Id);
923 Parameter_List := Build_Discriminant_Formals (Rec_Id, False);
925 Set_Parameter_Specifications (Spec_Node, Parameter_List);
926 Set_Result_Definition (Spec_Node,
927 New_Occurrence_Of (Standard_Boolean, Loc));
928 Set_Specification (Body_Node, Spec_Node);
929 Set_Declarations (Body_Node, New_List);
931 Set_Handled_Statement_Sequence (Body_Node,
932 Make_Handled_Sequence_Of_Statements (Loc,
933 Statements => New_List (
934 Build_Case_Statement (Case_Id, Variant))));
936 Set_Ekind (Func_Id, E_Function);
937 Set_Mechanism (Func_Id, Default_Mechanism);
938 Set_Is_Inlined (Func_Id, True);
939 Set_Is_Pure (Func_Id, True);
940 Set_Is_Public (Func_Id, Is_Public (Rec_Id));
941 Set_Is_Internal (Func_Id, True);
943 if not Debug_Generated_Code then
944 Set_Debug_Info_Off (Func_Id);
945 end if;
947 Analyze (Body_Node);
949 Append_Freeze_Action (Rec_Id, Body_Node);
950 Set_Dcheck_Function (Variant, Func_Id);
951 return Func_Id;
952 end Build_Dcheck_Function;
954 ----------------------------
955 -- Build_Dcheck_Functions --
956 ----------------------------
958 procedure Build_Dcheck_Functions (Variant_Part_Node : Node_Id) is
959 Component_List_Node : Node_Id;
960 Decl : Entity_Id;
961 Discr_Name : Entity_Id;
962 Func_Id : Entity_Id;
963 Variant : Node_Id;
964 Saved_Enclosing_Func_Id : Entity_Id;
966 begin
967 -- Build the discriminant-checking function for each variant, and
968 -- label all components of that variant with the function's name.
969 -- We only Generate a discriminant-checking function when the
970 -- variant is not empty, to prevent the creation of dead code.
972 Discr_Name := Entity (Name (Variant_Part_Node));
973 Variant := First_Non_Pragma (Variants (Variant_Part_Node));
975 while Present (Variant) loop
976 Component_List_Node := Component_List (Variant);
978 if not Null_Present (Component_List_Node) then
979 Func_Id := Build_Dcheck_Function (Discr_Name, Variant);
981 Decl :=
982 First_Non_Pragma (Component_Items (Component_List_Node));
983 while Present (Decl) loop
984 Set_Discriminant_Checking_Func
985 (Defining_Identifier (Decl), Func_Id);
986 Next_Non_Pragma (Decl);
987 end loop;
989 if Present (Variant_Part (Component_List_Node)) then
990 Saved_Enclosing_Func_Id := Enclosing_Func_Id;
991 Enclosing_Func_Id := Func_Id;
992 Build_Dcheck_Functions (Variant_Part (Component_List_Node));
993 Enclosing_Func_Id := Saved_Enclosing_Func_Id;
994 end if;
995 end if;
997 Next_Non_Pragma (Variant);
998 end loop;
999 end Build_Dcheck_Functions;
1001 -- Start of processing for Build_Discr_Checking_Funcs
1003 begin
1004 -- Only build if not done already
1006 if not Discr_Check_Funcs_Built (N) then
1007 Type_Def := Type_Definition (N);
1009 if Nkind (Type_Def) = N_Record_Definition then
1010 if No (Component_List (Type_Def)) then -- null record.
1011 return;
1012 else
1013 V := Variant_Part (Component_List (Type_Def));
1014 end if;
1016 else pragma Assert (Nkind (Type_Def) = N_Derived_Type_Definition);
1017 if No (Component_List (Record_Extension_Part (Type_Def))) then
1018 return;
1019 else
1020 V := Variant_Part
1021 (Component_List (Record_Extension_Part (Type_Def)));
1022 end if;
1023 end if;
1025 Rec_Id := Defining_Identifier (N);
1027 if Present (V) and then not Is_Unchecked_Union (Rec_Id) then
1028 Loc := Sloc (N);
1029 Enclosing_Func_Id := Empty;
1030 Build_Dcheck_Functions (V);
1031 end if;
1033 Set_Discr_Check_Funcs_Built (N);
1034 end if;
1035 end Build_Discr_Checking_Funcs;
1037 --------------------------------
1038 -- Build_Discriminant_Formals --
1039 --------------------------------
1041 function Build_Discriminant_Formals
1042 (Rec_Id : Entity_Id;
1043 Use_Dl : Boolean) return List_Id
1045 Loc : Source_Ptr := Sloc (Rec_Id);
1046 Parameter_List : constant List_Id := New_List;
1047 D : Entity_Id;
1048 Formal : Entity_Id;
1049 Formal_Type : Entity_Id;
1050 Param_Spec_Node : Node_Id;
1052 begin
1053 if Has_Discriminants (Rec_Id) then
1054 D := First_Discriminant (Rec_Id);
1055 while Present (D) loop
1056 Loc := Sloc (D);
1058 if Use_Dl then
1059 Formal := Discriminal (D);
1060 Formal_Type := Etype (Formal);
1061 else
1062 Formal := Make_Defining_Identifier (Loc, Chars (D));
1063 Formal_Type := Etype (D);
1064 end if;
1066 Param_Spec_Node :=
1067 Make_Parameter_Specification (Loc,
1068 Defining_Identifier => Formal,
1069 Parameter_Type =>
1070 New_Occurrence_Of (Formal_Type, Loc));
1071 Append (Param_Spec_Node, Parameter_List);
1072 Next_Discriminant (D);
1073 end loop;
1074 end if;
1076 return Parameter_List;
1077 end Build_Discriminant_Formals;
1079 --------------------------------------
1080 -- Build_Equivalent_Array_Aggregate --
1081 --------------------------------------
1083 function Build_Equivalent_Array_Aggregate (T : Entity_Id) return Node_Id is
1084 Loc : constant Source_Ptr := Sloc (T);
1085 Comp_Type : constant Entity_Id := Component_Type (T);
1086 Index_Type : constant Entity_Id := Etype (First_Index (T));
1087 Proc : constant Entity_Id := Base_Init_Proc (T);
1088 Lo, Hi : Node_Id;
1089 Aggr : Node_Id;
1090 Expr : Node_Id;
1092 begin
1093 if not Is_Constrained (T)
1094 or else Number_Dimensions (T) > 1
1095 or else No (Proc)
1096 then
1097 Initialization_Warning (T);
1098 return Empty;
1099 end if;
1101 Lo := Type_Low_Bound (Index_Type);
1102 Hi := Type_High_Bound (Index_Type);
1104 if not Compile_Time_Known_Value (Lo)
1105 or else not Compile_Time_Known_Value (Hi)
1106 then
1107 Initialization_Warning (T);
1108 return Empty;
1109 end if;
1111 if Is_Record_Type (Comp_Type)
1112 and then Present (Base_Init_Proc (Comp_Type))
1113 then
1114 Expr := Static_Initialization (Base_Init_Proc (Comp_Type));
1116 if No (Expr) then
1117 Initialization_Warning (T);
1118 return Empty;
1119 end if;
1121 else
1122 Initialization_Warning (T);
1123 return Empty;
1124 end if;
1126 Aggr := Make_Aggregate (Loc, No_List, New_List);
1127 Set_Etype (Aggr, T);
1128 Set_Aggregate_Bounds (Aggr,
1129 Make_Range (Loc,
1130 Low_Bound => New_Copy (Lo),
1131 High_Bound => New_Copy (Hi)));
1132 Set_Parent (Aggr, Parent (Proc));
1134 Append_To (Component_Associations (Aggr),
1135 Make_Component_Association (Loc,
1136 Choices =>
1137 New_List (
1138 Make_Range (Loc,
1139 Low_Bound => New_Copy (Lo),
1140 High_Bound => New_Copy (Hi))),
1141 Expression => Expr));
1143 if Static_Array_Aggregate (Aggr) then
1144 return Aggr;
1145 else
1146 Initialization_Warning (T);
1147 return Empty;
1148 end if;
1149 end Build_Equivalent_Array_Aggregate;
1151 ---------------------------------------
1152 -- Build_Equivalent_Record_Aggregate --
1153 ---------------------------------------
1155 function Build_Equivalent_Record_Aggregate (T : Entity_Id) return Node_Id is
1156 Agg : Node_Id;
1157 Comp : Entity_Id;
1158 Comp_Type : Entity_Id;
1160 -- Start of processing for Build_Equivalent_Record_Aggregate
1162 begin
1163 if not Is_Record_Type (T)
1164 or else Has_Discriminants (T)
1165 or else Is_Limited_Type (T)
1166 or else Has_Non_Standard_Rep (T)
1167 then
1168 Initialization_Warning (T);
1169 return Empty;
1170 end if;
1172 Comp := First_Component (T);
1174 -- A null record needs no warning
1176 if No (Comp) then
1177 return Empty;
1178 end if;
1180 while Present (Comp) loop
1182 -- Array components are acceptable if initialized by a positional
1183 -- aggregate with static components.
1185 if Is_Array_Type (Etype (Comp)) then
1186 Comp_Type := Component_Type (Etype (Comp));
1188 if Nkind (Parent (Comp)) /= N_Component_Declaration
1189 or else No (Expression (Parent (Comp)))
1190 or else Nkind (Expression (Parent (Comp))) /= N_Aggregate
1191 then
1192 Initialization_Warning (T);
1193 return Empty;
1195 elsif Is_Scalar_Type (Component_Type (Etype (Comp)))
1196 and then
1197 (not Compile_Time_Known_Value (Type_Low_Bound (Comp_Type))
1198 or else
1199 not Compile_Time_Known_Value (Type_High_Bound (Comp_Type)))
1200 then
1201 Initialization_Warning (T);
1202 return Empty;
1204 elsif
1205 not Static_Array_Aggregate (Expression (Parent (Comp)))
1206 then
1207 Initialization_Warning (T);
1208 return Empty;
1209 end if;
1211 elsif Is_Scalar_Type (Etype (Comp)) then
1212 Comp_Type := Etype (Comp);
1214 if Nkind (Parent (Comp)) /= N_Component_Declaration
1215 or else No (Expression (Parent (Comp)))
1216 or else not Compile_Time_Known_Value (Expression (Parent (Comp)))
1217 or else not Compile_Time_Known_Value (Type_Low_Bound (Comp_Type))
1218 or else not
1219 Compile_Time_Known_Value (Type_High_Bound (Comp_Type))
1220 then
1221 Initialization_Warning (T);
1222 return Empty;
1223 end if;
1225 -- For now, other types are excluded
1227 else
1228 Initialization_Warning (T);
1229 return Empty;
1230 end if;
1232 Next_Component (Comp);
1233 end loop;
1235 -- All components have static initialization. Build positional aggregate
1236 -- from the given expressions or defaults.
1238 Agg := Make_Aggregate (Sloc (T), New_List, New_List);
1239 Set_Parent (Agg, Parent (T));
1241 Comp := First_Component (T);
1242 while Present (Comp) loop
1243 Append
1244 (New_Copy_Tree (Expression (Parent (Comp))), Expressions (Agg));
1245 Next_Component (Comp);
1246 end loop;
1248 Analyze_And_Resolve (Agg, T);
1249 return Agg;
1250 end Build_Equivalent_Record_Aggregate;
1252 -------------------------------
1253 -- Build_Initialization_Call --
1254 -------------------------------
1256 -- References to a discriminant inside the record type declaration can
1257 -- appear either in the subtype_indication to constrain a record or an
1258 -- array, or as part of a larger expression given for the initial value
1259 -- of a component. In both of these cases N appears in the record
1260 -- initialization procedure and needs to be replaced by the formal
1261 -- parameter of the initialization procedure which corresponds to that
1262 -- discriminant.
1264 -- In the example below, references to discriminants D1 and D2 in proc_1
1265 -- are replaced by references to formals with the same name
1266 -- (discriminals)
1268 -- A similar replacement is done for calls to any record initialization
1269 -- procedure for any components that are themselves of a record type.
1271 -- type R (D1, D2 : Integer) is record
1272 -- X : Integer := F * D1;
1273 -- Y : Integer := F * D2;
1274 -- end record;
1276 -- procedure proc_1 (Out_2 : out R; D1 : Integer; D2 : Integer) is
1277 -- begin
1278 -- Out_2.D1 := D1;
1279 -- Out_2.D2 := D2;
1280 -- Out_2.X := F * D1;
1281 -- Out_2.Y := F * D2;
1282 -- end;
1284 function Build_Initialization_Call
1285 (Loc : Source_Ptr;
1286 Id_Ref : Node_Id;
1287 Typ : Entity_Id;
1288 In_Init_Proc : Boolean := False;
1289 Enclos_Type : Entity_Id := Empty;
1290 Discr_Map : Elist_Id := New_Elmt_List;
1291 With_Default_Init : Boolean := False;
1292 Constructor_Ref : Node_Id := Empty) return List_Id
1294 Res : constant List_Id := New_List;
1296 Full_Type : Entity_Id;
1298 procedure Check_Predicated_Discriminant
1299 (Val : Node_Id;
1300 Discr : Entity_Id);
1301 -- Discriminants whose subtypes have predicates are checked in two
1302 -- cases:
1303 -- a) When an object is default-initialized and assertions are enabled
1304 -- we check that the value of the discriminant obeys the predicate.
1306 -- b) In all cases, if the discriminant controls a variant and the
1307 -- variant has no others_choice, Constraint_Error must be raised if
1308 -- the predicate is violated, because there is no variant covered
1309 -- by the illegal discriminant value.
1311 -----------------------------------
1312 -- Check_Predicated_Discriminant --
1313 -----------------------------------
1315 procedure Check_Predicated_Discriminant
1316 (Val : Node_Id;
1317 Discr : Entity_Id)
1319 Typ : constant Entity_Id := Etype (Discr);
1321 procedure Check_Missing_Others (V : Node_Id);
1322 -- ???
1324 --------------------------
1325 -- Check_Missing_Others --
1326 --------------------------
1328 procedure Check_Missing_Others (V : Node_Id) is
1329 Alt : Node_Id;
1330 Choice : Node_Id;
1331 Last_Var : Node_Id;
1333 begin
1334 Last_Var := Last_Non_Pragma (Variants (V));
1335 Choice := First (Discrete_Choices (Last_Var));
1337 -- An others_choice is added during expansion for gcc use, but
1338 -- does not cover the illegality.
1340 if Entity (Name (V)) = Discr then
1341 if Present (Choice)
1342 and then (Nkind (Choice) /= N_Others_Choice
1343 or else not Comes_From_Source (Choice))
1344 then
1345 Check_Expression_Against_Static_Predicate (Val, Typ);
1347 if not Is_Static_Expression (Val) then
1348 Prepend_To (Res,
1349 Make_Raise_Constraint_Error (Loc,
1350 Condition =>
1351 Make_Op_Not (Loc,
1352 Right_Opnd => Make_Predicate_Call (Typ, Val)),
1353 Reason => CE_Invalid_Data));
1354 end if;
1355 end if;
1356 end if;
1358 -- Check whether some nested variant is ruled by the predicated
1359 -- discriminant.
1361 Alt := First (Variants (V));
1362 while Present (Alt) loop
1363 if Nkind (Alt) = N_Variant
1364 and then Present (Variant_Part (Component_List (Alt)))
1365 then
1366 Check_Missing_Others
1367 (Variant_Part (Component_List (Alt)));
1368 end if;
1370 Next (Alt);
1371 end loop;
1372 end Check_Missing_Others;
1374 -- Local variables
1376 Def : Node_Id;
1378 -- Start of processing for Check_Predicated_Discriminant
1380 begin
1381 if Ekind (Base_Type (Full_Type)) = E_Record_Type then
1382 Def := Type_Definition (Parent (Base_Type (Full_Type)));
1383 else
1384 return;
1385 end if;
1387 if Policy_In_Effect (Name_Assert) = Name_Check
1388 and then not Predicates_Ignored (Etype (Discr))
1389 then
1390 Prepend_To (Res, Make_Predicate_Check (Typ, Val));
1391 end if;
1393 -- If discriminant controls a variant, verify that predicate is
1394 -- obeyed or else an Others_Choice is present.
1396 if Nkind (Def) = N_Record_Definition
1397 and then Present (Variant_Part (Component_List (Def)))
1398 and then Policy_In_Effect (Name_Assert) = Name_Ignore
1399 then
1400 Check_Missing_Others (Variant_Part (Component_List (Def)));
1401 end if;
1402 end Check_Predicated_Discriminant;
1404 -- Local variables
1406 Arg : Node_Id;
1407 Args : List_Id;
1408 Decls : List_Id;
1409 Decl : Node_Id;
1410 Discr : Entity_Id;
1411 First_Arg : Node_Id;
1412 Full_Init_Type : Entity_Id;
1413 Init_Call : Node_Id;
1414 Init_Type : Entity_Id;
1415 Proc : Entity_Id;
1417 -- Start of processing for Build_Initialization_Call
1419 begin
1420 pragma Assert (Constructor_Ref = Empty
1421 or else Is_CPP_Constructor_Call (Constructor_Ref));
1423 if No (Constructor_Ref) then
1424 Proc := Base_Init_Proc (Typ);
1425 else
1426 Proc := Base_Init_Proc (Typ, Entity (Name (Constructor_Ref)));
1427 end if;
1429 pragma Assert (Present (Proc));
1430 Init_Type := Etype (First_Formal (Proc));
1431 Full_Init_Type := Underlying_Type (Init_Type);
1433 -- Nothing to do if the Init_Proc is null, unless Initialize_Scalars
1434 -- is active (in which case we make the call anyway, since in the
1435 -- actual compiled client it may be non null).
1437 if Is_Null_Init_Proc (Proc) and then not Init_Or_Norm_Scalars then
1438 return Empty_List;
1440 -- Nothing to do for an array of controlled components that have only
1441 -- the inherited Initialize primitive. This is a useful optimization
1442 -- for CodePeer.
1444 elsif Is_Trivial_Subprogram (Proc)
1445 and then Is_Array_Type (Full_Init_Type)
1446 then
1447 return New_List (Make_Null_Statement (Loc));
1448 end if;
1450 -- Use the [underlying] full view when dealing with a private type. This
1451 -- may require several steps depending on derivations.
1453 Full_Type := Typ;
1454 loop
1455 if Is_Private_Type (Full_Type) then
1456 if Present (Full_View (Full_Type)) then
1457 Full_Type := Full_View (Full_Type);
1459 elsif Present (Underlying_Full_View (Full_Type)) then
1460 Full_Type := Underlying_Full_View (Full_Type);
1462 -- When a private type acts as a generic actual and lacks a full
1463 -- view, use the base type.
1465 elsif Is_Generic_Actual_Type (Full_Type) then
1466 Full_Type := Base_Type (Full_Type);
1468 elsif Ekind (Full_Type) = E_Private_Subtype
1469 and then (not Has_Discriminants (Full_Type)
1470 or else No (Discriminant_Constraint (Full_Type)))
1471 then
1472 Full_Type := Etype (Full_Type);
1474 -- The loop has recovered the [underlying] full view, stop the
1475 -- traversal.
1477 else
1478 exit;
1479 end if;
1481 -- The type is not private, nothing to do
1483 else
1484 exit;
1485 end if;
1486 end loop;
1488 -- If Typ is derived, the procedure is the initialization procedure for
1489 -- the root type. Wrap the argument in an conversion to make it type
1490 -- honest. Actually it isn't quite type honest, because there can be
1491 -- conflicts of views in the private type case. That is why we set
1492 -- Conversion_OK in the conversion node.
1494 if (Is_Record_Type (Typ)
1495 or else Is_Array_Type (Typ)
1496 or else Is_Private_Type (Typ))
1497 and then Init_Type /= Base_Type (Typ)
1498 then
1499 First_Arg := OK_Convert_To (Etype (Init_Type), Id_Ref);
1500 Set_Etype (First_Arg, Init_Type);
1502 else
1503 First_Arg := Id_Ref;
1504 end if;
1506 Args := New_List (Convert_Concurrent (First_Arg, Typ));
1508 -- In the tasks case, add _Master as the value of the _Master parameter
1509 -- and _Chain as the value of the _Chain parameter. At the outer level,
1510 -- these will be variables holding the corresponding values obtained
1511 -- from GNARL. At inner levels, they will be the parameters passed down
1512 -- through the outer routines.
1514 if Has_Task (Full_Type) then
1515 if Restriction_Active (No_Task_Hierarchy) then
1516 Append_To (Args,
1517 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
1518 else
1519 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
1520 end if;
1522 -- Add _Chain (not done for sequential elaboration policy, see
1523 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
1525 if Partition_Elaboration_Policy /= 'S' then
1526 Append_To (Args, Make_Identifier (Loc, Name_uChain));
1527 end if;
1529 -- Ada 2005 (AI-287): In case of default initialized components
1530 -- with tasks, we generate a null string actual parameter.
1531 -- This is just a workaround that must be improved later???
1533 if With_Default_Init then
1534 Append_To (Args,
1535 Make_String_Literal (Loc,
1536 Strval => ""));
1538 else
1539 Decls :=
1540 Build_Task_Image_Decls (Loc, Id_Ref, Enclos_Type, In_Init_Proc);
1541 Decl := Last (Decls);
1543 Append_To (Args,
1544 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
1545 Append_List (Decls, Res);
1546 end if;
1548 else
1549 Decls := No_List;
1550 Decl := Empty;
1551 end if;
1553 -- Add discriminant values if discriminants are present
1555 if Has_Discriminants (Full_Init_Type) then
1556 Discr := First_Discriminant (Full_Init_Type);
1557 while Present (Discr) loop
1559 -- If this is a discriminated concurrent type, the init_proc
1560 -- for the corresponding record is being called. Use that type
1561 -- directly to find the discriminant value, to handle properly
1562 -- intervening renamed discriminants.
1564 declare
1565 T : Entity_Id := Full_Type;
1567 begin
1568 if Is_Protected_Type (T) then
1569 T := Corresponding_Record_Type (T);
1570 end if;
1572 Arg :=
1573 Get_Discriminant_Value (
1574 Discr,
1576 Discriminant_Constraint (Full_Type));
1577 end;
1579 -- If the target has access discriminants, and is constrained by
1580 -- an access to the enclosing construct, i.e. a current instance,
1581 -- replace the reference to the type by a reference to the object.
1583 if Nkind (Arg) = N_Attribute_Reference
1584 and then Is_Access_Type (Etype (Arg))
1585 and then Is_Entity_Name (Prefix (Arg))
1586 and then Is_Type (Entity (Prefix (Arg)))
1587 then
1588 Arg :=
1589 Make_Attribute_Reference (Loc,
1590 Prefix => New_Copy (Prefix (Id_Ref)),
1591 Attribute_Name => Name_Unrestricted_Access);
1593 elsif In_Init_Proc then
1595 -- Replace any possible references to the discriminant in the
1596 -- call to the record initialization procedure with references
1597 -- to the appropriate formal parameter.
1599 if Nkind (Arg) = N_Identifier
1600 and then Ekind (Entity (Arg)) = E_Discriminant
1601 then
1602 Arg := New_Occurrence_Of (Discriminal (Entity (Arg)), Loc);
1604 -- Otherwise make a copy of the default expression. Note that
1605 -- we use the current Sloc for this, because we do not want the
1606 -- call to appear to be at the declaration point. Within the
1607 -- expression, replace discriminants with their discriminals.
1609 else
1610 Arg :=
1611 New_Copy_Tree (Arg, Map => Discr_Map, New_Sloc => Loc);
1612 end if;
1614 else
1615 if Is_Constrained (Full_Type) then
1616 Arg := Duplicate_Subexpr_No_Checks (Arg);
1617 else
1618 -- The constraints come from the discriminant default exps,
1619 -- they must be reevaluated, so we use New_Copy_Tree but we
1620 -- ensure the proper Sloc (for any embedded calls).
1621 -- In addition, if a predicate check is needed on the value
1622 -- of the discriminant, insert it ahead of the call.
1624 Arg := New_Copy_Tree (Arg, New_Sloc => Loc);
1625 end if;
1627 if Has_Predicates (Etype (Discr)) then
1628 Check_Predicated_Discriminant (Arg, Discr);
1629 end if;
1630 end if;
1632 -- Ada 2005 (AI-287): In case of default initialized components,
1633 -- if the component is constrained with a discriminant of the
1634 -- enclosing type, we need to generate the corresponding selected
1635 -- component node to access the discriminant value. In other cases
1636 -- this is not required, either because we are inside the init
1637 -- proc and we use the corresponding formal, or else because the
1638 -- component is constrained by an expression.
1640 if With_Default_Init
1641 and then Nkind (Id_Ref) = N_Selected_Component
1642 and then Nkind (Arg) = N_Identifier
1643 and then Ekind (Entity (Arg)) = E_Discriminant
1644 then
1645 Append_To (Args,
1646 Make_Selected_Component (Loc,
1647 Prefix => New_Copy_Tree (Prefix (Id_Ref)),
1648 Selector_Name => Arg));
1649 else
1650 Append_To (Args, Arg);
1651 end if;
1653 Next_Discriminant (Discr);
1654 end loop;
1655 end if;
1657 -- If this is a call to initialize the parent component of a derived
1658 -- tagged type, indicate that the tag should not be set in the parent.
1660 if Is_Tagged_Type (Full_Init_Type)
1661 and then not Is_CPP_Class (Full_Init_Type)
1662 and then Nkind (Id_Ref) = N_Selected_Component
1663 and then Chars (Selector_Name (Id_Ref)) = Name_uParent
1664 then
1665 Append_To (Args, New_Occurrence_Of (Standard_False, Loc));
1667 elsif Present (Constructor_Ref) then
1668 Append_List_To (Args,
1669 New_Copy_List (Parameter_Associations (Constructor_Ref)));
1670 end if;
1672 Append_To (Res,
1673 Make_Procedure_Call_Statement (Loc,
1674 Name => New_Occurrence_Of (Proc, Loc),
1675 Parameter_Associations => Args));
1677 if Needs_Finalization (Typ)
1678 and then Nkind (Id_Ref) = N_Selected_Component
1679 then
1680 if Chars (Selector_Name (Id_Ref)) /= Name_uParent then
1681 Init_Call :=
1682 Make_Init_Call
1683 (Obj_Ref => New_Copy_Tree (First_Arg),
1684 Typ => Typ);
1686 -- Guard against a missing [Deep_]Initialize when the type was not
1687 -- properly frozen.
1689 if Present (Init_Call) then
1690 Append_To (Res, Init_Call);
1691 end if;
1692 end if;
1693 end if;
1695 return Res;
1697 exception
1698 when RE_Not_Available =>
1699 return Empty_List;
1700 end Build_Initialization_Call;
1702 ----------------------------
1703 -- Build_Record_Init_Proc --
1704 ----------------------------
1706 procedure Build_Record_Init_Proc (N : Node_Id; Rec_Ent : Entity_Id) is
1707 Decls : constant List_Id := New_List;
1708 Discr_Map : constant Elist_Id := New_Elmt_List;
1709 Loc : constant Source_Ptr := Sloc (Rec_Ent);
1710 Counter : Nat := 0;
1711 Proc_Id : Entity_Id;
1712 Rec_Type : Entity_Id;
1713 Set_Tag : Entity_Id := Empty;
1715 function Build_Assignment
1716 (Id : Entity_Id;
1717 Default : Node_Id) return List_Id;
1718 -- Build an assignment statement that assigns the default expression to
1719 -- its corresponding record component if defined. The left-hand side of
1720 -- the assignment is marked Assignment_OK so that initialization of
1721 -- limited private records works correctly. This routine may also build
1722 -- an adjustment call if the component is controlled.
1724 procedure Build_Discriminant_Assignments (Statement_List : List_Id);
1725 -- If the record has discriminants, add assignment statements to
1726 -- Statement_List to initialize the discriminant values from the
1727 -- arguments of the initialization procedure.
1729 function Build_Init_Statements (Comp_List : Node_Id) return List_Id;
1730 -- Build a list representing a sequence of statements which initialize
1731 -- components of the given component list. This may involve building
1732 -- case statements for the variant parts. Append any locally declared
1733 -- objects on list Decls.
1735 function Build_Init_Call_Thru (Parameters : List_Id) return List_Id;
1736 -- Given an untagged type-derivation that declares discriminants, e.g.
1738 -- type R (R1, R2 : Integer) is record ... end record;
1739 -- type D (D1 : Integer) is new R (1, D1);
1741 -- we make the _init_proc of D be
1743 -- procedure _init_proc (X : D; D1 : Integer) is
1744 -- begin
1745 -- _init_proc (R (X), 1, D1);
1746 -- end _init_proc;
1748 -- This function builds the call statement in this _init_proc.
1750 procedure Build_CPP_Init_Procedure;
1751 -- Build the tree corresponding to the procedure specification and body
1752 -- of the IC procedure that initializes the C++ part of the dispatch
1753 -- table of an Ada tagged type that is a derivation of a CPP type.
1754 -- Install it as the CPP_Init TSS.
1756 procedure Build_Init_Procedure;
1757 -- Build the tree corresponding to the procedure specification and body
1758 -- of the initialization procedure and install it as the _init TSS.
1760 procedure Build_Offset_To_Top_Functions;
1761 -- Ada 2005 (AI-251): Build the tree corresponding to the procedure spec
1762 -- and body of Offset_To_Top, a function used in conjuction with types
1763 -- having secondary dispatch tables.
1765 procedure Build_Record_Checks (S : Node_Id; Check_List : List_Id);
1766 -- Add range checks to components of discriminated records. S is a
1767 -- subtype indication of a record component. Check_List is a list
1768 -- to which the check actions are appended.
1770 function Component_Needs_Simple_Initialization
1771 (T : Entity_Id) return Boolean;
1772 -- Determine if a component needs simple initialization, given its type
1773 -- T. This routine is the same as Needs_Simple_Initialization except for
1774 -- components of type Tag and Interface_Tag. These two access types do
1775 -- not require initialization since they are explicitly initialized by
1776 -- other means.
1778 function Parent_Subtype_Renaming_Discrims return Boolean;
1779 -- Returns True for base types N that rename discriminants, else False
1781 function Requires_Init_Proc (Rec_Id : Entity_Id) return Boolean;
1782 -- Determine whether a record initialization procedure needs to be
1783 -- generated for the given record type.
1785 ----------------------
1786 -- Build_Assignment --
1787 ----------------------
1789 function Build_Assignment
1790 (Id : Entity_Id;
1791 Default : Node_Id) return List_Id
1793 Default_Loc : constant Source_Ptr := Sloc (Default);
1794 Typ : constant Entity_Id := Underlying_Type (Etype (Id));
1796 Adj_Call : Node_Id;
1797 Exp : Node_Id := Default;
1798 Kind : Node_Kind := Nkind (Default);
1799 Lhs : Node_Id;
1800 Res : List_Id;
1802 function Replace_Discr_Ref (N : Node_Id) return Traverse_Result;
1803 -- Analysis of the aggregate has replaced discriminants by their
1804 -- corresponding discriminals, but these are irrelevant when the
1805 -- component has a mutable type and is initialized with an aggregate.
1806 -- Instead, they must be replaced by the values supplied in the
1807 -- aggregate, that will be assigned during the expansion of the
1808 -- assignment.
1810 -----------------------
1811 -- Replace_Discr_Ref --
1812 -----------------------
1814 function Replace_Discr_Ref (N : Node_Id) return Traverse_Result is
1815 Val : Node_Id;
1817 begin
1818 if Is_Entity_Name (N)
1819 and then Present (Entity (N))
1820 and then Is_Formal (Entity (N))
1821 and then Present (Discriminal_Link (Entity (N)))
1822 then
1823 Val :=
1824 Make_Selected_Component (Default_Loc,
1825 Prefix => New_Copy_Tree (Lhs),
1826 Selector_Name =>
1827 New_Occurrence_Of
1828 (Discriminal_Link (Entity (N)), Default_Loc));
1830 if Present (Val) then
1831 Rewrite (N, New_Copy_Tree (Val));
1832 end if;
1833 end if;
1835 return OK;
1836 end Replace_Discr_Ref;
1838 procedure Replace_Discriminant_References is
1839 new Traverse_Proc (Replace_Discr_Ref);
1841 -- Start of processing for Build_Assignment
1843 begin
1844 Lhs :=
1845 Make_Selected_Component (Default_Loc,
1846 Prefix => Make_Identifier (Loc, Name_uInit),
1847 Selector_Name => New_Occurrence_Of (Id, Default_Loc));
1848 Set_Assignment_OK (Lhs);
1850 if Nkind (Exp) = N_Aggregate
1851 and then Has_Discriminants (Typ)
1852 and then not Is_Constrained (Base_Type (Typ))
1853 then
1854 -- The aggregate may provide new values for the discriminants
1855 -- of the component, and other components may depend on those
1856 -- discriminants. Previous analysis of those expressions have
1857 -- replaced the discriminants by the formals of the initialization
1858 -- procedure for the type, but these are irrelevant in the
1859 -- enclosing initialization procedure: those discriminant
1860 -- references must be replaced by the values provided in the
1861 -- aggregate.
1863 Replace_Discriminant_References (Exp);
1864 end if;
1866 -- Case of an access attribute applied to the current instance.
1867 -- Replace the reference to the type by a reference to the actual
1868 -- object. (Note that this handles the case of the top level of
1869 -- the expression being given by such an attribute, but does not
1870 -- cover uses nested within an initial value expression. Nested
1871 -- uses are unlikely to occur in practice, but are theoretically
1872 -- possible.) It is not clear how to handle them without fully
1873 -- traversing the expression. ???
1875 if Kind = N_Attribute_Reference
1876 and then Nam_In (Attribute_Name (Default), Name_Unchecked_Access,
1877 Name_Unrestricted_Access)
1878 and then Is_Entity_Name (Prefix (Default))
1879 and then Is_Type (Entity (Prefix (Default)))
1880 and then Entity (Prefix (Default)) = Rec_Type
1881 then
1882 Exp :=
1883 Make_Attribute_Reference (Default_Loc,
1884 Prefix =>
1885 Make_Identifier (Default_Loc, Name_uInit),
1886 Attribute_Name => Name_Unrestricted_Access);
1887 end if;
1889 -- Take a copy of Exp to ensure that later copies of this component
1890 -- declaration in derived types see the original tree, not a node
1891 -- rewritten during expansion of the init_proc. If the copy contains
1892 -- itypes, the scope of the new itypes is the init_proc being built.
1894 Exp := New_Copy_Tree (Exp, New_Scope => Proc_Id);
1896 Res := New_List (
1897 Make_Assignment_Statement (Loc,
1898 Name => Lhs,
1899 Expression => Exp));
1901 Set_No_Ctrl_Actions (First (Res));
1903 -- Adjust the tag if tagged (because of possible view conversions).
1904 -- Suppress the tag adjustment when not Tagged_Type_Expansion because
1905 -- tags are represented implicitly in objects.
1907 if Is_Tagged_Type (Typ) and then Tagged_Type_Expansion then
1908 Append_To (Res,
1909 Make_Assignment_Statement (Default_Loc,
1910 Name =>
1911 Make_Selected_Component (Default_Loc,
1912 Prefix =>
1913 New_Copy_Tree (Lhs, New_Scope => Proc_Id),
1914 Selector_Name =>
1915 New_Occurrence_Of
1916 (First_Tag_Component (Typ), Default_Loc)),
1918 Expression =>
1919 Unchecked_Convert_To (RTE (RE_Tag),
1920 New_Occurrence_Of
1921 (Node (First_Elmt (Access_Disp_Table (Underlying_Type
1922 (Typ)))),
1923 Default_Loc))));
1924 end if;
1926 -- Adjust the component if controlled except if it is an aggregate
1927 -- that will be expanded inline.
1929 if Kind = N_Qualified_Expression then
1930 Kind := Nkind (Expression (Default));
1931 end if;
1933 if Needs_Finalization (Typ)
1934 and then not (Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate))
1935 and then not Is_Build_In_Place_Function_Call (Exp)
1936 then
1937 Adj_Call :=
1938 Make_Adjust_Call
1939 (Obj_Ref => New_Copy_Tree (Lhs),
1940 Typ => Etype (Id));
1942 -- Guard against a missing [Deep_]Adjust when the component type
1943 -- was not properly frozen.
1945 if Present (Adj_Call) then
1946 Append_To (Res, Adj_Call);
1947 end if;
1948 end if;
1950 -- If a component type has a predicate, add check to the component
1951 -- assignment. Discriminants are handled at the point of the call,
1952 -- which provides for a better error message.
1954 if Comes_From_Source (Exp)
1955 and then Has_Predicates (Typ)
1956 and then not Predicate_Checks_Suppressed (Empty)
1957 and then not Predicates_Ignored (Typ)
1958 then
1959 Append (Make_Predicate_Check (Typ, Exp), Res);
1960 end if;
1962 return Res;
1964 exception
1965 when RE_Not_Available =>
1966 return Empty_List;
1967 end Build_Assignment;
1969 ------------------------------------
1970 -- Build_Discriminant_Assignments --
1971 ------------------------------------
1973 procedure Build_Discriminant_Assignments (Statement_List : List_Id) is
1974 Is_Tagged : constant Boolean := Is_Tagged_Type (Rec_Type);
1975 D : Entity_Id;
1976 D_Loc : Source_Ptr;
1978 begin
1979 if Has_Discriminants (Rec_Type)
1980 and then not Is_Unchecked_Union (Rec_Type)
1981 then
1982 D := First_Discriminant (Rec_Type);
1983 while Present (D) loop
1985 -- Don't generate the assignment for discriminants in derived
1986 -- tagged types if the discriminant is a renaming of some
1987 -- ancestor discriminant. This initialization will be done
1988 -- when initializing the _parent field of the derived record.
1990 if Is_Tagged
1991 and then Present (Corresponding_Discriminant (D))
1992 then
1993 null;
1995 else
1996 D_Loc := Sloc (D);
1997 Append_List_To (Statement_List,
1998 Build_Assignment (D,
1999 New_Occurrence_Of (Discriminal (D), D_Loc)));
2000 end if;
2002 Next_Discriminant (D);
2003 end loop;
2004 end if;
2005 end Build_Discriminant_Assignments;
2007 --------------------------
2008 -- Build_Init_Call_Thru --
2009 --------------------------
2011 function Build_Init_Call_Thru (Parameters : List_Id) return List_Id is
2012 Parent_Proc : constant Entity_Id :=
2013 Base_Init_Proc (Etype (Rec_Type));
2015 Parent_Type : constant Entity_Id :=
2016 Etype (First_Formal (Parent_Proc));
2018 Uparent_Type : constant Entity_Id :=
2019 Underlying_Type (Parent_Type);
2021 First_Discr_Param : Node_Id;
2023 Arg : Node_Id;
2024 Args : List_Id;
2025 First_Arg : Node_Id;
2026 Parent_Discr : Entity_Id;
2027 Res : List_Id;
2029 begin
2030 -- First argument (_Init) is the object to be initialized.
2031 -- ??? not sure where to get a reasonable Loc for First_Arg
2033 First_Arg :=
2034 OK_Convert_To (Parent_Type,
2035 New_Occurrence_Of
2036 (Defining_Identifier (First (Parameters)), Loc));
2038 Set_Etype (First_Arg, Parent_Type);
2040 Args := New_List (Convert_Concurrent (First_Arg, Rec_Type));
2042 -- In the tasks case,
2043 -- add _Master as the value of the _Master parameter
2044 -- add _Chain as the value of the _Chain parameter.
2045 -- add _Task_Name as the value of the _Task_Name parameter.
2046 -- At the outer level, these will be variables holding the
2047 -- corresponding values obtained from GNARL or the expander.
2049 -- At inner levels, they will be the parameters passed down through
2050 -- the outer routines.
2052 First_Discr_Param := Next (First (Parameters));
2054 if Has_Task (Rec_Type) then
2055 if Restriction_Active (No_Task_Hierarchy) then
2056 Append_To (Args,
2057 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
2058 else
2059 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
2060 end if;
2062 -- Add _Chain (not done for sequential elaboration policy, see
2063 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
2065 if Partition_Elaboration_Policy /= 'S' then
2066 Append_To (Args, Make_Identifier (Loc, Name_uChain));
2067 end if;
2069 Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
2070 First_Discr_Param := Next (Next (Next (First_Discr_Param)));
2071 end if;
2073 -- Append discriminant values
2075 if Has_Discriminants (Uparent_Type) then
2076 pragma Assert (not Is_Tagged_Type (Uparent_Type));
2078 Parent_Discr := First_Discriminant (Uparent_Type);
2079 while Present (Parent_Discr) loop
2081 -- Get the initial value for this discriminant
2082 -- ??? needs to be cleaned up to use parent_Discr_Constr
2083 -- directly.
2085 declare
2086 Discr : Entity_Id :=
2087 First_Stored_Discriminant (Uparent_Type);
2089 Discr_Value : Elmt_Id :=
2090 First_Elmt (Stored_Constraint (Rec_Type));
2092 begin
2093 while Original_Record_Component (Parent_Discr) /= Discr loop
2094 Next_Stored_Discriminant (Discr);
2095 Next_Elmt (Discr_Value);
2096 end loop;
2098 Arg := Node (Discr_Value);
2099 end;
2101 -- Append it to the list
2103 if Nkind (Arg) = N_Identifier
2104 and then Ekind (Entity (Arg)) = E_Discriminant
2105 then
2106 Append_To (Args,
2107 New_Occurrence_Of (Discriminal (Entity (Arg)), Loc));
2109 -- Case of access discriminants. We replace the reference
2110 -- to the type by a reference to the actual object.
2112 -- Is above comment right??? Use of New_Copy below seems mighty
2113 -- suspicious ???
2115 else
2116 Append_To (Args, New_Copy (Arg));
2117 end if;
2119 Next_Discriminant (Parent_Discr);
2120 end loop;
2121 end if;
2123 Res :=
2124 New_List (
2125 Make_Procedure_Call_Statement (Loc,
2126 Name =>
2127 New_Occurrence_Of (Parent_Proc, Loc),
2128 Parameter_Associations => Args));
2130 return Res;
2131 end Build_Init_Call_Thru;
2133 -----------------------------------
2134 -- Build_Offset_To_Top_Functions --
2135 -----------------------------------
2137 procedure Build_Offset_To_Top_Functions is
2139 procedure Build_Offset_To_Top_Function (Iface_Comp : Entity_Id);
2140 -- Generate:
2141 -- function Fxx (O : Address) return Storage_Offset is
2142 -- type Acc is access all <Typ>;
2143 -- begin
2144 -- return Acc!(O).Iface_Comp'Position;
2145 -- end Fxx;
2147 ----------------------------------
2148 -- Build_Offset_To_Top_Function --
2149 ----------------------------------
2151 procedure Build_Offset_To_Top_Function (Iface_Comp : Entity_Id) is
2152 Body_Node : Node_Id;
2153 Func_Id : Entity_Id;
2154 Spec_Node : Node_Id;
2155 Acc_Type : Entity_Id;
2157 begin
2158 Func_Id := Make_Temporary (Loc, 'F');
2159 Set_DT_Offset_To_Top_Func (Iface_Comp, Func_Id);
2161 -- Generate
2162 -- function Fxx (O : in Rec_Typ) return Storage_Offset;
2164 Spec_Node := New_Node (N_Function_Specification, Loc);
2165 Set_Defining_Unit_Name (Spec_Node, Func_Id);
2166 Set_Parameter_Specifications (Spec_Node, New_List (
2167 Make_Parameter_Specification (Loc,
2168 Defining_Identifier =>
2169 Make_Defining_Identifier (Loc, Name_uO),
2170 In_Present => True,
2171 Parameter_Type =>
2172 New_Occurrence_Of (RTE (RE_Address), Loc))));
2173 Set_Result_Definition (Spec_Node,
2174 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
2176 -- Generate
2177 -- function Fxx (O : in Rec_Typ) return Storage_Offset is
2178 -- begin
2179 -- return -O.Iface_Comp'Position;
2180 -- end Fxx;
2182 Body_Node := New_Node (N_Subprogram_Body, Loc);
2183 Set_Specification (Body_Node, Spec_Node);
2185 Acc_Type := Make_Temporary (Loc, 'T');
2186 Set_Declarations (Body_Node, New_List (
2187 Make_Full_Type_Declaration (Loc,
2188 Defining_Identifier => Acc_Type,
2189 Type_Definition =>
2190 Make_Access_To_Object_Definition (Loc,
2191 All_Present => True,
2192 Null_Exclusion_Present => False,
2193 Constant_Present => False,
2194 Subtype_Indication =>
2195 New_Occurrence_Of (Rec_Type, Loc)))));
2197 Set_Handled_Statement_Sequence (Body_Node,
2198 Make_Handled_Sequence_Of_Statements (Loc,
2199 Statements => New_List (
2200 Make_Simple_Return_Statement (Loc,
2201 Expression =>
2202 Make_Op_Minus (Loc,
2203 Make_Attribute_Reference (Loc,
2204 Prefix =>
2205 Make_Selected_Component (Loc,
2206 Prefix =>
2207 Unchecked_Convert_To (Acc_Type,
2208 Make_Identifier (Loc, Name_uO)),
2209 Selector_Name =>
2210 New_Occurrence_Of (Iface_Comp, Loc)),
2211 Attribute_Name => Name_Position))))));
2213 Set_Ekind (Func_Id, E_Function);
2214 Set_Mechanism (Func_Id, Default_Mechanism);
2215 Set_Is_Internal (Func_Id, True);
2217 if not Debug_Generated_Code then
2218 Set_Debug_Info_Off (Func_Id);
2219 end if;
2221 Analyze (Body_Node);
2223 Append_Freeze_Action (Rec_Type, Body_Node);
2224 end Build_Offset_To_Top_Function;
2226 -- Local variables
2228 Iface_Comp : Node_Id;
2229 Iface_Comp_Elmt : Elmt_Id;
2230 Ifaces_Comp_List : Elist_Id;
2232 -- Start of processing for Build_Offset_To_Top_Functions
2234 begin
2235 -- Offset_To_Top_Functions are built only for derivations of types
2236 -- with discriminants that cover interface types.
2237 -- Nothing is needed either in case of virtual targets, since
2238 -- interfaces are handled directly by the target.
2240 if not Is_Tagged_Type (Rec_Type)
2241 or else Etype (Rec_Type) = Rec_Type
2242 or else not Has_Discriminants (Etype (Rec_Type))
2243 or else not Tagged_Type_Expansion
2244 then
2245 return;
2246 end if;
2248 Collect_Interface_Components (Rec_Type, Ifaces_Comp_List);
2250 -- For each interface type with secondary dispatch table we generate
2251 -- the Offset_To_Top_Functions (required to displace the pointer in
2252 -- interface conversions)
2254 Iface_Comp_Elmt := First_Elmt (Ifaces_Comp_List);
2255 while Present (Iface_Comp_Elmt) loop
2256 Iface_Comp := Node (Iface_Comp_Elmt);
2257 pragma Assert (Is_Interface (Related_Type (Iface_Comp)));
2259 -- If the interface is a parent of Rec_Type it shares the primary
2260 -- dispatch table and hence there is no need to build the function
2262 if not Is_Ancestor (Related_Type (Iface_Comp), Rec_Type,
2263 Use_Full_View => True)
2264 then
2265 Build_Offset_To_Top_Function (Iface_Comp);
2266 end if;
2268 Next_Elmt (Iface_Comp_Elmt);
2269 end loop;
2270 end Build_Offset_To_Top_Functions;
2272 ------------------------------
2273 -- Build_CPP_Init_Procedure --
2274 ------------------------------
2276 procedure Build_CPP_Init_Procedure is
2277 Body_Node : Node_Id;
2278 Body_Stmts : List_Id;
2279 Flag_Id : Entity_Id;
2280 Handled_Stmt_Node : Node_Id;
2281 Init_Tags_List : List_Id;
2282 Proc_Id : Entity_Id;
2283 Proc_Spec_Node : Node_Id;
2285 begin
2286 -- Check cases requiring no IC routine
2288 if not Is_CPP_Class (Root_Type (Rec_Type))
2289 or else Is_CPP_Class (Rec_Type)
2290 or else CPP_Num_Prims (Rec_Type) = 0
2291 or else not Tagged_Type_Expansion
2292 or else No_Run_Time_Mode
2293 then
2294 return;
2295 end if;
2297 -- Generate:
2299 -- Flag : Boolean := False;
2301 -- procedure Typ_IC is
2302 -- begin
2303 -- if not Flag then
2304 -- Copy C++ dispatch table slots from parent
2305 -- Update C++ slots of overridden primitives
2306 -- end if;
2307 -- end;
2309 Flag_Id := Make_Temporary (Loc, 'F');
2311 Append_Freeze_Action (Rec_Type,
2312 Make_Object_Declaration (Loc,
2313 Defining_Identifier => Flag_Id,
2314 Object_Definition =>
2315 New_Occurrence_Of (Standard_Boolean, Loc),
2316 Expression =>
2317 New_Occurrence_Of (Standard_True, Loc)));
2319 Body_Stmts := New_List;
2320 Body_Node := New_Node (N_Subprogram_Body, Loc);
2322 Proc_Spec_Node := New_Node (N_Procedure_Specification, Loc);
2324 Proc_Id :=
2325 Make_Defining_Identifier (Loc,
2326 Chars => Make_TSS_Name (Rec_Type, TSS_CPP_Init_Proc));
2328 Set_Ekind (Proc_Id, E_Procedure);
2329 Set_Is_Internal (Proc_Id);
2331 Set_Defining_Unit_Name (Proc_Spec_Node, Proc_Id);
2333 Set_Parameter_Specifications (Proc_Spec_Node, New_List);
2334 Set_Specification (Body_Node, Proc_Spec_Node);
2335 Set_Declarations (Body_Node, New_List);
2337 Init_Tags_List := Build_Inherit_CPP_Prims (Rec_Type);
2339 Append_To (Init_Tags_List,
2340 Make_Assignment_Statement (Loc,
2341 Name =>
2342 New_Occurrence_Of (Flag_Id, Loc),
2343 Expression =>
2344 New_Occurrence_Of (Standard_False, Loc)));
2346 Append_To (Body_Stmts,
2347 Make_If_Statement (Loc,
2348 Condition => New_Occurrence_Of (Flag_Id, Loc),
2349 Then_Statements => Init_Tags_List));
2351 Handled_Stmt_Node :=
2352 New_Node (N_Handled_Sequence_Of_Statements, Loc);
2353 Set_Statements (Handled_Stmt_Node, Body_Stmts);
2354 Set_Exception_Handlers (Handled_Stmt_Node, No_List);
2355 Set_Handled_Statement_Sequence (Body_Node, Handled_Stmt_Node);
2357 if not Debug_Generated_Code then
2358 Set_Debug_Info_Off (Proc_Id);
2359 end if;
2361 -- Associate CPP_Init_Proc with type
2363 Set_Init_Proc (Rec_Type, Proc_Id);
2364 end Build_CPP_Init_Procedure;
2366 --------------------------
2367 -- Build_Init_Procedure --
2368 --------------------------
2370 procedure Build_Init_Procedure is
2371 Body_Stmts : List_Id;
2372 Body_Node : Node_Id;
2373 Handled_Stmt_Node : Node_Id;
2374 Init_Tags_List : List_Id;
2375 Parameters : List_Id;
2376 Proc_Spec_Node : Node_Id;
2377 Record_Extension_Node : Node_Id;
2379 begin
2380 Body_Stmts := New_List;
2381 Body_Node := New_Node (N_Subprogram_Body, Loc);
2382 Set_Ekind (Proc_Id, E_Procedure);
2384 Proc_Spec_Node := New_Node (N_Procedure_Specification, Loc);
2385 Set_Defining_Unit_Name (Proc_Spec_Node, Proc_Id);
2387 Parameters := Init_Formals (Rec_Type);
2388 Append_List_To (Parameters,
2389 Build_Discriminant_Formals (Rec_Type, True));
2391 -- For tagged types, we add a flag to indicate whether the routine
2392 -- is called to initialize a parent component in the init_proc of
2393 -- a type extension. If the flag is false, we do not set the tag
2394 -- because it has been set already in the extension.
2396 if Is_Tagged_Type (Rec_Type) then
2397 Set_Tag := Make_Temporary (Loc, 'P');
2399 Append_To (Parameters,
2400 Make_Parameter_Specification (Loc,
2401 Defining_Identifier => Set_Tag,
2402 Parameter_Type =>
2403 New_Occurrence_Of (Standard_Boolean, Loc),
2404 Expression =>
2405 New_Occurrence_Of (Standard_True, Loc)));
2406 end if;
2408 Set_Parameter_Specifications (Proc_Spec_Node, Parameters);
2409 Set_Specification (Body_Node, Proc_Spec_Node);
2410 Set_Declarations (Body_Node, Decls);
2412 -- N is a Derived_Type_Definition that renames the parameters of the
2413 -- ancestor type. We initialize it by expanding our discriminants and
2414 -- call the ancestor _init_proc with a type-converted object.
2416 if Parent_Subtype_Renaming_Discrims then
2417 Append_List_To (Body_Stmts, Build_Init_Call_Thru (Parameters));
2419 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
2420 Build_Discriminant_Assignments (Body_Stmts);
2422 if not Null_Present (Type_Definition (N)) then
2423 Append_List_To (Body_Stmts,
2424 Build_Init_Statements (Component_List (Type_Definition (N))));
2425 end if;
2427 -- N is a Derived_Type_Definition with a possible non-empty
2428 -- extension. The initialization of a type extension consists in the
2429 -- initialization of the components in the extension.
2431 else
2432 Build_Discriminant_Assignments (Body_Stmts);
2434 Record_Extension_Node :=
2435 Record_Extension_Part (Type_Definition (N));
2437 if not Null_Present (Record_Extension_Node) then
2438 declare
2439 Stmts : constant List_Id :=
2440 Build_Init_Statements (
2441 Component_List (Record_Extension_Node));
2443 begin
2444 -- The parent field must be initialized first because the
2445 -- offset of the new discriminants may depend on it. This is
2446 -- not needed if the parent is an interface type because in
2447 -- such case the initialization of the _parent field was not
2448 -- generated.
2450 if not Is_Interface (Etype (Rec_Ent)) then
2451 declare
2452 Parent_IP : constant Name_Id :=
2453 Make_Init_Proc_Name (Etype (Rec_Ent));
2454 Stmt : Node_Id;
2455 IP_Call : Node_Id;
2456 IP_Stmts : List_Id;
2458 begin
2459 -- Look for a call to the parent IP at the beginning
2460 -- of Stmts associated with the record extension
2462 Stmt := First (Stmts);
2463 IP_Call := Empty;
2464 while Present (Stmt) loop
2465 if Nkind (Stmt) = N_Procedure_Call_Statement
2466 and then Chars (Name (Stmt)) = Parent_IP
2467 then
2468 IP_Call := Stmt;
2469 exit;
2470 end if;
2472 Next (Stmt);
2473 end loop;
2475 -- If found then move it to the beginning of the
2476 -- statements of this IP routine
2478 if Present (IP_Call) then
2479 IP_Stmts := New_List;
2480 loop
2481 Stmt := Remove_Head (Stmts);
2482 Append_To (IP_Stmts, Stmt);
2483 exit when Stmt = IP_Call;
2484 end loop;
2486 Prepend_List_To (Body_Stmts, IP_Stmts);
2487 end if;
2488 end;
2489 end if;
2491 Append_List_To (Body_Stmts, Stmts);
2492 end;
2493 end if;
2494 end if;
2496 -- Add here the assignment to instantiate the Tag
2498 -- The assignment corresponds to the code:
2500 -- _Init._Tag := Typ'Tag;
2502 -- Suppress the tag assignment when not Tagged_Type_Expansion because
2503 -- tags are represented implicitly in objects. It is also suppressed
2504 -- in case of CPP_Class types because in this case the tag is
2505 -- initialized in the C++ side.
2507 if Is_Tagged_Type (Rec_Type)
2508 and then Tagged_Type_Expansion
2509 and then not No_Run_Time_Mode
2510 then
2511 -- Case 1: Ada tagged types with no CPP ancestor. Set the tags of
2512 -- the actual object and invoke the IP of the parent (in this
2513 -- order). The tag must be initialized before the call to the IP
2514 -- of the parent and the assignments to other components because
2515 -- the initial value of the components may depend on the tag (eg.
2516 -- through a dispatching operation on an access to the current
2517 -- type). The tag assignment is not done when initializing the
2518 -- parent component of a type extension, because in that case the
2519 -- tag is set in the extension.
2521 if not Is_CPP_Class (Root_Type (Rec_Type)) then
2523 -- Initialize the primary tag component
2525 Init_Tags_List := New_List (
2526 Make_Assignment_Statement (Loc,
2527 Name =>
2528 Make_Selected_Component (Loc,
2529 Prefix => Make_Identifier (Loc, Name_uInit),
2530 Selector_Name =>
2531 New_Occurrence_Of
2532 (First_Tag_Component (Rec_Type), Loc)),
2533 Expression =>
2534 New_Occurrence_Of
2535 (Node
2536 (First_Elmt (Access_Disp_Table (Rec_Type))), Loc)));
2538 -- Ada 2005 (AI-251): Initialize the secondary tags components
2539 -- located at fixed positions (tags whose position depends on
2540 -- variable size components are initialized later ---see below)
2542 if Ada_Version >= Ada_2005
2543 and then not Is_Interface (Rec_Type)
2544 and then Has_Interfaces (Rec_Type)
2545 then
2546 declare
2547 Elab_Sec_DT_Stmts_List : constant List_Id := New_List;
2548 Elab_List : List_Id := New_List;
2550 begin
2551 Init_Secondary_Tags
2552 (Typ => Rec_Type,
2553 Target => Make_Identifier (Loc, Name_uInit),
2554 Init_Tags_List => Init_Tags_List,
2555 Stmts_List => Elab_Sec_DT_Stmts_List,
2556 Fixed_Comps => True,
2557 Variable_Comps => False);
2559 Elab_List := New_List (
2560 Make_If_Statement (Loc,
2561 Condition => New_Occurrence_Of (Set_Tag, Loc),
2562 Then_Statements => Init_Tags_List));
2564 if Elab_Flag_Needed (Rec_Type) then
2565 Append_To (Elab_Sec_DT_Stmts_List,
2566 Make_Assignment_Statement (Loc,
2567 Name =>
2568 New_Occurrence_Of
2569 (Access_Disp_Table_Elab_Flag (Rec_Type),
2570 Loc),
2571 Expression =>
2572 New_Occurrence_Of (Standard_False, Loc)));
2574 Append_To (Elab_List,
2575 Make_If_Statement (Loc,
2576 Condition =>
2577 New_Occurrence_Of
2578 (Access_Disp_Table_Elab_Flag (Rec_Type), Loc),
2579 Then_Statements => Elab_Sec_DT_Stmts_List));
2580 end if;
2582 Prepend_List_To (Body_Stmts, Elab_List);
2583 end;
2584 else
2585 Prepend_To (Body_Stmts,
2586 Make_If_Statement (Loc,
2587 Condition => New_Occurrence_Of (Set_Tag, Loc),
2588 Then_Statements => Init_Tags_List));
2589 end if;
2591 -- Case 2: CPP type. The imported C++ constructor takes care of
2592 -- tags initialization. No action needed here because the IP
2593 -- is built by Set_CPP_Constructors; in this case the IP is a
2594 -- wrapper that invokes the C++ constructor and copies the C++
2595 -- tags locally. Done to inherit the C++ slots in Ada derivations
2596 -- (see case 3).
2598 elsif Is_CPP_Class (Rec_Type) then
2599 pragma Assert (False);
2600 null;
2602 -- Case 3: Combined hierarchy containing C++ types and Ada tagged
2603 -- type derivations. Derivations of imported C++ classes add a
2604 -- complication, because we cannot inhibit tag setting in the
2605 -- constructor for the parent. Hence we initialize the tag after
2606 -- the call to the parent IP (that is, in reverse order compared
2607 -- with pure Ada hierarchies ---see comment on case 1).
2609 else
2610 -- Initialize the primary tag
2612 Init_Tags_List := New_List (
2613 Make_Assignment_Statement (Loc,
2614 Name =>
2615 Make_Selected_Component (Loc,
2616 Prefix => Make_Identifier (Loc, Name_uInit),
2617 Selector_Name =>
2618 New_Occurrence_Of
2619 (First_Tag_Component (Rec_Type), Loc)),
2620 Expression =>
2621 New_Occurrence_Of
2622 (Node
2623 (First_Elmt (Access_Disp_Table (Rec_Type))), Loc)));
2625 -- Ada 2005 (AI-251): Initialize the secondary tags components
2626 -- located at fixed positions (tags whose position depends on
2627 -- variable size components are initialized later ---see below)
2629 if Ada_Version >= Ada_2005
2630 and then not Is_Interface (Rec_Type)
2631 and then Has_Interfaces (Rec_Type)
2632 then
2633 Init_Secondary_Tags
2634 (Typ => Rec_Type,
2635 Target => Make_Identifier (Loc, Name_uInit),
2636 Init_Tags_List => Init_Tags_List,
2637 Stmts_List => Init_Tags_List,
2638 Fixed_Comps => True,
2639 Variable_Comps => False);
2640 end if;
2642 -- Initialize the tag component after invocation of parent IP.
2644 -- Generate:
2645 -- parent_IP(_init.parent); // Invokes the C++ constructor
2646 -- [ typIC; ] // Inherit C++ slots from parent
2647 -- init_tags
2649 declare
2650 Ins_Nod : Node_Id;
2652 begin
2653 -- Search for the call to the IP of the parent. We assume
2654 -- that the first init_proc call is for the parent.
2656 Ins_Nod := First (Body_Stmts);
2657 while Present (Next (Ins_Nod))
2658 and then (Nkind (Ins_Nod) /= N_Procedure_Call_Statement
2659 or else not Is_Init_Proc (Name (Ins_Nod)))
2660 loop
2661 Next (Ins_Nod);
2662 end loop;
2664 -- The IC routine copies the inherited slots of the C+ part
2665 -- of the dispatch table from the parent and updates the
2666 -- overridden C++ slots.
2668 if CPP_Num_Prims (Rec_Type) > 0 then
2669 declare
2670 Init_DT : Entity_Id;
2671 New_Nod : Node_Id;
2673 begin
2674 Init_DT := CPP_Init_Proc (Rec_Type);
2675 pragma Assert (Present (Init_DT));
2677 New_Nod :=
2678 Make_Procedure_Call_Statement (Loc,
2679 New_Occurrence_Of (Init_DT, Loc));
2680 Insert_After (Ins_Nod, New_Nod);
2682 -- Update location of init tag statements
2684 Ins_Nod := New_Nod;
2685 end;
2686 end if;
2688 Insert_List_After (Ins_Nod, Init_Tags_List);
2689 end;
2690 end if;
2692 -- Ada 2005 (AI-251): Initialize the secondary tag components
2693 -- located at variable positions. We delay the generation of this
2694 -- code until here because the value of the attribute 'Position
2695 -- applied to variable size components of the parent type that
2696 -- depend on discriminants is only safely read at runtime after
2697 -- the parent components have been initialized.
2699 if Ada_Version >= Ada_2005
2700 and then not Is_Interface (Rec_Type)
2701 and then Has_Interfaces (Rec_Type)
2702 and then Has_Discriminants (Etype (Rec_Type))
2703 and then Is_Variable_Size_Record (Etype (Rec_Type))
2704 then
2705 Init_Tags_List := New_List;
2707 Init_Secondary_Tags
2708 (Typ => Rec_Type,
2709 Target => Make_Identifier (Loc, Name_uInit),
2710 Init_Tags_List => Init_Tags_List,
2711 Stmts_List => Init_Tags_List,
2712 Fixed_Comps => False,
2713 Variable_Comps => True);
2715 if Is_Non_Empty_List (Init_Tags_List) then
2716 Append_List_To (Body_Stmts, Init_Tags_List);
2717 end if;
2718 end if;
2719 end if;
2721 Handled_Stmt_Node := New_Node (N_Handled_Sequence_Of_Statements, Loc);
2722 Set_Statements (Handled_Stmt_Node, Body_Stmts);
2724 -- Generate:
2725 -- Deep_Finalize (_init, C1, ..., CN);
2726 -- raise;
2728 if Counter > 0
2729 and then Needs_Finalization (Rec_Type)
2730 and then not Is_Abstract_Type (Rec_Type)
2731 and then not Restriction_Active (No_Exception_Propagation)
2732 then
2733 declare
2734 DF_Id : Entity_Id;
2736 begin
2737 -- Create a local version of Deep_Finalize which has indication
2738 -- of partial initialization state.
2740 DF_Id :=
2741 Make_Defining_Identifier (Loc,
2742 Chars => New_External_Name (Name_uFinalizer));
2744 Append_To (Decls, Make_Local_Deep_Finalize (Rec_Type, DF_Id));
2746 Set_Exception_Handlers (Handled_Stmt_Node, New_List (
2747 Make_Exception_Handler (Loc,
2748 Exception_Choices => New_List (
2749 Make_Others_Choice (Loc)),
2750 Statements => New_List (
2751 Make_Procedure_Call_Statement (Loc,
2752 Name =>
2753 New_Occurrence_Of (DF_Id, Loc),
2754 Parameter_Associations => New_List (
2755 Make_Identifier (Loc, Name_uInit),
2756 New_Occurrence_Of (Standard_False, Loc))),
2758 Make_Raise_Statement (Loc)))));
2759 end;
2760 else
2761 Set_Exception_Handlers (Handled_Stmt_Node, No_List);
2762 end if;
2764 Set_Handled_Statement_Sequence (Body_Node, Handled_Stmt_Node);
2766 if not Debug_Generated_Code then
2767 Set_Debug_Info_Off (Proc_Id);
2768 end if;
2770 -- Associate Init_Proc with type, and determine if the procedure
2771 -- is null (happens because of the Initialize_Scalars pragma case,
2772 -- where we have to generate a null procedure in case it is called
2773 -- by a client with Initialize_Scalars set). Such procedures have
2774 -- to be generated, but do not have to be called, so we mark them
2775 -- as null to suppress the call.
2777 Set_Init_Proc (Rec_Type, Proc_Id);
2779 if List_Length (Body_Stmts) = 1
2781 -- We must skip SCIL nodes because they may have been added to this
2782 -- list by Insert_Actions.
2784 and then Nkind (First_Non_SCIL_Node (Body_Stmts)) = N_Null_Statement
2785 then
2786 Set_Is_Null_Init_Proc (Proc_Id);
2787 end if;
2788 end Build_Init_Procedure;
2790 ---------------------------
2791 -- Build_Init_Statements --
2792 ---------------------------
2794 function Build_Init_Statements (Comp_List : Node_Id) return List_Id is
2795 Checks : constant List_Id := New_List;
2796 Actions : List_Id := No_List;
2797 Counter_Id : Entity_Id := Empty;
2798 Comp_Loc : Source_Ptr;
2799 Decl : Node_Id;
2800 Has_POC : Boolean;
2801 Id : Entity_Id;
2802 Parent_Stmts : List_Id;
2803 Stmts : List_Id;
2804 Typ : Entity_Id;
2806 procedure Increment_Counter (Loc : Source_Ptr);
2807 -- Generate an "increment by one" statement for the current counter
2808 -- and append it to the list Stmts.
2810 procedure Make_Counter (Loc : Source_Ptr);
2811 -- Create a new counter for the current component list. The routine
2812 -- creates a new defining Id, adds an object declaration and sets
2813 -- the Id generator for the next variant.
2815 -----------------------
2816 -- Increment_Counter --
2817 -----------------------
2819 procedure Increment_Counter (Loc : Source_Ptr) is
2820 begin
2821 -- Generate:
2822 -- Counter := Counter + 1;
2824 Append_To (Stmts,
2825 Make_Assignment_Statement (Loc,
2826 Name => New_Occurrence_Of (Counter_Id, Loc),
2827 Expression =>
2828 Make_Op_Add (Loc,
2829 Left_Opnd => New_Occurrence_Of (Counter_Id, Loc),
2830 Right_Opnd => Make_Integer_Literal (Loc, 1))));
2831 end Increment_Counter;
2833 ------------------
2834 -- Make_Counter --
2835 ------------------
2837 procedure Make_Counter (Loc : Source_Ptr) is
2838 begin
2839 -- Increment the Id generator
2841 Counter := Counter + 1;
2843 -- Create the entity and declaration
2845 Counter_Id :=
2846 Make_Defining_Identifier (Loc,
2847 Chars => New_External_Name ('C', Counter));
2849 -- Generate:
2850 -- Cnn : Integer := 0;
2852 Append_To (Decls,
2853 Make_Object_Declaration (Loc,
2854 Defining_Identifier => Counter_Id,
2855 Object_Definition =>
2856 New_Occurrence_Of (Standard_Integer, Loc),
2857 Expression =>
2858 Make_Integer_Literal (Loc, 0)));
2859 end Make_Counter;
2861 -- Start of processing for Build_Init_Statements
2863 begin
2864 if Null_Present (Comp_List) then
2865 return New_List (Make_Null_Statement (Loc));
2866 end if;
2868 Parent_Stmts := New_List;
2869 Stmts := New_List;
2871 -- Loop through visible declarations of task types and protected
2872 -- types moving any expanded code from the spec to the body of the
2873 -- init procedure.
2875 if Is_Task_Record_Type (Rec_Type)
2876 or else Is_Protected_Record_Type (Rec_Type)
2877 then
2878 declare
2879 Decl : constant Node_Id :=
2880 Parent (Corresponding_Concurrent_Type (Rec_Type));
2881 Def : Node_Id;
2882 N1 : Node_Id;
2883 N2 : Node_Id;
2885 begin
2886 if Is_Task_Record_Type (Rec_Type) then
2887 Def := Task_Definition (Decl);
2888 else
2889 Def := Protected_Definition (Decl);
2890 end if;
2892 if Present (Def) then
2893 N1 := First (Visible_Declarations (Def));
2894 while Present (N1) loop
2895 N2 := N1;
2896 N1 := Next (N1);
2898 if Nkind (N2) in N_Statement_Other_Than_Procedure_Call
2899 or else Nkind (N2) in N_Raise_xxx_Error
2900 or else Nkind (N2) = N_Procedure_Call_Statement
2901 then
2902 Append_To (Stmts,
2903 New_Copy_Tree (N2, New_Scope => Proc_Id));
2904 Rewrite (N2, Make_Null_Statement (Sloc (N2)));
2905 Analyze (N2);
2906 end if;
2907 end loop;
2908 end if;
2909 end;
2910 end if;
2912 -- Loop through components, skipping pragmas, in 2 steps. The first
2913 -- step deals with regular components. The second step deals with
2914 -- components that have per object constraints and no explicit
2915 -- initialization.
2917 Has_POC := False;
2919 -- First pass : regular components
2921 Decl := First_Non_Pragma (Component_Items (Comp_List));
2922 while Present (Decl) loop
2923 Comp_Loc := Sloc (Decl);
2924 Build_Record_Checks
2925 (Subtype_Indication (Component_Definition (Decl)), Checks);
2927 Id := Defining_Identifier (Decl);
2928 Typ := Etype (Id);
2930 -- Leave any processing of per-object constrained component for
2931 -- the second pass.
2933 if Has_Access_Constraint (Id) and then No (Expression (Decl)) then
2934 Has_POC := True;
2936 -- Regular component cases
2938 else
2939 -- In the context of the init proc, references to discriminants
2940 -- resolve to denote the discriminals: this is where we can
2941 -- freeze discriminant dependent component subtypes.
2943 if not Is_Frozen (Typ) then
2944 Append_List_To (Stmts, Freeze_Entity (Typ, N));
2945 end if;
2947 -- Explicit initialization
2949 if Present (Expression (Decl)) then
2950 if Is_CPP_Constructor_Call (Expression (Decl)) then
2951 Actions :=
2952 Build_Initialization_Call
2953 (Comp_Loc,
2954 Id_Ref =>
2955 Make_Selected_Component (Comp_Loc,
2956 Prefix =>
2957 Make_Identifier (Comp_Loc, Name_uInit),
2958 Selector_Name =>
2959 New_Occurrence_Of (Id, Comp_Loc)),
2960 Typ => Typ,
2961 In_Init_Proc => True,
2962 Enclos_Type => Rec_Type,
2963 Discr_Map => Discr_Map,
2964 Constructor_Ref => Expression (Decl));
2965 else
2966 Actions := Build_Assignment (Id, Expression (Decl));
2967 end if;
2969 -- CPU, Dispatching_Domain, Priority, and Secondary_Stack_Size
2970 -- components are filled in with the corresponding rep-item
2971 -- expression of the concurrent type (if any).
2973 elsif Ekind (Scope (Id)) = E_Record_Type
2974 and then Present (Corresponding_Concurrent_Type (Scope (Id)))
2975 and then Nam_In (Chars (Id), Name_uCPU,
2976 Name_uDispatching_Domain,
2977 Name_uPriority,
2978 Name_uSecondary_Stack_Size)
2979 then
2980 declare
2981 Exp : Node_Id;
2982 Nam : Name_Id;
2983 pragma Warnings (Off, Nam);
2984 Ritem : Node_Id;
2986 begin
2987 if Chars (Id) = Name_uCPU then
2988 Nam := Name_CPU;
2990 elsif Chars (Id) = Name_uDispatching_Domain then
2991 Nam := Name_Dispatching_Domain;
2993 elsif Chars (Id) = Name_uPriority then
2994 Nam := Name_Priority;
2996 elsif Chars (Id) = Name_uSecondary_Stack_Size then
2997 Nam := Name_Secondary_Stack_Size;
2998 end if;
3000 -- Get the Rep Item (aspect specification, attribute
3001 -- definition clause or pragma) of the corresponding
3002 -- concurrent type.
3004 Ritem :=
3005 Get_Rep_Item
3006 (Corresponding_Concurrent_Type (Scope (Id)),
3007 Nam,
3008 Check_Parents => False);
3010 if Present (Ritem) then
3012 -- Pragma case
3014 if Nkind (Ritem) = N_Pragma then
3015 Exp := First (Pragma_Argument_Associations (Ritem));
3017 if Nkind (Exp) = N_Pragma_Argument_Association then
3018 Exp := Expression (Exp);
3019 end if;
3021 -- Conversion for Priority expression
3023 if Nam = Name_Priority then
3024 if Pragma_Name (Ritem) = Name_Priority
3025 and then not GNAT_Mode
3026 then
3027 Exp := Convert_To (RTE (RE_Priority), Exp);
3028 else
3029 Exp :=
3030 Convert_To (RTE (RE_Any_Priority), Exp);
3031 end if;
3032 end if;
3034 -- Aspect/Attribute definition clause case
3036 else
3037 Exp := Expression (Ritem);
3039 -- Conversion for Priority expression
3041 if Nam = Name_Priority then
3042 if Chars (Ritem) = Name_Priority
3043 and then not GNAT_Mode
3044 then
3045 Exp := Convert_To (RTE (RE_Priority), Exp);
3046 else
3047 Exp :=
3048 Convert_To (RTE (RE_Any_Priority), Exp);
3049 end if;
3050 end if;
3051 end if;
3053 -- Conversion for Dispatching_Domain value
3055 if Nam = Name_Dispatching_Domain then
3056 Exp :=
3057 Unchecked_Convert_To
3058 (RTE (RE_Dispatching_Domain_Access), Exp);
3060 -- Conversion for Secondary_Stack_Size value
3062 elsif Nam = Name_Secondary_Stack_Size then
3063 Exp := Convert_To (RTE (RE_Size_Type), Exp);
3064 end if;
3066 Actions := Build_Assignment (Id, Exp);
3068 -- Nothing needed if no Rep Item
3070 else
3071 Actions := No_List;
3072 end if;
3073 end;
3075 -- Composite component with its own Init_Proc
3077 elsif not Is_Interface (Typ)
3078 and then Has_Non_Null_Base_Init_Proc (Typ)
3079 then
3080 Actions :=
3081 Build_Initialization_Call
3082 (Comp_Loc,
3083 Make_Selected_Component (Comp_Loc,
3084 Prefix =>
3085 Make_Identifier (Comp_Loc, Name_uInit),
3086 Selector_Name => New_Occurrence_Of (Id, Comp_Loc)),
3087 Typ,
3088 In_Init_Proc => True,
3089 Enclos_Type => Rec_Type,
3090 Discr_Map => Discr_Map);
3092 Clean_Task_Names (Typ, Proc_Id);
3094 -- Simple initialization
3096 elsif Component_Needs_Simple_Initialization (Typ) then
3097 Actions :=
3098 Build_Assignment
3099 (Id, Get_Simple_Init_Val (Typ, N, Esize (Id)));
3101 -- Nothing needed for this case
3103 else
3104 Actions := No_List;
3105 end if;
3107 if Present (Checks) then
3108 if Chars (Id) = Name_uParent then
3109 Append_List_To (Parent_Stmts, Checks);
3110 else
3111 Append_List_To (Stmts, Checks);
3112 end if;
3113 end if;
3115 if Present (Actions) then
3116 if Chars (Id) = Name_uParent then
3117 Append_List_To (Parent_Stmts, Actions);
3119 else
3120 Append_List_To (Stmts, Actions);
3122 -- Preserve initialization state in the current counter
3124 if Needs_Finalization (Typ) then
3125 if No (Counter_Id) then
3126 Make_Counter (Comp_Loc);
3127 end if;
3129 Increment_Counter (Comp_Loc);
3130 end if;
3131 end if;
3132 end if;
3133 end if;
3135 Next_Non_Pragma (Decl);
3136 end loop;
3138 -- The parent field must be initialized first because variable
3139 -- size components of the parent affect the location of all the
3140 -- new components.
3142 Prepend_List_To (Stmts, Parent_Stmts);
3144 -- Set up tasks and protected object support. This needs to be done
3145 -- before any component with a per-object access discriminant
3146 -- constraint, or any variant part (which may contain such
3147 -- components) is initialized, because the initialization of these
3148 -- components may reference the enclosing concurrent object.
3150 -- For a task record type, add the task create call and calls to bind
3151 -- any interrupt (signal) entries.
3153 if Is_Task_Record_Type (Rec_Type) then
3155 -- In the case of the restricted run time the ATCB has already
3156 -- been preallocated.
3158 if Restricted_Profile then
3159 Append_To (Stmts,
3160 Make_Assignment_Statement (Loc,
3161 Name =>
3162 Make_Selected_Component (Loc,
3163 Prefix => Make_Identifier (Loc, Name_uInit),
3164 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)),
3165 Expression =>
3166 Make_Attribute_Reference (Loc,
3167 Prefix =>
3168 Make_Selected_Component (Loc,
3169 Prefix => Make_Identifier (Loc, Name_uInit),
3170 Selector_Name => Make_Identifier (Loc, Name_uATCB)),
3171 Attribute_Name => Name_Unchecked_Access)));
3172 end if;
3174 Append_To (Stmts, Make_Task_Create_Call (Rec_Type));
3176 declare
3177 Task_Type : constant Entity_Id :=
3178 Corresponding_Concurrent_Type (Rec_Type);
3179 Task_Decl : constant Node_Id := Parent (Task_Type);
3180 Task_Def : constant Node_Id := Task_Definition (Task_Decl);
3181 Decl_Loc : Source_Ptr;
3182 Ent : Entity_Id;
3183 Vis_Decl : Node_Id;
3185 begin
3186 if Present (Task_Def) then
3187 Vis_Decl := First (Visible_Declarations (Task_Def));
3188 while Present (Vis_Decl) loop
3189 Decl_Loc := Sloc (Vis_Decl);
3191 if Nkind (Vis_Decl) = N_Attribute_Definition_Clause then
3192 if Get_Attribute_Id (Chars (Vis_Decl)) =
3193 Attribute_Address
3194 then
3195 Ent := Entity (Name (Vis_Decl));
3197 if Ekind (Ent) = E_Entry then
3198 Append_To (Stmts,
3199 Make_Procedure_Call_Statement (Decl_Loc,
3200 Name =>
3201 New_Occurrence_Of (RTE (
3202 RE_Bind_Interrupt_To_Entry), Decl_Loc),
3203 Parameter_Associations => New_List (
3204 Make_Selected_Component (Decl_Loc,
3205 Prefix =>
3206 Make_Identifier (Decl_Loc, Name_uInit),
3207 Selector_Name =>
3208 Make_Identifier
3209 (Decl_Loc, Name_uTask_Id)),
3210 Entry_Index_Expression
3211 (Decl_Loc, Ent, Empty, Task_Type),
3212 Expression (Vis_Decl))));
3213 end if;
3214 end if;
3215 end if;
3217 Next (Vis_Decl);
3218 end loop;
3219 end if;
3220 end;
3221 end if;
3223 -- For a protected type, add statements generated by
3224 -- Make_Initialize_Protection.
3226 if Is_Protected_Record_Type (Rec_Type) then
3227 Append_List_To (Stmts,
3228 Make_Initialize_Protection (Rec_Type));
3229 end if;
3231 -- Second pass: components with per-object constraints
3233 if Has_POC then
3234 Decl := First_Non_Pragma (Component_Items (Comp_List));
3235 while Present (Decl) loop
3236 Comp_Loc := Sloc (Decl);
3237 Id := Defining_Identifier (Decl);
3238 Typ := Etype (Id);
3240 if Has_Access_Constraint (Id)
3241 and then No (Expression (Decl))
3242 then
3243 if Has_Non_Null_Base_Init_Proc (Typ) then
3244 Append_List_To (Stmts,
3245 Build_Initialization_Call (Comp_Loc,
3246 Make_Selected_Component (Comp_Loc,
3247 Prefix =>
3248 Make_Identifier (Comp_Loc, Name_uInit),
3249 Selector_Name => New_Occurrence_Of (Id, Comp_Loc)),
3250 Typ,
3251 In_Init_Proc => True,
3252 Enclos_Type => Rec_Type,
3253 Discr_Map => Discr_Map));
3255 Clean_Task_Names (Typ, Proc_Id);
3257 -- Preserve initialization state in the current counter
3259 if Needs_Finalization (Typ) then
3260 if No (Counter_Id) then
3261 Make_Counter (Comp_Loc);
3262 end if;
3264 Increment_Counter (Comp_Loc);
3265 end if;
3267 elsif Component_Needs_Simple_Initialization (Typ) then
3268 Append_List_To (Stmts,
3269 Build_Assignment
3270 (Id, Get_Simple_Init_Val (Typ, N, Esize (Id))));
3271 end if;
3272 end if;
3274 Next_Non_Pragma (Decl);
3275 end loop;
3276 end if;
3278 -- Process the variant part
3280 if Present (Variant_Part (Comp_List)) then
3281 declare
3282 Variant_Alts : constant List_Id := New_List;
3283 Var_Loc : Source_Ptr := No_Location;
3284 Variant : Node_Id;
3286 begin
3287 Variant :=
3288 First_Non_Pragma (Variants (Variant_Part (Comp_List)));
3289 while Present (Variant) loop
3290 Var_Loc := Sloc (Variant);
3291 Append_To (Variant_Alts,
3292 Make_Case_Statement_Alternative (Var_Loc,
3293 Discrete_Choices =>
3294 New_Copy_List (Discrete_Choices (Variant)),
3295 Statements =>
3296 Build_Init_Statements (Component_List (Variant))));
3297 Next_Non_Pragma (Variant);
3298 end loop;
3300 -- The expression of the case statement which is a reference
3301 -- to one of the discriminants is replaced by the appropriate
3302 -- formal parameter of the initialization procedure.
3304 Append_To (Stmts,
3305 Make_Case_Statement (Var_Loc,
3306 Expression =>
3307 New_Occurrence_Of (Discriminal (
3308 Entity (Name (Variant_Part (Comp_List)))), Var_Loc),
3309 Alternatives => Variant_Alts));
3310 end;
3311 end if;
3313 -- If no initializations when generated for component declarations
3314 -- corresponding to this Stmts, append a null statement to Stmts to
3315 -- to make it a valid Ada tree.
3317 if Is_Empty_List (Stmts) then
3318 Append (Make_Null_Statement (Loc), Stmts);
3319 end if;
3321 return Stmts;
3323 exception
3324 when RE_Not_Available =>
3325 return Empty_List;
3326 end Build_Init_Statements;
3328 -------------------------
3329 -- Build_Record_Checks --
3330 -------------------------
3332 procedure Build_Record_Checks (S : Node_Id; Check_List : List_Id) is
3333 Subtype_Mark_Id : Entity_Id;
3335 procedure Constrain_Array
3336 (SI : Node_Id;
3337 Check_List : List_Id);
3338 -- Apply a list of index constraints to an unconstrained array type.
3339 -- The first parameter is the entity for the resulting subtype.
3340 -- Check_List is a list to which the check actions are appended.
3342 ---------------------
3343 -- Constrain_Array --
3344 ---------------------
3346 procedure Constrain_Array
3347 (SI : Node_Id;
3348 Check_List : List_Id)
3350 C : constant Node_Id := Constraint (SI);
3351 Number_Of_Constraints : Nat := 0;
3352 Index : Node_Id;
3353 S, T : Entity_Id;
3355 procedure Constrain_Index
3356 (Index : Node_Id;
3357 S : Node_Id;
3358 Check_List : List_Id);
3359 -- Process an index constraint in a constrained array declaration.
3360 -- The constraint can be either a subtype name or a range with or
3361 -- without an explicit subtype mark. Index is the corresponding
3362 -- index of the unconstrained array. S is the range expression.
3363 -- Check_List is a list to which the check actions are appended.
3365 ---------------------
3366 -- Constrain_Index --
3367 ---------------------
3369 procedure Constrain_Index
3370 (Index : Node_Id;
3371 S : Node_Id;
3372 Check_List : List_Id)
3374 T : constant Entity_Id := Etype (Index);
3376 begin
3377 if Nkind (S) = N_Range then
3378 Process_Range_Expr_In_Decl (S, T, Check_List => Check_List);
3379 end if;
3380 end Constrain_Index;
3382 -- Start of processing for Constrain_Array
3384 begin
3385 T := Entity (Subtype_Mark (SI));
3387 if Is_Access_Type (T) then
3388 T := Designated_Type (T);
3389 end if;
3391 S := First (Constraints (C));
3392 while Present (S) loop
3393 Number_Of_Constraints := Number_Of_Constraints + 1;
3394 Next (S);
3395 end loop;
3397 -- In either case, the index constraint must provide a discrete
3398 -- range for each index of the array type and the type of each
3399 -- discrete range must be the same as that of the corresponding
3400 -- index. (RM 3.6.1)
3402 S := First (Constraints (C));
3403 Index := First_Index (T);
3404 Analyze (Index);
3406 -- Apply constraints to each index type
3408 for J in 1 .. Number_Of_Constraints loop
3409 Constrain_Index (Index, S, Check_List);
3410 Next (Index);
3411 Next (S);
3412 end loop;
3413 end Constrain_Array;
3415 -- Start of processing for Build_Record_Checks
3417 begin
3418 if Nkind (S) = N_Subtype_Indication then
3419 Find_Type (Subtype_Mark (S));
3420 Subtype_Mark_Id := Entity (Subtype_Mark (S));
3422 -- Remaining processing depends on type
3424 case Ekind (Subtype_Mark_Id) is
3425 when Array_Kind =>
3426 Constrain_Array (S, Check_List);
3428 when others =>
3429 null;
3430 end case;
3431 end if;
3432 end Build_Record_Checks;
3434 -------------------------------------------
3435 -- Component_Needs_Simple_Initialization --
3436 -------------------------------------------
3438 function Component_Needs_Simple_Initialization
3439 (T : Entity_Id) return Boolean
3441 begin
3442 return
3443 Needs_Simple_Initialization (T)
3444 and then not Is_RTE (T, RE_Tag)
3446 -- Ada 2005 (AI-251): Check also the tag of abstract interfaces
3448 and then not Is_RTE (T, RE_Interface_Tag);
3449 end Component_Needs_Simple_Initialization;
3451 --------------------------------------
3452 -- Parent_Subtype_Renaming_Discrims --
3453 --------------------------------------
3455 function Parent_Subtype_Renaming_Discrims return Boolean is
3456 De : Entity_Id;
3457 Dp : Entity_Id;
3459 begin
3460 if Base_Type (Rec_Ent) /= Rec_Ent then
3461 return False;
3462 end if;
3464 if Etype (Rec_Ent) = Rec_Ent
3465 or else not Has_Discriminants (Rec_Ent)
3466 or else Is_Constrained (Rec_Ent)
3467 or else Is_Tagged_Type (Rec_Ent)
3468 then
3469 return False;
3470 end if;
3472 -- If there are no explicit stored discriminants we have inherited
3473 -- the root type discriminants so far, so no renamings occurred.
3475 if First_Discriminant (Rec_Ent) =
3476 First_Stored_Discriminant (Rec_Ent)
3477 then
3478 return False;
3479 end if;
3481 -- Check if we have done some trivial renaming of the parent
3482 -- discriminants, i.e. something like
3484 -- type DT (X1, X2: int) is new PT (X1, X2);
3486 De := First_Discriminant (Rec_Ent);
3487 Dp := First_Discriminant (Etype (Rec_Ent));
3488 while Present (De) loop
3489 pragma Assert (Present (Dp));
3491 if Corresponding_Discriminant (De) /= Dp then
3492 return True;
3493 end if;
3495 Next_Discriminant (De);
3496 Next_Discriminant (Dp);
3497 end loop;
3499 return Present (Dp);
3500 end Parent_Subtype_Renaming_Discrims;
3502 ------------------------
3503 -- Requires_Init_Proc --
3504 ------------------------
3506 function Requires_Init_Proc (Rec_Id : Entity_Id) return Boolean is
3507 Comp_Decl : Node_Id;
3508 Id : Entity_Id;
3509 Typ : Entity_Id;
3511 begin
3512 -- Definitely do not need one if specifically suppressed
3514 if Initialization_Suppressed (Rec_Id) then
3515 return False;
3516 end if;
3518 -- If it is a type derived from a type with unknown discriminants,
3519 -- we cannot build an initialization procedure for it.
3521 if Has_Unknown_Discriminants (Rec_Id)
3522 or else Has_Unknown_Discriminants (Etype (Rec_Id))
3523 then
3524 return False;
3525 end if;
3527 -- Otherwise we need to generate an initialization procedure if
3528 -- Is_CPP_Class is False and at least one of the following applies:
3530 -- 1. Discriminants are present, since they need to be initialized
3531 -- with the appropriate discriminant constraint expressions.
3532 -- However, the discriminant of an unchecked union does not
3533 -- count, since the discriminant is not present.
3535 -- 2. The type is a tagged type, since the implicit Tag component
3536 -- needs to be initialized with a pointer to the dispatch table.
3538 -- 3. The type contains tasks
3540 -- 4. One or more components has an initial value
3542 -- 5. One or more components is for a type which itself requires
3543 -- an initialization procedure.
3545 -- 6. One or more components is a type that requires simple
3546 -- initialization (see Needs_Simple_Initialization), except
3547 -- that types Tag and Interface_Tag are excluded, since fields
3548 -- of these types are initialized by other means.
3550 -- 7. The type is the record type built for a task type (since at
3551 -- the very least, Create_Task must be called)
3553 -- 8. The type is the record type built for a protected type (since
3554 -- at least Initialize_Protection must be called)
3556 -- 9. The type is marked as a public entity. The reason we add this
3557 -- case (even if none of the above apply) is to properly handle
3558 -- Initialize_Scalars. If a package is compiled without an IS
3559 -- pragma, and the client is compiled with an IS pragma, then
3560 -- the client will think an initialization procedure is present
3561 -- and call it, when in fact no such procedure is required, but
3562 -- since the call is generated, there had better be a routine
3563 -- at the other end of the call, even if it does nothing).
3565 -- Note: the reason we exclude the CPP_Class case is because in this
3566 -- case the initialization is performed by the C++ constructors, and
3567 -- the IP is built by Set_CPP_Constructors.
3569 if Is_CPP_Class (Rec_Id) then
3570 return False;
3572 elsif Is_Interface (Rec_Id) then
3573 return False;
3575 elsif (Has_Discriminants (Rec_Id)
3576 and then not Is_Unchecked_Union (Rec_Id))
3577 or else Is_Tagged_Type (Rec_Id)
3578 or else Is_Concurrent_Record_Type (Rec_Id)
3579 or else Has_Task (Rec_Id)
3580 then
3581 return True;
3582 end if;
3584 Id := First_Component (Rec_Id);
3585 while Present (Id) loop
3586 Comp_Decl := Parent (Id);
3587 Typ := Etype (Id);
3589 if Present (Expression (Comp_Decl))
3590 or else Has_Non_Null_Base_Init_Proc (Typ)
3591 or else Component_Needs_Simple_Initialization (Typ)
3592 then
3593 return True;
3594 end if;
3596 Next_Component (Id);
3597 end loop;
3599 -- As explained above, a record initialization procedure is needed
3600 -- for public types in case Initialize_Scalars applies to a client.
3601 -- However, such a procedure is not needed in the case where either
3602 -- of restrictions No_Initialize_Scalars or No_Default_Initialization
3603 -- applies. No_Initialize_Scalars excludes the possibility of using
3604 -- Initialize_Scalars in any partition, and No_Default_Initialization
3605 -- implies that no initialization should ever be done for objects of
3606 -- the type, so is incompatible with Initialize_Scalars.
3608 if not Restriction_Active (No_Initialize_Scalars)
3609 and then not Restriction_Active (No_Default_Initialization)
3610 and then Is_Public (Rec_Id)
3611 then
3612 return True;
3613 end if;
3615 return False;
3616 end Requires_Init_Proc;
3618 -- Start of processing for Build_Record_Init_Proc
3620 begin
3621 Rec_Type := Defining_Identifier (N);
3623 -- This may be full declaration of a private type, in which case
3624 -- the visible entity is a record, and the private entity has been
3625 -- exchanged with it in the private part of the current package.
3626 -- The initialization procedure is built for the record type, which
3627 -- is retrievable from the private entity.
3629 if Is_Incomplete_Or_Private_Type (Rec_Type) then
3630 Rec_Type := Underlying_Type (Rec_Type);
3631 end if;
3633 -- If we have a variant record with restriction No_Implicit_Conditionals
3634 -- in effect, then we skip building the procedure. This is safe because
3635 -- if we can see the restriction, so can any caller, calls to initialize
3636 -- such records are not allowed for variant records if this restriction
3637 -- is active.
3639 if Has_Variant_Part (Rec_Type)
3640 and then Restriction_Active (No_Implicit_Conditionals)
3641 then
3642 return;
3643 end if;
3645 -- If there are discriminants, build the discriminant map to replace
3646 -- discriminants by their discriminals in complex bound expressions.
3647 -- These only arise for the corresponding records of synchronized types.
3649 if Is_Concurrent_Record_Type (Rec_Type)
3650 and then Has_Discriminants (Rec_Type)
3651 then
3652 declare
3653 Disc : Entity_Id;
3654 begin
3655 Disc := First_Discriminant (Rec_Type);
3656 while Present (Disc) loop
3657 Append_Elmt (Disc, Discr_Map);
3658 Append_Elmt (Discriminal (Disc), Discr_Map);
3659 Next_Discriminant (Disc);
3660 end loop;
3661 end;
3662 end if;
3664 -- Derived types that have no type extension can use the initialization
3665 -- procedure of their parent and do not need a procedure of their own.
3666 -- This is only correct if there are no representation clauses for the
3667 -- type or its parent, and if the parent has in fact been frozen so
3668 -- that its initialization procedure exists.
3670 if Is_Derived_Type (Rec_Type)
3671 and then not Is_Tagged_Type (Rec_Type)
3672 and then not Is_Unchecked_Union (Rec_Type)
3673 and then not Has_New_Non_Standard_Rep (Rec_Type)
3674 and then not Parent_Subtype_Renaming_Discrims
3675 and then Has_Non_Null_Base_Init_Proc (Etype (Rec_Type))
3676 then
3677 Copy_TSS (Base_Init_Proc (Etype (Rec_Type)), Rec_Type);
3679 -- Otherwise if we need an initialization procedure, then build one,
3680 -- mark it as public and inlinable and as having a completion.
3682 elsif Requires_Init_Proc (Rec_Type)
3683 or else Is_Unchecked_Union (Rec_Type)
3684 then
3685 Proc_Id :=
3686 Make_Defining_Identifier (Loc,
3687 Chars => Make_Init_Proc_Name (Rec_Type));
3689 -- If No_Default_Initialization restriction is active, then we don't
3690 -- want to build an init_proc, but we need to mark that an init_proc
3691 -- would be needed if this restriction was not active (so that we can
3692 -- detect attempts to call it), so set a dummy init_proc in place.
3694 if Restriction_Active (No_Default_Initialization) then
3695 Set_Init_Proc (Rec_Type, Proc_Id);
3696 return;
3697 end if;
3699 Build_Offset_To_Top_Functions;
3700 Build_CPP_Init_Procedure;
3701 Build_Init_Procedure;
3703 Set_Is_Public (Proc_Id, Is_Public (Rec_Ent));
3704 Set_Is_Internal (Proc_Id);
3705 Set_Has_Completion (Proc_Id);
3707 if not Debug_Generated_Code then
3708 Set_Debug_Info_Off (Proc_Id);
3709 end if;
3711 Set_Is_Inlined (Proc_Id, Inline_Init_Proc (Rec_Type));
3713 -- Do not build an aggregate if Modify_Tree_For_C, this isn't
3714 -- needed and may generate early references to non frozen types
3715 -- since we expand aggregate much more systematically.
3717 if Modify_Tree_For_C then
3718 return;
3719 end if;
3721 declare
3722 Agg : constant Node_Id :=
3723 Build_Equivalent_Record_Aggregate (Rec_Type);
3725 procedure Collect_Itypes (Comp : Node_Id);
3726 -- Generate references to itypes in the aggregate, because
3727 -- the first use of the aggregate may be in a nested scope.
3729 --------------------
3730 -- Collect_Itypes --
3731 --------------------
3733 procedure Collect_Itypes (Comp : Node_Id) is
3734 Ref : Node_Id;
3735 Sub_Aggr : Node_Id;
3736 Typ : constant Entity_Id := Etype (Comp);
3738 begin
3739 if Is_Array_Type (Typ) and then Is_Itype (Typ) then
3740 Ref := Make_Itype_Reference (Loc);
3741 Set_Itype (Ref, Typ);
3742 Append_Freeze_Action (Rec_Type, Ref);
3744 Ref := Make_Itype_Reference (Loc);
3745 Set_Itype (Ref, Etype (First_Index (Typ)));
3746 Append_Freeze_Action (Rec_Type, Ref);
3748 -- Recurse on nested arrays
3750 Sub_Aggr := First (Expressions (Comp));
3751 while Present (Sub_Aggr) loop
3752 Collect_Itypes (Sub_Aggr);
3753 Next (Sub_Aggr);
3754 end loop;
3755 end if;
3756 end Collect_Itypes;
3758 begin
3759 -- If there is a static initialization aggregate for the type,
3760 -- generate itype references for the types of its (sub)components,
3761 -- to prevent out-of-scope errors in the resulting tree.
3762 -- The aggregate may have been rewritten as a Raise node, in which
3763 -- case there are no relevant itypes.
3765 if Present (Agg) and then Nkind (Agg) = N_Aggregate then
3766 Set_Static_Initialization (Proc_Id, Agg);
3768 declare
3769 Comp : Node_Id;
3770 begin
3771 Comp := First (Component_Associations (Agg));
3772 while Present (Comp) loop
3773 Collect_Itypes (Expression (Comp));
3774 Next (Comp);
3775 end loop;
3776 end;
3777 end if;
3778 end;
3779 end if;
3780 end Build_Record_Init_Proc;
3782 ----------------------------
3783 -- Build_Slice_Assignment --
3784 ----------------------------
3786 -- Generates the following subprogram:
3788 -- procedure Assign
3789 -- (Source, Target : Array_Type,
3790 -- Left_Lo, Left_Hi : Index;
3791 -- Right_Lo, Right_Hi : Index;
3792 -- Rev : Boolean)
3793 -- is
3794 -- Li1 : Index;
3795 -- Ri1 : Index;
3797 -- begin
3799 -- if Left_Hi < Left_Lo then
3800 -- return;
3801 -- end if;
3803 -- if Rev then
3804 -- Li1 := Left_Hi;
3805 -- Ri1 := Right_Hi;
3806 -- else
3807 -- Li1 := Left_Lo;
3808 -- Ri1 := Right_Lo;
3809 -- end if;
3811 -- loop
3812 -- Target (Li1) := Source (Ri1);
3814 -- if Rev then
3815 -- exit when Li1 = Left_Lo;
3816 -- Li1 := Index'pred (Li1);
3817 -- Ri1 := Index'pred (Ri1);
3818 -- else
3819 -- exit when Li1 = Left_Hi;
3820 -- Li1 := Index'succ (Li1);
3821 -- Ri1 := Index'succ (Ri1);
3822 -- end if;
3823 -- end loop;
3824 -- end Assign;
3826 procedure Build_Slice_Assignment (Typ : Entity_Id) is
3827 Loc : constant Source_Ptr := Sloc (Typ);
3828 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
3830 Larray : constant Entity_Id := Make_Temporary (Loc, 'A');
3831 Rarray : constant Entity_Id := Make_Temporary (Loc, 'R');
3832 Left_Lo : constant Entity_Id := Make_Temporary (Loc, 'L');
3833 Left_Hi : constant Entity_Id := Make_Temporary (Loc, 'L');
3834 Right_Lo : constant Entity_Id := Make_Temporary (Loc, 'R');
3835 Right_Hi : constant Entity_Id := Make_Temporary (Loc, 'R');
3836 Rev : constant Entity_Id := Make_Temporary (Loc, 'D');
3837 -- Formal parameters of procedure
3839 Proc_Name : constant Entity_Id :=
3840 Make_Defining_Identifier (Loc,
3841 Chars => Make_TSS_Name (Typ, TSS_Slice_Assign));
3843 Lnn : constant Entity_Id := Make_Temporary (Loc, 'L');
3844 Rnn : constant Entity_Id := Make_Temporary (Loc, 'R');
3845 -- Subscripts for left and right sides
3847 Decls : List_Id;
3848 Loops : Node_Id;
3849 Stats : List_Id;
3851 begin
3852 -- Build declarations for indexes
3854 Decls := New_List;
3856 Append_To (Decls,
3857 Make_Object_Declaration (Loc,
3858 Defining_Identifier => Lnn,
3859 Object_Definition =>
3860 New_Occurrence_Of (Index, Loc)));
3862 Append_To (Decls,
3863 Make_Object_Declaration (Loc,
3864 Defining_Identifier => Rnn,
3865 Object_Definition =>
3866 New_Occurrence_Of (Index, Loc)));
3868 Stats := New_List;
3870 -- Build test for empty slice case
3872 Append_To (Stats,
3873 Make_If_Statement (Loc,
3874 Condition =>
3875 Make_Op_Lt (Loc,
3876 Left_Opnd => New_Occurrence_Of (Left_Hi, Loc),
3877 Right_Opnd => New_Occurrence_Of (Left_Lo, Loc)),
3878 Then_Statements => New_List (Make_Simple_Return_Statement (Loc))));
3880 -- Build initializations for indexes
3882 declare
3883 F_Init : constant List_Id := New_List;
3884 B_Init : constant List_Id := New_List;
3886 begin
3887 Append_To (F_Init,
3888 Make_Assignment_Statement (Loc,
3889 Name => New_Occurrence_Of (Lnn, Loc),
3890 Expression => New_Occurrence_Of (Left_Lo, Loc)));
3892 Append_To (F_Init,
3893 Make_Assignment_Statement (Loc,
3894 Name => New_Occurrence_Of (Rnn, Loc),
3895 Expression => New_Occurrence_Of (Right_Lo, Loc)));
3897 Append_To (B_Init,
3898 Make_Assignment_Statement (Loc,
3899 Name => New_Occurrence_Of (Lnn, Loc),
3900 Expression => New_Occurrence_Of (Left_Hi, Loc)));
3902 Append_To (B_Init,
3903 Make_Assignment_Statement (Loc,
3904 Name => New_Occurrence_Of (Rnn, Loc),
3905 Expression => New_Occurrence_Of (Right_Hi, Loc)));
3907 Append_To (Stats,
3908 Make_If_Statement (Loc,
3909 Condition => New_Occurrence_Of (Rev, Loc),
3910 Then_Statements => B_Init,
3911 Else_Statements => F_Init));
3912 end;
3914 -- Now construct the assignment statement
3916 Loops :=
3917 Make_Loop_Statement (Loc,
3918 Statements => New_List (
3919 Make_Assignment_Statement (Loc,
3920 Name =>
3921 Make_Indexed_Component (Loc,
3922 Prefix => New_Occurrence_Of (Larray, Loc),
3923 Expressions => New_List (New_Occurrence_Of (Lnn, Loc))),
3924 Expression =>
3925 Make_Indexed_Component (Loc,
3926 Prefix => New_Occurrence_Of (Rarray, Loc),
3927 Expressions => New_List (New_Occurrence_Of (Rnn, Loc))))),
3928 End_Label => Empty);
3930 -- Build the exit condition and increment/decrement statements
3932 declare
3933 F_Ass : constant List_Id := New_List;
3934 B_Ass : constant List_Id := New_List;
3936 begin
3937 Append_To (F_Ass,
3938 Make_Exit_Statement (Loc,
3939 Condition =>
3940 Make_Op_Eq (Loc,
3941 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
3942 Right_Opnd => New_Occurrence_Of (Left_Hi, Loc))));
3944 Append_To (F_Ass,
3945 Make_Assignment_Statement (Loc,
3946 Name => New_Occurrence_Of (Lnn, Loc),
3947 Expression =>
3948 Make_Attribute_Reference (Loc,
3949 Prefix =>
3950 New_Occurrence_Of (Index, Loc),
3951 Attribute_Name => Name_Succ,
3952 Expressions => New_List (
3953 New_Occurrence_Of (Lnn, Loc)))));
3955 Append_To (F_Ass,
3956 Make_Assignment_Statement (Loc,
3957 Name => New_Occurrence_Of (Rnn, Loc),
3958 Expression =>
3959 Make_Attribute_Reference (Loc,
3960 Prefix =>
3961 New_Occurrence_Of (Index, Loc),
3962 Attribute_Name => Name_Succ,
3963 Expressions => New_List (
3964 New_Occurrence_Of (Rnn, Loc)))));
3966 Append_To (B_Ass,
3967 Make_Exit_Statement (Loc,
3968 Condition =>
3969 Make_Op_Eq (Loc,
3970 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
3971 Right_Opnd => New_Occurrence_Of (Left_Lo, Loc))));
3973 Append_To (B_Ass,
3974 Make_Assignment_Statement (Loc,
3975 Name => New_Occurrence_Of (Lnn, Loc),
3976 Expression =>
3977 Make_Attribute_Reference (Loc,
3978 Prefix =>
3979 New_Occurrence_Of (Index, Loc),
3980 Attribute_Name => Name_Pred,
3981 Expressions => New_List (
3982 New_Occurrence_Of (Lnn, Loc)))));
3984 Append_To (B_Ass,
3985 Make_Assignment_Statement (Loc,
3986 Name => New_Occurrence_Of (Rnn, Loc),
3987 Expression =>
3988 Make_Attribute_Reference (Loc,
3989 Prefix =>
3990 New_Occurrence_Of (Index, Loc),
3991 Attribute_Name => Name_Pred,
3992 Expressions => New_List (
3993 New_Occurrence_Of (Rnn, Loc)))));
3995 Append_To (Statements (Loops),
3996 Make_If_Statement (Loc,
3997 Condition => New_Occurrence_Of (Rev, Loc),
3998 Then_Statements => B_Ass,
3999 Else_Statements => F_Ass));
4000 end;
4002 Append_To (Stats, Loops);
4004 declare
4005 Spec : Node_Id;
4006 Formals : List_Id := New_List;
4008 begin
4009 Formals := New_List (
4010 Make_Parameter_Specification (Loc,
4011 Defining_Identifier => Larray,
4012 Out_Present => True,
4013 Parameter_Type =>
4014 New_Occurrence_Of (Base_Type (Typ), Loc)),
4016 Make_Parameter_Specification (Loc,
4017 Defining_Identifier => Rarray,
4018 Parameter_Type =>
4019 New_Occurrence_Of (Base_Type (Typ), Loc)),
4021 Make_Parameter_Specification (Loc,
4022 Defining_Identifier => Left_Lo,
4023 Parameter_Type =>
4024 New_Occurrence_Of (Index, Loc)),
4026 Make_Parameter_Specification (Loc,
4027 Defining_Identifier => Left_Hi,
4028 Parameter_Type =>
4029 New_Occurrence_Of (Index, Loc)),
4031 Make_Parameter_Specification (Loc,
4032 Defining_Identifier => Right_Lo,
4033 Parameter_Type =>
4034 New_Occurrence_Of (Index, Loc)),
4036 Make_Parameter_Specification (Loc,
4037 Defining_Identifier => Right_Hi,
4038 Parameter_Type =>
4039 New_Occurrence_Of (Index, Loc)));
4041 Append_To (Formals,
4042 Make_Parameter_Specification (Loc,
4043 Defining_Identifier => Rev,
4044 Parameter_Type =>
4045 New_Occurrence_Of (Standard_Boolean, Loc)));
4047 Spec :=
4048 Make_Procedure_Specification (Loc,
4049 Defining_Unit_Name => Proc_Name,
4050 Parameter_Specifications => Formals);
4052 Discard_Node (
4053 Make_Subprogram_Body (Loc,
4054 Specification => Spec,
4055 Declarations => Decls,
4056 Handled_Statement_Sequence =>
4057 Make_Handled_Sequence_Of_Statements (Loc,
4058 Statements => Stats)));
4059 end;
4061 Set_TSS (Typ, Proc_Name);
4062 Set_Is_Pure (Proc_Name);
4063 end Build_Slice_Assignment;
4065 -----------------------------
4066 -- Build_Untagged_Equality --
4067 -----------------------------
4069 procedure Build_Untagged_Equality (Typ : Entity_Id) is
4070 Build_Eq : Boolean;
4071 Comp : Entity_Id;
4072 Decl : Node_Id;
4073 Op : Entity_Id;
4074 Prim : Elmt_Id;
4075 Eq_Op : Entity_Id;
4077 function User_Defined_Eq (T : Entity_Id) return Entity_Id;
4078 -- Check whether the type T has a user-defined primitive equality. If so
4079 -- return it, else return Empty. If true for a component of Typ, we have
4080 -- to build the primitive equality for it.
4082 ---------------------
4083 -- User_Defined_Eq --
4084 ---------------------
4086 function User_Defined_Eq (T : Entity_Id) return Entity_Id is
4087 Prim : Elmt_Id;
4088 Op : Entity_Id;
4090 begin
4091 Op := TSS (T, TSS_Composite_Equality);
4093 if Present (Op) then
4094 return Op;
4095 end if;
4097 Prim := First_Elmt (Collect_Primitive_Operations (T));
4098 while Present (Prim) loop
4099 Op := Node (Prim);
4101 if Chars (Op) = Name_Op_Eq
4102 and then Etype (Op) = Standard_Boolean
4103 and then Etype (First_Formal (Op)) = T
4104 and then Etype (Next_Formal (First_Formal (Op))) = T
4105 then
4106 return Op;
4107 end if;
4109 Next_Elmt (Prim);
4110 end loop;
4112 return Empty;
4113 end User_Defined_Eq;
4115 -- Start of processing for Build_Untagged_Equality
4117 begin
4118 -- If a record component has a primitive equality operation, we must
4119 -- build the corresponding one for the current type.
4121 Build_Eq := False;
4122 Comp := First_Component (Typ);
4123 while Present (Comp) loop
4124 if Is_Record_Type (Etype (Comp))
4125 and then Present (User_Defined_Eq (Etype (Comp)))
4126 then
4127 Build_Eq := True;
4128 end if;
4130 Next_Component (Comp);
4131 end loop;
4133 -- If there is a user-defined equality for the type, we do not create
4134 -- the implicit one.
4136 Prim := First_Elmt (Collect_Primitive_Operations (Typ));
4137 Eq_Op := Empty;
4138 while Present (Prim) loop
4139 if Chars (Node (Prim)) = Name_Op_Eq
4140 and then Comes_From_Source (Node (Prim))
4142 -- Don't we also need to check formal types and return type as in
4143 -- User_Defined_Eq above???
4145 then
4146 Eq_Op := Node (Prim);
4147 Build_Eq := False;
4148 exit;
4149 end if;
4151 Next_Elmt (Prim);
4152 end loop;
4154 -- If the type is derived, inherit the operation, if present, from the
4155 -- parent type. It may have been declared after the type derivation. If
4156 -- the parent type itself is derived, it may have inherited an operation
4157 -- that has itself been overridden, so update its alias and related
4158 -- flags. Ditto for inequality.
4160 if No (Eq_Op) and then Is_Derived_Type (Typ) then
4161 Prim := First_Elmt (Collect_Primitive_Operations (Etype (Typ)));
4162 while Present (Prim) loop
4163 if Chars (Node (Prim)) = Name_Op_Eq then
4164 Copy_TSS (Node (Prim), Typ);
4165 Build_Eq := False;
4167 declare
4168 Op : constant Entity_Id := User_Defined_Eq (Typ);
4169 Eq_Op : constant Entity_Id := Node (Prim);
4170 NE_Op : constant Entity_Id := Next_Entity (Eq_Op);
4172 begin
4173 if Present (Op) then
4174 Set_Alias (Op, Eq_Op);
4175 Set_Is_Abstract_Subprogram
4176 (Op, Is_Abstract_Subprogram (Eq_Op));
4178 if Chars (Next_Entity (Op)) = Name_Op_Ne then
4179 Set_Is_Abstract_Subprogram
4180 (Next_Entity (Op), Is_Abstract_Subprogram (NE_Op));
4181 end if;
4182 end if;
4183 end;
4185 exit;
4186 end if;
4188 Next_Elmt (Prim);
4189 end loop;
4190 end if;
4192 -- If not inherited and not user-defined, build body as for a type with
4193 -- tagged components.
4195 if Build_Eq then
4196 Decl :=
4197 Make_Eq_Body (Typ, Make_TSS_Name (Typ, TSS_Composite_Equality));
4198 Op := Defining_Entity (Decl);
4199 Set_TSS (Typ, Op);
4200 Set_Is_Pure (Op);
4202 if Is_Library_Level_Entity (Typ) then
4203 Set_Is_Public (Op);
4204 end if;
4205 end if;
4206 end Build_Untagged_Equality;
4208 -----------------------------------
4209 -- Build_Variant_Record_Equality --
4210 -----------------------------------
4212 -- Generates:
4214 -- function _Equality (X, Y : T) return Boolean is
4215 -- begin
4216 -- -- Compare discriminants
4218 -- if X.D1 /= Y.D1 or else X.D2 /= Y.D2 or else ... then
4219 -- return False;
4220 -- end if;
4222 -- -- Compare components
4224 -- if X.C1 /= Y.C1 or else X.C2 /= Y.C2 or else ... then
4225 -- return False;
4226 -- end if;
4228 -- -- Compare variant part
4230 -- case X.D1 is
4231 -- when V1 =>
4232 -- if X.C2 /= Y.C2 or else X.C3 /= Y.C3 or else ... then
4233 -- return False;
4234 -- end if;
4235 -- ...
4236 -- when Vn =>
4237 -- if X.Cn /= Y.Cn or else ... then
4238 -- return False;
4239 -- end if;
4240 -- end case;
4242 -- return True;
4243 -- end _Equality;
4245 procedure Build_Variant_Record_Equality (Typ : Entity_Id) is
4246 Loc : constant Source_Ptr := Sloc (Typ);
4248 F : constant Entity_Id :=
4249 Make_Defining_Identifier (Loc,
4250 Chars => Make_TSS_Name (Typ, TSS_Composite_Equality));
4252 X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_X);
4253 Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_Y);
4255 Def : constant Node_Id := Parent (Typ);
4256 Comps : constant Node_Id := Component_List (Type_Definition (Def));
4257 Stmts : constant List_Id := New_List;
4258 Pspecs : constant List_Id := New_List;
4260 begin
4261 -- If we have a variant record with restriction No_Implicit_Conditionals
4262 -- in effect, then we skip building the procedure. This is safe because
4263 -- if we can see the restriction, so can any caller, calls to equality
4264 -- test routines are not allowed for variant records if this restriction
4265 -- is active.
4267 if Restriction_Active (No_Implicit_Conditionals) then
4268 return;
4269 end if;
4271 -- Derived Unchecked_Union types no longer inherit the equality function
4272 -- of their parent.
4274 if Is_Derived_Type (Typ)
4275 and then not Is_Unchecked_Union (Typ)
4276 and then not Has_New_Non_Standard_Rep (Typ)
4277 then
4278 declare
4279 Parent_Eq : constant Entity_Id :=
4280 TSS (Root_Type (Typ), TSS_Composite_Equality);
4281 begin
4282 if Present (Parent_Eq) then
4283 Copy_TSS (Parent_Eq, Typ);
4284 return;
4285 end if;
4286 end;
4287 end if;
4289 Discard_Node (
4290 Make_Subprogram_Body (Loc,
4291 Specification =>
4292 Make_Function_Specification (Loc,
4293 Defining_Unit_Name => F,
4294 Parameter_Specifications => Pspecs,
4295 Result_Definition => New_Occurrence_Of (Standard_Boolean, Loc)),
4296 Declarations => New_List,
4297 Handled_Statement_Sequence =>
4298 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts)));
4300 Append_To (Pspecs,
4301 Make_Parameter_Specification (Loc,
4302 Defining_Identifier => X,
4303 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
4305 Append_To (Pspecs,
4306 Make_Parameter_Specification (Loc,
4307 Defining_Identifier => Y,
4308 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
4310 -- Unchecked_Unions require additional machinery to support equality.
4311 -- Two extra parameters (A and B) are added to the equality function
4312 -- parameter list for each discriminant of the type, in order to
4313 -- capture the inferred values of the discriminants in equality calls.
4314 -- The names of the parameters match the names of the corresponding
4315 -- discriminant, with an added suffix.
4317 if Is_Unchecked_Union (Typ) then
4318 declare
4319 Discr : Entity_Id;
4320 Discr_Type : Entity_Id;
4321 A, B : Entity_Id;
4322 New_Discrs : Elist_Id;
4324 begin
4325 New_Discrs := New_Elmt_List;
4327 Discr := First_Discriminant (Typ);
4328 while Present (Discr) loop
4329 Discr_Type := Etype (Discr);
4330 A := Make_Defining_Identifier (Loc,
4331 Chars => New_External_Name (Chars (Discr), 'A'));
4333 B := Make_Defining_Identifier (Loc,
4334 Chars => New_External_Name (Chars (Discr), 'B'));
4336 -- Add new parameters to the parameter list
4338 Append_To (Pspecs,
4339 Make_Parameter_Specification (Loc,
4340 Defining_Identifier => A,
4341 Parameter_Type =>
4342 New_Occurrence_Of (Discr_Type, Loc)));
4344 Append_To (Pspecs,
4345 Make_Parameter_Specification (Loc,
4346 Defining_Identifier => B,
4347 Parameter_Type =>
4348 New_Occurrence_Of (Discr_Type, Loc)));
4350 Append_Elmt (A, New_Discrs);
4352 -- Generate the following code to compare each of the inferred
4353 -- discriminants:
4355 -- if a /= b then
4356 -- return False;
4357 -- end if;
4359 Append_To (Stmts,
4360 Make_If_Statement (Loc,
4361 Condition =>
4362 Make_Op_Ne (Loc,
4363 Left_Opnd => New_Occurrence_Of (A, Loc),
4364 Right_Opnd => New_Occurrence_Of (B, Loc)),
4365 Then_Statements => New_List (
4366 Make_Simple_Return_Statement (Loc,
4367 Expression =>
4368 New_Occurrence_Of (Standard_False, Loc)))));
4369 Next_Discriminant (Discr);
4370 end loop;
4372 -- Generate component-by-component comparison. Note that we must
4373 -- propagate the inferred discriminants formals to act as
4374 -- the case statement switch. Their value is added when an
4375 -- equality call on unchecked unions is expanded.
4377 Append_List_To (Stmts, Make_Eq_Case (Typ, Comps, New_Discrs));
4378 end;
4380 -- Normal case (not unchecked union)
4382 else
4383 Append_To (Stmts,
4384 Make_Eq_If (Typ, Discriminant_Specifications (Def)));
4385 Append_List_To (Stmts, Make_Eq_Case (Typ, Comps));
4386 end if;
4388 Append_To (Stmts,
4389 Make_Simple_Return_Statement (Loc,
4390 Expression => New_Occurrence_Of (Standard_True, Loc)));
4392 Set_TSS (Typ, F);
4393 Set_Is_Pure (F);
4395 if not Debug_Generated_Code then
4396 Set_Debug_Info_Off (F);
4397 end if;
4398 end Build_Variant_Record_Equality;
4400 -----------------------------
4401 -- Check_Stream_Attributes --
4402 -----------------------------
4404 procedure Check_Stream_Attributes (Typ : Entity_Id) is
4405 Comp : Entity_Id;
4406 Par_Read : constant Boolean :=
4407 Stream_Attribute_Available (Typ, TSS_Stream_Read)
4408 and then not Has_Specified_Stream_Read (Typ);
4409 Par_Write : constant Boolean :=
4410 Stream_Attribute_Available (Typ, TSS_Stream_Write)
4411 and then not Has_Specified_Stream_Write (Typ);
4413 procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type);
4414 -- Check that Comp has a user-specified Nam stream attribute
4416 ----------------
4417 -- Check_Attr --
4418 ----------------
4420 procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type) is
4421 begin
4422 if not Stream_Attribute_Available (Etype (Comp), TSS_Nam) then
4423 Error_Msg_Name_1 := Nam;
4424 Error_Msg_N
4425 ("|component& in limited extension must have% attribute", Comp);
4426 end if;
4427 end Check_Attr;
4429 -- Start of processing for Check_Stream_Attributes
4431 begin
4432 if Par_Read or else Par_Write then
4433 Comp := First_Component (Typ);
4434 while Present (Comp) loop
4435 if Comes_From_Source (Comp)
4436 and then Original_Record_Component (Comp) = Comp
4437 and then Is_Limited_Type (Etype (Comp))
4438 then
4439 if Par_Read then
4440 Check_Attr (Name_Read, TSS_Stream_Read);
4441 end if;
4443 if Par_Write then
4444 Check_Attr (Name_Write, TSS_Stream_Write);
4445 end if;
4446 end if;
4448 Next_Component (Comp);
4449 end loop;
4450 end if;
4451 end Check_Stream_Attributes;
4453 ----------------------
4454 -- Clean_Task_Names --
4455 ----------------------
4457 procedure Clean_Task_Names
4458 (Typ : Entity_Id;
4459 Proc_Id : Entity_Id)
4461 begin
4462 if Has_Task (Typ)
4463 and then not Restriction_Active (No_Implicit_Heap_Allocations)
4464 and then not Global_Discard_Names
4465 and then Tagged_Type_Expansion
4466 then
4467 Set_Uses_Sec_Stack (Proc_Id);
4468 end if;
4469 end Clean_Task_Names;
4471 ------------------------------
4472 -- Expand_Freeze_Array_Type --
4473 ------------------------------
4475 procedure Expand_Freeze_Array_Type (N : Node_Id) is
4476 Typ : constant Entity_Id := Entity (N);
4477 Base : constant Entity_Id := Base_Type (Typ);
4478 Comp_Typ : constant Entity_Id := Component_Type (Typ);
4480 begin
4481 if not Is_Bit_Packed_Array (Typ) then
4483 -- If the component contains tasks, so does the array type. This may
4484 -- not be indicated in the array type because the component may have
4485 -- been a private type at the point of definition. Same if component
4486 -- type is controlled or contains protected objects.
4488 Propagate_Concurrent_Flags (Base, Comp_Typ);
4489 Set_Has_Controlled_Component
4490 (Base, Has_Controlled_Component (Comp_Typ)
4491 or else Is_Controlled (Comp_Typ));
4493 if No (Init_Proc (Base)) then
4495 -- If this is an anonymous array created for a declaration with
4496 -- an initial value, its init_proc will never be called. The
4497 -- initial value itself may have been expanded into assignments,
4498 -- in which case the object declaration is carries the
4499 -- No_Initialization flag.
4501 if Is_Itype (Base)
4502 and then Nkind (Associated_Node_For_Itype (Base)) =
4503 N_Object_Declaration
4504 and then
4505 (Present (Expression (Associated_Node_For_Itype (Base)))
4506 or else No_Initialization (Associated_Node_For_Itype (Base)))
4507 then
4508 null;
4510 -- We do not need an init proc for string or wide [wide] string,
4511 -- since the only time these need initialization in normalize or
4512 -- initialize scalars mode, and these types are treated specially
4513 -- and do not need initialization procedures.
4515 elsif Is_Standard_String_Type (Base) then
4516 null;
4518 -- Otherwise we have to build an init proc for the subtype
4520 else
4521 Build_Array_Init_Proc (Base, N);
4522 end if;
4523 end if;
4525 if Typ = Base and then Has_Controlled_Component (Base) then
4526 Build_Controlling_Procs (Base);
4528 if not Is_Limited_Type (Comp_Typ)
4529 and then Number_Dimensions (Typ) = 1
4530 then
4531 Build_Slice_Assignment (Typ);
4532 end if;
4533 end if;
4535 -- For packed case, default initialization, except if the component type
4536 -- is itself a packed structure with an initialization procedure, or
4537 -- initialize/normalize scalars active, and we have a base type, or the
4538 -- type is public, because in that case a client might specify
4539 -- Normalize_Scalars and there better be a public Init_Proc for it.
4541 elsif (Present (Init_Proc (Component_Type (Base)))
4542 and then No (Base_Init_Proc (Base)))
4543 or else (Init_Or_Norm_Scalars and then Base = Typ)
4544 or else Is_Public (Typ)
4545 then
4546 Build_Array_Init_Proc (Base, N);
4547 end if;
4548 end Expand_Freeze_Array_Type;
4550 -----------------------------------
4551 -- Expand_Freeze_Class_Wide_Type --
4552 -----------------------------------
4554 procedure Expand_Freeze_Class_Wide_Type (N : Node_Id) is
4555 function Is_C_Derivation (Typ : Entity_Id) return Boolean;
4556 -- Given a type, determine whether it is derived from a C or C++ root
4558 ---------------------
4559 -- Is_C_Derivation --
4560 ---------------------
4562 function Is_C_Derivation (Typ : Entity_Id) return Boolean is
4563 T : Entity_Id;
4565 begin
4566 T := Typ;
4567 loop
4568 if Is_CPP_Class (T)
4569 or else Convention (T) = Convention_C
4570 or else Convention (T) = Convention_CPP
4571 then
4572 return True;
4573 end if;
4575 exit when T = Etype (T);
4577 T := Etype (T);
4578 end loop;
4580 return False;
4581 end Is_C_Derivation;
4583 -- Local variables
4585 Typ : constant Entity_Id := Entity (N);
4586 Root : constant Entity_Id := Root_Type (Typ);
4588 -- Start of processing for Expand_Freeze_Class_Wide_Type
4590 begin
4591 -- Certain run-time configurations and targets do not provide support
4592 -- for controlled types.
4594 if Restriction_Active (No_Finalization) then
4595 return;
4597 -- Do not create TSS routine Finalize_Address when dispatching calls are
4598 -- disabled since the core of the routine is a dispatching call.
4600 elsif Restriction_Active (No_Dispatching_Calls) then
4601 return;
4603 -- Do not create TSS routine Finalize_Address for concurrent class-wide
4604 -- types. Ignore C, C++, CIL and Java types since it is assumed that the
4605 -- non-Ada side will handle their destruction.
4607 elsif Is_Concurrent_Type (Root)
4608 or else Is_C_Derivation (Root)
4609 or else Convention (Typ) = Convention_CPP
4610 then
4611 return;
4613 -- Do not create TSS routine Finalize_Address when compiling in CodePeer
4614 -- mode since the routine contains an Unchecked_Conversion.
4616 elsif CodePeer_Mode then
4617 return;
4618 end if;
4620 -- Create the body of TSS primitive Finalize_Address. This automatically
4621 -- sets the TSS entry for the class-wide type.
4623 Make_Finalize_Address_Body (Typ);
4624 end Expand_Freeze_Class_Wide_Type;
4626 ------------------------------------
4627 -- Expand_Freeze_Enumeration_Type --
4628 ------------------------------------
4630 procedure Expand_Freeze_Enumeration_Type (N : Node_Id) is
4631 Typ : constant Entity_Id := Entity (N);
4632 Loc : constant Source_Ptr := Sloc (Typ);
4634 Arr : Entity_Id;
4635 Ent : Entity_Id;
4636 Fent : Entity_Id;
4637 Is_Contiguous : Boolean;
4638 Ityp : Entity_Id;
4639 Last_Repval : Uint;
4640 Lst : List_Id;
4641 Num : Nat;
4642 Pos_Expr : Node_Id;
4644 Func : Entity_Id;
4645 pragma Warnings (Off, Func);
4647 begin
4648 -- Various optimizations possible if given representation is contiguous
4650 Is_Contiguous := True;
4652 Ent := First_Literal (Typ);
4653 Last_Repval := Enumeration_Rep (Ent);
4655 Next_Literal (Ent);
4656 while Present (Ent) loop
4657 if Enumeration_Rep (Ent) - Last_Repval /= 1 then
4658 Is_Contiguous := False;
4659 exit;
4660 else
4661 Last_Repval := Enumeration_Rep (Ent);
4662 end if;
4664 Next_Literal (Ent);
4665 end loop;
4667 if Is_Contiguous then
4668 Set_Has_Contiguous_Rep (Typ);
4669 Ent := First_Literal (Typ);
4670 Num := 1;
4671 Lst := New_List (New_Occurrence_Of (Ent, Sloc (Ent)));
4673 else
4674 -- Build list of literal references
4676 Lst := New_List;
4677 Num := 0;
4679 Ent := First_Literal (Typ);
4680 while Present (Ent) loop
4681 Append_To (Lst, New_Occurrence_Of (Ent, Sloc (Ent)));
4682 Num := Num + 1;
4683 Next_Literal (Ent);
4684 end loop;
4685 end if;
4687 -- Now build an array declaration
4689 -- typA : array (Natural range 0 .. num - 1) of ctype :=
4690 -- (v, v, v, v, v, ....)
4692 -- where ctype is the corresponding integer type. If the representation
4693 -- is contiguous, we only keep the first literal, which provides the
4694 -- offset for Pos_To_Rep computations.
4696 Arr :=
4697 Make_Defining_Identifier (Loc,
4698 Chars => New_External_Name (Chars (Typ), 'A'));
4700 Append_Freeze_Action (Typ,
4701 Make_Object_Declaration (Loc,
4702 Defining_Identifier => Arr,
4703 Constant_Present => True,
4705 Object_Definition =>
4706 Make_Constrained_Array_Definition (Loc,
4707 Discrete_Subtype_Definitions => New_List (
4708 Make_Subtype_Indication (Loc,
4709 Subtype_Mark => New_Occurrence_Of (Standard_Natural, Loc),
4710 Constraint =>
4711 Make_Range_Constraint (Loc,
4712 Range_Expression =>
4713 Make_Range (Loc,
4714 Low_Bound =>
4715 Make_Integer_Literal (Loc, 0),
4716 High_Bound =>
4717 Make_Integer_Literal (Loc, Num - 1))))),
4719 Component_Definition =>
4720 Make_Component_Definition (Loc,
4721 Aliased_Present => False,
4722 Subtype_Indication => New_Occurrence_Of (Typ, Loc))),
4724 Expression =>
4725 Make_Aggregate (Loc,
4726 Expressions => Lst)));
4728 Set_Enum_Pos_To_Rep (Typ, Arr);
4730 -- Now we build the function that converts representation values to
4731 -- position values. This function has the form:
4733 -- function _Rep_To_Pos (A : etype; F : Boolean) return Integer is
4734 -- begin
4735 -- case ityp!(A) is
4736 -- when enum-lit'Enum_Rep => return posval;
4737 -- when enum-lit'Enum_Rep => return posval;
4738 -- ...
4739 -- when others =>
4740 -- [raise Constraint_Error when F "invalid data"]
4741 -- return -1;
4742 -- end case;
4743 -- end;
4745 -- Note: the F parameter determines whether the others case (no valid
4746 -- representation) raises Constraint_Error or returns a unique value
4747 -- of minus one. The latter case is used, e.g. in 'Valid code.
4749 -- Note: the reason we use Enum_Rep values in the case here is to avoid
4750 -- the code generator making inappropriate assumptions about the range
4751 -- of the values in the case where the value is invalid. ityp is a
4752 -- signed or unsigned integer type of appropriate width.
4754 -- Note: if exceptions are not supported, then we suppress the raise
4755 -- and return -1 unconditionally (this is an erroneous program in any
4756 -- case and there is no obligation to raise Constraint_Error here). We
4757 -- also do this if pragma Restrictions (No_Exceptions) is active.
4759 -- Is this right??? What about No_Exception_Propagation???
4761 -- Representations are signed
4763 if Enumeration_Rep (First_Literal (Typ)) < 0 then
4765 -- The underlying type is signed. Reset the Is_Unsigned_Type
4766 -- explicitly, because it might have been inherited from
4767 -- parent type.
4769 Set_Is_Unsigned_Type (Typ, False);
4771 if Esize (Typ) <= Standard_Integer_Size then
4772 Ityp := Standard_Integer;
4773 else
4774 Ityp := Universal_Integer;
4775 end if;
4777 -- Representations are unsigned
4779 else
4780 if Esize (Typ) <= Standard_Integer_Size then
4781 Ityp := RTE (RE_Unsigned);
4782 else
4783 Ityp := RTE (RE_Long_Long_Unsigned);
4784 end if;
4785 end if;
4787 -- The body of the function is a case statement. First collect case
4788 -- alternatives, or optimize the contiguous case.
4790 Lst := New_List;
4792 -- If representation is contiguous, Pos is computed by subtracting
4793 -- the representation of the first literal.
4795 if Is_Contiguous then
4796 Ent := First_Literal (Typ);
4798 if Enumeration_Rep (Ent) = Last_Repval then
4800 -- Another special case: for a single literal, Pos is zero
4802 Pos_Expr := Make_Integer_Literal (Loc, Uint_0);
4804 else
4805 Pos_Expr :=
4806 Convert_To (Standard_Integer,
4807 Make_Op_Subtract (Loc,
4808 Left_Opnd =>
4809 Unchecked_Convert_To
4810 (Ityp, Make_Identifier (Loc, Name_uA)),
4811 Right_Opnd =>
4812 Make_Integer_Literal (Loc,
4813 Intval => Enumeration_Rep (First_Literal (Typ)))));
4814 end if;
4816 Append_To (Lst,
4817 Make_Case_Statement_Alternative (Loc,
4818 Discrete_Choices => New_List (
4819 Make_Range (Sloc (Enumeration_Rep_Expr (Ent)),
4820 Low_Bound =>
4821 Make_Integer_Literal (Loc,
4822 Intval => Enumeration_Rep (Ent)),
4823 High_Bound =>
4824 Make_Integer_Literal (Loc, Intval => Last_Repval))),
4826 Statements => New_List (
4827 Make_Simple_Return_Statement (Loc,
4828 Expression => Pos_Expr))));
4830 else
4831 Ent := First_Literal (Typ);
4832 while Present (Ent) loop
4833 Append_To (Lst,
4834 Make_Case_Statement_Alternative (Loc,
4835 Discrete_Choices => New_List (
4836 Make_Integer_Literal (Sloc (Enumeration_Rep_Expr (Ent)),
4837 Intval => Enumeration_Rep (Ent))),
4839 Statements => New_List (
4840 Make_Simple_Return_Statement (Loc,
4841 Expression =>
4842 Make_Integer_Literal (Loc,
4843 Intval => Enumeration_Pos (Ent))))));
4845 Next_Literal (Ent);
4846 end loop;
4847 end if;
4849 -- In normal mode, add the others clause with the test.
4850 -- If Predicates_Ignored is True, validity checks do not apply to
4851 -- the subtype.
4853 if not No_Exception_Handlers_Set
4854 and then not Predicates_Ignored (Typ)
4855 then
4856 Append_To (Lst,
4857 Make_Case_Statement_Alternative (Loc,
4858 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
4859 Statements => New_List (
4860 Make_Raise_Constraint_Error (Loc,
4861 Condition => Make_Identifier (Loc, Name_uF),
4862 Reason => CE_Invalid_Data),
4863 Make_Simple_Return_Statement (Loc,
4864 Expression => Make_Integer_Literal (Loc, -1)))));
4866 -- If either of the restrictions No_Exceptions_Handlers/Propagation is
4867 -- active then return -1 (we cannot usefully raise Constraint_Error in
4868 -- this case). See description above for further details.
4870 else
4871 Append_To (Lst,
4872 Make_Case_Statement_Alternative (Loc,
4873 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
4874 Statements => New_List (
4875 Make_Simple_Return_Statement (Loc,
4876 Expression => Make_Integer_Literal (Loc, -1)))));
4877 end if;
4879 -- Now we can build the function body
4881 Fent :=
4882 Make_Defining_Identifier (Loc, Make_TSS_Name (Typ, TSS_Rep_To_Pos));
4884 Func :=
4885 Make_Subprogram_Body (Loc,
4886 Specification =>
4887 Make_Function_Specification (Loc,
4888 Defining_Unit_Name => Fent,
4889 Parameter_Specifications => New_List (
4890 Make_Parameter_Specification (Loc,
4891 Defining_Identifier =>
4892 Make_Defining_Identifier (Loc, Name_uA),
4893 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
4894 Make_Parameter_Specification (Loc,
4895 Defining_Identifier =>
4896 Make_Defining_Identifier (Loc, Name_uF),
4897 Parameter_Type =>
4898 New_Occurrence_Of (Standard_Boolean, Loc))),
4900 Result_Definition => New_Occurrence_Of (Standard_Integer, Loc)),
4902 Declarations => Empty_List,
4904 Handled_Statement_Sequence =>
4905 Make_Handled_Sequence_Of_Statements (Loc,
4906 Statements => New_List (
4907 Make_Case_Statement (Loc,
4908 Expression =>
4909 Unchecked_Convert_To
4910 (Ityp, Make_Identifier (Loc, Name_uA)),
4911 Alternatives => Lst))));
4913 Set_TSS (Typ, Fent);
4915 -- Set Pure flag (it will be reset if the current context is not Pure).
4916 -- We also pretend there was a pragma Pure_Function so that for purposes
4917 -- of optimization and constant-folding, we will consider the function
4918 -- Pure even if we are not in a Pure context).
4920 Set_Is_Pure (Fent);
4921 Set_Has_Pragma_Pure_Function (Fent);
4923 -- Unless we are in -gnatD mode, where we are debugging generated code,
4924 -- this is an internal entity for which we don't need debug info.
4926 if not Debug_Generated_Code then
4927 Set_Debug_Info_Off (Fent);
4928 end if;
4930 Set_Is_Inlined (Fent);
4932 exception
4933 when RE_Not_Available =>
4934 return;
4935 end Expand_Freeze_Enumeration_Type;
4937 -------------------------------
4938 -- Expand_Freeze_Record_Type --
4939 -------------------------------
4941 procedure Expand_Freeze_Record_Type (N : Node_Id) is
4942 Typ : constant Node_Id := Entity (N);
4943 Typ_Decl : constant Node_Id := Parent (Typ);
4945 Comp : Entity_Id;
4946 Comp_Typ : Entity_Id;
4947 Predef_List : List_Id;
4949 Wrapper_Decl_List : List_Id := No_List;
4950 Wrapper_Body_List : List_Id := No_List;
4952 Renamed_Eq : Node_Id := Empty;
4953 -- Defining unit name for the predefined equality function in the case
4954 -- where the type has a primitive operation that is a renaming of
4955 -- predefined equality (but only if there is also an overriding
4956 -- user-defined equality function). Used to pass this entity from
4957 -- Make_Predefined_Primitive_Specs to Predefined_Primitive_Bodies.
4959 -- Start of processing for Expand_Freeze_Record_Type
4961 begin
4962 -- Build discriminant checking functions if not a derived type (for
4963 -- derived types that are not tagged types, always use the discriminant
4964 -- checking functions of the parent type). However, for untagged types
4965 -- the derivation may have taken place before the parent was frozen, so
4966 -- we copy explicitly the discriminant checking functions from the
4967 -- parent into the components of the derived type.
4969 if not Is_Derived_Type (Typ)
4970 or else Has_New_Non_Standard_Rep (Typ)
4971 or else Is_Tagged_Type (Typ)
4972 then
4973 Build_Discr_Checking_Funcs (Typ_Decl);
4975 elsif Is_Derived_Type (Typ)
4976 and then not Is_Tagged_Type (Typ)
4978 -- If we have a derived Unchecked_Union, we do not inherit the
4979 -- discriminant checking functions from the parent type since the
4980 -- discriminants are non existent.
4982 and then not Is_Unchecked_Union (Typ)
4983 and then Has_Discriminants (Typ)
4984 then
4985 declare
4986 Old_Comp : Entity_Id;
4988 begin
4989 Old_Comp :=
4990 First_Component (Base_Type (Underlying_Type (Etype (Typ))));
4991 Comp := First_Component (Typ);
4992 while Present (Comp) loop
4993 if Ekind (Comp) = E_Component
4994 and then Chars (Comp) = Chars (Old_Comp)
4995 then
4996 Set_Discriminant_Checking_Func
4997 (Comp, Discriminant_Checking_Func (Old_Comp));
4998 end if;
5000 Next_Component (Old_Comp);
5001 Next_Component (Comp);
5002 end loop;
5003 end;
5004 end if;
5006 if Is_Derived_Type (Typ)
5007 and then Is_Limited_Type (Typ)
5008 and then Is_Tagged_Type (Typ)
5009 then
5010 Check_Stream_Attributes (Typ);
5011 end if;
5013 -- Update task, protected, and controlled component flags, because some
5014 -- of the component types may have been private at the point of the
5015 -- record declaration. Detect anonymous access-to-controlled components.
5017 Comp := First_Component (Typ);
5018 while Present (Comp) loop
5019 Comp_Typ := Etype (Comp);
5021 Propagate_Concurrent_Flags (Typ, Comp_Typ);
5023 -- Do not set Has_Controlled_Component on a class-wide equivalent
5024 -- type. See Make_CW_Equivalent_Type.
5026 if not Is_Class_Wide_Equivalent_Type (Typ)
5027 and then
5028 (Has_Controlled_Component (Comp_Typ)
5029 or else (Chars (Comp) /= Name_uParent
5030 and then Is_Controlled (Comp_Typ)))
5031 then
5032 Set_Has_Controlled_Component (Typ);
5033 end if;
5035 Next_Component (Comp);
5036 end loop;
5038 -- Handle constructors of untagged CPP_Class types
5040 if not Is_Tagged_Type (Typ) and then Is_CPP_Class (Typ) then
5041 Set_CPP_Constructors (Typ);
5042 end if;
5044 -- Creation of the Dispatch Table. Note that a Dispatch Table is built
5045 -- for regular tagged types as well as for Ada types deriving from a C++
5046 -- Class, but not for tagged types directly corresponding to C++ classes
5047 -- In the later case we assume that it is created in the C++ side and we
5048 -- just use it.
5050 if Is_Tagged_Type (Typ) then
5052 -- Add the _Tag component
5054 if Underlying_Type (Etype (Typ)) = Typ then
5055 Expand_Tagged_Root (Typ);
5056 end if;
5058 if Is_CPP_Class (Typ) then
5059 Set_All_DT_Position (Typ);
5061 -- Create the tag entities with a minimum decoration
5063 if Tagged_Type_Expansion then
5064 Append_Freeze_Actions (Typ, Make_Tags (Typ));
5065 end if;
5067 Set_CPP_Constructors (Typ);
5069 else
5070 if not Building_Static_DT (Typ) then
5072 -- Usually inherited primitives are not delayed but the first
5073 -- Ada extension of a CPP_Class is an exception since the
5074 -- address of the inherited subprogram has to be inserted in
5075 -- the new Ada Dispatch Table and this is a freezing action.
5077 -- Similarly, if this is an inherited operation whose parent is
5078 -- not frozen yet, it is not in the DT of the parent, and we
5079 -- generate an explicit freeze node for the inherited operation
5080 -- so it is properly inserted in the DT of the current type.
5082 declare
5083 Elmt : Elmt_Id;
5084 Subp : Entity_Id;
5086 begin
5087 Elmt := First_Elmt (Primitive_Operations (Typ));
5088 while Present (Elmt) loop
5089 Subp := Node (Elmt);
5091 if Present (Alias (Subp)) then
5092 if Is_CPP_Class (Etype (Typ)) then
5093 Set_Has_Delayed_Freeze (Subp);
5095 elsif Has_Delayed_Freeze (Alias (Subp))
5096 and then not Is_Frozen (Alias (Subp))
5097 then
5098 Set_Is_Frozen (Subp, False);
5099 Set_Has_Delayed_Freeze (Subp);
5100 end if;
5101 end if;
5103 Next_Elmt (Elmt);
5104 end loop;
5105 end;
5106 end if;
5108 -- Unfreeze momentarily the type to add the predefined primitives
5109 -- operations. The reason we unfreeze is so that these predefined
5110 -- operations will indeed end up as primitive operations (which
5111 -- must be before the freeze point).
5113 Set_Is_Frozen (Typ, False);
5115 -- Do not add the spec of predefined primitives in case of
5116 -- CPP tagged type derivations that have convention CPP.
5118 if Is_CPP_Class (Root_Type (Typ))
5119 and then Convention (Typ) = Convention_CPP
5120 then
5121 null;
5123 -- Do not add the spec of the predefined primitives if we are
5124 -- compiling under restriction No_Dispatching_Calls.
5126 elsif not Restriction_Active (No_Dispatching_Calls) then
5127 Make_Predefined_Primitive_Specs (Typ, Predef_List, Renamed_Eq);
5128 Insert_List_Before_And_Analyze (N, Predef_List);
5129 end if;
5131 -- Ada 2005 (AI-391): For a nonabstract null extension, create
5132 -- wrapper functions for each nonoverridden inherited function
5133 -- with a controlling result of the type. The wrapper for such
5134 -- a function returns an extension aggregate that invokes the
5135 -- parent function.
5137 if Ada_Version >= Ada_2005
5138 and then not Is_Abstract_Type (Typ)
5139 and then Is_Null_Extension (Typ)
5140 then
5141 Make_Controlling_Function_Wrappers
5142 (Typ, Wrapper_Decl_List, Wrapper_Body_List);
5143 Insert_List_Before_And_Analyze (N, Wrapper_Decl_List);
5144 end if;
5146 -- Ada 2005 (AI-251): For a nonabstract type extension, build
5147 -- null procedure declarations for each set of homographic null
5148 -- procedures that are inherited from interface types but not
5149 -- overridden. This is done to ensure that the dispatch table
5150 -- entry associated with such null primitives are properly filled.
5152 if Ada_Version >= Ada_2005
5153 and then Etype (Typ) /= Typ
5154 and then not Is_Abstract_Type (Typ)
5155 and then Has_Interfaces (Typ)
5156 then
5157 Insert_Actions (N, Make_Null_Procedure_Specs (Typ));
5158 end if;
5160 Set_Is_Frozen (Typ);
5162 if not Is_Derived_Type (Typ)
5163 or else Is_Tagged_Type (Etype (Typ))
5164 then
5165 Set_All_DT_Position (Typ);
5167 -- If this is a type derived from an untagged private type whose
5168 -- full view is tagged, the type is marked tagged for layout
5169 -- reasons, but it has no dispatch table.
5171 elsif Is_Derived_Type (Typ)
5172 and then Is_Private_Type (Etype (Typ))
5173 and then not Is_Tagged_Type (Etype (Typ))
5174 then
5175 return;
5176 end if;
5178 -- Create and decorate the tags. Suppress their creation when
5179 -- not Tagged_Type_Expansion because the dispatching mechanism is
5180 -- handled internally by the virtual target.
5182 if Tagged_Type_Expansion then
5183 Append_Freeze_Actions (Typ, Make_Tags (Typ));
5185 -- Generate dispatch table of locally defined tagged type.
5186 -- Dispatch tables of library level tagged types are built
5187 -- later (see Analyze_Declarations).
5189 if not Building_Static_DT (Typ) then
5190 Append_Freeze_Actions (Typ, Make_DT (Typ));
5191 end if;
5192 end if;
5194 -- If the type has unknown discriminants, propagate dispatching
5195 -- information to its underlying record view, which does not get
5196 -- its own dispatch table.
5198 if Is_Derived_Type (Typ)
5199 and then Has_Unknown_Discriminants (Typ)
5200 and then Present (Underlying_Record_View (Typ))
5201 then
5202 declare
5203 Rep : constant Entity_Id := Underlying_Record_View (Typ);
5204 begin
5205 Set_Access_Disp_Table
5206 (Rep, Access_Disp_Table (Typ));
5207 Set_Dispatch_Table_Wrappers
5208 (Rep, Dispatch_Table_Wrappers (Typ));
5209 Set_Direct_Primitive_Operations
5210 (Rep, Direct_Primitive_Operations (Typ));
5211 end;
5212 end if;
5214 -- Make sure that the primitives Initialize, Adjust and Finalize
5215 -- are Frozen before other TSS subprograms. We don't want them
5216 -- Frozen inside.
5218 if Is_Controlled (Typ) then
5219 if not Is_Limited_Type (Typ) then
5220 Append_Freeze_Actions (Typ,
5221 Freeze_Entity (Find_Prim_Op (Typ, Name_Adjust), Typ));
5222 end if;
5224 Append_Freeze_Actions (Typ,
5225 Freeze_Entity (Find_Prim_Op (Typ, Name_Initialize), Typ));
5227 Append_Freeze_Actions (Typ,
5228 Freeze_Entity (Find_Prim_Op (Typ, Name_Finalize), Typ));
5229 end if;
5231 -- Freeze rest of primitive operations. There is no need to handle
5232 -- the predefined primitives if we are compiling under restriction
5233 -- No_Dispatching_Calls.
5235 if not Restriction_Active (No_Dispatching_Calls) then
5236 Append_Freeze_Actions (Typ, Predefined_Primitive_Freeze (Typ));
5237 end if;
5238 end if;
5240 -- In the untagged case, ever since Ada 83 an equality function must
5241 -- be provided for variant records that are not unchecked unions.
5242 -- In Ada 2012 the equality function composes, and thus must be built
5243 -- explicitly just as for tagged records.
5245 elsif Has_Discriminants (Typ)
5246 and then not Is_Limited_Type (Typ)
5247 then
5248 declare
5249 Comps : constant Node_Id :=
5250 Component_List (Type_Definition (Typ_Decl));
5251 begin
5252 if Present (Comps)
5253 and then Present (Variant_Part (Comps))
5254 then
5255 Build_Variant_Record_Equality (Typ);
5256 end if;
5257 end;
5259 -- Otherwise create primitive equality operation (AI05-0123)
5261 -- This is done unconditionally to ensure that tools can be linked
5262 -- properly with user programs compiled with older language versions.
5263 -- In addition, this is needed because "=" composes for bounded strings
5264 -- in all language versions (see Exp_Ch4.Expand_Composite_Equality).
5266 elsif Comes_From_Source (Typ)
5267 and then Convention (Typ) = Convention_Ada
5268 and then not Is_Limited_Type (Typ)
5269 then
5270 Build_Untagged_Equality (Typ);
5271 end if;
5273 -- Before building the record initialization procedure, if we are
5274 -- dealing with a concurrent record value type, then we must go through
5275 -- the discriminants, exchanging discriminals between the concurrent
5276 -- type and the concurrent record value type. See the section "Handling
5277 -- of Discriminants" in the Einfo spec for details.
5279 if Is_Concurrent_Record_Type (Typ)
5280 and then Has_Discriminants (Typ)
5281 then
5282 declare
5283 Ctyp : constant Entity_Id :=
5284 Corresponding_Concurrent_Type (Typ);
5285 Conc_Discr : Entity_Id;
5286 Rec_Discr : Entity_Id;
5287 Temp : Entity_Id;
5289 begin
5290 Conc_Discr := First_Discriminant (Ctyp);
5291 Rec_Discr := First_Discriminant (Typ);
5292 while Present (Conc_Discr) loop
5293 Temp := Discriminal (Conc_Discr);
5294 Set_Discriminal (Conc_Discr, Discriminal (Rec_Discr));
5295 Set_Discriminal (Rec_Discr, Temp);
5297 Set_Discriminal_Link (Discriminal (Conc_Discr), Conc_Discr);
5298 Set_Discriminal_Link (Discriminal (Rec_Discr), Rec_Discr);
5300 Next_Discriminant (Conc_Discr);
5301 Next_Discriminant (Rec_Discr);
5302 end loop;
5303 end;
5304 end if;
5306 if Has_Controlled_Component (Typ) then
5307 Build_Controlling_Procs (Typ);
5308 end if;
5310 Adjust_Discriminants (Typ);
5312 -- Do not need init for interfaces on virtual targets since they're
5313 -- abstract.
5315 if Tagged_Type_Expansion or else not Is_Interface (Typ) then
5316 Build_Record_Init_Proc (Typ_Decl, Typ);
5317 end if;
5319 -- For tagged type that are not interfaces, build bodies of primitive
5320 -- operations. Note: do this after building the record initialization
5321 -- procedure, since the primitive operations may need the initialization
5322 -- routine. There is no need to add predefined primitives of interfaces
5323 -- because all their predefined primitives are abstract.
5325 if Is_Tagged_Type (Typ) and then not Is_Interface (Typ) then
5327 -- Do not add the body of predefined primitives in case of CPP tagged
5328 -- type derivations that have convention CPP.
5330 if Is_CPP_Class (Root_Type (Typ))
5331 and then Convention (Typ) = Convention_CPP
5332 then
5333 null;
5335 -- Do not add the body of the predefined primitives if we are
5336 -- compiling under restriction No_Dispatching_Calls or if we are
5337 -- compiling a CPP tagged type.
5339 elsif not Restriction_Active (No_Dispatching_Calls) then
5341 -- Create the body of TSS primitive Finalize_Address. This must
5342 -- be done before the bodies of all predefined primitives are
5343 -- created. If Typ is limited, Stream_Input and Stream_Read may
5344 -- produce build-in-place allocations and for those the expander
5345 -- needs Finalize_Address.
5347 Make_Finalize_Address_Body (Typ);
5348 Predef_List := Predefined_Primitive_Bodies (Typ, Renamed_Eq);
5349 Append_Freeze_Actions (Typ, Predef_List);
5350 end if;
5352 -- Ada 2005 (AI-391): If any wrappers were created for nonoverridden
5353 -- inherited functions, then add their bodies to the freeze actions.
5355 if Present (Wrapper_Body_List) then
5356 Append_Freeze_Actions (Typ, Wrapper_Body_List);
5357 end if;
5359 -- Create extra formals for the primitive operations of the type.
5360 -- This must be done before analyzing the body of the initialization
5361 -- procedure, because a self-referential type might call one of these
5362 -- primitives in the body of the init_proc itself.
5364 declare
5365 Elmt : Elmt_Id;
5366 Subp : Entity_Id;
5368 begin
5369 Elmt := First_Elmt (Primitive_Operations (Typ));
5370 while Present (Elmt) loop
5371 Subp := Node (Elmt);
5372 if not Has_Foreign_Convention (Subp)
5373 and then not Is_Predefined_Dispatching_Operation (Subp)
5374 then
5375 Create_Extra_Formals (Subp);
5376 end if;
5378 Next_Elmt (Elmt);
5379 end loop;
5380 end;
5381 end if;
5382 end Expand_Freeze_Record_Type;
5384 ------------------------------------
5385 -- Expand_N_Full_Type_Declaration --
5386 ------------------------------------
5388 procedure Expand_N_Full_Type_Declaration (N : Node_Id) is
5389 procedure Build_Master (Ptr_Typ : Entity_Id);
5390 -- Create the master associated with Ptr_Typ
5392 ------------------
5393 -- Build_Master --
5394 ------------------
5396 procedure Build_Master (Ptr_Typ : Entity_Id) is
5397 Desig_Typ : Entity_Id := Designated_Type (Ptr_Typ);
5399 begin
5400 -- If the designated type is an incomplete view coming from a
5401 -- limited-with'ed package, we need to use the nonlimited view in
5402 -- case it has tasks.
5404 if Ekind (Desig_Typ) in Incomplete_Kind
5405 and then Present (Non_Limited_View (Desig_Typ))
5406 then
5407 Desig_Typ := Non_Limited_View (Desig_Typ);
5408 end if;
5410 -- Anonymous access types are created for the components of the
5411 -- record parameter for an entry declaration. No master is created
5412 -- for such a type.
5414 if Comes_From_Source (N) and then Has_Task (Desig_Typ) then
5415 Build_Master_Entity (Ptr_Typ);
5416 Build_Master_Renaming (Ptr_Typ);
5418 -- Create a class-wide master because a Master_Id must be generated
5419 -- for access-to-limited-class-wide types whose root may be extended
5420 -- with task components.
5422 -- Note: This code covers access-to-limited-interfaces because they
5423 -- can be used to reference tasks implementing them.
5425 elsif Is_Limited_Class_Wide_Type (Desig_Typ)
5426 and then Tasking_Allowed
5427 then
5428 Build_Class_Wide_Master (Ptr_Typ);
5429 end if;
5430 end Build_Master;
5432 -- Local declarations
5434 Def_Id : constant Entity_Id := Defining_Identifier (N);
5435 B_Id : constant Entity_Id := Base_Type (Def_Id);
5436 FN : Node_Id;
5437 Par_Id : Entity_Id;
5439 -- Start of processing for Expand_N_Full_Type_Declaration
5441 begin
5442 if Is_Access_Type (Def_Id) then
5443 Build_Master (Def_Id);
5445 if Ekind (Def_Id) = E_Access_Protected_Subprogram_Type then
5446 Expand_Access_Protected_Subprogram_Type (N);
5447 end if;
5449 -- Array of anonymous access-to-task pointers
5451 elsif Ada_Version >= Ada_2005
5452 and then Is_Array_Type (Def_Id)
5453 and then Is_Access_Type (Component_Type (Def_Id))
5454 and then Ekind (Component_Type (Def_Id)) = E_Anonymous_Access_Type
5455 then
5456 Build_Master (Component_Type (Def_Id));
5458 elsif Has_Task (Def_Id) then
5459 Expand_Previous_Access_Type (Def_Id);
5461 -- Check the components of a record type or array of records for
5462 -- anonymous access-to-task pointers.
5464 elsif Ada_Version >= Ada_2005
5465 and then (Is_Record_Type (Def_Id)
5466 or else
5467 (Is_Array_Type (Def_Id)
5468 and then Is_Record_Type (Component_Type (Def_Id))))
5469 then
5470 declare
5471 Comp : Entity_Id;
5472 First : Boolean;
5473 M_Id : Entity_Id;
5474 Typ : Entity_Id;
5476 begin
5477 if Is_Array_Type (Def_Id) then
5478 Comp := First_Entity (Component_Type (Def_Id));
5479 else
5480 Comp := First_Entity (Def_Id);
5481 end if;
5483 -- Examine all components looking for anonymous access-to-task
5484 -- types.
5486 First := True;
5487 while Present (Comp) loop
5488 Typ := Etype (Comp);
5490 if Ekind (Typ) = E_Anonymous_Access_Type
5491 and then Has_Task (Available_View (Designated_Type (Typ)))
5492 and then No (Master_Id (Typ))
5493 then
5494 -- Ensure that the record or array type have a _master
5496 if First then
5497 Build_Master_Entity (Def_Id);
5498 Build_Master_Renaming (Typ);
5499 M_Id := Master_Id (Typ);
5501 First := False;
5503 -- Reuse the same master to service any additional types
5505 else
5506 Set_Master_Id (Typ, M_Id);
5507 end if;
5508 end if;
5510 Next_Entity (Comp);
5511 end loop;
5512 end;
5513 end if;
5515 Par_Id := Etype (B_Id);
5517 -- The parent type is private then we need to inherit any TSS operations
5518 -- from the full view.
5520 if Ekind (Par_Id) in Private_Kind
5521 and then Present (Full_View (Par_Id))
5522 then
5523 Par_Id := Base_Type (Full_View (Par_Id));
5524 end if;
5526 if Nkind (Type_Definition (Original_Node (N))) =
5527 N_Derived_Type_Definition
5528 and then not Is_Tagged_Type (Def_Id)
5529 and then Present (Freeze_Node (Par_Id))
5530 and then Present (TSS_Elist (Freeze_Node (Par_Id)))
5531 then
5532 Ensure_Freeze_Node (B_Id);
5533 FN := Freeze_Node (B_Id);
5535 if No (TSS_Elist (FN)) then
5536 Set_TSS_Elist (FN, New_Elmt_List);
5537 end if;
5539 declare
5540 T_E : constant Elist_Id := TSS_Elist (FN);
5541 Elmt : Elmt_Id;
5543 begin
5544 Elmt := First_Elmt (TSS_Elist (Freeze_Node (Par_Id)));
5545 while Present (Elmt) loop
5546 if Chars (Node (Elmt)) /= Name_uInit then
5547 Append_Elmt (Node (Elmt), T_E);
5548 end if;
5550 Next_Elmt (Elmt);
5551 end loop;
5553 -- If the derived type itself is private with a full view, then
5554 -- associate the full view with the inherited TSS_Elist as well.
5556 if Ekind (B_Id) in Private_Kind
5557 and then Present (Full_View (B_Id))
5558 then
5559 Ensure_Freeze_Node (Base_Type (Full_View (B_Id)));
5560 Set_TSS_Elist
5561 (Freeze_Node (Base_Type (Full_View (B_Id))), TSS_Elist (FN));
5562 end if;
5563 end;
5564 end if;
5565 end Expand_N_Full_Type_Declaration;
5567 ---------------------------------
5568 -- Expand_N_Object_Declaration --
5569 ---------------------------------
5571 procedure Expand_N_Object_Declaration (N : Node_Id) is
5572 Loc : constant Source_Ptr := Sloc (N);
5573 Def_Id : constant Entity_Id := Defining_Identifier (N);
5574 Expr : constant Node_Id := Expression (N);
5575 Obj_Def : constant Node_Id := Object_Definition (N);
5576 Typ : constant Entity_Id := Etype (Def_Id);
5577 Base_Typ : constant Entity_Id := Base_Type (Typ);
5578 Expr_Q : Node_Id;
5580 function Build_Equivalent_Aggregate return Boolean;
5581 -- If the object has a constrained discriminated type and no initial
5582 -- value, it may be possible to build an equivalent aggregate instead,
5583 -- and prevent an actual call to the initialization procedure.
5585 procedure Check_Large_Modular_Array;
5586 -- Check that the size of the array can be computed without overflow,
5587 -- and generate a Storage_Error otherwise. This is only relevant for
5588 -- array types whose index in a (mod 2**64) type, where wrap-around
5589 -- arithmetic might yield a meaningless value for the length of the
5590 -- array, or its corresponding attribute.
5592 procedure Count_Default_Sized_Task_Stacks
5593 (Typ : Entity_Id;
5594 Pri_Stacks : out Int;
5595 Sec_Stacks : out Int);
5596 -- Count the number of default-sized primary and secondary task stacks
5597 -- required for task objects contained within type Typ. If the number of
5598 -- task objects contained within the type is not known at compile time
5599 -- the procedure will return the stack counts of zero.
5601 procedure Default_Initialize_Object (After : Node_Id);
5602 -- Generate all default initialization actions for object Def_Id. Any
5603 -- new code is inserted after node After.
5605 function Rewrite_As_Renaming return Boolean;
5606 -- Indicate whether to rewrite a declaration with initialization into an
5607 -- object renaming declaration (see below).
5609 --------------------------------
5610 -- Build_Equivalent_Aggregate --
5611 --------------------------------
5613 function Build_Equivalent_Aggregate return Boolean is
5614 Aggr : Node_Id;
5615 Comp : Entity_Id;
5616 Discr : Elmt_Id;
5617 Full_Type : Entity_Id;
5619 begin
5620 Full_Type := Typ;
5622 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
5623 Full_Type := Full_View (Typ);
5624 end if;
5626 -- Only perform this transformation if Elaboration_Code is forbidden
5627 -- or undesirable, and if this is a global entity of a constrained
5628 -- record type.
5630 -- If Initialize_Scalars might be active this transformation cannot
5631 -- be performed either, because it will lead to different semantics
5632 -- or because elaboration code will in fact be created.
5634 if Ekind (Full_Type) /= E_Record_Subtype
5635 or else not Has_Discriminants (Full_Type)
5636 or else not Is_Constrained (Full_Type)
5637 or else Is_Controlled (Full_Type)
5638 or else Is_Limited_Type (Full_Type)
5639 or else not Restriction_Active (No_Initialize_Scalars)
5640 then
5641 return False;
5642 end if;
5644 if Ekind (Current_Scope) = E_Package
5645 and then
5646 (Restriction_Active (No_Elaboration_Code)
5647 or else Is_Preelaborated (Current_Scope))
5648 then
5649 -- Building a static aggregate is possible if the discriminants
5650 -- have static values and the other components have static
5651 -- defaults or none.
5653 Discr := First_Elmt (Discriminant_Constraint (Full_Type));
5654 while Present (Discr) loop
5655 if not Is_OK_Static_Expression (Node (Discr)) then
5656 return False;
5657 end if;
5659 Next_Elmt (Discr);
5660 end loop;
5662 -- Check that initialized components are OK, and that non-
5663 -- initialized components do not require a call to their own
5664 -- initialization procedure.
5666 Comp := First_Component (Full_Type);
5667 while Present (Comp) loop
5668 if Ekind (Comp) = E_Component
5669 and then Present (Expression (Parent (Comp)))
5670 and then
5671 not Is_OK_Static_Expression (Expression (Parent (Comp)))
5672 then
5673 return False;
5675 elsif Has_Non_Null_Base_Init_Proc (Etype (Comp)) then
5676 return False;
5678 end if;
5680 Next_Component (Comp);
5681 end loop;
5683 -- Everything is static, assemble the aggregate, discriminant
5684 -- values first.
5686 Aggr :=
5687 Make_Aggregate (Loc,
5688 Expressions => New_List,
5689 Component_Associations => New_List);
5691 Discr := First_Elmt (Discriminant_Constraint (Full_Type));
5692 while Present (Discr) loop
5693 Append_To (Expressions (Aggr), New_Copy (Node (Discr)));
5694 Next_Elmt (Discr);
5695 end loop;
5697 -- Now collect values of initialized components
5699 Comp := First_Component (Full_Type);
5700 while Present (Comp) loop
5701 if Ekind (Comp) = E_Component
5702 and then Present (Expression (Parent (Comp)))
5703 then
5704 Append_To (Component_Associations (Aggr),
5705 Make_Component_Association (Loc,
5706 Choices => New_List (New_Occurrence_Of (Comp, Loc)),
5707 Expression => New_Copy_Tree
5708 (Expression (Parent (Comp)))));
5709 end if;
5711 Next_Component (Comp);
5712 end loop;
5714 -- Finally, box-initialize remaining components
5716 Append_To (Component_Associations (Aggr),
5717 Make_Component_Association (Loc,
5718 Choices => New_List (Make_Others_Choice (Loc)),
5719 Expression => Empty));
5720 Set_Box_Present (Last (Component_Associations (Aggr)));
5721 Set_Expression (N, Aggr);
5723 if Typ /= Full_Type then
5724 Analyze_And_Resolve (Aggr, Full_View (Base_Type (Full_Type)));
5725 Rewrite (Aggr, Unchecked_Convert_To (Typ, Aggr));
5726 Analyze_And_Resolve (Aggr, Typ);
5727 else
5728 Analyze_And_Resolve (Aggr, Full_Type);
5729 end if;
5731 return True;
5733 else
5734 return False;
5735 end if;
5736 end Build_Equivalent_Aggregate;
5738 -------------------------------
5739 -- Check_Large_Modular_Array --
5740 -------------------------------
5742 procedure Check_Large_Modular_Array is
5743 Index_Typ : Entity_Id;
5745 begin
5746 if Is_Array_Type (Typ)
5747 and then Is_Modular_Integer_Type (Etype (First_Index (Typ)))
5748 then
5749 -- To prevent arithmetic overflow with large values, we raise
5750 -- Storage_Error under the following guard:
5752 -- (Arr'Last / 2 - Arr'First / 2) > (2 ** 30)
5754 -- This takes care of the boundary case, but it is preferable to
5755 -- use a smaller limit, because even on 64-bit architectures an
5756 -- array of more than 2 ** 30 bytes is likely to raise
5757 -- Storage_Error.
5759 Index_Typ := Etype (First_Index (Typ));
5761 if RM_Size (Index_Typ) = RM_Size (Standard_Long_Long_Integer) then
5762 Insert_Action (N,
5763 Make_Raise_Storage_Error (Loc,
5764 Condition =>
5765 Make_Op_Ge (Loc,
5766 Left_Opnd =>
5767 Make_Op_Subtract (Loc,
5768 Left_Opnd =>
5769 Make_Op_Divide (Loc,
5770 Left_Opnd =>
5771 Make_Attribute_Reference (Loc,
5772 Prefix =>
5773 New_Occurrence_Of (Typ, Loc),
5774 Attribute_Name => Name_Last),
5775 Right_Opnd =>
5776 Make_Integer_Literal (Loc, Uint_2)),
5777 Right_Opnd =>
5778 Make_Op_Divide (Loc,
5779 Left_Opnd =>
5780 Make_Attribute_Reference (Loc,
5781 Prefix =>
5782 New_Occurrence_Of (Typ, Loc),
5783 Attribute_Name => Name_First),
5784 Right_Opnd =>
5785 Make_Integer_Literal (Loc, Uint_2))),
5786 Right_Opnd =>
5787 Make_Integer_Literal (Loc, (Uint_2 ** 30))),
5788 Reason => SE_Object_Too_Large));
5789 end if;
5790 end if;
5791 end Check_Large_Modular_Array;
5793 -------------------------------------
5794 -- Count_Default_Sized_Task_Stacks --
5795 -------------------------------------
5797 procedure Count_Default_Sized_Task_Stacks
5798 (Typ : Entity_Id;
5799 Pri_Stacks : out Int;
5800 Sec_Stacks : out Int)
5802 Component : Entity_Id;
5804 begin
5805 -- To calculate the number of default-sized task stacks required for
5806 -- an object of Typ, a depth-first recursive traversal of the AST
5807 -- from the Typ entity node is undertaken. Only type nodes containing
5808 -- task objects are visited.
5810 Pri_Stacks := 0;
5811 Sec_Stacks := 0;
5813 if not Has_Task (Typ) then
5814 return;
5815 end if;
5817 case Ekind (Typ) is
5818 when E_Task_Subtype
5819 | E_Task_Type
5821 -- A task type is found marking the bottom of the descent. If
5822 -- the type has no representation aspect for the corresponding
5823 -- stack then that stack is using the default size.
5825 if Present (Get_Rep_Item (Typ, Name_Storage_Size)) then
5826 Pri_Stacks := 0;
5827 else
5828 Pri_Stacks := 1;
5829 end if;
5831 if Present (Get_Rep_Item (Typ, Name_Secondary_Stack_Size)) then
5832 Sec_Stacks := 0;
5833 else
5834 Sec_Stacks := 1;
5835 end if;
5837 when E_Array_Subtype
5838 | E_Array_Type
5840 -- First find the number of default stacks contained within an
5841 -- array component.
5843 Count_Default_Sized_Task_Stacks
5844 (Component_Type (Typ),
5845 Pri_Stacks,
5846 Sec_Stacks);
5848 -- Then multiply the result by the size of the array
5850 declare
5851 Quantity : constant Int := Number_Of_Elements_In_Array (Typ);
5852 -- Number_Of_Elements_In_Array is non-trival, consequently
5853 -- its result is captured as an optimization.
5855 begin
5856 Pri_Stacks := Pri_Stacks * Quantity;
5857 Sec_Stacks := Sec_Stacks * Quantity;
5858 end;
5860 when E_Protected_Subtype
5861 | E_Protected_Type
5862 | E_Record_Subtype
5863 | E_Record_Type
5865 Component := First_Component_Or_Discriminant (Typ);
5867 -- Recursively descend each component of the composite type
5868 -- looking for tasks, but only if the component is marked as
5869 -- having a task.
5871 while Present (Component) loop
5872 if Has_Task (Etype (Component)) then
5873 declare
5874 P : Int;
5875 S : Int;
5877 begin
5878 Count_Default_Sized_Task_Stacks
5879 (Etype (Component), P, S);
5880 Pri_Stacks := Pri_Stacks + P;
5881 Sec_Stacks := Sec_Stacks + S;
5882 end;
5883 end if;
5885 Next_Component_Or_Discriminant (Component);
5886 end loop;
5888 when E_Limited_Private_Subtype
5889 | E_Limited_Private_Type
5890 | E_Record_Subtype_With_Private
5891 | E_Record_Type_With_Private
5893 -- Switch to the full view of the private type to continue
5894 -- search.
5896 Count_Default_Sized_Task_Stacks
5897 (Full_View (Typ), Pri_Stacks, Sec_Stacks);
5899 -- Other types should not contain tasks
5901 when others =>
5902 raise Program_Error;
5903 end case;
5904 end Count_Default_Sized_Task_Stacks;
5906 -------------------------------
5907 -- Default_Initialize_Object --
5908 -------------------------------
5910 procedure Default_Initialize_Object (After : Node_Id) is
5911 function New_Object_Reference return Node_Id;
5912 -- Return a new reference to Def_Id with attributes Assignment_OK and
5913 -- Must_Not_Freeze already set.
5915 --------------------------
5916 -- New_Object_Reference --
5917 --------------------------
5919 function New_Object_Reference return Node_Id is
5920 Obj_Ref : constant Node_Id := New_Occurrence_Of (Def_Id, Loc);
5922 begin
5923 -- The call to the type init proc or [Deep_]Finalize must not
5924 -- freeze the related object as the call is internally generated.
5925 -- This way legal rep clauses that apply to the object will not be
5926 -- flagged. Note that the initialization call may be removed if
5927 -- pragma Import is encountered or moved to the freeze actions of
5928 -- the object because of an address clause.
5930 Set_Assignment_OK (Obj_Ref);
5931 Set_Must_Not_Freeze (Obj_Ref);
5933 return Obj_Ref;
5934 end New_Object_Reference;
5936 -- Local variables
5938 Exceptions_OK : constant Boolean :=
5939 not Restriction_Active (No_Exception_Propagation);
5941 Aggr_Init : Node_Id;
5942 Comp_Init : List_Id := No_List;
5943 Fin_Block : Node_Id;
5944 Fin_Call : Node_Id;
5945 Init_Stmts : List_Id := No_List;
5946 Obj_Init : Node_Id := Empty;
5947 Obj_Ref : Node_Id;
5949 -- Start of processing for Default_Initialize_Object
5951 begin
5952 -- Default initialization is suppressed for objects that are already
5953 -- known to be imported (i.e. whose declaration specifies the Import
5954 -- aspect). Note that for objects with a pragma Import, we generate
5955 -- initialization here, and then remove it downstream when processing
5956 -- the pragma. It is also suppressed for variables for which a pragma
5957 -- Suppress_Initialization has been explicitly given
5959 if Is_Imported (Def_Id) or else Suppress_Initialization (Def_Id) then
5960 return;
5962 -- Nothing to do if the object being initialized is of a task type
5963 -- and restriction No_Tasking is in effect, because this is a direct
5964 -- violation of the restriction.
5966 elsif Is_Task_Type (Base_Typ)
5967 and then Restriction_Active (No_Tasking)
5968 then
5969 return;
5970 end if;
5972 -- The expansion performed by this routine is as follows:
5974 -- begin
5975 -- Abort_Defer;
5976 -- Type_Init_Proc (Obj);
5978 -- begin
5979 -- [Deep_]Initialize (Obj);
5981 -- exception
5982 -- when others =>
5983 -- [Deep_]Finalize (Obj, Self => False);
5984 -- raise;
5985 -- end;
5986 -- at end
5987 -- Abort_Undefer_Direct;
5988 -- end;
5990 -- Initialize the components of the object
5992 if Has_Non_Null_Base_Init_Proc (Typ)
5993 and then not No_Initialization (N)
5994 and then not Initialization_Suppressed (Typ)
5995 then
5996 -- Do not initialize the components if No_Default_Initialization
5997 -- applies as the actual restriction check will occur later
5998 -- when the object is frozen as it is not known yet whether the
5999 -- object is imported or not.
6001 if not Restriction_Active (No_Default_Initialization) then
6003 -- If the values of the components are compile-time known, use
6004 -- their prebuilt aggregate form directly.
6006 Aggr_Init := Static_Initialization (Base_Init_Proc (Typ));
6008 if Present (Aggr_Init) then
6009 Set_Expression
6010 (N, New_Copy_Tree (Aggr_Init, New_Scope => Current_Scope));
6012 -- If type has discriminants, try to build an equivalent
6013 -- aggregate using discriminant values from the declaration.
6014 -- This is a useful optimization, in particular if restriction
6015 -- No_Elaboration_Code is active.
6017 elsif Build_Equivalent_Aggregate then
6018 null;
6020 -- Otherwise invoke the type init proc, generate:
6021 -- Type_Init_Proc (Obj);
6023 else
6024 Obj_Ref := New_Object_Reference;
6026 if Comes_From_Source (Def_Id) then
6027 Initialization_Warning (Obj_Ref);
6028 end if;
6030 Comp_Init := Build_Initialization_Call (Loc, Obj_Ref, Typ);
6031 end if;
6032 end if;
6034 -- Provide a default value if the object needs simple initialization
6035 -- and does not already have an initial value. A generated temporary
6036 -- does not require initialization because it will be assigned later.
6038 elsif Needs_Simple_Initialization
6039 (Typ, Initialize_Scalars
6040 and then No (Following_Address_Clause (N)))
6041 and then not Is_Internal (Def_Id)
6042 and then not Has_Init_Expression (N)
6043 then
6044 Set_No_Initialization (N, False);
6045 Set_Expression (N, Get_Simple_Init_Val (Typ, N, Esize (Def_Id)));
6046 Analyze_And_Resolve (Expression (N), Typ);
6047 end if;
6049 -- Initialize the object, generate:
6050 -- [Deep_]Initialize (Obj);
6052 if Needs_Finalization (Typ) and then not No_Initialization (N) then
6053 Obj_Init :=
6054 Make_Init_Call
6055 (Obj_Ref => New_Occurrence_Of (Def_Id, Loc),
6056 Typ => Typ);
6057 end if;
6059 -- Build a special finalization block when both the object and its
6060 -- controlled components are to be initialized. The block finalizes
6061 -- the components if the object initialization fails. Generate:
6063 -- begin
6064 -- <Obj_Init>
6066 -- exception
6067 -- when others =>
6068 -- <Fin_Call>
6069 -- raise;
6070 -- end;
6072 if Has_Controlled_Component (Typ)
6073 and then Present (Comp_Init)
6074 and then Present (Obj_Init)
6075 and then Exceptions_OK
6076 then
6077 Init_Stmts := Comp_Init;
6079 Fin_Call :=
6080 Make_Final_Call
6081 (Obj_Ref => New_Object_Reference,
6082 Typ => Typ,
6083 Skip_Self => True);
6085 if Present (Fin_Call) then
6086 Fin_Block :=
6087 Make_Block_Statement (Loc,
6088 Declarations => No_List,
6090 Handled_Statement_Sequence =>
6091 Make_Handled_Sequence_Of_Statements (Loc,
6092 Statements => New_List (Obj_Init),
6094 Exception_Handlers => New_List (
6095 Make_Exception_Handler (Loc,
6096 Exception_Choices => New_List (
6097 Make_Others_Choice (Loc)),
6099 Statements => New_List (
6100 Fin_Call,
6101 Make_Raise_Statement (Loc))))));
6103 -- Signal the ABE mechanism that the block carries out
6104 -- initialization actions.
6106 Set_Is_Initialization_Block (Fin_Block);
6108 Append_To (Init_Stmts, Fin_Block);
6109 end if;
6111 -- Otherwise finalization is not required, the initialization calls
6112 -- are passed to the abort block building circuitry, generate:
6114 -- Type_Init_Proc (Obj);
6115 -- [Deep_]Initialize (Obj);
6117 else
6118 if Present (Comp_Init) then
6119 Init_Stmts := Comp_Init;
6120 end if;
6122 if Present (Obj_Init) then
6123 if No (Init_Stmts) then
6124 Init_Stmts := New_List;
6125 end if;
6127 Append_To (Init_Stmts, Obj_Init);
6128 end if;
6129 end if;
6131 -- Build an abort block to protect the initialization calls
6133 if Abort_Allowed
6134 and then Present (Comp_Init)
6135 and then Present (Obj_Init)
6136 then
6137 -- Generate:
6138 -- Abort_Defer;
6140 Prepend_To (Init_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
6142 -- When exceptions are propagated, abort deferral must take place
6143 -- in the presence of initialization or finalization exceptions.
6144 -- Generate:
6146 -- begin
6147 -- Abort_Defer;
6148 -- <Init_Stmts>
6149 -- at end
6150 -- Abort_Undefer_Direct;
6151 -- end;
6153 if Exceptions_OK then
6154 Init_Stmts := New_List (
6155 Build_Abort_Undefer_Block (Loc,
6156 Stmts => Init_Stmts,
6157 Context => N));
6159 -- Otherwise exceptions are not propagated. Generate:
6161 -- Abort_Defer;
6162 -- <Init_Stmts>
6163 -- Abort_Undefer;
6165 else
6166 Append_To (Init_Stmts,
6167 Build_Runtime_Call (Loc, RE_Abort_Undefer));
6168 end if;
6169 end if;
6171 -- Insert the whole initialization sequence into the tree. If the
6172 -- object has a delayed freeze, as will be the case when it has
6173 -- aspect specifications, the initialization sequence is part of
6174 -- the freeze actions.
6176 if Present (Init_Stmts) then
6177 if Has_Delayed_Freeze (Def_Id) then
6178 Append_Freeze_Actions (Def_Id, Init_Stmts);
6179 else
6180 Insert_Actions_After (After, Init_Stmts);
6181 end if;
6182 end if;
6183 end Default_Initialize_Object;
6185 -------------------------
6186 -- Rewrite_As_Renaming --
6187 -------------------------
6189 function Rewrite_As_Renaming return Boolean is
6190 begin
6191 -- If the object declaration appears in the form
6193 -- Obj : Ctrl_Typ := Func (...);
6195 -- where Ctrl_Typ is controlled but not immutably limited type, then
6196 -- the expansion of the function call should use a dereference of the
6197 -- result to reference the value on the secondary stack.
6199 -- Obj : Ctrl_Typ renames Func (...).all;
6201 -- As a result, the call avoids an extra copy. This an optimization,
6202 -- but it is required for passing ACATS tests in some cases where it
6203 -- would otherwise make two copies. The RM allows removing redunant
6204 -- Adjust/Finalize calls, but does not allow insertion of extra ones.
6206 -- This part is disabled for now, because it breaks GPS builds
6208 return (False -- ???
6209 and then Nkind (Expr_Q) = N_Explicit_Dereference
6210 and then not Comes_From_Source (Expr_Q)
6211 and then Nkind (Original_Node (Expr_Q)) = N_Function_Call
6212 and then Nkind (Object_Definition (N)) in N_Has_Entity
6213 and then (Needs_Finalization (Entity (Object_Definition (N)))))
6215 -- If the initializing expression is for a variable with attribute
6216 -- OK_To_Rename set, then transform:
6218 -- Obj : Typ := Expr;
6220 -- into
6222 -- Obj : Typ renames Expr;
6224 -- provided that Obj is not aliased. The aliased case has to be
6225 -- excluded in general because Expr will not be aliased in
6226 -- general.
6228 or else
6229 (not Aliased_Present (N)
6230 and then Is_Entity_Name (Expr_Q)
6231 and then Ekind (Entity (Expr_Q)) = E_Variable
6232 and then OK_To_Rename (Entity (Expr_Q))
6233 and then Is_Entity_Name (Obj_Def));
6234 end Rewrite_As_Renaming;
6236 -- Local variables
6238 Next_N : constant Node_Id := Next (N);
6240 Adj_Call : Node_Id;
6241 Id_Ref : Node_Id;
6242 Tag_Assign : Node_Id;
6244 Init_After : Node_Id := N;
6245 -- Node after which the initialization actions are to be inserted. This
6246 -- is normally N, except for the case of a shared passive variable, in
6247 -- which case the init proc call must be inserted only after the bodies
6248 -- of the shared variable procedures have been seen.
6250 -- Start of processing for Expand_N_Object_Declaration
6252 begin
6253 -- Don't do anything for deferred constants. All proper actions will be
6254 -- expanded during the full declaration.
6256 if No (Expr) and Constant_Present (N) then
6257 return;
6258 end if;
6260 -- The type of the object cannot be abstract. This is diagnosed at the
6261 -- point the object is frozen, which happens after the declaration is
6262 -- fully expanded, so simply return now.
6264 if Is_Abstract_Type (Typ) then
6265 return;
6266 end if;
6268 -- No action needed for the internal imported dummy object added by
6269 -- Make_DT to compute the offset of the components that reference
6270 -- secondary dispatch tables; required to avoid never-ending loop
6271 -- processing this internal object declaration.
6273 if Tagged_Type_Expansion
6274 and then Is_Internal (Def_Id)
6275 and then Is_Imported (Def_Id)
6276 and then Related_Type (Def_Id) = Implementation_Base_Type (Typ)
6277 then
6278 return;
6279 end if;
6281 -- First we do special processing for objects of a tagged type where
6282 -- this is the point at which the type is frozen. The creation of the
6283 -- dispatch table and the initialization procedure have to be deferred
6284 -- to this point, since we reference previously declared primitive
6285 -- subprograms.
6287 -- Force construction of dispatch tables of library level tagged types
6289 if Tagged_Type_Expansion
6290 and then Building_Static_Dispatch_Tables
6291 and then Is_Library_Level_Entity (Def_Id)
6292 and then Is_Library_Level_Tagged_Type (Base_Typ)
6293 and then Ekind_In (Base_Typ, E_Record_Type,
6294 E_Protected_Type,
6295 E_Task_Type)
6296 and then not Has_Dispatch_Table (Base_Typ)
6297 then
6298 declare
6299 New_Nodes : List_Id := No_List;
6301 begin
6302 if Is_Concurrent_Type (Base_Typ) then
6303 New_Nodes := Make_DT (Corresponding_Record_Type (Base_Typ), N);
6304 else
6305 New_Nodes := Make_DT (Base_Typ, N);
6306 end if;
6308 if not Is_Empty_List (New_Nodes) then
6309 Insert_List_Before (N, New_Nodes);
6310 end if;
6311 end;
6312 end if;
6314 -- Make shared memory routines for shared passive variable
6316 if Is_Shared_Passive (Def_Id) then
6317 Init_After := Make_Shared_Var_Procs (N);
6318 end if;
6320 -- If tasks being declared, make sure we have an activation chain
6321 -- defined for the tasks (has no effect if we already have one), and
6322 -- also that a Master variable is established and that the appropriate
6323 -- enclosing construct is established as a task master.
6325 if Has_Task (Typ) then
6326 Build_Activation_Chain_Entity (N);
6327 Build_Master_Entity (Def_Id);
6328 end if;
6330 Check_Large_Modular_Array;
6332 -- If No_Implicit_Heap_Allocations or No_Implicit_Task_Allocations
6333 -- restrictions are active then default-sized secondary stacks are
6334 -- generated by the binder and allocated by SS_Init. To provide the
6335 -- binder the number of stacks to generate, the number of default-sized
6336 -- stacks required for task objects contained within the object
6337 -- declaration N is calculated here as it is at this point where
6338 -- unconstrained types become constrained. The result is stored in the
6339 -- enclosing unit's Unit_Record.
6341 -- Note if N is an array object declaration that has an initialization
6342 -- expression, a second object declaration for the initialization
6343 -- expression is created by the compiler. To prevent double counting
6344 -- of the stacks in this scenario, the stacks of the first array are
6345 -- not counted.
6347 if Has_Task (Typ)
6348 and then not Restriction_Active (No_Secondary_Stack)
6349 and then (Restriction_Active (No_Implicit_Heap_Allocations)
6350 or else Restriction_Active (No_Implicit_Task_Allocations))
6351 and then not (Ekind_In (Ekind (Typ), E_Array_Type, E_Array_Subtype)
6352 and then (Has_Init_Expression (N)))
6353 then
6354 declare
6355 PS_Count, SS_Count : Int := 0;
6356 begin
6357 Count_Default_Sized_Task_Stacks (Typ, PS_Count, SS_Count);
6358 Increment_Primary_Stack_Count (PS_Count);
6359 Increment_Sec_Stack_Count (SS_Count);
6360 end;
6361 end if;
6363 -- Default initialization required, and no expression present
6365 if No (Expr) then
6367 -- If we have a type with a variant part, the initialization proc
6368 -- will contain implicit tests of the discriminant values, which
6369 -- counts as a violation of the restriction No_Implicit_Conditionals.
6371 if Has_Variant_Part (Typ) then
6372 declare
6373 Msg : Boolean;
6375 begin
6376 Check_Restriction (Msg, No_Implicit_Conditionals, Obj_Def);
6378 if Msg then
6379 Error_Msg_N
6380 ("\initialization of variant record tests discriminants",
6381 Obj_Def);
6382 return;
6383 end if;
6384 end;
6385 end if;
6387 -- For the default initialization case, if we have a private type
6388 -- with invariants, and invariant checks are enabled, then insert an
6389 -- invariant check after the object declaration. Note that it is OK
6390 -- to clobber the object with an invalid value since if the exception
6391 -- is raised, then the object will go out of scope. In the case where
6392 -- an array object is initialized with an aggregate, the expression
6393 -- is removed. Check flag Has_Init_Expression to avoid generating a
6394 -- junk invariant check and flag No_Initialization to avoid checking
6395 -- an uninitialized object such as a compiler temporary used for an
6396 -- aggregate.
6398 if Has_Invariants (Base_Typ)
6399 and then Present (Invariant_Procedure (Base_Typ))
6400 and then not Has_Init_Expression (N)
6401 and then not No_Initialization (N)
6402 then
6403 -- If entity has an address clause or aspect, make invariant
6404 -- call into a freeze action for the explicit freeze node for
6405 -- object. Otherwise insert invariant check after declaration.
6407 if Present (Following_Address_Clause (N))
6408 or else Has_Aspect (Def_Id, Aspect_Address)
6409 then
6410 Ensure_Freeze_Node (Def_Id);
6411 Set_Has_Delayed_Freeze (Def_Id);
6412 Set_Is_Frozen (Def_Id, False);
6414 if not Partial_View_Has_Unknown_Discr (Typ) then
6415 Append_Freeze_Action (Def_Id,
6416 Make_Invariant_Call (New_Occurrence_Of (Def_Id, Loc)));
6417 end if;
6419 elsif not Partial_View_Has_Unknown_Discr (Typ) then
6420 Insert_After (N,
6421 Make_Invariant_Call (New_Occurrence_Of (Def_Id, Loc)));
6422 end if;
6423 end if;
6425 Default_Initialize_Object (Init_After);
6427 -- Generate attribute for Persistent_BSS if needed
6429 if Persistent_BSS_Mode
6430 and then Comes_From_Source (N)
6431 and then Is_Potentially_Persistent_Type (Typ)
6432 and then not Has_Init_Expression (N)
6433 and then Is_Library_Level_Entity (Def_Id)
6434 then
6435 declare
6436 Prag : Node_Id;
6437 begin
6438 Prag :=
6439 Make_Linker_Section_Pragma
6440 (Def_Id, Sloc (N), ".persistent.bss");
6441 Insert_After (N, Prag);
6442 Analyze (Prag);
6443 end;
6444 end if;
6446 -- If access type, then we know it is null if not initialized
6448 if Is_Access_Type (Typ) then
6449 Set_Is_Known_Null (Def_Id);
6450 end if;
6452 -- Explicit initialization present
6454 else
6455 -- Obtain actual expression from qualified expression
6457 if Nkind (Expr) = N_Qualified_Expression then
6458 Expr_Q := Expression (Expr);
6459 else
6460 Expr_Q := Expr;
6461 end if;
6463 -- When we have the appropriate type of aggregate in the expression
6464 -- (it has been determined during analysis of the aggregate by
6465 -- setting the delay flag), let's perform in place assignment and
6466 -- thus avoid creating a temporary.
6468 if Is_Delayed_Aggregate (Expr_Q) then
6469 Convert_Aggr_In_Object_Decl (N);
6471 -- Ada 2005 (AI-318-02): If the initialization expression is a call
6472 -- to a build-in-place function, then access to the declared object
6473 -- must be passed to the function. Currently we limit such functions
6474 -- to those with constrained limited result subtypes, but eventually
6475 -- plan to expand the allowed forms of functions that are treated as
6476 -- build-in-place.
6478 elsif Is_Build_In_Place_Function_Call (Expr_Q) then
6479 Make_Build_In_Place_Call_In_Object_Declaration (N, Expr_Q);
6481 -- The previous call expands the expression initializing the
6482 -- built-in-place object into further code that will be analyzed
6483 -- later. No further expansion needed here.
6485 return;
6487 -- This is the same as the previous 'elsif', except that the call has
6488 -- been transformed by other expansion activities into something like
6489 -- F(...)'Reference.
6491 elsif Nkind (Expr_Q) = N_Reference
6492 and then Is_Build_In_Place_Function_Call (Prefix (Expr_Q))
6493 and then not Is_Expanded_Build_In_Place_Call
6494 (Unqual_Conv (Prefix (Expr_Q)))
6495 then
6496 Make_Build_In_Place_Call_In_Anonymous_Context (Prefix (Expr_Q));
6498 -- The previous call expands the expression initializing the
6499 -- built-in-place object into further code that will be analyzed
6500 -- later. No further expansion needed here.
6502 return;
6504 -- Ada 2005 (AI-318-02): Specialization of the previous case for
6505 -- expressions containing a build-in-place function call whose
6506 -- returned object covers interface types, and Expr_Q has calls to
6507 -- Ada.Tags.Displace to displace the pointer to the returned build-
6508 -- in-place object to reference the secondary dispatch table of a
6509 -- covered interface type.
6511 elsif Present (Unqual_BIP_Iface_Function_Call (Expr_Q)) then
6512 Make_Build_In_Place_Iface_Call_In_Object_Declaration (N, Expr_Q);
6514 -- The previous call expands the expression initializing the
6515 -- built-in-place object into further code that will be analyzed
6516 -- later. No further expansion needed here.
6518 return;
6520 -- Ada 2005 (AI-251): Rewrite the expression that initializes a
6521 -- class-wide interface object to ensure that we copy the full
6522 -- object, unless we are targetting a VM where interfaces are handled
6523 -- by VM itself. Note that if the root type of Typ is an ancestor of
6524 -- Expr's type, both types share the same dispatch table and there is
6525 -- no need to displace the pointer.
6527 elsif Is_Interface (Typ)
6529 -- Avoid never-ending recursion because if Equivalent_Type is set
6530 -- then we've done it already and must not do it again.
6532 and then not
6533 (Nkind (Obj_Def) = N_Identifier
6534 and then Present (Equivalent_Type (Entity (Obj_Def))))
6535 then
6536 pragma Assert (Is_Class_Wide_Type (Typ));
6538 -- If the object is a return object of an inherently limited type,
6539 -- which implies build-in-place treatment, bypass the special
6540 -- treatment of class-wide interface initialization below. In this
6541 -- case, the expansion of the return statement will take care of
6542 -- creating the object (via allocator) and initializing it.
6544 if Is_Return_Object (Def_Id) and then Is_Limited_View (Typ) then
6545 null;
6547 elsif Tagged_Type_Expansion then
6548 declare
6549 Iface : constant Entity_Id := Root_Type (Typ);
6550 Expr_N : Node_Id := Expr;
6551 Expr_Typ : Entity_Id;
6552 New_Expr : Node_Id;
6553 Obj_Id : Entity_Id;
6554 Tag_Comp : Node_Id;
6556 begin
6557 -- If the original node of the expression was a conversion
6558 -- to this specific class-wide interface type then restore
6559 -- the original node because we must copy the object before
6560 -- displacing the pointer to reference the secondary tag
6561 -- component. This code must be kept synchronized with the
6562 -- expansion done by routine Expand_Interface_Conversion
6564 if not Comes_From_Source (Expr_N)
6565 and then Nkind (Expr_N) = N_Explicit_Dereference
6566 and then Nkind (Original_Node (Expr_N)) = N_Type_Conversion
6567 and then Etype (Original_Node (Expr_N)) = Typ
6568 then
6569 Rewrite (Expr_N, Original_Node (Expression (N)));
6570 end if;
6572 -- Avoid expansion of redundant interface conversion
6574 if Is_Interface (Etype (Expr_N))
6575 and then Nkind (Expr_N) = N_Type_Conversion
6576 and then Etype (Expr_N) = Typ
6577 then
6578 Expr_N := Expression (Expr_N);
6579 Set_Expression (N, Expr_N);
6580 end if;
6582 Obj_Id := Make_Temporary (Loc, 'D', Expr_N);
6583 Expr_Typ := Base_Type (Etype (Expr_N));
6585 if Is_Class_Wide_Type (Expr_Typ) then
6586 Expr_Typ := Root_Type (Expr_Typ);
6587 end if;
6589 -- Replace
6590 -- CW : I'Class := Obj;
6591 -- by
6592 -- Tmp : T := Obj;
6593 -- type Ityp is not null access I'Class;
6594 -- CW : I'Class renames Ityp (Tmp.I_Tag'Address).all;
6596 if Comes_From_Source (Expr_N)
6597 and then Nkind (Expr_N) = N_Identifier
6598 and then not Is_Interface (Expr_Typ)
6599 and then Interface_Present_In_Ancestor (Expr_Typ, Typ)
6600 and then (Expr_Typ = Etype (Expr_Typ)
6601 or else not
6602 Is_Variable_Size_Record (Etype (Expr_Typ)))
6603 then
6604 -- Copy the object
6606 Insert_Action (N,
6607 Make_Object_Declaration (Loc,
6608 Defining_Identifier => Obj_Id,
6609 Object_Definition =>
6610 New_Occurrence_Of (Expr_Typ, Loc),
6611 Expression => Relocate_Node (Expr_N)));
6613 -- Statically reference the tag associated with the
6614 -- interface
6616 Tag_Comp :=
6617 Make_Selected_Component (Loc,
6618 Prefix => New_Occurrence_Of (Obj_Id, Loc),
6619 Selector_Name =>
6620 New_Occurrence_Of
6621 (Find_Interface_Tag (Expr_Typ, Iface), Loc));
6623 -- Replace
6624 -- IW : I'Class := Obj;
6625 -- by
6626 -- type Equiv_Record is record ... end record;
6627 -- implicit subtype CW is <Class_Wide_Subtype>;
6628 -- Tmp : CW := CW!(Obj);
6629 -- type Ityp is not null access I'Class;
6630 -- IW : I'Class renames
6631 -- Ityp!(Displace (Temp'Address, I'Tag)).all;
6633 else
6634 -- Generate the equivalent record type and update the
6635 -- subtype indication to reference it.
6637 Expand_Subtype_From_Expr
6638 (N => N,
6639 Unc_Type => Typ,
6640 Subtype_Indic => Obj_Def,
6641 Exp => Expr_N);
6643 if not Is_Interface (Etype (Expr_N)) then
6644 New_Expr := Relocate_Node (Expr_N);
6646 -- For interface types we use 'Address which displaces
6647 -- the pointer to the base of the object (if required)
6649 else
6650 New_Expr :=
6651 Unchecked_Convert_To (Etype (Obj_Def),
6652 Make_Explicit_Dereference (Loc,
6653 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6654 Make_Attribute_Reference (Loc,
6655 Prefix => Relocate_Node (Expr_N),
6656 Attribute_Name => Name_Address))));
6657 end if;
6659 -- Copy the object
6661 if not Is_Limited_Record (Expr_Typ) then
6662 Insert_Action (N,
6663 Make_Object_Declaration (Loc,
6664 Defining_Identifier => Obj_Id,
6665 Object_Definition =>
6666 New_Occurrence_Of (Etype (Obj_Def), Loc),
6667 Expression => New_Expr));
6669 -- Rename limited type object since they cannot be copied
6670 -- This case occurs when the initialization expression
6671 -- has been previously expanded into a temporary object.
6673 else pragma Assert (not Comes_From_Source (Expr_Q));
6674 Insert_Action (N,
6675 Make_Object_Renaming_Declaration (Loc,
6676 Defining_Identifier => Obj_Id,
6677 Subtype_Mark =>
6678 New_Occurrence_Of (Etype (Obj_Def), Loc),
6679 Name =>
6680 Unchecked_Convert_To
6681 (Etype (Obj_Def), New_Expr)));
6682 end if;
6684 -- Dynamically reference the tag associated with the
6685 -- interface.
6687 Tag_Comp :=
6688 Make_Function_Call (Loc,
6689 Name => New_Occurrence_Of (RTE (RE_Displace), Loc),
6690 Parameter_Associations => New_List (
6691 Make_Attribute_Reference (Loc,
6692 Prefix => New_Occurrence_Of (Obj_Id, Loc),
6693 Attribute_Name => Name_Address),
6694 New_Occurrence_Of
6695 (Node (First_Elmt (Access_Disp_Table (Iface))),
6696 Loc)));
6697 end if;
6699 Rewrite (N,
6700 Make_Object_Renaming_Declaration (Loc,
6701 Defining_Identifier => Make_Temporary (Loc, 'D'),
6702 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
6703 Name =>
6704 Convert_Tag_To_Interface (Typ, Tag_Comp)));
6706 -- If the original entity comes from source, then mark the
6707 -- new entity as needing debug information, even though it's
6708 -- defined by a generated renaming that does not come from
6709 -- source, so that Materialize_Entity will be set on the
6710 -- entity when Debug_Renaming_Declaration is called during
6711 -- analysis.
6713 if Comes_From_Source (Def_Id) then
6714 Set_Debug_Info_Needed (Defining_Identifier (N));
6715 end if;
6717 Analyze (N, Suppress => All_Checks);
6719 -- Replace internal identifier of rewritten node by the
6720 -- identifier found in the sources. We also have to exchange
6721 -- entities containing their defining identifiers to ensure
6722 -- the correct replacement of the object declaration by this
6723 -- object renaming declaration because these identifiers
6724 -- were previously added by Enter_Name to the current scope.
6725 -- We must preserve the homonym chain of the source entity
6726 -- as well. We must also preserve the kind of the entity,
6727 -- which may be a constant. Preserve entity chain because
6728 -- itypes may have been generated already, and the full
6729 -- chain must be preserved for final freezing. Finally,
6730 -- preserve Comes_From_Source setting, so that debugging
6731 -- and cross-referencing information is properly kept, and
6732 -- preserve source location, to prevent spurious errors when
6733 -- entities are declared (they must have their own Sloc).
6735 declare
6736 New_Id : constant Entity_Id := Defining_Identifier (N);
6737 Next_Temp : constant Entity_Id := Next_Entity (New_Id);
6738 Save_CFS : constant Boolean :=
6739 Comes_From_Source (Def_Id);
6740 Save_SP : constant Node_Id := SPARK_Pragma (Def_Id);
6741 Save_SPI : constant Boolean :=
6742 SPARK_Pragma_Inherited (Def_Id);
6744 begin
6745 Set_Next_Entity (New_Id, Next_Entity (Def_Id));
6746 Set_Next_Entity (Def_Id, Next_Temp);
6748 Set_Chars (Defining_Identifier (N), Chars (Def_Id));
6749 Set_Homonym (Defining_Identifier (N), Homonym (Def_Id));
6750 Set_Ekind (Defining_Identifier (N), Ekind (Def_Id));
6751 Set_Sloc (Defining_Identifier (N), Sloc (Def_Id));
6753 Set_Comes_From_Source (Def_Id, False);
6755 -- ??? This is extremely dangerous!!! Exchanging entities
6756 -- is very low level, and as a result it resets flags and
6757 -- fields which belong to the original Def_Id. Several of
6758 -- these attributes are saved and restored, but there may
6759 -- be many more that need to be preserverd.
6761 Exchange_Entities (Defining_Identifier (N), Def_Id);
6763 -- Restore clobbered attributes
6765 Set_Comes_From_Source (Def_Id, Save_CFS);
6766 Set_SPARK_Pragma (Def_Id, Save_SP);
6767 Set_SPARK_Pragma_Inherited (Def_Id, Save_SPI);
6768 end;
6769 end;
6770 end if;
6772 return;
6774 -- Common case of explicit object initialization
6776 else
6777 -- In most cases, we must check that the initial value meets any
6778 -- constraint imposed by the declared type. However, there is one
6779 -- very important exception to this rule. If the entity has an
6780 -- unconstrained nominal subtype, then it acquired its constraints
6781 -- from the expression in the first place, and not only does this
6782 -- mean that the constraint check is not needed, but an attempt to
6783 -- perform the constraint check can cause order of elaboration
6784 -- problems.
6786 if not Is_Constr_Subt_For_U_Nominal (Typ) then
6788 -- If this is an allocator for an aggregate that has been
6789 -- allocated in place, delay checks until assignments are
6790 -- made, because the discriminants are not initialized.
6792 if Nkind (Expr) = N_Allocator
6793 and then No_Initialization (Expr)
6794 then
6795 null;
6797 -- Otherwise apply a constraint check now if no prev error
6799 elsif Nkind (Expr) /= N_Error then
6800 Apply_Constraint_Check (Expr, Typ);
6802 -- Deal with possible range check
6804 if Do_Range_Check (Expr) then
6806 -- If assignment checks are suppressed, turn off flag
6808 if Suppress_Assignment_Checks (N) then
6809 Set_Do_Range_Check (Expr, False);
6811 -- Otherwise generate the range check
6813 else
6814 Generate_Range_Check
6815 (Expr, Typ, CE_Range_Check_Failed);
6816 end if;
6817 end if;
6818 end if;
6819 end if;
6821 -- If the type is controlled and not inherently limited, then
6822 -- the target is adjusted after the copy and attached to the
6823 -- finalization list. However, no adjustment is done in the case
6824 -- where the object was initialized by a call to a function whose
6825 -- result is built in place, since no copy occurred. Similarly, no
6826 -- adjustment is required if we are going to rewrite the object
6827 -- declaration into a renaming declaration.
6829 if Needs_Finalization (Typ)
6830 and then not Is_Limited_View (Typ)
6831 and then not Rewrite_As_Renaming
6832 then
6833 Adj_Call :=
6834 Make_Adjust_Call (
6835 Obj_Ref => New_Occurrence_Of (Def_Id, Loc),
6836 Typ => Base_Typ);
6838 -- Guard against a missing [Deep_]Adjust when the base type
6839 -- was not properly frozen.
6841 if Present (Adj_Call) then
6842 Insert_Action_After (Init_After, Adj_Call);
6843 end if;
6844 end if;
6846 -- For tagged types, when an init value is given, the tag has to
6847 -- be re-initialized separately in order to avoid the propagation
6848 -- of a wrong tag coming from a view conversion unless the type
6849 -- is class wide (in this case the tag comes from the init value).
6850 -- Suppress the tag assignment when not Tagged_Type_Expansion
6851 -- because tags are represented implicitly in objects. Ditto for
6852 -- types that are CPP_CLASS, and for initializations that are
6853 -- aggregates, because they have to have the right tag.
6855 -- The re-assignment of the tag has to be done even if the object
6856 -- is a constant. The assignment must be analyzed after the
6857 -- declaration. If an address clause follows, this is handled as
6858 -- part of the freeze actions for the object, otherwise insert
6859 -- tag assignment here.
6861 Tag_Assign := Make_Tag_Assignment (N);
6863 if Present (Tag_Assign) then
6864 if Present (Following_Address_Clause (N)) then
6865 Ensure_Freeze_Node (Def_Id);
6867 else
6868 Insert_Action_After (Init_After, Tag_Assign);
6869 end if;
6871 -- Handle C++ constructor calls. Note that we do not check that
6872 -- Typ is a tagged type since the equivalent Ada type of a C++
6873 -- class that has no virtual methods is an untagged limited
6874 -- record type.
6876 elsif Is_CPP_Constructor_Call (Expr) then
6878 -- The call to the initialization procedure does NOT freeze the
6879 -- object being initialized.
6881 Id_Ref := New_Occurrence_Of (Def_Id, Loc);
6882 Set_Must_Not_Freeze (Id_Ref);
6883 Set_Assignment_OK (Id_Ref);
6885 Insert_Actions_After (Init_After,
6886 Build_Initialization_Call (Loc, Id_Ref, Typ,
6887 Constructor_Ref => Expr));
6889 -- We remove here the original call to the constructor
6890 -- to avoid its management in the backend
6892 Set_Expression (N, Empty);
6893 return;
6895 -- Handle initialization of limited tagged types
6897 elsif Is_Tagged_Type (Typ)
6898 and then Is_Class_Wide_Type (Typ)
6899 and then Is_Limited_Record (Typ)
6900 and then not Is_Limited_Interface (Typ)
6901 then
6902 -- Given that the type is limited we cannot perform a copy. If
6903 -- Expr_Q is the reference to a variable we mark the variable
6904 -- as OK_To_Rename to expand this declaration into a renaming
6905 -- declaration (see bellow).
6907 if Is_Entity_Name (Expr_Q) then
6908 Set_OK_To_Rename (Entity (Expr_Q));
6910 -- If we cannot convert the expression into a renaming we must
6911 -- consider it an internal error because the backend does not
6912 -- have support to handle it.
6914 else
6915 pragma Assert (False);
6916 raise Program_Error;
6917 end if;
6919 -- For discrete types, set the Is_Known_Valid flag if the
6920 -- initializing value is known to be valid. Only do this for
6921 -- source assignments, since otherwise we can end up turning
6922 -- on the known valid flag prematurely from inserted code.
6924 elsif Comes_From_Source (N)
6925 and then Is_Discrete_Type (Typ)
6926 and then Expr_Known_Valid (Expr)
6927 then
6928 Set_Is_Known_Valid (Def_Id);
6930 elsif Is_Access_Type (Typ) then
6932 -- For access types set the Is_Known_Non_Null flag if the
6933 -- initializing value is known to be non-null. We can also set
6934 -- Can_Never_Be_Null if this is a constant.
6936 if Known_Non_Null (Expr) then
6937 Set_Is_Known_Non_Null (Def_Id, True);
6939 if Constant_Present (N) then
6940 Set_Can_Never_Be_Null (Def_Id);
6941 end if;
6942 end if;
6943 end if;
6945 -- If validity checking on copies, validate initial expression.
6946 -- But skip this if declaration is for a generic type, since it
6947 -- makes no sense to validate generic types. Not clear if this
6948 -- can happen for legal programs, but it definitely can arise
6949 -- from previous instantiation errors.
6951 if Validity_Checks_On
6952 and then Comes_From_Source (N)
6953 and then Validity_Check_Copies
6954 and then not Is_Generic_Type (Etype (Def_Id))
6955 then
6956 Ensure_Valid (Expr);
6957 Set_Is_Known_Valid (Def_Id);
6958 end if;
6959 end if;
6961 -- Cases where the back end cannot handle the initialization
6962 -- directly. In such cases, we expand an assignment that will
6963 -- be appropriately handled by Expand_N_Assignment_Statement.
6965 -- The exclusion of the unconstrained case is wrong, but for now it
6966 -- is too much trouble ???
6968 if (Is_Possibly_Unaligned_Slice (Expr)
6969 or else (Is_Possibly_Unaligned_Object (Expr)
6970 and then not Represented_As_Scalar (Etype (Expr))))
6971 and then not (Is_Array_Type (Etype (Expr))
6972 and then not Is_Constrained (Etype (Expr)))
6973 then
6974 declare
6975 Stat : constant Node_Id :=
6976 Make_Assignment_Statement (Loc,
6977 Name => New_Occurrence_Of (Def_Id, Loc),
6978 Expression => Relocate_Node (Expr));
6979 begin
6980 Set_Expression (N, Empty);
6981 Set_No_Initialization (N);
6982 Set_Assignment_OK (Name (Stat));
6983 Set_No_Ctrl_Actions (Stat);
6984 Insert_After_And_Analyze (Init_After, Stat);
6985 end;
6986 end if;
6987 end if;
6989 if Nkind (Obj_Def) = N_Access_Definition
6990 and then not Is_Local_Anonymous_Access (Etype (Def_Id))
6991 then
6992 -- An Ada 2012 stand-alone object of an anonymous access type
6994 declare
6995 Loc : constant Source_Ptr := Sloc (N);
6997 Level : constant Entity_Id :=
6998 Make_Defining_Identifier (Sloc (N),
6999 Chars =>
7000 New_External_Name (Chars (Def_Id), Suffix => "L"));
7002 Level_Expr : Node_Id;
7003 Level_Decl : Node_Id;
7005 begin
7006 Set_Ekind (Level, Ekind (Def_Id));
7007 Set_Etype (Level, Standard_Natural);
7008 Set_Scope (Level, Scope (Def_Id));
7010 if No (Expr) then
7012 -- Set accessibility level of null
7014 Level_Expr :=
7015 Make_Integer_Literal (Loc, Scope_Depth (Standard_Standard));
7017 else
7018 Level_Expr := Dynamic_Accessibility_Level (Expr);
7019 end if;
7021 Level_Decl :=
7022 Make_Object_Declaration (Loc,
7023 Defining_Identifier => Level,
7024 Object_Definition =>
7025 New_Occurrence_Of (Standard_Natural, Loc),
7026 Expression => Level_Expr,
7027 Constant_Present => Constant_Present (N),
7028 Has_Init_Expression => True);
7030 Insert_Action_After (Init_After, Level_Decl);
7032 Set_Extra_Accessibility (Def_Id, Level);
7033 end;
7034 end if;
7036 -- If the object is default initialized and its type is subject to
7037 -- pragma Default_Initial_Condition, add a runtime check to verify
7038 -- the assumption of the pragma (SPARK RM 7.3.3). Generate:
7040 -- <Base_Typ>DIC (<Base_Typ> (Def_Id));
7042 -- Note that the check is generated for source objects only
7044 if Comes_From_Source (Def_Id)
7045 and then Has_DIC (Typ)
7046 and then Present (DIC_Procedure (Typ))
7047 and then not Has_Init_Expression (N)
7048 then
7049 declare
7050 DIC_Call : constant Node_Id := Build_DIC_Call (Loc, Def_Id, Typ);
7052 begin
7053 if Present (Next_N) then
7054 Insert_Before_And_Analyze (Next_N, DIC_Call);
7056 -- The object declaration is the last node in a declarative or a
7057 -- statement list.
7059 else
7060 Append_To (List_Containing (N), DIC_Call);
7061 Analyze (DIC_Call);
7062 end if;
7063 end;
7064 end if;
7066 -- Final transformation - turn the object declaration into a renaming
7067 -- if appropriate. If this is the completion of a deferred constant
7068 -- declaration, then this transformation generates what would be
7069 -- illegal code if written by hand, but that's OK.
7071 if Present (Expr) then
7072 if Rewrite_As_Renaming then
7073 Rewrite (N,
7074 Make_Object_Renaming_Declaration (Loc,
7075 Defining_Identifier => Defining_Identifier (N),
7076 Subtype_Mark => Obj_Def,
7077 Name => Expr_Q));
7079 -- We do not analyze this renaming declaration, because all its
7080 -- components have already been analyzed, and if we were to go
7081 -- ahead and analyze it, we would in effect be trying to generate
7082 -- another declaration of X, which won't do.
7084 Set_Renamed_Object (Defining_Identifier (N), Expr_Q);
7085 Set_Analyzed (N);
7087 -- We do need to deal with debug issues for this renaming
7089 -- First, if entity comes from source, then mark it as needing
7090 -- debug information, even though it is defined by a generated
7091 -- renaming that does not come from source.
7093 if Comes_From_Source (Defining_Identifier (N)) then
7094 Set_Debug_Info_Needed (Defining_Identifier (N));
7095 end if;
7097 -- Now call the routine to generate debug info for the renaming
7099 declare
7100 Decl : constant Node_Id := Debug_Renaming_Declaration (N);
7101 begin
7102 if Present (Decl) then
7103 Insert_Action (N, Decl);
7104 end if;
7105 end;
7106 end if;
7107 end if;
7109 -- Exception on library entity not available
7111 exception
7112 when RE_Not_Available =>
7113 return;
7114 end Expand_N_Object_Declaration;
7116 ---------------------------------
7117 -- Expand_N_Subtype_Indication --
7118 ---------------------------------
7120 -- Add a check on the range of the subtype. The static case is partially
7121 -- duplicated by Process_Range_Expr_In_Decl in Sem_Ch3, but we still need
7122 -- to check here for the static case in order to avoid generating
7123 -- extraneous expanded code. Also deal with validity checking.
7125 procedure Expand_N_Subtype_Indication (N : Node_Id) is
7126 Ran : constant Node_Id := Range_Expression (Constraint (N));
7127 Typ : constant Entity_Id := Entity (Subtype_Mark (N));
7129 begin
7130 if Nkind (Constraint (N)) = N_Range_Constraint then
7131 Validity_Check_Range (Range_Expression (Constraint (N)));
7132 end if;
7134 if Nkind_In (Parent (N), N_Constrained_Array_Definition, N_Slice) then
7135 Apply_Range_Check (Ran, Typ);
7136 end if;
7137 end Expand_N_Subtype_Indication;
7139 ---------------------------
7140 -- Expand_N_Variant_Part --
7141 ---------------------------
7143 -- Note: this procedure no longer has any effect. It used to be that we
7144 -- would replace the choices in the last variant by a when others, and
7145 -- also expanded static predicates in variant choices here, but both of
7146 -- those activities were being done too early, since we can't check the
7147 -- choices until the statically predicated subtypes are frozen, which can
7148 -- happen as late as the free point of the record, and we can't change the
7149 -- last choice to an others before checking the choices, which is now done
7150 -- at the freeze point of the record.
7152 procedure Expand_N_Variant_Part (N : Node_Id) is
7153 begin
7154 null;
7155 end Expand_N_Variant_Part;
7157 ---------------------------------
7158 -- Expand_Previous_Access_Type --
7159 ---------------------------------
7161 procedure Expand_Previous_Access_Type (Def_Id : Entity_Id) is
7162 Ptr_Typ : Entity_Id;
7164 begin
7165 -- Find all access types in the current scope whose designated type is
7166 -- Def_Id and build master renamings for them.
7168 Ptr_Typ := First_Entity (Current_Scope);
7169 while Present (Ptr_Typ) loop
7170 if Is_Access_Type (Ptr_Typ)
7171 and then Designated_Type (Ptr_Typ) = Def_Id
7172 and then No (Master_Id (Ptr_Typ))
7173 then
7174 -- Ensure that the designated type has a master
7176 Build_Master_Entity (Def_Id);
7178 -- Private and incomplete types complicate the insertion of master
7179 -- renamings because the access type may precede the full view of
7180 -- the designated type. For this reason, the master renamings are
7181 -- inserted relative to the designated type.
7183 Build_Master_Renaming (Ptr_Typ, Ins_Nod => Parent (Def_Id));
7184 end if;
7186 Next_Entity (Ptr_Typ);
7187 end loop;
7188 end Expand_Previous_Access_Type;
7190 -----------------------------
7191 -- Expand_Record_Extension --
7192 -----------------------------
7194 -- Add a field _parent at the beginning of the record extension. This is
7195 -- used to implement inheritance. Here are some examples of expansion:
7197 -- 1. no discriminants
7198 -- type T2 is new T1 with null record;
7199 -- gives
7200 -- type T2 is new T1 with record
7201 -- _Parent : T1;
7202 -- end record;
7204 -- 2. renamed discriminants
7205 -- type T2 (B, C : Int) is new T1 (A => B) with record
7206 -- _Parent : T1 (A => B);
7207 -- D : Int;
7208 -- end;
7210 -- 3. inherited discriminants
7211 -- type T2 is new T1 with record -- discriminant A inherited
7212 -- _Parent : T1 (A);
7213 -- D : Int;
7214 -- end;
7216 procedure Expand_Record_Extension (T : Entity_Id; Def : Node_Id) is
7217 Indic : constant Node_Id := Subtype_Indication (Def);
7218 Loc : constant Source_Ptr := Sloc (Def);
7219 Rec_Ext_Part : Node_Id := Record_Extension_Part (Def);
7220 Par_Subtype : Entity_Id;
7221 Comp_List : Node_Id;
7222 Comp_Decl : Node_Id;
7223 Parent_N : Node_Id;
7224 D : Entity_Id;
7225 List_Constr : constant List_Id := New_List;
7227 begin
7228 -- Expand_Record_Extension is called directly from the semantics, so
7229 -- we must check to see whether expansion is active before proceeding,
7230 -- because this affects the visibility of selected components in bodies
7231 -- of instances.
7233 if not Expander_Active then
7234 return;
7235 end if;
7237 -- This may be a derivation of an untagged private type whose full
7238 -- view is tagged, in which case the Derived_Type_Definition has no
7239 -- extension part. Build an empty one now.
7241 if No (Rec_Ext_Part) then
7242 Rec_Ext_Part :=
7243 Make_Record_Definition (Loc,
7244 End_Label => Empty,
7245 Component_List => Empty,
7246 Null_Present => True);
7248 Set_Record_Extension_Part (Def, Rec_Ext_Part);
7249 Mark_Rewrite_Insertion (Rec_Ext_Part);
7250 end if;
7252 Comp_List := Component_List (Rec_Ext_Part);
7254 Parent_N := Make_Defining_Identifier (Loc, Name_uParent);
7256 -- If the derived type inherits its discriminants the type of the
7257 -- _parent field must be constrained by the inherited discriminants
7259 if Has_Discriminants (T)
7260 and then Nkind (Indic) /= N_Subtype_Indication
7261 and then not Is_Constrained (Entity (Indic))
7262 then
7263 D := First_Discriminant (T);
7264 while Present (D) loop
7265 Append_To (List_Constr, New_Occurrence_Of (D, Loc));
7266 Next_Discriminant (D);
7267 end loop;
7269 Par_Subtype :=
7270 Process_Subtype (
7271 Make_Subtype_Indication (Loc,
7272 Subtype_Mark => New_Occurrence_Of (Entity (Indic), Loc),
7273 Constraint =>
7274 Make_Index_Or_Discriminant_Constraint (Loc,
7275 Constraints => List_Constr)),
7276 Def);
7278 -- Otherwise the original subtype_indication is just what is needed
7280 else
7281 Par_Subtype := Process_Subtype (New_Copy_Tree (Indic), Def);
7282 end if;
7284 Set_Parent_Subtype (T, Par_Subtype);
7286 Comp_Decl :=
7287 Make_Component_Declaration (Loc,
7288 Defining_Identifier => Parent_N,
7289 Component_Definition =>
7290 Make_Component_Definition (Loc,
7291 Aliased_Present => False,
7292 Subtype_Indication => New_Occurrence_Of (Par_Subtype, Loc)));
7294 if Null_Present (Rec_Ext_Part) then
7295 Set_Component_List (Rec_Ext_Part,
7296 Make_Component_List (Loc,
7297 Component_Items => New_List (Comp_Decl),
7298 Variant_Part => Empty,
7299 Null_Present => False));
7300 Set_Null_Present (Rec_Ext_Part, False);
7302 elsif Null_Present (Comp_List)
7303 or else Is_Empty_List (Component_Items (Comp_List))
7304 then
7305 Set_Component_Items (Comp_List, New_List (Comp_Decl));
7306 Set_Null_Present (Comp_List, False);
7308 else
7309 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
7310 end if;
7312 Analyze (Comp_Decl);
7313 end Expand_Record_Extension;
7315 ------------------------
7316 -- Expand_Tagged_Root --
7317 ------------------------
7319 procedure Expand_Tagged_Root (T : Entity_Id) is
7320 Def : constant Node_Id := Type_Definition (Parent (T));
7321 Comp_List : Node_Id;
7322 Comp_Decl : Node_Id;
7323 Sloc_N : Source_Ptr;
7325 begin
7326 if Null_Present (Def) then
7327 Set_Component_List (Def,
7328 Make_Component_List (Sloc (Def),
7329 Component_Items => Empty_List,
7330 Variant_Part => Empty,
7331 Null_Present => True));
7332 end if;
7334 Comp_List := Component_List (Def);
7336 if Null_Present (Comp_List)
7337 or else Is_Empty_List (Component_Items (Comp_List))
7338 then
7339 Sloc_N := Sloc (Comp_List);
7340 else
7341 Sloc_N := Sloc (First (Component_Items (Comp_List)));
7342 end if;
7344 Comp_Decl :=
7345 Make_Component_Declaration (Sloc_N,
7346 Defining_Identifier => First_Tag_Component (T),
7347 Component_Definition =>
7348 Make_Component_Definition (Sloc_N,
7349 Aliased_Present => False,
7350 Subtype_Indication => New_Occurrence_Of (RTE (RE_Tag), Sloc_N)));
7352 if Null_Present (Comp_List)
7353 or else Is_Empty_List (Component_Items (Comp_List))
7354 then
7355 Set_Component_Items (Comp_List, New_List (Comp_Decl));
7356 Set_Null_Present (Comp_List, False);
7358 else
7359 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
7360 end if;
7362 -- We don't Analyze the whole expansion because the tag component has
7363 -- already been analyzed previously. Here we just insure that the tree
7364 -- is coherent with the semantic decoration
7366 Find_Type (Subtype_Indication (Component_Definition (Comp_Decl)));
7368 exception
7369 when RE_Not_Available =>
7370 return;
7371 end Expand_Tagged_Root;
7373 ------------------------------
7374 -- Freeze_Stream_Operations --
7375 ------------------------------
7377 procedure Freeze_Stream_Operations (N : Node_Id; Typ : Entity_Id) is
7378 Names : constant array (1 .. 4) of TSS_Name_Type :=
7379 (TSS_Stream_Input,
7380 TSS_Stream_Output,
7381 TSS_Stream_Read,
7382 TSS_Stream_Write);
7383 Stream_Op : Entity_Id;
7385 begin
7386 -- Primitive operations of tagged types are frozen when the dispatch
7387 -- table is constructed.
7389 if not Comes_From_Source (Typ) or else Is_Tagged_Type (Typ) then
7390 return;
7391 end if;
7393 for J in Names'Range loop
7394 Stream_Op := TSS (Typ, Names (J));
7396 if Present (Stream_Op)
7397 and then Is_Subprogram (Stream_Op)
7398 and then Nkind (Unit_Declaration_Node (Stream_Op)) =
7399 N_Subprogram_Declaration
7400 and then not Is_Frozen (Stream_Op)
7401 then
7402 Append_Freeze_Actions (Typ, Freeze_Entity (Stream_Op, N));
7403 end if;
7404 end loop;
7405 end Freeze_Stream_Operations;
7407 -----------------
7408 -- Freeze_Type --
7409 -----------------
7411 -- Full type declarations are expanded at the point at which the type is
7412 -- frozen. The formal N is the Freeze_Node for the type. Any statements or
7413 -- declarations generated by the freezing (e.g. the procedure generated
7414 -- for initialization) are chained in the Actions field list of the freeze
7415 -- node using Append_Freeze_Actions.
7417 -- WARNING: This routine manages Ghost regions. Return statements must be
7418 -- replaced by gotos which jump to the end of the routine and restore the
7419 -- Ghost mode.
7421 function Freeze_Type (N : Node_Id) return Boolean is
7422 procedure Process_RACW_Types (Typ : Entity_Id);
7423 -- Validate and generate stubs for all RACW types associated with type
7424 -- Typ.
7426 procedure Process_Pending_Access_Types (Typ : Entity_Id);
7427 -- Associate type Typ's Finalize_Address primitive with the finalization
7428 -- masters of pending access-to-Typ types.
7430 ------------------------
7431 -- Process_RACW_Types --
7432 ------------------------
7434 procedure Process_RACW_Types (Typ : Entity_Id) is
7435 List : constant Elist_Id := Access_Types_To_Process (N);
7436 E : Elmt_Id;
7437 Seen : Boolean := False;
7439 begin
7440 if Present (List) then
7441 E := First_Elmt (List);
7442 while Present (E) loop
7443 if Is_Remote_Access_To_Class_Wide_Type (Node (E)) then
7444 Validate_RACW_Primitives (Node (E));
7445 Seen := True;
7446 end if;
7448 Next_Elmt (E);
7449 end loop;
7450 end if;
7452 -- If there are RACWs designating this type, make stubs now
7454 if Seen then
7455 Remote_Types_Tagged_Full_View_Encountered (Typ);
7456 end if;
7457 end Process_RACW_Types;
7459 ----------------------------------
7460 -- Process_Pending_Access_Types --
7461 ----------------------------------
7463 procedure Process_Pending_Access_Types (Typ : Entity_Id) is
7464 E : Elmt_Id;
7466 begin
7467 -- Finalize_Address is not generated in CodePeer mode because the
7468 -- body contains address arithmetic. This processing is disabled.
7470 if CodePeer_Mode then
7471 null;
7473 -- Certain itypes are generated for contexts that cannot allocate
7474 -- objects and should not set primitive Finalize_Address.
7476 elsif Is_Itype (Typ)
7477 and then Nkind (Associated_Node_For_Itype (Typ)) =
7478 N_Explicit_Dereference
7479 then
7480 null;
7482 -- When an access type is declared after the incomplete view of a
7483 -- Taft-amendment type, the access type is considered pending in
7484 -- case the full view of the Taft-amendment type is controlled. If
7485 -- this is indeed the case, associate the Finalize_Address routine
7486 -- of the full view with the finalization masters of all pending
7487 -- access types. This scenario applies to anonymous access types as
7488 -- well.
7490 elsif Needs_Finalization (Typ)
7491 and then Present (Pending_Access_Types (Typ))
7492 then
7493 E := First_Elmt (Pending_Access_Types (Typ));
7494 while Present (E) loop
7496 -- Generate:
7497 -- Set_Finalize_Address
7498 -- (Ptr_Typ, <Typ>FD'Unrestricted_Access);
7500 Append_Freeze_Action (Typ,
7501 Make_Set_Finalize_Address_Call
7502 (Loc => Sloc (N),
7503 Ptr_Typ => Node (E)));
7505 Next_Elmt (E);
7506 end loop;
7507 end if;
7508 end Process_Pending_Access_Types;
7510 -- Local variables
7512 Def_Id : constant Entity_Id := Entity (N);
7514 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
7515 -- Save the Ghost mode to restore on exit
7517 Result : Boolean := False;
7519 -- Start of processing for Freeze_Type
7521 begin
7522 -- The type being frozen may be subject to pragma Ghost. Set the mode
7523 -- now to ensure that any nodes generated during freezing are properly
7524 -- marked as Ghost.
7526 Set_Ghost_Mode (Def_Id);
7528 -- Process any remote access-to-class-wide types designating the type
7529 -- being frozen.
7531 Process_RACW_Types (Def_Id);
7533 -- Freeze processing for record types
7535 if Is_Record_Type (Def_Id) then
7536 if Ekind (Def_Id) = E_Record_Type then
7537 Expand_Freeze_Record_Type (N);
7538 elsif Is_Class_Wide_Type (Def_Id) then
7539 Expand_Freeze_Class_Wide_Type (N);
7540 end if;
7542 -- Freeze processing for array types
7544 elsif Is_Array_Type (Def_Id) then
7545 Expand_Freeze_Array_Type (N);
7547 -- Freeze processing for access types
7549 -- For pool-specific access types, find out the pool object used for
7550 -- this type, needs actual expansion of it in some cases. Here are the
7551 -- different cases :
7553 -- 1. Rep Clause "for Def_Id'Storage_Size use 0;"
7554 -- ---> don't use any storage pool
7556 -- 2. Rep Clause : for Def_Id'Storage_Size use Expr.
7557 -- Expand:
7558 -- Def_Id__Pool : Stack_Bounded_Pool (Expr, DT'Size, DT'Alignment);
7560 -- 3. Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
7561 -- ---> Storage Pool is the specified one
7563 -- See GNAT Pool packages in the Run-Time for more details
7565 elsif Ekind_In (Def_Id, E_Access_Type, E_General_Access_Type) then
7566 declare
7567 Loc : constant Source_Ptr := Sloc (N);
7568 Desig_Type : constant Entity_Id := Designated_Type (Def_Id);
7570 Freeze_Action_Typ : Entity_Id;
7571 Pool_Object : Entity_Id;
7573 begin
7574 -- Case 1
7576 -- Rep Clause "for Def_Id'Storage_Size use 0;"
7577 -- ---> don't use any storage pool
7579 if No_Pool_Assigned (Def_Id) then
7580 null;
7582 -- Case 2
7584 -- Rep Clause : for Def_Id'Storage_Size use Expr.
7585 -- ---> Expand:
7586 -- Def_Id__Pool : Stack_Bounded_Pool
7587 -- (Expr, DT'Size, DT'Alignment);
7589 elsif Has_Storage_Size_Clause (Def_Id) then
7590 declare
7591 DT_Align : Node_Id;
7592 DT_Size : Node_Id;
7594 begin
7595 -- For unconstrained composite types we give a size of zero
7596 -- so that the pool knows that it needs a special algorithm
7597 -- for variable size object allocation.
7599 if Is_Composite_Type (Desig_Type)
7600 and then not Is_Constrained (Desig_Type)
7601 then
7602 DT_Size := Make_Integer_Literal (Loc, 0);
7603 DT_Align := Make_Integer_Literal (Loc, Maximum_Alignment);
7605 else
7606 DT_Size :=
7607 Make_Attribute_Reference (Loc,
7608 Prefix => New_Occurrence_Of (Desig_Type, Loc),
7609 Attribute_Name => Name_Max_Size_In_Storage_Elements);
7611 DT_Align :=
7612 Make_Attribute_Reference (Loc,
7613 Prefix => New_Occurrence_Of (Desig_Type, Loc),
7614 Attribute_Name => Name_Alignment);
7615 end if;
7617 Pool_Object :=
7618 Make_Defining_Identifier (Loc,
7619 Chars => New_External_Name (Chars (Def_Id), 'P'));
7621 -- We put the code associated with the pools in the entity
7622 -- that has the later freeze node, usually the access type
7623 -- but it can also be the designated_type; because the pool
7624 -- code requires both those types to be frozen
7626 if Is_Frozen (Desig_Type)
7627 and then (No (Freeze_Node (Desig_Type))
7628 or else Analyzed (Freeze_Node (Desig_Type)))
7629 then
7630 Freeze_Action_Typ := Def_Id;
7632 -- A Taft amendment type cannot get the freeze actions
7633 -- since the full view is not there.
7635 elsif Is_Incomplete_Or_Private_Type (Desig_Type)
7636 and then No (Full_View (Desig_Type))
7637 then
7638 Freeze_Action_Typ := Def_Id;
7640 else
7641 Freeze_Action_Typ := Desig_Type;
7642 end if;
7644 Append_Freeze_Action (Freeze_Action_Typ,
7645 Make_Object_Declaration (Loc,
7646 Defining_Identifier => Pool_Object,
7647 Object_Definition =>
7648 Make_Subtype_Indication (Loc,
7649 Subtype_Mark =>
7650 New_Occurrence_Of
7651 (RTE (RE_Stack_Bounded_Pool), Loc),
7653 Constraint =>
7654 Make_Index_Or_Discriminant_Constraint (Loc,
7655 Constraints => New_List (
7657 -- First discriminant is the Pool Size
7659 New_Occurrence_Of (
7660 Storage_Size_Variable (Def_Id), Loc),
7662 -- Second discriminant is the element size
7664 DT_Size,
7666 -- Third discriminant is the alignment
7668 DT_Align)))));
7669 end;
7671 Set_Associated_Storage_Pool (Def_Id, Pool_Object);
7673 -- Case 3
7675 -- Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
7676 -- ---> Storage Pool is the specified one
7678 -- When compiling in Ada 2012 mode, ensure that the accessibility
7679 -- level of the subpool access type is not deeper than that of the
7680 -- pool_with_subpools.
7682 elsif Ada_Version >= Ada_2012
7683 and then Present (Associated_Storage_Pool (Def_Id))
7685 -- Omit this check for the case of a configurable run-time that
7686 -- does not provide package System.Storage_Pools.Subpools.
7688 and then RTE_Available (RE_Root_Storage_Pool_With_Subpools)
7689 then
7690 declare
7691 Loc : constant Source_Ptr := Sloc (Def_Id);
7692 Pool : constant Entity_Id :=
7693 Associated_Storage_Pool (Def_Id);
7694 RSPWS : constant Entity_Id :=
7695 RTE (RE_Root_Storage_Pool_With_Subpools);
7697 begin
7698 -- It is known that the accessibility level of the access
7699 -- type is deeper than that of the pool.
7701 if Type_Access_Level (Def_Id) > Object_Access_Level (Pool)
7702 and then not Accessibility_Checks_Suppressed (Def_Id)
7703 and then not Accessibility_Checks_Suppressed (Pool)
7704 then
7705 -- Static case: the pool is known to be a descendant of
7706 -- Root_Storage_Pool_With_Subpools.
7708 if Is_Ancestor (RSPWS, Etype (Pool)) then
7709 Error_Msg_N
7710 ("??subpool access type has deeper accessibility "
7711 & "level than pool", Def_Id);
7713 Append_Freeze_Action (Def_Id,
7714 Make_Raise_Program_Error (Loc,
7715 Reason => PE_Accessibility_Check_Failed));
7717 -- Dynamic case: when the pool is of a class-wide type,
7718 -- it may or may not support subpools depending on the
7719 -- path of derivation. Generate:
7721 -- if Def_Id in RSPWS'Class then
7722 -- raise Program_Error;
7723 -- end if;
7725 elsif Is_Class_Wide_Type (Etype (Pool)) then
7726 Append_Freeze_Action (Def_Id,
7727 Make_If_Statement (Loc,
7728 Condition =>
7729 Make_In (Loc,
7730 Left_Opnd => New_Occurrence_Of (Pool, Loc),
7731 Right_Opnd =>
7732 New_Occurrence_Of
7733 (Class_Wide_Type (RSPWS), Loc)),
7735 Then_Statements => New_List (
7736 Make_Raise_Program_Error (Loc,
7737 Reason => PE_Accessibility_Check_Failed))));
7738 end if;
7739 end if;
7740 end;
7741 end if;
7743 -- For access-to-controlled types (including class-wide types and
7744 -- Taft-amendment types, which potentially have controlled
7745 -- components), expand the list controller object that will store
7746 -- the dynamically allocated objects. Don't do this transformation
7747 -- for expander-generated access types, but do it for types that
7748 -- are the full view of types derived from other private types.
7749 -- Also suppress the list controller in the case of a designated
7750 -- type with convention Java, since this is used when binding to
7751 -- Java API specs, where there's no equivalent of a finalization
7752 -- list and we don't want to pull in the finalization support if
7753 -- not needed.
7755 if not Comes_From_Source (Def_Id)
7756 and then not Has_Private_Declaration (Def_Id)
7757 then
7758 null;
7760 -- An exception is made for types defined in the run-time because
7761 -- Ada.Tags.Tag itself is such a type and cannot afford this
7762 -- unnecessary overhead that would generates a loop in the
7763 -- expansion scheme. Another exception is if Restrictions
7764 -- (No_Finalization) is active, since then we know nothing is
7765 -- controlled.
7767 elsif Restriction_Active (No_Finalization)
7768 or else In_Runtime (Def_Id)
7769 then
7770 null;
7772 -- Create a finalization master for an access-to-controlled type
7773 -- or an access-to-incomplete type. It is assumed that the full
7774 -- view will be controlled.
7776 elsif Needs_Finalization (Desig_Type)
7777 or else (Is_Incomplete_Type (Desig_Type)
7778 and then No (Full_View (Desig_Type)))
7779 then
7780 Build_Finalization_Master (Def_Id);
7782 -- Create a finalization master when the designated type contains
7783 -- a private component. It is assumed that the full view will be
7784 -- controlled.
7786 elsif Has_Private_Component (Desig_Type) then
7787 Build_Finalization_Master
7788 (Typ => Def_Id,
7789 For_Private => True,
7790 Context_Scope => Scope (Def_Id),
7791 Insertion_Node => Declaration_Node (Desig_Type));
7792 end if;
7793 end;
7795 -- Freeze processing for enumeration types
7797 elsif Ekind (Def_Id) = E_Enumeration_Type then
7799 -- We only have something to do if we have a non-standard
7800 -- representation (i.e. at least one literal whose pos value
7801 -- is not the same as its representation)
7803 if Has_Non_Standard_Rep (Def_Id) then
7804 Expand_Freeze_Enumeration_Type (N);
7805 end if;
7807 -- Private types that are completed by a derivation from a private
7808 -- type have an internally generated full view, that needs to be
7809 -- frozen. This must be done explicitly because the two views share
7810 -- the freeze node, and the underlying full view is not visible when
7811 -- the freeze node is analyzed.
7813 elsif Is_Private_Type (Def_Id)
7814 and then Is_Derived_Type (Def_Id)
7815 and then Present (Full_View (Def_Id))
7816 and then Is_Itype (Full_View (Def_Id))
7817 and then Has_Private_Declaration (Full_View (Def_Id))
7818 and then Freeze_Node (Full_View (Def_Id)) = N
7819 then
7820 Set_Entity (N, Full_View (Def_Id));
7821 Result := Freeze_Type (N);
7822 Set_Entity (N, Def_Id);
7824 -- All other types require no expander action. There are such cases
7825 -- (e.g. task types and protected types). In such cases, the freeze
7826 -- nodes are there for use by Gigi.
7828 end if;
7830 -- Complete the initialization of all pending access types' finalization
7831 -- masters now that the designated type has been is frozen and primitive
7832 -- Finalize_Address generated.
7834 Process_Pending_Access_Types (Def_Id);
7835 Freeze_Stream_Operations (N, Def_Id);
7837 -- Generate the [spec and] body of the procedure tasked with the runtime
7838 -- verification of pragma Default_Initial_Condition's expression.
7840 if Has_DIC (Def_Id) then
7841 Build_DIC_Procedure_Body (Def_Id, For_Freeze => True);
7842 end if;
7844 -- Generate the [spec and] body of the invariant procedure tasked with
7845 -- the runtime verification of all invariants that pertain to the type.
7846 -- This includes invariants on the partial and full view, inherited
7847 -- class-wide invariants from parent types or interfaces, and invariants
7848 -- on array elements or record components.
7850 if Is_Interface (Def_Id) then
7852 -- Interfaces are treated as the partial view of a private type in
7853 -- order to achieve uniformity with the general case. As a result, an
7854 -- interface receives only a "partial" invariant procedure which is
7855 -- never called.
7857 if Has_Own_Invariants (Def_Id) then
7858 Build_Invariant_Procedure_Body
7859 (Typ => Def_Id,
7860 Partial_Invariant => Is_Interface (Def_Id));
7861 end if;
7863 -- Non-interface types
7865 -- Do not generate invariant procedure within other assertion
7866 -- subprograms, which may involve local declarations of local
7867 -- subtypes to which these checks do not apply.
7869 elsif Has_Invariants (Def_Id) then
7870 if Within_Internal_Subprogram
7871 or else (Ekind (Current_Scope) = E_Function
7872 and then Is_Predicate_Function (Current_Scope))
7873 then
7874 null;
7875 else
7876 Build_Invariant_Procedure_Body (Def_Id);
7877 end if;
7878 end if;
7880 Restore_Ghost_Mode (Saved_GM);
7882 return Result;
7884 exception
7885 when RE_Not_Available =>
7886 Restore_Ghost_Mode (Saved_GM);
7888 return False;
7889 end Freeze_Type;
7891 -------------------------
7892 -- Get_Simple_Init_Val --
7893 -------------------------
7895 function Get_Simple_Init_Val
7896 (T : Entity_Id;
7897 N : Node_Id;
7898 Size : Uint := No_Uint) return Node_Id
7900 Loc : constant Source_Ptr := Sloc (N);
7901 Val : Node_Id;
7902 Result : Node_Id;
7903 Val_RE : RE_Id;
7905 Size_To_Use : Uint;
7906 -- This is the size to be used for computation of the appropriate
7907 -- initial value for the Normalize_Scalars and Initialize_Scalars case.
7909 IV_Attribute : constant Boolean :=
7910 Nkind (N) = N_Attribute_Reference
7911 and then Attribute_Name (N) = Name_Invalid_Value;
7913 Lo_Bound : Uint;
7914 Hi_Bound : Uint;
7915 -- These are the values computed by the procedure Check_Subtype_Bounds
7917 procedure Check_Subtype_Bounds;
7918 -- This procedure examines the subtype T, and its ancestor subtypes and
7919 -- derived types to determine the best known information about the
7920 -- bounds of the subtype. After the call Lo_Bound is set either to
7921 -- No_Uint if no information can be determined, or to a value which
7922 -- represents a known low bound, i.e. a valid value of the subtype can
7923 -- not be less than this value. Hi_Bound is similarly set to a known
7924 -- high bound (valid value cannot be greater than this).
7926 --------------------------
7927 -- Check_Subtype_Bounds --
7928 --------------------------
7930 procedure Check_Subtype_Bounds is
7931 ST1 : Entity_Id;
7932 ST2 : Entity_Id;
7933 Lo : Node_Id;
7934 Hi : Node_Id;
7935 Loval : Uint;
7936 Hival : Uint;
7938 begin
7939 Lo_Bound := No_Uint;
7940 Hi_Bound := No_Uint;
7942 -- Loop to climb ancestor subtypes and derived types
7944 ST1 := T;
7945 loop
7946 if not Is_Discrete_Type (ST1) then
7947 return;
7948 end if;
7950 Lo := Type_Low_Bound (ST1);
7951 Hi := Type_High_Bound (ST1);
7953 if Compile_Time_Known_Value (Lo) then
7954 Loval := Expr_Value (Lo);
7956 if Lo_Bound = No_Uint or else Lo_Bound < Loval then
7957 Lo_Bound := Loval;
7958 end if;
7959 end if;
7961 if Compile_Time_Known_Value (Hi) then
7962 Hival := Expr_Value (Hi);
7964 if Hi_Bound = No_Uint or else Hi_Bound > Hival then
7965 Hi_Bound := Hival;
7966 end if;
7967 end if;
7969 ST2 := Ancestor_Subtype (ST1);
7971 if No (ST2) then
7972 ST2 := Etype (ST1);
7973 end if;
7975 exit when ST1 = ST2;
7976 ST1 := ST2;
7977 end loop;
7978 end Check_Subtype_Bounds;
7980 -- Start of processing for Get_Simple_Init_Val
7982 begin
7983 -- For a private type, we should always have an underlying type (because
7984 -- this was already checked in Needs_Simple_Initialization). What we do
7985 -- is to get the value for the underlying type and then do an unchecked
7986 -- conversion to the private type.
7988 if Is_Private_Type (T) then
7989 Val := Get_Simple_Init_Val (Underlying_Type (T), N, Size);
7991 -- A special case, if the underlying value is null, then qualify it
7992 -- with the underlying type, so that the null is properly typed.
7993 -- Similarly, if it is an aggregate it must be qualified, because an
7994 -- unchecked conversion does not provide a context for it.
7996 if Nkind_In (Val, N_Null, N_Aggregate) then
7997 Val :=
7998 Make_Qualified_Expression (Loc,
7999 Subtype_Mark =>
8000 New_Occurrence_Of (Underlying_Type (T), Loc),
8001 Expression => Val);
8002 end if;
8004 Result := Unchecked_Convert_To (T, Val);
8006 -- Don't truncate result (important for Initialize/Normalize_Scalars)
8008 if Nkind (Result) = N_Unchecked_Type_Conversion
8009 and then Is_Scalar_Type (Underlying_Type (T))
8010 then
8011 Set_No_Truncation (Result);
8012 end if;
8014 return Result;
8016 -- Scalars with Default_Value aspect. The first subtype may now be
8017 -- private, so retrieve value from underlying type.
8019 elsif Is_Scalar_Type (T) and then Has_Default_Aspect (T) then
8020 if Is_Private_Type (First_Subtype (T)) then
8021 return Unchecked_Convert_To (T,
8022 Default_Aspect_Value (Full_View (First_Subtype (T))));
8023 else
8024 return
8025 Convert_To (T, Default_Aspect_Value (First_Subtype (T)));
8026 end if;
8028 -- Otherwise, for scalars, we must have normalize/initialize scalars
8029 -- case, or if the node N is an 'Invalid_Value attribute node.
8031 elsif Is_Scalar_Type (T) then
8032 pragma Assert (Init_Or_Norm_Scalars or IV_Attribute);
8034 -- Compute size of object. If it is given by the caller, we can use
8035 -- it directly, otherwise we use Esize (T) as an estimate. As far as
8036 -- we know this covers all cases correctly.
8038 if Size = No_Uint or else Size <= Uint_0 then
8039 Size_To_Use := UI_Max (Uint_1, Esize (T));
8040 else
8041 Size_To_Use := Size;
8042 end if;
8044 -- Maximum size to use is 64 bits, since we will create values of
8045 -- type Unsigned_64 and the range must fit this type.
8047 if Size_To_Use /= No_Uint and then Size_To_Use > Uint_64 then
8048 Size_To_Use := Uint_64;
8049 end if;
8051 -- Check known bounds of subtype
8053 Check_Subtype_Bounds;
8055 -- Processing for Normalize_Scalars case
8057 if Normalize_Scalars and then not IV_Attribute then
8059 -- If zero is invalid, it is a convenient value to use that is
8060 -- for sure an appropriate invalid value in all situations.
8062 if Lo_Bound /= No_Uint and then Lo_Bound > Uint_0 then
8063 Val := Make_Integer_Literal (Loc, 0);
8065 -- Cases where all one bits is the appropriate invalid value
8067 -- For modular types, all 1 bits is either invalid or valid. If
8068 -- it is valid, then there is nothing that can be done since there
8069 -- are no invalid values (we ruled out zero already).
8071 -- For signed integer types that have no negative values, either
8072 -- there is room for negative values, or there is not. If there
8073 -- is, then all 1-bits may be interpreted as minus one, which is
8074 -- certainly invalid. Alternatively it is treated as the largest
8075 -- positive value, in which case the observation for modular types
8076 -- still applies.
8078 -- For float types, all 1-bits is a NaN (not a number), which is
8079 -- certainly an appropriately invalid value.
8081 elsif Is_Unsigned_Type (T)
8082 or else Is_Floating_Point_Type (T)
8083 or else Is_Enumeration_Type (T)
8084 then
8085 Val := Make_Integer_Literal (Loc, 2 ** Size_To_Use - 1);
8087 -- Resolve as Unsigned_64, because the largest number we can
8088 -- generate is out of range of universal integer.
8090 Analyze_And_Resolve (Val, RTE (RE_Unsigned_64));
8092 -- Case of signed types
8094 else
8095 declare
8096 Signed_Size : constant Uint :=
8097 UI_Min (Uint_63, Size_To_Use - 1);
8099 begin
8100 -- Normally we like to use the most negative number. The one
8101 -- exception is when this number is in the known subtype
8102 -- range and the largest positive number is not in the known
8103 -- subtype range.
8105 -- For this exceptional case, use largest positive value
8107 if Lo_Bound /= No_Uint and then Hi_Bound /= No_Uint
8108 and then Lo_Bound <= (-(2 ** Signed_Size))
8109 and then Hi_Bound < 2 ** Signed_Size
8110 then
8111 Val := Make_Integer_Literal (Loc, 2 ** Signed_Size - 1);
8113 -- Normal case of largest negative value
8115 else
8116 Val := Make_Integer_Literal (Loc, -(2 ** Signed_Size));
8117 end if;
8118 end;
8119 end if;
8121 -- Here for Initialize_Scalars case (or Invalid_Value attribute used)
8123 else
8124 -- For float types, use float values from System.Scalar_Values
8126 if Is_Floating_Point_Type (T) then
8127 if Root_Type (T) = Standard_Short_Float then
8128 Val_RE := RE_IS_Isf;
8129 elsif Root_Type (T) = Standard_Float then
8130 Val_RE := RE_IS_Ifl;
8131 elsif Root_Type (T) = Standard_Long_Float then
8132 Val_RE := RE_IS_Ilf;
8133 else pragma Assert (Root_Type (T) = Standard_Long_Long_Float);
8134 Val_RE := RE_IS_Ill;
8135 end if;
8137 -- If zero is invalid, use zero values from System.Scalar_Values
8139 elsif Lo_Bound /= No_Uint and then Lo_Bound > Uint_0 then
8140 if Size_To_Use <= 8 then
8141 Val_RE := RE_IS_Iz1;
8142 elsif Size_To_Use <= 16 then
8143 Val_RE := RE_IS_Iz2;
8144 elsif Size_To_Use <= 32 then
8145 Val_RE := RE_IS_Iz4;
8146 else
8147 Val_RE := RE_IS_Iz8;
8148 end if;
8150 -- For unsigned, use unsigned values from System.Scalar_Values
8152 elsif Is_Unsigned_Type (T) then
8153 if Size_To_Use <= 8 then
8154 Val_RE := RE_IS_Iu1;
8155 elsif Size_To_Use <= 16 then
8156 Val_RE := RE_IS_Iu2;
8157 elsif Size_To_Use <= 32 then
8158 Val_RE := RE_IS_Iu4;
8159 else
8160 Val_RE := RE_IS_Iu8;
8161 end if;
8163 -- For signed, use signed values from System.Scalar_Values
8165 else
8166 if Size_To_Use <= 8 then
8167 Val_RE := RE_IS_Is1;
8168 elsif Size_To_Use <= 16 then
8169 Val_RE := RE_IS_Is2;
8170 elsif Size_To_Use <= 32 then
8171 Val_RE := RE_IS_Is4;
8172 else
8173 Val_RE := RE_IS_Is8;
8174 end if;
8175 end if;
8177 Val := New_Occurrence_Of (RTE (Val_RE), Loc);
8178 end if;
8180 -- The final expression is obtained by doing an unchecked conversion
8181 -- of this result to the base type of the required subtype. Use the
8182 -- base type to prevent the unchecked conversion from chopping bits,
8183 -- and then we set Kill_Range_Check to preserve the "bad" value.
8185 Result := Unchecked_Convert_To (Base_Type (T), Val);
8187 -- Ensure result is not truncated, since we want the "bad" bits, and
8188 -- also kill range check on result.
8190 if Nkind (Result) = N_Unchecked_Type_Conversion then
8191 Set_No_Truncation (Result);
8192 Set_Kill_Range_Check (Result, True);
8193 end if;
8195 return Result;
8197 -- String or Wide_[Wide]_String (must have Initialize_Scalars set)
8199 elsif Is_Standard_String_Type (T) then
8200 pragma Assert (Init_Or_Norm_Scalars);
8202 return
8203 Make_Aggregate (Loc,
8204 Component_Associations => New_List (
8205 Make_Component_Association (Loc,
8206 Choices => New_List (
8207 Make_Others_Choice (Loc)),
8208 Expression =>
8209 Get_Simple_Init_Val
8210 (Component_Type (T), N, Esize (Root_Type (T))))));
8212 -- Access type is initialized to null
8214 elsif Is_Access_Type (T) then
8215 return Make_Null (Loc);
8217 -- No other possibilities should arise, since we should only be calling
8218 -- Get_Simple_Init_Val if Needs_Simple_Initialization returned True,
8219 -- indicating one of the above cases held.
8221 else
8222 raise Program_Error;
8223 end if;
8225 exception
8226 when RE_Not_Available =>
8227 return Empty;
8228 end Get_Simple_Init_Val;
8230 ------------------------------
8231 -- Has_New_Non_Standard_Rep --
8232 ------------------------------
8234 function Has_New_Non_Standard_Rep (T : Entity_Id) return Boolean is
8235 begin
8236 if not Is_Derived_Type (T) then
8237 return Has_Non_Standard_Rep (T)
8238 or else Has_Non_Standard_Rep (Root_Type (T));
8240 -- If Has_Non_Standard_Rep is not set on the derived type, the
8241 -- representation is fully inherited.
8243 elsif not Has_Non_Standard_Rep (T) then
8244 return False;
8246 else
8247 return First_Rep_Item (T) /= First_Rep_Item (Root_Type (T));
8249 -- May need a more precise check here: the First_Rep_Item may be a
8250 -- stream attribute, which does not affect the representation of the
8251 -- type ???
8253 end if;
8254 end Has_New_Non_Standard_Rep;
8256 ----------------------
8257 -- Inline_Init_Proc --
8258 ----------------------
8260 function Inline_Init_Proc (Typ : Entity_Id) return Boolean is
8261 begin
8262 -- The initialization proc of protected records is not worth inlining.
8263 -- In addition, when compiled for another unit for inlining purposes,
8264 -- it may make reference to entities that have not been elaborated yet.
8265 -- The initialization proc of records that need finalization contains
8266 -- a nested clean-up procedure that makes it impractical to inline as
8267 -- well, except for simple controlled types themselves. And similar
8268 -- considerations apply to task types.
8270 if Is_Concurrent_Type (Typ) then
8271 return False;
8273 elsif Needs_Finalization (Typ) and then not Is_Controlled (Typ) then
8274 return False;
8276 elsif Has_Task (Typ) then
8277 return False;
8279 else
8280 return True;
8281 end if;
8282 end Inline_Init_Proc;
8284 ----------------
8285 -- In_Runtime --
8286 ----------------
8288 function In_Runtime (E : Entity_Id) return Boolean is
8289 S1 : Entity_Id;
8291 begin
8292 S1 := Scope (E);
8293 while Scope (S1) /= Standard_Standard loop
8294 S1 := Scope (S1);
8295 end loop;
8297 return Is_RTU (S1, System) or else Is_RTU (S1, Ada);
8298 end In_Runtime;
8300 ----------------------------
8301 -- Initialization_Warning --
8302 ----------------------------
8304 procedure Initialization_Warning (E : Entity_Id) is
8305 Warning_Needed : Boolean;
8307 begin
8308 Warning_Needed := False;
8310 if Ekind (Current_Scope) = E_Package
8311 and then Static_Elaboration_Desired (Current_Scope)
8312 then
8313 if Is_Type (E) then
8314 if Is_Record_Type (E) then
8315 if Has_Discriminants (E)
8316 or else Is_Limited_Type (E)
8317 or else Has_Non_Standard_Rep (E)
8318 then
8319 Warning_Needed := True;
8321 else
8322 -- Verify that at least one component has an initialization
8323 -- expression. No need for a warning on a type if all its
8324 -- components have no initialization.
8326 declare
8327 Comp : Entity_Id;
8329 begin
8330 Comp := First_Component (E);
8331 while Present (Comp) loop
8332 if Ekind (Comp) = E_Discriminant
8333 or else
8334 (Nkind (Parent (Comp)) = N_Component_Declaration
8335 and then Present (Expression (Parent (Comp))))
8336 then
8337 Warning_Needed := True;
8338 exit;
8339 end if;
8341 Next_Component (Comp);
8342 end loop;
8343 end;
8344 end if;
8346 if Warning_Needed then
8347 Error_Msg_N
8348 ("Objects of the type cannot be initialized statically "
8349 & "by default??", Parent (E));
8350 end if;
8351 end if;
8353 else
8354 Error_Msg_N ("Object cannot be initialized statically??", E);
8355 end if;
8356 end if;
8357 end Initialization_Warning;
8359 ------------------
8360 -- Init_Formals --
8361 ------------------
8363 function Init_Formals (Typ : Entity_Id) return List_Id is
8364 Loc : constant Source_Ptr := Sloc (Typ);
8365 Formals : List_Id;
8367 begin
8368 -- First parameter is always _Init : in out typ. Note that we need this
8369 -- to be in/out because in the case of the task record value, there
8370 -- are default record fields (_Priority, _Size, -Task_Info) that may
8371 -- be referenced in the generated initialization routine.
8373 Formals := New_List (
8374 Make_Parameter_Specification (Loc,
8375 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uInit),
8376 In_Present => True,
8377 Out_Present => True,
8378 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
8380 -- For task record value, or type that contains tasks, add two more
8381 -- formals, _Master : Master_Id and _Chain : in out Activation_Chain
8382 -- We also add these parameters for the task record type case.
8384 if Has_Task (Typ)
8385 or else (Is_Record_Type (Typ) and then Is_Task_Record_Type (Typ))
8386 then
8387 Append_To (Formals,
8388 Make_Parameter_Specification (Loc,
8389 Defining_Identifier =>
8390 Make_Defining_Identifier (Loc, Name_uMaster),
8391 Parameter_Type =>
8392 New_Occurrence_Of (RTE (RE_Master_Id), Loc)));
8394 -- Add _Chain (not done for sequential elaboration policy, see
8395 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
8397 if Partition_Elaboration_Policy /= 'S' then
8398 Append_To (Formals,
8399 Make_Parameter_Specification (Loc,
8400 Defining_Identifier =>
8401 Make_Defining_Identifier (Loc, Name_uChain),
8402 In_Present => True,
8403 Out_Present => True,
8404 Parameter_Type =>
8405 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc)));
8406 end if;
8408 Append_To (Formals,
8409 Make_Parameter_Specification (Loc,
8410 Defining_Identifier =>
8411 Make_Defining_Identifier (Loc, Name_uTask_Name),
8412 In_Present => True,
8413 Parameter_Type => New_Occurrence_Of (Standard_String, Loc)));
8414 end if;
8416 return Formals;
8418 exception
8419 when RE_Not_Available =>
8420 return Empty_List;
8421 end Init_Formals;
8423 -------------------------
8424 -- Init_Secondary_Tags --
8425 -------------------------
8427 procedure Init_Secondary_Tags
8428 (Typ : Entity_Id;
8429 Target : Node_Id;
8430 Init_Tags_List : List_Id;
8431 Stmts_List : List_Id;
8432 Fixed_Comps : Boolean := True;
8433 Variable_Comps : Boolean := True)
8435 Loc : constant Source_Ptr := Sloc (Target);
8437 -- Inherit the C++ tag of the secondary dispatch table of Typ associated
8438 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
8440 procedure Initialize_Tag
8441 (Typ : Entity_Id;
8442 Iface : Entity_Id;
8443 Tag_Comp : Entity_Id;
8444 Iface_Tag : Node_Id);
8445 -- Initialize the tag of the secondary dispatch table of Typ associated
8446 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
8447 -- Compiling under the CPP full ABI compatibility mode, if the ancestor
8448 -- of Typ CPP tagged type we generate code to inherit the contents of
8449 -- the dispatch table directly from the ancestor.
8451 --------------------
8452 -- Initialize_Tag --
8453 --------------------
8455 procedure Initialize_Tag
8456 (Typ : Entity_Id;
8457 Iface : Entity_Id;
8458 Tag_Comp : Entity_Id;
8459 Iface_Tag : Node_Id)
8461 Comp_Typ : Entity_Id;
8462 Offset_To_Top_Comp : Entity_Id := Empty;
8464 begin
8465 -- Initialize pointer to secondary DT associated with the interface
8467 if not Is_Ancestor (Iface, Typ, Use_Full_View => True) then
8468 Append_To (Init_Tags_List,
8469 Make_Assignment_Statement (Loc,
8470 Name =>
8471 Make_Selected_Component (Loc,
8472 Prefix => New_Copy_Tree (Target),
8473 Selector_Name => New_Occurrence_Of (Tag_Comp, Loc)),
8474 Expression =>
8475 New_Occurrence_Of (Iface_Tag, Loc)));
8476 end if;
8478 Comp_Typ := Scope (Tag_Comp);
8480 -- Initialize the entries of the table of interfaces. We generate a
8481 -- different call when the parent of the type has variable size
8482 -- components.
8484 if Comp_Typ /= Etype (Comp_Typ)
8485 and then Is_Variable_Size_Record (Etype (Comp_Typ))
8486 and then Chars (Tag_Comp) /= Name_uTag
8487 then
8488 pragma Assert (Present (DT_Offset_To_Top_Func (Tag_Comp)));
8490 -- Issue error if Set_Dynamic_Offset_To_Top is not available in a
8491 -- configurable run-time environment.
8493 if not RTE_Available (RE_Set_Dynamic_Offset_To_Top) then
8494 Error_Msg_CRT
8495 ("variable size record with interface types", Typ);
8496 return;
8497 end if;
8499 -- Generate:
8500 -- Set_Dynamic_Offset_To_Top
8501 -- (This => Init,
8502 -- Prim_T => Typ'Tag,
8503 -- Interface_T => Iface'Tag,
8504 -- Offset_Value => n,
8505 -- Offset_Func => Fn'Address)
8507 Append_To (Stmts_List,
8508 Make_Procedure_Call_Statement (Loc,
8509 Name =>
8510 New_Occurrence_Of (RTE (RE_Set_Dynamic_Offset_To_Top), Loc),
8511 Parameter_Associations => New_List (
8512 Make_Attribute_Reference (Loc,
8513 Prefix => New_Copy_Tree (Target),
8514 Attribute_Name => Name_Address),
8516 Unchecked_Convert_To (RTE (RE_Tag),
8517 New_Occurrence_Of
8518 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc)),
8520 Unchecked_Convert_To (RTE (RE_Tag),
8521 New_Occurrence_Of
8522 (Node (First_Elmt (Access_Disp_Table (Iface))),
8523 Loc)),
8525 Unchecked_Convert_To
8526 (RTE (RE_Storage_Offset),
8527 Make_Op_Minus (Loc,
8528 Make_Attribute_Reference (Loc,
8529 Prefix =>
8530 Make_Selected_Component (Loc,
8531 Prefix => New_Copy_Tree (Target),
8532 Selector_Name =>
8533 New_Occurrence_Of (Tag_Comp, Loc)),
8534 Attribute_Name => Name_Position))),
8536 Unchecked_Convert_To (RTE (RE_Offset_To_Top_Function_Ptr),
8537 Make_Attribute_Reference (Loc,
8538 Prefix => New_Occurrence_Of
8539 (DT_Offset_To_Top_Func (Tag_Comp), Loc),
8540 Attribute_Name => Name_Address)))));
8542 -- In this case the next component stores the value of the offset
8543 -- to the top.
8545 Offset_To_Top_Comp := Next_Entity (Tag_Comp);
8546 pragma Assert (Present (Offset_To_Top_Comp));
8548 Append_To (Init_Tags_List,
8549 Make_Assignment_Statement (Loc,
8550 Name =>
8551 Make_Selected_Component (Loc,
8552 Prefix => New_Copy_Tree (Target),
8553 Selector_Name =>
8554 New_Occurrence_Of (Offset_To_Top_Comp, Loc)),
8556 Expression =>
8557 Make_Op_Minus (Loc,
8558 Make_Attribute_Reference (Loc,
8559 Prefix =>
8560 Make_Selected_Component (Loc,
8561 Prefix => New_Copy_Tree (Target),
8562 Selector_Name => New_Occurrence_Of (Tag_Comp, Loc)),
8563 Attribute_Name => Name_Position))));
8565 -- Normal case: No discriminants in the parent type
8567 else
8568 -- Don't need to set any value if the offset-to-top field is
8569 -- statically set or if this interface shares the primary
8570 -- dispatch table.
8572 if not Building_Static_Secondary_DT (Typ)
8573 and then not Is_Ancestor (Iface, Typ, Use_Full_View => True)
8574 then
8575 Append_To (Stmts_List,
8576 Build_Set_Static_Offset_To_Top (Loc,
8577 Iface_Tag => New_Occurrence_Of (Iface_Tag, Loc),
8578 Offset_Value =>
8579 Unchecked_Convert_To (RTE (RE_Storage_Offset),
8580 Make_Op_Minus (Loc,
8581 Make_Attribute_Reference (Loc,
8582 Prefix =>
8583 Make_Selected_Component (Loc,
8584 Prefix => New_Copy_Tree (Target),
8585 Selector_Name =>
8586 New_Occurrence_Of (Tag_Comp, Loc)),
8587 Attribute_Name => Name_Position)))));
8588 end if;
8590 -- Generate:
8591 -- Register_Interface_Offset
8592 -- (Prim_T => Typ'Tag,
8593 -- Interface_T => Iface'Tag,
8594 -- Is_Constant => True,
8595 -- Offset_Value => n,
8596 -- Offset_Func => null);
8598 if not Building_Static_Secondary_DT (Typ)
8599 and then RTE_Available (RE_Register_Interface_Offset)
8600 then
8601 Append_To (Stmts_List,
8602 Make_Procedure_Call_Statement (Loc,
8603 Name =>
8604 New_Occurrence_Of
8605 (RTE (RE_Register_Interface_Offset), Loc),
8606 Parameter_Associations => New_List (
8607 Unchecked_Convert_To (RTE (RE_Tag),
8608 New_Occurrence_Of
8609 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc)),
8611 Unchecked_Convert_To (RTE (RE_Tag),
8612 New_Occurrence_Of
8613 (Node (First_Elmt (Access_Disp_Table (Iface))), Loc)),
8615 New_Occurrence_Of (Standard_True, Loc),
8617 Unchecked_Convert_To (RTE (RE_Storage_Offset),
8618 Make_Op_Minus (Loc,
8619 Make_Attribute_Reference (Loc,
8620 Prefix =>
8621 Make_Selected_Component (Loc,
8622 Prefix => New_Copy_Tree (Target),
8623 Selector_Name =>
8624 New_Occurrence_Of (Tag_Comp, Loc)),
8625 Attribute_Name => Name_Position))),
8627 Make_Null (Loc))));
8628 end if;
8629 end if;
8630 end Initialize_Tag;
8632 -- Local variables
8634 Full_Typ : Entity_Id;
8635 Ifaces_List : Elist_Id;
8636 Ifaces_Comp_List : Elist_Id;
8637 Ifaces_Tag_List : Elist_Id;
8638 Iface_Elmt : Elmt_Id;
8639 Iface_Comp_Elmt : Elmt_Id;
8640 Iface_Tag_Elmt : Elmt_Id;
8641 Tag_Comp : Node_Id;
8642 In_Variable_Pos : Boolean;
8644 -- Start of processing for Init_Secondary_Tags
8646 begin
8647 -- Handle private types
8649 if Present (Full_View (Typ)) then
8650 Full_Typ := Full_View (Typ);
8651 else
8652 Full_Typ := Typ;
8653 end if;
8655 Collect_Interfaces_Info
8656 (Full_Typ, Ifaces_List, Ifaces_Comp_List, Ifaces_Tag_List);
8658 Iface_Elmt := First_Elmt (Ifaces_List);
8659 Iface_Comp_Elmt := First_Elmt (Ifaces_Comp_List);
8660 Iface_Tag_Elmt := First_Elmt (Ifaces_Tag_List);
8661 while Present (Iface_Elmt) loop
8662 Tag_Comp := Node (Iface_Comp_Elmt);
8664 -- Check if parent of record type has variable size components
8666 In_Variable_Pos := Scope (Tag_Comp) /= Etype (Scope (Tag_Comp))
8667 and then Is_Variable_Size_Record (Etype (Scope (Tag_Comp)));
8669 -- If we are compiling under the CPP full ABI compatibility mode and
8670 -- the ancestor is a CPP_Pragma tagged type then we generate code to
8671 -- initialize the secondary tag components from tags that reference
8672 -- secondary tables filled with copy of parent slots.
8674 if Is_CPP_Class (Root_Type (Full_Typ)) then
8676 -- Reject interface components located at variable offset in
8677 -- C++ derivations. This is currently unsupported.
8679 if not Fixed_Comps and then In_Variable_Pos then
8681 -- Locate the first dynamic component of the record. Done to
8682 -- improve the text of the warning.
8684 declare
8685 Comp : Entity_Id;
8686 Comp_Typ : Entity_Id;
8688 begin
8689 Comp := First_Entity (Typ);
8690 while Present (Comp) loop
8691 Comp_Typ := Etype (Comp);
8693 if Ekind (Comp) /= E_Discriminant
8694 and then not Is_Tag (Comp)
8695 then
8696 exit when
8697 (Is_Record_Type (Comp_Typ)
8698 and then
8699 Is_Variable_Size_Record (Base_Type (Comp_Typ)))
8700 or else
8701 (Is_Array_Type (Comp_Typ)
8702 and then Is_Variable_Size_Array (Comp_Typ));
8703 end if;
8705 Next_Entity (Comp);
8706 end loop;
8708 pragma Assert (Present (Comp));
8709 Error_Msg_Node_2 := Comp;
8710 Error_Msg_NE
8711 ("parent type & with dynamic component & cannot be parent"
8712 & " of 'C'P'P derivation if new interfaces are present",
8713 Typ, Scope (Original_Record_Component (Comp)));
8715 Error_Msg_Sloc :=
8716 Sloc (Scope (Original_Record_Component (Comp)));
8717 Error_Msg_NE
8718 ("type derived from 'C'P'P type & defined #",
8719 Typ, Scope (Original_Record_Component (Comp)));
8721 -- Avoid duplicated warnings
8723 exit;
8724 end;
8726 -- Initialize secondary tags
8728 else
8729 Initialize_Tag
8730 (Typ => Full_Typ,
8731 Iface => Node (Iface_Elmt),
8732 Tag_Comp => Tag_Comp,
8733 Iface_Tag => Node (Iface_Tag_Elmt));
8734 end if;
8736 -- Otherwise generate code to initialize the tag
8738 else
8739 if (In_Variable_Pos and then Variable_Comps)
8740 or else (not In_Variable_Pos and then Fixed_Comps)
8741 then
8742 Initialize_Tag
8743 (Typ => Full_Typ,
8744 Iface => Node (Iface_Elmt),
8745 Tag_Comp => Tag_Comp,
8746 Iface_Tag => Node (Iface_Tag_Elmt));
8747 end if;
8748 end if;
8750 Next_Elmt (Iface_Elmt);
8751 Next_Elmt (Iface_Comp_Elmt);
8752 Next_Elmt (Iface_Tag_Elmt);
8753 end loop;
8754 end Init_Secondary_Tags;
8756 ------------------------
8757 -- Is_User_Defined_Eq --
8758 ------------------------
8760 function Is_User_Defined_Equality (Prim : Node_Id) return Boolean is
8761 begin
8762 return Chars (Prim) = Name_Op_Eq
8763 and then Etype (First_Formal (Prim)) =
8764 Etype (Next_Formal (First_Formal (Prim)))
8765 and then Base_Type (Etype (Prim)) = Standard_Boolean;
8766 end Is_User_Defined_Equality;
8768 ----------------------------------------
8769 -- Make_Controlling_Function_Wrappers --
8770 ----------------------------------------
8772 procedure Make_Controlling_Function_Wrappers
8773 (Tag_Typ : Entity_Id;
8774 Decl_List : out List_Id;
8775 Body_List : out List_Id)
8777 Loc : constant Source_Ptr := Sloc (Tag_Typ);
8778 Prim_Elmt : Elmt_Id;
8779 Subp : Entity_Id;
8780 Actual_List : List_Id;
8781 Formal_List : List_Id;
8782 Formal : Entity_Id;
8783 Par_Formal : Entity_Id;
8784 Formal_Node : Node_Id;
8785 Func_Body : Node_Id;
8786 Func_Decl : Node_Id;
8787 Func_Spec : Node_Id;
8788 Return_Stmt : Node_Id;
8790 begin
8791 Decl_List := New_List;
8792 Body_List := New_List;
8794 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
8795 while Present (Prim_Elmt) loop
8796 Subp := Node (Prim_Elmt);
8798 -- If a primitive function with a controlling result of the type has
8799 -- not been overridden by the user, then we must create a wrapper
8800 -- function here that effectively overrides it and invokes the
8801 -- (non-abstract) parent function. This can only occur for a null
8802 -- extension. Note that functions with anonymous controlling access
8803 -- results don't qualify and must be overridden. We also exclude
8804 -- Input attributes, since each type will have its own version of
8805 -- Input constructed by the expander. The test for Comes_From_Source
8806 -- is needed to distinguish inherited operations from renamings
8807 -- (which also have Alias set). We exclude internal entities with
8808 -- Interface_Alias to avoid generating duplicated wrappers since
8809 -- the primitive which covers the interface is also available in
8810 -- the list of primitive operations.
8812 -- The function may be abstract, or require_Overriding may be set
8813 -- for it, because tests for null extensions may already have reset
8814 -- the Is_Abstract_Subprogram_Flag. If Requires_Overriding is not
8815 -- set, functions that need wrappers are recognized by having an
8816 -- alias that returns the parent type.
8818 if Comes_From_Source (Subp)
8819 or else No (Alias (Subp))
8820 or else Present (Interface_Alias (Subp))
8821 or else Ekind (Subp) /= E_Function
8822 or else not Has_Controlling_Result (Subp)
8823 or else Is_Access_Type (Etype (Subp))
8824 or else Is_Abstract_Subprogram (Alias (Subp))
8825 or else Is_TSS (Subp, TSS_Stream_Input)
8826 then
8827 goto Next_Prim;
8829 elsif Is_Abstract_Subprogram (Subp)
8830 or else Requires_Overriding (Subp)
8831 or else
8832 (Is_Null_Extension (Etype (Subp))
8833 and then Etype (Alias (Subp)) /= Etype (Subp))
8834 then
8835 Formal_List := No_List;
8836 Formal := First_Formal (Subp);
8838 if Present (Formal) then
8839 Formal_List := New_List;
8841 while Present (Formal) loop
8842 Append
8843 (Make_Parameter_Specification
8844 (Loc,
8845 Defining_Identifier =>
8846 Make_Defining_Identifier (Sloc (Formal),
8847 Chars => Chars (Formal)),
8848 In_Present => In_Present (Parent (Formal)),
8849 Out_Present => Out_Present (Parent (Formal)),
8850 Null_Exclusion_Present =>
8851 Null_Exclusion_Present (Parent (Formal)),
8852 Parameter_Type =>
8853 New_Occurrence_Of (Etype (Formal), Loc),
8854 Expression =>
8855 New_Copy_Tree (Expression (Parent (Formal)))),
8856 Formal_List);
8858 Next_Formal (Formal);
8859 end loop;
8860 end if;
8862 Func_Spec :=
8863 Make_Function_Specification (Loc,
8864 Defining_Unit_Name =>
8865 Make_Defining_Identifier (Loc,
8866 Chars => Chars (Subp)),
8867 Parameter_Specifications => Formal_List,
8868 Result_Definition =>
8869 New_Occurrence_Of (Etype (Subp), Loc));
8871 Func_Decl := Make_Subprogram_Declaration (Loc, Func_Spec);
8872 Append_To (Decl_List, Func_Decl);
8874 -- Build a wrapper body that calls the parent function. The body
8875 -- contains a single return statement that returns an extension
8876 -- aggregate whose ancestor part is a call to the parent function,
8877 -- passing the formals as actuals (with any controlling arguments
8878 -- converted to the types of the corresponding formals of the
8879 -- parent function, which might be anonymous access types), and
8880 -- having a null extension.
8882 Formal := First_Formal (Subp);
8883 Par_Formal := First_Formal (Alias (Subp));
8884 Formal_Node := First (Formal_List);
8886 if Present (Formal) then
8887 Actual_List := New_List;
8888 else
8889 Actual_List := No_List;
8890 end if;
8892 while Present (Formal) loop
8893 if Is_Controlling_Formal (Formal) then
8894 Append_To (Actual_List,
8895 Make_Type_Conversion (Loc,
8896 Subtype_Mark =>
8897 New_Occurrence_Of (Etype (Par_Formal), Loc),
8898 Expression =>
8899 New_Occurrence_Of
8900 (Defining_Identifier (Formal_Node), Loc)));
8901 else
8902 Append_To
8903 (Actual_List,
8904 New_Occurrence_Of
8905 (Defining_Identifier (Formal_Node), Loc));
8906 end if;
8908 Next_Formal (Formal);
8909 Next_Formal (Par_Formal);
8910 Next (Formal_Node);
8911 end loop;
8913 Return_Stmt :=
8914 Make_Simple_Return_Statement (Loc,
8915 Expression =>
8916 Make_Extension_Aggregate (Loc,
8917 Ancestor_Part =>
8918 Make_Function_Call (Loc,
8919 Name =>
8920 New_Occurrence_Of (Alias (Subp), Loc),
8921 Parameter_Associations => Actual_List),
8922 Null_Record_Present => True));
8924 Func_Body :=
8925 Make_Subprogram_Body (Loc,
8926 Specification => New_Copy_Tree (Func_Spec),
8927 Declarations => Empty_List,
8928 Handled_Statement_Sequence =>
8929 Make_Handled_Sequence_Of_Statements (Loc,
8930 Statements => New_List (Return_Stmt)));
8932 Set_Defining_Unit_Name
8933 (Specification (Func_Body),
8934 Make_Defining_Identifier (Loc, Chars (Subp)));
8936 Append_To (Body_List, Func_Body);
8938 -- Replace the inherited function with the wrapper function in the
8939 -- primitive operations list. We add the minimum decoration needed
8940 -- to override interface primitives.
8942 Set_Ekind (Defining_Unit_Name (Func_Spec), E_Function);
8944 Override_Dispatching_Operation
8945 (Tag_Typ, Subp, New_Op => Defining_Unit_Name (Func_Spec),
8946 Is_Wrapper => True);
8947 end if;
8949 <<Next_Prim>>
8950 Next_Elmt (Prim_Elmt);
8951 end loop;
8952 end Make_Controlling_Function_Wrappers;
8954 -------------------
8955 -- Make_Eq_Body --
8956 -------------------
8958 function Make_Eq_Body
8959 (Typ : Entity_Id;
8960 Eq_Name : Name_Id) return Node_Id
8962 Loc : constant Source_Ptr := Sloc (Parent (Typ));
8963 Decl : Node_Id;
8964 Def : constant Node_Id := Parent (Typ);
8965 Stmts : constant List_Id := New_List;
8966 Variant_Case : Boolean := Has_Discriminants (Typ);
8967 Comps : Node_Id := Empty;
8968 Typ_Def : Node_Id := Type_Definition (Def);
8970 begin
8971 Decl :=
8972 Predef_Spec_Or_Body (Loc,
8973 Tag_Typ => Typ,
8974 Name => Eq_Name,
8975 Profile => New_List (
8976 Make_Parameter_Specification (Loc,
8977 Defining_Identifier =>
8978 Make_Defining_Identifier (Loc, Name_X),
8979 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
8981 Make_Parameter_Specification (Loc,
8982 Defining_Identifier =>
8983 Make_Defining_Identifier (Loc, Name_Y),
8984 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8986 Ret_Type => Standard_Boolean,
8987 For_Body => True);
8989 if Variant_Case then
8990 if Nkind (Typ_Def) = N_Derived_Type_Definition then
8991 Typ_Def := Record_Extension_Part (Typ_Def);
8992 end if;
8994 if Present (Typ_Def) then
8995 Comps := Component_List (Typ_Def);
8996 end if;
8998 Variant_Case :=
8999 Present (Comps) and then Present (Variant_Part (Comps));
9000 end if;
9002 if Variant_Case then
9003 Append_To (Stmts,
9004 Make_Eq_If (Typ, Discriminant_Specifications (Def)));
9005 Append_List_To (Stmts, Make_Eq_Case (Typ, Comps));
9006 Append_To (Stmts,
9007 Make_Simple_Return_Statement (Loc,
9008 Expression => New_Occurrence_Of (Standard_True, Loc)));
9010 else
9011 Append_To (Stmts,
9012 Make_Simple_Return_Statement (Loc,
9013 Expression =>
9014 Expand_Record_Equality
9015 (Typ,
9016 Typ => Typ,
9017 Lhs => Make_Identifier (Loc, Name_X),
9018 Rhs => Make_Identifier (Loc, Name_Y),
9019 Bodies => Declarations (Decl))));
9020 end if;
9022 Set_Handled_Statement_Sequence
9023 (Decl, Make_Handled_Sequence_Of_Statements (Loc, Stmts));
9024 return Decl;
9025 end Make_Eq_Body;
9027 ------------------
9028 -- Make_Eq_Case --
9029 ------------------
9031 -- <Make_Eq_If shared components>
9033 -- case X.D1 is
9034 -- when V1 => <Make_Eq_Case> on subcomponents
9035 -- ...
9036 -- when Vn => <Make_Eq_Case> on subcomponents
9037 -- end case;
9039 function Make_Eq_Case
9040 (E : Entity_Id;
9041 CL : Node_Id;
9042 Discrs : Elist_Id := New_Elmt_List) return List_Id
9044 Loc : constant Source_Ptr := Sloc (E);
9045 Result : constant List_Id := New_List;
9046 Variant : Node_Id;
9047 Alt_List : List_Id;
9049 function Corresponding_Formal (C : Node_Id) return Entity_Id;
9050 -- Given the discriminant that controls a given variant of an unchecked
9051 -- union, find the formal of the equality function that carries the
9052 -- inferred value of the discriminant.
9054 function External_Name (E : Entity_Id) return Name_Id;
9055 -- The value of a given discriminant is conveyed in the corresponding
9056 -- formal parameter of the equality routine. The name of this formal
9057 -- parameter carries a one-character suffix which is removed here.
9059 --------------------------
9060 -- Corresponding_Formal --
9061 --------------------------
9063 function Corresponding_Formal (C : Node_Id) return Entity_Id is
9064 Discr : constant Entity_Id := Entity (Name (Variant_Part (C)));
9065 Elm : Elmt_Id;
9067 begin
9068 Elm := First_Elmt (Discrs);
9069 while Present (Elm) loop
9070 if Chars (Discr) = External_Name (Node (Elm)) then
9071 return Node (Elm);
9072 end if;
9074 Next_Elmt (Elm);
9075 end loop;
9077 -- A formal of the proper name must be found
9079 raise Program_Error;
9080 end Corresponding_Formal;
9082 -------------------
9083 -- External_Name --
9084 -------------------
9086 function External_Name (E : Entity_Id) return Name_Id is
9087 begin
9088 Get_Name_String (Chars (E));
9089 Name_Len := Name_Len - 1;
9090 return Name_Find;
9091 end External_Name;
9093 -- Start of processing for Make_Eq_Case
9095 begin
9096 Append_To (Result, Make_Eq_If (E, Component_Items (CL)));
9098 if No (Variant_Part (CL)) then
9099 return Result;
9100 end if;
9102 Variant := First_Non_Pragma (Variants (Variant_Part (CL)));
9104 if No (Variant) then
9105 return Result;
9106 end if;
9108 Alt_List := New_List;
9109 while Present (Variant) loop
9110 Append_To (Alt_List,
9111 Make_Case_Statement_Alternative (Loc,
9112 Discrete_Choices => New_Copy_List (Discrete_Choices (Variant)),
9113 Statements =>
9114 Make_Eq_Case (E, Component_List (Variant), Discrs)));
9115 Next_Non_Pragma (Variant);
9116 end loop;
9118 -- If we have an Unchecked_Union, use one of the parameters of the
9119 -- enclosing equality routine that captures the discriminant, to use
9120 -- as the expression in the generated case statement.
9122 if Is_Unchecked_Union (E) then
9123 Append_To (Result,
9124 Make_Case_Statement (Loc,
9125 Expression =>
9126 New_Occurrence_Of (Corresponding_Formal (CL), Loc),
9127 Alternatives => Alt_List));
9129 else
9130 Append_To (Result,
9131 Make_Case_Statement (Loc,
9132 Expression =>
9133 Make_Selected_Component (Loc,
9134 Prefix => Make_Identifier (Loc, Name_X),
9135 Selector_Name => New_Copy (Name (Variant_Part (CL)))),
9136 Alternatives => Alt_List));
9137 end if;
9139 return Result;
9140 end Make_Eq_Case;
9142 ----------------
9143 -- Make_Eq_If --
9144 ----------------
9146 -- Generates:
9148 -- if
9149 -- X.C1 /= Y.C1
9150 -- or else
9151 -- X.C2 /= Y.C2
9152 -- ...
9153 -- then
9154 -- return False;
9155 -- end if;
9157 -- or a null statement if the list L is empty
9159 function Make_Eq_If
9160 (E : Entity_Id;
9161 L : List_Id) return Node_Id
9163 Loc : constant Source_Ptr := Sloc (E);
9164 C : Node_Id;
9165 Field_Name : Name_Id;
9166 Cond : Node_Id;
9168 begin
9169 if No (L) then
9170 return Make_Null_Statement (Loc);
9172 else
9173 Cond := Empty;
9175 C := First_Non_Pragma (L);
9176 while Present (C) loop
9177 Field_Name := Chars (Defining_Identifier (C));
9179 -- The tags must not be compared: they are not part of the value.
9180 -- Ditto for parent interfaces because their equality operator is
9181 -- abstract.
9183 -- Note also that in the following, we use Make_Identifier for
9184 -- the component names. Use of New_Occurrence_Of to identify the
9185 -- components would be incorrect because the wrong entities for
9186 -- discriminants could be picked up in the private type case.
9188 if Field_Name = Name_uParent
9189 and then Is_Interface (Etype (Defining_Identifier (C)))
9190 then
9191 null;
9193 elsif Field_Name /= Name_uTag then
9194 Evolve_Or_Else (Cond,
9195 Make_Op_Ne (Loc,
9196 Left_Opnd =>
9197 Make_Selected_Component (Loc,
9198 Prefix => Make_Identifier (Loc, Name_X),
9199 Selector_Name => Make_Identifier (Loc, Field_Name)),
9201 Right_Opnd =>
9202 Make_Selected_Component (Loc,
9203 Prefix => Make_Identifier (Loc, Name_Y),
9204 Selector_Name => Make_Identifier (Loc, Field_Name))));
9205 end if;
9207 Next_Non_Pragma (C);
9208 end loop;
9210 if No (Cond) then
9211 return Make_Null_Statement (Loc);
9213 else
9214 return
9215 Make_Implicit_If_Statement (E,
9216 Condition => Cond,
9217 Then_Statements => New_List (
9218 Make_Simple_Return_Statement (Loc,
9219 Expression => New_Occurrence_Of (Standard_False, Loc))));
9220 end if;
9221 end if;
9222 end Make_Eq_If;
9224 -------------------
9225 -- Make_Neq_Body --
9226 -------------------
9228 function Make_Neq_Body (Tag_Typ : Entity_Id) return Node_Id is
9230 function Is_Predefined_Neq_Renaming (Prim : Node_Id) return Boolean;
9231 -- Returns true if Prim is a renaming of an unresolved predefined
9232 -- inequality operation.
9234 --------------------------------
9235 -- Is_Predefined_Neq_Renaming --
9236 --------------------------------
9238 function Is_Predefined_Neq_Renaming (Prim : Node_Id) return Boolean is
9239 begin
9240 return Chars (Prim) /= Name_Op_Ne
9241 and then Present (Alias (Prim))
9242 and then Comes_From_Source (Prim)
9243 and then Is_Intrinsic_Subprogram (Alias (Prim))
9244 and then Chars (Alias (Prim)) = Name_Op_Ne;
9245 end Is_Predefined_Neq_Renaming;
9247 -- Local variables
9249 Loc : constant Source_Ptr := Sloc (Parent (Tag_Typ));
9250 Stmts : constant List_Id := New_List;
9251 Decl : Node_Id;
9252 Eq_Prim : Entity_Id;
9253 Left_Op : Entity_Id;
9254 Renaming_Prim : Entity_Id;
9255 Right_Op : Entity_Id;
9256 Target : Entity_Id;
9258 -- Start of processing for Make_Neq_Body
9260 begin
9261 -- For a call on a renaming of a dispatching subprogram that is
9262 -- overridden, if the overriding occurred before the renaming, then
9263 -- the body executed is that of the overriding declaration, even if the
9264 -- overriding declaration is not visible at the place of the renaming;
9265 -- otherwise, the inherited or predefined subprogram is called, see
9266 -- (RM 8.5.4(8))
9268 -- Stage 1: Search for a renaming of the inequality primitive and also
9269 -- search for an overriding of the equality primitive located before the
9270 -- renaming declaration.
9272 declare
9273 Elmt : Elmt_Id;
9274 Prim : Node_Id;
9276 begin
9277 Eq_Prim := Empty;
9278 Renaming_Prim := Empty;
9280 Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
9281 while Present (Elmt) loop
9282 Prim := Node (Elmt);
9284 if Is_User_Defined_Equality (Prim) and then No (Alias (Prim)) then
9285 if No (Renaming_Prim) then
9286 pragma Assert (No (Eq_Prim));
9287 Eq_Prim := Prim;
9288 end if;
9290 elsif Is_Predefined_Neq_Renaming (Prim) then
9291 Renaming_Prim := Prim;
9292 end if;
9294 Next_Elmt (Elmt);
9295 end loop;
9296 end;
9298 -- No further action needed if no renaming was found
9300 if No (Renaming_Prim) then
9301 return Empty;
9302 end if;
9304 -- Stage 2: Replace the renaming declaration by a subprogram declaration
9305 -- (required to add its body)
9307 Decl := Parent (Parent (Renaming_Prim));
9308 Rewrite (Decl,
9309 Make_Subprogram_Declaration (Loc,
9310 Specification => Specification (Decl)));
9311 Set_Analyzed (Decl);
9313 -- Remove the decoration of intrinsic renaming subprogram
9315 Set_Is_Intrinsic_Subprogram (Renaming_Prim, False);
9316 Set_Convention (Renaming_Prim, Convention_Ada);
9317 Set_Alias (Renaming_Prim, Empty);
9318 Set_Has_Completion (Renaming_Prim, False);
9320 -- Stage 3: Build the corresponding body
9322 Left_Op := First_Formal (Renaming_Prim);
9323 Right_Op := Next_Formal (Left_Op);
9325 Decl :=
9326 Predef_Spec_Or_Body (Loc,
9327 Tag_Typ => Tag_Typ,
9328 Name => Chars (Renaming_Prim),
9329 Profile => New_List (
9330 Make_Parameter_Specification (Loc,
9331 Defining_Identifier =>
9332 Make_Defining_Identifier (Loc, Chars (Left_Op)),
9333 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
9335 Make_Parameter_Specification (Loc,
9336 Defining_Identifier =>
9337 Make_Defining_Identifier (Loc, Chars (Right_Op)),
9338 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
9340 Ret_Type => Standard_Boolean,
9341 For_Body => True);
9343 -- If the overriding of the equality primitive occurred before the
9344 -- renaming, then generate:
9346 -- function <Neq_Name> (X : Y : Typ) return Boolean is
9347 -- begin
9348 -- return not Oeq (X, Y);
9349 -- end;
9351 if Present (Eq_Prim) then
9352 Target := Eq_Prim;
9354 -- Otherwise build a nested subprogram which performs the predefined
9355 -- evaluation of the equality operator. That is, generate:
9357 -- function <Neq_Name> (X : Y : Typ) return Boolean is
9358 -- function Oeq (X : Y) return Boolean is
9359 -- begin
9360 -- <<body of default implementation>>
9361 -- end;
9362 -- begin
9363 -- return not Oeq (X, Y);
9364 -- end;
9366 else
9367 declare
9368 Local_Subp : Node_Id;
9369 begin
9370 Local_Subp := Make_Eq_Body (Tag_Typ, Name_Op_Eq);
9371 Set_Declarations (Decl, New_List (Local_Subp));
9372 Target := Defining_Entity (Local_Subp);
9373 end;
9374 end if;
9376 Append_To (Stmts,
9377 Make_Simple_Return_Statement (Loc,
9378 Expression =>
9379 Make_Op_Not (Loc,
9380 Make_Function_Call (Loc,
9381 Name => New_Occurrence_Of (Target, Loc),
9382 Parameter_Associations => New_List (
9383 Make_Identifier (Loc, Chars (Left_Op)),
9384 Make_Identifier (Loc, Chars (Right_Op)))))));
9386 Set_Handled_Statement_Sequence
9387 (Decl, Make_Handled_Sequence_Of_Statements (Loc, Stmts));
9388 return Decl;
9389 end Make_Neq_Body;
9391 -------------------------------
9392 -- Make_Null_Procedure_Specs --
9393 -------------------------------
9395 function Make_Null_Procedure_Specs (Tag_Typ : Entity_Id) return List_Id is
9396 Decl_List : constant List_Id := New_List;
9397 Loc : constant Source_Ptr := Sloc (Tag_Typ);
9398 Formal : Entity_Id;
9399 Formal_List : List_Id;
9400 New_Param_Spec : Node_Id;
9401 Parent_Subp : Entity_Id;
9402 Prim_Elmt : Elmt_Id;
9403 Subp : Entity_Id;
9405 begin
9406 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
9407 while Present (Prim_Elmt) loop
9408 Subp := Node (Prim_Elmt);
9410 -- If a null procedure inherited from an interface has not been
9411 -- overridden, then we build a null procedure declaration to
9412 -- override the inherited procedure.
9414 Parent_Subp := Alias (Subp);
9416 if Present (Parent_Subp)
9417 and then Is_Null_Interface_Primitive (Parent_Subp)
9418 then
9419 Formal_List := No_List;
9420 Formal := First_Formal (Subp);
9422 if Present (Formal) then
9423 Formal_List := New_List;
9425 while Present (Formal) loop
9427 -- Copy the parameter spec including default expressions
9429 New_Param_Spec :=
9430 New_Copy_Tree (Parent (Formal), New_Sloc => Loc);
9432 -- Generate a new defining identifier for the new formal.
9433 -- required because New_Copy_Tree does not duplicate
9434 -- semantic fields (except itypes).
9436 Set_Defining_Identifier (New_Param_Spec,
9437 Make_Defining_Identifier (Sloc (Formal),
9438 Chars => Chars (Formal)));
9440 -- For controlling arguments we must change their
9441 -- parameter type to reference the tagged type (instead
9442 -- of the interface type)
9444 if Is_Controlling_Formal (Formal) then
9445 if Nkind (Parameter_Type (Parent (Formal))) = N_Identifier
9446 then
9447 Set_Parameter_Type (New_Param_Spec,
9448 New_Occurrence_Of (Tag_Typ, Loc));
9450 else pragma Assert
9451 (Nkind (Parameter_Type (Parent (Formal))) =
9452 N_Access_Definition);
9453 Set_Subtype_Mark (Parameter_Type (New_Param_Spec),
9454 New_Occurrence_Of (Tag_Typ, Loc));
9455 end if;
9456 end if;
9458 Append (New_Param_Spec, Formal_List);
9460 Next_Formal (Formal);
9461 end loop;
9462 end if;
9464 Append_To (Decl_List,
9465 Make_Subprogram_Declaration (Loc,
9466 Make_Procedure_Specification (Loc,
9467 Defining_Unit_Name =>
9468 Make_Defining_Identifier (Loc, Chars (Subp)),
9469 Parameter_Specifications => Formal_List,
9470 Null_Present => True)));
9471 end if;
9473 Next_Elmt (Prim_Elmt);
9474 end loop;
9476 return Decl_List;
9477 end Make_Null_Procedure_Specs;
9479 -------------------------------------
9480 -- Make_Predefined_Primitive_Specs --
9481 -------------------------------------
9483 procedure Make_Predefined_Primitive_Specs
9484 (Tag_Typ : Entity_Id;
9485 Predef_List : out List_Id;
9486 Renamed_Eq : out Entity_Id)
9488 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean;
9489 -- Returns true if Prim is a renaming of an unresolved predefined
9490 -- equality operation.
9492 -------------------------------
9493 -- Is_Predefined_Eq_Renaming --
9494 -------------------------------
9496 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean is
9497 begin
9498 return Chars (Prim) /= Name_Op_Eq
9499 and then Present (Alias (Prim))
9500 and then Comes_From_Source (Prim)
9501 and then Is_Intrinsic_Subprogram (Alias (Prim))
9502 and then Chars (Alias (Prim)) = Name_Op_Eq;
9503 end Is_Predefined_Eq_Renaming;
9505 -- Local variables
9507 Loc : constant Source_Ptr := Sloc (Tag_Typ);
9508 Res : constant List_Id := New_List;
9509 Eq_Name : Name_Id := Name_Op_Eq;
9510 Eq_Needed : Boolean;
9511 Eq_Spec : Node_Id;
9512 Prim : Elmt_Id;
9514 Has_Predef_Eq_Renaming : Boolean := False;
9515 -- Set to True if Tag_Typ has a primitive that renames the predefined
9516 -- equality operator. Used to implement (RM 8-5-4(8)).
9518 -- Start of processing for Make_Predefined_Primitive_Specs
9520 begin
9521 Renamed_Eq := Empty;
9523 -- Spec of _Size
9525 Append_To (Res, Predef_Spec_Or_Body (Loc,
9526 Tag_Typ => Tag_Typ,
9527 Name => Name_uSize,
9528 Profile => New_List (
9529 Make_Parameter_Specification (Loc,
9530 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
9531 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
9533 Ret_Type => Standard_Long_Long_Integer));
9535 -- Specs for dispatching stream attributes
9537 declare
9538 Stream_Op_TSS_Names :
9539 constant array (Positive range <>) of TSS_Name_Type :=
9540 (TSS_Stream_Read,
9541 TSS_Stream_Write,
9542 TSS_Stream_Input,
9543 TSS_Stream_Output);
9545 begin
9546 for Op in Stream_Op_TSS_Names'Range loop
9547 if Stream_Operation_OK (Tag_Typ, Stream_Op_TSS_Names (Op)) then
9548 Append_To (Res,
9549 Predef_Stream_Attr_Spec (Loc, Tag_Typ,
9550 Stream_Op_TSS_Names (Op)));
9551 end if;
9552 end loop;
9553 end;
9555 -- Spec of "=" is expanded if the type is not limited and if a user
9556 -- defined "=" was not already declared for the non-full view of a
9557 -- private extension
9559 if not Is_Limited_Type (Tag_Typ) then
9560 Eq_Needed := True;
9561 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
9562 while Present (Prim) loop
9564 -- If a primitive is encountered that renames the predefined
9565 -- equality operator before reaching any explicit equality
9566 -- primitive, then we still need to create a predefined equality
9567 -- function, because calls to it can occur via the renaming. A
9568 -- new name is created for the equality to avoid conflicting with
9569 -- any user-defined equality. (Note that this doesn't account for
9570 -- renamings of equality nested within subpackages???)
9572 if Is_Predefined_Eq_Renaming (Node (Prim)) then
9573 Has_Predef_Eq_Renaming := True;
9574 Eq_Name := New_External_Name (Chars (Node (Prim)), 'E');
9576 -- User-defined equality
9578 elsif Is_User_Defined_Equality (Node (Prim)) then
9579 if No (Alias (Node (Prim)))
9580 or else Nkind (Unit_Declaration_Node (Node (Prim))) =
9581 N_Subprogram_Renaming_Declaration
9582 then
9583 Eq_Needed := False;
9584 exit;
9586 -- If the parent is not an interface type and has an abstract
9587 -- equality function explicitly defined in the sources, then
9588 -- the inherited equality is abstract as well, and no body can
9589 -- be created for it.
9591 elsif not Is_Interface (Etype (Tag_Typ))
9592 and then Present (Alias (Node (Prim)))
9593 and then Comes_From_Source (Alias (Node (Prim)))
9594 and then Is_Abstract_Subprogram (Alias (Node (Prim)))
9595 then
9596 Eq_Needed := False;
9597 exit;
9599 -- If the type has an equality function corresponding with
9600 -- a primitive defined in an interface type, the inherited
9601 -- equality is abstract as well, and no body can be created
9602 -- for it.
9604 elsif Present (Alias (Node (Prim)))
9605 and then Comes_From_Source (Ultimate_Alias (Node (Prim)))
9606 and then
9607 Is_Interface
9608 (Find_Dispatching_Type (Ultimate_Alias (Node (Prim))))
9609 then
9610 Eq_Needed := False;
9611 exit;
9612 end if;
9613 end if;
9615 Next_Elmt (Prim);
9616 end loop;
9618 -- If a renaming of predefined equality was found but there was no
9619 -- user-defined equality (so Eq_Needed is still true), then set the
9620 -- name back to Name_Op_Eq. But in the case where a user-defined
9621 -- equality was located after such a renaming, then the predefined
9622 -- equality function is still needed, so Eq_Needed must be set back
9623 -- to True.
9625 if Eq_Name /= Name_Op_Eq then
9626 if Eq_Needed then
9627 Eq_Name := Name_Op_Eq;
9628 else
9629 Eq_Needed := True;
9630 end if;
9631 end if;
9633 if Eq_Needed then
9634 Eq_Spec := Predef_Spec_Or_Body (Loc,
9635 Tag_Typ => Tag_Typ,
9636 Name => Eq_Name,
9637 Profile => New_List (
9638 Make_Parameter_Specification (Loc,
9639 Defining_Identifier =>
9640 Make_Defining_Identifier (Loc, Name_X),
9641 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
9643 Make_Parameter_Specification (Loc,
9644 Defining_Identifier =>
9645 Make_Defining_Identifier (Loc, Name_Y),
9646 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
9647 Ret_Type => Standard_Boolean);
9648 Append_To (Res, Eq_Spec);
9650 if Has_Predef_Eq_Renaming then
9651 Renamed_Eq := Defining_Unit_Name (Specification (Eq_Spec));
9653 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
9654 while Present (Prim) loop
9656 -- Any renamings of equality that appeared before an
9657 -- overriding equality must be updated to refer to the
9658 -- entity for the predefined equality, otherwise calls via
9659 -- the renaming would get incorrectly resolved to call the
9660 -- user-defined equality function.
9662 if Is_Predefined_Eq_Renaming (Node (Prim)) then
9663 Set_Alias (Node (Prim), Renamed_Eq);
9665 -- Exit upon encountering a user-defined equality
9667 elsif Chars (Node (Prim)) = Name_Op_Eq
9668 and then No (Alias (Node (Prim)))
9669 then
9670 exit;
9671 end if;
9673 Next_Elmt (Prim);
9674 end loop;
9675 end if;
9676 end if;
9678 -- Spec for dispatching assignment
9680 Append_To (Res, Predef_Spec_Or_Body (Loc,
9681 Tag_Typ => Tag_Typ,
9682 Name => Name_uAssign,
9683 Profile => New_List (
9684 Make_Parameter_Specification (Loc,
9685 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
9686 Out_Present => True,
9687 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
9689 Make_Parameter_Specification (Loc,
9690 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
9691 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)))));
9692 end if;
9694 -- Ada 2005: Generate declarations for the following primitive
9695 -- operations for limited interfaces and synchronized types that
9696 -- implement a limited interface.
9698 -- Disp_Asynchronous_Select
9699 -- Disp_Conditional_Select
9700 -- Disp_Get_Prim_Op_Kind
9701 -- Disp_Get_Task_Id
9702 -- Disp_Requeue
9703 -- Disp_Timed_Select
9705 -- Disable the generation of these bodies if No_Dispatching_Calls,
9706 -- Ravenscar or ZFP is active.
9708 if Ada_Version >= Ada_2005
9709 and then not Restriction_Active (No_Dispatching_Calls)
9710 and then not Restriction_Active (No_Select_Statements)
9711 and then RTE_Available (RE_Select_Specific_Data)
9712 then
9713 -- These primitives are defined abstract in interface types
9715 if Is_Interface (Tag_Typ)
9716 and then Is_Limited_Record (Tag_Typ)
9717 then
9718 Append_To (Res,
9719 Make_Abstract_Subprogram_Declaration (Loc,
9720 Specification =>
9721 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
9723 Append_To (Res,
9724 Make_Abstract_Subprogram_Declaration (Loc,
9725 Specification =>
9726 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
9728 Append_To (Res,
9729 Make_Abstract_Subprogram_Declaration (Loc,
9730 Specification =>
9731 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
9733 Append_To (Res,
9734 Make_Abstract_Subprogram_Declaration (Loc,
9735 Specification =>
9736 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
9738 Append_To (Res,
9739 Make_Abstract_Subprogram_Declaration (Loc,
9740 Specification =>
9741 Make_Disp_Requeue_Spec (Tag_Typ)));
9743 Append_To (Res,
9744 Make_Abstract_Subprogram_Declaration (Loc,
9745 Specification =>
9746 Make_Disp_Timed_Select_Spec (Tag_Typ)));
9748 -- If ancestor is an interface type, declare non-abstract primitives
9749 -- to override the abstract primitives of the interface type.
9751 -- In VM targets we define these primitives in all root tagged types
9752 -- that are not interface types. Done because in VM targets we don't
9753 -- have secondary dispatch tables and any derivation of Tag_Typ may
9754 -- cover limited interfaces (which always have these primitives since
9755 -- they may be ancestors of synchronized interface types).
9757 elsif (not Is_Interface (Tag_Typ)
9758 and then Is_Interface (Etype (Tag_Typ))
9759 and then Is_Limited_Record (Etype (Tag_Typ)))
9760 or else
9761 (Is_Concurrent_Record_Type (Tag_Typ)
9762 and then Has_Interfaces (Tag_Typ))
9763 or else
9764 (not Tagged_Type_Expansion
9765 and then not Is_Interface (Tag_Typ)
9766 and then Tag_Typ = Root_Type (Tag_Typ))
9767 then
9768 Append_To (Res,
9769 Make_Subprogram_Declaration (Loc,
9770 Specification =>
9771 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
9773 Append_To (Res,
9774 Make_Subprogram_Declaration (Loc,
9775 Specification =>
9776 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
9778 Append_To (Res,
9779 Make_Subprogram_Declaration (Loc,
9780 Specification =>
9781 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
9783 Append_To (Res,
9784 Make_Subprogram_Declaration (Loc,
9785 Specification =>
9786 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
9788 Append_To (Res,
9789 Make_Subprogram_Declaration (Loc,
9790 Specification =>
9791 Make_Disp_Requeue_Spec (Tag_Typ)));
9793 Append_To (Res,
9794 Make_Subprogram_Declaration (Loc,
9795 Specification =>
9796 Make_Disp_Timed_Select_Spec (Tag_Typ)));
9797 end if;
9798 end if;
9800 -- All tagged types receive their own Deep_Adjust and Deep_Finalize
9801 -- regardless of whether they are controlled or may contain controlled
9802 -- components.
9804 -- Do not generate the routines if finalization is disabled
9806 if Restriction_Active (No_Finalization) then
9807 null;
9809 else
9810 if not Is_Limited_Type (Tag_Typ) then
9811 Append_To (Res, Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust));
9812 end if;
9814 Append_To (Res, Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize));
9815 end if;
9817 Predef_List := Res;
9818 end Make_Predefined_Primitive_Specs;
9820 -------------------------
9821 -- Make_Tag_Assignment --
9822 -------------------------
9824 function Make_Tag_Assignment (N : Node_Id) return Node_Id is
9825 Loc : constant Source_Ptr := Sloc (N);
9826 Def_If : constant Entity_Id := Defining_Identifier (N);
9827 Expr : constant Node_Id := Expression (N);
9828 Typ : constant Entity_Id := Etype (Def_If);
9829 Full_Typ : constant Entity_Id := Underlying_Type (Typ);
9830 New_Ref : Node_Id;
9832 begin
9833 -- This expansion activity is called during analysis, but cannot
9834 -- be applied in ASIS mode when other expansion is disabled.
9836 if Is_Tagged_Type (Typ)
9837 and then not Is_Class_Wide_Type (Typ)
9838 and then not Is_CPP_Class (Typ)
9839 and then Tagged_Type_Expansion
9840 and then Nkind (Expr) /= N_Aggregate
9841 and then not ASIS_Mode
9842 and then (Nkind (Expr) /= N_Qualified_Expression
9843 or else Nkind (Expression (Expr)) /= N_Aggregate)
9844 then
9845 New_Ref :=
9846 Make_Selected_Component (Loc,
9847 Prefix => New_Occurrence_Of (Def_If, Loc),
9848 Selector_Name =>
9849 New_Occurrence_Of (First_Tag_Component (Full_Typ), Loc));
9850 Set_Assignment_OK (New_Ref);
9852 return
9853 Make_Assignment_Statement (Loc,
9854 Name => New_Ref,
9855 Expression =>
9856 Unchecked_Convert_To (RTE (RE_Tag),
9857 New_Occurrence_Of (Node
9858 (First_Elmt (Access_Disp_Table (Full_Typ))), Loc)));
9859 else
9860 return Empty;
9861 end if;
9862 end Make_Tag_Assignment;
9864 ---------------------------------
9865 -- Needs_Simple_Initialization --
9866 ---------------------------------
9868 function Needs_Simple_Initialization
9869 (T : Entity_Id;
9870 Consider_IS : Boolean := True) return Boolean
9872 Consider_IS_NS : constant Boolean :=
9873 Normalize_Scalars or (Initialize_Scalars and Consider_IS);
9875 begin
9876 -- Never need initialization if it is suppressed
9878 if Initialization_Suppressed (T) then
9879 return False;
9880 end if;
9882 -- Check for private type, in which case test applies to the underlying
9883 -- type of the private type.
9885 if Is_Private_Type (T) then
9886 declare
9887 RT : constant Entity_Id := Underlying_Type (T);
9888 begin
9889 if Present (RT) then
9890 return Needs_Simple_Initialization (RT);
9891 else
9892 return False;
9893 end if;
9894 end;
9896 -- Scalar type with Default_Value aspect requires initialization
9898 elsif Is_Scalar_Type (T) and then Has_Default_Aspect (T) then
9899 return True;
9901 -- Cases needing simple initialization are access types, and, if pragma
9902 -- Normalize_Scalars or Initialize_Scalars is in effect, then all scalar
9903 -- types.
9905 elsif Is_Access_Type (T)
9906 or else (Consider_IS_NS and then (Is_Scalar_Type (T)))
9907 then
9908 return True;
9910 -- If Initialize/Normalize_Scalars is in effect, string objects also
9911 -- need initialization, unless they are created in the course of
9912 -- expanding an aggregate (since in the latter case they will be
9913 -- filled with appropriate initializing values before they are used).
9915 elsif Consider_IS_NS
9916 and then Is_Standard_String_Type (T)
9917 and then
9918 (not Is_Itype (T)
9919 or else Nkind (Associated_Node_For_Itype (T)) /= N_Aggregate)
9920 then
9921 return True;
9923 else
9924 return False;
9925 end if;
9926 end Needs_Simple_Initialization;
9928 ----------------------
9929 -- Predef_Deep_Spec --
9930 ----------------------
9932 function Predef_Deep_Spec
9933 (Loc : Source_Ptr;
9934 Tag_Typ : Entity_Id;
9935 Name : TSS_Name_Type;
9936 For_Body : Boolean := False) return Node_Id
9938 Formals : List_Id;
9940 begin
9941 -- V : in out Tag_Typ
9943 Formals := New_List (
9944 Make_Parameter_Specification (Loc,
9945 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
9946 In_Present => True,
9947 Out_Present => True,
9948 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)));
9950 -- F : Boolean := True
9952 if Name = TSS_Deep_Adjust
9953 or else Name = TSS_Deep_Finalize
9954 then
9955 Append_To (Formals,
9956 Make_Parameter_Specification (Loc,
9957 Defining_Identifier => Make_Defining_Identifier (Loc, Name_F),
9958 Parameter_Type => New_Occurrence_Of (Standard_Boolean, Loc),
9959 Expression => New_Occurrence_Of (Standard_True, Loc)));
9960 end if;
9962 return
9963 Predef_Spec_Or_Body (Loc,
9964 Name => Make_TSS_Name (Tag_Typ, Name),
9965 Tag_Typ => Tag_Typ,
9966 Profile => Formals,
9967 For_Body => For_Body);
9969 exception
9970 when RE_Not_Available =>
9971 return Empty;
9972 end Predef_Deep_Spec;
9974 -------------------------
9975 -- Predef_Spec_Or_Body --
9976 -------------------------
9978 function Predef_Spec_Or_Body
9979 (Loc : Source_Ptr;
9980 Tag_Typ : Entity_Id;
9981 Name : Name_Id;
9982 Profile : List_Id;
9983 Ret_Type : Entity_Id := Empty;
9984 For_Body : Boolean := False) return Node_Id
9986 Id : constant Entity_Id := Make_Defining_Identifier (Loc, Name);
9987 Spec : Node_Id;
9989 begin
9990 Set_Is_Public (Id, Is_Public (Tag_Typ));
9992 -- The internal flag is set to mark these declarations because they have
9993 -- specific properties. First, they are primitives even if they are not
9994 -- defined in the type scope (the freezing point is not necessarily in
9995 -- the same scope). Second, the predefined equality can be overridden by
9996 -- a user-defined equality, no body will be generated in this case.
9998 Set_Is_Internal (Id);
10000 if not Debug_Generated_Code then
10001 Set_Debug_Info_Off (Id);
10002 end if;
10004 if No (Ret_Type) then
10005 Spec :=
10006 Make_Procedure_Specification (Loc,
10007 Defining_Unit_Name => Id,
10008 Parameter_Specifications => Profile);
10009 else
10010 Spec :=
10011 Make_Function_Specification (Loc,
10012 Defining_Unit_Name => Id,
10013 Parameter_Specifications => Profile,
10014 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
10015 end if;
10017 if Is_Interface (Tag_Typ) then
10018 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
10020 -- If body case, return empty subprogram body. Note that this is ill-
10021 -- formed, because there is not even a null statement, and certainly not
10022 -- a return in the function case. The caller is expected to do surgery
10023 -- on the body to add the appropriate stuff.
10025 elsif For_Body then
10026 return Make_Subprogram_Body (Loc, Spec, Empty_List, Empty);
10028 -- For the case of an Input attribute predefined for an abstract type,
10029 -- generate an abstract specification. This will never be called, but we
10030 -- need the slot allocated in the dispatching table so that attributes
10031 -- typ'Class'Input and typ'Class'Output will work properly.
10033 elsif Is_TSS (Name, TSS_Stream_Input)
10034 and then Is_Abstract_Type (Tag_Typ)
10035 then
10036 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
10038 -- Normal spec case, where we return a subprogram declaration
10040 else
10041 return Make_Subprogram_Declaration (Loc, Spec);
10042 end if;
10043 end Predef_Spec_Or_Body;
10045 -----------------------------
10046 -- Predef_Stream_Attr_Spec --
10047 -----------------------------
10049 function Predef_Stream_Attr_Spec
10050 (Loc : Source_Ptr;
10051 Tag_Typ : Entity_Id;
10052 Name : TSS_Name_Type;
10053 For_Body : Boolean := False) return Node_Id
10055 Ret_Type : Entity_Id;
10057 begin
10058 if Name = TSS_Stream_Input then
10059 Ret_Type := Tag_Typ;
10060 else
10061 Ret_Type := Empty;
10062 end if;
10064 return
10065 Predef_Spec_Or_Body
10066 (Loc,
10067 Name => Make_TSS_Name (Tag_Typ, Name),
10068 Tag_Typ => Tag_Typ,
10069 Profile => Build_Stream_Attr_Profile (Loc, Tag_Typ, Name),
10070 Ret_Type => Ret_Type,
10071 For_Body => For_Body);
10072 end Predef_Stream_Attr_Spec;
10074 ---------------------------------
10075 -- Predefined_Primitive_Bodies --
10076 ---------------------------------
10078 function Predefined_Primitive_Bodies
10079 (Tag_Typ : Entity_Id;
10080 Renamed_Eq : Entity_Id) return List_Id
10082 Loc : constant Source_Ptr := Sloc (Tag_Typ);
10083 Res : constant List_Id := New_List;
10084 Adj_Call : Node_Id;
10085 Decl : Node_Id;
10086 Fin_Call : Node_Id;
10087 Prim : Elmt_Id;
10088 Eq_Needed : Boolean;
10089 Eq_Name : Name_Id;
10090 Ent : Entity_Id;
10092 pragma Warnings (Off, Ent);
10094 begin
10095 pragma Assert (not Is_Interface (Tag_Typ));
10097 -- See if we have a predefined "=" operator
10099 if Present (Renamed_Eq) then
10100 Eq_Needed := True;
10101 Eq_Name := Chars (Renamed_Eq);
10103 -- If the parent is an interface type then it has defined all the
10104 -- predefined primitives abstract and we need to check if the type
10105 -- has some user defined "=" function which matches the profile of
10106 -- the Ada predefined equality operator to avoid generating it.
10108 elsif Is_Interface (Etype (Tag_Typ)) then
10109 Eq_Needed := True;
10110 Eq_Name := Name_Op_Eq;
10112 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
10113 while Present (Prim) loop
10114 if Chars (Node (Prim)) = Name_Op_Eq
10115 and then not Is_Internal (Node (Prim))
10116 and then Present (First_Entity (Node (Prim)))
10118 -- The predefined equality primitive must have exactly two
10119 -- formals whose type is this tagged type
10121 and then Present (Last_Entity (Node (Prim)))
10122 and then Next_Entity (First_Entity (Node (Prim)))
10123 = Last_Entity (Node (Prim))
10124 and then Etype (First_Entity (Node (Prim))) = Tag_Typ
10125 and then Etype (Last_Entity (Node (Prim))) = Tag_Typ
10126 then
10127 Eq_Needed := False;
10128 Eq_Name := No_Name;
10129 exit;
10130 end if;
10132 Next_Elmt (Prim);
10133 end loop;
10135 else
10136 Eq_Needed := False;
10137 Eq_Name := No_Name;
10139 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
10140 while Present (Prim) loop
10141 if Chars (Node (Prim)) = Name_Op_Eq
10142 and then Is_Internal (Node (Prim))
10143 then
10144 Eq_Needed := True;
10145 Eq_Name := Name_Op_Eq;
10146 exit;
10147 end if;
10149 Next_Elmt (Prim);
10150 end loop;
10151 end if;
10153 -- Body of _Size
10155 Decl := Predef_Spec_Or_Body (Loc,
10156 Tag_Typ => Tag_Typ,
10157 Name => Name_uSize,
10158 Profile => New_List (
10159 Make_Parameter_Specification (Loc,
10160 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
10161 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
10163 Ret_Type => Standard_Long_Long_Integer,
10164 For_Body => True);
10166 Set_Handled_Statement_Sequence (Decl,
10167 Make_Handled_Sequence_Of_Statements (Loc, New_List (
10168 Make_Simple_Return_Statement (Loc,
10169 Expression =>
10170 Make_Attribute_Reference (Loc,
10171 Prefix => Make_Identifier (Loc, Name_X),
10172 Attribute_Name => Name_Size)))));
10174 Append_To (Res, Decl);
10176 -- Bodies for Dispatching stream IO routines. We need these only for
10177 -- non-limited types (in the limited case there is no dispatching).
10178 -- We also skip them if dispatching or finalization are not available
10179 -- or if stream operations are prohibited by restriction No_Streams or
10180 -- from use of pragma/aspect No_Tagged_Streams.
10182 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Read)
10183 and then No (TSS (Tag_Typ, TSS_Stream_Read))
10184 then
10185 Build_Record_Read_Procedure (Loc, Tag_Typ, Decl, Ent);
10186 Append_To (Res, Decl);
10187 end if;
10189 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Write)
10190 and then No (TSS (Tag_Typ, TSS_Stream_Write))
10191 then
10192 Build_Record_Write_Procedure (Loc, Tag_Typ, Decl, Ent);
10193 Append_To (Res, Decl);
10194 end if;
10196 -- Skip body of _Input for the abstract case, since the corresponding
10197 -- spec is abstract (see Predef_Spec_Or_Body).
10199 if not Is_Abstract_Type (Tag_Typ)
10200 and then Stream_Operation_OK (Tag_Typ, TSS_Stream_Input)
10201 and then No (TSS (Tag_Typ, TSS_Stream_Input))
10202 then
10203 Build_Record_Or_Elementary_Input_Function
10204 (Loc, Tag_Typ, Decl, Ent);
10205 Append_To (Res, Decl);
10206 end if;
10208 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Output)
10209 and then No (TSS (Tag_Typ, TSS_Stream_Output))
10210 then
10211 Build_Record_Or_Elementary_Output_Procedure (Loc, Tag_Typ, Decl, Ent);
10212 Append_To (Res, Decl);
10213 end if;
10215 -- Ada 2005: Generate bodies for the following primitive operations for
10216 -- limited interfaces and synchronized types that implement a limited
10217 -- interface.
10219 -- disp_asynchronous_select
10220 -- disp_conditional_select
10221 -- disp_get_prim_op_kind
10222 -- disp_get_task_id
10223 -- disp_timed_select
10225 -- The interface versions will have null bodies
10227 -- Disable the generation of these bodies if No_Dispatching_Calls,
10228 -- Ravenscar or ZFP is active.
10230 -- In VM targets we define these primitives in all root tagged types
10231 -- that are not interface types. Done because in VM targets we don't
10232 -- have secondary dispatch tables and any derivation of Tag_Typ may
10233 -- cover limited interfaces (which always have these primitives since
10234 -- they may be ancestors of synchronized interface types).
10236 if Ada_Version >= Ada_2005
10237 and then not Is_Interface (Tag_Typ)
10238 and then
10239 ((Is_Interface (Etype (Tag_Typ))
10240 and then Is_Limited_Record (Etype (Tag_Typ)))
10241 or else
10242 (Is_Concurrent_Record_Type (Tag_Typ)
10243 and then Has_Interfaces (Tag_Typ))
10244 or else
10245 (not Tagged_Type_Expansion
10246 and then Tag_Typ = Root_Type (Tag_Typ)))
10247 and then not Restriction_Active (No_Dispatching_Calls)
10248 and then not Restriction_Active (No_Select_Statements)
10249 and then RTE_Available (RE_Select_Specific_Data)
10250 then
10251 Append_To (Res, Make_Disp_Asynchronous_Select_Body (Tag_Typ));
10252 Append_To (Res, Make_Disp_Conditional_Select_Body (Tag_Typ));
10253 Append_To (Res, Make_Disp_Get_Prim_Op_Kind_Body (Tag_Typ));
10254 Append_To (Res, Make_Disp_Get_Task_Id_Body (Tag_Typ));
10255 Append_To (Res, Make_Disp_Requeue_Body (Tag_Typ));
10256 Append_To (Res, Make_Disp_Timed_Select_Body (Tag_Typ));
10257 end if;
10259 if not Is_Limited_Type (Tag_Typ) and then not Is_Interface (Tag_Typ) then
10261 -- Body for equality
10263 if Eq_Needed then
10264 Decl := Make_Eq_Body (Tag_Typ, Eq_Name);
10265 Append_To (Res, Decl);
10266 end if;
10268 -- Body for inequality (if required)
10270 Decl := Make_Neq_Body (Tag_Typ);
10272 if Present (Decl) then
10273 Append_To (Res, Decl);
10274 end if;
10276 -- Body for dispatching assignment
10278 Decl :=
10279 Predef_Spec_Or_Body (Loc,
10280 Tag_Typ => Tag_Typ,
10281 Name => Name_uAssign,
10282 Profile => New_List (
10283 Make_Parameter_Specification (Loc,
10284 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
10285 Out_Present => True,
10286 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
10288 Make_Parameter_Specification (Loc,
10289 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
10290 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
10291 For_Body => True);
10293 Set_Handled_Statement_Sequence (Decl,
10294 Make_Handled_Sequence_Of_Statements (Loc, New_List (
10295 Make_Assignment_Statement (Loc,
10296 Name => Make_Identifier (Loc, Name_X),
10297 Expression => Make_Identifier (Loc, Name_Y)))));
10299 Append_To (Res, Decl);
10300 end if;
10302 -- Generate empty bodies of routines Deep_Adjust and Deep_Finalize for
10303 -- tagged types which do not contain controlled components.
10305 -- Do not generate the routines if finalization is disabled
10307 if Restriction_Active (No_Finalization) then
10308 null;
10310 elsif not Has_Controlled_Component (Tag_Typ) then
10311 if not Is_Limited_Type (Tag_Typ) then
10312 Adj_Call := Empty;
10313 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust, True);
10315 if Is_Controlled (Tag_Typ) then
10316 Adj_Call :=
10317 Make_Adjust_Call (
10318 Obj_Ref => Make_Identifier (Loc, Name_V),
10319 Typ => Tag_Typ);
10320 end if;
10322 if No (Adj_Call) then
10323 Adj_Call := Make_Null_Statement (Loc);
10324 end if;
10326 Set_Handled_Statement_Sequence (Decl,
10327 Make_Handled_Sequence_Of_Statements (Loc,
10328 Statements => New_List (Adj_Call)));
10330 Append_To (Res, Decl);
10331 end if;
10333 Fin_Call := Empty;
10334 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize, True);
10336 if Is_Controlled (Tag_Typ) then
10337 Fin_Call :=
10338 Make_Final_Call
10339 (Obj_Ref => Make_Identifier (Loc, Name_V),
10340 Typ => Tag_Typ);
10341 end if;
10343 if No (Fin_Call) then
10344 Fin_Call := Make_Null_Statement (Loc);
10345 end if;
10347 Set_Handled_Statement_Sequence (Decl,
10348 Make_Handled_Sequence_Of_Statements (Loc,
10349 Statements => New_List (Fin_Call)));
10351 Append_To (Res, Decl);
10352 end if;
10354 return Res;
10355 end Predefined_Primitive_Bodies;
10357 ---------------------------------
10358 -- Predefined_Primitive_Freeze --
10359 ---------------------------------
10361 function Predefined_Primitive_Freeze
10362 (Tag_Typ : Entity_Id) return List_Id
10364 Res : constant List_Id := New_List;
10365 Prim : Elmt_Id;
10366 Frnodes : List_Id;
10368 begin
10369 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
10370 while Present (Prim) loop
10371 if Is_Predefined_Dispatching_Operation (Node (Prim)) then
10372 Frnodes := Freeze_Entity (Node (Prim), Tag_Typ);
10374 if Present (Frnodes) then
10375 Append_List_To (Res, Frnodes);
10376 end if;
10377 end if;
10379 Next_Elmt (Prim);
10380 end loop;
10382 return Res;
10383 end Predefined_Primitive_Freeze;
10385 -------------------------
10386 -- Stream_Operation_OK --
10387 -------------------------
10389 function Stream_Operation_OK
10390 (Typ : Entity_Id;
10391 Operation : TSS_Name_Type) return Boolean
10393 Has_Predefined_Or_Specified_Stream_Attribute : Boolean := False;
10395 begin
10396 -- Special case of a limited type extension: a default implementation
10397 -- of the stream attributes Read or Write exists if that attribute
10398 -- has been specified or is available for an ancestor type; a default
10399 -- implementation of the attribute Output (resp. Input) exists if the
10400 -- attribute has been specified or Write (resp. Read) is available for
10401 -- an ancestor type. The last condition only applies under Ada 2005.
10403 if Is_Limited_Type (Typ) and then Is_Tagged_Type (Typ) then
10404 if Operation = TSS_Stream_Read then
10405 Has_Predefined_Or_Specified_Stream_Attribute :=
10406 Has_Specified_Stream_Read (Typ);
10408 elsif Operation = TSS_Stream_Write then
10409 Has_Predefined_Or_Specified_Stream_Attribute :=
10410 Has_Specified_Stream_Write (Typ);
10412 elsif Operation = TSS_Stream_Input then
10413 Has_Predefined_Or_Specified_Stream_Attribute :=
10414 Has_Specified_Stream_Input (Typ)
10415 or else
10416 (Ada_Version >= Ada_2005
10417 and then Stream_Operation_OK (Typ, TSS_Stream_Read));
10419 elsif Operation = TSS_Stream_Output then
10420 Has_Predefined_Or_Specified_Stream_Attribute :=
10421 Has_Specified_Stream_Output (Typ)
10422 or else
10423 (Ada_Version >= Ada_2005
10424 and then Stream_Operation_OK (Typ, TSS_Stream_Write));
10425 end if;
10427 -- Case of inherited TSS_Stream_Read or TSS_Stream_Write
10429 if not Has_Predefined_Or_Specified_Stream_Attribute
10430 and then Is_Derived_Type (Typ)
10431 and then (Operation = TSS_Stream_Read
10432 or else Operation = TSS_Stream_Write)
10433 then
10434 Has_Predefined_Or_Specified_Stream_Attribute :=
10435 Present
10436 (Find_Inherited_TSS (Base_Type (Etype (Typ)), Operation));
10437 end if;
10438 end if;
10440 -- If the type is not limited, or else is limited but the attribute is
10441 -- explicitly specified or is predefined for the type, then return True,
10442 -- unless other conditions prevail, such as restrictions prohibiting
10443 -- streams or dispatching operations. We also return True for limited
10444 -- interfaces, because they may be extended by nonlimited types and
10445 -- permit inheritance in this case (addresses cases where an abstract
10446 -- extension doesn't get 'Input declared, as per comments below, but
10447 -- 'Class'Input must still be allowed). Note that attempts to apply
10448 -- stream attributes to a limited interface or its class-wide type
10449 -- (or limited extensions thereof) will still get properly rejected
10450 -- by Check_Stream_Attribute.
10452 -- We exclude the Input operation from being a predefined subprogram in
10453 -- the case where the associated type is an abstract extension, because
10454 -- the attribute is not callable in that case, per 13.13.2(49/2). Also,
10455 -- we don't want an abstract version created because types derived from
10456 -- the abstract type may not even have Input available (for example if
10457 -- derived from a private view of the abstract type that doesn't have
10458 -- a visible Input).
10460 -- Do not generate stream routines for type Finalization_Master because
10461 -- a master may never appear in types and therefore cannot be read or
10462 -- written.
10464 return
10465 (not Is_Limited_Type (Typ)
10466 or else Is_Interface (Typ)
10467 or else Has_Predefined_Or_Specified_Stream_Attribute)
10468 and then
10469 (Operation /= TSS_Stream_Input
10470 or else not Is_Abstract_Type (Typ)
10471 or else not Is_Derived_Type (Typ))
10472 and then not Has_Unknown_Discriminants (Typ)
10473 and then not
10474 (Is_Interface (Typ)
10475 and then
10476 (Is_Task_Interface (Typ)
10477 or else Is_Protected_Interface (Typ)
10478 or else Is_Synchronized_Interface (Typ)))
10479 and then not Restriction_Active (No_Streams)
10480 and then not Restriction_Active (No_Dispatch)
10481 and then No (No_Tagged_Streams_Pragma (Typ))
10482 and then not No_Run_Time_Mode
10483 and then RTE_Available (RE_Tag)
10484 and then No (Type_Without_Stream_Operation (Typ))
10485 and then RTE_Available (RE_Root_Stream_Type)
10486 and then not Is_RTE (Typ, RE_Finalization_Master);
10487 end Stream_Operation_OK;
10489 end Exp_Ch3;