OSDN Git Service

2005-06-15 Andrew Pinski <pinskia@physics.uc.edu>
[pf3gnuchains/gcc-fork.git] / gcc / ada / sinput.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                               S I N P U T                                --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2005 Free Software Foundation, Inc.          --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
20 -- MA 02111-1307, USA.                                                      --
21 --                                                                          --
22 -- As a special exception,  if other files  instantiate  generics from this --
23 -- unit, or you link  this unit with other files  to produce an executable, --
24 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
25 -- covered  by the  GNU  General  Public  License.  This exception does not --
26 -- however invalidate  any other reasons why  the executable file  might be --
27 -- covered by the  GNU Public License.                                      --
28 --                                                                          --
29 -- GNAT was originally developed  by the GNAT team at  New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 pragma Style_Checks (All_Checks);
35 --  Subprograms not all in alpha order
36
37 with Debug;    use Debug;
38 with Namet;    use Namet;
39 with Opt;      use Opt;
40 with Output;   use Output;
41 with Tree_IO;  use Tree_IO;
42 with System;   use System;
43 with Widechar; use Widechar;
44
45 with System.Memory;
46
47 with Unchecked_Conversion;
48 with Unchecked_Deallocation;
49
50 package body Sinput is
51
52    use ASCII;
53    --  Make control characters visible
54
55    First_Time_Around : Boolean := True;
56
57    --  Routines to support conversion between types Lines_Table_Ptr,
58    --  Logical_Lines_Table_Ptr and System.Address.
59
60    pragma Warnings (Off);
61    --  These unchecked conversions are aliasing safe, since they are never
62    --  used to construct improperly aliased pointer values.
63
64    function To_Address is
65      new Unchecked_Conversion (Lines_Table_Ptr, Address);
66
67    function To_Address is
68      new Unchecked_Conversion (Logical_Lines_Table_Ptr, Address);
69
70    function To_Pointer is
71      new Unchecked_Conversion (Address, Lines_Table_Ptr);
72
73    function To_Pointer is
74      new Unchecked_Conversion (Address, Logical_Lines_Table_Ptr);
75
76    pragma Warnings (On);
77
78    ---------------------------
79    -- Add_Line_Tables_Entry --
80    ---------------------------
81
82    procedure Add_Line_Tables_Entry
83      (S : in out Source_File_Record;
84       P : Source_Ptr)
85    is
86       LL : Physical_Line_Number;
87
88    begin
89       --  Reallocate the lines tables if necessary.
90
91       --  Note: the reason we do not use the normal Table package
92       --  mechanism is that we have several of these tables. We could
93       --  use the new GNAT.Dynamic_Tables package and that would probably
94       --  be a good idea ???
95
96       if S.Last_Source_Line = S.Lines_Table_Max then
97          Alloc_Line_Tables
98            (S,
99             Int (S.Last_Source_Line) *
100               ((100 + Alloc.Lines_Increment) / 100));
101
102          if Debug_Flag_D then
103             Write_Str ("--> Reallocating lines table, size = ");
104             Write_Int (Int (S.Lines_Table_Max));
105             Write_Eol;
106          end if;
107       end if;
108
109       S.Last_Source_Line := S.Last_Source_Line + 1;
110       LL := S.Last_Source_Line;
111
112       S.Lines_Table (LL) := P;
113
114       --  Deal with setting new entry in logical lines table if one is
115       --  present. Note that there is always space (because the call to
116       --  Alloc_Line_Tables makes sure both tables are the same length),
117
118       if S.Logical_Lines_Table /= null then
119
120          --  We can always set the entry from the previous one, because
121          --  the processing for a Source_Reference pragma ensures that
122          --  at least one entry following the pragma is set up correctly.
123
124          S.Logical_Lines_Table (LL) := S.Logical_Lines_Table (LL - 1) + 1;
125       end if;
126    end Add_Line_Tables_Entry;
127
128    -----------------------
129    -- Alloc_Line_Tables --
130    -----------------------
131
132    procedure Alloc_Line_Tables
133      (S       : in out Source_File_Record;
134       New_Max : Nat)
135    is
136       subtype size_t is Memory.size_t;
137
138       New_Table : Lines_Table_Ptr;
139
140       New_Logical_Table : Logical_Lines_Table_Ptr;
141
142       New_Size : constant size_t :=
143                    size_t (New_Max * Lines_Table_Type'Component_Size /
144                                                              Storage_Unit);
145
146    begin
147       if S.Lines_Table = null then
148          New_Table := To_Pointer (Memory.Alloc (New_Size));
149
150       else
151          New_Table :=
152            To_Pointer (Memory.Realloc (To_Address (S.Lines_Table), New_Size));
153       end if;
154
155       if New_Table = null then
156          raise Storage_Error;
157       else
158          S.Lines_Table     := New_Table;
159          S.Lines_Table_Max := Physical_Line_Number (New_Max);
160       end if;
161
162       if S.Num_SRef_Pragmas /= 0 then
163          if S.Logical_Lines_Table = null then
164             New_Logical_Table := To_Pointer (Memory.Alloc (New_Size));
165          else
166             New_Logical_Table := To_Pointer
167               (Memory.Realloc (To_Address (S.Logical_Lines_Table), New_Size));
168          end if;
169
170          if New_Logical_Table = null then
171             raise Storage_Error;
172          else
173             S.Logical_Lines_Table := New_Logical_Table;
174          end if;
175       end if;
176    end Alloc_Line_Tables;
177
178    -----------------
179    -- Backup_Line --
180    -----------------
181
182    procedure Backup_Line (P : in out Source_Ptr) is
183       Sindex : constant Source_File_Index := Get_Source_File_Index (P);
184       Src    : constant Source_Buffer_Ptr :=
185                  Source_File.Table (Sindex).Source_Text;
186       Sfirst : constant Source_Ptr :=
187                  Source_File.Table (Sindex).Source_First;
188
189    begin
190       P := P - 1;
191
192       if P = Sfirst then
193          return;
194       end if;
195
196       if Src (P) = CR then
197          if Src (P - 1) = LF then
198             P := P - 1;
199          end if;
200
201       else -- Src (P) = LF
202          if Src (P - 1) = CR then
203             P := P - 1;
204          end if;
205       end if;
206
207       --  Now find first character of the previous line
208
209       while P > Sfirst
210         and then Src (P - 1) /= LF
211         and then Src (P - 1) /= CR
212       loop
213          P := P - 1;
214       end loop;
215    end Backup_Line;
216
217    ---------------------------
218    -- Build_Location_String --
219    ---------------------------
220
221    procedure Build_Location_String (Loc : Source_Ptr) is
222       Ptr : Source_Ptr;
223
224    begin
225       Name_Len := 0;
226
227       --  Loop through instantiations
228
229       Ptr := Loc;
230       loop
231          Get_Name_String_And_Append
232            (Reference_Name (Get_Source_File_Index (Ptr)));
233          Add_Char_To_Name_Buffer (':');
234          Add_Nat_To_Name_Buffer
235            (Nat (Get_Logical_Line_Number (Ptr)));
236
237          Ptr := Instantiation_Location (Ptr);
238          exit when Ptr = No_Location;
239          Add_Str_To_Name_Buffer (" instantiated at ");
240       end loop;
241
242       Name_Buffer (Name_Len + 1) := NUL;
243       return;
244    end Build_Location_String;
245
246    -----------------------
247    -- Get_Column_Number --
248    -----------------------
249
250    function Get_Column_Number (P : Source_Ptr) return Column_Number is
251       S      : Source_Ptr;
252       C      : Column_Number;
253       Sindex : Source_File_Index;
254       Src    : Source_Buffer_Ptr;
255
256    begin
257       --  If the input source pointer is not a meaningful value then return
258       --  at once with column number 1. This can happen for a file not found
259       --  condition for a file loaded indirectly by RTE, and also perhaps on
260       --  some unknown internal error conditions. In either case we certainly
261       --  don't want to blow up.
262
263       if P < 1 then
264          return 1;
265
266       else
267          Sindex := Get_Source_File_Index (P);
268          Src := Source_File.Table (Sindex).Source_Text;
269          S := Line_Start (P);
270          C := 1;
271
272          while S < P loop
273             if Src (S) = HT then
274                C := (C - 1) / 8 * 8 + (8 + 1);
275             else
276                C := C + 1;
277             end if;
278
279             S := S + 1;
280          end loop;
281
282          return C;
283       end if;
284    end Get_Column_Number;
285
286    -----------------------------
287    -- Get_Logical_Line_Number --
288    -----------------------------
289
290    function Get_Logical_Line_Number
291      (P    : Source_Ptr)
292       return Logical_Line_Number
293    is
294       SFR : Source_File_Record
295               renames Source_File.Table (Get_Source_File_Index (P));
296
297       L : constant Physical_Line_Number := Get_Physical_Line_Number (P);
298
299    begin
300       if SFR.Num_SRef_Pragmas = 0 then
301          return Logical_Line_Number (L);
302       else
303          return SFR.Logical_Lines_Table (L);
304       end if;
305    end Get_Logical_Line_Number;
306
307    ------------------------------
308    -- Get_Physical_Line_Number --
309    ------------------------------
310
311    function Get_Physical_Line_Number
312      (P    : Source_Ptr)
313       return Physical_Line_Number
314    is
315       Sfile : Source_File_Index;
316       Table : Lines_Table_Ptr;
317       Lo    : Physical_Line_Number;
318       Hi    : Physical_Line_Number;
319       Mid   : Physical_Line_Number;
320       Loc   : Source_Ptr;
321
322    begin
323       --  If the input source pointer is not a meaningful value then return
324       --  at once with line number 1. This can happen for a file not found
325       --  condition for a file loaded indirectly by RTE, and also perhaps on
326       --  some unknown internal error conditions. In either case we certainly
327       --  don't want to blow up.
328
329       if P < 1 then
330          return 1;
331
332       --  Otherwise we can do the binary search
333
334       else
335          Sfile := Get_Source_File_Index (P);
336          Loc   := P + Source_File.Table (Sfile).Sloc_Adjust;
337          Table := Source_File.Table (Sfile).Lines_Table;
338          Lo    := 1;
339          Hi    := Source_File.Table (Sfile).Last_Source_Line;
340
341          loop
342             Mid := (Lo + Hi) / 2;
343
344             if Loc < Table (Mid) then
345                Hi := Mid - 1;
346
347             else -- Loc >= Table (Mid)
348
349                if Mid = Hi or else
350                   Loc < Table (Mid + 1)
351                then
352                   return Mid;
353                else
354                   Lo := Mid + 1;
355                end if;
356
357             end if;
358
359          end loop;
360       end if;
361    end Get_Physical_Line_Number;
362
363    ---------------------------
364    -- Get_Source_File_Index --
365    ---------------------------
366
367    Source_Cache_First : Source_Ptr := 1;
368    Source_Cache_Last  : Source_Ptr := 0;
369    --  Records the First and Last subscript values for the most recently
370    --  referenced entry in the source table, to optimize the common case
371    --  of repeated references to the same entry. The initial values force
372    --  an initial search to set the cache value.
373
374    Source_Cache_Index : Source_File_Index := No_Source_File;
375    --  Contains the index of the entry corresponding to Source_Cache
376
377    function Get_Source_File_Index
378      (S    : Source_Ptr)
379       return Source_File_Index
380    is
381    begin
382       if S in Source_Cache_First .. Source_Cache_Last then
383          return Source_Cache_Index;
384
385       else
386          for J in Source_File_Index_Table (Int (S) / Chunk_Size)
387                                                     .. Source_File.Last
388          loop
389             if S in Source_File.Table (J).Source_First ..
390                     Source_File.Table (J).Source_Last
391             then
392                Source_Cache_Index := J;
393                Source_Cache_First :=
394                  Source_File.Table (Source_Cache_Index).Source_First;
395                Source_Cache_Last :=
396                  Source_File.Table (Source_Cache_Index).Source_Last;
397                return Source_Cache_Index;
398             end if;
399          end loop;
400       end if;
401
402       --  We must find a matching entry in the above loop!
403
404       raise Program_Error;
405    end Get_Source_File_Index;
406
407    ----------------
408    -- Initialize --
409    ----------------
410
411    procedure Initialize is
412    begin
413       Source_Cache_First := 1;
414       Source_Cache_Last  := 0;
415       Source_Cache_Index := No_Source_File;
416       Source_gnat_adc    := No_Source_File;
417       First_Time_Around  := True;
418
419       Source_File.Init;
420    end Initialize;
421
422    -------------------------
423    -- Instantiation_Depth --
424    -------------------------
425
426    function Instantiation_Depth (S : Source_Ptr) return Nat is
427       Sind  : Source_File_Index;
428       Sval  : Source_Ptr;
429       Depth : Nat;
430
431    begin
432       Sval := S;
433       Depth := 0;
434
435       loop
436          Sind := Get_Source_File_Index (Sval);
437          Sval := Instantiation (Sind);
438          exit when Sval = No_Location;
439          Depth := Depth + 1;
440       end loop;
441
442       return Depth;
443    end Instantiation_Depth;
444
445    ----------------------------
446    -- Instantiation_Location --
447    ----------------------------
448
449    function Instantiation_Location (S : Source_Ptr) return Source_Ptr is
450    begin
451       return Instantiation (Get_Source_File_Index (S));
452    end Instantiation_Location;
453
454    ----------------------
455    -- Last_Source_File --
456    ----------------------
457
458    function Last_Source_File return Source_File_Index is
459    begin
460       return Source_File.Last;
461    end Last_Source_File;
462
463    ----------------
464    -- Line_Start --
465    ----------------
466
467    function Line_Start (P : Source_Ptr) return Source_Ptr is
468       Sindex : constant Source_File_Index := Get_Source_File_Index (P);
469       Src    : constant Source_Buffer_Ptr :=
470                  Source_File.Table (Sindex).Source_Text;
471       Sfirst : constant Source_Ptr :=
472                  Source_File.Table (Sindex).Source_First;
473       S      : Source_Ptr;
474
475    begin
476       S := P;
477
478       while S > Sfirst
479         and then Src (S - 1) /= CR
480         and then Src (S - 1) /= LF
481       loop
482          S := S - 1;
483       end loop;
484
485       return S;
486    end Line_Start;
487
488    function Line_Start
489      (L    : Physical_Line_Number;
490       S    : Source_File_Index)
491       return Source_Ptr
492    is
493    begin
494       return Source_File.Table (S).Lines_Table (L);
495    end Line_Start;
496
497    ----------
498    -- Lock --
499    ----------
500
501    procedure Lock is
502    begin
503       Source_File.Locked := True;
504       Source_File.Release;
505    end Lock;
506
507    ----------------------
508    -- Num_Source_Files --
509    ----------------------
510
511    function Num_Source_Files return Nat is
512    begin
513       return Int (Source_File.Last) - Int (Source_File.First) + 1;
514    end Num_Source_Files;
515
516    ----------------------
517    -- Num_Source_Lines --
518    ----------------------
519
520    function Num_Source_Lines (S : Source_File_Index) return Nat is
521    begin
522       return Nat (Source_File.Table (S).Last_Source_Line);
523    end Num_Source_Lines;
524
525    -----------------------
526    -- Original_Location --
527    -----------------------
528
529    function Original_Location (S : Source_Ptr) return Source_Ptr is
530       Sindex : Source_File_Index;
531       Tindex : Source_File_Index;
532
533    begin
534       if S <= No_Location then
535          return S;
536
537       else
538          Sindex := Get_Source_File_Index (S);
539
540          if Instantiation (Sindex) = No_Location then
541             return S;
542
543          else
544             Tindex := Template (Sindex);
545             while Instantiation (Tindex) /= No_Location loop
546                Tindex := Template (Tindex);
547             end loop;
548
549             return S - Source_First (Sindex) + Source_First (Tindex);
550          end if;
551       end if;
552    end Original_Location;
553
554    -------------------------
555    -- Physical_To_Logical --
556    -------------------------
557
558    function Physical_To_Logical
559      (Line : Physical_Line_Number;
560       S    : Source_File_Index)
561       return Logical_Line_Number
562    is
563       SFR : Source_File_Record renames Source_File.Table (S);
564
565    begin
566       if SFR.Num_SRef_Pragmas = 0 then
567          return Logical_Line_Number (Line);
568       else
569          return SFR.Logical_Lines_Table (Line);
570       end if;
571    end Physical_To_Logical;
572
573    --------------------------------
574    -- Register_Source_Ref_Pragma --
575    --------------------------------
576
577    procedure Register_Source_Ref_Pragma
578      (File_Name          : Name_Id;
579       Stripped_File_Name : Name_Id;
580       Mapped_Line        : Nat;
581       Line_After_Pragma  : Physical_Line_Number)
582    is
583       subtype size_t is Memory.size_t;
584
585       SFR : Source_File_Record renames Source_File.Table (Current_Source_File);
586
587       ML : Logical_Line_Number;
588
589    begin
590       if File_Name /= No_Name then
591          SFR.Reference_Name := Stripped_File_Name;
592          SFR.Full_Ref_Name  := File_Name;
593
594          if not Debug_Generated_Code then
595             SFR.Debug_Source_Name := Stripped_File_Name;
596             SFR.Full_Debug_Name   := File_Name;
597          end if;
598
599          SFR.Num_SRef_Pragmas := SFR.Num_SRef_Pragmas + 1;
600       end if;
601
602       if SFR.Num_SRef_Pragmas = 1 then
603          SFR.First_Mapped_Line := Logical_Line_Number (Mapped_Line);
604       end if;
605
606       if SFR.Logical_Lines_Table = null then
607          SFR.Logical_Lines_Table := To_Pointer
608            (Memory.Alloc
609              (size_t (SFR.Lines_Table_Max *
610                         Logical_Lines_Table_Type'Component_Size /
611                                                         Storage_Unit)));
612       end if;
613
614       SFR.Logical_Lines_Table (Line_After_Pragma - 1) := No_Line_Number;
615
616       ML := Logical_Line_Number (Mapped_Line);
617       for J in Line_After_Pragma .. SFR.Last_Source_Line loop
618          SFR.Logical_Lines_Table (J) := ML;
619          ML := ML + 1;
620       end loop;
621    end Register_Source_Ref_Pragma;
622
623    ---------------------------------
624    -- Set_Source_File_Index_Table --
625    ---------------------------------
626
627    procedure Set_Source_File_Index_Table (Xnew : Source_File_Index) is
628       Ind : Int;
629       SP  : Source_Ptr;
630       SL  : constant Source_Ptr := Source_File.Table (Xnew).Source_Last;
631
632    begin
633       SP  := (Source_File.Table (Xnew).Source_First + Chunk_Size - 1)
634                                                     / Chunk_Size * Chunk_Size;
635       Ind := Int (SP) / Chunk_Size;
636
637       while SP <= SL loop
638          Source_File_Index_Table (Ind) := Xnew;
639          SP := SP + Chunk_Size;
640          Ind := Ind + 1;
641       end loop;
642    end Set_Source_File_Index_Table;
643
644    ---------------------------
645    -- Skip_Line_Terminators --
646    ---------------------------
647
648    procedure Skip_Line_Terminators
649      (P        : in out Source_Ptr;
650       Physical : out Boolean)
651    is
652       Chr : constant Character := Source (P);
653
654    begin
655       if  Chr = CR then
656          if Source (P + 1) = LF then
657             P := P + 2;
658          else
659             P := P + 1;
660          end if;
661
662       elsif Chr = LF then
663          if Source (P) = CR then
664             P := P + 2;
665          else
666             P := P + 1;
667          end if;
668
669       elsif Chr = FF or else Chr = VT then
670          P := P + 1;
671          Physical := False;
672          return;
673
674          --  Otherwise we have a wide character
675
676       else
677          Skip_Wide (Source, P);
678       end if;
679
680       --  Fall through in the physical line terminator case. First deal with
681       --  making a possible entry into the lines table if one is needed.
682
683       --  Note: we are dealing with a real source file here, this cannot be
684       --  the instantiation case, so we need not worry about Sloc adjustment.
685
686       declare
687          S : Source_File_Record
688                renames Source_File.Table (Current_Source_File);
689
690       begin
691          Physical := True;
692
693          --  Make entry in lines table if not already made (in some scan backup
694          --  cases, we will be rescanning previously scanned source, so the
695          --  entry may have already been made on the previous forward scan).
696
697          if Source (P) /= EOF
698            and then P > S.Lines_Table (S.Last_Source_Line)
699          then
700             Add_Line_Tables_Entry (S, P);
701          end if;
702       end;
703    end Skip_Line_Terminators;
704
705    -------------------
706    -- Source_Offset --
707    -------------------
708
709    function Source_Offset (S : Source_Ptr) return Nat is
710       Sindex : constant Source_File_Index := Get_Source_File_Index (S);
711       Sfirst : constant Source_Ptr :=
712                  Source_File.Table (Sindex).Source_First;
713
714    begin
715       return Nat (S - Sfirst);
716    end Source_Offset;
717
718    ------------------------
719    -- Top_Level_Location --
720    ------------------------
721
722    function Top_Level_Location (S : Source_Ptr) return Source_Ptr is
723       Oldloc : Source_Ptr;
724       Newloc : Source_Ptr;
725
726    begin
727       Newloc := S;
728       loop
729          Oldloc := Newloc;
730          Newloc := Instantiation_Location (Oldloc);
731          exit when Newloc = No_Location;
732       end loop;
733
734       return Oldloc;
735    end Top_Level_Location;
736
737    ---------------
738    -- Tree_Read --
739    ---------------
740
741    procedure Tree_Read is
742    begin
743       --  First we must free any old source buffer pointers
744
745       if not First_Time_Around then
746          for J in Source_File.First .. Source_File.Last loop
747             declare
748                S : Source_File_Record renames Source_File.Table (J);
749
750                procedure Free_Ptr is new Unchecked_Deallocation
751                  (Big_Source_Buffer, Source_Buffer_Ptr);
752
753                pragma Warnings (Off);
754                --  This unchecked conversion is aliasing safe, since it is not
755                --  used to create improperly aliased pointer values.
756
757                function To_Source_Buffer_Ptr is new
758                  Unchecked_Conversion (Address, Source_Buffer_Ptr);
759
760                pragma Warnings (On);
761
762                Tmp1 : Source_Buffer_Ptr;
763
764             begin
765                if S.Instantiation /= No_Location then
766                   null;
767
768                else
769                   --  We have to recreate a proper pointer to the actual array
770                   --  from the zero origin pointer stored in the source table.
771
772                   Tmp1 :=
773                     To_Source_Buffer_Ptr
774                       (S.Source_Text (S.Source_First)'Address);
775                   Free_Ptr (Tmp1);
776
777                   --  Note: we are using free here, because we used malloc
778                   --  or realloc directly to allocate the tables. That is
779                   --  because we were playing the big array trick.
780
781                   if S.Lines_Table /= null then
782                      Memory.Free (To_Address (S.Lines_Table));
783                      S.Lines_Table := null;
784                   end if;
785
786                   if S.Logical_Lines_Table /= null then
787                      Memory.Free (To_Address (S.Logical_Lines_Table));
788                      S.Logical_Lines_Table := null;
789                   end if;
790                end if;
791             end;
792          end loop;
793       end if;
794
795       --  Reset source cache pointers to force new read
796
797       Source_Cache_First := 1;
798       Source_Cache_Last  := 0;
799
800       --  Read in source file table
801
802       Source_File.Tree_Read;
803
804       --  The pointers we read in there for the source buffer and lines
805       --  table pointers are junk. We now read in the actual data that
806       --  is referenced by these two fields.
807
808       for J in Source_File.First .. Source_File.Last loop
809          declare
810             S : Source_File_Record renames Source_File.Table (J);
811
812          begin
813             --  For the instantiation case, we do not read in any data. Instead
814             --  we share the data for the generic template entry. Since the
815             --  template always occurs first, we can safetly refer to its data.
816
817             if S.Instantiation /= No_Location then
818                declare
819                   ST : Source_File_Record renames
820                          Source_File.Table (S.Template);
821
822                begin
823                   --  The lines tables are copied from the template entry
824
825                   S.Lines_Table :=
826                     Source_File.Table (S.Template).Lines_Table;
827                   S.Logical_Lines_Table :=
828                     Source_File.Table (S.Template).Logical_Lines_Table;
829
830                   --  In the case of the source table pointer, we share the
831                   --  same data as the generic template, but the virtual origin
832                   --  is adjusted. For example, if the first subscript of the
833                   --  template is 100, and that of the instantiation is 200,
834                   --  then the instantiation pointer is obtained by subtracting
835                   --  100 from the template pointer.
836
837                   declare
838                      pragma Suppress (All_Checks);
839
840                      pragma Warnings (Off);
841                      --  This unchecked conversion is aliasing safe since it
842                      --  not used to create improperly aliased pointer values.
843
844                      function To_Source_Buffer_Ptr is new
845                        Unchecked_Conversion (Address, Source_Buffer_Ptr);
846
847                      pragma Warnings (On);
848
849                   begin
850                      S.Source_Text :=
851                        To_Source_Buffer_Ptr
852                           (ST.Source_Text
853                             (ST.Source_First - S.Source_First)'Address);
854                   end;
855                end;
856
857             --  Normal case (non-instantiation)
858
859             else
860                First_Time_Around := False;
861                S.Lines_Table := null;
862                S.Logical_Lines_Table := null;
863                Alloc_Line_Tables (S, Int (S.Last_Source_Line));
864
865                for J in 1 .. S.Last_Source_Line loop
866                   Tree_Read_Int (Int (S.Lines_Table (J)));
867                end loop;
868
869                if S.Num_SRef_Pragmas /= 0 then
870                   for J in 1 .. S.Last_Source_Line loop
871                      Tree_Read_Int (Int (S.Logical_Lines_Table (J)));
872                   end loop;
873                end if;
874
875                --  Allocate source buffer and read in the data and then set the
876                --  virtual origin to point to the logical zero'th element. This
877                --  address must be computed with subscript checks turned off.
878
879                declare
880                   subtype B is Text_Buffer (S.Source_First .. S.Source_Last);
881                   type Text_Buffer_Ptr is access B;
882                   T : Text_Buffer_Ptr;
883
884                   pragma Suppress (All_Checks);
885
886                   pragma Warnings (Off);
887                   --  This unchecked conversion is aliasing safe, since it is
888                   --  never used to create improperly aliased pointer values.
889
890                   function To_Source_Buffer_Ptr is new
891                     Unchecked_Conversion (Address, Source_Buffer_Ptr);
892
893                   pragma Warnings (On);
894
895                begin
896                   T := new B;
897
898                   Tree_Read_Data (T (S.Source_First)'Address,
899                      Int (S.Source_Last) - Int (S.Source_First) + 1);
900
901                   S.Source_Text := To_Source_Buffer_Ptr (T (0)'Address);
902                end;
903             end if;
904          end;
905
906          Set_Source_File_Index_Table (J);
907       end loop;
908    end Tree_Read;
909
910    ----------------
911    -- Tree_Write --
912    ----------------
913
914    procedure Tree_Write is
915    begin
916       Source_File.Tree_Write;
917
918       --  The pointers we wrote out there for the source buffer and lines
919       --  table pointers are junk, we now write out the actual data that
920       --  is referenced by these two fields.
921
922       for J in Source_File.First .. Source_File.Last loop
923          declare
924             S : Source_File_Record renames Source_File.Table (J);
925
926          begin
927             --  For instantiations, there is nothing to do, since the data is
928             --  shared with the generic template. When the tree is read, the
929             --  pointers must be set, but no extra data needs to be written.
930
931             if S.Instantiation /= No_Location then
932                null;
933
934             --  For the normal case, write out the data of the tables
935
936             else
937                --  Lines table
938
939                for J in 1 .. S.Last_Source_Line loop
940                   Tree_Write_Int (Int (S.Lines_Table (J)));
941                end loop;
942
943                --  Logical lines table if present
944
945                if S.Num_SRef_Pragmas /= 0 then
946                   for J in 1 .. S.Last_Source_Line loop
947                      Tree_Write_Int (Int (S.Logical_Lines_Table (J)));
948                   end loop;
949                end if;
950
951                --  Source buffer
952
953                Tree_Write_Data
954                  (S.Source_Text (S.Source_First)'Address,
955                    Int (S.Source_Last) - Int (S.Source_First) + 1);
956             end if;
957          end;
958       end loop;
959    end Tree_Write;
960
961    --------------------
962    -- Write_Location --
963    --------------------
964
965    procedure Write_Location (P : Source_Ptr) is
966    begin
967       if P = No_Location then
968          Write_Str ("<no location>");
969
970       elsif P <= Standard_Location then
971          Write_Str ("<standard location>");
972
973       else
974          declare
975             SI : constant Source_File_Index := Get_Source_File_Index (P);
976
977          begin
978             Write_Name (Debug_Source_Name (SI));
979             Write_Char (':');
980             Write_Int (Int (Get_Logical_Line_Number (P)));
981             Write_Char (':');
982             Write_Int (Int (Get_Column_Number (P)));
983
984             if Instantiation (SI) /= No_Location then
985                Write_Str (" [");
986                Write_Location (Instantiation (SI));
987                Write_Char (']');
988             end if;
989          end;
990       end if;
991    end Write_Location;
992
993    ----------------------
994    -- Write_Time_Stamp --
995    ----------------------
996
997    procedure Write_Time_Stamp (S : Source_File_Index) is
998       T : constant Time_Stamp_Type := Time_Stamp (S);
999       P : Natural;
1000
1001    begin
1002       if T (1) = '9' then
1003          Write_Str ("19");
1004          P := 0;
1005       else
1006          Write_Char (T (1));
1007          Write_Char (T (2));
1008          P := 2;
1009       end if;
1010
1011       Write_Char (T (P + 1));
1012       Write_Char (T (P + 2));
1013       Write_Char ('-');
1014
1015       Write_Char (T (P + 3));
1016       Write_Char (T (P + 4));
1017       Write_Char ('-');
1018
1019       Write_Char (T (P + 5));
1020       Write_Char (T (P + 6));
1021       Write_Char (' ');
1022
1023       Write_Char (T (P + 7));
1024       Write_Char (T (P + 8));
1025       Write_Char (':');
1026
1027       Write_Char (T (P + 9));
1028       Write_Char (T (P + 10));
1029       Write_Char (':');
1030
1031       Write_Char (T (P + 11));
1032       Write_Char (T (P + 12));
1033    end Write_Time_Stamp;
1034
1035    ----------------------------------------------
1036    -- Access Subprograms for Source File Table --
1037    ----------------------------------------------
1038
1039    function Debug_Source_Name (S : SFI) return File_Name_Type is
1040    begin
1041       return Source_File.Table (S).Debug_Source_Name;
1042    end Debug_Source_Name;
1043
1044    function File_Name (S : SFI) return File_Name_Type is
1045    begin
1046       return Source_File.Table (S).File_Name;
1047    end File_Name;
1048
1049    function File_Type (S : SFI) return Type_Of_File is
1050    begin
1051       return Source_File.Table (S).File_Type;
1052    end File_Type;
1053
1054    function First_Mapped_Line (S : SFI) return Logical_Line_Number is
1055    begin
1056       return Source_File.Table (S).First_Mapped_Line;
1057    end First_Mapped_Line;
1058
1059    function Full_Debug_Name (S : SFI) return File_Name_Type is
1060    begin
1061       return Source_File.Table (S).Full_Debug_Name;
1062    end Full_Debug_Name;
1063
1064    function Full_File_Name (S : SFI) return File_Name_Type is
1065    begin
1066       return Source_File.Table (S).Full_File_Name;
1067    end Full_File_Name;
1068
1069    function Full_Ref_Name (S : SFI) return File_Name_Type is
1070    begin
1071       return Source_File.Table (S).Full_Ref_Name;
1072    end Full_Ref_Name;
1073
1074    function Identifier_Casing (S : SFI) return Casing_Type is
1075    begin
1076       return Source_File.Table (S).Identifier_Casing;
1077    end Identifier_Casing;
1078
1079    function Inlined_Body (S : SFI) return Boolean is
1080    begin
1081       return Source_File.Table (S).Inlined_Body;
1082    end Inlined_Body;
1083
1084    function Instantiation (S : SFI) return Source_Ptr is
1085    begin
1086       return Source_File.Table (S).Instantiation;
1087    end Instantiation;
1088
1089    function Keyword_Casing (S : SFI) return Casing_Type is
1090    begin
1091       return Source_File.Table (S).Keyword_Casing;
1092    end Keyword_Casing;
1093
1094    function Last_Source_Line (S : SFI) return Physical_Line_Number is
1095    begin
1096       return Source_File.Table (S).Last_Source_Line;
1097    end Last_Source_Line;
1098
1099    function License (S : SFI) return License_Type is
1100    begin
1101       return Source_File.Table (S).License;
1102    end License;
1103
1104    function Num_SRef_Pragmas (S : SFI) return Nat is
1105    begin
1106       return Source_File.Table (S).Num_SRef_Pragmas;
1107    end Num_SRef_Pragmas;
1108
1109    function Reference_Name (S : SFI) return File_Name_Type is
1110    begin
1111       return Source_File.Table (S).Reference_Name;
1112    end Reference_Name;
1113
1114    function Source_Checksum (S : SFI) return Word is
1115    begin
1116       return Source_File.Table (S).Source_Checksum;
1117    end Source_Checksum;
1118
1119    function Source_First (S : SFI) return Source_Ptr is
1120    begin
1121       if S = Internal_Source_File then
1122          return Internal_Source'First;
1123       else
1124          return Source_File.Table (S).Source_First;
1125       end if;
1126    end Source_First;
1127
1128    function Source_Last (S : SFI) return Source_Ptr is
1129    begin
1130       if S = Internal_Source_File then
1131          return Internal_Source'Last;
1132       else
1133          return Source_File.Table (S).Source_Last;
1134       end if;
1135
1136    end Source_Last;
1137
1138    function Source_Text (S : SFI) return Source_Buffer_Ptr is
1139    begin
1140       if S = Internal_Source_File then
1141          return Internal_Source_Ptr;
1142       else
1143          return Source_File.Table (S).Source_Text;
1144       end if;
1145
1146    end Source_Text;
1147
1148    function Template (S : SFI) return SFI is
1149    begin
1150       return Source_File.Table (S).Template;
1151    end Template;
1152
1153    function Time_Stamp (S : SFI) return Time_Stamp_Type is
1154    begin
1155       return Source_File.Table (S).Time_Stamp;
1156    end Time_Stamp;
1157
1158    ------------------------------------------
1159    -- Set Procedures for Source File Table --
1160    ------------------------------------------
1161
1162    procedure Set_Identifier_Casing (S : SFI; C : Casing_Type) is
1163    begin
1164       Source_File.Table (S).Identifier_Casing := C;
1165    end Set_Identifier_Casing;
1166
1167    procedure Set_Keyword_Casing (S : SFI; C : Casing_Type) is
1168    begin
1169       Source_File.Table (S).Keyword_Casing := C;
1170    end Set_Keyword_Casing;
1171
1172    procedure Set_License (S : SFI; L : License_Type) is
1173    begin
1174       Source_File.Table (S).License := L;
1175    end Set_License;
1176
1177    ----------------------
1178    -- Trim_Lines_Table --
1179    ----------------------
1180
1181    procedure Trim_Lines_Table (S : Source_File_Index) is
1182       Max : constant Nat := Nat (Source_File.Table (S).Last_Source_Line);
1183
1184    begin
1185       --  Release allocated storage that is no longer needed
1186
1187       Source_File.Table (S).Lines_Table := To_Pointer
1188         (Memory.Realloc
1189           (To_Address (Source_File.Table (S).Lines_Table),
1190            Memory.size_t
1191             (Max * (Lines_Table_Type'Component_Size / System.Storage_Unit))));
1192       Source_File.Table (S).Lines_Table_Max := Physical_Line_Number (Max);
1193    end Trim_Lines_Table;
1194
1195    --------
1196    -- wl --
1197    --------
1198
1199    procedure wl (P : Source_Ptr) is
1200    begin
1201       Write_Location (P);
1202       Write_Eol;
1203    end wl;
1204
1205 end Sinput;