OSDN Git Service

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