Concretize three gimple_return_ accessors
[official-gcc.git] / gcc / ada / exp_ch3.adb
blob1aa813e3acc4980b902accf7c6d20659dfe4a2c5
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-2014, 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 Atree; use Atree;
27 with Checks; use Checks;
28 with Einfo; use Einfo;
29 with Errout; use Errout;
30 with Exp_Aggr; use Exp_Aggr;
31 with Exp_Atag; use Exp_Atag;
32 with Exp_Ch4; use Exp_Ch4;
33 with Exp_Ch6; use Exp_Ch6;
34 with Exp_Ch7; use Exp_Ch7;
35 with Exp_Ch9; use Exp_Ch9;
36 with Exp_Ch11; use Exp_Ch11;
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 Namet; use Namet;
46 with Nlists; use Nlists;
47 with Nmake; use Nmake;
48 with Opt; use Opt;
49 with Restrict; use Restrict;
50 with Rident; use Rident;
51 with Rtsfind; use Rtsfind;
52 with Sem; use Sem;
53 with Sem_Aux; use Sem_Aux;
54 with Sem_Attr; use Sem_Attr;
55 with Sem_Cat; use Sem_Cat;
56 with Sem_Ch3; use Sem_Ch3;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch8; use Sem_Ch8;
59 with Sem_Ch13; use Sem_Ch13;
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_Array_Invariant_Proc
93 (A_Type : Entity_Id;
94 Nod : Node_Id) return Node_Id;
95 -- If the component of type of array type has invariants, build procedure
96 -- that checks invariant on all components of the array. Ada 2012 specifies
97 -- that an invariant on some type T must be applied to in-out parameters
98 -- and return values that include a part of type T. If the array type has
99 -- an otherwise specified invariant, the component check procedure is
100 -- called from within the user-specified invariant. Otherwise this becomes
101 -- the invariant procedure for the array type.
103 function Build_Record_Invariant_Proc
104 (R_Type : Entity_Id;
105 Nod : Node_Id) return Node_Id;
106 -- Ditto for record types.
108 function Build_Discriminant_Formals
109 (Rec_Id : Entity_Id;
110 Use_Dl : Boolean) return List_Id;
111 -- This function uses the discriminants of a type to build a list of
112 -- formal parameters, used in Build_Init_Procedure among other places.
113 -- If the flag Use_Dl is set, the list is built using the already
114 -- defined discriminals of the type, as is the case for concurrent
115 -- types with discriminants. Otherwise new identifiers are created,
116 -- with the source names of the discriminants.
118 function Build_Equivalent_Array_Aggregate (T : Entity_Id) return Node_Id;
119 -- This function builds a static aggregate that can serve as the initial
120 -- value for an array type whose bounds are static, and whose component
121 -- type is a composite type that has a static equivalent aggregate.
122 -- The equivalent array aggregate is used both for object initialization
123 -- and for component initialization, when used in the following function.
125 function Build_Equivalent_Record_Aggregate (T : Entity_Id) return Node_Id;
126 -- This function builds a static aggregate that can serve as the initial
127 -- value for a record type whose components are scalar and initialized
128 -- with compile-time values, or arrays with similar initialization or
129 -- defaults. When possible, initialization of an object of the type can
130 -- be achieved by using a copy of the aggregate as an initial value, thus
131 -- removing the implicit call that would otherwise constitute elaboration
132 -- code.
134 procedure Build_Record_Init_Proc (N : Node_Id; Rec_Ent : Entity_Id);
135 -- Build record initialization procedure. N is the type declaration
136 -- node, and Rec_Ent is the corresponding entity for the record type.
138 procedure Build_Slice_Assignment (Typ : Entity_Id);
139 -- Build assignment procedure for one-dimensional arrays of controlled
140 -- types. Other array and slice assignments are expanded in-line, but
141 -- the code expansion for controlled components (when control actions
142 -- are active) can lead to very large blocks that GCC3 handles poorly.
144 procedure Build_Untagged_Equality (Typ : Entity_Id);
145 -- AI05-0123: Equality on untagged records composes. This procedure
146 -- builds the equality routine for an untagged record that has components
147 -- of a record type that has user-defined primitive equality operations.
148 -- The resulting operation is a TSS subprogram.
150 procedure Build_Variant_Record_Equality (Typ : Entity_Id);
151 -- Create An Equality function for the untagged variant record Typ and
152 -- attach it to the TSS list
154 procedure Check_Stream_Attributes (Typ : Entity_Id);
155 -- Check that if a limited extension has a parent with user-defined stream
156 -- attributes, and does not itself have user-defined stream-attributes,
157 -- then any limited component of the extension also has the corresponding
158 -- user-defined stream attributes.
160 procedure Clean_Task_Names
161 (Typ : Entity_Id;
162 Proc_Id : Entity_Id);
163 -- If an initialization procedure includes calls to generate names
164 -- for task subcomponents, indicate that secondary stack cleanup is
165 -- needed after an initialization. Typ is the component type, and Proc_Id
166 -- the initialization procedure for the enclosing composite type.
168 procedure Expand_Freeze_Array_Type (N : Node_Id);
169 -- Freeze an array type. Deals with building the initialization procedure,
170 -- creating the packed array type for a packed array and also with the
171 -- creation of the controlling procedures for the controlled case. The
172 -- argument N is the N_Freeze_Entity node for the type.
174 procedure Expand_Freeze_Class_Wide_Type (N : Node_Id);
175 -- Freeze a class-wide type. Build routine Finalize_Address for the purpose
176 -- of finalizing controlled derivations from the class-wide's root type.
178 procedure Expand_Freeze_Enumeration_Type (N : Node_Id);
179 -- Freeze enumeration type with non-standard representation. Builds the
180 -- array and function needed to convert between enumeration pos and
181 -- enumeration representation values. N is the N_Freeze_Entity node
182 -- for the type.
184 procedure Expand_Freeze_Record_Type (N : Node_Id);
185 -- Freeze record type. Builds all necessary discriminant checking
186 -- and other ancillary functions, and builds dispatch tables where
187 -- needed. The argument N is the N_Freeze_Entity node. This processing
188 -- applies only to E_Record_Type entities, not to class wide types,
189 -- record subtypes, or private types.
191 procedure Expand_Tagged_Root (T : Entity_Id);
192 -- Add a field _Tag at the beginning of the record. This field carries
193 -- the value of the access to the Dispatch table. This procedure is only
194 -- called on root type, the _Tag field being inherited by the descendants.
196 procedure Freeze_Stream_Operations (N : Node_Id; Typ : Entity_Id);
197 -- Treat user-defined stream operations as renaming_as_body if the
198 -- subprogram they rename is not frozen when the type is frozen.
200 procedure Insert_Component_Invariant_Checks
201 (N : Node_Id;
202 Typ : Entity_Id;
203 Proc : Node_Id);
204 -- If a composite type has invariants and also has components with defined
205 -- invariants. the component invariant procedure is inserted into the user-
206 -- defined invariant procedure and added to the checks to be performed.
208 procedure Initialization_Warning (E : Entity_Id);
209 -- If static elaboration of the package is requested, indicate
210 -- when a type does meet the conditions for static initialization. If
211 -- E is a type, it has components that have no static initialization.
212 -- if E is an entity, its initial expression is not compile-time known.
214 function Init_Formals (Typ : Entity_Id) return List_Id;
215 -- This function builds the list of formals for an initialization routine.
216 -- The first formal is always _Init with the given type. For task value
217 -- record types and types containing tasks, three additional formals are
218 -- added:
220 -- _Master : Master_Id
221 -- _Chain : in out Activation_Chain
222 -- _Task_Name : String
224 -- The caller must append additional entries for discriminants if required.
226 function In_Runtime (E : Entity_Id) return Boolean;
227 -- Check if E is defined in the RTL (in a child of Ada or System). Used
228 -- to avoid to bring in the overhead of _Input, _Output for tagged types.
230 function Is_User_Defined_Equality (Prim : Node_Id) return Boolean;
231 -- Returns true if Prim is a user defined equality function
233 function Make_Eq_Body
234 (Typ : Entity_Id;
235 Eq_Name : Name_Id) return Node_Id;
236 -- Build the body of a primitive equality operation for a tagged record
237 -- type, or in Ada 2012 for any record type that has components with a
238 -- user-defined equality. Factored out of Predefined_Primitive_Bodies.
240 function Make_Eq_Case
241 (E : Entity_Id;
242 CL : Node_Id;
243 Discrs : Elist_Id := New_Elmt_List) return List_Id;
244 -- Building block for variant record equality. Defined to share the code
245 -- between the tagged and untagged case. Given a Component_List node CL,
246 -- it generates an 'if' followed by a 'case' statement that compares all
247 -- components of local temporaries named X and Y (that are declared as
248 -- formals at some upper level). E provides the Sloc to be used for the
249 -- generated code.
251 -- IF E is an unchecked_union, Discrs is the list of formals created for
252 -- the inferred discriminants of one operand. These formals are used in
253 -- the generated case statements for each variant of the unchecked union.
255 function Make_Eq_If
256 (E : Entity_Id;
257 L : List_Id) return Node_Id;
258 -- Building block for variant record equality. Defined to share the code
259 -- between the tagged and untagged case. Given the list of components
260 -- (or discriminants) L, it generates a return statement that compares all
261 -- components of local temporaries named X and Y (that are declared as
262 -- formals at some upper level). E provides the Sloc to be used for the
263 -- generated code.
265 function Make_Neq_Body (Tag_Typ : Entity_Id) return Node_Id;
266 -- Search for a renaming of the inequality dispatching primitive of
267 -- this tagged type. If found then build and return the corresponding
268 -- rename-as-body inequality subprogram; otherwise return Empty.
270 procedure Make_Predefined_Primitive_Specs
271 (Tag_Typ : Entity_Id;
272 Predef_List : out List_Id;
273 Renamed_Eq : out Entity_Id);
274 -- Create a list with the specs of the predefined primitive operations.
275 -- For tagged types that are interfaces all these primitives are defined
276 -- abstract.
278 -- The following entries are present for all tagged types, and provide
279 -- the results of the corresponding attribute applied to the object.
280 -- Dispatching is required in general, since the result of the attribute
281 -- will vary with the actual object subtype.
283 -- _size provides result of 'Size attribute
284 -- typSR provides result of 'Read attribute
285 -- typSW provides result of 'Write attribute
286 -- typSI provides result of 'Input attribute
287 -- typSO provides result of 'Output attribute
289 -- The following entries are additionally present for non-limited tagged
290 -- types, and implement additional dispatching operations for predefined
291 -- operations:
293 -- _equality implements "=" operator
294 -- _assign implements assignment operation
295 -- typDF implements deep finalization
296 -- typDA implements deep adjust
298 -- The latter two are empty procedures unless the type contains some
299 -- controlled components that require finalization actions (the deep
300 -- in the name refers to the fact that the action applies to components).
302 -- The list is returned in Predef_List. The Parameter Renamed_Eq either
303 -- returns the value Empty, or else the defining unit name for the
304 -- predefined equality function in the case where the type has a primitive
305 -- operation that is a renaming of predefined equality (but only if there
306 -- is also an overriding user-defined equality function). The returned
307 -- Renamed_Eq will be passed to the corresponding parameter of
308 -- Predefined_Primitive_Bodies.
310 function Has_New_Non_Standard_Rep (T : Entity_Id) return Boolean;
311 -- returns True if there are representation clauses for type T that are not
312 -- inherited. If the result is false, the init_proc and the discriminant
313 -- checking functions of the parent can be reused by a derived type.
315 procedure Make_Controlling_Function_Wrappers
316 (Tag_Typ : Entity_Id;
317 Decl_List : out List_Id;
318 Body_List : out List_Id);
319 -- Ada 2005 (AI-391): Makes specs and bodies for the wrapper functions
320 -- associated with inherited functions with controlling results which
321 -- are not overridden. The body of each wrapper function consists solely
322 -- of a return statement whose expression is an extension aggregate
323 -- invoking the inherited subprogram's parent subprogram and extended
324 -- with a null association list.
326 function Make_Null_Procedure_Specs (Tag_Typ : Entity_Id) return List_Id;
327 -- Ada 2005 (AI-251): Makes specs for null procedures associated with any
328 -- null procedures inherited from an interface type that have not been
329 -- overridden. Only one null procedure will be created for a given set of
330 -- inherited null procedures with homographic profiles.
332 function Predef_Spec_Or_Body
333 (Loc : Source_Ptr;
334 Tag_Typ : Entity_Id;
335 Name : Name_Id;
336 Profile : List_Id;
337 Ret_Type : Entity_Id := Empty;
338 For_Body : Boolean := False) return Node_Id;
339 -- This function generates the appropriate expansion for a predefined
340 -- primitive operation specified by its name, parameter profile and
341 -- return type (Empty means this is a procedure). If For_Body is false,
342 -- then the returned node is a subprogram declaration. If For_Body is
343 -- true, then the returned node is a empty subprogram body containing
344 -- no declarations and no statements.
346 function Predef_Stream_Attr_Spec
347 (Loc : Source_Ptr;
348 Tag_Typ : Entity_Id;
349 Name : TSS_Name_Type;
350 For_Body : Boolean := False) return Node_Id;
351 -- Specialized version of Predef_Spec_Or_Body that apply to read, write,
352 -- input and output attribute whose specs are constructed in Exp_Strm.
354 function Predef_Deep_Spec
355 (Loc : Source_Ptr;
356 Tag_Typ : Entity_Id;
357 Name : TSS_Name_Type;
358 For_Body : Boolean := False) return Node_Id;
359 -- Specialized version of Predef_Spec_Or_Body that apply to _deep_adjust
360 -- and _deep_finalize
362 function Predefined_Primitive_Bodies
363 (Tag_Typ : Entity_Id;
364 Renamed_Eq : Entity_Id) return List_Id;
365 -- Create the bodies of the predefined primitives that are described in
366 -- Predefined_Primitive_Specs. When not empty, Renamed_Eq must denote
367 -- the defining unit name of the type's predefined equality as returned
368 -- by Make_Predefined_Primitive_Specs.
370 function Predefined_Primitive_Freeze (Tag_Typ : Entity_Id) return List_Id;
371 -- Freeze entities of all predefined primitive operations. This is needed
372 -- because the bodies of these operations do not normally do any freezing.
374 function Stream_Operation_OK
375 (Typ : Entity_Id;
376 Operation : TSS_Name_Type) return Boolean;
377 -- Check whether the named stream operation must be emitted for a given
378 -- type. The rules for inheritance of stream attributes by type extensions
379 -- are enforced by this function. Furthermore, various restrictions prevent
380 -- the generation of these operations, as a useful optimization or for
381 -- certification purposes.
383 --------------------------
384 -- Adjust_Discriminants --
385 --------------------------
387 -- This procedure attempts to define subtypes for discriminants that are
388 -- more restrictive than those declared. Such a replacement is possible if
389 -- we can demonstrate that values outside the restricted range would cause
390 -- constraint errors in any case. The advantage of restricting the
391 -- discriminant types in this way is that the maximum size of the variant
392 -- record can be calculated more conservatively.
394 -- An example of a situation in which we can perform this type of
395 -- restriction is the following:
397 -- subtype B is range 1 .. 10;
398 -- type Q is array (B range <>) of Integer;
400 -- type V (N : Natural) is record
401 -- C : Q (1 .. N);
402 -- end record;
404 -- In this situation, we can restrict the upper bound of N to 10, since
405 -- any larger value would cause a constraint error in any case.
407 -- There are many situations in which such restriction is possible, but
408 -- for now, we just look for cases like the above, where the component
409 -- in question is a one dimensional array whose upper bound is one of
410 -- the record discriminants. Also the component must not be part of
411 -- any variant part, since then the component does not always exist.
413 procedure Adjust_Discriminants (Rtype : Entity_Id) is
414 Loc : constant Source_Ptr := Sloc (Rtype);
415 Comp : Entity_Id;
416 Ctyp : Entity_Id;
417 Ityp : Entity_Id;
418 Lo : Node_Id;
419 Hi : Node_Id;
420 P : Node_Id;
421 Loval : Uint;
422 Discr : Entity_Id;
423 Dtyp : Entity_Id;
424 Dhi : Node_Id;
425 Dhiv : Uint;
426 Ahi : Node_Id;
427 Ahiv : Uint;
428 Tnn : Entity_Id;
430 begin
431 Comp := First_Component (Rtype);
432 while Present (Comp) loop
434 -- If our parent is a variant, quit, we do not look at components
435 -- that are in variant parts, because they may not always exist.
437 P := Parent (Comp); -- component declaration
438 P := Parent (P); -- component list
440 exit when Nkind (Parent (P)) = N_Variant;
442 -- We are looking for a one dimensional array type
444 Ctyp := Etype (Comp);
446 if not Is_Array_Type (Ctyp) or else Number_Dimensions (Ctyp) > 1 then
447 goto Continue;
448 end if;
450 -- The lower bound must be constant, and the upper bound is a
451 -- discriminant (which is a discriminant of the current record).
453 Ityp := Etype (First_Index (Ctyp));
454 Lo := Type_Low_Bound (Ityp);
455 Hi := Type_High_Bound (Ityp);
457 if not Compile_Time_Known_Value (Lo)
458 or else Nkind (Hi) /= N_Identifier
459 or else No (Entity (Hi))
460 or else Ekind (Entity (Hi)) /= E_Discriminant
461 then
462 goto Continue;
463 end if;
465 -- We have an array with appropriate bounds
467 Loval := Expr_Value (Lo);
468 Discr := Entity (Hi);
469 Dtyp := Etype (Discr);
471 -- See if the discriminant has a known upper bound
473 Dhi := Type_High_Bound (Dtyp);
475 if not Compile_Time_Known_Value (Dhi) then
476 goto Continue;
477 end if;
479 Dhiv := Expr_Value (Dhi);
481 -- See if base type of component array has known upper bound
483 Ahi := Type_High_Bound (Etype (First_Index (Base_Type (Ctyp))));
485 if not Compile_Time_Known_Value (Ahi) then
486 goto Continue;
487 end if;
489 Ahiv := Expr_Value (Ahi);
491 -- The condition for doing the restriction is that the high bound
492 -- of the discriminant is greater than the low bound of the array,
493 -- and is also greater than the high bound of the base type index.
495 if Dhiv > Loval and then Dhiv > Ahiv then
497 -- We can reset the upper bound of the discriminant type to
498 -- whichever is larger, the low bound of the component, or
499 -- the high bound of the base type array index.
501 -- We build a subtype that is declared as
503 -- subtype Tnn is discr_type range discr_type'First .. max;
505 -- And insert this declaration into the tree. The type of the
506 -- discriminant is then reset to this more restricted subtype.
508 Tnn := Make_Temporary (Loc, 'T');
510 Insert_Action (Declaration_Node (Rtype),
511 Make_Subtype_Declaration (Loc,
512 Defining_Identifier => Tnn,
513 Subtype_Indication =>
514 Make_Subtype_Indication (Loc,
515 Subtype_Mark => New_Occurrence_Of (Dtyp, Loc),
516 Constraint =>
517 Make_Range_Constraint (Loc,
518 Range_Expression =>
519 Make_Range (Loc,
520 Low_Bound =>
521 Make_Attribute_Reference (Loc,
522 Attribute_Name => Name_First,
523 Prefix => New_Occurrence_Of (Dtyp, Loc)),
524 High_Bound =>
525 Make_Integer_Literal (Loc,
526 Intval => UI_Max (Loval, Ahiv)))))));
528 Set_Etype (Discr, Tnn);
529 end if;
531 <<Continue>>
532 Next_Component (Comp);
533 end loop;
534 end Adjust_Discriminants;
536 ---------------------------
537 -- Build_Array_Init_Proc --
538 ---------------------------
540 procedure Build_Array_Init_Proc (A_Type : Entity_Id; Nod : Node_Id) is
541 Comp_Type : constant Entity_Id := Component_Type (A_Type);
542 Body_Stmts : List_Id;
543 Has_Default_Init : Boolean;
544 Index_List : List_Id;
545 Loc : Source_Ptr;
546 Proc_Id : Entity_Id;
548 function Init_Component return List_Id;
549 -- Create one statement to initialize one array component, designated
550 -- by a full set of indexes.
552 function Init_One_Dimension (N : Int) return List_Id;
553 -- Create loop to initialize one dimension of the array. The single
554 -- statement in the loop body initializes the inner dimensions if any,
555 -- or else the single component. Note that this procedure is called
556 -- recursively, with N being the dimension to be initialized. A call
557 -- with N greater than the number of dimensions simply generates the
558 -- component initialization, terminating the recursion.
560 --------------------
561 -- Init_Component --
562 --------------------
564 function Init_Component return List_Id is
565 Comp : Node_Id;
567 begin
568 Comp :=
569 Make_Indexed_Component (Loc,
570 Prefix => Make_Identifier (Loc, Name_uInit),
571 Expressions => Index_List);
573 if Has_Default_Aspect (A_Type) then
574 Set_Assignment_OK (Comp);
575 return New_List (
576 Make_Assignment_Statement (Loc,
577 Name => Comp,
578 Expression =>
579 Convert_To (Comp_Type,
580 Default_Aspect_Component_Value (First_Subtype (A_Type)))));
582 elsif Needs_Simple_Initialization (Comp_Type) then
583 Set_Assignment_OK (Comp);
584 return New_List (
585 Make_Assignment_Statement (Loc,
586 Name => Comp,
587 Expression =>
588 Get_Simple_Init_Val
589 (Comp_Type, Nod, Component_Size (A_Type))));
591 else
592 Clean_Task_Names (Comp_Type, Proc_Id);
593 return
594 Build_Initialization_Call
595 (Loc, Comp, Comp_Type,
596 In_Init_Proc => True,
597 Enclos_Type => A_Type);
598 end if;
599 end Init_Component;
601 ------------------------
602 -- Init_One_Dimension --
603 ------------------------
605 function Init_One_Dimension (N : Int) return List_Id is
606 Index : Entity_Id;
608 begin
609 -- If the component does not need initializing, then there is nothing
610 -- to do here, so we return a null body. This occurs when generating
611 -- the dummy Init_Proc needed for Initialize_Scalars processing.
613 if not Has_Non_Null_Base_Init_Proc (Comp_Type)
614 and then not Needs_Simple_Initialization (Comp_Type)
615 and then not Has_Task (Comp_Type)
616 and then not Has_Default_Aspect (A_Type)
617 then
618 return New_List (Make_Null_Statement (Loc));
620 -- If all dimensions dealt with, we simply initialize the component
622 elsif N > Number_Dimensions (A_Type) then
623 return Init_Component;
625 -- Here we generate the required loop
627 else
628 Index :=
629 Make_Defining_Identifier (Loc, New_External_Name ('J', N));
631 Append (New_Occurrence_Of (Index, Loc), Index_List);
633 return New_List (
634 Make_Implicit_Loop_Statement (Nod,
635 Identifier => Empty,
636 Iteration_Scheme =>
637 Make_Iteration_Scheme (Loc,
638 Loop_Parameter_Specification =>
639 Make_Loop_Parameter_Specification (Loc,
640 Defining_Identifier => Index,
641 Discrete_Subtype_Definition =>
642 Make_Attribute_Reference (Loc,
643 Prefix =>
644 Make_Identifier (Loc, Name_uInit),
645 Attribute_Name => Name_Range,
646 Expressions => New_List (
647 Make_Integer_Literal (Loc, N))))),
648 Statements => Init_One_Dimension (N + 1)));
649 end if;
650 end Init_One_Dimension;
652 -- Start of processing for Build_Array_Init_Proc
654 begin
655 -- The init proc is created when analyzing the freeze node for the type,
656 -- but it properly belongs with the array type declaration. However, if
657 -- the freeze node is for a subtype of a type declared in another unit
658 -- it seems preferable to use the freeze node as the source location of
659 -- the init proc. In any case this is preferable for gcov usage, and
660 -- the Sloc is not otherwise used by the compiler.
662 if In_Open_Scopes (Scope (A_Type)) then
663 Loc := Sloc (A_Type);
664 else
665 Loc := Sloc (Nod);
666 end if;
668 -- Nothing to generate in the following cases:
670 -- 1. Initialization is suppressed for the type
671 -- 2. The type is a value type, in the CIL sense.
672 -- 3. The type has CIL/JVM convention.
673 -- 4. An initialization already exists for the base type
675 if Initialization_Suppressed (A_Type)
676 or else Is_Value_Type (Comp_Type)
677 or else Convention (A_Type) = Convention_CIL
678 or else Convention (A_Type) = Convention_Java
679 or else Present (Base_Init_Proc (A_Type))
680 then
681 return;
682 end if;
684 Index_List := New_List;
686 -- We need an initialization procedure if any of the following is true:
688 -- 1. The component type has an initialization procedure
689 -- 2. The component type needs simple initialization
690 -- 3. Tasks are present
691 -- 4. The type is marked as a public entity
692 -- 5. The array type has a Default_Component_Value aspect
694 -- The reason for the public entity test is to deal properly with the
695 -- Initialize_Scalars pragma. This pragma can be set in the client and
696 -- not in the declaring package, this means the client will make a call
697 -- to the initialization procedure (because one of conditions 1-3 must
698 -- apply in this case), and we must generate a procedure (even if it is
699 -- null) to satisfy the call in this case.
701 -- Exception: do not build an array init_proc for a type whose root
702 -- type is Standard.String or Standard.Wide_[Wide_]String, since there
703 -- is no place to put the code, and in any case we handle initialization
704 -- of such types (in the Initialize_Scalars case, that's the only time
705 -- the issue arises) in a special manner anyway which does not need an
706 -- init_proc.
708 Has_Default_Init := Has_Non_Null_Base_Init_Proc (Comp_Type)
709 or else Needs_Simple_Initialization (Comp_Type)
710 or else Has_Task (Comp_Type)
711 or else Has_Default_Aspect (A_Type);
713 if Has_Default_Init
714 or else (not Restriction_Active (No_Initialize_Scalars)
715 and then Is_Public (A_Type)
716 and then not Is_Standard_String_Type (A_Type))
717 then
718 Proc_Id :=
719 Make_Defining_Identifier (Loc,
720 Chars => Make_Init_Proc_Name (A_Type));
722 -- If No_Default_Initialization restriction is active, then we don't
723 -- want to build an init_proc, but we need to mark that an init_proc
724 -- would be needed if this restriction was not active (so that we can
725 -- detect attempts to call it), so set a dummy init_proc in place.
726 -- This is only done though when actual default initialization is
727 -- needed (and not done when only Is_Public is True), since otherwise
728 -- objects such as arrays of scalars could be wrongly flagged as
729 -- violating the restriction.
731 if Restriction_Active (No_Default_Initialization) then
732 if Has_Default_Init then
733 Set_Init_Proc (A_Type, Proc_Id);
734 end if;
736 return;
737 end if;
739 Body_Stmts := Init_One_Dimension (1);
741 Discard_Node (
742 Make_Subprogram_Body (Loc,
743 Specification =>
744 Make_Procedure_Specification (Loc,
745 Defining_Unit_Name => Proc_Id,
746 Parameter_Specifications => Init_Formals (A_Type)),
747 Declarations => New_List,
748 Handled_Statement_Sequence =>
749 Make_Handled_Sequence_Of_Statements (Loc,
750 Statements => Body_Stmts)));
752 Set_Ekind (Proc_Id, E_Procedure);
753 Set_Is_Public (Proc_Id, Is_Public (A_Type));
754 Set_Is_Internal (Proc_Id);
755 Set_Has_Completion (Proc_Id);
757 if not Debug_Generated_Code then
758 Set_Debug_Info_Off (Proc_Id);
759 end if;
761 -- Set inlined unless controlled stuff or tasks around, in which
762 -- case we do not want to inline, because nested stuff may cause
763 -- difficulties in inter-unit inlining, and furthermore there is
764 -- in any case no point in inlining such complex init procs.
766 if not Has_Task (Proc_Id)
767 and then not Needs_Finalization (Proc_Id)
768 then
769 Set_Is_Inlined (Proc_Id);
770 end if;
772 -- Associate Init_Proc with type, and determine if the procedure
773 -- is null (happens because of the Initialize_Scalars pragma case,
774 -- where we have to generate a null procedure in case it is called
775 -- by a client with Initialize_Scalars set). Such procedures have
776 -- to be generated, but do not have to be called, so we mark them
777 -- as null to suppress the call.
779 Set_Init_Proc (A_Type, Proc_Id);
781 if List_Length (Body_Stmts) = 1
783 -- We must skip SCIL nodes because they may have been added to this
784 -- list by Insert_Actions.
786 and then Nkind (First_Non_SCIL_Node (Body_Stmts)) = N_Null_Statement
787 then
788 Set_Is_Null_Init_Proc (Proc_Id);
790 else
791 -- Try to build a static aggregate to statically initialize
792 -- objects of the type. This can only be done for constrained
793 -- one-dimensional arrays with static bounds.
795 Set_Static_Initialization
796 (Proc_Id,
797 Build_Equivalent_Array_Aggregate (First_Subtype (A_Type)));
798 end if;
799 end if;
800 end Build_Array_Init_Proc;
802 --------------------------------
803 -- Build_Array_Invariant_Proc --
804 --------------------------------
806 function Build_Array_Invariant_Proc
807 (A_Type : Entity_Id;
808 Nod : Node_Id) return Node_Id
810 Loc : constant Source_Ptr := Sloc (Nod);
812 Object_Name : constant Name_Id := New_Internal_Name ('I');
813 -- Name for argument of invariant procedure
815 Object_Entity : constant Node_Id :=
816 Make_Defining_Identifier (Loc, Object_Name);
817 -- The procedure declaration entity for the argument
819 Body_Stmts : List_Id;
820 Index_List : List_Id;
821 Proc_Id : Entity_Id;
822 Proc_Body : Node_Id;
824 function Build_Component_Invariant_Call return Node_Id;
825 -- Create one statement to verify invariant on one array component,
826 -- designated by a full set of indexes.
828 function Check_One_Dimension (N : Int) return List_Id;
829 -- Create loop to check on one dimension of the array. The single
830 -- statement in the loop body checks the inner dimensions if any, or
831 -- else a single component. This procedure is called recursively, with
832 -- N being the dimension to be initialized. A call with N greater than
833 -- the number of dimensions generates the component initialization
834 -- and terminates the recursion.
836 ------------------------------------
837 -- Build_Component_Invariant_Call --
838 ------------------------------------
840 function Build_Component_Invariant_Call return Node_Id is
841 Comp : Node_Id;
842 begin
843 Comp :=
844 Make_Indexed_Component (Loc,
845 Prefix => New_Occurrence_Of (Object_Entity, Loc),
846 Expressions => Index_List);
847 return
848 Make_Procedure_Call_Statement (Loc,
849 Name =>
850 New_Occurrence_Of
851 (Invariant_Procedure (Component_Type (A_Type)), Loc),
852 Parameter_Associations => New_List (Comp));
853 end Build_Component_Invariant_Call;
855 -------------------------
856 -- Check_One_Dimension --
857 -------------------------
859 function Check_One_Dimension (N : Int) return List_Id is
860 Index : Entity_Id;
862 begin
863 -- If all dimensions dealt with, we simply check invariant of the
864 -- component.
866 if N > Number_Dimensions (A_Type) then
867 return New_List (Build_Component_Invariant_Call);
869 -- Else generate one loop and recurse
871 else
872 Index :=
873 Make_Defining_Identifier (Loc, New_External_Name ('J', N));
875 Append (New_Occurrence_Of (Index, Loc), Index_List);
877 return New_List (
878 Make_Implicit_Loop_Statement (Nod,
879 Identifier => Empty,
880 Iteration_Scheme =>
881 Make_Iteration_Scheme (Loc,
882 Loop_Parameter_Specification =>
883 Make_Loop_Parameter_Specification (Loc,
884 Defining_Identifier => Index,
885 Discrete_Subtype_Definition =>
886 Make_Attribute_Reference (Loc,
887 Prefix =>
888 New_Occurrence_Of (Object_Entity, Loc),
889 Attribute_Name => Name_Range,
890 Expressions => New_List (
891 Make_Integer_Literal (Loc, N))))),
892 Statements => Check_One_Dimension (N + 1)));
893 end if;
894 end Check_One_Dimension;
896 -- Start of processing for Build_Array_Invariant_Proc
898 begin
899 Index_List := New_List;
901 Proc_Id :=
902 Make_Defining_Identifier (Loc,
903 Chars => New_External_Name (Chars (A_Type), "CInvariant"));
905 Body_Stmts := Check_One_Dimension (1);
907 Proc_Body :=
908 Make_Subprogram_Body (Loc,
909 Specification =>
910 Make_Procedure_Specification (Loc,
911 Defining_Unit_Name => Proc_Id,
912 Parameter_Specifications => New_List (
913 Make_Parameter_Specification (Loc,
914 Defining_Identifier => Object_Entity,
915 Parameter_Type => New_Occurrence_Of (A_Type, Loc)))),
917 Declarations => Empty_List,
918 Handled_Statement_Sequence =>
919 Make_Handled_Sequence_Of_Statements (Loc,
920 Statements => Body_Stmts));
922 Set_Ekind (Proc_Id, E_Procedure);
923 Set_Is_Public (Proc_Id, Is_Public (A_Type));
924 Set_Is_Internal (Proc_Id);
925 Set_Has_Completion (Proc_Id);
927 if not Debug_Generated_Code then
928 Set_Debug_Info_Off (Proc_Id);
929 end if;
931 return Proc_Body;
932 end Build_Array_Invariant_Proc;
934 --------------------------------
935 -- Build_Discr_Checking_Funcs --
936 --------------------------------
938 procedure Build_Discr_Checking_Funcs (N : Node_Id) is
939 Rec_Id : Entity_Id;
940 Loc : Source_Ptr;
941 Enclosing_Func_Id : Entity_Id;
942 Sequence : Nat := 1;
943 Type_Def : Node_Id;
944 V : Node_Id;
946 function Build_Case_Statement
947 (Case_Id : Entity_Id;
948 Variant : Node_Id) return Node_Id;
949 -- Build a case statement containing only two alternatives. The first
950 -- alternative corresponds exactly to the discrete choices given on the
951 -- variant with contains the components that we are generating the
952 -- checks for. If the discriminant is one of these return False. The
953 -- second alternative is an OTHERS choice that will return True
954 -- indicating the discriminant did not match.
956 function Build_Dcheck_Function
957 (Case_Id : Entity_Id;
958 Variant : Node_Id) return Entity_Id;
959 -- Build the discriminant checking function for a given variant
961 procedure Build_Dcheck_Functions (Variant_Part_Node : Node_Id);
962 -- Builds the discriminant checking function for each variant of the
963 -- given variant part of the record type.
965 --------------------------
966 -- Build_Case_Statement --
967 --------------------------
969 function Build_Case_Statement
970 (Case_Id : Entity_Id;
971 Variant : Node_Id) return Node_Id
973 Alt_List : constant List_Id := New_List;
974 Actuals_List : List_Id;
975 Case_Node : Node_Id;
976 Case_Alt_Node : Node_Id;
977 Choice : Node_Id;
978 Choice_List : List_Id;
979 D : Entity_Id;
980 Return_Node : Node_Id;
982 begin
983 Case_Node := New_Node (N_Case_Statement, Loc);
985 -- Replace the discriminant which controls the variant with the name
986 -- of the formal of the checking function.
988 Set_Expression (Case_Node, Make_Identifier (Loc, Chars (Case_Id)));
990 Choice := First (Discrete_Choices (Variant));
992 if Nkind (Choice) = N_Others_Choice then
993 Choice_List := New_Copy_List (Others_Discrete_Choices (Choice));
994 else
995 Choice_List := New_Copy_List (Discrete_Choices (Variant));
996 end if;
998 if not Is_Empty_List (Choice_List) then
999 Case_Alt_Node := New_Node (N_Case_Statement_Alternative, Loc);
1000 Set_Discrete_Choices (Case_Alt_Node, Choice_List);
1002 -- In case this is a nested variant, we need to return the result
1003 -- of the discriminant checking function for the immediately
1004 -- enclosing variant.
1006 if Present (Enclosing_Func_Id) then
1007 Actuals_List := New_List;
1009 D := First_Discriminant (Rec_Id);
1010 while Present (D) loop
1011 Append (Make_Identifier (Loc, Chars (D)), Actuals_List);
1012 Next_Discriminant (D);
1013 end loop;
1015 Return_Node :=
1016 Make_Simple_Return_Statement (Loc,
1017 Expression =>
1018 Make_Function_Call (Loc,
1019 Name =>
1020 New_Occurrence_Of (Enclosing_Func_Id, Loc),
1021 Parameter_Associations =>
1022 Actuals_List));
1024 else
1025 Return_Node :=
1026 Make_Simple_Return_Statement (Loc,
1027 Expression =>
1028 New_Occurrence_Of (Standard_False, Loc));
1029 end if;
1031 Set_Statements (Case_Alt_Node, New_List (Return_Node));
1032 Append (Case_Alt_Node, Alt_List);
1033 end if;
1035 Case_Alt_Node := New_Node (N_Case_Statement_Alternative, Loc);
1036 Choice_List := New_List (New_Node (N_Others_Choice, Loc));
1037 Set_Discrete_Choices (Case_Alt_Node, Choice_List);
1039 Return_Node :=
1040 Make_Simple_Return_Statement (Loc,
1041 Expression =>
1042 New_Occurrence_Of (Standard_True, Loc));
1044 Set_Statements (Case_Alt_Node, New_List (Return_Node));
1045 Append (Case_Alt_Node, Alt_List);
1047 Set_Alternatives (Case_Node, Alt_List);
1048 return Case_Node;
1049 end Build_Case_Statement;
1051 ---------------------------
1052 -- Build_Dcheck_Function --
1053 ---------------------------
1055 function Build_Dcheck_Function
1056 (Case_Id : Entity_Id;
1057 Variant : Node_Id) return Entity_Id
1059 Body_Node : Node_Id;
1060 Func_Id : Entity_Id;
1061 Parameter_List : List_Id;
1062 Spec_Node : Node_Id;
1064 begin
1065 Body_Node := New_Node (N_Subprogram_Body, Loc);
1066 Sequence := Sequence + 1;
1068 Func_Id :=
1069 Make_Defining_Identifier (Loc,
1070 Chars => New_External_Name (Chars (Rec_Id), 'D', Sequence));
1071 Set_Is_Discriminant_Check_Function (Func_Id);
1073 Spec_Node := New_Node (N_Function_Specification, Loc);
1074 Set_Defining_Unit_Name (Spec_Node, Func_Id);
1076 Parameter_List := Build_Discriminant_Formals (Rec_Id, False);
1078 Set_Parameter_Specifications (Spec_Node, Parameter_List);
1079 Set_Result_Definition (Spec_Node,
1080 New_Occurrence_Of (Standard_Boolean, Loc));
1081 Set_Specification (Body_Node, Spec_Node);
1082 Set_Declarations (Body_Node, New_List);
1084 Set_Handled_Statement_Sequence (Body_Node,
1085 Make_Handled_Sequence_Of_Statements (Loc,
1086 Statements => New_List (
1087 Build_Case_Statement (Case_Id, Variant))));
1089 Set_Ekind (Func_Id, E_Function);
1090 Set_Mechanism (Func_Id, Default_Mechanism);
1091 Set_Is_Inlined (Func_Id, True);
1092 Set_Is_Pure (Func_Id, True);
1093 Set_Is_Public (Func_Id, Is_Public (Rec_Id));
1094 Set_Is_Internal (Func_Id, True);
1096 if not Debug_Generated_Code then
1097 Set_Debug_Info_Off (Func_Id);
1098 end if;
1100 Analyze (Body_Node);
1102 Append_Freeze_Action (Rec_Id, Body_Node);
1103 Set_Dcheck_Function (Variant, Func_Id);
1104 return Func_Id;
1105 end Build_Dcheck_Function;
1107 ----------------------------
1108 -- Build_Dcheck_Functions --
1109 ----------------------------
1111 procedure Build_Dcheck_Functions (Variant_Part_Node : Node_Id) is
1112 Component_List_Node : Node_Id;
1113 Decl : Entity_Id;
1114 Discr_Name : Entity_Id;
1115 Func_Id : Entity_Id;
1116 Variant : Node_Id;
1117 Saved_Enclosing_Func_Id : Entity_Id;
1119 begin
1120 -- Build the discriminant-checking function for each variant, and
1121 -- label all components of that variant with the function's name.
1122 -- We only Generate a discriminant-checking function when the
1123 -- variant is not empty, to prevent the creation of dead code.
1124 -- The exception to that is when Frontend_Layout_On_Target is set,
1125 -- because the variant record size function generated in package
1126 -- Layout needs to generate calls to all discriminant-checking
1127 -- functions, including those for empty variants.
1129 Discr_Name := Entity (Name (Variant_Part_Node));
1130 Variant := First_Non_Pragma (Variants (Variant_Part_Node));
1132 while Present (Variant) loop
1133 Component_List_Node := Component_List (Variant);
1135 if not Null_Present (Component_List_Node)
1136 or else Frontend_Layout_On_Target
1137 then
1138 Func_Id := Build_Dcheck_Function (Discr_Name, Variant);
1139 Decl :=
1140 First_Non_Pragma (Component_Items (Component_List_Node));
1142 while Present (Decl) loop
1143 Set_Discriminant_Checking_Func
1144 (Defining_Identifier (Decl), Func_Id);
1146 Next_Non_Pragma (Decl);
1147 end loop;
1149 if Present (Variant_Part (Component_List_Node)) then
1150 Saved_Enclosing_Func_Id := Enclosing_Func_Id;
1151 Enclosing_Func_Id := Func_Id;
1152 Build_Dcheck_Functions (Variant_Part (Component_List_Node));
1153 Enclosing_Func_Id := Saved_Enclosing_Func_Id;
1154 end if;
1155 end if;
1157 Next_Non_Pragma (Variant);
1158 end loop;
1159 end Build_Dcheck_Functions;
1161 -- Start of processing for Build_Discr_Checking_Funcs
1163 begin
1164 -- Only build if not done already
1166 if not Discr_Check_Funcs_Built (N) then
1167 Type_Def := Type_Definition (N);
1169 if Nkind (Type_Def) = N_Record_Definition then
1170 if No (Component_List (Type_Def)) then -- null record.
1171 return;
1172 else
1173 V := Variant_Part (Component_List (Type_Def));
1174 end if;
1176 else pragma Assert (Nkind (Type_Def) = N_Derived_Type_Definition);
1177 if No (Component_List (Record_Extension_Part (Type_Def))) then
1178 return;
1179 else
1180 V := Variant_Part
1181 (Component_List (Record_Extension_Part (Type_Def)));
1182 end if;
1183 end if;
1185 Rec_Id := Defining_Identifier (N);
1187 if Present (V) and then not Is_Unchecked_Union (Rec_Id) then
1188 Loc := Sloc (N);
1189 Enclosing_Func_Id := Empty;
1190 Build_Dcheck_Functions (V);
1191 end if;
1193 Set_Discr_Check_Funcs_Built (N);
1194 end if;
1195 end Build_Discr_Checking_Funcs;
1197 --------------------------------
1198 -- Build_Discriminant_Formals --
1199 --------------------------------
1201 function Build_Discriminant_Formals
1202 (Rec_Id : Entity_Id;
1203 Use_Dl : Boolean) return List_Id
1205 Loc : Source_Ptr := Sloc (Rec_Id);
1206 Parameter_List : constant List_Id := New_List;
1207 D : Entity_Id;
1208 Formal : Entity_Id;
1209 Formal_Type : Entity_Id;
1210 Param_Spec_Node : Node_Id;
1212 begin
1213 if Has_Discriminants (Rec_Id) then
1214 D := First_Discriminant (Rec_Id);
1215 while Present (D) loop
1216 Loc := Sloc (D);
1218 if Use_Dl then
1219 Formal := Discriminal (D);
1220 Formal_Type := Etype (Formal);
1221 else
1222 Formal := Make_Defining_Identifier (Loc, Chars (D));
1223 Formal_Type := Etype (D);
1224 end if;
1226 Param_Spec_Node :=
1227 Make_Parameter_Specification (Loc,
1228 Defining_Identifier => Formal,
1229 Parameter_Type =>
1230 New_Occurrence_Of (Formal_Type, Loc));
1231 Append (Param_Spec_Node, Parameter_List);
1232 Next_Discriminant (D);
1233 end loop;
1234 end if;
1236 return Parameter_List;
1237 end Build_Discriminant_Formals;
1239 --------------------------------------
1240 -- Build_Equivalent_Array_Aggregate --
1241 --------------------------------------
1243 function Build_Equivalent_Array_Aggregate (T : Entity_Id) return Node_Id is
1244 Loc : constant Source_Ptr := Sloc (T);
1245 Comp_Type : constant Entity_Id := Component_Type (T);
1246 Index_Type : constant Entity_Id := Etype (First_Index (T));
1247 Proc : constant Entity_Id := Base_Init_Proc (T);
1248 Lo, Hi : Node_Id;
1249 Aggr : Node_Id;
1250 Expr : Node_Id;
1252 begin
1253 if not Is_Constrained (T)
1254 or else Number_Dimensions (T) > 1
1255 or else No (Proc)
1256 then
1257 Initialization_Warning (T);
1258 return Empty;
1259 end if;
1261 Lo := Type_Low_Bound (Index_Type);
1262 Hi := Type_High_Bound (Index_Type);
1264 if not Compile_Time_Known_Value (Lo)
1265 or else not Compile_Time_Known_Value (Hi)
1266 then
1267 Initialization_Warning (T);
1268 return Empty;
1269 end if;
1271 if Is_Record_Type (Comp_Type)
1272 and then Present (Base_Init_Proc (Comp_Type))
1273 then
1274 Expr := Static_Initialization (Base_Init_Proc (Comp_Type));
1276 if No (Expr) then
1277 Initialization_Warning (T);
1278 return Empty;
1279 end if;
1281 else
1282 Initialization_Warning (T);
1283 return Empty;
1284 end if;
1286 Aggr := Make_Aggregate (Loc, No_List, New_List);
1287 Set_Etype (Aggr, T);
1288 Set_Aggregate_Bounds (Aggr,
1289 Make_Range (Loc,
1290 Low_Bound => New_Copy (Lo),
1291 High_Bound => New_Copy (Hi)));
1292 Set_Parent (Aggr, Parent (Proc));
1294 Append_To (Component_Associations (Aggr),
1295 Make_Component_Association (Loc,
1296 Choices =>
1297 New_List (
1298 Make_Range (Loc,
1299 Low_Bound => New_Copy (Lo),
1300 High_Bound => New_Copy (Hi))),
1301 Expression => Expr));
1303 if Static_Array_Aggregate (Aggr) then
1304 return Aggr;
1305 else
1306 Initialization_Warning (T);
1307 return Empty;
1308 end if;
1309 end Build_Equivalent_Array_Aggregate;
1311 ---------------------------------------
1312 -- Build_Equivalent_Record_Aggregate --
1313 ---------------------------------------
1315 function Build_Equivalent_Record_Aggregate (T : Entity_Id) return Node_Id is
1316 Agg : Node_Id;
1317 Comp : Entity_Id;
1318 Comp_Type : Entity_Id;
1320 -- Start of processing for Build_Equivalent_Record_Aggregate
1322 begin
1323 if not Is_Record_Type (T)
1324 or else Has_Discriminants (T)
1325 or else Is_Limited_Type (T)
1326 or else Has_Non_Standard_Rep (T)
1327 then
1328 Initialization_Warning (T);
1329 return Empty;
1330 end if;
1332 Comp := First_Component (T);
1334 -- A null record needs no warning
1336 if No (Comp) then
1337 return Empty;
1338 end if;
1340 while Present (Comp) loop
1342 -- Array components are acceptable if initialized by a positional
1343 -- aggregate with static components.
1345 if Is_Array_Type (Etype (Comp)) then
1346 Comp_Type := Component_Type (Etype (Comp));
1348 if Nkind (Parent (Comp)) /= N_Component_Declaration
1349 or else No (Expression (Parent (Comp)))
1350 or else Nkind (Expression (Parent (Comp))) /= N_Aggregate
1351 then
1352 Initialization_Warning (T);
1353 return Empty;
1355 elsif Is_Scalar_Type (Component_Type (Etype (Comp)))
1356 and then
1357 (not Compile_Time_Known_Value (Type_Low_Bound (Comp_Type))
1358 or else
1359 not Compile_Time_Known_Value (Type_High_Bound (Comp_Type)))
1360 then
1361 Initialization_Warning (T);
1362 return Empty;
1364 elsif
1365 not Static_Array_Aggregate (Expression (Parent (Comp)))
1366 then
1367 Initialization_Warning (T);
1368 return Empty;
1369 end if;
1371 elsif Is_Scalar_Type (Etype (Comp)) then
1372 Comp_Type := Etype (Comp);
1374 if Nkind (Parent (Comp)) /= N_Component_Declaration
1375 or else No (Expression (Parent (Comp)))
1376 or else not Compile_Time_Known_Value (Expression (Parent (Comp)))
1377 or else not Compile_Time_Known_Value (Type_Low_Bound (Comp_Type))
1378 or else not
1379 Compile_Time_Known_Value (Type_High_Bound (Comp_Type))
1380 then
1381 Initialization_Warning (T);
1382 return Empty;
1383 end if;
1385 -- For now, other types are excluded
1387 else
1388 Initialization_Warning (T);
1389 return Empty;
1390 end if;
1392 Next_Component (Comp);
1393 end loop;
1395 -- All components have static initialization. Build positional aggregate
1396 -- from the given expressions or defaults.
1398 Agg := Make_Aggregate (Sloc (T), New_List, New_List);
1399 Set_Parent (Agg, Parent (T));
1401 Comp := First_Component (T);
1402 while Present (Comp) loop
1403 Append
1404 (New_Copy_Tree (Expression (Parent (Comp))), Expressions (Agg));
1405 Next_Component (Comp);
1406 end loop;
1408 Analyze_And_Resolve (Agg, T);
1409 return Agg;
1410 end Build_Equivalent_Record_Aggregate;
1412 -------------------------------
1413 -- Build_Initialization_Call --
1414 -------------------------------
1416 -- References to a discriminant inside the record type declaration can
1417 -- appear either in the subtype_indication to constrain a record or an
1418 -- array, or as part of a larger expression given for the initial value
1419 -- of a component. In both of these cases N appears in the record
1420 -- initialization procedure and needs to be replaced by the formal
1421 -- parameter of the initialization procedure which corresponds to that
1422 -- discriminant.
1424 -- In the example below, references to discriminants D1 and D2 in proc_1
1425 -- are replaced by references to formals with the same name
1426 -- (discriminals)
1428 -- A similar replacement is done for calls to any record initialization
1429 -- procedure for any components that are themselves of a record type.
1431 -- type R (D1, D2 : Integer) is record
1432 -- X : Integer := F * D1;
1433 -- Y : Integer := F * D2;
1434 -- end record;
1436 -- procedure proc_1 (Out_2 : out R; D1 : Integer; D2 : Integer) is
1437 -- begin
1438 -- Out_2.D1 := D1;
1439 -- Out_2.D2 := D2;
1440 -- Out_2.X := F * D1;
1441 -- Out_2.Y := F * D2;
1442 -- end;
1444 function Build_Initialization_Call
1445 (Loc : Source_Ptr;
1446 Id_Ref : Node_Id;
1447 Typ : Entity_Id;
1448 In_Init_Proc : Boolean := False;
1449 Enclos_Type : Entity_Id := Empty;
1450 Discr_Map : Elist_Id := New_Elmt_List;
1451 With_Default_Init : Boolean := False;
1452 Constructor_Ref : Node_Id := Empty) return List_Id
1454 Res : constant List_Id := New_List;
1455 Arg : Node_Id;
1456 Args : List_Id;
1457 Decls : List_Id;
1458 Decl : Node_Id;
1459 Discr : Entity_Id;
1460 First_Arg : Node_Id;
1461 Full_Init_Type : Entity_Id;
1462 Full_Type : Entity_Id := Typ;
1463 Init_Type : Entity_Id;
1464 Proc : Entity_Id;
1466 begin
1467 pragma Assert (Constructor_Ref = Empty
1468 or else Is_CPP_Constructor_Call (Constructor_Ref));
1470 if No (Constructor_Ref) then
1471 Proc := Base_Init_Proc (Typ);
1472 else
1473 Proc := Base_Init_Proc (Typ, Entity (Name (Constructor_Ref)));
1474 end if;
1476 pragma Assert (Present (Proc));
1477 Init_Type := Etype (First_Formal (Proc));
1478 Full_Init_Type := Underlying_Type (Init_Type);
1480 -- Nothing to do if the Init_Proc is null, unless Initialize_Scalars
1481 -- is active (in which case we make the call anyway, since in the
1482 -- actual compiled client it may be non null).
1483 -- Also nothing to do for value types.
1485 if (Is_Null_Init_Proc (Proc) and then not Init_Or_Norm_Scalars)
1486 or else Is_Value_Type (Typ)
1487 or else
1488 (Is_Array_Type (Typ) and then Is_Value_Type (Component_Type (Typ)))
1489 then
1490 return Empty_List;
1491 end if;
1493 -- Go to full view or underlying full view if private type. In the case
1494 -- of successive private derivations, this can require two steps.
1496 if Is_Private_Type (Full_Type)
1497 and then Present (Full_View (Full_Type))
1498 then
1499 Full_Type := Full_View (Full_Type);
1500 end if;
1502 if Is_Private_Type (Full_Type)
1503 and then Present (Underlying_Full_View (Full_Type))
1504 then
1505 Full_Type := Underlying_Full_View (Full_Type);
1506 end if;
1508 -- If Typ is derived, the procedure is the initialization procedure for
1509 -- the root type. Wrap the argument in an conversion to make it type
1510 -- honest. Actually it isn't quite type honest, because there can be
1511 -- conflicts of views in the private type case. That is why we set
1512 -- Conversion_OK in the conversion node.
1514 if (Is_Record_Type (Typ)
1515 or else Is_Array_Type (Typ)
1516 or else Is_Private_Type (Typ))
1517 and then Init_Type /= Base_Type (Typ)
1518 then
1519 First_Arg := OK_Convert_To (Etype (Init_Type), Id_Ref);
1520 Set_Etype (First_Arg, Init_Type);
1522 else
1523 First_Arg := Id_Ref;
1524 end if;
1526 Args := New_List (Convert_Concurrent (First_Arg, Typ));
1528 -- In the tasks case, add _Master as the value of the _Master parameter
1529 -- and _Chain as the value of the _Chain parameter. At the outer level,
1530 -- these will be variables holding the corresponding values obtained
1531 -- from GNARL. At inner levels, they will be the parameters passed down
1532 -- through the outer routines.
1534 if Has_Task (Full_Type) then
1535 if Restriction_Active (No_Task_Hierarchy) then
1536 Append_To (Args,
1537 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
1538 else
1539 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
1540 end if;
1542 -- Add _Chain (not done for sequential elaboration policy, see
1543 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
1545 if Partition_Elaboration_Policy /= 'S' then
1546 Append_To (Args, Make_Identifier (Loc, Name_uChain));
1547 end if;
1549 -- Ada 2005 (AI-287): In case of default initialized components
1550 -- with tasks, we generate a null string actual parameter.
1551 -- This is just a workaround that must be improved later???
1553 if With_Default_Init then
1554 Append_To (Args,
1555 Make_String_Literal (Loc,
1556 Strval => ""));
1558 else
1559 Decls :=
1560 Build_Task_Image_Decls (Loc, Id_Ref, Enclos_Type, In_Init_Proc);
1561 Decl := Last (Decls);
1563 Append_To (Args,
1564 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
1565 Append_List (Decls, Res);
1566 end if;
1568 else
1569 Decls := No_List;
1570 Decl := Empty;
1571 end if;
1573 -- Add discriminant values if discriminants are present
1575 if Has_Discriminants (Full_Init_Type) then
1576 Discr := First_Discriminant (Full_Init_Type);
1578 while Present (Discr) loop
1580 -- If this is a discriminated concurrent type, the init_proc
1581 -- for the corresponding record is being called. Use that type
1582 -- directly to find the discriminant value, to handle properly
1583 -- intervening renamed discriminants.
1585 declare
1586 T : Entity_Id := Full_Type;
1588 begin
1589 if Is_Protected_Type (T) then
1590 T := Corresponding_Record_Type (T);
1591 end if;
1593 Arg :=
1594 Get_Discriminant_Value (
1595 Discr,
1597 Discriminant_Constraint (Full_Type));
1598 end;
1600 -- If the target has access discriminants, and is constrained by
1601 -- an access to the enclosing construct, i.e. a current instance,
1602 -- replace the reference to the type by a reference to the object.
1604 if Nkind (Arg) = N_Attribute_Reference
1605 and then Is_Access_Type (Etype (Arg))
1606 and then Is_Entity_Name (Prefix (Arg))
1607 and then Is_Type (Entity (Prefix (Arg)))
1608 then
1609 Arg :=
1610 Make_Attribute_Reference (Loc,
1611 Prefix => New_Copy (Prefix (Id_Ref)),
1612 Attribute_Name => Name_Unrestricted_Access);
1614 elsif In_Init_Proc then
1616 -- Replace any possible references to the discriminant in the
1617 -- call to the record initialization procedure with references
1618 -- to the appropriate formal parameter.
1620 if Nkind (Arg) = N_Identifier
1621 and then Ekind (Entity (Arg)) = E_Discriminant
1622 then
1623 Arg := New_Occurrence_Of (Discriminal (Entity (Arg)), Loc);
1625 -- Otherwise make a copy of the default expression. Note that
1626 -- we use the current Sloc for this, because we do not want the
1627 -- call to appear to be at the declaration point. Within the
1628 -- expression, replace discriminants with their discriminals.
1630 else
1631 Arg :=
1632 New_Copy_Tree (Arg, Map => Discr_Map, New_Sloc => Loc);
1633 end if;
1635 else
1636 if Is_Constrained (Full_Type) then
1637 Arg := Duplicate_Subexpr_No_Checks (Arg);
1638 else
1639 -- The constraints come from the discriminant default exps,
1640 -- they must be reevaluated, so we use New_Copy_Tree but we
1641 -- ensure the proper Sloc (for any embedded calls).
1643 Arg := New_Copy_Tree (Arg, New_Sloc => Loc);
1644 end if;
1645 end if;
1647 -- Ada 2005 (AI-287): In case of default initialized components,
1648 -- if the component is constrained with a discriminant of the
1649 -- enclosing type, we need to generate the corresponding selected
1650 -- component node to access the discriminant value. In other cases
1651 -- this is not required, either because we are inside the init
1652 -- proc and we use the corresponding formal, or else because the
1653 -- component is constrained by an expression.
1655 if With_Default_Init
1656 and then Nkind (Id_Ref) = N_Selected_Component
1657 and then Nkind (Arg) = N_Identifier
1658 and then Ekind (Entity (Arg)) = E_Discriminant
1659 then
1660 Append_To (Args,
1661 Make_Selected_Component (Loc,
1662 Prefix => New_Copy_Tree (Prefix (Id_Ref)),
1663 Selector_Name => Arg));
1664 else
1665 Append_To (Args, Arg);
1666 end if;
1668 Next_Discriminant (Discr);
1669 end loop;
1670 end if;
1672 -- If this is a call to initialize the parent component of a derived
1673 -- tagged type, indicate that the tag should not be set in the parent.
1675 if Is_Tagged_Type (Full_Init_Type)
1676 and then not Is_CPP_Class (Full_Init_Type)
1677 and then Nkind (Id_Ref) = N_Selected_Component
1678 and then Chars (Selector_Name (Id_Ref)) = Name_uParent
1679 then
1680 Append_To (Args, New_Occurrence_Of (Standard_False, Loc));
1682 elsif Present (Constructor_Ref) then
1683 Append_List_To (Args,
1684 New_Copy_List (Parameter_Associations (Constructor_Ref)));
1685 end if;
1687 Append_To (Res,
1688 Make_Procedure_Call_Statement (Loc,
1689 Name => New_Occurrence_Of (Proc, Loc),
1690 Parameter_Associations => Args));
1692 if Needs_Finalization (Typ)
1693 and then Nkind (Id_Ref) = N_Selected_Component
1694 then
1695 if Chars (Selector_Name (Id_Ref)) /= Name_uParent then
1696 Append_To (Res,
1697 Make_Init_Call
1698 (Obj_Ref => New_Copy_Tree (First_Arg),
1699 Typ => Typ));
1700 end if;
1701 end if;
1703 return Res;
1705 exception
1706 when RE_Not_Available =>
1707 return Empty_List;
1708 end Build_Initialization_Call;
1710 ----------------------------
1711 -- Build_Record_Init_Proc --
1712 ----------------------------
1714 procedure Build_Record_Init_Proc (N : Node_Id; Rec_Ent : Entity_Id) is
1715 Decls : constant List_Id := New_List;
1716 Discr_Map : constant Elist_Id := New_Elmt_List;
1717 Loc : constant Source_Ptr := Sloc (Rec_Ent);
1718 Counter : Int := 0;
1719 Proc_Id : Entity_Id;
1720 Rec_Type : Entity_Id;
1721 Set_Tag : Entity_Id := Empty;
1723 function Build_Assignment (Id : Entity_Id; N : Node_Id) return List_Id;
1724 -- Build an assignment statement which assigns the default expression
1725 -- to its corresponding record component if defined. The left hand side
1726 -- of the assignment is marked Assignment_OK so that initialization of
1727 -- limited private records works correctly. This routine may also build
1728 -- an adjustment call if the component is controlled.
1730 procedure Build_Discriminant_Assignments (Statement_List : List_Id);
1731 -- If the record has discriminants, add assignment statements to
1732 -- Statement_List to initialize the discriminant values from the
1733 -- arguments of the initialization procedure.
1735 function Build_Init_Statements (Comp_List : Node_Id) return List_Id;
1736 -- Build a list representing a sequence of statements which initialize
1737 -- components of the given component list. This may involve building
1738 -- case statements for the variant parts. Append any locally declared
1739 -- objects on list Decls.
1741 function Build_Init_Call_Thru (Parameters : List_Id) return List_Id;
1742 -- Given an untagged type-derivation that declares discriminants, e.g.
1744 -- type R (R1, R2 : Integer) is record ... end record;
1745 -- type D (D1 : Integer) is new R (1, D1);
1747 -- we make the _init_proc of D be
1749 -- procedure _init_proc (X : D; D1 : Integer) is
1750 -- begin
1751 -- _init_proc (R (X), 1, D1);
1752 -- end _init_proc;
1754 -- This function builds the call statement in this _init_proc.
1756 procedure Build_CPP_Init_Procedure;
1757 -- Build the tree corresponding to the procedure specification and body
1758 -- of the IC procedure that initializes the C++ part of the dispatch
1759 -- table of an Ada tagged type that is a derivation of a CPP type.
1760 -- Install it as the CPP_Init TSS.
1762 procedure Build_Init_Procedure;
1763 -- Build the tree corresponding to the procedure specification and body
1764 -- of the initialization procedure and install it as the _init TSS.
1766 procedure Build_Offset_To_Top_Functions;
1767 -- Ada 2005 (AI-251): Build the tree corresponding to the procedure spec
1768 -- and body of Offset_To_Top, a function used in conjuction with types
1769 -- having secondary dispatch tables.
1771 procedure Build_Record_Checks (S : Node_Id; Check_List : List_Id);
1772 -- Add range checks to components of discriminated records. S is a
1773 -- subtype indication of a record component. Check_List is a list
1774 -- to which the check actions are appended.
1776 function Component_Needs_Simple_Initialization
1777 (T : Entity_Id) return Boolean;
1778 -- Determine if a component needs simple initialization, given its type
1779 -- T. This routine is the same as Needs_Simple_Initialization except for
1780 -- components of type Tag and Interface_Tag. These two access types do
1781 -- not require initialization since they are explicitly initialized by
1782 -- other means.
1784 function Parent_Subtype_Renaming_Discrims return Boolean;
1785 -- Returns True for base types N that rename discriminants, else False
1787 function Requires_Init_Proc (Rec_Id : Entity_Id) return Boolean;
1788 -- Determine whether a record initialization procedure needs to be
1789 -- generated for the given record type.
1791 ----------------------
1792 -- Build_Assignment --
1793 ----------------------
1795 function Build_Assignment (Id : Entity_Id; N : Node_Id) return List_Id is
1796 N_Loc : constant Source_Ptr := Sloc (N);
1797 Typ : constant Entity_Id := Underlying_Type (Etype (Id));
1798 Exp : Node_Id := N;
1799 Kind : Node_Kind := Nkind (N);
1800 Lhs : Node_Id;
1801 Res : List_Id;
1803 begin
1804 Lhs :=
1805 Make_Selected_Component (N_Loc,
1806 Prefix => Make_Identifier (Loc, Name_uInit),
1807 Selector_Name => New_Occurrence_Of (Id, N_Loc));
1808 Set_Assignment_OK (Lhs);
1810 -- Case of an access attribute applied to the current instance.
1811 -- Replace the reference to the type by a reference to the actual
1812 -- object. (Note that this handles the case of the top level of
1813 -- the expression being given by such an attribute, but does not
1814 -- cover uses nested within an initial value expression. Nested
1815 -- uses are unlikely to occur in practice, but are theoretically
1816 -- possible.) It is not clear how to handle them without fully
1817 -- traversing the expression. ???
1819 if Kind = N_Attribute_Reference
1820 and then Nam_In (Attribute_Name (N), Name_Unchecked_Access,
1821 Name_Unrestricted_Access)
1822 and then Is_Entity_Name (Prefix (N))
1823 and then Is_Type (Entity (Prefix (N)))
1824 and then Entity (Prefix (N)) = Rec_Type
1825 then
1826 Exp :=
1827 Make_Attribute_Reference (N_Loc,
1828 Prefix =>
1829 Make_Identifier (N_Loc, Name_uInit),
1830 Attribute_Name => Name_Unrestricted_Access);
1831 end if;
1833 -- Take a copy of Exp to ensure that later copies of this component
1834 -- declaration in derived types see the original tree, not a node
1835 -- rewritten during expansion of the init_proc. If the copy contains
1836 -- itypes, the scope of the new itypes is the init_proc being built.
1838 Exp := New_Copy_Tree (Exp, New_Scope => Proc_Id);
1840 Res := New_List (
1841 Make_Assignment_Statement (Loc,
1842 Name => Lhs,
1843 Expression => Exp));
1845 Set_No_Ctrl_Actions (First (Res));
1847 -- Adjust the tag if tagged (because of possible view conversions).
1848 -- Suppress the tag adjustment when VM_Target because VM tags are
1849 -- represented implicitly in objects.
1851 if Is_Tagged_Type (Typ) and then Tagged_Type_Expansion then
1852 Append_To (Res,
1853 Make_Assignment_Statement (N_Loc,
1854 Name =>
1855 Make_Selected_Component (N_Loc,
1856 Prefix =>
1857 New_Copy_Tree (Lhs, New_Scope => Proc_Id),
1858 Selector_Name =>
1859 New_Occurrence_Of (First_Tag_Component (Typ), N_Loc)),
1861 Expression =>
1862 Unchecked_Convert_To (RTE (RE_Tag),
1863 New_Occurrence_Of
1864 (Node
1865 (First_Elmt
1866 (Access_Disp_Table (Underlying_Type (Typ)))),
1867 N_Loc))));
1868 end if;
1870 -- Adjust the component if controlled except if it is an aggregate
1871 -- that will be expanded inline.
1873 if Kind = N_Qualified_Expression then
1874 Kind := Nkind (Expression (N));
1875 end if;
1877 if Needs_Finalization (Typ)
1878 and then not (Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate))
1879 and then not Is_Limited_View (Typ)
1880 then
1881 Append_To (Res,
1882 Make_Adjust_Call
1883 (Obj_Ref => New_Copy_Tree (Lhs),
1884 Typ => Etype (Id)));
1885 end if;
1887 return Res;
1889 exception
1890 when RE_Not_Available =>
1891 return Empty_List;
1892 end Build_Assignment;
1894 ------------------------------------
1895 -- Build_Discriminant_Assignments --
1896 ------------------------------------
1898 procedure Build_Discriminant_Assignments (Statement_List : List_Id) is
1899 Is_Tagged : constant Boolean := Is_Tagged_Type (Rec_Type);
1900 D : Entity_Id;
1901 D_Loc : Source_Ptr;
1903 begin
1904 if Has_Discriminants (Rec_Type)
1905 and then not Is_Unchecked_Union (Rec_Type)
1906 then
1907 D := First_Discriminant (Rec_Type);
1908 while Present (D) loop
1910 -- Don't generate the assignment for discriminants in derived
1911 -- tagged types if the discriminant is a renaming of some
1912 -- ancestor discriminant. This initialization will be done
1913 -- when initializing the _parent field of the derived record.
1915 if Is_Tagged
1916 and then Present (Corresponding_Discriminant (D))
1917 then
1918 null;
1920 else
1921 D_Loc := Sloc (D);
1922 Append_List_To (Statement_List,
1923 Build_Assignment (D,
1924 New_Occurrence_Of (Discriminal (D), D_Loc)));
1925 end if;
1927 Next_Discriminant (D);
1928 end loop;
1929 end if;
1930 end Build_Discriminant_Assignments;
1932 --------------------------
1933 -- Build_Init_Call_Thru --
1934 --------------------------
1936 function Build_Init_Call_Thru (Parameters : List_Id) return List_Id is
1937 Parent_Proc : constant Entity_Id :=
1938 Base_Init_Proc (Etype (Rec_Type));
1940 Parent_Type : constant Entity_Id :=
1941 Etype (First_Formal (Parent_Proc));
1943 Uparent_Type : constant Entity_Id :=
1944 Underlying_Type (Parent_Type);
1946 First_Discr_Param : Node_Id;
1948 Arg : Node_Id;
1949 Args : List_Id;
1950 First_Arg : Node_Id;
1951 Parent_Discr : Entity_Id;
1952 Res : List_Id;
1954 begin
1955 -- First argument (_Init) is the object to be initialized.
1956 -- ??? not sure where to get a reasonable Loc for First_Arg
1958 First_Arg :=
1959 OK_Convert_To (Parent_Type,
1960 New_Occurrence_Of
1961 (Defining_Identifier (First (Parameters)), Loc));
1963 Set_Etype (First_Arg, Parent_Type);
1965 Args := New_List (Convert_Concurrent (First_Arg, Rec_Type));
1967 -- In the tasks case,
1968 -- add _Master as the value of the _Master parameter
1969 -- add _Chain as the value of the _Chain parameter.
1970 -- add _Task_Name as the value of the _Task_Name parameter.
1971 -- At the outer level, these will be variables holding the
1972 -- corresponding values obtained from GNARL or the expander.
1974 -- At inner levels, they will be the parameters passed down through
1975 -- the outer routines.
1977 First_Discr_Param := Next (First (Parameters));
1979 if Has_Task (Rec_Type) then
1980 if Restriction_Active (No_Task_Hierarchy) then
1981 Append_To (Args,
1982 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
1983 else
1984 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
1985 end if;
1987 -- Add _Chain (not done for sequential elaboration policy, see
1988 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
1990 if Partition_Elaboration_Policy /= 'S' then
1991 Append_To (Args, Make_Identifier (Loc, Name_uChain));
1992 end if;
1994 Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
1995 First_Discr_Param := Next (Next (Next (First_Discr_Param)));
1996 end if;
1998 -- Append discriminant values
2000 if Has_Discriminants (Uparent_Type) then
2001 pragma Assert (not Is_Tagged_Type (Uparent_Type));
2003 Parent_Discr := First_Discriminant (Uparent_Type);
2004 while Present (Parent_Discr) loop
2006 -- Get the initial value for this discriminant
2007 -- ??? needs to be cleaned up to use parent_Discr_Constr
2008 -- directly.
2010 declare
2011 Discr : Entity_Id :=
2012 First_Stored_Discriminant (Uparent_Type);
2014 Discr_Value : Elmt_Id :=
2015 First_Elmt (Stored_Constraint (Rec_Type));
2017 begin
2018 while Original_Record_Component (Parent_Discr) /= Discr loop
2019 Next_Stored_Discriminant (Discr);
2020 Next_Elmt (Discr_Value);
2021 end loop;
2023 Arg := Node (Discr_Value);
2024 end;
2026 -- Append it to the list
2028 if Nkind (Arg) = N_Identifier
2029 and then Ekind (Entity (Arg)) = E_Discriminant
2030 then
2031 Append_To (Args,
2032 New_Occurrence_Of (Discriminal (Entity (Arg)), Loc));
2034 -- Case of access discriminants. We replace the reference
2035 -- to the type by a reference to the actual object.
2037 -- Is above comment right??? Use of New_Copy below seems mighty
2038 -- suspicious ???
2040 else
2041 Append_To (Args, New_Copy (Arg));
2042 end if;
2044 Next_Discriminant (Parent_Discr);
2045 end loop;
2046 end if;
2048 Res :=
2049 New_List (
2050 Make_Procedure_Call_Statement (Loc,
2051 Name =>
2052 New_Occurrence_Of (Parent_Proc, Loc),
2053 Parameter_Associations => Args));
2055 return Res;
2056 end Build_Init_Call_Thru;
2058 -----------------------------------
2059 -- Build_Offset_To_Top_Functions --
2060 -----------------------------------
2062 procedure Build_Offset_To_Top_Functions is
2064 procedure Build_Offset_To_Top_Function (Iface_Comp : Entity_Id);
2065 -- Generate:
2066 -- function Fxx (O : Address) return Storage_Offset is
2067 -- type Acc is access all <Typ>;
2068 -- begin
2069 -- return Acc!(O).Iface_Comp'Position;
2070 -- end Fxx;
2072 ----------------------------------
2073 -- Build_Offset_To_Top_Function --
2074 ----------------------------------
2076 procedure Build_Offset_To_Top_Function (Iface_Comp : Entity_Id) is
2077 Body_Node : Node_Id;
2078 Func_Id : Entity_Id;
2079 Spec_Node : Node_Id;
2080 Acc_Type : Entity_Id;
2082 begin
2083 Func_Id := Make_Temporary (Loc, 'F');
2084 Set_DT_Offset_To_Top_Func (Iface_Comp, Func_Id);
2086 -- Generate
2087 -- function Fxx (O : in Rec_Typ) return Storage_Offset;
2089 Spec_Node := New_Node (N_Function_Specification, Loc);
2090 Set_Defining_Unit_Name (Spec_Node, Func_Id);
2091 Set_Parameter_Specifications (Spec_Node, New_List (
2092 Make_Parameter_Specification (Loc,
2093 Defining_Identifier =>
2094 Make_Defining_Identifier (Loc, Name_uO),
2095 In_Present => True,
2096 Parameter_Type =>
2097 New_Occurrence_Of (RTE (RE_Address), Loc))));
2098 Set_Result_Definition (Spec_Node,
2099 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
2101 -- Generate
2102 -- function Fxx (O : in Rec_Typ) return Storage_Offset is
2103 -- begin
2104 -- return O.Iface_Comp'Position;
2105 -- end Fxx;
2107 Body_Node := New_Node (N_Subprogram_Body, Loc);
2108 Set_Specification (Body_Node, Spec_Node);
2110 Acc_Type := Make_Temporary (Loc, 'T');
2111 Set_Declarations (Body_Node, New_List (
2112 Make_Full_Type_Declaration (Loc,
2113 Defining_Identifier => Acc_Type,
2114 Type_Definition =>
2115 Make_Access_To_Object_Definition (Loc,
2116 All_Present => True,
2117 Null_Exclusion_Present => False,
2118 Constant_Present => False,
2119 Subtype_Indication =>
2120 New_Occurrence_Of (Rec_Type, Loc)))));
2122 Set_Handled_Statement_Sequence (Body_Node,
2123 Make_Handled_Sequence_Of_Statements (Loc,
2124 Statements => New_List (
2125 Make_Simple_Return_Statement (Loc,
2126 Expression =>
2127 Make_Attribute_Reference (Loc,
2128 Prefix =>
2129 Make_Selected_Component (Loc,
2130 Prefix =>
2131 Unchecked_Convert_To (Acc_Type,
2132 Make_Identifier (Loc, Name_uO)),
2133 Selector_Name =>
2134 New_Occurrence_Of (Iface_Comp, Loc)),
2135 Attribute_Name => Name_Position)))));
2137 Set_Ekind (Func_Id, E_Function);
2138 Set_Mechanism (Func_Id, Default_Mechanism);
2139 Set_Is_Internal (Func_Id, True);
2141 if not Debug_Generated_Code then
2142 Set_Debug_Info_Off (Func_Id);
2143 end if;
2145 Analyze (Body_Node);
2147 Append_Freeze_Action (Rec_Type, Body_Node);
2148 end Build_Offset_To_Top_Function;
2150 -- Local variables
2152 Iface_Comp : Node_Id;
2153 Iface_Comp_Elmt : Elmt_Id;
2154 Ifaces_Comp_List : Elist_Id;
2156 -- Start of processing for Build_Offset_To_Top_Functions
2158 begin
2159 -- Offset_To_Top_Functions are built only for derivations of types
2160 -- with discriminants that cover interface types.
2161 -- Nothing is needed either in case of virtual machines, since
2162 -- interfaces are handled directly by the VM.
2164 if not Is_Tagged_Type (Rec_Type)
2165 or else Etype (Rec_Type) = Rec_Type
2166 or else not Has_Discriminants (Etype (Rec_Type))
2167 or else not Tagged_Type_Expansion
2168 then
2169 return;
2170 end if;
2172 Collect_Interface_Components (Rec_Type, Ifaces_Comp_List);
2174 -- For each interface type with secondary dispatch table we generate
2175 -- the Offset_To_Top_Functions (required to displace the pointer in
2176 -- interface conversions)
2178 Iface_Comp_Elmt := First_Elmt (Ifaces_Comp_List);
2179 while Present (Iface_Comp_Elmt) loop
2180 Iface_Comp := Node (Iface_Comp_Elmt);
2181 pragma Assert (Is_Interface (Related_Type (Iface_Comp)));
2183 -- If the interface is a parent of Rec_Type it shares the primary
2184 -- dispatch table and hence there is no need to build the function
2186 if not Is_Ancestor (Related_Type (Iface_Comp), Rec_Type,
2187 Use_Full_View => True)
2188 then
2189 Build_Offset_To_Top_Function (Iface_Comp);
2190 end if;
2192 Next_Elmt (Iface_Comp_Elmt);
2193 end loop;
2194 end Build_Offset_To_Top_Functions;
2196 ------------------------------
2197 -- Build_CPP_Init_Procedure --
2198 ------------------------------
2200 procedure Build_CPP_Init_Procedure is
2201 Body_Node : Node_Id;
2202 Body_Stmts : List_Id;
2203 Flag_Id : Entity_Id;
2204 Handled_Stmt_Node : Node_Id;
2205 Init_Tags_List : List_Id;
2206 Proc_Id : Entity_Id;
2207 Proc_Spec_Node : Node_Id;
2209 begin
2210 -- Check cases requiring no IC routine
2212 if not Is_CPP_Class (Root_Type (Rec_Type))
2213 or else Is_CPP_Class (Rec_Type)
2214 or else CPP_Num_Prims (Rec_Type) = 0
2215 or else not Tagged_Type_Expansion
2216 or else No_Run_Time_Mode
2217 then
2218 return;
2219 end if;
2221 -- Generate:
2223 -- Flag : Boolean := False;
2225 -- procedure Typ_IC is
2226 -- begin
2227 -- if not Flag then
2228 -- Copy C++ dispatch table slots from parent
2229 -- Update C++ slots of overridden primitives
2230 -- end if;
2231 -- end;
2233 Flag_Id := Make_Temporary (Loc, 'F');
2235 Append_Freeze_Action (Rec_Type,
2236 Make_Object_Declaration (Loc,
2237 Defining_Identifier => Flag_Id,
2238 Object_Definition =>
2239 New_Occurrence_Of (Standard_Boolean, Loc),
2240 Expression =>
2241 New_Occurrence_Of (Standard_True, Loc)));
2243 Body_Stmts := New_List;
2244 Body_Node := New_Node (N_Subprogram_Body, Loc);
2246 Proc_Spec_Node := New_Node (N_Procedure_Specification, Loc);
2248 Proc_Id :=
2249 Make_Defining_Identifier (Loc,
2250 Chars => Make_TSS_Name (Rec_Type, TSS_CPP_Init_Proc));
2252 Set_Ekind (Proc_Id, E_Procedure);
2253 Set_Is_Internal (Proc_Id);
2255 Set_Defining_Unit_Name (Proc_Spec_Node, Proc_Id);
2257 Set_Parameter_Specifications (Proc_Spec_Node, New_List);
2258 Set_Specification (Body_Node, Proc_Spec_Node);
2259 Set_Declarations (Body_Node, New_List);
2261 Init_Tags_List := Build_Inherit_CPP_Prims (Rec_Type);
2263 Append_To (Init_Tags_List,
2264 Make_Assignment_Statement (Loc,
2265 Name =>
2266 New_Occurrence_Of (Flag_Id, Loc),
2267 Expression =>
2268 New_Occurrence_Of (Standard_False, Loc)));
2270 Append_To (Body_Stmts,
2271 Make_If_Statement (Loc,
2272 Condition => New_Occurrence_Of (Flag_Id, Loc),
2273 Then_Statements => Init_Tags_List));
2275 Handled_Stmt_Node :=
2276 New_Node (N_Handled_Sequence_Of_Statements, Loc);
2277 Set_Statements (Handled_Stmt_Node, Body_Stmts);
2278 Set_Exception_Handlers (Handled_Stmt_Node, No_List);
2279 Set_Handled_Statement_Sequence (Body_Node, Handled_Stmt_Node);
2281 if not Debug_Generated_Code then
2282 Set_Debug_Info_Off (Proc_Id);
2283 end if;
2285 -- Associate CPP_Init_Proc with type
2287 Set_Init_Proc (Rec_Type, Proc_Id);
2288 end Build_CPP_Init_Procedure;
2290 --------------------------
2291 -- Build_Init_Procedure --
2292 --------------------------
2294 procedure Build_Init_Procedure is
2295 Body_Stmts : List_Id;
2296 Body_Node : Node_Id;
2297 Handled_Stmt_Node : Node_Id;
2298 Init_Tags_List : List_Id;
2299 Parameters : List_Id;
2300 Proc_Spec_Node : Node_Id;
2301 Record_Extension_Node : Node_Id;
2303 begin
2304 Body_Stmts := New_List;
2305 Body_Node := New_Node (N_Subprogram_Body, Loc);
2306 Set_Ekind (Proc_Id, E_Procedure);
2308 Proc_Spec_Node := New_Node (N_Procedure_Specification, Loc);
2309 Set_Defining_Unit_Name (Proc_Spec_Node, Proc_Id);
2311 Parameters := Init_Formals (Rec_Type);
2312 Append_List_To (Parameters,
2313 Build_Discriminant_Formals (Rec_Type, True));
2315 -- For tagged types, we add a flag to indicate whether the routine
2316 -- is called to initialize a parent component in the init_proc of
2317 -- a type extension. If the flag is false, we do not set the tag
2318 -- because it has been set already in the extension.
2320 if Is_Tagged_Type (Rec_Type) then
2321 Set_Tag := Make_Temporary (Loc, 'P');
2323 Append_To (Parameters,
2324 Make_Parameter_Specification (Loc,
2325 Defining_Identifier => Set_Tag,
2326 Parameter_Type =>
2327 New_Occurrence_Of (Standard_Boolean, Loc),
2328 Expression =>
2329 New_Occurrence_Of (Standard_True, Loc)));
2330 end if;
2332 Set_Parameter_Specifications (Proc_Spec_Node, Parameters);
2333 Set_Specification (Body_Node, Proc_Spec_Node);
2334 Set_Declarations (Body_Node, Decls);
2336 -- N is a Derived_Type_Definition that renames the parameters of the
2337 -- ancestor type. We initialize it by expanding our discriminants and
2338 -- call the ancestor _init_proc with a type-converted object.
2340 if Parent_Subtype_Renaming_Discrims then
2341 Append_List_To (Body_Stmts, Build_Init_Call_Thru (Parameters));
2343 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
2344 Build_Discriminant_Assignments (Body_Stmts);
2346 if not Null_Present (Type_Definition (N)) then
2347 Append_List_To (Body_Stmts,
2348 Build_Init_Statements (Component_List (Type_Definition (N))));
2349 end if;
2351 -- N is a Derived_Type_Definition with a possible non-empty
2352 -- extension. The initialization of a type extension consists in the
2353 -- initialization of the components in the extension.
2355 else
2356 Build_Discriminant_Assignments (Body_Stmts);
2358 Record_Extension_Node :=
2359 Record_Extension_Part (Type_Definition (N));
2361 if not Null_Present (Record_Extension_Node) then
2362 declare
2363 Stmts : constant List_Id :=
2364 Build_Init_Statements (
2365 Component_List (Record_Extension_Node));
2367 begin
2368 -- The parent field must be initialized first because the
2369 -- offset of the new discriminants may depend on it. This is
2370 -- not needed if the parent is an interface type because in
2371 -- such case the initialization of the _parent field was not
2372 -- generated.
2374 if not Is_Interface (Etype (Rec_Ent))
2375 and then Nkind (First (Stmts)) = N_Procedure_Call_Statement
2376 and then Is_Init_Proc (Name (First (Stmts)))
2377 then
2378 Prepend_To (Body_Stmts, Remove_Head (Stmts));
2379 end if;
2381 Append_List_To (Body_Stmts, Stmts);
2382 end;
2383 end if;
2384 end if;
2386 -- Add here the assignment to instantiate the Tag
2388 -- The assignment corresponds to the code:
2390 -- _Init._Tag := Typ'Tag;
2392 -- Suppress the tag assignment when VM_Target because VM tags are
2393 -- represented implicitly in objects. It is also suppressed in case
2394 -- of CPP_Class types because in this case the tag is initialized in
2395 -- the C++ side.
2397 if Is_Tagged_Type (Rec_Type)
2398 and then Tagged_Type_Expansion
2399 and then not No_Run_Time_Mode
2400 then
2401 -- Case 1: Ada tagged types with no CPP ancestor. Set the tags of
2402 -- the actual object and invoke the IP of the parent (in this
2403 -- order). The tag must be initialized before the call to the IP
2404 -- of the parent and the assignments to other components because
2405 -- the initial value of the components may depend on the tag (eg.
2406 -- through a dispatching operation on an access to the current
2407 -- type). The tag assignment is not done when initializing the
2408 -- parent component of a type extension, because in that case the
2409 -- tag is set in the extension.
2411 if not Is_CPP_Class (Root_Type (Rec_Type)) then
2413 -- Initialize the primary tag component
2415 Init_Tags_List := New_List (
2416 Make_Assignment_Statement (Loc,
2417 Name =>
2418 Make_Selected_Component (Loc,
2419 Prefix => Make_Identifier (Loc, Name_uInit),
2420 Selector_Name =>
2421 New_Occurrence_Of
2422 (First_Tag_Component (Rec_Type), Loc)),
2423 Expression =>
2424 New_Occurrence_Of
2425 (Node
2426 (First_Elmt (Access_Disp_Table (Rec_Type))), Loc)));
2428 -- Ada 2005 (AI-251): Initialize the secondary tags components
2429 -- located at fixed positions (tags whose position depends on
2430 -- variable size components are initialized later ---see below)
2432 if Ada_Version >= Ada_2005
2433 and then not Is_Interface (Rec_Type)
2434 and then Has_Interfaces (Rec_Type)
2435 then
2436 Init_Secondary_Tags
2437 (Typ => Rec_Type,
2438 Target => Make_Identifier (Loc, Name_uInit),
2439 Stmts_List => Init_Tags_List,
2440 Fixed_Comps => True,
2441 Variable_Comps => False);
2442 end if;
2444 Prepend_To (Body_Stmts,
2445 Make_If_Statement (Loc,
2446 Condition => New_Occurrence_Of (Set_Tag, Loc),
2447 Then_Statements => Init_Tags_List));
2449 -- Case 2: CPP type. The imported C++ constructor takes care of
2450 -- tags initialization. No action needed here because the IP
2451 -- is built by Set_CPP_Constructors; in this case the IP is a
2452 -- wrapper that invokes the C++ constructor and copies the C++
2453 -- tags locally. Done to inherit the C++ slots in Ada derivations
2454 -- (see case 3).
2456 elsif Is_CPP_Class (Rec_Type) then
2457 pragma Assert (False);
2458 null;
2460 -- Case 3: Combined hierarchy containing C++ types and Ada tagged
2461 -- type derivations. Derivations of imported C++ classes add a
2462 -- complication, because we cannot inhibit tag setting in the
2463 -- constructor for the parent. Hence we initialize the tag after
2464 -- the call to the parent IP (that is, in reverse order compared
2465 -- with pure Ada hierarchies ---see comment on case 1).
2467 else
2468 -- Initialize the primary tag
2470 Init_Tags_List := New_List (
2471 Make_Assignment_Statement (Loc,
2472 Name =>
2473 Make_Selected_Component (Loc,
2474 Prefix => Make_Identifier (Loc, Name_uInit),
2475 Selector_Name =>
2476 New_Occurrence_Of
2477 (First_Tag_Component (Rec_Type), Loc)),
2478 Expression =>
2479 New_Occurrence_Of
2480 (Node
2481 (First_Elmt (Access_Disp_Table (Rec_Type))), Loc)));
2483 -- Ada 2005 (AI-251): Initialize the secondary tags components
2484 -- located at fixed positions (tags whose position depends on
2485 -- variable size components are initialized later ---see below)
2487 if Ada_Version >= Ada_2005
2488 and then not Is_Interface (Rec_Type)
2489 and then Has_Interfaces (Rec_Type)
2490 then
2491 Init_Secondary_Tags
2492 (Typ => Rec_Type,
2493 Target => Make_Identifier (Loc, Name_uInit),
2494 Stmts_List => Init_Tags_List,
2495 Fixed_Comps => True,
2496 Variable_Comps => False);
2497 end if;
2499 -- Initialize the tag component after invocation of parent IP.
2501 -- Generate:
2502 -- parent_IP(_init.parent); // Invokes the C++ constructor
2503 -- [ typIC; ] // Inherit C++ slots from parent
2504 -- init_tags
2506 declare
2507 Ins_Nod : Node_Id;
2509 begin
2510 -- Search for the call to the IP of the parent. We assume
2511 -- that the first init_proc call is for the parent.
2513 Ins_Nod := First (Body_Stmts);
2514 while Present (Next (Ins_Nod))
2515 and then (Nkind (Ins_Nod) /= N_Procedure_Call_Statement
2516 or else not Is_Init_Proc (Name (Ins_Nod)))
2517 loop
2518 Next (Ins_Nod);
2519 end loop;
2521 -- The IC routine copies the inherited slots of the C+ part
2522 -- of the dispatch table from the parent and updates the
2523 -- overridden C++ slots.
2525 if CPP_Num_Prims (Rec_Type) > 0 then
2526 declare
2527 Init_DT : Entity_Id;
2528 New_Nod : Node_Id;
2530 begin
2531 Init_DT := CPP_Init_Proc (Rec_Type);
2532 pragma Assert (Present (Init_DT));
2534 New_Nod :=
2535 Make_Procedure_Call_Statement (Loc,
2536 New_Occurrence_Of (Init_DT, Loc));
2537 Insert_After (Ins_Nod, New_Nod);
2539 -- Update location of init tag statements
2541 Ins_Nod := New_Nod;
2542 end;
2543 end if;
2545 Insert_List_After (Ins_Nod, Init_Tags_List);
2546 end;
2547 end if;
2549 -- Ada 2005 (AI-251): Initialize the secondary tag components
2550 -- located at variable positions. We delay the generation of this
2551 -- code until here because the value of the attribute 'Position
2552 -- applied to variable size components of the parent type that
2553 -- depend on discriminants is only safely read at runtime after
2554 -- the parent components have been initialized.
2556 if Ada_Version >= Ada_2005
2557 and then not Is_Interface (Rec_Type)
2558 and then Has_Interfaces (Rec_Type)
2559 and then Has_Discriminants (Etype (Rec_Type))
2560 and then Is_Variable_Size_Record (Etype (Rec_Type))
2561 then
2562 Init_Tags_List := New_List;
2564 Init_Secondary_Tags
2565 (Typ => Rec_Type,
2566 Target => Make_Identifier (Loc, Name_uInit),
2567 Stmts_List => Init_Tags_List,
2568 Fixed_Comps => False,
2569 Variable_Comps => True);
2571 if Is_Non_Empty_List (Init_Tags_List) then
2572 Append_List_To (Body_Stmts, Init_Tags_List);
2573 end if;
2574 end if;
2575 end if;
2577 Handled_Stmt_Node := New_Node (N_Handled_Sequence_Of_Statements, Loc);
2578 Set_Statements (Handled_Stmt_Node, Body_Stmts);
2580 -- Generate:
2581 -- Deep_Finalize (_init, C1, ..., CN);
2582 -- raise;
2584 if Counter > 0
2585 and then Needs_Finalization (Rec_Type)
2586 and then not Is_Abstract_Type (Rec_Type)
2587 and then not Restriction_Active (No_Exception_Propagation)
2588 then
2589 declare
2590 DF_Call : Node_Id;
2591 DF_Id : Entity_Id;
2593 begin
2594 -- Create a local version of Deep_Finalize which has indication
2595 -- of partial initialization state.
2597 DF_Id := Make_Temporary (Loc, 'F');
2599 Append_To (Decls, Make_Local_Deep_Finalize (Rec_Type, DF_Id));
2601 DF_Call :=
2602 Make_Procedure_Call_Statement (Loc,
2603 Name => New_Occurrence_Of (DF_Id, Loc),
2604 Parameter_Associations => New_List (
2605 Make_Identifier (Loc, Name_uInit),
2606 New_Occurrence_Of (Standard_False, Loc)));
2608 -- Do not emit warnings related to the elaboration order when a
2609 -- controlled object is declared before the body of Finalize is
2610 -- seen.
2612 Set_No_Elaboration_Check (DF_Call);
2614 Set_Exception_Handlers (Handled_Stmt_Node, New_List (
2615 Make_Exception_Handler (Loc,
2616 Exception_Choices => New_List (
2617 Make_Others_Choice (Loc)),
2618 Statements => New_List (
2619 DF_Call,
2620 Make_Raise_Statement (Loc)))));
2621 end;
2622 else
2623 Set_Exception_Handlers (Handled_Stmt_Node, No_List);
2624 end if;
2626 Set_Handled_Statement_Sequence (Body_Node, Handled_Stmt_Node);
2628 if not Debug_Generated_Code then
2629 Set_Debug_Info_Off (Proc_Id);
2630 end if;
2632 -- Associate Init_Proc with type, and determine if the procedure
2633 -- is null (happens because of the Initialize_Scalars pragma case,
2634 -- where we have to generate a null procedure in case it is called
2635 -- by a client with Initialize_Scalars set). Such procedures have
2636 -- to be generated, but do not have to be called, so we mark them
2637 -- as null to suppress the call.
2639 Set_Init_Proc (Rec_Type, Proc_Id);
2641 if List_Length (Body_Stmts) = 1
2643 -- We must skip SCIL nodes because they may have been added to this
2644 -- list by Insert_Actions.
2646 and then Nkind (First_Non_SCIL_Node (Body_Stmts)) = N_Null_Statement
2647 and then VM_Target = No_VM
2648 then
2649 -- Even though the init proc may be null at this time it might get
2650 -- some stuff added to it later by the VM backend.
2652 Set_Is_Null_Init_Proc (Proc_Id);
2653 end if;
2654 end Build_Init_Procedure;
2656 ---------------------------
2657 -- Build_Init_Statements --
2658 ---------------------------
2660 function Build_Init_Statements (Comp_List : Node_Id) return List_Id is
2661 Checks : constant List_Id := New_List;
2662 Actions : List_Id := No_List;
2663 Counter_Id : Entity_Id := Empty;
2664 Comp_Loc : Source_Ptr;
2665 Decl : Node_Id;
2666 Has_POC : Boolean;
2667 Id : Entity_Id;
2668 Parent_Stmts : List_Id;
2669 Stmts : List_Id;
2670 Typ : Entity_Id;
2672 procedure Increment_Counter (Loc : Source_Ptr);
2673 -- Generate an "increment by one" statement for the current counter
2674 -- and append it to the list Stmts.
2676 procedure Make_Counter (Loc : Source_Ptr);
2677 -- Create a new counter for the current component list. The routine
2678 -- creates a new defining Id, adds an object declaration and sets
2679 -- the Id generator for the next variant.
2681 -----------------------
2682 -- Increment_Counter --
2683 -----------------------
2685 procedure Increment_Counter (Loc : Source_Ptr) is
2686 begin
2687 -- Generate:
2688 -- Counter := Counter + 1;
2690 Append_To (Stmts,
2691 Make_Assignment_Statement (Loc,
2692 Name => New_Occurrence_Of (Counter_Id, Loc),
2693 Expression =>
2694 Make_Op_Add (Loc,
2695 Left_Opnd => New_Occurrence_Of (Counter_Id, Loc),
2696 Right_Opnd => Make_Integer_Literal (Loc, 1))));
2697 end Increment_Counter;
2699 ------------------
2700 -- Make_Counter --
2701 ------------------
2703 procedure Make_Counter (Loc : Source_Ptr) is
2704 begin
2705 -- Increment the Id generator
2707 Counter := Counter + 1;
2709 -- Create the entity and declaration
2711 Counter_Id :=
2712 Make_Defining_Identifier (Loc,
2713 Chars => New_External_Name ('C', Counter));
2715 -- Generate:
2716 -- Cnn : Integer := 0;
2718 Append_To (Decls,
2719 Make_Object_Declaration (Loc,
2720 Defining_Identifier => Counter_Id,
2721 Object_Definition =>
2722 New_Occurrence_Of (Standard_Integer, Loc),
2723 Expression =>
2724 Make_Integer_Literal (Loc, 0)));
2725 end Make_Counter;
2727 -- Start of processing for Build_Init_Statements
2729 begin
2730 if Null_Present (Comp_List) then
2731 return New_List (Make_Null_Statement (Loc));
2732 end if;
2734 Parent_Stmts := New_List;
2735 Stmts := New_List;
2737 -- Loop through visible declarations of task types and protected
2738 -- types moving any expanded code from the spec to the body of the
2739 -- init procedure.
2741 if Is_Task_Record_Type (Rec_Type)
2742 or else Is_Protected_Record_Type (Rec_Type)
2743 then
2744 declare
2745 Decl : constant Node_Id :=
2746 Parent (Corresponding_Concurrent_Type (Rec_Type));
2747 Def : Node_Id;
2748 N1 : Node_Id;
2749 N2 : Node_Id;
2751 begin
2752 if Is_Task_Record_Type (Rec_Type) then
2753 Def := Task_Definition (Decl);
2754 else
2755 Def := Protected_Definition (Decl);
2756 end if;
2758 if Present (Def) then
2759 N1 := First (Visible_Declarations (Def));
2760 while Present (N1) loop
2761 N2 := N1;
2762 N1 := Next (N1);
2764 if Nkind (N2) in N_Statement_Other_Than_Procedure_Call
2765 or else Nkind (N2) in N_Raise_xxx_Error
2766 or else Nkind (N2) = N_Procedure_Call_Statement
2767 then
2768 Append_To (Stmts,
2769 New_Copy_Tree (N2, New_Scope => Proc_Id));
2770 Rewrite (N2, Make_Null_Statement (Sloc (N2)));
2771 Analyze (N2);
2772 end if;
2773 end loop;
2774 end if;
2775 end;
2776 end if;
2778 -- Loop through components, skipping pragmas, in 2 steps. The first
2779 -- step deals with regular components. The second step deals with
2780 -- components that have per object constraints and no explicit
2781 -- initialization.
2783 Has_POC := False;
2785 -- First pass : regular components
2787 Decl := First_Non_Pragma (Component_Items (Comp_List));
2788 while Present (Decl) loop
2789 Comp_Loc := Sloc (Decl);
2790 Build_Record_Checks
2791 (Subtype_Indication (Component_Definition (Decl)), Checks);
2793 Id := Defining_Identifier (Decl);
2794 Typ := Etype (Id);
2796 -- Leave any processing of per-object constrained component for
2797 -- the second pass.
2799 if Has_Access_Constraint (Id) and then No (Expression (Decl)) then
2800 Has_POC := True;
2802 -- Regular component cases
2804 else
2805 -- In the context of the init proc, references to discriminants
2806 -- resolve to denote the discriminals: this is where we can
2807 -- freeze discriminant dependent component subtypes.
2809 if not Is_Frozen (Typ) then
2810 Append_List_To (Stmts, Freeze_Entity (Typ, N));
2811 end if;
2813 -- Explicit initialization
2815 if Present (Expression (Decl)) then
2816 if Is_CPP_Constructor_Call (Expression (Decl)) then
2817 Actions :=
2818 Build_Initialization_Call
2819 (Comp_Loc,
2820 Id_Ref =>
2821 Make_Selected_Component (Comp_Loc,
2822 Prefix =>
2823 Make_Identifier (Comp_Loc, Name_uInit),
2824 Selector_Name =>
2825 New_Occurrence_Of (Id, Comp_Loc)),
2826 Typ => Typ,
2827 In_Init_Proc => True,
2828 Enclos_Type => Rec_Type,
2829 Discr_Map => Discr_Map,
2830 Constructor_Ref => Expression (Decl));
2831 else
2832 Actions := Build_Assignment (Id, Expression (Decl));
2833 end if;
2835 -- CPU, Dispatching_Domain, Priority and Size components are
2836 -- filled with the corresponding rep item expression of the
2837 -- concurrent type (if any).
2839 elsif Ekind (Scope (Id)) = E_Record_Type
2840 and then Present (Corresponding_Concurrent_Type (Scope (Id)))
2841 and then Nam_In (Chars (Id), Name_uCPU,
2842 Name_uDispatching_Domain,
2843 Name_uPriority)
2844 then
2845 declare
2846 Exp : Node_Id;
2847 Nam : Name_Id;
2848 Ritem : Node_Id;
2850 begin
2851 if Chars (Id) = Name_uCPU then
2852 Nam := Name_CPU;
2854 elsif Chars (Id) = Name_uDispatching_Domain then
2855 Nam := Name_Dispatching_Domain;
2857 elsif Chars (Id) = Name_uPriority then
2858 Nam := Name_Priority;
2859 end if;
2861 -- Get the Rep Item (aspect specification, attribute
2862 -- definition clause or pragma) of the corresponding
2863 -- concurrent type.
2865 Ritem :=
2866 Get_Rep_Item
2867 (Corresponding_Concurrent_Type (Scope (Id)),
2868 Nam,
2869 Check_Parents => False);
2871 if Present (Ritem) then
2873 -- Pragma case
2875 if Nkind (Ritem) = N_Pragma then
2876 Exp := First (Pragma_Argument_Associations (Ritem));
2878 if Nkind (Exp) = N_Pragma_Argument_Association then
2879 Exp := Expression (Exp);
2880 end if;
2882 -- Conversion for Priority expression
2884 if Nam = Name_Priority then
2885 if Pragma_Name (Ritem) = Name_Priority
2886 and then not GNAT_Mode
2887 then
2888 Exp := Convert_To (RTE (RE_Priority), Exp);
2889 else
2890 Exp :=
2891 Convert_To (RTE (RE_Any_Priority), Exp);
2892 end if;
2893 end if;
2895 -- Aspect/Attribute definition clause case
2897 else
2898 Exp := Expression (Ritem);
2900 -- Conversion for Priority expression
2902 if Nam = Name_Priority then
2903 if Chars (Ritem) = Name_Priority
2904 and then not GNAT_Mode
2905 then
2906 Exp := Convert_To (RTE (RE_Priority), Exp);
2907 else
2908 Exp :=
2909 Convert_To (RTE (RE_Any_Priority), Exp);
2910 end if;
2911 end if;
2912 end if;
2914 -- Conversion for Dispatching_Domain value
2916 if Nam = Name_Dispatching_Domain then
2917 Exp :=
2918 Unchecked_Convert_To
2919 (RTE (RE_Dispatching_Domain_Access), Exp);
2920 end if;
2922 Actions := Build_Assignment (Id, Exp);
2924 -- Nothing needed if no Rep Item
2926 else
2927 Actions := No_List;
2928 end if;
2929 end;
2931 -- Composite component with its own Init_Proc
2933 elsif not Is_Interface (Typ)
2934 and then Has_Non_Null_Base_Init_Proc (Typ)
2935 then
2936 Actions :=
2937 Build_Initialization_Call
2938 (Comp_Loc,
2939 Make_Selected_Component (Comp_Loc,
2940 Prefix =>
2941 Make_Identifier (Comp_Loc, Name_uInit),
2942 Selector_Name => New_Occurrence_Of (Id, Comp_Loc)),
2943 Typ,
2944 In_Init_Proc => True,
2945 Enclos_Type => Rec_Type,
2946 Discr_Map => Discr_Map);
2948 Clean_Task_Names (Typ, Proc_Id);
2950 -- Simple initialization
2952 elsif Component_Needs_Simple_Initialization (Typ) then
2953 Actions :=
2954 Build_Assignment
2955 (Id, Get_Simple_Init_Val (Typ, N, Esize (Id)));
2957 -- Nothing needed for this case
2959 else
2960 Actions := No_List;
2961 end if;
2963 if Present (Checks) then
2964 if Chars (Id) = Name_uParent then
2965 Append_List_To (Parent_Stmts, Checks);
2966 else
2967 Append_List_To (Stmts, Checks);
2968 end if;
2969 end if;
2971 if Present (Actions) then
2972 if Chars (Id) = Name_uParent then
2973 Append_List_To (Parent_Stmts, Actions);
2975 else
2976 Append_List_To (Stmts, Actions);
2978 -- Preserve initialization state in the current counter
2980 if Needs_Finalization (Typ) then
2981 if No (Counter_Id) then
2982 Make_Counter (Comp_Loc);
2983 end if;
2985 Increment_Counter (Comp_Loc);
2986 end if;
2987 end if;
2988 end if;
2989 end if;
2991 Next_Non_Pragma (Decl);
2992 end loop;
2994 -- The parent field must be initialized first because variable
2995 -- size components of the parent affect the location of all the
2996 -- new components.
2998 Prepend_List_To (Stmts, Parent_Stmts);
3000 -- Set up tasks and protected object support. This needs to be done
3001 -- before any component with a per-object access discriminant
3002 -- constraint, or any variant part (which may contain such
3003 -- components) is initialized, because the initialization of these
3004 -- components may reference the enclosing concurrent object.
3006 -- For a task record type, add the task create call and calls to bind
3007 -- any interrupt (signal) entries.
3009 if Is_Task_Record_Type (Rec_Type) then
3011 -- In the case of the restricted run time the ATCB has already
3012 -- been preallocated.
3014 if Restricted_Profile then
3015 Append_To (Stmts,
3016 Make_Assignment_Statement (Loc,
3017 Name =>
3018 Make_Selected_Component (Loc,
3019 Prefix => Make_Identifier (Loc, Name_uInit),
3020 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)),
3021 Expression =>
3022 Make_Attribute_Reference (Loc,
3023 Prefix =>
3024 Make_Selected_Component (Loc,
3025 Prefix => Make_Identifier (Loc, Name_uInit),
3026 Selector_Name => Make_Identifier (Loc, Name_uATCB)),
3027 Attribute_Name => Name_Unchecked_Access)));
3028 end if;
3030 Append_To (Stmts, Make_Task_Create_Call (Rec_Type));
3032 declare
3033 Task_Type : constant Entity_Id :=
3034 Corresponding_Concurrent_Type (Rec_Type);
3035 Task_Decl : constant Node_Id := Parent (Task_Type);
3036 Task_Def : constant Node_Id := Task_Definition (Task_Decl);
3037 Decl_Loc : Source_Ptr;
3038 Ent : Entity_Id;
3039 Vis_Decl : Node_Id;
3041 begin
3042 if Present (Task_Def) then
3043 Vis_Decl := First (Visible_Declarations (Task_Def));
3044 while Present (Vis_Decl) loop
3045 Decl_Loc := Sloc (Vis_Decl);
3047 if Nkind (Vis_Decl) = N_Attribute_Definition_Clause then
3048 if Get_Attribute_Id (Chars (Vis_Decl)) =
3049 Attribute_Address
3050 then
3051 Ent := Entity (Name (Vis_Decl));
3053 if Ekind (Ent) = E_Entry then
3054 Append_To (Stmts,
3055 Make_Procedure_Call_Statement (Decl_Loc,
3056 Name =>
3057 New_Occurrence_Of (RTE (
3058 RE_Bind_Interrupt_To_Entry), Decl_Loc),
3059 Parameter_Associations => New_List (
3060 Make_Selected_Component (Decl_Loc,
3061 Prefix =>
3062 Make_Identifier (Decl_Loc, Name_uInit),
3063 Selector_Name =>
3064 Make_Identifier
3065 (Decl_Loc, Name_uTask_Id)),
3066 Entry_Index_Expression
3067 (Decl_Loc, Ent, Empty, Task_Type),
3068 Expression (Vis_Decl))));
3069 end if;
3070 end if;
3071 end if;
3073 Next (Vis_Decl);
3074 end loop;
3075 end if;
3076 end;
3077 end if;
3079 -- For a protected type, add statements generated by
3080 -- Make_Initialize_Protection.
3082 if Is_Protected_Record_Type (Rec_Type) then
3083 Append_List_To (Stmts,
3084 Make_Initialize_Protection (Rec_Type));
3085 end if;
3087 -- Second pass: components with per-object constraints
3089 if Has_POC then
3090 Decl := First_Non_Pragma (Component_Items (Comp_List));
3091 while Present (Decl) loop
3092 Comp_Loc := Sloc (Decl);
3093 Id := Defining_Identifier (Decl);
3094 Typ := Etype (Id);
3096 if Has_Access_Constraint (Id)
3097 and then No (Expression (Decl))
3098 then
3099 if Has_Non_Null_Base_Init_Proc (Typ) then
3100 Append_List_To (Stmts,
3101 Build_Initialization_Call (Comp_Loc,
3102 Make_Selected_Component (Comp_Loc,
3103 Prefix =>
3104 Make_Identifier (Comp_Loc, Name_uInit),
3105 Selector_Name => New_Occurrence_Of (Id, Comp_Loc)),
3106 Typ,
3107 In_Init_Proc => True,
3108 Enclos_Type => Rec_Type,
3109 Discr_Map => Discr_Map));
3111 Clean_Task_Names (Typ, Proc_Id);
3113 -- Preserve initialization state in the current counter
3115 if Needs_Finalization (Typ) then
3116 if No (Counter_Id) then
3117 Make_Counter (Comp_Loc);
3118 end if;
3120 Increment_Counter (Comp_Loc);
3121 end if;
3123 elsif Component_Needs_Simple_Initialization (Typ) then
3124 Append_List_To (Stmts,
3125 Build_Assignment
3126 (Id, Get_Simple_Init_Val (Typ, N, Esize (Id))));
3127 end if;
3128 end if;
3130 Next_Non_Pragma (Decl);
3131 end loop;
3132 end if;
3134 -- Process the variant part
3136 if Present (Variant_Part (Comp_List)) then
3137 declare
3138 Variant_Alts : constant List_Id := New_List;
3139 Var_Loc : Source_Ptr;
3140 Variant : Node_Id;
3142 begin
3143 Variant :=
3144 First_Non_Pragma (Variants (Variant_Part (Comp_List)));
3145 while Present (Variant) loop
3146 Var_Loc := Sloc (Variant);
3147 Append_To (Variant_Alts,
3148 Make_Case_Statement_Alternative (Var_Loc,
3149 Discrete_Choices =>
3150 New_Copy_List (Discrete_Choices (Variant)),
3151 Statements =>
3152 Build_Init_Statements (Component_List (Variant))));
3153 Next_Non_Pragma (Variant);
3154 end loop;
3156 -- The expression of the case statement which is a reference
3157 -- to one of the discriminants is replaced by the appropriate
3158 -- formal parameter of the initialization procedure.
3160 Append_To (Stmts,
3161 Make_Case_Statement (Var_Loc,
3162 Expression =>
3163 New_Occurrence_Of (Discriminal (
3164 Entity (Name (Variant_Part (Comp_List)))), Var_Loc),
3165 Alternatives => Variant_Alts));
3166 end;
3167 end if;
3169 -- If no initializations when generated for component declarations
3170 -- corresponding to this Stmts, append a null statement to Stmts to
3171 -- to make it a valid Ada tree.
3173 if Is_Empty_List (Stmts) then
3174 Append (Make_Null_Statement (Loc), Stmts);
3175 end if;
3177 return Stmts;
3179 exception
3180 when RE_Not_Available =>
3181 return Empty_List;
3182 end Build_Init_Statements;
3184 -------------------------
3185 -- Build_Record_Checks --
3186 -------------------------
3188 procedure Build_Record_Checks (S : Node_Id; Check_List : List_Id) is
3189 Subtype_Mark_Id : Entity_Id;
3191 procedure Constrain_Array
3192 (SI : Node_Id;
3193 Check_List : List_Id);
3194 -- Apply a list of index constraints to an unconstrained array type.
3195 -- The first parameter is the entity for the resulting subtype.
3196 -- Check_List is a list to which the check actions are appended.
3198 ---------------------
3199 -- Constrain_Array --
3200 ---------------------
3202 procedure Constrain_Array
3203 (SI : Node_Id;
3204 Check_List : List_Id)
3206 C : constant Node_Id := Constraint (SI);
3207 Number_Of_Constraints : Nat := 0;
3208 Index : Node_Id;
3209 S, T : Entity_Id;
3211 procedure Constrain_Index
3212 (Index : Node_Id;
3213 S : Node_Id;
3214 Check_List : List_Id);
3215 -- Process an index constraint in a constrained array declaration.
3216 -- The constraint can be either a subtype name or a range with or
3217 -- without an explicit subtype mark. Index is the corresponding
3218 -- index of the unconstrained array. S is the range expression.
3219 -- Check_List is a list to which the check actions are appended.
3221 ---------------------
3222 -- Constrain_Index --
3223 ---------------------
3225 procedure Constrain_Index
3226 (Index : Node_Id;
3227 S : Node_Id;
3228 Check_List : List_Id)
3230 T : constant Entity_Id := Etype (Index);
3232 begin
3233 if Nkind (S) = N_Range then
3234 Process_Range_Expr_In_Decl (S, T, Check_List => Check_List);
3235 end if;
3236 end Constrain_Index;
3238 -- Start of processing for Constrain_Array
3240 begin
3241 T := Entity (Subtype_Mark (SI));
3243 if Is_Access_Type (T) then
3244 T := Designated_Type (T);
3245 end if;
3247 S := First (Constraints (C));
3249 while Present (S) loop
3250 Number_Of_Constraints := Number_Of_Constraints + 1;
3251 Next (S);
3252 end loop;
3254 -- In either case, the index constraint must provide a discrete
3255 -- range for each index of the array type and the type of each
3256 -- discrete range must be the same as that of the corresponding
3257 -- index. (RM 3.6.1)
3259 S := First (Constraints (C));
3260 Index := First_Index (T);
3261 Analyze (Index);
3263 -- Apply constraints to each index type
3265 for J in 1 .. Number_Of_Constraints loop
3266 Constrain_Index (Index, S, Check_List);
3267 Next (Index);
3268 Next (S);
3269 end loop;
3270 end Constrain_Array;
3272 -- Start of processing for Build_Record_Checks
3274 begin
3275 if Nkind (S) = N_Subtype_Indication then
3276 Find_Type (Subtype_Mark (S));
3277 Subtype_Mark_Id := Entity (Subtype_Mark (S));
3279 -- Remaining processing depends on type
3281 case Ekind (Subtype_Mark_Id) is
3283 when Array_Kind =>
3284 Constrain_Array (S, Check_List);
3286 when others =>
3287 null;
3288 end case;
3289 end if;
3290 end Build_Record_Checks;
3292 -------------------------------------------
3293 -- Component_Needs_Simple_Initialization --
3294 -------------------------------------------
3296 function Component_Needs_Simple_Initialization
3297 (T : Entity_Id) return Boolean
3299 begin
3300 return
3301 Needs_Simple_Initialization (T)
3302 and then not Is_RTE (T, RE_Tag)
3304 -- Ada 2005 (AI-251): Check also the tag of abstract interfaces
3306 and then not Is_RTE (T, RE_Interface_Tag);
3307 end Component_Needs_Simple_Initialization;
3309 --------------------------------------
3310 -- Parent_Subtype_Renaming_Discrims --
3311 --------------------------------------
3313 function Parent_Subtype_Renaming_Discrims return Boolean is
3314 De : Entity_Id;
3315 Dp : Entity_Id;
3317 begin
3318 if Base_Type (Rec_Ent) /= Rec_Ent then
3319 return False;
3320 end if;
3322 if Etype (Rec_Ent) = Rec_Ent
3323 or else not Has_Discriminants (Rec_Ent)
3324 or else Is_Constrained (Rec_Ent)
3325 or else Is_Tagged_Type (Rec_Ent)
3326 then
3327 return False;
3328 end if;
3330 -- If there are no explicit stored discriminants we have inherited
3331 -- the root type discriminants so far, so no renamings occurred.
3333 if First_Discriminant (Rec_Ent) =
3334 First_Stored_Discriminant (Rec_Ent)
3335 then
3336 return False;
3337 end if;
3339 -- Check if we have done some trivial renaming of the parent
3340 -- discriminants, i.e. something like
3342 -- type DT (X1, X2: int) is new PT (X1, X2);
3344 De := First_Discriminant (Rec_Ent);
3345 Dp := First_Discriminant (Etype (Rec_Ent));
3346 while Present (De) loop
3347 pragma Assert (Present (Dp));
3349 if Corresponding_Discriminant (De) /= Dp then
3350 return True;
3351 end if;
3353 Next_Discriminant (De);
3354 Next_Discriminant (Dp);
3355 end loop;
3357 return Present (Dp);
3358 end Parent_Subtype_Renaming_Discrims;
3360 ------------------------
3361 -- Requires_Init_Proc --
3362 ------------------------
3364 function Requires_Init_Proc (Rec_Id : Entity_Id) return Boolean is
3365 Comp_Decl : Node_Id;
3366 Id : Entity_Id;
3367 Typ : Entity_Id;
3369 begin
3370 -- Definitely do not need one if specifically suppressed
3372 if Initialization_Suppressed (Rec_Id) then
3373 return False;
3374 end if;
3376 -- If it is a type derived from a type with unknown discriminants,
3377 -- we cannot build an initialization procedure for it.
3379 if Has_Unknown_Discriminants (Rec_Id)
3380 or else Has_Unknown_Discriminants (Etype (Rec_Id))
3381 then
3382 return False;
3383 end if;
3385 -- Otherwise we need to generate an initialization procedure if
3386 -- Is_CPP_Class is False and at least one of the following applies:
3388 -- 1. Discriminants are present, since they need to be initialized
3389 -- with the appropriate discriminant constraint expressions.
3390 -- However, the discriminant of an unchecked union does not
3391 -- count, since the discriminant is not present.
3393 -- 2. The type is a tagged type, since the implicit Tag component
3394 -- needs to be initialized with a pointer to the dispatch table.
3396 -- 3. The type contains tasks
3398 -- 4. One or more components has an initial value
3400 -- 5. One or more components is for a type which itself requires
3401 -- an initialization procedure.
3403 -- 6. One or more components is a type that requires simple
3404 -- initialization (see Needs_Simple_Initialization), except
3405 -- that types Tag and Interface_Tag are excluded, since fields
3406 -- of these types are initialized by other means.
3408 -- 7. The type is the record type built for a task type (since at
3409 -- the very least, Create_Task must be called)
3411 -- 8. The type is the record type built for a protected type (since
3412 -- at least Initialize_Protection must be called)
3414 -- 9. The type is marked as a public entity. The reason we add this
3415 -- case (even if none of the above apply) is to properly handle
3416 -- Initialize_Scalars. If a package is compiled without an IS
3417 -- pragma, and the client is compiled with an IS pragma, then
3418 -- the client will think an initialization procedure is present
3419 -- and call it, when in fact no such procedure is required, but
3420 -- since the call is generated, there had better be a routine
3421 -- at the other end of the call, even if it does nothing).
3423 -- Note: the reason we exclude the CPP_Class case is because in this
3424 -- case the initialization is performed by the C++ constructors, and
3425 -- the IP is built by Set_CPP_Constructors.
3427 if Is_CPP_Class (Rec_Id) then
3428 return False;
3430 elsif Is_Interface (Rec_Id) then
3431 return False;
3433 elsif (Has_Discriminants (Rec_Id)
3434 and then not Is_Unchecked_Union (Rec_Id))
3435 or else Is_Tagged_Type (Rec_Id)
3436 or else Is_Concurrent_Record_Type (Rec_Id)
3437 or else Has_Task (Rec_Id)
3438 then
3439 return True;
3440 end if;
3442 Id := First_Component (Rec_Id);
3443 while Present (Id) loop
3444 Comp_Decl := Parent (Id);
3445 Typ := Etype (Id);
3447 if Present (Expression (Comp_Decl))
3448 or else Has_Non_Null_Base_Init_Proc (Typ)
3449 or else Component_Needs_Simple_Initialization (Typ)
3450 then
3451 return True;
3452 end if;
3454 Next_Component (Id);
3455 end loop;
3457 -- As explained above, a record initialization procedure is needed
3458 -- for public types in case Initialize_Scalars applies to a client.
3459 -- However, such a procedure is not needed in the case where either
3460 -- of restrictions No_Initialize_Scalars or No_Default_Initialization
3461 -- applies. No_Initialize_Scalars excludes the possibility of using
3462 -- Initialize_Scalars in any partition, and No_Default_Initialization
3463 -- implies that no initialization should ever be done for objects of
3464 -- the type, so is incompatible with Initialize_Scalars.
3466 if not Restriction_Active (No_Initialize_Scalars)
3467 and then not Restriction_Active (No_Default_Initialization)
3468 and then Is_Public (Rec_Id)
3469 then
3470 return True;
3471 end if;
3473 return False;
3474 end Requires_Init_Proc;
3476 -- Start of processing for Build_Record_Init_Proc
3478 begin
3479 -- Check for value type, which means no initialization required
3481 Rec_Type := Defining_Identifier (N);
3483 if Is_Value_Type (Rec_Type) then
3484 return;
3485 end if;
3487 -- This may be full declaration of a private type, in which case
3488 -- the visible entity is a record, and the private entity has been
3489 -- exchanged with it in the private part of the current package.
3490 -- The initialization procedure is built for the record type, which
3491 -- is retrievable from the private entity.
3493 if Is_Incomplete_Or_Private_Type (Rec_Type) then
3494 Rec_Type := Underlying_Type (Rec_Type);
3495 end if;
3497 -- If we have a variant record with restriction No_Implicit_Conditionals
3498 -- in effect, then we skip building the procedure. This is safe because
3499 -- if we can see the restriction, so can any caller, calls to initialize
3500 -- such records are not allowed for variant records if this restriction
3501 -- is active.
3503 if Has_Variant_Part (Rec_Type)
3504 and then Restriction_Active (No_Implicit_Conditionals)
3505 then
3506 return;
3507 end if;
3509 -- If there are discriminants, build the discriminant map to replace
3510 -- discriminants by their discriminals in complex bound expressions.
3511 -- These only arise for the corresponding records of synchronized types.
3513 if Is_Concurrent_Record_Type (Rec_Type)
3514 and then Has_Discriminants (Rec_Type)
3515 then
3516 declare
3517 Disc : Entity_Id;
3518 begin
3519 Disc := First_Discriminant (Rec_Type);
3520 while Present (Disc) loop
3521 Append_Elmt (Disc, Discr_Map);
3522 Append_Elmt (Discriminal (Disc), Discr_Map);
3523 Next_Discriminant (Disc);
3524 end loop;
3525 end;
3526 end if;
3528 -- Derived types that have no type extension can use the initialization
3529 -- procedure of their parent and do not need a procedure of their own.
3530 -- This is only correct if there are no representation clauses for the
3531 -- type or its parent, and if the parent has in fact been frozen so
3532 -- that its initialization procedure exists.
3534 if Is_Derived_Type (Rec_Type)
3535 and then not Is_Tagged_Type (Rec_Type)
3536 and then not Is_Unchecked_Union (Rec_Type)
3537 and then not Has_New_Non_Standard_Rep (Rec_Type)
3538 and then not Parent_Subtype_Renaming_Discrims
3539 and then Has_Non_Null_Base_Init_Proc (Etype (Rec_Type))
3540 then
3541 Copy_TSS (Base_Init_Proc (Etype (Rec_Type)), Rec_Type);
3543 -- Otherwise if we need an initialization procedure, then build one,
3544 -- mark it as public and inlinable and as having a completion.
3546 elsif Requires_Init_Proc (Rec_Type)
3547 or else Is_Unchecked_Union (Rec_Type)
3548 then
3549 Proc_Id :=
3550 Make_Defining_Identifier (Loc,
3551 Chars => Make_Init_Proc_Name (Rec_Type));
3553 -- If No_Default_Initialization restriction is active, then we don't
3554 -- want to build an init_proc, but we need to mark that an init_proc
3555 -- would be needed if this restriction was not active (so that we can
3556 -- detect attempts to call it), so set a dummy init_proc in place.
3558 if Restriction_Active (No_Default_Initialization) then
3559 Set_Init_Proc (Rec_Type, Proc_Id);
3560 return;
3561 end if;
3563 Build_Offset_To_Top_Functions;
3564 Build_CPP_Init_Procedure;
3565 Build_Init_Procedure;
3566 Set_Is_Public (Proc_Id, Is_Public (Rec_Ent));
3568 -- The initialization of protected records is not worth inlining.
3569 -- In addition, when compiled for another unit for inlining purposes,
3570 -- it may make reference to entities that have not been elaborated
3571 -- yet. The initialization of controlled records contains a nested
3572 -- clean-up procedure that makes it impractical to inline as well,
3573 -- and leads to undefined symbols if inlined in a different unit.
3574 -- Similar considerations apply to task types.
3576 if not Is_Concurrent_Type (Rec_Type)
3577 and then not Has_Task (Rec_Type)
3578 and then not Needs_Finalization (Rec_Type)
3579 then
3580 Set_Is_Inlined (Proc_Id);
3581 end if;
3583 Set_Is_Internal (Proc_Id);
3584 Set_Has_Completion (Proc_Id);
3586 if not Debug_Generated_Code then
3587 Set_Debug_Info_Off (Proc_Id);
3588 end if;
3590 declare
3591 Agg : constant Node_Id :=
3592 Build_Equivalent_Record_Aggregate (Rec_Type);
3594 procedure Collect_Itypes (Comp : Node_Id);
3595 -- Generate references to itypes in the aggregate, because
3596 -- the first use of the aggregate may be in a nested scope.
3598 --------------------
3599 -- Collect_Itypes --
3600 --------------------
3602 procedure Collect_Itypes (Comp : Node_Id) is
3603 Ref : Node_Id;
3604 Sub_Aggr : Node_Id;
3605 Typ : constant Entity_Id := Etype (Comp);
3607 begin
3608 if Is_Array_Type (Typ) and then Is_Itype (Typ) then
3609 Ref := Make_Itype_Reference (Loc);
3610 Set_Itype (Ref, Typ);
3611 Append_Freeze_Action (Rec_Type, Ref);
3613 Ref := Make_Itype_Reference (Loc);
3614 Set_Itype (Ref, Etype (First_Index (Typ)));
3615 Append_Freeze_Action (Rec_Type, Ref);
3617 Sub_Aggr := First (Expressions (Comp));
3619 -- Recurse on nested arrays
3621 while Present (Sub_Aggr) loop
3622 Collect_Itypes (Sub_Aggr);
3623 Next (Sub_Aggr);
3624 end loop;
3625 end if;
3626 end Collect_Itypes;
3628 begin
3629 -- If there is a static initialization aggregate for the type,
3630 -- generate itype references for the types of its (sub)components,
3631 -- to prevent out-of-scope errors in the resulting tree.
3632 -- The aggregate may have been rewritten as a Raise node, in which
3633 -- case there are no relevant itypes.
3635 if Present (Agg) and then Nkind (Agg) = N_Aggregate then
3636 Set_Static_Initialization (Proc_Id, Agg);
3638 declare
3639 Comp : Node_Id;
3640 begin
3641 Comp := First (Component_Associations (Agg));
3642 while Present (Comp) loop
3643 Collect_Itypes (Expression (Comp));
3644 Next (Comp);
3645 end loop;
3646 end;
3647 end if;
3648 end;
3649 end if;
3650 end Build_Record_Init_Proc;
3652 --------------------------------
3653 -- Build_Record_Invariant_Proc --
3654 --------------------------------
3656 function Build_Record_Invariant_Proc
3657 (R_Type : Entity_Id;
3658 Nod : Node_Id) return Node_Id
3660 Loc : constant Source_Ptr := Sloc (Nod);
3662 Object_Name : constant Name_Id := New_Internal_Name ('I');
3663 -- Name for argument of invariant procedure
3665 Object_Entity : constant Node_Id :=
3666 Make_Defining_Identifier (Loc, Object_Name);
3667 -- The procedure declaration entity for the argument
3669 Invariant_Found : Boolean;
3670 -- Set if any component needs an invariant check.
3672 Proc_Id : Entity_Id;
3673 Proc_Body : Node_Id;
3674 Stmts : List_Id;
3675 Type_Def : Node_Id;
3677 function Build_Invariant_Checks (Comp_List : Node_Id) return List_Id;
3678 -- Recursive procedure that generates a list of checks for components
3679 -- that need it, and recurses through variant parts when present.
3681 function Build_Component_Invariant_Call (Comp : Entity_Id)
3682 return Node_Id;
3683 -- Build call to invariant procedure for a record component.
3685 ------------------------------------
3686 -- Build_Component_Invariant_Call --
3687 ------------------------------------
3689 function Build_Component_Invariant_Call (Comp : Entity_Id)
3690 return Node_Id
3692 Sel_Comp : Node_Id;
3693 Typ : Entity_Id;
3694 Call : Node_Id;
3696 begin
3697 Invariant_Found := True;
3698 Typ := Etype (Comp);
3700 Sel_Comp :=
3701 Make_Selected_Component (Loc,
3702 Prefix => New_Occurrence_Of (Object_Entity, Loc),
3703 Selector_Name => New_Occurrence_Of (Comp, Loc));
3705 if Is_Access_Type (Typ) then
3707 -- If the access component designates a type with an invariant,
3708 -- the check applies to the designated object. The access type
3709 -- itself may have an invariant, in which case it applies to the
3710 -- access value directly.
3712 -- Note: we are assuming that invariants will not occur on both
3713 -- the access type and the type that it designates. This is not
3714 -- really justified but it is hard to imagine that this case will
3715 -- ever cause trouble ???
3717 if not (Has_Invariants (Typ)) then
3718 Sel_Comp := Make_Explicit_Dereference (Loc, Sel_Comp);
3719 Typ := Designated_Type (Typ);
3720 end if;
3721 end if;
3723 Call :=
3724 Make_Procedure_Call_Statement (Loc,
3725 Name =>
3726 New_Occurrence_Of (Invariant_Procedure (Typ), Loc),
3727 Parameter_Associations => New_List (Sel_Comp));
3729 if Is_Access_Type (Etype (Comp)) then
3730 Call :=
3731 Make_If_Statement (Loc,
3732 Condition =>
3733 Make_Op_Ne (Loc,
3734 Left_Opnd => Make_Null (Loc),
3735 Right_Opnd =>
3736 Make_Selected_Component (Loc,
3737 Prefix => New_Occurrence_Of (Object_Entity, Loc),
3738 Selector_Name => New_Occurrence_Of (Comp, Loc))),
3739 Then_Statements => New_List (Call));
3740 end if;
3742 return Call;
3743 end Build_Component_Invariant_Call;
3745 ----------------------------
3746 -- Build_Invariant_Checks --
3747 ----------------------------
3749 function Build_Invariant_Checks (Comp_List : Node_Id) return List_Id is
3750 Decl : Node_Id;
3751 Id : Entity_Id;
3752 Stmts : List_Id;
3754 begin
3755 Stmts := New_List;
3756 Decl := First_Non_Pragma (Component_Items (Comp_List));
3757 while Present (Decl) loop
3758 if Nkind (Decl) = N_Component_Declaration then
3759 Id := Defining_Identifier (Decl);
3761 if Has_Invariants (Etype (Id))
3762 and then In_Open_Scopes (Scope (R_Type))
3763 then
3764 if Has_Unchecked_Union (R_Type) then
3765 Error_Msg_NE
3766 ("invariants cannot be checked on components of "
3767 & "unchecked_union type&?", Decl, R_Type);
3768 return Empty_List;
3770 else
3771 Append_To (Stmts, Build_Component_Invariant_Call (Id));
3772 end if;
3774 elsif Is_Access_Type (Etype (Id))
3775 and then not Is_Access_Constant (Etype (Id))
3776 and then Has_Invariants (Designated_Type (Etype (Id)))
3777 and then In_Open_Scopes (Scope (Designated_Type (Etype (Id))))
3778 then
3779 Append_To (Stmts, Build_Component_Invariant_Call (Id));
3780 end if;
3781 end if;
3783 Next (Decl);
3784 end loop;
3786 if Present (Variant_Part (Comp_List)) then
3787 declare
3788 Variant_Alts : constant List_Id := New_List;
3789 Var_Loc : Source_Ptr;
3790 Variant : Node_Id;
3791 Variant_Stmts : List_Id;
3793 begin
3794 Variant :=
3795 First_Non_Pragma (Variants (Variant_Part (Comp_List)));
3796 while Present (Variant) loop
3797 Variant_Stmts :=
3798 Build_Invariant_Checks (Component_List (Variant));
3799 Var_Loc := Sloc (Variant);
3800 Append_To (Variant_Alts,
3801 Make_Case_Statement_Alternative (Var_Loc,
3802 Discrete_Choices =>
3803 New_Copy_List (Discrete_Choices (Variant)),
3804 Statements => Variant_Stmts));
3806 Next_Non_Pragma (Variant);
3807 end loop;
3809 -- The expression in the case statement is the reference to
3810 -- the discriminant of the target object.
3812 Append_To (Stmts,
3813 Make_Case_Statement (Var_Loc,
3814 Expression =>
3815 Make_Selected_Component (Var_Loc,
3816 Prefix => New_Occurrence_Of (Object_Entity, Var_Loc),
3817 Selector_Name => New_Occurrence_Of
3818 (Entity
3819 (Name (Variant_Part (Comp_List))), Var_Loc)),
3820 Alternatives => Variant_Alts));
3821 end;
3822 end if;
3824 return Stmts;
3825 end Build_Invariant_Checks;
3827 -- Start of processing for Build_Record_Invariant_Proc
3829 begin
3830 Invariant_Found := False;
3831 Type_Def := Type_Definition (Parent (R_Type));
3833 if Nkind (Type_Def) = N_Record_Definition
3834 and then not Null_Present (Type_Def)
3835 then
3836 Stmts := Build_Invariant_Checks (Component_List (Type_Def));
3837 else
3838 return Empty;
3839 end if;
3841 if not Invariant_Found then
3842 return Empty;
3843 end if;
3845 -- The name of the invariant procedure reflects the fact that the
3846 -- checks correspond to invariants on the component types. The
3847 -- record type itself may have invariants that will create a separate
3848 -- procedure whose name carries the Invariant suffix.
3850 Proc_Id :=
3851 Make_Defining_Identifier (Loc,
3852 Chars => New_External_Name (Chars (R_Type), "CInvariant"));
3854 Proc_Body :=
3855 Make_Subprogram_Body (Loc,
3856 Specification =>
3857 Make_Procedure_Specification (Loc,
3858 Defining_Unit_Name => Proc_Id,
3859 Parameter_Specifications => New_List (
3860 Make_Parameter_Specification (Loc,
3861 Defining_Identifier => Object_Entity,
3862 Parameter_Type => New_Occurrence_Of (R_Type, Loc)))),
3864 Declarations => Empty_List,
3865 Handled_Statement_Sequence =>
3866 Make_Handled_Sequence_Of_Statements (Loc,
3867 Statements => Stmts));
3869 Set_Ekind (Proc_Id, E_Procedure);
3870 Set_Is_Public (Proc_Id, Is_Public (R_Type));
3871 Set_Is_Internal (Proc_Id);
3872 Set_Has_Completion (Proc_Id);
3874 return Proc_Body;
3875 -- Insert_After (Nod, Proc_Body);
3876 -- Analyze (Proc_Body);
3877 end Build_Record_Invariant_Proc;
3879 ----------------------------
3880 -- Build_Slice_Assignment --
3881 ----------------------------
3883 -- Generates the following subprogram:
3885 -- procedure Assign
3886 -- (Source, Target : Array_Type,
3887 -- Left_Lo, Left_Hi : Index;
3888 -- Right_Lo, Right_Hi : Index;
3889 -- Rev : Boolean)
3890 -- is
3891 -- Li1 : Index;
3892 -- Ri1 : Index;
3894 -- begin
3896 -- if Left_Hi < Left_Lo then
3897 -- return;
3898 -- end if;
3900 -- if Rev then
3901 -- Li1 := Left_Hi;
3902 -- Ri1 := Right_Hi;
3903 -- else
3904 -- Li1 := Left_Lo;
3905 -- Ri1 := Right_Lo;
3906 -- end if;
3908 -- loop
3909 -- Target (Li1) := Source (Ri1);
3911 -- if Rev then
3912 -- exit when Li1 = Left_Lo;
3913 -- Li1 := Index'pred (Li1);
3914 -- Ri1 := Index'pred (Ri1);
3915 -- else
3916 -- exit when Li1 = Left_Hi;
3917 -- Li1 := Index'succ (Li1);
3918 -- Ri1 := Index'succ (Ri1);
3919 -- end if;
3920 -- end loop;
3921 -- end Assign;
3923 procedure Build_Slice_Assignment (Typ : Entity_Id) is
3924 Loc : constant Source_Ptr := Sloc (Typ);
3925 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
3927 Larray : constant Entity_Id := Make_Temporary (Loc, 'A');
3928 Rarray : constant Entity_Id := Make_Temporary (Loc, 'R');
3929 Left_Lo : constant Entity_Id := Make_Temporary (Loc, 'L');
3930 Left_Hi : constant Entity_Id := Make_Temporary (Loc, 'L');
3931 Right_Lo : constant Entity_Id := Make_Temporary (Loc, 'R');
3932 Right_Hi : constant Entity_Id := Make_Temporary (Loc, 'R');
3933 Rev : constant Entity_Id := Make_Temporary (Loc, 'D');
3934 -- Formal parameters of procedure
3936 Proc_Name : constant Entity_Id :=
3937 Make_Defining_Identifier (Loc,
3938 Chars => Make_TSS_Name (Typ, TSS_Slice_Assign));
3940 Lnn : constant Entity_Id := Make_Temporary (Loc, 'L');
3941 Rnn : constant Entity_Id := Make_Temporary (Loc, 'R');
3942 -- Subscripts for left and right sides
3944 Decls : List_Id;
3945 Loops : Node_Id;
3946 Stats : List_Id;
3948 begin
3949 -- Build declarations for indexes
3951 Decls := New_List;
3953 Append_To (Decls,
3954 Make_Object_Declaration (Loc,
3955 Defining_Identifier => Lnn,
3956 Object_Definition =>
3957 New_Occurrence_Of (Index, Loc)));
3959 Append_To (Decls,
3960 Make_Object_Declaration (Loc,
3961 Defining_Identifier => Rnn,
3962 Object_Definition =>
3963 New_Occurrence_Of (Index, Loc)));
3965 Stats := New_List;
3967 -- Build test for empty slice case
3969 Append_To (Stats,
3970 Make_If_Statement (Loc,
3971 Condition =>
3972 Make_Op_Lt (Loc,
3973 Left_Opnd => New_Occurrence_Of (Left_Hi, Loc),
3974 Right_Opnd => New_Occurrence_Of (Left_Lo, Loc)),
3975 Then_Statements => New_List (Make_Simple_Return_Statement (Loc))));
3977 -- Build initializations for indexes
3979 declare
3980 F_Init : constant List_Id := New_List;
3981 B_Init : constant List_Id := New_List;
3983 begin
3984 Append_To (F_Init,
3985 Make_Assignment_Statement (Loc,
3986 Name => New_Occurrence_Of (Lnn, Loc),
3987 Expression => New_Occurrence_Of (Left_Lo, Loc)));
3989 Append_To (F_Init,
3990 Make_Assignment_Statement (Loc,
3991 Name => New_Occurrence_Of (Rnn, Loc),
3992 Expression => New_Occurrence_Of (Right_Lo, Loc)));
3994 Append_To (B_Init,
3995 Make_Assignment_Statement (Loc,
3996 Name => New_Occurrence_Of (Lnn, Loc),
3997 Expression => New_Occurrence_Of (Left_Hi, Loc)));
3999 Append_To (B_Init,
4000 Make_Assignment_Statement (Loc,
4001 Name => New_Occurrence_Of (Rnn, Loc),
4002 Expression => New_Occurrence_Of (Right_Hi, Loc)));
4004 Append_To (Stats,
4005 Make_If_Statement (Loc,
4006 Condition => New_Occurrence_Of (Rev, Loc),
4007 Then_Statements => B_Init,
4008 Else_Statements => F_Init));
4009 end;
4011 -- Now construct the assignment statement
4013 Loops :=
4014 Make_Loop_Statement (Loc,
4015 Statements => New_List (
4016 Make_Assignment_Statement (Loc,
4017 Name =>
4018 Make_Indexed_Component (Loc,
4019 Prefix => New_Occurrence_Of (Larray, Loc),
4020 Expressions => New_List (New_Occurrence_Of (Lnn, Loc))),
4021 Expression =>
4022 Make_Indexed_Component (Loc,
4023 Prefix => New_Occurrence_Of (Rarray, Loc),
4024 Expressions => New_List (New_Occurrence_Of (Rnn, Loc))))),
4025 End_Label => Empty);
4027 -- Build the exit condition and increment/decrement statements
4029 declare
4030 F_Ass : constant List_Id := New_List;
4031 B_Ass : constant List_Id := New_List;
4033 begin
4034 Append_To (F_Ass,
4035 Make_Exit_Statement (Loc,
4036 Condition =>
4037 Make_Op_Eq (Loc,
4038 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
4039 Right_Opnd => New_Occurrence_Of (Left_Hi, Loc))));
4041 Append_To (F_Ass,
4042 Make_Assignment_Statement (Loc,
4043 Name => New_Occurrence_Of (Lnn, Loc),
4044 Expression =>
4045 Make_Attribute_Reference (Loc,
4046 Prefix =>
4047 New_Occurrence_Of (Index, Loc),
4048 Attribute_Name => Name_Succ,
4049 Expressions => New_List (
4050 New_Occurrence_Of (Lnn, Loc)))));
4052 Append_To (F_Ass,
4053 Make_Assignment_Statement (Loc,
4054 Name => New_Occurrence_Of (Rnn, Loc),
4055 Expression =>
4056 Make_Attribute_Reference (Loc,
4057 Prefix =>
4058 New_Occurrence_Of (Index, Loc),
4059 Attribute_Name => Name_Succ,
4060 Expressions => New_List (
4061 New_Occurrence_Of (Rnn, Loc)))));
4063 Append_To (B_Ass,
4064 Make_Exit_Statement (Loc,
4065 Condition =>
4066 Make_Op_Eq (Loc,
4067 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
4068 Right_Opnd => New_Occurrence_Of (Left_Lo, Loc))));
4070 Append_To (B_Ass,
4071 Make_Assignment_Statement (Loc,
4072 Name => New_Occurrence_Of (Lnn, Loc),
4073 Expression =>
4074 Make_Attribute_Reference (Loc,
4075 Prefix =>
4076 New_Occurrence_Of (Index, Loc),
4077 Attribute_Name => Name_Pred,
4078 Expressions => New_List (
4079 New_Occurrence_Of (Lnn, Loc)))));
4081 Append_To (B_Ass,
4082 Make_Assignment_Statement (Loc,
4083 Name => New_Occurrence_Of (Rnn, Loc),
4084 Expression =>
4085 Make_Attribute_Reference (Loc,
4086 Prefix =>
4087 New_Occurrence_Of (Index, Loc),
4088 Attribute_Name => Name_Pred,
4089 Expressions => New_List (
4090 New_Occurrence_Of (Rnn, Loc)))));
4092 Append_To (Statements (Loops),
4093 Make_If_Statement (Loc,
4094 Condition => New_Occurrence_Of (Rev, Loc),
4095 Then_Statements => B_Ass,
4096 Else_Statements => F_Ass));
4097 end;
4099 Append_To (Stats, Loops);
4101 declare
4102 Spec : Node_Id;
4103 Formals : List_Id := New_List;
4105 begin
4106 Formals := New_List (
4107 Make_Parameter_Specification (Loc,
4108 Defining_Identifier => Larray,
4109 Out_Present => True,
4110 Parameter_Type =>
4111 New_Occurrence_Of (Base_Type (Typ), Loc)),
4113 Make_Parameter_Specification (Loc,
4114 Defining_Identifier => Rarray,
4115 Parameter_Type =>
4116 New_Occurrence_Of (Base_Type (Typ), Loc)),
4118 Make_Parameter_Specification (Loc,
4119 Defining_Identifier => Left_Lo,
4120 Parameter_Type =>
4121 New_Occurrence_Of (Index, Loc)),
4123 Make_Parameter_Specification (Loc,
4124 Defining_Identifier => Left_Hi,
4125 Parameter_Type =>
4126 New_Occurrence_Of (Index, Loc)),
4128 Make_Parameter_Specification (Loc,
4129 Defining_Identifier => Right_Lo,
4130 Parameter_Type =>
4131 New_Occurrence_Of (Index, Loc)),
4133 Make_Parameter_Specification (Loc,
4134 Defining_Identifier => Right_Hi,
4135 Parameter_Type =>
4136 New_Occurrence_Of (Index, Loc)));
4138 Append_To (Formals,
4139 Make_Parameter_Specification (Loc,
4140 Defining_Identifier => Rev,
4141 Parameter_Type =>
4142 New_Occurrence_Of (Standard_Boolean, Loc)));
4144 Spec :=
4145 Make_Procedure_Specification (Loc,
4146 Defining_Unit_Name => Proc_Name,
4147 Parameter_Specifications => Formals);
4149 Discard_Node (
4150 Make_Subprogram_Body (Loc,
4151 Specification => Spec,
4152 Declarations => Decls,
4153 Handled_Statement_Sequence =>
4154 Make_Handled_Sequence_Of_Statements (Loc,
4155 Statements => Stats)));
4156 end;
4158 Set_TSS (Typ, Proc_Name);
4159 Set_Is_Pure (Proc_Name);
4160 end Build_Slice_Assignment;
4162 -----------------------------
4163 -- Build_Untagged_Equality --
4164 -----------------------------
4166 procedure Build_Untagged_Equality (Typ : Entity_Id) is
4167 Build_Eq : Boolean;
4168 Comp : Entity_Id;
4169 Decl : Node_Id;
4170 Op : Entity_Id;
4171 Prim : Elmt_Id;
4172 Eq_Op : Entity_Id;
4174 function User_Defined_Eq (T : Entity_Id) return Entity_Id;
4175 -- Check whether the type T has a user-defined primitive equality. If so
4176 -- return it, else return Empty. If true for a component of Typ, we have
4177 -- to build the primitive equality for it.
4179 ---------------------
4180 -- User_Defined_Eq --
4181 ---------------------
4183 function User_Defined_Eq (T : Entity_Id) return Entity_Id is
4184 Prim : Elmt_Id;
4185 Op : Entity_Id;
4187 begin
4188 Op := TSS (T, TSS_Composite_Equality);
4190 if Present (Op) then
4191 return Op;
4192 end if;
4194 Prim := First_Elmt (Collect_Primitive_Operations (T));
4195 while Present (Prim) loop
4196 Op := Node (Prim);
4198 if Chars (Op) = Name_Op_Eq
4199 and then Etype (Op) = Standard_Boolean
4200 and then Etype (First_Formal (Op)) = T
4201 and then Etype (Next_Formal (First_Formal (Op))) = T
4202 then
4203 return Op;
4204 end if;
4206 Next_Elmt (Prim);
4207 end loop;
4209 return Empty;
4210 end User_Defined_Eq;
4212 -- Start of processing for Build_Untagged_Equality
4214 begin
4215 -- If a record component has a primitive equality operation, we must
4216 -- build the corresponding one for the current type.
4218 Build_Eq := False;
4219 Comp := First_Component (Typ);
4220 while Present (Comp) loop
4221 if Is_Record_Type (Etype (Comp))
4222 and then Present (User_Defined_Eq (Etype (Comp)))
4223 then
4224 Build_Eq := True;
4225 end if;
4227 Next_Component (Comp);
4228 end loop;
4230 -- If there is a user-defined equality for the type, we do not create
4231 -- the implicit one.
4233 Prim := First_Elmt (Collect_Primitive_Operations (Typ));
4234 Eq_Op := Empty;
4235 while Present (Prim) loop
4236 if Chars (Node (Prim)) = Name_Op_Eq
4237 and then Comes_From_Source (Node (Prim))
4239 -- Don't we also need to check formal types and return type as in
4240 -- User_Defined_Eq above???
4242 then
4243 Eq_Op := Node (Prim);
4244 Build_Eq := False;
4245 exit;
4246 end if;
4248 Next_Elmt (Prim);
4249 end loop;
4251 -- If the type is derived, inherit the operation, if present, from the
4252 -- parent type. It may have been declared after the type derivation. If
4253 -- the parent type itself is derived, it may have inherited an operation
4254 -- that has itself been overridden, so update its alias and related
4255 -- flags. Ditto for inequality.
4257 if No (Eq_Op) and then Is_Derived_Type (Typ) then
4258 Prim := First_Elmt (Collect_Primitive_Operations (Etype (Typ)));
4259 while Present (Prim) loop
4260 if Chars (Node (Prim)) = Name_Op_Eq then
4261 Copy_TSS (Node (Prim), Typ);
4262 Build_Eq := False;
4264 declare
4265 Op : constant Entity_Id := User_Defined_Eq (Typ);
4266 Eq_Op : constant Entity_Id := Node (Prim);
4267 NE_Op : constant Entity_Id := Next_Entity (Eq_Op);
4269 begin
4270 if Present (Op) then
4271 Set_Alias (Op, Eq_Op);
4272 Set_Is_Abstract_Subprogram
4273 (Op, Is_Abstract_Subprogram (Eq_Op));
4275 if Chars (Next_Entity (Op)) = Name_Op_Ne then
4276 Set_Is_Abstract_Subprogram
4277 (Next_Entity (Op), Is_Abstract_Subprogram (NE_Op));
4278 end if;
4279 end if;
4280 end;
4282 exit;
4283 end if;
4285 Next_Elmt (Prim);
4286 end loop;
4287 end if;
4289 -- If not inherited and not user-defined, build body as for a type with
4290 -- tagged components.
4292 if Build_Eq then
4293 Decl :=
4294 Make_Eq_Body (Typ, Make_TSS_Name (Typ, TSS_Composite_Equality));
4295 Op := Defining_Entity (Decl);
4296 Set_TSS (Typ, Op);
4297 Set_Is_Pure (Op);
4299 if Is_Library_Level_Entity (Typ) then
4300 Set_Is_Public (Op);
4301 end if;
4302 end if;
4303 end Build_Untagged_Equality;
4305 -----------------------------------
4306 -- Build_Variant_Record_Equality --
4307 -----------------------------------
4309 -- Generates:
4311 -- function _Equality (X, Y : T) return Boolean is
4312 -- begin
4313 -- -- Compare discriminants
4315 -- if X.D1 /= Y.D1 or else X.D2 /= Y.D2 or else ... then
4316 -- return False;
4317 -- end if;
4319 -- -- Compare components
4321 -- if X.C1 /= Y.C1 or else X.C2 /= Y.C2 or else ... then
4322 -- return False;
4323 -- end if;
4325 -- -- Compare variant part
4327 -- case X.D1 is
4328 -- when V1 =>
4329 -- if X.C2 /= Y.C2 or else X.C3 /= Y.C3 or else ... then
4330 -- return False;
4331 -- end if;
4332 -- ...
4333 -- when Vn =>
4334 -- if X.Cn /= Y.Cn or else ... then
4335 -- return False;
4336 -- end if;
4337 -- end case;
4339 -- return True;
4340 -- end _Equality;
4342 procedure Build_Variant_Record_Equality (Typ : Entity_Id) is
4343 Loc : constant Source_Ptr := Sloc (Typ);
4345 F : constant Entity_Id :=
4346 Make_Defining_Identifier (Loc,
4347 Chars => Make_TSS_Name (Typ, TSS_Composite_Equality));
4349 X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_X);
4350 Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_Y);
4352 Def : constant Node_Id := Parent (Typ);
4353 Comps : constant Node_Id := Component_List (Type_Definition (Def));
4354 Stmts : constant List_Id := New_List;
4355 Pspecs : constant List_Id := New_List;
4357 begin
4358 -- If we have a variant record with restriction No_Implicit_Conditionals
4359 -- in effect, then we skip building the procedure. This is safe because
4360 -- if we can see the restriction, so can any caller, calls to equality
4361 -- test routines are not allowed for variant records if this restriction
4362 -- is active.
4364 if Restriction_Active (No_Implicit_Conditionals) then
4365 return;
4366 end if;
4368 -- Derived Unchecked_Union types no longer inherit the equality function
4369 -- of their parent.
4371 if Is_Derived_Type (Typ)
4372 and then not Is_Unchecked_Union (Typ)
4373 and then not Has_New_Non_Standard_Rep (Typ)
4374 then
4375 declare
4376 Parent_Eq : constant Entity_Id :=
4377 TSS (Root_Type (Typ), TSS_Composite_Equality);
4378 begin
4379 if Present (Parent_Eq) then
4380 Copy_TSS (Parent_Eq, Typ);
4381 return;
4382 end if;
4383 end;
4384 end if;
4386 Discard_Node (
4387 Make_Subprogram_Body (Loc,
4388 Specification =>
4389 Make_Function_Specification (Loc,
4390 Defining_Unit_Name => F,
4391 Parameter_Specifications => Pspecs,
4392 Result_Definition => New_Occurrence_Of (Standard_Boolean, Loc)),
4393 Declarations => New_List,
4394 Handled_Statement_Sequence =>
4395 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts)));
4397 Append_To (Pspecs,
4398 Make_Parameter_Specification (Loc,
4399 Defining_Identifier => X,
4400 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
4402 Append_To (Pspecs,
4403 Make_Parameter_Specification (Loc,
4404 Defining_Identifier => Y,
4405 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
4407 -- Unchecked_Unions require additional machinery to support equality.
4408 -- Two extra parameters (A and B) are added to the equality function
4409 -- parameter list for each discriminant of the type, in order to
4410 -- capture the inferred values of the discriminants in equality calls.
4411 -- The names of the parameters match the names of the corresponding
4412 -- discriminant, with an added suffix.
4414 if Is_Unchecked_Union (Typ) then
4415 declare
4416 Discr : Entity_Id;
4417 Discr_Type : Entity_Id;
4418 A, B : Entity_Id;
4419 New_Discrs : Elist_Id;
4421 begin
4422 New_Discrs := New_Elmt_List;
4424 Discr := First_Discriminant (Typ);
4425 while Present (Discr) loop
4426 Discr_Type := Etype (Discr);
4427 A := Make_Defining_Identifier (Loc,
4428 Chars => New_External_Name (Chars (Discr), 'A'));
4430 B := Make_Defining_Identifier (Loc,
4431 Chars => New_External_Name (Chars (Discr), 'B'));
4433 -- Add new parameters to the parameter list
4435 Append_To (Pspecs,
4436 Make_Parameter_Specification (Loc,
4437 Defining_Identifier => A,
4438 Parameter_Type =>
4439 New_Occurrence_Of (Discr_Type, Loc)));
4441 Append_To (Pspecs,
4442 Make_Parameter_Specification (Loc,
4443 Defining_Identifier => B,
4444 Parameter_Type =>
4445 New_Occurrence_Of (Discr_Type, Loc)));
4447 Append_Elmt (A, New_Discrs);
4449 -- Generate the following code to compare each of the inferred
4450 -- discriminants:
4452 -- if a /= b then
4453 -- return False;
4454 -- end if;
4456 Append_To (Stmts,
4457 Make_If_Statement (Loc,
4458 Condition =>
4459 Make_Op_Ne (Loc,
4460 Left_Opnd => New_Occurrence_Of (A, Loc),
4461 Right_Opnd => New_Occurrence_Of (B, Loc)),
4462 Then_Statements => New_List (
4463 Make_Simple_Return_Statement (Loc,
4464 Expression =>
4465 New_Occurrence_Of (Standard_False, Loc)))));
4466 Next_Discriminant (Discr);
4467 end loop;
4469 -- Generate component-by-component comparison. Note that we must
4470 -- propagate the inferred discriminants formals to act as
4471 -- the case statement switch. Their value is added when an
4472 -- equality call on unchecked unions is expanded.
4474 Append_List_To (Stmts, Make_Eq_Case (Typ, Comps, New_Discrs));
4475 end;
4477 -- Normal case (not unchecked union)
4479 else
4480 Append_To (Stmts,
4481 Make_Eq_If (Typ, Discriminant_Specifications (Def)));
4482 Append_List_To (Stmts, Make_Eq_Case (Typ, Comps));
4483 end if;
4485 Append_To (Stmts,
4486 Make_Simple_Return_Statement (Loc,
4487 Expression => New_Occurrence_Of (Standard_True, Loc)));
4489 Set_TSS (Typ, F);
4490 Set_Is_Pure (F);
4492 if not Debug_Generated_Code then
4493 Set_Debug_Info_Off (F);
4494 end if;
4495 end Build_Variant_Record_Equality;
4497 -----------------------------
4498 -- Check_Stream_Attributes --
4499 -----------------------------
4501 procedure Check_Stream_Attributes (Typ : Entity_Id) is
4502 Comp : Entity_Id;
4503 Par_Read : constant Boolean :=
4504 Stream_Attribute_Available (Typ, TSS_Stream_Read)
4505 and then not Has_Specified_Stream_Read (Typ);
4506 Par_Write : constant Boolean :=
4507 Stream_Attribute_Available (Typ, TSS_Stream_Write)
4508 and then not Has_Specified_Stream_Write (Typ);
4510 procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type);
4511 -- Check that Comp has a user-specified Nam stream attribute
4513 ----------------
4514 -- Check_Attr --
4515 ----------------
4517 procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type) is
4518 begin
4519 if not Stream_Attribute_Available (Etype (Comp), TSS_Nam) then
4520 Error_Msg_Name_1 := Nam;
4521 Error_Msg_N
4522 ("|component& in limited extension must have% attribute", Comp);
4523 end if;
4524 end Check_Attr;
4526 -- Start of processing for Check_Stream_Attributes
4528 begin
4529 if Par_Read or else Par_Write then
4530 Comp := First_Component (Typ);
4531 while Present (Comp) loop
4532 if Comes_From_Source (Comp)
4533 and then Original_Record_Component (Comp) = Comp
4534 and then Is_Limited_Type (Etype (Comp))
4535 then
4536 if Par_Read then
4537 Check_Attr (Name_Read, TSS_Stream_Read);
4538 end if;
4540 if Par_Write then
4541 Check_Attr (Name_Write, TSS_Stream_Write);
4542 end if;
4543 end if;
4545 Next_Component (Comp);
4546 end loop;
4547 end if;
4548 end Check_Stream_Attributes;
4550 -----------------------------
4551 -- Expand_Record_Extension --
4552 -----------------------------
4554 -- Add a field _parent at the beginning of the record extension. This is
4555 -- used to implement inheritance. Here are some examples of expansion:
4557 -- 1. no discriminants
4558 -- type T2 is new T1 with null record;
4559 -- gives
4560 -- type T2 is new T1 with record
4561 -- _Parent : T1;
4562 -- end record;
4564 -- 2. renamed discriminants
4565 -- type T2 (B, C : Int) is new T1 (A => B) with record
4566 -- _Parent : T1 (A => B);
4567 -- D : Int;
4568 -- end;
4570 -- 3. inherited discriminants
4571 -- type T2 is new T1 with record -- discriminant A inherited
4572 -- _Parent : T1 (A);
4573 -- D : Int;
4574 -- end;
4576 procedure Expand_Record_Extension (T : Entity_Id; Def : Node_Id) is
4577 Indic : constant Node_Id := Subtype_Indication (Def);
4578 Loc : constant Source_Ptr := Sloc (Def);
4579 Rec_Ext_Part : Node_Id := Record_Extension_Part (Def);
4580 Par_Subtype : Entity_Id;
4581 Comp_List : Node_Id;
4582 Comp_Decl : Node_Id;
4583 Parent_N : Node_Id;
4584 D : Entity_Id;
4585 List_Constr : constant List_Id := New_List;
4587 begin
4588 -- Expand_Record_Extension is called directly from the semantics, so
4589 -- we must check to see whether expansion is active before proceeding,
4590 -- because this affects the visibility of selected components in bodies
4591 -- of instances.
4593 if not Expander_Active then
4594 return;
4595 end if;
4597 -- This may be a derivation of an untagged private type whose full
4598 -- view is tagged, in which case the Derived_Type_Definition has no
4599 -- extension part. Build an empty one now.
4601 if No (Rec_Ext_Part) then
4602 Rec_Ext_Part :=
4603 Make_Record_Definition (Loc,
4604 End_Label => Empty,
4605 Component_List => Empty,
4606 Null_Present => True);
4608 Set_Record_Extension_Part (Def, Rec_Ext_Part);
4609 Mark_Rewrite_Insertion (Rec_Ext_Part);
4610 end if;
4612 Comp_List := Component_List (Rec_Ext_Part);
4614 Parent_N := Make_Defining_Identifier (Loc, Name_uParent);
4616 -- If the derived type inherits its discriminants the type of the
4617 -- _parent field must be constrained by the inherited discriminants
4619 if Has_Discriminants (T)
4620 and then Nkind (Indic) /= N_Subtype_Indication
4621 and then not Is_Constrained (Entity (Indic))
4622 then
4623 D := First_Discriminant (T);
4624 while Present (D) loop
4625 Append_To (List_Constr, New_Occurrence_Of (D, Loc));
4626 Next_Discriminant (D);
4627 end loop;
4629 Par_Subtype :=
4630 Process_Subtype (
4631 Make_Subtype_Indication (Loc,
4632 Subtype_Mark => New_Occurrence_Of (Entity (Indic), Loc),
4633 Constraint =>
4634 Make_Index_Or_Discriminant_Constraint (Loc,
4635 Constraints => List_Constr)),
4636 Def);
4638 -- Otherwise the original subtype_indication is just what is needed
4640 else
4641 Par_Subtype := Process_Subtype (New_Copy_Tree (Indic), Def);
4642 end if;
4644 Set_Parent_Subtype (T, Par_Subtype);
4646 Comp_Decl :=
4647 Make_Component_Declaration (Loc,
4648 Defining_Identifier => Parent_N,
4649 Component_Definition =>
4650 Make_Component_Definition (Loc,
4651 Aliased_Present => False,
4652 Subtype_Indication => New_Occurrence_Of (Par_Subtype, Loc)));
4654 if Null_Present (Rec_Ext_Part) then
4655 Set_Component_List (Rec_Ext_Part,
4656 Make_Component_List (Loc,
4657 Component_Items => New_List (Comp_Decl),
4658 Variant_Part => Empty,
4659 Null_Present => False));
4660 Set_Null_Present (Rec_Ext_Part, False);
4662 elsif Null_Present (Comp_List)
4663 or else Is_Empty_List (Component_Items (Comp_List))
4664 then
4665 Set_Component_Items (Comp_List, New_List (Comp_Decl));
4666 Set_Null_Present (Comp_List, False);
4668 else
4669 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
4670 end if;
4672 Analyze (Comp_Decl);
4673 end Expand_Record_Extension;
4675 ------------------------------------
4676 -- Expand_N_Full_Type_Declaration --
4677 ------------------------------------
4679 procedure Expand_N_Full_Type_Declaration (N : Node_Id) is
4680 procedure Build_Master (Ptr_Typ : Entity_Id);
4681 -- Create the master associated with Ptr_Typ
4683 ------------------
4684 -- Build_Master --
4685 ------------------
4687 procedure Build_Master (Ptr_Typ : Entity_Id) is
4688 Desig_Typ : Entity_Id := Designated_Type (Ptr_Typ);
4690 begin
4691 -- If the designated type is an incomplete view coming from a
4692 -- limited-with'ed package, we need to use the nonlimited view in
4693 -- case it has tasks.
4695 if Ekind (Desig_Typ) in Incomplete_Kind
4696 and then Present (Non_Limited_View (Desig_Typ))
4697 then
4698 Desig_Typ := Non_Limited_View (Desig_Typ);
4699 end if;
4701 -- Anonymous access types are created for the components of the
4702 -- record parameter for an entry declaration. No master is created
4703 -- for such a type.
4705 if Comes_From_Source (N) and then Has_Task (Desig_Typ) then
4706 Build_Master_Entity (Ptr_Typ);
4707 Build_Master_Renaming (Ptr_Typ);
4709 -- Create a class-wide master because a Master_Id must be generated
4710 -- for access-to-limited-class-wide types whose root may be extended
4711 -- with task components.
4713 -- Note: This code covers access-to-limited-interfaces because they
4714 -- can be used to reference tasks implementing them.
4716 elsif Is_Limited_Class_Wide_Type (Desig_Typ)
4717 and then Tasking_Allowed
4719 -- Do not create a class-wide master for types whose convention is
4720 -- Java since these types cannot embed Ada tasks anyway. Note that
4721 -- the following test cannot catch the following case:
4723 -- package java.lang.Object is
4724 -- type Typ is tagged limited private;
4725 -- type Ref is access all Typ'Class;
4726 -- private
4727 -- type Typ is tagged limited ...;
4728 -- pragma Convention (Typ, Java)
4729 -- end;
4731 -- Because the convention appears after we have done the
4732 -- processing for type Ref.
4734 and then Convention (Desig_Typ) /= Convention_Java
4735 and then Convention (Desig_Typ) /= Convention_CIL
4736 then
4737 Build_Class_Wide_Master (Ptr_Typ);
4738 end if;
4739 end Build_Master;
4741 -- Local declarations
4743 Def_Id : constant Entity_Id := Defining_Identifier (N);
4744 B_Id : constant Entity_Id := Base_Type (Def_Id);
4745 FN : Node_Id;
4746 Par_Id : Entity_Id;
4748 -- Start of processing for Expand_N_Full_Type_Declaration
4750 begin
4751 if Is_Access_Type (Def_Id) then
4752 Build_Master (Def_Id);
4754 if Ekind (Def_Id) = E_Access_Protected_Subprogram_Type then
4755 Expand_Access_Protected_Subprogram_Type (N);
4756 end if;
4758 -- Array of anonymous access-to-task pointers
4760 elsif Ada_Version >= Ada_2005
4761 and then Is_Array_Type (Def_Id)
4762 and then Is_Access_Type (Component_Type (Def_Id))
4763 and then Ekind (Component_Type (Def_Id)) = E_Anonymous_Access_Type
4764 then
4765 Build_Master (Component_Type (Def_Id));
4767 elsif Has_Task (Def_Id) then
4768 Expand_Previous_Access_Type (Def_Id);
4770 -- Check the components of a record type or array of records for
4771 -- anonymous access-to-task pointers.
4773 elsif Ada_Version >= Ada_2005
4774 and then (Is_Record_Type (Def_Id)
4775 or else
4776 (Is_Array_Type (Def_Id)
4777 and then Is_Record_Type (Component_Type (Def_Id))))
4778 then
4779 declare
4780 Comp : Entity_Id;
4781 First : Boolean;
4782 M_Id : Entity_Id;
4783 Typ : Entity_Id;
4785 begin
4786 if Is_Array_Type (Def_Id) then
4787 Comp := First_Entity (Component_Type (Def_Id));
4788 else
4789 Comp := First_Entity (Def_Id);
4790 end if;
4792 -- Examine all components looking for anonymous access-to-task
4793 -- types.
4795 First := True;
4796 while Present (Comp) loop
4797 Typ := Etype (Comp);
4799 if Ekind (Typ) = E_Anonymous_Access_Type
4800 and then Has_Task (Available_View (Designated_Type (Typ)))
4801 and then No (Master_Id (Typ))
4802 then
4803 -- Ensure that the record or array type have a _master
4805 if First then
4806 Build_Master_Entity (Def_Id);
4807 Build_Master_Renaming (Typ);
4808 M_Id := Master_Id (Typ);
4810 First := False;
4812 -- Reuse the same master to service any additional types
4814 else
4815 Set_Master_Id (Typ, M_Id);
4816 end if;
4817 end if;
4819 Next_Entity (Comp);
4820 end loop;
4821 end;
4822 end if;
4824 Par_Id := Etype (B_Id);
4826 -- The parent type is private then we need to inherit any TSS operations
4827 -- from the full view.
4829 if Ekind (Par_Id) in Private_Kind
4830 and then Present (Full_View (Par_Id))
4831 then
4832 Par_Id := Base_Type (Full_View (Par_Id));
4833 end if;
4835 if Nkind (Type_Definition (Original_Node (N))) =
4836 N_Derived_Type_Definition
4837 and then not Is_Tagged_Type (Def_Id)
4838 and then Present (Freeze_Node (Par_Id))
4839 and then Present (TSS_Elist (Freeze_Node (Par_Id)))
4840 then
4841 Ensure_Freeze_Node (B_Id);
4842 FN := Freeze_Node (B_Id);
4844 if No (TSS_Elist (FN)) then
4845 Set_TSS_Elist (FN, New_Elmt_List);
4846 end if;
4848 declare
4849 T_E : constant Elist_Id := TSS_Elist (FN);
4850 Elmt : Elmt_Id;
4852 begin
4853 Elmt := First_Elmt (TSS_Elist (Freeze_Node (Par_Id)));
4854 while Present (Elmt) loop
4855 if Chars (Node (Elmt)) /= Name_uInit then
4856 Append_Elmt (Node (Elmt), T_E);
4857 end if;
4859 Next_Elmt (Elmt);
4860 end loop;
4862 -- If the derived type itself is private with a full view, then
4863 -- associate the full view with the inherited TSS_Elist as well.
4865 if Ekind (B_Id) in Private_Kind
4866 and then Present (Full_View (B_Id))
4867 then
4868 Ensure_Freeze_Node (Base_Type (Full_View (B_Id)));
4869 Set_TSS_Elist
4870 (Freeze_Node (Base_Type (Full_View (B_Id))), TSS_Elist (FN));
4871 end if;
4872 end;
4873 end if;
4874 end Expand_N_Full_Type_Declaration;
4876 ---------------------------------
4877 -- Expand_N_Object_Declaration --
4878 ---------------------------------
4880 procedure Expand_N_Object_Declaration (N : Node_Id) is
4881 Def_Id : constant Entity_Id := Defining_Identifier (N);
4882 Expr : constant Node_Id := Expression (N);
4883 Loc : constant Source_Ptr := Sloc (N);
4884 Obj_Def : constant Node_Id := Object_Definition (N);
4885 Typ : constant Entity_Id := Etype (Def_Id);
4886 Base_Typ : constant Entity_Id := Base_Type (Typ);
4887 Expr_Q : Node_Id;
4889 function Build_Equivalent_Aggregate return Boolean;
4890 -- If the object has a constrained discriminated type and no initial
4891 -- value, it may be possible to build an equivalent aggregate instead,
4892 -- and prevent an actual call to the initialization procedure.
4894 procedure Default_Initialize_Object (After : Node_Id);
4895 -- Generate all default initialization actions for object Def_Id. Any
4896 -- new code is inserted after node After.
4898 function Rewrite_As_Renaming return Boolean;
4899 -- Indicate whether to rewrite a declaration with initialization into an
4900 -- object renaming declaration (see below).
4902 --------------------------------
4903 -- Build_Equivalent_Aggregate --
4904 --------------------------------
4906 function Build_Equivalent_Aggregate return Boolean is
4907 Aggr : Node_Id;
4908 Comp : Entity_Id;
4909 Discr : Elmt_Id;
4910 Full_Type : Entity_Id;
4912 begin
4913 Full_Type := Typ;
4915 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
4916 Full_Type := Full_View (Typ);
4917 end if;
4919 -- Only perform this transformation if Elaboration_Code is forbidden
4920 -- or undesirable, and if this is a global entity of a constrained
4921 -- record type.
4923 -- If Initialize_Scalars might be active this transformation cannot
4924 -- be performed either, because it will lead to different semantics
4925 -- or because elaboration code will in fact be created.
4927 if Ekind (Full_Type) /= E_Record_Subtype
4928 or else not Has_Discriminants (Full_Type)
4929 or else not Is_Constrained (Full_Type)
4930 or else Is_Controlled (Full_Type)
4931 or else Is_Limited_Type (Full_Type)
4932 or else not Restriction_Active (No_Initialize_Scalars)
4933 then
4934 return False;
4935 end if;
4937 if Ekind (Current_Scope) = E_Package
4938 and then
4939 (Restriction_Active (No_Elaboration_Code)
4940 or else Is_Preelaborated (Current_Scope))
4941 then
4942 -- Building a static aggregate is possible if the discriminants
4943 -- have static values and the other components have static
4944 -- defaults or none.
4946 Discr := First_Elmt (Discriminant_Constraint (Full_Type));
4947 while Present (Discr) loop
4948 if not Is_OK_Static_Expression (Node (Discr)) then
4949 return False;
4950 end if;
4952 Next_Elmt (Discr);
4953 end loop;
4955 -- Check that initialized components are OK, and that non-
4956 -- initialized components do not require a call to their own
4957 -- initialization procedure.
4959 Comp := First_Component (Full_Type);
4960 while Present (Comp) loop
4961 if Ekind (Comp) = E_Component
4962 and then Present (Expression (Parent (Comp)))
4963 and then
4964 not Is_OK_Static_Expression (Expression (Parent (Comp)))
4965 then
4966 return False;
4968 elsif Has_Non_Null_Base_Init_Proc (Etype (Comp)) then
4969 return False;
4971 end if;
4973 Next_Component (Comp);
4974 end loop;
4976 -- Everything is static, assemble the aggregate, discriminant
4977 -- values first.
4979 Aggr :=
4980 Make_Aggregate (Loc,
4981 Expressions => New_List,
4982 Component_Associations => New_List);
4984 Discr := First_Elmt (Discriminant_Constraint (Full_Type));
4985 while Present (Discr) loop
4986 Append_To (Expressions (Aggr), New_Copy (Node (Discr)));
4987 Next_Elmt (Discr);
4988 end loop;
4990 -- Now collect values of initialized components
4992 Comp := First_Component (Full_Type);
4993 while Present (Comp) loop
4994 if Ekind (Comp) = E_Component
4995 and then Present (Expression (Parent (Comp)))
4996 then
4997 Append_To (Component_Associations (Aggr),
4998 Make_Component_Association (Loc,
4999 Choices => New_List (New_Occurrence_Of (Comp, Loc)),
5000 Expression => New_Copy_Tree
5001 (Expression (Parent (Comp)))));
5002 end if;
5004 Next_Component (Comp);
5005 end loop;
5007 -- Finally, box-initialize remaining components
5009 Append_To (Component_Associations (Aggr),
5010 Make_Component_Association (Loc,
5011 Choices => New_List (Make_Others_Choice (Loc)),
5012 Expression => Empty));
5013 Set_Box_Present (Last (Component_Associations (Aggr)));
5014 Set_Expression (N, Aggr);
5016 if Typ /= Full_Type then
5017 Analyze_And_Resolve (Aggr, Full_View (Base_Type (Full_Type)));
5018 Rewrite (Aggr, Unchecked_Convert_To (Typ, Aggr));
5019 Analyze_And_Resolve (Aggr, Typ);
5020 else
5021 Analyze_And_Resolve (Aggr, Full_Type);
5022 end if;
5024 return True;
5026 else
5027 return False;
5028 end if;
5029 end Build_Equivalent_Aggregate;
5031 -------------------------------
5032 -- Default_Initialize_Object --
5033 -------------------------------
5035 procedure Default_Initialize_Object (After : Node_Id) is
5036 function New_Object_Reference return Node_Id;
5037 -- Return a new reference to Def_Id with attributes Assignment_OK and
5038 -- Must_Not_Freeze already set.
5040 --------------------------
5041 -- New_Object_Reference --
5042 --------------------------
5044 function New_Object_Reference return Node_Id is
5045 Obj_Ref : constant Node_Id := New_Occurrence_Of (Def_Id, Loc);
5047 begin
5048 -- The call to the type init proc or [Deep_]Finalize must not
5049 -- freeze the related object as the call is internally generated.
5050 -- This way legal rep clauses that apply to the object will not be
5051 -- flagged. Note that the initialization call may be removed if
5052 -- pragma Import is encountered or moved to the freeze actions of
5053 -- the object because of an address clause.
5055 Set_Assignment_OK (Obj_Ref);
5056 Set_Must_Not_Freeze (Obj_Ref);
5058 return Obj_Ref;
5059 end New_Object_Reference;
5061 -- Local variables
5063 Abrt_Blk : Node_Id;
5064 Abrt_HSS : Node_Id;
5065 Abrt_Id : Entity_Id;
5066 Abrt_Stmts : List_Id;
5067 Aggr_Init : Node_Id;
5068 Comp_Init : List_Id := No_List;
5069 Fin_Call : Node_Id;
5070 Fin_Stmts : List_Id := No_List;
5071 Obj_Init : Node_Id := Empty;
5072 Obj_Ref : Node_Id;
5074 Dummy : Entity_Id;
5075 -- This variable captures a dummy internal entity, see the comment
5076 -- associated with its use.
5078 -- Start of processing for Default_Initialize_Object
5080 begin
5081 -- Default initialization is suppressed for objects that are already
5082 -- known to be imported (i.e. whose declaration specifies the Import
5083 -- aspect). Note that for objects with a pragma Import, we generate
5084 -- initialization here, and then remove it downstream when processing
5085 -- the pragma.
5087 if Is_Imported (Def_Id) then
5088 return;
5089 end if;
5091 -- Step 1: Initialize the object
5093 if Needs_Finalization (Typ) and then not No_Initialization (N) then
5094 Obj_Init :=
5095 Make_Init_Call
5096 (Obj_Ref => New_Occurrence_Of (Def_Id, Loc),
5097 Typ => Typ);
5098 end if;
5100 -- Step 2: Initialize the components of the object
5102 -- Do not initialize the components if their initialization is
5103 -- prohibited or the type represents a value type in a .NET VM.
5105 if Has_Non_Null_Base_Init_Proc (Typ)
5106 and then not No_Initialization (N)
5107 and then not Initialization_Suppressed (Typ)
5108 and then not Is_Value_Type (Typ)
5109 then
5110 -- Do not initialize the components if No_Default_Initialization
5111 -- applies as the the actual restriction check will occur later
5112 -- when the object is frozen as it is not known yet whether the
5113 -- object is imported or not.
5115 if not Restriction_Active (No_Default_Initialization) then
5117 -- If the values of the components are compile-time known, use
5118 -- their prebuilt aggregate form directly.
5120 Aggr_Init := Static_Initialization (Base_Init_Proc (Typ));
5122 if Present (Aggr_Init) then
5123 Set_Expression
5124 (N, New_Copy_Tree (Aggr_Init, New_Scope => Current_Scope));
5126 -- If type has discriminants, try to build an equivalent
5127 -- aggregate using discriminant values from the declaration.
5128 -- This is a useful optimization, in particular if restriction
5129 -- No_Elaboration_Code is active.
5131 elsif Build_Equivalent_Aggregate then
5132 null;
5134 -- Otherwise invoke the type init proc
5136 else
5137 Obj_Ref := New_Object_Reference;
5139 if Comes_From_Source (Def_Id) then
5140 Initialization_Warning (Obj_Ref);
5141 end if;
5143 Comp_Init := Build_Initialization_Call (Loc, Obj_Ref, Typ);
5144 end if;
5145 end if;
5147 -- Provide a default value if the object needs simple initialization
5148 -- and does not already have an initial value. A generated temporary
5149 -- do not require initialization because it will be assigned later.
5151 elsif Needs_Simple_Initialization
5152 (Typ, Initialize_Scalars
5153 and then not Has_Following_Address_Clause (N))
5154 and then not Is_Internal (Def_Id)
5155 and then not Has_Init_Expression (N)
5156 then
5157 Set_No_Initialization (N, False);
5158 Set_Expression (N, Get_Simple_Init_Val (Typ, N, Esize (Def_Id)));
5159 Analyze_And_Resolve (Expression (N), Typ);
5160 end if;
5162 -- Step 3: Add partial finalization and abort actions, generate:
5164 -- Type_Init_Proc (Obj);
5165 -- begin
5166 -- Deep_Initialize (Obj);
5167 -- exception
5168 -- when others =>
5169 -- Deep_Finalize (Obj, Self => False);
5170 -- raise;
5171 -- end;
5173 -- Step 3a: Build the finalization block (if applicable)
5175 -- The finalization block is required when both the object and its
5176 -- controlled components are to be initialized. The block finalizes
5177 -- the components if the object initialization fails.
5179 if Has_Controlled_Component (Typ)
5180 and then Present (Comp_Init)
5181 and then Present (Obj_Init)
5182 and then not Restriction_Active (No_Exception_Propagation)
5183 then
5184 -- Generate:
5185 -- Type_Init_Proc (Obj);
5187 Fin_Stmts := Comp_Init;
5189 -- Generate:
5190 -- begin
5191 -- Deep_Initialize (Obj);
5192 -- exception
5193 -- when others =>
5194 -- Deep_Finalize (Obj, Self => False);
5195 -- raise;
5196 -- end;
5198 Fin_Call :=
5199 Make_Final_Call
5200 (Obj_Ref => New_Object_Reference,
5201 Typ => Typ,
5202 Skip_Self => True);
5204 if Present (Fin_Call) then
5206 -- Do not emit warnings related to the elaboration order when a
5207 -- controlled object is declared before the body of Finalize is
5208 -- seen.
5210 Set_No_Elaboration_Check (Fin_Call);
5212 Append_To (Fin_Stmts,
5213 Make_Block_Statement (Loc,
5214 Declarations => No_List,
5216 Handled_Statement_Sequence =>
5217 Make_Handled_Sequence_Of_Statements (Loc,
5218 Statements => New_List (Obj_Init),
5220 Exception_Handlers => New_List (
5221 Make_Exception_Handler (Loc,
5222 Exception_Choices => New_List (
5223 Make_Others_Choice (Loc)),
5225 Statements => New_List (
5226 Fin_Call,
5227 Make_Raise_Statement (Loc)))))));
5228 end if;
5230 -- Finalization is not required, the initialization calls are passed
5231 -- to the abort block building circuitry, generate:
5233 -- Type_Init_Proc (Obj);
5234 -- Deep_Initialize (Obj);
5236 else
5237 if Present (Comp_Init) then
5238 Fin_Stmts := Comp_Init;
5239 end if;
5241 if Present (Obj_Init) then
5242 if No (Fin_Stmts) then
5243 Fin_Stmts := New_List;
5244 end if;
5246 Append_To (Fin_Stmts, Obj_Init);
5247 end if;
5248 end if;
5250 -- Step 3b: Build the abort block (if applicable)
5252 -- The abort block is required when aborts are allowed in order to
5253 -- protect both initialization calls.
5255 if Present (Comp_Init) and then Present (Obj_Init) then
5256 if Abort_Allowed then
5258 -- Generate:
5259 -- Abort_Defer;
5261 Prepend_To
5262 (Fin_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
5264 -- Generate:
5265 -- begin
5266 -- Abort_Defer;
5267 -- <finalization statements>
5268 -- at end
5269 -- Abort_Undefer_Direct;
5270 -- end;
5272 Abrt_HSS :=
5273 Make_Handled_Sequence_Of_Statements (Loc,
5274 Statements => Fin_Stmts,
5275 At_End_Proc =>
5276 New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc));
5278 Abrt_Blk :=
5279 Make_Block_Statement (Loc,
5280 Declarations => No_List,
5281 Handled_Statement_Sequence => Abrt_HSS);
5283 Add_Block_Identifier (Abrt_Blk, Abrt_Id);
5284 Expand_At_End_Handler (Abrt_HSS, Abrt_Id);
5286 Abrt_Stmts := New_List (Abrt_Blk);
5288 -- Abort is not required
5290 else
5291 -- Generate a dummy entity to ensure that the internal symbols
5292 -- are in sync when a unit is compiled with and without aborts.
5293 -- The entity is a block with proper scope and type.
5295 Dummy := New_Internal_Entity (E_Block, Current_Scope, Loc, 'B');
5296 Set_Etype (Dummy, Standard_Void_Type);
5297 Abrt_Stmts := Fin_Stmts;
5298 end if;
5300 -- No initialization calls present
5302 else
5303 Abrt_Stmts := Fin_Stmts;
5304 end if;
5306 -- Step 4: Insert the whole initialization sequence into the tree
5308 Insert_Actions_After (After, Abrt_Stmts);
5309 end Default_Initialize_Object;
5311 -------------------------
5312 -- Rewrite_As_Renaming --
5313 -------------------------
5315 function Rewrite_As_Renaming return Boolean is
5316 begin
5317 return not Aliased_Present (N)
5318 and then Is_Entity_Name (Expr_Q)
5319 and then Ekind (Entity (Expr_Q)) = E_Variable
5320 and then OK_To_Rename (Entity (Expr_Q))
5321 and then Is_Entity_Name (Obj_Def);
5322 end Rewrite_As_Renaming;
5324 -- Local variables
5326 Next_N : constant Node_Id := Next (N);
5327 Id_Ref : Node_Id;
5328 New_Ref : Node_Id;
5330 Init_After : Node_Id := N;
5331 -- Node after which the initialization actions are to be inserted. This
5332 -- is normally N, except for the case of a shared passive variable, in
5333 -- which case the init proc call must be inserted only after the bodies
5334 -- of the shared variable procedures have been seen.
5336 -- Start of processing for Expand_N_Object_Declaration
5338 begin
5339 -- Don't do anything for deferred constants. All proper actions will be
5340 -- expanded during the full declaration.
5342 if No (Expr) and Constant_Present (N) then
5343 return;
5344 end if;
5346 -- First we do special processing for objects of a tagged type where
5347 -- this is the point at which the type is frozen. The creation of the
5348 -- dispatch table and the initialization procedure have to be deferred
5349 -- to this point, since we reference previously declared primitive
5350 -- subprograms.
5352 -- Force construction of dispatch tables of library level tagged types
5354 if Tagged_Type_Expansion
5355 and then Static_Dispatch_Tables
5356 and then Is_Library_Level_Entity (Def_Id)
5357 and then Is_Library_Level_Tagged_Type (Base_Typ)
5358 and then (Ekind (Base_Typ) = E_Record_Type
5359 or else Ekind (Base_Typ) = E_Protected_Type
5360 or else Ekind (Base_Typ) = E_Task_Type)
5361 and then not Has_Dispatch_Table (Base_Typ)
5362 then
5363 declare
5364 New_Nodes : List_Id := No_List;
5366 begin
5367 if Is_Concurrent_Type (Base_Typ) then
5368 New_Nodes := Make_DT (Corresponding_Record_Type (Base_Typ), N);
5369 else
5370 New_Nodes := Make_DT (Base_Typ, N);
5371 end if;
5373 if not Is_Empty_List (New_Nodes) then
5374 Insert_List_Before (N, New_Nodes);
5375 end if;
5376 end;
5377 end if;
5379 -- Make shared memory routines for shared passive variable
5381 if Is_Shared_Passive (Def_Id) then
5382 Init_After := Make_Shared_Var_Procs (N);
5383 end if;
5385 -- If tasks being declared, make sure we have an activation chain
5386 -- defined for the tasks (has no effect if we already have one), and
5387 -- also that a Master variable is established and that the appropriate
5388 -- enclosing construct is established as a task master.
5390 if Has_Task (Typ) then
5391 Build_Activation_Chain_Entity (N);
5392 Build_Master_Entity (Def_Id);
5393 end if;
5395 -- Default initialization required, and no expression present
5397 if No (Expr) then
5399 -- If we have a type with a variant part, the initialization proc
5400 -- will contain implicit tests of the discriminant values, which
5401 -- counts as a violation of the restriction No_Implicit_Conditionals.
5403 if Has_Variant_Part (Typ) then
5404 declare
5405 Msg : Boolean;
5407 begin
5408 Check_Restriction (Msg, No_Implicit_Conditionals, Obj_Def);
5410 if Msg then
5411 Error_Msg_N
5412 ("\initialization of variant record tests discriminants",
5413 Obj_Def);
5414 return;
5415 end if;
5416 end;
5417 end if;
5419 -- For the default initialization case, if we have a private type
5420 -- with invariants, and invariant checks are enabled, then insert an
5421 -- invariant check after the object declaration. Note that it is OK
5422 -- to clobber the object with an invalid value since if the exception
5423 -- is raised, then the object will go out of scope. In the case where
5424 -- an array object is initialized with an aggregate, the expression
5425 -- is removed. Check flag Has_Init_Expression to avoid generating a
5426 -- junk invariant check and flag No_Initialization to avoid checking
5427 -- an uninitialized object such as a compiler temporary used for an
5428 -- aggregate.
5430 if Has_Invariants (Base_Typ)
5431 and then Present (Invariant_Procedure (Base_Typ))
5432 and then not Has_Init_Expression (N)
5433 and then not No_Initialization (N)
5434 then
5435 Insert_After (N,
5436 Make_Invariant_Call (New_Occurrence_Of (Def_Id, Loc)));
5437 end if;
5439 Default_Initialize_Object (Init_After);
5441 -- Generate attribute for Persistent_BSS if needed
5443 if Persistent_BSS_Mode
5444 and then Comes_From_Source (N)
5445 and then Is_Potentially_Persistent_Type (Typ)
5446 and then not Has_Init_Expression (N)
5447 and then Is_Library_Level_Entity (Def_Id)
5448 then
5449 declare
5450 Prag : Node_Id;
5451 begin
5452 Prag :=
5453 Make_Linker_Section_Pragma
5454 (Def_Id, Sloc (N), ".persistent.bss");
5455 Insert_After (N, Prag);
5456 Analyze (Prag);
5457 end;
5458 end if;
5460 -- If access type, then we know it is null if not initialized
5462 if Is_Access_Type (Typ) then
5463 Set_Is_Known_Null (Def_Id);
5464 end if;
5466 -- Explicit initialization present
5468 else
5469 -- Obtain actual expression from qualified expression
5471 if Nkind (Expr) = N_Qualified_Expression then
5472 Expr_Q := Expression (Expr);
5473 else
5474 Expr_Q := Expr;
5475 end if;
5477 -- When we have the appropriate type of aggregate in the expression
5478 -- (it has been determined during analysis of the aggregate by
5479 -- setting the delay flag), let's perform in place assignment and
5480 -- thus avoid creating a temporary.
5482 if Is_Delayed_Aggregate (Expr_Q) then
5483 Convert_Aggr_In_Object_Decl (N);
5485 -- Ada 2005 (AI-318-02): If the initialization expression is a call
5486 -- to a build-in-place function, then access to the declared object
5487 -- must be passed to the function. Currently we limit such functions
5488 -- to those with constrained limited result subtypes, but eventually
5489 -- plan to expand the allowed forms of functions that are treated as
5490 -- build-in-place.
5492 elsif Ada_Version >= Ada_2005
5493 and then Is_Build_In_Place_Function_Call (Expr_Q)
5494 then
5495 Make_Build_In_Place_Call_In_Object_Declaration (N, Expr_Q);
5497 -- The previous call expands the expression initializing the
5498 -- built-in-place object into further code that will be analyzed
5499 -- later. No further expansion needed here.
5501 return;
5503 -- Ada 2005 (AI-251): Rewrite the expression that initializes a
5504 -- class-wide interface object to ensure that we copy the full
5505 -- object, unless we are targetting a VM where interfaces are handled
5506 -- by VM itself. Note that if the root type of Typ is an ancestor of
5507 -- Expr's type, both types share the same dispatch table and there is
5508 -- no need to displace the pointer.
5510 elsif Is_Interface (Typ)
5512 -- Avoid never-ending recursion because if Equivalent_Type is set
5513 -- then we've done it already and must not do it again.
5515 and then not
5516 (Nkind (Obj_Def) = N_Identifier
5517 and then Present (Equivalent_Type (Entity (Obj_Def))))
5518 then
5519 pragma Assert (Is_Class_Wide_Type (Typ));
5521 -- If the object is a return object of an inherently limited type,
5522 -- which implies build-in-place treatment, bypass the special
5523 -- treatment of class-wide interface initialization below. In this
5524 -- case, the expansion of the return statement will take care of
5525 -- creating the object (via allocator) and initializing it.
5527 if Is_Return_Object (Def_Id) and then Is_Limited_View (Typ) then
5528 null;
5530 elsif Tagged_Type_Expansion then
5531 declare
5532 Iface : constant Entity_Id := Root_Type (Typ);
5533 Expr_N : Node_Id := Expr;
5534 Expr_Typ : Entity_Id;
5535 New_Expr : Node_Id;
5536 Obj_Id : Entity_Id;
5537 Tag_Comp : Node_Id;
5539 begin
5540 -- If the original node of the expression was a conversion
5541 -- to this specific class-wide interface type then restore
5542 -- the original node because we must copy the object before
5543 -- displacing the pointer to reference the secondary tag
5544 -- component. This code must be kept synchronized with the
5545 -- expansion done by routine Expand_Interface_Conversion
5547 if not Comes_From_Source (Expr_N)
5548 and then Nkind (Expr_N) = N_Explicit_Dereference
5549 and then Nkind (Original_Node (Expr_N)) = N_Type_Conversion
5550 and then Etype (Original_Node (Expr_N)) = Typ
5551 then
5552 Rewrite (Expr_N, Original_Node (Expression (N)));
5553 end if;
5555 -- Avoid expansion of redundant interface conversion
5557 if Is_Interface (Etype (Expr_N))
5558 and then Nkind (Expr_N) = N_Type_Conversion
5559 and then Etype (Expr_N) = Typ
5560 then
5561 Expr_N := Expression (Expr_N);
5562 Set_Expression (N, Expr_N);
5563 end if;
5565 Obj_Id := Make_Temporary (Loc, 'D', Expr_N);
5566 Expr_Typ := Base_Type (Etype (Expr_N));
5568 if Is_Class_Wide_Type (Expr_Typ) then
5569 Expr_Typ := Root_Type (Expr_Typ);
5570 end if;
5572 -- Replace
5573 -- CW : I'Class := Obj;
5574 -- by
5575 -- Tmp : T := Obj;
5576 -- type Ityp is not null access I'Class;
5577 -- CW : I'Class renames Ityp (Tmp.I_Tag'Address).all;
5579 if Comes_From_Source (Expr_N)
5580 and then Nkind (Expr_N) = N_Identifier
5581 and then not Is_Interface (Expr_Typ)
5582 and then Interface_Present_In_Ancestor (Expr_Typ, Typ)
5583 and then (Expr_Typ = Etype (Expr_Typ)
5584 or else not
5585 Is_Variable_Size_Record (Etype (Expr_Typ)))
5586 then
5587 -- Copy the object
5589 Insert_Action (N,
5590 Make_Object_Declaration (Loc,
5591 Defining_Identifier => Obj_Id,
5592 Object_Definition =>
5593 New_Occurrence_Of (Expr_Typ, Loc),
5594 Expression => Relocate_Node (Expr_N)));
5596 -- Statically reference the tag associated with the
5597 -- interface
5599 Tag_Comp :=
5600 Make_Selected_Component (Loc,
5601 Prefix => New_Occurrence_Of (Obj_Id, Loc),
5602 Selector_Name =>
5603 New_Occurrence_Of
5604 (Find_Interface_Tag (Expr_Typ, Iface), Loc));
5606 -- Replace
5607 -- IW : I'Class := Obj;
5608 -- by
5609 -- type Equiv_Record is record ... end record;
5610 -- implicit subtype CW is <Class_Wide_Subtype>;
5611 -- Tmp : CW := CW!(Obj);
5612 -- type Ityp is not null access I'Class;
5613 -- IW : I'Class renames
5614 -- Ityp!(Displace (Temp'Address, I'Tag)).all;
5616 else
5617 -- Generate the equivalent record type and update the
5618 -- subtype indication to reference it.
5620 Expand_Subtype_From_Expr
5621 (N => N,
5622 Unc_Type => Typ,
5623 Subtype_Indic => Obj_Def,
5624 Exp => Expr_N);
5626 if not Is_Interface (Etype (Expr_N)) then
5627 New_Expr := Relocate_Node (Expr_N);
5629 -- For interface types we use 'Address which displaces
5630 -- the pointer to the base of the object (if required)
5632 else
5633 New_Expr :=
5634 Unchecked_Convert_To (Etype (Obj_Def),
5635 Make_Explicit_Dereference (Loc,
5636 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
5637 Make_Attribute_Reference (Loc,
5638 Prefix => Relocate_Node (Expr_N),
5639 Attribute_Name => Name_Address))));
5640 end if;
5642 -- Copy the object
5644 if not Is_Limited_Record (Expr_Typ) then
5645 Insert_Action (N,
5646 Make_Object_Declaration (Loc,
5647 Defining_Identifier => Obj_Id,
5648 Object_Definition =>
5649 New_Occurrence_Of (Etype (Obj_Def), Loc),
5650 Expression => New_Expr));
5652 -- Rename limited type object since they cannot be copied
5653 -- This case occurs when the initialization expression
5654 -- has been previously expanded into a temporary object.
5656 else pragma Assert (not Comes_From_Source (Expr_Q));
5657 Insert_Action (N,
5658 Make_Object_Renaming_Declaration (Loc,
5659 Defining_Identifier => Obj_Id,
5660 Subtype_Mark =>
5661 New_Occurrence_Of (Etype (Obj_Def), Loc),
5662 Name =>
5663 Unchecked_Convert_To
5664 (Etype (Obj_Def), New_Expr)));
5665 end if;
5667 -- Dynamically reference the tag associated with the
5668 -- interface.
5670 Tag_Comp :=
5671 Make_Function_Call (Loc,
5672 Name => New_Occurrence_Of (RTE (RE_Displace), Loc),
5673 Parameter_Associations => New_List (
5674 Make_Attribute_Reference (Loc,
5675 Prefix => New_Occurrence_Of (Obj_Id, Loc),
5676 Attribute_Name => Name_Address),
5677 New_Occurrence_Of
5678 (Node (First_Elmt (Access_Disp_Table (Iface))),
5679 Loc)));
5680 end if;
5682 Rewrite (N,
5683 Make_Object_Renaming_Declaration (Loc,
5684 Defining_Identifier => Make_Temporary (Loc, 'D'),
5685 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
5686 Name =>
5687 Convert_Tag_To_Interface (Typ, Tag_Comp)));
5689 -- If the original entity comes from source, then mark the
5690 -- new entity as needing debug information, even though it's
5691 -- defined by a generated renaming that does not come from
5692 -- source, so that Materialize_Entity will be set on the
5693 -- entity when Debug_Renaming_Declaration is called during
5694 -- analysis.
5696 if Comes_From_Source (Def_Id) then
5697 Set_Debug_Info_Needed (Defining_Identifier (N));
5698 end if;
5700 Analyze (N, Suppress => All_Checks);
5702 -- Replace internal identifier of rewritten node by the
5703 -- identifier found in the sources. We also have to exchange
5704 -- entities containing their defining identifiers to ensure
5705 -- the correct replacement of the object declaration by this
5706 -- object renaming declaration because these identifiers
5707 -- were previously added by Enter_Name to the current scope.
5708 -- We must preserve the homonym chain of the source entity
5709 -- as well. We must also preserve the kind of the entity,
5710 -- which may be a constant. Preserve entity chain because
5711 -- itypes may have been generated already, and the full
5712 -- chain must be preserved for final freezing. Finally,
5713 -- preserve Comes_From_Source setting, so that debugging
5714 -- and cross-referencing information is properly kept, and
5715 -- preserve source location, to prevent spurious errors when
5716 -- entities are declared (they must have their own Sloc).
5718 declare
5719 New_Id : constant Entity_Id := Defining_Identifier (N);
5720 Next_Temp : constant Entity_Id := Next_Entity (New_Id);
5721 S_Flag : constant Boolean :=
5722 Comes_From_Source (Def_Id);
5724 begin
5725 Set_Next_Entity (New_Id, Next_Entity (Def_Id));
5726 Set_Next_Entity (Def_Id, Next_Temp);
5728 Set_Chars (Defining_Identifier (N), Chars (Def_Id));
5729 Set_Homonym (Defining_Identifier (N), Homonym (Def_Id));
5730 Set_Ekind (Defining_Identifier (N), Ekind (Def_Id));
5731 Set_Sloc (Defining_Identifier (N), Sloc (Def_Id));
5733 Set_Comes_From_Source (Def_Id, False);
5734 Exchange_Entities (Defining_Identifier (N), Def_Id);
5735 Set_Comes_From_Source (Def_Id, S_Flag);
5736 end;
5737 end;
5738 end if;
5740 return;
5742 -- Common case of explicit object initialization
5744 else
5745 -- In most cases, we must check that the initial value meets any
5746 -- constraint imposed by the declared type. However, there is one
5747 -- very important exception to this rule. If the entity has an
5748 -- unconstrained nominal subtype, then it acquired its constraints
5749 -- from the expression in the first place, and not only does this
5750 -- mean that the constraint check is not needed, but an attempt to
5751 -- perform the constraint check can cause order of elaboration
5752 -- problems.
5754 if not Is_Constr_Subt_For_U_Nominal (Typ) then
5756 -- If this is an allocator for an aggregate that has been
5757 -- allocated in place, delay checks until assignments are
5758 -- made, because the discriminants are not initialized.
5760 if Nkind (Expr) = N_Allocator and then No_Initialization (Expr)
5761 then
5762 null;
5764 -- Otherwise apply a constraint check now if no prev error
5766 elsif Nkind (Expr) /= N_Error then
5767 Apply_Constraint_Check (Expr, Typ);
5769 -- Deal with possible range check
5771 if Do_Range_Check (Expr) then
5773 -- If assignment checks are suppressed, turn off flag
5775 if Suppress_Assignment_Checks (N) then
5776 Set_Do_Range_Check (Expr, False);
5778 -- Otherwise generate the range check
5780 else
5781 Generate_Range_Check
5782 (Expr, Typ, CE_Range_Check_Failed);
5783 end if;
5784 end if;
5785 end if;
5786 end if;
5788 -- If the type is controlled and not inherently limited, then
5789 -- the target is adjusted after the copy and attached to the
5790 -- finalization list. However, no adjustment is done in the case
5791 -- where the object was initialized by a call to a function whose
5792 -- result is built in place, since no copy occurred. (Eventually
5793 -- we plan to support in-place function results for some cases
5794 -- of nonlimited types. ???) Similarly, no adjustment is required
5795 -- if we are going to rewrite the object declaration into a
5796 -- renaming declaration.
5798 if Needs_Finalization (Typ)
5799 and then not Is_Limited_View (Typ)
5800 and then not Rewrite_As_Renaming
5801 then
5802 Insert_Action_After (Init_After,
5803 Make_Adjust_Call (
5804 Obj_Ref => New_Occurrence_Of (Def_Id, Loc),
5805 Typ => Base_Typ));
5806 end if;
5808 -- For tagged types, when an init value is given, the tag has to
5809 -- be re-initialized separately in order to avoid the propagation
5810 -- of a wrong tag coming from a view conversion unless the type
5811 -- is class wide (in this case the tag comes from the init value).
5812 -- Suppress the tag assignment when VM_Target because VM tags are
5813 -- represented implicitly in objects. Ditto for types that are
5814 -- CPP_CLASS, and for initializations that are aggregates, because
5815 -- they have to have the right tag.
5817 if Is_Tagged_Type (Typ)
5818 and then not Is_Class_Wide_Type (Typ)
5819 and then not Is_CPP_Class (Typ)
5820 and then Tagged_Type_Expansion
5821 and then Nkind (Expr) /= N_Aggregate
5822 and then (Nkind (Expr) /= N_Qualified_Expression
5823 or else Nkind (Expression (Expr)) /= N_Aggregate)
5824 then
5825 declare
5826 Full_Typ : constant Entity_Id := Underlying_Type (Typ);
5828 begin
5829 -- The re-assignment of the tag has to be done even if the
5830 -- object is a constant. The assignment must be analyzed
5831 -- after the declaration.
5833 New_Ref :=
5834 Make_Selected_Component (Loc,
5835 Prefix => New_Occurrence_Of (Def_Id, Loc),
5836 Selector_Name =>
5837 New_Occurrence_Of (First_Tag_Component (Full_Typ),
5838 Loc));
5839 Set_Assignment_OK (New_Ref);
5841 Insert_Action_After (Init_After,
5842 Make_Assignment_Statement (Loc,
5843 Name => New_Ref,
5844 Expression =>
5845 Unchecked_Convert_To (RTE (RE_Tag),
5846 New_Occurrence_Of
5847 (Node (First_Elmt (Access_Disp_Table (Full_Typ))),
5848 Loc))));
5849 end;
5851 -- Handle C++ constructor calls. Note that we do not check that
5852 -- Typ is a tagged type since the equivalent Ada type of a C++
5853 -- class that has no virtual methods is an untagged limited
5854 -- record type.
5856 elsif Is_CPP_Constructor_Call (Expr) then
5858 -- The call to the initialization procedure does NOT freeze the
5859 -- object being initialized.
5861 Id_Ref := New_Occurrence_Of (Def_Id, Loc);
5862 Set_Must_Not_Freeze (Id_Ref);
5863 Set_Assignment_OK (Id_Ref);
5865 Insert_Actions_After (Init_After,
5866 Build_Initialization_Call (Loc, Id_Ref, Typ,
5867 Constructor_Ref => Expr));
5869 -- We remove here the original call to the constructor
5870 -- to avoid its management in the backend
5872 Set_Expression (N, Empty);
5873 return;
5875 -- For discrete types, set the Is_Known_Valid flag if the
5876 -- initializing value is known to be valid. Only do this for
5877 -- source assignments, since otherwise we can end up turning
5878 -- on the known valid flag prematurely from inserted code.
5880 elsif Comes_From_Source (N)
5881 and then Is_Discrete_Type (Typ)
5882 and then Expr_Known_Valid (Expr)
5883 then
5884 Set_Is_Known_Valid (Def_Id);
5886 elsif Is_Access_Type (Typ) then
5888 -- For access types set the Is_Known_Non_Null flag if the
5889 -- initializing value is known to be non-null. We can also set
5890 -- Can_Never_Be_Null if this is a constant.
5892 if Known_Non_Null (Expr) then
5893 Set_Is_Known_Non_Null (Def_Id, True);
5895 if Constant_Present (N) then
5896 Set_Can_Never_Be_Null (Def_Id);
5897 end if;
5898 end if;
5899 end if;
5901 -- If validity checking on copies, validate initial expression.
5902 -- But skip this if declaration is for a generic type, since it
5903 -- makes no sense to validate generic types. Not clear if this
5904 -- can happen for legal programs, but it definitely can arise
5905 -- from previous instantiation errors.
5907 if Validity_Checks_On
5908 and then Validity_Check_Copies
5909 and then not Is_Generic_Type (Etype (Def_Id))
5910 then
5911 Ensure_Valid (Expr);
5912 Set_Is_Known_Valid (Def_Id);
5913 end if;
5914 end if;
5916 -- Cases where the back end cannot handle the initialization directly
5917 -- In such cases, we expand an assignment that will be appropriately
5918 -- handled by Expand_N_Assignment_Statement.
5920 -- The exclusion of the unconstrained case is wrong, but for now it
5921 -- is too much trouble ???
5923 if (Is_Possibly_Unaligned_Slice (Expr)
5924 or else (Is_Possibly_Unaligned_Object (Expr)
5925 and then not Represented_As_Scalar (Etype (Expr))))
5926 and then not (Is_Array_Type (Etype (Expr))
5927 and then not Is_Constrained (Etype (Expr)))
5928 then
5929 declare
5930 Stat : constant Node_Id :=
5931 Make_Assignment_Statement (Loc,
5932 Name => New_Occurrence_Of (Def_Id, Loc),
5933 Expression => Relocate_Node (Expr));
5934 begin
5935 Set_Expression (N, Empty);
5936 Set_No_Initialization (N);
5937 Set_Assignment_OK (Name (Stat));
5938 Set_No_Ctrl_Actions (Stat);
5939 Insert_After_And_Analyze (Init_After, Stat);
5940 end;
5941 end if;
5943 -- Final transformation, if the initializing expression is an entity
5944 -- for a variable with OK_To_Rename set, then we transform:
5946 -- X : typ := expr;
5948 -- into
5950 -- X : typ renames expr
5952 -- provided that X is not aliased. The aliased case has to be
5953 -- excluded in general because Expr will not be aliased in general.
5955 if Rewrite_As_Renaming then
5956 Rewrite (N,
5957 Make_Object_Renaming_Declaration (Loc,
5958 Defining_Identifier => Defining_Identifier (N),
5959 Subtype_Mark => Obj_Def,
5960 Name => Expr_Q));
5962 -- We do not analyze this renaming declaration, because all its
5963 -- components have already been analyzed, and if we were to go
5964 -- ahead and analyze it, we would in effect be trying to generate
5965 -- another declaration of X, which won't do.
5967 Set_Renamed_Object (Defining_Identifier (N), Expr_Q);
5968 Set_Analyzed (N);
5970 -- We do need to deal with debug issues for this renaming
5972 -- First, if entity comes from source, then mark it as needing
5973 -- debug information, even though it is defined by a generated
5974 -- renaming that does not come from source.
5976 if Comes_From_Source (Defining_Identifier (N)) then
5977 Set_Debug_Info_Needed (Defining_Identifier (N));
5978 end if;
5980 -- Now call the routine to generate debug info for the renaming
5982 declare
5983 Decl : constant Node_Id := Debug_Renaming_Declaration (N);
5984 begin
5985 if Present (Decl) then
5986 Insert_Action (N, Decl);
5987 end if;
5988 end;
5989 end if;
5990 end if;
5992 if Nkind (N) = N_Object_Declaration
5993 and then Nkind (Obj_Def) = N_Access_Definition
5994 and then not Is_Local_Anonymous_Access (Etype (Def_Id))
5995 then
5996 -- An Ada 2012 stand-alone object of an anonymous access type
5998 declare
5999 Loc : constant Source_Ptr := Sloc (N);
6001 Level : constant Entity_Id :=
6002 Make_Defining_Identifier (Sloc (N),
6003 Chars =>
6004 New_External_Name (Chars (Def_Id), Suffix => "L"));
6006 Level_Expr : Node_Id;
6007 Level_Decl : Node_Id;
6009 begin
6010 Set_Ekind (Level, Ekind (Def_Id));
6011 Set_Etype (Level, Standard_Natural);
6012 Set_Scope (Level, Scope (Def_Id));
6014 if No (Expr) then
6016 -- Set accessibility level of null
6018 Level_Expr :=
6019 Make_Integer_Literal (Loc, Scope_Depth (Standard_Standard));
6021 else
6022 Level_Expr := Dynamic_Accessibility_Level (Expr);
6023 end if;
6025 Level_Decl :=
6026 Make_Object_Declaration (Loc,
6027 Defining_Identifier => Level,
6028 Object_Definition =>
6029 New_Occurrence_Of (Standard_Natural, Loc),
6030 Expression => Level_Expr,
6031 Constant_Present => Constant_Present (N),
6032 Has_Init_Expression => True);
6034 Insert_Action_After (Init_After, Level_Decl);
6036 Set_Extra_Accessibility (Def_Id, Level);
6037 end;
6038 end if;
6040 -- At this point the object is fully initialized by either invoking the
6041 -- related type init proc, routine [Deep_]Initialize or performing in-
6042 -- place assingments for an array object. If the related type is subject
6043 -- to pragma Default_Initial_Condition, add a runtime check to verify
6044 -- the assumption of the pragma. Generate:
6046 -- <Base_Typ>Default_Init_Cond (<Base_Typ> (Def_Id));
6048 -- Note that the check is generated for source objects only
6050 if Comes_From_Source (Def_Id)
6051 and then (Has_Default_Init_Cond (Base_Typ)
6052 or else
6053 Has_Inherited_Default_Init_Cond (Base_Typ))
6054 then
6055 declare
6056 DIC_Call : constant Node_Id :=
6057 Build_Default_Init_Cond_Call (Loc, Def_Id, Base_Typ);
6058 begin
6059 if Present (Next_N) then
6060 Insert_Before_And_Analyze (Next_N, DIC_Call);
6062 -- The object declaration is the last node in a declarative or a
6063 -- statement list.
6065 else
6066 Append_To (List_Containing (N), DIC_Call);
6067 Analyze (DIC_Call);
6068 end if;
6069 end;
6070 end if;
6072 -- Exception on library entity not available
6074 exception
6075 when RE_Not_Available =>
6076 return;
6077 end Expand_N_Object_Declaration;
6079 ---------------------------------
6080 -- Expand_N_Subtype_Indication --
6081 ---------------------------------
6083 -- Add a check on the range of the subtype. The static case is partially
6084 -- duplicated by Process_Range_Expr_In_Decl in Sem_Ch3, but we still need
6085 -- to check here for the static case in order to avoid generating
6086 -- extraneous expanded code. Also deal with validity checking.
6088 procedure Expand_N_Subtype_Indication (N : Node_Id) is
6089 Ran : constant Node_Id := Range_Expression (Constraint (N));
6090 Typ : constant Entity_Id := Entity (Subtype_Mark (N));
6092 begin
6093 if Nkind (Constraint (N)) = N_Range_Constraint then
6094 Validity_Check_Range (Range_Expression (Constraint (N)));
6095 end if;
6097 if Nkind_In (Parent (N), N_Constrained_Array_Definition, N_Slice) then
6098 Apply_Range_Check (Ran, Typ);
6099 end if;
6100 end Expand_N_Subtype_Indication;
6102 ---------------------------
6103 -- Expand_N_Variant_Part --
6104 ---------------------------
6106 -- Note: this procedure no longer has any effect. It used to be that we
6107 -- would replace the choices in the last variant by a when others, and
6108 -- also expanded static predicates in variant choices here, but both of
6109 -- those activities were being done too early, since we can't check the
6110 -- choices until the statically predicated subtypes are frozen, which can
6111 -- happen as late as the free point of the record, and we can't change the
6112 -- last choice to an others before checking the choices, which is now done
6113 -- at the freeze point of the record.
6115 procedure Expand_N_Variant_Part (N : Node_Id) is
6116 begin
6117 null;
6118 end Expand_N_Variant_Part;
6120 ---------------------------------
6121 -- Expand_Previous_Access_Type --
6122 ---------------------------------
6124 procedure Expand_Previous_Access_Type (Def_Id : Entity_Id) is
6125 Ptr_Typ : Entity_Id;
6127 begin
6128 -- Find all access types in the current scope whose designated type is
6129 -- Def_Id and build master renamings for them.
6131 Ptr_Typ := First_Entity (Current_Scope);
6132 while Present (Ptr_Typ) loop
6133 if Is_Access_Type (Ptr_Typ)
6134 and then Designated_Type (Ptr_Typ) = Def_Id
6135 and then No (Master_Id (Ptr_Typ))
6136 then
6137 -- Ensure that the designated type has a master
6139 Build_Master_Entity (Def_Id);
6141 -- Private and incomplete types complicate the insertion of master
6142 -- renamings because the access type may precede the full view of
6143 -- the designated type. For this reason, the master renamings are
6144 -- inserted relative to the designated type.
6146 Build_Master_Renaming (Ptr_Typ, Ins_Nod => Parent (Def_Id));
6147 end if;
6149 Next_Entity (Ptr_Typ);
6150 end loop;
6151 end Expand_Previous_Access_Type;
6153 ------------------------
6154 -- Expand_Tagged_Root --
6155 ------------------------
6157 procedure Expand_Tagged_Root (T : Entity_Id) is
6158 Def : constant Node_Id := Type_Definition (Parent (T));
6159 Comp_List : Node_Id;
6160 Comp_Decl : Node_Id;
6161 Sloc_N : Source_Ptr;
6163 begin
6164 if Null_Present (Def) then
6165 Set_Component_List (Def,
6166 Make_Component_List (Sloc (Def),
6167 Component_Items => Empty_List,
6168 Variant_Part => Empty,
6169 Null_Present => True));
6170 end if;
6172 Comp_List := Component_List (Def);
6174 if Null_Present (Comp_List)
6175 or else Is_Empty_List (Component_Items (Comp_List))
6176 then
6177 Sloc_N := Sloc (Comp_List);
6178 else
6179 Sloc_N := Sloc (First (Component_Items (Comp_List)));
6180 end if;
6182 Comp_Decl :=
6183 Make_Component_Declaration (Sloc_N,
6184 Defining_Identifier => First_Tag_Component (T),
6185 Component_Definition =>
6186 Make_Component_Definition (Sloc_N,
6187 Aliased_Present => False,
6188 Subtype_Indication => New_Occurrence_Of (RTE (RE_Tag), Sloc_N)));
6190 if Null_Present (Comp_List)
6191 or else Is_Empty_List (Component_Items (Comp_List))
6192 then
6193 Set_Component_Items (Comp_List, New_List (Comp_Decl));
6194 Set_Null_Present (Comp_List, False);
6196 else
6197 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
6198 end if;
6200 -- We don't Analyze the whole expansion because the tag component has
6201 -- already been analyzed previously. Here we just insure that the tree
6202 -- is coherent with the semantic decoration
6204 Find_Type (Subtype_Indication (Component_Definition (Comp_Decl)));
6206 exception
6207 when RE_Not_Available =>
6208 return;
6209 end Expand_Tagged_Root;
6211 ----------------------
6212 -- Clean_Task_Names --
6213 ----------------------
6215 procedure Clean_Task_Names
6216 (Typ : Entity_Id;
6217 Proc_Id : Entity_Id)
6219 begin
6220 if Has_Task (Typ)
6221 and then not Restriction_Active (No_Implicit_Heap_Allocations)
6222 and then not Global_Discard_Names
6223 and then Tagged_Type_Expansion
6224 then
6225 Set_Uses_Sec_Stack (Proc_Id);
6226 end if;
6227 end Clean_Task_Names;
6229 ------------------------------
6230 -- Expand_Freeze_Array_Type --
6231 ------------------------------
6233 procedure Expand_Freeze_Array_Type (N : Node_Id) is
6234 Typ : constant Entity_Id := Entity (N);
6235 Comp_Typ : constant Entity_Id := Component_Type (Typ);
6236 Base : constant Entity_Id := Base_Type (Typ);
6238 begin
6239 if not Is_Bit_Packed_Array (Typ) then
6241 -- If the component contains tasks, so does the array type. This may
6242 -- not be indicated in the array type because the component may have
6243 -- been a private type at the point of definition. Same if component
6244 -- type is controlled or contains protected objects.
6246 Set_Has_Task (Base, Has_Task (Comp_Typ));
6247 Set_Has_Protected (Base, Has_Protected (Comp_Typ));
6248 Set_Has_Controlled_Component
6249 (Base, Has_Controlled_Component
6250 (Comp_Typ)
6251 or else
6252 Is_Controlled (Comp_Typ));
6254 if No (Init_Proc (Base)) then
6256 -- If this is an anonymous array created for a declaration with
6257 -- an initial value, its init_proc will never be called. The
6258 -- initial value itself may have been expanded into assignments,
6259 -- in which case the object declaration is carries the
6260 -- No_Initialization flag.
6262 if Is_Itype (Base)
6263 and then Nkind (Associated_Node_For_Itype (Base)) =
6264 N_Object_Declaration
6265 and then
6266 (Present (Expression (Associated_Node_For_Itype (Base)))
6267 or else No_Initialization (Associated_Node_For_Itype (Base)))
6268 then
6269 null;
6271 -- We do not need an init proc for string or wide [wide] string,
6272 -- since the only time these need initialization in normalize or
6273 -- initialize scalars mode, and these types are treated specially
6274 -- and do not need initialization procedures.
6276 elsif Is_Standard_String_Type (Base) then
6277 null;
6279 -- Otherwise we have to build an init proc for the subtype
6281 else
6282 Build_Array_Init_Proc (Base, N);
6283 end if;
6284 end if;
6286 if Typ = Base then
6287 if Has_Controlled_Component (Base) then
6288 Build_Controlling_Procs (Base);
6290 if not Is_Limited_Type (Comp_Typ)
6291 and then Number_Dimensions (Typ) = 1
6292 then
6293 Build_Slice_Assignment (Typ);
6294 end if;
6295 end if;
6297 -- Create a finalization master to service the anonymous access
6298 -- components of the array.
6300 if Ekind (Comp_Typ) = E_Anonymous_Access_Type
6301 and then Needs_Finalization (Designated_Type (Comp_Typ))
6302 then
6303 Build_Finalization_Master
6304 (Typ => Comp_Typ,
6305 Ins_Node => Parent (Typ),
6306 Encl_Scope => Scope (Typ));
6307 end if;
6308 end if;
6310 -- For packed case, default initialization, except if the component type
6311 -- is itself a packed structure with an initialization procedure, or
6312 -- initialize/normalize scalars active, and we have a base type, or the
6313 -- type is public, because in that case a client might specify
6314 -- Normalize_Scalars and there better be a public Init_Proc for it.
6316 elsif (Present (Init_Proc (Component_Type (Base)))
6317 and then No (Base_Init_Proc (Base)))
6318 or else (Init_Or_Norm_Scalars and then Base = Typ)
6319 or else Is_Public (Typ)
6320 then
6321 Build_Array_Init_Proc (Base, N);
6322 end if;
6324 if Has_Invariants (Component_Type (Base))
6325 and then Typ = Base
6326 and then In_Open_Scopes (Scope (Component_Type (Base)))
6327 then
6328 -- Generate component invariant checking procedure. This is only
6329 -- relevant if the array type is within the scope of the component
6330 -- type. Otherwise an array object can only be built using the public
6331 -- subprograms for the component type, and calls to those will have
6332 -- invariant checks. The invariant procedure is only generated for
6333 -- a base type, not a subtype.
6335 Insert_Component_Invariant_Checks
6336 (N, Base, Build_Array_Invariant_Proc (Base, N));
6337 end if;
6338 end Expand_Freeze_Array_Type;
6340 -----------------------------------
6341 -- Expand_Freeze_Class_Wide_Type --
6342 -----------------------------------
6344 procedure Expand_Freeze_Class_Wide_Type (N : Node_Id) is
6345 Typ : constant Entity_Id := Entity (N);
6346 Root : constant Entity_Id := Root_Type (Typ);
6348 function Is_C_Derivation (Typ : Entity_Id) return Boolean;
6349 -- Given a type, determine whether it is derived from a C or C++ root
6351 ---------------------
6352 -- Is_C_Derivation --
6353 ---------------------
6355 function Is_C_Derivation (Typ : Entity_Id) return Boolean is
6356 T : Entity_Id := Typ;
6358 begin
6359 loop
6360 if Is_CPP_Class (T)
6361 or else Convention (T) = Convention_C
6362 or else Convention (T) = Convention_CPP
6363 then
6364 return True;
6365 end if;
6367 exit when T = Etype (T);
6369 T := Etype (T);
6370 end loop;
6372 return False;
6373 end Is_C_Derivation;
6375 -- Start of processing for Expand_Freeze_Class_Wide_Type
6377 begin
6378 -- Certain run-time configurations and targets do not provide support
6379 -- for controlled types.
6381 if Restriction_Active (No_Finalization) then
6382 return;
6384 -- Do not create TSS routine Finalize_Address when dispatching calls are
6385 -- disabled since the core of the routine is a dispatching call.
6387 elsif Restriction_Active (No_Dispatching_Calls) then
6388 return;
6390 -- Do not create TSS routine Finalize_Address for concurrent class-wide
6391 -- types. Ignore C, C++, CIL and Java types since it is assumed that the
6392 -- non-Ada side will handle their destruction.
6394 elsif Is_Concurrent_Type (Root)
6395 or else Is_C_Derivation (Root)
6396 or else Convention (Typ) = Convention_CIL
6397 or else Convention (Typ) = Convention_CPP
6398 or else Convention (Typ) = Convention_Java
6399 then
6400 return;
6402 -- Do not create TSS routine Finalize_Address for .NET/JVM because these
6403 -- targets do not support address arithmetic and unchecked conversions.
6405 elsif VM_Target /= No_VM then
6406 return;
6408 -- Do not create TSS routine Finalize_Address when compiling in CodePeer
6409 -- mode since the routine contains an Unchecked_Conversion.
6411 elsif CodePeer_Mode then
6412 return;
6413 end if;
6415 -- Create the body of TSS primitive Finalize_Address. This automatically
6416 -- sets the TSS entry for the class-wide type.
6418 Make_Finalize_Address_Body (Typ);
6419 end Expand_Freeze_Class_Wide_Type;
6421 ------------------------------------
6422 -- Expand_Freeze_Enumeration_Type --
6423 ------------------------------------
6425 procedure Expand_Freeze_Enumeration_Type (N : Node_Id) is
6426 Typ : constant Entity_Id := Entity (N);
6427 Loc : constant Source_Ptr := Sloc (Typ);
6428 Ent : Entity_Id;
6429 Lst : List_Id;
6430 Num : Nat;
6431 Arr : Entity_Id;
6432 Fent : Entity_Id;
6433 Ityp : Entity_Id;
6434 Is_Contiguous : Boolean;
6435 Pos_Expr : Node_Id;
6436 Last_Repval : Uint;
6438 Func : Entity_Id;
6439 pragma Warnings (Off, Func);
6441 begin
6442 -- Various optimizations possible if given representation is contiguous
6444 Is_Contiguous := True;
6446 Ent := First_Literal (Typ);
6447 Last_Repval := Enumeration_Rep (Ent);
6449 Next_Literal (Ent);
6450 while Present (Ent) loop
6451 if Enumeration_Rep (Ent) - Last_Repval /= 1 then
6452 Is_Contiguous := False;
6453 exit;
6454 else
6455 Last_Repval := Enumeration_Rep (Ent);
6456 end if;
6458 Next_Literal (Ent);
6459 end loop;
6461 if Is_Contiguous then
6462 Set_Has_Contiguous_Rep (Typ);
6463 Ent := First_Literal (Typ);
6464 Num := 1;
6465 Lst := New_List (New_Occurrence_Of (Ent, Sloc (Ent)));
6467 else
6468 -- Build list of literal references
6470 Lst := New_List;
6471 Num := 0;
6473 Ent := First_Literal (Typ);
6474 while Present (Ent) loop
6475 Append_To (Lst, New_Occurrence_Of (Ent, Sloc (Ent)));
6476 Num := Num + 1;
6477 Next_Literal (Ent);
6478 end loop;
6479 end if;
6481 -- Now build an array declaration
6483 -- typA : array (Natural range 0 .. num - 1) of ctype :=
6484 -- (v, v, v, v, v, ....)
6486 -- where ctype is the corresponding integer type. If the representation
6487 -- is contiguous, we only keep the first literal, which provides the
6488 -- offset for Pos_To_Rep computations.
6490 Arr :=
6491 Make_Defining_Identifier (Loc,
6492 Chars => New_External_Name (Chars (Typ), 'A'));
6494 Append_Freeze_Action (Typ,
6495 Make_Object_Declaration (Loc,
6496 Defining_Identifier => Arr,
6497 Constant_Present => True,
6499 Object_Definition =>
6500 Make_Constrained_Array_Definition (Loc,
6501 Discrete_Subtype_Definitions => New_List (
6502 Make_Subtype_Indication (Loc,
6503 Subtype_Mark => New_Occurrence_Of (Standard_Natural, Loc),
6504 Constraint =>
6505 Make_Range_Constraint (Loc,
6506 Range_Expression =>
6507 Make_Range (Loc,
6508 Low_Bound =>
6509 Make_Integer_Literal (Loc, 0),
6510 High_Bound =>
6511 Make_Integer_Literal (Loc, Num - 1))))),
6513 Component_Definition =>
6514 Make_Component_Definition (Loc,
6515 Aliased_Present => False,
6516 Subtype_Indication => New_Occurrence_Of (Typ, Loc))),
6518 Expression =>
6519 Make_Aggregate (Loc,
6520 Expressions => Lst)));
6522 Set_Enum_Pos_To_Rep (Typ, Arr);
6524 -- Now we build the function that converts representation values to
6525 -- position values. This function has the form:
6527 -- function _Rep_To_Pos (A : etype; F : Boolean) return Integer is
6528 -- begin
6529 -- case ityp!(A) is
6530 -- when enum-lit'Enum_Rep => return posval;
6531 -- when enum-lit'Enum_Rep => return posval;
6532 -- ...
6533 -- when others =>
6534 -- [raise Constraint_Error when F "invalid data"]
6535 -- return -1;
6536 -- end case;
6537 -- end;
6539 -- Note: the F parameter determines whether the others case (no valid
6540 -- representation) raises Constraint_Error or returns a unique value
6541 -- of minus one. The latter case is used, e.g. in 'Valid code.
6543 -- Note: the reason we use Enum_Rep values in the case here is to avoid
6544 -- the code generator making inappropriate assumptions about the range
6545 -- of the values in the case where the value is invalid. ityp is a
6546 -- signed or unsigned integer type of appropriate width.
6548 -- Note: if exceptions are not supported, then we suppress the raise
6549 -- and return -1 unconditionally (this is an erroneous program in any
6550 -- case and there is no obligation to raise Constraint_Error here). We
6551 -- also do this if pragma Restrictions (No_Exceptions) is active.
6553 -- Is this right??? What about No_Exception_Propagation???
6555 -- Representations are signed
6557 if Enumeration_Rep (First_Literal (Typ)) < 0 then
6559 -- The underlying type is signed. Reset the Is_Unsigned_Type
6560 -- explicitly, because it might have been inherited from
6561 -- parent type.
6563 Set_Is_Unsigned_Type (Typ, False);
6565 if Esize (Typ) <= Standard_Integer_Size then
6566 Ityp := Standard_Integer;
6567 else
6568 Ityp := Universal_Integer;
6569 end if;
6571 -- Representations are unsigned
6573 else
6574 if Esize (Typ) <= Standard_Integer_Size then
6575 Ityp := RTE (RE_Unsigned);
6576 else
6577 Ityp := RTE (RE_Long_Long_Unsigned);
6578 end if;
6579 end if;
6581 -- The body of the function is a case statement. First collect case
6582 -- alternatives, or optimize the contiguous case.
6584 Lst := New_List;
6586 -- If representation is contiguous, Pos is computed by subtracting
6587 -- the representation of the first literal.
6589 if Is_Contiguous then
6590 Ent := First_Literal (Typ);
6592 if Enumeration_Rep (Ent) = Last_Repval then
6594 -- Another special case: for a single literal, Pos is zero
6596 Pos_Expr := Make_Integer_Literal (Loc, Uint_0);
6598 else
6599 Pos_Expr :=
6600 Convert_To (Standard_Integer,
6601 Make_Op_Subtract (Loc,
6602 Left_Opnd =>
6603 Unchecked_Convert_To
6604 (Ityp, Make_Identifier (Loc, Name_uA)),
6605 Right_Opnd =>
6606 Make_Integer_Literal (Loc,
6607 Intval => Enumeration_Rep (First_Literal (Typ)))));
6608 end if;
6610 Append_To (Lst,
6611 Make_Case_Statement_Alternative (Loc,
6612 Discrete_Choices => New_List (
6613 Make_Range (Sloc (Enumeration_Rep_Expr (Ent)),
6614 Low_Bound =>
6615 Make_Integer_Literal (Loc,
6616 Intval => Enumeration_Rep (Ent)),
6617 High_Bound =>
6618 Make_Integer_Literal (Loc, Intval => Last_Repval))),
6620 Statements => New_List (
6621 Make_Simple_Return_Statement (Loc,
6622 Expression => Pos_Expr))));
6624 else
6625 Ent := First_Literal (Typ);
6626 while Present (Ent) loop
6627 Append_To (Lst,
6628 Make_Case_Statement_Alternative (Loc,
6629 Discrete_Choices => New_List (
6630 Make_Integer_Literal (Sloc (Enumeration_Rep_Expr (Ent)),
6631 Intval => Enumeration_Rep (Ent))),
6633 Statements => New_List (
6634 Make_Simple_Return_Statement (Loc,
6635 Expression =>
6636 Make_Integer_Literal (Loc,
6637 Intval => Enumeration_Pos (Ent))))));
6639 Next_Literal (Ent);
6640 end loop;
6641 end if;
6643 -- In normal mode, add the others clause with the test
6645 if not No_Exception_Handlers_Set then
6646 Append_To (Lst,
6647 Make_Case_Statement_Alternative (Loc,
6648 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
6649 Statements => New_List (
6650 Make_Raise_Constraint_Error (Loc,
6651 Condition => Make_Identifier (Loc, Name_uF),
6652 Reason => CE_Invalid_Data),
6653 Make_Simple_Return_Statement (Loc,
6654 Expression =>
6655 Make_Integer_Literal (Loc, -1)))));
6657 -- If either of the restrictions No_Exceptions_Handlers/Propagation is
6658 -- active then return -1 (we cannot usefully raise Constraint_Error in
6659 -- this case). See description above for further details.
6661 else
6662 Append_To (Lst,
6663 Make_Case_Statement_Alternative (Loc,
6664 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
6665 Statements => New_List (
6666 Make_Simple_Return_Statement (Loc,
6667 Expression =>
6668 Make_Integer_Literal (Loc, -1)))));
6669 end if;
6671 -- Now we can build the function body
6673 Fent :=
6674 Make_Defining_Identifier (Loc, Make_TSS_Name (Typ, TSS_Rep_To_Pos));
6676 Func :=
6677 Make_Subprogram_Body (Loc,
6678 Specification =>
6679 Make_Function_Specification (Loc,
6680 Defining_Unit_Name => Fent,
6681 Parameter_Specifications => New_List (
6682 Make_Parameter_Specification (Loc,
6683 Defining_Identifier =>
6684 Make_Defining_Identifier (Loc, Name_uA),
6685 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
6686 Make_Parameter_Specification (Loc,
6687 Defining_Identifier =>
6688 Make_Defining_Identifier (Loc, Name_uF),
6689 Parameter_Type =>
6690 New_Occurrence_Of (Standard_Boolean, Loc))),
6692 Result_Definition => New_Occurrence_Of (Standard_Integer, Loc)),
6694 Declarations => Empty_List,
6696 Handled_Statement_Sequence =>
6697 Make_Handled_Sequence_Of_Statements (Loc,
6698 Statements => New_List (
6699 Make_Case_Statement (Loc,
6700 Expression =>
6701 Unchecked_Convert_To
6702 (Ityp, Make_Identifier (Loc, Name_uA)),
6703 Alternatives => Lst))));
6705 Set_TSS (Typ, Fent);
6707 -- Set Pure flag (it will be reset if the current context is not Pure).
6708 -- We also pretend there was a pragma Pure_Function so that for purposes
6709 -- of optimization and constant-folding, we will consider the function
6710 -- Pure even if we are not in a Pure context).
6712 Set_Is_Pure (Fent);
6713 Set_Has_Pragma_Pure_Function (Fent);
6715 -- Unless we are in -gnatD mode, where we are debugging generated code,
6716 -- this is an internal entity for which we don't need debug info.
6718 if not Debug_Generated_Code then
6719 Set_Debug_Info_Off (Fent);
6720 end if;
6722 exception
6723 when RE_Not_Available =>
6724 return;
6725 end Expand_Freeze_Enumeration_Type;
6727 -------------------------------
6728 -- Expand_Freeze_Record_Type --
6729 -------------------------------
6731 procedure Expand_Freeze_Record_Type (N : Node_Id) is
6732 Def_Id : constant Node_Id := Entity (N);
6733 Type_Decl : constant Node_Id := Parent (Def_Id);
6734 Comp : Entity_Id;
6735 Comp_Typ : Entity_Id;
6736 Has_AACC : Boolean;
6737 Predef_List : List_Id;
6739 Renamed_Eq : Node_Id := Empty;
6740 -- Defining unit name for the predefined equality function in the case
6741 -- where the type has a primitive operation that is a renaming of
6742 -- predefined equality (but only if there is also an overriding
6743 -- user-defined equality function). Used to pass this entity from
6744 -- Make_Predefined_Primitive_Specs to Predefined_Primitive_Bodies.
6746 Wrapper_Decl_List : List_Id := No_List;
6747 Wrapper_Body_List : List_Id := No_List;
6749 -- Start of processing for Expand_Freeze_Record_Type
6751 begin
6752 -- Build discriminant checking functions if not a derived type (for
6753 -- derived types that are not tagged types, always use the discriminant
6754 -- checking functions of the parent type). However, for untagged types
6755 -- the derivation may have taken place before the parent was frozen, so
6756 -- we copy explicitly the discriminant checking functions from the
6757 -- parent into the components of the derived type.
6759 if not Is_Derived_Type (Def_Id)
6760 or else Has_New_Non_Standard_Rep (Def_Id)
6761 or else Is_Tagged_Type (Def_Id)
6762 then
6763 Build_Discr_Checking_Funcs (Type_Decl);
6765 elsif Is_Derived_Type (Def_Id)
6766 and then not Is_Tagged_Type (Def_Id)
6768 -- If we have a derived Unchecked_Union, we do not inherit the
6769 -- discriminant checking functions from the parent type since the
6770 -- discriminants are non existent.
6772 and then not Is_Unchecked_Union (Def_Id)
6773 and then Has_Discriminants (Def_Id)
6774 then
6775 declare
6776 Old_Comp : Entity_Id;
6778 begin
6779 Old_Comp :=
6780 First_Component (Base_Type (Underlying_Type (Etype (Def_Id))));
6781 Comp := First_Component (Def_Id);
6782 while Present (Comp) loop
6783 if Ekind (Comp) = E_Component
6784 and then Chars (Comp) = Chars (Old_Comp)
6785 then
6786 Set_Discriminant_Checking_Func (Comp,
6787 Discriminant_Checking_Func (Old_Comp));
6788 end if;
6790 Next_Component (Old_Comp);
6791 Next_Component (Comp);
6792 end loop;
6793 end;
6794 end if;
6796 if Is_Derived_Type (Def_Id)
6797 and then Is_Limited_Type (Def_Id)
6798 and then Is_Tagged_Type (Def_Id)
6799 then
6800 Check_Stream_Attributes (Def_Id);
6801 end if;
6803 -- Update task, protected, and controlled component flags, because some
6804 -- of the component types may have been private at the point of the
6805 -- record declaration. Detect anonymous access-to-controlled components.
6807 Has_AACC := False;
6809 Comp := First_Component (Def_Id);
6810 while Present (Comp) loop
6811 Comp_Typ := Etype (Comp);
6813 if Has_Task (Comp_Typ) then
6814 Set_Has_Task (Def_Id);
6815 end if;
6817 if Has_Protected (Comp_Typ) then
6818 Set_Has_Protected (Def_Id);
6819 end if;
6821 -- Do not set Has_Controlled_Component on a class-wide equivalent
6822 -- type. See Make_CW_Equivalent_Type.
6824 if not Is_Class_Wide_Equivalent_Type (Def_Id)
6825 and then (Has_Controlled_Component (Comp_Typ)
6826 or else (Chars (Comp) /= Name_uParent
6827 and then Is_Controlled (Comp_Typ)))
6828 then
6829 Set_Has_Controlled_Component (Def_Id);
6830 end if;
6832 -- Non-self-referential anonymous access-to-controlled component
6834 if Ekind (Comp_Typ) = E_Anonymous_Access_Type
6835 and then Needs_Finalization (Designated_Type (Comp_Typ))
6836 and then Designated_Type (Comp_Typ) /= Def_Id
6837 then
6838 Has_AACC := True;
6839 end if;
6841 Next_Component (Comp);
6842 end loop;
6844 -- Handle constructors of untagged CPP_Class types
6846 if not Is_Tagged_Type (Def_Id) and then Is_CPP_Class (Def_Id) then
6847 Set_CPP_Constructors (Def_Id);
6848 end if;
6850 -- Creation of the Dispatch Table. Note that a Dispatch Table is built
6851 -- for regular tagged types as well as for Ada types deriving from a C++
6852 -- Class, but not for tagged types directly corresponding to C++ classes
6853 -- In the later case we assume that it is created in the C++ side and we
6854 -- just use it.
6856 if Is_Tagged_Type (Def_Id) then
6858 -- Add the _Tag component
6860 if Underlying_Type (Etype (Def_Id)) = Def_Id then
6861 Expand_Tagged_Root (Def_Id);
6862 end if;
6864 if Is_CPP_Class (Def_Id) then
6865 Set_All_DT_Position (Def_Id);
6867 -- Create the tag entities with a minimum decoration
6869 if Tagged_Type_Expansion then
6870 Append_Freeze_Actions (Def_Id, Make_Tags (Def_Id));
6871 end if;
6873 Set_CPP_Constructors (Def_Id);
6875 else
6876 if not Building_Static_DT (Def_Id) then
6878 -- Usually inherited primitives are not delayed but the first
6879 -- Ada extension of a CPP_Class is an exception since the
6880 -- address of the inherited subprogram has to be inserted in
6881 -- the new Ada Dispatch Table and this is a freezing action.
6883 -- Similarly, if this is an inherited operation whose parent is
6884 -- not frozen yet, it is not in the DT of the parent, and we
6885 -- generate an explicit freeze node for the inherited operation
6886 -- so it is properly inserted in the DT of the current type.
6888 declare
6889 Elmt : Elmt_Id;
6890 Subp : Entity_Id;
6892 begin
6893 Elmt := First_Elmt (Primitive_Operations (Def_Id));
6894 while Present (Elmt) loop
6895 Subp := Node (Elmt);
6897 if Present (Alias (Subp)) then
6898 if Is_CPP_Class (Etype (Def_Id)) then
6899 Set_Has_Delayed_Freeze (Subp);
6901 elsif Has_Delayed_Freeze (Alias (Subp))
6902 and then not Is_Frozen (Alias (Subp))
6903 then
6904 Set_Is_Frozen (Subp, False);
6905 Set_Has_Delayed_Freeze (Subp);
6906 end if;
6907 end if;
6909 Next_Elmt (Elmt);
6910 end loop;
6911 end;
6912 end if;
6914 -- Unfreeze momentarily the type to add the predefined primitives
6915 -- operations. The reason we unfreeze is so that these predefined
6916 -- operations will indeed end up as primitive operations (which
6917 -- must be before the freeze point).
6919 Set_Is_Frozen (Def_Id, False);
6921 -- Do not add the spec of predefined primitives in case of
6922 -- CPP tagged type derivations that have convention CPP.
6924 if Is_CPP_Class (Root_Type (Def_Id))
6925 and then Convention (Def_Id) = Convention_CPP
6926 then
6927 null;
6929 -- Do not add the spec of predefined primitives in case of
6930 -- CIL and Java tagged types
6932 elsif Convention (Def_Id) = Convention_CIL
6933 or else Convention (Def_Id) = Convention_Java
6934 then
6935 null;
6937 -- Do not add the spec of the predefined primitives if we are
6938 -- compiling under restriction No_Dispatching_Calls.
6940 elsif not Restriction_Active (No_Dispatching_Calls) then
6941 Make_Predefined_Primitive_Specs
6942 (Def_Id, Predef_List, Renamed_Eq);
6943 Insert_List_Before_And_Analyze (N, Predef_List);
6944 end if;
6946 -- Ada 2005 (AI-391): For a nonabstract null extension, create
6947 -- wrapper functions for each nonoverridden inherited function
6948 -- with a controlling result of the type. The wrapper for such
6949 -- a function returns an extension aggregate that invokes the
6950 -- parent function.
6952 if Ada_Version >= Ada_2005
6953 and then not Is_Abstract_Type (Def_Id)
6954 and then Is_Null_Extension (Def_Id)
6955 then
6956 Make_Controlling_Function_Wrappers
6957 (Def_Id, Wrapper_Decl_List, Wrapper_Body_List);
6958 Insert_List_Before_And_Analyze (N, Wrapper_Decl_List);
6959 end if;
6961 -- Ada 2005 (AI-251): For a nonabstract type extension, build
6962 -- null procedure declarations for each set of homographic null
6963 -- procedures that are inherited from interface types but not
6964 -- overridden. This is done to ensure that the dispatch table
6965 -- entry associated with such null primitives are properly filled.
6967 if Ada_Version >= Ada_2005
6968 and then Etype (Def_Id) /= Def_Id
6969 and then not Is_Abstract_Type (Def_Id)
6970 and then Has_Interfaces (Def_Id)
6971 then
6972 Insert_Actions (N, Make_Null_Procedure_Specs (Def_Id));
6973 end if;
6975 Set_Is_Frozen (Def_Id);
6976 if not Is_Derived_Type (Def_Id)
6977 or else Is_Tagged_Type (Etype (Def_Id))
6978 then
6979 Set_All_DT_Position (Def_Id);
6981 -- If this is a type derived from an untagged private type whose
6982 -- full view is tagged, the type is marked tagged for layout
6983 -- reasons, but it has no dispatch table.
6985 elsif Is_Derived_Type (Def_Id)
6986 and then Is_Private_Type (Etype (Def_Id))
6987 and then not Is_Tagged_Type (Etype (Def_Id))
6988 then
6989 return;
6990 end if;
6992 -- Create and decorate the tags. Suppress their creation when
6993 -- VM_Target because the dispatching mechanism is handled
6994 -- internally by the VMs.
6996 if Tagged_Type_Expansion then
6997 Append_Freeze_Actions (Def_Id, Make_Tags (Def_Id));
6999 -- Generate dispatch table of locally defined tagged type.
7000 -- Dispatch tables of library level tagged types are built
7001 -- later (see Analyze_Declarations).
7003 if not Building_Static_DT (Def_Id) then
7004 Append_Freeze_Actions (Def_Id, Make_DT (Def_Id));
7005 end if;
7007 elsif VM_Target /= No_VM then
7008 Append_Freeze_Actions (Def_Id, Make_VM_TSD (Def_Id));
7009 end if;
7011 -- If the type has unknown discriminants, propagate dispatching
7012 -- information to its underlying record view, which does not get
7013 -- its own dispatch table.
7015 if Is_Derived_Type (Def_Id)
7016 and then Has_Unknown_Discriminants (Def_Id)
7017 and then Present (Underlying_Record_View (Def_Id))
7018 then
7019 declare
7020 Rep : constant Entity_Id := Underlying_Record_View (Def_Id);
7021 begin
7022 Set_Access_Disp_Table
7023 (Rep, Access_Disp_Table (Def_Id));
7024 Set_Dispatch_Table_Wrappers
7025 (Rep, Dispatch_Table_Wrappers (Def_Id));
7026 Set_Direct_Primitive_Operations
7027 (Rep, Direct_Primitive_Operations (Def_Id));
7028 end;
7029 end if;
7031 -- Make sure that the primitives Initialize, Adjust and Finalize
7032 -- are Frozen before other TSS subprograms. We don't want them
7033 -- Frozen inside.
7035 if Is_Controlled (Def_Id) then
7036 if not Is_Limited_Type (Def_Id) then
7037 Append_Freeze_Actions (Def_Id,
7038 Freeze_Entity
7039 (Find_Prim_Op (Def_Id, Name_Adjust), Def_Id));
7040 end if;
7042 Append_Freeze_Actions (Def_Id,
7043 Freeze_Entity
7044 (Find_Prim_Op (Def_Id, Name_Initialize), Def_Id));
7046 Append_Freeze_Actions (Def_Id,
7047 Freeze_Entity
7048 (Find_Prim_Op (Def_Id, Name_Finalize), Def_Id));
7049 end if;
7051 -- Freeze rest of primitive operations. There is no need to handle
7052 -- the predefined primitives if we are compiling under restriction
7053 -- No_Dispatching_Calls.
7055 if not Restriction_Active (No_Dispatching_Calls) then
7056 Append_Freeze_Actions
7057 (Def_Id, Predefined_Primitive_Freeze (Def_Id));
7058 end if;
7059 end if;
7061 -- In the untagged case, ever since Ada 83 an equality function must
7062 -- be provided for variant records that are not unchecked unions.
7063 -- In Ada 2012 the equality function composes, and thus must be built
7064 -- explicitly just as for tagged records.
7066 elsif Has_Discriminants (Def_Id)
7067 and then not Is_Limited_Type (Def_Id)
7068 then
7069 declare
7070 Comps : constant Node_Id :=
7071 Component_List (Type_Definition (Type_Decl));
7072 begin
7073 if Present (Comps)
7074 and then Present (Variant_Part (Comps))
7075 then
7076 Build_Variant_Record_Equality (Def_Id);
7077 end if;
7078 end;
7080 -- Otherwise create primitive equality operation (AI05-0123)
7082 -- This is done unconditionally to ensure that tools can be linked
7083 -- properly with user programs compiled with older language versions.
7084 -- In addition, this is needed because "=" composes for bounded strings
7085 -- in all language versions (see Exp_Ch4.Expand_Composite_Equality).
7087 elsif Comes_From_Source (Def_Id)
7088 and then Convention (Def_Id) = Convention_Ada
7089 and then not Is_Limited_Type (Def_Id)
7090 then
7091 Build_Untagged_Equality (Def_Id);
7092 end if;
7094 -- Before building the record initialization procedure, if we are
7095 -- dealing with a concurrent record value type, then we must go through
7096 -- the discriminants, exchanging discriminals between the concurrent
7097 -- type and the concurrent record value type. See the section "Handling
7098 -- of Discriminants" in the Einfo spec for details.
7100 if Is_Concurrent_Record_Type (Def_Id)
7101 and then Has_Discriminants (Def_Id)
7102 then
7103 declare
7104 Ctyp : constant Entity_Id :=
7105 Corresponding_Concurrent_Type (Def_Id);
7106 Conc_Discr : Entity_Id;
7107 Rec_Discr : Entity_Id;
7108 Temp : Entity_Id;
7110 begin
7111 Conc_Discr := First_Discriminant (Ctyp);
7112 Rec_Discr := First_Discriminant (Def_Id);
7113 while Present (Conc_Discr) loop
7114 Temp := Discriminal (Conc_Discr);
7115 Set_Discriminal (Conc_Discr, Discriminal (Rec_Discr));
7116 Set_Discriminal (Rec_Discr, Temp);
7118 Set_Discriminal_Link (Discriminal (Conc_Discr), Conc_Discr);
7119 Set_Discriminal_Link (Discriminal (Rec_Discr), Rec_Discr);
7121 Next_Discriminant (Conc_Discr);
7122 Next_Discriminant (Rec_Discr);
7123 end loop;
7124 end;
7125 end if;
7127 if Has_Controlled_Component (Def_Id) then
7128 Build_Controlling_Procs (Def_Id);
7129 end if;
7131 Adjust_Discriminants (Def_Id);
7133 if Tagged_Type_Expansion or else not Is_Interface (Def_Id) then
7135 -- Do not need init for interfaces on e.g. CIL since they're
7136 -- abstract. Helps operation of peverify (the PE Verify tool).
7138 Build_Record_Init_Proc (Type_Decl, Def_Id);
7139 end if;
7141 -- For tagged type that are not interfaces, build bodies of primitive
7142 -- operations. Note: do this after building the record initialization
7143 -- procedure, since the primitive operations may need the initialization
7144 -- routine. There is no need to add predefined primitives of interfaces
7145 -- because all their predefined primitives are abstract.
7147 if Is_Tagged_Type (Def_Id) and then not Is_Interface (Def_Id) then
7149 -- Do not add the body of predefined primitives in case of CPP tagged
7150 -- type derivations that have convention CPP.
7152 if Is_CPP_Class (Root_Type (Def_Id))
7153 and then Convention (Def_Id) = Convention_CPP
7154 then
7155 null;
7157 -- Do not add the body of predefined primitives in case of CIL and
7158 -- Java tagged types.
7160 elsif Convention (Def_Id) = Convention_CIL
7161 or else Convention (Def_Id) = Convention_Java
7162 then
7163 null;
7165 -- Do not add the body of the predefined primitives if we are
7166 -- compiling under restriction No_Dispatching_Calls or if we are
7167 -- compiling a CPP tagged type.
7169 elsif not Restriction_Active (No_Dispatching_Calls) then
7171 -- Create the body of TSS primitive Finalize_Address. This must
7172 -- be done before the bodies of all predefined primitives are
7173 -- created. If Def_Id is limited, Stream_Input and Stream_Read
7174 -- may produce build-in-place allocations and for those the
7175 -- expander needs Finalize_Address.
7177 Make_Finalize_Address_Body (Def_Id);
7178 Predef_List := Predefined_Primitive_Bodies (Def_Id, Renamed_Eq);
7179 Append_Freeze_Actions (Def_Id, Predef_List);
7180 end if;
7182 -- Ada 2005 (AI-391): If any wrappers were created for nonoverridden
7183 -- inherited functions, then add their bodies to the freeze actions.
7185 if Present (Wrapper_Body_List) then
7186 Append_Freeze_Actions (Def_Id, Wrapper_Body_List);
7187 end if;
7189 -- Create extra formals for the primitive operations of the type.
7190 -- This must be done before analyzing the body of the initialization
7191 -- procedure, because a self-referential type might call one of these
7192 -- primitives in the body of the init_proc itself.
7194 declare
7195 Elmt : Elmt_Id;
7196 Subp : Entity_Id;
7198 begin
7199 Elmt := First_Elmt (Primitive_Operations (Def_Id));
7200 while Present (Elmt) loop
7201 Subp := Node (Elmt);
7202 if not Has_Foreign_Convention (Subp)
7203 and then not Is_Predefined_Dispatching_Operation (Subp)
7204 then
7205 Create_Extra_Formals (Subp);
7206 end if;
7208 Next_Elmt (Elmt);
7209 end loop;
7210 end;
7211 end if;
7213 -- Create a heterogeneous finalization master to service the anonymous
7214 -- access-to-controlled components of the record type.
7216 if Has_AACC then
7217 declare
7218 Encl_Scope : constant Entity_Id := Scope (Def_Id);
7219 Ins_Node : constant Node_Id := Parent (Def_Id);
7220 Loc : constant Source_Ptr := Sloc (Def_Id);
7221 Fin_Mas_Id : Entity_Id;
7223 Attributes_Set : Boolean := False;
7224 Master_Built : Boolean := False;
7225 -- Two flags which control the creation and initialization of a
7226 -- common heterogeneous master.
7228 begin
7229 Comp := First_Component (Def_Id);
7230 while Present (Comp) loop
7231 Comp_Typ := Etype (Comp);
7233 -- A non-self-referential anonymous access-to-controlled
7234 -- component.
7236 if Ekind (Comp_Typ) = E_Anonymous_Access_Type
7237 and then Needs_Finalization (Designated_Type (Comp_Typ))
7238 and then Designated_Type (Comp_Typ) /= Def_Id
7239 then
7240 if VM_Target = No_VM then
7242 -- Build a homogeneous master for the first anonymous
7243 -- access-to-controlled component. This master may be
7244 -- converted into a heterogeneous collection if more
7245 -- components are to follow.
7247 if not Master_Built then
7248 Master_Built := True;
7250 -- All anonymous access-to-controlled types allocate
7251 -- on the global pool. Note that the finalization
7252 -- master and the associated storage pool must be set
7253 -- on the root type (both are "root type only").
7255 Set_Associated_Storage_Pool
7256 (Root_Type (Comp_Typ), RTE (RE_Global_Pool_Object));
7258 Build_Finalization_Master
7259 (Typ => Root_Type (Comp_Typ),
7260 Ins_Node => Ins_Node,
7261 Encl_Scope => Encl_Scope);
7263 Fin_Mas_Id := Finalization_Master (Comp_Typ);
7265 -- Subsequent anonymous access-to-controlled components
7266 -- reuse the available master.
7268 else
7269 -- All anonymous access-to-controlled types allocate
7270 -- on the global pool. Note that both the finalization
7271 -- master and the associated storage pool must be set
7272 -- on the root type (both are "root type only").
7274 Set_Associated_Storage_Pool
7275 (Root_Type (Comp_Typ), RTE (RE_Global_Pool_Object));
7277 -- Shared the master among multiple components
7279 Set_Finalization_Master
7280 (Root_Type (Comp_Typ), Fin_Mas_Id);
7282 -- Convert the master into a heterogeneous collection.
7283 -- Generate:
7284 -- Set_Is_Heterogeneous (<Fin_Mas_Id>);
7286 if not Attributes_Set then
7287 Attributes_Set := True;
7289 Insert_Action (Ins_Node,
7290 Make_Procedure_Call_Statement (Loc,
7291 Name =>
7292 New_Occurrence_Of
7293 (RTE (RE_Set_Is_Heterogeneous), Loc),
7294 Parameter_Associations => New_List (
7295 New_Occurrence_Of (Fin_Mas_Id, Loc))));
7296 end if;
7297 end if;
7299 -- Since .NET/JVM targets do not support heterogeneous
7300 -- masters, each component must have its own master.
7302 else
7303 Build_Finalization_Master
7304 (Typ => Comp_Typ,
7305 Ins_Node => Ins_Node,
7306 Encl_Scope => Encl_Scope);
7307 end if;
7308 end if;
7310 Next_Component (Comp);
7311 end loop;
7312 end;
7313 end if;
7315 -- Check whether individual components have a defined invariant, and add
7316 -- the corresponding component invariant checks.
7318 -- Do not create an invariant procedure for some internally generated
7319 -- subtypes, in particular those created for objects of a class-wide
7320 -- type. Such types may have components to which invariant apply, but
7321 -- the corresponding checks will be applied when an object of the parent
7322 -- type is constructed.
7324 -- Such objects will show up in a class-wide postcondition, and the
7325 -- invariant will be checked, if necessary, upon return from the
7326 -- enclosing subprogram.
7328 if not Is_Class_Wide_Equivalent_Type (Def_Id) then
7329 Insert_Component_Invariant_Checks
7330 (N, Def_Id, Build_Record_Invariant_Proc (Def_Id, N));
7331 end if;
7332 end Expand_Freeze_Record_Type;
7334 ------------------------------
7335 -- Freeze_Stream_Operations --
7336 ------------------------------
7338 procedure Freeze_Stream_Operations (N : Node_Id; Typ : Entity_Id) is
7339 Names : constant array (1 .. 4) of TSS_Name_Type :=
7340 (TSS_Stream_Input,
7341 TSS_Stream_Output,
7342 TSS_Stream_Read,
7343 TSS_Stream_Write);
7344 Stream_Op : Entity_Id;
7346 begin
7347 -- Primitive operations of tagged types are frozen when the dispatch
7348 -- table is constructed.
7350 if not Comes_From_Source (Typ) or else Is_Tagged_Type (Typ) then
7351 return;
7352 end if;
7354 for J in Names'Range loop
7355 Stream_Op := TSS (Typ, Names (J));
7357 if Present (Stream_Op)
7358 and then Is_Subprogram (Stream_Op)
7359 and then Nkind (Unit_Declaration_Node (Stream_Op)) =
7360 N_Subprogram_Declaration
7361 and then not Is_Frozen (Stream_Op)
7362 then
7363 Append_Freeze_Actions (Typ, Freeze_Entity (Stream_Op, N));
7364 end if;
7365 end loop;
7366 end Freeze_Stream_Operations;
7368 -----------------
7369 -- Freeze_Type --
7370 -----------------
7372 -- Full type declarations are expanded at the point at which the type is
7373 -- frozen. The formal N is the Freeze_Node for the type. Any statements or
7374 -- declarations generated by the freezing (e.g. the procedure generated
7375 -- for initialization) are chained in the Actions field list of the freeze
7376 -- node using Append_Freeze_Actions.
7378 function Freeze_Type (N : Node_Id) return Boolean is
7379 Def_Id : constant Entity_Id := Entity (N);
7380 RACW_Seen : Boolean := False;
7381 Result : Boolean := False;
7383 begin
7384 -- Process associated access types needing special processing
7386 if Present (Access_Types_To_Process (N)) then
7387 declare
7388 E : Elmt_Id := First_Elmt (Access_Types_To_Process (N));
7390 begin
7391 while Present (E) loop
7392 if Is_Remote_Access_To_Class_Wide_Type (Node (E)) then
7393 Validate_RACW_Primitives (Node (E));
7394 RACW_Seen := True;
7395 end if;
7397 E := Next_Elmt (E);
7398 end loop;
7399 end;
7401 -- If there are RACWs designating this type, make stubs now
7403 if RACW_Seen then
7404 Remote_Types_Tagged_Full_View_Encountered (Def_Id);
7405 end if;
7406 end if;
7408 -- Freeze processing for record types
7410 if Is_Record_Type (Def_Id) then
7411 if Ekind (Def_Id) = E_Record_Type then
7412 Expand_Freeze_Record_Type (N);
7413 elsif Is_Class_Wide_Type (Def_Id) then
7414 Expand_Freeze_Class_Wide_Type (N);
7415 end if;
7417 -- Freeze processing for array types
7419 elsif Is_Array_Type (Def_Id) then
7420 Expand_Freeze_Array_Type (N);
7422 -- Freeze processing for access types
7424 -- For pool-specific access types, find out the pool object used for
7425 -- this type, needs actual expansion of it in some cases. Here are the
7426 -- different cases :
7428 -- 1. Rep Clause "for Def_Id'Storage_Size use 0;"
7429 -- ---> don't use any storage pool
7431 -- 2. Rep Clause : for Def_Id'Storage_Size use Expr.
7432 -- Expand:
7433 -- Def_Id__Pool : Stack_Bounded_Pool (Expr, DT'Size, DT'Alignment);
7435 -- 3. Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
7436 -- ---> Storage Pool is the specified one
7438 -- See GNAT Pool packages in the Run-Time for more details
7440 elsif Ekind_In (Def_Id, E_Access_Type, E_General_Access_Type) then
7441 declare
7442 Loc : constant Source_Ptr := Sloc (N);
7443 Desig_Type : constant Entity_Id := Designated_Type (Def_Id);
7444 Pool_Object : Entity_Id;
7446 Freeze_Action_Typ : Entity_Id;
7448 begin
7449 -- Case 1
7451 -- Rep Clause "for Def_Id'Storage_Size use 0;"
7452 -- ---> don't use any storage pool
7454 if No_Pool_Assigned (Def_Id) then
7455 null;
7457 -- Case 2
7459 -- Rep Clause : for Def_Id'Storage_Size use Expr.
7460 -- ---> Expand:
7461 -- Def_Id__Pool : Stack_Bounded_Pool
7462 -- (Expr, DT'Size, DT'Alignment);
7464 elsif Has_Storage_Size_Clause (Def_Id) then
7465 declare
7466 DT_Size : Node_Id;
7467 DT_Align : Node_Id;
7469 begin
7470 -- For unconstrained composite types we give a size of zero
7471 -- so that the pool knows that it needs a special algorithm
7472 -- for variable size object allocation.
7474 if Is_Composite_Type (Desig_Type)
7475 and then not Is_Constrained (Desig_Type)
7476 then
7477 DT_Size := Make_Integer_Literal (Loc, 0);
7478 DT_Align := Make_Integer_Literal (Loc, Maximum_Alignment);
7480 else
7481 DT_Size :=
7482 Make_Attribute_Reference (Loc,
7483 Prefix => New_Occurrence_Of (Desig_Type, Loc),
7484 Attribute_Name => Name_Max_Size_In_Storage_Elements);
7486 DT_Align :=
7487 Make_Attribute_Reference (Loc,
7488 Prefix => New_Occurrence_Of (Desig_Type, Loc),
7489 Attribute_Name => Name_Alignment);
7490 end if;
7492 Pool_Object :=
7493 Make_Defining_Identifier (Loc,
7494 Chars => New_External_Name (Chars (Def_Id), 'P'));
7496 -- We put the code associated with the pools in the entity
7497 -- that has the later freeze node, usually the access type
7498 -- but it can also be the designated_type; because the pool
7499 -- code requires both those types to be frozen
7501 if Is_Frozen (Desig_Type)
7502 and then (No (Freeze_Node (Desig_Type))
7503 or else Analyzed (Freeze_Node (Desig_Type)))
7504 then
7505 Freeze_Action_Typ := Def_Id;
7507 -- A Taft amendment type cannot get the freeze actions
7508 -- since the full view is not there.
7510 elsif Is_Incomplete_Or_Private_Type (Desig_Type)
7511 and then No (Full_View (Desig_Type))
7512 then
7513 Freeze_Action_Typ := Def_Id;
7515 else
7516 Freeze_Action_Typ := Desig_Type;
7517 end if;
7519 Append_Freeze_Action (Freeze_Action_Typ,
7520 Make_Object_Declaration (Loc,
7521 Defining_Identifier => Pool_Object,
7522 Object_Definition =>
7523 Make_Subtype_Indication (Loc,
7524 Subtype_Mark =>
7525 New_Occurrence_Of
7526 (RTE (RE_Stack_Bounded_Pool), Loc),
7528 Constraint =>
7529 Make_Index_Or_Discriminant_Constraint (Loc,
7530 Constraints => New_List (
7532 -- First discriminant is the Pool Size
7534 New_Occurrence_Of (
7535 Storage_Size_Variable (Def_Id), Loc),
7537 -- Second discriminant is the element size
7539 DT_Size,
7541 -- Third discriminant is the alignment
7543 DT_Align)))));
7544 end;
7546 Set_Associated_Storage_Pool (Def_Id, Pool_Object);
7548 -- Case 3
7550 -- Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
7551 -- ---> Storage Pool is the specified one
7553 -- When compiling in Ada 2012 mode, ensure that the accessibility
7554 -- level of the subpool access type is not deeper than that of the
7555 -- pool_with_subpools.
7557 elsif Ada_Version >= Ada_2012
7558 and then Present (Associated_Storage_Pool (Def_Id))
7560 -- Omit this check on .NET/JVM where pools are not supported
7562 and then VM_Target = No_VM
7564 -- Omit this check for the case of a configurable run-time that
7565 -- does not provide package System.Storage_Pools.Subpools.
7567 and then RTE_Available (RE_Root_Storage_Pool_With_Subpools)
7568 then
7569 declare
7570 Loc : constant Source_Ptr := Sloc (Def_Id);
7571 Pool : constant Entity_Id :=
7572 Associated_Storage_Pool (Def_Id);
7573 RSPWS : constant Entity_Id :=
7574 RTE (RE_Root_Storage_Pool_With_Subpools);
7576 begin
7577 -- It is known that the accessibility level of the access
7578 -- type is deeper than that of the pool.
7580 if Type_Access_Level (Def_Id) > Object_Access_Level (Pool)
7581 and then not Accessibility_Checks_Suppressed (Def_Id)
7582 and then not Accessibility_Checks_Suppressed (Pool)
7583 then
7584 -- Static case: the pool is known to be a descendant of
7585 -- Root_Storage_Pool_With_Subpools.
7587 if Is_Ancestor (RSPWS, Etype (Pool)) then
7588 Error_Msg_N
7589 ("??subpool access type has deeper accessibility "
7590 & "level than pool", Def_Id);
7592 Append_Freeze_Action (Def_Id,
7593 Make_Raise_Program_Error (Loc,
7594 Reason => PE_Accessibility_Check_Failed));
7596 -- Dynamic case: when the pool is of a class-wide type,
7597 -- it may or may not support subpools depending on the
7598 -- path of derivation. Generate:
7600 -- if Def_Id in RSPWS'Class then
7601 -- raise Program_Error;
7602 -- end if;
7604 elsif Is_Class_Wide_Type (Etype (Pool)) then
7605 Append_Freeze_Action (Def_Id,
7606 Make_If_Statement (Loc,
7607 Condition =>
7608 Make_In (Loc,
7609 Left_Opnd => New_Occurrence_Of (Pool, Loc),
7610 Right_Opnd =>
7611 New_Occurrence_Of
7612 (Class_Wide_Type (RSPWS), Loc)),
7614 Then_Statements => New_List (
7615 Make_Raise_Program_Error (Loc,
7616 Reason => PE_Accessibility_Check_Failed))));
7617 end if;
7618 end if;
7619 end;
7620 end if;
7622 -- For access-to-controlled types (including class-wide types and
7623 -- Taft-amendment types, which potentially have controlled
7624 -- components), expand the list controller object that will store
7625 -- the dynamically allocated objects. Don't do this transformation
7626 -- for expander-generated access types, but do it for types that
7627 -- are the full view of types derived from other private types.
7628 -- Also suppress the list controller in the case of a designated
7629 -- type with convention Java, since this is used when binding to
7630 -- Java API specs, where there's no equivalent of a finalization
7631 -- list and we don't want to pull in the finalization support if
7632 -- not needed.
7634 if not Comes_From_Source (Def_Id)
7635 and then not Has_Private_Declaration (Def_Id)
7636 then
7637 null;
7639 -- An exception is made for types defined in the run-time because
7640 -- Ada.Tags.Tag itself is such a type and cannot afford this
7641 -- unnecessary overhead that would generates a loop in the
7642 -- expansion scheme. Another exception is if Restrictions
7643 -- (No_Finalization) is active, since then we know nothing is
7644 -- controlled.
7646 elsif Restriction_Active (No_Finalization)
7647 or else In_Runtime (Def_Id)
7648 then
7649 null;
7651 -- Assume that incomplete and private types are always completed
7652 -- by a controlled full view.
7654 elsif Needs_Finalization (Desig_Type)
7655 or else
7656 (Is_Incomplete_Or_Private_Type (Desig_Type)
7657 and then No (Full_View (Desig_Type)))
7658 or else
7659 (Is_Array_Type (Desig_Type)
7660 and then Needs_Finalization (Component_Type (Desig_Type)))
7661 then
7662 Build_Finalization_Master (Def_Id);
7663 end if;
7664 end;
7666 -- Freeze processing for enumeration types
7668 elsif Ekind (Def_Id) = E_Enumeration_Type then
7670 -- We only have something to do if we have a non-standard
7671 -- representation (i.e. at least one literal whose pos value
7672 -- is not the same as its representation)
7674 if Has_Non_Standard_Rep (Def_Id) then
7675 Expand_Freeze_Enumeration_Type (N);
7676 end if;
7678 -- Private types that are completed by a derivation from a private
7679 -- type have an internally generated full view, that needs to be
7680 -- frozen. This must be done explicitly because the two views share
7681 -- the freeze node, and the underlying full view is not visible when
7682 -- the freeze node is analyzed.
7684 elsif Is_Private_Type (Def_Id)
7685 and then Is_Derived_Type (Def_Id)
7686 and then Present (Full_View (Def_Id))
7687 and then Is_Itype (Full_View (Def_Id))
7688 and then Has_Private_Declaration (Full_View (Def_Id))
7689 and then Freeze_Node (Full_View (Def_Id)) = N
7690 then
7691 Set_Entity (N, Full_View (Def_Id));
7692 Result := Freeze_Type (N);
7693 Set_Entity (N, Def_Id);
7695 -- All other types require no expander action. There are such cases
7696 -- (e.g. task types and protected types). In such cases, the freeze
7697 -- nodes are there for use by Gigi.
7699 end if;
7701 Freeze_Stream_Operations (N, Def_Id);
7702 return Result;
7704 exception
7705 when RE_Not_Available =>
7706 return False;
7707 end Freeze_Type;
7709 -------------------------
7710 -- Get_Simple_Init_Val --
7711 -------------------------
7713 function Get_Simple_Init_Val
7714 (T : Entity_Id;
7715 N : Node_Id;
7716 Size : Uint := No_Uint) return Node_Id
7718 Loc : constant Source_Ptr := Sloc (N);
7719 Val : Node_Id;
7720 Result : Node_Id;
7721 Val_RE : RE_Id;
7723 Size_To_Use : Uint;
7724 -- This is the size to be used for computation of the appropriate
7725 -- initial value for the Normalize_Scalars and Initialize_Scalars case.
7727 IV_Attribute : constant Boolean :=
7728 Nkind (N) = N_Attribute_Reference
7729 and then Attribute_Name (N) = Name_Invalid_Value;
7731 Lo_Bound : Uint;
7732 Hi_Bound : Uint;
7733 -- These are the values computed by the procedure Check_Subtype_Bounds
7735 procedure Check_Subtype_Bounds;
7736 -- This procedure examines the subtype T, and its ancestor subtypes and
7737 -- derived types to determine the best known information about the
7738 -- bounds of the subtype. After the call Lo_Bound is set either to
7739 -- No_Uint if no information can be determined, or to a value which
7740 -- represents a known low bound, i.e. a valid value of the subtype can
7741 -- not be less than this value. Hi_Bound is similarly set to a known
7742 -- high bound (valid value cannot be greater than this).
7744 --------------------------
7745 -- Check_Subtype_Bounds --
7746 --------------------------
7748 procedure Check_Subtype_Bounds is
7749 ST1 : Entity_Id;
7750 ST2 : Entity_Id;
7751 Lo : Node_Id;
7752 Hi : Node_Id;
7753 Loval : Uint;
7754 Hival : Uint;
7756 begin
7757 Lo_Bound := No_Uint;
7758 Hi_Bound := No_Uint;
7760 -- Loop to climb ancestor subtypes and derived types
7762 ST1 := T;
7763 loop
7764 if not Is_Discrete_Type (ST1) then
7765 return;
7766 end if;
7768 Lo := Type_Low_Bound (ST1);
7769 Hi := Type_High_Bound (ST1);
7771 if Compile_Time_Known_Value (Lo) then
7772 Loval := Expr_Value (Lo);
7774 if Lo_Bound = No_Uint or else Lo_Bound < Loval then
7775 Lo_Bound := Loval;
7776 end if;
7777 end if;
7779 if Compile_Time_Known_Value (Hi) then
7780 Hival := Expr_Value (Hi);
7782 if Hi_Bound = No_Uint or else Hi_Bound > Hival then
7783 Hi_Bound := Hival;
7784 end if;
7785 end if;
7787 ST2 := Ancestor_Subtype (ST1);
7789 if No (ST2) then
7790 ST2 := Etype (ST1);
7791 end if;
7793 exit when ST1 = ST2;
7794 ST1 := ST2;
7795 end loop;
7796 end Check_Subtype_Bounds;
7798 -- Start of processing for Get_Simple_Init_Val
7800 begin
7801 -- For a private type, we should always have an underlying type (because
7802 -- this was already checked in Needs_Simple_Initialization). What we do
7803 -- is to get the value for the underlying type and then do an unchecked
7804 -- conversion to the private type.
7806 if Is_Private_Type (T) then
7807 Val := Get_Simple_Init_Val (Underlying_Type (T), N, Size);
7809 -- A special case, if the underlying value is null, then qualify it
7810 -- with the underlying type, so that the null is properly typed.
7811 -- Similarly, if it is an aggregate it must be qualified, because an
7812 -- unchecked conversion does not provide a context for it.
7814 if Nkind_In (Val, N_Null, N_Aggregate) then
7815 Val :=
7816 Make_Qualified_Expression (Loc,
7817 Subtype_Mark =>
7818 New_Occurrence_Of (Underlying_Type (T), Loc),
7819 Expression => Val);
7820 end if;
7822 Result := Unchecked_Convert_To (T, Val);
7824 -- Don't truncate result (important for Initialize/Normalize_Scalars)
7826 if Nkind (Result) = N_Unchecked_Type_Conversion
7827 and then Is_Scalar_Type (Underlying_Type (T))
7828 then
7829 Set_No_Truncation (Result);
7830 end if;
7832 return Result;
7834 -- Scalars with Default_Value aspect. The first subtype may now be
7835 -- private, so retrieve value from underlying type.
7837 elsif Is_Scalar_Type (T) and then Has_Default_Aspect (T) then
7838 if Is_Private_Type (First_Subtype (T)) then
7839 return Unchecked_Convert_To (T,
7840 Default_Aspect_Value (Full_View (First_Subtype (T))));
7841 else
7842 return
7843 Convert_To (T, Default_Aspect_Value (First_Subtype (T)));
7844 end if;
7846 -- Otherwise, for scalars, we must have normalize/initialize scalars
7847 -- case, or if the node N is an 'Invalid_Value attribute node.
7849 elsif Is_Scalar_Type (T) then
7850 pragma Assert (Init_Or_Norm_Scalars or IV_Attribute);
7852 -- Compute size of object. If it is given by the caller, we can use
7853 -- it directly, otherwise we use Esize (T) as an estimate. As far as
7854 -- we know this covers all cases correctly.
7856 if Size = No_Uint or else Size <= Uint_0 then
7857 Size_To_Use := UI_Max (Uint_1, Esize (T));
7858 else
7859 Size_To_Use := Size;
7860 end if;
7862 -- Maximum size to use is 64 bits, since we will create values of
7863 -- type Unsigned_64 and the range must fit this type.
7865 if Size_To_Use /= No_Uint and then Size_To_Use > Uint_64 then
7866 Size_To_Use := Uint_64;
7867 end if;
7869 -- Check known bounds of subtype
7871 Check_Subtype_Bounds;
7873 -- Processing for Normalize_Scalars case
7875 if Normalize_Scalars and then not IV_Attribute then
7877 -- If zero is invalid, it is a convenient value to use that is
7878 -- for sure an appropriate invalid value in all situations.
7880 if Lo_Bound /= No_Uint and then Lo_Bound > Uint_0 then
7881 Val := Make_Integer_Literal (Loc, 0);
7883 -- Cases where all one bits is the appropriate invalid value
7885 -- For modular types, all 1 bits is either invalid or valid. If
7886 -- it is valid, then there is nothing that can be done since there
7887 -- are no invalid values (we ruled out zero already).
7889 -- For signed integer types that have no negative values, either
7890 -- there is room for negative values, or there is not. If there
7891 -- is, then all 1-bits may be interpreted as minus one, which is
7892 -- certainly invalid. Alternatively it is treated as the largest
7893 -- positive value, in which case the observation for modular types
7894 -- still applies.
7896 -- For float types, all 1-bits is a NaN (not a number), which is
7897 -- certainly an appropriately invalid value.
7899 elsif Is_Unsigned_Type (T)
7900 or else Is_Floating_Point_Type (T)
7901 or else Is_Enumeration_Type (T)
7902 then
7903 Val := Make_Integer_Literal (Loc, 2 ** Size_To_Use - 1);
7905 -- Resolve as Unsigned_64, because the largest number we can
7906 -- generate is out of range of universal integer.
7908 Analyze_And_Resolve (Val, RTE (RE_Unsigned_64));
7910 -- Case of signed types
7912 else
7913 declare
7914 Signed_Size : constant Uint :=
7915 UI_Min (Uint_63, Size_To_Use - 1);
7917 begin
7918 -- Normally we like to use the most negative number. The one
7919 -- exception is when this number is in the known subtype
7920 -- range and the largest positive number is not in the known
7921 -- subtype range.
7923 -- For this exceptional case, use largest positive value
7925 if Lo_Bound /= No_Uint and then Hi_Bound /= No_Uint
7926 and then Lo_Bound <= (-(2 ** Signed_Size))
7927 and then Hi_Bound < 2 ** Signed_Size
7928 then
7929 Val := Make_Integer_Literal (Loc, 2 ** Signed_Size - 1);
7931 -- Normal case of largest negative value
7933 else
7934 Val := Make_Integer_Literal (Loc, -(2 ** Signed_Size));
7935 end if;
7936 end;
7937 end if;
7939 -- Here for Initialize_Scalars case (or Invalid_Value attribute used)
7941 else
7942 -- For float types, use float values from System.Scalar_Values
7944 if Is_Floating_Point_Type (T) then
7945 if Root_Type (T) = Standard_Short_Float then
7946 Val_RE := RE_IS_Isf;
7947 elsif Root_Type (T) = Standard_Float then
7948 Val_RE := RE_IS_Ifl;
7949 elsif Root_Type (T) = Standard_Long_Float then
7950 Val_RE := RE_IS_Ilf;
7951 else pragma Assert (Root_Type (T) = Standard_Long_Long_Float);
7952 Val_RE := RE_IS_Ill;
7953 end if;
7955 -- If zero is invalid, use zero values from System.Scalar_Values
7957 elsif Lo_Bound /= No_Uint and then Lo_Bound > Uint_0 then
7958 if Size_To_Use <= 8 then
7959 Val_RE := RE_IS_Iz1;
7960 elsif Size_To_Use <= 16 then
7961 Val_RE := RE_IS_Iz2;
7962 elsif Size_To_Use <= 32 then
7963 Val_RE := RE_IS_Iz4;
7964 else
7965 Val_RE := RE_IS_Iz8;
7966 end if;
7968 -- For unsigned, use unsigned values from System.Scalar_Values
7970 elsif Is_Unsigned_Type (T) then
7971 if Size_To_Use <= 8 then
7972 Val_RE := RE_IS_Iu1;
7973 elsif Size_To_Use <= 16 then
7974 Val_RE := RE_IS_Iu2;
7975 elsif Size_To_Use <= 32 then
7976 Val_RE := RE_IS_Iu4;
7977 else
7978 Val_RE := RE_IS_Iu8;
7979 end if;
7981 -- For signed, use signed values from System.Scalar_Values
7983 else
7984 if Size_To_Use <= 8 then
7985 Val_RE := RE_IS_Is1;
7986 elsif Size_To_Use <= 16 then
7987 Val_RE := RE_IS_Is2;
7988 elsif Size_To_Use <= 32 then
7989 Val_RE := RE_IS_Is4;
7990 else
7991 Val_RE := RE_IS_Is8;
7992 end if;
7993 end if;
7995 Val := New_Occurrence_Of (RTE (Val_RE), Loc);
7996 end if;
7998 -- The final expression is obtained by doing an unchecked conversion
7999 -- of this result to the base type of the required subtype. Use the
8000 -- base type to prevent the unchecked conversion from chopping bits,
8001 -- and then we set Kill_Range_Check to preserve the "bad" value.
8003 Result := Unchecked_Convert_To (Base_Type (T), Val);
8005 -- Ensure result is not truncated, since we want the "bad" bits, and
8006 -- also kill range check on result.
8008 if Nkind (Result) = N_Unchecked_Type_Conversion then
8009 Set_No_Truncation (Result);
8010 Set_Kill_Range_Check (Result, True);
8011 end if;
8013 return Result;
8015 -- String or Wide_[Wide]_String (must have Initialize_Scalars set)
8017 elsif Is_Standard_String_Type (T) then
8018 pragma Assert (Init_Or_Norm_Scalars);
8020 return
8021 Make_Aggregate (Loc,
8022 Component_Associations => New_List (
8023 Make_Component_Association (Loc,
8024 Choices => New_List (
8025 Make_Others_Choice (Loc)),
8026 Expression =>
8027 Get_Simple_Init_Val
8028 (Component_Type (T), N, Esize (Root_Type (T))))));
8030 -- Access type is initialized to null
8032 elsif Is_Access_Type (T) then
8033 return Make_Null (Loc);
8035 -- No other possibilities should arise, since we should only be calling
8036 -- Get_Simple_Init_Val if Needs_Simple_Initialization returned True,
8037 -- indicating one of the above cases held.
8039 else
8040 raise Program_Error;
8041 end if;
8043 exception
8044 when RE_Not_Available =>
8045 return Empty;
8046 end Get_Simple_Init_Val;
8048 ------------------------------
8049 -- Has_New_Non_Standard_Rep --
8050 ------------------------------
8052 function Has_New_Non_Standard_Rep (T : Entity_Id) return Boolean is
8053 begin
8054 if not Is_Derived_Type (T) then
8055 return Has_Non_Standard_Rep (T)
8056 or else Has_Non_Standard_Rep (Root_Type (T));
8058 -- If Has_Non_Standard_Rep is not set on the derived type, the
8059 -- representation is fully inherited.
8061 elsif not Has_Non_Standard_Rep (T) then
8062 return False;
8064 else
8065 return First_Rep_Item (T) /= First_Rep_Item (Root_Type (T));
8067 -- May need a more precise check here: the First_Rep_Item may be a
8068 -- stream attribute, which does not affect the representation of the
8069 -- type ???
8071 end if;
8072 end Has_New_Non_Standard_Rep;
8074 ----------------
8075 -- In_Runtime --
8076 ----------------
8078 function In_Runtime (E : Entity_Id) return Boolean is
8079 S1 : Entity_Id;
8081 begin
8082 S1 := Scope (E);
8083 while Scope (S1) /= Standard_Standard loop
8084 S1 := Scope (S1);
8085 end loop;
8087 return Is_RTU (S1, System) or else Is_RTU (S1, Ada);
8088 end In_Runtime;
8090 ---------------------------------------
8091 -- Insert_Component_Invariant_Checks --
8092 ---------------------------------------
8094 procedure Insert_Component_Invariant_Checks
8095 (N : Node_Id;
8096 Typ : Entity_Id;
8097 Proc : Node_Id)
8099 Loc : constant Source_Ptr := Sloc (Typ);
8100 Proc_Id : Entity_Id;
8102 begin
8103 if Present (Proc) then
8104 Proc_Id := Defining_Entity (Proc);
8106 if not Has_Invariants (Typ) then
8107 Set_Has_Invariants (Typ);
8108 Set_Is_Invariant_Procedure (Proc_Id);
8109 Set_Invariant_Procedure (Typ, Proc_Id);
8110 Insert_After (N, Proc);
8111 Analyze (Proc);
8113 else
8115 -- Find already created invariant subprogram, insert body of
8116 -- component invariant proc in its body, and add call after
8117 -- other checks.
8119 declare
8120 Bod : Node_Id;
8121 Inv_Id : constant Entity_Id := Invariant_Procedure (Typ);
8122 Call : constant Node_Id :=
8123 Make_Procedure_Call_Statement (Sloc (N),
8124 Name => New_Occurrence_Of (Proc_Id, Loc),
8125 Parameter_Associations =>
8126 New_List
8127 (New_Occurrence_Of (First_Formal (Inv_Id), Loc)));
8129 begin
8130 -- The invariant body has not been analyzed yet, so we do a
8131 -- sequential search forward, and retrieve it by name.
8133 Bod := Next (N);
8134 while Present (Bod) loop
8135 exit when Nkind (Bod) = N_Subprogram_Body
8136 and then Chars (Defining_Entity (Bod)) = Chars (Inv_Id);
8137 Next (Bod);
8138 end loop;
8140 -- If the body is not found, it is the case of an invariant
8141 -- appearing on a full declaration in a private part, in
8142 -- which case the type has been frozen but the invariant
8143 -- procedure for the composite type not created yet. Create
8144 -- body now.
8146 if No (Bod) then
8147 Build_Invariant_Procedure (Typ, Parent (Current_Scope));
8148 Bod := Unit_Declaration_Node
8149 (Corresponding_Body (Unit_Declaration_Node (Inv_Id)));
8150 end if;
8152 Append_To (Declarations (Bod), Proc);
8153 Append_To (Statements (Handled_Statement_Sequence (Bod)), Call);
8154 Analyze (Proc);
8155 Analyze (Call);
8156 end;
8157 end if;
8158 end if;
8159 end Insert_Component_Invariant_Checks;
8161 ----------------------------
8162 -- Initialization_Warning --
8163 ----------------------------
8165 procedure Initialization_Warning (E : Entity_Id) is
8166 Warning_Needed : Boolean;
8168 begin
8169 Warning_Needed := False;
8171 if Ekind (Current_Scope) = E_Package
8172 and then Static_Elaboration_Desired (Current_Scope)
8173 then
8174 if Is_Type (E) then
8175 if Is_Record_Type (E) then
8176 if Has_Discriminants (E)
8177 or else Is_Limited_Type (E)
8178 or else Has_Non_Standard_Rep (E)
8179 then
8180 Warning_Needed := True;
8182 else
8183 -- Verify that at least one component has an initialization
8184 -- expression. No need for a warning on a type if all its
8185 -- components have no initialization.
8187 declare
8188 Comp : Entity_Id;
8190 begin
8191 Comp := First_Component (E);
8192 while Present (Comp) loop
8193 if Ekind (Comp) = E_Discriminant
8194 or else
8195 (Nkind (Parent (Comp)) = N_Component_Declaration
8196 and then Present (Expression (Parent (Comp))))
8197 then
8198 Warning_Needed := True;
8199 exit;
8200 end if;
8202 Next_Component (Comp);
8203 end loop;
8204 end;
8205 end if;
8207 if Warning_Needed then
8208 Error_Msg_N
8209 ("Objects of the type cannot be initialized statically "
8210 & "by default??", Parent (E));
8211 end if;
8212 end if;
8214 else
8215 Error_Msg_N ("Object cannot be initialized statically??", E);
8216 end if;
8217 end if;
8218 end Initialization_Warning;
8220 ------------------
8221 -- Init_Formals --
8222 ------------------
8224 function Init_Formals (Typ : Entity_Id) return List_Id is
8225 Loc : constant Source_Ptr := Sloc (Typ);
8226 Formals : List_Id;
8228 begin
8229 -- First parameter is always _Init : in out typ. Note that we need this
8230 -- to be in/out because in the case of the task record value, there
8231 -- are default record fields (_Priority, _Size, -Task_Info) that may
8232 -- be referenced in the generated initialization routine.
8234 Formals := New_List (
8235 Make_Parameter_Specification (Loc,
8236 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uInit),
8237 In_Present => True,
8238 Out_Present => True,
8239 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
8241 -- For task record value, or type that contains tasks, add two more
8242 -- formals, _Master : Master_Id and _Chain : in out Activation_Chain
8243 -- We also add these parameters for the task record type case.
8245 if Has_Task (Typ)
8246 or else (Is_Record_Type (Typ) and then Is_Task_Record_Type (Typ))
8247 then
8248 Append_To (Formals,
8249 Make_Parameter_Specification (Loc,
8250 Defining_Identifier =>
8251 Make_Defining_Identifier (Loc, Name_uMaster),
8252 Parameter_Type =>
8253 New_Occurrence_Of (RTE (RE_Master_Id), Loc)));
8255 -- Add _Chain (not done for sequential elaboration policy, see
8256 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
8258 if Partition_Elaboration_Policy /= 'S' then
8259 Append_To (Formals,
8260 Make_Parameter_Specification (Loc,
8261 Defining_Identifier =>
8262 Make_Defining_Identifier (Loc, Name_uChain),
8263 In_Present => True,
8264 Out_Present => True,
8265 Parameter_Type =>
8266 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc)));
8267 end if;
8269 Append_To (Formals,
8270 Make_Parameter_Specification (Loc,
8271 Defining_Identifier =>
8272 Make_Defining_Identifier (Loc, Name_uTask_Name),
8273 In_Present => True,
8274 Parameter_Type => New_Occurrence_Of (Standard_String, Loc)));
8275 end if;
8277 return Formals;
8279 exception
8280 when RE_Not_Available =>
8281 return Empty_List;
8282 end Init_Formals;
8284 -------------------------
8285 -- Init_Secondary_Tags --
8286 -------------------------
8288 procedure Init_Secondary_Tags
8289 (Typ : Entity_Id;
8290 Target : Node_Id;
8291 Stmts_List : List_Id;
8292 Fixed_Comps : Boolean := True;
8293 Variable_Comps : Boolean := True)
8295 Loc : constant Source_Ptr := Sloc (Target);
8297 -- Inherit the C++ tag of the secondary dispatch table of Typ associated
8298 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
8300 procedure Initialize_Tag
8301 (Typ : Entity_Id;
8302 Iface : Entity_Id;
8303 Tag_Comp : Entity_Id;
8304 Iface_Tag : Node_Id);
8305 -- Initialize the tag of the secondary dispatch table of Typ associated
8306 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
8307 -- Compiling under the CPP full ABI compatibility mode, if the ancestor
8308 -- of Typ CPP tagged type we generate code to inherit the contents of
8309 -- the dispatch table directly from the ancestor.
8311 --------------------
8312 -- Initialize_Tag --
8313 --------------------
8315 procedure Initialize_Tag
8316 (Typ : Entity_Id;
8317 Iface : Entity_Id;
8318 Tag_Comp : Entity_Id;
8319 Iface_Tag : Node_Id)
8321 Comp_Typ : Entity_Id;
8322 Offset_To_Top_Comp : Entity_Id := Empty;
8324 begin
8325 -- Initialize pointer to secondary DT associated with the interface
8327 if not Is_Ancestor (Iface, Typ, Use_Full_View => True) then
8328 Append_To (Stmts_List,
8329 Make_Assignment_Statement (Loc,
8330 Name =>
8331 Make_Selected_Component (Loc,
8332 Prefix => New_Copy_Tree (Target),
8333 Selector_Name => New_Occurrence_Of (Tag_Comp, Loc)),
8334 Expression =>
8335 New_Occurrence_Of (Iface_Tag, Loc)));
8336 end if;
8338 Comp_Typ := Scope (Tag_Comp);
8340 -- Initialize the entries of the table of interfaces. We generate a
8341 -- different call when the parent of the type has variable size
8342 -- components.
8344 if Comp_Typ /= Etype (Comp_Typ)
8345 and then Is_Variable_Size_Record (Etype (Comp_Typ))
8346 and then Chars (Tag_Comp) /= Name_uTag
8347 then
8348 pragma Assert (Present (DT_Offset_To_Top_Func (Tag_Comp)));
8350 -- Issue error if Set_Dynamic_Offset_To_Top is not available in a
8351 -- configurable run-time environment.
8353 if not RTE_Available (RE_Set_Dynamic_Offset_To_Top) then
8354 Error_Msg_CRT
8355 ("variable size record with interface types", Typ);
8356 return;
8357 end if;
8359 -- Generate:
8360 -- Set_Dynamic_Offset_To_Top
8361 -- (This => Init,
8362 -- Interface_T => Iface'Tag,
8363 -- Offset_Value => n,
8364 -- Offset_Func => Fn'Address)
8366 Append_To (Stmts_List,
8367 Make_Procedure_Call_Statement (Loc,
8368 Name =>
8369 New_Occurrence_Of (RTE (RE_Set_Dynamic_Offset_To_Top), Loc),
8370 Parameter_Associations => New_List (
8371 Make_Attribute_Reference (Loc,
8372 Prefix => New_Copy_Tree (Target),
8373 Attribute_Name => Name_Address),
8375 Unchecked_Convert_To (RTE (RE_Tag),
8376 New_Occurrence_Of
8377 (Node (First_Elmt (Access_Disp_Table (Iface))),
8378 Loc)),
8380 Unchecked_Convert_To
8381 (RTE (RE_Storage_Offset),
8382 Make_Attribute_Reference (Loc,
8383 Prefix =>
8384 Make_Selected_Component (Loc,
8385 Prefix => New_Copy_Tree (Target),
8386 Selector_Name =>
8387 New_Occurrence_Of (Tag_Comp, Loc)),
8388 Attribute_Name => Name_Position)),
8390 Unchecked_Convert_To (RTE (RE_Offset_To_Top_Function_Ptr),
8391 Make_Attribute_Reference (Loc,
8392 Prefix => New_Occurrence_Of
8393 (DT_Offset_To_Top_Func (Tag_Comp), Loc),
8394 Attribute_Name => Name_Address)))));
8396 -- In this case the next component stores the value of the offset
8397 -- to the top.
8399 Offset_To_Top_Comp := Next_Entity (Tag_Comp);
8400 pragma Assert (Present (Offset_To_Top_Comp));
8402 Append_To (Stmts_List,
8403 Make_Assignment_Statement (Loc,
8404 Name =>
8405 Make_Selected_Component (Loc,
8406 Prefix => New_Copy_Tree (Target),
8407 Selector_Name =>
8408 New_Occurrence_Of (Offset_To_Top_Comp, Loc)),
8410 Expression =>
8411 Make_Attribute_Reference (Loc,
8412 Prefix =>
8413 Make_Selected_Component (Loc,
8414 Prefix => New_Copy_Tree (Target),
8415 Selector_Name => New_Occurrence_Of (Tag_Comp, Loc)),
8416 Attribute_Name => Name_Position)));
8418 -- Normal case: No discriminants in the parent type
8420 else
8421 -- Don't need to set any value if this interface shares the
8422 -- primary dispatch table.
8424 if not Is_Ancestor (Iface, Typ, Use_Full_View => True) then
8425 Append_To (Stmts_List,
8426 Build_Set_Static_Offset_To_Top (Loc,
8427 Iface_Tag => New_Occurrence_Of (Iface_Tag, Loc),
8428 Offset_Value =>
8429 Unchecked_Convert_To (RTE (RE_Storage_Offset),
8430 Make_Attribute_Reference (Loc,
8431 Prefix =>
8432 Make_Selected_Component (Loc,
8433 Prefix => New_Copy_Tree (Target),
8434 Selector_Name =>
8435 New_Occurrence_Of (Tag_Comp, Loc)),
8436 Attribute_Name => Name_Position))));
8437 end if;
8439 -- Generate:
8440 -- Register_Interface_Offset
8441 -- (This => Init,
8442 -- Interface_T => Iface'Tag,
8443 -- Is_Constant => True,
8444 -- Offset_Value => n,
8445 -- Offset_Func => null);
8447 if RTE_Available (RE_Register_Interface_Offset) then
8448 Append_To (Stmts_List,
8449 Make_Procedure_Call_Statement (Loc,
8450 Name =>
8451 New_Occurrence_Of
8452 (RTE (RE_Register_Interface_Offset), Loc),
8453 Parameter_Associations => New_List (
8454 Make_Attribute_Reference (Loc,
8455 Prefix => New_Copy_Tree (Target),
8456 Attribute_Name => Name_Address),
8458 Unchecked_Convert_To (RTE (RE_Tag),
8459 New_Occurrence_Of
8460 (Node (First_Elmt (Access_Disp_Table (Iface))), Loc)),
8462 New_Occurrence_Of (Standard_True, Loc),
8464 Unchecked_Convert_To (RTE (RE_Storage_Offset),
8465 Make_Attribute_Reference (Loc,
8466 Prefix =>
8467 Make_Selected_Component (Loc,
8468 Prefix => New_Copy_Tree (Target),
8469 Selector_Name =>
8470 New_Occurrence_Of (Tag_Comp, Loc)),
8471 Attribute_Name => Name_Position)),
8473 Make_Null (Loc))));
8474 end if;
8475 end if;
8476 end Initialize_Tag;
8478 -- Local variables
8480 Full_Typ : Entity_Id;
8481 Ifaces_List : Elist_Id;
8482 Ifaces_Comp_List : Elist_Id;
8483 Ifaces_Tag_List : Elist_Id;
8484 Iface_Elmt : Elmt_Id;
8485 Iface_Comp_Elmt : Elmt_Id;
8486 Iface_Tag_Elmt : Elmt_Id;
8487 Tag_Comp : Node_Id;
8488 In_Variable_Pos : Boolean;
8490 -- Start of processing for Init_Secondary_Tags
8492 begin
8493 -- Handle private types
8495 if Present (Full_View (Typ)) then
8496 Full_Typ := Full_View (Typ);
8497 else
8498 Full_Typ := Typ;
8499 end if;
8501 Collect_Interfaces_Info
8502 (Full_Typ, Ifaces_List, Ifaces_Comp_List, Ifaces_Tag_List);
8504 Iface_Elmt := First_Elmt (Ifaces_List);
8505 Iface_Comp_Elmt := First_Elmt (Ifaces_Comp_List);
8506 Iface_Tag_Elmt := First_Elmt (Ifaces_Tag_List);
8507 while Present (Iface_Elmt) loop
8508 Tag_Comp := Node (Iface_Comp_Elmt);
8510 -- Check if parent of record type has variable size components
8512 In_Variable_Pos := Scope (Tag_Comp) /= Etype (Scope (Tag_Comp))
8513 and then Is_Variable_Size_Record (Etype (Scope (Tag_Comp)));
8515 -- If we are compiling under the CPP full ABI compatibility mode and
8516 -- the ancestor is a CPP_Pragma tagged type then we generate code to
8517 -- initialize the secondary tag components from tags that reference
8518 -- secondary tables filled with copy of parent slots.
8520 if Is_CPP_Class (Root_Type (Full_Typ)) then
8522 -- Reject interface components located at variable offset in
8523 -- C++ derivations. This is currently unsupported.
8525 if not Fixed_Comps and then In_Variable_Pos then
8527 -- Locate the first dynamic component of the record. Done to
8528 -- improve the text of the warning.
8530 declare
8531 Comp : Entity_Id;
8532 Comp_Typ : Entity_Id;
8534 begin
8535 Comp := First_Entity (Typ);
8536 while Present (Comp) loop
8537 Comp_Typ := Etype (Comp);
8539 if Ekind (Comp) /= E_Discriminant
8540 and then not Is_Tag (Comp)
8541 then
8542 exit when
8543 (Is_Record_Type (Comp_Typ)
8544 and then
8545 Is_Variable_Size_Record (Base_Type (Comp_Typ)))
8546 or else
8547 (Is_Array_Type (Comp_Typ)
8548 and then Is_Variable_Size_Array (Comp_Typ));
8549 end if;
8551 Next_Entity (Comp);
8552 end loop;
8554 pragma Assert (Present (Comp));
8555 Error_Msg_Node_2 := Comp;
8556 Error_Msg_NE
8557 ("parent type & with dynamic component & cannot be parent"
8558 & " of 'C'P'P derivation if new interfaces are present",
8559 Typ, Scope (Original_Record_Component (Comp)));
8561 Error_Msg_Sloc :=
8562 Sloc (Scope (Original_Record_Component (Comp)));
8563 Error_Msg_NE
8564 ("type derived from 'C'P'P type & defined #",
8565 Typ, Scope (Original_Record_Component (Comp)));
8567 -- Avoid duplicated warnings
8569 exit;
8570 end;
8572 -- Initialize secondary tags
8574 else
8575 Append_To (Stmts_List,
8576 Make_Assignment_Statement (Loc,
8577 Name =>
8578 Make_Selected_Component (Loc,
8579 Prefix => New_Copy_Tree (Target),
8580 Selector_Name =>
8581 New_Occurrence_Of (Node (Iface_Comp_Elmt), Loc)),
8582 Expression =>
8583 New_Occurrence_Of (Node (Iface_Tag_Elmt), Loc)));
8584 end if;
8586 -- Otherwise generate code to initialize the tag
8588 else
8589 if (In_Variable_Pos and then Variable_Comps)
8590 or else (not In_Variable_Pos and then Fixed_Comps)
8591 then
8592 Initialize_Tag (Full_Typ,
8593 Iface => Node (Iface_Elmt),
8594 Tag_Comp => Tag_Comp,
8595 Iface_Tag => Node (Iface_Tag_Elmt));
8596 end if;
8597 end if;
8599 Next_Elmt (Iface_Elmt);
8600 Next_Elmt (Iface_Comp_Elmt);
8601 Next_Elmt (Iface_Tag_Elmt);
8602 end loop;
8603 end Init_Secondary_Tags;
8605 ------------------------
8606 -- Is_User_Defined_Eq --
8607 ------------------------
8609 function Is_User_Defined_Equality (Prim : Node_Id) return Boolean is
8610 begin
8611 return Chars (Prim) = Name_Op_Eq
8612 and then Etype (First_Formal (Prim)) =
8613 Etype (Next_Formal (First_Formal (Prim)))
8614 and then Base_Type (Etype (Prim)) = Standard_Boolean;
8615 end Is_User_Defined_Equality;
8617 ----------------------------------------
8618 -- Make_Controlling_Function_Wrappers --
8619 ----------------------------------------
8621 procedure Make_Controlling_Function_Wrappers
8622 (Tag_Typ : Entity_Id;
8623 Decl_List : out List_Id;
8624 Body_List : out List_Id)
8626 Loc : constant Source_Ptr := Sloc (Tag_Typ);
8627 Prim_Elmt : Elmt_Id;
8628 Subp : Entity_Id;
8629 Actual_List : List_Id;
8630 Formal_List : List_Id;
8631 Formal : Entity_Id;
8632 Par_Formal : Entity_Id;
8633 Formal_Node : Node_Id;
8634 Func_Body : Node_Id;
8635 Func_Decl : Node_Id;
8636 Func_Spec : Node_Id;
8637 Return_Stmt : Node_Id;
8639 begin
8640 Decl_List := New_List;
8641 Body_List := New_List;
8643 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
8645 while Present (Prim_Elmt) loop
8646 Subp := Node (Prim_Elmt);
8648 -- If a primitive function with a controlling result of the type has
8649 -- not been overridden by the user, then we must create a wrapper
8650 -- function here that effectively overrides it and invokes the
8651 -- (non-abstract) parent function. This can only occur for a null
8652 -- extension. Note that functions with anonymous controlling access
8653 -- results don't qualify and must be overridden. We also exclude
8654 -- Input attributes, since each type will have its own version of
8655 -- Input constructed by the expander. The test for Comes_From_Source
8656 -- is needed to distinguish inherited operations from renamings
8657 -- (which also have Alias set). We exclude internal entities with
8658 -- Interface_Alias to avoid generating duplicated wrappers since
8659 -- the primitive which covers the interface is also available in
8660 -- the list of primitive operations.
8662 -- The function may be abstract, or require_Overriding may be set
8663 -- for it, because tests for null extensions may already have reset
8664 -- the Is_Abstract_Subprogram_Flag. If Requires_Overriding is not
8665 -- set, functions that need wrappers are recognized by having an
8666 -- alias that returns the parent type.
8668 if Comes_From_Source (Subp)
8669 or else No (Alias (Subp))
8670 or else Present (Interface_Alias (Subp))
8671 or else Ekind (Subp) /= E_Function
8672 or else not Has_Controlling_Result (Subp)
8673 or else Is_Access_Type (Etype (Subp))
8674 or else Is_Abstract_Subprogram (Alias (Subp))
8675 or else Is_TSS (Subp, TSS_Stream_Input)
8676 then
8677 goto Next_Prim;
8679 elsif Is_Abstract_Subprogram (Subp)
8680 or else Requires_Overriding (Subp)
8681 or else
8682 (Is_Null_Extension (Etype (Subp))
8683 and then Etype (Alias (Subp)) /= Etype (Subp))
8684 then
8685 Formal_List := No_List;
8686 Formal := First_Formal (Subp);
8688 if Present (Formal) then
8689 Formal_List := New_List;
8691 while Present (Formal) loop
8692 Append
8693 (Make_Parameter_Specification
8694 (Loc,
8695 Defining_Identifier =>
8696 Make_Defining_Identifier (Sloc (Formal),
8697 Chars => Chars (Formal)),
8698 In_Present => In_Present (Parent (Formal)),
8699 Out_Present => Out_Present (Parent (Formal)),
8700 Null_Exclusion_Present =>
8701 Null_Exclusion_Present (Parent (Formal)),
8702 Parameter_Type =>
8703 New_Occurrence_Of (Etype (Formal), Loc),
8704 Expression =>
8705 New_Copy_Tree (Expression (Parent (Formal)))),
8706 Formal_List);
8708 Next_Formal (Formal);
8709 end loop;
8710 end if;
8712 Func_Spec :=
8713 Make_Function_Specification (Loc,
8714 Defining_Unit_Name =>
8715 Make_Defining_Identifier (Loc,
8716 Chars => Chars (Subp)),
8717 Parameter_Specifications => Formal_List,
8718 Result_Definition =>
8719 New_Occurrence_Of (Etype (Subp), Loc));
8721 Func_Decl := Make_Subprogram_Declaration (Loc, Func_Spec);
8722 Append_To (Decl_List, Func_Decl);
8724 -- Build a wrapper body that calls the parent function. The body
8725 -- contains a single return statement that returns an extension
8726 -- aggregate whose ancestor part is a call to the parent function,
8727 -- passing the formals as actuals (with any controlling arguments
8728 -- converted to the types of the corresponding formals of the
8729 -- parent function, which might be anonymous access types), and
8730 -- having a null extension.
8732 Formal := First_Formal (Subp);
8733 Par_Formal := First_Formal (Alias (Subp));
8734 Formal_Node := First (Formal_List);
8736 if Present (Formal) then
8737 Actual_List := New_List;
8738 else
8739 Actual_List := No_List;
8740 end if;
8742 while Present (Formal) loop
8743 if Is_Controlling_Formal (Formal) then
8744 Append_To (Actual_List,
8745 Make_Type_Conversion (Loc,
8746 Subtype_Mark =>
8747 New_Occurrence_Of (Etype (Par_Formal), Loc),
8748 Expression =>
8749 New_Occurrence_Of
8750 (Defining_Identifier (Formal_Node), Loc)));
8751 else
8752 Append_To
8753 (Actual_List,
8754 New_Occurrence_Of
8755 (Defining_Identifier (Formal_Node), Loc));
8756 end if;
8758 Next_Formal (Formal);
8759 Next_Formal (Par_Formal);
8760 Next (Formal_Node);
8761 end loop;
8763 Return_Stmt :=
8764 Make_Simple_Return_Statement (Loc,
8765 Expression =>
8766 Make_Extension_Aggregate (Loc,
8767 Ancestor_Part =>
8768 Make_Function_Call (Loc,
8769 Name =>
8770 New_Occurrence_Of (Alias (Subp), Loc),
8771 Parameter_Associations => Actual_List),
8772 Null_Record_Present => True));
8774 Func_Body :=
8775 Make_Subprogram_Body (Loc,
8776 Specification => New_Copy_Tree (Func_Spec),
8777 Declarations => Empty_List,
8778 Handled_Statement_Sequence =>
8779 Make_Handled_Sequence_Of_Statements (Loc,
8780 Statements => New_List (Return_Stmt)));
8782 Set_Defining_Unit_Name
8783 (Specification (Func_Body),
8784 Make_Defining_Identifier (Loc, Chars (Subp)));
8786 Append_To (Body_List, Func_Body);
8788 -- Replace the inherited function with the wrapper function in the
8789 -- primitive operations list. We add the minimum decoration needed
8790 -- to override interface primitives.
8792 Set_Ekind (Defining_Unit_Name (Func_Spec), E_Function);
8794 Override_Dispatching_Operation
8795 (Tag_Typ, Subp, New_Op => Defining_Unit_Name (Func_Spec),
8796 Is_Wrapper => True);
8797 end if;
8799 <<Next_Prim>>
8800 Next_Elmt (Prim_Elmt);
8801 end loop;
8802 end Make_Controlling_Function_Wrappers;
8804 -------------------
8805 -- Make_Eq_Body --
8806 -------------------
8808 function Make_Eq_Body
8809 (Typ : Entity_Id;
8810 Eq_Name : Name_Id) return Node_Id
8812 Loc : constant Source_Ptr := Sloc (Parent (Typ));
8813 Decl : Node_Id;
8814 Def : constant Node_Id := Parent (Typ);
8815 Stmts : constant List_Id := New_List;
8816 Variant_Case : Boolean := Has_Discriminants (Typ);
8817 Comps : Node_Id := Empty;
8818 Typ_Def : Node_Id := Type_Definition (Def);
8820 begin
8821 Decl :=
8822 Predef_Spec_Or_Body (Loc,
8823 Tag_Typ => Typ,
8824 Name => Eq_Name,
8825 Profile => New_List (
8826 Make_Parameter_Specification (Loc,
8827 Defining_Identifier =>
8828 Make_Defining_Identifier (Loc, Name_X),
8829 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
8831 Make_Parameter_Specification (Loc,
8832 Defining_Identifier =>
8833 Make_Defining_Identifier (Loc, Name_Y),
8834 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8836 Ret_Type => Standard_Boolean,
8837 For_Body => True);
8839 if Variant_Case then
8840 if Nkind (Typ_Def) = N_Derived_Type_Definition then
8841 Typ_Def := Record_Extension_Part (Typ_Def);
8842 end if;
8844 if Present (Typ_Def) then
8845 Comps := Component_List (Typ_Def);
8846 end if;
8848 Variant_Case :=
8849 Present (Comps) and then Present (Variant_Part (Comps));
8850 end if;
8852 if Variant_Case then
8853 Append_To (Stmts,
8854 Make_Eq_If (Typ, Discriminant_Specifications (Def)));
8855 Append_List_To (Stmts, Make_Eq_Case (Typ, Comps));
8856 Append_To (Stmts,
8857 Make_Simple_Return_Statement (Loc,
8858 Expression => New_Occurrence_Of (Standard_True, Loc)));
8860 else
8861 Append_To (Stmts,
8862 Make_Simple_Return_Statement (Loc,
8863 Expression =>
8864 Expand_Record_Equality
8865 (Typ,
8866 Typ => Typ,
8867 Lhs => Make_Identifier (Loc, Name_X),
8868 Rhs => Make_Identifier (Loc, Name_Y),
8869 Bodies => Declarations (Decl))));
8870 end if;
8872 Set_Handled_Statement_Sequence
8873 (Decl, Make_Handled_Sequence_Of_Statements (Loc, Stmts));
8874 return Decl;
8875 end Make_Eq_Body;
8877 ------------------
8878 -- Make_Eq_Case --
8879 ------------------
8881 -- <Make_Eq_If shared components>
8883 -- case X.D1 is
8884 -- when V1 => <Make_Eq_Case> on subcomponents
8885 -- ...
8886 -- when Vn => <Make_Eq_Case> on subcomponents
8887 -- end case;
8889 function Make_Eq_Case
8890 (E : Entity_Id;
8891 CL : Node_Id;
8892 Discrs : Elist_Id := New_Elmt_List) return List_Id
8894 Loc : constant Source_Ptr := Sloc (E);
8895 Result : constant List_Id := New_List;
8896 Variant : Node_Id;
8897 Alt_List : List_Id;
8899 function Corresponding_Formal (C : Node_Id) return Entity_Id;
8900 -- Given the discriminant that controls a given variant of an unchecked
8901 -- union, find the formal of the equality function that carries the
8902 -- inferred value of the discriminant.
8904 function External_Name (E : Entity_Id) return Name_Id;
8905 -- The value of a given discriminant is conveyed in the corresponding
8906 -- formal parameter of the equality routine. The name of this formal
8907 -- parameter carries a one-character suffix which is removed here.
8909 --------------------------
8910 -- Corresponding_Formal --
8911 --------------------------
8913 function Corresponding_Formal (C : Node_Id) return Entity_Id is
8914 Discr : constant Entity_Id := Entity (Name (Variant_Part (C)));
8915 Elm : Elmt_Id;
8917 begin
8918 Elm := First_Elmt (Discrs);
8919 while Present (Elm) loop
8920 if Chars (Discr) = External_Name (Node (Elm)) then
8921 return Node (Elm);
8922 end if;
8924 Next_Elmt (Elm);
8925 end loop;
8927 -- A formal of the proper name must be found
8929 raise Program_Error;
8930 end Corresponding_Formal;
8932 -------------------
8933 -- External_Name --
8934 -------------------
8936 function External_Name (E : Entity_Id) return Name_Id is
8937 begin
8938 Get_Name_String (Chars (E));
8939 Name_Len := Name_Len - 1;
8940 return Name_Find;
8941 end External_Name;
8943 -- Start of processing for Make_Eq_Case
8945 begin
8946 Append_To (Result, Make_Eq_If (E, Component_Items (CL)));
8948 if No (Variant_Part (CL)) then
8949 return Result;
8950 end if;
8952 Variant := First_Non_Pragma (Variants (Variant_Part (CL)));
8954 if No (Variant) then
8955 return Result;
8956 end if;
8958 Alt_List := New_List;
8959 while Present (Variant) loop
8960 Append_To (Alt_List,
8961 Make_Case_Statement_Alternative (Loc,
8962 Discrete_Choices => New_Copy_List (Discrete_Choices (Variant)),
8963 Statements =>
8964 Make_Eq_Case (E, Component_List (Variant), Discrs)));
8965 Next_Non_Pragma (Variant);
8966 end loop;
8968 -- If we have an Unchecked_Union, use one of the parameters of the
8969 -- enclosing equality routine that captures the discriminant, to use
8970 -- as the expression in the generated case statement.
8972 if Is_Unchecked_Union (E) then
8973 Append_To (Result,
8974 Make_Case_Statement (Loc,
8975 Expression =>
8976 New_Occurrence_Of (Corresponding_Formal (CL), Loc),
8977 Alternatives => Alt_List));
8979 else
8980 Append_To (Result,
8981 Make_Case_Statement (Loc,
8982 Expression =>
8983 Make_Selected_Component (Loc,
8984 Prefix => Make_Identifier (Loc, Name_X),
8985 Selector_Name => New_Copy (Name (Variant_Part (CL)))),
8986 Alternatives => Alt_List));
8987 end if;
8989 return Result;
8990 end Make_Eq_Case;
8992 ----------------
8993 -- Make_Eq_If --
8994 ----------------
8996 -- Generates:
8998 -- if
8999 -- X.C1 /= Y.C1
9000 -- or else
9001 -- X.C2 /= Y.C2
9002 -- ...
9003 -- then
9004 -- return False;
9005 -- end if;
9007 -- or a null statement if the list L is empty
9009 function Make_Eq_If
9010 (E : Entity_Id;
9011 L : List_Id) return Node_Id
9013 Loc : constant Source_Ptr := Sloc (E);
9014 C : Node_Id;
9015 Field_Name : Name_Id;
9016 Cond : Node_Id;
9018 begin
9019 if No (L) then
9020 return Make_Null_Statement (Loc);
9022 else
9023 Cond := Empty;
9025 C := First_Non_Pragma (L);
9026 while Present (C) loop
9027 Field_Name := Chars (Defining_Identifier (C));
9029 -- The tags must not be compared: they are not part of the value.
9030 -- Ditto for parent interfaces because their equality operator is
9031 -- abstract.
9033 -- Note also that in the following, we use Make_Identifier for
9034 -- the component names. Use of New_Occurrence_Of to identify the
9035 -- components would be incorrect because the wrong entities for
9036 -- discriminants could be picked up in the private type case.
9038 if Field_Name = Name_uParent
9039 and then Is_Interface (Etype (Defining_Identifier (C)))
9040 then
9041 null;
9043 elsif Field_Name /= Name_uTag then
9044 Evolve_Or_Else (Cond,
9045 Make_Op_Ne (Loc,
9046 Left_Opnd =>
9047 Make_Selected_Component (Loc,
9048 Prefix => Make_Identifier (Loc, Name_X),
9049 Selector_Name => Make_Identifier (Loc, Field_Name)),
9051 Right_Opnd =>
9052 Make_Selected_Component (Loc,
9053 Prefix => Make_Identifier (Loc, Name_Y),
9054 Selector_Name => Make_Identifier (Loc, Field_Name))));
9055 end if;
9057 Next_Non_Pragma (C);
9058 end loop;
9060 if No (Cond) then
9061 return Make_Null_Statement (Loc);
9063 else
9064 return
9065 Make_Implicit_If_Statement (E,
9066 Condition => Cond,
9067 Then_Statements => New_List (
9068 Make_Simple_Return_Statement (Loc,
9069 Expression => New_Occurrence_Of (Standard_False, Loc))));
9070 end if;
9071 end if;
9072 end Make_Eq_If;
9074 -------------------
9075 -- Make_Neq_Body --
9076 -------------------
9078 function Make_Neq_Body (Tag_Typ : Entity_Id) return Node_Id is
9080 function Is_Predefined_Neq_Renaming (Prim : Node_Id) return Boolean;
9081 -- Returns true if Prim is a renaming of an unresolved predefined
9082 -- inequality operation.
9084 --------------------------------
9085 -- Is_Predefined_Neq_Renaming --
9086 --------------------------------
9088 function Is_Predefined_Neq_Renaming (Prim : Node_Id) return Boolean is
9089 begin
9090 return Chars (Prim) /= Name_Op_Ne
9091 and then Present (Alias (Prim))
9092 and then Comes_From_Source (Prim)
9093 and then Is_Intrinsic_Subprogram (Alias (Prim))
9094 and then Chars (Alias (Prim)) = Name_Op_Ne;
9095 end Is_Predefined_Neq_Renaming;
9097 -- Local variables
9099 Loc : constant Source_Ptr := Sloc (Parent (Tag_Typ));
9100 Stmts : constant List_Id := New_List;
9101 Decl : Node_Id;
9102 Eq_Prim : Entity_Id;
9103 Left_Op : Entity_Id;
9104 Renaming_Prim : Entity_Id;
9105 Right_Op : Entity_Id;
9106 Target : Entity_Id;
9108 -- Start of processing for Make_Neq_Body
9110 begin
9111 -- For a call on a renaming of a dispatching subprogram that is
9112 -- overridden, if the overriding occurred before the renaming, then
9113 -- the body executed is that of the overriding declaration, even if the
9114 -- overriding declaration is not visible at the place of the renaming;
9115 -- otherwise, the inherited or predefined subprogram is called, see
9116 -- (RM 8.5.4(8))
9118 -- Stage 1: Search for a renaming of the inequality primitive and also
9119 -- search for an overriding of the equality primitive located before the
9120 -- renaming declaration.
9122 declare
9123 Elmt : Elmt_Id;
9124 Prim : Node_Id;
9126 begin
9127 Eq_Prim := Empty;
9128 Renaming_Prim := Empty;
9130 Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
9131 while Present (Elmt) loop
9132 Prim := Node (Elmt);
9134 if Is_User_Defined_Equality (Prim) and then No (Alias (Prim)) then
9135 if No (Renaming_Prim) then
9136 pragma Assert (No (Eq_Prim));
9137 Eq_Prim := Prim;
9138 end if;
9140 elsif Is_Predefined_Neq_Renaming (Prim) then
9141 Renaming_Prim := Prim;
9142 end if;
9144 Next_Elmt (Elmt);
9145 end loop;
9146 end;
9148 -- No further action needed if no renaming was found
9150 if No (Renaming_Prim) then
9151 return Empty;
9152 end if;
9154 -- Stage 2: Replace the renaming declaration by a subprogram declaration
9155 -- (required to add its body)
9157 Decl := Parent (Parent (Renaming_Prim));
9158 Rewrite (Decl,
9159 Make_Subprogram_Declaration (Loc,
9160 Specification => Specification (Decl)));
9161 Set_Analyzed (Decl);
9163 -- Remove the decoration of intrinsic renaming subprogram
9165 Set_Is_Intrinsic_Subprogram (Renaming_Prim, False);
9166 Set_Convention (Renaming_Prim, Convention_Ada);
9167 Set_Alias (Renaming_Prim, Empty);
9168 Set_Has_Completion (Renaming_Prim, False);
9170 -- Stage 3: Build the corresponding body
9172 Left_Op := First_Formal (Renaming_Prim);
9173 Right_Op := Next_Formal (Left_Op);
9175 Decl :=
9176 Predef_Spec_Or_Body (Loc,
9177 Tag_Typ => Tag_Typ,
9178 Name => Chars (Renaming_Prim),
9179 Profile => New_List (
9180 Make_Parameter_Specification (Loc,
9181 Defining_Identifier =>
9182 Make_Defining_Identifier (Loc, Chars (Left_Op)),
9183 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
9185 Make_Parameter_Specification (Loc,
9186 Defining_Identifier =>
9187 Make_Defining_Identifier (Loc, Chars (Right_Op)),
9188 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
9190 Ret_Type => Standard_Boolean,
9191 For_Body => True);
9193 -- If the overriding of the equality primitive occurred before the
9194 -- renaming, then generate:
9196 -- function <Neq_Name> (X : Y : Typ) return Boolean is
9197 -- begin
9198 -- return not Oeq (X, Y);
9199 -- end;
9201 if Present (Eq_Prim) then
9202 Target := Eq_Prim;
9204 -- Otherwise build a nested subprogram which performs the predefined
9205 -- evaluation of the equality operator. That is, generate:
9207 -- function <Neq_Name> (X : Y : Typ) return Boolean is
9208 -- function Oeq (X : Y) return Boolean is
9209 -- begin
9210 -- <<body of default implementation>>
9211 -- end;
9212 -- begin
9213 -- return not Oeq (X, Y);
9214 -- end;
9216 else
9217 declare
9218 Local_Subp : Node_Id;
9219 begin
9220 Local_Subp := Make_Eq_Body (Tag_Typ, Name_Op_Eq);
9221 Set_Declarations (Decl, New_List (Local_Subp));
9222 Target := Defining_Entity (Local_Subp);
9223 end;
9224 end if;
9226 Append_To (Stmts,
9227 Make_Simple_Return_Statement (Loc,
9228 Expression =>
9229 Make_Op_Not (Loc,
9230 Make_Function_Call (Loc,
9231 Name => New_Occurrence_Of (Target, Loc),
9232 Parameter_Associations => New_List (
9233 Make_Identifier (Loc, Chars (Left_Op)),
9234 Make_Identifier (Loc, Chars (Right_Op)))))));
9236 Set_Handled_Statement_Sequence
9237 (Decl, Make_Handled_Sequence_Of_Statements (Loc, Stmts));
9238 return Decl;
9239 end Make_Neq_Body;
9241 -------------------------------
9242 -- Make_Null_Procedure_Specs --
9243 -------------------------------
9245 function Make_Null_Procedure_Specs (Tag_Typ : Entity_Id) return List_Id is
9246 Decl_List : constant List_Id := New_List;
9247 Loc : constant Source_Ptr := Sloc (Tag_Typ);
9248 Formal : Entity_Id;
9249 Formal_List : List_Id;
9250 New_Param_Spec : Node_Id;
9251 Parent_Subp : Entity_Id;
9252 Prim_Elmt : Elmt_Id;
9253 Subp : Entity_Id;
9255 begin
9256 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
9257 while Present (Prim_Elmt) loop
9258 Subp := Node (Prim_Elmt);
9260 -- If a null procedure inherited from an interface has not been
9261 -- overridden, then we build a null procedure declaration to
9262 -- override the inherited procedure.
9264 Parent_Subp := Alias (Subp);
9266 if Present (Parent_Subp)
9267 and then Is_Null_Interface_Primitive (Parent_Subp)
9268 then
9269 Formal_List := No_List;
9270 Formal := First_Formal (Subp);
9272 if Present (Formal) then
9273 Formal_List := New_List;
9275 while Present (Formal) loop
9277 -- Copy the parameter spec including default expressions
9279 New_Param_Spec :=
9280 New_Copy_Tree (Parent (Formal), New_Sloc => Loc);
9282 -- Generate a new defining identifier for the new formal.
9283 -- required because New_Copy_Tree does not duplicate
9284 -- semantic fields (except itypes).
9286 Set_Defining_Identifier (New_Param_Spec,
9287 Make_Defining_Identifier (Sloc (Formal),
9288 Chars => Chars (Formal)));
9290 -- For controlling arguments we must change their
9291 -- parameter type to reference the tagged type (instead
9292 -- of the interface type)
9294 if Is_Controlling_Formal (Formal) then
9295 if Nkind (Parameter_Type (Parent (Formal))) = N_Identifier
9296 then
9297 Set_Parameter_Type (New_Param_Spec,
9298 New_Occurrence_Of (Tag_Typ, Loc));
9300 else pragma Assert
9301 (Nkind (Parameter_Type (Parent (Formal))) =
9302 N_Access_Definition);
9303 Set_Subtype_Mark (Parameter_Type (New_Param_Spec),
9304 New_Occurrence_Of (Tag_Typ, Loc));
9305 end if;
9306 end if;
9308 Append (New_Param_Spec, Formal_List);
9310 Next_Formal (Formal);
9311 end loop;
9312 end if;
9314 Append_To (Decl_List,
9315 Make_Subprogram_Declaration (Loc,
9316 Make_Procedure_Specification (Loc,
9317 Defining_Unit_Name =>
9318 Make_Defining_Identifier (Loc, Chars (Subp)),
9319 Parameter_Specifications => Formal_List,
9320 Null_Present => True)));
9321 end if;
9323 Next_Elmt (Prim_Elmt);
9324 end loop;
9326 return Decl_List;
9327 end Make_Null_Procedure_Specs;
9329 -------------------------------------
9330 -- Make_Predefined_Primitive_Specs --
9331 -------------------------------------
9333 procedure Make_Predefined_Primitive_Specs
9334 (Tag_Typ : Entity_Id;
9335 Predef_List : out List_Id;
9336 Renamed_Eq : out Entity_Id)
9338 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean;
9339 -- Returns true if Prim is a renaming of an unresolved predefined
9340 -- equality operation.
9342 -------------------------------
9343 -- Is_Predefined_Eq_Renaming --
9344 -------------------------------
9346 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean is
9347 begin
9348 return Chars (Prim) /= Name_Op_Eq
9349 and then Present (Alias (Prim))
9350 and then Comes_From_Source (Prim)
9351 and then Is_Intrinsic_Subprogram (Alias (Prim))
9352 and then Chars (Alias (Prim)) = Name_Op_Eq;
9353 end Is_Predefined_Eq_Renaming;
9355 -- Local variables
9357 Loc : constant Source_Ptr := Sloc (Tag_Typ);
9358 Res : constant List_Id := New_List;
9359 Eq_Name : Name_Id := Name_Op_Eq;
9360 Eq_Needed : Boolean;
9361 Eq_Spec : Node_Id;
9362 Prim : Elmt_Id;
9364 Has_Predef_Eq_Renaming : Boolean := False;
9365 -- Set to True if Tag_Typ has a primitive that renames the predefined
9366 -- equality operator. Used to implement (RM 8-5-4(8)).
9368 -- Start of processing for Make_Predefined_Primitive_Specs
9370 begin
9371 Renamed_Eq := Empty;
9373 -- Spec of _Size
9375 Append_To (Res, Predef_Spec_Or_Body (Loc,
9376 Tag_Typ => Tag_Typ,
9377 Name => Name_uSize,
9378 Profile => New_List (
9379 Make_Parameter_Specification (Loc,
9380 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
9381 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
9383 Ret_Type => Standard_Long_Long_Integer));
9385 -- Specs for dispatching stream attributes
9387 declare
9388 Stream_Op_TSS_Names :
9389 constant array (Integer range <>) of TSS_Name_Type :=
9390 (TSS_Stream_Read,
9391 TSS_Stream_Write,
9392 TSS_Stream_Input,
9393 TSS_Stream_Output);
9395 begin
9396 for Op in Stream_Op_TSS_Names'Range loop
9397 if Stream_Operation_OK (Tag_Typ, Stream_Op_TSS_Names (Op)) then
9398 Append_To (Res,
9399 Predef_Stream_Attr_Spec (Loc, Tag_Typ,
9400 Stream_Op_TSS_Names (Op)));
9401 end if;
9402 end loop;
9403 end;
9405 -- Spec of "=" is expanded if the type is not limited and if a user
9406 -- defined "=" was not already declared for the non-full view of a
9407 -- private extension
9409 if not Is_Limited_Type (Tag_Typ) then
9410 Eq_Needed := True;
9411 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
9412 while Present (Prim) loop
9414 -- If a primitive is encountered that renames the predefined
9415 -- equality operator before reaching any explicit equality
9416 -- primitive, then we still need to create a predefined equality
9417 -- function, because calls to it can occur via the renaming. A
9418 -- new name is created for the equality to avoid conflicting with
9419 -- any user-defined equality. (Note that this doesn't account for
9420 -- renamings of equality nested within subpackages???)
9422 if Is_Predefined_Eq_Renaming (Node (Prim)) then
9423 Has_Predef_Eq_Renaming := True;
9424 Eq_Name := New_External_Name (Chars (Node (Prim)), 'E');
9426 -- User-defined equality
9428 elsif Is_User_Defined_Equality (Node (Prim)) then
9429 if No (Alias (Node (Prim)))
9430 or else Nkind (Unit_Declaration_Node (Node (Prim))) =
9431 N_Subprogram_Renaming_Declaration
9432 then
9433 Eq_Needed := False;
9434 exit;
9436 -- If the parent is not an interface type and has an abstract
9437 -- equality function, the inherited equality is abstract as
9438 -- well, and no body can be created for it.
9440 elsif not Is_Interface (Etype (Tag_Typ))
9441 and then Present (Alias (Node (Prim)))
9442 and then Is_Abstract_Subprogram (Alias (Node (Prim)))
9443 then
9444 Eq_Needed := False;
9445 exit;
9447 -- If the type has an equality function corresponding with
9448 -- a primitive defined in an interface type, the inherited
9449 -- equality is abstract as well, and no body can be created
9450 -- for it.
9452 elsif Present (Alias (Node (Prim)))
9453 and then Comes_From_Source (Ultimate_Alias (Node (Prim)))
9454 and then
9455 Is_Interface
9456 (Find_Dispatching_Type (Ultimate_Alias (Node (Prim))))
9457 then
9458 Eq_Needed := False;
9459 exit;
9460 end if;
9461 end if;
9463 Next_Elmt (Prim);
9464 end loop;
9466 -- If a renaming of predefined equality was found but there was no
9467 -- user-defined equality (so Eq_Needed is still true), then set the
9468 -- name back to Name_Op_Eq. But in the case where a user-defined
9469 -- equality was located after such a renaming, then the predefined
9470 -- equality function is still needed, so Eq_Needed must be set back
9471 -- to True.
9473 if Eq_Name /= Name_Op_Eq then
9474 if Eq_Needed then
9475 Eq_Name := Name_Op_Eq;
9476 else
9477 Eq_Needed := True;
9478 end if;
9479 end if;
9481 if Eq_Needed then
9482 Eq_Spec := Predef_Spec_Or_Body (Loc,
9483 Tag_Typ => Tag_Typ,
9484 Name => Eq_Name,
9485 Profile => New_List (
9486 Make_Parameter_Specification (Loc,
9487 Defining_Identifier =>
9488 Make_Defining_Identifier (Loc, Name_X),
9489 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
9491 Make_Parameter_Specification (Loc,
9492 Defining_Identifier =>
9493 Make_Defining_Identifier (Loc, Name_Y),
9494 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
9495 Ret_Type => Standard_Boolean);
9496 Append_To (Res, Eq_Spec);
9498 if Has_Predef_Eq_Renaming then
9499 Renamed_Eq := Defining_Unit_Name (Specification (Eq_Spec));
9501 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
9502 while Present (Prim) loop
9504 -- Any renamings of equality that appeared before an
9505 -- overriding equality must be updated to refer to the
9506 -- entity for the predefined equality, otherwise calls via
9507 -- the renaming would get incorrectly resolved to call the
9508 -- user-defined equality function.
9510 if Is_Predefined_Eq_Renaming (Node (Prim)) then
9511 Set_Alias (Node (Prim), Renamed_Eq);
9513 -- Exit upon encountering a user-defined equality
9515 elsif Chars (Node (Prim)) = Name_Op_Eq
9516 and then No (Alias (Node (Prim)))
9517 then
9518 exit;
9519 end if;
9521 Next_Elmt (Prim);
9522 end loop;
9523 end if;
9524 end if;
9526 -- Spec for dispatching assignment
9528 Append_To (Res, Predef_Spec_Or_Body (Loc,
9529 Tag_Typ => Tag_Typ,
9530 Name => Name_uAssign,
9531 Profile => New_List (
9532 Make_Parameter_Specification (Loc,
9533 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
9534 Out_Present => True,
9535 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
9537 Make_Parameter_Specification (Loc,
9538 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
9539 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)))));
9540 end if;
9542 -- Ada 2005: Generate declarations for the following primitive
9543 -- operations for limited interfaces and synchronized types that
9544 -- implement a limited interface.
9546 -- Disp_Asynchronous_Select
9547 -- Disp_Conditional_Select
9548 -- Disp_Get_Prim_Op_Kind
9549 -- Disp_Get_Task_Id
9550 -- Disp_Requeue
9551 -- Disp_Timed_Select
9553 -- Disable the generation of these bodies if No_Dispatching_Calls,
9554 -- Ravenscar or ZFP is active.
9556 if Ada_Version >= Ada_2005
9557 and then not Restriction_Active (No_Dispatching_Calls)
9558 and then not Restriction_Active (No_Select_Statements)
9559 and then RTE_Available (RE_Select_Specific_Data)
9560 then
9561 -- These primitives are defined abstract in interface types
9563 if Is_Interface (Tag_Typ)
9564 and then Is_Limited_Record (Tag_Typ)
9565 then
9566 Append_To (Res,
9567 Make_Abstract_Subprogram_Declaration (Loc,
9568 Specification =>
9569 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
9571 Append_To (Res,
9572 Make_Abstract_Subprogram_Declaration (Loc,
9573 Specification =>
9574 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
9576 Append_To (Res,
9577 Make_Abstract_Subprogram_Declaration (Loc,
9578 Specification =>
9579 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
9581 Append_To (Res,
9582 Make_Abstract_Subprogram_Declaration (Loc,
9583 Specification =>
9584 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
9586 Append_To (Res,
9587 Make_Abstract_Subprogram_Declaration (Loc,
9588 Specification =>
9589 Make_Disp_Requeue_Spec (Tag_Typ)));
9591 Append_To (Res,
9592 Make_Abstract_Subprogram_Declaration (Loc,
9593 Specification =>
9594 Make_Disp_Timed_Select_Spec (Tag_Typ)));
9596 -- If ancestor is an interface type, declare non-abstract primitives
9597 -- to override the abstract primitives of the interface type.
9599 -- In VM targets we define these primitives in all root tagged types
9600 -- that are not interface types. Done because in VM targets we don't
9601 -- have secondary dispatch tables and any derivation of Tag_Typ may
9602 -- cover limited interfaces (which always have these primitives since
9603 -- they may be ancestors of synchronized interface types).
9605 elsif (not Is_Interface (Tag_Typ)
9606 and then Is_Interface (Etype (Tag_Typ))
9607 and then Is_Limited_Record (Etype (Tag_Typ)))
9608 or else
9609 (Is_Concurrent_Record_Type (Tag_Typ)
9610 and then Has_Interfaces (Tag_Typ))
9611 or else
9612 (not Tagged_Type_Expansion
9613 and then not Is_Interface (Tag_Typ)
9614 and then Tag_Typ = Root_Type (Tag_Typ))
9615 then
9616 Append_To (Res,
9617 Make_Subprogram_Declaration (Loc,
9618 Specification =>
9619 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
9621 Append_To (Res,
9622 Make_Subprogram_Declaration (Loc,
9623 Specification =>
9624 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
9626 Append_To (Res,
9627 Make_Subprogram_Declaration (Loc,
9628 Specification =>
9629 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
9631 Append_To (Res,
9632 Make_Subprogram_Declaration (Loc,
9633 Specification =>
9634 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
9636 Append_To (Res,
9637 Make_Subprogram_Declaration (Loc,
9638 Specification =>
9639 Make_Disp_Requeue_Spec (Tag_Typ)));
9641 Append_To (Res,
9642 Make_Subprogram_Declaration (Loc,
9643 Specification =>
9644 Make_Disp_Timed_Select_Spec (Tag_Typ)));
9645 end if;
9646 end if;
9648 -- All tagged types receive their own Deep_Adjust and Deep_Finalize
9649 -- regardless of whether they are controlled or may contain controlled
9650 -- components.
9652 -- Do not generate the routines if finalization is disabled
9654 if Restriction_Active (No_Finalization) then
9655 null;
9657 -- Finalization is not available for CIL value types
9659 elsif Is_Value_Type (Tag_Typ) then
9660 null;
9662 else
9663 if not Is_Limited_Type (Tag_Typ) then
9664 Append_To (Res, Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust));
9665 end if;
9667 Append_To (Res, Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize));
9668 end if;
9670 Predef_List := Res;
9671 end Make_Predefined_Primitive_Specs;
9673 ---------------------------------
9674 -- Needs_Simple_Initialization --
9675 ---------------------------------
9677 function Needs_Simple_Initialization
9678 (T : Entity_Id;
9679 Consider_IS : Boolean := True) return Boolean
9681 Consider_IS_NS : constant Boolean :=
9682 Normalize_Scalars or (Initialize_Scalars and Consider_IS);
9684 begin
9685 -- Never need initialization if it is suppressed
9687 if Initialization_Suppressed (T) then
9688 return False;
9689 end if;
9691 -- Check for private type, in which case test applies to the underlying
9692 -- type of the private type.
9694 if Is_Private_Type (T) then
9695 declare
9696 RT : constant Entity_Id := Underlying_Type (T);
9697 begin
9698 if Present (RT) then
9699 return Needs_Simple_Initialization (RT);
9700 else
9701 return False;
9702 end if;
9703 end;
9705 -- Scalar type with Default_Value aspect requires initialization
9707 elsif Is_Scalar_Type (T) and then Has_Default_Aspect (T) then
9708 return True;
9710 -- Cases needing simple initialization are access types, and, if pragma
9711 -- Normalize_Scalars or Initialize_Scalars is in effect, then all scalar
9712 -- types.
9714 elsif Is_Access_Type (T)
9715 or else (Consider_IS_NS and then (Is_Scalar_Type (T)))
9716 then
9717 return True;
9719 -- If Initialize/Normalize_Scalars is in effect, string objects also
9720 -- need initialization, unless they are created in the course of
9721 -- expanding an aggregate (since in the latter case they will be
9722 -- filled with appropriate initializing values before they are used).
9724 elsif Consider_IS_NS
9725 and then Is_Standard_String_Type (T)
9726 and then
9727 (not Is_Itype (T)
9728 or else Nkind (Associated_Node_For_Itype (T)) /= N_Aggregate)
9729 then
9730 return True;
9732 else
9733 return False;
9734 end if;
9735 end Needs_Simple_Initialization;
9737 ----------------------
9738 -- Predef_Deep_Spec --
9739 ----------------------
9741 function Predef_Deep_Spec
9742 (Loc : Source_Ptr;
9743 Tag_Typ : Entity_Id;
9744 Name : TSS_Name_Type;
9745 For_Body : Boolean := False) return Node_Id
9747 Formals : List_Id;
9749 begin
9750 -- V : in out Tag_Typ
9752 Formals := New_List (
9753 Make_Parameter_Specification (Loc,
9754 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
9755 In_Present => True,
9756 Out_Present => True,
9757 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)));
9759 -- F : Boolean := True
9761 if Name = TSS_Deep_Adjust
9762 or else Name = TSS_Deep_Finalize
9763 then
9764 Append_To (Formals,
9765 Make_Parameter_Specification (Loc,
9766 Defining_Identifier => Make_Defining_Identifier (Loc, Name_F),
9767 Parameter_Type => New_Occurrence_Of (Standard_Boolean, Loc),
9768 Expression => New_Occurrence_Of (Standard_True, Loc)));
9769 end if;
9771 return
9772 Predef_Spec_Or_Body (Loc,
9773 Name => Make_TSS_Name (Tag_Typ, Name),
9774 Tag_Typ => Tag_Typ,
9775 Profile => Formals,
9776 For_Body => For_Body);
9778 exception
9779 when RE_Not_Available =>
9780 return Empty;
9781 end Predef_Deep_Spec;
9783 -------------------------
9784 -- Predef_Spec_Or_Body --
9785 -------------------------
9787 function Predef_Spec_Or_Body
9788 (Loc : Source_Ptr;
9789 Tag_Typ : Entity_Id;
9790 Name : Name_Id;
9791 Profile : List_Id;
9792 Ret_Type : Entity_Id := Empty;
9793 For_Body : Boolean := False) return Node_Id
9795 Id : constant Entity_Id := Make_Defining_Identifier (Loc, Name);
9796 Spec : Node_Id;
9798 begin
9799 Set_Is_Public (Id, Is_Public (Tag_Typ));
9801 -- The internal flag is set to mark these declarations because they have
9802 -- specific properties. First, they are primitives even if they are not
9803 -- defined in the type scope (the freezing point is not necessarily in
9804 -- the same scope). Second, the predefined equality can be overridden by
9805 -- a user-defined equality, no body will be generated in this case.
9807 Set_Is_Internal (Id);
9809 if not Debug_Generated_Code then
9810 Set_Debug_Info_Off (Id);
9811 end if;
9813 if No (Ret_Type) then
9814 Spec :=
9815 Make_Procedure_Specification (Loc,
9816 Defining_Unit_Name => Id,
9817 Parameter_Specifications => Profile);
9818 else
9819 Spec :=
9820 Make_Function_Specification (Loc,
9821 Defining_Unit_Name => Id,
9822 Parameter_Specifications => Profile,
9823 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
9824 end if;
9826 if Is_Interface (Tag_Typ) then
9827 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
9829 -- If body case, return empty subprogram body. Note that this is ill-
9830 -- formed, because there is not even a null statement, and certainly not
9831 -- a return in the function case. The caller is expected to do surgery
9832 -- on the body to add the appropriate stuff.
9834 elsif For_Body then
9835 return Make_Subprogram_Body (Loc, Spec, Empty_List, Empty);
9837 -- For the case of an Input attribute predefined for an abstract type,
9838 -- generate an abstract specification. This will never be called, but we
9839 -- need the slot allocated in the dispatching table so that attributes
9840 -- typ'Class'Input and typ'Class'Output will work properly.
9842 elsif Is_TSS (Name, TSS_Stream_Input)
9843 and then Is_Abstract_Type (Tag_Typ)
9844 then
9845 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
9847 -- Normal spec case, where we return a subprogram declaration
9849 else
9850 return Make_Subprogram_Declaration (Loc, Spec);
9851 end if;
9852 end Predef_Spec_Or_Body;
9854 -----------------------------
9855 -- Predef_Stream_Attr_Spec --
9856 -----------------------------
9858 function Predef_Stream_Attr_Spec
9859 (Loc : Source_Ptr;
9860 Tag_Typ : Entity_Id;
9861 Name : TSS_Name_Type;
9862 For_Body : Boolean := False) return Node_Id
9864 Ret_Type : Entity_Id;
9866 begin
9867 if Name = TSS_Stream_Input then
9868 Ret_Type := Tag_Typ;
9869 else
9870 Ret_Type := Empty;
9871 end if;
9873 return
9874 Predef_Spec_Or_Body
9875 (Loc,
9876 Name => Make_TSS_Name (Tag_Typ, Name),
9877 Tag_Typ => Tag_Typ,
9878 Profile => Build_Stream_Attr_Profile (Loc, Tag_Typ, Name),
9879 Ret_Type => Ret_Type,
9880 For_Body => For_Body);
9881 end Predef_Stream_Attr_Spec;
9883 ---------------------------------
9884 -- Predefined_Primitive_Bodies --
9885 ---------------------------------
9887 function Predefined_Primitive_Bodies
9888 (Tag_Typ : Entity_Id;
9889 Renamed_Eq : Entity_Id) return List_Id
9891 Loc : constant Source_Ptr := Sloc (Tag_Typ);
9892 Res : constant List_Id := New_List;
9893 Decl : Node_Id;
9894 Prim : Elmt_Id;
9895 Eq_Needed : Boolean;
9896 Eq_Name : Name_Id;
9897 Ent : Entity_Id;
9899 pragma Warnings (Off, Ent);
9901 begin
9902 pragma Assert (not Is_Interface (Tag_Typ));
9904 -- See if we have a predefined "=" operator
9906 if Present (Renamed_Eq) then
9907 Eq_Needed := True;
9908 Eq_Name := Chars (Renamed_Eq);
9910 -- If the parent is an interface type then it has defined all the
9911 -- predefined primitives abstract and we need to check if the type
9912 -- has some user defined "=" function which matches the profile of
9913 -- the Ada predefined equality operator to avoid generating it.
9915 elsif Is_Interface (Etype (Tag_Typ)) then
9916 Eq_Needed := True;
9917 Eq_Name := Name_Op_Eq;
9919 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
9920 while Present (Prim) loop
9921 if Chars (Node (Prim)) = Name_Op_Eq
9922 and then not Is_Internal (Node (Prim))
9923 and then Present (First_Entity (Node (Prim)))
9925 -- The predefined equality primitive must have exactly two
9926 -- formals whose type is this tagged type
9928 and then Present (Last_Entity (Node (Prim)))
9929 and then Next_Entity (First_Entity (Node (Prim)))
9930 = Last_Entity (Node (Prim))
9931 and then Etype (First_Entity (Node (Prim))) = Tag_Typ
9932 and then Etype (Last_Entity (Node (Prim))) = Tag_Typ
9933 then
9934 Eq_Needed := False;
9935 Eq_Name := No_Name;
9936 exit;
9937 end if;
9939 Next_Elmt (Prim);
9940 end loop;
9942 else
9943 Eq_Needed := False;
9944 Eq_Name := No_Name;
9946 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
9947 while Present (Prim) loop
9948 if Chars (Node (Prim)) = Name_Op_Eq
9949 and then Is_Internal (Node (Prim))
9950 then
9951 Eq_Needed := True;
9952 Eq_Name := Name_Op_Eq;
9953 exit;
9954 end if;
9956 Next_Elmt (Prim);
9957 end loop;
9958 end if;
9960 -- Body of _Size
9962 Decl := Predef_Spec_Or_Body (Loc,
9963 Tag_Typ => Tag_Typ,
9964 Name => Name_uSize,
9965 Profile => New_List (
9966 Make_Parameter_Specification (Loc,
9967 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
9968 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
9970 Ret_Type => Standard_Long_Long_Integer,
9971 For_Body => True);
9973 Set_Handled_Statement_Sequence (Decl,
9974 Make_Handled_Sequence_Of_Statements (Loc, New_List (
9975 Make_Simple_Return_Statement (Loc,
9976 Expression =>
9977 Make_Attribute_Reference (Loc,
9978 Prefix => Make_Identifier (Loc, Name_X),
9979 Attribute_Name => Name_Size)))));
9981 Append_To (Res, Decl);
9983 -- Bodies for Dispatching stream IO routines. We need these only for
9984 -- non-limited types (in the limited case there is no dispatching).
9985 -- We also skip them if dispatching or finalization are not available.
9987 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Read)
9988 and then No (TSS (Tag_Typ, TSS_Stream_Read))
9989 then
9990 Build_Record_Read_Procedure (Loc, Tag_Typ, Decl, Ent);
9991 Append_To (Res, Decl);
9992 end if;
9994 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Write)
9995 and then No (TSS (Tag_Typ, TSS_Stream_Write))
9996 then
9997 Build_Record_Write_Procedure (Loc, Tag_Typ, Decl, Ent);
9998 Append_To (Res, Decl);
9999 end if;
10001 -- Skip body of _Input for the abstract case, since the corresponding
10002 -- spec is abstract (see Predef_Spec_Or_Body).
10004 if not Is_Abstract_Type (Tag_Typ)
10005 and then Stream_Operation_OK (Tag_Typ, TSS_Stream_Input)
10006 and then No (TSS (Tag_Typ, TSS_Stream_Input))
10007 then
10008 Build_Record_Or_Elementary_Input_Function
10009 (Loc, Tag_Typ, Decl, Ent);
10010 Append_To (Res, Decl);
10011 end if;
10013 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Output)
10014 and then No (TSS (Tag_Typ, TSS_Stream_Output))
10015 then
10016 Build_Record_Or_Elementary_Output_Procedure (Loc, Tag_Typ, Decl, Ent);
10017 Append_To (Res, Decl);
10018 end if;
10020 -- Ada 2005: Generate bodies for the following primitive operations for
10021 -- limited interfaces and synchronized types that implement a limited
10022 -- interface.
10024 -- disp_asynchronous_select
10025 -- disp_conditional_select
10026 -- disp_get_prim_op_kind
10027 -- disp_get_task_id
10028 -- disp_timed_select
10030 -- The interface versions will have null bodies
10032 -- Disable the generation of these bodies if No_Dispatching_Calls,
10033 -- Ravenscar or ZFP is active.
10035 -- In VM targets we define these primitives in all root tagged types
10036 -- that are not interface types. Done because in VM targets we don't
10037 -- have secondary dispatch tables and any derivation of Tag_Typ may
10038 -- cover limited interfaces (which always have these primitives since
10039 -- they may be ancestors of synchronized interface types).
10041 if Ada_Version >= Ada_2005
10042 and then not Is_Interface (Tag_Typ)
10043 and then
10044 ((Is_Interface (Etype (Tag_Typ))
10045 and then Is_Limited_Record (Etype (Tag_Typ)))
10046 or else
10047 (Is_Concurrent_Record_Type (Tag_Typ)
10048 and then Has_Interfaces (Tag_Typ))
10049 or else
10050 (not Tagged_Type_Expansion
10051 and then Tag_Typ = Root_Type (Tag_Typ)))
10052 and then not Restriction_Active (No_Dispatching_Calls)
10053 and then not Restriction_Active (No_Select_Statements)
10054 and then RTE_Available (RE_Select_Specific_Data)
10055 then
10056 Append_To (Res, Make_Disp_Asynchronous_Select_Body (Tag_Typ));
10057 Append_To (Res, Make_Disp_Conditional_Select_Body (Tag_Typ));
10058 Append_To (Res, Make_Disp_Get_Prim_Op_Kind_Body (Tag_Typ));
10059 Append_To (Res, Make_Disp_Get_Task_Id_Body (Tag_Typ));
10060 Append_To (Res, Make_Disp_Requeue_Body (Tag_Typ));
10061 Append_To (Res, Make_Disp_Timed_Select_Body (Tag_Typ));
10062 end if;
10064 if not Is_Limited_Type (Tag_Typ) and then not Is_Interface (Tag_Typ) then
10066 -- Body for equality
10068 if Eq_Needed then
10069 Decl := Make_Eq_Body (Tag_Typ, Eq_Name);
10070 Append_To (Res, Decl);
10071 end if;
10073 -- Body for inequality (if required)
10075 Decl := Make_Neq_Body (Tag_Typ);
10077 if Present (Decl) then
10078 Append_To (Res, Decl);
10079 end if;
10081 -- Body for dispatching assignment
10083 Decl :=
10084 Predef_Spec_Or_Body (Loc,
10085 Tag_Typ => Tag_Typ,
10086 Name => Name_uAssign,
10087 Profile => New_List (
10088 Make_Parameter_Specification (Loc,
10089 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
10090 Out_Present => True,
10091 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
10093 Make_Parameter_Specification (Loc,
10094 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
10095 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
10096 For_Body => True);
10098 Set_Handled_Statement_Sequence (Decl,
10099 Make_Handled_Sequence_Of_Statements (Loc, New_List (
10100 Make_Assignment_Statement (Loc,
10101 Name => Make_Identifier (Loc, Name_X),
10102 Expression => Make_Identifier (Loc, Name_Y)))));
10104 Append_To (Res, Decl);
10105 end if;
10107 -- Generate empty bodies of routines Deep_Adjust and Deep_Finalize for
10108 -- tagged types which do not contain controlled components.
10110 -- Do not generate the routines if finalization is disabled
10112 if Restriction_Active (No_Finalization) then
10113 null;
10115 elsif not Has_Controlled_Component (Tag_Typ) then
10116 if not Is_Limited_Type (Tag_Typ) then
10117 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust, True);
10119 if Is_Controlled (Tag_Typ) then
10120 Set_Handled_Statement_Sequence (Decl,
10121 Make_Handled_Sequence_Of_Statements (Loc,
10122 Statements => New_List (
10123 Make_Adjust_Call (
10124 Obj_Ref => Make_Identifier (Loc, Name_V),
10125 Typ => Tag_Typ))));
10127 else
10128 Set_Handled_Statement_Sequence (Decl,
10129 Make_Handled_Sequence_Of_Statements (Loc,
10130 Statements => New_List (
10131 Make_Null_Statement (Loc))));
10132 end if;
10134 Append_To (Res, Decl);
10135 end if;
10137 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize, True);
10139 if Is_Controlled (Tag_Typ) then
10140 Set_Handled_Statement_Sequence (Decl,
10141 Make_Handled_Sequence_Of_Statements (Loc,
10142 Statements => New_List (
10143 Make_Final_Call
10144 (Obj_Ref => Make_Identifier (Loc, Name_V),
10145 Typ => Tag_Typ))));
10147 else
10148 Set_Handled_Statement_Sequence (Decl,
10149 Make_Handled_Sequence_Of_Statements (Loc,
10150 Statements => New_List (Make_Null_Statement (Loc))));
10151 end if;
10153 Append_To (Res, Decl);
10154 end if;
10156 return Res;
10157 end Predefined_Primitive_Bodies;
10159 ---------------------------------
10160 -- Predefined_Primitive_Freeze --
10161 ---------------------------------
10163 function Predefined_Primitive_Freeze
10164 (Tag_Typ : Entity_Id) return List_Id
10166 Res : constant List_Id := New_List;
10167 Prim : Elmt_Id;
10168 Frnodes : List_Id;
10170 begin
10171 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
10172 while Present (Prim) loop
10173 if Is_Predefined_Dispatching_Operation (Node (Prim)) then
10174 Frnodes := Freeze_Entity (Node (Prim), Tag_Typ);
10176 if Present (Frnodes) then
10177 Append_List_To (Res, Frnodes);
10178 end if;
10179 end if;
10181 Next_Elmt (Prim);
10182 end loop;
10184 return Res;
10185 end Predefined_Primitive_Freeze;
10187 -------------------------
10188 -- Stream_Operation_OK --
10189 -------------------------
10191 function Stream_Operation_OK
10192 (Typ : Entity_Id;
10193 Operation : TSS_Name_Type) return Boolean
10195 Has_Predefined_Or_Specified_Stream_Attribute : Boolean := False;
10197 begin
10198 -- Special case of a limited type extension: a default implementation
10199 -- of the stream attributes Read or Write exists if that attribute
10200 -- has been specified or is available for an ancestor type; a default
10201 -- implementation of the attribute Output (resp. Input) exists if the
10202 -- attribute has been specified or Write (resp. Read) is available for
10203 -- an ancestor type. The last condition only applies under Ada 2005.
10205 if Is_Limited_Type (Typ) and then Is_Tagged_Type (Typ) then
10206 if Operation = TSS_Stream_Read then
10207 Has_Predefined_Or_Specified_Stream_Attribute :=
10208 Has_Specified_Stream_Read (Typ);
10210 elsif Operation = TSS_Stream_Write then
10211 Has_Predefined_Or_Specified_Stream_Attribute :=
10212 Has_Specified_Stream_Write (Typ);
10214 elsif Operation = TSS_Stream_Input then
10215 Has_Predefined_Or_Specified_Stream_Attribute :=
10216 Has_Specified_Stream_Input (Typ)
10217 or else
10218 (Ada_Version >= Ada_2005
10219 and then Stream_Operation_OK (Typ, TSS_Stream_Read));
10221 elsif Operation = TSS_Stream_Output then
10222 Has_Predefined_Or_Specified_Stream_Attribute :=
10223 Has_Specified_Stream_Output (Typ)
10224 or else
10225 (Ada_Version >= Ada_2005
10226 and then Stream_Operation_OK (Typ, TSS_Stream_Write));
10227 end if;
10229 -- Case of inherited TSS_Stream_Read or TSS_Stream_Write
10231 if not Has_Predefined_Or_Specified_Stream_Attribute
10232 and then Is_Derived_Type (Typ)
10233 and then (Operation = TSS_Stream_Read
10234 or else Operation = TSS_Stream_Write)
10235 then
10236 Has_Predefined_Or_Specified_Stream_Attribute :=
10237 Present
10238 (Find_Inherited_TSS (Base_Type (Etype (Typ)), Operation));
10239 end if;
10240 end if;
10242 -- If the type is not limited, or else is limited but the attribute is
10243 -- explicitly specified or is predefined for the type, then return True,
10244 -- unless other conditions prevail, such as restrictions prohibiting
10245 -- streams or dispatching operations. We also return True for limited
10246 -- interfaces, because they may be extended by nonlimited types and
10247 -- permit inheritance in this case (addresses cases where an abstract
10248 -- extension doesn't get 'Input declared, as per comments below, but
10249 -- 'Class'Input must still be allowed). Note that attempts to apply
10250 -- stream attributes to a limited interface or its class-wide type
10251 -- (or limited extensions thereof) will still get properly rejected
10252 -- by Check_Stream_Attribute.
10254 -- We exclude the Input operation from being a predefined subprogram in
10255 -- the case where the associated type is an abstract extension, because
10256 -- the attribute is not callable in that case, per 13.13.2(49/2). Also,
10257 -- we don't want an abstract version created because types derived from
10258 -- the abstract type may not even have Input available (for example if
10259 -- derived from a private view of the abstract type that doesn't have
10260 -- a visible Input), but a VM such as .NET or the Java VM can treat the
10261 -- operation as inherited anyway, and we don't want an abstract function
10262 -- to be (implicitly) inherited in that case because it can lead to a VM
10263 -- exception.
10265 -- Do not generate stream routines for type Finalization_Master because
10266 -- a master may never appear in types and therefore cannot be read or
10267 -- written.
10269 return
10270 (not Is_Limited_Type (Typ)
10271 or else Is_Interface (Typ)
10272 or else Has_Predefined_Or_Specified_Stream_Attribute)
10273 and then
10274 (Operation /= TSS_Stream_Input
10275 or else not Is_Abstract_Type (Typ)
10276 or else not Is_Derived_Type (Typ))
10277 and then not Has_Unknown_Discriminants (Typ)
10278 and then not
10279 (Is_Interface (Typ)
10280 and then
10281 (Is_Task_Interface (Typ)
10282 or else Is_Protected_Interface (Typ)
10283 or else Is_Synchronized_Interface (Typ)))
10284 and then not Restriction_Active (No_Streams)
10285 and then not Restriction_Active (No_Dispatch)
10286 and then not No_Run_Time_Mode
10287 and then RTE_Available (RE_Tag)
10288 and then No (Type_Without_Stream_Operation (Typ))
10289 and then RTE_Available (RE_Root_Stream_Type)
10290 and then not Is_RTE (Typ, RE_Finalization_Master);
10291 end Stream_Operation_OK;
10293 end Exp_Ch3;