1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2015, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree
; use Atree
;
27 with Aspects
; use Aspects
;
28 with Csets
; use Csets
;
29 with Einfo
; use Einfo
;
30 with Elists
; use Elists
;
32 with Nlists
; use Nlists
;
33 with Nmake
; use Nmake
;
35 with Restrict
; use Restrict
;
36 with Rident
; use Rident
;
37 with Sem_Aux
; use Sem_Aux
;
38 with Snames
; use Snames
;
39 with Stand
; use Stand
;
40 with Stringt
; use Stringt
;
41 with Urealp
; use Urealp
;
43 package body Tbuild
is
45 -----------------------
46 -- Local Subprograms --
47 -----------------------
49 procedure Add_Unique_Serial_Number
;
50 -- Add a unique serialization to the string in the Name_Buffer. This
51 -- consists of a unit specific serial number, and b/s for body/spec.
53 ------------------------------
54 -- Add_Unique_Serial_Number --
55 ------------------------------
57 Config_Serial_Number
: Nat
:= 0;
58 -- Counter for use in config pragmas, see comment below
60 procedure Add_Unique_Serial_Number
is
62 -- If we are analyzing configuration pragmas, Cunit (Main_Unit) will
63 -- not be set yet. This happens for example when analyzing static
64 -- string expressions in configuration pragmas. For this case, we
65 -- just maintain a local counter, defined above and we do not need
66 -- to add a b or s indication in this case.
68 if No
(Cunit
(Current_Sem_Unit
)) then
69 Config_Serial_Number
:= Config_Serial_Number
+ 1;
70 Add_Nat_To_Name_Buffer
(Config_Serial_Number
);
73 -- Normal case, within a unit
77 Unit_Node
: constant Node_Id
:= Unit
(Cunit
(Current_Sem_Unit
));
80 Add_Nat_To_Name_Buffer
(Increment_Serial_Number
);
82 -- Add either b or s, depending on whether current unit is a spec
83 -- or a body. This is needed because we may generate the same name
84 -- in a spec and a body otherwise.
86 Name_Len
:= Name_Len
+ 1;
88 if Nkind
(Unit_Node
) = N_Package_Declaration
89 or else Nkind
(Unit_Node
) = N_Subprogram_Declaration
90 or else Nkind
(Unit_Node
) in N_Generic_Declaration
92 Name_Buffer
(Name_Len
) := 's';
94 Name_Buffer
(Name_Len
) := 'b';
98 end Add_Unique_Serial_Number
;
104 function Checks_Off
(N
: Node_Id
) return Node_Id
is
107 Make_Unchecked_Expression
(Sloc
(N
),
115 function Convert_To
(Typ
: Entity_Id
; Expr
: Node_Id
) return Node_Id
is
119 if Present
(Etype
(Expr
))
120 and then (Etype
(Expr
)) = Typ
122 return Relocate_Node
(Expr
);
125 Make_Type_Conversion
(Sloc
(Expr
),
126 Subtype_Mark
=> New_Occurrence_Of
(Typ
, Sloc
(Expr
)),
127 Expression
=> Relocate_Node
(Expr
));
129 Set_Etype
(Result
, Typ
);
134 ----------------------------
135 -- Convert_To_And_Rewrite --
136 ----------------------------
138 procedure Convert_To_And_Rewrite
(Typ
: Entity_Id
; Expr
: Node_Id
) is
140 Rewrite
(Expr
, Convert_To
(Typ
, Expr
));
141 end Convert_To_And_Rewrite
;
147 procedure Discard_List
(L
: List_Id
) is
148 pragma Warnings
(Off
, L
);
157 procedure Discard_Node
(N
: Node_Or_Entity_Id
) is
158 pragma Warnings
(Off
, N
);
163 -------------------------------------------
164 -- Make_Byte_Aligned_Attribute_Reference --
165 -------------------------------------------
167 function Make_Byte_Aligned_Attribute_Reference
170 Attribute_Name
: Name_Id
)
173 N
: constant Node_Id
:=
174 Make_Attribute_Reference
(Sloc
,
176 Attribute_Name
=> Attribute_Name
);
179 pragma Assert
(Nam_In
(Attribute_Name
, Name_Address
,
180 Name_Unrestricted_Access
));
181 Set_Must_Be_Byte_Aligned
(N
, True);
183 end Make_Byte_Aligned_Attribute_Reference
;
189 function Make_DT_Access
192 Typ
: Entity_Id
) return Node_Id
194 Full_Type
: Entity_Id
:= Typ
;
197 if Is_Private_Type
(Typ
) then
198 Full_Type
:= Underlying_Type
(Typ
);
202 Unchecked_Convert_To
(
204 (Etype
(Node
(First_Elmt
(Access_Disp_Table
(Full_Type
)))), Loc
),
205 Make_Selected_Component
(Loc
,
206 Prefix
=> New_Copy
(Rec
),
208 New_Occurrence_Of
(First_Tag_Component
(Full_Type
), Loc
)));
211 ------------------------
212 -- Make_Float_Literal --
213 ------------------------
215 function Make_Float_Literal
219 Exponent
: Uint
) return Node_Id
222 if Radix
= 2 and then abs Significand
/= 1 then
226 Significand
* Radix
**(Exponent
mod 4),
231 N
: constant Node_Id
:= New_Node
(N_Real_Literal
, Loc
);
236 (Num
=> abs Significand
,
238 Rbase
=> UI_To_Int
(Radix
),
239 Negative
=> Significand
< 0));
243 end Make_Float_Literal
;
249 function Make_Id
(Str
: Text_Buffer
) return Node_Id
is
253 for J
in Str
'Range loop
254 Name_Len
:= Name_Len
+ 1;
255 Name_Buffer
(Name_Len
) := Fold_Lower
(Str
(J
));
259 Make_Identifier
(System_Location
,
263 -------------------------------------
264 -- Make_Implicit_Exception_Handler --
265 -------------------------------------
267 function Make_Implicit_Exception_Handler
269 Choice_Parameter
: Node_Id
:= Empty
;
270 Exception_Choices
: List_Id
;
271 Statements
: List_Id
) return Node_Id
277 -- Set the source location only when debugging the expanded code
279 -- When debugging the source code directly, we do not want the compiler
280 -- to associate this implicit exception handler with any specific source
281 -- line, because it can potentially confuse the debugger. The most
282 -- damaging situation would arise when the debugger tries to insert a
283 -- breakpoint at a certain line. If the code of the associated implicit
284 -- exception handler is generated before the code of that line, then the
285 -- debugger will end up inserting the breakpoint inside the exception
286 -- handler, rather than the code the user intended to break on. As a
287 -- result, it is likely that the program will not hit the breakpoint
290 if Debug_Generated_Code
then
297 Make_Exception_Handler
298 (Loc
, Choice_Parameter
, Exception_Choices
, Statements
);
299 Set_Local_Raise_Statements
(Handler
, No_Elist
);
301 end Make_Implicit_Exception_Handler
;
303 --------------------------------
304 -- Make_Implicit_If_Statement --
305 --------------------------------
307 function Make_Implicit_If_Statement
310 Then_Statements
: List_Id
;
311 Elsif_Parts
: List_Id
:= No_List
;
312 Else_Statements
: List_Id
:= No_List
) return Node_Id
315 Check_Restriction
(No_Implicit_Conditionals
, Node
);
317 return Make_If_Statement
(Sloc
(Node
),
322 end Make_Implicit_If_Statement
;
324 -------------------------------------
325 -- Make_Implicit_Label_Declaration --
326 -------------------------------------
328 function Make_Implicit_Label_Declaration
330 Defining_Identifier
: Node_Id
;
331 Label_Construct
: Node_Id
) return Node_Id
333 N
: constant Node_Id
:=
334 Make_Implicit_Label_Declaration
(Loc
, Defining_Identifier
);
336 Set_Label_Construct
(N
, Label_Construct
);
338 end Make_Implicit_Label_Declaration
;
340 ----------------------------------
341 -- Make_Implicit_Loop_Statement --
342 ----------------------------------
344 function Make_Implicit_Loop_Statement
346 Statements
: List_Id
;
347 Identifier
: Node_Id
:= Empty
;
348 Iteration_Scheme
: Node_Id
:= Empty
;
349 Has_Created_Identifier
: Boolean := False;
350 End_Label
: Node_Id
:= Empty
) return Node_Id
353 Check_Restriction
(No_Implicit_Loops
, Node
);
355 if Present
(Iteration_Scheme
)
356 and then Present
(Condition
(Iteration_Scheme
))
358 Check_Restriction
(No_Implicit_Conditionals
, Node
);
361 return Make_Loop_Statement
(Sloc
(Node
),
362 Identifier
=> Identifier
,
363 Iteration_Scheme
=> Iteration_Scheme
,
364 Statements
=> Statements
,
365 Has_Created_Identifier
=> Has_Created_Identifier
,
366 End_Label
=> End_Label
);
367 end Make_Implicit_Loop_Statement
;
369 --------------------------
370 -- Make_Integer_Literal --
371 ---------------------------
373 function Make_Integer_Literal
375 Intval
: Int
) return Node_Id
378 return Make_Integer_Literal
(Loc
, UI_From_Int
(Intval
));
379 end Make_Integer_Literal
;
381 --------------------------------
382 -- Make_Linker_Section_Pragma --
383 --------------------------------
385 function Make_Linker_Section_Pragma
388 Sec
: String) return Node_Id
398 (Make_Pragma_Argument_Association
400 Expression
=> New_Occurrence_Of
(Ent
, Loc
)),
401 Make_Pragma_Argument_Association
408 Set_Has_Gigi_Rep_Item
(Ent
);
410 end Make_Linker_Section_Pragma
;
419 Pragma_Argument_Associations
: List_Id
:= No_List
) return Node_Id
424 Pragma_Argument_Associations
=> Pragma_Argument_Associations
,
425 Pragma_Identifier
=> Make_Identifier
(Sloc
, Chars
));
428 ---------------------------------
429 -- Make_Raise_Constraint_Error --
430 ---------------------------------
432 function Make_Raise_Constraint_Error
434 Condition
: Node_Id
:= Empty
;
435 Reason
: RT_Exception_Code
) return Node_Id
438 pragma Assert
(Rkind
(Reason
) = CE_Reason
);
440 Make_Raise_Constraint_Error
(Sloc
,
441 Condition
=> Condition
,
442 Reason
=> UI_From_Int
(RT_Exception_Code
'Pos (Reason
)));
443 end Make_Raise_Constraint_Error
;
445 ------------------------------
446 -- Make_Raise_Program_Error --
447 ------------------------------
449 function Make_Raise_Program_Error
451 Condition
: Node_Id
:= Empty
;
452 Reason
: RT_Exception_Code
) return Node_Id
455 pragma Assert
(Rkind
(Reason
) = PE_Reason
);
457 Make_Raise_Program_Error
(Sloc
,
458 Condition
=> Condition
,
459 Reason
=> UI_From_Int
(RT_Exception_Code
'Pos (Reason
)));
460 end Make_Raise_Program_Error
;
462 ------------------------------
463 -- Make_Raise_Storage_Error --
464 ------------------------------
466 function Make_Raise_Storage_Error
468 Condition
: Node_Id
:= Empty
;
469 Reason
: RT_Exception_Code
) return Node_Id
472 pragma Assert
(Rkind
(Reason
) = SE_Reason
);
474 Make_Raise_Storage_Error
(Sloc
,
475 Condition
=> Condition
,
476 Reason
=> UI_From_Int
(RT_Exception_Code
'Pos (Reason
)));
477 end Make_Raise_Storage_Error
;
483 function Make_SC
(Pre
, Sel
: Node_Id
) return Node_Id
is
486 Make_Selected_Component
(System_Location
,
488 Selector_Name
=> Sel
);
491 -------------------------
492 -- Make_String_Literal --
493 -------------------------
495 function Make_String_Literal
497 Strval
: String) return Node_Id
501 Store_String_Chars
(Strval
);
502 return Make_String_Literal
(Sloc
, Strval
=> End_String
);
503 end Make_String_Literal
;
509 function Make_Temporary
512 Related_Node
: Node_Id
:= Empty
) return Entity_Id
514 Temp
: constant Entity_Id
:=
515 Make_Defining_Identifier
(Loc
, Chars
=> New_Internal_Name
(Id
));
517 Set_Related_Expression
(Temp
, Related_Node
);
521 ---------------------------
522 -- Make_Unsuppress_Block --
523 ---------------------------
525 -- Generates the following expansion:
528 -- pragma Suppress (<check>);
533 function Make_Unsuppress_Block
536 Stmts
: List_Id
) return Node_Id
540 Make_Block_Statement
(Loc
,
541 Declarations
=> New_List
(
543 Chars
=> Name_Suppress
,
544 Pragma_Argument_Associations
=> New_List
(
545 Make_Pragma_Argument_Association
(Loc
,
546 Expression
=> Make_Identifier
(Loc
, Check
))))),
548 Handled_Statement_Sequence
=>
549 Make_Handled_Sequence_Of_Statements
(Loc
,
550 Statements
=> Stmts
));
551 end Make_Unsuppress_Block
;
553 --------------------------
554 -- New_Constraint_Error --
555 --------------------------
557 function New_Constraint_Error
(Loc
: Source_Ptr
) return Node_Id
is
558 Ident_Node
: Node_Id
;
559 Raise_Node
: Node_Id
;
562 Ident_Node
:= New_Node
(N_Identifier
, Loc
);
563 Set_Chars
(Ident_Node
, Chars
(Standard_Entity
(S_Constraint_Error
)));
564 Set_Entity
(Ident_Node
, Standard_Entity
(S_Constraint_Error
));
565 Raise_Node
:= New_Node
(N_Raise_Statement
, Loc
);
566 Set_Name
(Raise_Node
, Ident_Node
);
568 end New_Constraint_Error
;
570 -----------------------
571 -- New_External_Name --
572 -----------------------
574 function New_External_Name
575 (Related_Id
: Name_Id
;
576 Suffix
: Character := ' ';
577 Suffix_Index
: Int
:= 0;
578 Prefix
: Character := ' ') return Name_Id
581 Get_Name_String
(Related_Id
);
583 if Prefix
/= ' ' then
584 pragma Assert
(Is_OK_Internal_Letter
(Prefix
) or else Prefix
= '_');
586 for J
in reverse 1 .. Name_Len
loop
587 Name_Buffer
(J
+ 1) := Name_Buffer
(J
);
590 Name_Len
:= Name_Len
+ 1;
591 Name_Buffer
(1) := Prefix
;
594 if Suffix
/= ' ' then
595 pragma Assert
(Is_OK_Internal_Letter
(Suffix
));
596 Add_Char_To_Name_Buffer
(Suffix
);
599 if Suffix_Index
/= 0 then
600 if Suffix_Index
< 0 then
601 Add_Unique_Serial_Number
;
603 Add_Nat_To_Name_Buffer
(Suffix_Index
);
608 end New_External_Name
;
610 function New_External_Name
611 (Related_Id
: Name_Id
;
613 Suffix_Index
: Int
:= 0;
614 Prefix
: Character := ' ') return Name_Id
617 Get_Name_String
(Related_Id
);
619 if Prefix
/= ' ' then
620 pragma Assert
(Is_OK_Internal_Letter
(Prefix
));
622 for J
in reverse 1 .. Name_Len
loop
623 Name_Buffer
(J
+ 1) := Name_Buffer
(J
);
626 Name_Len
:= Name_Len
+ 1;
627 Name_Buffer
(1) := Prefix
;
631 Name_Buffer
(Name_Len
+ 1 .. Name_Len
+ Suffix
'Length) := Suffix
;
632 Name_Len
:= Name_Len
+ Suffix
'Length;
635 if Suffix_Index
/= 0 then
636 if Suffix_Index
< 0 then
637 Add_Unique_Serial_Number
;
639 Add_Nat_To_Name_Buffer
(Suffix_Index
);
644 end New_External_Name
;
646 function New_External_Name
648 Suffix_Index
: Nat
) return Name_Id
651 Name_Buffer
(1) := Suffix
;
653 Add_Nat_To_Name_Buffer
(Suffix_Index
);
655 end New_External_Name
;
657 -----------------------
658 -- New_Internal_Name --
659 -----------------------
661 function New_Internal_Name
(Id_Char
: Character) return Name_Id
is
663 pragma Assert
(Is_OK_Internal_Letter
(Id_Char
));
664 Name_Buffer
(1) := Id_Char
;
666 Add_Unique_Serial_Number
;
668 end New_Internal_Name
;
670 -----------------------
671 -- New_Occurrence_Of --
672 -----------------------
674 function New_Occurrence_Of
676 Loc
: Source_Ptr
) return Node_Id
678 pragma Assert
(Present
(Def_Id
) and then Nkind
(Def_Id
) in N_Entity
);
679 Occurrence
: Node_Id
;
682 Occurrence
:= New_Node
(N_Identifier
, Loc
);
683 Set_Chars
(Occurrence
, Chars
(Def_Id
));
684 Set_Entity
(Occurrence
, Def_Id
);
686 if Is_Type
(Def_Id
) then
687 Set_Etype
(Occurrence
, Def_Id
);
689 Set_Etype
(Occurrence
, Etype
(Def_Id
));
692 if Ekind
(Def_Id
) = E_Enumeration_Literal
then
693 Set_Is_Static_Expression
(Occurrence
, True);
697 end New_Occurrence_Of
;
704 (New_Node_Kind
: Node_Kind
;
705 New_Sloc
: Source_Ptr
) return Node_Id
707 type Name_Of_Type
is array (N_Op
) of Name_Id
;
708 Name_Of
: constant Name_Of_Type
:= Name_Of_Type
'(
709 N_Op_And => Name_Op_And,
710 N_Op_Or => Name_Op_Or,
711 N_Op_Xor => Name_Op_Xor,
712 N_Op_Eq => Name_Op_Eq,
713 N_Op_Ne => Name_Op_Ne,
714 N_Op_Lt => Name_Op_Lt,
715 N_Op_Le => Name_Op_Le,
716 N_Op_Gt => Name_Op_Gt,
717 N_Op_Ge => Name_Op_Ge,
718 N_Op_Add => Name_Op_Add,
719 N_Op_Subtract => Name_Op_Subtract,
720 N_Op_Concat => Name_Op_Concat,
721 N_Op_Multiply => Name_Op_Multiply,
722 N_Op_Divide => Name_Op_Divide,
723 N_Op_Mod => Name_Op_Mod,
724 N_Op_Rem => Name_Op_Rem,
725 N_Op_Expon => Name_Op_Expon,
726 N_Op_Plus => Name_Op_Add,
727 N_Op_Minus => Name_Op_Subtract,
728 N_Op_Abs => Name_Op_Abs,
729 N_Op_Not => Name_Op_Not,
731 -- We don't really need these shift operators, since they never
732 -- appear as operators in the source, but the path of least
733 -- resistance is to put them in (the aggregate must be complete).
735 N_Op_Rotate_Left => Name_Rotate_Left,
736 N_Op_Rotate_Right => Name_Rotate_Right,
737 N_Op_Shift_Left => Name_Shift_Left,
738 N_Op_Shift_Right => Name_Shift_Right,
739 N_Op_Shift_Right_Arithmetic => Name_Shift_Right_Arithmetic);
741 Nod : constant Node_Id := New_Node (New_Node_Kind, New_Sloc);
744 if New_Node_Kind in Name_Of'Range then
745 Set_Chars (Nod, Name_Of (New_Node_Kind));
751 -----------------------
752 -- New_Suffixed_Name --
753 -----------------------
755 function New_Suffixed_Name
756 (Related_Id : Name_Id;
757 Suffix : String) return Name_Id
760 Get_Name_String (Related_Id);
761 Add_Char_To_Name_Buffer ('_
');
762 Add_Str_To_Name_Buffer (Suffix);
764 end New_Suffixed_Name;
770 function OK_Convert_To (Typ : Entity_Id; Expr : Node_Id) return Node_Id is
774 Make_Type_Conversion (Sloc (Expr),
775 Subtype_Mark => New_Occurrence_Of (Typ, Sloc (Expr)),
776 Expression => Relocate_Node (Expr));
777 Set_Conversion_OK (Result, True);
778 Set_Etype (Result, Typ);
786 procedure Set_NOD (Unit : Node_Id) is
788 Set_Restriction_No_Dependence (Unit, Warn => False);
795 procedure Set_NSA (Asp : Name_Id; OK : out Boolean) is
796 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
798 if Asp_Id = No_Aspect then
802 Set_Restriction_No_Specification_Of_Aspect (Asp_Id);
810 procedure Set_NUA (Attr : Name_Id; OK : out Boolean) is
812 if Is_Attribute_Name (Attr) then
814 Set_Restriction_No_Use_Of_Attribute (Get_Attribute_Id (Attr));
824 procedure Set_NUP (Prag : Name_Id; OK : out Boolean) is
826 if Is_Pragma_Name (Prag) then
828 Set_Restriction_No_Use_Of_Pragma (Get_Pragma_Id (Prag));
834 --------------------------
835 -- Unchecked_Convert_To --
836 --------------------------
838 function Unchecked_Convert_To
840 Expr : Node_Id) return Node_Id
842 Loc : constant Source_Ptr := Sloc (Expr);
844 Expr_Parent : Node_Id;
847 -- If the expression is already of the correct type, then nothing
848 -- to do, except for relocating the node in case this is required.
850 if Present (Etype (Expr))
851 and then (Base_Type (Etype (Expr)) = Typ
852 or else Etype (Expr) = Typ)
854 return Relocate_Node (Expr);
856 -- Cases where the inner expression is itself an unchecked conversion
857 -- to the same type, and we can thus eliminate the outer conversion.
859 elsif Nkind (Expr) = N_Unchecked_Type_Conversion
860 and then Entity (Subtype_Mark (Expr)) = Typ
862 Result := Relocate_Node (Expr);
864 elsif Nkind (Expr) = N_Null
865 and then Is_Access_Type (Typ)
867 -- No need for a conversion
869 Result := Relocate_Node (Expr);
874 -- Capture the parent of the expression before relocating it and
875 -- creating the conversion, so the conversion's parent can be set
876 -- to the original parent below.
878 Expr_Parent := Parent (Expr);
881 Make_Unchecked_Type_Conversion (Loc,
882 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
883 Expression => Relocate_Node (Expr));
885 Set_Parent (Result, Expr_Parent);
888 Set_Etype (Result, Typ);
890 end Unchecked_Convert_To;