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