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