OSDN Git Service

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