OSDN Git Service

optimize
[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-2004 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,  59 Temple Place - Suite 330,  Boston, --
20 -- MA 02111-1307, 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 Namet;   use Namet;
42 with Namet;   use Namet;
43 with Output;  use Output;
44 with Sinfo;   use Sinfo;
45 with Sinput;  use Sinput;
46 with Stand;   use Stand;
47 with Stringt; use Stringt;
48 with Tree_IO; use Tree_IO;
49 with Uname;   use Uname;
50
51 package body Lib is
52
53    Switch_Storing_Enabled : Boolean := True;
54    --  Set to False by Disable_Switch_Storing
55
56    -----------------------
57    -- Local Subprograms --
58    -----------------------
59
60    type SEU_Result is (
61       Yes_Before, -- S1 is in same extended unit as S2 and appears before it
62       Yes_Same,   -- S1 is in same extended unit as S2, Slocs are the same
63       Yes_After,  -- S1 is in same extended unit as S2, and appears after it
64       No);        -- S2 is not in same extended unit as S2
65
66    function Check_Same_Extended_Unit (S1, S2 : Source_Ptr) return SEU_Result;
67    --  Used by In_Same_Extended_Unit and Earlier_In_Extended_Unit. Returns
68    --  value as described above.
69
70    --------------------------------------------
71    -- Access Functions for Unit Table Fields --
72    --------------------------------------------
73
74    function Cunit (U : Unit_Number_Type) return Node_Id is
75    begin
76       return Units.Table (U).Cunit;
77    end Cunit;
78
79    function Cunit_Entity (U : Unit_Number_Type) return Entity_Id is
80    begin
81       return Units.Table (U).Cunit_Entity;
82    end Cunit_Entity;
83
84    function Dependency_Num (U : Unit_Number_Type) return Nat is
85    begin
86       return Units.Table (U).Dependency_Num;
87    end Dependency_Num;
88
89    function Dynamic_Elab (U : Unit_Number_Type) return Boolean is
90    begin
91       return Units.Table (U).Dynamic_Elab;
92    end Dynamic_Elab;
93
94    function Error_Location (U : Unit_Number_Type) return Source_Ptr is
95    begin
96       return Units.Table (U).Error_Location;
97    end Error_Location;
98
99    function Expected_Unit (U : Unit_Number_Type) return Unit_Name_Type is
100    begin
101       return Units.Table (U).Expected_Unit;
102    end Expected_Unit;
103
104    function Fatal_Error (U : Unit_Number_Type) return Boolean is
105    begin
106       return Units.Table (U).Fatal_Error;
107    end Fatal_Error;
108
109    function Generate_Code (U : Unit_Number_Type) return Boolean is
110    begin
111       return Units.Table (U).Generate_Code;
112    end Generate_Code;
113
114    function Has_RACW (U : Unit_Number_Type) return Boolean is
115    begin
116       return Units.Table (U).Has_RACW;
117    end Has_RACW;
118
119    function Ident_String (U : Unit_Number_Type) return Node_Id is
120    begin
121       return Units.Table (U).Ident_String;
122    end Ident_String;
123
124    function Loading (U : Unit_Number_Type) return Boolean is
125    begin
126       return Units.Table (U).Loading;
127    end Loading;
128
129    function Main_Priority (U : Unit_Number_Type) return Int is
130    begin
131       return Units.Table (U).Main_Priority;
132    end Main_Priority;
133
134    function Munit_Index (U : Unit_Number_Type) return Nat is
135    begin
136       return Units.Table (U).Munit_Index;
137    end Munit_Index;
138
139    function Source_Index (U : Unit_Number_Type) return Source_File_Index is
140    begin
141       return Units.Table (U).Source_Index;
142    end Source_Index;
143
144    function Unit_File_Name (U : Unit_Number_Type) return File_Name_Type is
145    begin
146       return Units.Table (U).Unit_File_Name;
147    end Unit_File_Name;
148
149    function Unit_Name (U : Unit_Number_Type) return Unit_Name_Type is
150    begin
151       return Units.Table (U).Unit_Name;
152    end Unit_Name;
153
154    ------------------------------------------
155    -- Subprograms to Set Unit Table Fields --
156    ------------------------------------------
157
158    procedure Set_Cunit (U : Unit_Number_Type; N : Node_Id) is
159    begin
160       Units.Table (U).Cunit := N;
161    end Set_Cunit;
162
163    procedure Set_Cunit_Entity (U : Unit_Number_Type; E : Entity_Id) is
164    begin
165       Units.Table (U).Cunit_Entity := E;
166       Set_Is_Compilation_Unit (E);
167    end Set_Cunit_Entity;
168
169    procedure Set_Dynamic_Elab (U : Unit_Number_Type; B : Boolean := True) is
170    begin
171       Units.Table (U).Dynamic_Elab := B;
172    end Set_Dynamic_Elab;
173
174    procedure Set_Error_Location (U : Unit_Number_Type; W : Source_Ptr) is
175    begin
176       Units.Table (U).Error_Location := W;
177    end Set_Error_Location;
178
179    procedure Set_Fatal_Error (U : Unit_Number_Type; B : Boolean := True) is
180    begin
181       Units.Table (U).Fatal_Error := B;
182    end Set_Fatal_Error;
183
184    procedure Set_Generate_Code (U : Unit_Number_Type; B : Boolean := True) is
185    begin
186       Units.Table (U).Generate_Code := B;
187    end Set_Generate_Code;
188
189    procedure Set_Has_RACW (U : Unit_Number_Type; B : Boolean := True) is
190    begin
191       Units.Table (U).Has_RACW := B;
192    end Set_Has_RACW;
193
194    procedure Set_Ident_String (U : Unit_Number_Type; N : Node_Id) is
195    begin
196       Units.Table (U).Ident_String := N;
197    end Set_Ident_String;
198
199    procedure Set_Loading (U : Unit_Number_Type; B : Boolean := True) is
200    begin
201       Units.Table (U).Loading := B;
202    end Set_Loading;
203
204    procedure Set_Main_Priority (U : Unit_Number_Type; P : Int) is
205    begin
206       Units.Table (U).Main_Priority := P;
207    end Set_Main_Priority;
208
209    procedure Set_Unit_Name (U : Unit_Number_Type; N : Unit_Name_Type) is
210    begin
211       Units.Table (U).Unit_Name := N;
212    end Set_Unit_Name;
213
214    ------------------------------
215    -- Check_Same_Extended_Unit --
216    ------------------------------
217
218    function Check_Same_Extended_Unit (S1, S2 : Source_Ptr) return SEU_Result is
219       Sloc1  : Source_Ptr;
220       Sloc2  : Source_Ptr;
221       Sind1  : Source_File_Index;
222       Sind2  : Source_File_Index;
223       Inst1  : Source_Ptr;
224       Inst2  : Source_Ptr;
225       Unum1  : Unit_Number_Type;
226       Unum2  : Unit_Number_Type;
227       Unit1  : Node_Id;
228       Unit2  : Node_Id;
229       Depth1 : Nat;
230       Depth2 : Nat;
231
232    begin
233       if S1 = No_Location or else S2 = No_Location then
234          return No;
235
236       elsif S1 = Standard_Location then
237          if S2 = Standard_Location then
238             return Yes_Same;
239          else
240             return No;
241          end if;
242
243       elsif S2 = Standard_Location then
244          return No;
245       end if;
246
247       Sloc1 := S1;
248       Sloc2 := S2;
249       Unum1 := Get_Code_Unit (Sloc1);
250       Unum2 := Get_Code_Unit (Sloc2);
251
252       loop
253          Sind1 := Get_Source_File_Index (Sloc1);
254          Sind2 := Get_Source_File_Index (Sloc2);
255
256          if Sind1 = Sind2 then
257             if Sloc1 < Sloc2 then
258                return Yes_Before;
259             elsif Sloc1 > Sloc2 then
260                return Yes_After;
261             else
262                return Yes_Same;
263             end if;
264          end if;
265
266          --  OK, the two nodes are in separate source elements, but this is not
267          --  decisive, because of the issue of subunits and instantiations.
268
269          --  First we deal with subunits, since if the subunit is in an
270          --  instantiation, we know that the parent is in the corresponding
271          --  instantiation, since that is the only way we can have a subunit
272          --  that is part of an instantiation.
273
274          Unit1 := Unit (Cunit (Unum1));
275          Unit2 := Unit (Cunit (Unum2));
276
277          if Nkind (Unit1) = N_Subunit
278            and then Present (Corresponding_Stub (Unit1))
279          then
280             --  Both in subunits. They could have a common ancestor. If they
281             --  do, then the deeper one must have a longer unit name. Replace
282             --  the deeper one with its corresponding stub, in order to find
283             --  nearest common ancestor, if any.
284
285             if Nkind (Unit2) = N_Subunit
286               and then Present (Corresponding_Stub (Unit2))
287             then
288                if Length_Of_Name (Unit_Name (Unum1)) <
289                   Length_Of_Name (Unit_Name (Unum2))
290                then
291                   Sloc2 := Sloc (Corresponding_Stub (Unit2));
292                   Unum2 := Get_Source_Unit (Sloc2);
293                   goto Continue;
294
295                else
296                   Sloc1 := Sloc (Corresponding_Stub (Unit1));
297                   Unum1 := Get_Source_Unit (Sloc1);
298                   goto Continue;
299                end if;
300
301             --  Nod1 in subunit, Nod2 not
302
303             else
304                Sloc1 := Sloc (Corresponding_Stub (Unit1));
305                Unum1 := Get_Source_Unit (Sloc1);
306                goto Continue;
307             end if;
308
309          --  Nod2 in subunit, Nod1 not
310
311          elsif Nkind (Unit2) = N_Subunit
312            and then Present (Corresponding_Stub (Unit2))
313          then
314             Sloc2 := Sloc (Corresponding_Stub (Unit2));
315             Unum2 := Get_Source_Unit (Sloc2);
316             goto Continue;
317          end if;
318
319          --  At this stage we know that neither is a subunit, so we deal
320          --  with instantiations, since we culd have a common ancestor
321
322          Inst1 := Instantiation (Sind1);
323          Inst2 := Instantiation (Sind2);
324
325          if Inst1 /= No_Location then
326
327             --  Both are instantiations
328
329             if Inst2 /= No_Location then
330
331                Depth1 := Instantiation_Depth (Sloc1);
332                Depth2 := Instantiation_Depth (Sloc2);
333
334                if Depth1 < Depth2 then
335                   Sloc2 := Inst2;
336                   Unum2 := Get_Source_Unit (Sloc2);
337                   goto Continue;
338
339                elsif Depth1 > Depth2 then
340                   Sloc1 := Inst1;
341                   Unum1 := Get_Source_Unit (Sloc1);
342                   goto Continue;
343
344                else
345                   Sloc1 := Inst1;
346                   Sloc2 := Inst2;
347                   Unum1 := Get_Source_Unit (Sloc1);
348                   Unum2 := Get_Source_Unit (Sloc2);
349                   goto Continue;
350                end if;
351
352             --  Only first node is in instantiation
353
354             else
355                Sloc1 := Inst1;
356                Unum1 := Get_Source_Unit (Sloc1);
357                goto Continue;
358             end if;
359
360          --  Only second node is instantiation
361
362          elsif Inst2 /= No_Location then
363             Sloc2 := Inst2;
364             Unum2 := Get_Source_Unit (Sloc2);
365             goto Continue;
366          end if;
367
368          --  No instantiations involved, so we are not in the same unit
369          --  However, there is one case still to check, namely the case
370          --  where one location is in the spec, and the other in the
371          --  corresponding body (the spec location is earlier).
372
373          if Nkind (Unit1) = N_Subprogram_Body
374               or else
375             Nkind (Unit1) = N_Package_Body
376          then
377             if Library_Unit (Cunit (Unum1)) = Cunit (Unum2) then
378                return Yes_After;
379             end if;
380
381          elsif Nkind (Unit2) = N_Subprogram_Body
382                  or else
383                Nkind (Unit2) = N_Package_Body
384          then
385             if Library_Unit (Cunit (Unum2)) = Cunit (Unum1) then
386                return Yes_Before;
387             end if;
388          end if;
389
390          --  If that special case does not occur, then we are certain that
391          --  the two locations are really in separate units.
392
393          return No;
394
395          <<Continue>>
396             null;
397       end loop;
398    end Check_Same_Extended_Unit;
399
400    -------------------------------
401    -- Compilation_Switches_Last --
402    -------------------------------
403
404    function Compilation_Switches_Last return Nat is
405    begin
406       return Compilation_Switches.Last;
407    end Compilation_Switches_Last;
408
409    procedure Disable_Switch_Storing is
410    begin
411       Switch_Storing_Enabled := False;
412    end Disable_Switch_Storing;
413
414    ------------------------------
415    -- Earlier_In_Extended_Unit --
416    ------------------------------
417
418    function Earlier_In_Extended_Unit (S1, S2 : Source_Ptr) return Boolean is
419    begin
420       return Check_Same_Extended_Unit (S1, S2) = Yes_Before;
421    end Earlier_In_Extended_Unit;
422
423    ----------------------------
424    -- Entity_Is_In_Main_Unit --
425    ----------------------------
426
427    function Entity_Is_In_Main_Unit (E : Entity_Id) return Boolean is
428       S : Entity_Id;
429
430    begin
431       S := Scope (E);
432
433       while S /= Standard_Standard loop
434          if S = Main_Unit_Entity then
435             return True;
436          elsif Ekind (S) = E_Package and then Is_Child_Unit (S) then
437             return False;
438          else
439             S := Scope (S);
440          end if;
441       end loop;
442
443       return False;
444    end Entity_Is_In_Main_Unit;
445
446    ---------------------------------
447    -- Generic_Separately_Compiled --
448    ---------------------------------
449
450    function Generic_Separately_Compiled (E : Entity_Id) return Boolean is
451    begin
452       --  We do not generate object files for internal generics, because
453       --  the only thing they would contain is the elaboration boolean, and
454       --  we are careful to elaborate all predefined units first anyway, so
455       --  this boolean is not needed.
456
457       if Is_Internal_File_Name
458           (Fname => Unit_File_Name (Get_Source_Unit (E)),
459            Renamings_Included => True)
460       then
461          return False;
462
463       --  All other generic units do generate object files
464
465       else
466          return True;
467       end if;
468    end Generic_Separately_Compiled;
469
470    function Generic_Separately_Compiled
471      (Sfile : File_Name_Type) return Boolean
472    is
473    begin
474       --  Exactly the same as previous function, but works directly on a file
475       --  name.
476
477       if Is_Internal_File_Name
478           (Fname              => Sfile,
479            Renamings_Included => True)
480       then
481          return False;
482
483       --  All other generic units do generate object files
484
485       else
486          return True;
487       end if;
488    end Generic_Separately_Compiled;
489
490    -------------------
491    -- Get_Code_Unit --
492    -------------------
493
494    function Get_Code_Unit (S : Source_Ptr) return Unit_Number_Type is
495    begin
496       --  Search table unless we have No_Location, which can happen if the
497       --  relevant location has not been set yet. Happens for example when
498       --  we obtain Sloc (Cunit (Main_Unit)) before it is set.
499
500       if S /= No_Location then
501          declare
502             Source_File : constant Source_File_Index :=
503                             Get_Source_File_Index (Top_Level_Location (S));
504
505          begin
506             for U in Units.First .. Units.Last loop
507                if Source_Index (U) = Source_File then
508                   return U;
509                end if;
510             end loop;
511          end;
512       end if;
513
514       --  If S was No_Location, or was not in the table, we must be in the
515       --  main source unit (and the value has not been placed in the table yet)
516
517       return Main_Unit;
518    end Get_Code_Unit;
519
520    function Get_Code_Unit (N : Node_Or_Entity_Id) return Unit_Number_Type is
521    begin
522       return Get_Code_Unit (Sloc (N));
523    end Get_Code_Unit;
524
525    ----------------------------
526    -- Get_Compilation_Switch --
527    ----------------------------
528
529    function Get_Compilation_Switch (N : Pos) return String_Ptr is
530    begin
531       if N <= Compilation_Switches.Last then
532          return Compilation_Switches.Table (N);
533
534       else
535          return null;
536       end if;
537    end Get_Compilation_Switch;
538
539    ----------------------------------
540    -- Get_Cunit_Entity_Unit_Number --
541    ----------------------------------
542
543    function Get_Cunit_Entity_Unit_Number
544      (E : Entity_Id) return Unit_Number_Type
545    is
546    begin
547       for U in Units.First .. Units.Last loop
548          if Cunit_Entity (U) = E then
549             return U;
550          end if;
551       end loop;
552
553       --  If not in the table, must be the main source unit, and we just
554       --  have not got it put into the table yet.
555
556       return Main_Unit;
557    end Get_Cunit_Entity_Unit_Number;
558
559    ---------------------------
560    -- Get_Cunit_Unit_Number --
561    ---------------------------
562
563    function Get_Cunit_Unit_Number (N : Node_Id) return Unit_Number_Type is
564    begin
565       for U in Units.First .. Units.Last loop
566          if Cunit (U) = N then
567             return U;
568          end if;
569       end loop;
570
571       --  If not in the table, must be the main source unit, and we just
572       --  have not got it put into the table yet.
573
574       return Main_Unit;
575    end Get_Cunit_Unit_Number;
576
577    ---------------------
578    -- Get_Source_Unit --
579    ---------------------
580
581    function Get_Source_Unit (S : Source_Ptr) return Unit_Number_Type is
582    begin
583       --  Search table unless we have No_Location, which can happen if the
584       --  relevant location has not been set yet. Happens for example when
585       --  we obtain Sloc (Cunit (Main_Unit)) before it is set.
586
587       if S /= No_Location then
588          declare
589             Source_File : Source_File_Index :=
590                             Get_Source_File_Index (Top_Level_Location (S));
591
592          begin
593             Source_File := Get_Source_File_Index (S);
594             while Template (Source_File) /= No_Source_File loop
595                Source_File := Template (Source_File);
596             end loop;
597
598             for U in Units.First .. Units.Last loop
599                if Source_Index (U) = Source_File then
600                   return U;
601                end if;
602             end loop;
603          end;
604       end if;
605
606       --  If S was No_Location, or was not in the table, we must be in the
607       --  main source unit (and the value has not got put into the table yet)
608
609       return Main_Unit;
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_Same_Code_Unit --
752    -----------------------
753
754    function In_Same_Code_Unit (N1, N2 : Node_Or_Entity_Id) return Boolean is
755       S1 : constant Source_Ptr := Sloc (N1);
756       S2 : constant Source_Ptr := Sloc (N2);
757
758    begin
759       if S1 = No_Location or else S2 = No_Location then
760          return False;
761
762       elsif S1 = Standard_Location then
763          return S2 = Standard_Location;
764
765       elsif S2 = Standard_Location then
766          return False;
767       end if;
768
769       return Get_Code_Unit (N1) = Get_Code_Unit (N2);
770    end In_Same_Code_Unit;
771
772    ---------------------------
773    -- In_Same_Extended_Unit --
774    ---------------------------
775
776    function In_Same_Extended_Unit
777      (N1, N2 : Node_Or_Entity_Id) return Boolean
778    is
779    begin
780       return Check_Same_Extended_Unit (Sloc (N1), Sloc (N2)) /= No;
781    end In_Same_Extended_Unit;
782
783    function In_Same_Extended_Unit (S1, S2 : Source_Ptr) return Boolean is
784    begin
785       return Check_Same_Extended_Unit (S1, S2) /= No;
786    end In_Same_Extended_Unit;
787
788    -------------------------
789    -- In_Same_Source_Unit --
790    -------------------------
791
792    function In_Same_Source_Unit (N1, N2 : Node_Or_Entity_Id) return Boolean is
793       S1 : constant Source_Ptr := Sloc (N1);
794       S2 : constant Source_Ptr := Sloc (N2);
795
796    begin
797       if S1 = No_Location or else S2 = No_Location then
798          return False;
799
800       elsif S1 = Standard_Location then
801          return S2 = Standard_Location;
802
803       elsif S2 = Standard_Location then
804          return False;
805       end if;
806
807       return Get_Source_Unit (N1) = Get_Source_Unit (N2);
808    end In_Same_Source_Unit;
809
810    -----------------------------
811    -- Increment_Serial_Number --
812    -----------------------------
813
814    function Increment_Serial_Number return Nat is
815       TSN : Int renames Units.Table (Current_Sem_Unit).Serial_Number;
816    begin
817       TSN := TSN + 1;
818       return TSN;
819    end Increment_Serial_Number;
820
821    ----------------
822    -- Initialize --
823    ----------------
824
825    procedure Initialize is
826    begin
827       Linker_Option_Lines.Init;
828       Load_Stack.Init;
829       Units.Init;
830       Unit_Exception_Table_Present := False;
831       Compilation_Switches.Init;
832    end Initialize;
833
834    ---------------
835    -- Is_Loaded --
836    ---------------
837
838    function Is_Loaded (Uname : Unit_Name_Type) return Boolean is
839    begin
840       for Unum in Units.First .. Units.Last loop
841          if Uname = Unit_Name (Unum) then
842             return True;
843          end if;
844       end loop;
845
846       return False;
847    end Is_Loaded;
848
849    ---------------
850    -- Last_Unit --
851    ---------------
852
853    function Last_Unit return Unit_Number_Type is
854    begin
855       return Units.Last;
856    end Last_Unit;
857
858    ----------
859    -- List --
860    ----------
861
862    procedure List (File_Names_Only : Boolean := False) is separate;
863
864    ----------
865    -- Lock --
866    ----------
867
868    procedure Lock is
869    begin
870       Linker_Option_Lines.Locked := True;
871       Load_Stack.Locked := True;
872       Units.Locked := True;
873       Linker_Option_Lines.Release;
874       Load_Stack.Release;
875       Units.Release;
876    end Lock;
877
878    ---------------
879    -- Num_Units --
880    ---------------
881
882    function Num_Units return Nat is
883    begin
884       return Int (Units.Last) - Int (Main_Unit) + 1;
885    end Num_Units;
886
887    -----------------
888    -- Remove_Unit --
889    -----------------
890
891    procedure Remove_Unit (U : Unit_Number_Type) is
892    begin
893       if U = Units.Last then
894          Units.Decrement_Last;
895       end if;
896    end Remove_Unit;
897
898    ----------------------------------
899    -- Replace_Linker_Option_String --
900    ----------------------------------
901
902    procedure Replace_Linker_Option_String
903      (S : String_Id; Match_String : String)
904    is
905    begin
906       if Match_String'Length > 0 then
907          for J in 1 .. Linker_Option_Lines.Last loop
908             String_To_Name_Buffer (Linker_Option_Lines.Table (J).Option);
909
910             if Match_String = Name_Buffer (1 .. Match_String'Length) then
911                Linker_Option_Lines.Table (J).Option := S;
912                return;
913             end if;
914          end loop;
915       end if;
916
917       Store_Linker_Option_String (S);
918    end Replace_Linker_Option_String;
919
920    ----------
921    -- Sort --
922    ----------
923
924    procedure Sort (Tbl : in out Unit_Ref_Table) is separate;
925
926    ------------------------------
927    -- Store_Compilation_Switch --
928    ------------------------------
929
930    procedure Store_Compilation_Switch (Switch : String) is
931    begin
932       if Switch_Storing_Enabled then
933          Compilation_Switches.Increment_Last;
934          Compilation_Switches.Table (Compilation_Switches.Last) :=
935            new String'(Switch);
936
937          --  Fix up --RTS flag which has been transformed by the gcc driver
938          --  into -fRTS
939
940          if Switch'Last >= Switch'First + 4
941            and then Switch (Switch'First .. Switch'First + 4) = "-fRTS"
942          then
943             Compilation_Switches.Table
944               (Compilation_Switches.Last) (Switch'First + 1) := '-';
945          end if;
946       end if;
947    end Store_Compilation_Switch;
948
949    --------------------------------
950    -- Store_Linker_Option_String --
951    --------------------------------
952
953    procedure Store_Linker_Option_String (S : String_Id) is
954    begin
955       Linker_Option_Lines.Increment_Last;
956       Linker_Option_Lines.Table (Linker_Option_Lines.Last) :=
957         (Option => S, Unit => Current_Sem_Unit);
958    end Store_Linker_Option_String;
959
960    -------------------------------
961    -- Synchronize_Serial_Number --
962    -------------------------------
963
964    procedure Synchronize_Serial_Number is
965       TSN : Int renames Units.Table (Current_Sem_Unit).Serial_Number;
966    begin
967       TSN := TSN + 1;
968    end Synchronize_Serial_Number;
969
970    ---------------
971    -- Tree_Read --
972    ---------------
973
974    procedure Tree_Read is
975       N : Nat;
976       S : String_Ptr;
977
978    begin
979       Units.Tree_Read;
980
981       --  Read Compilation_Switches table
982
983       Tree_Read_Int (N);
984       Compilation_Switches.Set_Last (N);
985
986       for J in 1 .. N loop
987          Tree_Read_Str (S);
988          Compilation_Switches.Table (J) := S;
989       end loop;
990    end Tree_Read;
991
992    ----------------
993    -- Tree_Write --
994    ----------------
995
996    procedure Tree_Write is
997    begin
998       Units.Tree_Write;
999
1000       --  Write Compilation_Switches table
1001
1002       Tree_Write_Int (Compilation_Switches.Last);
1003
1004       for J in 1 .. Compilation_Switches.Last loop
1005          Tree_Write_Str (Compilation_Switches.Table (J));
1006       end loop;
1007    end Tree_Write;
1008
1009    -----------------
1010    -- Version_Get --
1011    -----------------
1012
1013    function Version_Get (U : Unit_Number_Type) return Word_Hex_String is
1014    begin
1015       return Get_Hex_String (Units.Table (U).Version);
1016    end Version_Get;
1017
1018    ------------------------
1019    -- Version_Referenced --
1020    ------------------------
1021
1022    procedure Version_Referenced (S : String_Id) is
1023    begin
1024       Version_Ref.Append (S);
1025    end Version_Referenced;
1026
1027 end Lib;