1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2003, 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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_Ch7
; use Exp_Ch7
;
33 with Exp_Ch11
; use Exp_Ch11
;
34 with Exp_Tss
; use Exp_Tss
;
35 with Hostparm
; use Hostparm
;
36 with Inline
; use Inline
;
37 with Itypes
; use Itypes
;
39 with Namet
; use Namet
;
40 with Nlists
; use Nlists
;
41 with Nmake
; use Nmake
;
43 with Restrict
; use Restrict
;
45 with Sem_Ch8
; use Sem_Ch8
;
46 with Sem_Eval
; use Sem_Eval
;
47 with Sem_Res
; use Sem_Res
;
48 with Sem_Util
; use Sem_Util
;
49 with Sinfo
; use Sinfo
;
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)
72 -- Build function to generate the image string for a task that is an
73 -- array component, concatenating the images of each index. To avoid
74 -- storage leaks, the string is built with successive slice assignments.
75 -- The flag Dyn indicates whether this is called for the initialization
76 -- procedure of an array of tasks, or for the name of a dynamically
77 -- created task that is assigned to an indexed component.
79 function Build_Task_Image_Function
85 -- Common processing for Task_Array_Image and Task_Record_Image.
86 -- Build function body that computes image.
88 procedure Build_Task_Image_Prefix
95 Decls
: in out List_Id
;
96 Stats
: in out List_Id
);
97 -- Common processing for Task_Array_Image and Task_Record_Image.
98 -- Create local variables and assign prefix of name to result string.
100 function Build_Task_Record_Image
103 Dyn
: Boolean := False)
105 -- Build function to generate the image string for a task that is a
106 -- record component. Concatenate name of variable with that of selector.
107 -- The flag Dyn indicates whether this is called for the initialization
108 -- procedure of record with task components, or for a dynamically
109 -- created task that is assigned to a selected component.
111 function Make_CW_Equivalent_Type
115 -- T is a class-wide type entity, E is the initial expression node that
116 -- constrains T in case such as: " X: T := E" or "new T'(E)"
117 -- This function returns the entity of the Equivalent type and inserts
118 -- on the fly the necessary declaration such as:
120 -- type anon is record
121 -- _parent : Root_Type (T); constrained with E discriminants (if any)
122 -- Extension : String (1 .. expr to match size of E);
125 -- This record is compatible with any object of the class of T thanks
126 -- to the first field and has the same size as E thanks to the second.
128 function Make_Literal_Range
130 Literal_Typ
: Entity_Id
)
132 -- Produce a Range node whose bounds are:
133 -- Low_Bound (Literal_Type) ..
134 -- Low_Bound (Literal_Type) + Length (Literal_Typ) - 1
135 -- this is used for expanding declarations like X : String := "sdfgdfg";
137 function New_Class_Wide_Subtype
141 -- Create an implicit subtype of CW_Typ attached to node N.
143 ----------------------
144 -- Adjust_Condition --
145 ----------------------
147 procedure Adjust_Condition
(N
: Node_Id
) is
154 Loc
: constant Source_Ptr
:= Sloc
(N
);
155 T
: constant Entity_Id
:= Etype
(N
);
159 -- For now, we simply ignore a call where the argument has no
160 -- type (probably case of unanalyzed condition), or has a type
161 -- that is not Boolean. This is because this is a pretty marginal
162 -- piece of functionality, and violations of these rules are
163 -- likely to be truly marginal (how much code uses Fortran Logical
164 -- as the barrier to a protected entry?) and we do not want to
165 -- blow up existing programs. We can change this to an assertion
166 -- after 3.12a is released ???
168 if No
(T
) or else not Is_Boolean_Type
(T
) then
172 -- Apply validity checking if needed
174 if Validity_Checks_On
and Validity_Check_Tests
then
178 -- Immediate return if standard boolean, the most common case,
179 -- where nothing needs to be done.
181 if Base_Type
(T
) = Standard_Boolean
then
185 -- Case of zero/non-zero semantics or non-standard enumeration
186 -- representation. In each case, we rewrite the node as:
188 -- ityp!(N) /= False'Enum_Rep
190 -- where ityp is an integer type with large enough size to hold
191 -- any value of type T.
193 if Nonzero_Is_True
(T
) or else Has_Non_Standard_Rep
(T
) then
194 if Esize
(T
) <= Esize
(Standard_Integer
) then
195 Ti
:= Standard_Integer
;
197 Ti
:= Standard_Long_Long_Integer
;
202 Left_Opnd
=> Unchecked_Convert_To
(Ti
, N
),
204 Make_Attribute_Reference
(Loc
,
205 Attribute_Name
=> Name_Enum_Rep
,
207 New_Occurrence_Of
(First_Literal
(T
), Loc
))));
208 Analyze_And_Resolve
(N
, Standard_Boolean
);
211 Rewrite
(N
, Convert_To
(Standard_Boolean
, N
));
212 Analyze_And_Resolve
(N
, Standard_Boolean
);
215 end Adjust_Condition
;
217 ------------------------
218 -- Adjust_Result_Type --
219 ------------------------
221 procedure Adjust_Result_Type
(N
: Node_Id
; T
: Entity_Id
) is
223 -- Ignore call if current type is not Standard.Boolean
225 if Etype
(N
) /= Standard_Boolean
then
229 -- If result is already of correct type, nothing to do. Note that
230 -- this will get the most common case where everything has a type
231 -- of Standard.Boolean.
233 if Base_Type
(T
) = Standard_Boolean
then
238 KP
: constant Node_Kind
:= Nkind
(Parent
(N
));
241 -- If result is to be used as a Condition in the syntax, no need
242 -- to convert it back, since if it was changed to Standard.Boolean
243 -- using Adjust_Condition, that is just fine for this usage.
245 if KP
in N_Raise_xxx_Error
or else KP
in N_Has_Condition
then
248 -- If result is an operand of another logical operation, no need
249 -- to reset its type, since Standard.Boolean is just fine, and
250 -- such operations always do Adjust_Condition on their operands.
252 elsif KP
in N_Op_Boolean
253 or else KP
= N_And_Then
254 or else KP
= N_Or_Else
255 or else KP
= N_Op_Not
259 -- Otherwise we perform a conversion from the current type,
260 -- which must be Standard.Boolean, to the desired type.
264 Rewrite
(N
, Convert_To
(T
, N
));
265 Analyze_And_Resolve
(N
, T
);
269 end Adjust_Result_Type
;
271 --------------------------
272 -- Append_Freeze_Action --
273 --------------------------
275 procedure Append_Freeze_Action
(T
: Entity_Id
; N
: Node_Id
) is
276 Fnode
: Node_Id
:= Freeze_Node
(T
);
279 Ensure_Freeze_Node
(T
);
280 Fnode
:= Freeze_Node
(T
);
282 if not Present
(Actions
(Fnode
)) then
283 Set_Actions
(Fnode
, New_List
);
286 Append
(N
, Actions
(Fnode
));
287 end Append_Freeze_Action
;
289 ---------------------------
290 -- Append_Freeze_Actions --
291 ---------------------------
293 procedure Append_Freeze_Actions
(T
: Entity_Id
; L
: List_Id
) is
294 Fnode
: constant Node_Id
:= Freeze_Node
(T
);
301 if No
(Actions
(Fnode
)) then
302 Set_Actions
(Fnode
, L
);
305 Append_List
(L
, Actions
(Fnode
));
309 end Append_Freeze_Actions
;
311 ------------------------
312 -- Build_Runtime_Call --
313 ------------------------
315 function Build_Runtime_Call
(Loc
: Source_Ptr
; RE
: RE_Id
) return Node_Id
is
317 -- If entity is not available, we can skip making the call (this avoids
318 -- junk duplicated error messages in a number of cases).
320 if not RTE_Available
(RE
) then
321 return Make_Null_Statement
(Loc
);
324 Make_Procedure_Call_Statement
(Loc
,
325 Name
=> New_Reference_To
(RTE
(RE
), Loc
));
327 end Build_Runtime_Call
;
329 -----------------------------
330 -- Build_Task_Array_Image --
331 -----------------------------
333 -- This function generates the body for a function that constructs the
334 -- image string for a task that is an array component. The function is
335 -- local to the init proc for the array type, and is called for each one
336 -- of the components. The constructed image has the form of an indexed
337 -- component, whose prefix is the outer variable of the array type.
338 -- The n-dimensional array type has known indices Index, Index2...
339 -- Id_Ref is an indexed component form created by the enclosing init proc.
340 -- Its successive indices are Val1, Val2,.. which are the loop variables
341 -- in the loops that call the individual task init proc on each component.
343 -- The generated function has the following structure:
345 -- function F return String is
346 -- Pref : string renames Task_Name;
347 -- T1 : String := Index1'Image (Val1);
349 -- Tn : String := indexn'image (Valn);
350 -- Len : Integer := T1'Length + ... + Tn'Length + n + 1;
351 -- -- Len includes commas and the end parentheses.
352 -- Res : String (1..Len);
353 -- Pos : Integer := Pref'Length;
356 -- Res (1 .. Pos) := Pref;
360 -- Res (Pos .. Pos + T1'Length - 1) := T1;
361 -- Pos := Pos + T1'Length;
365 -- Res (Pos .. Pos + Tn'Length - 1) := Tn;
371 -- Needless to say, multidimensional arrays of tasks are rare enough
372 -- that the bulkiness of this code is not really a concern.
374 function Build_Task_Array_Image
378 Dyn
: Boolean := False)
381 Dims
: constant Nat
:= Number_Dimensions
(A_Type
);
382 -- Number of dimensions for array of tasks.
384 Temps
: array (1 .. Dims
) of Entity_Id
;
385 -- Array of temporaries to hold string for each index.
391 -- Total length of generated name
394 -- Running index for substring assignments
397 -- Name of enclosing variable, prefix of resulting name
400 -- String to hold result
403 -- Value of successive indices
406 -- Expression to compute total size of string
409 -- Entity for name at one index position
411 Decls
: List_Id
:= New_List
;
412 Stats
: List_Id
:= New_List
;
415 Pref
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('P'));
417 -- For a dynamic task, the name comes from the target variable.
418 -- For a static one it is a formal of the enclosing init proc.
421 Get_Name_String
(Chars
(Entity
(Prefix
(Id_Ref
))));
423 Make_Object_Declaration
(Loc
,
424 Defining_Identifier
=> Pref
,
425 Object_Definition
=> New_Occurrence_Of
(Standard_String
, Loc
),
427 Make_String_Literal
(Loc
, Strval
=> String_From_Name_Buffer
)));
431 Make_Object_Renaming_Declaration
(Loc
,
432 Defining_Identifier
=> Pref
,
433 Subtype_Mark
=> New_Occurrence_Of
(Standard_String
, Loc
),
434 Name
=> Make_Identifier
(Loc
, Name_uTask_Name
)));
437 Indx
:= First_Index
(A_Type
);
438 Val
:= First
(Expressions
(Id_Ref
));
440 for J
in 1 .. Dims
loop
441 T
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('T'));
445 Make_Object_Declaration
(Loc
,
446 Defining_Identifier
=> T
,
447 Object_Definition
=> New_Occurrence_Of
(Standard_String
, Loc
),
449 Make_Attribute_Reference
(Loc
,
450 Attribute_Name
=> Name_Image
,
452 New_Occurrence_Of
(Etype
(Indx
), Loc
),
453 Expressions
=> New_List
(
454 New_Copy_Tree
(Val
)))));
460 Sum
:= Make_Integer_Literal
(Loc
, Dims
+ 1);
466 Make_Attribute_Reference
(Loc
,
467 Attribute_Name
=> Name_Length
,
469 New_Occurrence_Of
(Pref
, Loc
),
470 Expressions
=> New_List
(Make_Integer_Literal
(Loc
, 1))));
472 for J
in 1 .. Dims
loop
477 Make_Attribute_Reference
(Loc
,
478 Attribute_Name
=> Name_Length
,
480 New_Occurrence_Of
(Temps
(J
), Loc
),
481 Expressions
=> New_List
(Make_Integer_Literal
(Loc
, 1))));
484 Build_Task_Image_Prefix
(Loc
, Len
, Res
, Pos
, Pref
, Sum
, Decls
, Stats
);
486 Set_Character_Literal_Name
(Char_Code
(Character'Pos ('(')));
489 Make_Assignment_Statement
(Loc
,
490 Name
=> Make_Indexed_Component
(Loc
,
491 Prefix
=> New_Occurrence_Of
(Res
, Loc
),
492 Expressions
=> New_List
(New_Occurrence_Of
(Pos
, Loc
))),
494 Make_Character_Literal
(Loc
,
496 Char_Literal_Value
=>
497 Char_Code
(Character'Pos ('(')))));
500 Make_Assignment_Statement
(Loc
,
501 Name
=> New_Occurrence_Of
(Pos
, Loc
),
504 Left_Opnd
=> New_Occurrence_Of
(Pos
, Loc
),
505 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1))));
507 for J
in 1 .. Dims
loop
510 Make_Assignment_Statement
(Loc
,
511 Name
=> Make_Slice
(Loc
,
512 Prefix
=> New_Occurrence_Of
(Res
, Loc
),
515 Low_Bound
=> New_Occurrence_Of
(Pos
, Loc
),
516 High_Bound
=> Make_Op_Subtract
(Loc
,
519 Left_Opnd
=> New_Occurrence_Of
(Pos
, Loc
),
521 Make_Attribute_Reference
(Loc
,
522 Attribute_Name
=> Name_Length
,
524 New_Occurrence_Of
(Temps
(J
), Loc
),
526 New_List
(Make_Integer_Literal
(Loc
, 1)))),
527 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1)))),
529 Expression
=> New_Occurrence_Of
(Temps
(J
), Loc
)));
533 Make_Assignment_Statement
(Loc
,
534 Name
=> New_Occurrence_Of
(Pos
, Loc
),
537 Left_Opnd
=> New_Occurrence_Of
(Pos
, Loc
),
539 Make_Attribute_Reference
(Loc
,
540 Attribute_Name
=> Name_Length
,
541 Prefix
=> New_Occurrence_Of
(Temps
(J
), Loc
),
543 New_List
(Make_Integer_Literal
(Loc
, 1))))));
545 Set_Character_Literal_Name
(Char_Code
(Character'Pos (',')));
548 Make_Assignment_Statement
(Loc
,
549 Name
=> Make_Indexed_Component
(Loc
,
550 Prefix
=> New_Occurrence_Of
(Res
, Loc
),
551 Expressions
=> New_List
(New_Occurrence_Of
(Pos
, Loc
))),
553 Make_Character_Literal
(Loc
,
555 Char_Literal_Value
=>
556 Char_Code
(Character'Pos (',')))));
559 Make_Assignment_Statement
(Loc
,
560 Name
=> New_Occurrence_Of
(Pos
, Loc
),
563 Left_Opnd
=> New_Occurrence_Of
(Pos
, Loc
),
564 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1))));
568 Set_Character_Literal_Name
(Char_Code
(Character'Pos (')')));
571 Make_Assignment_Statement
(Loc
,
572 Name
=> Make_Indexed_Component
(Loc
,
573 Prefix
=> New_Occurrence_Of
(Res
, Loc
),
574 Expressions
=> New_List
(New_Occurrence_Of
(Len
, Loc
))),
576 Make_Character_Literal
(Loc
,
578 Char_Literal_Value
=>
579 Char_Code
(Character'Pos (')')))));
580 return Build_Task_Image_Function
(Loc
, Decls
, Stats
, Res
);
581 end Build_Task_Array_Image
;
583 ----------------------------
584 -- Build_Task_Image_Decls --
585 ----------------------------
587 function Build_Task_Image_Decls
593 Decls
: constant List_Id
:= New_List
;
594 T_Id
: Entity_Id
:= Empty
;
596 Expr
: Node_Id
:= Empty
;
597 Fun
: Node_Id
:= Empty
;
598 Is_Dyn
: constant Boolean :=
599 Nkind
(Parent
(Id_Ref
)) = N_Assignment_Statement
601 Nkind
(Expression
(Parent
(Id_Ref
))) = N_Allocator
;
604 -- If Discard_Names or No_Implicit_Heap_Allocations are in effect,
605 -- generate a dummy declaration only.
607 if Restrictions
(No_Implicit_Heap_Allocations
)
608 or else Global_Discard_Names
610 T_Id
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('J'));
615 Make_Object_Declaration
(Loc
,
616 Defining_Identifier
=> T_Id
,
617 Object_Definition
=> New_Occurrence_Of
(Standard_String
, Loc
),
620 (Loc
, Strval
=> String_From_Name_Buffer
)));
623 if Nkind
(Id_Ref
) = N_Identifier
624 or else Nkind
(Id_Ref
) = N_Defining_Identifier
626 -- For a simple variable, the image of the task is the name
630 Make_Defining_Identifier
(Loc
,
631 New_External_Name
(Chars
(Id_Ref
), 'T'));
633 Get_Name_String
(Chars
(Id_Ref
));
635 Expr
:= Make_String_Literal
636 (Loc
, Strval
=> String_From_Name_Buffer
);
638 elsif Nkind
(Id_Ref
) = N_Selected_Component
then
640 Make_Defining_Identifier
(Loc
,
641 New_External_Name
(Chars
(Selector_Name
(Id_Ref
)), 'T'));
642 Fun
:= Build_Task_Record_Image
(Loc
, Id_Ref
, Is_Dyn
);
644 elsif Nkind
(Id_Ref
) = N_Indexed_Component
then
646 Make_Defining_Identifier
(Loc
,
647 New_External_Name
(Chars
(A_Type
), 'N'));
649 Fun
:= Build_Task_Array_Image
(Loc
, Id_Ref
, A_Type
, Is_Dyn
);
653 if Present
(Fun
) then
655 Expr
:= Make_Function_Call
(Loc
,
656 Name
=> New_Occurrence_Of
(Defining_Entity
(Fun
), Loc
));
659 Decl
:= Make_Object_Declaration
(Loc
,
660 Defining_Identifier
=> T_Id
,
661 Object_Definition
=> New_Occurrence_Of
(Standard_String
, Loc
),
662 Constant_Present
=> True,
665 Append
(Decl
, Decls
);
667 end Build_Task_Image_Decls
;
669 -------------------------------
670 -- Build_Task_Image_Function --
671 -------------------------------
673 function Build_Task_Image_Function
684 Make_Return_Statement
(Loc
,
685 Expression
=> New_Occurrence_Of
(Res
, Loc
)));
687 Spec
:= Make_Function_Specification
(Loc
,
688 Defining_Unit_Name
=>
689 Make_Defining_Identifier
(Loc
, New_Internal_Name
('F')),
690 Subtype_Mark
=> New_Occurrence_Of
(Standard_String
, Loc
));
692 -- Calls to 'Image use the secondary stack, which must be cleaned
693 -- up after the task name is built.
695 Set_Uses_Sec_Stack
(Defining_Unit_Name
(Spec
));
697 return Make_Subprogram_Body
(Loc
,
698 Specification
=> Spec
,
699 Declarations
=> Decls
,
700 Handled_Statement_Sequence
=>
701 Make_Handled_Sequence_Of_Statements
(Loc
, Statements
=> Stats
));
702 end Build_Task_Image_Function
;
704 -----------------------------
705 -- Build_Task_Image_Prefix --
706 -----------------------------
708 procedure Build_Task_Image_Prefix
715 Decls
: in out List_Id
;
716 Stats
: in out List_Id
)
719 Len
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('L'));
722 Make_Object_Declaration
(Loc
,
723 Defining_Identifier
=> Len
,
724 Object_Definition
=> New_Occurrence_Of
(Standard_Integer
, Loc
),
727 Res
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('R'));
730 Make_Object_Declaration
(Loc
,
731 Defining_Identifier
=> Res
,
733 Make_Subtype_Indication
(Loc
,
734 Subtype_Mark
=> New_Occurrence_Of
(Standard_String
, Loc
),
736 Make_Index_Or_Discriminant_Constraint
(Loc
,
740 Low_Bound
=> Make_Integer_Literal
(Loc
, 1),
741 High_Bound
=> New_Occurrence_Of
(Len
, Loc
)))))));
743 Pos
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('P'));
746 Make_Object_Declaration
(Loc
,
747 Defining_Identifier
=> Pos
,
748 Object_Definition
=> New_Occurrence_Of
(Standard_Integer
, Loc
)));
750 -- Pos := Prefix'Length;
753 Make_Assignment_Statement
(Loc
,
754 Name
=> New_Occurrence_Of
(Pos
, Loc
),
756 Make_Attribute_Reference
(Loc
,
757 Attribute_Name
=> Name_Length
,
758 Prefix
=> New_Occurrence_Of
(Prefix
, Loc
),
760 New_List
(Make_Integer_Literal
(Loc
, 1)))));
762 -- Res (1 .. Pos) := Prefix;
765 Make_Assignment_Statement
(Loc
,
766 Name
=> Make_Slice
(Loc
,
767 Prefix
=> New_Occurrence_Of
(Res
, Loc
),
770 Low_Bound
=> Make_Integer_Literal
(Loc
, 1),
771 High_Bound
=> New_Occurrence_Of
(Pos
, Loc
))),
773 Expression
=> New_Occurrence_Of
(Prefix
, Loc
)));
776 Make_Assignment_Statement
(Loc
,
777 Name
=> New_Occurrence_Of
(Pos
, Loc
),
780 Left_Opnd
=> New_Occurrence_Of
(Pos
, Loc
),
781 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1))));
782 end Build_Task_Image_Prefix
;
784 -----------------------------
785 -- Build_Task_Record_Image --
786 -----------------------------
788 function Build_Task_Record_Image
791 Dyn
: Boolean := False)
795 -- Total length of generated name
801 -- String to hold result
804 -- Name of enclosing variable, prefix of resulting name
807 -- Expression to compute total size of string.
810 -- Entity for selector name
812 Decls
: List_Id
:= New_List
;
813 Stats
: List_Id
:= New_List
;
816 Pref
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('P'));
818 -- For a dynamic task, the name comes from the target variable.
819 -- For a static one it is a formal of the enclosing init proc.
822 Get_Name_String
(Chars
(Entity
(Prefix
(Id_Ref
))));
824 Make_Object_Declaration
(Loc
,
825 Defining_Identifier
=> Pref
,
826 Object_Definition
=> New_Occurrence_Of
(Standard_String
, Loc
),
828 Make_String_Literal
(Loc
, Strval
=> String_From_Name_Buffer
)));
832 Make_Object_Renaming_Declaration
(Loc
,
833 Defining_Identifier
=> Pref
,
834 Subtype_Mark
=> New_Occurrence_Of
(Standard_String
, Loc
),
835 Name
=> Make_Identifier
(Loc
, Name_uTask_Name
)));
838 Sel
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('S'));
840 Get_Name_String
(Chars
(Selector_Name
(Id_Ref
)));
843 Make_Object_Declaration
(Loc
,
844 Defining_Identifier
=> Sel
,
845 Object_Definition
=> New_Occurrence_Of
(Standard_String
, Loc
),
847 Make_String_Literal
(Loc
, Strval
=> String_From_Name_Buffer
)));
849 Sum
:= Make_Integer_Literal
(Loc
, Nat
(Name_Len
+ 1));
855 Make_Attribute_Reference
(Loc
,
856 Attribute_Name
=> Name_Length
,
858 New_Occurrence_Of
(Pref
, Loc
),
859 Expressions
=> New_List
(Make_Integer_Literal
(Loc
, 1))));
861 Build_Task_Image_Prefix
(Loc
, Len
, Res
, Pos
, Pref
, Sum
, Decls
, Stats
);
863 Set_Character_Literal_Name
(Char_Code
(Character'Pos ('.')));
868 Make_Assignment_Statement
(Loc
,
869 Name
=> Make_Indexed_Component
(Loc
,
870 Prefix
=> New_Occurrence_Of
(Res
, Loc
),
871 Expressions
=> New_List
(New_Occurrence_Of
(Pos
, Loc
))),
873 Make_Character_Literal
(Loc
,
875 Char_Literal_Value
=>
876 Char_Code
(Character'Pos ('.')))));
879 Make_Assignment_Statement
(Loc
,
880 Name
=> New_Occurrence_Of
(Pos
, Loc
),
883 Left_Opnd
=> New_Occurrence_Of
(Pos
, Loc
),
884 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1))));
886 -- Res (Pos .. Len) := Selector;
889 Make_Assignment_Statement
(Loc
,
890 Name
=> Make_Slice
(Loc
,
891 Prefix
=> New_Occurrence_Of
(Res
, Loc
),
894 Low_Bound
=> New_Occurrence_Of
(Pos
, Loc
),
895 High_Bound
=> New_Occurrence_Of
(Len
, Loc
))),
896 Expression
=> New_Occurrence_Of
(Sel
, Loc
)));
898 return Build_Task_Image_Function
(Loc
, Decls
, Stats
, Res
);
899 end Build_Task_Record_Image
;
901 -------------------------------
902 -- Convert_To_Actual_Subtype --
903 -------------------------------
905 procedure Convert_To_Actual_Subtype
(Exp
: Entity_Id
) is
909 Act_ST
:= Get_Actual_Subtype
(Exp
);
911 if Act_ST
= Etype
(Exp
) then
916 Convert_To
(Act_ST
, Relocate_Node
(Exp
)));
917 Analyze_And_Resolve
(Exp
, Act_ST
);
919 end Convert_To_Actual_Subtype
;
921 -----------------------------------
922 -- Current_Sem_Unit_Declarations --
923 -----------------------------------
925 function Current_Sem_Unit_Declarations
return List_Id
is
926 U
: Node_Id
:= Unit
(Cunit
(Current_Sem_Unit
));
930 -- If the current unit is a package body, locate the visible
931 -- declarations of the package spec.
933 if Nkind
(U
) = N_Package_Body
then
934 U
:= Unit
(Library_Unit
(Cunit
(Current_Sem_Unit
)));
937 if Nkind
(U
) = N_Package_Declaration
then
938 U
:= Specification
(U
);
939 Decls
:= Visible_Declarations
(U
);
943 Set_Visible_Declarations
(U
, Decls
);
947 Decls
:= Declarations
(U
);
951 Set_Declarations
(U
, Decls
);
956 end Current_Sem_Unit_Declarations
;
958 -----------------------
959 -- Duplicate_Subexpr --
960 -----------------------
962 function Duplicate_Subexpr
964 Name_Req
: Boolean := False)
968 Remove_Side_Effects
(Exp
, Name_Req
);
969 return New_Copy_Tree
(Exp
);
970 end Duplicate_Subexpr
;
972 ---------------------------------
973 -- Duplicate_Subexpr_No_Checks --
974 ---------------------------------
976 function Duplicate_Subexpr_No_Checks
978 Name_Req
: Boolean := False)
984 Remove_Side_Effects
(Exp
, Name_Req
);
985 New_Exp
:= New_Copy_Tree
(Exp
);
986 Remove_Checks
(New_Exp
);
988 end Duplicate_Subexpr_No_Checks
;
990 -----------------------------------
991 -- Duplicate_Subexpr_Move_Checks --
992 -----------------------------------
994 function Duplicate_Subexpr_Move_Checks
996 Name_Req
: Boolean := False)
1002 Remove_Side_Effects
(Exp
, Name_Req
);
1003 New_Exp
:= New_Copy_Tree
(Exp
);
1004 Remove_Checks
(Exp
);
1006 end Duplicate_Subexpr_Move_Checks
;
1008 --------------------
1009 -- Ensure_Defined --
1010 --------------------
1012 procedure Ensure_Defined
(Typ
: Entity_Id
; N
: Node_Id
) is
1017 if Is_Itype
(Typ
) then
1018 IR
:= Make_Itype_Reference
(Sloc
(N
));
1019 Set_Itype
(IR
, Typ
);
1021 if not In_Open_Scopes
(Scope
(Typ
))
1022 and then Is_Subprogram
(Current_Scope
)
1023 and then Scope
(Current_Scope
) /= Standard_Standard
1025 -- Insert node in front of subprogram, to avoid scope anomalies
1031 and then Nkind
(P
) /= N_Subprogram_Body
1037 Insert_Action
(P
, IR
);
1039 Insert_Action
(N
, IR
);
1043 Insert_Action
(N
, IR
);
1048 ---------------------
1049 -- Evolve_And_Then --
1050 ---------------------
1052 procedure Evolve_And_Then
(Cond
: in out Node_Id
; Cond1
: Node_Id
) is
1058 Make_And_Then
(Sloc
(Cond1
),
1060 Right_Opnd
=> Cond1
);
1062 end Evolve_And_Then
;
1064 --------------------
1065 -- Evolve_Or_Else --
1066 --------------------
1068 procedure Evolve_Or_Else
(Cond
: in out Node_Id
; Cond1
: Node_Id
) is
1074 Make_Or_Else
(Sloc
(Cond1
),
1076 Right_Opnd
=> Cond1
);
1080 ------------------------------
1081 -- Expand_Subtype_From_Expr --
1082 ------------------------------
1084 -- This function is applicable for both static and dynamic allocation of
1085 -- objects which are constrained by an initial expression. Basically it
1086 -- transforms an unconstrained subtype indication into a constrained one.
1087 -- The expression may also be transformed in certain cases in order to
1088 -- avoid multiple evaulation. In the static allocation case, the general
1093 -- is transformed into
1095 -- Val : Constrained_Subtype_of_T := Maybe_Modified_Expr;
1097 -- Here are the main cases :
1099 -- <if Expr is a Slice>
1100 -- Val : T ([Index_Subtype (Expr)]) := Expr;
1102 -- <elsif Expr is a String Literal>
1103 -- Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
1105 -- <elsif Expr is Constrained>
1106 -- subtype T is Type_Of_Expr
1109 -- <elsif Expr is an entity_name>
1110 -- Val : T (constraints taken from Expr) := Expr;
1113 -- type Axxx is access all T;
1114 -- Rval : Axxx := Expr'ref;
1115 -- Val : T (constraints taken from Rval) := Rval.all;
1117 -- ??? note: when the Expression is allocated in the secondary stack
1118 -- we could use it directly instead of copying it by declaring
1119 -- Val : T (...) renames Rval.all
1121 procedure Expand_Subtype_From_Expr
1123 Unc_Type
: Entity_Id
;
1124 Subtype_Indic
: Node_Id
;
1127 Loc
: constant Source_Ptr
:= Sloc
(N
);
1128 Exp_Typ
: constant Entity_Id
:= Etype
(Exp
);
1132 -- In general we cannot build the subtype if expansion is disabled,
1133 -- because internal entities may not have been defined. However, to
1134 -- avoid some cascaded errors, we try to continue when the expression
1135 -- is an array (or string), because it is safe to compute the bounds.
1136 -- It is in fact required to do so even in a generic context, because
1137 -- there may be constants that depend on bounds of string literal.
1139 if not Expander_Active
1140 and then (No
(Etype
(Exp
))
1141 or else Base_Type
(Etype
(Exp
)) /= Standard_String
)
1146 if Nkind
(Exp
) = N_Slice
then
1148 Slice_Type
: constant Entity_Id
:= Etype
(First_Index
(Exp_Typ
));
1151 Rewrite
(Subtype_Indic
,
1152 Make_Subtype_Indication
(Loc
,
1153 Subtype_Mark
=> New_Reference_To
(Unc_Type
, Loc
),
1155 Make_Index_Or_Discriminant_Constraint
(Loc
,
1156 Constraints
=> New_List
1157 (New_Reference_To
(Slice_Type
, Loc
)))));
1159 -- This subtype indication may be used later for contraint checks
1160 -- we better make sure that if a variable was used as a bound of
1161 -- of the original slice, its value is frozen.
1163 Force_Evaluation
(Low_Bound
(Scalar_Range
(Slice_Type
)));
1164 Force_Evaluation
(High_Bound
(Scalar_Range
(Slice_Type
)));
1167 elsif Ekind
(Exp_Typ
) = E_String_Literal_Subtype
then
1168 Rewrite
(Subtype_Indic
,
1169 Make_Subtype_Indication
(Loc
,
1170 Subtype_Mark
=> New_Reference_To
(Unc_Type
, Loc
),
1172 Make_Index_Or_Discriminant_Constraint
(Loc
,
1173 Constraints
=> New_List
(
1174 Make_Literal_Range
(Loc
,
1175 Literal_Typ
=> Exp_Typ
)))));
1177 elsif Is_Constrained
(Exp_Typ
)
1178 and then not Is_Class_Wide_Type
(Unc_Type
)
1180 if Is_Itype
(Exp_Typ
) then
1182 -- No need to generate a new one.
1188 Make_Defining_Identifier
(Loc
,
1189 Chars
=> New_Internal_Name
('T'));
1192 Make_Subtype_Declaration
(Loc
,
1193 Defining_Identifier
=> T
,
1194 Subtype_Indication
=> New_Reference_To
(Exp_Typ
, Loc
)));
1196 -- This type is marked as an itype even though it has an
1197 -- explicit declaration because otherwise it can be marked
1198 -- with Is_Generic_Actual_Type and generate spurious errors.
1199 -- (see sem_ch8.Analyze_Package_Renaming and sem_type.covers)
1202 Set_Associated_Node_For_Itype
(T
, Exp
);
1205 Rewrite
(Subtype_Indic
, New_Reference_To
(T
, Loc
));
1207 -- nothing needs to be done for private types with unknown discriminants
1208 -- if the underlying type is not an unconstrained composite type.
1210 elsif Is_Private_Type
(Unc_Type
)
1211 and then Has_Unknown_Discriminants
(Unc_Type
)
1212 and then (not Is_Composite_Type
(Underlying_Type
(Unc_Type
))
1213 or else Is_Constrained
(Underlying_Type
(Unc_Type
)))
1218 Remove_Side_Effects
(Exp
);
1219 Rewrite
(Subtype_Indic
,
1220 Make_Subtype_From_Expr
(Exp
, Unc_Type
));
1222 end Expand_Subtype_From_Expr
;
1228 function Find_Prim_Op
(T
: Entity_Id
; Name
: Name_Id
) return Entity_Id
is
1230 Typ
: Entity_Id
:= T
;
1233 if Is_Class_Wide_Type
(Typ
) then
1234 Typ
:= Root_Type
(Typ
);
1237 Typ
:= Underlying_Type
(Typ
);
1239 Prim
:= First_Elmt
(Primitive_Operations
(Typ
));
1240 while Chars
(Node
(Prim
)) /= Name
loop
1242 pragma Assert
(Present
(Prim
));
1248 function Find_Prim_Op
1250 Name
: TSS_Name_Type
) return Entity_Id
1253 Typ
: Entity_Id
:= T
;
1256 if Is_Class_Wide_Type
(Typ
) then
1257 Typ
:= Root_Type
(Typ
);
1260 Typ
:= Underlying_Type
(Typ
);
1262 Prim
:= First_Elmt
(Primitive_Operations
(Typ
));
1263 while not Is_TSS
(Node
(Prim
), Name
) loop
1265 pragma Assert
(Present
(Prim
));
1271 ----------------------
1272 -- Force_Evaluation --
1273 ----------------------
1275 procedure Force_Evaluation
(Exp
: Node_Id
; Name_Req
: Boolean := False) is
1277 Remove_Side_Effects
(Exp
, Name_Req
, Variable_Ref
=> True);
1278 end Force_Evaluation
;
1280 ------------------------
1281 -- Generate_Poll_Call --
1282 ------------------------
1284 procedure Generate_Poll_Call
(N
: Node_Id
) is
1286 -- No poll call if polling not active
1288 if not Polling_Required
then
1291 -- Otherwise generate require poll call
1294 Insert_Before_And_Analyze
(N
,
1295 Make_Procedure_Call_Statement
(Sloc
(N
),
1296 Name
=> New_Occurrence_Of
(RTE
(RE_Poll
), Sloc
(N
))));
1298 end Generate_Poll_Call
;
1300 ---------------------------------
1301 -- Get_Current_Value_Condition --
1302 ---------------------------------
1304 procedure Get_Current_Value_Condition
1309 Loc
: constant Source_Ptr
:= Sloc
(Var
);
1310 CV
: constant Node_Id
:= Current_Value
(Entity
(Var
));
1319 -- If statement. Condition is known true in THEN section, known False
1320 -- in any ELSIF or ELSE part, and unknown outside the IF statement.
1322 if Nkind
(CV
) = N_If_Statement
then
1324 -- Before start of IF statement
1326 if Loc
< Sloc
(CV
) then
1329 -- After end of IF statement
1331 elsif Loc
>= Sloc
(CV
) + Text_Ptr
(UI_To_Int
(End_Span
(CV
))) then
1335 -- At this stage we know that we are within the IF statement, but
1336 -- unfortunately, the tree does not record the SLOC of the ELSE so
1337 -- we cannot use a simple SLOC comparison to distinguish between
1338 -- the then/else statements, so we have to climb the tree.
1345 while Parent
(N
) /= CV
loop
1348 -- If we fall off the top of the tree, then that's odd, but
1349 -- perhaps it could occur in some error situation, and the
1350 -- safest response is simply to assume that the outcome of
1351 -- the condition is unknown. No point in bombing during an
1352 -- attempt to optimize things.
1359 -- Now we have N pointing to a node whose parent is the IF
1360 -- statement in question, so now we can tell if we are within
1361 -- the THEN statements.
1363 if Is_List_Member
(N
)
1364 and then List_Containing
(N
) = Then_Statements
(CV
)
1368 -- Otherwise we must be in ELSIF or ELSE part
1375 -- ELSIF part. Condition is known true within the referenced
1376 -- ELSIF, known False in any subsequent ELSIF or ELSE part,
1377 -- and unknown before the ELSE part or after the IF statement.
1379 elsif Nkind
(CV
) = N_Elsif_Part
then
1382 -- Before start of ELSIF part
1384 if Loc
< Sloc
(CV
) then
1387 -- After end of IF statement
1389 elsif Loc
>= Sloc
(Stm
) +
1390 Text_Ptr
(UI_To_Int
(End_Span
(Stm
)))
1395 -- Again we lack the SLOC of the ELSE, so we need to climb the
1396 -- tree to see if we are within the ELSIF part in question.
1403 while Parent
(N
) /= Stm
loop
1406 -- If we fall off the top of the tree, then that's odd, but
1407 -- perhaps it could occur in some error situation, and the
1408 -- safest response is simply to assume that the outcome of
1409 -- the condition is unknown. No point in bombing during an
1410 -- attempt to optimize things.
1417 -- Now we have N pointing to a node whose parent is the IF
1418 -- statement in question, so see if is the ELSIF part we want.
1419 -- the THEN statements.
1424 -- Otherwise we must be in susbequent ELSIF or ELSE part
1431 -- All other cases of Current_Value settings
1437 -- If we fall through here, then we have a reportable
1438 -- condition, Sens is True if the condition is true and
1439 -- False if it needs inverting.
1441 Cond
:= Condition
(CV
);
1443 -- Deal with NOT operators, inverting sense
1445 while Nkind
(Cond
) = N_Op_Not
loop
1446 Cond
:= Right_Opnd
(Cond
);
1450 -- Now we must have a relational operator
1452 pragma Assert
(Entity
(Var
) = Entity
(Left_Opnd
(Cond
)));
1453 Val
:= Right_Opnd
(Cond
);
1456 if Sens
= False then
1458 when N_Op_Eq
=> Op
:= N_Op_Ne
;
1459 when N_Op_Ne
=> Op
:= N_Op_Eq
;
1460 when N_Op_Lt
=> Op
:= N_Op_Ge
;
1461 when N_Op_Gt
=> Op
:= N_Op_Le
;
1462 when N_Op_Le
=> Op
:= N_Op_Gt
;
1463 when N_Op_Ge
=> Op
:= N_Op_Lt
;
1465 -- No other entry should be possible
1468 raise Program_Error
;
1471 end Get_Current_Value_Condition
;
1473 --------------------
1474 -- Homonym_Number --
1475 --------------------
1477 function Homonym_Number
(Subp
: Entity_Id
) return Nat
is
1483 Hom
:= Homonym
(Subp
);
1484 while Present
(Hom
) loop
1485 if Scope
(Hom
) = Scope
(Subp
) then
1489 Hom
:= Homonym
(Hom
);
1495 ------------------------------
1496 -- In_Unconditional_Context --
1497 ------------------------------
1499 function In_Unconditional_Context
(Node
: Node_Id
) return Boolean is
1504 while Present
(P
) loop
1506 when N_Subprogram_Body
=>
1509 when N_If_Statement
=>
1512 when N_Loop_Statement
=>
1515 when N_Case_Statement
=>
1524 end In_Unconditional_Context
;
1530 procedure Insert_Action
(Assoc_Node
: Node_Id
; Ins_Action
: Node_Id
) is
1532 if Present
(Ins_Action
) then
1533 Insert_Actions
(Assoc_Node
, New_List
(Ins_Action
));
1537 -- Version with check(s) suppressed
1539 procedure Insert_Action
1540 (Assoc_Node
: Node_Id
; Ins_Action
: Node_Id
; Suppress
: Check_Id
)
1543 Insert_Actions
(Assoc_Node
, New_List
(Ins_Action
), Suppress
);
1546 --------------------
1547 -- Insert_Actions --
1548 --------------------
1550 procedure Insert_Actions
(Assoc_Node
: Node_Id
; Ins_Actions
: List_Id
) is
1554 Wrapped_Node
: Node_Id
:= Empty
;
1557 if No
(Ins_Actions
) or else Is_Empty_List
(Ins_Actions
) then
1561 -- Ignore insert of actions from inside default expression in the
1562 -- special preliminary analyze mode. Any insertions at this point
1563 -- have no relevance, since we are only doing the analyze to freeze
1564 -- the types of any static expressions. See section "Handling of
1565 -- Default Expressions" in the spec of package Sem for further details.
1567 if In_Default_Expression
then
1571 -- If the action derives from stuff inside a record, then the actions
1572 -- are attached to the current scope, to be inserted and analyzed on
1573 -- exit from the scope. The reason for this is that we may also
1574 -- be generating freeze actions at the same time, and they must
1575 -- eventually be elaborated in the correct order.
1577 if Is_Record_Type
(Current_Scope
)
1578 and then not Is_Frozen
(Current_Scope
)
1580 if No
(Scope_Stack
.Table
1581 (Scope_Stack
.Last
).Pending_Freeze_Actions
)
1583 Scope_Stack
.Table
(Scope_Stack
.Last
).Pending_Freeze_Actions
:=
1588 Scope_Stack
.Table
(Scope_Stack
.Last
).Pending_Freeze_Actions
);
1594 -- We now intend to climb up the tree to find the right point to
1595 -- insert the actions. We start at Assoc_Node, unless this node is
1596 -- a subexpression in which case we start with its parent. We do this
1597 -- for two reasons. First it speeds things up. Second, if Assoc_Node
1598 -- is itself one of the special nodes like N_And_Then, then we assume
1599 -- that an initial request to insert actions for such a node does not
1600 -- expect the actions to get deposited in the node for later handling
1601 -- when the node is expanded, since clearly the node is being dealt
1602 -- with by the caller. Note that in the subexpression case, N is
1603 -- always the child we came from.
1605 -- N_Raise_xxx_Error is an annoying special case, it is a statement
1606 -- if it has type Standard_Void_Type, and a subexpression otherwise.
1607 -- otherwise. Procedure attribute references are also statements.
1609 if Nkind
(Assoc_Node
) in N_Subexpr
1610 and then (Nkind
(Assoc_Node
) in N_Raise_xxx_Error
1611 or else Etype
(Assoc_Node
) /= Standard_Void_Type
)
1612 and then (Nkind
(Assoc_Node
) /= N_Attribute_Reference
1614 not Is_Procedure_Attribute_Name
1615 (Attribute_Name
(Assoc_Node
)))
1617 P
:= Assoc_Node
; -- ??? does not agree with above!
1618 N
:= Parent
(Assoc_Node
);
1620 -- Non-subexpression case. Note that N is initially Empty in this
1621 -- case (N is only guaranteed Non-Empty in the subexpr case).
1628 -- Capture root of the transient scope
1630 if Scope_Is_Transient
then
1631 Wrapped_Node
:= Node_To_Be_Wrapped
;
1635 pragma Assert
(Present
(P
));
1639 -- Case of right operand of AND THEN or OR ELSE. Put the actions
1640 -- in the Actions field of the right operand. They will be moved
1641 -- out further when the AND THEN or OR ELSE operator is expanded.
1642 -- Nothing special needs to be done for the left operand since
1643 -- in that case the actions are executed unconditionally.
1645 when N_And_Then | N_Or_Else
=>
1646 if N
= Right_Opnd
(P
) then
1647 if Present
(Actions
(P
)) then
1648 Insert_List_After_And_Analyze
1649 (Last
(Actions
(P
)), Ins_Actions
);
1651 Set_Actions
(P
, Ins_Actions
);
1652 Analyze_List
(Actions
(P
));
1658 -- Then or Else operand of conditional expression. Add actions to
1659 -- Then_Actions or Else_Actions field as appropriate. The actions
1660 -- will be moved further out when the conditional is expanded.
1662 when N_Conditional_Expression
=>
1664 ThenX
: constant Node_Id
:= Next
(First
(Expressions
(P
)));
1665 ElseX
: constant Node_Id
:= Next
(ThenX
);
1668 -- Actions belong to the then expression, temporarily
1669 -- place them as Then_Actions of the conditional expr.
1670 -- They will be moved to the proper place later when
1671 -- the conditional expression is expanded.
1674 if Present
(Then_Actions
(P
)) then
1675 Insert_List_After_And_Analyze
1676 (Last
(Then_Actions
(P
)), Ins_Actions
);
1678 Set_Then_Actions
(P
, Ins_Actions
);
1679 Analyze_List
(Then_Actions
(P
));
1684 -- Actions belong to the else expression, temporarily
1685 -- place them as Else_Actions of the conditional expr.
1686 -- They will be moved to the proper place later when
1687 -- the conditional expression is expanded.
1689 elsif N
= ElseX
then
1690 if Present
(Else_Actions
(P
)) then
1691 Insert_List_After_And_Analyze
1692 (Last
(Else_Actions
(P
)), Ins_Actions
);
1694 Set_Else_Actions
(P
, Ins_Actions
);
1695 Analyze_List
(Else_Actions
(P
));
1700 -- Actions belong to the condition. In this case they are
1701 -- unconditionally executed, and so we can continue the
1702 -- search for the proper insert point.
1709 -- Case of appearing in the condition of a while expression or
1710 -- elsif. We insert the actions into the Condition_Actions field.
1711 -- They will be moved further out when the while loop or elsif
1714 when N_Iteration_Scheme |
1717 if N
= Condition
(P
) then
1718 if Present
(Condition_Actions
(P
)) then
1719 Insert_List_After_And_Analyze
1720 (Last
(Condition_Actions
(P
)), Ins_Actions
);
1722 Set_Condition_Actions
(P
, Ins_Actions
);
1724 -- Set the parent of the insert actions explicitly.
1725 -- This is not a syntactic field, but we need the
1726 -- parent field set, in particular so that freeze
1727 -- can understand that it is dealing with condition
1728 -- actions, and properly insert the freezing actions.
1730 Set_Parent
(Ins_Actions
, P
);
1731 Analyze_List
(Condition_Actions
(P
));
1737 -- Statements, declarations, pragmas, representation clauses.
1742 N_Procedure_Call_Statement |
1743 N_Statement_Other_Than_Procedure_Call |
1749 -- Representation_Clause
1752 N_Attribute_Definition_Clause |
1753 N_Enumeration_Representation_Clause |
1754 N_Record_Representation_Clause |
1758 N_Abstract_Subprogram_Declaration |
1760 N_Exception_Declaration |
1761 N_Exception_Renaming_Declaration |
1762 N_Formal_Object_Declaration |
1763 N_Formal_Subprogram_Declaration |
1764 N_Formal_Type_Declaration |
1765 N_Full_Type_Declaration |
1766 N_Function_Instantiation |
1767 N_Generic_Function_Renaming_Declaration |
1768 N_Generic_Package_Declaration |
1769 N_Generic_Package_Renaming_Declaration |
1770 N_Generic_Procedure_Renaming_Declaration |
1771 N_Generic_Subprogram_Declaration |
1772 N_Implicit_Label_Declaration |
1773 N_Incomplete_Type_Declaration |
1774 N_Number_Declaration |
1775 N_Object_Declaration |
1776 N_Object_Renaming_Declaration |
1778 N_Package_Body_Stub |
1779 N_Package_Declaration |
1780 N_Package_Instantiation |
1781 N_Package_Renaming_Declaration |
1782 N_Private_Extension_Declaration |
1783 N_Private_Type_Declaration |
1784 N_Procedure_Instantiation |
1785 N_Protected_Body_Stub |
1786 N_Protected_Type_Declaration |
1787 N_Single_Task_Declaration |
1789 N_Subprogram_Body_Stub |
1790 N_Subprogram_Declaration |
1791 N_Subprogram_Renaming_Declaration |
1792 N_Subtype_Declaration |
1795 N_Task_Type_Declaration |
1797 -- Freeze entity behaves like a declaration or statement
1801 -- Do not insert here if the item is not a list member (this
1802 -- happens for example with a triggering statement, and the
1803 -- proper approach is to insert before the entire select).
1805 if not Is_List_Member
(P
) then
1808 -- Do not insert if parent of P is an N_Component_Association
1809 -- node (i.e. we are in the context of an N_Aggregate node.
1810 -- In this case we want to insert before the entire aggregate.
1812 elsif Nkind
(Parent
(P
)) = N_Component_Association
then
1815 -- Do not insert if the parent of P is either an N_Variant
1816 -- node or an N_Record_Definition node, meaning in either
1817 -- case that P is a member of a component list, and that
1818 -- therefore the actions should be inserted outside the
1819 -- complete record declaration.
1821 elsif Nkind
(Parent
(P
)) = N_Variant
1822 or else Nkind
(Parent
(P
)) = N_Record_Definition
1826 -- Do not insert freeze nodes within the loop generated for
1827 -- an aggregate, because they may be elaborated too late for
1828 -- subsequent use in the back end: within a package spec the
1829 -- loop is part of the elaboration procedure and is only
1830 -- elaborated during the second pass.
1831 -- If the loop comes from source, or the entity is local to
1832 -- the loop itself it must remain within.
1834 elsif Nkind
(Parent
(P
)) = N_Loop_Statement
1835 and then not Comes_From_Source
(Parent
(P
))
1836 and then Nkind
(First
(Ins_Actions
)) = N_Freeze_Entity
1838 Scope
(Entity
(First
(Ins_Actions
))) /= Current_Scope
1842 -- Otherwise we can go ahead and do the insertion
1844 elsif P
= Wrapped_Node
then
1845 Store_Before_Actions_In_Scope
(Ins_Actions
);
1849 Insert_List_Before_And_Analyze
(P
, Ins_Actions
);
1853 -- A special case, N_Raise_xxx_Error can act either as a
1854 -- statement or a subexpression. We tell the difference
1855 -- by looking at the Etype. It is set to Standard_Void_Type
1856 -- in the statement case.
1859 N_Raise_xxx_Error
=>
1860 if Etype
(P
) = Standard_Void_Type
then
1861 if P
= Wrapped_Node
then
1862 Store_Before_Actions_In_Scope
(Ins_Actions
);
1864 Insert_List_Before_And_Analyze
(P
, Ins_Actions
);
1869 -- In the subexpression case, keep climbing
1875 -- If a component association appears within a loop created for
1876 -- an array aggregate, attach the actions to the association so
1877 -- they can be subsequently inserted within the loop. For other
1878 -- component associations insert outside of the aggregate. For
1879 -- an association that will generate a loop, its Loop_Actions
1880 -- attribute is already initialized (see exp_aggr.adb).
1882 -- The list of loop_actions can in turn generate additional ones,
1883 -- that are inserted before the associated node. If the associated
1884 -- node is outside the aggregate, the new actions are collected
1885 -- at the end of the loop actions, to respect the order in which
1886 -- they are to be elaborated.
1889 N_Component_Association
=>
1890 if Nkind
(Parent
(P
)) = N_Aggregate
1891 and then Present
(Loop_Actions
(P
))
1893 if Is_Empty_List
(Loop_Actions
(P
)) then
1894 Set_Loop_Actions
(P
, Ins_Actions
);
1895 Analyze_List
(Ins_Actions
);
1899 Decl
: Node_Id
:= Assoc_Node
;
1902 -- Check whether these actions were generated
1903 -- by a declaration that is part of the loop_
1904 -- actions for the component_association.
1906 while Present
(Decl
) loop
1907 exit when Parent
(Decl
) = P
1908 and then Is_List_Member
(Decl
)
1910 List_Containing
(Decl
) = Loop_Actions
(P
);
1911 Decl
:= Parent
(Decl
);
1914 if Present
(Decl
) then
1915 Insert_List_Before_And_Analyze
1916 (Decl
, Ins_Actions
);
1918 Insert_List_After_And_Analyze
1919 (Last
(Loop_Actions
(P
)), Ins_Actions
);
1930 -- Another special case, an attribute denoting a procedure call
1933 N_Attribute_Reference
=>
1934 if Is_Procedure_Attribute_Name
(Attribute_Name
(P
)) then
1935 if P
= Wrapped_Node
then
1936 Store_Before_Actions_In_Scope
(Ins_Actions
);
1938 Insert_List_Before_And_Analyze
(P
, Ins_Actions
);
1943 -- In the subexpression case, keep climbing
1949 -- For all other node types, keep climbing tree
1953 N_Accept_Alternative |
1954 N_Access_Definition |
1955 N_Access_Function_Definition |
1956 N_Access_Procedure_Definition |
1957 N_Access_To_Object_Definition |
1960 N_Case_Statement_Alternative |
1961 N_Character_Literal |
1962 N_Compilation_Unit |
1963 N_Compilation_Unit_Aux |
1964 N_Component_Clause |
1965 N_Component_Declaration |
1967 N_Constrained_Array_Definition |
1968 N_Decimal_Fixed_Point_Definition |
1969 N_Defining_Character_Literal |
1970 N_Defining_Identifier |
1971 N_Defining_Operator_Symbol |
1972 N_Defining_Program_Unit_Name |
1973 N_Delay_Alternative |
1974 N_Delta_Constraint |
1975 N_Derived_Type_Definition |
1977 N_Digits_Constraint |
1978 N_Discriminant_Association |
1979 N_Discriminant_Specification |
1981 N_Entry_Body_Formal_Part |
1982 N_Entry_Call_Alternative |
1983 N_Entry_Declaration |
1984 N_Entry_Index_Specification |
1985 N_Enumeration_Type_Definition |
1987 N_Exception_Handler |
1989 N_Explicit_Dereference |
1990 N_Extension_Aggregate |
1991 N_Floating_Point_Definition |
1992 N_Formal_Decimal_Fixed_Point_Definition |
1993 N_Formal_Derived_Type_Definition |
1994 N_Formal_Discrete_Type_Definition |
1995 N_Formal_Floating_Point_Definition |
1996 N_Formal_Modular_Type_Definition |
1997 N_Formal_Ordinary_Fixed_Point_Definition |
1998 N_Formal_Package_Declaration |
1999 N_Formal_Private_Type_Definition |
2000 N_Formal_Signed_Integer_Type_Definition |
2002 N_Function_Specification |
2003 N_Generic_Association |
2004 N_Handled_Sequence_Of_Statements |
2007 N_Index_Or_Discriminant_Constraint |
2008 N_Indexed_Component |
2012 N_Loop_Parameter_Specification |
2014 N_Modular_Type_Definition |
2040 N_Op_Shift_Right_Arithmetic |
2044 N_Ordinary_Fixed_Point_Definition |
2046 N_Package_Specification |
2047 N_Parameter_Association |
2048 N_Parameter_Specification |
2049 N_Pragma_Argument_Association |
2050 N_Procedure_Specification |
2052 N_Protected_Definition |
2053 N_Qualified_Expression |
2055 N_Range_Constraint |
2057 N_Real_Range_Specification |
2058 N_Record_Definition |
2060 N_Selected_Component |
2061 N_Signed_Integer_Type_Definition |
2062 N_Single_Protected_Declaration |
2066 N_Subtype_Indication |
2069 N_Terminate_Alternative |
2070 N_Triggering_Alternative |
2072 N_Unchecked_Expression |
2073 N_Unchecked_Type_Conversion |
2074 N_Unconstrained_Array_Definition |
2077 N_Use_Package_Clause |
2081 N_Validate_Unchecked_Conversion |
2089 -- Make sure that inserted actions stay in the transient scope
2091 if P
= Wrapped_Node
then
2092 Store_Before_Actions_In_Scope
(Ins_Actions
);
2096 -- If we fall through above tests, keep climbing tree
2100 if Nkind
(Parent
(N
)) = N_Subunit
then
2102 -- This is the proper body corresponding to a stub. Insertion
2103 -- must be done at the point of the stub, which is in the decla-
2104 -- tive part of the parent unit.
2106 P
:= Corresponding_Stub
(Parent
(N
));
2115 -- Version with check(s) suppressed
2117 procedure Insert_Actions
2118 (Assoc_Node
: Node_Id
; Ins_Actions
: List_Id
; Suppress
: Check_Id
)
2121 if Suppress
= All_Checks
then
2123 Svg
: constant Suppress_Array
:= Scope_Suppress
;
2126 Scope_Suppress
:= (others => True);
2127 Insert_Actions
(Assoc_Node
, Ins_Actions
);
2128 Scope_Suppress
:= Svg
;
2133 Svg
: constant Boolean := Scope_Suppress
(Suppress
);
2136 Scope_Suppress
(Suppress
) := True;
2137 Insert_Actions
(Assoc_Node
, Ins_Actions
);
2138 Scope_Suppress
(Suppress
) := Svg
;
2143 --------------------------
2144 -- Insert_Actions_After --
2145 --------------------------
2147 procedure Insert_Actions_After
2148 (Assoc_Node
: Node_Id
;
2149 Ins_Actions
: List_Id
)
2152 if Scope_Is_Transient
2153 and then Assoc_Node
= Node_To_Be_Wrapped
2155 Store_After_Actions_In_Scope
(Ins_Actions
);
2157 Insert_List_After_And_Analyze
(Assoc_Node
, Ins_Actions
);
2159 end Insert_Actions_After
;
2161 ---------------------------------
2162 -- Insert_Library_Level_Action --
2163 ---------------------------------
2165 procedure Insert_Library_Level_Action
(N
: Node_Id
) is
2166 Aux
: constant Node_Id
:= Aux_Decls_Node
(Cunit
(Main_Unit
));
2169 New_Scope
(Cunit_Entity
(Main_Unit
));
2171 if No
(Actions
(Aux
)) then
2172 Set_Actions
(Aux
, New_List
(N
));
2174 Append
(N
, Actions
(Aux
));
2179 end Insert_Library_Level_Action
;
2181 ----------------------------------
2182 -- Insert_Library_Level_Actions --
2183 ----------------------------------
2185 procedure Insert_Library_Level_Actions
(L
: List_Id
) is
2186 Aux
: constant Node_Id
:= Aux_Decls_Node
(Cunit
(Main_Unit
));
2189 if Is_Non_Empty_List
(L
) then
2190 New_Scope
(Cunit_Entity
(Main_Unit
));
2192 if No
(Actions
(Aux
)) then
2193 Set_Actions
(Aux
, L
);
2196 Insert_List_After_And_Analyze
(Last
(Actions
(Aux
)), L
);
2201 end Insert_Library_Level_Actions
;
2203 ----------------------
2204 -- Inside_Init_Proc --
2205 ----------------------
2207 function Inside_Init_Proc
return Boolean is
2213 and then S
/= Standard_Standard
2215 if Is_Init_Proc
(S
) then
2223 end Inside_Init_Proc
;
2225 ----------------------------
2226 -- Is_All_Null_Statements --
2227 ----------------------------
2229 function Is_All_Null_Statements
(L
: List_Id
) return Boolean is
2234 while Present
(Stm
) loop
2235 if Nkind
(Stm
) /= N_Null_Statement
then
2243 end Is_All_Null_Statements
;
2245 ----------------------------------
2246 -- Is_Possibly_Unaligned_Object --
2247 ----------------------------------
2249 function Is_Possibly_Unaligned_Object
(P
: Node_Id
) return Boolean is
2251 -- If target does not have strict alignment, result is always
2252 -- False, since correctness of code does no depend on alignment.
2254 if not Target_Strict_Alignment
then
2258 -- If renamed object, apply test to underlying object
2260 if Is_Entity_Name
(P
)
2261 and then Is_Object
(Entity
(P
))
2262 and then Present
(Renamed_Object
(Entity
(P
)))
2264 return Is_Possibly_Unaligned_Object
(Renamed_Object
(Entity
(P
)));
2267 -- If this is an element of a packed array, may be unaligned
2269 if Is_Ref_To_Bit_Packed_Array
(P
) then
2273 -- Case of component reference
2275 if Nkind
(P
) = N_Selected_Component
then
2277 -- If component reference is for a record that is bit packed
2278 -- or has a specified alignment (that might be too small) or
2279 -- the component reference has a component clause, then the
2280 -- object may be unaligned.
2282 if Is_Packed
(Etype
(Prefix
(P
)))
2283 or else Known_Alignment
(Etype
(Prefix
(P
)))
2284 or else Present
(Component_Clause
(Entity
(Selector_Name
(P
))))
2288 -- Otherwise, for a component reference, test prefix
2291 return Is_Possibly_Unaligned_Object
(Prefix
(P
));
2294 -- If not a component reference, must be aligned
2299 end Is_Possibly_Unaligned_Object
;
2301 ---------------------------------
2302 -- Is_Possibly_Unaligned_Slice --
2303 ---------------------------------
2305 function Is_Possibly_Unaligned_Slice
(P
: Node_Id
) return Boolean is
2307 if Is_Entity_Name
(P
)
2308 and then Is_Object
(Entity
(P
))
2309 and then Present
(Renamed_Object
(Entity
(P
)))
2311 return Is_Possibly_Unaligned_Slice
(Renamed_Object
(Entity
(P
)));
2314 -- We only need to worry if the target has strict alignment, unless
2315 -- it is a nested record component with a component clause, which
2316 -- Gigi does not handle well. This patch should disappear with GCC 3.0
2317 -- and it is not clear why it is needed even when the representation
2318 -- clause is a confirming one, but in its absence gigi complains that
2319 -- the slice is not addressable.???
2321 if not Target_Strict_Alignment
then
2322 if Nkind
(P
) /= N_Slice
2323 or else Nkind
(Prefix
(P
)) /= N_Selected_Component
2324 or else Nkind
(Prefix
(Prefix
(P
))) /= N_Selected_Component
2330 -- The reference must be a slice
2332 if Nkind
(P
) /= N_Slice
then
2336 -- If it is a slice, then look at the array type being sliced
2339 Pref
: constant Node_Id
:= Prefix
(P
);
2340 Typ
: constant Entity_Id
:= Etype
(Prefix
(P
));
2343 -- The worrisome case is one where we don't know the alignment
2344 -- of the array, or we know it and it is greater than 1 (if the
2345 -- alignment is one, then obviously it cannot be misaligned).
2347 if Known_Alignment
(Typ
) and then Alignment
(Typ
) = 1 then
2351 -- The only way we can be unaligned is if the array being sliced
2352 -- is a component of a record, and either the record is packed,
2353 -- or the component has a component clause, or the record has
2354 -- a specified alignment (that might be too small).
2357 Nkind
(Pref
) = N_Selected_Component
2359 (Is_Packed
(Etype
(Prefix
(Pref
)))
2361 Known_Alignment
(Etype
(Prefix
(Pref
)))
2363 Present
(Component_Clause
(Entity
(Selector_Name
(Pref
)))));
2365 end Is_Possibly_Unaligned_Slice
;
2367 --------------------------------
2368 -- Is_Ref_To_Bit_Packed_Array --
2369 --------------------------------
2371 function Is_Ref_To_Bit_Packed_Array
(P
: Node_Id
) return Boolean is
2376 if Is_Entity_Name
(P
)
2377 and then Is_Object
(Entity
(P
))
2378 and then Present
(Renamed_Object
(Entity
(P
)))
2380 return Is_Ref_To_Bit_Packed_Array
(Renamed_Object
(Entity
(P
)));
2383 if Nkind
(P
) = N_Indexed_Component
2385 Nkind
(P
) = N_Selected_Component
2387 if Is_Bit_Packed_Array
(Etype
(Prefix
(P
))) then
2390 Result
:= Is_Ref_To_Bit_Packed_Array
(Prefix
(P
));
2393 if Result
and then Nkind
(P
) = N_Indexed_Component
then
2394 Expr
:= First
(Expressions
(P
));
2396 while Present
(Expr
) loop
2397 Force_Evaluation
(Expr
);
2407 end Is_Ref_To_Bit_Packed_Array
;
2409 --------------------------------
2410 -- Is_Ref_To_Bit_Packed_Slice --
2411 --------------------------------
2413 function Is_Ref_To_Bit_Packed_Slice
(P
: Node_Id
) return Boolean is
2415 if Is_Entity_Name
(P
)
2416 and then Is_Object
(Entity
(P
))
2417 and then Present
(Renamed_Object
(Entity
(P
)))
2419 return Is_Ref_To_Bit_Packed_Slice
(Renamed_Object
(Entity
(P
)));
2422 if Nkind
(P
) = N_Slice
2423 and then Is_Bit_Packed_Array
(Etype
(Prefix
(P
)))
2427 elsif Nkind
(P
) = N_Indexed_Component
2429 Nkind
(P
) = N_Selected_Component
2431 return Is_Ref_To_Bit_Packed_Slice
(Prefix
(P
));
2436 end Is_Ref_To_Bit_Packed_Slice
;
2438 -----------------------
2439 -- Is_Renamed_Object --
2440 -----------------------
2442 function Is_Renamed_Object
(N
: Node_Id
) return Boolean is
2443 Pnod
: constant Node_Id
:= Parent
(N
);
2444 Kind
: constant Node_Kind
:= Nkind
(Pnod
);
2447 if Kind
= N_Object_Renaming_Declaration
then
2450 elsif Kind
= N_Indexed_Component
2451 or else Kind
= N_Selected_Component
2453 return Is_Renamed_Object
(Pnod
);
2458 end Is_Renamed_Object
;
2460 ----------------------------
2461 -- Is_Untagged_Derivation --
2462 ----------------------------
2464 function Is_Untagged_Derivation
(T
: Entity_Id
) return Boolean is
2466 return (not Is_Tagged_Type
(T
) and then Is_Derived_Type
(T
))
2468 (Is_Private_Type
(T
) and then Present
(Full_View
(T
))
2469 and then not Is_Tagged_Type
(Full_View
(T
))
2470 and then Is_Derived_Type
(Full_View
(T
))
2471 and then Etype
(Full_View
(T
)) /= T
);
2473 end Is_Untagged_Derivation
;
2475 --------------------
2476 -- Kill_Dead_Code --
2477 --------------------
2479 procedure Kill_Dead_Code
(N
: Node_Id
) is
2482 Remove_Handler_Entries
(N
);
2483 Remove_Warning_Messages
(N
);
2485 -- Recurse into block statements and bodies to process declarations
2488 if Nkind
(N
) = N_Block_Statement
2489 or else Nkind
(N
) = N_Subprogram_Body
2490 or else Nkind
(N
) = N_Package_Body
2492 Kill_Dead_Code
(Declarations
(N
));
2493 Kill_Dead_Code
(Statements
(Handled_Statement_Sequence
(N
)));
2495 if Nkind
(N
) = N_Subprogram_Body
then
2496 Set_Is_Eliminated
(Defining_Entity
(N
));
2499 -- Recurse into composite statement to kill individual statements,
2500 -- in particular instantiations.
2502 elsif Nkind
(N
) = N_If_Statement
then
2503 Kill_Dead_Code
(Then_Statements
(N
));
2504 Kill_Dead_Code
(Elsif_Parts
(N
));
2505 Kill_Dead_Code
(Else_Statements
(N
));
2507 elsif Nkind
(N
) = N_Loop_Statement
then
2508 Kill_Dead_Code
(Statements
(N
));
2510 elsif Nkind
(N
) = N_Case_Statement
then
2512 Alt
: Node_Id
:= First
(Alternatives
(N
));
2515 while Present
(Alt
) loop
2516 Kill_Dead_Code
(Statements
(Alt
));
2521 elsif Nkind
(N
) = N_Case_Statement_Alternative
then
2522 Kill_Dead_Code
(Statements
(N
));
2524 -- Deal with dead instances caused by deleting instantiations
2526 elsif Nkind
(N
) in N_Generic_Instantiation
then
2527 Remove_Dead_Instance
(N
);
2534 -- Case where argument is a list of nodes to be killed
2536 procedure Kill_Dead_Code
(L
: List_Id
) is
2540 if Is_Non_Empty_List
(L
) then
2542 N
:= Remove_Head
(L
);
2549 ------------------------
2550 -- Known_Non_Negative --
2551 ------------------------
2553 function Known_Non_Negative
(Opnd
: Node_Id
) return Boolean is
2555 if Is_OK_Static_Expression
(Opnd
)
2556 and then Expr_Value
(Opnd
) >= 0
2562 Lo
: constant Node_Id
:= Type_Low_Bound
(Etype
(Opnd
));
2566 Is_OK_Static_Expression
(Lo
) and then Expr_Value
(Lo
) >= 0;
2569 end Known_Non_Negative
;
2571 --------------------
2572 -- Known_Non_Null --
2573 --------------------
2575 function Known_Non_Null
(N
: Node_Id
) return Boolean is
2577 pragma Assert
(Is_Access_Type
(Underlying_Type
(Etype
(N
))));
2579 -- Case of entity for which Is_Known_Non_Null is True
2581 if Is_Entity_Name
(N
) and then Is_Known_Non_Null
(Entity
(N
)) then
2583 -- If the entity is aliased or volatile, then we decide that
2584 -- we don't know it is really non-null even if the sequential
2585 -- flow indicates that it is, since such variables can be
2586 -- changed without us noticing.
2588 if Is_Aliased
(Entity
(N
))
2589 or else Treat_As_Volatile
(Entity
(N
))
2593 -- For all other cases, the flag is decisive
2599 -- True if access attribute
2601 elsif Nkind
(N
) = N_Attribute_Reference
2602 and then (Attribute_Name
(N
) = Name_Access
2604 Attribute_Name
(N
) = Name_Unchecked_Access
2606 Attribute_Name
(N
) = Name_Unrestricted_Access
)
2610 -- True if allocator
2612 elsif Nkind
(N
) = N_Allocator
then
2615 -- For a conversion, true if expression is known non-null
2617 elsif Nkind
(N
) = N_Type_Conversion
then
2618 return Known_Non_Null
(Expression
(N
));
2620 -- One more case is when Current_Value references a condition
2621 -- that ensures a non-null value.
2623 elsif Is_Entity_Name
(N
) then
2629 Get_Current_Value_Condition
(N
, Op
, Val
);
2630 return Op
= N_Op_Ne
and then Nkind
(Val
) = N_Null
;
2633 -- Above are all cases where the value could be determined to be
2634 -- non-null. In all other cases, we don't know, so return False.
2641 -----------------------------
2642 -- Make_CW_Equivalent_Type --
2643 -----------------------------
2645 -- Create a record type used as an equivalent of any member
2646 -- of the class which takes its size from exp.
2648 -- Generate the following code:
2650 -- type Equiv_T is record
2651 -- _parent : T (List of discriminant constaints taken from Exp);
2652 -- Ext__50 : Storage_Array (1 .. (Exp'size - Typ'object_size)/8);
2655 -- ??? Note that this type does not guarantee same alignment as all
2658 function Make_CW_Equivalent_Type
2663 Loc
: constant Source_Ptr
:= Sloc
(E
);
2664 Root_Typ
: constant Entity_Id
:= Root_Type
(T
);
2665 List_Def
: constant List_Id
:= Empty_List
;
2666 Equiv_Type
: Entity_Id
;
2667 Range_Type
: Entity_Id
;
2668 Str_Type
: Entity_Id
;
2669 Constr_Root
: Entity_Id
;
2673 if not Has_Discriminants
(Root_Typ
) then
2674 Constr_Root
:= Root_Typ
;
2677 Make_Defining_Identifier
(Loc
, New_Internal_Name
('R'));
2679 -- subtype cstr__n is T (List of discr constraints taken from Exp)
2681 Append_To
(List_Def
,
2682 Make_Subtype_Declaration
(Loc
,
2683 Defining_Identifier
=> Constr_Root
,
2684 Subtype_Indication
=>
2685 Make_Subtype_From_Expr
(E
, Root_Typ
)));
2688 -- subtype rg__xx is Storage_Offset range
2689 -- (Expr'size - typ'size) / Storage_Unit
2691 Range_Type
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('G'));
2694 Make_Op_Subtract
(Loc
,
2696 Make_Attribute_Reference
(Loc
,
2698 OK_Convert_To
(T
, Duplicate_Subexpr_No_Checks
(E
)),
2699 Attribute_Name
=> Name_Size
),
2701 Make_Attribute_Reference
(Loc
,
2702 Prefix
=> New_Reference_To
(Constr_Root
, Loc
),
2703 Attribute_Name
=> Name_Object_Size
));
2705 Set_Paren_Count
(Sizexpr
, 1);
2707 Append_To
(List_Def
,
2708 Make_Subtype_Declaration
(Loc
,
2709 Defining_Identifier
=> Range_Type
,
2710 Subtype_Indication
=>
2711 Make_Subtype_Indication
(Loc
,
2712 Subtype_Mark
=> New_Reference_To
(RTE
(RE_Storage_Offset
), Loc
),
2713 Constraint
=> Make_Range_Constraint
(Loc
,
2716 Low_Bound
=> Make_Integer_Literal
(Loc
, 1),
2718 Make_Op_Divide
(Loc
,
2719 Left_Opnd
=> Sizexpr
,
2720 Right_Opnd
=> Make_Integer_Literal
(Loc
,
2721 Intval
=> System_Storage_Unit
)))))));
2723 -- subtype str__nn is Storage_Array (rg__x);
2725 Str_Type
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('S'));
2726 Append_To
(List_Def
,
2727 Make_Subtype_Declaration
(Loc
,
2728 Defining_Identifier
=> Str_Type
,
2729 Subtype_Indication
=>
2730 Make_Subtype_Indication
(Loc
,
2731 Subtype_Mark
=> New_Reference_To
(RTE
(RE_Storage_Array
), Loc
),
2733 Make_Index_Or_Discriminant_Constraint
(Loc
,
2735 New_List
(New_Reference_To
(Range_Type
, Loc
))))));
2737 -- type Equiv_T is record
2742 Equiv_Type
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('T'));
2744 -- When the target requires front-end layout, it's necessary to allow
2745 -- the equivalent type to be frozen so that layout can occur (when the
2746 -- associated class-wide subtype is frozen, the equivalent type will
2747 -- be frozen, see freeze.adb). For other targets, Gigi wants to have
2748 -- the equivalent type marked as frozen and deals with this type itself.
2749 -- In the Gigi case this will also avoid the generation of an init
2750 -- procedure for the type.
2752 if not Frontend_Layout_On_Target
then
2753 Set_Is_Frozen
(Equiv_Type
);
2756 Set_Ekind
(Equiv_Type
, E_Record_Type
);
2757 Set_Parent_Subtype
(Equiv_Type
, Constr_Root
);
2759 Append_To
(List_Def
,
2760 Make_Full_Type_Declaration
(Loc
,
2761 Defining_Identifier
=> Equiv_Type
,
2764 Make_Record_Definition
(Loc
,
2765 Component_List
=> Make_Component_List
(Loc
,
2766 Component_Items
=> New_List
(
2767 Make_Component_Declaration
(Loc
,
2768 Defining_Identifier
=>
2769 Make_Defining_Identifier
(Loc
, Name_uParent
),
2770 Subtype_Indication
=> New_Reference_To
(Constr_Root
, Loc
)),
2772 Make_Component_Declaration
(Loc
,
2773 Defining_Identifier
=>
2774 Make_Defining_Identifier
(Loc
,
2775 Chars
=> New_Internal_Name
('C')),
2776 Subtype_Indication
=> New_Reference_To
(Str_Type
, Loc
))),
2777 Variant_Part
=> Empty
))));
2779 Insert_Actions
(E
, List_Def
);
2781 end Make_CW_Equivalent_Type
;
2783 ------------------------
2784 -- Make_Literal_Range --
2785 ------------------------
2787 function Make_Literal_Range
2789 Literal_Typ
: Entity_Id
)
2792 Lo
: constant Node_Id
:=
2793 New_Copy_Tree
(String_Literal_Low_Bound
(Literal_Typ
));
2796 Set_Analyzed
(Lo
, False);
2803 Make_Op_Subtract
(Loc
,
2806 Left_Opnd
=> New_Copy_Tree
(Lo
),
2808 Make_Integer_Literal
(Loc
,
2809 String_Literal_Length
(Literal_Typ
))),
2810 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1)));
2811 end Make_Literal_Range
;
2813 ----------------------------
2814 -- Make_Subtype_From_Expr --
2815 ----------------------------
2817 -- 1. If Expr is an uncontrained array expression, creates
2818 -- Unc_Type(Expr'first(1)..Expr'Last(1),..., Expr'first(n)..Expr'last(n))
2820 -- 2. If Expr is a unconstrained discriminated type expression, creates
2821 -- Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
2823 -- 3. If Expr is class-wide, creates an implicit class wide subtype
2825 function Make_Subtype_From_Expr
2827 Unc_Typ
: Entity_Id
)
2830 Loc
: constant Source_Ptr
:= Sloc
(E
);
2831 List_Constr
: constant List_Id
:= New_List
;
2834 Full_Subtyp
: Entity_Id
;
2835 Priv_Subtyp
: Entity_Id
;
2840 if Is_Private_Type
(Unc_Typ
)
2841 and then Has_Unknown_Discriminants
(Unc_Typ
)
2843 -- Prepare the subtype completion, Go to base type to
2844 -- find underlying type.
2846 Utyp
:= Underlying_Type
(Base_Type
(Unc_Typ
));
2847 Full_Subtyp
:= Make_Defining_Identifier
(Loc
,
2848 New_Internal_Name
('C'));
2850 Unchecked_Convert_To
2851 (Utyp
, Duplicate_Subexpr_No_Checks
(E
));
2852 Set_Parent
(Full_Exp
, Parent
(E
));
2855 Make_Defining_Identifier
(Loc
, New_Internal_Name
('P'));
2858 Make_Subtype_Declaration
(Loc
,
2859 Defining_Identifier
=> Full_Subtyp
,
2860 Subtype_Indication
=> Make_Subtype_From_Expr
(Full_Exp
, Utyp
)));
2862 -- Define the dummy private subtype
2864 Set_Ekind
(Priv_Subtyp
, Subtype_Kind
(Ekind
(Unc_Typ
)));
2865 Set_Etype
(Priv_Subtyp
, Unc_Typ
);
2866 Set_Scope
(Priv_Subtyp
, Full_Subtyp
);
2867 Set_Is_Constrained
(Priv_Subtyp
);
2868 Set_Is_Tagged_Type
(Priv_Subtyp
, Is_Tagged_Type
(Unc_Typ
));
2869 Set_Is_Itype
(Priv_Subtyp
);
2870 Set_Associated_Node_For_Itype
(Priv_Subtyp
, E
);
2872 if Is_Tagged_Type
(Priv_Subtyp
) then
2874 (Base_Type
(Priv_Subtyp
), Class_Wide_Type
(Unc_Typ
));
2875 Set_Primitive_Operations
(Priv_Subtyp
,
2876 Primitive_Operations
(Unc_Typ
));
2879 Set_Full_View
(Priv_Subtyp
, Full_Subtyp
);
2881 return New_Reference_To
(Priv_Subtyp
, Loc
);
2883 elsif Is_Array_Type
(Unc_Typ
) then
2884 for J
in 1 .. Number_Dimensions
(Unc_Typ
) loop
2885 Append_To
(List_Constr
,
2888 Make_Attribute_Reference
(Loc
,
2889 Prefix
=> Duplicate_Subexpr_No_Checks
(E
),
2890 Attribute_Name
=> Name_First
,
2891 Expressions
=> New_List
(
2892 Make_Integer_Literal
(Loc
, J
))),
2895 Make_Attribute_Reference
(Loc
,
2896 Prefix
=> Duplicate_Subexpr_No_Checks
(E
),
2897 Attribute_Name
=> Name_Last
,
2898 Expressions
=> New_List
(
2899 Make_Integer_Literal
(Loc
, J
)))));
2902 elsif Is_Class_Wide_Type
(Unc_Typ
) then
2904 CW_Subtype
: Entity_Id
;
2905 EQ_Typ
: Entity_Id
:= Empty
;
2908 -- A class-wide equivalent type is not needed when Java_VM
2909 -- because the JVM back end handles the class-wide object
2910 -- initialization itself (and doesn't need or want the
2911 -- additional intermediate type to handle the assignment).
2913 if Expander_Active
and then not Java_VM
then
2914 EQ_Typ
:= Make_CW_Equivalent_Type
(Unc_Typ
, E
);
2917 CW_Subtype
:= New_Class_Wide_Subtype
(Unc_Typ
, E
);
2918 Set_Equivalent_Type
(CW_Subtype
, EQ_Typ
);
2920 if Present
(EQ_Typ
) then
2921 Set_Is_Class_Wide_Equivalent_Type
(EQ_Typ
);
2924 Set_Cloned_Subtype
(CW_Subtype
, Base_Type
(Unc_Typ
));
2926 return New_Occurrence_Of
(CW_Subtype
, Loc
);
2929 -- Comment needed (what case is this ???)
2932 D
:= First_Discriminant
(Unc_Typ
);
2933 while Present
(D
) loop
2934 Append_To
(List_Constr
,
2935 Make_Selected_Component
(Loc
,
2936 Prefix
=> Duplicate_Subexpr_No_Checks
(E
),
2937 Selector_Name
=> New_Reference_To
(D
, Loc
)));
2939 Next_Discriminant
(D
);
2944 Make_Subtype_Indication
(Loc
,
2945 Subtype_Mark
=> New_Reference_To
(Unc_Typ
, Loc
),
2947 Make_Index_Or_Discriminant_Constraint
(Loc
,
2948 Constraints
=> List_Constr
));
2949 end Make_Subtype_From_Expr
;
2951 -----------------------------
2952 -- May_Generate_Large_Temp --
2953 -----------------------------
2955 -- At the current time, the only types that we return False for (i.e.
2956 -- where we decide we know they cannot generate large temps) are ones
2957 -- where we know the size is 128 bits or less at compile time, and we
2958 -- are still not doing a thorough job on arrays and records ???
2960 function May_Generate_Large_Temp
(Typ
: Entity_Id
) return Boolean is
2962 if not Stack_Checking_Enabled
then
2965 elsif not Size_Known_At_Compile_Time
(Typ
) then
2968 elsif Esize
(Typ
) /= 0 and then Esize
(Typ
) <= 256 then
2971 elsif Is_Array_Type
(Typ
)
2972 and then Present
(Packed_Array_Type
(Typ
))
2974 return May_Generate_Large_Temp
(Packed_Array_Type
(Typ
));
2976 -- We could do more here to find other small types ???
2981 end May_Generate_Large_Temp
;
2983 ----------------------------
2984 -- New_Class_Wide_Subtype --
2985 ----------------------------
2987 function New_Class_Wide_Subtype
2988 (CW_Typ
: Entity_Id
;
2992 Res
: constant Entity_Id
:= Create_Itype
(E_Void
, N
);
2993 Res_Name
: constant Name_Id
:= Chars
(Res
);
2994 Res_Scope
: constant Entity_Id
:= Scope
(Res
);
2997 Copy_Node
(CW_Typ
, Res
);
2998 Set_Sloc
(Res
, Sloc
(N
));
3000 Set_Associated_Node_For_Itype
(Res
, N
);
3001 Set_Is_Public
(Res
, False); -- By default, may be changed below.
3002 Set_Public_Status
(Res
);
3003 Set_Chars
(Res
, Res_Name
);
3004 Set_Scope
(Res
, Res_Scope
);
3005 Set_Ekind
(Res
, E_Class_Wide_Subtype
);
3006 Set_Next_Entity
(Res
, Empty
);
3007 Set_Etype
(Res
, Base_Type
(CW_Typ
));
3009 -- For targets where front-end layout is required, reset the Is_Frozen
3010 -- status of the subtype to False (it can be implicitly set to true
3011 -- from the copy of the class-wide type). For other targets, Gigi
3012 -- doesn't want the class-wide subtype to go through the freezing
3013 -- process (though it's unclear why that causes problems and it would
3014 -- be nice to allow freezing to occur normally for all targets ???).
3016 if Frontend_Layout_On_Target
then
3017 Set_Is_Frozen
(Res
, False);
3020 Set_Freeze_Node
(Res
, Empty
);
3022 end New_Class_Wide_Subtype
;
3024 -------------------------
3025 -- Remove_Side_Effects --
3026 -------------------------
3028 procedure Remove_Side_Effects
3030 Name_Req
: Boolean := False;
3031 Variable_Ref
: Boolean := False)
3033 Loc
: constant Source_Ptr
:= Sloc
(Exp
);
3034 Exp_Type
: constant Entity_Id
:= Etype
(Exp
);
3035 Svg_Suppress
: constant Suppress_Array
:= Scope_Suppress
;
3037 Ref_Type
: Entity_Id
;
3039 Ptr_Typ_Decl
: Node_Id
;
3043 function Side_Effect_Free
(N
: Node_Id
) return Boolean;
3044 -- Determines if the tree N represents an expession that is known
3045 -- not to have side effects, and for which no processing is required.
3047 function Side_Effect_Free
(L
: List_Id
) return Boolean;
3048 -- Determines if all elements of the list L are side effect free
3050 function Safe_Prefixed_Reference
(N
: Node_Id
) return Boolean;
3051 -- The argument N is a construct where the Prefix is dereferenced
3052 -- if it is a an access type and the result is a variable. The call
3053 -- returns True if the construct is side effect free (not considering
3054 -- side effects in other than the prefix which are to be tested by the
3057 function Within_In_Parameter
(N
: Node_Id
) return Boolean;
3058 -- Determines if N is a subcomponent of a composite in-parameter.
3059 -- If so, N is not side-effect free when the actual is global and
3060 -- modifiable indirectly from within a subprogram, because it may
3061 -- be passed by reference. The front-end must be conservative here
3062 -- and assume that this may happen with any array or record type.
3063 -- On the other hand, we cannot create temporaries for all expressions
3064 -- for which this condition is true, for various reasons that might
3065 -- require clearing up ??? For example, descriminant references that
3066 -- appear out of place, or spurious type errors with class-wide
3067 -- expressions. As a result, we limit the transformation to loop
3068 -- bounds, which is so far the only case that requires it.
3070 -----------------------------
3071 -- Safe_Prefixed_Reference --
3072 -----------------------------
3074 function Safe_Prefixed_Reference
(N
: Node_Id
) return Boolean is
3076 -- If prefix is not side effect free, definitely not safe
3078 if not Side_Effect_Free
(Prefix
(N
)) then
3081 -- If the prefix is of an access type that is not access-to-constant,
3082 -- then this construct is a variable reference, which means it is to
3083 -- be considered to have side effects if Variable_Ref is set True
3084 -- Exception is an access to an entity that is a constant or an
3085 -- in-parameter which does not come from source, and is the result
3086 -- of a previous removal of side-effects.
3088 elsif Is_Access_Type
(Etype
(Prefix
(N
)))
3089 and then not Is_Access_Constant
(Etype
(Prefix
(N
)))
3090 and then Variable_Ref
3092 if not Is_Entity_Name
(Prefix
(N
)) then
3095 return Ekind
(Entity
(Prefix
(N
))) = E_Constant
3096 or else Ekind
(Entity
(Prefix
(N
))) = E_In_Parameter
;
3099 -- The following test is the simplest way of solving a complex
3100 -- problem uncovered by BB08-010: Side effect on loop bound that
3101 -- is a subcomponent of a global variable:
3102 -- If a loop bound is a subcomponent of a global variable, a
3103 -- modification of that variable within the loop may incorrectly
3104 -- affect the execution of the loop.
3107 (Nkind
(Parent
(Parent
(N
))) /= N_Loop_Parameter_Specification
3108 or else not Within_In_Parameter
(Prefix
(N
)))
3112 -- All other cases are side effect free
3117 end Safe_Prefixed_Reference
;
3119 ----------------------
3120 -- Side_Effect_Free --
3121 ----------------------
3123 function Side_Effect_Free
(N
: Node_Id
) return Boolean is
3125 -- Note on checks that could raise Constraint_Error. Strictly, if
3126 -- we take advantage of 11.6, these checks do not count as side
3127 -- effects. However, we would just as soon consider that they are
3128 -- side effects, since the backend CSE does not work very well on
3129 -- expressions which can raise Constraint_Error. On the other
3130 -- hand, if we do not consider them to be side effect free, then
3131 -- we get some awkward expansions in -gnato mode, resulting in
3132 -- code insertions at a point where we do not have a clear model
3133 -- for performing the insertions. See 4908-002/comment for details.
3135 -- Special handling for entity names
3137 if Is_Entity_Name
(N
) then
3139 -- If the entity is a constant, it is definitely side effect
3140 -- free. Note that the test of Is_Variable (N) below might
3141 -- be expected to catch this case, but it does not, because
3142 -- this test goes to the original tree, and we may have
3143 -- already rewritten a variable node with a constant as
3144 -- a result of an earlier Force_Evaluation call.
3146 if Ekind
(Entity
(N
)) = E_Constant
3147 or else Ekind
(Entity
(N
)) = E_In_Parameter
3151 -- Functions are not side effect free
3153 elsif Ekind
(Entity
(N
)) = E_Function
then
3156 -- Variables are considered to be a side effect if Variable_Ref
3157 -- is set or if we have a volatile variable and Name_Req is off.
3158 -- If Name_Req is True then we can't help returning a name which
3159 -- effectively allows multiple references in any case.
3161 elsif Is_Variable
(N
) then
3162 return not Variable_Ref
3163 and then (not Treat_As_Volatile
(Entity
(N
))
3166 -- Any other entity (e.g. a subtype name) is definitely side
3173 -- A value known at compile time is always side effect free
3175 elsif Compile_Time_Known_Value
(N
) then
3179 -- For other than entity names and compile time known values,
3180 -- check the node kind for special processing.
3184 -- An attribute reference is side effect free if its expressions
3185 -- are side effect free and its prefix is side effect free or
3186 -- is an entity reference.
3188 -- Is this right? what about x'first where x is a variable???
3190 when N_Attribute_Reference
=>
3191 return Side_Effect_Free
(Expressions
(N
))
3192 and then (Is_Entity_Name
(Prefix
(N
))
3193 or else Side_Effect_Free
(Prefix
(N
)));
3195 -- A binary operator is side effect free if and both operands
3196 -- are side effect free. For this purpose binary operators
3197 -- include membership tests and short circuit forms
3205 return Side_Effect_Free
(Left_Opnd
(N
))
3206 and then Side_Effect_Free
(Right_Opnd
(N
));
3208 -- An explicit dereference is side effect free only if it is
3209 -- a side effect free prefixed reference.
3211 when N_Explicit_Dereference
=>
3212 return Safe_Prefixed_Reference
(N
);
3214 -- A call to _rep_to_pos is side effect free, since we generate
3215 -- this pure function call ourselves. Moreover it is critically
3216 -- important to make this exception, since otherwise we can
3217 -- have discriminants in array components which don't look
3218 -- side effect free in the case of an array whose index type
3219 -- is an enumeration type with an enumeration rep clause.
3221 -- All other function calls are not side effect free
3223 when N_Function_Call
=>
3224 return Nkind
(Name
(N
)) = N_Identifier
3225 and then Is_TSS
(Name
(N
), TSS_Rep_To_Pos
)
3227 Side_Effect_Free
(First
(Parameter_Associations
(N
)));
3229 -- An indexed component is side effect free if it is a side
3230 -- effect free prefixed reference and all the indexing
3231 -- expressions are side effect free.
3233 when N_Indexed_Component
=>
3234 return Side_Effect_Free
(Expressions
(N
))
3235 and then Safe_Prefixed_Reference
(N
);
3237 -- A type qualification is side effect free if the expression
3238 -- is side effect free.
3240 when N_Qualified_Expression
=>
3241 return Side_Effect_Free
(Expression
(N
));
3243 -- A selected component is side effect free only if it is a
3244 -- side effect free prefixed reference.
3246 when N_Selected_Component
=>
3247 return Safe_Prefixed_Reference
(N
);
3249 -- A range is side effect free if the bounds are side effect free
3252 return Side_Effect_Free
(Low_Bound
(N
))
3253 and then Side_Effect_Free
(High_Bound
(N
));
3255 -- A slice is side effect free if it is a side effect free
3256 -- prefixed reference and the bounds are side effect free.
3259 return Side_Effect_Free
(Discrete_Range
(N
))
3260 and then Safe_Prefixed_Reference
(N
);
3262 -- A type conversion is side effect free if the expression
3263 -- to be converted is side effect free.
3265 when N_Type_Conversion
=>
3266 return Side_Effect_Free
(Expression
(N
));
3268 -- A unary operator is side effect free if the operand
3269 -- is side effect free.
3272 return Side_Effect_Free
(Right_Opnd
(N
));
3274 -- An unchecked type conversion is side effect free only if it
3275 -- is safe and its argument is side effect free.
3277 when N_Unchecked_Type_Conversion
=>
3278 return Safe_Unchecked_Type_Conversion
(N
)
3279 and then Side_Effect_Free
(Expression
(N
));
3281 -- An unchecked expression is side effect free if its expression
3282 -- is side effect free.
3284 when N_Unchecked_Expression
=>
3285 return Side_Effect_Free
(Expression
(N
));
3287 -- We consider that anything else has side effects. This is a bit
3288 -- crude, but we are pretty close for most common cases, and we
3289 -- are certainly correct (i.e. we never return True when the
3290 -- answer should be False).
3295 end Side_Effect_Free
;
3297 -- A list is side effect free if all elements of the list are
3298 -- side effect free.
3300 function Side_Effect_Free
(L
: List_Id
) return Boolean is
3304 if L
= No_List
or else L
= Error_List
then
3310 while Present
(N
) loop
3311 if not Side_Effect_Free
(N
) then
3320 end Side_Effect_Free
;
3322 -------------------------
3323 -- Within_In_Parameter --
3324 -------------------------
3326 function Within_In_Parameter
(N
: Node_Id
) return Boolean is
3328 if not Comes_From_Source
(N
) then
3331 elsif Is_Entity_Name
(N
) then
3333 Ekind
(Entity
(N
)) = E_In_Parameter
;
3335 elsif Nkind
(N
) = N_Indexed_Component
3336 or else Nkind
(N
) = N_Selected_Component
3338 return Within_In_Parameter
(Prefix
(N
));
3343 end Within_In_Parameter
;
3345 -- Start of processing for Remove_Side_Effects
3348 -- If we are side effect free already or expansion is disabled,
3349 -- there is nothing to do.
3351 if Side_Effect_Free
(Exp
) or else not Expander_Active
then
3355 -- All this must not have any checks
3357 Scope_Suppress
:= (others => True);
3359 -- If the expression has the form v.all then we can just capture
3360 -- the pointer, and then do an explicit dereference on the result.
3362 if Nkind
(Exp
) = N_Explicit_Dereference
then
3364 Make_Defining_Identifier
(Loc
, New_Internal_Name
('R'));
3366 Make_Explicit_Dereference
(Loc
, New_Reference_To
(Def_Id
, Loc
));
3369 Make_Object_Declaration
(Loc
,
3370 Defining_Identifier
=> Def_Id
,
3371 Object_Definition
=>
3372 New_Reference_To
(Etype
(Prefix
(Exp
)), Loc
),
3373 Constant_Present
=> True,
3374 Expression
=> Relocate_Node
(Prefix
(Exp
))));
3376 -- Similar processing for an unchecked conversion of an expression
3377 -- of the form v.all, where we want the same kind of treatment.
3379 elsif Nkind
(Exp
) = N_Unchecked_Type_Conversion
3380 and then Nkind
(Expression
(Exp
)) = N_Explicit_Dereference
3382 Remove_Side_Effects
(Expression
(Exp
), Variable_Ref
);
3383 Scope_Suppress
:= Svg_Suppress
;
3386 -- If this is a type conversion, leave the type conversion and remove
3387 -- the side effects in the expression. This is important in several
3388 -- circumstances: for change of representations, and also when this
3389 -- is a view conversion to a smaller object, where gigi can end up
3390 -- its own temporary of the wrong size.
3392 -- ??? this transformation is inhibited for elementary types that are
3393 -- not involved in a change of representation because it causes
3394 -- regressions that are not fully understood yet.
3396 elsif Nkind
(Exp
) = N_Type_Conversion
3397 and then (not Is_Elementary_Type
(Underlying_Type
(Exp_Type
))
3398 or else Nkind
(Parent
(Exp
)) = N_Assignment_Statement
)
3400 Remove_Side_Effects
(Expression
(Exp
), Variable_Ref
);
3401 Scope_Suppress
:= Svg_Suppress
;
3404 -- For expressions that denote objects, we can use a renaming scheme.
3405 -- We skip using this if we have a volatile variable and we do not
3406 -- have Nam_Req set true (see comments above for Side_Effect_Free).
3407 -- We also skip this scheme for class-wide expressions in order to
3408 -- avoid recursive expansion (see Expand_N_Object_Renaming_Declaration)
3409 -- If the object is a function call, we need to create a temporary and
3412 -- Note that we could use ordinary object declarations in the case of
3413 -- expressions not appearing as lvalues. That is left as a possible
3414 -- optimization in the future but we prefer to generate renamings
3415 -- right now, since we may indeed be transforming an lvalue.
3417 elsif Is_Object_Reference
(Exp
)
3418 and then Nkind
(Exp
) /= N_Function_Call
3419 and then not Variable_Ref
3421 or else not Is_Entity_Name
(Exp
)
3422 or else not Treat_As_Volatile
(Entity
(Exp
)))
3423 and then not Is_Class_Wide_Type
(Exp_Type
)
3425 Def_Id
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('R'));
3427 if Nkind
(Exp
) = N_Selected_Component
3428 and then Nkind
(Prefix
(Exp
)) = N_Function_Call
3429 and then Is_Array_Type
(Etype
(Exp
))
3431 -- Avoid generating a variable-sized temporary, by generating
3432 -- the renaming declaration just for the function call. The
3433 -- transformation could be refined to apply only when the array
3434 -- component is constrained by a discriminant???
3437 Make_Selected_Component
(Loc
,
3438 Prefix
=> New_Occurrence_Of
(Def_Id
, Loc
),
3439 Selector_Name
=> Selector_Name
(Exp
));
3442 Make_Object_Renaming_Declaration
(Loc
,
3443 Defining_Identifier
=> Def_Id
,
3445 New_Reference_To
(Base_Type
(Etype
(Prefix
(Exp
))), Loc
),
3446 Name
=> Relocate_Node
(Prefix
(Exp
))));
3448 -- The temporary must be elaborated by gigi, and is of course
3449 -- not to be replaced in-line by the expression it renames,
3450 -- which would defeat the purpose of removing the side-effect.
3452 Set_Is_Renaming_Of_Object
(Def_Id
, False);
3455 Res
:= New_Reference_To
(Def_Id
, Loc
);
3458 Make_Object_Renaming_Declaration
(Loc
,
3459 Defining_Identifier
=> Def_Id
,
3460 Subtype_Mark
=> New_Reference_To
(Exp_Type
, Loc
),
3461 Name
=> Relocate_Node
(Exp
)));
3463 Set_Is_Renaming_Of_Object
(Def_Id
, False);
3466 -- If it is a scalar type, just make a copy.
3468 elsif Is_Elementary_Type
(Exp_Type
) then
3469 Def_Id
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('R'));
3470 Set_Etype
(Def_Id
, Exp_Type
);
3471 Res
:= New_Reference_To
(Def_Id
, Loc
);
3474 Make_Object_Declaration
(Loc
,
3475 Defining_Identifier
=> Def_Id
,
3476 Object_Definition
=> New_Reference_To
(Exp_Type
, Loc
),
3477 Constant_Present
=> True,
3478 Expression
=> Relocate_Node
(Exp
));
3480 Set_Assignment_OK
(E
);
3481 Insert_Action
(Exp
, E
);
3483 -- Always use a renaming for an unchecked conversion
3484 -- If this is an unchecked conversion that Gigi can't handle, make
3485 -- a copy or a use a renaming to capture the value.
3487 elsif Nkind
(Exp
) = N_Unchecked_Type_Conversion
3488 and then not Safe_Unchecked_Type_Conversion
(Exp
)
3490 if Controlled_Type
(Etype
(Exp
)) then
3492 -- Use a renaming to capture the expression, rather than create
3493 -- a controlled temporary.
3495 Def_Id
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('R'));
3496 Res
:= New_Reference_To
(Def_Id
, Loc
);
3499 Make_Object_Renaming_Declaration
(Loc
,
3500 Defining_Identifier
=> Def_Id
,
3501 Subtype_Mark
=> New_Reference_To
(Exp_Type
, Loc
),
3502 Name
=> Relocate_Node
(Exp
)));
3505 Def_Id
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('R'));
3506 Set_Etype
(Def_Id
, Exp_Type
);
3507 Res
:= New_Reference_To
(Def_Id
, Loc
);
3510 Make_Object_Declaration
(Loc
,
3511 Defining_Identifier
=> Def_Id
,
3512 Object_Definition
=> New_Reference_To
(Exp_Type
, Loc
),
3513 Constant_Present
=> not Is_Variable
(Exp
),
3514 Expression
=> Relocate_Node
(Exp
));
3516 Set_Assignment_OK
(E
);
3517 Insert_Action
(Exp
, E
);
3520 -- Otherwise we generate a reference to the value
3523 Ref_Type
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('A'));
3526 Make_Full_Type_Declaration
(Loc
,
3527 Defining_Identifier
=> Ref_Type
,
3529 Make_Access_To_Object_Definition
(Loc
,
3530 All_Present
=> True,
3531 Subtype_Indication
=>
3532 New_Reference_To
(Exp_Type
, Loc
)));
3535 Insert_Action
(Exp
, Ptr_Typ_Decl
);
3537 Def_Id
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('R'));
3538 Set_Etype
(Def_Id
, Exp_Type
);
3541 Make_Explicit_Dereference
(Loc
,
3542 Prefix
=> New_Reference_To
(Def_Id
, Loc
));
3544 if Nkind
(E
) = N_Explicit_Dereference
then
3545 New_Exp
:= Relocate_Node
(Prefix
(E
));
3547 E
:= Relocate_Node
(E
);
3548 New_Exp
:= Make_Reference
(Loc
, E
);
3551 if Nkind
(E
) = N_Aggregate
and then Expansion_Delayed
(E
) then
3552 Set_Expansion_Delayed
(E
, False);
3553 Set_Analyzed
(E
, False);
3557 Make_Object_Declaration
(Loc
,
3558 Defining_Identifier
=> Def_Id
,
3559 Object_Definition
=> New_Reference_To
(Ref_Type
, Loc
),
3560 Expression
=> New_Exp
));
3563 -- Preserve the Assignment_OK flag in all copies, since at least
3564 -- one copy may be used in a context where this flag must be set
3565 -- (otherwise why would the flag be set in the first place).
3567 Set_Assignment_OK
(Res
, Assignment_OK
(Exp
));
3569 -- Finally rewrite the original expression and we are done
3572 Analyze_And_Resolve
(Exp
, Exp_Type
);
3573 Scope_Suppress
:= Svg_Suppress
;
3574 end Remove_Side_Effects
;
3576 ------------------------------------
3577 -- Safe_Unchecked_Type_Conversion --
3578 ------------------------------------
3580 -- Note: this function knows quite a bit about the exact requirements
3581 -- of Gigi with respect to unchecked type conversions, and its code
3582 -- must be coordinated with any changes in Gigi in this area.
3584 -- The above requirements should be documented in Sinfo ???
3586 function Safe_Unchecked_Type_Conversion
(Exp
: Node_Id
) return Boolean is
3591 Pexp
: constant Node_Id
:= Parent
(Exp
);
3594 -- If the expression is the RHS of an assignment or object declaration
3595 -- we are always OK because there will always be a target.
3597 -- Object renaming declarations, (generated for view conversions of
3598 -- actuals in inlined calls), like object declarations, provide an
3599 -- explicit type, and are safe as well.
3601 if (Nkind
(Pexp
) = N_Assignment_Statement
3602 and then Expression
(Pexp
) = Exp
)
3603 or else Nkind
(Pexp
) = N_Object_Declaration
3604 or else Nkind
(Pexp
) = N_Object_Renaming_Declaration
3608 -- If the expression is the prefix of an N_Selected_Component
3609 -- we should also be OK because GCC knows to look inside the
3610 -- conversion except if the type is discriminated. We assume
3611 -- that we are OK anyway if the type is not set yet or if it is
3612 -- controlled since we can't afford to introduce a temporary in
3615 elsif Nkind
(Pexp
) = N_Selected_Component
3616 and then Prefix
(Pexp
) = Exp
3618 if No
(Etype
(Pexp
)) then
3622 not Has_Discriminants
(Etype
(Pexp
))
3623 or else Is_Constrained
(Etype
(Pexp
));
3627 -- Set the output type, this comes from Etype if it is set, otherwise
3628 -- we take it from the subtype mark, which we assume was already
3631 if Present
(Etype
(Exp
)) then
3632 Otyp
:= Etype
(Exp
);
3634 Otyp
:= Entity
(Subtype_Mark
(Exp
));
3637 -- The input type always comes from the expression, and we assume
3638 -- this is indeed always analyzed, so we can simply get the Etype.
3640 Ityp
:= Etype
(Expression
(Exp
));
3642 -- Initialize alignments to unknown so far
3647 -- Replace a concurrent type by its corresponding record type
3648 -- and each type by its underlying type and do the tests on those.
3649 -- The original type may be a private type whose completion is a
3650 -- concurrent type, so find the underlying type first.
3652 if Present
(Underlying_Type
(Otyp
)) then
3653 Otyp
:= Underlying_Type
(Otyp
);
3656 if Present
(Underlying_Type
(Ityp
)) then
3657 Ityp
:= Underlying_Type
(Ityp
);
3660 if Is_Concurrent_Type
(Otyp
) then
3661 Otyp
:= Corresponding_Record_Type
(Otyp
);
3664 if Is_Concurrent_Type
(Ityp
) then
3665 Ityp
:= Corresponding_Record_Type
(Ityp
);
3668 -- If the base types are the same, we know there is no problem since
3669 -- this conversion will be a noop.
3671 if Implementation_Base_Type
(Otyp
) = Implementation_Base_Type
(Ityp
) then
3674 -- If the size of output type is known at compile time, there is
3675 -- never a problem. Note that unconstrained records are considered
3676 -- to be of known size, but we can't consider them that way here,
3677 -- because we are talking about the actual size of the object.
3679 -- We also make sure that in addition to the size being known, we do
3680 -- not have a case which might generate an embarrassingly large temp
3681 -- in stack checking mode.
3683 elsif Size_Known_At_Compile_Time
(Otyp
)
3684 and then not May_Generate_Large_Temp
(Otyp
)
3685 and then not (Is_Record_Type
(Otyp
) and then not Is_Constrained
(Otyp
))
3689 -- If either type is tagged, then we know the alignment is OK so
3690 -- Gigi will be able to use pointer punning.
3692 elsif Is_Tagged_Type
(Otyp
) or else Is_Tagged_Type
(Ityp
) then
3695 -- If either type is a limited record type, we cannot do a copy, so
3696 -- say safe since there's nothing else we can do.
3698 elsif Is_Limited_Record
(Otyp
) or else Is_Limited_Record
(Ityp
) then
3701 -- Conversions to and from packed array types are always ignored and
3704 elsif Is_Packed_Array_Type
(Otyp
)
3705 or else Is_Packed_Array_Type
(Ityp
)
3710 -- The only other cases known to be safe is if the input type's
3711 -- alignment is known to be at least the maximum alignment for the
3712 -- target or if both alignments are known and the output type's
3713 -- alignment is no stricter than the input's. We can use the alignment
3714 -- of the component type of an array if a type is an unpacked
3717 if Present
(Alignment_Clause
(Otyp
)) then
3718 Oalign
:= Expr_Value
(Expression
(Alignment_Clause
(Otyp
)));
3720 elsif Is_Array_Type
(Otyp
)
3721 and then Present
(Alignment_Clause
(Component_Type
(Otyp
)))
3723 Oalign
:= Expr_Value
(Expression
(Alignment_Clause
3724 (Component_Type
(Otyp
))));
3727 if Present
(Alignment_Clause
(Ityp
)) then
3728 Ialign
:= Expr_Value
(Expression
(Alignment_Clause
(Ityp
)));
3730 elsif Is_Array_Type
(Ityp
)
3731 and then Present
(Alignment_Clause
(Component_Type
(Ityp
)))
3733 Ialign
:= Expr_Value
(Expression
(Alignment_Clause
3734 (Component_Type
(Ityp
))));
3737 if Ialign
/= No_Uint
and then Ialign
> Maximum_Alignment
then
3740 elsif Ialign
/= No_Uint
and then Oalign
/= No_Uint
3741 and then Ialign
<= Oalign
3745 -- Otherwise, Gigi cannot handle this and we must make a temporary.
3751 end Safe_Unchecked_Type_Conversion
;
3753 --------------------------
3754 -- Set_Elaboration_Flag --
3755 --------------------------
3757 procedure Set_Elaboration_Flag
(N
: Node_Id
; Spec_Id
: Entity_Id
) is
3758 Loc
: constant Source_Ptr
:= Sloc
(N
);
3759 Ent
: constant Entity_Id
:= Elaboration_Entity
(Spec_Id
);
3763 if Present
(Ent
) then
3765 -- Nothing to do if at the compilation unit level, because in this
3766 -- case the flag is set by the binder generated elaboration routine.
3768 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
3771 -- Here we do need to generate an assignment statement
3774 Check_Restriction
(No_Elaboration_Code
, N
);
3776 Make_Assignment_Statement
(Loc
,
3777 Name
=> New_Occurrence_Of
(Ent
, Loc
),
3778 Expression
=> New_Occurrence_Of
(Standard_True
, Loc
));
3780 if Nkind
(Parent
(N
)) = N_Subunit
then
3781 Insert_After
(Corresponding_Stub
(Parent
(N
)), Asn
);
3783 Insert_After
(N
, Asn
);
3788 -- Kill current value indication. This is necessary because
3789 -- the tests of this flag are inserted out of sequence and must
3790 -- not pick up bogus indications of the wrong constant value.
3792 Set_Current_Value
(Ent
, Empty
);
3795 end Set_Elaboration_Flag
;
3797 --------------------------
3798 -- Target_Has_Fixed_Ops --
3799 --------------------------
3801 Integer_Sized_Small
: Ureal
;
3802 -- Set to 2.0 ** -(Integer'Size - 1) the first time that this
3803 -- function is called (we don't want to compute it more than once!)
3805 Long_Integer_Sized_Small
: Ureal
;
3806 -- Set to 2.0 ** -(Long_Integer'Size - 1) the first time that this
3807 -- functoin is called (we don't want to compute it more than once)
3809 First_Time_For_THFO
: Boolean := True;
3810 -- Set to False after first call (if Fractional_Fixed_Ops_On_Target)
3812 function Target_Has_Fixed_Ops
3813 (Left_Typ
: Entity_Id
;
3814 Right_Typ
: Entity_Id
;
3815 Result_Typ
: Entity_Id
)
3818 function Is_Fractional_Type
(Typ
: Entity_Id
) return Boolean;
3819 -- Return True if the given type is a fixed-point type with a small
3820 -- value equal to 2 ** (-(T'Object_Size - 1)) and whose values have
3821 -- an absolute value less than 1.0. This is currently limited
3822 -- to fixed-point types that map to Integer or Long_Integer.
3824 ------------------------
3825 -- Is_Fractional_Type --
3826 ------------------------
3828 function Is_Fractional_Type
(Typ
: Entity_Id
) return Boolean is
3830 if Esize
(Typ
) = Standard_Integer_Size
then
3831 return Small_Value
(Typ
) = Integer_Sized_Small
;
3833 elsif Esize
(Typ
) = Standard_Long_Integer_Size
then
3834 return Small_Value
(Typ
) = Long_Integer_Sized_Small
;
3839 end Is_Fractional_Type
;
3841 -- Start of processing for Target_Has_Fixed_Ops
3844 -- Return False if Fractional_Fixed_Ops_On_Target is false
3846 if not Fractional_Fixed_Ops_On_Target
then
3850 -- Here the target has Fractional_Fixed_Ops, if first time, compute
3851 -- standard constants used by Is_Fractional_Type.
3853 if First_Time_For_THFO
then
3854 First_Time_For_THFO
:= False;
3856 Integer_Sized_Small
:=
3859 Den
=> UI_From_Int
(Standard_Integer_Size
- 1),
3862 Long_Integer_Sized_Small
:=
3865 Den
=> UI_From_Int
(Standard_Long_Integer_Size
- 1),
3869 -- Return True if target supports fixed-by-fixed multiply/divide
3870 -- for fractional fixed-point types (see Is_Fractional_Type) and
3871 -- the operand and result types are equivalent fractional types.
3873 return Is_Fractional_Type
(Base_Type
(Left_Typ
))
3874 and then Is_Fractional_Type
(Base_Type
(Right_Typ
))
3875 and then Is_Fractional_Type
(Base_Type
(Result_Typ
))
3876 and then Esize
(Left_Typ
) = Esize
(Right_Typ
)
3877 and then Esize
(Left_Typ
) = Esize
(Result_Typ
);
3878 end Target_Has_Fixed_Ops
;
3880 ----------------------------
3881 -- Wrap_Cleanup_Procedure --
3882 ----------------------------
3884 procedure Wrap_Cleanup_Procedure
(N
: Node_Id
) is
3885 Loc
: constant Source_Ptr
:= Sloc
(N
);
3886 Stseq
: constant Node_Id
:= Handled_Statement_Sequence
(N
);
3887 Stmts
: constant List_Id
:= Statements
(Stseq
);
3890 if Abort_Allowed
then
3891 Prepend_To
(Stmts
, Build_Runtime_Call
(Loc
, RE_Abort_Defer
));
3892 Append_To
(Stmts
, Build_Runtime_Call
(Loc
, RE_Abort_Undefer
));
3894 end Wrap_Cleanup_Procedure
;