rtsfind.ads, [...]: Complete support for Ada 2005 interfaces.
[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-2005, 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
26
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Errout; use Errout;
31 with Exp_Aggr; use Exp_Aggr;
32 with Exp_Ch4; use Exp_Ch4;
33 with Exp_Ch7; use Exp_Ch7;
34 with Exp_Ch9; use Exp_Ch9;
35 with Exp_Ch11; use Exp_Ch11;
36 with Exp_Disp; use Exp_Disp;
37 with Exp_Dist; use Exp_Dist;
38 with Exp_Smem; use Exp_Smem;
39 with Exp_Strm; use Exp_Strm;
40 with Exp_Tss; use Exp_Tss;
41 with Exp_Util; use Exp_Util;
42 with Freeze; use Freeze;
43 with Hostparm; use Hostparm;
44 with Nlists; use Nlists;
45 with Nmake; use Nmake;
46 with Opt; use Opt;
47 with Restrict; use Restrict;
48 with Rident; use Rident;
49 with Rtsfind; use Rtsfind;
50 with Sem; use Sem;
51 with Sem_Attr; use Sem_Attr;
52 with Sem_Ch3; use Sem_Ch3;
53 with Sem_Ch8; use Sem_Ch8;
54 with Sem_Eval; use Sem_Eval;
55 with Sem_Mech; use Sem_Mech;
56 with Sem_Res; use Sem_Res;
57 with Sem_Util; use Sem_Util;
58 with Sinfo; use Sinfo;
59 with Stand; use Stand;
60 with Snames; use Snames;
61 with Tbuild; use Tbuild;
62 with Ttypes; use Ttypes;
63 with Validsw; use Validsw;
64
65 package body Exp_Ch3 is
66
67 -----------------------
68 -- Local Subprograms --
69 -----------------------
70
71 procedure Adjust_Discriminants (Rtype : Entity_Id);
72 -- This is used when freezing a record type. It attempts to construct
73 -- more restrictive subtypes for discriminants so that the max size of
74 -- the record can be calculated more accurately. See the body of this
75 -- procedure for details.
76
77 procedure Build_Array_Init_Proc (A_Type : Entity_Id; Nod : Node_Id);
78 -- Build initialization procedure for given array type. Nod is a node
79 -- used for attachment of any actions required in its construction.
80 -- It also supplies the source location used for the procedure.
81
82 function Build_Discriminant_Formals
83 (Rec_Id : Entity_Id;
84 Use_Dl : Boolean) return List_Id;
85 -- This function uses the discriminants of a type to build a list of
86 -- formal parameters, used in the following function. If the flag Use_Dl
87 -- is set, the list is built using the already defined discriminals
88 -- of the type. Otherwise new identifiers are created, with the source
89 -- names of the discriminants.
90
91 procedure Build_Master_Renaming (N : Node_Id; T : Entity_Id);
92 -- If the designated type of an access type is a task type or contains
93 -- tasks, we make sure that a _Master variable is declared in the current
94 -- scope, and then declare a renaming for it:
95 --
96 -- atypeM : Master_Id renames _Master;
97 --
98 -- where atyp is the name of the access type. This declaration is
99 -- used when an allocator for the access type is expanded. The node N
100 -- is the full declaration of the designated type that contains tasks.
101 -- The renaming declaration is inserted before N, and after the Master
102 -- declaration.
103
104 procedure Build_Record_Init_Proc (N : Node_Id; Pe : Entity_Id);
105 -- Build record initialization procedure. N is the type declaration
106 -- node, and Pe is the corresponding entity for the record type.
107
108 procedure Build_Slice_Assignment (Typ : Entity_Id);
109 -- Build assignment procedure for one-dimensional arrays of controlled
110 -- types. Other array and slice assignments are expanded in-line, but
111 -- the code expansion for controlled components (when control actions
112 -- are active) can lead to very large blocks that GCC3 handles poorly.
113
114 procedure Build_Variant_Record_Equality (Typ : Entity_Id);
115 -- Create An Equality function for the non-tagged variant record 'Typ'
116 -- and attach it to the TSS list
117
118 procedure Check_Stream_Attributes (Typ : Entity_Id);
119 -- Check that if a limited extension has a parent with user-defined
120 -- stream attributes, and does not itself have user-definer
121 -- stream-attributes, then any limited component of the extension also
122 -- has the corresponding user-defined stream attributes.
123
124 procedure Expand_Tagged_Root (T : Entity_Id);
125 -- Add a field _Tag at the beginning of the record. This field carries
126 -- the value of the access to the Dispatch table. This procedure is only
127 -- called on root (non CPP_Class) types, the _Tag field being inherited
128 -- by the descendants.
129
130 procedure Expand_Record_Controller (T : Entity_Id);
131 -- T must be a record type that Has_Controlled_Component. Add a field
132 -- _controller of type Record_Controller or Limited_Record_Controller
133 -- in the record T.
134
135 procedure Freeze_Array_Type (N : Node_Id);
136 -- Freeze an array type. Deals with building the initialization procedure,
137 -- creating the packed array type for a packed array and also with the
138 -- creation of the controlling procedures for the controlled case. The
139 -- argument N is the N_Freeze_Entity node for the type.
140
141 procedure Freeze_Enumeration_Type (N : Node_Id);
142 -- Freeze enumeration type with non-standard representation. Builds the
143 -- array and function needed to convert between enumeration pos and
144 -- enumeration representation values. N is the N_Freeze_Entity node
145 -- for the type.
146
147 procedure Freeze_Record_Type (N : Node_Id);
148 -- Freeze record type. Builds all necessary discriminant checking
149 -- and other ancillary functions, and builds dispatch tables where
150 -- needed. The argument N is the N_Freeze_Entity node. This processing
151 -- applies only to E_Record_Type entities, not to class wide types,
152 -- record subtypes, or private types.
153
154 procedure Freeze_Stream_Operations (N : Node_Id; Typ : Entity_Id);
155 -- Treat user-defined stream operations as renaming_as_body if the
156 -- subprogram they rename is not frozen when the type is frozen.
157
158 function Init_Formals (Typ : Entity_Id) return List_Id;
159 -- This function builds the list of formals for an initialization routine.
160 -- The first formal is always _Init with the given type. For task value
161 -- record types and types containing tasks, three additional formals are
162 -- added:
163 --
164 -- _Master : Master_Id
165 -- _Chain : in out Activation_Chain
166 -- _Task_Name : String
167 --
168 -- The caller must append additional entries for discriminants if required.
169
170 function In_Runtime (E : Entity_Id) return Boolean;
171 -- Check if E is defined in the RTL (in a child of Ada or System). Used
172 -- to avoid to bring in the overhead of _Input, _Output for tagged types.
173
174 function Make_Eq_Case
175 (E : Entity_Id;
176 CL : Node_Id;
177 Discr : Entity_Id := Empty) return List_Id;
178 -- Building block for variant record equality. Defined to share the
179 -- code between the tagged and non-tagged case. Given a Component_List
180 -- node CL, it generates an 'if' followed by a 'case' statement that
181 -- compares all components of local temporaries named X and Y (that
182 -- are declared as formals at some upper level). E provides the Sloc to be
183 -- used for the generated code. Discr is used as the case statement switch
184 -- in the case of Unchecked_Union equality.
185
186 function Make_Eq_If
187 (E : Entity_Id;
188 L : List_Id) return Node_Id;
189 -- Building block for variant record equality. Defined to share the
190 -- code between the tagged and non-tagged case. Given the list of
191 -- components (or discriminants) L, it generates a return statement
192 -- that compares all components of local temporaries named X and Y
193 -- (that are declared as formals at some upper level). E provides the Sloc
194 -- to be used for the generated code.
195
196 procedure Make_Predefined_Primitive_Specs
197 (Tag_Typ : Entity_Id;
198 Predef_List : out List_Id;
199 Renamed_Eq : out Node_Id);
200 -- Create a list with the specs of the predefined primitive operations.
201 -- The following entries are present for all tagged types, and provide
202 -- the results of the corresponding attribute applied to the object.
203 -- Dispatching is required in general, since the result of the attribute
204 -- will vary with the actual object subtype.
205 --
206 -- _alignment provides result of 'Alignment attribute
207 -- _size provides result of 'Size attribute
208 -- typSR provides result of 'Read attribute
209 -- typSW provides result of 'Write attribute
210 -- typSI provides result of 'Input attribute
211 -- typSO provides result of 'Output attribute
212 --
213 -- The following entries are additionally present for non-limited
214 -- tagged types, and implement additional dispatching operations
215 -- for predefined operations:
216 --
217 -- _equality implements "=" operator
218 -- _assign implements assignment operation
219 -- typDF implements deep finalization
220 -- typDA implements deep adust
221 --
222 -- The latter two are empty procedures unless the type contains some
223 -- controlled components that require finalization actions (the deep
224 -- in the name refers to the fact that the action applies to components).
225 --
226 -- The list is returned in Predef_List. The Parameter Renamed_Eq
227 -- either returns the value Empty, or else the defining unit name
228 -- for the predefined equality function in the case where the type
229 -- has a primitive operation that is a renaming of predefined equality
230 -- (but only if there is also an overriding user-defined equality
231 -- function). The returned Renamed_Eq will be passed to the
232 -- corresponding parameter of Predefined_Primitive_Bodies.
233
234 function Has_New_Non_Standard_Rep (T : Entity_Id) return Boolean;
235 -- returns True if there are representation clauses for type T that
236 -- are not inherited. If the result is false, the init_proc and the
237 -- discriminant_checking functions of the parent can be reused by
238 -- a derived type.
239
240 function Predef_Spec_Or_Body
241 (Loc : Source_Ptr;
242 Tag_Typ : Entity_Id;
243 Name : Name_Id;
244 Profile : List_Id;
245 Ret_Type : Entity_Id := Empty;
246 For_Body : Boolean := False) return Node_Id;
247 -- This function generates the appropriate expansion for a predefined
248 -- primitive operation specified by its name, parameter profile and
249 -- return type (Empty means this is a procedure). If For_Body is false,
250 -- then the returned node is a subprogram declaration. If For_Body is
251 -- true, then the returned node is a empty subprogram body containing
252 -- no declarations and no statements.
253
254 function Predef_Stream_Attr_Spec
255 (Loc : Source_Ptr;
256 Tag_Typ : Entity_Id;
257 Name : TSS_Name_Type;
258 For_Body : Boolean := False) return Node_Id;
259 -- Specialized version of Predef_Spec_Or_Body that apply to read, write,
260 -- input and output attribute whose specs are constructed in Exp_Strm.
261
262 function Predef_Deep_Spec
263 (Loc : Source_Ptr;
264 Tag_Typ : Entity_Id;
265 Name : TSS_Name_Type;
266 For_Body : Boolean := False) return Node_Id;
267 -- Specialized version of Predef_Spec_Or_Body that apply to _deep_adjust
268 -- and _deep_finalize
269
270 function Predefined_Primitive_Bodies
271 (Tag_Typ : Entity_Id;
272 Renamed_Eq : Node_Id) return List_Id;
273 -- Create the bodies of the predefined primitives that are described in
274 -- Predefined_Primitive_Specs. When not empty, Renamed_Eq must denote
275 -- the defining unit name of the type's predefined equality as returned
276 -- by Make_Predefined_Primitive_Specs.
277
278 function Predefined_Primitive_Freeze (Tag_Typ : Entity_Id) return List_Id;
279 -- Freeze entities of all predefined primitive operations. This is needed
280 -- because the bodies of these operations do not normally do any freezeing.
281
282 function Stream_Operation_OK
283 (Typ : Entity_Id;
284 Operation : TSS_Name_Type) return Boolean;
285 -- Check whether the named stream operation must be emitted for a given
286 -- type. The rules for inheritance of stream attributes by type extensions
287 -- are enforced by this function. Furthermore, various restrictions prevent
288 -- the generation of these operations, as a useful optimization or for
289 -- certification purposes.
290
291 --------------------------
292 -- Adjust_Discriminants --
293 --------------------------
294
295 -- This procedure attempts to define subtypes for discriminants that
296 -- are more restrictive than those declared. Such a replacement is
297 -- possible if we can demonstrate that values outside the restricted
298 -- range would cause constraint errors in any case. The advantage of
299 -- restricting the discriminant types in this way is tha the maximum
300 -- size of the variant record can be calculated more conservatively.
301
302 -- An example of a situation in which we can perform this type of
303 -- restriction is the following:
304
305 -- subtype B is range 1 .. 10;
306 -- type Q is array (B range <>) of Integer;
307
308 -- type V (N : Natural) is record
309 -- C : Q (1 .. N);
310 -- end record;
311
312 -- In this situation, we can restrict the upper bound of N to 10, since
313 -- any larger value would cause a constraint error in any case.
314
315 -- There are many situations in which such restriction is possible, but
316 -- for now, we just look for cases like the above, where the component
317 -- in question is a one dimensional array whose upper bound is one of
318 -- the record discriminants. Also the component must not be part of
319 -- any variant part, since then the component does not always exist.
320
321 procedure Adjust_Discriminants (Rtype : Entity_Id) is
322 Loc : constant Source_Ptr := Sloc (Rtype);
323 Comp : Entity_Id;
324 Ctyp : Entity_Id;
325 Ityp : Entity_Id;
326 Lo : Node_Id;
327 Hi : Node_Id;
328 P : Node_Id;
329 Loval : Uint;
330 Discr : Entity_Id;
331 Dtyp : Entity_Id;
332 Dhi : Node_Id;
333 Dhiv : Uint;
334 Ahi : Node_Id;
335 Ahiv : Uint;
336 Tnn : Entity_Id;
337
338 begin
339 Comp := First_Component (Rtype);
340 while Present (Comp) loop
341
342 -- If our parent is a variant, quit, we do not look at components
343 -- that are in variant parts, because they may not always exist.
344
345 P := Parent (Comp); -- component declaration
346 P := Parent (P); -- component list
347
348 exit when Nkind (Parent (P)) = N_Variant;
349
350 -- We are looking for a one dimensional array type
351
352 Ctyp := Etype (Comp);
353
354 if not Is_Array_Type (Ctyp)
355 or else Number_Dimensions (Ctyp) > 1
356 then
357 goto Continue;
358 end if;
359
360 -- The lower bound must be constant, and the upper bound is a
361 -- discriminant (which is a discriminant of the current record).
362
363 Ityp := Etype (First_Index (Ctyp));
364 Lo := Type_Low_Bound (Ityp);
365 Hi := Type_High_Bound (Ityp);
366
367 if not Compile_Time_Known_Value (Lo)
368 or else Nkind (Hi) /= N_Identifier
369 or else No (Entity (Hi))
370 or else Ekind (Entity (Hi)) /= E_Discriminant
371 then
372 goto Continue;
373 end if;
374
375 -- We have an array with appropriate bounds
376
377 Loval := Expr_Value (Lo);
378 Discr := Entity (Hi);
379 Dtyp := Etype (Discr);
380
381 -- See if the discriminant has a known upper bound
382
383 Dhi := Type_High_Bound (Dtyp);
384
385 if not Compile_Time_Known_Value (Dhi) then
386 goto Continue;
387 end if;
388
389 Dhiv := Expr_Value (Dhi);
390
391 -- See if base type of component array has known upper bound
392
393 Ahi := Type_High_Bound (Etype (First_Index (Base_Type (Ctyp))));
394
395 if not Compile_Time_Known_Value (Ahi) then
396 goto Continue;
397 end if;
398
399 Ahiv := Expr_Value (Ahi);
400
401 -- The condition for doing the restriction is that the high bound
402 -- of the discriminant is greater than the low bound of the array,
403 -- and is also greater than the high bound of the base type index.
404
405 if Dhiv > Loval and then Dhiv > Ahiv then
406
407 -- We can reset the upper bound of the discriminant type to
408 -- whichever is larger, the low bound of the component, or
409 -- the high bound of the base type array index.
410
411 -- We build a subtype that is declared as
412
413 -- subtype Tnn is discr_type range discr_type'First .. max;
414
415 -- And insert this declaration into the tree. The type of the
416 -- discriminant is then reset to this more restricted subtype.
417
418 Tnn := Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
419
420 Insert_Action (Declaration_Node (Rtype),
421 Make_Subtype_Declaration (Loc,
422 Defining_Identifier => Tnn,
423 Subtype_Indication =>
424 Make_Subtype_Indication (Loc,
425 Subtype_Mark => New_Occurrence_Of (Dtyp, Loc),
426 Constraint =>
427 Make_Range_Constraint (Loc,
428 Range_Expression =>
429 Make_Range (Loc,
430 Low_Bound =>
431 Make_Attribute_Reference (Loc,
432 Attribute_Name => Name_First,
433 Prefix => New_Occurrence_Of (Dtyp, Loc)),
434 High_Bound =>
435 Make_Integer_Literal (Loc,
436 Intval => UI_Max (Loval, Ahiv)))))));
437
438 Set_Etype (Discr, Tnn);
439 end if;
440
441 <<Continue>>
442 Next_Component (Comp);
443 end loop;
444 end Adjust_Discriminants;
445
446 ---------------------------
447 -- Build_Array_Init_Proc --
448 ---------------------------
449
450 procedure Build_Array_Init_Proc (A_Type : Entity_Id; Nod : Node_Id) is
451 Loc : constant Source_Ptr := Sloc (Nod);
452 Comp_Type : constant Entity_Id := Component_Type (A_Type);
453 Index_List : List_Id;
454 Proc_Id : Entity_Id;
455 Body_Stmts : List_Id;
456
457 function Init_Component return List_Id;
458 -- Create one statement to initialize one array component, designated
459 -- by a full set of indices.
460
461 function Init_One_Dimension (N : Int) return List_Id;
462 -- Create loop to initialize one dimension of the array. The single
463 -- statement in the loop body initializes the inner dimensions if any,
464 -- or else the single component. Note that this procedure is called
465 -- recursively, with N being the dimension to be initialized. A call
466 -- with N greater than the number of dimensions simply generates the
467 -- component initialization, terminating the recursion.
468
469 --------------------
470 -- Init_Component --
471 --------------------
472
473 function Init_Component return List_Id is
474 Comp : Node_Id;
475
476 begin
477 Comp :=
478 Make_Indexed_Component (Loc,
479 Prefix => Make_Identifier (Loc, Name_uInit),
480 Expressions => Index_List);
481
482 if Needs_Simple_Initialization (Comp_Type) then
483 Set_Assignment_OK (Comp);
484 return New_List (
485 Make_Assignment_Statement (Loc,
486 Name => Comp,
487 Expression =>
488 Get_Simple_Init_Val
489 (Comp_Type, Loc, Component_Size (A_Type))));
490
491 else
492 return
493 Build_Initialization_Call (Loc, Comp, Comp_Type, True, A_Type);
494 end if;
495 end Init_Component;
496
497 ------------------------
498 -- Init_One_Dimension --
499 ------------------------
500
501 function Init_One_Dimension (N : Int) return List_Id is
502 Index : Entity_Id;
503
504 begin
505 -- If the component does not need initializing, then there is nothing
506 -- to do here, so we return a null body. This occurs when generating
507 -- the dummy Init_Proc needed for Initialize_Scalars processing.
508
509 if not Has_Non_Null_Base_Init_Proc (Comp_Type)
510 and then not Needs_Simple_Initialization (Comp_Type)
511 and then not Has_Task (Comp_Type)
512 then
513 return New_List (Make_Null_Statement (Loc));
514
515 -- If all dimensions dealt with, we simply initialize the component
516
517 elsif N > Number_Dimensions (A_Type) then
518 return Init_Component;
519
520 -- Here we generate the required loop
521
522 else
523 Index :=
524 Make_Defining_Identifier (Loc, New_External_Name ('J', N));
525
526 Append (New_Reference_To (Index, Loc), Index_List);
527
528 return New_List (
529 Make_Implicit_Loop_Statement (Nod,
530 Identifier => Empty,
531 Iteration_Scheme =>
532 Make_Iteration_Scheme (Loc,
533 Loop_Parameter_Specification =>
534 Make_Loop_Parameter_Specification (Loc,
535 Defining_Identifier => Index,
536 Discrete_Subtype_Definition =>
537 Make_Attribute_Reference (Loc,
538 Prefix => Make_Identifier (Loc, Name_uInit),
539 Attribute_Name => Name_Range,
540 Expressions => New_List (
541 Make_Integer_Literal (Loc, N))))),
542 Statements => Init_One_Dimension (N + 1)));
543 end if;
544 end Init_One_Dimension;
545
546 -- Start of processing for Build_Array_Init_Proc
547
548 begin
549 if Suppress_Init_Proc (A_Type) then
550 return;
551 end if;
552
553 Index_List := New_List;
554
555 -- We need an initialization procedure if any of the following is true:
556
557 -- 1. The component type has an initialization procedure
558 -- 2. The component type needs simple initialization
559 -- 3. Tasks are present
560 -- 4. The type is marked as a publc entity
561
562 -- The reason for the public entity test is to deal properly with the
563 -- Initialize_Scalars pragma. This pragma can be set in the client and
564 -- not in the declaring package, this means the client will make a call
565 -- to the initialization procedure (because one of conditions 1-3 must
566 -- apply in this case), and we must generate a procedure (even if it is
567 -- null) to satisfy the call in this case.
568
569 -- Exception: do not build an array init_proc for a type whose root
570 -- type is Standard.String or Standard.Wide_[Wide_]String, since there
571 -- is no place to put the code, and in any case we handle initialization
572 -- of such types (in the Initialize_Scalars case, that's the only time
573 -- the issue arises) in a special manner anyway which does not need an
574 -- init_proc.
575
576 if Has_Non_Null_Base_Init_Proc (Comp_Type)
577 or else Needs_Simple_Initialization (Comp_Type)
578 or else Has_Task (Comp_Type)
579 or else (not Restriction_Active (No_Initialize_Scalars)
580 and then Is_Public (A_Type)
581 and then Root_Type (A_Type) /= Standard_String
582 and then Root_Type (A_Type) /= Standard_Wide_String
583 and then Root_Type (A_Type) /= Standard_Wide_Wide_String)
584 then
585 Proc_Id :=
586 Make_Defining_Identifier (Loc, Make_Init_Proc_Name (A_Type));
587
588 Body_Stmts := Init_One_Dimension (1);
589
590 Discard_Node (
591 Make_Subprogram_Body (Loc,
592 Specification =>
593 Make_Procedure_Specification (Loc,
594 Defining_Unit_Name => Proc_Id,
595 Parameter_Specifications => Init_Formals (A_Type)),
596 Declarations => New_List,
597 Handled_Statement_Sequence =>
598 Make_Handled_Sequence_Of_Statements (Loc,
599 Statements => Body_Stmts)));
600
601 Set_Ekind (Proc_Id, E_Procedure);
602 Set_Is_Public (Proc_Id, Is_Public (A_Type));
603 Set_Is_Internal (Proc_Id);
604 Set_Has_Completion (Proc_Id);
605
606 if not Debug_Generated_Code then
607 Set_Debug_Info_Off (Proc_Id);
608 end if;
609
610 -- Set inlined unless controlled stuff or tasks around, in which
611 -- case we do not want to inline, because nested stuff may cause
612 -- difficulties in interunit inlining, and furthermore there is
613 -- in any case no point in inlining such complex init procs.
614
615 if not Has_Task (Proc_Id)
616 and then not Controlled_Type (Proc_Id)
617 then
618 Set_Is_Inlined (Proc_Id);
619 end if;
620
621 -- Associate Init_Proc with type, and determine if the procedure
622 -- is null (happens because of the Initialize_Scalars pragma case,
623 -- where we have to generate a null procedure in case it is called
624 -- by a client with Initialize_Scalars set). Such procedures have
625 -- to be generated, but do not have to be called, so we mark them
626 -- as null to suppress the call.
627
628 Set_Init_Proc (A_Type, Proc_Id);
629
630 if List_Length (Body_Stmts) = 1
631 and then Nkind (First (Body_Stmts)) = N_Null_Statement
632 then
633 Set_Is_Null_Init_Proc (Proc_Id);
634 end if;
635 end if;
636 end Build_Array_Init_Proc;
637
638 -----------------------------
639 -- Build_Class_Wide_Master --
640 -----------------------------
641
642 procedure Build_Class_Wide_Master (T : Entity_Id) is
643 Loc : constant Source_Ptr := Sloc (T);
644 M_Id : Entity_Id;
645 Decl : Node_Id;
646 P : Node_Id;
647 Par : Node_Id;
648
649 begin
650 -- Nothing to do if there is no task hierarchy
651
652 if Restriction_Active (No_Task_Hierarchy) then
653 return;
654 end if;
655
656 -- Find declaration that created the access type: either a
657 -- type declaration, or an object declaration with an
658 -- access definition, in which case the type is anonymous.
659
660 if Is_Itype (T) then
661 P := Associated_Node_For_Itype (T);
662 else
663 P := Parent (T);
664 end if;
665
666 -- Nothing to do if we already built a master entity for this scope
667
668 if not Has_Master_Entity (Scope (T)) then
669
670 -- first build the master entity
671 -- _Master : constant Master_Id := Current_Master.all;
672 -- and insert it just before the current declaration
673
674 Decl :=
675 Make_Object_Declaration (Loc,
676 Defining_Identifier =>
677 Make_Defining_Identifier (Loc, Name_uMaster),
678 Constant_Present => True,
679 Object_Definition => New_Reference_To (Standard_Integer, Loc),
680 Expression =>
681 Make_Explicit_Dereference (Loc,
682 New_Reference_To (RTE (RE_Current_Master), Loc)));
683
684 Insert_Before (P, Decl);
685 Analyze (Decl);
686 Set_Has_Master_Entity (Scope (T));
687
688 -- Now mark the containing scope as a task master
689
690 Par := P;
691 while Nkind (Par) /= N_Compilation_Unit loop
692 Par := Parent (Par);
693
694 -- If we fall off the top, we are at the outer level, and the
695 -- environment task is our effective master, so nothing to mark.
696
697 if Nkind (Par) = N_Task_Body
698 or else Nkind (Par) = N_Block_Statement
699 or else Nkind (Par) = N_Subprogram_Body
700 then
701 Set_Is_Task_Master (Par, True);
702 exit;
703 end if;
704 end loop;
705 end if;
706
707 -- Now define the renaming of the master_id
708
709 M_Id :=
710 Make_Defining_Identifier (Loc,
711 New_External_Name (Chars (T), 'M'));
712
713 Decl :=
714 Make_Object_Renaming_Declaration (Loc,
715 Defining_Identifier => M_Id,
716 Subtype_Mark => New_Reference_To (Standard_Integer, Loc),
717 Name => Make_Identifier (Loc, Name_uMaster));
718 Insert_Before (P, Decl);
719 Analyze (Decl);
720
721 Set_Master_Id (T, M_Id);
722
723 exception
724 when RE_Not_Available =>
725 return;
726 end Build_Class_Wide_Master;
727
728 --------------------------------
729 -- Build_Discr_Checking_Funcs --
730 --------------------------------
731
732 procedure Build_Discr_Checking_Funcs (N : Node_Id) is
733 Rec_Id : Entity_Id;
734 Loc : Source_Ptr;
735 Enclosing_Func_Id : Entity_Id;
736 Sequence : Nat := 1;
737 Type_Def : Node_Id;
738 V : Node_Id;
739
740 function Build_Case_Statement
741 (Case_Id : Entity_Id;
742 Variant : Node_Id) return Node_Id;
743 -- Build a case statement containing only two alternatives. The
744 -- first alternative corresponds exactly to the discrete choices
745 -- given on the variant with contains the components that we are
746 -- generating the checks for. If the discriminant is one of these
747 -- return False. The second alternative is an OTHERS choice that
748 -- will return True indicating the discriminant did not match.
749
750 function Build_Dcheck_Function
751 (Case_Id : Entity_Id;
752 Variant : Node_Id) return Entity_Id;
753 -- Build the discriminant checking function for a given variant
754
755 procedure Build_Dcheck_Functions (Variant_Part_Node : Node_Id);
756 -- Builds the discriminant checking function for each variant of the
757 -- given variant part of the record type.
758
759 --------------------------
760 -- Build_Case_Statement --
761 --------------------------
762
763 function Build_Case_Statement
764 (Case_Id : Entity_Id;
765 Variant : Node_Id) return Node_Id
766 is
767 Alt_List : constant List_Id := New_List;
768 Actuals_List : List_Id;
769 Case_Node : Node_Id;
770 Case_Alt_Node : Node_Id;
771 Choice : Node_Id;
772 Choice_List : List_Id;
773 D : Entity_Id;
774 Return_Node : Node_Id;
775
776 begin
777 Case_Node := New_Node (N_Case_Statement, Loc);
778
779 -- Replace the discriminant which controls the variant, with the
780 -- name of the formal of the checking function.
781
782 Set_Expression (Case_Node,
783 Make_Identifier (Loc, Chars (Case_Id)));
784
785 Choice := First (Discrete_Choices (Variant));
786
787 if Nkind (Choice) = N_Others_Choice then
788 Choice_List := New_Copy_List (Others_Discrete_Choices (Choice));
789 else
790 Choice_List := New_Copy_List (Discrete_Choices (Variant));
791 end if;
792
793 if not Is_Empty_List (Choice_List) then
794 Case_Alt_Node := New_Node (N_Case_Statement_Alternative, Loc);
795 Set_Discrete_Choices (Case_Alt_Node, Choice_List);
796
797 -- In case this is a nested variant, we need to return the result
798 -- of the discriminant checking function for the immediately
799 -- enclosing variant.
800
801 if Present (Enclosing_Func_Id) then
802 Actuals_List := New_List;
803
804 D := First_Discriminant (Rec_Id);
805 while Present (D) loop
806 Append (Make_Identifier (Loc, Chars (D)), Actuals_List);
807 Next_Discriminant (D);
808 end loop;
809
810 Return_Node :=
811 Make_Return_Statement (Loc,
812 Expression =>
813 Make_Function_Call (Loc,
814 Name =>
815 New_Reference_To (Enclosing_Func_Id, Loc),
816 Parameter_Associations =>
817 Actuals_List));
818
819 else
820 Return_Node :=
821 Make_Return_Statement (Loc,
822 Expression =>
823 New_Reference_To (Standard_False, Loc));
824 end if;
825
826 Set_Statements (Case_Alt_Node, New_List (Return_Node));
827 Append (Case_Alt_Node, Alt_List);
828 end if;
829
830 Case_Alt_Node := New_Node (N_Case_Statement_Alternative, Loc);
831 Choice_List := New_List (New_Node (N_Others_Choice, Loc));
832 Set_Discrete_Choices (Case_Alt_Node, Choice_List);
833
834 Return_Node :=
835 Make_Return_Statement (Loc,
836 Expression =>
837 New_Reference_To (Standard_True, Loc));
838
839 Set_Statements (Case_Alt_Node, New_List (Return_Node));
840 Append (Case_Alt_Node, Alt_List);
841
842 Set_Alternatives (Case_Node, Alt_List);
843 return Case_Node;
844 end Build_Case_Statement;
845
846 ---------------------------
847 -- Build_Dcheck_Function --
848 ---------------------------
849
850 function Build_Dcheck_Function
851 (Case_Id : Entity_Id;
852 Variant : Node_Id) return Entity_Id
853 is
854 Body_Node : Node_Id;
855 Func_Id : Entity_Id;
856 Parameter_List : List_Id;
857 Spec_Node : Node_Id;
858
859 begin
860 Body_Node := New_Node (N_Subprogram_Body, Loc);
861 Sequence := Sequence + 1;
862
863 Func_Id :=
864 Make_Defining_Identifier (Loc,
865 Chars => New_External_Name (Chars (Rec_Id), 'D', Sequence));
866
867 Spec_Node := New_Node (N_Function_Specification, Loc);
868 Set_Defining_Unit_Name (Spec_Node, Func_Id);
869
870 Parameter_List := Build_Discriminant_Formals (Rec_Id, False);
871
872 Set_Parameter_Specifications (Spec_Node, Parameter_List);
873 Set_Result_Definition (Spec_Node,
874 New_Reference_To (Standard_Boolean, Loc));
875 Set_Specification (Body_Node, Spec_Node);
876 Set_Declarations (Body_Node, New_List);
877
878 Set_Handled_Statement_Sequence (Body_Node,
879 Make_Handled_Sequence_Of_Statements (Loc,
880 Statements => New_List (
881 Build_Case_Statement (Case_Id, Variant))));
882
883 Set_Ekind (Func_Id, E_Function);
884 Set_Mechanism (Func_Id, Default_Mechanism);
885 Set_Is_Inlined (Func_Id, True);
886 Set_Is_Pure (Func_Id, True);
887 Set_Is_Public (Func_Id, Is_Public (Rec_Id));
888 Set_Is_Internal (Func_Id, True);
889
890 if not Debug_Generated_Code then
891 Set_Debug_Info_Off (Func_Id);
892 end if;
893
894 Analyze (Body_Node);
895
896 Append_Freeze_Action (Rec_Id, Body_Node);
897 Set_Dcheck_Function (Variant, Func_Id);
898 return Func_Id;
899 end Build_Dcheck_Function;
900
901 ----------------------------
902 -- Build_Dcheck_Functions --
903 ----------------------------
904
905 procedure Build_Dcheck_Functions (Variant_Part_Node : Node_Id) is
906 Component_List_Node : Node_Id;
907 Decl : Entity_Id;
908 Discr_Name : Entity_Id;
909 Func_Id : Entity_Id;
910 Variant : Node_Id;
911 Saved_Enclosing_Func_Id : Entity_Id;
912
913 begin
914 -- Build the discriminant checking function for each variant, label
915 -- all components of that variant with the function's name.
916
917 Discr_Name := Entity (Name (Variant_Part_Node));
918 Variant := First_Non_Pragma (Variants (Variant_Part_Node));
919
920 while Present (Variant) loop
921 Func_Id := Build_Dcheck_Function (Discr_Name, Variant);
922 Component_List_Node := Component_List (Variant);
923
924 if not Null_Present (Component_List_Node) then
925 Decl :=
926 First_Non_Pragma (Component_Items (Component_List_Node));
927
928 while Present (Decl) loop
929 Set_Discriminant_Checking_Func
930 (Defining_Identifier (Decl), Func_Id);
931
932 Next_Non_Pragma (Decl);
933 end loop;
934
935 if Present (Variant_Part (Component_List_Node)) then
936 Saved_Enclosing_Func_Id := Enclosing_Func_Id;
937 Enclosing_Func_Id := Func_Id;
938 Build_Dcheck_Functions (Variant_Part (Component_List_Node));
939 Enclosing_Func_Id := Saved_Enclosing_Func_Id;
940 end if;
941 end if;
942
943 Next_Non_Pragma (Variant);
944 end loop;
945 end Build_Dcheck_Functions;
946
947 -- Start of processing for Build_Discr_Checking_Funcs
948
949 begin
950 -- Only build if not done already
951
952 if not Discr_Check_Funcs_Built (N) then
953 Type_Def := Type_Definition (N);
954
955 if Nkind (Type_Def) = N_Record_Definition then
956 if No (Component_List (Type_Def)) then -- null record.
957 return;
958 else
959 V := Variant_Part (Component_List (Type_Def));
960 end if;
961
962 else pragma Assert (Nkind (Type_Def) = N_Derived_Type_Definition);
963 if No (Component_List (Record_Extension_Part (Type_Def))) then
964 return;
965 else
966 V := Variant_Part
967 (Component_List (Record_Extension_Part (Type_Def)));
968 end if;
969 end if;
970
971 Rec_Id := Defining_Identifier (N);
972
973 if Present (V) and then not Is_Unchecked_Union (Rec_Id) then
974 Loc := Sloc (N);
975 Enclosing_Func_Id := Empty;
976 Build_Dcheck_Functions (V);
977 end if;
978
979 Set_Discr_Check_Funcs_Built (N);
980 end if;
981 end Build_Discr_Checking_Funcs;
982
983 --------------------------------
984 -- Build_Discriminant_Formals --
985 --------------------------------
986
987 function Build_Discriminant_Formals
988 (Rec_Id : Entity_Id;
989 Use_Dl : Boolean) return List_Id
990 is
991 Loc : Source_Ptr := Sloc (Rec_Id);
992 Parameter_List : constant List_Id := New_List;
993 D : Entity_Id;
994 Formal : Entity_Id;
995 Param_Spec_Node : Node_Id;
996
997 begin
998 if Has_Discriminants (Rec_Id) then
999 D := First_Discriminant (Rec_Id);
1000 while Present (D) loop
1001 Loc := Sloc (D);
1002
1003 if Use_Dl then
1004 Formal := Discriminal (D);
1005 else
1006 Formal := Make_Defining_Identifier (Loc, Chars (D));
1007 end if;
1008
1009 Param_Spec_Node :=
1010 Make_Parameter_Specification (Loc,
1011 Defining_Identifier => Formal,
1012 Parameter_Type =>
1013 New_Reference_To (Etype (D), Loc));
1014 Append (Param_Spec_Node, Parameter_List);
1015 Next_Discriminant (D);
1016 end loop;
1017 end if;
1018
1019 return Parameter_List;
1020 end Build_Discriminant_Formals;
1021
1022 -------------------------------
1023 -- Build_Initialization_Call --
1024 -------------------------------
1025
1026 -- References to a discriminant inside the record type declaration
1027 -- can appear either in the subtype_indication to constrain a
1028 -- record or an array, or as part of a larger expression given for
1029 -- the initial value of a component. In both of these cases N appears
1030 -- in the record initialization procedure and needs to be replaced by
1031 -- the formal parameter of the initialization procedure which
1032 -- corresponds to that discriminant.
1033
1034 -- In the example below, references to discriminants D1 and D2 in proc_1
1035 -- are replaced by references to formals with the same name
1036 -- (discriminals)
1037
1038 -- A similar replacement is done for calls to any record
1039 -- initialization procedure for any components that are themselves
1040 -- of a record type.
1041
1042 -- type R (D1, D2 : Integer) is record
1043 -- X : Integer := F * D1;
1044 -- Y : Integer := F * D2;
1045 -- end record;
1046
1047 -- procedure proc_1 (Out_2 : out R; D1 : Integer; D2 : Integer) is
1048 -- begin
1049 -- Out_2.D1 := D1;
1050 -- Out_2.D2 := D2;
1051 -- Out_2.X := F * D1;
1052 -- Out_2.Y := F * D2;
1053 -- end;
1054
1055 function Build_Initialization_Call
1056 (Loc : Source_Ptr;
1057 Id_Ref : Node_Id;
1058 Typ : Entity_Id;
1059 In_Init_Proc : Boolean := False;
1060 Enclos_Type : Entity_Id := Empty;
1061 Discr_Map : Elist_Id := New_Elmt_List;
1062 With_Default_Init : Boolean := False) return List_Id
1063 is
1064 First_Arg : Node_Id;
1065 Args : List_Id;
1066 Decls : List_Id;
1067 Decl : Node_Id;
1068 Discr : Entity_Id;
1069 Arg : Node_Id;
1070 Proc : constant Entity_Id := Base_Init_Proc (Typ);
1071 Init_Type : constant Entity_Id := Etype (First_Formal (Proc));
1072 Full_Init_Type : constant Entity_Id := Underlying_Type (Init_Type);
1073 Res : constant List_Id := New_List;
1074 Full_Type : Entity_Id := Typ;
1075 Controller_Typ : Entity_Id;
1076
1077 begin
1078 -- Nothing to do if the Init_Proc is null, unless Initialize_Scalars
1079 -- is active (in which case we make the call anyway, since in the
1080 -- actual compiled client it may be non null).
1081
1082 if Is_Null_Init_Proc (Proc) and then not Init_Or_Norm_Scalars then
1083 return Empty_List;
1084 end if;
1085
1086 -- Go to full view if private type. In the case of successive
1087 -- private derivations, this can require more than one step.
1088
1089 while Is_Private_Type (Full_Type)
1090 and then Present (Full_View (Full_Type))
1091 loop
1092 Full_Type := Full_View (Full_Type);
1093 end loop;
1094
1095 -- If Typ is derived, the procedure is the initialization procedure for
1096 -- the root type. Wrap the argument in an conversion to make it type
1097 -- honest. Actually it isn't quite type honest, because there can be
1098 -- conflicts of views in the private type case. That is why we set
1099 -- Conversion_OK in the conversion node.
1100 if (Is_Record_Type (Typ)
1101 or else Is_Array_Type (Typ)
1102 or else Is_Private_Type (Typ))
1103 and then Init_Type /= Base_Type (Typ)
1104 then
1105 First_Arg := OK_Convert_To (Etype (Init_Type), Id_Ref);
1106 Set_Etype (First_Arg, Init_Type);
1107
1108 else
1109 First_Arg := Id_Ref;
1110 end if;
1111
1112 Args := New_List (Convert_Concurrent (First_Arg, Typ));
1113
1114 -- In the tasks case, add _Master as the value of the _Master parameter
1115 -- and _Chain as the value of the _Chain parameter. At the outer level,
1116 -- these will be variables holding the corresponding values obtained
1117 -- from GNARL. At inner levels, they will be the parameters passed down
1118 -- through the outer routines.
1119
1120 if Has_Task (Full_Type) then
1121 if Restriction_Active (No_Task_Hierarchy) then
1122
1123 -- See comments in System.Tasking.Initialization.Init_RTS
1124 -- for the value 3 (should be rtsfindable constant ???)
1125
1126 Append_To (Args, Make_Integer_Literal (Loc, 3));
1127 else
1128 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
1129 end if;
1130
1131 Append_To (Args, Make_Identifier (Loc, Name_uChain));
1132
1133 -- Ada 2005 (AI-287): In case of default initialized components
1134 -- with tasks, we generate a null string actual parameter.
1135 -- This is just a workaround that must be improved later???
1136
1137 if With_Default_Init then
1138 Append_To (Args,
1139 Make_String_Literal (Loc,
1140 Strval => ""));
1141
1142 else
1143 Decls := Build_Task_Image_Decls (Loc, Id_Ref, Enclos_Type);
1144 Decl := Last (Decls);
1145
1146 Append_To (Args,
1147 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
1148 Append_List (Decls, Res);
1149 end if;
1150
1151 else
1152 Decls := No_List;
1153 Decl := Empty;
1154 end if;
1155
1156 -- Add discriminant values if discriminants are present
1157
1158 if Has_Discriminants (Full_Init_Type) then
1159 Discr := First_Discriminant (Full_Init_Type);
1160
1161 while Present (Discr) loop
1162
1163 -- If this is a discriminated concurrent type, the init_proc
1164 -- for the corresponding record is being called. Use that
1165 -- type directly to find the discriminant value, to handle
1166 -- properly intervening renamed discriminants.
1167
1168 declare
1169 T : Entity_Id := Full_Type;
1170
1171 begin
1172 if Is_Protected_Type (T) then
1173 T := Corresponding_Record_Type (T);
1174
1175 elsif Is_Private_Type (T)
1176 and then Present (Underlying_Full_View (T))
1177 and then Is_Protected_Type (Underlying_Full_View (T))
1178 then
1179 T := Corresponding_Record_Type (Underlying_Full_View (T));
1180 end if;
1181
1182 Arg :=
1183 Get_Discriminant_Value (
1184 Discr,
1185 T,
1186 Discriminant_Constraint (Full_Type));
1187 end;
1188
1189 if In_Init_Proc then
1190
1191 -- Replace any possible references to the discriminant in the
1192 -- call to the record initialization procedure with references
1193 -- to the appropriate formal parameter.
1194
1195 if Nkind (Arg) = N_Identifier
1196 and then Ekind (Entity (Arg)) = E_Discriminant
1197 then
1198 Arg := New_Reference_To (Discriminal (Entity (Arg)), Loc);
1199
1200 -- Case of access discriminants. We replace the reference
1201 -- to the type by a reference to the actual object
1202
1203 elsif Nkind (Arg) = N_Attribute_Reference
1204 and then Is_Access_Type (Etype (Arg))
1205 and then Is_Entity_Name (Prefix (Arg))
1206 and then Is_Type (Entity (Prefix (Arg)))
1207 then
1208 Arg :=
1209 Make_Attribute_Reference (Loc,
1210 Prefix => New_Copy (Prefix (Id_Ref)),
1211 Attribute_Name => Name_Unrestricted_Access);
1212
1213 -- Otherwise make a copy of the default expression. Note
1214 -- that we use the current Sloc for this, because we do not
1215 -- want the call to appear to be at the declaration point.
1216 -- Within the expression, replace discriminants with their
1217 -- discriminals.
1218
1219 else
1220 Arg :=
1221 New_Copy_Tree (Arg, Map => Discr_Map, New_Sloc => Loc);
1222 end if;
1223
1224 else
1225 if Is_Constrained (Full_Type) then
1226 Arg := Duplicate_Subexpr_No_Checks (Arg);
1227 else
1228 -- The constraints come from the discriminant default
1229 -- exps, they must be reevaluated, so we use New_Copy_Tree
1230 -- but we ensure the proper Sloc (for any embedded calls).
1231
1232 Arg := New_Copy_Tree (Arg, New_Sloc => Loc);
1233 end if;
1234 end if;
1235
1236 -- Ada 2005 (AI-287) In case of default initialized components,
1237 -- we need to generate the corresponding selected component node
1238 -- to access the discriminant value. In other cases this is not
1239 -- required because we are inside the init proc and we use the
1240 -- corresponding formal.
1241
1242 if With_Default_Init
1243 and then Nkind (Id_Ref) = N_Selected_Component
1244 then
1245 Append_To (Args,
1246 Make_Selected_Component (Loc,
1247 Prefix => New_Copy_Tree (Prefix (Id_Ref)),
1248 Selector_Name => Arg));
1249 else
1250 Append_To (Args, Arg);
1251 end if;
1252
1253 Next_Discriminant (Discr);
1254 end loop;
1255 end if;
1256
1257 -- If this is a call to initialize the parent component of a derived
1258 -- tagged type, indicate that the tag should not be set in the parent.
1259
1260 if Is_Tagged_Type (Full_Init_Type)
1261 and then not Is_CPP_Class (Full_Init_Type)
1262 and then Nkind (Id_Ref) = N_Selected_Component
1263 and then Chars (Selector_Name (Id_Ref)) = Name_uParent
1264 then
1265 Append_To (Args, New_Occurrence_Of (Standard_False, Loc));
1266 end if;
1267
1268 Append_To (Res,
1269 Make_Procedure_Call_Statement (Loc,
1270 Name => New_Occurrence_Of (Proc, Loc),
1271 Parameter_Associations => Args));
1272
1273 if Controlled_Type (Typ)
1274 and then Nkind (Id_Ref) = N_Selected_Component
1275 then
1276 if Chars (Selector_Name (Id_Ref)) /= Name_uParent then
1277 Append_List_To (Res,
1278 Make_Init_Call (
1279 Ref => New_Copy_Tree (First_Arg),
1280 Typ => Typ,
1281 Flist_Ref =>
1282 Find_Final_List (Typ, New_Copy_Tree (First_Arg)),
1283 With_Attach => Make_Integer_Literal (Loc, 1)));
1284
1285 -- If the enclosing type is an extension with new controlled
1286 -- components, it has his own record controller. If the parent
1287 -- also had a record controller, attach it to the new one.
1288 -- Build_Init_Statements relies on the fact that in this specific
1289 -- case the last statement of the result is the attach call to
1290 -- the controller. If this is changed, it must be synchronized.
1291
1292 elsif Present (Enclos_Type)
1293 and then Has_New_Controlled_Component (Enclos_Type)
1294 and then Has_Controlled_Component (Typ)
1295 then
1296 if Is_Return_By_Reference_Type (Typ) then
1297 Controller_Typ := RTE (RE_Limited_Record_Controller);
1298 else
1299 Controller_Typ := RTE (RE_Record_Controller);
1300 end if;
1301
1302 Append_List_To (Res,
1303 Make_Init_Call (
1304 Ref =>
1305 Make_Selected_Component (Loc,
1306 Prefix => New_Copy_Tree (First_Arg),
1307 Selector_Name => Make_Identifier (Loc, Name_uController)),
1308 Typ => Controller_Typ,
1309 Flist_Ref => Find_Final_List (Typ, New_Copy_Tree (First_Arg)),
1310 With_Attach => Make_Integer_Literal (Loc, 1)));
1311 end if;
1312 end if;
1313
1314 return Res;
1315
1316 exception
1317 when RE_Not_Available =>
1318 return Empty_List;
1319 end Build_Initialization_Call;
1320
1321 ---------------------------
1322 -- Build_Master_Renaming --
1323 ---------------------------
1324
1325 procedure Build_Master_Renaming (N : Node_Id; T : Entity_Id) is
1326 Loc : constant Source_Ptr := Sloc (N);
1327 M_Id : Entity_Id;
1328 Decl : Node_Id;
1329
1330 begin
1331 -- Nothing to do if there is no task hierarchy
1332
1333 if Restriction_Active (No_Task_Hierarchy) then
1334 return;
1335 end if;
1336
1337 M_Id :=
1338 Make_Defining_Identifier (Loc,
1339 New_External_Name (Chars (T), 'M'));
1340
1341 Decl :=
1342 Make_Object_Renaming_Declaration (Loc,
1343 Defining_Identifier => M_Id,
1344 Subtype_Mark => New_Reference_To (RTE (RE_Master_Id), Loc),
1345 Name => Make_Identifier (Loc, Name_uMaster));
1346 Insert_Before (N, Decl);
1347 Analyze (Decl);
1348
1349 Set_Master_Id (T, M_Id);
1350
1351 exception
1352 when RE_Not_Available =>
1353 return;
1354 end Build_Master_Renaming;
1355
1356 ----------------------------
1357 -- Build_Record_Init_Proc --
1358 ----------------------------
1359
1360 procedure Build_Record_Init_Proc (N : Node_Id; Pe : Entity_Id) is
1361 Loc : Source_Ptr := Sloc (N);
1362 Discr_Map : constant Elist_Id := New_Elmt_List;
1363 Proc_Id : Entity_Id;
1364 Rec_Type : Entity_Id;
1365 Set_Tag : Entity_Id := Empty;
1366
1367 function Build_Assignment (Id : Entity_Id; N : Node_Id) return List_Id;
1368 -- Build a assignment statement node which assigns to record
1369 -- component its default expression if defined. The left hand side
1370 -- of the assignment is marked Assignment_OK so that initialization
1371 -- of limited private records works correctly, Return also the
1372 -- adjustment call for controlled objects
1373
1374 procedure Build_Discriminant_Assignments (Statement_List : List_Id);
1375 -- If the record has discriminants, adds assignment statements to
1376 -- statement list to initialize the discriminant values from the
1377 -- arguments of the initialization procedure.
1378
1379 function Build_Init_Statements (Comp_List : Node_Id) return List_Id;
1380 -- Build a list representing a sequence of statements which initialize
1381 -- components of the given component list. This may involve building
1382 -- case statements for the variant parts.
1383
1384 function Build_Init_Call_Thru (Parameters : List_Id) return List_Id;
1385 -- Given a non-tagged type-derivation that declares discriminants,
1386 -- such as
1387 --
1388 -- type R (R1, R2 : Integer) is record ... end record;
1389 --
1390 -- type D (D1 : Integer) is new R (1, D1);
1391 --
1392 -- we make the _init_proc of D be
1393 --
1394 -- procedure _init_proc(X : D; D1 : Integer) is
1395 -- begin
1396 -- _init_proc( R(X), 1, D1);
1397 -- end _init_proc;
1398 --
1399 -- This function builds the call statement in this _init_proc.
1400
1401 procedure Build_Init_Procedure;
1402 -- Build the tree corresponding to the procedure specification and body
1403 -- of the initialization procedure (by calling all the preceding
1404 -- auxiliary routines), and install it as the _init TSS.
1405
1406 procedure Build_Record_Checks (S : Node_Id; Check_List : List_Id);
1407 -- Add range checks to components of disciminated records. S is a
1408 -- subtype indication of a record component. Check_List is a list
1409 -- to which the check actions are appended.
1410
1411 function Component_Needs_Simple_Initialization
1412 (T : Entity_Id) return Boolean;
1413 -- Determines if a component needs simple initialization, given its type
1414 -- T. This is the same as Needs_Simple_Initialization except for the
1415 -- following difference: the types Tag, Interface_Tag, and Vtable_Ptr
1416 -- which are access types which would normally require simple
1417 -- initialization to null, do not require initialization as components,
1418 -- since they are explicitly initialized by other means.
1419
1420 procedure Constrain_Array
1421 (SI : Node_Id;
1422 Check_List : List_Id);
1423 -- Called from Build_Record_Checks.
1424 -- Apply a list of index constraints to an unconstrained array type.
1425 -- The first parameter is the entity for the resulting subtype.
1426 -- Check_List is a list to which the check actions are appended.
1427
1428 procedure Constrain_Index
1429 (Index : Node_Id;
1430 S : Node_Id;
1431 Check_List : List_Id);
1432 -- Called from Build_Record_Checks.
1433 -- Process an index constraint in a constrained array declaration.
1434 -- The constraint can be a subtype name, or a range with or without
1435 -- an explicit subtype mark. The index is the corresponding index of the
1436 -- unconstrained array. S is the range expression. Check_List is a list
1437 -- to which the check actions are appended.
1438
1439 function Parent_Subtype_Renaming_Discrims return Boolean;
1440 -- Returns True for base types N that rename discriminants, else False
1441
1442 function Requires_Init_Proc (Rec_Id : Entity_Id) return Boolean;
1443 -- Determines whether a record initialization procedure needs to be
1444 -- generated for the given record type.
1445
1446 ----------------------
1447 -- Build_Assignment --
1448 ----------------------
1449
1450 function Build_Assignment (Id : Entity_Id; N : Node_Id) return List_Id is
1451 Exp : Node_Id := N;
1452 Lhs : Node_Id;
1453 Typ : constant Entity_Id := Underlying_Type (Etype (Id));
1454 Kind : Node_Kind := Nkind (N);
1455 Res : List_Id;
1456
1457 begin
1458 Loc := Sloc (N);
1459 Lhs :=
1460 Make_Selected_Component (Loc,
1461 Prefix => Make_Identifier (Loc, Name_uInit),
1462 Selector_Name => New_Occurrence_Of (Id, Loc));
1463 Set_Assignment_OK (Lhs);
1464
1465 -- Case of an access attribute applied to the current instance.
1466 -- Replace the reference to the type by a reference to the actual
1467 -- object. (Note that this handles the case of the top level of
1468 -- the expression being given by such an attribute, but does not
1469 -- cover uses nested within an initial value expression. Nested
1470 -- uses are unlikely to occur in practice, but are theoretically
1471 -- possible. It is not clear how to handle them without fully
1472 -- traversing the expression. ???
1473
1474 if Kind = N_Attribute_Reference
1475 and then (Attribute_Name (N) = Name_Unchecked_Access
1476 or else
1477 Attribute_Name (N) = Name_Unrestricted_Access)
1478 and then Is_Entity_Name (Prefix (N))
1479 and then Is_Type (Entity (Prefix (N)))
1480 and then Entity (Prefix (N)) = Rec_Type
1481 then
1482 Exp :=
1483 Make_Attribute_Reference (Loc,
1484 Prefix => Make_Identifier (Loc, Name_uInit),
1485 Attribute_Name => Name_Unrestricted_Access);
1486 end if;
1487
1488 -- Ada 2005 (AI-231): Add the run-time check if required
1489
1490 if Ada_Version >= Ada_05
1491 and then Can_Never_Be_Null (Etype (Id)) -- Lhs
1492 then
1493 if Nkind (Exp) = N_Null then
1494 return New_List (
1495 Make_Raise_Constraint_Error (Sloc (Exp),
1496 Reason => CE_Null_Not_Allowed));
1497
1498 elsif Present (Etype (Exp))
1499 and then not Can_Never_Be_Null (Etype (Exp))
1500 then
1501 Install_Null_Excluding_Check (Exp);
1502 end if;
1503 end if;
1504
1505 -- Take a copy of Exp to ensure that later copies of this
1506 -- component_declaration in derived types see the original tree,
1507 -- not a node rewritten during expansion of the init_proc.
1508
1509 Exp := New_Copy_Tree (Exp);
1510
1511 Res := New_List (
1512 Make_Assignment_Statement (Loc,
1513 Name => Lhs,
1514 Expression => Exp));
1515
1516 Set_No_Ctrl_Actions (First (Res));
1517
1518 -- Adjust the tag if tagged (because of possible view conversions).
1519 -- Suppress the tag adjustment when Java_VM because JVM tags are
1520 -- represented implicitly in objects.
1521
1522 if Is_Tagged_Type (Typ) and then not Java_VM then
1523 Append_To (Res,
1524 Make_Assignment_Statement (Loc,
1525 Name =>
1526 Make_Selected_Component (Loc,
1527 Prefix => New_Copy_Tree (Lhs),
1528 Selector_Name =>
1529 New_Reference_To (First_Tag_Component (Typ), Loc)),
1530
1531 Expression =>
1532 Unchecked_Convert_To (RTE (RE_Tag),
1533 New_Reference_To
1534 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc))));
1535 end if;
1536
1537 -- Adjust the component if controlled except if it is an
1538 -- aggregate that will be expanded inline
1539
1540 if Kind = N_Qualified_Expression then
1541 Kind := Nkind (Expression (N));
1542 end if;
1543
1544 if Controlled_Type (Typ)
1545 and then not (Kind = N_Aggregate or else Kind = N_Extension_Aggregate)
1546 then
1547 Append_List_To (Res,
1548 Make_Adjust_Call (
1549 Ref => New_Copy_Tree (Lhs),
1550 Typ => Etype (Id),
1551 Flist_Ref =>
1552 Find_Final_List (Etype (Id), New_Copy_Tree (Lhs)),
1553 With_Attach => Make_Integer_Literal (Loc, 1)));
1554 end if;
1555
1556 return Res;
1557
1558 exception
1559 when RE_Not_Available =>
1560 return Empty_List;
1561 end Build_Assignment;
1562
1563 ------------------------------------
1564 -- Build_Discriminant_Assignments --
1565 ------------------------------------
1566
1567 procedure Build_Discriminant_Assignments (Statement_List : List_Id) is
1568 D : Entity_Id;
1569 Is_Tagged : constant Boolean := Is_Tagged_Type (Rec_Type);
1570
1571 begin
1572 if Has_Discriminants (Rec_Type)
1573 and then not Is_Unchecked_Union (Rec_Type)
1574 then
1575 D := First_Discriminant (Rec_Type);
1576
1577 while Present (D) loop
1578 -- Don't generate the assignment for discriminants in derived
1579 -- tagged types if the discriminant is a renaming of some
1580 -- ancestor discriminant. This initialization will be done
1581 -- when initializing the _parent field of the derived record.
1582
1583 if Is_Tagged and then
1584 Present (Corresponding_Discriminant (D))
1585 then
1586 null;
1587
1588 else
1589 Loc := Sloc (D);
1590 Append_List_To (Statement_List,
1591 Build_Assignment (D,
1592 New_Reference_To (Discriminal (D), Loc)));
1593 end if;
1594
1595 Next_Discriminant (D);
1596 end loop;
1597 end if;
1598 end Build_Discriminant_Assignments;
1599
1600 --------------------------
1601 -- Build_Init_Call_Thru --
1602 --------------------------
1603
1604 function Build_Init_Call_Thru (Parameters : List_Id) return List_Id is
1605 Parent_Proc : constant Entity_Id :=
1606 Base_Init_Proc (Etype (Rec_Type));
1607
1608 Parent_Type : constant Entity_Id :=
1609 Etype (First_Formal (Parent_Proc));
1610
1611 Uparent_Type : constant Entity_Id :=
1612 Underlying_Type (Parent_Type);
1613
1614 First_Discr_Param : Node_Id;
1615
1616 Parent_Discr : Entity_Id;
1617 First_Arg : Node_Id;
1618 Args : List_Id;
1619 Arg : Node_Id;
1620 Res : List_Id;
1621
1622 begin
1623 -- First argument (_Init) is the object to be initialized.
1624 -- ??? not sure where to get a reasonable Loc for First_Arg
1625
1626 First_Arg :=
1627 OK_Convert_To (Parent_Type,
1628 New_Reference_To (Defining_Identifier (First (Parameters)), Loc));
1629
1630 Set_Etype (First_Arg, Parent_Type);
1631
1632 Args := New_List (Convert_Concurrent (First_Arg, Rec_Type));
1633
1634 -- In the tasks case,
1635 -- add _Master as the value of the _Master parameter
1636 -- add _Chain as the value of the _Chain parameter.
1637 -- add _Task_Name as the value of the _Task_Name parameter.
1638 -- At the outer level, these will be variables holding the
1639 -- corresponding values obtained from GNARL or the expander.
1640 --
1641 -- At inner levels, they will be the parameters passed down through
1642 -- the outer routines.
1643
1644 First_Discr_Param := Next (First (Parameters));
1645
1646 if Has_Task (Rec_Type) then
1647 if Restriction_Active (No_Task_Hierarchy) then
1648
1649 -- See comments in System.Tasking.Initialization.Init_RTS
1650 -- for the value 3.
1651
1652 Append_To (Args, Make_Integer_Literal (Loc, 3));
1653 else
1654 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
1655 end if;
1656
1657 Append_To (Args, Make_Identifier (Loc, Name_uChain));
1658 Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
1659 First_Discr_Param := Next (Next (Next (First_Discr_Param)));
1660 end if;
1661
1662 -- Append discriminant values
1663
1664 if Has_Discriminants (Uparent_Type) then
1665 pragma Assert (not Is_Tagged_Type (Uparent_Type));
1666
1667 Parent_Discr := First_Discriminant (Uparent_Type);
1668 while Present (Parent_Discr) loop
1669
1670 -- Get the initial value for this discriminant
1671 -- ??? needs to be cleaned up to use parent_Discr_Constr
1672 -- directly.
1673
1674 declare
1675 Discr_Value : Elmt_Id :=
1676 First_Elmt
1677 (Stored_Constraint (Rec_Type));
1678
1679 Discr : Entity_Id :=
1680 First_Stored_Discriminant (Uparent_Type);
1681 begin
1682 while Original_Record_Component (Parent_Discr) /= Discr loop
1683 Next_Stored_Discriminant (Discr);
1684 Next_Elmt (Discr_Value);
1685 end loop;
1686
1687 Arg := Node (Discr_Value);
1688 end;
1689
1690 -- Append it to the list
1691
1692 if Nkind (Arg) = N_Identifier
1693 and then Ekind (Entity (Arg)) = E_Discriminant
1694 then
1695 Append_To (Args,
1696 New_Reference_To (Discriminal (Entity (Arg)), Loc));
1697
1698 -- Case of access discriminants. We replace the reference
1699 -- to the type by a reference to the actual object
1700
1701 -- ??? why is this code deleted without comment
1702
1703 -- elsif Nkind (Arg) = N_Attribute_Reference
1704 -- and then Is_Entity_Name (Prefix (Arg))
1705 -- and then Is_Type (Entity (Prefix (Arg)))
1706 -- then
1707 -- Append_To (Args,
1708 -- Make_Attribute_Reference (Loc,
1709 -- Prefix => New_Copy (Prefix (Id_Ref)),
1710 -- Attribute_Name => Name_Unrestricted_Access));
1711
1712 else
1713 Append_To (Args, New_Copy (Arg));
1714 end if;
1715
1716 Next_Discriminant (Parent_Discr);
1717 end loop;
1718 end if;
1719
1720 Res :=
1721 New_List (
1722 Make_Procedure_Call_Statement (Loc,
1723 Name => New_Occurrence_Of (Parent_Proc, Loc),
1724 Parameter_Associations => Args));
1725
1726 return Res;
1727 end Build_Init_Call_Thru;
1728
1729 --------------------------
1730 -- Build_Init_Procedure --
1731 --------------------------
1732
1733 procedure Build_Init_Procedure is
1734 Body_Node : Node_Id;
1735 Handled_Stmt_Node : Node_Id;
1736 Parameters : List_Id;
1737 Proc_Spec_Node : Node_Id;
1738 Body_Stmts : List_Id;
1739 Record_Extension_Node : Node_Id;
1740 Init_Tag : Node_Id;
1741
1742 procedure Init_Secondary_Tags (Typ : Entity_Id);
1743 -- Ada 2005 (AI-251): Initialize the tags of all the secondary
1744 -- tables associated with abstract interface types
1745
1746 -------------------------
1747 -- Init_Secondary_Tags --
1748 -------------------------
1749
1750 procedure Init_Secondary_Tags (Typ : Entity_Id) is
1751 ADT : Elmt_Id;
1752
1753 procedure Init_Secondary_Tags_Internal (Typ : Entity_Id);
1754 -- Internal subprogram used to recursively climb to the root type
1755
1756 ----------------------------------
1757 -- Init_Secondary_Tags_Internal --
1758 ----------------------------------
1759
1760 procedure Init_Secondary_Tags_Internal (Typ : Entity_Id) is
1761 E : Entity_Id;
1762 Aux_N : Node_Id;
1763
1764 begin
1765 if not Is_Interface (Typ) then
1766
1767 -- Climb to the ancestor (if any) handling private types
1768
1769 if Present (Full_View (Etype (Typ))) then
1770 if Full_View (Etype (Typ)) /= Typ then
1771 Init_Secondary_Tags_Internal (Full_View (Etype (Typ)));
1772 end if;
1773
1774 elsif Etype (Typ) /= Typ then
1775 Init_Secondary_Tags_Internal (Etype (Typ));
1776 end if;
1777 end if;
1778
1779 if Present (Abstract_Interfaces (Typ))
1780 and then not Is_Empty_Elmt_List (Abstract_Interfaces (Typ))
1781 then
1782 E := First_Entity (Typ);
1783 while Present (E) loop
1784 if Is_Tag (E)
1785 and then Chars (E) /= Name_uTag
1786 then
1787 Aux_N := Node (ADT);
1788 pragma Assert (Present (Aux_N));
1789
1790 -- Initialize the pointer to the secondary DT
1791 -- associated with the interface
1792
1793 Append_To (Body_Stmts,
1794 Make_Assignment_Statement (Loc,
1795 Name =>
1796 Make_Selected_Component (Loc,
1797 Prefix => Make_Identifier (Loc, Name_uInit),
1798 Selector_Name =>
1799 New_Reference_To (E, Loc)),
1800 Expression =>
1801 New_Reference_To (Aux_N, Loc)));
1802
1803 -- Generate:
1804 -- Set_Offset_To_Top (DT_Ptr, n);
1805
1806 Append_To (Body_Stmts,
1807 Make_Procedure_Call_Statement (Loc,
1808 Name => New_Reference_To
1809 (RTE (RE_Set_Offset_To_Top), Loc),
1810 Parameter_Associations => New_List (
1811 Unchecked_Convert_To (RTE (RE_Tag),
1812 New_Reference_To (Aux_N, Loc)),
1813 Unchecked_Convert_To (RTE (RE_Storage_Offset),
1814 Make_Attribute_Reference (Loc,
1815 Prefix =>
1816 Make_Selected_Component (Loc,
1817 Prefix => Make_Identifier (Loc,
1818 Name_uInit),
1819 Selector_Name => New_Reference_To
1820 (E, Loc)),
1821 Attribute_Name => Name_Position)))));
1822
1823 Next_Elmt (ADT);
1824 end if;
1825
1826 Next_Entity (E);
1827 end loop;
1828 end if;
1829 end Init_Secondary_Tags_Internal;
1830
1831 -- Start of processing for Init_Secondary_Tags
1832
1833 begin
1834 -- Skip the first _Tag, which is the main tag of the
1835 -- tagged type. Following tags correspond with abstract
1836 -- interfaces.
1837
1838 ADT := Next_Elmt (First_Elmt (Access_Disp_Table (Typ)));
1839
1840 -- Handle private types
1841
1842 if Present (Full_View (Typ)) then
1843 Init_Secondary_Tags_Internal (Full_View (Typ));
1844 else
1845 Init_Secondary_Tags_Internal (Typ);
1846 end if;
1847 end Init_Secondary_Tags;
1848
1849 -- Start of processing for Build_Init_Procedure
1850
1851 begin
1852 Body_Stmts := New_List;
1853 Body_Node := New_Node (N_Subprogram_Body, Loc);
1854
1855 Proc_Id :=
1856 Make_Defining_Identifier (Loc,
1857 Chars => Make_Init_Proc_Name (Rec_Type));
1858 Set_Ekind (Proc_Id, E_Procedure);
1859
1860 Proc_Spec_Node := New_Node (N_Procedure_Specification, Loc);
1861 Set_Defining_Unit_Name (Proc_Spec_Node, Proc_Id);
1862
1863 Parameters := Init_Formals (Rec_Type);
1864 Append_List_To (Parameters,
1865 Build_Discriminant_Formals (Rec_Type, True));
1866
1867 -- For tagged types, we add a flag to indicate whether the routine
1868 -- is called to initialize a parent component in the init_proc of
1869 -- a type extension. If the flag is false, we do not set the tag
1870 -- because it has been set already in the extension.
1871
1872 if Is_Tagged_Type (Rec_Type)
1873 and then not Is_CPP_Class (Rec_Type)
1874 then
1875 Set_Tag :=
1876 Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
1877
1878 Append_To (Parameters,
1879 Make_Parameter_Specification (Loc,
1880 Defining_Identifier => Set_Tag,
1881 Parameter_Type => New_Occurrence_Of (Standard_Boolean, Loc),
1882 Expression => New_Occurrence_Of (Standard_True, Loc)));
1883 end if;
1884
1885 Set_Parameter_Specifications (Proc_Spec_Node, Parameters);
1886 Set_Specification (Body_Node, Proc_Spec_Node);
1887 Set_Declarations (Body_Node, New_List);
1888
1889 if Parent_Subtype_Renaming_Discrims then
1890
1891 -- N is a Derived_Type_Definition that renames the parameters
1892 -- of the ancestor type. We init it by expanding our discrims
1893 -- and call the ancestor _init_proc with a type-converted object
1894
1895 Append_List_To (Body_Stmts,
1896 Build_Init_Call_Thru (Parameters));
1897
1898 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
1899 Build_Discriminant_Assignments (Body_Stmts);
1900
1901 if not Null_Present (Type_Definition (N)) then
1902 Append_List_To (Body_Stmts,
1903 Build_Init_Statements (
1904 Component_List (Type_Definition (N))));
1905 end if;
1906
1907 else
1908 -- N is a Derived_Type_Definition with a possible non-empty
1909 -- extension. The initialization of a type extension consists
1910 -- in the initialization of the components in the extension.
1911
1912 Build_Discriminant_Assignments (Body_Stmts);
1913
1914 Record_Extension_Node :=
1915 Record_Extension_Part (Type_Definition (N));
1916
1917 if not Null_Present (Record_Extension_Node) then
1918 declare
1919 Stmts : constant List_Id :=
1920 Build_Init_Statements (
1921 Component_List (Record_Extension_Node));
1922
1923 begin
1924 -- The parent field must be initialized first because
1925 -- the offset of the new discriminants may depend on it
1926
1927 Prepend_To (Body_Stmts, Remove_Head (Stmts));
1928 Append_List_To (Body_Stmts, Stmts);
1929 end;
1930 end if;
1931 end if;
1932
1933 -- Add here the assignment to instantiate the Tag
1934
1935 -- The assignement corresponds to the code:
1936
1937 -- _Init._Tag := Typ'Tag;
1938
1939 -- Suppress the tag assignment when Java_VM because JVM tags are
1940 -- represented implicitly in objects.
1941
1942 if Is_Tagged_Type (Rec_Type)
1943 and then not Is_CPP_Class (Rec_Type)
1944 and then not Java_VM
1945 then
1946 Init_Tag :=
1947 Make_Assignment_Statement (Loc,
1948 Name =>
1949 Make_Selected_Component (Loc,
1950 Prefix => Make_Identifier (Loc, Name_uInit),
1951 Selector_Name =>
1952 New_Reference_To (First_Tag_Component (Rec_Type), Loc)),
1953
1954 Expression =>
1955 New_Reference_To
1956 (Node (First_Elmt (Access_Disp_Table (Rec_Type))), Loc));
1957
1958 -- The tag must be inserted before the assignments to other
1959 -- components, because the initial value of the component may
1960 -- depend ot the tag (eg. through a dispatching operation on
1961 -- an access to the current type). The tag assignment is not done
1962 -- when initializing the parent component of a type extension,
1963 -- because in that case the tag is set in the extension.
1964 -- Extensions of imported C++ classes add a final complication,
1965 -- because we cannot inhibit tag setting in the constructor for
1966 -- the parent. In that case we insert the tag initialization
1967 -- after the calls to initialize the parent.
1968
1969 Init_Tag :=
1970 Make_If_Statement (Loc,
1971 Condition => New_Occurrence_Of (Set_Tag, Loc),
1972 Then_Statements => New_List (Init_Tag));
1973
1974 if not Is_CPP_Class (Etype (Rec_Type)) then
1975 Prepend_To (Body_Stmts, Init_Tag);
1976
1977 -- Ada 2005 (AI-251): Initialization of all the tags
1978 -- corresponding with abstract interfaces
1979
1980 if Ada_Version >= Ada_05
1981 and then not Is_Interface (Rec_Type)
1982 then
1983 Init_Secondary_Tags (Rec_Type);
1984 end if;
1985
1986 else
1987 declare
1988 Nod : Node_Id := First (Body_Stmts);
1989
1990 begin
1991 -- We assume the first init_proc call is for the parent
1992
1993 while Present (Next (Nod))
1994 and then (Nkind (Nod) /= N_Procedure_Call_Statement
1995 or else not Is_Init_Proc (Name (Nod)))
1996 loop
1997 Nod := Next (Nod);
1998 end loop;
1999
2000 Insert_After (Nod, Init_Tag);
2001 end;
2002 end if;
2003 end if;
2004
2005 Handled_Stmt_Node := New_Node (N_Handled_Sequence_Of_Statements, Loc);
2006 Set_Statements (Handled_Stmt_Node, Body_Stmts);
2007 Set_Exception_Handlers (Handled_Stmt_Node, No_List);
2008 Set_Handled_Statement_Sequence (Body_Node, Handled_Stmt_Node);
2009
2010 if not Debug_Generated_Code then
2011 Set_Debug_Info_Off (Proc_Id);
2012 end if;
2013
2014 -- Associate Init_Proc with type, and determine if the procedure
2015 -- is null (happens because of the Initialize_Scalars pragma case,
2016 -- where we have to generate a null procedure in case it is called
2017 -- by a client with Initialize_Scalars set). Such procedures have
2018 -- to be generated, but do not have to be called, so we mark them
2019 -- as null to suppress the call.
2020
2021 Set_Init_Proc (Rec_Type, Proc_Id);
2022
2023 if List_Length (Body_Stmts) = 1
2024 and then Nkind (First (Body_Stmts)) = N_Null_Statement
2025 then
2026 Set_Is_Null_Init_Proc (Proc_Id);
2027 end if;
2028 end Build_Init_Procedure;
2029
2030 ---------------------------
2031 -- Build_Init_Statements --
2032 ---------------------------
2033
2034 function Build_Init_Statements (Comp_List : Node_Id) return List_Id is
2035 Check_List : constant List_Id := New_List;
2036 Alt_List : List_Id;
2037 Statement_List : List_Id;
2038 Stmts : List_Id;
2039
2040 Per_Object_Constraint_Components : Boolean;
2041
2042 Decl : Node_Id;
2043 Variant : Node_Id;
2044
2045 Id : Entity_Id;
2046 Typ : Entity_Id;
2047
2048 function Has_Access_Constraint (E : Entity_Id) return Boolean;
2049 -- Components with access discriminants that depend on the current
2050 -- instance must be initialized after all other components.
2051
2052 ---------------------------
2053 -- Has_Access_Constraint --
2054 ---------------------------
2055
2056 function Has_Access_Constraint (E : Entity_Id) return Boolean is
2057 Disc : Entity_Id;
2058 T : constant Entity_Id := Etype (E);
2059
2060 begin
2061 if Has_Per_Object_Constraint (E)
2062 and then Has_Discriminants (T)
2063 then
2064 Disc := First_Discriminant (T);
2065 while Present (Disc) loop
2066 if Is_Access_Type (Etype (Disc)) then
2067 return True;
2068 end if;
2069
2070 Next_Discriminant (Disc);
2071 end loop;
2072
2073 return False;
2074 else
2075 return False;
2076 end if;
2077 end Has_Access_Constraint;
2078
2079 -- Start of processing for Build_Init_Statements
2080
2081 begin
2082 if Null_Present (Comp_List) then
2083 return New_List (Make_Null_Statement (Loc));
2084 end if;
2085
2086 Statement_List := New_List;
2087
2088 -- Loop through components, skipping pragmas, in 2 steps. The first
2089 -- step deals with regular components. The second step deals with
2090 -- components have per object constraints, and no explicit initia-
2091 -- lization.
2092
2093 Per_Object_Constraint_Components := False;
2094
2095 -- First step : regular components
2096
2097 Decl := First_Non_Pragma (Component_Items (Comp_List));
2098 while Present (Decl) loop
2099 Loc := Sloc (Decl);
2100 Build_Record_Checks
2101 (Subtype_Indication (Component_Definition (Decl)), Check_List);
2102
2103 Id := Defining_Identifier (Decl);
2104 Typ := Etype (Id);
2105
2106 if Has_Access_Constraint (Id)
2107 and then No (Expression (Decl))
2108 then
2109 -- Skip processing for now and ask for a second pass
2110
2111 Per_Object_Constraint_Components := True;
2112
2113 else
2114 -- Case of explicit initialization
2115
2116 if Present (Expression (Decl)) then
2117 Stmts := Build_Assignment (Id, Expression (Decl));
2118
2119 -- Case of composite component with its own Init_Proc
2120
2121 elsif Has_Non_Null_Base_Init_Proc (Typ) then
2122 Stmts :=
2123 Build_Initialization_Call
2124 (Loc,
2125 Make_Selected_Component (Loc,
2126 Prefix => Make_Identifier (Loc, Name_uInit),
2127 Selector_Name => New_Occurrence_Of (Id, Loc)),
2128 Typ,
2129 True,
2130 Rec_Type,
2131 Discr_Map => Discr_Map);
2132
2133 -- Case of component needing simple initialization
2134
2135 elsif Component_Needs_Simple_Initialization (Typ) then
2136 Stmts :=
2137 Build_Assignment
2138 (Id, Get_Simple_Init_Val (Typ, Loc, Esize (Id)));
2139
2140 -- Nothing needed for this case
2141
2142 else
2143 Stmts := No_List;
2144 end if;
2145
2146 if Present (Check_List) then
2147 Append_List_To (Statement_List, Check_List);
2148 end if;
2149
2150 if Present (Stmts) then
2151
2152 -- Add the initialization of the record controller before
2153 -- the _Parent field is attached to it when the attachment
2154 -- can occur. It does not work to simply initialize the
2155 -- controller first: it must be initialized after the parent
2156 -- if the parent holds discriminants that can be used
2157 -- to compute the offset of the controller. We assume here
2158 -- that the last statement of the initialization call is the
2159 -- attachement of the parent (see Build_Initialization_Call)
2160
2161 if Chars (Id) = Name_uController
2162 and then Rec_Type /= Etype (Rec_Type)
2163 and then Has_Controlled_Component (Etype (Rec_Type))
2164 and then Has_New_Controlled_Component (Rec_Type)
2165 then
2166 Insert_List_Before (Last (Statement_List), Stmts);
2167 else
2168 Append_List_To (Statement_List, Stmts);
2169 end if;
2170 end if;
2171 end if;
2172
2173 Next_Non_Pragma (Decl);
2174 end loop;
2175
2176 if Per_Object_Constraint_Components then
2177
2178 -- Second pass: components with per-object constraints
2179
2180 Decl := First_Non_Pragma (Component_Items (Comp_List));
2181
2182 while Present (Decl) loop
2183 Loc := Sloc (Decl);
2184 Id := Defining_Identifier (Decl);
2185 Typ := Etype (Id);
2186
2187 if Has_Access_Constraint (Id)
2188 and then No (Expression (Decl))
2189 then
2190 if Has_Non_Null_Base_Init_Proc (Typ) then
2191 Append_List_To (Statement_List,
2192 Build_Initialization_Call (Loc,
2193 Make_Selected_Component (Loc,
2194 Prefix => Make_Identifier (Loc, Name_uInit),
2195 Selector_Name => New_Occurrence_Of (Id, Loc)),
2196 Typ, True, Rec_Type, Discr_Map => Discr_Map));
2197
2198 elsif Component_Needs_Simple_Initialization (Typ) then
2199 Append_List_To (Statement_List,
2200 Build_Assignment
2201 (Id, Get_Simple_Init_Val (Typ, Loc, Esize (Id))));
2202 end if;
2203 end if;
2204
2205 Next_Non_Pragma (Decl);
2206 end loop;
2207 end if;
2208
2209 -- Process the variant part
2210
2211 if Present (Variant_Part (Comp_List)) then
2212 Alt_List := New_List;
2213 Variant := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
2214
2215 while Present (Variant) loop
2216 Loc := Sloc (Variant);
2217 Append_To (Alt_List,
2218 Make_Case_Statement_Alternative (Loc,
2219 Discrete_Choices =>
2220 New_Copy_List (Discrete_Choices (Variant)),
2221 Statements =>
2222 Build_Init_Statements (Component_List (Variant))));
2223
2224 Next_Non_Pragma (Variant);
2225 end loop;
2226
2227 -- The expression of the case statement which is a reference
2228 -- to one of the discriminants is replaced by the appropriate
2229 -- formal parameter of the initialization procedure.
2230
2231 Append_To (Statement_List,
2232 Make_Case_Statement (Loc,
2233 Expression =>
2234 New_Reference_To (Discriminal (
2235 Entity (Name (Variant_Part (Comp_List)))), Loc),
2236 Alternatives => Alt_List));
2237 end if;
2238
2239 -- For a task record type, add the task create call and calls
2240 -- to bind any interrupt (signal) entries.
2241
2242 if Is_Task_Record_Type (Rec_Type) then
2243
2244 -- In the case of the restricted run time the ATCB has already
2245 -- been preallocated.
2246
2247 if Restricted_Profile then
2248 Append_To (Statement_List,
2249 Make_Assignment_Statement (Loc,
2250 Name => Make_Selected_Component (Loc,
2251 Prefix => Make_Identifier (Loc, Name_uInit),
2252 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)),
2253 Expression => Make_Attribute_Reference (Loc,
2254 Prefix =>
2255 Make_Selected_Component (Loc,
2256 Prefix => Make_Identifier (Loc, Name_uInit),
2257 Selector_Name =>
2258 Make_Identifier (Loc, Name_uATCB)),
2259 Attribute_Name => Name_Unchecked_Access)));
2260 end if;
2261
2262 Append_To (Statement_List, Make_Task_Create_Call (Rec_Type));
2263
2264 declare
2265 Task_Type : constant Entity_Id :=
2266 Corresponding_Concurrent_Type (Rec_Type);
2267 Task_Decl : constant Node_Id := Parent (Task_Type);
2268 Task_Def : constant Node_Id := Task_Definition (Task_Decl);
2269 Vis_Decl : Node_Id;
2270 Ent : Entity_Id;
2271
2272 begin
2273 if Present (Task_Def) then
2274 Vis_Decl := First (Visible_Declarations (Task_Def));
2275 while Present (Vis_Decl) loop
2276 Loc := Sloc (Vis_Decl);
2277
2278 if Nkind (Vis_Decl) = N_Attribute_Definition_Clause then
2279 if Get_Attribute_Id (Chars (Vis_Decl)) =
2280 Attribute_Address
2281 then
2282 Ent := Entity (Name (Vis_Decl));
2283
2284 if Ekind (Ent) = E_Entry then
2285 Append_To (Statement_List,
2286 Make_Procedure_Call_Statement (Loc,
2287 Name => New_Reference_To (
2288 RTE (RE_Bind_Interrupt_To_Entry), Loc),
2289 Parameter_Associations => New_List (
2290 Make_Selected_Component (Loc,
2291 Prefix =>
2292 Make_Identifier (Loc, Name_uInit),
2293 Selector_Name =>
2294 Make_Identifier (Loc, Name_uTask_Id)),
2295 Entry_Index_Expression (
2296 Loc, Ent, Empty, Task_Type),
2297 Expression (Vis_Decl))));
2298 end if;
2299 end if;
2300 end if;
2301
2302 Next (Vis_Decl);
2303 end loop;
2304 end if;
2305 end;
2306 end if;
2307
2308 -- For a protected type, add statements generated by
2309 -- Make_Initialize_Protection.
2310
2311 if Is_Protected_Record_Type (Rec_Type) then
2312 Append_List_To (Statement_List,
2313 Make_Initialize_Protection (Rec_Type));
2314 end if;
2315
2316 -- If no initializations when generated for component declarations
2317 -- corresponding to this Statement_List, append a null statement
2318 -- to the Statement_List to make it a valid Ada tree.
2319
2320 if Is_Empty_List (Statement_List) then
2321 Append (New_Node (N_Null_Statement, Loc), Statement_List);
2322 end if;
2323
2324 return Statement_List;
2325
2326 exception
2327 when RE_Not_Available =>
2328 return Empty_List;
2329 end Build_Init_Statements;
2330
2331 -------------------------
2332 -- Build_Record_Checks --
2333 -------------------------
2334
2335 procedure Build_Record_Checks (S : Node_Id; Check_List : List_Id) is
2336 Subtype_Mark_Id : Entity_Id;
2337
2338 begin
2339 if Nkind (S) = N_Subtype_Indication then
2340 Find_Type (Subtype_Mark (S));
2341 Subtype_Mark_Id := Entity (Subtype_Mark (S));
2342
2343 -- Remaining processing depends on type
2344
2345 case Ekind (Subtype_Mark_Id) is
2346
2347 when Array_Kind =>
2348 Constrain_Array (S, Check_List);
2349
2350 when others =>
2351 null;
2352 end case;
2353 end if;
2354 end Build_Record_Checks;
2355
2356 -------------------------------------------
2357 -- Component_Needs_Simple_Initialization --
2358 -------------------------------------------
2359
2360 function Component_Needs_Simple_Initialization
2361 (T : Entity_Id) return Boolean
2362 is
2363 begin
2364 return
2365 Needs_Simple_Initialization (T)
2366 and then not Is_RTE (T, RE_Tag)
2367 and then not Is_RTE (T, RE_Vtable_Ptr)
2368 and then not Is_RTE (T, RE_Interface_Tag); -- Ada 2005 (AI-251)
2369 end Component_Needs_Simple_Initialization;
2370
2371 ---------------------
2372 -- Constrain_Array --
2373 ---------------------
2374
2375 procedure Constrain_Array
2376 (SI : Node_Id;
2377 Check_List : List_Id)
2378 is
2379 C : constant Node_Id := Constraint (SI);
2380 Number_Of_Constraints : Nat := 0;
2381 Index : Node_Id;
2382 S, T : Entity_Id;
2383
2384 begin
2385 T := Entity (Subtype_Mark (SI));
2386
2387 if Ekind (T) in Access_Kind then
2388 T := Designated_Type (T);
2389 end if;
2390
2391 S := First (Constraints (C));
2392
2393 while Present (S) loop
2394 Number_Of_Constraints := Number_Of_Constraints + 1;
2395 Next (S);
2396 end loop;
2397
2398 -- In either case, the index constraint must provide a discrete
2399 -- range for each index of the array type and the type of each
2400 -- discrete range must be the same as that of the corresponding
2401 -- index. (RM 3.6.1)
2402
2403 S := First (Constraints (C));
2404 Index := First_Index (T);
2405 Analyze (Index);
2406
2407 -- Apply constraints to each index type
2408
2409 for J in 1 .. Number_Of_Constraints loop
2410 Constrain_Index (Index, S, Check_List);
2411 Next (Index);
2412 Next (S);
2413 end loop;
2414
2415 end Constrain_Array;
2416
2417 ---------------------
2418 -- Constrain_Index --
2419 ---------------------
2420
2421 procedure Constrain_Index
2422 (Index : Node_Id;
2423 S : Node_Id;
2424 Check_List : List_Id)
2425 is
2426 T : constant Entity_Id := Etype (Index);
2427
2428 begin
2429 if Nkind (S) = N_Range then
2430 Process_Range_Expr_In_Decl (S, T, Check_List);
2431 end if;
2432 end Constrain_Index;
2433
2434 --------------------------------------
2435 -- Parent_Subtype_Renaming_Discrims --
2436 --------------------------------------
2437
2438 function Parent_Subtype_Renaming_Discrims return Boolean is
2439 De : Entity_Id;
2440 Dp : Entity_Id;
2441
2442 begin
2443 if Base_Type (Pe) /= Pe then
2444 return False;
2445 end if;
2446
2447 if Etype (Pe) = Pe
2448 or else not Has_Discriminants (Pe)
2449 or else Is_Constrained (Pe)
2450 or else Is_Tagged_Type (Pe)
2451 then
2452 return False;
2453 end if;
2454
2455 -- If there are no explicit stored discriminants we have inherited
2456 -- the root type discriminants so far, so no renamings occurred.
2457
2458 if First_Discriminant (Pe) = First_Stored_Discriminant (Pe) then
2459 return False;
2460 end if;
2461
2462 -- Check if we have done some trivial renaming of the parent
2463 -- discriminants, i.e. someting like
2464 --
2465 -- type DT (X1,X2: int) is new PT (X1,X2);
2466
2467 De := First_Discriminant (Pe);
2468 Dp := First_Discriminant (Etype (Pe));
2469
2470 while Present (De) loop
2471 pragma Assert (Present (Dp));
2472
2473 if Corresponding_Discriminant (De) /= Dp then
2474 return True;
2475 end if;
2476
2477 Next_Discriminant (De);
2478 Next_Discriminant (Dp);
2479 end loop;
2480
2481 return Present (Dp);
2482 end Parent_Subtype_Renaming_Discrims;
2483
2484 ------------------------
2485 -- Requires_Init_Proc --
2486 ------------------------
2487
2488 function Requires_Init_Proc (Rec_Id : Entity_Id) return Boolean is
2489 Comp_Decl : Node_Id;
2490 Id : Entity_Id;
2491 Typ : Entity_Id;
2492
2493 begin
2494 -- Definitely do not need one if specifically suppressed
2495
2496 if Suppress_Init_Proc (Rec_Id) then
2497 return False;
2498 end if;
2499
2500 -- If it is a type derived from a type with unknown discriminants,
2501 -- we cannot build an initialization procedure for it.
2502
2503 if Has_Unknown_Discriminants (Rec_Id) then
2504 return False;
2505 end if;
2506
2507 -- Otherwise we need to generate an initialization procedure if
2508 -- Is_CPP_Class is False and at least one of the following applies:
2509
2510 -- 1. Discriminants are present, since they need to be initialized
2511 -- with the appropriate discriminant constraint expressions.
2512 -- However, the discriminant of an unchecked union does not
2513 -- count, since the discriminant is not present.
2514
2515 -- 2. The type is a tagged type, since the implicit Tag component
2516 -- needs to be initialized with a pointer to the dispatch table.
2517
2518 -- 3. The type contains tasks
2519
2520 -- 4. One or more components has an initial value
2521
2522 -- 5. One or more components is for a type which itself requires
2523 -- an initialization procedure.
2524
2525 -- 6. One or more components is a type that requires simple
2526 -- initialization (see Needs_Simple_Initialization), except
2527 -- that types Tag and Interface_Tag are excluded, since fields
2528 -- of these types are initialized by other means.
2529
2530 -- 7. The type is the record type built for a task type (since at
2531 -- the very least, Create_Task must be called)
2532
2533 -- 8. The type is the record type built for a protected type (since
2534 -- at least Initialize_Protection must be called)
2535
2536 -- 9. The type is marked as a public entity. The reason we add this
2537 -- case (even if none of the above apply) is to properly handle
2538 -- Initialize_Scalars. If a package is compiled without an IS
2539 -- pragma, and the client is compiled with an IS pragma, then
2540 -- the client will think an initialization procedure is present
2541 -- and call it, when in fact no such procedure is required, but
2542 -- since the call is generated, there had better be a routine
2543 -- at the other end of the call, even if it does nothing!)
2544
2545 -- Note: the reason we exclude the CPP_Class case is ???
2546
2547 if Is_CPP_Class (Rec_Id) then
2548 return False;
2549
2550 elsif not Restriction_Active (No_Initialize_Scalars)
2551 and then Is_Public (Rec_Id)
2552 then
2553 return True;
2554
2555 elsif (Has_Discriminants (Rec_Id)
2556 and then not Is_Unchecked_Union (Rec_Id))
2557 or else Is_Tagged_Type (Rec_Id)
2558 or else Is_Concurrent_Record_Type (Rec_Id)
2559 or else Has_Task (Rec_Id)
2560 then
2561 return True;
2562 end if;
2563
2564 Id := First_Component (Rec_Id);
2565
2566 while Present (Id) loop
2567 Comp_Decl := Parent (Id);
2568 Typ := Etype (Id);
2569
2570 if Present (Expression (Comp_Decl))
2571 or else Has_Non_Null_Base_Init_Proc (Typ)
2572 or else Component_Needs_Simple_Initialization (Typ)
2573 then
2574 return True;
2575 end if;
2576
2577 Next_Component (Id);
2578 end loop;
2579
2580 return False;
2581 end Requires_Init_Proc;
2582
2583 -- Start of processing for Build_Record_Init_Proc
2584
2585 begin
2586 Rec_Type := Defining_Identifier (N);
2587
2588 -- This may be full declaration of a private type, in which case
2589 -- the visible entity is a record, and the private entity has been
2590 -- exchanged with it in the private part of the current package.
2591 -- The initialization procedure is built for the record type, which
2592 -- is retrievable from the private entity.
2593
2594 if Is_Incomplete_Or_Private_Type (Rec_Type) then
2595 Rec_Type := Underlying_Type (Rec_Type);
2596 end if;
2597
2598 -- If there are discriminants, build the discriminant map to replace
2599 -- discriminants by their discriminals in complex bound expressions.
2600 -- These only arise for the corresponding records of protected types.
2601
2602 if Is_Concurrent_Record_Type (Rec_Type)
2603 and then Has_Discriminants (Rec_Type)
2604 then
2605 declare
2606 Disc : Entity_Id;
2607
2608 begin
2609 Disc := First_Discriminant (Rec_Type);
2610
2611 while Present (Disc) loop
2612 Append_Elmt (Disc, Discr_Map);
2613 Append_Elmt (Discriminal (Disc), Discr_Map);
2614 Next_Discriminant (Disc);
2615 end loop;
2616 end;
2617 end if;
2618
2619 -- Derived types that have no type extension can use the initialization
2620 -- procedure of their parent and do not need a procedure of their own.
2621 -- This is only correct if there are no representation clauses for the
2622 -- type or its parent, and if the parent has in fact been frozen so
2623 -- that its initialization procedure exists.
2624
2625 if Is_Derived_Type (Rec_Type)
2626 and then not Is_Tagged_Type (Rec_Type)
2627 and then not Is_Unchecked_Union (Rec_Type)
2628 and then not Has_New_Non_Standard_Rep (Rec_Type)
2629 and then not Parent_Subtype_Renaming_Discrims
2630 and then Has_Non_Null_Base_Init_Proc (Etype (Rec_Type))
2631 then
2632 Copy_TSS (Base_Init_Proc (Etype (Rec_Type)), Rec_Type);
2633
2634 -- Otherwise if we need an initialization procedure, then build one,
2635 -- mark it as public and inlinable and as having a completion.
2636
2637 elsif Requires_Init_Proc (Rec_Type)
2638 or else Is_Unchecked_Union (Rec_Type)
2639 then
2640 Build_Init_Procedure;
2641 Set_Is_Public (Proc_Id, Is_Public (Pe));
2642
2643 -- The initialization of protected records is not worth inlining.
2644 -- In addition, when compiled for another unit for inlining purposes,
2645 -- it may make reference to entities that have not been elaborated
2646 -- yet. The initialization of controlled records contains a nested
2647 -- clean-up procedure that makes it impractical to inline as well,
2648 -- and leads to undefined symbols if inlined in a different unit.
2649 -- Similar considerations apply to task types.
2650
2651 if not Is_Concurrent_Type (Rec_Type)
2652 and then not Has_Task (Rec_Type)
2653 and then not Controlled_Type (Rec_Type)
2654 then
2655 Set_Is_Inlined (Proc_Id);
2656 end if;
2657
2658 Set_Is_Internal (Proc_Id);
2659 Set_Has_Completion (Proc_Id);
2660
2661 if not Debug_Generated_Code then
2662 Set_Debug_Info_Off (Proc_Id);
2663 end if;
2664 end if;
2665 end Build_Record_Init_Proc;
2666
2667 ----------------------------
2668 -- Build_Slice_Assignment --
2669 ----------------------------
2670
2671 -- Generates the following subprogram:
2672
2673 -- procedure Assign
2674 -- (Source, Target : Array_Type,
2675 -- Left_Lo, Left_Hi, Right_Lo, Right_Hi : Index;
2676 -- Rev : Boolean)
2677 -- is
2678 -- Li1 : Index;
2679 -- Ri1 : Index;
2680
2681 -- begin
2682 -- if Rev then
2683 -- Li1 := Left_Hi;
2684 -- Ri1 := Right_Hi;
2685 -- else
2686 -- Li1 := Left_Lo;
2687 -- Ri1 := Right_Lo;
2688 -- end if;
2689
2690 -- loop
2691 -- if Rev then
2692 -- exit when Li1 < Left_Lo;
2693 -- else
2694 -- exit when Li1 > Left_Hi;
2695 -- end if;
2696
2697 -- Target (Li1) := Source (Ri1);
2698
2699 -- if Rev then
2700 -- Li1 := Index'pred (Li1);
2701 -- Ri1 := Index'pred (Ri1);
2702 -- else
2703 -- Li1 := Index'succ (Li1);
2704 -- Ri1 := Index'succ (Ri1);
2705 -- end if;
2706 -- end loop;
2707 -- end Assign;
2708
2709 procedure Build_Slice_Assignment (Typ : Entity_Id) is
2710 Loc : constant Source_Ptr := Sloc (Typ);
2711 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
2712
2713 -- Build formal parameters of procedure
2714
2715 Larray : constant Entity_Id :=
2716 Make_Defining_Identifier
2717 (Loc, Chars => New_Internal_Name ('A'));
2718 Rarray : constant Entity_Id :=
2719 Make_Defining_Identifier
2720 (Loc, Chars => New_Internal_Name ('R'));
2721 Left_Lo : constant Entity_Id :=
2722 Make_Defining_Identifier
2723 (Loc, Chars => New_Internal_Name ('L'));
2724 Left_Hi : constant Entity_Id :=
2725 Make_Defining_Identifier
2726 (Loc, Chars => New_Internal_Name ('L'));
2727 Right_Lo : constant Entity_Id :=
2728 Make_Defining_Identifier
2729 (Loc, Chars => New_Internal_Name ('R'));
2730 Right_Hi : constant Entity_Id :=
2731 Make_Defining_Identifier
2732 (Loc, Chars => New_Internal_Name ('R'));
2733 Rev : constant Entity_Id :=
2734 Make_Defining_Identifier
2735 (Loc, Chars => New_Internal_Name ('D'));
2736 Proc_Name : constant Entity_Id :=
2737 Make_Defining_Identifier (Loc,
2738 Chars => Make_TSS_Name (Typ, TSS_Slice_Assign));
2739
2740 Lnn : constant Entity_Id :=
2741 Make_Defining_Identifier (Loc, New_Internal_Name ('L'));
2742 Rnn : constant Entity_Id :=
2743 Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
2744 -- Subscripts for left and right sides
2745
2746 Decls : List_Id;
2747 Loops : Node_Id;
2748 Stats : List_Id;
2749
2750 begin
2751 -- Build declarations for indices
2752
2753 Decls := New_List;
2754
2755 Append_To (Decls,
2756 Make_Object_Declaration (Loc,
2757 Defining_Identifier => Lnn,
2758 Object_Definition =>
2759 New_Occurrence_Of (Index, Loc)));
2760
2761 Append_To (Decls,
2762 Make_Object_Declaration (Loc,
2763 Defining_Identifier => Rnn,
2764 Object_Definition =>
2765 New_Occurrence_Of (Index, Loc)));
2766
2767 Stats := New_List;
2768
2769 -- Build initializations for indices
2770
2771 declare
2772 F_Init : constant List_Id := New_List;
2773 B_Init : constant List_Id := New_List;
2774
2775 begin
2776 Append_To (F_Init,
2777 Make_Assignment_Statement (Loc,
2778 Name => New_Occurrence_Of (Lnn, Loc),
2779 Expression => New_Occurrence_Of (Left_Lo, Loc)));
2780
2781 Append_To (F_Init,
2782 Make_Assignment_Statement (Loc,
2783 Name => New_Occurrence_Of (Rnn, Loc),
2784 Expression => New_Occurrence_Of (Right_Lo, Loc)));
2785
2786 Append_To (B_Init,
2787 Make_Assignment_Statement (Loc,
2788 Name => New_Occurrence_Of (Lnn, Loc),
2789 Expression => New_Occurrence_Of (Left_Hi, Loc)));
2790
2791 Append_To (B_Init,
2792 Make_Assignment_Statement (Loc,
2793 Name => New_Occurrence_Of (Rnn, Loc),
2794 Expression => New_Occurrence_Of (Right_Hi, Loc)));
2795
2796 Append_To (Stats,
2797 Make_If_Statement (Loc,
2798 Condition => New_Occurrence_Of (Rev, Loc),
2799 Then_Statements => B_Init,
2800 Else_Statements => F_Init));
2801 end;
2802
2803 -- Now construct the assignment statement
2804
2805 Loops :=
2806 Make_Loop_Statement (Loc,
2807 Statements => New_List (
2808 Make_Assignment_Statement (Loc,
2809 Name =>
2810 Make_Indexed_Component (Loc,
2811 Prefix => New_Occurrence_Of (Larray, Loc),
2812 Expressions => New_List (New_Occurrence_Of (Lnn, Loc))),
2813 Expression =>
2814 Make_Indexed_Component (Loc,
2815 Prefix => New_Occurrence_Of (Rarray, Loc),
2816 Expressions => New_List (New_Occurrence_Of (Rnn, Loc))))),
2817 End_Label => Empty);
2818
2819 -- Build exit condition
2820
2821 declare
2822 F_Ass : constant List_Id := New_List;
2823 B_Ass : constant List_Id := New_List;
2824
2825 begin
2826 Append_To (F_Ass,
2827 Make_Exit_Statement (Loc,
2828 Condition =>
2829 Make_Op_Gt (Loc,
2830 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
2831 Right_Opnd => New_Occurrence_Of (Left_Hi, Loc))));
2832
2833 Append_To (B_Ass,
2834 Make_Exit_Statement (Loc,
2835 Condition =>
2836 Make_Op_Lt (Loc,
2837 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
2838 Right_Opnd => New_Occurrence_Of (Left_Lo, Loc))));
2839
2840 Prepend_To (Statements (Loops),
2841 Make_If_Statement (Loc,
2842 Condition => New_Occurrence_Of (Rev, Loc),
2843 Then_Statements => B_Ass,
2844 Else_Statements => F_Ass));
2845 end;
2846
2847 -- Build the increment/decrement statements
2848
2849 declare
2850 F_Ass : constant List_Id := New_List;
2851 B_Ass : constant List_Id := New_List;
2852
2853 begin
2854 Append_To (F_Ass,
2855 Make_Assignment_Statement (Loc,
2856 Name => New_Occurrence_Of (Lnn, Loc),
2857 Expression =>
2858 Make_Attribute_Reference (Loc,
2859 Prefix =>
2860 New_Occurrence_Of (Index, Loc),
2861 Attribute_Name => Name_Succ,
2862 Expressions => New_List (
2863 New_Occurrence_Of (Lnn, Loc)))));
2864
2865 Append_To (F_Ass,
2866 Make_Assignment_Statement (Loc,
2867 Name => New_Occurrence_Of (Rnn, Loc),
2868 Expression =>
2869 Make_Attribute_Reference (Loc,
2870 Prefix =>
2871 New_Occurrence_Of (Index, Loc),
2872 Attribute_Name => Name_Succ,
2873 Expressions => New_List (
2874 New_Occurrence_Of (Rnn, Loc)))));
2875
2876 Append_To (B_Ass,
2877 Make_Assignment_Statement (Loc,
2878 Name => New_Occurrence_Of (Lnn, Loc),
2879 Expression =>
2880 Make_Attribute_Reference (Loc,
2881 Prefix =>
2882 New_Occurrence_Of (Index, Loc),
2883 Attribute_Name => Name_Pred,
2884 Expressions => New_List (
2885 New_Occurrence_Of (Lnn, Loc)))));
2886
2887 Append_To (B_Ass,
2888 Make_Assignment_Statement (Loc,
2889 Name => New_Occurrence_Of (Rnn, Loc),
2890 Expression =>
2891 Make_Attribute_Reference (Loc,
2892 Prefix =>
2893 New_Occurrence_Of (Index, Loc),
2894 Attribute_Name => Name_Pred,
2895 Expressions => New_List (
2896 New_Occurrence_Of (Rnn, Loc)))));
2897
2898 Append_To (Statements (Loops),
2899 Make_If_Statement (Loc,
2900 Condition => New_Occurrence_Of (Rev, Loc),
2901 Then_Statements => B_Ass,
2902 Else_Statements => F_Ass));
2903 end;
2904
2905 Append_To (Stats, Loops);
2906
2907 declare
2908 Spec : Node_Id;
2909 Formals : List_Id := New_List;
2910
2911 begin
2912 Formals := New_List (
2913 Make_Parameter_Specification (Loc,
2914 Defining_Identifier => Larray,
2915 Out_Present => True,
2916 Parameter_Type =>
2917 New_Reference_To (Base_Type (Typ), Loc)),
2918
2919 Make_Parameter_Specification (Loc,
2920 Defining_Identifier => Rarray,
2921 Parameter_Type =>
2922 New_Reference_To (Base_Type (Typ), Loc)),
2923
2924 Make_Parameter_Specification (Loc,
2925 Defining_Identifier => Left_Lo,
2926 Parameter_Type =>
2927 New_Reference_To (Index, Loc)),
2928
2929 Make_Parameter_Specification (Loc,
2930 Defining_Identifier => Left_Hi,
2931 Parameter_Type =>
2932 New_Reference_To (Index, Loc)),
2933
2934 Make_Parameter_Specification (Loc,
2935 Defining_Identifier => Right_Lo,
2936 Parameter_Type =>
2937 New_Reference_To (Index, Loc)),
2938
2939 Make_Parameter_Specification (Loc,
2940 Defining_Identifier => Right_Hi,
2941 Parameter_Type =>
2942 New_Reference_To (Index, Loc)));
2943
2944 Append_To (Formals,
2945 Make_Parameter_Specification (Loc,
2946 Defining_Identifier => Rev,
2947 Parameter_Type =>
2948 New_Reference_To (Standard_Boolean, Loc)));
2949
2950 Spec :=
2951 Make_Procedure_Specification (Loc,
2952 Defining_Unit_Name => Proc_Name,
2953 Parameter_Specifications => Formals);
2954
2955 Discard_Node (
2956 Make_Subprogram_Body (Loc,
2957 Specification => Spec,
2958 Declarations => Decls,
2959 Handled_Statement_Sequence =>
2960 Make_Handled_Sequence_Of_Statements (Loc,
2961 Statements => Stats)));
2962 end;
2963
2964 Set_TSS (Typ, Proc_Name);
2965 Set_Is_Pure (Proc_Name);
2966 end Build_Slice_Assignment;
2967
2968 ------------------------------------
2969 -- Build_Variant_Record_Equality --
2970 ------------------------------------
2971
2972 -- Generates:
2973
2974 -- function _Equality (X, Y : T) return Boolean is
2975 -- begin
2976 -- -- Compare discriminants
2977
2978 -- if False or else X.D1 /= Y.D1 or else X.D2 /= Y.D2 then
2979 -- return False;
2980 -- end if;
2981
2982 -- -- Compare components
2983
2984 -- if False or else X.C1 /= Y.C1 or else X.C2 /= Y.C2 then
2985 -- return False;
2986 -- end if;
2987
2988 -- -- Compare variant part
2989
2990 -- case X.D1 is
2991 -- when V1 =>
2992 -- if False or else X.C2 /= Y.C2 or else X.C3 /= Y.C3 then
2993 -- return False;
2994 -- end if;
2995 -- ...
2996 -- when Vn =>
2997 -- if False or else X.Cn /= Y.Cn then
2998 -- return False;
2999 -- end if;
3000 -- end case;
3001 -- return True;
3002 -- end _Equality;
3003
3004 procedure Build_Variant_Record_Equality (Typ : Entity_Id) is
3005 Loc : constant Source_Ptr := Sloc (Typ);
3006
3007 F : constant Entity_Id :=
3008 Make_Defining_Identifier (Loc,
3009 Chars => Make_TSS_Name (Typ, TSS_Composite_Equality));
3010
3011 X : constant Entity_Id :=
3012 Make_Defining_Identifier (Loc,
3013 Chars => Name_X);
3014
3015 Y : constant Entity_Id :=
3016 Make_Defining_Identifier (Loc,
3017 Chars => Name_Y);
3018
3019 Def : constant Node_Id := Parent (Typ);
3020 Comps : constant Node_Id := Component_List (Type_Definition (Def));
3021 Stmts : constant List_Id := New_List;
3022 Pspecs : constant List_Id := New_List;
3023
3024 begin
3025 -- Derived Unchecked_Union types no longer inherit the equality function
3026 -- of their parent.
3027
3028 if Is_Derived_Type (Typ)
3029 and then not Is_Unchecked_Union (Typ)
3030 and then not Has_New_Non_Standard_Rep (Typ)
3031 then
3032 declare
3033 Parent_Eq : constant Entity_Id :=
3034 TSS (Root_Type (Typ), TSS_Composite_Equality);
3035
3036 begin
3037 if Present (Parent_Eq) then
3038 Copy_TSS (Parent_Eq, Typ);
3039 return;
3040 end if;
3041 end;
3042 end if;
3043
3044 Discard_Node (
3045 Make_Subprogram_Body (Loc,
3046 Specification =>
3047 Make_Function_Specification (Loc,
3048 Defining_Unit_Name => F,
3049 Parameter_Specifications => Pspecs,
3050 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
3051 Declarations => New_List,
3052 Handled_Statement_Sequence =>
3053 Make_Handled_Sequence_Of_Statements (Loc,
3054 Statements => Stmts)));
3055
3056 Append_To (Pspecs,
3057 Make_Parameter_Specification (Loc,
3058 Defining_Identifier => X,
3059 Parameter_Type => New_Reference_To (Typ, Loc)));
3060
3061 Append_To (Pspecs,
3062 Make_Parameter_Specification (Loc,
3063 Defining_Identifier => Y,
3064 Parameter_Type => New_Reference_To (Typ, Loc)));
3065
3066 -- Unchecked_Unions require additional machinery to support equality.
3067 -- Two extra parameters (A and B) are added to the equality function
3068 -- parameter list in order to capture the inferred values of the
3069 -- discriminants in later calls.
3070
3071 if Is_Unchecked_Union (Typ) then
3072 declare
3073 Discr_Type : constant Node_Id := Etype (First_Discriminant (Typ));
3074
3075 A : constant Node_Id :=
3076 Make_Defining_Identifier (Loc,
3077 Chars => Name_A);
3078
3079 B : constant Node_Id :=
3080 Make_Defining_Identifier (Loc,
3081 Chars => Name_B);
3082
3083 begin
3084 -- Add A and B to the parameter list
3085
3086 Append_To (Pspecs,
3087 Make_Parameter_Specification (Loc,
3088 Defining_Identifier => A,
3089 Parameter_Type => New_Reference_To (Discr_Type, Loc)));
3090
3091 Append_To (Pspecs,
3092 Make_Parameter_Specification (Loc,
3093 Defining_Identifier => B,
3094 Parameter_Type => New_Reference_To (Discr_Type, Loc)));
3095
3096 -- Generate the following header code to compare the inferred
3097 -- discriminants:
3098
3099 -- if a /= b then
3100 -- return False;
3101 -- end if;
3102
3103 Append_To (Stmts,
3104 Make_If_Statement (Loc,
3105 Condition =>
3106 Make_Op_Ne (Loc,
3107 Left_Opnd => New_Reference_To (A, Loc),
3108 Right_Opnd => New_Reference_To (B, Loc)),
3109 Then_Statements => New_List (
3110 Make_Return_Statement (Loc,
3111 Expression => New_Occurrence_Of (Standard_False, Loc)))));
3112
3113 -- Generate component-by-component comparison. Note that we must
3114 -- propagate one of the inferred discriminant formals to act as
3115 -- the case statement switch.
3116
3117 Append_List_To (Stmts,
3118 Make_Eq_Case (Typ, Comps, A));
3119
3120 end;
3121
3122 -- Normal case (not unchecked union)
3123
3124 else
3125 Append_To (Stmts,
3126 Make_Eq_If (Typ,
3127 Discriminant_Specifications (Def)));
3128
3129 Append_List_To (Stmts,
3130 Make_Eq_Case (Typ, Comps));
3131 end if;
3132
3133 Append_To (Stmts,
3134 Make_Return_Statement (Loc,
3135 Expression => New_Reference_To (Standard_True, Loc)));
3136
3137 Set_TSS (Typ, F);
3138 Set_Is_Pure (F);
3139
3140 if not Debug_Generated_Code then
3141 Set_Debug_Info_Off (F);
3142 end if;
3143 end Build_Variant_Record_Equality;
3144
3145 -----------------------------
3146 -- Check_Stream_Attributes --
3147 -----------------------------
3148
3149 procedure Check_Stream_Attributes (Typ : Entity_Id) is
3150 Comp : Entity_Id;
3151 Par_Read : constant Boolean :=
3152 Stream_Attribute_Available (Typ, TSS_Stream_Read)
3153 and then not Has_Specified_Stream_Read (Typ);
3154 Par_Write : constant Boolean :=
3155 Stream_Attribute_Available (Typ, TSS_Stream_Write)
3156 and then not Has_Specified_Stream_Write (Typ);
3157
3158 procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type);
3159 -- Check that Comp has a user-specified Nam stream attribute
3160
3161 ----------------
3162 -- Check_Attr --
3163 ----------------
3164
3165 procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type) is
3166 begin
3167 if not Stream_Attribute_Available (Etype (Comp), TSS_Nam) then
3168 Error_Msg_Name_1 := Nam;
3169 Error_Msg_N
3170 ("|component& in limited extension must have% attribute", Comp);
3171 end if;
3172 end Check_Attr;
3173
3174 -- Start of processing for Check_Stream_Attributes
3175
3176 begin
3177 if Par_Read or else Par_Write then
3178 Comp := First_Component (Typ);
3179 while Present (Comp) loop
3180 if Comes_From_Source (Comp)
3181 and then Original_Record_Component (Comp) = Comp
3182 and then Is_Limited_Type (Etype (Comp))
3183 then
3184 if Par_Read then
3185 Check_Attr (Name_Read, TSS_Stream_Read);
3186 end if;
3187
3188 if Par_Write then
3189 Check_Attr (Name_Write, TSS_Stream_Write);
3190 end if;
3191 end if;
3192
3193 Next_Component (Comp);
3194 end loop;
3195 end if;
3196 end Check_Stream_Attributes;
3197
3198 -----------------------------
3199 -- Expand_Record_Extension --
3200 -----------------------------
3201
3202 -- Add a field _parent at the beginning of the record extension. This is
3203 -- used to implement inheritance. Here are some examples of expansion:
3204
3205 -- 1. no discriminants
3206 -- type T2 is new T1 with null record;
3207 -- gives
3208 -- type T2 is new T1 with record
3209 -- _Parent : T1;
3210 -- end record;
3211
3212 -- 2. renamed discriminants
3213 -- type T2 (B, C : Int) is new T1 (A => B) with record
3214 -- _Parent : T1 (A => B);
3215 -- D : Int;
3216 -- end;
3217
3218 -- 3. inherited discriminants
3219 -- type T2 is new T1 with record -- discriminant A inherited
3220 -- _Parent : T1 (A);
3221 -- D : Int;
3222 -- end;
3223
3224 procedure Expand_Record_Extension (T : Entity_Id; Def : Node_Id) is
3225 Indic : constant Node_Id := Subtype_Indication (Def);
3226 Loc : constant Source_Ptr := Sloc (Def);
3227 Rec_Ext_Part : Node_Id := Record_Extension_Part (Def);
3228 Par_Subtype : Entity_Id;
3229 Comp_List : Node_Id;
3230 Comp_Decl : Node_Id;
3231 Parent_N : Node_Id;
3232 D : Entity_Id;
3233 List_Constr : constant List_Id := New_List;
3234
3235 begin
3236 -- Expand_Record_Extension is called directly from the semantics, so
3237 -- we must check to see whether expansion is active before proceeding
3238
3239 if not Expander_Active then
3240 return;
3241 end if;
3242
3243 -- This may be a derivation of an untagged private type whose full
3244 -- view is tagged, in which case the Derived_Type_Definition has no
3245 -- extension part. Build an empty one now.
3246
3247 if No (Rec_Ext_Part) then
3248 Rec_Ext_Part :=
3249 Make_Record_Definition (Loc,
3250 End_Label => Empty,
3251 Component_List => Empty,
3252 Null_Present => True);
3253
3254 Set_Record_Extension_Part (Def, Rec_Ext_Part);
3255 Mark_Rewrite_Insertion (Rec_Ext_Part);
3256 end if;
3257
3258 Comp_List := Component_List (Rec_Ext_Part);
3259
3260 Parent_N := Make_Defining_Identifier (Loc, Name_uParent);
3261
3262 -- If the derived type inherits its discriminants the type of the
3263 -- _parent field must be constrained by the inherited discriminants
3264
3265 if Has_Discriminants (T)
3266 and then Nkind (Indic) /= N_Subtype_Indication
3267 and then not Is_Constrained (Entity (Indic))
3268 then
3269 D := First_Discriminant (T);
3270 while Present (D) loop
3271 Append_To (List_Constr, New_Occurrence_Of (D, Loc));
3272 Next_Discriminant (D);
3273 end loop;
3274
3275 Par_Subtype :=
3276 Process_Subtype (
3277 Make_Subtype_Indication (Loc,
3278 Subtype_Mark => New_Reference_To (Entity (Indic), Loc),
3279 Constraint =>
3280 Make_Index_Or_Discriminant_Constraint (Loc,
3281 Constraints => List_Constr)),
3282 Def);
3283
3284 -- Otherwise the original subtype_indication is just what is needed
3285
3286 else
3287 Par_Subtype := Process_Subtype (New_Copy_Tree (Indic), Def);
3288 end if;
3289
3290 Set_Parent_Subtype (T, Par_Subtype);
3291
3292 Comp_Decl :=
3293 Make_Component_Declaration (Loc,
3294 Defining_Identifier => Parent_N,
3295 Component_Definition =>
3296 Make_Component_Definition (Loc,
3297 Aliased_Present => False,
3298 Subtype_Indication => New_Reference_To (Par_Subtype, Loc)));
3299
3300 if Null_Present (Rec_Ext_Part) then
3301 Set_Component_List (Rec_Ext_Part,
3302 Make_Component_List (Loc,
3303 Component_Items => New_List (Comp_Decl),
3304 Variant_Part => Empty,
3305 Null_Present => False));
3306 Set_Null_Present (Rec_Ext_Part, False);
3307
3308 elsif Null_Present (Comp_List)
3309 or else Is_Empty_List (Component_Items (Comp_List))
3310 then
3311 Set_Component_Items (Comp_List, New_List (Comp_Decl));
3312 Set_Null_Present (Comp_List, False);
3313
3314 else
3315 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
3316 end if;
3317
3318 Analyze (Comp_Decl);
3319 end Expand_Record_Extension;
3320
3321 ------------------------------------
3322 -- Expand_N_Full_Type_Declaration --
3323 ------------------------------------
3324
3325 procedure Expand_N_Full_Type_Declaration (N : Node_Id) is
3326 Def_Id : constant Entity_Id := Defining_Identifier (N);
3327 B_Id : constant Entity_Id := Base_Type (Def_Id);
3328 Par_Id : Entity_Id;
3329 FN : Node_Id;
3330
3331 begin
3332 if Is_Access_Type (Def_Id) then
3333
3334 -- Anonymous access types are created for the components of the
3335 -- record parameter for an entry declaration. No master is created
3336 -- for such a type.
3337
3338 if Has_Task (Designated_Type (Def_Id))
3339 and then Comes_From_Source (N)
3340 then
3341 Build_Master_Entity (Def_Id);
3342 Build_Master_Renaming (Parent (Def_Id), Def_Id);
3343
3344 -- Create a class-wide master because a Master_Id must be generated
3345 -- for access-to-limited-class-wide types, whose root may be extended
3346 -- with task components.
3347
3348 elsif Is_Class_Wide_Type (Designated_Type (Def_Id))
3349 and then Is_Limited_Type (Designated_Type (Def_Id))
3350 and then Tasking_Allowed
3351
3352 -- Don't create a class-wide master for types whose convention is
3353 -- Java since these types cannot embed Ada tasks anyway. Note that
3354 -- the following test cannot catch the following case:
3355 --
3356 -- package java.lang.Object is
3357 -- type Typ is tagged limited private;
3358 -- type Ref is access all Typ'Class;
3359 -- private
3360 -- type Typ is tagged limited ...;
3361 -- pragma Convention (Typ, Java)
3362 -- end;
3363 --
3364 -- Because the convention appears after we have done the
3365 -- processing for type Ref.
3366
3367 and then Convention (Designated_Type (Def_Id)) /= Convention_Java
3368 then
3369 Build_Class_Wide_Master (Def_Id);
3370
3371 elsif Ekind (Def_Id) = E_Access_Protected_Subprogram_Type then
3372 Expand_Access_Protected_Subprogram_Type (N);
3373 end if;
3374
3375 elsif Has_Task (Def_Id) then
3376 Expand_Previous_Access_Type (Def_Id);
3377 end if;
3378
3379 Par_Id := Etype (B_Id);
3380
3381 -- The parent type is private then we need to inherit
3382 -- any TSS operations from the full view.
3383
3384 if Ekind (Par_Id) in Private_Kind
3385 and then Present (Full_View (Par_Id))
3386 then
3387 Par_Id := Base_Type (Full_View (Par_Id));
3388 end if;
3389
3390 if Nkind (Type_Definition (Original_Node (N)))
3391 = N_Derived_Type_Definition
3392 and then not Is_Tagged_Type (Def_Id)
3393 and then Present (Freeze_Node (Par_Id))
3394 and then Present (TSS_Elist (Freeze_Node (Par_Id)))
3395 then
3396 Ensure_Freeze_Node (B_Id);
3397 FN := Freeze_Node (B_Id);
3398
3399 if No (TSS_Elist (FN)) then
3400 Set_TSS_Elist (FN, New_Elmt_List);
3401 end if;
3402
3403 declare
3404 T_E : constant Elist_Id := TSS_Elist (FN);
3405 Elmt : Elmt_Id;
3406
3407 begin
3408 Elmt := First_Elmt (TSS_Elist (Freeze_Node (Par_Id)));
3409
3410 while Present (Elmt) loop
3411 if Chars (Node (Elmt)) /= Name_uInit then
3412 Append_Elmt (Node (Elmt), T_E);
3413 end if;
3414
3415 Next_Elmt (Elmt);
3416 end loop;
3417
3418 -- If the derived type itself is private with a full view, then
3419 -- associate the full view with the inherited TSS_Elist as well.
3420
3421 if Ekind (B_Id) in Private_Kind
3422 and then Present (Full_View (B_Id))
3423 then
3424 Ensure_Freeze_Node (Base_Type (Full_View (B_Id)));
3425 Set_TSS_Elist
3426 (Freeze_Node (Base_Type (Full_View (B_Id))), TSS_Elist (FN));
3427 end if;
3428 end;
3429 end if;
3430 end Expand_N_Full_Type_Declaration;
3431
3432 ---------------------------------
3433 -- Expand_N_Object_Declaration --
3434 ---------------------------------
3435
3436 -- First we do special processing for objects of a tagged type where this
3437 -- is the point at which the type is frozen. The creation of the dispatch
3438 -- table and the initialization procedure have to be deferred to this
3439 -- point, since we reference previously declared primitive subprograms.
3440
3441 -- For all types, we call an initialization procedure if there is one
3442
3443 procedure Expand_N_Object_Declaration (N : Node_Id) is
3444 Def_Id : constant Entity_Id := Defining_Identifier (N);
3445 Typ : constant Entity_Id := Etype (Def_Id);
3446 Loc : constant Source_Ptr := Sloc (N);
3447 Expr : constant Node_Id := Expression (N);
3448 New_Ref : Node_Id;
3449 Id_Ref : Node_Id;
3450 Expr_Q : Node_Id;
3451
3452 begin
3453 -- Don't do anything for deferred constants. All proper actions will
3454 -- be expanded during the full declaration.
3455
3456 if No (Expr) and Constant_Present (N) then
3457 return;
3458 end if;
3459
3460 -- Make shared memory routines for shared passive variable
3461
3462 if Is_Shared_Passive (Def_Id) then
3463 Make_Shared_Var_Procs (N);
3464 end if;
3465
3466 -- If tasks being declared, make sure we have an activation chain
3467 -- defined for the tasks (has no effect if we already have one), and
3468 -- also that a Master variable is established and that the appropriate
3469 -- enclosing construct is established as a task master.
3470
3471 if Has_Task (Typ) then
3472 Build_Activation_Chain_Entity (N);
3473 Build_Master_Entity (Def_Id);
3474 end if;
3475
3476 -- Default initialization required, and no expression present
3477
3478 if No (Expr) then
3479
3480 -- Expand Initialize call for controlled objects. One may wonder why
3481 -- the Initialize Call is not done in the regular Init procedure
3482 -- attached to the record type. That's because the init procedure is
3483 -- recursively called on each component, including _Parent, thus the
3484 -- Init call for a controlled object would generate not only one
3485 -- Initialize call as it is required but one for each ancestor of
3486 -- its type. This processing is suppressed if No_Initialization set.
3487
3488 if not Controlled_Type (Typ)
3489 or else No_Initialization (N)
3490 then
3491 null;
3492
3493 elsif not Abort_Allowed
3494 or else not Comes_From_Source (N)
3495 then
3496 Insert_Actions_After (N,
3497 Make_Init_Call (
3498 Ref => New_Occurrence_Of (Def_Id, Loc),
3499 Typ => Base_Type (Typ),
3500 Flist_Ref => Find_Final_List (Def_Id),
3501 With_Attach => Make_Integer_Literal (Loc, 1)));
3502
3503 -- Abort allowed
3504
3505 else
3506 -- We need to protect the initialize call
3507
3508 -- begin
3509 -- Defer_Abort.all;
3510 -- Initialize (...);
3511 -- at end
3512 -- Undefer_Abort.all;
3513 -- end;
3514
3515 -- ??? this won't protect the initialize call for controlled
3516 -- components which are part of the init proc, so this block
3517 -- should probably also contain the call to _init_proc but this
3518 -- requires some code reorganization...
3519
3520 declare
3521 L : constant List_Id :=
3522 Make_Init_Call (
3523 Ref => New_Occurrence_Of (Def_Id, Loc),
3524 Typ => Base_Type (Typ),
3525 Flist_Ref => Find_Final_List (Def_Id),
3526 With_Attach => Make_Integer_Literal (Loc, 1));
3527
3528 Blk : constant Node_Id :=
3529 Make_Block_Statement (Loc,
3530 Handled_Statement_Sequence =>
3531 Make_Handled_Sequence_Of_Statements (Loc, L));
3532
3533 begin
3534 Prepend_To (L, Build_Runtime_Call (Loc, RE_Abort_Defer));
3535 Set_At_End_Proc (Handled_Statement_Sequence (Blk),
3536 New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc));
3537 Insert_Actions_After (N, New_List (Blk));
3538 Expand_At_End_Handler
3539 (Handled_Statement_Sequence (Blk), Entity (Identifier (Blk)));
3540 end;
3541 end if;
3542
3543 -- Call type initialization procedure if there is one. We build the
3544 -- call and put it immediately after the object declaration, so that
3545 -- it will be expanded in the usual manner. Note that this will
3546 -- result in proper handling of defaulted discriminants. The call
3547 -- to the Init_Proc is suppressed if No_Initialization is set.
3548
3549 if Has_Non_Null_Base_Init_Proc (Typ)
3550 and then not No_Initialization (N)
3551 then
3552 -- The call to the initialization procedure does NOT freeze
3553 -- the object being initialized. This is because the call is
3554 -- not a source level call. This works fine, because the only
3555 -- possible statements depending on freeze status that can
3556 -- appear after the _Init call are rep clauses which can
3557 -- safely appear after actual references to the object.
3558
3559 Id_Ref := New_Reference_To (Def_Id, Loc);
3560 Set_Must_Not_Freeze (Id_Ref);
3561 Set_Assignment_OK (Id_Ref);
3562
3563 Insert_Actions_After (N,
3564 Build_Initialization_Call (Loc, Id_Ref, Typ));
3565
3566 -- If simple initialization is required, then set an appropriate
3567 -- simple initialization expression in place. This special
3568 -- initialization is required even though No_Init_Flag is present.
3569
3570 -- An internally generated temporary needs no initialization because
3571 -- it will be assigned subsequently. In particular, there is no
3572 -- point in applying Initialize_Scalars to such a temporary.
3573
3574 elsif Needs_Simple_Initialization (Typ)
3575 and then not Is_Internal (Def_Id)
3576 then
3577 Set_No_Initialization (N, False);
3578 Set_Expression (N, Get_Simple_Init_Val (Typ, Loc, Esize (Def_Id)));
3579 Analyze_And_Resolve (Expression (N), Typ);
3580 end if;
3581
3582 -- Generate attribute for Persistent_BSS if needed
3583
3584 declare
3585 Prag : Node_Id;
3586 begin
3587 if Persistent_BSS_Mode
3588 and then Comes_From_Source (N)
3589 and then Is_Potentially_Persistent_Type (Typ)
3590 and then Is_Library_Level_Entity (Def_Id)
3591 then
3592 Prag :=
3593 Make_Linker_Section_Pragma
3594 (Def_Id, Sloc (N), ".persistent.bss");
3595 Insert_After (N, Prag);
3596 Analyze (Prag);
3597 end if;
3598 end;
3599
3600 -- Explicit initialization present
3601
3602 else
3603 -- Obtain actual expression from qualified expression
3604
3605 if Nkind (Expr) = N_Qualified_Expression then
3606 Expr_Q := Expression (Expr);
3607 else
3608 Expr_Q := Expr;
3609 end if;
3610
3611 -- When we have the appropriate type of aggregate in the
3612 -- expression (it has been determined during analysis of the
3613 -- aggregate by setting the delay flag), let's perform in
3614 -- place assignment and thus avoid creating a temporary.
3615
3616 if Is_Delayed_Aggregate (Expr_Q) then
3617 Convert_Aggr_In_Object_Decl (N);
3618
3619 else
3620 -- In most cases, we must check that the initial value meets
3621 -- any constraint imposed by the declared type. However, there
3622 -- is one very important exception to this rule. If the entity
3623 -- has an unconstrained nominal subtype, then it acquired its
3624 -- constraints from the expression in the first place, and not
3625 -- only does this mean that the constraint check is not needed,
3626 -- but an attempt to perform the constraint check can
3627 -- cause order of elaboration problems.
3628
3629 if not Is_Constr_Subt_For_U_Nominal (Typ) then
3630
3631 -- If this is an allocator for an aggregate that has been
3632 -- allocated in place, delay checks until assignments are
3633 -- made, because the discriminants are not initialized.
3634
3635 if Nkind (Expr) = N_Allocator
3636 and then No_Initialization (Expr)
3637 then
3638 null;
3639 else
3640 Apply_Constraint_Check (Expr, Typ);
3641 end if;
3642 end if;
3643
3644 -- If the type is controlled we attach the object to the final
3645 -- list and adjust the target after the copy. This
3646
3647 if Controlled_Type (Typ) then
3648 declare
3649 Flist : Node_Id;
3650 F : Entity_Id;
3651
3652 begin
3653 -- Attach the result to a dummy final list which will never
3654 -- be finalized if Delay_Finalize_Attachis set. It is
3655 -- important to attach to a dummy final list rather than
3656 -- not attaching at all in order to reset the pointers
3657 -- coming from the initial value. Equivalent code exists
3658 -- in the sec-stack case in Exp_Ch4.Expand_N_Allocator.
3659
3660 if Delay_Finalize_Attach (N) then
3661 F :=
3662 Make_Defining_Identifier (Loc, New_Internal_Name ('F'));
3663 Insert_Action (N,
3664 Make_Object_Declaration (Loc,
3665 Defining_Identifier => F,
3666 Object_Definition =>
3667 New_Reference_To (RTE (RE_Finalizable_Ptr), Loc)));
3668
3669 Flist := New_Reference_To (F, Loc);
3670
3671 else
3672 Flist := Find_Final_List (Def_Id);
3673 end if;
3674
3675 Insert_Actions_After (N,
3676 Make_Adjust_Call (
3677 Ref => New_Reference_To (Def_Id, Loc),
3678 Typ => Base_Type (Typ),
3679 Flist_Ref => Flist,
3680 With_Attach => Make_Integer_Literal (Loc, 1)));
3681 end;
3682 end if;
3683
3684 -- For tagged types, when an init value is given, the tag has to
3685 -- be re-initialized separately in order to avoid the propagation
3686 -- of a wrong tag coming from a view conversion unless the type
3687 -- is class wide (in this case the tag comes from the init
3688 -- value). Suppress the tag assignment when Java_VM because JVM
3689 -- tags are represented implicitly in objects. Ditto for types
3690 -- that are CPP_CLASS, and for initializations that are
3691 -- aggregates, because they have to have the right tag.
3692
3693 if Is_Tagged_Type (Typ)
3694 and then not Is_Class_Wide_Type (Typ)
3695 and then not Is_CPP_Class (Typ)
3696 and then not Java_VM
3697 and then Nkind (Expr) /= N_Aggregate
3698 then
3699 -- The re-assignment of the tag has to be done even if
3700 -- the object is a constant
3701
3702 New_Ref :=
3703 Make_Selected_Component (Loc,
3704 Prefix => New_Reference_To (Def_Id, Loc),
3705 Selector_Name =>
3706 New_Reference_To (First_Tag_Component (Typ), Loc));
3707
3708 Set_Assignment_OK (New_Ref);
3709
3710 Insert_After (N,
3711 Make_Assignment_Statement (Loc,
3712 Name => New_Ref,
3713 Expression =>
3714 Unchecked_Convert_To (RTE (RE_Tag),
3715 New_Reference_To
3716 (Node
3717 (First_Elmt
3718 (Access_Disp_Table (Base_Type (Typ)))),
3719 Loc))));
3720
3721 -- For discrete types, set the Is_Known_Valid flag if the
3722 -- initializing value is known to be valid.
3723
3724 elsif Is_Discrete_Type (Typ)
3725 and then Expr_Known_Valid (Expr)
3726 then
3727 Set_Is_Known_Valid (Def_Id);
3728
3729 elsif Is_Access_Type (Typ) then
3730
3731 -- For access types set the Is_Known_Non_Null flag if the
3732 -- initializing value is known to be non-null. We can also set
3733 -- Can_Never_Be_Null if this is a constant.
3734
3735 if Known_Non_Null (Expr) then
3736 Set_Is_Known_Non_Null (Def_Id);
3737
3738 if Constant_Present (N) then
3739 Set_Can_Never_Be_Null (Def_Id);
3740 end if;
3741 end if;
3742 end if;
3743
3744 -- If validity checking on copies, validate initial expression
3745
3746 if Validity_Checks_On
3747 and then Validity_Check_Copies
3748 then
3749 Ensure_Valid (Expr);
3750 Set_Is_Known_Valid (Def_Id);
3751 end if;
3752 end if;
3753
3754 -- Cases where the back end cannot handle the initialization
3755 -- directly. In such cases, we expand an assignment that will
3756 -- be appropriately handled by Expand_N_Assignment_Statement.
3757
3758 -- The exclusion of the unconstrained case is wrong, but for
3759 -- now it is too much trouble ???
3760
3761 if (Is_Possibly_Unaligned_Slice (Expr)
3762 or else (Is_Possibly_Unaligned_Object (Expr)
3763 and then not Represented_As_Scalar (Etype (Expr))))
3764
3765 -- The exclusion of the unconstrained case is wrong, but for
3766 -- now it is too much trouble ???
3767
3768 and then not (Is_Array_Type (Etype (Expr))
3769 and then not Is_Constrained (Etype (Expr)))
3770 then
3771 declare
3772 Stat : constant Node_Id :=
3773 Make_Assignment_Statement (Loc,
3774 Name => New_Reference_To (Def_Id, Loc),
3775 Expression => Relocate_Node (Expr));
3776 begin
3777 Set_Expression (N, Empty);
3778 Set_No_Initialization (N);
3779 Set_Assignment_OK (Name (Stat));
3780 Set_No_Ctrl_Actions (Stat);
3781 Insert_After (N, Stat);
3782 Analyze (Stat);
3783 end;
3784 end if;
3785 end if;
3786
3787 -- For array type, check for size too large
3788 -- We really need this for record types too???
3789
3790 if Is_Array_Type (Typ) then
3791 Apply_Array_Size_Check (N, Typ);
3792 end if;
3793
3794 exception
3795 when RE_Not_Available =>
3796 return;
3797 end Expand_N_Object_Declaration;
3798
3799 ---------------------------------
3800 -- Expand_N_Subtype_Indication --
3801 ---------------------------------
3802
3803 -- Add a check on the range of the subtype. The static case is partially
3804 -- duplicated by Process_Range_Expr_In_Decl in Sem_Ch3, but we still need
3805 -- to check here for the static case in order to avoid generating
3806 -- extraneous expanded code.
3807
3808 procedure Expand_N_Subtype_Indication (N : Node_Id) is
3809 Ran : constant Node_Id := Range_Expression (Constraint (N));
3810 Typ : constant Entity_Id := Entity (Subtype_Mark (N));
3811
3812 begin
3813 if Nkind (Parent (N)) = N_Constrained_Array_Definition or else
3814 Nkind (Parent (N)) = N_Slice
3815 then
3816 Resolve (Ran, Typ);
3817 Apply_Range_Check (Ran, Typ);
3818 end if;
3819 end Expand_N_Subtype_Indication;
3820
3821 ---------------------------
3822 -- Expand_N_Variant_Part --
3823 ---------------------------
3824
3825 -- If the last variant does not contain the Others choice, replace it with
3826 -- an N_Others_Choice node since Gigi always wants an Others. Note that we
3827 -- do not bother to call Analyze on the modified variant part, since it's
3828 -- only effect would be to compute the contents of the
3829 -- Others_Discrete_Choices node laboriously, and of course we already know
3830 -- the list of choices that corresponds to the others choice (it's the
3831 -- list we are replacing!)
3832
3833 procedure Expand_N_Variant_Part (N : Node_Id) is
3834 Last_Var : constant Node_Id := Last_Non_Pragma (Variants (N));
3835 Others_Node : Node_Id;
3836 begin
3837 if Nkind (First (Discrete_Choices (Last_Var))) /= N_Others_Choice then
3838 Others_Node := Make_Others_Choice (Sloc (Last_Var));
3839 Set_Others_Discrete_Choices
3840 (Others_Node, Discrete_Choices (Last_Var));
3841 Set_Discrete_Choices (Last_Var, New_List (Others_Node));
3842 end if;
3843 end Expand_N_Variant_Part;
3844
3845 ---------------------------------
3846 -- Expand_Previous_Access_Type --
3847 ---------------------------------
3848
3849 procedure Expand_Previous_Access_Type (Def_Id : Entity_Id) is
3850 T : Entity_Id := First_Entity (Current_Scope);
3851
3852 begin
3853 -- Find all access types declared in the current scope, whose
3854 -- designated type is Def_Id.
3855
3856 while Present (T) loop
3857 if Is_Access_Type (T)
3858 and then Designated_Type (T) = Def_Id
3859 then
3860 Build_Master_Entity (Def_Id);
3861 Build_Master_Renaming (Parent (Def_Id), T);
3862 end if;
3863
3864 Next_Entity (T);
3865 end loop;
3866 end Expand_Previous_Access_Type;
3867
3868 ------------------------------
3869 -- Expand_Record_Controller --
3870 ------------------------------
3871
3872 procedure Expand_Record_Controller (T : Entity_Id) is
3873 Def : Node_Id := Type_Definition (Parent (T));
3874 Comp_List : Node_Id;
3875 Comp_Decl : Node_Id;
3876 Loc : Source_Ptr;
3877 First_Comp : Node_Id;
3878 Controller_Type : Entity_Id;
3879 Ent : Entity_Id;
3880
3881 begin
3882 if Nkind (Def) = N_Derived_Type_Definition then
3883 Def := Record_Extension_Part (Def);
3884 end if;
3885
3886 if Null_Present (Def) then
3887 Set_Component_List (Def,
3888 Make_Component_List (Sloc (Def),
3889 Component_Items => Empty_List,
3890 Variant_Part => Empty,
3891 Null_Present => True));
3892 end if;
3893
3894 Comp_List := Component_List (Def);
3895
3896 if Null_Present (Comp_List)
3897 or else Is_Empty_List (Component_Items (Comp_List))
3898 then
3899 Loc := Sloc (Comp_List);
3900 else
3901 Loc := Sloc (First (Component_Items (Comp_List)));
3902 end if;
3903
3904 if Is_Return_By_Reference_Type (T) then
3905 Controller_Type := RTE (RE_Limited_Record_Controller);
3906 else
3907 Controller_Type := RTE (RE_Record_Controller);
3908 end if;
3909
3910 Ent := Make_Defining_Identifier (Loc, Name_uController);
3911
3912 Comp_Decl :=
3913 Make_Component_Declaration (Loc,
3914 Defining_Identifier => Ent,
3915 Component_Definition =>
3916 Make_Component_Definition (Loc,
3917 Aliased_Present => False,
3918 Subtype_Indication => New_Reference_To (Controller_Type, Loc)));
3919
3920 if Null_Present (Comp_List)
3921 or else Is_Empty_List (Component_Items (Comp_List))
3922 then
3923 Set_Component_Items (Comp_List, New_List (Comp_Decl));
3924 Set_Null_Present (Comp_List, False);
3925
3926 else
3927 -- The controller cannot be placed before the _Parent field since
3928 -- gigi lays out field in order and _parent must be first to
3929 -- preserve the polymorphism of tagged types.
3930
3931 First_Comp := First (Component_Items (Comp_List));
3932
3933 if Chars (Defining_Identifier (First_Comp)) /= Name_uParent
3934 and then Chars (Defining_Identifier (First_Comp)) /= Name_uTag
3935 then
3936 Insert_Before (First_Comp, Comp_Decl);
3937 else
3938 Insert_After (First_Comp, Comp_Decl);
3939 end if;
3940 end if;
3941
3942 New_Scope (T);
3943 Analyze (Comp_Decl);
3944 Set_Ekind (Ent, E_Component);
3945 Init_Component_Location (Ent);
3946
3947 -- Move the _controller entity ahead in the list of internal entities
3948 -- of the enclosing record so that it is selected instead of a
3949 -- potentially inherited one.
3950
3951 declare
3952 E : constant Entity_Id := Last_Entity (T);
3953 Comp : Entity_Id;
3954
3955 begin
3956 pragma Assert (Chars (E) = Name_uController);
3957
3958 Set_Next_Entity (E, First_Entity (T));
3959 Set_First_Entity (T, E);
3960
3961 Comp := Next_Entity (E);
3962 while Next_Entity (Comp) /= E loop
3963 Next_Entity (Comp);
3964 end loop;
3965
3966 Set_Next_Entity (Comp, Empty);
3967 Set_Last_Entity (T, Comp);
3968 end;
3969
3970 End_Scope;
3971
3972 exception
3973 when RE_Not_Available =>
3974 return;
3975 end Expand_Record_Controller;
3976
3977 ------------------------
3978 -- Expand_Tagged_Root --
3979 ------------------------
3980
3981 procedure Expand_Tagged_Root (T : Entity_Id) is
3982 Def : constant Node_Id := Type_Definition (Parent (T));
3983 Comp_List : Node_Id;
3984 Comp_Decl : Node_Id;
3985 Sloc_N : Source_Ptr;
3986
3987 begin
3988 if Null_Present (Def) then
3989 Set_Component_List (Def,
3990 Make_Component_List (Sloc (Def),
3991 Component_Items => Empty_List,
3992 Variant_Part => Empty,
3993 Null_Present => True));
3994 end if;
3995
3996 Comp_List := Component_List (Def);
3997
3998 if Null_Present (Comp_List)
3999 or else Is_Empty_List (Component_Items (Comp_List))
4000 then
4001 Sloc_N := Sloc (Comp_List);
4002 else
4003 Sloc_N := Sloc (First (Component_Items (Comp_List)));
4004 end if;
4005
4006 Comp_Decl :=
4007 Make_Component_Declaration (Sloc_N,
4008 Defining_Identifier => First_Tag_Component (T),
4009 Component_Definition =>
4010 Make_Component_Definition (Sloc_N,
4011 Aliased_Present => False,
4012 Subtype_Indication => New_Reference_To (RTE (RE_Tag), Sloc_N)));
4013
4014 if Null_Present (Comp_List)
4015 or else Is_Empty_List (Component_Items (Comp_List))
4016 then
4017 Set_Component_Items (Comp_List, New_List (Comp_Decl));
4018 Set_Null_Present (Comp_List, False);
4019
4020 else
4021 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
4022 end if;
4023
4024 -- We don't Analyze the whole expansion because the tag component has
4025 -- already been analyzed previously. Here we just insure that the tree
4026 -- is coherent with the semantic decoration
4027
4028 Find_Type (Subtype_Indication (Component_Definition (Comp_Decl)));
4029
4030 exception
4031 when RE_Not_Available =>
4032 return;
4033 end Expand_Tagged_Root;
4034
4035 -----------------------
4036 -- Freeze_Array_Type --
4037 -----------------------
4038
4039 procedure Freeze_Array_Type (N : Node_Id) is
4040 Typ : constant Entity_Id := Entity (N);
4041 Base : constant Entity_Id := Base_Type (Typ);
4042
4043 begin
4044 if not Is_Bit_Packed_Array (Typ) then
4045
4046 -- If the component contains tasks, so does the array type. This may
4047 -- not be indicated in the array type because the component may have
4048 -- been a private type at the point of definition. Same if component
4049 -- type is controlled.
4050
4051 Set_Has_Task (Base, Has_Task (Component_Type (Typ)));
4052 Set_Has_Controlled_Component (Base,
4053 Has_Controlled_Component (Component_Type (Typ))
4054 or else Is_Controlled (Component_Type (Typ)));
4055
4056 if No (Init_Proc (Base)) then
4057
4058 -- If this is an anonymous array created for a declaration with
4059 -- an initial value, its init_proc will never be called. The
4060 -- initial value itself may have been expanded into assign-
4061 -- ments, in which case the object declaration is carries the
4062 -- No_Initialization flag.
4063
4064 if Is_Itype (Base)
4065 and then Nkind (Associated_Node_For_Itype (Base)) =
4066 N_Object_Declaration
4067 and then (Present (Expression (Associated_Node_For_Itype (Base)))
4068 or else
4069 No_Initialization (Associated_Node_For_Itype (Base)))
4070 then
4071 null;
4072
4073 -- We do not need an init proc for string or wide [wide] string,
4074 -- since the only time these need initialization in normalize or
4075 -- initialize scalars mode, and these types are treated specially
4076 -- and do not need initialization procedures.
4077
4078 elsif Root_Type (Base) = Standard_String
4079 or else Root_Type (Base) = Standard_Wide_String
4080 or else Root_Type (Base) = Standard_Wide_Wide_String
4081 then
4082 null;
4083
4084 -- Otherwise we have to build an init proc for the subtype
4085
4086 else
4087 Build_Array_Init_Proc (Base, N);
4088 end if;
4089 end if;
4090
4091 if Typ = Base and then Has_Controlled_Component (Base) then
4092 Build_Controlling_Procs (Base);
4093
4094 if not Is_Limited_Type (Component_Type (Typ))
4095 and then Number_Dimensions (Typ) = 1
4096 then
4097 Build_Slice_Assignment (Typ);
4098 end if;
4099 end if;
4100
4101 -- For packed case, there is a default initialization, except if the
4102 -- component type is itself a packed structure with an initialization
4103 -- procedure.
4104
4105 elsif Present (Init_Proc (Component_Type (Base)))
4106 and then No (Base_Init_Proc (Base))
4107 then
4108 Build_Array_Init_Proc (Base, N);
4109 end if;
4110 end Freeze_Array_Type;
4111
4112 -----------------------------
4113 -- Freeze_Enumeration_Type --
4114 -----------------------------
4115
4116 procedure Freeze_Enumeration_Type (N : Node_Id) is
4117 Typ : constant Entity_Id := Entity (N);
4118 Loc : constant Source_Ptr := Sloc (Typ);
4119 Ent : Entity_Id;
4120 Lst : List_Id;
4121 Num : Nat;
4122 Arr : Entity_Id;
4123 Fent : Entity_Id;
4124 Ityp : Entity_Id;
4125 Is_Contiguous : Boolean;
4126 Pos_Expr : Node_Id;
4127 Last_Repval : Uint;
4128
4129 Func : Entity_Id;
4130 pragma Warnings (Off, Func);
4131
4132 begin
4133 -- Various optimization are possible if the given representation is
4134 -- contiguous.
4135
4136 Is_Contiguous := True;
4137 Ent := First_Literal (Typ);
4138 Last_Repval := Enumeration_Rep (Ent);
4139 Next_Literal (Ent);
4140
4141 while Present (Ent) loop
4142 if Enumeration_Rep (Ent) - Last_Repval /= 1 then
4143 Is_Contiguous := False;
4144 exit;
4145 else
4146 Last_Repval := Enumeration_Rep (Ent);
4147 end if;
4148
4149 Next_Literal (Ent);
4150 end loop;
4151
4152 if Is_Contiguous then
4153 Set_Has_Contiguous_Rep (Typ);
4154 Ent := First_Literal (Typ);
4155 Num := 1;
4156 Lst := New_List (New_Reference_To (Ent, Sloc (Ent)));
4157
4158 else
4159 -- Build list of literal references
4160
4161 Lst := New_List;
4162 Num := 0;
4163
4164 Ent := First_Literal (Typ);
4165 while Present (Ent) loop
4166 Append_To (Lst, New_Reference_To (Ent, Sloc (Ent)));
4167 Num := Num + 1;
4168 Next_Literal (Ent);
4169 end loop;
4170 end if;
4171
4172 -- Now build an array declaration
4173
4174 -- typA : array (Natural range 0 .. num - 1) of ctype :=
4175 -- (v, v, v, v, v, ....)
4176
4177 -- where ctype is the corresponding integer type. If the representation
4178 -- is contiguous, we only keep the first literal, which provides the
4179 -- offset for Pos_To_Rep computations.
4180
4181 Arr :=
4182 Make_Defining_Identifier (Loc,
4183 Chars => New_External_Name (Chars (Typ), 'A'));
4184
4185 Append_Freeze_Action (Typ,
4186 Make_Object_Declaration (Loc,
4187 Defining_Identifier => Arr,
4188 Constant_Present => True,
4189
4190 Object_Definition =>
4191 Make_Constrained_Array_Definition (Loc,
4192 Discrete_Subtype_Definitions => New_List (
4193 Make_Subtype_Indication (Loc,
4194 Subtype_Mark => New_Reference_To (Standard_Natural, Loc),
4195 Constraint =>
4196 Make_Range_Constraint (Loc,
4197 Range_Expression =>
4198 Make_Range (Loc,
4199 Low_Bound =>
4200 Make_Integer_Literal (Loc, 0),
4201 High_Bound =>
4202 Make_Integer_Literal (Loc, Num - 1))))),
4203
4204 Component_Definition =>
4205 Make_Component_Definition (Loc,
4206 Aliased_Present => False,
4207 Subtype_Indication => New_Reference_To (Typ, Loc))),
4208
4209 Expression =>
4210 Make_Aggregate (Loc,
4211 Expressions => Lst)));
4212
4213 Set_Enum_Pos_To_Rep (Typ, Arr);
4214
4215 -- Now we build the function that converts representation values to
4216 -- position values. This function has the form:
4217
4218 -- function _Rep_To_Pos (A : etype; F : Boolean) return Integer is
4219 -- begin
4220 -- case ityp!(A) is
4221 -- when enum-lit'Enum_Rep => return posval;
4222 -- when enum-lit'Enum_Rep => return posval;
4223 -- ...
4224 -- when others =>
4225 -- [raise Constraint_Error when F "invalid data"]
4226 -- return -1;
4227 -- end case;
4228 -- end;
4229
4230 -- Note: the F parameter determines whether the others case (no valid
4231 -- representation) raises Constraint_Error or returns a unique value
4232 -- of minus one. The latter case is used, e.g. in 'Valid code.
4233
4234 -- Note: the reason we use Enum_Rep values in the case here is to avoid
4235 -- the code generator making inappropriate assumptions about the range
4236 -- of the values in the case where the value is invalid. ityp is a
4237 -- signed or unsigned integer type of appropriate width.
4238
4239 -- Note: if exceptions are not supported, then we suppress the raise
4240 -- and return -1 unconditionally (this is an erroneous program in any
4241 -- case and there is no obligation to raise Constraint_Error here!) We
4242 -- also do this if pragma Restrictions (No_Exceptions) is active.
4243
4244 -- Representations are signed
4245
4246 if Enumeration_Rep (First_Literal (Typ)) < 0 then
4247
4248 -- The underlying type is signed. Reset the Is_Unsigned_Type
4249 -- explicitly, because it might have been inherited from
4250 -- parent type.
4251
4252 Set_Is_Unsigned_Type (Typ, False);
4253
4254 if Esize (Typ) <= Standard_Integer_Size then
4255 Ityp := Standard_Integer;
4256 else
4257 Ityp := Universal_Integer;
4258 end if;
4259
4260 -- Representations are unsigned
4261
4262 else
4263 if Esize (Typ) <= Standard_Integer_Size then
4264 Ityp := RTE (RE_Unsigned);
4265 else
4266 Ityp := RTE (RE_Long_Long_Unsigned);
4267 end if;
4268 end if;
4269
4270 -- The body of the function is a case statement. First collect case
4271 -- alternatives, or optimize the contiguous case.
4272
4273 Lst := New_List;
4274
4275 -- If representation is contiguous, Pos is computed by subtracting
4276 -- the representation of the first literal.
4277
4278 if Is_Contiguous then
4279 Ent := First_Literal (Typ);
4280
4281 if Enumeration_Rep (Ent) = Last_Repval then
4282
4283 -- Another special case: for a single literal, Pos is zero
4284
4285 Pos_Expr := Make_Integer_Literal (Loc, Uint_0);
4286
4287 else
4288 Pos_Expr :=
4289 Convert_To (Standard_Integer,
4290 Make_Op_Subtract (Loc,
4291 Left_Opnd =>
4292 Unchecked_Convert_To (Ityp,
4293 Make_Identifier (Loc, Name_uA)),
4294 Right_Opnd =>
4295 Make_Integer_Literal (Loc,
4296 Intval =>
4297 Enumeration_Rep (First_Literal (Typ)))));
4298 end if;
4299
4300 Append_To (Lst,
4301 Make_Case_Statement_Alternative (Loc,
4302 Discrete_Choices => New_List (
4303 Make_Range (Sloc (Enumeration_Rep_Expr (Ent)),
4304 Low_Bound =>
4305 Make_Integer_Literal (Loc,
4306 Intval => Enumeration_Rep (Ent)),
4307 High_Bound =>
4308 Make_Integer_Literal (Loc, Intval => Last_Repval))),
4309
4310 Statements => New_List (
4311 Make_Return_Statement (Loc,
4312 Expression => Pos_Expr))));
4313
4314 else
4315 Ent := First_Literal (Typ);
4316
4317 while Present (Ent) loop
4318 Append_To (Lst,
4319 Make_Case_Statement_Alternative (Loc,
4320 Discrete_Choices => New_List (
4321 Make_Integer_Literal (Sloc (Enumeration_Rep_Expr (Ent)),
4322 Intval => Enumeration_Rep (Ent))),
4323
4324 Statements => New_List (
4325 Make_Return_Statement (Loc,
4326 Expression =>
4327 Make_Integer_Literal (Loc,
4328 Intval => Enumeration_Pos (Ent))))));
4329
4330 Next_Literal (Ent);
4331 end loop;
4332 end if;
4333
4334 -- In normal mode, add the others clause with the test
4335
4336 if not Restriction_Active (No_Exception_Handlers) then
4337 Append_To (Lst,
4338 Make_Case_Statement_Alternative (Loc,
4339 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
4340 Statements => New_List (
4341 Make_Raise_Constraint_Error (Loc,
4342 Condition => Make_Identifier (Loc, Name_uF),
4343 Reason => CE_Invalid_Data),
4344 Make_Return_Statement (Loc,
4345 Expression =>
4346 Make_Integer_Literal (Loc, -1)))));
4347
4348 -- If Restriction (No_Exceptions_Handlers) is active then we always
4349 -- return -1 (since we cannot usefully raise Constraint_Error in
4350 -- this case). See description above for further details.
4351
4352 else
4353 Append_To (Lst,
4354 Make_Case_Statement_Alternative (Loc,
4355 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
4356 Statements => New_List (
4357 Make_Return_Statement (Loc,
4358 Expression =>
4359 Make_Integer_Literal (Loc, -1)))));
4360 end if;
4361
4362 -- Now we can build the function body
4363
4364 Fent :=
4365 Make_Defining_Identifier (Loc, Make_TSS_Name (Typ, TSS_Rep_To_Pos));
4366
4367 Func :=
4368 Make_Subprogram_Body (Loc,
4369 Specification =>
4370 Make_Function_Specification (Loc,
4371 Defining_Unit_Name => Fent,
4372 Parameter_Specifications => New_List (
4373 Make_Parameter_Specification (Loc,
4374 Defining_Identifier =>
4375 Make_Defining_Identifier (Loc, Name_uA),
4376 Parameter_Type => New_Reference_To (Typ, Loc)),
4377 Make_Parameter_Specification (Loc,
4378 Defining_Identifier =>
4379 Make_Defining_Identifier (Loc, Name_uF),
4380 Parameter_Type => New_Reference_To (Standard_Boolean, Loc))),
4381
4382 Result_Definition => New_Reference_To (Standard_Integer, Loc)),
4383
4384 Declarations => Empty_List,
4385
4386 Handled_Statement_Sequence =>
4387 Make_Handled_Sequence_Of_Statements (Loc,
4388 Statements => New_List (
4389 Make_Case_Statement (Loc,
4390 Expression =>
4391 Unchecked_Convert_To (Ityp,
4392 Make_Identifier (Loc, Name_uA)),
4393 Alternatives => Lst))));
4394
4395 Set_TSS (Typ, Fent);
4396 Set_Is_Pure (Fent);
4397
4398 if not Debug_Generated_Code then
4399 Set_Debug_Info_Off (Fent);
4400 end if;
4401
4402 exception
4403 when RE_Not_Available =>
4404 return;
4405 end Freeze_Enumeration_Type;
4406
4407 ------------------------
4408 -- Freeze_Record_Type --
4409 ------------------------
4410
4411 procedure Freeze_Record_Type (N : Node_Id) is
4412 Comp : Entity_Id;
4413 Def_Id : constant Node_Id := Entity (N);
4414 Predef_List : List_Id;
4415 Type_Decl : constant Node_Id := Parent (Def_Id);
4416
4417 Renamed_Eq : Node_Id := Empty;
4418 -- Could use some comments ???
4419
4420 begin
4421 -- Build discriminant checking functions if not a derived type (for
4422 -- derived types that are not tagged types, we always use the
4423 -- discriminant checking functions of the parent type). However, for
4424 -- untagged types the derivation may have taken place before the
4425 -- parent was frozen, so we copy explicitly the discriminant checking
4426 -- functions from the parent into the components of the derived type.
4427
4428 if not Is_Derived_Type (Def_Id)
4429 or else Has_New_Non_Standard_Rep (Def_Id)
4430 or else Is_Tagged_Type (Def_Id)
4431 then
4432 Build_Discr_Checking_Funcs (Type_Decl);
4433
4434 elsif Is_Derived_Type (Def_Id)
4435 and then not Is_Tagged_Type (Def_Id)
4436
4437 -- If we have a derived Unchecked_Union, we do not inherit the
4438 -- discriminant checking functions from the parent type since the
4439 -- discriminants are non existent.
4440
4441 and then not Is_Unchecked_Union (Def_Id)
4442 and then Has_Discriminants (Def_Id)
4443 then
4444 declare
4445 Old_Comp : Entity_Id;
4446
4447 begin
4448 Old_Comp :=
4449 First_Component (Base_Type (Underlying_Type (Etype (Def_Id))));
4450 Comp := First_Component (Def_Id);
4451 while Present (Comp) loop
4452 if Ekind (Comp) = E_Component
4453 and then Chars (Comp) = Chars (Old_Comp)
4454 then
4455 Set_Discriminant_Checking_Func (Comp,
4456 Discriminant_Checking_Func (Old_Comp));
4457 end if;
4458
4459 Next_Component (Old_Comp);
4460 Next_Component (Comp);
4461 end loop;
4462 end;
4463 end if;
4464
4465 if Is_Derived_Type (Def_Id)
4466 and then Is_Limited_Type (Def_Id)
4467 and then Is_Tagged_Type (Def_Id)
4468 then
4469 Check_Stream_Attributes (Def_Id);
4470 end if;
4471
4472 -- Update task and controlled component flags, because some of the
4473 -- component types may have been private at the point of the record
4474 -- declaration.
4475
4476 Comp := First_Component (Def_Id);
4477
4478 while Present (Comp) loop
4479 if Has_Task (Etype (Comp)) then
4480 Set_Has_Task (Def_Id);
4481
4482 elsif Has_Controlled_Component (Etype (Comp))
4483 or else (Chars (Comp) /= Name_uParent
4484 and then Is_Controlled (Etype (Comp)))
4485 then
4486 Set_Has_Controlled_Component (Def_Id);
4487 end if;
4488
4489 Next_Component (Comp);
4490 end loop;
4491
4492 -- Creation of the Dispatch Table. Note that a Dispatch Table is
4493 -- created for regular tagged types as well as for Ada types deriving
4494 -- from a C++ Class, but not for tagged types directly corresponding to
4495 -- the C++ classes. In the later case we assume that the Vtable is
4496 -- created in the C++ side and we just use it.
4497
4498 if Is_Tagged_Type (Def_Id) then
4499
4500 if Is_CPP_Class (Def_Id) then
4501 Set_All_DT_Position (Def_Id);
4502 Set_Default_Constructor (Def_Id);
4503
4504 else
4505 -- Usually inherited primitives are not delayed but the first Ada
4506 -- extension of a CPP_Class is an exception since the address of
4507 -- the inherited subprogram has to be inserted in the new Ada
4508 -- Dispatch Table and this is a freezing action (usually the
4509 -- inherited primitive address is inserted in the DT by
4510 -- Inherit_DT)
4511
4512 -- Similarly, if this is an inherited operation whose parent is
4513 -- not frozen yet, it is not in the DT of the parent, and we
4514 -- generate an explicit freeze node for the inherited operation,
4515 -- so that it is properly inserted in the DT of the current type.
4516
4517 declare
4518 Elmt : Elmt_Id := First_Elmt (Primitive_Operations (Def_Id));
4519 Subp : Entity_Id;
4520
4521 begin
4522 while Present (Elmt) loop
4523 Subp := Node (Elmt);
4524
4525 if Present (Alias (Subp)) then
4526 if Is_CPP_Class (Etype (Def_Id)) then
4527 Set_Has_Delayed_Freeze (Subp);
4528
4529 elsif Has_Delayed_Freeze (Alias (Subp))
4530 and then not Is_Frozen (Alias (Subp))
4531 then
4532 Set_Is_Frozen (Subp, False);
4533 Set_Has_Delayed_Freeze (Subp);
4534 end if;
4535 end if;
4536
4537 Next_Elmt (Elmt);
4538 end loop;
4539 end;
4540
4541 if Underlying_Type (Etype (Def_Id)) = Def_Id then
4542 Expand_Tagged_Root (Def_Id);
4543 end if;
4544
4545 -- Unfreeze momentarily the type to add the predefined primitives
4546 -- operations. The reason we unfreeze is so that these predefined
4547 -- operations will indeed end up as primitive operations (which
4548 -- must be before the freeze point).
4549
4550 Set_Is_Frozen (Def_Id, False);
4551 Make_Predefined_Primitive_Specs
4552 (Def_Id, Predef_List, Renamed_Eq);
4553 Insert_List_Before_And_Analyze (N, Predef_List);
4554
4555 Set_Is_Frozen (Def_Id, True);
4556 Set_All_DT_Position (Def_Id);
4557
4558 -- Add the controlled component before the freezing actions
4559 -- referenced in those actions.
4560
4561 if Has_New_Controlled_Component (Def_Id) then
4562 Expand_Record_Controller (Def_Id);
4563 end if;
4564
4565 -- Suppress creation of a dispatch table when Java_VM because the
4566 -- dispatching mechanism is handled internally by the JVM.
4567
4568 if not Java_VM then
4569
4570 -- Ada 2005 (AI-251): Build the secondary dispatch tables
4571
4572 declare
4573 ADT : Elist_Id := Access_Disp_Table (Def_Id);
4574
4575 procedure Add_Secondary_Tables (Typ : Entity_Id);
4576 -- Internal subprogram, recursively climb to the ancestors
4577
4578 --------------------------
4579 -- Add_Secondary_Tables --
4580 --------------------------
4581
4582 procedure Add_Secondary_Tables (Typ : Entity_Id) is
4583 E : Entity_Id;
4584 Iface : Elmt_Id;
4585 Result : List_Id;
4586 Suffix_Index : Int;
4587
4588 begin
4589 -- Climb to the ancestor (if any) handling private types
4590
4591 if Present (Full_View (Etype (Typ))) then
4592 if Full_View (Etype (Typ)) /= Typ then
4593 Add_Secondary_Tables (Full_View (Etype (Typ)));
4594 end if;
4595
4596 elsif Etype (Typ) /= Typ then
4597 Add_Secondary_Tables (Etype (Typ));
4598 end if;
4599
4600 if Present (Abstract_Interfaces (Typ))
4601 and then
4602 not Is_Empty_Elmt_List (Abstract_Interfaces (Typ))
4603 then
4604 Iface := First_Elmt (Abstract_Interfaces (Typ));
4605 Suffix_Index := 0;
4606
4607 E := First_Entity (Typ);
4608 while Present (E) loop
4609 if Is_Tag (E) and then Chars (E) /= Name_uTag then
4610 Make_Secondary_DT
4611 (Typ => Def_Id,
4612 Ancestor_Typ => Typ,
4613 Suffix_Index => Suffix_Index,
4614 Iface => Node (Iface),
4615 AI_Tag => E,
4616 Acc_Disp_Tables => ADT,
4617 Result => Result);
4618
4619 Append_Freeze_Actions (Def_Id, Result);
4620 Suffix_Index := Suffix_Index + 1;
4621 Next_Elmt (Iface);
4622 end if;
4623
4624 Next_Entity (E);
4625 end loop;
4626 end if;
4627 end Add_Secondary_Tables;
4628
4629 -- Start of processing to build secondary dispatch tables
4630
4631 begin
4632 -- Handle private types
4633
4634 if Present (Full_View (Def_Id)) then
4635 Add_Secondary_Tables (Full_View (Def_Id));
4636 else
4637 Add_Secondary_Tables (Def_Id);
4638 end if;
4639
4640 Set_Access_Disp_Table (Def_Id, ADT);
4641 Append_Freeze_Actions (Def_Id, Make_DT (Def_Id));
4642 end;
4643 end if;
4644
4645 -- Make sure that the primitives Initialize, Adjust and Finalize
4646 -- are Frozen before other TSS subprograms. We don't want them
4647 -- Frozen inside.
4648
4649 if Is_Controlled (Def_Id) then
4650 if not Is_Limited_Type (Def_Id) then
4651 Append_Freeze_Actions (Def_Id,
4652 Freeze_Entity
4653 (Find_Prim_Op (Def_Id, Name_Adjust), Sloc (Def_Id)));
4654 end if;
4655
4656 Append_Freeze_Actions (Def_Id,
4657 Freeze_Entity
4658 (Find_Prim_Op (Def_Id, Name_Initialize), Sloc (Def_Id)));
4659
4660 Append_Freeze_Actions (Def_Id,
4661 Freeze_Entity
4662 (Find_Prim_Op (Def_Id, Name_Finalize), Sloc (Def_Id)));
4663 end if;
4664
4665 -- Freeze rest of primitive operations
4666
4667 Append_Freeze_Actions
4668 (Def_Id, Predefined_Primitive_Freeze (Def_Id));
4669 Append_Freeze_Actions
4670 (Def_Id, Init_Predefined_Interface_Primitives (Def_Id));
4671 end if;
4672
4673 -- In the non-tagged case, an equality function is provided only for
4674 -- variant records (that are not unchecked unions).
4675
4676 elsif Has_Discriminants (Def_Id)
4677 and then not Is_Limited_Type (Def_Id)
4678 then
4679 declare
4680 Comps : constant Node_Id :=
4681 Component_List (Type_Definition (Type_Decl));
4682
4683 begin
4684 if Present (Comps)
4685 and then Present (Variant_Part (Comps))
4686 then
4687 Build_Variant_Record_Equality (Def_Id);
4688 end if;
4689 end;
4690 end if;
4691
4692 -- Before building the record initialization procedure, if we are
4693 -- dealing with a concurrent record value type, then we must go through
4694 -- the discriminants, exchanging discriminals between the concurrent
4695 -- type and the concurrent record value type. See the section "Handling
4696 -- of Discriminants" in the Einfo spec for details.
4697
4698 if Is_Concurrent_Record_Type (Def_Id)
4699 and then Has_Discriminants (Def_Id)
4700 then
4701 declare
4702 Ctyp : constant Entity_Id :=
4703 Corresponding_Concurrent_Type (Def_Id);
4704 Conc_Discr : Entity_Id;
4705 Rec_Discr : Entity_Id;
4706 Temp : Entity_Id;
4707
4708 begin
4709 Conc_Discr := First_Discriminant (Ctyp);
4710 Rec_Discr := First_Discriminant (Def_Id);
4711
4712 while Present (Conc_Discr) loop
4713 Temp := Discriminal (Conc_Discr);
4714 Set_Discriminal (Conc_Discr, Discriminal (Rec_Discr));
4715 Set_Discriminal (Rec_Discr, Temp);
4716
4717 Set_Discriminal_Link (Discriminal (Conc_Discr), Conc_Discr);
4718 Set_Discriminal_Link (Discriminal (Rec_Discr), Rec_Discr);
4719
4720 Next_Discriminant (Conc_Discr);
4721 Next_Discriminant (Rec_Discr);
4722 end loop;
4723 end;
4724 end if;
4725
4726 if Has_Controlled_Component (Def_Id) then
4727 if No (Controller_Component (Def_Id)) then
4728 Expand_Record_Controller (Def_Id);
4729 end if;
4730
4731 Build_Controlling_Procs (Def_Id);
4732 end if;
4733
4734 Adjust_Discriminants (Def_Id);
4735 Build_Record_Init_Proc (Type_Decl, Def_Id);
4736
4737 -- For tagged type, build bodies of primitive operations. Note that we
4738 -- do this after building the record initialization experiment, since
4739 -- the primitive operations may need the initialization routine
4740
4741 if Is_Tagged_Type (Def_Id) then
4742 Predef_List := Predefined_Primitive_Bodies (Def_Id, Renamed_Eq);
4743 Append_Freeze_Actions (Def_Id, Predef_List);
4744
4745 -- Populate the two auxiliary tables used for dispatching
4746 -- asynchronous, conditional and timed selects for tagged
4747 -- types that implement a limited interface.
4748
4749 if Ada_Version >= Ada_05
4750 and then not Is_Interface (Def_Id)
4751 and then not Is_Abstract (Def_Id)
4752 and then not Is_Controlled (Def_Id)
4753 and then
4754 Implements_Interface
4755 (Typ => Def_Id,
4756 Kind => Any_Limited_Interface,
4757 Check_Parent => True)
4758 then
4759 Append_Freeze_Actions (Def_Id,
4760 Make_Select_Specific_Data_Table (Def_Id));
4761 end if;
4762 end if;
4763 end Freeze_Record_Type;
4764
4765 ------------------------------
4766 -- Freeze_Stream_Operations --
4767 ------------------------------
4768
4769 procedure Freeze_Stream_Operations (N : Node_Id; Typ : Entity_Id) is
4770 Names : constant array (1 .. 4) of TSS_Name_Type :=
4771 (TSS_Stream_Input,
4772 TSS_Stream_Output,
4773 TSS_Stream_Read,
4774 TSS_Stream_Write);
4775 Stream_Op : Entity_Id;
4776
4777 begin
4778 -- Primitive operations of tagged types are frozen when the dispatch
4779 -- table is constructed.
4780
4781 if not Comes_From_Source (Typ)
4782 or else Is_Tagged_Type (Typ)
4783 then
4784 return;
4785 end if;
4786
4787 for J in Names'Range loop
4788 Stream_Op := TSS (Typ, Names (J));
4789
4790 if Present (Stream_Op)
4791 and then Is_Subprogram (Stream_Op)
4792 and then Nkind (Unit_Declaration_Node (Stream_Op)) =
4793 N_Subprogram_Declaration
4794 and then not Is_Frozen (Stream_Op)
4795 then
4796 Append_Freeze_Actions
4797 (Typ, Freeze_Entity (Stream_Op, Sloc (N)));
4798 end if;
4799 end loop;
4800 end Freeze_Stream_Operations;
4801
4802 -----------------
4803 -- Freeze_Type --
4804 -----------------
4805
4806 -- Full type declarations are expanded at the point at which the type is
4807 -- frozen. The formal N is the Freeze_Node for the type. Any statements or
4808 -- declarations generated by the freezing (e.g. the procedure generated
4809 -- for initialization) are chained in the Actions field list of the freeze
4810 -- node using Append_Freeze_Actions.
4811
4812 function Freeze_Type (N : Node_Id) return Boolean is
4813 Def_Id : constant Entity_Id := Entity (N);
4814 RACW_Seen : Boolean := False;
4815 Result : Boolean := False;
4816
4817 begin
4818 -- Process associated access types needing special processing
4819
4820 if Present (Access_Types_To_Process (N)) then
4821 declare
4822 E : Elmt_Id := First_Elmt (Access_Types_To_Process (N));
4823 begin
4824 while Present (E) loop
4825
4826 if Is_Remote_Access_To_Class_Wide_Type (Node (E)) then
4827 RACW_Seen := True;
4828 end if;
4829
4830 E := Next_Elmt (E);
4831 end loop;
4832 end;
4833
4834 if RACW_Seen then
4835
4836 -- If there are RACWs designating this type, make stubs now
4837
4838 Remote_Types_Tagged_Full_View_Encountered (Def_Id);
4839 end if;
4840 end if;
4841
4842 -- Freeze processing for record types
4843
4844 if Is_Record_Type (Def_Id) then
4845 if Ekind (Def_Id) = E_Record_Type then
4846 Freeze_Record_Type (N);
4847
4848 -- The subtype may have been declared before the type was frozen. If
4849 -- the type has controlled components it is necessary to create the
4850 -- entity for the controller explicitly because it did not exist at
4851 -- the point of the subtype declaration. Only the entity is needed,
4852 -- the back-end will obtain the layout from the type. This is only
4853 -- necessary if this is constrained subtype whose component list is
4854 -- not shared with the base type.
4855
4856 elsif Ekind (Def_Id) = E_Record_Subtype
4857 and then Has_Discriminants (Def_Id)
4858 and then Last_Entity (Def_Id) /= Last_Entity (Base_Type (Def_Id))
4859 and then Present (Controller_Component (Def_Id))
4860 then
4861 declare
4862 Old_C : constant Entity_Id := Controller_Component (Def_Id);
4863 New_C : Entity_Id;
4864
4865 begin
4866 if Scope (Old_C) = Base_Type (Def_Id) then
4867
4868 -- The entity is the one in the parent. Create new one
4869
4870 New_C := New_Copy (Old_C);
4871 Set_Parent (New_C, Parent (Old_C));
4872 New_Scope (Def_Id);
4873 Enter_Name (New_C);
4874 End_Scope;
4875 end if;
4876 end;
4877
4878 if Is_Itype (Def_Id)
4879 and then Is_Record_Type (Underlying_Type (Scope (Def_Id)))
4880 then
4881 -- The freeze node is only used to introduce the controller,
4882 -- the back-end has no use for it for a discriminated
4883 -- component.
4884
4885 Set_Freeze_Node (Def_Id, Empty);
4886 Set_Has_Delayed_Freeze (Def_Id, False);
4887 Result := True;
4888 end if;
4889
4890 -- Similar process if the controller of the subtype is not present
4891 -- but the parent has it. This can happen with constrained
4892 -- record components where the subtype is an itype.
4893
4894 elsif Ekind (Def_Id) = E_Record_Subtype
4895 and then Is_Itype (Def_Id)
4896 and then No (Controller_Component (Def_Id))
4897 and then Present (Controller_Component (Etype (Def_Id)))
4898 then
4899 declare
4900 Old_C : constant Entity_Id :=
4901 Controller_Component (Etype (Def_Id));
4902 New_C : constant Entity_Id := New_Copy (Old_C);
4903
4904 begin
4905 Set_Next_Entity (New_C, First_Entity (Def_Id));
4906 Set_First_Entity (Def_Id, New_C);
4907
4908 -- The freeze node is only used to introduce the controller,
4909 -- the back-end has no use for it for a discriminated
4910 -- component.
4911
4912 Set_Freeze_Node (Def_Id, Empty);
4913 Set_Has_Delayed_Freeze (Def_Id, False);
4914 Result := True;
4915 end;
4916 end if;
4917
4918 -- Freeze processing for array types
4919
4920 elsif Is_Array_Type (Def_Id) then
4921 Freeze_Array_Type (N);
4922
4923 -- Freeze processing for access types
4924
4925 -- For pool-specific access types, find out the pool object used for
4926 -- this type, needs actual expansion of it in some cases. Here are the
4927 -- different cases :
4928
4929 -- 1. Rep Clause "for Def_Id'Storage_Size use 0;"
4930 -- ---> don't use any storage pool
4931
4932 -- 2. Rep Clause : for Def_Id'Storage_Size use Expr.
4933 -- Expand:
4934 -- Def_Id__Pool : Stack_Bounded_Pool (Expr, DT'Size, DT'Alignment);
4935
4936 -- 3. Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
4937 -- ---> Storage Pool is the specified one
4938
4939 -- See GNAT Pool packages in the Run-Time for more details
4940
4941 elsif Ekind (Def_Id) = E_Access_Type
4942 or else Ekind (Def_Id) = E_General_Access_Type
4943 then
4944 declare
4945 Loc : constant Source_Ptr := Sloc (N);
4946 Desig_Type : constant Entity_Id := Designated_Type (Def_Id);
4947 Pool_Object : Entity_Id;
4948 Siz_Exp : Node_Id;
4949
4950 Freeze_Action_Typ : Entity_Id;
4951
4952 begin
4953 if Has_Storage_Size_Clause (Def_Id) then
4954 Siz_Exp := Expression (Parent (Storage_Size_Variable (Def_Id)));
4955 else
4956 Siz_Exp := Empty;
4957 end if;
4958
4959 -- Case 1
4960
4961 -- Rep Clause "for Def_Id'Storage_Size use 0;"
4962 -- ---> don't use any storage pool
4963
4964 if Has_Storage_Size_Clause (Def_Id)
4965 and then Compile_Time_Known_Value (Siz_Exp)
4966 and then Expr_Value (Siz_Exp) = 0
4967 then
4968 null;
4969
4970 -- Case 2
4971
4972 -- Rep Clause : for Def_Id'Storage_Size use Expr.
4973 -- ---> Expand:
4974 -- Def_Id__Pool : Stack_Bounded_Pool
4975 -- (Expr, DT'Size, DT'Alignment);
4976
4977 elsif Has_Storage_Size_Clause (Def_Id) then
4978 declare
4979 DT_Size : Node_Id;
4980 DT_Align : Node_Id;
4981
4982 begin
4983 -- For unconstrained composite types we give a size of zero
4984 -- so that the pool knows that it needs a special algorithm
4985 -- for variable size object allocation.
4986
4987 if Is_Composite_Type (Desig_Type)
4988 and then not Is_Constrained (Desig_Type)
4989 then
4990 DT_Size :=
4991 Make_Integer_Literal (Loc, 0);
4992
4993 DT_Align :=
4994 Make_Integer_Literal (Loc, Maximum_Alignment);
4995
4996 else
4997 DT_Size :=
4998 Make_Attribute_Reference (Loc,
4999 Prefix => New_Reference_To (Desig_Type, Loc),
5000 Attribute_Name => Name_Max_Size_In_Storage_Elements);
5001
5002 DT_Align :=
5003 Make_Attribute_Reference (Loc,
5004 Prefix => New_Reference_To (Desig_Type, Loc),
5005 Attribute_Name => Name_Alignment);
5006 end if;
5007
5008 Pool_Object :=
5009 Make_Defining_Identifier (Loc,
5010 Chars => New_External_Name (Chars (Def_Id), 'P'));
5011
5012 -- We put the code associated with the pools in the entity
5013 -- that has the later freeze node, usually the acces type
5014 -- but it can also be the designated_type; because the pool
5015 -- code requires both those types to be frozen
5016
5017 if Is_Frozen (Desig_Type)
5018 and then (not Present (Freeze_Node (Desig_Type))
5019 or else Analyzed (Freeze_Node (Desig_Type)))
5020 then
5021 Freeze_Action_Typ := Def_Id;
5022
5023 -- A Taft amendment type cannot get the freeze actions
5024 -- since the full view is not there.
5025
5026 elsif Is_Incomplete_Or_Private_Type (Desig_Type)
5027 and then No (Full_View (Desig_Type))
5028 then
5029 Freeze_Action_Typ := Def_Id;
5030
5031 else
5032 Freeze_Action_Typ := Desig_Type;
5033 end if;
5034
5035 Append_Freeze_Action (Freeze_Action_Typ,
5036 Make_Object_Declaration (Loc,
5037 Defining_Identifier => Pool_Object,
5038 Object_Definition =>
5039 Make_Subtype_Indication (Loc,
5040 Subtype_Mark =>
5041 New_Reference_To
5042 (RTE (RE_Stack_Bounded_Pool), Loc),
5043
5044 Constraint =>
5045 Make_Index_Or_Discriminant_Constraint (Loc,
5046 Constraints => New_List (
5047
5048 -- First discriminant is the Pool Size
5049
5050 New_Reference_To (
5051 Storage_Size_Variable (Def_Id), Loc),
5052
5053 -- Second discriminant is the element size
5054
5055 DT_Size,
5056
5057 -- Third discriminant is the alignment
5058
5059 DT_Align)))));
5060 end;
5061
5062 Set_Associated_Storage_Pool (Def_Id, Pool_Object);
5063
5064 -- Case 3
5065
5066 -- Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
5067 -- ---> Storage Pool is the specified one
5068
5069 elsif Present (Associated_Storage_Pool (Def_Id)) then
5070
5071 -- Nothing to do the associated storage pool has been attached
5072 -- when analyzing the rep. clause
5073
5074 null;
5075 end if;
5076
5077 -- For access-to-controlled types (including class-wide types and
5078 -- Taft-amendment types which potentially have controlled
5079 -- components), expand the list controller object that will store
5080 -- the dynamically allocated objects. Do not do this
5081 -- transformation for expander-generated access types, but do it
5082 -- for types that are the full view of types derived from other
5083 -- private types. Also suppress the list controller in the case
5084 -- of a designated type with convention Java, since this is used
5085 -- when binding to Java API specs, where there's no equivalent of
5086 -- a finalization list and we don't want to pull in the
5087 -- finalization support if not needed.
5088
5089 if not Comes_From_Source (Def_Id)
5090 and then not Has_Private_Declaration (Def_Id)
5091 then
5092 null;
5093
5094 elsif (Controlled_Type (Desig_Type)
5095 and then Convention (Desig_Type) /= Convention_Java)
5096 or else
5097 (Is_Incomplete_Or_Private_Type (Desig_Type)
5098 and then No (Full_View (Desig_Type))
5099
5100 -- An exception is made for types defined in the run-time
5101 -- because Ada.Tags.Tag itself is such a type and cannot
5102 -- afford this unnecessary overhead that would generates a
5103 -- loop in the expansion scheme...
5104
5105 and then not In_Runtime (Def_Id)
5106
5107 -- Another exception is if Restrictions (No_Finalization)
5108 -- is active, since then we know nothing is controlled.
5109
5110 and then not Restriction_Active (No_Finalization))
5111
5112 -- If the designated type is not frozen yet, its controlled
5113 -- status must be retrieved explicitly.
5114
5115 or else (Is_Array_Type (Desig_Type)
5116 and then not Is_Frozen (Desig_Type)
5117 and then Controlled_Type (Component_Type (Desig_Type)))
5118 then
5119 Set_Associated_Final_Chain (Def_Id,
5120 Make_Defining_Identifier (Loc,
5121 New_External_Name (Chars (Def_Id), 'L')));
5122
5123 Append_Freeze_Action (Def_Id,
5124 Make_Object_Declaration (Loc,
5125 Defining_Identifier => Associated_Final_Chain (Def_Id),
5126 Object_Definition =>
5127 New_Reference_To (RTE (RE_List_Controller), Loc)));
5128 end if;
5129 end;
5130
5131 -- Freeze processing for enumeration types
5132
5133 elsif Ekind (Def_Id) = E_Enumeration_Type then
5134
5135 -- We only have something to do if we have a non-standard
5136 -- representation (i.e. at least one literal whose pos value
5137 -- is not the same as its representation)
5138
5139 if Has_Non_Standard_Rep (Def_Id) then
5140 Freeze_Enumeration_Type (N);
5141 end if;
5142
5143 -- Private types that are completed by a derivation from a private
5144 -- type have an internally generated full view, that needs to be
5145 -- frozen. This must be done explicitly because the two views share
5146 -- the freeze node, and the underlying full view is not visible when
5147 -- the freeze node is analyzed.
5148
5149 elsif Is_Private_Type (Def_Id)
5150 and then Is_Derived_Type (Def_Id)
5151 and then Present (Full_View (Def_Id))
5152 and then Is_Itype (Full_View (Def_Id))
5153 and then Has_Private_Declaration (Full_View (Def_Id))
5154 and then Freeze_Node (Full_View (Def_Id)) = N
5155 then
5156 Set_Entity (N, Full_View (Def_Id));
5157 Result := Freeze_Type (N);
5158 Set_Entity (N, Def_Id);
5159
5160 -- All other types require no expander action. There are such cases
5161 -- (e.g. task types and protected types). In such cases, the freeze
5162 -- nodes are there for use by Gigi.
5163
5164 end if;
5165
5166 Freeze_Stream_Operations (N, Def_Id);
5167 return Result;
5168
5169 exception
5170 when RE_Not_Available =>
5171 return False;
5172 end Freeze_Type;
5173
5174 -------------------------
5175 -- Get_Simple_Init_Val --
5176 -------------------------
5177
5178 function Get_Simple_Init_Val
5179 (T : Entity_Id;
5180 Loc : Source_Ptr;
5181 Size : Uint := No_Uint) return Node_Id
5182 is
5183 Val : Node_Id;
5184 Result : Node_Id;
5185 Val_RE : RE_Id;
5186
5187 Size_To_Use : Uint;
5188 -- This is the size to be used for computation of the appropriate
5189 -- initial value for the Normalize_Scalars and Initialize_Scalars case.
5190
5191 Lo_Bound : Uint;
5192 Hi_Bound : Uint;
5193 -- These are the values computed by the procedure Check_Subtype_Bounds
5194
5195 procedure Check_Subtype_Bounds;
5196 -- This procedure examines the subtype T, and its ancestor subtypes and
5197 -- derived types to determine the best known information about the
5198 -- bounds of the subtype. After the call Lo_Bound is set either to
5199 -- No_Uint if no information can be determined, or to a value which
5200 -- represents a known low bound, i.e. a valid value of the subtype can
5201 -- not be less than this value. Hi_Bound is similarly set to a known
5202 -- high bound (valid value cannot be greater than this).
5203
5204 --------------------------
5205 -- Check_Subtype_Bounds --
5206 --------------------------
5207
5208 procedure Check_Subtype_Bounds is
5209 ST1 : Entity_Id;
5210 ST2 : Entity_Id;
5211 Lo : Node_Id;
5212 Hi : Node_Id;
5213 Loval : Uint;
5214 Hival : Uint;
5215
5216 begin
5217 Lo_Bound := No_Uint;
5218 Hi_Bound := No_Uint;
5219
5220 -- Loop to climb ancestor subtypes and derived types
5221
5222 ST1 := T;
5223 loop
5224 if not Is_Discrete_Type (ST1) then
5225 return;
5226 end if;
5227
5228 Lo := Type_Low_Bound (ST1);
5229 Hi := Type_High_Bound (ST1);
5230
5231 if Compile_Time_Known_Value (Lo) then
5232 Loval := Expr_Value (Lo);
5233
5234 if Lo_Bound = No_Uint or else Lo_Bound < Loval then
5235 Lo_Bound := Loval;
5236 end if;
5237 end if;
5238
5239 if Compile_Time_Known_Value (Hi) then
5240 Hival := Expr_Value (Hi);
5241
5242 if Hi_Bound = No_Uint or else Hi_Bound > Hival then
5243 Hi_Bound := Hival;
5244 end if;
5245 end if;
5246
5247 ST2 := Ancestor_Subtype (ST1);
5248
5249 if No (ST2) then
5250 ST2 := Etype (ST1);
5251 end if;
5252
5253 exit when ST1 = ST2;
5254 ST1 := ST2;
5255 end loop;
5256 end Check_Subtype_Bounds;
5257
5258 -- Start of processing for Get_Simple_Init_Val
5259
5260 begin
5261 -- For a private type, we should always have an underlying type
5262 -- (because this was already checked in Needs_Simple_Initialization).
5263 -- What we do is to get the value for the underlying type and then do
5264 -- an Unchecked_Convert to the private type.
5265
5266 if Is_Private_Type (T) then
5267 Val := Get_Simple_Init_Val (Underlying_Type (T), Loc, Size);
5268
5269 -- A special case, if the underlying value is null, then qualify it
5270 -- with the underlying type, so that the null is properly typed
5271 -- Similarly, if it is an aggregate it must be qualified, because an
5272 -- unchecked conversion does not provide a context for it.
5273
5274 if Nkind (Val) = N_Null
5275 or else Nkind (Val) = N_Aggregate
5276 then
5277 Val :=
5278 Make_Qualified_Expression (Loc,
5279 Subtype_Mark =>
5280 New_Occurrence_Of (Underlying_Type (T), Loc),
5281 Expression => Val);
5282 end if;
5283
5284 Result := Unchecked_Convert_To (T, Val);
5285
5286 -- Don't truncate result (important for Initialize/Normalize_Scalars)
5287
5288 if Nkind (Result) = N_Unchecked_Type_Conversion
5289 and then Is_Scalar_Type (Underlying_Type (T))
5290 then
5291 Set_No_Truncation (Result);
5292 end if;
5293
5294 return Result;
5295
5296 -- For scalars, we must have normalize/initialize scalars case
5297
5298 elsif Is_Scalar_Type (T) then
5299 pragma Assert (Init_Or_Norm_Scalars);
5300
5301 -- Compute size of object. If it is given by the caller, we can use
5302 -- it directly, otherwise we use Esize (T) as an estimate. As far as
5303 -- we know this covers all cases correctly.
5304
5305 if Size = No_Uint or else Size <= Uint_0 then
5306 Size_To_Use := UI_Max (Uint_1, Esize (T));
5307 else
5308 Size_To_Use := Size;
5309 end if;
5310
5311 -- Maximum size to use is 64 bits, since we will create values
5312 -- of type Unsigned_64 and the range must fit this type.
5313
5314 if Size_To_Use /= No_Uint and then Size_To_Use > Uint_64 then
5315 Size_To_Use := Uint_64;
5316 end if;
5317
5318 -- Check known bounds of subtype
5319
5320 Check_Subtype_Bounds;
5321
5322 -- Processing for Normalize_Scalars case
5323
5324 if Normalize_Scalars then
5325
5326 -- If zero is invalid, it is a convenient value to use that is
5327 -- for sure an appropriate invalid value in all situations.
5328
5329 if Lo_Bound /= No_Uint and then Lo_Bound > Uint_0 then
5330 Val := Make_Integer_Literal (Loc, 0);
5331
5332 -- Cases where all one bits is the appropriate invalid value
5333
5334 -- For modular types, all 1 bits is either invalid or valid. If
5335 -- it is valid, then there is nothing that can be done since there
5336 -- are no invalid values (we ruled out zero already).
5337
5338 -- For signed integer types that have no negative values, either
5339 -- there is room for negative values, or there is not. If there
5340 -- is, then all 1 bits may be interpretecd as minus one, which is
5341 -- certainly invalid. Alternatively it is treated as the largest
5342 -- positive value, in which case the observation for modular types
5343 -- still applies.
5344
5345 -- For float types, all 1-bits is a NaN (not a number), which is
5346 -- certainly an appropriately invalid value.
5347
5348 elsif Is_Unsigned_Type (T)
5349 or else Is_Floating_Point_Type (T)
5350 or else Is_Enumeration_Type (T)
5351 then
5352 Val := Make_Integer_Literal (Loc, 2 ** Size_To_Use - 1);
5353
5354 -- Resolve as Unsigned_64, because the largest number we
5355 -- can generate is out of range of universal integer.
5356
5357 Analyze_And_Resolve (Val, RTE (RE_Unsigned_64));
5358
5359 -- Case of signed types
5360
5361 else
5362 declare
5363 Signed_Size : constant Uint :=
5364 UI_Min (Uint_63, Size_To_Use - 1);
5365
5366 begin
5367 -- Normally we like to use the most negative number. The
5368 -- one exception is when this number is in the known
5369 -- subtype range and the largest positive number is not in
5370 -- the known subtype range.
5371
5372 -- For this exceptional case, use largest positive value
5373
5374 if Lo_Bound /= No_Uint and then Hi_Bound /= No_Uint
5375 and then Lo_Bound <= (-(2 ** Signed_Size))
5376 and then Hi_Bound < 2 ** Signed_Size
5377 then
5378 Val := Make_Integer_Literal (Loc, 2 ** Signed_Size - 1);
5379
5380 -- Normal case of largest negative value
5381
5382 else
5383 Val := Make_Integer_Literal (Loc, -(2 ** Signed_Size));
5384 end if;
5385 end;
5386 end if;
5387
5388 -- Here for Initialize_Scalars case
5389
5390 else
5391 -- For float types, use float values from System.Scalar_Values
5392
5393 if Is_Floating_Point_Type (T) then
5394 if Root_Type (T) = Standard_Short_Float then
5395 Val_RE := RE_IS_Isf;
5396 elsif Root_Type (T) = Standard_Float then
5397 Val_RE := RE_IS_Ifl;
5398 elsif Root_Type (T) = Standard_Long_Float then
5399 Val_RE := RE_IS_Ilf;
5400 else pragma Assert (Root_Type (T) = Standard_Long_Long_Float);
5401 Val_RE := RE_IS_Ill;
5402 end if;
5403
5404 -- If zero is invalid, use zero values from System.Scalar_Values
5405
5406 elsif Lo_Bound /= No_Uint and then Lo_Bound > Uint_0 then
5407 if Size_To_Use <= 8 then
5408 Val_RE := RE_IS_Iz1;
5409 elsif Size_To_Use <= 16 then
5410 Val_RE := RE_IS_Iz2;
5411 elsif Size_To_Use <= 32 then
5412 Val_RE := RE_IS_Iz4;
5413 else
5414 Val_RE := RE_IS_Iz8;
5415 end if;
5416
5417 -- For unsigned, use unsigned values from System.Scalar_Values
5418
5419 elsif Is_Unsigned_Type (T) then
5420 if Size_To_Use <= 8 then
5421 Val_RE := RE_IS_Iu1;
5422 elsif Size_To_Use <= 16 then
5423 Val_RE := RE_IS_Iu2;
5424 elsif Size_To_Use <= 32 then
5425 Val_RE := RE_IS_Iu4;
5426 else
5427 Val_RE := RE_IS_Iu8;
5428 end if;
5429
5430 -- For signed, use signed values from System.Scalar_Values
5431
5432 else
5433 if Size_To_Use <= 8 then
5434 Val_RE := RE_IS_Is1;
5435 elsif Size_To_Use <= 16 then
5436 Val_RE := RE_IS_Is2;
5437 elsif Size_To_Use <= 32 then
5438 Val_RE := RE_IS_Is4;
5439 else
5440 Val_RE := RE_IS_Is8;
5441 end if;
5442 end if;
5443
5444 Val := New_Occurrence_Of (RTE (Val_RE), Loc);
5445 end if;
5446
5447 -- The final expression is obtained by doing an unchecked conversion
5448 -- of this result to the base type of the required subtype. We use
5449 -- the base type to avoid the unchecked conversion from chopping
5450 -- bits, and then we set Kill_Range_Check to preserve the "bad"
5451 -- value.
5452
5453 Result := Unchecked_Convert_To (Base_Type (T), Val);
5454
5455 -- Ensure result is not truncated, since we want the "bad" bits
5456 -- and also kill range check on result.
5457
5458 if Nkind (Result) = N_Unchecked_Type_Conversion then
5459 Set_No_Truncation (Result);
5460 Set_Kill_Range_Check (Result, True);
5461 end if;
5462
5463 return Result;
5464
5465 -- String or Wide_[Wide]_String (must have Initialize_Scalars set)
5466
5467 elsif Root_Type (T) = Standard_String
5468 or else
5469 Root_Type (T) = Standard_Wide_String
5470 or else
5471 Root_Type (T) = Standard_Wide_Wide_String
5472 then
5473 pragma Assert (Init_Or_Norm_Scalars);
5474
5475 return
5476 Make_Aggregate (Loc,
5477 Component_Associations => New_List (
5478 Make_Component_Association (Loc,
5479 Choices => New_List (
5480 Make_Others_Choice (Loc)),
5481 Expression =>
5482 Get_Simple_Init_Val
5483 (Component_Type (T), Loc, Esize (Root_Type (T))))));
5484
5485 -- Access type is initialized to null
5486
5487 elsif Is_Access_Type (T) then
5488 return
5489 Make_Null (Loc);
5490
5491 -- No other possibilities should arise, since we should only be
5492 -- calling Get_Simple_Init_Val if Needs_Simple_Initialization
5493 -- returned True, indicating one of the above cases held.
5494
5495 else
5496 raise Program_Error;
5497 end if;
5498
5499 exception
5500 when RE_Not_Available =>
5501 return Empty;
5502 end Get_Simple_Init_Val;
5503
5504 ------------------------------
5505 -- Has_New_Non_Standard_Rep --
5506 ------------------------------
5507
5508 function Has_New_Non_Standard_Rep (T : Entity_Id) return Boolean is
5509 begin
5510 if not Is_Derived_Type (T) then
5511 return Has_Non_Standard_Rep (T)
5512 or else Has_Non_Standard_Rep (Root_Type (T));
5513
5514 -- If Has_Non_Standard_Rep is not set on the derived type, the
5515 -- representation is fully inherited.
5516
5517 elsif not Has_Non_Standard_Rep (T) then
5518 return False;
5519
5520 else
5521 return First_Rep_Item (T) /= First_Rep_Item (Root_Type (T));
5522
5523 -- May need a more precise check here: the First_Rep_Item may
5524 -- be a stream attribute, which does not affect the representation
5525 -- of the type ???
5526 end if;
5527 end Has_New_Non_Standard_Rep;
5528
5529 ----------------
5530 -- In_Runtime --
5531 ----------------
5532
5533 function In_Runtime (E : Entity_Id) return Boolean is
5534 S1 : Entity_Id := Scope (E);
5535
5536 begin
5537 while Scope (S1) /= Standard_Standard loop
5538 S1 := Scope (S1);
5539 end loop;
5540
5541 return Chars (S1) = Name_System or else Chars (S1) = Name_Ada;
5542 end In_Runtime;
5543
5544 ------------------
5545 -- Init_Formals --
5546 ------------------
5547
5548 function Init_Formals (Typ : Entity_Id) return List_Id is
5549 Loc : constant Source_Ptr := Sloc (Typ);
5550 Formals : List_Id;
5551
5552 begin
5553 -- First parameter is always _Init : in out typ. Note that we need
5554 -- this to be in/out because in the case of the task record value,
5555 -- there are default record fields (_Priority, _Size, -Task_Info)
5556 -- that may be referenced in the generated initialization routine.
5557
5558 Formals := New_List (
5559 Make_Parameter_Specification (Loc,
5560 Defining_Identifier =>
5561 Make_Defining_Identifier (Loc, Name_uInit),
5562 In_Present => True,
5563 Out_Present => True,
5564 Parameter_Type => New_Reference_To (Typ, Loc)));
5565
5566 -- For task record value, or type that contains tasks, add two more
5567 -- formals, _Master : Master_Id and _Chain : in out Activation_Chain
5568 -- We also add these parameters for the task record type case.
5569
5570 if Has_Task (Typ)
5571 or else (Is_Record_Type (Typ) and then Is_Task_Record_Type (Typ))
5572 then
5573 Append_To (Formals,
5574 Make_Parameter_Specification (Loc,
5575 Defining_Identifier =>
5576 Make_Defining_Identifier (Loc, Name_uMaster),
5577 Parameter_Type => New_Reference_To (RTE (RE_Master_Id), Loc)));
5578
5579 Append_To (Formals,
5580 Make_Parameter_Specification (Loc,
5581 Defining_Identifier =>
5582 Make_Defining_Identifier (Loc, Name_uChain),
5583 In_Present => True,
5584 Out_Present => True,
5585 Parameter_Type =>
5586 New_Reference_To (RTE (RE_Activation_Chain), Loc)));
5587
5588 Append_To (Formals,
5589 Make_Parameter_Specification (Loc,
5590 Defining_Identifier =>
5591 Make_Defining_Identifier (Loc, Name_uTask_Name),
5592 In_Present => True,
5593 Parameter_Type =>
5594 New_Reference_To (Standard_String, Loc)));
5595 end if;
5596
5597 return Formals;
5598
5599 exception
5600 when RE_Not_Available =>
5601 return Empty_List;
5602 end Init_Formals;
5603
5604 ------------------
5605 -- Make_Eq_Case --
5606 ------------------
5607
5608 -- <Make_Eq_if shared components>
5609 -- case X.D1 is
5610 -- when V1 => <Make_Eq_Case> on subcomponents
5611 -- ...
5612 -- when Vn => <Make_Eq_Case> on subcomponents
5613 -- end case;
5614
5615 function Make_Eq_Case
5616 (E : Entity_Id;
5617 CL : Node_Id;
5618 Discr : Entity_Id := Empty) return List_Id
5619 is
5620 Loc : constant Source_Ptr := Sloc (E);
5621 Result : constant List_Id := New_List;
5622 Variant : Node_Id;
5623 Alt_List : List_Id;
5624
5625 begin
5626 Append_To (Result, Make_Eq_If (E, Component_Items (CL)));
5627
5628 if No (Variant_Part (CL)) then
5629 return Result;
5630 end if;
5631
5632 Variant := First_Non_Pragma (Variants (Variant_Part (CL)));
5633
5634 if No (Variant) then
5635 return Result;
5636 end if;
5637
5638 Alt_List := New_List;
5639
5640 while Present (Variant) loop
5641 Append_To (Alt_List,
5642 Make_Case_Statement_Alternative (Loc,
5643 Discrete_Choices => New_Copy_List (Discrete_Choices (Variant)),
5644 Statements => Make_Eq_Case (E, Component_List (Variant))));
5645
5646 Next_Non_Pragma (Variant);
5647 end loop;
5648
5649 -- If we have an Unchecked_Union, use one of the parameters that
5650 -- captures the discriminants.
5651
5652 if Is_Unchecked_Union (E) then
5653 Append_To (Result,
5654 Make_Case_Statement (Loc,
5655 Expression => New_Reference_To (Discr, Loc),
5656 Alternatives => Alt_List));
5657
5658 else
5659 Append_To (Result,
5660 Make_Case_Statement (Loc,
5661 Expression =>
5662 Make_Selected_Component (Loc,
5663 Prefix => Make_Identifier (Loc, Name_X),
5664 Selector_Name => New_Copy (Name (Variant_Part (CL)))),
5665 Alternatives => Alt_List));
5666 end if;
5667
5668 return Result;
5669 end Make_Eq_Case;
5670
5671 ----------------
5672 -- Make_Eq_If --
5673 ----------------
5674
5675 -- Generates:
5676
5677 -- if
5678 -- X.C1 /= Y.C1
5679 -- or else
5680 -- X.C2 /= Y.C2
5681 -- ...
5682 -- then
5683 -- return False;
5684 -- end if;
5685
5686 -- or a null statement if the list L is empty
5687
5688 function Make_Eq_If
5689 (E : Entity_Id;
5690 L : List_Id) return Node_Id
5691 is
5692 Loc : constant Source_Ptr := Sloc (E);
5693 C : Node_Id;
5694 Field_Name : Name_Id;
5695 Cond : Node_Id;
5696
5697 begin
5698 if No (L) then
5699 return Make_Null_Statement (Loc);
5700
5701 else
5702 Cond := Empty;
5703
5704 C := First_Non_Pragma (L);
5705 while Present (C) loop
5706 Field_Name := Chars (Defining_Identifier (C));
5707
5708 -- The tags must not be compared they are not part of the value.
5709 -- Note also that in the following, we use Make_Identifier for
5710 -- the component names. Use of New_Reference_To to identify the
5711 -- components would be incorrect because the wrong entities for
5712 -- discriminants could be picked up in the private type case.
5713
5714 if Field_Name /= Name_uTag then
5715 Evolve_Or_Else (Cond,
5716 Make_Op_Ne (Loc,
5717 Left_Opnd =>
5718 Make_Selected_Component (Loc,
5719 Prefix => Make_Identifier (Loc, Name_X),
5720 Selector_Name =>
5721 Make_Identifier (Loc, Field_Name)),
5722
5723 Right_Opnd =>
5724 Make_Selected_Component (Loc,
5725 Prefix => Make_Identifier (Loc, Name_Y),
5726 Selector_Name =>
5727 Make_Identifier (Loc, Field_Name))));
5728 end if;
5729
5730 Next_Non_Pragma (C);
5731 end loop;
5732
5733 if No (Cond) then
5734 return Make_Null_Statement (Loc);
5735
5736 else
5737 return
5738 Make_Implicit_If_Statement (E,
5739 Condition => Cond,
5740 Then_Statements => New_List (
5741 Make_Return_Statement (Loc,
5742 Expression => New_Occurrence_Of (Standard_False, Loc))));
5743 end if;
5744 end if;
5745 end Make_Eq_If;
5746
5747 -------------------------------------
5748 -- Make_Predefined_Primitive_Specs --
5749 -------------------------------------
5750
5751 procedure Make_Predefined_Primitive_Specs
5752 (Tag_Typ : Entity_Id;
5753 Predef_List : out List_Id;
5754 Renamed_Eq : out Node_Id)
5755 is
5756 Loc : constant Source_Ptr := Sloc (Tag_Typ);
5757 Res : constant List_Id := New_List;
5758 Prim : Elmt_Id;
5759 Eq_Needed : Boolean;
5760 Eq_Spec : Node_Id;
5761 Eq_Name : Name_Id := Name_Op_Eq;
5762
5763 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean;
5764 -- Returns true if Prim is a renaming of an unresolved predefined
5765 -- equality operation.
5766
5767 -------------------------------
5768 -- Is_Predefined_Eq_Renaming --
5769 -------------------------------
5770
5771 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean is
5772 begin
5773 return Chars (Prim) /= Name_Op_Eq
5774 and then Present (Alias (Prim))
5775 and then Comes_From_Source (Prim)
5776 and then Is_Intrinsic_Subprogram (Alias (Prim))
5777 and then Chars (Alias (Prim)) = Name_Op_Eq;
5778 end Is_Predefined_Eq_Renaming;
5779
5780 -- Start of processing for Make_Predefined_Primitive_Specs
5781
5782 begin
5783 Renamed_Eq := Empty;
5784
5785 -- Spec of _Size
5786
5787 Append_To (Res, Predef_Spec_Or_Body (Loc,
5788 Tag_Typ => Tag_Typ,
5789 Name => Name_uSize,
5790 Profile => New_List (
5791 Make_Parameter_Specification (Loc,
5792 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
5793 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
5794
5795 Ret_Type => Standard_Long_Long_Integer));
5796
5797 -- Spec of _Alignment
5798
5799 Append_To (Res, Predef_Spec_Or_Body (Loc,
5800 Tag_Typ => Tag_Typ,
5801 Name => Name_uAlignment,
5802 Profile => New_List (
5803 Make_Parameter_Specification (Loc,
5804 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
5805 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
5806
5807 Ret_Type => Standard_Integer));
5808
5809 -- Specs for dispatching stream attributes
5810
5811 declare
5812 Stream_Op_TSS_Names :
5813 constant array (Integer range <>) of TSS_Name_Type :=
5814 (TSS_Stream_Read,
5815 TSS_Stream_Write,
5816 TSS_Stream_Input,
5817 TSS_Stream_Output);
5818 begin
5819 for Op in Stream_Op_TSS_Names'Range loop
5820 if Stream_Operation_OK (Tag_Typ, Stream_Op_TSS_Names (Op)) then
5821 Append_To (Res,
5822 Predef_Stream_Attr_Spec (Loc, Tag_Typ,
5823 Stream_Op_TSS_Names (Op)));
5824 end if;
5825 end loop;
5826 end;
5827
5828 -- Spec of "=" if expanded if the type is not limited and if a
5829 -- user defined "=" was not already declared for the non-full
5830 -- view of a private extension
5831
5832 if not Is_Limited_Type (Tag_Typ) then
5833 Eq_Needed := True;
5834
5835 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
5836 while Present (Prim) loop
5837
5838 -- If a primitive is encountered that renames the predefined
5839 -- equality operator before reaching any explicit equality
5840 -- primitive, then we still need to create a predefined
5841 -- equality function, because calls to it can occur via
5842 -- the renaming. A new name is created for the equality
5843 -- to avoid conflicting with any user-defined equality.
5844 -- (Note that this doesn't account for renamings of
5845 -- equality nested within subpackages???)
5846
5847 if Is_Predefined_Eq_Renaming (Node (Prim)) then
5848 Eq_Name := New_External_Name (Chars (Node (Prim)), 'E');
5849
5850 elsif Chars (Node (Prim)) = Name_Op_Eq
5851 and then (No (Alias (Node (Prim)))
5852 or else Nkind (Unit_Declaration_Node (Node (Prim))) =
5853 N_Subprogram_Renaming_Declaration)
5854 and then Etype (First_Formal (Node (Prim))) =
5855 Etype (Next_Formal (First_Formal (Node (Prim))))
5856 and then Base_Type (Etype (Node (Prim))) = Standard_Boolean
5857
5858 then
5859 Eq_Needed := False;
5860 exit;
5861
5862 -- If the parent equality is abstract, the inherited equality is
5863 -- abstract as well, and no body can be created for for it.
5864
5865 elsif Chars (Node (Prim)) = Name_Op_Eq
5866 and then Present (Alias (Node (Prim)))
5867 and then Is_Abstract (Alias (Node (Prim)))
5868 then
5869 Eq_Needed := False;
5870 exit;
5871 end if;
5872
5873 Next_Elmt (Prim);
5874 end loop;
5875
5876 -- If a renaming of predefined equality was found
5877 -- but there was no user-defined equality (so Eq_Needed
5878 -- is still true), then set the name back to Name_Op_Eq.
5879 -- But in the case where a user-defined equality was
5880 -- located after such a renaming, then the predefined
5881 -- equality function is still needed, so Eq_Needed must
5882 -- be set back to True.
5883
5884 if Eq_Name /= Name_Op_Eq then
5885 if Eq_Needed then
5886 Eq_Name := Name_Op_Eq;
5887 else
5888 Eq_Needed := True;
5889 end if;
5890 end if;
5891
5892 if Eq_Needed then
5893 Eq_Spec := Predef_Spec_Or_Body (Loc,
5894 Tag_Typ => Tag_Typ,
5895 Name => Eq_Name,
5896 Profile => New_List (
5897 Make_Parameter_Specification (Loc,
5898 Defining_Identifier =>
5899 Make_Defining_Identifier (Loc, Name_X),
5900 Parameter_Type => New_Reference_To (Tag_Typ, Loc)),
5901 Make_Parameter_Specification (Loc,
5902 Defining_Identifier =>
5903 Make_Defining_Identifier (Loc, Name_Y),
5904 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
5905 Ret_Type => Standard_Boolean);
5906 Append_To (Res, Eq_Spec);
5907
5908 if Eq_Name /= Name_Op_Eq then
5909 Renamed_Eq := Defining_Unit_Name (Specification (Eq_Spec));
5910
5911 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
5912 while Present (Prim) loop
5913
5914 -- Any renamings of equality that appeared before an
5915 -- overriding equality must be updated to refer to
5916 -- the entity for the predefined equality, otherwise
5917 -- calls via the renaming would get incorrectly
5918 -- resolved to call the user-defined equality function.
5919
5920 if Is_Predefined_Eq_Renaming (Node (Prim)) then
5921 Set_Alias (Node (Prim), Renamed_Eq);
5922
5923 -- Exit upon encountering a user-defined equality
5924
5925 elsif Chars (Node (Prim)) = Name_Op_Eq
5926 and then No (Alias (Node (Prim)))
5927 then
5928 exit;
5929 end if;
5930
5931 Next_Elmt (Prim);
5932 end loop;
5933 end if;
5934 end if;
5935
5936 -- Spec for dispatching assignment
5937
5938 Append_To (Res, Predef_Spec_Or_Body (Loc,
5939 Tag_Typ => Tag_Typ,
5940 Name => Name_uAssign,
5941 Profile => New_List (
5942 Make_Parameter_Specification (Loc,
5943 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
5944 Out_Present => True,
5945 Parameter_Type => New_Reference_To (Tag_Typ, Loc)),
5946
5947 Make_Parameter_Specification (Loc,
5948 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
5949 Parameter_Type => New_Reference_To (Tag_Typ, Loc)))));
5950 end if;
5951
5952 -- Generate the declarations for the following primitive operations:
5953 -- disp_asynchronous_select
5954 -- disp_conditional_select
5955 -- disp_get_prim_op_kind
5956 -- disp_get_task_id
5957 -- disp_timed_select
5958 -- for limited interfaces and tagged types that implement a limited
5959 -- interface.
5960
5961 if Ada_Version >= Ada_05
5962 and then
5963 ((Is_Interface (Tag_Typ)
5964 and then Is_Limited_Record (Tag_Typ))
5965 or else
5966 (not Is_Abstract (Tag_Typ)
5967 and then not Is_Controlled (Tag_Typ)
5968 and then
5969 Implements_Interface
5970 (Typ => Tag_Typ,
5971 Kind => Any_Limited_Interface,
5972 Check_Parent => True)))
5973 then
5974 Append_To (Res,
5975 Make_Subprogram_Declaration (Loc,
5976 Specification =>
5977 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
5978
5979 Append_To (Res,
5980 Make_Subprogram_Declaration (Loc,
5981 Specification =>
5982 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
5983
5984 Append_To (Res,
5985 Make_Subprogram_Declaration (Loc,
5986 Specification =>
5987 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
5988
5989 Append_To (Res,
5990 Make_Subprogram_Declaration (Loc,
5991 Specification =>
5992 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
5993
5994 Append_To (Res,
5995 Make_Subprogram_Declaration (Loc,
5996 Specification =>
5997 Make_Disp_Timed_Select_Spec (Tag_Typ)));
5998 end if;
5999
6000 -- Specs for finalization actions that may be required in case a
6001 -- future extension contain a controlled element. We generate those
6002 -- only for root tagged types where they will get dummy bodies or
6003 -- when the type has controlled components and their body must be
6004 -- generated. It is also impossible to provide those for tagged
6005 -- types defined within s-finimp since it would involve circularity
6006 -- problems
6007
6008 if In_Finalization_Root (Tag_Typ) then
6009 null;
6010
6011 -- We also skip these if finalization is not available
6012
6013 elsif Restriction_Active (No_Finalization) then
6014 null;
6015
6016 elsif Etype (Tag_Typ) = Tag_Typ or else Controlled_Type (Tag_Typ) then
6017 if not Is_Limited_Type (Tag_Typ) then
6018 Append_To (Res,
6019 Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust));
6020 end if;
6021
6022 Append_To (Res, Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize));
6023 end if;
6024
6025 Predef_List := Res;
6026 end Make_Predefined_Primitive_Specs;
6027
6028 ---------------------------------
6029 -- Needs_Simple_Initialization --
6030 ---------------------------------
6031
6032 function Needs_Simple_Initialization (T : Entity_Id) return Boolean is
6033 begin
6034 -- Check for private type, in which case test applies to the
6035 -- underlying type of the private type.
6036
6037 if Is_Private_Type (T) then
6038 declare
6039 RT : constant Entity_Id := Underlying_Type (T);
6040
6041 begin
6042 if Present (RT) then
6043 return Needs_Simple_Initialization (RT);
6044 else
6045 return False;
6046 end if;
6047 end;
6048
6049 -- Cases needing simple initialization are access types, and, if pragma
6050 -- Normalize_Scalars or Initialize_Scalars is in effect, then all scalar
6051 -- types.
6052
6053 elsif Is_Access_Type (T)
6054 or else (Init_Or_Norm_Scalars and then (Is_Scalar_Type (T)))
6055 then
6056 return True;
6057
6058 -- If Initialize/Normalize_Scalars is in effect, string objects also
6059 -- need initialization, unless they are created in the course of
6060 -- expanding an aggregate (since in the latter case they will be
6061 -- filled with appropriate initializing values before they are used).
6062
6063 elsif Init_Or_Norm_Scalars
6064 and then
6065 (Root_Type (T) = Standard_String
6066 or else Root_Type (T) = Standard_Wide_String
6067 or else Root_Type (T) = Standard_Wide_Wide_String)
6068 and then
6069 (not Is_Itype (T)
6070 or else Nkind (Associated_Node_For_Itype (T)) /= N_Aggregate)
6071 then
6072 return True;
6073
6074 else
6075 return False;
6076 end if;
6077 end Needs_Simple_Initialization;
6078
6079 ----------------------
6080 -- Predef_Deep_Spec --
6081 ----------------------
6082
6083 function Predef_Deep_Spec
6084 (Loc : Source_Ptr;
6085 Tag_Typ : Entity_Id;
6086 Name : TSS_Name_Type;
6087 For_Body : Boolean := False) return Node_Id
6088 is
6089 Prof : List_Id;
6090 Type_B : Entity_Id;
6091
6092 begin
6093 if Name = TSS_Deep_Finalize then
6094 Prof := New_List;
6095 Type_B := Standard_Boolean;
6096
6097 else
6098 Prof := New_List (
6099 Make_Parameter_Specification (Loc,
6100 Defining_Identifier => Make_Defining_Identifier (Loc, Name_L),
6101 In_Present => True,
6102 Out_Present => True,
6103 Parameter_Type =>
6104 New_Reference_To (RTE (RE_Finalizable_Ptr), Loc)));
6105 Type_B := Standard_Short_Short_Integer;
6106 end if;
6107
6108 Append_To (Prof,
6109 Make_Parameter_Specification (Loc,
6110 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
6111 In_Present => True,
6112 Out_Present => True,
6113 Parameter_Type => New_Reference_To (Tag_Typ, Loc)));
6114
6115 Append_To (Prof,
6116 Make_Parameter_Specification (Loc,
6117 Defining_Identifier => Make_Defining_Identifier (Loc, Name_B),
6118 Parameter_Type => New_Reference_To (Type_B, Loc)));
6119
6120 return Predef_Spec_Or_Body (Loc,
6121 Name => Make_TSS_Name (Tag_Typ, Name),
6122 Tag_Typ => Tag_Typ,
6123 Profile => Prof,
6124 For_Body => For_Body);
6125
6126 exception
6127 when RE_Not_Available =>
6128 return Empty;
6129 end Predef_Deep_Spec;
6130
6131 -------------------------
6132 -- Predef_Spec_Or_Body --
6133 -------------------------
6134
6135 function Predef_Spec_Or_Body
6136 (Loc : Source_Ptr;
6137 Tag_Typ : Entity_Id;
6138 Name : Name_Id;
6139 Profile : List_Id;
6140 Ret_Type : Entity_Id := Empty;
6141 For_Body : Boolean := False) return Node_Id
6142 is
6143 Id : constant Entity_Id := Make_Defining_Identifier (Loc, Name);
6144 Spec : Node_Id;
6145
6146 begin
6147 Set_Is_Public (Id, Is_Public (Tag_Typ));
6148
6149 -- The internal flag is set to mark these declarations because
6150 -- they have specific properties. First they are primitives even
6151 -- if they are not defined in the type scope (the freezing point
6152 -- is not necessarily in the same scope), furthermore the
6153 -- predefined equality can be overridden by a user-defined
6154 -- equality, no body will be generated in this case.
6155
6156 Set_Is_Internal (Id);
6157
6158 if not Debug_Generated_Code then
6159 Set_Debug_Info_Off (Id);
6160 end if;
6161
6162 if No (Ret_Type) then
6163 Spec :=
6164 Make_Procedure_Specification (Loc,
6165 Defining_Unit_Name => Id,
6166 Parameter_Specifications => Profile);
6167 else
6168 Spec :=
6169 Make_Function_Specification (Loc,
6170 Defining_Unit_Name => Id,
6171 Parameter_Specifications => Profile,
6172 Result_Definition =>
6173 New_Reference_To (Ret_Type, Loc));
6174 end if;
6175
6176 -- If body case, return empty subprogram body. Note that this is
6177 -- ill-formed, because there is not even a null statement, and
6178 -- certainly not a return in the function case. The caller is
6179 -- expected to do surgery on the body to add the appropriate stuff.
6180
6181 if For_Body then
6182 return Make_Subprogram_Body (Loc, Spec, Empty_List, Empty);
6183
6184 -- For the case of Input/Output attributes applied to an abstract type,
6185 -- generate abstract specifications. These will never be called,
6186 -- but we need the slots allocated in the dispatching table so
6187 -- that typ'Class'Input and typ'Class'Output will work properly.
6188
6189 elsif (Is_TSS (Name, TSS_Stream_Input)
6190 or else
6191 Is_TSS (Name, TSS_Stream_Output))
6192 and then Is_Abstract (Tag_Typ)
6193 then
6194 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
6195
6196 -- Normal spec case, where we return a subprogram declaration
6197
6198 else
6199 return Make_Subprogram_Declaration (Loc, Spec);
6200 end if;
6201 end Predef_Spec_Or_Body;
6202
6203 -----------------------------
6204 -- Predef_Stream_Attr_Spec --
6205 -----------------------------
6206
6207 function Predef_Stream_Attr_Spec
6208 (Loc : Source_Ptr;
6209 Tag_Typ : Entity_Id;
6210 Name : TSS_Name_Type;
6211 For_Body : Boolean := False) return Node_Id
6212 is
6213 Ret_Type : Entity_Id;
6214
6215 begin
6216 if Name = TSS_Stream_Input then
6217 Ret_Type := Tag_Typ;
6218 else
6219 Ret_Type := Empty;
6220 end if;
6221
6222 return Predef_Spec_Or_Body (Loc,
6223 Name => Make_TSS_Name (Tag_Typ, Name),
6224 Tag_Typ => Tag_Typ,
6225 Profile => Build_Stream_Attr_Profile (Loc, Tag_Typ, Name),
6226 Ret_Type => Ret_Type,
6227 For_Body => For_Body);
6228 end Predef_Stream_Attr_Spec;
6229
6230 ---------------------------------
6231 -- Predefined_Primitive_Bodies --
6232 ---------------------------------
6233
6234 function Predefined_Primitive_Bodies
6235 (Tag_Typ : Entity_Id;
6236 Renamed_Eq : Node_Id) return List_Id
6237 is
6238 Loc : constant Source_Ptr := Sloc (Tag_Typ);
6239 Res : constant List_Id := New_List;
6240 Decl : Node_Id;
6241 Prim : Elmt_Id;
6242 Eq_Needed : Boolean;
6243 Eq_Name : Name_Id;
6244 Ent : Entity_Id;
6245
6246 begin
6247 -- See if we have a predefined "=" operator
6248
6249 if Present (Renamed_Eq) then
6250 Eq_Needed := True;
6251 Eq_Name := Chars (Renamed_Eq);
6252
6253 else
6254 Eq_Needed := False;
6255 Eq_Name := No_Name;
6256
6257 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
6258 while Present (Prim) loop
6259 if Chars (Node (Prim)) = Name_Op_Eq
6260 and then Is_Internal (Node (Prim))
6261 then
6262 Eq_Needed := True;
6263 Eq_Name := Name_Op_Eq;
6264 end if;
6265
6266 Next_Elmt (Prim);
6267 end loop;
6268 end if;
6269
6270 -- Body of _Alignment
6271
6272 Decl := Predef_Spec_Or_Body (Loc,
6273 Tag_Typ => Tag_Typ,
6274 Name => Name_uAlignment,
6275 Profile => New_List (
6276 Make_Parameter_Specification (Loc,
6277 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
6278 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
6279
6280 Ret_Type => Standard_Integer,
6281 For_Body => True);
6282
6283 Set_Handled_Statement_Sequence (Decl,
6284 Make_Handled_Sequence_Of_Statements (Loc, New_List (
6285 Make_Return_Statement (Loc,
6286 Expression =>
6287 Make_Attribute_Reference (Loc,
6288 Prefix => Make_Identifier (Loc, Name_X),
6289 Attribute_Name => Name_Alignment)))));
6290
6291 Append_To (Res, Decl);
6292
6293 -- Body of _Size
6294
6295 Decl := Predef_Spec_Or_Body (Loc,
6296 Tag_Typ => Tag_Typ,
6297 Name => Name_uSize,
6298 Profile => New_List (
6299 Make_Parameter_Specification (Loc,
6300 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
6301 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
6302
6303 Ret_Type => Standard_Long_Long_Integer,
6304 For_Body => True);
6305
6306 Set_Handled_Statement_Sequence (Decl,
6307 Make_Handled_Sequence_Of_Statements (Loc, New_List (
6308 Make_Return_Statement (Loc,
6309 Expression =>
6310 Make_Attribute_Reference (Loc,
6311 Prefix => Make_Identifier (Loc, Name_X),
6312 Attribute_Name => Name_Size)))));
6313
6314 Append_To (Res, Decl);
6315
6316 -- Bodies for Dispatching stream IO routines. We need these only for
6317 -- non-limited types (in the limited case there is no dispatching).
6318 -- We also skip them if dispatching or finalization are not available.
6319
6320 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Read)
6321 and then No (TSS (Tag_Typ, TSS_Stream_Read))
6322 then
6323 Build_Record_Read_Procedure (Loc, Tag_Typ, Decl, Ent);
6324 Append_To (Res, Decl);
6325 end if;
6326
6327 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Write)
6328 and then No (TSS (Tag_Typ, TSS_Stream_Write))
6329 then
6330 Build_Record_Write_Procedure (Loc, Tag_Typ, Decl, Ent);
6331 Append_To (Res, Decl);
6332 end if;
6333
6334 -- Skip bodies of _Input and _Output for the abstract case, since
6335 -- the corresponding specs are abstract (see Predef_Spec_Or_Body)
6336
6337 if not Is_Abstract (Tag_Typ) then
6338 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Input)
6339 and then No (TSS (Tag_Typ, TSS_Stream_Input))
6340 then
6341 Build_Record_Or_Elementary_Input_Function
6342 (Loc, Tag_Typ, Decl, Ent);
6343 Append_To (Res, Decl);
6344 end if;
6345
6346 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Output)
6347 and then No (TSS (Tag_Typ, TSS_Stream_Output))
6348 then
6349 Build_Record_Or_Elementary_Output_Procedure
6350 (Loc, Tag_Typ, Decl, Ent);
6351 Append_To (Res, Decl);
6352 end if;
6353 end if;
6354
6355 -- Generate the bodies for the following primitive operations:
6356
6357 -- disp_asynchronous_select
6358 -- disp_conditional_select
6359 -- disp_get_prim_op_kind
6360 -- disp_get_task_id
6361 -- disp_timed_select
6362
6363 -- for limited interfaces and tagged types that implement a limited
6364 -- interface. The interface versions will have null bodies.
6365
6366 if Ada_Version >= Ada_05
6367 and then
6368 ((Is_Interface (Tag_Typ) and then Is_Limited_Record (Tag_Typ))
6369 or else
6370 (not Is_Abstract (Tag_Typ)
6371 and then not Is_Controlled (Tag_Typ)
6372 and then
6373 Implements_Interface
6374 (Typ => Tag_Typ,
6375 Kind => Any_Limited_Interface,
6376 Check_Parent => True)))
6377 then
6378 Append_To (Res, Make_Disp_Asynchronous_Select_Body (Tag_Typ));
6379 Append_To (Res, Make_Disp_Conditional_Select_Body (Tag_Typ));
6380 Append_To (Res, Make_Disp_Get_Prim_Op_Kind_Body (Tag_Typ));
6381 Append_To (Res, Make_Disp_Get_Task_Id_Body (Tag_Typ));
6382 Append_To (Res, Make_Disp_Timed_Select_Body (Tag_Typ));
6383 end if;
6384
6385 if not Is_Limited_Type (Tag_Typ) then
6386
6387 -- Body for equality
6388
6389 if Eq_Needed then
6390 Decl :=
6391 Predef_Spec_Or_Body (Loc,
6392 Tag_Typ => Tag_Typ,
6393 Name => Eq_Name,
6394 Profile => New_List (
6395 Make_Parameter_Specification (Loc,
6396 Defining_Identifier =>
6397 Make_Defining_Identifier (Loc, Name_X),
6398 Parameter_Type => New_Reference_To (Tag_Typ, Loc)),
6399
6400 Make_Parameter_Specification (Loc,
6401 Defining_Identifier =>
6402 Make_Defining_Identifier (Loc, Name_Y),
6403 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
6404
6405 Ret_Type => Standard_Boolean,
6406 For_Body => True);
6407
6408 declare
6409 Def : constant Node_Id := Parent (Tag_Typ);
6410 Stmts : constant List_Id := New_List;
6411 Variant_Case : Boolean := Has_Discriminants (Tag_Typ);
6412 Comps : Node_Id := Empty;
6413 Typ_Def : Node_Id := Type_Definition (Def);
6414
6415 begin
6416 if Variant_Case then
6417 if Nkind (Typ_Def) = N_Derived_Type_Definition then
6418 Typ_Def := Record_Extension_Part (Typ_Def);
6419 end if;
6420
6421 if Present (Typ_Def) then
6422 Comps := Component_List (Typ_Def);
6423 end if;
6424
6425 Variant_Case := Present (Comps)
6426 and then Present (Variant_Part (Comps));
6427 end if;
6428
6429 if Variant_Case then
6430 Append_To (Stmts,
6431 Make_Eq_If (Tag_Typ, Discriminant_Specifications (Def)));
6432 Append_List_To (Stmts, Make_Eq_Case (Tag_Typ, Comps));
6433 Append_To (Stmts,
6434 Make_Return_Statement (Loc,
6435 Expression => New_Reference_To (Standard_True, Loc)));
6436
6437 else
6438 Append_To (Stmts,
6439 Make_Return_Statement (Loc,
6440 Expression =>
6441 Expand_Record_Equality (Tag_Typ,
6442 Typ => Tag_Typ,
6443 Lhs => Make_Identifier (Loc, Name_X),
6444 Rhs => Make_Identifier (Loc, Name_Y),
6445 Bodies => Declarations (Decl))));
6446 end if;
6447
6448 Set_Handled_Statement_Sequence (Decl,
6449 Make_Handled_Sequence_Of_Statements (Loc, Stmts));
6450 end;
6451 Append_To (Res, Decl);
6452 end if;
6453
6454 -- Body for dispatching assignment
6455
6456 Decl :=
6457 Predef_Spec_Or_Body (Loc,
6458 Tag_Typ => Tag_Typ,
6459 Name => Name_uAssign,
6460 Profile => New_List (
6461 Make_Parameter_Specification (Loc,
6462 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
6463 Out_Present => True,
6464 Parameter_Type => New_Reference_To (Tag_Typ, Loc)),
6465
6466 Make_Parameter_Specification (Loc,
6467 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
6468 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
6469 For_Body => True);
6470
6471 Set_Handled_Statement_Sequence (Decl,
6472 Make_Handled_Sequence_Of_Statements (Loc, New_List (
6473 Make_Assignment_Statement (Loc,
6474 Name => Make_Identifier (Loc, Name_X),
6475 Expression => Make_Identifier (Loc, Name_Y)))));
6476
6477 Append_To (Res, Decl);
6478 end if;
6479
6480 -- Generate dummy bodies for finalization actions of types that have
6481 -- no controlled components.
6482
6483 -- Skip this processing if we are in the finalization routine in the
6484 -- runtime itself, otherwise we get hopelessly circularly confused!
6485
6486 if In_Finalization_Root (Tag_Typ) then
6487 null;
6488
6489 -- Skip this if finalization is not available
6490
6491 elsif Restriction_Active (No_Finalization) then
6492 null;
6493
6494 elsif (Etype (Tag_Typ) = Tag_Typ or else Is_Controlled (Tag_Typ))
6495 and then not Has_Controlled_Component (Tag_Typ)
6496 then
6497 if not Is_Limited_Type (Tag_Typ) then
6498 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust, True);
6499
6500 if Is_Controlled (Tag_Typ) then
6501 Set_Handled_Statement_Sequence (Decl,
6502 Make_Handled_Sequence_Of_Statements (Loc,
6503 Make_Adjust_Call (
6504 Ref => Make_Identifier (Loc, Name_V),
6505 Typ => Tag_Typ,
6506 Flist_Ref => Make_Identifier (Loc, Name_L),
6507 With_Attach => Make_Identifier (Loc, Name_B))));
6508
6509 else
6510 Set_Handled_Statement_Sequence (Decl,
6511 Make_Handled_Sequence_Of_Statements (Loc, New_List (
6512 Make_Null_Statement (Loc))));
6513 end if;
6514
6515 Append_To (Res, Decl);
6516 end if;
6517
6518 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize, True);
6519
6520 if Is_Controlled (Tag_Typ) then
6521 Set_Handled_Statement_Sequence (Decl,
6522 Make_Handled_Sequence_Of_Statements (Loc,
6523 Make_Final_Call (
6524 Ref => Make_Identifier (Loc, Name_V),
6525 Typ => Tag_Typ,
6526 With_Detach => Make_Identifier (Loc, Name_B))));
6527
6528 else
6529 Set_Handled_Statement_Sequence (Decl,
6530 Make_Handled_Sequence_Of_Statements (Loc, New_List (
6531 Make_Null_Statement (Loc))));
6532 end if;
6533
6534 Append_To (Res, Decl);
6535 end if;
6536
6537 return Res;
6538 end Predefined_Primitive_Bodies;
6539
6540 ---------------------------------
6541 -- Predefined_Primitive_Freeze --
6542 ---------------------------------
6543
6544 function Predefined_Primitive_Freeze
6545 (Tag_Typ : Entity_Id) return List_Id
6546 is
6547 Loc : constant Source_Ptr := Sloc (Tag_Typ);
6548 Res : constant List_Id := New_List;
6549 Prim : Elmt_Id;
6550 Frnodes : List_Id;
6551
6552 begin
6553 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
6554 while Present (Prim) loop
6555 if Is_Internal (Node (Prim)) then
6556 Frnodes := Freeze_Entity (Node (Prim), Loc);
6557
6558 if Present (Frnodes) then
6559 Append_List_To (Res, Frnodes);
6560 end if;
6561 end if;
6562
6563 Next_Elmt (Prim);
6564 end loop;
6565
6566 return Res;
6567 end Predefined_Primitive_Freeze;
6568
6569 -------------------------
6570 -- Stream_Operation_OK --
6571 -------------------------
6572
6573 function Stream_Operation_OK
6574 (Typ : Entity_Id;
6575 Operation : TSS_Name_Type) return Boolean
6576 is
6577 Has_Inheritable_Stream_Attribute : Boolean := False;
6578
6579 begin
6580 if Is_Limited_Type (Typ)
6581 and then Is_Tagged_Type (Typ)
6582 and then Is_Derived_Type (Typ)
6583 then
6584 -- Special case of a limited type extension: a default implementation
6585 -- of the stream attributes Read and Write exists if the attribute
6586 -- has been specified for an ancestor type.
6587
6588 Has_Inheritable_Stream_Attribute :=
6589 Present (Find_Inherited_TSS (Base_Type (Etype (Typ)), Operation));
6590 end if;
6591
6592 return
6593 not (Is_Limited_Type (Typ)
6594 and then not Has_Inheritable_Stream_Attribute)
6595 and then not Has_Unknown_Discriminants (Typ)
6596 and then RTE_Available (RE_Tag)
6597 and then RTE_Available (RE_Root_Stream_Type)
6598 and then not Restriction_Active (No_Dispatch)
6599 and then not Restriction_Active (No_Streams);
6600 end Stream_Operation_OK;
6601 end Exp_Ch3;