libgo: correct golang_org Makefile variables not used on all systems
[official-gcc.git] / gcc / ada / exp_ch3.adb
blobbae35be45fede7b772d80f0dba828356f051d19c
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ C H 3 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2016, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Errout; use Errout;
31 with Exp_Aggr; use Exp_Aggr;
32 with Exp_Atag; use Exp_Atag;
33 with Exp_Ch4; use Exp_Ch4;
34 with Exp_Ch6; use Exp_Ch6;
35 with Exp_Ch7; use Exp_Ch7;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Dbug; use Exp_Dbug;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Dist; use Exp_Dist;
40 with Exp_Smem; use Exp_Smem;
41 with Exp_Strm; use Exp_Strm;
42 with Exp_Tss; use Exp_Tss;
43 with Exp_Util; use Exp_Util;
44 with Freeze; use Freeze;
45 with Ghost; use Ghost;
46 with Namet; use Namet;
47 with Nlists; use Nlists;
48 with Nmake; use Nmake;
49 with Opt; use Opt;
50 with Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
53 with Sem; use Sem;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Attr; use Sem_Attr;
56 with Sem_Cat; use Sem_Cat;
57 with Sem_Ch3; use Sem_Ch3;
58 with Sem_Ch6; use Sem_Ch6;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Disp; use Sem_Disp;
61 with Sem_Eval; use Sem_Eval;
62 with Sem_Mech; use Sem_Mech;
63 with Sem_Res; use Sem_Res;
64 with Sem_SCIL; use Sem_SCIL;
65 with Sem_Type; use Sem_Type;
66 with Sem_Util; use Sem_Util;
67 with Sinfo; use Sinfo;
68 with Stand; use Stand;
69 with Snames; use Snames;
70 with Targparm; use Targparm;
71 with Tbuild; use Tbuild;
72 with Ttypes; use Ttypes;
73 with Validsw; use Validsw;
75 package body Exp_Ch3 is
77 -----------------------
78 -- Local Subprograms --
79 -----------------------
81 procedure Adjust_Discriminants (Rtype : Entity_Id);
82 -- This is used when freezing a record type. It attempts to construct
83 -- more restrictive subtypes for discriminants so that the max size of
84 -- the record can be calculated more accurately. See the body of this
85 -- procedure for details.
87 procedure Build_Array_Init_Proc (A_Type : Entity_Id; Nod : Node_Id);
88 -- Build initialization procedure for given array type. Nod is a node
89 -- used for attachment of any actions required in its construction.
90 -- It also supplies the source location used for the procedure.
92 function Build_Discriminant_Formals
93 (Rec_Id : Entity_Id;
94 Use_Dl : Boolean) return List_Id;
95 -- This function uses the discriminants of a type to build a list of
96 -- formal parameters, used in Build_Init_Procedure among other places.
97 -- If the flag Use_Dl is set, the list is built using the already
98 -- defined discriminals of the type, as is the case for concurrent
99 -- types with discriminants. Otherwise new identifiers are created,
100 -- with the source names of the discriminants.
102 function Build_Equivalent_Array_Aggregate (T : Entity_Id) return Node_Id;
103 -- This function builds a static aggregate that can serve as the initial
104 -- value for an array type whose bounds are static, and whose component
105 -- type is a composite type that has a static equivalent aggregate.
106 -- The equivalent array aggregate is used both for object initialization
107 -- and for component initialization, when used in the following function.
109 function Build_Equivalent_Record_Aggregate (T : Entity_Id) return Node_Id;
110 -- This function builds a static aggregate that can serve as the initial
111 -- value for a record type whose components are scalar and initialized
112 -- with compile-time values, or arrays with similar initialization or
113 -- defaults. When possible, initialization of an object of the type can
114 -- be achieved by using a copy of the aggregate as an initial value, thus
115 -- removing the implicit call that would otherwise constitute elaboration
116 -- code.
118 procedure Build_Record_Init_Proc (N : Node_Id; Rec_Ent : Entity_Id);
119 -- Build record initialization procedure. N is the type declaration
120 -- node, and Rec_Ent is the corresponding entity for the record type.
122 procedure Build_Slice_Assignment (Typ : Entity_Id);
123 -- Build assignment procedure for one-dimensional arrays of controlled
124 -- types. Other array and slice assignments are expanded in-line, but
125 -- the code expansion for controlled components (when control actions
126 -- are active) can lead to very large blocks that GCC3 handles poorly.
128 procedure Build_Untagged_Equality (Typ : Entity_Id);
129 -- AI05-0123: Equality on untagged records composes. This procedure
130 -- builds the equality routine for an untagged record that has components
131 -- of a record type that has user-defined primitive equality operations.
132 -- The resulting operation is a TSS subprogram.
134 procedure Build_Variant_Record_Equality (Typ : Entity_Id);
135 -- Create An Equality function for the untagged variant record Typ and
136 -- attach it to the TSS list
138 procedure Check_Stream_Attributes (Typ : Entity_Id);
139 -- Check that if a limited extension has a parent with user-defined stream
140 -- attributes, and does not itself have user-defined stream-attributes,
141 -- then any limited component of the extension also has the corresponding
142 -- user-defined stream attributes.
144 procedure Clean_Task_Names
145 (Typ : Entity_Id;
146 Proc_Id : Entity_Id);
147 -- If an initialization procedure includes calls to generate names
148 -- for task subcomponents, indicate that secondary stack cleanup is
149 -- needed after an initialization. Typ is the component type, and Proc_Id
150 -- the initialization procedure for the enclosing composite type.
152 procedure Expand_Freeze_Array_Type (N : Node_Id);
153 -- Freeze an array type. Deals with building the initialization procedure,
154 -- creating the packed array type for a packed array and also with the
155 -- creation of the controlling procedures for the controlled case. The
156 -- argument N is the N_Freeze_Entity node for the type.
158 procedure Expand_Freeze_Class_Wide_Type (N : Node_Id);
159 -- Freeze a class-wide type. Build routine Finalize_Address for the purpose
160 -- of finalizing controlled derivations from the class-wide's root type.
162 procedure Expand_Freeze_Enumeration_Type (N : Node_Id);
163 -- Freeze enumeration type with non-standard representation. Builds the
164 -- array and function needed to convert between enumeration pos and
165 -- enumeration representation values. N is the N_Freeze_Entity node
166 -- for the type.
168 procedure Expand_Freeze_Record_Type (N : Node_Id);
169 -- Freeze record type. Builds all necessary discriminant checking
170 -- and other ancillary functions, and builds dispatch tables where
171 -- needed. The argument N is the N_Freeze_Entity node. This processing
172 -- applies only to E_Record_Type entities, not to class wide types,
173 -- record subtypes, or private types.
175 procedure Expand_Tagged_Root (T : Entity_Id);
176 -- Add a field _Tag at the beginning of the record. This field carries
177 -- the value of the access to the Dispatch table. This procedure is only
178 -- called on root type, the _Tag field being inherited by the descendants.
180 procedure Freeze_Stream_Operations (N : Node_Id; Typ : Entity_Id);
181 -- Treat user-defined stream operations as renaming_as_body if the
182 -- subprogram they rename is not frozen when the type is frozen.
184 procedure Initialization_Warning (E : Entity_Id);
185 -- If static elaboration of the package is requested, indicate
186 -- when a type does meet the conditions for static initialization. If
187 -- E is a type, it has components that have no static initialization.
188 -- if E is an entity, its initial expression is not compile-time known.
190 function Init_Formals (Typ : Entity_Id) return List_Id;
191 -- This function builds the list of formals for an initialization routine.
192 -- The first formal is always _Init with the given type. For task value
193 -- record types and types containing tasks, three additional formals are
194 -- added:
196 -- _Master : Master_Id
197 -- _Chain : in out Activation_Chain
198 -- _Task_Name : String
200 -- The caller must append additional entries for discriminants if required.
202 function Inline_Init_Proc (Typ : Entity_Id) return Boolean;
203 -- Returns true if the initialization procedure of Typ should be inlined
205 function In_Runtime (E : Entity_Id) return Boolean;
206 -- Check if E is defined in the RTL (in a child of Ada or System). Used
207 -- to avoid to bring in the overhead of _Input, _Output for tagged types.
209 function Is_User_Defined_Equality (Prim : Node_Id) return Boolean;
210 -- Returns true if Prim is a user defined equality function
212 function Make_Eq_Body
213 (Typ : Entity_Id;
214 Eq_Name : Name_Id) return Node_Id;
215 -- Build the body of a primitive equality operation for a tagged record
216 -- type, or in Ada 2012 for any record type that has components with a
217 -- user-defined equality. Factored out of Predefined_Primitive_Bodies.
219 function Make_Eq_Case
220 (E : Entity_Id;
221 CL : Node_Id;
222 Discrs : Elist_Id := New_Elmt_List) return List_Id;
223 -- Building block for variant record equality. Defined to share the code
224 -- between the tagged and untagged case. Given a Component_List node CL,
225 -- it generates an 'if' followed by a 'case' statement that compares all
226 -- components of local temporaries named X and Y (that are declared as
227 -- formals at some upper level). E provides the Sloc to be used for the
228 -- generated code.
230 -- IF E is an unchecked_union, Discrs is the list of formals created for
231 -- the inferred discriminants of one operand. These formals are used in
232 -- the generated case statements for each variant of the unchecked union.
234 function Make_Eq_If
235 (E : Entity_Id;
236 L : List_Id) return Node_Id;
237 -- Building block for variant record equality. Defined to share the code
238 -- between the tagged and untagged case. Given the list of components
239 -- (or discriminants) L, it generates a return statement that compares all
240 -- components of local temporaries named X and Y (that are declared as
241 -- formals at some upper level). E provides the Sloc to be used for the
242 -- generated code.
244 function Make_Neq_Body (Tag_Typ : Entity_Id) return Node_Id;
245 -- Search for a renaming of the inequality dispatching primitive of
246 -- this tagged type. If found then build and return the corresponding
247 -- rename-as-body inequality subprogram; otherwise return Empty.
249 procedure Make_Predefined_Primitive_Specs
250 (Tag_Typ : Entity_Id;
251 Predef_List : out List_Id;
252 Renamed_Eq : out Entity_Id);
253 -- Create a list with the specs of the predefined primitive operations.
254 -- For tagged types that are interfaces all these primitives are defined
255 -- abstract.
257 -- The following entries are present for all tagged types, and provide
258 -- the results of the corresponding attribute applied to the object.
259 -- Dispatching is required in general, since the result of the attribute
260 -- will vary with the actual object subtype.
262 -- _size provides result of 'Size attribute
263 -- typSR provides result of 'Read attribute
264 -- typSW provides result of 'Write attribute
265 -- typSI provides result of 'Input attribute
266 -- typSO provides result of 'Output attribute
268 -- The following entries are additionally present for non-limited tagged
269 -- types, and implement additional dispatching operations for predefined
270 -- operations:
272 -- _equality implements "=" operator
273 -- _assign implements assignment operation
274 -- typDF implements deep finalization
275 -- typDA implements deep adjust
277 -- The latter two are empty procedures unless the type contains some
278 -- controlled components that require finalization actions (the deep
279 -- in the name refers to the fact that the action applies to components).
281 -- The list is returned in Predef_List. The Parameter Renamed_Eq either
282 -- returns the value Empty, or else the defining unit name for the
283 -- predefined equality function in the case where the type has a primitive
284 -- operation that is a renaming of predefined equality (but only if there
285 -- is also an overriding user-defined equality function). The returned
286 -- Renamed_Eq will be passed to the corresponding parameter of
287 -- Predefined_Primitive_Bodies.
289 function Has_New_Non_Standard_Rep (T : Entity_Id) return Boolean;
290 -- Returns True if there are representation clauses for type T that are not
291 -- inherited. If the result is false, the init_proc and the discriminant
292 -- checking functions of the parent can be reused by a derived type.
294 procedure Make_Controlling_Function_Wrappers
295 (Tag_Typ : Entity_Id;
296 Decl_List : out List_Id;
297 Body_List : out List_Id);
298 -- Ada 2005 (AI-391): Makes specs and bodies for the wrapper functions
299 -- associated with inherited functions with controlling results which
300 -- are not overridden. The body of each wrapper function consists solely
301 -- of a return statement whose expression is an extension aggregate
302 -- invoking the inherited subprogram's parent subprogram and extended
303 -- with a null association list.
305 function Make_Null_Procedure_Specs (Tag_Typ : Entity_Id) return List_Id;
306 -- Ada 2005 (AI-251): Makes specs for null procedures associated with any
307 -- null procedures inherited from an interface type that have not been
308 -- overridden. Only one null procedure will be created for a given set of
309 -- inherited null procedures with homographic profiles.
311 function Predef_Spec_Or_Body
312 (Loc : Source_Ptr;
313 Tag_Typ : Entity_Id;
314 Name : Name_Id;
315 Profile : List_Id;
316 Ret_Type : Entity_Id := Empty;
317 For_Body : Boolean := False) return Node_Id;
318 -- This function generates the appropriate expansion for a predefined
319 -- primitive operation specified by its name, parameter profile and
320 -- return type (Empty means this is a procedure). If For_Body is false,
321 -- then the returned node is a subprogram declaration. If For_Body is
322 -- true, then the returned node is a empty subprogram body containing
323 -- no declarations and no statements.
325 function Predef_Stream_Attr_Spec
326 (Loc : Source_Ptr;
327 Tag_Typ : Entity_Id;
328 Name : TSS_Name_Type;
329 For_Body : Boolean := False) return Node_Id;
330 -- Specialized version of Predef_Spec_Or_Body that apply to read, write,
331 -- input and output attribute whose specs are constructed in Exp_Strm.
333 function Predef_Deep_Spec
334 (Loc : Source_Ptr;
335 Tag_Typ : Entity_Id;
336 Name : TSS_Name_Type;
337 For_Body : Boolean := False) return Node_Id;
338 -- Specialized version of Predef_Spec_Or_Body that apply to _deep_adjust
339 -- and _deep_finalize
341 function Predefined_Primitive_Bodies
342 (Tag_Typ : Entity_Id;
343 Renamed_Eq : Entity_Id) return List_Id;
344 -- Create the bodies of the predefined primitives that are described in
345 -- Predefined_Primitive_Specs. When not empty, Renamed_Eq must denote
346 -- the defining unit name of the type's predefined equality as returned
347 -- by Make_Predefined_Primitive_Specs.
349 function Predefined_Primitive_Freeze (Tag_Typ : Entity_Id) return List_Id;
350 -- Freeze entities of all predefined primitive operations. This is needed
351 -- because the bodies of these operations do not normally do any freezing.
353 function Stream_Operation_OK
354 (Typ : Entity_Id;
355 Operation : TSS_Name_Type) return Boolean;
356 -- Check whether the named stream operation must be emitted for a given
357 -- type. The rules for inheritance of stream attributes by type extensions
358 -- are enforced by this function. Furthermore, various restrictions prevent
359 -- the generation of these operations, as a useful optimization or for
360 -- certification purposes and to save unnecessary generated code.
362 --------------------------
363 -- Adjust_Discriminants --
364 --------------------------
366 -- This procedure attempts to define subtypes for discriminants that are
367 -- more restrictive than those declared. Such a replacement is possible if
368 -- we can demonstrate that values outside the restricted range would cause
369 -- constraint errors in any case. The advantage of restricting the
370 -- discriminant types in this way is that the maximum size of the variant
371 -- record can be calculated more conservatively.
373 -- An example of a situation in which we can perform this type of
374 -- restriction is the following:
376 -- subtype B is range 1 .. 10;
377 -- type Q is array (B range <>) of Integer;
379 -- type V (N : Natural) is record
380 -- C : Q (1 .. N);
381 -- end record;
383 -- In this situation, we can restrict the upper bound of N to 10, since
384 -- any larger value would cause a constraint error in any case.
386 -- There are many situations in which such restriction is possible, but
387 -- for now, we just look for cases like the above, where the component
388 -- in question is a one dimensional array whose upper bound is one of
389 -- the record discriminants. Also the component must not be part of
390 -- any variant part, since then the component does not always exist.
392 procedure Adjust_Discriminants (Rtype : Entity_Id) is
393 Loc : constant Source_Ptr := Sloc (Rtype);
394 Comp : Entity_Id;
395 Ctyp : Entity_Id;
396 Ityp : Entity_Id;
397 Lo : Node_Id;
398 Hi : Node_Id;
399 P : Node_Id;
400 Loval : Uint;
401 Discr : Entity_Id;
402 Dtyp : Entity_Id;
403 Dhi : Node_Id;
404 Dhiv : Uint;
405 Ahi : Node_Id;
406 Ahiv : Uint;
407 Tnn : Entity_Id;
409 begin
410 Comp := First_Component (Rtype);
411 while Present (Comp) loop
413 -- If our parent is a variant, quit, we do not look at components
414 -- that are in variant parts, because they may not always exist.
416 P := Parent (Comp); -- component declaration
417 P := Parent (P); -- component list
419 exit when Nkind (Parent (P)) = N_Variant;
421 -- We are looking for a one dimensional array type
423 Ctyp := Etype (Comp);
425 if not Is_Array_Type (Ctyp) or else Number_Dimensions (Ctyp) > 1 then
426 goto Continue;
427 end if;
429 -- The lower bound must be constant, and the upper bound is a
430 -- discriminant (which is a discriminant of the current record).
432 Ityp := Etype (First_Index (Ctyp));
433 Lo := Type_Low_Bound (Ityp);
434 Hi := Type_High_Bound (Ityp);
436 if not Compile_Time_Known_Value (Lo)
437 or else Nkind (Hi) /= N_Identifier
438 or else No (Entity (Hi))
439 or else Ekind (Entity (Hi)) /= E_Discriminant
440 then
441 goto Continue;
442 end if;
444 -- We have an array with appropriate bounds
446 Loval := Expr_Value (Lo);
447 Discr := Entity (Hi);
448 Dtyp := Etype (Discr);
450 -- See if the discriminant has a known upper bound
452 Dhi := Type_High_Bound (Dtyp);
454 if not Compile_Time_Known_Value (Dhi) then
455 goto Continue;
456 end if;
458 Dhiv := Expr_Value (Dhi);
460 -- See if base type of component array has known upper bound
462 Ahi := Type_High_Bound (Etype (First_Index (Base_Type (Ctyp))));
464 if not Compile_Time_Known_Value (Ahi) then
465 goto Continue;
466 end if;
468 Ahiv := Expr_Value (Ahi);
470 -- The condition for doing the restriction is that the high bound
471 -- of the discriminant is greater than the low bound of the array,
472 -- and is also greater than the high bound of the base type index.
474 if Dhiv > Loval and then Dhiv > Ahiv then
476 -- We can reset the upper bound of the discriminant type to
477 -- whichever is larger, the low bound of the component, or
478 -- the high bound of the base type array index.
480 -- We build a subtype that is declared as
482 -- subtype Tnn is discr_type range discr_type'First .. max;
484 -- And insert this declaration into the tree. The type of the
485 -- discriminant is then reset to this more restricted subtype.
487 Tnn := Make_Temporary (Loc, 'T');
489 Insert_Action (Declaration_Node (Rtype),
490 Make_Subtype_Declaration (Loc,
491 Defining_Identifier => Tnn,
492 Subtype_Indication =>
493 Make_Subtype_Indication (Loc,
494 Subtype_Mark => New_Occurrence_Of (Dtyp, Loc),
495 Constraint =>
496 Make_Range_Constraint (Loc,
497 Range_Expression =>
498 Make_Range (Loc,
499 Low_Bound =>
500 Make_Attribute_Reference (Loc,
501 Attribute_Name => Name_First,
502 Prefix => New_Occurrence_Of (Dtyp, Loc)),
503 High_Bound =>
504 Make_Integer_Literal (Loc,
505 Intval => UI_Max (Loval, Ahiv)))))));
507 Set_Etype (Discr, Tnn);
508 end if;
510 <<Continue>>
511 Next_Component (Comp);
512 end loop;
513 end Adjust_Discriminants;
515 ---------------------------
516 -- Build_Array_Init_Proc --
517 ---------------------------
519 procedure Build_Array_Init_Proc (A_Type : Entity_Id; Nod : Node_Id) is
520 Comp_Type : constant Entity_Id := Component_Type (A_Type);
521 Body_Stmts : List_Id;
522 Has_Default_Init : Boolean;
523 Index_List : List_Id;
524 Loc : Source_Ptr;
525 Proc_Id : Entity_Id;
527 function Init_Component return List_Id;
528 -- Create one statement to initialize one array component, designated
529 -- by a full set of indexes.
531 function Init_One_Dimension (N : Int) return List_Id;
532 -- Create loop to initialize one dimension of the array. The single
533 -- statement in the loop body initializes the inner dimensions if any,
534 -- or else the single component. Note that this procedure is called
535 -- recursively, with N being the dimension to be initialized. A call
536 -- with N greater than the number of dimensions simply generates the
537 -- component initialization, terminating the recursion.
539 --------------------
540 -- Init_Component --
541 --------------------
543 function Init_Component return List_Id is
544 Comp : Node_Id;
546 begin
547 Comp :=
548 Make_Indexed_Component (Loc,
549 Prefix => Make_Identifier (Loc, Name_uInit),
550 Expressions => Index_List);
552 if Has_Default_Aspect (A_Type) then
553 Set_Assignment_OK (Comp);
554 return New_List (
555 Make_Assignment_Statement (Loc,
556 Name => Comp,
557 Expression =>
558 Convert_To (Comp_Type,
559 Default_Aspect_Component_Value (First_Subtype (A_Type)))));
561 elsif Needs_Simple_Initialization (Comp_Type) then
562 Set_Assignment_OK (Comp);
563 return New_List (
564 Make_Assignment_Statement (Loc,
565 Name => Comp,
566 Expression =>
567 Get_Simple_Init_Val
568 (Comp_Type, Nod, Component_Size (A_Type))));
570 else
571 Clean_Task_Names (Comp_Type, Proc_Id);
572 return
573 Build_Initialization_Call
574 (Loc, Comp, Comp_Type,
575 In_Init_Proc => True,
576 Enclos_Type => A_Type);
577 end if;
578 end Init_Component;
580 ------------------------
581 -- Init_One_Dimension --
582 ------------------------
584 function Init_One_Dimension (N : Int) return List_Id is
585 Index : Entity_Id;
587 begin
588 -- If the component does not need initializing, then there is nothing
589 -- to do here, so we return a null body. This occurs when generating
590 -- the dummy Init_Proc needed for Initialize_Scalars processing.
592 if not Has_Non_Null_Base_Init_Proc (Comp_Type)
593 and then not Needs_Simple_Initialization (Comp_Type)
594 and then not Has_Task (Comp_Type)
595 and then not Has_Default_Aspect (A_Type)
596 then
597 return New_List (Make_Null_Statement (Loc));
599 -- If all dimensions dealt with, we simply initialize the component
601 elsif N > Number_Dimensions (A_Type) then
602 return Init_Component;
604 -- Here we generate the required loop
606 else
607 Index :=
608 Make_Defining_Identifier (Loc, New_External_Name ('J', N));
610 Append (New_Occurrence_Of (Index, Loc), Index_List);
612 return New_List (
613 Make_Implicit_Loop_Statement (Nod,
614 Identifier => Empty,
615 Iteration_Scheme =>
616 Make_Iteration_Scheme (Loc,
617 Loop_Parameter_Specification =>
618 Make_Loop_Parameter_Specification (Loc,
619 Defining_Identifier => Index,
620 Discrete_Subtype_Definition =>
621 Make_Attribute_Reference (Loc,
622 Prefix =>
623 Make_Identifier (Loc, Name_uInit),
624 Attribute_Name => Name_Range,
625 Expressions => New_List (
626 Make_Integer_Literal (Loc, N))))),
627 Statements => Init_One_Dimension (N + 1)));
628 end if;
629 end Init_One_Dimension;
631 -- Start of processing for Build_Array_Init_Proc
633 begin
634 -- The init proc is created when analyzing the freeze node for the type,
635 -- but it properly belongs with the array type declaration. However, if
636 -- the freeze node is for a subtype of a type declared in another unit
637 -- it seems preferable to use the freeze node as the source location of
638 -- the init proc. In any case this is preferable for gcov usage, and
639 -- the Sloc is not otherwise used by the compiler.
641 if In_Open_Scopes (Scope (A_Type)) then
642 Loc := Sloc (A_Type);
643 else
644 Loc := Sloc (Nod);
645 end if;
647 -- Nothing to generate in the following cases:
649 -- 1. Initialization is suppressed for the type
650 -- 2. An initialization already exists for the base type
652 if Initialization_Suppressed (A_Type)
653 or else Present (Base_Init_Proc (A_Type))
654 then
655 return;
656 end if;
658 Index_List := New_List;
660 -- We need an initialization procedure if any of the following is true:
662 -- 1. The component type has an initialization procedure
663 -- 2. The component type needs simple initialization
664 -- 3. Tasks are present
665 -- 4. The type is marked as a public entity
666 -- 5. The array type has a Default_Component_Value aspect
668 -- The reason for the public entity test is to deal properly with the
669 -- Initialize_Scalars pragma. This pragma can be set in the client and
670 -- not in the declaring package, this means the client will make a call
671 -- to the initialization procedure (because one of conditions 1-3 must
672 -- apply in this case), and we must generate a procedure (even if it is
673 -- null) to satisfy the call in this case.
675 -- Exception: do not build an array init_proc for a type whose root
676 -- type is Standard.String or Standard.Wide_[Wide_]String, since there
677 -- is no place to put the code, and in any case we handle initialization
678 -- of such types (in the Initialize_Scalars case, that's the only time
679 -- the issue arises) in a special manner anyway which does not need an
680 -- init_proc.
682 Has_Default_Init := Has_Non_Null_Base_Init_Proc (Comp_Type)
683 or else Needs_Simple_Initialization (Comp_Type)
684 or else Has_Task (Comp_Type)
685 or else Has_Default_Aspect (A_Type);
687 if Has_Default_Init
688 or else (not Restriction_Active (No_Initialize_Scalars)
689 and then Is_Public (A_Type)
690 and then not Is_Standard_String_Type (A_Type))
691 then
692 Proc_Id :=
693 Make_Defining_Identifier (Loc,
694 Chars => Make_Init_Proc_Name (A_Type));
696 -- If No_Default_Initialization restriction is active, then we don't
697 -- want to build an init_proc, but we need to mark that an init_proc
698 -- would be needed if this restriction was not active (so that we can
699 -- detect attempts to call it), so set a dummy init_proc in place.
700 -- This is only done though when actual default initialization is
701 -- needed (and not done when only Is_Public is True), since otherwise
702 -- objects such as arrays of scalars could be wrongly flagged as
703 -- violating the restriction.
705 if Restriction_Active (No_Default_Initialization) then
706 if Has_Default_Init then
707 Set_Init_Proc (A_Type, Proc_Id);
708 end if;
710 return;
711 end if;
713 Body_Stmts := Init_One_Dimension (1);
715 Discard_Node (
716 Make_Subprogram_Body (Loc,
717 Specification =>
718 Make_Procedure_Specification (Loc,
719 Defining_Unit_Name => Proc_Id,
720 Parameter_Specifications => Init_Formals (A_Type)),
721 Declarations => New_List,
722 Handled_Statement_Sequence =>
723 Make_Handled_Sequence_Of_Statements (Loc,
724 Statements => Body_Stmts)));
726 Set_Ekind (Proc_Id, E_Procedure);
727 Set_Is_Public (Proc_Id, Is_Public (A_Type));
728 Set_Is_Internal (Proc_Id);
729 Set_Has_Completion (Proc_Id);
731 if not Debug_Generated_Code then
732 Set_Debug_Info_Off (Proc_Id);
733 end if;
735 -- Set Inlined on Init_Proc if it is set on the Init_Proc of the
736 -- component type itself (see also Build_Record_Init_Proc).
738 Set_Is_Inlined (Proc_Id, Inline_Init_Proc (Comp_Type));
740 -- Associate Init_Proc with type, and determine if the procedure
741 -- is null (happens because of the Initialize_Scalars pragma case,
742 -- where we have to generate a null procedure in case it is called
743 -- by a client with Initialize_Scalars set). Such procedures have
744 -- to be generated, but do not have to be called, so we mark them
745 -- as null to suppress the call.
747 Set_Init_Proc (A_Type, Proc_Id);
749 if List_Length (Body_Stmts) = 1
751 -- We must skip SCIL nodes because they may have been added to this
752 -- list by Insert_Actions.
754 and then Nkind (First_Non_SCIL_Node (Body_Stmts)) = N_Null_Statement
755 then
756 Set_Is_Null_Init_Proc (Proc_Id);
758 else
759 -- Try to build a static aggregate to statically initialize
760 -- objects of the type. This can only be done for constrained
761 -- one-dimensional arrays with static bounds.
763 Set_Static_Initialization
764 (Proc_Id,
765 Build_Equivalent_Array_Aggregate (First_Subtype (A_Type)));
766 end if;
767 end if;
768 end Build_Array_Init_Proc;
770 --------------------------------
771 -- Build_Discr_Checking_Funcs --
772 --------------------------------
774 procedure Build_Discr_Checking_Funcs (N : Node_Id) is
775 Rec_Id : Entity_Id;
776 Loc : Source_Ptr;
777 Enclosing_Func_Id : Entity_Id;
778 Sequence : Nat := 1;
779 Type_Def : Node_Id;
780 V : Node_Id;
782 function Build_Case_Statement
783 (Case_Id : Entity_Id;
784 Variant : Node_Id) return Node_Id;
785 -- Build a case statement containing only two alternatives. The first
786 -- alternative corresponds exactly to the discrete choices given on the
787 -- variant with contains the components that we are generating the
788 -- checks for. If the discriminant is one of these return False. The
789 -- second alternative is an OTHERS choice that will return True
790 -- indicating the discriminant did not match.
792 function Build_Dcheck_Function
793 (Case_Id : Entity_Id;
794 Variant : Node_Id) return Entity_Id;
795 -- Build the discriminant checking function for a given variant
797 procedure Build_Dcheck_Functions (Variant_Part_Node : Node_Id);
798 -- Builds the discriminant checking function for each variant of the
799 -- given variant part of the record type.
801 --------------------------
802 -- Build_Case_Statement --
803 --------------------------
805 function Build_Case_Statement
806 (Case_Id : Entity_Id;
807 Variant : Node_Id) return Node_Id
809 Alt_List : constant List_Id := New_List;
810 Actuals_List : List_Id;
811 Case_Node : Node_Id;
812 Case_Alt_Node : Node_Id;
813 Choice : Node_Id;
814 Choice_List : List_Id;
815 D : Entity_Id;
816 Return_Node : Node_Id;
818 begin
819 Case_Node := New_Node (N_Case_Statement, Loc);
821 -- Replace the discriminant which controls the variant with the name
822 -- of the formal of the checking function.
824 Set_Expression (Case_Node, Make_Identifier (Loc, Chars (Case_Id)));
826 Choice := First (Discrete_Choices (Variant));
828 if Nkind (Choice) = N_Others_Choice then
829 Choice_List := New_Copy_List (Others_Discrete_Choices (Choice));
830 else
831 Choice_List := New_Copy_List (Discrete_Choices (Variant));
832 end if;
834 if not Is_Empty_List (Choice_List) then
835 Case_Alt_Node := New_Node (N_Case_Statement_Alternative, Loc);
836 Set_Discrete_Choices (Case_Alt_Node, Choice_List);
838 -- In case this is a nested variant, we need to return the result
839 -- of the discriminant checking function for the immediately
840 -- enclosing variant.
842 if Present (Enclosing_Func_Id) then
843 Actuals_List := New_List;
845 D := First_Discriminant (Rec_Id);
846 while Present (D) loop
847 Append (Make_Identifier (Loc, Chars (D)), Actuals_List);
848 Next_Discriminant (D);
849 end loop;
851 Return_Node :=
852 Make_Simple_Return_Statement (Loc,
853 Expression =>
854 Make_Function_Call (Loc,
855 Name =>
856 New_Occurrence_Of (Enclosing_Func_Id, Loc),
857 Parameter_Associations =>
858 Actuals_List));
860 else
861 Return_Node :=
862 Make_Simple_Return_Statement (Loc,
863 Expression =>
864 New_Occurrence_Of (Standard_False, Loc));
865 end if;
867 Set_Statements (Case_Alt_Node, New_List (Return_Node));
868 Append (Case_Alt_Node, Alt_List);
869 end if;
871 Case_Alt_Node := New_Node (N_Case_Statement_Alternative, Loc);
872 Choice_List := New_List (New_Node (N_Others_Choice, Loc));
873 Set_Discrete_Choices (Case_Alt_Node, Choice_List);
875 Return_Node :=
876 Make_Simple_Return_Statement (Loc,
877 Expression =>
878 New_Occurrence_Of (Standard_True, Loc));
880 Set_Statements (Case_Alt_Node, New_List (Return_Node));
881 Append (Case_Alt_Node, Alt_List);
883 Set_Alternatives (Case_Node, Alt_List);
884 return Case_Node;
885 end Build_Case_Statement;
887 ---------------------------
888 -- Build_Dcheck_Function --
889 ---------------------------
891 function Build_Dcheck_Function
892 (Case_Id : Entity_Id;
893 Variant : Node_Id) return Entity_Id
895 Body_Node : Node_Id;
896 Func_Id : Entity_Id;
897 Parameter_List : List_Id;
898 Spec_Node : Node_Id;
900 begin
901 Body_Node := New_Node (N_Subprogram_Body, Loc);
902 Sequence := Sequence + 1;
904 Func_Id :=
905 Make_Defining_Identifier (Loc,
906 Chars => New_External_Name (Chars (Rec_Id), 'D', Sequence));
907 Set_Is_Discriminant_Check_Function (Func_Id);
909 Spec_Node := New_Node (N_Function_Specification, Loc);
910 Set_Defining_Unit_Name (Spec_Node, Func_Id);
912 Parameter_List := Build_Discriminant_Formals (Rec_Id, False);
914 Set_Parameter_Specifications (Spec_Node, Parameter_List);
915 Set_Result_Definition (Spec_Node,
916 New_Occurrence_Of (Standard_Boolean, Loc));
917 Set_Specification (Body_Node, Spec_Node);
918 Set_Declarations (Body_Node, New_List);
920 Set_Handled_Statement_Sequence (Body_Node,
921 Make_Handled_Sequence_Of_Statements (Loc,
922 Statements => New_List (
923 Build_Case_Statement (Case_Id, Variant))));
925 Set_Ekind (Func_Id, E_Function);
926 Set_Mechanism (Func_Id, Default_Mechanism);
927 Set_Is_Inlined (Func_Id, True);
928 Set_Is_Pure (Func_Id, True);
929 Set_Is_Public (Func_Id, Is_Public (Rec_Id));
930 Set_Is_Internal (Func_Id, True);
932 if not Debug_Generated_Code then
933 Set_Debug_Info_Off (Func_Id);
934 end if;
936 Analyze (Body_Node);
938 Append_Freeze_Action (Rec_Id, Body_Node);
939 Set_Dcheck_Function (Variant, Func_Id);
940 return Func_Id;
941 end Build_Dcheck_Function;
943 ----------------------------
944 -- Build_Dcheck_Functions --
945 ----------------------------
947 procedure Build_Dcheck_Functions (Variant_Part_Node : Node_Id) is
948 Component_List_Node : Node_Id;
949 Decl : Entity_Id;
950 Discr_Name : Entity_Id;
951 Func_Id : Entity_Id;
952 Variant : Node_Id;
953 Saved_Enclosing_Func_Id : Entity_Id;
955 begin
956 -- Build the discriminant-checking function for each variant, and
957 -- label all components of that variant with the function's name.
958 -- We only Generate a discriminant-checking function when the
959 -- variant is not empty, to prevent the creation of dead code.
960 -- The exception to that is when Frontend_Layout_On_Target is set,
961 -- because the variant record size function generated in package
962 -- Layout needs to generate calls to all discriminant-checking
963 -- functions, including those for empty variants.
965 Discr_Name := Entity (Name (Variant_Part_Node));
966 Variant := First_Non_Pragma (Variants (Variant_Part_Node));
968 while Present (Variant) loop
969 Component_List_Node := Component_List (Variant);
971 if not Null_Present (Component_List_Node)
972 or else Frontend_Layout_On_Target
973 then
974 Func_Id := Build_Dcheck_Function (Discr_Name, Variant);
976 Decl :=
977 First_Non_Pragma (Component_Items (Component_List_Node));
978 while Present (Decl) loop
979 Set_Discriminant_Checking_Func
980 (Defining_Identifier (Decl), Func_Id);
981 Next_Non_Pragma (Decl);
982 end loop;
984 if Present (Variant_Part (Component_List_Node)) then
985 Saved_Enclosing_Func_Id := Enclosing_Func_Id;
986 Enclosing_Func_Id := Func_Id;
987 Build_Dcheck_Functions (Variant_Part (Component_List_Node));
988 Enclosing_Func_Id := Saved_Enclosing_Func_Id;
989 end if;
990 end if;
992 Next_Non_Pragma (Variant);
993 end loop;
994 end Build_Dcheck_Functions;
996 -- Start of processing for Build_Discr_Checking_Funcs
998 begin
999 -- Only build if not done already
1001 if not Discr_Check_Funcs_Built (N) then
1002 Type_Def := Type_Definition (N);
1004 if Nkind (Type_Def) = N_Record_Definition then
1005 if No (Component_List (Type_Def)) then -- null record.
1006 return;
1007 else
1008 V := Variant_Part (Component_List (Type_Def));
1009 end if;
1011 else pragma Assert (Nkind (Type_Def) = N_Derived_Type_Definition);
1012 if No (Component_List (Record_Extension_Part (Type_Def))) then
1013 return;
1014 else
1015 V := Variant_Part
1016 (Component_List (Record_Extension_Part (Type_Def)));
1017 end if;
1018 end if;
1020 Rec_Id := Defining_Identifier (N);
1022 if Present (V) and then not Is_Unchecked_Union (Rec_Id) then
1023 Loc := Sloc (N);
1024 Enclosing_Func_Id := Empty;
1025 Build_Dcheck_Functions (V);
1026 end if;
1028 Set_Discr_Check_Funcs_Built (N);
1029 end if;
1030 end Build_Discr_Checking_Funcs;
1032 --------------------------------
1033 -- Build_Discriminant_Formals --
1034 --------------------------------
1036 function Build_Discriminant_Formals
1037 (Rec_Id : Entity_Id;
1038 Use_Dl : Boolean) return List_Id
1040 Loc : Source_Ptr := Sloc (Rec_Id);
1041 Parameter_List : constant List_Id := New_List;
1042 D : Entity_Id;
1043 Formal : Entity_Id;
1044 Formal_Type : Entity_Id;
1045 Param_Spec_Node : Node_Id;
1047 begin
1048 if Has_Discriminants (Rec_Id) then
1049 D := First_Discriminant (Rec_Id);
1050 while Present (D) loop
1051 Loc := Sloc (D);
1053 if Use_Dl then
1054 Formal := Discriminal (D);
1055 Formal_Type := Etype (Formal);
1056 else
1057 Formal := Make_Defining_Identifier (Loc, Chars (D));
1058 Formal_Type := Etype (D);
1059 end if;
1061 Param_Spec_Node :=
1062 Make_Parameter_Specification (Loc,
1063 Defining_Identifier => Formal,
1064 Parameter_Type =>
1065 New_Occurrence_Of (Formal_Type, Loc));
1066 Append (Param_Spec_Node, Parameter_List);
1067 Next_Discriminant (D);
1068 end loop;
1069 end if;
1071 return Parameter_List;
1072 end Build_Discriminant_Formals;
1074 --------------------------------------
1075 -- Build_Equivalent_Array_Aggregate --
1076 --------------------------------------
1078 function Build_Equivalent_Array_Aggregate (T : Entity_Id) return Node_Id is
1079 Loc : constant Source_Ptr := Sloc (T);
1080 Comp_Type : constant Entity_Id := Component_Type (T);
1081 Index_Type : constant Entity_Id := Etype (First_Index (T));
1082 Proc : constant Entity_Id := Base_Init_Proc (T);
1083 Lo, Hi : Node_Id;
1084 Aggr : Node_Id;
1085 Expr : Node_Id;
1087 begin
1088 if not Is_Constrained (T)
1089 or else Number_Dimensions (T) > 1
1090 or else No (Proc)
1091 then
1092 Initialization_Warning (T);
1093 return Empty;
1094 end if;
1096 Lo := Type_Low_Bound (Index_Type);
1097 Hi := Type_High_Bound (Index_Type);
1099 if not Compile_Time_Known_Value (Lo)
1100 or else not Compile_Time_Known_Value (Hi)
1101 then
1102 Initialization_Warning (T);
1103 return Empty;
1104 end if;
1106 if Is_Record_Type (Comp_Type)
1107 and then Present (Base_Init_Proc (Comp_Type))
1108 then
1109 Expr := Static_Initialization (Base_Init_Proc (Comp_Type));
1111 if No (Expr) then
1112 Initialization_Warning (T);
1113 return Empty;
1114 end if;
1116 else
1117 Initialization_Warning (T);
1118 return Empty;
1119 end if;
1121 Aggr := Make_Aggregate (Loc, No_List, New_List);
1122 Set_Etype (Aggr, T);
1123 Set_Aggregate_Bounds (Aggr,
1124 Make_Range (Loc,
1125 Low_Bound => New_Copy (Lo),
1126 High_Bound => New_Copy (Hi)));
1127 Set_Parent (Aggr, Parent (Proc));
1129 Append_To (Component_Associations (Aggr),
1130 Make_Component_Association (Loc,
1131 Choices =>
1132 New_List (
1133 Make_Range (Loc,
1134 Low_Bound => New_Copy (Lo),
1135 High_Bound => New_Copy (Hi))),
1136 Expression => Expr));
1138 if Static_Array_Aggregate (Aggr) then
1139 return Aggr;
1140 else
1141 Initialization_Warning (T);
1142 return Empty;
1143 end if;
1144 end Build_Equivalent_Array_Aggregate;
1146 ---------------------------------------
1147 -- Build_Equivalent_Record_Aggregate --
1148 ---------------------------------------
1150 function Build_Equivalent_Record_Aggregate (T : Entity_Id) return Node_Id is
1151 Agg : Node_Id;
1152 Comp : Entity_Id;
1153 Comp_Type : Entity_Id;
1155 -- Start of processing for Build_Equivalent_Record_Aggregate
1157 begin
1158 if not Is_Record_Type (T)
1159 or else Has_Discriminants (T)
1160 or else Is_Limited_Type (T)
1161 or else Has_Non_Standard_Rep (T)
1162 then
1163 Initialization_Warning (T);
1164 return Empty;
1165 end if;
1167 Comp := First_Component (T);
1169 -- A null record needs no warning
1171 if No (Comp) then
1172 return Empty;
1173 end if;
1175 while Present (Comp) loop
1177 -- Array components are acceptable if initialized by a positional
1178 -- aggregate with static components.
1180 if Is_Array_Type (Etype (Comp)) then
1181 Comp_Type := Component_Type (Etype (Comp));
1183 if Nkind (Parent (Comp)) /= N_Component_Declaration
1184 or else No (Expression (Parent (Comp)))
1185 or else Nkind (Expression (Parent (Comp))) /= N_Aggregate
1186 then
1187 Initialization_Warning (T);
1188 return Empty;
1190 elsif Is_Scalar_Type (Component_Type (Etype (Comp)))
1191 and then
1192 (not Compile_Time_Known_Value (Type_Low_Bound (Comp_Type))
1193 or else
1194 not Compile_Time_Known_Value (Type_High_Bound (Comp_Type)))
1195 then
1196 Initialization_Warning (T);
1197 return Empty;
1199 elsif
1200 not Static_Array_Aggregate (Expression (Parent (Comp)))
1201 then
1202 Initialization_Warning (T);
1203 return Empty;
1204 end if;
1206 elsif Is_Scalar_Type (Etype (Comp)) then
1207 Comp_Type := Etype (Comp);
1209 if Nkind (Parent (Comp)) /= N_Component_Declaration
1210 or else No (Expression (Parent (Comp)))
1211 or else not Compile_Time_Known_Value (Expression (Parent (Comp)))
1212 or else not Compile_Time_Known_Value (Type_Low_Bound (Comp_Type))
1213 or else not
1214 Compile_Time_Known_Value (Type_High_Bound (Comp_Type))
1215 then
1216 Initialization_Warning (T);
1217 return Empty;
1218 end if;
1220 -- For now, other types are excluded
1222 else
1223 Initialization_Warning (T);
1224 return Empty;
1225 end if;
1227 Next_Component (Comp);
1228 end loop;
1230 -- All components have static initialization. Build positional aggregate
1231 -- from the given expressions or defaults.
1233 Agg := Make_Aggregate (Sloc (T), New_List, New_List);
1234 Set_Parent (Agg, Parent (T));
1236 Comp := First_Component (T);
1237 while Present (Comp) loop
1238 Append
1239 (New_Copy_Tree (Expression (Parent (Comp))), Expressions (Agg));
1240 Next_Component (Comp);
1241 end loop;
1243 Analyze_And_Resolve (Agg, T);
1244 return Agg;
1245 end Build_Equivalent_Record_Aggregate;
1247 -------------------------------
1248 -- Build_Initialization_Call --
1249 -------------------------------
1251 -- References to a discriminant inside the record type declaration can
1252 -- appear either in the subtype_indication to constrain a record or an
1253 -- array, or as part of a larger expression given for the initial value
1254 -- of a component. In both of these cases N appears in the record
1255 -- initialization procedure and needs to be replaced by the formal
1256 -- parameter of the initialization procedure which corresponds to that
1257 -- discriminant.
1259 -- In the example below, references to discriminants D1 and D2 in proc_1
1260 -- are replaced by references to formals with the same name
1261 -- (discriminals)
1263 -- A similar replacement is done for calls to any record initialization
1264 -- procedure for any components that are themselves of a record type.
1266 -- type R (D1, D2 : Integer) is record
1267 -- X : Integer := F * D1;
1268 -- Y : Integer := F * D2;
1269 -- end record;
1271 -- procedure proc_1 (Out_2 : out R; D1 : Integer; D2 : Integer) is
1272 -- begin
1273 -- Out_2.D1 := D1;
1274 -- Out_2.D2 := D2;
1275 -- Out_2.X := F * D1;
1276 -- Out_2.Y := F * D2;
1277 -- end;
1279 function Build_Initialization_Call
1280 (Loc : Source_Ptr;
1281 Id_Ref : Node_Id;
1282 Typ : Entity_Id;
1283 In_Init_Proc : Boolean := False;
1284 Enclos_Type : Entity_Id := Empty;
1285 Discr_Map : Elist_Id := New_Elmt_List;
1286 With_Default_Init : Boolean := False;
1287 Constructor_Ref : Node_Id := Empty) return List_Id
1289 Res : constant List_Id := New_List;
1291 Full_Type : Entity_Id;
1293 procedure Check_Predicated_Discriminant
1294 (Val : Node_Id;
1295 Discr : Entity_Id);
1296 -- Discriminants whose subtypes have predicates are checked in two
1297 -- cases:
1298 -- a) When an object is default-initialized and assertions are enabled
1299 -- we check that the value of the discriminant obeys the predicate.
1301 -- b) In all cases, if the discriminant controls a variant and the
1302 -- variant has no others_choice, Constraint_Error must be raised if
1303 -- the predicate is violated, because there is no variant covered
1304 -- by the illegal discriminant value.
1306 -----------------------------------
1307 -- Check_Predicated_Discriminant --
1308 -----------------------------------
1310 procedure Check_Predicated_Discriminant
1311 (Val : Node_Id;
1312 Discr : Entity_Id)
1314 Typ : constant Entity_Id := Etype (Discr);
1316 procedure Check_Missing_Others (V : Node_Id);
1317 -- ???
1319 --------------------------
1320 -- Check_Missing_Others --
1321 --------------------------
1323 procedure Check_Missing_Others (V : Node_Id) is
1324 Alt : Node_Id;
1325 Choice : Node_Id;
1326 Last_Var : Node_Id;
1328 begin
1329 Last_Var := Last_Non_Pragma (Variants (V));
1330 Choice := First (Discrete_Choices (Last_Var));
1332 -- An others_choice is added during expansion for gcc use, but
1333 -- does not cover the illegality.
1335 if Entity (Name (V)) = Discr then
1336 if Present (Choice)
1337 and then (Nkind (Choice) /= N_Others_Choice
1338 or else not Comes_From_Source (Choice))
1339 then
1340 Check_Expression_Against_Static_Predicate (Val, Typ);
1342 if not Is_Static_Expression (Val) then
1343 Prepend_To (Res,
1344 Make_Raise_Constraint_Error (Loc,
1345 Condition =>
1346 Make_Op_Not (Loc,
1347 Right_Opnd => Make_Predicate_Call (Typ, Val)),
1348 Reason => CE_Invalid_Data));
1349 end if;
1350 end if;
1351 end if;
1353 -- Check whether some nested variant is ruled by the predicated
1354 -- discriminant.
1356 Alt := First (Variants (V));
1357 while Present (Alt) loop
1358 if Nkind (Alt) = N_Variant
1359 and then Present (Variant_Part (Component_List (Alt)))
1360 then
1361 Check_Missing_Others
1362 (Variant_Part (Component_List (Alt)));
1363 end if;
1365 Next (Alt);
1366 end loop;
1367 end Check_Missing_Others;
1369 -- Local variables
1371 Def : Node_Id;
1373 -- Start of processing for Check_Predicated_Discriminant
1375 begin
1376 if Ekind (Base_Type (Full_Type)) = E_Record_Type then
1377 Def := Type_Definition (Parent (Base_Type (Full_Type)));
1378 else
1379 return;
1380 end if;
1382 if Policy_In_Effect (Name_Assert) = Name_Check
1383 and then not Predicates_Ignored (Etype (Discr))
1384 then
1385 Prepend_To (Res, Make_Predicate_Check (Typ, Val));
1386 end if;
1388 -- If discriminant controls a variant, verify that predicate is
1389 -- obeyed or else an Others_Choice is present.
1391 if Nkind (Def) = N_Record_Definition
1392 and then Present (Variant_Part (Component_List (Def)))
1393 and then Policy_In_Effect (Name_Assert) = Name_Ignore
1394 then
1395 Check_Missing_Others (Variant_Part (Component_List (Def)));
1396 end if;
1397 end Check_Predicated_Discriminant;
1399 -- Local variables
1401 Arg : Node_Id;
1402 Args : List_Id;
1403 Decls : List_Id;
1404 Decl : Node_Id;
1405 Discr : Entity_Id;
1406 First_Arg : Node_Id;
1407 Full_Init_Type : Entity_Id;
1408 Init_Call : Node_Id;
1409 Init_Type : Entity_Id;
1410 Proc : Entity_Id;
1412 -- Start of processing for Build_Initialization_Call
1414 begin
1415 pragma Assert (Constructor_Ref = Empty
1416 or else Is_CPP_Constructor_Call (Constructor_Ref));
1418 if No (Constructor_Ref) then
1419 Proc := Base_Init_Proc (Typ);
1420 else
1421 Proc := Base_Init_Proc (Typ, Entity (Name (Constructor_Ref)));
1422 end if;
1424 pragma Assert (Present (Proc));
1425 Init_Type := Etype (First_Formal (Proc));
1426 Full_Init_Type := Underlying_Type (Init_Type);
1428 -- Nothing to do if the Init_Proc is null, unless Initialize_Scalars
1429 -- is active (in which case we make the call anyway, since in the
1430 -- actual compiled client it may be non null).
1432 if Is_Null_Init_Proc (Proc) and then not Init_Or_Norm_Scalars then
1433 return Empty_List;
1434 end if;
1436 -- Use the [underlying] full view when dealing with a private type. This
1437 -- may require several steps depending on derivations.
1439 Full_Type := Typ;
1440 loop
1441 if Is_Private_Type (Full_Type) then
1442 if Present (Full_View (Full_Type)) then
1443 Full_Type := Full_View (Full_Type);
1445 elsif Present (Underlying_Full_View (Full_Type)) then
1446 Full_Type := Underlying_Full_View (Full_Type);
1448 -- When a private type acts as a generic actual and lacks a full
1449 -- view, use the base type.
1451 elsif Is_Generic_Actual_Type (Full_Type) then
1452 Full_Type := Base_Type (Full_Type);
1454 -- The loop has recovered the [underlying] full view, stop the
1455 -- traversal.
1457 else
1458 exit;
1459 end if;
1461 -- The type is not private, nothing to do
1463 else
1464 exit;
1465 end if;
1466 end loop;
1468 -- If Typ is derived, the procedure is the initialization procedure for
1469 -- the root type. Wrap the argument in an conversion to make it type
1470 -- honest. Actually it isn't quite type honest, because there can be
1471 -- conflicts of views in the private type case. That is why we set
1472 -- Conversion_OK in the conversion node.
1474 if (Is_Record_Type (Typ)
1475 or else Is_Array_Type (Typ)
1476 or else Is_Private_Type (Typ))
1477 and then Init_Type /= Base_Type (Typ)
1478 then
1479 First_Arg := OK_Convert_To (Etype (Init_Type), Id_Ref);
1480 Set_Etype (First_Arg, Init_Type);
1482 else
1483 First_Arg := Id_Ref;
1484 end if;
1486 Args := New_List (Convert_Concurrent (First_Arg, Typ));
1488 -- In the tasks case, add _Master as the value of the _Master parameter
1489 -- and _Chain as the value of the _Chain parameter. At the outer level,
1490 -- these will be variables holding the corresponding values obtained
1491 -- from GNARL. At inner levels, they will be the parameters passed down
1492 -- through the outer routines.
1494 if Has_Task (Full_Type) then
1495 if Restriction_Active (No_Task_Hierarchy) then
1496 Append_To (Args,
1497 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
1498 else
1499 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
1500 end if;
1502 -- Add _Chain (not done for sequential elaboration policy, see
1503 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
1505 if Partition_Elaboration_Policy /= 'S' then
1506 Append_To (Args, Make_Identifier (Loc, Name_uChain));
1507 end if;
1509 -- Ada 2005 (AI-287): In case of default initialized components
1510 -- with tasks, we generate a null string actual parameter.
1511 -- This is just a workaround that must be improved later???
1513 if With_Default_Init then
1514 Append_To (Args,
1515 Make_String_Literal (Loc,
1516 Strval => ""));
1518 else
1519 Decls :=
1520 Build_Task_Image_Decls (Loc, Id_Ref, Enclos_Type, In_Init_Proc);
1521 Decl := Last (Decls);
1523 Append_To (Args,
1524 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
1525 Append_List (Decls, Res);
1526 end if;
1528 else
1529 Decls := No_List;
1530 Decl := Empty;
1531 end if;
1533 -- Add discriminant values if discriminants are present
1535 if Has_Discriminants (Full_Init_Type) then
1536 Discr := First_Discriminant (Full_Init_Type);
1537 while Present (Discr) loop
1539 -- If this is a discriminated concurrent type, the init_proc
1540 -- for the corresponding record is being called. Use that type
1541 -- directly to find the discriminant value, to handle properly
1542 -- intervening renamed discriminants.
1544 declare
1545 T : Entity_Id := Full_Type;
1547 begin
1548 if Is_Protected_Type (T) then
1549 T := Corresponding_Record_Type (T);
1550 end if;
1552 Arg :=
1553 Get_Discriminant_Value (
1554 Discr,
1556 Discriminant_Constraint (Full_Type));
1557 end;
1559 -- If the target has access discriminants, and is constrained by
1560 -- an access to the enclosing construct, i.e. a current instance,
1561 -- replace the reference to the type by a reference to the object.
1563 if Nkind (Arg) = N_Attribute_Reference
1564 and then Is_Access_Type (Etype (Arg))
1565 and then Is_Entity_Name (Prefix (Arg))
1566 and then Is_Type (Entity (Prefix (Arg)))
1567 then
1568 Arg :=
1569 Make_Attribute_Reference (Loc,
1570 Prefix => New_Copy (Prefix (Id_Ref)),
1571 Attribute_Name => Name_Unrestricted_Access);
1573 elsif In_Init_Proc then
1575 -- Replace any possible references to the discriminant in the
1576 -- call to the record initialization procedure with references
1577 -- to the appropriate formal parameter.
1579 if Nkind (Arg) = N_Identifier
1580 and then Ekind (Entity (Arg)) = E_Discriminant
1581 then
1582 Arg := New_Occurrence_Of (Discriminal (Entity (Arg)), Loc);
1584 -- Otherwise make a copy of the default expression. Note that
1585 -- we use the current Sloc for this, because we do not want the
1586 -- call to appear to be at the declaration point. Within the
1587 -- expression, replace discriminants with their discriminals.
1589 else
1590 Arg :=
1591 New_Copy_Tree (Arg, Map => Discr_Map, New_Sloc => Loc);
1592 end if;
1594 else
1595 if Is_Constrained (Full_Type) then
1596 Arg := Duplicate_Subexpr_No_Checks (Arg);
1597 else
1598 -- The constraints come from the discriminant default exps,
1599 -- they must be reevaluated, so we use New_Copy_Tree but we
1600 -- ensure the proper Sloc (for any embedded calls).
1601 -- In addition, if a predicate check is needed on the value
1602 -- of the discriminant, insert it ahead of the call.
1604 Arg := New_Copy_Tree (Arg, New_Sloc => Loc);
1605 end if;
1607 if Has_Predicates (Etype (Discr)) then
1608 Check_Predicated_Discriminant (Arg, Discr);
1609 end if;
1610 end if;
1612 -- Ada 2005 (AI-287): In case of default initialized components,
1613 -- if the component is constrained with a discriminant of the
1614 -- enclosing type, we need to generate the corresponding selected
1615 -- component node to access the discriminant value. In other cases
1616 -- this is not required, either because we are inside the init
1617 -- proc and we use the corresponding formal, or else because the
1618 -- component is constrained by an expression.
1620 if With_Default_Init
1621 and then Nkind (Id_Ref) = N_Selected_Component
1622 and then Nkind (Arg) = N_Identifier
1623 and then Ekind (Entity (Arg)) = E_Discriminant
1624 then
1625 Append_To (Args,
1626 Make_Selected_Component (Loc,
1627 Prefix => New_Copy_Tree (Prefix (Id_Ref)),
1628 Selector_Name => Arg));
1629 else
1630 Append_To (Args, Arg);
1631 end if;
1633 Next_Discriminant (Discr);
1634 end loop;
1635 end if;
1637 -- If this is a call to initialize the parent component of a derived
1638 -- tagged type, indicate that the tag should not be set in the parent.
1640 if Is_Tagged_Type (Full_Init_Type)
1641 and then not Is_CPP_Class (Full_Init_Type)
1642 and then Nkind (Id_Ref) = N_Selected_Component
1643 and then Chars (Selector_Name (Id_Ref)) = Name_uParent
1644 then
1645 Append_To (Args, New_Occurrence_Of (Standard_False, Loc));
1647 elsif Present (Constructor_Ref) then
1648 Append_List_To (Args,
1649 New_Copy_List (Parameter_Associations (Constructor_Ref)));
1650 end if;
1652 Append_To (Res,
1653 Make_Procedure_Call_Statement (Loc,
1654 Name => New_Occurrence_Of (Proc, Loc),
1655 Parameter_Associations => Args));
1657 if Needs_Finalization (Typ)
1658 and then Nkind (Id_Ref) = N_Selected_Component
1659 then
1660 if Chars (Selector_Name (Id_Ref)) /= Name_uParent then
1661 Init_Call :=
1662 Make_Init_Call
1663 (Obj_Ref => New_Copy_Tree (First_Arg),
1664 Typ => Typ);
1666 -- Guard against a missing [Deep_]Initialize when the type was not
1667 -- properly frozen.
1669 if Present (Init_Call) then
1670 Append_To (Res, Init_Call);
1671 end if;
1672 end if;
1673 end if;
1675 return Res;
1677 exception
1678 when RE_Not_Available =>
1679 return Empty_List;
1680 end Build_Initialization_Call;
1682 ----------------------------
1683 -- Build_Record_Init_Proc --
1684 ----------------------------
1686 procedure Build_Record_Init_Proc (N : Node_Id; Rec_Ent : Entity_Id) is
1687 Decls : constant List_Id := New_List;
1688 Discr_Map : constant Elist_Id := New_Elmt_List;
1689 Loc : constant Source_Ptr := Sloc (Rec_Ent);
1690 Counter : Nat := 0;
1691 Proc_Id : Entity_Id;
1692 Rec_Type : Entity_Id;
1693 Set_Tag : Entity_Id := Empty;
1695 function Build_Assignment (Id : Entity_Id; N : Node_Id) return List_Id;
1696 -- Build an assignment statement which assigns the default expression
1697 -- to its corresponding record component if defined. The left hand side
1698 -- of the assignment is marked Assignment_OK so that initialization of
1699 -- limited private records works correctly. This routine may also build
1700 -- an adjustment call if the component is controlled.
1702 procedure Build_Discriminant_Assignments (Statement_List : List_Id);
1703 -- If the record has discriminants, add assignment statements to
1704 -- Statement_List to initialize the discriminant values from the
1705 -- arguments of the initialization procedure.
1707 function Build_Init_Statements (Comp_List : Node_Id) return List_Id;
1708 -- Build a list representing a sequence of statements which initialize
1709 -- components of the given component list. This may involve building
1710 -- case statements for the variant parts. Append any locally declared
1711 -- objects on list Decls.
1713 function Build_Init_Call_Thru (Parameters : List_Id) return List_Id;
1714 -- Given an untagged type-derivation that declares discriminants, e.g.
1716 -- type R (R1, R2 : Integer) is record ... end record;
1717 -- type D (D1 : Integer) is new R (1, D1);
1719 -- we make the _init_proc of D be
1721 -- procedure _init_proc (X : D; D1 : Integer) is
1722 -- begin
1723 -- _init_proc (R (X), 1, D1);
1724 -- end _init_proc;
1726 -- This function builds the call statement in this _init_proc.
1728 procedure Build_CPP_Init_Procedure;
1729 -- Build the tree corresponding to the procedure specification and body
1730 -- of the IC procedure that initializes the C++ part of the dispatch
1731 -- table of an Ada tagged type that is a derivation of a CPP type.
1732 -- Install it as the CPP_Init TSS.
1734 procedure Build_Init_Procedure;
1735 -- Build the tree corresponding to the procedure specification and body
1736 -- of the initialization procedure and install it as the _init TSS.
1738 procedure Build_Offset_To_Top_Functions;
1739 -- Ada 2005 (AI-251): Build the tree corresponding to the procedure spec
1740 -- and body of Offset_To_Top, a function used in conjuction with types
1741 -- having secondary dispatch tables.
1743 procedure Build_Record_Checks (S : Node_Id; Check_List : List_Id);
1744 -- Add range checks to components of discriminated records. S is a
1745 -- subtype indication of a record component. Check_List is a list
1746 -- to which the check actions are appended.
1748 function Component_Needs_Simple_Initialization
1749 (T : Entity_Id) return Boolean;
1750 -- Determine if a component needs simple initialization, given its type
1751 -- T. This routine is the same as Needs_Simple_Initialization except for
1752 -- components of type Tag and Interface_Tag. These two access types do
1753 -- not require initialization since they are explicitly initialized by
1754 -- other means.
1756 function Parent_Subtype_Renaming_Discrims return Boolean;
1757 -- Returns True for base types N that rename discriminants, else False
1759 function Requires_Init_Proc (Rec_Id : Entity_Id) return Boolean;
1760 -- Determine whether a record initialization procedure needs to be
1761 -- generated for the given record type.
1763 ----------------------
1764 -- Build_Assignment --
1765 ----------------------
1767 function Build_Assignment (Id : Entity_Id; N : Node_Id) return List_Id is
1768 N_Loc : constant Source_Ptr := Sloc (N);
1769 Typ : constant Entity_Id := Underlying_Type (Etype (Id));
1771 Adj_Call : Node_Id;
1772 Exp : Node_Id := N;
1773 Kind : Node_Kind := Nkind (N);
1774 Lhs : Node_Id;
1775 Res : List_Id;
1777 begin
1778 Lhs :=
1779 Make_Selected_Component (N_Loc,
1780 Prefix => Make_Identifier (Loc, Name_uInit),
1781 Selector_Name => New_Occurrence_Of (Id, N_Loc));
1782 Set_Assignment_OK (Lhs);
1784 -- Case of an access attribute applied to the current instance.
1785 -- Replace the reference to the type by a reference to the actual
1786 -- object. (Note that this handles the case of the top level of
1787 -- the expression being given by such an attribute, but does not
1788 -- cover uses nested within an initial value expression. Nested
1789 -- uses are unlikely to occur in practice, but are theoretically
1790 -- possible.) It is not clear how to handle them without fully
1791 -- traversing the expression. ???
1793 if Kind = N_Attribute_Reference
1794 and then Nam_In (Attribute_Name (N), Name_Unchecked_Access,
1795 Name_Unrestricted_Access)
1796 and then Is_Entity_Name (Prefix (N))
1797 and then Is_Type (Entity (Prefix (N)))
1798 and then Entity (Prefix (N)) = Rec_Type
1799 then
1800 Exp :=
1801 Make_Attribute_Reference (N_Loc,
1802 Prefix =>
1803 Make_Identifier (N_Loc, Name_uInit),
1804 Attribute_Name => Name_Unrestricted_Access);
1805 end if;
1807 -- Take a copy of Exp to ensure that later copies of this component
1808 -- declaration in derived types see the original tree, not a node
1809 -- rewritten during expansion of the init_proc. If the copy contains
1810 -- itypes, the scope of the new itypes is the init_proc being built.
1812 Exp := New_Copy_Tree (Exp, New_Scope => Proc_Id);
1814 Res := New_List (
1815 Make_Assignment_Statement (Loc,
1816 Name => Lhs,
1817 Expression => Exp));
1819 Set_No_Ctrl_Actions (First (Res));
1821 -- Adjust the tag if tagged (because of possible view conversions).
1822 -- Suppress the tag adjustment when not Tagged_Type_Expansion because
1823 -- tags are represented implicitly in objects.
1825 if Is_Tagged_Type (Typ) and then Tagged_Type_Expansion then
1826 Append_To (Res,
1827 Make_Assignment_Statement (N_Loc,
1828 Name =>
1829 Make_Selected_Component (N_Loc,
1830 Prefix =>
1831 New_Copy_Tree (Lhs, New_Scope => Proc_Id),
1832 Selector_Name =>
1833 New_Occurrence_Of (First_Tag_Component (Typ), N_Loc)),
1835 Expression =>
1836 Unchecked_Convert_To (RTE (RE_Tag),
1837 New_Occurrence_Of
1838 (Node
1839 (First_Elmt
1840 (Access_Disp_Table (Underlying_Type (Typ)))),
1841 N_Loc))));
1842 end if;
1844 -- Adjust the component if controlled except if it is an aggregate
1845 -- that will be expanded inline.
1847 if Kind = N_Qualified_Expression then
1848 Kind := Nkind (Expression (N));
1849 end if;
1851 if Needs_Finalization (Typ)
1852 and then not (Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate))
1853 and then not Is_Limited_View (Typ)
1854 then
1855 Adj_Call :=
1856 Make_Adjust_Call
1857 (Obj_Ref => New_Copy_Tree (Lhs),
1858 Typ => Etype (Id));
1860 -- Guard against a missing [Deep_]Adjust when the component type
1861 -- was not properly frozen.
1863 if Present (Adj_Call) then
1864 Append_To (Res, Adj_Call);
1865 end if;
1866 end if;
1868 -- If a component type has a predicate, add check to the component
1869 -- assignment. Discriminants are handled at the point of the call,
1870 -- which provides for a better error message.
1872 if Comes_From_Source (Exp)
1873 and then Has_Predicates (Typ)
1874 and then not Predicate_Checks_Suppressed (Empty)
1875 and then not Predicates_Ignored (Typ)
1876 then
1877 Append (Make_Predicate_Check (Typ, Exp), Res);
1878 end if;
1880 return Res;
1882 exception
1883 when RE_Not_Available =>
1884 return Empty_List;
1885 end Build_Assignment;
1887 ------------------------------------
1888 -- Build_Discriminant_Assignments --
1889 ------------------------------------
1891 procedure Build_Discriminant_Assignments (Statement_List : List_Id) is
1892 Is_Tagged : constant Boolean := Is_Tagged_Type (Rec_Type);
1893 D : Entity_Id;
1894 D_Loc : Source_Ptr;
1896 begin
1897 if Has_Discriminants (Rec_Type)
1898 and then not Is_Unchecked_Union (Rec_Type)
1899 then
1900 D := First_Discriminant (Rec_Type);
1901 while Present (D) loop
1903 -- Don't generate the assignment for discriminants in derived
1904 -- tagged types if the discriminant is a renaming of some
1905 -- ancestor discriminant. This initialization will be done
1906 -- when initializing the _parent field of the derived record.
1908 if Is_Tagged
1909 and then Present (Corresponding_Discriminant (D))
1910 then
1911 null;
1913 else
1914 D_Loc := Sloc (D);
1915 Append_List_To (Statement_List,
1916 Build_Assignment (D,
1917 New_Occurrence_Of (Discriminal (D), D_Loc)));
1918 end if;
1920 Next_Discriminant (D);
1921 end loop;
1922 end if;
1923 end Build_Discriminant_Assignments;
1925 --------------------------
1926 -- Build_Init_Call_Thru --
1927 --------------------------
1929 function Build_Init_Call_Thru (Parameters : List_Id) return List_Id is
1930 Parent_Proc : constant Entity_Id :=
1931 Base_Init_Proc (Etype (Rec_Type));
1933 Parent_Type : constant Entity_Id :=
1934 Etype (First_Formal (Parent_Proc));
1936 Uparent_Type : constant Entity_Id :=
1937 Underlying_Type (Parent_Type);
1939 First_Discr_Param : Node_Id;
1941 Arg : Node_Id;
1942 Args : List_Id;
1943 First_Arg : Node_Id;
1944 Parent_Discr : Entity_Id;
1945 Res : List_Id;
1947 begin
1948 -- First argument (_Init) is the object to be initialized.
1949 -- ??? not sure where to get a reasonable Loc for First_Arg
1951 First_Arg :=
1952 OK_Convert_To (Parent_Type,
1953 New_Occurrence_Of
1954 (Defining_Identifier (First (Parameters)), Loc));
1956 Set_Etype (First_Arg, Parent_Type);
1958 Args := New_List (Convert_Concurrent (First_Arg, Rec_Type));
1960 -- In the tasks case,
1961 -- add _Master as the value of the _Master parameter
1962 -- add _Chain as the value of the _Chain parameter.
1963 -- add _Task_Name as the value of the _Task_Name parameter.
1964 -- At the outer level, these will be variables holding the
1965 -- corresponding values obtained from GNARL or the expander.
1967 -- At inner levels, they will be the parameters passed down through
1968 -- the outer routines.
1970 First_Discr_Param := Next (First (Parameters));
1972 if Has_Task (Rec_Type) then
1973 if Restriction_Active (No_Task_Hierarchy) then
1974 Append_To (Args,
1975 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
1976 else
1977 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
1978 end if;
1980 -- Add _Chain (not done for sequential elaboration policy, see
1981 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
1983 if Partition_Elaboration_Policy /= 'S' then
1984 Append_To (Args, Make_Identifier (Loc, Name_uChain));
1985 end if;
1987 Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
1988 First_Discr_Param := Next (Next (Next (First_Discr_Param)));
1989 end if;
1991 -- Append discriminant values
1993 if Has_Discriminants (Uparent_Type) then
1994 pragma Assert (not Is_Tagged_Type (Uparent_Type));
1996 Parent_Discr := First_Discriminant (Uparent_Type);
1997 while Present (Parent_Discr) loop
1999 -- Get the initial value for this discriminant
2000 -- ??? needs to be cleaned up to use parent_Discr_Constr
2001 -- directly.
2003 declare
2004 Discr : Entity_Id :=
2005 First_Stored_Discriminant (Uparent_Type);
2007 Discr_Value : Elmt_Id :=
2008 First_Elmt (Stored_Constraint (Rec_Type));
2010 begin
2011 while Original_Record_Component (Parent_Discr) /= Discr loop
2012 Next_Stored_Discriminant (Discr);
2013 Next_Elmt (Discr_Value);
2014 end loop;
2016 Arg := Node (Discr_Value);
2017 end;
2019 -- Append it to the list
2021 if Nkind (Arg) = N_Identifier
2022 and then Ekind (Entity (Arg)) = E_Discriminant
2023 then
2024 Append_To (Args,
2025 New_Occurrence_Of (Discriminal (Entity (Arg)), Loc));
2027 -- Case of access discriminants. We replace the reference
2028 -- to the type by a reference to the actual object.
2030 -- Is above comment right??? Use of New_Copy below seems mighty
2031 -- suspicious ???
2033 else
2034 Append_To (Args, New_Copy (Arg));
2035 end if;
2037 Next_Discriminant (Parent_Discr);
2038 end loop;
2039 end if;
2041 Res :=
2042 New_List (
2043 Make_Procedure_Call_Statement (Loc,
2044 Name =>
2045 New_Occurrence_Of (Parent_Proc, Loc),
2046 Parameter_Associations => Args));
2048 return Res;
2049 end Build_Init_Call_Thru;
2051 -----------------------------------
2052 -- Build_Offset_To_Top_Functions --
2053 -----------------------------------
2055 procedure Build_Offset_To_Top_Functions is
2057 procedure Build_Offset_To_Top_Function (Iface_Comp : Entity_Id);
2058 -- Generate:
2059 -- function Fxx (O : Address) return Storage_Offset is
2060 -- type Acc is access all <Typ>;
2061 -- begin
2062 -- return Acc!(O).Iface_Comp'Position;
2063 -- end Fxx;
2065 ----------------------------------
2066 -- Build_Offset_To_Top_Function --
2067 ----------------------------------
2069 procedure Build_Offset_To_Top_Function (Iface_Comp : Entity_Id) is
2070 Body_Node : Node_Id;
2071 Func_Id : Entity_Id;
2072 Spec_Node : Node_Id;
2073 Acc_Type : Entity_Id;
2075 begin
2076 Func_Id := Make_Temporary (Loc, 'F');
2077 Set_DT_Offset_To_Top_Func (Iface_Comp, Func_Id);
2079 -- Generate
2080 -- function Fxx (O : in Rec_Typ) return Storage_Offset;
2082 Spec_Node := New_Node (N_Function_Specification, Loc);
2083 Set_Defining_Unit_Name (Spec_Node, Func_Id);
2084 Set_Parameter_Specifications (Spec_Node, New_List (
2085 Make_Parameter_Specification (Loc,
2086 Defining_Identifier =>
2087 Make_Defining_Identifier (Loc, Name_uO),
2088 In_Present => True,
2089 Parameter_Type =>
2090 New_Occurrence_Of (RTE (RE_Address), Loc))));
2091 Set_Result_Definition (Spec_Node,
2092 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
2094 -- Generate
2095 -- function Fxx (O : in Rec_Typ) return Storage_Offset is
2096 -- begin
2097 -- return O.Iface_Comp'Position;
2098 -- end Fxx;
2100 Body_Node := New_Node (N_Subprogram_Body, Loc);
2101 Set_Specification (Body_Node, Spec_Node);
2103 Acc_Type := Make_Temporary (Loc, 'T');
2104 Set_Declarations (Body_Node, New_List (
2105 Make_Full_Type_Declaration (Loc,
2106 Defining_Identifier => Acc_Type,
2107 Type_Definition =>
2108 Make_Access_To_Object_Definition (Loc,
2109 All_Present => True,
2110 Null_Exclusion_Present => False,
2111 Constant_Present => False,
2112 Subtype_Indication =>
2113 New_Occurrence_Of (Rec_Type, Loc)))));
2115 Set_Handled_Statement_Sequence (Body_Node,
2116 Make_Handled_Sequence_Of_Statements (Loc,
2117 Statements => New_List (
2118 Make_Simple_Return_Statement (Loc,
2119 Expression =>
2120 Make_Attribute_Reference (Loc,
2121 Prefix =>
2122 Make_Selected_Component (Loc,
2123 Prefix =>
2124 Unchecked_Convert_To (Acc_Type,
2125 Make_Identifier (Loc, Name_uO)),
2126 Selector_Name =>
2127 New_Occurrence_Of (Iface_Comp, Loc)),
2128 Attribute_Name => Name_Position)))));
2130 Set_Ekind (Func_Id, E_Function);
2131 Set_Mechanism (Func_Id, Default_Mechanism);
2132 Set_Is_Internal (Func_Id, True);
2134 if not Debug_Generated_Code then
2135 Set_Debug_Info_Off (Func_Id);
2136 end if;
2138 Analyze (Body_Node);
2140 Append_Freeze_Action (Rec_Type, Body_Node);
2141 end Build_Offset_To_Top_Function;
2143 -- Local variables
2145 Iface_Comp : Node_Id;
2146 Iface_Comp_Elmt : Elmt_Id;
2147 Ifaces_Comp_List : Elist_Id;
2149 -- Start of processing for Build_Offset_To_Top_Functions
2151 begin
2152 -- Offset_To_Top_Functions are built only for derivations of types
2153 -- with discriminants that cover interface types.
2154 -- Nothing is needed either in case of virtual targets, since
2155 -- interfaces are handled directly by the target.
2157 if not Is_Tagged_Type (Rec_Type)
2158 or else Etype (Rec_Type) = Rec_Type
2159 or else not Has_Discriminants (Etype (Rec_Type))
2160 or else not Tagged_Type_Expansion
2161 then
2162 return;
2163 end if;
2165 Collect_Interface_Components (Rec_Type, Ifaces_Comp_List);
2167 -- For each interface type with secondary dispatch table we generate
2168 -- the Offset_To_Top_Functions (required to displace the pointer in
2169 -- interface conversions)
2171 Iface_Comp_Elmt := First_Elmt (Ifaces_Comp_List);
2172 while Present (Iface_Comp_Elmt) loop
2173 Iface_Comp := Node (Iface_Comp_Elmt);
2174 pragma Assert (Is_Interface (Related_Type (Iface_Comp)));
2176 -- If the interface is a parent of Rec_Type it shares the primary
2177 -- dispatch table and hence there is no need to build the function
2179 if not Is_Ancestor (Related_Type (Iface_Comp), Rec_Type,
2180 Use_Full_View => True)
2181 then
2182 Build_Offset_To_Top_Function (Iface_Comp);
2183 end if;
2185 Next_Elmt (Iface_Comp_Elmt);
2186 end loop;
2187 end Build_Offset_To_Top_Functions;
2189 ------------------------------
2190 -- Build_CPP_Init_Procedure --
2191 ------------------------------
2193 procedure Build_CPP_Init_Procedure is
2194 Body_Node : Node_Id;
2195 Body_Stmts : List_Id;
2196 Flag_Id : Entity_Id;
2197 Handled_Stmt_Node : Node_Id;
2198 Init_Tags_List : List_Id;
2199 Proc_Id : Entity_Id;
2200 Proc_Spec_Node : Node_Id;
2202 begin
2203 -- Check cases requiring no IC routine
2205 if not Is_CPP_Class (Root_Type (Rec_Type))
2206 or else Is_CPP_Class (Rec_Type)
2207 or else CPP_Num_Prims (Rec_Type) = 0
2208 or else not Tagged_Type_Expansion
2209 or else No_Run_Time_Mode
2210 then
2211 return;
2212 end if;
2214 -- Generate:
2216 -- Flag : Boolean := False;
2218 -- procedure Typ_IC is
2219 -- begin
2220 -- if not Flag then
2221 -- Copy C++ dispatch table slots from parent
2222 -- Update C++ slots of overridden primitives
2223 -- end if;
2224 -- end;
2226 Flag_Id := Make_Temporary (Loc, 'F');
2228 Append_Freeze_Action (Rec_Type,
2229 Make_Object_Declaration (Loc,
2230 Defining_Identifier => Flag_Id,
2231 Object_Definition =>
2232 New_Occurrence_Of (Standard_Boolean, Loc),
2233 Expression =>
2234 New_Occurrence_Of (Standard_True, Loc)));
2236 Body_Stmts := New_List;
2237 Body_Node := New_Node (N_Subprogram_Body, Loc);
2239 Proc_Spec_Node := New_Node (N_Procedure_Specification, Loc);
2241 Proc_Id :=
2242 Make_Defining_Identifier (Loc,
2243 Chars => Make_TSS_Name (Rec_Type, TSS_CPP_Init_Proc));
2245 Set_Ekind (Proc_Id, E_Procedure);
2246 Set_Is_Internal (Proc_Id);
2248 Set_Defining_Unit_Name (Proc_Spec_Node, Proc_Id);
2250 Set_Parameter_Specifications (Proc_Spec_Node, New_List);
2251 Set_Specification (Body_Node, Proc_Spec_Node);
2252 Set_Declarations (Body_Node, New_List);
2254 Init_Tags_List := Build_Inherit_CPP_Prims (Rec_Type);
2256 Append_To (Init_Tags_List,
2257 Make_Assignment_Statement (Loc,
2258 Name =>
2259 New_Occurrence_Of (Flag_Id, Loc),
2260 Expression =>
2261 New_Occurrence_Of (Standard_False, Loc)));
2263 Append_To (Body_Stmts,
2264 Make_If_Statement (Loc,
2265 Condition => New_Occurrence_Of (Flag_Id, Loc),
2266 Then_Statements => Init_Tags_List));
2268 Handled_Stmt_Node :=
2269 New_Node (N_Handled_Sequence_Of_Statements, Loc);
2270 Set_Statements (Handled_Stmt_Node, Body_Stmts);
2271 Set_Exception_Handlers (Handled_Stmt_Node, No_List);
2272 Set_Handled_Statement_Sequence (Body_Node, Handled_Stmt_Node);
2274 if not Debug_Generated_Code then
2275 Set_Debug_Info_Off (Proc_Id);
2276 end if;
2278 -- Associate CPP_Init_Proc with type
2280 Set_Init_Proc (Rec_Type, Proc_Id);
2281 end Build_CPP_Init_Procedure;
2283 --------------------------
2284 -- Build_Init_Procedure --
2285 --------------------------
2287 procedure Build_Init_Procedure is
2288 Body_Stmts : List_Id;
2289 Body_Node : Node_Id;
2290 Handled_Stmt_Node : Node_Id;
2291 Init_Tags_List : List_Id;
2292 Parameters : List_Id;
2293 Proc_Spec_Node : Node_Id;
2294 Record_Extension_Node : Node_Id;
2296 begin
2297 Body_Stmts := New_List;
2298 Body_Node := New_Node (N_Subprogram_Body, Loc);
2299 Set_Ekind (Proc_Id, E_Procedure);
2301 Proc_Spec_Node := New_Node (N_Procedure_Specification, Loc);
2302 Set_Defining_Unit_Name (Proc_Spec_Node, Proc_Id);
2304 Parameters := Init_Formals (Rec_Type);
2305 Append_List_To (Parameters,
2306 Build_Discriminant_Formals (Rec_Type, True));
2308 -- For tagged types, we add a flag to indicate whether the routine
2309 -- is called to initialize a parent component in the init_proc of
2310 -- a type extension. If the flag is false, we do not set the tag
2311 -- because it has been set already in the extension.
2313 if Is_Tagged_Type (Rec_Type) then
2314 Set_Tag := Make_Temporary (Loc, 'P');
2316 Append_To (Parameters,
2317 Make_Parameter_Specification (Loc,
2318 Defining_Identifier => Set_Tag,
2319 Parameter_Type =>
2320 New_Occurrence_Of (Standard_Boolean, Loc),
2321 Expression =>
2322 New_Occurrence_Of (Standard_True, Loc)));
2323 end if;
2325 Set_Parameter_Specifications (Proc_Spec_Node, Parameters);
2326 Set_Specification (Body_Node, Proc_Spec_Node);
2327 Set_Declarations (Body_Node, Decls);
2329 -- N is a Derived_Type_Definition that renames the parameters of the
2330 -- ancestor type. We initialize it by expanding our discriminants and
2331 -- call the ancestor _init_proc with a type-converted object.
2333 if Parent_Subtype_Renaming_Discrims then
2334 Append_List_To (Body_Stmts, Build_Init_Call_Thru (Parameters));
2336 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
2337 Build_Discriminant_Assignments (Body_Stmts);
2339 if not Null_Present (Type_Definition (N)) then
2340 Append_List_To (Body_Stmts,
2341 Build_Init_Statements (Component_List (Type_Definition (N))));
2342 end if;
2344 -- N is a Derived_Type_Definition with a possible non-empty
2345 -- extension. The initialization of a type extension consists in the
2346 -- initialization of the components in the extension.
2348 else
2349 Build_Discriminant_Assignments (Body_Stmts);
2351 Record_Extension_Node :=
2352 Record_Extension_Part (Type_Definition (N));
2354 if not Null_Present (Record_Extension_Node) then
2355 declare
2356 Stmts : constant List_Id :=
2357 Build_Init_Statements (
2358 Component_List (Record_Extension_Node));
2360 begin
2361 -- The parent field must be initialized first because the
2362 -- offset of the new discriminants may depend on it. This is
2363 -- not needed if the parent is an interface type because in
2364 -- such case the initialization of the _parent field was not
2365 -- generated.
2367 if not Is_Interface (Etype (Rec_Ent)) then
2368 declare
2369 Parent_IP : constant Name_Id :=
2370 Make_Init_Proc_Name (Etype (Rec_Ent));
2371 Stmt : Node_Id;
2372 IP_Call : Node_Id;
2373 IP_Stmts : List_Id;
2375 begin
2376 -- Look for a call to the parent IP at the beginning
2377 -- of Stmts associated with the record extension
2379 Stmt := First (Stmts);
2380 IP_Call := Empty;
2381 while Present (Stmt) loop
2382 if Nkind (Stmt) = N_Procedure_Call_Statement
2383 and then Chars (Name (Stmt)) = Parent_IP
2384 then
2385 IP_Call := Stmt;
2386 exit;
2387 end if;
2389 Next (Stmt);
2390 end loop;
2392 -- If found then move it to the beginning of the
2393 -- statements of this IP routine
2395 if Present (IP_Call) then
2396 IP_Stmts := New_List;
2397 loop
2398 Stmt := Remove_Head (Stmts);
2399 Append_To (IP_Stmts, Stmt);
2400 exit when Stmt = IP_Call;
2401 end loop;
2403 Prepend_List_To (Body_Stmts, IP_Stmts);
2404 end if;
2405 end;
2406 end if;
2408 Append_List_To (Body_Stmts, Stmts);
2409 end;
2410 end if;
2411 end if;
2413 -- Add here the assignment to instantiate the Tag
2415 -- The assignment corresponds to the code:
2417 -- _Init._Tag := Typ'Tag;
2419 -- Suppress the tag assignment when not Tagged_Type_Expansion because
2420 -- tags are represented implicitly in objects. It is also suppressed
2421 -- in case of CPP_Class types because in this case the tag is
2422 -- initialized in the C++ side.
2424 if Is_Tagged_Type (Rec_Type)
2425 and then Tagged_Type_Expansion
2426 and then not No_Run_Time_Mode
2427 then
2428 -- Case 1: Ada tagged types with no CPP ancestor. Set the tags of
2429 -- the actual object and invoke the IP of the parent (in this
2430 -- order). The tag must be initialized before the call to the IP
2431 -- of the parent and the assignments to other components because
2432 -- the initial value of the components may depend on the tag (eg.
2433 -- through a dispatching operation on an access to the current
2434 -- type). The tag assignment is not done when initializing the
2435 -- parent component of a type extension, because in that case the
2436 -- tag is set in the extension.
2438 if not Is_CPP_Class (Root_Type (Rec_Type)) then
2440 -- Initialize the primary tag component
2442 Init_Tags_List := New_List (
2443 Make_Assignment_Statement (Loc,
2444 Name =>
2445 Make_Selected_Component (Loc,
2446 Prefix => Make_Identifier (Loc, Name_uInit),
2447 Selector_Name =>
2448 New_Occurrence_Of
2449 (First_Tag_Component (Rec_Type), Loc)),
2450 Expression =>
2451 New_Occurrence_Of
2452 (Node
2453 (First_Elmt (Access_Disp_Table (Rec_Type))), Loc)));
2455 -- Ada 2005 (AI-251): Initialize the secondary tags components
2456 -- located at fixed positions (tags whose position depends on
2457 -- variable size components are initialized later ---see below)
2459 if Ada_Version >= Ada_2005
2460 and then not Is_Interface (Rec_Type)
2461 and then Has_Interfaces (Rec_Type)
2462 then
2463 Init_Secondary_Tags
2464 (Typ => Rec_Type,
2465 Target => Make_Identifier (Loc, Name_uInit),
2466 Stmts_List => Init_Tags_List,
2467 Fixed_Comps => True,
2468 Variable_Comps => False);
2469 end if;
2471 Prepend_To (Body_Stmts,
2472 Make_If_Statement (Loc,
2473 Condition => New_Occurrence_Of (Set_Tag, Loc),
2474 Then_Statements => Init_Tags_List));
2476 -- Case 2: CPP type. The imported C++ constructor takes care of
2477 -- tags initialization. No action needed here because the IP
2478 -- is built by Set_CPP_Constructors; in this case the IP is a
2479 -- wrapper that invokes the C++ constructor and copies the C++
2480 -- tags locally. Done to inherit the C++ slots in Ada derivations
2481 -- (see case 3).
2483 elsif Is_CPP_Class (Rec_Type) then
2484 pragma Assert (False);
2485 null;
2487 -- Case 3: Combined hierarchy containing C++ types and Ada tagged
2488 -- type derivations. Derivations of imported C++ classes add a
2489 -- complication, because we cannot inhibit tag setting in the
2490 -- constructor for the parent. Hence we initialize the tag after
2491 -- the call to the parent IP (that is, in reverse order compared
2492 -- with pure Ada hierarchies ---see comment on case 1).
2494 else
2495 -- Initialize the primary tag
2497 Init_Tags_List := New_List (
2498 Make_Assignment_Statement (Loc,
2499 Name =>
2500 Make_Selected_Component (Loc,
2501 Prefix => Make_Identifier (Loc, Name_uInit),
2502 Selector_Name =>
2503 New_Occurrence_Of
2504 (First_Tag_Component (Rec_Type), Loc)),
2505 Expression =>
2506 New_Occurrence_Of
2507 (Node
2508 (First_Elmt (Access_Disp_Table (Rec_Type))), Loc)));
2510 -- Ada 2005 (AI-251): Initialize the secondary tags components
2511 -- located at fixed positions (tags whose position depends on
2512 -- variable size components are initialized later ---see below)
2514 if Ada_Version >= Ada_2005
2515 and then not Is_Interface (Rec_Type)
2516 and then Has_Interfaces (Rec_Type)
2517 then
2518 Init_Secondary_Tags
2519 (Typ => Rec_Type,
2520 Target => Make_Identifier (Loc, Name_uInit),
2521 Stmts_List => Init_Tags_List,
2522 Fixed_Comps => True,
2523 Variable_Comps => False);
2524 end if;
2526 -- Initialize the tag component after invocation of parent IP.
2528 -- Generate:
2529 -- parent_IP(_init.parent); // Invokes the C++ constructor
2530 -- [ typIC; ] // Inherit C++ slots from parent
2531 -- init_tags
2533 declare
2534 Ins_Nod : Node_Id;
2536 begin
2537 -- Search for the call to the IP of the parent. We assume
2538 -- that the first init_proc call is for the parent.
2540 Ins_Nod := First (Body_Stmts);
2541 while Present (Next (Ins_Nod))
2542 and then (Nkind (Ins_Nod) /= N_Procedure_Call_Statement
2543 or else not Is_Init_Proc (Name (Ins_Nod)))
2544 loop
2545 Next (Ins_Nod);
2546 end loop;
2548 -- The IC routine copies the inherited slots of the C+ part
2549 -- of the dispatch table from the parent and updates the
2550 -- overridden C++ slots.
2552 if CPP_Num_Prims (Rec_Type) > 0 then
2553 declare
2554 Init_DT : Entity_Id;
2555 New_Nod : Node_Id;
2557 begin
2558 Init_DT := CPP_Init_Proc (Rec_Type);
2559 pragma Assert (Present (Init_DT));
2561 New_Nod :=
2562 Make_Procedure_Call_Statement (Loc,
2563 New_Occurrence_Of (Init_DT, Loc));
2564 Insert_After (Ins_Nod, New_Nod);
2566 -- Update location of init tag statements
2568 Ins_Nod := New_Nod;
2569 end;
2570 end if;
2572 Insert_List_After (Ins_Nod, Init_Tags_List);
2573 end;
2574 end if;
2576 -- Ada 2005 (AI-251): Initialize the secondary tag components
2577 -- located at variable positions. We delay the generation of this
2578 -- code until here because the value of the attribute 'Position
2579 -- applied to variable size components of the parent type that
2580 -- depend on discriminants is only safely read at runtime after
2581 -- the parent components have been initialized.
2583 if Ada_Version >= Ada_2005
2584 and then not Is_Interface (Rec_Type)
2585 and then Has_Interfaces (Rec_Type)
2586 and then Has_Discriminants (Etype (Rec_Type))
2587 and then Is_Variable_Size_Record (Etype (Rec_Type))
2588 then
2589 Init_Tags_List := New_List;
2591 Init_Secondary_Tags
2592 (Typ => Rec_Type,
2593 Target => Make_Identifier (Loc, Name_uInit),
2594 Stmts_List => Init_Tags_List,
2595 Fixed_Comps => False,
2596 Variable_Comps => True);
2598 if Is_Non_Empty_List (Init_Tags_List) then
2599 Append_List_To (Body_Stmts, Init_Tags_List);
2600 end if;
2601 end if;
2602 end if;
2604 Handled_Stmt_Node := New_Node (N_Handled_Sequence_Of_Statements, Loc);
2605 Set_Statements (Handled_Stmt_Node, Body_Stmts);
2607 -- Generate:
2608 -- Deep_Finalize (_init, C1, ..., CN);
2609 -- raise;
2611 if Counter > 0
2612 and then Needs_Finalization (Rec_Type)
2613 and then not Is_Abstract_Type (Rec_Type)
2614 and then not Restriction_Active (No_Exception_Propagation)
2615 then
2616 declare
2617 DF_Call : Node_Id;
2618 DF_Id : Entity_Id;
2620 begin
2621 -- Create a local version of Deep_Finalize which has indication
2622 -- of partial initialization state.
2624 DF_Id := Make_Temporary (Loc, 'F');
2626 Append_To (Decls, Make_Local_Deep_Finalize (Rec_Type, DF_Id));
2628 DF_Call :=
2629 Make_Procedure_Call_Statement (Loc,
2630 Name => New_Occurrence_Of (DF_Id, Loc),
2631 Parameter_Associations => New_List (
2632 Make_Identifier (Loc, Name_uInit),
2633 New_Occurrence_Of (Standard_False, Loc)));
2635 -- Do not emit warnings related to the elaboration order when a
2636 -- controlled object is declared before the body of Finalize is
2637 -- seen.
2639 Set_No_Elaboration_Check (DF_Call);
2641 Set_Exception_Handlers (Handled_Stmt_Node, New_List (
2642 Make_Exception_Handler (Loc,
2643 Exception_Choices => New_List (
2644 Make_Others_Choice (Loc)),
2645 Statements => New_List (
2646 DF_Call,
2647 Make_Raise_Statement (Loc)))));
2648 end;
2649 else
2650 Set_Exception_Handlers (Handled_Stmt_Node, No_List);
2651 end if;
2653 Set_Handled_Statement_Sequence (Body_Node, Handled_Stmt_Node);
2655 if not Debug_Generated_Code then
2656 Set_Debug_Info_Off (Proc_Id);
2657 end if;
2659 -- Associate Init_Proc with type, and determine if the procedure
2660 -- is null (happens because of the Initialize_Scalars pragma case,
2661 -- where we have to generate a null procedure in case it is called
2662 -- by a client with Initialize_Scalars set). Such procedures have
2663 -- to be generated, but do not have to be called, so we mark them
2664 -- as null to suppress the call.
2666 Set_Init_Proc (Rec_Type, Proc_Id);
2668 if List_Length (Body_Stmts) = 1
2670 -- We must skip SCIL nodes because they may have been added to this
2671 -- list by Insert_Actions.
2673 and then Nkind (First_Non_SCIL_Node (Body_Stmts)) = N_Null_Statement
2674 then
2675 Set_Is_Null_Init_Proc (Proc_Id);
2676 end if;
2677 end Build_Init_Procedure;
2679 ---------------------------
2680 -- Build_Init_Statements --
2681 ---------------------------
2683 function Build_Init_Statements (Comp_List : Node_Id) return List_Id is
2684 Checks : constant List_Id := New_List;
2685 Actions : List_Id := No_List;
2686 Counter_Id : Entity_Id := Empty;
2687 Comp_Loc : Source_Ptr;
2688 Decl : Node_Id;
2689 Has_POC : Boolean;
2690 Id : Entity_Id;
2691 Parent_Stmts : List_Id;
2692 Stmts : List_Id;
2693 Typ : Entity_Id;
2695 procedure Increment_Counter (Loc : Source_Ptr);
2696 -- Generate an "increment by one" statement for the current counter
2697 -- and append it to the list Stmts.
2699 procedure Make_Counter (Loc : Source_Ptr);
2700 -- Create a new counter for the current component list. The routine
2701 -- creates a new defining Id, adds an object declaration and sets
2702 -- the Id generator for the next variant.
2704 -----------------------
2705 -- Increment_Counter --
2706 -----------------------
2708 procedure Increment_Counter (Loc : Source_Ptr) is
2709 begin
2710 -- Generate:
2711 -- Counter := Counter + 1;
2713 Append_To (Stmts,
2714 Make_Assignment_Statement (Loc,
2715 Name => New_Occurrence_Of (Counter_Id, Loc),
2716 Expression =>
2717 Make_Op_Add (Loc,
2718 Left_Opnd => New_Occurrence_Of (Counter_Id, Loc),
2719 Right_Opnd => Make_Integer_Literal (Loc, 1))));
2720 end Increment_Counter;
2722 ------------------
2723 -- Make_Counter --
2724 ------------------
2726 procedure Make_Counter (Loc : Source_Ptr) is
2727 begin
2728 -- Increment the Id generator
2730 Counter := Counter + 1;
2732 -- Create the entity and declaration
2734 Counter_Id :=
2735 Make_Defining_Identifier (Loc,
2736 Chars => New_External_Name ('C', Counter));
2738 -- Generate:
2739 -- Cnn : Integer := 0;
2741 Append_To (Decls,
2742 Make_Object_Declaration (Loc,
2743 Defining_Identifier => Counter_Id,
2744 Object_Definition =>
2745 New_Occurrence_Of (Standard_Integer, Loc),
2746 Expression =>
2747 Make_Integer_Literal (Loc, 0)));
2748 end Make_Counter;
2750 -- Start of processing for Build_Init_Statements
2752 begin
2753 if Null_Present (Comp_List) then
2754 return New_List (Make_Null_Statement (Loc));
2755 end if;
2757 Parent_Stmts := New_List;
2758 Stmts := New_List;
2760 -- Loop through visible declarations of task types and protected
2761 -- types moving any expanded code from the spec to the body of the
2762 -- init procedure.
2764 if Is_Task_Record_Type (Rec_Type)
2765 or else Is_Protected_Record_Type (Rec_Type)
2766 then
2767 declare
2768 Decl : constant Node_Id :=
2769 Parent (Corresponding_Concurrent_Type (Rec_Type));
2770 Def : Node_Id;
2771 N1 : Node_Id;
2772 N2 : Node_Id;
2774 begin
2775 if Is_Task_Record_Type (Rec_Type) then
2776 Def := Task_Definition (Decl);
2777 else
2778 Def := Protected_Definition (Decl);
2779 end if;
2781 if Present (Def) then
2782 N1 := First (Visible_Declarations (Def));
2783 while Present (N1) loop
2784 N2 := N1;
2785 N1 := Next (N1);
2787 if Nkind (N2) in N_Statement_Other_Than_Procedure_Call
2788 or else Nkind (N2) in N_Raise_xxx_Error
2789 or else Nkind (N2) = N_Procedure_Call_Statement
2790 then
2791 Append_To (Stmts,
2792 New_Copy_Tree (N2, New_Scope => Proc_Id));
2793 Rewrite (N2, Make_Null_Statement (Sloc (N2)));
2794 Analyze (N2);
2795 end if;
2796 end loop;
2797 end if;
2798 end;
2799 end if;
2801 -- Loop through components, skipping pragmas, in 2 steps. The first
2802 -- step deals with regular components. The second step deals with
2803 -- components that have per object constraints and no explicit
2804 -- initialization.
2806 Has_POC := False;
2808 -- First pass : regular components
2810 Decl := First_Non_Pragma (Component_Items (Comp_List));
2811 while Present (Decl) loop
2812 Comp_Loc := Sloc (Decl);
2813 Build_Record_Checks
2814 (Subtype_Indication (Component_Definition (Decl)), Checks);
2816 Id := Defining_Identifier (Decl);
2817 Typ := Etype (Id);
2819 -- Leave any processing of per-object constrained component for
2820 -- the second pass.
2822 if Has_Access_Constraint (Id) and then No (Expression (Decl)) then
2823 Has_POC := True;
2825 -- Regular component cases
2827 else
2828 -- In the context of the init proc, references to discriminants
2829 -- resolve to denote the discriminals: this is where we can
2830 -- freeze discriminant dependent component subtypes.
2832 if not Is_Frozen (Typ) then
2833 Append_List_To (Stmts, Freeze_Entity (Typ, N));
2834 end if;
2836 -- Explicit initialization
2838 if Present (Expression (Decl)) then
2839 if Is_CPP_Constructor_Call (Expression (Decl)) then
2840 Actions :=
2841 Build_Initialization_Call
2842 (Comp_Loc,
2843 Id_Ref =>
2844 Make_Selected_Component (Comp_Loc,
2845 Prefix =>
2846 Make_Identifier (Comp_Loc, Name_uInit),
2847 Selector_Name =>
2848 New_Occurrence_Of (Id, Comp_Loc)),
2849 Typ => Typ,
2850 In_Init_Proc => True,
2851 Enclos_Type => Rec_Type,
2852 Discr_Map => Discr_Map,
2853 Constructor_Ref => Expression (Decl));
2854 else
2855 Actions := Build_Assignment (Id, Expression (Decl));
2856 end if;
2858 -- CPU, Dispatching_Domain, Priority, and Secondary_Stack_Size
2859 -- components are filled in with the corresponding rep-item
2860 -- expression of the concurrent type (if any).
2862 elsif Ekind (Scope (Id)) = E_Record_Type
2863 and then Present (Corresponding_Concurrent_Type (Scope (Id)))
2864 and then Nam_In (Chars (Id), Name_uCPU,
2865 Name_uDispatching_Domain,
2866 Name_uPriority,
2867 Name_uSecondary_Stack_Size)
2868 then
2869 declare
2870 Exp : Node_Id;
2871 Nam : Name_Id;
2872 Ritem : Node_Id;
2874 begin
2875 if Chars (Id) = Name_uCPU then
2876 Nam := Name_CPU;
2878 elsif Chars (Id) = Name_uDispatching_Domain then
2879 Nam := Name_Dispatching_Domain;
2881 elsif Chars (Id) = Name_uPriority then
2882 Nam := Name_Priority;
2884 elsif Chars (Id) = Name_uSecondary_Stack_Size then
2885 Nam := Name_Secondary_Stack_Size;
2886 end if;
2888 -- Get the Rep Item (aspect specification, attribute
2889 -- definition clause or pragma) of the corresponding
2890 -- concurrent type.
2892 Ritem :=
2893 Get_Rep_Item
2894 (Corresponding_Concurrent_Type (Scope (Id)),
2895 Nam,
2896 Check_Parents => False);
2898 if Present (Ritem) then
2900 -- Pragma case
2902 if Nkind (Ritem) = N_Pragma then
2903 Exp := First (Pragma_Argument_Associations (Ritem));
2905 if Nkind (Exp) = N_Pragma_Argument_Association then
2906 Exp := Expression (Exp);
2907 end if;
2909 -- Conversion for Priority expression
2911 if Nam = Name_Priority then
2912 if Pragma_Name (Ritem) = Name_Priority
2913 and then not GNAT_Mode
2914 then
2915 Exp := Convert_To (RTE (RE_Priority), Exp);
2916 else
2917 Exp :=
2918 Convert_To (RTE (RE_Any_Priority), Exp);
2919 end if;
2920 end if;
2922 -- Aspect/Attribute definition clause case
2924 else
2925 Exp := Expression (Ritem);
2927 -- Conversion for Priority expression
2929 if Nam = Name_Priority then
2930 if Chars (Ritem) = Name_Priority
2931 and then not GNAT_Mode
2932 then
2933 Exp := Convert_To (RTE (RE_Priority), Exp);
2934 else
2935 Exp :=
2936 Convert_To (RTE (RE_Any_Priority), Exp);
2937 end if;
2938 end if;
2939 end if;
2941 -- Conversion for Dispatching_Domain value
2943 if Nam = Name_Dispatching_Domain then
2944 Exp :=
2945 Unchecked_Convert_To
2946 (RTE (RE_Dispatching_Domain_Access), Exp);
2947 end if;
2949 Actions := Build_Assignment (Id, Exp);
2951 -- Nothing needed if no Rep Item
2953 else
2954 Actions := No_List;
2955 end if;
2956 end;
2958 -- Composite component with its own Init_Proc
2960 elsif not Is_Interface (Typ)
2961 and then Has_Non_Null_Base_Init_Proc (Typ)
2962 then
2963 Actions :=
2964 Build_Initialization_Call
2965 (Comp_Loc,
2966 Make_Selected_Component (Comp_Loc,
2967 Prefix =>
2968 Make_Identifier (Comp_Loc, Name_uInit),
2969 Selector_Name => New_Occurrence_Of (Id, Comp_Loc)),
2970 Typ,
2971 In_Init_Proc => True,
2972 Enclos_Type => Rec_Type,
2973 Discr_Map => Discr_Map);
2975 Clean_Task_Names (Typ, Proc_Id);
2977 -- Simple initialization
2979 elsif Component_Needs_Simple_Initialization (Typ) then
2980 Actions :=
2981 Build_Assignment
2982 (Id, Get_Simple_Init_Val (Typ, N, Esize (Id)));
2984 -- Nothing needed for this case
2986 else
2987 Actions := No_List;
2988 end if;
2990 if Present (Checks) then
2991 if Chars (Id) = Name_uParent then
2992 Append_List_To (Parent_Stmts, Checks);
2993 else
2994 Append_List_To (Stmts, Checks);
2995 end if;
2996 end if;
2998 if Present (Actions) then
2999 if Chars (Id) = Name_uParent then
3000 Append_List_To (Parent_Stmts, Actions);
3002 else
3003 Append_List_To (Stmts, Actions);
3005 -- Preserve initialization state in the current counter
3007 if Needs_Finalization (Typ) then
3008 if No (Counter_Id) then
3009 Make_Counter (Comp_Loc);
3010 end if;
3012 Increment_Counter (Comp_Loc);
3013 end if;
3014 end if;
3015 end if;
3016 end if;
3018 Next_Non_Pragma (Decl);
3019 end loop;
3021 -- The parent field must be initialized first because variable
3022 -- size components of the parent affect the location of all the
3023 -- new components.
3025 Prepend_List_To (Stmts, Parent_Stmts);
3027 -- Set up tasks and protected object support. This needs to be done
3028 -- before any component with a per-object access discriminant
3029 -- constraint, or any variant part (which may contain such
3030 -- components) is initialized, because the initialization of these
3031 -- components may reference the enclosing concurrent object.
3033 -- For a task record type, add the task create call and calls to bind
3034 -- any interrupt (signal) entries.
3036 if Is_Task_Record_Type (Rec_Type) then
3038 -- In the case of the restricted run time the ATCB has already
3039 -- been preallocated.
3041 if Restricted_Profile then
3042 Append_To (Stmts,
3043 Make_Assignment_Statement (Loc,
3044 Name =>
3045 Make_Selected_Component (Loc,
3046 Prefix => Make_Identifier (Loc, Name_uInit),
3047 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)),
3048 Expression =>
3049 Make_Attribute_Reference (Loc,
3050 Prefix =>
3051 Make_Selected_Component (Loc,
3052 Prefix => Make_Identifier (Loc, Name_uInit),
3053 Selector_Name => Make_Identifier (Loc, Name_uATCB)),
3054 Attribute_Name => Name_Unchecked_Access)));
3055 end if;
3057 Append_To (Stmts, Make_Task_Create_Call (Rec_Type));
3059 declare
3060 Task_Type : constant Entity_Id :=
3061 Corresponding_Concurrent_Type (Rec_Type);
3062 Task_Decl : constant Node_Id := Parent (Task_Type);
3063 Task_Def : constant Node_Id := Task_Definition (Task_Decl);
3064 Decl_Loc : Source_Ptr;
3065 Ent : Entity_Id;
3066 Vis_Decl : Node_Id;
3068 begin
3069 if Present (Task_Def) then
3070 Vis_Decl := First (Visible_Declarations (Task_Def));
3071 while Present (Vis_Decl) loop
3072 Decl_Loc := Sloc (Vis_Decl);
3074 if Nkind (Vis_Decl) = N_Attribute_Definition_Clause then
3075 if Get_Attribute_Id (Chars (Vis_Decl)) =
3076 Attribute_Address
3077 then
3078 Ent := Entity (Name (Vis_Decl));
3080 if Ekind (Ent) = E_Entry then
3081 Append_To (Stmts,
3082 Make_Procedure_Call_Statement (Decl_Loc,
3083 Name =>
3084 New_Occurrence_Of (RTE (
3085 RE_Bind_Interrupt_To_Entry), Decl_Loc),
3086 Parameter_Associations => New_List (
3087 Make_Selected_Component (Decl_Loc,
3088 Prefix =>
3089 Make_Identifier (Decl_Loc, Name_uInit),
3090 Selector_Name =>
3091 Make_Identifier
3092 (Decl_Loc, Name_uTask_Id)),
3093 Entry_Index_Expression
3094 (Decl_Loc, Ent, Empty, Task_Type),
3095 Expression (Vis_Decl))));
3096 end if;
3097 end if;
3098 end if;
3100 Next (Vis_Decl);
3101 end loop;
3102 end if;
3103 end;
3104 end if;
3106 -- For a protected type, add statements generated by
3107 -- Make_Initialize_Protection.
3109 if Is_Protected_Record_Type (Rec_Type) then
3110 Append_List_To (Stmts,
3111 Make_Initialize_Protection (Rec_Type));
3112 end if;
3114 -- Second pass: components with per-object constraints
3116 if Has_POC then
3117 Decl := First_Non_Pragma (Component_Items (Comp_List));
3118 while Present (Decl) loop
3119 Comp_Loc := Sloc (Decl);
3120 Id := Defining_Identifier (Decl);
3121 Typ := Etype (Id);
3123 if Has_Access_Constraint (Id)
3124 and then No (Expression (Decl))
3125 then
3126 if Has_Non_Null_Base_Init_Proc (Typ) then
3127 Append_List_To (Stmts,
3128 Build_Initialization_Call (Comp_Loc,
3129 Make_Selected_Component (Comp_Loc,
3130 Prefix =>
3131 Make_Identifier (Comp_Loc, Name_uInit),
3132 Selector_Name => New_Occurrence_Of (Id, Comp_Loc)),
3133 Typ,
3134 In_Init_Proc => True,
3135 Enclos_Type => Rec_Type,
3136 Discr_Map => Discr_Map));
3138 Clean_Task_Names (Typ, Proc_Id);
3140 -- Preserve initialization state in the current counter
3142 if Needs_Finalization (Typ) then
3143 if No (Counter_Id) then
3144 Make_Counter (Comp_Loc);
3145 end if;
3147 Increment_Counter (Comp_Loc);
3148 end if;
3150 elsif Component_Needs_Simple_Initialization (Typ) then
3151 Append_List_To (Stmts,
3152 Build_Assignment
3153 (Id, Get_Simple_Init_Val (Typ, N, Esize (Id))));
3154 end if;
3155 end if;
3157 Next_Non_Pragma (Decl);
3158 end loop;
3159 end if;
3161 -- Process the variant part
3163 if Present (Variant_Part (Comp_List)) then
3164 declare
3165 Variant_Alts : constant List_Id := New_List;
3166 Var_Loc : Source_Ptr;
3167 Variant : Node_Id;
3169 begin
3170 Variant :=
3171 First_Non_Pragma (Variants (Variant_Part (Comp_List)));
3172 while Present (Variant) loop
3173 Var_Loc := Sloc (Variant);
3174 Append_To (Variant_Alts,
3175 Make_Case_Statement_Alternative (Var_Loc,
3176 Discrete_Choices =>
3177 New_Copy_List (Discrete_Choices (Variant)),
3178 Statements =>
3179 Build_Init_Statements (Component_List (Variant))));
3180 Next_Non_Pragma (Variant);
3181 end loop;
3183 -- The expression of the case statement which is a reference
3184 -- to one of the discriminants is replaced by the appropriate
3185 -- formal parameter of the initialization procedure.
3187 Append_To (Stmts,
3188 Make_Case_Statement (Var_Loc,
3189 Expression =>
3190 New_Occurrence_Of (Discriminal (
3191 Entity (Name (Variant_Part (Comp_List)))), Var_Loc),
3192 Alternatives => Variant_Alts));
3193 end;
3194 end if;
3196 -- If no initializations when generated for component declarations
3197 -- corresponding to this Stmts, append a null statement to Stmts to
3198 -- to make it a valid Ada tree.
3200 if Is_Empty_List (Stmts) then
3201 Append (Make_Null_Statement (Loc), Stmts);
3202 end if;
3204 return Stmts;
3206 exception
3207 when RE_Not_Available =>
3208 return Empty_List;
3209 end Build_Init_Statements;
3211 -------------------------
3212 -- Build_Record_Checks --
3213 -------------------------
3215 procedure Build_Record_Checks (S : Node_Id; Check_List : List_Id) is
3216 Subtype_Mark_Id : Entity_Id;
3218 procedure Constrain_Array
3219 (SI : Node_Id;
3220 Check_List : List_Id);
3221 -- Apply a list of index constraints to an unconstrained array type.
3222 -- The first parameter is the entity for the resulting subtype.
3223 -- Check_List is a list to which the check actions are appended.
3225 ---------------------
3226 -- Constrain_Array --
3227 ---------------------
3229 procedure Constrain_Array
3230 (SI : Node_Id;
3231 Check_List : List_Id)
3233 C : constant Node_Id := Constraint (SI);
3234 Number_Of_Constraints : Nat := 0;
3235 Index : Node_Id;
3236 S, T : Entity_Id;
3238 procedure Constrain_Index
3239 (Index : Node_Id;
3240 S : Node_Id;
3241 Check_List : List_Id);
3242 -- Process an index constraint in a constrained array declaration.
3243 -- The constraint can be either a subtype name or a range with or
3244 -- without an explicit subtype mark. Index is the corresponding
3245 -- index of the unconstrained array. S is the range expression.
3246 -- Check_List is a list to which the check actions are appended.
3248 ---------------------
3249 -- Constrain_Index --
3250 ---------------------
3252 procedure Constrain_Index
3253 (Index : Node_Id;
3254 S : Node_Id;
3255 Check_List : List_Id)
3257 T : constant Entity_Id := Etype (Index);
3259 begin
3260 if Nkind (S) = N_Range then
3261 Process_Range_Expr_In_Decl (S, T, Check_List => Check_List);
3262 end if;
3263 end Constrain_Index;
3265 -- Start of processing for Constrain_Array
3267 begin
3268 T := Entity (Subtype_Mark (SI));
3270 if Is_Access_Type (T) then
3271 T := Designated_Type (T);
3272 end if;
3274 S := First (Constraints (C));
3275 while Present (S) loop
3276 Number_Of_Constraints := Number_Of_Constraints + 1;
3277 Next (S);
3278 end loop;
3280 -- In either case, the index constraint must provide a discrete
3281 -- range for each index of the array type and the type of each
3282 -- discrete range must be the same as that of the corresponding
3283 -- index. (RM 3.6.1)
3285 S := First (Constraints (C));
3286 Index := First_Index (T);
3287 Analyze (Index);
3289 -- Apply constraints to each index type
3291 for J in 1 .. Number_Of_Constraints loop
3292 Constrain_Index (Index, S, Check_List);
3293 Next (Index);
3294 Next (S);
3295 end loop;
3296 end Constrain_Array;
3298 -- Start of processing for Build_Record_Checks
3300 begin
3301 if Nkind (S) = N_Subtype_Indication then
3302 Find_Type (Subtype_Mark (S));
3303 Subtype_Mark_Id := Entity (Subtype_Mark (S));
3305 -- Remaining processing depends on type
3307 case Ekind (Subtype_Mark_Id) is
3308 when Array_Kind =>
3309 Constrain_Array (S, Check_List);
3311 when others =>
3312 null;
3313 end case;
3314 end if;
3315 end Build_Record_Checks;
3317 -------------------------------------------
3318 -- Component_Needs_Simple_Initialization --
3319 -------------------------------------------
3321 function Component_Needs_Simple_Initialization
3322 (T : Entity_Id) return Boolean
3324 begin
3325 return
3326 Needs_Simple_Initialization (T)
3327 and then not Is_RTE (T, RE_Tag)
3329 -- Ada 2005 (AI-251): Check also the tag of abstract interfaces
3331 and then not Is_RTE (T, RE_Interface_Tag);
3332 end Component_Needs_Simple_Initialization;
3334 --------------------------------------
3335 -- Parent_Subtype_Renaming_Discrims --
3336 --------------------------------------
3338 function Parent_Subtype_Renaming_Discrims return Boolean is
3339 De : Entity_Id;
3340 Dp : Entity_Id;
3342 begin
3343 if Base_Type (Rec_Ent) /= Rec_Ent then
3344 return False;
3345 end if;
3347 if Etype (Rec_Ent) = Rec_Ent
3348 or else not Has_Discriminants (Rec_Ent)
3349 or else Is_Constrained (Rec_Ent)
3350 or else Is_Tagged_Type (Rec_Ent)
3351 then
3352 return False;
3353 end if;
3355 -- If there are no explicit stored discriminants we have inherited
3356 -- the root type discriminants so far, so no renamings occurred.
3358 if First_Discriminant (Rec_Ent) =
3359 First_Stored_Discriminant (Rec_Ent)
3360 then
3361 return False;
3362 end if;
3364 -- Check if we have done some trivial renaming of the parent
3365 -- discriminants, i.e. something like
3367 -- type DT (X1, X2: int) is new PT (X1, X2);
3369 De := First_Discriminant (Rec_Ent);
3370 Dp := First_Discriminant (Etype (Rec_Ent));
3371 while Present (De) loop
3372 pragma Assert (Present (Dp));
3374 if Corresponding_Discriminant (De) /= Dp then
3375 return True;
3376 end if;
3378 Next_Discriminant (De);
3379 Next_Discriminant (Dp);
3380 end loop;
3382 return Present (Dp);
3383 end Parent_Subtype_Renaming_Discrims;
3385 ------------------------
3386 -- Requires_Init_Proc --
3387 ------------------------
3389 function Requires_Init_Proc (Rec_Id : Entity_Id) return Boolean is
3390 Comp_Decl : Node_Id;
3391 Id : Entity_Id;
3392 Typ : Entity_Id;
3394 begin
3395 -- Definitely do not need one if specifically suppressed
3397 if Initialization_Suppressed (Rec_Id) then
3398 return False;
3399 end if;
3401 -- If it is a type derived from a type with unknown discriminants,
3402 -- we cannot build an initialization procedure for it.
3404 if Has_Unknown_Discriminants (Rec_Id)
3405 or else Has_Unknown_Discriminants (Etype (Rec_Id))
3406 then
3407 return False;
3408 end if;
3410 -- Otherwise we need to generate an initialization procedure if
3411 -- Is_CPP_Class is False and at least one of the following applies:
3413 -- 1. Discriminants are present, since they need to be initialized
3414 -- with the appropriate discriminant constraint expressions.
3415 -- However, the discriminant of an unchecked union does not
3416 -- count, since the discriminant is not present.
3418 -- 2. The type is a tagged type, since the implicit Tag component
3419 -- needs to be initialized with a pointer to the dispatch table.
3421 -- 3. The type contains tasks
3423 -- 4. One or more components has an initial value
3425 -- 5. One or more components is for a type which itself requires
3426 -- an initialization procedure.
3428 -- 6. One or more components is a type that requires simple
3429 -- initialization (see Needs_Simple_Initialization), except
3430 -- that types Tag and Interface_Tag are excluded, since fields
3431 -- of these types are initialized by other means.
3433 -- 7. The type is the record type built for a task type (since at
3434 -- the very least, Create_Task must be called)
3436 -- 8. The type is the record type built for a protected type (since
3437 -- at least Initialize_Protection must be called)
3439 -- 9. The type is marked as a public entity. The reason we add this
3440 -- case (even if none of the above apply) is to properly handle
3441 -- Initialize_Scalars. If a package is compiled without an IS
3442 -- pragma, and the client is compiled with an IS pragma, then
3443 -- the client will think an initialization procedure is present
3444 -- and call it, when in fact no such procedure is required, but
3445 -- since the call is generated, there had better be a routine
3446 -- at the other end of the call, even if it does nothing).
3448 -- Note: the reason we exclude the CPP_Class case is because in this
3449 -- case the initialization is performed by the C++ constructors, and
3450 -- the IP is built by Set_CPP_Constructors.
3452 if Is_CPP_Class (Rec_Id) then
3453 return False;
3455 elsif Is_Interface (Rec_Id) then
3456 return False;
3458 elsif (Has_Discriminants (Rec_Id)
3459 and then not Is_Unchecked_Union (Rec_Id))
3460 or else Is_Tagged_Type (Rec_Id)
3461 or else Is_Concurrent_Record_Type (Rec_Id)
3462 or else Has_Task (Rec_Id)
3463 then
3464 return True;
3465 end if;
3467 Id := First_Component (Rec_Id);
3468 while Present (Id) loop
3469 Comp_Decl := Parent (Id);
3470 Typ := Etype (Id);
3472 if Present (Expression (Comp_Decl))
3473 or else Has_Non_Null_Base_Init_Proc (Typ)
3474 or else Component_Needs_Simple_Initialization (Typ)
3475 then
3476 return True;
3477 end if;
3479 Next_Component (Id);
3480 end loop;
3482 -- As explained above, a record initialization procedure is needed
3483 -- for public types in case Initialize_Scalars applies to a client.
3484 -- However, such a procedure is not needed in the case where either
3485 -- of restrictions No_Initialize_Scalars or No_Default_Initialization
3486 -- applies. No_Initialize_Scalars excludes the possibility of using
3487 -- Initialize_Scalars in any partition, and No_Default_Initialization
3488 -- implies that no initialization should ever be done for objects of
3489 -- the type, so is incompatible with Initialize_Scalars.
3491 if not Restriction_Active (No_Initialize_Scalars)
3492 and then not Restriction_Active (No_Default_Initialization)
3493 and then Is_Public (Rec_Id)
3494 then
3495 return True;
3496 end if;
3498 return False;
3499 end Requires_Init_Proc;
3501 -- Start of processing for Build_Record_Init_Proc
3503 begin
3504 Rec_Type := Defining_Identifier (N);
3506 -- This may be full declaration of a private type, in which case
3507 -- the visible entity is a record, and the private entity has been
3508 -- exchanged with it in the private part of the current package.
3509 -- The initialization procedure is built for the record type, which
3510 -- is retrievable from the private entity.
3512 if Is_Incomplete_Or_Private_Type (Rec_Type) then
3513 Rec_Type := Underlying_Type (Rec_Type);
3514 end if;
3516 -- If we have a variant record with restriction No_Implicit_Conditionals
3517 -- in effect, then we skip building the procedure. This is safe because
3518 -- if we can see the restriction, so can any caller, calls to initialize
3519 -- such records are not allowed for variant records if this restriction
3520 -- is active.
3522 if Has_Variant_Part (Rec_Type)
3523 and then Restriction_Active (No_Implicit_Conditionals)
3524 then
3525 return;
3526 end if;
3528 -- If there are discriminants, build the discriminant map to replace
3529 -- discriminants by their discriminals in complex bound expressions.
3530 -- These only arise for the corresponding records of synchronized types.
3532 if Is_Concurrent_Record_Type (Rec_Type)
3533 and then Has_Discriminants (Rec_Type)
3534 then
3535 declare
3536 Disc : Entity_Id;
3537 begin
3538 Disc := First_Discriminant (Rec_Type);
3539 while Present (Disc) loop
3540 Append_Elmt (Disc, Discr_Map);
3541 Append_Elmt (Discriminal (Disc), Discr_Map);
3542 Next_Discriminant (Disc);
3543 end loop;
3544 end;
3545 end if;
3547 -- Derived types that have no type extension can use the initialization
3548 -- procedure of their parent and do not need a procedure of their own.
3549 -- This is only correct if there are no representation clauses for the
3550 -- type or its parent, and if the parent has in fact been frozen so
3551 -- that its initialization procedure exists.
3553 if Is_Derived_Type (Rec_Type)
3554 and then not Is_Tagged_Type (Rec_Type)
3555 and then not Is_Unchecked_Union (Rec_Type)
3556 and then not Has_New_Non_Standard_Rep (Rec_Type)
3557 and then not Parent_Subtype_Renaming_Discrims
3558 and then Has_Non_Null_Base_Init_Proc (Etype (Rec_Type))
3559 then
3560 Copy_TSS (Base_Init_Proc (Etype (Rec_Type)), Rec_Type);
3562 -- Otherwise if we need an initialization procedure, then build one,
3563 -- mark it as public and inlinable and as having a completion.
3565 elsif Requires_Init_Proc (Rec_Type)
3566 or else Is_Unchecked_Union (Rec_Type)
3567 then
3568 Proc_Id :=
3569 Make_Defining_Identifier (Loc,
3570 Chars => Make_Init_Proc_Name (Rec_Type));
3572 -- If No_Default_Initialization restriction is active, then we don't
3573 -- want to build an init_proc, but we need to mark that an init_proc
3574 -- would be needed if this restriction was not active (so that we can
3575 -- detect attempts to call it), so set a dummy init_proc in place.
3577 if Restriction_Active (No_Default_Initialization) then
3578 Set_Init_Proc (Rec_Type, Proc_Id);
3579 return;
3580 end if;
3582 Build_Offset_To_Top_Functions;
3583 Build_CPP_Init_Procedure;
3584 Build_Init_Procedure;
3586 Set_Is_Public (Proc_Id, Is_Public (Rec_Ent));
3587 Set_Is_Internal (Proc_Id);
3588 Set_Has_Completion (Proc_Id);
3590 if not Debug_Generated_Code then
3591 Set_Debug_Info_Off (Proc_Id);
3592 end if;
3594 Set_Is_Inlined (Proc_Id, Inline_Init_Proc (Rec_Type));
3596 -- Do not build an aggregate if Modify_Tree_For_C, this isn't
3597 -- needed and may generate early references to non frozen types
3598 -- since we expand aggregate much more systematically.
3600 if Modify_Tree_For_C then
3601 return;
3602 end if;
3604 declare
3605 Agg : constant Node_Id :=
3606 Build_Equivalent_Record_Aggregate (Rec_Type);
3608 procedure Collect_Itypes (Comp : Node_Id);
3609 -- Generate references to itypes in the aggregate, because
3610 -- the first use of the aggregate may be in a nested scope.
3612 --------------------
3613 -- Collect_Itypes --
3614 --------------------
3616 procedure Collect_Itypes (Comp : Node_Id) is
3617 Ref : Node_Id;
3618 Sub_Aggr : Node_Id;
3619 Typ : constant Entity_Id := Etype (Comp);
3621 begin
3622 if Is_Array_Type (Typ) and then Is_Itype (Typ) then
3623 Ref := Make_Itype_Reference (Loc);
3624 Set_Itype (Ref, Typ);
3625 Append_Freeze_Action (Rec_Type, Ref);
3627 Ref := Make_Itype_Reference (Loc);
3628 Set_Itype (Ref, Etype (First_Index (Typ)));
3629 Append_Freeze_Action (Rec_Type, Ref);
3631 -- Recurse on nested arrays
3633 Sub_Aggr := First (Expressions (Comp));
3634 while Present (Sub_Aggr) loop
3635 Collect_Itypes (Sub_Aggr);
3636 Next (Sub_Aggr);
3637 end loop;
3638 end if;
3639 end Collect_Itypes;
3641 begin
3642 -- If there is a static initialization aggregate for the type,
3643 -- generate itype references for the types of its (sub)components,
3644 -- to prevent out-of-scope errors in the resulting tree.
3645 -- The aggregate may have been rewritten as a Raise node, in which
3646 -- case there are no relevant itypes.
3648 if Present (Agg) and then Nkind (Agg) = N_Aggregate then
3649 Set_Static_Initialization (Proc_Id, Agg);
3651 declare
3652 Comp : Node_Id;
3653 begin
3654 Comp := First (Component_Associations (Agg));
3655 while Present (Comp) loop
3656 Collect_Itypes (Expression (Comp));
3657 Next (Comp);
3658 end loop;
3659 end;
3660 end if;
3661 end;
3662 end if;
3663 end Build_Record_Init_Proc;
3665 ----------------------------
3666 -- Build_Slice_Assignment --
3667 ----------------------------
3669 -- Generates the following subprogram:
3671 -- procedure Assign
3672 -- (Source, Target : Array_Type,
3673 -- Left_Lo, Left_Hi : Index;
3674 -- Right_Lo, Right_Hi : Index;
3675 -- Rev : Boolean)
3676 -- is
3677 -- Li1 : Index;
3678 -- Ri1 : Index;
3680 -- begin
3682 -- if Left_Hi < Left_Lo then
3683 -- return;
3684 -- end if;
3686 -- if Rev then
3687 -- Li1 := Left_Hi;
3688 -- Ri1 := Right_Hi;
3689 -- else
3690 -- Li1 := Left_Lo;
3691 -- Ri1 := Right_Lo;
3692 -- end if;
3694 -- loop
3695 -- Target (Li1) := Source (Ri1);
3697 -- if Rev then
3698 -- exit when Li1 = Left_Lo;
3699 -- Li1 := Index'pred (Li1);
3700 -- Ri1 := Index'pred (Ri1);
3701 -- else
3702 -- exit when Li1 = Left_Hi;
3703 -- Li1 := Index'succ (Li1);
3704 -- Ri1 := Index'succ (Ri1);
3705 -- end if;
3706 -- end loop;
3707 -- end Assign;
3709 procedure Build_Slice_Assignment (Typ : Entity_Id) is
3710 Loc : constant Source_Ptr := Sloc (Typ);
3711 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
3713 Larray : constant Entity_Id := Make_Temporary (Loc, 'A');
3714 Rarray : constant Entity_Id := Make_Temporary (Loc, 'R');
3715 Left_Lo : constant Entity_Id := Make_Temporary (Loc, 'L');
3716 Left_Hi : constant Entity_Id := Make_Temporary (Loc, 'L');
3717 Right_Lo : constant Entity_Id := Make_Temporary (Loc, 'R');
3718 Right_Hi : constant Entity_Id := Make_Temporary (Loc, 'R');
3719 Rev : constant Entity_Id := Make_Temporary (Loc, 'D');
3720 -- Formal parameters of procedure
3722 Proc_Name : constant Entity_Id :=
3723 Make_Defining_Identifier (Loc,
3724 Chars => Make_TSS_Name (Typ, TSS_Slice_Assign));
3726 Lnn : constant Entity_Id := Make_Temporary (Loc, 'L');
3727 Rnn : constant Entity_Id := Make_Temporary (Loc, 'R');
3728 -- Subscripts for left and right sides
3730 Decls : List_Id;
3731 Loops : Node_Id;
3732 Stats : List_Id;
3734 begin
3735 -- Build declarations for indexes
3737 Decls := New_List;
3739 Append_To (Decls,
3740 Make_Object_Declaration (Loc,
3741 Defining_Identifier => Lnn,
3742 Object_Definition =>
3743 New_Occurrence_Of (Index, Loc)));
3745 Append_To (Decls,
3746 Make_Object_Declaration (Loc,
3747 Defining_Identifier => Rnn,
3748 Object_Definition =>
3749 New_Occurrence_Of (Index, Loc)));
3751 Stats := New_List;
3753 -- Build test for empty slice case
3755 Append_To (Stats,
3756 Make_If_Statement (Loc,
3757 Condition =>
3758 Make_Op_Lt (Loc,
3759 Left_Opnd => New_Occurrence_Of (Left_Hi, Loc),
3760 Right_Opnd => New_Occurrence_Of (Left_Lo, Loc)),
3761 Then_Statements => New_List (Make_Simple_Return_Statement (Loc))));
3763 -- Build initializations for indexes
3765 declare
3766 F_Init : constant List_Id := New_List;
3767 B_Init : constant List_Id := New_List;
3769 begin
3770 Append_To (F_Init,
3771 Make_Assignment_Statement (Loc,
3772 Name => New_Occurrence_Of (Lnn, Loc),
3773 Expression => New_Occurrence_Of (Left_Lo, Loc)));
3775 Append_To (F_Init,
3776 Make_Assignment_Statement (Loc,
3777 Name => New_Occurrence_Of (Rnn, Loc),
3778 Expression => New_Occurrence_Of (Right_Lo, Loc)));
3780 Append_To (B_Init,
3781 Make_Assignment_Statement (Loc,
3782 Name => New_Occurrence_Of (Lnn, Loc),
3783 Expression => New_Occurrence_Of (Left_Hi, Loc)));
3785 Append_To (B_Init,
3786 Make_Assignment_Statement (Loc,
3787 Name => New_Occurrence_Of (Rnn, Loc),
3788 Expression => New_Occurrence_Of (Right_Hi, Loc)));
3790 Append_To (Stats,
3791 Make_If_Statement (Loc,
3792 Condition => New_Occurrence_Of (Rev, Loc),
3793 Then_Statements => B_Init,
3794 Else_Statements => F_Init));
3795 end;
3797 -- Now construct the assignment statement
3799 Loops :=
3800 Make_Loop_Statement (Loc,
3801 Statements => New_List (
3802 Make_Assignment_Statement (Loc,
3803 Name =>
3804 Make_Indexed_Component (Loc,
3805 Prefix => New_Occurrence_Of (Larray, Loc),
3806 Expressions => New_List (New_Occurrence_Of (Lnn, Loc))),
3807 Expression =>
3808 Make_Indexed_Component (Loc,
3809 Prefix => New_Occurrence_Of (Rarray, Loc),
3810 Expressions => New_List (New_Occurrence_Of (Rnn, Loc))))),
3811 End_Label => Empty);
3813 -- Build the exit condition and increment/decrement statements
3815 declare
3816 F_Ass : constant List_Id := New_List;
3817 B_Ass : constant List_Id := New_List;
3819 begin
3820 Append_To (F_Ass,
3821 Make_Exit_Statement (Loc,
3822 Condition =>
3823 Make_Op_Eq (Loc,
3824 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
3825 Right_Opnd => New_Occurrence_Of (Left_Hi, Loc))));
3827 Append_To (F_Ass,
3828 Make_Assignment_Statement (Loc,
3829 Name => New_Occurrence_Of (Lnn, Loc),
3830 Expression =>
3831 Make_Attribute_Reference (Loc,
3832 Prefix =>
3833 New_Occurrence_Of (Index, Loc),
3834 Attribute_Name => Name_Succ,
3835 Expressions => New_List (
3836 New_Occurrence_Of (Lnn, Loc)))));
3838 Append_To (F_Ass,
3839 Make_Assignment_Statement (Loc,
3840 Name => New_Occurrence_Of (Rnn, Loc),
3841 Expression =>
3842 Make_Attribute_Reference (Loc,
3843 Prefix =>
3844 New_Occurrence_Of (Index, Loc),
3845 Attribute_Name => Name_Succ,
3846 Expressions => New_List (
3847 New_Occurrence_Of (Rnn, Loc)))));
3849 Append_To (B_Ass,
3850 Make_Exit_Statement (Loc,
3851 Condition =>
3852 Make_Op_Eq (Loc,
3853 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
3854 Right_Opnd => New_Occurrence_Of (Left_Lo, Loc))));
3856 Append_To (B_Ass,
3857 Make_Assignment_Statement (Loc,
3858 Name => New_Occurrence_Of (Lnn, Loc),
3859 Expression =>
3860 Make_Attribute_Reference (Loc,
3861 Prefix =>
3862 New_Occurrence_Of (Index, Loc),
3863 Attribute_Name => Name_Pred,
3864 Expressions => New_List (
3865 New_Occurrence_Of (Lnn, Loc)))));
3867 Append_To (B_Ass,
3868 Make_Assignment_Statement (Loc,
3869 Name => New_Occurrence_Of (Rnn, Loc),
3870 Expression =>
3871 Make_Attribute_Reference (Loc,
3872 Prefix =>
3873 New_Occurrence_Of (Index, Loc),
3874 Attribute_Name => Name_Pred,
3875 Expressions => New_List (
3876 New_Occurrence_Of (Rnn, Loc)))));
3878 Append_To (Statements (Loops),
3879 Make_If_Statement (Loc,
3880 Condition => New_Occurrence_Of (Rev, Loc),
3881 Then_Statements => B_Ass,
3882 Else_Statements => F_Ass));
3883 end;
3885 Append_To (Stats, Loops);
3887 declare
3888 Spec : Node_Id;
3889 Formals : List_Id := New_List;
3891 begin
3892 Formals := New_List (
3893 Make_Parameter_Specification (Loc,
3894 Defining_Identifier => Larray,
3895 Out_Present => True,
3896 Parameter_Type =>
3897 New_Occurrence_Of (Base_Type (Typ), Loc)),
3899 Make_Parameter_Specification (Loc,
3900 Defining_Identifier => Rarray,
3901 Parameter_Type =>
3902 New_Occurrence_Of (Base_Type (Typ), Loc)),
3904 Make_Parameter_Specification (Loc,
3905 Defining_Identifier => Left_Lo,
3906 Parameter_Type =>
3907 New_Occurrence_Of (Index, Loc)),
3909 Make_Parameter_Specification (Loc,
3910 Defining_Identifier => Left_Hi,
3911 Parameter_Type =>
3912 New_Occurrence_Of (Index, Loc)),
3914 Make_Parameter_Specification (Loc,
3915 Defining_Identifier => Right_Lo,
3916 Parameter_Type =>
3917 New_Occurrence_Of (Index, Loc)),
3919 Make_Parameter_Specification (Loc,
3920 Defining_Identifier => Right_Hi,
3921 Parameter_Type =>
3922 New_Occurrence_Of (Index, Loc)));
3924 Append_To (Formals,
3925 Make_Parameter_Specification (Loc,
3926 Defining_Identifier => Rev,
3927 Parameter_Type =>
3928 New_Occurrence_Of (Standard_Boolean, Loc)));
3930 Spec :=
3931 Make_Procedure_Specification (Loc,
3932 Defining_Unit_Name => Proc_Name,
3933 Parameter_Specifications => Formals);
3935 Discard_Node (
3936 Make_Subprogram_Body (Loc,
3937 Specification => Spec,
3938 Declarations => Decls,
3939 Handled_Statement_Sequence =>
3940 Make_Handled_Sequence_Of_Statements (Loc,
3941 Statements => Stats)));
3942 end;
3944 Set_TSS (Typ, Proc_Name);
3945 Set_Is_Pure (Proc_Name);
3946 end Build_Slice_Assignment;
3948 -----------------------------
3949 -- Build_Untagged_Equality --
3950 -----------------------------
3952 procedure Build_Untagged_Equality (Typ : Entity_Id) is
3953 Build_Eq : Boolean;
3954 Comp : Entity_Id;
3955 Decl : Node_Id;
3956 Op : Entity_Id;
3957 Prim : Elmt_Id;
3958 Eq_Op : Entity_Id;
3960 function User_Defined_Eq (T : Entity_Id) return Entity_Id;
3961 -- Check whether the type T has a user-defined primitive equality. If so
3962 -- return it, else return Empty. If true for a component of Typ, we have
3963 -- to build the primitive equality for it.
3965 ---------------------
3966 -- User_Defined_Eq --
3967 ---------------------
3969 function User_Defined_Eq (T : Entity_Id) return Entity_Id is
3970 Prim : Elmt_Id;
3971 Op : Entity_Id;
3973 begin
3974 Op := TSS (T, TSS_Composite_Equality);
3976 if Present (Op) then
3977 return Op;
3978 end if;
3980 Prim := First_Elmt (Collect_Primitive_Operations (T));
3981 while Present (Prim) loop
3982 Op := Node (Prim);
3984 if Chars (Op) = Name_Op_Eq
3985 and then Etype (Op) = Standard_Boolean
3986 and then Etype (First_Formal (Op)) = T
3987 and then Etype (Next_Formal (First_Formal (Op))) = T
3988 then
3989 return Op;
3990 end if;
3992 Next_Elmt (Prim);
3993 end loop;
3995 return Empty;
3996 end User_Defined_Eq;
3998 -- Start of processing for Build_Untagged_Equality
4000 begin
4001 -- If a record component has a primitive equality operation, we must
4002 -- build the corresponding one for the current type.
4004 Build_Eq := False;
4005 Comp := First_Component (Typ);
4006 while Present (Comp) loop
4007 if Is_Record_Type (Etype (Comp))
4008 and then Present (User_Defined_Eq (Etype (Comp)))
4009 then
4010 Build_Eq := True;
4011 end if;
4013 Next_Component (Comp);
4014 end loop;
4016 -- If there is a user-defined equality for the type, we do not create
4017 -- the implicit one.
4019 Prim := First_Elmt (Collect_Primitive_Operations (Typ));
4020 Eq_Op := Empty;
4021 while Present (Prim) loop
4022 if Chars (Node (Prim)) = Name_Op_Eq
4023 and then Comes_From_Source (Node (Prim))
4025 -- Don't we also need to check formal types and return type as in
4026 -- User_Defined_Eq above???
4028 then
4029 Eq_Op := Node (Prim);
4030 Build_Eq := False;
4031 exit;
4032 end if;
4034 Next_Elmt (Prim);
4035 end loop;
4037 -- If the type is derived, inherit the operation, if present, from the
4038 -- parent type. It may have been declared after the type derivation. If
4039 -- the parent type itself is derived, it may have inherited an operation
4040 -- that has itself been overridden, so update its alias and related
4041 -- flags. Ditto for inequality.
4043 if No (Eq_Op) and then Is_Derived_Type (Typ) then
4044 Prim := First_Elmt (Collect_Primitive_Operations (Etype (Typ)));
4045 while Present (Prim) loop
4046 if Chars (Node (Prim)) = Name_Op_Eq then
4047 Copy_TSS (Node (Prim), Typ);
4048 Build_Eq := False;
4050 declare
4051 Op : constant Entity_Id := User_Defined_Eq (Typ);
4052 Eq_Op : constant Entity_Id := Node (Prim);
4053 NE_Op : constant Entity_Id := Next_Entity (Eq_Op);
4055 begin
4056 if Present (Op) then
4057 Set_Alias (Op, Eq_Op);
4058 Set_Is_Abstract_Subprogram
4059 (Op, Is_Abstract_Subprogram (Eq_Op));
4061 if Chars (Next_Entity (Op)) = Name_Op_Ne then
4062 Set_Is_Abstract_Subprogram
4063 (Next_Entity (Op), Is_Abstract_Subprogram (NE_Op));
4064 end if;
4065 end if;
4066 end;
4068 exit;
4069 end if;
4071 Next_Elmt (Prim);
4072 end loop;
4073 end if;
4075 -- If not inherited and not user-defined, build body as for a type with
4076 -- tagged components.
4078 if Build_Eq then
4079 Decl :=
4080 Make_Eq_Body (Typ, Make_TSS_Name (Typ, TSS_Composite_Equality));
4081 Op := Defining_Entity (Decl);
4082 Set_TSS (Typ, Op);
4083 Set_Is_Pure (Op);
4085 if Is_Library_Level_Entity (Typ) then
4086 Set_Is_Public (Op);
4087 end if;
4088 end if;
4089 end Build_Untagged_Equality;
4091 -----------------------------------
4092 -- Build_Variant_Record_Equality --
4093 -----------------------------------
4095 -- Generates:
4097 -- function _Equality (X, Y : T) return Boolean is
4098 -- begin
4099 -- -- Compare discriminants
4101 -- if X.D1 /= Y.D1 or else X.D2 /= Y.D2 or else ... then
4102 -- return False;
4103 -- end if;
4105 -- -- Compare components
4107 -- if X.C1 /= Y.C1 or else X.C2 /= Y.C2 or else ... then
4108 -- return False;
4109 -- end if;
4111 -- -- Compare variant part
4113 -- case X.D1 is
4114 -- when V1 =>
4115 -- if X.C2 /= Y.C2 or else X.C3 /= Y.C3 or else ... then
4116 -- return False;
4117 -- end if;
4118 -- ...
4119 -- when Vn =>
4120 -- if X.Cn /= Y.Cn or else ... then
4121 -- return False;
4122 -- end if;
4123 -- end case;
4125 -- return True;
4126 -- end _Equality;
4128 procedure Build_Variant_Record_Equality (Typ : Entity_Id) is
4129 Loc : constant Source_Ptr := Sloc (Typ);
4131 F : constant Entity_Id :=
4132 Make_Defining_Identifier (Loc,
4133 Chars => Make_TSS_Name (Typ, TSS_Composite_Equality));
4135 X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_X);
4136 Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_Y);
4138 Def : constant Node_Id := Parent (Typ);
4139 Comps : constant Node_Id := Component_List (Type_Definition (Def));
4140 Stmts : constant List_Id := New_List;
4141 Pspecs : constant List_Id := New_List;
4143 begin
4144 -- If we have a variant record with restriction No_Implicit_Conditionals
4145 -- in effect, then we skip building the procedure. This is safe because
4146 -- if we can see the restriction, so can any caller, calls to equality
4147 -- test routines are not allowed for variant records if this restriction
4148 -- is active.
4150 if Restriction_Active (No_Implicit_Conditionals) then
4151 return;
4152 end if;
4154 -- Derived Unchecked_Union types no longer inherit the equality function
4155 -- of their parent.
4157 if Is_Derived_Type (Typ)
4158 and then not Is_Unchecked_Union (Typ)
4159 and then not Has_New_Non_Standard_Rep (Typ)
4160 then
4161 declare
4162 Parent_Eq : constant Entity_Id :=
4163 TSS (Root_Type (Typ), TSS_Composite_Equality);
4164 begin
4165 if Present (Parent_Eq) then
4166 Copy_TSS (Parent_Eq, Typ);
4167 return;
4168 end if;
4169 end;
4170 end if;
4172 Discard_Node (
4173 Make_Subprogram_Body (Loc,
4174 Specification =>
4175 Make_Function_Specification (Loc,
4176 Defining_Unit_Name => F,
4177 Parameter_Specifications => Pspecs,
4178 Result_Definition => New_Occurrence_Of (Standard_Boolean, Loc)),
4179 Declarations => New_List,
4180 Handled_Statement_Sequence =>
4181 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts)));
4183 Append_To (Pspecs,
4184 Make_Parameter_Specification (Loc,
4185 Defining_Identifier => X,
4186 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
4188 Append_To (Pspecs,
4189 Make_Parameter_Specification (Loc,
4190 Defining_Identifier => Y,
4191 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
4193 -- Unchecked_Unions require additional machinery to support equality.
4194 -- Two extra parameters (A and B) are added to the equality function
4195 -- parameter list for each discriminant of the type, in order to
4196 -- capture the inferred values of the discriminants in equality calls.
4197 -- The names of the parameters match the names of the corresponding
4198 -- discriminant, with an added suffix.
4200 if Is_Unchecked_Union (Typ) then
4201 declare
4202 Discr : Entity_Id;
4203 Discr_Type : Entity_Id;
4204 A, B : Entity_Id;
4205 New_Discrs : Elist_Id;
4207 begin
4208 New_Discrs := New_Elmt_List;
4210 Discr := First_Discriminant (Typ);
4211 while Present (Discr) loop
4212 Discr_Type := Etype (Discr);
4213 A := Make_Defining_Identifier (Loc,
4214 Chars => New_External_Name (Chars (Discr), 'A'));
4216 B := Make_Defining_Identifier (Loc,
4217 Chars => New_External_Name (Chars (Discr), 'B'));
4219 -- Add new parameters to the parameter list
4221 Append_To (Pspecs,
4222 Make_Parameter_Specification (Loc,
4223 Defining_Identifier => A,
4224 Parameter_Type =>
4225 New_Occurrence_Of (Discr_Type, Loc)));
4227 Append_To (Pspecs,
4228 Make_Parameter_Specification (Loc,
4229 Defining_Identifier => B,
4230 Parameter_Type =>
4231 New_Occurrence_Of (Discr_Type, Loc)));
4233 Append_Elmt (A, New_Discrs);
4235 -- Generate the following code to compare each of the inferred
4236 -- discriminants:
4238 -- if a /= b then
4239 -- return False;
4240 -- end if;
4242 Append_To (Stmts,
4243 Make_If_Statement (Loc,
4244 Condition =>
4245 Make_Op_Ne (Loc,
4246 Left_Opnd => New_Occurrence_Of (A, Loc),
4247 Right_Opnd => New_Occurrence_Of (B, Loc)),
4248 Then_Statements => New_List (
4249 Make_Simple_Return_Statement (Loc,
4250 Expression =>
4251 New_Occurrence_Of (Standard_False, Loc)))));
4252 Next_Discriminant (Discr);
4253 end loop;
4255 -- Generate component-by-component comparison. Note that we must
4256 -- propagate the inferred discriminants formals to act as
4257 -- the case statement switch. Their value is added when an
4258 -- equality call on unchecked unions is expanded.
4260 Append_List_To (Stmts, Make_Eq_Case (Typ, Comps, New_Discrs));
4261 end;
4263 -- Normal case (not unchecked union)
4265 else
4266 Append_To (Stmts,
4267 Make_Eq_If (Typ, Discriminant_Specifications (Def)));
4268 Append_List_To (Stmts, Make_Eq_Case (Typ, Comps));
4269 end if;
4271 Append_To (Stmts,
4272 Make_Simple_Return_Statement (Loc,
4273 Expression => New_Occurrence_Of (Standard_True, Loc)));
4275 Set_TSS (Typ, F);
4276 Set_Is_Pure (F);
4278 if not Debug_Generated_Code then
4279 Set_Debug_Info_Off (F);
4280 end if;
4281 end Build_Variant_Record_Equality;
4283 -----------------------------
4284 -- Check_Stream_Attributes --
4285 -----------------------------
4287 procedure Check_Stream_Attributes (Typ : Entity_Id) is
4288 Comp : Entity_Id;
4289 Par_Read : constant Boolean :=
4290 Stream_Attribute_Available (Typ, TSS_Stream_Read)
4291 and then not Has_Specified_Stream_Read (Typ);
4292 Par_Write : constant Boolean :=
4293 Stream_Attribute_Available (Typ, TSS_Stream_Write)
4294 and then not Has_Specified_Stream_Write (Typ);
4296 procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type);
4297 -- Check that Comp has a user-specified Nam stream attribute
4299 ----------------
4300 -- Check_Attr --
4301 ----------------
4303 procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type) is
4304 begin
4305 if not Stream_Attribute_Available (Etype (Comp), TSS_Nam) then
4306 Error_Msg_Name_1 := Nam;
4307 Error_Msg_N
4308 ("|component& in limited extension must have% attribute", Comp);
4309 end if;
4310 end Check_Attr;
4312 -- Start of processing for Check_Stream_Attributes
4314 begin
4315 if Par_Read or else Par_Write then
4316 Comp := First_Component (Typ);
4317 while Present (Comp) loop
4318 if Comes_From_Source (Comp)
4319 and then Original_Record_Component (Comp) = Comp
4320 and then Is_Limited_Type (Etype (Comp))
4321 then
4322 if Par_Read then
4323 Check_Attr (Name_Read, TSS_Stream_Read);
4324 end if;
4326 if Par_Write then
4327 Check_Attr (Name_Write, TSS_Stream_Write);
4328 end if;
4329 end if;
4331 Next_Component (Comp);
4332 end loop;
4333 end if;
4334 end Check_Stream_Attributes;
4336 ----------------------
4337 -- Clean_Task_Names --
4338 ----------------------
4340 procedure Clean_Task_Names
4341 (Typ : Entity_Id;
4342 Proc_Id : Entity_Id)
4344 begin
4345 if Has_Task (Typ)
4346 and then not Restriction_Active (No_Implicit_Heap_Allocations)
4347 and then not Global_Discard_Names
4348 and then Tagged_Type_Expansion
4349 then
4350 Set_Uses_Sec_Stack (Proc_Id);
4351 end if;
4352 end Clean_Task_Names;
4354 ------------------------------
4355 -- Expand_Freeze_Array_Type --
4356 ------------------------------
4358 procedure Expand_Freeze_Array_Type (N : Node_Id) is
4359 Typ : constant Entity_Id := Entity (N);
4360 Base : constant Entity_Id := Base_Type (Typ);
4361 Comp_Typ : constant Entity_Id := Component_Type (Typ);
4363 begin
4364 if not Is_Bit_Packed_Array (Typ) then
4366 -- If the component contains tasks, so does the array type. This may
4367 -- not be indicated in the array type because the component may have
4368 -- been a private type at the point of definition. Same if component
4369 -- type is controlled or contains protected objects.
4371 Propagate_Concurrent_Flags (Base, Comp_Typ);
4372 Set_Has_Controlled_Component
4373 (Base, Has_Controlled_Component (Comp_Typ)
4374 or else Is_Controlled (Comp_Typ));
4376 if No (Init_Proc (Base)) then
4378 -- If this is an anonymous array created for a declaration with
4379 -- an initial value, its init_proc will never be called. The
4380 -- initial value itself may have been expanded into assignments,
4381 -- in which case the object declaration is carries the
4382 -- No_Initialization flag.
4384 if Is_Itype (Base)
4385 and then Nkind (Associated_Node_For_Itype (Base)) =
4386 N_Object_Declaration
4387 and then
4388 (Present (Expression (Associated_Node_For_Itype (Base)))
4389 or else No_Initialization (Associated_Node_For_Itype (Base)))
4390 then
4391 null;
4393 -- We do not need an init proc for string or wide [wide] string,
4394 -- since the only time these need initialization in normalize or
4395 -- initialize scalars mode, and these types are treated specially
4396 -- and do not need initialization procedures.
4398 elsif Is_Standard_String_Type (Base) then
4399 null;
4401 -- Otherwise we have to build an init proc for the subtype
4403 else
4404 Build_Array_Init_Proc (Base, N);
4405 end if;
4406 end if;
4408 if Typ = Base and then Has_Controlled_Component (Base) then
4409 Build_Controlling_Procs (Base);
4411 if not Is_Limited_Type (Comp_Typ)
4412 and then Number_Dimensions (Typ) = 1
4413 then
4414 Build_Slice_Assignment (Typ);
4415 end if;
4416 end if;
4418 -- For packed case, default initialization, except if the component type
4419 -- is itself a packed structure with an initialization procedure, or
4420 -- initialize/normalize scalars active, and we have a base type, or the
4421 -- type is public, because in that case a client might specify
4422 -- Normalize_Scalars and there better be a public Init_Proc for it.
4424 elsif (Present (Init_Proc (Component_Type (Base)))
4425 and then No (Base_Init_Proc (Base)))
4426 or else (Init_Or_Norm_Scalars and then Base = Typ)
4427 or else Is_Public (Typ)
4428 then
4429 Build_Array_Init_Proc (Base, N);
4430 end if;
4431 end Expand_Freeze_Array_Type;
4433 -----------------------------------
4434 -- Expand_Freeze_Class_Wide_Type --
4435 -----------------------------------
4437 procedure Expand_Freeze_Class_Wide_Type (N : Node_Id) is
4438 function Is_C_Derivation (Typ : Entity_Id) return Boolean;
4439 -- Given a type, determine whether it is derived from a C or C++ root
4441 ---------------------
4442 -- Is_C_Derivation --
4443 ---------------------
4445 function Is_C_Derivation (Typ : Entity_Id) return Boolean is
4446 T : Entity_Id;
4448 begin
4449 T := Typ;
4450 loop
4451 if Is_CPP_Class (T)
4452 or else Convention (T) = Convention_C
4453 or else Convention (T) = Convention_CPP
4454 then
4455 return True;
4456 end if;
4458 exit when T = Etype (T);
4460 T := Etype (T);
4461 end loop;
4463 return False;
4464 end Is_C_Derivation;
4466 -- Local variables
4468 Typ : constant Entity_Id := Entity (N);
4469 Root : constant Entity_Id := Root_Type (Typ);
4471 -- Start of processing for Expand_Freeze_Class_Wide_Type
4473 begin
4474 -- Certain run-time configurations and targets do not provide support
4475 -- for controlled types.
4477 if Restriction_Active (No_Finalization) then
4478 return;
4480 -- Do not create TSS routine Finalize_Address when dispatching calls are
4481 -- disabled since the core of the routine is a dispatching call.
4483 elsif Restriction_Active (No_Dispatching_Calls) then
4484 return;
4486 -- Do not create TSS routine Finalize_Address for concurrent class-wide
4487 -- types. Ignore C, C++, CIL and Java types since it is assumed that the
4488 -- non-Ada side will handle their destruction.
4490 elsif Is_Concurrent_Type (Root)
4491 or else Is_C_Derivation (Root)
4492 or else Convention (Typ) = Convention_CPP
4493 then
4494 return;
4496 -- Do not create TSS routine Finalize_Address when compiling in CodePeer
4497 -- mode since the routine contains an Unchecked_Conversion.
4499 elsif CodePeer_Mode then
4500 return;
4501 end if;
4503 -- Create the body of TSS primitive Finalize_Address. This automatically
4504 -- sets the TSS entry for the class-wide type.
4506 Make_Finalize_Address_Body (Typ);
4507 end Expand_Freeze_Class_Wide_Type;
4509 ------------------------------------
4510 -- Expand_Freeze_Enumeration_Type --
4511 ------------------------------------
4513 procedure Expand_Freeze_Enumeration_Type (N : Node_Id) is
4514 Typ : constant Entity_Id := Entity (N);
4515 Loc : constant Source_Ptr := Sloc (Typ);
4517 Arr : Entity_Id;
4518 Ent : Entity_Id;
4519 Fent : Entity_Id;
4520 Is_Contiguous : Boolean;
4521 Ityp : Entity_Id;
4522 Last_Repval : Uint;
4523 Lst : List_Id;
4524 Num : Nat;
4525 Pos_Expr : Node_Id;
4527 Func : Entity_Id;
4528 pragma Warnings (Off, Func);
4530 begin
4531 -- Various optimizations possible if given representation is contiguous
4533 Is_Contiguous := True;
4535 Ent := First_Literal (Typ);
4536 Last_Repval := Enumeration_Rep (Ent);
4538 Next_Literal (Ent);
4539 while Present (Ent) loop
4540 if Enumeration_Rep (Ent) - Last_Repval /= 1 then
4541 Is_Contiguous := False;
4542 exit;
4543 else
4544 Last_Repval := Enumeration_Rep (Ent);
4545 end if;
4547 Next_Literal (Ent);
4548 end loop;
4550 if Is_Contiguous then
4551 Set_Has_Contiguous_Rep (Typ);
4552 Ent := First_Literal (Typ);
4553 Num := 1;
4554 Lst := New_List (New_Occurrence_Of (Ent, Sloc (Ent)));
4556 else
4557 -- Build list of literal references
4559 Lst := New_List;
4560 Num := 0;
4562 Ent := First_Literal (Typ);
4563 while Present (Ent) loop
4564 Append_To (Lst, New_Occurrence_Of (Ent, Sloc (Ent)));
4565 Num := Num + 1;
4566 Next_Literal (Ent);
4567 end loop;
4568 end if;
4570 -- Now build an array declaration
4572 -- typA : array (Natural range 0 .. num - 1) of ctype :=
4573 -- (v, v, v, v, v, ....)
4575 -- where ctype is the corresponding integer type. If the representation
4576 -- is contiguous, we only keep the first literal, which provides the
4577 -- offset for Pos_To_Rep computations.
4579 Arr :=
4580 Make_Defining_Identifier (Loc,
4581 Chars => New_External_Name (Chars (Typ), 'A'));
4583 Append_Freeze_Action (Typ,
4584 Make_Object_Declaration (Loc,
4585 Defining_Identifier => Arr,
4586 Constant_Present => True,
4588 Object_Definition =>
4589 Make_Constrained_Array_Definition (Loc,
4590 Discrete_Subtype_Definitions => New_List (
4591 Make_Subtype_Indication (Loc,
4592 Subtype_Mark => New_Occurrence_Of (Standard_Natural, Loc),
4593 Constraint =>
4594 Make_Range_Constraint (Loc,
4595 Range_Expression =>
4596 Make_Range (Loc,
4597 Low_Bound =>
4598 Make_Integer_Literal (Loc, 0),
4599 High_Bound =>
4600 Make_Integer_Literal (Loc, Num - 1))))),
4602 Component_Definition =>
4603 Make_Component_Definition (Loc,
4604 Aliased_Present => False,
4605 Subtype_Indication => New_Occurrence_Of (Typ, Loc))),
4607 Expression =>
4608 Make_Aggregate (Loc,
4609 Expressions => Lst)));
4611 Set_Enum_Pos_To_Rep (Typ, Arr);
4613 -- Now we build the function that converts representation values to
4614 -- position values. This function has the form:
4616 -- function _Rep_To_Pos (A : etype; F : Boolean) return Integer is
4617 -- begin
4618 -- case ityp!(A) is
4619 -- when enum-lit'Enum_Rep => return posval;
4620 -- when enum-lit'Enum_Rep => return posval;
4621 -- ...
4622 -- when others =>
4623 -- [raise Constraint_Error when F "invalid data"]
4624 -- return -1;
4625 -- end case;
4626 -- end;
4628 -- Note: the F parameter determines whether the others case (no valid
4629 -- representation) raises Constraint_Error or returns a unique value
4630 -- of minus one. The latter case is used, e.g. in 'Valid code.
4632 -- Note: the reason we use Enum_Rep values in the case here is to avoid
4633 -- the code generator making inappropriate assumptions about the range
4634 -- of the values in the case where the value is invalid. ityp is a
4635 -- signed or unsigned integer type of appropriate width.
4637 -- Note: if exceptions are not supported, then we suppress the raise
4638 -- and return -1 unconditionally (this is an erroneous program in any
4639 -- case and there is no obligation to raise Constraint_Error here). We
4640 -- also do this if pragma Restrictions (No_Exceptions) is active.
4642 -- Is this right??? What about No_Exception_Propagation???
4644 -- Representations are signed
4646 if Enumeration_Rep (First_Literal (Typ)) < 0 then
4648 -- The underlying type is signed. Reset the Is_Unsigned_Type
4649 -- explicitly, because it might have been inherited from
4650 -- parent type.
4652 Set_Is_Unsigned_Type (Typ, False);
4654 if Esize (Typ) <= Standard_Integer_Size then
4655 Ityp := Standard_Integer;
4656 else
4657 Ityp := Universal_Integer;
4658 end if;
4660 -- Representations are unsigned
4662 else
4663 if Esize (Typ) <= Standard_Integer_Size then
4664 Ityp := RTE (RE_Unsigned);
4665 else
4666 Ityp := RTE (RE_Long_Long_Unsigned);
4667 end if;
4668 end if;
4670 -- The body of the function is a case statement. First collect case
4671 -- alternatives, or optimize the contiguous case.
4673 Lst := New_List;
4675 -- If representation is contiguous, Pos is computed by subtracting
4676 -- the representation of the first literal.
4678 if Is_Contiguous then
4679 Ent := First_Literal (Typ);
4681 if Enumeration_Rep (Ent) = Last_Repval then
4683 -- Another special case: for a single literal, Pos is zero
4685 Pos_Expr := Make_Integer_Literal (Loc, Uint_0);
4687 else
4688 Pos_Expr :=
4689 Convert_To (Standard_Integer,
4690 Make_Op_Subtract (Loc,
4691 Left_Opnd =>
4692 Unchecked_Convert_To
4693 (Ityp, Make_Identifier (Loc, Name_uA)),
4694 Right_Opnd =>
4695 Make_Integer_Literal (Loc,
4696 Intval => Enumeration_Rep (First_Literal (Typ)))));
4697 end if;
4699 Append_To (Lst,
4700 Make_Case_Statement_Alternative (Loc,
4701 Discrete_Choices => New_List (
4702 Make_Range (Sloc (Enumeration_Rep_Expr (Ent)),
4703 Low_Bound =>
4704 Make_Integer_Literal (Loc,
4705 Intval => Enumeration_Rep (Ent)),
4706 High_Bound =>
4707 Make_Integer_Literal (Loc, Intval => Last_Repval))),
4709 Statements => New_List (
4710 Make_Simple_Return_Statement (Loc,
4711 Expression => Pos_Expr))));
4713 else
4714 Ent := First_Literal (Typ);
4715 while Present (Ent) loop
4716 Append_To (Lst,
4717 Make_Case_Statement_Alternative (Loc,
4718 Discrete_Choices => New_List (
4719 Make_Integer_Literal (Sloc (Enumeration_Rep_Expr (Ent)),
4720 Intval => Enumeration_Rep (Ent))),
4722 Statements => New_List (
4723 Make_Simple_Return_Statement (Loc,
4724 Expression =>
4725 Make_Integer_Literal (Loc,
4726 Intval => Enumeration_Pos (Ent))))));
4728 Next_Literal (Ent);
4729 end loop;
4730 end if;
4732 -- In normal mode, add the others clause with the test.
4733 -- If Predicates_Ignored is True, validity checks do not apply to
4734 -- the subtype.
4736 if not No_Exception_Handlers_Set
4737 and then not Predicates_Ignored (Typ)
4738 then
4739 Append_To (Lst,
4740 Make_Case_Statement_Alternative (Loc,
4741 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
4742 Statements => New_List (
4743 Make_Raise_Constraint_Error (Loc,
4744 Condition => Make_Identifier (Loc, Name_uF),
4745 Reason => CE_Invalid_Data),
4746 Make_Simple_Return_Statement (Loc,
4747 Expression => Make_Integer_Literal (Loc, -1)))));
4749 -- If either of the restrictions No_Exceptions_Handlers/Propagation is
4750 -- active then return -1 (we cannot usefully raise Constraint_Error in
4751 -- this case). See description above for further details.
4753 else
4754 Append_To (Lst,
4755 Make_Case_Statement_Alternative (Loc,
4756 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
4757 Statements => New_List (
4758 Make_Simple_Return_Statement (Loc,
4759 Expression => Make_Integer_Literal (Loc, -1)))));
4760 end if;
4762 -- Now we can build the function body
4764 Fent :=
4765 Make_Defining_Identifier (Loc, Make_TSS_Name (Typ, TSS_Rep_To_Pos));
4767 Func :=
4768 Make_Subprogram_Body (Loc,
4769 Specification =>
4770 Make_Function_Specification (Loc,
4771 Defining_Unit_Name => Fent,
4772 Parameter_Specifications => New_List (
4773 Make_Parameter_Specification (Loc,
4774 Defining_Identifier =>
4775 Make_Defining_Identifier (Loc, Name_uA),
4776 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
4777 Make_Parameter_Specification (Loc,
4778 Defining_Identifier =>
4779 Make_Defining_Identifier (Loc, Name_uF),
4780 Parameter_Type =>
4781 New_Occurrence_Of (Standard_Boolean, Loc))),
4783 Result_Definition => New_Occurrence_Of (Standard_Integer, Loc)),
4785 Declarations => Empty_List,
4787 Handled_Statement_Sequence =>
4788 Make_Handled_Sequence_Of_Statements (Loc,
4789 Statements => New_List (
4790 Make_Case_Statement (Loc,
4791 Expression =>
4792 Unchecked_Convert_To
4793 (Ityp, Make_Identifier (Loc, Name_uA)),
4794 Alternatives => Lst))));
4796 Set_TSS (Typ, Fent);
4798 -- Set Pure flag (it will be reset if the current context is not Pure).
4799 -- We also pretend there was a pragma Pure_Function so that for purposes
4800 -- of optimization and constant-folding, we will consider the function
4801 -- Pure even if we are not in a Pure context).
4803 Set_Is_Pure (Fent);
4804 Set_Has_Pragma_Pure_Function (Fent);
4806 -- Unless we are in -gnatD mode, where we are debugging generated code,
4807 -- this is an internal entity for which we don't need debug info.
4809 if not Debug_Generated_Code then
4810 Set_Debug_Info_Off (Fent);
4811 end if;
4813 exception
4814 when RE_Not_Available =>
4815 return;
4816 end Expand_Freeze_Enumeration_Type;
4818 -------------------------------
4819 -- Expand_Freeze_Record_Type --
4820 -------------------------------
4822 procedure Expand_Freeze_Record_Type (N : Node_Id) is
4823 Typ : constant Node_Id := Entity (N);
4824 Typ_Decl : constant Node_Id := Parent (Typ);
4826 Comp : Entity_Id;
4827 Comp_Typ : Entity_Id;
4828 Predef_List : List_Id;
4830 Wrapper_Decl_List : List_Id := No_List;
4831 Wrapper_Body_List : List_Id := No_List;
4833 Renamed_Eq : Node_Id := Empty;
4834 -- Defining unit name for the predefined equality function in the case
4835 -- where the type has a primitive operation that is a renaming of
4836 -- predefined equality (but only if there is also an overriding
4837 -- user-defined equality function). Used to pass this entity from
4838 -- Make_Predefined_Primitive_Specs to Predefined_Primitive_Bodies.
4840 -- Start of processing for Expand_Freeze_Record_Type
4842 begin
4843 -- Build discriminant checking functions if not a derived type (for
4844 -- derived types that are not tagged types, always use the discriminant
4845 -- checking functions of the parent type). However, for untagged types
4846 -- the derivation may have taken place before the parent was frozen, so
4847 -- we copy explicitly the discriminant checking functions from the
4848 -- parent into the components of the derived type.
4850 if not Is_Derived_Type (Typ)
4851 or else Has_New_Non_Standard_Rep (Typ)
4852 or else Is_Tagged_Type (Typ)
4853 then
4854 Build_Discr_Checking_Funcs (Typ_Decl);
4856 elsif Is_Derived_Type (Typ)
4857 and then not Is_Tagged_Type (Typ)
4859 -- If we have a derived Unchecked_Union, we do not inherit the
4860 -- discriminant checking functions from the parent type since the
4861 -- discriminants are non existent.
4863 and then not Is_Unchecked_Union (Typ)
4864 and then Has_Discriminants (Typ)
4865 then
4866 declare
4867 Old_Comp : Entity_Id;
4869 begin
4870 Old_Comp :=
4871 First_Component (Base_Type (Underlying_Type (Etype (Typ))));
4872 Comp := First_Component (Typ);
4873 while Present (Comp) loop
4874 if Ekind (Comp) = E_Component
4875 and then Chars (Comp) = Chars (Old_Comp)
4876 then
4877 Set_Discriminant_Checking_Func
4878 (Comp, Discriminant_Checking_Func (Old_Comp));
4879 end if;
4881 Next_Component (Old_Comp);
4882 Next_Component (Comp);
4883 end loop;
4884 end;
4885 end if;
4887 if Is_Derived_Type (Typ)
4888 and then Is_Limited_Type (Typ)
4889 and then Is_Tagged_Type (Typ)
4890 then
4891 Check_Stream_Attributes (Typ);
4892 end if;
4894 -- Update task, protected, and controlled component flags, because some
4895 -- of the component types may have been private at the point of the
4896 -- record declaration. Detect anonymous access-to-controlled components.
4898 Comp := First_Component (Typ);
4899 while Present (Comp) loop
4900 Comp_Typ := Etype (Comp);
4902 Propagate_Concurrent_Flags (Typ, Comp_Typ);
4904 -- Do not set Has_Controlled_Component on a class-wide equivalent
4905 -- type. See Make_CW_Equivalent_Type.
4907 if not Is_Class_Wide_Equivalent_Type (Typ)
4908 and then
4909 (Has_Controlled_Component (Comp_Typ)
4910 or else (Chars (Comp) /= Name_uParent
4911 and then (Is_Controlled_Active (Comp_Typ))))
4912 then
4913 Set_Has_Controlled_Component (Typ);
4914 end if;
4916 Next_Component (Comp);
4917 end loop;
4919 -- Handle constructors of untagged CPP_Class types
4921 if not Is_Tagged_Type (Typ) and then Is_CPP_Class (Typ) then
4922 Set_CPP_Constructors (Typ);
4923 end if;
4925 -- Creation of the Dispatch Table. Note that a Dispatch Table is built
4926 -- for regular tagged types as well as for Ada types deriving from a C++
4927 -- Class, but not for tagged types directly corresponding to C++ classes
4928 -- In the later case we assume that it is created in the C++ side and we
4929 -- just use it.
4931 if Is_Tagged_Type (Typ) then
4933 -- Add the _Tag component
4935 if Underlying_Type (Etype (Typ)) = Typ then
4936 Expand_Tagged_Root (Typ);
4937 end if;
4939 if Is_CPP_Class (Typ) then
4940 Set_All_DT_Position (Typ);
4942 -- Create the tag entities with a minimum decoration
4944 if Tagged_Type_Expansion then
4945 Append_Freeze_Actions (Typ, Make_Tags (Typ));
4946 end if;
4948 Set_CPP_Constructors (Typ);
4950 else
4951 if not Building_Static_DT (Typ) then
4953 -- Usually inherited primitives are not delayed but the first
4954 -- Ada extension of a CPP_Class is an exception since the
4955 -- address of the inherited subprogram has to be inserted in
4956 -- the new Ada Dispatch Table and this is a freezing action.
4958 -- Similarly, if this is an inherited operation whose parent is
4959 -- not frozen yet, it is not in the DT of the parent, and we
4960 -- generate an explicit freeze node for the inherited operation
4961 -- so it is properly inserted in the DT of the current type.
4963 declare
4964 Elmt : Elmt_Id;
4965 Subp : Entity_Id;
4967 begin
4968 Elmt := First_Elmt (Primitive_Operations (Typ));
4969 while Present (Elmt) loop
4970 Subp := Node (Elmt);
4972 if Present (Alias (Subp)) then
4973 if Is_CPP_Class (Etype (Typ)) then
4974 Set_Has_Delayed_Freeze (Subp);
4976 elsif Has_Delayed_Freeze (Alias (Subp))
4977 and then not Is_Frozen (Alias (Subp))
4978 then
4979 Set_Is_Frozen (Subp, False);
4980 Set_Has_Delayed_Freeze (Subp);
4981 end if;
4982 end if;
4984 Next_Elmt (Elmt);
4985 end loop;
4986 end;
4987 end if;
4989 -- Unfreeze momentarily the type to add the predefined primitives
4990 -- operations. The reason we unfreeze is so that these predefined
4991 -- operations will indeed end up as primitive operations (which
4992 -- must be before the freeze point).
4994 Set_Is_Frozen (Typ, False);
4996 -- Do not add the spec of predefined primitives in case of
4997 -- CPP tagged type derivations that have convention CPP.
4999 if Is_CPP_Class (Root_Type (Typ))
5000 and then Convention (Typ) = Convention_CPP
5001 then
5002 null;
5004 -- Do not add the spec of the predefined primitives if we are
5005 -- compiling under restriction No_Dispatching_Calls.
5007 elsif not Restriction_Active (No_Dispatching_Calls) then
5008 Make_Predefined_Primitive_Specs (Typ, Predef_List, Renamed_Eq);
5009 Insert_List_Before_And_Analyze (N, Predef_List);
5010 end if;
5012 -- Ada 2005 (AI-391): For a nonabstract null extension, create
5013 -- wrapper functions for each nonoverridden inherited function
5014 -- with a controlling result of the type. The wrapper for such
5015 -- a function returns an extension aggregate that invokes the
5016 -- parent function.
5018 if Ada_Version >= Ada_2005
5019 and then not Is_Abstract_Type (Typ)
5020 and then Is_Null_Extension (Typ)
5021 then
5022 Make_Controlling_Function_Wrappers
5023 (Typ, Wrapper_Decl_List, Wrapper_Body_List);
5024 Insert_List_Before_And_Analyze (N, Wrapper_Decl_List);
5025 end if;
5027 -- Ada 2005 (AI-251): For a nonabstract type extension, build
5028 -- null procedure declarations for each set of homographic null
5029 -- procedures that are inherited from interface types but not
5030 -- overridden. This is done to ensure that the dispatch table
5031 -- entry associated with such null primitives are properly filled.
5033 if Ada_Version >= Ada_2005
5034 and then Etype (Typ) /= Typ
5035 and then not Is_Abstract_Type (Typ)
5036 and then Has_Interfaces (Typ)
5037 then
5038 Insert_Actions (N, Make_Null_Procedure_Specs (Typ));
5039 end if;
5041 Set_Is_Frozen (Typ);
5043 if not Is_Derived_Type (Typ)
5044 or else Is_Tagged_Type (Etype (Typ))
5045 then
5046 Set_All_DT_Position (Typ);
5048 -- If this is a type derived from an untagged private type whose
5049 -- full view is tagged, the type is marked tagged for layout
5050 -- reasons, but it has no dispatch table.
5052 elsif Is_Derived_Type (Typ)
5053 and then Is_Private_Type (Etype (Typ))
5054 and then not Is_Tagged_Type (Etype (Typ))
5055 then
5056 return;
5057 end if;
5059 -- Create and decorate the tags. Suppress their creation when
5060 -- not Tagged_Type_Expansion because the dispatching mechanism is
5061 -- handled internally by the virtual target.
5063 if Tagged_Type_Expansion then
5064 Append_Freeze_Actions (Typ, Make_Tags (Typ));
5066 -- Generate dispatch table of locally defined tagged type.
5067 -- Dispatch tables of library level tagged types are built
5068 -- later (see Analyze_Declarations).
5070 if not Building_Static_DT (Typ) then
5071 Append_Freeze_Actions (Typ, Make_DT (Typ));
5072 end if;
5073 end if;
5075 -- If the type has unknown discriminants, propagate dispatching
5076 -- information to its underlying record view, which does not get
5077 -- its own dispatch table.
5079 if Is_Derived_Type (Typ)
5080 and then Has_Unknown_Discriminants (Typ)
5081 and then Present (Underlying_Record_View (Typ))
5082 then
5083 declare
5084 Rep : constant Entity_Id := Underlying_Record_View (Typ);
5085 begin
5086 Set_Access_Disp_Table
5087 (Rep, Access_Disp_Table (Typ));
5088 Set_Dispatch_Table_Wrappers
5089 (Rep, Dispatch_Table_Wrappers (Typ));
5090 Set_Direct_Primitive_Operations
5091 (Rep, Direct_Primitive_Operations (Typ));
5092 end;
5093 end if;
5095 -- Make sure that the primitives Initialize, Adjust and Finalize
5096 -- are Frozen before other TSS subprograms. We don't want them
5097 -- Frozen inside.
5099 if Is_Controlled (Typ) then
5100 if not Is_Limited_Type (Typ) then
5101 Append_Freeze_Actions (Typ,
5102 Freeze_Entity (Find_Prim_Op (Typ, Name_Adjust), Typ));
5103 end if;
5105 Append_Freeze_Actions (Typ,
5106 Freeze_Entity (Find_Prim_Op (Typ, Name_Initialize), Typ));
5108 Append_Freeze_Actions (Typ,
5109 Freeze_Entity (Find_Prim_Op (Typ, Name_Finalize), Typ));
5110 end if;
5112 -- Freeze rest of primitive operations. There is no need to handle
5113 -- the predefined primitives if we are compiling under restriction
5114 -- No_Dispatching_Calls.
5116 if not Restriction_Active (No_Dispatching_Calls) then
5117 Append_Freeze_Actions (Typ, Predefined_Primitive_Freeze (Typ));
5118 end if;
5119 end if;
5121 -- In the untagged case, ever since Ada 83 an equality function must
5122 -- be provided for variant records that are not unchecked unions.
5123 -- In Ada 2012 the equality function composes, and thus must be built
5124 -- explicitly just as for tagged records.
5126 elsif Has_Discriminants (Typ)
5127 and then not Is_Limited_Type (Typ)
5128 then
5129 declare
5130 Comps : constant Node_Id :=
5131 Component_List (Type_Definition (Typ_Decl));
5132 begin
5133 if Present (Comps)
5134 and then Present (Variant_Part (Comps))
5135 then
5136 Build_Variant_Record_Equality (Typ);
5137 end if;
5138 end;
5140 -- Otherwise create primitive equality operation (AI05-0123)
5142 -- This is done unconditionally to ensure that tools can be linked
5143 -- properly with user programs compiled with older language versions.
5144 -- In addition, this is needed because "=" composes for bounded strings
5145 -- in all language versions (see Exp_Ch4.Expand_Composite_Equality).
5147 elsif Comes_From_Source (Typ)
5148 and then Convention (Typ) = Convention_Ada
5149 and then not Is_Limited_Type (Typ)
5150 then
5151 Build_Untagged_Equality (Typ);
5152 end if;
5154 -- Before building the record initialization procedure, if we are
5155 -- dealing with a concurrent record value type, then we must go through
5156 -- the discriminants, exchanging discriminals between the concurrent
5157 -- type and the concurrent record value type. See the section "Handling
5158 -- of Discriminants" in the Einfo spec for details.
5160 if Is_Concurrent_Record_Type (Typ)
5161 and then Has_Discriminants (Typ)
5162 then
5163 declare
5164 Ctyp : constant Entity_Id :=
5165 Corresponding_Concurrent_Type (Typ);
5166 Conc_Discr : Entity_Id;
5167 Rec_Discr : Entity_Id;
5168 Temp : Entity_Id;
5170 begin
5171 Conc_Discr := First_Discriminant (Ctyp);
5172 Rec_Discr := First_Discriminant (Typ);
5173 while Present (Conc_Discr) loop
5174 Temp := Discriminal (Conc_Discr);
5175 Set_Discriminal (Conc_Discr, Discriminal (Rec_Discr));
5176 Set_Discriminal (Rec_Discr, Temp);
5178 Set_Discriminal_Link (Discriminal (Conc_Discr), Conc_Discr);
5179 Set_Discriminal_Link (Discriminal (Rec_Discr), Rec_Discr);
5181 Next_Discriminant (Conc_Discr);
5182 Next_Discriminant (Rec_Discr);
5183 end loop;
5184 end;
5185 end if;
5187 if Has_Controlled_Component (Typ) then
5188 Build_Controlling_Procs (Typ);
5189 end if;
5191 Adjust_Discriminants (Typ);
5193 -- Do not need init for interfaces on virtual targets since they're
5194 -- abstract.
5196 if Tagged_Type_Expansion or else not Is_Interface (Typ) then
5197 Build_Record_Init_Proc (Typ_Decl, Typ);
5198 end if;
5200 -- For tagged type that are not interfaces, build bodies of primitive
5201 -- operations. Note: do this after building the record initialization
5202 -- procedure, since the primitive operations may need the initialization
5203 -- routine. There is no need to add predefined primitives of interfaces
5204 -- because all their predefined primitives are abstract.
5206 if Is_Tagged_Type (Typ) and then not Is_Interface (Typ) then
5208 -- Do not add the body of predefined primitives in case of CPP tagged
5209 -- type derivations that have convention CPP.
5211 if Is_CPP_Class (Root_Type (Typ))
5212 and then Convention (Typ) = Convention_CPP
5213 then
5214 null;
5216 -- Do not add the body of the predefined primitives if we are
5217 -- compiling under restriction No_Dispatching_Calls or if we are
5218 -- compiling a CPP tagged type.
5220 elsif not Restriction_Active (No_Dispatching_Calls) then
5222 -- Create the body of TSS primitive Finalize_Address. This must
5223 -- be done before the bodies of all predefined primitives are
5224 -- created. If Typ is limited, Stream_Input and Stream_Read may
5225 -- produce build-in-place allocations and for those the expander
5226 -- needs Finalize_Address.
5228 Make_Finalize_Address_Body (Typ);
5229 Predef_List := Predefined_Primitive_Bodies (Typ, Renamed_Eq);
5230 Append_Freeze_Actions (Typ, Predef_List);
5231 end if;
5233 -- Ada 2005 (AI-391): If any wrappers were created for nonoverridden
5234 -- inherited functions, then add their bodies to the freeze actions.
5236 if Present (Wrapper_Body_List) then
5237 Append_Freeze_Actions (Typ, Wrapper_Body_List);
5238 end if;
5240 -- Create extra formals for the primitive operations of the type.
5241 -- This must be done before analyzing the body of the initialization
5242 -- procedure, because a self-referential type might call one of these
5243 -- primitives in the body of the init_proc itself.
5245 declare
5246 Elmt : Elmt_Id;
5247 Subp : Entity_Id;
5249 begin
5250 Elmt := First_Elmt (Primitive_Operations (Typ));
5251 while Present (Elmt) loop
5252 Subp := Node (Elmt);
5253 if not Has_Foreign_Convention (Subp)
5254 and then not Is_Predefined_Dispatching_Operation (Subp)
5255 then
5256 Create_Extra_Formals (Subp);
5257 end if;
5259 Next_Elmt (Elmt);
5260 end loop;
5261 end;
5262 end if;
5263 end Expand_Freeze_Record_Type;
5265 ------------------------------------
5266 -- Expand_N_Full_Type_Declaration --
5267 ------------------------------------
5269 procedure Expand_N_Full_Type_Declaration (N : Node_Id) is
5270 procedure Build_Master (Ptr_Typ : Entity_Id);
5271 -- Create the master associated with Ptr_Typ
5273 ------------------
5274 -- Build_Master --
5275 ------------------
5277 procedure Build_Master (Ptr_Typ : Entity_Id) is
5278 Desig_Typ : Entity_Id := Designated_Type (Ptr_Typ);
5280 begin
5281 -- If the designated type is an incomplete view coming from a
5282 -- limited-with'ed package, we need to use the nonlimited view in
5283 -- case it has tasks.
5285 if Ekind (Desig_Typ) in Incomplete_Kind
5286 and then Present (Non_Limited_View (Desig_Typ))
5287 then
5288 Desig_Typ := Non_Limited_View (Desig_Typ);
5289 end if;
5291 -- Anonymous access types are created for the components of the
5292 -- record parameter for an entry declaration. No master is created
5293 -- for such a type.
5295 if Comes_From_Source (N) and then Has_Task (Desig_Typ) then
5296 Build_Master_Entity (Ptr_Typ);
5297 Build_Master_Renaming (Ptr_Typ);
5299 -- Create a class-wide master because a Master_Id must be generated
5300 -- for access-to-limited-class-wide types whose root may be extended
5301 -- with task components.
5303 -- Note: This code covers access-to-limited-interfaces because they
5304 -- can be used to reference tasks implementing them.
5306 elsif Is_Limited_Class_Wide_Type (Desig_Typ)
5307 and then Tasking_Allowed
5308 then
5309 Build_Class_Wide_Master (Ptr_Typ);
5310 end if;
5311 end Build_Master;
5313 -- Local declarations
5315 Def_Id : constant Entity_Id := Defining_Identifier (N);
5316 B_Id : constant Entity_Id := Base_Type (Def_Id);
5317 FN : Node_Id;
5318 Par_Id : Entity_Id;
5320 -- Start of processing for Expand_N_Full_Type_Declaration
5322 begin
5323 if Is_Access_Type (Def_Id) then
5324 Build_Master (Def_Id);
5326 if Ekind (Def_Id) = E_Access_Protected_Subprogram_Type then
5327 Expand_Access_Protected_Subprogram_Type (N);
5328 end if;
5330 -- Array of anonymous access-to-task pointers
5332 elsif Ada_Version >= Ada_2005
5333 and then Is_Array_Type (Def_Id)
5334 and then Is_Access_Type (Component_Type (Def_Id))
5335 and then Ekind (Component_Type (Def_Id)) = E_Anonymous_Access_Type
5336 then
5337 Build_Master (Component_Type (Def_Id));
5339 elsif Has_Task (Def_Id) then
5340 Expand_Previous_Access_Type (Def_Id);
5342 -- Check the components of a record type or array of records for
5343 -- anonymous access-to-task pointers.
5345 elsif Ada_Version >= Ada_2005
5346 and then (Is_Record_Type (Def_Id)
5347 or else
5348 (Is_Array_Type (Def_Id)
5349 and then Is_Record_Type (Component_Type (Def_Id))))
5350 then
5351 declare
5352 Comp : Entity_Id;
5353 First : Boolean;
5354 M_Id : Entity_Id;
5355 Typ : Entity_Id;
5357 begin
5358 if Is_Array_Type (Def_Id) then
5359 Comp := First_Entity (Component_Type (Def_Id));
5360 else
5361 Comp := First_Entity (Def_Id);
5362 end if;
5364 -- Examine all components looking for anonymous access-to-task
5365 -- types.
5367 First := True;
5368 while Present (Comp) loop
5369 Typ := Etype (Comp);
5371 if Ekind (Typ) = E_Anonymous_Access_Type
5372 and then Has_Task (Available_View (Designated_Type (Typ)))
5373 and then No (Master_Id (Typ))
5374 then
5375 -- Ensure that the record or array type have a _master
5377 if First then
5378 Build_Master_Entity (Def_Id);
5379 Build_Master_Renaming (Typ);
5380 M_Id := Master_Id (Typ);
5382 First := False;
5384 -- Reuse the same master to service any additional types
5386 else
5387 Set_Master_Id (Typ, M_Id);
5388 end if;
5389 end if;
5391 Next_Entity (Comp);
5392 end loop;
5393 end;
5394 end if;
5396 Par_Id := Etype (B_Id);
5398 -- The parent type is private then we need to inherit any TSS operations
5399 -- from the full view.
5401 if Ekind (Par_Id) in Private_Kind
5402 and then Present (Full_View (Par_Id))
5403 then
5404 Par_Id := Base_Type (Full_View (Par_Id));
5405 end if;
5407 if Nkind (Type_Definition (Original_Node (N))) =
5408 N_Derived_Type_Definition
5409 and then not Is_Tagged_Type (Def_Id)
5410 and then Present (Freeze_Node (Par_Id))
5411 and then Present (TSS_Elist (Freeze_Node (Par_Id)))
5412 then
5413 Ensure_Freeze_Node (B_Id);
5414 FN := Freeze_Node (B_Id);
5416 if No (TSS_Elist (FN)) then
5417 Set_TSS_Elist (FN, New_Elmt_List);
5418 end if;
5420 declare
5421 T_E : constant Elist_Id := TSS_Elist (FN);
5422 Elmt : Elmt_Id;
5424 begin
5425 Elmt := First_Elmt (TSS_Elist (Freeze_Node (Par_Id)));
5426 while Present (Elmt) loop
5427 if Chars (Node (Elmt)) /= Name_uInit then
5428 Append_Elmt (Node (Elmt), T_E);
5429 end if;
5431 Next_Elmt (Elmt);
5432 end loop;
5434 -- If the derived type itself is private with a full view, then
5435 -- associate the full view with the inherited TSS_Elist as well.
5437 if Ekind (B_Id) in Private_Kind
5438 and then Present (Full_View (B_Id))
5439 then
5440 Ensure_Freeze_Node (Base_Type (Full_View (B_Id)));
5441 Set_TSS_Elist
5442 (Freeze_Node (Base_Type (Full_View (B_Id))), TSS_Elist (FN));
5443 end if;
5444 end;
5445 end if;
5446 end Expand_N_Full_Type_Declaration;
5448 ---------------------------------
5449 -- Expand_N_Object_Declaration --
5450 ---------------------------------
5452 procedure Expand_N_Object_Declaration (N : Node_Id) is
5453 Loc : constant Source_Ptr := Sloc (N);
5454 Def_Id : constant Entity_Id := Defining_Identifier (N);
5455 Expr : constant Node_Id := Expression (N);
5456 Obj_Def : constant Node_Id := Object_Definition (N);
5457 Typ : constant Entity_Id := Etype (Def_Id);
5458 Base_Typ : constant Entity_Id := Base_Type (Typ);
5459 Expr_Q : Node_Id;
5461 function Build_Equivalent_Aggregate return Boolean;
5462 -- If the object has a constrained discriminated type and no initial
5463 -- value, it may be possible to build an equivalent aggregate instead,
5464 -- and prevent an actual call to the initialization procedure.
5466 procedure Default_Initialize_Object (After : Node_Id);
5467 -- Generate all default initialization actions for object Def_Id. Any
5468 -- new code is inserted after node After.
5470 function Rewrite_As_Renaming return Boolean;
5471 -- Indicate whether to rewrite a declaration with initialization into an
5472 -- object renaming declaration (see below).
5474 --------------------------------
5475 -- Build_Equivalent_Aggregate --
5476 --------------------------------
5478 function Build_Equivalent_Aggregate return Boolean is
5479 Aggr : Node_Id;
5480 Comp : Entity_Id;
5481 Discr : Elmt_Id;
5482 Full_Type : Entity_Id;
5484 begin
5485 Full_Type := Typ;
5487 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
5488 Full_Type := Full_View (Typ);
5489 end if;
5491 -- Only perform this transformation if Elaboration_Code is forbidden
5492 -- or undesirable, and if this is a global entity of a constrained
5493 -- record type.
5495 -- If Initialize_Scalars might be active this transformation cannot
5496 -- be performed either, because it will lead to different semantics
5497 -- or because elaboration code will in fact be created.
5499 if Ekind (Full_Type) /= E_Record_Subtype
5500 or else not Has_Discriminants (Full_Type)
5501 or else not Is_Constrained (Full_Type)
5502 or else Is_Controlled (Full_Type)
5503 or else Is_Limited_Type (Full_Type)
5504 or else not Restriction_Active (No_Initialize_Scalars)
5505 then
5506 return False;
5507 end if;
5509 if Ekind (Current_Scope) = E_Package
5510 and then
5511 (Restriction_Active (No_Elaboration_Code)
5512 or else Is_Preelaborated (Current_Scope))
5513 then
5514 -- Building a static aggregate is possible if the discriminants
5515 -- have static values and the other components have static
5516 -- defaults or none.
5518 Discr := First_Elmt (Discriminant_Constraint (Full_Type));
5519 while Present (Discr) loop
5520 if not Is_OK_Static_Expression (Node (Discr)) then
5521 return False;
5522 end if;
5524 Next_Elmt (Discr);
5525 end loop;
5527 -- Check that initialized components are OK, and that non-
5528 -- initialized components do not require a call to their own
5529 -- initialization procedure.
5531 Comp := First_Component (Full_Type);
5532 while Present (Comp) loop
5533 if Ekind (Comp) = E_Component
5534 and then Present (Expression (Parent (Comp)))
5535 and then
5536 not Is_OK_Static_Expression (Expression (Parent (Comp)))
5537 then
5538 return False;
5540 elsif Has_Non_Null_Base_Init_Proc (Etype (Comp)) then
5541 return False;
5543 end if;
5545 Next_Component (Comp);
5546 end loop;
5548 -- Everything is static, assemble the aggregate, discriminant
5549 -- values first.
5551 Aggr :=
5552 Make_Aggregate (Loc,
5553 Expressions => New_List,
5554 Component_Associations => New_List);
5556 Discr := First_Elmt (Discriminant_Constraint (Full_Type));
5557 while Present (Discr) loop
5558 Append_To (Expressions (Aggr), New_Copy (Node (Discr)));
5559 Next_Elmt (Discr);
5560 end loop;
5562 -- Now collect values of initialized components
5564 Comp := First_Component (Full_Type);
5565 while Present (Comp) loop
5566 if Ekind (Comp) = E_Component
5567 and then Present (Expression (Parent (Comp)))
5568 then
5569 Append_To (Component_Associations (Aggr),
5570 Make_Component_Association (Loc,
5571 Choices => New_List (New_Occurrence_Of (Comp, Loc)),
5572 Expression => New_Copy_Tree
5573 (Expression (Parent (Comp)))));
5574 end if;
5576 Next_Component (Comp);
5577 end loop;
5579 -- Finally, box-initialize remaining components
5581 Append_To (Component_Associations (Aggr),
5582 Make_Component_Association (Loc,
5583 Choices => New_List (Make_Others_Choice (Loc)),
5584 Expression => Empty));
5585 Set_Box_Present (Last (Component_Associations (Aggr)));
5586 Set_Expression (N, Aggr);
5588 if Typ /= Full_Type then
5589 Analyze_And_Resolve (Aggr, Full_View (Base_Type (Full_Type)));
5590 Rewrite (Aggr, Unchecked_Convert_To (Typ, Aggr));
5591 Analyze_And_Resolve (Aggr, Typ);
5592 else
5593 Analyze_And_Resolve (Aggr, Full_Type);
5594 end if;
5596 return True;
5598 else
5599 return False;
5600 end if;
5601 end Build_Equivalent_Aggregate;
5603 -------------------------------
5604 -- Default_Initialize_Object --
5605 -------------------------------
5607 procedure Default_Initialize_Object (After : Node_Id) is
5608 function New_Object_Reference return Node_Id;
5609 -- Return a new reference to Def_Id with attributes Assignment_OK and
5610 -- Must_Not_Freeze already set.
5612 --------------------------
5613 -- New_Object_Reference --
5614 --------------------------
5616 function New_Object_Reference return Node_Id is
5617 Obj_Ref : constant Node_Id := New_Occurrence_Of (Def_Id, Loc);
5619 begin
5620 -- The call to the type init proc or [Deep_]Finalize must not
5621 -- freeze the related object as the call is internally generated.
5622 -- This way legal rep clauses that apply to the object will not be
5623 -- flagged. Note that the initialization call may be removed if
5624 -- pragma Import is encountered or moved to the freeze actions of
5625 -- the object because of an address clause.
5627 Set_Assignment_OK (Obj_Ref);
5628 Set_Must_Not_Freeze (Obj_Ref);
5630 return Obj_Ref;
5631 end New_Object_Reference;
5633 -- Local variables
5635 Exceptions_OK : constant Boolean :=
5636 not Restriction_Active (No_Exception_Propagation);
5638 Aggr_Init : Node_Id;
5639 Comp_Init : List_Id := No_List;
5640 Fin_Call : Node_Id;
5641 Init_Stmts : List_Id := No_List;
5642 Obj_Init : Node_Id := Empty;
5643 Obj_Ref : Node_Id;
5645 -- Start of processing for Default_Initialize_Object
5647 begin
5648 -- Default initialization is suppressed for objects that are already
5649 -- known to be imported (i.e. whose declaration specifies the Import
5650 -- aspect). Note that for objects with a pragma Import, we generate
5651 -- initialization here, and then remove it downstream when processing
5652 -- the pragma. It is also suppressed for variables for which a pragma
5653 -- Suppress_Initialization has been explicitly given
5655 if Is_Imported (Def_Id) or else Suppress_Initialization (Def_Id) then
5656 return;
5658 -- Nothing to do if the object being initialized is of a task type
5659 -- and restriction No_Tasking is in effect, because this is a direct
5660 -- violation of the restriction.
5662 elsif Is_Task_Type (Base_Typ)
5663 and then Restriction_Active (No_Tasking)
5664 then
5665 return;
5666 end if;
5668 -- The expansion performed by this routine is as follows:
5670 -- begin
5671 -- Abort_Defer;
5672 -- Type_Init_Proc (Obj);
5674 -- begin
5675 -- [Deep_]Initialize (Obj);
5677 -- exception
5678 -- when others =>
5679 -- [Deep_]Finalize (Obj, Self => False);
5680 -- raise;
5681 -- end;
5682 -- at end
5683 -- Abort_Undefer_Direct;
5684 -- end;
5686 -- Initialize the components of the object
5688 if Has_Non_Null_Base_Init_Proc (Typ)
5689 and then not No_Initialization (N)
5690 and then not Initialization_Suppressed (Typ)
5691 then
5692 -- Do not initialize the components if No_Default_Initialization
5693 -- applies as the actual restriction check will occur later
5694 -- when the object is frozen as it is not known yet whether the
5695 -- object is imported or not.
5697 if not Restriction_Active (No_Default_Initialization) then
5699 -- If the values of the components are compile-time known, use
5700 -- their prebuilt aggregate form directly.
5702 Aggr_Init := Static_Initialization (Base_Init_Proc (Typ));
5704 if Present (Aggr_Init) then
5705 Set_Expression
5706 (N, New_Copy_Tree (Aggr_Init, New_Scope => Current_Scope));
5708 -- If type has discriminants, try to build an equivalent
5709 -- aggregate using discriminant values from the declaration.
5710 -- This is a useful optimization, in particular if restriction
5711 -- No_Elaboration_Code is active.
5713 elsif Build_Equivalent_Aggregate then
5714 null;
5716 -- Otherwise invoke the type init proc, generate:
5717 -- Type_Init_Proc (Obj);
5719 else
5720 Obj_Ref := New_Object_Reference;
5722 if Comes_From_Source (Def_Id) then
5723 Initialization_Warning (Obj_Ref);
5724 end if;
5726 Comp_Init := Build_Initialization_Call (Loc, Obj_Ref, Typ);
5727 end if;
5728 end if;
5730 -- Provide a default value if the object needs simple initialization
5731 -- and does not already have an initial value. A generated temporary
5732 -- does not require initialization because it will be assigned later.
5734 elsif Needs_Simple_Initialization
5735 (Typ, Initialize_Scalars
5736 and then No (Following_Address_Clause (N)))
5737 and then not Is_Internal (Def_Id)
5738 and then not Has_Init_Expression (N)
5739 then
5740 Set_No_Initialization (N, False);
5741 Set_Expression (N, Get_Simple_Init_Val (Typ, N, Esize (Def_Id)));
5742 Analyze_And_Resolve (Expression (N), Typ);
5743 end if;
5745 -- Initialize the object, generate:
5746 -- [Deep_]Initialize (Obj);
5748 if Needs_Finalization (Typ) and then not No_Initialization (N) then
5749 Obj_Init :=
5750 Make_Init_Call
5751 (Obj_Ref => New_Occurrence_Of (Def_Id, Loc),
5752 Typ => Typ);
5753 end if;
5755 -- Build a special finalization block when both the object and its
5756 -- controlled components are to be initialized. The block finalizes
5757 -- the components if the object initialization fails. Generate:
5759 -- begin
5760 -- <Obj_Init>
5762 -- exception
5763 -- when others =>
5764 -- <Fin_Call>
5765 -- raise;
5766 -- end;
5768 if Has_Controlled_Component (Typ)
5769 and then Present (Comp_Init)
5770 and then Present (Obj_Init)
5771 and then Exceptions_OK
5772 then
5773 Init_Stmts := Comp_Init;
5775 Fin_Call :=
5776 Make_Final_Call
5777 (Obj_Ref => New_Object_Reference,
5778 Typ => Typ,
5779 Skip_Self => True);
5781 if Present (Fin_Call) then
5783 -- Do not emit warnings related to the elaboration order when a
5784 -- controlled object is declared before the body of Finalize is
5785 -- seen.
5787 Set_No_Elaboration_Check (Fin_Call);
5789 Append_To (Init_Stmts,
5790 Make_Block_Statement (Loc,
5791 Declarations => No_List,
5793 Handled_Statement_Sequence =>
5794 Make_Handled_Sequence_Of_Statements (Loc,
5795 Statements => New_List (Obj_Init),
5797 Exception_Handlers => New_List (
5798 Make_Exception_Handler (Loc,
5799 Exception_Choices => New_List (
5800 Make_Others_Choice (Loc)),
5802 Statements => New_List (
5803 Fin_Call,
5804 Make_Raise_Statement (Loc)))))));
5805 end if;
5807 -- Otherwise finalization is not required, the initialization calls
5808 -- are passed to the abort block building circuitry, generate:
5810 -- Type_Init_Proc (Obj);
5811 -- [Deep_]Initialize (Obj);
5813 else
5814 if Present (Comp_Init) then
5815 Init_Stmts := Comp_Init;
5816 end if;
5818 if Present (Obj_Init) then
5819 if No (Init_Stmts) then
5820 Init_Stmts := New_List;
5821 end if;
5823 Append_To (Init_Stmts, Obj_Init);
5824 end if;
5825 end if;
5827 -- Build an abort block to protect the initialization calls
5829 if Abort_Allowed
5830 and then Present (Comp_Init)
5831 and then Present (Obj_Init)
5832 then
5833 -- Generate:
5834 -- Abort_Defer;
5836 Prepend_To (Init_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
5838 -- When exceptions are propagated, abort deferral must take place
5839 -- in the presence of initialization or finalization exceptions.
5840 -- Generate:
5842 -- begin
5843 -- Abort_Defer;
5844 -- <Init_Stmts>
5845 -- at end
5846 -- Abort_Undefer_Direct;
5847 -- end;
5849 if Exceptions_OK then
5850 Init_Stmts := New_List (
5851 Build_Abort_Undefer_Block (Loc,
5852 Stmts => Init_Stmts,
5853 Context => N));
5855 -- Otherwise exceptions are not propagated. Generate:
5857 -- Abort_Defer;
5858 -- <Init_Stmts>
5859 -- Abort_Undefer;
5861 else
5862 Append_To (Init_Stmts,
5863 Build_Runtime_Call (Loc, RE_Abort_Undefer));
5864 end if;
5865 end if;
5867 -- Insert the whole initialization sequence into the tree. If the
5868 -- object has a delayed freeze, as will be the case when it has
5869 -- aspect specifications, the initialization sequence is part of
5870 -- the freeze actions.
5872 if Present (Init_Stmts) then
5873 if Has_Delayed_Freeze (Def_Id) then
5874 Append_Freeze_Actions (Def_Id, Init_Stmts);
5875 else
5876 Insert_Actions_After (After, Init_Stmts);
5877 end if;
5878 end if;
5879 end Default_Initialize_Object;
5881 -------------------------
5882 -- Rewrite_As_Renaming --
5883 -------------------------
5885 function Rewrite_As_Renaming return Boolean is
5886 begin
5887 -- If the object declaration appears in the form
5889 -- Obj : Ctrl_Typ := Func (...);
5891 -- where Ctrl_Typ is controlled but not immutably limited type, then
5892 -- the expansion of the function call should use a dereference of the
5893 -- result to reference the value on the secondary stack.
5895 -- Obj : Ctrl_Typ renames Func (...).all;
5897 -- As a result, the call avoids an extra copy. This an optimization,
5898 -- but it is required for passing ACATS tests in some cases where it
5899 -- would otherwise make two copies. The RM allows removing redunant
5900 -- Adjust/Finalize calls, but does not allow insertion of extra ones.
5902 -- This part is disabled for now, because it breaks GPS builds
5904 return (False -- ???
5905 and then Nkind (Expr_Q) = N_Explicit_Dereference
5906 and then not Comes_From_Source (Expr_Q)
5907 and then Nkind (Original_Node (Expr_Q)) = N_Function_Call
5908 and then Nkind (Object_Definition (N)) in N_Has_Entity
5909 and then (Needs_Finalization (Entity (Object_Definition (N)))))
5911 -- If the initializing expression is for a variable with attribute
5912 -- OK_To_Rename set, then transform:
5914 -- Obj : Typ := Expr;
5916 -- into
5918 -- Obj : Typ renames Expr;
5920 -- provided that Obj is not aliased. The aliased case has to be
5921 -- excluded in general because Expr will not be aliased in
5922 -- general.
5924 or else
5925 (not Aliased_Present (N)
5926 and then Is_Entity_Name (Expr_Q)
5927 and then Ekind (Entity (Expr_Q)) = E_Variable
5928 and then OK_To_Rename (Entity (Expr_Q))
5929 and then Is_Entity_Name (Obj_Def));
5930 end Rewrite_As_Renaming;
5932 -- Local variables
5934 Next_N : constant Node_Id := Next (N);
5936 Adj_Call : Node_Id;
5937 Id_Ref : Node_Id;
5938 Tag_Assign : Node_Id;
5940 Init_After : Node_Id := N;
5941 -- Node after which the initialization actions are to be inserted. This
5942 -- is normally N, except for the case of a shared passive variable, in
5943 -- which case the init proc call must be inserted only after the bodies
5944 -- of the shared variable procedures have been seen.
5946 -- Start of processing for Expand_N_Object_Declaration
5948 begin
5949 -- Don't do anything for deferred constants. All proper actions will be
5950 -- expanded during the full declaration.
5952 if No (Expr) and Constant_Present (N) then
5953 return;
5954 end if;
5956 -- The type of the object cannot be abstract. This is diagnosed at the
5957 -- point the object is frozen, which happens after the declaration is
5958 -- fully expanded, so simply return now.
5960 if Is_Abstract_Type (Typ) then
5961 return;
5962 end if;
5964 -- First we do special processing for objects of a tagged type where
5965 -- this is the point at which the type is frozen. The creation of the
5966 -- dispatch table and the initialization procedure have to be deferred
5967 -- to this point, since we reference previously declared primitive
5968 -- subprograms.
5970 -- Force construction of dispatch tables of library level tagged types
5972 if Tagged_Type_Expansion
5973 and then Static_Dispatch_Tables
5974 and then Is_Library_Level_Entity (Def_Id)
5975 and then Is_Library_Level_Tagged_Type (Base_Typ)
5976 and then Ekind_In (Base_Typ, E_Record_Type,
5977 E_Protected_Type,
5978 E_Task_Type)
5979 and then not Has_Dispatch_Table (Base_Typ)
5980 then
5981 declare
5982 New_Nodes : List_Id := No_List;
5984 begin
5985 if Is_Concurrent_Type (Base_Typ) then
5986 New_Nodes := Make_DT (Corresponding_Record_Type (Base_Typ), N);
5987 else
5988 New_Nodes := Make_DT (Base_Typ, N);
5989 end if;
5991 if not Is_Empty_List (New_Nodes) then
5992 Insert_List_Before (N, New_Nodes);
5993 end if;
5994 end;
5995 end if;
5997 -- Make shared memory routines for shared passive variable
5999 if Is_Shared_Passive (Def_Id) then
6000 Init_After := Make_Shared_Var_Procs (N);
6001 end if;
6003 -- If tasks being declared, make sure we have an activation chain
6004 -- defined for the tasks (has no effect if we already have one), and
6005 -- also that a Master variable is established and that the appropriate
6006 -- enclosing construct is established as a task master.
6008 if Has_Task (Typ) then
6009 Build_Activation_Chain_Entity (N);
6010 Build_Master_Entity (Def_Id);
6011 end if;
6013 -- Default initialization required, and no expression present
6015 if No (Expr) then
6017 -- If we have a type with a variant part, the initialization proc
6018 -- will contain implicit tests of the discriminant values, which
6019 -- counts as a violation of the restriction No_Implicit_Conditionals.
6021 if Has_Variant_Part (Typ) then
6022 declare
6023 Msg : Boolean;
6025 begin
6026 Check_Restriction (Msg, No_Implicit_Conditionals, Obj_Def);
6028 if Msg then
6029 Error_Msg_N
6030 ("\initialization of variant record tests discriminants",
6031 Obj_Def);
6032 return;
6033 end if;
6034 end;
6035 end if;
6037 -- For the default initialization case, if we have a private type
6038 -- with invariants, and invariant checks are enabled, then insert an
6039 -- invariant check after the object declaration. Note that it is OK
6040 -- to clobber the object with an invalid value since if the exception
6041 -- is raised, then the object will go out of scope. In the case where
6042 -- an array object is initialized with an aggregate, the expression
6043 -- is removed. Check flag Has_Init_Expression to avoid generating a
6044 -- junk invariant check and flag No_Initialization to avoid checking
6045 -- an uninitialized object such as a compiler temporary used for an
6046 -- aggregate.
6048 if Has_Invariants (Base_Typ)
6049 and then Present (Invariant_Procedure (Base_Typ))
6050 and then not Has_Init_Expression (N)
6051 and then not No_Initialization (N)
6052 then
6053 -- If entity has an address clause or aspect, make invariant
6054 -- call into a freeze action for the explicit freeze node for
6055 -- object. Otherwise insert invariant check after declaration.
6057 if Present (Following_Address_Clause (N))
6058 or else Has_Aspect (Def_Id, Aspect_Address)
6059 then
6060 Ensure_Freeze_Node (Def_Id);
6061 Set_Has_Delayed_Freeze (Def_Id);
6062 Set_Is_Frozen (Def_Id, False);
6064 if not Partial_View_Has_Unknown_Discr (Typ) then
6065 Append_Freeze_Action (Def_Id,
6066 Make_Invariant_Call (New_Occurrence_Of (Def_Id, Loc)));
6067 end if;
6069 elsif not Partial_View_Has_Unknown_Discr (Typ) then
6070 Insert_After (N,
6071 Make_Invariant_Call (New_Occurrence_Of (Def_Id, Loc)));
6072 end if;
6073 end if;
6075 Default_Initialize_Object (Init_After);
6077 -- Generate attribute for Persistent_BSS if needed
6079 if Persistent_BSS_Mode
6080 and then Comes_From_Source (N)
6081 and then Is_Potentially_Persistent_Type (Typ)
6082 and then not Has_Init_Expression (N)
6083 and then Is_Library_Level_Entity (Def_Id)
6084 then
6085 declare
6086 Prag : Node_Id;
6087 begin
6088 Prag :=
6089 Make_Linker_Section_Pragma
6090 (Def_Id, Sloc (N), ".persistent.bss");
6091 Insert_After (N, Prag);
6092 Analyze (Prag);
6093 end;
6094 end if;
6096 -- If access type, then we know it is null if not initialized
6098 if Is_Access_Type (Typ) then
6099 Set_Is_Known_Null (Def_Id);
6100 end if;
6102 -- Explicit initialization present
6104 else
6105 -- Obtain actual expression from qualified expression
6107 if Nkind (Expr) = N_Qualified_Expression then
6108 Expr_Q := Expression (Expr);
6109 else
6110 Expr_Q := Expr;
6111 end if;
6113 -- When we have the appropriate type of aggregate in the expression
6114 -- (it has been determined during analysis of the aggregate by
6115 -- setting the delay flag), let's perform in place assignment and
6116 -- thus avoid creating a temporary.
6118 if Is_Delayed_Aggregate (Expr_Q) then
6119 Convert_Aggr_In_Object_Decl (N);
6121 -- Ada 2005 (AI-318-02): If the initialization expression is a call
6122 -- to a build-in-place function, then access to the declared object
6123 -- must be passed to the function. Currently we limit such functions
6124 -- to those with constrained limited result subtypes, but eventually
6125 -- plan to expand the allowed forms of functions that are treated as
6126 -- build-in-place.
6128 elsif Ada_Version >= Ada_2005
6129 and then Is_Build_In_Place_Function_Call (Expr_Q)
6130 then
6131 Make_Build_In_Place_Call_In_Object_Declaration (N, Expr_Q);
6133 -- The previous call expands the expression initializing the
6134 -- built-in-place object into further code that will be analyzed
6135 -- later. No further expansion needed here.
6137 return;
6139 -- Ada 2005 (AI-251): Rewrite the expression that initializes a
6140 -- class-wide interface object to ensure that we copy the full
6141 -- object, unless we are targetting a VM where interfaces are handled
6142 -- by VM itself. Note that if the root type of Typ is an ancestor of
6143 -- Expr's type, both types share the same dispatch table and there is
6144 -- no need to displace the pointer.
6146 elsif Is_Interface (Typ)
6148 -- Avoid never-ending recursion because if Equivalent_Type is set
6149 -- then we've done it already and must not do it again.
6151 and then not
6152 (Nkind (Obj_Def) = N_Identifier
6153 and then Present (Equivalent_Type (Entity (Obj_Def))))
6154 then
6155 pragma Assert (Is_Class_Wide_Type (Typ));
6157 -- If the object is a return object of an inherently limited type,
6158 -- which implies build-in-place treatment, bypass the special
6159 -- treatment of class-wide interface initialization below. In this
6160 -- case, the expansion of the return statement will take care of
6161 -- creating the object (via allocator) and initializing it.
6163 if Is_Return_Object (Def_Id) and then Is_Limited_View (Typ) then
6164 null;
6166 elsif Tagged_Type_Expansion then
6167 declare
6168 Iface : constant Entity_Id := Root_Type (Typ);
6169 Expr_N : Node_Id := Expr;
6170 Expr_Typ : Entity_Id;
6171 New_Expr : Node_Id;
6172 Obj_Id : Entity_Id;
6173 Tag_Comp : Node_Id;
6175 begin
6176 -- If the original node of the expression was a conversion
6177 -- to this specific class-wide interface type then restore
6178 -- the original node because we must copy the object before
6179 -- displacing the pointer to reference the secondary tag
6180 -- component. This code must be kept synchronized with the
6181 -- expansion done by routine Expand_Interface_Conversion
6183 if not Comes_From_Source (Expr_N)
6184 and then Nkind (Expr_N) = N_Explicit_Dereference
6185 and then Nkind (Original_Node (Expr_N)) = N_Type_Conversion
6186 and then Etype (Original_Node (Expr_N)) = Typ
6187 then
6188 Rewrite (Expr_N, Original_Node (Expression (N)));
6189 end if;
6191 -- Avoid expansion of redundant interface conversion
6193 if Is_Interface (Etype (Expr_N))
6194 and then Nkind (Expr_N) = N_Type_Conversion
6195 and then Etype (Expr_N) = Typ
6196 then
6197 Expr_N := Expression (Expr_N);
6198 Set_Expression (N, Expr_N);
6199 end if;
6201 Obj_Id := Make_Temporary (Loc, 'D', Expr_N);
6202 Expr_Typ := Base_Type (Etype (Expr_N));
6204 if Is_Class_Wide_Type (Expr_Typ) then
6205 Expr_Typ := Root_Type (Expr_Typ);
6206 end if;
6208 -- Replace
6209 -- CW : I'Class := Obj;
6210 -- by
6211 -- Tmp : T := Obj;
6212 -- type Ityp is not null access I'Class;
6213 -- CW : I'Class renames Ityp (Tmp.I_Tag'Address).all;
6215 if Comes_From_Source (Expr_N)
6216 and then Nkind (Expr_N) = N_Identifier
6217 and then not Is_Interface (Expr_Typ)
6218 and then Interface_Present_In_Ancestor (Expr_Typ, Typ)
6219 and then (Expr_Typ = Etype (Expr_Typ)
6220 or else not
6221 Is_Variable_Size_Record (Etype (Expr_Typ)))
6222 then
6223 -- Copy the object
6225 Insert_Action (N,
6226 Make_Object_Declaration (Loc,
6227 Defining_Identifier => Obj_Id,
6228 Object_Definition =>
6229 New_Occurrence_Of (Expr_Typ, Loc),
6230 Expression => Relocate_Node (Expr_N)));
6232 -- Statically reference the tag associated with the
6233 -- interface
6235 Tag_Comp :=
6236 Make_Selected_Component (Loc,
6237 Prefix => New_Occurrence_Of (Obj_Id, Loc),
6238 Selector_Name =>
6239 New_Occurrence_Of
6240 (Find_Interface_Tag (Expr_Typ, Iface), Loc));
6242 -- Replace
6243 -- IW : I'Class := Obj;
6244 -- by
6245 -- type Equiv_Record is record ... end record;
6246 -- implicit subtype CW is <Class_Wide_Subtype>;
6247 -- Tmp : CW := CW!(Obj);
6248 -- type Ityp is not null access I'Class;
6249 -- IW : I'Class renames
6250 -- Ityp!(Displace (Temp'Address, I'Tag)).all;
6252 else
6253 -- Generate the equivalent record type and update the
6254 -- subtype indication to reference it.
6256 Expand_Subtype_From_Expr
6257 (N => N,
6258 Unc_Type => Typ,
6259 Subtype_Indic => Obj_Def,
6260 Exp => Expr_N);
6262 if not Is_Interface (Etype (Expr_N)) then
6263 New_Expr := Relocate_Node (Expr_N);
6265 -- For interface types we use 'Address which displaces
6266 -- the pointer to the base of the object (if required)
6268 else
6269 New_Expr :=
6270 Unchecked_Convert_To (Etype (Obj_Def),
6271 Make_Explicit_Dereference (Loc,
6272 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6273 Make_Attribute_Reference (Loc,
6274 Prefix => Relocate_Node (Expr_N),
6275 Attribute_Name => Name_Address))));
6276 end if;
6278 -- Copy the object
6280 if not Is_Limited_Record (Expr_Typ) then
6281 Insert_Action (N,
6282 Make_Object_Declaration (Loc,
6283 Defining_Identifier => Obj_Id,
6284 Object_Definition =>
6285 New_Occurrence_Of (Etype (Obj_Def), Loc),
6286 Expression => New_Expr));
6288 -- Rename limited type object since they cannot be copied
6289 -- This case occurs when the initialization expression
6290 -- has been previously expanded into a temporary object.
6292 else pragma Assert (not Comes_From_Source (Expr_Q));
6293 Insert_Action (N,
6294 Make_Object_Renaming_Declaration (Loc,
6295 Defining_Identifier => Obj_Id,
6296 Subtype_Mark =>
6297 New_Occurrence_Of (Etype (Obj_Def), Loc),
6298 Name =>
6299 Unchecked_Convert_To
6300 (Etype (Obj_Def), New_Expr)));
6301 end if;
6303 -- Dynamically reference the tag associated with the
6304 -- interface.
6306 Tag_Comp :=
6307 Make_Function_Call (Loc,
6308 Name => New_Occurrence_Of (RTE (RE_Displace), Loc),
6309 Parameter_Associations => New_List (
6310 Make_Attribute_Reference (Loc,
6311 Prefix => New_Occurrence_Of (Obj_Id, Loc),
6312 Attribute_Name => Name_Address),
6313 New_Occurrence_Of
6314 (Node (First_Elmt (Access_Disp_Table (Iface))),
6315 Loc)));
6316 end if;
6318 Rewrite (N,
6319 Make_Object_Renaming_Declaration (Loc,
6320 Defining_Identifier => Make_Temporary (Loc, 'D'),
6321 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
6322 Name =>
6323 Convert_Tag_To_Interface (Typ, Tag_Comp)));
6325 -- If the original entity comes from source, then mark the
6326 -- new entity as needing debug information, even though it's
6327 -- defined by a generated renaming that does not come from
6328 -- source, so that Materialize_Entity will be set on the
6329 -- entity when Debug_Renaming_Declaration is called during
6330 -- analysis.
6332 if Comes_From_Source (Def_Id) then
6333 Set_Debug_Info_Needed (Defining_Identifier (N));
6334 end if;
6336 Analyze (N, Suppress => All_Checks);
6338 -- Replace internal identifier of rewritten node by the
6339 -- identifier found in the sources. We also have to exchange
6340 -- entities containing their defining identifiers to ensure
6341 -- the correct replacement of the object declaration by this
6342 -- object renaming declaration because these identifiers
6343 -- were previously added by Enter_Name to the current scope.
6344 -- We must preserve the homonym chain of the source entity
6345 -- as well. We must also preserve the kind of the entity,
6346 -- which may be a constant. Preserve entity chain because
6347 -- itypes may have been generated already, and the full
6348 -- chain must be preserved for final freezing. Finally,
6349 -- preserve Comes_From_Source setting, so that debugging
6350 -- and cross-referencing information is properly kept, and
6351 -- preserve source location, to prevent spurious errors when
6352 -- entities are declared (they must have their own Sloc).
6354 declare
6355 New_Id : constant Entity_Id := Defining_Identifier (N);
6356 Next_Temp : constant Entity_Id := Next_Entity (New_Id);
6357 S_Flag : constant Boolean :=
6358 Comes_From_Source (Def_Id);
6360 begin
6361 Set_Next_Entity (New_Id, Next_Entity (Def_Id));
6362 Set_Next_Entity (Def_Id, Next_Temp);
6364 Set_Chars (Defining_Identifier (N), Chars (Def_Id));
6365 Set_Homonym (Defining_Identifier (N), Homonym (Def_Id));
6366 Set_Ekind (Defining_Identifier (N), Ekind (Def_Id));
6367 Set_Sloc (Defining_Identifier (N), Sloc (Def_Id));
6369 Set_Comes_From_Source (Def_Id, False);
6370 Exchange_Entities (Defining_Identifier (N), Def_Id);
6371 Set_Comes_From_Source (Def_Id, S_Flag);
6372 end;
6373 end;
6374 end if;
6376 return;
6378 -- Common case of explicit object initialization
6380 else
6381 -- In most cases, we must check that the initial value meets any
6382 -- constraint imposed by the declared type. However, there is one
6383 -- very important exception to this rule. If the entity has an
6384 -- unconstrained nominal subtype, then it acquired its constraints
6385 -- from the expression in the first place, and not only does this
6386 -- mean that the constraint check is not needed, but an attempt to
6387 -- perform the constraint check can cause order of elaboration
6388 -- problems.
6390 if not Is_Constr_Subt_For_U_Nominal (Typ) then
6392 -- If this is an allocator for an aggregate that has been
6393 -- allocated in place, delay checks until assignments are
6394 -- made, because the discriminants are not initialized.
6396 if Nkind (Expr) = N_Allocator and then No_Initialization (Expr)
6397 then
6398 null;
6400 -- Otherwise apply a constraint check now if no prev error
6402 elsif Nkind (Expr) /= N_Error then
6403 Apply_Constraint_Check (Expr, Typ);
6405 -- Deal with possible range check
6407 if Do_Range_Check (Expr) then
6409 -- If assignment checks are suppressed, turn off flag
6411 if Suppress_Assignment_Checks (N) then
6412 Set_Do_Range_Check (Expr, False);
6414 -- Otherwise generate the range check
6416 else
6417 Generate_Range_Check
6418 (Expr, Typ, CE_Range_Check_Failed);
6419 end if;
6420 end if;
6421 end if;
6422 end if;
6424 -- If the type is controlled and not inherently limited, then
6425 -- the target is adjusted after the copy and attached to the
6426 -- finalization list. However, no adjustment is done in the case
6427 -- where the object was initialized by a call to a function whose
6428 -- result is built in place, since no copy occurred. (Eventually
6429 -- we plan to support in-place function results for some cases
6430 -- of nonlimited types. ???) Similarly, no adjustment is required
6431 -- if we are going to rewrite the object declaration into a
6432 -- renaming declaration.
6434 if Needs_Finalization (Typ)
6435 and then not Is_Limited_View (Typ)
6436 and then not Rewrite_As_Renaming
6437 then
6438 Adj_Call :=
6439 Make_Adjust_Call (
6440 Obj_Ref => New_Occurrence_Of (Def_Id, Loc),
6441 Typ => Base_Typ);
6443 -- Guard against a missing [Deep_]Adjust when the base type
6444 -- was not properly frozen.
6446 if Present (Adj_Call) then
6447 Insert_Action_After (Init_After, Adj_Call);
6448 end if;
6449 end if;
6451 -- For tagged types, when an init value is given, the tag has to
6452 -- be re-initialized separately in order to avoid the propagation
6453 -- of a wrong tag coming from a view conversion unless the type
6454 -- is class wide (in this case the tag comes from the init value).
6455 -- Suppress the tag assignment when not Tagged_Type_Expansion
6456 -- because tags are represented implicitly in objects. Ditto for
6457 -- types that are CPP_CLASS, and for initializations that are
6458 -- aggregates, because they have to have the right tag.
6460 -- The re-assignment of the tag has to be done even if the object
6461 -- is a constant. The assignment must be analyzed after the
6462 -- declaration. If an address clause follows, this is handled as
6463 -- part of the freeze actions for the object, otherwise insert
6464 -- tag assignment here.
6466 Tag_Assign := Make_Tag_Assignment (N);
6468 if Present (Tag_Assign) then
6469 if Present (Following_Address_Clause (N)) then
6470 Ensure_Freeze_Node (Def_Id);
6472 else
6473 Insert_Action_After (Init_After, Tag_Assign);
6474 end if;
6476 -- Handle C++ constructor calls. Note that we do not check that
6477 -- Typ is a tagged type since the equivalent Ada type of a C++
6478 -- class that has no virtual methods is an untagged limited
6479 -- record type.
6481 elsif Is_CPP_Constructor_Call (Expr) then
6483 -- The call to the initialization procedure does NOT freeze the
6484 -- object being initialized.
6486 Id_Ref := New_Occurrence_Of (Def_Id, Loc);
6487 Set_Must_Not_Freeze (Id_Ref);
6488 Set_Assignment_OK (Id_Ref);
6490 Insert_Actions_After (Init_After,
6491 Build_Initialization_Call (Loc, Id_Ref, Typ,
6492 Constructor_Ref => Expr));
6494 -- We remove here the original call to the constructor
6495 -- to avoid its management in the backend
6497 Set_Expression (N, Empty);
6498 return;
6500 -- Handle initialization of limited tagged types
6502 elsif Is_Tagged_Type (Typ)
6503 and then Is_Class_Wide_Type (Typ)
6504 and then Is_Limited_Record (Typ)
6505 and then not Is_Limited_Interface (Typ)
6506 then
6507 -- Given that the type is limited we cannot perform a copy. If
6508 -- Expr_Q is the reference to a variable we mark the variable
6509 -- as OK_To_Rename to expand this declaration into a renaming
6510 -- declaration (see bellow).
6512 if Is_Entity_Name (Expr_Q) then
6513 Set_OK_To_Rename (Entity (Expr_Q));
6515 -- If we cannot convert the expression into a renaming we must
6516 -- consider it an internal error because the backend does not
6517 -- have support to handle it.
6519 else
6520 pragma Assert (False);
6521 raise Program_Error;
6522 end if;
6524 -- For discrete types, set the Is_Known_Valid flag if the
6525 -- initializing value is known to be valid. Only do this for
6526 -- source assignments, since otherwise we can end up turning
6527 -- on the known valid flag prematurely from inserted code.
6529 elsif Comes_From_Source (N)
6530 and then Is_Discrete_Type (Typ)
6531 and then Expr_Known_Valid (Expr)
6532 then
6533 Set_Is_Known_Valid (Def_Id);
6535 elsif Is_Access_Type (Typ) then
6537 -- For access types set the Is_Known_Non_Null flag if the
6538 -- initializing value is known to be non-null. We can also set
6539 -- Can_Never_Be_Null if this is a constant.
6541 if Known_Non_Null (Expr) then
6542 Set_Is_Known_Non_Null (Def_Id, True);
6544 if Constant_Present (N) then
6545 Set_Can_Never_Be_Null (Def_Id);
6546 end if;
6547 end if;
6548 end if;
6550 -- If validity checking on copies, validate initial expression.
6551 -- But skip this if declaration is for a generic type, since it
6552 -- makes no sense to validate generic types. Not clear if this
6553 -- can happen for legal programs, but it definitely can arise
6554 -- from previous instantiation errors.
6556 if Validity_Checks_On
6557 and then Comes_From_Source (N)
6558 and then Validity_Check_Copies
6559 and then not Is_Generic_Type (Etype (Def_Id))
6560 then
6561 Ensure_Valid (Expr);
6562 Set_Is_Known_Valid (Def_Id);
6563 end if;
6564 end if;
6566 -- Cases where the back end cannot handle the initialization directly
6567 -- In such cases, we expand an assignment that will be appropriately
6568 -- handled by Expand_N_Assignment_Statement.
6570 -- The exclusion of the unconstrained case is wrong, but for now it
6571 -- is too much trouble ???
6573 if (Is_Possibly_Unaligned_Slice (Expr)
6574 or else (Is_Possibly_Unaligned_Object (Expr)
6575 and then not Represented_As_Scalar (Etype (Expr))))
6576 and then not (Is_Array_Type (Etype (Expr))
6577 and then not Is_Constrained (Etype (Expr)))
6578 then
6579 declare
6580 Stat : constant Node_Id :=
6581 Make_Assignment_Statement (Loc,
6582 Name => New_Occurrence_Of (Def_Id, Loc),
6583 Expression => Relocate_Node (Expr));
6584 begin
6585 Set_Expression (N, Empty);
6586 Set_No_Initialization (N);
6587 Set_Assignment_OK (Name (Stat));
6588 Set_No_Ctrl_Actions (Stat);
6589 Insert_After_And_Analyze (Init_After, Stat);
6590 end;
6591 end if;
6592 end if;
6594 if Nkind (Obj_Def) = N_Access_Definition
6595 and then not Is_Local_Anonymous_Access (Etype (Def_Id))
6596 then
6597 -- An Ada 2012 stand-alone object of an anonymous access type
6599 declare
6600 Loc : constant Source_Ptr := Sloc (N);
6602 Level : constant Entity_Id :=
6603 Make_Defining_Identifier (Sloc (N),
6604 Chars =>
6605 New_External_Name (Chars (Def_Id), Suffix => "L"));
6607 Level_Expr : Node_Id;
6608 Level_Decl : Node_Id;
6610 begin
6611 Set_Ekind (Level, Ekind (Def_Id));
6612 Set_Etype (Level, Standard_Natural);
6613 Set_Scope (Level, Scope (Def_Id));
6615 if No (Expr) then
6617 -- Set accessibility level of null
6619 Level_Expr :=
6620 Make_Integer_Literal (Loc, Scope_Depth (Standard_Standard));
6622 else
6623 Level_Expr := Dynamic_Accessibility_Level (Expr);
6624 end if;
6626 Level_Decl :=
6627 Make_Object_Declaration (Loc,
6628 Defining_Identifier => Level,
6629 Object_Definition =>
6630 New_Occurrence_Of (Standard_Natural, Loc),
6631 Expression => Level_Expr,
6632 Constant_Present => Constant_Present (N),
6633 Has_Init_Expression => True);
6635 Insert_Action_After (Init_After, Level_Decl);
6637 Set_Extra_Accessibility (Def_Id, Level);
6638 end;
6639 end if;
6641 -- If the object is default initialized and its type is subject to
6642 -- pragma Default_Initial_Condition, add a runtime check to verify
6643 -- the assumption of the pragma (SPARK RM 7.3.3). Generate:
6645 -- <Base_Typ>DIC (<Base_Typ> (Def_Id));
6647 -- Note that the check is generated for source objects only
6649 if Comes_From_Source (Def_Id)
6650 and then Has_DIC (Typ)
6651 and then Present (DIC_Procedure (Typ))
6652 and then not Has_Init_Expression (N)
6653 then
6654 declare
6655 DIC_Call : constant Node_Id := Build_DIC_Call (Loc, Def_Id, Typ);
6657 begin
6658 if Present (Next_N) then
6659 Insert_Before_And_Analyze (Next_N, DIC_Call);
6661 -- The object declaration is the last node in a declarative or a
6662 -- statement list.
6664 else
6665 Append_To (List_Containing (N), DIC_Call);
6666 Analyze (DIC_Call);
6667 end if;
6668 end;
6669 end if;
6671 -- Final transformation - turn the object declaration into a renaming
6672 -- if appropriate. If this is the completion of a deferred constant
6673 -- declaration, then this transformation generates what would be
6674 -- illegal code if written by hand, but that's OK.
6676 if Present (Expr) then
6677 if Rewrite_As_Renaming then
6678 Rewrite (N,
6679 Make_Object_Renaming_Declaration (Loc,
6680 Defining_Identifier => Defining_Identifier (N),
6681 Subtype_Mark => Obj_Def,
6682 Name => Expr_Q));
6684 -- We do not analyze this renaming declaration, because all its
6685 -- components have already been analyzed, and if we were to go
6686 -- ahead and analyze it, we would in effect be trying to generate
6687 -- another declaration of X, which won't do.
6689 Set_Renamed_Object (Defining_Identifier (N), Expr_Q);
6690 Set_Analyzed (N);
6692 -- We do need to deal with debug issues for this renaming
6694 -- First, if entity comes from source, then mark it as needing
6695 -- debug information, even though it is defined by a generated
6696 -- renaming that does not come from source.
6698 if Comes_From_Source (Defining_Identifier (N)) then
6699 Set_Debug_Info_Needed (Defining_Identifier (N));
6700 end if;
6702 -- Now call the routine to generate debug info for the renaming
6704 declare
6705 Decl : constant Node_Id := Debug_Renaming_Declaration (N);
6706 begin
6707 if Present (Decl) then
6708 Insert_Action (N, Decl);
6709 end if;
6710 end;
6711 end if;
6712 end if;
6714 -- Exception on library entity not available
6716 exception
6717 when RE_Not_Available =>
6718 return;
6719 end Expand_N_Object_Declaration;
6721 ---------------------------------
6722 -- Expand_N_Subtype_Indication --
6723 ---------------------------------
6725 -- Add a check on the range of the subtype. The static case is partially
6726 -- duplicated by Process_Range_Expr_In_Decl in Sem_Ch3, but we still need
6727 -- to check here for the static case in order to avoid generating
6728 -- extraneous expanded code. Also deal with validity checking.
6730 procedure Expand_N_Subtype_Indication (N : Node_Id) is
6731 Ran : constant Node_Id := Range_Expression (Constraint (N));
6732 Typ : constant Entity_Id := Entity (Subtype_Mark (N));
6734 begin
6735 if Nkind (Constraint (N)) = N_Range_Constraint then
6736 Validity_Check_Range (Range_Expression (Constraint (N)));
6737 end if;
6739 if Nkind_In (Parent (N), N_Constrained_Array_Definition, N_Slice) then
6740 Apply_Range_Check (Ran, Typ);
6741 end if;
6742 end Expand_N_Subtype_Indication;
6744 ---------------------------
6745 -- Expand_N_Variant_Part --
6746 ---------------------------
6748 -- Note: this procedure no longer has any effect. It used to be that we
6749 -- would replace the choices in the last variant by a when others, and
6750 -- also expanded static predicates in variant choices here, but both of
6751 -- those activities were being done too early, since we can't check the
6752 -- choices until the statically predicated subtypes are frozen, which can
6753 -- happen as late as the free point of the record, and we can't change the
6754 -- last choice to an others before checking the choices, which is now done
6755 -- at the freeze point of the record.
6757 procedure Expand_N_Variant_Part (N : Node_Id) is
6758 begin
6759 null;
6760 end Expand_N_Variant_Part;
6762 ---------------------------------
6763 -- Expand_Previous_Access_Type --
6764 ---------------------------------
6766 procedure Expand_Previous_Access_Type (Def_Id : Entity_Id) is
6767 Ptr_Typ : Entity_Id;
6769 begin
6770 -- Find all access types in the current scope whose designated type is
6771 -- Def_Id and build master renamings for them.
6773 Ptr_Typ := First_Entity (Current_Scope);
6774 while Present (Ptr_Typ) loop
6775 if Is_Access_Type (Ptr_Typ)
6776 and then Designated_Type (Ptr_Typ) = Def_Id
6777 and then No (Master_Id (Ptr_Typ))
6778 then
6779 -- Ensure that the designated type has a master
6781 Build_Master_Entity (Def_Id);
6783 -- Private and incomplete types complicate the insertion of master
6784 -- renamings because the access type may precede the full view of
6785 -- the designated type. For this reason, the master renamings are
6786 -- inserted relative to the designated type.
6788 Build_Master_Renaming (Ptr_Typ, Ins_Nod => Parent (Def_Id));
6789 end if;
6791 Next_Entity (Ptr_Typ);
6792 end loop;
6793 end Expand_Previous_Access_Type;
6795 -----------------------------
6796 -- Expand_Record_Extension --
6797 -----------------------------
6799 -- Add a field _parent at the beginning of the record extension. This is
6800 -- used to implement inheritance. Here are some examples of expansion:
6802 -- 1. no discriminants
6803 -- type T2 is new T1 with null record;
6804 -- gives
6805 -- type T2 is new T1 with record
6806 -- _Parent : T1;
6807 -- end record;
6809 -- 2. renamed discriminants
6810 -- type T2 (B, C : Int) is new T1 (A => B) with record
6811 -- _Parent : T1 (A => B);
6812 -- D : Int;
6813 -- end;
6815 -- 3. inherited discriminants
6816 -- type T2 is new T1 with record -- discriminant A inherited
6817 -- _Parent : T1 (A);
6818 -- D : Int;
6819 -- end;
6821 procedure Expand_Record_Extension (T : Entity_Id; Def : Node_Id) is
6822 Indic : constant Node_Id := Subtype_Indication (Def);
6823 Loc : constant Source_Ptr := Sloc (Def);
6824 Rec_Ext_Part : Node_Id := Record_Extension_Part (Def);
6825 Par_Subtype : Entity_Id;
6826 Comp_List : Node_Id;
6827 Comp_Decl : Node_Id;
6828 Parent_N : Node_Id;
6829 D : Entity_Id;
6830 List_Constr : constant List_Id := New_List;
6832 begin
6833 -- Expand_Record_Extension is called directly from the semantics, so
6834 -- we must check to see whether expansion is active before proceeding,
6835 -- because this affects the visibility of selected components in bodies
6836 -- of instances.
6838 if not Expander_Active then
6839 return;
6840 end if;
6842 -- This may be a derivation of an untagged private type whose full
6843 -- view is tagged, in which case the Derived_Type_Definition has no
6844 -- extension part. Build an empty one now.
6846 if No (Rec_Ext_Part) then
6847 Rec_Ext_Part :=
6848 Make_Record_Definition (Loc,
6849 End_Label => Empty,
6850 Component_List => Empty,
6851 Null_Present => True);
6853 Set_Record_Extension_Part (Def, Rec_Ext_Part);
6854 Mark_Rewrite_Insertion (Rec_Ext_Part);
6855 end if;
6857 Comp_List := Component_List (Rec_Ext_Part);
6859 Parent_N := Make_Defining_Identifier (Loc, Name_uParent);
6861 -- If the derived type inherits its discriminants the type of the
6862 -- _parent field must be constrained by the inherited discriminants
6864 if Has_Discriminants (T)
6865 and then Nkind (Indic) /= N_Subtype_Indication
6866 and then not Is_Constrained (Entity (Indic))
6867 then
6868 D := First_Discriminant (T);
6869 while Present (D) loop
6870 Append_To (List_Constr, New_Occurrence_Of (D, Loc));
6871 Next_Discriminant (D);
6872 end loop;
6874 Par_Subtype :=
6875 Process_Subtype (
6876 Make_Subtype_Indication (Loc,
6877 Subtype_Mark => New_Occurrence_Of (Entity (Indic), Loc),
6878 Constraint =>
6879 Make_Index_Or_Discriminant_Constraint (Loc,
6880 Constraints => List_Constr)),
6881 Def);
6883 -- Otherwise the original subtype_indication is just what is needed
6885 else
6886 Par_Subtype := Process_Subtype (New_Copy_Tree (Indic), Def);
6887 end if;
6889 Set_Parent_Subtype (T, Par_Subtype);
6891 Comp_Decl :=
6892 Make_Component_Declaration (Loc,
6893 Defining_Identifier => Parent_N,
6894 Component_Definition =>
6895 Make_Component_Definition (Loc,
6896 Aliased_Present => False,
6897 Subtype_Indication => New_Occurrence_Of (Par_Subtype, Loc)));
6899 if Null_Present (Rec_Ext_Part) then
6900 Set_Component_List (Rec_Ext_Part,
6901 Make_Component_List (Loc,
6902 Component_Items => New_List (Comp_Decl),
6903 Variant_Part => Empty,
6904 Null_Present => False));
6905 Set_Null_Present (Rec_Ext_Part, False);
6907 elsif Null_Present (Comp_List)
6908 or else Is_Empty_List (Component_Items (Comp_List))
6909 then
6910 Set_Component_Items (Comp_List, New_List (Comp_Decl));
6911 Set_Null_Present (Comp_List, False);
6913 else
6914 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
6915 end if;
6917 Analyze (Comp_Decl);
6918 end Expand_Record_Extension;
6920 ------------------------
6921 -- Expand_Tagged_Root --
6922 ------------------------
6924 procedure Expand_Tagged_Root (T : Entity_Id) is
6925 Def : constant Node_Id := Type_Definition (Parent (T));
6926 Comp_List : Node_Id;
6927 Comp_Decl : Node_Id;
6928 Sloc_N : Source_Ptr;
6930 begin
6931 if Null_Present (Def) then
6932 Set_Component_List (Def,
6933 Make_Component_List (Sloc (Def),
6934 Component_Items => Empty_List,
6935 Variant_Part => Empty,
6936 Null_Present => True));
6937 end if;
6939 Comp_List := Component_List (Def);
6941 if Null_Present (Comp_List)
6942 or else Is_Empty_List (Component_Items (Comp_List))
6943 then
6944 Sloc_N := Sloc (Comp_List);
6945 else
6946 Sloc_N := Sloc (First (Component_Items (Comp_List)));
6947 end if;
6949 Comp_Decl :=
6950 Make_Component_Declaration (Sloc_N,
6951 Defining_Identifier => First_Tag_Component (T),
6952 Component_Definition =>
6953 Make_Component_Definition (Sloc_N,
6954 Aliased_Present => False,
6955 Subtype_Indication => New_Occurrence_Of (RTE (RE_Tag), Sloc_N)));
6957 if Null_Present (Comp_List)
6958 or else Is_Empty_List (Component_Items (Comp_List))
6959 then
6960 Set_Component_Items (Comp_List, New_List (Comp_Decl));
6961 Set_Null_Present (Comp_List, False);
6963 else
6964 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
6965 end if;
6967 -- We don't Analyze the whole expansion because the tag component has
6968 -- already been analyzed previously. Here we just insure that the tree
6969 -- is coherent with the semantic decoration
6971 Find_Type (Subtype_Indication (Component_Definition (Comp_Decl)));
6973 exception
6974 when RE_Not_Available =>
6975 return;
6976 end Expand_Tagged_Root;
6978 ------------------------------
6979 -- Freeze_Stream_Operations --
6980 ------------------------------
6982 procedure Freeze_Stream_Operations (N : Node_Id; Typ : Entity_Id) is
6983 Names : constant array (1 .. 4) of TSS_Name_Type :=
6984 (TSS_Stream_Input,
6985 TSS_Stream_Output,
6986 TSS_Stream_Read,
6987 TSS_Stream_Write);
6988 Stream_Op : Entity_Id;
6990 begin
6991 -- Primitive operations of tagged types are frozen when the dispatch
6992 -- table is constructed.
6994 if not Comes_From_Source (Typ) or else Is_Tagged_Type (Typ) then
6995 return;
6996 end if;
6998 for J in Names'Range loop
6999 Stream_Op := TSS (Typ, Names (J));
7001 if Present (Stream_Op)
7002 and then Is_Subprogram (Stream_Op)
7003 and then Nkind (Unit_Declaration_Node (Stream_Op)) =
7004 N_Subprogram_Declaration
7005 and then not Is_Frozen (Stream_Op)
7006 then
7007 Append_Freeze_Actions (Typ, Freeze_Entity (Stream_Op, N));
7008 end if;
7009 end loop;
7010 end Freeze_Stream_Operations;
7012 -----------------
7013 -- Freeze_Type --
7014 -----------------
7016 -- Full type declarations are expanded at the point at which the type is
7017 -- frozen. The formal N is the Freeze_Node for the type. Any statements or
7018 -- declarations generated by the freezing (e.g. the procedure generated
7019 -- for initialization) are chained in the Actions field list of the freeze
7020 -- node using Append_Freeze_Actions.
7022 -- WARNING: This routine manages Ghost regions. Return statements must be
7023 -- replaced by gotos which jump to the end of the routine and restore the
7024 -- Ghost mode.
7026 function Freeze_Type (N : Node_Id) return Boolean is
7027 procedure Process_RACW_Types (Typ : Entity_Id);
7028 -- Validate and generate stubs for all RACW types associated with type
7029 -- Typ.
7031 procedure Process_Pending_Access_Types (Typ : Entity_Id);
7032 -- Associate type Typ's Finalize_Address primitive with the finalization
7033 -- masters of pending access-to-Typ types.
7035 ------------------------
7036 -- Process_RACW_Types --
7037 ------------------------
7039 procedure Process_RACW_Types (Typ : Entity_Id) is
7040 List : constant Elist_Id := Access_Types_To_Process (N);
7041 E : Elmt_Id;
7042 Seen : Boolean := False;
7044 begin
7045 if Present (List) then
7046 E := First_Elmt (List);
7047 while Present (E) loop
7048 if Is_Remote_Access_To_Class_Wide_Type (Node (E)) then
7049 Validate_RACW_Primitives (Node (E));
7050 Seen := True;
7051 end if;
7053 Next_Elmt (E);
7054 end loop;
7055 end if;
7057 -- If there are RACWs designating this type, make stubs now
7059 if Seen then
7060 Remote_Types_Tagged_Full_View_Encountered (Typ);
7061 end if;
7062 end Process_RACW_Types;
7064 ----------------------------------
7065 -- Process_Pending_Access_Types --
7066 ----------------------------------
7068 procedure Process_Pending_Access_Types (Typ : Entity_Id) is
7069 E : Elmt_Id;
7071 begin
7072 -- Finalize_Address is not generated in CodePeer mode because the
7073 -- body contains address arithmetic. This processing is disabled.
7075 if CodePeer_Mode then
7076 null;
7078 -- Certain itypes are generated for contexts that cannot allocate
7079 -- objects and should not set primitive Finalize_Address.
7081 elsif Is_Itype (Typ)
7082 and then Nkind (Associated_Node_For_Itype (Typ)) =
7083 N_Explicit_Dereference
7084 then
7085 null;
7087 -- When an access type is declared after the incomplete view of a
7088 -- Taft-amendment type, the access type is considered pending in
7089 -- case the full view of the Taft-amendment type is controlled. If
7090 -- this is indeed the case, associate the Finalize_Address routine
7091 -- of the full view with the finalization masters of all pending
7092 -- access types. This scenario applies to anonymous access types as
7093 -- well.
7095 elsif Needs_Finalization (Typ)
7096 and then Present (Pending_Access_Types (Typ))
7097 then
7098 E := First_Elmt (Pending_Access_Types (Typ));
7099 while Present (E) loop
7101 -- Generate:
7102 -- Set_Finalize_Address
7103 -- (Ptr_Typ, <Typ>FD'Unrestricted_Access);
7105 Append_Freeze_Action (Typ,
7106 Make_Set_Finalize_Address_Call
7107 (Loc => Sloc (N),
7108 Ptr_Typ => Node (E)));
7110 Next_Elmt (E);
7111 end loop;
7112 end if;
7113 end Process_Pending_Access_Types;
7115 -- Local variables
7117 Def_Id : constant Entity_Id := Entity (N);
7119 Mode : Ghost_Mode_Type;
7120 Mode_Set : Boolean := False;
7121 Result : Boolean := False;
7123 -- Start of processing for Freeze_Type
7125 begin
7126 -- The type being frozen may be subject to pragma Ghost. Set the mode
7127 -- now to ensure that any nodes generated during freezing are properly
7128 -- marked as Ghost.
7130 Set_Ghost_Mode (Def_Id, Mode);
7131 Mode_Set := True;
7133 -- Process any remote access-to-class-wide types designating the type
7134 -- being frozen.
7136 Process_RACW_Types (Def_Id);
7138 -- Freeze processing for record types
7140 if Is_Record_Type (Def_Id) then
7141 if Ekind (Def_Id) = E_Record_Type then
7142 Expand_Freeze_Record_Type (N);
7143 elsif Is_Class_Wide_Type (Def_Id) then
7144 Expand_Freeze_Class_Wide_Type (N);
7145 end if;
7147 -- Freeze processing for array types
7149 elsif Is_Array_Type (Def_Id) then
7150 Expand_Freeze_Array_Type (N);
7152 -- Freeze processing for access types
7154 -- For pool-specific access types, find out the pool object used for
7155 -- this type, needs actual expansion of it in some cases. Here are the
7156 -- different cases :
7158 -- 1. Rep Clause "for Def_Id'Storage_Size use 0;"
7159 -- ---> don't use any storage pool
7161 -- 2. Rep Clause : for Def_Id'Storage_Size use Expr.
7162 -- Expand:
7163 -- Def_Id__Pool : Stack_Bounded_Pool (Expr, DT'Size, DT'Alignment);
7165 -- 3. Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
7166 -- ---> Storage Pool is the specified one
7168 -- See GNAT Pool packages in the Run-Time for more details
7170 elsif Ekind_In (Def_Id, E_Access_Type, E_General_Access_Type) then
7171 declare
7172 Loc : constant Source_Ptr := Sloc (N);
7173 Desig_Type : constant Entity_Id := Designated_Type (Def_Id);
7175 Freeze_Action_Typ : Entity_Id;
7176 Pool_Object : Entity_Id;
7178 begin
7179 -- Case 1
7181 -- Rep Clause "for Def_Id'Storage_Size use 0;"
7182 -- ---> don't use any storage pool
7184 if No_Pool_Assigned (Def_Id) then
7185 null;
7187 -- Case 2
7189 -- Rep Clause : for Def_Id'Storage_Size use Expr.
7190 -- ---> Expand:
7191 -- Def_Id__Pool : Stack_Bounded_Pool
7192 -- (Expr, DT'Size, DT'Alignment);
7194 elsif Has_Storage_Size_Clause (Def_Id) then
7195 declare
7196 DT_Align : Node_Id;
7197 DT_Size : Node_Id;
7199 begin
7200 -- For unconstrained composite types we give a size of zero
7201 -- so that the pool knows that it needs a special algorithm
7202 -- for variable size object allocation.
7204 if Is_Composite_Type (Desig_Type)
7205 and then not Is_Constrained (Desig_Type)
7206 then
7207 DT_Size := Make_Integer_Literal (Loc, 0);
7208 DT_Align := Make_Integer_Literal (Loc, Maximum_Alignment);
7210 else
7211 DT_Size :=
7212 Make_Attribute_Reference (Loc,
7213 Prefix => New_Occurrence_Of (Desig_Type, Loc),
7214 Attribute_Name => Name_Max_Size_In_Storage_Elements);
7216 DT_Align :=
7217 Make_Attribute_Reference (Loc,
7218 Prefix => New_Occurrence_Of (Desig_Type, Loc),
7219 Attribute_Name => Name_Alignment);
7220 end if;
7222 Pool_Object :=
7223 Make_Defining_Identifier (Loc,
7224 Chars => New_External_Name (Chars (Def_Id), 'P'));
7226 -- We put the code associated with the pools in the entity
7227 -- that has the later freeze node, usually the access type
7228 -- but it can also be the designated_type; because the pool
7229 -- code requires both those types to be frozen
7231 if Is_Frozen (Desig_Type)
7232 and then (No (Freeze_Node (Desig_Type))
7233 or else Analyzed (Freeze_Node (Desig_Type)))
7234 then
7235 Freeze_Action_Typ := Def_Id;
7237 -- A Taft amendment type cannot get the freeze actions
7238 -- since the full view is not there.
7240 elsif Is_Incomplete_Or_Private_Type (Desig_Type)
7241 and then No (Full_View (Desig_Type))
7242 then
7243 Freeze_Action_Typ := Def_Id;
7245 else
7246 Freeze_Action_Typ := Desig_Type;
7247 end if;
7249 Append_Freeze_Action (Freeze_Action_Typ,
7250 Make_Object_Declaration (Loc,
7251 Defining_Identifier => Pool_Object,
7252 Object_Definition =>
7253 Make_Subtype_Indication (Loc,
7254 Subtype_Mark =>
7255 New_Occurrence_Of
7256 (RTE (RE_Stack_Bounded_Pool), Loc),
7258 Constraint =>
7259 Make_Index_Or_Discriminant_Constraint (Loc,
7260 Constraints => New_List (
7262 -- First discriminant is the Pool Size
7264 New_Occurrence_Of (
7265 Storage_Size_Variable (Def_Id), Loc),
7267 -- Second discriminant is the element size
7269 DT_Size,
7271 -- Third discriminant is the alignment
7273 DT_Align)))));
7274 end;
7276 Set_Associated_Storage_Pool (Def_Id, Pool_Object);
7278 -- Case 3
7280 -- Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
7281 -- ---> Storage Pool is the specified one
7283 -- When compiling in Ada 2012 mode, ensure that the accessibility
7284 -- level of the subpool access type is not deeper than that of the
7285 -- pool_with_subpools.
7287 elsif Ada_Version >= Ada_2012
7288 and then Present (Associated_Storage_Pool (Def_Id))
7290 -- Omit this check for the case of a configurable run-time that
7291 -- does not provide package System.Storage_Pools.Subpools.
7293 and then RTE_Available (RE_Root_Storage_Pool_With_Subpools)
7294 then
7295 declare
7296 Loc : constant Source_Ptr := Sloc (Def_Id);
7297 Pool : constant Entity_Id :=
7298 Associated_Storage_Pool (Def_Id);
7299 RSPWS : constant Entity_Id :=
7300 RTE (RE_Root_Storage_Pool_With_Subpools);
7302 begin
7303 -- It is known that the accessibility level of the access
7304 -- type is deeper than that of the pool.
7306 if Type_Access_Level (Def_Id) > Object_Access_Level (Pool)
7307 and then not Accessibility_Checks_Suppressed (Def_Id)
7308 and then not Accessibility_Checks_Suppressed (Pool)
7309 then
7310 -- Static case: the pool is known to be a descendant of
7311 -- Root_Storage_Pool_With_Subpools.
7313 if Is_Ancestor (RSPWS, Etype (Pool)) then
7314 Error_Msg_N
7315 ("??subpool access type has deeper accessibility "
7316 & "level than pool", Def_Id);
7318 Append_Freeze_Action (Def_Id,
7319 Make_Raise_Program_Error (Loc,
7320 Reason => PE_Accessibility_Check_Failed));
7322 -- Dynamic case: when the pool is of a class-wide type,
7323 -- it may or may not support subpools depending on the
7324 -- path of derivation. Generate:
7326 -- if Def_Id in RSPWS'Class then
7327 -- raise Program_Error;
7328 -- end if;
7330 elsif Is_Class_Wide_Type (Etype (Pool)) then
7331 Append_Freeze_Action (Def_Id,
7332 Make_If_Statement (Loc,
7333 Condition =>
7334 Make_In (Loc,
7335 Left_Opnd => New_Occurrence_Of (Pool, Loc),
7336 Right_Opnd =>
7337 New_Occurrence_Of
7338 (Class_Wide_Type (RSPWS), Loc)),
7340 Then_Statements => New_List (
7341 Make_Raise_Program_Error (Loc,
7342 Reason => PE_Accessibility_Check_Failed))));
7343 end if;
7344 end if;
7345 end;
7346 end if;
7348 -- For access-to-controlled types (including class-wide types and
7349 -- Taft-amendment types, which potentially have controlled
7350 -- components), expand the list controller object that will store
7351 -- the dynamically allocated objects. Don't do this transformation
7352 -- for expander-generated access types, but do it for types that
7353 -- are the full view of types derived from other private types.
7354 -- Also suppress the list controller in the case of a designated
7355 -- type with convention Java, since this is used when binding to
7356 -- Java API specs, where there's no equivalent of a finalization
7357 -- list and we don't want to pull in the finalization support if
7358 -- not needed.
7360 if not Comes_From_Source (Def_Id)
7361 and then not Has_Private_Declaration (Def_Id)
7362 then
7363 null;
7365 -- An exception is made for types defined in the run-time because
7366 -- Ada.Tags.Tag itself is such a type and cannot afford this
7367 -- unnecessary overhead that would generates a loop in the
7368 -- expansion scheme. Another exception is if Restrictions
7369 -- (No_Finalization) is active, since then we know nothing is
7370 -- controlled.
7372 elsif Restriction_Active (No_Finalization)
7373 or else In_Runtime (Def_Id)
7374 then
7375 null;
7377 -- Create a finalization master for an access-to-controlled type
7378 -- or an access-to-incomplete type. It is assumed that the full
7379 -- view will be controlled.
7381 elsif Needs_Finalization (Desig_Type)
7382 or else (Is_Incomplete_Type (Desig_Type)
7383 and then No (Full_View (Desig_Type)))
7384 then
7385 Build_Finalization_Master (Def_Id);
7387 -- Create a finalization master when the designated type contains
7388 -- a private component. It is assumed that the full view will be
7389 -- controlled.
7391 elsif Has_Private_Component (Desig_Type) then
7392 Build_Finalization_Master
7393 (Typ => Def_Id,
7394 For_Private => True,
7395 Context_Scope => Scope (Def_Id),
7396 Insertion_Node => Declaration_Node (Desig_Type));
7397 end if;
7398 end;
7400 -- Freeze processing for enumeration types
7402 elsif Ekind (Def_Id) = E_Enumeration_Type then
7404 -- We only have something to do if we have a non-standard
7405 -- representation (i.e. at least one literal whose pos value
7406 -- is not the same as its representation)
7408 if Has_Non_Standard_Rep (Def_Id) then
7409 Expand_Freeze_Enumeration_Type (N);
7410 end if;
7412 -- Private types that are completed by a derivation from a private
7413 -- type have an internally generated full view, that needs to be
7414 -- frozen. This must be done explicitly because the two views share
7415 -- the freeze node, and the underlying full view is not visible when
7416 -- the freeze node is analyzed.
7418 elsif Is_Private_Type (Def_Id)
7419 and then Is_Derived_Type (Def_Id)
7420 and then Present (Full_View (Def_Id))
7421 and then Is_Itype (Full_View (Def_Id))
7422 and then Has_Private_Declaration (Full_View (Def_Id))
7423 and then Freeze_Node (Full_View (Def_Id)) = N
7424 then
7425 Set_Entity (N, Full_View (Def_Id));
7426 Result := Freeze_Type (N);
7427 Set_Entity (N, Def_Id);
7429 -- All other types require no expander action. There are such cases
7430 -- (e.g. task types and protected types). In such cases, the freeze
7431 -- nodes are there for use by Gigi.
7433 end if;
7435 -- Complete the initialization of all pending access types' finalization
7436 -- masters now that the designated type has been is frozen and primitive
7437 -- Finalize_Address generated.
7439 Process_Pending_Access_Types (Def_Id);
7440 Freeze_Stream_Operations (N, Def_Id);
7442 -- Generate the [spec and] body of the procedure tasked with the runtime
7443 -- verification of pragma Default_Initial_Condition's expression.
7445 if Has_DIC (Def_Id) then
7446 Build_DIC_Procedure_Body (Def_Id);
7447 end if;
7449 -- Generate the [spec and] body of the invariant procedure tasked with
7450 -- the runtime verification of all invariants that pertain to the type.
7451 -- This includes invariants on the partial and full view, inherited
7452 -- class-wide invariants from parent types or interfaces, and invariants
7453 -- on array elements or record components.
7455 if Has_Invariants (Def_Id) then
7456 Build_Invariant_Procedure_Body (Def_Id);
7457 end if;
7459 if Mode_Set then
7460 Restore_Ghost_Mode (Mode);
7461 end if;
7463 return Result;
7465 exception
7466 when RE_Not_Available =>
7467 if Mode_Set then
7468 Restore_Ghost_Mode (Mode);
7469 end if;
7471 return False;
7472 end Freeze_Type;
7474 -------------------------
7475 -- Get_Simple_Init_Val --
7476 -------------------------
7478 function Get_Simple_Init_Val
7479 (T : Entity_Id;
7480 N : Node_Id;
7481 Size : Uint := No_Uint) return Node_Id
7483 Loc : constant Source_Ptr := Sloc (N);
7484 Val : Node_Id;
7485 Result : Node_Id;
7486 Val_RE : RE_Id;
7488 Size_To_Use : Uint;
7489 -- This is the size to be used for computation of the appropriate
7490 -- initial value for the Normalize_Scalars and Initialize_Scalars case.
7492 IV_Attribute : constant Boolean :=
7493 Nkind (N) = N_Attribute_Reference
7494 and then Attribute_Name (N) = Name_Invalid_Value;
7496 Lo_Bound : Uint;
7497 Hi_Bound : Uint;
7498 -- These are the values computed by the procedure Check_Subtype_Bounds
7500 procedure Check_Subtype_Bounds;
7501 -- This procedure examines the subtype T, and its ancestor subtypes and
7502 -- derived types to determine the best known information about the
7503 -- bounds of the subtype. After the call Lo_Bound is set either to
7504 -- No_Uint if no information can be determined, or to a value which
7505 -- represents a known low bound, i.e. a valid value of the subtype can
7506 -- not be less than this value. Hi_Bound is similarly set to a known
7507 -- high bound (valid value cannot be greater than this).
7509 --------------------------
7510 -- Check_Subtype_Bounds --
7511 --------------------------
7513 procedure Check_Subtype_Bounds is
7514 ST1 : Entity_Id;
7515 ST2 : Entity_Id;
7516 Lo : Node_Id;
7517 Hi : Node_Id;
7518 Loval : Uint;
7519 Hival : Uint;
7521 begin
7522 Lo_Bound := No_Uint;
7523 Hi_Bound := No_Uint;
7525 -- Loop to climb ancestor subtypes and derived types
7527 ST1 := T;
7528 loop
7529 if not Is_Discrete_Type (ST1) then
7530 return;
7531 end if;
7533 Lo := Type_Low_Bound (ST1);
7534 Hi := Type_High_Bound (ST1);
7536 if Compile_Time_Known_Value (Lo) then
7537 Loval := Expr_Value (Lo);
7539 if Lo_Bound = No_Uint or else Lo_Bound < Loval then
7540 Lo_Bound := Loval;
7541 end if;
7542 end if;
7544 if Compile_Time_Known_Value (Hi) then
7545 Hival := Expr_Value (Hi);
7547 if Hi_Bound = No_Uint or else Hi_Bound > Hival then
7548 Hi_Bound := Hival;
7549 end if;
7550 end if;
7552 ST2 := Ancestor_Subtype (ST1);
7554 if No (ST2) then
7555 ST2 := Etype (ST1);
7556 end if;
7558 exit when ST1 = ST2;
7559 ST1 := ST2;
7560 end loop;
7561 end Check_Subtype_Bounds;
7563 -- Start of processing for Get_Simple_Init_Val
7565 begin
7566 -- For a private type, we should always have an underlying type (because
7567 -- this was already checked in Needs_Simple_Initialization). What we do
7568 -- is to get the value for the underlying type and then do an unchecked
7569 -- conversion to the private type.
7571 if Is_Private_Type (T) then
7572 Val := Get_Simple_Init_Val (Underlying_Type (T), N, Size);
7574 -- A special case, if the underlying value is null, then qualify it
7575 -- with the underlying type, so that the null is properly typed.
7576 -- Similarly, if it is an aggregate it must be qualified, because an
7577 -- unchecked conversion does not provide a context for it.
7579 if Nkind_In (Val, N_Null, N_Aggregate) then
7580 Val :=
7581 Make_Qualified_Expression (Loc,
7582 Subtype_Mark =>
7583 New_Occurrence_Of (Underlying_Type (T), Loc),
7584 Expression => Val);
7585 end if;
7587 Result := Unchecked_Convert_To (T, Val);
7589 -- Don't truncate result (important for Initialize/Normalize_Scalars)
7591 if Nkind (Result) = N_Unchecked_Type_Conversion
7592 and then Is_Scalar_Type (Underlying_Type (T))
7593 then
7594 Set_No_Truncation (Result);
7595 end if;
7597 return Result;
7599 -- Scalars with Default_Value aspect. The first subtype may now be
7600 -- private, so retrieve value from underlying type.
7602 elsif Is_Scalar_Type (T) and then Has_Default_Aspect (T) then
7603 if Is_Private_Type (First_Subtype (T)) then
7604 return Unchecked_Convert_To (T,
7605 Default_Aspect_Value (Full_View (First_Subtype (T))));
7606 else
7607 return
7608 Convert_To (T, Default_Aspect_Value (First_Subtype (T)));
7609 end if;
7611 -- Otherwise, for scalars, we must have normalize/initialize scalars
7612 -- case, or if the node N is an 'Invalid_Value attribute node.
7614 elsif Is_Scalar_Type (T) then
7615 pragma Assert (Init_Or_Norm_Scalars or IV_Attribute);
7617 -- Compute size of object. If it is given by the caller, we can use
7618 -- it directly, otherwise we use Esize (T) as an estimate. As far as
7619 -- we know this covers all cases correctly.
7621 if Size = No_Uint or else Size <= Uint_0 then
7622 Size_To_Use := UI_Max (Uint_1, Esize (T));
7623 else
7624 Size_To_Use := Size;
7625 end if;
7627 -- Maximum size to use is 64 bits, since we will create values of
7628 -- type Unsigned_64 and the range must fit this type.
7630 if Size_To_Use /= No_Uint and then Size_To_Use > Uint_64 then
7631 Size_To_Use := Uint_64;
7632 end if;
7634 -- Check known bounds of subtype
7636 Check_Subtype_Bounds;
7638 -- Processing for Normalize_Scalars case
7640 if Normalize_Scalars and then not IV_Attribute then
7642 -- If zero is invalid, it is a convenient value to use that is
7643 -- for sure an appropriate invalid value in all situations.
7645 if Lo_Bound /= No_Uint and then Lo_Bound > Uint_0 then
7646 Val := Make_Integer_Literal (Loc, 0);
7648 -- Cases where all one bits is the appropriate invalid value
7650 -- For modular types, all 1 bits is either invalid or valid. If
7651 -- it is valid, then there is nothing that can be done since there
7652 -- are no invalid values (we ruled out zero already).
7654 -- For signed integer types that have no negative values, either
7655 -- there is room for negative values, or there is not. If there
7656 -- is, then all 1-bits may be interpreted as minus one, which is
7657 -- certainly invalid. Alternatively it is treated as the largest
7658 -- positive value, in which case the observation for modular types
7659 -- still applies.
7661 -- For float types, all 1-bits is a NaN (not a number), which is
7662 -- certainly an appropriately invalid value.
7664 elsif Is_Unsigned_Type (T)
7665 or else Is_Floating_Point_Type (T)
7666 or else Is_Enumeration_Type (T)
7667 then
7668 Val := Make_Integer_Literal (Loc, 2 ** Size_To_Use - 1);
7670 -- Resolve as Unsigned_64, because the largest number we can
7671 -- generate is out of range of universal integer.
7673 Analyze_And_Resolve (Val, RTE (RE_Unsigned_64));
7675 -- Case of signed types
7677 else
7678 declare
7679 Signed_Size : constant Uint :=
7680 UI_Min (Uint_63, Size_To_Use - 1);
7682 begin
7683 -- Normally we like to use the most negative number. The one
7684 -- exception is when this number is in the known subtype
7685 -- range and the largest positive number is not in the known
7686 -- subtype range.
7688 -- For this exceptional case, use largest positive value
7690 if Lo_Bound /= No_Uint and then Hi_Bound /= No_Uint
7691 and then Lo_Bound <= (-(2 ** Signed_Size))
7692 and then Hi_Bound < 2 ** Signed_Size
7693 then
7694 Val := Make_Integer_Literal (Loc, 2 ** Signed_Size - 1);
7696 -- Normal case of largest negative value
7698 else
7699 Val := Make_Integer_Literal (Loc, -(2 ** Signed_Size));
7700 end if;
7701 end;
7702 end if;
7704 -- Here for Initialize_Scalars case (or Invalid_Value attribute used)
7706 else
7707 -- For float types, use float values from System.Scalar_Values
7709 if Is_Floating_Point_Type (T) then
7710 if Root_Type (T) = Standard_Short_Float then
7711 Val_RE := RE_IS_Isf;
7712 elsif Root_Type (T) = Standard_Float then
7713 Val_RE := RE_IS_Ifl;
7714 elsif Root_Type (T) = Standard_Long_Float then
7715 Val_RE := RE_IS_Ilf;
7716 else pragma Assert (Root_Type (T) = Standard_Long_Long_Float);
7717 Val_RE := RE_IS_Ill;
7718 end if;
7720 -- If zero is invalid, use zero values from System.Scalar_Values
7722 elsif Lo_Bound /= No_Uint and then Lo_Bound > Uint_0 then
7723 if Size_To_Use <= 8 then
7724 Val_RE := RE_IS_Iz1;
7725 elsif Size_To_Use <= 16 then
7726 Val_RE := RE_IS_Iz2;
7727 elsif Size_To_Use <= 32 then
7728 Val_RE := RE_IS_Iz4;
7729 else
7730 Val_RE := RE_IS_Iz8;
7731 end if;
7733 -- For unsigned, use unsigned values from System.Scalar_Values
7735 elsif Is_Unsigned_Type (T) then
7736 if Size_To_Use <= 8 then
7737 Val_RE := RE_IS_Iu1;
7738 elsif Size_To_Use <= 16 then
7739 Val_RE := RE_IS_Iu2;
7740 elsif Size_To_Use <= 32 then
7741 Val_RE := RE_IS_Iu4;
7742 else
7743 Val_RE := RE_IS_Iu8;
7744 end if;
7746 -- For signed, use signed values from System.Scalar_Values
7748 else
7749 if Size_To_Use <= 8 then
7750 Val_RE := RE_IS_Is1;
7751 elsif Size_To_Use <= 16 then
7752 Val_RE := RE_IS_Is2;
7753 elsif Size_To_Use <= 32 then
7754 Val_RE := RE_IS_Is4;
7755 else
7756 Val_RE := RE_IS_Is8;
7757 end if;
7758 end if;
7760 Val := New_Occurrence_Of (RTE (Val_RE), Loc);
7761 end if;
7763 -- The final expression is obtained by doing an unchecked conversion
7764 -- of this result to the base type of the required subtype. Use the
7765 -- base type to prevent the unchecked conversion from chopping bits,
7766 -- and then we set Kill_Range_Check to preserve the "bad" value.
7768 Result := Unchecked_Convert_To (Base_Type (T), Val);
7770 -- Ensure result is not truncated, since we want the "bad" bits, and
7771 -- also kill range check on result.
7773 if Nkind (Result) = N_Unchecked_Type_Conversion then
7774 Set_No_Truncation (Result);
7775 Set_Kill_Range_Check (Result, True);
7776 end if;
7778 return Result;
7780 -- String or Wide_[Wide]_String (must have Initialize_Scalars set)
7782 elsif Is_Standard_String_Type (T) then
7783 pragma Assert (Init_Or_Norm_Scalars);
7785 return
7786 Make_Aggregate (Loc,
7787 Component_Associations => New_List (
7788 Make_Component_Association (Loc,
7789 Choices => New_List (
7790 Make_Others_Choice (Loc)),
7791 Expression =>
7792 Get_Simple_Init_Val
7793 (Component_Type (T), N, Esize (Root_Type (T))))));
7795 -- Access type is initialized to null
7797 elsif Is_Access_Type (T) then
7798 return Make_Null (Loc);
7800 -- No other possibilities should arise, since we should only be calling
7801 -- Get_Simple_Init_Val if Needs_Simple_Initialization returned True,
7802 -- indicating one of the above cases held.
7804 else
7805 raise Program_Error;
7806 end if;
7808 exception
7809 when RE_Not_Available =>
7810 return Empty;
7811 end Get_Simple_Init_Val;
7813 ------------------------------
7814 -- Has_New_Non_Standard_Rep --
7815 ------------------------------
7817 function Has_New_Non_Standard_Rep (T : Entity_Id) return Boolean is
7818 begin
7819 if not Is_Derived_Type (T) then
7820 return Has_Non_Standard_Rep (T)
7821 or else Has_Non_Standard_Rep (Root_Type (T));
7823 -- If Has_Non_Standard_Rep is not set on the derived type, the
7824 -- representation is fully inherited.
7826 elsif not Has_Non_Standard_Rep (T) then
7827 return False;
7829 else
7830 return First_Rep_Item (T) /= First_Rep_Item (Root_Type (T));
7832 -- May need a more precise check here: the First_Rep_Item may be a
7833 -- stream attribute, which does not affect the representation of the
7834 -- type ???
7836 end if;
7837 end Has_New_Non_Standard_Rep;
7839 ----------------------
7840 -- Inline_Init_Proc --
7841 ----------------------
7843 function Inline_Init_Proc (Typ : Entity_Id) return Boolean is
7844 begin
7845 -- The initialization proc of protected records is not worth inlining.
7846 -- In addition, when compiled for another unit for inlining purposes,
7847 -- it may make reference to entities that have not been elaborated yet.
7848 -- The initialization proc of records that need finalization contains
7849 -- a nested clean-up procedure that makes it impractical to inline as
7850 -- well, except for simple controlled types themselves. And similar
7851 -- considerations apply to task types.
7853 if Is_Concurrent_Type (Typ) then
7854 return False;
7856 elsif Needs_Finalization (Typ) and then not Is_Controlled (Typ) then
7857 return False;
7859 elsif Has_Task (Typ) then
7860 return False;
7862 else
7863 return True;
7864 end if;
7865 end Inline_Init_Proc;
7867 ----------------
7868 -- In_Runtime --
7869 ----------------
7871 function In_Runtime (E : Entity_Id) return Boolean is
7872 S1 : Entity_Id;
7874 begin
7875 S1 := Scope (E);
7876 while Scope (S1) /= Standard_Standard loop
7877 S1 := Scope (S1);
7878 end loop;
7880 return Is_RTU (S1, System) or else Is_RTU (S1, Ada);
7881 end In_Runtime;
7883 ----------------------------
7884 -- Initialization_Warning --
7885 ----------------------------
7887 procedure Initialization_Warning (E : Entity_Id) is
7888 Warning_Needed : Boolean;
7890 begin
7891 Warning_Needed := False;
7893 if Ekind (Current_Scope) = E_Package
7894 and then Static_Elaboration_Desired (Current_Scope)
7895 then
7896 if Is_Type (E) then
7897 if Is_Record_Type (E) then
7898 if Has_Discriminants (E)
7899 or else Is_Limited_Type (E)
7900 or else Has_Non_Standard_Rep (E)
7901 then
7902 Warning_Needed := True;
7904 else
7905 -- Verify that at least one component has an initialization
7906 -- expression. No need for a warning on a type if all its
7907 -- components have no initialization.
7909 declare
7910 Comp : Entity_Id;
7912 begin
7913 Comp := First_Component (E);
7914 while Present (Comp) loop
7915 if Ekind (Comp) = E_Discriminant
7916 or else
7917 (Nkind (Parent (Comp)) = N_Component_Declaration
7918 and then Present (Expression (Parent (Comp))))
7919 then
7920 Warning_Needed := True;
7921 exit;
7922 end if;
7924 Next_Component (Comp);
7925 end loop;
7926 end;
7927 end if;
7929 if Warning_Needed then
7930 Error_Msg_N
7931 ("Objects of the type cannot be initialized statically "
7932 & "by default??", Parent (E));
7933 end if;
7934 end if;
7936 else
7937 Error_Msg_N ("Object cannot be initialized statically??", E);
7938 end if;
7939 end if;
7940 end Initialization_Warning;
7942 ------------------
7943 -- Init_Formals --
7944 ------------------
7946 function Init_Formals (Typ : Entity_Id) return List_Id is
7947 Loc : constant Source_Ptr := Sloc (Typ);
7948 Formals : List_Id;
7950 begin
7951 -- First parameter is always _Init : in out typ. Note that we need this
7952 -- to be in/out because in the case of the task record value, there
7953 -- are default record fields (_Priority, _Size, -Task_Info) that may
7954 -- be referenced in the generated initialization routine.
7956 Formals := New_List (
7957 Make_Parameter_Specification (Loc,
7958 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uInit),
7959 In_Present => True,
7960 Out_Present => True,
7961 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
7963 -- For task record value, or type that contains tasks, add two more
7964 -- formals, _Master : Master_Id and _Chain : in out Activation_Chain
7965 -- We also add these parameters for the task record type case.
7967 if Has_Task (Typ)
7968 or else (Is_Record_Type (Typ) and then Is_Task_Record_Type (Typ))
7969 then
7970 Append_To (Formals,
7971 Make_Parameter_Specification (Loc,
7972 Defining_Identifier =>
7973 Make_Defining_Identifier (Loc, Name_uMaster),
7974 Parameter_Type =>
7975 New_Occurrence_Of (RTE (RE_Master_Id), Loc)));
7977 -- Add _Chain (not done for sequential elaboration policy, see
7978 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
7980 if Partition_Elaboration_Policy /= 'S' then
7981 Append_To (Formals,
7982 Make_Parameter_Specification (Loc,
7983 Defining_Identifier =>
7984 Make_Defining_Identifier (Loc, Name_uChain),
7985 In_Present => True,
7986 Out_Present => True,
7987 Parameter_Type =>
7988 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc)));
7989 end if;
7991 Append_To (Formals,
7992 Make_Parameter_Specification (Loc,
7993 Defining_Identifier =>
7994 Make_Defining_Identifier (Loc, Name_uTask_Name),
7995 In_Present => True,
7996 Parameter_Type => New_Occurrence_Of (Standard_String, Loc)));
7997 end if;
7999 return Formals;
8001 exception
8002 when RE_Not_Available =>
8003 return Empty_List;
8004 end Init_Formals;
8006 -------------------------
8007 -- Init_Secondary_Tags --
8008 -------------------------
8010 procedure Init_Secondary_Tags
8011 (Typ : Entity_Id;
8012 Target : Node_Id;
8013 Stmts_List : List_Id;
8014 Fixed_Comps : Boolean := True;
8015 Variable_Comps : Boolean := True)
8017 Loc : constant Source_Ptr := Sloc (Target);
8019 -- Inherit the C++ tag of the secondary dispatch table of Typ associated
8020 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
8022 procedure Initialize_Tag
8023 (Typ : Entity_Id;
8024 Iface : Entity_Id;
8025 Tag_Comp : Entity_Id;
8026 Iface_Tag : Node_Id);
8027 -- Initialize the tag of the secondary dispatch table of Typ associated
8028 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
8029 -- Compiling under the CPP full ABI compatibility mode, if the ancestor
8030 -- of Typ CPP tagged type we generate code to inherit the contents of
8031 -- the dispatch table directly from the ancestor.
8033 --------------------
8034 -- Initialize_Tag --
8035 --------------------
8037 procedure Initialize_Tag
8038 (Typ : Entity_Id;
8039 Iface : Entity_Id;
8040 Tag_Comp : Entity_Id;
8041 Iface_Tag : Node_Id)
8043 Comp_Typ : Entity_Id;
8044 Offset_To_Top_Comp : Entity_Id := Empty;
8046 begin
8047 -- Initialize pointer to secondary DT associated with the interface
8049 if not Is_Ancestor (Iface, Typ, Use_Full_View => True) then
8050 Append_To (Stmts_List,
8051 Make_Assignment_Statement (Loc,
8052 Name =>
8053 Make_Selected_Component (Loc,
8054 Prefix => New_Copy_Tree (Target),
8055 Selector_Name => New_Occurrence_Of (Tag_Comp, Loc)),
8056 Expression =>
8057 New_Occurrence_Of (Iface_Tag, Loc)));
8058 end if;
8060 Comp_Typ := Scope (Tag_Comp);
8062 -- Initialize the entries of the table of interfaces. We generate a
8063 -- different call when the parent of the type has variable size
8064 -- components.
8066 if Comp_Typ /= Etype (Comp_Typ)
8067 and then Is_Variable_Size_Record (Etype (Comp_Typ))
8068 and then Chars (Tag_Comp) /= Name_uTag
8069 then
8070 pragma Assert (Present (DT_Offset_To_Top_Func (Tag_Comp)));
8072 -- Issue error if Set_Dynamic_Offset_To_Top is not available in a
8073 -- configurable run-time environment.
8075 if not RTE_Available (RE_Set_Dynamic_Offset_To_Top) then
8076 Error_Msg_CRT
8077 ("variable size record with interface types", Typ);
8078 return;
8079 end if;
8081 -- Generate:
8082 -- Set_Dynamic_Offset_To_Top
8083 -- (This => Init,
8084 -- Interface_T => Iface'Tag,
8085 -- Offset_Value => n,
8086 -- Offset_Func => Fn'Address)
8088 Append_To (Stmts_List,
8089 Make_Procedure_Call_Statement (Loc,
8090 Name =>
8091 New_Occurrence_Of (RTE (RE_Set_Dynamic_Offset_To_Top), Loc),
8092 Parameter_Associations => New_List (
8093 Make_Attribute_Reference (Loc,
8094 Prefix => New_Copy_Tree (Target),
8095 Attribute_Name => Name_Address),
8097 Unchecked_Convert_To (RTE (RE_Tag),
8098 New_Occurrence_Of
8099 (Node (First_Elmt (Access_Disp_Table (Iface))),
8100 Loc)),
8102 Unchecked_Convert_To
8103 (RTE (RE_Storage_Offset),
8104 Make_Attribute_Reference (Loc,
8105 Prefix =>
8106 Make_Selected_Component (Loc,
8107 Prefix => New_Copy_Tree (Target),
8108 Selector_Name =>
8109 New_Occurrence_Of (Tag_Comp, Loc)),
8110 Attribute_Name => Name_Position)),
8112 Unchecked_Convert_To (RTE (RE_Offset_To_Top_Function_Ptr),
8113 Make_Attribute_Reference (Loc,
8114 Prefix => New_Occurrence_Of
8115 (DT_Offset_To_Top_Func (Tag_Comp), Loc),
8116 Attribute_Name => Name_Address)))));
8118 -- In this case the next component stores the value of the offset
8119 -- to the top.
8121 Offset_To_Top_Comp := Next_Entity (Tag_Comp);
8122 pragma Assert (Present (Offset_To_Top_Comp));
8124 Append_To (Stmts_List,
8125 Make_Assignment_Statement (Loc,
8126 Name =>
8127 Make_Selected_Component (Loc,
8128 Prefix => New_Copy_Tree (Target),
8129 Selector_Name =>
8130 New_Occurrence_Of (Offset_To_Top_Comp, Loc)),
8132 Expression =>
8133 Make_Attribute_Reference (Loc,
8134 Prefix =>
8135 Make_Selected_Component (Loc,
8136 Prefix => New_Copy_Tree (Target),
8137 Selector_Name => New_Occurrence_Of (Tag_Comp, Loc)),
8138 Attribute_Name => Name_Position)));
8140 -- Normal case: No discriminants in the parent type
8142 else
8143 -- Don't need to set any value if this interface shares the
8144 -- primary dispatch table.
8146 if not Is_Ancestor (Iface, Typ, Use_Full_View => True) then
8147 Append_To (Stmts_List,
8148 Build_Set_Static_Offset_To_Top (Loc,
8149 Iface_Tag => New_Occurrence_Of (Iface_Tag, Loc),
8150 Offset_Value =>
8151 Unchecked_Convert_To (RTE (RE_Storage_Offset),
8152 Make_Attribute_Reference (Loc,
8153 Prefix =>
8154 Make_Selected_Component (Loc,
8155 Prefix => New_Copy_Tree (Target),
8156 Selector_Name =>
8157 New_Occurrence_Of (Tag_Comp, Loc)),
8158 Attribute_Name => Name_Position))));
8159 end if;
8161 -- Generate:
8162 -- Register_Interface_Offset
8163 -- (This => Init,
8164 -- Interface_T => Iface'Tag,
8165 -- Is_Constant => True,
8166 -- Offset_Value => n,
8167 -- Offset_Func => null);
8169 if RTE_Available (RE_Register_Interface_Offset) then
8170 Append_To (Stmts_List,
8171 Make_Procedure_Call_Statement (Loc,
8172 Name =>
8173 New_Occurrence_Of
8174 (RTE (RE_Register_Interface_Offset), Loc),
8175 Parameter_Associations => New_List (
8176 Make_Attribute_Reference (Loc,
8177 Prefix => New_Copy_Tree (Target),
8178 Attribute_Name => Name_Address),
8180 Unchecked_Convert_To (RTE (RE_Tag),
8181 New_Occurrence_Of
8182 (Node (First_Elmt (Access_Disp_Table (Iface))), Loc)),
8184 New_Occurrence_Of (Standard_True, Loc),
8186 Unchecked_Convert_To (RTE (RE_Storage_Offset),
8187 Make_Attribute_Reference (Loc,
8188 Prefix =>
8189 Make_Selected_Component (Loc,
8190 Prefix => New_Copy_Tree (Target),
8191 Selector_Name =>
8192 New_Occurrence_Of (Tag_Comp, Loc)),
8193 Attribute_Name => Name_Position)),
8195 Make_Null (Loc))));
8196 end if;
8197 end if;
8198 end Initialize_Tag;
8200 -- Local variables
8202 Full_Typ : Entity_Id;
8203 Ifaces_List : Elist_Id;
8204 Ifaces_Comp_List : Elist_Id;
8205 Ifaces_Tag_List : Elist_Id;
8206 Iface_Elmt : Elmt_Id;
8207 Iface_Comp_Elmt : Elmt_Id;
8208 Iface_Tag_Elmt : Elmt_Id;
8209 Tag_Comp : Node_Id;
8210 In_Variable_Pos : Boolean;
8212 -- Start of processing for Init_Secondary_Tags
8214 begin
8215 -- Handle private types
8217 if Present (Full_View (Typ)) then
8218 Full_Typ := Full_View (Typ);
8219 else
8220 Full_Typ := Typ;
8221 end if;
8223 Collect_Interfaces_Info
8224 (Full_Typ, Ifaces_List, Ifaces_Comp_List, Ifaces_Tag_List);
8226 Iface_Elmt := First_Elmt (Ifaces_List);
8227 Iface_Comp_Elmt := First_Elmt (Ifaces_Comp_List);
8228 Iface_Tag_Elmt := First_Elmt (Ifaces_Tag_List);
8229 while Present (Iface_Elmt) loop
8230 Tag_Comp := Node (Iface_Comp_Elmt);
8232 -- Check if parent of record type has variable size components
8234 In_Variable_Pos := Scope (Tag_Comp) /= Etype (Scope (Tag_Comp))
8235 and then Is_Variable_Size_Record (Etype (Scope (Tag_Comp)));
8237 -- If we are compiling under the CPP full ABI compatibility mode and
8238 -- the ancestor is a CPP_Pragma tagged type then we generate code to
8239 -- initialize the secondary tag components from tags that reference
8240 -- secondary tables filled with copy of parent slots.
8242 if Is_CPP_Class (Root_Type (Full_Typ)) then
8244 -- Reject interface components located at variable offset in
8245 -- C++ derivations. This is currently unsupported.
8247 if not Fixed_Comps and then In_Variable_Pos then
8249 -- Locate the first dynamic component of the record. Done to
8250 -- improve the text of the warning.
8252 declare
8253 Comp : Entity_Id;
8254 Comp_Typ : Entity_Id;
8256 begin
8257 Comp := First_Entity (Typ);
8258 while Present (Comp) loop
8259 Comp_Typ := Etype (Comp);
8261 if Ekind (Comp) /= E_Discriminant
8262 and then not Is_Tag (Comp)
8263 then
8264 exit when
8265 (Is_Record_Type (Comp_Typ)
8266 and then
8267 Is_Variable_Size_Record (Base_Type (Comp_Typ)))
8268 or else
8269 (Is_Array_Type (Comp_Typ)
8270 and then Is_Variable_Size_Array (Comp_Typ));
8271 end if;
8273 Next_Entity (Comp);
8274 end loop;
8276 pragma Assert (Present (Comp));
8277 Error_Msg_Node_2 := Comp;
8278 Error_Msg_NE
8279 ("parent type & with dynamic component & cannot be parent"
8280 & " of 'C'P'P derivation if new interfaces are present",
8281 Typ, Scope (Original_Record_Component (Comp)));
8283 Error_Msg_Sloc :=
8284 Sloc (Scope (Original_Record_Component (Comp)));
8285 Error_Msg_NE
8286 ("type derived from 'C'P'P type & defined #",
8287 Typ, Scope (Original_Record_Component (Comp)));
8289 -- Avoid duplicated warnings
8291 exit;
8292 end;
8294 -- Initialize secondary tags
8296 else
8297 Append_To (Stmts_List,
8298 Make_Assignment_Statement (Loc,
8299 Name =>
8300 Make_Selected_Component (Loc,
8301 Prefix => New_Copy_Tree (Target),
8302 Selector_Name =>
8303 New_Occurrence_Of (Node (Iface_Comp_Elmt), Loc)),
8304 Expression =>
8305 New_Occurrence_Of (Node (Iface_Tag_Elmt), Loc)));
8306 end if;
8308 -- Otherwise generate code to initialize the tag
8310 else
8311 if (In_Variable_Pos and then Variable_Comps)
8312 or else (not In_Variable_Pos and then Fixed_Comps)
8313 then
8314 Initialize_Tag (Full_Typ,
8315 Iface => Node (Iface_Elmt),
8316 Tag_Comp => Tag_Comp,
8317 Iface_Tag => Node (Iface_Tag_Elmt));
8318 end if;
8319 end if;
8321 Next_Elmt (Iface_Elmt);
8322 Next_Elmt (Iface_Comp_Elmt);
8323 Next_Elmt (Iface_Tag_Elmt);
8324 end loop;
8325 end Init_Secondary_Tags;
8327 ------------------------
8328 -- Is_User_Defined_Eq --
8329 ------------------------
8331 function Is_User_Defined_Equality (Prim : Node_Id) return Boolean is
8332 begin
8333 return Chars (Prim) = Name_Op_Eq
8334 and then Etype (First_Formal (Prim)) =
8335 Etype (Next_Formal (First_Formal (Prim)))
8336 and then Base_Type (Etype (Prim)) = Standard_Boolean;
8337 end Is_User_Defined_Equality;
8339 ----------------------------------------
8340 -- Make_Controlling_Function_Wrappers --
8341 ----------------------------------------
8343 procedure Make_Controlling_Function_Wrappers
8344 (Tag_Typ : Entity_Id;
8345 Decl_List : out List_Id;
8346 Body_List : out List_Id)
8348 Loc : constant Source_Ptr := Sloc (Tag_Typ);
8349 Prim_Elmt : Elmt_Id;
8350 Subp : Entity_Id;
8351 Actual_List : List_Id;
8352 Formal_List : List_Id;
8353 Formal : Entity_Id;
8354 Par_Formal : Entity_Id;
8355 Formal_Node : Node_Id;
8356 Func_Body : Node_Id;
8357 Func_Decl : Node_Id;
8358 Func_Spec : Node_Id;
8359 Return_Stmt : Node_Id;
8361 begin
8362 Decl_List := New_List;
8363 Body_List := New_List;
8365 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
8366 while Present (Prim_Elmt) loop
8367 Subp := Node (Prim_Elmt);
8369 -- If a primitive function with a controlling result of the type has
8370 -- not been overridden by the user, then we must create a wrapper
8371 -- function here that effectively overrides it and invokes the
8372 -- (non-abstract) parent function. This can only occur for a null
8373 -- extension. Note that functions with anonymous controlling access
8374 -- results don't qualify and must be overridden. We also exclude
8375 -- Input attributes, since each type will have its own version of
8376 -- Input constructed by the expander. The test for Comes_From_Source
8377 -- is needed to distinguish inherited operations from renamings
8378 -- (which also have Alias set). We exclude internal entities with
8379 -- Interface_Alias to avoid generating duplicated wrappers since
8380 -- the primitive which covers the interface is also available in
8381 -- the list of primitive operations.
8383 -- The function may be abstract, or require_Overriding may be set
8384 -- for it, because tests for null extensions may already have reset
8385 -- the Is_Abstract_Subprogram_Flag. If Requires_Overriding is not
8386 -- set, functions that need wrappers are recognized by having an
8387 -- alias that returns the parent type.
8389 if Comes_From_Source (Subp)
8390 or else No (Alias (Subp))
8391 or else Present (Interface_Alias (Subp))
8392 or else Ekind (Subp) /= E_Function
8393 or else not Has_Controlling_Result (Subp)
8394 or else Is_Access_Type (Etype (Subp))
8395 or else Is_Abstract_Subprogram (Alias (Subp))
8396 or else Is_TSS (Subp, TSS_Stream_Input)
8397 then
8398 goto Next_Prim;
8400 elsif Is_Abstract_Subprogram (Subp)
8401 or else Requires_Overriding (Subp)
8402 or else
8403 (Is_Null_Extension (Etype (Subp))
8404 and then Etype (Alias (Subp)) /= Etype (Subp))
8405 then
8406 Formal_List := No_List;
8407 Formal := First_Formal (Subp);
8409 if Present (Formal) then
8410 Formal_List := New_List;
8412 while Present (Formal) loop
8413 Append
8414 (Make_Parameter_Specification
8415 (Loc,
8416 Defining_Identifier =>
8417 Make_Defining_Identifier (Sloc (Formal),
8418 Chars => Chars (Formal)),
8419 In_Present => In_Present (Parent (Formal)),
8420 Out_Present => Out_Present (Parent (Formal)),
8421 Null_Exclusion_Present =>
8422 Null_Exclusion_Present (Parent (Formal)),
8423 Parameter_Type =>
8424 New_Occurrence_Of (Etype (Formal), Loc),
8425 Expression =>
8426 New_Copy_Tree (Expression (Parent (Formal)))),
8427 Formal_List);
8429 Next_Formal (Formal);
8430 end loop;
8431 end if;
8433 Func_Spec :=
8434 Make_Function_Specification (Loc,
8435 Defining_Unit_Name =>
8436 Make_Defining_Identifier (Loc,
8437 Chars => Chars (Subp)),
8438 Parameter_Specifications => Formal_List,
8439 Result_Definition =>
8440 New_Occurrence_Of (Etype (Subp), Loc));
8442 Func_Decl := Make_Subprogram_Declaration (Loc, Func_Spec);
8443 Append_To (Decl_List, Func_Decl);
8445 -- Build a wrapper body that calls the parent function. The body
8446 -- contains a single return statement that returns an extension
8447 -- aggregate whose ancestor part is a call to the parent function,
8448 -- passing the formals as actuals (with any controlling arguments
8449 -- converted to the types of the corresponding formals of the
8450 -- parent function, which might be anonymous access types), and
8451 -- having a null extension.
8453 Formal := First_Formal (Subp);
8454 Par_Formal := First_Formal (Alias (Subp));
8455 Formal_Node := First (Formal_List);
8457 if Present (Formal) then
8458 Actual_List := New_List;
8459 else
8460 Actual_List := No_List;
8461 end if;
8463 while Present (Formal) loop
8464 if Is_Controlling_Formal (Formal) then
8465 Append_To (Actual_List,
8466 Make_Type_Conversion (Loc,
8467 Subtype_Mark =>
8468 New_Occurrence_Of (Etype (Par_Formal), Loc),
8469 Expression =>
8470 New_Occurrence_Of
8471 (Defining_Identifier (Formal_Node), Loc)));
8472 else
8473 Append_To
8474 (Actual_List,
8475 New_Occurrence_Of
8476 (Defining_Identifier (Formal_Node), Loc));
8477 end if;
8479 Next_Formal (Formal);
8480 Next_Formal (Par_Formal);
8481 Next (Formal_Node);
8482 end loop;
8484 Return_Stmt :=
8485 Make_Simple_Return_Statement (Loc,
8486 Expression =>
8487 Make_Extension_Aggregate (Loc,
8488 Ancestor_Part =>
8489 Make_Function_Call (Loc,
8490 Name =>
8491 New_Occurrence_Of (Alias (Subp), Loc),
8492 Parameter_Associations => Actual_List),
8493 Null_Record_Present => True));
8495 Func_Body :=
8496 Make_Subprogram_Body (Loc,
8497 Specification => New_Copy_Tree (Func_Spec),
8498 Declarations => Empty_List,
8499 Handled_Statement_Sequence =>
8500 Make_Handled_Sequence_Of_Statements (Loc,
8501 Statements => New_List (Return_Stmt)));
8503 Set_Defining_Unit_Name
8504 (Specification (Func_Body),
8505 Make_Defining_Identifier (Loc, Chars (Subp)));
8507 Append_To (Body_List, Func_Body);
8509 -- Replace the inherited function with the wrapper function in the
8510 -- primitive operations list. We add the minimum decoration needed
8511 -- to override interface primitives.
8513 Set_Ekind (Defining_Unit_Name (Func_Spec), E_Function);
8515 Override_Dispatching_Operation
8516 (Tag_Typ, Subp, New_Op => Defining_Unit_Name (Func_Spec),
8517 Is_Wrapper => True);
8518 end if;
8520 <<Next_Prim>>
8521 Next_Elmt (Prim_Elmt);
8522 end loop;
8523 end Make_Controlling_Function_Wrappers;
8525 -------------------
8526 -- Make_Eq_Body --
8527 -------------------
8529 function Make_Eq_Body
8530 (Typ : Entity_Id;
8531 Eq_Name : Name_Id) return Node_Id
8533 Loc : constant Source_Ptr := Sloc (Parent (Typ));
8534 Decl : Node_Id;
8535 Def : constant Node_Id := Parent (Typ);
8536 Stmts : constant List_Id := New_List;
8537 Variant_Case : Boolean := Has_Discriminants (Typ);
8538 Comps : Node_Id := Empty;
8539 Typ_Def : Node_Id := Type_Definition (Def);
8541 begin
8542 Decl :=
8543 Predef_Spec_Or_Body (Loc,
8544 Tag_Typ => Typ,
8545 Name => Eq_Name,
8546 Profile => New_List (
8547 Make_Parameter_Specification (Loc,
8548 Defining_Identifier =>
8549 Make_Defining_Identifier (Loc, Name_X),
8550 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
8552 Make_Parameter_Specification (Loc,
8553 Defining_Identifier =>
8554 Make_Defining_Identifier (Loc, Name_Y),
8555 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8557 Ret_Type => Standard_Boolean,
8558 For_Body => True);
8560 if Variant_Case then
8561 if Nkind (Typ_Def) = N_Derived_Type_Definition then
8562 Typ_Def := Record_Extension_Part (Typ_Def);
8563 end if;
8565 if Present (Typ_Def) then
8566 Comps := Component_List (Typ_Def);
8567 end if;
8569 Variant_Case :=
8570 Present (Comps) and then Present (Variant_Part (Comps));
8571 end if;
8573 if Variant_Case then
8574 Append_To (Stmts,
8575 Make_Eq_If (Typ, Discriminant_Specifications (Def)));
8576 Append_List_To (Stmts, Make_Eq_Case (Typ, Comps));
8577 Append_To (Stmts,
8578 Make_Simple_Return_Statement (Loc,
8579 Expression => New_Occurrence_Of (Standard_True, Loc)));
8581 else
8582 Append_To (Stmts,
8583 Make_Simple_Return_Statement (Loc,
8584 Expression =>
8585 Expand_Record_Equality
8586 (Typ,
8587 Typ => Typ,
8588 Lhs => Make_Identifier (Loc, Name_X),
8589 Rhs => Make_Identifier (Loc, Name_Y),
8590 Bodies => Declarations (Decl))));
8591 end if;
8593 Set_Handled_Statement_Sequence
8594 (Decl, Make_Handled_Sequence_Of_Statements (Loc, Stmts));
8595 return Decl;
8596 end Make_Eq_Body;
8598 ------------------
8599 -- Make_Eq_Case --
8600 ------------------
8602 -- <Make_Eq_If shared components>
8604 -- case X.D1 is
8605 -- when V1 => <Make_Eq_Case> on subcomponents
8606 -- ...
8607 -- when Vn => <Make_Eq_Case> on subcomponents
8608 -- end case;
8610 function Make_Eq_Case
8611 (E : Entity_Id;
8612 CL : Node_Id;
8613 Discrs : Elist_Id := New_Elmt_List) return List_Id
8615 Loc : constant Source_Ptr := Sloc (E);
8616 Result : constant List_Id := New_List;
8617 Variant : Node_Id;
8618 Alt_List : List_Id;
8620 function Corresponding_Formal (C : Node_Id) return Entity_Id;
8621 -- Given the discriminant that controls a given variant of an unchecked
8622 -- union, find the formal of the equality function that carries the
8623 -- inferred value of the discriminant.
8625 function External_Name (E : Entity_Id) return Name_Id;
8626 -- The value of a given discriminant is conveyed in the corresponding
8627 -- formal parameter of the equality routine. The name of this formal
8628 -- parameter carries a one-character suffix which is removed here.
8630 --------------------------
8631 -- Corresponding_Formal --
8632 --------------------------
8634 function Corresponding_Formal (C : Node_Id) return Entity_Id is
8635 Discr : constant Entity_Id := Entity (Name (Variant_Part (C)));
8636 Elm : Elmt_Id;
8638 begin
8639 Elm := First_Elmt (Discrs);
8640 while Present (Elm) loop
8641 if Chars (Discr) = External_Name (Node (Elm)) then
8642 return Node (Elm);
8643 end if;
8645 Next_Elmt (Elm);
8646 end loop;
8648 -- A formal of the proper name must be found
8650 raise Program_Error;
8651 end Corresponding_Formal;
8653 -------------------
8654 -- External_Name --
8655 -------------------
8657 function External_Name (E : Entity_Id) return Name_Id is
8658 begin
8659 Get_Name_String (Chars (E));
8660 Name_Len := Name_Len - 1;
8661 return Name_Find;
8662 end External_Name;
8664 -- Start of processing for Make_Eq_Case
8666 begin
8667 Append_To (Result, Make_Eq_If (E, Component_Items (CL)));
8669 if No (Variant_Part (CL)) then
8670 return Result;
8671 end if;
8673 Variant := First_Non_Pragma (Variants (Variant_Part (CL)));
8675 if No (Variant) then
8676 return Result;
8677 end if;
8679 Alt_List := New_List;
8680 while Present (Variant) loop
8681 Append_To (Alt_List,
8682 Make_Case_Statement_Alternative (Loc,
8683 Discrete_Choices => New_Copy_List (Discrete_Choices (Variant)),
8684 Statements =>
8685 Make_Eq_Case (E, Component_List (Variant), Discrs)));
8686 Next_Non_Pragma (Variant);
8687 end loop;
8689 -- If we have an Unchecked_Union, use one of the parameters of the
8690 -- enclosing equality routine that captures the discriminant, to use
8691 -- as the expression in the generated case statement.
8693 if Is_Unchecked_Union (E) then
8694 Append_To (Result,
8695 Make_Case_Statement (Loc,
8696 Expression =>
8697 New_Occurrence_Of (Corresponding_Formal (CL), Loc),
8698 Alternatives => Alt_List));
8700 else
8701 Append_To (Result,
8702 Make_Case_Statement (Loc,
8703 Expression =>
8704 Make_Selected_Component (Loc,
8705 Prefix => Make_Identifier (Loc, Name_X),
8706 Selector_Name => New_Copy (Name (Variant_Part (CL)))),
8707 Alternatives => Alt_List));
8708 end if;
8710 return Result;
8711 end Make_Eq_Case;
8713 ----------------
8714 -- Make_Eq_If --
8715 ----------------
8717 -- Generates:
8719 -- if
8720 -- X.C1 /= Y.C1
8721 -- or else
8722 -- X.C2 /= Y.C2
8723 -- ...
8724 -- then
8725 -- return False;
8726 -- end if;
8728 -- or a null statement if the list L is empty
8730 function Make_Eq_If
8731 (E : Entity_Id;
8732 L : List_Id) return Node_Id
8734 Loc : constant Source_Ptr := Sloc (E);
8735 C : Node_Id;
8736 Field_Name : Name_Id;
8737 Cond : Node_Id;
8739 begin
8740 if No (L) then
8741 return Make_Null_Statement (Loc);
8743 else
8744 Cond := Empty;
8746 C := First_Non_Pragma (L);
8747 while Present (C) loop
8748 Field_Name := Chars (Defining_Identifier (C));
8750 -- The tags must not be compared: they are not part of the value.
8751 -- Ditto for parent interfaces because their equality operator is
8752 -- abstract.
8754 -- Note also that in the following, we use Make_Identifier for
8755 -- the component names. Use of New_Occurrence_Of to identify the
8756 -- components would be incorrect because the wrong entities for
8757 -- discriminants could be picked up in the private type case.
8759 if Field_Name = Name_uParent
8760 and then Is_Interface (Etype (Defining_Identifier (C)))
8761 then
8762 null;
8764 elsif Field_Name /= Name_uTag then
8765 Evolve_Or_Else (Cond,
8766 Make_Op_Ne (Loc,
8767 Left_Opnd =>
8768 Make_Selected_Component (Loc,
8769 Prefix => Make_Identifier (Loc, Name_X),
8770 Selector_Name => Make_Identifier (Loc, Field_Name)),
8772 Right_Opnd =>
8773 Make_Selected_Component (Loc,
8774 Prefix => Make_Identifier (Loc, Name_Y),
8775 Selector_Name => Make_Identifier (Loc, Field_Name))));
8776 end if;
8778 Next_Non_Pragma (C);
8779 end loop;
8781 if No (Cond) then
8782 return Make_Null_Statement (Loc);
8784 else
8785 return
8786 Make_Implicit_If_Statement (E,
8787 Condition => Cond,
8788 Then_Statements => New_List (
8789 Make_Simple_Return_Statement (Loc,
8790 Expression => New_Occurrence_Of (Standard_False, Loc))));
8791 end if;
8792 end if;
8793 end Make_Eq_If;
8795 -------------------
8796 -- Make_Neq_Body --
8797 -------------------
8799 function Make_Neq_Body (Tag_Typ : Entity_Id) return Node_Id is
8801 function Is_Predefined_Neq_Renaming (Prim : Node_Id) return Boolean;
8802 -- Returns true if Prim is a renaming of an unresolved predefined
8803 -- inequality operation.
8805 --------------------------------
8806 -- Is_Predefined_Neq_Renaming --
8807 --------------------------------
8809 function Is_Predefined_Neq_Renaming (Prim : Node_Id) return Boolean is
8810 begin
8811 return Chars (Prim) /= Name_Op_Ne
8812 and then Present (Alias (Prim))
8813 and then Comes_From_Source (Prim)
8814 and then Is_Intrinsic_Subprogram (Alias (Prim))
8815 and then Chars (Alias (Prim)) = Name_Op_Ne;
8816 end Is_Predefined_Neq_Renaming;
8818 -- Local variables
8820 Loc : constant Source_Ptr := Sloc (Parent (Tag_Typ));
8821 Stmts : constant List_Id := New_List;
8822 Decl : Node_Id;
8823 Eq_Prim : Entity_Id;
8824 Left_Op : Entity_Id;
8825 Renaming_Prim : Entity_Id;
8826 Right_Op : Entity_Id;
8827 Target : Entity_Id;
8829 -- Start of processing for Make_Neq_Body
8831 begin
8832 -- For a call on a renaming of a dispatching subprogram that is
8833 -- overridden, if the overriding occurred before the renaming, then
8834 -- the body executed is that of the overriding declaration, even if the
8835 -- overriding declaration is not visible at the place of the renaming;
8836 -- otherwise, the inherited or predefined subprogram is called, see
8837 -- (RM 8.5.4(8))
8839 -- Stage 1: Search for a renaming of the inequality primitive and also
8840 -- search for an overriding of the equality primitive located before the
8841 -- renaming declaration.
8843 declare
8844 Elmt : Elmt_Id;
8845 Prim : Node_Id;
8847 begin
8848 Eq_Prim := Empty;
8849 Renaming_Prim := Empty;
8851 Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
8852 while Present (Elmt) loop
8853 Prim := Node (Elmt);
8855 if Is_User_Defined_Equality (Prim) and then No (Alias (Prim)) then
8856 if No (Renaming_Prim) then
8857 pragma Assert (No (Eq_Prim));
8858 Eq_Prim := Prim;
8859 end if;
8861 elsif Is_Predefined_Neq_Renaming (Prim) then
8862 Renaming_Prim := Prim;
8863 end if;
8865 Next_Elmt (Elmt);
8866 end loop;
8867 end;
8869 -- No further action needed if no renaming was found
8871 if No (Renaming_Prim) then
8872 return Empty;
8873 end if;
8875 -- Stage 2: Replace the renaming declaration by a subprogram declaration
8876 -- (required to add its body)
8878 Decl := Parent (Parent (Renaming_Prim));
8879 Rewrite (Decl,
8880 Make_Subprogram_Declaration (Loc,
8881 Specification => Specification (Decl)));
8882 Set_Analyzed (Decl);
8884 -- Remove the decoration of intrinsic renaming subprogram
8886 Set_Is_Intrinsic_Subprogram (Renaming_Prim, False);
8887 Set_Convention (Renaming_Prim, Convention_Ada);
8888 Set_Alias (Renaming_Prim, Empty);
8889 Set_Has_Completion (Renaming_Prim, False);
8891 -- Stage 3: Build the corresponding body
8893 Left_Op := First_Formal (Renaming_Prim);
8894 Right_Op := Next_Formal (Left_Op);
8896 Decl :=
8897 Predef_Spec_Or_Body (Loc,
8898 Tag_Typ => Tag_Typ,
8899 Name => Chars (Renaming_Prim),
8900 Profile => New_List (
8901 Make_Parameter_Specification (Loc,
8902 Defining_Identifier =>
8903 Make_Defining_Identifier (Loc, Chars (Left_Op)),
8904 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
8906 Make_Parameter_Specification (Loc,
8907 Defining_Identifier =>
8908 Make_Defining_Identifier (Loc, Chars (Right_Op)),
8909 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
8911 Ret_Type => Standard_Boolean,
8912 For_Body => True);
8914 -- If the overriding of the equality primitive occurred before the
8915 -- renaming, then generate:
8917 -- function <Neq_Name> (X : Y : Typ) return Boolean is
8918 -- begin
8919 -- return not Oeq (X, Y);
8920 -- end;
8922 if Present (Eq_Prim) then
8923 Target := Eq_Prim;
8925 -- Otherwise build a nested subprogram which performs the predefined
8926 -- evaluation of the equality operator. That is, generate:
8928 -- function <Neq_Name> (X : Y : Typ) return Boolean is
8929 -- function Oeq (X : Y) return Boolean is
8930 -- begin
8931 -- <<body of default implementation>>
8932 -- end;
8933 -- begin
8934 -- return not Oeq (X, Y);
8935 -- end;
8937 else
8938 declare
8939 Local_Subp : Node_Id;
8940 begin
8941 Local_Subp := Make_Eq_Body (Tag_Typ, Name_Op_Eq);
8942 Set_Declarations (Decl, New_List (Local_Subp));
8943 Target := Defining_Entity (Local_Subp);
8944 end;
8945 end if;
8947 Append_To (Stmts,
8948 Make_Simple_Return_Statement (Loc,
8949 Expression =>
8950 Make_Op_Not (Loc,
8951 Make_Function_Call (Loc,
8952 Name => New_Occurrence_Of (Target, Loc),
8953 Parameter_Associations => New_List (
8954 Make_Identifier (Loc, Chars (Left_Op)),
8955 Make_Identifier (Loc, Chars (Right_Op)))))));
8957 Set_Handled_Statement_Sequence
8958 (Decl, Make_Handled_Sequence_Of_Statements (Loc, Stmts));
8959 return Decl;
8960 end Make_Neq_Body;
8962 -------------------------------
8963 -- Make_Null_Procedure_Specs --
8964 -------------------------------
8966 function Make_Null_Procedure_Specs (Tag_Typ : Entity_Id) return List_Id is
8967 Decl_List : constant List_Id := New_List;
8968 Loc : constant Source_Ptr := Sloc (Tag_Typ);
8969 Formal : Entity_Id;
8970 Formal_List : List_Id;
8971 New_Param_Spec : Node_Id;
8972 Parent_Subp : Entity_Id;
8973 Prim_Elmt : Elmt_Id;
8974 Subp : Entity_Id;
8976 begin
8977 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
8978 while Present (Prim_Elmt) loop
8979 Subp := Node (Prim_Elmt);
8981 -- If a null procedure inherited from an interface has not been
8982 -- overridden, then we build a null procedure declaration to
8983 -- override the inherited procedure.
8985 Parent_Subp := Alias (Subp);
8987 if Present (Parent_Subp)
8988 and then Is_Null_Interface_Primitive (Parent_Subp)
8989 then
8990 Formal_List := No_List;
8991 Formal := First_Formal (Subp);
8993 if Present (Formal) then
8994 Formal_List := New_List;
8996 while Present (Formal) loop
8998 -- Copy the parameter spec including default expressions
9000 New_Param_Spec :=
9001 New_Copy_Tree (Parent (Formal), New_Sloc => Loc);
9003 -- Generate a new defining identifier for the new formal.
9004 -- required because New_Copy_Tree does not duplicate
9005 -- semantic fields (except itypes).
9007 Set_Defining_Identifier (New_Param_Spec,
9008 Make_Defining_Identifier (Sloc (Formal),
9009 Chars => Chars (Formal)));
9011 -- For controlling arguments we must change their
9012 -- parameter type to reference the tagged type (instead
9013 -- of the interface type)
9015 if Is_Controlling_Formal (Formal) then
9016 if Nkind (Parameter_Type (Parent (Formal))) = N_Identifier
9017 then
9018 Set_Parameter_Type (New_Param_Spec,
9019 New_Occurrence_Of (Tag_Typ, Loc));
9021 else pragma Assert
9022 (Nkind (Parameter_Type (Parent (Formal))) =
9023 N_Access_Definition);
9024 Set_Subtype_Mark (Parameter_Type (New_Param_Spec),
9025 New_Occurrence_Of (Tag_Typ, Loc));
9026 end if;
9027 end if;
9029 Append (New_Param_Spec, Formal_List);
9031 Next_Formal (Formal);
9032 end loop;
9033 end if;
9035 Append_To (Decl_List,
9036 Make_Subprogram_Declaration (Loc,
9037 Make_Procedure_Specification (Loc,
9038 Defining_Unit_Name =>
9039 Make_Defining_Identifier (Loc, Chars (Subp)),
9040 Parameter_Specifications => Formal_List,
9041 Null_Present => True)));
9042 end if;
9044 Next_Elmt (Prim_Elmt);
9045 end loop;
9047 return Decl_List;
9048 end Make_Null_Procedure_Specs;
9050 -------------------------------------
9051 -- Make_Predefined_Primitive_Specs --
9052 -------------------------------------
9054 procedure Make_Predefined_Primitive_Specs
9055 (Tag_Typ : Entity_Id;
9056 Predef_List : out List_Id;
9057 Renamed_Eq : out Entity_Id)
9059 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean;
9060 -- Returns true if Prim is a renaming of an unresolved predefined
9061 -- equality operation.
9063 -------------------------------
9064 -- Is_Predefined_Eq_Renaming --
9065 -------------------------------
9067 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean is
9068 begin
9069 return Chars (Prim) /= Name_Op_Eq
9070 and then Present (Alias (Prim))
9071 and then Comes_From_Source (Prim)
9072 and then Is_Intrinsic_Subprogram (Alias (Prim))
9073 and then Chars (Alias (Prim)) = Name_Op_Eq;
9074 end Is_Predefined_Eq_Renaming;
9076 -- Local variables
9078 Loc : constant Source_Ptr := Sloc (Tag_Typ);
9079 Res : constant List_Id := New_List;
9080 Eq_Name : Name_Id := Name_Op_Eq;
9081 Eq_Needed : Boolean;
9082 Eq_Spec : Node_Id;
9083 Prim : Elmt_Id;
9085 Has_Predef_Eq_Renaming : Boolean := False;
9086 -- Set to True if Tag_Typ has a primitive that renames the predefined
9087 -- equality operator. Used to implement (RM 8-5-4(8)).
9089 -- Start of processing for Make_Predefined_Primitive_Specs
9091 begin
9092 Renamed_Eq := Empty;
9094 -- Spec of _Size
9096 Append_To (Res, Predef_Spec_Or_Body (Loc,
9097 Tag_Typ => Tag_Typ,
9098 Name => Name_uSize,
9099 Profile => New_List (
9100 Make_Parameter_Specification (Loc,
9101 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
9102 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
9104 Ret_Type => Standard_Long_Long_Integer));
9106 -- Specs for dispatching stream attributes
9108 declare
9109 Stream_Op_TSS_Names :
9110 constant array (Integer range <>) of TSS_Name_Type :=
9111 (TSS_Stream_Read,
9112 TSS_Stream_Write,
9113 TSS_Stream_Input,
9114 TSS_Stream_Output);
9116 begin
9117 for Op in Stream_Op_TSS_Names'Range loop
9118 if Stream_Operation_OK (Tag_Typ, Stream_Op_TSS_Names (Op)) then
9119 Append_To (Res,
9120 Predef_Stream_Attr_Spec (Loc, Tag_Typ,
9121 Stream_Op_TSS_Names (Op)));
9122 end if;
9123 end loop;
9124 end;
9126 -- Spec of "=" is expanded if the type is not limited and if a user
9127 -- defined "=" was not already declared for the non-full view of a
9128 -- private extension
9130 if not Is_Limited_Type (Tag_Typ) then
9131 Eq_Needed := True;
9132 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
9133 while Present (Prim) loop
9135 -- If a primitive is encountered that renames the predefined
9136 -- equality operator before reaching any explicit equality
9137 -- primitive, then we still need to create a predefined equality
9138 -- function, because calls to it can occur via the renaming. A
9139 -- new name is created for the equality to avoid conflicting with
9140 -- any user-defined equality. (Note that this doesn't account for
9141 -- renamings of equality nested within subpackages???)
9143 if Is_Predefined_Eq_Renaming (Node (Prim)) then
9144 Has_Predef_Eq_Renaming := True;
9145 Eq_Name := New_External_Name (Chars (Node (Prim)), 'E');
9147 -- User-defined equality
9149 elsif Is_User_Defined_Equality (Node (Prim)) then
9150 if No (Alias (Node (Prim)))
9151 or else Nkind (Unit_Declaration_Node (Node (Prim))) =
9152 N_Subprogram_Renaming_Declaration
9153 then
9154 Eq_Needed := False;
9155 exit;
9157 -- If the parent is not an interface type and has an abstract
9158 -- equality function explicitly defined in the sources, then
9159 -- the inherited equality is abstract as well, and no body can
9160 -- be created for it.
9162 elsif not Is_Interface (Etype (Tag_Typ))
9163 and then Present (Alias (Node (Prim)))
9164 and then Comes_From_Source (Alias (Node (Prim)))
9165 and then Is_Abstract_Subprogram (Alias (Node (Prim)))
9166 then
9167 Eq_Needed := False;
9168 exit;
9170 -- If the type has an equality function corresponding with
9171 -- a primitive defined in an interface type, the inherited
9172 -- equality is abstract as well, and no body can be created
9173 -- for it.
9175 elsif Present (Alias (Node (Prim)))
9176 and then Comes_From_Source (Ultimate_Alias (Node (Prim)))
9177 and then
9178 Is_Interface
9179 (Find_Dispatching_Type (Ultimate_Alias (Node (Prim))))
9180 then
9181 Eq_Needed := False;
9182 exit;
9183 end if;
9184 end if;
9186 Next_Elmt (Prim);
9187 end loop;
9189 -- If a renaming of predefined equality was found but there was no
9190 -- user-defined equality (so Eq_Needed is still true), then set the
9191 -- name back to Name_Op_Eq. But in the case where a user-defined
9192 -- equality was located after such a renaming, then the predefined
9193 -- equality function is still needed, so Eq_Needed must be set back
9194 -- to True.
9196 if Eq_Name /= Name_Op_Eq then
9197 if Eq_Needed then
9198 Eq_Name := Name_Op_Eq;
9199 else
9200 Eq_Needed := True;
9201 end if;
9202 end if;
9204 if Eq_Needed then
9205 Eq_Spec := Predef_Spec_Or_Body (Loc,
9206 Tag_Typ => Tag_Typ,
9207 Name => Eq_Name,
9208 Profile => New_List (
9209 Make_Parameter_Specification (Loc,
9210 Defining_Identifier =>
9211 Make_Defining_Identifier (Loc, Name_X),
9212 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
9214 Make_Parameter_Specification (Loc,
9215 Defining_Identifier =>
9216 Make_Defining_Identifier (Loc, Name_Y),
9217 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
9218 Ret_Type => Standard_Boolean);
9219 Append_To (Res, Eq_Spec);
9221 if Has_Predef_Eq_Renaming then
9222 Renamed_Eq := Defining_Unit_Name (Specification (Eq_Spec));
9224 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
9225 while Present (Prim) loop
9227 -- Any renamings of equality that appeared before an
9228 -- overriding equality must be updated to refer to the
9229 -- entity for the predefined equality, otherwise calls via
9230 -- the renaming would get incorrectly resolved to call the
9231 -- user-defined equality function.
9233 if Is_Predefined_Eq_Renaming (Node (Prim)) then
9234 Set_Alias (Node (Prim), Renamed_Eq);
9236 -- Exit upon encountering a user-defined equality
9238 elsif Chars (Node (Prim)) = Name_Op_Eq
9239 and then No (Alias (Node (Prim)))
9240 then
9241 exit;
9242 end if;
9244 Next_Elmt (Prim);
9245 end loop;
9246 end if;
9247 end if;
9249 -- Spec for dispatching assignment
9251 Append_To (Res, Predef_Spec_Or_Body (Loc,
9252 Tag_Typ => Tag_Typ,
9253 Name => Name_uAssign,
9254 Profile => New_List (
9255 Make_Parameter_Specification (Loc,
9256 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
9257 Out_Present => True,
9258 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
9260 Make_Parameter_Specification (Loc,
9261 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
9262 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)))));
9263 end if;
9265 -- Ada 2005: Generate declarations for the following primitive
9266 -- operations for limited interfaces and synchronized types that
9267 -- implement a limited interface.
9269 -- Disp_Asynchronous_Select
9270 -- Disp_Conditional_Select
9271 -- Disp_Get_Prim_Op_Kind
9272 -- Disp_Get_Task_Id
9273 -- Disp_Requeue
9274 -- Disp_Timed_Select
9276 -- Disable the generation of these bodies if No_Dispatching_Calls,
9277 -- Ravenscar or ZFP is active.
9279 if Ada_Version >= Ada_2005
9280 and then not Restriction_Active (No_Dispatching_Calls)
9281 and then not Restriction_Active (No_Select_Statements)
9282 and then RTE_Available (RE_Select_Specific_Data)
9283 then
9284 -- These primitives are defined abstract in interface types
9286 if Is_Interface (Tag_Typ)
9287 and then Is_Limited_Record (Tag_Typ)
9288 then
9289 Append_To (Res,
9290 Make_Abstract_Subprogram_Declaration (Loc,
9291 Specification =>
9292 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
9294 Append_To (Res,
9295 Make_Abstract_Subprogram_Declaration (Loc,
9296 Specification =>
9297 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
9299 Append_To (Res,
9300 Make_Abstract_Subprogram_Declaration (Loc,
9301 Specification =>
9302 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
9304 Append_To (Res,
9305 Make_Abstract_Subprogram_Declaration (Loc,
9306 Specification =>
9307 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
9309 Append_To (Res,
9310 Make_Abstract_Subprogram_Declaration (Loc,
9311 Specification =>
9312 Make_Disp_Requeue_Spec (Tag_Typ)));
9314 Append_To (Res,
9315 Make_Abstract_Subprogram_Declaration (Loc,
9316 Specification =>
9317 Make_Disp_Timed_Select_Spec (Tag_Typ)));
9319 -- If ancestor is an interface type, declare non-abstract primitives
9320 -- to override the abstract primitives of the interface type.
9322 -- In VM targets we define these primitives in all root tagged types
9323 -- that are not interface types. Done because in VM targets we don't
9324 -- have secondary dispatch tables and any derivation of Tag_Typ may
9325 -- cover limited interfaces (which always have these primitives since
9326 -- they may be ancestors of synchronized interface types).
9328 elsif (not Is_Interface (Tag_Typ)
9329 and then Is_Interface (Etype (Tag_Typ))
9330 and then Is_Limited_Record (Etype (Tag_Typ)))
9331 or else
9332 (Is_Concurrent_Record_Type (Tag_Typ)
9333 and then Has_Interfaces (Tag_Typ))
9334 or else
9335 (not Tagged_Type_Expansion
9336 and then not Is_Interface (Tag_Typ)
9337 and then Tag_Typ = Root_Type (Tag_Typ))
9338 then
9339 Append_To (Res,
9340 Make_Subprogram_Declaration (Loc,
9341 Specification =>
9342 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
9344 Append_To (Res,
9345 Make_Subprogram_Declaration (Loc,
9346 Specification =>
9347 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
9349 Append_To (Res,
9350 Make_Subprogram_Declaration (Loc,
9351 Specification =>
9352 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
9354 Append_To (Res,
9355 Make_Subprogram_Declaration (Loc,
9356 Specification =>
9357 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
9359 Append_To (Res,
9360 Make_Subprogram_Declaration (Loc,
9361 Specification =>
9362 Make_Disp_Requeue_Spec (Tag_Typ)));
9364 Append_To (Res,
9365 Make_Subprogram_Declaration (Loc,
9366 Specification =>
9367 Make_Disp_Timed_Select_Spec (Tag_Typ)));
9368 end if;
9369 end if;
9371 -- All tagged types receive their own Deep_Adjust and Deep_Finalize
9372 -- regardless of whether they are controlled or may contain controlled
9373 -- components.
9375 -- Do not generate the routines if finalization is disabled
9377 if Restriction_Active (No_Finalization) then
9378 null;
9380 else
9381 if not Is_Limited_Type (Tag_Typ) then
9382 Append_To (Res, Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust));
9383 end if;
9385 Append_To (Res, Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize));
9386 end if;
9388 Predef_List := Res;
9389 end Make_Predefined_Primitive_Specs;
9391 -------------------------
9392 -- Make_Tag_Assignment --
9393 -------------------------
9395 function Make_Tag_Assignment (N : Node_Id) return Node_Id is
9396 Loc : constant Source_Ptr := Sloc (N);
9397 Def_If : constant Entity_Id := Defining_Identifier (N);
9398 Expr : constant Node_Id := Expression (N);
9399 Typ : constant Entity_Id := Etype (Def_If);
9400 Full_Typ : constant Entity_Id := Underlying_Type (Typ);
9401 New_Ref : Node_Id;
9403 begin
9404 -- This expansion activity is called during analysis, but cannot
9405 -- be applied in ASIS mode when other expansion is disabled.
9407 if Is_Tagged_Type (Typ)
9408 and then not Is_Class_Wide_Type (Typ)
9409 and then not Is_CPP_Class (Typ)
9410 and then Tagged_Type_Expansion
9411 and then Nkind (Expr) /= N_Aggregate
9412 and then not ASIS_Mode
9413 and then (Nkind (Expr) /= N_Qualified_Expression
9414 or else Nkind (Expression (Expr)) /= N_Aggregate)
9415 then
9416 New_Ref :=
9417 Make_Selected_Component (Loc,
9418 Prefix => New_Occurrence_Of (Def_If, Loc),
9419 Selector_Name =>
9420 New_Occurrence_Of (First_Tag_Component (Full_Typ), Loc));
9421 Set_Assignment_OK (New_Ref);
9423 return
9424 Make_Assignment_Statement (Loc,
9425 Name => New_Ref,
9426 Expression =>
9427 Unchecked_Convert_To (RTE (RE_Tag),
9428 New_Occurrence_Of (Node
9429 (First_Elmt (Access_Disp_Table (Full_Typ))), Loc)));
9430 else
9431 return Empty;
9432 end if;
9433 end Make_Tag_Assignment;
9435 ---------------------------------
9436 -- Needs_Simple_Initialization --
9437 ---------------------------------
9439 function Needs_Simple_Initialization
9440 (T : Entity_Id;
9441 Consider_IS : Boolean := True) return Boolean
9443 Consider_IS_NS : constant Boolean :=
9444 Normalize_Scalars or (Initialize_Scalars and Consider_IS);
9446 begin
9447 -- Never need initialization if it is suppressed
9449 if Initialization_Suppressed (T) then
9450 return False;
9451 end if;
9453 -- Check for private type, in which case test applies to the underlying
9454 -- type of the private type.
9456 if Is_Private_Type (T) then
9457 declare
9458 RT : constant Entity_Id := Underlying_Type (T);
9459 begin
9460 if Present (RT) then
9461 return Needs_Simple_Initialization (RT);
9462 else
9463 return False;
9464 end if;
9465 end;
9467 -- Scalar type with Default_Value aspect requires initialization
9469 elsif Is_Scalar_Type (T) and then Has_Default_Aspect (T) then
9470 return True;
9472 -- Cases needing simple initialization are access types, and, if pragma
9473 -- Normalize_Scalars or Initialize_Scalars is in effect, then all scalar
9474 -- types.
9476 elsif Is_Access_Type (T)
9477 or else (Consider_IS_NS and then (Is_Scalar_Type (T)))
9478 then
9479 return True;
9481 -- If Initialize/Normalize_Scalars is in effect, string objects also
9482 -- need initialization, unless they are created in the course of
9483 -- expanding an aggregate (since in the latter case they will be
9484 -- filled with appropriate initializing values before they are used).
9486 elsif Consider_IS_NS
9487 and then Is_Standard_String_Type (T)
9488 and then
9489 (not Is_Itype (T)
9490 or else Nkind (Associated_Node_For_Itype (T)) /= N_Aggregate)
9491 then
9492 return True;
9494 else
9495 return False;
9496 end if;
9497 end Needs_Simple_Initialization;
9499 ----------------------
9500 -- Predef_Deep_Spec --
9501 ----------------------
9503 function Predef_Deep_Spec
9504 (Loc : Source_Ptr;
9505 Tag_Typ : Entity_Id;
9506 Name : TSS_Name_Type;
9507 For_Body : Boolean := False) return Node_Id
9509 Formals : List_Id;
9511 begin
9512 -- V : in out Tag_Typ
9514 Formals := New_List (
9515 Make_Parameter_Specification (Loc,
9516 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
9517 In_Present => True,
9518 Out_Present => True,
9519 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)));
9521 -- F : Boolean := True
9523 if Name = TSS_Deep_Adjust
9524 or else Name = TSS_Deep_Finalize
9525 then
9526 Append_To (Formals,
9527 Make_Parameter_Specification (Loc,
9528 Defining_Identifier => Make_Defining_Identifier (Loc, Name_F),
9529 Parameter_Type => New_Occurrence_Of (Standard_Boolean, Loc),
9530 Expression => New_Occurrence_Of (Standard_True, Loc)));
9531 end if;
9533 return
9534 Predef_Spec_Or_Body (Loc,
9535 Name => Make_TSS_Name (Tag_Typ, Name),
9536 Tag_Typ => Tag_Typ,
9537 Profile => Formals,
9538 For_Body => For_Body);
9540 exception
9541 when RE_Not_Available =>
9542 return Empty;
9543 end Predef_Deep_Spec;
9545 -------------------------
9546 -- Predef_Spec_Or_Body --
9547 -------------------------
9549 function Predef_Spec_Or_Body
9550 (Loc : Source_Ptr;
9551 Tag_Typ : Entity_Id;
9552 Name : Name_Id;
9553 Profile : List_Id;
9554 Ret_Type : Entity_Id := Empty;
9555 For_Body : Boolean := False) return Node_Id
9557 Id : constant Entity_Id := Make_Defining_Identifier (Loc, Name);
9558 Spec : Node_Id;
9560 begin
9561 Set_Is_Public (Id, Is_Public (Tag_Typ));
9563 -- The internal flag is set to mark these declarations because they have
9564 -- specific properties. First, they are primitives even if they are not
9565 -- defined in the type scope (the freezing point is not necessarily in
9566 -- the same scope). Second, the predefined equality can be overridden by
9567 -- a user-defined equality, no body will be generated in this case.
9569 Set_Is_Internal (Id);
9571 if not Debug_Generated_Code then
9572 Set_Debug_Info_Off (Id);
9573 end if;
9575 if No (Ret_Type) then
9576 Spec :=
9577 Make_Procedure_Specification (Loc,
9578 Defining_Unit_Name => Id,
9579 Parameter_Specifications => Profile);
9580 else
9581 Spec :=
9582 Make_Function_Specification (Loc,
9583 Defining_Unit_Name => Id,
9584 Parameter_Specifications => Profile,
9585 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
9586 end if;
9588 if Is_Interface (Tag_Typ) then
9589 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
9591 -- If body case, return empty subprogram body. Note that this is ill-
9592 -- formed, because there is not even a null statement, and certainly not
9593 -- a return in the function case. The caller is expected to do surgery
9594 -- on the body to add the appropriate stuff.
9596 elsif For_Body then
9597 return Make_Subprogram_Body (Loc, Spec, Empty_List, Empty);
9599 -- For the case of an Input attribute predefined for an abstract type,
9600 -- generate an abstract specification. This will never be called, but we
9601 -- need the slot allocated in the dispatching table so that attributes
9602 -- typ'Class'Input and typ'Class'Output will work properly.
9604 elsif Is_TSS (Name, TSS_Stream_Input)
9605 and then Is_Abstract_Type (Tag_Typ)
9606 then
9607 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
9609 -- Normal spec case, where we return a subprogram declaration
9611 else
9612 return Make_Subprogram_Declaration (Loc, Spec);
9613 end if;
9614 end Predef_Spec_Or_Body;
9616 -----------------------------
9617 -- Predef_Stream_Attr_Spec --
9618 -----------------------------
9620 function Predef_Stream_Attr_Spec
9621 (Loc : Source_Ptr;
9622 Tag_Typ : Entity_Id;
9623 Name : TSS_Name_Type;
9624 For_Body : Boolean := False) return Node_Id
9626 Ret_Type : Entity_Id;
9628 begin
9629 if Name = TSS_Stream_Input then
9630 Ret_Type := Tag_Typ;
9631 else
9632 Ret_Type := Empty;
9633 end if;
9635 return
9636 Predef_Spec_Or_Body
9637 (Loc,
9638 Name => Make_TSS_Name (Tag_Typ, Name),
9639 Tag_Typ => Tag_Typ,
9640 Profile => Build_Stream_Attr_Profile (Loc, Tag_Typ, Name),
9641 Ret_Type => Ret_Type,
9642 For_Body => For_Body);
9643 end Predef_Stream_Attr_Spec;
9645 ---------------------------------
9646 -- Predefined_Primitive_Bodies --
9647 ---------------------------------
9649 function Predefined_Primitive_Bodies
9650 (Tag_Typ : Entity_Id;
9651 Renamed_Eq : Entity_Id) return List_Id
9653 Loc : constant Source_Ptr := Sloc (Tag_Typ);
9654 Res : constant List_Id := New_List;
9655 Adj_Call : Node_Id;
9656 Decl : Node_Id;
9657 Fin_Call : Node_Id;
9658 Prim : Elmt_Id;
9659 Eq_Needed : Boolean;
9660 Eq_Name : Name_Id;
9661 Ent : Entity_Id;
9663 pragma Warnings (Off, Ent);
9665 begin
9666 pragma Assert (not Is_Interface (Tag_Typ));
9668 -- See if we have a predefined "=" operator
9670 if Present (Renamed_Eq) then
9671 Eq_Needed := True;
9672 Eq_Name := Chars (Renamed_Eq);
9674 -- If the parent is an interface type then it has defined all the
9675 -- predefined primitives abstract and we need to check if the type
9676 -- has some user defined "=" function which matches the profile of
9677 -- the Ada predefined equality operator to avoid generating it.
9679 elsif Is_Interface (Etype (Tag_Typ)) then
9680 Eq_Needed := True;
9681 Eq_Name := Name_Op_Eq;
9683 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
9684 while Present (Prim) loop
9685 if Chars (Node (Prim)) = Name_Op_Eq
9686 and then not Is_Internal (Node (Prim))
9687 and then Present (First_Entity (Node (Prim)))
9689 -- The predefined equality primitive must have exactly two
9690 -- formals whose type is this tagged type
9692 and then Present (Last_Entity (Node (Prim)))
9693 and then Next_Entity (First_Entity (Node (Prim)))
9694 = Last_Entity (Node (Prim))
9695 and then Etype (First_Entity (Node (Prim))) = Tag_Typ
9696 and then Etype (Last_Entity (Node (Prim))) = Tag_Typ
9697 then
9698 Eq_Needed := False;
9699 Eq_Name := No_Name;
9700 exit;
9701 end if;
9703 Next_Elmt (Prim);
9704 end loop;
9706 else
9707 Eq_Needed := False;
9708 Eq_Name := No_Name;
9710 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
9711 while Present (Prim) loop
9712 if Chars (Node (Prim)) = Name_Op_Eq
9713 and then Is_Internal (Node (Prim))
9714 then
9715 Eq_Needed := True;
9716 Eq_Name := Name_Op_Eq;
9717 exit;
9718 end if;
9720 Next_Elmt (Prim);
9721 end loop;
9722 end if;
9724 -- Body of _Size
9726 Decl := Predef_Spec_Or_Body (Loc,
9727 Tag_Typ => Tag_Typ,
9728 Name => Name_uSize,
9729 Profile => New_List (
9730 Make_Parameter_Specification (Loc,
9731 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
9732 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
9734 Ret_Type => Standard_Long_Long_Integer,
9735 For_Body => True);
9737 Set_Handled_Statement_Sequence (Decl,
9738 Make_Handled_Sequence_Of_Statements (Loc, New_List (
9739 Make_Simple_Return_Statement (Loc,
9740 Expression =>
9741 Make_Attribute_Reference (Loc,
9742 Prefix => Make_Identifier (Loc, Name_X),
9743 Attribute_Name => Name_Size)))));
9745 Append_To (Res, Decl);
9747 -- Bodies for Dispatching stream IO routines. We need these only for
9748 -- non-limited types (in the limited case there is no dispatching).
9749 -- We also skip them if dispatching or finalization are not available
9750 -- or if stream operations are prohibited by restriction No_Streams or
9751 -- from use of pragma/aspect No_Tagged_Streams.
9753 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Read)
9754 and then No (TSS (Tag_Typ, TSS_Stream_Read))
9755 then
9756 Build_Record_Read_Procedure (Loc, Tag_Typ, Decl, Ent);
9757 Append_To (Res, Decl);
9758 end if;
9760 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Write)
9761 and then No (TSS (Tag_Typ, TSS_Stream_Write))
9762 then
9763 Build_Record_Write_Procedure (Loc, Tag_Typ, Decl, Ent);
9764 Append_To (Res, Decl);
9765 end if;
9767 -- Skip body of _Input for the abstract case, since the corresponding
9768 -- spec is abstract (see Predef_Spec_Or_Body).
9770 if not Is_Abstract_Type (Tag_Typ)
9771 and then Stream_Operation_OK (Tag_Typ, TSS_Stream_Input)
9772 and then No (TSS (Tag_Typ, TSS_Stream_Input))
9773 then
9774 Build_Record_Or_Elementary_Input_Function
9775 (Loc, Tag_Typ, Decl, Ent);
9776 Append_To (Res, Decl);
9777 end if;
9779 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Output)
9780 and then No (TSS (Tag_Typ, TSS_Stream_Output))
9781 then
9782 Build_Record_Or_Elementary_Output_Procedure (Loc, Tag_Typ, Decl, Ent);
9783 Append_To (Res, Decl);
9784 end if;
9786 -- Ada 2005: Generate bodies for the following primitive operations for
9787 -- limited interfaces and synchronized types that implement a limited
9788 -- interface.
9790 -- disp_asynchronous_select
9791 -- disp_conditional_select
9792 -- disp_get_prim_op_kind
9793 -- disp_get_task_id
9794 -- disp_timed_select
9796 -- The interface versions will have null bodies
9798 -- Disable the generation of these bodies if No_Dispatching_Calls,
9799 -- Ravenscar or ZFP is active.
9801 -- In VM targets we define these primitives in all root tagged types
9802 -- that are not interface types. Done because in VM targets we don't
9803 -- have secondary dispatch tables and any derivation of Tag_Typ may
9804 -- cover limited interfaces (which always have these primitives since
9805 -- they may be ancestors of synchronized interface types).
9807 if Ada_Version >= Ada_2005
9808 and then not Is_Interface (Tag_Typ)
9809 and then
9810 ((Is_Interface (Etype (Tag_Typ))
9811 and then Is_Limited_Record (Etype (Tag_Typ)))
9812 or else
9813 (Is_Concurrent_Record_Type (Tag_Typ)
9814 and then Has_Interfaces (Tag_Typ))
9815 or else
9816 (not Tagged_Type_Expansion
9817 and then Tag_Typ = Root_Type (Tag_Typ)))
9818 and then not Restriction_Active (No_Dispatching_Calls)
9819 and then not Restriction_Active (No_Select_Statements)
9820 and then RTE_Available (RE_Select_Specific_Data)
9821 then
9822 Append_To (Res, Make_Disp_Asynchronous_Select_Body (Tag_Typ));
9823 Append_To (Res, Make_Disp_Conditional_Select_Body (Tag_Typ));
9824 Append_To (Res, Make_Disp_Get_Prim_Op_Kind_Body (Tag_Typ));
9825 Append_To (Res, Make_Disp_Get_Task_Id_Body (Tag_Typ));
9826 Append_To (Res, Make_Disp_Requeue_Body (Tag_Typ));
9827 Append_To (Res, Make_Disp_Timed_Select_Body (Tag_Typ));
9828 end if;
9830 if not Is_Limited_Type (Tag_Typ) and then not Is_Interface (Tag_Typ) then
9832 -- Body for equality
9834 if Eq_Needed then
9835 Decl := Make_Eq_Body (Tag_Typ, Eq_Name);
9836 Append_To (Res, Decl);
9837 end if;
9839 -- Body for inequality (if required)
9841 Decl := Make_Neq_Body (Tag_Typ);
9843 if Present (Decl) then
9844 Append_To (Res, Decl);
9845 end if;
9847 -- Body for dispatching assignment
9849 Decl :=
9850 Predef_Spec_Or_Body (Loc,
9851 Tag_Typ => Tag_Typ,
9852 Name => Name_uAssign,
9853 Profile => New_List (
9854 Make_Parameter_Specification (Loc,
9855 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
9856 Out_Present => True,
9857 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
9859 Make_Parameter_Specification (Loc,
9860 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
9861 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
9862 For_Body => True);
9864 Set_Handled_Statement_Sequence (Decl,
9865 Make_Handled_Sequence_Of_Statements (Loc, New_List (
9866 Make_Assignment_Statement (Loc,
9867 Name => Make_Identifier (Loc, Name_X),
9868 Expression => Make_Identifier (Loc, Name_Y)))));
9870 Append_To (Res, Decl);
9871 end if;
9873 -- Generate empty bodies of routines Deep_Adjust and Deep_Finalize for
9874 -- tagged types which do not contain controlled components.
9876 -- Do not generate the routines if finalization is disabled
9878 if Restriction_Active (No_Finalization) then
9879 null;
9881 elsif not Has_Controlled_Component (Tag_Typ) then
9882 if not Is_Limited_Type (Tag_Typ) then
9883 Adj_Call := Empty;
9884 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust, True);
9886 if Is_Controlled (Tag_Typ) then
9887 Adj_Call :=
9888 Make_Adjust_Call (
9889 Obj_Ref => Make_Identifier (Loc, Name_V),
9890 Typ => Tag_Typ);
9891 end if;
9893 if No (Adj_Call) then
9894 Adj_Call := Make_Null_Statement (Loc);
9895 end if;
9897 Set_Handled_Statement_Sequence (Decl,
9898 Make_Handled_Sequence_Of_Statements (Loc,
9899 Statements => New_List (Adj_Call)));
9901 Append_To (Res, Decl);
9902 end if;
9904 Fin_Call := Empty;
9905 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize, True);
9907 if Is_Controlled (Tag_Typ) then
9908 Fin_Call :=
9909 Make_Final_Call
9910 (Obj_Ref => Make_Identifier (Loc, Name_V),
9911 Typ => Tag_Typ);
9912 end if;
9914 if No (Fin_Call) then
9915 Fin_Call := Make_Null_Statement (Loc);
9916 end if;
9918 Set_Handled_Statement_Sequence (Decl,
9919 Make_Handled_Sequence_Of_Statements (Loc,
9920 Statements => New_List (Fin_Call)));
9922 Append_To (Res, Decl);
9923 end if;
9925 return Res;
9926 end Predefined_Primitive_Bodies;
9928 ---------------------------------
9929 -- Predefined_Primitive_Freeze --
9930 ---------------------------------
9932 function Predefined_Primitive_Freeze
9933 (Tag_Typ : Entity_Id) return List_Id
9935 Res : constant List_Id := New_List;
9936 Prim : Elmt_Id;
9937 Frnodes : List_Id;
9939 begin
9940 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
9941 while Present (Prim) loop
9942 if Is_Predefined_Dispatching_Operation (Node (Prim)) then
9943 Frnodes := Freeze_Entity (Node (Prim), Tag_Typ);
9945 if Present (Frnodes) then
9946 Append_List_To (Res, Frnodes);
9947 end if;
9948 end if;
9950 Next_Elmt (Prim);
9951 end loop;
9953 return Res;
9954 end Predefined_Primitive_Freeze;
9956 -------------------------
9957 -- Stream_Operation_OK --
9958 -------------------------
9960 function Stream_Operation_OK
9961 (Typ : Entity_Id;
9962 Operation : TSS_Name_Type) return Boolean
9964 Has_Predefined_Or_Specified_Stream_Attribute : Boolean := False;
9966 begin
9967 -- Special case of a limited type extension: a default implementation
9968 -- of the stream attributes Read or Write exists if that attribute
9969 -- has been specified or is available for an ancestor type; a default
9970 -- implementation of the attribute Output (resp. Input) exists if the
9971 -- attribute has been specified or Write (resp. Read) is available for
9972 -- an ancestor type. The last condition only applies under Ada 2005.
9974 if Is_Limited_Type (Typ) and then Is_Tagged_Type (Typ) then
9975 if Operation = TSS_Stream_Read then
9976 Has_Predefined_Or_Specified_Stream_Attribute :=
9977 Has_Specified_Stream_Read (Typ);
9979 elsif Operation = TSS_Stream_Write then
9980 Has_Predefined_Or_Specified_Stream_Attribute :=
9981 Has_Specified_Stream_Write (Typ);
9983 elsif Operation = TSS_Stream_Input then
9984 Has_Predefined_Or_Specified_Stream_Attribute :=
9985 Has_Specified_Stream_Input (Typ)
9986 or else
9987 (Ada_Version >= Ada_2005
9988 and then Stream_Operation_OK (Typ, TSS_Stream_Read));
9990 elsif Operation = TSS_Stream_Output then
9991 Has_Predefined_Or_Specified_Stream_Attribute :=
9992 Has_Specified_Stream_Output (Typ)
9993 or else
9994 (Ada_Version >= Ada_2005
9995 and then Stream_Operation_OK (Typ, TSS_Stream_Write));
9996 end if;
9998 -- Case of inherited TSS_Stream_Read or TSS_Stream_Write
10000 if not Has_Predefined_Or_Specified_Stream_Attribute
10001 and then Is_Derived_Type (Typ)
10002 and then (Operation = TSS_Stream_Read
10003 or else Operation = TSS_Stream_Write)
10004 then
10005 Has_Predefined_Or_Specified_Stream_Attribute :=
10006 Present
10007 (Find_Inherited_TSS (Base_Type (Etype (Typ)), Operation));
10008 end if;
10009 end if;
10011 -- If the type is not limited, or else is limited but the attribute is
10012 -- explicitly specified or is predefined for the type, then return True,
10013 -- unless other conditions prevail, such as restrictions prohibiting
10014 -- streams or dispatching operations. We also return True for limited
10015 -- interfaces, because they may be extended by nonlimited types and
10016 -- permit inheritance in this case (addresses cases where an abstract
10017 -- extension doesn't get 'Input declared, as per comments below, but
10018 -- 'Class'Input must still be allowed). Note that attempts to apply
10019 -- stream attributes to a limited interface or its class-wide type
10020 -- (or limited extensions thereof) will still get properly rejected
10021 -- by Check_Stream_Attribute.
10023 -- We exclude the Input operation from being a predefined subprogram in
10024 -- the case where the associated type is an abstract extension, because
10025 -- the attribute is not callable in that case, per 13.13.2(49/2). Also,
10026 -- we don't want an abstract version created because types derived from
10027 -- the abstract type may not even have Input available (for example if
10028 -- derived from a private view of the abstract type that doesn't have
10029 -- a visible Input).
10031 -- Do not generate stream routines for type Finalization_Master because
10032 -- a master may never appear in types and therefore cannot be read or
10033 -- written.
10035 return
10036 (not Is_Limited_Type (Typ)
10037 or else Is_Interface (Typ)
10038 or else Has_Predefined_Or_Specified_Stream_Attribute)
10039 and then
10040 (Operation /= TSS_Stream_Input
10041 or else not Is_Abstract_Type (Typ)
10042 or else not Is_Derived_Type (Typ))
10043 and then not Has_Unknown_Discriminants (Typ)
10044 and then not
10045 (Is_Interface (Typ)
10046 and then
10047 (Is_Task_Interface (Typ)
10048 or else Is_Protected_Interface (Typ)
10049 or else Is_Synchronized_Interface (Typ)))
10050 and then not Restriction_Active (No_Streams)
10051 and then not Restriction_Active (No_Dispatch)
10052 and then No (No_Tagged_Streams_Pragma (Typ))
10053 and then not No_Run_Time_Mode
10054 and then RTE_Available (RE_Tag)
10055 and then No (Type_Without_Stream_Operation (Typ))
10056 and then RTE_Available (RE_Root_Stream_Type)
10057 and then not Is_RTE (Typ, RE_Finalization_Master);
10058 end Stream_Operation_OK;
10060 end Exp_Ch3;