1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
10 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
30 -- GNAT was originally developed by the GNAT team at New York University. --
31 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
33 ------------------------------------------------------------------------------
35 pragma Style_Checks (All_Checks);
36 -- Subprogram ordering not enforced in this unit
37 -- (because of some logical groupings).
39 with Atree; use Atree;
40 with Einfo; use Einfo;
41 with Fname; use Fname;
42 with Namet; use Namet;
43 with Namet; use Namet;
44 with Output; use Output;
45 with Sinfo; use Sinfo;
46 with Sinput; use Sinput;
47 with Stand; use Stand;
48 with Stringt; use Stringt;
49 with Tree_IO; use Tree_IO;
50 with Uname; use Uname;
54 -----------------------
55 -- Local Subprograms --
56 -----------------------
59 Yes_Before, -- S1 is in same extended unit as S2 and appears before it
60 Yes_Same, -- S1 is in same extended unit as S2, Slocs are the same
61 Yes_After, -- S1 is in same extended unit as S2, and appears after it
62 No); -- S2 is not in same extended unit as S2
64 function Check_Same_Extended_Unit (S1, S2 : Source_Ptr) return SEU_Result;
65 -- Used by In_Same_Extended_Unit and Earlier_In_Extended_Unit. Returns
66 -- value as described above.
68 --------------------------------------------
69 -- Access Functions for Unit Table Fields --
70 --------------------------------------------
72 function Cunit (U : Unit_Number_Type) return Node_Id is
74 return Units.Table (U).Cunit;
77 function Cunit_Entity (U : Unit_Number_Type) return Entity_Id is
79 return Units.Table (U).Cunit_Entity;
82 function Dependency_Num (U : Unit_Number_Type) return Nat is
84 return Units.Table (U).Dependency_Num;
87 function Dependent_Unit (U : Unit_Number_Type) return Boolean is
89 return Units.Table (U).Dependent_Unit;
92 function Dynamic_Elab (U : Unit_Number_Type) return Boolean is
94 return Units.Table (U).Dynamic_Elab;
97 function Error_Location (U : Unit_Number_Type) return Source_Ptr is
99 return Units.Table (U).Error_Location;
102 function Expected_Unit (U : Unit_Number_Type) return Unit_Name_Type is
104 return Units.Table (U).Expected_Unit;
107 function Fatal_Error (U : Unit_Number_Type) return Boolean is
109 return Units.Table (U).Fatal_Error;
112 function Generate_Code (U : Unit_Number_Type) return Boolean is
114 return Units.Table (U).Generate_Code;
117 function Has_RACW (U : Unit_Number_Type) return Boolean is
119 return Units.Table (U).Has_RACW;
122 function Ident_String (U : Unit_Number_Type) return Node_Id is
124 return Units.Table (U).Ident_String;
127 function Loading (U : Unit_Number_Type) return Boolean is
129 return Units.Table (U).Loading;
132 function Main_Priority (U : Unit_Number_Type) return Int is
134 return Units.Table (U).Main_Priority;
137 function Source_Index (U : Unit_Number_Type) return Source_File_Index is
139 return Units.Table (U).Source_Index;
142 function Unit_File_Name (U : Unit_Number_Type) return File_Name_Type is
144 return Units.Table (U).Unit_File_Name;
147 function Unit_Name (U : Unit_Number_Type) return Unit_Name_Type is
149 return Units.Table (U).Unit_Name;
152 ------------------------------------------
153 -- Subprograms to Set Unit Table Fields --
154 ------------------------------------------
156 procedure Set_Cunit (U : Unit_Number_Type; N : Node_Id) is
158 Units.Table (U).Cunit := N;
161 procedure Set_Cunit_Entity (U : Unit_Number_Type; E : Entity_Id) is
163 Units.Table (U).Cunit_Entity := E;
164 Set_Is_Compilation_Unit (E);
165 end Set_Cunit_Entity;
167 procedure Set_Dynamic_Elab (U : Unit_Number_Type; B : Boolean := True) is
169 Units.Table (U).Dynamic_Elab := B;
170 end Set_Dynamic_Elab;
172 procedure Set_Error_Location (U : Unit_Number_Type; W : Source_Ptr) is
174 Units.Table (U).Error_Location := W;
175 end Set_Error_Location;
177 procedure Set_Fatal_Error (U : Unit_Number_Type; B : Boolean := True) is
179 Units.Table (U).Fatal_Error := B;
182 procedure Set_Generate_Code (U : Unit_Number_Type; B : Boolean := True) is
184 Units.Table (U).Generate_Code := B;
185 end Set_Generate_Code;
187 procedure Set_Has_RACW (U : Unit_Number_Type; B : Boolean := True) is
189 Units.Table (U).Has_RACW := B;
192 procedure Set_Ident_String (U : Unit_Number_Type; N : Node_Id) is
194 Units.Table (U).Ident_String := N;
195 end Set_Ident_String;
197 procedure Set_Loading (U : Unit_Number_Type; B : Boolean := True) is
199 Units.Table (U).Loading := B;
202 procedure Set_Main_Priority (U : Unit_Number_Type; P : Int) is
204 Units.Table (U).Main_Priority := P;
205 end Set_Main_Priority;
207 procedure Set_Unit_Name (U : Unit_Number_Type; N : Unit_Name_Type) is
209 Units.Table (U).Unit_Name := N;
212 ------------------------------
213 -- Check_Same_Extended_Unit --
214 ------------------------------
216 function Check_Same_Extended_Unit (S1, S2 : Source_Ptr) return SEU_Result is
219 Sind1 : Source_File_Index;
220 Sind2 : Source_File_Index;
223 Unum1 : Unit_Number_Type;
224 Unum2 : Unit_Number_Type;
231 if S1 = No_Location or else S2 = No_Location then
234 elsif S1 = Standard_Location then
235 if S2 = Standard_Location then
241 elsif S2 = Standard_Location then
247 Unum1 := Get_Code_Unit (Sloc1);
248 Unum2 := Get_Code_Unit (Sloc2);
251 Sind1 := Get_Source_File_Index (Sloc1);
252 Sind2 := Get_Source_File_Index (Sloc2);
254 if Sind1 = Sind2 then
255 if Sloc1 < Sloc2 then
257 elsif Sloc1 > Sloc2 then
264 -- OK, the two nodes are in separate source elements, but this is not
265 -- decisive, because of the issue of subunits and instantiations.
267 -- First we deal with subunits, since if the subunit is in an
268 -- instantiation, we know that the parent is in the corresponding
269 -- instantiation, since that is the only way we can have a subunit
270 -- that is part of an instantiation.
272 Unit1 := Unit (Cunit (Unum1));
273 Unit2 := Unit (Cunit (Unum2));
275 if Nkind (Unit1) = N_Subunit
276 and then Present (Corresponding_Stub (Unit1))
278 -- Both in subunits. They could have a common ancestor. If they
279 -- do, then the deeper one must have a longer unit name. Replace
280 -- the deeper one with its corresponding stub, in order to find
281 -- nearest common ancestor, if any.
283 if Nkind (Unit2) = N_Subunit
284 and then Present (Corresponding_Stub (Unit2))
286 if Length_Of_Name (Unit_Name (Unum1)) <
287 Length_Of_Name (Unit_Name (Unum2))
289 Sloc2 := Sloc (Corresponding_Stub (Unit2));
290 Unum2 := Get_Source_Unit (Sloc2);
294 Sloc1 := Sloc (Corresponding_Stub (Unit1));
295 Unum1 := Get_Source_Unit (Sloc1);
299 -- Nod1 in subunit, Nod2 not
302 Sloc1 := Sloc (Corresponding_Stub (Unit1));
303 Unum1 := Get_Source_Unit (Sloc1);
307 -- Nod2 in subunit, Nod1 not
309 elsif Nkind (Unit2) = N_Subunit
310 and then Present (Corresponding_Stub (Unit2))
312 Sloc2 := Sloc (Corresponding_Stub (Unit2));
313 Unum2 := Get_Source_Unit (Sloc2);
317 -- At this stage we know that neither is a subunit, so we deal
318 -- with instantiations, since we culd have a common ancestor
320 Inst1 := Instantiation (Sind1);
321 Inst2 := Instantiation (Sind2);
323 if Inst1 /= No_Location then
325 -- Both are instantiations
327 if Inst2 /= No_Location then
329 Depth1 := Instantiation_Depth (Sloc1);
330 Depth2 := Instantiation_Depth (Sloc2);
332 if Depth1 < Depth2 then
334 Unum2 := Get_Source_Unit (Sloc2);
337 elsif Depth1 > Depth2 then
339 Unum1 := Get_Source_Unit (Sloc1);
345 Unum1 := Get_Source_Unit (Sloc1);
346 Unum2 := Get_Source_Unit (Sloc2);
350 -- Only first node is in instantiation
354 Unum1 := Get_Source_Unit (Sloc1);
358 -- Only second node is instantiation
360 elsif Inst2 /= No_Location then
362 Unum2 := Get_Source_Unit (Sloc2);
366 -- No instantiations involved, so we are not in the same unit
367 -- However, there is one case still to check, namely the case
368 -- where one location is in the spec, and the other in the
369 -- corresponding body (the spec location is earlier).
371 if Nkind (Unit1) = N_Subprogram_Body
373 Nkind (Unit1) = N_Package_Body
375 if Library_Unit (Cunit (Unum1)) = Cunit (Unum2) then
379 elsif Nkind (Unit2) = N_Subprogram_Body
381 Nkind (Unit2) = N_Package_Body
383 if Library_Unit (Cunit (Unum2)) = Cunit (Unum1) then
388 -- If that special case does not occur, then we are certain that
389 -- 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 ------------------------------
409 -- Earlier_In_Extended_Unit --
410 ------------------------------
412 function Earlier_In_Extended_Unit (S1, S2 : Source_Ptr) return Boolean is
414 return Check_Same_Extended_Unit (S1, S2) = Yes_Before;
415 end Earlier_In_Extended_Unit;
417 ----------------------------
418 -- Entity_Is_In_Main_Unit --
419 ----------------------------
421 function Entity_Is_In_Main_Unit (E : Entity_Id) return Boolean is
427 while S /= Standard_Standard loop
428 if S = Main_Unit_Entity then
430 elsif Ekind (S) = E_Package and then Is_Child_Unit (S) then
438 end Entity_Is_In_Main_Unit;
440 ---------------------------------
441 -- Generic_Separately_Compiled --
442 ---------------------------------
444 function Generic_Separately_Compiled (E : Entity_Id) return Boolean is
446 -- We do not generate object files for internal generics, because
447 -- the only thing they would contain is the elaboration boolean, and
448 -- we are careful to elaborate all predefined units first anyway, so
449 -- this boolean is not needed.
451 if Is_Internal_File_Name
452 (Fname => Unit_File_Name (Get_Source_Unit (E)),
453 Renamings_Included => True)
457 -- All other generic units do generate object files
462 end Generic_Separately_Compiled;
468 function Get_Code_Unit (S : Source_Ptr) return Unit_Number_Type is
469 Source_File : Source_File_Index :=
470 Get_Source_File_Index (Top_Level_Location (S));
473 for U in Units.First .. Units.Last loop
474 if Source_Index (U) = Source_File then
479 -- If not in the table, must be the main source unit, and we just
480 -- have not got it put into the table yet.
485 function Get_Code_Unit (N : Node_Or_Entity_Id) return Unit_Number_Type is
487 return Get_Code_Unit (Sloc (N));
490 ----------------------------
491 -- Get_Compilation_Switch --
492 ----------------------------
494 function Get_Compilation_Switch (N : Pos) return String_Ptr is
496 if N <= Compilation_Switches.Last then
497 return Compilation_Switches.Table (N);
502 end Get_Compilation_Switch;
504 ----------------------------------
505 -- Get_Cunit_Entity_Unit_Number --
506 ----------------------------------
508 function Get_Cunit_Entity_Unit_Number
510 return Unit_Number_Type
513 for U in Units.First .. Units.Last loop
514 if Cunit_Entity (U) = E then
519 -- If not in the table, must be the main source unit, and we just
520 -- have not got it put into the table yet.
523 end Get_Cunit_Entity_Unit_Number;
525 ---------------------------
526 -- Get_Cunit_Unit_Number --
527 ---------------------------
529 function Get_Cunit_Unit_Number (N : Node_Id) return Unit_Number_Type is
531 for U in Units.First .. Units.Last loop
532 if Cunit (U) = N then
537 -- If not in the table, must be the main source unit, and we just
538 -- have not got it put into the table yet.
541 end Get_Cunit_Unit_Number;
543 ---------------------
544 -- Get_Source_Unit --
545 ---------------------
547 function Get_Source_Unit (S : Source_Ptr) return Unit_Number_Type is
548 Source_File : Source_File_Index :=
549 Get_Source_File_Index (Top_Level_Location (S));
552 Source_File := Get_Source_File_Index (S);
553 while Template (Source_File) /= No_Source_File loop
554 Source_File := Template (Source_File);
557 for U in Units.First .. Units.Last loop
558 if Source_Index (U) = Source_File 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.
569 function Get_Source_Unit (N : Node_Or_Entity_Id) return Unit_Number_Type is
571 return Get_Source_Unit (Sloc (N));
574 --------------------------------
575 -- In_Extended_Main_Code_Unit --
576 --------------------------------
578 function In_Extended_Main_Code_Unit
579 (N : Node_Or_Entity_Id)
583 if Sloc (N) = Standard_Location then
586 elsif Sloc (N) = No_Location then
589 -- Special case Itypes to test the Sloc of the associated node. The
590 -- reason we do this is for possible calls from gigi after -gnatD
591 -- processing is complete in sprint. This processing updates the
592 -- sloc fields of all nodes in the tree, but itypes are not in the
593 -- tree so their slocs do not get updated.
595 elsif Nkind (N) = N_Defining_Identifier
596 and then Is_Itype (N)
598 return In_Extended_Main_Code_Unit (Associated_Node_For_Itype (N));
600 elsif Get_Code_Unit (Sloc (N)) = Get_Code_Unit (Cunit (Main_Unit)) then
603 else -- node may be in spec of main unit
605 In_Same_Extended_Unit (Sloc (N), Sloc (Cunit (Main_Unit)));
607 end In_Extended_Main_Code_Unit;
609 ----------------------------------
610 -- In_Extended_Main_Source_Unit --
611 ----------------------------------
613 function In_Extended_Main_Source_Unit
614 (N : Node_Or_Entity_Id)
618 if Sloc (N) = Standard_Location then
621 elsif Sloc (N) = No_Location then
624 -- Special case Itypes to test the Sloc of the associated node. The
625 -- reason we do this is for possible calls from gigi after -gnatD
626 -- processing is complete in sprint. This processing updates the
627 -- sloc fields of all nodes in the tree, but itypes are not in the
628 -- tree so their slocs do not get updated.
630 elsif Nkind (N) = N_Defining_Identifier
631 and then Is_Itype (N)
633 return In_Extended_Main_Source_Unit (Associated_Node_For_Itype (N));
637 In_Same_Extended_Unit
638 (Original_Location (Sloc (N)),
639 Original_Location (Sloc (Cunit (Main_Unit))));
641 end In_Extended_Main_Source_Unit;
643 -----------------------
644 -- In_Same_Code_Unit --
645 -----------------------
647 function In_Same_Code_Unit (N1, N2 : Node_Or_Entity_Id) return Boolean is
648 S1 : constant Source_Ptr := Sloc (N1);
649 S2 : constant Source_Ptr := Sloc (N2);
652 if S1 = No_Location or else S2 = No_Location then
655 elsif S1 = Standard_Location then
656 return S2 = Standard_Location;
658 elsif S2 = Standard_Location then
662 return Get_Code_Unit (N1) = Get_Code_Unit (N2);
663 end In_Same_Code_Unit;
665 ---------------------------
666 -- In_Same_Extended_Unit --
667 ---------------------------
669 function In_Same_Extended_Unit (S1, S2 : Source_Ptr) return Boolean is
671 return Check_Same_Extended_Unit (S1, S2) /= No;
672 end In_Same_Extended_Unit;
674 -------------------------
675 -- In_Same_Source_Unit --
676 -------------------------
678 function In_Same_Source_Unit (N1, N2 : Node_Or_Entity_Id) return Boolean is
679 S1 : constant Source_Ptr := Sloc (N1);
680 S2 : constant Source_Ptr := Sloc (N2);
683 if S1 = No_Location or else S2 = No_Location then
686 elsif S1 = Standard_Location then
687 return S2 = Standard_Location;
689 elsif S2 = Standard_Location then
693 return Get_Source_Unit (N1) = Get_Source_Unit (N2);
694 end In_Same_Source_Unit;
696 -----------------------------
697 -- Increment_Serial_Number --
698 -----------------------------
700 function Increment_Serial_Number return Nat is
701 TSN : Int renames Units.Table (Current_Sem_Unit).Serial_Number;
706 end Increment_Serial_Number;
712 procedure Initialize is
714 Linker_Option_Lines.Init;
717 Unit_Exception_Table_Present := False;
718 Compilation_Switches.Init;
725 function Is_Loaded (Uname : Unit_Name_Type) return Boolean is
727 for Unum in Units.First .. Units.Last loop
728 if Uname = Unit_Name (Unum) then
740 function Last_Unit return Unit_Number_Type is
749 procedure List (File_Names_Only : Boolean := False) is separate;
757 Linker_Option_Lines.Locked := True;
758 Load_Stack.Locked := True;
759 Units.Locked := True;
760 Linker_Option_Lines.Release;
769 function Num_Units return Nat is
771 return Int (Units.Last) - Int (Main_Unit) + 1;
774 ----------------------------------
775 -- Replace_Linker_Option_String --
776 ----------------------------------
778 procedure Replace_Linker_Option_String
779 (S : String_Id; Match_String : String)
782 if Match_String'Length > 0 then
783 for J in 1 .. Linker_Option_Lines.Last loop
784 String_To_Name_Buffer (Linker_Option_Lines.Table (J).Option);
786 if Match_String = Name_Buffer (1 .. Match_String'Length) then
787 Linker_Option_Lines.Table (J).Option := S;
793 Store_Linker_Option_String (S);
794 end Replace_Linker_Option_String;
800 procedure Sort (Tbl : in out Unit_Ref_Table) is separate;
802 ------------------------------
803 -- Store_Compilation_Switch --
804 ------------------------------
806 procedure Store_Compilation_Switch (Switch : String) is
808 Compilation_Switches.Increment_Last;
809 Compilation_Switches.Table (Compilation_Switches.Last) :=
811 end Store_Compilation_Switch;
813 --------------------------------
814 -- Store_Linker_Option_String --
815 --------------------------------
817 procedure Store_Linker_Option_String (S : String_Id) is
819 Linker_Option_Lines.Increment_Last;
820 Linker_Option_Lines.Table (Linker_Option_Lines.Last) :=
821 (Option => S, Unit => Current_Sem_Unit);
822 end Store_Linker_Option_String;
828 procedure Tree_Read is
835 -- Read Compilation_Switches table
838 Compilation_Switches.Set_Last (N);
842 Compilation_Switches.Table (J) := S;
850 procedure Tree_Write is
854 -- Write Compilation_Switches table
856 Tree_Write_Int (Compilation_Switches.Last);
858 for J in 1 .. Compilation_Switches.Last loop
859 Tree_Write_Str (Compilation_Switches.Table (J));
867 function Version_Get (U : Unit_Number_Type) return Word_Hex_String is
869 return Get_Hex_String (Units.Table (U).Version);
872 ------------------------
873 -- Version_Referenced --
874 ------------------------
876 procedure Version_Referenced (S : String_Id) is
878 Version_Ref.Append (S);
879 end Version_Referenced;