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 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 pragma Style_Checks (All_Checks);
35 -- Subprogram ordering not enforced in this unit
36 -- (because of some logical groupings).
38 with Atree; use Atree;
39 with Einfo; use Einfo;
40 with Fname; use Fname;
41 with Namet; use Namet;
42 with Output; use Output;
43 with Sinfo; use Sinfo;
44 with Sinput; use Sinput;
45 with Stand; use Stand;
46 with Stringt; use Stringt;
47 with Tree_IO; use Tree_IO;
48 with Uname; use Uname;
52 Switch_Storing_Enabled : Boolean := True;
53 -- Set to False by Disable_Switch_Storing
55 -----------------------
56 -- Local Subprograms --
57 -----------------------
60 Yes_Before, -- S1 is in same extended unit as S2 and appears before it
61 Yes_Same, -- S1 is in same extended unit as S2, Slocs are the same
62 Yes_After, -- S1 is in same extended unit as S2, and appears after it
63 No); -- S2 is not in same extended unit as S2
65 function Check_Same_Extended_Unit (S1, S2 : Source_Ptr) return SEU_Result;
66 -- Used by In_Same_Extended_Unit and Earlier_In_Extended_Unit. Returns
67 -- value as described above.
69 --------------------------------------------
70 -- Access Functions for Unit Table Fields --
71 --------------------------------------------
73 function Cunit (U : Unit_Number_Type) return Node_Id is
75 return Units.Table (U).Cunit;
78 function Cunit_Entity (U : Unit_Number_Type) return Entity_Id is
80 return Units.Table (U).Cunit_Entity;
83 function Dependency_Num (U : Unit_Number_Type) return Nat is
85 return Units.Table (U).Dependency_Num;
88 function Dynamic_Elab (U : Unit_Number_Type) return Boolean is
90 return Units.Table (U).Dynamic_Elab;
93 function Error_Location (U : Unit_Number_Type) return Source_Ptr is
95 return Units.Table (U).Error_Location;
98 function Expected_Unit (U : Unit_Number_Type) return Unit_Name_Type is
100 return Units.Table (U).Expected_Unit;
103 function Fatal_Error (U : Unit_Number_Type) return Boolean is
105 return Units.Table (U).Fatal_Error;
108 function Generate_Code (U : Unit_Number_Type) return Boolean is
110 return Units.Table (U).Generate_Code;
113 function Has_RACW (U : Unit_Number_Type) return Boolean is
115 return Units.Table (U).Has_RACW;
118 function Ident_String (U : Unit_Number_Type) return Node_Id is
120 return Units.Table (U).Ident_String;
123 function Loading (U : Unit_Number_Type) return Boolean is
125 return Units.Table (U).Loading;
128 function Main_Priority (U : Unit_Number_Type) return Int is
130 return Units.Table (U).Main_Priority;
133 function Munit_Index (U : Unit_Number_Type) return Nat is
135 return Units.Table (U).Munit_Index;
138 function Source_Index (U : Unit_Number_Type) return Source_File_Index is
140 return Units.Table (U).Source_Index;
143 function Unit_File_Name (U : Unit_Number_Type) return File_Name_Type is
145 return Units.Table (U).Unit_File_Name;
148 function Unit_Name (U : Unit_Number_Type) return Unit_Name_Type is
150 return Units.Table (U).Unit_Name;
153 ------------------------------------------
154 -- Subprograms to Set Unit Table Fields --
155 ------------------------------------------
157 procedure Set_Cunit (U : Unit_Number_Type; N : Node_Id) is
159 Units.Table (U).Cunit := N;
162 procedure Set_Cunit_Entity (U : Unit_Number_Type; E : Entity_Id) is
164 Units.Table (U).Cunit_Entity := E;
165 Set_Is_Compilation_Unit (E);
166 end Set_Cunit_Entity;
168 procedure Set_Dynamic_Elab (U : Unit_Number_Type; B : Boolean := True) is
170 Units.Table (U).Dynamic_Elab := B;
171 end Set_Dynamic_Elab;
173 procedure Set_Error_Location (U : Unit_Number_Type; W : Source_Ptr) is
175 Units.Table (U).Error_Location := W;
176 end Set_Error_Location;
178 procedure Set_Fatal_Error (U : Unit_Number_Type; B : Boolean := True) is
180 Units.Table (U).Fatal_Error := B;
183 procedure Set_Generate_Code (U : Unit_Number_Type; B : Boolean := True) is
185 Units.Table (U).Generate_Code := B;
186 end Set_Generate_Code;
188 procedure Set_Has_RACW (U : Unit_Number_Type; B : Boolean := True) is
190 Units.Table (U).Has_RACW := B;
193 procedure Set_Ident_String (U : Unit_Number_Type; N : Node_Id) is
195 Units.Table (U).Ident_String := N;
196 end Set_Ident_String;
198 procedure Set_Loading (U : Unit_Number_Type; B : Boolean := True) is
200 Units.Table (U).Loading := B;
203 procedure Set_Main_Priority (U : Unit_Number_Type; P : Int) is
205 Units.Table (U).Main_Priority := P;
206 end Set_Main_Priority;
208 procedure Set_Unit_Name (U : Unit_Number_Type; N : Unit_Name_Type) is
210 Units.Table (U).Unit_Name := N;
213 ------------------------------
214 -- Check_Same_Extended_Unit --
215 ------------------------------
217 function Check_Same_Extended_Unit (S1, S2 : Source_Ptr) return SEU_Result is
220 Sind1 : Source_File_Index;
221 Sind2 : Source_File_Index;
224 Unum1 : Unit_Number_Type;
225 Unum2 : Unit_Number_Type;
232 if S1 = No_Location or else S2 = No_Location then
235 elsif S1 = Standard_Location then
236 if S2 = Standard_Location then
242 elsif S2 = Standard_Location then
248 Unum1 := Get_Code_Unit (Sloc1);
249 Unum2 := Get_Code_Unit (Sloc2);
252 Sind1 := Get_Source_File_Index (Sloc1);
253 Sind2 := Get_Source_File_Index (Sloc2);
255 if Sind1 = Sind2 then
256 if Sloc1 < Sloc2 then
258 elsif Sloc1 > Sloc2 then
265 -- OK, the two nodes are in separate source elements, but this is not
266 -- decisive, because of the issue of subunits and instantiations.
268 -- First we deal with subunits, since if the subunit is in an
269 -- instantiation, we know that the parent is in the corresponding
270 -- instantiation, since that is the only way we can have a subunit
271 -- that is part of an instantiation.
273 Unit1 := Unit (Cunit (Unum1));
274 Unit2 := Unit (Cunit (Unum2));
276 if Nkind (Unit1) = N_Subunit
277 and then Present (Corresponding_Stub (Unit1))
279 -- Both in subunits. They could have a common ancestor. If they
280 -- do, then the deeper one must have a longer unit name. Replace
281 -- the deeper one with its corresponding stub, in order to find
282 -- nearest common ancestor, if any.
284 if Nkind (Unit2) = N_Subunit
285 and then Present (Corresponding_Stub (Unit2))
287 if Length_Of_Name (Unit_Name (Unum1)) <
288 Length_Of_Name (Unit_Name (Unum2))
290 Sloc2 := Sloc (Corresponding_Stub (Unit2));
291 Unum2 := Get_Source_Unit (Sloc2);
295 Sloc1 := Sloc (Corresponding_Stub (Unit1));
296 Unum1 := Get_Source_Unit (Sloc1);
300 -- Nod1 in subunit, Nod2 not
303 Sloc1 := Sloc (Corresponding_Stub (Unit1));
304 Unum1 := Get_Source_Unit (Sloc1);
308 -- Nod2 in subunit, Nod1 not
310 elsif Nkind (Unit2) = N_Subunit
311 and then Present (Corresponding_Stub (Unit2))
313 Sloc2 := Sloc (Corresponding_Stub (Unit2));
314 Unum2 := Get_Source_Unit (Sloc2);
318 -- At this stage we know that neither is a subunit, so we deal
319 -- with instantiations, since we culd have a common ancestor
321 Inst1 := Instantiation (Sind1);
322 Inst2 := Instantiation (Sind2);
324 if Inst1 /= No_Location then
326 -- Both are instantiations
328 if Inst2 /= No_Location then
330 Depth1 := Instantiation_Depth (Sloc1);
331 Depth2 := Instantiation_Depth (Sloc2);
333 if Depth1 < Depth2 then
335 Unum2 := Get_Source_Unit (Sloc2);
338 elsif Depth1 > Depth2 then
340 Unum1 := Get_Source_Unit (Sloc1);
346 Unum1 := Get_Source_Unit (Sloc1);
347 Unum2 := Get_Source_Unit (Sloc2);
351 -- Only first node is in instantiation
355 Unum1 := Get_Source_Unit (Sloc1);
359 -- Only second node is instantiation
361 elsif Inst2 /= No_Location then
363 Unum2 := Get_Source_Unit (Sloc2);
367 -- No instantiations involved, so we are not in the same unit
368 -- However, there is one case still to check, namely the case
369 -- where one location is in the spec, and the other in the
370 -- corresponding body (the spec location is earlier).
372 if Nkind (Unit1) = N_Subprogram_Body
374 Nkind (Unit1) = N_Package_Body
376 if Library_Unit (Cunit (Unum1)) = Cunit (Unum2) then
380 elsif Nkind (Unit2) = N_Subprogram_Body
382 Nkind (Unit2) = N_Package_Body
384 if Library_Unit (Cunit (Unum2)) = Cunit (Unum1) then
389 -- If that special case does not occur, then we are certain that
390 -- the two locations are really in separate units.
397 end Check_Same_Extended_Unit;
399 -------------------------------
400 -- Compilation_Switches_Last --
401 -------------------------------
403 function Compilation_Switches_Last return Nat is
405 return Compilation_Switches.Last;
406 end Compilation_Switches_Last;
408 procedure Disable_Switch_Storing is
410 Switch_Storing_Enabled := False;
411 end Disable_Switch_Storing;
413 ------------------------------
414 -- Earlier_In_Extended_Unit --
415 ------------------------------
417 function Earlier_In_Extended_Unit (S1, S2 : Source_Ptr) return Boolean is
419 return Check_Same_Extended_Unit (S1, S2) = Yes_Before;
420 end Earlier_In_Extended_Unit;
422 ----------------------------
423 -- Entity_Is_In_Main_Unit --
424 ----------------------------
426 function Entity_Is_In_Main_Unit (E : Entity_Id) return Boolean is
432 while S /= Standard_Standard loop
433 if S = Main_Unit_Entity then
435 elsif Ekind (S) = E_Package and then Is_Child_Unit (S) then
443 end Entity_Is_In_Main_Unit;
445 ---------------------------------
446 -- Generic_Separately_Compiled --
447 ---------------------------------
449 function Generic_Separately_Compiled (E : Entity_Id) return Boolean is
451 -- We do not generate object files for internal generics, because
452 -- the only thing they would contain is the elaboration boolean, and
453 -- we are careful to elaborate all predefined units first anyway, so
454 -- this boolean is not needed.
456 if Is_Internal_File_Name
457 (Fname => Unit_File_Name (Get_Source_Unit (E)),
458 Renamings_Included => True)
462 -- All other generic units do generate object files
467 end Generic_Separately_Compiled;
469 function Generic_Separately_Compiled
470 (Sfile : File_Name_Type) return Boolean
473 -- Exactly the same as previous function, but works directly on a file
476 if Is_Internal_File_Name
478 Renamings_Included => True)
482 -- All other generic units do generate object files
487 end Generic_Separately_Compiled;
493 function Get_Code_Unit (S : Source_Ptr) return Unit_Number_Type is
495 -- Search table unless we have No_Location, which can happen if the
496 -- relevant location has not been set yet. Happens for example when
497 -- we obtain Sloc (Cunit (Main_Unit)) before it is set.
499 if S /= No_Location then
501 Source_File : constant Source_File_Index :=
502 Get_Source_File_Index (Top_Level_Location (S));
505 for U in Units.First .. Units.Last loop
506 if Source_Index (U) = Source_File then
513 -- If S was No_Location, or was not in the table, we must be in the
514 -- main source unit (and the value has not been placed in the table yet)
519 function Get_Code_Unit (N : Node_Or_Entity_Id) return Unit_Number_Type is
521 return Get_Code_Unit (Sloc (N));
524 ----------------------------
525 -- Get_Compilation_Switch --
526 ----------------------------
528 function Get_Compilation_Switch (N : Pos) return String_Ptr is
530 if N <= Compilation_Switches.Last then
531 return Compilation_Switches.Table (N);
536 end Get_Compilation_Switch;
538 ----------------------------------
539 -- Get_Cunit_Entity_Unit_Number --
540 ----------------------------------
542 function Get_Cunit_Entity_Unit_Number
543 (E : Entity_Id) return Unit_Number_Type
546 for U in Units.First .. Units.Last loop
547 if Cunit_Entity (U) = E then
552 -- If not in the table, must be the main source unit, and we just
553 -- have not got it put into the table yet.
556 end Get_Cunit_Entity_Unit_Number;
558 ---------------------------
559 -- Get_Cunit_Unit_Number --
560 ---------------------------
562 function Get_Cunit_Unit_Number (N : Node_Id) return Unit_Number_Type is
564 for U in Units.First .. Units.Last loop
565 if Cunit (U) = N then
570 -- If not in the table, must be the main source unit, and we just
571 -- have not got it put into the table yet.
574 end Get_Cunit_Unit_Number;
576 ---------------------
577 -- Get_Source_Unit --
578 ---------------------
580 function Get_Source_Unit (S : Source_Ptr) return Unit_Number_Type is
582 -- Search table unless we have No_Location, which can happen if the
583 -- relevant location has not been set yet. Happens for example when
584 -- we obtain Sloc (Cunit (Main_Unit)) before it is set.
586 if S /= No_Location then
588 Source_File : Source_File_Index :=
589 Get_Source_File_Index (Top_Level_Location (S));
592 Source_File := Get_Source_File_Index (S);
593 while Template (Source_File) /= No_Source_File loop
594 Source_File := Template (Source_File);
597 for U in Units.First .. Units.Last loop
598 if Source_Index (U) = Source_File then
605 -- If S was No_Location, or was not in the table, we must be in the
606 -- main source unit (and the value has not got put into the table yet)
611 function Get_Source_Unit (N : Node_Or_Entity_Id) return Unit_Number_Type is
613 return Get_Source_Unit (Sloc (N));
616 --------------------------------
617 -- In_Extended_Main_Code_Unit --
618 --------------------------------
620 function In_Extended_Main_Code_Unit
621 (N : Node_Or_Entity_Id) return Boolean
624 if Sloc (N) = Standard_Location then
627 elsif Sloc (N) = No_Location then
630 -- Special case Itypes to test the Sloc of the associated node. The
631 -- reason we do this is for possible calls from gigi after -gnatD
632 -- processing is complete in sprint. This processing updates the
633 -- sloc fields of all nodes in the tree, but itypes are not in the
634 -- tree so their slocs do not get updated.
636 elsif Nkind (N) = N_Defining_Identifier
637 and then Is_Itype (N)
639 return In_Extended_Main_Code_Unit (Associated_Node_For_Itype (N));
641 -- Otherwise see if we are in the main unit
643 elsif Get_Code_Unit (Sloc (N)) = Get_Code_Unit (Cunit (Main_Unit)) then
646 -- Node may be in spec (or subunit etc) of main unit
650 In_Same_Extended_Unit (N, Cunit (Main_Unit));
652 end In_Extended_Main_Code_Unit;
654 function In_Extended_Main_Code_Unit (Loc : Source_Ptr) return Boolean is
656 if Loc = Standard_Location then
659 elsif Loc = No_Location then
662 -- Otherwise see if we are in the main unit
664 elsif Get_Code_Unit (Loc) = Get_Code_Unit (Cunit (Main_Unit)) then
667 -- Location may be in spec (or subunit etc) of main unit
671 In_Same_Extended_Unit (Loc, Sloc (Cunit (Main_Unit)));
673 end In_Extended_Main_Code_Unit;
675 ----------------------------------
676 -- In_Extended_Main_Source_Unit --
677 ----------------------------------
679 function In_Extended_Main_Source_Unit
680 (N : Node_Or_Entity_Id) return Boolean
682 Nloc : constant Source_Ptr := Sloc (N);
683 Mloc : constant Source_Ptr := Sloc (Cunit (Main_Unit));
686 -- If Mloc is not set, it means we are still parsing the main unit,
687 -- so everything so far is in the extended main source unit.
689 if Mloc = No_Location then
692 -- Special value cases
694 elsif Nloc = Standard_Location then
697 elsif Nloc = No_Location then
700 -- Special case Itypes to test the Sloc of the associated node. The
701 -- reason we do this is for possible calls from gigi after -gnatD
702 -- processing is complete in sprint. This processing updates the
703 -- sloc fields of all nodes in the tree, but itypes are not in the
704 -- tree so their slocs do not get updated.
706 elsif Nkind (N) = N_Defining_Identifier
707 and then Is_Itype (N)
709 return In_Extended_Main_Source_Unit (Associated_Node_For_Itype (N));
711 -- Otherwise compare original locations to see if in same unit
715 In_Same_Extended_Unit
716 (Original_Location (Nloc), Original_Location (Mloc));
718 end In_Extended_Main_Source_Unit;
720 function In_Extended_Main_Source_Unit
721 (Loc : Source_Ptr) return Boolean
723 Mloc : constant Source_Ptr := Sloc (Cunit (Main_Unit));
726 -- If Mloc is not set, it means we are still parsing the main unit,
727 -- so everything so far is in the extended main source unit.
729 if Mloc = No_Location then
732 -- Special value cases
734 elsif Loc = Standard_Location then
737 elsif Loc = No_Location then
740 -- Otherwise compare original locations to see if in same unit
744 In_Same_Extended_Unit
745 (Original_Location (Loc), Original_Location (Mloc));
747 end In_Extended_Main_Source_Unit;
749 -----------------------
750 -- In_Same_Code_Unit --
751 -----------------------
753 function In_Same_Code_Unit (N1, N2 : Node_Or_Entity_Id) return Boolean is
754 S1 : constant Source_Ptr := Sloc (N1);
755 S2 : constant Source_Ptr := Sloc (N2);
758 if S1 = No_Location or else S2 = No_Location then
761 elsif S1 = Standard_Location then
762 return S2 = Standard_Location;
764 elsif S2 = Standard_Location then
768 return Get_Code_Unit (N1) = Get_Code_Unit (N2);
769 end In_Same_Code_Unit;
771 ---------------------------
772 -- In_Same_Extended_Unit --
773 ---------------------------
775 function In_Same_Extended_Unit
776 (N1, N2 : Node_Or_Entity_Id) return Boolean
779 return Check_Same_Extended_Unit (Sloc (N1), Sloc (N2)) /= No;
780 end In_Same_Extended_Unit;
782 function In_Same_Extended_Unit (S1, S2 : Source_Ptr) return Boolean is
784 return Check_Same_Extended_Unit (S1, S2) /= No;
785 end In_Same_Extended_Unit;
787 -------------------------
788 -- In_Same_Source_Unit --
789 -------------------------
791 function In_Same_Source_Unit (N1, N2 : Node_Or_Entity_Id) return Boolean is
792 S1 : constant Source_Ptr := Sloc (N1);
793 S2 : constant Source_Ptr := Sloc (N2);
796 if S1 = No_Location or else S2 = No_Location then
799 elsif S1 = Standard_Location then
800 return S2 = Standard_Location;
802 elsif S2 = Standard_Location then
806 return Get_Source_Unit (N1) = Get_Source_Unit (N2);
807 end In_Same_Source_Unit;
809 -----------------------------
810 -- Increment_Serial_Number --
811 -----------------------------
813 function Increment_Serial_Number return Nat is
814 TSN : Int renames Units.Table (Current_Sem_Unit).Serial_Number;
818 end Increment_Serial_Number;
824 procedure Initialize is
826 Linker_Option_Lines.Init;
829 Compilation_Switches.Init;
836 function Is_Loaded (Uname : Unit_Name_Type) return Boolean is
838 for Unum in Units.First .. Units.Last loop
839 if Uname = Unit_Name (Unum) then
851 function Last_Unit return Unit_Number_Type is
860 procedure List (File_Names_Only : Boolean := False) is separate;
868 Linker_Option_Lines.Locked := True;
869 Load_Stack.Locked := True;
870 Units.Locked := True;
871 Linker_Option_Lines.Release;
880 function Num_Units return Nat is
882 return Int (Units.Last) - Int (Main_Unit) + 1;
889 procedure Remove_Unit (U : Unit_Number_Type) is
891 if U = Units.Last then
892 Units.Decrement_Last;
896 ----------------------------------
897 -- Replace_Linker_Option_String --
898 ----------------------------------
900 procedure Replace_Linker_Option_String
901 (S : String_Id; Match_String : String)
904 if Match_String'Length > 0 then
905 for J in 1 .. Linker_Option_Lines.Last loop
906 String_To_Name_Buffer (Linker_Option_Lines.Table (J).Option);
908 if Match_String = Name_Buffer (1 .. Match_String'Length) then
909 Linker_Option_Lines.Table (J).Option := S;
915 Store_Linker_Option_String (S);
916 end Replace_Linker_Option_String;
922 procedure Sort (Tbl : in out Unit_Ref_Table) is separate;
924 ------------------------------
925 -- Store_Compilation_Switch --
926 ------------------------------
928 procedure Store_Compilation_Switch (Switch : String) is
930 if Switch_Storing_Enabled then
931 Compilation_Switches.Increment_Last;
932 Compilation_Switches.Table (Compilation_Switches.Last) :=
935 -- Fix up --RTS flag which has been transformed by the gcc driver
938 if Switch'Last >= Switch'First + 4
939 and then Switch (Switch'First .. Switch'First + 4) = "-fRTS"
941 Compilation_Switches.Table
942 (Compilation_Switches.Last) (Switch'First + 1) := '-';
945 end Store_Compilation_Switch;
947 --------------------------------
948 -- Store_Linker_Option_String --
949 --------------------------------
951 procedure Store_Linker_Option_String (S : String_Id) is
953 Linker_Option_Lines.Increment_Last;
954 Linker_Option_Lines.Table (Linker_Option_Lines.Last) :=
955 (Option => S, Unit => Current_Sem_Unit);
956 end Store_Linker_Option_String;
958 -------------------------------
959 -- Synchronize_Serial_Number --
960 -------------------------------
962 procedure Synchronize_Serial_Number is
963 TSN : Int renames Units.Table (Current_Sem_Unit).Serial_Number;
966 end Synchronize_Serial_Number;
972 procedure Tree_Read is
979 -- Read Compilation_Switches table
982 Compilation_Switches.Set_Last (N);
986 Compilation_Switches.Table (J) := S;
994 procedure Tree_Write is
998 -- Write Compilation_Switches table
1000 Tree_Write_Int (Compilation_Switches.Last);
1002 for J in 1 .. Compilation_Switches.Last loop
1003 Tree_Write_Str (Compilation_Switches.Table (J));
1011 function Version_Get (U : Unit_Number_Type) return Word_Hex_String is
1013 return Get_Hex_String (Units.Table (U).Version);
1016 ------------------------
1017 -- Version_Referenced --
1018 ------------------------
1020 procedure Version_Referenced (S : String_Id) is
1022 Version_Ref.Append (S);
1023 end Version_Referenced;