OSDN Git Service

* sysdep.c: Problem discovered during IA64 VMS port.
[pf3gnuchains/gcc-fork.git] / gcc / ada / sinput-c.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             S I N P U T . P                              --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2003 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 -- GNAT was originally developed  by the GNAT team at  New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
24 --                                                                          --
25 ------------------------------------------------------------------------------
26
27 with Ada.Unchecked_Conversion;
28
29 with GNAT.OS_Lib; use GNAT.OS_Lib;
30 with Namet;       use Namet;
31 with Opt;         use Opt;
32 with System;      use System;
33
34 package body Sinput.C is
35
36    ---------------
37    -- Load_File --
38    ---------------
39
40    function Load_File (Path : String) return Source_File_Index is
41       Src  : Source_Buffer_Ptr;
42       X    : Source_File_Index;
43       Lo   : Source_Ptr;
44       Hi   : Source_Ptr;
45
46       Source_File_FD : File_Descriptor;
47       --  The file descriptor for the current source file. A negative value
48       --  indicates failure to open the specified source file.
49
50       Len : Integer;
51       --  Length of file. Assume no more than 2 gigabytes of source!
52
53       Actual_Len : Integer;
54
55       Path_Id : Name_Id;
56       File_Id : Name_Id;
57
58    begin
59       if Path = "" then
60          return No_Source_File;
61       end if;
62
63       Source_File.Increment_Last;
64       X := Source_File.Last;
65
66       if X = Source_File.First then
67          Lo := First_Source_Ptr;
68       else
69          Lo := Source_File.Table (X - 1).Source_Last + 1;
70       end if;
71
72       Name_Len := Path'Length;
73       Name_Buffer (1 .. Name_Len) := Path;
74       Path_Id := Name_Find;
75       Name_Buffer (Name_Len + 1) := ASCII.NUL;
76
77       --  Open the source FD, note that we open in binary mode, because as
78       --  documented in the spec, the caller is expected to handle either
79       --  DOS or Unix mode files, and there is no point in wasting time on
80       --  text translation when it is not required.
81
82       Source_File_FD := Open_Read (Name_Buffer'Address, Binary);
83
84       if Source_File_FD = Invalid_FD then
85          Source_File.Decrement_Last;
86          return No_Source_File;
87
88       end if;
89
90       Len := Integer (File_Length (Source_File_FD));
91
92       --  Set Hi so that length is one more than the physical length,
93       --  allowing for the extra EOF character at the end of the buffer
94
95       Hi := Lo + Source_Ptr (Len);
96
97       --  Do the actual read operation
98
99       declare
100          subtype Actual_Source_Buffer is Source_Buffer (Lo .. Hi);
101          --  Physical buffer allocated
102
103          type Actual_Source_Ptr is access Actual_Source_Buffer;
104          --  This is the pointer type for the physical buffer allocated
105
106          Actual_Ptr : Actual_Source_Ptr := new Actual_Source_Buffer;
107          --  And this is the actual physical buffer
108
109       begin
110          --  Allocate source buffer, allowing extra character at end for EOF
111
112          --  Some systems (e.g. VMS) have file types that require one
113          --  read per line, so read until we get the Len bytes or until
114          --  there are no more characters.
115
116          Hi := Lo;
117          loop
118             Actual_Len := Read (Source_File_FD, Actual_Ptr (Hi)'Address, Len);
119             Hi := Hi + Source_Ptr (Actual_Len);
120             exit when Actual_Len = Len or Actual_Len <= 0;
121          end loop;
122
123          Actual_Ptr (Hi) := EOF;
124
125          --  Now we need to work out the proper virtual origin pointer to
126          --  return. This is exactly Actual_Ptr (0)'Address, but we have
127          --  to be careful to suppress checks to compute this address.
128
129          declare
130             pragma Suppress (All_Checks);
131
132             function To_Source_Buffer_Ptr is new
133               Ada.Unchecked_Conversion (Address, Source_Buffer_Ptr);
134
135          begin
136             Src := To_Source_Buffer_Ptr (Actual_Ptr (0)'Address);
137          end;
138       end;
139
140       --  Read is complete, close the file and we are done (no need to test
141       --  status from close, since we have successfully read the file!)
142
143       Close (Source_File_FD);
144
145       --  Get the file name, without path information
146
147       declare
148          Index : Positive := Path'Last;
149
150       begin
151          while Index > Path'First loop
152             exit when Path (Index - 1) = '/';
153             exit when Path (Index - 1) = Directory_Separator;
154             Index := Index - 1;
155          end loop;
156
157          Name_Len := Path'Last - Index + 1;
158          Name_Buffer (1 .. Name_Len) := Path (Index .. Path'Last);
159          File_Id := Name_Find;
160       end;
161
162       declare
163          S : Source_File_Record renames Source_File.Table (X);
164
165       begin
166          S := (Debug_Source_Name   => File_Id,
167                File_Name           => File_Id,
168                File_Type           => Config,
169                First_Mapped_Line   => No_Line_Number,
170                Full_Debug_Name     => Path_Id,
171                Full_File_Name      => Path_Id,
172                Full_Ref_Name       => Path_Id,
173                Identifier_Casing   => Unknown,
174                Inlined_Body        => False,
175                Instantiation       => No_Location,
176                Keyword_Casing      => Unknown,
177                Last_Source_Line    => 1,
178                License             => Unknown,
179                Lines_Table         => null,
180                Lines_Table_Max     => 1,
181                Logical_Lines_Table => null,
182                Num_SRef_Pragmas    => 0,
183                Reference_Name      => File_Id,
184                Sloc_Adjust         => 0,
185                Source_Checksum     => 0,
186                Source_First        => Lo,
187                Source_Last         => Hi,
188                Source_Text         => Src,
189                Template            => No_Source_File,
190                Time_Stamp          => Empty_Time_Stamp);
191
192          Alloc_Line_Tables (S, Opt.Table_Factor * Alloc.Lines_Initial);
193          S.Lines_Table (1) := Lo;
194       end;
195
196       Set_Source_File_Index_Table (X);
197       return X;
198    end Load_File;
199
200 end Sinput.C;