[19/77] Add a smallest_int_mode_for_size helper function
[official-gcc.git] / gcc / ada / exp_ch3.adb
blob00d74c5fe1fe8c661277cbb37472cd68b84a880c
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ C H 3 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2017, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Errout; use Errout;
31 with Exp_Aggr; use Exp_Aggr;
32 with Exp_Atag; use Exp_Atag;
33 with Exp_Ch4; use Exp_Ch4;
34 with Exp_Ch6; use Exp_Ch6;
35 with Exp_Ch7; use Exp_Ch7;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Dbug; use Exp_Dbug;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Dist; use Exp_Dist;
40 with Exp_Smem; use Exp_Smem;
41 with Exp_Strm; use Exp_Strm;
42 with Exp_Tss; use Exp_Tss;
43 with Exp_Util; use Exp_Util;
44 with Freeze; use Freeze;
45 with Ghost; use Ghost;
46 with 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;
1435 -- Nothing to do for an array of controlled components that have only
1436 -- the inherited Initialize primitive. This is a useful optimization
1437 -- for CodePeer.
1439 elsif Is_Trivial_Subprogram (Proc)
1440 and then Is_Array_Type (Full_Init_Type)
1441 then
1442 return New_List (Make_Null_Statement (Loc));
1443 end if;
1445 -- Use the [underlying] full view when dealing with a private type. This
1446 -- may require several steps depending on derivations.
1448 Full_Type := Typ;
1449 loop
1450 if Is_Private_Type (Full_Type) then
1451 if Present (Full_View (Full_Type)) then
1452 Full_Type := Full_View (Full_Type);
1454 elsif Present (Underlying_Full_View (Full_Type)) then
1455 Full_Type := Underlying_Full_View (Full_Type);
1457 -- When a private type acts as a generic actual and lacks a full
1458 -- view, use the base type.
1460 elsif Is_Generic_Actual_Type (Full_Type) then
1461 Full_Type := Base_Type (Full_Type);
1463 elsif Ekind (Full_Type) = E_Private_Subtype
1464 and then (not Has_Discriminants (Full_Type)
1465 or else No (Discriminant_Constraint (Full_Type)))
1466 then
1467 Full_Type := Etype (Full_Type);
1469 -- The loop has recovered the [underlying] full view, stop the
1470 -- traversal.
1472 else
1473 exit;
1474 end if;
1476 -- The type is not private, nothing to do
1478 else
1479 exit;
1480 end if;
1481 end loop;
1483 -- If Typ is derived, the procedure is the initialization procedure for
1484 -- the root type. Wrap the argument in an conversion to make it type
1485 -- honest. Actually it isn't quite type honest, because there can be
1486 -- conflicts of views in the private type case. That is why we set
1487 -- Conversion_OK in the conversion node.
1489 if (Is_Record_Type (Typ)
1490 or else Is_Array_Type (Typ)
1491 or else Is_Private_Type (Typ))
1492 and then Init_Type /= Base_Type (Typ)
1493 then
1494 First_Arg := OK_Convert_To (Etype (Init_Type), Id_Ref);
1495 Set_Etype (First_Arg, Init_Type);
1497 else
1498 First_Arg := Id_Ref;
1499 end if;
1501 Args := New_List (Convert_Concurrent (First_Arg, Typ));
1503 -- In the tasks case, add _Master as the value of the _Master parameter
1504 -- and _Chain as the value of the _Chain parameter. At the outer level,
1505 -- these will be variables holding the corresponding values obtained
1506 -- from GNARL. At inner levels, they will be the parameters passed down
1507 -- through the outer routines.
1509 if Has_Task (Full_Type) then
1510 if Restriction_Active (No_Task_Hierarchy) then
1511 Append_To (Args,
1512 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
1513 else
1514 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
1515 end if;
1517 -- Add _Chain (not done for sequential elaboration policy, see
1518 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
1520 if Partition_Elaboration_Policy /= 'S' then
1521 Append_To (Args, Make_Identifier (Loc, Name_uChain));
1522 end if;
1524 -- Ada 2005 (AI-287): In case of default initialized components
1525 -- with tasks, we generate a null string actual parameter.
1526 -- This is just a workaround that must be improved later???
1528 if With_Default_Init then
1529 Append_To (Args,
1530 Make_String_Literal (Loc,
1531 Strval => ""));
1533 else
1534 Decls :=
1535 Build_Task_Image_Decls (Loc, Id_Ref, Enclos_Type, In_Init_Proc);
1536 Decl := Last (Decls);
1538 Append_To (Args,
1539 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
1540 Append_List (Decls, Res);
1541 end if;
1543 else
1544 Decls := No_List;
1545 Decl := Empty;
1546 end if;
1548 -- Add discriminant values if discriminants are present
1550 if Has_Discriminants (Full_Init_Type) then
1551 Discr := First_Discriminant (Full_Init_Type);
1552 while Present (Discr) loop
1554 -- If this is a discriminated concurrent type, the init_proc
1555 -- for the corresponding record is being called. Use that type
1556 -- directly to find the discriminant value, to handle properly
1557 -- intervening renamed discriminants.
1559 declare
1560 T : Entity_Id := Full_Type;
1562 begin
1563 if Is_Protected_Type (T) then
1564 T := Corresponding_Record_Type (T);
1565 end if;
1567 Arg :=
1568 Get_Discriminant_Value (
1569 Discr,
1571 Discriminant_Constraint (Full_Type));
1572 end;
1574 -- If the target has access discriminants, and is constrained by
1575 -- an access to the enclosing construct, i.e. a current instance,
1576 -- replace the reference to the type by a reference to the object.
1578 if Nkind (Arg) = N_Attribute_Reference
1579 and then Is_Access_Type (Etype (Arg))
1580 and then Is_Entity_Name (Prefix (Arg))
1581 and then Is_Type (Entity (Prefix (Arg)))
1582 then
1583 Arg :=
1584 Make_Attribute_Reference (Loc,
1585 Prefix => New_Copy (Prefix (Id_Ref)),
1586 Attribute_Name => Name_Unrestricted_Access);
1588 elsif In_Init_Proc then
1590 -- Replace any possible references to the discriminant in the
1591 -- call to the record initialization procedure with references
1592 -- to the appropriate formal parameter.
1594 if Nkind (Arg) = N_Identifier
1595 and then Ekind (Entity (Arg)) = E_Discriminant
1596 then
1597 Arg := New_Occurrence_Of (Discriminal (Entity (Arg)), Loc);
1599 -- Otherwise make a copy of the default expression. Note that
1600 -- we use the current Sloc for this, because we do not want the
1601 -- call to appear to be at the declaration point. Within the
1602 -- expression, replace discriminants with their discriminals.
1604 else
1605 Arg :=
1606 New_Copy_Tree (Arg, Map => Discr_Map, New_Sloc => Loc);
1607 end if;
1609 else
1610 if Is_Constrained (Full_Type) then
1611 Arg := Duplicate_Subexpr_No_Checks (Arg);
1612 else
1613 -- The constraints come from the discriminant default exps,
1614 -- they must be reevaluated, so we use New_Copy_Tree but we
1615 -- ensure the proper Sloc (for any embedded calls).
1616 -- In addition, if a predicate check is needed on the value
1617 -- of the discriminant, insert it ahead of the call.
1619 Arg := New_Copy_Tree (Arg, New_Sloc => Loc);
1620 end if;
1622 if Has_Predicates (Etype (Discr)) then
1623 Check_Predicated_Discriminant (Arg, Discr);
1624 end if;
1625 end if;
1627 -- Ada 2005 (AI-287): In case of default initialized components,
1628 -- if the component is constrained with a discriminant of the
1629 -- enclosing type, we need to generate the corresponding selected
1630 -- component node to access the discriminant value. In other cases
1631 -- this is not required, either because we are inside the init
1632 -- proc and we use the corresponding formal, or else because the
1633 -- component is constrained by an expression.
1635 if With_Default_Init
1636 and then Nkind (Id_Ref) = N_Selected_Component
1637 and then Nkind (Arg) = N_Identifier
1638 and then Ekind (Entity (Arg)) = E_Discriminant
1639 then
1640 Append_To (Args,
1641 Make_Selected_Component (Loc,
1642 Prefix => New_Copy_Tree (Prefix (Id_Ref)),
1643 Selector_Name => Arg));
1644 else
1645 Append_To (Args, Arg);
1646 end if;
1648 Next_Discriminant (Discr);
1649 end loop;
1650 end if;
1652 -- If this is a call to initialize the parent component of a derived
1653 -- tagged type, indicate that the tag should not be set in the parent.
1655 if Is_Tagged_Type (Full_Init_Type)
1656 and then not Is_CPP_Class (Full_Init_Type)
1657 and then Nkind (Id_Ref) = N_Selected_Component
1658 and then Chars (Selector_Name (Id_Ref)) = Name_uParent
1659 then
1660 Append_To (Args, New_Occurrence_Of (Standard_False, Loc));
1662 elsif Present (Constructor_Ref) then
1663 Append_List_To (Args,
1664 New_Copy_List (Parameter_Associations (Constructor_Ref)));
1665 end if;
1667 Append_To (Res,
1668 Make_Procedure_Call_Statement (Loc,
1669 Name => New_Occurrence_Of (Proc, Loc),
1670 Parameter_Associations => Args));
1672 if Needs_Finalization (Typ)
1673 and then Nkind (Id_Ref) = N_Selected_Component
1674 then
1675 if Chars (Selector_Name (Id_Ref)) /= Name_uParent then
1676 Init_Call :=
1677 Make_Init_Call
1678 (Obj_Ref => New_Copy_Tree (First_Arg),
1679 Typ => Typ);
1681 -- Guard against a missing [Deep_]Initialize when the type was not
1682 -- properly frozen.
1684 if Present (Init_Call) then
1685 Append_To (Res, Init_Call);
1686 end if;
1687 end if;
1688 end if;
1690 return Res;
1692 exception
1693 when RE_Not_Available =>
1694 return Empty_List;
1695 end Build_Initialization_Call;
1697 ----------------------------
1698 -- Build_Record_Init_Proc --
1699 ----------------------------
1701 procedure Build_Record_Init_Proc (N : Node_Id; Rec_Ent : Entity_Id) is
1702 Decls : constant List_Id := New_List;
1703 Discr_Map : constant Elist_Id := New_Elmt_List;
1704 Loc : constant Source_Ptr := Sloc (Rec_Ent);
1705 Counter : Nat := 0;
1706 Proc_Id : Entity_Id;
1707 Rec_Type : Entity_Id;
1708 Set_Tag : Entity_Id := Empty;
1710 function Build_Assignment (Id : Entity_Id; N : Node_Id) return List_Id;
1711 -- Build an assignment statement which assigns the default expression
1712 -- to its corresponding record component if defined. The left hand side
1713 -- of the assignment is marked Assignment_OK so that initialization of
1714 -- limited private records works correctly. This routine may also build
1715 -- an adjustment call if the component is controlled.
1717 procedure Build_Discriminant_Assignments (Statement_List : List_Id);
1718 -- If the record has discriminants, add assignment statements to
1719 -- Statement_List to initialize the discriminant values from the
1720 -- arguments of the initialization procedure.
1722 function Build_Init_Statements (Comp_List : Node_Id) return List_Id;
1723 -- Build a list representing a sequence of statements which initialize
1724 -- components of the given component list. This may involve building
1725 -- case statements for the variant parts. Append any locally declared
1726 -- objects on list Decls.
1728 function Build_Init_Call_Thru (Parameters : List_Id) return List_Id;
1729 -- Given an untagged type-derivation that declares discriminants, e.g.
1731 -- type R (R1, R2 : Integer) is record ... end record;
1732 -- type D (D1 : Integer) is new R (1, D1);
1734 -- we make the _init_proc of D be
1736 -- procedure _init_proc (X : D; D1 : Integer) is
1737 -- begin
1738 -- _init_proc (R (X), 1, D1);
1739 -- end _init_proc;
1741 -- This function builds the call statement in this _init_proc.
1743 procedure Build_CPP_Init_Procedure;
1744 -- Build the tree corresponding to the procedure specification and body
1745 -- of the IC procedure that initializes the C++ part of the dispatch
1746 -- table of an Ada tagged type that is a derivation of a CPP type.
1747 -- Install it as the CPP_Init TSS.
1749 procedure Build_Init_Procedure;
1750 -- Build the tree corresponding to the procedure specification and body
1751 -- of the initialization procedure and install it as the _init TSS.
1753 procedure Build_Offset_To_Top_Functions;
1754 -- Ada 2005 (AI-251): Build the tree corresponding to the procedure spec
1755 -- and body of Offset_To_Top, a function used in conjuction with types
1756 -- having secondary dispatch tables.
1758 procedure Build_Record_Checks (S : Node_Id; Check_List : List_Id);
1759 -- Add range checks to components of discriminated records. S is a
1760 -- subtype indication of a record component. Check_List is a list
1761 -- to which the check actions are appended.
1763 function Component_Needs_Simple_Initialization
1764 (T : Entity_Id) return Boolean;
1765 -- Determine if a component needs simple initialization, given its type
1766 -- T. This routine is the same as Needs_Simple_Initialization except for
1767 -- components of type Tag and Interface_Tag. These two access types do
1768 -- not require initialization since they are explicitly initialized by
1769 -- other means.
1771 function Parent_Subtype_Renaming_Discrims return Boolean;
1772 -- Returns True for base types N that rename discriminants, else False
1774 function Requires_Init_Proc (Rec_Id : Entity_Id) return Boolean;
1775 -- Determine whether a record initialization procedure needs to be
1776 -- generated for the given record type.
1778 ----------------------
1779 -- Build_Assignment --
1780 ----------------------
1782 function Build_Assignment (Id : Entity_Id; N : Node_Id) return List_Id is
1783 N_Loc : constant Source_Ptr := Sloc (N);
1784 Typ : constant Entity_Id := Underlying_Type (Etype (Id));
1786 Adj_Call : Node_Id;
1787 Exp : Node_Id := N;
1788 Kind : Node_Kind := Nkind (N);
1789 Lhs : Node_Id;
1790 Res : List_Id;
1792 begin
1793 Lhs :=
1794 Make_Selected_Component (N_Loc,
1795 Prefix => Make_Identifier (Loc, Name_uInit),
1796 Selector_Name => New_Occurrence_Of (Id, N_Loc));
1797 Set_Assignment_OK (Lhs);
1799 -- Case of an access attribute applied to the current instance.
1800 -- Replace the reference to the type by a reference to the actual
1801 -- object. (Note that this handles the case of the top level of
1802 -- the expression being given by such an attribute, but does not
1803 -- cover uses nested within an initial value expression. Nested
1804 -- uses are unlikely to occur in practice, but are theoretically
1805 -- possible.) It is not clear how to handle them without fully
1806 -- traversing the expression. ???
1808 if Kind = N_Attribute_Reference
1809 and then Nam_In (Attribute_Name (N), Name_Unchecked_Access,
1810 Name_Unrestricted_Access)
1811 and then Is_Entity_Name (Prefix (N))
1812 and then Is_Type (Entity (Prefix (N)))
1813 and then Entity (Prefix (N)) = Rec_Type
1814 then
1815 Exp :=
1816 Make_Attribute_Reference (N_Loc,
1817 Prefix =>
1818 Make_Identifier (N_Loc, Name_uInit),
1819 Attribute_Name => Name_Unrestricted_Access);
1820 end if;
1822 -- Take a copy of Exp to ensure that later copies of this component
1823 -- declaration in derived types see the original tree, not a node
1824 -- rewritten during expansion of the init_proc. If the copy contains
1825 -- itypes, the scope of the new itypes is the init_proc being built.
1827 Exp := New_Copy_Tree (Exp, New_Scope => Proc_Id);
1829 Res := New_List (
1830 Make_Assignment_Statement (Loc,
1831 Name => Lhs,
1832 Expression => Exp));
1834 Set_No_Ctrl_Actions (First (Res));
1836 -- Adjust the tag if tagged (because of possible view conversions).
1837 -- Suppress the tag adjustment when not Tagged_Type_Expansion because
1838 -- tags are represented implicitly in objects.
1840 if Is_Tagged_Type (Typ) and then Tagged_Type_Expansion then
1841 Append_To (Res,
1842 Make_Assignment_Statement (N_Loc,
1843 Name =>
1844 Make_Selected_Component (N_Loc,
1845 Prefix =>
1846 New_Copy_Tree (Lhs, New_Scope => Proc_Id),
1847 Selector_Name =>
1848 New_Occurrence_Of (First_Tag_Component (Typ), N_Loc)),
1850 Expression =>
1851 Unchecked_Convert_To (RTE (RE_Tag),
1852 New_Occurrence_Of
1853 (Node
1854 (First_Elmt
1855 (Access_Disp_Table (Underlying_Type (Typ)))),
1856 N_Loc))));
1857 end if;
1859 -- Adjust the component if controlled except if it is an aggregate
1860 -- that will be expanded inline.
1862 if Kind = N_Qualified_Expression then
1863 Kind := Nkind (Expression (N));
1864 end if;
1866 if Needs_Finalization (Typ)
1867 and then not (Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate))
1868 and then not Is_Limited_View (Typ)
1869 then
1870 Adj_Call :=
1871 Make_Adjust_Call
1872 (Obj_Ref => New_Copy_Tree (Lhs),
1873 Typ => Etype (Id));
1875 -- Guard against a missing [Deep_]Adjust when the component type
1876 -- was not properly frozen.
1878 if Present (Adj_Call) then
1879 Append_To (Res, Adj_Call);
1880 end if;
1881 end if;
1883 -- If a component type has a predicate, add check to the component
1884 -- assignment. Discriminants are handled at the point of the call,
1885 -- which provides for a better error message.
1887 if Comes_From_Source (Exp)
1888 and then Has_Predicates (Typ)
1889 and then not Predicate_Checks_Suppressed (Empty)
1890 and then not Predicates_Ignored (Typ)
1891 then
1892 Append (Make_Predicate_Check (Typ, Exp), Res);
1893 end if;
1895 return Res;
1897 exception
1898 when RE_Not_Available =>
1899 return Empty_List;
1900 end Build_Assignment;
1902 ------------------------------------
1903 -- Build_Discriminant_Assignments --
1904 ------------------------------------
1906 procedure Build_Discriminant_Assignments (Statement_List : List_Id) is
1907 Is_Tagged : constant Boolean := Is_Tagged_Type (Rec_Type);
1908 D : Entity_Id;
1909 D_Loc : Source_Ptr;
1911 begin
1912 if Has_Discriminants (Rec_Type)
1913 and then not Is_Unchecked_Union (Rec_Type)
1914 then
1915 D := First_Discriminant (Rec_Type);
1916 while Present (D) loop
1918 -- Don't generate the assignment for discriminants in derived
1919 -- tagged types if the discriminant is a renaming of some
1920 -- ancestor discriminant. This initialization will be done
1921 -- when initializing the _parent field of the derived record.
1923 if Is_Tagged
1924 and then Present (Corresponding_Discriminant (D))
1925 then
1926 null;
1928 else
1929 D_Loc := Sloc (D);
1930 Append_List_To (Statement_List,
1931 Build_Assignment (D,
1932 New_Occurrence_Of (Discriminal (D), D_Loc)));
1933 end if;
1935 Next_Discriminant (D);
1936 end loop;
1937 end if;
1938 end Build_Discriminant_Assignments;
1940 --------------------------
1941 -- Build_Init_Call_Thru --
1942 --------------------------
1944 function Build_Init_Call_Thru (Parameters : List_Id) return List_Id is
1945 Parent_Proc : constant Entity_Id :=
1946 Base_Init_Proc (Etype (Rec_Type));
1948 Parent_Type : constant Entity_Id :=
1949 Etype (First_Formal (Parent_Proc));
1951 Uparent_Type : constant Entity_Id :=
1952 Underlying_Type (Parent_Type);
1954 First_Discr_Param : Node_Id;
1956 Arg : Node_Id;
1957 Args : List_Id;
1958 First_Arg : Node_Id;
1959 Parent_Discr : Entity_Id;
1960 Res : List_Id;
1962 begin
1963 -- First argument (_Init) is the object to be initialized.
1964 -- ??? not sure where to get a reasonable Loc for First_Arg
1966 First_Arg :=
1967 OK_Convert_To (Parent_Type,
1968 New_Occurrence_Of
1969 (Defining_Identifier (First (Parameters)), Loc));
1971 Set_Etype (First_Arg, Parent_Type);
1973 Args := New_List (Convert_Concurrent (First_Arg, Rec_Type));
1975 -- In the tasks case,
1976 -- add _Master as the value of the _Master parameter
1977 -- add _Chain as the value of the _Chain parameter.
1978 -- add _Task_Name as the value of the _Task_Name parameter.
1979 -- At the outer level, these will be variables holding the
1980 -- corresponding values obtained from GNARL or the expander.
1982 -- At inner levels, they will be the parameters passed down through
1983 -- the outer routines.
1985 First_Discr_Param := Next (First (Parameters));
1987 if Has_Task (Rec_Type) then
1988 if Restriction_Active (No_Task_Hierarchy) then
1989 Append_To (Args,
1990 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
1991 else
1992 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
1993 end if;
1995 -- Add _Chain (not done for sequential elaboration policy, see
1996 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
1998 if Partition_Elaboration_Policy /= 'S' then
1999 Append_To (Args, Make_Identifier (Loc, Name_uChain));
2000 end if;
2002 Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
2003 First_Discr_Param := Next (Next (Next (First_Discr_Param)));
2004 end if;
2006 -- Append discriminant values
2008 if Has_Discriminants (Uparent_Type) then
2009 pragma Assert (not Is_Tagged_Type (Uparent_Type));
2011 Parent_Discr := First_Discriminant (Uparent_Type);
2012 while Present (Parent_Discr) loop
2014 -- Get the initial value for this discriminant
2015 -- ??? needs to be cleaned up to use parent_Discr_Constr
2016 -- directly.
2018 declare
2019 Discr : Entity_Id :=
2020 First_Stored_Discriminant (Uparent_Type);
2022 Discr_Value : Elmt_Id :=
2023 First_Elmt (Stored_Constraint (Rec_Type));
2025 begin
2026 while Original_Record_Component (Parent_Discr) /= Discr loop
2027 Next_Stored_Discriminant (Discr);
2028 Next_Elmt (Discr_Value);
2029 end loop;
2031 Arg := Node (Discr_Value);
2032 end;
2034 -- Append it to the list
2036 if Nkind (Arg) = N_Identifier
2037 and then Ekind (Entity (Arg)) = E_Discriminant
2038 then
2039 Append_To (Args,
2040 New_Occurrence_Of (Discriminal (Entity (Arg)), Loc));
2042 -- Case of access discriminants. We replace the reference
2043 -- to the type by a reference to the actual object.
2045 -- Is above comment right??? Use of New_Copy below seems mighty
2046 -- suspicious ???
2048 else
2049 Append_To (Args, New_Copy (Arg));
2050 end if;
2052 Next_Discriminant (Parent_Discr);
2053 end loop;
2054 end if;
2056 Res :=
2057 New_List (
2058 Make_Procedure_Call_Statement (Loc,
2059 Name =>
2060 New_Occurrence_Of (Parent_Proc, Loc),
2061 Parameter_Associations => Args));
2063 return Res;
2064 end Build_Init_Call_Thru;
2066 -----------------------------------
2067 -- Build_Offset_To_Top_Functions --
2068 -----------------------------------
2070 procedure Build_Offset_To_Top_Functions is
2072 procedure Build_Offset_To_Top_Function (Iface_Comp : Entity_Id);
2073 -- Generate:
2074 -- function Fxx (O : Address) return Storage_Offset is
2075 -- type Acc is access all <Typ>;
2076 -- begin
2077 -- return Acc!(O).Iface_Comp'Position;
2078 -- end Fxx;
2080 ----------------------------------
2081 -- Build_Offset_To_Top_Function --
2082 ----------------------------------
2084 procedure Build_Offset_To_Top_Function (Iface_Comp : Entity_Id) is
2085 Body_Node : Node_Id;
2086 Func_Id : Entity_Id;
2087 Spec_Node : Node_Id;
2088 Acc_Type : Entity_Id;
2090 begin
2091 Func_Id := Make_Temporary (Loc, 'F');
2092 Set_DT_Offset_To_Top_Func (Iface_Comp, Func_Id);
2094 -- Generate
2095 -- function Fxx (O : in Rec_Typ) return Storage_Offset;
2097 Spec_Node := New_Node (N_Function_Specification, Loc);
2098 Set_Defining_Unit_Name (Spec_Node, Func_Id);
2099 Set_Parameter_Specifications (Spec_Node, New_List (
2100 Make_Parameter_Specification (Loc,
2101 Defining_Identifier =>
2102 Make_Defining_Identifier (Loc, Name_uO),
2103 In_Present => True,
2104 Parameter_Type =>
2105 New_Occurrence_Of (RTE (RE_Address), Loc))));
2106 Set_Result_Definition (Spec_Node,
2107 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
2109 -- Generate
2110 -- function Fxx (O : in Rec_Typ) return Storage_Offset is
2111 -- begin
2112 -- return O.Iface_Comp'Position;
2113 -- end Fxx;
2115 Body_Node := New_Node (N_Subprogram_Body, Loc);
2116 Set_Specification (Body_Node, Spec_Node);
2118 Acc_Type := Make_Temporary (Loc, 'T');
2119 Set_Declarations (Body_Node, New_List (
2120 Make_Full_Type_Declaration (Loc,
2121 Defining_Identifier => Acc_Type,
2122 Type_Definition =>
2123 Make_Access_To_Object_Definition (Loc,
2124 All_Present => True,
2125 Null_Exclusion_Present => False,
2126 Constant_Present => False,
2127 Subtype_Indication =>
2128 New_Occurrence_Of (Rec_Type, Loc)))));
2130 Set_Handled_Statement_Sequence (Body_Node,
2131 Make_Handled_Sequence_Of_Statements (Loc,
2132 Statements => New_List (
2133 Make_Simple_Return_Statement (Loc,
2134 Expression =>
2135 Make_Attribute_Reference (Loc,
2136 Prefix =>
2137 Make_Selected_Component (Loc,
2138 Prefix =>
2139 Unchecked_Convert_To (Acc_Type,
2140 Make_Identifier (Loc, Name_uO)),
2141 Selector_Name =>
2142 New_Occurrence_Of (Iface_Comp, Loc)),
2143 Attribute_Name => Name_Position)))));
2145 Set_Ekind (Func_Id, E_Function);
2146 Set_Mechanism (Func_Id, Default_Mechanism);
2147 Set_Is_Internal (Func_Id, True);
2149 if not Debug_Generated_Code then
2150 Set_Debug_Info_Off (Func_Id);
2151 end if;
2153 Analyze (Body_Node);
2155 Append_Freeze_Action (Rec_Type, Body_Node);
2156 end Build_Offset_To_Top_Function;
2158 -- Local variables
2160 Iface_Comp : Node_Id;
2161 Iface_Comp_Elmt : Elmt_Id;
2162 Ifaces_Comp_List : Elist_Id;
2164 -- Start of processing for Build_Offset_To_Top_Functions
2166 begin
2167 -- Offset_To_Top_Functions are built only for derivations of types
2168 -- with discriminants that cover interface types.
2169 -- Nothing is needed either in case of virtual targets, since
2170 -- interfaces are handled directly by the target.
2172 if not Is_Tagged_Type (Rec_Type)
2173 or else Etype (Rec_Type) = Rec_Type
2174 or else not Has_Discriminants (Etype (Rec_Type))
2175 or else not Tagged_Type_Expansion
2176 then
2177 return;
2178 end if;
2180 Collect_Interface_Components (Rec_Type, Ifaces_Comp_List);
2182 -- For each interface type with secondary dispatch table we generate
2183 -- the Offset_To_Top_Functions (required to displace the pointer in
2184 -- interface conversions)
2186 Iface_Comp_Elmt := First_Elmt (Ifaces_Comp_List);
2187 while Present (Iface_Comp_Elmt) loop
2188 Iface_Comp := Node (Iface_Comp_Elmt);
2189 pragma Assert (Is_Interface (Related_Type (Iface_Comp)));
2191 -- If the interface is a parent of Rec_Type it shares the primary
2192 -- dispatch table and hence there is no need to build the function
2194 if not Is_Ancestor (Related_Type (Iface_Comp), Rec_Type,
2195 Use_Full_View => True)
2196 then
2197 Build_Offset_To_Top_Function (Iface_Comp);
2198 end if;
2200 Next_Elmt (Iface_Comp_Elmt);
2201 end loop;
2202 end Build_Offset_To_Top_Functions;
2204 ------------------------------
2205 -- Build_CPP_Init_Procedure --
2206 ------------------------------
2208 procedure Build_CPP_Init_Procedure is
2209 Body_Node : Node_Id;
2210 Body_Stmts : List_Id;
2211 Flag_Id : Entity_Id;
2212 Handled_Stmt_Node : Node_Id;
2213 Init_Tags_List : List_Id;
2214 Proc_Id : Entity_Id;
2215 Proc_Spec_Node : Node_Id;
2217 begin
2218 -- Check cases requiring no IC routine
2220 if not Is_CPP_Class (Root_Type (Rec_Type))
2221 or else Is_CPP_Class (Rec_Type)
2222 or else CPP_Num_Prims (Rec_Type) = 0
2223 or else not Tagged_Type_Expansion
2224 or else No_Run_Time_Mode
2225 then
2226 return;
2227 end if;
2229 -- Generate:
2231 -- Flag : Boolean := False;
2233 -- procedure Typ_IC is
2234 -- begin
2235 -- if not Flag then
2236 -- Copy C++ dispatch table slots from parent
2237 -- Update C++ slots of overridden primitives
2238 -- end if;
2239 -- end;
2241 Flag_Id := Make_Temporary (Loc, 'F');
2243 Append_Freeze_Action (Rec_Type,
2244 Make_Object_Declaration (Loc,
2245 Defining_Identifier => Flag_Id,
2246 Object_Definition =>
2247 New_Occurrence_Of (Standard_Boolean, Loc),
2248 Expression =>
2249 New_Occurrence_Of (Standard_True, Loc)));
2251 Body_Stmts := New_List;
2252 Body_Node := New_Node (N_Subprogram_Body, Loc);
2254 Proc_Spec_Node := New_Node (N_Procedure_Specification, Loc);
2256 Proc_Id :=
2257 Make_Defining_Identifier (Loc,
2258 Chars => Make_TSS_Name (Rec_Type, TSS_CPP_Init_Proc));
2260 Set_Ekind (Proc_Id, E_Procedure);
2261 Set_Is_Internal (Proc_Id);
2263 Set_Defining_Unit_Name (Proc_Spec_Node, Proc_Id);
2265 Set_Parameter_Specifications (Proc_Spec_Node, New_List);
2266 Set_Specification (Body_Node, Proc_Spec_Node);
2267 Set_Declarations (Body_Node, New_List);
2269 Init_Tags_List := Build_Inherit_CPP_Prims (Rec_Type);
2271 Append_To (Init_Tags_List,
2272 Make_Assignment_Statement (Loc,
2273 Name =>
2274 New_Occurrence_Of (Flag_Id, Loc),
2275 Expression =>
2276 New_Occurrence_Of (Standard_False, Loc)));
2278 Append_To (Body_Stmts,
2279 Make_If_Statement (Loc,
2280 Condition => New_Occurrence_Of (Flag_Id, Loc),
2281 Then_Statements => Init_Tags_List));
2283 Handled_Stmt_Node :=
2284 New_Node (N_Handled_Sequence_Of_Statements, Loc);
2285 Set_Statements (Handled_Stmt_Node, Body_Stmts);
2286 Set_Exception_Handlers (Handled_Stmt_Node, No_List);
2287 Set_Handled_Statement_Sequence (Body_Node, Handled_Stmt_Node);
2289 if not Debug_Generated_Code then
2290 Set_Debug_Info_Off (Proc_Id);
2291 end if;
2293 -- Associate CPP_Init_Proc with type
2295 Set_Init_Proc (Rec_Type, Proc_Id);
2296 end Build_CPP_Init_Procedure;
2298 --------------------------
2299 -- Build_Init_Procedure --
2300 --------------------------
2302 procedure Build_Init_Procedure is
2303 Body_Stmts : List_Id;
2304 Body_Node : Node_Id;
2305 Handled_Stmt_Node : Node_Id;
2306 Init_Tags_List : List_Id;
2307 Parameters : List_Id;
2308 Proc_Spec_Node : Node_Id;
2309 Record_Extension_Node : Node_Id;
2311 begin
2312 Body_Stmts := New_List;
2313 Body_Node := New_Node (N_Subprogram_Body, Loc);
2314 Set_Ekind (Proc_Id, E_Procedure);
2316 Proc_Spec_Node := New_Node (N_Procedure_Specification, Loc);
2317 Set_Defining_Unit_Name (Proc_Spec_Node, Proc_Id);
2319 Parameters := Init_Formals (Rec_Type);
2320 Append_List_To (Parameters,
2321 Build_Discriminant_Formals (Rec_Type, True));
2323 -- For tagged types, we add a flag to indicate whether the routine
2324 -- is called to initialize a parent component in the init_proc of
2325 -- a type extension. If the flag is false, we do not set the tag
2326 -- because it has been set already in the extension.
2328 if Is_Tagged_Type (Rec_Type) then
2329 Set_Tag := Make_Temporary (Loc, 'P');
2331 Append_To (Parameters,
2332 Make_Parameter_Specification (Loc,
2333 Defining_Identifier => Set_Tag,
2334 Parameter_Type =>
2335 New_Occurrence_Of (Standard_Boolean, Loc),
2336 Expression =>
2337 New_Occurrence_Of (Standard_True, Loc)));
2338 end if;
2340 Set_Parameter_Specifications (Proc_Spec_Node, Parameters);
2341 Set_Specification (Body_Node, Proc_Spec_Node);
2342 Set_Declarations (Body_Node, Decls);
2344 -- N is a Derived_Type_Definition that renames the parameters of the
2345 -- ancestor type. We initialize it by expanding our discriminants and
2346 -- call the ancestor _init_proc with a type-converted object.
2348 if Parent_Subtype_Renaming_Discrims then
2349 Append_List_To (Body_Stmts, Build_Init_Call_Thru (Parameters));
2351 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
2352 Build_Discriminant_Assignments (Body_Stmts);
2354 if not Null_Present (Type_Definition (N)) then
2355 Append_List_To (Body_Stmts,
2356 Build_Init_Statements (Component_List (Type_Definition (N))));
2357 end if;
2359 -- N is a Derived_Type_Definition with a possible non-empty
2360 -- extension. The initialization of a type extension consists in the
2361 -- initialization of the components in the extension.
2363 else
2364 Build_Discriminant_Assignments (Body_Stmts);
2366 Record_Extension_Node :=
2367 Record_Extension_Part (Type_Definition (N));
2369 if not Null_Present (Record_Extension_Node) then
2370 declare
2371 Stmts : constant List_Id :=
2372 Build_Init_Statements (
2373 Component_List (Record_Extension_Node));
2375 begin
2376 -- The parent field must be initialized first because the
2377 -- offset of the new discriminants may depend on it. This is
2378 -- not needed if the parent is an interface type because in
2379 -- such case the initialization of the _parent field was not
2380 -- generated.
2382 if not Is_Interface (Etype (Rec_Ent)) then
2383 declare
2384 Parent_IP : constant Name_Id :=
2385 Make_Init_Proc_Name (Etype (Rec_Ent));
2386 Stmt : Node_Id;
2387 IP_Call : Node_Id;
2388 IP_Stmts : List_Id;
2390 begin
2391 -- Look for a call to the parent IP at the beginning
2392 -- of Stmts associated with the record extension
2394 Stmt := First (Stmts);
2395 IP_Call := Empty;
2396 while Present (Stmt) loop
2397 if Nkind (Stmt) = N_Procedure_Call_Statement
2398 and then Chars (Name (Stmt)) = Parent_IP
2399 then
2400 IP_Call := Stmt;
2401 exit;
2402 end if;
2404 Next (Stmt);
2405 end loop;
2407 -- If found then move it to the beginning of the
2408 -- statements of this IP routine
2410 if Present (IP_Call) then
2411 IP_Stmts := New_List;
2412 loop
2413 Stmt := Remove_Head (Stmts);
2414 Append_To (IP_Stmts, Stmt);
2415 exit when Stmt = IP_Call;
2416 end loop;
2418 Prepend_List_To (Body_Stmts, IP_Stmts);
2419 end if;
2420 end;
2421 end if;
2423 Append_List_To (Body_Stmts, Stmts);
2424 end;
2425 end if;
2426 end if;
2428 -- Add here the assignment to instantiate the Tag
2430 -- The assignment corresponds to the code:
2432 -- _Init._Tag := Typ'Tag;
2434 -- Suppress the tag assignment when not Tagged_Type_Expansion because
2435 -- tags are represented implicitly in objects. It is also suppressed
2436 -- in case of CPP_Class types because in this case the tag is
2437 -- initialized in the C++ side.
2439 if Is_Tagged_Type (Rec_Type)
2440 and then Tagged_Type_Expansion
2441 and then not No_Run_Time_Mode
2442 then
2443 -- Case 1: Ada tagged types with no CPP ancestor. Set the tags of
2444 -- the actual object and invoke the IP of the parent (in this
2445 -- order). The tag must be initialized before the call to the IP
2446 -- of the parent and the assignments to other components because
2447 -- the initial value of the components may depend on the tag (eg.
2448 -- through a dispatching operation on an access to the current
2449 -- type). The tag assignment is not done when initializing the
2450 -- parent component of a type extension, because in that case the
2451 -- tag is set in the extension.
2453 if not Is_CPP_Class (Root_Type (Rec_Type)) then
2455 -- Initialize the primary tag component
2457 Init_Tags_List := New_List (
2458 Make_Assignment_Statement (Loc,
2459 Name =>
2460 Make_Selected_Component (Loc,
2461 Prefix => Make_Identifier (Loc, Name_uInit),
2462 Selector_Name =>
2463 New_Occurrence_Of
2464 (First_Tag_Component (Rec_Type), Loc)),
2465 Expression =>
2466 New_Occurrence_Of
2467 (Node
2468 (First_Elmt (Access_Disp_Table (Rec_Type))), Loc)));
2470 -- Ada 2005 (AI-251): Initialize the secondary tags components
2471 -- located at fixed positions (tags whose position depends on
2472 -- variable size components are initialized later ---see below)
2474 if Ada_Version >= Ada_2005
2475 and then not Is_Interface (Rec_Type)
2476 and then Has_Interfaces (Rec_Type)
2477 then
2478 Init_Secondary_Tags
2479 (Typ => Rec_Type,
2480 Target => Make_Identifier (Loc, Name_uInit),
2481 Stmts_List => Init_Tags_List,
2482 Fixed_Comps => True,
2483 Variable_Comps => False);
2484 end if;
2486 Prepend_To (Body_Stmts,
2487 Make_If_Statement (Loc,
2488 Condition => New_Occurrence_Of (Set_Tag, Loc),
2489 Then_Statements => Init_Tags_List));
2491 -- Case 2: CPP type. The imported C++ constructor takes care of
2492 -- tags initialization. No action needed here because the IP
2493 -- is built by Set_CPP_Constructors; in this case the IP is a
2494 -- wrapper that invokes the C++ constructor and copies the C++
2495 -- tags locally. Done to inherit the C++ slots in Ada derivations
2496 -- (see case 3).
2498 elsif Is_CPP_Class (Rec_Type) then
2499 pragma Assert (False);
2500 null;
2502 -- Case 3: Combined hierarchy containing C++ types and Ada tagged
2503 -- type derivations. Derivations of imported C++ classes add a
2504 -- complication, because we cannot inhibit tag setting in the
2505 -- constructor for the parent. Hence we initialize the tag after
2506 -- the call to the parent IP (that is, in reverse order compared
2507 -- with pure Ada hierarchies ---see comment on case 1).
2509 else
2510 -- Initialize the primary tag
2512 Init_Tags_List := New_List (
2513 Make_Assignment_Statement (Loc,
2514 Name =>
2515 Make_Selected_Component (Loc,
2516 Prefix => Make_Identifier (Loc, Name_uInit),
2517 Selector_Name =>
2518 New_Occurrence_Of
2519 (First_Tag_Component (Rec_Type), Loc)),
2520 Expression =>
2521 New_Occurrence_Of
2522 (Node
2523 (First_Elmt (Access_Disp_Table (Rec_Type))), Loc)));
2525 -- Ada 2005 (AI-251): Initialize the secondary tags components
2526 -- located at fixed positions (tags whose position depends on
2527 -- variable size components are initialized later ---see below)
2529 if Ada_Version >= Ada_2005
2530 and then not Is_Interface (Rec_Type)
2531 and then Has_Interfaces (Rec_Type)
2532 then
2533 Init_Secondary_Tags
2534 (Typ => Rec_Type,
2535 Target => Make_Identifier (Loc, Name_uInit),
2536 Stmts_List => Init_Tags_List,
2537 Fixed_Comps => True,
2538 Variable_Comps => False);
2539 end if;
2541 -- Initialize the tag component after invocation of parent IP.
2543 -- Generate:
2544 -- parent_IP(_init.parent); // Invokes the C++ constructor
2545 -- [ typIC; ] // Inherit C++ slots from parent
2546 -- init_tags
2548 declare
2549 Ins_Nod : Node_Id;
2551 begin
2552 -- Search for the call to the IP of the parent. We assume
2553 -- that the first init_proc call is for the parent.
2555 Ins_Nod := First (Body_Stmts);
2556 while Present (Next (Ins_Nod))
2557 and then (Nkind (Ins_Nod) /= N_Procedure_Call_Statement
2558 or else not Is_Init_Proc (Name (Ins_Nod)))
2559 loop
2560 Next (Ins_Nod);
2561 end loop;
2563 -- The IC routine copies the inherited slots of the C+ part
2564 -- of the dispatch table from the parent and updates the
2565 -- overridden C++ slots.
2567 if CPP_Num_Prims (Rec_Type) > 0 then
2568 declare
2569 Init_DT : Entity_Id;
2570 New_Nod : Node_Id;
2572 begin
2573 Init_DT := CPP_Init_Proc (Rec_Type);
2574 pragma Assert (Present (Init_DT));
2576 New_Nod :=
2577 Make_Procedure_Call_Statement (Loc,
2578 New_Occurrence_Of (Init_DT, Loc));
2579 Insert_After (Ins_Nod, New_Nod);
2581 -- Update location of init tag statements
2583 Ins_Nod := New_Nod;
2584 end;
2585 end if;
2587 Insert_List_After (Ins_Nod, Init_Tags_List);
2588 end;
2589 end if;
2591 -- Ada 2005 (AI-251): Initialize the secondary tag components
2592 -- located at variable positions. We delay the generation of this
2593 -- code until here because the value of the attribute 'Position
2594 -- applied to variable size components of the parent type that
2595 -- depend on discriminants is only safely read at runtime after
2596 -- the parent components have been initialized.
2598 if Ada_Version >= Ada_2005
2599 and then not Is_Interface (Rec_Type)
2600 and then Has_Interfaces (Rec_Type)
2601 and then Has_Discriminants (Etype (Rec_Type))
2602 and then Is_Variable_Size_Record (Etype (Rec_Type))
2603 then
2604 Init_Tags_List := New_List;
2606 Init_Secondary_Tags
2607 (Typ => Rec_Type,
2608 Target => Make_Identifier (Loc, Name_uInit),
2609 Stmts_List => Init_Tags_List,
2610 Fixed_Comps => False,
2611 Variable_Comps => True);
2613 if Is_Non_Empty_List (Init_Tags_List) then
2614 Append_List_To (Body_Stmts, Init_Tags_List);
2615 end if;
2616 end if;
2617 end if;
2619 Handled_Stmt_Node := New_Node (N_Handled_Sequence_Of_Statements, Loc);
2620 Set_Statements (Handled_Stmt_Node, Body_Stmts);
2622 -- Generate:
2623 -- Deep_Finalize (_init, C1, ..., CN);
2624 -- raise;
2626 if Counter > 0
2627 and then Needs_Finalization (Rec_Type)
2628 and then not Is_Abstract_Type (Rec_Type)
2629 and then not Restriction_Active (No_Exception_Propagation)
2630 then
2631 declare
2632 DF_Call : Node_Id;
2633 DF_Id : Entity_Id;
2635 begin
2636 -- Create a local version of Deep_Finalize which has indication
2637 -- of partial initialization state.
2639 DF_Id := Make_Temporary (Loc, 'F');
2641 Append_To (Decls, Make_Local_Deep_Finalize (Rec_Type, DF_Id));
2643 DF_Call :=
2644 Make_Procedure_Call_Statement (Loc,
2645 Name => New_Occurrence_Of (DF_Id, Loc),
2646 Parameter_Associations => New_List (
2647 Make_Identifier (Loc, Name_uInit),
2648 New_Occurrence_Of (Standard_False, Loc)));
2650 -- Do not emit warnings related to the elaboration order when a
2651 -- controlled object is declared before the body of Finalize is
2652 -- seen.
2654 Set_No_Elaboration_Check (DF_Call);
2656 Set_Exception_Handlers (Handled_Stmt_Node, New_List (
2657 Make_Exception_Handler (Loc,
2658 Exception_Choices => New_List (
2659 Make_Others_Choice (Loc)),
2660 Statements => New_List (
2661 DF_Call,
2662 Make_Raise_Statement (Loc)))));
2663 end;
2664 else
2665 Set_Exception_Handlers (Handled_Stmt_Node, No_List);
2666 end if;
2668 Set_Handled_Statement_Sequence (Body_Node, Handled_Stmt_Node);
2670 if not Debug_Generated_Code then
2671 Set_Debug_Info_Off (Proc_Id);
2672 end if;
2674 -- Associate Init_Proc with type, and determine if the procedure
2675 -- is null (happens because of the Initialize_Scalars pragma case,
2676 -- where we have to generate a null procedure in case it is called
2677 -- by a client with Initialize_Scalars set). Such procedures have
2678 -- to be generated, but do not have to be called, so we mark them
2679 -- as null to suppress the call.
2681 Set_Init_Proc (Rec_Type, Proc_Id);
2683 if List_Length (Body_Stmts) = 1
2685 -- We must skip SCIL nodes because they may have been added to this
2686 -- list by Insert_Actions.
2688 and then Nkind (First_Non_SCIL_Node (Body_Stmts)) = N_Null_Statement
2689 then
2690 Set_Is_Null_Init_Proc (Proc_Id);
2691 end if;
2692 end Build_Init_Procedure;
2694 ---------------------------
2695 -- Build_Init_Statements --
2696 ---------------------------
2698 function Build_Init_Statements (Comp_List : Node_Id) return List_Id is
2699 Checks : constant List_Id := New_List;
2700 Actions : List_Id := No_List;
2701 Counter_Id : Entity_Id := Empty;
2702 Comp_Loc : Source_Ptr;
2703 Decl : Node_Id;
2704 Has_POC : Boolean;
2705 Id : Entity_Id;
2706 Parent_Stmts : List_Id;
2707 Stmts : List_Id;
2708 Typ : Entity_Id;
2710 procedure Increment_Counter (Loc : Source_Ptr);
2711 -- Generate an "increment by one" statement for the current counter
2712 -- and append it to the list Stmts.
2714 procedure Make_Counter (Loc : Source_Ptr);
2715 -- Create a new counter for the current component list. The routine
2716 -- creates a new defining Id, adds an object declaration and sets
2717 -- the Id generator for the next variant.
2719 -----------------------
2720 -- Increment_Counter --
2721 -----------------------
2723 procedure Increment_Counter (Loc : Source_Ptr) is
2724 begin
2725 -- Generate:
2726 -- Counter := Counter + 1;
2728 Append_To (Stmts,
2729 Make_Assignment_Statement (Loc,
2730 Name => New_Occurrence_Of (Counter_Id, Loc),
2731 Expression =>
2732 Make_Op_Add (Loc,
2733 Left_Opnd => New_Occurrence_Of (Counter_Id, Loc),
2734 Right_Opnd => Make_Integer_Literal (Loc, 1))));
2735 end Increment_Counter;
2737 ------------------
2738 -- Make_Counter --
2739 ------------------
2741 procedure Make_Counter (Loc : Source_Ptr) is
2742 begin
2743 -- Increment the Id generator
2745 Counter := Counter + 1;
2747 -- Create the entity and declaration
2749 Counter_Id :=
2750 Make_Defining_Identifier (Loc,
2751 Chars => New_External_Name ('C', Counter));
2753 -- Generate:
2754 -- Cnn : Integer := 0;
2756 Append_To (Decls,
2757 Make_Object_Declaration (Loc,
2758 Defining_Identifier => Counter_Id,
2759 Object_Definition =>
2760 New_Occurrence_Of (Standard_Integer, Loc),
2761 Expression =>
2762 Make_Integer_Literal (Loc, 0)));
2763 end Make_Counter;
2765 -- Start of processing for Build_Init_Statements
2767 begin
2768 if Null_Present (Comp_List) then
2769 return New_List (Make_Null_Statement (Loc));
2770 end if;
2772 Parent_Stmts := New_List;
2773 Stmts := New_List;
2775 -- Loop through visible declarations of task types and protected
2776 -- types moving any expanded code from the spec to the body of the
2777 -- init procedure.
2779 if Is_Task_Record_Type (Rec_Type)
2780 or else Is_Protected_Record_Type (Rec_Type)
2781 then
2782 declare
2783 Decl : constant Node_Id :=
2784 Parent (Corresponding_Concurrent_Type (Rec_Type));
2785 Def : Node_Id;
2786 N1 : Node_Id;
2787 N2 : Node_Id;
2789 begin
2790 if Is_Task_Record_Type (Rec_Type) then
2791 Def := Task_Definition (Decl);
2792 else
2793 Def := Protected_Definition (Decl);
2794 end if;
2796 if Present (Def) then
2797 N1 := First (Visible_Declarations (Def));
2798 while Present (N1) loop
2799 N2 := N1;
2800 N1 := Next (N1);
2802 if Nkind (N2) in N_Statement_Other_Than_Procedure_Call
2803 or else Nkind (N2) in N_Raise_xxx_Error
2804 or else Nkind (N2) = N_Procedure_Call_Statement
2805 then
2806 Append_To (Stmts,
2807 New_Copy_Tree (N2, New_Scope => Proc_Id));
2808 Rewrite (N2, Make_Null_Statement (Sloc (N2)));
2809 Analyze (N2);
2810 end if;
2811 end loop;
2812 end if;
2813 end;
2814 end if;
2816 -- Loop through components, skipping pragmas, in 2 steps. The first
2817 -- step deals with regular components. The second step deals with
2818 -- components that have per object constraints and no explicit
2819 -- initialization.
2821 Has_POC := False;
2823 -- First pass : regular components
2825 Decl := First_Non_Pragma (Component_Items (Comp_List));
2826 while Present (Decl) loop
2827 Comp_Loc := Sloc (Decl);
2828 Build_Record_Checks
2829 (Subtype_Indication (Component_Definition (Decl)), Checks);
2831 Id := Defining_Identifier (Decl);
2832 Typ := Etype (Id);
2834 -- Leave any processing of per-object constrained component for
2835 -- the second pass.
2837 if Has_Access_Constraint (Id) and then No (Expression (Decl)) then
2838 Has_POC := True;
2840 -- Regular component cases
2842 else
2843 -- In the context of the init proc, references to discriminants
2844 -- resolve to denote the discriminals: this is where we can
2845 -- freeze discriminant dependent component subtypes.
2847 if not Is_Frozen (Typ) then
2848 Append_List_To (Stmts, Freeze_Entity (Typ, N));
2849 end if;
2851 -- Explicit initialization
2853 if Present (Expression (Decl)) then
2854 if Is_CPP_Constructor_Call (Expression (Decl)) then
2855 Actions :=
2856 Build_Initialization_Call
2857 (Comp_Loc,
2858 Id_Ref =>
2859 Make_Selected_Component (Comp_Loc,
2860 Prefix =>
2861 Make_Identifier (Comp_Loc, Name_uInit),
2862 Selector_Name =>
2863 New_Occurrence_Of (Id, Comp_Loc)),
2864 Typ => Typ,
2865 In_Init_Proc => True,
2866 Enclos_Type => Rec_Type,
2867 Discr_Map => Discr_Map,
2868 Constructor_Ref => Expression (Decl));
2869 else
2870 Actions := Build_Assignment (Id, Expression (Decl));
2871 end if;
2873 -- CPU, Dispatching_Domain, Priority, and Secondary_Stack_Size
2874 -- components are filled in with the corresponding rep-item
2875 -- expression of the concurrent type (if any).
2877 elsif Ekind (Scope (Id)) = E_Record_Type
2878 and then Present (Corresponding_Concurrent_Type (Scope (Id)))
2879 and then Nam_In (Chars (Id), Name_uCPU,
2880 Name_uDispatching_Domain,
2881 Name_uPriority,
2882 Name_uSecondary_Stack_Size)
2883 then
2884 declare
2885 Exp : Node_Id;
2886 Nam : Name_Id;
2887 pragma Warnings (Off, Nam);
2888 Ritem : Node_Id;
2890 begin
2891 if Chars (Id) = Name_uCPU then
2892 Nam := Name_CPU;
2894 elsif Chars (Id) = Name_uDispatching_Domain then
2895 Nam := Name_Dispatching_Domain;
2897 elsif Chars (Id) = Name_uPriority then
2898 Nam := Name_Priority;
2900 elsif Chars (Id) = Name_uSecondary_Stack_Size then
2901 Nam := Name_Secondary_Stack_Size;
2902 end if;
2904 -- Get the Rep Item (aspect specification, attribute
2905 -- definition clause or pragma) of the corresponding
2906 -- concurrent type.
2908 Ritem :=
2909 Get_Rep_Item
2910 (Corresponding_Concurrent_Type (Scope (Id)),
2911 Nam,
2912 Check_Parents => False);
2914 if Present (Ritem) then
2916 -- Pragma case
2918 if Nkind (Ritem) = N_Pragma then
2919 Exp := First (Pragma_Argument_Associations (Ritem));
2921 if Nkind (Exp) = N_Pragma_Argument_Association then
2922 Exp := Expression (Exp);
2923 end if;
2925 -- Conversion for Priority expression
2927 if Nam = Name_Priority then
2928 if Pragma_Name (Ritem) = Name_Priority
2929 and then not GNAT_Mode
2930 then
2931 Exp := Convert_To (RTE (RE_Priority), Exp);
2932 else
2933 Exp :=
2934 Convert_To (RTE (RE_Any_Priority), Exp);
2935 end if;
2936 end if;
2938 -- Aspect/Attribute definition clause case
2940 else
2941 Exp := Expression (Ritem);
2943 -- Conversion for Priority expression
2945 if Nam = Name_Priority then
2946 if Chars (Ritem) = Name_Priority
2947 and then not GNAT_Mode
2948 then
2949 Exp := Convert_To (RTE (RE_Priority), Exp);
2950 else
2951 Exp :=
2952 Convert_To (RTE (RE_Any_Priority), Exp);
2953 end if;
2954 end if;
2955 end if;
2957 -- Conversion for Dispatching_Domain value
2959 if Nam = Name_Dispatching_Domain then
2960 Exp :=
2961 Unchecked_Convert_To
2962 (RTE (RE_Dispatching_Domain_Access), Exp);
2964 -- Conversion for Secondary_Stack_Size value
2966 elsif Nam = Name_Secondary_Stack_Size then
2967 Exp := Convert_To (RTE (RE_Size_Type), Exp);
2968 end if;
2970 Actions := Build_Assignment (Id, Exp);
2972 -- Nothing needed if no Rep Item
2974 else
2975 Actions := No_List;
2976 end if;
2977 end;
2979 -- Composite component with its own Init_Proc
2981 elsif not Is_Interface (Typ)
2982 and then Has_Non_Null_Base_Init_Proc (Typ)
2983 then
2984 Actions :=
2985 Build_Initialization_Call
2986 (Comp_Loc,
2987 Make_Selected_Component (Comp_Loc,
2988 Prefix =>
2989 Make_Identifier (Comp_Loc, Name_uInit),
2990 Selector_Name => New_Occurrence_Of (Id, Comp_Loc)),
2991 Typ,
2992 In_Init_Proc => True,
2993 Enclos_Type => Rec_Type,
2994 Discr_Map => Discr_Map);
2996 Clean_Task_Names (Typ, Proc_Id);
2998 -- Simple initialization
3000 elsif Component_Needs_Simple_Initialization (Typ) then
3001 Actions :=
3002 Build_Assignment
3003 (Id, Get_Simple_Init_Val (Typ, N, Esize (Id)));
3005 -- Nothing needed for this case
3007 else
3008 Actions := No_List;
3009 end if;
3011 if Present (Checks) then
3012 if Chars (Id) = Name_uParent then
3013 Append_List_To (Parent_Stmts, Checks);
3014 else
3015 Append_List_To (Stmts, Checks);
3016 end if;
3017 end if;
3019 if Present (Actions) then
3020 if Chars (Id) = Name_uParent then
3021 Append_List_To (Parent_Stmts, Actions);
3023 else
3024 Append_List_To (Stmts, Actions);
3026 -- Preserve initialization state in the current counter
3028 if Needs_Finalization (Typ) then
3029 if No (Counter_Id) then
3030 Make_Counter (Comp_Loc);
3031 end if;
3033 Increment_Counter (Comp_Loc);
3034 end if;
3035 end if;
3036 end if;
3037 end if;
3039 Next_Non_Pragma (Decl);
3040 end loop;
3042 -- The parent field must be initialized first because variable
3043 -- size components of the parent affect the location of all the
3044 -- new components.
3046 Prepend_List_To (Stmts, Parent_Stmts);
3048 -- Set up tasks and protected object support. This needs to be done
3049 -- before any component with a per-object access discriminant
3050 -- constraint, or any variant part (which may contain such
3051 -- components) is initialized, because the initialization of these
3052 -- components may reference the enclosing concurrent object.
3054 -- For a task record type, add the task create call and calls to bind
3055 -- any interrupt (signal) entries.
3057 if Is_Task_Record_Type (Rec_Type) then
3059 -- In the case of the restricted run time the ATCB has already
3060 -- been preallocated.
3062 if Restricted_Profile then
3063 Append_To (Stmts,
3064 Make_Assignment_Statement (Loc,
3065 Name =>
3066 Make_Selected_Component (Loc,
3067 Prefix => Make_Identifier (Loc, Name_uInit),
3068 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)),
3069 Expression =>
3070 Make_Attribute_Reference (Loc,
3071 Prefix =>
3072 Make_Selected_Component (Loc,
3073 Prefix => Make_Identifier (Loc, Name_uInit),
3074 Selector_Name => Make_Identifier (Loc, Name_uATCB)),
3075 Attribute_Name => Name_Unchecked_Access)));
3076 end if;
3078 Append_To (Stmts, Make_Task_Create_Call (Rec_Type));
3080 declare
3081 Task_Type : constant Entity_Id :=
3082 Corresponding_Concurrent_Type (Rec_Type);
3083 Task_Decl : constant Node_Id := Parent (Task_Type);
3084 Task_Def : constant Node_Id := Task_Definition (Task_Decl);
3085 Decl_Loc : Source_Ptr;
3086 Ent : Entity_Id;
3087 Vis_Decl : Node_Id;
3089 begin
3090 if Present (Task_Def) then
3091 Vis_Decl := First (Visible_Declarations (Task_Def));
3092 while Present (Vis_Decl) loop
3093 Decl_Loc := Sloc (Vis_Decl);
3095 if Nkind (Vis_Decl) = N_Attribute_Definition_Clause then
3096 if Get_Attribute_Id (Chars (Vis_Decl)) =
3097 Attribute_Address
3098 then
3099 Ent := Entity (Name (Vis_Decl));
3101 if Ekind (Ent) = E_Entry then
3102 Append_To (Stmts,
3103 Make_Procedure_Call_Statement (Decl_Loc,
3104 Name =>
3105 New_Occurrence_Of (RTE (
3106 RE_Bind_Interrupt_To_Entry), Decl_Loc),
3107 Parameter_Associations => New_List (
3108 Make_Selected_Component (Decl_Loc,
3109 Prefix =>
3110 Make_Identifier (Decl_Loc, Name_uInit),
3111 Selector_Name =>
3112 Make_Identifier
3113 (Decl_Loc, Name_uTask_Id)),
3114 Entry_Index_Expression
3115 (Decl_Loc, Ent, Empty, Task_Type),
3116 Expression (Vis_Decl))));
3117 end if;
3118 end if;
3119 end if;
3121 Next (Vis_Decl);
3122 end loop;
3123 end if;
3124 end;
3125 end if;
3127 -- For a protected type, add statements generated by
3128 -- Make_Initialize_Protection.
3130 if Is_Protected_Record_Type (Rec_Type) then
3131 Append_List_To (Stmts,
3132 Make_Initialize_Protection (Rec_Type));
3133 end if;
3135 -- Second pass: components with per-object constraints
3137 if Has_POC then
3138 Decl := First_Non_Pragma (Component_Items (Comp_List));
3139 while Present (Decl) loop
3140 Comp_Loc := Sloc (Decl);
3141 Id := Defining_Identifier (Decl);
3142 Typ := Etype (Id);
3144 if Has_Access_Constraint (Id)
3145 and then No (Expression (Decl))
3146 then
3147 if Has_Non_Null_Base_Init_Proc (Typ) then
3148 Append_List_To (Stmts,
3149 Build_Initialization_Call (Comp_Loc,
3150 Make_Selected_Component (Comp_Loc,
3151 Prefix =>
3152 Make_Identifier (Comp_Loc, Name_uInit),
3153 Selector_Name => New_Occurrence_Of (Id, Comp_Loc)),
3154 Typ,
3155 In_Init_Proc => True,
3156 Enclos_Type => Rec_Type,
3157 Discr_Map => Discr_Map));
3159 Clean_Task_Names (Typ, Proc_Id);
3161 -- Preserve initialization state in the current counter
3163 if Needs_Finalization (Typ) then
3164 if No (Counter_Id) then
3165 Make_Counter (Comp_Loc);
3166 end if;
3168 Increment_Counter (Comp_Loc);
3169 end if;
3171 elsif Component_Needs_Simple_Initialization (Typ) then
3172 Append_List_To (Stmts,
3173 Build_Assignment
3174 (Id, Get_Simple_Init_Val (Typ, N, Esize (Id))));
3175 end if;
3176 end if;
3178 Next_Non_Pragma (Decl);
3179 end loop;
3180 end if;
3182 -- Process the variant part
3184 if Present (Variant_Part (Comp_List)) then
3185 declare
3186 Variant_Alts : constant List_Id := New_List;
3187 Var_Loc : Source_Ptr := No_Location;
3188 Variant : Node_Id;
3190 begin
3191 Variant :=
3192 First_Non_Pragma (Variants (Variant_Part (Comp_List)));
3193 while Present (Variant) loop
3194 Var_Loc := Sloc (Variant);
3195 Append_To (Variant_Alts,
3196 Make_Case_Statement_Alternative (Var_Loc,
3197 Discrete_Choices =>
3198 New_Copy_List (Discrete_Choices (Variant)),
3199 Statements =>
3200 Build_Init_Statements (Component_List (Variant))));
3201 Next_Non_Pragma (Variant);
3202 end loop;
3204 -- The expression of the case statement which is a reference
3205 -- to one of the discriminants is replaced by the appropriate
3206 -- formal parameter of the initialization procedure.
3208 Append_To (Stmts,
3209 Make_Case_Statement (Var_Loc,
3210 Expression =>
3211 New_Occurrence_Of (Discriminal (
3212 Entity (Name (Variant_Part (Comp_List)))), Var_Loc),
3213 Alternatives => Variant_Alts));
3214 end;
3215 end if;
3217 -- If no initializations when generated for component declarations
3218 -- corresponding to this Stmts, append a null statement to Stmts to
3219 -- to make it a valid Ada tree.
3221 if Is_Empty_List (Stmts) then
3222 Append (Make_Null_Statement (Loc), Stmts);
3223 end if;
3225 return Stmts;
3227 exception
3228 when RE_Not_Available =>
3229 return Empty_List;
3230 end Build_Init_Statements;
3232 -------------------------
3233 -- Build_Record_Checks --
3234 -------------------------
3236 procedure Build_Record_Checks (S : Node_Id; Check_List : List_Id) is
3237 Subtype_Mark_Id : Entity_Id;
3239 procedure Constrain_Array
3240 (SI : Node_Id;
3241 Check_List : List_Id);
3242 -- Apply a list of index constraints to an unconstrained array type.
3243 -- The first parameter is the entity for the resulting subtype.
3244 -- Check_List is a list to which the check actions are appended.
3246 ---------------------
3247 -- Constrain_Array --
3248 ---------------------
3250 procedure Constrain_Array
3251 (SI : Node_Id;
3252 Check_List : List_Id)
3254 C : constant Node_Id := Constraint (SI);
3255 Number_Of_Constraints : Nat := 0;
3256 Index : Node_Id;
3257 S, T : Entity_Id;
3259 procedure Constrain_Index
3260 (Index : Node_Id;
3261 S : Node_Id;
3262 Check_List : List_Id);
3263 -- Process an index constraint in a constrained array declaration.
3264 -- The constraint can be either a subtype name or a range with or
3265 -- without an explicit subtype mark. Index is the corresponding
3266 -- index of the unconstrained array. S is the range expression.
3267 -- Check_List is a list to which the check actions are appended.
3269 ---------------------
3270 -- Constrain_Index --
3271 ---------------------
3273 procedure Constrain_Index
3274 (Index : Node_Id;
3275 S : Node_Id;
3276 Check_List : List_Id)
3278 T : constant Entity_Id := Etype (Index);
3280 begin
3281 if Nkind (S) = N_Range then
3282 Process_Range_Expr_In_Decl (S, T, Check_List => Check_List);
3283 end if;
3284 end Constrain_Index;
3286 -- Start of processing for Constrain_Array
3288 begin
3289 T := Entity (Subtype_Mark (SI));
3291 if Is_Access_Type (T) then
3292 T := Designated_Type (T);
3293 end if;
3295 S := First (Constraints (C));
3296 while Present (S) loop
3297 Number_Of_Constraints := Number_Of_Constraints + 1;
3298 Next (S);
3299 end loop;
3301 -- In either case, the index constraint must provide a discrete
3302 -- range for each index of the array type and the type of each
3303 -- discrete range must be the same as that of the corresponding
3304 -- index. (RM 3.6.1)
3306 S := First (Constraints (C));
3307 Index := First_Index (T);
3308 Analyze (Index);
3310 -- Apply constraints to each index type
3312 for J in 1 .. Number_Of_Constraints loop
3313 Constrain_Index (Index, S, Check_List);
3314 Next (Index);
3315 Next (S);
3316 end loop;
3317 end Constrain_Array;
3319 -- Start of processing for Build_Record_Checks
3321 begin
3322 if Nkind (S) = N_Subtype_Indication then
3323 Find_Type (Subtype_Mark (S));
3324 Subtype_Mark_Id := Entity (Subtype_Mark (S));
3326 -- Remaining processing depends on type
3328 case Ekind (Subtype_Mark_Id) is
3329 when Array_Kind =>
3330 Constrain_Array (S, Check_List);
3332 when others =>
3333 null;
3334 end case;
3335 end if;
3336 end Build_Record_Checks;
3338 -------------------------------------------
3339 -- Component_Needs_Simple_Initialization --
3340 -------------------------------------------
3342 function Component_Needs_Simple_Initialization
3343 (T : Entity_Id) return Boolean
3345 begin
3346 return
3347 Needs_Simple_Initialization (T)
3348 and then not Is_RTE (T, RE_Tag)
3350 -- Ada 2005 (AI-251): Check also the tag of abstract interfaces
3352 and then not Is_RTE (T, RE_Interface_Tag);
3353 end Component_Needs_Simple_Initialization;
3355 --------------------------------------
3356 -- Parent_Subtype_Renaming_Discrims --
3357 --------------------------------------
3359 function Parent_Subtype_Renaming_Discrims return Boolean is
3360 De : Entity_Id;
3361 Dp : Entity_Id;
3363 begin
3364 if Base_Type (Rec_Ent) /= Rec_Ent then
3365 return False;
3366 end if;
3368 if Etype (Rec_Ent) = Rec_Ent
3369 or else not Has_Discriminants (Rec_Ent)
3370 or else Is_Constrained (Rec_Ent)
3371 or else Is_Tagged_Type (Rec_Ent)
3372 then
3373 return False;
3374 end if;
3376 -- If there are no explicit stored discriminants we have inherited
3377 -- the root type discriminants so far, so no renamings occurred.
3379 if First_Discriminant (Rec_Ent) =
3380 First_Stored_Discriminant (Rec_Ent)
3381 then
3382 return False;
3383 end if;
3385 -- Check if we have done some trivial renaming of the parent
3386 -- discriminants, i.e. something like
3388 -- type DT (X1, X2: int) is new PT (X1, X2);
3390 De := First_Discriminant (Rec_Ent);
3391 Dp := First_Discriminant (Etype (Rec_Ent));
3392 while Present (De) loop
3393 pragma Assert (Present (Dp));
3395 if Corresponding_Discriminant (De) /= Dp then
3396 return True;
3397 end if;
3399 Next_Discriminant (De);
3400 Next_Discriminant (Dp);
3401 end loop;
3403 return Present (Dp);
3404 end Parent_Subtype_Renaming_Discrims;
3406 ------------------------
3407 -- Requires_Init_Proc --
3408 ------------------------
3410 function Requires_Init_Proc (Rec_Id : Entity_Id) return Boolean is
3411 Comp_Decl : Node_Id;
3412 Id : Entity_Id;
3413 Typ : Entity_Id;
3415 begin
3416 -- Definitely do not need one if specifically suppressed
3418 if Initialization_Suppressed (Rec_Id) then
3419 return False;
3420 end if;
3422 -- If it is a type derived from a type with unknown discriminants,
3423 -- we cannot build an initialization procedure for it.
3425 if Has_Unknown_Discriminants (Rec_Id)
3426 or else Has_Unknown_Discriminants (Etype (Rec_Id))
3427 then
3428 return False;
3429 end if;
3431 -- Otherwise we need to generate an initialization procedure if
3432 -- Is_CPP_Class is False and at least one of the following applies:
3434 -- 1. Discriminants are present, since they need to be initialized
3435 -- with the appropriate discriminant constraint expressions.
3436 -- However, the discriminant of an unchecked union does not
3437 -- count, since the discriminant is not present.
3439 -- 2. The type is a tagged type, since the implicit Tag component
3440 -- needs to be initialized with a pointer to the dispatch table.
3442 -- 3. The type contains tasks
3444 -- 4. One or more components has an initial value
3446 -- 5. One or more components is for a type which itself requires
3447 -- an initialization procedure.
3449 -- 6. One or more components is a type that requires simple
3450 -- initialization (see Needs_Simple_Initialization), except
3451 -- that types Tag and Interface_Tag are excluded, since fields
3452 -- of these types are initialized by other means.
3454 -- 7. The type is the record type built for a task type (since at
3455 -- the very least, Create_Task must be called)
3457 -- 8. The type is the record type built for a protected type (since
3458 -- at least Initialize_Protection must be called)
3460 -- 9. The type is marked as a public entity. The reason we add this
3461 -- case (even if none of the above apply) is to properly handle
3462 -- Initialize_Scalars. If a package is compiled without an IS
3463 -- pragma, and the client is compiled with an IS pragma, then
3464 -- the client will think an initialization procedure is present
3465 -- and call it, when in fact no such procedure is required, but
3466 -- since the call is generated, there had better be a routine
3467 -- at the other end of the call, even if it does nothing).
3469 -- Note: the reason we exclude the CPP_Class case is because in this
3470 -- case the initialization is performed by the C++ constructors, and
3471 -- the IP is built by Set_CPP_Constructors.
3473 if Is_CPP_Class (Rec_Id) then
3474 return False;
3476 elsif Is_Interface (Rec_Id) then
3477 return False;
3479 elsif (Has_Discriminants (Rec_Id)
3480 and then not Is_Unchecked_Union (Rec_Id))
3481 or else Is_Tagged_Type (Rec_Id)
3482 or else Is_Concurrent_Record_Type (Rec_Id)
3483 or else Has_Task (Rec_Id)
3484 then
3485 return True;
3486 end if;
3488 Id := First_Component (Rec_Id);
3489 while Present (Id) loop
3490 Comp_Decl := Parent (Id);
3491 Typ := Etype (Id);
3493 if Present (Expression (Comp_Decl))
3494 or else Has_Non_Null_Base_Init_Proc (Typ)
3495 or else Component_Needs_Simple_Initialization (Typ)
3496 then
3497 return True;
3498 end if;
3500 Next_Component (Id);
3501 end loop;
3503 -- As explained above, a record initialization procedure is needed
3504 -- for public types in case Initialize_Scalars applies to a client.
3505 -- However, such a procedure is not needed in the case where either
3506 -- of restrictions No_Initialize_Scalars or No_Default_Initialization
3507 -- applies. No_Initialize_Scalars excludes the possibility of using
3508 -- Initialize_Scalars in any partition, and No_Default_Initialization
3509 -- implies that no initialization should ever be done for objects of
3510 -- the type, so is incompatible with Initialize_Scalars.
3512 if not Restriction_Active (No_Initialize_Scalars)
3513 and then not Restriction_Active (No_Default_Initialization)
3514 and then Is_Public (Rec_Id)
3515 then
3516 return True;
3517 end if;
3519 return False;
3520 end Requires_Init_Proc;
3522 -- Start of processing for Build_Record_Init_Proc
3524 begin
3525 Rec_Type := Defining_Identifier (N);
3527 -- This may be full declaration of a private type, in which case
3528 -- the visible entity is a record, and the private entity has been
3529 -- exchanged with it in the private part of the current package.
3530 -- The initialization procedure is built for the record type, which
3531 -- is retrievable from the private entity.
3533 if Is_Incomplete_Or_Private_Type (Rec_Type) then
3534 Rec_Type := Underlying_Type (Rec_Type);
3535 end if;
3537 -- If we have a variant record with restriction No_Implicit_Conditionals
3538 -- in effect, then we skip building the procedure. This is safe because
3539 -- if we can see the restriction, so can any caller, calls to initialize
3540 -- such records are not allowed for variant records if this restriction
3541 -- is active.
3543 if Has_Variant_Part (Rec_Type)
3544 and then Restriction_Active (No_Implicit_Conditionals)
3545 then
3546 return;
3547 end if;
3549 -- If there are discriminants, build the discriminant map to replace
3550 -- discriminants by their discriminals in complex bound expressions.
3551 -- These only arise for the corresponding records of synchronized types.
3553 if Is_Concurrent_Record_Type (Rec_Type)
3554 and then Has_Discriminants (Rec_Type)
3555 then
3556 declare
3557 Disc : Entity_Id;
3558 begin
3559 Disc := First_Discriminant (Rec_Type);
3560 while Present (Disc) loop
3561 Append_Elmt (Disc, Discr_Map);
3562 Append_Elmt (Discriminal (Disc), Discr_Map);
3563 Next_Discriminant (Disc);
3564 end loop;
3565 end;
3566 end if;
3568 -- Derived types that have no type extension can use the initialization
3569 -- procedure of their parent and do not need a procedure of their own.
3570 -- This is only correct if there are no representation clauses for the
3571 -- type or its parent, and if the parent has in fact been frozen so
3572 -- that its initialization procedure exists.
3574 if Is_Derived_Type (Rec_Type)
3575 and then not Is_Tagged_Type (Rec_Type)
3576 and then not Is_Unchecked_Union (Rec_Type)
3577 and then not Has_New_Non_Standard_Rep (Rec_Type)
3578 and then not Parent_Subtype_Renaming_Discrims
3579 and then Has_Non_Null_Base_Init_Proc (Etype (Rec_Type))
3580 then
3581 Copy_TSS (Base_Init_Proc (Etype (Rec_Type)), Rec_Type);
3583 -- Otherwise if we need an initialization procedure, then build one,
3584 -- mark it as public and inlinable and as having a completion.
3586 elsif Requires_Init_Proc (Rec_Type)
3587 or else Is_Unchecked_Union (Rec_Type)
3588 then
3589 Proc_Id :=
3590 Make_Defining_Identifier (Loc,
3591 Chars => Make_Init_Proc_Name (Rec_Type));
3593 -- If No_Default_Initialization restriction is active, then we don't
3594 -- want to build an init_proc, but we need to mark that an init_proc
3595 -- would be needed if this restriction was not active (so that we can
3596 -- detect attempts to call it), so set a dummy init_proc in place.
3598 if Restriction_Active (No_Default_Initialization) then
3599 Set_Init_Proc (Rec_Type, Proc_Id);
3600 return;
3601 end if;
3603 Build_Offset_To_Top_Functions;
3604 Build_CPP_Init_Procedure;
3605 Build_Init_Procedure;
3607 Set_Is_Public (Proc_Id, Is_Public (Rec_Ent));
3608 Set_Is_Internal (Proc_Id);
3609 Set_Has_Completion (Proc_Id);
3611 if not Debug_Generated_Code then
3612 Set_Debug_Info_Off (Proc_Id);
3613 end if;
3615 Set_Is_Inlined (Proc_Id, Inline_Init_Proc (Rec_Type));
3617 -- Do not build an aggregate if Modify_Tree_For_C, this isn't
3618 -- needed and may generate early references to non frozen types
3619 -- since we expand aggregate much more systematically.
3621 if Modify_Tree_For_C then
3622 return;
3623 end if;
3625 declare
3626 Agg : constant Node_Id :=
3627 Build_Equivalent_Record_Aggregate (Rec_Type);
3629 procedure Collect_Itypes (Comp : Node_Id);
3630 -- Generate references to itypes in the aggregate, because
3631 -- the first use of the aggregate may be in a nested scope.
3633 --------------------
3634 -- Collect_Itypes --
3635 --------------------
3637 procedure Collect_Itypes (Comp : Node_Id) is
3638 Ref : Node_Id;
3639 Sub_Aggr : Node_Id;
3640 Typ : constant Entity_Id := Etype (Comp);
3642 begin
3643 if Is_Array_Type (Typ) and then Is_Itype (Typ) then
3644 Ref := Make_Itype_Reference (Loc);
3645 Set_Itype (Ref, Typ);
3646 Append_Freeze_Action (Rec_Type, Ref);
3648 Ref := Make_Itype_Reference (Loc);
3649 Set_Itype (Ref, Etype (First_Index (Typ)));
3650 Append_Freeze_Action (Rec_Type, Ref);
3652 -- Recurse on nested arrays
3654 Sub_Aggr := First (Expressions (Comp));
3655 while Present (Sub_Aggr) loop
3656 Collect_Itypes (Sub_Aggr);
3657 Next (Sub_Aggr);
3658 end loop;
3659 end if;
3660 end Collect_Itypes;
3662 begin
3663 -- If there is a static initialization aggregate for the type,
3664 -- generate itype references for the types of its (sub)components,
3665 -- to prevent out-of-scope errors in the resulting tree.
3666 -- The aggregate may have been rewritten as a Raise node, in which
3667 -- case there are no relevant itypes.
3669 if Present (Agg) and then Nkind (Agg) = N_Aggregate then
3670 Set_Static_Initialization (Proc_Id, Agg);
3672 declare
3673 Comp : Node_Id;
3674 begin
3675 Comp := First (Component_Associations (Agg));
3676 while Present (Comp) loop
3677 Collect_Itypes (Expression (Comp));
3678 Next (Comp);
3679 end loop;
3680 end;
3681 end if;
3682 end;
3683 end if;
3684 end Build_Record_Init_Proc;
3686 ----------------------------
3687 -- Build_Slice_Assignment --
3688 ----------------------------
3690 -- Generates the following subprogram:
3692 -- procedure Assign
3693 -- (Source, Target : Array_Type,
3694 -- Left_Lo, Left_Hi : Index;
3695 -- Right_Lo, Right_Hi : Index;
3696 -- Rev : Boolean)
3697 -- is
3698 -- Li1 : Index;
3699 -- Ri1 : Index;
3701 -- begin
3703 -- if Left_Hi < Left_Lo then
3704 -- return;
3705 -- end if;
3707 -- if Rev then
3708 -- Li1 := Left_Hi;
3709 -- Ri1 := Right_Hi;
3710 -- else
3711 -- Li1 := Left_Lo;
3712 -- Ri1 := Right_Lo;
3713 -- end if;
3715 -- loop
3716 -- Target (Li1) := Source (Ri1);
3718 -- if Rev then
3719 -- exit when Li1 = Left_Lo;
3720 -- Li1 := Index'pred (Li1);
3721 -- Ri1 := Index'pred (Ri1);
3722 -- else
3723 -- exit when Li1 = Left_Hi;
3724 -- Li1 := Index'succ (Li1);
3725 -- Ri1 := Index'succ (Ri1);
3726 -- end if;
3727 -- end loop;
3728 -- end Assign;
3730 procedure Build_Slice_Assignment (Typ : Entity_Id) is
3731 Loc : constant Source_Ptr := Sloc (Typ);
3732 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
3734 Larray : constant Entity_Id := Make_Temporary (Loc, 'A');
3735 Rarray : constant Entity_Id := Make_Temporary (Loc, 'R');
3736 Left_Lo : constant Entity_Id := Make_Temporary (Loc, 'L');
3737 Left_Hi : constant Entity_Id := Make_Temporary (Loc, 'L');
3738 Right_Lo : constant Entity_Id := Make_Temporary (Loc, 'R');
3739 Right_Hi : constant Entity_Id := Make_Temporary (Loc, 'R');
3740 Rev : constant Entity_Id := Make_Temporary (Loc, 'D');
3741 -- Formal parameters of procedure
3743 Proc_Name : constant Entity_Id :=
3744 Make_Defining_Identifier (Loc,
3745 Chars => Make_TSS_Name (Typ, TSS_Slice_Assign));
3747 Lnn : constant Entity_Id := Make_Temporary (Loc, 'L');
3748 Rnn : constant Entity_Id := Make_Temporary (Loc, 'R');
3749 -- Subscripts for left and right sides
3751 Decls : List_Id;
3752 Loops : Node_Id;
3753 Stats : List_Id;
3755 begin
3756 -- Build declarations for indexes
3758 Decls := New_List;
3760 Append_To (Decls,
3761 Make_Object_Declaration (Loc,
3762 Defining_Identifier => Lnn,
3763 Object_Definition =>
3764 New_Occurrence_Of (Index, Loc)));
3766 Append_To (Decls,
3767 Make_Object_Declaration (Loc,
3768 Defining_Identifier => Rnn,
3769 Object_Definition =>
3770 New_Occurrence_Of (Index, Loc)));
3772 Stats := New_List;
3774 -- Build test for empty slice case
3776 Append_To (Stats,
3777 Make_If_Statement (Loc,
3778 Condition =>
3779 Make_Op_Lt (Loc,
3780 Left_Opnd => New_Occurrence_Of (Left_Hi, Loc),
3781 Right_Opnd => New_Occurrence_Of (Left_Lo, Loc)),
3782 Then_Statements => New_List (Make_Simple_Return_Statement (Loc))));
3784 -- Build initializations for indexes
3786 declare
3787 F_Init : constant List_Id := New_List;
3788 B_Init : constant List_Id := New_List;
3790 begin
3791 Append_To (F_Init,
3792 Make_Assignment_Statement (Loc,
3793 Name => New_Occurrence_Of (Lnn, Loc),
3794 Expression => New_Occurrence_Of (Left_Lo, Loc)));
3796 Append_To (F_Init,
3797 Make_Assignment_Statement (Loc,
3798 Name => New_Occurrence_Of (Rnn, Loc),
3799 Expression => New_Occurrence_Of (Right_Lo, Loc)));
3801 Append_To (B_Init,
3802 Make_Assignment_Statement (Loc,
3803 Name => New_Occurrence_Of (Lnn, Loc),
3804 Expression => New_Occurrence_Of (Left_Hi, Loc)));
3806 Append_To (B_Init,
3807 Make_Assignment_Statement (Loc,
3808 Name => New_Occurrence_Of (Rnn, Loc),
3809 Expression => New_Occurrence_Of (Right_Hi, Loc)));
3811 Append_To (Stats,
3812 Make_If_Statement (Loc,
3813 Condition => New_Occurrence_Of (Rev, Loc),
3814 Then_Statements => B_Init,
3815 Else_Statements => F_Init));
3816 end;
3818 -- Now construct the assignment statement
3820 Loops :=
3821 Make_Loop_Statement (Loc,
3822 Statements => New_List (
3823 Make_Assignment_Statement (Loc,
3824 Name =>
3825 Make_Indexed_Component (Loc,
3826 Prefix => New_Occurrence_Of (Larray, Loc),
3827 Expressions => New_List (New_Occurrence_Of (Lnn, Loc))),
3828 Expression =>
3829 Make_Indexed_Component (Loc,
3830 Prefix => New_Occurrence_Of (Rarray, Loc),
3831 Expressions => New_List (New_Occurrence_Of (Rnn, Loc))))),
3832 End_Label => Empty);
3834 -- Build the exit condition and increment/decrement statements
3836 declare
3837 F_Ass : constant List_Id := New_List;
3838 B_Ass : constant List_Id := New_List;
3840 begin
3841 Append_To (F_Ass,
3842 Make_Exit_Statement (Loc,
3843 Condition =>
3844 Make_Op_Eq (Loc,
3845 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
3846 Right_Opnd => New_Occurrence_Of (Left_Hi, Loc))));
3848 Append_To (F_Ass,
3849 Make_Assignment_Statement (Loc,
3850 Name => New_Occurrence_Of (Lnn, Loc),
3851 Expression =>
3852 Make_Attribute_Reference (Loc,
3853 Prefix =>
3854 New_Occurrence_Of (Index, Loc),
3855 Attribute_Name => Name_Succ,
3856 Expressions => New_List (
3857 New_Occurrence_Of (Lnn, Loc)))));
3859 Append_To (F_Ass,
3860 Make_Assignment_Statement (Loc,
3861 Name => New_Occurrence_Of (Rnn, Loc),
3862 Expression =>
3863 Make_Attribute_Reference (Loc,
3864 Prefix =>
3865 New_Occurrence_Of (Index, Loc),
3866 Attribute_Name => Name_Succ,
3867 Expressions => New_List (
3868 New_Occurrence_Of (Rnn, Loc)))));
3870 Append_To (B_Ass,
3871 Make_Exit_Statement (Loc,
3872 Condition =>
3873 Make_Op_Eq (Loc,
3874 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
3875 Right_Opnd => New_Occurrence_Of (Left_Lo, Loc))));
3877 Append_To (B_Ass,
3878 Make_Assignment_Statement (Loc,
3879 Name => New_Occurrence_Of (Lnn, Loc),
3880 Expression =>
3881 Make_Attribute_Reference (Loc,
3882 Prefix =>
3883 New_Occurrence_Of (Index, Loc),
3884 Attribute_Name => Name_Pred,
3885 Expressions => New_List (
3886 New_Occurrence_Of (Lnn, Loc)))));
3888 Append_To (B_Ass,
3889 Make_Assignment_Statement (Loc,
3890 Name => New_Occurrence_Of (Rnn, Loc),
3891 Expression =>
3892 Make_Attribute_Reference (Loc,
3893 Prefix =>
3894 New_Occurrence_Of (Index, Loc),
3895 Attribute_Name => Name_Pred,
3896 Expressions => New_List (
3897 New_Occurrence_Of (Rnn, Loc)))));
3899 Append_To (Statements (Loops),
3900 Make_If_Statement (Loc,
3901 Condition => New_Occurrence_Of (Rev, Loc),
3902 Then_Statements => B_Ass,
3903 Else_Statements => F_Ass));
3904 end;
3906 Append_To (Stats, Loops);
3908 declare
3909 Spec : Node_Id;
3910 Formals : List_Id := New_List;
3912 begin
3913 Formals := New_List (
3914 Make_Parameter_Specification (Loc,
3915 Defining_Identifier => Larray,
3916 Out_Present => True,
3917 Parameter_Type =>
3918 New_Occurrence_Of (Base_Type (Typ), Loc)),
3920 Make_Parameter_Specification (Loc,
3921 Defining_Identifier => Rarray,
3922 Parameter_Type =>
3923 New_Occurrence_Of (Base_Type (Typ), Loc)),
3925 Make_Parameter_Specification (Loc,
3926 Defining_Identifier => Left_Lo,
3927 Parameter_Type =>
3928 New_Occurrence_Of (Index, Loc)),
3930 Make_Parameter_Specification (Loc,
3931 Defining_Identifier => Left_Hi,
3932 Parameter_Type =>
3933 New_Occurrence_Of (Index, Loc)),
3935 Make_Parameter_Specification (Loc,
3936 Defining_Identifier => Right_Lo,
3937 Parameter_Type =>
3938 New_Occurrence_Of (Index, Loc)),
3940 Make_Parameter_Specification (Loc,
3941 Defining_Identifier => Right_Hi,
3942 Parameter_Type =>
3943 New_Occurrence_Of (Index, Loc)));
3945 Append_To (Formals,
3946 Make_Parameter_Specification (Loc,
3947 Defining_Identifier => Rev,
3948 Parameter_Type =>
3949 New_Occurrence_Of (Standard_Boolean, Loc)));
3951 Spec :=
3952 Make_Procedure_Specification (Loc,
3953 Defining_Unit_Name => Proc_Name,
3954 Parameter_Specifications => Formals);
3956 Discard_Node (
3957 Make_Subprogram_Body (Loc,
3958 Specification => Spec,
3959 Declarations => Decls,
3960 Handled_Statement_Sequence =>
3961 Make_Handled_Sequence_Of_Statements (Loc,
3962 Statements => Stats)));
3963 end;
3965 Set_TSS (Typ, Proc_Name);
3966 Set_Is_Pure (Proc_Name);
3967 end Build_Slice_Assignment;
3969 -----------------------------
3970 -- Build_Untagged_Equality --
3971 -----------------------------
3973 procedure Build_Untagged_Equality (Typ : Entity_Id) is
3974 Build_Eq : Boolean;
3975 Comp : Entity_Id;
3976 Decl : Node_Id;
3977 Op : Entity_Id;
3978 Prim : Elmt_Id;
3979 Eq_Op : Entity_Id;
3981 function User_Defined_Eq (T : Entity_Id) return Entity_Id;
3982 -- Check whether the type T has a user-defined primitive equality. If so
3983 -- return it, else return Empty. If true for a component of Typ, we have
3984 -- to build the primitive equality for it.
3986 ---------------------
3987 -- User_Defined_Eq --
3988 ---------------------
3990 function User_Defined_Eq (T : Entity_Id) return Entity_Id is
3991 Prim : Elmt_Id;
3992 Op : Entity_Id;
3994 begin
3995 Op := TSS (T, TSS_Composite_Equality);
3997 if Present (Op) then
3998 return Op;
3999 end if;
4001 Prim := First_Elmt (Collect_Primitive_Operations (T));
4002 while Present (Prim) loop
4003 Op := Node (Prim);
4005 if Chars (Op) = Name_Op_Eq
4006 and then Etype (Op) = Standard_Boolean
4007 and then Etype (First_Formal (Op)) = T
4008 and then Etype (Next_Formal (First_Formal (Op))) = T
4009 then
4010 return Op;
4011 end if;
4013 Next_Elmt (Prim);
4014 end loop;
4016 return Empty;
4017 end User_Defined_Eq;
4019 -- Start of processing for Build_Untagged_Equality
4021 begin
4022 -- If a record component has a primitive equality operation, we must
4023 -- build the corresponding one for the current type.
4025 Build_Eq := False;
4026 Comp := First_Component (Typ);
4027 while Present (Comp) loop
4028 if Is_Record_Type (Etype (Comp))
4029 and then Present (User_Defined_Eq (Etype (Comp)))
4030 then
4031 Build_Eq := True;
4032 end if;
4034 Next_Component (Comp);
4035 end loop;
4037 -- If there is a user-defined equality for the type, we do not create
4038 -- the implicit one.
4040 Prim := First_Elmt (Collect_Primitive_Operations (Typ));
4041 Eq_Op := Empty;
4042 while Present (Prim) loop
4043 if Chars (Node (Prim)) = Name_Op_Eq
4044 and then Comes_From_Source (Node (Prim))
4046 -- Don't we also need to check formal types and return type as in
4047 -- User_Defined_Eq above???
4049 then
4050 Eq_Op := Node (Prim);
4051 Build_Eq := False;
4052 exit;
4053 end if;
4055 Next_Elmt (Prim);
4056 end loop;
4058 -- If the type is derived, inherit the operation, if present, from the
4059 -- parent type. It may have been declared after the type derivation. If
4060 -- the parent type itself is derived, it may have inherited an operation
4061 -- that has itself been overridden, so update its alias and related
4062 -- flags. Ditto for inequality.
4064 if No (Eq_Op) and then Is_Derived_Type (Typ) then
4065 Prim := First_Elmt (Collect_Primitive_Operations (Etype (Typ)));
4066 while Present (Prim) loop
4067 if Chars (Node (Prim)) = Name_Op_Eq then
4068 Copy_TSS (Node (Prim), Typ);
4069 Build_Eq := False;
4071 declare
4072 Op : constant Entity_Id := User_Defined_Eq (Typ);
4073 Eq_Op : constant Entity_Id := Node (Prim);
4074 NE_Op : constant Entity_Id := Next_Entity (Eq_Op);
4076 begin
4077 if Present (Op) then
4078 Set_Alias (Op, Eq_Op);
4079 Set_Is_Abstract_Subprogram
4080 (Op, Is_Abstract_Subprogram (Eq_Op));
4082 if Chars (Next_Entity (Op)) = Name_Op_Ne then
4083 Set_Is_Abstract_Subprogram
4084 (Next_Entity (Op), Is_Abstract_Subprogram (NE_Op));
4085 end if;
4086 end if;
4087 end;
4089 exit;
4090 end if;
4092 Next_Elmt (Prim);
4093 end loop;
4094 end if;
4096 -- If not inherited and not user-defined, build body as for a type with
4097 -- tagged components.
4099 if Build_Eq then
4100 Decl :=
4101 Make_Eq_Body (Typ, Make_TSS_Name (Typ, TSS_Composite_Equality));
4102 Op := Defining_Entity (Decl);
4103 Set_TSS (Typ, Op);
4104 Set_Is_Pure (Op);
4106 if Is_Library_Level_Entity (Typ) then
4107 Set_Is_Public (Op);
4108 end if;
4109 end if;
4110 end Build_Untagged_Equality;
4112 -----------------------------------
4113 -- Build_Variant_Record_Equality --
4114 -----------------------------------
4116 -- Generates:
4118 -- function _Equality (X, Y : T) return Boolean is
4119 -- begin
4120 -- -- Compare discriminants
4122 -- if X.D1 /= Y.D1 or else X.D2 /= Y.D2 or else ... then
4123 -- return False;
4124 -- end if;
4126 -- -- Compare components
4128 -- if X.C1 /= Y.C1 or else X.C2 /= Y.C2 or else ... then
4129 -- return False;
4130 -- end if;
4132 -- -- Compare variant part
4134 -- case X.D1 is
4135 -- when V1 =>
4136 -- if X.C2 /= Y.C2 or else X.C3 /= Y.C3 or else ... then
4137 -- return False;
4138 -- end if;
4139 -- ...
4140 -- when Vn =>
4141 -- if X.Cn /= Y.Cn or else ... then
4142 -- return False;
4143 -- end if;
4144 -- end case;
4146 -- return True;
4147 -- end _Equality;
4149 procedure Build_Variant_Record_Equality (Typ : Entity_Id) is
4150 Loc : constant Source_Ptr := Sloc (Typ);
4152 F : constant Entity_Id :=
4153 Make_Defining_Identifier (Loc,
4154 Chars => Make_TSS_Name (Typ, TSS_Composite_Equality));
4156 X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_X);
4157 Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_Y);
4159 Def : constant Node_Id := Parent (Typ);
4160 Comps : constant Node_Id := Component_List (Type_Definition (Def));
4161 Stmts : constant List_Id := New_List;
4162 Pspecs : constant List_Id := New_List;
4164 begin
4165 -- If we have a variant record with restriction No_Implicit_Conditionals
4166 -- in effect, then we skip building the procedure. This is safe because
4167 -- if we can see the restriction, so can any caller, calls to equality
4168 -- test routines are not allowed for variant records if this restriction
4169 -- is active.
4171 if Restriction_Active (No_Implicit_Conditionals) then
4172 return;
4173 end if;
4175 -- Derived Unchecked_Union types no longer inherit the equality function
4176 -- of their parent.
4178 if Is_Derived_Type (Typ)
4179 and then not Is_Unchecked_Union (Typ)
4180 and then not Has_New_Non_Standard_Rep (Typ)
4181 then
4182 declare
4183 Parent_Eq : constant Entity_Id :=
4184 TSS (Root_Type (Typ), TSS_Composite_Equality);
4185 begin
4186 if Present (Parent_Eq) then
4187 Copy_TSS (Parent_Eq, Typ);
4188 return;
4189 end if;
4190 end;
4191 end if;
4193 Discard_Node (
4194 Make_Subprogram_Body (Loc,
4195 Specification =>
4196 Make_Function_Specification (Loc,
4197 Defining_Unit_Name => F,
4198 Parameter_Specifications => Pspecs,
4199 Result_Definition => New_Occurrence_Of (Standard_Boolean, Loc)),
4200 Declarations => New_List,
4201 Handled_Statement_Sequence =>
4202 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts)));
4204 Append_To (Pspecs,
4205 Make_Parameter_Specification (Loc,
4206 Defining_Identifier => X,
4207 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
4209 Append_To (Pspecs,
4210 Make_Parameter_Specification (Loc,
4211 Defining_Identifier => Y,
4212 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
4214 -- Unchecked_Unions require additional machinery to support equality.
4215 -- Two extra parameters (A and B) are added to the equality function
4216 -- parameter list for each discriminant of the type, in order to
4217 -- capture the inferred values of the discriminants in equality calls.
4218 -- The names of the parameters match the names of the corresponding
4219 -- discriminant, with an added suffix.
4221 if Is_Unchecked_Union (Typ) then
4222 declare
4223 Discr : Entity_Id;
4224 Discr_Type : Entity_Id;
4225 A, B : Entity_Id;
4226 New_Discrs : Elist_Id;
4228 begin
4229 New_Discrs := New_Elmt_List;
4231 Discr := First_Discriminant (Typ);
4232 while Present (Discr) loop
4233 Discr_Type := Etype (Discr);
4234 A := Make_Defining_Identifier (Loc,
4235 Chars => New_External_Name (Chars (Discr), 'A'));
4237 B := Make_Defining_Identifier (Loc,
4238 Chars => New_External_Name (Chars (Discr), 'B'));
4240 -- Add new parameters to the parameter list
4242 Append_To (Pspecs,
4243 Make_Parameter_Specification (Loc,
4244 Defining_Identifier => A,
4245 Parameter_Type =>
4246 New_Occurrence_Of (Discr_Type, Loc)));
4248 Append_To (Pspecs,
4249 Make_Parameter_Specification (Loc,
4250 Defining_Identifier => B,
4251 Parameter_Type =>
4252 New_Occurrence_Of (Discr_Type, Loc)));
4254 Append_Elmt (A, New_Discrs);
4256 -- Generate the following code to compare each of the inferred
4257 -- discriminants:
4259 -- if a /= b then
4260 -- return False;
4261 -- end if;
4263 Append_To (Stmts,
4264 Make_If_Statement (Loc,
4265 Condition =>
4266 Make_Op_Ne (Loc,
4267 Left_Opnd => New_Occurrence_Of (A, Loc),
4268 Right_Opnd => New_Occurrence_Of (B, Loc)),
4269 Then_Statements => New_List (
4270 Make_Simple_Return_Statement (Loc,
4271 Expression =>
4272 New_Occurrence_Of (Standard_False, Loc)))));
4273 Next_Discriminant (Discr);
4274 end loop;
4276 -- Generate component-by-component comparison. Note that we must
4277 -- propagate the inferred discriminants formals to act as
4278 -- the case statement switch. Their value is added when an
4279 -- equality call on unchecked unions is expanded.
4281 Append_List_To (Stmts, Make_Eq_Case (Typ, Comps, New_Discrs));
4282 end;
4284 -- Normal case (not unchecked union)
4286 else
4287 Append_To (Stmts,
4288 Make_Eq_If (Typ, Discriminant_Specifications (Def)));
4289 Append_List_To (Stmts, Make_Eq_Case (Typ, Comps));
4290 end if;
4292 Append_To (Stmts,
4293 Make_Simple_Return_Statement (Loc,
4294 Expression => New_Occurrence_Of (Standard_True, Loc)));
4296 Set_TSS (Typ, F);
4297 Set_Is_Pure (F);
4299 if not Debug_Generated_Code then
4300 Set_Debug_Info_Off (F);
4301 end if;
4302 end Build_Variant_Record_Equality;
4304 -----------------------------
4305 -- Check_Stream_Attributes --
4306 -----------------------------
4308 procedure Check_Stream_Attributes (Typ : Entity_Id) is
4309 Comp : Entity_Id;
4310 Par_Read : constant Boolean :=
4311 Stream_Attribute_Available (Typ, TSS_Stream_Read)
4312 and then not Has_Specified_Stream_Read (Typ);
4313 Par_Write : constant Boolean :=
4314 Stream_Attribute_Available (Typ, TSS_Stream_Write)
4315 and then not Has_Specified_Stream_Write (Typ);
4317 procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type);
4318 -- Check that Comp has a user-specified Nam stream attribute
4320 ----------------
4321 -- Check_Attr --
4322 ----------------
4324 procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type) is
4325 begin
4326 if not Stream_Attribute_Available (Etype (Comp), TSS_Nam) then
4327 Error_Msg_Name_1 := Nam;
4328 Error_Msg_N
4329 ("|component& in limited extension must have% attribute", Comp);
4330 end if;
4331 end Check_Attr;
4333 -- Start of processing for Check_Stream_Attributes
4335 begin
4336 if Par_Read or else Par_Write then
4337 Comp := First_Component (Typ);
4338 while Present (Comp) loop
4339 if Comes_From_Source (Comp)
4340 and then Original_Record_Component (Comp) = Comp
4341 and then Is_Limited_Type (Etype (Comp))
4342 then
4343 if Par_Read then
4344 Check_Attr (Name_Read, TSS_Stream_Read);
4345 end if;
4347 if Par_Write then
4348 Check_Attr (Name_Write, TSS_Stream_Write);
4349 end if;
4350 end if;
4352 Next_Component (Comp);
4353 end loop;
4354 end if;
4355 end Check_Stream_Attributes;
4357 ----------------------
4358 -- Clean_Task_Names --
4359 ----------------------
4361 procedure Clean_Task_Names
4362 (Typ : Entity_Id;
4363 Proc_Id : Entity_Id)
4365 begin
4366 if Has_Task (Typ)
4367 and then not Restriction_Active (No_Implicit_Heap_Allocations)
4368 and then not Global_Discard_Names
4369 and then Tagged_Type_Expansion
4370 then
4371 Set_Uses_Sec_Stack (Proc_Id);
4372 end if;
4373 end Clean_Task_Names;
4375 ------------------------------
4376 -- Expand_Freeze_Array_Type --
4377 ------------------------------
4379 procedure Expand_Freeze_Array_Type (N : Node_Id) is
4380 Typ : constant Entity_Id := Entity (N);
4381 Base : constant Entity_Id := Base_Type (Typ);
4382 Comp_Typ : constant Entity_Id := Component_Type (Typ);
4384 begin
4385 if not Is_Bit_Packed_Array (Typ) then
4387 -- If the component contains tasks, so does the array type. This may
4388 -- not be indicated in the array type because the component may have
4389 -- been a private type at the point of definition. Same if component
4390 -- type is controlled or contains protected objects.
4392 Propagate_Concurrent_Flags (Base, Comp_Typ);
4393 Set_Has_Controlled_Component
4394 (Base, Has_Controlled_Component (Comp_Typ)
4395 or else Is_Controlled (Comp_Typ));
4397 if No (Init_Proc (Base)) then
4399 -- If this is an anonymous array created for a declaration with
4400 -- an initial value, its init_proc will never be called. The
4401 -- initial value itself may have been expanded into assignments,
4402 -- in which case the object declaration is carries the
4403 -- No_Initialization flag.
4405 if Is_Itype (Base)
4406 and then Nkind (Associated_Node_For_Itype (Base)) =
4407 N_Object_Declaration
4408 and then
4409 (Present (Expression (Associated_Node_For_Itype (Base)))
4410 or else No_Initialization (Associated_Node_For_Itype (Base)))
4411 then
4412 null;
4414 -- We do not need an init proc for string or wide [wide] string,
4415 -- since the only time these need initialization in normalize or
4416 -- initialize scalars mode, and these types are treated specially
4417 -- and do not need initialization procedures.
4419 elsif Is_Standard_String_Type (Base) then
4420 null;
4422 -- Otherwise we have to build an init proc for the subtype
4424 else
4425 Build_Array_Init_Proc (Base, N);
4426 end if;
4427 end if;
4429 if Typ = Base and then Has_Controlled_Component (Base) then
4430 Build_Controlling_Procs (Base);
4432 if not Is_Limited_Type (Comp_Typ)
4433 and then Number_Dimensions (Typ) = 1
4434 then
4435 Build_Slice_Assignment (Typ);
4436 end if;
4437 end if;
4439 -- For packed case, default initialization, except if the component type
4440 -- is itself a packed structure with an initialization procedure, or
4441 -- initialize/normalize scalars active, and we have a base type, or the
4442 -- type is public, because in that case a client might specify
4443 -- Normalize_Scalars and there better be a public Init_Proc for it.
4445 elsif (Present (Init_Proc (Component_Type (Base)))
4446 and then No (Base_Init_Proc (Base)))
4447 or else (Init_Or_Norm_Scalars and then Base = Typ)
4448 or else Is_Public (Typ)
4449 then
4450 Build_Array_Init_Proc (Base, N);
4451 end if;
4452 end Expand_Freeze_Array_Type;
4454 -----------------------------------
4455 -- Expand_Freeze_Class_Wide_Type --
4456 -----------------------------------
4458 procedure Expand_Freeze_Class_Wide_Type (N : Node_Id) is
4459 function Is_C_Derivation (Typ : Entity_Id) return Boolean;
4460 -- Given a type, determine whether it is derived from a C or C++ root
4462 ---------------------
4463 -- Is_C_Derivation --
4464 ---------------------
4466 function Is_C_Derivation (Typ : Entity_Id) return Boolean is
4467 T : Entity_Id;
4469 begin
4470 T := Typ;
4471 loop
4472 if Is_CPP_Class (T)
4473 or else Convention (T) = Convention_C
4474 or else Convention (T) = Convention_CPP
4475 then
4476 return True;
4477 end if;
4479 exit when T = Etype (T);
4481 T := Etype (T);
4482 end loop;
4484 return False;
4485 end Is_C_Derivation;
4487 -- Local variables
4489 Typ : constant Entity_Id := Entity (N);
4490 Root : constant Entity_Id := Root_Type (Typ);
4492 -- Start of processing for Expand_Freeze_Class_Wide_Type
4494 begin
4495 -- Certain run-time configurations and targets do not provide support
4496 -- for controlled types.
4498 if Restriction_Active (No_Finalization) then
4499 return;
4501 -- Do not create TSS routine Finalize_Address when dispatching calls are
4502 -- disabled since the core of the routine is a dispatching call.
4504 elsif Restriction_Active (No_Dispatching_Calls) then
4505 return;
4507 -- Do not create TSS routine Finalize_Address for concurrent class-wide
4508 -- types. Ignore C, C++, CIL and Java types since it is assumed that the
4509 -- non-Ada side will handle their destruction.
4511 elsif Is_Concurrent_Type (Root)
4512 or else Is_C_Derivation (Root)
4513 or else Convention (Typ) = Convention_CPP
4514 then
4515 return;
4517 -- Do not create TSS routine Finalize_Address when compiling in CodePeer
4518 -- mode since the routine contains an Unchecked_Conversion.
4520 elsif CodePeer_Mode then
4521 return;
4522 end if;
4524 -- Create the body of TSS primitive Finalize_Address. This automatically
4525 -- sets the TSS entry for the class-wide type.
4527 Make_Finalize_Address_Body (Typ);
4528 end Expand_Freeze_Class_Wide_Type;
4530 ------------------------------------
4531 -- Expand_Freeze_Enumeration_Type --
4532 ------------------------------------
4534 procedure Expand_Freeze_Enumeration_Type (N : Node_Id) is
4535 Typ : constant Entity_Id := Entity (N);
4536 Loc : constant Source_Ptr := Sloc (Typ);
4538 Arr : Entity_Id;
4539 Ent : Entity_Id;
4540 Fent : Entity_Id;
4541 Is_Contiguous : Boolean;
4542 Ityp : Entity_Id;
4543 Last_Repval : Uint;
4544 Lst : List_Id;
4545 Num : Nat;
4546 Pos_Expr : Node_Id;
4548 Func : Entity_Id;
4549 pragma Warnings (Off, Func);
4551 begin
4552 -- Various optimizations possible if given representation is contiguous
4554 Is_Contiguous := True;
4556 Ent := First_Literal (Typ);
4557 Last_Repval := Enumeration_Rep (Ent);
4559 Next_Literal (Ent);
4560 while Present (Ent) loop
4561 if Enumeration_Rep (Ent) - Last_Repval /= 1 then
4562 Is_Contiguous := False;
4563 exit;
4564 else
4565 Last_Repval := Enumeration_Rep (Ent);
4566 end if;
4568 Next_Literal (Ent);
4569 end loop;
4571 if Is_Contiguous then
4572 Set_Has_Contiguous_Rep (Typ);
4573 Ent := First_Literal (Typ);
4574 Num := 1;
4575 Lst := New_List (New_Occurrence_Of (Ent, Sloc (Ent)));
4577 else
4578 -- Build list of literal references
4580 Lst := New_List;
4581 Num := 0;
4583 Ent := First_Literal (Typ);
4584 while Present (Ent) loop
4585 Append_To (Lst, New_Occurrence_Of (Ent, Sloc (Ent)));
4586 Num := Num + 1;
4587 Next_Literal (Ent);
4588 end loop;
4589 end if;
4591 -- Now build an array declaration
4593 -- typA : array (Natural range 0 .. num - 1) of ctype :=
4594 -- (v, v, v, v, v, ....)
4596 -- where ctype is the corresponding integer type. If the representation
4597 -- is contiguous, we only keep the first literal, which provides the
4598 -- offset for Pos_To_Rep computations.
4600 Arr :=
4601 Make_Defining_Identifier (Loc,
4602 Chars => New_External_Name (Chars (Typ), 'A'));
4604 Append_Freeze_Action (Typ,
4605 Make_Object_Declaration (Loc,
4606 Defining_Identifier => Arr,
4607 Constant_Present => True,
4609 Object_Definition =>
4610 Make_Constrained_Array_Definition (Loc,
4611 Discrete_Subtype_Definitions => New_List (
4612 Make_Subtype_Indication (Loc,
4613 Subtype_Mark => New_Occurrence_Of (Standard_Natural, Loc),
4614 Constraint =>
4615 Make_Range_Constraint (Loc,
4616 Range_Expression =>
4617 Make_Range (Loc,
4618 Low_Bound =>
4619 Make_Integer_Literal (Loc, 0),
4620 High_Bound =>
4621 Make_Integer_Literal (Loc, Num - 1))))),
4623 Component_Definition =>
4624 Make_Component_Definition (Loc,
4625 Aliased_Present => False,
4626 Subtype_Indication => New_Occurrence_Of (Typ, Loc))),
4628 Expression =>
4629 Make_Aggregate (Loc,
4630 Expressions => Lst)));
4632 Set_Enum_Pos_To_Rep (Typ, Arr);
4634 -- Now we build the function that converts representation values to
4635 -- position values. This function has the form:
4637 -- function _Rep_To_Pos (A : etype; F : Boolean) return Integer is
4638 -- begin
4639 -- case ityp!(A) is
4640 -- when enum-lit'Enum_Rep => return posval;
4641 -- when enum-lit'Enum_Rep => return posval;
4642 -- ...
4643 -- when others =>
4644 -- [raise Constraint_Error when F "invalid data"]
4645 -- return -1;
4646 -- end case;
4647 -- end;
4649 -- Note: the F parameter determines whether the others case (no valid
4650 -- representation) raises Constraint_Error or returns a unique value
4651 -- of minus one. The latter case is used, e.g. in 'Valid code.
4653 -- Note: the reason we use Enum_Rep values in the case here is to avoid
4654 -- the code generator making inappropriate assumptions about the range
4655 -- of the values in the case where the value is invalid. ityp is a
4656 -- signed or unsigned integer type of appropriate width.
4658 -- Note: if exceptions are not supported, then we suppress the raise
4659 -- and return -1 unconditionally (this is an erroneous program in any
4660 -- case and there is no obligation to raise Constraint_Error here). We
4661 -- also do this if pragma Restrictions (No_Exceptions) is active.
4663 -- Is this right??? What about No_Exception_Propagation???
4665 -- Representations are signed
4667 if Enumeration_Rep (First_Literal (Typ)) < 0 then
4669 -- The underlying type is signed. Reset the Is_Unsigned_Type
4670 -- explicitly, because it might have been inherited from
4671 -- parent type.
4673 Set_Is_Unsigned_Type (Typ, False);
4675 if Esize (Typ) <= Standard_Integer_Size then
4676 Ityp := Standard_Integer;
4677 else
4678 Ityp := Universal_Integer;
4679 end if;
4681 -- Representations are unsigned
4683 else
4684 if Esize (Typ) <= Standard_Integer_Size then
4685 Ityp := RTE (RE_Unsigned);
4686 else
4687 Ityp := RTE (RE_Long_Long_Unsigned);
4688 end if;
4689 end if;
4691 -- The body of the function is a case statement. First collect case
4692 -- alternatives, or optimize the contiguous case.
4694 Lst := New_List;
4696 -- If representation is contiguous, Pos is computed by subtracting
4697 -- the representation of the first literal.
4699 if Is_Contiguous then
4700 Ent := First_Literal (Typ);
4702 if Enumeration_Rep (Ent) = Last_Repval then
4704 -- Another special case: for a single literal, Pos is zero
4706 Pos_Expr := Make_Integer_Literal (Loc, Uint_0);
4708 else
4709 Pos_Expr :=
4710 Convert_To (Standard_Integer,
4711 Make_Op_Subtract (Loc,
4712 Left_Opnd =>
4713 Unchecked_Convert_To
4714 (Ityp, Make_Identifier (Loc, Name_uA)),
4715 Right_Opnd =>
4716 Make_Integer_Literal (Loc,
4717 Intval => Enumeration_Rep (First_Literal (Typ)))));
4718 end if;
4720 Append_To (Lst,
4721 Make_Case_Statement_Alternative (Loc,
4722 Discrete_Choices => New_List (
4723 Make_Range (Sloc (Enumeration_Rep_Expr (Ent)),
4724 Low_Bound =>
4725 Make_Integer_Literal (Loc,
4726 Intval => Enumeration_Rep (Ent)),
4727 High_Bound =>
4728 Make_Integer_Literal (Loc, Intval => Last_Repval))),
4730 Statements => New_List (
4731 Make_Simple_Return_Statement (Loc,
4732 Expression => Pos_Expr))));
4734 else
4735 Ent := First_Literal (Typ);
4736 while Present (Ent) loop
4737 Append_To (Lst,
4738 Make_Case_Statement_Alternative (Loc,
4739 Discrete_Choices => New_List (
4740 Make_Integer_Literal (Sloc (Enumeration_Rep_Expr (Ent)),
4741 Intval => Enumeration_Rep (Ent))),
4743 Statements => New_List (
4744 Make_Simple_Return_Statement (Loc,
4745 Expression =>
4746 Make_Integer_Literal (Loc,
4747 Intval => Enumeration_Pos (Ent))))));
4749 Next_Literal (Ent);
4750 end loop;
4751 end if;
4753 -- In normal mode, add the others clause with the test.
4754 -- If Predicates_Ignored is True, validity checks do not apply to
4755 -- the subtype.
4757 if not No_Exception_Handlers_Set
4758 and then not Predicates_Ignored (Typ)
4759 then
4760 Append_To (Lst,
4761 Make_Case_Statement_Alternative (Loc,
4762 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
4763 Statements => New_List (
4764 Make_Raise_Constraint_Error (Loc,
4765 Condition => Make_Identifier (Loc, Name_uF),
4766 Reason => CE_Invalid_Data),
4767 Make_Simple_Return_Statement (Loc,
4768 Expression => Make_Integer_Literal (Loc, -1)))));
4770 -- If either of the restrictions No_Exceptions_Handlers/Propagation is
4771 -- active then return -1 (we cannot usefully raise Constraint_Error in
4772 -- this case). See description above for further details.
4774 else
4775 Append_To (Lst,
4776 Make_Case_Statement_Alternative (Loc,
4777 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
4778 Statements => New_List (
4779 Make_Simple_Return_Statement (Loc,
4780 Expression => Make_Integer_Literal (Loc, -1)))));
4781 end if;
4783 -- Now we can build the function body
4785 Fent :=
4786 Make_Defining_Identifier (Loc, Make_TSS_Name (Typ, TSS_Rep_To_Pos));
4788 Func :=
4789 Make_Subprogram_Body (Loc,
4790 Specification =>
4791 Make_Function_Specification (Loc,
4792 Defining_Unit_Name => Fent,
4793 Parameter_Specifications => New_List (
4794 Make_Parameter_Specification (Loc,
4795 Defining_Identifier =>
4796 Make_Defining_Identifier (Loc, Name_uA),
4797 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
4798 Make_Parameter_Specification (Loc,
4799 Defining_Identifier =>
4800 Make_Defining_Identifier (Loc, Name_uF),
4801 Parameter_Type =>
4802 New_Occurrence_Of (Standard_Boolean, Loc))),
4804 Result_Definition => New_Occurrence_Of (Standard_Integer, Loc)),
4806 Declarations => Empty_List,
4808 Handled_Statement_Sequence =>
4809 Make_Handled_Sequence_Of_Statements (Loc,
4810 Statements => New_List (
4811 Make_Case_Statement (Loc,
4812 Expression =>
4813 Unchecked_Convert_To
4814 (Ityp, Make_Identifier (Loc, Name_uA)),
4815 Alternatives => Lst))));
4817 Set_TSS (Typ, Fent);
4819 -- Set Pure flag (it will be reset if the current context is not Pure).
4820 -- We also pretend there was a pragma Pure_Function so that for purposes
4821 -- of optimization and constant-folding, we will consider the function
4822 -- Pure even if we are not in a Pure context).
4824 Set_Is_Pure (Fent);
4825 Set_Has_Pragma_Pure_Function (Fent);
4827 -- Unless we are in -gnatD mode, where we are debugging generated code,
4828 -- this is an internal entity for which we don't need debug info.
4830 if not Debug_Generated_Code then
4831 Set_Debug_Info_Off (Fent);
4832 end if;
4834 Set_Is_Inlined (Fent);
4836 exception
4837 when RE_Not_Available =>
4838 return;
4839 end Expand_Freeze_Enumeration_Type;
4841 -------------------------------
4842 -- Expand_Freeze_Record_Type --
4843 -------------------------------
4845 procedure Expand_Freeze_Record_Type (N : Node_Id) is
4846 Typ : constant Node_Id := Entity (N);
4847 Typ_Decl : constant Node_Id := Parent (Typ);
4849 Comp : Entity_Id;
4850 Comp_Typ : Entity_Id;
4851 Predef_List : List_Id;
4853 Wrapper_Decl_List : List_Id := No_List;
4854 Wrapper_Body_List : List_Id := No_List;
4856 Renamed_Eq : Node_Id := Empty;
4857 -- Defining unit name for the predefined equality function in the case
4858 -- where the type has a primitive operation that is a renaming of
4859 -- predefined equality (but only if there is also an overriding
4860 -- user-defined equality function). Used to pass this entity from
4861 -- Make_Predefined_Primitive_Specs to Predefined_Primitive_Bodies.
4863 -- Start of processing for Expand_Freeze_Record_Type
4865 begin
4866 -- Build discriminant checking functions if not a derived type (for
4867 -- derived types that are not tagged types, always use the discriminant
4868 -- checking functions of the parent type). However, for untagged types
4869 -- the derivation may have taken place before the parent was frozen, so
4870 -- we copy explicitly the discriminant checking functions from the
4871 -- parent into the components of the derived type.
4873 if not Is_Derived_Type (Typ)
4874 or else Has_New_Non_Standard_Rep (Typ)
4875 or else Is_Tagged_Type (Typ)
4876 then
4877 Build_Discr_Checking_Funcs (Typ_Decl);
4879 elsif Is_Derived_Type (Typ)
4880 and then not Is_Tagged_Type (Typ)
4882 -- If we have a derived Unchecked_Union, we do not inherit the
4883 -- discriminant checking functions from the parent type since the
4884 -- discriminants are non existent.
4886 and then not Is_Unchecked_Union (Typ)
4887 and then Has_Discriminants (Typ)
4888 then
4889 declare
4890 Old_Comp : Entity_Id;
4892 begin
4893 Old_Comp :=
4894 First_Component (Base_Type (Underlying_Type (Etype (Typ))));
4895 Comp := First_Component (Typ);
4896 while Present (Comp) loop
4897 if Ekind (Comp) = E_Component
4898 and then Chars (Comp) = Chars (Old_Comp)
4899 then
4900 Set_Discriminant_Checking_Func
4901 (Comp, Discriminant_Checking_Func (Old_Comp));
4902 end if;
4904 Next_Component (Old_Comp);
4905 Next_Component (Comp);
4906 end loop;
4907 end;
4908 end if;
4910 if Is_Derived_Type (Typ)
4911 and then Is_Limited_Type (Typ)
4912 and then Is_Tagged_Type (Typ)
4913 then
4914 Check_Stream_Attributes (Typ);
4915 end if;
4917 -- Update task, protected, and controlled component flags, because some
4918 -- of the component types may have been private at the point of the
4919 -- record declaration. Detect anonymous access-to-controlled components.
4921 Comp := First_Component (Typ);
4922 while Present (Comp) loop
4923 Comp_Typ := Etype (Comp);
4925 Propagate_Concurrent_Flags (Typ, Comp_Typ);
4927 -- Do not set Has_Controlled_Component on a class-wide equivalent
4928 -- type. See Make_CW_Equivalent_Type.
4930 if not Is_Class_Wide_Equivalent_Type (Typ)
4931 and then
4932 (Has_Controlled_Component (Comp_Typ)
4933 or else (Chars (Comp) /= Name_uParent
4934 and then (Is_Controlled_Active (Comp_Typ))))
4935 then
4936 Set_Has_Controlled_Component (Typ);
4937 end if;
4939 Next_Component (Comp);
4940 end loop;
4942 -- Handle constructors of untagged CPP_Class types
4944 if not Is_Tagged_Type (Typ) and then Is_CPP_Class (Typ) then
4945 Set_CPP_Constructors (Typ);
4946 end if;
4948 -- Creation of the Dispatch Table. Note that a Dispatch Table is built
4949 -- for regular tagged types as well as for Ada types deriving from a C++
4950 -- Class, but not for tagged types directly corresponding to C++ classes
4951 -- In the later case we assume that it is created in the C++ side and we
4952 -- just use it.
4954 if Is_Tagged_Type (Typ) then
4956 -- Add the _Tag component
4958 if Underlying_Type (Etype (Typ)) = Typ then
4959 Expand_Tagged_Root (Typ);
4960 end if;
4962 if Is_CPP_Class (Typ) then
4963 Set_All_DT_Position (Typ);
4965 -- Create the tag entities with a minimum decoration
4967 if Tagged_Type_Expansion then
4968 Append_Freeze_Actions (Typ, Make_Tags (Typ));
4969 end if;
4971 Set_CPP_Constructors (Typ);
4973 else
4974 if not Building_Static_DT (Typ) then
4976 -- Usually inherited primitives are not delayed but the first
4977 -- Ada extension of a CPP_Class is an exception since the
4978 -- address of the inherited subprogram has to be inserted in
4979 -- the new Ada Dispatch Table and this is a freezing action.
4981 -- Similarly, if this is an inherited operation whose parent is
4982 -- not frozen yet, it is not in the DT of the parent, and we
4983 -- generate an explicit freeze node for the inherited operation
4984 -- so it is properly inserted in the DT of the current type.
4986 declare
4987 Elmt : Elmt_Id;
4988 Subp : Entity_Id;
4990 begin
4991 Elmt := First_Elmt (Primitive_Operations (Typ));
4992 while Present (Elmt) loop
4993 Subp := Node (Elmt);
4995 if Present (Alias (Subp)) then
4996 if Is_CPP_Class (Etype (Typ)) then
4997 Set_Has_Delayed_Freeze (Subp);
4999 elsif Has_Delayed_Freeze (Alias (Subp))
5000 and then not Is_Frozen (Alias (Subp))
5001 then
5002 Set_Is_Frozen (Subp, False);
5003 Set_Has_Delayed_Freeze (Subp);
5004 end if;
5005 end if;
5007 Next_Elmt (Elmt);
5008 end loop;
5009 end;
5010 end if;
5012 -- Unfreeze momentarily the type to add the predefined primitives
5013 -- operations. The reason we unfreeze is so that these predefined
5014 -- operations will indeed end up as primitive operations (which
5015 -- must be before the freeze point).
5017 Set_Is_Frozen (Typ, False);
5019 -- Do not add the spec of predefined primitives in case of
5020 -- CPP tagged type derivations that have convention CPP.
5022 if Is_CPP_Class (Root_Type (Typ))
5023 and then Convention (Typ) = Convention_CPP
5024 then
5025 null;
5027 -- Do not add the spec of the predefined primitives if we are
5028 -- compiling under restriction No_Dispatching_Calls.
5030 elsif not Restriction_Active (No_Dispatching_Calls) then
5031 Make_Predefined_Primitive_Specs (Typ, Predef_List, Renamed_Eq);
5032 Insert_List_Before_And_Analyze (N, Predef_List);
5033 end if;
5035 -- Ada 2005 (AI-391): For a nonabstract null extension, create
5036 -- wrapper functions for each nonoverridden inherited function
5037 -- with a controlling result of the type. The wrapper for such
5038 -- a function returns an extension aggregate that invokes the
5039 -- parent function.
5041 if Ada_Version >= Ada_2005
5042 and then not Is_Abstract_Type (Typ)
5043 and then Is_Null_Extension (Typ)
5044 then
5045 Make_Controlling_Function_Wrappers
5046 (Typ, Wrapper_Decl_List, Wrapper_Body_List);
5047 Insert_List_Before_And_Analyze (N, Wrapper_Decl_List);
5048 end if;
5050 -- Ada 2005 (AI-251): For a nonabstract type extension, build
5051 -- null procedure declarations for each set of homographic null
5052 -- procedures that are inherited from interface types but not
5053 -- overridden. This is done to ensure that the dispatch table
5054 -- entry associated with such null primitives are properly filled.
5056 if Ada_Version >= Ada_2005
5057 and then Etype (Typ) /= Typ
5058 and then not Is_Abstract_Type (Typ)
5059 and then Has_Interfaces (Typ)
5060 then
5061 Insert_Actions (N, Make_Null_Procedure_Specs (Typ));
5062 end if;
5064 Set_Is_Frozen (Typ);
5066 if not Is_Derived_Type (Typ)
5067 or else Is_Tagged_Type (Etype (Typ))
5068 then
5069 Set_All_DT_Position (Typ);
5071 -- If this is a type derived from an untagged private type whose
5072 -- full view is tagged, the type is marked tagged for layout
5073 -- reasons, but it has no dispatch table.
5075 elsif Is_Derived_Type (Typ)
5076 and then Is_Private_Type (Etype (Typ))
5077 and then not Is_Tagged_Type (Etype (Typ))
5078 then
5079 return;
5080 end if;
5082 -- Create and decorate the tags. Suppress their creation when
5083 -- not Tagged_Type_Expansion because the dispatching mechanism is
5084 -- handled internally by the virtual target.
5086 if Tagged_Type_Expansion then
5087 Append_Freeze_Actions (Typ, Make_Tags (Typ));
5089 -- Generate dispatch table of locally defined tagged type.
5090 -- Dispatch tables of library level tagged types are built
5091 -- later (see Analyze_Declarations).
5093 if not Building_Static_DT (Typ) then
5094 Append_Freeze_Actions (Typ, Make_DT (Typ));
5095 end if;
5096 end if;
5098 -- If the type has unknown discriminants, propagate dispatching
5099 -- information to its underlying record view, which does not get
5100 -- its own dispatch table.
5102 if Is_Derived_Type (Typ)
5103 and then Has_Unknown_Discriminants (Typ)
5104 and then Present (Underlying_Record_View (Typ))
5105 then
5106 declare
5107 Rep : constant Entity_Id := Underlying_Record_View (Typ);
5108 begin
5109 Set_Access_Disp_Table
5110 (Rep, Access_Disp_Table (Typ));
5111 Set_Dispatch_Table_Wrappers
5112 (Rep, Dispatch_Table_Wrappers (Typ));
5113 Set_Direct_Primitive_Operations
5114 (Rep, Direct_Primitive_Operations (Typ));
5115 end;
5116 end if;
5118 -- Make sure that the primitives Initialize, Adjust and Finalize
5119 -- are Frozen before other TSS subprograms. We don't want them
5120 -- Frozen inside.
5122 if Is_Controlled (Typ) then
5123 if not Is_Limited_Type (Typ) then
5124 Append_Freeze_Actions (Typ,
5125 Freeze_Entity (Find_Prim_Op (Typ, Name_Adjust), Typ));
5126 end if;
5128 Append_Freeze_Actions (Typ,
5129 Freeze_Entity (Find_Prim_Op (Typ, Name_Initialize), Typ));
5131 Append_Freeze_Actions (Typ,
5132 Freeze_Entity (Find_Prim_Op (Typ, Name_Finalize), Typ));
5133 end if;
5135 -- Freeze rest of primitive operations. There is no need to handle
5136 -- the predefined primitives if we are compiling under restriction
5137 -- No_Dispatching_Calls.
5139 if not Restriction_Active (No_Dispatching_Calls) then
5140 Append_Freeze_Actions (Typ, Predefined_Primitive_Freeze (Typ));
5141 end if;
5142 end if;
5144 -- In the untagged case, ever since Ada 83 an equality function must
5145 -- be provided for variant records that are not unchecked unions.
5146 -- In Ada 2012 the equality function composes, and thus must be built
5147 -- explicitly just as for tagged records.
5149 elsif Has_Discriminants (Typ)
5150 and then not Is_Limited_Type (Typ)
5151 then
5152 declare
5153 Comps : constant Node_Id :=
5154 Component_List (Type_Definition (Typ_Decl));
5155 begin
5156 if Present (Comps)
5157 and then Present (Variant_Part (Comps))
5158 then
5159 Build_Variant_Record_Equality (Typ);
5160 end if;
5161 end;
5163 -- Otherwise create primitive equality operation (AI05-0123)
5165 -- This is done unconditionally to ensure that tools can be linked
5166 -- properly with user programs compiled with older language versions.
5167 -- In addition, this is needed because "=" composes for bounded strings
5168 -- in all language versions (see Exp_Ch4.Expand_Composite_Equality).
5170 elsif Comes_From_Source (Typ)
5171 and then Convention (Typ) = Convention_Ada
5172 and then not Is_Limited_Type (Typ)
5173 then
5174 Build_Untagged_Equality (Typ);
5175 end if;
5177 -- Before building the record initialization procedure, if we are
5178 -- dealing with a concurrent record value type, then we must go through
5179 -- the discriminants, exchanging discriminals between the concurrent
5180 -- type and the concurrent record value type. See the section "Handling
5181 -- of Discriminants" in the Einfo spec for details.
5183 if Is_Concurrent_Record_Type (Typ)
5184 and then Has_Discriminants (Typ)
5185 then
5186 declare
5187 Ctyp : constant Entity_Id :=
5188 Corresponding_Concurrent_Type (Typ);
5189 Conc_Discr : Entity_Id;
5190 Rec_Discr : Entity_Id;
5191 Temp : Entity_Id;
5193 begin
5194 Conc_Discr := First_Discriminant (Ctyp);
5195 Rec_Discr := First_Discriminant (Typ);
5196 while Present (Conc_Discr) loop
5197 Temp := Discriminal (Conc_Discr);
5198 Set_Discriminal (Conc_Discr, Discriminal (Rec_Discr));
5199 Set_Discriminal (Rec_Discr, Temp);
5201 Set_Discriminal_Link (Discriminal (Conc_Discr), Conc_Discr);
5202 Set_Discriminal_Link (Discriminal (Rec_Discr), Rec_Discr);
5204 Next_Discriminant (Conc_Discr);
5205 Next_Discriminant (Rec_Discr);
5206 end loop;
5207 end;
5208 end if;
5210 if Has_Controlled_Component (Typ) then
5211 Build_Controlling_Procs (Typ);
5212 end if;
5214 Adjust_Discriminants (Typ);
5216 -- Do not need init for interfaces on virtual targets since they're
5217 -- abstract.
5219 if Tagged_Type_Expansion or else not Is_Interface (Typ) then
5220 Build_Record_Init_Proc (Typ_Decl, Typ);
5221 end if;
5223 -- For tagged type that are not interfaces, build bodies of primitive
5224 -- operations. Note: do this after building the record initialization
5225 -- procedure, since the primitive operations may need the initialization
5226 -- routine. There is no need to add predefined primitives of interfaces
5227 -- because all their predefined primitives are abstract.
5229 if Is_Tagged_Type (Typ) and then not Is_Interface (Typ) then
5231 -- Do not add the body of predefined primitives in case of CPP tagged
5232 -- type derivations that have convention CPP.
5234 if Is_CPP_Class (Root_Type (Typ))
5235 and then Convention (Typ) = Convention_CPP
5236 then
5237 null;
5239 -- Do not add the body of the predefined primitives if we are
5240 -- compiling under restriction No_Dispatching_Calls or if we are
5241 -- compiling a CPP tagged type.
5243 elsif not Restriction_Active (No_Dispatching_Calls) then
5245 -- Create the body of TSS primitive Finalize_Address. This must
5246 -- be done before the bodies of all predefined primitives are
5247 -- created. If Typ is limited, Stream_Input and Stream_Read may
5248 -- produce build-in-place allocations and for those the expander
5249 -- needs Finalize_Address.
5251 Make_Finalize_Address_Body (Typ);
5252 Predef_List := Predefined_Primitive_Bodies (Typ, Renamed_Eq);
5253 Append_Freeze_Actions (Typ, Predef_List);
5254 end if;
5256 -- Ada 2005 (AI-391): If any wrappers were created for nonoverridden
5257 -- inherited functions, then add their bodies to the freeze actions.
5259 if Present (Wrapper_Body_List) then
5260 Append_Freeze_Actions (Typ, Wrapper_Body_List);
5261 end if;
5263 -- Create extra formals for the primitive operations of the type.
5264 -- This must be done before analyzing the body of the initialization
5265 -- procedure, because a self-referential type might call one of these
5266 -- primitives in the body of the init_proc itself.
5268 declare
5269 Elmt : Elmt_Id;
5270 Subp : Entity_Id;
5272 begin
5273 Elmt := First_Elmt (Primitive_Operations (Typ));
5274 while Present (Elmt) loop
5275 Subp := Node (Elmt);
5276 if not Has_Foreign_Convention (Subp)
5277 and then not Is_Predefined_Dispatching_Operation (Subp)
5278 then
5279 Create_Extra_Formals (Subp);
5280 end if;
5282 Next_Elmt (Elmt);
5283 end loop;
5284 end;
5285 end if;
5286 end Expand_Freeze_Record_Type;
5288 ------------------------------------
5289 -- Expand_N_Full_Type_Declaration --
5290 ------------------------------------
5292 procedure Expand_N_Full_Type_Declaration (N : Node_Id) is
5293 procedure Build_Master (Ptr_Typ : Entity_Id);
5294 -- Create the master associated with Ptr_Typ
5296 ------------------
5297 -- Build_Master --
5298 ------------------
5300 procedure Build_Master (Ptr_Typ : Entity_Id) is
5301 Desig_Typ : Entity_Id := Designated_Type (Ptr_Typ);
5303 begin
5304 -- If the designated type is an incomplete view coming from a
5305 -- limited-with'ed package, we need to use the nonlimited view in
5306 -- case it has tasks.
5308 if Ekind (Desig_Typ) in Incomplete_Kind
5309 and then Present (Non_Limited_View (Desig_Typ))
5310 then
5311 Desig_Typ := Non_Limited_View (Desig_Typ);
5312 end if;
5314 -- Anonymous access types are created for the components of the
5315 -- record parameter for an entry declaration. No master is created
5316 -- for such a type.
5318 if Comes_From_Source (N) and then Has_Task (Desig_Typ) then
5319 Build_Master_Entity (Ptr_Typ);
5320 Build_Master_Renaming (Ptr_Typ);
5322 -- Create a class-wide master because a Master_Id must be generated
5323 -- for access-to-limited-class-wide types whose root may be extended
5324 -- with task components.
5326 -- Note: This code covers access-to-limited-interfaces because they
5327 -- can be used to reference tasks implementing them.
5329 elsif Is_Limited_Class_Wide_Type (Desig_Typ)
5330 and then Tasking_Allowed
5331 then
5332 Build_Class_Wide_Master (Ptr_Typ);
5333 end if;
5334 end Build_Master;
5336 -- Local declarations
5338 Def_Id : constant Entity_Id := Defining_Identifier (N);
5339 B_Id : constant Entity_Id := Base_Type (Def_Id);
5340 FN : Node_Id;
5341 Par_Id : Entity_Id;
5343 -- Start of processing for Expand_N_Full_Type_Declaration
5345 begin
5346 if Is_Access_Type (Def_Id) then
5347 Build_Master (Def_Id);
5349 if Ekind (Def_Id) = E_Access_Protected_Subprogram_Type then
5350 Expand_Access_Protected_Subprogram_Type (N);
5351 end if;
5353 -- Array of anonymous access-to-task pointers
5355 elsif Ada_Version >= Ada_2005
5356 and then Is_Array_Type (Def_Id)
5357 and then Is_Access_Type (Component_Type (Def_Id))
5358 and then Ekind (Component_Type (Def_Id)) = E_Anonymous_Access_Type
5359 then
5360 Build_Master (Component_Type (Def_Id));
5362 elsif Has_Task (Def_Id) then
5363 Expand_Previous_Access_Type (Def_Id);
5365 -- Check the components of a record type or array of records for
5366 -- anonymous access-to-task pointers.
5368 elsif Ada_Version >= Ada_2005
5369 and then (Is_Record_Type (Def_Id)
5370 or else
5371 (Is_Array_Type (Def_Id)
5372 and then Is_Record_Type (Component_Type (Def_Id))))
5373 then
5374 declare
5375 Comp : Entity_Id;
5376 First : Boolean;
5377 M_Id : Entity_Id;
5378 Typ : Entity_Id;
5380 begin
5381 if Is_Array_Type (Def_Id) then
5382 Comp := First_Entity (Component_Type (Def_Id));
5383 else
5384 Comp := First_Entity (Def_Id);
5385 end if;
5387 -- Examine all components looking for anonymous access-to-task
5388 -- types.
5390 First := True;
5391 while Present (Comp) loop
5392 Typ := Etype (Comp);
5394 if Ekind (Typ) = E_Anonymous_Access_Type
5395 and then Has_Task (Available_View (Designated_Type (Typ)))
5396 and then No (Master_Id (Typ))
5397 then
5398 -- Ensure that the record or array type have a _master
5400 if First then
5401 Build_Master_Entity (Def_Id);
5402 Build_Master_Renaming (Typ);
5403 M_Id := Master_Id (Typ);
5405 First := False;
5407 -- Reuse the same master to service any additional types
5409 else
5410 Set_Master_Id (Typ, M_Id);
5411 end if;
5412 end if;
5414 Next_Entity (Comp);
5415 end loop;
5416 end;
5417 end if;
5419 Par_Id := Etype (B_Id);
5421 -- The parent type is private then we need to inherit any TSS operations
5422 -- from the full view.
5424 if Ekind (Par_Id) in Private_Kind
5425 and then Present (Full_View (Par_Id))
5426 then
5427 Par_Id := Base_Type (Full_View (Par_Id));
5428 end if;
5430 if Nkind (Type_Definition (Original_Node (N))) =
5431 N_Derived_Type_Definition
5432 and then not Is_Tagged_Type (Def_Id)
5433 and then Present (Freeze_Node (Par_Id))
5434 and then Present (TSS_Elist (Freeze_Node (Par_Id)))
5435 then
5436 Ensure_Freeze_Node (B_Id);
5437 FN := Freeze_Node (B_Id);
5439 if No (TSS_Elist (FN)) then
5440 Set_TSS_Elist (FN, New_Elmt_List);
5441 end if;
5443 declare
5444 T_E : constant Elist_Id := TSS_Elist (FN);
5445 Elmt : Elmt_Id;
5447 begin
5448 Elmt := First_Elmt (TSS_Elist (Freeze_Node (Par_Id)));
5449 while Present (Elmt) loop
5450 if Chars (Node (Elmt)) /= Name_uInit then
5451 Append_Elmt (Node (Elmt), T_E);
5452 end if;
5454 Next_Elmt (Elmt);
5455 end loop;
5457 -- If the derived type itself is private with a full view, then
5458 -- associate the full view with the inherited TSS_Elist as well.
5460 if Ekind (B_Id) in Private_Kind
5461 and then Present (Full_View (B_Id))
5462 then
5463 Ensure_Freeze_Node (Base_Type (Full_View (B_Id)));
5464 Set_TSS_Elist
5465 (Freeze_Node (Base_Type (Full_View (B_Id))), TSS_Elist (FN));
5466 end if;
5467 end;
5468 end if;
5469 end Expand_N_Full_Type_Declaration;
5471 ---------------------------------
5472 -- Expand_N_Object_Declaration --
5473 ---------------------------------
5475 procedure Expand_N_Object_Declaration (N : Node_Id) is
5476 Loc : constant Source_Ptr := Sloc (N);
5477 Def_Id : constant Entity_Id := Defining_Identifier (N);
5478 Expr : constant Node_Id := Expression (N);
5479 Obj_Def : constant Node_Id := Object_Definition (N);
5480 Typ : constant Entity_Id := Etype (Def_Id);
5481 Base_Typ : constant Entity_Id := Base_Type (Typ);
5482 Expr_Q : Node_Id;
5484 function Build_Equivalent_Aggregate return Boolean;
5485 -- If the object has a constrained discriminated type and no initial
5486 -- value, it may be possible to build an equivalent aggregate instead,
5487 -- and prevent an actual call to the initialization procedure.
5489 procedure Check_Large_Modular_Array;
5490 -- Check that the size of the array can be computed without overflow,
5491 -- and generate a Storage_Error otherwise. This is only relevant for
5492 -- array types whose index in a (mod 2**64) type, where wrap-around
5493 -- arithmetic might yield a meaningless value for the length of the
5494 -- array, or its corresponding attribute.
5496 procedure Default_Initialize_Object (After : Node_Id);
5497 -- Generate all default initialization actions for object Def_Id. Any
5498 -- new code is inserted after node After.
5500 function Rewrite_As_Renaming return Boolean;
5501 -- Indicate whether to rewrite a declaration with initialization into an
5502 -- object renaming declaration (see below).
5504 --------------------------------
5505 -- Build_Equivalent_Aggregate --
5506 --------------------------------
5508 function Build_Equivalent_Aggregate return Boolean is
5509 Aggr : Node_Id;
5510 Comp : Entity_Id;
5511 Discr : Elmt_Id;
5512 Full_Type : Entity_Id;
5514 begin
5515 Full_Type := Typ;
5517 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
5518 Full_Type := Full_View (Typ);
5519 end if;
5521 -- Only perform this transformation if Elaboration_Code is forbidden
5522 -- or undesirable, and if this is a global entity of a constrained
5523 -- record type.
5525 -- If Initialize_Scalars might be active this transformation cannot
5526 -- be performed either, because it will lead to different semantics
5527 -- or because elaboration code will in fact be created.
5529 if Ekind (Full_Type) /= E_Record_Subtype
5530 or else not Has_Discriminants (Full_Type)
5531 or else not Is_Constrained (Full_Type)
5532 or else Is_Controlled (Full_Type)
5533 or else Is_Limited_Type (Full_Type)
5534 or else not Restriction_Active (No_Initialize_Scalars)
5535 then
5536 return False;
5537 end if;
5539 if Ekind (Current_Scope) = E_Package
5540 and then
5541 (Restriction_Active (No_Elaboration_Code)
5542 or else Is_Preelaborated (Current_Scope))
5543 then
5544 -- Building a static aggregate is possible if the discriminants
5545 -- have static values and the other components have static
5546 -- defaults or none.
5548 Discr := First_Elmt (Discriminant_Constraint (Full_Type));
5549 while Present (Discr) loop
5550 if not Is_OK_Static_Expression (Node (Discr)) then
5551 return False;
5552 end if;
5554 Next_Elmt (Discr);
5555 end loop;
5557 -- Check that initialized components are OK, and that non-
5558 -- initialized components do not require a call to their own
5559 -- initialization procedure.
5561 Comp := First_Component (Full_Type);
5562 while Present (Comp) loop
5563 if Ekind (Comp) = E_Component
5564 and then Present (Expression (Parent (Comp)))
5565 and then
5566 not Is_OK_Static_Expression (Expression (Parent (Comp)))
5567 then
5568 return False;
5570 elsif Has_Non_Null_Base_Init_Proc (Etype (Comp)) then
5571 return False;
5573 end if;
5575 Next_Component (Comp);
5576 end loop;
5578 -- Everything is static, assemble the aggregate, discriminant
5579 -- values first.
5581 Aggr :=
5582 Make_Aggregate (Loc,
5583 Expressions => New_List,
5584 Component_Associations => New_List);
5586 Discr := First_Elmt (Discriminant_Constraint (Full_Type));
5587 while Present (Discr) loop
5588 Append_To (Expressions (Aggr), New_Copy (Node (Discr)));
5589 Next_Elmt (Discr);
5590 end loop;
5592 -- Now collect values of initialized components
5594 Comp := First_Component (Full_Type);
5595 while Present (Comp) loop
5596 if Ekind (Comp) = E_Component
5597 and then Present (Expression (Parent (Comp)))
5598 then
5599 Append_To (Component_Associations (Aggr),
5600 Make_Component_Association (Loc,
5601 Choices => New_List (New_Occurrence_Of (Comp, Loc)),
5602 Expression => New_Copy_Tree
5603 (Expression (Parent (Comp)))));
5604 end if;
5606 Next_Component (Comp);
5607 end loop;
5609 -- Finally, box-initialize remaining components
5611 Append_To (Component_Associations (Aggr),
5612 Make_Component_Association (Loc,
5613 Choices => New_List (Make_Others_Choice (Loc)),
5614 Expression => Empty));
5615 Set_Box_Present (Last (Component_Associations (Aggr)));
5616 Set_Expression (N, Aggr);
5618 if Typ /= Full_Type then
5619 Analyze_And_Resolve (Aggr, Full_View (Base_Type (Full_Type)));
5620 Rewrite (Aggr, Unchecked_Convert_To (Typ, Aggr));
5621 Analyze_And_Resolve (Aggr, Typ);
5622 else
5623 Analyze_And_Resolve (Aggr, Full_Type);
5624 end if;
5626 return True;
5628 else
5629 return False;
5630 end if;
5631 end Build_Equivalent_Aggregate;
5633 -------------------------------
5634 -- Check_Large_Modular_Array --
5635 -------------------------------
5637 procedure Check_Large_Modular_Array is
5638 Index_Typ : Entity_Id;
5640 begin
5641 if Is_Array_Type (Typ)
5642 and then Is_Modular_Integer_Type (Etype (First_Index (Typ)))
5643 then
5644 -- To prevent arithmetic overflow with large values, we raise
5645 -- Storage_Error under the following guard:
5647 -- (Arr'Last / 2 - Arr'First / 2) > (2 ** 30)
5649 -- This takes care of the boundary case, but it is preferable to
5650 -- use a smaller limit, because even on 64-bit architectures an
5651 -- array of more than 2 ** 30 bytes is likely to raise
5652 -- Storage_Error.
5654 Index_Typ := Etype (First_Index (Typ));
5656 if RM_Size (Index_Typ) = RM_Size (Standard_Long_Long_Integer) then
5657 Insert_Action (N,
5658 Make_Raise_Storage_Error (Loc,
5659 Condition =>
5660 Make_Op_Ge (Loc,
5661 Left_Opnd =>
5662 Make_Op_Subtract (Loc,
5663 Left_Opnd =>
5664 Make_Op_Divide (Loc,
5665 Left_Opnd =>
5666 Make_Attribute_Reference (Loc,
5667 Prefix =>
5668 New_Occurrence_Of (Typ, Loc),
5669 Attribute_Name => Name_Last),
5670 Right_Opnd =>
5671 Make_Integer_Literal (Loc, Uint_2)),
5672 Right_Opnd =>
5673 Make_Op_Divide (Loc,
5674 Left_Opnd =>
5675 Make_Attribute_Reference (Loc,
5676 Prefix =>
5677 New_Occurrence_Of (Typ, Loc),
5678 Attribute_Name => Name_First),
5679 Right_Opnd =>
5680 Make_Integer_Literal (Loc, Uint_2))),
5681 Right_Opnd =>
5682 Make_Integer_Literal (Loc, (Uint_2 ** 30))),
5683 Reason => SE_Object_Too_Large));
5684 end if;
5685 end if;
5686 end Check_Large_Modular_Array;
5688 -------------------------------
5689 -- Default_Initialize_Object --
5690 -------------------------------
5692 procedure Default_Initialize_Object (After : Node_Id) is
5693 function New_Object_Reference return Node_Id;
5694 -- Return a new reference to Def_Id with attributes Assignment_OK and
5695 -- Must_Not_Freeze already set.
5697 --------------------------
5698 -- New_Object_Reference --
5699 --------------------------
5701 function New_Object_Reference return Node_Id is
5702 Obj_Ref : constant Node_Id := New_Occurrence_Of (Def_Id, Loc);
5704 begin
5705 -- The call to the type init proc or [Deep_]Finalize must not
5706 -- freeze the related object as the call is internally generated.
5707 -- This way legal rep clauses that apply to the object will not be
5708 -- flagged. Note that the initialization call may be removed if
5709 -- pragma Import is encountered or moved to the freeze actions of
5710 -- the object because of an address clause.
5712 Set_Assignment_OK (Obj_Ref);
5713 Set_Must_Not_Freeze (Obj_Ref);
5715 return Obj_Ref;
5716 end New_Object_Reference;
5718 -- Local variables
5720 Exceptions_OK : constant Boolean :=
5721 not Restriction_Active (No_Exception_Propagation);
5723 Aggr_Init : Node_Id;
5724 Comp_Init : List_Id := No_List;
5725 Fin_Call : Node_Id;
5726 Init_Stmts : List_Id := No_List;
5727 Obj_Init : Node_Id := Empty;
5728 Obj_Ref : Node_Id;
5730 -- Start of processing for Default_Initialize_Object
5732 begin
5733 -- Default initialization is suppressed for objects that are already
5734 -- known to be imported (i.e. whose declaration specifies the Import
5735 -- aspect). Note that for objects with a pragma Import, we generate
5736 -- initialization here, and then remove it downstream when processing
5737 -- the pragma. It is also suppressed for variables for which a pragma
5738 -- Suppress_Initialization has been explicitly given
5740 if Is_Imported (Def_Id) or else Suppress_Initialization (Def_Id) then
5741 return;
5743 -- Nothing to do if the object being initialized is of a task type
5744 -- and restriction No_Tasking is in effect, because this is a direct
5745 -- violation of the restriction.
5747 elsif Is_Task_Type (Base_Typ)
5748 and then Restriction_Active (No_Tasking)
5749 then
5750 return;
5751 end if;
5753 -- The expansion performed by this routine is as follows:
5755 -- begin
5756 -- Abort_Defer;
5757 -- Type_Init_Proc (Obj);
5759 -- begin
5760 -- [Deep_]Initialize (Obj);
5762 -- exception
5763 -- when others =>
5764 -- [Deep_]Finalize (Obj, Self => False);
5765 -- raise;
5766 -- end;
5767 -- at end
5768 -- Abort_Undefer_Direct;
5769 -- end;
5771 -- Initialize the components of the object
5773 if Has_Non_Null_Base_Init_Proc (Typ)
5774 and then not No_Initialization (N)
5775 and then not Initialization_Suppressed (Typ)
5776 then
5777 -- Do not initialize the components if No_Default_Initialization
5778 -- applies as the actual restriction check will occur later
5779 -- when the object is frozen as it is not known yet whether the
5780 -- object is imported or not.
5782 if not Restriction_Active (No_Default_Initialization) then
5784 -- If the values of the components are compile-time known, use
5785 -- their prebuilt aggregate form directly.
5787 Aggr_Init := Static_Initialization (Base_Init_Proc (Typ));
5789 if Present (Aggr_Init) then
5790 Set_Expression
5791 (N, New_Copy_Tree (Aggr_Init, New_Scope => Current_Scope));
5793 -- If type has discriminants, try to build an equivalent
5794 -- aggregate using discriminant values from the declaration.
5795 -- This is a useful optimization, in particular if restriction
5796 -- No_Elaboration_Code is active.
5798 elsif Build_Equivalent_Aggregate then
5799 null;
5801 -- Otherwise invoke the type init proc, generate:
5802 -- Type_Init_Proc (Obj);
5804 else
5805 Obj_Ref := New_Object_Reference;
5807 if Comes_From_Source (Def_Id) then
5808 Initialization_Warning (Obj_Ref);
5809 end if;
5811 Comp_Init := Build_Initialization_Call (Loc, Obj_Ref, Typ);
5812 end if;
5813 end if;
5815 -- Provide a default value if the object needs simple initialization
5816 -- and does not already have an initial value. A generated temporary
5817 -- does not require initialization because it will be assigned later.
5819 elsif Needs_Simple_Initialization
5820 (Typ, Initialize_Scalars
5821 and then No (Following_Address_Clause (N)))
5822 and then not Is_Internal (Def_Id)
5823 and then not Has_Init_Expression (N)
5824 then
5825 Set_No_Initialization (N, False);
5826 Set_Expression (N, Get_Simple_Init_Val (Typ, N, Esize (Def_Id)));
5827 Analyze_And_Resolve (Expression (N), Typ);
5828 end if;
5830 -- Initialize the object, generate:
5831 -- [Deep_]Initialize (Obj);
5833 if Needs_Finalization (Typ) and then not No_Initialization (N) then
5834 Obj_Init :=
5835 Make_Init_Call
5836 (Obj_Ref => New_Occurrence_Of (Def_Id, Loc),
5837 Typ => Typ);
5838 end if;
5840 -- Build a special finalization block when both the object and its
5841 -- controlled components are to be initialized. The block finalizes
5842 -- the components if the object initialization fails. Generate:
5844 -- begin
5845 -- <Obj_Init>
5847 -- exception
5848 -- when others =>
5849 -- <Fin_Call>
5850 -- raise;
5851 -- end;
5853 if Has_Controlled_Component (Typ)
5854 and then Present (Comp_Init)
5855 and then Present (Obj_Init)
5856 and then Exceptions_OK
5857 then
5858 Init_Stmts := Comp_Init;
5860 Fin_Call :=
5861 Make_Final_Call
5862 (Obj_Ref => New_Object_Reference,
5863 Typ => Typ,
5864 Skip_Self => True);
5866 if Present (Fin_Call) then
5868 -- Do not emit warnings related to the elaboration order when a
5869 -- controlled object is declared before the body of Finalize is
5870 -- seen.
5872 Set_No_Elaboration_Check (Fin_Call);
5874 Append_To (Init_Stmts,
5875 Make_Block_Statement (Loc,
5876 Declarations => No_List,
5878 Handled_Statement_Sequence =>
5879 Make_Handled_Sequence_Of_Statements (Loc,
5880 Statements => New_List (Obj_Init),
5882 Exception_Handlers => New_List (
5883 Make_Exception_Handler (Loc,
5884 Exception_Choices => New_List (
5885 Make_Others_Choice (Loc)),
5887 Statements => New_List (
5888 Fin_Call,
5889 Make_Raise_Statement (Loc)))))));
5890 end if;
5892 -- Otherwise finalization is not required, the initialization calls
5893 -- are passed to the abort block building circuitry, generate:
5895 -- Type_Init_Proc (Obj);
5896 -- [Deep_]Initialize (Obj);
5898 else
5899 if Present (Comp_Init) then
5900 Init_Stmts := Comp_Init;
5901 end if;
5903 if Present (Obj_Init) then
5904 if No (Init_Stmts) then
5905 Init_Stmts := New_List;
5906 end if;
5908 Append_To (Init_Stmts, Obj_Init);
5909 end if;
5910 end if;
5912 -- Build an abort block to protect the initialization calls
5914 if Abort_Allowed
5915 and then Present (Comp_Init)
5916 and then Present (Obj_Init)
5917 then
5918 -- Generate:
5919 -- Abort_Defer;
5921 Prepend_To (Init_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
5923 -- When exceptions are propagated, abort deferral must take place
5924 -- in the presence of initialization or finalization exceptions.
5925 -- Generate:
5927 -- begin
5928 -- Abort_Defer;
5929 -- <Init_Stmts>
5930 -- at end
5931 -- Abort_Undefer_Direct;
5932 -- end;
5934 if Exceptions_OK then
5935 Init_Stmts := New_List (
5936 Build_Abort_Undefer_Block (Loc,
5937 Stmts => Init_Stmts,
5938 Context => N));
5940 -- Otherwise exceptions are not propagated. Generate:
5942 -- Abort_Defer;
5943 -- <Init_Stmts>
5944 -- Abort_Undefer;
5946 else
5947 Append_To (Init_Stmts,
5948 Build_Runtime_Call (Loc, RE_Abort_Undefer));
5949 end if;
5950 end if;
5952 -- Insert the whole initialization sequence into the tree. If the
5953 -- object has a delayed freeze, as will be the case when it has
5954 -- aspect specifications, the initialization sequence is part of
5955 -- the freeze actions.
5957 if Present (Init_Stmts) then
5958 if Has_Delayed_Freeze (Def_Id) then
5959 Append_Freeze_Actions (Def_Id, Init_Stmts);
5960 else
5961 Insert_Actions_After (After, Init_Stmts);
5962 end if;
5963 end if;
5964 end Default_Initialize_Object;
5966 -------------------------
5967 -- Rewrite_As_Renaming --
5968 -------------------------
5970 function Rewrite_As_Renaming return Boolean is
5971 begin
5972 -- If the object declaration appears in the form
5974 -- Obj : Ctrl_Typ := Func (...);
5976 -- where Ctrl_Typ is controlled but not immutably limited type, then
5977 -- the expansion of the function call should use a dereference of the
5978 -- result to reference the value on the secondary stack.
5980 -- Obj : Ctrl_Typ renames Func (...).all;
5982 -- As a result, the call avoids an extra copy. This an optimization,
5983 -- but it is required for passing ACATS tests in some cases where it
5984 -- would otherwise make two copies. The RM allows removing redunant
5985 -- Adjust/Finalize calls, but does not allow insertion of extra ones.
5987 -- This part is disabled for now, because it breaks GPS builds
5989 return (False -- ???
5990 and then Nkind (Expr_Q) = N_Explicit_Dereference
5991 and then not Comes_From_Source (Expr_Q)
5992 and then Nkind (Original_Node (Expr_Q)) = N_Function_Call
5993 and then Nkind (Object_Definition (N)) in N_Has_Entity
5994 and then (Needs_Finalization (Entity (Object_Definition (N)))))
5996 -- If the initializing expression is for a variable with attribute
5997 -- OK_To_Rename set, then transform:
5999 -- Obj : Typ := Expr;
6001 -- into
6003 -- Obj : Typ renames Expr;
6005 -- provided that Obj is not aliased. The aliased case has to be
6006 -- excluded in general because Expr will not be aliased in
6007 -- general.
6009 or else
6010 (not Aliased_Present (N)
6011 and then Is_Entity_Name (Expr_Q)
6012 and then Ekind (Entity (Expr_Q)) = E_Variable
6013 and then OK_To_Rename (Entity (Expr_Q))
6014 and then Is_Entity_Name (Obj_Def));
6015 end Rewrite_As_Renaming;
6017 -- Local variables
6019 Next_N : constant Node_Id := Next (N);
6021 Adj_Call : Node_Id;
6022 Id_Ref : Node_Id;
6023 Tag_Assign : Node_Id;
6025 Init_After : Node_Id := N;
6026 -- Node after which the initialization actions are to be inserted. This
6027 -- is normally N, except for the case of a shared passive variable, in
6028 -- which case the init proc call must be inserted only after the bodies
6029 -- of the shared variable procedures have been seen.
6031 -- Start of processing for Expand_N_Object_Declaration
6033 begin
6034 -- Don't do anything for deferred constants. All proper actions will be
6035 -- expanded during the full declaration.
6037 if No (Expr) and Constant_Present (N) then
6038 return;
6039 end if;
6041 -- The type of the object cannot be abstract. This is diagnosed at the
6042 -- point the object is frozen, which happens after the declaration is
6043 -- fully expanded, so simply return now.
6045 if Is_Abstract_Type (Typ) then
6046 return;
6047 end if;
6049 -- First we do special processing for objects of a tagged type where
6050 -- this is the point at which the type is frozen. The creation of the
6051 -- dispatch table and the initialization procedure have to be deferred
6052 -- to this point, since we reference previously declared primitive
6053 -- subprograms.
6055 -- Force construction of dispatch tables of library level tagged types
6057 if Tagged_Type_Expansion
6058 and then Static_Dispatch_Tables
6059 and then Is_Library_Level_Entity (Def_Id)
6060 and then Is_Library_Level_Tagged_Type (Base_Typ)
6061 and then Ekind_In (Base_Typ, E_Record_Type,
6062 E_Protected_Type,
6063 E_Task_Type)
6064 and then not Has_Dispatch_Table (Base_Typ)
6065 then
6066 declare
6067 New_Nodes : List_Id := No_List;
6069 begin
6070 if Is_Concurrent_Type (Base_Typ) then
6071 New_Nodes := Make_DT (Corresponding_Record_Type (Base_Typ), N);
6072 else
6073 New_Nodes := Make_DT (Base_Typ, N);
6074 end if;
6076 if not Is_Empty_List (New_Nodes) then
6077 Insert_List_Before (N, New_Nodes);
6078 end if;
6079 end;
6080 end if;
6082 -- Make shared memory routines for shared passive variable
6084 if Is_Shared_Passive (Def_Id) then
6085 Init_After := Make_Shared_Var_Procs (N);
6086 end if;
6088 -- If tasks being declared, make sure we have an activation chain
6089 -- defined for the tasks (has no effect if we already have one), and
6090 -- also that a Master variable is established and that the appropriate
6091 -- enclosing construct is established as a task master.
6093 if Has_Task (Typ) then
6094 Build_Activation_Chain_Entity (N);
6095 Build_Master_Entity (Def_Id);
6096 end if;
6098 Check_Large_Modular_Array;
6100 -- Default initialization required, and no expression present
6102 if No (Expr) then
6104 -- If we have a type with a variant part, the initialization proc
6105 -- will contain implicit tests of the discriminant values, which
6106 -- counts as a violation of the restriction No_Implicit_Conditionals.
6108 if Has_Variant_Part (Typ) then
6109 declare
6110 Msg : Boolean;
6112 begin
6113 Check_Restriction (Msg, No_Implicit_Conditionals, Obj_Def);
6115 if Msg then
6116 Error_Msg_N
6117 ("\initialization of variant record tests discriminants",
6118 Obj_Def);
6119 return;
6120 end if;
6121 end;
6122 end if;
6124 -- For the default initialization case, if we have a private type
6125 -- with invariants, and invariant checks are enabled, then insert an
6126 -- invariant check after the object declaration. Note that it is OK
6127 -- to clobber the object with an invalid value since if the exception
6128 -- is raised, then the object will go out of scope. In the case where
6129 -- an array object is initialized with an aggregate, the expression
6130 -- is removed. Check flag Has_Init_Expression to avoid generating a
6131 -- junk invariant check and flag No_Initialization to avoid checking
6132 -- an uninitialized object such as a compiler temporary used for an
6133 -- aggregate.
6135 if Has_Invariants (Base_Typ)
6136 and then Present (Invariant_Procedure (Base_Typ))
6137 and then not Has_Init_Expression (N)
6138 and then not No_Initialization (N)
6139 then
6140 -- If entity has an address clause or aspect, make invariant
6141 -- call into a freeze action for the explicit freeze node for
6142 -- object. Otherwise insert invariant check after declaration.
6144 if Present (Following_Address_Clause (N))
6145 or else Has_Aspect (Def_Id, Aspect_Address)
6146 then
6147 Ensure_Freeze_Node (Def_Id);
6148 Set_Has_Delayed_Freeze (Def_Id);
6149 Set_Is_Frozen (Def_Id, False);
6151 if not Partial_View_Has_Unknown_Discr (Typ) then
6152 Append_Freeze_Action (Def_Id,
6153 Make_Invariant_Call (New_Occurrence_Of (Def_Id, Loc)));
6154 end if;
6156 elsif not Partial_View_Has_Unknown_Discr (Typ) then
6157 Insert_After (N,
6158 Make_Invariant_Call (New_Occurrence_Of (Def_Id, Loc)));
6159 end if;
6160 end if;
6162 Default_Initialize_Object (Init_After);
6164 -- Generate attribute for Persistent_BSS if needed
6166 if Persistent_BSS_Mode
6167 and then Comes_From_Source (N)
6168 and then Is_Potentially_Persistent_Type (Typ)
6169 and then not Has_Init_Expression (N)
6170 and then Is_Library_Level_Entity (Def_Id)
6171 then
6172 declare
6173 Prag : Node_Id;
6174 begin
6175 Prag :=
6176 Make_Linker_Section_Pragma
6177 (Def_Id, Sloc (N), ".persistent.bss");
6178 Insert_After (N, Prag);
6179 Analyze (Prag);
6180 end;
6181 end if;
6183 -- If access type, then we know it is null if not initialized
6185 if Is_Access_Type (Typ) then
6186 Set_Is_Known_Null (Def_Id);
6187 end if;
6189 -- Explicit initialization present
6191 else
6192 -- Obtain actual expression from qualified expression
6194 if Nkind (Expr) = N_Qualified_Expression then
6195 Expr_Q := Expression (Expr);
6196 else
6197 Expr_Q := Expr;
6198 end if;
6200 -- When we have the appropriate type of aggregate in the expression
6201 -- (it has been determined during analysis of the aggregate by
6202 -- setting the delay flag), let's perform in place assignment and
6203 -- thus avoid creating a temporary.
6205 if Is_Delayed_Aggregate (Expr_Q) then
6206 Convert_Aggr_In_Object_Decl (N);
6208 -- Ada 2005 (AI-318-02): If the initialization expression is a call
6209 -- to a build-in-place function, then access to the declared object
6210 -- must be passed to the function. Currently we limit such functions
6211 -- to those with constrained limited result subtypes, but eventually
6212 -- plan to expand the allowed forms of functions that are treated as
6213 -- build-in-place.
6215 elsif Ada_Version >= Ada_2005
6216 and then Is_Build_In_Place_Function_Call (Expr_Q)
6217 then
6218 Make_Build_In_Place_Call_In_Object_Declaration (N, Expr_Q);
6220 -- The previous call expands the expression initializing the
6221 -- built-in-place object into further code that will be analyzed
6222 -- later. No further expansion needed here.
6224 return;
6226 -- Ada 2005 (AI-251): Rewrite the expression that initializes a
6227 -- class-wide interface object to ensure that we copy the full
6228 -- object, unless we are targetting a VM where interfaces are handled
6229 -- by VM itself. Note that if the root type of Typ is an ancestor of
6230 -- Expr's type, both types share the same dispatch table and there is
6231 -- no need to displace the pointer.
6233 elsif Is_Interface (Typ)
6235 -- Avoid never-ending recursion because if Equivalent_Type is set
6236 -- then we've done it already and must not do it again.
6238 and then not
6239 (Nkind (Obj_Def) = N_Identifier
6240 and then Present (Equivalent_Type (Entity (Obj_Def))))
6241 then
6242 pragma Assert (Is_Class_Wide_Type (Typ));
6244 -- If the object is a return object of an inherently limited type,
6245 -- which implies build-in-place treatment, bypass the special
6246 -- treatment of class-wide interface initialization below. In this
6247 -- case, the expansion of the return statement will take care of
6248 -- creating the object (via allocator) and initializing it.
6250 if Is_Return_Object (Def_Id) and then Is_Limited_View (Typ) then
6251 null;
6253 elsif Tagged_Type_Expansion then
6254 declare
6255 Iface : constant Entity_Id := Root_Type (Typ);
6256 Expr_N : Node_Id := Expr;
6257 Expr_Typ : Entity_Id;
6258 New_Expr : Node_Id;
6259 Obj_Id : Entity_Id;
6260 Tag_Comp : Node_Id;
6262 begin
6263 -- If the original node of the expression was a conversion
6264 -- to this specific class-wide interface type then restore
6265 -- the original node because we must copy the object before
6266 -- displacing the pointer to reference the secondary tag
6267 -- component. This code must be kept synchronized with the
6268 -- expansion done by routine Expand_Interface_Conversion
6270 if not Comes_From_Source (Expr_N)
6271 and then Nkind (Expr_N) = N_Explicit_Dereference
6272 and then Nkind (Original_Node (Expr_N)) = N_Type_Conversion
6273 and then Etype (Original_Node (Expr_N)) = Typ
6274 then
6275 Rewrite (Expr_N, Original_Node (Expression (N)));
6276 end if;
6278 -- Avoid expansion of redundant interface conversion
6280 if Is_Interface (Etype (Expr_N))
6281 and then Nkind (Expr_N) = N_Type_Conversion
6282 and then Etype (Expr_N) = Typ
6283 then
6284 Expr_N := Expression (Expr_N);
6285 Set_Expression (N, Expr_N);
6286 end if;
6288 Obj_Id := Make_Temporary (Loc, 'D', Expr_N);
6289 Expr_Typ := Base_Type (Etype (Expr_N));
6291 if Is_Class_Wide_Type (Expr_Typ) then
6292 Expr_Typ := Root_Type (Expr_Typ);
6293 end if;
6295 -- Replace
6296 -- CW : I'Class := Obj;
6297 -- by
6298 -- Tmp : T := Obj;
6299 -- type Ityp is not null access I'Class;
6300 -- CW : I'Class renames Ityp (Tmp.I_Tag'Address).all;
6302 if Comes_From_Source (Expr_N)
6303 and then Nkind (Expr_N) = N_Identifier
6304 and then not Is_Interface (Expr_Typ)
6305 and then Interface_Present_In_Ancestor (Expr_Typ, Typ)
6306 and then (Expr_Typ = Etype (Expr_Typ)
6307 or else not
6308 Is_Variable_Size_Record (Etype (Expr_Typ)))
6309 then
6310 -- Copy the object
6312 Insert_Action (N,
6313 Make_Object_Declaration (Loc,
6314 Defining_Identifier => Obj_Id,
6315 Object_Definition =>
6316 New_Occurrence_Of (Expr_Typ, Loc),
6317 Expression => Relocate_Node (Expr_N)));
6319 -- Statically reference the tag associated with the
6320 -- interface
6322 Tag_Comp :=
6323 Make_Selected_Component (Loc,
6324 Prefix => New_Occurrence_Of (Obj_Id, Loc),
6325 Selector_Name =>
6326 New_Occurrence_Of
6327 (Find_Interface_Tag (Expr_Typ, Iface), Loc));
6329 -- Replace
6330 -- IW : I'Class := Obj;
6331 -- by
6332 -- type Equiv_Record is record ... end record;
6333 -- implicit subtype CW is <Class_Wide_Subtype>;
6334 -- Tmp : CW := CW!(Obj);
6335 -- type Ityp is not null access I'Class;
6336 -- IW : I'Class renames
6337 -- Ityp!(Displace (Temp'Address, I'Tag)).all;
6339 else
6340 -- Generate the equivalent record type and update the
6341 -- subtype indication to reference it.
6343 Expand_Subtype_From_Expr
6344 (N => N,
6345 Unc_Type => Typ,
6346 Subtype_Indic => Obj_Def,
6347 Exp => Expr_N);
6349 if not Is_Interface (Etype (Expr_N)) then
6350 New_Expr := Relocate_Node (Expr_N);
6352 -- For interface types we use 'Address which displaces
6353 -- the pointer to the base of the object (if required)
6355 else
6356 New_Expr :=
6357 Unchecked_Convert_To (Etype (Obj_Def),
6358 Make_Explicit_Dereference (Loc,
6359 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6360 Make_Attribute_Reference (Loc,
6361 Prefix => Relocate_Node (Expr_N),
6362 Attribute_Name => Name_Address))));
6363 end if;
6365 -- Copy the object
6367 if not Is_Limited_Record (Expr_Typ) then
6368 Insert_Action (N,
6369 Make_Object_Declaration (Loc,
6370 Defining_Identifier => Obj_Id,
6371 Object_Definition =>
6372 New_Occurrence_Of (Etype (Obj_Def), Loc),
6373 Expression => New_Expr));
6375 -- Rename limited type object since they cannot be copied
6376 -- This case occurs when the initialization expression
6377 -- has been previously expanded into a temporary object.
6379 else pragma Assert (not Comes_From_Source (Expr_Q));
6380 Insert_Action (N,
6381 Make_Object_Renaming_Declaration (Loc,
6382 Defining_Identifier => Obj_Id,
6383 Subtype_Mark =>
6384 New_Occurrence_Of (Etype (Obj_Def), Loc),
6385 Name =>
6386 Unchecked_Convert_To
6387 (Etype (Obj_Def), New_Expr)));
6388 end if;
6390 -- Dynamically reference the tag associated with the
6391 -- interface.
6393 Tag_Comp :=
6394 Make_Function_Call (Loc,
6395 Name => New_Occurrence_Of (RTE (RE_Displace), Loc),
6396 Parameter_Associations => New_List (
6397 Make_Attribute_Reference (Loc,
6398 Prefix => New_Occurrence_Of (Obj_Id, Loc),
6399 Attribute_Name => Name_Address),
6400 New_Occurrence_Of
6401 (Node (First_Elmt (Access_Disp_Table (Iface))),
6402 Loc)));
6403 end if;
6405 Rewrite (N,
6406 Make_Object_Renaming_Declaration (Loc,
6407 Defining_Identifier => Make_Temporary (Loc, 'D'),
6408 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
6409 Name =>
6410 Convert_Tag_To_Interface (Typ, Tag_Comp)));
6412 -- If the original entity comes from source, then mark the
6413 -- new entity as needing debug information, even though it's
6414 -- defined by a generated renaming that does not come from
6415 -- source, so that Materialize_Entity will be set on the
6416 -- entity when Debug_Renaming_Declaration is called during
6417 -- analysis.
6419 if Comes_From_Source (Def_Id) then
6420 Set_Debug_Info_Needed (Defining_Identifier (N));
6421 end if;
6423 Analyze (N, Suppress => All_Checks);
6425 -- Replace internal identifier of rewritten node by the
6426 -- identifier found in the sources. We also have to exchange
6427 -- entities containing their defining identifiers to ensure
6428 -- the correct replacement of the object declaration by this
6429 -- object renaming declaration because these identifiers
6430 -- were previously added by Enter_Name to the current scope.
6431 -- We must preserve the homonym chain of the source entity
6432 -- as well. We must also preserve the kind of the entity,
6433 -- which may be a constant. Preserve entity chain because
6434 -- itypes may have been generated already, and the full
6435 -- chain must be preserved for final freezing. Finally,
6436 -- preserve Comes_From_Source setting, so that debugging
6437 -- and cross-referencing information is properly kept, and
6438 -- preserve source location, to prevent spurious errors when
6439 -- entities are declared (they must have their own Sloc).
6441 declare
6442 New_Id : constant Entity_Id := Defining_Identifier (N);
6443 Next_Temp : constant Entity_Id := Next_Entity (New_Id);
6444 S_Flag : constant Boolean :=
6445 Comes_From_Source (Def_Id);
6447 begin
6448 Set_Next_Entity (New_Id, Next_Entity (Def_Id));
6449 Set_Next_Entity (Def_Id, Next_Temp);
6451 Set_Chars (Defining_Identifier (N), Chars (Def_Id));
6452 Set_Homonym (Defining_Identifier (N), Homonym (Def_Id));
6453 Set_Ekind (Defining_Identifier (N), Ekind (Def_Id));
6454 Set_Sloc (Defining_Identifier (N), Sloc (Def_Id));
6456 Set_Comes_From_Source (Def_Id, False);
6457 Exchange_Entities (Defining_Identifier (N), Def_Id);
6458 Set_Comes_From_Source (Def_Id, S_Flag);
6459 end;
6460 end;
6461 end if;
6463 return;
6465 -- Common case of explicit object initialization
6467 else
6468 -- In most cases, we must check that the initial value meets any
6469 -- constraint imposed by the declared type. However, there is one
6470 -- very important exception to this rule. If the entity has an
6471 -- unconstrained nominal subtype, then it acquired its constraints
6472 -- from the expression in the first place, and not only does this
6473 -- mean that the constraint check is not needed, but an attempt to
6474 -- perform the constraint check can cause order of elaboration
6475 -- problems.
6477 if not Is_Constr_Subt_For_U_Nominal (Typ) then
6479 -- If this is an allocator for an aggregate that has been
6480 -- allocated in place, delay checks until assignments are
6481 -- made, because the discriminants are not initialized.
6483 if Nkind (Expr) = N_Allocator and then No_Initialization (Expr)
6484 then
6485 null;
6487 -- Otherwise apply a constraint check now if no prev error
6489 elsif Nkind (Expr) /= N_Error then
6490 Apply_Constraint_Check (Expr, Typ);
6492 -- Deal with possible range check
6494 if Do_Range_Check (Expr) then
6496 -- If assignment checks are suppressed, turn off flag
6498 if Suppress_Assignment_Checks (N) then
6499 Set_Do_Range_Check (Expr, False);
6501 -- Otherwise generate the range check
6503 else
6504 Generate_Range_Check
6505 (Expr, Typ, CE_Range_Check_Failed);
6506 end if;
6507 end if;
6508 end if;
6509 end if;
6511 -- If the type is controlled and not inherently limited, then
6512 -- the target is adjusted after the copy and attached to the
6513 -- finalization list. However, no adjustment is done in the case
6514 -- where the object was initialized by a call to a function whose
6515 -- result is built in place, since no copy occurred. (Eventually
6516 -- we plan to support in-place function results for some cases
6517 -- of nonlimited types. ???) Similarly, no adjustment is required
6518 -- if we are going to rewrite the object declaration into a
6519 -- renaming declaration.
6521 if Needs_Finalization (Typ)
6522 and then not Is_Limited_View (Typ)
6523 and then not Rewrite_As_Renaming
6524 then
6525 Adj_Call :=
6526 Make_Adjust_Call (
6527 Obj_Ref => New_Occurrence_Of (Def_Id, Loc),
6528 Typ => Base_Typ);
6530 -- Guard against a missing [Deep_]Adjust when the base type
6531 -- was not properly frozen.
6533 if Present (Adj_Call) then
6534 Insert_Action_After (Init_After, Adj_Call);
6535 end if;
6536 end if;
6538 -- For tagged types, when an init value is given, the tag has to
6539 -- be re-initialized separately in order to avoid the propagation
6540 -- of a wrong tag coming from a view conversion unless the type
6541 -- is class wide (in this case the tag comes from the init value).
6542 -- Suppress the tag assignment when not Tagged_Type_Expansion
6543 -- because tags are represented implicitly in objects. Ditto for
6544 -- types that are CPP_CLASS, and for initializations that are
6545 -- aggregates, because they have to have the right tag.
6547 -- The re-assignment of the tag has to be done even if the object
6548 -- is a constant. The assignment must be analyzed after the
6549 -- declaration. If an address clause follows, this is handled as
6550 -- part of the freeze actions for the object, otherwise insert
6551 -- tag assignment here.
6553 Tag_Assign := Make_Tag_Assignment (N);
6555 if Present (Tag_Assign) then
6556 if Present (Following_Address_Clause (N)) then
6557 Ensure_Freeze_Node (Def_Id);
6559 else
6560 Insert_Action_After (Init_After, Tag_Assign);
6561 end if;
6563 -- Handle C++ constructor calls. Note that we do not check that
6564 -- Typ is a tagged type since the equivalent Ada type of a C++
6565 -- class that has no virtual methods is an untagged limited
6566 -- record type.
6568 elsif Is_CPP_Constructor_Call (Expr) then
6570 -- The call to the initialization procedure does NOT freeze the
6571 -- object being initialized.
6573 Id_Ref := New_Occurrence_Of (Def_Id, Loc);
6574 Set_Must_Not_Freeze (Id_Ref);
6575 Set_Assignment_OK (Id_Ref);
6577 Insert_Actions_After (Init_After,
6578 Build_Initialization_Call (Loc, Id_Ref, Typ,
6579 Constructor_Ref => Expr));
6581 -- We remove here the original call to the constructor
6582 -- to avoid its management in the backend
6584 Set_Expression (N, Empty);
6585 return;
6587 -- Handle initialization of limited tagged types
6589 elsif Is_Tagged_Type (Typ)
6590 and then Is_Class_Wide_Type (Typ)
6591 and then Is_Limited_Record (Typ)
6592 and then not Is_Limited_Interface (Typ)
6593 then
6594 -- Given that the type is limited we cannot perform a copy. If
6595 -- Expr_Q is the reference to a variable we mark the variable
6596 -- as OK_To_Rename to expand this declaration into a renaming
6597 -- declaration (see bellow).
6599 if Is_Entity_Name (Expr_Q) then
6600 Set_OK_To_Rename (Entity (Expr_Q));
6602 -- If we cannot convert the expression into a renaming we must
6603 -- consider it an internal error because the backend does not
6604 -- have support to handle it.
6606 else
6607 pragma Assert (False);
6608 raise Program_Error;
6609 end if;
6611 -- For discrete types, set the Is_Known_Valid flag if the
6612 -- initializing value is known to be valid. Only do this for
6613 -- source assignments, since otherwise we can end up turning
6614 -- on the known valid flag prematurely from inserted code.
6616 elsif Comes_From_Source (N)
6617 and then Is_Discrete_Type (Typ)
6618 and then Expr_Known_Valid (Expr)
6619 then
6620 Set_Is_Known_Valid (Def_Id);
6622 elsif Is_Access_Type (Typ) then
6624 -- For access types set the Is_Known_Non_Null flag if the
6625 -- initializing value is known to be non-null. We can also set
6626 -- Can_Never_Be_Null if this is a constant.
6628 if Known_Non_Null (Expr) then
6629 Set_Is_Known_Non_Null (Def_Id, True);
6631 if Constant_Present (N) then
6632 Set_Can_Never_Be_Null (Def_Id);
6633 end if;
6634 end if;
6635 end if;
6637 -- If validity checking on copies, validate initial expression.
6638 -- But skip this if declaration is for a generic type, since it
6639 -- makes no sense to validate generic types. Not clear if this
6640 -- can happen for legal programs, but it definitely can arise
6641 -- from previous instantiation errors.
6643 if Validity_Checks_On
6644 and then Comes_From_Source (N)
6645 and then Validity_Check_Copies
6646 and then not Is_Generic_Type (Etype (Def_Id))
6647 then
6648 Ensure_Valid (Expr);
6649 Set_Is_Known_Valid (Def_Id);
6650 end if;
6651 end if;
6653 -- Cases where the back end cannot handle the initialization directly
6654 -- In such cases, we expand an assignment that will be appropriately
6655 -- handled by Expand_N_Assignment_Statement.
6657 -- The exclusion of the unconstrained case is wrong, but for now it
6658 -- is too much trouble ???
6660 if (Is_Possibly_Unaligned_Slice (Expr)
6661 or else (Is_Possibly_Unaligned_Object (Expr)
6662 and then not Represented_As_Scalar (Etype (Expr))))
6663 and then not (Is_Array_Type (Etype (Expr))
6664 and then not Is_Constrained (Etype (Expr)))
6665 then
6666 declare
6667 Stat : constant Node_Id :=
6668 Make_Assignment_Statement (Loc,
6669 Name => New_Occurrence_Of (Def_Id, Loc),
6670 Expression => Relocate_Node (Expr));
6671 begin
6672 Set_Expression (N, Empty);
6673 Set_No_Initialization (N);
6674 Set_Assignment_OK (Name (Stat));
6675 Set_No_Ctrl_Actions (Stat);
6676 Insert_After_And_Analyze (Init_After, Stat);
6677 end;
6678 end if;
6679 end if;
6681 if Nkind (Obj_Def) = N_Access_Definition
6682 and then not Is_Local_Anonymous_Access (Etype (Def_Id))
6683 then
6684 -- An Ada 2012 stand-alone object of an anonymous access type
6686 declare
6687 Loc : constant Source_Ptr := Sloc (N);
6689 Level : constant Entity_Id :=
6690 Make_Defining_Identifier (Sloc (N),
6691 Chars =>
6692 New_External_Name (Chars (Def_Id), Suffix => "L"));
6694 Level_Expr : Node_Id;
6695 Level_Decl : Node_Id;
6697 begin
6698 Set_Ekind (Level, Ekind (Def_Id));
6699 Set_Etype (Level, Standard_Natural);
6700 Set_Scope (Level, Scope (Def_Id));
6702 if No (Expr) then
6704 -- Set accessibility level of null
6706 Level_Expr :=
6707 Make_Integer_Literal (Loc, Scope_Depth (Standard_Standard));
6709 else
6710 Level_Expr := Dynamic_Accessibility_Level (Expr);
6711 end if;
6713 Level_Decl :=
6714 Make_Object_Declaration (Loc,
6715 Defining_Identifier => Level,
6716 Object_Definition =>
6717 New_Occurrence_Of (Standard_Natural, Loc),
6718 Expression => Level_Expr,
6719 Constant_Present => Constant_Present (N),
6720 Has_Init_Expression => True);
6722 Insert_Action_After (Init_After, Level_Decl);
6724 Set_Extra_Accessibility (Def_Id, Level);
6725 end;
6726 end if;
6728 -- If the object is default initialized and its type is subject to
6729 -- pragma Default_Initial_Condition, add a runtime check to verify
6730 -- the assumption of the pragma (SPARK RM 7.3.3). Generate:
6732 -- <Base_Typ>DIC (<Base_Typ> (Def_Id));
6734 -- Note that the check is generated for source objects only
6736 if Comes_From_Source (Def_Id)
6737 and then Has_DIC (Typ)
6738 and then Present (DIC_Procedure (Typ))
6739 and then not Has_Init_Expression (N)
6740 then
6741 declare
6742 DIC_Call : constant Node_Id := Build_DIC_Call (Loc, Def_Id, Typ);
6744 begin
6745 if Present (Next_N) then
6746 Insert_Before_And_Analyze (Next_N, DIC_Call);
6748 -- The object declaration is the last node in a declarative or a
6749 -- statement list.
6751 else
6752 Append_To (List_Containing (N), DIC_Call);
6753 Analyze (DIC_Call);
6754 end if;
6755 end;
6756 end if;
6758 -- Final transformation - turn the object declaration into a renaming
6759 -- if appropriate. If this is the completion of a deferred constant
6760 -- declaration, then this transformation generates what would be
6761 -- illegal code if written by hand, but that's OK.
6763 if Present (Expr) then
6764 if Rewrite_As_Renaming then
6765 Rewrite (N,
6766 Make_Object_Renaming_Declaration (Loc,
6767 Defining_Identifier => Defining_Identifier (N),
6768 Subtype_Mark => Obj_Def,
6769 Name => Expr_Q));
6771 -- We do not analyze this renaming declaration, because all its
6772 -- components have already been analyzed, and if we were to go
6773 -- ahead and analyze it, we would in effect be trying to generate
6774 -- another declaration of X, which won't do.
6776 Set_Renamed_Object (Defining_Identifier (N), Expr_Q);
6777 Set_Analyzed (N);
6779 -- We do need to deal with debug issues for this renaming
6781 -- First, if entity comes from source, then mark it as needing
6782 -- debug information, even though it is defined by a generated
6783 -- renaming that does not come from source.
6785 if Comes_From_Source (Defining_Identifier (N)) then
6786 Set_Debug_Info_Needed (Defining_Identifier (N));
6787 end if;
6789 -- Now call the routine to generate debug info for the renaming
6791 declare
6792 Decl : constant Node_Id := Debug_Renaming_Declaration (N);
6793 begin
6794 if Present (Decl) then
6795 Insert_Action (N, Decl);
6796 end if;
6797 end;
6798 end if;
6799 end if;
6801 -- Exception on library entity not available
6803 exception
6804 when RE_Not_Available =>
6805 return;
6806 end Expand_N_Object_Declaration;
6808 ---------------------------------
6809 -- Expand_N_Subtype_Indication --
6810 ---------------------------------
6812 -- Add a check on the range of the subtype. The static case is partially
6813 -- duplicated by Process_Range_Expr_In_Decl in Sem_Ch3, but we still need
6814 -- to check here for the static case in order to avoid generating
6815 -- extraneous expanded code. Also deal with validity checking.
6817 procedure Expand_N_Subtype_Indication (N : Node_Id) is
6818 Ran : constant Node_Id := Range_Expression (Constraint (N));
6819 Typ : constant Entity_Id := Entity (Subtype_Mark (N));
6821 begin
6822 if Nkind (Constraint (N)) = N_Range_Constraint then
6823 Validity_Check_Range (Range_Expression (Constraint (N)));
6824 end if;
6826 if Nkind_In (Parent (N), N_Constrained_Array_Definition, N_Slice) then
6827 Apply_Range_Check (Ran, Typ);
6828 end if;
6829 end Expand_N_Subtype_Indication;
6831 ---------------------------
6832 -- Expand_N_Variant_Part --
6833 ---------------------------
6835 -- Note: this procedure no longer has any effect. It used to be that we
6836 -- would replace the choices in the last variant by a when others, and
6837 -- also expanded static predicates in variant choices here, but both of
6838 -- those activities were being done too early, since we can't check the
6839 -- choices until the statically predicated subtypes are frozen, which can
6840 -- happen as late as the free point of the record, and we can't change the
6841 -- last choice to an others before checking the choices, which is now done
6842 -- at the freeze point of the record.
6844 procedure Expand_N_Variant_Part (N : Node_Id) is
6845 begin
6846 null;
6847 end Expand_N_Variant_Part;
6849 ---------------------------------
6850 -- Expand_Previous_Access_Type --
6851 ---------------------------------
6853 procedure Expand_Previous_Access_Type (Def_Id : Entity_Id) is
6854 Ptr_Typ : Entity_Id;
6856 begin
6857 -- Find all access types in the current scope whose designated type is
6858 -- Def_Id and build master renamings for them.
6860 Ptr_Typ := First_Entity (Current_Scope);
6861 while Present (Ptr_Typ) loop
6862 if Is_Access_Type (Ptr_Typ)
6863 and then Designated_Type (Ptr_Typ) = Def_Id
6864 and then No (Master_Id (Ptr_Typ))
6865 then
6866 -- Ensure that the designated type has a master
6868 Build_Master_Entity (Def_Id);
6870 -- Private and incomplete types complicate the insertion of master
6871 -- renamings because the access type may precede the full view of
6872 -- the designated type. For this reason, the master renamings are
6873 -- inserted relative to the designated type.
6875 Build_Master_Renaming (Ptr_Typ, Ins_Nod => Parent (Def_Id));
6876 end if;
6878 Next_Entity (Ptr_Typ);
6879 end loop;
6880 end Expand_Previous_Access_Type;
6882 -----------------------------
6883 -- Expand_Record_Extension --
6884 -----------------------------
6886 -- Add a field _parent at the beginning of the record extension. This is
6887 -- used to implement inheritance. Here are some examples of expansion:
6889 -- 1. no discriminants
6890 -- type T2 is new T1 with null record;
6891 -- gives
6892 -- type T2 is new T1 with record
6893 -- _Parent : T1;
6894 -- end record;
6896 -- 2. renamed discriminants
6897 -- type T2 (B, C : Int) is new T1 (A => B) with record
6898 -- _Parent : T1 (A => B);
6899 -- D : Int;
6900 -- end;
6902 -- 3. inherited discriminants
6903 -- type T2 is new T1 with record -- discriminant A inherited
6904 -- _Parent : T1 (A);
6905 -- D : Int;
6906 -- end;
6908 procedure Expand_Record_Extension (T : Entity_Id; Def : Node_Id) is
6909 Indic : constant Node_Id := Subtype_Indication (Def);
6910 Loc : constant Source_Ptr := Sloc (Def);
6911 Rec_Ext_Part : Node_Id := Record_Extension_Part (Def);
6912 Par_Subtype : Entity_Id;
6913 Comp_List : Node_Id;
6914 Comp_Decl : Node_Id;
6915 Parent_N : Node_Id;
6916 D : Entity_Id;
6917 List_Constr : constant List_Id := New_List;
6919 begin
6920 -- Expand_Record_Extension is called directly from the semantics, so
6921 -- we must check to see whether expansion is active before proceeding,
6922 -- because this affects the visibility of selected components in bodies
6923 -- of instances.
6925 if not Expander_Active then
6926 return;
6927 end if;
6929 -- This may be a derivation of an untagged private type whose full
6930 -- view is tagged, in which case the Derived_Type_Definition has no
6931 -- extension part. Build an empty one now.
6933 if No (Rec_Ext_Part) then
6934 Rec_Ext_Part :=
6935 Make_Record_Definition (Loc,
6936 End_Label => Empty,
6937 Component_List => Empty,
6938 Null_Present => True);
6940 Set_Record_Extension_Part (Def, Rec_Ext_Part);
6941 Mark_Rewrite_Insertion (Rec_Ext_Part);
6942 end if;
6944 Comp_List := Component_List (Rec_Ext_Part);
6946 Parent_N := Make_Defining_Identifier (Loc, Name_uParent);
6948 -- If the derived type inherits its discriminants the type of the
6949 -- _parent field must be constrained by the inherited discriminants
6951 if Has_Discriminants (T)
6952 and then Nkind (Indic) /= N_Subtype_Indication
6953 and then not Is_Constrained (Entity (Indic))
6954 then
6955 D := First_Discriminant (T);
6956 while Present (D) loop
6957 Append_To (List_Constr, New_Occurrence_Of (D, Loc));
6958 Next_Discriminant (D);
6959 end loop;
6961 Par_Subtype :=
6962 Process_Subtype (
6963 Make_Subtype_Indication (Loc,
6964 Subtype_Mark => New_Occurrence_Of (Entity (Indic), Loc),
6965 Constraint =>
6966 Make_Index_Or_Discriminant_Constraint (Loc,
6967 Constraints => List_Constr)),
6968 Def);
6970 -- Otherwise the original subtype_indication is just what is needed
6972 else
6973 Par_Subtype := Process_Subtype (New_Copy_Tree (Indic), Def);
6974 end if;
6976 Set_Parent_Subtype (T, Par_Subtype);
6978 Comp_Decl :=
6979 Make_Component_Declaration (Loc,
6980 Defining_Identifier => Parent_N,
6981 Component_Definition =>
6982 Make_Component_Definition (Loc,
6983 Aliased_Present => False,
6984 Subtype_Indication => New_Occurrence_Of (Par_Subtype, Loc)));
6986 if Null_Present (Rec_Ext_Part) then
6987 Set_Component_List (Rec_Ext_Part,
6988 Make_Component_List (Loc,
6989 Component_Items => New_List (Comp_Decl),
6990 Variant_Part => Empty,
6991 Null_Present => False));
6992 Set_Null_Present (Rec_Ext_Part, False);
6994 elsif Null_Present (Comp_List)
6995 or else Is_Empty_List (Component_Items (Comp_List))
6996 then
6997 Set_Component_Items (Comp_List, New_List (Comp_Decl));
6998 Set_Null_Present (Comp_List, False);
7000 else
7001 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
7002 end if;
7004 Analyze (Comp_Decl);
7005 end Expand_Record_Extension;
7007 ------------------------
7008 -- Expand_Tagged_Root --
7009 ------------------------
7011 procedure Expand_Tagged_Root (T : Entity_Id) is
7012 Def : constant Node_Id := Type_Definition (Parent (T));
7013 Comp_List : Node_Id;
7014 Comp_Decl : Node_Id;
7015 Sloc_N : Source_Ptr;
7017 begin
7018 if Null_Present (Def) then
7019 Set_Component_List (Def,
7020 Make_Component_List (Sloc (Def),
7021 Component_Items => Empty_List,
7022 Variant_Part => Empty,
7023 Null_Present => True));
7024 end if;
7026 Comp_List := Component_List (Def);
7028 if Null_Present (Comp_List)
7029 or else Is_Empty_List (Component_Items (Comp_List))
7030 then
7031 Sloc_N := Sloc (Comp_List);
7032 else
7033 Sloc_N := Sloc (First (Component_Items (Comp_List)));
7034 end if;
7036 Comp_Decl :=
7037 Make_Component_Declaration (Sloc_N,
7038 Defining_Identifier => First_Tag_Component (T),
7039 Component_Definition =>
7040 Make_Component_Definition (Sloc_N,
7041 Aliased_Present => False,
7042 Subtype_Indication => New_Occurrence_Of (RTE (RE_Tag), Sloc_N)));
7044 if Null_Present (Comp_List)
7045 or else Is_Empty_List (Component_Items (Comp_List))
7046 then
7047 Set_Component_Items (Comp_List, New_List (Comp_Decl));
7048 Set_Null_Present (Comp_List, False);
7050 else
7051 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
7052 end if;
7054 -- We don't Analyze the whole expansion because the tag component has
7055 -- already been analyzed previously. Here we just insure that the tree
7056 -- is coherent with the semantic decoration
7058 Find_Type (Subtype_Indication (Component_Definition (Comp_Decl)));
7060 exception
7061 when RE_Not_Available =>
7062 return;
7063 end Expand_Tagged_Root;
7065 ------------------------------
7066 -- Freeze_Stream_Operations --
7067 ------------------------------
7069 procedure Freeze_Stream_Operations (N : Node_Id; Typ : Entity_Id) is
7070 Names : constant array (1 .. 4) of TSS_Name_Type :=
7071 (TSS_Stream_Input,
7072 TSS_Stream_Output,
7073 TSS_Stream_Read,
7074 TSS_Stream_Write);
7075 Stream_Op : Entity_Id;
7077 begin
7078 -- Primitive operations of tagged types are frozen when the dispatch
7079 -- table is constructed.
7081 if not Comes_From_Source (Typ) or else Is_Tagged_Type (Typ) then
7082 return;
7083 end if;
7085 for J in Names'Range loop
7086 Stream_Op := TSS (Typ, Names (J));
7088 if Present (Stream_Op)
7089 and then Is_Subprogram (Stream_Op)
7090 and then Nkind (Unit_Declaration_Node (Stream_Op)) =
7091 N_Subprogram_Declaration
7092 and then not Is_Frozen (Stream_Op)
7093 then
7094 Append_Freeze_Actions (Typ, Freeze_Entity (Stream_Op, N));
7095 end if;
7096 end loop;
7097 end Freeze_Stream_Operations;
7099 -----------------
7100 -- Freeze_Type --
7101 -----------------
7103 -- Full type declarations are expanded at the point at which the type is
7104 -- frozen. The formal N is the Freeze_Node for the type. Any statements or
7105 -- declarations generated by the freezing (e.g. the procedure generated
7106 -- for initialization) are chained in the Actions field list of the freeze
7107 -- node using Append_Freeze_Actions.
7109 -- WARNING: This routine manages Ghost regions. Return statements must be
7110 -- replaced by gotos which jump to the end of the routine and restore the
7111 -- Ghost mode.
7113 function Freeze_Type (N : Node_Id) return Boolean is
7114 procedure Process_RACW_Types (Typ : Entity_Id);
7115 -- Validate and generate stubs for all RACW types associated with type
7116 -- Typ.
7118 procedure Process_Pending_Access_Types (Typ : Entity_Id);
7119 -- Associate type Typ's Finalize_Address primitive with the finalization
7120 -- masters of pending access-to-Typ types.
7122 ------------------------
7123 -- Process_RACW_Types --
7124 ------------------------
7126 procedure Process_RACW_Types (Typ : Entity_Id) is
7127 List : constant Elist_Id := Access_Types_To_Process (N);
7128 E : Elmt_Id;
7129 Seen : Boolean := False;
7131 begin
7132 if Present (List) then
7133 E := First_Elmt (List);
7134 while Present (E) loop
7135 if Is_Remote_Access_To_Class_Wide_Type (Node (E)) then
7136 Validate_RACW_Primitives (Node (E));
7137 Seen := True;
7138 end if;
7140 Next_Elmt (E);
7141 end loop;
7142 end if;
7144 -- If there are RACWs designating this type, make stubs now
7146 if Seen then
7147 Remote_Types_Tagged_Full_View_Encountered (Typ);
7148 end if;
7149 end Process_RACW_Types;
7151 ----------------------------------
7152 -- Process_Pending_Access_Types --
7153 ----------------------------------
7155 procedure Process_Pending_Access_Types (Typ : Entity_Id) is
7156 E : Elmt_Id;
7158 begin
7159 -- Finalize_Address is not generated in CodePeer mode because the
7160 -- body contains address arithmetic. This processing is disabled.
7162 if CodePeer_Mode then
7163 null;
7165 -- Certain itypes are generated for contexts that cannot allocate
7166 -- objects and should not set primitive Finalize_Address.
7168 elsif Is_Itype (Typ)
7169 and then Nkind (Associated_Node_For_Itype (Typ)) =
7170 N_Explicit_Dereference
7171 then
7172 null;
7174 -- When an access type is declared after the incomplete view of a
7175 -- Taft-amendment type, the access type is considered pending in
7176 -- case the full view of the Taft-amendment type is controlled. If
7177 -- this is indeed the case, associate the Finalize_Address routine
7178 -- of the full view with the finalization masters of all pending
7179 -- access types. This scenario applies to anonymous access types as
7180 -- well.
7182 elsif Needs_Finalization (Typ)
7183 and then Present (Pending_Access_Types (Typ))
7184 then
7185 E := First_Elmt (Pending_Access_Types (Typ));
7186 while Present (E) loop
7188 -- Generate:
7189 -- Set_Finalize_Address
7190 -- (Ptr_Typ, <Typ>FD'Unrestricted_Access);
7192 Append_Freeze_Action (Typ,
7193 Make_Set_Finalize_Address_Call
7194 (Loc => Sloc (N),
7195 Ptr_Typ => Node (E)));
7197 Next_Elmt (E);
7198 end loop;
7199 end if;
7200 end Process_Pending_Access_Types;
7202 -- Local variables
7204 Def_Id : constant Entity_Id := Entity (N);
7206 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
7207 -- Save the Ghost mode to restore on exit
7209 Result : Boolean := False;
7211 -- Start of processing for Freeze_Type
7213 begin
7214 -- The type being frozen may be subject to pragma Ghost. Set the mode
7215 -- now to ensure that any nodes generated during freezing are properly
7216 -- marked as Ghost.
7218 Set_Ghost_Mode (Def_Id);
7220 -- Process any remote access-to-class-wide types designating the type
7221 -- being frozen.
7223 Process_RACW_Types (Def_Id);
7225 -- Freeze processing for record types
7227 if Is_Record_Type (Def_Id) then
7228 if Ekind (Def_Id) = E_Record_Type then
7229 Expand_Freeze_Record_Type (N);
7230 elsif Is_Class_Wide_Type (Def_Id) then
7231 Expand_Freeze_Class_Wide_Type (N);
7232 end if;
7234 -- Freeze processing for array types
7236 elsif Is_Array_Type (Def_Id) then
7237 Expand_Freeze_Array_Type (N);
7239 -- Freeze processing for access types
7241 -- For pool-specific access types, find out the pool object used for
7242 -- this type, needs actual expansion of it in some cases. Here are the
7243 -- different cases :
7245 -- 1. Rep Clause "for Def_Id'Storage_Size use 0;"
7246 -- ---> don't use any storage pool
7248 -- 2. Rep Clause : for Def_Id'Storage_Size use Expr.
7249 -- Expand:
7250 -- Def_Id__Pool : Stack_Bounded_Pool (Expr, DT'Size, DT'Alignment);
7252 -- 3. Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
7253 -- ---> Storage Pool is the specified one
7255 -- See GNAT Pool packages in the Run-Time for more details
7257 elsif Ekind_In (Def_Id, E_Access_Type, E_General_Access_Type) then
7258 declare
7259 Loc : constant Source_Ptr := Sloc (N);
7260 Desig_Type : constant Entity_Id := Designated_Type (Def_Id);
7262 Freeze_Action_Typ : Entity_Id;
7263 Pool_Object : Entity_Id;
7265 begin
7266 -- Case 1
7268 -- Rep Clause "for Def_Id'Storage_Size use 0;"
7269 -- ---> don't use any storage pool
7271 if No_Pool_Assigned (Def_Id) then
7272 null;
7274 -- Case 2
7276 -- Rep Clause : for Def_Id'Storage_Size use Expr.
7277 -- ---> Expand:
7278 -- Def_Id__Pool : Stack_Bounded_Pool
7279 -- (Expr, DT'Size, DT'Alignment);
7281 elsif Has_Storage_Size_Clause (Def_Id) then
7282 declare
7283 DT_Align : Node_Id;
7284 DT_Size : Node_Id;
7286 begin
7287 -- For unconstrained composite types we give a size of zero
7288 -- so that the pool knows that it needs a special algorithm
7289 -- for variable size object allocation.
7291 if Is_Composite_Type (Desig_Type)
7292 and then not Is_Constrained (Desig_Type)
7293 then
7294 DT_Size := Make_Integer_Literal (Loc, 0);
7295 DT_Align := Make_Integer_Literal (Loc, Maximum_Alignment);
7297 else
7298 DT_Size :=
7299 Make_Attribute_Reference (Loc,
7300 Prefix => New_Occurrence_Of (Desig_Type, Loc),
7301 Attribute_Name => Name_Max_Size_In_Storage_Elements);
7303 DT_Align :=
7304 Make_Attribute_Reference (Loc,
7305 Prefix => New_Occurrence_Of (Desig_Type, Loc),
7306 Attribute_Name => Name_Alignment);
7307 end if;
7309 Pool_Object :=
7310 Make_Defining_Identifier (Loc,
7311 Chars => New_External_Name (Chars (Def_Id), 'P'));
7313 -- We put the code associated with the pools in the entity
7314 -- that has the later freeze node, usually the access type
7315 -- but it can also be the designated_type; because the pool
7316 -- code requires both those types to be frozen
7318 if Is_Frozen (Desig_Type)
7319 and then (No (Freeze_Node (Desig_Type))
7320 or else Analyzed (Freeze_Node (Desig_Type)))
7321 then
7322 Freeze_Action_Typ := Def_Id;
7324 -- A Taft amendment type cannot get the freeze actions
7325 -- since the full view is not there.
7327 elsif Is_Incomplete_Or_Private_Type (Desig_Type)
7328 and then No (Full_View (Desig_Type))
7329 then
7330 Freeze_Action_Typ := Def_Id;
7332 else
7333 Freeze_Action_Typ := Desig_Type;
7334 end if;
7336 Append_Freeze_Action (Freeze_Action_Typ,
7337 Make_Object_Declaration (Loc,
7338 Defining_Identifier => Pool_Object,
7339 Object_Definition =>
7340 Make_Subtype_Indication (Loc,
7341 Subtype_Mark =>
7342 New_Occurrence_Of
7343 (RTE (RE_Stack_Bounded_Pool), Loc),
7345 Constraint =>
7346 Make_Index_Or_Discriminant_Constraint (Loc,
7347 Constraints => New_List (
7349 -- First discriminant is the Pool Size
7351 New_Occurrence_Of (
7352 Storage_Size_Variable (Def_Id), Loc),
7354 -- Second discriminant is the element size
7356 DT_Size,
7358 -- Third discriminant is the alignment
7360 DT_Align)))));
7361 end;
7363 Set_Associated_Storage_Pool (Def_Id, Pool_Object);
7365 -- Case 3
7367 -- Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
7368 -- ---> Storage Pool is the specified one
7370 -- When compiling in Ada 2012 mode, ensure that the accessibility
7371 -- level of the subpool access type is not deeper than that of the
7372 -- pool_with_subpools.
7374 elsif Ada_Version >= Ada_2012
7375 and then Present (Associated_Storage_Pool (Def_Id))
7377 -- Omit this check for the case of a configurable run-time that
7378 -- does not provide package System.Storage_Pools.Subpools.
7380 and then RTE_Available (RE_Root_Storage_Pool_With_Subpools)
7381 then
7382 declare
7383 Loc : constant Source_Ptr := Sloc (Def_Id);
7384 Pool : constant Entity_Id :=
7385 Associated_Storage_Pool (Def_Id);
7386 RSPWS : constant Entity_Id :=
7387 RTE (RE_Root_Storage_Pool_With_Subpools);
7389 begin
7390 -- It is known that the accessibility level of the access
7391 -- type is deeper than that of the pool.
7393 if Type_Access_Level (Def_Id) > Object_Access_Level (Pool)
7394 and then not Accessibility_Checks_Suppressed (Def_Id)
7395 and then not Accessibility_Checks_Suppressed (Pool)
7396 then
7397 -- Static case: the pool is known to be a descendant of
7398 -- Root_Storage_Pool_With_Subpools.
7400 if Is_Ancestor (RSPWS, Etype (Pool)) then
7401 Error_Msg_N
7402 ("??subpool access type has deeper accessibility "
7403 & "level than pool", Def_Id);
7405 Append_Freeze_Action (Def_Id,
7406 Make_Raise_Program_Error (Loc,
7407 Reason => PE_Accessibility_Check_Failed));
7409 -- Dynamic case: when the pool is of a class-wide type,
7410 -- it may or may not support subpools depending on the
7411 -- path of derivation. Generate:
7413 -- if Def_Id in RSPWS'Class then
7414 -- raise Program_Error;
7415 -- end if;
7417 elsif Is_Class_Wide_Type (Etype (Pool)) then
7418 Append_Freeze_Action (Def_Id,
7419 Make_If_Statement (Loc,
7420 Condition =>
7421 Make_In (Loc,
7422 Left_Opnd => New_Occurrence_Of (Pool, Loc),
7423 Right_Opnd =>
7424 New_Occurrence_Of
7425 (Class_Wide_Type (RSPWS), Loc)),
7427 Then_Statements => New_List (
7428 Make_Raise_Program_Error (Loc,
7429 Reason => PE_Accessibility_Check_Failed))));
7430 end if;
7431 end if;
7432 end;
7433 end if;
7435 -- For access-to-controlled types (including class-wide types and
7436 -- Taft-amendment types, which potentially have controlled
7437 -- components), expand the list controller object that will store
7438 -- the dynamically allocated objects. Don't do this transformation
7439 -- for expander-generated access types, but do it for types that
7440 -- are the full view of types derived from other private types.
7441 -- Also suppress the list controller in the case of a designated
7442 -- type with convention Java, since this is used when binding to
7443 -- Java API specs, where there's no equivalent of a finalization
7444 -- list and we don't want to pull in the finalization support if
7445 -- not needed.
7447 if not Comes_From_Source (Def_Id)
7448 and then not Has_Private_Declaration (Def_Id)
7449 then
7450 null;
7452 -- An exception is made for types defined in the run-time because
7453 -- Ada.Tags.Tag itself is such a type and cannot afford this
7454 -- unnecessary overhead that would generates a loop in the
7455 -- expansion scheme. Another exception is if Restrictions
7456 -- (No_Finalization) is active, since then we know nothing is
7457 -- controlled.
7459 elsif Restriction_Active (No_Finalization)
7460 or else In_Runtime (Def_Id)
7461 then
7462 null;
7464 -- Create a finalization master for an access-to-controlled type
7465 -- or an access-to-incomplete type. It is assumed that the full
7466 -- view will be controlled.
7468 elsif Needs_Finalization (Desig_Type)
7469 or else (Is_Incomplete_Type (Desig_Type)
7470 and then No (Full_View (Desig_Type)))
7471 then
7472 Build_Finalization_Master (Def_Id);
7474 -- Create a finalization master when the designated type contains
7475 -- a private component. It is assumed that the full view will be
7476 -- controlled.
7478 elsif Has_Private_Component (Desig_Type) then
7479 Build_Finalization_Master
7480 (Typ => Def_Id,
7481 For_Private => True,
7482 Context_Scope => Scope (Def_Id),
7483 Insertion_Node => Declaration_Node (Desig_Type));
7484 end if;
7485 end;
7487 -- Freeze processing for enumeration types
7489 elsif Ekind (Def_Id) = E_Enumeration_Type then
7491 -- We only have something to do if we have a non-standard
7492 -- representation (i.e. at least one literal whose pos value
7493 -- is not the same as its representation)
7495 if Has_Non_Standard_Rep (Def_Id) then
7496 Expand_Freeze_Enumeration_Type (N);
7497 end if;
7499 -- Private types that are completed by a derivation from a private
7500 -- type have an internally generated full view, that needs to be
7501 -- frozen. This must be done explicitly because the two views share
7502 -- the freeze node, and the underlying full view is not visible when
7503 -- the freeze node is analyzed.
7505 elsif Is_Private_Type (Def_Id)
7506 and then Is_Derived_Type (Def_Id)
7507 and then Present (Full_View (Def_Id))
7508 and then Is_Itype (Full_View (Def_Id))
7509 and then Has_Private_Declaration (Full_View (Def_Id))
7510 and then Freeze_Node (Full_View (Def_Id)) = N
7511 then
7512 Set_Entity (N, Full_View (Def_Id));
7513 Result := Freeze_Type (N);
7514 Set_Entity (N, Def_Id);
7516 -- All other types require no expander action. There are such cases
7517 -- (e.g. task types and protected types). In such cases, the freeze
7518 -- nodes are there for use by Gigi.
7520 end if;
7522 -- Complete the initialization of all pending access types' finalization
7523 -- masters now that the designated type has been is frozen and primitive
7524 -- Finalize_Address generated.
7526 Process_Pending_Access_Types (Def_Id);
7527 Freeze_Stream_Operations (N, Def_Id);
7529 -- Generate the [spec and] body of the procedure tasked with the runtime
7530 -- verification of pragma Default_Initial_Condition's expression.
7532 if Has_DIC (Def_Id) then
7533 Build_DIC_Procedure_Body (Def_Id, For_Freeze => True);
7534 end if;
7536 -- Generate the [spec and] body of the invariant procedure tasked with
7537 -- the runtime verification of all invariants that pertain to the type.
7538 -- This includes invariants on the partial and full view, inherited
7539 -- class-wide invariants from parent types or interfaces, and invariants
7540 -- on array elements or record components.
7542 if Is_Interface (Def_Id) then
7544 -- Interfaces are treated as the partial view of a private type in
7545 -- order to achieve uniformity with the general case. As a result, an
7546 -- interface receives only a "partial" invariant procedure which is
7547 -- never called.
7549 if Has_Own_Invariants (Def_Id) then
7550 Build_Invariant_Procedure_Body
7551 (Typ => Def_Id,
7552 Partial_Invariant => Is_Interface (Def_Id));
7553 end if;
7555 -- Non-interface types
7557 -- Do not generate invariant procedure within other assertion
7558 -- subprograms, which may involve local declarations of local
7559 -- subtypes to which these checks don't apply.
7561 elsif Has_Invariants (Def_Id) then
7562 if Within_Internal_Subprogram
7563 or else (Ekind (Current_Scope) = E_Function
7564 and then Is_Predicate_Function (Current_Scope))
7565 then
7566 null;
7567 else
7568 Build_Invariant_Procedure_Body (Def_Id);
7569 end if;
7570 end if;
7572 Restore_Ghost_Mode (Saved_GM);
7574 return Result;
7576 exception
7577 when RE_Not_Available =>
7578 Restore_Ghost_Mode (Saved_GM);
7580 return False;
7581 end Freeze_Type;
7583 -------------------------
7584 -- Get_Simple_Init_Val --
7585 -------------------------
7587 function Get_Simple_Init_Val
7588 (T : Entity_Id;
7589 N : Node_Id;
7590 Size : Uint := No_Uint) return Node_Id
7592 Loc : constant Source_Ptr := Sloc (N);
7593 Val : Node_Id;
7594 Result : Node_Id;
7595 Val_RE : RE_Id;
7597 Size_To_Use : Uint;
7598 -- This is the size to be used for computation of the appropriate
7599 -- initial value for the Normalize_Scalars and Initialize_Scalars case.
7601 IV_Attribute : constant Boolean :=
7602 Nkind (N) = N_Attribute_Reference
7603 and then Attribute_Name (N) = Name_Invalid_Value;
7605 Lo_Bound : Uint;
7606 Hi_Bound : Uint;
7607 -- These are the values computed by the procedure Check_Subtype_Bounds
7609 procedure Check_Subtype_Bounds;
7610 -- This procedure examines the subtype T, and its ancestor subtypes and
7611 -- derived types to determine the best known information about the
7612 -- bounds of the subtype. After the call Lo_Bound is set either to
7613 -- No_Uint if no information can be determined, or to a value which
7614 -- represents a known low bound, i.e. a valid value of the subtype can
7615 -- not be less than this value. Hi_Bound is similarly set to a known
7616 -- high bound (valid value cannot be greater than this).
7618 --------------------------
7619 -- Check_Subtype_Bounds --
7620 --------------------------
7622 procedure Check_Subtype_Bounds is
7623 ST1 : Entity_Id;
7624 ST2 : Entity_Id;
7625 Lo : Node_Id;
7626 Hi : Node_Id;
7627 Loval : Uint;
7628 Hival : Uint;
7630 begin
7631 Lo_Bound := No_Uint;
7632 Hi_Bound := No_Uint;
7634 -- Loop to climb ancestor subtypes and derived types
7636 ST1 := T;
7637 loop
7638 if not Is_Discrete_Type (ST1) then
7639 return;
7640 end if;
7642 Lo := Type_Low_Bound (ST1);
7643 Hi := Type_High_Bound (ST1);
7645 if Compile_Time_Known_Value (Lo) then
7646 Loval := Expr_Value (Lo);
7648 if Lo_Bound = No_Uint or else Lo_Bound < Loval then
7649 Lo_Bound := Loval;
7650 end if;
7651 end if;
7653 if Compile_Time_Known_Value (Hi) then
7654 Hival := Expr_Value (Hi);
7656 if Hi_Bound = No_Uint or else Hi_Bound > Hival then
7657 Hi_Bound := Hival;
7658 end if;
7659 end if;
7661 ST2 := Ancestor_Subtype (ST1);
7663 if No (ST2) then
7664 ST2 := Etype (ST1);
7665 end if;
7667 exit when ST1 = ST2;
7668 ST1 := ST2;
7669 end loop;
7670 end Check_Subtype_Bounds;
7672 -- Start of processing for Get_Simple_Init_Val
7674 begin
7675 -- For a private type, we should always have an underlying type (because
7676 -- this was already checked in Needs_Simple_Initialization). What we do
7677 -- is to get the value for the underlying type and then do an unchecked
7678 -- conversion to the private type.
7680 if Is_Private_Type (T) then
7681 Val := Get_Simple_Init_Val (Underlying_Type (T), N, Size);
7683 -- A special case, if the underlying value is null, then qualify it
7684 -- with the underlying type, so that the null is properly typed.
7685 -- Similarly, if it is an aggregate it must be qualified, because an
7686 -- unchecked conversion does not provide a context for it.
7688 if Nkind_In (Val, N_Null, N_Aggregate) then
7689 Val :=
7690 Make_Qualified_Expression (Loc,
7691 Subtype_Mark =>
7692 New_Occurrence_Of (Underlying_Type (T), Loc),
7693 Expression => Val);
7694 end if;
7696 Result := Unchecked_Convert_To (T, Val);
7698 -- Don't truncate result (important for Initialize/Normalize_Scalars)
7700 if Nkind (Result) = N_Unchecked_Type_Conversion
7701 and then Is_Scalar_Type (Underlying_Type (T))
7702 then
7703 Set_No_Truncation (Result);
7704 end if;
7706 return Result;
7708 -- Scalars with Default_Value aspect. The first subtype may now be
7709 -- private, so retrieve value from underlying type.
7711 elsif Is_Scalar_Type (T) and then Has_Default_Aspect (T) then
7712 if Is_Private_Type (First_Subtype (T)) then
7713 return Unchecked_Convert_To (T,
7714 Default_Aspect_Value (Full_View (First_Subtype (T))));
7715 else
7716 return
7717 Convert_To (T, Default_Aspect_Value (First_Subtype (T)));
7718 end if;
7720 -- Otherwise, for scalars, we must have normalize/initialize scalars
7721 -- case, or if the node N is an 'Invalid_Value attribute node.
7723 elsif Is_Scalar_Type (T) then
7724 pragma Assert (Init_Or_Norm_Scalars or IV_Attribute);
7726 -- Compute size of object. If it is given by the caller, we can use
7727 -- it directly, otherwise we use Esize (T) as an estimate. As far as
7728 -- we know this covers all cases correctly.
7730 if Size = No_Uint or else Size <= Uint_0 then
7731 Size_To_Use := UI_Max (Uint_1, Esize (T));
7732 else
7733 Size_To_Use := Size;
7734 end if;
7736 -- Maximum size to use is 64 bits, since we will create values of
7737 -- type Unsigned_64 and the range must fit this type.
7739 if Size_To_Use /= No_Uint and then Size_To_Use > Uint_64 then
7740 Size_To_Use := Uint_64;
7741 end if;
7743 -- Check known bounds of subtype
7745 Check_Subtype_Bounds;
7747 -- Processing for Normalize_Scalars case
7749 if Normalize_Scalars and then not IV_Attribute then
7751 -- If zero is invalid, it is a convenient value to use that is
7752 -- for sure an appropriate invalid value in all situations.
7754 if Lo_Bound /= No_Uint and then Lo_Bound > Uint_0 then
7755 Val := Make_Integer_Literal (Loc, 0);
7757 -- Cases where all one bits is the appropriate invalid value
7759 -- For modular types, all 1 bits is either invalid or valid. If
7760 -- it is valid, then there is nothing that can be done since there
7761 -- are no invalid values (we ruled out zero already).
7763 -- For signed integer types that have no negative values, either
7764 -- there is room for negative values, or there is not. If there
7765 -- is, then all 1-bits may be interpreted as minus one, which is
7766 -- certainly invalid. Alternatively it is treated as the largest
7767 -- positive value, in which case the observation for modular types
7768 -- still applies.
7770 -- For float types, all 1-bits is a NaN (not a number), which is
7771 -- certainly an appropriately invalid value.
7773 elsif Is_Unsigned_Type (T)
7774 or else Is_Floating_Point_Type (T)
7775 or else Is_Enumeration_Type (T)
7776 then
7777 Val := Make_Integer_Literal (Loc, 2 ** Size_To_Use - 1);
7779 -- Resolve as Unsigned_64, because the largest number we can
7780 -- generate is out of range of universal integer.
7782 Analyze_And_Resolve (Val, RTE (RE_Unsigned_64));
7784 -- Case of signed types
7786 else
7787 declare
7788 Signed_Size : constant Uint :=
7789 UI_Min (Uint_63, Size_To_Use - 1);
7791 begin
7792 -- Normally we like to use the most negative number. The one
7793 -- exception is when this number is in the known subtype
7794 -- range and the largest positive number is not in the known
7795 -- subtype range.
7797 -- For this exceptional case, use largest positive value
7799 if Lo_Bound /= No_Uint and then Hi_Bound /= No_Uint
7800 and then Lo_Bound <= (-(2 ** Signed_Size))
7801 and then Hi_Bound < 2 ** Signed_Size
7802 then
7803 Val := Make_Integer_Literal (Loc, 2 ** Signed_Size - 1);
7805 -- Normal case of largest negative value
7807 else
7808 Val := Make_Integer_Literal (Loc, -(2 ** Signed_Size));
7809 end if;
7810 end;
7811 end if;
7813 -- Here for Initialize_Scalars case (or Invalid_Value attribute used)
7815 else
7816 -- For float types, use float values from System.Scalar_Values
7818 if Is_Floating_Point_Type (T) then
7819 if Root_Type (T) = Standard_Short_Float then
7820 Val_RE := RE_IS_Isf;
7821 elsif Root_Type (T) = Standard_Float then
7822 Val_RE := RE_IS_Ifl;
7823 elsif Root_Type (T) = Standard_Long_Float then
7824 Val_RE := RE_IS_Ilf;
7825 else pragma Assert (Root_Type (T) = Standard_Long_Long_Float);
7826 Val_RE := RE_IS_Ill;
7827 end if;
7829 -- If zero is invalid, use zero values from System.Scalar_Values
7831 elsif Lo_Bound /= No_Uint and then Lo_Bound > Uint_0 then
7832 if Size_To_Use <= 8 then
7833 Val_RE := RE_IS_Iz1;
7834 elsif Size_To_Use <= 16 then
7835 Val_RE := RE_IS_Iz2;
7836 elsif Size_To_Use <= 32 then
7837 Val_RE := RE_IS_Iz4;
7838 else
7839 Val_RE := RE_IS_Iz8;
7840 end if;
7842 -- For unsigned, use unsigned values from System.Scalar_Values
7844 elsif Is_Unsigned_Type (T) then
7845 if Size_To_Use <= 8 then
7846 Val_RE := RE_IS_Iu1;
7847 elsif Size_To_Use <= 16 then
7848 Val_RE := RE_IS_Iu2;
7849 elsif Size_To_Use <= 32 then
7850 Val_RE := RE_IS_Iu4;
7851 else
7852 Val_RE := RE_IS_Iu8;
7853 end if;
7855 -- For signed, use signed values from System.Scalar_Values
7857 else
7858 if Size_To_Use <= 8 then
7859 Val_RE := RE_IS_Is1;
7860 elsif Size_To_Use <= 16 then
7861 Val_RE := RE_IS_Is2;
7862 elsif Size_To_Use <= 32 then
7863 Val_RE := RE_IS_Is4;
7864 else
7865 Val_RE := RE_IS_Is8;
7866 end if;
7867 end if;
7869 Val := New_Occurrence_Of (RTE (Val_RE), Loc);
7870 end if;
7872 -- The final expression is obtained by doing an unchecked conversion
7873 -- of this result to the base type of the required subtype. Use the
7874 -- base type to prevent the unchecked conversion from chopping bits,
7875 -- and then we set Kill_Range_Check to preserve the "bad" value.
7877 Result := Unchecked_Convert_To (Base_Type (T), Val);
7879 -- Ensure result is not truncated, since we want the "bad" bits, and
7880 -- also kill range check on result.
7882 if Nkind (Result) = N_Unchecked_Type_Conversion then
7883 Set_No_Truncation (Result);
7884 Set_Kill_Range_Check (Result, True);
7885 end if;
7887 return Result;
7889 -- String or Wide_[Wide]_String (must have Initialize_Scalars set)
7891 elsif Is_Standard_String_Type (T) then
7892 pragma Assert (Init_Or_Norm_Scalars);
7894 return
7895 Make_Aggregate (Loc,
7896 Component_Associations => New_List (
7897 Make_Component_Association (Loc,
7898 Choices => New_List (
7899 Make_Others_Choice (Loc)),
7900 Expression =>
7901 Get_Simple_Init_Val
7902 (Component_Type (T), N, Esize (Root_Type (T))))));
7904 -- Access type is initialized to null
7906 elsif Is_Access_Type (T) then
7907 return Make_Null (Loc);
7909 -- No other possibilities should arise, since we should only be calling
7910 -- Get_Simple_Init_Val if Needs_Simple_Initialization returned True,
7911 -- indicating one of the above cases held.
7913 else
7914 raise Program_Error;
7915 end if;
7917 exception
7918 when RE_Not_Available =>
7919 return Empty;
7920 end Get_Simple_Init_Val;
7922 ------------------------------
7923 -- Has_New_Non_Standard_Rep --
7924 ------------------------------
7926 function Has_New_Non_Standard_Rep (T : Entity_Id) return Boolean is
7927 begin
7928 if not Is_Derived_Type (T) then
7929 return Has_Non_Standard_Rep (T)
7930 or else Has_Non_Standard_Rep (Root_Type (T));
7932 -- If Has_Non_Standard_Rep is not set on the derived type, the
7933 -- representation is fully inherited.
7935 elsif not Has_Non_Standard_Rep (T) then
7936 return False;
7938 else
7939 return First_Rep_Item (T) /= First_Rep_Item (Root_Type (T));
7941 -- May need a more precise check here: the First_Rep_Item may be a
7942 -- stream attribute, which does not affect the representation of the
7943 -- type ???
7945 end if;
7946 end Has_New_Non_Standard_Rep;
7948 ----------------------
7949 -- Inline_Init_Proc --
7950 ----------------------
7952 function Inline_Init_Proc (Typ : Entity_Id) return Boolean is
7953 begin
7954 -- The initialization proc of protected records is not worth inlining.
7955 -- In addition, when compiled for another unit for inlining purposes,
7956 -- it may make reference to entities that have not been elaborated yet.
7957 -- The initialization proc of records that need finalization contains
7958 -- a nested clean-up procedure that makes it impractical to inline as
7959 -- well, except for simple controlled types themselves. And similar
7960 -- considerations apply to task types.
7962 if Is_Concurrent_Type (Typ) then
7963 return False;
7965 elsif Needs_Finalization (Typ) and then not Is_Controlled (Typ) then
7966 return False;
7968 elsif Has_Task (Typ) then
7969 return False;
7971 else
7972 return True;
7973 end if;
7974 end Inline_Init_Proc;
7976 ----------------
7977 -- In_Runtime --
7978 ----------------
7980 function In_Runtime (E : Entity_Id) return Boolean is
7981 S1 : Entity_Id;
7983 begin
7984 S1 := Scope (E);
7985 while Scope (S1) /= Standard_Standard loop
7986 S1 := Scope (S1);
7987 end loop;
7989 return Is_RTU (S1, System) or else Is_RTU (S1, Ada);
7990 end In_Runtime;
7992 ----------------------------
7993 -- Initialization_Warning --
7994 ----------------------------
7996 procedure Initialization_Warning (E : Entity_Id) is
7997 Warning_Needed : Boolean;
7999 begin
8000 Warning_Needed := False;
8002 if Ekind (Current_Scope) = E_Package
8003 and then Static_Elaboration_Desired (Current_Scope)
8004 then
8005 if Is_Type (E) then
8006 if Is_Record_Type (E) then
8007 if Has_Discriminants (E)
8008 or else Is_Limited_Type (E)
8009 or else Has_Non_Standard_Rep (E)
8010 then
8011 Warning_Needed := True;
8013 else
8014 -- Verify that at least one component has an initialization
8015 -- expression. No need for a warning on a type if all its
8016 -- components have no initialization.
8018 declare
8019 Comp : Entity_Id;
8021 begin
8022 Comp := First_Component (E);
8023 while Present (Comp) loop
8024 if Ekind (Comp) = E_Discriminant
8025 or else
8026 (Nkind (Parent (Comp)) = N_Component_Declaration
8027 and then Present (Expression (Parent (Comp))))
8028 then
8029 Warning_Needed := True;
8030 exit;
8031 end if;
8033 Next_Component (Comp);
8034 end loop;
8035 end;
8036 end if;
8038 if Warning_Needed then
8039 Error_Msg_N
8040 ("Objects of the type cannot be initialized statically "
8041 & "by default??", Parent (E));
8042 end if;
8043 end if;
8045 else
8046 Error_Msg_N ("Object cannot be initialized statically??", E);
8047 end if;
8048 end if;
8049 end Initialization_Warning;
8051 ------------------
8052 -- Init_Formals --
8053 ------------------
8055 function Init_Formals (Typ : Entity_Id) return List_Id is
8056 Loc : constant Source_Ptr := Sloc (Typ);
8057 Formals : List_Id;
8059 begin
8060 -- First parameter is always _Init : in out typ. Note that we need this
8061 -- to be in/out because in the case of the task record value, there
8062 -- are default record fields (_Priority, _Size, -Task_Info) that may
8063 -- be referenced in the generated initialization routine.
8065 Formals := New_List (
8066 Make_Parameter_Specification (Loc,
8067 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uInit),
8068 In_Present => True,
8069 Out_Present => True,
8070 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
8072 -- For task record value, or type that contains tasks, add two more
8073 -- formals, _Master : Master_Id and _Chain : in out Activation_Chain
8074 -- We also add these parameters for the task record type case.
8076 if Has_Task (Typ)
8077 or else (Is_Record_Type (Typ) and then Is_Task_Record_Type (Typ))
8078 then
8079 Append_To (Formals,
8080 Make_Parameter_Specification (Loc,
8081 Defining_Identifier =>
8082 Make_Defining_Identifier (Loc, Name_uMaster),
8083 Parameter_Type =>
8084 New_Occurrence_Of (RTE (RE_Master_Id), Loc)));
8086 -- Add _Chain (not done for sequential elaboration policy, see
8087 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
8089 if Partition_Elaboration_Policy /= 'S' then
8090 Append_To (Formals,
8091 Make_Parameter_Specification (Loc,
8092 Defining_Identifier =>
8093 Make_Defining_Identifier (Loc, Name_uChain),
8094 In_Present => True,
8095 Out_Present => True,
8096 Parameter_Type =>
8097 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc)));
8098 end if;
8100 Append_To (Formals,
8101 Make_Parameter_Specification (Loc,
8102 Defining_Identifier =>
8103 Make_Defining_Identifier (Loc, Name_uTask_Name),
8104 In_Present => True,
8105 Parameter_Type => New_Occurrence_Of (Standard_String, Loc)));
8106 end if;
8108 return Formals;
8110 exception
8111 when RE_Not_Available =>
8112 return Empty_List;
8113 end Init_Formals;
8115 -------------------------
8116 -- Init_Secondary_Tags --
8117 -------------------------
8119 procedure Init_Secondary_Tags
8120 (Typ : Entity_Id;
8121 Target : Node_Id;
8122 Stmts_List : List_Id;
8123 Fixed_Comps : Boolean := True;
8124 Variable_Comps : Boolean := True)
8126 Loc : constant Source_Ptr := Sloc (Target);
8128 -- Inherit the C++ tag of the secondary dispatch table of Typ associated
8129 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
8131 procedure Initialize_Tag
8132 (Typ : Entity_Id;
8133 Iface : Entity_Id;
8134 Tag_Comp : Entity_Id;
8135 Iface_Tag : Node_Id);
8136 -- Initialize the tag of the secondary dispatch table of Typ associated
8137 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
8138 -- Compiling under the CPP full ABI compatibility mode, if the ancestor
8139 -- of Typ CPP tagged type we generate code to inherit the contents of
8140 -- the dispatch table directly from the ancestor.
8142 --------------------
8143 -- Initialize_Tag --
8144 --------------------
8146 procedure Initialize_Tag
8147 (Typ : Entity_Id;
8148 Iface : Entity_Id;
8149 Tag_Comp : Entity_Id;
8150 Iface_Tag : Node_Id)
8152 Comp_Typ : Entity_Id;
8153 Offset_To_Top_Comp : Entity_Id := Empty;
8155 begin
8156 -- Initialize pointer to secondary DT associated with the interface
8158 if not Is_Ancestor (Iface, Typ, Use_Full_View => True) then
8159 Append_To (Stmts_List,
8160 Make_Assignment_Statement (Loc,
8161 Name =>
8162 Make_Selected_Component (Loc,
8163 Prefix => New_Copy_Tree (Target),
8164 Selector_Name => New_Occurrence_Of (Tag_Comp, Loc)),
8165 Expression =>
8166 New_Occurrence_Of (Iface_Tag, Loc)));
8167 end if;
8169 Comp_Typ := Scope (Tag_Comp);
8171 -- Initialize the entries of the table of interfaces. We generate a
8172 -- different call when the parent of the type has variable size
8173 -- components.
8175 if Comp_Typ /= Etype (Comp_Typ)
8176 and then Is_Variable_Size_Record (Etype (Comp_Typ))
8177 and then Chars (Tag_Comp) /= Name_uTag
8178 then
8179 pragma Assert (Present (DT_Offset_To_Top_Func (Tag_Comp)));
8181 -- Issue error if Set_Dynamic_Offset_To_Top is not available in a
8182 -- configurable run-time environment.
8184 if not RTE_Available (RE_Set_Dynamic_Offset_To_Top) then
8185 Error_Msg_CRT
8186 ("variable size record with interface types", Typ);
8187 return;
8188 end if;
8190 -- Generate:
8191 -- Set_Dynamic_Offset_To_Top
8192 -- (This => Init,
8193 -- Interface_T => Iface'Tag,
8194 -- Offset_Value => n,
8195 -- Offset_Func => Fn'Address)
8197 Append_To (Stmts_List,
8198 Make_Procedure_Call_Statement (Loc,
8199 Name =>
8200 New_Occurrence_Of (RTE (RE_Set_Dynamic_Offset_To_Top), Loc),
8201 Parameter_Associations => New_List (
8202 Make_Attribute_Reference (Loc,
8203 Prefix => New_Copy_Tree (Target),
8204 Attribute_Name => Name_Address),
8206 Unchecked_Convert_To (RTE (RE_Tag),
8207 New_Occurrence_Of
8208 (Node (First_Elmt (Access_Disp_Table (Iface))),
8209 Loc)),
8211 Unchecked_Convert_To
8212 (RTE (RE_Storage_Offset),
8213 Make_Attribute_Reference (Loc,
8214 Prefix =>
8215 Make_Selected_Component (Loc,
8216 Prefix => New_Copy_Tree (Target),
8217 Selector_Name =>
8218 New_Occurrence_Of (Tag_Comp, Loc)),
8219 Attribute_Name => Name_Position)),
8221 Unchecked_Convert_To (RTE (RE_Offset_To_Top_Function_Ptr),
8222 Make_Attribute_Reference (Loc,
8223 Prefix => New_Occurrence_Of
8224 (DT_Offset_To_Top_Func (Tag_Comp), Loc),
8225 Attribute_Name => Name_Address)))));
8227 -- In this case the next component stores the value of the offset
8228 -- to the top.
8230 Offset_To_Top_Comp := Next_Entity (Tag_Comp);
8231 pragma Assert (Present (Offset_To_Top_Comp));
8233 Append_To (Stmts_List,
8234 Make_Assignment_Statement (Loc,
8235 Name =>
8236 Make_Selected_Component (Loc,
8237 Prefix => New_Copy_Tree (Target),
8238 Selector_Name =>
8239 New_Occurrence_Of (Offset_To_Top_Comp, Loc)),
8241 Expression =>
8242 Make_Attribute_Reference (Loc,
8243 Prefix =>
8244 Make_Selected_Component (Loc,
8245 Prefix => New_Copy_Tree (Target),
8246 Selector_Name => New_Occurrence_Of (Tag_Comp, Loc)),
8247 Attribute_Name => Name_Position)));
8249 -- Normal case: No discriminants in the parent type
8251 else
8252 -- Don't need to set any value if this interface shares the
8253 -- primary dispatch table.
8255 if not Is_Ancestor (Iface, Typ, Use_Full_View => True) then
8256 Append_To (Stmts_List,
8257 Build_Set_Static_Offset_To_Top (Loc,
8258 Iface_Tag => New_Occurrence_Of (Iface_Tag, Loc),
8259 Offset_Value =>
8260 Unchecked_Convert_To (RTE (RE_Storage_Offset),
8261 Make_Attribute_Reference (Loc,
8262 Prefix =>
8263 Make_Selected_Component (Loc,
8264 Prefix => New_Copy_Tree (Target),
8265 Selector_Name =>
8266 New_Occurrence_Of (Tag_Comp, Loc)),
8267 Attribute_Name => Name_Position))));
8268 end if;
8270 -- Generate:
8271 -- Register_Interface_Offset
8272 -- (This => Init,
8273 -- Interface_T => Iface'Tag,
8274 -- Is_Constant => True,
8275 -- Offset_Value => n,
8276 -- Offset_Func => null);
8278 if RTE_Available (RE_Register_Interface_Offset) then
8279 Append_To (Stmts_List,
8280 Make_Procedure_Call_Statement (Loc,
8281 Name =>
8282 New_Occurrence_Of
8283 (RTE (RE_Register_Interface_Offset), Loc),
8284 Parameter_Associations => New_List (
8285 Make_Attribute_Reference (Loc,
8286 Prefix => New_Copy_Tree (Target),
8287 Attribute_Name => Name_Address),
8289 Unchecked_Convert_To (RTE (RE_Tag),
8290 New_Occurrence_Of
8291 (Node (First_Elmt (Access_Disp_Table (Iface))), Loc)),
8293 New_Occurrence_Of (Standard_True, Loc),
8295 Unchecked_Convert_To (RTE (RE_Storage_Offset),
8296 Make_Attribute_Reference (Loc,
8297 Prefix =>
8298 Make_Selected_Component (Loc,
8299 Prefix => New_Copy_Tree (Target),
8300 Selector_Name =>
8301 New_Occurrence_Of (Tag_Comp, Loc)),
8302 Attribute_Name => Name_Position)),
8304 Make_Null (Loc))));
8305 end if;
8306 end if;
8307 end Initialize_Tag;
8309 -- Local variables
8311 Full_Typ : Entity_Id;
8312 Ifaces_List : Elist_Id;
8313 Ifaces_Comp_List : Elist_Id;
8314 Ifaces_Tag_List : Elist_Id;
8315 Iface_Elmt : Elmt_Id;
8316 Iface_Comp_Elmt : Elmt_Id;
8317 Iface_Tag_Elmt : Elmt_Id;
8318 Tag_Comp : Node_Id;
8319 In_Variable_Pos : Boolean;
8321 -- Start of processing for Init_Secondary_Tags
8323 begin
8324 -- Handle private types
8326 if Present (Full_View (Typ)) then
8327 Full_Typ := Full_View (Typ);
8328 else
8329 Full_Typ := Typ;
8330 end if;
8332 Collect_Interfaces_Info
8333 (Full_Typ, Ifaces_List, Ifaces_Comp_List, Ifaces_Tag_List);
8335 Iface_Elmt := First_Elmt (Ifaces_List);
8336 Iface_Comp_Elmt := First_Elmt (Ifaces_Comp_List);
8337 Iface_Tag_Elmt := First_Elmt (Ifaces_Tag_List);
8338 while Present (Iface_Elmt) loop
8339 Tag_Comp := Node (Iface_Comp_Elmt);
8341 -- Check if parent of record type has variable size components
8343 In_Variable_Pos := Scope (Tag_Comp) /= Etype (Scope (Tag_Comp))
8344 and then Is_Variable_Size_Record (Etype (Scope (Tag_Comp)));
8346 -- If we are compiling under the CPP full ABI compatibility mode and
8347 -- the ancestor is a CPP_Pragma tagged type then we generate code to
8348 -- initialize the secondary tag components from tags that reference
8349 -- secondary tables filled with copy of parent slots.
8351 if Is_CPP_Class (Root_Type (Full_Typ)) then
8353 -- Reject interface components located at variable offset in
8354 -- C++ derivations. This is currently unsupported.
8356 if not Fixed_Comps and then In_Variable_Pos then
8358 -- Locate the first dynamic component of the record. Done to
8359 -- improve the text of the warning.
8361 declare
8362 Comp : Entity_Id;
8363 Comp_Typ : Entity_Id;
8365 begin
8366 Comp := First_Entity (Typ);
8367 while Present (Comp) loop
8368 Comp_Typ := Etype (Comp);
8370 if Ekind (Comp) /= E_Discriminant
8371 and then not Is_Tag (Comp)
8372 then
8373 exit when
8374 (Is_Record_Type (Comp_Typ)
8375 and then
8376 Is_Variable_Size_Record (Base_Type (Comp_Typ)))
8377 or else
8378 (Is_Array_Type (Comp_Typ)
8379 and then Is_Variable_Size_Array (Comp_Typ));
8380 end if;
8382 Next_Entity (Comp);
8383 end loop;
8385 pragma Assert (Present (Comp));
8386 Error_Msg_Node_2 := Comp;
8387 Error_Msg_NE
8388 ("parent type & with dynamic component & cannot be parent"
8389 & " of 'C'P'P derivation if new interfaces are present",
8390 Typ, Scope (Original_Record_Component (Comp)));
8392 Error_Msg_Sloc :=
8393 Sloc (Scope (Original_Record_Component (Comp)));
8394 Error_Msg_NE
8395 ("type derived from 'C'P'P type & defined #",
8396 Typ, Scope (Original_Record_Component (Comp)));
8398 -- Avoid duplicated warnings
8400 exit;
8401 end;
8403 -- Initialize secondary tags
8405 else
8406 Append_To (Stmts_List,
8407 Make_Assignment_Statement (Loc,
8408 Name =>
8409 Make_Selected_Component (Loc,
8410 Prefix => New_Copy_Tree (Target),
8411 Selector_Name =>
8412 New_Occurrence_Of (Node (Iface_Comp_Elmt), Loc)),
8413 Expression =>
8414 New_Occurrence_Of (Node (Iface_Tag_Elmt), Loc)));
8415 end if;
8417 -- Otherwise generate code to initialize the tag
8419 else
8420 if (In_Variable_Pos and then Variable_Comps)
8421 or else (not In_Variable_Pos and then Fixed_Comps)
8422 then
8423 Initialize_Tag (Full_Typ,
8424 Iface => Node (Iface_Elmt),
8425 Tag_Comp => Tag_Comp,
8426 Iface_Tag => Node (Iface_Tag_Elmt));
8427 end if;
8428 end if;
8430 Next_Elmt (Iface_Elmt);
8431 Next_Elmt (Iface_Comp_Elmt);
8432 Next_Elmt (Iface_Tag_Elmt);
8433 end loop;
8434 end Init_Secondary_Tags;
8436 ------------------------
8437 -- Is_User_Defined_Eq --
8438 ------------------------
8440 function Is_User_Defined_Equality (Prim : Node_Id) return Boolean is
8441 begin
8442 return Chars (Prim) = Name_Op_Eq
8443 and then Etype (First_Formal (Prim)) =
8444 Etype (Next_Formal (First_Formal (Prim)))
8445 and then Base_Type (Etype (Prim)) = Standard_Boolean;
8446 end Is_User_Defined_Equality;
8448 ----------------------------------------
8449 -- Make_Controlling_Function_Wrappers --
8450 ----------------------------------------
8452 procedure Make_Controlling_Function_Wrappers
8453 (Tag_Typ : Entity_Id;
8454 Decl_List : out List_Id;
8455 Body_List : out List_Id)
8457 Loc : constant Source_Ptr := Sloc (Tag_Typ);
8458 Prim_Elmt : Elmt_Id;
8459 Subp : Entity_Id;
8460 Actual_List : List_Id;
8461 Formal_List : List_Id;
8462 Formal : Entity_Id;
8463 Par_Formal : Entity_Id;
8464 Formal_Node : Node_Id;
8465 Func_Body : Node_Id;
8466 Func_Decl : Node_Id;
8467 Func_Spec : Node_Id;
8468 Return_Stmt : Node_Id;
8470 begin
8471 Decl_List := New_List;
8472 Body_List := New_List;
8474 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
8475 while Present (Prim_Elmt) loop
8476 Subp := Node (Prim_Elmt);
8478 -- If a primitive function with a controlling result of the type has
8479 -- not been overridden by the user, then we must create a wrapper
8480 -- function here that effectively overrides it and invokes the
8481 -- (non-abstract) parent function. This can only occur for a null
8482 -- extension. Note that functions with anonymous controlling access
8483 -- results don't qualify and must be overridden. We also exclude
8484 -- Input attributes, since each type will have its own version of
8485 -- Input constructed by the expander. The test for Comes_From_Source
8486 -- is needed to distinguish inherited operations from renamings
8487 -- (which also have Alias set). We exclude internal entities with
8488 -- Interface_Alias to avoid generating duplicated wrappers since
8489 -- the primitive which covers the interface is also available in
8490 -- the list of primitive operations.
8492 -- The function may be abstract, or require_Overriding may be set
8493 -- for it, because tests for null extensions may already have reset
8494 -- the Is_Abstract_Subprogram_Flag. If Requires_Overriding is not
8495 -- set, functions that need wrappers are recognized by having an
8496 -- alias that returns the parent type.
8498 if Comes_From_Source (Subp)
8499 or else No (Alias (Subp))
8500 or else Present (Interface_Alias (Subp))
8501 or else Ekind (Subp) /= E_Function
8502 or else not Has_Controlling_Result (Subp)
8503 or else Is_Access_Type (Etype (Subp))
8504 or else Is_Abstract_Subprogram (Alias (Subp))
8505 or else Is_TSS (Subp, TSS_Stream_Input)
8506 then
8507 goto Next_Prim;
8509 elsif Is_Abstract_Subprogram (Subp)
8510 or else Requires_Overriding (Subp)
8511 or else
8512 (Is_Null_Extension (Etype (Subp))
8513 and then Etype (Alias (Subp)) /= Etype (Subp))
8514 then
8515 Formal_List := No_List;
8516 Formal := First_Formal (Subp);
8518 if Present (Formal) then
8519 Formal_List := New_List;
8521 while Present (Formal) loop
8522 Append
8523 (Make_Parameter_Specification
8524 (Loc,
8525 Defining_Identifier =>
8526 Make_Defining_Identifier (Sloc (Formal),
8527 Chars => Chars (Formal)),
8528 In_Present => In_Present (Parent (Formal)),
8529 Out_Present => Out_Present (Parent (Formal)),
8530 Null_Exclusion_Present =>
8531 Null_Exclusion_Present (Parent (Formal)),
8532 Parameter_Type =>
8533 New_Occurrence_Of (Etype (Formal), Loc),
8534 Expression =>
8535 New_Copy_Tree (Expression (Parent (Formal)))),
8536 Formal_List);
8538 Next_Formal (Formal);
8539 end loop;
8540 end if;
8542 Func_Spec :=
8543 Make_Function_Specification (Loc,
8544 Defining_Unit_Name =>
8545 Make_Defining_Identifier (Loc,
8546 Chars => Chars (Subp)),
8547 Parameter_Specifications => Formal_List,
8548 Result_Definition =>
8549 New_Occurrence_Of (Etype (Subp), Loc));
8551 Func_Decl := Make_Subprogram_Declaration (Loc, Func_Spec);
8552 Append_To (Decl_List, Func_Decl);
8554 -- Build a wrapper body that calls the parent function. The body
8555 -- contains a single return statement that returns an extension
8556 -- aggregate whose ancestor part is a call to the parent function,
8557 -- passing the formals as actuals (with any controlling arguments
8558 -- converted to the types of the corresponding formals of the
8559 -- parent function, which might be anonymous access types), and
8560 -- having a null extension.
8562 Formal := First_Formal (Subp);
8563 Par_Formal := First_Formal (Alias (Subp));
8564 Formal_Node := First (Formal_List);
8566 if Present (Formal) then
8567 Actual_List := New_List;
8568 else
8569 Actual_List := No_List;
8570 end if;
8572 while Present (Formal) loop
8573 if Is_Controlling_Formal (Formal) then
8574 Append_To (Actual_List,
8575 Make_Type_Conversion (Loc,
8576 Subtype_Mark =>
8577 New_Occurrence_Of (Etype (Par_Formal), Loc),
8578 Expression =>
8579 New_Occurrence_Of
8580 (Defining_Identifier (Formal_Node), Loc)));
8581 else
8582 Append_To
8583 (Actual_List,
8584 New_Occurrence_Of
8585 (Defining_Identifier (Formal_Node), Loc));
8586 end if;
8588 Next_Formal (Formal);
8589 Next_Formal (Par_Formal);
8590 Next (Formal_Node);
8591 end loop;
8593 Return_Stmt :=
8594 Make_Simple_Return_Statement (Loc,
8595 Expression =>
8596 Make_Extension_Aggregate (Loc,
8597 Ancestor_Part =>
8598 Make_Function_Call (Loc,
8599 Name =>
8600 New_Occurrence_Of (Alias (Subp), Loc),
8601 Parameter_Associations => Actual_List),
8602 Null_Record_Present => True));
8604 Func_Body :=
8605 Make_Subprogram_Body (Loc,
8606 Specification => New_Copy_Tree (Func_Spec),
8607 Declarations => Empty_List,
8608 Handled_Statement_Sequence =>
8609 Make_Handled_Sequence_Of_Statements (Loc,
8610 Statements => New_List (Return_Stmt)));
8612 Set_Defining_Unit_Name
8613 (Specification (Func_Body),
8614 Make_Defining_Identifier (Loc, Chars (Subp)));
8616 Append_To (Body_List, Func_Body);
8618 -- Replace the inherited function with the wrapper function in the
8619 -- primitive operations list. We add the minimum decoration needed
8620 -- to override interface primitives.
8622 Set_Ekind (Defining_Unit_Name (Func_Spec), E_Function);
8624 Override_Dispatching_Operation
8625 (Tag_Typ, Subp, New_Op => Defining_Unit_Name (Func_Spec),
8626 Is_Wrapper => True);
8627 end if;
8629 <<Next_Prim>>
8630 Next_Elmt (Prim_Elmt);
8631 end loop;
8632 end Make_Controlling_Function_Wrappers;
8634 -------------------
8635 -- Make_Eq_Body --
8636 -------------------
8638 function Make_Eq_Body
8639 (Typ : Entity_Id;
8640 Eq_Name : Name_Id) return Node_Id
8642 Loc : constant Source_Ptr := Sloc (Parent (Typ));
8643 Decl : Node_Id;
8644 Def : constant Node_Id := Parent (Typ);
8645 Stmts : constant List_Id := New_List;
8646 Variant_Case : Boolean := Has_Discriminants (Typ);
8647 Comps : Node_Id := Empty;
8648 Typ_Def : Node_Id := Type_Definition (Def);
8650 begin
8651 Decl :=
8652 Predef_Spec_Or_Body (Loc,
8653 Tag_Typ => Typ,
8654 Name => Eq_Name,
8655 Profile => New_List (
8656 Make_Parameter_Specification (Loc,
8657 Defining_Identifier =>
8658 Make_Defining_Identifier (Loc, Name_X),
8659 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
8661 Make_Parameter_Specification (Loc,
8662 Defining_Identifier =>
8663 Make_Defining_Identifier (Loc, Name_Y),
8664 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8666 Ret_Type => Standard_Boolean,
8667 For_Body => True);
8669 if Variant_Case then
8670 if Nkind (Typ_Def) = N_Derived_Type_Definition then
8671 Typ_Def := Record_Extension_Part (Typ_Def);
8672 end if;
8674 if Present (Typ_Def) then
8675 Comps := Component_List (Typ_Def);
8676 end if;
8678 Variant_Case :=
8679 Present (Comps) and then Present (Variant_Part (Comps));
8680 end if;
8682 if Variant_Case then
8683 Append_To (Stmts,
8684 Make_Eq_If (Typ, Discriminant_Specifications (Def)));
8685 Append_List_To (Stmts, Make_Eq_Case (Typ, Comps));
8686 Append_To (Stmts,
8687 Make_Simple_Return_Statement (Loc,
8688 Expression => New_Occurrence_Of (Standard_True, Loc)));
8690 else
8691 Append_To (Stmts,
8692 Make_Simple_Return_Statement (Loc,
8693 Expression =>
8694 Expand_Record_Equality
8695 (Typ,
8696 Typ => Typ,
8697 Lhs => Make_Identifier (Loc, Name_X),
8698 Rhs => Make_Identifier (Loc, Name_Y),
8699 Bodies => Declarations (Decl))));
8700 end if;
8702 Set_Handled_Statement_Sequence
8703 (Decl, Make_Handled_Sequence_Of_Statements (Loc, Stmts));
8704 return Decl;
8705 end Make_Eq_Body;
8707 ------------------
8708 -- Make_Eq_Case --
8709 ------------------
8711 -- <Make_Eq_If shared components>
8713 -- case X.D1 is
8714 -- when V1 => <Make_Eq_Case> on subcomponents
8715 -- ...
8716 -- when Vn => <Make_Eq_Case> on subcomponents
8717 -- end case;
8719 function Make_Eq_Case
8720 (E : Entity_Id;
8721 CL : Node_Id;
8722 Discrs : Elist_Id := New_Elmt_List) return List_Id
8724 Loc : constant Source_Ptr := Sloc (E);
8725 Result : constant List_Id := New_List;
8726 Variant : Node_Id;
8727 Alt_List : List_Id;
8729 function Corresponding_Formal (C : Node_Id) return Entity_Id;
8730 -- Given the discriminant that controls a given variant of an unchecked
8731 -- union, find the formal of the equality function that carries the
8732 -- inferred value of the discriminant.
8734 function External_Name (E : Entity_Id) return Name_Id;
8735 -- The value of a given discriminant is conveyed in the corresponding
8736 -- formal parameter of the equality routine. The name of this formal
8737 -- parameter carries a one-character suffix which is removed here.
8739 --------------------------
8740 -- Corresponding_Formal --
8741 --------------------------
8743 function Corresponding_Formal (C : Node_Id) return Entity_Id is
8744 Discr : constant Entity_Id := Entity (Name (Variant_Part (C)));
8745 Elm : Elmt_Id;
8747 begin
8748 Elm := First_Elmt (Discrs);
8749 while Present (Elm) loop
8750 if Chars (Discr) = External_Name (Node (Elm)) then
8751 return Node (Elm);
8752 end if;
8754 Next_Elmt (Elm);
8755 end loop;
8757 -- A formal of the proper name must be found
8759 raise Program_Error;
8760 end Corresponding_Formal;
8762 -------------------
8763 -- External_Name --
8764 -------------------
8766 function External_Name (E : Entity_Id) return Name_Id is
8767 begin
8768 Get_Name_String (Chars (E));
8769 Name_Len := Name_Len - 1;
8770 return Name_Find;
8771 end External_Name;
8773 -- Start of processing for Make_Eq_Case
8775 begin
8776 Append_To (Result, Make_Eq_If (E, Component_Items (CL)));
8778 if No (Variant_Part (CL)) then
8779 return Result;
8780 end if;
8782 Variant := First_Non_Pragma (Variants (Variant_Part (CL)));
8784 if No (Variant) then
8785 return Result;
8786 end if;
8788 Alt_List := New_List;
8789 while Present (Variant) loop
8790 Append_To (Alt_List,
8791 Make_Case_Statement_Alternative (Loc,
8792 Discrete_Choices => New_Copy_List (Discrete_Choices (Variant)),
8793 Statements =>
8794 Make_Eq_Case (E, Component_List (Variant), Discrs)));
8795 Next_Non_Pragma (Variant);
8796 end loop;
8798 -- If we have an Unchecked_Union, use one of the parameters of the
8799 -- enclosing equality routine that captures the discriminant, to use
8800 -- as the expression in the generated case statement.
8802 if Is_Unchecked_Union (E) then
8803 Append_To (Result,
8804 Make_Case_Statement (Loc,
8805 Expression =>
8806 New_Occurrence_Of (Corresponding_Formal (CL), Loc),
8807 Alternatives => Alt_List));
8809 else
8810 Append_To (Result,
8811 Make_Case_Statement (Loc,
8812 Expression =>
8813 Make_Selected_Component (Loc,
8814 Prefix => Make_Identifier (Loc, Name_X),
8815 Selector_Name => New_Copy (Name (Variant_Part (CL)))),
8816 Alternatives => Alt_List));
8817 end if;
8819 return Result;
8820 end Make_Eq_Case;
8822 ----------------
8823 -- Make_Eq_If --
8824 ----------------
8826 -- Generates:
8828 -- if
8829 -- X.C1 /= Y.C1
8830 -- or else
8831 -- X.C2 /= Y.C2
8832 -- ...
8833 -- then
8834 -- return False;
8835 -- end if;
8837 -- or a null statement if the list L is empty
8839 function Make_Eq_If
8840 (E : Entity_Id;
8841 L : List_Id) return Node_Id
8843 Loc : constant Source_Ptr := Sloc (E);
8844 C : Node_Id;
8845 Field_Name : Name_Id;
8846 Cond : Node_Id;
8848 begin
8849 if No (L) then
8850 return Make_Null_Statement (Loc);
8852 else
8853 Cond := Empty;
8855 C := First_Non_Pragma (L);
8856 while Present (C) loop
8857 Field_Name := Chars (Defining_Identifier (C));
8859 -- The tags must not be compared: they are not part of the value.
8860 -- Ditto for parent interfaces because their equality operator is
8861 -- abstract.
8863 -- Note also that in the following, we use Make_Identifier for
8864 -- the component names. Use of New_Occurrence_Of to identify the
8865 -- components would be incorrect because the wrong entities for
8866 -- discriminants could be picked up in the private type case.
8868 if Field_Name = Name_uParent
8869 and then Is_Interface (Etype (Defining_Identifier (C)))
8870 then
8871 null;
8873 elsif Field_Name /= Name_uTag then
8874 Evolve_Or_Else (Cond,
8875 Make_Op_Ne (Loc,
8876 Left_Opnd =>
8877 Make_Selected_Component (Loc,
8878 Prefix => Make_Identifier (Loc, Name_X),
8879 Selector_Name => Make_Identifier (Loc, Field_Name)),
8881 Right_Opnd =>
8882 Make_Selected_Component (Loc,
8883 Prefix => Make_Identifier (Loc, Name_Y),
8884 Selector_Name => Make_Identifier (Loc, Field_Name))));
8885 end if;
8887 Next_Non_Pragma (C);
8888 end loop;
8890 if No (Cond) then
8891 return Make_Null_Statement (Loc);
8893 else
8894 return
8895 Make_Implicit_If_Statement (E,
8896 Condition => Cond,
8897 Then_Statements => New_List (
8898 Make_Simple_Return_Statement (Loc,
8899 Expression => New_Occurrence_Of (Standard_False, Loc))));
8900 end if;
8901 end if;
8902 end Make_Eq_If;
8904 -------------------
8905 -- Make_Neq_Body --
8906 -------------------
8908 function Make_Neq_Body (Tag_Typ : Entity_Id) return Node_Id is
8910 function Is_Predefined_Neq_Renaming (Prim : Node_Id) return Boolean;
8911 -- Returns true if Prim is a renaming of an unresolved predefined
8912 -- inequality operation.
8914 --------------------------------
8915 -- Is_Predefined_Neq_Renaming --
8916 --------------------------------
8918 function Is_Predefined_Neq_Renaming (Prim : Node_Id) return Boolean is
8919 begin
8920 return Chars (Prim) /= Name_Op_Ne
8921 and then Present (Alias (Prim))
8922 and then Comes_From_Source (Prim)
8923 and then Is_Intrinsic_Subprogram (Alias (Prim))
8924 and then Chars (Alias (Prim)) = Name_Op_Ne;
8925 end Is_Predefined_Neq_Renaming;
8927 -- Local variables
8929 Loc : constant Source_Ptr := Sloc (Parent (Tag_Typ));
8930 Stmts : constant List_Id := New_List;
8931 Decl : Node_Id;
8932 Eq_Prim : Entity_Id;
8933 Left_Op : Entity_Id;
8934 Renaming_Prim : Entity_Id;
8935 Right_Op : Entity_Id;
8936 Target : Entity_Id;
8938 -- Start of processing for Make_Neq_Body
8940 begin
8941 -- For a call on a renaming of a dispatching subprogram that is
8942 -- overridden, if the overriding occurred before the renaming, then
8943 -- the body executed is that of the overriding declaration, even if the
8944 -- overriding declaration is not visible at the place of the renaming;
8945 -- otherwise, the inherited or predefined subprogram is called, see
8946 -- (RM 8.5.4(8))
8948 -- Stage 1: Search for a renaming of the inequality primitive and also
8949 -- search for an overriding of the equality primitive located before the
8950 -- renaming declaration.
8952 declare
8953 Elmt : Elmt_Id;
8954 Prim : Node_Id;
8956 begin
8957 Eq_Prim := Empty;
8958 Renaming_Prim := Empty;
8960 Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
8961 while Present (Elmt) loop
8962 Prim := Node (Elmt);
8964 if Is_User_Defined_Equality (Prim) and then No (Alias (Prim)) then
8965 if No (Renaming_Prim) then
8966 pragma Assert (No (Eq_Prim));
8967 Eq_Prim := Prim;
8968 end if;
8970 elsif Is_Predefined_Neq_Renaming (Prim) then
8971 Renaming_Prim := Prim;
8972 end if;
8974 Next_Elmt (Elmt);
8975 end loop;
8976 end;
8978 -- No further action needed if no renaming was found
8980 if No (Renaming_Prim) then
8981 return Empty;
8982 end if;
8984 -- Stage 2: Replace the renaming declaration by a subprogram declaration
8985 -- (required to add its body)
8987 Decl := Parent (Parent (Renaming_Prim));
8988 Rewrite (Decl,
8989 Make_Subprogram_Declaration (Loc,
8990 Specification => Specification (Decl)));
8991 Set_Analyzed (Decl);
8993 -- Remove the decoration of intrinsic renaming subprogram
8995 Set_Is_Intrinsic_Subprogram (Renaming_Prim, False);
8996 Set_Convention (Renaming_Prim, Convention_Ada);
8997 Set_Alias (Renaming_Prim, Empty);
8998 Set_Has_Completion (Renaming_Prim, False);
9000 -- Stage 3: Build the corresponding body
9002 Left_Op := First_Formal (Renaming_Prim);
9003 Right_Op := Next_Formal (Left_Op);
9005 Decl :=
9006 Predef_Spec_Or_Body (Loc,
9007 Tag_Typ => Tag_Typ,
9008 Name => Chars (Renaming_Prim),
9009 Profile => New_List (
9010 Make_Parameter_Specification (Loc,
9011 Defining_Identifier =>
9012 Make_Defining_Identifier (Loc, Chars (Left_Op)),
9013 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
9015 Make_Parameter_Specification (Loc,
9016 Defining_Identifier =>
9017 Make_Defining_Identifier (Loc, Chars (Right_Op)),
9018 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
9020 Ret_Type => Standard_Boolean,
9021 For_Body => True);
9023 -- If the overriding of the equality primitive occurred before the
9024 -- renaming, then generate:
9026 -- function <Neq_Name> (X : Y : Typ) return Boolean is
9027 -- begin
9028 -- return not Oeq (X, Y);
9029 -- end;
9031 if Present (Eq_Prim) then
9032 Target := Eq_Prim;
9034 -- Otherwise build a nested subprogram which performs the predefined
9035 -- evaluation of the equality operator. That is, generate:
9037 -- function <Neq_Name> (X : Y : Typ) return Boolean is
9038 -- function Oeq (X : Y) return Boolean is
9039 -- begin
9040 -- <<body of default implementation>>
9041 -- end;
9042 -- begin
9043 -- return not Oeq (X, Y);
9044 -- end;
9046 else
9047 declare
9048 Local_Subp : Node_Id;
9049 begin
9050 Local_Subp := Make_Eq_Body (Tag_Typ, Name_Op_Eq);
9051 Set_Declarations (Decl, New_List (Local_Subp));
9052 Target := Defining_Entity (Local_Subp);
9053 end;
9054 end if;
9056 Append_To (Stmts,
9057 Make_Simple_Return_Statement (Loc,
9058 Expression =>
9059 Make_Op_Not (Loc,
9060 Make_Function_Call (Loc,
9061 Name => New_Occurrence_Of (Target, Loc),
9062 Parameter_Associations => New_List (
9063 Make_Identifier (Loc, Chars (Left_Op)),
9064 Make_Identifier (Loc, Chars (Right_Op)))))));
9066 Set_Handled_Statement_Sequence
9067 (Decl, Make_Handled_Sequence_Of_Statements (Loc, Stmts));
9068 return Decl;
9069 end Make_Neq_Body;
9071 -------------------------------
9072 -- Make_Null_Procedure_Specs --
9073 -------------------------------
9075 function Make_Null_Procedure_Specs (Tag_Typ : Entity_Id) return List_Id is
9076 Decl_List : constant List_Id := New_List;
9077 Loc : constant Source_Ptr := Sloc (Tag_Typ);
9078 Formal : Entity_Id;
9079 Formal_List : List_Id;
9080 New_Param_Spec : Node_Id;
9081 Parent_Subp : Entity_Id;
9082 Prim_Elmt : Elmt_Id;
9083 Subp : Entity_Id;
9085 begin
9086 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
9087 while Present (Prim_Elmt) loop
9088 Subp := Node (Prim_Elmt);
9090 -- If a null procedure inherited from an interface has not been
9091 -- overridden, then we build a null procedure declaration to
9092 -- override the inherited procedure.
9094 Parent_Subp := Alias (Subp);
9096 if Present (Parent_Subp)
9097 and then Is_Null_Interface_Primitive (Parent_Subp)
9098 then
9099 Formal_List := No_List;
9100 Formal := First_Formal (Subp);
9102 if Present (Formal) then
9103 Formal_List := New_List;
9105 while Present (Formal) loop
9107 -- Copy the parameter spec including default expressions
9109 New_Param_Spec :=
9110 New_Copy_Tree (Parent (Formal), New_Sloc => Loc);
9112 -- Generate a new defining identifier for the new formal.
9113 -- required because New_Copy_Tree does not duplicate
9114 -- semantic fields (except itypes).
9116 Set_Defining_Identifier (New_Param_Spec,
9117 Make_Defining_Identifier (Sloc (Formal),
9118 Chars => Chars (Formal)));
9120 -- For controlling arguments we must change their
9121 -- parameter type to reference the tagged type (instead
9122 -- of the interface type)
9124 if Is_Controlling_Formal (Formal) then
9125 if Nkind (Parameter_Type (Parent (Formal))) = N_Identifier
9126 then
9127 Set_Parameter_Type (New_Param_Spec,
9128 New_Occurrence_Of (Tag_Typ, Loc));
9130 else pragma Assert
9131 (Nkind (Parameter_Type (Parent (Formal))) =
9132 N_Access_Definition);
9133 Set_Subtype_Mark (Parameter_Type (New_Param_Spec),
9134 New_Occurrence_Of (Tag_Typ, Loc));
9135 end if;
9136 end if;
9138 Append (New_Param_Spec, Formal_List);
9140 Next_Formal (Formal);
9141 end loop;
9142 end if;
9144 Append_To (Decl_List,
9145 Make_Subprogram_Declaration (Loc,
9146 Make_Procedure_Specification (Loc,
9147 Defining_Unit_Name =>
9148 Make_Defining_Identifier (Loc, Chars (Subp)),
9149 Parameter_Specifications => Formal_List,
9150 Null_Present => True)));
9151 end if;
9153 Next_Elmt (Prim_Elmt);
9154 end loop;
9156 return Decl_List;
9157 end Make_Null_Procedure_Specs;
9159 -------------------------------------
9160 -- Make_Predefined_Primitive_Specs --
9161 -------------------------------------
9163 procedure Make_Predefined_Primitive_Specs
9164 (Tag_Typ : Entity_Id;
9165 Predef_List : out List_Id;
9166 Renamed_Eq : out Entity_Id)
9168 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean;
9169 -- Returns true if Prim is a renaming of an unresolved predefined
9170 -- equality operation.
9172 -------------------------------
9173 -- Is_Predefined_Eq_Renaming --
9174 -------------------------------
9176 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean is
9177 begin
9178 return Chars (Prim) /= Name_Op_Eq
9179 and then Present (Alias (Prim))
9180 and then Comes_From_Source (Prim)
9181 and then Is_Intrinsic_Subprogram (Alias (Prim))
9182 and then Chars (Alias (Prim)) = Name_Op_Eq;
9183 end Is_Predefined_Eq_Renaming;
9185 -- Local variables
9187 Loc : constant Source_Ptr := Sloc (Tag_Typ);
9188 Res : constant List_Id := New_List;
9189 Eq_Name : Name_Id := Name_Op_Eq;
9190 Eq_Needed : Boolean;
9191 Eq_Spec : Node_Id;
9192 Prim : Elmt_Id;
9194 Has_Predef_Eq_Renaming : Boolean := False;
9195 -- Set to True if Tag_Typ has a primitive that renames the predefined
9196 -- equality operator. Used to implement (RM 8-5-4(8)).
9198 -- Start of processing for Make_Predefined_Primitive_Specs
9200 begin
9201 Renamed_Eq := Empty;
9203 -- Spec of _Size
9205 Append_To (Res, Predef_Spec_Or_Body (Loc,
9206 Tag_Typ => Tag_Typ,
9207 Name => Name_uSize,
9208 Profile => New_List (
9209 Make_Parameter_Specification (Loc,
9210 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
9211 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
9213 Ret_Type => Standard_Long_Long_Integer));
9215 -- Specs for dispatching stream attributes
9217 declare
9218 Stream_Op_TSS_Names :
9219 constant array (Positive range <>) of TSS_Name_Type :=
9220 (TSS_Stream_Read,
9221 TSS_Stream_Write,
9222 TSS_Stream_Input,
9223 TSS_Stream_Output);
9225 begin
9226 for Op in Stream_Op_TSS_Names'Range loop
9227 if Stream_Operation_OK (Tag_Typ, Stream_Op_TSS_Names (Op)) then
9228 Append_To (Res,
9229 Predef_Stream_Attr_Spec (Loc, Tag_Typ,
9230 Stream_Op_TSS_Names (Op)));
9231 end if;
9232 end loop;
9233 end;
9235 -- Spec of "=" is expanded if the type is not limited and if a user
9236 -- defined "=" was not already declared for the non-full view of a
9237 -- private extension
9239 if not Is_Limited_Type (Tag_Typ) then
9240 Eq_Needed := True;
9241 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
9242 while Present (Prim) loop
9244 -- If a primitive is encountered that renames the predefined
9245 -- equality operator before reaching any explicit equality
9246 -- primitive, then we still need to create a predefined equality
9247 -- function, because calls to it can occur via the renaming. A
9248 -- new name is created for the equality to avoid conflicting with
9249 -- any user-defined equality. (Note that this doesn't account for
9250 -- renamings of equality nested within subpackages???)
9252 if Is_Predefined_Eq_Renaming (Node (Prim)) then
9253 Has_Predef_Eq_Renaming := True;
9254 Eq_Name := New_External_Name (Chars (Node (Prim)), 'E');
9256 -- User-defined equality
9258 elsif Is_User_Defined_Equality (Node (Prim)) then
9259 if No (Alias (Node (Prim)))
9260 or else Nkind (Unit_Declaration_Node (Node (Prim))) =
9261 N_Subprogram_Renaming_Declaration
9262 then
9263 Eq_Needed := False;
9264 exit;
9266 -- If the parent is not an interface type and has an abstract
9267 -- equality function explicitly defined in the sources, then
9268 -- the inherited equality is abstract as well, and no body can
9269 -- be created for it.
9271 elsif not Is_Interface (Etype (Tag_Typ))
9272 and then Present (Alias (Node (Prim)))
9273 and then Comes_From_Source (Alias (Node (Prim)))
9274 and then Is_Abstract_Subprogram (Alias (Node (Prim)))
9275 then
9276 Eq_Needed := False;
9277 exit;
9279 -- If the type has an equality function corresponding with
9280 -- a primitive defined in an interface type, the inherited
9281 -- equality is abstract as well, and no body can be created
9282 -- for it.
9284 elsif Present (Alias (Node (Prim)))
9285 and then Comes_From_Source (Ultimate_Alias (Node (Prim)))
9286 and then
9287 Is_Interface
9288 (Find_Dispatching_Type (Ultimate_Alias (Node (Prim))))
9289 then
9290 Eq_Needed := False;
9291 exit;
9292 end if;
9293 end if;
9295 Next_Elmt (Prim);
9296 end loop;
9298 -- If a renaming of predefined equality was found but there was no
9299 -- user-defined equality (so Eq_Needed is still true), then set the
9300 -- name back to Name_Op_Eq. But in the case where a user-defined
9301 -- equality was located after such a renaming, then the predefined
9302 -- equality function is still needed, so Eq_Needed must be set back
9303 -- to True.
9305 if Eq_Name /= Name_Op_Eq then
9306 if Eq_Needed then
9307 Eq_Name := Name_Op_Eq;
9308 else
9309 Eq_Needed := True;
9310 end if;
9311 end if;
9313 if Eq_Needed then
9314 Eq_Spec := Predef_Spec_Or_Body (Loc,
9315 Tag_Typ => Tag_Typ,
9316 Name => Eq_Name,
9317 Profile => New_List (
9318 Make_Parameter_Specification (Loc,
9319 Defining_Identifier =>
9320 Make_Defining_Identifier (Loc, Name_X),
9321 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
9323 Make_Parameter_Specification (Loc,
9324 Defining_Identifier =>
9325 Make_Defining_Identifier (Loc, Name_Y),
9326 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
9327 Ret_Type => Standard_Boolean);
9328 Append_To (Res, Eq_Spec);
9330 if Has_Predef_Eq_Renaming then
9331 Renamed_Eq := Defining_Unit_Name (Specification (Eq_Spec));
9333 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
9334 while Present (Prim) loop
9336 -- Any renamings of equality that appeared before an
9337 -- overriding equality must be updated to refer to the
9338 -- entity for the predefined equality, otherwise calls via
9339 -- the renaming would get incorrectly resolved to call the
9340 -- user-defined equality function.
9342 if Is_Predefined_Eq_Renaming (Node (Prim)) then
9343 Set_Alias (Node (Prim), Renamed_Eq);
9345 -- Exit upon encountering a user-defined equality
9347 elsif Chars (Node (Prim)) = Name_Op_Eq
9348 and then No (Alias (Node (Prim)))
9349 then
9350 exit;
9351 end if;
9353 Next_Elmt (Prim);
9354 end loop;
9355 end if;
9356 end if;
9358 -- Spec for dispatching assignment
9360 Append_To (Res, Predef_Spec_Or_Body (Loc,
9361 Tag_Typ => Tag_Typ,
9362 Name => Name_uAssign,
9363 Profile => New_List (
9364 Make_Parameter_Specification (Loc,
9365 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
9366 Out_Present => True,
9367 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
9369 Make_Parameter_Specification (Loc,
9370 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
9371 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)))));
9372 end if;
9374 -- Ada 2005: Generate declarations for the following primitive
9375 -- operations for limited interfaces and synchronized types that
9376 -- implement a limited interface.
9378 -- Disp_Asynchronous_Select
9379 -- Disp_Conditional_Select
9380 -- Disp_Get_Prim_Op_Kind
9381 -- Disp_Get_Task_Id
9382 -- Disp_Requeue
9383 -- Disp_Timed_Select
9385 -- Disable the generation of these bodies if No_Dispatching_Calls,
9386 -- Ravenscar or ZFP is active.
9388 if Ada_Version >= Ada_2005
9389 and then not Restriction_Active (No_Dispatching_Calls)
9390 and then not Restriction_Active (No_Select_Statements)
9391 and then RTE_Available (RE_Select_Specific_Data)
9392 then
9393 -- These primitives are defined abstract in interface types
9395 if Is_Interface (Tag_Typ)
9396 and then Is_Limited_Record (Tag_Typ)
9397 then
9398 Append_To (Res,
9399 Make_Abstract_Subprogram_Declaration (Loc,
9400 Specification =>
9401 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
9403 Append_To (Res,
9404 Make_Abstract_Subprogram_Declaration (Loc,
9405 Specification =>
9406 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
9408 Append_To (Res,
9409 Make_Abstract_Subprogram_Declaration (Loc,
9410 Specification =>
9411 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
9413 Append_To (Res,
9414 Make_Abstract_Subprogram_Declaration (Loc,
9415 Specification =>
9416 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
9418 Append_To (Res,
9419 Make_Abstract_Subprogram_Declaration (Loc,
9420 Specification =>
9421 Make_Disp_Requeue_Spec (Tag_Typ)));
9423 Append_To (Res,
9424 Make_Abstract_Subprogram_Declaration (Loc,
9425 Specification =>
9426 Make_Disp_Timed_Select_Spec (Tag_Typ)));
9428 -- If ancestor is an interface type, declare non-abstract primitives
9429 -- to override the abstract primitives of the interface type.
9431 -- In VM targets we define these primitives in all root tagged types
9432 -- that are not interface types. Done because in VM targets we don't
9433 -- have secondary dispatch tables and any derivation of Tag_Typ may
9434 -- cover limited interfaces (which always have these primitives since
9435 -- they may be ancestors of synchronized interface types).
9437 elsif (not Is_Interface (Tag_Typ)
9438 and then Is_Interface (Etype (Tag_Typ))
9439 and then Is_Limited_Record (Etype (Tag_Typ)))
9440 or else
9441 (Is_Concurrent_Record_Type (Tag_Typ)
9442 and then Has_Interfaces (Tag_Typ))
9443 or else
9444 (not Tagged_Type_Expansion
9445 and then not Is_Interface (Tag_Typ)
9446 and then Tag_Typ = Root_Type (Tag_Typ))
9447 then
9448 Append_To (Res,
9449 Make_Subprogram_Declaration (Loc,
9450 Specification =>
9451 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
9453 Append_To (Res,
9454 Make_Subprogram_Declaration (Loc,
9455 Specification =>
9456 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
9458 Append_To (Res,
9459 Make_Subprogram_Declaration (Loc,
9460 Specification =>
9461 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
9463 Append_To (Res,
9464 Make_Subprogram_Declaration (Loc,
9465 Specification =>
9466 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
9468 Append_To (Res,
9469 Make_Subprogram_Declaration (Loc,
9470 Specification =>
9471 Make_Disp_Requeue_Spec (Tag_Typ)));
9473 Append_To (Res,
9474 Make_Subprogram_Declaration (Loc,
9475 Specification =>
9476 Make_Disp_Timed_Select_Spec (Tag_Typ)));
9477 end if;
9478 end if;
9480 -- All tagged types receive their own Deep_Adjust and Deep_Finalize
9481 -- regardless of whether they are controlled or may contain controlled
9482 -- components.
9484 -- Do not generate the routines if finalization is disabled
9486 if Restriction_Active (No_Finalization) then
9487 null;
9489 else
9490 if not Is_Limited_Type (Tag_Typ) then
9491 Append_To (Res, Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust));
9492 end if;
9494 Append_To (Res, Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize));
9495 end if;
9497 Predef_List := Res;
9498 end Make_Predefined_Primitive_Specs;
9500 -------------------------
9501 -- Make_Tag_Assignment --
9502 -------------------------
9504 function Make_Tag_Assignment (N : Node_Id) return Node_Id is
9505 Loc : constant Source_Ptr := Sloc (N);
9506 Def_If : constant Entity_Id := Defining_Identifier (N);
9507 Expr : constant Node_Id := Expression (N);
9508 Typ : constant Entity_Id := Etype (Def_If);
9509 Full_Typ : constant Entity_Id := Underlying_Type (Typ);
9510 New_Ref : Node_Id;
9512 begin
9513 -- This expansion activity is called during analysis, but cannot
9514 -- be applied in ASIS mode when other expansion is disabled.
9516 if Is_Tagged_Type (Typ)
9517 and then not Is_Class_Wide_Type (Typ)
9518 and then not Is_CPP_Class (Typ)
9519 and then Tagged_Type_Expansion
9520 and then Nkind (Expr) /= N_Aggregate
9521 and then not ASIS_Mode
9522 and then (Nkind (Expr) /= N_Qualified_Expression
9523 or else Nkind (Expression (Expr)) /= N_Aggregate)
9524 then
9525 New_Ref :=
9526 Make_Selected_Component (Loc,
9527 Prefix => New_Occurrence_Of (Def_If, Loc),
9528 Selector_Name =>
9529 New_Occurrence_Of (First_Tag_Component (Full_Typ), Loc));
9530 Set_Assignment_OK (New_Ref);
9532 return
9533 Make_Assignment_Statement (Loc,
9534 Name => New_Ref,
9535 Expression =>
9536 Unchecked_Convert_To (RTE (RE_Tag),
9537 New_Occurrence_Of (Node
9538 (First_Elmt (Access_Disp_Table (Full_Typ))), Loc)));
9539 else
9540 return Empty;
9541 end if;
9542 end Make_Tag_Assignment;
9544 ---------------------------------
9545 -- Needs_Simple_Initialization --
9546 ---------------------------------
9548 function Needs_Simple_Initialization
9549 (T : Entity_Id;
9550 Consider_IS : Boolean := True) return Boolean
9552 Consider_IS_NS : constant Boolean :=
9553 Normalize_Scalars or (Initialize_Scalars and Consider_IS);
9555 begin
9556 -- Never need initialization if it is suppressed
9558 if Initialization_Suppressed (T) then
9559 return False;
9560 end if;
9562 -- Check for private type, in which case test applies to the underlying
9563 -- type of the private type.
9565 if Is_Private_Type (T) then
9566 declare
9567 RT : constant Entity_Id := Underlying_Type (T);
9568 begin
9569 if Present (RT) then
9570 return Needs_Simple_Initialization (RT);
9571 else
9572 return False;
9573 end if;
9574 end;
9576 -- Scalar type with Default_Value aspect requires initialization
9578 elsif Is_Scalar_Type (T) and then Has_Default_Aspect (T) then
9579 return True;
9581 -- Cases needing simple initialization are access types, and, if pragma
9582 -- Normalize_Scalars or Initialize_Scalars is in effect, then all scalar
9583 -- types.
9585 elsif Is_Access_Type (T)
9586 or else (Consider_IS_NS and then (Is_Scalar_Type (T)))
9587 then
9588 return True;
9590 -- If Initialize/Normalize_Scalars is in effect, string objects also
9591 -- need initialization, unless they are created in the course of
9592 -- expanding an aggregate (since in the latter case they will be
9593 -- filled with appropriate initializing values before they are used).
9595 elsif Consider_IS_NS
9596 and then Is_Standard_String_Type (T)
9597 and then
9598 (not Is_Itype (T)
9599 or else Nkind (Associated_Node_For_Itype (T)) /= N_Aggregate)
9600 then
9601 return True;
9603 else
9604 return False;
9605 end if;
9606 end Needs_Simple_Initialization;
9608 ----------------------
9609 -- Predef_Deep_Spec --
9610 ----------------------
9612 function Predef_Deep_Spec
9613 (Loc : Source_Ptr;
9614 Tag_Typ : Entity_Id;
9615 Name : TSS_Name_Type;
9616 For_Body : Boolean := False) return Node_Id
9618 Formals : List_Id;
9620 begin
9621 -- V : in out Tag_Typ
9623 Formals := New_List (
9624 Make_Parameter_Specification (Loc,
9625 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
9626 In_Present => True,
9627 Out_Present => True,
9628 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)));
9630 -- F : Boolean := True
9632 if Name = TSS_Deep_Adjust
9633 or else Name = TSS_Deep_Finalize
9634 then
9635 Append_To (Formals,
9636 Make_Parameter_Specification (Loc,
9637 Defining_Identifier => Make_Defining_Identifier (Loc, Name_F),
9638 Parameter_Type => New_Occurrence_Of (Standard_Boolean, Loc),
9639 Expression => New_Occurrence_Of (Standard_True, Loc)));
9640 end if;
9642 return
9643 Predef_Spec_Or_Body (Loc,
9644 Name => Make_TSS_Name (Tag_Typ, Name),
9645 Tag_Typ => Tag_Typ,
9646 Profile => Formals,
9647 For_Body => For_Body);
9649 exception
9650 when RE_Not_Available =>
9651 return Empty;
9652 end Predef_Deep_Spec;
9654 -------------------------
9655 -- Predef_Spec_Or_Body --
9656 -------------------------
9658 function Predef_Spec_Or_Body
9659 (Loc : Source_Ptr;
9660 Tag_Typ : Entity_Id;
9661 Name : Name_Id;
9662 Profile : List_Id;
9663 Ret_Type : Entity_Id := Empty;
9664 For_Body : Boolean := False) return Node_Id
9666 Id : constant Entity_Id := Make_Defining_Identifier (Loc, Name);
9667 Spec : Node_Id;
9669 begin
9670 Set_Is_Public (Id, Is_Public (Tag_Typ));
9672 -- The internal flag is set to mark these declarations because they have
9673 -- specific properties. First, they are primitives even if they are not
9674 -- defined in the type scope (the freezing point is not necessarily in
9675 -- the same scope). Second, the predefined equality can be overridden by
9676 -- a user-defined equality, no body will be generated in this case.
9678 Set_Is_Internal (Id);
9680 if not Debug_Generated_Code then
9681 Set_Debug_Info_Off (Id);
9682 end if;
9684 if No (Ret_Type) then
9685 Spec :=
9686 Make_Procedure_Specification (Loc,
9687 Defining_Unit_Name => Id,
9688 Parameter_Specifications => Profile);
9689 else
9690 Spec :=
9691 Make_Function_Specification (Loc,
9692 Defining_Unit_Name => Id,
9693 Parameter_Specifications => Profile,
9694 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
9695 end if;
9697 if Is_Interface (Tag_Typ) then
9698 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
9700 -- If body case, return empty subprogram body. Note that this is ill-
9701 -- formed, because there is not even a null statement, and certainly not
9702 -- a return in the function case. The caller is expected to do surgery
9703 -- on the body to add the appropriate stuff.
9705 elsif For_Body then
9706 return Make_Subprogram_Body (Loc, Spec, Empty_List, Empty);
9708 -- For the case of an Input attribute predefined for an abstract type,
9709 -- generate an abstract specification. This will never be called, but we
9710 -- need the slot allocated in the dispatching table so that attributes
9711 -- typ'Class'Input and typ'Class'Output will work properly.
9713 elsif Is_TSS (Name, TSS_Stream_Input)
9714 and then Is_Abstract_Type (Tag_Typ)
9715 then
9716 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
9718 -- Normal spec case, where we return a subprogram declaration
9720 else
9721 return Make_Subprogram_Declaration (Loc, Spec);
9722 end if;
9723 end Predef_Spec_Or_Body;
9725 -----------------------------
9726 -- Predef_Stream_Attr_Spec --
9727 -----------------------------
9729 function Predef_Stream_Attr_Spec
9730 (Loc : Source_Ptr;
9731 Tag_Typ : Entity_Id;
9732 Name : TSS_Name_Type;
9733 For_Body : Boolean := False) return Node_Id
9735 Ret_Type : Entity_Id;
9737 begin
9738 if Name = TSS_Stream_Input then
9739 Ret_Type := Tag_Typ;
9740 else
9741 Ret_Type := Empty;
9742 end if;
9744 return
9745 Predef_Spec_Or_Body
9746 (Loc,
9747 Name => Make_TSS_Name (Tag_Typ, Name),
9748 Tag_Typ => Tag_Typ,
9749 Profile => Build_Stream_Attr_Profile (Loc, Tag_Typ, Name),
9750 Ret_Type => Ret_Type,
9751 For_Body => For_Body);
9752 end Predef_Stream_Attr_Spec;
9754 ---------------------------------
9755 -- Predefined_Primitive_Bodies --
9756 ---------------------------------
9758 function Predefined_Primitive_Bodies
9759 (Tag_Typ : Entity_Id;
9760 Renamed_Eq : Entity_Id) return List_Id
9762 Loc : constant Source_Ptr := Sloc (Tag_Typ);
9763 Res : constant List_Id := New_List;
9764 Adj_Call : Node_Id;
9765 Decl : Node_Id;
9766 Fin_Call : Node_Id;
9767 Prim : Elmt_Id;
9768 Eq_Needed : Boolean;
9769 Eq_Name : Name_Id;
9770 Ent : Entity_Id;
9772 pragma Warnings (Off, Ent);
9774 begin
9775 pragma Assert (not Is_Interface (Tag_Typ));
9777 -- See if we have a predefined "=" operator
9779 if Present (Renamed_Eq) then
9780 Eq_Needed := True;
9781 Eq_Name := Chars (Renamed_Eq);
9783 -- If the parent is an interface type then it has defined all the
9784 -- predefined primitives abstract and we need to check if the type
9785 -- has some user defined "=" function which matches the profile of
9786 -- the Ada predefined equality operator to avoid generating it.
9788 elsif Is_Interface (Etype (Tag_Typ)) then
9789 Eq_Needed := True;
9790 Eq_Name := Name_Op_Eq;
9792 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
9793 while Present (Prim) loop
9794 if Chars (Node (Prim)) = Name_Op_Eq
9795 and then not Is_Internal (Node (Prim))
9796 and then Present (First_Entity (Node (Prim)))
9798 -- The predefined equality primitive must have exactly two
9799 -- formals whose type is this tagged type
9801 and then Present (Last_Entity (Node (Prim)))
9802 and then Next_Entity (First_Entity (Node (Prim)))
9803 = Last_Entity (Node (Prim))
9804 and then Etype (First_Entity (Node (Prim))) = Tag_Typ
9805 and then Etype (Last_Entity (Node (Prim))) = Tag_Typ
9806 then
9807 Eq_Needed := False;
9808 Eq_Name := No_Name;
9809 exit;
9810 end if;
9812 Next_Elmt (Prim);
9813 end loop;
9815 else
9816 Eq_Needed := False;
9817 Eq_Name := No_Name;
9819 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
9820 while Present (Prim) loop
9821 if Chars (Node (Prim)) = Name_Op_Eq
9822 and then Is_Internal (Node (Prim))
9823 then
9824 Eq_Needed := True;
9825 Eq_Name := Name_Op_Eq;
9826 exit;
9827 end if;
9829 Next_Elmt (Prim);
9830 end loop;
9831 end if;
9833 -- Body of _Size
9835 Decl := Predef_Spec_Or_Body (Loc,
9836 Tag_Typ => Tag_Typ,
9837 Name => Name_uSize,
9838 Profile => New_List (
9839 Make_Parameter_Specification (Loc,
9840 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
9841 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
9843 Ret_Type => Standard_Long_Long_Integer,
9844 For_Body => True);
9846 Set_Handled_Statement_Sequence (Decl,
9847 Make_Handled_Sequence_Of_Statements (Loc, New_List (
9848 Make_Simple_Return_Statement (Loc,
9849 Expression =>
9850 Make_Attribute_Reference (Loc,
9851 Prefix => Make_Identifier (Loc, Name_X),
9852 Attribute_Name => Name_Size)))));
9854 Append_To (Res, Decl);
9856 -- Bodies for Dispatching stream IO routines. We need these only for
9857 -- non-limited types (in the limited case there is no dispatching).
9858 -- We also skip them if dispatching or finalization are not available
9859 -- or if stream operations are prohibited by restriction No_Streams or
9860 -- from use of pragma/aspect No_Tagged_Streams.
9862 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Read)
9863 and then No (TSS (Tag_Typ, TSS_Stream_Read))
9864 then
9865 Build_Record_Read_Procedure (Loc, Tag_Typ, Decl, Ent);
9866 Append_To (Res, Decl);
9867 end if;
9869 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Write)
9870 and then No (TSS (Tag_Typ, TSS_Stream_Write))
9871 then
9872 Build_Record_Write_Procedure (Loc, Tag_Typ, Decl, Ent);
9873 Append_To (Res, Decl);
9874 end if;
9876 -- Skip body of _Input for the abstract case, since the corresponding
9877 -- spec is abstract (see Predef_Spec_Or_Body).
9879 if not Is_Abstract_Type (Tag_Typ)
9880 and then Stream_Operation_OK (Tag_Typ, TSS_Stream_Input)
9881 and then No (TSS (Tag_Typ, TSS_Stream_Input))
9882 then
9883 Build_Record_Or_Elementary_Input_Function
9884 (Loc, Tag_Typ, Decl, Ent);
9885 Append_To (Res, Decl);
9886 end if;
9888 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Output)
9889 and then No (TSS (Tag_Typ, TSS_Stream_Output))
9890 then
9891 Build_Record_Or_Elementary_Output_Procedure (Loc, Tag_Typ, Decl, Ent);
9892 Append_To (Res, Decl);
9893 end if;
9895 -- Ada 2005: Generate bodies for the following primitive operations for
9896 -- limited interfaces and synchronized types that implement a limited
9897 -- interface.
9899 -- disp_asynchronous_select
9900 -- disp_conditional_select
9901 -- disp_get_prim_op_kind
9902 -- disp_get_task_id
9903 -- disp_timed_select
9905 -- The interface versions will have null bodies
9907 -- Disable the generation of these bodies if No_Dispatching_Calls,
9908 -- Ravenscar or ZFP is active.
9910 -- In VM targets we define these primitives in all root tagged types
9911 -- that are not interface types. Done because in VM targets we don't
9912 -- have secondary dispatch tables and any derivation of Tag_Typ may
9913 -- cover limited interfaces (which always have these primitives since
9914 -- they may be ancestors of synchronized interface types).
9916 if Ada_Version >= Ada_2005
9917 and then not Is_Interface (Tag_Typ)
9918 and then
9919 ((Is_Interface (Etype (Tag_Typ))
9920 and then Is_Limited_Record (Etype (Tag_Typ)))
9921 or else
9922 (Is_Concurrent_Record_Type (Tag_Typ)
9923 and then Has_Interfaces (Tag_Typ))
9924 or else
9925 (not Tagged_Type_Expansion
9926 and then Tag_Typ = Root_Type (Tag_Typ)))
9927 and then not Restriction_Active (No_Dispatching_Calls)
9928 and then not Restriction_Active (No_Select_Statements)
9929 and then RTE_Available (RE_Select_Specific_Data)
9930 then
9931 Append_To (Res, Make_Disp_Asynchronous_Select_Body (Tag_Typ));
9932 Append_To (Res, Make_Disp_Conditional_Select_Body (Tag_Typ));
9933 Append_To (Res, Make_Disp_Get_Prim_Op_Kind_Body (Tag_Typ));
9934 Append_To (Res, Make_Disp_Get_Task_Id_Body (Tag_Typ));
9935 Append_To (Res, Make_Disp_Requeue_Body (Tag_Typ));
9936 Append_To (Res, Make_Disp_Timed_Select_Body (Tag_Typ));
9937 end if;
9939 if not Is_Limited_Type (Tag_Typ) and then not Is_Interface (Tag_Typ) then
9941 -- Body for equality
9943 if Eq_Needed then
9944 Decl := Make_Eq_Body (Tag_Typ, Eq_Name);
9945 Append_To (Res, Decl);
9946 end if;
9948 -- Body for inequality (if required)
9950 Decl := Make_Neq_Body (Tag_Typ);
9952 if Present (Decl) then
9953 Append_To (Res, Decl);
9954 end if;
9956 -- Body for dispatching assignment
9958 Decl :=
9959 Predef_Spec_Or_Body (Loc,
9960 Tag_Typ => Tag_Typ,
9961 Name => Name_uAssign,
9962 Profile => New_List (
9963 Make_Parameter_Specification (Loc,
9964 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
9965 Out_Present => True,
9966 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
9968 Make_Parameter_Specification (Loc,
9969 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
9970 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
9971 For_Body => True);
9973 Set_Handled_Statement_Sequence (Decl,
9974 Make_Handled_Sequence_Of_Statements (Loc, New_List (
9975 Make_Assignment_Statement (Loc,
9976 Name => Make_Identifier (Loc, Name_X),
9977 Expression => Make_Identifier (Loc, Name_Y)))));
9979 Append_To (Res, Decl);
9980 end if;
9982 -- Generate empty bodies of routines Deep_Adjust and Deep_Finalize for
9983 -- tagged types which do not contain controlled components.
9985 -- Do not generate the routines if finalization is disabled
9987 if Restriction_Active (No_Finalization) then
9988 null;
9990 elsif not Has_Controlled_Component (Tag_Typ) then
9991 if not Is_Limited_Type (Tag_Typ) then
9992 Adj_Call := Empty;
9993 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust, True);
9995 if Is_Controlled (Tag_Typ) then
9996 Adj_Call :=
9997 Make_Adjust_Call (
9998 Obj_Ref => Make_Identifier (Loc, Name_V),
9999 Typ => Tag_Typ);
10000 end if;
10002 if No (Adj_Call) then
10003 Adj_Call := Make_Null_Statement (Loc);
10004 end if;
10006 Set_Handled_Statement_Sequence (Decl,
10007 Make_Handled_Sequence_Of_Statements (Loc,
10008 Statements => New_List (Adj_Call)));
10010 Append_To (Res, Decl);
10011 end if;
10013 Fin_Call := Empty;
10014 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize, True);
10016 if Is_Controlled (Tag_Typ) then
10017 Fin_Call :=
10018 Make_Final_Call
10019 (Obj_Ref => Make_Identifier (Loc, Name_V),
10020 Typ => Tag_Typ);
10021 end if;
10023 if No (Fin_Call) then
10024 Fin_Call := Make_Null_Statement (Loc);
10025 end if;
10027 Set_Handled_Statement_Sequence (Decl,
10028 Make_Handled_Sequence_Of_Statements (Loc,
10029 Statements => New_List (Fin_Call)));
10031 Append_To (Res, Decl);
10032 end if;
10034 return Res;
10035 end Predefined_Primitive_Bodies;
10037 ---------------------------------
10038 -- Predefined_Primitive_Freeze --
10039 ---------------------------------
10041 function Predefined_Primitive_Freeze
10042 (Tag_Typ : Entity_Id) return List_Id
10044 Res : constant List_Id := New_List;
10045 Prim : Elmt_Id;
10046 Frnodes : List_Id;
10048 begin
10049 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
10050 while Present (Prim) loop
10051 if Is_Predefined_Dispatching_Operation (Node (Prim)) then
10052 Frnodes := Freeze_Entity (Node (Prim), Tag_Typ);
10054 if Present (Frnodes) then
10055 Append_List_To (Res, Frnodes);
10056 end if;
10057 end if;
10059 Next_Elmt (Prim);
10060 end loop;
10062 return Res;
10063 end Predefined_Primitive_Freeze;
10065 -------------------------
10066 -- Stream_Operation_OK --
10067 -------------------------
10069 function Stream_Operation_OK
10070 (Typ : Entity_Id;
10071 Operation : TSS_Name_Type) return Boolean
10073 Has_Predefined_Or_Specified_Stream_Attribute : Boolean := False;
10075 begin
10076 -- Special case of a limited type extension: a default implementation
10077 -- of the stream attributes Read or Write exists if that attribute
10078 -- has been specified or is available for an ancestor type; a default
10079 -- implementation of the attribute Output (resp. Input) exists if the
10080 -- attribute has been specified or Write (resp. Read) is available for
10081 -- an ancestor type. The last condition only applies under Ada 2005.
10083 if Is_Limited_Type (Typ) and then Is_Tagged_Type (Typ) then
10084 if Operation = TSS_Stream_Read then
10085 Has_Predefined_Or_Specified_Stream_Attribute :=
10086 Has_Specified_Stream_Read (Typ);
10088 elsif Operation = TSS_Stream_Write then
10089 Has_Predefined_Or_Specified_Stream_Attribute :=
10090 Has_Specified_Stream_Write (Typ);
10092 elsif Operation = TSS_Stream_Input then
10093 Has_Predefined_Or_Specified_Stream_Attribute :=
10094 Has_Specified_Stream_Input (Typ)
10095 or else
10096 (Ada_Version >= Ada_2005
10097 and then Stream_Operation_OK (Typ, TSS_Stream_Read));
10099 elsif Operation = TSS_Stream_Output then
10100 Has_Predefined_Or_Specified_Stream_Attribute :=
10101 Has_Specified_Stream_Output (Typ)
10102 or else
10103 (Ada_Version >= Ada_2005
10104 and then Stream_Operation_OK (Typ, TSS_Stream_Write));
10105 end if;
10107 -- Case of inherited TSS_Stream_Read or TSS_Stream_Write
10109 if not Has_Predefined_Or_Specified_Stream_Attribute
10110 and then Is_Derived_Type (Typ)
10111 and then (Operation = TSS_Stream_Read
10112 or else Operation = TSS_Stream_Write)
10113 then
10114 Has_Predefined_Or_Specified_Stream_Attribute :=
10115 Present
10116 (Find_Inherited_TSS (Base_Type (Etype (Typ)), Operation));
10117 end if;
10118 end if;
10120 -- If the type is not limited, or else is limited but the attribute is
10121 -- explicitly specified or is predefined for the type, then return True,
10122 -- unless other conditions prevail, such as restrictions prohibiting
10123 -- streams or dispatching operations. We also return True for limited
10124 -- interfaces, because they may be extended by nonlimited types and
10125 -- permit inheritance in this case (addresses cases where an abstract
10126 -- extension doesn't get 'Input declared, as per comments below, but
10127 -- 'Class'Input must still be allowed). Note that attempts to apply
10128 -- stream attributes to a limited interface or its class-wide type
10129 -- (or limited extensions thereof) will still get properly rejected
10130 -- by Check_Stream_Attribute.
10132 -- We exclude the Input operation from being a predefined subprogram in
10133 -- the case where the associated type is an abstract extension, because
10134 -- the attribute is not callable in that case, per 13.13.2(49/2). Also,
10135 -- we don't want an abstract version created because types derived from
10136 -- the abstract type may not even have Input available (for example if
10137 -- derived from a private view of the abstract type that doesn't have
10138 -- a visible Input).
10140 -- Do not generate stream routines for type Finalization_Master because
10141 -- a master may never appear in types and therefore cannot be read or
10142 -- written.
10144 return
10145 (not Is_Limited_Type (Typ)
10146 or else Is_Interface (Typ)
10147 or else Has_Predefined_Or_Specified_Stream_Attribute)
10148 and then
10149 (Operation /= TSS_Stream_Input
10150 or else not Is_Abstract_Type (Typ)
10151 or else not Is_Derived_Type (Typ))
10152 and then not Has_Unknown_Discriminants (Typ)
10153 and then not
10154 (Is_Interface (Typ)
10155 and then
10156 (Is_Task_Interface (Typ)
10157 or else Is_Protected_Interface (Typ)
10158 or else Is_Synchronized_Interface (Typ)))
10159 and then not Restriction_Active (No_Streams)
10160 and then not Restriction_Active (No_Dispatch)
10161 and then No (No_Tagged_Streams_Pragma (Typ))
10162 and then not No_Run_Time_Mode
10163 and then RTE_Available (RE_Tag)
10164 and then No (Type_Without_Stream_Operation (Typ))
10165 and then RTE_Available (RE_Root_Stream_Type)
10166 and then not Is_RTE (Typ, RE_Finalization_Master);
10167 end Stream_Operation_OK;
10169 end Exp_Ch3;