OSDN Git Service

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