1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2012, 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 Einfo
; use Einfo
;
28 with Elists
; use Elists
;
30 with Nlists
; use Nlists
;
31 with Nmake
; use Nmake
;
33 with Restrict
; use Restrict
;
34 with Rident
; use Rident
;
35 with Sem_Aux
; use Sem_Aux
;
36 with Snames
; use Snames
;
37 with Stand
; use Stand
;
38 with Stringt
; use Stringt
;
39 with Urealp
; use Urealp
;
41 package body Tbuild
is
43 -----------------------
44 -- Local Subprograms --
45 -----------------------
47 procedure Add_Unique_Serial_Number
;
48 -- Add a unique serialization to the string in the Name_Buffer. This
49 -- consists of a unit specific serial number, and b/s for body/spec.
51 ------------------------------
52 -- Add_Unique_Serial_Number --
53 ------------------------------
55 Config_Serial_Number
: Nat
:= 0;
56 -- Counter for use in config pragmas, see comment below
58 procedure Add_Unique_Serial_Number
is
60 -- If we are analyzing configuration pragmas, Cunit (Main_Unit) will
61 -- not be set yet. This happens for example when analyzing static
62 -- string expressions in configuration pragmas. For this case, we
63 -- just maintain a local counter, defined above and we do not need
64 -- to add a b or s indication in this case.
66 if No
(Cunit
(Current_Sem_Unit
)) then
67 Config_Serial_Number
:= Config_Serial_Number
+ 1;
68 Add_Nat_To_Name_Buffer
(Config_Serial_Number
);
71 -- Normal case, within a unit
75 Unit_Node
: constant Node_Id
:= Unit
(Cunit
(Current_Sem_Unit
));
78 Add_Nat_To_Name_Buffer
(Increment_Serial_Number
);
80 -- Add either b or s, depending on whether current unit is a spec
81 -- or a body. This is needed because we may generate the same name
82 -- in a spec and a body otherwise.
84 Name_Len
:= Name_Len
+ 1;
86 if Nkind
(Unit_Node
) = N_Package_Declaration
87 or else Nkind
(Unit_Node
) = N_Subprogram_Declaration
88 or else Nkind
(Unit_Node
) in N_Generic_Declaration
90 Name_Buffer
(Name_Len
) := 's';
92 Name_Buffer
(Name_Len
) := 'b';
96 end Add_Unique_Serial_Number
;
102 function Checks_Off
(N
: Node_Id
) return Node_Id
is
105 Make_Unchecked_Expression
(Sloc
(N
),
113 function Convert_To
(Typ
: Entity_Id
; Expr
: Node_Id
) return Node_Id
is
117 if Present
(Etype
(Expr
))
118 and then (Etype
(Expr
)) = Typ
120 return Relocate_Node
(Expr
);
123 Make_Type_Conversion
(Sloc
(Expr
),
124 Subtype_Mark
=> New_Occurrence_Of
(Typ
, Sloc
(Expr
)),
125 Expression
=> Relocate_Node
(Expr
));
127 Set_Etype
(Result
, Typ
);
132 ----------------------------
133 -- Convert_To_And_Rewrite --
134 ----------------------------
136 procedure Convert_To_And_Rewrite
(Typ
: Entity_Id
; Expr
: Node_Id
) is
138 Rewrite
(Expr
, Convert_To
(Typ
, Expr
));
139 end Convert_To_And_Rewrite
;
145 procedure Discard_List
(L
: List_Id
) is
146 pragma Warnings
(Off
, L
);
155 procedure Discard_Node
(N
: Node_Or_Entity_Id
) is
156 pragma Warnings
(Off
, N
);
161 -------------------------------------------
162 -- Make_Byte_Aligned_Attribute_Reference --
163 -------------------------------------------
165 function Make_Byte_Aligned_Attribute_Reference
168 Attribute_Name
: Name_Id
)
171 N
: constant Node_Id
:=
172 Make_Attribute_Reference
(Sloc
,
174 Attribute_Name
=> Attribute_Name
);
177 pragma Assert
(Attribute_Name
= Name_Address
179 Attribute_Name
= Name_Unrestricted_Access
);
180 Set_Must_Be_Byte_Aligned
(N
, True);
182 end Make_Byte_Aligned_Attribute_Reference
;
188 function Make_DT_Access
191 Typ
: Entity_Id
) return Node_Id
193 Full_Type
: Entity_Id
:= Typ
;
196 if Is_Private_Type
(Typ
) then
197 Full_Type
:= Underlying_Type
(Typ
);
201 Unchecked_Convert_To
(
203 (Etype
(Node
(First_Elmt
(Access_Disp_Table
(Full_Type
)))), Loc
),
204 Make_Selected_Component
(Loc
,
205 Prefix
=> New_Copy
(Rec
),
207 New_Reference_To
(First_Tag_Component
(Full_Type
), Loc
)));
210 ------------------------
211 -- Make_Float_Literal --
212 ------------------------
214 function Make_Float_Literal
218 Exponent
: Uint
) return Node_Id
221 if Radix
= 2 and then abs Significand
/= 1 then
225 Significand
* Radix
**(Exponent
mod 4),
230 N
: constant Node_Id
:= New_Node
(N_Real_Literal
, Loc
);
235 (Num
=> abs Significand
,
237 Rbase
=> UI_To_Int
(Radix
),
238 Negative
=> Significand
< 0));
242 end Make_Float_Literal
;
244 -------------------------------------
245 -- Make_Implicit_Exception_Handler --
246 -------------------------------------
248 function Make_Implicit_Exception_Handler
250 Choice_Parameter
: Node_Id
:= Empty
;
251 Exception_Choices
: List_Id
;
252 Statements
: List_Id
) return Node_Id
258 -- Set the source location only when debugging the expanded code
260 -- When debugging the source code directly, we do not want the compiler
261 -- to associate this implicit exception handler with any specific source
262 -- line, because it can potentially confuse the debugger. The most
263 -- damaging situation would arise when the debugger tries to insert a
264 -- breakpoint at a certain line. If the code of the associated implicit
265 -- exception handler is generated before the code of that line, then the
266 -- debugger will end up inserting the breakpoint inside the exception
267 -- handler, rather than the code the user intended to break on. As a
268 -- result, it is likely that the program will not hit the breakpoint
271 if Debug_Generated_Code
then
278 Make_Exception_Handler
279 (Loc
, Choice_Parameter
, Exception_Choices
, Statements
);
280 Set_Local_Raise_Statements
(Handler
, No_Elist
);
282 end Make_Implicit_Exception_Handler
;
284 --------------------------------
285 -- Make_Implicit_If_Statement --
286 --------------------------------
288 function Make_Implicit_If_Statement
291 Then_Statements
: List_Id
;
292 Elsif_Parts
: List_Id
:= No_List
;
293 Else_Statements
: List_Id
:= No_List
) return Node_Id
296 Check_Restriction
(No_Implicit_Conditionals
, Node
);
298 return Make_If_Statement
(Sloc
(Node
),
303 end Make_Implicit_If_Statement
;
305 -------------------------------------
306 -- Make_Implicit_Label_Declaration --
307 -------------------------------------
309 function Make_Implicit_Label_Declaration
311 Defining_Identifier
: Node_Id
;
312 Label_Construct
: Node_Id
) return Node_Id
314 N
: constant Node_Id
:=
315 Make_Implicit_Label_Declaration
(Loc
, Defining_Identifier
);
317 Set_Label_Construct
(N
, Label_Construct
);
319 end Make_Implicit_Label_Declaration
;
321 ----------------------------------
322 -- Make_Implicit_Loop_Statement --
323 ----------------------------------
325 function Make_Implicit_Loop_Statement
327 Statements
: List_Id
;
328 Identifier
: Node_Id
:= Empty
;
329 Iteration_Scheme
: Node_Id
:= Empty
;
330 Has_Created_Identifier
: Boolean := False;
331 End_Label
: Node_Id
:= Empty
) return Node_Id
334 Check_Restriction
(No_Implicit_Loops
, Node
);
336 if Present
(Iteration_Scheme
)
337 and then Present
(Condition
(Iteration_Scheme
))
339 Check_Restriction
(No_Implicit_Conditionals
, Node
);
342 return Make_Loop_Statement
(Sloc
(Node
),
343 Identifier
=> Identifier
,
344 Iteration_Scheme
=> Iteration_Scheme
,
345 Statements
=> Statements
,
346 Has_Created_Identifier
=> Has_Created_Identifier
,
347 End_Label
=> End_Label
);
348 end Make_Implicit_Loop_Statement
;
350 --------------------------
351 -- Make_Integer_Literal --
352 ---------------------------
354 function Make_Integer_Literal
356 Intval
: Int
) return Node_Id
359 return Make_Integer_Literal
(Loc
, UI_From_Int
(Intval
));
360 end Make_Integer_Literal
;
362 --------------------------------
363 -- Make_Linker_Section_Pragma --
364 --------------------------------
366 function Make_Linker_Section_Pragma
369 Sec
: String) return Node_Id
379 (Make_Pragma_Argument_Association
381 Expression
=> New_Occurrence_Of
(Ent
, Loc
)),
382 Make_Pragma_Argument_Association
389 Set_Has_Gigi_Rep_Item
(Ent
);
391 end Make_Linker_Section_Pragma
;
400 Pragma_Argument_Associations
: List_Id
:= No_List
) return Node_Id
405 Pragma_Argument_Associations
=> Pragma_Argument_Associations
,
406 Pragma_Identifier
=> Make_Identifier
(Sloc
, Chars
));
409 ---------------------------------
410 -- Make_Raise_Constraint_Error --
411 ---------------------------------
413 function Make_Raise_Constraint_Error
415 Condition
: Node_Id
:= Empty
;
416 Reason
: RT_Exception_Code
) return Node_Id
419 pragma Assert
(Reason
in RT_CE_Exceptions
);
421 Make_Raise_Constraint_Error
(Sloc
,
422 Condition
=> Condition
,
424 UI_From_Int
(RT_Exception_Code
'Pos (Reason
)));
425 end Make_Raise_Constraint_Error
;
427 ------------------------------
428 -- Make_Raise_Program_Error --
429 ------------------------------
431 function Make_Raise_Program_Error
433 Condition
: Node_Id
:= Empty
;
434 Reason
: RT_Exception_Code
) return Node_Id
437 pragma Assert
(Reason
in RT_PE_Exceptions
);
439 Make_Raise_Program_Error
(Sloc
,
440 Condition
=> Condition
,
442 UI_From_Int
(RT_Exception_Code
'Pos (Reason
)));
443 end Make_Raise_Program_Error
;
445 ------------------------------
446 -- Make_Raise_Storage_Error --
447 ------------------------------
449 function Make_Raise_Storage_Error
451 Condition
: Node_Id
:= Empty
;
452 Reason
: RT_Exception_Code
) return Node_Id
455 pragma Assert
(Reason
in RT_SE_Exceptions
);
457 Make_Raise_Storage_Error
(Sloc
,
458 Condition
=> Condition
,
460 UI_From_Int
(RT_Exception_Code
'Pos (Reason
)));
461 end Make_Raise_Storage_Error
;
463 -------------------------
464 -- Make_String_Literal --
465 -------------------------
467 function Make_String_Literal
469 Strval
: String) return Node_Id
473 Store_String_Chars
(Strval
);
475 Make_String_Literal
(Sloc
,
476 Strval
=> End_String
);
477 end Make_String_Literal
;
483 function Make_Temporary
486 Related_Node
: Node_Id
:= Empty
) return Entity_Id
488 Temp
: constant Entity_Id
:=
489 Make_Defining_Identifier
(Loc
,
490 Chars
=> New_Internal_Name
(Id
));
492 Set_Related_Expression
(Temp
, Related_Node
);
496 ---------------------------
497 -- Make_Unsuppress_Block --
498 ---------------------------
500 -- Generates the following expansion:
503 -- pragma Suppress (<check>);
508 function Make_Unsuppress_Block
511 Stmts
: List_Id
) return Node_Id
515 Make_Block_Statement
(Loc
,
516 Declarations
=> New_List
(
518 Chars
=> Name_Suppress
,
519 Pragma_Argument_Associations
=> New_List
(
520 Make_Pragma_Argument_Association
(Loc
,
521 Expression
=> Make_Identifier
(Loc
, Check
))))),
523 Handled_Statement_Sequence
=>
524 Make_Handled_Sequence_Of_Statements
(Loc
,
525 Statements
=> Stmts
));
526 end Make_Unsuppress_Block
;
528 --------------------------
529 -- New_Constraint_Error --
530 --------------------------
532 function New_Constraint_Error
(Loc
: Source_Ptr
) return Node_Id
is
533 Ident_Node
: Node_Id
;
534 Raise_Node
: Node_Id
;
537 Ident_Node
:= New_Node
(N_Identifier
, Loc
);
538 Set_Chars
(Ident_Node
, Chars
(Standard_Entity
(S_Constraint_Error
)));
539 Set_Entity
(Ident_Node
, Standard_Entity
(S_Constraint_Error
));
540 Raise_Node
:= New_Node
(N_Raise_Statement
, Loc
);
541 Set_Name
(Raise_Node
, Ident_Node
);
543 end New_Constraint_Error
;
545 -----------------------
546 -- New_External_Name --
547 -----------------------
549 function New_External_Name
550 (Related_Id
: Name_Id
;
551 Suffix
: Character := ' ';
552 Suffix_Index
: Int
:= 0;
553 Prefix
: Character := ' ') return Name_Id
556 Get_Name_String
(Related_Id
);
558 if Prefix
/= ' ' then
559 pragma Assert
(Is_OK_Internal_Letter
(Prefix
) or else Prefix
= '_');
561 for J
in reverse 1 .. Name_Len
loop
562 Name_Buffer
(J
+ 1) := Name_Buffer
(J
);
565 Name_Len
:= Name_Len
+ 1;
566 Name_Buffer
(1) := Prefix
;
569 if Suffix
/= ' ' then
570 pragma Assert
(Is_OK_Internal_Letter
(Suffix
));
571 Add_Char_To_Name_Buffer
(Suffix
);
574 if Suffix_Index
/= 0 then
575 if Suffix_Index
< 0 then
576 Add_Unique_Serial_Number
;
578 Add_Nat_To_Name_Buffer
(Suffix_Index
);
583 end New_External_Name
;
585 function New_External_Name
586 (Related_Id
: Name_Id
;
588 Suffix_Index
: Int
:= 0;
589 Prefix
: Character := ' ') return Name_Id
592 Get_Name_String
(Related_Id
);
594 if Prefix
/= ' ' then
595 pragma Assert
(Is_OK_Internal_Letter
(Prefix
));
597 for J
in reverse 1 .. Name_Len
loop
598 Name_Buffer
(J
+ 1) := Name_Buffer
(J
);
601 Name_Len
:= Name_Len
+ 1;
602 Name_Buffer
(1) := Prefix
;
606 Name_Buffer
(Name_Len
+ 1 .. Name_Len
+ Suffix
'Length) := Suffix
;
607 Name_Len
:= Name_Len
+ Suffix
'Length;
610 if Suffix_Index
/= 0 then
611 if Suffix_Index
< 0 then
612 Add_Unique_Serial_Number
;
614 Add_Nat_To_Name_Buffer
(Suffix_Index
);
619 end New_External_Name
;
621 function New_External_Name
623 Suffix_Index
: Nat
) return Name_Id
626 Name_Buffer
(1) := Suffix
;
628 Add_Nat_To_Name_Buffer
(Suffix_Index
);
630 end New_External_Name
;
632 -----------------------
633 -- New_Internal_Name --
634 -----------------------
636 function New_Internal_Name
(Id_Char
: Character) return Name_Id
is
638 pragma Assert
(Is_OK_Internal_Letter
(Id_Char
));
639 Name_Buffer
(1) := Id_Char
;
641 Add_Unique_Serial_Number
;
643 end New_Internal_Name
;
645 -----------------------
646 -- New_Occurrence_Of --
647 -----------------------
649 function New_Occurrence_Of
651 Loc
: Source_Ptr
) return Node_Id
653 Occurrence
: Node_Id
;
656 Occurrence
:= New_Node
(N_Identifier
, Loc
);
657 Set_Chars
(Occurrence
, Chars
(Def_Id
));
658 Set_Entity
(Occurrence
, Def_Id
);
660 if Is_Type
(Def_Id
) then
661 Set_Etype
(Occurrence
, Def_Id
);
663 Set_Etype
(Occurrence
, Etype
(Def_Id
));
667 end New_Occurrence_Of
;
674 (New_Node_Kind
: Node_Kind
;
675 New_Sloc
: Source_Ptr
) return Node_Id
677 type Name_Of_Type
is array (N_Op
) of Name_Id
;
678 Name_Of
: constant Name_Of_Type
:= Name_Of_Type
'(
679 N_Op_And => Name_Op_And,
680 N_Op_Or => Name_Op_Or,
681 N_Op_Xor => Name_Op_Xor,
682 N_Op_Eq => Name_Op_Eq,
683 N_Op_Ne => Name_Op_Ne,
684 N_Op_Lt => Name_Op_Lt,
685 N_Op_Le => Name_Op_Le,
686 N_Op_Gt => Name_Op_Gt,
687 N_Op_Ge => Name_Op_Ge,
688 N_Op_Add => Name_Op_Add,
689 N_Op_Subtract => Name_Op_Subtract,
690 N_Op_Concat => Name_Op_Concat,
691 N_Op_Multiply => Name_Op_Multiply,
692 N_Op_Divide => Name_Op_Divide,
693 N_Op_Mod => Name_Op_Mod,
694 N_Op_Rem => Name_Op_Rem,
695 N_Op_Expon => Name_Op_Expon,
696 N_Op_Plus => Name_Op_Add,
697 N_Op_Minus => Name_Op_Subtract,
698 N_Op_Abs => Name_Op_Abs,
699 N_Op_Not => Name_Op_Not,
701 -- We don't really need these shift operators, since they never
702 -- appear as operators in the source, but the path of least
703 -- resistance is to put them in (the aggregate must be complete).
705 N_Op_Rotate_Left => Name_Rotate_Left,
706 N_Op_Rotate_Right => Name_Rotate_Right,
707 N_Op_Shift_Left => Name_Shift_Left,
708 N_Op_Shift_Right => Name_Shift_Right,
709 N_Op_Shift_Right_Arithmetic => Name_Shift_Right_Arithmetic);
711 Nod : constant Node_Id := New_Node (New_Node_Kind, New_Sloc);
714 if New_Node_Kind in Name_Of'Range then
715 Set_Chars (Nod, Name_Of (New_Node_Kind));
721 ----------------------
722 -- New_Reference_To --
723 ----------------------
725 function New_Reference_To
727 Loc : Source_Ptr) return Node_Id
729 pragma Assert (Nkind (Def_Id) in N_Entity);
730 Occurrence : Node_Id;
732 Occurrence := New_Node (N_Identifier, Loc);
733 Set_Chars (Occurrence, Chars (Def_Id));
734 Set_Entity (Occurrence, Def_Id);
736 end New_Reference_To;
738 -----------------------
739 -- New_Suffixed_Name --
740 -----------------------
742 function New_Suffixed_Name
743 (Related_Id : Name_Id;
744 Suffix : String) return Name_Id
747 Get_Name_String (Related_Id);
748 Add_Char_To_Name_Buffer ('_
');
749 Add_Str_To_Name_Buffer (Suffix);
751 end New_Suffixed_Name;
757 function OK_Convert_To (Typ : Entity_Id; Expr : Node_Id) return Node_Id is
761 Make_Type_Conversion (Sloc (Expr),
762 Subtype_Mark => New_Occurrence_Of (Typ, Sloc (Expr)),
763 Expression => Relocate_Node (Expr));
764 Set_Conversion_OK (Result, True);
765 Set_Etype (Result, Typ);
769 --------------------------
770 -- Unchecked_Convert_To --
771 --------------------------
773 function Unchecked_Convert_To
775 Expr : Node_Id) return Node_Id
777 Loc : constant Source_Ptr := Sloc (Expr);
779 Expr_Parent : Node_Id;
782 -- If the expression is already of the correct type, then nothing
783 -- to do, except for relocating the node in case this is required.
785 if Present (Etype (Expr))
786 and then (Base_Type (Etype (Expr)) = Typ
787 or else Etype (Expr) = Typ)
789 return Relocate_Node (Expr);
791 -- Cases where the inner expression is itself an unchecked conversion
792 -- to the same type, and we can thus eliminate the outer conversion.
794 elsif Nkind (Expr) = N_Unchecked_Type_Conversion
795 and then Entity (Subtype_Mark (Expr)) = Typ
797 Result := Relocate_Node (Expr);
799 elsif Nkind (Expr) = N_Null
800 and then Is_Access_Type (Typ)
802 -- No need for a conversion
804 Result := Relocate_Node (Expr);
809 -- Capture the parent of the expression before relocating it and
810 -- creating the conversion, so the conversion's parent can be set
811 -- to the original parent below.
813 Expr_Parent := Parent (Expr);
816 Make_Unchecked_Type_Conversion (Loc,
817 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
818 Expression => Relocate_Node (Expr));
820 Set_Parent (Result, Expr_Parent);
823 Set_Etype (Result, Typ);
825 end Unchecked_Convert_To;