OSDN Git Service

PR ada/52494
[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-2011, 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 3,  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.                                     --
17 --                                                                          --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception,   --
20 -- version 3.1, as published by the Free Software Foundation.               --
21 --                                                                          --
22 -- You should have received a copy of the GNU General Public License and    --
23 -- a copy of the GCC Runtime Library Exception along with this program;     --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
25 -- <http://www.gnu.org/licenses/>.                                          --
26 --                                                                          --
27 -- GNAT was originally developed  by the GNAT team at  New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
29 --                                                                          --
30 ------------------------------------------------------------------------------
31
32 pragma Style_Checks (All_Checks);
33 --  Subprogram ordering not enforced in this unit
34 --  (because of some logical groupings).
35
36 with Atree;    use Atree;
37 with Csets;    use Csets;
38 with Einfo;    use Einfo;
39 with Fname;    use Fname;
40 with Output;   use Output;
41 with Sinfo;    use Sinfo;
42 with Sinput;   use Sinput;
43 with Stand;    use Stand;
44 with Stringt;  use Stringt;
45 with Tree_IO;  use Tree_IO;
46 with Uname;    use Uname;
47 with Widechar; use Widechar;
48
49 package body Lib is
50
51    Switch_Storing_Enabled : Boolean := True;
52    --  Controlled by Enable_Switch_Storing/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_Allocator (U : Unit_Number_Type) return Boolean is
119    begin
120       return Units.Table (U).Has_Allocator;
121    end Has_Allocator;
122
123    function Has_RACW (U : Unit_Number_Type) return Boolean is
124    begin
125       return Units.Table (U).Has_RACW;
126    end Has_RACW;
127
128    function Is_Compiler_Unit (U : Unit_Number_Type) return Boolean is
129    begin
130       return Units.Table (U).Is_Compiler_Unit;
131    end Is_Compiler_Unit;
132
133    function Ident_String (U : Unit_Number_Type) return Node_Id is
134    begin
135       return Units.Table (U).Ident_String;
136    end Ident_String;
137
138    function Loading (U : Unit_Number_Type) return Boolean is
139    begin
140       return Units.Table (U).Loading;
141    end Loading;
142
143    function Main_CPU (U : Unit_Number_Type) return Int is
144    begin
145       return Units.Table (U).Main_CPU;
146    end Main_CPU;
147
148    function Main_Priority (U : Unit_Number_Type) return Int is
149    begin
150       return Units.Table (U).Main_Priority;
151    end Main_Priority;
152
153    function Munit_Index (U : Unit_Number_Type) return Nat is
154    begin
155       return Units.Table (U).Munit_Index;
156    end Munit_Index;
157
158    function OA_Setting (U : Unit_Number_Type) return Character is
159    begin
160       return Units.Table (U).OA_Setting;
161    end OA_Setting;
162
163    function Source_Index (U : Unit_Number_Type) return Source_File_Index is
164    begin
165       return Units.Table (U).Source_Index;
166    end Source_Index;
167
168    function Unit_File_Name (U : Unit_Number_Type) return File_Name_Type is
169    begin
170       return Units.Table (U).Unit_File_Name;
171    end Unit_File_Name;
172
173    function Unit_Name (U : Unit_Number_Type) return Unit_Name_Type is
174    begin
175       return Units.Table (U).Unit_Name;
176    end Unit_Name;
177
178    ------------------------------------------
179    -- Subprograms to Set Unit Table Fields --
180    ------------------------------------------
181
182    procedure Set_Cunit (U : Unit_Number_Type; N : Node_Id) is
183    begin
184       Units.Table (U).Cunit := N;
185    end Set_Cunit;
186
187    procedure Set_Cunit_Entity (U : Unit_Number_Type; E : Entity_Id) is
188    begin
189       Units.Table (U).Cunit_Entity := E;
190       Set_Is_Compilation_Unit (E);
191    end Set_Cunit_Entity;
192
193    procedure Set_Dynamic_Elab (U : Unit_Number_Type; B : Boolean := True) is
194    begin
195       Units.Table (U).Dynamic_Elab := B;
196    end Set_Dynamic_Elab;
197
198    procedure Set_Error_Location (U : Unit_Number_Type; W : Source_Ptr) is
199    begin
200       Units.Table (U).Error_Location := W;
201    end Set_Error_Location;
202
203    procedure Set_Fatal_Error (U : Unit_Number_Type; B : Boolean := True) is
204    begin
205       Units.Table (U).Fatal_Error := B;
206    end Set_Fatal_Error;
207
208    procedure Set_Generate_Code (U : Unit_Number_Type; B : Boolean := True) is
209    begin
210       Units.Table (U).Generate_Code := B;
211    end Set_Generate_Code;
212
213    procedure Set_Has_Allocator (U : Unit_Number_Type; B : Boolean := True) is
214    begin
215       Units.Table (U).Has_Allocator := B;
216    end Set_Has_Allocator;
217
218    procedure Set_Has_RACW (U : Unit_Number_Type; B : Boolean := True) is
219    begin
220       Units.Table (U).Has_RACW := B;
221    end Set_Has_RACW;
222
223    procedure Set_Is_Compiler_Unit
224      (U : Unit_Number_Type;
225       B : Boolean := True)
226    is
227    begin
228       Units.Table (U).Is_Compiler_Unit := B;
229    end Set_Is_Compiler_Unit;
230
231    procedure Set_Ident_String (U : Unit_Number_Type; N : Node_Id) is
232    begin
233       Units.Table (U).Ident_String := N;
234    end Set_Ident_String;
235
236    procedure Set_Loading (U : Unit_Number_Type; B : Boolean := True) is
237    begin
238       Units.Table (U).Loading := B;
239    end Set_Loading;
240
241    procedure Set_Main_CPU (U : Unit_Number_Type; P : Int) is
242    begin
243       Units.Table (U).Main_CPU := P;
244    end Set_Main_CPU;
245
246    procedure Set_Main_Priority (U : Unit_Number_Type; P : Int) is
247    begin
248       Units.Table (U).Main_Priority := P;
249    end Set_Main_Priority;
250
251    procedure Set_OA_Setting (U : Unit_Number_Type; C : Character) is
252    begin
253       Units.Table (U).OA_Setting := C;
254    end Set_OA_Setting;
255
256    procedure Set_Unit_Name (U : Unit_Number_Type; N : Unit_Name_Type) is
257    begin
258       Units.Table (U).Unit_Name := N;
259    end Set_Unit_Name;
260
261    ------------------------------
262    -- Check_Same_Extended_Unit --
263    ------------------------------
264
265    function Check_Same_Extended_Unit (S1, S2 : Source_Ptr) return SEU_Result is
266       Sloc1  : Source_Ptr;
267       Sloc2  : Source_Ptr;
268       Sind1  : Source_File_Index;
269       Sind2  : Source_File_Index;
270       Inst1  : Source_Ptr;
271       Inst2  : Source_Ptr;
272       Unum1  : Unit_Number_Type;
273       Unum2  : Unit_Number_Type;
274       Unit1  : Node_Id;
275       Unit2  : Node_Id;
276       Depth1 : Nat;
277       Depth2 : Nat;
278
279    begin
280       if S1 = No_Location or else S2 = No_Location then
281          return No;
282
283       elsif S1 = Standard_Location then
284          if S2 = Standard_Location then
285             return Yes_Same;
286          else
287             return No;
288          end if;
289
290       elsif S2 = Standard_Location then
291          return No;
292       end if;
293
294       Sloc1 := S1;
295       Sloc2 := S2;
296
297       Unum1 := Get_Source_Unit (Sloc1);
298       Unum2 := Get_Source_Unit (Sloc2);
299
300       loop
301          --  Step 1: Check whether the two locations are in the same source
302          --  file.
303
304          Sind1 := Get_Source_File_Index (Sloc1);
305          Sind2 := Get_Source_File_Index (Sloc2);
306
307          if Sind1 = Sind2 then
308             if Sloc1 < Sloc2 then
309                return Yes_Before;
310             elsif Sloc1 > Sloc2 then
311                return Yes_After;
312             else
313                return Yes_Same;
314             end if;
315          end if;
316
317          --  Step 2: Check subunits. If a subunit is instantiated, follow the
318          --  instantiation chain rather than the stub chain.
319
320          Unit1 := Unit (Cunit (Unum1));
321          Unit2 := Unit (Cunit (Unum2));
322          Inst1 := Instantiation (Sind1);
323          Inst2 := Instantiation (Sind2);
324
325          if Nkind (Unit1) = N_Subunit
326            and then Present (Corresponding_Stub (Unit1))
327            and then Inst1 = No_Location
328          then
329             if Nkind (Unit2) = N_Subunit
330               and then Present (Corresponding_Stub (Unit2))
331               and then Inst2 = No_Location
332             then
333                --  Both locations refer to subunits which may have a common
334                --  ancestor. If they do, the deeper subunit must have a longer
335                --  unit name. Replace the deeper one with its corresponding
336                --  stub in order to find the nearest ancestor.
337
338                if Length_Of_Name (Unit_Name (Unum1)) <
339                   Length_Of_Name (Unit_Name (Unum2))
340                then
341                   Sloc2 := Sloc (Corresponding_Stub (Unit2));
342                   Unum2 := Get_Source_Unit (Sloc2);
343                   goto Continue;
344
345                else
346                   Sloc1 := Sloc (Corresponding_Stub (Unit1));
347                   Unum1 := Get_Source_Unit (Sloc1);
348                   goto Continue;
349                end if;
350
351             --  Sloc1 in subunit, Sloc2 not
352
353             else
354                Sloc1 := Sloc (Corresponding_Stub (Unit1));
355                Unum1 := Get_Source_Unit (Sloc1);
356                goto Continue;
357             end if;
358
359          --  Sloc2 in subunit, Sloc1 not
360
361          elsif Nkind (Unit2) = N_Subunit
362            and then Present (Corresponding_Stub (Unit2))
363            and then Inst2 = No_Location
364          then
365             Sloc2 := Sloc (Corresponding_Stub (Unit2));
366             Unum2 := Get_Source_Unit (Sloc2);
367             goto Continue;
368          end if;
369
370          --  Step 3: Check instances. The two locations may yield a common
371          --  ancestor.
372
373          if Inst1 /= No_Location then
374             if Inst2 /= No_Location then
375
376                --  Both locations denote instantiations
377
378                Depth1 := Instantiation_Depth (Sloc1);
379                Depth2 := Instantiation_Depth (Sloc2);
380
381                if Depth1 < Depth2 then
382                   Sloc2 := Inst2;
383                   Unum2 := Get_Source_Unit (Sloc2);
384                   goto Continue;
385
386                elsif Depth1 > Depth2 then
387                   Sloc1 := Inst1;
388                   Unum1 := Get_Source_Unit (Sloc1);
389                   goto Continue;
390
391                else
392                   Sloc1 := Inst1;
393                   Sloc2 := Inst2;
394                   Unum1 := Get_Source_Unit (Sloc1);
395                   Unum2 := Get_Source_Unit (Sloc2);
396                   goto Continue;
397                end if;
398
399             --  Sloc1 is an instantiation
400
401             else
402                Sloc1 := Inst1;
403                Unum1 := Get_Source_Unit (Sloc1);
404                goto Continue;
405             end if;
406
407          --  Sloc2 is an instantiation
408
409          elsif Inst2 /= No_Location then
410             Sloc2 := Inst2;
411             Unum2 := Get_Source_Unit (Sloc2);
412             goto Continue;
413          end if;
414
415          --  Step 4: One location in the spec, the other in the corresponding
416          --  body of the same unit. The location in the spec is considered
417          --  earlier.
418
419          if Nkind (Unit1) = N_Subprogram_Body
420               or else
421             Nkind (Unit1) = N_Package_Body
422          then
423             if Library_Unit (Cunit (Unum1)) = Cunit (Unum2) then
424                return Yes_After;
425             end if;
426
427          elsif Nkind (Unit2) = N_Subprogram_Body
428                  or else
429                Nkind (Unit2) = N_Package_Body
430          then
431             if Library_Unit (Cunit (Unum2)) = Cunit (Unum1) then
432                return Yes_Before;
433             end if;
434          end if;
435
436          --  At this point it is certain that the two locations denote two
437          --  entirely separate units.
438
439          return No;
440
441          <<Continue>>
442             null;
443       end loop;
444    end Check_Same_Extended_Unit;
445
446    -------------------------------
447    -- Compilation_Switches_Last --
448    -------------------------------
449
450    function Compilation_Switches_Last return Nat is
451    begin
452       return Compilation_Switches.Last;
453    end Compilation_Switches_Last;
454
455    ---------------------------
456    -- Enable_Switch_Storing --
457    ---------------------------
458
459    procedure Enable_Switch_Storing is
460    begin
461       Switch_Storing_Enabled := True;
462    end Enable_Switch_Storing;
463
464    ----------------------------
465    -- Disable_Switch_Storing --
466    ----------------------------
467
468    procedure Disable_Switch_Storing is
469    begin
470       Switch_Storing_Enabled := False;
471    end Disable_Switch_Storing;
472
473    ------------------------------
474    -- Earlier_In_Extended_Unit --
475    ------------------------------
476
477    function Earlier_In_Extended_Unit (S1, S2 : Source_Ptr) return Boolean is
478    begin
479       return Check_Same_Extended_Unit (S1, S2) = Yes_Before;
480    end Earlier_In_Extended_Unit;
481
482    -----------------------
483    -- Exact_Source_Name --
484    -----------------------
485
486    function Exact_Source_Name (Loc : Source_Ptr) return String is
487       U    : constant Unit_Number_Type  := Get_Source_Unit (Loc);
488       Buf  : constant Source_Buffer_Ptr := Source_Text (Source_Index (U));
489       Orig : constant Source_Ptr        := Original_Location (Loc);
490       P    : Source_Ptr;
491
492       WC   : Char_Code;
493       Err  : Boolean;
494       pragma Warnings (Off, WC);
495       pragma Warnings (Off, Err);
496
497    begin
498       --  Entity is character literal
499
500       if Buf (Orig) = ''' then
501          return String (Buf (Orig .. Orig + 2));
502
503       --  Entity is operator symbol
504
505       elsif Buf (Orig) = '"' or else Buf (Orig) = '%' then
506          P := Orig;
507
508          loop
509             P := P + 1;
510             exit when Buf (P) = Buf (Orig);
511          end loop;
512
513          return String (Buf (Orig .. P));
514
515       --  Entity is identifier
516
517       else
518          P := Orig;
519
520          loop
521             if Is_Start_Of_Wide_Char (Buf, P) then
522                Scan_Wide (Buf, P, WC, Err);
523             elsif not Identifier_Char (Buf (P)) then
524                exit;
525             else
526                P := P + 1;
527             end if;
528          end loop;
529
530          --  Write out the identifier by copying the exact source characters
531          --  used in its declaration. Note that this means wide characters will
532          --  be in their original encoded form.
533
534          return String (Buf (Orig .. P - 1));
535       end if;
536    end Exact_Source_Name;
537
538    ----------------------------
539    -- Entity_Is_In_Main_Unit --
540    ----------------------------
541
542    function Entity_Is_In_Main_Unit (E : Entity_Id) return Boolean is
543       S : Entity_Id;
544
545    begin
546       S := Scope (E);
547
548       while S /= Standard_Standard loop
549          if S = Main_Unit_Entity then
550             return True;
551          elsif Ekind (S) = E_Package and then Is_Child_Unit (S) then
552             return False;
553          else
554             S := Scope (S);
555          end if;
556       end loop;
557
558       return False;
559    end Entity_Is_In_Main_Unit;
560
561    --------------------------
562    -- Generic_May_Lack_ALI --
563    --------------------------
564
565    function Generic_May_Lack_ALI (Sfile : File_Name_Type) return Boolean is
566    begin
567       --  We allow internal generic units to be used without having a
568       --  corresponding ALI files to help bootstrapping with older compilers
569       --  that did not support generating ALIs for such generics. It is safe
570       --  to do so because the only thing the generated code would contain
571       --  is the elaboration boolean, and we are careful to elaborate all
572       --  predefined units first anyway.
573
574       return Is_Internal_File_Name
575                (Fname              => Sfile,
576                 Renamings_Included => True);
577    end Generic_May_Lack_ALI;
578
579    -----------------------------
580    -- Get_Code_Or_Source_Unit --
581    -----------------------------
582
583    function Get_Code_Or_Source_Unit
584      (S                : Source_Ptr;
585       Unwind_Instances : Boolean) return Unit_Number_Type
586    is
587    begin
588       --  Search table unless we have No_Location, which can happen if the
589       --  relevant location has not been set yet. Happens for example when
590       --  we obtain Sloc (Cunit (Main_Unit)) before it is set.
591
592       if S /= No_Location then
593          declare
594             Source_File : Source_File_Index;
595             Source_Unit : Unit_Number_Type;
596
597          begin
598             Source_File := Get_Source_File_Index (S);
599
600             if Unwind_Instances then
601                while Template (Source_File) /= No_Source_File loop
602                   Source_File := Template (Source_File);
603                end loop;
604             end if;
605
606             Source_Unit := Unit (Source_File);
607
608             if Source_Unit /= No_Unit then
609                return Source_Unit;
610             end if;
611          end;
612       end if;
613
614       --  If S was No_Location, or was not in the table, we must be in the main
615       --  source unit (and the value has not been placed in the table yet),
616       --  or in one of the configuration pragma files.
617
618       return Main_Unit;
619    end Get_Code_Or_Source_Unit;
620
621    -------------------
622    -- Get_Code_Unit --
623    -------------------
624
625    function Get_Code_Unit (S : Source_Ptr) return Unit_Number_Type is
626    begin
627       return Get_Code_Or_Source_Unit (Top_Level_Location (S),
628         Unwind_Instances => False);
629    end Get_Code_Unit;
630
631    function Get_Code_Unit (N : Node_Or_Entity_Id) return Unit_Number_Type is
632    begin
633       return Get_Code_Unit (Sloc (N));
634    end Get_Code_Unit;
635
636    ----------------------------
637    -- Get_Compilation_Switch --
638    ----------------------------
639
640    function Get_Compilation_Switch (N : Pos) return String_Ptr is
641    begin
642       if N <= Compilation_Switches.Last then
643          return Compilation_Switches.Table (N);
644
645       else
646          return null;
647       end if;
648    end Get_Compilation_Switch;
649
650    ----------------------------------
651    -- Get_Cunit_Entity_Unit_Number --
652    ----------------------------------
653
654    function Get_Cunit_Entity_Unit_Number
655      (E : Entity_Id) return Unit_Number_Type
656    is
657    begin
658       for U in Units.First .. Units.Last loop
659          if Cunit_Entity (U) = E then
660             return U;
661          end if;
662       end loop;
663
664       --  If not in the table, must be the main source unit, and we just
665       --  have not got it put into the table yet.
666
667       return Main_Unit;
668    end Get_Cunit_Entity_Unit_Number;
669
670    ---------------------------
671    -- Get_Cunit_Unit_Number --
672    ---------------------------
673
674    function Get_Cunit_Unit_Number (N : Node_Id) return Unit_Number_Type is
675    begin
676       for U in Units.First .. Units.Last loop
677          if Cunit (U) = N then
678             return U;
679          end if;
680       end loop;
681
682       --  If not in the table, must be a spec created for a main unit that is a
683       --  child subprogram body which we have not inserted into the table yet.
684
685       if N = Library_Unit (Cunit (Main_Unit)) then
686          return Main_Unit;
687
688       --  If it is anything else, something is seriously wrong, and we really
689       --  don't want to proceed, even if assertions are off, so we explicitly
690       --  raise an exception in this case to terminate compilation.
691
692       else
693          raise Program_Error;
694       end if;
695    end Get_Cunit_Unit_Number;
696
697    ---------------------
698    -- Get_Source_Unit --
699    ---------------------
700
701    function Get_Source_Unit (S : Source_Ptr) return Unit_Number_Type is
702    begin
703       return Get_Code_Or_Source_Unit (S, Unwind_Instances => True);
704    end Get_Source_Unit;
705
706    function Get_Source_Unit (N : Node_Or_Entity_Id) return Unit_Number_Type is
707    begin
708       return Get_Source_Unit (Sloc (N));
709    end Get_Source_Unit;
710
711    --------------------------------
712    -- In_Extended_Main_Code_Unit --
713    --------------------------------
714
715    function In_Extended_Main_Code_Unit
716      (N : Node_Or_Entity_Id) return Boolean
717    is
718    begin
719       if Sloc (N) = Standard_Location then
720          return True;
721
722       elsif Sloc (N) = No_Location then
723          return False;
724
725       --  Special case Itypes to test the Sloc of the associated node. The
726       --  reason we do this is for possible calls from gigi after -gnatD
727       --  processing is complete in sprint. This processing updates the
728       --  sloc fields of all nodes in the tree, but itypes are not in the
729       --  tree so their slocs do not get updated.
730
731       elsif Nkind (N) = N_Defining_Identifier
732         and then Is_Itype (N)
733       then
734          return In_Extended_Main_Code_Unit (Associated_Node_For_Itype (N));
735
736       --  Otherwise see if we are in the main unit
737
738       elsif Get_Code_Unit (Sloc (N)) = Get_Code_Unit (Cunit (Main_Unit)) then
739          return True;
740
741       --  Node may be in spec (or subunit etc) of main unit
742
743       else
744          return
745            In_Same_Extended_Unit (N, Cunit (Main_Unit));
746       end if;
747    end In_Extended_Main_Code_Unit;
748
749    function In_Extended_Main_Code_Unit (Loc : Source_Ptr) return Boolean is
750    begin
751       if Loc = Standard_Location then
752          return True;
753
754       elsif Loc = No_Location then
755          return False;
756
757       --  Otherwise see if we are in the main unit
758
759       elsif Get_Code_Unit (Loc) = Get_Code_Unit (Cunit (Main_Unit)) then
760          return True;
761
762       --  Location may be in spec (or subunit etc) of main unit
763
764       else
765          return
766            In_Same_Extended_Unit (Loc, Sloc (Cunit (Main_Unit)));
767       end if;
768    end In_Extended_Main_Code_Unit;
769
770    ----------------------------------
771    -- In_Extended_Main_Source_Unit --
772    ----------------------------------
773
774    function In_Extended_Main_Source_Unit
775      (N : Node_Or_Entity_Id) return Boolean
776    is
777       Nloc : constant Source_Ptr := Sloc (N);
778       Mloc : constant Source_Ptr := Sloc (Cunit (Main_Unit));
779
780    begin
781       --  If parsing, then use the global flag to indicate result
782
783       if Compiler_State = Parsing then
784          return Parsing_Main_Extended_Source;
785
786       --  Special value cases
787
788       elsif Nloc = Standard_Location then
789          return True;
790
791       elsif Nloc = No_Location then
792          return False;
793
794       --  Special case Itypes to test the Sloc of the associated node. The
795       --  reason we do this is for possible calls from gigi after -gnatD
796       --  processing is complete in sprint. This processing updates the
797       --  sloc fields of all nodes in the tree, but itypes are not in the
798       --  tree so their slocs do not get updated.
799
800       elsif Nkind (N) = N_Defining_Identifier
801         and then Is_Itype (N)
802       then
803          return In_Extended_Main_Source_Unit (Associated_Node_For_Itype (N));
804
805       --  Otherwise compare original locations to see if in same unit
806
807       else
808          return
809            In_Same_Extended_Unit
810              (Original_Location (Nloc), Original_Location (Mloc));
811       end if;
812    end In_Extended_Main_Source_Unit;
813
814    function In_Extended_Main_Source_Unit
815      (Loc : Source_Ptr) return Boolean
816    is
817       Mloc : constant Source_Ptr := Sloc (Cunit (Main_Unit));
818
819    begin
820       --  If parsing, then use the global flag to indicate result
821
822       if Compiler_State = Parsing then
823          return Parsing_Main_Extended_Source;
824
825       --  Special value cases
826
827       elsif Loc = Standard_Location then
828          return True;
829
830       elsif Loc = No_Location then
831          return False;
832
833       --  Otherwise compare original locations to see if in same unit
834
835       else
836          return
837            In_Same_Extended_Unit
838              (Original_Location (Loc), Original_Location (Mloc));
839       end if;
840    end In_Extended_Main_Source_Unit;
841
842    ------------------------
843    -- In_Predefined_Unit --
844    ------------------------
845
846    function In_Predefined_Unit (N : Node_Or_Entity_Id) return Boolean is
847    begin
848       return In_Predefined_Unit (Sloc (N));
849    end In_Predefined_Unit;
850
851    function In_Predefined_Unit (S : Source_Ptr) return Boolean is
852       Unit : constant Unit_Number_Type := Get_Source_Unit (S);
853       File : constant File_Name_Type   := Unit_File_Name (Unit);
854    begin
855       return Is_Predefined_File_Name (File);
856    end In_Predefined_Unit;
857
858    -----------------------
859    -- In_Same_Code_Unit --
860    -----------------------
861
862    function In_Same_Code_Unit (N1, N2 : Node_Or_Entity_Id) return Boolean is
863       S1 : constant Source_Ptr := Sloc (N1);
864       S2 : constant Source_Ptr := Sloc (N2);
865
866    begin
867       if S1 = No_Location or else S2 = No_Location then
868          return False;
869
870       elsif S1 = Standard_Location then
871          return S2 = Standard_Location;
872
873       elsif S2 = Standard_Location then
874          return False;
875       end if;
876
877       return Get_Code_Unit (N1) = Get_Code_Unit (N2);
878    end In_Same_Code_Unit;
879
880    ---------------------------
881    -- In_Same_Extended_Unit --
882    ---------------------------
883
884    function In_Same_Extended_Unit
885      (N1, N2 : Node_Or_Entity_Id) return Boolean
886    is
887    begin
888       return Check_Same_Extended_Unit (Sloc (N1), Sloc (N2)) /= No;
889    end In_Same_Extended_Unit;
890
891    function In_Same_Extended_Unit (S1, S2 : Source_Ptr) return Boolean is
892    begin
893       return Check_Same_Extended_Unit (S1, S2) /= No;
894    end In_Same_Extended_Unit;
895
896    -------------------------
897    -- In_Same_Source_Unit --
898    -------------------------
899
900    function In_Same_Source_Unit (N1, N2 : Node_Or_Entity_Id) return Boolean is
901       S1 : constant Source_Ptr := Sloc (N1);
902       S2 : constant Source_Ptr := Sloc (N2);
903
904    begin
905       if S1 = No_Location or else S2 = No_Location then
906          return False;
907
908       elsif S1 = Standard_Location then
909          return S2 = Standard_Location;
910
911       elsif S2 = Standard_Location then
912          return False;
913       end if;
914
915       return Get_Source_Unit (N1) = Get_Source_Unit (N2);
916    end In_Same_Source_Unit;
917
918    -----------------------------
919    -- Increment_Serial_Number --
920    -----------------------------
921
922    function Increment_Serial_Number return Nat is
923       TSN : Int renames Units.Table (Current_Sem_Unit).Serial_Number;
924    begin
925       TSN := TSN + 1;
926       return TSN;
927    end Increment_Serial_Number;
928
929    ----------------
930    -- Initialize --
931    ----------------
932
933    procedure Initialize is
934    begin
935       Linker_Option_Lines.Init;
936       Notes.Init;
937       Load_Stack.Init;
938       Units.Init;
939       Compilation_Switches.Init;
940    end Initialize;
941
942    ---------------
943    -- Is_Loaded --
944    ---------------
945
946    function Is_Loaded (Uname : Unit_Name_Type) return Boolean is
947    begin
948       for Unum in Units.First .. Units.Last loop
949          if Uname = Unit_Name (Unum) then
950             return True;
951          end if;
952       end loop;
953
954       return False;
955    end Is_Loaded;
956
957    ---------------
958    -- Last_Unit --
959    ---------------
960
961    function Last_Unit return Unit_Number_Type is
962    begin
963       return Units.Last;
964    end Last_Unit;
965
966    ----------
967    -- List --
968    ----------
969
970    procedure List (File_Names_Only : Boolean := False) is separate;
971
972    ----------
973    -- Lock --
974    ----------
975
976    procedure Lock is
977    begin
978       Linker_Option_Lines.Locked := True;
979       Load_Stack.Locked := True;
980       Units.Locked := True;
981       Linker_Option_Lines.Release;
982       Load_Stack.Release;
983       Units.Release;
984    end Lock;
985
986    ---------------
987    -- Num_Units --
988    ---------------
989
990    function Num_Units return Nat is
991    begin
992       return Int (Units.Last) - Int (Main_Unit) + 1;
993    end Num_Units;
994
995    -----------------
996    -- Remove_Unit --
997    -----------------
998
999    procedure Remove_Unit (U : Unit_Number_Type) is
1000    begin
1001       if U = Units.Last then
1002          Units.Decrement_Last;
1003       end if;
1004    end Remove_Unit;
1005
1006    ----------------------------------
1007    -- Replace_Linker_Option_String --
1008    ----------------------------------
1009
1010    procedure Replace_Linker_Option_String
1011      (S : String_Id; Match_String : String)
1012    is
1013    begin
1014       if Match_String'Length > 0 then
1015          for J in 1 .. Linker_Option_Lines.Last loop
1016             String_To_Name_Buffer (Linker_Option_Lines.Table (J).Option);
1017
1018             if Match_String = Name_Buffer (1 .. Match_String'Length) then
1019                Linker_Option_Lines.Table (J).Option := S;
1020                return;
1021             end if;
1022          end loop;
1023       end if;
1024
1025       Store_Linker_Option_String (S);
1026    end Replace_Linker_Option_String;
1027
1028    ----------
1029    -- Sort --
1030    ----------
1031
1032    procedure Sort (Tbl : in out Unit_Ref_Table) is separate;
1033
1034    ------------------------------
1035    -- Store_Compilation_Switch --
1036    ------------------------------
1037
1038    procedure Store_Compilation_Switch (Switch : String) is
1039    begin
1040       if Switch_Storing_Enabled then
1041          Compilation_Switches.Increment_Last;
1042          Compilation_Switches.Table (Compilation_Switches.Last) :=
1043            new String'(Switch);
1044
1045          --  Fix up --RTS flag which has been transformed by the gcc driver
1046          --  into -fRTS
1047
1048          if Switch'Last >= Switch'First + 4
1049            and then Switch (Switch'First .. Switch'First + 4) = "-fRTS"
1050          then
1051             Compilation_Switches.Table
1052               (Compilation_Switches.Last) (Switch'First + 1) := '-';
1053          end if;
1054       end if;
1055    end Store_Compilation_Switch;
1056
1057    --------------------------------
1058    -- Store_Linker_Option_String --
1059    --------------------------------
1060
1061    procedure Store_Linker_Option_String (S : String_Id) is
1062    begin
1063       Linker_Option_Lines.Append ((Option => S, Unit => Current_Sem_Unit));
1064    end Store_Linker_Option_String;
1065
1066    ----------------
1067    -- Store_Note --
1068    ----------------
1069
1070    procedure Store_Note (N : Node_Id) is
1071    begin
1072       Notes.Append ((Pragma_Node => N, Unit => Current_Sem_Unit));
1073    end Store_Note;
1074
1075    -------------------------------
1076    -- Synchronize_Serial_Number --
1077    -------------------------------
1078
1079    procedure Synchronize_Serial_Number is
1080       TSN : Int renames Units.Table (Current_Sem_Unit).Serial_Number;
1081    begin
1082       TSN := TSN + 1;
1083    end Synchronize_Serial_Number;
1084
1085    ---------------
1086    -- Tree_Read --
1087    ---------------
1088
1089    procedure Tree_Read is
1090       N : Nat;
1091       S : String_Ptr;
1092
1093    begin
1094       Units.Tree_Read;
1095
1096       --  Read Compilation_Switches table. First release the memory occupied
1097       --  by the previously loaded switches.
1098
1099       for J in Compilation_Switches.First .. Compilation_Switches.Last loop
1100          Free (Compilation_Switches.Table (J));
1101       end loop;
1102
1103       Tree_Read_Int (N);
1104       Compilation_Switches.Set_Last (N);
1105
1106       for J in 1 .. N loop
1107          Tree_Read_Str (S);
1108          Compilation_Switches.Table (J) := S;
1109       end loop;
1110    end Tree_Read;
1111
1112    ----------------
1113    -- Tree_Write --
1114    ----------------
1115
1116    procedure Tree_Write is
1117    begin
1118       Units.Tree_Write;
1119
1120       --  Write Compilation_Switches table
1121
1122       Tree_Write_Int (Compilation_Switches.Last);
1123
1124       for J in 1 .. Compilation_Switches.Last loop
1125          Tree_Write_Str (Compilation_Switches.Table (J));
1126       end loop;
1127    end Tree_Write;
1128
1129    ------------
1130    -- Unlock --
1131    ------------
1132
1133    procedure Unlock is
1134    begin
1135       Linker_Option_Lines.Locked := False;
1136       Load_Stack.Locked := False;
1137       Units.Locked := False;
1138    end Unlock;
1139
1140    -----------------
1141    -- Version_Get --
1142    -----------------
1143
1144    function Version_Get (U : Unit_Number_Type) return Word_Hex_String is
1145    begin
1146       return Get_Hex_String (Units.Table (U).Version);
1147    end Version_Get;
1148
1149    ------------------------
1150    -- Version_Referenced --
1151    ------------------------
1152
1153    procedure Version_Referenced (S : String_Id) is
1154    begin
1155       Version_Ref.Append (S);
1156    end Version_Referenced;
1157
1158 end Lib;