1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
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. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Ada
.Unchecked_Conversion
;
29 with GNAT
.OS_Lib
; use GNAT
.OS_Lib
;
30 with Namet
; use Namet
;
32 with System
; use System
;
34 package body Sinput
.P
is
36 First
: Boolean := True;
37 -- Flag used when Load_Project_File is called the first time,
38 -- to set Main_Source_File.
39 -- The flag is reset to False at the first call to Load_Project_File
41 -----------------------
42 -- Load_Project_File --
43 -----------------------
45 function Load_Project_File
(Path
: String) return Source_File_Index
is
46 Src
: Source_Buffer_Ptr
;
47 X
: Source_File_Index
;
51 Source_File_FD
: File_Descriptor
;
52 -- The file descriptor for the current source file. A negative value
53 -- indicates failure to open the specified source file.
56 -- Length of file. Assume no more than 2 gigabytes of source!
65 return No_Source_File
;
68 Source_File
.Increment_Last
;
69 X
:= Source_File
.Last
;
72 Main_Source_File
:= X
;
76 if X
= Source_File
.First
then
77 Lo
:= First_Source_Ptr
;
79 Lo
:= Source_File
.Table
(X
- 1).Source_Last
+ 1;
82 Name_Len
:= Path
'Length;
83 Name_Buffer
(1 .. Name_Len
) := Path
;
85 Name_Buffer
(Name_Len
+ 1) := ASCII
.NUL
;
87 -- Open the source FD, note that we open in binary mode, because as
88 -- documented in the spec, the caller is expected to handle either
89 -- DOS or Unix mode files, and there is no point in wasting time on
90 -- text translation when it is not required.
92 Source_File_FD
:= Open_Read
(Name_Buffer
'Address, Binary
);
94 if Source_File_FD
= Invalid_FD
then
95 Source_File
.Decrement_Last
;
96 return No_Source_File
;
100 Len
:= Integer (File_Length
(Source_File_FD
));
102 -- Set Hi so that length is one more than the physical length,
103 -- allowing for the extra EOF character at the end of the buffer
105 Hi
:= Lo
+ Source_Ptr
(Len
);
107 -- Do the actual read operation
110 subtype Actual_Source_Buffer
is Source_Buffer
(Lo
.. Hi
);
111 -- Physical buffer allocated
113 type Actual_Source_Ptr
is access Actual_Source_Buffer
;
114 -- This is the pointer type for the physical buffer allocated
116 Actual_Ptr
: Actual_Source_Ptr
:= new Actual_Source_Buffer
;
117 -- And this is the actual physical buffer
120 -- Allocate source buffer, allowing extra character at end for EOF
122 -- Some systems (e.g. VMS) have file types that require one
123 -- read per line, so read until we get the Len bytes or until
124 -- there are no more characters.
128 Actual_Len
:= Read
(Source_File_FD
, Actual_Ptr
(Hi
)'Address, Len
);
129 Hi
:= Hi
+ Source_Ptr
(Actual_Len
);
130 exit when Actual_Len
= Len
or Actual_Len
<= 0;
133 Actual_Ptr
(Hi
) := EOF
;
135 -- Now we need to work out the proper virtual origin pointer to
136 -- return. This is exactly Actual_Ptr (0)'Address, but we have
137 -- to be careful to suppress checks to compute this address.
140 pragma Suppress
(All_Checks
);
142 function To_Source_Buffer_Ptr
is new
143 Ada
.Unchecked_Conversion
(Address
, Source_Buffer_Ptr
);
146 Src
:= To_Source_Buffer_Ptr
(Actual_Ptr
(0)'Address);
150 -- Read is complete, get time stamp and close file and we are done
152 Close
(Source_File_FD
);
154 -- Get the file name, without path information
157 Index
: Positive := Path
'Last;
160 while Index
> Path
'First loop
161 exit when Path
(Index
- 1) = '/';
162 exit when Path
(Index
- 1) = Directory_Separator
;
166 Name_Len
:= Path
'Last - Index
+ 1;
167 Name_Buffer
(1 .. Name_Len
) := Path
(Index
.. Path
'Last);
168 File_Id
:= Name_Find
;
172 S
: Source_File_Record
renames Source_File
.Table
(X
);
175 S
:= (Debug_Source_Name
=> Path_Id
,
176 File_Name
=> File_Id
,
177 First_Mapped_Line
=> No_Line_Number
,
178 Full_File_Name
=> Path_Id
,
179 Full_Ref_Name
=> Path_Id
,
180 Identifier_Casing
=> Unknown
,
181 Instantiation
=> No_Location
,
182 Keyword_Casing
=> Unknown
,
183 Last_Source_Line
=> 1,
186 Lines_Table_Max
=> 1,
187 Logical_Lines_Table
=> null,
188 Num_SRef_Pragmas
=> 0,
189 Reference_Name
=> File_Id
,
191 Source_Checksum
=> 0,
195 Template
=> No_Source_File
,
196 Time_Stamp
=> Empty_Time_Stamp
);
198 Alloc_Line_Tables
(S
, Opt
.Table_Factor
* Alloc
.Lines_Initial
);
199 S
.Lines_Table
(1) := Lo
;
203 end Load_Project_File
;
205 --------------------------------
206 -- Restore_Project_Scan_State --
207 --------------------------------
209 procedure Restore_Project_Scan_State
210 (Saved_State
: in Saved_Project_Scan_State
)
213 Restore_Scan_State
(Saved_State
.Scan_State
);
214 Source
:= Saved_State
.Source
;
215 Current_Source_File
:= Saved_State
.Current_Source_File
;
216 end Restore_Project_Scan_State
;
218 -----------------------------
219 -- Save_Project_Scan_State --
220 -----------------------------
222 procedure Save_Project_Scan_State
223 (Saved_State
: out Saved_Project_Scan_State
)
226 Save_Scan_State
(Saved_State
.Scan_State
);
227 Saved_State
.Source
:= Source
;
228 Saved_State
.Current_Source_File
:= Current_Source_File
;
229 end Save_Project_Scan_State
;