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