OSDN Git Service

gcc/ada/
[pf3gnuchains/gcc-fork.git] / gcc / ada / lib.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                                  L I B                                   --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2007, Free Software Foundation, Inc.         --
10 --                                                                          --
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.                                              --
21 --                                                                          --
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.                                      --
28 --                                                                          --
29 -- GNAT was originally developed  by the GNAT team at  New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 pragma Style_Checks (All_Checks);
35 --  Subprogram ordering not enforced in this unit
36 --  (because of some logical groupings).
37
38 with Atree;   use Atree;
39 with Einfo;   use Einfo;
40 with Fname;   use Fname;
41 with Output;  use Output;
42 with Sinfo;   use Sinfo;
43 with Sinput;  use Sinput;
44 with Stand;   use Stand;
45 with Stringt; use Stringt;
46 with Tree_IO; use Tree_IO;
47 with Uname;   use Uname;
48
49 package body Lib is
50
51    Switch_Storing_Enabled : Boolean := True;
52    --  Set to False by Disable_Switch_Storing
53
54    -----------------------
55    -- Local Subprograms --
56    -----------------------
57
58    type SEU_Result is (
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
63
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.
67
68    function Get_Code_Or_Source_Unit
69      (S                : Source_Ptr;
70       Unwind_Instances : Boolean) return Unit_Number_Type;
71    --  Common code for Get_Code_Unit (get unit of instantiation for location)
72    --  and Get_Source_Unit (get unit of template for location).
73
74    --------------------------------------------
75    -- Access Functions for Unit Table Fields --
76    --------------------------------------------
77
78    function Cunit (U : Unit_Number_Type) return Node_Id is
79    begin
80       return Units.Table (U).Cunit;
81    end Cunit;
82
83    function Cunit_Entity (U : Unit_Number_Type) return Entity_Id is
84    begin
85       return Units.Table (U).Cunit_Entity;
86    end Cunit_Entity;
87
88    function Dependency_Num (U : Unit_Number_Type) return Nat is
89    begin
90       return Units.Table (U).Dependency_Num;
91    end Dependency_Num;
92
93    function Dynamic_Elab (U : Unit_Number_Type) return Boolean is
94    begin
95       return Units.Table (U).Dynamic_Elab;
96    end Dynamic_Elab;
97
98    function Error_Location (U : Unit_Number_Type) return Source_Ptr is
99    begin
100       return Units.Table (U).Error_Location;
101    end Error_Location;
102
103    function Expected_Unit (U : Unit_Number_Type) return Unit_Name_Type is
104    begin
105       return Units.Table (U).Expected_Unit;
106    end Expected_Unit;
107
108    function Fatal_Error (U : Unit_Number_Type) return Boolean is
109    begin
110       return Units.Table (U).Fatal_Error;
111    end Fatal_Error;
112
113    function Generate_Code (U : Unit_Number_Type) return Boolean is
114    begin
115       return Units.Table (U).Generate_Code;
116    end Generate_Code;
117
118    function Has_RACW (U : Unit_Number_Type) return Boolean is
119    begin
120       return Units.Table (U).Has_RACW;
121    end Has_RACW;
122
123    function Is_Compiler_Unit (U : Unit_Number_Type) return Boolean is
124    begin
125       return Units.Table (U).Is_Compiler_Unit;
126    end Is_Compiler_Unit;
127
128    function Ident_String (U : Unit_Number_Type) return Node_Id is
129    begin
130       return Units.Table (U).Ident_String;
131    end Ident_String;
132
133    function Loading (U : Unit_Number_Type) return Boolean is
134    begin
135       return Units.Table (U).Loading;
136    end Loading;
137
138    function Main_Priority (U : Unit_Number_Type) return Int is
139    begin
140       return Units.Table (U).Main_Priority;
141    end Main_Priority;
142
143    function Munit_Index (U : Unit_Number_Type) return Nat is
144    begin
145       return Units.Table (U).Munit_Index;
146    end Munit_Index;
147
148    function Source_Index (U : Unit_Number_Type) return Source_File_Index is
149    begin
150       return Units.Table (U).Source_Index;
151    end Source_Index;
152
153    function Unit_File_Name (U : Unit_Number_Type) return File_Name_Type is
154    begin
155       return Units.Table (U).Unit_File_Name;
156    end Unit_File_Name;
157
158    function Unit_Name (U : Unit_Number_Type) return Unit_Name_Type is
159    begin
160       return Units.Table (U).Unit_Name;
161    end Unit_Name;
162
163    ------------------------------------------
164    -- Subprograms to Set Unit Table Fields --
165    ------------------------------------------
166
167    procedure Set_Cunit (U : Unit_Number_Type; N : Node_Id) is
168    begin
169       Units.Table (U).Cunit := N;
170    end Set_Cunit;
171
172    procedure Set_Cunit_Entity (U : Unit_Number_Type; E : Entity_Id) is
173    begin
174       Units.Table (U).Cunit_Entity := E;
175       Set_Is_Compilation_Unit (E);
176    end Set_Cunit_Entity;
177
178    procedure Set_Dynamic_Elab (U : Unit_Number_Type; B : Boolean := True) is
179    begin
180       Units.Table (U).Dynamic_Elab := B;
181    end Set_Dynamic_Elab;
182
183    procedure Set_Error_Location (U : Unit_Number_Type; W : Source_Ptr) is
184    begin
185       Units.Table (U).Error_Location := W;
186    end Set_Error_Location;
187
188    procedure Set_Fatal_Error (U : Unit_Number_Type; B : Boolean := True) is
189    begin
190       Units.Table (U).Fatal_Error := B;
191    end Set_Fatal_Error;
192
193    procedure Set_Generate_Code (U : Unit_Number_Type; B : Boolean := True) is
194    begin
195       Units.Table (U).Generate_Code := B;
196    end Set_Generate_Code;
197
198    procedure Set_Has_RACW (U : Unit_Number_Type; B : Boolean := True) is
199    begin
200       Units.Table (U).Has_RACW := B;
201    end Set_Has_RACW;
202
203    procedure Set_Is_Compiler_Unit
204      (U : Unit_Number_Type;
205       B : Boolean := True)
206    is
207    begin
208       Units.Table (U).Is_Compiler_Unit := B;
209    end Set_Is_Compiler_Unit;
210
211    procedure Set_Ident_String (U : Unit_Number_Type; N : Node_Id) is
212    begin
213       Units.Table (U).Ident_String := N;
214    end Set_Ident_String;
215
216    procedure Set_Loading (U : Unit_Number_Type; B : Boolean := True) is
217    begin
218       Units.Table (U).Loading := B;
219    end Set_Loading;
220
221    procedure Set_Main_Priority (U : Unit_Number_Type; P : Int) is
222    begin
223       Units.Table (U).Main_Priority := P;
224    end Set_Main_Priority;
225
226    procedure Set_Unit_Name (U : Unit_Number_Type; N : Unit_Name_Type) is
227    begin
228       Units.Table (U).Unit_Name := N;
229    end Set_Unit_Name;
230
231    ------------------------------
232    -- Check_Same_Extended_Unit --
233    ------------------------------
234
235    function Check_Same_Extended_Unit (S1, S2 : Source_Ptr) return SEU_Result is
236       Sloc1  : Source_Ptr;
237       Sloc2  : Source_Ptr;
238       Sind1  : Source_File_Index;
239       Sind2  : Source_File_Index;
240       Inst1  : Source_Ptr;
241       Inst2  : Source_Ptr;
242       Unum1  : Unit_Number_Type;
243       Unum2  : Unit_Number_Type;
244       Unit1  : Node_Id;
245       Unit2  : Node_Id;
246       Depth1 : Nat;
247       Depth2 : Nat;
248
249    begin
250       if S1 = No_Location or else S2 = No_Location then
251          return No;
252
253       elsif S1 = Standard_Location then
254          if S2 = Standard_Location then
255             return Yes_Same;
256          else
257             return No;
258          end if;
259
260       elsif S2 = Standard_Location then
261          return No;
262       end if;
263
264       Sloc1 := S1;
265       Sloc2 := S2;
266       Unum1 := Get_Code_Unit (Sloc1);
267       Unum2 := Get_Code_Unit (Sloc2);
268
269       loop
270          Sind1 := Get_Source_File_Index (Sloc1);
271          Sind2 := Get_Source_File_Index (Sloc2);
272
273          if Sind1 = Sind2 then
274             if Sloc1 < Sloc2 then
275                return Yes_Before;
276             elsif Sloc1 > Sloc2 then
277                return Yes_After;
278             else
279                return Yes_Same;
280             end if;
281          end if;
282
283          --  OK, the two nodes are in separate source elements, but this is not
284          --  decisive, because of the issue of subunits and instantiations.
285
286          --  First we deal with subunits, since if the subunit is in an
287          --  instantiation, we know that the parent is in the corresponding
288          --  instantiation, since that is the only way we can have a subunit
289          --  that is part of an instantiation.
290
291          Unit1 := Unit (Cunit (Unum1));
292          Unit2 := Unit (Cunit (Unum2));
293
294          if Nkind (Unit1) = N_Subunit
295            and then Present (Corresponding_Stub (Unit1))
296          then
297             --  Both in subunits. They could have a common ancestor. If they
298             --  do, then the deeper one must have a longer unit name. Replace
299             --  the deeper one with its corresponding stub, in order to find
300             --  nearest common ancestor, if any.
301
302             if Nkind (Unit2) = N_Subunit
303               and then Present (Corresponding_Stub (Unit2))
304             then
305                if Length_Of_Name (Unit_Name (Unum1)) <
306                   Length_Of_Name (Unit_Name (Unum2))
307                then
308                   Sloc2 := Sloc (Corresponding_Stub (Unit2));
309                   Unum2 := Get_Source_Unit (Sloc2);
310                   goto Continue;
311
312                else
313                   Sloc1 := Sloc (Corresponding_Stub (Unit1));
314                   Unum1 := Get_Source_Unit (Sloc1);
315                   goto Continue;
316                end if;
317
318             --  Nod1 in subunit, Nod2 not
319
320             else
321                Sloc1 := Sloc (Corresponding_Stub (Unit1));
322                Unum1 := Get_Source_Unit (Sloc1);
323                goto Continue;
324             end if;
325
326          --  Nod2 in subunit, Nod1 not
327
328          elsif Nkind (Unit2) = N_Subunit
329            and then Present (Corresponding_Stub (Unit2))
330          then
331             Sloc2 := Sloc (Corresponding_Stub (Unit2));
332             Unum2 := Get_Source_Unit (Sloc2);
333             goto Continue;
334          end if;
335
336          --  At this stage we know that neither is a subunit, so we deal
337          --  with instantiations, since we could have a common ancestor
338
339          Inst1 := Instantiation (Sind1);
340          Inst2 := Instantiation (Sind2);
341
342          if Inst1 /= No_Location then
343
344             --  Both are instantiations
345
346             if Inst2 /= No_Location then
347
348                Depth1 := Instantiation_Depth (Sloc1);
349                Depth2 := Instantiation_Depth (Sloc2);
350
351                if Depth1 < Depth2 then
352                   Sloc2 := Inst2;
353                   Unum2 := Get_Source_Unit (Sloc2);
354                   goto Continue;
355
356                elsif Depth1 > Depth2 then
357                   Sloc1 := Inst1;
358                   Unum1 := Get_Source_Unit (Sloc1);
359                   goto Continue;
360
361                else
362                   Sloc1 := Inst1;
363                   Sloc2 := Inst2;
364                   Unum1 := Get_Source_Unit (Sloc1);
365                   Unum2 := Get_Source_Unit (Sloc2);
366                   goto Continue;
367                end if;
368
369             --  Only first node is in instantiation
370
371             else
372                Sloc1 := Inst1;
373                Unum1 := Get_Source_Unit (Sloc1);
374                goto Continue;
375             end if;
376
377          --  Only second node is instantiation
378
379          elsif Inst2 /= No_Location then
380             Sloc2 := Inst2;
381             Unum2 := Get_Source_Unit (Sloc2);
382             goto Continue;
383          end if;
384
385          --  No instantiations involved, so we are not in the same unit
386          --  However, there is one case still to check, namely the case
387          --  where one location is in the spec, and the other in the
388          --  corresponding body (the spec location is earlier).
389
390          if Nkind (Unit1) = N_Subprogram_Body
391               or else
392             Nkind (Unit1) = N_Package_Body
393          then
394             if Library_Unit (Cunit (Unum1)) = Cunit (Unum2) then
395                return Yes_After;
396             end if;
397
398          elsif Nkind (Unit2) = N_Subprogram_Body
399                  or else
400                Nkind (Unit2) = N_Package_Body
401          then
402             if Library_Unit (Cunit (Unum2)) = Cunit (Unum1) then
403                return Yes_Before;
404             end if;
405          end if;
406
407          --  If that special case does not occur, then we are certain that
408          --  the two locations are really in separate units.
409
410          return No;
411
412          <<Continue>>
413             null;
414       end loop;
415    end Check_Same_Extended_Unit;
416
417    -------------------------------
418    -- Compilation_Switches_Last --
419    -------------------------------
420
421    function Compilation_Switches_Last return Nat is
422    begin
423       return Compilation_Switches.Last;
424    end Compilation_Switches_Last;
425
426    procedure Disable_Switch_Storing is
427    begin
428       Switch_Storing_Enabled := False;
429    end Disable_Switch_Storing;
430
431    ------------------------------
432    -- Earlier_In_Extended_Unit --
433    ------------------------------
434
435    function Earlier_In_Extended_Unit (S1, S2 : Source_Ptr) return Boolean is
436    begin
437       return Check_Same_Extended_Unit (S1, S2) = Yes_Before;
438    end Earlier_In_Extended_Unit;
439
440    ----------------------------
441    -- Entity_Is_In_Main_Unit --
442    ----------------------------
443
444    function Entity_Is_In_Main_Unit (E : Entity_Id) return Boolean is
445       S : Entity_Id;
446
447    begin
448       S := Scope (E);
449
450       while S /= Standard_Standard loop
451          if S = Main_Unit_Entity then
452             return True;
453          elsif Ekind (S) = E_Package and then Is_Child_Unit (S) then
454             return False;
455          else
456             S := Scope (S);
457          end if;
458       end loop;
459
460       return False;
461    end Entity_Is_In_Main_Unit;
462
463    --------------------------
464    -- Generic_May_Lack_ALI --
465    --------------------------
466
467    function Generic_May_Lack_ALI (Sfile : File_Name_Type) return Boolean is
468    begin
469       --  We allow internal generic units to be used without having a
470       --  corresponding ALI files to help bootstrapping with older compilers
471       --  that did not support generating ALIs for such generics. It is safe
472       --  to do so because the only thing the generated code would contain
473       --  is the elaboration boolean, and we are careful to elaborate all
474       --  predefined units first anyway.
475
476       return Is_Internal_File_Name
477                (Fname              => Sfile,
478                 Renamings_Included => True);
479    end Generic_May_Lack_ALI;
480
481    -----------------------------
482    -- Get_Code_Or_Source_Unit --
483    -----------------------------
484
485    function Get_Code_Or_Source_Unit
486      (S                : Source_Ptr;
487       Unwind_Instances : Boolean) return Unit_Number_Type
488    is
489    begin
490       --  Search table unless we have No_Location, which can happen if the
491       --  relevant location has not been set yet. Happens for example when
492       --  we obtain Sloc (Cunit (Main_Unit)) before it is set.
493
494       if S /= No_Location then
495          declare
496             Source_File : Source_File_Index;
497             Source_Unit : Unit_Number_Type;
498
499          begin
500             Source_File := Get_Source_File_Index (S);
501
502             if Unwind_Instances then
503                while Template (Source_File) /= No_Source_File loop
504                   Source_File := Template (Source_File);
505                end loop;
506             end if;
507
508             Source_Unit := Unit (Source_File);
509
510             if Source_Unit /= No_Unit then
511                return Source_Unit;
512             end if;
513          end;
514       end if;
515
516       --  If S was No_Location, or was not in the table, we must be in the main
517       --  source unit (and the value has not been placed in the table yet),
518       --  or in one of the configuration pragma files.
519
520       return Main_Unit;
521    end Get_Code_Or_Source_Unit;
522
523    -------------------
524    -- Get_Code_Unit --
525    -------------------
526
527    function Get_Code_Unit (S : Source_Ptr) return Unit_Number_Type is
528    begin
529       return Get_Code_Or_Source_Unit (Top_Level_Location (S),
530         Unwind_Instances => False);
531    end Get_Code_Unit;
532
533    function Get_Code_Unit (N : Node_Or_Entity_Id) return Unit_Number_Type is
534    begin
535       return Get_Code_Unit (Sloc (N));
536    end Get_Code_Unit;
537
538    ----------------------------
539    -- Get_Compilation_Switch --
540    ----------------------------
541
542    function Get_Compilation_Switch (N : Pos) return String_Ptr is
543    begin
544       if N <= Compilation_Switches.Last then
545          return Compilation_Switches.Table (N);
546
547       else
548          return null;
549       end if;
550    end Get_Compilation_Switch;
551
552    ----------------------------------
553    -- Get_Cunit_Entity_Unit_Number --
554    ----------------------------------
555
556    function Get_Cunit_Entity_Unit_Number
557      (E : Entity_Id) return Unit_Number_Type
558    is
559    begin
560       for U in Units.First .. Units.Last loop
561          if Cunit_Entity (U) = E then
562             return U;
563          end if;
564       end loop;
565
566       --  If not in the table, must be the main source unit, and we just
567       --  have not got it put into the table yet.
568
569       return Main_Unit;
570    end Get_Cunit_Entity_Unit_Number;
571
572    ---------------------------
573    -- Get_Cunit_Unit_Number --
574    ---------------------------
575
576    function Get_Cunit_Unit_Number (N : Node_Id) return Unit_Number_Type is
577    begin
578       for U in Units.First .. Units.Last loop
579          if Cunit (U) = N then
580             return U;
581          end if;
582       end loop;
583
584       --  If not in the table, must be the main source unit, and we just
585       --  have not got it put into the table yet.
586
587       return Main_Unit;
588    end Get_Cunit_Unit_Number;
589
590    ---------------------
591    -- Get_Source_Unit --
592    ---------------------
593
594    function Get_Source_Unit (S : Source_Ptr) return Unit_Number_Type is
595    begin
596       return Get_Code_Or_Source_Unit (S, Unwind_Instances => True);
597    end Get_Source_Unit;
598
599    function Get_Source_Unit (N : Node_Or_Entity_Id) return Unit_Number_Type is
600    begin
601       return Get_Source_Unit (Sloc (N));
602    end Get_Source_Unit;
603
604    --------------------------------
605    -- In_Extended_Main_Code_Unit --
606    --------------------------------
607
608    function In_Extended_Main_Code_Unit
609      (N : Node_Or_Entity_Id) return Boolean
610    is
611    begin
612       if Sloc (N) = Standard_Location then
613          return True;
614
615       elsif Sloc (N) = No_Location then
616          return False;
617
618       --  Special case Itypes to test the Sloc of the associated node. The
619       --  reason we do this is for possible calls from gigi after -gnatD
620       --  processing is complete in sprint. This processing updates the
621       --  sloc fields of all nodes in the tree, but itypes are not in the
622       --  tree so their slocs do not get updated.
623
624       elsif Nkind (N) = N_Defining_Identifier
625         and then Is_Itype (N)
626       then
627          return In_Extended_Main_Code_Unit (Associated_Node_For_Itype (N));
628
629       --  Otherwise see if we are in the main unit
630
631       elsif Get_Code_Unit (Sloc (N)) = Get_Code_Unit (Cunit (Main_Unit)) then
632          return True;
633
634       --  Node may be in spec (or subunit etc) of main unit
635
636       else
637          return
638            In_Same_Extended_Unit (N, Cunit (Main_Unit));
639       end if;
640    end In_Extended_Main_Code_Unit;
641
642    function In_Extended_Main_Code_Unit (Loc : Source_Ptr) return Boolean is
643    begin
644       if Loc = Standard_Location then
645          return True;
646
647       elsif Loc = No_Location then
648          return False;
649
650       --  Otherwise see if we are in the main unit
651
652       elsif Get_Code_Unit (Loc) = Get_Code_Unit (Cunit (Main_Unit)) then
653          return True;
654
655       --  Location may be in spec (or subunit etc) of main unit
656
657       else
658          return
659            In_Same_Extended_Unit (Loc, Sloc (Cunit (Main_Unit)));
660       end if;
661    end In_Extended_Main_Code_Unit;
662
663    ----------------------------------
664    -- In_Extended_Main_Source_Unit --
665    ----------------------------------
666
667    function In_Extended_Main_Source_Unit
668      (N : Node_Or_Entity_Id) return Boolean
669    is
670       Nloc : constant Source_Ptr := Sloc (N);
671       Mloc : constant Source_Ptr := Sloc (Cunit (Main_Unit));
672
673    begin
674       --  If Mloc is not set, it means we are still parsing the main unit,
675       --  so everything so far is in the extended main source unit.
676
677       if Mloc = No_Location then
678          return True;
679
680       --  Special value cases
681
682       elsif Nloc = Standard_Location then
683          return True;
684
685       elsif Nloc = No_Location then
686          return False;
687
688       --  Special case Itypes to test the Sloc of the associated node. The
689       --  reason we do this is for possible calls from gigi after -gnatD
690       --  processing is complete in sprint. This processing updates the
691       --  sloc fields of all nodes in the tree, but itypes are not in the
692       --  tree so their slocs do not get updated.
693
694       elsif Nkind (N) = N_Defining_Identifier
695         and then Is_Itype (N)
696       then
697          return In_Extended_Main_Source_Unit (Associated_Node_For_Itype (N));
698
699       --  Otherwise compare original locations to see if in same unit
700
701       else
702          return
703            In_Same_Extended_Unit
704              (Original_Location (Nloc), Original_Location (Mloc));
705       end if;
706    end In_Extended_Main_Source_Unit;
707
708    function In_Extended_Main_Source_Unit
709      (Loc : Source_Ptr) return Boolean
710    is
711       Mloc : constant Source_Ptr := Sloc (Cunit (Main_Unit));
712
713    begin
714       --  If Mloc is not set, it means we are still parsing the main unit,
715       --  so everything so far is in the extended main source unit.
716
717       if Mloc = No_Location then
718          return True;
719
720       --  Special value cases
721
722       elsif Loc = Standard_Location then
723          return True;
724
725       elsif Loc = No_Location then
726          return False;
727
728       --  Otherwise compare original locations to see if in same unit
729
730       else
731          return
732            In_Same_Extended_Unit
733              (Original_Location (Loc), Original_Location (Mloc));
734       end if;
735    end In_Extended_Main_Source_Unit;
736
737    ------------------------
738    -- In_Predefined_Unit --
739    ------------------------
740
741    function In_Predefined_Unit (N : Node_Or_Entity_Id) return Boolean is
742    begin
743       return In_Predefined_Unit (Sloc (N));
744    end In_Predefined_Unit;
745
746    function In_Predefined_Unit (S : Source_Ptr) return Boolean is
747       Unit : constant Unit_Number_Type := Get_Source_Unit (S);
748       File : constant File_Name_Type   := Unit_File_Name (Unit);
749    begin
750       return Is_Predefined_File_Name (File);
751    end In_Predefined_Unit;
752
753    -----------------------
754    -- In_Same_Code_Unit --
755    -----------------------
756
757    function In_Same_Code_Unit (N1, N2 : Node_Or_Entity_Id) return Boolean is
758       S1 : constant Source_Ptr := Sloc (N1);
759       S2 : constant Source_Ptr := Sloc (N2);
760
761    begin
762       if S1 = No_Location or else S2 = No_Location then
763          return False;
764
765       elsif S1 = Standard_Location then
766          return S2 = Standard_Location;
767
768       elsif S2 = Standard_Location then
769          return False;
770       end if;
771
772       return Get_Code_Unit (N1) = Get_Code_Unit (N2);
773    end In_Same_Code_Unit;
774
775    ---------------------------
776    -- In_Same_Extended_Unit --
777    ---------------------------
778
779    function In_Same_Extended_Unit
780      (N1, N2 : Node_Or_Entity_Id) return Boolean
781    is
782    begin
783       return Check_Same_Extended_Unit (Sloc (N1), Sloc (N2)) /= No;
784    end In_Same_Extended_Unit;
785
786    function In_Same_Extended_Unit (S1, S2 : Source_Ptr) return Boolean is
787    begin
788       return Check_Same_Extended_Unit (S1, S2) /= No;
789    end In_Same_Extended_Unit;
790
791    -------------------------
792    -- In_Same_Source_Unit --
793    -------------------------
794
795    function In_Same_Source_Unit (N1, N2 : Node_Or_Entity_Id) return Boolean is
796       S1 : constant Source_Ptr := Sloc (N1);
797       S2 : constant Source_Ptr := Sloc (N2);
798
799    begin
800       if S1 = No_Location or else S2 = No_Location then
801          return False;
802
803       elsif S1 = Standard_Location then
804          return S2 = Standard_Location;
805
806       elsif S2 = Standard_Location then
807          return False;
808       end if;
809
810       return Get_Source_Unit (N1) = Get_Source_Unit (N2);
811    end In_Same_Source_Unit;
812
813    -----------------------------
814    -- Increment_Serial_Number --
815    -----------------------------
816
817    function Increment_Serial_Number return Nat is
818       TSN : Int renames Units.Table (Current_Sem_Unit).Serial_Number;
819    begin
820       TSN := TSN + 1;
821       return TSN;
822    end Increment_Serial_Number;
823
824    ----------------
825    -- Initialize --
826    ----------------
827
828    procedure Initialize is
829    begin
830       Linker_Option_Lines.Init;
831       Load_Stack.Init;
832       Units.Init;
833       Compilation_Switches.Init;
834    end Initialize;
835
836    ---------------
837    -- Is_Loaded --
838    ---------------
839
840    function Is_Loaded (Uname : Unit_Name_Type) return Boolean is
841    begin
842       for Unum in Units.First .. Units.Last loop
843          if Uname = Unit_Name (Unum) then
844             return True;
845          end if;
846       end loop;
847
848       return False;
849    end Is_Loaded;
850
851    ---------------
852    -- Last_Unit --
853    ---------------
854
855    function Last_Unit return Unit_Number_Type is
856    begin
857       return Units.Last;
858    end Last_Unit;
859
860    ----------
861    -- List --
862    ----------
863
864    procedure List (File_Names_Only : Boolean := False) is separate;
865
866    ----------
867    -- Lock --
868    ----------
869
870    procedure Lock is
871    begin
872       Linker_Option_Lines.Locked := True;
873       Load_Stack.Locked := True;
874       Units.Locked := True;
875       Linker_Option_Lines.Release;
876       Load_Stack.Release;
877       Units.Release;
878    end Lock;
879
880    ---------------
881    -- Num_Units --
882    ---------------
883
884    function Num_Units return Nat is
885    begin
886       return Int (Units.Last) - Int (Main_Unit) + 1;
887    end Num_Units;
888
889    -----------------
890    -- Remove_Unit --
891    -----------------
892
893    procedure Remove_Unit (U : Unit_Number_Type) is
894    begin
895       if U = Units.Last then
896          Units.Decrement_Last;
897       end if;
898    end Remove_Unit;
899
900    ----------------------------------
901    -- Replace_Linker_Option_String --
902    ----------------------------------
903
904    procedure Replace_Linker_Option_String
905      (S : String_Id; Match_String : String)
906    is
907    begin
908       if Match_String'Length > 0 then
909          for J in 1 .. Linker_Option_Lines.Last loop
910             String_To_Name_Buffer (Linker_Option_Lines.Table (J).Option);
911
912             if Match_String = Name_Buffer (1 .. Match_String'Length) then
913                Linker_Option_Lines.Table (J).Option := S;
914                return;
915             end if;
916          end loop;
917       end if;
918
919       Store_Linker_Option_String (S);
920    end Replace_Linker_Option_String;
921
922    ----------
923    -- Sort --
924    ----------
925
926    procedure Sort (Tbl : in out Unit_Ref_Table) is separate;
927
928    ------------------------------
929    -- Store_Compilation_Switch --
930    ------------------------------
931
932    procedure Store_Compilation_Switch (Switch : String) is
933    begin
934       if Switch_Storing_Enabled then
935          Compilation_Switches.Increment_Last;
936          Compilation_Switches.Table (Compilation_Switches.Last) :=
937            new String'(Switch);
938
939          --  Fix up --RTS flag which has been transformed by the gcc driver
940          --  into -fRTS
941
942          if Switch'Last >= Switch'First + 4
943            and then Switch (Switch'First .. Switch'First + 4) = "-fRTS"
944          then
945             Compilation_Switches.Table
946               (Compilation_Switches.Last) (Switch'First + 1) := '-';
947          end if;
948       end if;
949    end Store_Compilation_Switch;
950
951    --------------------------------
952    -- Store_Linker_Option_String --
953    --------------------------------
954
955    procedure Store_Linker_Option_String (S : String_Id) is
956    begin
957       Linker_Option_Lines.Increment_Last;
958       Linker_Option_Lines.Table (Linker_Option_Lines.Last) :=
959         (Option => S, Unit => Current_Sem_Unit);
960    end Store_Linker_Option_String;
961
962    -------------------------------
963    -- Synchronize_Serial_Number --
964    -------------------------------
965
966    procedure Synchronize_Serial_Number is
967       TSN : Int renames Units.Table (Current_Sem_Unit).Serial_Number;
968    begin
969       TSN := TSN + 1;
970    end Synchronize_Serial_Number;
971
972    ---------------
973    -- Tree_Read --
974    ---------------
975
976    procedure Tree_Read is
977       N : Nat;
978       S : String_Ptr;
979
980    begin
981       Units.Tree_Read;
982
983       --  Read Compilation_Switches table. First release the memory occupied
984       --  by the previously loaded switches.
985
986       for J in Compilation_Switches.First .. Compilation_Switches.Last loop
987          Free (Compilation_Switches.Table (J));
988       end loop;
989
990       Tree_Read_Int (N);
991       Compilation_Switches.Set_Last (N);
992
993       for J in 1 .. N loop
994          Tree_Read_Str (S);
995          Compilation_Switches.Table (J) := S;
996       end loop;
997    end Tree_Read;
998
999    ----------------
1000    -- Tree_Write --
1001    ----------------
1002
1003    procedure Tree_Write is
1004    begin
1005       Units.Tree_Write;
1006
1007       --  Write Compilation_Switches table
1008
1009       Tree_Write_Int (Compilation_Switches.Last);
1010
1011       for J in 1 .. Compilation_Switches.Last loop
1012          Tree_Write_Str (Compilation_Switches.Table (J));
1013       end loop;
1014    end Tree_Write;
1015
1016    ------------
1017    -- Unlock --
1018    ------------
1019
1020    procedure Unlock is
1021    begin
1022       Linker_Option_Lines.Locked := False;
1023       Load_Stack.Locked := False;
1024       Units.Locked := False;
1025    end Unlock;
1026
1027    -----------------
1028    -- Version_Get --
1029    -----------------
1030
1031    function Version_Get (U : Unit_Number_Type) return Word_Hex_String is
1032    begin
1033       return Get_Hex_String (Units.Table (U).Version);
1034    end Version_Get;
1035
1036    ------------------------
1037    -- Version_Referenced --
1038    ------------------------
1039
1040    procedure Version_Referenced (S : String_Id) is
1041    begin
1042       Version_Ref.Append (S);
1043    end Version_Referenced;
1044
1045 end Lib;