1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2007, 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 Sinfo
; use Sinfo
;
36 with Snames
; use Snames
;
37 with Stand
; use Stand
;
38 with Stringt
; use Stringt
;
39 with Uintp
; use Uintp
;
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
);
136 procedure Discard_List
(L
: List_Id
) is
137 pragma Warnings
(Off
, L
);
146 procedure Discard_Node
(N
: Node_Or_Entity_Id
) is
147 pragma Warnings
(Off
, N
);
152 -------------------------------------------
153 -- Make_Byte_Aligned_Attribute_Reference --
154 -------------------------------------------
156 function Make_Byte_Aligned_Attribute_Reference
159 Attribute_Name
: Name_Id
)
162 N
: constant Node_Id
:=
163 Make_Attribute_Reference
(Sloc
,
165 Attribute_Name
=> Attribute_Name
);
168 pragma Assert
(Attribute_Name
= Name_Address
170 Attribute_Name
= Name_Unrestricted_Access
);
171 Set_Must_Be_Byte_Aligned
(N
, True);
173 end Make_Byte_Aligned_Attribute_Reference
;
179 function Make_DT_Access
182 Typ
: Entity_Id
) return Node_Id
184 Full_Type
: Entity_Id
:= Typ
;
187 if Is_Private_Type
(Typ
) then
188 Full_Type
:= Underlying_Type
(Typ
);
192 Unchecked_Convert_To
(
194 (Etype
(Node
(First_Elmt
(Access_Disp_Table
(Full_Type
)))), Loc
),
195 Make_Selected_Component
(Loc
,
196 Prefix
=> New_Copy
(Rec
),
198 New_Reference_To
(First_Tag_Component
(Full_Type
), Loc
)));
201 -------------------------------------
202 -- Make_Implicit_Exception_Handler --
203 -------------------------------------
205 function Make_Implicit_Exception_Handler
207 Choice_Parameter
: Node_Id
:= Empty
;
208 Exception_Choices
: List_Id
;
209 Statements
: List_Id
) return Node_Id
215 -- Set the source location only when debugging the expanded code
217 -- When debugging the source code directly, we do not want the compiler
218 -- to associate this implicit exception handler with any specific source
219 -- line, because it can potentially confuse the debugger. The most
220 -- damaging situation would arise when the debugger tries to insert a
221 -- breakpoint at a certain line. If the code of the associated implicit
222 -- exception handler is generated before the code of that line, then the
223 -- debugger will end up inserting the breakpoint inside the exception
224 -- handler, rather than the code the user intended to break on. As a
225 -- result, it is likely that the program will not hit the breakpoint
228 if Debug_Generated_Code
then
235 Make_Exception_Handler
236 (Loc
, Choice_Parameter
, Exception_Choices
, Statements
);
237 Set_Local_Raise_Statements
(Handler
, No_Elist
);
239 end Make_Implicit_Exception_Handler
;
241 --------------------------------
242 -- Make_Implicit_If_Statement --
243 --------------------------------
245 function Make_Implicit_If_Statement
248 Then_Statements
: List_Id
;
249 Elsif_Parts
: List_Id
:= No_List
;
250 Else_Statements
: List_Id
:= No_List
) return Node_Id
253 Check_Restriction
(No_Implicit_Conditionals
, Node
);
255 return Make_If_Statement
(Sloc
(Node
),
260 end Make_Implicit_If_Statement
;
262 -------------------------------------
263 -- Make_Implicit_Label_Declaration --
264 -------------------------------------
266 function Make_Implicit_Label_Declaration
268 Defining_Identifier
: Node_Id
;
269 Label_Construct
: Node_Id
) return Node_Id
271 N
: constant Node_Id
:=
272 Make_Implicit_Label_Declaration
(Loc
, Defining_Identifier
);
274 Set_Label_Construct
(N
, Label_Construct
);
276 end Make_Implicit_Label_Declaration
;
278 ----------------------------------
279 -- Make_Implicit_Loop_Statement --
280 ----------------------------------
282 function Make_Implicit_Loop_Statement
284 Statements
: List_Id
;
285 Identifier
: Node_Id
:= Empty
;
286 Iteration_Scheme
: Node_Id
:= Empty
;
287 Has_Created_Identifier
: Boolean := False;
288 End_Label
: Node_Id
:= Empty
) return Node_Id
291 Check_Restriction
(No_Implicit_Loops
, Node
);
293 if Present
(Iteration_Scheme
)
294 and then Present
(Condition
(Iteration_Scheme
))
296 Check_Restriction
(No_Implicit_Conditionals
, Node
);
299 return Make_Loop_Statement
(Sloc
(Node
),
300 Identifier
=> Identifier
,
301 Iteration_Scheme
=> Iteration_Scheme
,
302 Statements
=> Statements
,
303 Has_Created_Identifier
=> Has_Created_Identifier
,
304 End_Label
=> End_Label
);
305 end Make_Implicit_Loop_Statement
;
307 --------------------------
308 -- Make_Integer_Literal --
309 ---------------------------
311 function Make_Integer_Literal
313 Intval
: Int
) return Node_Id
316 return Make_Integer_Literal
(Loc
, UI_From_Int
(Intval
));
317 end Make_Integer_Literal
;
319 --------------------------------
320 -- Make_Linker_Section_Pragma --
321 --------------------------------
323 function Make_Linker_Section_Pragma
326 Sec
: String) return Node_Id
336 (Make_Pragma_Argument_Association
338 Expression
=> New_Occurrence_Of
(Ent
, Loc
)),
339 Make_Pragma_Argument_Association
346 Set_Has_Gigi_Rep_Item
(Ent
);
348 end Make_Linker_Section_Pragma
;
350 ---------------------------------
351 -- Make_Raise_Constraint_Error --
352 ---------------------------------
354 function Make_Raise_Constraint_Error
356 Condition
: Node_Id
:= Empty
;
357 Reason
: RT_Exception_Code
) return Node_Id
360 pragma Assert
(Reason
in RT_CE_Exceptions
);
362 Make_Raise_Constraint_Error
(Sloc
,
363 Condition
=> Condition
,
365 UI_From_Int
(RT_Exception_Code
'Pos (Reason
)));
366 end Make_Raise_Constraint_Error
;
368 ------------------------------
369 -- Make_Raise_Program_Error --
370 ------------------------------
372 function Make_Raise_Program_Error
374 Condition
: Node_Id
:= Empty
;
375 Reason
: RT_Exception_Code
) return Node_Id
378 pragma Assert
(Reason
in RT_PE_Exceptions
);
380 Make_Raise_Program_Error
(Sloc
,
381 Condition
=> Condition
,
383 UI_From_Int
(RT_Exception_Code
'Pos (Reason
)));
384 end Make_Raise_Program_Error
;
386 ------------------------------
387 -- Make_Raise_Storage_Error --
388 ------------------------------
390 function Make_Raise_Storage_Error
392 Condition
: Node_Id
:= Empty
;
393 Reason
: RT_Exception_Code
) return Node_Id
396 pragma Assert
(Reason
in RT_SE_Exceptions
);
398 Make_Raise_Storage_Error
(Sloc
,
399 Condition
=> Condition
,
401 UI_From_Int
(RT_Exception_Code
'Pos (Reason
)));
402 end Make_Raise_Storage_Error
;
404 -------------------------
405 -- Make_String_Literal --
406 -------------------------
408 function Make_String_Literal
410 Strval
: String) return Node_Id
414 Store_String_Chars
(Strval
);
416 Make_String_Literal
(Sloc
,
417 Strval
=> End_String
);
418 end Make_String_Literal
;
420 ---------------------------
421 -- Make_Unsuppress_Block --
422 ---------------------------
424 -- Generates the following expansion:
427 -- pragma Suppress (<check>);
432 function Make_Unsuppress_Block
435 Stmts
: List_Id
) return Node_Id
439 Make_Block_Statement
(Loc
,
440 Declarations
=> New_List
(
442 Chars
=> Name_Suppress
,
443 Pragma_Argument_Associations
=> New_List
(
444 Make_Pragma_Argument_Association
(Loc
,
445 Expression
=> Make_Identifier
(Loc
, Check
))))),
447 Handled_Statement_Sequence
=>
448 Make_Handled_Sequence_Of_Statements
(Loc
,
449 Statements
=> Stmts
));
450 end Make_Unsuppress_Block
;
452 --------------------------
453 -- New_Constraint_Error --
454 --------------------------
456 function New_Constraint_Error
(Loc
: Source_Ptr
) return Node_Id
is
457 Ident_Node
: Node_Id
;
458 Raise_Node
: Node_Id
;
461 Ident_Node
:= New_Node
(N_Identifier
, Loc
);
462 Set_Chars
(Ident_Node
, Chars
(Standard_Entity
(S_Constraint_Error
)));
463 Set_Entity
(Ident_Node
, Standard_Entity
(S_Constraint_Error
));
464 Raise_Node
:= New_Node
(N_Raise_Statement
, Loc
);
465 Set_Name
(Raise_Node
, Ident_Node
);
467 end New_Constraint_Error
;
469 -----------------------
470 -- New_External_Name --
471 -----------------------
473 function New_External_Name
474 (Related_Id
: Name_Id
;
475 Suffix
: Character := ' ';
476 Suffix_Index
: Int
:= 0;
477 Prefix
: Character := ' ') return Name_Id
480 Get_Name_String
(Related_Id
);
482 if Prefix
/= ' ' then
483 pragma Assert
(Is_OK_Internal_Letter
(Prefix
));
485 for J
in reverse 1 .. Name_Len
loop
486 Name_Buffer
(J
+ 1) := Name_Buffer
(J
);
489 Name_Len
:= Name_Len
+ 1;
490 Name_Buffer
(1) := Prefix
;
493 if Suffix
/= ' ' then
494 pragma Assert
(Is_OK_Internal_Letter
(Suffix
));
495 Name_Len
:= Name_Len
+ 1;
496 Name_Buffer
(Name_Len
) := Suffix
;
499 if Suffix_Index
/= 0 then
500 if Suffix_Index
< 0 then
501 Add_Unique_Serial_Number
;
503 Add_Nat_To_Name_Buffer
(Suffix_Index
);
508 end New_External_Name
;
510 function New_External_Name
511 (Related_Id
: Name_Id
;
513 Suffix_Index
: Int
:= 0;
514 Prefix
: Character := ' ') return Name_Id
517 Get_Name_String
(Related_Id
);
519 if Prefix
/= ' ' then
520 pragma Assert
(Is_OK_Internal_Letter
(Prefix
));
522 for J
in reverse 1 .. Name_Len
loop
523 Name_Buffer
(J
+ 1) := Name_Buffer
(J
);
526 Name_Len
:= Name_Len
+ 1;
527 Name_Buffer
(1) := Prefix
;
531 Name_Buffer
(Name_Len
+ 1 .. Name_Len
+ Suffix
'Length) := Suffix
;
532 Name_Len
:= Name_Len
+ Suffix
'Length;
535 if Suffix_Index
/= 0 then
536 if Suffix_Index
< 0 then
537 Add_Unique_Serial_Number
;
539 Add_Nat_To_Name_Buffer
(Suffix_Index
);
544 end New_External_Name
;
546 function New_External_Name
548 Suffix_Index
: Nat
) return Name_Id
551 Name_Buffer
(1) := Suffix
;
553 Add_Nat_To_Name_Buffer
(Suffix_Index
);
555 end New_External_Name
;
557 -----------------------
558 -- New_Internal_Name --
559 -----------------------
561 function New_Internal_Name
(Id_Char
: Character) return Name_Id
is
563 pragma Assert
(Is_OK_Internal_Letter
(Id_Char
));
564 Name_Buffer
(1) := Id_Char
;
566 Add_Unique_Serial_Number
;
568 end New_Internal_Name
;
570 -----------------------
571 -- New_Occurrence_Of --
572 -----------------------
574 function New_Occurrence_Of
576 Loc
: Source_Ptr
) return Node_Id
578 Occurrence
: Node_Id
;
581 Occurrence
:= New_Node
(N_Identifier
, Loc
);
582 Set_Chars
(Occurrence
, Chars
(Def_Id
));
583 Set_Entity
(Occurrence
, Def_Id
);
585 if Is_Type
(Def_Id
) then
586 Set_Etype
(Occurrence
, Def_Id
);
588 Set_Etype
(Occurrence
, Etype
(Def_Id
));
592 end New_Occurrence_Of
;
594 ----------------------
595 -- New_Reference_To --
596 ----------------------
598 function New_Reference_To
600 Loc
: Source_Ptr
) return Node_Id
602 Occurrence
: Node_Id
;
605 Occurrence
:= New_Node
(N_Identifier
, Loc
);
606 Set_Chars
(Occurrence
, Chars
(Def_Id
));
607 Set_Entity
(Occurrence
, Def_Id
);
609 end New_Reference_To
;
611 -----------------------
612 -- New_Suffixed_Name --
613 -----------------------
615 function New_Suffixed_Name
616 (Related_Id
: Name_Id
;
617 Suffix
: String) return Name_Id
620 Get_Name_String
(Related_Id
);
621 Name_Len
:= Name_Len
+ 1;
622 Name_Buffer
(Name_Len
) := '_';
623 Name_Buffer
(Name_Len
+ 1 .. Name_Len
+ Suffix
'Length) := Suffix
;
624 Name_Len
:= Name_Len
+ Suffix
'Length;
626 end New_Suffixed_Name
;
632 function OK_Convert_To
(Typ
: Entity_Id
; Expr
: Node_Id
) return Node_Id
is
636 Make_Type_Conversion
(Sloc
(Expr
),
637 Subtype_Mark
=> New_Occurrence_Of
(Typ
, Sloc
(Expr
)),
638 Expression
=> Relocate_Node
(Expr
));
639 Set_Conversion_OK
(Result
, True);
640 Set_Etype
(Result
, Typ
);
644 --------------------------
645 -- Unchecked_Convert_To --
646 --------------------------
648 function Unchecked_Convert_To
650 Expr
: Node_Id
) return Node_Id
652 Loc
: constant Source_Ptr
:= Sloc
(Expr
);
656 -- If the expression is already of the correct type, then nothing
657 -- to do, except for relocating the node in case this is required.
659 if Present
(Etype
(Expr
))
660 and then (Base_Type
(Etype
(Expr
)) = Typ
661 or else Etype
(Expr
) = Typ
)
663 return Relocate_Node
(Expr
);
665 -- Cases where the inner expression is itself an unchecked conversion
666 -- to the same type, and we can thus eliminate the outer conversion.
668 elsif Nkind
(Expr
) = N_Unchecked_Type_Conversion
669 and then Entity
(Subtype_Mark
(Expr
)) = Typ
671 Result
:= Relocate_Node
(Expr
);
673 elsif Nkind
(Expr
) = N_Null
674 and then Is_Access_Type
(Typ
)
676 -- No need for a conversion
678 Result
:= Relocate_Node
(Expr
);
684 Make_Unchecked_Type_Conversion
(Loc
,
685 Subtype_Mark
=> New_Occurrence_Of
(Typ
, Loc
),
686 Expression
=> Relocate_Node
(Expr
));
689 Set_Etype
(Result
, Typ
);
691 end Unchecked_Convert_To
;