1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2005, 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 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. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree
; use Atree
;
28 with Checks
; use Checks
;
29 with Einfo
; use Einfo
;
30 with Elists
; use Elists
;
31 with Errout
; use Errout
;
32 with Exp_Aggr
; use Exp_Aggr
;
33 with Exp_Ch7
; use Exp_Ch7
;
34 with Hostparm
; use Hostparm
;
35 with Inline
; use Inline
;
36 with Itypes
; use Itypes
;
38 with Namet
; use Namet
;
39 with Nlists
; use Nlists
;
40 with Nmake
; use Nmake
;
42 with Restrict
; use Restrict
;
43 with Rident
; use Rident
;
45 with Sem_Ch8
; use Sem_Ch8
;
46 with Sem_Eval
; use Sem_Eval
;
47 with Sem_Res
; use Sem_Res
;
48 with Sem_Type
; use Sem_Type
;
49 with Sem_Util
; use Sem_Util
;
50 with Snames
; use Snames
;
51 with Stand
; use Stand
;
52 with Stringt
; use Stringt
;
53 with Targparm
; use Targparm
;
54 with Tbuild
; use Tbuild
;
55 with Ttypes
; use Ttypes
;
56 with Uintp
; use Uintp
;
57 with Urealp
; use Urealp
;
58 with Validsw
; use Validsw
;
60 package body Exp_Util
is
62 -----------------------
63 -- Local Subprograms --
64 -----------------------
66 function Build_Task_Array_Image
70 Dyn
: Boolean := False) return Node_Id
;
71 -- Build function to generate the image string for a task that is an
72 -- array component, concatenating the images of each index. To avoid
73 -- storage leaks, the string is built with successive slice assignments.
74 -- The flag Dyn indicates whether this is called for the initialization
75 -- procedure of an array of tasks, or for the name of a dynamically
76 -- created task that is assigned to an indexed component.
78 function Build_Task_Image_Function
82 Res
: Entity_Id
) return Node_Id
;
83 -- Common processing for Task_Array_Image and Task_Record_Image.
84 -- Build function body that computes image.
86 procedure Build_Task_Image_Prefix
93 Decls
: in out List_Id
;
94 Stats
: in out List_Id
);
95 -- Common processing for Task_Array_Image and Task_Record_Image.
96 -- Create local variables and assign prefix of name to result string.
98 function Build_Task_Record_Image
101 Dyn
: Boolean := False) return Node_Id
;
102 -- Build function to generate the image string for a task that is a
103 -- record component. Concatenate name of variable with that of selector.
104 -- The flag Dyn indicates whether this is called for the initialization
105 -- procedure of record with task components, or for a dynamically
106 -- created task that is assigned to a selected component.
108 function Make_CW_Equivalent_Type
110 E
: Node_Id
) return Entity_Id
;
111 -- T is a class-wide type entity, E is the initial expression node that
112 -- constrains T in case such as: " X: T := E" or "new T'(E)"
113 -- This function returns the entity of the Equivalent type and inserts
114 -- on the fly the necessary declaration such as:
116 -- type anon is record
117 -- _parent : Root_Type (T); constrained with E discriminants (if any)
118 -- Extension : String (1 .. expr to match size of E);
121 -- This record is compatible with any object of the class of T thanks
122 -- to the first field and has the same size as E thanks to the second.
124 function Make_Literal_Range
126 Literal_Typ
: Entity_Id
) return Node_Id
;
127 -- Produce a Range node whose bounds are:
128 -- Low_Bound (Literal_Type) ..
129 -- Low_Bound (Literal_Type) + Length (Literal_Typ) - 1
130 -- this is used for expanding declarations like X : String := "sdfgdfg";
132 function New_Class_Wide_Subtype
134 N
: Node_Id
) return Entity_Id
;
135 -- Create an implicit subtype of CW_Typ attached to node N
137 ----------------------
138 -- Adjust_Condition --
139 ----------------------
141 procedure Adjust_Condition
(N
: Node_Id
) is
148 Loc
: constant Source_Ptr
:= Sloc
(N
);
149 T
: constant Entity_Id
:= Etype
(N
);
153 -- For now, we simply ignore a call where the argument has no
154 -- type (probably case of unanalyzed condition), or has a type
155 -- that is not Boolean. This is because this is a pretty marginal
156 -- piece of functionality, and violations of these rules are
157 -- likely to be truly marginal (how much code uses Fortran Logical
158 -- as the barrier to a protected entry?) and we do not want to
159 -- blow up existing programs. We can change this to an assertion
160 -- after 3.12a is released ???
162 if No
(T
) or else not Is_Boolean_Type
(T
) then
166 -- Apply validity checking if needed
168 if Validity_Checks_On
and Validity_Check_Tests
then
172 -- Immediate return if standard boolean, the most common case,
173 -- where nothing needs to be done.
175 if Base_Type
(T
) = Standard_Boolean
then
179 -- Case of zero/non-zero semantics or non-standard enumeration
180 -- representation. In each case, we rewrite the node as:
182 -- ityp!(N) /= False'Enum_Rep
184 -- where ityp is an integer type with large enough size to hold
185 -- any value of type T.
187 if Nonzero_Is_True
(T
) or else Has_Non_Standard_Rep
(T
) then
188 if Esize
(T
) <= Esize
(Standard_Integer
) then
189 Ti
:= Standard_Integer
;
191 Ti
:= Standard_Long_Long_Integer
;
196 Left_Opnd
=> Unchecked_Convert_To
(Ti
, N
),
198 Make_Attribute_Reference
(Loc
,
199 Attribute_Name
=> Name_Enum_Rep
,
201 New_Occurrence_Of
(First_Literal
(T
), Loc
))));
202 Analyze_And_Resolve
(N
, Standard_Boolean
);
205 Rewrite
(N
, Convert_To
(Standard_Boolean
, N
));
206 Analyze_And_Resolve
(N
, Standard_Boolean
);
209 end Adjust_Condition
;
211 ------------------------
212 -- Adjust_Result_Type --
213 ------------------------
215 procedure Adjust_Result_Type
(N
: Node_Id
; T
: Entity_Id
) is
217 -- Ignore call if current type is not Standard.Boolean
219 if Etype
(N
) /= Standard_Boolean
then
223 -- If result is already of correct type, nothing to do. Note that
224 -- this will get the most common case where everything has a type
225 -- of Standard.Boolean.
227 if Base_Type
(T
) = Standard_Boolean
then
232 KP
: constant Node_Kind
:= Nkind
(Parent
(N
));
235 -- If result is to be used as a Condition in the syntax, no need
236 -- to convert it back, since if it was changed to Standard.Boolean
237 -- using Adjust_Condition, that is just fine for this usage.
239 if KP
in N_Raise_xxx_Error
or else KP
in N_Has_Condition
then
242 -- If result is an operand of another logical operation, no need
243 -- to reset its type, since Standard.Boolean is just fine, and
244 -- such operations always do Adjust_Condition on their operands.
246 elsif KP
in N_Op_Boolean
247 or else KP
= N_And_Then
248 or else KP
= N_Or_Else
249 or else KP
= N_Op_Not
253 -- Otherwise we perform a conversion from the current type,
254 -- which must be Standard.Boolean, to the desired type.
258 Rewrite
(N
, Convert_To
(T
, N
));
259 Analyze_And_Resolve
(N
, T
);
263 end Adjust_Result_Type
;
265 --------------------------
266 -- Append_Freeze_Action --
267 --------------------------
269 procedure Append_Freeze_Action
(T
: Entity_Id
; N
: Node_Id
) is
270 Fnode
: Node_Id
:= Freeze_Node
(T
);
273 Ensure_Freeze_Node
(T
);
274 Fnode
:= Freeze_Node
(T
);
276 if not Present
(Actions
(Fnode
)) then
277 Set_Actions
(Fnode
, New_List
);
280 Append
(N
, Actions
(Fnode
));
281 end Append_Freeze_Action
;
283 ---------------------------
284 -- Append_Freeze_Actions --
285 ---------------------------
287 procedure Append_Freeze_Actions
(T
: Entity_Id
; L
: List_Id
) is
288 Fnode
: constant Node_Id
:= Freeze_Node
(T
);
295 if No
(Actions
(Fnode
)) then
296 Set_Actions
(Fnode
, L
);
299 Append_List
(L
, Actions
(Fnode
));
303 end Append_Freeze_Actions
;
305 ------------------------
306 -- Build_Runtime_Call --
307 ------------------------
309 function Build_Runtime_Call
(Loc
: Source_Ptr
; RE
: RE_Id
) return Node_Id
is
311 -- If entity is not available, we can skip making the call (this avoids
312 -- junk duplicated error messages in a number of cases).
314 if not RTE_Available
(RE
) then
315 return Make_Null_Statement
(Loc
);
318 Make_Procedure_Call_Statement
(Loc
,
319 Name
=> New_Reference_To
(RTE
(RE
), Loc
));
321 end Build_Runtime_Call
;
323 ----------------------------
324 -- Build_Task_Array_Image --
325 ----------------------------
327 -- This function generates the body for a function that constructs the
328 -- image string for a task that is an array component. The function is
329 -- local to the init proc for the array type, and is called for each one
330 -- of the components. The constructed image has the form of an indexed
331 -- component, whose prefix is the outer variable of the array type.
332 -- The n-dimensional array type has known indices Index, Index2...
333 -- Id_Ref is an indexed component form created by the enclosing init proc.
334 -- Its successive indices are Val1, Val2,.. which are the loop variables
335 -- in the loops that call the individual task init proc on each component.
337 -- The generated function has the following structure:
339 -- function F return String is
340 -- Pref : string renames Task_Name;
341 -- T1 : String := Index1'Image (Val1);
343 -- Tn : String := indexn'image (Valn);
344 -- Len : Integer := T1'Length + ... + Tn'Length + n + 1;
345 -- -- Len includes commas and the end parentheses.
346 -- Res : String (1..Len);
347 -- Pos : Integer := Pref'Length;
350 -- Res (1 .. Pos) := Pref;
354 -- Res (Pos .. Pos + T1'Length - 1) := T1;
355 -- Pos := Pos + T1'Length;
359 -- Res (Pos .. Pos + Tn'Length - 1) := Tn;
365 -- Needless to say, multidimensional arrays of tasks are rare enough
366 -- that the bulkiness of this code is not really a concern.
368 function Build_Task_Array_Image
372 Dyn
: Boolean := False) return Node_Id
374 Dims
: constant Nat
:= Number_Dimensions
(A_Type
);
375 -- Number of dimensions for array of tasks
377 Temps
: array (1 .. Dims
) of Entity_Id
;
378 -- Array of temporaries to hold string for each index
384 -- Total length of generated name
387 -- Running index for substring assignments
390 -- Name of enclosing variable, prefix of resulting name
393 -- String to hold result
396 -- Value of successive indices
399 -- Expression to compute total size of string
402 -- Entity for name at one index position
404 Decls
: List_Id
:= New_List
;
405 Stats
: List_Id
:= New_List
;
408 Pref
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('P'));
410 -- For a dynamic task, the name comes from the target variable.
411 -- For a static one it is a formal of the enclosing init proc.
414 Get_Name_String
(Chars
(Entity
(Prefix
(Id_Ref
))));
416 Make_Object_Declaration
(Loc
,
417 Defining_Identifier
=> Pref
,
418 Object_Definition
=> New_Occurrence_Of
(Standard_String
, Loc
),
420 Make_String_Literal
(Loc
,
421 Strval
=> String_From_Name_Buffer
)));
425 Make_Object_Renaming_Declaration
(Loc
,
426 Defining_Identifier
=> Pref
,
427 Subtype_Mark
=> New_Occurrence_Of
(Standard_String
, Loc
),
428 Name
=> Make_Identifier
(Loc
, Name_uTask_Name
)));
431 Indx
:= First_Index
(A_Type
);
432 Val
:= First
(Expressions
(Id_Ref
));
434 for J
in 1 .. Dims
loop
435 T
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('T'));
439 Make_Object_Declaration
(Loc
,
440 Defining_Identifier
=> T
,
441 Object_Definition
=> New_Occurrence_Of
(Standard_String
, Loc
),
443 Make_Attribute_Reference
(Loc
,
444 Attribute_Name
=> Name_Image
,
446 New_Occurrence_Of
(Etype
(Indx
), Loc
),
447 Expressions
=> New_List
(
448 New_Copy_Tree
(Val
)))));
454 Sum
:= Make_Integer_Literal
(Loc
, Dims
+ 1);
460 Make_Attribute_Reference
(Loc
,
461 Attribute_Name
=> Name_Length
,
463 New_Occurrence_Of
(Pref
, Loc
),
464 Expressions
=> New_List
(Make_Integer_Literal
(Loc
, 1))));
466 for J
in 1 .. Dims
loop
471 Make_Attribute_Reference
(Loc
,
472 Attribute_Name
=> Name_Length
,
474 New_Occurrence_Of
(Temps
(J
), Loc
),
475 Expressions
=> New_List
(Make_Integer_Literal
(Loc
, 1))));
478 Build_Task_Image_Prefix
(Loc
, Len
, Res
, Pos
, Pref
, Sum
, Decls
, Stats
);
480 Set_Character_Literal_Name
(Char_Code
(Character'Pos ('(')));
483 Make_Assignment_Statement
(Loc
,
484 Name
=> Make_Indexed_Component
(Loc
,
485 Prefix
=> New_Occurrence_Of
(Res
, Loc
),
486 Expressions
=> New_List
(New_Occurrence_Of
(Pos
, Loc
))),
488 Make_Character_Literal
(Loc
,
490 Char_Literal_Value
=>
491 UI_From_Int
(Character'Pos ('(')))));
494 Make_Assignment_Statement
(Loc
,
495 Name
=> New_Occurrence_Of
(Pos
, Loc
),
498 Left_Opnd
=> New_Occurrence_Of
(Pos
, Loc
),
499 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1))));
501 for J
in 1 .. Dims
loop
504 Make_Assignment_Statement
(Loc
,
505 Name
=> Make_Slice
(Loc
,
506 Prefix
=> New_Occurrence_Of
(Res
, Loc
),
509 Low_Bound
=> New_Occurrence_Of
(Pos
, Loc
),
510 High_Bound
=> Make_Op_Subtract
(Loc
,
513 Left_Opnd
=> New_Occurrence_Of
(Pos
, Loc
),
515 Make_Attribute_Reference
(Loc
,
516 Attribute_Name
=> Name_Length
,
518 New_Occurrence_Of
(Temps
(J
), Loc
),
520 New_List
(Make_Integer_Literal
(Loc
, 1)))),
521 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1)))),
523 Expression
=> New_Occurrence_Of
(Temps
(J
), Loc
)));
527 Make_Assignment_Statement
(Loc
,
528 Name
=> New_Occurrence_Of
(Pos
, Loc
),
531 Left_Opnd
=> New_Occurrence_Of
(Pos
, Loc
),
533 Make_Attribute_Reference
(Loc
,
534 Attribute_Name
=> Name_Length
,
535 Prefix
=> New_Occurrence_Of
(Temps
(J
), Loc
),
537 New_List
(Make_Integer_Literal
(Loc
, 1))))));
539 Set_Character_Literal_Name
(Char_Code
(Character'Pos (',')));
542 Make_Assignment_Statement
(Loc
,
543 Name
=> Make_Indexed_Component
(Loc
,
544 Prefix
=> New_Occurrence_Of
(Res
, Loc
),
545 Expressions
=> New_List
(New_Occurrence_Of
(Pos
, Loc
))),
547 Make_Character_Literal
(Loc
,
549 Char_Literal_Value
=>
550 UI_From_Int
(Character'Pos (',')))));
553 Make_Assignment_Statement
(Loc
,
554 Name
=> New_Occurrence_Of
(Pos
, Loc
),
557 Left_Opnd
=> New_Occurrence_Of
(Pos
, Loc
),
558 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1))));
562 Set_Character_Literal_Name
(Char_Code
(Character'Pos (')')));
565 Make_Assignment_Statement
(Loc
,
566 Name
=> Make_Indexed_Component
(Loc
,
567 Prefix
=> New_Occurrence_Of
(Res
, Loc
),
568 Expressions
=> New_List
(New_Occurrence_Of
(Len
, Loc
))),
570 Make_Character_Literal
(Loc
,
572 Char_Literal_Value
=>
573 UI_From_Int
(Character'Pos (')')))));
574 return Build_Task_Image_Function
(Loc
, Decls
, Stats
, Res
);
575 end Build_Task_Array_Image
;
577 ----------------------------
578 -- Build_Task_Image_Decls --
579 ----------------------------
581 function Build_Task_Image_Decls
584 A_Type
: Entity_Id
) return List_Id
586 Decls
: constant List_Id
:= New_List
;
587 T_Id
: Entity_Id
:= Empty
;
589 Expr
: Node_Id
:= Empty
;
590 Fun
: Node_Id
:= Empty
;
591 Is_Dyn
: constant Boolean :=
592 Nkind
(Parent
(Id_Ref
)) = N_Assignment_Statement
594 Nkind
(Expression
(Parent
(Id_Ref
))) = N_Allocator
;
597 -- If Discard_Names or No_Implicit_Heap_Allocations are in effect,
598 -- generate a dummy declaration only.
600 if Restriction_Active
(No_Implicit_Heap_Allocations
)
601 or else Global_Discard_Names
603 T_Id
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('J'));
608 Make_Object_Declaration
(Loc
,
609 Defining_Identifier
=> T_Id
,
610 Object_Definition
=> New_Occurrence_Of
(Standard_String
, Loc
),
612 Make_String_Literal
(Loc
,
613 Strval
=> String_From_Name_Buffer
)));
616 if Nkind
(Id_Ref
) = N_Identifier
617 or else Nkind
(Id_Ref
) = N_Defining_Identifier
619 -- For a simple variable, the image of the task is built from
620 -- the name of the variable. To avoid possible conflict with
621 -- the anonymous type created for a single protected object,
622 -- add a numeric suffix.
625 Make_Defining_Identifier
(Loc
,
626 New_External_Name
(Chars
(Id_Ref
), 'T', 1));
628 Get_Name_String
(Chars
(Id_Ref
));
631 Make_String_Literal
(Loc
,
632 Strval
=> String_From_Name_Buffer
);
634 elsif Nkind
(Id_Ref
) = N_Selected_Component
then
636 Make_Defining_Identifier
(Loc
,
637 New_External_Name
(Chars
(Selector_Name
(Id_Ref
)), 'T'));
638 Fun
:= Build_Task_Record_Image
(Loc
, Id_Ref
, Is_Dyn
);
640 elsif Nkind
(Id_Ref
) = N_Indexed_Component
then
642 Make_Defining_Identifier
(Loc
,
643 New_External_Name
(Chars
(A_Type
), 'N'));
645 Fun
:= Build_Task_Array_Image
(Loc
, Id_Ref
, A_Type
, Is_Dyn
);
649 if Present
(Fun
) then
651 Expr
:= Make_Function_Call
(Loc
,
652 Name
=> New_Occurrence_Of
(Defining_Entity
(Fun
), Loc
));
655 Decl
:= Make_Object_Declaration
(Loc
,
656 Defining_Identifier
=> T_Id
,
657 Object_Definition
=> New_Occurrence_Of
(Standard_String
, Loc
),
658 Constant_Present
=> True,
661 Append
(Decl
, Decls
);
663 end Build_Task_Image_Decls
;
665 -------------------------------
666 -- Build_Task_Image_Function --
667 -------------------------------
669 function Build_Task_Image_Function
673 Res
: Entity_Id
) return Node_Id
679 Make_Return_Statement
(Loc
,
680 Expression
=> New_Occurrence_Of
(Res
, Loc
)));
682 Spec
:= Make_Function_Specification
(Loc
,
683 Defining_Unit_Name
=>
684 Make_Defining_Identifier
(Loc
, New_Internal_Name
('F')),
685 Result_Definition
=> New_Occurrence_Of
(Standard_String
, Loc
));
687 -- Calls to 'Image use the secondary stack, which must be cleaned
688 -- up after the task name is built.
690 Set_Uses_Sec_Stack
(Defining_Unit_Name
(Spec
));
692 return Make_Subprogram_Body
(Loc
,
693 Specification
=> Spec
,
694 Declarations
=> Decls
,
695 Handled_Statement_Sequence
=>
696 Make_Handled_Sequence_Of_Statements
(Loc
, Statements
=> Stats
));
697 end Build_Task_Image_Function
;
699 -----------------------------
700 -- Build_Task_Image_Prefix --
701 -----------------------------
703 procedure Build_Task_Image_Prefix
710 Decls
: in out List_Id
;
711 Stats
: in out List_Id
)
714 Len
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('L'));
717 Make_Object_Declaration
(Loc
,
718 Defining_Identifier
=> Len
,
719 Object_Definition
=> New_Occurrence_Of
(Standard_Integer
, Loc
),
722 Res
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('R'));
725 Make_Object_Declaration
(Loc
,
726 Defining_Identifier
=> Res
,
728 Make_Subtype_Indication
(Loc
,
729 Subtype_Mark
=> New_Occurrence_Of
(Standard_String
, Loc
),
731 Make_Index_Or_Discriminant_Constraint
(Loc
,
735 Low_Bound
=> Make_Integer_Literal
(Loc
, 1),
736 High_Bound
=> New_Occurrence_Of
(Len
, Loc
)))))));
738 Pos
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('P'));
741 Make_Object_Declaration
(Loc
,
742 Defining_Identifier
=> Pos
,
743 Object_Definition
=> New_Occurrence_Of
(Standard_Integer
, Loc
)));
745 -- Pos := Prefix'Length;
748 Make_Assignment_Statement
(Loc
,
749 Name
=> New_Occurrence_Of
(Pos
, Loc
),
751 Make_Attribute_Reference
(Loc
,
752 Attribute_Name
=> Name_Length
,
753 Prefix
=> New_Occurrence_Of
(Prefix
, Loc
),
755 New_List
(Make_Integer_Literal
(Loc
, 1)))));
757 -- Res (1 .. Pos) := Prefix;
760 Make_Assignment_Statement
(Loc
,
761 Name
=> Make_Slice
(Loc
,
762 Prefix
=> New_Occurrence_Of
(Res
, Loc
),
765 Low_Bound
=> Make_Integer_Literal
(Loc
, 1),
766 High_Bound
=> New_Occurrence_Of
(Pos
, Loc
))),
768 Expression
=> New_Occurrence_Of
(Prefix
, Loc
)));
771 Make_Assignment_Statement
(Loc
,
772 Name
=> New_Occurrence_Of
(Pos
, Loc
),
775 Left_Opnd
=> New_Occurrence_Of
(Pos
, Loc
),
776 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1))));
777 end Build_Task_Image_Prefix
;
779 -----------------------------
780 -- Build_Task_Record_Image --
781 -----------------------------
783 function Build_Task_Record_Image
786 Dyn
: Boolean := False) return Node_Id
789 -- Total length of generated name
795 -- String to hold result
798 -- Name of enclosing variable, prefix of resulting name
801 -- Expression to compute total size of string
804 -- Entity for selector name
806 Decls
: List_Id
:= New_List
;
807 Stats
: List_Id
:= New_List
;
810 Pref
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('P'));
812 -- For a dynamic task, the name comes from the target variable.
813 -- For a static one it is a formal of the enclosing init proc.
816 Get_Name_String
(Chars
(Entity
(Prefix
(Id_Ref
))));
818 Make_Object_Declaration
(Loc
,
819 Defining_Identifier
=> Pref
,
820 Object_Definition
=> New_Occurrence_Of
(Standard_String
, Loc
),
822 Make_String_Literal
(Loc
,
823 Strval
=> String_From_Name_Buffer
)));
827 Make_Object_Renaming_Declaration
(Loc
,
828 Defining_Identifier
=> Pref
,
829 Subtype_Mark
=> New_Occurrence_Of
(Standard_String
, Loc
),
830 Name
=> Make_Identifier
(Loc
, Name_uTask_Name
)));
833 Sel
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('S'));
835 Get_Name_String
(Chars
(Selector_Name
(Id_Ref
)));
838 Make_Object_Declaration
(Loc
,
839 Defining_Identifier
=> Sel
,
840 Object_Definition
=> New_Occurrence_Of
(Standard_String
, Loc
),
842 Make_String_Literal
(Loc
,
843 Strval
=> String_From_Name_Buffer
)));
845 Sum
:= Make_Integer_Literal
(Loc
, Nat
(Name_Len
+ 1));
851 Make_Attribute_Reference
(Loc
,
852 Attribute_Name
=> Name_Length
,
854 New_Occurrence_Of
(Pref
, Loc
),
855 Expressions
=> New_List
(Make_Integer_Literal
(Loc
, 1))));
857 Build_Task_Image_Prefix
(Loc
, Len
, Res
, Pos
, Pref
, Sum
, Decls
, Stats
);
859 Set_Character_Literal_Name
(Char_Code
(Character'Pos ('.')));
864 Make_Assignment_Statement
(Loc
,
865 Name
=> Make_Indexed_Component
(Loc
,
866 Prefix
=> New_Occurrence_Of
(Res
, Loc
),
867 Expressions
=> New_List
(New_Occurrence_Of
(Pos
, Loc
))),
869 Make_Character_Literal
(Loc
,
871 Char_Literal_Value
=>
872 UI_From_Int
(Character'Pos ('.')))));
875 Make_Assignment_Statement
(Loc
,
876 Name
=> New_Occurrence_Of
(Pos
, Loc
),
879 Left_Opnd
=> New_Occurrence_Of
(Pos
, Loc
),
880 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1))));
882 -- Res (Pos .. Len) := Selector;
885 Make_Assignment_Statement
(Loc
,
886 Name
=> Make_Slice
(Loc
,
887 Prefix
=> New_Occurrence_Of
(Res
, Loc
),
890 Low_Bound
=> New_Occurrence_Of
(Pos
, Loc
),
891 High_Bound
=> New_Occurrence_Of
(Len
, Loc
))),
892 Expression
=> New_Occurrence_Of
(Sel
, Loc
)));
894 return Build_Task_Image_Function
(Loc
, Decls
, Stats
, Res
);
895 end Build_Task_Record_Image
;
897 ----------------------------------
898 -- Component_May_Be_Bit_Aligned --
899 ----------------------------------
901 function Component_May_Be_Bit_Aligned
(Comp
: Entity_Id
) return Boolean is
903 -- If no component clause, then everything is fine, since the
904 -- back end never bit-misaligns by default, even if there is
905 -- a pragma Packed for the record.
907 if No
(Component_Clause
(Comp
)) then
911 -- It is only array and record types that cause trouble
913 if not Is_Record_Type
(Etype
(Comp
))
914 and then not Is_Array_Type
(Etype
(Comp
))
918 -- If we know that we have a small (64 bits or less) record
919 -- or bit-packed array, then everything is fine, since the
920 -- back end can handle these cases correctly.
922 elsif Esize
(Comp
) <= 64
923 and then (Is_Record_Type
(Etype
(Comp
))
924 or else Is_Bit_Packed_Array
(Etype
(Comp
)))
928 -- Otherwise if the component is not byte aligned, we
929 -- know we have the nasty unaligned case.
931 elsif Normalized_First_Bit
(Comp
) /= Uint_0
932 or else Esize
(Comp
) mod System_Storage_Unit
/= Uint_0
936 -- If we are large and byte aligned, then OK at this level
941 end Component_May_Be_Bit_Aligned
;
943 -------------------------------
944 -- Convert_To_Actual_Subtype --
945 -------------------------------
947 procedure Convert_To_Actual_Subtype
(Exp
: Entity_Id
) is
951 Act_ST
:= Get_Actual_Subtype
(Exp
);
953 if Act_ST
= Etype
(Exp
) then
958 Convert_To
(Act_ST
, Relocate_Node
(Exp
)));
959 Analyze_And_Resolve
(Exp
, Act_ST
);
961 end Convert_To_Actual_Subtype
;
963 -----------------------------------
964 -- Current_Sem_Unit_Declarations --
965 -----------------------------------
967 function Current_Sem_Unit_Declarations
return List_Id
is
968 U
: Node_Id
:= Unit
(Cunit
(Current_Sem_Unit
));
972 -- If the current unit is a package body, locate the visible
973 -- declarations of the package spec.
975 if Nkind
(U
) = N_Package_Body
then
976 U
:= Unit
(Library_Unit
(Cunit
(Current_Sem_Unit
)));
979 if Nkind
(U
) = N_Package_Declaration
then
980 U
:= Specification
(U
);
981 Decls
:= Visible_Declarations
(U
);
985 Set_Visible_Declarations
(U
, Decls
);
989 Decls
:= Declarations
(U
);
993 Set_Declarations
(U
, Decls
);
998 end Current_Sem_Unit_Declarations
;
1000 -----------------------
1001 -- Duplicate_Subexpr --
1002 -----------------------
1004 function Duplicate_Subexpr
1006 Name_Req
: Boolean := False) return Node_Id
1009 Remove_Side_Effects
(Exp
, Name_Req
);
1010 return New_Copy_Tree
(Exp
);
1011 end Duplicate_Subexpr
;
1013 ---------------------------------
1014 -- Duplicate_Subexpr_No_Checks --
1015 ---------------------------------
1017 function Duplicate_Subexpr_No_Checks
1019 Name_Req
: Boolean := False) return Node_Id
1024 Remove_Side_Effects
(Exp
, Name_Req
);
1025 New_Exp
:= New_Copy_Tree
(Exp
);
1026 Remove_Checks
(New_Exp
);
1028 end Duplicate_Subexpr_No_Checks
;
1030 -----------------------------------
1031 -- Duplicate_Subexpr_Move_Checks --
1032 -----------------------------------
1034 function Duplicate_Subexpr_Move_Checks
1036 Name_Req
: Boolean := False) return Node_Id
1041 Remove_Side_Effects
(Exp
, Name_Req
);
1042 New_Exp
:= New_Copy_Tree
(Exp
);
1043 Remove_Checks
(Exp
);
1045 end Duplicate_Subexpr_Move_Checks
;
1047 --------------------
1048 -- Ensure_Defined --
1049 --------------------
1051 procedure Ensure_Defined
(Typ
: Entity_Id
; N
: Node_Id
) is
1056 if Is_Itype
(Typ
) then
1057 IR
:= Make_Itype_Reference
(Sloc
(N
));
1058 Set_Itype
(IR
, Typ
);
1060 if not In_Open_Scopes
(Scope
(Typ
))
1061 and then Is_Subprogram
(Current_Scope
)
1062 and then Scope
(Current_Scope
) /= Standard_Standard
1064 -- Insert node in front of subprogram, to avoid scope anomalies
1069 and then Nkind
(P
) /= N_Subprogram_Body
1075 Insert_Action
(P
, IR
);
1077 Insert_Action
(N
, IR
);
1081 Insert_Action
(N
, IR
);
1086 ---------------------
1087 -- Evolve_And_Then --
1088 ---------------------
1090 procedure Evolve_And_Then
(Cond
: in out Node_Id
; Cond1
: Node_Id
) is
1096 Make_And_Then
(Sloc
(Cond1
),
1098 Right_Opnd
=> Cond1
);
1100 end Evolve_And_Then
;
1102 --------------------
1103 -- Evolve_Or_Else --
1104 --------------------
1106 procedure Evolve_Or_Else
(Cond
: in out Node_Id
; Cond1
: Node_Id
) is
1112 Make_Or_Else
(Sloc
(Cond1
),
1114 Right_Opnd
=> Cond1
);
1118 ------------------------------
1119 -- Expand_Subtype_From_Expr --
1120 ------------------------------
1122 -- This function is applicable for both static and dynamic allocation of
1123 -- objects which are constrained by an initial expression. Basically it
1124 -- transforms an unconstrained subtype indication into a constrained one.
1125 -- The expression may also be transformed in certain cases in order to
1126 -- avoid multiple evaulation. In the static allocation case, the general
1131 -- is transformed into
1133 -- Val : Constrained_Subtype_of_T := Maybe_Modified_Expr;
1135 -- Here are the main cases :
1137 -- <if Expr is a Slice>
1138 -- Val : T ([Index_Subtype (Expr)]) := Expr;
1140 -- <elsif Expr is a String Literal>
1141 -- Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
1143 -- <elsif Expr is Constrained>
1144 -- subtype T is Type_Of_Expr
1147 -- <elsif Expr is an entity_name>
1148 -- Val : T (constraints taken from Expr) := Expr;
1151 -- type Axxx is access all T;
1152 -- Rval : Axxx := Expr'ref;
1153 -- Val : T (constraints taken from Rval) := Rval.all;
1155 -- ??? note: when the Expression is allocated in the secondary stack
1156 -- we could use it directly instead of copying it by declaring
1157 -- Val : T (...) renames Rval.all
1159 procedure Expand_Subtype_From_Expr
1161 Unc_Type
: Entity_Id
;
1162 Subtype_Indic
: Node_Id
;
1165 Loc
: constant Source_Ptr
:= Sloc
(N
);
1166 Exp_Typ
: constant Entity_Id
:= Etype
(Exp
);
1170 -- In general we cannot build the subtype if expansion is disabled,
1171 -- because internal entities may not have been defined. However, to
1172 -- avoid some cascaded errors, we try to continue when the expression
1173 -- is an array (or string), because it is safe to compute the bounds.
1174 -- It is in fact required to do so even in a generic context, because
1175 -- there may be constants that depend on bounds of string literal.
1177 if not Expander_Active
1178 and then (No
(Etype
(Exp
))
1179 or else Base_Type
(Etype
(Exp
)) /= Standard_String
)
1184 if Nkind
(Exp
) = N_Slice
then
1186 Slice_Type
: constant Entity_Id
:= Etype
(First_Index
(Exp_Typ
));
1189 Rewrite
(Subtype_Indic
,
1190 Make_Subtype_Indication
(Loc
,
1191 Subtype_Mark
=> New_Reference_To
(Unc_Type
, Loc
),
1193 Make_Index_Or_Discriminant_Constraint
(Loc
,
1194 Constraints
=> New_List
1195 (New_Reference_To
(Slice_Type
, Loc
)))));
1197 -- This subtype indication may be used later for contraint checks
1198 -- we better make sure that if a variable was used as a bound of
1199 -- of the original slice, its value is frozen.
1201 Force_Evaluation
(Low_Bound
(Scalar_Range
(Slice_Type
)));
1202 Force_Evaluation
(High_Bound
(Scalar_Range
(Slice_Type
)));
1205 elsif Ekind
(Exp_Typ
) = E_String_Literal_Subtype
then
1206 Rewrite
(Subtype_Indic
,
1207 Make_Subtype_Indication
(Loc
,
1208 Subtype_Mark
=> New_Reference_To
(Unc_Type
, Loc
),
1210 Make_Index_Or_Discriminant_Constraint
(Loc
,
1211 Constraints
=> New_List
(
1212 Make_Literal_Range
(Loc
,
1213 Literal_Typ
=> Exp_Typ
)))));
1215 elsif Is_Constrained
(Exp_Typ
)
1216 and then not Is_Class_Wide_Type
(Unc_Type
)
1218 if Is_Itype
(Exp_Typ
) then
1220 -- Within an initialization procedure, a selected component
1221 -- denotes a component of the enclosing record, and it appears
1222 -- as an actual in a call to its own initialization procedure.
1223 -- If this component depends on the outer discriminant, we must
1224 -- generate the proper actual subtype for it.
1226 if Nkind
(Exp
) = N_Selected_Component
1227 and then Within_Init_Proc
1230 Decl
: constant Node_Id
:=
1231 Build_Actual_Subtype_Of_Component
(Exp_Typ
, Exp
);
1233 if Present
(Decl
) then
1234 Insert_Action
(N
, Decl
);
1235 T
:= Defining_Identifier
(Decl
);
1241 -- No need to generate a new one (new what???)
1249 Make_Defining_Identifier
(Loc
,
1250 Chars
=> New_Internal_Name
('T'));
1253 Make_Subtype_Declaration
(Loc
,
1254 Defining_Identifier
=> T
,
1255 Subtype_Indication
=> New_Reference_To
(Exp_Typ
, Loc
)));
1257 -- This type is marked as an itype even though it has an
1258 -- explicit declaration because otherwise it can be marked
1259 -- with Is_Generic_Actual_Type and generate spurious errors.
1260 -- (see sem_ch8.Analyze_Package_Renaming and sem_type.covers)
1263 Set_Associated_Node_For_Itype
(T
, Exp
);
1266 Rewrite
(Subtype_Indic
, New_Reference_To
(T
, Loc
));
1268 -- nothing needs to be done for private types with unknown discriminants
1269 -- if the underlying type is not an unconstrained composite type.
1271 elsif Is_Private_Type
(Unc_Type
)
1272 and then Has_Unknown_Discriminants
(Unc_Type
)
1273 and then (not Is_Composite_Type
(Underlying_Type
(Unc_Type
))
1274 or else Is_Constrained
(Underlying_Type
(Unc_Type
)))
1278 -- Nothing to be done for derived types with unknown discriminants if
1279 -- the parent type also has unknown discriminants.
1281 elsif Is_Record_Type
(Unc_Type
)
1282 and then not Is_Class_Wide_Type
(Unc_Type
)
1283 and then Has_Unknown_Discriminants
(Unc_Type
)
1284 and then Has_Unknown_Discriminants
(Underlying_Type
(Unc_Type
))
1288 -- Nothing to be done if the type of the expression is limited, because
1289 -- in this case the expression cannot be copied, and its use can only
1290 -- be by reference and there is no need for the actual subtype.
1292 elsif Is_Limited_Type
(Exp_Typ
) then
1296 Remove_Side_Effects
(Exp
);
1297 Rewrite
(Subtype_Indic
,
1298 Make_Subtype_From_Expr
(Exp
, Unc_Type
));
1300 end Expand_Subtype_From_Expr
;
1302 --------------------------------
1303 -- Find_Implemented_Interface --
1304 --------------------------------
1306 -- Given the following code (XXX denotes irrelevant value):
1308 -- type Limd_Iface is limited interface;
1309 -- type Prot_Iface is protected interface;
1310 -- type Sync_Iface is synchronized interface;
1312 -- type Parent_Subtype is new Limd_Iface and Sync_Iface with ...
1313 -- type Child_Subtype is new Parent_Subtype and Prot_Iface with ...
1315 -- The following calls will return the following values:
1317 -- Find_Implemented_Interface
1318 -- (Child_Subtype, Synchronized_Interface, False) -> Empty
1320 -- Find_Implemented_Interface
1321 -- (Child_Subtype, Synchronized_Interface, True) -> Sync_Iface
1323 -- Find_Implemented_Interface
1324 -- (Child_Subtype, Any_Synchronized_Interface, XXX) -> Prot_Iface
1326 -- Find_Implemented_Interface
1327 -- (Child_Subtype, Any_Limited_Interface, XXX) -> Prot_Iface
1329 function Find_Implemented_Interface
1331 Kind
: Interface_Kind
;
1332 Check_Parent
: Boolean := False) return Entity_Id
1334 Iface_Elmt
: Elmt_Id
;
1336 function Interface_In_Kind
1338 Kind
: Interface_Kind
) return Boolean;
1339 -- Determine whether an interface falls into a specified kind
1341 -----------------------
1342 -- Interface_In_Kind --
1343 -----------------------
1345 function Interface_In_Kind
1347 Kind
: Interface_Kind
) return Boolean is
1349 if Is_Limited_Interface
(I
)
1350 and then (Kind
= Any_Interface
1351 or else Kind
= Any_Limited_Interface
1352 or else Kind
= Limited_Interface
)
1356 elsif Is_Protected_Interface
(I
)
1357 and then (Kind
= Any_Interface
1358 or else Kind
= Any_Limited_Interface
1359 or else Kind
= Any_Synchronized_Interface
1360 or else Kind
= Protected_Interface
)
1364 elsif Is_Synchronized_Interface
(I
)
1365 and then (Kind
= Any_Interface
1366 or else Kind
= Any_Limited_Interface
1367 or else Kind
= Synchronized_Interface
)
1371 elsif Is_Task_Interface
(I
)
1372 and then (Kind
= Any_Interface
1373 or else Kind
= Any_Limited_Interface
1374 or else Kind
= Any_Synchronized_Interface
1375 or else Kind
= Task_Interface
)
1379 -- Regular interface. This should be the last kind to check since
1380 -- all of the previous cases have their Is_Interface flags set.
1382 elsif Is_Interface
(I
)
1383 and then (Kind
= Any_Interface
1384 or else Kind
= Iface
)
1391 end Interface_In_Kind
;
1393 -- Start of processing for Find_Implemented_Interface
1396 if not Is_Tagged_Type
(Typ
) then
1400 -- Implementations of the form:
1401 -- Typ is new Interface ...
1403 if Is_Interface
(Etype
(Typ
))
1404 and then Interface_In_Kind
(Etype
(Typ
), Kind
)
1409 -- Implementations of the form:
1410 -- Typ is new Typ_Parent and Interface ...
1412 if Present
(Abstract_Interfaces
(Typ
)) then
1413 Iface_Elmt
:= First_Elmt
(Abstract_Interfaces
(Typ
));
1414 while Present
(Iface_Elmt
) loop
1415 if Interface_In_Kind
(Node
(Iface_Elmt
), Kind
) then
1416 return Node
(Iface_Elmt
);
1419 Iface_Elmt
:= Next_Elmt
(Iface_Elmt
);
1423 -- Typ is a derived type and may implement a limited interface
1424 -- through its parent subtype. Check the parent subtype as well
1425 -- as any interfaces explicitly implemented at this level.
1428 and then Ekind
(Typ
) = E_Record_Type
1429 and then Present
(Parent_Subtype
(Typ
))
1431 return Find_Implemented_Interface
(
1432 Parent_Subtype
(Typ
), Kind
, Check_Parent
);
1435 -- Typ does not implement a limited interface either at this level or
1436 -- in any of its parent subtypes.
1439 end Find_Implemented_Interface
;
1441 ------------------------
1442 -- Find_Interface_ADT --
1443 ------------------------
1445 function Find_Interface_ADT
1447 Iface
: Entity_Id
) return Entity_Id
1450 Found
: Boolean := False;
1451 Typ
: Entity_Id
:= T
;
1453 procedure Find_Secondary_Table
(Typ
: Entity_Id
);
1454 -- Internal subprogram used to recursively climb to the ancestors
1456 --------------------------
1457 -- Find_Secondary_Table --
1458 --------------------------
1460 procedure Find_Secondary_Table
(Typ
: Entity_Id
) is
1465 -- Climb to the ancestor (if any) handling private types
1467 if Present
(Full_View
(Etype
(Typ
))) then
1468 if Full_View
(Etype
(Typ
)) /= Typ
then
1469 Find_Secondary_Table
(Full_View
(Etype
(Typ
)));
1472 elsif Etype
(Typ
) /= Typ
then
1473 Find_Secondary_Table
(Etype
(Typ
));
1476 -- If we already found it there is nothing else to do
1482 if Present
(Abstract_Interfaces
(Typ
))
1483 and then not Is_Empty_Elmt_List
(Abstract_Interfaces
(Typ
))
1485 AI_Elmt
:= First_Elmt
(Abstract_Interfaces
(Typ
));
1486 while Present
(AI_Elmt
) loop
1487 AI
:= Node
(AI_Elmt
);
1489 if AI
= Iface
or else Is_Ancestor
(Iface
, AI
) then
1495 Next_Elmt
(AI_Elmt
);
1498 end Find_Secondary_Table
;
1500 -- Start of processing for Find_Interface_Tag
1503 -- Handle private types
1505 if Has_Private_Declaration
(Typ
)
1506 and then Present
(Full_View
(Typ
))
1508 Typ
:= Full_View
(Typ
);
1511 -- Handle access types
1513 if Is_Access_Type
(Typ
) then
1514 Typ
:= Directly_Designated_Type
(Typ
);
1517 -- Handle task and protected types implementing interfaces
1519 if Ekind
(Typ
) = E_Protected_Type
1520 or else Ekind
(Typ
) = E_Task_Type
1522 Typ
:= Corresponding_Record_Type
(Typ
);
1525 ADT
:= Next_Elmt
(First_Elmt
(Access_Disp_Table
(Typ
)));
1526 pragma Assert
(Present
(Node
(ADT
)));
1527 Find_Secondary_Table
(Typ
);
1528 pragma Assert
(Found
);
1530 end Find_Interface_ADT
;
1532 ------------------------
1533 -- Find_Interface_Tag --
1534 ------------------------
1536 function Find_Interface_Tag
1538 Iface
: Entity_Id
) return Entity_Id
1541 Found
: Boolean := False;
1542 Typ
: Entity_Id
:= T
;
1544 procedure Find_Tag
(Typ
: in Entity_Id
);
1545 -- Internal subprogram used to recursively climb to the ancestors
1551 procedure Find_Tag
(Typ
: in Entity_Id
) is
1556 -- Check if the interface is an immediate ancestor of the type and
1557 -- therefore shares the main tag.
1560 pragma Assert
(Etype
(First_Tag_Component
(Typ
)) = RTE
(RE_Tag
));
1561 AI_Tag
:= First_Tag_Component
(Typ
);
1566 -- Climb to the root type handling private types
1568 if Present
(Full_View
(Etype
(Typ
))) then
1569 if Full_View
(Etype
(Typ
)) /= Typ
then
1570 Find_Tag
(Full_View
(Etype
(Typ
)));
1573 elsif Etype
(Typ
) /= Typ
then
1574 Find_Tag
(Etype
(Typ
));
1577 -- Traverse the list of interfaces implemented by the type
1580 and then Present
(Abstract_Interfaces
(Typ
))
1581 and then not (Is_Empty_Elmt_List
(Abstract_Interfaces
(Typ
)))
1583 -- Skip the tag associated with the primary table
1585 pragma Assert
(Etype
(First_Tag_Component
(Typ
)) = RTE
(RE_Tag
));
1586 AI_Tag
:= Next_Tag_Component
(First_Tag_Component
(Typ
));
1587 pragma Assert
(Present
(AI_Tag
));
1589 AI_Elmt
:= First_Elmt
(Abstract_Interfaces
(Typ
));
1590 while Present
(AI_Elmt
) loop
1591 AI
:= Node
(AI_Elmt
);
1593 if AI
= Iface
or else Is_Ancestor
(Iface
, AI
) then
1598 AI_Tag
:= Next_Tag_Component
(AI_Tag
);
1599 Next_Elmt
(AI_Elmt
);
1604 -- Start of processing for Find_Interface_Tag
1607 pragma Assert
(Is_Interface
(Iface
));
1609 -- Handle private types
1611 if Has_Private_Declaration
(Typ
)
1612 and then Present
(Full_View
(Typ
))
1614 Typ
:= Full_View
(Typ
);
1617 -- Handle access types
1619 if Is_Access_Type
(Typ
) then
1620 Typ
:= Directly_Designated_Type
(Typ
);
1623 -- Handle task and protected types implementing interfaces
1625 if Is_Concurrent_Type
(Typ
) then
1626 Typ
:= Corresponding_Record_Type
(Typ
);
1629 if Is_Class_Wide_Type
(Typ
) then
1633 -- Handle entities from the limited view
1635 if Ekind
(Typ
) = E_Incomplete_Type
then
1636 pragma Assert
(Present
(Non_Limited_View
(Typ
)));
1637 Typ
:= Non_Limited_View
(Typ
);
1641 pragma Assert
(Found
);
1643 end Find_Interface_Tag
;
1645 --------------------
1646 -- Find_Interface --
1647 --------------------
1649 function Find_Interface
1651 Comp
: Entity_Id
) return Entity_Id
1654 Found
: Boolean := False;
1656 Typ
: Entity_Id
:= T
;
1658 procedure Find_Iface
(Typ
: in Entity_Id
);
1659 -- Internal subprogram used to recursively climb to the ancestors
1665 procedure Find_Iface
(Typ
: in Entity_Id
) is
1669 -- Climb to the root type
1671 if Etype
(Typ
) /= Typ
then
1672 Find_Iface
(Etype
(Typ
));
1675 -- Traverse the list of interfaces implemented by the type
1678 and then Present
(Abstract_Interfaces
(Typ
))
1679 and then not (Is_Empty_Elmt_List
(Abstract_Interfaces
(Typ
)))
1681 -- Skip the tag associated with the primary table
1683 pragma Assert
(Etype
(First_Tag_Component
(Typ
)) = RTE
(RE_Tag
));
1684 AI_Tag
:= Next_Tag_Component
(First_Tag_Component
(Typ
));
1685 pragma Assert
(Present
(AI_Tag
));
1687 AI_Elmt
:= First_Elmt
(Abstract_Interfaces
(Typ
));
1688 while Present
(AI_Elmt
) loop
1689 if AI_Tag
= Comp
then
1690 Iface
:= Node
(AI_Elmt
);
1695 AI_Tag
:= Next_Tag_Component
(AI_Tag
);
1696 Next_Elmt
(AI_Elmt
);
1701 -- Start of processing for Find_Interface
1704 -- Handle private types
1706 if Has_Private_Declaration
(Typ
)
1707 and then Present
(Full_View
(Typ
))
1709 Typ
:= Full_View
(Typ
);
1712 -- Handle access types
1714 if Is_Access_Type
(Typ
) then
1715 Typ
:= Directly_Designated_Type
(Typ
);
1718 -- Handle task and protected types implementing interfaces
1720 if Is_Concurrent_Type
(Typ
) then
1721 Typ
:= Corresponding_Record_Type
(Typ
);
1724 if Is_Class_Wide_Type
(Typ
) then
1728 -- Handle entities from the limited view
1730 if Ekind
(Typ
) = E_Incomplete_Type
then
1731 pragma Assert
(Present
(Non_Limited_View
(Typ
)));
1732 Typ
:= Non_Limited_View
(Typ
);
1736 pragma Assert
(Found
);
1744 function Find_Prim_Op
(T
: Entity_Id
; Name
: Name_Id
) return Entity_Id
is
1746 Typ
: Entity_Id
:= T
;
1749 if Is_Class_Wide_Type
(Typ
) then
1750 Typ
:= Root_Type
(Typ
);
1753 Typ
:= Underlying_Type
(Typ
);
1755 Prim
:= First_Elmt
(Primitive_Operations
(Typ
));
1756 while Chars
(Node
(Prim
)) /= Name
loop
1758 pragma Assert
(Present
(Prim
));
1764 function Find_Prim_Op
1766 Name
: TSS_Name_Type
) return Entity_Id
1769 Typ
: Entity_Id
:= T
;
1772 if Is_Class_Wide_Type
(Typ
) then
1773 Typ
:= Root_Type
(Typ
);
1776 Typ
:= Underlying_Type
(Typ
);
1778 Prim
:= First_Elmt
(Primitive_Operations
(Typ
));
1779 while not Is_TSS
(Node
(Prim
), Name
) loop
1781 pragma Assert
(Present
(Prim
));
1787 ----------------------
1788 -- Force_Evaluation --
1789 ----------------------
1791 procedure Force_Evaluation
(Exp
: Node_Id
; Name_Req
: Boolean := False) is
1793 Remove_Side_Effects
(Exp
, Name_Req
, Variable_Ref
=> True);
1794 end Force_Evaluation
;
1796 ------------------------
1797 -- Generate_Poll_Call --
1798 ------------------------
1800 procedure Generate_Poll_Call
(N
: Node_Id
) is
1802 -- No poll call if polling not active
1804 if not Polling_Required
then
1807 -- Otherwise generate require poll call
1810 Insert_Before_And_Analyze
(N
,
1811 Make_Procedure_Call_Statement
(Sloc
(N
),
1812 Name
=> New_Occurrence_Of
(RTE
(RE_Poll
), Sloc
(N
))));
1814 end Generate_Poll_Call
;
1816 ---------------------------------
1817 -- Get_Current_Value_Condition --
1818 ---------------------------------
1820 procedure Get_Current_Value_Condition
1825 Loc
: constant Source_Ptr
:= Sloc
(Var
);
1826 CV
: constant Node_Id
:= Current_Value
(Entity
(Var
));
1835 -- If statement. Condition is known true in THEN section, known False
1836 -- in any ELSIF or ELSE part, and unknown outside the IF statement.
1838 if Nkind
(CV
) = N_If_Statement
then
1840 -- Before start of IF statement
1842 if Loc
< Sloc
(CV
) then
1845 -- After end of IF statement
1847 elsif Loc
>= Sloc
(CV
) + Text_Ptr
(UI_To_Int
(End_Span
(CV
))) then
1851 -- At this stage we know that we are within the IF statement, but
1852 -- unfortunately, the tree does not record the SLOC of the ELSE so
1853 -- we cannot use a simple SLOC comparison to distinguish between
1854 -- the then/else statements, so we have to climb the tree.
1861 while Parent
(N
) /= CV
loop
1864 -- If we fall off the top of the tree, then that's odd, but
1865 -- perhaps it could occur in some error situation, and the
1866 -- safest response is simply to assume that the outcome of the
1867 -- condition is unknown. No point in bombing during an attempt
1868 -- to optimize things.
1875 -- Now we have N pointing to a node whose parent is the IF
1876 -- statement in question, so now we can tell if we are within
1877 -- the THEN statements.
1879 if Is_List_Member
(N
)
1880 and then List_Containing
(N
) = Then_Statements
(CV
)
1884 -- Otherwise we must be in ELSIF or ELSE part
1891 -- ELSIF part. Condition is known true within the referenced ELSIF,
1892 -- known False in any subsequent ELSIF or ELSE part, and unknown before
1893 -- the ELSE part or after the IF statement.
1895 elsif Nkind
(CV
) = N_Elsif_Part
then
1898 -- Before start of ELSIF part
1900 if Loc
< Sloc
(CV
) then
1903 -- After end of IF statement
1905 elsif Loc
>= Sloc
(Stm
) +
1906 Text_Ptr
(UI_To_Int
(End_Span
(Stm
)))
1911 -- Again we lack the SLOC of the ELSE, so we need to climb the tree
1912 -- to see if we are within the ELSIF part in question.
1919 while Parent
(N
) /= Stm
loop
1922 -- If we fall off the top of the tree, then that's odd, but
1923 -- perhaps it could occur in some error situation, and the
1924 -- safest response is simply to assume that the outcome of the
1925 -- condition is unknown. No point in bombing during an attempt
1926 -- to optimize things.
1933 -- Now we have N pointing to a node whose parent is the IF
1934 -- statement in question, so see if is the ELSIF part we want.
1935 -- the THEN statements.
1940 -- Otherwise we must be in susbequent ELSIF or ELSE part
1947 -- All other cases of Current_Value settings
1953 -- If we fall through here, then we have a reportable condition, Sens is
1954 -- True if the condition is true and False if it needs inverting.
1956 -- Deal with NOT operators, inverting sense
1958 Cond
:= Condition
(CV
);
1959 while Nkind
(Cond
) = N_Op_Not
loop
1960 Cond
:= Right_Opnd
(Cond
);
1964 -- Now we must have a relational operator
1966 pragma Assert
(Entity
(Var
) = Entity
(Left_Opnd
(Cond
)));
1967 Val
:= Right_Opnd
(Cond
);
1970 if Sens
= False then
1972 when N_Op_Eq
=> Op
:= N_Op_Ne
;
1973 when N_Op_Ne
=> Op
:= N_Op_Eq
;
1974 when N_Op_Lt
=> Op
:= N_Op_Ge
;
1975 when N_Op_Gt
=> Op
:= N_Op_Le
;
1976 when N_Op_Le
=> Op
:= N_Op_Gt
;
1977 when N_Op_Ge
=> Op
:= N_Op_Lt
;
1979 -- No other entry should be possible
1982 raise Program_Error
;
1985 end Get_Current_Value_Condition
;
1987 --------------------
1988 -- Homonym_Number --
1989 --------------------
1991 function Homonym_Number
(Subp
: Entity_Id
) return Nat
is
1997 Hom
:= Homonym
(Subp
);
1998 while Present
(Hom
) loop
1999 if Scope
(Hom
) = Scope
(Subp
) then
2003 Hom
:= Homonym
(Hom
);
2009 --------------------------
2010 -- Implements_Interface --
2011 --------------------------
2013 function Implements_Interface
2015 Kind
: Interface_Kind
;
2016 Check_Parent
: Boolean := False) return Boolean is
2018 return Find_Implemented_Interface
(Typ
, Kind
, Check_Parent
) /= Empty
;
2019 end Implements_Interface
;
2021 ------------------------------
2022 -- In_Unconditional_Context --
2023 ------------------------------
2025 function In_Unconditional_Context
(Node
: Node_Id
) return Boolean is
2030 while Present
(P
) loop
2032 when N_Subprogram_Body
=>
2035 when N_If_Statement
=>
2038 when N_Loop_Statement
=>
2041 when N_Case_Statement
=>
2050 end In_Unconditional_Context
;
2056 procedure Insert_Action
(Assoc_Node
: Node_Id
; Ins_Action
: Node_Id
) is
2058 if Present
(Ins_Action
) then
2059 Insert_Actions
(Assoc_Node
, New_List
(Ins_Action
));
2063 -- Version with check(s) suppressed
2065 procedure Insert_Action
2066 (Assoc_Node
: Node_Id
; Ins_Action
: Node_Id
; Suppress
: Check_Id
)
2069 Insert_Actions
(Assoc_Node
, New_List
(Ins_Action
), Suppress
);
2072 --------------------
2073 -- Insert_Actions --
2074 --------------------
2076 procedure Insert_Actions
(Assoc_Node
: Node_Id
; Ins_Actions
: List_Id
) is
2080 Wrapped_Node
: Node_Id
:= Empty
;
2083 if No
(Ins_Actions
) or else Is_Empty_List
(Ins_Actions
) then
2087 -- Ignore insert of actions from inside default expression in the
2088 -- special preliminary analyze mode. Any insertions at this point
2089 -- have no relevance, since we are only doing the analyze to freeze
2090 -- the types of any static expressions. See section "Handling of
2091 -- Default Expressions" in the spec of package Sem for further details.
2093 if In_Default_Expression
then
2097 -- If the action derives from stuff inside a record, then the actions
2098 -- are attached to the current scope, to be inserted and analyzed on
2099 -- exit from the scope. The reason for this is that we may also
2100 -- be generating freeze actions at the same time, and they must
2101 -- eventually be elaborated in the correct order.
2103 if Is_Record_Type
(Current_Scope
)
2104 and then not Is_Frozen
(Current_Scope
)
2106 if No
(Scope_Stack
.Table
2107 (Scope_Stack
.Last
).Pending_Freeze_Actions
)
2109 Scope_Stack
.Table
(Scope_Stack
.Last
).Pending_Freeze_Actions
:=
2114 Scope_Stack
.Table
(Scope_Stack
.Last
).Pending_Freeze_Actions
);
2120 -- We now intend to climb up the tree to find the right point to
2121 -- insert the actions. We start at Assoc_Node, unless this node is
2122 -- a subexpression in which case we start with its parent. We do this
2123 -- for two reasons. First it speeds things up. Second, if Assoc_Node
2124 -- is itself one of the special nodes like N_And_Then, then we assume
2125 -- that an initial request to insert actions for such a node does not
2126 -- expect the actions to get deposited in the node for later handling
2127 -- when the node is expanded, since clearly the node is being dealt
2128 -- with by the caller. Note that in the subexpression case, N is
2129 -- always the child we came from.
2131 -- N_Raise_xxx_Error is an annoying special case, it is a statement
2132 -- if it has type Standard_Void_Type, and a subexpression otherwise.
2133 -- otherwise. Procedure attribute references are also statements.
2135 if Nkind
(Assoc_Node
) in N_Subexpr
2136 and then (Nkind
(Assoc_Node
) in N_Raise_xxx_Error
2137 or else Etype
(Assoc_Node
) /= Standard_Void_Type
)
2138 and then (Nkind
(Assoc_Node
) /= N_Attribute_Reference
2140 not Is_Procedure_Attribute_Name
2141 (Attribute_Name
(Assoc_Node
)))
2143 P
:= Assoc_Node
; -- ??? does not agree with above!
2144 N
:= Parent
(Assoc_Node
);
2146 -- Non-subexpression case. Note that N is initially Empty in this
2147 -- case (N is only guaranteed Non-Empty in the subexpr case).
2154 -- Capture root of the transient scope
2156 if Scope_Is_Transient
then
2157 Wrapped_Node
:= Node_To_Be_Wrapped
;
2161 pragma Assert
(Present
(P
));
2165 -- Case of right operand of AND THEN or OR ELSE. Put the actions
2166 -- in the Actions field of the right operand. They will be moved
2167 -- out further when the AND THEN or OR ELSE operator is expanded.
2168 -- Nothing special needs to be done for the left operand since
2169 -- in that case the actions are executed unconditionally.
2171 when N_And_Then | N_Or_Else
=>
2172 if N
= Right_Opnd
(P
) then
2173 if Present
(Actions
(P
)) then
2174 Insert_List_After_And_Analyze
2175 (Last
(Actions
(P
)), Ins_Actions
);
2177 Set_Actions
(P
, Ins_Actions
);
2178 Analyze_List
(Actions
(P
));
2184 -- Then or Else operand of conditional expression. Add actions to
2185 -- Then_Actions or Else_Actions field as appropriate. The actions
2186 -- will be moved further out when the conditional is expanded.
2188 when N_Conditional_Expression
=>
2190 ThenX
: constant Node_Id
:= Next
(First
(Expressions
(P
)));
2191 ElseX
: constant Node_Id
:= Next
(ThenX
);
2194 -- Actions belong to the then expression, temporarily
2195 -- place them as Then_Actions of the conditional expr.
2196 -- They will be moved to the proper place later when
2197 -- the conditional expression is expanded.
2200 if Present
(Then_Actions
(P
)) then
2201 Insert_List_After_And_Analyze
2202 (Last
(Then_Actions
(P
)), Ins_Actions
);
2204 Set_Then_Actions
(P
, Ins_Actions
);
2205 Analyze_List
(Then_Actions
(P
));
2210 -- Actions belong to the else expression, temporarily
2211 -- place them as Else_Actions of the conditional expr.
2212 -- They will be moved to the proper place later when
2213 -- the conditional expression is expanded.
2215 elsif N
= ElseX
then
2216 if Present
(Else_Actions
(P
)) then
2217 Insert_List_After_And_Analyze
2218 (Last
(Else_Actions
(P
)), Ins_Actions
);
2220 Set_Else_Actions
(P
, Ins_Actions
);
2221 Analyze_List
(Else_Actions
(P
));
2226 -- Actions belong to the condition. In this case they are
2227 -- unconditionally executed, and so we can continue the
2228 -- search for the proper insert point.
2235 -- Case of appearing in the condition of a while expression or
2236 -- elsif. We insert the actions into the Condition_Actions field.
2237 -- They will be moved further out when the while loop or elsif
2240 when N_Iteration_Scheme |
2243 if N
= Condition
(P
) then
2244 if Present
(Condition_Actions
(P
)) then
2245 Insert_List_After_And_Analyze
2246 (Last
(Condition_Actions
(P
)), Ins_Actions
);
2248 Set_Condition_Actions
(P
, Ins_Actions
);
2250 -- Set the parent of the insert actions explicitly.
2251 -- This is not a syntactic field, but we need the
2252 -- parent field set, in particular so that freeze
2253 -- can understand that it is dealing with condition
2254 -- actions, and properly insert the freezing actions.
2256 Set_Parent
(Ins_Actions
, P
);
2257 Analyze_List
(Condition_Actions
(P
));
2263 -- Statements, declarations, pragmas, representation clauses
2268 N_Procedure_Call_Statement |
2269 N_Statement_Other_Than_Procedure_Call |
2275 -- Representation_Clause
2278 N_Attribute_Definition_Clause |
2279 N_Enumeration_Representation_Clause |
2280 N_Record_Representation_Clause |
2284 N_Abstract_Subprogram_Declaration |
2286 N_Exception_Declaration |
2287 N_Exception_Renaming_Declaration |
2288 N_Formal_Abstract_Subprogram_Declaration |
2289 N_Formal_Concrete_Subprogram_Declaration |
2290 N_Formal_Object_Declaration |
2291 N_Formal_Type_Declaration |
2292 N_Full_Type_Declaration |
2293 N_Function_Instantiation |
2294 N_Generic_Function_Renaming_Declaration |
2295 N_Generic_Package_Declaration |
2296 N_Generic_Package_Renaming_Declaration |
2297 N_Generic_Procedure_Renaming_Declaration |
2298 N_Generic_Subprogram_Declaration |
2299 N_Implicit_Label_Declaration |
2300 N_Incomplete_Type_Declaration |
2301 N_Number_Declaration |
2302 N_Object_Declaration |
2303 N_Object_Renaming_Declaration |
2305 N_Package_Body_Stub |
2306 N_Package_Declaration |
2307 N_Package_Instantiation |
2308 N_Package_Renaming_Declaration |
2309 N_Private_Extension_Declaration |
2310 N_Private_Type_Declaration |
2311 N_Procedure_Instantiation |
2312 N_Protected_Body_Stub |
2313 N_Protected_Type_Declaration |
2314 N_Single_Task_Declaration |
2316 N_Subprogram_Body_Stub |
2317 N_Subprogram_Declaration |
2318 N_Subprogram_Renaming_Declaration |
2319 N_Subtype_Declaration |
2322 N_Task_Type_Declaration |
2324 -- Freeze entity behaves like a declaration or statement
2328 -- Do not insert here if the item is not a list member (this
2329 -- happens for example with a triggering statement, and the
2330 -- proper approach is to insert before the entire select).
2332 if not Is_List_Member
(P
) then
2335 -- Do not insert if parent of P is an N_Component_Association
2336 -- node (i.e. we are in the context of an N_Aggregate node.
2337 -- In this case we want to insert before the entire aggregate.
2339 elsif Nkind
(Parent
(P
)) = N_Component_Association
then
2342 -- Do not insert if the parent of P is either an N_Variant
2343 -- node or an N_Record_Definition node, meaning in either
2344 -- case that P is a member of a component list, and that
2345 -- therefore the actions should be inserted outside the
2346 -- complete record declaration.
2348 elsif Nkind
(Parent
(P
)) = N_Variant
2349 or else Nkind
(Parent
(P
)) = N_Record_Definition
2353 -- Do not insert freeze nodes within the loop generated for
2354 -- an aggregate, because they may be elaborated too late for
2355 -- subsequent use in the back end: within a package spec the
2356 -- loop is part of the elaboration procedure and is only
2357 -- elaborated during the second pass.
2358 -- If the loop comes from source, or the entity is local to
2359 -- the loop itself it must remain within.
2361 elsif Nkind
(Parent
(P
)) = N_Loop_Statement
2362 and then not Comes_From_Source
(Parent
(P
))
2363 and then Nkind
(First
(Ins_Actions
)) = N_Freeze_Entity
2365 Scope
(Entity
(First
(Ins_Actions
))) /= Current_Scope
2369 -- Otherwise we can go ahead and do the insertion
2371 elsif P
= Wrapped_Node
then
2372 Store_Before_Actions_In_Scope
(Ins_Actions
);
2376 Insert_List_Before_And_Analyze
(P
, Ins_Actions
);
2380 -- A special case, N_Raise_xxx_Error can act either as a
2381 -- statement or a subexpression. We tell the difference
2382 -- by looking at the Etype. It is set to Standard_Void_Type
2383 -- in the statement case.
2386 N_Raise_xxx_Error
=>
2387 if Etype
(P
) = Standard_Void_Type
then
2388 if P
= Wrapped_Node
then
2389 Store_Before_Actions_In_Scope
(Ins_Actions
);
2391 Insert_List_Before_And_Analyze
(P
, Ins_Actions
);
2396 -- In the subexpression case, keep climbing
2402 -- If a component association appears within a loop created for
2403 -- an array aggregate, attach the actions to the association so
2404 -- they can be subsequently inserted within the loop. For other
2405 -- component associations insert outside of the aggregate. For
2406 -- an association that will generate a loop, its Loop_Actions
2407 -- attribute is already initialized (see exp_aggr.adb).
2409 -- The list of loop_actions can in turn generate additional ones,
2410 -- that are inserted before the associated node. If the associated
2411 -- node is outside the aggregate, the new actions are collected
2412 -- at the end of the loop actions, to respect the order in which
2413 -- they are to be elaborated.
2416 N_Component_Association
=>
2417 if Nkind
(Parent
(P
)) = N_Aggregate
2418 and then Present
(Loop_Actions
(P
))
2420 if Is_Empty_List
(Loop_Actions
(P
)) then
2421 Set_Loop_Actions
(P
, Ins_Actions
);
2422 Analyze_List
(Ins_Actions
);
2429 -- Check whether these actions were generated
2430 -- by a declaration that is part of the loop_
2431 -- actions for the component_association.
2434 while Present
(Decl
) loop
2435 exit when Parent
(Decl
) = P
2436 and then Is_List_Member
(Decl
)
2438 List_Containing
(Decl
) = Loop_Actions
(P
);
2439 Decl
:= Parent
(Decl
);
2442 if Present
(Decl
) then
2443 Insert_List_Before_And_Analyze
2444 (Decl
, Ins_Actions
);
2446 Insert_List_After_And_Analyze
2447 (Last
(Loop_Actions
(P
)), Ins_Actions
);
2458 -- Another special case, an attribute denoting a procedure call
2461 N_Attribute_Reference
=>
2462 if Is_Procedure_Attribute_Name
(Attribute_Name
(P
)) then
2463 if P
= Wrapped_Node
then
2464 Store_Before_Actions_In_Scope
(Ins_Actions
);
2466 Insert_List_Before_And_Analyze
(P
, Ins_Actions
);
2471 -- In the subexpression case, keep climbing
2477 -- For all other node types, keep climbing tree
2481 N_Accept_Alternative |
2482 N_Access_Definition |
2483 N_Access_Function_Definition |
2484 N_Access_Procedure_Definition |
2485 N_Access_To_Object_Definition |
2488 N_Case_Statement_Alternative |
2489 N_Character_Literal |
2490 N_Compilation_Unit |
2491 N_Compilation_Unit_Aux |
2492 N_Component_Clause |
2493 N_Component_Declaration |
2494 N_Component_Definition |
2496 N_Constrained_Array_Definition |
2497 N_Decimal_Fixed_Point_Definition |
2498 N_Defining_Character_Literal |
2499 N_Defining_Identifier |
2500 N_Defining_Operator_Symbol |
2501 N_Defining_Program_Unit_Name |
2502 N_Delay_Alternative |
2503 N_Delta_Constraint |
2504 N_Derived_Type_Definition |
2506 N_Digits_Constraint |
2507 N_Discriminant_Association |
2508 N_Discriminant_Specification |
2510 N_Entry_Body_Formal_Part |
2511 N_Entry_Call_Alternative |
2512 N_Entry_Declaration |
2513 N_Entry_Index_Specification |
2514 N_Enumeration_Type_Definition |
2516 N_Exception_Handler |
2518 N_Explicit_Dereference |
2519 N_Extension_Aggregate |
2520 N_Floating_Point_Definition |
2521 N_Formal_Decimal_Fixed_Point_Definition |
2522 N_Formal_Derived_Type_Definition |
2523 N_Formal_Discrete_Type_Definition |
2524 N_Formal_Floating_Point_Definition |
2525 N_Formal_Modular_Type_Definition |
2526 N_Formal_Ordinary_Fixed_Point_Definition |
2527 N_Formal_Package_Declaration |
2528 N_Formal_Private_Type_Definition |
2529 N_Formal_Signed_Integer_Type_Definition |
2531 N_Function_Specification |
2532 N_Generic_Association |
2533 N_Handled_Sequence_Of_Statements |
2536 N_Index_Or_Discriminant_Constraint |
2537 N_Indexed_Component |
2541 N_Loop_Parameter_Specification |
2543 N_Modular_Type_Definition |
2569 N_Op_Shift_Right_Arithmetic |
2573 N_Ordinary_Fixed_Point_Definition |
2575 N_Package_Specification |
2576 N_Parameter_Association |
2577 N_Parameter_Specification |
2578 N_Pragma_Argument_Association |
2579 N_Procedure_Specification |
2581 N_Protected_Definition |
2582 N_Qualified_Expression |
2584 N_Range_Constraint |
2586 N_Real_Range_Specification |
2587 N_Record_Definition |
2589 N_Selected_Component |
2590 N_Signed_Integer_Type_Definition |
2591 N_Single_Protected_Declaration |
2595 N_Subtype_Indication |
2598 N_Terminate_Alternative |
2599 N_Triggering_Alternative |
2601 N_Unchecked_Expression |
2602 N_Unchecked_Type_Conversion |
2603 N_Unconstrained_Array_Definition |
2606 N_Use_Package_Clause |
2610 N_Validate_Unchecked_Conversion |
2618 -- Make sure that inserted actions stay in the transient scope
2620 if P
= Wrapped_Node
then
2621 Store_Before_Actions_In_Scope
(Ins_Actions
);
2625 -- If we fall through above tests, keep climbing tree
2629 if Nkind
(Parent
(N
)) = N_Subunit
then
2631 -- This is the proper body corresponding to a stub. Insertion
2632 -- must be done at the point of the stub, which is in the decla-
2633 -- tive part of the parent unit.
2635 P
:= Corresponding_Stub
(Parent
(N
));
2644 -- Version with check(s) suppressed
2646 procedure Insert_Actions
2647 (Assoc_Node
: Node_Id
; Ins_Actions
: List_Id
; Suppress
: Check_Id
)
2650 if Suppress
= All_Checks
then
2652 Svg
: constant Suppress_Array
:= Scope_Suppress
;
2654 Scope_Suppress
:= (others => True);
2655 Insert_Actions
(Assoc_Node
, Ins_Actions
);
2656 Scope_Suppress
:= Svg
;
2661 Svg
: constant Boolean := Scope_Suppress
(Suppress
);
2663 Scope_Suppress
(Suppress
) := True;
2664 Insert_Actions
(Assoc_Node
, Ins_Actions
);
2665 Scope_Suppress
(Suppress
) := Svg
;
2670 --------------------------
2671 -- Insert_Actions_After --
2672 --------------------------
2674 procedure Insert_Actions_After
2675 (Assoc_Node
: Node_Id
;
2676 Ins_Actions
: List_Id
)
2679 if Scope_Is_Transient
2680 and then Assoc_Node
= Node_To_Be_Wrapped
2682 Store_After_Actions_In_Scope
(Ins_Actions
);
2684 Insert_List_After_And_Analyze
(Assoc_Node
, Ins_Actions
);
2686 end Insert_Actions_After
;
2688 ---------------------------------
2689 -- Insert_Library_Level_Action --
2690 ---------------------------------
2692 procedure Insert_Library_Level_Action
(N
: Node_Id
) is
2693 Aux
: constant Node_Id
:= Aux_Decls_Node
(Cunit
(Main_Unit
));
2696 New_Scope
(Cunit_Entity
(Main_Unit
));
2698 if No
(Actions
(Aux
)) then
2699 Set_Actions
(Aux
, New_List
(N
));
2701 Append
(N
, Actions
(Aux
));
2706 end Insert_Library_Level_Action
;
2708 ----------------------------------
2709 -- Insert_Library_Level_Actions --
2710 ----------------------------------
2712 procedure Insert_Library_Level_Actions
(L
: List_Id
) is
2713 Aux
: constant Node_Id
:= Aux_Decls_Node
(Cunit
(Main_Unit
));
2716 if Is_Non_Empty_List
(L
) then
2717 New_Scope
(Cunit_Entity
(Main_Unit
));
2719 if No
(Actions
(Aux
)) then
2720 Set_Actions
(Aux
, L
);
2723 Insert_List_After_And_Analyze
(Last
(Actions
(Aux
)), L
);
2728 end Insert_Library_Level_Actions
;
2730 ----------------------
2731 -- Inside_Init_Proc --
2732 ----------------------
2734 function Inside_Init_Proc
return Boolean is
2740 and then S
/= Standard_Standard
2742 if Is_Init_Proc
(S
) then
2750 end Inside_Init_Proc
;
2752 ----------------------------
2753 -- Is_All_Null_Statements --
2754 ----------------------------
2756 function Is_All_Null_Statements
(L
: List_Id
) return Boolean is
2761 while Present
(Stm
) loop
2762 if Nkind
(Stm
) /= N_Null_Statement
then
2770 end Is_All_Null_Statements
;
2772 -----------------------------------------
2773 -- Is_Predefined_Dispatching_Operation --
2774 -----------------------------------------
2776 function Is_Predefined_Dispatching_Operation
2777 (Subp
: Entity_Id
) return Boolean
2779 TSS_Name
: TSS_Name_Type
;
2780 E
: Entity_Id
:= Subp
;
2782 pragma Assert
(Is_Dispatching_Operation
(Subp
));
2784 -- Handle overriden subprograms
2786 while Present
(Alias
(E
)) loop
2790 Get_Name_String
(Chars
(E
));
2792 if Name_Len
> TSS_Name_Type
'Last then
2793 TSS_Name
:= TSS_Name_Type
(Name_Buffer
(Name_Len
- TSS_Name
'Length + 1
2795 if Chars
(E
) = Name_uSize
2796 or else Chars
(E
) = Name_uAlignment
2797 or else TSS_Name
= TSS_Stream_Read
2798 or else TSS_Name
= TSS_Stream_Write
2799 or else TSS_Name
= TSS_Stream_Input
2800 or else TSS_Name
= TSS_Stream_Output
2801 or else Chars
(E
) = Name_Op_Eq
2802 or else Chars
(E
) = Name_uAssign
2803 or else TSS_Name
= TSS_Deep_Adjust
2804 or else TSS_Name
= TSS_Deep_Finalize
2805 or else (Ada_Version
>= Ada_05
2806 and then (Chars
(E
) = Name_uDisp_Asynchronous_Select
2807 or else Chars
(E
) = Name_uDisp_Conditional_Select
2808 or else Chars
(E
) = Name_uDisp_Get_Prim_Op_Kind
2809 or else Chars
(E
) = Name_uDisp_Get_Task_Id
2810 or else Chars
(E
) = Name_uDisp_Timed_Select
))
2817 end Is_Predefined_Dispatching_Operation
;
2819 ----------------------------------
2820 -- Is_Possibly_Unaligned_Object --
2821 ----------------------------------
2823 function Is_Possibly_Unaligned_Object
(N
: Node_Id
) return Boolean is
2824 T
: constant Entity_Id
:= Etype
(N
);
2827 -- If renamed object, apply test to underlying object
2829 if Is_Entity_Name
(N
)
2830 and then Is_Object
(Entity
(N
))
2831 and then Present
(Renamed_Object
(Entity
(N
)))
2833 return Is_Possibly_Unaligned_Object
(Renamed_Object
(Entity
(N
)));
2836 -- Tagged and controlled types and aliased types are always aligned,
2837 -- as are concurrent types.
2840 or else Has_Controlled_Component
(T
)
2841 or else Is_Concurrent_Type
(T
)
2842 or else Is_Tagged_Type
(T
)
2843 or else Is_Controlled
(T
)
2848 -- If this is an element of a packed array, may be unaligned
2850 if Is_Ref_To_Bit_Packed_Array
(N
) then
2854 -- Case of component reference
2856 if Nkind
(N
) = N_Selected_Component
then
2858 P
: constant Node_Id
:= Prefix
(N
);
2859 C
: constant Entity_Id
:= Entity
(Selector_Name
(N
));
2864 -- If component reference is for an array with non-static bounds,
2865 -- then it is always aligned: we can only process unaligned
2866 -- arrays with static bounds (more accurately bounds known at
2869 if Is_Array_Type
(T
)
2870 and then not Compile_Time_Known_Bounds
(T
)
2875 -- If component is aliased, it is definitely properly aligned
2877 if Is_Aliased
(C
) then
2881 -- If component is for a type implemented as a scalar, and the
2882 -- record is packed, and the component is other than the first
2883 -- component of the record, then the component may be unaligned.
2885 if Is_Packed
(Etype
(P
))
2886 and then Represented_As_Scalar
(Etype
(C
))
2887 and then First_Entity
(Scope
(C
)) /= C
2892 -- Compute maximum possible alignment for T
2894 -- If alignment is known, then that settles things
2896 if Known_Alignment
(T
) then
2897 M
:= UI_To_Int
(Alignment
(T
));
2899 -- If alignment is not known, tentatively set max alignment
2902 M
:= Ttypes
.Maximum_Alignment
;
2904 -- We can reduce this if the Esize is known since the default
2905 -- alignment will never be more than the smallest power of 2
2906 -- that does not exceed this Esize value.
2908 if Known_Esize
(T
) then
2909 S
:= UI_To_Int
(Esize
(T
));
2911 while (M
/ 2) >= S
loop
2917 -- If the component reference is for a record that has a specified
2918 -- alignment, and we either know it is too small, or cannot tell,
2919 -- then the component may be unaligned
2921 if Known_Alignment
(Etype
(P
))
2922 and then Alignment
(Etype
(P
)) < Ttypes
.Maximum_Alignment
2923 and then M
> Alignment
(Etype
(P
))
2928 -- Case of component clause present which may specify an
2929 -- unaligned position.
2931 if Present
(Component_Clause
(C
)) then
2933 -- Otherwise we can do a test to make sure that the actual
2934 -- start position in the record, and the length, are both
2935 -- consistent with the required alignment. If not, we know
2936 -- that we are unaligned.
2939 Align_In_Bits
: constant Nat
:= M
* System_Storage_Unit
;
2941 if Component_Bit_Offset
(C
) mod Align_In_Bits
/= 0
2942 or else Esize
(C
) mod Align_In_Bits
/= 0
2949 -- Otherwise, for a component reference, test prefix
2951 return Is_Possibly_Unaligned_Object
(P
);
2954 -- If not a component reference, must be aligned
2959 end Is_Possibly_Unaligned_Object
;
2961 ---------------------------------
2962 -- Is_Possibly_Unaligned_Slice --
2963 ---------------------------------
2965 function Is_Possibly_Unaligned_Slice
(N
: Node_Id
) return Boolean is
2967 -- ??? GCC3 will eventually handle strings with arbitrary alignments,
2968 -- but for now the following check must be disabled.
2970 -- if get_gcc_version >= 3 then
2974 -- For renaming case, go to renamed object
2976 if Is_Entity_Name
(N
)
2977 and then Is_Object
(Entity
(N
))
2978 and then Present
(Renamed_Object
(Entity
(N
)))
2980 return Is_Possibly_Unaligned_Slice
(Renamed_Object
(Entity
(N
)));
2983 -- The reference must be a slice
2985 if Nkind
(N
) /= N_Slice
then
2989 -- Always assume the worst for a nested record component with a
2990 -- component clause, which gigi/gcc does not appear to handle well.
2991 -- It is not clear why this special test is needed at all ???
2993 if Nkind
(Prefix
(N
)) = N_Selected_Component
2994 and then Nkind
(Prefix
(Prefix
(N
))) = N_Selected_Component
2996 Present
(Component_Clause
(Entity
(Selector_Name
(Prefix
(N
)))))
3001 -- We only need to worry if the target has strict alignment
3003 if not Target_Strict_Alignment
then
3007 -- If it is a slice, then look at the array type being sliced
3010 Sarr
: constant Node_Id
:= Prefix
(N
);
3011 -- Prefix of the slice, i.e. the array being sliced
3013 Styp
: constant Entity_Id
:= Etype
(Prefix
(N
));
3014 -- Type of the array being sliced
3020 -- The problems arise if the array object that is being sliced
3021 -- is a component of a record or array, and we cannot guarantee
3022 -- the alignment of the array within its containing object.
3024 -- To investigate this, we look at successive prefixes to see
3025 -- if we have a worrisome indexed or selected component.
3029 -- Case of array is part of an indexed component reference
3031 if Nkind
(Pref
) = N_Indexed_Component
then
3032 Ptyp
:= Etype
(Prefix
(Pref
));
3034 -- The only problematic case is when the array is packed,
3035 -- in which case we really know nothing about the alignment
3036 -- of individual components.
3038 if Is_Bit_Packed_Array
(Ptyp
) then
3042 -- Case of array is part of a selected component reference
3044 elsif Nkind
(Pref
) = N_Selected_Component
then
3045 Ptyp
:= Etype
(Prefix
(Pref
));
3047 -- We are definitely in trouble if the record in question
3048 -- has an alignment, and either we know this alignment is
3049 -- inconsistent with the alignment of the slice, or we
3050 -- don't know what the alignment of the slice should be.
3052 if Known_Alignment
(Ptyp
)
3053 and then (Unknown_Alignment
(Styp
)
3054 or else Alignment
(Styp
) > Alignment
(Ptyp
))
3059 -- We are in potential trouble if the record type is packed.
3060 -- We could special case when we know that the array is the
3061 -- first component, but that's not such a simple case ???
3063 if Is_Packed
(Ptyp
) then
3067 -- We are in trouble if there is a component clause, and
3068 -- either we do not know the alignment of the slice, or
3069 -- the alignment of the slice is inconsistent with the
3070 -- bit position specified by the component clause.
3073 Field
: constant Entity_Id
:= Entity
(Selector_Name
(Pref
));
3075 if Present
(Component_Clause
(Field
))
3077 (Unknown_Alignment
(Styp
)
3079 (Component_Bit_Offset
(Field
) mod
3080 (System_Storage_Unit
* Alignment
(Styp
))) /= 0)
3086 -- For cases other than selected or indexed components we
3087 -- know we are OK, since no issues arise over alignment.
3093 -- We processed an indexed component or selected component
3094 -- reference that looked safe, so keep checking prefixes.
3096 Pref
:= Prefix
(Pref
);
3099 end Is_Possibly_Unaligned_Slice
;
3101 --------------------------------
3102 -- Is_Ref_To_Bit_Packed_Array --
3103 --------------------------------
3105 function Is_Ref_To_Bit_Packed_Array
(N
: Node_Id
) return Boolean is
3110 if Is_Entity_Name
(N
)
3111 and then Is_Object
(Entity
(N
))
3112 and then Present
(Renamed_Object
(Entity
(N
)))
3114 return Is_Ref_To_Bit_Packed_Array
(Renamed_Object
(Entity
(N
)));
3117 if Nkind
(N
) = N_Indexed_Component
3119 Nkind
(N
) = N_Selected_Component
3121 if Is_Bit_Packed_Array
(Etype
(Prefix
(N
))) then
3124 Result
:= Is_Ref_To_Bit_Packed_Array
(Prefix
(N
));
3127 if Result
and then Nkind
(N
) = N_Indexed_Component
then
3128 Expr
:= First
(Expressions
(N
));
3129 while Present
(Expr
) loop
3130 Force_Evaluation
(Expr
);
3140 end Is_Ref_To_Bit_Packed_Array
;
3142 --------------------------------
3143 -- Is_Ref_To_Bit_Packed_Slice --
3144 --------------------------------
3146 function Is_Ref_To_Bit_Packed_Slice
(N
: Node_Id
) return Boolean is
3148 if Nkind
(N
) = N_Type_Conversion
then
3149 return Is_Ref_To_Bit_Packed_Slice
(Expression
(N
));
3151 elsif Is_Entity_Name
(N
)
3152 and then Is_Object
(Entity
(N
))
3153 and then Present
(Renamed_Object
(Entity
(N
)))
3155 return Is_Ref_To_Bit_Packed_Slice
(Renamed_Object
(Entity
(N
)));
3157 elsif Nkind
(N
) = N_Slice
3158 and then Is_Bit_Packed_Array
(Etype
(Prefix
(N
)))
3162 elsif Nkind
(N
) = N_Indexed_Component
3164 Nkind
(N
) = N_Selected_Component
3166 return Is_Ref_To_Bit_Packed_Slice
(Prefix
(N
));
3171 end Is_Ref_To_Bit_Packed_Slice
;
3173 -----------------------
3174 -- Is_Renamed_Object --
3175 -----------------------
3177 function Is_Renamed_Object
(N
: Node_Id
) return Boolean is
3178 Pnod
: constant Node_Id
:= Parent
(N
);
3179 Kind
: constant Node_Kind
:= Nkind
(Pnod
);
3182 if Kind
= N_Object_Renaming_Declaration
then
3185 elsif Kind
= N_Indexed_Component
3186 or else Kind
= N_Selected_Component
3188 return Is_Renamed_Object
(Pnod
);
3193 end Is_Renamed_Object
;
3195 ----------------------------
3196 -- Is_Untagged_Derivation --
3197 ----------------------------
3199 function Is_Untagged_Derivation
(T
: Entity_Id
) return Boolean is
3201 return (not Is_Tagged_Type
(T
) and then Is_Derived_Type
(T
))
3203 (Is_Private_Type
(T
) and then Present
(Full_View
(T
))
3204 and then not Is_Tagged_Type
(Full_View
(T
))
3205 and then Is_Derived_Type
(Full_View
(T
))
3206 and then Etype
(Full_View
(T
)) /= T
);
3208 end Is_Untagged_Derivation
;
3210 --------------------
3211 -- Kill_Dead_Code --
3212 --------------------
3214 procedure Kill_Dead_Code
(N
: Node_Id
) is
3217 Remove_Warning_Messages
(N
);
3219 -- Recurse into block statements and bodies to process declarations
3222 if Nkind
(N
) = N_Block_Statement
3223 or else Nkind
(N
) = N_Subprogram_Body
3224 or else Nkind
(N
) = N_Package_Body
3226 Kill_Dead_Code
(Declarations
(N
));
3227 Kill_Dead_Code
(Statements
(Handled_Statement_Sequence
(N
)));
3229 if Nkind
(N
) = N_Subprogram_Body
then
3230 Set_Is_Eliminated
(Defining_Entity
(N
));
3233 elsif Nkind
(N
) = N_Package_Declaration
then
3234 Kill_Dead_Code
(Visible_Declarations
(Specification
(N
)));
3235 Kill_Dead_Code
(Private_Declarations
(Specification
(N
)));
3238 E
: Entity_Id
:= First_Entity
(Defining_Entity
(N
));
3240 while Present
(E
) loop
3241 if Ekind
(E
) = E_Operator
then
3242 Set_Is_Eliminated
(E
);
3249 -- Recurse into composite statement to kill individual statements,
3250 -- in particular instantiations.
3252 elsif Nkind
(N
) = N_If_Statement
then
3253 Kill_Dead_Code
(Then_Statements
(N
));
3254 Kill_Dead_Code
(Elsif_Parts
(N
));
3255 Kill_Dead_Code
(Else_Statements
(N
));
3257 elsif Nkind
(N
) = N_Loop_Statement
then
3258 Kill_Dead_Code
(Statements
(N
));
3260 elsif Nkind
(N
) = N_Case_Statement
then
3264 Alt
:= First
(Alternatives
(N
));
3265 while Present
(Alt
) loop
3266 Kill_Dead_Code
(Statements
(Alt
));
3271 elsif Nkind
(N
) = N_Case_Statement_Alternative
then
3272 Kill_Dead_Code
(Statements
(N
));
3274 -- Deal with dead instances caused by deleting instantiations
3276 elsif Nkind
(N
) in N_Generic_Instantiation
then
3277 Remove_Dead_Instance
(N
);
3284 -- Case where argument is a list of nodes to be killed
3286 procedure Kill_Dead_Code
(L
: List_Id
) is
3290 if Is_Non_Empty_List
(L
) then
3292 N
:= Remove_Head
(L
);
3299 ------------------------
3300 -- Known_Non_Negative --
3301 ------------------------
3303 function Known_Non_Negative
(Opnd
: Node_Id
) return Boolean is
3305 if Is_OK_Static_Expression
(Opnd
)
3306 and then Expr_Value
(Opnd
) >= 0
3312 Lo
: constant Node_Id
:= Type_Low_Bound
(Etype
(Opnd
));
3316 Is_OK_Static_Expression
(Lo
) and then Expr_Value
(Lo
) >= 0;
3319 end Known_Non_Negative
;
3321 --------------------
3322 -- Known_Non_Null --
3323 --------------------
3325 function Known_Non_Null
(N
: Node_Id
) return Boolean is
3327 pragma Assert
(Is_Access_Type
(Underlying_Type
(Etype
(N
))));
3329 -- Case of entity for which Is_Known_Non_Null is True
3331 if Is_Entity_Name
(N
) and then Is_Known_Non_Null
(Entity
(N
)) then
3333 -- If the entity is aliased or volatile, then we decide that
3334 -- we don't know it is really non-null even if the sequential
3335 -- flow indicates that it is, since such variables can be
3336 -- changed without us noticing.
3338 if Is_Aliased
(Entity
(N
))
3339 or else Treat_As_Volatile
(Entity
(N
))
3343 -- For all other cases, the flag is decisive
3349 -- True if access attribute
3351 elsif Nkind
(N
) = N_Attribute_Reference
3352 and then (Attribute_Name
(N
) = Name_Access
3354 Attribute_Name
(N
) = Name_Unchecked_Access
3356 Attribute_Name
(N
) = Name_Unrestricted_Access
)
3360 -- True if allocator
3362 elsif Nkind
(N
) = N_Allocator
then
3365 -- For a conversion, true if expression is known non-null
3367 elsif Nkind
(N
) = N_Type_Conversion
then
3368 return Known_Non_Null
(Expression
(N
));
3370 -- One more case is when Current_Value references a condition
3371 -- that ensures a non-null value.
3373 elsif Is_Entity_Name
(N
) then
3379 Get_Current_Value_Condition
(N
, Op
, Val
);
3380 return Op
= N_Op_Ne
and then Nkind
(Val
) = N_Null
;
3383 -- Above are all cases where the value could be determined to be
3384 -- non-null. In all other cases, we don't know, so return False.
3391 -----------------------------
3392 -- Make_CW_Equivalent_Type --
3393 -----------------------------
3395 -- Create a record type used as an equivalent of any member
3396 -- of the class which takes its size from exp.
3398 -- Generate the following code:
3400 -- type Equiv_T is record
3401 -- _parent : T (List of discriminant constaints taken from Exp);
3402 -- Ext__50 : Storage_Array (1 .. (Exp'size - Typ'object_size)/8);
3405 -- ??? Note that this type does not guarantee same alignment as all
3408 function Make_CW_Equivalent_Type
3410 E
: Node_Id
) return Entity_Id
3412 Loc
: constant Source_Ptr
:= Sloc
(E
);
3413 Root_Typ
: constant Entity_Id
:= Root_Type
(T
);
3414 List_Def
: constant List_Id
:= Empty_List
;
3415 Equiv_Type
: Entity_Id
;
3416 Range_Type
: Entity_Id
;
3417 Str_Type
: Entity_Id
;
3418 Constr_Root
: Entity_Id
;
3422 if not Has_Discriminants
(Root_Typ
) then
3423 Constr_Root
:= Root_Typ
;
3426 Make_Defining_Identifier
(Loc
, New_Internal_Name
('R'));
3428 -- subtype cstr__n is T (List of discr constraints taken from Exp)
3430 Append_To
(List_Def
,
3431 Make_Subtype_Declaration
(Loc
,
3432 Defining_Identifier
=> Constr_Root
,
3433 Subtype_Indication
=>
3434 Make_Subtype_From_Expr
(E
, Root_Typ
)));
3437 -- subtype rg__xx is Storage_Offset range
3438 -- (Expr'size - typ'size) / Storage_Unit
3440 Range_Type
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('G'));
3443 Make_Op_Subtract
(Loc
,
3445 Make_Attribute_Reference
(Loc
,
3447 OK_Convert_To
(T
, Duplicate_Subexpr_No_Checks
(E
)),
3448 Attribute_Name
=> Name_Size
),
3450 Make_Attribute_Reference
(Loc
,
3451 Prefix
=> New_Reference_To
(Constr_Root
, Loc
),
3452 Attribute_Name
=> Name_Object_Size
));
3454 Set_Paren_Count
(Sizexpr
, 1);
3456 Append_To
(List_Def
,
3457 Make_Subtype_Declaration
(Loc
,
3458 Defining_Identifier
=> Range_Type
,
3459 Subtype_Indication
=>
3460 Make_Subtype_Indication
(Loc
,
3461 Subtype_Mark
=> New_Reference_To
(RTE
(RE_Storage_Offset
), Loc
),
3462 Constraint
=> Make_Range_Constraint
(Loc
,
3465 Low_Bound
=> Make_Integer_Literal
(Loc
, 1),
3467 Make_Op_Divide
(Loc
,
3468 Left_Opnd
=> Sizexpr
,
3469 Right_Opnd
=> Make_Integer_Literal
(Loc
,
3470 Intval
=> System_Storage_Unit
)))))));
3472 -- subtype str__nn is Storage_Array (rg__x);
3474 Str_Type
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('S'));
3475 Append_To
(List_Def
,
3476 Make_Subtype_Declaration
(Loc
,
3477 Defining_Identifier
=> Str_Type
,
3478 Subtype_Indication
=>
3479 Make_Subtype_Indication
(Loc
,
3480 Subtype_Mark
=> New_Reference_To
(RTE
(RE_Storage_Array
), Loc
),
3482 Make_Index_Or_Discriminant_Constraint
(Loc
,
3484 New_List
(New_Reference_To
(Range_Type
, Loc
))))));
3486 -- type Equiv_T is record
3491 Equiv_Type
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('T'));
3493 -- When the target requires front-end layout, it's necessary to allow
3494 -- the equivalent type to be frozen so that layout can occur (when the
3495 -- associated class-wide subtype is frozen, the equivalent type will
3496 -- be frozen, see freeze.adb). For other targets, Gigi wants to have
3497 -- the equivalent type marked as frozen and deals with this type itself.
3498 -- In the Gigi case this will also avoid the generation of an init
3499 -- procedure for the type.
3501 if not Frontend_Layout_On_Target
then
3502 Set_Is_Frozen
(Equiv_Type
);
3505 Set_Ekind
(Equiv_Type
, E_Record_Type
);
3506 Set_Parent_Subtype
(Equiv_Type
, Constr_Root
);
3508 Append_To
(List_Def
,
3509 Make_Full_Type_Declaration
(Loc
,
3510 Defining_Identifier
=> Equiv_Type
,
3513 Make_Record_Definition
(Loc
,
3514 Component_List
=> Make_Component_List
(Loc
,
3515 Component_Items
=> New_List
(
3516 Make_Component_Declaration
(Loc
,
3517 Defining_Identifier
=>
3518 Make_Defining_Identifier
(Loc
, Name_uParent
),
3519 Component_Definition
=>
3520 Make_Component_Definition
(Loc
,
3521 Aliased_Present
=> False,
3522 Subtype_Indication
=>
3523 New_Reference_To
(Constr_Root
, Loc
))),
3525 Make_Component_Declaration
(Loc
,
3526 Defining_Identifier
=>
3527 Make_Defining_Identifier
(Loc
,
3528 Chars
=> New_Internal_Name
('C')),
3529 Component_Definition
=>
3530 Make_Component_Definition
(Loc
,
3531 Aliased_Present
=> False,
3532 Subtype_Indication
=>
3533 New_Reference_To
(Str_Type
, Loc
)))),
3535 Variant_Part
=> Empty
))));
3537 Insert_Actions
(E
, List_Def
);
3539 end Make_CW_Equivalent_Type
;
3541 ------------------------
3542 -- Make_Literal_Range --
3543 ------------------------
3545 function Make_Literal_Range
3547 Literal_Typ
: Entity_Id
) return Node_Id
3549 Lo
: constant Node_Id
:=
3550 New_Copy_Tree
(String_Literal_Low_Bound
(Literal_Typ
));
3553 Set_Analyzed
(Lo
, False);
3560 Make_Op_Subtract
(Loc
,
3563 Left_Opnd
=> New_Copy_Tree
(Lo
),
3565 Make_Integer_Literal
(Loc
,
3566 String_Literal_Length
(Literal_Typ
))),
3567 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1)));
3568 end Make_Literal_Range
;
3570 ----------------------------
3571 -- Make_Subtype_From_Expr --
3572 ----------------------------
3574 -- 1. If Expr is an uncontrained array expression, creates
3575 -- Unc_Type(Expr'first(1)..Expr'Last(1),..., Expr'first(n)..Expr'last(n))
3577 -- 2. If Expr is a unconstrained discriminated type expression, creates
3578 -- Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
3580 -- 3. If Expr is class-wide, creates an implicit class wide subtype
3582 function Make_Subtype_From_Expr
3584 Unc_Typ
: Entity_Id
) return Node_Id
3586 Loc
: constant Source_Ptr
:= Sloc
(E
);
3587 List_Constr
: constant List_Id
:= New_List
;
3590 Full_Subtyp
: Entity_Id
;
3591 Priv_Subtyp
: Entity_Id
;
3596 if Is_Private_Type
(Unc_Typ
)
3597 and then Has_Unknown_Discriminants
(Unc_Typ
)
3599 -- Prepare the subtype completion, Go to base type to
3600 -- find underlying type, because the type may be a generic
3601 -- actual or an explicit subtype.
3603 Utyp
:= Underlying_Type
(Base_Type
(Unc_Typ
));
3604 Full_Subtyp
:= Make_Defining_Identifier
(Loc
,
3605 New_Internal_Name
('C'));
3607 Unchecked_Convert_To
3608 (Utyp
, Duplicate_Subexpr_No_Checks
(E
));
3609 Set_Parent
(Full_Exp
, Parent
(E
));
3612 Make_Defining_Identifier
(Loc
, New_Internal_Name
('P'));
3615 Make_Subtype_Declaration
(Loc
,
3616 Defining_Identifier
=> Full_Subtyp
,
3617 Subtype_Indication
=> Make_Subtype_From_Expr
(Full_Exp
, Utyp
)));
3619 -- Define the dummy private subtype
3621 Set_Ekind
(Priv_Subtyp
, Subtype_Kind
(Ekind
(Unc_Typ
)));
3622 Set_Etype
(Priv_Subtyp
, Base_Type
(Unc_Typ
));
3623 Set_Scope
(Priv_Subtyp
, Full_Subtyp
);
3624 Set_Is_Constrained
(Priv_Subtyp
);
3625 Set_Is_Tagged_Type
(Priv_Subtyp
, Is_Tagged_Type
(Unc_Typ
));
3626 Set_Is_Itype
(Priv_Subtyp
);
3627 Set_Associated_Node_For_Itype
(Priv_Subtyp
, E
);
3629 if Is_Tagged_Type
(Priv_Subtyp
) then
3631 (Base_Type
(Priv_Subtyp
), Class_Wide_Type
(Unc_Typ
));
3632 Set_Primitive_Operations
(Priv_Subtyp
,
3633 Primitive_Operations
(Unc_Typ
));
3636 Set_Full_View
(Priv_Subtyp
, Full_Subtyp
);
3638 return New_Reference_To
(Priv_Subtyp
, Loc
);
3640 elsif Is_Array_Type
(Unc_Typ
) then
3641 for J
in 1 .. Number_Dimensions
(Unc_Typ
) loop
3642 Append_To
(List_Constr
,
3645 Make_Attribute_Reference
(Loc
,
3646 Prefix
=> Duplicate_Subexpr_No_Checks
(E
),
3647 Attribute_Name
=> Name_First
,
3648 Expressions
=> New_List
(
3649 Make_Integer_Literal
(Loc
, J
))),
3652 Make_Attribute_Reference
(Loc
,
3653 Prefix
=> Duplicate_Subexpr_No_Checks
(E
),
3654 Attribute_Name
=> Name_Last
,
3655 Expressions
=> New_List
(
3656 Make_Integer_Literal
(Loc
, J
)))));
3659 elsif Is_Class_Wide_Type
(Unc_Typ
) then
3661 CW_Subtype
: Entity_Id
;
3662 EQ_Typ
: Entity_Id
:= Empty
;
3665 -- A class-wide equivalent type is not needed when Java_VM
3666 -- because the JVM back end handles the class-wide object
3667 -- initialization itself (and doesn't need or want the
3668 -- additional intermediate type to handle the assignment).
3670 if Expander_Active
and then not Java_VM
then
3671 EQ_Typ
:= Make_CW_Equivalent_Type
(Unc_Typ
, E
);
3674 CW_Subtype
:= New_Class_Wide_Subtype
(Unc_Typ
, E
);
3675 Set_Equivalent_Type
(CW_Subtype
, EQ_Typ
);
3677 if Present
(EQ_Typ
) then
3678 Set_Is_Class_Wide_Equivalent_Type
(EQ_Typ
);
3681 Set_Cloned_Subtype
(CW_Subtype
, Base_Type
(Unc_Typ
));
3683 return New_Occurrence_Of
(CW_Subtype
, Loc
);
3686 -- Indefinite record type with discriminants
3689 D
:= First_Discriminant
(Unc_Typ
);
3690 while Present
(D
) loop
3691 Append_To
(List_Constr
,
3692 Make_Selected_Component
(Loc
,
3693 Prefix
=> Duplicate_Subexpr_No_Checks
(E
),
3694 Selector_Name
=> New_Reference_To
(D
, Loc
)));
3696 Next_Discriminant
(D
);
3701 Make_Subtype_Indication
(Loc
,
3702 Subtype_Mark
=> New_Reference_To
(Unc_Typ
, Loc
),
3704 Make_Index_Or_Discriminant_Constraint
(Loc
,
3705 Constraints
=> List_Constr
));
3706 end Make_Subtype_From_Expr
;
3708 -----------------------------
3709 -- May_Generate_Large_Temp --
3710 -----------------------------
3712 -- At the current time, the only types that we return False for (i.e.
3713 -- where we decide we know they cannot generate large temps) are ones
3714 -- where we know the size is 256 bits or less at compile time, and we
3715 -- are still not doing a thorough job on arrays and records ???
3717 function May_Generate_Large_Temp
(Typ
: Entity_Id
) return Boolean is
3719 if not Size_Known_At_Compile_Time
(Typ
) then
3722 elsif Esize
(Typ
) /= 0 and then Esize
(Typ
) <= 256 then
3725 elsif Is_Array_Type
(Typ
)
3726 and then Present
(Packed_Array_Type
(Typ
))
3728 return May_Generate_Large_Temp
(Packed_Array_Type
(Typ
));
3730 -- We could do more here to find other small types ???
3735 end May_Generate_Large_Temp
;
3737 ----------------------------
3738 -- New_Class_Wide_Subtype --
3739 ----------------------------
3741 function New_Class_Wide_Subtype
3742 (CW_Typ
: Entity_Id
;
3743 N
: Node_Id
) return Entity_Id
3745 Res
: constant Entity_Id
:= Create_Itype
(E_Void
, N
);
3746 Res_Name
: constant Name_Id
:= Chars
(Res
);
3747 Res_Scope
: constant Entity_Id
:= Scope
(Res
);
3750 Copy_Node
(CW_Typ
, Res
);
3751 Set_Sloc
(Res
, Sloc
(N
));
3753 Set_Associated_Node_For_Itype
(Res
, N
);
3754 Set_Is_Public
(Res
, False); -- By default, may be changed below.
3755 Set_Public_Status
(Res
);
3756 Set_Chars
(Res
, Res_Name
);
3757 Set_Scope
(Res
, Res_Scope
);
3758 Set_Ekind
(Res
, E_Class_Wide_Subtype
);
3759 Set_Next_Entity
(Res
, Empty
);
3760 Set_Etype
(Res
, Base_Type
(CW_Typ
));
3762 -- For targets where front-end layout is required, reset the Is_Frozen
3763 -- status of the subtype to False (it can be implicitly set to true
3764 -- from the copy of the class-wide type). For other targets, Gigi
3765 -- doesn't want the class-wide subtype to go through the freezing
3766 -- process (though it's unclear why that causes problems and it would
3767 -- be nice to allow freezing to occur normally for all targets ???).
3769 if Frontend_Layout_On_Target
then
3770 Set_Is_Frozen
(Res
, False);
3773 Set_Freeze_Node
(Res
, Empty
);
3775 end New_Class_Wide_Subtype
;
3777 -------------------------
3778 -- Remove_Side_Effects --
3779 -------------------------
3781 procedure Remove_Side_Effects
3783 Name_Req
: Boolean := False;
3784 Variable_Ref
: Boolean := False)
3786 Loc
: constant Source_Ptr
:= Sloc
(Exp
);
3787 Exp_Type
: constant Entity_Id
:= Etype
(Exp
);
3788 Svg_Suppress
: constant Suppress_Array
:= Scope_Suppress
;
3790 Ref_Type
: Entity_Id
;
3792 Ptr_Typ_Decl
: Node_Id
;
3796 function Side_Effect_Free
(N
: Node_Id
) return Boolean;
3797 -- Determines if the tree N represents an expression that is known
3798 -- not to have side effects, and for which no processing is required.
3800 function Side_Effect_Free
(L
: List_Id
) return Boolean;
3801 -- Determines if all elements of the list L are side effect free
3803 function Safe_Prefixed_Reference
(N
: Node_Id
) return Boolean;
3804 -- The argument N is a construct where the Prefix is dereferenced
3805 -- if it is a an access type and the result is a variable. The call
3806 -- returns True if the construct is side effect free (not considering
3807 -- side effects in other than the prefix which are to be tested by the
3810 function Within_In_Parameter
(N
: Node_Id
) return Boolean;
3811 -- Determines if N is a subcomponent of a composite in-parameter.
3812 -- If so, N is not side-effect free when the actual is global and
3813 -- modifiable indirectly from within a subprogram, because it may
3814 -- be passed by reference. The front-end must be conservative here
3815 -- and assume that this may happen with any array or record type.
3816 -- On the other hand, we cannot create temporaries for all expressions
3817 -- for which this condition is true, for various reasons that might
3818 -- require clearing up ??? For example, descriminant references that
3819 -- appear out of place, or spurious type errors with class-wide
3820 -- expressions. As a result, we limit the transformation to loop
3821 -- bounds, which is so far the only case that requires it.
3823 -----------------------------
3824 -- Safe_Prefixed_Reference --
3825 -----------------------------
3827 function Safe_Prefixed_Reference
(N
: Node_Id
) return Boolean is
3829 -- If prefix is not side effect free, definitely not safe
3831 if not Side_Effect_Free
(Prefix
(N
)) then
3834 -- If the prefix is of an access type that is not access-to-constant,
3835 -- then this construct is a variable reference, which means it is to
3836 -- be considered to have side effects if Variable_Ref is set True
3837 -- Exception is an access to an entity that is a constant or an
3838 -- in-parameter which does not come from source, and is the result
3839 -- of a previous removal of side-effects.
3841 elsif Is_Access_Type
(Etype
(Prefix
(N
)))
3842 and then not Is_Access_Constant
(Etype
(Prefix
(N
)))
3843 and then Variable_Ref
3845 if not Is_Entity_Name
(Prefix
(N
)) then
3848 return Ekind
(Entity
(Prefix
(N
))) = E_Constant
3849 or else Ekind
(Entity
(Prefix
(N
))) = E_In_Parameter
;
3852 -- The following test is the simplest way of solving a complex
3853 -- problem uncovered by BB08-010: Side effect on loop bound that
3854 -- is a subcomponent of a global variable:
3855 -- If a loop bound is a subcomponent of a global variable, a
3856 -- modification of that variable within the loop may incorrectly
3857 -- affect the execution of the loop.
3860 (Nkind
(Parent
(Parent
(N
))) /= N_Loop_Parameter_Specification
3861 or else not Within_In_Parameter
(Prefix
(N
)))
3865 -- All other cases are side effect free
3870 end Safe_Prefixed_Reference
;
3872 ----------------------
3873 -- Side_Effect_Free --
3874 ----------------------
3876 function Side_Effect_Free
(N
: Node_Id
) return Boolean is
3878 -- Note on checks that could raise Constraint_Error. Strictly, if
3879 -- we take advantage of 11.6, these checks do not count as side
3880 -- effects. However, we would just as soon consider that they are
3881 -- side effects, since the backend CSE does not work very well on
3882 -- expressions which can raise Constraint_Error. On the other
3883 -- hand, if we do not consider them to be side effect free, then
3884 -- we get some awkward expansions in -gnato mode, resulting in
3885 -- code insertions at a point where we do not have a clear model
3886 -- for performing the insertions. See 4908-002/comment for details.
3888 -- Special handling for entity names
3890 if Is_Entity_Name
(N
) then
3892 -- If the entity is a constant, it is definitely side effect
3893 -- free. Note that the test of Is_Variable (N) below might
3894 -- be expected to catch this case, but it does not, because
3895 -- this test goes to the original tree, and we may have
3896 -- already rewritten a variable node with a constant as
3897 -- a result of an earlier Force_Evaluation call.
3899 if Ekind
(Entity
(N
)) = E_Constant
3900 or else Ekind
(Entity
(N
)) = E_In_Parameter
3904 -- Functions are not side effect free
3906 elsif Ekind
(Entity
(N
)) = E_Function
then
3909 -- Variables are considered to be a side effect if Variable_Ref
3910 -- is set or if we have a volatile variable and Name_Req is off.
3911 -- If Name_Req is True then we can't help returning a name which
3912 -- effectively allows multiple references in any case.
3914 elsif Is_Variable
(N
) then
3915 return not Variable_Ref
3916 and then (not Treat_As_Volatile
(Entity
(N
))
3919 -- Any other entity (e.g. a subtype name) is definitely side
3926 -- A value known at compile time is always side effect free
3928 elsif Compile_Time_Known_Value
(N
) then
3932 -- For other than entity names and compile time known values,
3933 -- check the node kind for special processing.
3937 -- An attribute reference is side effect free if its expressions
3938 -- are side effect free and its prefix is side effect free or
3939 -- is an entity reference.
3941 -- Is this right? what about x'first where x is a variable???
3943 when N_Attribute_Reference
=>
3944 return Side_Effect_Free
(Expressions
(N
))
3945 and then (Is_Entity_Name
(Prefix
(N
))
3946 or else Side_Effect_Free
(Prefix
(N
)));
3948 -- A binary operator is side effect free if and both operands
3949 -- are side effect free. For this purpose binary operators
3950 -- include membership tests and short circuit forms
3957 return Side_Effect_Free
(Left_Opnd
(N
))
3958 and then Side_Effect_Free
(Right_Opnd
(N
));
3960 -- An explicit dereference is side effect free only if it is
3961 -- a side effect free prefixed reference.
3963 when N_Explicit_Dereference
=>
3964 return Safe_Prefixed_Reference
(N
);
3966 -- A call to _rep_to_pos is side effect free, since we generate
3967 -- this pure function call ourselves. Moreover it is critically
3968 -- important to make this exception, since otherwise we can
3969 -- have discriminants in array components which don't look
3970 -- side effect free in the case of an array whose index type
3971 -- is an enumeration type with an enumeration rep clause.
3973 -- All other function calls are not side effect free
3975 when N_Function_Call
=>
3976 return Nkind
(Name
(N
)) = N_Identifier
3977 and then Is_TSS
(Name
(N
), TSS_Rep_To_Pos
)
3979 Side_Effect_Free
(First
(Parameter_Associations
(N
)));
3981 -- An indexed component is side effect free if it is a side
3982 -- effect free prefixed reference and all the indexing
3983 -- expressions are side effect free.
3985 when N_Indexed_Component
=>
3986 return Side_Effect_Free
(Expressions
(N
))
3987 and then Safe_Prefixed_Reference
(N
);
3989 -- A type qualification is side effect free if the expression
3990 -- is side effect free.
3992 when N_Qualified_Expression
=>
3993 return Side_Effect_Free
(Expression
(N
));
3995 -- A selected component is side effect free only if it is a
3996 -- side effect free prefixed reference.
3998 when N_Selected_Component
=>
3999 return Safe_Prefixed_Reference
(N
);
4001 -- A range is side effect free if the bounds are side effect free
4004 return Side_Effect_Free
(Low_Bound
(N
))
4005 and then Side_Effect_Free
(High_Bound
(N
));
4007 -- A slice is side effect free if it is a side effect free
4008 -- prefixed reference and the bounds are side effect free.
4011 return Side_Effect_Free
(Discrete_Range
(N
))
4012 and then Safe_Prefixed_Reference
(N
);
4014 -- A type conversion is side effect free if the expression
4015 -- to be converted is side effect free.
4017 when N_Type_Conversion
=>
4018 return Side_Effect_Free
(Expression
(N
));
4020 -- A unary operator is side effect free if the operand
4021 -- is side effect free.
4024 return Side_Effect_Free
(Right_Opnd
(N
));
4026 -- An unchecked type conversion is side effect free only if it
4027 -- is safe and its argument is side effect free.
4029 when N_Unchecked_Type_Conversion
=>
4030 return Safe_Unchecked_Type_Conversion
(N
)
4031 and then Side_Effect_Free
(Expression
(N
));
4033 -- An unchecked expression is side effect free if its expression
4034 -- is side effect free.
4036 when N_Unchecked_Expression
=>
4037 return Side_Effect_Free
(Expression
(N
));
4039 -- A literal is side effect free
4041 when N_Character_Literal |
4047 -- We consider that anything else has side effects. This is a bit
4048 -- crude, but we are pretty close for most common cases, and we
4049 -- are certainly correct (i.e. we never return True when the
4050 -- answer should be False).
4055 end Side_Effect_Free
;
4057 -- A list is side effect free if all elements of the list are
4058 -- side effect free.
4060 function Side_Effect_Free
(L
: List_Id
) return Boolean is
4064 if L
= No_List
or else L
= Error_List
then
4069 while Present
(N
) loop
4070 if not Side_Effect_Free
(N
) then
4079 end Side_Effect_Free
;
4081 -------------------------
4082 -- Within_In_Parameter --
4083 -------------------------
4085 function Within_In_Parameter
(N
: Node_Id
) return Boolean is
4087 if not Comes_From_Source
(N
) then
4090 elsif Is_Entity_Name
(N
) then
4092 Ekind
(Entity
(N
)) = E_In_Parameter
;
4094 elsif Nkind
(N
) = N_Indexed_Component
4095 or else Nkind
(N
) = N_Selected_Component
4097 return Within_In_Parameter
(Prefix
(N
));
4102 end Within_In_Parameter
;
4104 -- Start of processing for Remove_Side_Effects
4107 -- If we are side effect free already or expansion is disabled,
4108 -- there is nothing to do.
4110 if Side_Effect_Free
(Exp
) or else not Expander_Active
then
4114 -- All this must not have any checks
4116 Scope_Suppress
:= (others => True);
4118 -- If it is a scalar type and we need to capture the value, just
4119 -- make a copy. Likewise for a function call. And if we have a
4120 -- volatile variable and Nam_Req is not set (see comments above
4121 -- for Side_Effect_Free).
4123 if Is_Elementary_Type
(Exp_Type
)
4124 and then (Variable_Ref
4125 or else Nkind
(Exp
) = N_Function_Call
4126 or else (not Name_Req
4127 and then Is_Entity_Name
(Exp
)
4128 and then Treat_As_Volatile
(Entity
(Exp
))))
4131 Def_Id
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('R'));
4132 Set_Etype
(Def_Id
, Exp_Type
);
4133 Res
:= New_Reference_To
(Def_Id
, Loc
);
4136 Make_Object_Declaration
(Loc
,
4137 Defining_Identifier
=> Def_Id
,
4138 Object_Definition
=> New_Reference_To
(Exp_Type
, Loc
),
4139 Constant_Present
=> True,
4140 Expression
=> Relocate_Node
(Exp
));
4142 Set_Assignment_OK
(E
);
4143 Insert_Action
(Exp
, E
);
4145 -- If the expression has the form v.all then we can just capture
4146 -- the pointer, and then do an explicit dereference on the result.
4148 elsif Nkind
(Exp
) = N_Explicit_Dereference
then
4150 Make_Defining_Identifier
(Loc
, New_Internal_Name
('R'));
4152 Make_Explicit_Dereference
(Loc
, New_Reference_To
(Def_Id
, Loc
));
4155 Make_Object_Declaration
(Loc
,
4156 Defining_Identifier
=> Def_Id
,
4157 Object_Definition
=>
4158 New_Reference_To
(Etype
(Prefix
(Exp
)), Loc
),
4159 Constant_Present
=> True,
4160 Expression
=> Relocate_Node
(Prefix
(Exp
))));
4162 -- Similar processing for an unchecked conversion of an expression
4163 -- of the form v.all, where we want the same kind of treatment.
4165 elsif Nkind
(Exp
) = N_Unchecked_Type_Conversion
4166 and then Nkind
(Expression
(Exp
)) = N_Explicit_Dereference
4168 Remove_Side_Effects
(Expression
(Exp
), Name_Req
, Variable_Ref
);
4169 Scope_Suppress
:= Svg_Suppress
;
4172 -- If this is a type conversion, leave the type conversion and remove
4173 -- the side effects in the expression. This is important in several
4174 -- circumstances: for change of representations, and also when this
4175 -- is a view conversion to a smaller object, where gigi can end up
4176 -- creating its own temporary of the wrong size.
4178 -- ??? this transformation is inhibited for elementary types that are
4179 -- not involved in a change of representation because it causes
4180 -- regressions that are not fully understood yet.
4182 elsif Nkind
(Exp
) = N_Type_Conversion
4183 and then (not Is_Elementary_Type
(Underlying_Type
(Exp_Type
))
4184 or else Nkind
(Parent
(Exp
)) = N_Assignment_Statement
)
4186 Remove_Side_Effects
(Expression
(Exp
), Name_Req
, Variable_Ref
);
4187 Scope_Suppress
:= Svg_Suppress
;
4190 -- If this is an unchecked conversion that Gigi can't handle, make
4191 -- a copy or a use a renaming to capture the value.
4193 elsif Nkind
(Exp
) = N_Unchecked_Type_Conversion
4194 and then not Safe_Unchecked_Type_Conversion
(Exp
)
4196 if Controlled_Type
(Etype
(Exp
)) then
4198 -- Use a renaming to capture the expression, rather than create
4199 -- a controlled temporary.
4201 Def_Id
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('R'));
4202 Res
:= New_Reference_To
(Def_Id
, Loc
);
4205 Make_Object_Renaming_Declaration
(Loc
,
4206 Defining_Identifier
=> Def_Id
,
4207 Subtype_Mark
=> New_Reference_To
(Exp_Type
, Loc
),
4208 Name
=> Relocate_Node
(Exp
)));
4211 Def_Id
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('R'));
4212 Set_Etype
(Def_Id
, Exp_Type
);
4213 Res
:= New_Reference_To
(Def_Id
, Loc
);
4216 Make_Object_Declaration
(Loc
,
4217 Defining_Identifier
=> Def_Id
,
4218 Object_Definition
=> New_Reference_To
(Exp_Type
, Loc
),
4219 Constant_Present
=> not Is_Variable
(Exp
),
4220 Expression
=> Relocate_Node
(Exp
));
4222 Set_Assignment_OK
(E
);
4223 Insert_Action
(Exp
, E
);
4226 -- For expressions that denote objects, we can use a renaming scheme.
4227 -- We skip using this if we have a volatile variable and we do not
4228 -- have Nam_Req set true (see comments above for Side_Effect_Free).
4230 elsif Is_Object_Reference
(Exp
)
4231 and then Nkind
(Exp
) /= N_Function_Call
4233 or else not Is_Entity_Name
(Exp
)
4234 or else not Treat_As_Volatile
(Entity
(Exp
)))
4236 Def_Id
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('R'));
4238 if Nkind
(Exp
) = N_Selected_Component
4239 and then Nkind
(Prefix
(Exp
)) = N_Function_Call
4240 and then Is_Array_Type
(Etype
(Exp
))
4242 -- Avoid generating a variable-sized temporary, by generating
4243 -- the renaming declaration just for the function call. The
4244 -- transformation could be refined to apply only when the array
4245 -- component is constrained by a discriminant???
4248 Make_Selected_Component
(Loc
,
4249 Prefix
=> New_Occurrence_Of
(Def_Id
, Loc
),
4250 Selector_Name
=> Selector_Name
(Exp
));
4253 Make_Object_Renaming_Declaration
(Loc
,
4254 Defining_Identifier
=> Def_Id
,
4256 New_Reference_To
(Base_Type
(Etype
(Prefix
(Exp
))), Loc
),
4257 Name
=> Relocate_Node
(Prefix
(Exp
))));
4260 Res
:= New_Reference_To
(Def_Id
, Loc
);
4263 Make_Object_Renaming_Declaration
(Loc
,
4264 Defining_Identifier
=> Def_Id
,
4265 Subtype_Mark
=> New_Reference_To
(Exp_Type
, Loc
),
4266 Name
=> Relocate_Node
(Exp
)));
4270 -- The temporary must be elaborated by gigi, and is of course
4271 -- not to be replaced in-line by the expression it renames,
4272 -- which would defeat the purpose of removing the side-effect.
4274 Set_Is_Renaming_Of_Object
(Def_Id
, False);
4276 -- Otherwise we generate a reference to the value
4279 Ref_Type
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('A'));
4282 Make_Full_Type_Declaration
(Loc
,
4283 Defining_Identifier
=> Ref_Type
,
4285 Make_Access_To_Object_Definition
(Loc
,
4286 All_Present
=> True,
4287 Subtype_Indication
=>
4288 New_Reference_To
(Exp_Type
, Loc
)));
4291 Insert_Action
(Exp
, Ptr_Typ_Decl
);
4293 Def_Id
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('R'));
4294 Set_Etype
(Def_Id
, Exp_Type
);
4297 Make_Explicit_Dereference
(Loc
,
4298 Prefix
=> New_Reference_To
(Def_Id
, Loc
));
4300 if Nkind
(E
) = N_Explicit_Dereference
then
4301 New_Exp
:= Relocate_Node
(Prefix
(E
));
4303 E
:= Relocate_Node
(E
);
4304 New_Exp
:= Make_Reference
(Loc
, E
);
4307 if Is_Delayed_Aggregate
(E
) then
4309 -- The expansion of nested aggregates is delayed until the
4310 -- enclosing aggregate is expanded. As aggregates are often
4311 -- qualified, the predicate applies to qualified expressions
4312 -- as well, indicating that the enclosing aggregate has not
4313 -- been expanded yet. At this point the aggregate is part of
4314 -- a stand-alone declaration, and must be fully expanded.
4316 if Nkind
(E
) = N_Qualified_Expression
then
4317 Set_Expansion_Delayed
(Expression
(E
), False);
4318 Set_Analyzed
(Expression
(E
), False);
4320 Set_Expansion_Delayed
(E
, False);
4323 Set_Analyzed
(E
, False);
4327 Make_Object_Declaration
(Loc
,
4328 Defining_Identifier
=> Def_Id
,
4329 Object_Definition
=> New_Reference_To
(Ref_Type
, Loc
),
4330 Expression
=> New_Exp
));
4333 -- Preserve the Assignment_OK flag in all copies, since at least
4334 -- one copy may be used in a context where this flag must be set
4335 -- (otherwise why would the flag be set in the first place).
4337 Set_Assignment_OK
(Res
, Assignment_OK
(Exp
));
4339 -- Finally rewrite the original expression and we are done
4342 Analyze_And_Resolve
(Exp
, Exp_Type
);
4343 Scope_Suppress
:= Svg_Suppress
;
4344 end Remove_Side_Effects
;
4346 ---------------------------
4347 -- Represented_As_Scalar --
4348 ---------------------------
4350 function Represented_As_Scalar
(T
: Entity_Id
) return Boolean is
4351 UT
: constant Entity_Id
:= Underlying_Type
(T
);
4353 return Is_Scalar_Type
(UT
)
4354 or else (Is_Bit_Packed_Array
(UT
)
4355 and then Is_Scalar_Type
(Packed_Array_Type
(UT
)));
4356 end Represented_As_Scalar
;
4358 ------------------------------------
4359 -- Safe_Unchecked_Type_Conversion --
4360 ------------------------------------
4362 -- Note: this function knows quite a bit about the exact requirements
4363 -- of Gigi with respect to unchecked type conversions, and its code
4364 -- must be coordinated with any changes in Gigi in this area.
4366 -- The above requirements should be documented in Sinfo ???
4368 function Safe_Unchecked_Type_Conversion
(Exp
: Node_Id
) return Boolean is
4373 Pexp
: constant Node_Id
:= Parent
(Exp
);
4376 -- If the expression is the RHS of an assignment or object declaration
4377 -- we are always OK because there will always be a target.
4379 -- Object renaming declarations, (generated for view conversions of
4380 -- actuals in inlined calls), like object declarations, provide an
4381 -- explicit type, and are safe as well.
4383 if (Nkind
(Pexp
) = N_Assignment_Statement
4384 and then Expression
(Pexp
) = Exp
)
4385 or else Nkind
(Pexp
) = N_Object_Declaration
4386 or else Nkind
(Pexp
) = N_Object_Renaming_Declaration
4390 -- If the expression is the prefix of an N_Selected_Component
4391 -- we should also be OK because GCC knows to look inside the
4392 -- conversion except if the type is discriminated. We assume
4393 -- that we are OK anyway if the type is not set yet or if it is
4394 -- controlled since we can't afford to introduce a temporary in
4397 elsif Nkind
(Pexp
) = N_Selected_Component
4398 and then Prefix
(Pexp
) = Exp
4400 if No
(Etype
(Pexp
)) then
4404 not Has_Discriminants
(Etype
(Pexp
))
4405 or else Is_Constrained
(Etype
(Pexp
));
4409 -- Set the output type, this comes from Etype if it is set, otherwise
4410 -- we take it from the subtype mark, which we assume was already
4413 if Present
(Etype
(Exp
)) then
4414 Otyp
:= Etype
(Exp
);
4416 Otyp
:= Entity
(Subtype_Mark
(Exp
));
4419 -- The input type always comes from the expression, and we assume
4420 -- this is indeed always analyzed, so we can simply get the Etype.
4422 Ityp
:= Etype
(Expression
(Exp
));
4424 -- Initialize alignments to unknown so far
4429 -- Replace a concurrent type by its corresponding record type
4430 -- and each type by its underlying type and do the tests on those.
4431 -- The original type may be a private type whose completion is a
4432 -- concurrent type, so find the underlying type first.
4434 if Present
(Underlying_Type
(Otyp
)) then
4435 Otyp
:= Underlying_Type
(Otyp
);
4438 if Present
(Underlying_Type
(Ityp
)) then
4439 Ityp
:= Underlying_Type
(Ityp
);
4442 if Is_Concurrent_Type
(Otyp
) then
4443 Otyp
:= Corresponding_Record_Type
(Otyp
);
4446 if Is_Concurrent_Type
(Ityp
) then
4447 Ityp
:= Corresponding_Record_Type
(Ityp
);
4450 -- If the base types are the same, we know there is no problem since
4451 -- this conversion will be a noop.
4453 if Implementation_Base_Type
(Otyp
) = Implementation_Base_Type
(Ityp
) then
4456 -- Same if this is an upwards conversion of an untagged type, and there
4457 -- are no constraints involved (could be more general???)
4459 elsif Etype
(Ityp
) = Otyp
4460 and then not Is_Tagged_Type
(Ityp
)
4461 and then not Has_Discriminants
(Ityp
)
4462 and then No
(First_Rep_Item
(Base_Type
(Ityp
)))
4466 -- If the size of output type is known at compile time, there is
4467 -- never a problem. Note that unconstrained records are considered
4468 -- to be of known size, but we can't consider them that way here,
4469 -- because we are talking about the actual size of the object.
4471 -- We also make sure that in addition to the size being known, we do
4472 -- not have a case which might generate an embarrassingly large temp
4473 -- in stack checking mode.
4475 elsif Size_Known_At_Compile_Time
(Otyp
)
4477 (not Stack_Checking_Enabled
4478 or else not May_Generate_Large_Temp
(Otyp
))
4479 and then not (Is_Record_Type
(Otyp
) and then not Is_Constrained
(Otyp
))
4483 -- If either type is tagged, then we know the alignment is OK so
4484 -- Gigi will be able to use pointer punning.
4486 elsif Is_Tagged_Type
(Otyp
) or else Is_Tagged_Type
(Ityp
) then
4489 -- If either type is a limited record type, we cannot do a copy, so
4490 -- say safe since there's nothing else we can do.
4492 elsif Is_Limited_Record
(Otyp
) or else Is_Limited_Record
(Ityp
) then
4495 -- Conversions to and from packed array types are always ignored and
4498 elsif Is_Packed_Array_Type
(Otyp
)
4499 or else Is_Packed_Array_Type
(Ityp
)
4504 -- The only other cases known to be safe is if the input type's
4505 -- alignment is known to be at least the maximum alignment for the
4506 -- target or if both alignments are known and the output type's
4507 -- alignment is no stricter than the input's. We can use the alignment
4508 -- of the component type of an array if a type is an unpacked
4511 if Present
(Alignment_Clause
(Otyp
)) then
4512 Oalign
:= Expr_Value
(Expression
(Alignment_Clause
(Otyp
)));
4514 elsif Is_Array_Type
(Otyp
)
4515 and then Present
(Alignment_Clause
(Component_Type
(Otyp
)))
4517 Oalign
:= Expr_Value
(Expression
(Alignment_Clause
4518 (Component_Type
(Otyp
))));
4521 if Present
(Alignment_Clause
(Ityp
)) then
4522 Ialign
:= Expr_Value
(Expression
(Alignment_Clause
(Ityp
)));
4524 elsif Is_Array_Type
(Ityp
)
4525 and then Present
(Alignment_Clause
(Component_Type
(Ityp
)))
4527 Ialign
:= Expr_Value
(Expression
(Alignment_Clause
4528 (Component_Type
(Ityp
))));
4531 if Ialign
/= No_Uint
and then Ialign
> Maximum_Alignment
then
4534 elsif Ialign
/= No_Uint
and then Oalign
/= No_Uint
4535 and then Ialign
<= Oalign
4539 -- Otherwise, Gigi cannot handle this and we must make a temporary
4545 end Safe_Unchecked_Type_Conversion
;
4547 --------------------------
4548 -- Set_Elaboration_Flag --
4549 --------------------------
4551 procedure Set_Elaboration_Flag
(N
: Node_Id
; Spec_Id
: Entity_Id
) is
4552 Loc
: constant Source_Ptr
:= Sloc
(N
);
4553 Ent
: constant Entity_Id
:= Elaboration_Entity
(Spec_Id
);
4557 if Present
(Ent
) then
4559 -- Nothing to do if at the compilation unit level, because in this
4560 -- case the flag is set by the binder generated elaboration routine.
4562 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
4565 -- Here we do need to generate an assignment statement
4568 Check_Restriction
(No_Elaboration_Code
, N
);
4570 Make_Assignment_Statement
(Loc
,
4571 Name
=> New_Occurrence_Of
(Ent
, Loc
),
4572 Expression
=> New_Occurrence_Of
(Standard_True
, Loc
));
4574 if Nkind
(Parent
(N
)) = N_Subunit
then
4575 Insert_After
(Corresponding_Stub
(Parent
(N
)), Asn
);
4577 Insert_After
(N
, Asn
);
4582 -- Kill current value indication. This is necessary because
4583 -- the tests of this flag are inserted out of sequence and must
4584 -- not pick up bogus indications of the wrong constant value.
4586 Set_Current_Value
(Ent
, Empty
);
4589 end Set_Elaboration_Flag
;
4591 --------------------------
4592 -- Target_Has_Fixed_Ops --
4593 --------------------------
4595 Integer_Sized_Small
: Ureal
;
4596 -- Set to 2.0 ** -(Integer'Size - 1) the first time that this
4597 -- function is called (we don't want to compute it more than once!)
4599 Long_Integer_Sized_Small
: Ureal
;
4600 -- Set to 2.0 ** -(Long_Integer'Size - 1) the first time that this
4601 -- functoin is called (we don't want to compute it more than once)
4603 First_Time_For_THFO
: Boolean := True;
4604 -- Set to False after first call (if Fractional_Fixed_Ops_On_Target)
4606 function Target_Has_Fixed_Ops
4607 (Left_Typ
: Entity_Id
;
4608 Right_Typ
: Entity_Id
;
4609 Result_Typ
: Entity_Id
) return Boolean
4611 function Is_Fractional_Type
(Typ
: Entity_Id
) return Boolean;
4612 -- Return True if the given type is a fixed-point type with a small
4613 -- value equal to 2 ** (-(T'Object_Size - 1)) and whose values have
4614 -- an absolute value less than 1.0. This is currently limited
4615 -- to fixed-point types that map to Integer or Long_Integer.
4617 ------------------------
4618 -- Is_Fractional_Type --
4619 ------------------------
4621 function Is_Fractional_Type
(Typ
: Entity_Id
) return Boolean is
4623 if Esize
(Typ
) = Standard_Integer_Size
then
4624 return Small_Value
(Typ
) = Integer_Sized_Small
;
4626 elsif Esize
(Typ
) = Standard_Long_Integer_Size
then
4627 return Small_Value
(Typ
) = Long_Integer_Sized_Small
;
4632 end Is_Fractional_Type
;
4634 -- Start of processing for Target_Has_Fixed_Ops
4637 -- Return False if Fractional_Fixed_Ops_On_Target is false
4639 if not Fractional_Fixed_Ops_On_Target
then
4643 -- Here the target has Fractional_Fixed_Ops, if first time, compute
4644 -- standard constants used by Is_Fractional_Type.
4646 if First_Time_For_THFO
then
4647 First_Time_For_THFO
:= False;
4649 Integer_Sized_Small
:=
4652 Den
=> UI_From_Int
(Standard_Integer_Size
- 1),
4655 Long_Integer_Sized_Small
:=
4658 Den
=> UI_From_Int
(Standard_Long_Integer_Size
- 1),
4662 -- Return True if target supports fixed-by-fixed multiply/divide
4663 -- for fractional fixed-point types (see Is_Fractional_Type) and
4664 -- the operand and result types are equivalent fractional types.
4666 return Is_Fractional_Type
(Base_Type
(Left_Typ
))
4667 and then Is_Fractional_Type
(Base_Type
(Right_Typ
))
4668 and then Is_Fractional_Type
(Base_Type
(Result_Typ
))
4669 and then Esize
(Left_Typ
) = Esize
(Right_Typ
)
4670 and then Esize
(Left_Typ
) = Esize
(Result_Typ
);
4671 end Target_Has_Fixed_Ops
;
4673 ------------------------------------------
4674 -- Type_May_Have_Bit_Aligned_Components --
4675 ------------------------------------------
4677 function Type_May_Have_Bit_Aligned_Components
4678 (Typ
: Entity_Id
) return Boolean
4681 -- Array type, check component type
4683 if Is_Array_Type
(Typ
) then
4685 Type_May_Have_Bit_Aligned_Components
(Component_Type
(Typ
));
4687 -- Record type, check components
4689 elsif Is_Record_Type
(Typ
) then
4694 E
:= First_Entity
(Typ
);
4695 while Present
(E
) loop
4696 if Ekind
(E
) = E_Component
4697 or else Ekind
(E
) = E_Discriminant
4699 if Component_May_Be_Bit_Aligned
(E
)
4701 Type_May_Have_Bit_Aligned_Components
(Etype
(E
))
4713 -- Type other than array or record is always OK
4718 end Type_May_Have_Bit_Aligned_Components
;
4720 ----------------------------
4721 -- Wrap_Cleanup_Procedure --
4722 ----------------------------
4724 procedure Wrap_Cleanup_Procedure
(N
: Node_Id
) is
4725 Loc
: constant Source_Ptr
:= Sloc
(N
);
4726 Stseq
: constant Node_Id
:= Handled_Statement_Sequence
(N
);
4727 Stmts
: constant List_Id
:= Statements
(Stseq
);
4730 if Abort_Allowed
then
4731 Prepend_To
(Stmts
, Build_Runtime_Call
(Loc
, RE_Abort_Defer
));
4732 Append_To
(Stmts
, Build_Runtime_Call
(Loc
, RE_Abort_Undefer
));
4734 end Wrap_Cleanup_Procedure
;