Debian Codegraph @

gcc/ada/exp_ch3.adb

raw
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-2025, 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 ------------------------------------------------------------------------------
25
26 with Accessibility; use Accessibility;
27 with Aspects; use Aspects;
28 with Atree; use Atree;
29 with Checks; use Checks;
30 with Contracts; use Contracts;
31 with Einfo; use Einfo;
32 with Einfo.Entities; use Einfo.Entities;
33 with Einfo.Utils; use Einfo.Utils;
34 with Errout; use Errout;
35 with Exp_Aggr; use Exp_Aggr;
36 with Exp_Atag; use Exp_Atag;
37 with Exp_Ch4; use Exp_Ch4;
38 with Exp_Ch6; use Exp_Ch6;
39 with Exp_Ch7; use Exp_Ch7;
40 with Exp_Ch9; use Exp_Ch9;
41 with Exp_Disp; use Exp_Disp;
42 with Exp_Dist; use Exp_Dist;
43 with Exp_Put_Image;
44 with Exp_Smem; use Exp_Smem;
45 with Exp_Strm; use Exp_Strm;
46 with Exp_Util; use Exp_Util;
47 with Freeze; use Freeze;
48 with Ghost; use Ghost;
49 with Lib; use Lib;
50 with Mutably_Tagged; use Mutably_Tagged;
51 with Namet; use Namet;
52 with Nlists; use Nlists;
53 with Nmake; use Nmake;
54 with Opt; use Opt;
55 with Restrict; use Restrict;
56 with Rident; use Rident;
57 with Rtsfind; use Rtsfind;
58 with Sem; use Sem;
59 with Sem_Aux; use Sem_Aux;
60 with Sem_Attr; use Sem_Attr;
61 with Sem_Cat; use Sem_Cat;
62 with Sem_Ch3; use Sem_Ch3;
63 with Sem_Ch6; use Sem_Ch6;
64 with Sem_Ch8; use Sem_Ch8;
65 with Sem_Disp; use Sem_Disp;
66 with Sem_Eval; use Sem_Eval;
67 with Sem_Mech; use Sem_Mech;
68 with Sem_Res; use Sem_Res;
69 with Sem_SCIL; use Sem_SCIL;
70 with Sem_Type; use Sem_Type;
71 with Sem_Util; use Sem_Util;
72 with Sinfo; use Sinfo;
73 with Sinfo.Nodes; use Sinfo.Nodes;
74 with Sinfo.Utils; use Sinfo.Utils;
75 with Stand; use Stand;
76 with Snames; use Snames;
77 with Tbuild; use Tbuild;
78 with Ttypes; use Ttypes;
79 with Validsw; use Validsw;
80
81 package body Exp_Ch3 is
82
83 -----------------------
84 -- Local Subprograms --
85 -----------------------
86
87 procedure Adjust_Discriminants (Rtype : Entity_Id);
88 -- This is used when freezing a record type. It attempts to construct
89 -- more restrictive subtypes for discriminants so that the max size of
90 -- the record can be calculated more accurately. See the body of this
91 -- procedure for details.
92
93 procedure Build_Array_Init_Proc (A_Type : Entity_Id; Nod : Node_Id);
94 -- Build initialization procedure for given array type. Nod is a node
95 -- used for attachment of any actions required in its construction.
96 -- It also supplies the source location used for the procedure.
97
98 function Build_Discriminant_Formals
99 (Rec_Id : Entity_Id;
100 Use_Dl : Boolean) return List_Id;
101 -- This function uses the discriminants of a type to build a list of
102 -- formal parameters, used in Build_Init_Procedure among other places.
103 -- If the flag Use_Dl is set, the list is built using the already
104 -- defined discriminals of the type, as is the case for concurrent
105 -- types with discriminants. Otherwise new identifiers are created,
106 -- with the source names of the discriminants.
107
108 procedure Build_Discr_Checking_Funcs (N : Node_Id);
109 -- For each variant component, builds a function which checks whether
110 -- the component name is consistent with the current discriminants
111 -- and sets the component's Dcheck_Function attribute to refer to it.
112 -- N is the full type declaration node; the discriminant checking
113 -- functions are inserted after this node.
114
115 function Build_Equivalent_Array_Aggregate (T : Entity_Id) return Node_Id;
116 -- This function builds a static aggregate that can serve as the initial
117 -- value for an array type whose bounds are static, and whose component
118 -- type is a composite type that has a static equivalent aggregate.
119 -- The equivalent array aggregate is used both for object initialization
120 -- and for component initialization, when used in the following function.
121
122 function Build_Equivalent_Record_Aggregate (T : Entity_Id) return Node_Id;
123 -- This function builds a static aggregate that can serve as the initial
124 -- value for a record type whose components are scalar and initialized
125 -- with compile-time values, or arrays with similar initialization or
126 -- defaults. When possible, initialization of an object of the type can
127 -- be achieved by using a copy of the aggregate as an initial value, thus
128 -- removing the implicit call that would otherwise constitute elaboration
129 -- code.
130
131 procedure Build_Record_Init_Proc (N : Node_Id; Rec_Ent : Entity_Id);
132 -- Build record initialization procedure. N is the type declaration
133 -- node, and Rec_Ent is the corresponding entity for the record type.
134
135 procedure Build_Slice_Assignment (Typ : Entity_Id);
136 -- Build assignment procedure for one-dimensional arrays of controlled
137 -- types. Other array and slice assignments are expanded in-line, but
138 -- the code expansion for controlled components (when control actions
139 -- are active) can lead to very large blocks that GCC handles poorly.
140
141 procedure Build_Untagged_Record_Equality (Typ : Entity_Id);
142 -- AI05-0123: Equality on untagged records composes. This procedure
143 -- builds the equality routine for an untagged record that has components
144 -- of a record type that has user-defined primitive equality operations.
145 -- The resulting operation is a TSS subprogram.
146
147 procedure Check_Stream_Attributes (Typ : Entity_Id);
148 -- Check that if a limited extension has a parent with user-defined stream
149 -- attributes, and does not itself have user-defined stream-attributes,
150 -- then any limited component of the extension also has the corresponding
151 -- user-defined stream attributes.
152
153 procedure Clean_Task_Names
154 (Typ : Entity_Id;
155 Proc_Id : Entity_Id);
156 -- If an initialization procedure includes calls to generate names
157 -- for task subcomponents, indicate that secondary stack cleanup is
158 -- needed after an initialization. Typ is the component type, and Proc_Id
159 -- the initialization procedure for the enclosing composite type.
160
161 procedure Copy_Discr_Checking_Funcs (N : Node_Id);
162 -- For a derived untagged type, copy the attributes that were set
163 -- for the components of the parent type onto the components of the
164 -- derived type. No new subprograms are constructed.
165 -- N is the full type declaration node, as for Build_Discr_Checking_Funcs.
166
167 procedure Expand_Freeze_Array_Type (N : Node_Id);
168 -- Freeze an array type. Deals with building the initialization procedure,
169 -- creating the packed array type for a packed array and also with the
170 -- creation of the controlling procedures for the controlled case. The
171 -- argument N is the N_Freeze_Entity node for the type.
172
173 procedure Expand_Freeze_Class_Wide_Type (N : Node_Id);
174 -- Freeze a class-wide type. Build routine Finalize_Address for the purpose
175 -- of finalizing controlled derivations from the class-wide's root type.
176
177 procedure Expand_Freeze_Enumeration_Type (N : Node_Id);
178 -- Freeze enumeration type with non-standard representation. Builds the
179 -- array and function needed to convert between enumeration pos and
180 -- enumeration representation values. N is the N_Freeze_Entity node
181 -- for the type.
182
183 procedure Expand_Freeze_Record_Type (N : Node_Id);
184 -- Freeze record type. Builds all necessary discriminant checking
185 -- and other ancillary functions, and builds dispatch tables where
186 -- needed. The argument N is the N_Freeze_Entity node. This processing
187 -- applies only to E_Record_Type entities, not to class wide types,
188 -- record subtypes, or private types.
189
190 procedure Expand_Tagged_Root (T : Entity_Id);
191 -- Add a field _Tag at the beginning of the record. This field carries
192 -- the value of the access to the Dispatch table. This procedure is only
193 -- called on root type, the _Tag field being inherited by the descendants.
194
195 procedure Freeze_Stream_Operations (N : Node_Id; Typ : Entity_Id);
196 -- Treat user-defined stream operations as renaming_as_body if the
197 -- subprogram they rename is not frozen when the type is frozen.
198
199 package Initialization_Control is
200
201 function Requires_Late_Init
202 (Decl : Node_Id; Rec_Type : Entity_Id) return Boolean;
203 -- Return True iff the given component declaration requires late
204 -- initialization, as defined by 3.3.1 (8.1/5).
205
206 function Has_Late_Init_Component
207 (Tagged_Rec_Type : Entity_Id) return Boolean;
208 -- Return True iff the given tagged record type has at least one
209 -- component that requires late initialization; this includes
210 -- components of ancestor types.
211
212 type Initialization_Mode is
213 (Full_Init, Full_Init_Except_Tag, Early_Init_Only, Late_Init_Only);
214 -- The initialization routine for a tagged type is passed in a
215 -- formal parameter of this type, indicating what initialization
216 -- is to be performed. This parameter defaults to Full_Init in all
217 -- cases except when the init proc of a type extension (let's call
218 -- that type T2) calls the init proc of its parent (let's call that
219 -- type T1). In that case, one of the other 3 values will
220 -- be passed in. In all three of those cases, the Tag component has
221 -- already been initialized before the call and is therefore not to be
222 -- modified. T2's init proc will either call T1's init proc
223 -- once (with Full_Init_Except_Tag as the parameter value) or twice
224 -- (first with Early_Init_Only, then later with Late_Init_Only),
225 -- depending on the result returned by Has_Late_Init_Component (T1).
226 -- In the latter case, the first call does not initialize any
227 -- components that require late initialization and the second call
228 -- then performs that deferred initialization.
229 -- Strictly speaking, the formal parameter subtype is actually Natural
230 -- but calls will only pass in values corresponding to literals
231 -- of this enumeration type.
232
233 function Make_Mode_Literal
234 (Loc : Source_Ptr; Mode : Initialization_Mode) return Node_Id
235 is (Make_Integer_Literal (Loc, Initialization_Mode'Pos (Mode)));
236 -- Generate an integer literal for a given mode value.
237
238 function Tag_Init_Condition
239 (Loc : Source_Ptr;
240 Init_Control_Formal : Entity_Id) return Node_Id;
241 function Early_Init_Condition
242 (Loc : Source_Ptr;
243 Init_Control_Formal : Entity_Id) return Node_Id;
244 function Late_Init_Condition
245 (Loc : Source_Ptr;
246 Init_Control_Formal : Entity_Id) return Node_Id;
247 -- These three functions each return a Boolean expression that
248 -- can be used to determine whether a given call to the initialization
249 -- expression for a tagged type should initialize (respectively)
250 -- the Tag component, the non-Tag components that do not require late
251 -- initialization, and the components that do require late
252 -- initialization.
253
254 end Initialization_Control;
255
256 procedure Initialization_Warning (E : Entity_Id);
257 -- If static elaboration of the package is requested, indicate
258 -- when a type does meet the conditions for static initialization. If
259 -- E is a type, it has components that have no static initialization.
260 -- if E is an entity, its initial expression is not compile-time known.
261
262 function Init_Formals (Typ : Entity_Id; Proc_Id : Entity_Id) return List_Id;
263 -- This function builds the list of formals for an initialization routine.
264 -- The first formal is always _Init with the given type. For task value
265 -- record types and types containing tasks, three additional formals are
266 -- added and Proc_Id is decorated with attribute Has_Master_Entity:
267 --
268 -- _Master : Master_Id
269 -- _Chain : in out Activation_Chain
270 -- _Task_Name : String
271 --
272 -- The caller must append additional entries for discriminants if required.
273
274 function Inline_Init_Proc (Typ : Entity_Id) return Boolean;
275 -- Returns true if the initialization procedure of Typ should be inlined
276
277 function In_Runtime (E : Entity_Id) return Boolean;
278 -- Check if E is defined in the RTL (in a child of Ada or System). Used
279 -- to avoid to bring in the overhead of _Input, _Output for tagged types.
280
281 function Is_Null_Statement_List (Stmts : List_Id) return Boolean;
282 -- Returns true if Stmts is made of null statements only, possibly wrapped
283 -- in a case statement, recursively. This latter pattern may occur for the
284 -- initialization procedure of an unchecked union.
285
286 function Make_Eq_Body
287 (Typ : Entity_Id;
288 Eq_Name : Name_Id) return Node_Id;
289 -- Build the body of a primitive equality operation for a tagged record
290 -- type, or in Ada 2012 for any record type that has components with a
291 -- user-defined equality. Factored out of Predefined_Primitive_Bodies.
292
293 function Make_Eq_Case
294 (E : Entity_Id;
295 CL : Node_Id;
296 Discrs : Elist_Id := New_Elmt_List) return List_Id;
297 -- Building block for variant record equality. Defined to share the code
298 -- between the tagged and untagged case. Given a Component_List node CL,
299 -- it generates an 'if' followed by a 'case' statement that compares all
300 -- components of local temporaries named X and Y (that are declared as
301 -- formals at some upper level). E provides the Sloc to be used for the
302 -- generated code.
303 --
304 -- IF E is an unchecked_union, Discrs is the list of formals created for
305 -- the inferred discriminants of one operand. These formals are used in
306 -- the generated case statements for each variant of the unchecked union.
307
308 function Make_Eq_If
309 (E : Entity_Id;
310 L : List_Id) return Node_Id;
311 -- Building block for variant record equality. Defined to share the code
312 -- between the tagged and untagged case. Given the list of components
313 -- (or discriminants) L, it generates a return statement that compares all
314 -- components of local temporaries named X and Y (that are declared as
315 -- formals at some upper level). E provides the Sloc to be used for the
316 -- generated code.
317
318 function Make_Neq_Body (Tag_Typ : Entity_Id) return Node_Id;
319 -- Search for a renaming of the inequality dispatching primitive of
320 -- this tagged type. If found then build and return the corresponding
321 -- rename-as-body inequality subprogram; otherwise return Empty.
322
323 procedure Make_Predefined_Primitive_Specs
324 (Tag_Typ : Entity_Id;
325 Predef_List : out List_Id;
326 Renamed_Eq : out Entity_Id);
327 -- Create a list with the specs of the predefined primitive operations.
328 -- For tagged types that are interfaces all these primitives are defined
329 -- abstract.
330 --
331 -- The following entries are present for all tagged types, and provide
332 -- the results of the corresponding attribute applied to the object.
333 -- Dispatching is required in general, since the result of the attribute
334 -- will vary with the actual object subtype.
335 --
336 -- _size provides result of 'Size attribute
337 -- typSR provides result of 'Read attribute
338 -- typSW provides result of 'Write attribute
339 -- typSI provides result of 'Input attribute
340 -- typSO provides result of 'Output attribute
341 -- typPI provides result of 'Put_Image attribute
342 --
343 -- The following entries are additionally present for non-limited tagged
344 -- types, and implement additional dispatching operations for predefined
345 -- operations:
346 --
347 -- _equality implements "=" operator
348 -- _assign implements assignment operation
349 -- typDF implements deep finalization
350 -- typDA implements deep adjust
351 --
352 -- The latter two are empty procedures unless the type contains some
353 -- controlled components that require finalization actions (the deep
354 -- in the name refers to the fact that the action applies to components).
355 --
356 -- The list of specs is returned in Predef_List
357
358 function Has_New_Non_Standard_Rep (T : Entity_Id) return Boolean;
359 -- Returns True if there are representation clauses for type T that are not
360 -- inherited. If the result is false, the init_proc and the discriminant
361 -- checking functions of the parent can be reused by a derived type.
362
363 function Make_Null_Procedure_Specs (Tag_Typ : Entity_Id) return List_Id;
364 -- Ada 2005 (AI-251): Makes specs for null procedures associated with any
365 -- null procedures inherited from an interface type that have not been
366 -- overridden. Only one null procedure will be created for a given set of
367 -- inherited null procedures with homographic profiles.
368
369 function Predef_Spec_Or_Body
370 (Loc : Source_Ptr;
371 Tag_Typ : Entity_Id;
372 Name : Name_Id;
373 Profile : List_Id;
374 Ret_Type : Entity_Id := Empty;
375 For_Body : Boolean := False) return Node_Id;
376 -- This function generates the appropriate expansion for a predefined
377 -- primitive operation specified by its name, parameter profile and
378 -- return type (Empty means this is a procedure). If For_Body is false,
379 -- then the returned node is a subprogram declaration. If For_Body is
380 -- true, then the returned node is a empty subprogram body containing
381 -- no declarations and no statements.
382
383 function Predef_Stream_Attr_Spec
384 (Loc : Source_Ptr;
385 Tag_Typ : Entity_Id;
386 Name : TSS_Name_Type) return Node_Id;
387 -- Specialized version of Predef_Spec_Or_Body that apply to read, write,
388 -- input and output attribute whose specs are constructed in Exp_Strm.
389
390 function Predef_Deep_Spec
391 (Loc : Source_Ptr;
392 Tag_Typ : Entity_Id;
393 Name : TSS_Name_Type;
394 For_Body : Boolean := False) return Node_Id;
395 -- Specialized version of Predef_Spec_Or_Body that apply to _deep_adjust
396 -- and _deep_finalize
397
398 function Predefined_Primitive_Bodies
399 (Tag_Typ : Entity_Id;
400 Renamed_Eq : Entity_Id) return List_Id;
401 -- Create the bodies of the predefined primitives that are described in
402 -- Predefined_Primitive_Specs. When not empty, Renamed_Eq must denote
403 -- the defining unit name of the type's predefined equality as returned
404 -- by Make_Predefined_Primitive_Specs.
405
406 function Predefined_Primitive_Freeze (Tag_Typ : Entity_Id) return List_Id;
407 -- Freeze entities of all predefined primitive operations. This is needed
408 -- because the bodies of these operations do not normally do any freezing.
409
410 --------------------------
411 -- Adjust_Discriminants --
412 --------------------------
413
414 -- This procedure attempts to define subtypes for discriminants that are
415 -- more restrictive than those declared. Such a replacement is possible if
416 -- we can demonstrate that values outside the restricted range would cause
417 -- constraint errors in any case. The advantage of restricting the
418 -- discriminant types in this way is that the maximum size of the variant
419 -- record can be calculated more conservatively.
420
421 -- An example of a situation in which we can perform this type of
422 -- restriction is the following:
423
424 -- subtype B is range 1 .. 10;
425 -- type Q is array (B range <>) of Integer;
426
427 -- type V (N : Natural) is record
428 -- C : Q (1 .. N);
429 -- end record;
430
431 -- In this situation, we can restrict the upper bound of N to 10, since
432 -- any larger value would cause a constraint error in any case.
433
434 -- There are many situations in which such restriction is possible, but
435 -- for now, we just look for cases like the above, where the component
436 -- in question is a one dimensional array whose upper bound is one of
437 -- the record discriminants. Also the component must not be part of
438 -- any variant part, since then the component does not always exist.
439
440 procedure Adjust_Discriminants (Rtype : Entity_Id) is
441 Loc : constant Source_Ptr := Sloc (Rtype);
442 Comp : Entity_Id;
443 Ctyp : Entity_Id;
444 Ityp : Entity_Id;
445 Lo : Node_Id;
446 Hi : Node_Id;
447 P : Node_Id;
448 Loval : Uint;
449 Discr : Entity_Id;
450 Dtyp : Entity_Id;
451 Dhi : Node_Id;
452 Dhiv : Uint;
453 Ahi : Node_Id;
454 Ahiv : Uint;
455 Tnn : Entity_Id;
456
457 begin
458 Comp := First_Component (Rtype);
459 while Present (Comp) loop
460
461 -- If our parent is a variant, quit, we do not look at components
462 -- that are in variant parts, because they may not always exist.
463
464 P := Parent (Comp); -- component declaration
465 P := Parent (P); -- component list
466
467 exit when Nkind (Parent (P)) = N_Variant;
468
469 -- We are looking for a one dimensional array type
470
471 Ctyp := Etype (Comp);
472
473 if not Is_Array_Type (Ctyp) or else Number_Dimensions (Ctyp) > 1 then
474 goto Continue;
475 end if;
476
477 -- The lower bound must be constant, and the upper bound is a
478 -- discriminant (which is a discriminant of the current record).
479
480 Ityp := Etype (First_Index (Ctyp));
481 Lo := Type_Low_Bound (Ityp);
482 Hi := Type_High_Bound (Ityp);
483
484 if not Compile_Time_Known_Value (Lo)
485 or else Nkind (Hi) /= N_Identifier
486 or else No (Entity (Hi))
487 or else Ekind (Entity (Hi)) /= E_Discriminant
488 then
489 goto Continue;
490 end if;
491
492 -- We have an array with appropriate bounds
493
494 Loval := Expr_Value (Lo);
495 Discr := Entity (Hi);
496 Dtyp := Etype (Discr);
497
498 -- See if the discriminant has a known upper bound
499
500 Dhi := Type_High_Bound (Dtyp);
501
502 if not Compile_Time_Known_Value (Dhi) then
503 goto Continue;
504 end if;
505
506 Dhiv := Expr_Value (Dhi);
507
508 -- See if base type of component array has known upper bound
509
510 Ahi := Type_High_Bound (Etype (First_Index (Base_Type (Ctyp))));
511
512 if not Compile_Time_Known_Value (Ahi) then
513 goto Continue;
514 end if;
515
516 Ahiv := Expr_Value (Ahi);
517
518 -- The condition for doing the restriction is that the high bound
519 -- of the discriminant is greater than the low bound of the array,
520 -- and is also greater than the high bound of the base type index.
521
522 if Dhiv > Loval and then Dhiv > Ahiv then
523
524 -- We can reset the upper bound of the discriminant type to
525 -- whichever is larger, the low bound of the component, or
526 -- the high bound of the base type array index.
527
528 -- We build a subtype that is declared as
529
530 -- subtype Tnn is discr_type range discr_type'First .. max;
531
532 -- And insert this declaration into the tree. The type of the
533 -- discriminant is then reset to this more restricted subtype.
534
535 Tnn := Make_Temporary (Loc, 'T');
536
537 Insert_Action (Declaration_Node (Rtype),
538 Make_Subtype_Declaration (Loc,
539 Defining_Identifier => Tnn,
540 Subtype_Indication =>
541 Make_Subtype_Indication (Loc,
542 Subtype_Mark => New_Occurrence_Of (Dtyp, Loc),
543 Constraint =>
544 Make_Range_Constraint (Loc,
545 Range_Expression =>
546 Make_Range (Loc,
547 Low_Bound =>
548 Make_Attribute_Reference (Loc,
549 Attribute_Name => Name_First,
550 Prefix => New_Occurrence_Of (Dtyp, Loc)),
551 High_Bound =>
552 Make_Integer_Literal (Loc,
553 Intval => UI_Max (Loval, Ahiv)))))));
554
555 Set_Etype (Discr, Tnn);
556 end if;
557
558 <<Continue>>
559 Next_Component (Comp);
560 end loop;
561 end Adjust_Discriminants;
562
563 ------------------------------------------
564 -- Build_Access_Subprogram_Wrapper_Body --
565 ------------------------------------------
566
567 procedure Build_Access_Subprogram_Wrapper_Body
568 (Decl : Node_Id;
569 New_Decl : Node_Id)
570 is
571 Loc : constant Source_Ptr := Sloc (Decl);
572 Actuals : constant List_Id := New_List;
573 Type_Def : constant Node_Id := Type_Definition (Decl);
574 Type_Id : constant Entity_Id := Defining_Identifier (Decl);
575 Spec_Node : constant Node_Id :=
576 Copy_Subprogram_Spec (Specification (New_Decl));
577 -- This copy creates new identifiers for formals and subprogram.
578
579 Act : Node_Id;
580 Body_Node : Node_Id;
581 Call_Stmt : Node_Id;
582 Ptr : Entity_Id;
583
584 begin
585 -- Create List of actuals for indirect call. The last parameter of the
586 -- subprogram declaration is the access value for the indirect call.
587
588 Act := First (Parameter_Specifications (Spec_Node));
589
590 while Present (Act) loop
591 exit when Act = Last (Parameter_Specifications (Spec_Node));
592 Append_To (Actuals,
593 Make_Identifier (Loc, Chars (Defining_Identifier (Act))));
594 Next (Act);
595 end loop;
596
597 Ptr :=
598 Defining_Identifier
599 (Last (Parameter_Specifications (Specification (New_Decl))));
600
601 if Nkind (Type_Def) = N_Access_Procedure_Definition then
602 Call_Stmt := Make_Procedure_Call_Statement (Loc,
603 Name =>
604 Make_Explicit_Dereference
605 (Loc, New_Occurrence_Of (Ptr, Loc)),
606 Parameter_Associations => Actuals);
607 else
608 Call_Stmt := Make_Simple_Return_Statement (Loc,
609 Expression =>
610 Make_Function_Call (Loc,
611 Name => Make_Explicit_Dereference
612 (Loc, New_Occurrence_Of (Ptr, Loc)),
613 Parameter_Associations => Actuals));
614 end if;
615
616 Body_Node := Make_Subprogram_Body (Loc,
617 Specification => Spec_Node,
618 Declarations => New_List,
619 Handled_Statement_Sequence =>
620 Make_Handled_Sequence_Of_Statements (Loc,
621 Statements => New_List (Call_Stmt)));
622
623 -- Place body in list of freeze actions for the type.
624
625 Append_Freeze_Action (Type_Id, Body_Node);
626 end Build_Access_Subprogram_Wrapper_Body;
627
628 ---------------------------
629 -- Build_Array_Init_Proc --
630 ---------------------------
631
632 procedure Build_Array_Init_Proc (A_Type : Entity_Id; Nod : Node_Id) is
633 -- Obtain the corresponding mutably tagged type's parent subtype to
634 -- handle default initialization.
635
636 Comp_Type : constant Entity_Id :=
637 Get_Corresponding_Tagged_Type_If_Present (Component_Type (A_Type));
638
639 Comp_Simple_Init : constant Boolean :=
640 Needs_Simple_Initialization
641 (Typ => Comp_Type,
642 Consider_IS =>
643 not (Validity_Check_Copies and Is_Bit_Packed_Array (A_Type)));
644 -- True if the component needs simple initialization, based on its type,
645 -- plus the fact that we do not do simple initialization for components
646 -- of bit-packed arrays when validity checks are enabled, because the
647 -- initialization with deliberately out-of-range values would raise
648 -- Constraint_Error.
649
650 Body_Stmts : List_Id;
651 Has_Default_Init : Boolean;
652 Index_List : List_Id;
653 Loc : Source_Ptr;
654 Parameters : List_Id;
655 Proc_Id : Entity_Id;
656
657 function Init_Component return List_Id;
658 -- Create one statement to initialize one array component, designated
659 -- by a full set of indexes.
660
661 function Init_One_Dimension (N : Int) return List_Id;
662 -- Create loop to initialize one dimension of the array. The single
663 -- statement in the loop body initializes the inner dimensions if any,
664 -- or else the single component. Note that this procedure is called
665 -- recursively, with N being the dimension to be initialized. A call
666 -- with N greater than the number of dimensions simply generates the
667 -- component initialization, terminating the recursion.
668
669 --------------------
670 -- Init_Component --
671 --------------------
672
673 function Init_Component return List_Id is
674 Comp : Node_Id;
675
676 begin
677 Comp :=
678 Make_Indexed_Component (Loc,
679 Prefix => Make_Identifier (Loc, Name_uInit),
680 Expressions => Index_List);
681
682 if Has_Default_Aspect (A_Type) then
683 Set_Assignment_OK (Comp);
684 return New_List (
685 Make_Assignment_Statement (Loc,
686 Name => Comp,
687 Expression =>
688 Convert_To (Comp_Type,
689 Default_Aspect_Component_Value (First_Subtype (A_Type)))));
690
691 elsif Comp_Simple_Init then
692 Set_Assignment_OK (Comp);
693 return New_List (
694 Make_Assignment_Statement (Loc,
695 Name => Comp,
696 Expression =>
697 Get_Simple_Init_Val
698 (Typ => Comp_Type,
699 N => Nod,
700 Size => Component_Size (A_Type))));
701
702 else
703 Clean_Task_Names (Comp_Type, Proc_Id);
704 return
705 Build_Initialization_Call
706 (N => Nod,
707 Id_Ref => Comp,
708 Typ => Comp_Type,
709 In_Init_Proc => True,
710 Enclos_Type => A_Type);
711 end if;
712 end Init_Component;
713
714 ------------------------
715 -- Init_One_Dimension --
716 ------------------------
717
718 function Init_One_Dimension (N : Int) return List_Id is
719 Index : Entity_Id;
720 DIC_Call : Node_Id;
721 Result_List : List_Id;
722
723 function Possible_DIC_Call return Node_Id;
724 -- If the component type has Default_Initial_Conditions and a DIC
725 -- procedure that is not an empty body, then builds a call to the
726 -- DIC procedure and returns it.
727
728 -----------------------
729 -- Possible_DIC_Call --
730 -----------------------
731
732 function Possible_DIC_Call return Node_Id is
733 begin
734 -- When the component's type has a Default_Initial_Condition, then
735 -- create a call for the DIC check.
736
737 if Has_DIC (Comp_Type)
738 -- In GNATprove mode, the component DICs are checked by other
739 -- means. They should not be added to the record type DIC
740 -- procedure, so that the procedure can be used to check the
741 -- record type invariants or DICs if any.
742
743 and then not GNATprove_Mode
744
745 -- DIC checks for components of controlled types are done later
746 -- (see Exp_Ch7.Make_Deep_Array_Body).
747
748 and then not Is_Controlled (Comp_Type)
749
750 and then Present (DIC_Procedure (Comp_Type))
751
752 and then not Has_Null_Body (DIC_Procedure (Comp_Type))
753 then
754 return
755 Build_DIC_Call (Loc,
756 Make_Indexed_Component (Loc,
757 Prefix => Make_Identifier (Loc, Name_uInit),
758 Expressions => Index_List),
759 Comp_Type);
760 else
761 return Empty;
762 end if;
763 end Possible_DIC_Call;
764
765 -- Start of processing for Init_One_Dimension
766
767 begin
768 -- If the component does not need initializing, then there is nothing
769 -- to do here, so we return a null body. This occurs when generating
770 -- the dummy Init_Proc needed for Initialize_Scalars processing.
771 -- An exception is if component type has a Default_Initial_Condition,
772 -- in which case we generate a call to the type's DIC procedure.
773
774 if not Has_Non_Null_Base_Init_Proc (Comp_Type)
775 and then not Comp_Simple_Init
776 and then not Has_Task (Comp_Type)
777 and then not Has_Default_Aspect (A_Type)
778 and then (not Has_DIC (Comp_Type)
779 or else N > Number_Dimensions (A_Type))
780 then
781 DIC_Call := Possible_DIC_Call;
782
783 if Present (DIC_Call) then
784 return New_List (DIC_Call);
785 else
786 return New_List (Make_Null_Statement (Loc));
787 end if;
788
789 -- If all dimensions dealt with, we simply initialize the component
790 -- and append a call to component type's DIC procedure when needed.
791
792 elsif N > Number_Dimensions (A_Type) then
793 DIC_Call := Possible_DIC_Call;
794
795 if Present (DIC_Call) then
796 Result_List := Init_Component;
797 Append (DIC_Call, Result_List);
798 return Result_List;
799
800 else
801 return Init_Component;
802 end if;
803
804 -- Here we generate the required loop
805
806 else
807 Index :=
808 Make_Defining_Identifier (Loc, New_External_Name ('J', N));
809
810 Append (New_Occurrence_Of (Index, Loc), Index_List);
811
812 return New_List (
813 Make_Implicit_Loop_Statement (Nod,
814 Identifier => Empty,
815 Iteration_Scheme =>
816 Make_Iteration_Scheme (Loc,
817 Loop_Parameter_Specification =>
818 Make_Loop_Parameter_Specification (Loc,
819 Defining_Identifier => Index,
820 Discrete_Subtype_Definition =>
821 Make_Attribute_Reference (Loc,
822 Prefix =>
823 Make_Identifier (Loc, Name_uInit),
824 Attribute_Name => Name_Range,
825 Expressions => New_List (
826 Make_Integer_Literal (Loc, N))))),
827 Statements => Init_One_Dimension (N + 1)));
828 end if;
829 end Init_One_Dimension;
830
831 -- Start of processing for Build_Array_Init_Proc
832
833 begin
834 -- The init proc is created when analyzing the freeze node for the type,
835 -- but it properly belongs with the array type declaration. However, if
836 -- the freeze node is for a subtype of a type declared in another unit
837 -- it seems preferable to use the freeze node as the source location of
838 -- the init proc. In any case this is preferable for gcov usage, and
839 -- the Sloc is not otherwise used by the compiler.
840
841 if In_Open_Scopes (Scope (A_Type)) then
842 Loc := Sloc (A_Type);
843 else
844 Loc := Sloc (Nod);
845 end if;
846
847 -- Nothing to generate in the following cases:
848
849 -- 1. Initialization is suppressed for the type
850 -- 2. An initialization already exists for the base type
851
852 if Initialization_Suppressed (A_Type)
853 or else Present (Base_Init_Proc (A_Type))
854 then
855 return;
856 end if;
857
858 Index_List := New_List;
859
860 -- We need an initialization procedure if any of the following is true:
861
862 -- 1. The component type has an initialization procedure
863 -- 2. The component type needs simple initialization
864 -- 3. Tasks are present
865 -- 4. The type is marked as a public entity
866 -- 5. The array type has a Default_Component_Value aspect
867 -- 6. The array component type has a Default_Initialization_Condition
868
869 -- The reason for the public entity test is to deal properly with the
870 -- Initialize_Scalars pragma. This pragma can be set in the client and
871 -- not in the declaring package, this means the client will make a call
872 -- to the initialization procedure (because one of conditions 1-3 must
873 -- apply in this case), and we must generate a procedure (even if it is
874 -- null) to satisfy the call in this case.
875
876 -- Exception: do not build an array init_proc for a type whose root
877 -- type is Standard.String or Standard.Wide_[Wide_]String, since there
878 -- is no place to put the code, and in any case we handle initialization
879 -- of such types (in the Initialize_Scalars case, that's the only time
880 -- the issue arises) in a special manner anyway which does not need an
881 -- init_proc.
882
883 Has_Default_Init := Has_Non_Null_Base_Init_Proc (Comp_Type)
884 or else Comp_Simple_Init
885 or else Has_Task (Comp_Type)
886 or else Has_Default_Aspect (A_Type)
887 or else Has_DIC (Comp_Type);
888
889 if Has_Default_Init
890 or else (not Restriction_Active (No_Initialize_Scalars)
891 and then Is_Public (A_Type)
892 and then not Is_Standard_String_Type (A_Type))
893 then
894 Proc_Id :=
895 Make_Defining_Identifier (Loc,
896 Chars => Make_Init_Proc_Name (A_Type));
897
898 -- If No_Default_Initialization restriction is active, then we don't
899 -- want to build an init_proc, but we need to mark that an init_proc
900 -- would be needed if this restriction was not active (so that we can
901 -- detect attempts to call it), so set a dummy init_proc in place.
902 -- This is only done though when actual default initialization is
903 -- needed (and not done when only Is_Public is True), since otherwise
904 -- objects such as arrays of scalars could be wrongly flagged as
905 -- violating the restriction.
906
907 if Restriction_Active (No_Default_Initialization) then
908 if Has_Default_Init then
909 Set_Init_Proc (A_Type, Proc_Id);
910 end if;
911
912 return;
913 end if;
914
915 Body_Stmts := Init_One_Dimension (1);
916 Parameters := Init_Formals (A_Type, Proc_Id);
917
918 Discard_Node (
919 Make_Subprogram_Body (Loc,
920 Specification =>
921 Make_Procedure_Specification (Loc,
922 Defining_Unit_Name => Proc_Id,
923 Parameter_Specifications => Parameters),
924 Declarations => New_List,
925 Handled_Statement_Sequence =>
926 Make_Handled_Sequence_Of_Statements (Loc,
927 Statements => Body_Stmts)));
928
929 Mutate_Ekind (Proc_Id, E_Procedure);
930 Set_Is_Public (Proc_Id, Is_Public (A_Type));
931 Set_Is_Internal (Proc_Id);
932 Set_Has_Completion (Proc_Id);
933
934 if not Debug_Generated_Code then
935 Set_Debug_Info_Off (Proc_Id);
936 end if;
937
938 -- Set Inlined on Init_Proc if it is set on the Init_Proc of the
939 -- component type itself (see also Build_Record_Init_Proc).
940
941 Set_Is_Inlined (Proc_Id, Inline_Init_Proc (Comp_Type));
942
943 -- Associate Init_Proc with type, and determine if the procedure
944 -- is null (happens because of the Initialize_Scalars pragma case,
945 -- where we have to generate a null procedure in case it is called
946 -- by a client with Initialize_Scalars set). Such procedures have
947 -- to be generated, but do not have to be called, so we mark them
948 -- as null to suppress the call. Kill also warnings for the _Init
949 -- out parameter, which is left entirely uninitialized.
950
951 Set_Init_Proc (A_Type, Proc_Id);
952
953 if Is_Null_Statement_List (Body_Stmts) then
954 Set_Is_Null_Init_Proc (Proc_Id);
955 Set_Warnings_Off (Defining_Identifier (First (Parameters)));
956
957 else
958 -- Try to build a static aggregate to statically initialize
959 -- objects of the type. This can only be done for constrained
960 -- one-dimensional arrays with static bounds.
961
962 Set_Static_Initialization
963 (Proc_Id,
964 Build_Equivalent_Array_Aggregate (First_Subtype (A_Type)));
965 end if;
966 end if;
967 end Build_Array_Init_Proc;
968
969 ----------------------------------
970 -- Build_Default_Initialization --
971 ----------------------------------
972
973 function Build_Default_Initialization
974 (N : Node_Id;
975 Typ : Entity_Id;
976 Obj_Id : Entity_Id;
977 For_CW : Boolean := False;
978 Target_Ref : Node_Id := Empty) return List_Id
979 is
980 Exceptions_OK : constant Boolean :=
981 not Restriction_Active (No_Exception_Propagation);
982 Loc : constant Source_Ptr := Sloc (N);
983
984 function New_Object_Reference return Node_Id;
985 -- Return either a reference to Obj_Id or a dereference of Obj_Id
986
987 --------------------------
988 -- New_Object_Reference --
989 --------------------------
990
991 function New_Object_Reference return Node_Id is
992 Obj_Ref : Node_Id := New_Occurrence_Of (Obj_Id, Loc);
993
994 begin
995 if Nkind (N) = N_Object_Declaration then
996 -- The call to the type init proc or [Deep_]Finalize must not
997 -- freeze the object since the call is internally generated.
998 -- This prevents representation clauses from being rejected.
999 -- Note that the initialization call may be removed if pragma
1000 -- Import is encountered or moved to the freeze actions of
1001 -- the object if an address clause is encountered.
1002
1003 Set_Assignment_OK (Obj_Ref);
1004 Set_Must_Not_Freeze (Obj_Ref);
1005
1006 else pragma Assert (Nkind (N) = N_Allocator);
1007 Obj_Ref := Make_Explicit_Dereference (Loc, Obj_Ref);
1008
1009 -- If the designated subtype is unconstrained and the allocator
1010 -- specifies a constrained subtype, or such a subtype has been
1011 -- created, associate that subtype with the dereference of the
1012 -- allocator's access value. This is needed by the expander for
1013 -- cases where the access type has a Designated_Storage_Model
1014 -- in order to support allocation of a host object of the right
1015 -- size for passing to the initialization procedure.
1016
1017 if not Is_Constrained (Designated_Type (Etype (N)))
1018 and then Is_Constrained (Typ)
1019 then
1020 Set_Actual_Designated_Subtype (Obj_Ref, Typ);
1021 end if;
1022
1023 -- The initialization procedure expects a specific type so.
1024 -- if the context is access to class-wide, indicate that the
1025 -- object being initialized has the right specific type.
1026
1027 if For_CW then
1028 Obj_Ref := Unchecked_Convert_To (Typ, Obj_Ref);
1029 end if;
1030 end if;
1031
1032 return Obj_Ref;
1033 end New_Object_Reference;
1034
1035 -- Local variables
1036
1037 Comp_Init : List_Id := No_List;
1038 Fin_Block : Node_Id;
1039 Fin_Call : Node_Id;
1040 Init_Stmts : List_Id := No_List;
1041 Obj_Init : Node_Id := Empty;
1042 Obj_Ref : Node_Id;
1043
1044 -- Start of processing for Build_Default_Initialization
1045
1046 begin
1047 -- The expansion performed by this routine is as follows:
1048
1049 -- begin
1050 -- Abort_Defer;
1051 -- Type_Init_Proc (Obj);
1052
1053 -- begin
1054 -- [Deep_]Initialize (Obj);
1055
1056 -- exception
1057 -- when others =>
1058 -- [Deep_]Finalize (Obj, Self => False);
1059 -- raise;
1060 -- end;
1061 -- at end
1062 -- Abort_Undefer_Direct;
1063 -- end;
1064
1065 -- Initialize the components of the object
1066
1067 if Has_Non_Null_Base_Init_Proc (Typ)
1068 and then not Initialization_Suppressed (Typ)
1069 then
1070 -- Do not initialize the components if No_Default_Initialization
1071 -- applies as the actual restriction check will occur later when
1072 -- the object is frozen as it is not known yet whether the object
1073 -- is imported or not.
1074
1075 if not Restriction_Active (No_Default_Initialization) then
1076
1077 -- Invoke the type init proc, generate:
1078 -- Type_Init_Proc (Obj);
1079
1080 Obj_Ref := New_Object_Reference;
1081
1082 if Comes_From_Source (Obj_Id) then
1083 Initialization_Warning (Obj_Ref);
1084 end if;
1085
1086 Comp_Init :=
1087 Build_Initialization_Call (N,
1088 Obj_Ref, Typ, Target_Ref => Target_Ref);
1089 end if;
1090 end if;
1091
1092 -- Initialize the object, generate:
1093 -- [Deep_]Initialize (Obj);
1094
1095 if Needs_Finalization (Typ) then
1096 Obj_Init :=
1097 Make_Init_Call
1098 (Obj_Ref => New_Object_Reference,
1099 Typ => Typ);
1100 end if;
1101
1102 -- Build a special finalization block when both the object and its
1103 -- controlled components are to be initialized. The block finalizes
1104 -- the components if the object initialization fails. Generate:
1105
1106 -- begin
1107 -- <Obj_Init>
1108
1109 -- exception
1110 -- when others =>
1111 -- <Fin_Call>
1112 -- raise;
1113 -- end;
1114
1115 if Has_Controlled_Component (Typ)
1116 and then Present (Comp_Init)
1117 and then Present (Obj_Init)
1118 and then Exceptions_OK
1119 then
1120 Init_Stmts := Comp_Init;
1121
1122 Fin_Call :=
1123 Make_Final_Call
1124 (Obj_Ref => New_Object_Reference,
1125 Typ => Typ,
1126 Skip_Self => True);
1127
1128 if Present (Fin_Call) then
1129
1130 -- Do not emit warnings related to the elaboration order when a
1131 -- controlled object is declared before the body of Finalize is
1132 -- seen.
1133
1134 if Legacy_Elaboration_Checks then
1135 Set_No_Elaboration_Check (Fin_Call);
1136 end if;
1137
1138 Fin_Block :=
1139 Make_Block_Statement (Loc,
1140 Declarations => No_List,
1141
1142 Handled_Statement_Sequence =>
1143 Make_Handled_Sequence_Of_Statements (Loc,
1144 Statements => New_List (Obj_Init),
1145
1146 Exception_Handlers => New_List (
1147 Make_Exception_Handler (Loc,
1148 Exception_Choices => New_List (
1149 Make_Others_Choice (Loc)),
1150
1151 Statements => New_List (
1152 Fin_Call,
1153 Make_Raise_Statement (Loc))))));
1154
1155 -- Signal the ABE mechanism that the block carries out
1156 -- initialization actions.
1157
1158 Set_Is_Initialization_Block (Fin_Block);
1159
1160 Append_To (Init_Stmts, Fin_Block);
1161 end if;
1162
1163 -- Otherwise finalization is not required, the initialization calls
1164 -- are passed to the abort block building circuitry, generate:
1165
1166 -- Type_Init_Proc (Obj);
1167 -- [Deep_]Initialize (Obj);
1168
1169 else
1170 if Present (Comp_Init) then
1171 Init_Stmts := Comp_Init;
1172 end if;
1173
1174 if Present (Obj_Init) then
1175 if No (Init_Stmts) then
1176 Init_Stmts := New_List;
1177 end if;
1178
1179 Append_To (Init_Stmts, Obj_Init);
1180 end if;
1181 end if;
1182
1183 -- Build an abort block to protect the initialization calls, except for
1184 -- a finalization collection, which does not need any protection.
1185
1186 if Abort_Allowed
1187 and then Present (Comp_Init)
1188 and then Present (Obj_Init)
1189 and then not Is_RTE (Typ, RE_Finalization_Collection)
1190 then
1191 -- Generate:
1192 -- Abort_Defer;
1193
1194 Prepend_To (Init_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
1195
1196 -- When exceptions are propagated, abort deferral must take place
1197 -- in the presence of initialization or finalization exceptions.
1198 -- Generate:
1199
1200 -- begin
1201 -- Abort_Defer;
1202 -- <Init_Stmts>
1203 -- at end
1204 -- Abort_Undefer_Direct;
1205 -- end;
1206
1207 if Exceptions_OK then
1208 Init_Stmts := New_List (
1209 Build_Abort_Undefer_Block (Loc,
1210 Stmts => Init_Stmts,
1211 Context => N));
1212
1213 -- Otherwise exceptions are not propagated. Generate:
1214
1215 -- Abort_Defer;
1216 -- <Init_Stmts>
1217 -- Abort_Undefer;
1218
1219 else
1220 Append_To (Init_Stmts,
1221 Build_Runtime_Call (Loc, RE_Abort_Undefer));
1222 end if;
1223 end if;
1224
1225 return Init_Stmts;
1226 end Build_Default_Initialization;
1227
1228 -----------------------------------------
1229 -- Build_Default_Simple_Initialization --
1230 -----------------------------------------
1231
1232 function Build_Default_Simple_Initialization
1233 (N : Node_Id;
1234 Typ : Entity_Id;
1235 Obj_Id : Entity_Id) return Node_Id
1236 is
1237 Loc : constant Source_Ptr := Sloc (N);
1238
1239 function Build_Equivalent_Aggregate return Node_Id;
1240 -- If the object has a constrained discriminated type and no initial
1241 -- value, it may be possible to build an equivalent aggregate instead,
1242 -- and prevent an actual call to the initialization procedure.
1243
1244 function Simple_Initialization_OK (Typ : Entity_Id) return Boolean;
1245 -- Determine whether object declaration N with entity Obj_Id if set, or
1246 -- object allocation N if Obj_Id is empty, needs simple initialization,
1247 -- assuming that it is of type Typ.
1248
1249 --------------------------------
1250 -- Build_Equivalent_Aggregate --
1251 --------------------------------
1252
1253 function Build_Equivalent_Aggregate return Node_Id is
1254 Aggr : Node_Id;
1255 Comp : Entity_Id;
1256 Discr : Elmt_Id;
1257 Full_Typ : Entity_Id;
1258
1259 begin
1260 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
1261 Full_Typ := Full_View (Typ);
1262 else
1263 Full_Typ := Typ;
1264 end if;
1265
1266 -- Only do this transformation for a package entity of a constrained
1267 -- record type and if Elaboration_Code is forbidden or undesirable.
1268
1269 -- If Initialize_Scalars might be active this transformation cannot
1270 -- be performed either, because it will lead to different semantics
1271 -- or because elaboration code will in fact be created.
1272
1273 if Ekind (Full_Typ) /= E_Record_Subtype
1274 or else not Has_Discriminants (Full_Typ)
1275 or else not Is_Constrained (Full_Typ)
1276 or else Is_Controlled (Full_Typ)
1277 or else Is_Limited_Type (Full_Typ)
1278 or else Ekind (Current_Scope) /= E_Package
1279 or else not (Is_Preelaborated (Current_Scope)
1280 or else Restriction_Active (No_Elaboration_Code))
1281 or else not Restriction_Active (No_Initialize_Scalars)
1282 then
1283 return Empty;
1284 end if;
1285
1286 -- Building a static aggregate is possible if the discriminants
1287 -- have static values and the other components have static
1288 -- defaults or none.
1289
1290 Discr := First_Elmt (Discriminant_Constraint (Full_Typ));
1291 while Present (Discr) loop
1292 if not Is_OK_Static_Expression (Node (Discr)) then
1293 return Empty;
1294 end if;
1295
1296 Next_Elmt (Discr);
1297 end loop;
1298
1299 -- Check that initialized components are OK, and that non-
1300 -- initialized components do not require a call to their own
1301 -- initialization procedure.
1302
1303 Comp := First_Component (Full_Typ);
1304 while Present (Comp) loop
1305 if Present (Expression (Parent (Comp)))
1306 and then not Is_OK_Static_Expression (Expression (Parent (Comp)))
1307 then
1308 return Empty;
1309
1310 elsif Has_Non_Null_Base_Init_Proc (Etype (Comp)) then
1311 return Empty;
1312
1313 end if;
1314
1315 Next_Component (Comp);
1316 end loop;
1317
1318 -- Everything is static, assemble the aggregate, discriminant
1319 -- values first.
1320
1321 Aggr :=
1322 Make_Aggregate (Loc,
1323 Expressions => New_List,
1324 Component_Associations => New_List);
1325 Set_Parent (Aggr, N);
1326
1327 Discr := First_Elmt (Discriminant_Constraint (Full_Typ));
1328 while Present (Discr) loop
1329 Append_To (Expressions (Aggr), New_Copy (Node (Discr)));
1330 Next_Elmt (Discr);
1331 end loop;
1332
1333 -- Now collect values of initialized components
1334
1335 Comp := First_Component (Full_Typ);
1336 while Present (Comp) loop
1337 if Present (Expression (Parent (Comp))) then
1338 Append_To (Component_Associations (Aggr),
1339 Make_Component_Association (Loc,
1340 Choices => New_List (New_Occurrence_Of (Comp, Loc)),
1341 Expression => New_Copy_Tree
1342 (Expression (Parent (Comp)))));
1343 end if;
1344
1345 Next_Component (Comp);
1346 end loop;
1347
1348 -- Finally, box-initialize remaining components
1349
1350 Append_To (Component_Associations (Aggr),
1351 Make_Component_Association (Loc,
1352 Choices => New_List (Make_Others_Choice (Loc)),
1353 Box_Present => True));
1354
1355 if Typ /= Full_Typ then
1356 Analyze_And_Resolve (Aggr, Full_View (Base_Type (Full_Typ)));
1357 Rewrite (Aggr, Unchecked_Convert_To (Typ, Aggr));
1358 end if;
1359
1360 return Aggr;
1361 end Build_Equivalent_Aggregate;
1362
1363 ------------------------------
1364 -- Simple_Initialization_OK --
1365 ------------------------------
1366
1367 function Simple_Initialization_OK (Typ : Entity_Id) return Boolean is
1368 begin
1369 -- Skip internal entities as specified in Einfo
1370
1371 return
1372 not (Present (Obj_Id) and then Is_Internal (Obj_Id))
1373 and then not Is_Mutably_Tagged_CW_Equivalent_Type (Typ)
1374 and then
1375 Needs_Simple_Initialization
1376 (Typ => Typ,
1377 Consider_IS =>
1378 Initialize_Scalars
1379 and then (No (Obj_Id)
1380 or else No (Following_Address_Clause (N))));
1381 end Simple_Initialization_OK;
1382
1383 -- Local variables
1384
1385 Aggr_Init : Node_Id;
1386
1387 -- Start of processing for Build_Default_Simple_Initialization
1388
1389 begin
1390 if Has_Non_Null_Base_Init_Proc (Typ)
1391 and then not Is_Dispatching_Operation (Base_Init_Proc (Typ))
1392 and then not Initialization_Suppressed (Typ)
1393 then
1394 -- Do not initialize the components if No_Default_Initialization
1395 -- applies as the actual restriction check will occur later when
1396 -- the object is frozen as it is not known yet whether the object
1397 -- is imported or not.
1398
1399 if not Restriction_Active (No_Default_Initialization) then
1400
1401 -- If the values of the components are compile-time known, use
1402 -- their prebuilt aggregate form directly.
1403
1404 Aggr_Init := Static_Initialization (Base_Init_Proc (Typ));
1405 if Present (Aggr_Init) then
1406 return New_Copy_Tree (Aggr_Init, New_Scope => Current_Scope);
1407 end if;
1408
1409 -- If type has discriminants, try to build an equivalent
1410 -- aggregate using discriminant values from the declaration.
1411 -- This is a useful optimization, in particular if restriction
1412 -- No_Elaboration_Code is active.
1413
1414 Aggr_Init := Build_Equivalent_Aggregate;
1415 if Present (Aggr_Init) then
1416 return Aggr_Init;
1417 end if;
1418
1419 -- Optimize the default initialization of an array object when
1420 -- pragma Initialize_Scalars or Normalize_Scalars is in effect.
1421 -- Construct an in-place initialization aggregate which may be
1422 -- convert into a fast memset by the backend.
1423
1424 if Init_Or_Norm_Scalars
1425 and then Is_Array_Type (Typ)
1426
1427 -- The array must lack atomic components because they are
1428 -- treated as non-static, and as a result the backend will
1429 -- not initialize the memory in one go.
1430
1431 and then not Has_Atomic_Components (Typ)
1432
1433 -- The array must not be packed because the invalid values
1434 -- in System.Scalar_Values are multiples of Storage_Unit.
1435
1436 and then not Is_Packed (Typ)
1437
1438 -- The array must have static non-empty ranges, otherwise
1439 -- the backend cannot initialize the memory in one go.
1440
1441 and then Has_Static_Non_Empty_Array_Bounds (Typ)
1442
1443 -- The optimization is only relevant for arrays of scalar
1444 -- types.
1445
1446 and then Is_Scalar_Type (Component_Type (Typ))
1447
1448 -- Similar to regular array initialization using a type
1449 -- init proc, predicate checks are not performed because the
1450 -- initialization values are intentionally invalid, and may
1451 -- violate the predicate.
1452
1453 and then not Has_Predicates (Component_Type (Typ))
1454
1455 -- Array default component value takes precedence over
1456 -- Init_Or_Norm_Scalars.
1457
1458 and then No (Find_Aspect (Typ, Aspect_Default_Component_Value))
1459
1460 -- The component type must have a single initialization value
1461
1462 and then Simple_Initialization_OK (Component_Type (Typ))
1463 then
1464 return
1465 Get_Simple_Init_Val
1466 (Typ => Typ,
1467 N => N,
1468 Size => (if Known_Esize (Typ)
1469 then Esize (Typ)
1470 else Uint_0));
1471 end if;
1472 end if;
1473
1474 -- Provide a default value if the object needs simple initialization
1475
1476 elsif Simple_Initialization_OK (Typ) then
1477 return
1478 Get_Simple_Init_Val
1479 (Typ => Typ,
1480 N => N,
1481 Size => (if Known_Esize (Typ)
1482 then Esize (Typ)
1483 else Uint_0));
1484 end if;
1485
1486 return Empty;
1487 end Build_Default_Simple_Initialization;
1488
1489 --------------------------------
1490 -- Build_Discr_Checking_Funcs --
1491 --------------------------------
1492
1493 procedure Build_Discr_Checking_Funcs (N : Node_Id) is
1494 Rec_Id : Entity_Id;
1495 Loc : Source_Ptr;
1496 Enclosing_Func_Id : Entity_Id;
1497 Sequence : Nat := 1;
1498 Type_Def : Node_Id;
1499 V : Node_Id;
1500
1501 function Build_Case_Statement
1502 (Case_Id : Entity_Id;
1503 Variant : Node_Id) return Node_Id;
1504 -- Build a case statement containing only two alternatives. The first
1505 -- alternative corresponds to the discrete choices given on the variant
1506 -- that contains the components that we are generating the checks
1507 -- for. If the discriminant is one of these return False. The second
1508 -- alternative is an OTHERS choice that returns True indicating the
1509 -- discriminant did not match.
1510
1511 function Build_Dcheck_Function
1512 (Case_Id : Entity_Id;
1513 Variant : Node_Id) return Entity_Id;
1514 -- Build the discriminant checking function for a given variant
1515
1516 procedure Build_Dcheck_Functions (Variant_Part_Node : Node_Id);
1517 -- Builds the discriminant checking function for each variant of the
1518 -- given variant part of the record type.
1519
1520 --------------------------
1521 -- Build_Case_Statement --
1522 --------------------------
1523
1524 function Build_Case_Statement
1525 (Case_Id : Entity_Id;
1526 Variant : Node_Id) return Node_Id
1527 is
1528 Alt_List : constant List_Id := New_List;
1529 Actuals_List : List_Id;
1530 Case_Node : Node_Id;
1531 Case_Alt_Node : Node_Id;
1532 Choice : Node_Id;
1533 Choice_List : List_Id;
1534 D : Entity_Id;
1535 Return_Node : Node_Id;
1536
1537 begin
1538 Case_Node := New_Node (N_Case_Statement, Loc);
1539 Set_End_Span (Case_Node, Uint_0);
1540
1541 -- Replace the discriminant which controls the variant with the name
1542 -- of the formal of the checking function.
1543
1544 Set_Expression (Case_Node, Make_Identifier (Loc, Chars (Case_Id)));
1545
1546 Choice := First (Discrete_Choices (Variant));
1547
1548 if Nkind (Choice) = N_Others_Choice then
1549 Choice_List := New_Copy_List (Others_Discrete_Choices (Choice));
1550 else
1551 Choice_List := New_Copy_List (Discrete_Choices (Variant));
1552 end if;
1553
1554 if not Is_Empty_List (Choice_List) then
1555 Case_Alt_Node := New_Node (N_Case_Statement_Alternative, Loc);
1556 Set_Discrete_Choices (Case_Alt_Node, Choice_List);
1557
1558 -- In case this is a nested variant, we need to return the result
1559 -- of the discriminant checking function for the immediately
1560 -- enclosing variant.
1561
1562 if Present (Enclosing_Func_Id) then
1563 Actuals_List := New_List;
1564
1565 D := First_Discriminant (Rec_Id);
1566 while Present (D) loop
1567 Append (Make_Identifier (Loc, Chars (D)), Actuals_List);
1568 Next_Discriminant (D);
1569 end loop;
1570
1571 Return_Node :=
1572 Make_Simple_Return_Statement (Loc,
1573 Expression =>
1574 Make_Function_Call (Loc,
1575 Name =>
1576 New_Occurrence_Of (Enclosing_Func_Id, Loc),
1577 Parameter_Associations =>
1578 Actuals_List));
1579
1580 else
1581 Return_Node :=
1582 Make_Simple_Return_Statement (Loc,
1583 Expression =>
1584 New_Occurrence_Of (Standard_False, Loc));
1585 end if;
1586
1587 Set_Statements (Case_Alt_Node, New_List (Return_Node));
1588 Append (Case_Alt_Node, Alt_List);
1589 end if;
1590
1591 Case_Alt_Node := New_Node (N_Case_Statement_Alternative, Loc);
1592 Choice_List := New_List (New_Node (N_Others_Choice, Loc));
1593 Set_Discrete_Choices (Case_Alt_Node, Choice_List);
1594
1595 Return_Node :=
1596 Make_Simple_Return_Statement (Loc,
1597 Expression =>
1598 New_Occurrence_Of (Standard_True, Loc));
1599
1600 Set_Statements (Case_Alt_Node, New_List (Return_Node));
1601 Append (Case_Alt_Node, Alt_List);
1602
1603 Set_Alternatives (Case_Node, Alt_List);
1604 return Case_Node;
1605 end Build_Case_Statement;
1606
1607 ---------------------------
1608 -- Build_Dcheck_Function --
1609 ---------------------------
1610
1611 function Build_Dcheck_Function
1612 (Case_Id : Entity_Id;
1613 Variant : Node_Id) return Entity_Id
1614 is
1615 Body_Node : Node_Id;
1616 Func_Id : Entity_Id;
1617 Parameter_List : List_Id;
1618 Spec_Node : Node_Id;
1619
1620 begin
1621 Body_Node := New_Node (N_Subprogram_Body, Loc);
1622 Sequence := Sequence + 1;
1623
1624 Func_Id :=
1625 Make_Defining_Identifier (Loc,
1626 Chars => New_External_Name (Chars (Rec_Id), 'D', Sequence));
1627 Set_Is_Discriminant_Check_Function (Func_Id);
1628
1629 Spec_Node := New_Node (N_Function_Specification, Loc);
1630 Set_Defining_Unit_Name (Spec_Node, Func_Id);
1631
1632 Parameter_List := Build_Discriminant_Formals (Rec_Id, False);
1633
1634 Set_Parameter_Specifications (Spec_Node, Parameter_List);
1635 Set_Result_Definition (Spec_Node,
1636 New_Occurrence_Of (Standard_Boolean, Loc));
1637 Set_Specification (Body_Node, Spec_Node);
1638 Set_Declarations (Body_Node, New_List);
1639
1640 Set_Handled_Statement_Sequence (Body_Node,
1641 Make_Handled_Sequence_Of_Statements (Loc,
1642 Statements => New_List (
1643 Build_Case_Statement (Case_Id, Variant))));
1644
1645 Mutate_Ekind (Func_Id, E_Function);
1646 Set_Mechanism (Func_Id, Default_Mechanism);
1647 Set_Is_Inlined (Func_Id, True);
1648 Set_Is_Pure (Func_Id, True);
1649 Set_Is_Public (Func_Id, Is_Public (Rec_Id));
1650 Set_Is_Internal (Func_Id, True);
1651
1652 if not Debug_Generated_Code then
1653 Set_Debug_Info_Off (Func_Id);
1654 end if;
1655
1656 Analyze (Body_Node);
1657
1658 Append_Freeze_Action (Rec_Id, Body_Node);
1659 Set_Dcheck_Function (Variant, Func_Id);
1660 return Func_Id;
1661 end Build_Dcheck_Function;
1662
1663 ----------------------------
1664 -- Build_Dcheck_Functions --
1665 ----------------------------
1666
1667 procedure Build_Dcheck_Functions (Variant_Part_Node : Node_Id) is
1668 Component_List_Node : Node_Id;
1669 Decl : Entity_Id;
1670 Discr_Name : Entity_Id;
1671 Func_Id : Entity_Id;
1672 Variant : Node_Id;
1673 Saved_Enclosing_Func_Id : Entity_Id;
1674
1675 begin
1676 -- Build the discriminant-checking function for each variant, and
1677 -- label all components of that variant with the function's name.
1678 -- We only Generate a discriminant-checking function when the
1679 -- variant is not empty, to prevent the creation of dead code.
1680
1681 Discr_Name := Entity (Name (Variant_Part_Node));
1682 Variant := First_Non_Pragma (Variants (Variant_Part_Node));
1683
1684 while Present (Variant) loop
1685 Component_List_Node := Component_List (Variant);
1686
1687 if not Null_Present (Component_List_Node) then
1688 Func_Id := Build_Dcheck_Function (Discr_Name, Variant);
1689
1690 Decl :=
1691 First_Non_Pragma (Component_Items (Component_List_Node));
1692 while Present (Decl) loop
1693 Set_Discriminant_Checking_Func
1694 (Defining_Identifier (Decl), Func_Id);
1695 Next_Non_Pragma (Decl);
1696 end loop;
1697
1698 if Present (Variant_Part (Component_List_Node)) then
1699 Saved_Enclosing_Func_Id := Enclosing_Func_Id;
1700 Enclosing_Func_Id := Func_Id;
1701 Build_Dcheck_Functions (Variant_Part (Component_List_Node));
1702 Enclosing_Func_Id := Saved_Enclosing_Func_Id;
1703 end if;
1704 end if;
1705
1706 Next_Non_Pragma (Variant);
1707 end loop;
1708 end Build_Dcheck_Functions;
1709
1710 -- Start of processing for Build_Discr_Checking_Funcs
1711
1712 begin
1713 -- Only build if not done already
1714
1715 if not Discr_Check_Funcs_Built (N) then
1716 Type_Def := Type_Definition (N);
1717
1718 if Nkind (Type_Def) = N_Record_Definition then
1719 if No (Component_List (Type_Def)) then -- null record.
1720 return;
1721 else
1722 V := Variant_Part (Component_List (Type_Def));
1723 end if;
1724
1725 else pragma Assert (Nkind (Type_Def) = N_Derived_Type_Definition);
1726 if No (Component_List (Record_Extension_Part (Type_Def))) then
1727 return;
1728 else
1729 V := Variant_Part
1730 (Component_List (Record_Extension_Part (Type_Def)));
1731 end if;
1732 end if;
1733
1734 Rec_Id := Defining_Identifier (N);
1735
1736 if Present (V) and then not Is_Unchecked_Union (Rec_Id) then
1737 Loc := Sloc (N);
1738 Enclosing_Func_Id := Empty;
1739 Build_Dcheck_Functions (V);
1740 end if;
1741
1742 Set_Discr_Check_Funcs_Built (N);
1743 end if;
1744 end Build_Discr_Checking_Funcs;
1745
1746 ----------------------------------------
1747 -- Build_Or_Copy_Discr_Checking_Funcs --
1748 ----------------------------------------
1749
1750 procedure Build_Or_Copy_Discr_Checking_Funcs (N : Node_Id) is
1751 Typ : constant Entity_Id := Defining_Identifier (N);
1752 begin
1753 if Is_Unchecked_Union (Typ) or else not Has_Discriminants (Typ) then
1754 null;
1755 elsif not Is_Derived_Type (Typ)
1756 or else Has_New_Non_Standard_Rep (Typ)
1757 or else Is_Tagged_Type (Typ)
1758 then
1759 Build_Discr_Checking_Funcs (N);
1760 else
1761 Copy_Discr_Checking_Funcs (N);
1762 end if;
1763 end Build_Or_Copy_Discr_Checking_Funcs;
1764
1765 --------------------------------
1766 -- Build_Discriminant_Formals --
1767 --------------------------------
1768
1769 function Build_Discriminant_Formals
1770 (Rec_Id : Entity_Id;
1771 Use_Dl : Boolean) return List_Id
1772 is
1773 Loc : Source_Ptr := Sloc (Rec_Id);
1774 Parameter_List : constant List_Id := New_List;
1775 D : Entity_Id;
1776 Formal : Entity_Id;
1777 Formal_Type : Entity_Id;
1778 Param_Spec_Node : Node_Id;
1779
1780 begin
1781 if Has_Discriminants (Rec_Id) then
1782 D := First_Discriminant (Rec_Id);
1783 while Present (D) loop
1784 Loc := Sloc (D);
1785
1786 if Use_Dl then
1787 Formal := Discriminal (D);
1788 Formal_Type := Etype (Formal);
1789 else
1790 Formal := Make_Defining_Identifier (Loc, Chars (D));
1791 Formal_Type := Etype (D);
1792 end if;
1793
1794 Param_Spec_Node :=
1795 Make_Parameter_Specification (Loc,
1796 Defining_Identifier => Formal,
1797 Parameter_Type =>
1798 New_Occurrence_Of (Formal_Type, Loc));
1799 Append (Param_Spec_Node, Parameter_List);
1800 Next_Discriminant (D);
1801 end loop;
1802 end if;
1803
1804 return Parameter_List;
1805 end Build_Discriminant_Formals;
1806
1807 --------------------------------------
1808 -- Build_Equivalent_Array_Aggregate --
1809 --------------------------------------
1810
1811 function Build_Equivalent_Array_Aggregate (T : Entity_Id) return Node_Id is
1812 Loc : constant Source_Ptr := Sloc (T);
1813 Comp_Type : constant Entity_Id := Component_Type (T);
1814 Index_Type : constant Entity_Id := Etype (First_Index (T));
1815 Proc : constant Entity_Id := Base_Init_Proc (T);
1816 Lo, Hi : Node_Id;
1817 Aggr : Node_Id;
1818 Expr : Node_Id;
1819
1820 begin
1821 if not Is_Constrained (T)
1822 or else Number_Dimensions (T) > 1
1823 or else No (Proc)
1824 then
1825 Initialization_Warning (T);
1826 return Empty;
1827 end if;
1828
1829 Lo := Type_Low_Bound (Index_Type);
1830 Hi := Type_High_Bound (Index_Type);
1831
1832 if not Compile_Time_Known_Value (Lo)
1833 or else not Compile_Time_Known_Value (Hi)
1834 then
1835 Initialization_Warning (T);
1836 return Empty;
1837 end if;
1838
1839 if Is_Record_Type (Comp_Type)
1840 and then Present (Base_Init_Proc (Comp_Type))
1841 then
1842 Expr := Static_Initialization (Base_Init_Proc (Comp_Type));
1843
1844 if No (Expr) then
1845 Initialization_Warning (T);
1846 return Empty;
1847 end if;
1848
1849 else
1850 Initialization_Warning (T);
1851 return Empty;
1852 end if;
1853
1854 Aggr := Make_Aggregate (Loc, No_List, New_List);
1855 Set_Etype (Aggr, T);
1856 Set_Aggregate_Bounds (Aggr,
1857 Make_Range (Loc,
1858 Low_Bound => New_Copy (Lo),
1859 High_Bound => New_Copy (Hi)));
1860 Set_Parent (Aggr, Parent (Proc));
1861
1862 Append_To (Component_Associations (Aggr),
1863 Make_Component_Association (Loc,
1864 Choices =>
1865 New_List (
1866 Make_Range (Loc,
1867 Low_Bound => New_Copy (Lo),
1868 High_Bound => New_Copy (Hi))),
1869 Expression => Expr));
1870
1871 if Static_Array_Aggregate (Aggr) then
1872 return Aggr;
1873 else
1874 Initialization_Warning (T);
1875 return Empty;
1876 end if;
1877 end Build_Equivalent_Array_Aggregate;
1878
1879 ---------------------------------------
1880 -- Build_Equivalent_Record_Aggregate --
1881 ---------------------------------------
1882
1883 function Build_Equivalent_Record_Aggregate (T : Entity_Id) return Node_Id is
1884 Agg : Node_Id;
1885 Comp : Entity_Id;
1886 Comp_Type : Entity_Id;
1887
1888 begin
1889 if not Is_Record_Type (T)
1890 or else Has_Discriminants (T)
1891 or else Is_Limited_Type (T)
1892 or else Has_Non_Standard_Rep (T)
1893 or else Needs_Finalization (T)
1894 then
1895 Initialization_Warning (T);
1896 return Empty;
1897 end if;
1898
1899 Comp := First_Component (T);
1900
1901 -- A null record needs no warning
1902
1903 if No (Comp) then
1904 return Empty;
1905 end if;
1906
1907 while Present (Comp) loop
1908
1909 -- Array components are acceptable if initialized by a positional
1910 -- aggregate with static components.
1911
1912 if Is_Array_Type (Etype (Comp)) then
1913 Comp_Type := Component_Type (Etype (Comp));
1914
1915 if Nkind (Parent (Comp)) /= N_Component_Declaration
1916 or else No (Expression (Parent (Comp)))
1917 or else Nkind (Expression (Parent (Comp))) /= N_Aggregate
1918 then
1919 Initialization_Warning (T);
1920 return Empty;
1921
1922 elsif Is_Scalar_Type (Component_Type (Etype (Comp)))
1923 and then
1924 (not Compile_Time_Known_Value (Type_Low_Bound (Comp_Type))
1925 or else
1926 not Compile_Time_Known_Value (Type_High_Bound (Comp_Type)))
1927 then
1928 Initialization_Warning (T);
1929 return Empty;
1930
1931 elsif
1932 not Static_Array_Aggregate (Expression (Parent (Comp)))
1933 then
1934 Initialization_Warning (T);
1935 return Empty;
1936
1937 -- We need to return empty if the type has predicates because
1938 -- this would otherwise duplicate calls to the predicate
1939 -- function. If the type hasn't been frozen before being
1940 -- referenced in the current record, the extraneous call to
1941 -- the predicate function would be inserted somewhere before
1942 -- the predicate function is elaborated, which would result in
1943 -- an invalid tree.
1944
1945 elsif Has_Predicates (Etype (Comp)) then
1946 return Empty;
1947 end if;
1948
1949 elsif Is_Scalar_Type (Etype (Comp)) then
1950 Comp_Type := Etype (Comp);
1951
1952 if Nkind (Parent (Comp)) /= N_Component_Declaration
1953 or else No (Expression (Parent (Comp)))
1954 or else not Compile_Time_Known_Value (Expression (Parent (Comp)))
1955 or else not Compile_Time_Known_Value (Type_Low_Bound (Comp_Type))
1956 or else not
1957 Compile_Time_Known_Value (Type_High_Bound (Comp_Type))
1958 or else Has_Predicates (Etype (Comp))
1959 then
1960 Initialization_Warning (T);
1961 return Empty;
1962 end if;
1963
1964 -- For now, other types are excluded
1965
1966 else
1967 Initialization_Warning (T);
1968 return Empty;
1969 end if;
1970
1971 Next_Component (Comp);
1972 end loop;
1973
1974 -- All components have static initialization. Build positional aggregate
1975 -- from the given expressions or defaults.
1976
1977 Agg := Make_Aggregate (Sloc (T), New_List, New_List);
1978 Set_Parent (Agg, Parent (T));
1979
1980 Comp := First_Component (T);
1981 while Present (Comp) loop
1982 Append
1983 (New_Copy_Tree (Expression (Parent (Comp))), Expressions (Agg));
1984 Next_Component (Comp);
1985 end loop;
1986
1987 Analyze_And_Resolve (Agg, T);
1988 return Agg;
1989 end Build_Equivalent_Record_Aggregate;
1990
1991 -------------------------------
1992 -- Build_Initialization_Call --
1993 -------------------------------
1994
1995 -- References to a discriminant inside the record type declaration can
1996 -- appear either in the subtype_indication to constrain a record or an
1997 -- array, or as part of a larger expression given for the initial value
1998 -- of a component. In both of these cases N appears in the record
1999 -- initialization procedure and needs to be replaced by the formal
2000 -- parameter of the initialization procedure which corresponds to that
2001 -- discriminant.
2002
2003 -- In the example below, references to discriminants D1 and D2 in proc_1
2004 -- are replaced by references to formals with the same name
2005 -- (discriminals)
2006
2007 -- A similar replacement is done for calls to any record initialization
2008 -- procedure for any components that are themselves of a record type.
2009
2010 -- type R (D1, D2 : Integer) is record
2011 -- X : Integer := F * D1;
2012 -- Y : Integer := F * D2;
2013 -- end record;
2014
2015 -- procedure proc_1 (Out_2 : out R; D1 : Integer; D2 : Integer) is
2016 -- begin
2017 -- Out_2.D1 := D1;
2018 -- Out_2.D2 := D2;
2019 -- Out_2.X := F * D1;
2020 -- Out_2.Y := F * D2;
2021 -- end;
2022
2023 function Build_Initialization_Call
2024 (N : Node_Id;
2025 Id_Ref : Node_Id;
2026 Typ : Entity_Id;
2027 In_Init_Proc : Boolean := False;
2028 Enclos_Type : Entity_Id := Empty;
2029 Target_Ref : Node_Id := Empty;
2030 Discr_Map : Elist_Id := New_Elmt_List;
2031 With_Default_Init : Boolean := False;
2032 Constructor_Ref : Node_Id := Empty;
2033 Init_Control_Actual : Entity_Id := Empty) return List_Id
2034 is
2035 Loc : constant Source_Ptr := Sloc (N);
2036 Res : constant List_Id := New_List;
2037
2038 Full_Type : Entity_Id;
2039
2040 procedure Check_Predicated_Discriminant
2041 (Val : Node_Id;
2042 Discr : Entity_Id);
2043 -- Discriminants whose subtypes have predicates are checked in two
2044 -- cases:
2045 -- a) When an object is default-initialized and assertions are enabled
2046 -- we check that the value of the discriminant obeys the predicate.
2047
2048 -- b) In all cases, if the discriminant controls a variant and the
2049 -- variant has no others_choice, Constraint_Error must be raised if
2050 -- the predicate is violated, because there is no variant covered
2051 -- by the illegal discriminant value.
2052
2053 -----------------------------------
2054 -- Check_Predicated_Discriminant --
2055 -----------------------------------
2056
2057 procedure Check_Predicated_Discriminant
2058 (Val : Node_Id;
2059 Discr : Entity_Id)
2060 is
2061 Typ : constant Entity_Id := Etype (Discr);
2062
2063 procedure Check_Missing_Others (V : Node_Id);
2064 -- Check that a given variant and its nested variants have an others
2065 -- choice, and generate a constraint error raise when it does not.
2066
2067 --------------------------
2068 -- Check_Missing_Others --
2069 --------------------------
2070
2071 procedure Check_Missing_Others (V : Node_Id) is
2072 Alt : Node_Id;
2073 Choice : Node_Id;
2074 Last_Var : Node_Id;
2075
2076 begin
2077 Last_Var := Last_Non_Pragma (Variants (V));
2078 Choice := First (Discrete_Choices (Last_Var));
2079
2080 -- An others_choice is added during expansion for gcc use, but
2081 -- does not cover the illegality.
2082
2083 if Entity (Name (V)) = Discr then
2084 if Present (Choice)
2085 and then (Nkind (Choice) /= N_Others_Choice
2086 or else not Comes_From_Source (Choice))
2087 then
2088 Check_Expression_Against_Static_Predicate (Val, Typ);
2089
2090 if not Is_Static_Expression (Val) then
2091 Prepend_To (Res,
2092 Make_Raise_Constraint_Error (Loc,
2093 Condition =>
2094 Make_Op_Not (Loc,
2095 Right_Opnd => Make_Predicate_Call (Typ, Val)),
2096 Reason => CE_Invalid_Data));
2097 end if;
2098 end if;
2099 end if;
2100
2101 -- Check whether some nested variant is ruled by the predicated
2102 -- discriminant.
2103
2104 Alt := First (Variants (V));
2105 while Present (Alt) loop
2106 if Nkind (Alt) = N_Variant
2107 and then Present (Variant_Part (Component_List (Alt)))
2108 then
2109 Check_Missing_Others
2110 (Variant_Part (Component_List (Alt)));
2111 end if;
2112
2113 Next (Alt);
2114 end loop;
2115 end Check_Missing_Others;
2116
2117 -- Local variables
2118
2119 Def : Node_Id;
2120
2121 -- Start of processing for Check_Predicated_Discriminant
2122
2123 begin
2124 if Ekind (Base_Type (Full_Type)) = E_Record_Type then
2125 Def := Type_Definition (Parent (Base_Type (Full_Type)));
2126 else
2127 return;
2128 end if;
2129
2130 if Policy_In_Effect (Name_Assert) = Name_Check
2131 and then not Predicates_Ignored (Etype (Discr))
2132 then
2133 Prepend_To (Res, Make_Predicate_Check (Typ, Val));
2134 end if;
2135
2136 -- If discriminant controls a variant, verify that predicate is
2137 -- obeyed or else an Others_Choice is present.
2138
2139 if Nkind (Def) = N_Record_Definition
2140 and then Present (Variant_Part (Component_List (Def)))
2141 and then Policy_In_Effect (Name_Assert) = Name_Ignore
2142 then
2143 Check_Missing_Others (Variant_Part (Component_List (Def)));
2144 end if;
2145 end Check_Predicated_Discriminant;
2146
2147 -- Local variables
2148
2149 A_Type : Entity_Id;
2150 Arg : Node_Id;
2151 Args : List_Id;
2152 Decls : List_Id;
2153 Decl : Node_Id;
2154 Discr : Entity_Id;
2155 First_Arg : Node_Id;
2156 Full_Init_Type : Entity_Id;
2157 Init_Call : Node_Id;
2158 Init_Type : Entity_Id;
2159 Proc : Entity_Id;
2160
2161 -- Start of processing for Build_Initialization_Call
2162
2163 begin
2164 pragma Assert (Constructor_Ref = Empty
2165 or else Is_CPP_Constructor_Call (Constructor_Ref));
2166
2167 if No (Constructor_Ref) then
2168 Proc := Base_Init_Proc (Typ);
2169 else
2170 Proc := Base_Init_Proc (Typ, Entity (Name (Constructor_Ref)));
2171 end if;
2172
2173 pragma Assert (Present (Proc));
2174 Init_Type := Etype (First_Formal (Proc));
2175 Full_Init_Type := Underlying_Type (Init_Type);
2176
2177 -- Nothing to do if the Init_Proc is null, unless Initialize_Scalars
2178 -- is active (in which case we make the call anyway, since in the
2179 -- actual compiled client it may be non null).
2180
2181 if Is_Null_Init_Proc (Proc) and then not Init_Or_Norm_Scalars then
2182 return Empty_List;
2183
2184 -- Nothing to do for an array of controlled components that have only
2185 -- the inherited Initialize primitive. This is a useful optimization
2186 -- for CodePeer.
2187
2188 elsif Is_Trivial_Subprogram (Proc)
2189 and then Is_Array_Type (Full_Init_Type)
2190 then
2191 return New_List (Make_Null_Statement (Loc));
2192 end if;
2193
2194 -- Use the [underlying] full view when dealing with a private type. This
2195 -- may require several steps depending on derivations.
2196
2197 Full_Type := Typ;
2198 loop
2199 if Is_Private_Type (Full_Type) then
2200 if Present (Full_View (Full_Type)) then
2201 Full_Type := Full_View (Full_Type);
2202
2203 elsif Present (Underlying_Full_View (Full_Type)) then
2204 Full_Type := Underlying_Full_View (Full_Type);
2205
2206 -- When a private type acts as a generic actual and lacks a full
2207 -- view, use the base type.
2208
2209 elsif Is_Generic_Actual_Type (Full_Type) then
2210 Full_Type := Base_Type (Full_Type);
2211
2212 elsif Ekind (Full_Type) = E_Private_Subtype
2213 and then (not Has_Discriminants (Full_Type)
2214 or else No (Discriminant_Constraint (Full_Type)))
2215 then
2216 Full_Type := Etype (Full_Type);
2217
2218 -- The loop has recovered the [underlying] full view, stop the
2219 -- traversal.
2220
2221 else
2222 exit;
2223 end if;
2224
2225 -- The type is not private, nothing to do
2226
2227 else
2228 exit;
2229 end if;
2230 end loop;
2231
2232 -- If Typ is derived, the procedure is the initialization procedure for
2233 -- the root type. Wrap the argument in an conversion to make it type
2234 -- honest. Actually it isn't quite type honest, because there can be
2235 -- conflicts of views in the private type case. That is why we set
2236 -- Conversion_OK in the conversion node.
2237
2238 if (Is_Record_Type (Typ)
2239 or else Is_Array_Type (Typ)
2240 or else Is_Private_Type (Typ))
2241 and then Init_Type /= Base_Type (Typ)
2242 then
2243 First_Arg := OK_Convert_To (Etype (Init_Type), Id_Ref);
2244 Set_Etype (First_Arg, Init_Type);
2245
2246 else
2247 First_Arg := Id_Ref;
2248 end if;
2249
2250 Args := New_List (Convert_Concurrent (First_Arg, Typ));
2251
2252 -- In the tasks case, add _Master as the value of the _Master parameter
2253 -- and _Chain as the value of the _Chain parameter. At the outer level,
2254 -- these will be variables holding the corresponding values obtained
2255 -- from GNARL. At inner levels, they will be the parameters passed down
2256 -- through the outer routines.
2257
2258 if Has_Task (Full_Type) then
2259 if Restriction_Active (No_Task_Hierarchy) then
2260 Append_To (Args, Make_Integer_Literal (Loc, Library_Task_Level));
2261 elsif Present (Target_Ref) then
2262 Append_To (Args,
2263 New_Occurrence_Of
2264 (Master_Id (Base_Type (Root_Type (Etype (Target_Ref)))), Loc));
2265 else
2266 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
2267 end if;
2268
2269 -- Add _Chain (not done for sequential elaboration policy, see
2270 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
2271
2272 if Partition_Elaboration_Policy /= 'S' then
2273 Append_To (Args, Make_Identifier (Loc, Name_uChain));
2274 end if;
2275
2276 -- Ada 2005 (AI-287): In case of default initialized components
2277 -- with tasks, we generate a null string actual parameter.
2278 -- This is just a workaround that must be improved later???
2279
2280 if With_Default_Init then
2281 Append_To (Args, Make_String_Literal (Loc, Strval => ""));
2282
2283 else
2284 if Present (Enclos_Type) then
2285 A_Type := Enclos_Type;
2286
2287 elsif Present (Target_Ref)
2288 and then Nkind (Target_Ref) in N_Indexed_Component
2289 | N_Selected_Component
2290 then
2291 A_Type := Etype (Prefix (Target_Ref));
2292
2293 else
2294 A_Type := Full_Type;
2295 end if;
2296
2297 Decls :=
2298 Build_Task_Image_Decls (Loc,
2299 (if Present (Target_Ref) then Target_Ref else Id_Ref),
2300 A_Type,
2301 In_Init_Proc);
2302 Decl := Last (Decls);
2303
2304 Append_To (Args,
2305 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
2306 Append_List (Decls, Res);
2307 end if;
2308
2309 else
2310 Decls := No_List;
2311 Decl := Empty;
2312 end if;
2313
2314 -- Handle the optionally generated formal *_skip_null_excluding_checks
2315
2316 -- Look at the associated node for the object we are referencing and
2317 -- verify that we are expanding a call to an Init_Proc for an internally
2318 -- generated object declaration before passing True and skipping the
2319 -- relevant checks.
2320
2321 if Needs_Conditional_Null_Excluding_Check (Full_Init_Type)
2322 and then Nkind (Id_Ref) in N_Has_Entity
2323 and then (Comes_From_Source (Id_Ref)
2324 or else (Present (Associated_Node (Id_Ref))
2325 and then Comes_From_Source
2326 (Associated_Node (Id_Ref))))
2327 then
2328 Append_To (Args, New_Occurrence_Of (Standard_True, Loc));
2329 end if;
2330
2331 -- Add discriminant values if discriminants are present
2332
2333 if Has_Discriminants (Full_Init_Type) then
2334 -- If an allocated object will be constrained by the default
2335 -- values for discriminants, then build a subtype with those
2336 -- defaults, and change the allocated subtype to that. Note
2337 -- that this happens in fewer cases in Ada 2005 (AI95-0363).
2338
2339 if Nkind (N) = N_Allocator
2340 and then not Is_Constrained (Full_Type)
2341 and then
2342 Present
2343 (Discriminant_Default_Value (First_Discriminant (Full_Type)))
2344 and then (Ada_Version < Ada_2005
2345 or else not Object_Type_Has_Constrained_Partial_View
2346 (Full_Type, Current_Scope))
2347 then
2348 Full_Type := Build_Default_Subtype (Full_Type, N);
2349 Set_Expression (N, New_Occurrence_Of (Full_Type, Loc));
2350 end if;
2351
2352 Discr := First_Discriminant (Full_Init_Type);
2353 while Present (Discr) loop
2354
2355 -- If this is a discriminated concurrent type, the init_proc
2356 -- for the corresponding record is being called. Use that type
2357 -- directly to find the discriminant value, to handle properly
2358 -- intervening renamed discriminants.
2359
2360 declare
2361 T : Entity_Id := Full_Type;
2362
2363 begin
2364 if Is_Protected_Type (T) then
2365 T := Corresponding_Record_Type (T);
2366 end if;
2367
2368 Arg :=
2369 Get_Discriminant_Value (
2370 Discr,
2371 T,
2372 Discriminant_Constraint (Full_Type));
2373 end;
2374
2375 -- If the target has access discriminants, and is constrained by
2376 -- an access to the enclosing construct, i.e. a current instance,
2377 -- replace the reference to the type by a reference to the object.
2378
2379 if Nkind (Arg) = N_Attribute_Reference
2380 and then Is_Access_Type (Etype (Arg))
2381 and then Is_Entity_Name (Prefix (Arg))
2382 and then Is_Type (Entity (Prefix (Arg)))
2383 then
2384 Arg :=
2385 Make_Attribute_Reference (Loc,
2386 Prefix => New_Copy (Prefix (Id_Ref)),
2387 Attribute_Name => Name_Unrestricted_Access);
2388
2389 elsif In_Init_Proc then
2390
2391 -- Replace any possible references to the discriminant in the
2392 -- call to the record initialization procedure with references
2393 -- to the appropriate formal parameter.
2394
2395 if Nkind (Arg) = N_Identifier
2396 and then Ekind (Entity (Arg)) = E_Discriminant
2397 then
2398 Arg := New_Occurrence_Of (Discriminal (Entity (Arg)), Loc);
2399
2400 -- Otherwise make a copy of the default expression. Note that
2401 -- we use the current Sloc for this, because we do not want the
2402 -- call to appear to be at the declaration point. Within the
2403 -- expression, replace discriminants with their discriminals.
2404
2405 else
2406 Arg :=
2407 New_Copy_Tree (Arg, Map => Discr_Map, New_Sloc => Loc);
2408 end if;
2409
2410 else
2411 if Is_Constrained (Full_Type) then
2412 Arg := Duplicate_Subexpr_No_Checks (Arg);
2413 else
2414 -- The constraints come from the discriminant default exps,
2415 -- they must be reevaluated, so we use New_Copy_Tree but we
2416 -- ensure the proper Sloc (for any embedded calls).
2417 -- In addition, if a predicate check is needed on the value
2418 -- of the discriminant, insert it ahead of the call.
2419
2420 Arg := New_Copy_Tree (Arg, New_Sloc => Loc);
2421 end if;
2422
2423 if Has_Predicates (Etype (Discr)) then
2424 Check_Predicated_Discriminant (Arg, Discr);
2425 end if;
2426 end if;
2427
2428 -- Ada 2005 (AI-287): In case of default initialized components,
2429 -- if the component is constrained with a discriminant of the
2430 -- enclosing type, we need to generate the corresponding selected
2431 -- component node to access the discriminant value. In other cases
2432 -- this is not required, either because we are inside the init
2433 -- proc and we use the corresponding formal, or else because the
2434 -- component is constrained by an expression.
2435
2436 if With_Default_Init
2437 and then Nkind (Id_Ref) = N_Selected_Component
2438 and then Nkind (Arg) = N_Identifier
2439 and then Ekind (Entity (Arg)) = E_Discriminant
2440 then
2441 Append_To (Args,
2442 Make_Selected_Component (Loc,
2443 Prefix => New_Copy_Tree (Prefix (Id_Ref)),
2444 Selector_Name => Arg));
2445 else
2446 Append_To (Args, Arg);
2447 end if;
2448
2449 Next_Discriminant (Discr);
2450 end loop;
2451 end if;
2452
2453 -- If this is a call to initialize the parent component of a derived
2454 -- tagged type, indicate that the tag should not be set in the parent.
2455 -- This is done via the actual parameter value for the Init_Control
2456 -- formal parameter, which is also used to deal with late initialization
2457 -- requirements.
2458 --
2459 -- We pass in Full_Init_Except_Tag unless the caller tells us to do
2460 -- otherwise (by passing in a nonempty Init_Control_Actual parameter).
2461
2462 if Is_Tagged_Type (Full_Init_Type)
2463 and then not Is_CPP_Class (Full_Init_Type)
2464 and then Nkind (Id_Ref) = N_Selected_Component
2465 and then Chars (Selector_Name (Id_Ref)) = Name_uParent
2466 then
2467 declare
2468 use Initialization_Control;
2469 begin
2470 Append_To (Args,
2471 (if Present (Init_Control_Actual)
2472 then Init_Control_Actual
2473 else Make_Mode_Literal (Loc, Full_Init_Except_Tag)));
2474 end;
2475 elsif Present (Constructor_Ref) then
2476 Append_List_To (Args,
2477 New_Copy_List (Parameter_Associations (Constructor_Ref)));
2478 end if;
2479
2480 -- Pass the extra accessibility level parameter associated with the
2481 -- level of the object being initialized when required.
2482
2483 if Is_Entity_Name (Id_Ref)
2484 and then Present (Init_Proc_Level_Formal (Proc))
2485 then
2486 Append_To (Args,
2487 Make_Parameter_Association (Loc,
2488 Selector_Name =>
2489 Make_Identifier (Loc, Name_uInit_Level),
2490 Explicit_Actual_Parameter =>
2491 Accessibility_Level (Id_Ref, Dynamic_Level)));
2492 end if;
2493
2494 Append_To (Res,
2495 Make_Procedure_Call_Statement (Loc,
2496 Name => New_Occurrence_Of (Proc, Loc),
2497 Parameter_Associations => Args));
2498
2499 if Needs_Finalization (Typ)
2500 and then Nkind (Id_Ref) = N_Selected_Component
2501 then
2502 if Chars (Selector_Name (Id_Ref)) /= Name_uParent then
2503 Init_Call :=
2504 Make_Init_Call
2505 (Obj_Ref => New_Copy_Tree (First_Arg),
2506 Typ => Typ);
2507
2508 -- Guard against a missing [Deep_]Initialize when the type was not
2509 -- properly frozen.
2510
2511 if Present (Init_Call) then
2512 Append_To (Res, Init_Call);
2513 end if;
2514 end if;
2515 end if;
2516
2517 return Res;
2518
2519 exception
2520 when RE_Not_Available =>
2521 return Empty_List;
2522 end Build_Initialization_Call;
2523
2524 ----------------------------
2525 -- Build_Record_Init_Proc --
2526 ----------------------------
2527
2528 procedure Build_Record_Init_Proc (N : Node_Id; Rec_Ent : Entity_Id) is
2529 Decls : constant List_Id := New_List;
2530 Discr_Map : constant Elist_Id := New_Elmt_List;
2531 Loc : constant Source_Ptr := Sloc (Rec_Ent);
2532 Counter : Nat := 0;
2533 Proc_Id : Entity_Id;
2534 Rec_Type : Entity_Id;
2535
2536 Init_Control_Formal : Entity_Id := Empty; -- set in Build_Init_Statements
2537 Has_Late_Init_Comp : Boolean := False; -- set in Build_Init_Statements
2538
2539 function Build_Assignment
2540 (Id : Entity_Id;
2541 Default : Node_Id) return List_Id;
2542 -- Build an assignment statement that assigns the default expression to
2543 -- its corresponding record component if defined. The left-hand side of
2544 -- the assignment is marked Assignment_OK so that initialization of
2545 -- limited private records works correctly. This routine may also build
2546 -- an adjustment call if the component is controlled.
2547
2548 procedure Build_Discriminant_Assignments (Statement_List : List_Id);
2549 -- If the record has discriminants, add assignment statements to
2550 -- Statement_List to initialize the discriminant values from the
2551 -- arguments of the initialization procedure.
2552
2553 function Build_Init_Statements (Comp_List : Node_Id) return List_Id;
2554 -- Build a list representing a sequence of statements which initialize
2555 -- components of the given component list. This may involve building
2556 -- case statements for the variant parts. Append any locally declared
2557 -- objects on list Decls.
2558
2559 function Build_Init_Call_Thru (Parameters : List_Id) return List_Id;
2560 -- Given an untagged type-derivation that declares discriminants, e.g.
2561 --
2562 -- type R (R1, R2 : Integer) is record ... end record;
2563 -- type D (D1 : Integer) is new R (1, D1);
2564 --
2565 -- we make the _init_proc of D be
2566 --
2567 -- procedure _init_proc (X : D; D1 : Integer) is
2568 -- begin
2569 -- _init_proc (R (X), 1, D1);
2570 -- end _init_proc;
2571 --
2572 -- This function builds the call statement in this _init_proc.
2573
2574 procedure Build_CPP_Init_Procedure;
2575 -- Build the tree corresponding to the procedure specification and body
2576 -- of the IC procedure that initializes the C++ part of the dispatch
2577 -- table of an Ada tagged type that is a derivation of a CPP type.
2578 -- Install it as the CPP_Init TSS.
2579
2580 procedure Build_Init_Procedure;
2581 -- Build the tree corresponding to the procedure specification and body
2582 -- of the initialization procedure and install it as the _init TSS.
2583
2584 procedure Build_Offset_To_Top_Functions;
2585 -- Ada 2005 (AI-251): Build the tree corresponding to the procedure spec
2586 -- and body of Offset_To_Top, a function used in conjuction with types
2587 -- having secondary dispatch tables.
2588
2589 procedure Build_Record_Checks (S : Node_Id; Check_List : List_Id);
2590 -- Add range checks to components of discriminated records. S is a
2591 -- subtype indication of a record component. Check_List is a list
2592 -- to which the check actions are appended.
2593
2594 function Component_Needs_Simple_Initialization
2595 (T : Entity_Id) return Boolean;
2596 -- Determine if a component needs simple initialization, given its type
2597 -- T. This routine is the same as Needs_Simple_Initialization except for
2598 -- components of type Tag and Interface_Tag. These two access types do
2599 -- not require initialization since they are explicitly initialized by
2600 -- other means.
2601
2602 function Parent_Subtype_Renaming_Discrims return Boolean;
2603 -- Returns True for base types N that rename discriminants, else False
2604
2605 function Requires_Init_Proc (Rec_Id : Entity_Id) return Boolean;
2606 -- Determine whether a record initialization procedure needs to be
2607 -- generated for the given record type.
2608
2609 ----------------------
2610 -- Build_Assignment --
2611 ----------------------
2612
2613 function Build_Assignment
2614 (Id : Entity_Id;
2615 Default : Node_Id) return List_Id
2616 is
2617 Default_Loc : constant Source_Ptr := Sloc (Default);
2618 Typ : constant Entity_Id := Underlying_Type (Etype (Id));
2619
2620 Exp : Node_Id;
2621 Exp_Q : Node_Id;
2622 Lhs : Node_Id;
2623 Res : List_Id;
2624
2625 begin
2626 Lhs :=
2627 Make_Selected_Component (Default_Loc,
2628 Prefix => Make_Identifier (Loc, Name_uInit),
2629 Selector_Name => New_Occurrence_Of (Id, Default_Loc));
2630 Set_Assignment_OK (Lhs);
2631
2632 -- Take copy of Default to ensure that later copies of this component
2633 -- declaration in derived types see the original tree, not a node
2634 -- rewritten during expansion of the init_proc. If the copy contains
2635 -- itypes, the scope of the new itypes is the init_proc being built.
2636
2637 declare
2638 Map : Elist_Id := No_Elist;
2639
2640 begin
2641 if Has_Late_Init_Comp then
2642 -- Map the type to the _Init parameter in order to
2643 -- handle "current instance" references.
2644
2645 Map := New_Elmt_List
2646 (Elmt1 => Rec_Type,
2647 Elmt2 => Defining_Identifier (First
2648 (Parameter_Specifications
2649 (Parent (Proc_Id)))));
2650
2651 -- If the type has an incomplete view, a current instance
2652 -- may have an incomplete type. In that case, it must also be
2653 -- replaced by the formal of the Init_Proc.
2654
2655 if Nkind (Parent (Rec_Type)) = N_Full_Type_Declaration
2656 and then Present (Incomplete_View (Parent (Rec_Type)))
2657 then
2658 Append_Elmt (
2659 N => Incomplete_View (Parent (Rec_Type)),
2660 To => Map);
2661 Append_Elmt (
2662 N => Defining_Identifier
2663 (First
2664 (Parameter_Specifications
2665 (Parent (Proc_Id)))),
2666 To => Map);
2667 end if;
2668 end if;
2669
2670 Exp := New_Copy_Tree (Default, New_Scope => Proc_Id, Map => Map);
2671 end;
2672
2673 Res := New_List (
2674 Make_Assignment_Statement (Loc,
2675 Name => Lhs,
2676 Expression => Exp));
2677
2678 Exp_Q := Unqualify (Exp);
2679
2680 -- Adjust the component if controlled, except if it is an aggregate
2681 -- that will be expanded inline (but note that the case of container
2682 -- aggregates does require component adjustment), or a function call.
2683 -- Note that, when we don't inhibit component adjustment, the tag
2684 -- will be automatically inserted by Make_Tag_Ctrl_Assignment in the
2685 -- tagged case. Otherwise, we have to generate a tag assignment here.
2686
2687 if Needs_Finalization (Typ)
2688 and then (Nkind (Exp_Q) not in N_Aggregate | N_Extension_Aggregate
2689 or else Is_Container_Aggregate (Exp_Q))
2690 and then not Is_Build_In_Place_Function_Call (Exp)
2691 and then Nkind (Exp) /= N_Function_Call
2692 then
2693 Set_No_Finalize_Actions (First (Res));
2694
2695 else
2696 Set_No_Ctrl_Actions (First (Res));
2697
2698 -- Adjust the tag if tagged because of possible view conversions
2699
2700 if Is_Tagged_Type (Typ)
2701 and then Tagged_Type_Expansion
2702 and then Nkind (Exp_Q) /= N_Raise_Expression
2703 then
2704 declare
2705 Utyp : Entity_Id := Underlying_Type (Typ);
2706
2707 begin
2708 -- Get the relevant type for Make_Tag_Assignment_From_Type,
2709 -- which, for concurrent types is the corresponding record.
2710
2711 if Ekind (Utyp) in E_Protected_Type | E_Task_Type then
2712 Utyp := Corresponding_Record_Type (Utyp);
2713 end if;
2714
2715 Append_To (Res,
2716 Make_Tag_Assignment_From_Type (Default_Loc,
2717 New_Copy_Tree (Lhs, New_Scope => Proc_Id),
2718 Utyp));
2719 end;
2720 end if;
2721 end if;
2722
2723 return Res;
2724
2725 exception
2726 when RE_Not_Available =>
2727 return Empty_List;
2728 end Build_Assignment;
2729
2730 ------------------------------------
2731 -- Build_Discriminant_Assignments --
2732 ------------------------------------
2733
2734 procedure Build_Discriminant_Assignments (Statement_List : List_Id) is
2735 Is_Tagged : constant Boolean := Is_Tagged_Type (Rec_Type);
2736 D : Entity_Id;
2737 D_Loc : Source_Ptr;
2738
2739 begin
2740 if Has_Discriminants (Rec_Type)
2741 and then not Is_Unchecked_Union (Rec_Type)
2742 then
2743 D := First_Discriminant (Rec_Type);
2744 while Present (D) loop
2745
2746 -- Don't generate the assignment for discriminants in derived
2747 -- tagged types if the discriminant is a renaming of some
2748 -- ancestor discriminant. This initialization will be done
2749 -- when initializing the _parent field of the derived record.
2750
2751 if Is_Tagged
2752 and then Present (Corresponding_Discriminant (D))
2753 then
2754 null;
2755
2756 else
2757 D_Loc := Sloc (D);
2758 Append_List_To (Statement_List,
2759 Build_Assignment (D,
2760 New_Occurrence_Of (Discriminal (D), D_Loc)));
2761 end if;
2762
2763 Next_Discriminant (D);
2764 end loop;
2765 end if;
2766 end Build_Discriminant_Assignments;
2767
2768 --------------------------
2769 -- Build_Init_Call_Thru --
2770 --------------------------
2771
2772 function Build_Init_Call_Thru (Parameters : List_Id) return List_Id is
2773 Parent_Proc : constant Entity_Id :=
2774 Base_Init_Proc (Etype (Rec_Type));
2775
2776 Parent_Type : constant Entity_Id :=
2777 Etype (First_Formal (Parent_Proc));
2778
2779 Uparent_Type : constant Entity_Id :=
2780 Underlying_Type (Parent_Type);
2781
2782 First_Discr_Param : Node_Id;
2783
2784 Arg : Node_Id;
2785 Args : List_Id;
2786 First_Arg : Node_Id;
2787 Parent_Discr : Entity_Id;
2788 Res : List_Id;
2789
2790 begin
2791 -- First argument (_Init) is the object to be initialized.
2792 -- ??? not sure where to get a reasonable Loc for First_Arg
2793
2794 First_Arg :=
2795 OK_Convert_To (Parent_Type,
2796 New_Occurrence_Of
2797 (Defining_Identifier (First (Parameters)), Loc));
2798
2799 Set_Etype (First_Arg, Parent_Type);
2800
2801 Args := New_List (Convert_Concurrent (First_Arg, Rec_Type));
2802
2803 -- In the tasks case,
2804 -- add _Master as the value of the _Master parameter
2805 -- add _Chain as the value of the _Chain parameter.
2806 -- add _Task_Name as the value of the _Task_Name parameter.
2807 -- At the outer level, these will be variables holding the
2808 -- corresponding values obtained from GNARL or the expander.
2809 --
2810 -- At inner levels, they will be the parameters passed down through
2811 -- the outer routines.
2812
2813 First_Discr_Param := Next (First (Parameters));
2814
2815 if Has_Task (Rec_Type) then
2816 if Restriction_Active (No_Task_Hierarchy) then
2817 Append_To
2818 (Args, Make_Integer_Literal (Loc, Library_Task_Level));
2819 else
2820 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
2821 end if;
2822
2823 -- Add _Chain (not done for sequential elaboration policy, see
2824 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
2825
2826 if Partition_Elaboration_Policy /= 'S' then
2827 Append_To (Args, Make_Identifier (Loc, Name_uChain));
2828 end if;
2829
2830 Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
2831 First_Discr_Param := Next (Next (Next (First_Discr_Param)));
2832 end if;
2833
2834 -- Append discriminant values
2835
2836 if Has_Discriminants (Uparent_Type) then
2837 pragma Assert (not Is_Tagged_Type (Uparent_Type));
2838
2839 Parent_Discr := First_Discriminant (Uparent_Type);
2840 while Present (Parent_Discr) loop
2841
2842 -- Get the initial value for this discriminant
2843 -- ??? needs to be cleaned up to use parent_Discr_Constr
2844 -- directly.
2845
2846 declare
2847 Discr : Entity_Id :=
2848 First_Stored_Discriminant (Uparent_Type);
2849
2850 Discr_Value : Elmt_Id :=
2851 First_Elmt (Stored_Constraint (Rec_Type));
2852
2853 begin
2854 while Original_Record_Component (Parent_Discr) /= Discr loop
2855 Next_Stored_Discriminant (Discr);
2856 Next_Elmt (Discr_Value);
2857 end loop;
2858
2859 Arg := Node (Discr_Value);
2860 end;
2861
2862 -- Append it to the list
2863
2864 if Nkind (Arg) = N_Identifier
2865 and then Ekind (Entity (Arg)) = E_Discriminant
2866 then
2867 Append_To (Args,
2868 New_Occurrence_Of (Discriminal (Entity (Arg)), Loc));
2869
2870 -- Case of access discriminants. We replace the reference
2871 -- to the type by a reference to the actual object.
2872
2873 -- Is above comment right??? Use of New_Copy below seems mighty
2874 -- suspicious ???
2875
2876 else
2877 Append_To (Args, New_Copy (Arg));
2878 end if;
2879
2880 Next_Discriminant (Parent_Discr);
2881 end loop;
2882 end if;
2883
2884 Res :=
2885 New_List (
2886 Make_Procedure_Call_Statement (Loc,
2887 Name =>
2888 New_Occurrence_Of (Parent_Proc, Loc),
2889 Parameter_Associations => Args));
2890
2891 return Res;
2892 end Build_Init_Call_Thru;
2893
2894 -----------------------------------
2895 -- Build_Offset_To_Top_Functions --
2896 -----------------------------------
2897
2898 procedure Build_Offset_To_Top_Functions is
2899
2900 procedure Build_Offset_To_Top_Function (Iface_Comp : Entity_Id);
2901 -- Generate:
2902 -- function Fxx (O : Address) return Storage_Offset is
2903 -- type Acc is access all <Typ>;
2904 -- begin
2905 -- return Acc!(O).Iface_Comp'Position;
2906 -- end Fxx;
2907
2908 ----------------------------------
2909 -- Build_Offset_To_Top_Function --
2910 ----------------------------------
2911
2912 procedure Build_Offset_To_Top_Function (Iface_Comp : Entity_Id) is
2913 Body_Node : Node_Id;
2914 Func_Id : Entity_Id;
2915 Spec_Node : Node_Id;
2916 Acc_Type : Entity_Id;
2917
2918 begin
2919 Func_Id := Make_Temporary (Loc, 'F');
2920 Set_DT_Offset_To_Top_Func (Iface_Comp, Func_Id);
2921
2922 -- Generate
2923 -- function Fxx (O : in Rec_Typ) return Storage_Offset;
2924
2925 Spec_Node := New_Node (N_Function_Specification, Loc);
2926 Set_Defining_Unit_Name (Spec_Node, Func_Id);
2927 Set_Parameter_Specifications (Spec_Node, New_List (
2928 Make_Parameter_Specification (Loc,
2929 Defining_Identifier =>
2930 Make_Defining_Identifier (Loc, Name_uO),
2931 In_Present => True,
2932 Parameter_Type =>
2933 New_Occurrence_Of (RTE (RE_Address), Loc))));
2934 Set_Result_Definition (Spec_Node,
2935 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
2936
2937 -- Generate
2938 -- function Fxx (O : in Rec_Typ) return Storage_Offset is
2939 -- begin
2940 -- return -O.Iface_Comp'Position;
2941 -- end Fxx;
2942
2943 Body_Node := New_Node (N_Subprogram_Body, Loc);
2944 Set_Specification (Body_Node, Spec_Node);
2945
2946 Acc_Type := Make_Temporary (Loc, 'T');
2947 Set_Declarations (Body_Node, New_List (
2948 Make_Full_Type_Declaration (Loc,
2949 Defining_Identifier => Acc_Type,
2950 Type_Definition =>
2951 Make_Access_To_Object_Definition (Loc,
2952 All_Present => True,
2953 Subtype_Indication =>
2954 New_Occurrence_Of (Rec_Type, Loc)))));
2955
2956 Set_Handled_Statement_Sequence (Body_Node,
2957 Make_Handled_Sequence_Of_Statements (Loc,
2958 Statements => New_List (
2959 Make_Simple_Return_Statement (Loc,
2960 Expression =>
2961 Make_Op_Minus (Loc,
2962 Make_Attribute_Reference (Loc,
2963 Prefix =>
2964 Make_Selected_Component (Loc,
2965 Prefix =>
2966 Make_Explicit_Dereference (Loc,
2967 Unchecked_Convert_To (Acc_Type,
2968 Make_Identifier (Loc, Name_uO))),
2969 Selector_Name =>
2970 New_Occurrence_Of (Iface_Comp, Loc)),
2971 Attribute_Name => Name_Position))))));
2972
2973 Mutate_Ekind (Func_Id, E_Function);
2974 Set_Mechanism (Func_Id, Default_Mechanism);
2975 Set_Is_Internal (Func_Id, True);
2976
2977 if not Debug_Generated_Code then
2978 Set_Debug_Info_Off (Func_Id);
2979 end if;
2980
2981 Analyze (Body_Node);
2982
2983 Append_Freeze_Action (Rec_Type, Body_Node);
2984 end Build_Offset_To_Top_Function;
2985
2986 -- Local variables
2987
2988 Iface_Comp : Node_Id;
2989 Iface_Comp_Elmt : Elmt_Id;
2990 Ifaces_Comp_List : Elist_Id;
2991
2992 -- Start of processing for Build_Offset_To_Top_Functions
2993
2994 begin
2995 -- Offset_To_Top_Functions are built only for derivations of types
2996 -- with discriminants that cover interface types.
2997 -- Nothing is needed either in case of virtual targets, since
2998 -- interfaces are handled directly by the target.
2999
3000 if not Is_Tagged_Type (Rec_Type)
3001 or else Etype (Rec_Type) = Rec_Type
3002 or else not Has_Discriminants (Etype (Rec_Type))
3003 or else not Tagged_Type_Expansion
3004 then
3005 return;
3006 end if;
3007
3008 Collect_Interface_Components (Rec_Type, Ifaces_Comp_List);
3009
3010 -- For each interface type with secondary dispatch table we generate
3011 -- the Offset_To_Top_Functions (required to displace the pointer in
3012 -- interface conversions)
3013
3014 Iface_Comp_Elmt := First_Elmt (Ifaces_Comp_List);
3015 while Present (Iface_Comp_Elmt) loop
3016 Iface_Comp := Node (Iface_Comp_Elmt);
3017 pragma Assert (Is_Interface (Related_Type (Iface_Comp)));
3018
3019 -- If the interface is a parent of Rec_Type it shares the primary
3020 -- dispatch table and hence there is no need to build the function
3021
3022 if not Is_Ancestor (Related_Type (Iface_Comp), Rec_Type,
3023 Use_Full_View => True)
3024 then
3025 Build_Offset_To_Top_Function (Iface_Comp);
3026 end if;
3027
3028 Next_Elmt (Iface_Comp_Elmt);
3029 end loop;
3030 end Build_Offset_To_Top_Functions;
3031
3032 ------------------------------
3033 -- Build_CPP_Init_Procedure --
3034 ------------------------------
3035
3036 procedure Build_CPP_Init_Procedure is
3037 Body_Node : Node_Id;
3038 Body_Stmts : List_Id;
3039 Flag_Id : Entity_Id;
3040 Handled_Stmt_Node : Node_Id;
3041 Init_Tags_List : List_Id;
3042 Proc_Id : Entity_Id;
3043 Proc_Spec_Node : Node_Id;
3044
3045 begin
3046 -- Check cases requiring no IC routine
3047
3048 if not Is_CPP_Class (Root_Type (Rec_Type))
3049 or else Is_CPP_Class (Rec_Type)
3050 or else CPP_Num_Prims (Rec_Type) = 0
3051 or else not Tagged_Type_Expansion
3052 or else No_Run_Time_Mode
3053 then
3054 return;
3055 end if;
3056
3057 -- Generate:
3058
3059 -- Flag : Boolean := False;
3060 --
3061 -- procedure Typ_IC is
3062 -- begin
3063 -- if not Flag then
3064 -- Copy C++ dispatch table slots from parent
3065 -- Update C++ slots of overridden primitives
3066 -- end if;
3067 -- end;
3068
3069 Flag_Id := Make_Temporary (Loc, 'F');
3070
3071 Append_Freeze_Action (Rec_Type,
3072 Make_Object_Declaration (Loc,
3073 Defining_Identifier => Flag_Id,
3074 Object_Definition =>
3075 New_Occurrence_Of (Standard_Boolean, Loc),
3076 Expression =>
3077 New_Occurrence_Of (Standard_True, Loc)));
3078
3079 Body_Stmts := New_List;
3080 Body_Node := New_Node (N_Subprogram_Body, Loc);
3081
3082 Proc_Spec_Node := New_Node (N_Procedure_Specification, Loc);
3083
3084 Proc_Id :=
3085 Make_Defining_Identifier (Loc,
3086 Chars => Make_TSS_Name (Rec_Type, TSS_CPP_Init_Proc));
3087
3088 Mutate_Ekind (Proc_Id, E_Procedure);
3089 Set_Is_Internal (Proc_Id);
3090
3091 Set_Defining_Unit_Name (Proc_Spec_Node, Proc_Id);
3092
3093 Set_Parameter_Specifications (Proc_Spec_Node, New_List);
3094 Set_Specification (Body_Node, Proc_Spec_Node);
3095 Set_Declarations (Body_Node, New_List);
3096
3097 Init_Tags_List := Build_Inherit_CPP_Prims (Rec_Type);
3098
3099 Append_To (Init_Tags_List,
3100 Make_Assignment_Statement (Loc,
3101 Name =>
3102 New_Occurrence_Of (Flag_Id, Loc),
3103 Expression =>
3104 New_Occurrence_Of (Standard_False, Loc)));
3105
3106 Append_To (Body_Stmts,
3107 Make_If_Statement (Loc,
3108 Condition => New_Occurrence_Of (Flag_Id, Loc),
3109 Then_Statements => Init_Tags_List));
3110
3111 Handled_Stmt_Node :=
3112 New_Node (N_Handled_Sequence_Of_Statements, Loc);
3113 Set_Statements (Handled_Stmt_Node, Body_Stmts);
3114 Set_Exception_Handlers (Handled_Stmt_Node, No_List);
3115 Set_Handled_Statement_Sequence (Body_Node, Handled_Stmt_Node);
3116
3117 if not Debug_Generated_Code then
3118 Set_Debug_Info_Off (Proc_Id);
3119 end if;
3120
3121 -- Associate CPP_Init_Proc with type
3122
3123 Set_Init_Proc (Rec_Type, Proc_Id);
3124 end Build_CPP_Init_Procedure;
3125
3126 --------------------------
3127 -- Build_Init_Procedure --
3128 --------------------------
3129
3130 procedure Build_Init_Procedure is
3131 Body_Stmts : List_Id;
3132 Body_Node : Node_Id;
3133 Handled_Stmt_Node : Node_Id;
3134 Init_Tags_List : List_Id;
3135 Parameters : List_Id;
3136 Proc_Spec_Node : Node_Id;
3137 Record_Extension_Node : Node_Id;
3138
3139 use Initialization_Control;
3140 begin
3141 Body_Stmts := New_List;
3142 Body_Node := New_Node (N_Subprogram_Body, Loc);
3143 Mutate_Ekind (Proc_Id, E_Procedure);
3144
3145 Proc_Spec_Node := New_Node (N_Procedure_Specification, Loc);
3146 Set_Defining_Unit_Name (Proc_Spec_Node, Proc_Id);
3147
3148 Parameters := Init_Formals (Rec_Type, Proc_Id);
3149 Append_List_To (Parameters,
3150 Build_Discriminant_Formals (Rec_Type, True));
3151
3152 -- For tagged types, we add a parameter to indicate what
3153 -- portion of the object's initialization is to be performed.
3154 -- This is used for two purposes:
3155 -- 1) When a type extension's initialization procedure calls
3156 -- the initialization procedure of the parent type, we do
3157 -- not want the parent to initialize the Tag component;
3158 -- it has been set already.
3159 -- 2) If an ancestor type has at least one component that requires
3160 -- late initialization, then we need to be able to initialize
3161 -- those components separately after initializing any other
3162 -- components.
3163
3164 if Is_Tagged_Type (Rec_Type) then
3165 Init_Control_Formal := Make_Temporary (Loc, 'P');
3166
3167 Append_To (Parameters,
3168 Make_Parameter_Specification (Loc,
3169 Defining_Identifier => Init_Control_Formal,
3170 Parameter_Type =>
3171 New_Occurrence_Of (Standard_Natural, Loc),
3172 Expression => Make_Mode_Literal (Loc, Full_Init)));
3173 end if;
3174
3175 -- Create an extra accessibility parameter to capture the level of
3176 -- the object being initialized when its type is a limited record.
3177
3178 if Is_Limited_Record (Rec_Type) then
3179 Append_To (Parameters,
3180 Make_Parameter_Specification (Loc,
3181 Defining_Identifier => Make_Defining_Identifier
3182 (Loc, Name_uInit_Level),
3183 Parameter_Type =>
3184 New_Occurrence_Of (Standard_Natural, Loc),
3185 Expression =>
3186 Make_Integer_Literal
3187 (Loc, Scope_Depth (Standard_Standard))));
3188 end if;
3189
3190 Set_Parameter_Specifications (Proc_Spec_Node, Parameters);
3191 Set_Specification (Body_Node, Proc_Spec_Node);
3192 Set_Declarations (Body_Node, Decls);
3193
3194 -- N is a Derived_Type_Definition that renames the parameters of the
3195 -- ancestor type. We initialize it by expanding our discriminants and
3196 -- call the ancestor _init_proc with a type-converted object.
3197
3198 if Parent_Subtype_Renaming_Discrims then
3199 Append_List_To (Body_Stmts, Build_Init_Call_Thru (Parameters));
3200
3201 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
3202 Build_Discriminant_Assignments (Body_Stmts);
3203
3204 if not Null_Present (Type_Definition (N)) then
3205 Append_List_To (Body_Stmts,
3206 Build_Init_Statements (Component_List (Type_Definition (N))));
3207 end if;
3208
3209 -- N is a Derived_Type_Definition with a possible non-empty
3210 -- extension. The initialization of a type extension consists in the
3211 -- initialization of the components in the extension.
3212
3213 else
3214 Build_Discriminant_Assignments (Body_Stmts);
3215
3216 Record_Extension_Node :=
3217 Record_Extension_Part (Type_Definition (N));
3218
3219 if not Null_Present (Record_Extension_Node) then
3220 declare
3221 Stmts : constant List_Id :=
3222 Build_Init_Statements (
3223 Component_List (Record_Extension_Node));
3224
3225 begin
3226 -- The parent field must be initialized first because the
3227 -- offset of the new discriminants may depend on it. This is
3228 -- not needed if the parent is an interface type because in
3229 -- such case the initialization of the _parent field was not
3230 -- generated.
3231
3232 if not Is_Interface (Etype (Rec_Ent)) then
3233 declare
3234 Parent_IP : constant Name_Id :=
3235 Make_Init_Proc_Name (Etype (Rec_Ent));
3236 Stmt : Node_Id := First (Stmts);
3237 IP_Call : Node_Id := Empty;
3238 begin
3239 -- Look for a call to the parent IP associated with
3240 -- the record extension.
3241 -- The call will be inside not one but two
3242 -- if-statements (with the same condition). Testing
3243 -- the same Early_Init condition twice might seem
3244 -- redundant. However, as soon as we exit this loop,
3245 -- we are going to hoist the inner if-statement out
3246 -- of the outer one; the "redundant" test was built
3247 -- in anticipation of this hoisting.
3248
3249 while Present (Stmt) loop
3250 if Nkind (Stmt) = N_If_Statement then
3251 declare
3252 Then_Stmt1 : Node_Id :=
3253 First (Then_Statements (Stmt));
3254 Then_Stmt2 : Node_Id;
3255 begin
3256 while Present (Then_Stmt1) loop
3257 if Nkind (Then_Stmt1) = N_If_Statement then
3258 Then_Stmt2 :=
3259 First (Then_Statements (Then_Stmt1));
3260
3261 if Nkind (Then_Stmt2) =
3262 N_Procedure_Call_Statement
3263 and then Chars (Name (Then_Stmt2)) =
3264 Parent_IP
3265 then
3266 -- IP_Call is a call wrapped in an
3267 -- if statement.
3268 IP_Call := Then_Stmt1;
3269 exit;
3270 end if;
3271 end if;
3272 Next (Then_Stmt1);
3273 end loop;
3274 end;
3275 end if;
3276
3277 Next (Stmt);
3278 end loop;
3279
3280 -- If found then move it to the beginning of the
3281 -- statements of this IP routine
3282
3283 if Present (IP_Call) then
3284 Remove (IP_Call);
3285 Prepend_List_To (Body_Stmts, New_List (IP_Call));
3286 end if;
3287 end;
3288 end if;
3289
3290 Append_List_To (Body_Stmts, Stmts);
3291 end;
3292 end if;
3293 end if;
3294
3295 -- Add here the assignment to instantiate the Tag
3296
3297 -- The assignment corresponds to the code:
3298
3299 -- _Init._Tag := Typ'Tag;
3300
3301 -- Suppress the tag assignment when not Tagged_Type_Expansion because
3302 -- tags are represented implicitly in objects. It is also suppressed
3303 -- in case of CPP_Class types because in this case the tag is
3304 -- initialized in the C++ side.
3305
3306 if Is_Tagged_Type (Rec_Type)
3307 and then Tagged_Type_Expansion
3308 and then not No_Run_Time_Mode
3309 then
3310 -- Case 1: Ada tagged types with no CPP ancestor. Set the tags of
3311 -- the actual object and invoke the IP of the parent (in this
3312 -- order). The tag must be initialized before the call to the IP
3313 -- of the parent and the assignments to other components because
3314 -- the initial value of the components may depend on the tag (eg.
3315 -- through a dispatching operation on an access to the current
3316 -- type). The tag assignment is not done when initializing the
3317 -- parent component of a type extension, because in that case the
3318 -- tag is set in the extension.
3319
3320 if not Is_CPP_Class (Root_Type (Rec_Type)) then
3321
3322 -- Initialize the primary tag component
3323
3324 Init_Tags_List := New_List (
3325 Make_Tag_Assignment_From_Type
3326 (Loc, Make_Identifier (Loc, Name_uInit), Rec_Type));
3327
3328 -- Ada 2005 (AI-251): Initialize the secondary tags components
3329 -- located at fixed positions (tags whose position depends on
3330 -- variable size components are initialized later ---see below)
3331
3332 if Ada_Version >= Ada_2005
3333 and then not Is_Interface (Rec_Type)
3334 and then Has_Interfaces (Rec_Type)
3335 then
3336 declare
3337 Elab_Sec_DT_Stmts_List : constant List_Id := New_List;
3338 Elab_List : List_Id := New_List;
3339
3340 begin
3341 Init_Secondary_Tags
3342 (Typ => Rec_Type,
3343 Target => Make_Identifier (Loc, Name_uInit),
3344 Init_Tags_List => Init_Tags_List,
3345 Stmts_List => Elab_Sec_DT_Stmts_List,
3346 Fixed_Comps => True,
3347 Variable_Comps => False);
3348
3349 Elab_List := New_List (
3350 Make_If_Statement (Loc,
3351 Condition =>
3352 Tag_Init_Condition (Loc, Init_Control_Formal),
3353 Then_Statements => Init_Tags_List));
3354
3355 if Elab_Flag_Needed (Rec_Type) then
3356 Append_To (Elab_Sec_DT_Stmts_List,
3357 Make_Assignment_Statement (Loc,
3358 Name =>
3359 New_Occurrence_Of
3360 (Access_Disp_Table_Elab_Flag (Rec_Type),
3361 Loc),
3362 Expression =>
3363 New_Occurrence_Of (Standard_False, Loc)));
3364
3365 Append_To (Elab_List,
3366 Make_If_Statement (Loc,
3367 Condition =>
3368 New_Occurrence_Of
3369 (Access_Disp_Table_Elab_Flag (Rec_Type), Loc),
3370 Then_Statements => Elab_Sec_DT_Stmts_List));
3371 end if;
3372
3373 Prepend_List_To (Body_Stmts, Elab_List);
3374 end;
3375 else
3376 Prepend_To (Body_Stmts,
3377 Make_If_Statement (Loc,
3378 Condition =>
3379 Tag_Init_Condition (Loc, Init_Control_Formal),
3380 Then_Statements => Init_Tags_List));
3381 end if;
3382
3383 -- Case 2: CPP type. The imported C++ constructor takes care of
3384 -- tags initialization. No action needed here because the IP
3385 -- is built by Set_CPP_Constructors; in this case the IP is a
3386 -- wrapper that invokes the C++ constructor and copies the C++
3387 -- tags locally. Done to inherit the C++ slots in Ada derivations
3388 -- (see case 3).
3389
3390 elsif Is_CPP_Class (Rec_Type) then
3391 pragma Assert (False);
3392 null;
3393
3394 -- Case 3: Combined hierarchy containing C++ types and Ada tagged
3395 -- type derivations. Derivations of imported C++ classes add a
3396 -- complication, because we cannot inhibit tag setting in the
3397 -- constructor for the parent. Hence we initialize the tag after
3398 -- the call to the parent IP (that is, in reverse order compared
3399 -- with pure Ada hierarchies ---see comment on case 1).
3400
3401 else
3402 -- Initialize the primary tag
3403
3404 Init_Tags_List := New_List (
3405 Make_Tag_Assignment_From_Type
3406 (Loc, Make_Identifier (Loc, Name_uInit), Rec_Type));
3407
3408 -- Ada 2005 (AI-251): Initialize the secondary tags components
3409 -- located at fixed positions (tags whose position depends on
3410 -- variable size components are initialized later ---see below)
3411
3412 if Ada_Version >= Ada_2005
3413 and then not Is_Interface (Rec_Type)
3414 and then Has_Interfaces (Rec_Type)
3415 then
3416 Init_Secondary_Tags
3417 (Typ => Rec_Type,
3418 Target => Make_Identifier (Loc, Name_uInit),
3419 Init_Tags_List => Init_Tags_List,
3420 Stmts_List => Init_Tags_List,
3421 Fixed_Comps => True,
3422 Variable_Comps => False);
3423 end if;
3424
3425 -- Initialize the tag component after invocation of parent IP.
3426
3427 -- Generate:
3428 -- parent_IP(_init.parent); // Invokes the C++ constructor
3429 -- [ typIC; ] // Inherit C++ slots from parent
3430 -- init_tags
3431
3432 declare
3433 Ins_Nod : Node_Id;
3434
3435 begin
3436 -- Search for the call to the IP of the parent. We assume
3437 -- that the first init_proc call is for the parent.
3438 -- It is wrapped in an "if Early_Init_Condition"
3439 -- if-statement.
3440
3441 Ins_Nod := First (Body_Stmts);
3442 while Present (Next (Ins_Nod))
3443 and then
3444 (Nkind (Ins_Nod) /= N_If_Statement
3445 or else Nkind (First (Then_Statements (Ins_Nod)))
3446 /= N_Procedure_Call_Statement
3447 or else not Is_Init_Proc
3448 (Name (First (Then_Statements
3449 (Ins_Nod)))))
3450 loop
3451 Next (Ins_Nod);
3452 end loop;
3453
3454 -- The IC routine copies the inherited slots of the C+ part
3455 -- of the dispatch table from the parent and updates the
3456 -- overridden C++ slots.
3457
3458 if CPP_Num_Prims (Rec_Type) > 0 then
3459 declare
3460 Init_DT : Entity_Id;
3461 New_Nod : Node_Id;
3462
3463 begin
3464 Init_DT := CPP_Init_Proc (Rec_Type);
3465 pragma Assert (Present (Init_DT));
3466
3467 New_Nod :=
3468 Make_Procedure_Call_Statement (Loc,
3469 New_Occurrence_Of (Init_DT, Loc));
3470 Insert_After (Ins_Nod, New_Nod);
3471
3472 -- Update location of init tag statements
3473
3474 Ins_Nod := New_Nod;
3475 end;
3476 end if;
3477
3478 Insert_List_After (Ins_Nod, Init_Tags_List);
3479 end;
3480 end if;
3481
3482 -- Ada 2005 (AI-251): Initialize the secondary tag components
3483 -- located at variable positions. We delay the generation of this
3484 -- code until here because the value of the attribute 'Position
3485 -- applied to variable size components of the parent type that
3486 -- depend on discriminants is only safely read at runtime after
3487 -- the parent components have been initialized.
3488
3489 if Ada_Version >= Ada_2005
3490 and then not Is_Interface (Rec_Type)
3491 and then Has_Interfaces (Rec_Type)
3492 and then Has_Discriminants (Etype (Rec_Type))
3493 and then Is_Variable_Size_Record (Etype (Rec_Type))
3494 then
3495 Init_Tags_List := New_List;
3496
3497 Init_Secondary_Tags
3498 (Typ => Rec_Type,
3499 Target => Make_Identifier (Loc, Name_uInit),
3500 Init_Tags_List => Init_Tags_List,
3501 Stmts_List => Init_Tags_List,
3502 Fixed_Comps => False,
3503 Variable_Comps => True);
3504
3505 Append_List_To (Body_Stmts, Init_Tags_List);
3506 end if;
3507 end if;
3508
3509 Handled_Stmt_Node := New_Node (N_Handled_Sequence_Of_Statements, Loc);
3510 Set_Statements (Handled_Stmt_Node, Body_Stmts);
3511
3512 -- Generate:
3513 -- Deep_Finalize (_init, C1, ..., CN);
3514 -- raise;
3515
3516 if Counter > 0
3517 and then Needs_Finalization (Rec_Type)
3518 and then not Is_Abstract_Type (Rec_Type)
3519 and then not Restriction_Active (No_Exception_Propagation)
3520 then
3521 declare
3522 DF_Call : Node_Id;
3523 DF_Id : Entity_Id;
3524
3525 begin
3526 -- Create a local version of Deep_Finalize which has indication
3527 -- of partial initialization state.
3528
3529 DF_Id :=
3530 Make_Defining_Identifier (Loc,
3531 Chars => New_External_Name (Name_uFinalizer));
3532 Set_Is_Finalizer (DF_Id);
3533
3534 Append_To (Decls, Make_Local_Deep_Finalize (Rec_Type, DF_Id));
3535
3536 DF_Call :=
3537 Make_Procedure_Call_Statement (Loc,
3538 Name => New_Occurrence_Of (DF_Id, Loc),
3539 Parameter_Associations => New_List (
3540 Make_Identifier (Loc, Name_uInit),
3541 New_Occurrence_Of (Standard_False, Loc)));
3542
3543 -- Do not emit warnings related to the elaboration order when a
3544 -- controlled object is declared before the body of Finalize is
3545 -- seen.
3546
3547 if Legacy_Elaboration_Checks then
3548 Set_No_Elaboration_Check (DF_Call);
3549 end if;
3550
3551 Set_Exception_Handlers (Handled_Stmt_Node, New_List (
3552 Make_Exception_Handler (Loc,
3553 Exception_Choices => New_List (
3554 Make_Others_Choice (Loc)),
3555 Statements => New_List (
3556 DF_Call,
3557 Make_Raise_Statement (Loc)))));
3558 end;
3559 else
3560 Set_Exception_Handlers (Handled_Stmt_Node, No_List);
3561 end if;
3562
3563 Set_Handled_Statement_Sequence (Body_Node, Handled_Stmt_Node);
3564
3565 if not Debug_Generated_Code then
3566 Set_Debug_Info_Off (Proc_Id);
3567 end if;
3568
3569 -- Associate Init_Proc with type, and determine if the procedure
3570 -- is null (happens because of the Initialize_Scalars pragma case,
3571 -- where we have to generate a null procedure in case it is called
3572 -- by a client with Initialize_Scalars set). Such procedures have
3573 -- to be generated, but do not have to be called, so we mark them
3574 -- as null to suppress the call. Kill also warnings for the _Init
3575 -- out parameter, which is left entirely uninitialized.
3576
3577 Set_Init_Proc (Rec_Type, Proc_Id);
3578
3579 if Is_Null_Statement_List (Body_Stmts) then
3580 Set_Is_Null_Init_Proc (Proc_Id);
3581 Set_Warnings_Off (Defining_Identifier (First (Parameters)));
3582 end if;
3583 end Build_Init_Procedure;
3584
3585 ---------------------------
3586 -- Build_Init_Statements --
3587 ---------------------------
3588
3589 function Build_Init_Statements (Comp_List : Node_Id) return List_Id is
3590 Checks : constant List_Id := New_List;
3591 Actions : List_Id := No_List;
3592 Counter_Id : Entity_Id := Empty;
3593 Comp_Loc : Source_Ptr;
3594 Decl : Node_Id;
3595 Id : Entity_Id;
3596 Parent_Stmts : List_Id;
3597 Parent_Id : Entity_Id := Empty;
3598 Stmts, Late_Stmts : List_Id := Empty_List;
3599 Typ : Entity_Id;
3600
3601 procedure Increment_Counter
3602 (Loc : Source_Ptr; Late : Boolean := False);
3603 -- Generate an "increment by one" statement for the current counter
3604 -- and append it to the appropriate statement list.
3605
3606 procedure Make_Counter (Loc : Source_Ptr);
3607 -- Create a new counter for the current component list. The routine
3608 -- creates a new defining Id, adds an object declaration and sets
3609 -- the Id generator for the next variant.
3610
3611 -----------------------
3612 -- Increment_Counter --
3613 -----------------------
3614
3615 procedure Increment_Counter
3616 (Loc : Source_Ptr; Late : Boolean := False) is
3617 begin
3618 -- Generate:
3619 -- Counter := Counter + 1;
3620
3621 Append_To ((if Late then Late_Stmts else Stmts),
3622 Make_Assignment_Statement (Loc,
3623 Name => New_Occurrence_Of (Counter_Id, Loc),
3624 Expression =>
3625 Make_Op_Add (Loc,
3626 Left_Opnd => New_Occurrence_Of (Counter_Id, Loc),
3627 Right_Opnd => Make_Integer_Literal (Loc, 1))));
3628 end Increment_Counter;
3629
3630 ------------------
3631 -- Make_Counter --
3632 ------------------
3633
3634 procedure Make_Counter (Loc : Source_Ptr) is
3635 begin
3636 -- Increment the Id generator
3637
3638 Counter := Counter + 1;
3639
3640 -- Create the entity and declaration
3641
3642 Counter_Id :=
3643 Make_Defining_Identifier (Loc,
3644 Chars => New_External_Name ('C', Counter));
3645
3646 -- Generate:
3647 -- Cnn : Integer := 0;
3648
3649 Append_To (Decls,
3650 Make_Object_Declaration (Loc,
3651 Defining_Identifier => Counter_Id,
3652 Object_Definition =>
3653 New_Occurrence_Of (Standard_Integer, Loc),
3654 Expression =>
3655 Make_Integer_Literal (Loc, 0)));
3656 end Make_Counter;
3657
3658 -- Start of processing for Build_Init_Statements
3659
3660 begin
3661 if Null_Present (Comp_List) then
3662 return New_List (Make_Null_Statement (Loc));
3663 end if;
3664
3665 Parent_Stmts := New_List;
3666 Stmts := New_List;
3667
3668 -- Loop through visible declarations of task types and protected
3669 -- types moving any expanded code from the spec to the body of the
3670 -- init procedure.
3671
3672 if Is_Concurrent_Record_Type (Rec_Type) then
3673 declare
3674 Decl : constant Node_Id :=
3675 Parent (Corresponding_Concurrent_Type (Rec_Type));
3676 Def : Node_Id;
3677 N1 : Node_Id;
3678 N2 : Node_Id;
3679
3680 begin
3681 if Is_Task_Record_Type (Rec_Type) then
3682 Def := Task_Definition (Decl);
3683 else
3684 Def := Protected_Definition (Decl);
3685 end if;
3686
3687 if Present (Def) then
3688 N1 := First (Visible_Declarations (Def));
3689 while Present (N1) loop
3690 N2 := N1;
3691 N1 := Next (N1);
3692
3693 if Nkind (N2) in N_Statement_Other_Than_Procedure_Call
3694 or else Nkind (N2) in N_Raise_xxx_Error
3695 or else Nkind (N2) = N_Procedure_Call_Statement
3696 then
3697 Append_To (Stmts,
3698 New_Copy_Tree (N2, New_Scope => Proc_Id));
3699 Rewrite (N2, Make_Null_Statement (Sloc (N2)));
3700 Analyze (N2);
3701 end if;
3702 end loop;
3703 end if;
3704 end;
3705 end if;
3706
3707 -- Loop through components, skipping pragmas, in 2 steps. The first
3708 -- step deals with regular components. The second step deals with
3709 -- components that require late initialization.
3710
3711 -- First pass : regular components
3712
3713 Decl := First_Non_Pragma (Component_Items (Comp_List));
3714 while Present (Decl) loop
3715 Comp_Loc := Sloc (Decl);
3716 Build_Record_Checks
3717 (Subtype_Indication (Component_Definition (Decl)), Checks);
3718
3719 Id := Defining_Identifier (Decl);
3720
3721 -- Obtain the corresponding mutably tagged type's parent subtype
3722 -- to handle default initialization.
3723
3724 Typ := Get_Corresponding_Tagged_Type_If_Present (Etype (Id));
3725
3726 -- Leave any processing of component requiring late initialization
3727 -- for the second pass.
3728
3729 if Initialization_Control.Requires_Late_Init (Decl, Rec_Type) then
3730 if not Has_Late_Init_Comp then
3731 Late_Stmts := New_List;
3732 end if;
3733 Has_Late_Init_Comp := True;
3734
3735 -- Regular component cases
3736
3737 else
3738 -- In the context of the init proc, references to discriminants
3739 -- resolve to denote the discriminals: this is where we can
3740 -- freeze discriminant dependent component subtypes.
3741
3742 if not Is_Frozen (Typ) then
3743 Append_List_To (Stmts, Freeze_Entity (Typ, N));
3744 end if;
3745
3746 -- Explicit initialization
3747
3748 if Present (Expression (Decl)) then
3749 if Is_CPP_Constructor_Call (Expression (Decl)) then
3750 Actions :=
3751 Build_Initialization_Call
3752 (Decl,
3753 Id_Ref =>
3754 Make_Selected_Component (Comp_Loc,
3755 Prefix =>
3756 Make_Identifier (Comp_Loc, Name_uInit),
3757 Selector_Name =>
3758 New_Occurrence_Of (Id, Comp_Loc)),
3759 Typ => Typ,
3760 In_Init_Proc => True,
3761 Enclos_Type => Rec_Type,
3762 Discr_Map => Discr_Map,
3763 Constructor_Ref => Expression (Decl));
3764 else
3765 Actions := Build_Assignment (Id, Expression (Decl));
3766 end if;
3767
3768 -- CPU, Dispatching_Domain, Priority, and Secondary_Stack_Size
3769 -- components are filled in with the corresponding rep-item
3770 -- expression of the concurrent type (if any).
3771
3772 elsif Ekind (Scope (Id)) = E_Record_Type
3773 and then Present (Corresponding_Concurrent_Type (Scope (Id)))
3774 and then Chars (Id) in Name_uCPU
3775 | Name_uDispatching_Domain
3776 | Name_uPriority
3777 | Name_uSecondary_Stack_Size
3778 then
3779 declare
3780 Exp : Node_Id;
3781 Nam : Name_Id;
3782 pragma Warnings (Off, Nam);
3783 Ritem : Node_Id;
3784
3785 begin
3786 if Chars (Id) = Name_uCPU then
3787 Nam := Name_CPU;
3788
3789 elsif Chars (Id) = Name_uDispatching_Domain then
3790 Nam := Name_Dispatching_Domain;
3791
3792 elsif Chars (Id) = Name_uPriority then
3793 Nam := Name_Priority;
3794
3795 elsif Chars (Id) = Name_uSecondary_Stack_Size then
3796 Nam := Name_Secondary_Stack_Size;
3797 end if;
3798
3799 -- Get the Rep Item (aspect specification, attribute
3800 -- definition clause or pragma) of the corresponding
3801 -- concurrent type.
3802
3803 Ritem :=
3804 Get_Rep_Item
3805 (Corresponding_Concurrent_Type (Scope (Id)),
3806 Nam,
3807 Check_Parents => False);
3808
3809 if Present (Ritem) then
3810
3811 -- Pragma case
3812
3813 if Nkind (Ritem) = N_Pragma then
3814 Exp :=
3815 Get_Pragma_Arg
3816 (First (Pragma_Argument_Associations (Ritem)));
3817
3818 -- Conversion for Priority expression
3819
3820 if Nam = Name_Priority then
3821 if Pragma_Name (Ritem) = Name_Priority
3822 and then not GNAT_Mode
3823 then
3824 Exp := Convert_To (RTE (RE_Priority), Exp);
3825 else
3826 Exp :=
3827 Convert_To (RTE (RE_Any_Priority), Exp);
3828 end if;
3829 end if;
3830
3831 -- Aspect/Attribute definition clause case
3832
3833 else
3834 Exp := Expression (Ritem);
3835
3836 -- Conversion for Priority expression
3837
3838 if Nam = Name_Priority then
3839 if Chars (Ritem) = Name_Priority
3840 and then not GNAT_Mode
3841 then
3842 Exp := Convert_To (RTE (RE_Priority), Exp);
3843 else
3844 Exp :=
3845 Convert_To (RTE (RE_Any_Priority), Exp);
3846 end if;
3847 end if;
3848 end if;
3849
3850 -- Conversion for Dispatching_Domain value
3851
3852 if Nam = Name_Dispatching_Domain then
3853 Exp :=
3854 Unchecked_Convert_To
3855 (RTE (RE_Dispatching_Domain_Access), Exp);
3856
3857 -- Conversion for Secondary_Stack_Size value
3858
3859 elsif Nam = Name_Secondary_Stack_Size then
3860 Exp := Convert_To (RTE (RE_Size_Type), Exp);
3861 end if;
3862
3863 Actions := Build_Assignment (Id, Exp);
3864
3865 -- Nothing needed if no Rep Item
3866
3867 else
3868 Actions := No_List;
3869 end if;
3870 end;
3871
3872 -- Composite component with its own Init_Proc
3873
3874 elsif not Is_Interface (Typ)
3875 and then Has_Non_Null_Base_Init_Proc (Typ)
3876 then
3877 declare
3878 use Initialization_Control;
3879 Init_Control_Actual : Node_Id := Empty;
3880 Is_Parent : constant Boolean := Chars (Id) = Name_uParent;
3881 Init_Call_Stmts : List_Id;
3882 begin
3883 if Is_Parent and then Has_Late_Init_Component (Etype (Id))
3884 then
3885 Init_Control_Actual :=
3886 Make_Mode_Literal (Comp_Loc, Early_Init_Only);
3887 -- Parent_Id used later in second call to parent's
3888 -- init proc to initialize late-init components.
3889 Parent_Id := Id;
3890 end if;
3891
3892 Init_Call_Stmts :=
3893 Build_Initialization_Call
3894 (Decl,
3895 Make_Selected_Component (Comp_Loc,
3896 Prefix =>
3897 Make_Identifier (Comp_Loc, Name_uInit),
3898 Selector_Name => New_Occurrence_Of (Id, Comp_Loc)),
3899 Typ,
3900 In_Init_Proc => True,
3901 Enclos_Type => Rec_Type,
3902 Discr_Map => Discr_Map,
3903 Init_Control_Actual => Init_Control_Actual);
3904
3905 if Is_Parent then
3906 -- This is tricky. At first it looks like
3907 -- we are going to end up with nested
3908 -- if-statements with the same condition:
3909 -- if Early_Init_Condition then
3910 -- if Early_Init_Condition then
3911 -- Parent_TypeIP (...);
3912 -- end if;
3913 -- end if;
3914 -- But later we will hoist the inner if-statement
3915 -- out of the outer one; we do this because the
3916 -- init-proc call for the _Parent component of a type
3917 -- extension has to precede any other initialization.
3918 Actions :=
3919 New_List (Make_If_Statement (Loc,
3920 Condition =>
3921 Early_Init_Condition (Loc, Init_Control_Formal),
3922 Then_Statements => Init_Call_Stmts));
3923 else
3924 Actions := Init_Call_Stmts;
3925 end if;
3926 end;
3927
3928 Clean_Task_Names (Typ, Proc_Id);
3929
3930 -- Simple initialization. If the Esize is not yet set, we pass
3931 -- Uint_0 as expected by Get_Simple_Init_Val.
3932
3933 elsif Component_Needs_Simple_Initialization (Typ) then
3934 Actions :=
3935 Build_Assignment
3936 (Id => Id,
3937 Default =>
3938 Get_Simple_Init_Val
3939 (Typ => Typ,
3940 N => N,
3941 Size =>
3942 (if Known_Esize (Id) then Esize (Id)
3943 else Uint_0)));
3944
3945 -- Nothing needed for this case
3946
3947 else
3948 Actions := No_List;
3949 end if;
3950
3951 -- When the component's type has a Default_Initial_Condition,
3952 -- and the component is default initialized, then check the
3953 -- DIC here.
3954
3955 if Has_DIC (Typ)
3956 and then No (Expression (Decl))
3957 and then Present (DIC_Procedure (Typ))
3958 and then not Has_Null_Body (DIC_Procedure (Typ))
3959
3960 -- The DICs of ancestors are checked as part of the type's
3961 -- DIC procedure.
3962
3963 and then Chars (Id) /= Name_uParent
3964
3965 -- In GNATprove mode, the component DICs are checked by other
3966 -- means. They should not be added to the record type DIC
3967 -- procedure, so that the procedure can be used to check the
3968 -- record type invariants or DICs if any.
3969
3970 and then not GNATprove_Mode
3971 then
3972 Append_New_To (Actions,
3973 Build_DIC_Call
3974 (Comp_Loc,
3975 Make_Selected_Component (Comp_Loc,
3976 Prefix =>
3977 Make_Identifier (Comp_Loc, Name_uInit),
3978 Selector_Name =>
3979 New_Occurrence_Of (Id, Comp_Loc)),
3980 Typ));
3981 end if;
3982
3983 if Present (Checks) then
3984 if Chars (Id) = Name_uParent then
3985 Append_List_To (Parent_Stmts, Checks);
3986 else
3987 Append_List_To (Stmts, Checks);
3988 end if;
3989 end if;
3990
3991 if Present (Actions) then
3992 if Chars (Id) = Name_uParent then
3993 Append_List_To (Parent_Stmts, Actions);
3994 else
3995 Append_List_To (Stmts, Actions);
3996
3997 -- Preserve initialization state in the current counter
3998
3999 if Needs_Finalization (Typ) then
4000 if No (Counter_Id) then
4001 Make_Counter (Comp_Loc);
4002 end if;
4003
4004 Increment_Counter (Comp_Loc);
4005 end if;
4006 end if;
4007 end if;
4008 end if;
4009
4010 Next_Non_Pragma (Decl);
4011 end loop;
4012
4013 -- The parent field must be initialized first because variable
4014 -- size components of the parent affect the location of all the
4015 -- new components.
4016
4017 Prepend_List_To (Stmts, Parent_Stmts);
4018
4019 -- Set up tasks and protected object support. This needs to be done
4020 -- before any component with a per-object access discriminant
4021 -- constraint, or any variant part (which may contain such
4022 -- components) is initialized, because the initialization of these
4023 -- components may reference the enclosing concurrent object.
4024
4025 -- For a task record type, add the task create call and calls to bind
4026 -- any interrupt (signal) entries.
4027
4028 if Is_Task_Record_Type (Rec_Type) then
4029
4030 -- In the case of the restricted run time the ATCB has already
4031 -- been preallocated.
4032
4033 if Restricted_Profile then
4034 Append_To (Stmts,
4035 Make_Assignment_Statement (Loc,
4036 Name =>
4037 Make_Selected_Component (Loc,
4038 Prefix => Make_Identifier (Loc, Name_uInit),
4039 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)),
4040 Expression =>
4041 Make_Attribute_Reference (Loc,
4042 Prefix =>
4043 Make_Selected_Component (Loc,
4044 Prefix => Make_Identifier (Loc, Name_uInit),
4045 Selector_Name => Make_Identifier (Loc, Name_uATCB)),
4046 Attribute_Name => Name_Unchecked_Access)));
4047 end if;
4048
4049 Append_To (Stmts, Make_Task_Create_Call (Rec_Type));
4050
4051 declare
4052 Task_Type : constant Entity_Id :=
4053 Corresponding_Concurrent_Type (Rec_Type);
4054 Task_Decl : constant Node_Id := Parent (Task_Type);
4055 Task_Def : constant Node_Id := Task_Definition (Task_Decl);
4056 Decl_Loc : Source_Ptr;
4057 Ent : Entity_Id;
4058 Vis_Decl : Node_Id;
4059
4060 begin
4061 if Present (Task_Def) then
4062 Vis_Decl := First (Visible_Declarations (Task_Def));
4063 while Present (Vis_Decl) loop
4064 Decl_Loc := Sloc (Vis_Decl);
4065
4066 if Nkind (Vis_Decl) = N_Attribute_Definition_Clause then
4067 if Get_Attribute_Id (Chars (Vis_Decl)) =
4068 Attribute_Address
4069 then
4070 Ent := Entity (Name (Vis_Decl));
4071
4072 if Ekind (Ent) = E_Entry then
4073 Append_To (Stmts,
4074 Make_Procedure_Call_Statement (Decl_Loc,
4075 Name =>
4076 New_Occurrence_Of (RTE (
4077 RE_Bind_Interrupt_To_Entry), Decl_Loc),
4078 Parameter_Associations => New_List (
4079 Make_Selected_Component (Decl_Loc,
4080 Prefix =>
4081 Make_Identifier (Decl_Loc, Name_uInit),
4082 Selector_Name =>
4083 Make_Identifier
4084 (Decl_Loc, Name_uTask_Id)),
4085 Entry_Index_Expression
4086 (Decl_Loc, Ent, Empty, Task_Type),
4087 Expression (Vis_Decl))));
4088 end if;
4089 end if;
4090 end if;
4091
4092 Next (Vis_Decl);
4093 end loop;
4094 end if;
4095 end;
4096
4097 -- For a protected type, add statements generated by
4098 -- Make_Initialize_Protection.
4099
4100 elsif Is_Protected_Record_Type (Rec_Type) then
4101 Append_List_To (Stmts,
4102 Make_Initialize_Protection (Rec_Type));
4103 end if;
4104
4105 -- Second pass: components that require late initialization
4106
4107 if Present (Parent_Id) then
4108 declare
4109 Parent_Loc : constant Source_Ptr := Sloc (Parent (Parent_Id));
4110 use Initialization_Control;
4111 begin
4112 -- We are building the init proc for a type extension.
4113 -- Call the parent type's init proc a second time, this
4114 -- time to initialize the parent's components that require
4115 -- late initialization.
4116
4117 Append_List_To (Late_Stmts,
4118 Build_Initialization_Call
4119 (N => Parent (Parent_Id),
4120 Id_Ref =>
4121 Make_Selected_Component (Parent_Loc,
4122 Prefix => Make_Identifier
4123 (Parent_Loc, Name_uInit),
4124 Selector_Name => New_Occurrence_Of (Parent_Id,
4125 Parent_Loc)),
4126 Typ => Etype (Parent_Id),
4127 In_Init_Proc => True,
4128 Enclos_Type => Rec_Type,
4129 Discr_Map => Discr_Map,
4130 Init_Control_Actual => Make_Mode_Literal
4131 (Parent_Loc, Late_Init_Only)));
4132 end;
4133 end if;
4134
4135 if Has_Late_Init_Comp then
4136 Decl := First_Non_Pragma (Component_Items (Comp_List));
4137 while Present (Decl) loop
4138 Comp_Loc := Sloc (Decl);
4139 Id := Defining_Identifier (Decl);
4140
4141 -- Obtain the corresponding mutably tagged type's parent
4142 -- subtype to handle default initialization.
4143
4144 Typ := Get_Corresponding_Tagged_Type_If_Present (Etype (Id));
4145
4146 if Initialization_Control.Requires_Late_Init (Decl, Rec_Type)
4147 then
4148 if Present (Expression (Decl)) then
4149 Append_List_To (Late_Stmts,
4150 Build_Assignment (Id, Expression (Decl)));
4151
4152 elsif Has_Non_Null_Base_Init_Proc (Typ) then
4153 Append_List_To (Late_Stmts,
4154 Build_Initialization_Call (Decl,
4155 Make_Selected_Component (Comp_Loc,
4156 Prefix =>
4157 Make_Identifier (Comp_Loc, Name_uInit),
4158 Selector_Name => New_Occurrence_Of (Id, Comp_Loc)),
4159 Typ,
4160 In_Init_Proc => True,
4161 Enclos_Type => Rec_Type,
4162 Discr_Map => Discr_Map));
4163
4164 Clean_Task_Names (Typ, Proc_Id);
4165
4166 -- Preserve initialization state in the current counter
4167
4168 if Needs_Finalization (Typ) then
4169 if No (Counter_Id) then
4170 Make_Counter (Comp_Loc);
4171 end if;
4172
4173 Increment_Counter (Comp_Loc, Late => True);
4174 end if;
4175 elsif Component_Needs_Simple_Initialization (Typ) then
4176 Append_List_To (Late_Stmts,
4177 Build_Assignment
4178 (Id => Id,
4179 Default =>
4180 Get_Simple_Init_Val
4181 (Typ => Typ,
4182 N => N,
4183 Size => Esize (Id))));
4184 end if;
4185 end if;
4186
4187 Next_Non_Pragma (Decl);
4188 end loop;
4189 end if;
4190
4191 -- Process the variant part (incorrectly ignoring late
4192 -- initialization requirements for components therein).
4193
4194 if Present (Variant_Part (Comp_List)) then
4195 declare
4196 Variant_Alts : constant List_Id := New_List;
4197 Var_Loc : Source_Ptr := No_Location;
4198 Variant : Node_Id;
4199
4200 begin
4201 Variant :=
4202 First_Non_Pragma (Variants (Variant_Part (Comp_List)));
4203 while Present (Variant) loop
4204 Var_Loc := Sloc (Variant);
4205 Append_To (Variant_Alts,
4206 Make_Case_Statement_Alternative (Var_Loc,
4207 Discrete_Choices =>
4208 New_Copy_List (Discrete_Choices (Variant)),
4209 Statements =>
4210 Build_Init_Statements (Component_List (Variant))));
4211 Next_Non_Pragma (Variant);
4212 end loop;
4213
4214 -- The expression of the case statement which is a reference
4215 -- to one of the discriminants is replaced by the appropriate
4216 -- formal parameter of the initialization procedure.
4217
4218 Append_To (Stmts,
4219 Make_Case_Statement (Var_Loc,
4220 Expression =>
4221 New_Occurrence_Of (Discriminal (
4222 Entity (Name (Variant_Part (Comp_List)))), Var_Loc),
4223 Alternatives => Variant_Alts));
4224 end;
4225 end if;
4226
4227 if No (Init_Control_Formal) then
4228 Append_List_To (Stmts, Late_Stmts);
4229
4230 -- If no initializations were generated for component declarations
4231 -- and included in Stmts, then append a null statement to Stmts
4232 -- to make it a valid Ada tree.
4233
4234 if Is_Empty_List (Stmts) then
4235 Append (Make_Null_Statement (Loc), Stmts);
4236 end if;
4237
4238 return Stmts;
4239 else
4240 declare
4241 use Initialization_Control;
4242
4243 If_Early : constant Node_Id :=
4244 (if Is_Empty_List (Stmts) then
4245 Make_Null_Statement (Loc)
4246 else
4247 Make_If_Statement (Loc,
4248 Condition =>
4249 Early_Init_Condition (Loc, Init_Control_Formal),
4250 Then_Statements => Stmts));
4251 If_Late : constant Node_Id :=
4252 (if Is_Empty_List (Late_Stmts) then
4253 Make_Null_Statement (Loc)
4254 else
4255 Make_If_Statement (Loc,
4256 Condition =>
4257 Late_Init_Condition (Loc, Init_Control_Formal),
4258 Then_Statements => Late_Stmts));
4259 begin
4260 return New_List (If_Early, If_Late);
4261 end;
4262 end if;
4263 exception
4264 when RE_Not_Available =>
4265 return Empty_List;
4266 end Build_Init_Statements;
4267
4268 -------------------------
4269 -- Build_Record_Checks --
4270 -------------------------
4271
4272 procedure Build_Record_Checks (S : Node_Id; Check_List : List_Id) is
4273 Subtype_Mark_Id : Entity_Id;
4274
4275 procedure Constrain_Array
4276 (SI : Node_Id;
4277 Check_List : List_Id);
4278 -- Apply a list of index constraints to an unconstrained array type.
4279 -- The first parameter is the entity for the resulting subtype.
4280 -- Check_List is a list to which the check actions are appended.
4281
4282 ---------------------
4283 -- Constrain_Array --
4284 ---------------------
4285
4286 procedure Constrain_Array
4287 (SI : Node_Id;
4288 Check_List : List_Id)
4289 is
4290 C : constant Node_Id := Constraint (SI);
4291 Index : Node_Id;
4292 S, T : Entity_Id;
4293
4294 procedure Constrain_Index
4295 (Index : Node_Id;
4296 S : Node_Id;
4297 Check_List : List_Id);
4298 -- Process an index constraint in a constrained array declaration.
4299 -- The constraint can be either a subtype name or a range with or
4300 -- without an explicit subtype mark. Index is the corresponding
4301 -- index of the unconstrained array. S is the range expression.
4302 -- Check_List is a list to which the check actions are appended.
4303
4304 ---------------------
4305 -- Constrain_Index --
4306 ---------------------
4307
4308 procedure Constrain_Index
4309 (Index : Node_Id;
4310 S : Node_Id;
4311 Check_List : List_Id)
4312 is
4313 T : constant Entity_Id := Etype (Index);
4314
4315 begin
4316 if Nkind (S) = N_Range then
4317 Process_Range_Expr_In_Decl (S, T, Check_List => Check_List);
4318 end if;
4319 end Constrain_Index;
4320
4321 -- Start of processing for Constrain_Array
4322
4323 begin
4324 T := Entity (Subtype_Mark (SI));
4325
4326 if Is_Access_Type (T) then
4327 T := Designated_Type (T);
4328 end if;
4329
4330 -- In either case, the index constraint must provide a discrete
4331 -- range for each index of the array type and the type of each
4332 -- discrete range must be the same as that of the corresponding
4333 -- index. (RM 3.6.1)
4334
4335 S := First (Constraints (C));
4336 Index := First_Index (T);
4337 Analyze (Index);
4338
4339 -- Apply constraints to each index type
4340
4341 while Present (S) loop
4342 Constrain_Index (Index, S, Check_List);
4343 Next (Index);
4344 Next (S);
4345 end loop;
4346 end Constrain_Array;
4347
4348 -- Start of processing for Build_Record_Checks
4349
4350 begin
4351 if Nkind (S) = N_Subtype_Indication then
4352 Find_Type (Subtype_Mark (S));
4353 Subtype_Mark_Id := Entity (Subtype_Mark (S));
4354
4355 -- Remaining processing depends on type
4356
4357 case Ekind (Subtype_Mark_Id) is
4358 when Array_Kind =>
4359 Constrain_Array (S, Check_List);
4360
4361 when others =>
4362 null;
4363 end case;
4364 end if;
4365 end Build_Record_Checks;
4366
4367 -------------------------------------------
4368 -- Component_Needs_Simple_Initialization --
4369 -------------------------------------------
4370
4371 function Component_Needs_Simple_Initialization
4372 (T : Entity_Id) return Boolean
4373 is
4374 begin
4375 return
4376 Needs_Simple_Initialization (T)
4377 and then not Is_RTE (T, RE_Tag)
4378
4379 -- Ada 2005 (AI-251): Check also the tag of abstract interfaces
4380
4381 and then not Is_RTE (T, RE_Interface_Tag);
4382 end Component_Needs_Simple_Initialization;
4383
4384 --------------------------------------
4385 -- Parent_Subtype_Renaming_Discrims --
4386 --------------------------------------
4387
4388 function Parent_Subtype_Renaming_Discrims return Boolean is
4389 De : Entity_Id;
4390 Dp : Entity_Id;
4391
4392 begin
4393 if Base_Type (Rec_Ent) /= Rec_Ent then
4394 return False;
4395 end if;
4396
4397 if Etype (Rec_Ent) = Rec_Ent
4398 or else not Has_Discriminants (Rec_Ent)
4399 or else Is_Constrained (Rec_Ent)
4400 or else Is_Tagged_Type (Rec_Ent)
4401 then
4402 return False;
4403 end if;
4404
4405 -- If there are no explicit stored discriminants we have inherited
4406 -- the root type discriminants so far, so no renamings occurred.
4407
4408 if First_Discriminant (Rec_Ent) =
4409 First_Stored_Discriminant (Rec_Ent)
4410 then
4411 return False;
4412 end if;
4413
4414 -- Check if we have done some trivial renaming of the parent
4415 -- discriminants, i.e. something like
4416 --
4417 -- type DT (X1, X2: int) is new PT (X1, X2);
4418
4419 De := First_Discriminant (Rec_Ent);
4420 Dp := First_Discriminant (Etype (Rec_Ent));
4421 while Present (De) loop
4422 pragma Assert (Present (Dp));
4423
4424 if Corresponding_Discriminant (De) /= Dp then
4425 return True;
4426 end if;
4427
4428 Next_Discriminant (De);
4429 Next_Discriminant (Dp);
4430 end loop;
4431
4432 return Present (Dp);
4433 end Parent_Subtype_Renaming_Discrims;
4434
4435 ------------------------
4436 -- Requires_Init_Proc --
4437 ------------------------
4438
4439 function Requires_Init_Proc (Rec_Id : Entity_Id) return Boolean is
4440 Comp_Decl : Node_Id;
4441 Id : Entity_Id;
4442 Typ : Entity_Id;
4443
4444 begin
4445 -- Definitely do not need one if specifically suppressed
4446
4447 if Initialization_Suppressed (Rec_Id) then
4448 return False;
4449 end if;
4450
4451 -- If it is a type derived from a type with unknown discriminants,
4452 -- we cannot build an initialization procedure for it.
4453
4454 if Has_Unknown_Discriminants (Rec_Id)
4455 or else Has_Unknown_Discriminants (Etype (Rec_Id))
4456 then
4457 return False;
4458 end if;
4459
4460 -- Otherwise we need to generate an initialization procedure if
4461 -- Is_CPP_Class is False and at least one of the following applies:
4462
4463 -- 1. Discriminants are present, since they need to be initialized
4464 -- with the appropriate discriminant constraint expressions.
4465 -- However, the discriminant of an unchecked union does not
4466 -- count, since the discriminant is not present.
4467
4468 -- 2. The type is a tagged type, since the implicit Tag component
4469 -- needs to be initialized with a pointer to the dispatch table.
4470
4471 -- 3. The type contains tasks
4472
4473 -- 4. One or more components has an initial value
4474
4475 -- 5. One or more components is for a type which itself requires
4476 -- an initialization procedure.
4477
4478 -- 6. One or more components is a type that requires simple
4479 -- initialization (see Needs_Simple_Initialization), except
4480 -- that types Tag and Interface_Tag are excluded, since fields
4481 -- of these types are initialized by other means.
4482
4483 -- 7. The type is the record type built for a task type (since at
4484 -- the very least, Create_Task must be called)
4485
4486 -- 8. The type is the record type built for a protected type (since
4487 -- at least Initialize_Protection must be called)
4488
4489 -- 9. The type is marked as a public entity. The reason we add this
4490 -- case (even if none of the above apply) is to properly handle
4491 -- Initialize_Scalars. If a package is compiled without an IS
4492 -- pragma, and the client is compiled with an IS pragma, then
4493 -- the client will think an initialization procedure is present
4494 -- and call it, when in fact no such procedure is required, but
4495 -- since the call is generated, there had better be a routine
4496 -- at the other end of the call, even if it does nothing).
4497
4498 -- Note: the reason we exclude the CPP_Class case is because in this
4499 -- case the initialization is performed by the C++ constructors, and
4500 -- the IP is built by Set_CPP_Constructors.
4501
4502 if Is_CPP_Class (Rec_Id) then
4503 return False;
4504
4505 elsif Is_Interface (Rec_Id) then
4506 return False;
4507
4508 elsif (Has_Discriminants (Rec_Id)
4509 and then not Is_Unchecked_Union (Rec_Id))
4510 or else Is_Tagged_Type (Rec_Id)
4511 or else Is_Concurrent_Record_Type (Rec_Id)
4512 or else Has_Task (Rec_Id)
4513 then
4514 return True;
4515 end if;
4516
4517 Id := First_Component (Rec_Id);
4518 while Present (Id) loop
4519 Comp_Decl := Parent (Id);
4520 Typ := Etype (Id);
4521
4522 if Present (Expression (Comp_Decl))
4523 or else Has_Non_Null_Base_Init_Proc (Typ)
4524 or else Component_Needs_Simple_Initialization (Typ)
4525 then
4526 return True;
4527 end if;
4528
4529 Next_Component (Id);
4530 end loop;
4531
4532 -- As explained above, a record initialization procedure is needed
4533 -- for public types in case Initialize_Scalars applies to a client.
4534 -- However, such a procedure is not needed in the case where either
4535 -- of restrictions No_Initialize_Scalars or No_Default_Initialization
4536 -- applies. No_Initialize_Scalars excludes the possibility of using
4537 -- Initialize_Scalars in any partition, and No_Default_Initialization
4538 -- implies that no initialization should ever be done for objects of
4539 -- the type, so is incompatible with Initialize_Scalars.
4540
4541 if not Restriction_Active (No_Initialize_Scalars)
4542 and then not Restriction_Active (No_Default_Initialization)
4543 and then Is_Public (Rec_Id)
4544 then
4545 return True;
4546 end if;
4547
4548 return False;
4549 end Requires_Init_Proc;
4550
4551 -- Start of processing for Build_Record_Init_Proc
4552
4553 begin
4554 Rec_Type := Defining_Identifier (N);
4555
4556 -- This may be full declaration of a private type, in which case
4557 -- the visible entity is a record, and the private entity has been
4558 -- exchanged with it in the private part of the current package.
4559 -- The initialization procedure is built for the record type, which
4560 -- is retrievable from the private entity.
4561
4562 if Is_Incomplete_Or_Private_Type (Rec_Type) then
4563 Rec_Type := Underlying_Type (Rec_Type);
4564 end if;
4565
4566 -- If we have a variant record with restriction No_Implicit_Conditionals
4567 -- in effect, then we skip building the procedure. This is safe because
4568 -- if we can see the restriction, so can any caller, calls to initialize
4569 -- such records are not allowed for variant records if this restriction
4570 -- is active.
4571
4572 if Has_Variant_Part (Rec_Type)
4573 and then Restriction_Active (No_Implicit_Conditionals)
4574 then
4575 return;
4576 end if;
4577
4578 -- If there are discriminants, build the discriminant map to replace
4579 -- discriminants by their discriminals in complex bound expressions.
4580 -- These only arise for the corresponding records of synchronized types.
4581
4582 if Is_Concurrent_Record_Type (Rec_Type)
4583 and then Has_Discriminants (Rec_Type)
4584 then
4585 declare
4586 Disc : Entity_Id;
4587 begin
4588 Disc := First_Discriminant (Rec_Type);
4589 while Present (Disc) loop
4590 Append_Elmt (Disc, Discr_Map);
4591 Append_Elmt (Discriminal (Disc), Discr_Map);
4592 Next_Discriminant (Disc);
4593 end loop;
4594 end;
4595 end if;
4596
4597 -- Derived types that have no type extension can use the initialization
4598 -- procedure of their parent and do not need a procedure of their own.
4599 -- This is only correct if there are no representation clauses for the
4600 -- type or its parent, and if the parent has in fact been frozen so
4601 -- that its initialization procedure exists.
4602
4603 if Is_Derived_Type (Rec_Type)
4604 and then not Is_Tagged_Type (Rec_Type)
4605 and then not Is_Unchecked_Union (Rec_Type)
4606 and then not Has_New_Non_Standard_Rep (Rec_Type)
4607 and then not Parent_Subtype_Renaming_Discrims
4608 and then Present (Base_Init_Proc (Etype (Rec_Type)))
4609 then
4610 Copy_TSS (Base_Init_Proc (Etype (Rec_Type)), Rec_Type);
4611
4612 -- Otherwise if we need an initialization procedure, then build one,
4613 -- mark it as public and inlinable and as having a completion.
4614
4615 elsif Requires_Init_Proc (Rec_Type)
4616 or else Is_Unchecked_Union (Rec_Type)
4617 then
4618 Proc_Id :=
4619 Make_Defining_Identifier (Loc,
4620 Chars => Make_Init_Proc_Name (Rec_Type));
4621
4622 -- If No_Default_Initialization restriction is active, then we don't
4623 -- want to build an init_proc, but we need to mark that an init_proc
4624 -- would be needed if this restriction was not active (so that we can
4625 -- detect attempts to call it), so set a dummy init_proc in place.
4626
4627 if Restriction_Active (No_Default_Initialization) then
4628 Set_Init_Proc (Rec_Type, Proc_Id);
4629 return;
4630 end if;
4631
4632 Build_Offset_To_Top_Functions;
4633 Build_CPP_Init_Procedure;
4634 Build_Init_Procedure;
4635
4636 Set_Is_Public (Proc_Id, Is_Public (Rec_Ent));
4637 Set_Is_Internal (Proc_Id);
4638 Set_Has_Completion (Proc_Id);
4639
4640 if not Debug_Generated_Code then
4641 Set_Debug_Info_Off (Proc_Id);
4642 end if;
4643
4644 Set_Is_Inlined (Proc_Id, Inline_Init_Proc (Rec_Type));
4645
4646 declare
4647 Agg : constant Node_Id :=
4648 Build_Equivalent_Record_Aggregate (Rec_Type);
4649
4650 procedure Collect_Itypes (Comp : Node_Id);
4651 -- Generate references to itypes in the aggregate, because
4652 -- the first use of the aggregate may be in a nested scope.
4653
4654 --------------------
4655 -- Collect_Itypes --
4656 --------------------
4657
4658 procedure Collect_Itypes (Comp : Node_Id) is
4659 Ref : Node_Id;
4660 Sub_Aggr : Node_Id;
4661 Typ : constant Entity_Id := Etype (Comp);
4662
4663 begin
4664 if Is_Array_Type (Typ) and then Is_Itype (Typ) then
4665 Ref := Make_Itype_Reference (Loc);
4666 Set_Itype (Ref, Typ);
4667 Append_Freeze_Action (Rec_Type, Ref);
4668
4669 Ref := Make_Itype_Reference (Loc);
4670 Set_Itype (Ref, Etype (First_Index (Typ)));
4671 Append_Freeze_Action (Rec_Type, Ref);
4672
4673 -- Recurse on nested arrays
4674
4675 Sub_Aggr := First (Expressions (Comp));
4676 while Present (Sub_Aggr) loop
4677 Collect_Itypes (Sub_Aggr);
4678 Next (Sub_Aggr);
4679 end loop;
4680 end if;
4681 end Collect_Itypes;
4682
4683 begin
4684 -- If there is a static initialization aggregate for the type,
4685 -- generate itype references for the types of its (sub)components,
4686 -- to prevent out-of-scope errors in the resulting tree.
4687 -- The aggregate may have been rewritten as a Raise node, in which
4688 -- case there are no relevant itypes.
4689
4690 if Present (Agg) and then Nkind (Agg) = N_Aggregate then
4691 Set_Static_Initialization (Proc_Id, Agg);
4692
4693 declare
4694 Comp : Node_Id;
4695 begin
4696 Comp := First (Component_Associations (Agg));
4697 while Present (Comp) loop
4698 Collect_Itypes (Expression (Comp));
4699 Next (Comp);
4700 end loop;
4701 end;
4702 end if;
4703 end;
4704 end if;
4705 end Build_Record_Init_Proc;
4706
4707 ----------------------------
4708 -- Build_Slice_Assignment --
4709 ----------------------------
4710
4711 -- Generates the following subprogram:
4712
4713 -- procedure array_typeSA
4714 -- (Source, Target : Array_Type,
4715 -- Left_Lo, Left_Hi : Index;
4716 -- Right_Lo, Right_Hi : Index;
4717 -- Rev : Boolean)
4718 -- is
4719 -- Li1 : Index;
4720 -- Ri1 : Index;
4721
4722 -- begin
4723 -- if Left_Hi < Left_Lo then
4724 -- return;
4725 -- end if;
4726
4727 -- if Rev then
4728 -- Li1 := Left_Hi;
4729 -- Ri1 := Right_Hi;
4730 -- else
4731 -- Li1 := Left_Lo;
4732 -- Ri1 := Right_Lo;
4733 -- end if;
4734
4735 -- loop
4736 -- Target (Li1) := Source (Ri1);
4737
4738 -- if Rev then
4739 -- exit when Li1 = Left_Lo;
4740 -- Li1 := Index'pred (Li1);
4741 -- Ri1 := Index'pred (Ri1);
4742 -- else
4743 -- exit when Li1 = Left_Hi;
4744 -- Li1 := Index'succ (Li1);
4745 -- Ri1 := Index'succ (Ri1);
4746 -- end if;
4747 -- end loop;
4748 -- end array_typeSA;
4749
4750 procedure Build_Slice_Assignment (Typ : Entity_Id) is
4751 Loc : constant Source_Ptr := Sloc (Typ);
4752 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
4753
4754 Larray : constant Entity_Id := Make_Temporary (Loc, 'A');
4755 Rarray : constant Entity_Id := Make_Temporary (Loc, 'R');
4756 Left_Lo : constant Entity_Id := Make_Temporary (Loc, 'L');
4757 Left_Hi : constant Entity_Id := Make_Temporary (Loc, 'L');
4758 Right_Lo : constant Entity_Id := Make_Temporary (Loc, 'R');
4759 Right_Hi : constant Entity_Id := Make_Temporary (Loc, 'R');
4760 Rev : constant Entity_Id := Make_Temporary (Loc, 'D');
4761 -- Formal parameters of procedure
4762
4763 Proc_Name : constant Entity_Id :=
4764 Make_Defining_Identifier (Loc,
4765 Chars => Make_TSS_Name (Typ, TSS_Slice_Assign));
4766
4767 Lnn : constant Entity_Id := Make_Temporary (Loc, 'L');
4768 Rnn : constant Entity_Id := Make_Temporary (Loc, 'R');
4769 -- Subscripts for left and right sides
4770
4771 Decls : List_Id;
4772 Loops : Node_Id;
4773 Stats : List_Id;
4774
4775 begin
4776 -- Build declarations for indexes
4777
4778 Decls := New_List;
4779
4780 Append_To (Decls,
4781 Make_Object_Declaration (Loc,
4782 Defining_Identifier => Lnn,
4783 Object_Definition =>
4784 New_Occurrence_Of (Index, Loc)));
4785
4786 Append_To (Decls,
4787 Make_Object_Declaration (Loc,
4788 Defining_Identifier => Rnn,
4789 Object_Definition =>
4790 New_Occurrence_Of (Index, Loc)));
4791
4792 Stats := New_List;
4793
4794 -- Build test for empty slice case
4795
4796 Append_To (Stats,
4797 Make_If_Statement (Loc,
4798 Condition =>
4799 Make_Op_Lt (Loc,
4800 Left_Opnd => New_Occurrence_Of (Left_Hi, Loc),
4801 Right_Opnd => New_Occurrence_Of (Left_Lo, Loc)),
4802 Then_Statements => New_List (Make_Simple_Return_Statement (Loc))));
4803
4804 -- Build initializations for indexes
4805
4806 declare
4807 F_Init : constant List_Id := New_List;
4808 B_Init : constant List_Id := New_List;
4809
4810 begin
4811 Append_To (F_Init,
4812 Make_Assignment_Statement (Loc,
4813 Name => New_Occurrence_Of (Lnn, Loc),
4814 Expression => New_Occurrence_Of (Left_Lo, Loc)));
4815
4816 Append_To (F_Init,
4817 Make_Assignment_Statement (Loc,
4818 Name => New_Occurrence_Of (Rnn, Loc),
4819 Expression => New_Occurrence_Of (Right_Lo, Loc)));
4820
4821 Append_To (B_Init,
4822 Make_Assignment_Statement (Loc,
4823 Name => New_Occurrence_Of (Lnn, Loc),
4824 Expression => New_Occurrence_Of (Left_Hi, Loc)));
4825
4826 Append_To (B_Init,
4827 Make_Assignment_Statement (Loc,
4828 Name => New_Occurrence_Of (Rnn, Loc),
4829 Expression => New_Occurrence_Of (Right_Hi, Loc)));
4830
4831 Append_To (Stats,
4832 Make_If_Statement (Loc,
4833 Condition => New_Occurrence_Of (Rev, Loc),
4834 Then_Statements => B_Init,
4835 Else_Statements => F_Init));
4836 end;
4837
4838 -- Now construct the assignment statement
4839
4840 Loops :=
4841 Make_Loop_Statement (Loc,
4842 Statements => New_List (
4843 Make_Assignment_Statement (Loc,
4844 Name =>
4845 Make_Indexed_Component (Loc,
4846 Prefix => New_Occurrence_Of (Larray, Loc),
4847 Expressions => New_List (New_Occurrence_Of (Lnn, Loc))),
4848 Expression =>
4849 Make_Indexed_Component (Loc,
4850 Prefix => New_Occurrence_Of (Rarray, Loc),
4851 Expressions => New_List (New_Occurrence_Of (Rnn, Loc))))),
4852 End_Label => Empty);
4853
4854 -- Build the exit condition and increment/decrement statements
4855
4856 declare
4857 F_Ass : constant List_Id := New_List;
4858 B_Ass : constant List_Id := New_List;
4859
4860 begin
4861 Append_To (F_Ass,
4862 Make_Exit_Statement (Loc,
4863 Condition =>
4864 Make_Op_Eq (Loc,
4865 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
4866 Right_Opnd => New_Occurrence_Of (Left_Hi, Loc))));
4867
4868 Append_To (F_Ass,
4869 Make_Assignment_Statement (Loc,
4870 Name => New_Occurrence_Of (Lnn, Loc),
4871 Expression =>
4872 Make_Attribute_Reference (Loc,
4873 Prefix =>
4874 New_Occurrence_Of (Index, Loc),
4875 Attribute_Name => Name_Succ,
4876 Expressions => New_List (
4877 New_Occurrence_Of (Lnn, Loc)))));
4878
4879 Append_To (F_Ass,
4880 Make_Assignment_Statement (Loc,
4881 Name => New_Occurrence_Of (Rnn, Loc),
4882 Expression =>
4883 Make_Attribute_Reference (Loc,
4884 Prefix =>
4885 New_Occurrence_Of (Index, Loc),
4886 Attribute_Name => Name_Succ,
4887 Expressions => New_List (
4888 New_Occurrence_Of (Rnn, Loc)))));
4889
4890 Append_To (B_Ass,
4891 Make_Exit_Statement (Loc,
4892 Condition =>
4893 Make_Op_Eq (Loc,
4894 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
4895 Right_Opnd => New_Occurrence_Of (Left_Lo, Loc))));
4896
4897 Append_To (B_Ass,
4898 Make_Assignment_Statement (Loc,
4899 Name => New_Occurrence_Of (Lnn, Loc),
4900 Expression =>
4901 Make_Attribute_Reference (Loc,
4902 Prefix =>
4903 New_Occurrence_Of (Index, Loc),
4904 Attribute_Name => Name_Pred,
4905 Expressions => New_List (
4906 New_Occurrence_Of (Lnn, Loc)))));
4907
4908 Append_To (B_Ass,
4909 Make_Assignment_Statement (Loc,
4910 Name => New_Occurrence_Of (Rnn, Loc),
4911 Expression =>
4912 Make_Attribute_Reference (Loc,
4913 Prefix =>
4914 New_Occurrence_Of (Index, Loc),
4915 Attribute_Name => Name_Pred,
4916 Expressions => New_List (
4917 New_Occurrence_Of (Rnn, Loc)))));
4918
4919 Append_To (Statements (Loops),
4920 Make_If_Statement (Loc,
4921 Condition => New_Occurrence_Of (Rev, Loc),
4922 Then_Statements => B_Ass,
4923 Else_Statements => F_Ass));
4924 end;
4925
4926 Append_To (Stats, Loops);
4927
4928 declare
4929 Spec : Node_Id;
4930 Formals : List_Id;
4931
4932 begin
4933 Formals := New_List (
4934 Make_Parameter_Specification (Loc,
4935 Defining_Identifier => Larray,
4936 Out_Present => True,
4937 Parameter_Type =>
4938 New_Occurrence_Of (Base_Type (Typ), Loc)),
4939
4940 Make_Parameter_Specification (Loc,
4941 Defining_Identifier => Rarray,
4942 Parameter_Type =>
4943 New_Occurrence_Of (Base_Type (Typ), Loc)),
4944
4945 Make_Parameter_Specification (Loc,
4946 Defining_Identifier => Left_Lo,
4947 Parameter_Type =>
4948 New_Occurrence_Of (Index, Loc)),
4949
4950 Make_Parameter_Specification (Loc,
4951 Defining_Identifier => Left_Hi,
4952 Parameter_Type =>
4953 New_Occurrence_Of (Index, Loc)),
4954
4955 Make_Parameter_Specification (Loc,
4956 Defining_Identifier => Right_Lo,
4957 Parameter_Type =>
4958 New_Occurrence_Of (Index, Loc)),
4959
4960 Make_Parameter_Specification (Loc,
4961 Defining_Identifier => Right_Hi,
4962 Parameter_Type =>
4963 New_Occurrence_Of (Index, Loc)));
4964
4965 Append_To (Formals,
4966 Make_Parameter_Specification (Loc,
4967 Defining_Identifier => Rev,
4968 Parameter_Type =>
4969 New_Occurrence_Of (Standard_Boolean, Loc)));
4970
4971 Spec :=
4972 Make_Procedure_Specification (Loc,
4973 Defining_Unit_Name => Proc_Name,
4974 Parameter_Specifications => Formals);
4975
4976 Discard_Node (
4977 Make_Subprogram_Body (Loc,
4978 Specification => Spec,
4979 Declarations => Decls,
4980 Handled_Statement_Sequence =>
4981 Make_Handled_Sequence_Of_Statements (Loc,
4982 Statements => Stats)));
4983 end;
4984
4985 Set_TSS (Typ, Proc_Name);
4986 Set_Is_Pure (Proc_Name);
4987 end Build_Slice_Assignment;
4988
4989 ------------------------------------
4990 -- Build_Untagged_Record_Equality --
4991 ------------------------------------
4992
4993 procedure Build_Untagged_Record_Equality (Typ : Entity_Id) is
4994 Build_Eq : Boolean;
4995 Comp : Entity_Id;
4996 Decl : Node_Id;
4997 Op : Entity_Id;
4998 Eq_Op : Entity_Id;
4999
5000 function User_Defined_Eq (T : Entity_Id) return Entity_Id;
5001 -- Check whether the type T has a user-defined primitive equality. If so
5002 -- return it, else return Empty. If true for a component of Typ, we have
5003 -- to build the primitive equality for it.
5004
5005 ---------------------
5006 -- User_Defined_Eq --
5007 ---------------------
5008
5009 function User_Defined_Eq (T : Entity_Id) return Entity_Id is
5010 Op : constant Entity_Id := TSS (T, TSS_Composite_Equality);
5011
5012 begin
5013 if Present (Op) then
5014 return Op;
5015 else
5016 return Get_User_Defined_Equality (T);
5017 end if;
5018 end User_Defined_Eq;
5019
5020 -- Start of processing for Build_Untagged_Record_Equality
5021
5022 begin
5023 -- If a record component has a primitive equality operation, we must
5024 -- build the corresponding one for the current type.
5025
5026 Build_Eq := False;
5027 Comp := First_Component (Typ);
5028 while Present (Comp) loop
5029 if Is_Record_Type (Etype (Comp))
5030 and then Present (User_Defined_Eq (Etype (Comp)))
5031 then
5032 Build_Eq := True;
5033 exit;
5034 end if;
5035
5036 Next_Component (Comp);
5037 end loop;
5038
5039 -- If there is a user-defined equality for the type, we do not create
5040 -- the implicit one.
5041
5042 Eq_Op := Get_User_Defined_Equality (Typ);
5043 if Present (Eq_Op) then
5044 if Comes_From_Source (Eq_Op) then
5045 Build_Eq := False;
5046 else
5047 Eq_Op := Empty;
5048 end if;
5049 end if;
5050
5051 -- If the type is derived, inherit the operation, if present, from the
5052 -- parent type. It may have been declared after the type derivation. If
5053 -- the parent type itself is derived, it may have inherited an operation
5054 -- that has itself been overridden, so update its alias and related
5055 -- flags. Ditto for inequality.
5056
5057 if No (Eq_Op) and then Is_Derived_Type (Typ) then
5058 Eq_Op := Get_User_Defined_Equality (Etype (Typ));
5059 if Present (Eq_Op) then
5060 Copy_TSS (Eq_Op, Typ);
5061 Build_Eq := False;
5062
5063 declare
5064 Op : constant Entity_Id := User_Defined_Eq (Typ);
5065 NE_Op : constant Entity_Id := Next_Entity (Eq_Op);
5066
5067 begin
5068 if Present (Op) then
5069 Set_Alias (Op, Eq_Op);
5070 Set_Is_Abstract_Subprogram
5071 (Op, Is_Abstract_Subprogram (Eq_Op));
5072
5073 if Chars (Next_Entity (Op)) = Name_Op_Ne then
5074 Set_Is_Abstract_Subprogram
5075 (Next_Entity (Op), Is_Abstract_Subprogram (NE_Op));
5076 end if;
5077 end if;
5078 end;
5079 end if;
5080 end if;
5081
5082 -- If not inherited and not user-defined, build body as for a type with
5083 -- components of record type (i.e. a type for which "=" composes when
5084 -- used as a component in an outer composite type).
5085
5086 if Build_Eq then
5087 Decl :=
5088 Make_Eq_Body (Typ, Make_TSS_Name (Typ, TSS_Composite_Equality));
5089 Op := Defining_Entity (Decl);
5090 Set_TSS (Typ, Op);
5091 Set_Is_Pure (Op);
5092
5093 if Is_Library_Level_Entity (Typ) then
5094 Set_Is_Public (Op);
5095 end if;
5096 end if;
5097 end Build_Untagged_Record_Equality;
5098
5099 -----------------------------------
5100 -- Build_Variant_Record_Equality --
5101 -----------------------------------
5102
5103 -- Generates:
5104
5105 -- function <<Body_Id>> (Left, Right : T) return Boolean is
5106 -- [ X : T renames Left; ]
5107 -- [ Y : T renames Right; ]
5108 -- -- The above renamings are generated only if the parameters of
5109 -- -- this built function (which are passed by the caller) are not
5110 -- -- named 'X' and 'Y'; these names are required to reuse several
5111 -- -- expander routines when generating this body.
5112
5113 -- begin
5114 -- -- Compare discriminants
5115
5116 -- if X.D1 /= Y.D1 or else X.D2 /= Y.D2 or else ... then
5117 -- return False;
5118 -- end if;
5119
5120 -- -- Compare components
5121
5122 -- if X.C1 /= Y.C1 or else X.C2 /= Y.C2 or else ... then
5123 -- return False;
5124 -- end if;
5125
5126 -- -- Compare variant part
5127
5128 -- case X.D1 is
5129 -- when V1 =>
5130 -- if X.C2 /= Y.C2 or else X.C3 /= Y.C3 or else ... then
5131 -- return False;
5132 -- end if;
5133 -- ...
5134 -- when Vn =>
5135 -- if X.Cn /= Y.Cn or else ... then
5136 -- return False;
5137 -- end if;
5138 -- end case;
5139
5140 -- return True;
5141 -- end _Equality;
5142
5143 function Build_Variant_Record_Equality
5144 (Typ : Entity_Id;
5145 Spec_Id : Entity_Id;
5146 Body_Id : Entity_Id;
5147 Param_Specs : List_Id) return Node_Id
5148 is
5149 Loc : constant Source_Ptr := Sloc (Typ);
5150 Def : constant Node_Id := Parent (Typ);
5151 Comps : constant Node_Id := Component_List (Type_Definition (Def));
5152 Left : constant Entity_Id := Defining_Identifier (First (Param_Specs));
5153 Right : constant Entity_Id :=
5154 Defining_Identifier (Next (First (Param_Specs)));
5155 Decls : constant List_Id := New_List;
5156 Stmts : constant List_Id := New_List;
5157
5158 Subp_Body : Node_Id;
5159
5160 begin
5161 pragma Assert (not Is_Tagged_Type (Typ));
5162
5163 -- In order to reuse the expander routines Make_Eq_If and Make_Eq_Case
5164 -- the name of the formals must be X and Y; otherwise we generate two
5165 -- renaming declarations for such purpose.
5166
5167 if Chars (Left) /= Name_X then
5168 Append_To (Decls,
5169 Make_Object_Renaming_Declaration (Loc,
5170 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
5171 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
5172 Name => Make_Identifier (Loc, Chars (Left))));
5173 end if;
5174
5175 if Chars (Right) /= Name_Y then
5176 Append_To (Decls,
5177 Make_Object_Renaming_Declaration (Loc,
5178 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
5179 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
5180 Name => Make_Identifier (Loc, Chars (Right))));
5181 end if;
5182
5183 -- Unchecked_Unions require additional machinery to support equality.
5184 -- Two extra parameters (A and B) are added to the equality function
5185 -- parameter list for each discriminant of the type, in order to
5186 -- capture the inferred values of the discriminants in equality calls.
5187 -- The names of the parameters match the names of the corresponding
5188 -- discriminant, with an added suffix.
5189
5190 if Is_Unchecked_Union (Typ) then
5191 declare
5192 Right_Formal : constant Entity_Id :=
5193 (if Present (Spec_Id) then Last_Formal (Spec_Id) else Right);
5194 Scop : constant Entity_Id :=
5195 (if Present (Spec_Id) then Spec_Id else Body_Id);
5196
5197 procedure Decorate_Extra_Formal (F, F_Typ : Entity_Id);
5198 -- Decorate extra formal F with type F_Typ
5199
5200 ---------------------------
5201 -- Decorate_Extra_Formal --
5202 ---------------------------
5203
5204 procedure Decorate_Extra_Formal (F, F_Typ : Entity_Id) is
5205 begin
5206 Mutate_Ekind (F, E_In_Parameter);
5207 Set_Etype (F, F_Typ);
5208 Set_Scope (F, Scop);
5209 Set_Mechanism (F, By_Copy);
5210 end Decorate_Extra_Formal;
5211
5212 A : Entity_Id;
5213 B : Entity_Id;
5214 Discr : Entity_Id;
5215 Discr_Type : Entity_Id;
5216 Last_Extra : Entity_Id := Empty;
5217 New_Discrs : Elist_Id;
5218
5219 begin
5220 Mutate_Ekind (Body_Id, E_Subprogram_Body);
5221 New_Discrs := New_Elmt_List;
5222
5223 Discr := First_Discriminant (Typ);
5224 while Present (Discr) loop
5225 Discr_Type := Etype (Discr);
5226
5227 -- Add the new parameters as extra formals
5228
5229 A :=
5230 Make_Defining_Identifier (Loc,
5231 Chars => New_External_Name (Chars (Discr), 'A'));
5232
5233 Decorate_Extra_Formal (A, Discr_Type);
5234
5235 if Present (Last_Extra) then
5236 Set_Extra_Formal (Last_Extra, A);
5237 else
5238 Set_Extra_Formal (Right_Formal, A);
5239 Set_Extra_Formals (Scop, A);
5240 end if;
5241
5242 Append_Elmt (A, New_Discrs);
5243
5244 B :=
5245 Make_Defining_Identifier (Loc,
5246 Chars => New_External_Name (Chars (Discr), 'B'));
5247
5248 Decorate_Extra_Formal (B, Discr_Type);
5249
5250 Set_Extra_Formal (A, B);
5251 Last_Extra := B;
5252
5253 -- Generate the following code to compare each of the inferred
5254 -- discriminants:
5255
5256 -- if a /= b then
5257 -- return False;
5258 -- end if;
5259
5260 Append_To (Stmts,
5261 Make_If_Statement (Loc,
5262 Condition =>
5263 Make_Op_Ne (Loc,
5264 Left_Opnd => New_Occurrence_Of (A, Loc),
5265 Right_Opnd => New_Occurrence_Of (B, Loc)),
5266 Then_Statements => New_List (
5267 Make_Simple_Return_Statement (Loc,
5268 Expression =>
5269 New_Occurrence_Of (Standard_False, Loc)))));
5270
5271 Next_Discriminant (Discr);
5272 end loop;
5273
5274 -- Generate component-by-component comparison. Note that we must
5275 -- propagate the inferred discriminants formals to act as the case
5276 -- statement switch. Their value is added when an equality call on
5277 -- unchecked unions is expanded.
5278
5279 Append_List_To (Stmts, Make_Eq_Case (Typ, Comps, New_Discrs));
5280 end;
5281
5282 -- Normal case (not unchecked union)
5283
5284 else
5285 Append_To (Stmts,
5286 Make_Eq_If (Typ, Discriminant_Specifications (Def)));
5287 Append_List_To (Stmts, Make_Eq_Case (Typ, Comps));
5288 end if;
5289
5290 Append_To (Stmts,
5291 Make_Simple_Return_Statement (Loc,
5292 Expression => New_Occurrence_Of (Standard_True, Loc)));
5293
5294 Subp_Body :=
5295 Make_Subprogram_Body (Loc,
5296 Specification =>
5297 Make_Function_Specification (Loc,
5298 Defining_Unit_Name => Body_Id,
5299 Parameter_Specifications => Param_Specs,
5300 Result_Definition =>
5301 New_Occurrence_Of (Standard_Boolean, Loc)),
5302 Declarations => Decls,
5303 Handled_Statement_Sequence =>
5304 Make_Handled_Sequence_Of_Statements (Loc,
5305 Statements => Stmts));
5306
5307 return Subp_Body;
5308 end Build_Variant_Record_Equality;
5309
5310 -----------------------------
5311 -- Check_Stream_Attributes --
5312 -----------------------------
5313
5314 procedure Check_Stream_Attributes (Typ : Entity_Id) is
5315 Comp : Entity_Id;
5316 Par_Read : constant Boolean :=
5317 Stream_Attribute_Available (Typ, TSS_Stream_Read)
5318 and then not Has_Specified_Stream_Read (Typ);
5319 Par_Write : constant Boolean :=
5320 Stream_Attribute_Available (Typ, TSS_Stream_Write)
5321 and then not Has_Specified_Stream_Write (Typ);
5322
5323 procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type);
5324 -- Check that Comp has a user-specified Nam stream attribute
5325
5326 ----------------
5327 -- Check_Attr --
5328 ----------------
5329
5330 procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type) is
5331 begin
5332 -- Move this check to sem???
5333
5334 if not Stream_Attribute_Available (Etype (Comp), TSS_Nam) then
5335 Error_Msg_Name_1 := Nam;
5336 Error_Msg_N
5337 ("|component& in limited extension must have% attribute", Comp);
5338 end if;
5339 end Check_Attr;
5340
5341 -- Start of processing for Check_Stream_Attributes
5342
5343 begin
5344 if Par_Read or else Par_Write then
5345 Comp := First_Component (Typ);
5346 while Present (Comp) loop
5347 if Comes_From_Source (Comp)
5348 and then Original_Record_Component (Comp) = Comp
5349 and then Is_Limited_Type (Etype (Comp))
5350 then
5351 if Par_Read then
5352 Check_Attr (Name_Read, TSS_Stream_Read);
5353 end if;
5354
5355 if Par_Write then
5356 Check_Attr (Name_Write, TSS_Stream_Write);
5357 end if;
5358 end if;
5359
5360 Next_Component (Comp);
5361 end loop;
5362 end if;
5363 end Check_Stream_Attributes;
5364
5365 ----------------------
5366 -- Clean_Task_Names --
5367 ----------------------
5368
5369 procedure Clean_Task_Names
5370 (Typ : Entity_Id;
5371 Proc_Id : Entity_Id)
5372 is
5373 begin
5374 if Has_Task (Typ)
5375 and then not Restriction_Active (No_Implicit_Heap_Allocations)
5376 and then not Global_Discard_Names
5377 and then Tagged_Type_Expansion
5378 then
5379 Set_Uses_Sec_Stack (Proc_Id);
5380 end if;
5381 end Clean_Task_Names;
5382
5383 -------------------------------
5384 -- Copy_Discr_Checking_Funcs --
5385 -------------------------------
5386
5387 procedure Copy_Discr_Checking_Funcs (N : Node_Id) is
5388 Typ : constant Entity_Id := Defining_Identifier (N);
5389 Comp : Entity_Id := First_Component (Typ);
5390 Old_Comp : Entity_Id := First_Component
5391 (Base_Type (Underlying_Type (Etype (Typ))));
5392 begin
5393 while Present (Comp) loop
5394 if Chars (Comp) = Chars (Old_Comp) then
5395 Set_Discriminant_Checking_Func
5396 (Comp, Discriminant_Checking_Func (Old_Comp));
5397 end if;
5398
5399 Next_Component (Old_Comp);
5400 Next_Component (Comp);
5401 end loop;
5402 end Copy_Discr_Checking_Funcs;
5403
5404 ------------------------------
5405 -- Expand_Freeze_Array_Type --
5406 ------------------------------
5407
5408 procedure Expand_Freeze_Array_Type (N : Node_Id) is
5409 Typ : constant Entity_Id := Entity (N);
5410 Base : constant Entity_Id := Base_Type (Typ);
5411
5412 -- Obtain the corresponding mutably tagged type if necessary
5413
5414 Comp_Typ : constant Entity_Id :=
5415 Get_Corresponding_Mutably_Tagged_Type_If_Present
5416 (Component_Type (Typ));
5417
5418 begin
5419 -- First, the nonpacked case
5420
5421 if not Is_Bit_Packed_Array (Typ) then
5422 if No (Init_Proc (Base)) then
5423
5424 -- If this is an anonymous array built for an object declaration
5425 -- with an initial value, its Init_Proc will never be called. The
5426 -- initial value itself may have been expanded into assignments,
5427 -- in which case the declaration has the No_Initialization flag.
5428
5429 if Is_Itype (Base)
5430 and then Nkind (Associated_Node_For_Itype (Base)) =
5431 N_Object_Declaration
5432 and then
5433 (Present (Expression (Associated_Node_For_Itype (Base)))
5434 or else No_Initialization (Associated_Node_For_Itype (Base)))
5435 then
5436 null;
5437
5438 -- We do not need an init proc for string or wide [wide] string,
5439 -- since the only time these need initialization in normalize or
5440 -- initialize scalars mode, and these types are treated specially
5441 -- and do not need initialization procedures.
5442
5443 elsif Is_Standard_String_Type (Base)
5444 and then not Has_Default_Aspect (Base)
5445 then
5446 null;
5447
5448 -- Otherwise we have to build an init proc for the subtype
5449
5450 else
5451 Build_Array_Init_Proc (Base, N);
5452 end if;
5453 end if;
5454
5455 if Typ = Base and then Has_Controlled_Component (Base) then
5456 Build_Controlling_Procs (Base);
5457
5458 if not Is_Limited_Type (Comp_Typ)
5459 and then Number_Dimensions (Typ) = 1
5460 then
5461 Build_Slice_Assignment (Typ);
5462 end if;
5463 end if;
5464
5465 -- For the packed case, no initialization, except if the component type
5466 -- has an initialization procedure, or Initialize/Normalize_Scalars is
5467 -- active, or there is a Default_Component_Value aspect, or the type is
5468 -- public, because a client might specify Initialize_Scalars and there
5469 -- better be a public Init_Proc for it.
5470
5471 elsif Present (Init_Proc (Component_Type (Base)))
5472 or else Init_Or_Norm_Scalars
5473 or else Has_Default_Aspect (Base)
5474 or else Is_Public (Typ)
5475 then
5476 if No (Init_Proc (Base)) then
5477 Build_Array_Init_Proc (Base, N);
5478 end if;
5479 end if;
5480 end Expand_Freeze_Array_Type;
5481
5482 -----------------------------------
5483 -- Expand_Freeze_Class_Wide_Type --
5484 -----------------------------------
5485
5486 procedure Expand_Freeze_Class_Wide_Type (N : Node_Id) is
5487 function Is_C_Derivation (Typ : Entity_Id) return Boolean;
5488 -- Given a type, determine whether it is derived from a C or C++ root
5489
5490 ---------------------
5491 -- Is_C_Derivation --
5492 ---------------------
5493
5494 function Is_C_Derivation (Typ : Entity_Id) return Boolean is
5495 T : Entity_Id;
5496
5497 begin
5498 T := Typ;
5499 loop
5500 if Is_CPP_Class (T)
5501 or else Convention (T) = Convention_C
5502 or else Convention (T) = Convention_CPP
5503 then
5504 return True;
5505 end if;
5506
5507 exit when T = Etype (T);
5508
5509 T := Etype (T);
5510 end loop;
5511
5512 return False;
5513 end Is_C_Derivation;
5514
5515 -- Local variables
5516
5517 Typ : constant Entity_Id := Entity (N);
5518 Root : constant Entity_Id := Root_Type (Typ);
5519
5520 -- Start of processing for Expand_Freeze_Class_Wide_Type
5521
5522 begin
5523 -- Certain run-time configurations and targets do not provide support
5524 -- for controlled types.
5525
5526 if Restriction_Active (No_Finalization) then
5527 return;
5528
5529 -- Do not create TSS routine Finalize_Address when dispatching calls are
5530 -- disabled since the core of the routine is a dispatching call.
5531
5532 elsif Restriction_Active (No_Dispatching_Calls) then
5533 return;
5534
5535 -- Do not create TSS routine Finalize_Address for concurrent class-wide
5536 -- types. Ignore C, C++, CIL and Java types since it is assumed that the
5537 -- non-Ada side will handle their destruction.
5538 --
5539 -- Concurrent Ada types are functionally represented by an associated
5540 -- "corresponding record type" (typenameV), which owns the actual TSS
5541 -- finalize bodies for the type (and technically class-wide type).
5542
5543 elsif Is_Concurrent_Type (Root)
5544 or else Is_C_Derivation (Root)
5545 or else Convention (Typ) = Convention_CPP
5546 then
5547 return;
5548
5549 -- Do not create TSS routine Finalize_Address when compiling in CodePeer
5550 -- mode since the routine contains an Unchecked_Conversion.
5551
5552 elsif CodePeer_Mode then
5553 return;
5554 end if;
5555
5556 -- Create the body of TSS primitive Finalize_Address. This automatically
5557 -- sets the TSS entry for the class-wide type.
5558
5559 if No (Finalize_Address (Typ)) then
5560 Make_Finalize_Address_Body (Typ);
5561 end if;
5562 end Expand_Freeze_Class_Wide_Type;
5563
5564 ------------------------------------
5565 -- Expand_Freeze_Enumeration_Type --
5566 ------------------------------------
5567
5568 procedure Expand_Freeze_Enumeration_Type (N : Node_Id) is
5569 Typ : constant Entity_Id := Entity (N);
5570 Loc : constant Source_Ptr := Sloc (Typ);
5571
5572 Arr : Entity_Id;
5573 Ent : Entity_Id;
5574 Fent : Entity_Id;
5575 Is_Contiguous : Boolean;
5576 Index_Typ : Entity_Id;
5577 Ityp : Entity_Id;
5578 Last_Repval : Uint;
5579 Lst : List_Id;
5580 Num : Nat;
5581 Pos_Expr : Node_Id;
5582
5583 Func : Entity_Id;
5584 pragma Warnings (Off, Func);
5585
5586 begin
5587 -- Various optimizations possible if given representation is contiguous
5588
5589 Is_Contiguous := True;
5590
5591 Ent := First_Literal (Typ);
5592 Last_Repval := Enumeration_Rep (Ent);
5593 Num := 1;
5594 Next_Literal (Ent);
5595
5596 while Present (Ent) loop
5597 if Enumeration_Rep (Ent) - Last_Repval /= 1 then
5598 Is_Contiguous := False;
5599 else
5600 Last_Repval := Enumeration_Rep (Ent);
5601 end if;
5602
5603 Num := Num + 1;
5604 Next_Literal (Ent);
5605 end loop;
5606
5607 if Is_Contiguous then
5608 Set_Has_Contiguous_Rep (Typ);
5609
5610 -- Now build a subtype declaration
5611
5612 -- subtype typI is new Natural range 0 .. num - 1
5613
5614 Index_Typ :=
5615 Make_Defining_Identifier (Loc,
5616 Chars => New_External_Name (Chars (Typ), 'I'));
5617
5618 Append_Freeze_Action (Typ,
5619 Make_Subtype_Declaration (Loc,
5620 Defining_Identifier => Index_Typ,
5621 Subtype_Indication =>
5622 Make_Subtype_Indication (Loc,
5623 Subtype_Mark =>
5624 New_Occurrence_Of (Standard_Natural, Loc),
5625 Constraint =>
5626 Make_Range_Constraint (Loc,
5627 Range_Expression =>
5628 Make_Range (Loc,
5629 Low_Bound =>
5630 Make_Integer_Literal (Loc, 0),
5631 High_Bound =>
5632 Make_Integer_Literal (Loc, Num - 1))))));
5633
5634 Set_Enum_Pos_To_Rep (Typ, Index_Typ);
5635
5636 else
5637 -- Build list of literal references
5638
5639 Lst := New_List;
5640 Ent := First_Literal (Typ);
5641 while Present (Ent) loop
5642 Append_To (Lst, New_Occurrence_Of (Ent, Sloc (Ent)));
5643 Next_Literal (Ent);
5644 end loop;
5645
5646 -- Now build an array declaration
5647
5648 -- typA : constant array (Natural range 0 .. num - 1) of typ :=
5649 -- (v, v, v, v, v, ....)
5650
5651 Arr :=
5652 Make_Defining_Identifier (Loc,
5653 Chars => New_External_Name (Chars (Typ), 'A'));
5654
5655 Append_Freeze_Action (Typ,
5656 Make_Object_Declaration (Loc,
5657 Defining_Identifier => Arr,
5658 Constant_Present => True,
5659
5660 Object_Definition =>
5661 Make_Constrained_Array_Definition (Loc,
5662 Discrete_Subtype_Definitions => New_List (
5663 Make_Subtype_Indication (Loc,
5664 Subtype_Mark =>
5665 New_Occurrence_Of (Standard_Natural, Loc),
5666 Constraint =>
5667 Make_Range_Constraint (Loc,
5668 Range_Expression =>
5669 Make_Range (Loc,
5670 Low_Bound =>
5671 Make_Integer_Literal (Loc, 0),
5672 High_Bound =>
5673 Make_Integer_Literal (Loc, Num - 1))))),
5674
5675 Component_Definition =>
5676 Make_Component_Definition (Loc,
5677 Subtype_Indication => New_Occurrence_Of (Typ, Loc))),
5678
5679 Expression =>
5680 Make_Aggregate (Loc,
5681 Expressions => Lst)));
5682
5683 Set_Enum_Pos_To_Rep (Typ, Arr);
5684 end if;
5685
5686 -- Now we build the function that converts representation values to
5687 -- position values. This function has the form:
5688
5689 -- function _Rep_To_Pos (A : etype; F : Boolean) return Integer is
5690 -- begin
5691 -- case ityp!(A) is
5692 -- when enum-lit'Enum_Rep => return posval;
5693 -- when enum-lit'Enum_Rep => return posval;
5694 -- ...
5695 -- when others =>
5696 -- [raise Constraint_Error when F "invalid data"]
5697 -- return -1;
5698 -- end case;
5699 -- end;
5700
5701 -- Note: the F parameter determines whether the others case (no valid
5702 -- representation) raises Constraint_Error or returns a unique value
5703 -- of minus one. The latter case is used, e.g. in 'Valid code.
5704
5705 -- Note: the reason we use Enum_Rep values in the case here is to avoid
5706 -- the code generator making inappropriate assumptions about the range
5707 -- of the values in the case where the value is invalid. ityp is a
5708 -- signed or unsigned integer type of appropriate width.
5709
5710 -- Note: if exceptions are not supported, then we suppress the raise
5711 -- and return -1 unconditionally (this is an erroneous program in any
5712 -- case and there is no obligation to raise Constraint_Error here). We
5713 -- also do this if pragma Restrictions (No_Exceptions) is active.
5714
5715 -- Is this right??? What about No_Exception_Propagation???
5716
5717 -- The underlying type is signed. Reset the Is_Unsigned_Type explicitly
5718 -- because it might have been inherited from the parent type.
5719
5720 if Enumeration_Rep (First_Literal (Typ)) < 0 then
5721 Set_Is_Unsigned_Type (Typ, False);
5722 end if;
5723
5724 Ityp := Integer_Type_For (Esize (Typ), Is_Unsigned_Type (Typ));
5725
5726 -- The body of the function is a case statement. First collect case
5727 -- alternatives, or optimize the contiguous case.
5728
5729 Lst := New_List;
5730
5731 -- If representation is contiguous, Pos is computed by subtracting
5732 -- the representation of the first literal.
5733
5734 if Is_Contiguous then
5735 Ent := First_Literal (Typ);
5736
5737 if Enumeration_Rep (Ent) = Last_Repval then
5738
5739 -- Another special case: for a single literal, Pos is zero
5740
5741 Pos_Expr := Make_Integer_Literal (Loc, Uint_0);
5742
5743 else
5744 Pos_Expr :=
5745 Convert_To (Standard_Integer,
5746 Make_Op_Subtract (Loc,
5747 Left_Opnd =>
5748 Unchecked_Convert_To
5749 (Ityp, Make_Identifier (Loc, Name_uA)),
5750 Right_Opnd =>
5751 Make_Integer_Literal (Loc,
5752 Intval => Enumeration_Rep (First_Literal (Typ)))));
5753 end if;
5754
5755 Append_To (Lst,
5756 Make_Case_Statement_Alternative (Loc,
5757 Discrete_Choices => New_List (
5758 Make_Range (Sloc (Enumeration_Rep_Expr (Ent)),
5759 Low_Bound =>
5760 Make_Integer_Literal (Loc,
5761 Intval => Enumeration_Rep (Ent)),
5762 High_Bound =>
5763 Make_Integer_Literal (Loc, Intval => Last_Repval))),
5764
5765 Statements => New_List (
5766 Make_Simple_Return_Statement (Loc,
5767 Expression => Pos_Expr))));
5768
5769 else
5770 Ent := First_Literal (Typ);
5771 while Present (Ent) loop
5772 Append_To (Lst,
5773 Make_Case_Statement_Alternative (Loc,
5774 Discrete_Choices => New_List (
5775 Make_Integer_Literal (Sloc (Enumeration_Rep_Expr (Ent)),
5776 Intval => Enumeration_Rep (Ent))),
5777
5778 Statements => New_List (
5779 Make_Simple_Return_Statement (Loc,
5780 Expression =>
5781 Make_Integer_Literal (Loc,
5782 Intval => Enumeration_Pos (Ent))))));
5783
5784 Next_Literal (Ent);
5785 end loop;
5786 end if;
5787
5788 -- In normal mode, add the others clause with the test.
5789 -- If Predicates_Ignored is True, validity checks do not apply to
5790 -- the subtype.
5791
5792 if not No_Exception_Handlers_Set
5793 and then not Predicates_Ignored (Typ)
5794 then
5795 Append_To (Lst,
5796 Make_Case_Statement_Alternative (Loc,
5797 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
5798 Statements => New_List (
5799 Make_Raise_Constraint_Error (Loc,
5800 Condition => Make_Identifier (Loc, Name_uF),
5801 Reason => CE_Invalid_Data),
5802 Make_Simple_Return_Statement (Loc,
5803 Expression => Make_Integer_Literal (Loc, -1)))));
5804
5805 -- If either of the restrictions No_Exceptions_Handlers/Propagation is
5806 -- active then return -1 (we cannot usefully raise Constraint_Error in
5807 -- this case). See description above for further details.
5808
5809 else
5810 Append_To (Lst,
5811 Make_Case_Statement_Alternative (Loc,
5812 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
5813 Statements => New_List (
5814 Make_Simple_Return_Statement (Loc,
5815 Expression => Make_Integer_Literal (Loc, -1)))));
5816 end if;
5817
5818 -- Now we can build the function body
5819
5820 Fent :=
5821 Make_Defining_Identifier (Loc, Make_TSS_Name (Typ, TSS_Rep_To_Pos));
5822
5823 Func :=
5824 Make_Subprogram_Body (Loc,
5825 Specification =>
5826 Make_Function_Specification (Loc,
5827 Defining_Unit_Name => Fent,
5828 Parameter_Specifications => New_List (
5829 Make_Parameter_Specification (Loc,
5830 Defining_Identifier =>
5831 Make_Defining_Identifier (Loc, Name_uA),
5832 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
5833 Make_Parameter_Specification (Loc,
5834 Defining_Identifier =>
5835 Make_Defining_Identifier (Loc, Name_uF),
5836 Parameter_Type =>
5837 New_Occurrence_Of (Standard_Boolean, Loc))),
5838
5839 Result_Definition => New_Occurrence_Of (Standard_Integer, Loc)),
5840
5841 Declarations => Empty_List,
5842
5843 Handled_Statement_Sequence =>
5844 Make_Handled_Sequence_Of_Statements (Loc,
5845 Statements => New_List (
5846 Make_Case_Statement (Loc,
5847 Expression =>
5848 Unchecked_Convert_To
5849 (Ityp, Make_Identifier (Loc, Name_uA)),
5850 Alternatives => Lst))));
5851
5852 Set_TSS (Typ, Fent);
5853
5854 -- Set Pure flag (it will be reset if the current context is not Pure).
5855 -- We also pretend there was a pragma Pure_Function so that for purposes
5856 -- of optimization and constant-folding, we will consider the function
5857 -- Pure even if we are not in a Pure context).
5858
5859 Set_Is_Pure (Fent);
5860 Set_Has_Pragma_Pure_Function (Fent);
5861
5862 -- Unless we are in -gnatD mode, where we are debugging generated code,
5863 -- this is an internal entity for which we don't need debug info.
5864
5865 if not Debug_Generated_Code then
5866 Set_Debug_Info_Off (Fent);
5867 end if;
5868
5869 Set_Is_Inlined (Fent);
5870
5871 exception
5872 when RE_Not_Available =>
5873 return;
5874 end Expand_Freeze_Enumeration_Type;
5875
5876 -------------------------------
5877 -- Expand_Freeze_Record_Type --
5878 -------------------------------
5879
5880 procedure Expand_Freeze_Record_Type (N : Node_Id) is
5881
5882 procedure Build_Class_Condition_Subprograms (Typ : Entity_Id);
5883 -- Create internal subprograms of Typ primitives that have class-wide
5884 -- preconditions or postconditions; they are invoked by the caller to
5885 -- evaluate the conditions.
5886
5887 procedure Build_Variant_Record_Equality (Typ : Entity_Id);
5888 -- Create an equality function for the untagged variant record Typ and
5889 -- attach it to the TSS list.
5890
5891 procedure Register_Dispatch_Table_Wrappers (Typ : Entity_Id);
5892 -- Register dispatch-table wrappers in the dispatch table of Typ
5893
5894 procedure Validate_Tagged_Type_Extra_Formals (Typ : Entity_Id);
5895 -- Check extra formals of dispatching primitives of tagged type Typ.
5896 -- Used in pragma Debug.
5897
5898 ---------------------------------------
5899 -- Build_Class_Condition_Subprograms --
5900 ---------------------------------------
5901
5902 procedure Build_Class_Condition_Subprograms (Typ : Entity_Id) is
5903 Prim_List : constant Elist_Id := Primitive_Operations (Typ);
5904 Prim_Elmt : Elmt_Id := First_Elmt (Prim_List);
5905 Prim : Entity_Id;
5906
5907 begin
5908 while Present (Prim_Elmt) loop
5909 Prim := Node (Prim_Elmt);
5910
5911 -- Primitive with class-wide preconditions
5912
5913 if Comes_From_Source (Prim)
5914 and then Has_Significant_Contract (Prim)
5915 and then
5916 (Present (Class_Preconditions (Prim))
5917 or else Present (Ignored_Class_Preconditions (Prim)))
5918 then
5919 if Expander_Active then
5920 Make_Class_Precondition_Subps (Prim);
5921 end if;
5922
5923 -- Wrapper of a primitive that has or inherits class-wide
5924 -- preconditions.
5925
5926 elsif Is_Primitive_Wrapper (Prim)
5927 and then
5928 (Present (Nearest_Class_Condition_Subprogram
5929 (Spec_Id => Prim,
5930 Kind => Class_Precondition))
5931 or else
5932 Present (Nearest_Class_Condition_Subprogram
5933 (Spec_Id => Prim,
5934 Kind => Ignored_Class_Precondition)))
5935 then
5936 if Expander_Active then
5937 Make_Class_Precondition_Subps (Prim);
5938 end if;
5939 end if;
5940
5941 Next_Elmt (Prim_Elmt);
5942 end loop;
5943 end Build_Class_Condition_Subprograms;
5944
5945 -----------------------------------
5946 -- Build_Variant_Record_Equality --
5947 -----------------------------------
5948
5949 procedure Build_Variant_Record_Equality (Typ : Entity_Id) is
5950 Loc : constant Source_Ptr := Sloc (Typ);
5951 F : constant Entity_Id :=
5952 Make_Defining_Identifier (Loc,
5953 Chars => Make_TSS_Name (Typ, TSS_Composite_Equality));
5954 begin
5955 -- For a variant record with restriction No_Implicit_Conditionals
5956 -- in effect we skip building the procedure. This is safe because
5957 -- if we can see the restriction, so can any caller, and calls to
5958 -- equality test routines are not allowed for variant records if
5959 -- this restriction is active.
5960
5961 if Restriction_Active (No_Implicit_Conditionals) then
5962 return;
5963 end if;
5964
5965 -- Derived Unchecked_Union types no longer inherit the equality
5966 -- function of their parent.
5967
5968 if Is_Derived_Type (Typ)
5969 and then not Is_Unchecked_Union (Typ)
5970 and then not Has_New_Non_Standard_Rep (Typ)
5971 then
5972 declare
5973 Parent_Eq : constant Entity_Id :=
5974 TSS (Root_Type (Typ), TSS_Composite_Equality);
5975 begin
5976 if Present (Parent_Eq) then
5977 Copy_TSS (Parent_Eq, Typ);
5978 return;
5979 end if;
5980 end;
5981 end if;
5982
5983 Discard_Node (
5984 Build_Variant_Record_Equality
5985 (Typ => Typ,
5986 Spec_Id => Empty,
5987 Body_Id => F,
5988 Param_Specs => New_List (
5989 Make_Parameter_Specification (Loc,
5990 Defining_Identifier =>
5991 Make_Defining_Identifier (Loc, Name_X),
5992 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
5993
5994 Make_Parameter_Specification (Loc,
5995 Defining_Identifier =>
5996 Make_Defining_Identifier (Loc, Name_Y),
5997 Parameter_Type => New_Occurrence_Of (Typ, Loc)))));
5998
5999 Set_TSS (Typ, F);
6000 Set_Is_Pure (F);
6001
6002 if not Debug_Generated_Code then
6003 Set_Debug_Info_Off (F);
6004 end if;
6005 end Build_Variant_Record_Equality;
6006
6007 --------------------------------------
6008 -- Register_Dispatch_Table_Wrappers --
6009 --------------------------------------
6010
6011 procedure Register_Dispatch_Table_Wrappers (Typ : Entity_Id) is
6012 Elmt : Elmt_Id := First_Elmt (Primitive_Operations (Typ));
6013 Subp : Entity_Id;
6014
6015 begin
6016 while Present (Elmt) loop
6017 Subp := Node (Elmt);
6018
6019 if Is_Dispatch_Table_Wrapper (Subp) then
6020 Append_Freeze_Actions (Typ,
6021 Register_Primitive (Sloc (Subp), Subp));
6022 end if;
6023
6024 Next_Elmt (Elmt);
6025 end loop;
6026 end Register_Dispatch_Table_Wrappers;
6027
6028 ----------------------------------------
6029 -- Validate_Tagged_Type_Extra_Formals --
6030 ----------------------------------------
6031
6032 procedure Validate_Tagged_Type_Extra_Formals (Typ : Entity_Id) is
6033 Ovr_Subp : Entity_Id;
6034 Elmt : Elmt_Id;
6035 Subp : Entity_Id;
6036
6037 begin
6038 pragma Assert (not Is_Class_Wide_Type (Typ));
6039
6040 -- No check required if expansion is not active since we never
6041 -- generate extra formals in such case.
6042
6043 if not Expander_Active then
6044 return;
6045 end if;
6046
6047 Elmt := First_Elmt (Primitive_Operations (Typ));
6048 while Present (Elmt) loop
6049 Subp := Node (Elmt);
6050
6051 -- Extra formals of a dispatching primitive must match:
6052
6053 -- 1) The extra formals of its covered interface primitive
6054
6055 if Present (Interface_Alias (Subp)) then
6056 pragma Assert
6057 (Extra_Formals_Match_OK
6058 (E => Interface_Alias (Subp),
6059 Ref_E => Alias (Subp)));
6060 end if;
6061
6062 -- 2) The extra formals of its renamed primitive
6063
6064 if Present (Alias (Subp)) then
6065 pragma Assert
6066 (Extra_Formals_Match_OK
6067 (E => Subp,
6068 Ref_E => Ultimate_Alias (Subp)));
6069 end if;
6070
6071 -- 3) The extra formals of its overridden primitive
6072
6073 if Present (Overridden_Operation (Subp)) then
6074 Ovr_Subp := Overridden_Operation (Subp);
6075
6076 -- Handle controlling function wrapper
6077
6078 if Is_Wrapper (Subp)
6079 and then Ultimate_Alias (Ovr_Subp) = Subp
6080 then
6081 if Present (Overridden_Operation (Ovr_Subp)) then
6082 pragma Assert
6083 (Extra_Formals_Match_OK
6084 (E => Subp,
6085 Ref_E => Overridden_Operation (Ovr_Subp)));
6086 end if;
6087
6088 else
6089 pragma Assert
6090 (Extra_Formals_Match_OK
6091 (E => Subp,
6092 Ref_E => Ovr_Subp));
6093 end if;
6094 end if;
6095
6096 Next_Elmt (Elmt);
6097 end loop;
6098 end Validate_Tagged_Type_Extra_Formals;
6099
6100 -- Local variables
6101
6102 Typ : constant Node_Id := Entity (N);
6103 Typ_Decl : constant Node_Id := Parent (Typ);
6104
6105 Predef_List : List_Id;
6106
6107 Wrapper_Decl_List : List_Id;
6108 Wrapper_Body_List : List_Id := No_List;
6109
6110 Renamed_Eq : Node_Id := Empty;
6111 -- Defining unit name for the predefined equality function in the case
6112 -- where the type has a primitive operation that is a renaming of
6113 -- predefined equality (but only if there is also an overriding
6114 -- user-defined equality function). Used to pass this entity from
6115 -- Make_Predefined_Primitive_Specs to Predefined_Primitive_Bodies.
6116
6117 -- Start of processing for Expand_Freeze_Record_Type
6118
6119 begin
6120 -- Build discriminant checking functions if not a derived type (for
6121 -- derived types that are not tagged types, always use the discriminant
6122 -- checking functions of the parent type). However, for untagged types
6123 -- the derivation may have taken place before the parent was frozen, so
6124 -- we copy explicitly the discriminant checking functions from the
6125 -- parent into the components of the derived type.
6126
6127 Build_Or_Copy_Discr_Checking_Funcs (Typ_Decl);
6128
6129 if Is_Derived_Type (Typ)
6130 and then Is_Limited_Type (Typ)
6131 and then Is_Tagged_Type (Typ)
6132 then
6133 Check_Stream_Attributes (Typ);
6134 end if;
6135
6136 -- Handle constructors of untagged CPP_Class types
6137
6138 if not Is_Tagged_Type (Typ) and then Is_CPP_Class (Typ) then
6139 Set_CPP_Constructors (Typ);
6140 end if;
6141
6142 -- Creation of the Dispatch Table. Note that a Dispatch Table is built
6143 -- for regular tagged types as well as for Ada types deriving from a C++
6144 -- Class, but not for tagged types directly corresponding to C++ classes
6145 -- In the later case we assume that it is created in the C++ side and we
6146 -- just use it.
6147
6148 if Is_Tagged_Type (Typ) then
6149
6150 -- Add the _Tag component
6151
6152 if Underlying_Type (Etype (Typ)) = Typ then
6153 Expand_Tagged_Root (Typ);
6154 end if;
6155
6156 if Is_CPP_Class (Typ) then
6157 Set_All_DT_Position (Typ);
6158
6159 -- Create the tag entities with a minimum decoration
6160
6161 if Tagged_Type_Expansion then
6162 Append_Freeze_Actions (Typ, Make_Tags (Typ));
6163 end if;
6164
6165 Set_CPP_Constructors (Typ);
6166
6167 else
6168 if not Building_Static_DT (Typ) then
6169
6170 -- Usually inherited primitives are not delayed but the first
6171 -- Ada extension of a CPP_Class is an exception since the
6172 -- address of the inherited subprogram has to be inserted in
6173 -- the new Ada Dispatch Table and this is a freezing action.
6174
6175 -- Similarly, if this is an inherited operation whose parent is
6176 -- not frozen yet, it is not in the DT of the parent, and we
6177 -- generate an explicit freeze node for the inherited operation
6178 -- so it is properly inserted in the DT of the current type.
6179
6180 declare
6181 Elmt : Elmt_Id;
6182 Subp : Entity_Id;
6183
6184 begin
6185 Elmt := First_Elmt (Primitive_Operations (Typ));
6186 while Present (Elmt) loop
6187 Subp := Node (Elmt);
6188
6189 if Present (Alias (Subp)) then
6190 if Is_CPP_Class (Etype (Typ)) then
6191 Set_Has_Delayed_Freeze (Subp);
6192
6193 elsif Has_Delayed_Freeze (Alias (Subp))
6194 and then not Is_Frozen (Alias (Subp))
6195 then
6196 Set_Is_Frozen (Subp, False);
6197 Set_Has_Delayed_Freeze (Subp);
6198 end if;
6199 end if;
6200
6201 Next_Elmt (Elmt);
6202 end loop;
6203 end;
6204 end if;
6205
6206 -- Unfreeze momentarily the type to add the predefined primitives
6207 -- operations. The reason we unfreeze is so that these predefined
6208 -- operations will indeed end up as primitive operations (which
6209 -- must be before the freeze point).
6210
6211 Set_Is_Frozen (Typ, False);
6212
6213 -- Do not add the spec of predefined primitives in case of
6214 -- CPP tagged type derivations that have convention CPP.
6215
6216 if Is_CPP_Class (Root_Type (Typ))
6217 and then Convention (Typ) = Convention_CPP
6218 then
6219 null;
6220
6221 -- Do not add the spec of the predefined primitives if we are
6222 -- compiling under restriction No_Dispatching_Calls.
6223
6224 elsif not Restriction_Active (No_Dispatching_Calls) then
6225 Make_Predefined_Primitive_Specs (Typ, Predef_List, Renamed_Eq);
6226 Insert_List_Before_And_Analyze (N, Predef_List);
6227 end if;
6228
6229 -- Ada 2005 (AI-391): For a nonabstract null extension, create
6230 -- wrapper functions for each nonoverridden inherited function
6231 -- with a controlling result of the type. The wrapper for such
6232 -- a function returns an extension aggregate that invokes the
6233 -- parent function.
6234
6235 if Ada_Version >= Ada_2005
6236 and then not Is_Abstract_Type (Typ)
6237 and then Is_Null_Extension (Typ)
6238 then
6239 Make_Controlling_Function_Wrappers
6240 (Typ, Wrapper_Decl_List, Wrapper_Body_List);
6241 Insert_List_Before_And_Analyze (N, Wrapper_Decl_List);
6242 end if;
6243
6244 -- Ada 2005 (AI-251): For a nonabstract type extension, build
6245 -- null procedure declarations for each set of homographic null
6246 -- procedures that are inherited from interface types but not
6247 -- overridden. This is done to ensure that the dispatch table
6248 -- entry associated with such null primitives are properly filled.
6249
6250 if Ada_Version >= Ada_2005
6251 and then Etype (Typ) /= Typ
6252 and then not Is_Abstract_Type (Typ)
6253 and then Has_Interfaces (Typ)
6254 then
6255 Insert_Actions (N, Make_Null_Procedure_Specs (Typ));
6256 end if;
6257
6258 Set_Is_Frozen (Typ);
6259
6260 if not Is_Derived_Type (Typ)
6261 or else Is_Tagged_Type (Etype (Typ))
6262 then
6263 Set_All_DT_Position (Typ);
6264
6265 -- If this is a type derived from an untagged private type whose
6266 -- full view is tagged, the type is marked tagged for layout
6267 -- reasons, but it has no dispatch table.
6268
6269 elsif Is_Derived_Type (Typ)
6270 and then Is_Private_Type (Etype (Typ))
6271 and then not Is_Tagged_Type (Etype (Typ))
6272 then
6273 return;
6274 end if;
6275
6276 -- Create and decorate the tags. Suppress their creation when
6277 -- not Tagged_Type_Expansion because the dispatching mechanism is
6278 -- handled internally by the virtual target.
6279
6280 if Tagged_Type_Expansion then
6281 Append_Freeze_Actions (Typ, Make_Tags (Typ));
6282
6283 -- Generate dispatch table of locally defined tagged type.
6284 -- Dispatch tables of library level tagged types are built
6285 -- later (see Build_Static_Dispatch_Tables).
6286
6287 if not Building_Static_DT (Typ) then
6288 Append_Freeze_Actions (Typ, Make_DT (Typ));
6289
6290 -- Register dispatch table wrappers in the dispatch table.
6291 -- It could not be done when these wrappers were built
6292 -- because, at that stage, the dispatch table was not
6293 -- available.
6294
6295 Register_Dispatch_Table_Wrappers (Typ);
6296 end if;
6297 end if;
6298
6299 -- If the type has unknown discriminants, propagate dispatching
6300 -- information to its underlying record view, which does not get
6301 -- its own dispatch table.
6302
6303 if Is_Derived_Type (Typ)
6304 and then Has_Unknown_Discriminants (Typ)
6305 and then Present (Underlying_Record_View (Typ))
6306 then
6307 declare
6308 Rep : constant Entity_Id := Underlying_Record_View (Typ);
6309 begin
6310 Set_Access_Disp_Table
6311 (Rep, Access_Disp_Table (Typ));
6312 Set_Dispatch_Table_Wrappers
6313 (Rep, Dispatch_Table_Wrappers (Typ));
6314 Set_Direct_Primitive_Operations
6315 (Rep, Direct_Primitive_Operations (Typ));
6316 end;
6317 end if;
6318
6319 -- Make sure that the primitives Initialize, Adjust and Finalize
6320 -- are Frozen before other TSS subprograms. We don't want them
6321 -- frozen inside.
6322
6323 if Is_Controlled (Typ) then
6324 declare
6325 Prim : Entity_Id;
6326
6327 begin
6328 Prim := Find_Controlled_Prim_Op (Typ, Name_Initialize);
6329 if Present (Prim) then
6330 Append_Freeze_Actions (Typ, Freeze_Entity (Prim, Typ));
6331 end if;
6332
6333 if not Is_Limited_Type (Typ) then
6334 Prim := Find_Controlled_Prim_Op (Typ, Name_Adjust);
6335 if Present (Prim) then
6336 Append_Freeze_Actions (Typ, Freeze_Entity (Prim, Typ));
6337 end if;
6338 end if;
6339
6340 Prim := Find_Controlled_Prim_Op (Typ, Name_Finalize);
6341 if Present (Prim) then
6342 Append_Freeze_Actions (Typ, Freeze_Entity (Prim, Typ));
6343 end if;
6344 end;
6345 end if;
6346
6347 -- Freeze rest of primitive operations. There is no need to handle
6348 -- the predefined primitives if we are compiling under restriction
6349 -- No_Dispatching_Calls.
6350
6351 if not Restriction_Active (No_Dispatching_Calls) then
6352 Append_Freeze_Actions (Typ, Predefined_Primitive_Freeze (Typ));
6353 end if;
6354 end if;
6355
6356 -- In the untagged case, ever since Ada 83 an equality function must
6357 -- be provided for variant records that are not unchecked unions.
6358
6359 elsif Has_Discriminants (Typ)
6360 and then not Is_Limited_Type (Typ)
6361 and then Present (Component_List (Type_Definition (Typ_Decl)))
6362 and then
6363 Present (Variant_Part (Component_List (Type_Definition (Typ_Decl))))
6364 then
6365 Build_Variant_Record_Equality (Typ);
6366
6367 -- In Ada 2012 the equality function composes, and thus must be built
6368 -- explicitly just as for tagged records.
6369
6370 -- This is done unconditionally to ensure that tools can be linked
6371 -- properly with user programs compiled with older language versions.
6372 -- In addition, this is needed because "=" composes for bounded strings
6373 -- in all language versions (see Exp_Ch4.Expand_Composite_Equality).
6374
6375 elsif Comes_From_Source (Typ)
6376 and then Convention (Typ) = Convention_Ada
6377 and then not Is_Limited_Type (Typ)
6378 then
6379 Build_Untagged_Record_Equality (Typ);
6380 end if;
6381
6382 -- Before building the record initialization procedure, if we are
6383 -- dealing with a concurrent record value type, then we must go through
6384 -- the discriminants, exchanging discriminals between the concurrent
6385 -- type and the concurrent record value type. See the section "Handling
6386 -- of Discriminants" in the Einfo spec for details.
6387
6388 if Is_Concurrent_Record_Type (Typ) and then Has_Discriminants (Typ) then
6389 declare
6390 Ctyp : constant Entity_Id :=
6391 Corresponding_Concurrent_Type (Typ);
6392 Conc_Discr : Entity_Id;
6393 Rec_Discr : Entity_Id;
6394 Temp : Entity_Id;
6395
6396 begin
6397 Conc_Discr := First_Discriminant (Ctyp);
6398 Rec_Discr := First_Discriminant (Typ);
6399 while Present (Conc_Discr) loop
6400 Temp := Discriminal (Conc_Discr);
6401 Set_Discriminal (Conc_Discr, Discriminal (Rec_Discr));
6402 Set_Discriminal (Rec_Discr, Temp);
6403
6404 Set_Discriminal_Link (Discriminal (Conc_Discr), Conc_Discr);
6405 Set_Discriminal_Link (Discriminal (Rec_Discr), Rec_Discr);
6406
6407 Next_Discriminant (Conc_Discr);
6408 Next_Discriminant (Rec_Discr);
6409 end loop;
6410 end;
6411 end if;
6412
6413 if Has_Controlled_Component (Typ) then
6414 Build_Controlling_Procs (Typ);
6415 end if;
6416
6417 Adjust_Discriminants (Typ);
6418
6419 -- Do not need init for interfaces on virtual targets since they're
6420 -- abstract.
6421
6422 if not Is_Mutably_Tagged_CW_Equivalent_Type (Typ)
6423 and then (Tagged_Type_Expansion or else not Is_Interface (Typ))
6424 then
6425 Build_Record_Init_Proc (Typ_Decl, Typ);
6426 end if;
6427
6428 -- Create the body of TSS primitive Finalize_Address. This must be done
6429 -- before the bodies of all predefined primitives are created. If Typ
6430 -- is limited, Stream_Input and Stream_Read may produce build-in-place
6431 -- allocations and for those the expander needs Finalize_Address.
6432
6433 if Is_Controlled (Typ) then
6434 Make_Finalize_Address_Body (Typ);
6435 end if;
6436
6437 -- For tagged type that are not interfaces, build bodies of primitive
6438 -- operations. Note: do this after building the record initialization
6439 -- procedure, since the primitive operations may need the initialization
6440 -- routine. There is no need to add predefined primitives of interfaces
6441 -- because all their predefined primitives are abstract.
6442
6443 if Is_Tagged_Type (Typ) and then not Is_Interface (Typ) then
6444
6445 -- Do not add the body of predefined primitives in case of CPP tagged
6446 -- type derivations that have convention CPP.
6447
6448 if Is_CPP_Class (Root_Type (Typ))
6449 and then Convention (Typ) = Convention_CPP
6450 then
6451 null;
6452
6453 -- Do not add the body of the predefined primitives if we are
6454 -- compiling under restriction No_Dispatching_Calls.
6455
6456 elsif not Restriction_Active (No_Dispatching_Calls) then
6457 -- Create the body of the class-wide type's TSS primitive
6458 -- Finalize_Address. This must be done before any class-wide
6459 -- precondition functions are created.
6460
6461 Make_Finalize_Address_Body (Class_Wide_Type (Typ));
6462
6463 Predef_List := Predefined_Primitive_Bodies (Typ, Renamed_Eq);
6464 Append_Freeze_Actions (Typ, Predef_List);
6465 end if;
6466
6467 -- Ada 2005 (AI-391): If any wrappers were created for nonoverridden
6468 -- inherited functions, then add their bodies to the freeze actions.
6469
6470 Append_Freeze_Actions (Typ, Wrapper_Body_List);
6471
6472 -- Create body of an interface type's class-wide type's TSS primitive
6473 -- Finalize_Address.
6474
6475 elsif Is_Tagged_Type (Typ)
6476 and then Is_Interface (Typ)
6477 and then not Restriction_Active (No_Dispatching_Calls)
6478 then
6479 Make_Finalize_Address_Body (Class_Wide_Type (Typ));
6480 end if;
6481
6482 -- Create extra formals for the primitive operations of the type.
6483 -- This must be done before analyzing the body of the initialization
6484 -- procedure, because a self-referential type might call one of these
6485 -- primitives in the body of the init_proc itself.
6486 --
6487 -- This is not needed:
6488 -- 1) If expansion is disabled, because extra formals are only added
6489 -- when we are generating code.
6490 --
6491 -- 2) For types with foreign convention since primitives with foreign
6492 -- convention don't have extra formals and AI95-117 requires that
6493 -- all primitives of a tagged type inherit the convention.
6494
6495 if Expander_Active
6496 and then Is_Tagged_Type (Typ)
6497 and then not Has_Foreign_Convention (Typ)
6498 then
6499 declare
6500 Elmt : Elmt_Id;
6501 E : Entity_Id;
6502
6503 begin
6504 -- Add extra formals to primitive operations
6505
6506 Elmt := First_Elmt (Primitive_Operations (Typ));
6507 while Present (Elmt) loop
6508 Create_Extra_Formals (Node (Elmt));
6509 Next_Elmt (Elmt);
6510 end loop;
6511
6512 -- Add extra formals to renamings of primitive operations. The
6513 -- addition of extra formals is done in two steps to minimize
6514 -- the compile time required for this action; the evaluation of
6515 -- Find_Dispatching_Type() and Contains() is only done here for
6516 -- renamings that are not primitive operations.
6517
6518 E := First_Entity (Scope (Typ));
6519 while Present (E) loop
6520 if Is_Dispatching_Operation (E)
6521 and then Present (Alias (E))
6522 and then Find_Dispatching_Type (E) = Typ
6523 and then not Contains (Primitive_Operations (Typ), E)
6524 then
6525 Create_Extra_Formals (E);
6526 end if;
6527
6528 Next_Entity (E);
6529 end loop;
6530
6531 pragma Debug (Validate_Tagged_Type_Extra_Formals (Typ));
6532 end;
6533 end if;
6534
6535 -- Build internal subprograms of primitives with class-wide
6536 -- pre/postconditions.
6537
6538 if Is_Tagged_Type (Typ) then
6539 Build_Class_Condition_Subprograms (Typ);
6540 end if;
6541 end Expand_Freeze_Record_Type;
6542
6543 ------------------------------------
6544 -- Expand_N_Full_Type_Declaration --
6545 ------------------------------------
6546
6547 procedure Expand_N_Full_Type_Declaration (N : Node_Id) is
6548 procedure Build_Master (Ptr_Typ : Entity_Id);
6549 -- Create the master associated with Ptr_Typ
6550
6551 ------------------
6552 -- Build_Master --
6553 ------------------
6554
6555 procedure Build_Master (Ptr_Typ : Entity_Id) is
6556 Desig_Typ : Entity_Id := Designated_Type (Ptr_Typ);
6557
6558 begin
6559 -- If the designated type is an incomplete view coming from a
6560 -- limited-with'ed package, we need to use the nonlimited view in
6561 -- case it has tasks.
6562
6563 if Is_Incomplete_Type (Desig_Typ)
6564 and then Present (Non_Limited_View (Desig_Typ))
6565 then
6566 Desig_Typ := Non_Limited_View (Desig_Typ);
6567 end if;
6568
6569 -- Anonymous access types are created for the components of the
6570 -- record parameter for an entry declaration. No master is created
6571 -- for such a type.
6572
6573 if Has_Task (Desig_Typ) then
6574 Build_Master_Entity (Ptr_Typ);
6575 Build_Master_Renaming (Ptr_Typ);
6576
6577 -- Create a class-wide master because a Master_Id must be generated
6578 -- for access-to-limited-class-wide types whose root may be extended
6579 -- with task components.
6580
6581 -- Note: This code covers access-to-limited-interfaces because they
6582 -- can be used to reference tasks implementing them.
6583
6584 -- Suppress the master creation for access types created for entry
6585 -- formal parameters (parameter block component types). Seems like
6586 -- suppression should be more general for compiler-generated types,
6587 -- but testing Comes_From_Source may be too general in this case
6588 -- (affects some test output)???
6589
6590 elsif not Is_Param_Block_Component_Type (Ptr_Typ)
6591 and then Is_Limited_Class_Wide_Type (Desig_Typ)
6592 then
6593 Build_Class_Wide_Master (Ptr_Typ);
6594 end if;
6595 end Build_Master;
6596
6597 -- Local declarations
6598
6599 Def_Id : constant Entity_Id := Defining_Identifier (N);
6600 B_Id : constant Entity_Id := Base_Type (Def_Id);
6601 FN : Node_Id;
6602 Par_Id : Entity_Id;
6603
6604 -- Start of processing for Expand_N_Full_Type_Declaration
6605
6606 begin
6607 if Is_Access_Type (Def_Id) then
6608 Build_Master (Def_Id);
6609
6610 if Ekind (Def_Id) = E_Access_Protected_Subprogram_Type then
6611 Expand_Access_Protected_Subprogram_Type (N);
6612 end if;
6613
6614 -- Array of anonymous access-to-task pointers
6615
6616 elsif Ada_Version >= Ada_2005
6617 and then Is_Array_Type (Def_Id)
6618 and then Is_Access_Type (Component_Type (Def_Id))
6619 and then Ekind (Component_Type (Def_Id)) = E_Anonymous_Access_Type
6620 then
6621 Build_Master (Component_Type (Def_Id));
6622
6623 elsif Has_Task (Def_Id) then
6624 Expand_Previous_Access_Type (Def_Id);
6625
6626 -- Check the components of a record type or array of records for
6627 -- anonymous access-to-task pointers.
6628
6629 elsif Ada_Version >= Ada_2005
6630 and then (Is_Record_Type (Def_Id)
6631 or else
6632 (Is_Array_Type (Def_Id)
6633 and then Is_Record_Type (Component_Type (Def_Id))))
6634 then
6635 declare
6636 Comp : Entity_Id;
6637 First : Boolean;
6638 M_Id : Entity_Id := Empty;
6639 Typ : Entity_Id;
6640
6641 begin
6642 if Is_Array_Type (Def_Id) then
6643 Comp := First_Entity (Component_Type (Def_Id));
6644 else
6645 Comp := First_Entity (Def_Id);
6646 end if;
6647
6648 -- Examine all components looking for anonymous access-to-task
6649 -- types.
6650
6651 First := True;
6652 while Present (Comp) loop
6653 Typ := Etype (Comp);
6654
6655 if Ekind (Typ) = E_Anonymous_Access_Type
6656 and then Might_Have_Tasks
6657 (Available_View (Designated_Type (Typ)))
6658 and then No (Master_Id (Typ))
6659 then
6660 -- Ensure that the record or array type have a _master
6661
6662 if First then
6663 Build_Master_Entity (Def_Id);
6664 Build_Master_Renaming (Typ);
6665 M_Id := Master_Id (Typ);
6666
6667 First := False;
6668
6669 -- Reuse the same master to service any additional types
6670
6671 else
6672 pragma Assert (Present (M_Id));
6673 Set_Master_Id (Typ, M_Id);
6674 end if;
6675 end if;
6676
6677 Next_Entity (Comp);
6678 end loop;
6679 end;
6680 end if;
6681
6682 -- Handle mutably tagged types by replacing their declarations with
6683 -- their class-wide equivalent types.
6684
6685 declare
6686 Comp : Entity_Id;
6687 begin
6688 if Is_Array_Type (Def_Id) then
6689 Comp := First_Entity (Component_Type (Def_Id));
6690 else
6691 Comp := First_Entity (Def_Id);
6692 end if;
6693
6694 while Present (Comp) loop
6695 if Ekind (Etype (Comp)) /= E_Void
6696 and then Is_Mutably_Tagged_Type (Etype (Comp))
6697 then
6698 Set_Etype
6699 (Comp, Class_Wide_Equivalent_Type (Etype (Comp)));
6700 end if;
6701 Next_Entity (Comp);
6702 end loop;
6703 end;
6704
6705 Par_Id := Etype (B_Id);
6706
6707 -- The parent type is private then we need to inherit any TSS operations
6708 -- from the full view.
6709
6710 if Is_Private_Type (Par_Id)
6711 and then Present (Full_View (Par_Id))
6712 then
6713 Par_Id := Base_Type (Full_View (Par_Id));
6714 end if;
6715
6716 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
6717 and then not Is_Tagged_Type (Def_Id)
6718 and then Present (Freeze_Node (Par_Id))
6719 and then Present (TSS_Elist (Freeze_Node (Par_Id)))
6720 then
6721 Ensure_Freeze_Node (B_Id);
6722 FN := Freeze_Node (B_Id);
6723
6724 if No (TSS_Elist (FN)) then
6725 Set_TSS_Elist (FN, New_Elmt_List);
6726 end if;
6727
6728 declare
6729 T_E : constant Elist_Id := TSS_Elist (FN);
6730 Elmt : Elmt_Id;
6731
6732 begin
6733 Elmt := First_Elmt (TSS_Elist (Freeze_Node (Par_Id)));
6734 while Present (Elmt) loop
6735 if Chars (Node (Elmt)) /= Name_uInit then
6736 Append_Elmt (Node (Elmt), T_E);
6737 end if;
6738
6739 Next_Elmt (Elmt);
6740 end loop;
6741
6742 -- If the derived type itself is private with a full view, then
6743 -- associate the full view with the inherited TSS_Elist as well.
6744
6745 if Is_Private_Type (B_Id)
6746 and then Present (Full_View (B_Id))
6747 then
6748 Ensure_Freeze_Node (Base_Type (Full_View (B_Id)));
6749 Set_TSS_Elist
6750 (Freeze_Node (Base_Type (Full_View (B_Id))), TSS_Elist (FN));
6751 end if;
6752 end;
6753 end if;
6754 end Expand_N_Full_Type_Declaration;
6755
6756 ---------------------------------
6757 -- Expand_N_Object_Declaration --
6758 ---------------------------------
6759
6760 procedure Expand_N_Object_Declaration (N : Node_Id) is
6761 Loc : constant Source_Ptr := Sloc (N);
6762 Def_Id : constant Entity_Id := Defining_Identifier (N);
6763 Expr : constant Node_Id := Expression (N);
6764 Obj_Def : constant Node_Id := Object_Definition (N);
6765 Typ : constant Entity_Id := Etype (Def_Id);
6766 Base_Typ : constant Entity_Id := Base_Type (Typ);
6767 Next_N : constant Node_Id := Next (N);
6768
6769 Special_Ret_Obj : constant Boolean := Is_Special_Return_Object (Def_Id);
6770 -- If this is a special return object, it will be allocated differently
6771 -- and ultimately rewritten as a renaming, so initialization activities
6772 -- need to be deferred until after that is done.
6773
6774 Func_Id : constant Entity_Id :=
6775 (if Special_Ret_Obj then Return_Applies_To (Scope (Def_Id)) else Empty);
6776 -- The function if this is a special return object, otherwise Empty
6777
6778 function Build_Heap_Or_Pool_Allocator
6779 (Temp_Id : Entity_Id;
6780 Temp_Typ : Entity_Id;
6781 Ret_Typ : Entity_Id;
6782 Alloc_Expr : Node_Id) return Node_Id;
6783 -- Create the statements necessary to allocate a return object on the
6784 -- heap or user-defined storage pool. The object may need finalization
6785 -- actions depending on the return type.
6786 --
6787 -- * Controlled case
6788 --
6789 -- if BIPcollection = null then
6790 -- Temp_Id := <Alloc_Expr>;
6791 -- else
6792 -- declare
6793 -- type Ptr_Typ is access Ret_Typ;
6794 -- for Ptr_Typ'Storage_Pool use BIPstoragepool.all;
6795 -- Local : Ptr_Typ;
6796 --
6797 -- begin
6798 -- procedure Allocate (...) is
6799 -- begin
6800 -- System.Storage_Pools.Subpools.Allocate_Any (...);
6801 -- end Allocate;
6802 --
6803 -- Local := <Alloc_Expr>;
6804 -- Temp_Id := Temp_Typ (Local);
6805 -- end;
6806 -- end if;
6807 --
6808 -- * Non-controlled case
6809 --
6810 -- Temp_Id := <Alloc_Expr>;
6811 --
6812 -- Temp_Id is the temporary which is used to reference the internally
6813 -- created object in all allocation forms. Temp_Typ is the type of the
6814 -- temporary. Func_Id is the enclosing function. Ret_Typ is the return
6815 -- type of Func_Id. Alloc_Expr is the actual allocator.
6816
6817 function BIP_Function_Call_Id return Entity_Id;
6818 -- If the object initialization expression is a call to a build-in-place
6819 -- function, return the id of the called function; otherwise return
6820 -- Empty.
6821
6822 procedure Count_Default_Sized_Task_Stacks
6823 (Typ : Entity_Id;
6824 Pri_Stacks : out Int;
6825 Sec_Stacks : out Int);
6826 -- Count the number of default-sized primary and secondary task stacks
6827 -- required for task objects contained within type Typ. If the number of
6828 -- task objects contained within the type is not known at compile time
6829 -- the procedure will return the stack counts of zero.
6830
6831 procedure Default_Initialize_Object (After : Node_Id);
6832 -- Generate all default initialization actions for object Def_Id. Any
6833 -- new code is inserted after node After.
6834
6835 procedure Initialize_Return_Object
6836 (Tag_Assign : Node_Id;
6837 Adj_Call : Node_Id;
6838 Expr : Node_Id;
6839 Init_Stmt : Node_Id;
6840 After : Node_Id);
6841 -- Generate all initialization actions for return object Def_Id. Any
6842 -- new code is inserted after node After.
6843
6844 function Is_Renamable_Function_Call (Expr : Node_Id) return Boolean;
6845 -- If we are not at library level and the object declaration originally
6846 -- appears in the form:
6847
6848 -- Obj : Typ := Func (...);
6849
6850 -- and has been rewritten as the dereference of a captured reference
6851 -- to the function result built either on the primary or the secondary
6852 -- stack, then the declaration can be rewritten as the renaming of this
6853 -- dereference:
6854
6855 -- type Ann is access all Typ;
6856 -- Rnn : constant Axx := Func (...)'reference;
6857 -- Obj : Typ renames Rnn.all;
6858
6859 -- This will avoid making an extra copy and, in the case where Typ needs
6860 -- finalization, a pair of calls to the Adjust and Finalize primitives,
6861 -- or Deep_Adjust and Deep_Finalize routines, depending on whether Typ
6862 -- has components that themselves need finalization.
6863
6864 -- However, in the case of a special return object, we need to make sure
6865 -- that the object Rnn is recognized by the Is_Related_To_Func_Return
6866 -- predicate; otherwise, if it is of a type that needs finalization,
6867 -- then Requires_Cleanup_Actions would return true because of this and
6868 -- Build_Finalizer would finalize it prematurely because of this (see
6869 -- also Expand_Simple_Function_Return for the same test in the case of
6870 -- a simple return).
6871
6872 -- Finally, in the case of a special return object, we also need to make
6873 -- sure that the two functions return on the same stack, otherwise we
6874 -- would create a dangling reference.
6875
6876 function Make_Allocator_For_Return (Expr : Node_Id) return Node_Id;
6877 -- Make an allocator for a return object initialized with Expr
6878
6879 function OK_To_Rename_Ref (N : Node_Id) return Boolean;
6880 -- Return True if N denotes an entity with OK_To_Rename set
6881
6882 ----------------------------------
6883 -- Build_Heap_Or_Pool_Allocator --
6884 ----------------------------------
6885
6886 function Build_Heap_Or_Pool_Allocator
6887 (Temp_Id : Entity_Id;
6888 Temp_Typ : Entity_Id;
6889 Ret_Typ : Entity_Id;
6890 Alloc_Expr : Node_Id) return Node_Id
6891 is
6892 begin
6893 pragma Assert (Is_Build_In_Place_Function (Func_Id));
6894
6895 -- Processing for objects that require finalization actions
6896
6897 if Needs_Finalization (Ret_Typ)
6898 and then not Has_Relaxed_Finalization (Ret_Typ)
6899 then
6900 declare
6901 Decls : constant List_Id := New_List;
6902 Fin_Coll_Id : constant Entity_Id :=
6903 Build_In_Place_Formal (Func_Id, BIP_Collection);
6904 Orig_Expr : constant Node_Id := New_Copy_Tree (Alloc_Expr);
6905 Stmts : constant List_Id := New_List;
6906 Local_Id : Entity_Id;
6907 Pool_Id : Entity_Id;
6908 Ptr_Typ : Entity_Id;
6909
6910 begin
6911 -- Generate:
6912 -- Pool_Id renames BIPstoragepool.all;
6913
6914 -- This formal is not added on ZFP as those targets do not
6915 -- support pools.
6916
6917 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
6918 Pool_Id := Make_Temporary (Loc, 'P');
6919
6920 Append_To (Decls,
6921 Make_Object_Renaming_Declaration (Loc,
6922 Defining_Identifier => Pool_Id,
6923 Subtype_Mark =>
6924 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc),
6925 Name =>
6926 Make_Explicit_Dereference (Loc,
6927 New_Occurrence_Of
6928 (Build_In_Place_Formal
6929 (Func_Id, BIP_Storage_Pool), Loc))));
6930 else
6931 Pool_Id := Empty;
6932 end if;
6933
6934 -- Create an access type which uses the storage pool of the
6935 -- caller. This additional type is necessary because the
6936 -- finalization collection cannot be associated with the type
6937 -- of the temporary. Otherwise the secondary stack allocation
6938 -- will fail.
6939
6940 -- Generate:
6941 -- type Ptr_Typ is access Ret_Typ;
6942
6943 Ptr_Typ := Make_Temporary (Loc, 'P');
6944
6945 Append_To (Decls,
6946 Make_Full_Type_Declaration (Loc,
6947 Defining_Identifier => Ptr_Typ,
6948 Type_Definition =>
6949 Make_Access_To_Object_Definition (Loc,
6950 Subtype_Indication =>
6951 New_Occurrence_Of (Ret_Typ, Loc))));
6952
6953 -- Perform minor decoration in order to set the collection and
6954 -- the storage pool attributes.
6955
6956 Mutate_Ekind (Ptr_Typ, E_Access_Type);
6957 Set_Finalization_Collection (Ptr_Typ, Fin_Coll_Id);
6958 Set_Associated_Storage_Pool (Ptr_Typ, Pool_Id);
6959
6960 -- Create the temporary, generate:
6961 -- Local_Id : Ptr_Typ;
6962
6963 Local_Id := Make_Temporary (Loc, 'T');
6964
6965 Append_To (Decls,
6966 Make_Object_Declaration (Loc,
6967 Defining_Identifier => Local_Id,
6968 Object_Definition =>
6969 New_Occurrence_Of (Ptr_Typ, Loc)));
6970 Set_No_Initialization (Last (Decls));
6971
6972 -- Allocate the object, generate:
6973 -- Local_Id := <Alloc_Expr>;
6974
6975 Append_To (Stmts,
6976 Make_Assignment_Statement (Loc,
6977 Name => New_Occurrence_Of (Local_Id, Loc),
6978 Expression => Alloc_Expr));
6979
6980 -- Generate:
6981 -- Temp_Id := Temp_Typ (Local_Id);
6982
6983 Append_To (Stmts,
6984 Make_Assignment_Statement (Loc,
6985 Name => New_Occurrence_Of (Temp_Id, Loc),
6986 Expression =>
6987 Unchecked_Convert_To (Temp_Typ,
6988 New_Occurrence_Of (Local_Id, Loc))));
6989
6990 -- Wrap the allocation in a block to make it conditioned by the
6991 -- presence of the caller's collection at run time.
6992
6993 -- Generate:
6994 -- if BIPcollection = null then
6995 -- Temp_Id := <Orig_Expr>;
6996 -- else
6997 -- declare
6998 -- <Decls>
6999 -- begin
7000 -- <Stmts>
7001 -- end;
7002 -- end if;
7003
7004 return
7005 Make_If_Statement (Loc,
7006 Condition =>
7007 Make_Op_Eq (Loc,
7008 Left_Opnd => New_Occurrence_Of (Fin_Coll_Id, Loc),
7009 Right_Opnd => Make_Null (Loc)),
7010
7011 Then_Statements => New_List (
7012 Make_Assignment_Statement (Loc,
7013 Name => New_Occurrence_Of (Temp_Id, Loc),
7014 Expression => Orig_Expr)),
7015
7016 Else_Statements => New_List (
7017 Make_Block_Statement (Loc,
7018 Declarations => Decls,
7019 Handled_Statement_Sequence =>
7020 Make_Handled_Sequence_Of_Statements (Loc,
7021 Statements => Stmts))));
7022 end;
7023
7024 -- For all other cases, generate:
7025 -- Temp_Id := <Alloc_Expr>;
7026
7027 else
7028 return
7029 Make_Assignment_Statement (Loc,
7030 Name => New_Occurrence_Of (Temp_Id, Loc),
7031 Expression => Alloc_Expr);
7032 end if;
7033 end Build_Heap_Or_Pool_Allocator;
7034
7035 --------------------------
7036 -- BIP_Function_Call_Id --
7037 --------------------------
7038
7039 function BIP_Function_Call_Id return Entity_Id is
7040
7041 function Func_Call_Id (Function_Call : Node_Id) return Entity_Id;
7042 -- Return the id of the called function.
7043
7044 function Func_Call_Id (Function_Call : Node_Id) return Entity_Id is
7045 Call_Node : constant Node_Id := Unqual_Conv (Function_Call);
7046
7047 begin
7048 if Is_Entity_Name (Name (Call_Node)) then
7049 return Entity (Name (Call_Node));
7050
7051 elsif Nkind (Name (Call_Node)) = N_Explicit_Dereference then
7052 return Etype (Name (Call_Node));
7053
7054 else
7055 pragma Assert (Nkind (Name (Call_Node)) = N_Selected_Component);
7056 return Etype (Entity (Selector_Name (Name (Call_Node))));
7057 end if;
7058 end Func_Call_Id;
7059
7060 -- Local declarations
7061
7062 BIP_Func_Call : Node_Id;
7063 Expr_Q : constant Node_Id := Unqual_Conv (Expr);
7064
7065 -- Start of processing for BIP_Function_Call_Id
7066
7067 begin
7068 if Is_Build_In_Place_Function_Call (Expr_Q) then
7069 return Func_Call_Id (Expr_Q);
7070 end if;
7071
7072 BIP_Func_Call := Unqual_BIP_Iface_Function_Call (Expr_Q);
7073
7074 if Present (BIP_Func_Call) then
7075
7076 -- In the case of an explicitly dereferenced call, return the
7077 -- subprogram type.
7078
7079 if Nkind (Name (BIP_Func_Call)) = N_Explicit_Dereference then
7080 return Etype (Name (BIP_Func_Call));
7081 else
7082 pragma Assert (Is_Entity_Name (Name (BIP_Func_Call)));
7083 return Entity (Name (BIP_Func_Call));
7084 end if;
7085
7086 elsif Nkind (Expr_Q) = N_Reference
7087 and then Is_Build_In_Place_Function_Call (Prefix (Expr_Q))
7088 then
7089 return Func_Call_Id (Prefix (Expr_Q));
7090
7091 else
7092 return Empty;
7093 end if;
7094 end BIP_Function_Call_Id;
7095
7096 -------------------------------------
7097 -- Count_Default_Sized_Task_Stacks --
7098 -------------------------------------
7099
7100 procedure Count_Default_Sized_Task_Stacks
7101 (Typ : Entity_Id;
7102 Pri_Stacks : out Int;
7103 Sec_Stacks : out Int)
7104 is
7105 Component : Entity_Id;
7106
7107 begin
7108 -- To calculate the number of default-sized task stacks required for
7109 -- an object of Typ, a depth-first recursive traversal of the AST
7110 -- from the Typ entity node is undertaken. Only type nodes containing
7111 -- task objects are visited.
7112
7113 Pri_Stacks := 0;
7114 Sec_Stacks := 0;
7115
7116 if not Has_Task (Typ) then
7117 return;
7118 end if;
7119
7120 case Ekind (Typ) is
7121 when E_Task_Subtype
7122 | E_Task_Type
7123 =>
7124 -- A task type is found marking the bottom of the descent. If
7125 -- the type has no representation aspect for the corresponding
7126 -- stack then that stack is using the default size.
7127
7128 if Present (Get_Rep_Item (Typ, Name_Storage_Size)) then
7129 Pri_Stacks := 0;
7130 else
7131 Pri_Stacks := 1;
7132 end if;
7133
7134 if Present (Get_Rep_Item (Typ, Name_Secondary_Stack_Size)) then
7135 Sec_Stacks := 0;
7136 else
7137 Sec_Stacks := 1;
7138 end if;
7139
7140 when E_Array_Subtype
7141 | E_Array_Type
7142 =>
7143 -- First find the number of default stacks contained within an
7144 -- array component.
7145
7146 Count_Default_Sized_Task_Stacks
7147 (Component_Type (Typ),
7148 Pri_Stacks,
7149 Sec_Stacks);
7150
7151 -- Then multiply the result by the size of the array
7152
7153 declare
7154 Quantity : constant Nat := Number_Of_Elements_In_Array (Typ);
7155 -- Number_Of_Elements_In_Array is non-trival, consequently
7156 -- its result is captured as an optimization.
7157
7158 begin
7159 Pri_Stacks := Pri_Stacks * Quantity;
7160 Sec_Stacks := Sec_Stacks * Quantity;
7161 end;
7162
7163 when E_Protected_Subtype
7164 | E_Protected_Type
7165 | E_Record_Subtype
7166 | E_Record_Type
7167 =>
7168 Component := First_Component (Typ);
7169
7170 -- Recursively descend each component of the composite type
7171 -- looking for tasks.
7172
7173 while Present (Component) loop
7174 declare
7175 P : Int;
7176 S : Int;
7177
7178 begin
7179 Count_Default_Sized_Task_Stacks (Etype (Component), P, S);
7180 Pri_Stacks := Pri_Stacks + P;
7181 Sec_Stacks := Sec_Stacks + S;
7182 end;
7183
7184 Next_Component (Component);
7185 end loop;
7186
7187 when E_Limited_Private_Subtype
7188 | E_Limited_Private_Type
7189 | E_Record_Subtype_With_Private
7190 | E_Record_Type_With_Private
7191 =>
7192 -- Switch to the full view of the private type to continue
7193 -- search.
7194
7195 Count_Default_Sized_Task_Stacks
7196 (Full_View (Typ), Pri_Stacks, Sec_Stacks);
7197
7198 -- Other types should not contain tasks
7199
7200 when others =>
7201 raise Program_Error;
7202 end case;
7203 end Count_Default_Sized_Task_Stacks;
7204
7205 -------------------------------
7206 -- Default_Initialize_Object --
7207 -------------------------------
7208
7209 procedure Default_Initialize_Object (After : Node_Id) is
7210 Init_Expr : Node_Id;
7211 Init_Stmts : List_Id;
7212
7213 begin
7214 -- Nothing to do if the object has an initialization expression or
7215 -- need not be initialized.
7216
7217 if Has_Init_Expression (N) or else No_Initialization (N) then
7218 return;
7219
7220 -- Default initialization is suppressed for objects that are already
7221 -- known to be imported (i.e. whose declaration specifies the Import
7222 -- aspect). Note that for objects with a pragma Import, we generate
7223 -- initialization here, and then remove it downstream when processing
7224 -- the pragma. It is also suppressed for variables for which a pragma
7225 -- Suppress_Initialization has been explicitly given
7226
7227 elsif Is_Imported (Def_Id)
7228 or else Suppress_Initialization (Def_Id)
7229 then
7230 return;
7231
7232 -- Nothing to do if the object being initialized is of a task type
7233 -- and restriction No_Tasking is in effect, because this is a direct
7234 -- violation of the restriction.
7235
7236 elsif Is_Task_Type (Base_Typ)
7237 and then Restriction_Active (No_Tasking)
7238 then
7239 return;
7240 end if;
7241
7242 -- First try a simple initialization; if it succeeds, then we just
7243 -- set the value as the expression of the declaration and let the
7244 -- code generator do the rest.
7245
7246 Init_Expr := Build_Default_Simple_Initialization (N, Typ, Def_Id);
7247
7248 if Present (Init_Expr) then
7249 Set_Expression (N, Init_Expr);
7250 Analyze_And_Resolve (Init_Expr, Typ);
7251 return;
7252 end if;
7253
7254 -- Or else build the fully-fledged initialization if need be
7255
7256 if Is_Mutably_Tagged_Type (Typ) then
7257 Init_Stmts :=
7258 Build_Default_Initialization (N, Etype (Typ), Def_Id);
7259 else
7260 Init_Stmts := Build_Default_Initialization (N, Typ, Def_Id);
7261 end if;
7262
7263 -- Insert the whole initialization sequence into the tree. If the
7264 -- object has a delayed freeze, as will be the case when it has
7265 -- aspect specifications, the initialization sequence is part of
7266 -- the freeze actions.
7267
7268 if Present (Init_Stmts) then
7269 if Has_Delayed_Freeze (Def_Id) then
7270 Append_Freeze_Actions (Def_Id, Init_Stmts);
7271 else
7272 Insert_Actions_After (After, Init_Stmts);
7273 end if;
7274 end if;
7275 end Default_Initialize_Object;
7276
7277 ------------------------------
7278 -- Initialize_Return_Object --
7279 ------------------------------
7280
7281 procedure Initialize_Return_Object
7282 (Tag_Assign : Node_Id;
7283 Adj_Call : Node_Id;
7284 Expr : Node_Id;
7285 Init_Stmt : Node_Id;
7286 After : Node_Id)
7287 is
7288 begin
7289 if Present (Tag_Assign) then
7290 Insert_Action_After (After, Tag_Assign);
7291 end if;
7292
7293 if Present (Adj_Call) then
7294 Insert_Action_After (After, Adj_Call);
7295 end if;
7296
7297 if No (Expr) then
7298 Default_Initialize_Object (After);
7299
7300 elsif Is_Delayed_Aggregate (Expr)
7301 and then not No_Initialization (N)
7302 then
7303 Convert_Aggr_In_Object_Decl (N);
7304
7305 elsif Present (Init_Stmt) then
7306 Insert_Action_After (After, Init_Stmt);
7307 Set_Expression (N, Empty);
7308 end if;
7309 end Initialize_Return_Object;
7310
7311 --------------------------------
7312 -- Is_Renamable_Function_Call --
7313 --------------------------------
7314
7315 function Is_Renamable_Function_Call (Expr : Node_Id) return Boolean is
7316 begin
7317 return not Is_Library_Level_Entity (Def_Id)
7318 and then Is_Captured_Function_Call (Expr)
7319 and then (not Special_Ret_Obj
7320 or else
7321 (Is_Related_To_Func_Return (Entity (Prefix (Expr)))
7322 and then Needs_Secondary_Stack (Etype (Expr)) =
7323 Needs_Secondary_Stack (Etype (Func_Id))));
7324 end Is_Renamable_Function_Call;
7325
7326 -------------------------------
7327 -- Make_Allocator_For_Return --
7328 -------------------------------
7329
7330 function Make_Allocator_For_Return (Expr : Node_Id) return Node_Id is
7331 Alloc : Node_Id;
7332 Alloc_Expr : Entity_Id;
7333 Alloc_Typ : Entity_Id;
7334
7335 begin
7336 -- If the return object's declaration does not include an expression,
7337 -- then we use its subtype for the allocation. Likewise in the case
7338 -- of a degenerate expression like a raise expression.
7339
7340 if No (Expr)
7341 or else Nkind (Original_Node (Expr)) = N_Raise_Expression
7342 then
7343 Alloc_Typ := Typ;
7344
7345 -- If the return object's declaration includes an expression, then
7346 -- there are two cases: either the nominal subtype of the object is
7347 -- definite and we can use it for the allocation directly, or it is
7348 -- not and Analyze_Object_Declaration should have built an actual
7349 -- subtype from the expression.
7350
7351 -- However, there are exceptions in the latter case for interfaces
7352 -- (see Analyze_Object_Declaration), as well as class-wide types and
7353 -- types with unknown discriminants if they have no underlying record
7354 -- view or are inherently limited (see Expand_Subtype_From_Expr), so
7355 -- we must cope with them.
7356
7357 elsif Is_Interface (Typ) then
7358 pragma Assert (Is_Class_Wide_Type (Typ));
7359
7360 -- For interfaces, we use the type of the expression, except if
7361 -- we need to put back a conversion that we have removed earlier
7362 -- in the processing.
7363
7364 if Is_Class_Wide_Type (Etype (Expr)) then
7365 Alloc_Typ := Typ;
7366 else
7367 Alloc_Typ := Etype (Expr);
7368 end if;
7369
7370 elsif Is_Class_Wide_Type (Typ) then
7371
7372 -- For class-wide types, we have to make sure that we use the
7373 -- dynamic type of the expression for the allocation, either by
7374 -- means of its (static) subtype or through the actual subtype.
7375
7376 if Has_Tag_Of_Type (Expr) then
7377 Alloc_Typ := Etype (Expr);
7378
7379 else pragma Assert (Ekind (Typ) = E_Class_Wide_Subtype
7380 and then Present (Equivalent_Type (Typ)));
7381
7382 Alloc_Typ := Typ;
7383 end if;
7384
7385 else pragma Assert (Is_Definite_Subtype (Typ)
7386 or else (Has_Unknown_Discriminants (Typ)
7387 and then (No (Underlying_Record_View (Typ))
7388 or else Is_Inherently_Limited_Type (Typ))));
7389
7390 Alloc_Typ := Typ;
7391 end if;
7392
7393 -- If the return object's declaration includes an expression and the
7394 -- declaration isn't marked as No_Initialization, then we generate an
7395 -- allocator with a qualified expression. Although this is necessary
7396 -- only in the case where the result type is an interface (or class-
7397 -- wide interface), we do it in all cases for the sake of consistency
7398 -- instead of subsequently generating a separate assignment.
7399
7400 if Present (Expr)
7401 and then not Is_Delayed_Aggregate (Expr)
7402 and then not No_Initialization (N)
7403 then
7404 -- Ada 2005 (AI95-344): If the result type is class-wide, insert
7405 -- a check that the level of the return expression's underlying
7406 -- type is not deeper than the level of the master enclosing the
7407 -- function.
7408
7409 -- AI12-043: The check is made immediately after the return object
7410 -- is created.
7411
7412 if Is_Class_Wide_Type (Etype (Func_Id)) then
7413 Apply_CW_Accessibility_Check (Expr, Func_Id);
7414 end if;
7415
7416 Alloc_Expr := New_Copy_Tree (Expr);
7417
7418 if Etype (Alloc_Expr) /= Alloc_Typ then
7419 Alloc_Expr := Convert_To (Alloc_Typ, Alloc_Expr);
7420 end if;
7421
7422 Alloc :=
7423 Make_Allocator (Loc,
7424 Expression =>
7425 Make_Qualified_Expression (Loc,
7426 Subtype_Mark =>
7427 New_Occurrence_Of (Alloc_Typ, Loc),
7428 Expression => Alloc_Expr));
7429
7430 else
7431 Alloc :=
7432 Make_Allocator (Loc,
7433 Expression => New_Occurrence_Of (Alloc_Typ, Loc));
7434
7435 -- If the return object requires default initialization, then it
7436 -- will happen later following the elaboration of the renaming.
7437 -- If we don't turn it off here, then the object will be default
7438 -- initialized twice.
7439
7440 Set_No_Initialization (Alloc);
7441 end if;
7442
7443 -- Set the flag indicating that the allocator is made for a special
7444 -- return object. This is used to bypass various legality checks as
7445 -- well as to make sure that the result is not adjusted twice.
7446
7447 Set_For_Special_Return_Object (Alloc);
7448
7449 return Alloc;
7450 end Make_Allocator_For_Return;
7451
7452 ----------------------
7453 -- OK_To_Rename_Ref --
7454 ----------------------
7455
7456 function OK_To_Rename_Ref (N : Node_Id) return Boolean is
7457 begin
7458 return Is_Entity_Name (N)
7459 and then Ekind (Entity (N)) = E_Variable
7460 and then OK_To_Rename (Entity (N));
7461 end OK_To_Rename_Ref;
7462
7463 -- Local variables
7464
7465 Adj_Call : Node_Id := Empty;
7466 Expr_Q : Node_Id := Empty;
7467 Tag_Assign : Node_Id := Empty;
7468
7469 Init_After : Node_Id := N;
7470 -- Node after which the initialization actions are to be inserted. This
7471 -- is normally N, except for the case of a shared passive variable, in
7472 -- which case the init proc call must be inserted only after the bodies
7473 -- of the shared variable procedures have been seen.
7474
7475 Has_BIP_Init_Expr : Boolean := False;
7476 -- Whether the object is initialized with a BIP function call
7477
7478 Rewrite_As_Renaming : Boolean := False;
7479 -- Whether to turn the declaration into a renaming at the end
7480
7481 -- Start of processing for Expand_N_Object_Declaration
7482
7483 begin
7484 -- Don't do anything for deferred constants. All proper actions will be
7485 -- expanded during the full declaration.
7486
7487 if No (Expr) and Constant_Present (N) then
7488 return;
7489 end if;
7490
7491 -- The type of the object cannot be abstract. This is diagnosed at the
7492 -- point the object is frozen, which happens after the declaration is
7493 -- fully expanded, so simply return now.
7494
7495 if Is_Abstract_Type (Typ) then
7496 return;
7497 end if;
7498
7499 -- No action needed for the internal imported dummy object added by
7500 -- Make_DT to compute the offset of the components that reference
7501 -- secondary dispatch tables; required to avoid never-ending loop
7502 -- processing this internal object declaration.
7503
7504 if Tagged_Type_Expansion
7505 and then Is_Internal (Def_Id)
7506 and then Is_Imported (Def_Id)
7507 and then Related_Type (Def_Id) = Implementation_Base_Type (Typ)
7508 then
7509 return;
7510 end if;
7511
7512 -- Make shared memory routines for shared passive variable
7513
7514 if Is_Shared_Passive (Def_Id) then
7515 Init_After := Make_Shared_Var_Procs (N);
7516 end if;
7517
7518 -- Determine whether the object is initialized with a BIP function call
7519
7520 if Present (Expr) then
7521 Expr_Q := Unqualify (Expr);
7522
7523 Has_BIP_Init_Expr :=
7524 Is_Build_In_Place_Function_Call (Expr_Q)
7525 or else Present (Unqual_BIP_Iface_Function_Call (Expr_Q))
7526 or else (Nkind (Expr_Q) = N_Reference
7527 and then
7528 Is_Build_In_Place_Function_Call (Prefix (Expr_Q)));
7529 end if;
7530
7531 -- If tasks are being declared, make sure we have an activation chain
7532 -- defined for the tasks (has no effect if we already have one), and
7533 -- also that a Master variable is established (and that the appropriate
7534 -- enclosing construct is established as a task master).
7535
7536 if Has_Task (Typ)
7537 or else Might_Have_Tasks (Typ)
7538 or else (Has_BIP_Init_Expr
7539 and then Needs_BIP_Task_Actuals (BIP_Function_Call_Id))
7540 then
7541 Build_Activation_Chain_Entity (N);
7542
7543 if Has_Task (Typ) then
7544 Build_Master_Entity (Def_Id);
7545
7546 -- Handle objects initialized with BIP function calls
7547
7548 elsif Has_BIP_Init_Expr then
7549 Build_Master_Entity (Def_Id);
7550 end if;
7551 end if;
7552
7553 -- If No_Implicit_Heap_Allocations or No_Implicit_Task_Allocations
7554 -- restrictions are active then default-sized secondary stacks are
7555 -- generated by the binder and allocated by SS_Init. To provide the
7556 -- binder the number of stacks to generate, the number of default-sized
7557 -- stacks required for task objects contained within the object
7558 -- declaration N is calculated here as it is at this point where
7559 -- unconstrained types become constrained. The result is stored in the
7560 -- enclosing unit's Unit_Record.
7561
7562 -- Note if N is an array object declaration that has an initialization
7563 -- expression, a second object declaration for the initialization
7564 -- expression is created by the compiler. To prevent double counting
7565 -- of the stacks in this scenario, the stacks of the first array are
7566 -- not counted.
7567
7568 if Might_Have_Tasks (Typ)
7569 and then not Restriction_Active (No_Secondary_Stack)
7570 and then (Restriction_Active (No_Implicit_Heap_Allocations)
7571 or else Restriction_Active (No_Implicit_Task_Allocations))
7572 and then not (Is_Array_Type (Typ) and then Has_Init_Expression (N))
7573 then
7574 declare
7575 PS_Count, SS_Count : Int;
7576 begin
7577 Count_Default_Sized_Task_Stacks (Typ, PS_Count, SS_Count);
7578 Increment_Primary_Stack_Count (PS_Count);
7579 Increment_Sec_Stack_Count (SS_Count);
7580 end;
7581 end if;
7582
7583 -- Default initialization required, and no expression present
7584
7585 if No (Expr) then
7586 -- If we have a type with a variant part, the initialization proc
7587 -- will contain implicit tests of the discriminant values, which
7588 -- counts as a violation of the restriction No_Implicit_Conditionals.
7589
7590 if Has_Variant_Part (Typ) then
7591 declare
7592 Msg : Boolean;
7593
7594 begin
7595 Check_Restriction (Msg, No_Implicit_Conditionals, Obj_Def);
7596
7597 if Msg then
7598 Error_Msg_N
7599 ("\initialization of variant record tests discriminants",
7600 Obj_Def);
7601 return;
7602 end if;
7603 end;
7604 end if;
7605
7606 -- For the default initialization case, if we have a private type
7607 -- with invariants, and invariant checks are enabled, then insert an
7608 -- invariant check after the object declaration. Note that it is OK
7609 -- to clobber the object with an invalid value since if the exception
7610 -- is raised, then the object will go out of scope. In the case where
7611 -- an array object is initialized with an aggregate, the expression
7612 -- is removed. Check flag Has_Init_Expression to avoid generating a
7613 -- junk invariant check and flag No_Initialization to avoid checking
7614 -- an uninitialized object such as a compiler temporary used for an
7615 -- aggregate.
7616
7617 if Has_Invariants (Base_Typ)
7618 and then Present (Invariant_Procedure (Base_Typ))
7619 and then not Has_Init_Expression (N)
7620 and then not No_Initialization (N)
7621 then
7622 -- If entity has an address clause or aspect, make invariant
7623 -- call into a freeze action for the explicit freeze node for
7624 -- object. Otherwise insert invariant check after declaration.
7625
7626 if Present (Following_Address_Clause (N))
7627 or else Has_Aspect (Def_Id, Aspect_Address)
7628 then
7629 Ensure_Freeze_Node (Def_Id);
7630
7631 if not Partial_View_Has_Unknown_Discr (Typ) then
7632 Append_Freeze_Action (Def_Id,
7633 Make_Invariant_Call (New_Occurrence_Of (Def_Id, Loc)));
7634 end if;
7635
7636 elsif not Partial_View_Has_Unknown_Discr (Typ) then
7637 Insert_After (N,
7638 Make_Invariant_Call (New_Occurrence_Of (Def_Id, Loc)));
7639 end if;
7640 end if;
7641
7642 -- For a special return object, the initialization must wait until
7643 -- after the object is turned into an allocator.
7644
7645 if not Special_Ret_Obj then
7646 Default_Initialize_Object (Init_After);
7647
7648 -- Check whether the object has been initialized above
7649
7650 if Present (Expression (N)) then
7651 if Is_Access_Type (Typ) then
7652 if Known_Non_Null (Expression (N)) then
7653 Set_Is_Known_Non_Null (Def_Id);
7654 elsif Known_Null (Expression (N)) then
7655 Set_Is_Known_Null (Def_Id);
7656 end if;
7657 end if;
7658
7659 if Is_Delayed_Aggregate (Expression (N)) then
7660 Convert_Aggr_In_Object_Decl (N);
7661 end if;
7662 end if;
7663 end if;
7664
7665 -- Generate attribute for Persistent_BSS if needed
7666
7667 if Persistent_BSS_Mode
7668 and then Comes_From_Source (N)
7669 and then Is_Potentially_Persistent_Type (Typ)
7670 and then not Has_Init_Expression (N)
7671 and then Is_Library_Level_Entity (Def_Id)
7672 then
7673 declare
7674 Prag : constant Node_Id :=
7675 Make_Linker_Section_Pragma (Def_Id, Loc, ".persistent.bss");
7676 begin
7677 Insert_After (N, Prag);
7678 Analyze (Prag);
7679 end;
7680 end if;
7681
7682 -- Explicit initialization present
7683
7684 else
7685 -- Obtain actual expression from qualified expression
7686
7687 Expr_Q := Unqualify (Expr);
7688
7689 -- When we have the appropriate kind of aggregate in the expression
7690 -- (this has been determined during analysis of the aggregate by
7691 -- setting the Expansion_Delayed flag), let's perform in place
7692 -- assignment and thus avoid creating a temporary.
7693
7694 if Is_Delayed_Aggregate (Expr_Q) then
7695
7696 -- For a special return object, the transformation must wait until
7697 -- after the object is turned into an allocator.
7698
7699 if not Special_Ret_Obj then
7700 Convert_Aggr_In_Object_Decl (N);
7701 end if;
7702
7703 -- If the initialization expression is a conditional expression whose
7704 -- expansion has been delayed, assign it explicitly to the object but
7705 -- only after analyzing it again and expanding it.
7706
7707 elsif Is_Delayed_Conditional_Expression (Expr_Q) then
7708 -- For a special return object, the transformation must wait until
7709 -- after the object is turned into an allocator, and will be done
7710 -- during the expansion of the allocator.
7711
7712 if not Special_Ret_Obj then
7713 declare
7714 Rhs : constant Node_Id := Relocate_Node (Expr);
7715 Assign : constant Node_Id :=
7716 Make_Assignment_Statement (Loc,
7717 Name => New_Occurrence_Of (Def_Id, Loc),
7718 Expression => Rhs);
7719
7720 begin
7721 Set_Assignment_OK (Name (Assign));
7722 Unanalyze_Delayed_Conditional_Expression (Rhs);
7723 Set_No_Finalize_Actions (Assign);
7724 Insert_Action_After (Init_After, Assign);
7725
7726 -- Save the assignment statement when declaring a controlled
7727 -- object. This reference is used later by the finalization
7728 -- machinery to mark the object as successfully initialized
7729
7730 if Needs_Finalization (Typ) then
7731 Set_Last_Aggregate_Assignment (Def_Id, Assign);
7732 end if;
7733
7734 Set_Expression (N, Empty);
7735 Set_No_Initialization (N);
7736 end;
7737 end if;
7738
7739 -- Ada 2005 (AI-318-02): If the initialization expression is a call
7740 -- to a build-in-place function, then access to the declared object
7741 -- must be passed to the function. Currently we limit such functions
7742 -- to those with constrained limited result subtypes, but eventually
7743 -- plan to expand the allowed forms of functions that are treated as
7744 -- build-in-place.
7745
7746 elsif Is_Build_In_Place_Function_Call (Expr_Q) then
7747 Make_Build_In_Place_Call_In_Object_Declaration (N, Expr_Q);
7748
7749 -- The previous call expands the expression initializing the
7750 -- built-in-place object into further code that will be analyzed
7751 -- later. No further expansion needed here.
7752
7753 return;
7754
7755 -- This is the same as the previous 'elsif', except that the call has
7756 -- been transformed by other expansion activities into something like
7757 -- F(...)'Reference.
7758
7759 elsif Nkind (Expr_Q) = N_Reference
7760 and then Is_Build_In_Place_Function_Call (Prefix (Expr_Q))
7761 and then not Is_Expanded_Build_In_Place_Call
7762 (Unqual_Conv (Prefix (Expr_Q)))
7763 then
7764 Make_Build_In_Place_Call_In_Anonymous_Context (Prefix (Expr_Q));
7765
7766 -- The previous call expands the expression initializing the
7767 -- built-in-place object into further code that will be analyzed
7768 -- later. No further expansion needed here.
7769
7770 return;
7771
7772 -- Ada 2005 (AI-318-02): Specialization of the previous case for
7773 -- expressions containing a build-in-place function call whose
7774 -- returned object covers interface types, and Expr_Q has calls to
7775 -- Ada.Tags.Displace to displace the pointer to the returned build-
7776 -- in-place object to reference the secondary dispatch table of a
7777 -- covered interface type.
7778
7779 elsif Present (Unqual_BIP_Iface_Function_Call (Expr_Q)) then
7780 Make_Build_In_Place_Iface_Call_In_Object_Declaration (N, Expr_Q);
7781
7782 -- The previous call expands the expression initializing the
7783 -- built-in-place object into further code that will be analyzed
7784 -- later. No further expansion needed here.
7785
7786 return;
7787
7788 -- Ada 2005 (AI-251): Rewrite the expression that initializes a
7789 -- class-wide interface object to ensure that we copy the full
7790 -- object, unless we are targetting a VM where interfaces are handled
7791 -- by VM itself. Note that if the root type of Typ is an ancestor of
7792 -- Expr's type, both types share the same dispatch table and there is
7793 -- no need to displace the pointer.
7794
7795 elsif Is_Interface (Typ)
7796
7797 -- Avoid never-ending recursion because if Equivalent_Type is set
7798 -- then we've done it already and must not do it again.
7799
7800 and then not
7801 (Nkind (Obj_Def) = N_Identifier
7802 and then Present (Equivalent_Type (Entity (Obj_Def))))
7803 then
7804 pragma Assert (Is_Class_Wide_Type (Typ));
7805
7806 -- If the original node of the expression was a conversion
7807 -- to this specific class-wide interface type then restore
7808 -- the original node because we must copy the object before
7809 -- displacing the pointer to reference the secondary tag
7810 -- component. This code must be kept synchronized with the
7811 -- expansion done by routine Expand_Interface_Conversion
7812
7813 if not Comes_From_Source (Expr)
7814 and then Nkind (Expr) = N_Explicit_Dereference
7815 and then Nkind (Original_Node (Expr)) = N_Type_Conversion
7816 and then Etype (Original_Node (Expr)) = Typ
7817 then
7818 Rewrite (Expr, Original_Node (Expression (N)));
7819 end if;
7820
7821 -- Avoid expansion of redundant interface conversion
7822
7823 if Nkind (Expr) = N_Type_Conversion
7824 and then Etype (Expr) = Typ
7825 then
7826 Expr_Q := Expression (Expr);
7827 else
7828 Expr_Q := Expr;
7829 end if;
7830
7831 -- We may use a renaming if the initialization expression is a
7832 -- captured function call that meets a few conditions.
7833
7834 Rewrite_As_Renaming := Is_Renamable_Function_Call (Expr_Q);
7835
7836 -- If the object is a special return object, then bypass special
7837 -- treatment of class-wide interface initialization below. In this
7838 -- case, the expansion of the return object will take care of this
7839 -- initialization via the expansion of the allocator.
7840
7841 if Special_Ret_Obj and then not Rewrite_As_Renaming then
7842
7843 -- If the type needs finalization and is not inherently
7844 -- limited, then the target is adjusted after the copy
7845 -- and attached to the finalization list.
7846
7847 if Needs_Finalization (Typ)
7848 and then not Is_Inherently_Limited_Type (Typ)
7849 then
7850 Adj_Call :=
7851 Make_Adjust_Call (
7852 Obj_Ref => New_Occurrence_Of (Def_Id, Loc),
7853 Typ => Base_Typ);
7854 end if;
7855
7856 -- Renaming an expression of the object's type is immediate
7857
7858 elsif Rewrite_As_Renaming
7859 and then Base_Type (Etype (Expr_Q)) = Base_Type (Typ)
7860 then
7861 null;
7862
7863 elsif Tagged_Type_Expansion then
7864 declare
7865 Iface : constant Entity_Id := Root_Type (Typ);
7866
7867 Expr_Typ : Entity_Id;
7868 New_Expr : Node_Id;
7869 Obj_Id : Entity_Id;
7870 Ptr_Obj_Decl : Node_Id;
7871 Ptr_Obj_Id : Entity_Id;
7872 Tag_Comp : Node_Id;
7873
7874 begin
7875 Expr_Typ := Base_Type (Etype (Expr_Q));
7876 if Is_Class_Wide_Type (Expr_Typ) then
7877 Expr_Typ := Root_Type (Expr_Typ);
7878 end if;
7879
7880 -- Rename limited objects since they cannot be copied
7881
7882 if Is_Limited_Record (Expr_Typ) then
7883 Rewrite_As_Renaming := True;
7884 end if;
7885
7886 Obj_Id := Make_Temporary (Loc, 'D', Expr_Q);
7887
7888 -- Replace
7889 -- IW : I'Class := Expr;
7890 -- by
7891 -- Dnn : Tag renames Tag_Ptr!(Expr'Address).all;
7892 -- type Ityp is not null access I'Class;
7893 -- Rnn : constant Ityp :=
7894 -- Ityp!(Displace (Dnn'Address, I'Tag));
7895 -- IW : I'Class renames Rnn.all;
7896
7897 if Rewrite_As_Renaming then
7898 New_Expr :=
7899 Make_Explicit_Dereference (Loc,
7900 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
7901 Make_Attribute_Reference (Loc,
7902 Prefix => Relocate_Node (Expr_Q),
7903 Attribute_Name => Name_Address)));
7904
7905 -- Suppress junk access checks on RE_Tag_Ptr
7906
7907 Insert_Action (N,
7908 Make_Object_Renaming_Declaration (Loc,
7909 Defining_Identifier => Obj_Id,
7910 Subtype_Mark =>
7911 New_Occurrence_Of (RTE (RE_Tag), Loc),
7912 Name => New_Expr),
7913 Suppress => Access_Check);
7914
7915 -- Dynamically reference the tag associated with the
7916 -- interface.
7917
7918 Tag_Comp :=
7919 Make_Function_Call (Loc,
7920 Name => New_Occurrence_Of (RTE (RE_Displace), Loc),
7921 Parameter_Associations => New_List (
7922 Make_Attribute_Reference (Loc,
7923 Prefix => New_Occurrence_Of (Obj_Id, Loc),
7924 Attribute_Name => Name_Address),
7925 New_Occurrence_Of
7926 (Node (First_Elmt (Access_Disp_Table (Iface))),
7927 Loc)));
7928
7929 -- Replace
7930 -- IW : I'Class := Expr;
7931 -- by
7932 -- Dnn : Typ := Expr;
7933 -- type Ityp is not null access I'Class;
7934 -- Rnn : constant Ityp := Ityp (Dnn.I_Tag'Address);
7935 -- IW : I'Class renames Rnn.all;
7936
7937 elsif Has_Tag_Of_Type (Expr_Q)
7938 and then Interface_Present_In_Ancestor (Expr_Typ, Typ)
7939 and then (Expr_Typ = Etype (Expr_Typ)
7940 or else not
7941 Is_Variable_Size_Record (Etype (Expr_Typ)))
7942 then
7943 Insert_Action (N,
7944 Make_Object_Declaration (Loc,
7945 Defining_Identifier => Obj_Id,
7946 Object_Definition =>
7947 New_Occurrence_Of (Expr_Typ, Loc),
7948 Expression => Relocate_Node (Expr_Q)));
7949
7950 -- Statically reference the tag associated with the
7951 -- interface
7952
7953 Tag_Comp :=
7954 Make_Selected_Component (Loc,
7955 Prefix => New_Occurrence_Of (Obj_Id, Loc),
7956 Selector_Name =>
7957 New_Occurrence_Of
7958 (Find_Interface_Tag (Expr_Typ, Iface), Loc));
7959
7960 -- Replace
7961 -- IW : I'Class := Expr;
7962 -- by
7963 -- type Equiv_Record is record ... end record;
7964 -- implicit subtype CW is <Class_Wide_Subtype>;
7965 -- Dnn : CW := CW!(Expr);
7966 -- type Ityp is not null access I'Class;
7967 -- Rnn : constant Ityp :=
7968 -- Ityp!(Displace (Dnn'Address, I'Tag));
7969 -- IW : I'Class renames Rnn.all;
7970
7971 else
7972 -- Generate the equivalent record type and update the
7973 -- subtype indication to reference it.
7974
7975 Expand_Subtype_From_Expr
7976 (N => N,
7977 Unc_Type => Typ,
7978 Subtype_Indic => Obj_Def,
7979 Exp => Expr_Q);
7980
7981 -- For interface types we use 'Address which displaces
7982 -- the pointer to the base of the object (if required).
7983
7984 if Is_Interface (Etype (Expr_Q)) then
7985 New_Expr :=
7986 Unchecked_Convert_To (Etype (Obj_Def),
7987 Make_Explicit_Dereference (Loc,
7988 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
7989 Make_Attribute_Reference (Loc,
7990 Prefix => Relocate_Node (Expr_Q),
7991 Attribute_Name => Name_Address))));
7992
7993 -- For other types, no displacement is needed
7994
7995 else
7996 New_Expr := Relocate_Node (Expr_Q);
7997 end if;
7998
7999 -- Suppress junk access checks on RE_Tag_Ptr
8000
8001 Insert_Action (N,
8002 Make_Object_Declaration (Loc,
8003 Defining_Identifier => Obj_Id,
8004 Object_Definition =>
8005 New_Occurrence_Of (Etype (Obj_Def), Loc),
8006 Expression => New_Expr),
8007 Suppress => Access_Check);
8008
8009 -- Dynamically reference the tag associated with the
8010 -- interface.
8011
8012 Tag_Comp :=
8013 Make_Function_Call (Loc,
8014 Name => New_Occurrence_Of (RTE (RE_Displace), Loc),
8015 Parameter_Associations => New_List (
8016 Make_Attribute_Reference (Loc,
8017 Prefix => New_Occurrence_Of (Obj_Id, Loc),
8018 Attribute_Name => Name_Address),
8019 New_Occurrence_Of
8020 (Node (First_Elmt (Access_Disp_Table (Iface))),
8021 Loc)));
8022 end if;
8023
8024 -- As explained in Exp_Disp, we use Convert_Tag_To_Interface
8025 -- to do the final conversion, but we insert an intermediate
8026 -- temporary before the dereference so that we can process
8027 -- the expansion as part of the analysis of the declaration
8028 -- of this temporary, and then rewrite manually the original
8029 -- object as the simple renaming of this dereference.
8030
8031 Tag_Comp := Convert_Tag_To_Interface (Typ, Tag_Comp);
8032 pragma Assert (Nkind (Tag_Comp) = N_Explicit_Dereference
8033 and then
8034 Nkind (Prefix (Tag_Comp)) = N_Unchecked_Type_Conversion);
8035
8036 Ptr_Obj_Id := Make_Temporary (Loc, 'R');
8037
8038 Ptr_Obj_Decl :=
8039 Make_Object_Declaration (Loc,
8040 Defining_Identifier => Ptr_Obj_Id,
8041 Constant_Present => True,
8042 Object_Definition =>
8043 New_Occurrence_Of
8044 (Entity (Subtype_Mark (Prefix (Tag_Comp))), Loc),
8045 Expression => Prefix (Tag_Comp));
8046
8047 Insert_Action (N, Ptr_Obj_Decl, Suppress => All_Checks);
8048
8049 Set_Prefix (Tag_Comp, New_Occurrence_Of (Ptr_Obj_Id, Loc));
8050 Expr_Q := Tag_Comp;
8051 Set_Etype (Expr_Q, Typ);
8052 Set_Parent (Expr_Q, N);
8053
8054 Rewrite_As_Renaming := True;
8055 end;
8056
8057 else
8058 return;
8059 end if;
8060
8061 -- Common case of explicit object initialization
8062
8063 else
8064 -- Small optimization: if the expression is a function call and
8065 -- the object is stand-alone, not declared at library level and of
8066 -- a class-wide type, then we capture the result of the call into
8067 -- a temporary, with the benefit that, if the result's type does
8068 -- not need finalization, nothing will be finalized and, if it
8069 -- does, the temporary only will be finalized by means of a direct
8070 -- call to the Finalize primitive if the result's type is not a
8071 -- class-wide type; whereas, in both cases, the stand-alone object
8072 -- itself would be finalized by means of a dispatching call to the
8073 -- Deep_Finalize routine.
8074
8075 if Nkind (Expr_Q) = N_Function_Call
8076 and then not Special_Ret_Obj
8077 and then not Is_Library_Level_Entity (Def_Id)
8078 and then Is_Class_Wide_Type (Typ)
8079 then
8080 Remove_Side_Effects (Expr_Q);
8081 end if;
8082
8083 -- In most cases, we must check that the initial value meets any
8084 -- constraint imposed by the declared type. However, there is one
8085 -- very important exception to this rule. If the entity has an
8086 -- unconstrained nominal subtype, then it acquired its constraints
8087 -- from the expression in the first place, and not only does this
8088 -- mean that the constraint check is not needed, but an attempt to
8089 -- perform the constraint check can cause order of elaboration
8090 -- problems.
8091
8092 if not Is_Constr_Subt_For_U_Nominal (Typ) then
8093
8094 -- If this is an allocator for an aggregate that has been
8095 -- allocated in place, delay checks until assignments are
8096 -- made, because the discriminants are not initialized.
8097
8098 if Nkind (Expr) = N_Allocator
8099 and then No_Initialization (Expr)
8100 then
8101 null;
8102
8103 -- Otherwise apply a constraint check now if no prev error
8104
8105 elsif Nkind (Expr) /= N_Error then
8106 Apply_Constraint_Check (Expr, Typ);
8107
8108 -- Deal with possible range check
8109
8110 if Do_Range_Check (Expr) then
8111
8112 -- If assignment checks are suppressed, turn off flag
8113
8114 if Suppress_Assignment_Checks (N) then
8115 Set_Do_Range_Check (Expr, False);
8116
8117 -- Otherwise generate the range check
8118
8119 else
8120 Generate_Range_Check
8121 (Expr, Typ, CE_Range_Check_Failed);
8122 end if;
8123 end if;
8124 end if;
8125 end if;
8126
8127 -- For tagged types, when an init value is given, the tag has to
8128 -- be re-initialized separately in order to avoid the propagation
8129 -- of a wrong tag coming from a view conversion unless the type
8130 -- is class wide (in this case the tag comes from the init value).
8131 -- Suppress the tag assignment when not Tagged_Type_Expansion
8132 -- because tags are represented implicitly in objects. Ditto for
8133 -- types that are CPP_CLASS, and for initializations that are
8134 -- aggregates, because they have to have the right tag.
8135
8136 -- The re-assignment of the tag has to be done even if the object
8137 -- is a constant. The assignment must be analyzed after the
8138 -- declaration. If an address clause follows, this is handled as
8139 -- part of the freeze actions for the object, otherwise insert
8140 -- tag assignment here.
8141
8142 Tag_Assign := Make_Tag_Assignment (N);
8143
8144 if Present (Tag_Assign) then
8145 if Present (Following_Address_Clause (N))
8146 or else Has_Aspect (Def_Id, Aspect_Address)
8147 then
8148 Ensure_Freeze_Node (Def_Id);
8149 elsif not Special_Ret_Obj then
8150 Insert_Action_After (Init_After, Tag_Assign);
8151 end if;
8152
8153 -- Handle C++ constructor calls. Note that we do not check that
8154 -- Typ is a tagged type since the equivalent Ada type of a C++
8155 -- class that has no virtual methods is an untagged limited
8156 -- record type.
8157
8158 elsif Is_CPP_Constructor_Call (Expr) then
8159 declare
8160 Id_Ref : constant Node_Id := New_Occurrence_Of (Def_Id, Loc);
8161
8162 begin
8163 -- The call to the initialization procedure does NOT freeze
8164 -- the object being initialized.
8165
8166 Set_Must_Not_Freeze (Id_Ref);
8167 Set_Assignment_OK (Id_Ref);
8168
8169 Insert_Actions_After (Init_After,
8170 Build_Initialization_Call (N, Id_Ref, Typ,
8171 Constructor_Ref => Expr));
8172
8173 -- We remove here the original call to the constructor
8174 -- to avoid its management in the backend
8175
8176 Set_Expression (N, Empty);
8177 return;
8178 end;
8179
8180 -- Handle initialization of limited tagged types
8181
8182 elsif Is_Tagged_Type (Typ)
8183 and then Is_Class_Wide_Type (Typ)
8184 and then Is_Limited_Record (Typ)
8185 and then not Is_Limited_Interface (Typ)
8186 then
8187 -- Given that the type is limited we cannot perform a copy. If
8188 -- Expr_Q is the reference to a variable we mark the variable
8189 -- as OK_To_Rename to expand this declaration into a renaming
8190 -- declaration (see below).
8191
8192 if Is_Entity_Name (Expr_Q) then
8193 Set_OK_To_Rename (Entity (Expr_Q));
8194
8195 -- If we cannot convert the expression into a renaming we must
8196 -- consider it an internal error because the backend does not
8197 -- have support to handle it. But avoid crashing on a raise
8198 -- expression or conditional expression.
8199
8200 elsif Nkind (Original_Node (Expr_Q)) not in
8201 N_Raise_Expression | N_If_Expression | N_Case_Expression
8202 then
8203 raise Program_Error;
8204 end if;
8205
8206 -- For discrete types, set the Is_Known_Valid flag if the
8207 -- initializing value is known to be valid. Only do this for
8208 -- source assignments, since otherwise we can end up turning
8209 -- on the known valid flag prematurely from inserted code.
8210
8211 elsif Comes_From_Source (N)
8212 and then Is_Discrete_Type (Typ)
8213 and then Expr_Known_Valid (Expr)
8214 and then Safe_To_Capture_Value (N, Def_Id)
8215 then
8216 Set_Is_Known_Valid (Def_Id);
8217
8218 -- For access types, set the Is_Known_Non_Null flag if the
8219 -- initializing value is known to be non-null. We can also
8220 -- set Can_Never_Be_Null if this is a constant.
8221
8222 elsif Is_Access_Type (Typ) and then Known_Non_Null (Expr) then
8223 Set_Is_Known_Non_Null (Def_Id, True);
8224
8225 if Constant_Present (N) then
8226 Set_Can_Never_Be_Null (Def_Id);
8227 end if;
8228 end if;
8229
8230 -- If validity checking on copies, validate initial expression.
8231 -- But skip this if declaration is for a generic type, since it
8232 -- makes no sense to validate generic types. Not clear if this
8233 -- can happen for legal programs, but it definitely can arise
8234 -- from previous instantiation errors.
8235
8236 if Validity_Checks_On
8237 and then Comes_From_Source (N)
8238 and then Validity_Check_Copies
8239 and then not Is_Generic_Type (Typ)
8240 then
8241 Ensure_Valid (Expr);
8242
8243 if Safe_To_Capture_Value (N, Def_Id) then
8244 Set_Is_Known_Valid (Def_Id);
8245 end if;
8246 end if;
8247
8248 -- Now determine whether we will use a renaming
8249
8250 Rewrite_As_Renaming :=
8251
8252 -- The declaration cannot be rewritten if it has got constraints
8253
8254 Is_Entity_Name (Original_Node (Obj_Def))
8255
8256 -- If we have "X : S := ...;", and S is a constrained array
8257 -- subtype, then we cannot rename, because renamings ignore
8258 -- the constraints of S, so that would change the semantics
8259 -- (sliding would not occur on the initial value). This is
8260 -- only a problem for source objects though, the others have
8261 -- the correct bounds.
8262
8263 and then not (Comes_From_Source (Obj_Def)
8264 and then Is_Array_Type (Typ)
8265 and then Is_Constrained (Typ))
8266
8267 -- Moreover, if we have "X : aliased S := "...;" and S is an
8268 -- unconstrained array type, then we can rename only if the
8269 -- initialization expression has an unconstrained subtype too,
8270 -- because the bounds must be present within X.
8271
8272 and then not (Is_Constr_Array_Subt_With_Bounds (Typ)
8273 and then Is_Constrained (Etype (Expr_Q)))
8274
8275 -- We may use a renaming if the initialization expression is a
8276 -- captured function call that meets a few conditions.
8277
8278 and then
8279 (Is_Renamable_Function_Call (Expr_Q)
8280
8281 -- Or else if it is a variable with OK_To_Rename set
8282
8283 or else (OK_To_Rename_Ref (Expr_Q)
8284 and then not Special_Ret_Obj)
8285
8286 -- Or else if it is a slice of such a variable
8287
8288 or else (Nkind (Expr_Q) = N_Slice
8289 and then OK_To_Rename_Ref (Prefix (Expr_Q))
8290 and then not Special_Ret_Obj));
8291
8292 -- If the type needs finalization and is not inherently limited,
8293 -- then the target is adjusted after the copy and attached to the
8294 -- finalization list. However, no adjustment is needed in the case
8295 -- where the object has been initialized by a call to a function
8296 -- returning on the primary stack (see Expand_Ctrl_Function_Call)
8297 -- since no copy occurred, given that the type is by-reference.
8298 -- Likewise if it is initialized by a 2-pass aggregate, since the
8299 -- actual initialization will only occur during the second pass.
8300 -- Similarly, no adjustment is needed if we are going to rewrite
8301 -- the object declaration into a renaming declaration.
8302
8303 if Needs_Finalization (Typ)
8304 and then not Is_Inherently_Limited_Type (Typ)
8305 and then Nkind (Expr_Q) /= N_Function_Call
8306 and then not Is_Two_Pass_Aggregate (Expr_Q)
8307 and then not Rewrite_As_Renaming
8308 then
8309 Adj_Call :=
8310 Make_Adjust_Call (
8311 Obj_Ref => New_Occurrence_Of (Def_Id, Loc),
8312 Typ => Base_Typ);
8313
8314 if Present (Adj_Call) and then not Special_Ret_Obj then
8315 Insert_Action_After (Init_After, Adj_Call);
8316 end if;
8317 end if;
8318 end if;
8319
8320 -- Cases where the back end cannot handle the initialization
8321 -- directly. In such cases, we expand an assignment that will
8322 -- be appropriately handled by Expand_N_Assignment_Statement.
8323
8324 -- The exclusion of the unconstrained case is wrong, but for now it
8325 -- is too much trouble ???
8326
8327 if (Is_Possibly_Unaligned_Slice (Expr)
8328 or else (Is_Possibly_Unaligned_Object (Expr)
8329 and then not Represented_As_Scalar (Etype (Expr))))
8330 and then not (Is_Array_Type (Etype (Expr))
8331 and then not Is_Constrained (Etype (Expr)))
8332 then
8333 declare
8334 Stat : constant Node_Id :=
8335 Make_Assignment_Statement (Loc,
8336 Name => New_Occurrence_Of (Def_Id, Loc),
8337 Expression => Relocate_Node (Expr));
8338 begin
8339 Set_Assignment_OK (Name (Stat));
8340 Set_No_Ctrl_Actions (Stat);
8341 Insert_Action_After (Init_After, Stat);
8342 Set_Expression (N, Empty);
8343 Set_No_Initialization (N);
8344 end;
8345 end if;
8346 end if;
8347
8348 if Nkind (Obj_Def) = N_Access_Definition
8349 and then not Is_Local_Anonymous_Access (Typ)
8350 then
8351 -- An Ada 2012 stand-alone object of an anonymous access type
8352
8353 declare
8354 Level : constant Entity_Id :=
8355 Make_Defining_Identifier (Loc,
8356 Chars =>
8357 New_External_Name (Chars (Def_Id), Suffix => "L"));
8358
8359 Level_Decl : Node_Id;
8360 Level_Expr : Node_Id;
8361
8362 begin
8363 Mutate_Ekind (Level, Ekind (Def_Id));
8364 Set_Etype (Level, Standard_Natural);
8365 Set_Scope (Level, Scope (Def_Id));
8366
8367 -- Set accessibility level of null
8368
8369 if No (Expr) then
8370 Level_Expr :=
8371 Make_Integer_Literal
8372 (Loc, Scope_Depth (Standard_Standard));
8373
8374 -- When the expression of the object is a function which returns
8375 -- an anonymous access type the master of the call is the object
8376 -- being initialized instead of the type.
8377
8378 elsif Nkind (Expr) = N_Function_Call
8379 and then Ekind (Etype (Name (Expr))) = E_Anonymous_Access_Type
8380 then
8381 Level_Expr := Accessibility_Level
8382 (Def_Id, Object_Decl_Level);
8383
8384 -- General case
8385
8386 else
8387 Level_Expr := Accessibility_Level (Expr, Dynamic_Level);
8388 end if;
8389
8390 Level_Decl :=
8391 Make_Object_Declaration (Loc,
8392 Defining_Identifier => Level,
8393 Object_Definition =>
8394 New_Occurrence_Of (Standard_Natural, Loc),
8395 Expression => Level_Expr,
8396 Constant_Present => Constant_Present (N),
8397 Has_Init_Expression => True);
8398
8399 Insert_Action_After (Init_After, Level_Decl);
8400
8401 Set_Extra_Accessibility (Def_Id, Level);
8402 end;
8403 end if;
8404
8405 -- If the object is default initialized and its type is subject to
8406 -- pragma Default_Initial_Condition, add a runtime check to verify
8407 -- the assumption of the pragma (SPARK RM 7.3.3). Generate:
8408
8409 -- <Base_Typ>DIC (<Base_Typ> (Def_Id));
8410
8411 -- Note that the check is generated for source objects only
8412
8413 if Comes_From_Source (Def_Id)
8414 and then Has_DIC (Typ)
8415 and then Present (DIC_Procedure (Typ))
8416 and then not Has_Null_Body (DIC_Procedure (Typ))
8417 and then not Has_Init_Expression (N)
8418 and then No (Expr)
8419 and then not Is_Imported (Def_Id)
8420 then
8421 declare
8422 DIC_Call : constant Node_Id :=
8423 Build_DIC_Call
8424 (Loc, New_Occurrence_Of (Def_Id, Loc), Typ);
8425 begin
8426 if Present (Next_N) then
8427 Insert_Before_And_Analyze (Next_N, DIC_Call);
8428
8429 -- The object declaration is the last node in a declarative or a
8430 -- statement list.
8431
8432 else
8433 Append_To (List_Containing (N), DIC_Call);
8434 Analyze (DIC_Call);
8435 end if;
8436 end;
8437 end if;
8438
8439 -- If this is the return object of a build-in-place function, locate the
8440 -- implicit BIPaccess parameter designating the caller-supplied return
8441 -- object and convert the declaration to a renaming of a dereference of
8442 -- this parameter. If the declaration includes an expression, add an
8443 -- assignment statement to ensure the return object gets initialized.
8444
8445 -- Result : T [:= <expression>];
8446
8447 -- is converted to
8448
8449 -- Result : T renames BIPaccess.all;
8450 -- [Result := <expression>;]
8451
8452 -- in the constrained case, or to
8453
8454 -- type Txx is access all ...;
8455 -- Rxx : Txx := null;
8456
8457 -- if BIPalloc = 1 then
8458 -- Rxx := BIPaccess;
8459 -- Rxx.all := <expression>;
8460 -- elsif BIPalloc = 2 then
8461 -- Rxx := new <expression-type>'(<expression>)[storage_pool =
8462 -- system__secondary_stack__ss_pool][procedure_to_call =
8463 -- system__secondary_stack__ss_allocate];
8464 -- elsif BIPalloc = 3 then
8465 -- Rxx := new <expression-type>'(<expression>)
8466 -- elsif BIPalloc = 4 then
8467 -- Pxx : system__storage_pools__root_storage_pool renames
8468 -- BIPstoragepool.all;
8469 -- Rxx := new <expression-type>'(<expression>)[storage_pool =
8470 -- Pxx][procedure_to_call =
8471 -- system__storage_pools__allocate_any];
8472 -- else
8473 -- [program_error "build in place mismatch"]
8474 -- end if;
8475
8476 -- Result : T renames Rxx.all;
8477
8478 -- in the unconstrained case.
8479
8480 if Is_Build_In_Place_Return_Object (Def_Id) then
8481 declare
8482 Init_Stmt : Node_Id;
8483 Obj_Acc_Formal : Entity_Id;
8484
8485 begin
8486 -- Retrieve the implicit access parameter passed by the caller
8487
8488 Obj_Acc_Formal :=
8489 Build_In_Place_Formal (Func_Id, BIP_Object_Access);
8490
8491 -- If the return object's declaration includes an expression
8492 -- and the declaration isn't marked as No_Initialization, then
8493 -- we need to generate an assignment to the object and insert
8494 -- it after the declaration before rewriting it as a renaming
8495 -- (otherwise we'll lose the initialization). The case where
8496 -- the result type is an interface (or class-wide interface)
8497 -- is also excluded because the context of the function call
8498 -- must be unconstrained, so the initialization will always
8499 -- be done as part of an allocator evaluation (storage pool
8500 -- or secondary stack), never to a constrained target object
8501 -- passed in by the caller. Besides the assignment being
8502 -- unneeded in this case, it avoids problems with trying to
8503 -- generate a dispatching assignment when the return expression
8504 -- is a nonlimited descendant of a limited interface (the
8505 -- interface has no assignment operation).
8506
8507 if Present (Expr_Q)
8508 and then not Is_Delayed_Aggregate (Expr_Q)
8509 and then not No_Initialization (N)
8510 and then not Is_Interface (Typ)
8511 then
8512 if Is_Class_Wide_Type (Typ)
8513 and then not Is_Class_Wide_Type (Etype (Expr_Q))
8514 then
8515 Init_Stmt :=
8516 Make_Assignment_Statement (Loc,
8517 Name => New_Occurrence_Of (Def_Id, Loc),
8518 Expression =>
8519 Make_Type_Conversion (Loc,
8520 Subtype_Mark =>
8521 New_Occurrence_Of (Typ, Loc),
8522 Expression => New_Copy_Tree (Expr_Q)));
8523
8524 else
8525 Init_Stmt :=
8526 Make_Assignment_Statement (Loc,
8527 Name => New_Occurrence_Of (Def_Id, Loc),
8528 Expression => New_Copy_Tree (Expr_Q));
8529 end if;
8530
8531 Set_Assignment_OK (Name (Init_Stmt));
8532 Set_No_Ctrl_Actions (Init_Stmt);
8533
8534 else
8535 Init_Stmt := Empty;
8536 end if;
8537
8538 -- When the function's subtype is unconstrained, a run-time
8539 -- test may be needed to decide the form of allocation to use
8540 -- for the return object. The function has an implicit formal
8541 -- parameter indicating this. If the BIP_Alloc_Form formal has
8542 -- the value one, then the caller has passed access to an
8543 -- existing object for use as the return object. If the value
8544 -- is two, then the return object must be allocated on the
8545 -- secondary stack. If the value is three, then the return
8546 -- object must be allocated on the heap. Otherwise, the object
8547 -- must be allocated in a storage pool. We generate an if
8548 -- statement to test the BIP_Alloc_Form formal and initialize
8549 -- a local access value appropriately.
8550
8551 if Needs_BIP_Alloc_Form (Func_Id) then
8552 declare
8553 Desig_Typ : constant Entity_Id :=
8554 (if Ekind (Typ) = E_Array_Subtype
8555 then Etype (Func_Id) else Typ);
8556 -- Ensure that the we use a fat pointer when allocating
8557 -- an unconstrained array on the heap. In this case the
8558 -- result object's type is a constrained array type even
8559 -- though the function's type is unconstrained.
8560
8561 Obj_Alloc_Formal : constant Entity_Id :=
8562 Build_In_Place_Formal (Func_Id, BIP_Alloc_Form);
8563 Pool_Id : constant Entity_Id :=
8564 Make_Temporary (Loc, 'P');
8565
8566 Acc_Typ : Entity_Id;
8567 Alloc_Obj_Decl : Node_Id;
8568 Alloc_Obj_Id : Entity_Id;
8569 Alloc_Stmt : Node_Id;
8570 Guard_Except : Node_Id;
8571 Heap_Allocator : Node_Id;
8572 Pool_Allocator : Node_Id;
8573 Pool_Decl : Node_Id;
8574 Ptr_Typ_Decl : Node_Id;
8575 SS_Allocator : Node_Id;
8576
8577 begin
8578 -- Create an access type designating the function's
8579 -- result subtype.
8580
8581 Acc_Typ := Make_Temporary (Loc, 'A');
8582
8583 Ptr_Typ_Decl :=
8584 Make_Full_Type_Declaration (Loc,
8585 Defining_Identifier => Acc_Typ,
8586 Type_Definition =>
8587 Make_Access_To_Object_Definition (Loc,
8588 All_Present => True,
8589 Subtype_Indication =>
8590 New_Occurrence_Of (Desig_Typ, Loc)));
8591
8592 Insert_Action (N, Ptr_Typ_Decl, Suppress => All_Checks);
8593
8594 -- Create an access object that will be initialized to an
8595 -- access value denoting the return object, either coming
8596 -- from an implicit access value passed in by the caller
8597 -- or from the result of an allocator.
8598
8599 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
8600
8601 Alloc_Obj_Decl :=
8602 Make_Object_Declaration (Loc,
8603 Defining_Identifier => Alloc_Obj_Id,
8604 Object_Definition =>
8605 New_Occurrence_Of (Acc_Typ, Loc));
8606
8607 Insert_Action (N, Alloc_Obj_Decl, Suppress => All_Checks);
8608
8609 -- First create the Heap_Allocator
8610
8611 Heap_Allocator := Make_Allocator_For_Return (Expr_Q);
8612
8613 -- The Pool_Allocator is just like the Heap_Allocator,
8614 -- except we set Storage_Pool and Procedure_To_Call so
8615 -- it will use the user-defined storage pool.
8616
8617 Pool_Allocator := Make_Allocator_For_Return (Expr_Q);
8618
8619 -- Do not generate the renaming of the build-in-place
8620 -- pool parameter on ZFP because the parameter is not
8621 -- created in the first place.
8622
8623 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
8624 Pool_Decl :=
8625 Make_Object_Renaming_Declaration (Loc,
8626 Defining_Identifier => Pool_Id,
8627 Subtype_Mark =>
8628 New_Occurrence_Of
8629 (RTE (RE_Root_Storage_Pool), Loc),
8630 Name =>
8631 Make_Explicit_Dereference (Loc,
8632 New_Occurrence_Of
8633 (Build_In_Place_Formal
8634 (Func_Id, BIP_Storage_Pool), Loc)));
8635 Set_Storage_Pool (Pool_Allocator, Pool_Id);
8636 Set_Procedure_To_Call
8637 (Pool_Allocator, RTE (RE_Allocate_Any));
8638 else
8639 Pool_Decl := Make_Null_Statement (Loc);
8640 end if;
8641
8642 -- If the No_Allocators restriction is active, then only
8643 -- an allocator for secondary stack allocation is needed.
8644 -- It's OK for such allocators to have Comes_From_Source
8645 -- set to False, because gigi knows not to flag them as
8646 -- being a violation of No_Implicit_Heap_Allocations.
8647
8648 if Restriction_Active (No_Allocators) then
8649 SS_Allocator := Heap_Allocator;
8650 Heap_Allocator := Make_Null (Loc);
8651 Pool_Allocator := Make_Null (Loc);
8652
8653 -- Otherwise the heap and pool allocators may be needed,
8654 -- so we make another allocator for secondary stack
8655 -- allocation.
8656
8657 else
8658 SS_Allocator := Make_Allocator_For_Return (Expr_Q);
8659
8660 -- The heap and pool allocators are marked as
8661 -- Comes_From_Source since they correspond to an
8662 -- explicit user-written allocator (that is, it will
8663 -- only be executed on behalf of callers that call the
8664 -- function as initialization for such an allocator).
8665 -- Prevents errors when No_Implicit_Heap_Allocations
8666 -- is in force.
8667
8668 Set_Comes_From_Source (Heap_Allocator, True);
8669 Set_Comes_From_Source (Pool_Allocator, True);
8670 end if;
8671
8672 -- The allocator is returned on the secondary stack
8673
8674 Check_Restriction (No_Secondary_Stack, N);
8675 Set_Storage_Pool (SS_Allocator, RTE (RE_SS_Pool));
8676 Set_Procedure_To_Call
8677 (SS_Allocator, RTE (RE_SS_Allocate));
8678
8679 -- The allocator is returned on the secondary stack,
8680 -- so indicate that the function return, as well as
8681 -- all blocks that encloses the allocator, must not
8682 -- release it. The flags must be set now because
8683 -- the decision to use the secondary stack is done
8684 -- very late in the course of expanding the return
8685 -- statement, past the point where these flags are
8686 -- normally set.
8687
8688 Set_Uses_Sec_Stack (Func_Id);
8689 Set_Uses_Sec_Stack (Scope (Def_Id));
8690 Set_Sec_Stack_Needed_For_Return (Scope (Def_Id));
8691
8692 -- Guard against poor expansion on the caller side by
8693 -- using a raise statement to catch out-of-range values
8694 -- of formal parameter BIP_Alloc_Form.
8695
8696 if Exceptions_OK then
8697 Guard_Except :=
8698 Make_Raise_Program_Error (Loc,
8699 Reason => PE_Build_In_Place_Mismatch);
8700 else
8701 Guard_Except := Make_Null_Statement (Loc);
8702 end if;
8703
8704 -- Create an if statement to test the BIP_Alloc_Form
8705 -- formal and initialize the access object to either the
8706 -- BIP_Object_Access formal (BIP_Alloc_Form =
8707 -- Caller_Allocation), the result of allocating the
8708 -- object in the secondary stack (BIP_Alloc_Form =
8709 -- Secondary_Stack), or else an allocator to create the
8710 -- return object in the heap or user-defined pool
8711 -- (BIP_Alloc_Form = Global_Heap or User_Storage_Pool).
8712
8713 -- ??? An unchecked type conversion must be made in the
8714 -- case of assigning the access object formal to the
8715 -- local access object, because a normal conversion would
8716 -- be illegal in some cases (such as converting access-
8717 -- to-unconstrained to access-to-constrained), but the
8718 -- the unchecked conversion will presumably fail to work
8719 -- right in just such cases. It's not clear at all how to
8720 -- handle this.
8721
8722 Alloc_Stmt :=
8723 Make_If_Statement (Loc,
8724 Condition =>
8725 Make_Op_Eq (Loc,
8726 Left_Opnd =>
8727 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
8728 Right_Opnd =>
8729 Make_Integer_Literal (Loc,
8730 UI_From_Int (BIP_Allocation_Form'Pos
8731 (Caller_Allocation)))),
8732
8733 Then_Statements => New_List (
8734 Make_Assignment_Statement (Loc,
8735 Name =>
8736 New_Occurrence_Of (Alloc_Obj_Id, Loc),
8737 Expression =>
8738 Unchecked_Convert_To
8739 (Acc_Typ,
8740 New_Occurrence_Of (Obj_Acc_Formal, Loc)))),
8741
8742 Elsif_Parts => New_List (
8743 Make_Elsif_Part (Loc,
8744 Condition =>
8745 Make_Op_Eq (Loc,
8746 Left_Opnd =>
8747 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
8748 Right_Opnd =>
8749 Make_Integer_Literal (Loc,
8750 UI_From_Int (BIP_Allocation_Form'Pos
8751 (Secondary_Stack)))),
8752
8753 Then_Statements => New_List (
8754 Make_Assignment_Statement (Loc,
8755 Name =>
8756 New_Occurrence_Of (Alloc_Obj_Id, Loc),
8757 Expression => SS_Allocator))),
8758
8759 Make_Elsif_Part (Loc,
8760 Condition =>
8761 Make_Op_Eq (Loc,
8762 Left_Opnd =>
8763 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
8764 Right_Opnd =>
8765 Make_Integer_Literal (Loc,
8766 UI_From_Int (BIP_Allocation_Form'Pos
8767 (Global_Heap)))),
8768
8769 Then_Statements => New_List (
8770 Build_Heap_Or_Pool_Allocator
8771 (Temp_Id => Alloc_Obj_Id,
8772 Temp_Typ => Acc_Typ,
8773 Ret_Typ => Desig_Typ,
8774 Alloc_Expr => Heap_Allocator))),
8775
8776 -- ??? If all is well, we can put the following
8777 -- 'elsif' in the 'else', but this is a useful
8778 -- self-check in case caller and callee don't agree
8779 -- on whether BIPAlloc and so on should be passed.
8780
8781 Make_Elsif_Part (Loc,
8782 Condition =>
8783 Make_Op_Eq (Loc,
8784 Left_Opnd =>
8785 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
8786 Right_Opnd =>
8787 Make_Integer_Literal (Loc,
8788 UI_From_Int (BIP_Allocation_Form'Pos
8789 (User_Storage_Pool)))),
8790
8791 Then_Statements => New_List (
8792 Pool_Decl,
8793 Build_Heap_Or_Pool_Allocator
8794 (Temp_Id => Alloc_Obj_Id,
8795 Temp_Typ => Acc_Typ,
8796 Ret_Typ => Desig_Typ,
8797 Alloc_Expr => Pool_Allocator)))),
8798
8799 -- Raise Program_Error if it's none of the above;
8800 -- this is a compiler bug.
8801
8802 Else_Statements => New_List (Guard_Except));
8803
8804 -- If a separate initialization assignment was created
8805 -- earlier, append that following the assignment of the
8806 -- implicit access formal to the access object, to ensure
8807 -- that the return object is initialized in that case. In
8808 -- this situation, the target of the assignment must be
8809 -- rewritten to denote a dereference of the access to the
8810 -- return object passed in by the caller.
8811
8812 if Present (Init_Stmt) then
8813 Set_Name (Init_Stmt,
8814 Make_Explicit_Dereference (Loc,
8815 Prefix => New_Occurrence_Of (Alloc_Obj_Id, Loc)));
8816 Set_Assignment_OK (Name (Init_Stmt));
8817
8818 Append_To (Then_Statements (Alloc_Stmt), Init_Stmt);
8819 Init_Stmt := Empty;
8820 end if;
8821
8822 Insert_Action (N, Alloc_Stmt, Suppress => All_Checks);
8823
8824 -- From now on, the type of the return object is the
8825 -- designated type.
8826
8827 if Desig_Typ /= Typ then
8828 Set_Etype (Def_Id, Desig_Typ);
8829 Set_Actual_Subtype (Def_Id, Typ);
8830 end if;
8831
8832 -- Remember the local access object for use in the
8833 -- dereference of the renaming created below.
8834
8835 Obj_Acc_Formal := Alloc_Obj_Id;
8836 end;
8837
8838 -- When the function's type is unconstrained and a run-time test
8839 -- is not needed, we nevertheless need to build the return using
8840 -- the return object's type.
8841
8842 elsif not Is_Constrained (Underlying_Type (Etype (Func_Id))) then
8843 declare
8844 Acc_Typ : Entity_Id;
8845 Alloc_Obj_Decl : Node_Id;
8846 Alloc_Obj_Id : Entity_Id;
8847 Ptr_Typ_Decl : Node_Id;
8848
8849 begin
8850 -- Create an access type designating the function's
8851 -- result subtype.
8852
8853 Acc_Typ := Make_Temporary (Loc, 'A');
8854
8855 Ptr_Typ_Decl :=
8856 Make_Full_Type_Declaration (Loc,
8857 Defining_Identifier => Acc_Typ,
8858 Type_Definition =>
8859 Make_Access_To_Object_Definition (Loc,
8860 All_Present => True,
8861 Subtype_Indication =>
8862 New_Occurrence_Of (Typ, Loc)));
8863
8864 Insert_Action (N, Ptr_Typ_Decl, Suppress => All_Checks);
8865
8866 -- Create an access object initialized to the conversion
8867 -- of the implicit access value passed in by the caller.
8868
8869 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
8870
8871 -- See the ??? comment a few lines above about the use of
8872 -- an unchecked conversion here.
8873
8874 Alloc_Obj_Decl :=
8875 Make_Object_Declaration (Loc,
8876 Defining_Identifier => Alloc_Obj_Id,
8877 Constant_Present => True,
8878 Object_Definition =>
8879 New_Occurrence_Of (Acc_Typ, Loc),
8880 Expression =>
8881 Unchecked_Convert_To
8882 (Acc_Typ, New_Occurrence_Of (Obj_Acc_Formal, Loc)));
8883
8884 Insert_Action (N, Alloc_Obj_Decl, Suppress => All_Checks);
8885
8886 -- Remember the local access object for use in the
8887 -- dereference of the renaming created below.
8888
8889 Obj_Acc_Formal := Alloc_Obj_Id;
8890 end;
8891 end if;
8892
8893 -- Initialize the object now that it has got its final subtype,
8894 -- but before rewriting it as a renaming.
8895
8896 Initialize_Return_Object
8897 (Tag_Assign, Adj_Call, Expr_Q, Init_Stmt, Init_After);
8898
8899 -- Save the assignment statement when returning a controlled
8900 -- object. This reference is used later by the finalization
8901 -- machinery to mark the object as successfully initialized.
8902
8903 if Present (Init_Stmt) and then Needs_Finalization (Typ) then
8904 Set_Last_Aggregate_Assignment (Def_Id, Init_Stmt);
8905 end if;
8906
8907 -- Replace the return object declaration with a renaming of a
8908 -- dereference of the access value designating the return object.
8909
8910 Expr_Q :=
8911 Make_Explicit_Dereference (Loc,
8912 Prefix => New_Occurrence_Of (Obj_Acc_Formal, Loc));
8913 Set_Etype (Expr_Q, Etype (Def_Id));
8914
8915 Rewrite_As_Renaming := True;
8916 end;
8917
8918 -- If we can rename the initialization expression, we need to make sure
8919 -- that we use the proper type in the case of a return object that lives
8920 -- on the secondary stack (see other cases below for a similar handling)
8921 -- and that the tag is assigned in the case of any return object.
8922
8923 elsif Rewrite_As_Renaming then
8924 if Special_Ret_Obj then
8925 declare
8926 Desig_Typ : constant Entity_Id :=
8927 (if Ekind (Typ) = E_Array_Subtype
8928 then Etype (Func_Id) else Typ);
8929
8930 begin
8931 -- From now on, the type of the return object is the
8932 -- designated type.
8933
8934 if Desig_Typ /= Typ then
8935 Set_Etype (Def_Id, Desig_Typ);
8936 Set_Actual_Subtype (Def_Id, Typ);
8937 end if;
8938
8939 if Present (Tag_Assign) then
8940 Insert_Action_After (Init_After, Tag_Assign);
8941 end if;
8942
8943 -- Ada 2005 (AI95-344): If the result type is class-wide,
8944 -- insert a check that the level of the return expression's
8945 -- underlying type is not deeper than the level of the master
8946 -- enclosing the function.
8947
8948 -- AI12-043: The check is made immediately after the return
8949 -- object is created.
8950
8951 if Is_Class_Wide_Type (Etype (Func_Id)) then
8952 Apply_CW_Accessibility_Check (Expr_Q, Func_Id);
8953 end if;
8954 end;
8955 end if;
8956
8957 -- If this is the return object of a function returning on the secondary
8958 -- stack, convert the declaration to a renaming of the dereference of ah
8959 -- allocator for the secondary stack.
8960
8961 -- Result : T [:= <expression>];
8962
8963 -- is converted to
8964
8965 -- type Txx is access all ...;
8966 -- Rxx : constant Txx :=
8967 -- new <expression-type>['(<expression>)][storage_pool =
8968 -- system__secondary_stack__ss_pool][procedure_to_call =
8969 -- system__secondary_stack__ss_allocate];
8970
8971 -- Result : T renames Rxx.all;
8972
8973 elsif Is_Secondary_Stack_Return_Object (Def_Id) then
8974 declare
8975 Desig_Typ : constant Entity_Id :=
8976 (if Ekind (Typ) = E_Array_Subtype
8977 then Etype (Func_Id) else Typ);
8978 -- Ensure that the we use a fat pointer when allocating
8979 -- an unconstrained array on the heap. In this case the
8980 -- result object's type is a constrained array type even
8981 -- though the function's type is unconstrained.
8982
8983 Acc_Typ : Entity_Id;
8984 Alloc_Obj_Decl : Node_Id;
8985 Alloc_Obj_Id : Entity_Id;
8986 Ptr_Type_Decl : Node_Id;
8987
8988 begin
8989 -- Create an access type designating the function's
8990 -- result subtype.
8991
8992 Acc_Typ := Make_Temporary (Loc, 'A');
8993
8994 Ptr_Type_Decl :=
8995 Make_Full_Type_Declaration (Loc,
8996 Defining_Identifier => Acc_Typ,
8997 Type_Definition =>
8998 Make_Access_To_Object_Definition (Loc,
8999 All_Present => True,
9000 Subtype_Indication =>
9001 New_Occurrence_Of (Desig_Typ, Loc)));
9002
9003 Insert_Action (N, Ptr_Type_Decl, Suppress => All_Checks);
9004
9005 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
9006
9007 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
9008
9009 Alloc_Obj_Decl :=
9010 Make_Object_Declaration (Loc,
9011 Defining_Identifier => Alloc_Obj_Id,
9012 Constant_Present => True,
9013 Object_Definition =>
9014 New_Occurrence_Of (Acc_Typ, Loc),
9015 Expression => Make_Allocator_For_Return (Expr_Q));
9016
9017 Insert_Action (N, Alloc_Obj_Decl, Suppress => All_Checks);
9018
9019 Set_Uses_Sec_Stack (Func_Id);
9020 Set_Uses_Sec_Stack (Scope (Def_Id));
9021 Set_Sec_Stack_Needed_For_Return (Scope (Def_Id));
9022
9023 -- From now on, the type of the return object is the
9024 -- designated type.
9025
9026 if Desig_Typ /= Typ then
9027 Set_Etype (Def_Id, Desig_Typ);
9028 Set_Actual_Subtype (Def_Id, Typ);
9029 end if;
9030
9031 -- Initialize the object now that it has got its final subtype,
9032 -- but before rewriting it as a renaming.
9033
9034 Initialize_Return_Object
9035 (Tag_Assign, Adj_Call, Expr_Q, Empty, Init_After);
9036
9037 -- Replace the return object declaration with a renaming of a
9038 -- dereference of the access value designating the return object.
9039
9040 Expr_Q :=
9041 Make_Explicit_Dereference (Loc,
9042 Prefix => New_Occurrence_Of (Alloc_Obj_Id, Loc));
9043 Set_Etype (Expr_Q, Etype (Def_Id));
9044
9045 Rewrite_As_Renaming := True;
9046 end;
9047
9048 -- If this is the return object of a function returning a by-reference
9049 -- type, convert the declaration to a renaming of the dereference of ah
9050 -- allocator for the return stack.
9051
9052 -- Result : T [:= <expression>];
9053
9054 -- is converted to
9055
9056 -- type Txx is access all ...;
9057 -- Rxx : constant Txx :=
9058 -- new <expression-type>['(<expression>)][storage_pool =
9059 -- system__return_stack__rs_pool][procedure_to_call =
9060 -- system__return_stack__rs_allocate];
9061
9062 -- Result : T renames Rxx.all;
9063
9064 elsif Back_End_Return_Slot
9065 and then Is_By_Reference_Return_Object (Def_Id)
9066 then
9067 declare
9068 Acc_Typ : Entity_Id;
9069 Alloc_Obj_Decl : Node_Id;
9070 Alloc_Obj_Id : Entity_Id;
9071 Ptr_Type_Decl : Node_Id;
9072
9073 begin
9074 -- Create an access type designating the function's
9075 -- result subtype.
9076
9077 Acc_Typ := Make_Temporary (Loc, 'A');
9078
9079 Ptr_Type_Decl :=
9080 Make_Full_Type_Declaration (Loc,
9081 Defining_Identifier => Acc_Typ,
9082 Type_Definition =>
9083 Make_Access_To_Object_Definition (Loc,
9084 All_Present => True,
9085 Subtype_Indication =>
9086 New_Occurrence_Of (Typ, Loc)));
9087
9088 Insert_Action (N, Ptr_Type_Decl, Suppress => All_Checks);
9089
9090 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_RS_Pool));
9091
9092 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
9093
9094 Alloc_Obj_Decl :=
9095 Make_Object_Declaration (Loc,
9096 Defining_Identifier => Alloc_Obj_Id,
9097 Constant_Present => True,
9098 Object_Definition =>
9099 New_Occurrence_Of (Acc_Typ, Loc),
9100 Expression => Make_Allocator_For_Return (Expr_Q));
9101
9102 Insert_Action (N, Alloc_Obj_Decl, Suppress => All_Checks);
9103
9104 -- Initialize the object now that it has got its final subtype,
9105 -- but before rewriting it as a renaming.
9106
9107 Initialize_Return_Object
9108 (Tag_Assign, Adj_Call, Expr_Q, Empty, Init_After);
9109
9110 -- Replace the return object declaration with a renaming of a
9111 -- dereference of the access value designating the return object.
9112
9113 Expr_Q :=
9114 Make_Explicit_Dereference (Loc,
9115 Prefix => New_Occurrence_Of (Alloc_Obj_Id, Loc));
9116 Set_Etype (Expr_Q, Etype (Def_Id));
9117
9118 Rewrite_As_Renaming := True;
9119 end;
9120 end if;
9121
9122 -- Final transformation - turn the object declaration into a renaming
9123 -- if appropriate. If this is the completion of a deferred constant
9124 -- declaration, then this transformation generates what would be
9125 -- illegal code if written by hand, but that's OK.
9126
9127 if Rewrite_As_Renaming then
9128 Rewrite_Object_Declaration_As_Renaming (N, Expr_Q);
9129 end if;
9130
9131 -- Exception on library entity not available
9132
9133 exception
9134 when RE_Not_Available =>
9135 return;
9136 end Expand_N_Object_Declaration;
9137
9138 ---------------------------------
9139 -- Expand_N_Subtype_Indication --
9140 ---------------------------------
9141
9142 -- Add a check on the range of the subtype and deal with validity checking
9143
9144 procedure Expand_N_Subtype_Indication (N : Node_Id) is
9145 Ran : constant Node_Id := Range_Expression (Constraint (N));
9146 Typ : constant Entity_Id := Entity (Subtype_Mark (N));
9147
9148 begin
9149 if Nkind (Constraint (N)) = N_Range_Constraint then
9150 Validity_Check_Range (Range_Expression (Constraint (N)));
9151 end if;
9152
9153 -- Do not duplicate the work of Process_Range_Expr_In_Decl in Sem_Ch3
9154
9155 if Nkind (Parent (N)) in N_Constrained_Array_Definition | N_Slice
9156 and then Nkind (Parent (Parent (N))) not in
9157 N_Full_Type_Declaration | N_Object_Declaration
9158 then
9159 Apply_Range_Check (Ran, Typ);
9160 end if;
9161 end Expand_N_Subtype_Indication;
9162
9163 ---------------------------
9164 -- Expand_N_Variant_Part --
9165 ---------------------------
9166
9167 -- Note: this procedure no longer has any effect. It used to be that we
9168 -- would replace the choices in the last variant by a when others, and
9169 -- also expanded static predicates in variant choices here, but both of
9170 -- those activities were being done too early, since we can't check the
9171 -- choices until the statically predicated subtypes are frozen, which can
9172 -- happen as late as the free point of the record, and we can't change the
9173 -- last choice to an others before checking the choices, which is now done
9174 -- at the freeze point of the record.
9175
9176 procedure Expand_N_Variant_Part (N : Node_Id) is
9177 begin
9178 null;
9179 end Expand_N_Variant_Part;
9180
9181 ---------------------------------
9182 -- Expand_Previous_Access_Type --
9183 ---------------------------------
9184
9185 procedure Expand_Previous_Access_Type (Def_Id : Entity_Id) is
9186 Ptr_Typ : Entity_Id;
9187
9188 begin
9189 -- Find all access types in the current scope whose designated type is
9190 -- Def_Id and build master renamings for them.
9191
9192 Ptr_Typ := First_Entity (Current_Scope);
9193 while Present (Ptr_Typ) loop
9194 if Is_Access_Type (Ptr_Typ)
9195 and then Designated_Type (Ptr_Typ) = Def_Id
9196 and then No (Master_Id (Ptr_Typ))
9197 then
9198 -- Ensure that the designated type has a master
9199
9200 Build_Master_Entity (Def_Id);
9201
9202 -- Private and incomplete types complicate the insertion of master
9203 -- renamings because the access type may precede the full view of
9204 -- the designated type. For this reason, the master renamings are
9205 -- inserted relative to the designated type.
9206
9207 Build_Master_Renaming (Ptr_Typ, Ins_Nod => Parent (Def_Id));
9208 end if;
9209
9210 Next_Entity (Ptr_Typ);
9211 end loop;
9212 end Expand_Previous_Access_Type;
9213
9214 -----------------------------
9215 -- Expand_Record_Extension --
9216 -----------------------------
9217
9218 -- Add a field _parent at the beginning of the record extension. This is
9219 -- used to implement inheritance. Here are some examples of expansion:
9220
9221 -- 1. no discriminants
9222 -- type T2 is new T1 with null record;
9223 -- gives
9224 -- type T2 is new T1 with record
9225 -- _Parent : T1;
9226 -- end record;
9227
9228 -- 2. renamed discriminants
9229 -- type T2 (B, C : Int) is new T1 (A => B) with record
9230 -- _Parent : T1 (A => B);
9231 -- D : Int;
9232 -- end;
9233
9234 -- 3. inherited discriminants
9235 -- type T2 is new T1 with record -- discriminant A inherited
9236 -- _Parent : T1 (A);
9237 -- D : Int;
9238 -- end;
9239
9240 procedure Expand_Record_Extension (T : Entity_Id; Def : Node_Id) is
9241 Indic : constant Node_Id := Subtype_Indication (Def);
9242 Loc : constant Source_Ptr := Sloc (Def);
9243 Rec_Ext_Part : Node_Id := Record_Extension_Part (Def);
9244 Par_Subtype : Entity_Id;
9245 Comp_List : Node_Id;
9246 Comp_Decl : Node_Id;
9247 Parent_N : Node_Id;
9248 D : Entity_Id;
9249 List_Constr : constant List_Id := New_List;
9250
9251 begin
9252 -- Expand_Record_Extension is called directly from the semantics, so
9253 -- we must check to see whether expansion is active before proceeding,
9254 -- because this affects the visibility of selected components in bodies
9255 -- of instances. Within a generic we still need to set Parent_Subtype
9256 -- link because the visibility of inherited components will have to be
9257 -- verified in subsequent instances.
9258
9259 if not Expander_Active then
9260 if Inside_A_Generic and then Ekind (T) = E_Record_Type then
9261 Set_Parent_Subtype (T, Etype (T));
9262 end if;
9263 return;
9264 end if;
9265
9266 -- This may be a derivation of an untagged private type whose full
9267 -- view is tagged, in which case the Derived_Type_Definition has no
9268 -- extension part. Build an empty one now.
9269
9270 if No (Rec_Ext_Part) then
9271 Rec_Ext_Part :=
9272 Make_Record_Definition (Loc,
9273 End_Label => Empty,
9274 Component_List => Empty,
9275 Null_Present => True);
9276
9277 Set_Record_Extension_Part (Def, Rec_Ext_Part);
9278 Mark_Rewrite_Insertion (Rec_Ext_Part);
9279 end if;
9280
9281 Comp_List := Component_List (Rec_Ext_Part);
9282
9283 Parent_N := Make_Defining_Identifier (Loc, Name_uParent);
9284
9285 -- If the derived type inherits its discriminants the type of the
9286 -- _parent field must be constrained by the inherited discriminants
9287
9288 if Has_Discriminants (T)
9289 and then Nkind (Indic) /= N_Subtype_Indication
9290 and then not Is_Constrained (Entity (Indic))
9291 then
9292 D := First_Discriminant (T);
9293 while Present (D) loop
9294 Append_To (List_Constr, New_Occurrence_Of (D, Loc));
9295 Next_Discriminant (D);
9296 end loop;
9297
9298 Par_Subtype :=
9299 Process_Subtype (
9300 Make_Subtype_Indication (Loc,
9301 Subtype_Mark => New_Occurrence_Of (Entity (Indic), Loc),
9302 Constraint =>
9303 Make_Index_Or_Discriminant_Constraint (Loc,
9304 Constraints => List_Constr)),
9305 Def);
9306
9307 -- Otherwise the original subtype_indication is just what is needed
9308
9309 else
9310 Par_Subtype := Process_Subtype (New_Copy_Tree (Indic), Def);
9311 end if;
9312
9313 Set_Parent_Subtype (T, Par_Subtype);
9314
9315 Comp_Decl :=
9316 Make_Component_Declaration (Loc,
9317 Defining_Identifier => Parent_N,
9318 Component_Definition =>
9319 Make_Component_Definition (Loc,
9320 Subtype_Indication => New_Occurrence_Of (Par_Subtype, Loc)));
9321
9322 if Null_Present (Rec_Ext_Part) then
9323 Set_Component_List (Rec_Ext_Part,
9324 Make_Component_List (Loc,
9325 Component_Items => New_List (Comp_Decl),
9326 Variant_Part => Empty,
9327 Null_Present => False));
9328 Set_Null_Present (Rec_Ext_Part, False);
9329
9330 elsif Null_Present (Comp_List)
9331 or else Is_Empty_List (Component_Items (Comp_List))
9332 then
9333 Set_Component_Items (Comp_List, New_List (Comp_Decl));
9334 Set_Null_Present (Comp_List, False);
9335
9336 else
9337 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
9338 end if;
9339
9340 Analyze (Comp_Decl);
9341 end Expand_Record_Extension;
9342
9343 ------------------------
9344 -- Expand_Tagged_Root --
9345 ------------------------
9346
9347 procedure Expand_Tagged_Root (T : Entity_Id) is
9348 Def : constant Node_Id := Type_Definition (Parent (T));
9349 Comp_List : Node_Id;
9350 Comp_Decl : Node_Id;
9351 Sloc_N : Source_Ptr;
9352
9353 begin
9354 if Null_Present (Def) then
9355 Set_Component_List (Def,
9356 Make_Component_List (Sloc (Def),
9357 Component_Items => Empty_List,
9358 Variant_Part => Empty,
9359 Null_Present => True));
9360 end if;
9361
9362 Comp_List := Component_List (Def);
9363
9364 if Null_Present (Comp_List)
9365 or else Is_Empty_List (Component_Items (Comp_List))
9366 then
9367 Sloc_N := Sloc (Comp_List);
9368 else
9369 Sloc_N := Sloc (First (Component_Items (Comp_List)));
9370 end if;
9371
9372 Comp_Decl :=
9373 Make_Component_Declaration (Sloc_N,
9374 Defining_Identifier => First_Tag_Component (T),
9375 Component_Definition =>
9376 Make_Component_Definition (Sloc_N,
9377 Subtype_Indication => New_Occurrence_Of (RTE (RE_Tag), Sloc_N)));
9378
9379 if Null_Present (Comp_List)
9380 or else Is_Empty_List (Component_Items (Comp_List))
9381 then
9382 Set_Component_Items (Comp_List, New_List (Comp_Decl));
9383 Set_Null_Present (Comp_List, False);
9384
9385 else
9386 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
9387 end if;
9388
9389 -- We don't Analyze the whole expansion because the tag component has
9390 -- already been analyzed previously. Here we just insure that the tree
9391 -- is coherent with the semantic decoration
9392
9393 Find_Type (Subtype_Indication (Component_Definition (Comp_Decl)));
9394
9395 exception
9396 when RE_Not_Available =>
9397 return;
9398 end Expand_Tagged_Root;
9399
9400 ------------------------------
9401 -- Freeze_Stream_Operations --
9402 ------------------------------
9403
9404 procedure Freeze_Stream_Operations (N : Node_Id; Typ : Entity_Id) is
9405 Names : constant array (1 .. 4) of TSS_Name_Type :=
9406 (TSS_Stream_Input,
9407 TSS_Stream_Output,
9408 TSS_Stream_Read,
9409 TSS_Stream_Write);
9410 Stream_Op : Entity_Id;
9411
9412 begin
9413 -- Primitive operations of tagged types are frozen when the dispatch
9414 -- table is constructed.
9415
9416 if not Comes_From_Source (Typ) or else Is_Tagged_Type (Typ) then
9417 return;
9418 end if;
9419
9420 for J in Names'Range loop
9421 Stream_Op := TSS (Typ, Names (J));
9422
9423 if Present (Stream_Op)
9424 and then Is_Subprogram (Stream_Op)
9425 and then Nkind (Unit_Declaration_Node (Stream_Op)) =
9426 N_Subprogram_Declaration
9427 and then not Is_Frozen (Stream_Op)
9428 then
9429 Append_Freeze_Actions (Typ, Freeze_Entity (Stream_Op, N));
9430 end if;
9431 end loop;
9432 end Freeze_Stream_Operations;
9433
9434 -----------------
9435 -- Freeze_Type --
9436 -----------------
9437
9438 -- Full type declarations are expanded at the point at which the type is
9439 -- frozen. The formal N is the Freeze_Node for the type. Any statements or
9440 -- declarations generated by the freezing (e.g. the procedure generated
9441 -- for initialization) are chained in the Actions field list of the freeze
9442 -- node using Append_Freeze_Actions.
9443
9444 -- WARNING: This routine manages Ghost regions. Return statements must be
9445 -- replaced by gotos which jump to the end of the routine and restore the
9446 -- Ghost mode.
9447
9448 function Freeze_Type (N : Node_Id) return Boolean is
9449 procedure Process_RACW_Types (Typ : Entity_Id);
9450 -- Validate and generate stubs for all RACW types associated with type
9451 -- Typ.
9452
9453 ------------------------
9454 -- Process_RACW_Types --
9455 ------------------------
9456
9457 procedure Process_RACW_Types (Typ : Entity_Id) is
9458 List : constant Elist_Id := Access_Types_To_Process (N);
9459 E : Elmt_Id;
9460 Seen : Boolean := False;
9461
9462 begin
9463 if Present (List) then
9464 E := First_Elmt (List);
9465 while Present (E) loop
9466 if Is_Remote_Access_To_Class_Wide_Type (Node (E)) then
9467 Validate_RACW_Primitives (Node (E));
9468 Seen := True;
9469 end if;
9470
9471 Next_Elmt (E);
9472 end loop;
9473 end if;
9474
9475 -- If there are RACWs designating this type, make stubs now
9476
9477 if Seen then
9478 Remote_Types_Tagged_Full_View_Encountered (Typ);
9479 end if;
9480 end Process_RACW_Types;
9481
9482 -- Local variables
9483
9484 Def_Id : constant Entity_Id := Entity (N);
9485
9486 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
9487 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
9488 -- Save the Ghost-related attributes to restore on exit
9489
9490 Result : Boolean := False;
9491
9492 -- Start of processing for Freeze_Type
9493
9494 begin
9495 -- The type being frozen may be subject to pragma Ghost. Set the mode
9496 -- now to ensure that any nodes generated during freezing are properly
9497 -- marked as Ghost.
9498
9499 Set_Ghost_Mode (Def_Id);
9500
9501 -- Process any remote access-to-class-wide types designating the type
9502 -- being frozen.
9503
9504 Process_RACW_Types (Def_Id);
9505
9506 -- Freeze processing for record types
9507
9508 if Is_Record_Type (Def_Id) then
9509 if Ekind (Def_Id) = E_Record_Type then
9510 Expand_Freeze_Record_Type (N);
9511 elsif Is_Class_Wide_Type (Def_Id) then
9512 Expand_Freeze_Class_Wide_Type (N);
9513 end if;
9514
9515 -- Freeze processing for array types
9516
9517 elsif Is_Array_Type (Def_Id) then
9518 Expand_Freeze_Array_Type (N);
9519
9520 -- Freeze processing for access types
9521
9522 -- For pool-specific access types, find out the pool object used for
9523 -- this type, needs actual expansion of it in some cases. Here are the
9524 -- different cases :
9525
9526 -- 1. Rep Clause "for Def_Id'Storage_Size use 0;"
9527 -- ---> don't use any storage pool
9528
9529 -- 2. Rep Clause : for Def_Id'Storage_Size use Expr.
9530 -- Expand:
9531 -- Def_Id__Pool : Stack_Bounded_Pool (Expr, DT'Size, DT'Alignment);
9532
9533 -- 3. Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
9534 -- ---> Storage Pool is the specified one
9535
9536 -- See GNAT Pool packages in the Run-Time for more details
9537
9538 elsif Ekind (Def_Id) in E_Access_Type | E_General_Access_Type then
9539 declare
9540 Loc : constant Source_Ptr := Sloc (N);
9541 Desig_Type : constant Entity_Id := Designated_Type (Def_Id);
9542
9543 Freeze_Action_Typ : Entity_Id;
9544 Pool_Object : Entity_Id;
9545
9546 begin
9547 -- Case 1
9548
9549 -- Rep Clause "for Def_Id'Storage_Size use 0;"
9550 -- ---> don't use any storage pool
9551
9552 if No_Pool_Assigned (Def_Id) then
9553 null;
9554
9555 -- Case 2
9556
9557 -- Rep Clause : for Def_Id'Storage_Size use Expr.
9558 -- ---> Expand:
9559 -- Def_Id__Pool : Stack_Bounded_Pool
9560 -- (Expr, DT'Size, DT'Alignment);
9561
9562 elsif Has_Storage_Size_Clause (Def_Id) then
9563 declare
9564 DT_Align : Node_Id;
9565 DT_Size : Node_Id;
9566
9567 begin
9568 -- For unconstrained composite types we give a size of zero
9569 -- so that the pool knows that it needs a special algorithm
9570 -- for variable size object allocation.
9571
9572 if Is_Composite_Type (Desig_Type)
9573 and then not Is_Constrained (Desig_Type)
9574 then
9575 DT_Size := Make_Integer_Literal (Loc, 0);
9576 DT_Align := Make_Integer_Literal (Loc, Maximum_Alignment);
9577
9578 else
9579 DT_Size :=
9580 Make_Attribute_Reference (Loc,
9581 Prefix => New_Occurrence_Of (Desig_Type, Loc),
9582 Attribute_Name => Name_Max_Size_In_Storage_Elements);
9583
9584 DT_Align :=
9585 Make_Attribute_Reference (Loc,
9586 Prefix => New_Occurrence_Of (Desig_Type, Loc),
9587 Attribute_Name => Name_Alignment);
9588 end if;
9589
9590 Pool_Object :=
9591 Make_Defining_Identifier (Loc,
9592 Chars => New_External_Name (Chars (Def_Id), 'P'));
9593
9594 -- We put the code associated with the pools in the entity
9595 -- that has the later freeze node, usually the access type
9596 -- but it can also be the designated_type; because the pool
9597 -- code requires both those types to be frozen
9598
9599 if Is_Frozen (Desig_Type)
9600 and then (No (Freeze_Node (Desig_Type))
9601 or else Analyzed (Freeze_Node (Desig_Type)))
9602 then
9603 Freeze_Action_Typ := Def_Id;
9604
9605 -- A Taft amendment type cannot get the freeze actions
9606 -- since the full view is not there.
9607
9608 elsif Is_Incomplete_Or_Private_Type (Desig_Type)
9609 and then No (Full_View (Desig_Type))
9610 then
9611 Freeze_Action_Typ := Def_Id;
9612
9613 else
9614 Freeze_Action_Typ := Desig_Type;
9615 end if;
9616
9617 Append_Freeze_Action (Freeze_Action_Typ,
9618 Make_Object_Declaration (Loc,
9619 Defining_Identifier => Pool_Object,
9620 Object_Definition =>
9621 Make_Subtype_Indication (Loc,
9622 Subtype_Mark =>
9623 New_Occurrence_Of
9624 (RTE (RE_Stack_Bounded_Pool), Loc),
9625
9626 Constraint =>
9627 Make_Index_Or_Discriminant_Constraint (Loc,
9628 Constraints => New_List (
9629
9630 -- First discriminant is the Pool Size
9631
9632 New_Occurrence_Of (
9633 Storage_Size_Variable (Def_Id), Loc),
9634
9635 -- Second discriminant is the element size
9636
9637 DT_Size,
9638
9639 -- Third discriminant is the alignment
9640
9641 DT_Align)))));
9642 end;
9643
9644 Set_Associated_Storage_Pool (Def_Id, Pool_Object);
9645
9646 -- Case 3
9647
9648 -- Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
9649 -- ---> Storage Pool is the specified one
9650
9651 -- When compiling in Ada 2012 mode, ensure that the accessibility
9652 -- level of the subpool access type is not deeper than that of the
9653 -- pool_with_subpools.
9654
9655 elsif Ada_Version >= Ada_2012
9656 and then Present (Associated_Storage_Pool (Def_Id))
9657 and then RTU_Loaded (System_Storage_Pools_Subpools)
9658 then
9659 declare
9660 Loc : constant Source_Ptr := Sloc (Def_Id);
9661 Pool : constant Entity_Id :=
9662 Associated_Storage_Pool (Def_Id);
9663
9664 begin
9665 -- It is known that the accessibility level of the access
9666 -- type is deeper than that of the pool.
9667
9668 if Type_Access_Level (Def_Id)
9669 > Static_Accessibility_Level (Pool, Object_Decl_Level)
9670 and then Is_Class_Wide_Type (Etype (Pool))
9671 and then not Accessibility_Checks_Suppressed (Def_Id)
9672 and then not Accessibility_Checks_Suppressed (Pool)
9673 then
9674 -- When the pool is of a class-wide type, it may or may
9675 -- not support subpools depending on the path of
9676 -- derivation. Generate:
9677
9678 -- if Def_Id in RSPWS'Class then
9679 -- raise Program_Error;
9680 -- end if;
9681
9682 Append_Freeze_Action (Def_Id,
9683 Make_If_Statement (Loc,
9684 Condition =>
9685 Make_In (Loc,
9686 Left_Opnd => New_Occurrence_Of (Pool, Loc),
9687 Right_Opnd =>
9688 New_Occurrence_Of
9689 (Class_Wide_Type
9690 (RTE
9691 (RE_Root_Storage_Pool_With_Subpools)),
9692 Loc)),
9693 Then_Statements => New_List (
9694 Make_Raise_Program_Error (Loc,
9695 Reason => PE_Accessibility_Check_Failed))));
9696 end if;
9697 end;
9698 end if;
9699
9700 -- For access-to-controlled types (including class-wide types and
9701 -- Taft-amendment types, which potentially have controlled
9702 -- components), expand the list controller object that will store
9703 -- the dynamically allocated objects. Don't do this transformation
9704 -- for expander-generated access types, except do it for types
9705 -- that are the full view of types derived from other private
9706 -- types and for access types used to implement indirect temps.
9707 -- Also suppress the list controller in the case of a designated
9708 -- type with convention Java, since this is used when binding to
9709 -- Java API specs, where there's no equivalent of a finalization
9710 -- list and we don't want to pull in the finalization support if
9711 -- not needed.
9712
9713 if not Comes_From_Source (Def_Id)
9714 and then not Has_Private_Declaration (Def_Id)
9715 and then not Old_Attr_Util.Indirect_Temps
9716 .Is_Access_Type_For_Indirect_Temp (Def_Id)
9717 then
9718 null;
9719
9720 -- An exception is made for types defined in the run-time because
9721 -- Ada.Tags.Tag itself is such a type and cannot afford this
9722 -- unnecessary overhead that would generates a loop in the
9723 -- expansion scheme. Another exception is if Restrictions
9724 -- (No_Finalization) is active, since then we know nothing is
9725 -- controlled.
9726
9727 elsif Restriction_Active (No_Finalization)
9728 or else In_Runtime (Def_Id)
9729 then
9730 null;
9731
9732 -- Create a finalization collection for an access-to-controlled
9733 -- type or an access-to-incomplete type. It is assumed that the
9734 -- full view will be controlled.
9735
9736 elsif Needs_Finalization (Desig_Type)
9737 or else (Is_Incomplete_Type (Desig_Type)
9738 and then No (Full_View (Desig_Type)))
9739 then
9740 Build_Finalization_Collection (Def_Id);
9741
9742 -- Also create a finalization collection when the designated type
9743 -- contains a private component. It is assumed that the full view
9744 -- will be controlled.
9745
9746 elsif Has_Private_Component (Desig_Type) then
9747 Build_Finalization_Collection
9748 (Typ => Def_Id,
9749 For_Private => True,
9750 Context_Scope => Scope (Def_Id),
9751 Insertion_Node => Declaration_Node (Desig_Type));
9752 end if;
9753 end;
9754
9755 -- Freeze processing for enumeration types
9756
9757 elsif Ekind (Def_Id) = E_Enumeration_Type then
9758
9759 -- We only have something to do if we have a non-standard
9760 -- representation (i.e. at least one literal whose pos value
9761 -- is not the same as its representation)
9762
9763 if Has_Non_Standard_Rep (Def_Id) then
9764 Expand_Freeze_Enumeration_Type (N);
9765 end if;
9766
9767 -- Private types that are completed by a derivation from a private
9768 -- type have an internally generated full view, that needs to be
9769 -- frozen. This must be done explicitly because the two views share
9770 -- the freeze node, and the underlying full view is not visible when
9771 -- the freeze node is analyzed.
9772
9773 elsif Is_Private_Type (Def_Id)
9774 and then Is_Derived_Type (Def_Id)
9775 and then Present (Full_View (Def_Id))
9776 and then Is_Itype (Full_View (Def_Id))
9777 and then Has_Private_Declaration (Full_View (Def_Id))
9778 and then Freeze_Node (Full_View (Def_Id)) = N
9779 then
9780 Set_Entity (N, Full_View (Def_Id));
9781 Result := Freeze_Type (N);
9782 Set_Entity (N, Def_Id);
9783
9784 -- All other types require no expander action. There are such cases
9785 -- (e.g. task types and protected types). In such cases, the freeze
9786 -- nodes are there for use by Gigi.
9787
9788 end if;
9789
9790 Freeze_Stream_Operations (N, Def_Id);
9791
9792 -- Generate the [spec and] body of the invariant procedure tasked with
9793 -- the runtime verification of all invariants that pertain to the type.
9794 -- This includes invariants on the partial and full view, inherited
9795 -- class-wide invariants from parent types or interfaces, and invariants
9796 -- on array elements or record components. But skip internal types.
9797
9798 if Is_Itype (Def_Id) then
9799 null;
9800
9801 elsif Is_Interface (Def_Id) then
9802
9803 -- Interfaces are treated as the partial view of a private type in
9804 -- order to achieve uniformity with the general case. As a result, an
9805 -- interface receives only a "partial" invariant procedure which is
9806 -- never called.
9807
9808 if Has_Own_Invariants (Def_Id) then
9809 Build_Invariant_Procedure_Body
9810 (Typ => Def_Id,
9811 Partial_Invariant => Is_Interface (Def_Id));
9812 end if;
9813
9814 -- Non-interface types
9815
9816 -- Do not generate invariant procedure within other assertion
9817 -- subprograms, which may involve local declarations of local
9818 -- subtypes to which these checks do not apply.
9819
9820 else
9821 if Has_Invariants (Def_Id) then
9822 if not Predicate_Check_In_Scope (Def_Id)
9823 or else (Ekind (Current_Scope) = E_Function
9824 and then Is_Predicate_Function (Current_Scope))
9825 then
9826 null;
9827 else
9828 Build_Invariant_Procedure_Body (Def_Id);
9829 end if;
9830 end if;
9831
9832 -- Generate the [spec and] body of the procedure tasked with the
9833 -- run-time verification of pragma Default_Initial_Condition's
9834 -- expression.
9835
9836 if Has_DIC (Def_Id) then
9837 Build_DIC_Procedure_Body (Def_Id);
9838 end if;
9839 end if;
9840
9841 Restore_Ghost_Region (Saved_GM, Saved_IGR);
9842
9843 return Result;
9844
9845 exception
9846 when RE_Not_Available =>
9847 Restore_Ghost_Region (Saved_GM, Saved_IGR);
9848
9849 return False;
9850 end Freeze_Type;
9851
9852 -------------------------
9853 -- Get_Simple_Init_Val --
9854 -------------------------
9855
9856 function Get_Simple_Init_Val
9857 (Typ : Entity_Id;
9858 N : Node_Id;
9859 Size : Uint := No_Uint) return Node_Id
9860 is
9861 IV_Attribute : constant Boolean :=
9862 Nkind (N) = N_Attribute_Reference
9863 and then Attribute_Name (N) = Name_Invalid_Value;
9864
9865 Loc : constant Source_Ptr := Sloc (N);
9866
9867 procedure Extract_Subtype_Bounds
9868 (Lo_Bound : out Uint;
9869 Hi_Bound : out Uint);
9870 -- Inspect subtype Typ as well its ancestor subtypes and derived types
9871 -- to determine the best known information about the bounds of the type.
9872 -- The output parameters are set as follows:
9873 --
9874 -- * Lo_Bound - Set to No_Unit when there is no information available,
9875 -- or to the known low bound.
9876 --
9877 -- * Hi_Bound - Set to No_Unit when there is no information available,
9878 -- or to the known high bound.
9879
9880 function Simple_Init_Array_Type return Node_Id;
9881 -- Build an expression to initialize array type Typ
9882
9883 function Simple_Init_Defaulted_Type return Node_Id;
9884 -- Build an expression to initialize type Typ which is subject to
9885 -- aspect Default_Value.
9886
9887 function Simple_Init_Initialize_Scalars_Type
9888 (Size_To_Use : Uint) return Node_Id;
9889 -- Build an expression to initialize scalar type Typ which is subject to
9890 -- pragma Initialize_Scalars. Size_To_Use is the size of the object.
9891
9892 function Simple_Init_Normalize_Scalars_Type
9893 (Size_To_Use : Uint) return Node_Id;
9894 -- Build an expression to initialize scalar type Typ which is subject to
9895 -- pragma Normalize_Scalars. Size_To_Use is the size of the object.
9896
9897 function Simple_Init_Private_Type return Node_Id;
9898 -- Build an expression to initialize private type Typ
9899
9900 function Simple_Init_Scalar_Type return Node_Id;
9901 -- Build an expression to initialize scalar type Typ
9902
9903 ----------------------------
9904 -- Extract_Subtype_Bounds --
9905 ----------------------------
9906
9907 procedure Extract_Subtype_Bounds
9908 (Lo_Bound : out Uint;
9909 Hi_Bound : out Uint)
9910 is
9911 ST1 : Entity_Id;
9912 ST2 : Entity_Id;
9913 Lo : Node_Id;
9914 Hi : Node_Id;
9915 Lo_Val : Uint;
9916 Hi_Val : Uint;
9917
9918 begin
9919 Lo_Bound := No_Uint;
9920 Hi_Bound := No_Uint;
9921
9922 -- Loop to climb ancestor subtypes and derived types
9923
9924 ST1 := Typ;
9925 loop
9926 if not Is_Discrete_Type (ST1) then
9927 return;
9928 end if;
9929
9930 Lo := Type_Low_Bound (ST1);
9931 Hi := Type_High_Bound (ST1);
9932
9933 if Compile_Time_Known_Value (Lo) then
9934 Lo_Val := Expr_Value (Lo);
9935
9936 if No (Lo_Bound) or else Lo_Bound < Lo_Val then
9937 Lo_Bound := Lo_Val;
9938 end if;
9939 end if;
9940
9941 if Compile_Time_Known_Value (Hi) then
9942 Hi_Val := Expr_Value (Hi);
9943
9944 if No (Hi_Bound) or else Hi_Bound > Hi_Val then
9945 Hi_Bound := Hi_Val;
9946 end if;
9947 end if;
9948
9949 ST2 := Ancestor_Subtype (ST1);
9950
9951 if No (ST2) then
9952 ST2 := Etype (ST1);
9953 end if;
9954
9955 exit when ST1 = ST2;
9956 ST1 := ST2;
9957 end loop;
9958 end Extract_Subtype_Bounds;
9959
9960 ----------------------------
9961 -- Simple_Init_Array_Type --
9962 ----------------------------
9963
9964 function Simple_Init_Array_Type return Node_Id is
9965 Comp_Typ : constant Entity_Id := Component_Type (Typ);
9966
9967 function Simple_Init_Dimension (Index : Node_Id) return Node_Id;
9968 -- Initialize a single array dimension with index constraint Index
9969
9970 --------------------
9971 -- Simple_Init_Dimension --
9972 --------------------
9973
9974 function Simple_Init_Dimension (Index : Node_Id) return Node_Id is
9975 begin
9976 -- Process the current dimension
9977
9978 if Present (Index) then
9979
9980 -- Build a suitable "others" aggregate for the next dimension,
9981 -- or initialize the component itself. Generate:
9982 --
9983 -- (others => ...)
9984
9985 return
9986 Make_Aggregate (Loc,
9987 Component_Associations => New_List (
9988 Make_Component_Association (Loc,
9989 Choices => New_List (Make_Others_Choice (Loc)),
9990 Expression =>
9991 Simple_Init_Dimension (Next_Index (Index)))));
9992
9993 -- Otherwise all dimensions have been processed. Initialize the
9994 -- component itself.
9995
9996 else
9997 return
9998 Get_Simple_Init_Val
9999 (Typ => Comp_Typ,
10000 N => N,
10001 Size => Esize (Comp_Typ));
10002 end if;
10003 end Simple_Init_Dimension;
10004
10005 -- Start of processing for Simple_Init_Array_Type
10006
10007 begin
10008 return Simple_Init_Dimension (First_Index (Typ));
10009 end Simple_Init_Array_Type;
10010
10011 --------------------------------
10012 -- Simple_Init_Defaulted_Type --
10013 --------------------------------
10014
10015 function Simple_Init_Defaulted_Type return Node_Id is
10016 Subtyp : Entity_Id := First_Subtype (Typ);
10017
10018 begin
10019 -- When the first subtype is private, retrieve the expression of the
10020 -- Default_Value from the underlying type.
10021
10022 if Is_Private_Type (Subtyp) then
10023 Subtyp := Full_View (Subtyp);
10024 end if;
10025
10026 -- Use the Sloc of the context node when constructing the initial
10027 -- value because the expression of Default_Value may come from a
10028 -- different unit. Updating the Sloc will result in accurate error
10029 -- diagnostics.
10030
10031 return
10032 OK_Convert_To
10033 (Typ => Typ,
10034 Expr =>
10035 New_Copy_Tree
10036 (Source => Default_Aspect_Value (Subtyp),
10037 New_Sloc => Loc));
10038 end Simple_Init_Defaulted_Type;
10039
10040 -----------------------------------------
10041 -- Simple_Init_Initialize_Scalars_Type --
10042 -----------------------------------------
10043
10044 function Simple_Init_Initialize_Scalars_Type
10045 (Size_To_Use : Uint) return Node_Id
10046 is
10047 Float_Typ : Entity_Id;
10048 Hi_Bound : Uint;
10049 Lo_Bound : Uint;
10050 Scal_Typ : Scalar_Id;
10051
10052 begin
10053 Extract_Subtype_Bounds (Lo_Bound, Hi_Bound);
10054
10055 -- Float types
10056
10057 if Is_Floating_Point_Type (Typ) then
10058 Float_Typ := Root_Type (Typ);
10059
10060 if Float_Typ = Standard_Short_Float then
10061 Scal_Typ := Name_Short_Float;
10062 elsif Float_Typ = Standard_Float then
10063 Scal_Typ := Name_Float;
10064 elsif Float_Typ = Standard_Long_Float then
10065 Scal_Typ := Name_Long_Float;
10066 else pragma Assert (Float_Typ = Standard_Long_Long_Float);
10067 Scal_Typ := Name_Long_Long_Float;
10068 end if;
10069
10070 -- If zero is invalid, it is a convenient value to use that is for
10071 -- sure an appropriate invalid value in all situations.
10072
10073 elsif Present (Lo_Bound) and then Lo_Bound > Uint_0 then
10074 return Make_Integer_Literal (Loc, 0);
10075
10076 -- Unsigned types
10077
10078 elsif Is_Unsigned_Type (Typ) then
10079 if Size_To_Use <= 8 then
10080 Scal_Typ := Name_Unsigned_8;
10081 elsif Size_To_Use <= 16 then
10082 Scal_Typ := Name_Unsigned_16;
10083 elsif Size_To_Use <= 32 then
10084 Scal_Typ := Name_Unsigned_32;
10085 elsif Size_To_Use <= 64 then
10086 Scal_Typ := Name_Unsigned_64;
10087 else
10088 Scal_Typ := Name_Unsigned_128;
10089 end if;
10090
10091 -- Signed types
10092
10093 else
10094 if Size_To_Use <= 8 then
10095 Scal_Typ := Name_Signed_8;
10096 elsif Size_To_Use <= 16 then
10097 Scal_Typ := Name_Signed_16;
10098 elsif Size_To_Use <= 32 then
10099 Scal_Typ := Name_Signed_32;
10100 elsif Size_To_Use <= 64 then
10101 Scal_Typ := Name_Signed_64;
10102 else
10103 Scal_Typ := Name_Signed_128;
10104 end if;
10105 end if;
10106
10107 -- Use the values specified by pragma Initialize_Scalars or the ones
10108 -- provided by the binder. Higher precedence is given to the pragma.
10109
10110 return Invalid_Scalar_Value (Loc, Scal_Typ);
10111 end Simple_Init_Initialize_Scalars_Type;
10112
10113 ----------------------------------------
10114 -- Simple_Init_Normalize_Scalars_Type --
10115 ----------------------------------------
10116
10117 function Simple_Init_Normalize_Scalars_Type
10118 (Size_To_Use : Uint) return Node_Id
10119 is
10120 Signed_Size : constant Uint := UI_Min (Uint_63, Size_To_Use - 1);
10121
10122 Expr : Node_Id;
10123 Hi_Bound : Uint;
10124 Lo_Bound : Uint;
10125
10126 begin
10127 Extract_Subtype_Bounds (Lo_Bound, Hi_Bound);
10128
10129 -- If zero is invalid, it is a convenient value to use that is for
10130 -- sure an appropriate invalid value in all situations.
10131
10132 if Present (Lo_Bound) and then Lo_Bound > Uint_0 then
10133 Expr := Make_Integer_Literal (Loc, 0);
10134
10135 -- Cases where all one bits is the appropriate invalid value
10136
10137 -- For modular types, all 1 bits is either invalid or valid. If it
10138 -- is valid, then there is nothing that can be done since there are
10139 -- no invalid values (we ruled out zero already).
10140
10141 -- For signed integer types that have no negative values, either
10142 -- there is room for negative values, or there is not. If there
10143 -- is, then all 1-bits may be interpreted as minus one, which is
10144 -- certainly invalid. Alternatively it is treated as the largest
10145 -- positive value, in which case the observation for modular types
10146 -- still applies.
10147
10148 -- For float types, all 1-bits is a NaN (not a number), which is
10149 -- certainly an appropriately invalid value.
10150
10151 elsif Is_Enumeration_Type (Typ)
10152 or else Is_Floating_Point_Type (Typ)
10153 or else Is_Unsigned_Type (Typ)
10154 then
10155 Expr := Make_Integer_Literal (Loc, 2 ** Size_To_Use - 1);
10156
10157 -- Resolve as Long_Long_Long_Unsigned, because the largest number
10158 -- we can generate is out of range of universal integer.
10159
10160 Analyze_And_Resolve (Expr, Standard_Long_Long_Long_Unsigned);
10161
10162 -- Case of signed types
10163
10164 else
10165 -- Normally we like to use the most negative number. The one
10166 -- exception is when this number is in the known subtype range and
10167 -- the largest positive number is not in the known subtype range.
10168
10169 -- For this exceptional case, use largest positive value
10170
10171 if Present (Lo_Bound) and then Present (Hi_Bound)
10172 and then Lo_Bound <= (-(2 ** Signed_Size))
10173 and then Hi_Bound < 2 ** Signed_Size
10174 then
10175 Expr := Make_Integer_Literal (Loc, 2 ** Signed_Size - 1);
10176
10177 -- Normal case of largest negative value
10178
10179 else
10180 Expr := Make_Integer_Literal (Loc, -(2 ** Signed_Size));
10181 end if;
10182 end if;
10183
10184 return Expr;
10185 end Simple_Init_Normalize_Scalars_Type;
10186
10187 ------------------------------
10188 -- Simple_Init_Private_Type --
10189 ------------------------------
10190
10191 function Simple_Init_Private_Type return Node_Id is
10192 Under_Typ : constant Entity_Id := Underlying_Type (Typ);
10193 Expr : Node_Id;
10194
10195 begin
10196 -- The availability of the underlying view must be checked by routine
10197 -- Needs_Simple_Initialization.
10198
10199 pragma Assert (Present (Under_Typ));
10200
10201 Expr := Get_Simple_Init_Val (Under_Typ, N, Size);
10202
10203 -- If the initial value is null or an aggregate, qualify it with the
10204 -- underlying type in order to provide a proper context.
10205
10206 if Nkind (Expr) in N_Aggregate | N_Null then
10207 Expr :=
10208 Make_Qualified_Expression (Loc,
10209 Subtype_Mark => New_Occurrence_Of (Under_Typ, Loc),
10210 Expression => Expr);
10211 end if;
10212
10213 Expr := Unchecked_Convert_To (Typ, Expr);
10214
10215 -- Do not truncate the result when scalar types are involved and
10216 -- Initialize/Normalize_Scalars is in effect.
10217
10218 if Nkind (Expr) = N_Unchecked_Type_Conversion
10219 and then Is_Scalar_Type (Under_Typ)
10220 then
10221 Set_Kill_Range_Check (Expr);
10222 Set_No_Truncation (Expr);
10223 end if;
10224
10225 return Expr;
10226 end Simple_Init_Private_Type;
10227
10228 -----------------------------
10229 -- Simple_Init_Scalar_Type --
10230 -----------------------------
10231
10232 function Simple_Init_Scalar_Type return Node_Id is
10233 Expr : Node_Id;
10234 Size_To_Use : Uint;
10235
10236 begin
10237 pragma Assert (Init_Or_Norm_Scalars or IV_Attribute);
10238
10239 -- Determine the size of the object. This is either the size provided
10240 -- by the caller, or the Esize of the scalar type.
10241
10242 if No (Size) or else Size <= Uint_0 then
10243 Size_To_Use := UI_Max (Uint_1, Esize (Typ));
10244 else
10245 Size_To_Use := Size;
10246 end if;
10247
10248 -- The maximum size to use is System_Max_Integer_Size bits. This
10249 -- will create values of type Long_Long_Long_Unsigned and the range
10250 -- must fit this type.
10251
10252 if Present (Size_To_Use)
10253 and then Size_To_Use > System_Max_Integer_Size
10254 then
10255 Size_To_Use := UI_From_Int (System_Max_Integer_Size);
10256 end if;
10257
10258 if Normalize_Scalars and then not IV_Attribute then
10259 Expr := Simple_Init_Normalize_Scalars_Type (Size_To_Use);
10260 else
10261 Expr := Simple_Init_Initialize_Scalars_Type (Size_To_Use);
10262 end if;
10263
10264 -- The final expression is obtained by doing an unchecked conversion
10265 -- of this result to the base type of the required subtype. Use the
10266 -- base type to prevent the unchecked conversion from chopping bits,
10267 -- and then we set Kill_Range_Check to preserve the "bad" value.
10268
10269 Expr := Unchecked_Convert_To (Base_Type (Typ), Expr);
10270
10271 -- Ensure that the expression is not truncated since the "bad" bits
10272 -- are desired, and also kill the range checks.
10273
10274 if Nkind (Expr) = N_Unchecked_Type_Conversion then
10275 Set_Kill_Range_Check (Expr);
10276 Set_No_Truncation (Expr);
10277 end if;
10278
10279 return Expr;
10280 end Simple_Init_Scalar_Type;
10281
10282 -- Start of processing for Get_Simple_Init_Val
10283
10284 begin
10285 if Is_Private_Type (Typ) then
10286 return Simple_Init_Private_Type;
10287
10288 elsif Is_Scalar_Type (Typ) then
10289 if Has_Default_Aspect (Typ) then
10290 return Simple_Init_Defaulted_Type;
10291 else
10292 return Simple_Init_Scalar_Type;
10293 end if;
10294
10295 -- Array type with Initialize or Normalize_Scalars
10296
10297 elsif Is_Array_Type (Typ) then
10298 pragma Assert (Init_Or_Norm_Scalars);
10299 return Simple_Init_Array_Type;
10300
10301 -- Access type is initialized to null
10302
10303 elsif Is_Access_Type (Typ) then
10304 return Make_Null (Loc);
10305
10306 -- No other possibilities should arise, since we should only be calling
10307 -- Get_Simple_Init_Val if Needs_Simple_Initialization returned True,
10308 -- indicating one of the above cases held.
10309
10310 else
10311 raise Program_Error;
10312 end if;
10313
10314 exception
10315 when RE_Not_Available =>
10316 return Empty;
10317 end Get_Simple_Init_Val;
10318
10319 ------------------------------
10320 -- Has_New_Non_Standard_Rep --
10321 ------------------------------
10322
10323 function Has_New_Non_Standard_Rep (T : Entity_Id) return Boolean is
10324 begin
10325 if not Is_Derived_Type (T) then
10326 return Has_Non_Standard_Rep (T)
10327 or else Has_Non_Standard_Rep (Root_Type (T));
10328
10329 -- If Has_Non_Standard_Rep is not set on the derived type, the
10330 -- representation is fully inherited.
10331
10332 elsif not Has_Non_Standard_Rep (T) then
10333 return False;
10334
10335 else
10336 return First_Rep_Item (T) /= First_Rep_Item (Root_Type (T));
10337
10338 -- May need a more precise check here: the First_Rep_Item may be a
10339 -- stream attribute, which does not affect the representation of the
10340 -- type ???
10341
10342 end if;
10343 end Has_New_Non_Standard_Rep;
10344
10345 ----------------------
10346 -- Inline_Init_Proc --
10347 ----------------------
10348
10349 function Inline_Init_Proc (Typ : Entity_Id) return Boolean is
10350 begin
10351 -- The initialization proc of protected records is not worth inlining.
10352 -- In addition, when compiled for another unit for inlining purposes,
10353 -- it may make reference to entities that have not been elaborated yet.
10354 -- The initialization proc of records that need finalization contains
10355 -- a nested clean-up procedure that makes it impractical to inline as
10356 -- well, except for simple controlled types themselves. And similar
10357 -- considerations apply to task types.
10358
10359 if Is_Concurrent_Type (Typ) then
10360 return False;
10361
10362 elsif Needs_Finalization (Typ) and then not Is_Controlled (Typ) then
10363 return False;
10364
10365 elsif Has_Task (Typ) then
10366 return False;
10367
10368 else
10369 return True;
10370 end if;
10371 end Inline_Init_Proc;
10372
10373 ----------------
10374 -- In_Runtime --
10375 ----------------
10376
10377 function In_Runtime (E : Entity_Id) return Boolean is
10378 S1 : Entity_Id;
10379
10380 begin
10381 S1 := Scope (E);
10382 while Scope (S1) /= Standard_Standard loop
10383 S1 := Scope (S1);
10384 end loop;
10385
10386 return Is_RTU (S1, System) or else Is_RTU (S1, Ada);
10387 end In_Runtime;
10388
10389 package body Initialization_Control is
10390
10391 ------------------------
10392 -- Requires_Late_Init --
10393 ------------------------
10394
10395 function Requires_Late_Init
10396 (Decl : Node_Id;
10397 Rec_Type : Entity_Id) return Boolean
10398 is
10399 function Is_Access_Discriminant
10400 (N : Node_Id) return Traverse_Result;
10401 -- Look for a name denoting an access discriminant
10402
10403 function Is_Current_Instance
10404 (N : Node_Id) return Traverse_Result;
10405 -- Look for a reference to the current instance of the type
10406
10407 function Is_Internal_Call
10408 (N : Node_Id) return Traverse_Result;
10409 -- Look for an internal protected function call
10410
10411 ----------------------------
10412 -- Is_Access_Discriminant --
10413 ----------------------------
10414
10415 function Is_Access_Discriminant
10416 (N : Node_Id) return Traverse_Result is
10417 begin
10418 if Is_Entity_Name (N)
10419 and then Denotes_Discriminant (N)
10420 and then Is_Access_Type (Etype (N))
10421 then
10422 return Abandon;
10423 else
10424 return OK;
10425 end if;
10426 end Is_Access_Discriminant;
10427
10428 -------------------------
10429 -- Is_Current_Instance --
10430 -------------------------
10431
10432 function Is_Current_Instance
10433 (N : Node_Id) return Traverse_Result is
10434 begin
10435 if Is_Entity_Name (N)
10436 and then Present (Entity (N))
10437 and then Is_Current_Instance (N)
10438 then
10439 return Abandon;
10440 else
10441 return OK;
10442 end if;
10443 end Is_Current_Instance;
10444
10445 ----------------------
10446 -- Is_Internal_Call --
10447 ----------------------
10448
10449 function Is_Internal_Call (N : Node_Id) return Traverse_Result is
10450
10451 function Call_Scope (N : Node_Id) return Entity_Id;
10452 -- Return the scope enclosing a given call node N
10453
10454 ----------------
10455 -- Call_Scope --
10456 ----------------
10457
10458 function Call_Scope (N : Node_Id) return Entity_Id is
10459 Nam : constant Node_Id := Name (N);
10460 begin
10461 if Nkind (Nam) = N_Selected_Component then
10462 return Scope (Entity (Prefix (Nam)));
10463 else
10464 return Scope (Entity (Nam));
10465 end if;
10466 end Call_Scope;
10467
10468 begin
10469 if Nkind (N) = N_Function_Call
10470 and then Call_Scope (N)
10471 = Corresponding_Concurrent_Type (Rec_Type)
10472 then
10473 return Abandon;
10474 else
10475 return OK;
10476 end if;
10477 end Is_Internal_Call;
10478
10479 function Search_Access_Discriminant is new
10480 Traverse_Func (Is_Access_Discriminant);
10481
10482 function Search_Current_Instance is new
10483 Traverse_Func (Is_Current_Instance);
10484
10485 function Search_Internal_Call is new
10486 Traverse_Func (Is_Internal_Call);
10487
10488 -- Start of processing for Requires_Late_Init
10489
10490 begin
10491 -- A component of an object is said to require late initialization
10492 -- if:
10493
10494 -- it has an access discriminant value constrained by a per-object
10495 -- expression;
10496
10497 if Has_Access_Constraint (Defining_Identifier (Decl))
10498 and then No (Expression (Decl))
10499 then
10500 return True;
10501
10502 elsif Present (Expression (Decl)) then
10503
10504 -- it has an initialization expression that includes a name
10505 -- denoting an access discriminant;
10506
10507 if Search_Access_Discriminant (Expression (Decl)) = Abandon then
10508 return True;
10509 end if;
10510
10511 -- or it has an initialization expression that includes a
10512 -- reference to the current instance of the type either by
10513 -- name...
10514
10515 if Search_Current_Instance (Expression (Decl)) = Abandon then
10516 return True;
10517 end if;
10518
10519 -- ...or implicitly as the target object of a call.
10520
10521 if Is_Protected_Record_Type (Rec_Type) then
10522 if Search_Internal_Call (Expression (Decl)) = Abandon then
10523 return True;
10524 end if;
10525 end if;
10526 end if;
10527
10528 return False;
10529 end Requires_Late_Init;
10530
10531 -----------------------------
10532 -- Has_Late_Init_Component --
10533 -----------------------------
10534
10535 function Has_Late_Init_Component
10536 (Tagged_Rec_Type : Entity_Id) return Boolean
10537 is
10538 Comp_Id : Entity_Id :=
10539 First_Component (Implementation_Base_Type (Tagged_Rec_Type));
10540 begin
10541 while Present (Comp_Id) loop
10542 if Requires_Late_Init (Decl => Parent (Comp_Id),
10543 Rec_Type => Tagged_Rec_Type)
10544 then
10545 return True; -- found a component that requires late init
10546
10547 elsif Chars (Comp_Id) = Name_uParent
10548 and then Has_Late_Init_Component (Etype (Comp_Id))
10549 then
10550 return True; -- an ancestor type has a late init component
10551 end if;
10552
10553 Next_Component (Comp_Id);
10554 end loop;
10555
10556 return False;
10557 end Has_Late_Init_Component;
10558
10559 ------------------------
10560 -- Tag_Init_Condition --
10561 ------------------------
10562
10563 function Tag_Init_Condition
10564 (Loc : Source_Ptr;
10565 Init_Control_Formal : Entity_Id) return Node_Id is
10566 begin
10567 return Make_Op_Eq (Loc,
10568 New_Occurrence_Of (Init_Control_Formal, Loc),
10569 Make_Mode_Literal (Loc, Full_Init));
10570 end Tag_Init_Condition;
10571
10572 --------------------------
10573 -- Early_Init_Condition --
10574 --------------------------
10575
10576 function Early_Init_Condition
10577 (Loc : Source_Ptr;
10578 Init_Control_Formal : Entity_Id) return Node_Id is
10579 begin
10580 return Make_Op_Ne (Loc,
10581 New_Occurrence_Of (Init_Control_Formal, Loc),
10582 Make_Mode_Literal (Loc, Late_Init_Only));
10583 end Early_Init_Condition;
10584
10585 -------------------------
10586 -- Late_Init_Condition --
10587 -------------------------
10588
10589 function Late_Init_Condition
10590 (Loc : Source_Ptr;
10591 Init_Control_Formal : Entity_Id) return Node_Id is
10592 begin
10593 return Make_Op_Ne (Loc,
10594 New_Occurrence_Of (Init_Control_Formal, Loc),
10595 Make_Mode_Literal (Loc, Early_Init_Only));
10596 end Late_Init_Condition;
10597
10598 end Initialization_Control;
10599
10600 ----------------------------
10601 -- Initialization_Warning --
10602 ----------------------------
10603
10604 procedure Initialization_Warning (E : Entity_Id) is
10605 Warning_Needed : Boolean;
10606
10607 begin
10608 Warning_Needed := False;
10609
10610 if Ekind (Current_Scope) = E_Package
10611 and then Static_Elaboration_Desired (Current_Scope)
10612 then
10613 if Is_Type (E) then
10614 if Is_Record_Type (E) then
10615 if Has_Discriminants (E)
10616 or else Is_Limited_Type (E)
10617 or else Has_Non_Standard_Rep (E)
10618 then
10619 Warning_Needed := True;
10620
10621 else
10622 -- Verify that at least one component has an initialization
10623 -- expression. No need for a warning on a type if all its
10624 -- components have no initialization.
10625
10626 declare
10627 Comp : Entity_Id;
10628
10629 begin
10630 Comp := First_Component (E);
10631 while Present (Comp) loop
10632 pragma Assert
10633 (Nkind (Parent (Comp)) = N_Component_Declaration);
10634
10635 if Present (Expression (Parent (Comp))) then
10636 Warning_Needed := True;
10637 exit;
10638 end if;
10639
10640 Next_Component (Comp);
10641 end loop;
10642 end;
10643 end if;
10644
10645 if Warning_Needed then
10646 Error_Msg_N
10647 ("objects of the type cannot be initialized statically "
10648 & "by default??", Parent (E));
10649 end if;
10650 end if;
10651
10652 else
10653 Error_Msg_N ("object cannot be initialized statically??", E);
10654 end if;
10655 end if;
10656 end Initialization_Warning;
10657
10658 ------------------
10659 -- Init_Formals --
10660 ------------------
10661
10662 function Init_Formals (Typ : Entity_Id; Proc_Id : Entity_Id) return List_Id
10663 is
10664 Loc : constant Source_Ptr := Sloc (Typ);
10665 Unc_Arr : constant Boolean :=
10666 Is_Array_Type (Typ) and then not Is_Constrained (Typ);
10667 With_Prot : constant Boolean :=
10668 Has_Protected (Typ)
10669 or else (Is_Record_Type (Typ)
10670 and then Is_Protected_Record_Type (Typ));
10671 With_Task : constant Boolean :=
10672 Has_Task (Typ)
10673 or else (Is_Record_Type (Typ)
10674 and then Is_Task_Record_Type (Typ));
10675 Formals : List_Id;
10676
10677 begin
10678 -- The first parameter is always _Init : [in] out Typ. Note that we need
10679 -- it to be in/out in the case of an unconstrained array, because of the
10680 -- need to have the bounds, and in the case of protected or task record
10681 -- value, because there are default record fields that may be referenced
10682 -- in the generated initialization routine.
10683
10684 Formals := New_List (
10685 Make_Parameter_Specification (Loc,
10686 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uInit),
10687 In_Present => Unc_Arr or else With_Prot or else With_Task,
10688 Out_Present => True,
10689 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
10690
10691 -- For task record value, or type that contains tasks, add two more
10692 -- formals, _Master : Master_Id and _Chain : in out Activation_Chain
10693 -- We also add these parameters for the task record type case.
10694
10695 if With_Task then
10696 Append_To (Formals,
10697 Make_Parameter_Specification (Loc,
10698 Defining_Identifier =>
10699 Make_Defining_Identifier (Loc, Name_uMaster),
10700 Parameter_Type =>
10701 New_Occurrence_Of (Standard_Integer, Loc)));
10702
10703 Set_Has_Master_Entity (Proc_Id);
10704
10705 -- Add _Chain (not done for sequential elaboration policy, see
10706 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
10707
10708 if Partition_Elaboration_Policy /= 'S' then
10709 Append_To (Formals,
10710 Make_Parameter_Specification (Loc,
10711 Defining_Identifier =>
10712 Make_Defining_Identifier (Loc, Name_uChain),
10713 In_Present => True,
10714 Out_Present => True,
10715 Parameter_Type =>
10716 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc)));
10717 end if;
10718
10719 Append_To (Formals,
10720 Make_Parameter_Specification (Loc,
10721 Defining_Identifier =>
10722 Make_Defining_Identifier (Loc, Name_uTask_Name),
10723 In_Present => True,
10724 Parameter_Type => New_Occurrence_Of (Standard_String, Loc)));
10725 end if;
10726
10727 -- Due to certain edge cases such as arrays with null-excluding
10728 -- components being built with the secondary stack it becomes necessary
10729 -- to add a formal to the Init_Proc which controls whether we raise
10730 -- Constraint_Errors on generated calls for internal object
10731 -- declarations.
10732
10733 if Needs_Conditional_Null_Excluding_Check (Typ) then
10734 Append_To (Formals,
10735 Make_Parameter_Specification (Loc,
10736 Defining_Identifier =>
10737 Make_Defining_Identifier (Loc,
10738 New_External_Name (Chars
10739 (Component_Type (Typ)), "_skip_null_excluding_check")),
10740 Expression => New_Occurrence_Of (Standard_False, Loc),
10741 In_Present => True,
10742 Parameter_Type =>
10743 New_Occurrence_Of (Standard_Boolean, Loc)));
10744 end if;
10745
10746 return Formals;
10747
10748 exception
10749 when RE_Not_Available =>
10750 return Empty_List;
10751 end Init_Formals;
10752
10753 -------------------------
10754 -- Init_Secondary_Tags --
10755 -------------------------
10756
10757 procedure Init_Secondary_Tags
10758 (Typ : Entity_Id;
10759 Target : Node_Id;
10760 Init_Tags_List : List_Id;
10761 Stmts_List : List_Id;
10762 Fixed_Comps : Boolean := True;
10763 Variable_Comps : Boolean := True)
10764 is
10765 Loc : constant Source_Ptr := Sloc (Target);
10766
10767 -- Inherit the C++ tag of the secondary dispatch table of Typ associated
10768 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
10769
10770 procedure Initialize_Tag
10771 (Typ : Entity_Id;
10772 Iface : Entity_Id;
10773 Tag_Comp : Entity_Id;
10774 Iface_Tag : Node_Id);
10775 -- Initialize the tag of the secondary dispatch table of Typ associated
10776 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
10777 -- Compiling under the CPP full ABI compatibility mode, if the ancestor
10778 -- of Typ CPP tagged type we generate code to inherit the contents of
10779 -- the dispatch table directly from the ancestor.
10780
10781 --------------------
10782 -- Initialize_Tag --
10783 --------------------
10784
10785 procedure Initialize_Tag
10786 (Typ : Entity_Id;
10787 Iface : Entity_Id;
10788 Tag_Comp : Entity_Id;
10789 Iface_Tag : Node_Id)
10790 is
10791 Comp_Typ : Entity_Id;
10792 Offset_To_Top_Comp : Entity_Id := Empty;
10793
10794 begin
10795 -- Initialize pointer to secondary DT associated with the interface
10796
10797 if not Is_Ancestor (Iface, Typ, Use_Full_View => True) then
10798 Append_To (Init_Tags_List,
10799 Make_Assignment_Statement (Loc,
10800 Name =>
10801 Make_Selected_Component (Loc,
10802 Prefix => New_Copy_Tree (Target),
10803 Selector_Name => New_Occurrence_Of (Tag_Comp, Loc)),
10804 Expression =>
10805 New_Occurrence_Of (Iface_Tag, Loc)));
10806 end if;
10807
10808 Comp_Typ := Scope (Tag_Comp);
10809
10810 -- Initialize the entries of the table of interfaces. We generate a
10811 -- different call when the parent of the type has variable size
10812 -- components.
10813
10814 if Comp_Typ /= Etype (Comp_Typ)
10815 and then Is_Variable_Size_Record (Etype (Comp_Typ))
10816 and then Chars (Tag_Comp) /= Name_uTag
10817 then
10818 pragma Assert (Present (DT_Offset_To_Top_Func (Tag_Comp)));
10819
10820 -- Issue error if Set_Dynamic_Offset_To_Top is not available in a
10821 -- configurable run-time environment.
10822
10823 if not RTE_Available (RE_Set_Dynamic_Offset_To_Top) then
10824 Error_Msg_CRT
10825 ("variable size record with interface types", Typ);
10826 return;
10827 end if;
10828
10829 -- Generate:
10830 -- Set_Dynamic_Offset_To_Top
10831 -- (This => Init,
10832 -- Prim_T => Typ'Tag,
10833 -- Interface_T => Iface'Tag,
10834 -- Offset_Value => n,
10835 -- Offset_Func => Fn'Unrestricted_Access)
10836
10837 Append_To (Stmts_List,
10838 Make_Procedure_Call_Statement (Loc,
10839 Name =>
10840 New_Occurrence_Of (RTE (RE_Set_Dynamic_Offset_To_Top), Loc),
10841 Parameter_Associations => New_List (
10842 Make_Attribute_Reference (Loc,
10843 Prefix => New_Copy_Tree (Target),
10844 Attribute_Name => Name_Address),
10845
10846 Unchecked_Convert_To (RTE (RE_Tag),
10847 New_Occurrence_Of
10848 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc)),
10849
10850 Unchecked_Convert_To (RTE (RE_Tag),
10851 New_Occurrence_Of
10852 (Node (First_Elmt (Access_Disp_Table (Iface))),
10853 Loc)),
10854
10855 Unchecked_Convert_To
10856 (RTE (RE_Storage_Offset),
10857 Make_Op_Minus (Loc,
10858 Make_Attribute_Reference (Loc,
10859 Prefix =>
10860 Make_Selected_Component (Loc,
10861 Prefix => New_Copy_Tree (Target),
10862 Selector_Name =>
10863 New_Occurrence_Of (Tag_Comp, Loc)),
10864 Attribute_Name => Name_Position))),
10865
10866 Unchecked_Convert_To (RTE (RE_Offset_To_Top_Function_Ptr),
10867 Make_Attribute_Reference (Loc,
10868 Prefix => New_Occurrence_Of
10869 (DT_Offset_To_Top_Func (Tag_Comp), Loc),
10870 Attribute_Name => Name_Unrestricted_Access)))));
10871
10872 -- In this case the next component stores the value of the offset
10873 -- to the top.
10874
10875 Offset_To_Top_Comp := Next_Entity (Tag_Comp);
10876 pragma Assert (Present (Offset_To_Top_Comp));
10877
10878 Append_To (Init_Tags_List,
10879 Make_Assignment_Statement (Loc,
10880 Name =>
10881 Make_Selected_Component (Loc,
10882 Prefix => New_Copy_Tree (Target),
10883 Selector_Name =>
10884 New_Occurrence_Of (Offset_To_Top_Comp, Loc)),
10885
10886 Expression =>
10887 Make_Op_Minus (Loc,
10888 Make_Attribute_Reference (Loc,
10889 Prefix =>
10890 Make_Selected_Component (Loc,
10891 Prefix => New_Copy_Tree (Target),
10892 Selector_Name => New_Occurrence_Of (Tag_Comp, Loc)),
10893 Attribute_Name => Name_Position))));
10894
10895 -- Normal case: No discriminants in the parent type
10896
10897 else
10898 -- Don't need to set any value if the offset-to-top field is
10899 -- statically set or if this interface shares the primary
10900 -- dispatch table.
10901
10902 if not Building_Static_Secondary_DT (Typ)
10903 and then not Is_Ancestor (Iface, Typ, Use_Full_View => True)
10904 then
10905 Append_To (Stmts_List,
10906 Build_Set_Static_Offset_To_Top (Loc,
10907 Iface_Tag => New_Occurrence_Of (Iface_Tag, Loc),
10908 Offset_Value =>
10909 Unchecked_Convert_To (RTE (RE_Storage_Offset),
10910 Make_Op_Minus (Loc,
10911 Make_Attribute_Reference (Loc,
10912 Prefix =>
10913 Make_Selected_Component (Loc,
10914 Prefix => New_Copy_Tree (Target),
10915 Selector_Name =>
10916 New_Occurrence_Of (Tag_Comp, Loc)),
10917 Attribute_Name => Name_Position)))));
10918 end if;
10919
10920 -- Generate:
10921 -- Register_Interface_Offset
10922 -- (Prim_T => Typ'Tag,
10923 -- Interface_T => Iface'Tag,
10924 -- Is_Constant => True,
10925 -- Offset_Value => n,
10926 -- Offset_Func => null);
10927
10928 if not Building_Static_Secondary_DT (Typ)
10929 and then RTE_Available (RE_Register_Interface_Offset)
10930 then
10931 Append_To (Stmts_List,
10932 Make_Procedure_Call_Statement (Loc,
10933 Name =>
10934 New_Occurrence_Of
10935 (RTE (RE_Register_Interface_Offset), Loc),
10936 Parameter_Associations => New_List (
10937 Unchecked_Convert_To (RTE (RE_Tag),
10938 New_Occurrence_Of
10939 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc)),
10940
10941 Unchecked_Convert_To (RTE (RE_Tag),
10942 New_Occurrence_Of
10943 (Node (First_Elmt (Access_Disp_Table (Iface))), Loc)),
10944
10945 New_Occurrence_Of (Standard_True, Loc),
10946
10947 Unchecked_Convert_To (RTE (RE_Storage_Offset),
10948 Make_Op_Minus (Loc,
10949 Make_Attribute_Reference (Loc,
10950 Prefix =>
10951 Make_Selected_Component (Loc,
10952 Prefix => New_Copy_Tree (Target),
10953 Selector_Name =>
10954 New_Occurrence_Of (Tag_Comp, Loc)),
10955 Attribute_Name => Name_Position))),
10956
10957 Make_Null (Loc))));
10958 end if;
10959 end if;
10960 end Initialize_Tag;
10961
10962 -- Local variables
10963
10964 Full_Typ : Entity_Id;
10965 Ifaces_List : Elist_Id;
10966 Ifaces_Comp_List : Elist_Id;
10967 Ifaces_Tag_List : Elist_Id;
10968 Iface_Elmt : Elmt_Id;
10969 Iface_Comp_Elmt : Elmt_Id;
10970 Iface_Tag_Elmt : Elmt_Id;
10971 Tag_Comp : Node_Id;
10972 In_Variable_Pos : Boolean;
10973
10974 -- Start of processing for Init_Secondary_Tags
10975
10976 begin
10977 -- Handle private types
10978
10979 if Present (Full_View (Typ)) then
10980 Full_Typ := Full_View (Typ);
10981 else
10982 Full_Typ := Typ;
10983 end if;
10984
10985 Collect_Interfaces_Info
10986 (Full_Typ, Ifaces_List, Ifaces_Comp_List, Ifaces_Tag_List);
10987
10988 Iface_Elmt := First_Elmt (Ifaces_List);
10989 Iface_Comp_Elmt := First_Elmt (Ifaces_Comp_List);
10990 Iface_Tag_Elmt := First_Elmt (Ifaces_Tag_List);
10991 while Present (Iface_Elmt) loop
10992 Tag_Comp := Node (Iface_Comp_Elmt);
10993
10994 -- Check if parent of record type has variable size components
10995
10996 In_Variable_Pos := Scope (Tag_Comp) /= Etype (Scope (Tag_Comp))
10997 and then Is_Variable_Size_Record (Etype (Scope (Tag_Comp)));
10998
10999 -- If we are compiling under the CPP full ABI compatibility mode and
11000 -- the ancestor is a CPP_Pragma tagged type then we generate code to
11001 -- initialize the secondary tag components from tags that reference
11002 -- secondary tables filled with copy of parent slots.
11003
11004 if Is_CPP_Class (Root_Type (Full_Typ)) then
11005
11006 -- Reject interface components located at variable offset in
11007 -- C++ derivations. This is currently unsupported.
11008
11009 if not Fixed_Comps and then In_Variable_Pos then
11010
11011 -- Locate the first dynamic component of the record. Done to
11012 -- improve the text of the warning.
11013
11014 declare
11015 Comp : Entity_Id;
11016 Comp_Typ : Entity_Id;
11017
11018 begin
11019 Comp := First_Entity (Typ);
11020 while Present (Comp) loop
11021 Comp_Typ := Etype (Comp);
11022
11023 if Ekind (Comp) /= E_Discriminant
11024 and then not Is_Tag (Comp)
11025 then
11026 exit when
11027 (Is_Record_Type (Comp_Typ)
11028 and then
11029 Is_Variable_Size_Record (Base_Type (Comp_Typ)))
11030 or else
11031 (Is_Array_Type (Comp_Typ)
11032 and then Is_Variable_Size_Array (Comp_Typ));
11033 end if;
11034
11035 Next_Entity (Comp);
11036 end loop;
11037
11038 pragma Assert (Present (Comp));
11039
11040 -- Move this check to sem???
11041 Error_Msg_Node_2 := Comp;
11042 Error_Msg_NE
11043 ("parent type & with dynamic component & cannot be parent"
11044 & " of 'C'P'P derivation if new interfaces are present",
11045 Typ, Scope (Original_Record_Component (Comp)));
11046
11047 Error_Msg_Sloc :=
11048 Sloc (Scope (Original_Record_Component (Comp)));
11049 Error_Msg_NE
11050 ("type derived from 'C'P'P type & defined #",
11051 Typ, Scope (Original_Record_Component (Comp)));
11052
11053 -- Avoid duplicated warnings
11054
11055 exit;
11056 end;
11057
11058 -- Initialize secondary tags
11059
11060 else
11061 Initialize_Tag
11062 (Typ => Full_Typ,
11063 Iface => Node (Iface_Elmt),
11064 Tag_Comp => Tag_Comp,
11065 Iface_Tag => Node (Iface_Tag_Elmt));
11066 end if;
11067
11068 -- Otherwise generate code to initialize the tag
11069
11070 else
11071 if (In_Variable_Pos and then Variable_Comps)
11072 or else (not In_Variable_Pos and then Fixed_Comps)
11073 then
11074 Initialize_Tag
11075 (Typ => Full_Typ,
11076 Iface => Node (Iface_Elmt),
11077 Tag_Comp => Tag_Comp,
11078 Iface_Tag => Node (Iface_Tag_Elmt));
11079 end if;
11080 end if;
11081
11082 Next_Elmt (Iface_Elmt);
11083 Next_Elmt (Iface_Comp_Elmt);
11084 Next_Elmt (Iface_Tag_Elmt);
11085 end loop;
11086 end Init_Secondary_Tags;
11087
11088 ----------------------------
11089 -- Is_Null_Statement_List --
11090 ----------------------------
11091
11092 function Is_Null_Statement_List (Stmts : List_Id) return Boolean is
11093 Stmt : Node_Id;
11094
11095 begin
11096 -- We must skip SCIL nodes because they may have been added to the list
11097 -- by Insert_Actions.
11098
11099 Stmt := First_Non_SCIL_Node (Stmts);
11100 while Present (Stmt) loop
11101 if Nkind (Stmt) = N_Case_Statement then
11102 declare
11103 Alt : Node_Id;
11104 begin
11105 Alt := First (Alternatives (Stmt));
11106 while Present (Alt) loop
11107 if not Is_Null_Statement_List (Statements (Alt)) then
11108 return False;
11109 end if;
11110
11111 Next (Alt);
11112 end loop;
11113 end;
11114
11115 elsif Nkind (Stmt) /= N_Null_Statement then
11116 return False;
11117 end if;
11118
11119 Stmt := Next_Non_SCIL_Node (Stmt);
11120 end loop;
11121
11122 return True;
11123 end Is_Null_Statement_List;
11124
11125 ----------------------------------------
11126 -- Make_Controlling_Function_Wrappers --
11127 ----------------------------------------
11128
11129 procedure Make_Controlling_Function_Wrappers
11130 (Tag_Typ : Entity_Id;
11131 Decl_List : out List_Id;
11132 Body_List : out List_Id)
11133 is
11134 Loc : constant Source_Ptr := Sloc (Tag_Typ);
11135
11136 function Make_Wrapper_Specification (Subp : Entity_Id) return Node_Id;
11137 -- Returns a function specification with the same profile as Subp
11138
11139 --------------------------------
11140 -- Make_Wrapper_Specification --
11141 --------------------------------
11142
11143 function Make_Wrapper_Specification (Subp : Entity_Id) return Node_Id is
11144 begin
11145 return
11146 Make_Function_Specification (Loc,
11147 Defining_Unit_Name =>
11148 Make_Defining_Identifier (Loc,
11149 Chars => Chars (Subp)),
11150 Parameter_Specifications =>
11151 Copy_Parameter_List (Subp),
11152 Result_Definition =>
11153 New_Occurrence_Of (Etype (Subp), Loc));
11154 end Make_Wrapper_Specification;
11155
11156 Prim_Elmt : Elmt_Id;
11157 Subp : Entity_Id;
11158 Actual_List : List_Id;
11159 Formal : Entity_Id;
11160 Par_Formal : Entity_Id;
11161 Ext_Aggr : Node_Id;
11162 Formal_Node : Node_Id;
11163 Func_Body : Node_Id;
11164 Func_Decl : Node_Id;
11165 Func_Id : Entity_Id;
11166
11167 -- Start of processing for Make_Controlling_Function_Wrappers
11168
11169 begin
11170 Decl_List := New_List;
11171 Body_List := New_List;
11172
11173 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
11174 while Present (Prim_Elmt) loop
11175 Subp := Node (Prim_Elmt);
11176
11177 -- If a primitive function with a controlling result of the type has
11178 -- not been overridden by the user, then we must create a wrapper
11179 -- function here that effectively overrides it and invokes the
11180 -- (non-abstract) parent function. This can only occur for a null
11181 -- extension. Note that functions with anonymous controlling access
11182 -- results don't qualify and must be overridden. We also exclude
11183 -- Input attributes, since each type will have its own version of
11184 -- Input constructed by the expander. The test for Comes_From_Source
11185 -- is needed to distinguish inherited operations from renamings
11186 -- (which also have Alias set). We exclude internal entities with
11187 -- Interface_Alias to avoid generating duplicated wrappers since
11188 -- the primitive which covers the interface is also available in
11189 -- the list of primitive operations.
11190
11191 -- The function may be abstract, or require_Overriding may be set
11192 -- for it, because tests for null extensions may already have reset
11193 -- the Is_Abstract_Subprogram_Flag. If Requires_Overriding is not
11194 -- set, functions that need wrappers are recognized by having an
11195 -- alias that returns the parent type.
11196
11197 if Comes_From_Source (Subp)
11198 or else No (Alias (Subp))
11199 or else Present (Interface_Alias (Subp))
11200 or else Ekind (Subp) /= E_Function
11201 or else not Has_Controlling_Result (Subp)
11202 or else Is_Access_Type (Etype (Subp))
11203 or else Is_Abstract_Subprogram (Alias (Subp))
11204 or else Is_TSS (Subp, TSS_Stream_Input)
11205 then
11206 goto Next_Prim;
11207
11208 elsif Is_Abstract_Subprogram (Subp)
11209 or else Requires_Overriding (Subp)
11210 or else
11211 (Is_Null_Extension (Etype (Subp))
11212 and then Etype (Alias (Subp)) /= Etype (Subp))
11213 then
11214 -- If there is a non-overloadable homonym in the current
11215 -- scope, the implicit declaration remains invisible.
11216 -- We check the current entity with the same name, or its
11217 -- homonym in case the derivation takes place after the
11218 -- hiding object declaration.
11219
11220 if Present (Current_Entity (Subp)) then
11221 declare
11222 Curr : constant Entity_Id := Current_Entity (Subp);
11223 Prev : constant Entity_Id := Homonym (Curr);
11224 begin
11225 if (Comes_From_Source (Curr)
11226 and then Scope (Curr) = Current_Scope
11227 and then not Is_Overloadable (Curr))
11228 or else
11229 (Present (Prev)
11230 and then Comes_From_Source (Prev)
11231 and then Scope (Prev) = Current_Scope
11232 and then not Is_Overloadable (Prev))
11233 then
11234 goto Next_Prim;
11235 end if;
11236 end;
11237 end if;
11238
11239 Func_Decl :=
11240 Make_Subprogram_Declaration (Loc,
11241 Specification => Make_Wrapper_Specification (Subp));
11242
11243 Append_To (Decl_List, Func_Decl);
11244
11245 -- Build a wrapper body that calls the parent function. The body
11246 -- contains a single return statement that returns an extension
11247 -- aggregate whose ancestor part is a call to the parent function,
11248 -- passing the formals as actuals (with any controlling arguments
11249 -- converted to the types of the corresponding formals of the
11250 -- parent function, which might be anonymous access types), and
11251 -- having a null extension.
11252
11253 Formal := First_Formal (Subp);
11254 Par_Formal := First_Formal (Alias (Subp));
11255 Formal_Node :=
11256 First (Parameter_Specifications (Specification (Func_Decl)));
11257
11258 if Present (Formal) then
11259 Actual_List := New_List;
11260
11261 while Present (Formal) loop
11262 if Is_Controlling_Formal (Formal) then
11263 Append_To (Actual_List,
11264 Make_Type_Conversion (Loc,
11265 Subtype_Mark =>
11266 New_Occurrence_Of (Etype (Par_Formal), Loc),
11267 Expression =>
11268 New_Occurrence_Of
11269 (Defining_Identifier (Formal_Node), Loc)));
11270 else
11271 Append_To
11272 (Actual_List,
11273 New_Occurrence_Of
11274 (Defining_Identifier (Formal_Node), Loc));
11275 end if;
11276
11277 Next_Formal (Formal);
11278 Next_Formal (Par_Formal);
11279 Next (Formal_Node);
11280 end loop;
11281 else
11282 Actual_List := No_List;
11283 end if;
11284
11285 Ext_Aggr :=
11286 Make_Extension_Aggregate (Loc,
11287 Ancestor_Part =>
11288 Make_Function_Call (Loc,
11289 Name =>
11290 New_Occurrence_Of (Alias (Subp), Loc),
11291 Parameter_Associations => Actual_List),
11292 Null_Record_Present => True);
11293
11294 -- GNATprove will use expression of an expression function as an
11295 -- implicit postcondition. GNAT will also benefit from expression
11296 -- function to avoid premature freezing, but would struggle if we
11297 -- added an expression function to freezing actions, so we create
11298 -- the expanded form directly.
11299
11300 if GNATprove_Mode then
11301 Func_Body :=
11302 Make_Expression_Function (Loc,
11303 Specification =>
11304 Make_Wrapper_Specification (Subp),
11305 Expression => Ext_Aggr);
11306 else
11307 Func_Body :=
11308 Make_Subprogram_Body (Loc,
11309 Specification =>
11310 Make_Wrapper_Specification (Subp),
11311 Declarations => Empty_List,
11312 Handled_Statement_Sequence =>
11313 Make_Handled_Sequence_Of_Statements (Loc,
11314 Statements => New_List (
11315 Make_Simple_Return_Statement (Loc,
11316 Expression => Ext_Aggr))));
11317 Set_Was_Expression_Function (Func_Body);
11318 end if;
11319
11320 Append_To (Body_List, Func_Body);
11321
11322 -- Replace the inherited function with the wrapper function in the
11323 -- primitive operations list. We add the minimum decoration needed
11324 -- to override interface primitives.
11325
11326 Func_Id := Defining_Unit_Name (Specification (Func_Decl));
11327
11328 Mutate_Ekind (Func_Id, E_Function);
11329 Set_Is_Wrapper (Func_Id);
11330
11331 -- Corresponding_Spec will be set again to the same value during
11332 -- analysis, but we need this information earlier.
11333 -- Expand_N_Freeze_Entity needs to know whether a subprogram body
11334 -- is a wrapper's body in order to get check suppression right.
11335
11336 Set_Corresponding_Spec (Func_Body, Func_Id);
11337 end if;
11338
11339 <<Next_Prim>>
11340 Next_Elmt (Prim_Elmt);
11341 end loop;
11342 end Make_Controlling_Function_Wrappers;
11343
11344 ------------------
11345 -- Make_Eq_Body --
11346 ------------------
11347
11348 function Make_Eq_Body
11349 (Typ : Entity_Id;
11350 Eq_Name : Name_Id) return Node_Id
11351 is
11352 Loc : constant Source_Ptr := Sloc (Parent (Typ));
11353 Decl : Node_Id;
11354 Def : constant Node_Id := Parent (Typ);
11355 Stmts : constant List_Id := New_List;
11356 Variant_Case : Boolean := Has_Discriminants (Typ);
11357 Comps : Node_Id := Empty;
11358 Typ_Def : Node_Id := Type_Definition (Def);
11359
11360 begin
11361 Decl :=
11362 Predef_Spec_Or_Body (Loc,
11363 Tag_Typ => Typ,
11364 Name => Eq_Name,
11365 Profile => New_List (
11366 Make_Parameter_Specification (Loc,
11367 Defining_Identifier =>
11368 Make_Defining_Identifier (Loc, Name_X),
11369 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
11370
11371 Make_Parameter_Specification (Loc,
11372 Defining_Identifier =>
11373 Make_Defining_Identifier (Loc, Name_Y),
11374 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
11375
11376 Ret_Type => Standard_Boolean,
11377 For_Body => True);
11378
11379 if Variant_Case then
11380 if Nkind (Typ_Def) = N_Derived_Type_Definition then
11381 Typ_Def := Record_Extension_Part (Typ_Def);
11382 end if;
11383
11384 if Present (Typ_Def) then
11385 Comps := Component_List (Typ_Def);
11386 end if;
11387
11388 Variant_Case :=
11389 Present (Comps) and then Present (Variant_Part (Comps));
11390 end if;
11391
11392 if Variant_Case then
11393 Append_To (Stmts,
11394 Make_Eq_If (Typ, Discriminant_Specifications (Def)));
11395 Append_List_To (Stmts, Make_Eq_Case (Typ, Comps));
11396 Append_To (Stmts,
11397 Make_Simple_Return_Statement (Loc,
11398 Expression => New_Occurrence_Of (Standard_True, Loc)));
11399
11400 else
11401 Append_To (Stmts,
11402 Make_Simple_Return_Statement (Loc,
11403 Expression =>
11404 Expand_Record_Equality
11405 (Typ,
11406 Typ => Typ,
11407 Lhs => Make_Identifier (Loc, Name_X),
11408 Rhs => Make_Identifier (Loc, Name_Y))));
11409 end if;
11410
11411 Set_Handled_Statement_Sequence
11412 (Decl, Make_Handled_Sequence_Of_Statements (Loc, Stmts));
11413 return Decl;
11414 end Make_Eq_Body;
11415
11416 ------------------
11417 -- Make_Eq_Case --
11418 ------------------
11419
11420 -- <Make_Eq_If shared components>
11421
11422 -- case X.D1 is
11423 -- when V1 => <Make_Eq_Case> on subcomponents
11424 -- ...
11425 -- when Vn => <Make_Eq_Case> on subcomponents
11426 -- end case;
11427
11428 function Make_Eq_Case
11429 (E : Entity_Id;
11430 CL : Node_Id;
11431 Discrs : Elist_Id := New_Elmt_List) return List_Id
11432 is
11433 Loc : constant Source_Ptr := Sloc (E);
11434 Result : constant List_Id := New_List;
11435 Variant : Node_Id;
11436 Alt_List : List_Id;
11437
11438 function Corresponding_Formal (C : Node_Id) return Entity_Id;
11439 -- Given the discriminant that controls a given variant of an unchecked
11440 -- union, find the formal of the equality function that carries the
11441 -- inferred value of the discriminant.
11442
11443 function External_Name (E : Entity_Id) return Name_Id;
11444 -- The value of a given discriminant is conveyed in the corresponding
11445 -- formal parameter of the equality routine. The name of this formal
11446 -- parameter carries a one-character suffix which is removed here.
11447
11448 --------------------------
11449 -- Corresponding_Formal --
11450 --------------------------
11451
11452 function Corresponding_Formal (C : Node_Id) return Entity_Id is
11453 Discr : constant Entity_Id := Entity (Name (Variant_Part (C)));
11454 Elm : Elmt_Id;
11455
11456 begin
11457 Elm := First_Elmt (Discrs);
11458 while Present (Elm) loop
11459 if Chars (Discr) = External_Name (Node (Elm)) then
11460 return Node (Elm);
11461 end if;
11462
11463 Next_Elmt (Elm);
11464 end loop;
11465
11466 -- A formal of the proper name must be found
11467
11468 raise Program_Error;
11469 end Corresponding_Formal;
11470
11471 -------------------
11472 -- External_Name --
11473 -------------------
11474
11475 function External_Name (E : Entity_Id) return Name_Id is
11476 begin
11477 Get_Name_String (Chars (E));
11478 Name_Len := Name_Len - 1;
11479 return Name_Find;
11480 end External_Name;
11481
11482 -- Start of processing for Make_Eq_Case
11483
11484 begin
11485 Append_To (Result, Make_Eq_If (E, Component_Items (CL)));
11486
11487 if No (Variant_Part (CL)) then
11488 return Result;
11489 end if;
11490
11491 Variant := First_Non_Pragma (Variants (Variant_Part (CL)));
11492
11493 if No (Variant) then
11494 return Result;
11495 end if;
11496
11497 Alt_List := New_List;
11498 while Present (Variant) loop
11499 Append_To (Alt_List,
11500 Make_Case_Statement_Alternative (Loc,
11501 Discrete_Choices => New_Copy_List (Discrete_Choices (Variant)),
11502 Statements =>
11503 Make_Eq_Case (E, Component_List (Variant), Discrs)));
11504 Next_Non_Pragma (Variant);
11505 end loop;
11506
11507 -- If we have an Unchecked_Union, use one of the parameters of the
11508 -- enclosing equality routine that captures the discriminant, to use
11509 -- as the expression in the generated case statement.
11510
11511 if Is_Unchecked_Union (E) then
11512 Append_To (Result,
11513 Make_Case_Statement (Loc,
11514 Expression =>
11515 New_Occurrence_Of (Corresponding_Formal (CL), Loc),
11516 Alternatives => Alt_List));
11517
11518 else
11519 Append_To (Result,
11520 Make_Case_Statement (Loc,
11521 Expression =>
11522 Make_Selected_Component (Loc,
11523 Prefix => Make_Identifier (Loc, Name_X),
11524 Selector_Name => New_Copy (Name (Variant_Part (CL)))),
11525 Alternatives => Alt_List));
11526 end if;
11527
11528 return Result;
11529 end Make_Eq_Case;
11530
11531 ----------------
11532 -- Make_Eq_If --
11533 ----------------
11534
11535 -- Generates:
11536
11537 -- if
11538 -- X.C1 /= Y.C1
11539 -- or else
11540 -- X.C2 /= Y.C2
11541 -- ...
11542 -- then
11543 -- return False;
11544 -- end if;
11545
11546 -- or a null statement if the list L is empty
11547
11548 -- Equality may be user-defined for a given component type, in which case
11549 -- a function call is constructed instead of an operator node. This is an
11550 -- Ada 2012 change in the composability of equality for untagged composite
11551 -- types.
11552
11553 function Make_Eq_If
11554 (E : Entity_Id;
11555 L : List_Id) return Node_Id
11556 is
11557 Loc : constant Source_Ptr := Sloc (E);
11558
11559 C : Node_Id;
11560 Cond : Node_Id;
11561 Field_Name : Name_Id;
11562 Next_Test : Node_Id;
11563 Typ : Entity_Id;
11564
11565 begin
11566 if No (L) then
11567 return Make_Null_Statement (Loc);
11568
11569 else
11570 Cond := Empty;
11571
11572 C := First_Non_Pragma (L);
11573 while Present (C) loop
11574 Typ := Etype (Defining_Identifier (C));
11575 Field_Name := Chars (Defining_Identifier (C));
11576
11577 -- The tags must not be compared: they are not part of the value.
11578 -- Ditto for parent interfaces because their equality operator is
11579 -- abstract.
11580
11581 -- Note also that in the following, we use Make_Identifier for
11582 -- the component names. Use of New_Occurrence_Of to identify the
11583 -- components would be incorrect because the wrong entities for
11584 -- discriminants could be picked up in the private type case.
11585
11586 if Field_Name = Name_uParent
11587 and then Is_Interface (Typ)
11588 then
11589 null;
11590
11591 elsif Field_Name /= Name_uTag then
11592 declare
11593 Lhs : constant Node_Id :=
11594 Make_Selected_Component (Loc,
11595 Prefix => Make_Identifier (Loc, Name_X),
11596 Selector_Name => Make_Identifier (Loc, Field_Name));
11597
11598 Rhs : constant Node_Id :=
11599 Make_Selected_Component (Loc,
11600 Prefix => Make_Identifier (Loc, Name_Y),
11601 Selector_Name => Make_Identifier (Loc, Field_Name));
11602 Eq_Call : Node_Id;
11603
11604 begin
11605 -- Build equality code with a user-defined operator, if
11606 -- available, and with the predefined "=" otherwise. For
11607 -- compatibility with older Ada versions, we also use the
11608 -- predefined operation if the component-type equality is
11609 -- abstract, rather than raising Program_Error.
11610
11611 if Ada_Version < Ada_2012 then
11612 Next_Test := Make_Op_Ne (Loc, Lhs, Rhs);
11613
11614 else
11615 Eq_Call := Build_Eq_Call (Typ, Loc, Lhs, Rhs);
11616
11617 if No (Eq_Call) then
11618 Next_Test := Make_Op_Ne (Loc, Lhs, Rhs);
11619
11620 -- If a component has a defined abstract equality, its
11621 -- application raises Program_Error on that component
11622 -- and therefore on the current variant.
11623
11624 elsif Nkind (Eq_Call) = N_Raise_Program_Error then
11625 Set_Etype (Eq_Call, Standard_Boolean);
11626 Next_Test := Make_Op_Not (Loc, Eq_Call);
11627
11628 else
11629 Next_Test := Make_Op_Not (Loc, Eq_Call);
11630 end if;
11631 end if;
11632 end;
11633
11634 Evolve_Or_Else (Cond, Next_Test);
11635 end if;
11636
11637 Next_Non_Pragma (C);
11638 end loop;
11639
11640 if No (Cond) then
11641 return Make_Null_Statement (Loc);
11642
11643 else
11644 return
11645 Make_Implicit_If_Statement (E,
11646 Condition => Cond,
11647 Then_Statements => New_List (
11648 Make_Simple_Return_Statement (Loc,
11649 Expression => New_Occurrence_Of (Standard_False, Loc))));
11650 end if;
11651 end if;
11652 end Make_Eq_If;
11653
11654 -------------------
11655 -- Make_Neq_Body --
11656 -------------------
11657
11658 function Make_Neq_Body (Tag_Typ : Entity_Id) return Node_Id is
11659
11660 function Is_Predefined_Neq_Renaming (Prim : Node_Id) return Boolean;
11661 -- Returns true if Prim is a renaming of an unresolved predefined
11662 -- inequality operation.
11663
11664 --------------------------------
11665 -- Is_Predefined_Neq_Renaming --
11666 --------------------------------
11667
11668 function Is_Predefined_Neq_Renaming (Prim : Node_Id) return Boolean is
11669 begin
11670 return Chars (Prim) /= Name_Op_Ne
11671 and then Present (Alias (Prim))
11672 and then Comes_From_Source (Prim)
11673 and then Is_Intrinsic_Subprogram (Alias (Prim))
11674 and then Chars (Alias (Prim)) = Name_Op_Ne;
11675 end Is_Predefined_Neq_Renaming;
11676
11677 -- Local variables
11678
11679 Loc : constant Source_Ptr := Sloc (Parent (Tag_Typ));
11680 Decl : Node_Id;
11681 Eq_Prim : Entity_Id;
11682 Left_Op : Entity_Id;
11683 Renaming_Prim : Entity_Id;
11684 Right_Op : Entity_Id;
11685 Target : Entity_Id;
11686
11687 -- Start of processing for Make_Neq_Body
11688
11689 begin
11690 -- For a call on a renaming of a dispatching subprogram that is
11691 -- overridden, if the overriding occurred before the renaming, then
11692 -- the body executed is that of the overriding declaration, even if the
11693 -- overriding declaration is not visible at the place of the renaming;
11694 -- otherwise, the inherited or predefined subprogram is called, see
11695 -- (RM 8.5.4(8)).
11696
11697 -- Stage 1: Search for a renaming of the inequality primitive and also
11698 -- search for an overriding of the equality primitive located before the
11699 -- renaming declaration.
11700
11701 declare
11702 Elmt : Elmt_Id;
11703 Prim : Node_Id;
11704
11705 begin
11706 Eq_Prim := Empty;
11707 Renaming_Prim := Empty;
11708
11709 Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
11710 while Present (Elmt) loop
11711 Prim := Node (Elmt);
11712
11713 if Is_User_Defined_Equality (Prim) and then No (Alias (Prim)) then
11714 if No (Renaming_Prim) then
11715 pragma Assert (No (Eq_Prim));
11716 Eq_Prim := Prim;
11717 end if;
11718
11719 elsif Is_Predefined_Neq_Renaming (Prim) then
11720 Renaming_Prim := Prim;
11721 end if;
11722
11723 Next_Elmt (Elmt);
11724 end loop;
11725 end;
11726
11727 -- No further action needed if no renaming was found
11728
11729 if No (Renaming_Prim) then
11730 return Empty;
11731 end if;
11732
11733 -- Stage 2: Replace the renaming declaration by a subprogram declaration
11734 -- (required to add its body)
11735
11736 Decl := Parent (Parent (Renaming_Prim));
11737 Rewrite (Decl,
11738 Make_Subprogram_Declaration (Loc,
11739 Specification => Specification (Decl)));
11740 Set_Analyzed (Decl);
11741
11742 -- Remove the decoration of intrinsic renaming subprogram
11743
11744 Set_Is_Intrinsic_Subprogram (Renaming_Prim, False);
11745 Set_Convention (Renaming_Prim, Convention_Ada);
11746 Set_Alias (Renaming_Prim, Empty);
11747 Set_Has_Completion (Renaming_Prim, False);
11748
11749 -- Stage 3: Build the corresponding body
11750
11751 Left_Op := First_Formal (Renaming_Prim);
11752 Right_Op := Next_Formal (Left_Op);
11753
11754 Decl :=
11755 Predef_Spec_Or_Body (Loc,
11756 Tag_Typ => Tag_Typ,
11757 Name => Chars (Renaming_Prim),
11758 Profile => New_List (
11759 Make_Parameter_Specification (Loc,
11760 Defining_Identifier =>
11761 Make_Defining_Identifier (Loc, Chars (Left_Op)),
11762 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
11763
11764 Make_Parameter_Specification (Loc,
11765 Defining_Identifier =>
11766 Make_Defining_Identifier (Loc, Chars (Right_Op)),
11767 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
11768
11769 Ret_Type => Standard_Boolean,
11770 For_Body => True);
11771
11772 -- If the overriding of the equality primitive occurred before the
11773 -- renaming, then generate:
11774
11775 -- function <Neq_Name> (X : Y : Typ) return Boolean is
11776 -- begin
11777 -- return not Oeq (X, Y);
11778 -- end;
11779
11780 if Present (Eq_Prim) then
11781 Target := Eq_Prim;
11782
11783 -- Otherwise build a nested subprogram which performs the predefined
11784 -- evaluation of the equality operator. That is, generate:
11785
11786 -- function <Neq_Name> (X : Y : Typ) return Boolean is
11787 -- function Oeq (X : Y) return Boolean is
11788 -- begin
11789 -- <<body of default implementation>>
11790 -- end;
11791 -- begin
11792 -- return not Oeq (X, Y);
11793 -- end;
11794
11795 else
11796 declare
11797 Local_Subp : Node_Id;
11798 begin
11799 Local_Subp := Make_Eq_Body (Tag_Typ, Name_Op_Eq);
11800 Set_Declarations (Decl, New_List (Local_Subp));
11801 Target := Defining_Entity (Local_Subp);
11802 end;
11803 end if;
11804
11805 Set_Handled_Statement_Sequence
11806 (Decl,
11807 Make_Handled_Sequence_Of_Statements (Loc, New_List (
11808 Make_Simple_Return_Statement (Loc,
11809 Expression =>
11810 Make_Op_Not (Loc,
11811 Make_Function_Call (Loc,
11812 Name => New_Occurrence_Of (Target, Loc),
11813 Parameter_Associations => New_List (
11814 Make_Identifier (Loc, Chars (Left_Op)),
11815 Make_Identifier (Loc, Chars (Right_Op)))))))));
11816
11817 return Decl;
11818 end Make_Neq_Body;
11819
11820 -------------------------------
11821 -- Make_Null_Procedure_Specs --
11822 -------------------------------
11823
11824 function Make_Null_Procedure_Specs (Tag_Typ : Entity_Id) return List_Id is
11825 Decl_List : constant List_Id := New_List;
11826 Loc : constant Source_Ptr := Sloc (Tag_Typ);
11827 Formal : Entity_Id;
11828 New_Param_Spec : Node_Id;
11829 New_Spec : Node_Id;
11830 Parent_Subp : Entity_Id;
11831 Prim_Elmt : Elmt_Id;
11832 Subp : Entity_Id;
11833
11834 begin
11835 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
11836 while Present (Prim_Elmt) loop
11837 Subp := Node (Prim_Elmt);
11838
11839 -- If a null procedure inherited from an interface has not been
11840 -- overridden, then we build a null procedure declaration to
11841 -- override the inherited procedure.
11842
11843 Parent_Subp := Alias (Subp);
11844
11845 if Present (Parent_Subp)
11846 and then Is_Null_Interface_Primitive (Parent_Subp)
11847 then
11848 -- The null procedure spec is copied from the inherited procedure,
11849 -- except for the IS NULL (which must be added) and the overriding
11850 -- indicators (which must be removed, if present).
11851
11852 New_Spec :=
11853 Copy_Subprogram_Spec (Subprogram_Specification (Subp), Loc);
11854
11855 Set_Null_Present (New_Spec, True);
11856 Set_Must_Override (New_Spec, False);
11857 Set_Must_Not_Override (New_Spec, False);
11858
11859 Formal := First_Formal (Subp);
11860 New_Param_Spec := First (Parameter_Specifications (New_Spec));
11861
11862 while Present (Formal) loop
11863
11864 -- For controlling arguments we must change their parameter
11865 -- type to reference the tagged type (instead of the interface
11866 -- type).
11867
11868 if Is_Controlling_Formal (Formal) then
11869 if Nkind (Parameter_Type (Parent (Formal))) = N_Identifier
11870 then
11871 Set_Parameter_Type (New_Param_Spec,
11872 New_Occurrence_Of (Tag_Typ, Loc));
11873
11874 else pragma Assert
11875 (Nkind (Parameter_Type (Parent (Formal))) =
11876 N_Access_Definition);
11877 Set_Subtype_Mark (Parameter_Type (New_Param_Spec),
11878 New_Occurrence_Of (Tag_Typ, Loc));
11879 end if;
11880 end if;
11881
11882 Next_Formal (Formal);
11883 Next (New_Param_Spec);
11884 end loop;
11885
11886 Append_To (Decl_List,
11887 Make_Subprogram_Declaration (Loc,
11888 Specification => New_Spec));
11889 end if;
11890
11891 Next_Elmt (Prim_Elmt);
11892 end loop;
11893
11894 return Decl_List;
11895 end Make_Null_Procedure_Specs;
11896
11897 ---------------------------------------
11898 -- Make_Predefined_Primitive_Eq_Spec --
11899 ---------------------------------------
11900
11901 procedure Make_Predefined_Primitive_Eq_Spec
11902 (Tag_Typ : Entity_Id;
11903 Predef_List : List_Id;
11904 Renamed_Eq : out Entity_Id)
11905 is
11906 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean;
11907 -- Returns true if Prim is a renaming of an unresolved predefined
11908 -- equality operation.
11909
11910 -------------------------------
11911 -- Is_Predefined_Eq_Renaming --
11912 -------------------------------
11913
11914 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean is
11915 begin
11916 return Chars (Prim) /= Name_Op_Eq
11917 and then Present (Alias (Prim))
11918 and then Comes_From_Source (Prim)
11919 and then Is_Intrinsic_Subprogram (Alias (Prim))
11920 and then Chars (Alias (Prim)) = Name_Op_Eq;
11921 end Is_Predefined_Eq_Renaming;
11922
11923 -- Local variables
11924
11925 Loc : constant Source_Ptr := Sloc (Tag_Typ);
11926
11927 Eq_Name : Name_Id := Name_Op_Eq;
11928 Eq_Needed : Boolean := True;
11929 Eq_Spec : Node_Id;
11930 Prim : Elmt_Id;
11931
11932 Has_Predef_Eq_Renaming : Boolean := False;
11933 -- Set to True if Tag_Typ has a primitive that renames the predefined
11934 -- equality operator. Used to implement (RM 8-5-4(8)).
11935
11936 -- Start of processing for Make_Predefined_Primitive_Specs
11937
11938 begin
11939 Renamed_Eq := Empty;
11940
11941 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
11942 while Present (Prim) loop
11943
11944 -- If a primitive is encountered that renames the predefined equality
11945 -- operator before reaching any explicit equality primitive, then we
11946 -- still need to create a predefined equality function, because calls
11947 -- to it can occur via the renaming. A new name is created for the
11948 -- equality to avoid conflicting with any user-defined equality.
11949 -- (Note that this doesn't account for renamings of equality nested
11950 -- within subpackages???)
11951
11952 if Is_Predefined_Eq_Renaming (Node (Prim)) then
11953 Has_Predef_Eq_Renaming := True;
11954 Eq_Name := New_External_Name (Chars (Node (Prim)), 'E');
11955
11956 -- User-defined equality
11957
11958 elsif Is_User_Defined_Equality (Node (Prim)) then
11959 if No (Alias (Node (Prim)))
11960 or else Nkind (Unit_Declaration_Node (Node (Prim))) =
11961 N_Subprogram_Renaming_Declaration
11962 then
11963 Eq_Needed := False;
11964 exit;
11965
11966 -- If the parent is not an interface type and has an abstract
11967 -- equality function explicitly defined in the sources, then the
11968 -- inherited equality is abstract as well, and no body can be
11969 -- created for it.
11970
11971 elsif not Is_Interface (Etype (Tag_Typ))
11972 and then Present (Alias (Node (Prim)))
11973 and then Comes_From_Source (Alias (Node (Prim)))
11974 and then Is_Abstract_Subprogram (Alias (Node (Prim)))
11975 then
11976 Eq_Needed := False;
11977 exit;
11978
11979 -- If the type has an equality function corresponding with a
11980 -- primitive defined in an interface type, the inherited equality
11981 -- is abstract as well, and no body can be created for it.
11982
11983 elsif Present (Alias (Node (Prim)))
11984 and then Comes_From_Source (Ultimate_Alias (Node (Prim)))
11985 and then
11986 Is_Interface
11987 (Find_Dispatching_Type (Ultimate_Alias (Node (Prim))))
11988 then
11989 Eq_Needed := False;
11990 exit;
11991 end if;
11992 end if;
11993
11994 Next_Elmt (Prim);
11995 end loop;
11996
11997 -- If a renaming of predefined equality was found but there was no
11998 -- user-defined equality (so Eq_Needed is still true), then set the name
11999 -- back to Name_Op_Eq. But in the case where a user-defined equality was
12000 -- located after such a renaming, then the predefined equality function
12001 -- is still needed, so Eq_Needed must be set back to True.
12002
12003 if Eq_Name /= Name_Op_Eq then
12004 if Eq_Needed then
12005 Eq_Name := Name_Op_Eq;
12006 else
12007 Eq_Needed := True;
12008 end if;
12009 end if;
12010
12011 if Eq_Needed then
12012 Eq_Spec := Predef_Spec_Or_Body (Loc,
12013 Tag_Typ => Tag_Typ,
12014 Name => Eq_Name,
12015 Profile => New_List (
12016 Make_Parameter_Specification (Loc,
12017 Defining_Identifier =>
12018 Make_Defining_Identifier (Loc, Name_X),
12019 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
12020
12021 Make_Parameter_Specification (Loc,
12022 Defining_Identifier =>
12023 Make_Defining_Identifier (Loc, Name_Y),
12024 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
12025 Ret_Type => Standard_Boolean);
12026 Append_To (Predef_List, Eq_Spec);
12027
12028 if Has_Predef_Eq_Renaming then
12029 Renamed_Eq := Defining_Unit_Name (Specification (Eq_Spec));
12030
12031 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
12032 while Present (Prim) loop
12033
12034 -- Any renamings of equality that appeared before an overriding
12035 -- equality must be updated to refer to the entity for the
12036 -- predefined equality, otherwise calls via the renaming would
12037 -- get incorrectly resolved to call the user-defined equality
12038 -- function.
12039
12040 if Is_Predefined_Eq_Renaming (Node (Prim)) then
12041 Set_Alias (Node (Prim), Renamed_Eq);
12042
12043 -- Exit upon encountering a user-defined equality
12044
12045 elsif Chars (Node (Prim)) = Name_Op_Eq
12046 and then No (Alias (Node (Prim)))
12047 then
12048 exit;
12049 end if;
12050
12051 Next_Elmt (Prim);
12052 end loop;
12053 end if;
12054 end if;
12055 end Make_Predefined_Primitive_Eq_Spec;
12056
12057 -------------------------------------
12058 -- Make_Predefined_Primitive_Specs --
12059 -------------------------------------
12060
12061 procedure Make_Predefined_Primitive_Specs
12062 (Tag_Typ : Entity_Id;
12063 Predef_List : out List_Id;
12064 Renamed_Eq : out Entity_Id)
12065 is
12066 Loc : constant Source_Ptr := Sloc (Tag_Typ);
12067 Res : constant List_Id := New_List;
12068
12069 use Exp_Put_Image;
12070
12071 begin
12072 Renamed_Eq := Empty;
12073
12074 -- Spec of _Size
12075
12076 Append_To (Res, Predef_Spec_Or_Body (Loc,
12077 Tag_Typ => Tag_Typ,
12078 Name => Name_uSize,
12079 Profile => New_List (
12080 Make_Parameter_Specification (Loc,
12081 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
12082 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
12083
12084 Ret_Type => Standard_Long_Long_Integer));
12085
12086 -- Spec of Put_Image
12087
12088 if not No_Run_Time_Mode
12089 and then RTE_Available (RE_Root_Buffer_Type)
12090 then
12091 -- No_Run_Time_Mode implies that the declaration of Tag_Typ
12092 -- (like any tagged type) will be rejected. Given this, avoid
12093 -- cascading errors associated with the Tag_Typ's TSS_Put_Image
12094 -- procedure.
12095
12096 Append_To (Res, Predef_Spec_Or_Body (Loc,
12097 Tag_Typ => Tag_Typ,
12098 Name => Make_TSS_Name (Tag_Typ, TSS_Put_Image),
12099 Profile => Build_Put_Image_Profile (Loc, Tag_Typ)));
12100 end if;
12101
12102 -- Specs for dispatching stream attributes
12103
12104 declare
12105 Stream_Op_TSS_Names :
12106 constant array (Positive range <>) of TSS_Name_Type :=
12107 (TSS_Stream_Read,
12108 TSS_Stream_Write,
12109 TSS_Stream_Input,
12110 TSS_Stream_Output);
12111
12112 begin
12113 for Op in Stream_Op_TSS_Names'Range loop
12114 if Stream_Operation_OK (Tag_Typ, Stream_Op_TSS_Names (Op)) then
12115 Append_To (Res,
12116 Predef_Stream_Attr_Spec (Loc, Tag_Typ,
12117 Stream_Op_TSS_Names (Op)));
12118 end if;
12119 end loop;
12120 end;
12121
12122 -- Spec of "=" is expanded if the type is not limited and if a user
12123 -- defined "=" was not already declared for the non-full view of a
12124 -- private extension.
12125
12126 if not Is_Limited_Type (Tag_Typ) then
12127 Make_Predefined_Primitive_Eq_Spec (Tag_Typ, Res, Renamed_Eq);
12128
12129 -- Spec for dispatching assignment
12130
12131 Append_To (Res, Predef_Spec_Or_Body (Loc,
12132 Tag_Typ => Tag_Typ,
12133 Name => Name_uAssign,
12134 Profile => New_List (
12135 Make_Parameter_Specification (Loc,
12136 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
12137 Out_Present => True,
12138 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
12139
12140 Make_Parameter_Specification (Loc,
12141 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
12142 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)))));
12143 end if;
12144
12145 -- Ada 2005: Generate declarations for the following primitive
12146 -- operations for limited interfaces and synchronized types that
12147 -- implement a limited interface.
12148
12149 -- Disp_Asynchronous_Select
12150 -- Disp_Conditional_Select
12151 -- Disp_Get_Prim_Op_Kind
12152 -- Disp_Get_Task_Id
12153 -- Disp_Requeue
12154 -- Disp_Timed_Select
12155
12156 -- Disable the generation of these bodies if Ravenscar or ZFP is active
12157
12158 if Ada_Version >= Ada_2005
12159 and then not Restriction_Active (No_Select_Statements)
12160 and then RTE_Available (RE_Select_Specific_Data)
12161 then
12162 -- These primitives are defined abstract in interface types
12163
12164 if Is_Interface (Tag_Typ)
12165 and then Is_Limited_Record (Tag_Typ)
12166 then
12167 Append_To (Res,
12168 Make_Abstract_Subprogram_Declaration (Loc,
12169 Specification =>
12170 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
12171
12172 Append_To (Res,
12173 Make_Abstract_Subprogram_Declaration (Loc,
12174 Specification =>
12175 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
12176
12177 Append_To (Res,
12178 Make_Abstract_Subprogram_Declaration (Loc,
12179 Specification =>
12180 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
12181
12182 Append_To (Res,
12183 Make_Abstract_Subprogram_Declaration (Loc,
12184 Specification =>
12185 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
12186
12187 Append_To (Res,
12188 Make_Abstract_Subprogram_Declaration (Loc,
12189 Specification =>
12190 Make_Disp_Requeue_Spec (Tag_Typ)));
12191
12192 Append_To (Res,
12193 Make_Abstract_Subprogram_Declaration (Loc,
12194 Specification =>
12195 Make_Disp_Timed_Select_Spec (Tag_Typ)));
12196
12197 -- If ancestor is an interface type, declare non-abstract primitives
12198 -- to override the abstract primitives of the interface type.
12199
12200 -- In VM targets we define these primitives in all root tagged types
12201 -- that are not interface types. Done because in VM targets we don't
12202 -- have secondary dispatch tables and any derivation of Tag_Typ may
12203 -- cover limited interfaces (which always have these primitives since
12204 -- they may be ancestors of synchronized interface types).
12205
12206 elsif (not Is_Interface (Tag_Typ)
12207 and then Is_Interface (Etype (Tag_Typ))
12208 and then Is_Limited_Record (Etype (Tag_Typ)))
12209 or else
12210 (Is_Concurrent_Record_Type (Tag_Typ)
12211 and then Has_Interfaces (Tag_Typ))
12212 or else
12213 (not Tagged_Type_Expansion
12214 and then not Is_Interface (Tag_Typ)
12215 and then Tag_Typ = Root_Type (Tag_Typ))
12216 then
12217 Append_To (Res,
12218 Make_Subprogram_Declaration (Loc,
12219 Specification =>
12220 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
12221
12222 Append_To (Res,
12223 Make_Subprogram_Declaration (Loc,
12224 Specification =>
12225 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
12226
12227 Append_To (Res,
12228 Make_Subprogram_Declaration (Loc,
12229 Specification =>
12230 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
12231
12232 Append_To (Res,
12233 Make_Subprogram_Declaration (Loc,
12234 Specification =>
12235 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
12236
12237 Append_To (Res,
12238 Make_Subprogram_Declaration (Loc,
12239 Specification =>
12240 Make_Disp_Requeue_Spec (Tag_Typ)));
12241
12242 Append_To (Res,
12243 Make_Subprogram_Declaration (Loc,
12244 Specification =>
12245 Make_Disp_Timed_Select_Spec (Tag_Typ)));
12246 end if;
12247 end if;
12248
12249 -- All tagged types receive their own Deep_Adjust and Deep_Finalize
12250 -- regardless of whether they are controlled or may contain controlled
12251 -- components.
12252
12253 -- Do not generate the routines if finalization is disabled
12254
12255 if Restriction_Active (No_Finalization) then
12256 null;
12257
12258 else
12259 if not Is_Limited_Type (Tag_Typ) then
12260 Append_To (Res, Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust));
12261 end if;
12262
12263 Append_To (Res, Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize));
12264 end if;
12265
12266 Predef_List := Res;
12267 end Make_Predefined_Primitive_Specs;
12268
12269 -------------------------
12270 -- Make_Tag_Assignment --
12271 -------------------------
12272
12273 function Make_Tag_Assignment (N : Node_Id) return Node_Id is
12274 Loc : constant Source_Ptr := Sloc (N);
12275 Def_Id : constant Entity_Id := Defining_Identifier (N);
12276 Expr : constant Node_Id := Expression (N);
12277 Typ : constant Entity_Id := Etype (Def_Id);
12278 Full_Typ : constant Entity_Id := Underlying_Type (Typ);
12279
12280 begin
12281 -- This expansion activity is called during analysis
12282
12283 if Is_Tagged_Type (Typ)
12284 and then not Is_Class_Wide_Type (Typ)
12285 and then not Is_CPP_Class (Typ)
12286 and then Tagged_Type_Expansion
12287 and then Nkind (Unqualify (Expr)) /= N_Aggregate
12288 then
12289 return
12290 Make_Tag_Assignment_From_Type
12291 (Loc, New_Occurrence_Of (Def_Id, Loc), Full_Typ);
12292
12293 else
12294 return Empty;
12295 end if;
12296 end Make_Tag_Assignment;
12297
12298 ----------------------
12299 -- Predef_Deep_Spec --
12300 ----------------------
12301
12302 function Predef_Deep_Spec
12303 (Loc : Source_Ptr;
12304 Tag_Typ : Entity_Id;
12305 Name : TSS_Name_Type;
12306 For_Body : Boolean := False) return Node_Id
12307 is
12308 Formals : List_Id;
12309
12310 begin
12311 -- V : in out Tag_Typ
12312
12313 Formals := New_List (
12314 Make_Parameter_Specification (Loc,
12315 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
12316 In_Present => True,
12317 Out_Present => True,
12318 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)));
12319
12320 -- F : Boolean := True
12321
12322 if Name = TSS_Deep_Adjust
12323 or else Name = TSS_Deep_Finalize
12324 then
12325 Append_To (Formals,
12326 Make_Parameter_Specification (Loc,
12327 Defining_Identifier => Make_Defining_Identifier (Loc, Name_F),
12328 Parameter_Type => New_Occurrence_Of (Standard_Boolean, Loc),
12329 Expression => New_Occurrence_Of (Standard_True, Loc)));
12330 end if;
12331
12332 return
12333 Predef_Spec_Or_Body (Loc,
12334 Name => Make_TSS_Name (Tag_Typ, Name),
12335 Tag_Typ => Tag_Typ,
12336 Profile => Formals,
12337 For_Body => For_Body);
12338
12339 exception
12340 when RE_Not_Available =>
12341 return Empty;
12342 end Predef_Deep_Spec;
12343
12344 -------------------------
12345 -- Predef_Spec_Or_Body --
12346 -------------------------
12347
12348 function Predef_Spec_Or_Body
12349 (Loc : Source_Ptr;
12350 Tag_Typ : Entity_Id;
12351 Name : Name_Id;
12352 Profile : List_Id;
12353 Ret_Type : Entity_Id := Empty;
12354 For_Body : Boolean := False) return Node_Id
12355 is
12356 Id : constant Entity_Id := Make_Defining_Identifier (Loc, Name);
12357 Spec : Node_Id;
12358
12359 begin
12360 Set_Is_Public (Id, Is_Public (Tag_Typ));
12361
12362 -- The internal flag is set to mark these declarations because they have
12363 -- specific properties. First, they are primitives even if they are not
12364 -- defined in the type scope (the freezing point is not necessarily in
12365 -- the same scope). Second, the predefined equality can be overridden by
12366 -- a user-defined equality, no body will be generated in this case.
12367
12368 Set_Is_Internal (Id);
12369
12370 if not Debug_Generated_Code then
12371 Set_Debug_Info_Off (Id);
12372 end if;
12373
12374 if No (Ret_Type) then
12375 Spec :=
12376 Make_Procedure_Specification (Loc,
12377 Defining_Unit_Name => Id,
12378 Parameter_Specifications => Profile);
12379 else
12380 Spec :=
12381 Make_Function_Specification (Loc,
12382 Defining_Unit_Name => Id,
12383 Parameter_Specifications => Profile,
12384 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
12385 end if;
12386
12387 -- Declare an abstract subprogram for primitive subprograms of an
12388 -- interface type (except for "=").
12389
12390 if Is_Interface (Tag_Typ) then
12391 if Name /= Name_Op_Eq then
12392 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
12393
12394 -- The equality function (if any) for an interface type is defined
12395 -- to be nonabstract, so we create an expression function for it that
12396 -- always returns False. Note that the function can never actually be
12397 -- invoked because interface types are abstract, so there aren't any
12398 -- objects of such types (and their equality operation will always
12399 -- dispatch).
12400
12401 else
12402 return Make_Expression_Function
12403 (Loc, Spec, New_Occurrence_Of (Standard_False, Loc));
12404 end if;
12405
12406 -- If body case, return empty subprogram body. Note that this is ill-
12407 -- formed, because there is not even a null statement, and certainly not
12408 -- a return in the function case. The caller is expected to do surgery
12409 -- on the body to add the appropriate stuff.
12410
12411 elsif For_Body then
12412 return Make_Subprogram_Body (Loc,
12413 Specification => Spec,
12414 Declarations => Empty_List,
12415 Handled_Statement_Sequence => Empty);
12416
12417 -- For the case of an Input attribute predefined for an abstract type,
12418 -- generate an abstract specification. This will never be called, but we
12419 -- need the slot allocated in the dispatching table so that attributes
12420 -- typ'Class'Input and typ'Class'Output will work properly.
12421
12422 elsif Is_TSS (Name, TSS_Stream_Input)
12423 and then Is_Abstract_Type (Tag_Typ)
12424 then
12425 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
12426
12427 -- Normal spec case, where we return a subprogram declaration
12428
12429 else
12430 return Make_Subprogram_Declaration (Loc, Spec);
12431 end if;
12432 end Predef_Spec_Or_Body;
12433
12434 -----------------------------
12435 -- Predef_Stream_Attr_Spec --
12436 -----------------------------
12437
12438 function Predef_Stream_Attr_Spec
12439 (Loc : Source_Ptr;
12440 Tag_Typ : Entity_Id;
12441 Name : TSS_Name_Type) return Node_Id
12442 is
12443 Ret_Type : Entity_Id;
12444
12445 begin
12446 if Name = TSS_Stream_Input then
12447 Ret_Type := Tag_Typ;
12448 else
12449 Ret_Type := Empty;
12450 end if;
12451
12452 return
12453 Predef_Spec_Or_Body
12454 (Loc,
12455 Name => Make_TSS_Name (Tag_Typ, Name),
12456 Tag_Typ => Tag_Typ,
12457 Profile => Build_Stream_Attr_Profile (Loc, Tag_Typ, Name),
12458 Ret_Type => Ret_Type,
12459 For_Body => False);
12460 end Predef_Stream_Attr_Spec;
12461
12462 ----------------------------------
12463 -- Predefined_Primitive_Eq_Body --
12464 ----------------------------------
12465
12466 procedure Predefined_Primitive_Eq_Body
12467 (Tag_Typ : Entity_Id;
12468 Predef_List : List_Id;
12469 Renamed_Eq : Entity_Id)
12470 is
12471 Decl : Node_Id;
12472 Eq_Needed : Boolean;
12473 Eq_Name : Name_Id;
12474 Prim : Elmt_Id;
12475
12476 begin
12477 -- See if we have a predefined "=" operator
12478
12479 if Present (Renamed_Eq) then
12480 Eq_Needed := True;
12481 Eq_Name := Chars (Renamed_Eq);
12482
12483 -- If the parent is an interface type then it has defined all the
12484 -- predefined primitives abstract and we need to check if the type
12485 -- has some user defined "=" function which matches the profile of
12486 -- the Ada predefined equality operator to avoid generating it.
12487
12488 elsif Is_Interface (Etype (Tag_Typ)) then
12489 Eq_Needed := True;
12490 Eq_Name := Name_Op_Eq;
12491
12492 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
12493 while Present (Prim) loop
12494 if Is_User_Defined_Equality (Node (Prim))
12495 and then not Is_Internal (Node (Prim))
12496 then
12497 Eq_Needed := False;
12498 Eq_Name := No_Name;
12499 exit;
12500 end if;
12501
12502 Next_Elmt (Prim);
12503 end loop;
12504
12505 else
12506 Eq_Needed := False;
12507 Eq_Name := No_Name;
12508
12509 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
12510 while Present (Prim) loop
12511 if Is_User_Defined_Equality (Node (Prim))
12512 and then Is_Internal (Node (Prim))
12513 then
12514 Eq_Needed := True;
12515 Eq_Name := Name_Op_Eq;
12516 exit;
12517 end if;
12518
12519 Next_Elmt (Prim);
12520 end loop;
12521 end if;
12522
12523 -- If equality is needed, we will have its name
12524
12525 pragma Assert (Eq_Needed = Present (Eq_Name));
12526
12527 -- Body for equality
12528
12529 if Eq_Needed then
12530 Decl := Make_Eq_Body (Tag_Typ, Eq_Name);
12531 Append_To (Predef_List, Decl);
12532 end if;
12533
12534 -- Body for inequality (if required)
12535
12536 Decl := Make_Neq_Body (Tag_Typ);
12537
12538 if Present (Decl) then
12539 Append_To (Predef_List, Decl);
12540 end if;
12541 end Predefined_Primitive_Eq_Body;
12542
12543 ---------------------------------
12544 -- Predefined_Primitive_Bodies --
12545 ---------------------------------
12546
12547 function Predefined_Primitive_Bodies
12548 (Tag_Typ : Entity_Id;
12549 Renamed_Eq : Entity_Id) return List_Id
12550 is
12551 Loc : constant Source_Ptr := Sloc (Tag_Typ);
12552 Res : constant List_Id := New_List;
12553 Adj_Call : Node_Id;
12554 Decl : Node_Id;
12555 Fin_Call : Node_Id;
12556 Ent : Entity_Id;
12557
12558 pragma Warnings (Off, Ent);
12559
12560 use Exp_Put_Image;
12561
12562 begin
12563 pragma Assert (not Is_Interface (Tag_Typ));
12564
12565 -- Body of _Size
12566
12567 Decl := Predef_Spec_Or_Body (Loc,
12568 Tag_Typ => Tag_Typ,
12569 Name => Name_uSize,
12570 Profile => New_List (
12571 Make_Parameter_Specification (Loc,
12572 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
12573 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
12574
12575 Ret_Type => Standard_Long_Long_Integer,
12576 For_Body => True);
12577
12578 Set_Handled_Statement_Sequence (Decl,
12579 Make_Handled_Sequence_Of_Statements (Loc, New_List (
12580 Make_Simple_Return_Statement (Loc,
12581 Expression =>
12582 Make_Attribute_Reference (Loc,
12583 Prefix => Make_Identifier (Loc, Name_X),
12584 Attribute_Name => Name_Size)))));
12585
12586 Append_To (Res, Decl);
12587
12588 -- Body of Put_Image
12589
12590 if No (TSS (Tag_Typ, TSS_Put_Image))
12591 and then not No_Run_Time_Mode
12592 and then RTE_Available (RE_Root_Buffer_Type)
12593 then
12594 Build_Record_Put_Image_Procedure (Tag_Typ, Decl, Ent);
12595 Append_To (Res, Decl);
12596 end if;
12597
12598 -- Bodies for Dispatching stream IO routines. We need these only for
12599 -- non-limited types (in the limited case there is no dispatching).
12600 -- We also skip them if dispatching or finalization are not available
12601 -- or if stream operations are prohibited by restriction No_Streams or
12602 -- from use of pragma/aspect No_Tagged_Streams.
12603
12604 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Read)
12605 and then No (TSS (Tag_Typ, TSS_Stream_Read))
12606 then
12607 Build_Record_Read_Procedure (Tag_Typ, Decl, Ent);
12608 Append_To (Res, Decl);
12609 end if;
12610
12611 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Write)
12612 and then No (TSS (Tag_Typ, TSS_Stream_Write))
12613 then
12614 Build_Record_Write_Procedure (Tag_Typ, Decl, Ent);
12615 Append_To (Res, Decl);
12616 end if;
12617
12618 -- Skip body of _Input for the abstract case, since the corresponding
12619 -- spec is abstract (see Predef_Spec_Or_Body).
12620
12621 if not Is_Abstract_Type (Tag_Typ)
12622 and then Stream_Operation_OK (Tag_Typ, TSS_Stream_Input)
12623 and then No (TSS (Tag_Typ, TSS_Stream_Input))
12624 then
12625 Build_Record_Or_Elementary_Input_Function (Tag_Typ, Decl, Ent);
12626 Append_To (Res, Decl);
12627 end if;
12628
12629 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Output)
12630 and then No (TSS (Tag_Typ, TSS_Stream_Output))
12631 then
12632 Build_Record_Or_Elementary_Output_Procedure (Tag_Typ, Decl, Ent);
12633 Append_To (Res, Decl);
12634 end if;
12635
12636 -- Ada 2005: Generate bodies for the following primitive operations for
12637 -- limited interfaces and synchronized types that implement a limited
12638 -- interface.
12639
12640 -- disp_asynchronous_select
12641 -- disp_conditional_select
12642 -- disp_get_prim_op_kind
12643 -- disp_get_task_id
12644 -- disp_timed_select
12645
12646 -- The interface versions will have null bodies
12647
12648 -- Disable the generation of these bodies if Ravenscar or ZFP is active
12649
12650 -- In VM targets we define these primitives in all root tagged types
12651 -- that are not interface types. Done because in VM targets we don't
12652 -- have secondary dispatch tables and any derivation of Tag_Typ may
12653 -- cover limited interfaces (which always have these primitives since
12654 -- they may be ancestors of synchronized interface types).
12655
12656 if Ada_Version >= Ada_2005
12657 and then
12658 ((Is_Interface (Etype (Tag_Typ))
12659 and then Is_Limited_Record (Etype (Tag_Typ)))
12660 or else
12661 (Is_Concurrent_Record_Type (Tag_Typ)
12662 and then Has_Interfaces (Tag_Typ))
12663 or else
12664 (not Tagged_Type_Expansion
12665 and then Tag_Typ = Root_Type (Tag_Typ)))
12666 and then not Restriction_Active (No_Select_Statements)
12667 and then RTE_Available (RE_Select_Specific_Data)
12668 then
12669 Append_To (Res, Make_Disp_Asynchronous_Select_Body (Tag_Typ));
12670 Append_To (Res, Make_Disp_Conditional_Select_Body (Tag_Typ));
12671 Append_To (Res, Make_Disp_Get_Prim_Op_Kind_Body (Tag_Typ));
12672 Append_To (Res, Make_Disp_Get_Task_Id_Body (Tag_Typ));
12673 Append_To (Res, Make_Disp_Requeue_Body (Tag_Typ));
12674 Append_To (Res, Make_Disp_Timed_Select_Body (Tag_Typ));
12675 end if;
12676
12677 if not Is_Limited_Type (Tag_Typ) then
12678 -- Body for equality and inequality
12679
12680 Predefined_Primitive_Eq_Body (Tag_Typ, Res, Renamed_Eq);
12681
12682 -- Body for dispatching assignment
12683
12684 Decl :=
12685 Predef_Spec_Or_Body (Loc,
12686 Tag_Typ => Tag_Typ,
12687 Name => Name_uAssign,
12688 Profile => New_List (
12689 Make_Parameter_Specification (Loc,
12690 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
12691 Out_Present => True,
12692 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc)),
12693
12694 Make_Parameter_Specification (Loc,
12695 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
12696 Parameter_Type => New_Occurrence_Of (Tag_Typ, Loc))),
12697 For_Body => True);
12698
12699 Set_Handled_Statement_Sequence (Decl,
12700 Make_Handled_Sequence_Of_Statements (Loc, New_List (
12701 Make_Assignment_Statement (Loc,
12702 Name => Make_Identifier (Loc, Name_X),
12703 Expression => Make_Identifier (Loc, Name_Y)))));
12704
12705 Append_To (Res, Decl);
12706 end if;
12707
12708 -- Generate empty bodies of routines Deep_Adjust and Deep_Finalize for
12709 -- tagged types which do not contain controlled components.
12710
12711 -- Do not generate the routines if finalization is disabled
12712
12713 if Restriction_Active (No_Finalization) then
12714 null;
12715
12716 elsif not Has_Controlled_Component (Tag_Typ) then
12717 if not Is_Limited_Type (Tag_Typ) then
12718 Adj_Call := Empty;
12719 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust, True);
12720
12721 if Is_Controlled (Tag_Typ) then
12722 Adj_Call :=
12723 Make_Adjust_Call (
12724 Obj_Ref => Make_Identifier (Loc, Name_V),
12725 Typ => Tag_Typ);
12726 end if;
12727
12728 if No (Adj_Call) then
12729 Adj_Call := Make_Null_Statement (Loc);
12730 end if;
12731
12732 Set_Handled_Statement_Sequence (Decl,
12733 Make_Handled_Sequence_Of_Statements (Loc,
12734 Statements => New_List (Adj_Call)));
12735
12736 Append_To (Res, Decl);
12737 end if;
12738
12739 Fin_Call := Empty;
12740 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize, True);
12741
12742 if Is_Controlled (Tag_Typ) then
12743 Fin_Call :=
12744 Make_Final_Call
12745 (Obj_Ref => Make_Identifier (Loc, Name_V),
12746 Typ => Tag_Typ);
12747 end if;
12748
12749 if No (Fin_Call) then
12750 Fin_Call := Make_Null_Statement (Loc);
12751 end if;
12752
12753 Set_Handled_Statement_Sequence (Decl,
12754 Make_Handled_Sequence_Of_Statements (Loc,
12755 Statements => New_List (Fin_Call)));
12756
12757 Append_To (Res, Decl);
12758 end if;
12759
12760 return Res;
12761 end Predefined_Primitive_Bodies;
12762
12763 ---------------------------------
12764 -- Predefined_Primitive_Freeze --
12765 ---------------------------------
12766
12767 function Predefined_Primitive_Freeze
12768 (Tag_Typ : Entity_Id) return List_Id
12769 is
12770 Res : constant List_Id := New_List;
12771 Prim : Elmt_Id;
12772 Frnodes : List_Id;
12773
12774 begin
12775 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
12776 while Present (Prim) loop
12777 if Is_Predefined_Dispatching_Operation (Node (Prim)) then
12778 Frnodes := Freeze_Entity (Node (Prim), Tag_Typ);
12779
12780 if Present (Frnodes) then
12781 Append_List_To (Res, Frnodes);
12782 end if;
12783 end if;
12784
12785 Next_Elmt (Prim);
12786 end loop;
12787
12788 return Res;
12789 end Predefined_Primitive_Freeze;
12790
12791 -------------------------
12792 -- Stream_Operation_OK --
12793 -------------------------
12794
12795 function Stream_Operation_OK
12796 (Typ : Entity_Id;
12797 Operation : TSS_Name_Type) return Boolean
12798 is
12799 Has_Predefined_Or_Specified_Stream_Attribute : Boolean := False;
12800
12801 begin
12802 -- Special case of a limited type extension: a default implementation
12803 -- of the stream attributes Read or Write exists if that attribute
12804 -- has been specified or is available for an ancestor type; a default
12805 -- implementation of the attribute Output (resp. Input) exists if the
12806 -- attribute has been specified or Write (resp. Read) is available for
12807 -- an ancestor type. The last condition only applies under Ada 2005.
12808
12809 if Is_Limited_Type (Typ) and then Is_Tagged_Type (Typ) then
12810 if Operation = TSS_Stream_Read then
12811 Has_Predefined_Or_Specified_Stream_Attribute :=
12812 Has_Specified_Stream_Read (Typ);
12813
12814 elsif Operation = TSS_Stream_Write then
12815 Has_Predefined_Or_Specified_Stream_Attribute :=
12816 Has_Specified_Stream_Write (Typ);
12817
12818 elsif Operation = TSS_Stream_Input then
12819 Has_Predefined_Or_Specified_Stream_Attribute :=
12820 Has_Specified_Stream_Input (Typ)
12821 or else
12822 (Ada_Version >= Ada_2005
12823 and then Stream_Operation_OK (Typ, TSS_Stream_Read));
12824
12825 elsif Operation = TSS_Stream_Output then
12826 Has_Predefined_Or_Specified_Stream_Attribute :=
12827 Has_Specified_Stream_Output (Typ)
12828 or else
12829 (Ada_Version >= Ada_2005
12830 and then Stream_Operation_OK (Typ, TSS_Stream_Write));
12831 end if;
12832
12833 -- Case of inherited TSS_Stream_Read or TSS_Stream_Write
12834
12835 if not Has_Predefined_Or_Specified_Stream_Attribute
12836 and then Is_Derived_Type (Typ)
12837 and then (Operation = TSS_Stream_Read
12838 or else Operation = TSS_Stream_Write)
12839 then
12840 Has_Predefined_Or_Specified_Stream_Attribute :=
12841 Present
12842 (Find_Inherited_TSS (Base_Type (Etype (Typ)), Operation));
12843 end if;
12844 end if;
12845
12846 -- If the type is not limited, or else is limited but the attribute is
12847 -- explicitly specified or is predefined for the type, then return True,
12848 -- unless other conditions prevail, such as restrictions prohibiting
12849 -- streams or dispatching operations. We also return True for limited
12850 -- interfaces, because they may be extended by nonlimited types and
12851 -- permit inheritance in this case (addresses cases where an abstract
12852 -- extension doesn't get 'Input declared, as per comments below, but
12853 -- 'Class'Input must still be allowed). Note that attempts to apply
12854 -- stream attributes to a limited interface or its class-wide type
12855 -- (or limited extensions thereof) will still get properly rejected
12856 -- by Check_Stream_Attribute.
12857
12858 -- We exclude the Input operation from being a predefined subprogram in
12859 -- the case where the associated type is an abstract extension, because
12860 -- the attribute is not callable in that case, per 13.13.2(49/2). Also,
12861 -- we don't want an abstract version created because types derived from
12862 -- the abstract type may not even have Input available (for example if
12863 -- derived from a private view of the abstract type that doesn't have
12864 -- a visible Input).
12865
12866 return
12867 (not Is_Limited_Type (Typ)
12868 or else Is_Interface (Typ)
12869 or else Has_Predefined_Or_Specified_Stream_Attribute)
12870 and then
12871 (Operation /= TSS_Stream_Input
12872 or else not Is_Abstract_Type (Typ)
12873 or else not Is_Derived_Type (Typ))
12874 and then not Has_Unknown_Discriminants (Typ)
12875 and then not Is_Concurrent_Interface (Typ)
12876 and then not Restriction_Active (No_Streams)
12877 and then not Restriction_Active (No_Dispatch)
12878 and then No (No_Tagged_Streams_Pragma (Typ))
12879 and then not No_Run_Time_Mode
12880 and then RTE_Available (RE_Tag)
12881 and then
12882 (not Restriction_Active (No_Default_Stream_Attributes)
12883 or else No (Type_Without_Stream_Operation (Typ)))
12884 and then RTE_Available (RE_Root_Stream_Type);
12885 end Stream_Operation_OK;
12886
12887 end Exp_Ch3;