1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2008, 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 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 Config_Serial_Number : Nat := 0;
57 -- Counter for use in config pragmas, see comment below
59 procedure Add_Unique_Serial_Number is
61 -- If we are analyzing configuration pragmas, Cunit (Main_Unit) will
62 -- not be set yet. This happens for example when analyzing static
63 -- string expressions in configuration pragmas. For this case, we
64 -- just maintain a local counter, defined above and we do not need
65 -- to add a b or s indication in this case.
67 if No (Cunit (Current_Sem_Unit)) then
68 Config_Serial_Number := Config_Serial_Number + 1;
69 Add_Nat_To_Name_Buffer (Config_Serial_Number);
72 -- Normal case, within a unit
76 Unit_Node : constant Node_Id := Unit (Cunit (Current_Sem_Unit));
79 Add_Nat_To_Name_Buffer (Increment_Serial_Number);
81 -- Add either b or s, depending on whether current unit is a spec
82 -- or a body. This is needed because we may generate the same name
83 -- in a spec and a body otherwise.
85 Name_Len := Name_Len + 1;
87 if Nkind (Unit_Node) = N_Package_Declaration
88 or else Nkind (Unit_Node) = N_Subprogram_Declaration
89 or else Nkind (Unit_Node) in N_Generic_Declaration
91 Name_Buffer (Name_Len) := 's';
93 Name_Buffer (Name_Len) := 'b';
97 end Add_Unique_Serial_Number;
103 function Checks_Off (N : Node_Id) return Node_Id is
106 Make_Unchecked_Expression (Sloc (N),
114 function Convert_To (Typ : Entity_Id; Expr : Node_Id) return Node_Id is
118 if Present (Etype (Expr))
119 and then (Etype (Expr)) = Typ
121 return Relocate_Node (Expr);
124 Make_Type_Conversion (Sloc (Expr),
125 Subtype_Mark => New_Occurrence_Of (Typ, Sloc (Expr)),
126 Expression => Relocate_Node (Expr));
128 Set_Etype (Result, Typ);
137 procedure Discard_List (L : List_Id) is
138 pragma Warnings (Off, L);
147 procedure Discard_Node (N : Node_Or_Entity_Id) is
148 pragma Warnings (Off, N);
153 -------------------------------------------
154 -- Make_Byte_Aligned_Attribute_Reference --
155 -------------------------------------------
157 function Make_Byte_Aligned_Attribute_Reference
160 Attribute_Name : Name_Id)
163 N : constant Node_Id :=
164 Make_Attribute_Reference (Sloc,
166 Attribute_Name => Attribute_Name);
169 pragma Assert (Attribute_Name = Name_Address
171 Attribute_Name = Name_Unrestricted_Access);
172 Set_Must_Be_Byte_Aligned (N, True);
174 end Make_Byte_Aligned_Attribute_Reference;
180 function Make_DT_Access
183 Typ : Entity_Id) return Node_Id
185 Full_Type : Entity_Id := Typ;
188 if Is_Private_Type (Typ) then
189 Full_Type := Underlying_Type (Typ);
193 Unchecked_Convert_To (
195 (Etype (Node (First_Elmt (Access_Disp_Table (Full_Type)))), Loc),
196 Make_Selected_Component (Loc,
197 Prefix => New_Copy (Rec),
199 New_Reference_To (First_Tag_Component (Full_Type), Loc)));
202 -------------------------------------
203 -- Make_Implicit_Exception_Handler --
204 -------------------------------------
206 function Make_Implicit_Exception_Handler
208 Choice_Parameter : Node_Id := Empty;
209 Exception_Choices : List_Id;
210 Statements : List_Id) return Node_Id
216 -- Set the source location only when debugging the expanded code
218 -- When debugging the source code directly, we do not want the compiler
219 -- to associate this implicit exception handler with any specific source
220 -- line, because it can potentially confuse the debugger. The most
221 -- damaging situation would arise when the debugger tries to insert a
222 -- breakpoint at a certain line. If the code of the associated implicit
223 -- exception handler is generated before the code of that line, then the
224 -- debugger will end up inserting the breakpoint inside the exception
225 -- handler, rather than the code the user intended to break on. As a
226 -- result, it is likely that the program will not hit the breakpoint
229 if Debug_Generated_Code then
236 Make_Exception_Handler
237 (Loc, Choice_Parameter, Exception_Choices, Statements);
238 Set_Local_Raise_Statements (Handler, No_Elist);
240 end Make_Implicit_Exception_Handler;
242 --------------------------------
243 -- Make_Implicit_If_Statement --
244 --------------------------------
246 function Make_Implicit_If_Statement
249 Then_Statements : List_Id;
250 Elsif_Parts : List_Id := No_List;
251 Else_Statements : List_Id := No_List) return Node_Id
254 Check_Restriction (No_Implicit_Conditionals, Node);
256 return Make_If_Statement (Sloc (Node),
261 end Make_Implicit_If_Statement;
263 -------------------------------------
264 -- Make_Implicit_Label_Declaration --
265 -------------------------------------
267 function Make_Implicit_Label_Declaration
269 Defining_Identifier : Node_Id;
270 Label_Construct : Node_Id) return Node_Id
272 N : constant Node_Id :=
273 Make_Implicit_Label_Declaration (Loc, Defining_Identifier);
275 Set_Label_Construct (N, Label_Construct);
277 end Make_Implicit_Label_Declaration;
279 ----------------------------------
280 -- Make_Implicit_Loop_Statement --
281 ----------------------------------
283 function Make_Implicit_Loop_Statement
285 Statements : List_Id;
286 Identifier : Node_Id := Empty;
287 Iteration_Scheme : Node_Id := Empty;
288 Has_Created_Identifier : Boolean := False;
289 End_Label : Node_Id := Empty) return Node_Id
292 Check_Restriction (No_Implicit_Loops, Node);
294 if Present (Iteration_Scheme)
295 and then Present (Condition (Iteration_Scheme))
297 Check_Restriction (No_Implicit_Conditionals, Node);
300 return Make_Loop_Statement (Sloc (Node),
301 Identifier => Identifier,
302 Iteration_Scheme => Iteration_Scheme,
303 Statements => Statements,
304 Has_Created_Identifier => Has_Created_Identifier,
305 End_Label => End_Label);
306 end Make_Implicit_Loop_Statement;
308 --------------------------
309 -- Make_Integer_Literal --
310 ---------------------------
312 function Make_Integer_Literal
314 Intval : Int) return Node_Id
317 return Make_Integer_Literal (Loc, UI_From_Int (Intval));
318 end Make_Integer_Literal;
320 --------------------------------
321 -- Make_Linker_Section_Pragma --
322 --------------------------------
324 function Make_Linker_Section_Pragma
327 Sec : String) return Node_Id
337 (Make_Pragma_Argument_Association
339 Expression => New_Occurrence_Of (Ent, Loc)),
340 Make_Pragma_Argument_Association
347 Set_Has_Gigi_Rep_Item (Ent);
349 end Make_Linker_Section_Pragma;
358 Pragma_Argument_Associations : List_Id := No_List;
359 Debug_Statement : Node_Id := Empty) return Node_Id
364 Pragma_Argument_Associations => Pragma_Argument_Associations,
365 Debug_Statement => Debug_Statement,
366 Pragma_Identifier => Make_Identifier (Sloc, Chars));
369 ---------------------------------
370 -- Make_Raise_Constraint_Error --
371 ---------------------------------
373 function Make_Raise_Constraint_Error
375 Condition : Node_Id := Empty;
376 Reason : RT_Exception_Code) return Node_Id
379 pragma Assert (Reason in RT_CE_Exceptions);
381 Make_Raise_Constraint_Error (Sloc,
382 Condition => Condition,
384 UI_From_Int (RT_Exception_Code'Pos (Reason)));
385 end Make_Raise_Constraint_Error;
387 ------------------------------
388 -- Make_Raise_Program_Error --
389 ------------------------------
391 function Make_Raise_Program_Error
393 Condition : Node_Id := Empty;
394 Reason : RT_Exception_Code) return Node_Id
397 pragma Assert (Reason in RT_PE_Exceptions);
399 Make_Raise_Program_Error (Sloc,
400 Condition => Condition,
402 UI_From_Int (RT_Exception_Code'Pos (Reason)));
403 end Make_Raise_Program_Error;
405 ------------------------------
406 -- Make_Raise_Storage_Error --
407 ------------------------------
409 function Make_Raise_Storage_Error
411 Condition : Node_Id := Empty;
412 Reason : RT_Exception_Code) return Node_Id
415 pragma Assert (Reason in RT_SE_Exceptions);
417 Make_Raise_Storage_Error (Sloc,
418 Condition => Condition,
420 UI_From_Int (RT_Exception_Code'Pos (Reason)));
421 end Make_Raise_Storage_Error;
423 -------------------------
424 -- Make_String_Literal --
425 -------------------------
427 function Make_String_Literal
429 Strval : String) return Node_Id
433 Store_String_Chars (Strval);
435 Make_String_Literal (Sloc,
436 Strval => End_String);
437 end Make_String_Literal;
439 ---------------------------
440 -- Make_Unsuppress_Block --
441 ---------------------------
443 -- Generates the following expansion:
446 -- pragma Suppress (<check>);
451 function Make_Unsuppress_Block
454 Stmts : List_Id) return Node_Id
458 Make_Block_Statement (Loc,
459 Declarations => New_List (
461 Chars => Name_Suppress,
462 Pragma_Argument_Associations => New_List (
463 Make_Pragma_Argument_Association (Loc,
464 Expression => Make_Identifier (Loc, Check))))),
466 Handled_Statement_Sequence =>
467 Make_Handled_Sequence_Of_Statements (Loc,
468 Statements => Stmts));
469 end Make_Unsuppress_Block;
471 --------------------------
472 -- New_Constraint_Error --
473 --------------------------
475 function New_Constraint_Error (Loc : Source_Ptr) return Node_Id is
476 Ident_Node : Node_Id;
477 Raise_Node : Node_Id;
480 Ident_Node := New_Node (N_Identifier, Loc);
481 Set_Chars (Ident_Node, Chars (Standard_Entity (S_Constraint_Error)));
482 Set_Entity (Ident_Node, Standard_Entity (S_Constraint_Error));
483 Raise_Node := New_Node (N_Raise_Statement, Loc);
484 Set_Name (Raise_Node, Ident_Node);
486 end New_Constraint_Error;
488 -----------------------
489 -- New_External_Name --
490 -----------------------
492 function New_External_Name
493 (Related_Id : Name_Id;
494 Suffix : Character := ' ';
495 Suffix_Index : Int := 0;
496 Prefix : Character := ' ') return Name_Id
499 Get_Name_String (Related_Id);
501 if Prefix /= ' ' then
502 pragma Assert (Is_OK_Internal_Letter (Prefix) or else Prefix = '_');
504 for J in reverse 1 .. Name_Len loop
505 Name_Buffer (J + 1) := Name_Buffer (J);
508 Name_Len := Name_Len + 1;
509 Name_Buffer (1) := Prefix;
512 if Suffix /= ' ' then
513 pragma Assert (Is_OK_Internal_Letter (Suffix));
514 Add_Char_To_Name_Buffer (Suffix);
517 if Suffix_Index /= 0 then
518 if Suffix_Index < 0 then
519 Add_Unique_Serial_Number;
521 Add_Nat_To_Name_Buffer (Suffix_Index);
526 end New_External_Name;
528 function New_External_Name
529 (Related_Id : Name_Id;
531 Suffix_Index : Int := 0;
532 Prefix : Character := ' ') return Name_Id
535 Get_Name_String (Related_Id);
537 if Prefix /= ' ' then
538 pragma Assert (Is_OK_Internal_Letter (Prefix));
540 for J in reverse 1 .. Name_Len loop
541 Name_Buffer (J + 1) := Name_Buffer (J);
544 Name_Len := Name_Len + 1;
545 Name_Buffer (1) := Prefix;
549 Name_Buffer (Name_Len + 1 .. Name_Len + Suffix'Length) := Suffix;
550 Name_Len := Name_Len + Suffix'Length;
553 if Suffix_Index /= 0 then
554 if Suffix_Index < 0 then
555 Add_Unique_Serial_Number;
557 Add_Nat_To_Name_Buffer (Suffix_Index);
562 end New_External_Name;
564 function New_External_Name
566 Suffix_Index : Nat) return Name_Id
569 Name_Buffer (1) := Suffix;
571 Add_Nat_To_Name_Buffer (Suffix_Index);
573 end New_External_Name;
575 -----------------------
576 -- New_Internal_Name --
577 -----------------------
579 function New_Internal_Name (Id_Char : Character) return Name_Id is
581 pragma Assert (Is_OK_Internal_Letter (Id_Char));
582 Name_Buffer (1) := Id_Char;
584 Add_Unique_Serial_Number;
586 end New_Internal_Name;
588 -----------------------
589 -- New_Occurrence_Of --
590 -----------------------
592 function New_Occurrence_Of
594 Loc : Source_Ptr) return Node_Id
596 Occurrence : Node_Id;
599 Occurrence := New_Node (N_Identifier, Loc);
600 Set_Chars (Occurrence, Chars (Def_Id));
601 Set_Entity (Occurrence, Def_Id);
603 if Is_Type (Def_Id) then
604 Set_Etype (Occurrence, Def_Id);
606 Set_Etype (Occurrence, Etype (Def_Id));
610 end New_Occurrence_Of;
612 ----------------------
613 -- New_Reference_To --
614 ----------------------
616 function New_Reference_To
618 Loc : Source_Ptr) return Node_Id
620 Occurrence : Node_Id;
623 Occurrence := New_Node (N_Identifier, Loc);
624 Set_Chars (Occurrence, Chars (Def_Id));
625 Set_Entity (Occurrence, Def_Id);
627 end New_Reference_To;
629 -----------------------
630 -- New_Suffixed_Name --
631 -----------------------
633 function New_Suffixed_Name
634 (Related_Id : Name_Id;
635 Suffix : String) return Name_Id
638 Get_Name_String (Related_Id);
639 Add_Char_To_Name_Buffer ('_');
640 Add_Str_To_Name_Buffer (Suffix);
642 end New_Suffixed_Name;
648 function OK_Convert_To (Typ : Entity_Id; Expr : Node_Id) return Node_Id is
652 Make_Type_Conversion (Sloc (Expr),
653 Subtype_Mark => New_Occurrence_Of (Typ, Sloc (Expr)),
654 Expression => Relocate_Node (Expr));
655 Set_Conversion_OK (Result, True);
656 Set_Etype (Result, Typ);
660 --------------------------
661 -- Unchecked_Convert_To --
662 --------------------------
664 function Unchecked_Convert_To
666 Expr : Node_Id) return Node_Id
668 Loc : constant Source_Ptr := Sloc (Expr);
672 -- If the expression is already of the correct type, then nothing
673 -- to do, except for relocating the node in case this is required.
675 if Present (Etype (Expr))
676 and then (Base_Type (Etype (Expr)) = Typ
677 or else Etype (Expr) = Typ)
679 return Relocate_Node (Expr);
681 -- Cases where the inner expression is itself an unchecked conversion
682 -- to the same type, and we can thus eliminate the outer conversion.
684 elsif Nkind (Expr) = N_Unchecked_Type_Conversion
685 and then Entity (Subtype_Mark (Expr)) = Typ
687 Result := Relocate_Node (Expr);
689 elsif Nkind (Expr) = N_Null
690 and then Is_Access_Type (Typ)
692 -- No need for a conversion
694 Result := Relocate_Node (Expr);
700 Make_Unchecked_Type_Conversion (Loc,
701 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
702 Expression => Relocate_Node (Expr));
705 Set_Etype (Result, Typ);
707 end Unchecked_Convert_To;