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