1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2023, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree
; use Atree
;
27 with Checks
; use Checks
;
28 with Einfo
; use Einfo
;
29 with Einfo
.Entities
; use Einfo
.Entities
;
30 with Einfo
.Utils
; use Einfo
.Utils
;
31 with Exp_Ch3
; use Exp_Ch3
;
33 with Exp_Imgv
; use Exp_Imgv
;
34 with Exp_Tss
; use Exp_Tss
;
35 with Exp_Util
; use Exp_Util
;
36 with Freeze
; use Freeze
;
37 with Namet
; use Namet
;
38 with Nlists
; use Nlists
;
39 with Nmake
; use Nmake
;
41 with Restrict
; use Restrict
;
42 with Rident
; use Rident
;
43 with Rtsfind
; use Rtsfind
;
45 with Sem_Aux
; use Sem_Aux
;
46 with Sem_Ch7
; use Sem_Ch7
;
47 with Sem_Ch8
; use Sem_Ch8
;
48 with Sem_Eval
; use Sem_Eval
;
49 with Sem_Util
; use Sem_Util
;
50 with Sinfo
; use Sinfo
;
51 with Sinfo
.Nodes
; use Sinfo
.Nodes
;
52 with Sinfo
.Utils
; use Sinfo
.Utils
;
53 with Snames
; use Snames
;
54 with Tbuild
; use Tbuild
;
55 with Uintp
; use Uintp
;
56 with Validsw
; use Validsw
;
58 package body Exp_Ch13
is
60 ------------------------------------------
61 -- Expand_N_Attribute_Definition_Clause --
62 ------------------------------------------
64 -- Expansion action depends on attribute involved
66 procedure Expand_N_Attribute_Definition_Clause
(N
: Node_Id
) is
67 Loc
: constant Source_Ptr
:= Sloc
(N
);
68 Exp
: constant Node_Id
:= Expression
(N
);
73 Ent
:= Entity
(Name
(N
));
76 Ent
:= Underlying_Type
(Ent
);
79 case Get_Attribute_Id
(Chars
(N
)) is
85 when Attribute_Address
=>
87 -- If there is an initialization which did not come from the
88 -- source program, then it is an artifact of our expansion, and we
89 -- suppress it. The case we are most concerned about here is the
90 -- initialization of a packed array to all false, which seems
91 -- inappropriate for variable to which an address clause is
92 -- applied. The expression may itself have been rewritten if the
93 -- type is packed array, so we need to examine whether the
94 -- original node is in the source. An exception though is the case
95 -- of an access variable which is default initialized to null, and
96 -- such initialization is retained.
98 -- Furthermore, if the initialization is the equivalent aggregate
99 -- of the type initialization procedure, it replaces an implicit
100 -- call to the init proc, and must be respected. Note that for
101 -- packed types we do not build equivalent aggregates.
103 -- Also, if Init_Or_Norm_Scalars applies, then we need to retain
104 -- any default initialization for objects of scalar types and
105 -- types with scalar components. Normally a composite type will
106 -- have an init_proc in the presence of Init_Or_Norm_Scalars,
107 -- so when that flag is set we have just have to do a test for
108 -- scalar and string types (the predefined string types such as
109 -- String and Wide_String don't have an init_proc).
112 Decl
: constant Node_Id
:= Declaration_Node
(Ent
);
113 Typ
: constant Entity_Id
:= Etype
(Ent
);
116 if Nkind
(Decl
) = N_Object_Declaration
117 and then Present
(Expression
(Decl
))
118 and then Nkind
(Expression
(Decl
)) /= N_Null
120 not Comes_From_Source
(Original_Node
(Expression
(Decl
)))
122 if Present
(Base_Init_Proc
(Typ
))
124 Present
(Static_Initialization
(Base_Init_Proc
(Typ
)))
128 elsif Init_Or_Norm_Scalars
129 and then (Is_Scalar_Type
(Typ
)
130 or else Is_String_Type
(Typ
))
135 Set_Expression
(Decl
, Empty
);
138 -- An object declaration to which an address clause applies
139 -- has a delayed freeze, but the address expression itself
140 -- must be elaborated at the point it appears. If the object
141 -- is controlled, additional checks apply elsewhere.
142 -- If the attribute comes from an aspect specification it
143 -- is being elaborated at the freeze point and side effects
144 -- need not be removed (and shouldn't, if the expression
145 -- depends on other entities that have delayed freeze).
146 -- This is another consequence of the delayed analysis of
147 -- aspects, and a real semantic difference.
149 elsif Nkind
(Decl
) = N_Object_Declaration
150 and then not Needs_Constant_Address
(Decl
, Typ
)
151 and then not From_Aspect_Specification
(N
)
153 Remove_Side_Effects
(Exp
);
161 when Attribute_Alignment
=>
163 -- As required by Gigi, we guarantee that the operand is an
164 -- integer literal (this simplifies things in Gigi).
166 if Nkind
(Exp
) /= N_Integer_Literal
then
167 Rewrite
(Exp
, Make_Integer_Literal
(Loc
, Expr_Value
(Exp
)));
170 -- A complex case arises if the alignment clause applies to an
171 -- unconstrained object initialized with a function call. The
172 -- result of the call is placed on the secondary stack, and the
173 -- declaration is rewritten as a renaming of a dereference, which
174 -- fails expansion. We must introduce a temporary and assign its
175 -- value to the existing entity.
177 if Nkind
(Parent
(Ent
)) = N_Object_Renaming_Declaration
178 and then not Is_Entity_Name
(Renamed_Object
(Ent
))
181 Decl
: constant Node_Id
:= Parent
(Ent
);
182 Loc
: constant Source_Ptr
:= Sloc
(N
);
183 Temp
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
188 -- Replace entity with temporary and reanalyze
190 Set_Defining_Identifier
(Decl
, Temp
);
191 Set_Analyzed
(Decl
, False);
194 -- Introduce new declaration for entity but do not reanalyze
195 -- because entity is already in scope. Type and expression
196 -- are already resolved.
199 Make_Object_Declaration
(Loc
,
200 Defining_Identifier
=> Ent
,
202 New_Occurrence_Of
(Etype
(Ent
), Loc
),
203 Expression
=> New_Occurrence_Of
(Temp
, Loc
));
205 Set_Renamed_Object
(Ent
, Empty
);
206 Insert_After
(Decl
, New_Decl
);
215 when Attribute_Storage_Size
=>
217 -- If the type is a task type, then assign the value of the
218 -- storage size to the Size variable associated with the task.
219 -- Insert the assignment right after the declaration of the Size
224 -- task_typeZ := expression
226 if Ekind
(Ent
) = E_Task_Type
then
232 Make_Assignment_Statement
(Loc
,
234 New_Occurrence_Of
(Storage_Size_Variable
(Ent
), Loc
),
236 Convert_To
(RTE
(RE_Size_Type
), Expression
(N
)));
238 -- If the clause is not generated by an aspect, insert
239 -- the assignment here. Freezing rules ensure that this
240 -- is safe, or clause will have been rejected already.
242 if Is_List_Member
(N
) then
243 Insert_After
(N
, Assign
);
245 -- Otherwise, insert assignment after task declaration.
249 (Parent
(Storage_Size_Variable
(Entity
(N
))), Assign
);
255 -- For Storage_Size for an access type, create a variable to hold
256 -- the value of the specified size with name typeV and expand an
257 -- assignment statement to initialize this value.
259 elsif Is_Access_Type
(Ent
) then
261 -- We don't need the variable for a storage size of zero
263 if not No_Pool_Assigned
(Ent
) then
265 Make_Defining_Identifier
(Loc
,
266 Chars
=> New_External_Name
(Chars
(Ent
), 'V'));
268 -- Insert the declaration of the object. If the expression
269 -- is not static it may depend on some other type that is
270 -- not frozen yet, so attach the declaration that captures
271 -- the value of the expression to the actions of the freeze
272 -- node of the current type.
275 Decl
: constant Node_Id
:=
276 Make_Object_Declaration
(Loc
,
277 Defining_Identifier
=> V
,
279 New_Occurrence_Of
(RTE
(RE_Storage_Offset
), Loc
),
282 (RTE
(RE_Storage_Offset
), Expression
(N
)));
284 if not Is_OK_Static_Expression
(Expression
(N
))
285 and then Present
(Freeze_Node
(Ent
))
287 if No
(Actions
(Freeze_Node
(Ent
))) then
288 Set_Actions
(Freeze_Node
(Ent
), New_List
(Decl
));
290 Append
(Decl
, Actions
(Freeze_Node
(Ent
)));
294 Insert_Action
(N
, Decl
);
298 Set_Storage_Size_Variable
(Ent
, Entity_Id
(V
));
302 -- Other attributes require no expansion
307 end Expand_N_Attribute_Definition_Clause
;
309 -----------------------------
310 -- Expand_N_Free_Statement --
311 -----------------------------
313 procedure Expand_N_Free_Statement
(N
: Node_Id
) is
314 Expr
: constant Node_Id
:= Expression
(N
);
318 -- Certain run-time configurations and targets do not provide support
319 -- for controlled types.
321 if Restriction_Active
(No_Finalization
) then
325 -- Use the base type to perform the check for finalization master
329 if Ekind
(Typ
) = E_Access_Subtype
then
333 -- Handle private access types
335 if Is_Private_Type
(Typ
)
336 and then Present
(Full_View
(Typ
))
338 Typ
:= Full_View
(Typ
);
341 -- Do not create a custom Deallocate when freeing an object with
342 -- suppressed finalization. In such cases the object is never attached
343 -- to a master, so it does not need to be detached. Use a regular free
344 -- statement instead.
346 if No
(Finalization_Master
(Typ
)) then
350 -- Use a temporary to store the result of a complex expression. Perform
351 -- the following transformation:
353 -- Free (Complex_Expression);
355 -- Temp : constant Type_Of_Expression := Complex_Expression;
358 if Nkind
(Expr
) /= N_Identifier
then
360 Expr_Typ
: constant Entity_Id
:= Etype
(Expr
);
361 Loc
: constant Source_Ptr
:= Sloc
(N
);
366 Temp_Id
:= Make_Temporary
(Loc
, 'T');
368 Make_Object_Declaration
(Loc
,
369 Defining_Identifier
=> Temp_Id
,
370 Object_Definition
=> New_Occurrence_Of
(Expr_Typ
, Loc
),
371 Expression
=> Relocate_Node
(Expr
)));
373 New_Expr
:= New_Occurrence_Of
(Temp_Id
, Loc
);
374 Set_Etype
(New_Expr
, Expr_Typ
);
376 Set_Expression
(N
, New_Expr
);
380 -- Create a custom Deallocate for a controlled object. This routine
381 -- ensures that the hidden list header will be deallocated along with
382 -- the actual object.
384 Build_Allocate_Deallocate_Proc
(N
, Is_Allocate
=> False);
385 end Expand_N_Free_Statement
;
387 ----------------------------
388 -- Expand_N_Freeze_Entity --
389 ----------------------------
391 procedure Expand_N_Freeze_Entity
(N
: Node_Id
) is
392 E
: constant Entity_Id
:= Entity
(N
);
395 Delete
: Boolean := False;
397 In_Other_Scope
: Boolean;
398 In_Outer_Scope
: Boolean;
401 -- If there are delayed aspect specifications, we insert them just
402 -- before the freeze node. They are already analyzed so we don't need
403 -- to reanalyze them (they were analyzed before the type was frozen),
404 -- but we want them in the tree for the back end, and so that the
405 -- listing from sprint is clearer on where these occur logically.
407 if Has_Delayed_Aspects
(E
) then
413 -- Look for aspect specs for this entity
415 Ritem
:= First_Rep_Item
(E
);
416 while Present
(Ritem
) loop
417 if Nkind
(Ritem
) = N_Aspect_Specification
418 and then Entity
(Ritem
) = E
420 Aitem
:= Aspect_Rep_Item
(Ritem
);
422 -- Skip this for aspects (e.g. Current_Value) for which
423 -- there is no corresponding pragma or attribute.
427 -- Also skip if we have a null statement rather than a
428 -- delayed aspect (this happens when we are ignoring rep
429 -- items from use of the -gnatI switch).
431 and then Nkind
(Aitem
) /= N_Null_Statement
433 pragma Assert
(Is_Delayed_Aspect
(Aitem
));
434 Insert_Before
(N
, Aitem
);
438 Next_Rep_Item
(Ritem
);
443 -- Processing for objects
445 if Is_Object
(E
) then
446 if Present
(Address_Clause
(E
)) then
447 Apply_Address_Clause_Check
(E
, N
);
450 -- Analyze actions in freeze node, if any
452 if Present
(Actions
(N
)) then
456 Act
:= First
(Actions
(N
));
457 while Present
(Act
) loop
464 -- If initialization statements have been captured in a compound
465 -- statement, insert them back into the tree now.
467 Explode_Initialization_Compound_Statement
(E
);
470 -- Only other items requiring any front end action are types and
473 elsif not Is_Type
(E
) and then not Is_Subprogram
(E
) then
477 -- Here E is a type or a subprogram
479 E_Scope
:= Scope
(E
);
481 -- This is an error protection against previous errors
484 Check_Error_Detected
;
488 -- The entity may be a subtype declared for a constrained record
489 -- component, in which case the relevant scope is the scope of
490 -- the record. This happens for class-wide subtypes created for
491 -- a constrained type extension with inherited discriminants.
494 and then not Is_Concurrent_Type
(E_Scope
)
496 E_Scope
:= Scope
(E_Scope
);
498 -- The entity may be a subtype declared for an iterator
500 elsif Ekind
(E_Scope
) = E_Loop
then
501 E_Scope
:= Scope
(E_Scope
);
504 -- Remember that we are processing a freezing entity and its freezing
505 -- nodes. This flag (non-zero = set) is used to avoid the need of
506 -- climbing through the tree while processing the freezing actions (ie.
507 -- to avoid generating spurious warnings or to avoid killing constant
508 -- indications while processing the code associated with freezing
509 -- actions). We use a counter to deal with nesting.
511 Inside_Freezing_Actions
:= Inside_Freezing_Actions
+ 1;
513 -- If we are freezing entities defined in protected types, they belong
514 -- in the enclosing scope, given that the original type has been
515 -- expanded away. The same is true for entities in task types, in
516 -- particular the parameter records of entries (Entities in bodies are
517 -- all frozen within the body). If we are in the task body, this is a
518 -- proper scope. If we are within a subprogram body, the proper scope
519 -- is the corresponding spec. This may happen for itypes generated in
520 -- the bodies of protected operations.
522 if Ekind
(E_Scope
) = E_Protected_Type
523 or else (Ekind
(E_Scope
) = E_Task_Type
524 and then not Has_Completion
(E_Scope
))
526 E_Scope
:= Scope
(E_Scope
);
528 elsif Ekind
(E_Scope
) = E_Subprogram_Body
then
529 E_Scope
:= Corresponding_Spec
(Unit_Declaration_Node
(E_Scope
));
532 -- If the scope of the entity is in open scopes, it is the current one
533 -- or an enclosing one, including a loop, a block, or a subprogram.
535 if In_Open_Scopes
(E_Scope
) then
536 In_Other_Scope
:= False;
537 In_Outer_Scope
:= E_Scope
/= Current_Scope
;
539 -- Otherwise it is a local package or a different compilation unit
542 In_Other_Scope
:= True;
543 In_Outer_Scope
:= False;
546 -- If the entity being frozen is defined in a scope that is not
547 -- currently on the scope stack, we must establish the proper
548 -- visibility before freezing the entity and related subprograms.
550 if In_Other_Scope
then
551 Push_Scope
(E_Scope
);
553 -- Finalizers are little odd in terms of freezing. The spec of the
554 -- procedure appears in the declarations while the body appears in
555 -- the statement part of a single construct. Since the finalizer must
556 -- be called by the At_End handler of the construct, the spec is
557 -- manually frozen right after its declaration. The only side effect
558 -- of this action appears in contexts where the construct is not in
559 -- its final resting place. These contexts are:
561 -- * Entry bodies - The declarations and statements are moved to
562 -- the procedure equivalen of the entry.
563 -- * Protected subprograms - The declarations and statements are
564 -- moved to the non-protected version of the subprogram.
565 -- * Task bodies - The declarations and statements are moved to the
566 -- task body procedure.
567 -- * Blocks that will be rewritten as subprograms when unnesting
570 -- Visible declarations do not need to be installed in these three
571 -- cases since it does not make semantic sense to do so. All entities
572 -- referenced by a finalizer are visible and already resolved, plus
573 -- the enclosing scope may not have visible declarations at all.
575 if Ekind
(E
) = E_Procedure
576 and then Is_Finalizer
(E
)
579 or else (Is_Subprogram
(E_Scope
)
580 and then Is_Protected_Type
(Scope
(E_Scope
)))
581 or else Is_Task_Type
(E_Scope
)
582 or else Ekind
(E_Scope
) = E_Block
)
586 Install_Visible_Declarations
(E_Scope
);
589 if Is_Concurrent_Type
(E_Scope
)
590 or else Is_Package_Or_Generic_Package
(E_Scope
)
592 Install_Private_Declarations
(E_Scope
);
595 -- If the entity is in an outer scope, then that scope needs to
596 -- temporarily become the current scope so that operations created
597 -- during type freezing will be declared in the right scope and
598 -- can properly override any corresponding inherited operations.
600 elsif In_Outer_Scope
then
601 Push_Scope
(E_Scope
);
604 -- If type, freeze the type
607 Delete
:= Freeze_Type
(N
);
609 -- And for enumeration type, build the enumeration tables
611 if Is_Enumeration_Type
(E
) then
612 Build_Enumeration_Image_Tables
(E
, N
);
615 -- If subprogram, freeze the subprogram
617 elsif Is_Subprogram
(E
) then
618 Exp_Ch6
.Freeze_Subprogram
(N
);
620 -- Ada 2005 (AI-251): Remove the freeze nodes associated with the
621 -- entities internally used by the front end to register primitives
622 -- covering abstract interfaces if they have no side effects. For the
623 -- others, gigi must discard them after evaluating the side effects.
625 Delete
:= Present
(Interface_Alias
(E
)) and then No
(Actions
(N
));
628 -- Analyze actions generated by freezing. The init_proc contains source
629 -- expressions that may raise Constraint_Error, the assignment
630 -- procedure for complex types needs checks on individual component
631 -- assignments, and wrappers may need checks. Other freezing actions
632 -- should be compiled with all checks off.
634 Decl
:= First
(Actions
(N
));
635 while Present
(Decl
) loop
636 if Nkind
(Decl
) = N_Subprogram_Body
637 and then (Is_Init_Proc
(Defining_Entity
(Decl
))
639 Chars
(Defining_Entity
(Decl
)) = Name_uAssign
641 (Present
(Corresponding_Spec
(Decl
))
643 (Corresponding_Spec
(Decl
))))
647 -- A subprogram body created for a renaming_as_body completes
648 -- a previous declaration, which may be in a different scope.
649 -- Establish the proper scope before analysis.
651 elsif Nkind
(Decl
) = N_Subprogram_Body
652 and then Present
(Corresponding_Spec
(Decl
))
653 and then Scope
(Corresponding_Spec
(Decl
)) /= Current_Scope
655 Push_Scope
(Scope
(Corresponding_Spec
(Decl
)));
656 Analyze
(Decl
, Suppress
=> All_Checks
);
659 -- We treat generated equality specially, if validity checks are
660 -- enabled, in order to detect components default-initialized with
663 elsif Nkind
(Decl
) = N_Subprogram_Body
664 and then Chars
(Defining_Entity
(Decl
)) = Name_Op_Eq
665 and then Validity_Checks_On
666 and then Initialize_Scalars
669 Save_Force
: constant Boolean := Force_Validity_Checks
;
671 Force_Validity_Checks
:= True;
673 Force_Validity_Checks
:= Save_Force
;
676 -- All other freezing actions
679 Analyze
(Decl
, Suppress
=> All_Checks
);
685 -- If we are to delete this N_Freeze_Entity, do so by rewriting so that
686 -- a loop on all nodes being inserted will work propertly.
689 Rewrite
(N
, Make_Null_Statement
(Sloc
(N
)));
692 -- Pop scope if we installed one for the analysis
694 if In_Other_Scope
then
695 if Ekind
(Current_Scope
) = E_Package
then
696 End_Package_Scope
(E_Scope
);
701 elsif In_Outer_Scope
then
705 -- Restore previous value of the nesting-level counter that records
706 -- whether we are inside a (possibly nested) call to this procedure.
708 Inside_Freezing_Actions
:= Inside_Freezing_Actions
- 1;
709 end Expand_N_Freeze_Entity
;
711 -------------------------------------------
712 -- Expand_N_Record_Representation_Clause --
713 -------------------------------------------
715 -- The only expansion required is for the case of a mod clause present,
716 -- which is removed, and translated into an alignment representation
717 -- clause inserted immediately after the record rep clause with any
718 -- initial pragmas inserted at the start of the component clause list.
720 procedure Expand_N_Record_Representation_Clause
(N
: Node_Id
) is
721 Loc
: constant Source_Ptr
:= Sloc
(N
);
722 Rectype
: constant Entity_Id
:= Entity
(Identifier
(N
));
729 if Present
(Mod_Clause
(N
)) and then not Ignore_Rep_Clauses
then
730 Mod_Val
:= Expr_Value
(Expression
(Mod_Clause
(N
)));
731 Citems
:= Pragmas_Before
(Mod_Clause
(N
));
733 if Present
(Citems
) then
734 Append_List_To
(Citems
, Component_Clauses
(N
));
735 Set_Component_Clauses
(N
, Citems
);
739 Make_Attribute_Definition_Clause
(Loc
,
740 Name
=> New_Occurrence_Of
(Base_Type
(Rectype
), Loc
),
741 Chars
=> Name_Alignment
,
742 Expression
=> Make_Integer_Literal
(Loc
, Mod_Val
));
744 Set_From_At_Mod
(AtM_Nod
);
745 Insert_After
(N
, AtM_Nod
);
746 Set_Mod_Clause
(N
, Empty
);
749 -- If the record representation clause has no components, then
750 -- completely remove it. Note that we also have to remove
751 -- ourself from the Rep Item list.
753 if Is_Empty_List
(Component_Clauses
(N
)) then
754 if First_Rep_Item
(Rectype
) = N
then
755 Set_First_Rep_Item
(Rectype
, Next_Rep_Item
(N
));
757 Repitem
:= First_Rep_Item
(Rectype
);
758 while Present
(Next_Rep_Item
(Repitem
)) loop
759 if Next_Rep_Item
(Repitem
) = N
then
760 Set_Next_Rep_Item
(Repitem
, Next_Rep_Item
(N
));
764 Next_Rep_Item
(Repitem
);
769 Make_Null_Statement
(Loc
));
771 end Expand_N_Record_Representation_Clause
;