1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2014, 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 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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
27 with System
; use System
;
29 with Ada
.Unchecked_Conversion
;
31 pragma Warnings
(Off
);
32 -- This package is used also by gnatcoll
33 with System
.OS_Lib
; use System
.OS_Lib
;
36 package body Sinput
.C
is
42 function Load_File
(Path
: String) return Source_File_Index
is
43 Src
: Source_Buffer_Ptr
;
44 X
: Source_File_Index
;
48 Source_File_FD
: File_Descriptor
;
49 -- The file descriptor for the current source file. A negative value
50 -- indicates failure to open the specified source file.
53 -- Length of file (assume no more than 2 gigabytes of source)
57 Path_Id
: File_Name_Type
;
58 File_Id
: File_Name_Type
;
62 return No_Source_File
;
65 Source_File
.Increment_Last
;
66 X
:= Source_File
.Last
;
68 if X
= Source_File
.First
then
69 Lo
:= First_Source_Ptr
;
71 Lo
:= ((Source_File
.Table
(X
- 1).Source_Last
+ Source_Align
) /
72 Source_Align
) * Source_Align
;
75 Name_Len
:= Path
'Length;
76 Name_Buffer
(1 .. Name_Len
) := Path
;
78 Name_Buffer
(Name_Len
+ 1) := ASCII
.NUL
;
80 -- Open the source FD, note that we open in binary mode, because as
81 -- documented in the spec, the caller is expected to handle either
82 -- DOS or Unix mode files, and there is no point in wasting time on
83 -- text translation when it is not required.
85 Source_File_FD
:= Open_Read
(Name_Buffer
'Address, Binary
);
87 if Source_File_FD
= Invalid_FD
then
88 Source_File
.Decrement_Last
;
89 return No_Source_File
;
93 Len
:= Integer (File_Length
(Source_File_FD
));
95 -- Set Hi so that length is one more than the physical length, allowing
96 -- for the extra EOF character at the end of the buffer
98 Hi
:= Lo
+ Source_Ptr
(Len
);
100 -- Do the actual read operation
103 subtype Actual_Source_Buffer
is Source_Buffer
(Lo
.. Hi
);
104 -- Physical buffer allocated
106 type Actual_Source_Ptr
is access Actual_Source_Buffer
;
107 -- This is the pointer type for the physical buffer allocated
109 Actual_Ptr
: constant Actual_Source_Ptr
:= new Actual_Source_Buffer
;
110 -- And this is the actual physical buffer
113 -- Allocate source buffer, allowing extra character at end for EOF
115 -- Some systems have file types that require one read per line,
116 -- so read until we get the Len bytes or until there are no more
121 Actual_Len
:= Read
(Source_File_FD
, Actual_Ptr
(Hi
)'Address, Len
);
122 Hi
:= Hi
+ Source_Ptr
(Actual_Len
);
123 exit when Actual_Len
= Len
or else Actual_Len
<= 0;
126 Actual_Ptr
(Hi
) := EOF
;
128 -- Now we need to work out the proper virtual origin pointer to
129 -- return. This is exactly Actual_Ptr (0)'Address, but we have to
130 -- be careful to suppress checks to compute this address.
133 pragma Suppress
(All_Checks
);
135 pragma Warnings
(Off
);
136 -- The following unchecked conversion is aliased safe, since it
137 -- is not used to create improperly aliased pointer values.
139 function To_Source_Buffer_Ptr
is new
140 Ada
.Unchecked_Conversion
(Address
, Source_Buffer_Ptr
);
142 pragma Warnings
(On
);
145 Src
:= To_Source_Buffer_Ptr
(Actual_Ptr
(0)'Address);
149 -- Read is complete, close the file and we are done (no need to test
150 -- status from close, since we have successfully read the file).
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
=> File_Id
,
176 File_Name
=> File_Id
,
178 First_Mapped_Line
=> No_Line_Number
,
179 Full_Debug_Name
=> Path_Id
,
180 Full_File_Name
=> Path_Id
,
181 Full_Ref_Name
=> Path_Id
,
182 Instance
=> No_Instance_Id
,
183 Identifier_Casing
=> Unknown
,
184 Inlined_Call
=> No_Location
,
185 Inlined_Body
=> False,
186 Keyword_Casing
=> Unknown
,
187 Last_Source_Line
=> 1,
190 Lines_Table_Max
=> 1,
191 Logical_Lines_Table
=> null,
192 Num_SRef_Pragmas
=> 0,
193 Reference_Name
=> File_Id
,
195 Source_Checksum
=> 0,
199 Template
=> No_Source_File
,
201 Time_Stamp
=> Empty_Time_Stamp
);
203 Alloc_Line_Tables
(S
, Opt
.Table_Factor
* Alloc
.Lines_Initial
);
204 S
.Lines_Table
(1) := Lo
;
207 Set_Source_File_Index_Table
(X
);