1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2005, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
31 with Namet; use Namet;
32 with Nlists; use Nlists;
33 with Nmake; use Nmake;
34 with Restrict; use Restrict;
35 with Rident; use Rident;
36 with Sinfo; use Sinfo;
37 with Snames; use Snames;
38 with Stand; use Stand;
39 with Stringt; use Stringt;
40 with Uintp; use Uintp;
42 package body Tbuild is
44 -----------------------
45 -- Local Subprograms --
46 -----------------------
48 procedure Add_Unique_Serial_Number;
49 -- Add a unique serialization to the string in the Name_Buffer. This
50 -- consists of a unit specific serial number, and b/s for body/spec.
52 ------------------------------
53 -- Add_Unique_Serial_Number --
54 ------------------------------
56 procedure Add_Unique_Serial_Number is
57 Unit_Node : constant Node_Id := Unit (Cunit (Current_Sem_Unit));
60 Add_Nat_To_Name_Buffer (Increment_Serial_Number);
62 -- Add either b or s, depending on whether current unit is a spec
63 -- or a body. This is needed because we may generate the same name
64 -- in a spec and a body otherwise.
66 Name_Len := Name_Len + 1;
68 if Nkind (Unit_Node) = N_Package_Declaration
69 or else Nkind (Unit_Node) = N_Subprogram_Declaration
70 or else Nkind (Unit_Node) in N_Generic_Declaration
72 Name_Buffer (Name_Len) := 's';
74 Name_Buffer (Name_Len) := 'b';
76 end Add_Unique_Serial_Number;
82 function Checks_Off (N : Node_Id) return Node_Id is
85 Make_Unchecked_Expression (Sloc (N),
93 function Convert_To (Typ : Entity_Id; Expr : Node_Id) return Node_Id is
97 if Present (Etype (Expr))
98 and then (Etype (Expr)) = Typ
100 return Relocate_Node (Expr);
103 Make_Type_Conversion (Sloc (Expr),
104 Subtype_Mark => New_Occurrence_Of (Typ, Sloc (Expr)),
105 Expression => Relocate_Node (Expr));
107 Set_Etype (Result, Typ);
116 procedure Discard_List (L : List_Id) is
117 pragma Warnings (Off, L);
126 procedure Discard_Node (N : Node_Or_Entity_Id) is
127 pragma Warnings (Off, N);
132 -------------------------------------------
133 -- Make_Byte_Aligned_Attribute_Reference --
134 -------------------------------------------
136 function Make_Byte_Aligned_Attribute_Reference
139 Attribute_Name : Name_Id)
142 N : constant Node_Id :=
143 Make_Attribute_Reference (Sloc,
145 Attribute_Name => Attribute_Name);
148 pragma Assert (Attribute_Name = Name_Address
150 Attribute_Name = Name_Unrestricted_Access);
151 Set_Must_Be_Byte_Aligned (N, True);
153 end Make_Byte_Aligned_Attribute_Reference;
159 function Make_DT_Access
162 Typ : Entity_Id) return Node_Id
164 Full_Type : Entity_Id := Typ;
167 if Is_Private_Type (Typ) then
168 Full_Type := Underlying_Type (Typ);
172 Unchecked_Convert_To (
174 (Etype (Node (First_Elmt (Access_Disp_Table (Full_Type)))), Loc),
175 Make_Selected_Component (Loc,
176 Prefix => New_Copy (Rec),
178 New_Reference_To (First_Tag_Component (Full_Type), Loc)));
181 --------------------------------
182 -- Make_Implicit_If_Statement --
183 --------------------------------
185 function Make_Implicit_If_Statement
188 Then_Statements : List_Id;
189 Elsif_Parts : List_Id := No_List;
190 Else_Statements : List_Id := No_List) return Node_Id
193 Check_Restriction (No_Implicit_Conditionals, Node);
195 return Make_If_Statement (Sloc (Node),
200 end Make_Implicit_If_Statement;
202 -------------------------------------
203 -- Make_Implicit_Label_Declaration --
204 -------------------------------------
206 function Make_Implicit_Label_Declaration
208 Defining_Identifier : Node_Id;
209 Label_Construct : Node_Id) return Node_Id
211 N : constant Node_Id :=
212 Make_Implicit_Label_Declaration (Loc, Defining_Identifier);
214 Set_Label_Construct (N, Label_Construct);
216 end Make_Implicit_Label_Declaration;
218 ----------------------------------
219 -- Make_Implicit_Loop_Statement --
220 ----------------------------------
222 function Make_Implicit_Loop_Statement
224 Statements : List_Id;
225 Identifier : Node_Id := Empty;
226 Iteration_Scheme : Node_Id := Empty;
227 Has_Created_Identifier : Boolean := False;
228 End_Label : Node_Id := Empty) return Node_Id
231 Check_Restriction (No_Implicit_Loops, Node);
233 if Present (Iteration_Scheme)
234 and then Present (Condition (Iteration_Scheme))
236 Check_Restriction (No_Implicit_Conditionals, Node);
239 return Make_Loop_Statement (Sloc (Node),
240 Identifier => Identifier,
241 Iteration_Scheme => Iteration_Scheme,
242 Statements => Statements,
243 Has_Created_Identifier => Has_Created_Identifier,
244 End_Label => End_Label);
245 end Make_Implicit_Loop_Statement;
247 --------------------------
248 -- Make_Integer_Literal --
249 ---------------------------
251 function Make_Integer_Literal
253 Intval : Int) return Node_Id
256 return Make_Integer_Literal (Loc, UI_From_Int (Intval));
257 end Make_Integer_Literal;
259 --------------------------------
260 -- Make_Linker_Section_Pragma --
261 --------------------------------
263 function Make_Linker_Section_Pragma
266 Sec : String) return Node_Id
276 (Make_Pragma_Argument_Association
278 Expression => New_Occurrence_Of (Ent, Loc)),
279 Make_Pragma_Argument_Association
286 Set_Has_Gigi_Rep_Item (Ent);
288 end Make_Linker_Section_Pragma;
290 ---------------------------------
291 -- Make_Raise_Constraint_Error --
292 ---------------------------------
294 function Make_Raise_Constraint_Error
296 Condition : Node_Id := Empty;
297 Reason : RT_Exception_Code) return Node_Id
300 pragma Assert (Reason in RT_CE_Exceptions);
302 Make_Raise_Constraint_Error (Sloc,
303 Condition => Condition,
305 UI_From_Int (RT_Exception_Code'Pos (Reason)));
306 end Make_Raise_Constraint_Error;
308 ------------------------------
309 -- Make_Raise_Program_Error --
310 ------------------------------
312 function Make_Raise_Program_Error
314 Condition : Node_Id := Empty;
315 Reason : RT_Exception_Code) return Node_Id
318 pragma Assert (Reason in RT_PE_Exceptions);
320 Make_Raise_Program_Error (Sloc,
321 Condition => Condition,
323 UI_From_Int (RT_Exception_Code'Pos (Reason)));
324 end Make_Raise_Program_Error;
326 ------------------------------
327 -- Make_Raise_Storage_Error --
328 ------------------------------
330 function Make_Raise_Storage_Error
332 Condition : Node_Id := Empty;
333 Reason : RT_Exception_Code) return Node_Id
336 pragma Assert (Reason in RT_SE_Exceptions);
338 Make_Raise_Storage_Error (Sloc,
339 Condition => Condition,
341 UI_From_Int (RT_Exception_Code'Pos (Reason)));
342 end Make_Raise_Storage_Error;
344 -------------------------
345 -- Make_String_Literal --
346 -------------------------
348 function Make_String_Literal
350 Strval : String) return Node_Id
354 Store_String_Chars (Strval);
356 Make_String_Literal (Sloc,
357 Strval => End_String);
358 end Make_String_Literal;
360 ---------------------------
361 -- Make_Unsuppress_Block --
362 ---------------------------
364 -- Generates the following expansion:
367 -- pragma Suppress (<check>);
372 function Make_Unsuppress_Block
375 Stmts : List_Id) return Node_Id
379 Make_Block_Statement (Loc,
380 Declarations => New_List (
382 Chars => Name_Suppress,
383 Pragma_Argument_Associations => New_List (
384 Make_Pragma_Argument_Association (Loc,
385 Expression => Make_Identifier (Loc, Check))))),
387 Handled_Statement_Sequence =>
388 Make_Handled_Sequence_Of_Statements (Loc,
389 Statements => Stmts));
390 end Make_Unsuppress_Block;
392 --------------------------
393 -- New_Constraint_Error --
394 --------------------------
396 function New_Constraint_Error (Loc : Source_Ptr) return Node_Id is
397 Ident_Node : Node_Id;
398 Raise_Node : Node_Id;
401 Ident_Node := New_Node (N_Identifier, Loc);
402 Set_Chars (Ident_Node, Chars (Standard_Entity (S_Constraint_Error)));
403 Set_Entity (Ident_Node, Standard_Entity (S_Constraint_Error));
404 Raise_Node := New_Node (N_Raise_Statement, Loc);
405 Set_Name (Raise_Node, Ident_Node);
407 end New_Constraint_Error;
409 -----------------------
410 -- New_External_Name --
411 -----------------------
413 function New_External_Name
414 (Related_Id : Name_Id;
415 Suffix : Character := ' ';
416 Suffix_Index : Int := 0;
417 Prefix : Character := ' ') return Name_Id
420 Get_Name_String (Related_Id);
422 if Prefix /= ' ' then
423 pragma Assert (Is_OK_Internal_Letter (Prefix));
425 for J in reverse 1 .. Name_Len loop
426 Name_Buffer (J + 1) := Name_Buffer (J);
429 Name_Len := Name_Len + 1;
430 Name_Buffer (1) := Prefix;
433 if Suffix /= ' ' then
434 pragma Assert (Is_OK_Internal_Letter (Suffix));
435 Name_Len := Name_Len + 1;
436 Name_Buffer (Name_Len) := Suffix;
439 if Suffix_Index /= 0 then
440 if Suffix_Index < 0 then
441 Add_Unique_Serial_Number;
443 Add_Nat_To_Name_Buffer (Suffix_Index);
448 end New_External_Name;
450 function New_External_Name
451 (Related_Id : Name_Id;
453 Suffix_Index : Int := 0;
454 Prefix : Character := ' ') return Name_Id
457 Get_Name_String (Related_Id);
459 if Prefix /= ' ' then
460 pragma Assert (Is_OK_Internal_Letter (Prefix));
462 for J in reverse 1 .. Name_Len loop
463 Name_Buffer (J + 1) := Name_Buffer (J);
466 Name_Len := Name_Len + 1;
467 Name_Buffer (1) := Prefix;
471 Name_Buffer (Name_Len + 1 .. Name_Len + Suffix'Length) := Suffix;
472 Name_Len := Name_Len + Suffix'Length;
475 if Suffix_Index /= 0 then
476 if Suffix_Index < 0 then
477 Add_Unique_Serial_Number;
479 Add_Nat_To_Name_Buffer (Suffix_Index);
484 end New_External_Name;
486 function New_External_Name
488 Suffix_Index : Nat) return Name_Id
491 Name_Buffer (1) := Suffix;
493 Add_Nat_To_Name_Buffer (Suffix_Index);
495 end New_External_Name;
497 -----------------------
498 -- New_Internal_Name --
499 -----------------------
501 function New_Internal_Name (Id_Char : Character) return Name_Id is
503 pragma Assert (Is_OK_Internal_Letter (Id_Char));
504 Name_Buffer (1) := Id_Char;
506 Add_Unique_Serial_Number;
508 end New_Internal_Name;
510 -----------------------
511 -- New_Occurrence_Of --
512 -----------------------
514 function New_Occurrence_Of
516 Loc : Source_Ptr) return Node_Id
518 Occurrence : Node_Id;
521 Occurrence := New_Node (N_Identifier, Loc);
522 Set_Chars (Occurrence, Chars (Def_Id));
523 Set_Entity (Occurrence, Def_Id);
525 if Is_Type (Def_Id) then
526 Set_Etype (Occurrence, Def_Id);
528 Set_Etype (Occurrence, Etype (Def_Id));
532 end New_Occurrence_Of;
534 ----------------------
535 -- New_Reference_To --
536 ----------------------
538 function New_Reference_To
540 Loc : Source_Ptr) return Node_Id
542 Occurrence : Node_Id;
545 Occurrence := New_Node (N_Identifier, Loc);
546 Set_Chars (Occurrence, Chars (Def_Id));
547 Set_Entity (Occurrence, Def_Id);
549 end New_Reference_To;
551 -----------------------
552 -- New_Suffixed_Name --
553 -----------------------
555 function New_Suffixed_Name
556 (Related_Id : Name_Id;
557 Suffix : String) return Name_Id
560 Get_Name_String (Related_Id);
561 Name_Len := Name_Len + 1;
562 Name_Buffer (Name_Len) := '_';
563 Name_Buffer (Name_Len + 1 .. Name_Len + Suffix'Length) := Suffix;
564 Name_Len := Name_Len + Suffix'Length;
566 end New_Suffixed_Name;
572 function OK_Convert_To (Typ : Entity_Id; Expr : Node_Id) return Node_Id is
576 Make_Type_Conversion (Sloc (Expr),
577 Subtype_Mark => New_Occurrence_Of (Typ, Sloc (Expr)),
578 Expression => Relocate_Node (Expr));
579 Set_Conversion_OK (Result, True);
580 Set_Etype (Result, Typ);
584 --------------------------
585 -- Unchecked_Convert_To --
586 --------------------------
588 function Unchecked_Convert_To
590 Expr : Node_Id) return Node_Id
592 Loc : constant Source_Ptr := Sloc (Expr);
596 -- If the expression is already of the correct type, then nothing
597 -- to do, except for relocating the node in case this is required.
599 if Present (Etype (Expr))
600 and then (Base_Type (Etype (Expr)) = Typ
601 or else Etype (Expr) = Typ)
603 return Relocate_Node (Expr);
605 -- Cases where the inner expression is itself an unchecked conversion
606 -- to the same type, and we can thus eliminate the outer conversion.
608 elsif Nkind (Expr) = N_Unchecked_Type_Conversion
609 and then Entity (Subtype_Mark (Expr)) = Typ
611 Result := Relocate_Node (Expr);
613 elsif Nkind (Expr) = N_Null
614 and then Is_Access_Type (Typ)
616 -- No need for a conversion
618 Result := Relocate_Node (Expr);
624 Make_Unchecked_Type_Conversion (Loc,
625 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
626 Expression => Relocate_Node (Expr));
629 Set_Etype (Result, Typ);
631 end Unchecked_Convert_To;