2014-10-17 Ed Schonberg <schonberg@adacore.com>
[official-gcc.git] / gcc / ada / exp_ch3.adb
blobdd6d34e00267c92fa872e2fa69d8cf5b539002e3
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 -- The aspect is type-specific, so retrieve it from the base type.
3725 Call :=
3726 Make_Procedure_Call_Statement (Loc,
3727 Name =>
3728 New_Occurrence_Of (Invariant_Procedure (Base_Type (Typ)), Loc),
3729 Parameter_Associations => New_List (Sel_Comp));
3731 if Is_Access_Type (Etype (Comp)) then
3732 Call :=
3733 Make_If_Statement (Loc,
3734 Condition =>
3735 Make_Op_Ne (Loc,
3736 Left_Opnd => Make_Null (Loc),
3737 Right_Opnd =>
3738 Make_Selected_Component (Loc,
3739 Prefix => New_Occurrence_Of (Object_Entity, Loc),
3740 Selector_Name => New_Occurrence_Of (Comp, Loc))),
3741 Then_Statements => New_List (Call));
3742 end if;
3744 return Call;
3745 end Build_Component_Invariant_Call;
3747 ----------------------------
3748 -- Build_Invariant_Checks --
3749 ----------------------------
3751 function Build_Invariant_Checks (Comp_List : Node_Id) return List_Id is
3752 Decl : Node_Id;
3753 Id : Entity_Id;
3754 Stmts : List_Id;
3756 begin
3757 Stmts := New_List;
3758 Decl := First_Non_Pragma (Component_Items (Comp_List));
3759 while Present (Decl) loop
3760 if Nkind (Decl) = N_Component_Declaration then
3761 Id := Defining_Identifier (Decl);
3763 if Has_Invariants (Etype (Id))
3764 and then In_Open_Scopes (Scope (R_Type))
3765 then
3766 if Has_Unchecked_Union (R_Type) then
3767 Error_Msg_NE
3768 ("invariants cannot be checked on components of "
3769 & "unchecked_union type&?", Decl, R_Type);
3770 return Empty_List;
3772 else
3773 Append_To (Stmts, Build_Component_Invariant_Call (Id));
3774 end if;
3776 elsif Is_Access_Type (Etype (Id))
3777 and then not Is_Access_Constant (Etype (Id))
3778 and then Has_Invariants (Designated_Type (Etype (Id)))
3779 and then In_Open_Scopes (Scope (Designated_Type (Etype (Id))))
3780 then
3781 Append_To (Stmts, Build_Component_Invariant_Call (Id));
3782 end if;
3783 end if;
3785 Next (Decl);
3786 end loop;
3788 if Present (Variant_Part (Comp_List)) then
3789 declare
3790 Variant_Alts : constant List_Id := New_List;
3791 Var_Loc : Source_Ptr;
3792 Variant : Node_Id;
3793 Variant_Stmts : List_Id;
3795 begin
3796 Variant :=
3797 First_Non_Pragma (Variants (Variant_Part (Comp_List)));
3798 while Present (Variant) loop
3799 Variant_Stmts :=
3800 Build_Invariant_Checks (Component_List (Variant));
3801 Var_Loc := Sloc (Variant);
3802 Append_To (Variant_Alts,
3803 Make_Case_Statement_Alternative (Var_Loc,
3804 Discrete_Choices =>
3805 New_Copy_List (Discrete_Choices (Variant)),
3806 Statements => Variant_Stmts));
3808 Next_Non_Pragma (Variant);
3809 end loop;
3811 -- The expression in the case statement is the reference to
3812 -- the discriminant of the target object.
3814 Append_To (Stmts,
3815 Make_Case_Statement (Var_Loc,
3816 Expression =>
3817 Make_Selected_Component (Var_Loc,
3818 Prefix => New_Occurrence_Of (Object_Entity, Var_Loc),
3819 Selector_Name => New_Occurrence_Of
3820 (Entity
3821 (Name (Variant_Part (Comp_List))), Var_Loc)),
3822 Alternatives => Variant_Alts));
3823 end;
3824 end if;
3826 return Stmts;
3827 end Build_Invariant_Checks;
3829 -- Start of processing for Build_Record_Invariant_Proc
3831 begin
3832 Invariant_Found := False;
3833 Type_Def := Type_Definition (Parent (R_Type));
3835 if Nkind (Type_Def) = N_Record_Definition
3836 and then not Null_Present (Type_Def)
3837 then
3838 Stmts := Build_Invariant_Checks (Component_List (Type_Def));
3839 else
3840 return Empty;
3841 end if;
3843 if not Invariant_Found then
3844 return Empty;
3845 end if;
3847 -- The name of the invariant procedure reflects the fact that the
3848 -- checks correspond to invariants on the component types. The
3849 -- record type itself may have invariants that will create a separate
3850 -- procedure whose name carries the Invariant suffix.
3852 Proc_Id :=
3853 Make_Defining_Identifier (Loc,
3854 Chars => New_External_Name (Chars (R_Type), "CInvariant"));
3856 Proc_Body :=
3857 Make_Subprogram_Body (Loc,
3858 Specification =>
3859 Make_Procedure_Specification (Loc,
3860 Defining_Unit_Name => Proc_Id,
3861 Parameter_Specifications => New_List (
3862 Make_Parameter_Specification (Loc,
3863 Defining_Identifier => Object_Entity,
3864 Parameter_Type => New_Occurrence_Of (R_Type, Loc)))),
3866 Declarations => Empty_List,
3867 Handled_Statement_Sequence =>
3868 Make_Handled_Sequence_Of_Statements (Loc,
3869 Statements => Stmts));
3871 Set_Ekind (Proc_Id, E_Procedure);
3872 Set_Is_Public (Proc_Id, Is_Public (R_Type));
3873 Set_Is_Internal (Proc_Id);
3874 Set_Has_Completion (Proc_Id);
3876 return Proc_Body;
3877 -- Insert_After (Nod, Proc_Body);
3878 -- Analyze (Proc_Body);
3879 end Build_Record_Invariant_Proc;
3881 ----------------------------
3882 -- Build_Slice_Assignment --
3883 ----------------------------
3885 -- Generates the following subprogram:
3887 -- procedure Assign
3888 -- (Source, Target : Array_Type,
3889 -- Left_Lo, Left_Hi : Index;
3890 -- Right_Lo, Right_Hi : Index;
3891 -- Rev : Boolean)
3892 -- is
3893 -- Li1 : Index;
3894 -- Ri1 : Index;
3896 -- begin
3898 -- if Left_Hi < Left_Lo then
3899 -- return;
3900 -- end if;
3902 -- if Rev then
3903 -- Li1 := Left_Hi;
3904 -- Ri1 := Right_Hi;
3905 -- else
3906 -- Li1 := Left_Lo;
3907 -- Ri1 := Right_Lo;
3908 -- end if;
3910 -- loop
3911 -- Target (Li1) := Source (Ri1);
3913 -- if Rev then
3914 -- exit when Li1 = Left_Lo;
3915 -- Li1 := Index'pred (Li1);
3916 -- Ri1 := Index'pred (Ri1);
3917 -- else
3918 -- exit when Li1 = Left_Hi;
3919 -- Li1 := Index'succ (Li1);
3920 -- Ri1 := Index'succ (Ri1);
3921 -- end if;
3922 -- end loop;
3923 -- end Assign;
3925 procedure Build_Slice_Assignment (Typ : Entity_Id) is
3926 Loc : constant Source_Ptr := Sloc (Typ);
3927 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
3929 Larray : constant Entity_Id := Make_Temporary (Loc, 'A');
3930 Rarray : constant Entity_Id := Make_Temporary (Loc, 'R');
3931 Left_Lo : constant Entity_Id := Make_Temporary (Loc, 'L');
3932 Left_Hi : constant Entity_Id := Make_Temporary (Loc, 'L');
3933 Right_Lo : constant Entity_Id := Make_Temporary (Loc, 'R');
3934 Right_Hi : constant Entity_Id := Make_Temporary (Loc, 'R');
3935 Rev : constant Entity_Id := Make_Temporary (Loc, 'D');
3936 -- Formal parameters of procedure
3938 Proc_Name : constant Entity_Id :=
3939 Make_Defining_Identifier (Loc,
3940 Chars => Make_TSS_Name (Typ, TSS_Slice_Assign));
3942 Lnn : constant Entity_Id := Make_Temporary (Loc, 'L');
3943 Rnn : constant Entity_Id := Make_Temporary (Loc, 'R');
3944 -- Subscripts for left and right sides
3946 Decls : List_Id;
3947 Loops : Node_Id;
3948 Stats : List_Id;
3950 begin
3951 -- Build declarations for indexes
3953 Decls := New_List;
3955 Append_To (Decls,
3956 Make_Object_Declaration (Loc,
3957 Defining_Identifier => Lnn,
3958 Object_Definition =>
3959 New_Occurrence_Of (Index, Loc)));
3961 Append_To (Decls,
3962 Make_Object_Declaration (Loc,
3963 Defining_Identifier => Rnn,
3964 Object_Definition =>
3965 New_Occurrence_Of (Index, Loc)));
3967 Stats := New_List;
3969 -- Build test for empty slice case
3971 Append_To (Stats,
3972 Make_If_Statement (Loc,
3973 Condition =>
3974 Make_Op_Lt (Loc,
3975 Left_Opnd => New_Occurrence_Of (Left_Hi, Loc),
3976 Right_Opnd => New_Occurrence_Of (Left_Lo, Loc)),
3977 Then_Statements => New_List (Make_Simple_Return_Statement (Loc))));
3979 -- Build initializations for indexes
3981 declare
3982 F_Init : constant List_Id := New_List;
3983 B_Init : constant List_Id := New_List;
3985 begin
3986 Append_To (F_Init,
3987 Make_Assignment_Statement (Loc,
3988 Name => New_Occurrence_Of (Lnn, Loc),
3989 Expression => New_Occurrence_Of (Left_Lo, Loc)));
3991 Append_To (F_Init,
3992 Make_Assignment_Statement (Loc,
3993 Name => New_Occurrence_Of (Rnn, Loc),
3994 Expression => New_Occurrence_Of (Right_Lo, Loc)));
3996 Append_To (B_Init,
3997 Make_Assignment_Statement (Loc,
3998 Name => New_Occurrence_Of (Lnn, Loc),
3999 Expression => New_Occurrence_Of (Left_Hi, Loc)));
4001 Append_To (B_Init,
4002 Make_Assignment_Statement (Loc,
4003 Name => New_Occurrence_Of (Rnn, Loc),
4004 Expression => New_Occurrence_Of (Right_Hi, Loc)));
4006 Append_To (Stats,
4007 Make_If_Statement (Loc,
4008 Condition => New_Occurrence_Of (Rev, Loc),
4009 Then_Statements => B_Init,
4010 Else_Statements => F_Init));
4011 end;
4013 -- Now construct the assignment statement
4015 Loops :=
4016 Make_Loop_Statement (Loc,
4017 Statements => New_List (
4018 Make_Assignment_Statement (Loc,
4019 Name =>
4020 Make_Indexed_Component (Loc,
4021 Prefix => New_Occurrence_Of (Larray, Loc),
4022 Expressions => New_List (New_Occurrence_Of (Lnn, Loc))),
4023 Expression =>
4024 Make_Indexed_Component (Loc,
4025 Prefix => New_Occurrence_Of (Rarray, Loc),
4026 Expressions => New_List (New_Occurrence_Of (Rnn, Loc))))),
4027 End_Label => Empty);
4029 -- Build the exit condition and increment/decrement statements
4031 declare
4032 F_Ass : constant List_Id := New_List;
4033 B_Ass : constant List_Id := New_List;
4035 begin
4036 Append_To (F_Ass,
4037 Make_Exit_Statement (Loc,
4038 Condition =>
4039 Make_Op_Eq (Loc,
4040 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
4041 Right_Opnd => New_Occurrence_Of (Left_Hi, Loc))));
4043 Append_To (F_Ass,
4044 Make_Assignment_Statement (Loc,
4045 Name => New_Occurrence_Of (Lnn, Loc),
4046 Expression =>
4047 Make_Attribute_Reference (Loc,
4048 Prefix =>
4049 New_Occurrence_Of (Index, Loc),
4050 Attribute_Name => Name_Succ,
4051 Expressions => New_List (
4052 New_Occurrence_Of (Lnn, Loc)))));
4054 Append_To (F_Ass,
4055 Make_Assignment_Statement (Loc,
4056 Name => New_Occurrence_Of (Rnn, Loc),
4057 Expression =>
4058 Make_Attribute_Reference (Loc,
4059 Prefix =>
4060 New_Occurrence_Of (Index, Loc),
4061 Attribute_Name => Name_Succ,
4062 Expressions => New_List (
4063 New_Occurrence_Of (Rnn, Loc)))));
4065 Append_To (B_Ass,
4066 Make_Exit_Statement (Loc,
4067 Condition =>
4068 Make_Op_Eq (Loc,
4069 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
4070 Right_Opnd => New_Occurrence_Of (Left_Lo, Loc))));
4072 Append_To (B_Ass,
4073 Make_Assignment_Statement (Loc,
4074 Name => New_Occurrence_Of (Lnn, Loc),
4075 Expression =>
4076 Make_Attribute_Reference (Loc,
4077 Prefix =>
4078 New_Occurrence_Of (Index, Loc),
4079 Attribute_Name => Name_Pred,
4080 Expressions => New_List (
4081 New_Occurrence_Of (Lnn, Loc)))));
4083 Append_To (B_Ass,
4084 Make_Assignment_Statement (Loc,
4085 Name => New_Occurrence_Of (Rnn, Loc),
4086 Expression =>
4087 Make_Attribute_Reference (Loc,
4088 Prefix =>
4089 New_Occurrence_Of (Index, Loc),
4090 Attribute_Name => Name_Pred,
4091 Expressions => New_List (
4092 New_Occurrence_Of (Rnn, Loc)))));
4094 Append_To (Statements (Loops),
4095 Make_If_Statement (Loc,
4096 Condition => New_Occurrence_Of (Rev, Loc),
4097 Then_Statements => B_Ass,
4098 Else_Statements => F_Ass));
4099 end;
4101 Append_To (Stats, Loops);
4103 declare
4104 Spec : Node_Id;
4105 Formals : List_Id := New_List;
4107 begin
4108 Formals := New_List (
4109 Make_Parameter_Specification (Loc,
4110 Defining_Identifier => Larray,
4111 Out_Present => True,
4112 Parameter_Type =>
4113 New_Occurrence_Of (Base_Type (Typ), Loc)),
4115 Make_Parameter_Specification (Loc,
4116 Defining_Identifier => Rarray,
4117 Parameter_Type =>
4118 New_Occurrence_Of (Base_Type (Typ), Loc)),
4120 Make_Parameter_Specification (Loc,
4121 Defining_Identifier => Left_Lo,
4122 Parameter_Type =>
4123 New_Occurrence_Of (Index, Loc)),
4125 Make_Parameter_Specification (Loc,
4126 Defining_Identifier => Left_Hi,
4127 Parameter_Type =>
4128 New_Occurrence_Of (Index, Loc)),
4130 Make_Parameter_Specification (Loc,
4131 Defining_Identifier => Right_Lo,
4132 Parameter_Type =>
4133 New_Occurrence_Of (Index, Loc)),
4135 Make_Parameter_Specification (Loc,
4136 Defining_Identifier => Right_Hi,
4137 Parameter_Type =>
4138 New_Occurrence_Of (Index, Loc)));
4140 Append_To (Formals,
4141 Make_Parameter_Specification (Loc,
4142 Defining_Identifier => Rev,
4143 Parameter_Type =>
4144 New_Occurrence_Of (Standard_Boolean, Loc)));
4146 Spec :=
4147 Make_Procedure_Specification (Loc,
4148 Defining_Unit_Name => Proc_Name,
4149 Parameter_Specifications => Formals);
4151 Discard_Node (
4152 Make_Subprogram_Body (Loc,
4153 Specification => Spec,
4154 Declarations => Decls,
4155 Handled_Statement_Sequence =>
4156 Make_Handled_Sequence_Of_Statements (Loc,
4157 Statements => Stats)));
4158 end;
4160 Set_TSS (Typ, Proc_Name);
4161 Set_Is_Pure (Proc_Name);
4162 end Build_Slice_Assignment;
4164 -----------------------------
4165 -- Build_Untagged_Equality --
4166 -----------------------------
4168 procedure Build_Untagged_Equality (Typ : Entity_Id) is
4169 Build_Eq : Boolean;
4170 Comp : Entity_Id;
4171 Decl : Node_Id;
4172 Op : Entity_Id;
4173 Prim : Elmt_Id;
4174 Eq_Op : Entity_Id;
4176 function User_Defined_Eq (T : Entity_Id) return Entity_Id;
4177 -- Check whether the type T has a user-defined primitive equality. If so
4178 -- return it, else return Empty. If true for a component of Typ, we have
4179 -- to build the primitive equality for it.
4181 ---------------------
4182 -- User_Defined_Eq --
4183 ---------------------
4185 function User_Defined_Eq (T : Entity_Id) return Entity_Id is
4186 Prim : Elmt_Id;
4187 Op : Entity_Id;
4189 begin
4190 Op := TSS (T, TSS_Composite_Equality);
4192 if Present (Op) then
4193 return Op;
4194 end if;
4196 Prim := First_Elmt (Collect_Primitive_Operations (T));
4197 while Present (Prim) loop
4198 Op := Node (Prim);
4200 if Chars (Op) = Name_Op_Eq
4201 and then Etype (Op) = Standard_Boolean
4202 and then Etype (First_Formal (Op)) = T
4203 and then Etype (Next_Formal (First_Formal (Op))) = T
4204 then
4205 return Op;
4206 end if;
4208 Next_Elmt (Prim);
4209 end loop;
4211 return Empty;
4212 end User_Defined_Eq;
4214 -- Start of processing for Build_Untagged_Equality
4216 begin
4217 -- If a record component has a primitive equality operation, we must
4218 -- build the corresponding one for the current type.
4220 Build_Eq := False;
4221 Comp := First_Component (Typ);
4222 while Present (Comp) loop
4223 if Is_Record_Type (Etype (Comp))
4224 and then Present (User_Defined_Eq (Etype (Comp)))
4225 then
4226 Build_Eq := True;
4227 end if;
4229 Next_Component (Comp);
4230 end loop;
4232 -- If there is a user-defined equality for the type, we do not create
4233 -- the implicit one.
4235 Prim := First_Elmt (Collect_Primitive_Operations (Typ));
4236 Eq_Op := Empty;
4237 while Present (Prim) loop
4238 if Chars (Node (Prim)) = Name_Op_Eq
4239 and then Comes_From_Source (Node (Prim))
4241 -- Don't we also need to check formal types and return type as in
4242 -- User_Defined_Eq above???
4244 then
4245 Eq_Op := Node (Prim);
4246 Build_Eq := False;
4247 exit;
4248 end if;
4250 Next_Elmt (Prim);
4251 end loop;
4253 -- If the type is derived, inherit the operation, if present, from the
4254 -- parent type. It may have been declared after the type derivation. If
4255 -- the parent type itself is derived, it may have inherited an operation
4256 -- that has itself been overridden, so update its alias and related
4257 -- flags. Ditto for inequality.
4259 if No (Eq_Op) and then Is_Derived_Type (Typ) then
4260 Prim := First_Elmt (Collect_Primitive_Operations (Etype (Typ)));
4261 while Present (Prim) loop
4262 if Chars (Node (Prim)) = Name_Op_Eq then
4263 Copy_TSS (Node (Prim), Typ);
4264 Build_Eq := False;
4266 declare
4267 Op : constant Entity_Id := User_Defined_Eq (Typ);
4268 Eq_Op : constant Entity_Id := Node (Prim);
4269 NE_Op : constant Entity_Id := Next_Entity (Eq_Op);
4271 begin
4272 if Present (Op) then
4273 Set_Alias (Op, Eq_Op);
4274 Set_Is_Abstract_Subprogram
4275 (Op, Is_Abstract_Subprogram (Eq_Op));
4277 if Chars (Next_Entity (Op)) = Name_Op_Ne then
4278 Set_Is_Abstract_Subprogram
4279 (Next_Entity (Op), Is_Abstract_Subprogram (NE_Op));
4280 end if;
4281 end if;
4282 end;
4284 exit;
4285 end if;
4287 Next_Elmt (Prim);
4288 end loop;
4289 end if;
4291 -- If not inherited and not user-defined, build body as for a type with
4292 -- tagged components.
4294 if Build_Eq then
4295 Decl :=
4296 Make_Eq_Body (Typ, Make_TSS_Name (Typ, TSS_Composite_Equality));
4297 Op := Defining_Entity (Decl);
4298 Set_TSS (Typ, Op);
4299 Set_Is_Pure (Op);
4301 if Is_Library_Level_Entity (Typ) then
4302 Set_Is_Public (Op);
4303 end if;
4304 end if;
4305 end Build_Untagged_Equality;
4307 -----------------------------------
4308 -- Build_Variant_Record_Equality --
4309 -----------------------------------
4311 -- Generates:
4313 -- function _Equality (X, Y : T) return Boolean is
4314 -- begin
4315 -- -- Compare discriminants
4317 -- if X.D1 /= Y.D1 or else X.D2 /= Y.D2 or else ... then
4318 -- return False;
4319 -- end if;
4321 -- -- Compare components
4323 -- if X.C1 /= Y.C1 or else X.C2 /= Y.C2 or else ... then
4324 -- return False;
4325 -- end if;
4327 -- -- Compare variant part
4329 -- case X.D1 is
4330 -- when V1 =>
4331 -- if X.C2 /= Y.C2 or else X.C3 /= Y.C3 or else ... then
4332 -- return False;
4333 -- end if;
4334 -- ...
4335 -- when Vn =>
4336 -- if X.Cn /= Y.Cn or else ... then
4337 -- return False;
4338 -- end if;
4339 -- end case;
4341 -- return True;
4342 -- end _Equality;
4344 procedure Build_Variant_Record_Equality (Typ : Entity_Id) is
4345 Loc : constant Source_Ptr := Sloc (Typ);
4347 F : constant Entity_Id :=
4348 Make_Defining_Identifier (Loc,
4349 Chars => Make_TSS_Name (Typ, TSS_Composite_Equality));
4351 X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_X);
4352 Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_Y);
4354 Def : constant Node_Id := Parent (Typ);
4355 Comps : constant Node_Id := Component_List (Type_Definition (Def));
4356 Stmts : constant List_Id := New_List;
4357 Pspecs : constant List_Id := New_List;
4359 begin
4360 -- If we have a variant record with restriction No_Implicit_Conditionals
4361 -- in effect, then we skip building the procedure. This is safe because
4362 -- if we can see the restriction, so can any caller, calls to equality
4363 -- test routines are not allowed for variant records if this restriction
4364 -- is active.
4366 if Restriction_Active (No_Implicit_Conditionals) then
4367 return;
4368 end if;
4370 -- Derived Unchecked_Union types no longer inherit the equality function
4371 -- of their parent.
4373 if Is_Derived_Type (Typ)
4374 and then not Is_Unchecked_Union (Typ)
4375 and then not Has_New_Non_Standard_Rep (Typ)
4376 then
4377 declare
4378 Parent_Eq : constant Entity_Id :=
4379 TSS (Root_Type (Typ), TSS_Composite_Equality);
4380 begin
4381 if Present (Parent_Eq) then
4382 Copy_TSS (Parent_Eq, Typ);
4383 return;
4384 end if;
4385 end;
4386 end if;
4388 Discard_Node (
4389 Make_Subprogram_Body (Loc,
4390 Specification =>
4391 Make_Function_Specification (Loc,
4392 Defining_Unit_Name => F,
4393 Parameter_Specifications => Pspecs,
4394 Result_Definition => New_Occurrence_Of (Standard_Boolean, Loc)),
4395 Declarations => New_List,
4396 Handled_Statement_Sequence =>
4397 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts)));
4399 Append_To (Pspecs,
4400 Make_Parameter_Specification (Loc,
4401 Defining_Identifier => X,
4402 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
4404 Append_To (Pspecs,
4405 Make_Parameter_Specification (Loc,
4406 Defining_Identifier => Y,
4407 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
4409 -- Unchecked_Unions require additional machinery to support equality.
4410 -- Two extra parameters (A and B) are added to the equality function
4411 -- parameter list for each discriminant of the type, in order to
4412 -- capture the inferred values of the discriminants in equality calls.
4413 -- The names of the parameters match the names of the corresponding
4414 -- discriminant, with an added suffix.
4416 if Is_Unchecked_Union (Typ) then
4417 declare
4418 Discr : Entity_Id;
4419 Discr_Type : Entity_Id;
4420 A, B : Entity_Id;
4421 New_Discrs : Elist_Id;
4423 begin
4424 New_Discrs := New_Elmt_List;
4426 Discr := First_Discriminant (Typ);
4427 while Present (Discr) loop
4428 Discr_Type := Etype (Discr);
4429 A := Make_Defining_Identifier (Loc,
4430 Chars => New_External_Name (Chars (Discr), 'A'));
4432 B := Make_Defining_Identifier (Loc,
4433 Chars => New_External_Name (Chars (Discr), 'B'));
4435 -- Add new parameters to the parameter list
4437 Append_To (Pspecs,
4438 Make_Parameter_Specification (Loc,
4439 Defining_Identifier => A,
4440 Parameter_Type =>
4441 New_Occurrence_Of (Discr_Type, Loc)));
4443 Append_To (Pspecs,
4444 Make_Parameter_Specification (Loc,
4445 Defining_Identifier => B,
4446 Parameter_Type =>
4447 New_Occurrence_Of (Discr_Type, Loc)));
4449 Append_Elmt (A, New_Discrs);
4451 -- Generate the following code to compare each of the inferred
4452 -- discriminants:
4454 -- if a /= b then
4455 -- return False;
4456 -- end if;
4458 Append_To (Stmts,
4459 Make_If_Statement (Loc,
4460 Condition =>
4461 Make_Op_Ne (Loc,
4462 Left_Opnd => New_Occurrence_Of (A, Loc),
4463 Right_Opnd => New_Occurrence_Of (B, Loc)),
4464 Then_Statements => New_List (
4465 Make_Simple_Return_Statement (Loc,
4466 Expression =>
4467 New_Occurrence_Of (Standard_False, Loc)))));
4468 Next_Discriminant (Discr);
4469 end loop;
4471 -- Generate component-by-component comparison. Note that we must
4472 -- propagate the inferred discriminants formals to act as
4473 -- the case statement switch. Their value is added when an
4474 -- equality call on unchecked unions is expanded.
4476 Append_List_To (Stmts, Make_Eq_Case (Typ, Comps, New_Discrs));
4477 end;
4479 -- Normal case (not unchecked union)
4481 else
4482 Append_To (Stmts,
4483 Make_Eq_If (Typ, Discriminant_Specifications (Def)));
4484 Append_List_To (Stmts, Make_Eq_Case (Typ, Comps));
4485 end if;
4487 Append_To (Stmts,
4488 Make_Simple_Return_Statement (Loc,
4489 Expression => New_Occurrence_Of (Standard_True, Loc)));
4491 Set_TSS (Typ, F);
4492 Set_Is_Pure (F);
4494 if not Debug_Generated_Code then
4495 Set_Debug_Info_Off (F);
4496 end if;
4497 end Build_Variant_Record_Equality;
4499 -----------------------------
4500 -- Check_Stream_Attributes --
4501 -----------------------------
4503 procedure Check_Stream_Attributes (Typ : Entity_Id) is
4504 Comp : Entity_Id;
4505 Par_Read : constant Boolean :=
4506 Stream_Attribute_Available (Typ, TSS_Stream_Read)
4507 and then not Has_Specified_Stream_Read (Typ);
4508 Par_Write : constant Boolean :=
4509 Stream_Attribute_Available (Typ, TSS_Stream_Write)
4510 and then not Has_Specified_Stream_Write (Typ);
4512 procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type);
4513 -- Check that Comp has a user-specified Nam stream attribute
4515 ----------------
4516 -- Check_Attr --
4517 ----------------
4519 procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type) is
4520 begin
4521 if not Stream_Attribute_Available (Etype (Comp), TSS_Nam) then
4522 Error_Msg_Name_1 := Nam;
4523 Error_Msg_N
4524 ("|component& in limited extension must have% attribute", Comp);
4525 end if;
4526 end Check_Attr;
4528 -- Start of processing for Check_Stream_Attributes
4530 begin
4531 if Par_Read or else Par_Write then
4532 Comp := First_Component (Typ);
4533 while Present (Comp) loop
4534 if Comes_From_Source (Comp)
4535 and then Original_Record_Component (Comp) = Comp
4536 and then Is_Limited_Type (Etype (Comp))
4537 then
4538 if Par_Read then
4539 Check_Attr (Name_Read, TSS_Stream_Read);
4540 end if;
4542 if Par_Write then
4543 Check_Attr (Name_Write, TSS_Stream_Write);
4544 end if;
4545 end if;
4547 Next_Component (Comp);
4548 end loop;
4549 end if;
4550 end Check_Stream_Attributes;
4552 -----------------------------
4553 -- Expand_Record_Extension --
4554 -----------------------------
4556 -- Add a field _parent at the beginning of the record extension. This is
4557 -- used to implement inheritance. Here are some examples of expansion:
4559 -- 1. no discriminants
4560 -- type T2 is new T1 with null record;
4561 -- gives
4562 -- type T2 is new T1 with record
4563 -- _Parent : T1;
4564 -- end record;
4566 -- 2. renamed discriminants
4567 -- type T2 (B, C : Int) is new T1 (A => B) with record
4568 -- _Parent : T1 (A => B);
4569 -- D : Int;
4570 -- end;
4572 -- 3. inherited discriminants
4573 -- type T2 is new T1 with record -- discriminant A inherited
4574 -- _Parent : T1 (A);
4575 -- D : Int;
4576 -- end;
4578 procedure Expand_Record_Extension (T : Entity_Id; Def : Node_Id) is
4579 Indic : constant Node_Id := Subtype_Indication (Def);
4580 Loc : constant Source_Ptr := Sloc (Def);
4581 Rec_Ext_Part : Node_Id := Record_Extension_Part (Def);
4582 Par_Subtype : Entity_Id;
4583 Comp_List : Node_Id;
4584 Comp_Decl : Node_Id;
4585 Parent_N : Node_Id;
4586 D : Entity_Id;
4587 List_Constr : constant List_Id := New_List;
4589 begin
4590 -- Expand_Record_Extension is called directly from the semantics, so
4591 -- we must check to see whether expansion is active before proceeding,
4592 -- because this affects the visibility of selected components in bodies
4593 -- of instances.
4595 if not Expander_Active then
4596 return;
4597 end if;
4599 -- This may be a derivation of an untagged private type whose full
4600 -- view is tagged, in which case the Derived_Type_Definition has no
4601 -- extension part. Build an empty one now.
4603 if No (Rec_Ext_Part) then
4604 Rec_Ext_Part :=
4605 Make_Record_Definition (Loc,
4606 End_Label => Empty,
4607 Component_List => Empty,
4608 Null_Present => True);
4610 Set_Record_Extension_Part (Def, Rec_Ext_Part);
4611 Mark_Rewrite_Insertion (Rec_Ext_Part);
4612 end if;
4614 Comp_List := Component_List (Rec_Ext_Part);
4616 Parent_N := Make_Defining_Identifier (Loc, Name_uParent);
4618 -- If the derived type inherits its discriminants the type of the
4619 -- _parent field must be constrained by the inherited discriminants
4621 if Has_Discriminants (T)
4622 and then Nkind (Indic) /= N_Subtype_Indication
4623 and then not Is_Constrained (Entity (Indic))
4624 then
4625 D := First_Discriminant (T);
4626 while Present (D) loop
4627 Append_To (List_Constr, New_Occurrence_Of (D, Loc));
4628 Next_Discriminant (D);
4629 end loop;
4631 Par_Subtype :=
4632 Process_Subtype (
4633 Make_Subtype_Indication (Loc,
4634 Subtype_Mark => New_Occurrence_Of (Entity (Indic), Loc),
4635 Constraint =>
4636 Make_Index_Or_Discriminant_Constraint (Loc,
4637 Constraints => List_Constr)),
4638 Def);
4640 -- Otherwise the original subtype_indication is just what is needed
4642 else
4643 Par_Subtype := Process_Subtype (New_Copy_Tree (Indic), Def);
4644 end if;
4646 Set_Parent_Subtype (T, Par_Subtype);
4648 Comp_Decl :=
4649 Make_Component_Declaration (Loc,
4650 Defining_Identifier => Parent_N,
4651 Component_Definition =>
4652 Make_Component_Definition (Loc,
4653 Aliased_Present => False,
4654 Subtype_Indication => New_Occurrence_Of (Par_Subtype, Loc)));
4656 if Null_Present (Rec_Ext_Part) then
4657 Set_Component_List (Rec_Ext_Part,
4658 Make_Component_List (Loc,
4659 Component_Items => New_List (Comp_Decl),
4660 Variant_Part => Empty,
4661 Null_Present => False));
4662 Set_Null_Present (Rec_Ext_Part, False);
4664 elsif Null_Present (Comp_List)
4665 or else Is_Empty_List (Component_Items (Comp_List))
4666 then
4667 Set_Component_Items (Comp_List, New_List (Comp_Decl));
4668 Set_Null_Present (Comp_List, False);
4670 else
4671 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
4672 end if;
4674 Analyze (Comp_Decl);
4675 end Expand_Record_Extension;
4677 ------------------------------------
4678 -- Expand_N_Full_Type_Declaration --
4679 ------------------------------------
4681 procedure Expand_N_Full_Type_Declaration (N : Node_Id) is
4682 procedure Build_Master (Ptr_Typ : Entity_Id);
4683 -- Create the master associated with Ptr_Typ
4685 ------------------
4686 -- Build_Master --
4687 ------------------
4689 procedure Build_Master (Ptr_Typ : Entity_Id) is
4690 Desig_Typ : Entity_Id := Designated_Type (Ptr_Typ);
4692 begin
4693 -- If the designated type is an incomplete view coming from a
4694 -- limited-with'ed package, we need to use the nonlimited view in
4695 -- case it has tasks.
4697 if Ekind (Desig_Typ) in Incomplete_Kind
4698 and then Present (Non_Limited_View (Desig_Typ))
4699 then
4700 Desig_Typ := Non_Limited_View (Desig_Typ);
4701 end if;
4703 -- Anonymous access types are created for the components of the
4704 -- record parameter for an entry declaration. No master is created
4705 -- for such a type.
4707 if Comes_From_Source (N) and then Has_Task (Desig_Typ) then
4708 Build_Master_Entity (Ptr_Typ);
4709 Build_Master_Renaming (Ptr_Typ);
4711 -- Create a class-wide master because a Master_Id must be generated
4712 -- for access-to-limited-class-wide types whose root may be extended
4713 -- with task components.
4715 -- Note: This code covers access-to-limited-interfaces because they
4716 -- can be used to reference tasks implementing them.
4718 elsif Is_Limited_Class_Wide_Type (Desig_Typ)
4719 and then Tasking_Allowed
4721 -- Do not create a class-wide master for types whose convention is
4722 -- Java since these types cannot embed Ada tasks anyway. Note that
4723 -- the following test cannot catch the following case:
4725 -- package java.lang.Object is
4726 -- type Typ is tagged limited private;
4727 -- type Ref is access all Typ'Class;
4728 -- private
4729 -- type Typ is tagged limited ...;
4730 -- pragma Convention (Typ, Java)
4731 -- end;
4733 -- Because the convention appears after we have done the
4734 -- processing for type Ref.
4736 and then Convention (Desig_Typ) /= Convention_Java
4737 and then Convention (Desig_Typ) /= Convention_CIL
4738 then
4739 Build_Class_Wide_Master (Ptr_Typ);
4740 end if;
4741 end Build_Master;
4743 -- Local declarations
4745 Def_Id : constant Entity_Id := Defining_Identifier (N);
4746 B_Id : constant Entity_Id := Base_Type (Def_Id);
4747 FN : Node_Id;
4748 Par_Id : Entity_Id;
4750 -- Start of processing for Expand_N_Full_Type_Declaration
4752 begin
4753 if Is_Access_Type (Def_Id) then
4754 Build_Master (Def_Id);
4756 if Ekind (Def_Id) = E_Access_Protected_Subprogram_Type then
4757 Expand_Access_Protected_Subprogram_Type (N);
4758 end if;
4760 -- Array of anonymous access-to-task pointers
4762 elsif Ada_Version >= Ada_2005
4763 and then Is_Array_Type (Def_Id)
4764 and then Is_Access_Type (Component_Type (Def_Id))
4765 and then Ekind (Component_Type (Def_Id)) = E_Anonymous_Access_Type
4766 then
4767 Build_Master (Component_Type (Def_Id));
4769 elsif Has_Task (Def_Id) then
4770 Expand_Previous_Access_Type (Def_Id);
4772 -- Check the components of a record type or array of records for
4773 -- anonymous access-to-task pointers.
4775 elsif Ada_Version >= Ada_2005
4776 and then (Is_Record_Type (Def_Id)
4777 or else
4778 (Is_Array_Type (Def_Id)
4779 and then Is_Record_Type (Component_Type (Def_Id))))
4780 then
4781 declare
4782 Comp : Entity_Id;
4783 First : Boolean;
4784 M_Id : Entity_Id;
4785 Typ : Entity_Id;
4787 begin
4788 if Is_Array_Type (Def_Id) then
4789 Comp := First_Entity (Component_Type (Def_Id));
4790 else
4791 Comp := First_Entity (Def_Id);
4792 end if;
4794 -- Examine all components looking for anonymous access-to-task
4795 -- types.
4797 First := True;
4798 while Present (Comp) loop
4799 Typ := Etype (Comp);
4801 if Ekind (Typ) = E_Anonymous_Access_Type
4802 and then Has_Task (Available_View (Designated_Type (Typ)))
4803 and then No (Master_Id (Typ))
4804 then
4805 -- Ensure that the record or array type have a _master
4807 if First then
4808 Build_Master_Entity (Def_Id);
4809 Build_Master_Renaming (Typ);
4810 M_Id := Master_Id (Typ);
4812 First := False;
4814 -- Reuse the same master to service any additional types
4816 else
4817 Set_Master_Id (Typ, M_Id);
4818 end if;
4819 end if;
4821 Next_Entity (Comp);
4822 end loop;
4823 end;
4824 end if;
4826 Par_Id := Etype (B_Id);
4828 -- The parent type is private then we need to inherit any TSS operations
4829 -- from the full view.
4831 if Ekind (Par_Id) in Private_Kind
4832 and then Present (Full_View (Par_Id))
4833 then
4834 Par_Id := Base_Type (Full_View (Par_Id));
4835 end if;
4837 if Nkind (Type_Definition (Original_Node (N))) =
4838 N_Derived_Type_Definition
4839 and then not Is_Tagged_Type (Def_Id)
4840 and then Present (Freeze_Node (Par_Id))
4841 and then Present (TSS_Elist (Freeze_Node (Par_Id)))
4842 then
4843 Ensure_Freeze_Node (B_Id);
4844 FN := Freeze_Node (B_Id);
4846 if No (TSS_Elist (FN)) then
4847 Set_TSS_Elist (FN, New_Elmt_List);
4848 end if;
4850 declare
4851 T_E : constant Elist_Id := TSS_Elist (FN);
4852 Elmt : Elmt_Id;
4854 begin
4855 Elmt := First_Elmt (TSS_Elist (Freeze_Node (Par_Id)));
4856 while Present (Elmt) loop
4857 if Chars (Node (Elmt)) /= Name_uInit then
4858 Append_Elmt (Node (Elmt), T_E);
4859 end if;
4861 Next_Elmt (Elmt);
4862 end loop;
4864 -- If the derived type itself is private with a full view, then
4865 -- associate the full view with the inherited TSS_Elist as well.
4867 if Ekind (B_Id) in Private_Kind
4868 and then Present (Full_View (B_Id))
4869 then
4870 Ensure_Freeze_Node (Base_Type (Full_View (B_Id)));
4871 Set_TSS_Elist
4872 (Freeze_Node (Base_Type (Full_View (B_Id))), TSS_Elist (FN));
4873 end if;
4874 end;
4875 end if;
4876 end Expand_N_Full_Type_Declaration;
4878 ---------------------------------
4879 -- Expand_N_Object_Declaration --
4880 ---------------------------------
4882 procedure Expand_N_Object_Declaration (N : Node_Id) is
4883 Def_Id : constant Entity_Id := Defining_Identifier (N);
4884 Expr : constant Node_Id := Expression (N);
4885 Loc : constant Source_Ptr := Sloc (N);
4886 Obj_Def : constant Node_Id := Object_Definition (N);
4887 Typ : constant Entity_Id := Etype (Def_Id);
4888 Base_Typ : constant Entity_Id := Base_Type (Typ);
4889 Expr_Q : Node_Id;
4891 function Build_Equivalent_Aggregate return Boolean;
4892 -- If the object has a constrained discriminated type and no initial
4893 -- value, it may be possible to build an equivalent aggregate instead,
4894 -- and prevent an actual call to the initialization procedure.
4896 procedure Default_Initialize_Object (After : Node_Id);
4897 -- Generate all default initialization actions for object Def_Id. Any
4898 -- new code is inserted after node After.
4900 function Rewrite_As_Renaming return Boolean;
4901 -- Indicate whether to rewrite a declaration with initialization into an
4902 -- object renaming declaration (see below).
4904 --------------------------------
4905 -- Build_Equivalent_Aggregate --
4906 --------------------------------
4908 function Build_Equivalent_Aggregate return Boolean is
4909 Aggr : Node_Id;
4910 Comp : Entity_Id;
4911 Discr : Elmt_Id;
4912 Full_Type : Entity_Id;
4914 begin
4915 Full_Type := Typ;
4917 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
4918 Full_Type := Full_View (Typ);
4919 end if;
4921 -- Only perform this transformation if Elaboration_Code is forbidden
4922 -- or undesirable, and if this is a global entity of a constrained
4923 -- record type.
4925 -- If Initialize_Scalars might be active this transformation cannot
4926 -- be performed either, because it will lead to different semantics
4927 -- or because elaboration code will in fact be created.
4929 if Ekind (Full_Type) /= E_Record_Subtype
4930 or else not Has_Discriminants (Full_Type)
4931 or else not Is_Constrained (Full_Type)
4932 or else Is_Controlled (Full_Type)
4933 or else Is_Limited_Type (Full_Type)
4934 or else not Restriction_Active (No_Initialize_Scalars)
4935 then
4936 return False;
4937 end if;
4939 if Ekind (Current_Scope) = E_Package
4940 and then
4941 (Restriction_Active (No_Elaboration_Code)
4942 or else Is_Preelaborated (Current_Scope))
4943 then
4944 -- Building a static aggregate is possible if the discriminants
4945 -- have static values and the other components have static
4946 -- defaults or none.
4948 Discr := First_Elmt (Discriminant_Constraint (Full_Type));
4949 while Present (Discr) loop
4950 if not Is_OK_Static_Expression (Node (Discr)) then
4951 return False;
4952 end if;
4954 Next_Elmt (Discr);
4955 end loop;
4957 -- Check that initialized components are OK, and that non-
4958 -- initialized components do not require a call to their own
4959 -- initialization procedure.
4961 Comp := First_Component (Full_Type);
4962 while Present (Comp) loop
4963 if Ekind (Comp) = E_Component
4964 and then Present (Expression (Parent (Comp)))
4965 and then
4966 not Is_OK_Static_Expression (Expression (Parent (Comp)))
4967 then
4968 return False;
4970 elsif Has_Non_Null_Base_Init_Proc (Etype (Comp)) then
4971 return False;
4973 end if;
4975 Next_Component (Comp);
4976 end loop;
4978 -- Everything is static, assemble the aggregate, discriminant
4979 -- values first.
4981 Aggr :=
4982 Make_Aggregate (Loc,
4983 Expressions => New_List,
4984 Component_Associations => New_List);
4986 Discr := First_Elmt (Discriminant_Constraint (Full_Type));
4987 while Present (Discr) loop
4988 Append_To (Expressions (Aggr), New_Copy (Node (Discr)));
4989 Next_Elmt (Discr);
4990 end loop;
4992 -- Now collect values of initialized components
4994 Comp := First_Component (Full_Type);
4995 while Present (Comp) loop
4996 if Ekind (Comp) = E_Component
4997 and then Present (Expression (Parent (Comp)))
4998 then
4999 Append_To (Component_Associations (Aggr),
5000 Make_Component_Association (Loc,
5001 Choices => New_List (New_Occurrence_Of (Comp, Loc)),
5002 Expression => New_Copy_Tree
5003 (Expression (Parent (Comp)))));
5004 end if;
5006 Next_Component (Comp);
5007 end loop;
5009 -- Finally, box-initialize remaining components
5011 Append_To (Component_Associations (Aggr),
5012 Make_Component_Association (Loc,
5013 Choices => New_List (Make_Others_Choice (Loc)),
5014 Expression => Empty));
5015 Set_Box_Present (Last (Component_Associations (Aggr)));
5016 Set_Expression (N, Aggr);
5018 if Typ /= Full_Type then
5019 Analyze_And_Resolve (Aggr, Full_View (Base_Type (Full_Type)));
5020 Rewrite (Aggr, Unchecked_Convert_To (Typ, Aggr));
5021 Analyze_And_Resolve (Aggr, Typ);
5022 else
5023 Analyze_And_Resolve (Aggr, Full_Type);
5024 end if;
5026 return True;
5028 else
5029 return False;
5030 end if;
5031 end Build_Equivalent_Aggregate;
5033 -------------------------------
5034 -- Default_Initialize_Object --
5035 -------------------------------
5037 procedure Default_Initialize_Object (After : Node_Id) is
5038 function New_Object_Reference return Node_Id;
5039 -- Return a new reference to Def_Id with attributes Assignment_OK and
5040 -- Must_Not_Freeze already set.
5042 --------------------------
5043 -- New_Object_Reference --
5044 --------------------------
5046 function New_Object_Reference return Node_Id is
5047 Obj_Ref : constant Node_Id := New_Occurrence_Of (Def_Id, Loc);
5049 begin
5050 -- The call to the type init proc or [Deep_]Finalize must not
5051 -- freeze the related object as the call is internally generated.
5052 -- This way legal rep clauses that apply to the object will not be
5053 -- flagged. Note that the initialization call may be removed if
5054 -- pragma Import is encountered or moved to the freeze actions of
5055 -- the object because of an address clause.
5057 Set_Assignment_OK (Obj_Ref);
5058 Set_Must_Not_Freeze (Obj_Ref);
5060 return Obj_Ref;
5061 end New_Object_Reference;
5063 -- Local variables
5065 Abrt_Blk : Node_Id;
5066 Abrt_HSS : Node_Id;
5067 Abrt_Id : Entity_Id;
5068 Abrt_Stmts : List_Id;
5069 Aggr_Init : Node_Id;
5070 Comp_Init : List_Id := No_List;
5071 Fin_Call : Node_Id;
5072 Fin_Stmts : List_Id := No_List;
5073 Obj_Init : Node_Id := Empty;
5074 Obj_Ref : Node_Id;
5076 Dummy : Entity_Id;
5077 -- This variable captures a dummy internal entity, see the comment
5078 -- associated with its use.
5080 -- Start of processing for Default_Initialize_Object
5082 begin
5083 -- Default initialization is suppressed for objects that are already
5084 -- known to be imported (i.e. whose declaration specifies the Import
5085 -- aspect). Note that for objects with a pragma Import, we generate
5086 -- initialization here, and then remove it downstream when processing
5087 -- the pragma. It is also suppressed for variables for which a pragma
5088 -- Suppress_Initialization has been explicitly given
5090 if Is_Imported (Def_Id) or else Suppress_Initialization (Def_Id) then
5091 return;
5092 end if;
5094 -- Step 1: Initialize the object
5096 if Needs_Finalization (Typ) and then not No_Initialization (N) then
5097 Obj_Init :=
5098 Make_Init_Call
5099 (Obj_Ref => New_Occurrence_Of (Def_Id, Loc),
5100 Typ => Typ);
5101 end if;
5103 -- Step 2: Initialize the components of the object
5105 -- Do not initialize the components if their initialization is
5106 -- prohibited or the type represents a value type in a .NET VM.
5108 if Has_Non_Null_Base_Init_Proc (Typ)
5109 and then not No_Initialization (N)
5110 and then not Initialization_Suppressed (Typ)
5111 and then not Is_Value_Type (Typ)
5112 then
5113 -- Do not initialize the components if No_Default_Initialization
5114 -- applies as the the actual restriction check will occur later
5115 -- when the object is frozen as it is not known yet whether the
5116 -- object is imported or not.
5118 if not Restriction_Active (No_Default_Initialization) then
5120 -- If the values of the components are compile-time known, use
5121 -- their prebuilt aggregate form directly.
5123 Aggr_Init := Static_Initialization (Base_Init_Proc (Typ));
5125 if Present (Aggr_Init) then
5126 Set_Expression
5127 (N, New_Copy_Tree (Aggr_Init, New_Scope => Current_Scope));
5129 -- If type has discriminants, try to build an equivalent
5130 -- aggregate using discriminant values from the declaration.
5131 -- This is a useful optimization, in particular if restriction
5132 -- No_Elaboration_Code is active.
5134 elsif Build_Equivalent_Aggregate then
5135 null;
5137 -- Otherwise invoke the type init proc
5139 else
5140 Obj_Ref := New_Object_Reference;
5142 if Comes_From_Source (Def_Id) then
5143 Initialization_Warning (Obj_Ref);
5144 end if;
5146 Comp_Init := Build_Initialization_Call (Loc, Obj_Ref, Typ);
5147 end if;
5148 end if;
5150 -- Provide a default value if the object needs simple initialization
5151 -- and does not already have an initial value. A generated temporary
5152 -- do not require initialization because it will be assigned later.
5154 elsif Needs_Simple_Initialization
5155 (Typ, Initialize_Scalars
5156 and then not Has_Following_Address_Clause (N))
5157 and then not Is_Internal (Def_Id)
5158 and then not Has_Init_Expression (N)
5159 then
5160 Set_No_Initialization (N, False);
5161 Set_Expression (N, Get_Simple_Init_Val (Typ, N, Esize (Def_Id)));
5162 Analyze_And_Resolve (Expression (N), Typ);
5163 end if;
5165 -- Step 3: Add partial finalization and abort actions, generate:
5167 -- Type_Init_Proc (Obj);
5168 -- begin
5169 -- Deep_Initialize (Obj);
5170 -- exception
5171 -- when others =>
5172 -- Deep_Finalize (Obj, Self => False);
5173 -- raise;
5174 -- end;
5176 -- Step 3a: Build the finalization block (if applicable)
5178 -- The finalization block is required when both the object and its
5179 -- controlled components are to be initialized. The block finalizes
5180 -- the components if the object initialization fails.
5182 if Has_Controlled_Component (Typ)
5183 and then Present (Comp_Init)
5184 and then Present (Obj_Init)
5185 and then not Restriction_Active (No_Exception_Propagation)
5186 then
5187 -- Generate:
5188 -- Type_Init_Proc (Obj);
5190 Fin_Stmts := Comp_Init;
5192 -- Generate:
5193 -- begin
5194 -- Deep_Initialize (Obj);
5195 -- exception
5196 -- when others =>
5197 -- Deep_Finalize (Obj, Self => False);
5198 -- raise;
5199 -- end;
5201 Fin_Call :=
5202 Make_Final_Call
5203 (Obj_Ref => New_Object_Reference,
5204 Typ => Typ,
5205 Skip_Self => True);
5207 if Present (Fin_Call) then
5209 -- Do not emit warnings related to the elaboration order when a
5210 -- controlled object is declared before the body of Finalize is
5211 -- seen.
5213 Set_No_Elaboration_Check (Fin_Call);
5215 Append_To (Fin_Stmts,
5216 Make_Block_Statement (Loc,
5217 Declarations => No_List,
5219 Handled_Statement_Sequence =>
5220 Make_Handled_Sequence_Of_Statements (Loc,
5221 Statements => New_List (Obj_Init),
5223 Exception_Handlers => New_List (
5224 Make_Exception_Handler (Loc,
5225 Exception_Choices => New_List (
5226 Make_Others_Choice (Loc)),
5228 Statements => New_List (
5229 Fin_Call,
5230 Make_Raise_Statement (Loc)))))));
5231 end if;
5233 -- Finalization is not required, the initialization calls are passed
5234 -- to the abort block building circuitry, generate:
5236 -- Type_Init_Proc (Obj);
5237 -- Deep_Initialize (Obj);
5239 else
5240 if Present (Comp_Init) then
5241 Fin_Stmts := Comp_Init;
5242 end if;
5244 if Present (Obj_Init) then
5245 if No (Fin_Stmts) then
5246 Fin_Stmts := New_List;
5247 end if;
5249 Append_To (Fin_Stmts, Obj_Init);
5250 end if;
5251 end if;
5253 -- Step 3b: Build the abort block (if applicable)
5255 -- The abort block is required when aborts are allowed in order to
5256 -- protect both initialization calls.
5258 if Present (Comp_Init) and then Present (Obj_Init) then
5259 if Abort_Allowed then
5261 -- Generate:
5262 -- Abort_Defer;
5264 Prepend_To
5265 (Fin_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
5267 -- Generate:
5268 -- begin
5269 -- Abort_Defer;
5270 -- <finalization statements>
5271 -- at end
5272 -- Abort_Undefer_Direct;
5273 -- end;
5275 Abrt_HSS :=
5276 Make_Handled_Sequence_Of_Statements (Loc,
5277 Statements => Fin_Stmts,
5278 At_End_Proc =>
5279 New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc));
5281 Abrt_Blk :=
5282 Make_Block_Statement (Loc,
5283 Declarations => No_List,
5284 Handled_Statement_Sequence => Abrt_HSS);
5286 Add_Block_Identifier (Abrt_Blk, Abrt_Id);
5287 Expand_At_End_Handler (Abrt_HSS, Abrt_Id);
5289 Abrt_Stmts := New_List (Abrt_Blk);
5291 -- Abort is not required
5293 else
5294 -- Generate a dummy entity to ensure that the internal symbols
5295 -- are in sync when a unit is compiled with and without aborts.
5296 -- The entity is a block with proper scope and type.
5298 Dummy := New_Internal_Entity (E_Block, Current_Scope, Loc, 'B');
5299 Set_Etype (Dummy, Standard_Void_Type);
5300 Abrt_Stmts := Fin_Stmts;
5301 end if;
5303 -- No initialization calls present
5305 else
5306 Abrt_Stmts := Fin_Stmts;
5307 end if;
5309 -- Step 4: Insert the whole initialization sequence into the tree
5311 Insert_Actions_After (After, Abrt_Stmts);
5312 end Default_Initialize_Object;
5314 -------------------------
5315 -- Rewrite_As_Renaming --
5316 -------------------------
5318 function Rewrite_As_Renaming return Boolean is
5319 begin
5320 return not Aliased_Present (N)
5321 and then Is_Entity_Name (Expr_Q)
5322 and then Ekind (Entity (Expr_Q)) = E_Variable
5323 and then OK_To_Rename (Entity (Expr_Q))
5324 and then Is_Entity_Name (Obj_Def);
5325 end Rewrite_As_Renaming;
5327 -- Local variables
5329 Next_N : constant Node_Id := Next (N);
5330 Id_Ref : Node_Id;
5331 New_Ref : Node_Id;
5333 Init_After : Node_Id := N;
5334 -- Node after which the initialization actions are to be inserted. This
5335 -- is normally N, except for the case of a shared passive variable, in
5336 -- which case the init proc call must be inserted only after the bodies
5337 -- of the shared variable procedures have been seen.
5339 -- Start of processing for Expand_N_Object_Declaration
5341 begin
5342 -- Don't do anything for deferred constants. All proper actions will be
5343 -- expanded during the full declaration.
5345 if No (Expr) and Constant_Present (N) then
5346 return;
5347 end if;
5349 -- First we do special processing for objects of a tagged type where
5350 -- this is the point at which the type is frozen. The creation of the
5351 -- dispatch table and the initialization procedure have to be deferred
5352 -- to this point, since we reference previously declared primitive
5353 -- subprograms.
5355 -- Force construction of dispatch tables of library level tagged types
5357 if Tagged_Type_Expansion
5358 and then Static_Dispatch_Tables
5359 and then Is_Library_Level_Entity (Def_Id)
5360 and then Is_Library_Level_Tagged_Type (Base_Typ)
5361 and then (Ekind (Base_Typ) = E_Record_Type
5362 or else Ekind (Base_Typ) = E_Protected_Type
5363 or else Ekind (Base_Typ) = E_Task_Type)
5364 and then not Has_Dispatch_Table (Base_Typ)
5365 then
5366 declare
5367 New_Nodes : List_Id := No_List;
5369 begin
5370 if Is_Concurrent_Type (Base_Typ) then
5371 New_Nodes := Make_DT (Corresponding_Record_Type (Base_Typ), N);
5372 else
5373 New_Nodes := Make_DT (Base_Typ, N);
5374 end if;
5376 if not Is_Empty_List (New_Nodes) then
5377 Insert_List_Before (N, New_Nodes);
5378 end if;
5379 end;
5380 end if;
5382 -- Make shared memory routines for shared passive variable
5384 if Is_Shared_Passive (Def_Id) then
5385 Init_After := Make_Shared_Var_Procs (N);
5386 end if;
5388 -- If tasks being declared, make sure we have an activation chain
5389 -- defined for the tasks (has no effect if we already have one), and
5390 -- also that a Master variable is established and that the appropriate
5391 -- enclosing construct is established as a task master.
5393 if Has_Task (Typ) then
5394 Build_Activation_Chain_Entity (N);
5395 Build_Master_Entity (Def_Id);
5396 end if;
5398 -- Default initialization required, and no expression present
5400 if No (Expr) then
5402 -- If we have a type with a variant part, the initialization proc
5403 -- will contain implicit tests of the discriminant values, which
5404 -- counts as a violation of the restriction No_Implicit_Conditionals.
5406 if Has_Variant_Part (Typ) then
5407 declare
5408 Msg : Boolean;
5410 begin
5411 Check_Restriction (Msg, No_Implicit_Conditionals, Obj_Def);
5413 if Msg then
5414 Error_Msg_N
5415 ("\initialization of variant record tests discriminants",
5416 Obj_Def);
5417 return;
5418 end if;
5419 end;
5420 end if;
5422 -- For the default initialization case, if we have a private type
5423 -- with invariants, and invariant checks are enabled, then insert an
5424 -- invariant check after the object declaration. Note that it is OK
5425 -- to clobber the object with an invalid value since if the exception
5426 -- is raised, then the object will go out of scope. In the case where
5427 -- an array object is initialized with an aggregate, the expression
5428 -- is removed. Check flag Has_Init_Expression to avoid generating a
5429 -- junk invariant check and flag No_Initialization to avoid checking
5430 -- an uninitialized object such as a compiler temporary used for an
5431 -- aggregate.
5433 if Has_Invariants (Base_Typ)
5434 and then Present (Invariant_Procedure (Base_Typ))
5435 and then not Has_Init_Expression (N)
5436 and then not No_Initialization (N)
5437 then
5438 Insert_After (N,
5439 Make_Invariant_Call (New_Occurrence_Of (Def_Id, Loc)));
5440 end if;
5442 Default_Initialize_Object (Init_After);
5444 -- Generate attribute for Persistent_BSS if needed
5446 if Persistent_BSS_Mode
5447 and then Comes_From_Source (N)
5448 and then Is_Potentially_Persistent_Type (Typ)
5449 and then not Has_Init_Expression (N)
5450 and then Is_Library_Level_Entity (Def_Id)
5451 then
5452 declare
5453 Prag : Node_Id;
5454 begin
5455 Prag :=
5456 Make_Linker_Section_Pragma
5457 (Def_Id, Sloc (N), ".persistent.bss");
5458 Insert_After (N, Prag);
5459 Analyze (Prag);
5460 end;
5461 end if;
5463 -- If access type, then we know it is null if not initialized
5465 if Is_Access_Type (Typ) then
5466 Set_Is_Known_Null (Def_Id);
5467 end if;
5469 -- Explicit initialization present
5471 else
5472 -- Obtain actual expression from qualified expression
5474 if Nkind (Expr) = N_Qualified_Expression then
5475 Expr_Q := Expression (Expr);
5476 else
5477 Expr_Q := Expr;
5478 end if;
5480 -- When we have the appropriate type of aggregate in the expression
5481 -- (it has been determined during analysis of the aggregate by
5482 -- setting the delay flag), let's perform in place assignment and
5483 -- thus avoid creating a temporary.
5485 if Is_Delayed_Aggregate (Expr_Q) then
5486 Convert_Aggr_In_Object_Decl (N);
5488 -- Ada 2005 (AI-318-02): If the initialization expression is a call
5489 -- to a build-in-place function, then access to the declared object
5490 -- must be passed to the function. Currently we limit such functions
5491 -- to those with constrained limited result subtypes, but eventually
5492 -- plan to expand the allowed forms of functions that are treated as
5493 -- build-in-place.
5495 elsif Ada_Version >= Ada_2005
5496 and then Is_Build_In_Place_Function_Call (Expr_Q)
5497 then
5498 Make_Build_In_Place_Call_In_Object_Declaration (N, Expr_Q);
5500 -- The previous call expands the expression initializing the
5501 -- built-in-place object into further code that will be analyzed
5502 -- later. No further expansion needed here.
5504 return;
5506 -- Ada 2005 (AI-251): Rewrite the expression that initializes a
5507 -- class-wide interface object to ensure that we copy the full
5508 -- object, unless we are targetting a VM where interfaces are handled
5509 -- by VM itself. Note that if the root type of Typ is an ancestor of
5510 -- Expr's type, both types share the same dispatch table and there is
5511 -- no need to displace the pointer.
5513 elsif Is_Interface (Typ)
5515 -- Avoid never-ending recursion because if Equivalent_Type is set
5516 -- then we've done it already and must not do it again.
5518 and then not
5519 (Nkind (Obj_Def) = N_Identifier
5520 and then Present (Equivalent_Type (Entity (Obj_Def))))
5521 then
5522 pragma Assert (Is_Class_Wide_Type (Typ));
5524 -- If the object is a return object of an inherently limited type,
5525 -- which implies build-in-place treatment, bypass the special
5526 -- treatment of class-wide interface initialization below. In this
5527 -- case, the expansion of the return statement will take care of
5528 -- creating the object (via allocator) and initializing it.
5530 if Is_Return_Object (Def_Id) and then Is_Limited_View (Typ) then
5531 null;
5533 elsif Tagged_Type_Expansion then
5534 declare
5535 Iface : constant Entity_Id := Root_Type (Typ);
5536 Expr_N : Node_Id := Expr;
5537 Expr_Typ : Entity_Id;
5538 New_Expr : Node_Id;
5539 Obj_Id : Entity_Id;
5540 Tag_Comp : Node_Id;
5542 begin
5543 -- If the original node of the expression was a conversion
5544 -- to this specific class-wide interface type then restore
5545 -- the original node because we must copy the object before
5546 -- displacing the pointer to reference the secondary tag
5547 -- component. This code must be kept synchronized with the
5548 -- expansion done by routine Expand_Interface_Conversion
5550 if not Comes_From_Source (Expr_N)
5551 and then Nkind (Expr_N) = N_Explicit_Dereference
5552 and then Nkind (Original_Node (Expr_N)) = N_Type_Conversion
5553 and then Etype (Original_Node (Expr_N)) = Typ
5554 then
5555 Rewrite (Expr_N, Original_Node (Expression (N)));
5556 end if;
5558 -- Avoid expansion of redundant interface conversion
5560 if Is_Interface (Etype (Expr_N))
5561 and then Nkind (Expr_N) = N_Type_Conversion
5562 and then Etype (Expr_N) = Typ
5563 then
5564 Expr_N := Expression (Expr_N);
5565 Set_Expression (N, Expr_N);
5566 end if;
5568 Obj_Id := Make_Temporary (Loc, 'D', Expr_N);
5569 Expr_Typ := Base_Type (Etype (Expr_N));
5571 if Is_Class_Wide_Type (Expr_Typ) then
5572 Expr_Typ := Root_Type (Expr_Typ);
5573 end if;
5575 -- Replace
5576 -- CW : I'Class := Obj;
5577 -- by
5578 -- Tmp : T := Obj;
5579 -- type Ityp is not null access I'Class;
5580 -- CW : I'Class renames Ityp (Tmp.I_Tag'Address).all;
5582 if Comes_From_Source (Expr_N)
5583 and then Nkind (Expr_N) = N_Identifier
5584 and then not Is_Interface (Expr_Typ)
5585 and then Interface_Present_In_Ancestor (Expr_Typ, Typ)
5586 and then (Expr_Typ = Etype (Expr_Typ)
5587 or else not
5588 Is_Variable_Size_Record (Etype (Expr_Typ)))
5589 then
5590 -- Copy the object
5592 Insert_Action (N,
5593 Make_Object_Declaration (Loc,
5594 Defining_Identifier => Obj_Id,
5595 Object_Definition =>
5596 New_Occurrence_Of (Expr_Typ, Loc),
5597 Expression => Relocate_Node (Expr_N)));
5599 -- Statically reference the tag associated with the
5600 -- interface
5602 Tag_Comp :=
5603 Make_Selected_Component (Loc,
5604 Prefix => New_Occurrence_Of (Obj_Id, Loc),
5605 Selector_Name =>
5606 New_Occurrence_Of
5607 (Find_Interface_Tag (Expr_Typ, Iface), Loc));
5609 -- Replace
5610 -- IW : I'Class := Obj;
5611 -- by
5612 -- type Equiv_Record is record ... end record;
5613 -- implicit subtype CW is <Class_Wide_Subtype>;
5614 -- Tmp : CW := CW!(Obj);
5615 -- type Ityp is not null access I'Class;
5616 -- IW : I'Class renames
5617 -- Ityp!(Displace (Temp'Address, I'Tag)).all;
5619 else
5620 -- Generate the equivalent record type and update the
5621 -- subtype indication to reference it.
5623 Expand_Subtype_From_Expr
5624 (N => N,
5625 Unc_Type => Typ,
5626 Subtype_Indic => Obj_Def,
5627 Exp => Expr_N);
5629 if not Is_Interface (Etype (Expr_N)) then
5630 New_Expr := Relocate_Node (Expr_N);
5632 -- For interface types we use 'Address which displaces
5633 -- the pointer to the base of the object (if required)
5635 else
5636 New_Expr :=
5637 Unchecked_Convert_To (Etype (Obj_Def),
5638 Make_Explicit_Dereference (Loc,
5639 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
5640 Make_Attribute_Reference (Loc,
5641 Prefix => Relocate_Node (Expr_N),
5642 Attribute_Name => Name_Address))));
5643 end if;
5645 -- Copy the object
5647 if not Is_Limited_Record (Expr_Typ) then
5648 Insert_Action (N,
5649 Make_Object_Declaration (Loc,
5650 Defining_Identifier => Obj_Id,
5651 Object_Definition =>
5652 New_Occurrence_Of (Etype (Obj_Def), Loc),
5653 Expression => New_Expr));
5655 -- Rename limited type object since they cannot be copied
5656 -- This case occurs when the initialization expression
5657 -- has been previously expanded into a temporary object.
5659 else pragma Assert (not Comes_From_Source (Expr_Q));
5660 Insert_Action (N,
5661 Make_Object_Renaming_Declaration (Loc,
5662 Defining_Identifier => Obj_Id,
5663 Subtype_Mark =>
5664 New_Occurrence_Of (Etype (Obj_Def), Loc),
5665 Name =>
5666 Unchecked_Convert_To
5667 (Etype (Obj_Def), New_Expr)));
5668 end if;
5670 -- Dynamically reference the tag associated with the
5671 -- interface.
5673 Tag_Comp :=
5674 Make_Function_Call (Loc,
5675 Name => New_Occurrence_Of (RTE (RE_Displace), Loc),
5676 Parameter_Associations => New_List (
5677 Make_Attribute_Reference (Loc,
5678 Prefix => New_Occurrence_Of (Obj_Id, Loc),
5679 Attribute_Name => Name_Address),
5680 New_Occurrence_Of
5681 (Node (First_Elmt (Access_Disp_Table (Iface))),
5682 Loc)));
5683 end if;
5685 Rewrite (N,
5686 Make_Object_Renaming_Declaration (Loc,
5687 Defining_Identifier => Make_Temporary (Loc, 'D'),
5688 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
5689 Name =>
5690 Convert_Tag_To_Interface (Typ, Tag_Comp)));
5692 -- If the original entity comes from source, then mark the
5693 -- new entity as needing debug information, even though it's
5694 -- defined by a generated renaming that does not come from
5695 -- source, so that Materialize_Entity will be set on the
5696 -- entity when Debug_Renaming_Declaration is called during
5697 -- analysis.
5699 if Comes_From_Source (Def_Id) then
5700 Set_Debug_Info_Needed (Defining_Identifier (N));
5701 end if;
5703 Analyze (N, Suppress => All_Checks);
5705 -- Replace internal identifier of rewritten node by the
5706 -- identifier found in the sources. We also have to exchange
5707 -- entities containing their defining identifiers to ensure
5708 -- the correct replacement of the object declaration by this
5709 -- object renaming declaration because these identifiers
5710 -- were previously added by Enter_Name to the current scope.
5711 -- We must preserve the homonym chain of the source entity
5712 -- as well. We must also preserve the kind of the entity,
5713 -- which may be a constant. Preserve entity chain because
5714 -- itypes may have been generated already, and the full
5715 -- chain must be preserved for final freezing. Finally,
5716 -- preserve Comes_From_Source setting, so that debugging
5717 -- and cross-referencing information is properly kept, and
5718 -- preserve source location, to prevent spurious errors when
5719 -- entities are declared (they must have their own Sloc).
5721 declare
5722 New_Id : constant Entity_Id := Defining_Identifier (N);
5723 Next_Temp : constant Entity_Id := Next_Entity (New_Id);
5724 S_Flag : constant Boolean :=
5725 Comes_From_Source (Def_Id);
5727 begin
5728 Set_Next_Entity (New_Id, Next_Entity (Def_Id));
5729 Set_Next_Entity (Def_Id, Next_Temp);
5731 Set_Chars (Defining_Identifier (N), Chars (Def_Id));
5732 Set_Homonym (Defining_Identifier (N), Homonym (Def_Id));
5733 Set_Ekind (Defining_Identifier (N), Ekind (Def_Id));
5734 Set_Sloc (Defining_Identifier (N), Sloc (Def_Id));
5736 Set_Comes_From_Source (Def_Id, False);
5737 Exchange_Entities (Defining_Identifier (N), Def_Id);
5738 Set_Comes_From_Source (Def_Id, S_Flag);
5739 end;
5740 end;
5741 end if;
5743 return;
5745 -- Common case of explicit object initialization
5747 else
5748 -- In most cases, we must check that the initial value meets any
5749 -- constraint imposed by the declared type. However, there is one
5750 -- very important exception to this rule. If the entity has an
5751 -- unconstrained nominal subtype, then it acquired its constraints
5752 -- from the expression in the first place, and not only does this
5753 -- mean that the constraint check is not needed, but an attempt to
5754 -- perform the constraint check can cause order of elaboration
5755 -- problems.
5757 if not Is_Constr_Subt_For_U_Nominal (Typ) then
5759 -- If this is an allocator for an aggregate that has been
5760 -- allocated in place, delay checks until assignments are
5761 -- made, because the discriminants are not initialized.
5763 if Nkind (Expr) = N_Allocator and then No_Initialization (Expr)
5764 then
5765 null;
5767 -- Otherwise apply a constraint check now if no prev error
5769 elsif Nkind (Expr) /= N_Error then
5770 Apply_Constraint_Check (Expr, Typ);
5772 -- Deal with possible range check
5774 if Do_Range_Check (Expr) then
5776 -- If assignment checks are suppressed, turn off flag
5778 if Suppress_Assignment_Checks (N) then
5779 Set_Do_Range_Check (Expr, False);
5781 -- Otherwise generate the range check
5783 else
5784 Generate_Range_Check
5785 (Expr, Typ, CE_Range_Check_Failed);
5786 end if;
5787 end if;
5788 end if;
5789 end if;
5791 -- If the type is controlled and not inherently limited, then
5792 -- the target is adjusted after the copy and attached to the
5793 -- finalization list. However, no adjustment is done in the case
5794 -- where the object was initialized by a call to a function whose
5795 -- result is built in place, since no copy occurred. (Eventually
5796 -- we plan to support in-place function results for some cases
5797 -- of nonlimited types. ???) Similarly, no adjustment is required
5798 -- if we are going to rewrite the object declaration into a
5799 -- renaming declaration.
5801 if Needs_Finalization (Typ)
5802 and then not Is_Limited_View (Typ)
5803 and then not Rewrite_As_Renaming
5804 then
5805 Insert_Action_After (Init_After,
5806 Make_Adjust_Call (
5807 Obj_Ref => New_Occurrence_Of (Def_Id, Loc),
5808 Typ => Base_Typ));
5809 end if;
5811 -- For tagged types, when an init value is given, the tag has to
5812 -- be re-initialized separately in order to avoid the propagation
5813 -- of a wrong tag coming from a view conversion unless the type
5814 -- is class wide (in this case the tag comes from the init value).
5815 -- Suppress the tag assignment when VM_Target because VM tags are
5816 -- represented implicitly in objects. Ditto for types that are
5817 -- CPP_CLASS, and for initializations that are aggregates, because
5818 -- they have to have the right tag.
5820 if Is_Tagged_Type (Typ)
5821 and then not Is_Class_Wide_Type (Typ)
5822 and then not Is_CPP_Class (Typ)
5823 and then Tagged_Type_Expansion
5824 and then Nkind (Expr) /= N_Aggregate
5825 and then (Nkind (Expr) /= N_Qualified_Expression
5826 or else Nkind (Expression (Expr)) /= N_Aggregate)
5827 then
5828 declare
5829 Full_Typ : constant Entity_Id := Underlying_Type (Typ);
5831 begin
5832 -- The re-assignment of the tag has to be done even if the
5833 -- object is a constant. The assignment must be analyzed
5834 -- after the declaration.
5836 New_Ref :=
5837 Make_Selected_Component (Loc,
5838 Prefix => New_Occurrence_Of (Def_Id, Loc),
5839 Selector_Name =>
5840 New_Occurrence_Of (First_Tag_Component (Full_Typ),
5841 Loc));
5842 Set_Assignment_OK (New_Ref);
5844 Insert_Action_After (Init_After,
5845 Make_Assignment_Statement (Loc,
5846 Name => New_Ref,
5847 Expression =>
5848 Unchecked_Convert_To (RTE (RE_Tag),
5849 New_Occurrence_Of
5850 (Node (First_Elmt (Access_Disp_Table (Full_Typ))),
5851 Loc))));
5852 end;
5854 -- Handle C++ constructor calls. Note that we do not check that
5855 -- Typ is a tagged type since the equivalent Ada type of a C++
5856 -- class that has no virtual methods is an untagged limited
5857 -- record type.
5859 elsif Is_CPP_Constructor_Call (Expr) then
5861 -- The call to the initialization procedure does NOT freeze the
5862 -- object being initialized.
5864 Id_Ref := New_Occurrence_Of (Def_Id, Loc);
5865 Set_Must_Not_Freeze (Id_Ref);
5866 Set_Assignment_OK (Id_Ref);
5868 Insert_Actions_After (Init_After,
5869 Build_Initialization_Call (Loc, Id_Ref, Typ,
5870 Constructor_Ref => Expr));
5872 -- We remove here the original call to the constructor
5873 -- to avoid its management in the backend
5875 Set_Expression (N, Empty);
5876 return;
5878 -- For discrete types, set the Is_Known_Valid flag if the
5879 -- initializing value is known to be valid. Only do this for
5880 -- source assignments, since otherwise we can end up turning
5881 -- on the known valid flag prematurely from inserted code.
5883 elsif Comes_From_Source (N)
5884 and then Is_Discrete_Type (Typ)
5885 and then Expr_Known_Valid (Expr)
5886 then
5887 Set_Is_Known_Valid (Def_Id);
5889 elsif Is_Access_Type (Typ) then
5891 -- For access types set the Is_Known_Non_Null flag if the
5892 -- initializing value is known to be non-null. We can also set
5893 -- Can_Never_Be_Null if this is a constant.
5895 if Known_Non_Null (Expr) then
5896 Set_Is_Known_Non_Null (Def_Id, True);
5898 if Constant_Present (N) then
5899 Set_Can_Never_Be_Null (Def_Id);
5900 end if;
5901 end if;
5902 end if;
5904 -- If validity checking on copies, validate initial expression.
5905 -- But skip this if declaration is for a generic type, since it
5906 -- makes no sense to validate generic types. Not clear if this
5907 -- can happen for legal programs, but it definitely can arise
5908 -- from previous instantiation errors.
5910 if Validity_Checks_On
5911 and then Validity_Check_Copies
5912 and then not Is_Generic_Type (Etype (Def_Id))
5913 then
5914 Ensure_Valid (Expr);
5915 Set_Is_Known_Valid (Def_Id);
5916 end if;
5917 end if;
5919 -- Cases where the back end cannot handle the initialization directly
5920 -- In such cases, we expand an assignment that will be appropriately
5921 -- handled by Expand_N_Assignment_Statement.
5923 -- The exclusion of the unconstrained case is wrong, but for now it
5924 -- is too much trouble ???
5926 if (Is_Possibly_Unaligned_Slice (Expr)
5927 or else (Is_Possibly_Unaligned_Object (Expr)
5928 and then not Represented_As_Scalar (Etype (Expr))))
5929 and then not (Is_Array_Type (Etype (Expr))
5930 and then not Is_Constrained (Etype (Expr)))
5931 then
5932 declare
5933 Stat : constant Node_Id :=
5934 Make_Assignment_Statement (Loc,
5935 Name => New_Occurrence_Of (Def_Id, Loc),
5936 Expression => Relocate_Node (Expr));
5937 begin
5938 Set_Expression (N, Empty);
5939 Set_No_Initialization (N);
5940 Set_Assignment_OK (Name (Stat));
5941 Set_No_Ctrl_Actions (Stat);
5942 Insert_After_And_Analyze (Init_After, Stat);
5943 end;
5944 end if;
5946 -- Final transformation, if the initializing expression is an entity
5947 -- for a variable with OK_To_Rename set, then we transform:
5949 -- X : typ := expr;
5951 -- into
5953 -- X : typ renames expr
5955 -- provided that X is not aliased. The aliased case has to be
5956 -- excluded in general because Expr will not be aliased in general.
5958 if Rewrite_As_Renaming then
5959 Rewrite (N,
5960 Make_Object_Renaming_Declaration (Loc,
5961 Defining_Identifier => Defining_Identifier (N),
5962 Subtype_Mark => Obj_Def,
5963 Name => Expr_Q));
5965 -- We do not analyze this renaming declaration, because all its
5966 -- components have already been analyzed, and if we were to go
5967 -- ahead and analyze it, we would in effect be trying to generate
5968 -- another declaration of X, which won't do.
5970 Set_Renamed_Object (Defining_Identifier (N), Expr_Q);
5971 Set_Analyzed (N);
5973 -- We do need to deal with debug issues for this renaming
5975 -- First, if entity comes from source, then mark it as needing
5976 -- debug information, even though it is defined by a generated
5977 -- renaming that does not come from source.
5979 if Comes_From_Source (Defining_Identifier (N)) then
5980 Set_Debug_Info_Needed (Defining_Identifier (N));
5981 end if;
5983 -- Now call the routine to generate debug info for the renaming
5985 declare
5986 Decl : constant Node_Id := Debug_Renaming_Declaration (N);
5987 begin
5988 if Present (Decl) then
5989 Insert_Action (N, Decl);
5990 end if;
5991 end;
5992 end if;
5993 end if;
5995 if Nkind (N) = N_Object_Declaration
5996 and then Nkind (Obj_Def) = N_Access_Definition
5997 and then not Is_Local_Anonymous_Access (Etype (Def_Id))
5998 then
5999 -- An Ada 2012 stand-alone object of an anonymous access type
6001 declare
6002 Loc : constant Source_Ptr := Sloc (N);
6004 Level : constant Entity_Id :=
6005 Make_Defining_Identifier (Sloc (N),
6006 Chars =>
6007 New_External_Name (Chars (Def_Id), Suffix => "L"));
6009 Level_Expr : Node_Id;
6010 Level_Decl : Node_Id;
6012 begin
6013 Set_Ekind (Level, Ekind (Def_Id));
6014 Set_Etype (Level, Standard_Natural);
6015 Set_Scope (Level, Scope (Def_Id));
6017 if No (Expr) then
6019 -- Set accessibility level of null
6021 Level_Expr :=
6022 Make_Integer_Literal (Loc, Scope_Depth (Standard_Standard));
6024 else
6025 Level_Expr := Dynamic_Accessibility_Level (Expr);
6026 end if;
6028 Level_Decl :=
6029 Make_Object_Declaration (Loc,
6030 Defining_Identifier => Level,
6031 Object_Definition =>
6032 New_Occurrence_Of (Standard_Natural, Loc),
6033 Expression => Level_Expr,
6034 Constant_Present => Constant_Present (N),
6035 Has_Init_Expression => True);
6037 Insert_Action_After (Init_After, Level_Decl);
6039 Set_Extra_Accessibility (Def_Id, Level);
6040 end;
6041 end if;
6043 -- At this point the object is fully initialized by either invoking the
6044 -- related type init proc, routine [Deep_]Initialize or performing in-
6045 -- place assingments for an array object. If the related type is subject
6046 -- to pragma Default_Initial_Condition, add a runtime check to verify
6047 -- the assumption of the pragma. Generate:
6049 -- <Base_Typ>Default_Init_Cond (<Base_Typ> (Def_Id));
6051 -- Note that the check is generated for source objects only
6053 if Comes_From_Source (Def_Id)
6054 and then (Has_Default_Init_Cond (Base_Typ)
6055 or else
6056 Has_Inherited_Default_Init_Cond (Base_Typ))
6057 then
6058 declare
6059 DIC_Call : constant Node_Id :=
6060 Build_Default_Init_Cond_Call (Loc, Def_Id, Base_Typ);
6061 begin
6062 if Present (Next_N) then
6063 Insert_Before_And_Analyze (Next_N, DIC_Call);
6065 -- The object declaration is the last node in a declarative or a
6066 -- statement list.
6068 else
6069 Append_To (List_Containing (N), DIC_Call);
6070 Analyze (DIC_Call);
6071 end if;
6072 end;
6073 end if;
6075 -- Exception on library entity not available
6077 exception
6078 when RE_Not_Available =>
6079 return;
6080 end Expand_N_Object_Declaration;
6082 ---------------------------------
6083 -- Expand_N_Subtype_Indication --
6084 ---------------------------------
6086 -- Add a check on the range of the subtype. The static case is partially
6087 -- duplicated by Process_Range_Expr_In_Decl in Sem_Ch3, but we still need
6088 -- to check here for the static case in order to avoid generating
6089 -- extraneous expanded code. Also deal with validity checking.
6091 procedure Expand_N_Subtype_Indication (N : Node_Id) is
6092 Ran : constant Node_Id := Range_Expression (Constraint (N));
6093 Typ : constant Entity_Id := Entity (Subtype_Mark (N));
6095 begin
6096 if Nkind (Constraint (N)) = N_Range_Constraint then
6097 Validity_Check_Range (Range_Expression (Constraint (N)));
6098 end if;
6100 if Nkind_In (Parent (N), N_Constrained_Array_Definition, N_Slice) then
6101 Apply_Range_Check (Ran, Typ);
6102 end if;
6103 end Expand_N_Subtype_Indication;
6105 ---------------------------
6106 -- Expand_N_Variant_Part --
6107 ---------------------------
6109 -- Note: this procedure no longer has any effect. It used to be that we
6110 -- would replace the choices in the last variant by a when others, and
6111 -- also expanded static predicates in variant choices here, but both of
6112 -- those activities were being done too early, since we can't check the
6113 -- choices until the statically predicated subtypes are frozen, which can
6114 -- happen as late as the free point of the record, and we can't change the
6115 -- last choice to an others before checking the choices, which is now done
6116 -- at the freeze point of the record.
6118 procedure Expand_N_Variant_Part (N : Node_Id) is
6119 begin
6120 null;
6121 end Expand_N_Variant_Part;
6123 ---------------------------------
6124 -- Expand_Previous_Access_Type --
6125 ---------------------------------
6127 procedure Expand_Previous_Access_Type (Def_Id : Entity_Id) is
6128 Ptr_Typ : Entity_Id;
6130 begin
6131 -- Find all access types in the current scope whose designated type is
6132 -- Def_Id and build master renamings for them.
6134 Ptr_Typ := First_Entity (Current_Scope);
6135 while Present (Ptr_Typ) loop
6136 if Is_Access_Type (Ptr_Typ)
6137 and then Designated_Type (Ptr_Typ) = Def_Id
6138 and then No (Master_Id (Ptr_Typ))
6139 then
6140 -- Ensure that the designated type has a master
6142 Build_Master_Entity (Def_Id);
6144 -- Private and incomplete types complicate the insertion of master
6145 -- renamings because the access type may precede the full view of
6146 -- the designated type. For this reason, the master renamings are
6147 -- inserted relative to the designated type.
6149 Build_Master_Renaming (Ptr_Typ, Ins_Nod => Parent (Def_Id));
6150 end if;
6152 Next_Entity (Ptr_Typ);
6153 end loop;
6154 end Expand_Previous_Access_Type;
6156 ------------------------
6157 -- Expand_Tagged_Root --
6158 ------------------------
6160 procedure Expand_Tagged_Root (T : Entity_Id) is
6161 Def : constant Node_Id := Type_Definition (Parent (T));
6162 Comp_List : Node_Id;
6163 Comp_Decl : Node_Id;
6164 Sloc_N : Source_Ptr;
6166 begin
6167 if Null_Present (Def) then
6168 Set_Component_List (Def,
6169 Make_Component_List (Sloc (Def),
6170 Component_Items => Empty_List,
6171 Variant_Part => Empty,
6172 Null_Present => True));
6173 end if;
6175 Comp_List := Component_List (Def);
6177 if Null_Present (Comp_List)
6178 or else Is_Empty_List (Component_Items (Comp_List))
6179 then
6180 Sloc_N := Sloc (Comp_List);
6181 else
6182 Sloc_N := Sloc (First (Component_Items (Comp_List)));
6183 end if;
6185 Comp_Decl :=
6186 Make_Component_Declaration (Sloc_N,
6187 Defining_Identifier => First_Tag_Component (T),
6188 Component_Definition =>
6189 Make_Component_Definition (Sloc_N,
6190 Aliased_Present => False,
6191 Subtype_Indication => New_Occurrence_Of (RTE (RE_Tag), Sloc_N)));
6193 if Null_Present (Comp_List)
6194 or else Is_Empty_List (Component_Items (Comp_List))
6195 then
6196 Set_Component_Items (Comp_List, New_List (Comp_Decl));
6197 Set_Null_Present (Comp_List, False);
6199 else
6200 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
6201 end if;
6203 -- We don't Analyze the whole expansion because the tag component has
6204 -- already been analyzed previously. Here we just insure that the tree
6205 -- is coherent with the semantic decoration
6207 Find_Type (Subtype_Indication (Component_Definition (Comp_Decl)));
6209 exception
6210 when RE_Not_Available =>
6211 return;
6212 end Expand_Tagged_Root;
6214 ----------------------
6215 -- Clean_Task_Names --
6216 ----------------------
6218 procedure Clean_Task_Names
6219 (Typ : Entity_Id;
6220 Proc_Id : Entity_Id)
6222 begin
6223 if Has_Task (Typ)
6224 and then not Restriction_Active (No_Implicit_Heap_Allocations)
6225 and then not Global_Discard_Names
6226 and then Tagged_Type_Expansion
6227 then
6228 Set_Uses_Sec_Stack (Proc_Id);
6229 end if;
6230 end Clean_Task_Names;
6232 ------------------------------
6233 -- Expand_Freeze_Array_Type --
6234 ------------------------------
6236 procedure Expand_Freeze_Array_Type (N : Node_Id) is
6237 Typ : constant Entity_Id := Entity (N);
6238 Comp_Typ : constant Entity_Id := Component_Type (Typ);
6239 Base : constant Entity_Id := Base_Type (Typ);
6241 begin
6242 if not Is_Bit_Packed_Array (Typ) then
6244 -- If the component contains tasks, so does the array type. This may
6245 -- not be indicated in the array type because the component may have
6246 -- been a private type at the point of definition. Same if component
6247 -- type is controlled or contains protected objects.
6249 Set_Has_Task (Base, Has_Task (Comp_Typ));
6250 Set_Has_Protected (Base, Has_Protected (Comp_Typ));
6251 Set_Has_Controlled_Component
6252 (Base, Has_Controlled_Component
6253 (Comp_Typ)
6254 or else
6255 Is_Controlled (Comp_Typ));
6257 if No (Init_Proc (Base)) then
6259 -- If this is an anonymous array created for a declaration with
6260 -- an initial value, its init_proc will never be called. The
6261 -- initial value itself may have been expanded into assignments,
6262 -- in which case the object declaration is carries the
6263 -- No_Initialization flag.
6265 if Is_Itype (Base)
6266 and then Nkind (Associated_Node_For_Itype (Base)) =
6267 N_Object_Declaration
6268 and then
6269 (Present (Expression (Associated_Node_For_Itype (Base)))
6270 or else No_Initialization (Associated_Node_For_Itype (Base)))
6271 then
6272 null;
6274 -- We do not need an init proc for string or wide [wide] string,
6275 -- since the only time these need initialization in normalize or
6276 -- initialize scalars mode, and these types are treated specially
6277 -- and do not need initialization procedures.
6279 elsif Is_Standard_String_Type (Base) then
6280 null;
6282 -- Otherwise we have to build an init proc for the subtype
6284 else
6285 Build_Array_Init_Proc (Base, N);
6286 end if;
6287 end if;
6289 if Typ = Base then
6290 if Has_Controlled_Component (Base) then
6291 Build_Controlling_Procs (Base);
6293 if not Is_Limited_Type (Comp_Typ)
6294 and then Number_Dimensions (Typ) = 1
6295 then
6296 Build_Slice_Assignment (Typ);
6297 end if;
6298 end if;
6300 -- Create a finalization master to service the anonymous access
6301 -- components of the array.
6303 if Ekind (Comp_Typ) = E_Anonymous_Access_Type
6304 and then Needs_Finalization (Designated_Type (Comp_Typ))
6305 then
6306 Build_Finalization_Master
6307 (Typ => Comp_Typ,
6308 Ins_Node => Parent (Typ),
6309 Encl_Scope => Scope (Typ));
6310 end if;
6311 end if;
6313 -- For packed case, default initialization, except if the component type
6314 -- is itself a packed structure with an initialization procedure, or
6315 -- initialize/normalize scalars active, and we have a base type, or the
6316 -- type is public, because in that case a client might specify
6317 -- Normalize_Scalars and there better be a public Init_Proc for it.
6319 elsif (Present (Init_Proc (Component_Type (Base)))
6320 and then No (Base_Init_Proc (Base)))
6321 or else (Init_Or_Norm_Scalars and then Base = Typ)
6322 or else Is_Public (Typ)
6323 then
6324 Build_Array_Init_Proc (Base, N);
6325 end if;
6327 if Has_Invariants (Component_Type (Base))
6328 and then Typ = Base
6329 and then In_Open_Scopes (Scope (Component_Type (Base)))
6330 then
6331 -- Generate component invariant checking procedure. This is only
6332 -- relevant if the array type is within the scope of the component
6333 -- type. Otherwise an array object can only be built using the public
6334 -- subprograms for the component type, and calls to those will have
6335 -- invariant checks. The invariant procedure is only generated for
6336 -- a base type, not a subtype.
6338 Insert_Component_Invariant_Checks
6339 (N, Base, Build_Array_Invariant_Proc (Base, N));
6340 end if;
6341 end Expand_Freeze_Array_Type;
6343 -----------------------------------
6344 -- Expand_Freeze_Class_Wide_Type --
6345 -----------------------------------
6347 procedure Expand_Freeze_Class_Wide_Type (N : Node_Id) is
6348 Typ : constant Entity_Id := Entity (N);
6349 Root : constant Entity_Id := Root_Type (Typ);
6351 function Is_C_Derivation (Typ : Entity_Id) return Boolean;
6352 -- Given a type, determine whether it is derived from a C or C++ root
6354 ---------------------
6355 -- Is_C_Derivation --
6356 ---------------------
6358 function Is_C_Derivation (Typ : Entity_Id) return Boolean is
6359 T : Entity_Id := Typ;
6361 begin
6362 loop
6363 if Is_CPP_Class (T)
6364 or else Convention (T) = Convention_C
6365 or else Convention (T) = Convention_CPP
6366 then
6367 return True;
6368 end if;
6370 exit when T = Etype (T);
6372 T := Etype (T);
6373 end loop;
6375 return False;
6376 end Is_C_Derivation;
6378 -- Start of processing for Expand_Freeze_Class_Wide_Type
6380 begin
6381 -- Certain run-time configurations and targets do not provide support
6382 -- for controlled types.
6384 if Restriction_Active (No_Finalization) then
6385 return;
6387 -- Do not create TSS routine Finalize_Address when dispatching calls are
6388 -- disabled since the core of the routine is a dispatching call.
6390 elsif Restriction_Active (No_Dispatching_Calls) then
6391 return;
6393 -- Do not create TSS routine Finalize_Address for concurrent class-wide
6394 -- types. Ignore C, C++, CIL and Java types since it is assumed that the
6395 -- non-Ada side will handle their destruction.
6397 elsif Is_Concurrent_Type (Root)
6398 or else Is_C_Derivation (Root)
6399 or else Convention (Typ) = Convention_CIL
6400 or else Convention (Typ) = Convention_CPP
6401 or else Convention (Typ) = Convention_Java
6402 then
6403 return;
6405 -- Do not create TSS routine Finalize_Address for .NET/JVM because these
6406 -- targets do not support address arithmetic and unchecked conversions.
6408 elsif VM_Target /= No_VM then
6409 return;
6411 -- Do not create TSS routine Finalize_Address when compiling in CodePeer
6412 -- mode since the routine contains an Unchecked_Conversion.
6414 elsif CodePeer_Mode then
6415 return;
6416 end if;
6418 -- Create the body of TSS primitive Finalize_Address. This automatically
6419 -- sets the TSS entry for the class-wide type.
6421 Make_Finalize_Address_Body (Typ);
6422 end Expand_Freeze_Class_Wide_Type;
6424 ------------------------------------
6425 -- Expand_Freeze_Enumeration_Type --
6426 ------------------------------------
6428 procedure Expand_Freeze_Enumeration_Type (N : Node_Id) is
6429 Typ : constant Entity_Id := Entity (N);
6430 Loc : constant Source_Ptr := Sloc (Typ);
6431 Ent : Entity_Id;
6432 Lst : List_Id;
6433 Num : Nat;
6434 Arr : Entity_Id;
6435 Fent : Entity_Id;
6436 Ityp : Entity_Id;
6437 Is_Contiguous : Boolean;
6438 Pos_Expr : Node_Id;
6439 Last_Repval : Uint;
6441 Func : Entity_Id;
6442 pragma Warnings (Off, Func);
6444 begin
6445 -- Various optimizations possible if given representation is contiguous
6447 Is_Contiguous := True;
6449 Ent := First_Literal (Typ);
6450 Last_Repval := Enumeration_Rep (Ent);
6452 Next_Literal (Ent);
6453 while Present (Ent) loop
6454 if Enumeration_Rep (Ent) - Last_Repval /= 1 then
6455 Is_Contiguous := False;
6456 exit;
6457 else
6458 Last_Repval := Enumeration_Rep (Ent);
6459 end if;
6461 Next_Literal (Ent);
6462 end loop;
6464 if Is_Contiguous then
6465 Set_Has_Contiguous_Rep (Typ);
6466 Ent := First_Literal (Typ);
6467 Num := 1;
6468 Lst := New_List (New_Occurrence_Of (Ent, Sloc (Ent)));
6470 else
6471 -- Build list of literal references
6473 Lst := New_List;
6474 Num := 0;
6476 Ent := First_Literal (Typ);
6477 while Present (Ent) loop
6478 Append_To (Lst, New_Occurrence_Of (Ent, Sloc (Ent)));
6479 Num := Num + 1;
6480 Next_Literal (Ent);
6481 end loop;
6482 end if;
6484 -- Now build an array declaration
6486 -- typA : array (Natural range 0 .. num - 1) of ctype :=
6487 -- (v, v, v, v, v, ....)
6489 -- where ctype is the corresponding integer type. If the representation
6490 -- is contiguous, we only keep the first literal, which provides the
6491 -- offset for Pos_To_Rep computations.
6493 Arr :=
6494 Make_Defining_Identifier (Loc,
6495 Chars => New_External_Name (Chars (Typ), 'A'));
6497 Append_Freeze_Action (Typ,
6498 Make_Object_Declaration (Loc,
6499 Defining_Identifier => Arr,
6500 Constant_Present => True,
6502 Object_Definition =>
6503 Make_Constrained_Array_Definition (Loc,
6504 Discrete_Subtype_Definitions => New_List (
6505 Make_Subtype_Indication (Loc,
6506 Subtype_Mark => New_Occurrence_Of (Standard_Natural, Loc),
6507 Constraint =>
6508 Make_Range_Constraint (Loc,
6509 Range_Expression =>
6510 Make_Range (Loc,
6511 Low_Bound =>
6512 Make_Integer_Literal (Loc, 0),
6513 High_Bound =>
6514 Make_Integer_Literal (Loc, Num - 1))))),
6516 Component_Definition =>
6517 Make_Component_Definition (Loc,
6518 Aliased_Present => False,
6519 Subtype_Indication => New_Occurrence_Of (Typ, Loc))),
6521 Expression =>
6522 Make_Aggregate (Loc,
6523 Expressions => Lst)));
6525 Set_Enum_Pos_To_Rep (Typ, Arr);
6527 -- Now we build the function that converts representation values to
6528 -- position values. This function has the form:
6530 -- function _Rep_To_Pos (A : etype; F : Boolean) return Integer is
6531 -- begin
6532 -- case ityp!(A) is
6533 -- when enum-lit'Enum_Rep => return posval;
6534 -- when enum-lit'Enum_Rep => return posval;
6535 -- ...
6536 -- when others =>
6537 -- [raise Constraint_Error when F "invalid data"]
6538 -- return -1;
6539 -- end case;
6540 -- end;
6542 -- Note: the F parameter determines whether the others case (no valid
6543 -- representation) raises Constraint_Error or returns a unique value
6544 -- of minus one. The latter case is used, e.g. in 'Valid code.
6546 -- Note: the reason we use Enum_Rep values in the case here is to avoid
6547 -- the code generator making inappropriate assumptions about the range
6548 -- of the values in the case where the value is invalid. ityp is a
6549 -- signed or unsigned integer type of appropriate width.
6551 -- Note: if exceptions are not supported, then we suppress the raise
6552 -- and return -1 unconditionally (this is an erroneous program in any
6553 -- case and there is no obligation to raise Constraint_Error here). We
6554 -- also do this if pragma Restrictions (No_Exceptions) is active.
6556 -- Is this right??? What about No_Exception_Propagation???
6558 -- Representations are signed
6560 if Enumeration_Rep (First_Literal (Typ)) < 0 then
6562 -- The underlying type is signed. Reset the Is_Unsigned_Type
6563 -- explicitly, because it might have been inherited from
6564 -- parent type.
6566 Set_Is_Unsigned_Type (Typ, False);
6568 if Esize (Typ) <= Standard_Integer_Size then
6569 Ityp := Standard_Integer;
6570 else
6571 Ityp := Universal_Integer;
6572 end if;
6574 -- Representations are unsigned
6576 else
6577 if Esize (Typ) <= Standard_Integer_Size then
6578 Ityp := RTE (RE_Unsigned);
6579 else
6580 Ityp := RTE (RE_Long_Long_Unsigned);
6581 end if;
6582 end if;
6584 -- The body of the function is a case statement. First collect case
6585 -- alternatives, or optimize the contiguous case.
6587 Lst := New_List;
6589 -- If representation is contiguous, Pos is computed by subtracting
6590 -- the representation of the first literal.
6592 if Is_Contiguous then
6593 Ent := First_Literal (Typ);
6595 if Enumeration_Rep (Ent) = Last_Repval then
6597 -- Another special case: for a single literal, Pos is zero
6599 Pos_Expr := Make_Integer_Literal (Loc, Uint_0);
6601 else
6602 Pos_Expr :=
6603 Convert_To (Standard_Integer,
6604 Make_Op_Subtract (Loc,
6605 Left_Opnd =>
6606 Unchecked_Convert_To
6607 (Ityp, Make_Identifier (Loc, Name_uA)),
6608 Right_Opnd =>
6609 Make_Integer_Literal (Loc,
6610 Intval => Enumeration_Rep (First_Literal (Typ)))));
6611 end if;
6613 Append_To (Lst,
6614 Make_Case_Statement_Alternative (Loc,
6615 Discrete_Choices => New_List (
6616 Make_Range (Sloc (Enumeration_Rep_Expr (Ent)),
6617 Low_Bound =>
6618 Make_Integer_Literal (Loc,
6619 Intval => Enumeration_Rep (Ent)),
6620 High_Bound =>
6621 Make_Integer_Literal (Loc, Intval => Last_Repval))),
6623 Statements => New_List (
6624 Make_Simple_Return_Statement (Loc,
6625 Expression => Pos_Expr))));
6627 else
6628 Ent := First_Literal (Typ);
6629 while Present (Ent) loop
6630 Append_To (Lst,
6631 Make_Case_Statement_Alternative (Loc,
6632 Discrete_Choices => New_List (
6633 Make_Integer_Literal (Sloc (Enumeration_Rep_Expr (Ent)),
6634 Intval => Enumeration_Rep (Ent))),
6636 Statements => New_List (
6637 Make_Simple_Return_Statement (Loc,
6638 Expression =>
6639 Make_Integer_Literal (Loc,
6640 Intval => Enumeration_Pos (Ent))))));
6642 Next_Literal (Ent);
6643 end loop;
6644 end if;
6646 -- In normal mode, add the others clause with the test
6648 if not No_Exception_Handlers_Set then
6649 Append_To (Lst,
6650 Make_Case_Statement_Alternative (Loc,
6651 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
6652 Statements => New_List (
6653 Make_Raise_Constraint_Error (Loc,
6654 Condition => Make_Identifier (Loc, Name_uF),
6655 Reason => CE_Invalid_Data),
6656 Make_Simple_Return_Statement (Loc,
6657 Expression =>
6658 Make_Integer_Literal (Loc, -1)))));
6660 -- If either of the restrictions No_Exceptions_Handlers/Propagation is
6661 -- active then return -1 (we cannot usefully raise Constraint_Error in
6662 -- this case). See description above for further details.
6664 else
6665 Append_To (Lst,
6666 Make_Case_Statement_Alternative (Loc,
6667 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
6668 Statements => New_List (
6669 Make_Simple_Return_Statement (Loc,
6670 Expression =>
6671 Make_Integer_Literal (Loc, -1)))));
6672 end if;
6674 -- Now we can build the function body
6676 Fent :=
6677 Make_Defining_Identifier (Loc, Make_TSS_Name (Typ, TSS_Rep_To_Pos));
6679 Func :=
6680 Make_Subprogram_Body (Loc,
6681 Specification =>
6682 Make_Function_Specification (Loc,
6683 Defining_Unit_Name => Fent,
6684 Parameter_Specifications => New_List (
6685 Make_Parameter_Specification (Loc,
6686 Defining_Identifier =>
6687 Make_Defining_Identifier (Loc, Name_uA),
6688 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
6689 Make_Parameter_Specification (Loc,
6690 Defining_Identifier =>
6691 Make_Defining_Identifier (Loc, Name_uF),
6692 Parameter_Type =>
6693 New_Occurrence_Of (Standard_Boolean, Loc))),
6695 Result_Definition => New_Occurrence_Of (Standard_Integer, Loc)),
6697 Declarations => Empty_List,
6699 Handled_Statement_Sequence =>
6700 Make_Handled_Sequence_Of_Statements (Loc,
6701 Statements => New_List (
6702 Make_Case_Statement (Loc,
6703 Expression =>
6704 Unchecked_Convert_To
6705 (Ityp, Make_Identifier (Loc, Name_uA)),
6706 Alternatives => Lst))));
6708 Set_TSS (Typ, Fent);
6710 -- Set Pure flag (it will be reset if the current context is not Pure).
6711 -- We also pretend there was a pragma Pure_Function so that for purposes
6712 -- of optimization and constant-folding, we will consider the function
6713 -- Pure even if we are not in a Pure context).
6715 Set_Is_Pure (Fent);
6716 Set_Has_Pragma_Pure_Function (Fent);
6718 -- Unless we are in -gnatD mode, where we are debugging generated code,
6719 -- this is an internal entity for which we don't need debug info.
6721 if not Debug_Generated_Code then
6722 Set_Debug_Info_Off (Fent);
6723 end if;
6725 exception
6726 when RE_Not_Available =>
6727 return;
6728 end Expand_Freeze_Enumeration_Type;
6730 -------------------------------
6731 -- Expand_Freeze_Record_Type --
6732 -------------------------------
6734 procedure Expand_Freeze_Record_Type (N : Node_Id) is
6735 Def_Id : constant Node_Id := Entity (N);
6736 Type_Decl : constant Node_Id := Parent (Def_Id);
6737 Comp : Entity_Id;
6738 Comp_Typ : Entity_Id;
6739 Has_AACC : Boolean;
6740 Predef_List : List_Id;
6742 Renamed_Eq : Node_Id := Empty;
6743 -- Defining unit name for the predefined equality function in the case
6744 -- where the type has a primitive operation that is a renaming of
6745 -- predefined equality (but only if there is also an overriding
6746 -- user-defined equality function). Used to pass this entity from
6747 -- Make_Predefined_Primitive_Specs to Predefined_Primitive_Bodies.
6749 Wrapper_Decl_List : List_Id := No_List;
6750 Wrapper_Body_List : List_Id := No_List;
6752 -- Start of processing for Expand_Freeze_Record_Type
6754 begin
6755 -- Build discriminant checking functions if not a derived type (for
6756 -- derived types that are not tagged types, always use the discriminant
6757 -- checking functions of the parent type). However, for untagged types
6758 -- the derivation may have taken place before the parent was frozen, so
6759 -- we copy explicitly the discriminant checking functions from the
6760 -- parent into the components of the derived type.
6762 if not Is_Derived_Type (Def_Id)
6763 or else Has_New_Non_Standard_Rep (Def_Id)
6764 or else Is_Tagged_Type (Def_Id)
6765 then
6766 Build_Discr_Checking_Funcs (Type_Decl);
6768 elsif Is_Derived_Type (Def_Id)
6769 and then not Is_Tagged_Type (Def_Id)
6771 -- If we have a derived Unchecked_Union, we do not inherit the
6772 -- discriminant checking functions from the parent type since the
6773 -- discriminants are non existent.
6775 and then not Is_Unchecked_Union (Def_Id)
6776 and then Has_Discriminants (Def_Id)
6777 then
6778 declare
6779 Old_Comp : Entity_Id;
6781 begin
6782 Old_Comp :=
6783 First_Component (Base_Type (Underlying_Type (Etype (Def_Id))));
6784 Comp := First_Component (Def_Id);
6785 while Present (Comp) loop
6786 if Ekind (Comp) = E_Component
6787 and then Chars (Comp) = Chars (Old_Comp)
6788 then
6789 Set_Discriminant_Checking_Func (Comp,
6790 Discriminant_Checking_Func (Old_Comp));
6791 end if;
6793 Next_Component (Old_Comp);
6794 Next_Component (Comp);
6795 end loop;
6796 end;
6797 end if;
6799 if Is_Derived_Type (Def_Id)
6800 and then Is_Limited_Type (Def_Id)
6801 and then Is_Tagged_Type (Def_Id)
6802 then
6803 Check_Stream_Attributes (Def_Id);
6804 end if;
6806 -- Update task, protected, and controlled component flags, because some
6807 -- of the component types may have been private at the point of the
6808 -- record declaration. Detect anonymous access-to-controlled components.
6810 Has_AACC := False;
6812 Comp := First_Component (Def_Id);
6813 while Present (Comp) loop
6814 Comp_Typ := Etype (Comp);
6816 if Has_Task (Comp_Typ) then
6817 Set_Has_Task (Def_Id);
6818 end if;
6820 if Has_Protected (Comp_Typ) then
6821 Set_Has_Protected (Def_Id);
6822 end if;
6824 -- Do not set Has_Controlled_Component on a class-wide equivalent
6825 -- type. See Make_CW_Equivalent_Type.
6827 if not Is_Class_Wide_Equivalent_Type (Def_Id)
6828 and then (Has_Controlled_Component (Comp_Typ)
6829 or else (Chars (Comp) /= Name_uParent
6830 and then Is_Controlled (Comp_Typ)))
6831 then
6832 Set_Has_Controlled_Component (Def_Id);
6833 end if;
6835 -- Non-self-referential anonymous access-to-controlled component
6837 if Ekind (Comp_Typ) = E_Anonymous_Access_Type
6838 and then Needs_Finalization (Designated_Type (Comp_Typ))
6839 and then Designated_Type (Comp_Typ) /= Def_Id
6840 then
6841 Has_AACC := True;
6842 end if;
6844 Next_Component (Comp);
6845 end loop;
6847 -- Handle constructors of untagged CPP_Class types
6849 if not Is_Tagged_Type (Def_Id) and then Is_CPP_Class (Def_Id) then
6850 Set_CPP_Constructors (Def_Id);
6851 end if;
6853 -- Creation of the Dispatch Table. Note that a Dispatch Table is built
6854 -- for regular tagged types as well as for Ada types deriving from a C++
6855 -- Class, but not for tagged types directly corresponding to C++ classes
6856 -- In the later case we assume that it is created in the C++ side and we
6857 -- just use it.
6859 if Is_Tagged_Type (Def_Id) then
6861 -- Add the _Tag component
6863 if Underlying_Type (Etype (Def_Id)) = Def_Id then
6864 Expand_Tagged_Root (Def_Id);
6865 end if;
6867 if Is_CPP_Class (Def_Id) then
6868 Set_All_DT_Position (Def_Id);
6870 -- Create the tag entities with a minimum decoration
6872 if Tagged_Type_Expansion then
6873 Append_Freeze_Actions (Def_Id, Make_Tags (Def_Id));
6874 end if;
6876 Set_CPP_Constructors (Def_Id);
6878 else
6879 if not Building_Static_DT (Def_Id) then
6881 -- Usually inherited primitives are not delayed but the first
6882 -- Ada extension of a CPP_Class is an exception since the
6883 -- address of the inherited subprogram has to be inserted in
6884 -- the new Ada Dispatch Table and this is a freezing action.
6886 -- Similarly, if this is an inherited operation whose parent is
6887 -- not frozen yet, it is not in the DT of the parent, and we
6888 -- generate an explicit freeze node for the inherited operation
6889 -- so it is properly inserted in the DT of the current type.
6891 declare
6892 Elmt : Elmt_Id;
6893 Subp : Entity_Id;
6895 begin
6896 Elmt := First_Elmt (Primitive_Operations (Def_Id));
6897 while Present (Elmt) loop
6898 Subp := Node (Elmt);
6900 if Present (Alias (Subp)) then
6901 if Is_CPP_Class (Etype (Def_Id)) then
6902 Set_Has_Delayed_Freeze (Subp);
6904 elsif Has_Delayed_Freeze (Alias (Subp))
6905 and then not Is_Frozen (Alias (Subp))
6906 then
6907 Set_Is_Frozen (Subp, False);
6908 Set_Has_Delayed_Freeze (Subp);
6909 end if;
6910 end if;
6912 Next_Elmt (Elmt);
6913 end loop;
6914 end;
6915 end if;
6917 -- Unfreeze momentarily the type to add the predefined primitives
6918 -- operations. The reason we unfreeze is so that these predefined
6919 -- operations will indeed end up as primitive operations (which
6920 -- must be before the freeze point).
6922 Set_Is_Frozen (Def_Id, False);
6924 -- Do not add the spec of predefined primitives in case of
6925 -- CPP tagged type derivations that have convention CPP.
6927 if Is_CPP_Class (Root_Type (Def_Id))
6928 and then Convention (Def_Id) = Convention_CPP
6929 then
6930 null;
6932 -- Do not add the spec of predefined primitives in case of
6933 -- CIL and Java tagged types
6935 elsif Convention (Def_Id) = Convention_CIL
6936 or else Convention (Def_Id) = Convention_Java
6937 then
6938 null;
6940 -- Do not add the spec of the predefined primitives if we are
6941 -- compiling under restriction No_Dispatching_Calls.
6943 elsif not Restriction_Active (No_Dispatching_Calls) then
6944 Make_Predefined_Primitive_Specs
6945 (Def_Id, Predef_List, Renamed_Eq);
6946 Insert_List_Before_And_Analyze (N, Predef_List);
6947 end if;
6949 -- Ada 2005 (AI-391): For a nonabstract null extension, create
6950 -- wrapper functions for each nonoverridden inherited function
6951 -- with a controlling result of the type. The wrapper for such
6952 -- a function returns an extension aggregate that invokes the
6953 -- parent function.
6955 if Ada_Version >= Ada_2005
6956 and then not Is_Abstract_Type (Def_Id)
6957 and then Is_Null_Extension (Def_Id)
6958 then
6959 Make_Controlling_Function_Wrappers
6960 (Def_Id, Wrapper_Decl_List, Wrapper_Body_List);
6961 Insert_List_Before_And_Analyze (N, Wrapper_Decl_List);
6962 end if;
6964 -- Ada 2005 (AI-251): For a nonabstract type extension, build
6965 -- null procedure declarations for each set of homographic null
6966 -- procedures that are inherited from interface types but not
6967 -- overridden. This is done to ensure that the dispatch table
6968 -- entry associated with such null primitives are properly filled.
6970 if Ada_Version >= Ada_2005
6971 and then Etype (Def_Id) /= Def_Id
6972 and then not Is_Abstract_Type (Def_Id)
6973 and then Has_Interfaces (Def_Id)
6974 then
6975 Insert_Actions (N, Make_Null_Procedure_Specs (Def_Id));
6976 end if;
6978 Set_Is_Frozen (Def_Id);
6979 if not Is_Derived_Type (Def_Id)
6980 or else Is_Tagged_Type (Etype (Def_Id))
6981 then
6982 Set_All_DT_Position (Def_Id);
6984 -- If this is a type derived from an untagged private type whose
6985 -- full view is tagged, the type is marked tagged for layout
6986 -- reasons, but it has no dispatch table.
6988 elsif Is_Derived_Type (Def_Id)
6989 and then Is_Private_Type (Etype (Def_Id))
6990 and then not Is_Tagged_Type (Etype (Def_Id))
6991 then
6992 return;
6993 end if;
6995 -- Create and decorate the tags. Suppress their creation when
6996 -- VM_Target because the dispatching mechanism is handled
6997 -- internally by the VMs.
6999 if Tagged_Type_Expansion then
7000 Append_Freeze_Actions (Def_Id, Make_Tags (Def_Id));
7002 -- Generate dispatch table of locally defined tagged type.
7003 -- Dispatch tables of library level tagged types are built
7004 -- later (see Analyze_Declarations).
7006 if not Building_Static_DT (Def_Id) then
7007 Append_Freeze_Actions (Def_Id, Make_DT (Def_Id));
7008 end if;
7010 elsif VM_Target /= No_VM then
7011 Append_Freeze_Actions (Def_Id, Make_VM_TSD (Def_Id));
7012 end if;
7014 -- If the type has unknown discriminants, propagate dispatching
7015 -- information to its underlying record view, which does not get
7016 -- its own dispatch table.
7018 if Is_Derived_Type (Def_Id)
7019 and then Has_Unknown_Discriminants (Def_Id)
7020 and then Present (Underlying_Record_View (Def_Id))
7021 then
7022 declare
7023 Rep : constant Entity_Id := Underlying_Record_View (Def_Id);
7024 begin
7025 Set_Access_Disp_Table
7026 (Rep, Access_Disp_Table (Def_Id));
7027 Set_Dispatch_Table_Wrappers
7028 (Rep, Dispatch_Table_Wrappers (Def_Id));
7029 Set_Direct_Primitive_Operations
7030 (Rep, Direct_Primitive_Operations (Def_Id));
7031 end;
7032 end if;
7034 -- Make sure that the primitives Initialize, Adjust and Finalize
7035 -- are Frozen before other TSS subprograms. We don't want them
7036 -- Frozen inside.
7038 if Is_Controlled (Def_Id) then
7039 if not Is_Limited_Type (Def_Id) then
7040 Append_Freeze_Actions (Def_Id,
7041 Freeze_Entity
7042 (Find_Prim_Op (Def_Id, Name_Adjust), Def_Id));
7043 end if;
7045 Append_Freeze_Actions (Def_Id,
7046 Freeze_Entity
7047 (Find_Prim_Op (Def_Id, Name_Initialize), Def_Id));
7049 Append_Freeze_Actions (Def_Id,
7050 Freeze_Entity
7051 (Find_Prim_Op (Def_Id, Name_Finalize), Def_Id));
7052 end if;
7054 -- Freeze rest of primitive operations. There is no need to handle
7055 -- the predefined primitives if we are compiling under restriction
7056 -- No_Dispatching_Calls.
7058 if not Restriction_Active (No_Dispatching_Calls) then
7059 Append_Freeze_Actions
7060 (Def_Id, Predefined_Primitive_Freeze (Def_Id));
7061 end if;
7062 end if;
7064 -- In the untagged case, ever since Ada 83 an equality function must
7065 -- be provided for variant records that are not unchecked unions.
7066 -- In Ada 2012 the equality function composes, and thus must be built
7067 -- explicitly just as for tagged records.
7069 elsif Has_Discriminants (Def_Id)
7070 and then not Is_Limited_Type (Def_Id)
7071 then
7072 declare
7073 Comps : constant Node_Id :=
7074 Component_List (Type_Definition (Type_Decl));
7075 begin
7076 if Present (Comps)
7077 and then Present (Variant_Part (Comps))
7078 then
7079 Build_Variant_Record_Equality (Def_Id);
7080 end if;
7081 end;
7083 -- Otherwise create primitive equality operation (AI05-0123)
7085 -- This is done unconditionally to ensure that tools can be linked
7086 -- properly with user programs compiled with older language versions.
7087 -- In addition, this is needed because "=" composes for bounded strings
7088 -- in all language versions (see Exp_Ch4.Expand_Composite_Equality).
7090 elsif Comes_From_Source (Def_Id)
7091 and then Convention (Def_Id) = Convention_Ada
7092 and then not Is_Limited_Type (Def_Id)
7093 then
7094 Build_Untagged_Equality (Def_Id);
7095 end if;
7097 -- Before building the record initialization procedure, if we are
7098 -- dealing with a concurrent record value type, then we must go through
7099 -- the discriminants, exchanging discriminals between the concurrent
7100 -- type and the concurrent record value type. See the section "Handling
7101 -- of Discriminants" in the Einfo spec for details.
7103 if Is_Concurrent_Record_Type (Def_Id)
7104 and then Has_Discriminants (Def_Id)
7105 then
7106 declare
7107 Ctyp : constant Entity_Id :=
7108 Corresponding_Concurrent_Type (Def_Id);
7109 Conc_Discr : Entity_Id;
7110 Rec_Discr : Entity_Id;
7111 Temp : Entity_Id;
7113 begin
7114 Conc_Discr := First_Discriminant (Ctyp);
7115 Rec_Discr := First_Discriminant (Def_Id);
7116 while Present (Conc_Discr) loop
7117 Temp := Discriminal (Conc_Discr);
7118 Set_Discriminal (Conc_Discr, Discriminal (Rec_Discr));
7119 Set_Discriminal (Rec_Discr, Temp);
7121 Set_Discriminal_Link (Discriminal (Conc_Discr), Conc_Discr);
7122 Set_Discriminal_Link (Discriminal (Rec_Discr), Rec_Discr);
7124 Next_Discriminant (Conc_Discr);
7125 Next_Discriminant (Rec_Discr);
7126 end loop;
7127 end;
7128 end if;
7130 if Has_Controlled_Component (Def_Id) then
7131 Build_Controlling_Procs (Def_Id);
7132 end if;
7134 Adjust_Discriminants (Def_Id);
7136 if Tagged_Type_Expansion or else not Is_Interface (Def_Id) then
7138 -- Do not need init for interfaces on e.g. CIL since they're
7139 -- abstract. Helps operation of peverify (the PE Verify tool).
7141 Build_Record_Init_Proc (Type_Decl, Def_Id);
7142 end if;
7144 -- For tagged type that are not interfaces, build bodies of primitive
7145 -- operations. Note: do this after building the record initialization
7146 -- procedure, since the primitive operations may need the initialization
7147 -- routine. There is no need to add predefined primitives of interfaces
7148 -- because all their predefined primitives are abstract.
7150 if Is_Tagged_Type (Def_Id) and then not Is_Interface (Def_Id) then
7152 -- Do not add the body of predefined primitives in case of CPP tagged
7153 -- type derivations that have convention CPP.
7155 if Is_CPP_Class (Root_Type (Def_Id))
7156 and then Convention (Def_Id) = Convention_CPP
7157 then
7158 null;
7160 -- Do not add the body of predefined primitives in case of CIL and
7161 -- Java tagged types.
7163 elsif Convention (Def_Id) = Convention_CIL
7164 or else Convention (Def_Id) = Convention_Java
7165 then
7166 null;
7168 -- Do not add the body of the predefined primitives if we are
7169 -- compiling under restriction No_Dispatching_Calls or if we are
7170 -- compiling a CPP tagged type.
7172 elsif not Restriction_Active (No_Dispatching_Calls) then
7174 -- Create the body of TSS primitive Finalize_Address. This must
7175 -- be done before the bodies of all predefined primitives are
7176 -- created. If Def_Id is limited, Stream_Input and Stream_Read
7177 -- may produce build-in-place allocations and for those the
7178 -- expander needs Finalize_Address.
7180 Make_Finalize_Address_Body (Def_Id);
7181 Predef_List := Predefined_Primitive_Bodies (Def_Id, Renamed_Eq);
7182 Append_Freeze_Actions (Def_Id, Predef_List);
7183 end if;
7185 -- Ada 2005 (AI-391): If any wrappers were created for nonoverridden
7186 -- inherited functions, then add their bodies to the freeze actions.
7188 if Present (Wrapper_Body_List) then
7189 Append_Freeze_Actions (Def_Id, Wrapper_Body_List);
7190 end if;
7192 -- Create extra formals for the primitive operations of the type.
7193 -- This must be done before analyzing the body of the initialization
7194 -- procedure, because a self-referential type might call one of these
7195 -- primitives in the body of the init_proc itself.
7197 declare
7198 Elmt : Elmt_Id;
7199 Subp : Entity_Id;
7201 begin
7202 Elmt := First_Elmt (Primitive_Operations (Def_Id));
7203 while Present (Elmt) loop
7204 Subp := Node (Elmt);
7205 if not Has_Foreign_Convention (Subp)
7206 and then not Is_Predefined_Dispatching_Operation (Subp)
7207 then
7208 Create_Extra_Formals (Subp);
7209 end if;
7211 Next_Elmt (Elmt);
7212 end loop;
7213 end;
7214 end if;
7216 -- Create a heterogeneous finalization master to service the anonymous
7217 -- access-to-controlled components of the record type.
7219 if Has_AACC then
7220 declare
7221 Encl_Scope : constant Entity_Id := Scope (Def_Id);
7222 Ins_Node : constant Node_Id := Parent (Def_Id);
7223 Loc : constant Source_Ptr := Sloc (Def_Id);
7224 Fin_Mas_Id : Entity_Id;
7226 Attributes_Set : Boolean := False;
7227 Master_Built : Boolean := False;
7228 -- Two flags which control the creation and initialization of a
7229 -- common heterogeneous master.
7231 begin
7232 Comp := First_Component (Def_Id);
7233 while Present (Comp) loop
7234 Comp_Typ := Etype (Comp);
7236 -- A non-self-referential anonymous access-to-controlled
7237 -- component.
7239 if Ekind (Comp_Typ) = E_Anonymous_Access_Type
7240 and then Needs_Finalization (Designated_Type (Comp_Typ))
7241 and then Designated_Type (Comp_Typ) /= Def_Id
7242 then
7243 if VM_Target = No_VM then
7245 -- Build a homogeneous master for the first anonymous
7246 -- access-to-controlled component. This master may be
7247 -- converted into a heterogeneous collection if more
7248 -- components are to follow.
7250 if not Master_Built then
7251 Master_Built := True;
7253 -- All anonymous access-to-controlled types allocate
7254 -- on the global pool. Note that the finalization
7255 -- master and the associated storage pool must be set
7256 -- on the root type (both are "root type only").
7258 Set_Associated_Storage_Pool
7259 (Root_Type (Comp_Typ), RTE (RE_Global_Pool_Object));
7261 Build_Finalization_Master
7262 (Typ => Root_Type (Comp_Typ),
7263 Ins_Node => Ins_Node,
7264 Encl_Scope => Encl_Scope);
7266 Fin_Mas_Id := Finalization_Master (Comp_Typ);
7268 -- Subsequent anonymous access-to-controlled components
7269 -- reuse the available master.
7271 else
7272 -- All anonymous access-to-controlled types allocate
7273 -- on the global pool. Note that both the finalization
7274 -- master and the associated storage pool must be set
7275 -- on the root type (both are "root type only").
7277 Set_Associated_Storage_Pool
7278 (Root_Type (Comp_Typ), RTE (RE_Global_Pool_Object));
7280 -- Shared the master among multiple components
7282 Set_Finalization_Master
7283 (Root_Type (Comp_Typ), Fin_Mas_Id);
7285 -- Convert the master into a heterogeneous collection.
7286 -- Generate:
7287 -- Set_Is_Heterogeneous (<Fin_Mas_Id>);
7289 if not Attributes_Set then
7290 Attributes_Set := True;
7292 Insert_Action (Ins_Node,
7293 Make_Procedure_Call_Statement (Loc,
7294 Name =>
7295 New_Occurrence_Of
7296 (RTE (RE_Set_Is_Heterogeneous), Loc),
7297 Parameter_Associations => New_List (
7298 New_Occurrence_Of (Fin_Mas_Id, Loc))));
7299 end if;
7300 end if;
7302 -- Since .NET/JVM targets do not support heterogeneous
7303 -- masters, each component must have its own master.
7305 else
7306 Build_Finalization_Master
7307 (Typ => Comp_Typ,
7308 Ins_Node => Ins_Node,
7309 Encl_Scope => Encl_Scope);
7310 end if;
7311 end if;
7313 Next_Component (Comp);
7314 end loop;
7315 end;
7316 end if;
7318 -- Check whether individual components have a defined invariant, and add
7319 -- the corresponding component invariant checks.
7321 -- Do not create an invariant procedure for some internally generated
7322 -- subtypes, in particular those created for objects of a class-wide
7323 -- type. Such types may have components to which invariant apply, but
7324 -- the corresponding checks will be applied when an object of the parent
7325 -- type is constructed.
7327 -- Such objects will show up in a class-wide postcondition, and the
7328 -- invariant will be checked, if necessary, upon return from the
7329 -- enclosing subprogram.
7331 if not Is_Class_Wide_Equivalent_Type (Def_Id) then
7332 Insert_Component_Invariant_Checks
7333 (N, Def_Id, Build_Record_Invariant_Proc (Def_Id, N));
7334 end if;
7335 end Expand_Freeze_Record_Type;
7337 ------------------------------
7338 -- Freeze_Stream_Operations --
7339 ------------------------------
7341 procedure Freeze_Stream_Operations (N : Node_Id; Typ : Entity_Id) is
7342 Names : constant array (1 .. 4) of TSS_Name_Type :=
7343 (TSS_Stream_Input,
7344 TSS_Stream_Output,
7345 TSS_Stream_Read,
7346 TSS_Stream_Write);
7347 Stream_Op : Entity_Id;
7349 begin
7350 -- Primitive operations of tagged types are frozen when the dispatch
7351 -- table is constructed.
7353 if not Comes_From_Source (Typ) or else Is_Tagged_Type (Typ) then
7354 return;
7355 end if;
7357 for J in Names'Range loop
7358 Stream_Op := TSS (Typ, Names (J));
7360 if Present (Stream_Op)
7361 and then Is_Subprogram (Stream_Op)
7362 and then Nkind (Unit_Declaration_Node (Stream_Op)) =
7363 N_Subprogram_Declaration
7364 and then not Is_Frozen (Stream_Op)
7365 then
7366 Append_Freeze_Actions (Typ, Freeze_Entity (Stream_Op, N));
7367 end if;
7368 end loop;
7369 end Freeze_Stream_Operations;
7371 -----------------
7372 -- Freeze_Type --
7373 -----------------
7375 -- Full type declarations are expanded at the point at which the type is
7376 -- frozen. The formal N is the Freeze_Node for the type. Any statements or
7377 -- declarations generated by the freezing (e.g. the procedure generated
7378 -- for initialization) are chained in the Actions field list of the freeze
7379 -- node using Append_Freeze_Actions.
7381 function Freeze_Type (N : Node_Id) return Boolean is
7382 Def_Id : constant Entity_Id := Entity (N);
7383 RACW_Seen : Boolean := False;
7384 Result : Boolean := False;
7386 begin
7387 -- Process associated access types needing special processing
7389 if Present (Access_Types_To_Process (N)) then
7390 declare
7391 E : Elmt_Id := First_Elmt (Access_Types_To_Process (N));
7393 begin
7394 while Present (E) loop
7395 if Is_Remote_Access_To_Class_Wide_Type (Node (E)) then
7396 Validate_RACW_Primitives (Node (E));
7397 RACW_Seen := True;
7398 end if;
7400 E := Next_Elmt (E);
7401 end loop;
7402 end;
7404 -- If there are RACWs designating this type, make stubs now
7406 if RACW_Seen then
7407 Remote_Types_Tagged_Full_View_Encountered (Def_Id);
7408 end if;
7409 end if;
7411 -- Freeze processing for record types
7413 if Is_Record_Type (Def_Id) then
7414 if Ekind (Def_Id) = E_Record_Type then
7415 Expand_Freeze_Record_Type (N);
7416 elsif Is_Class_Wide_Type (Def_Id) then
7417 Expand_Freeze_Class_Wide_Type (N);
7418 end if;
7420 -- Freeze processing for array types
7422 elsif Is_Array_Type (Def_Id) then
7423 Expand_Freeze_Array_Type (N);
7425 -- Freeze processing for access types
7427 -- For pool-specific access types, find out the pool object used for
7428 -- this type, needs actual expansion of it in some cases. Here are the
7429 -- different cases :
7431 -- 1. Rep Clause "for Def_Id'Storage_Size use 0;"
7432 -- ---> don't use any storage pool
7434 -- 2. Rep Clause : for Def_Id'Storage_Size use Expr.
7435 -- Expand:
7436 -- Def_Id__Pool : Stack_Bounded_Pool (Expr, DT'Size, DT'Alignment);
7438 -- 3. Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
7439 -- ---> Storage Pool is the specified one
7441 -- See GNAT Pool packages in the Run-Time for more details
7443 elsif Ekind_In (Def_Id, E_Access_Type, E_General_Access_Type) then
7444 declare
7445 Loc : constant Source_Ptr := Sloc (N);
7446 Desig_Type : constant Entity_Id := Designated_Type (Def_Id);
7447 Pool_Object : Entity_Id;
7449 Freeze_Action_Typ : Entity_Id;
7451 begin
7452 -- Case 1
7454 -- Rep Clause "for Def_Id'Storage_Size use 0;"
7455 -- ---> don't use any storage pool
7457 if No_Pool_Assigned (Def_Id) then
7458 null;
7460 -- Case 2
7462 -- Rep Clause : for Def_Id'Storage_Size use Expr.
7463 -- ---> Expand:
7464 -- Def_Id__Pool : Stack_Bounded_Pool
7465 -- (Expr, DT'Size, DT'Alignment);
7467 elsif Has_Storage_Size_Clause (Def_Id) then
7468 declare
7469 DT_Size : Node_Id;
7470 DT_Align : Node_Id;
7472 begin
7473 -- For unconstrained composite types we give a size of zero
7474 -- so that the pool knows that it needs a special algorithm
7475 -- for variable size object allocation.
7477 if Is_Composite_Type (Desig_Type)
7478 and then not Is_Constrained (Desig_Type)
7479 then
7480 DT_Size := Make_Integer_Literal (Loc, 0);
7481 DT_Align := Make_Integer_Literal (Loc, Maximum_Alignment);
7483 else
7484 DT_Size :=
7485 Make_Attribute_Reference (Loc,
7486 Prefix => New_Occurrence_Of (Desig_Type, Loc),
7487 Attribute_Name => Name_Max_Size_In_Storage_Elements);
7489 DT_Align :=
7490 Make_Attribute_Reference (Loc,
7491 Prefix => New_Occurrence_Of (Desig_Type, Loc),
7492 Attribute_Name => Name_Alignment);
7493 end if;
7495 Pool_Object :=
7496 Make_Defining_Identifier (Loc,
7497 Chars => New_External_Name (Chars (Def_Id), 'P'));
7499 -- We put the code associated with the pools in the entity
7500 -- that has the later freeze node, usually the access type
7501 -- but it can also be the designated_type; because the pool
7502 -- code requires both those types to be frozen
7504 if Is_Frozen (Desig_Type)
7505 and then (No (Freeze_Node (Desig_Type))
7506 or else Analyzed (Freeze_Node (Desig_Type)))
7507 then
7508 Freeze_Action_Typ := Def_Id;
7510 -- A Taft amendment type cannot get the freeze actions
7511 -- since the full view is not there.
7513 elsif Is_Incomplete_Or_Private_Type (Desig_Type)
7514 and then No (Full_View (Desig_Type))
7515 then
7516 Freeze_Action_Typ := Def_Id;
7518 else
7519 Freeze_Action_Typ := Desig_Type;
7520 end if;
7522 Append_Freeze_Action (Freeze_Action_Typ,
7523 Make_Object_Declaration (Loc,
7524 Defining_Identifier => Pool_Object,
7525 Object_Definition =>
7526 Make_Subtype_Indication (Loc,
7527 Subtype_Mark =>
7528 New_Occurrence_Of
7529 (RTE (RE_Stack_Bounded_Pool), Loc),
7531 Constraint =>
7532 Make_Index_Or_Discriminant_Constraint (Loc,
7533 Constraints => New_List (
7535 -- First discriminant is the Pool Size
7537 New_Occurrence_Of (
7538 Storage_Size_Variable (Def_Id), Loc),
7540 -- Second discriminant is the element size
7542 DT_Size,
7544 -- Third discriminant is the alignment
7546 DT_Align)))));
7547 end;
7549 Set_Associated_Storage_Pool (Def_Id, Pool_Object);
7551 -- Case 3
7553 -- Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
7554 -- ---> Storage Pool is the specified one
7556 -- When compiling in Ada 2012 mode, ensure that the accessibility
7557 -- level of the subpool access type is not deeper than that of the
7558 -- pool_with_subpools.
7560 elsif Ada_Version >= Ada_2012
7561 and then Present (Associated_Storage_Pool (Def_Id))
7563 -- Omit this check on .NET/JVM where pools are not supported
7565 and then VM_Target = No_VM
7567 -- Omit this check for the case of a configurable run-time that
7568 -- does not provide package System.Storage_Pools.Subpools.
7570 and then RTE_Available (RE_Root_Storage_Pool_With_Subpools)
7571 then
7572 declare
7573 Loc : constant Source_Ptr := Sloc (Def_Id);
7574 Pool : constant Entity_Id :=
7575 Associated_Storage_Pool (Def_Id);
7576 RSPWS : constant Entity_Id :=
7577 RTE (RE_Root_Storage_Pool_With_Subpools);
7579 begin
7580 -- It is known that the accessibility level of the access
7581 -- type is deeper than that of the pool.
7583 if Type_Access_Level (Def_Id) > Object_Access_Level (Pool)
7584 and then not Accessibility_Checks_Suppressed (Def_Id)
7585 and then not Accessibility_Checks_Suppressed (Pool)
7586 then
7587 -- Static case: the pool is known to be a descendant of
7588 -- Root_Storage_Pool_With_Subpools.
7590 if Is_Ancestor (RSPWS, Etype (Pool)) then
7591 Error_Msg_N
7592 ("??subpool access type has deeper accessibility "
7593 & "level than pool", Def_Id);
7595 Append_Freeze_Action (Def_Id,
7596 Make_Raise_Program_Error (Loc,
7597 Reason => PE_Accessibility_Check_Failed));
7599 -- Dynamic case: when the pool is of a class-wide type,
7600 -- it may or may not support subpools depending on the
7601 -- path of derivation. Generate:
7603 -- if Def_Id in RSPWS'Class then
7604 -- raise Program_Error;
7605 -- end if;
7607 elsif Is_Class_Wide_Type (Etype (Pool)) then
7608 Append_Freeze_Action (Def_Id,
7609 Make_If_Statement (Loc,
7610 Condition =>
7611 Make_In (Loc,
7612 Left_Opnd => New_Occurrence_Of (Pool, Loc),
7613 Right_Opnd =>
7614 New_Occurrence_Of
7615 (Class_Wide_Type (RSPWS), Loc)),
7617 Then_Statements => New_List (
7618 Make_Raise_Program_Error (Loc,
7619 Reason => PE_Accessibility_Check_Failed))));
7620 end if;
7621 end if;
7622 end;
7623 end if;
7625 -- For access-to-controlled types (including class-wide types and
7626 -- Taft-amendment types, which potentially have controlled
7627 -- components), expand the list controller object that will store
7628 -- the dynamically allocated objects. Don't do this transformation
7629 -- for expander-generated access types, but do it for types that
7630 -- are the full view of types derived from other private types.
7631 -- Also suppress the list controller in the case of a designated
7632 -- type with convention Java, since this is used when binding to
7633 -- Java API specs, where there's no equivalent of a finalization
7634 -- list and we don't want to pull in the finalization support if
7635 -- not needed.
7637 if not Comes_From_Source (Def_Id)
7638 and then not Has_Private_Declaration (Def_Id)
7639 then
7640 null;
7642 -- An exception is made for types defined in the run-time because
7643 -- Ada.Tags.Tag itself is such a type and cannot afford this
7644 -- unnecessary overhead that would generates a loop in the
7645 -- expansion scheme. Another exception is if Restrictions
7646 -- (No_Finalization) is active, since then we know nothing is
7647 -- controlled.
7649 elsif Restriction_Active (No_Finalization)
7650 or else In_Runtime (Def_Id)
7651 then
7652 null;
7654 -- Assume that incomplete and private types are always completed
7655 -- by a controlled full view.
7657 elsif Needs_Finalization (Desig_Type)
7658 or else
7659 (Is_Incomplete_Or_Private_Type (Desig_Type)
7660 and then No (Full_View (Desig_Type)))
7661 or else
7662 (Is_Array_Type (Desig_Type)
7663 and then Needs_Finalization (Component_Type (Desig_Type)))
7664 then
7665 Build_Finalization_Master (Def_Id);
7666 end if;
7667 end;
7669 -- Freeze processing for enumeration types
7671 elsif Ekind (Def_Id) = E_Enumeration_Type then
7673 -- We only have something to do if we have a non-standard
7674 -- representation (i.e. at least one literal whose pos value
7675 -- is not the same as its representation)
7677 if Has_Non_Standard_Rep (Def_Id) then
7678 Expand_Freeze_Enumeration_Type (N);
7679 end if;
7681 -- Private types that are completed by a derivation from a private
7682 -- type have an internally generated full view, that needs to be
7683 -- frozen. This must be done explicitly because the two views share
7684 -- the freeze node, and the underlying full view is not visible when
7685 -- the freeze node is analyzed.
7687 elsif Is_Private_Type (Def_Id)
7688 and then Is_Derived_Type (Def_Id)
7689 and then Present (Full_View (Def_Id))
7690 and then Is_Itype (Full_View (Def_Id))
7691 and then Has_Private_Declaration (Full_View (Def_Id))
7692 and then Freeze_Node (Full_View (Def_Id)) = N
7693 then
7694 Set_Entity (N, Full_View (Def_Id));
7695 Result := Freeze_Type (N);
7696 Set_Entity (N, Def_Id);
7698 -- All other types require no expander action. There are such cases
7699 -- (e.g. task types and protected types). In such cases, the freeze
7700 -- nodes are there for use by Gigi.
7702 end if;
7704 Freeze_Stream_Operations (N, Def_Id);
7705 return Result;
7707 exception
7708 when RE_Not_Available =>
7709 return False;
7710 end Freeze_Type;
7712 -------------------------
7713 -- Get_Simple_Init_Val --
7714 -------------------------
7716 function Get_Simple_Init_Val
7717 (T : Entity_Id;
7718 N : Node_Id;
7719 Size : Uint := No_Uint) return Node_Id
7721 Loc : constant Source_Ptr := Sloc (N);
7722 Val : Node_Id;
7723 Result : Node_Id;
7724 Val_RE : RE_Id;
7726 Size_To_Use : Uint;
7727 -- This is the size to be used for computation of the appropriate
7728 -- initial value for the Normalize_Scalars and Initialize_Scalars case.
7730 IV_Attribute : constant Boolean :=
7731 Nkind (N) = N_Attribute_Reference
7732 and then Attribute_Name (N) = Name_Invalid_Value;
7734 Lo_Bound : Uint;
7735 Hi_Bound : Uint;
7736 -- These are the values computed by the procedure Check_Subtype_Bounds
7738 procedure Check_Subtype_Bounds;
7739 -- This procedure examines the subtype T, and its ancestor subtypes and
7740 -- derived types to determine the best known information about the
7741 -- bounds of the subtype. After the call Lo_Bound is set either to
7742 -- No_Uint if no information can be determined, or to a value which
7743 -- represents a known low bound, i.e. a valid value of the subtype can
7744 -- not be less than this value. Hi_Bound is similarly set to a known
7745 -- high bound (valid value cannot be greater than this).
7747 --------------------------
7748 -- Check_Subtype_Bounds --
7749 --------------------------
7751 procedure Check_Subtype_Bounds is
7752 ST1 : Entity_Id;
7753 ST2 : Entity_Id;
7754 Lo : Node_Id;
7755 Hi : Node_Id;
7756 Loval : Uint;
7757 Hival : Uint;
7759 begin
7760 Lo_Bound := No_Uint;
7761 Hi_Bound := No_Uint;
7763 -- Loop to climb ancestor subtypes and derived types
7765 ST1 := T;
7766 loop
7767 if not Is_Discrete_Type (ST1) then
7768 return;
7769 end if;
7771 Lo := Type_Low_Bound (ST1);
7772 Hi := Type_High_Bound (ST1);
7774 if Compile_Time_Known_Value (Lo) then
7775 Loval := Expr_Value (Lo);
7777 if Lo_Bound = No_Uint or else Lo_Bound < Loval then
7778 Lo_Bound := Loval;
7779 end if;
7780 end if;
7782 if Compile_Time_Known_Value (Hi) then
7783 Hival := Expr_Value (Hi);
7785 if Hi_Bound = No_Uint or else Hi_Bound > Hival then
7786 Hi_Bound := Hival;
7787 end if;
7788 end if;
7790 ST2 := Ancestor_Subtype (ST1);
7792 if No (ST2) then
7793 ST2 := Etype (ST1);
7794 end if;
7796 exit when ST1 = ST2;
7797 ST1 := ST2;
7798 end loop;
7799 end Check_Subtype_Bounds;
7801 -- Start of processing for Get_Simple_Init_Val
7803 begin
7804 -- For a private type, we should always have an underlying type (because
7805 -- this was already checked in Needs_Simple_Initialization). What we do
7806 -- is to get the value for the underlying type and then do an unchecked
7807 -- conversion to the private type.
7809 if Is_Private_Type (T) then
7810 Val := Get_Simple_Init_Val (Underlying_Type (T), N, Size);
7812 -- A special case, if the underlying value is null, then qualify it
7813 -- with the underlying type, so that the null is properly typed.
7814 -- Similarly, if it is an aggregate it must be qualified, because an
7815 -- unchecked conversion does not provide a context for it.
7817 if Nkind_In (Val, N_Null, N_Aggregate) then
7818 Val :=
7819 Make_Qualified_Expression (Loc,
7820 Subtype_Mark =>
7821 New_Occurrence_Of (Underlying_Type (T), Loc),
7822 Expression => Val);
7823 end if;
7825 Result := Unchecked_Convert_To (T, Val);
7827 -- Don't truncate result (important for Initialize/Normalize_Scalars)
7829 if Nkind (Result) = N_Unchecked_Type_Conversion
7830 and then Is_Scalar_Type (Underlying_Type (T))
7831 then
7832 Set_No_Truncation (Result);
7833 end if;
7835 return Result;
7837 -- Scalars with Default_Value aspect. The first subtype may now be
7838 -- private, so retrieve value from underlying type.
7840 elsif Is_Scalar_Type (T) and then Has_Default_Aspect (T) then
7841 if Is_Private_Type (First_Subtype (T)) then
7842 return Unchecked_Convert_To (T,
7843 Default_Aspect_Value (Full_View (First_Subtype (T))));
7844 else
7845 return
7846 Convert_To (T, Default_Aspect_Value (First_Subtype (T)));
7847 end if;
7849 -- Otherwise, for scalars, we must have normalize/initialize scalars
7850 -- case, or if the node N is an 'Invalid_Value attribute node.
7852 elsif Is_Scalar_Type (T) then
7853 pragma Assert (Init_Or_Norm_Scalars or IV_Attribute);
7855 -- Compute size of object. If it is given by the caller, we can use
7856 -- it directly, otherwise we use Esize (T) as an estimate. As far as
7857 -- we know this covers all cases correctly.
7859 if Size = No_Uint or else Size <= Uint_0 then
7860 Size_To_Use := UI_Max (Uint_1, Esize (T));
7861 else
7862 Size_To_Use := Size;
7863 end if;
7865 -- Maximum size to use is 64 bits, since we will create values of
7866 -- type Unsigned_64 and the range must fit this type.
7868 if Size_To_Use /= No_Uint and then Size_To_Use > Uint_64 then
7869 Size_To_Use := Uint_64;
7870 end if;
7872 -- Check known bounds of subtype
7874 Check_Subtype_Bounds;
7876 -- Processing for Normalize_Scalars case
7878 if Normalize_Scalars and then not IV_Attribute then
7880 -- If zero is invalid, it is a convenient value to use that is
7881 -- for sure an appropriate invalid value in all situations.
7883 if Lo_Bound /= No_Uint and then Lo_Bound > Uint_0 then
7884 Val := Make_Integer_Literal (Loc, 0);
7886 -- Cases where all one bits is the appropriate invalid value
7888 -- For modular types, all 1 bits is either invalid or valid. If
7889 -- it is valid, then there is nothing that can be done since there
7890 -- are no invalid values (we ruled out zero already).
7892 -- For signed integer types that have no negative values, either
7893 -- there is room for negative values, or there is not. If there
7894 -- is, then all 1-bits may be interpreted as minus one, which is
7895 -- certainly invalid. Alternatively it is treated as the largest
7896 -- positive value, in which case the observation for modular types
7897 -- still applies.
7899 -- For float types, all 1-bits is a NaN (not a number), which is
7900 -- certainly an appropriately invalid value.
7902 elsif Is_Unsigned_Type (T)
7903 or else Is_Floating_Point_Type (T)
7904 or else Is_Enumeration_Type (T)
7905 then
7906 Val := Make_Integer_Literal (Loc, 2 ** Size_To_Use - 1);
7908 -- Resolve as Unsigned_64, because the largest number we can
7909 -- generate is out of range of universal integer.
7911 Analyze_And_Resolve (Val, RTE (RE_Unsigned_64));
7913 -- Case of signed types
7915 else
7916 declare
7917 Signed_Size : constant Uint :=
7918 UI_Min (Uint_63, Size_To_Use - 1);
7920 begin
7921 -- Normally we like to use the most negative number. The one
7922 -- exception is when this number is in the known subtype
7923 -- range and the largest positive number is not in the known
7924 -- subtype range.
7926 -- For this exceptional case, use largest positive value
7928 if Lo_Bound /= No_Uint and then Hi_Bound /= No_Uint
7929 and then Lo_Bound <= (-(2 ** Signed_Size))
7930 and then Hi_Bound < 2 ** Signed_Size
7931 then
7932 Val := Make_Integer_Literal (Loc, 2 ** Signed_Size - 1);
7934 -- Normal case of largest negative value
7936 else
7937 Val := Make_Integer_Literal (Loc, -(2 ** Signed_Size));
7938 end if;
7939 end;
7940 end if;
7942 -- Here for Initialize_Scalars case (or Invalid_Value attribute used)
7944 else
7945 -- For float types, use float values from System.Scalar_Values
7947 if Is_Floating_Point_Type (T) then
7948 if Root_Type (T) = Standard_Short_Float then
7949 Val_RE := RE_IS_Isf;
7950 elsif Root_Type (T) = Standard_Float then
7951 Val_RE := RE_IS_Ifl;
7952 elsif Root_Type (T) = Standard_Long_Float then
7953 Val_RE := RE_IS_Ilf;
7954 else pragma Assert (Root_Type (T) = Standard_Long_Long_Float);
7955 Val_RE := RE_IS_Ill;
7956 end if;
7958 -- If zero is invalid, use zero values from System.Scalar_Values
7960 elsif Lo_Bound /= No_Uint and then Lo_Bound > Uint_0 then
7961 if Size_To_Use <= 8 then
7962 Val_RE := RE_IS_Iz1;
7963 elsif Size_To_Use <= 16 then
7964 Val_RE := RE_IS_Iz2;
7965 elsif Size_To_Use <= 32 then
7966 Val_RE := RE_IS_Iz4;
7967 else
7968 Val_RE := RE_IS_Iz8;
7969 end if;
7971 -- For unsigned, use unsigned values from System.Scalar_Values
7973 elsif Is_Unsigned_Type (T) then
7974 if Size_To_Use <= 8 then
7975 Val_RE := RE_IS_Iu1;
7976 elsif Size_To_Use <= 16 then
7977 Val_RE := RE_IS_Iu2;
7978 elsif Size_To_Use <= 32 then
7979 Val_RE := RE_IS_Iu4;
7980 else
7981 Val_RE := RE_IS_Iu8;
7982 end if;
7984 -- For signed, use signed values from System.Scalar_Values
7986 else
7987 if Size_To_Use <= 8 then
7988 Val_RE := RE_IS_Is1;
7989 elsif Size_To_Use <= 16 then
7990 Val_RE := RE_IS_Is2;
7991 elsif Size_To_Use <= 32 then
7992 Val_RE := RE_IS_Is4;
7993 else
7994 Val_RE := RE_IS_Is8;
7995 end if;
7996 end if;
7998 Val := New_Occurrence_Of (RTE (Val_RE), Loc);
7999 end if;
8001 -- The final expression is obtained by doing an unchecked conversion
8002 -- of this result to the base type of the required subtype. Use the
8003 -- base type to prevent the unchecked conversion from chopping bits,
8004 -- and then we set Kill_Range_Check to preserve the "bad" value.
8006 Result := Unchecked_Convert_To (Base_Type (T), Val);
8008 -- Ensure result is not truncated, since we want the "bad" bits, and
8009 -- also kill range check on result.
8011 if Nkind (Result) = N_Unchecked_Type_Conversion then
8012 Set_No_Truncation (Result);
8013 Set_Kill_Range_Check (Result, True);
8014 end if;
8016 return Result;
8018 -- String or Wide_[Wide]_String (must have Initialize_Scalars set)
8020 elsif Is_Standard_String_Type (T) then
8021 pragma Assert (Init_Or_Norm_Scalars);
8023 return
8024 Make_Aggregate (Loc,
8025 Component_Associations => New_List (
8026 Make_Component_Association (Loc,
8027 Choices => New_List (
8028 Make_Others_Choice (Loc)),
8029 Expression =>
8030 Get_Simple_Init_Val
8031 (Component_Type (T), N, Esize (Root_Type (T))))));
8033 -- Access type is initialized to null
8035 elsif Is_Access_Type (T) then
8036 return Make_Null (Loc);
8038 -- No other possibilities should arise, since we should only be calling
8039 -- Get_Simple_Init_Val if Needs_Simple_Initialization returned True,
8040 -- indicating one of the above cases held.
8042 else
8043 raise Program_Error;
8044 end if;
8046 exception
8047 when RE_Not_Available =>
8048 return Empty;
8049 end Get_Simple_Init_Val;
8051 ------------------------------
8052 -- Has_New_Non_Standard_Rep --
8053 ------------------------------
8055 function Has_New_Non_Standard_Rep (T : Entity_Id) return Boolean is
8056 begin
8057 if not Is_Derived_Type (T) then
8058 return Has_Non_Standard_Rep (T)
8059 or else Has_Non_Standard_Rep (Root_Type (T));
8061 -- If Has_Non_Standard_Rep is not set on the derived type, the
8062 -- representation is fully inherited.
8064 elsif not Has_Non_Standard_Rep (T) then
8065 return False;
8067 else
8068 return First_Rep_Item (T) /= First_Rep_Item (Root_Type (T));
8070 -- May need a more precise check here: the First_Rep_Item may be a
8071 -- stream attribute, which does not affect the representation of the
8072 -- type ???
8074 end if;
8075 end Has_New_Non_Standard_Rep;
8077 ----------------
8078 -- In_Runtime --
8079 ----------------
8081 function In_Runtime (E : Entity_Id) return Boolean is
8082 S1 : Entity_Id;
8084 begin
8085 S1 := Scope (E);
8086 while Scope (S1) /= Standard_Standard loop
8087 S1 := Scope (S1);
8088 end loop;
8090 return Is_RTU (S1, System) or else Is_RTU (S1, Ada);
8091 end In_Runtime;
8093 ---------------------------------------
8094 -- Insert_Component_Invariant_Checks --
8095 ---------------------------------------
8097 procedure Insert_Component_Invariant_Checks
8098 (N : Node_Id;
8099 Typ : Entity_Id;
8100 Proc : Node_Id)
8102 Loc : constant Source_Ptr := Sloc (Typ);
8103 Proc_Id : Entity_Id;
8105 begin
8106 if Present (Proc) then
8107 Proc_Id := Defining_Entity (Proc);
8109 if not Has_Invariants (Typ) then
8110 Set_Has_Invariants (Typ);
8111 Set_Is_Invariant_Procedure (Proc_Id);
8112 Set_Invariant_Procedure (Typ, Proc_Id);
8113 Insert_After (N, Proc);
8114 Analyze (Proc);
8116 else
8118 -- Find already created invariant subprogram, insert body of
8119 -- component invariant proc in its body, and add call after
8120 -- other checks.
8122 declare
8123 Bod : Node_Id;
8124 Inv_Id : constant Entity_Id := Invariant_Procedure (Typ);
8125 Call : constant Node_Id :=
8126 Make_Procedure_Call_Statement (Sloc (N),
8127 Name => New_Occurrence_Of (Proc_Id, Loc),
8128 Parameter_Associations =>
8129 New_List
8130 (New_Occurrence_Of (First_Formal (Inv_Id), Loc)));
8132 begin
8133 -- The invariant body has not been analyzed yet, so we do a
8134 -- sequential search forward, and retrieve it by name.
8136 Bod := Next (N);
8137 while Present (Bod) loop
8138 exit when Nkind (Bod) = N_Subprogram_Body
8139 and then Chars (Defining_Entity (Bod)) = Chars (Inv_Id);
8140 Next (Bod);
8141 end loop;
8143 -- If the body is not found, it is the case of an invariant
8144 -- appearing on a full declaration in a private part, in
8145 -- which case the type has been frozen but the invariant
8146 -- procedure for the composite type not created yet. Create
8147 -- body now.
8149 if No (Bod) then
8150 Build_Invariant_Procedure (Typ, Parent (Current_Scope));
8151 Bod := Unit_Declaration_Node
8152 (Corresponding_Body (Unit_Declaration_Node (Inv_Id)));
8153 end if;
8155 Append_To (Declarations (Bod), Proc);
8156 Append_To (Statements (Handled_Statement_Sequence (Bod)), Call);
8157 Analyze (Proc);
8158 Analyze (Call);
8159 end;
8160 end if;
8161 end if;
8162 end Insert_Component_Invariant_Checks;
8164 ----------------------------
8165 -- Initialization_Warning --
8166 ----------------------------
8168 procedure Initialization_Warning (E : Entity_Id) is
8169 Warning_Needed : Boolean;
8171 begin
8172 Warning_Needed := False;
8174 if Ekind (Current_Scope) = E_Package
8175 and then Static_Elaboration_Desired (Current_Scope)
8176 then
8177 if Is_Type (E) then
8178 if Is_Record_Type (E) then
8179 if Has_Discriminants (E)
8180 or else Is_Limited_Type (E)
8181 or else Has_Non_Standard_Rep (E)
8182 then
8183 Warning_Needed := True;
8185 else
8186 -- Verify that at least one component has an initialization
8187 -- expression. No need for a warning on a type if all its
8188 -- components have no initialization.
8190 declare
8191 Comp : Entity_Id;
8193 begin
8194 Comp := First_Component (E);
8195 while Present (Comp) loop
8196 if Ekind (Comp) = E_Discriminant
8197 or else
8198 (Nkind (Parent (Comp)) = N_Component_Declaration
8199 and then Present (Expression (Parent (Comp))))
8200 then
8201 Warning_Needed := True;
8202 exit;
8203 end if;
8205 Next_Component (Comp);
8206 end loop;
8207 end;
8208 end if;
8210 if Warning_Needed then
8211 Error_Msg_N
8212 ("Objects of the type cannot be initialized statically "
8213 & "by default??", Parent (E));
8214 end if;
8215 end if;
8217 else
8218 Error_Msg_N ("Object cannot be initialized statically??", E);
8219 end if;
8220 end if;
8221 end Initialization_Warning;
8223 ------------------
8224 -- Init_Formals --
8225 ------------------
8227 function Init_Formals (Typ : Entity_Id) return List_Id is
8228 Loc : constant Source_Ptr := Sloc (Typ);
8229 Formals : List_Id;
8231 begin
8232 -- First parameter is always _Init : in out typ. Note that we need this
8233 -- to be in/out because in the case of the task record value, there
8234 -- are default record fields (_Priority, _Size, -Task_Info) that may
8235 -- be referenced in the generated initialization routine.
8237 Formals := New_List (
8238 Make_Parameter_Specification (Loc,
8239 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uInit),
8240 In_Present => True,
8241 Out_Present => True,
8242 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
8244 -- For task record value, or type that contains tasks, add two more
8245 -- formals, _Master : Master_Id and _Chain : in out Activation_Chain
8246 -- We also add these parameters for the task record type case.
8248 if Has_Task (Typ)
8249 or else (Is_Record_Type (Typ) and then Is_Task_Record_Type (Typ))
8250 then
8251 Append_To (Formals,
8252 Make_Parameter_Specification (Loc,
8253 Defining_Identifier =>
8254 Make_Defining_Identifier (Loc, Name_uMaster),
8255 Parameter_Type =>
8256 New_Occurrence_Of (RTE (RE_Master_Id), Loc)));
8258 -- Add _Chain (not done for sequential elaboration policy, see
8259 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
8261 if Partition_Elaboration_Policy /= 'S' then
8262 Append_To (Formals,
8263 Make_Parameter_Specification (Loc,
8264 Defining_Identifier =>
8265 Make_Defining_Identifier (Loc, Name_uChain),
8266 In_Present => True,
8267 Out_Present => True,
8268 Parameter_Type =>
8269 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc)));
8270 end if;
8272 Append_To (Formals,
8273 Make_Parameter_Specification (Loc,
8274 Defining_Identifier =>
8275 Make_Defining_Identifier (Loc, Name_uTask_Name),
8276 In_Present => True,
8277 Parameter_Type => New_Occurrence_Of (Standard_String, Loc)));
8278 end if;
8280 return Formals;
8282 exception
8283 when RE_Not_Available =>
8284 return Empty_List;
8285 end Init_Formals;
8287 -------------------------
8288 -- Init_Secondary_Tags --
8289 -------------------------
8291 procedure Init_Secondary_Tags
8292 (Typ : Entity_Id;
8293 Target : Node_Id;
8294 Stmts_List : List_Id;
8295 Fixed_Comps : Boolean := True;
8296 Variable_Comps : Boolean := True)
8298 Loc : constant Source_Ptr := Sloc (Target);
8300 -- Inherit the C++ tag of the secondary dispatch table of Typ associated
8301 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
8303 procedure Initialize_Tag
8304 (Typ : Entity_Id;
8305 Iface : Entity_Id;
8306 Tag_Comp : Entity_Id;
8307 Iface_Tag : Node_Id);
8308 -- Initialize the tag of the secondary dispatch table of Typ associated
8309 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
8310 -- Compiling under the CPP full ABI compatibility mode, if the ancestor
8311 -- of Typ CPP tagged type we generate code to inherit the contents of
8312 -- the dispatch table directly from the ancestor.
8314 --------------------
8315 -- Initialize_Tag --
8316 --------------------
8318 procedure Initialize_Tag
8319 (Typ : Entity_Id;
8320 Iface : Entity_Id;
8321 Tag_Comp : Entity_Id;
8322 Iface_Tag : Node_Id)
8324 Comp_Typ : Entity_Id;
8325 Offset_To_Top_Comp : Entity_Id := Empty;
8327 begin
8328 -- Initialize pointer to secondary DT associated with the interface
8330 if not Is_Ancestor (Iface, Typ, Use_Full_View => True) then
8331 Append_To (Stmts_List,
8332 Make_Assignment_Statement (Loc,
8333 Name =>
8334 Make_Selected_Component (Loc,
8335 Prefix => New_Copy_Tree (Target),
8336 Selector_Name => New_Occurrence_Of (Tag_Comp, Loc)),
8337 Expression =>
8338 New_Occurrence_Of (Iface_Tag, Loc)));
8339 end if;
8341 Comp_Typ := Scope (Tag_Comp);
8343 -- Initialize the entries of the table of interfaces. We generate a
8344 -- different call when the parent of the type has variable size
8345 -- components.
8347 if Comp_Typ /= Etype (Comp_Typ)
8348 and then Is_Variable_Size_Record (Etype (Comp_Typ))
8349 and then Chars (Tag_Comp) /= Name_uTag
8350 then
8351 pragma Assert (Present (DT_Offset_To_Top_Func (Tag_Comp)));
8353 -- Issue error if Set_Dynamic_Offset_To_Top is not available in a
8354 -- configurable run-time environment.
8356 if not RTE_Available (RE_Set_Dynamic_Offset_To_Top) then
8357 Error_Msg_CRT
8358 ("variable size record with interface types", Typ);
8359 return;
8360 end if;
8362 -- Generate:
8363 -- Set_Dynamic_Offset_To_Top
8364 -- (This => Init,
8365 -- Interface_T => Iface'Tag,
8366 -- Offset_Value => n,
8367 -- Offset_Func => Fn'Address)
8369 Append_To (Stmts_List,
8370 Make_Procedure_Call_Statement (Loc,
8371 Name =>
8372 New_Occurrence_Of (RTE (RE_Set_Dynamic_Offset_To_Top), Loc),
8373 Parameter_Associations => New_List (
8374 Make_Attribute_Reference (Loc,
8375 Prefix => New_Copy_Tree (Target),
8376 Attribute_Name => Name_Address),
8378 Unchecked_Convert_To (RTE (RE_Tag),
8379 New_Occurrence_Of
8380 (Node (First_Elmt (Access_Disp_Table (Iface))),
8381 Loc)),
8383 Unchecked_Convert_To
8384 (RTE (RE_Storage_Offset),
8385 Make_Attribute_Reference (Loc,
8386 Prefix =>
8387 Make_Selected_Component (Loc,
8388 Prefix => New_Copy_Tree (Target),
8389 Selector_Name =>
8390 New_Occurrence_Of (Tag_Comp, Loc)),
8391 Attribute_Name => Name_Position)),
8393 Unchecked_Convert_To (RTE (RE_Offset_To_Top_Function_Ptr),
8394 Make_Attribute_Reference (Loc,
8395 Prefix => New_Occurrence_Of
8396 (DT_Offset_To_Top_Func (Tag_Comp), Loc),
8397 Attribute_Name => Name_Address)))));
8399 -- In this case the next component stores the value of the offset
8400 -- to the top.
8402 Offset_To_Top_Comp := Next_Entity (Tag_Comp);
8403 pragma Assert (Present (Offset_To_Top_Comp));
8405 Append_To (Stmts_List,
8406 Make_Assignment_Statement (Loc,
8407 Name =>
8408 Make_Selected_Component (Loc,
8409 Prefix => New_Copy_Tree (Target),
8410 Selector_Name =>
8411 New_Occurrence_Of (Offset_To_Top_Comp, Loc)),
8413 Expression =>
8414 Make_Attribute_Reference (Loc,
8415 Prefix =>
8416 Make_Selected_Component (Loc,
8417 Prefix => New_Copy_Tree (Target),
8418 Selector_Name => New_Occurrence_Of (Tag_Comp, Loc)),
8419 Attribute_Name => Name_Position)));
8421 -- Normal case: No discriminants in the parent type
8423 else
8424 -- Don't need to set any value if this interface shares the
8425 -- primary dispatch table.
8427 if not Is_Ancestor (Iface, Typ, Use_Full_View => True) then
8428 Append_To (Stmts_List,
8429 Build_Set_Static_Offset_To_Top (Loc,
8430 Iface_Tag => New_Occurrence_Of (Iface_Tag, Loc),
8431 Offset_Value =>
8432 Unchecked_Convert_To (RTE (RE_Storage_Offset),
8433 Make_Attribute_Reference (Loc,
8434 Prefix =>
8435 Make_Selected_Component (Loc,
8436 Prefix => New_Copy_Tree (Target),
8437 Selector_Name =>
8438 New_Occurrence_Of (Tag_Comp, Loc)),
8439 Attribute_Name => Name_Position))));
8440 end if;
8442 -- Generate:
8443 -- Register_Interface_Offset
8444 -- (This => Init,
8445 -- Interface_T => Iface'Tag,
8446 -- Is_Constant => True,
8447 -- Offset_Value => n,
8448 -- Offset_Func => null);
8450 if RTE_Available (RE_Register_Interface_Offset) then
8451 Append_To (Stmts_List,
8452 Make_Procedure_Call_Statement (Loc,
8453 Name =>
8454 New_Occurrence_Of
8455 (RTE (RE_Register_Interface_Offset), Loc),
8456 Parameter_Associations => New_List (
8457 Make_Attribute_Reference (Loc,
8458 Prefix => New_Copy_Tree (Target),
8459 Attribute_Name => Name_Address),
8461 Unchecked_Convert_To (RTE (RE_Tag),
8462 New_Occurrence_Of
8463 (Node (First_Elmt (Access_Disp_Table (Iface))), Loc)),
8465 New_Occurrence_Of (Standard_True, Loc),
8467 Unchecked_Convert_To (RTE (RE_Storage_Offset),
8468 Make_Attribute_Reference (Loc,
8469 Prefix =>
8470 Make_Selected_Component (Loc,
8471 Prefix => New_Copy_Tree (Target),
8472 Selector_Name =>
8473 New_Occurrence_Of (Tag_Comp, Loc)),
8474 Attribute_Name => Name_Position)),
8476 Make_Null (Loc))));
8477 end if;
8478 end if;
8479 end Initialize_Tag;
8481 -- Local variables
8483 Full_Typ : Entity_Id;
8484 Ifaces_List : Elist_Id;
8485 Ifaces_Comp_List : Elist_Id;
8486 Ifaces_Tag_List : Elist_Id;
8487 Iface_Elmt : Elmt_Id;
8488 Iface_Comp_Elmt : Elmt_Id;
8489 Iface_Tag_Elmt : Elmt_Id;
8490 Tag_Comp : Node_Id;
8491 In_Variable_Pos : Boolean;
8493 -- Start of processing for Init_Secondary_Tags
8495 begin
8496 -- Handle private types
8498 if Present (Full_View (Typ)) then
8499 Full_Typ := Full_View (Typ);
8500 else
8501 Full_Typ := Typ;
8502 end if;
8504 Collect_Interfaces_Info
8505 (Full_Typ, Ifaces_List, Ifaces_Comp_List, Ifaces_Tag_List);
8507 Iface_Elmt := First_Elmt (Ifaces_List);
8508 Iface_Comp_Elmt := First_Elmt (Ifaces_Comp_List);
8509 Iface_Tag_Elmt := First_Elmt (Ifaces_Tag_List);
8510 while Present (Iface_Elmt) loop
8511 Tag_Comp := Node (Iface_Comp_Elmt);
8513 -- Check if parent of record type has variable size components
8515 In_Variable_Pos := Scope (Tag_Comp) /= Etype (Scope (Tag_Comp))
8516 and then Is_Variable_Size_Record (Etype (Scope (Tag_Comp)));
8518 -- If we are compiling under the CPP full ABI compatibility mode and
8519 -- the ancestor is a CPP_Pragma tagged type then we generate code to
8520 -- initialize the secondary tag components from tags that reference
8521 -- secondary tables filled with copy of parent slots.
8523 if Is_CPP_Class (Root_Type (Full_Typ)) then
8525 -- Reject interface components located at variable offset in
8526 -- C++ derivations. This is currently unsupported.
8528 if not Fixed_Comps and then In_Variable_Pos then
8530 -- Locate the first dynamic component of the record. Done to
8531 -- improve the text of the warning.
8533 declare
8534 Comp : Entity_Id;
8535 Comp_Typ : Entity_Id;
8537 begin
8538 Comp := First_Entity (Typ);
8539 while Present (Comp) loop
8540 Comp_Typ := Etype (Comp);
8542 if Ekind (Comp) /= E_Discriminant
8543 and then not Is_Tag (Comp)
8544 then
8545 exit when
8546 (Is_Record_Type (Comp_Typ)
8547 and then
8548 Is_Variable_Size_Record (Base_Type (Comp_Typ)))
8549 or else
8550 (Is_Array_Type (Comp_Typ)
8551 and then Is_Variable_Size_Array (Comp_Typ));
8552 end if;
8554 Next_Entity (Comp);
8555 end loop;
8557 pragma Assert (Present (Comp));
8558 Error_Msg_Node_2 := Comp;
8559 Error_Msg_NE
8560 ("parent type & with dynamic component & cannot be parent"
8561 & " of 'C'P'P derivation if new interfaces are present",
8562 Typ, Scope (Original_Record_Component (Comp)));
8564 Error_Msg_Sloc :=
8565 Sloc (Scope (Original_Record_Component (Comp)));
8566 Error_Msg_NE
8567 ("type derived from 'C'P'P type & defined #",
8568 Typ, Scope (Original_Record_Component (Comp)));
8570 -- Avoid duplicated warnings
8572 exit;
8573 end;
8575 -- Initialize secondary tags
8577 else
8578 Append_To (Stmts_List,
8579 Make_Assignment_Statement (Loc,
8580 Name =>
8581 Make_Selected_Component (Loc,
8582 Prefix => New_Copy_Tree (Target),
8583 Selector_Name =>
8584 New_Occurrence_Of (Node (Iface_Comp_Elmt), Loc)),
8585 Expression =>
8586 New_Occurrence_Of (Node (Iface_Tag_Elmt), Loc)));
8587 end if;
8589 -- Otherwise generate code to initialize the tag
8591 else
8592 if (In_Variable_Pos and then Variable_Comps)
8593 or else (not In_Variable_Pos and then Fixed_Comps)
8594 then
8595 Initialize_Tag (Full_Typ,
8596 Iface => Node (Iface_Elmt),
8597 Tag_Comp => Tag_Comp,
8598 Iface_Tag => Node (Iface_Tag_Elmt));
8599 end if;
8600 end if;
8602 Next_Elmt (Iface_Elmt);
8603 Next_Elmt (Iface_Comp_Elmt);
8604 Next_Elmt (Iface_Tag_Elmt);
8605 end loop;
8606 end Init_Secondary_Tags;
8608 ------------------------
8609 -- Is_User_Defined_Eq --
8610 ------------------------
8612 function Is_User_Defined_Equality (Prim : Node_Id) return Boolean is
8613 begin
8614 return Chars (Prim) = Name_Op_Eq
8615 and then Etype (First_Formal (Prim)) =
8616 Etype (Next_Formal (First_Formal (Prim)))
8617 and then Base_Type (Etype (Prim)) = Standard_Boolean;
8618 end Is_User_Defined_Equality;
8620 ----------------------------------------
8621 -- Make_Controlling_Function_Wrappers --
8622 ----------------------------------------
8624 procedure Make_Controlling_Function_Wrappers
8625 (Tag_Typ : Entity_Id;
8626 Decl_List : out List_Id;
8627 Body_List : out List_Id)
8629 Loc : constant Source_Ptr := Sloc (Tag_Typ);
8630 Prim_Elmt : Elmt_Id;
8631 Subp : Entity_Id;
8632 Actual_List : List_Id;
8633 Formal_List : List_Id;
8634 Formal : Entity_Id;
8635 Par_Formal : Entity_Id;
8636 Formal_Node : Node_Id;
8637 Func_Body : Node_Id;
8638 Func_Decl : Node_Id;
8639 Func_Spec : Node_Id;
8640 Return_Stmt : Node_Id;
8642 begin
8643 Decl_List := New_List;
8644 Body_List := New_List;
8646 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
8648 while Present (Prim_Elmt) loop
8649 Subp := Node (Prim_Elmt);
8651 -- If a primitive function with a controlling result of the type has
8652 -- not been overridden by the user, then we must create a wrapper
8653 -- function here that effectively overrides it and invokes the
8654 -- (non-abstract) parent function. This can only occur for a null
8655 -- extension. Note that functions with anonymous controlling access
8656 -- results don't qualify and must be overridden. We also exclude
8657 -- Input attributes, since each type will have its own version of
8658 -- Input constructed by the expander. The test for Comes_From_Source
8659 -- is needed to distinguish inherited operations from renamings
8660 -- (which also have Alias set). We exclude internal entities with
8661 -- Interface_Alias to avoid generating duplicated wrappers since
8662 -- the primitive which covers the interface is also available in
8663 -- the list of primitive operations.
8665 -- The function may be abstract, or require_Overriding may be set
8666 -- for it, because tests for null extensions may already have reset
8667 -- the Is_Abstract_Subprogram_Flag. If Requires_Overriding is not
8668 -- set, functions that need wrappers are recognized by having an
8669 -- alias that returns the parent type.
8671 if Comes_From_Source (Subp)
8672 or else No (Alias (Subp))
8673 or else Present (Interface_Alias (Subp))
8674 or else Ekind (Subp) /= E_Function
8675 or else not Has_Controlling_Result (Subp)
8676 or else Is_Access_Type (Etype (Subp))
8677 or else Is_Abstract_Subprogram (Alias (Subp))
8678 or else Is_TSS (Subp, TSS_Stream_Input)
8679 then
8680 goto Next_Prim;
8682 elsif Is_Abstract_Subprogram (Subp)
8683 or else Requires_Overriding (Subp)
8684 or else
8685 (Is_Null_Extension (Etype (Subp))
8686 and then Etype (Alias (Subp)) /= Etype (Subp))
8687 then
8688 Formal_List := No_List;
8689 Formal := First_Formal (Subp);
8691 if Present (Formal) then
8692 Formal_List := New_List;
8694 while Present (Formal) loop
8695 Append
8696 (Make_Parameter_Specification
8697 (Loc,
8698 Defining_Identifier =>
8699 Make_Defining_Identifier (Sloc (Formal),
8700 Chars => Chars (Formal)),
8701 In_Present => In_Present (Parent (Formal)),
8702 Out_Present => Out_Present (Parent (Formal)),
8703 Null_Exclusion_Present =>
8704 Null_Exclusion_Present (Parent (Formal)),
8705 Parameter_Type =>
8706 New_Occurrence_Of (Etype (Formal), Loc),
8707 Expression =>
8708 New_Copy_Tree (Expression (Parent (Formal)))),
8709 Formal_List);
8711 Next_Formal (Formal);
8712 end loop;
8713 end if;
8715 Func_Spec :=
8716 Make_Function_Specification (Loc,
8717 Defining_Unit_Name =>
8718 Make_Defining_Identifier (Loc,
8719 Chars => Chars (Subp)),
8720 Parameter_Specifications => Formal_List,
8721 Result_Definition =>
8722 New_Occurrence_Of (Etype (Subp), Loc));
8724 Func_Decl := Make_Subprogram_Declaration (Loc, Func_Spec);
8725 Append_To (Decl_List, Func_Decl);
8727 -- Build a wrapper body that calls the parent function. The body
8728 -- contains a single return statement that returns an extension
8729 -- aggregate whose ancestor part is a call to the parent function,
8730 -- passing the formals as actuals (with any controlling arguments
8731 -- converted to the types of the corresponding formals of the
8732 -- parent function, which might be anonymous access types), and
8733 -- having a null extension.
8735 Formal := First_Formal (Subp);
8736 Par_Formal := First_Formal (Alias (Subp));
8737 Formal_Node := First (Formal_List);
8739 if Present (Formal) then
8740 Actual_List := New_List;
8741 else
8742 Actual_List := No_List;
8743 end if;
8745 while Present (Formal) loop
8746 if Is_Controlling_Formal (Formal) then
8747 Append_To (Actual_List,
8748 Make_Type_Conversion (Loc,
8749 Subtype_Mark =>
8750 New_Occurrence_Of (Etype (Par_Formal), Loc),
8751 Expression =>
8752 New_Occurrence_Of
8753 (Defining_Identifier (Formal_Node), Loc)));
8754 else
8755 Append_To
8756 (Actual_List,
8757 New_Occurrence_Of
8758 (Defining_Identifier (Formal_Node), Loc));
8759 end if;
8761 Next_Formal (Formal);
8762 Next_Formal (Par_Formal);
8763 Next (Formal_Node);
8764 end loop;
8766 Return_Stmt :=
8767 Make_Simple_Return_Statement (Loc,
8768 Expression =>
8769 Make_Extension_Aggregate (Loc,
8770 Ancestor_Part =>
8771 Make_Function_Call (Loc,
8772 Name =>
8773 New_Occurrence_Of (Alias (Subp), Loc),
8774 Parameter_Associations => Actual_List),
8775 Null_Record_Present => True));
8777 Func_Body :=
8778 Make_Subprogram_Body (Loc,
8779 Specification => New_Copy_Tree (Func_Spec),
8780 Declarations => Empty_List,
8781 Handled_Statement_Sequence =>
8782 Make_Handled_Sequence_Of_Statements (Loc,
8783 Statements => New_List (Return_Stmt)));
8785 Set_Defining_Unit_Name
8786 (Specification (Func_Body),
8787 Make_Defining_Identifier (Loc, Chars (Subp)));
8789 Append_To (Body_List, Func_Body);
8791 -- Replace the inherited function with the wrapper function in the
8792 -- primitive operations list. We add the minimum decoration needed
8793 -- to override interface primitives.
8795 Set_Ekind (Defining_Unit_Name (Func_Spec), E_Function);
8797 Override_Dispatching_Operation
8798 (Tag_Typ, Subp, New_Op => Defining_Unit_Name (Func_Spec),
8799 Is_Wrapper => True);
8800 end if;
8802 <<Next_Prim>>
8803 Next_Elmt (Prim_Elmt);
8804 end loop;
8805 end Make_Controlling_Function_Wrappers;
8807 -------------------
8808 -- Make_Eq_Body --
8809 -------------------
8811 function Make_Eq_Body
8812 (Typ : Entity_Id;
8813 Eq_Name : Name_Id) return Node_Id
8815 Loc : constant Source_Ptr := Sloc (Parent (Typ));
8816 Decl : Node_Id;
8817 Def : constant Node_Id := Parent (Typ);
8818 Stmts : constant List_Id := New_List;
8819 Variant_Case : Boolean := Has_Discriminants (Typ);
8820 Comps : Node_Id := Empty;
8821 Typ_Def : Node_Id := Type_Definition (Def);
8823 begin
8824 Decl :=
8825 Predef_Spec_Or_Body (Loc,
8826 Tag_Typ => Typ,
8827 Name => Eq_Name,
8828 Profile => New_List (
8829 Make_Parameter_Specification (Loc,
8830 Defining_Identifier =>
8831 Make_Defining_Identifier (Loc, Name_X),
8832 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
8834 Make_Parameter_Specification (Loc,
8835 Defining_Identifier =>
8836 Make_Defining_Identifier (Loc, Name_Y),
8837 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8839 Ret_Type => Standard_Boolean,
8840 For_Body => True);
8842 if Variant_Case then
8843 if Nkind (Typ_Def) = N_Derived_Type_Definition then
8844 Typ_Def := Record_Extension_Part (Typ_Def);
8845 end if;
8847 if Present (Typ_Def) then
8848 Comps := Component_List (Typ_Def);
8849 end if;
8851 Variant_Case :=
8852 Present (Comps) and then Present (Variant_Part (Comps));
8853 end if;
8855 if Variant_Case then
8856 Append_To (Stmts,
8857 Make_Eq_If (Typ, Discriminant_Specifications (Def)));
8858 Append_List_To (Stmts, Make_Eq_Case (Typ, Comps));
8859 Append_To (Stmts,
8860 Make_Simple_Return_Statement (Loc,
8861 Expression => New_Occurrence_Of (Standard_True, Loc)));
8863 else
8864 Append_To (Stmts,
8865 Make_Simple_Return_Statement (Loc,
8866 Expression =>
8867 Expand_Record_Equality
8868 (Typ,
8869 Typ => Typ,
8870 Lhs => Make_Identifier (Loc, Name_X),
8871 Rhs => Make_Identifier (Loc, Name_Y),
8872 Bodies => Declarations (Decl))));
8873 end if;
8875 Set_Handled_Statement_Sequence
8876 (Decl, Make_Handled_Sequence_Of_Statements (Loc, Stmts));
8877 return Decl;
8878 end Make_Eq_Body;
8880 ------------------
8881 -- Make_Eq_Case --
8882 ------------------
8884 -- <Make_Eq_If shared components>
8886 -- case X.D1 is
8887 -- when V1 => <Make_Eq_Case> on subcomponents
8888 -- ...
8889 -- when Vn => <Make_Eq_Case> on subcomponents
8890 -- end case;
8892 function Make_Eq_Case
8893 (E : Entity_Id;
8894 CL : Node_Id;
8895 Discrs : Elist_Id := New_Elmt_List) return List_Id
8897 Loc : constant Source_Ptr := Sloc (E);
8898 Result : constant List_Id := New_List;
8899 Variant : Node_Id;
8900 Alt_List : List_Id;
8902 function Corresponding_Formal (C : Node_Id) return Entity_Id;
8903 -- Given the discriminant that controls a given variant of an unchecked
8904 -- union, find the formal of the equality function that carries the
8905 -- inferred value of the discriminant.
8907 function External_Name (E : Entity_Id) return Name_Id;
8908 -- The value of a given discriminant is conveyed in the corresponding
8909 -- formal parameter of the equality routine. The name of this formal
8910 -- parameter carries a one-character suffix which is removed here.
8912 --------------------------
8913 -- Corresponding_Formal --
8914 --------------------------
8916 function Corresponding_Formal (C : Node_Id) return Entity_Id is
8917 Discr : constant Entity_Id := Entity (Name (Variant_Part (C)));
8918 Elm : Elmt_Id;
8920 begin
8921 Elm := First_Elmt (Discrs);
8922 while Present (Elm) loop
8923 if Chars (Discr) = External_Name (Node (Elm)) then
8924 return Node (Elm);
8925 end if;
8927 Next_Elmt (Elm);
8928 end loop;
8930 -- A formal of the proper name must be found
8932 raise Program_Error;
8933 end Corresponding_Formal;
8935 -------------------
8936 -- External_Name --
8937 -------------------
8939 function External_Name (E : Entity_Id) return Name_Id is
8940 begin
8941 Get_Name_String (Chars (E));
8942 Name_Len := Name_Len - 1;
8943 return Name_Find;
8944 end External_Name;
8946 -- Start of processing for Make_Eq_Case
8948 begin
8949 Append_To (Result, Make_Eq_If (E, Component_Items (CL)));
8951 if No (Variant_Part (CL)) then
8952 return Result;
8953 end if;
8955 Variant := First_Non_Pragma (Variants (Variant_Part (CL)));
8957 if No (Variant) then
8958 return Result;
8959 end if;
8961 Alt_List := New_List;
8962 while Present (Variant) loop
8963 Append_To (Alt_List,
8964 Make_Case_Statement_Alternative (Loc,
8965 Discrete_Choices => New_Copy_List (Discrete_Choices (Variant)),
8966 Statements =>
8967 Make_Eq_Case (E, Component_List (Variant), Discrs)));
8968 Next_Non_Pragma (Variant);
8969 end loop;
8971 -- If we have an Unchecked_Union, use one of the parameters of the
8972 -- enclosing equality routine that captures the discriminant, to use
8973 -- as the expression in the generated case statement.
8975 if Is_Unchecked_Union (E) then
8976 Append_To (Result,
8977 Make_Case_Statement (Loc,
8978 Expression =>
8979 New_Occurrence_Of (Corresponding_Formal (CL), Loc),
8980 Alternatives => Alt_List));
8982 else
8983 Append_To (Result,
8984 Make_Case_Statement (Loc,
8985 Expression =>
8986 Make_Selected_Component (Loc,
8987 Prefix => Make_Identifier (Loc, Name_X),
8988 Selector_Name => New_Copy (Name (Variant_Part (CL)))),
8989 Alternatives => Alt_List));
8990 end if;
8992 return Result;
8993 end Make_Eq_Case;
8995 ----------------
8996 -- Make_Eq_If --
8997 ----------------
8999 -- Generates:
9001 -- if
9002 -- X.C1 /= Y.C1
9003 -- or else
9004 -- X.C2 /= Y.C2
9005 -- ...
9006 -- then
9007 -- return False;
9008 -- end if;
9010 -- or a null statement if the list L is empty
9012 function Make_Eq_If
9013 (E : Entity_Id;
9014 L : List_Id) return Node_Id
9016 Loc : constant Source_Ptr := Sloc (E);
9017 C : Node_Id;
9018 Field_Name : Name_Id;
9019 Cond : Node_Id;
9021 begin
9022 if No (L) then
9023 return Make_Null_Statement (Loc);
9025 else
9026 Cond := Empty;
9028 C := First_Non_Pragma (L);
9029 while Present (C) loop
9030 Field_Name := Chars (Defining_Identifier (C));
9032 -- The tags must not be compared: they are not part of the value.
9033 -- Ditto for parent interfaces because their equality operator is
9034 -- abstract.
9036 -- Note also that in the following, we use Make_Identifier for
9037 -- the component names. Use of New_Occurrence_Of to identify the
9038 -- components would be incorrect because the wrong entities for
9039 -- discriminants could be picked up in the private type case.
9041 if Field_Name = Name_uParent
9042 and then Is_Interface (Etype (Defining_Identifier (C)))
9043 then
9044 null;
9046 elsif Field_Name /= Name_uTag then
9047 Evolve_Or_Else (Cond,
9048 Make_Op_Ne (Loc,
9049 Left_Opnd =>
9050 Make_Selected_Component (Loc,
9051 Prefix => Make_Identifier (Loc, Name_X),
9052 Selector_Name => Make_Identifier (Loc, Field_Name)),
9054 Right_Opnd =>
9055 Make_Selected_Component (Loc,
9056 Prefix => Make_Identifier (Loc, Name_Y),
9057 Selector_Name => Make_Identifier (Loc, Field_Name))));
9058 end if;
9060 Next_Non_Pragma (C);
9061 end loop;
9063 if No (Cond) then
9064 return Make_Null_Statement (Loc);
9066 else
9067 return
9068 Make_Implicit_If_Statement (E,
9069 Condition => Cond,
9070 Then_Statements => New_List (
9071 Make_Simple_Return_Statement (Loc,
9072 Expression => New_Occurrence_Of (Standard_False, Loc))));
9073 end if;
9074 end if;
9075 end Make_Eq_If;
9077 -------------------
9078 -- Make_Neq_Body --
9079 -------------------
9081 function Make_Neq_Body (Tag_Typ : Entity_Id) return Node_Id is
9083 function Is_Predefined_Neq_Renaming (Prim : Node_Id) return Boolean;
9084 -- Returns true if Prim is a renaming of an unresolved predefined
9085 -- inequality operation.
9087 --------------------------------
9088 -- Is_Predefined_Neq_Renaming --
9089 --------------------------------
9091 function Is_Predefined_Neq_Renaming (Prim : Node_Id) return Boolean is
9092 begin
9093 return Chars (Prim) /= Name_Op_Ne
9094 and then Present (Alias (Prim))
9095 and then Comes_From_Source (Prim)
9096 and then Is_Intrinsic_Subprogram (Alias (Prim))
9097 and then Chars (Alias (Prim)) = Name_Op_Ne;
9098 end Is_Predefined_Neq_Renaming;
9100 -- Local variables
9102 Loc : constant Source_Ptr := Sloc (Parent (Tag_Typ));
9103 Stmts : constant List_Id := New_List;
9104 Decl : Node_Id;
9105 Eq_Prim : Entity_Id;
9106 Left_Op : Entity_Id;
9107 Renaming_Prim : Entity_Id;
9108 Right_Op : Entity_Id;
9109 Target : Entity_Id;
9111 -- Start of processing for Make_Neq_Body
9113 begin
9114 -- For a call on a renaming of a dispatching subprogram that is
9115 -- overridden, if the overriding occurred before the renaming, then
9116 -- the body executed is that of the overriding declaration, even if the
9117 -- overriding declaration is not visible at the place of the renaming;
9118 -- otherwise, the inherited or predefined subprogram is called, see
9119 -- (RM 8.5.4(8))
9121 -- Stage 1: Search for a renaming of the inequality primitive and also
9122 -- search for an overriding of the equality primitive located before the
9123 -- renaming declaration.
9125 declare
9126 Elmt : Elmt_Id;
9127 Prim : Node_Id;
9129 begin
9130 Eq_Prim := Empty;
9131 Renaming_Prim := Empty;
9133 Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
9134 while Present (Elmt) loop
9135 Prim := Node (Elmt);
9137 if Is_User_Defined_Equality (Prim) and then No (Alias (Prim)) then
9138 if No (Renaming_Prim) then
9139 pragma Assert (No (Eq_Prim));
9140 Eq_Prim := Prim;
9141 end if;
9143 elsif Is_Predefined_Neq_Renaming (Prim) then
9144 Renaming_Prim := Prim;
9145 end if;
9147 Next_Elmt (Elmt);
9148 end loop;
9149 end;
9151 -- No further action needed if no renaming was found
9153 if No (Renaming_Prim) then
9154 return Empty;
9155 end if;
9157 -- Stage 2: Replace the renaming declaration by a subprogram declaration
9158 -- (required to add its body)
9160 Decl := Parent (Parent (Renaming_Prim));
9161 Rewrite (Decl,
9162 Make_Subprogram_Declaration (Loc,
9163 Specification => Specification (Decl)));
9164 Set_Analyzed (Decl);
9166 -- Remove the decoration of intrinsic renaming subprogram
9168 Set_Is_Intrinsic_Subprogram (Renaming_Prim, False);
9169 Set_Convention (Renaming_Prim, Convention_Ada);
9170 Set_Alias (Renaming_Prim, Empty);
9171 Set_Has_Completion (Renaming_Prim, False);
9173 -- Stage 3: Build the corresponding body
9175 Left_Op := First_Formal (Renaming_Prim);
9176 Right_Op := Next_Formal (Left_Op);
9178 Decl :=
9179 Predef_Spec_Or_Body (Loc,
9180 Tag_Typ => Tag_Typ,
9181 Name => Chars (Renaming_Prim),
9182 Profile => New_List (
9183 Make_Parameter_Specification (Loc,
9184 Defining_Identifier =>
9185 Make_Defining_Identifier (Loc, Chars (Left_Op)),
9186 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
9188 Make_Parameter_Specification (Loc,
9189 Defining_Identifier =>
9190 Make_Defining_Identifier (Loc, Chars (Right_Op)),
9191 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
9193 Ret_Type => Standard_Boolean,
9194 For_Body => True);
9196 -- If the overriding of the equality primitive occurred before the
9197 -- renaming, then generate:
9199 -- function <Neq_Name> (X : Y : Typ) return Boolean is
9200 -- begin
9201 -- return not Oeq (X, Y);
9202 -- end;
9204 if Present (Eq_Prim) then
9205 Target := Eq_Prim;
9207 -- Otherwise build a nested subprogram which performs the predefined
9208 -- evaluation of the equality operator. That is, generate:
9210 -- function <Neq_Name> (X : Y : Typ) return Boolean is
9211 -- function Oeq (X : Y) return Boolean is
9212 -- begin
9213 -- <<body of default implementation>>
9214 -- end;
9215 -- begin
9216 -- return not Oeq (X, Y);
9217 -- end;
9219 else
9220 declare
9221 Local_Subp : Node_Id;
9222 begin
9223 Local_Subp := Make_Eq_Body (Tag_Typ, Name_Op_Eq);
9224 Set_Declarations (Decl, New_List (Local_Subp));
9225 Target := Defining_Entity (Local_Subp);
9226 end;
9227 end if;
9229 Append_To (Stmts,
9230 Make_Simple_Return_Statement (Loc,
9231 Expression =>
9232 Make_Op_Not (Loc,
9233 Make_Function_Call (Loc,
9234 Name => New_Occurrence_Of (Target, Loc),
9235 Parameter_Associations => New_List (
9236 Make_Identifier (Loc, Chars (Left_Op)),
9237 Make_Identifier (Loc, Chars (Right_Op)))))));
9239 Set_Handled_Statement_Sequence
9240 (Decl, Make_Handled_Sequence_Of_Statements (Loc, Stmts));
9241 return Decl;
9242 end Make_Neq_Body;
9244 -------------------------------
9245 -- Make_Null_Procedure_Specs --
9246 -------------------------------
9248 function Make_Null_Procedure_Specs (Tag_Typ : Entity_Id) return List_Id is
9249 Decl_List : constant List_Id := New_List;
9250 Loc : constant Source_Ptr := Sloc (Tag_Typ);
9251 Formal : Entity_Id;
9252 Formal_List : List_Id;
9253 New_Param_Spec : Node_Id;
9254 Parent_Subp : Entity_Id;
9255 Prim_Elmt : Elmt_Id;
9256 Subp : Entity_Id;
9258 begin
9259 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
9260 while Present (Prim_Elmt) loop
9261 Subp := Node (Prim_Elmt);
9263 -- If a null procedure inherited from an interface has not been
9264 -- overridden, then we build a null procedure declaration to
9265 -- override the inherited procedure.
9267 Parent_Subp := Alias (Subp);
9269 if Present (Parent_Subp)
9270 and then Is_Null_Interface_Primitive (Parent_Subp)
9271 then
9272 Formal_List := No_List;
9273 Formal := First_Formal (Subp);
9275 if Present (Formal) then
9276 Formal_List := New_List;
9278 while Present (Formal) loop
9280 -- Copy the parameter spec including default expressions
9282 New_Param_Spec :=
9283 New_Copy_Tree (Parent (Formal), New_Sloc => Loc);
9285 -- Generate a new defining identifier for the new formal.
9286 -- required because New_Copy_Tree does not duplicate
9287 -- semantic fields (except itypes).
9289 Set_Defining_Identifier (New_Param_Spec,
9290 Make_Defining_Identifier (Sloc (Formal),
9291 Chars => Chars (Formal)));
9293 -- For controlling arguments we must change their
9294 -- parameter type to reference the tagged type (instead
9295 -- of the interface type)
9297 if Is_Controlling_Formal (Formal) then
9298 if Nkind (Parameter_Type (Parent (Formal))) = N_Identifier
9299 then
9300 Set_Parameter_Type (New_Param_Spec,
9301 New_Occurrence_Of (Tag_Typ, Loc));
9303 else pragma Assert
9304 (Nkind (Parameter_Type (Parent (Formal))) =
9305 N_Access_Definition);
9306 Set_Subtype_Mark (Parameter_Type (New_Param_Spec),
9307 New_Occurrence_Of (Tag_Typ, Loc));
9308 end if;
9309 end if;
9311 Append (New_Param_Spec, Formal_List);
9313 Next_Formal (Formal);
9314 end loop;
9315 end if;
9317 Append_To (Decl_List,
9318 Make_Subprogram_Declaration (Loc,
9319 Make_Procedure_Specification (Loc,
9320 Defining_Unit_Name =>
9321 Make_Defining_Identifier (Loc, Chars (Subp)),
9322 Parameter_Specifications => Formal_List,
9323 Null_Present => True)));
9324 end if;
9326 Next_Elmt (Prim_Elmt);
9327 end loop;
9329 return Decl_List;
9330 end Make_Null_Procedure_Specs;
9332 -------------------------------------
9333 -- Make_Predefined_Primitive_Specs --
9334 -------------------------------------
9336 procedure Make_Predefined_Primitive_Specs
9337 (Tag_Typ : Entity_Id;
9338 Predef_List : out List_Id;
9339 Renamed_Eq : out Entity_Id)
9341 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean;
9342 -- Returns true if Prim is a renaming of an unresolved predefined
9343 -- equality operation.
9345 -------------------------------
9346 -- Is_Predefined_Eq_Renaming --
9347 -------------------------------
9349 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean is
9350 begin
9351 return Chars (Prim) /= Name_Op_Eq
9352 and then Present (Alias (Prim))
9353 and then Comes_From_Source (Prim)
9354 and then Is_Intrinsic_Subprogram (Alias (Prim))
9355 and then Chars (Alias (Prim)) = Name_Op_Eq;
9356 end Is_Predefined_Eq_Renaming;
9358 -- Local variables
9360 Loc : constant Source_Ptr := Sloc (Tag_Typ);
9361 Res : constant List_Id := New_List;
9362 Eq_Name : Name_Id := Name_Op_Eq;
9363 Eq_Needed : Boolean;
9364 Eq_Spec : Node_Id;
9365 Prim : Elmt_Id;
9367 Has_Predef_Eq_Renaming : Boolean := False;
9368 -- Set to True if Tag_Typ has a primitive that renames the predefined
9369 -- equality operator. Used to implement (RM 8-5-4(8)).
9371 -- Start of processing for Make_Predefined_Primitive_Specs
9373 begin
9374 Renamed_Eq := Empty;
9376 -- Spec of _Size
9378 Append_To (Res, Predef_Spec_Or_Body (Loc,
9379 Tag_Typ => Tag_Typ,
9380 Name => Name_uSize,
9381 Profile => New_List (
9382 Make_Parameter_Specification (Loc,
9383 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
9384 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
9386 Ret_Type => Standard_Long_Long_Integer));
9388 -- Specs for dispatching stream attributes
9390 declare
9391 Stream_Op_TSS_Names :
9392 constant array (Integer range <>) of TSS_Name_Type :=
9393 (TSS_Stream_Read,
9394 TSS_Stream_Write,
9395 TSS_Stream_Input,
9396 TSS_Stream_Output);
9398 begin
9399 for Op in Stream_Op_TSS_Names'Range loop
9400 if Stream_Operation_OK (Tag_Typ, Stream_Op_TSS_Names (Op)) then
9401 Append_To (Res,
9402 Predef_Stream_Attr_Spec (Loc, Tag_Typ,
9403 Stream_Op_TSS_Names (Op)));
9404 end if;
9405 end loop;
9406 end;
9408 -- Spec of "=" is expanded if the type is not limited and if a user
9409 -- defined "=" was not already declared for the non-full view of a
9410 -- private extension
9412 if not Is_Limited_Type (Tag_Typ) then
9413 Eq_Needed := True;
9414 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
9415 while Present (Prim) loop
9417 -- If a primitive is encountered that renames the predefined
9418 -- equality operator before reaching any explicit equality
9419 -- primitive, then we still need to create a predefined equality
9420 -- function, because calls to it can occur via the renaming. A
9421 -- new name is created for the equality to avoid conflicting with
9422 -- any user-defined equality. (Note that this doesn't account for
9423 -- renamings of equality nested within subpackages???)
9425 if Is_Predefined_Eq_Renaming (Node (Prim)) then
9426 Has_Predef_Eq_Renaming := True;
9427 Eq_Name := New_External_Name (Chars (Node (Prim)), 'E');
9429 -- User-defined equality
9431 elsif Is_User_Defined_Equality (Node (Prim)) then
9432 if No (Alias (Node (Prim)))
9433 or else Nkind (Unit_Declaration_Node (Node (Prim))) =
9434 N_Subprogram_Renaming_Declaration
9435 then
9436 Eq_Needed := False;
9437 exit;
9439 -- If the parent is not an interface type and has an abstract
9440 -- equality function, the inherited equality is abstract as
9441 -- well, and no body can be created for it.
9443 elsif not Is_Interface (Etype (Tag_Typ))
9444 and then Present (Alias (Node (Prim)))
9445 and then Is_Abstract_Subprogram (Alias (Node (Prim)))
9446 then
9447 Eq_Needed := False;
9448 exit;
9450 -- If the type has an equality function corresponding with
9451 -- a primitive defined in an interface type, the inherited
9452 -- equality is abstract as well, and no body can be created
9453 -- for it.
9455 elsif Present (Alias (Node (Prim)))
9456 and then Comes_From_Source (Ultimate_Alias (Node (Prim)))
9457 and then
9458 Is_Interface
9459 (Find_Dispatching_Type (Ultimate_Alias (Node (Prim))))
9460 then
9461 Eq_Needed := False;
9462 exit;
9463 end if;
9464 end if;
9466 Next_Elmt (Prim);
9467 end loop;
9469 -- If a renaming of predefined equality was found but there was no
9470 -- user-defined equality (so Eq_Needed is still true), then set the
9471 -- name back to Name_Op_Eq. But in the case where a user-defined
9472 -- equality was located after such a renaming, then the predefined
9473 -- equality function is still needed, so Eq_Needed must be set back
9474 -- to True.
9476 if Eq_Name /= Name_Op_Eq then
9477 if Eq_Needed then
9478 Eq_Name := Name_Op_Eq;
9479 else
9480 Eq_Needed := True;
9481 end if;
9482 end if;
9484 if Eq_Needed then
9485 Eq_Spec := Predef_Spec_Or_Body (Loc,
9486 Tag_Typ => Tag_Typ,
9487 Name => Eq_Name,
9488 Profile => New_List (
9489 Make_Parameter_Specification (Loc,
9490 Defining_Identifier =>
9491 Make_Defining_Identifier (Loc, Name_X),
9492 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
9494 Make_Parameter_Specification (Loc,
9495 Defining_Identifier =>
9496 Make_Defining_Identifier (Loc, Name_Y),
9497 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
9498 Ret_Type => Standard_Boolean);
9499 Append_To (Res, Eq_Spec);
9501 if Has_Predef_Eq_Renaming then
9502 Renamed_Eq := Defining_Unit_Name (Specification (Eq_Spec));
9504 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
9505 while Present (Prim) loop
9507 -- Any renamings of equality that appeared before an
9508 -- overriding equality must be updated to refer to the
9509 -- entity for the predefined equality, otherwise calls via
9510 -- the renaming would get incorrectly resolved to call the
9511 -- user-defined equality function.
9513 if Is_Predefined_Eq_Renaming (Node (Prim)) then
9514 Set_Alias (Node (Prim), Renamed_Eq);
9516 -- Exit upon encountering a user-defined equality
9518 elsif Chars (Node (Prim)) = Name_Op_Eq
9519 and then No (Alias (Node (Prim)))
9520 then
9521 exit;
9522 end if;
9524 Next_Elmt (Prim);
9525 end loop;
9526 end if;
9527 end if;
9529 -- Spec for dispatching assignment
9531 Append_To (Res, Predef_Spec_Or_Body (Loc,
9532 Tag_Typ => Tag_Typ,
9533 Name => Name_uAssign,
9534 Profile => New_List (
9535 Make_Parameter_Specification (Loc,
9536 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
9537 Out_Present => True,
9538 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
9540 Make_Parameter_Specification (Loc,
9541 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
9542 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)))));
9543 end if;
9545 -- Ada 2005: Generate declarations for the following primitive
9546 -- operations for limited interfaces and synchronized types that
9547 -- implement a limited interface.
9549 -- Disp_Asynchronous_Select
9550 -- Disp_Conditional_Select
9551 -- Disp_Get_Prim_Op_Kind
9552 -- Disp_Get_Task_Id
9553 -- Disp_Requeue
9554 -- Disp_Timed_Select
9556 -- Disable the generation of these bodies if No_Dispatching_Calls,
9557 -- Ravenscar or ZFP is active.
9559 if Ada_Version >= Ada_2005
9560 and then not Restriction_Active (No_Dispatching_Calls)
9561 and then not Restriction_Active (No_Select_Statements)
9562 and then RTE_Available (RE_Select_Specific_Data)
9563 then
9564 -- These primitives are defined abstract in interface types
9566 if Is_Interface (Tag_Typ)
9567 and then Is_Limited_Record (Tag_Typ)
9568 then
9569 Append_To (Res,
9570 Make_Abstract_Subprogram_Declaration (Loc,
9571 Specification =>
9572 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
9574 Append_To (Res,
9575 Make_Abstract_Subprogram_Declaration (Loc,
9576 Specification =>
9577 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
9579 Append_To (Res,
9580 Make_Abstract_Subprogram_Declaration (Loc,
9581 Specification =>
9582 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
9584 Append_To (Res,
9585 Make_Abstract_Subprogram_Declaration (Loc,
9586 Specification =>
9587 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
9589 Append_To (Res,
9590 Make_Abstract_Subprogram_Declaration (Loc,
9591 Specification =>
9592 Make_Disp_Requeue_Spec (Tag_Typ)));
9594 Append_To (Res,
9595 Make_Abstract_Subprogram_Declaration (Loc,
9596 Specification =>
9597 Make_Disp_Timed_Select_Spec (Tag_Typ)));
9599 -- If ancestor is an interface type, declare non-abstract primitives
9600 -- to override the abstract primitives of the interface type.
9602 -- In VM targets we define these primitives in all root tagged types
9603 -- that are not interface types. Done because in VM targets we don't
9604 -- have secondary dispatch tables and any derivation of Tag_Typ may
9605 -- cover limited interfaces (which always have these primitives since
9606 -- they may be ancestors of synchronized interface types).
9608 elsif (not Is_Interface (Tag_Typ)
9609 and then Is_Interface (Etype (Tag_Typ))
9610 and then Is_Limited_Record (Etype (Tag_Typ)))
9611 or else
9612 (Is_Concurrent_Record_Type (Tag_Typ)
9613 and then Has_Interfaces (Tag_Typ))
9614 or else
9615 (not Tagged_Type_Expansion
9616 and then not Is_Interface (Tag_Typ)
9617 and then Tag_Typ = Root_Type (Tag_Typ))
9618 then
9619 Append_To (Res,
9620 Make_Subprogram_Declaration (Loc,
9621 Specification =>
9622 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
9624 Append_To (Res,
9625 Make_Subprogram_Declaration (Loc,
9626 Specification =>
9627 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
9629 Append_To (Res,
9630 Make_Subprogram_Declaration (Loc,
9631 Specification =>
9632 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
9634 Append_To (Res,
9635 Make_Subprogram_Declaration (Loc,
9636 Specification =>
9637 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
9639 Append_To (Res,
9640 Make_Subprogram_Declaration (Loc,
9641 Specification =>
9642 Make_Disp_Requeue_Spec (Tag_Typ)));
9644 Append_To (Res,
9645 Make_Subprogram_Declaration (Loc,
9646 Specification =>
9647 Make_Disp_Timed_Select_Spec (Tag_Typ)));
9648 end if;
9649 end if;
9651 -- All tagged types receive their own Deep_Adjust and Deep_Finalize
9652 -- regardless of whether they are controlled or may contain controlled
9653 -- components.
9655 -- Do not generate the routines if finalization is disabled
9657 if Restriction_Active (No_Finalization) then
9658 null;
9660 -- Finalization is not available for CIL value types
9662 elsif Is_Value_Type (Tag_Typ) then
9663 null;
9665 else
9666 if not Is_Limited_Type (Tag_Typ) then
9667 Append_To (Res, Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust));
9668 end if;
9670 Append_To (Res, Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize));
9671 end if;
9673 Predef_List := Res;
9674 end Make_Predefined_Primitive_Specs;
9676 ---------------------------------
9677 -- Needs_Simple_Initialization --
9678 ---------------------------------
9680 function Needs_Simple_Initialization
9681 (T : Entity_Id;
9682 Consider_IS : Boolean := True) return Boolean
9684 Consider_IS_NS : constant Boolean :=
9685 Normalize_Scalars or (Initialize_Scalars and Consider_IS);
9687 begin
9688 -- Never need initialization if it is suppressed
9690 if Initialization_Suppressed (T) then
9691 return False;
9692 end if;
9694 -- Check for private type, in which case test applies to the underlying
9695 -- type of the private type.
9697 if Is_Private_Type (T) then
9698 declare
9699 RT : constant Entity_Id := Underlying_Type (T);
9700 begin
9701 if Present (RT) then
9702 return Needs_Simple_Initialization (RT);
9703 else
9704 return False;
9705 end if;
9706 end;
9708 -- Scalar type with Default_Value aspect requires initialization
9710 elsif Is_Scalar_Type (T) and then Has_Default_Aspect (T) then
9711 return True;
9713 -- Cases needing simple initialization are access types, and, if pragma
9714 -- Normalize_Scalars or Initialize_Scalars is in effect, then all scalar
9715 -- types.
9717 elsif Is_Access_Type (T)
9718 or else (Consider_IS_NS and then (Is_Scalar_Type (T)))
9719 then
9720 return True;
9722 -- If Initialize/Normalize_Scalars is in effect, string objects also
9723 -- need initialization, unless they are created in the course of
9724 -- expanding an aggregate (since in the latter case they will be
9725 -- filled with appropriate initializing values before they are used).
9727 elsif Consider_IS_NS
9728 and then Is_Standard_String_Type (T)
9729 and then
9730 (not Is_Itype (T)
9731 or else Nkind (Associated_Node_For_Itype (T)) /= N_Aggregate)
9732 then
9733 return True;
9735 else
9736 return False;
9737 end if;
9738 end Needs_Simple_Initialization;
9740 ----------------------
9741 -- Predef_Deep_Spec --
9742 ----------------------
9744 function Predef_Deep_Spec
9745 (Loc : Source_Ptr;
9746 Tag_Typ : Entity_Id;
9747 Name : TSS_Name_Type;
9748 For_Body : Boolean := False) return Node_Id
9750 Formals : List_Id;
9752 begin
9753 -- V : in out Tag_Typ
9755 Formals := New_List (
9756 Make_Parameter_Specification (Loc,
9757 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
9758 In_Present => True,
9759 Out_Present => True,
9760 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)));
9762 -- F : Boolean := True
9764 if Name = TSS_Deep_Adjust
9765 or else Name = TSS_Deep_Finalize
9766 then
9767 Append_To (Formals,
9768 Make_Parameter_Specification (Loc,
9769 Defining_Identifier => Make_Defining_Identifier (Loc, Name_F),
9770 Parameter_Type => New_Occurrence_Of (Standard_Boolean, Loc),
9771 Expression => New_Occurrence_Of (Standard_True, Loc)));
9772 end if;
9774 return
9775 Predef_Spec_Or_Body (Loc,
9776 Name => Make_TSS_Name (Tag_Typ, Name),
9777 Tag_Typ => Tag_Typ,
9778 Profile => Formals,
9779 For_Body => For_Body);
9781 exception
9782 when RE_Not_Available =>
9783 return Empty;
9784 end Predef_Deep_Spec;
9786 -------------------------
9787 -- Predef_Spec_Or_Body --
9788 -------------------------
9790 function Predef_Spec_Or_Body
9791 (Loc : Source_Ptr;
9792 Tag_Typ : Entity_Id;
9793 Name : Name_Id;
9794 Profile : List_Id;
9795 Ret_Type : Entity_Id := Empty;
9796 For_Body : Boolean := False) return Node_Id
9798 Id : constant Entity_Id := Make_Defining_Identifier (Loc, Name);
9799 Spec : Node_Id;
9801 begin
9802 Set_Is_Public (Id, Is_Public (Tag_Typ));
9804 -- The internal flag is set to mark these declarations because they have
9805 -- specific properties. First, they are primitives even if they are not
9806 -- defined in the type scope (the freezing point is not necessarily in
9807 -- the same scope). Second, the predefined equality can be overridden by
9808 -- a user-defined equality, no body will be generated in this case.
9810 Set_Is_Internal (Id);
9812 if not Debug_Generated_Code then
9813 Set_Debug_Info_Off (Id);
9814 end if;
9816 if No (Ret_Type) then
9817 Spec :=
9818 Make_Procedure_Specification (Loc,
9819 Defining_Unit_Name => Id,
9820 Parameter_Specifications => Profile);
9821 else
9822 Spec :=
9823 Make_Function_Specification (Loc,
9824 Defining_Unit_Name => Id,
9825 Parameter_Specifications => Profile,
9826 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
9827 end if;
9829 if Is_Interface (Tag_Typ) then
9830 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
9832 -- If body case, return empty subprogram body. Note that this is ill-
9833 -- formed, because there is not even a null statement, and certainly not
9834 -- a return in the function case. The caller is expected to do surgery
9835 -- on the body to add the appropriate stuff.
9837 elsif For_Body then
9838 return Make_Subprogram_Body (Loc, Spec, Empty_List, Empty);
9840 -- For the case of an Input attribute predefined for an abstract type,
9841 -- generate an abstract specification. This will never be called, but we
9842 -- need the slot allocated in the dispatching table so that attributes
9843 -- typ'Class'Input and typ'Class'Output will work properly.
9845 elsif Is_TSS (Name, TSS_Stream_Input)
9846 and then Is_Abstract_Type (Tag_Typ)
9847 then
9848 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
9850 -- Normal spec case, where we return a subprogram declaration
9852 else
9853 return Make_Subprogram_Declaration (Loc, Spec);
9854 end if;
9855 end Predef_Spec_Or_Body;
9857 -----------------------------
9858 -- Predef_Stream_Attr_Spec --
9859 -----------------------------
9861 function Predef_Stream_Attr_Spec
9862 (Loc : Source_Ptr;
9863 Tag_Typ : Entity_Id;
9864 Name : TSS_Name_Type;
9865 For_Body : Boolean := False) return Node_Id
9867 Ret_Type : Entity_Id;
9869 begin
9870 if Name = TSS_Stream_Input then
9871 Ret_Type := Tag_Typ;
9872 else
9873 Ret_Type := Empty;
9874 end if;
9876 return
9877 Predef_Spec_Or_Body
9878 (Loc,
9879 Name => Make_TSS_Name (Tag_Typ, Name),
9880 Tag_Typ => Tag_Typ,
9881 Profile => Build_Stream_Attr_Profile (Loc, Tag_Typ, Name),
9882 Ret_Type => Ret_Type,
9883 For_Body => For_Body);
9884 end Predef_Stream_Attr_Spec;
9886 ---------------------------------
9887 -- Predefined_Primitive_Bodies --
9888 ---------------------------------
9890 function Predefined_Primitive_Bodies
9891 (Tag_Typ : Entity_Id;
9892 Renamed_Eq : Entity_Id) return List_Id
9894 Loc : constant Source_Ptr := Sloc (Tag_Typ);
9895 Res : constant List_Id := New_List;
9896 Decl : Node_Id;
9897 Prim : Elmt_Id;
9898 Eq_Needed : Boolean;
9899 Eq_Name : Name_Id;
9900 Ent : Entity_Id;
9902 pragma Warnings (Off, Ent);
9904 begin
9905 pragma Assert (not Is_Interface (Tag_Typ));
9907 -- See if we have a predefined "=" operator
9909 if Present (Renamed_Eq) then
9910 Eq_Needed := True;
9911 Eq_Name := Chars (Renamed_Eq);
9913 -- If the parent is an interface type then it has defined all the
9914 -- predefined primitives abstract and we need to check if the type
9915 -- has some user defined "=" function which matches the profile of
9916 -- the Ada predefined equality operator to avoid generating it.
9918 elsif Is_Interface (Etype (Tag_Typ)) then
9919 Eq_Needed := True;
9920 Eq_Name := Name_Op_Eq;
9922 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
9923 while Present (Prim) loop
9924 if Chars (Node (Prim)) = Name_Op_Eq
9925 and then not Is_Internal (Node (Prim))
9926 and then Present (First_Entity (Node (Prim)))
9928 -- The predefined equality primitive must have exactly two
9929 -- formals whose type is this tagged type
9931 and then Present (Last_Entity (Node (Prim)))
9932 and then Next_Entity (First_Entity (Node (Prim)))
9933 = Last_Entity (Node (Prim))
9934 and then Etype (First_Entity (Node (Prim))) = Tag_Typ
9935 and then Etype (Last_Entity (Node (Prim))) = Tag_Typ
9936 then
9937 Eq_Needed := False;
9938 Eq_Name := No_Name;
9939 exit;
9940 end if;
9942 Next_Elmt (Prim);
9943 end loop;
9945 else
9946 Eq_Needed := False;
9947 Eq_Name := No_Name;
9949 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
9950 while Present (Prim) loop
9951 if Chars (Node (Prim)) = Name_Op_Eq
9952 and then Is_Internal (Node (Prim))
9953 then
9954 Eq_Needed := True;
9955 Eq_Name := Name_Op_Eq;
9956 exit;
9957 end if;
9959 Next_Elmt (Prim);
9960 end loop;
9961 end if;
9963 -- Body of _Size
9965 Decl := Predef_Spec_Or_Body (Loc,
9966 Tag_Typ => Tag_Typ,
9967 Name => Name_uSize,
9968 Profile => New_List (
9969 Make_Parameter_Specification (Loc,
9970 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
9971 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
9973 Ret_Type => Standard_Long_Long_Integer,
9974 For_Body => True);
9976 Set_Handled_Statement_Sequence (Decl,
9977 Make_Handled_Sequence_Of_Statements (Loc, New_List (
9978 Make_Simple_Return_Statement (Loc,
9979 Expression =>
9980 Make_Attribute_Reference (Loc,
9981 Prefix => Make_Identifier (Loc, Name_X),
9982 Attribute_Name => Name_Size)))));
9984 Append_To (Res, Decl);
9986 -- Bodies for Dispatching stream IO routines. We need these only for
9987 -- non-limited types (in the limited case there is no dispatching).
9988 -- We also skip them if dispatching or finalization are not available.
9990 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Read)
9991 and then No (TSS (Tag_Typ, TSS_Stream_Read))
9992 then
9993 Build_Record_Read_Procedure (Loc, Tag_Typ, Decl, Ent);
9994 Append_To (Res, Decl);
9995 end if;
9997 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Write)
9998 and then No (TSS (Tag_Typ, TSS_Stream_Write))
9999 then
10000 Build_Record_Write_Procedure (Loc, Tag_Typ, Decl, Ent);
10001 Append_To (Res, Decl);
10002 end if;
10004 -- Skip body of _Input for the abstract case, since the corresponding
10005 -- spec is abstract (see Predef_Spec_Or_Body).
10007 if not Is_Abstract_Type (Tag_Typ)
10008 and then Stream_Operation_OK (Tag_Typ, TSS_Stream_Input)
10009 and then No (TSS (Tag_Typ, TSS_Stream_Input))
10010 then
10011 Build_Record_Or_Elementary_Input_Function
10012 (Loc, Tag_Typ, Decl, Ent);
10013 Append_To (Res, Decl);
10014 end if;
10016 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Output)
10017 and then No (TSS (Tag_Typ, TSS_Stream_Output))
10018 then
10019 Build_Record_Or_Elementary_Output_Procedure (Loc, Tag_Typ, Decl, Ent);
10020 Append_To (Res, Decl);
10021 end if;
10023 -- Ada 2005: Generate bodies for the following primitive operations for
10024 -- limited interfaces and synchronized types that implement a limited
10025 -- interface.
10027 -- disp_asynchronous_select
10028 -- disp_conditional_select
10029 -- disp_get_prim_op_kind
10030 -- disp_get_task_id
10031 -- disp_timed_select
10033 -- The interface versions will have null bodies
10035 -- Disable the generation of these bodies if No_Dispatching_Calls,
10036 -- Ravenscar or ZFP is active.
10038 -- In VM targets we define these primitives in all root tagged types
10039 -- that are not interface types. Done because in VM targets we don't
10040 -- have secondary dispatch tables and any derivation of Tag_Typ may
10041 -- cover limited interfaces (which always have these primitives since
10042 -- they may be ancestors of synchronized interface types).
10044 if Ada_Version >= Ada_2005
10045 and then not Is_Interface (Tag_Typ)
10046 and then
10047 ((Is_Interface (Etype (Tag_Typ))
10048 and then Is_Limited_Record (Etype (Tag_Typ)))
10049 or else
10050 (Is_Concurrent_Record_Type (Tag_Typ)
10051 and then Has_Interfaces (Tag_Typ))
10052 or else
10053 (not Tagged_Type_Expansion
10054 and then Tag_Typ = Root_Type (Tag_Typ)))
10055 and then not Restriction_Active (No_Dispatching_Calls)
10056 and then not Restriction_Active (No_Select_Statements)
10057 and then RTE_Available (RE_Select_Specific_Data)
10058 then
10059 Append_To (Res, Make_Disp_Asynchronous_Select_Body (Tag_Typ));
10060 Append_To (Res, Make_Disp_Conditional_Select_Body (Tag_Typ));
10061 Append_To (Res, Make_Disp_Get_Prim_Op_Kind_Body (Tag_Typ));
10062 Append_To (Res, Make_Disp_Get_Task_Id_Body (Tag_Typ));
10063 Append_To (Res, Make_Disp_Requeue_Body (Tag_Typ));
10064 Append_To (Res, Make_Disp_Timed_Select_Body (Tag_Typ));
10065 end if;
10067 if not Is_Limited_Type (Tag_Typ) and then not Is_Interface (Tag_Typ) then
10069 -- Body for equality
10071 if Eq_Needed then
10072 Decl := Make_Eq_Body (Tag_Typ, Eq_Name);
10073 Append_To (Res, Decl);
10074 end if;
10076 -- Body for inequality (if required)
10078 Decl := Make_Neq_Body (Tag_Typ);
10080 if Present (Decl) then
10081 Append_To (Res, Decl);
10082 end if;
10084 -- Body for dispatching assignment
10086 Decl :=
10087 Predef_Spec_Or_Body (Loc,
10088 Tag_Typ => Tag_Typ,
10089 Name => Name_uAssign,
10090 Profile => New_List (
10091 Make_Parameter_Specification (Loc,
10092 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
10093 Out_Present => True,
10094 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
10096 Make_Parameter_Specification (Loc,
10097 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
10098 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
10099 For_Body => True);
10101 Set_Handled_Statement_Sequence (Decl,
10102 Make_Handled_Sequence_Of_Statements (Loc, New_List (
10103 Make_Assignment_Statement (Loc,
10104 Name => Make_Identifier (Loc, Name_X),
10105 Expression => Make_Identifier (Loc, Name_Y)))));
10107 Append_To (Res, Decl);
10108 end if;
10110 -- Generate empty bodies of routines Deep_Adjust and Deep_Finalize for
10111 -- tagged types which do not contain controlled components.
10113 -- Do not generate the routines if finalization is disabled
10115 if Restriction_Active (No_Finalization) then
10116 null;
10118 elsif not Has_Controlled_Component (Tag_Typ) then
10119 if not Is_Limited_Type (Tag_Typ) then
10120 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust, True);
10122 if Is_Controlled (Tag_Typ) then
10123 Set_Handled_Statement_Sequence (Decl,
10124 Make_Handled_Sequence_Of_Statements (Loc,
10125 Statements => New_List (
10126 Make_Adjust_Call (
10127 Obj_Ref => Make_Identifier (Loc, Name_V),
10128 Typ => Tag_Typ))));
10130 else
10131 Set_Handled_Statement_Sequence (Decl,
10132 Make_Handled_Sequence_Of_Statements (Loc,
10133 Statements => New_List (
10134 Make_Null_Statement (Loc))));
10135 end if;
10137 Append_To (Res, Decl);
10138 end if;
10140 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize, True);
10142 if Is_Controlled (Tag_Typ) then
10143 Set_Handled_Statement_Sequence (Decl,
10144 Make_Handled_Sequence_Of_Statements (Loc,
10145 Statements => New_List (
10146 Make_Final_Call
10147 (Obj_Ref => Make_Identifier (Loc, Name_V),
10148 Typ => Tag_Typ))));
10150 else
10151 Set_Handled_Statement_Sequence (Decl,
10152 Make_Handled_Sequence_Of_Statements (Loc,
10153 Statements => New_List (Make_Null_Statement (Loc))));
10154 end if;
10156 Append_To (Res, Decl);
10157 end if;
10159 return Res;
10160 end Predefined_Primitive_Bodies;
10162 ---------------------------------
10163 -- Predefined_Primitive_Freeze --
10164 ---------------------------------
10166 function Predefined_Primitive_Freeze
10167 (Tag_Typ : Entity_Id) return List_Id
10169 Res : constant List_Id := New_List;
10170 Prim : Elmt_Id;
10171 Frnodes : List_Id;
10173 begin
10174 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
10175 while Present (Prim) loop
10176 if Is_Predefined_Dispatching_Operation (Node (Prim)) then
10177 Frnodes := Freeze_Entity (Node (Prim), Tag_Typ);
10179 if Present (Frnodes) then
10180 Append_List_To (Res, Frnodes);
10181 end if;
10182 end if;
10184 Next_Elmt (Prim);
10185 end loop;
10187 return Res;
10188 end Predefined_Primitive_Freeze;
10190 -------------------------
10191 -- Stream_Operation_OK --
10192 -------------------------
10194 function Stream_Operation_OK
10195 (Typ : Entity_Id;
10196 Operation : TSS_Name_Type) return Boolean
10198 Has_Predefined_Or_Specified_Stream_Attribute : Boolean := False;
10200 begin
10201 -- Special case of a limited type extension: a default implementation
10202 -- of the stream attributes Read or Write exists if that attribute
10203 -- has been specified or is available for an ancestor type; a default
10204 -- implementation of the attribute Output (resp. Input) exists if the
10205 -- attribute has been specified or Write (resp. Read) is available for
10206 -- an ancestor type. The last condition only applies under Ada 2005.
10208 if Is_Limited_Type (Typ) and then Is_Tagged_Type (Typ) then
10209 if Operation = TSS_Stream_Read then
10210 Has_Predefined_Or_Specified_Stream_Attribute :=
10211 Has_Specified_Stream_Read (Typ);
10213 elsif Operation = TSS_Stream_Write then
10214 Has_Predefined_Or_Specified_Stream_Attribute :=
10215 Has_Specified_Stream_Write (Typ);
10217 elsif Operation = TSS_Stream_Input then
10218 Has_Predefined_Or_Specified_Stream_Attribute :=
10219 Has_Specified_Stream_Input (Typ)
10220 or else
10221 (Ada_Version >= Ada_2005
10222 and then Stream_Operation_OK (Typ, TSS_Stream_Read));
10224 elsif Operation = TSS_Stream_Output then
10225 Has_Predefined_Or_Specified_Stream_Attribute :=
10226 Has_Specified_Stream_Output (Typ)
10227 or else
10228 (Ada_Version >= Ada_2005
10229 and then Stream_Operation_OK (Typ, TSS_Stream_Write));
10230 end if;
10232 -- Case of inherited TSS_Stream_Read or TSS_Stream_Write
10234 if not Has_Predefined_Or_Specified_Stream_Attribute
10235 and then Is_Derived_Type (Typ)
10236 and then (Operation = TSS_Stream_Read
10237 or else Operation = TSS_Stream_Write)
10238 then
10239 Has_Predefined_Or_Specified_Stream_Attribute :=
10240 Present
10241 (Find_Inherited_TSS (Base_Type (Etype (Typ)), Operation));
10242 end if;
10243 end if;
10245 -- If the type is not limited, or else is limited but the attribute is
10246 -- explicitly specified or is predefined for the type, then return True,
10247 -- unless other conditions prevail, such as restrictions prohibiting
10248 -- streams or dispatching operations. We also return True for limited
10249 -- interfaces, because they may be extended by nonlimited types and
10250 -- permit inheritance in this case (addresses cases where an abstract
10251 -- extension doesn't get 'Input declared, as per comments below, but
10252 -- 'Class'Input must still be allowed). Note that attempts to apply
10253 -- stream attributes to a limited interface or its class-wide type
10254 -- (or limited extensions thereof) will still get properly rejected
10255 -- by Check_Stream_Attribute.
10257 -- We exclude the Input operation from being a predefined subprogram in
10258 -- the case where the associated type is an abstract extension, because
10259 -- the attribute is not callable in that case, per 13.13.2(49/2). Also,
10260 -- we don't want an abstract version created because types derived from
10261 -- the abstract type may not even have Input available (for example if
10262 -- derived from a private view of the abstract type that doesn't have
10263 -- a visible Input), but a VM such as .NET or the Java VM can treat the
10264 -- operation as inherited anyway, and we don't want an abstract function
10265 -- to be (implicitly) inherited in that case because it can lead to a VM
10266 -- exception.
10268 -- Do not generate stream routines for type Finalization_Master because
10269 -- a master may never appear in types and therefore cannot be read or
10270 -- written.
10272 return
10273 (not Is_Limited_Type (Typ)
10274 or else Is_Interface (Typ)
10275 or else Has_Predefined_Or_Specified_Stream_Attribute)
10276 and then
10277 (Operation /= TSS_Stream_Input
10278 or else not Is_Abstract_Type (Typ)
10279 or else not Is_Derived_Type (Typ))
10280 and then not Has_Unknown_Discriminants (Typ)
10281 and then not
10282 (Is_Interface (Typ)
10283 and then
10284 (Is_Task_Interface (Typ)
10285 or else Is_Protected_Interface (Typ)
10286 or else Is_Synchronized_Interface (Typ)))
10287 and then not Restriction_Active (No_Streams)
10288 and then not Restriction_Active (No_Dispatch)
10289 and then not No_Run_Time_Mode
10290 and then RTE_Available (RE_Tag)
10291 and then No (Type_Without_Stream_Operation (Typ))
10292 and then RTE_Available (RE_Root_Stream_Type)
10293 and then not Is_RTE (Typ, RE_Finalization_Master);
10294 end Stream_Operation_OK;
10296 end Exp_Ch3;