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