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