1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT COMPILER COMPONENTS                         --
4--                                                                          --
5--                             S I N P U T . D                              --
6--                                                                          --
7--                                 B o d y                                  --
8--                                                                          --
9--          Copyright (C) 2002-2013, 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.  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 COPYING3.  If not, go to --
19-- http://www.gnu.org/licenses for a complete copy of the license.          --
20--                                                                          --
21-- GNAT was originally developed  by the GNAT team at  New York University. --
22-- Extensive contributions were provided by Ada Core Technologies Inc.      --
23--                                                                          --
24------------------------------------------------------------------------------
25
26with Osint;   use Osint;
27with Osint.C; use Osint.C;
28
29package body Sinput.D is
30
31   Dfile : Source_File_Index;
32   --  Index of currently active debug source file
33
34   ------------------------
35   -- Close_Debug_Source --
36   ------------------------
37
38   procedure Close_Debug_Source is
39      S    : Source_File_Record renames Source_File.Table (Dfile);
40      Src  : Source_Buffer_Ptr;
41
42      pragma Warnings (Off, S);
43
44   begin
45      Trim_Lines_Table (Dfile);
46      Close_Debug_File;
47
48      --  Now we need to read the file that we wrote and store it in memory for
49      --  subsequent access.
50
51      Read_Source_File
52        (S.Full_Debug_Name, S.Source_First, S.Source_Last, Src);
53      S.Source_Text := Src;
54   end Close_Debug_Source;
55
56   -------------------------
57   -- Create_Debug_Source --
58   -------------------------
59
60   procedure Create_Debug_Source
61     (Source : Source_File_Index;
62      Loc    : out Source_Ptr)
63   is
64   begin
65      Loc :=
66        ((Source_File.Table (Source_File.Last).Source_Last + Source_Align) /
67           Source_Align) * Source_Align;
68      Source_File.Append (Source_File.Table (Source));
69      Dfile := Source_File.Last;
70
71      declare
72         S : Source_File_Record renames Source_File.Table (Dfile);
73
74      begin
75         S.Full_Debug_Name   := Create_Debug_File (S.File_Name);
76         S.Debug_Source_Name := Strip_Directory (S.Full_Debug_Name);
77         S.Source_First      := Loc;
78         S.Source_Last       := Loc;
79         S.Lines_Table       := null;
80         S.Last_Source_Line  := 1;
81
82         --  Allocate lines table, guess that it needs to be three times bigger
83         --  than the original source (in number of lines).
84
85         Alloc_Line_Tables
86           (S, Int (Source_File.Table (Source).Last_Source_Line * 3));
87         S.Lines_Table (1) := Loc;
88      end;
89   end Create_Debug_Source;
90
91   ----------------------
92   -- Write_Debug_Line --
93   ----------------------
94
95   procedure Write_Debug_Line (Str : String; Loc : in out Source_Ptr) is
96      S : Source_File_Record renames Source_File.Table (Dfile);
97
98   begin
99      --  Ignore write request if null line at start of file
100
101      if Str'Length = 0 and then Loc = S.Source_First then
102         return;
103
104      --  Here we write the line, compute the source location for the following
105      --  line, allocate its table entry, and update the source record entry.
106
107      else
108         Write_Debug_Info (Str (Str'First .. Str'Last - 1));
109         Loc := Loc - 1 + Source_Ptr (Str'Length + Debug_File_Eol_Length);
110         Add_Line_Tables_Entry (S, Loc);
111         S.Source_Last := Loc;
112         Set_Source_File_Index_Table (Dfile);
113      end if;
114   end Write_Debug_Line;
115
116end Sinput.D;
117