1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2003 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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 Namet; use Namet;
43 with Output; use Output;
44 with Sinfo; use Sinfo;
45 with Sinput; use Sinput;
46 with Stand; use Stand;
47 with Stringt; use Stringt;
48 with Tree_IO; use Tree_IO;
49 with Uname; use Uname;
53 -----------------------
54 -- Local Subprograms --
55 -----------------------
58 Yes_Before, -- S1 is in same extended unit as S2 and appears before it
59 Yes_Same, -- S1 is in same extended unit as S2, Slocs are the same
60 Yes_After, -- S1 is in same extended unit as S2, and appears after it
61 No); -- S2 is not in same extended unit as S2
63 function Check_Same_Extended_Unit (S1, S2 : Source_Ptr) return SEU_Result;
64 -- Used by In_Same_Extended_Unit and Earlier_In_Extended_Unit. Returns
65 -- value as described above.
67 --------------------------------------------
68 -- Access Functions for Unit Table Fields --
69 --------------------------------------------
71 function Cunit (U : Unit_Number_Type) return Node_Id is
73 return Units.Table (U).Cunit;
76 function Cunit_Entity (U : Unit_Number_Type) return Entity_Id is
78 return Units.Table (U).Cunit_Entity;
81 function Dependency_Num (U : Unit_Number_Type) return Nat is
83 return Units.Table (U).Dependency_Num;
86 function Dependent_Unit (U : Unit_Number_Type) return Boolean is
88 return Units.Table (U).Dependent_Unit;
91 function Dynamic_Elab (U : Unit_Number_Type) return Boolean is
93 return Units.Table (U).Dynamic_Elab;
96 function Error_Location (U : Unit_Number_Type) return Source_Ptr is
98 return Units.Table (U).Error_Location;
101 function Expected_Unit (U : Unit_Number_Type) return Unit_Name_Type is
103 return Units.Table (U).Expected_Unit;
106 function Fatal_Error (U : Unit_Number_Type) return Boolean is
108 return Units.Table (U).Fatal_Error;
111 function Generate_Code (U : Unit_Number_Type) return Boolean is
113 return Units.Table (U).Generate_Code;
116 function Has_RACW (U : Unit_Number_Type) return Boolean is
118 return Units.Table (U).Has_RACW;
121 function Ident_String (U : Unit_Number_Type) return Node_Id is
123 return Units.Table (U).Ident_String;
126 function Loading (U : Unit_Number_Type) return Boolean is
128 return Units.Table (U).Loading;
131 function Main_Priority (U : Unit_Number_Type) return Int is
133 return Units.Table (U).Main_Priority;
136 function Source_Index (U : Unit_Number_Type) return Source_File_Index is
138 return Units.Table (U).Source_Index;
141 function Unit_File_Name (U : Unit_Number_Type) return File_Name_Type is
143 return Units.Table (U).Unit_File_Name;
146 function Unit_Name (U : Unit_Number_Type) return Unit_Name_Type is
148 return Units.Table (U).Unit_Name;
151 ------------------------------------------
152 -- Subprograms to Set Unit Table Fields --
153 ------------------------------------------
155 procedure Set_Cunit (U : Unit_Number_Type; N : Node_Id) is
157 Units.Table (U).Cunit := N;
160 procedure Set_Cunit_Entity (U : Unit_Number_Type; E : Entity_Id) is
162 Units.Table (U).Cunit_Entity := E;
163 Set_Is_Compilation_Unit (E);
164 end Set_Cunit_Entity;
166 procedure Set_Dynamic_Elab (U : Unit_Number_Type; B : Boolean := True) is
168 Units.Table (U).Dynamic_Elab := B;
169 end Set_Dynamic_Elab;
171 procedure Set_Error_Location (U : Unit_Number_Type; W : Source_Ptr) is
173 Units.Table (U).Error_Location := W;
174 end Set_Error_Location;
176 procedure Set_Fatal_Error (U : Unit_Number_Type; B : Boolean := True) is
178 Units.Table (U).Fatal_Error := B;
181 procedure Set_Generate_Code (U : Unit_Number_Type; B : Boolean := True) is
183 Units.Table (U).Generate_Code := B;
184 end Set_Generate_Code;
186 procedure Set_Has_RACW (U : Unit_Number_Type; B : Boolean := True) is
188 Units.Table (U).Has_RACW := B;
191 procedure Set_Ident_String (U : Unit_Number_Type; N : Node_Id) is
193 Units.Table (U).Ident_String := N;
194 end Set_Ident_String;
196 procedure Set_Loading (U : Unit_Number_Type; B : Boolean := True) is
198 Units.Table (U).Loading := B;
201 procedure Set_Main_Priority (U : Unit_Number_Type; P : Int) is
203 Units.Table (U).Main_Priority := P;
204 end Set_Main_Priority;
206 procedure Set_Unit_Name (U : Unit_Number_Type; N : Unit_Name_Type) is
208 Units.Table (U).Unit_Name := N;
211 ------------------------------
212 -- Check_Same_Extended_Unit --
213 ------------------------------
215 function Check_Same_Extended_Unit (S1, S2 : Source_Ptr) return SEU_Result is
218 Sind1 : Source_File_Index;
219 Sind2 : Source_File_Index;
222 Unum1 : Unit_Number_Type;
223 Unum2 : Unit_Number_Type;
230 if S1 = No_Location or else S2 = No_Location then
233 elsif S1 = Standard_Location then
234 if S2 = Standard_Location then
240 elsif S2 = Standard_Location then
246 Unum1 := Get_Code_Unit (Sloc1);
247 Unum2 := Get_Code_Unit (Sloc2);
250 Sind1 := Get_Source_File_Index (Sloc1);
251 Sind2 := Get_Source_File_Index (Sloc2);
253 if Sind1 = Sind2 then
254 if Sloc1 < Sloc2 then
256 elsif Sloc1 > Sloc2 then
263 -- OK, the two nodes are in separate source elements, but this is not
264 -- decisive, because of the issue of subunits and instantiations.
266 -- First we deal with subunits, since if the subunit is in an
267 -- instantiation, we know that the parent is in the corresponding
268 -- instantiation, since that is the only way we can have a subunit
269 -- that is part of an instantiation.
271 Unit1 := Unit (Cunit (Unum1));
272 Unit2 := Unit (Cunit (Unum2));
274 if Nkind (Unit1) = N_Subunit
275 and then Present (Corresponding_Stub (Unit1))
277 -- Both in subunits. They could have a common ancestor. If they
278 -- do, then the deeper one must have a longer unit name. Replace
279 -- the deeper one with its corresponding stub, in order to find
280 -- nearest common ancestor, if any.
282 if Nkind (Unit2) = N_Subunit
283 and then Present (Corresponding_Stub (Unit2))
285 if Length_Of_Name (Unit_Name (Unum1)) <
286 Length_Of_Name (Unit_Name (Unum2))
288 Sloc2 := Sloc (Corresponding_Stub (Unit2));
289 Unum2 := Get_Source_Unit (Sloc2);
293 Sloc1 := Sloc (Corresponding_Stub (Unit1));
294 Unum1 := Get_Source_Unit (Sloc1);
298 -- Nod1 in subunit, Nod2 not
301 Sloc1 := Sloc (Corresponding_Stub (Unit1));
302 Unum1 := Get_Source_Unit (Sloc1);
306 -- Nod2 in subunit, Nod1 not
308 elsif Nkind (Unit2) = N_Subunit
309 and then Present (Corresponding_Stub (Unit2))
311 Sloc2 := Sloc (Corresponding_Stub (Unit2));
312 Unum2 := Get_Source_Unit (Sloc2);
316 -- At this stage we know that neither is a subunit, so we deal
317 -- with instantiations, since we culd have a common ancestor
319 Inst1 := Instantiation (Sind1);
320 Inst2 := Instantiation (Sind2);
322 if Inst1 /= No_Location then
324 -- Both are instantiations
326 if Inst2 /= No_Location then
328 Depth1 := Instantiation_Depth (Sloc1);
329 Depth2 := Instantiation_Depth (Sloc2);
331 if Depth1 < Depth2 then
333 Unum2 := Get_Source_Unit (Sloc2);
336 elsif Depth1 > Depth2 then
338 Unum1 := Get_Source_Unit (Sloc1);
344 Unum1 := Get_Source_Unit (Sloc1);
345 Unum2 := Get_Source_Unit (Sloc2);
349 -- Only first node is in instantiation
353 Unum1 := Get_Source_Unit (Sloc1);
357 -- Only second node is instantiation
359 elsif Inst2 /= No_Location then
361 Unum2 := Get_Source_Unit (Sloc2);
365 -- No instantiations involved, so we are not in the same unit
366 -- However, there is one case still to check, namely the case
367 -- where one location is in the spec, and the other in the
368 -- corresponding body (the spec location is earlier).
370 if Nkind (Unit1) = N_Subprogram_Body
372 Nkind (Unit1) = N_Package_Body
374 if Library_Unit (Cunit (Unum1)) = Cunit (Unum2) then
378 elsif Nkind (Unit2) = N_Subprogram_Body
380 Nkind (Unit2) = N_Package_Body
382 if Library_Unit (Cunit (Unum2)) = Cunit (Unum1) then
387 -- If that special case does not occur, then we are certain that
388 -- the two locations are really in separate units.
395 end Check_Same_Extended_Unit;
397 -------------------------------
398 -- Compilation_Switches_Last --
399 -------------------------------
401 function Compilation_Switches_Last return Nat is
403 return Compilation_Switches.Last;
404 end Compilation_Switches_Last;
406 ------------------------------
407 -- Earlier_In_Extended_Unit --
408 ------------------------------
410 function Earlier_In_Extended_Unit (S1, S2 : Source_Ptr) return Boolean is
412 return Check_Same_Extended_Unit (S1, S2) = Yes_Before;
413 end Earlier_In_Extended_Unit;
415 ----------------------------
416 -- Entity_Is_In_Main_Unit --
417 ----------------------------
419 function Entity_Is_In_Main_Unit (E : Entity_Id) return Boolean is
425 while S /= Standard_Standard loop
426 if S = Main_Unit_Entity then
428 elsif Ekind (S) = E_Package and then Is_Child_Unit (S) then
436 end Entity_Is_In_Main_Unit;
438 ---------------------------------
439 -- Generic_Separately_Compiled --
440 ---------------------------------
442 function Generic_Separately_Compiled (E : Entity_Id) return Boolean is
444 -- We do not generate object files for internal generics, because
445 -- the only thing they would contain is the elaboration boolean, and
446 -- we are careful to elaborate all predefined units first anyway, so
447 -- this boolean is not needed.
449 if Is_Internal_File_Name
450 (Fname => Unit_File_Name (Get_Source_Unit (E)),
451 Renamings_Included => True)
455 -- All other generic units do generate object files
460 end Generic_Separately_Compiled;
462 function Generic_Separately_Compiled
463 (Sfile : File_Name_Type) return Boolean
466 -- Exactly the same as previous function, but works directly on a file
469 if Is_Internal_File_Name
471 Renamings_Included => True)
475 -- All other generic units do generate object files
480 end Generic_Separately_Compiled;
486 function Get_Code_Unit (S : Source_Ptr) return Unit_Number_Type is
488 -- Search table unless we have No_Location, which can happen if the
489 -- relevant location has not been set yet. Happens for example when
490 -- we obtain Sloc (Cunit (Main_Unit)) before it is set.
492 if S /= No_Location then
494 Source_File : constant Source_File_Index :=
495 Get_Source_File_Index (Top_Level_Location (S));
498 for U in Units.First .. Units.Last loop
499 if Source_Index (U) = Source_File then
506 -- If S was No_Location, or was not in the table, we must be in the
507 -- main source unit (and the value has not been placed in the table yet)
512 function Get_Code_Unit (N : Node_Or_Entity_Id) return Unit_Number_Type is
514 return Get_Code_Unit (Sloc (N));
517 ----------------------------
518 -- Get_Compilation_Switch --
519 ----------------------------
521 function Get_Compilation_Switch (N : Pos) return String_Ptr is
523 if N <= Compilation_Switches.Last then
524 return Compilation_Switches.Table (N);
529 end Get_Compilation_Switch;
531 ----------------------------------
532 -- Get_Cunit_Entity_Unit_Number --
533 ----------------------------------
535 function Get_Cunit_Entity_Unit_Number
536 (E : Entity_Id) return Unit_Number_Type
539 for U in Units.First .. Units.Last loop
540 if Cunit_Entity (U) = E then
545 -- If not in the table, must be the main source unit, and we just
546 -- have not got it put into the table yet.
549 end Get_Cunit_Entity_Unit_Number;
551 ---------------------------
552 -- Get_Cunit_Unit_Number --
553 ---------------------------
555 function Get_Cunit_Unit_Number (N : Node_Id) return Unit_Number_Type is
557 for U in Units.First .. Units.Last loop
558 if Cunit (U) = N then
563 -- If not in the table, must be the main source unit, and we just
564 -- have not got it put into the table yet.
567 end Get_Cunit_Unit_Number;
569 ---------------------
570 -- Get_Source_Unit --
571 ---------------------
573 function Get_Source_Unit (S : Source_Ptr) return Unit_Number_Type is
575 -- Search table unless we have No_Location, which can happen if the
576 -- relevant location has not been set yet. Happens for example when
577 -- we obtain Sloc (Cunit (Main_Unit)) before it is set.
579 if S /= No_Location then
581 Source_File : Source_File_Index :=
582 Get_Source_File_Index (Top_Level_Location (S));
585 Source_File := Get_Source_File_Index (S);
586 while Template (Source_File) /= No_Source_File loop
587 Source_File := Template (Source_File);
590 for U in Units.First .. Units.Last loop
591 if Source_Index (U) = Source_File then
598 -- If S was No_Location, or was not in the table, we must be in the
599 -- main source unit (and the value is not got put into the table yet)
604 function Get_Source_Unit (N : Node_Or_Entity_Id) return Unit_Number_Type is
606 return Get_Source_Unit (Sloc (N));
609 --------------------------------
610 -- In_Extended_Main_Code_Unit --
611 --------------------------------
613 function In_Extended_Main_Code_Unit
614 (N : Node_Or_Entity_Id) return Boolean
617 if Sloc (N) = Standard_Location then
620 elsif Sloc (N) = No_Location then
623 -- Special case Itypes to test the Sloc of the associated node. The
624 -- reason we do this is for possible calls from gigi after -gnatD
625 -- processing is complete in sprint. This processing updates the
626 -- sloc fields of all nodes in the tree, but itypes are not in the
627 -- tree so their slocs do not get updated.
629 elsif Nkind (N) = N_Defining_Identifier
630 and then Is_Itype (N)
632 return In_Extended_Main_Code_Unit (Associated_Node_For_Itype (N));
634 -- Otherwise see if we are in the main unit
636 elsif Get_Code_Unit (Sloc (N)) = Get_Code_Unit (Cunit (Main_Unit)) then
639 -- Node may be in spec (or subunit etc) of main unit
643 In_Same_Extended_Unit (Sloc (N), Sloc (Cunit (Main_Unit)));
645 end In_Extended_Main_Code_Unit;
647 function In_Extended_Main_Code_Unit (Loc : Source_Ptr) return Boolean is
649 if Loc = Standard_Location then
652 elsif Loc = No_Location then
655 -- Otherwise see if we are in the main unit
657 elsif Get_Code_Unit (Loc) = Get_Code_Unit (Cunit (Main_Unit)) then
660 -- Location may be in spec (or subunit etc) of main unit
664 In_Same_Extended_Unit (Loc, Sloc (Cunit (Main_Unit)));
666 end In_Extended_Main_Code_Unit;
668 ----------------------------------
669 -- In_Extended_Main_Source_Unit --
670 ----------------------------------
672 function In_Extended_Main_Source_Unit
673 (N : Node_Or_Entity_Id) return Boolean
675 Nloc : constant Source_Ptr := Sloc (N);
676 Mloc : constant Source_Ptr := Sloc (Cunit (Main_Unit));
679 -- If Mloc is not set, it means we are still parsing the main unit,
680 -- so everything so far is in the extended main source unit.
682 if Mloc = No_Location then
685 -- Special value cases
687 elsif Nloc = Standard_Location then
690 elsif Nloc = No_Location then
693 -- Special case Itypes to test the Sloc of the associated node. The
694 -- reason we do this is for possible calls from gigi after -gnatD
695 -- processing is complete in sprint. This processing updates the
696 -- sloc fields of all nodes in the tree, but itypes are not in the
697 -- tree so their slocs do not get updated.
699 elsif Nkind (N) = N_Defining_Identifier
700 and then Is_Itype (N)
702 return In_Extended_Main_Source_Unit (Associated_Node_For_Itype (N));
704 -- Otherwise compare original locations to see if in same unit
708 In_Same_Extended_Unit
709 (Original_Location (Nloc), Original_Location (Mloc));
711 end In_Extended_Main_Source_Unit;
713 function In_Extended_Main_Source_Unit
714 (Loc : Source_Ptr) return Boolean
716 Mloc : constant Source_Ptr := Sloc (Cunit (Main_Unit));
719 -- If Mloc is not set, it means we are still parsing the main unit,
720 -- so everything so far is in the extended main source unit.
722 if Mloc = No_Location then
725 -- Special value cases
727 elsif Loc = Standard_Location then
730 elsif Loc = No_Location then
733 -- Otherwise compare original locations to see if in same unit
737 In_Same_Extended_Unit
738 (Original_Location (Loc), Original_Location (Mloc));
740 end In_Extended_Main_Source_Unit;
742 -----------------------
743 -- In_Same_Code_Unit --
744 -----------------------
746 function In_Same_Code_Unit (N1, N2 : Node_Or_Entity_Id) return Boolean is
747 S1 : constant Source_Ptr := Sloc (N1);
748 S2 : constant Source_Ptr := Sloc (N2);
751 if S1 = No_Location or else S2 = No_Location then
754 elsif S1 = Standard_Location then
755 return S2 = Standard_Location;
757 elsif S2 = Standard_Location then
761 return Get_Code_Unit (N1) = Get_Code_Unit (N2);
762 end In_Same_Code_Unit;
764 ---------------------------
765 -- In_Same_Extended_Unit --
766 ---------------------------
768 function In_Same_Extended_Unit (S1, S2 : Source_Ptr) return Boolean is
770 return Check_Same_Extended_Unit (S1, S2) /= No;
771 end In_Same_Extended_Unit;
773 -------------------------
774 -- In_Same_Source_Unit --
775 -------------------------
777 function In_Same_Source_Unit (N1, N2 : Node_Or_Entity_Id) return Boolean is
778 S1 : constant Source_Ptr := Sloc (N1);
779 S2 : constant Source_Ptr := Sloc (N2);
782 if S1 = No_Location or else S2 = No_Location then
785 elsif S1 = Standard_Location then
786 return S2 = Standard_Location;
788 elsif S2 = Standard_Location then
792 return Get_Source_Unit (N1) = Get_Source_Unit (N2);
793 end In_Same_Source_Unit;
795 -----------------------------
796 -- Increment_Serial_Number --
797 -----------------------------
799 function Increment_Serial_Number return Nat is
800 TSN : Int renames Units.Table (Current_Sem_Unit).Serial_Number;
805 end Increment_Serial_Number;
811 procedure Initialize is
813 Linker_Option_Lines.Init;
816 Unit_Exception_Table_Present := False;
817 Compilation_Switches.Init;
824 function Is_Loaded (Uname : Unit_Name_Type) return Boolean is
826 for Unum in Units.First .. Units.Last loop
827 if Uname = Unit_Name (Unum) then
839 function Last_Unit return Unit_Number_Type is
848 procedure List (File_Names_Only : Boolean := False) is separate;
856 Linker_Option_Lines.Locked := True;
857 Load_Stack.Locked := True;
858 Units.Locked := True;
859 Linker_Option_Lines.Release;
868 function Num_Units return Nat is
870 return Int (Units.Last) - Int (Main_Unit) + 1;
877 procedure Remove_Unit (U : Unit_Number_Type) is
879 if U = Units.Last then
880 Units.Decrement_Last;
884 ----------------------------------
885 -- Replace_Linker_Option_String --
886 ----------------------------------
888 procedure Replace_Linker_Option_String
889 (S : String_Id; Match_String : String)
892 if Match_String'Length > 0 then
893 for J in 1 .. Linker_Option_Lines.Last loop
894 String_To_Name_Buffer (Linker_Option_Lines.Table (J).Option);
896 if Match_String = Name_Buffer (1 .. Match_String'Length) then
897 Linker_Option_Lines.Table (J).Option := S;
903 Store_Linker_Option_String (S);
904 end Replace_Linker_Option_String;
910 procedure Sort (Tbl : in out Unit_Ref_Table) is separate;
912 ------------------------------
913 -- Store_Compilation_Switch --
914 ------------------------------
916 procedure Store_Compilation_Switch (Switch : String) is
918 Compilation_Switches.Increment_Last;
919 Compilation_Switches.Table (Compilation_Switches.Last) :=
922 -- Fix up --RTS flag which has been transformed by the gcc driver
925 if Switch'Last >= Switch'First + 4
926 and then Switch (Switch'First .. Switch'First + 4) = "-fRTS"
928 Compilation_Switches.Table
929 (Compilation_Switches.Last) (Switch'First + 1) := '-';
931 end Store_Compilation_Switch;
933 --------------------------------
934 -- Store_Linker_Option_String --
935 --------------------------------
937 procedure Store_Linker_Option_String (S : String_Id) is
939 Linker_Option_Lines.Increment_Last;
940 Linker_Option_Lines.Table (Linker_Option_Lines.Last) :=
941 (Option => S, Unit => Current_Sem_Unit);
942 end Store_Linker_Option_String;
948 procedure Tree_Read is
955 -- Read Compilation_Switches table
958 Compilation_Switches.Set_Last (N);
962 Compilation_Switches.Table (J) := S;
970 procedure Tree_Write is
974 -- Write Compilation_Switches table
976 Tree_Write_Int (Compilation_Switches.Last);
978 for J in 1 .. Compilation_Switches.Last loop
979 Tree_Write_Str (Compilation_Switches.Table (J));
987 function Version_Get (U : Unit_Number_Type) return Word_Hex_String is
989 return Get_Hex_String (Units.Table (U).Version);
992 ------------------------
993 -- Version_Referenced --
994 ------------------------
996 procedure Version_Referenced (S : String_Id) is
998 Version_Ref.Append (S);
999 end Version_Referenced;