1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
31 -- GNAT was originally developed by the GNAT team at New York University. --
32 -- Extensive contributions were provided by Ada Core Technologies Inc. --
34 ------------------------------------------------------------------------------
36 pragma Style_Checks
(All_Checks
);
37 -- Subprogram ordering not enforced in this unit
38 -- (because of some logical groupings).
40 with Atree
; use Atree
;
41 with Einfo
; use Einfo
;
42 with Fname
; use Fname
;
43 with Namet
; use Namet
;
44 with Namet
; use Namet
;
45 with Output
; use Output
;
46 with Sinfo
; use Sinfo
;
47 with Sinput
; use Sinput
;
48 with Stand
; use Stand
;
49 with Stringt
; use Stringt
;
50 with Tree_IO
; use Tree_IO
;
51 with Uname
; use Uname
;
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 Dependent_Unit
(U
: Unit_Number_Type
) return Boolean is
90 return Units
.Table
(U
).Dependent_Unit
;
93 function Dynamic_Elab
(U
: Unit_Number_Type
) return Boolean is
95 return Units
.Table
(U
).Dynamic_Elab
;
98 function Error_Location
(U
: Unit_Number_Type
) return Source_Ptr
is
100 return Units
.Table
(U
).Error_Location
;
103 function Expected_Unit
(U
: Unit_Number_Type
) return Unit_Name_Type
is
105 return Units
.Table
(U
).Expected_Unit
;
108 function Fatal_Error
(U
: Unit_Number_Type
) return Boolean is
110 return Units
.Table
(U
).Fatal_Error
;
113 function Generate_Code
(U
: Unit_Number_Type
) return Boolean is
115 return Units
.Table
(U
).Generate_Code
;
118 function Has_RACW
(U
: Unit_Number_Type
) return Boolean is
120 return Units
.Table
(U
).Has_RACW
;
123 function Ident_String
(U
: Unit_Number_Type
) return Node_Id
is
125 return Units
.Table
(U
).Ident_String
;
128 function Loading
(U
: Unit_Number_Type
) return Boolean is
130 return Units
.Table
(U
).Loading
;
133 function Main_Priority
(U
: Unit_Number_Type
) return Int
is
135 return Units
.Table
(U
).Main_Priority
;
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
:= True;
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.
398 end Check_Same_Extended_Unit
;
400 ------------------------------
401 -- Earlier_In_Extended_Unit --
402 ------------------------------
404 function Earlier_In_Extended_Unit
(S1
, S2
: Source_Ptr
) return Boolean is
406 return Check_Same_Extended_Unit
(S1
, S2
) = Yes_Before
;
407 end Earlier_In_Extended_Unit
;
409 ----------------------------
410 -- Entity_Is_In_Main_Unit --
411 ----------------------------
413 function Entity_Is_In_Main_Unit
(E
: Entity_Id
) return Boolean is
419 while S
/= Standard_Standard
loop
420 if S
= Main_Unit_Entity
then
422 elsif Ekind
(S
) = E_Package
and then Is_Child_Unit
(S
) then
430 end Entity_Is_In_Main_Unit
;
432 ---------------------------------
433 -- Generic_Separately_Compiled --
434 ---------------------------------
436 function Generic_Separately_Compiled
(E
: Entity_Id
) return Boolean is
438 -- We do not generate object files for internal generics, because
439 -- the only thing they would contain is the elaboration boolean, and
440 -- we are careful to elaborate all predefined units first anyway, so
441 -- this boolean is not needed.
443 if Is_Internal_File_Name
444 (Fname
=> Unit_File_Name
(Get_Source_Unit
(E
)),
445 Renamings_Included
=> True)
449 -- All other generic units do generate object files
454 end Generic_Separately_Compiled
;
460 function Get_Code_Unit
(S
: Source_Ptr
) return Unit_Number_Type
is
461 Source_File
: Source_File_Index
:=
462 Get_Source_File_Index
(Top_Level_Location
(S
));
465 for U
in Units
.First
.. Units
.Last
loop
466 if Source_Index
(U
) = Source_File
then
471 -- If not in the table, must be the main source unit, and we just
472 -- have not got it put into the table yet.
477 function Get_Code_Unit
(N
: Node_Id
) return Unit_Number_Type
is
479 return Get_Code_Unit
(Sloc
(N
));
482 ----------------------------
483 -- Get_Compilation_Switch --
484 ----------------------------
486 function Get_Compilation_Switch
(N
: Pos
) return String_Ptr
is
488 if N
>= Compilation_Switches
.Last
then
489 return Compilation_Switches
.Table
(N
);
494 end Get_Compilation_Switch
;
496 ----------------------------------
497 -- Get_Cunit_Entity_Unit_Number --
498 ----------------------------------
500 function Get_Cunit_Entity_Unit_Number
502 return Unit_Number_Type
505 for U
in Units
.First
.. Units
.Last
loop
506 if Cunit_Entity
(U
) = E
then
511 -- If not in the table, must be the main source unit, and we just
512 -- have not got it put into the table yet.
515 end Get_Cunit_Entity_Unit_Number
;
517 ---------------------------
518 -- Get_Cunit_Unit_Number --
519 ---------------------------
521 function Get_Cunit_Unit_Number
(N
: Node_Id
) return Unit_Number_Type
is
523 for U
in Units
.First
.. Units
.Last
loop
524 if Cunit
(U
) = N
then
529 -- If not in the table, must be the main source unit, and we just
530 -- have not got it put into the table yet.
533 end Get_Cunit_Unit_Number
;
535 ---------------------
536 -- Get_Source_Unit --
537 ---------------------
539 function Get_Source_Unit
(S
: Source_Ptr
) return Unit_Number_Type
is
540 Source_File
: Source_File_Index
:=
541 Get_Source_File_Index
(Top_Level_Location
(S
));
544 Source_File
:= Get_Source_File_Index
(S
);
545 while Template
(Source_File
) /= No_Source_File
loop
546 Source_File
:= Template
(Source_File
);
549 for U
in Units
.First
.. Units
.Last
loop
550 if Source_Index
(U
) = Source_File
then
555 -- If not in the table, must be the main source unit, and we just
556 -- have not got it put into the table yet.
561 function Get_Source_Unit
(N
: Node_Id
) return Unit_Number_Type
is
563 return Get_Source_Unit
(Sloc
(N
));
566 --------------------------------
567 -- In_Extended_Main_Code_Unit --
568 --------------------------------
570 function In_Extended_Main_Code_Unit
(N
: Node_Id
) return Boolean is
572 if Sloc
(N
) = Standard_Location
then
575 elsif Sloc
(N
) = No_Location
then
578 -- Special case Itypes to test the Sloc of the associated node. The
579 -- reason we do this is for possible calls from gigi after -gnatD
580 -- processing is complete in sprint. This processing updates the
581 -- sloc fields of all nodes in the tree, but itypes are not in the
582 -- tree so their slocs do not get updated.
584 elsif Nkind
(N
) = N_Defining_Identifier
585 and then Is_Itype
(N
)
587 return In_Extended_Main_Code_Unit
(Associated_Node_For_Itype
(N
));
589 elsif Get_Code_Unit
(Sloc
(N
)) = Get_Code_Unit
(Cunit
(Main_Unit
)) then
592 else -- node may be in spec of main unit
594 In_Same_Extended_Unit
(Sloc
(N
), Sloc
(Cunit
(Main_Unit
)));
596 end In_Extended_Main_Code_Unit
;
598 ----------------------------------
599 -- In_Extended_Main_Source_Unit --
600 ----------------------------------
602 function In_Extended_Main_Source_Unit
(N
: Node_Id
) return Boolean is
604 if Sloc
(N
) = Standard_Location
then
607 elsif Sloc
(N
) = No_Location
then
610 -- Special case Itypes to test the Sloc of the associated node. The
611 -- reason we do this is for possible calls from gigi after -gnatD
612 -- processing is complete in sprint. This processing updates the
613 -- sloc fields of all nodes in the tree, but itypes are not in the
614 -- tree so their slocs do not get updated.
616 elsif Nkind
(N
) = N_Defining_Identifier
617 and then Is_Itype
(N
)
619 return In_Extended_Main_Source_Unit
(Associated_Node_For_Itype
(N
));
623 In_Same_Extended_Unit
624 (Original_Location
(Sloc
(N
)),
625 Original_Location
(Sloc
(Cunit
(Main_Unit
))));
627 end In_Extended_Main_Source_Unit
;
629 -----------------------
630 -- In_Same_Code_Unit --
631 -----------------------
633 function In_Same_Code_Unit
(N1
, N2
: Node_Or_Entity_Id
) return Boolean is
634 S1
: constant Source_Ptr
:= Sloc
(N1
);
635 S2
: constant Source_Ptr
:= Sloc
(N2
);
638 if S1
= No_Location
or else S2
= No_Location
then
641 elsif S1
= Standard_Location
then
642 return S2
= Standard_Location
;
644 elsif S2
= Standard_Location
then
648 return Get_Code_Unit
(N1
) = Get_Code_Unit
(N2
);
649 end In_Same_Code_Unit
;
651 ---------------------------
652 -- In_Same_Extended_Unit --
653 ---------------------------
655 function In_Same_Extended_Unit
(S1
, S2
: Source_Ptr
) return Boolean is
657 return Check_Same_Extended_Unit
(S1
, S2
) /= No
;
658 end In_Same_Extended_Unit
;
660 -------------------------
661 -- In_Same_Source_Unit --
662 -------------------------
664 function In_Same_Source_Unit
(N1
, N2
: Node_Or_Entity_Id
) return Boolean is
665 S1
: constant Source_Ptr
:= Sloc
(N1
);
666 S2
: constant Source_Ptr
:= Sloc
(N2
);
669 if S1
= No_Location
or else S2
= No_Location
then
672 elsif S1
= Standard_Location
then
673 return S2
= Standard_Location
;
675 elsif S2
= Standard_Location
then
679 return Get_Source_Unit
(N1
) = Get_Source_Unit
(N2
);
680 end In_Same_Source_Unit
;
682 -----------------------------
683 -- Increment_Serial_Number --
684 -----------------------------
686 function Increment_Serial_Number
return Nat
is
687 TSN
: Int
renames Units
.Table
(Current_Sem_Unit
).Serial_Number
;
692 end Increment_Serial_Number
;
698 procedure Initialize
is
700 Linker_Option_Lines
.Init
;
703 Unit_Exception_Table_Present
:= False;
704 Compilation_Switches
.Init
;
711 function Is_Loaded
(Uname
: Unit_Name_Type
) return Boolean is
713 for Unum
in Units
.First
.. Units
.Last
loop
714 if Uname
= Unit_Name
(Unum
) then
726 function Last_Unit
return Unit_Number_Type
is
735 procedure List
(File_Names_Only
: Boolean := False) is separate;
743 Linker_Option_Lines
.Locked
:= True;
744 Load_Stack
.Locked
:= True;
745 Units
.Locked
:= True;
746 Linker_Option_Lines
.Release
;
755 function Num_Units
return Nat
is
757 return Int
(Units
.Last
) - Int
(Main_Unit
) + 1;
760 ----------------------------------
761 -- Replace_Linker_Option_String --
762 ----------------------------------
764 procedure Replace_Linker_Option_String
765 (S
: String_Id
; Match_String
: String)
768 if Match_String
'Length > 0 then
769 for J
in 1 .. Linker_Option_Lines
.Last
loop
770 String_To_Name_Buffer
(Linker_Option_Lines
.Table
(J
));
772 if Match_String
= Name_Buffer
(1 .. Match_String
'Length) then
773 Linker_Option_Lines
.Table
(J
) := S
;
779 Store_Linker_Option_String
(S
);
780 end Replace_Linker_Option_String
;
786 procedure Sort
(Tbl
: in out Unit_Ref_Table
) is separate;
788 ------------------------------
789 -- Store_Compilation_Switch --
790 ------------------------------
792 procedure Store_Compilation_Switch
(Switch
: String) is
794 Compilation_Switches
.Increment_Last
;
795 Compilation_Switches
.Table
(Compilation_Switches
.Last
) :=
797 end Store_Compilation_Switch;
799 --------------------------------
800 -- Store_Linker_Option_String --
801 --------------------------------
803 procedure Store_Linker_Option_String (S : String_Id) is
805 Linker_Option_Lines.Increment_Last;
806 Linker_Option_Lines.Table (Linker_Option_Lines.Last) := S;
807 end Store_Linker_Option_String;
813 procedure Tree_Read is
820 -- Read Compilation_Switches table
823 Compilation_Switches.Set_Last (N);
827 Compilation_Switches.Table (J) := S;
835 procedure Tree_Write is
839 -- Write Compilation_Switches table
841 Tree_Write_Int (Compilation_Switches.Last);
843 for J in 1 .. Compilation_Switches.Last loop
844 Tree_Write_Str (Compilation_Switches.Table (J));
852 function Version_Get (U : Unit_Number_Type) return Word_Hex_String is
854 return Get_Hex_String (Units.Table (U).Version);
857 ------------------------
858 -- Version_Referenced --
859 ------------------------
861 procedure Version_Referenced (S : String_Id) is
863 Version_Ref.Append (S);
864 end Version_Referenced;