Fix date
[official-gcc.git] / gcc / ada / sinput-c.adb
blobfe9285c873e265722005425f310d32ceff9f739a
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-2017, 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 Debug; use Debug;
27 with Opt; use Opt;
28 with Output; use Output;
29 with System; use System;
31 with Ada.Unchecked_Conversion;
33 pragma Warnings (Off);
34 -- This package is used also by gnatcoll
35 with System.OS_Lib; use System.OS_Lib;
36 pragma Warnings (On);
38 package body Sinput.C is
40 ---------------
41 -- Load_File --
42 ---------------
44 function Load_File (Path : String) return Source_File_Index is
45 Src : Source_Buffer_Ptr;
46 X : Source_File_Index;
47 Lo : Source_Ptr;
48 Hi : Source_Ptr;
50 Source_File_FD : File_Descriptor;
51 -- The file descriptor for the current source file. A negative value
52 -- indicates failure to open the specified source file.
54 Len : Integer;
55 -- Length of file (assume no more than 2 gigabytes of source)
57 Actual_Len : Integer;
59 Path_Id : File_Name_Type;
60 File_Id : File_Name_Type;
62 begin
63 if Path = "" then
64 return No_Source_File;
65 end if;
67 Source_File.Increment_Last;
68 X := Source_File.Last;
70 if Debug_Flag_L then
71 Write_Str ("Sinput.C.Load_File: created source ");
72 Write_Int (Int (X));
73 Write_Str (" for ");
74 Write_Str (Path);
75 Write_Line ("");
76 end if;
78 if X = Source_File.First then
79 Lo := First_Source_Ptr;
80 else
81 Lo := ((Source_File.Table (X - 1).Source_Last + Source_Align) /
82 Source_Align) * Source_Align;
83 end if;
85 Name_Len := Path'Length;
86 Name_Buffer (1 .. Name_Len) := Path;
87 Path_Id := Name_Find;
88 Name_Buffer (Name_Len + 1) := ASCII.NUL;
90 -- Open the source FD, note that we open in binary mode, because as
91 -- documented in the spec, the caller is expected to handle either
92 -- DOS or Unix mode files, and there is no point in wasting time on
93 -- text translation when it is not required.
95 Source_File_FD := Open_Read (Name_Buffer'Address, Binary);
97 if Source_File_FD = Invalid_FD then
98 Source_File.Decrement_Last;
99 return No_Source_File;
101 end if;
103 Len := Integer (File_Length (Source_File_FD));
105 -- Set Hi so that length is one more than the physical length, allowing
106 -- for the extra EOF character at the end of the buffer
108 Hi := Lo + Source_Ptr (Len);
110 -- Do the actual read operation
112 declare
113 Var_Ptr : constant Source_Buffer_Ptr_Var :=
114 new Source_Buffer (Lo .. Hi);
115 -- Allocate source buffer, allowing extra character at end for EOF
117 begin
118 -- Some systems have file types that require one read per line,
119 -- so read until we get the Len bytes or until there are no more
120 -- characters.
122 Hi := Lo;
123 loop
124 Actual_Len := Read (Source_File_FD, Var_Ptr (Hi)'Address, Len);
125 Hi := Hi + Source_Ptr (Actual_Len);
126 exit when Actual_Len = Len or else Actual_Len <= 0;
127 end loop;
129 Var_Ptr (Hi) := EOF;
130 Src := Var_Ptr.all'Access;
131 end;
133 -- Read is complete, close the file and we are done (no need to test
134 -- status from close, since we have successfully read the file).
136 Close (Source_File_FD);
138 -- Get the file name, without path information
140 declare
141 Index : Positive := Path'Last;
143 begin
144 while Index > Path'First loop
145 exit when Path (Index - 1) = '/';
146 exit when Path (Index - 1) = Directory_Separator;
147 Index := Index - 1;
148 end loop;
150 Name_Len := Path'Last - Index + 1;
151 Name_Buffer (1 .. Name_Len) := Path (Index .. Path'Last);
152 File_Id := Name_Find;
153 end;
155 declare
156 S : Source_File_Record renames Source_File.Table (X);
158 begin
159 S := (Debug_Source_Name => File_Id,
160 File_Name => File_Id,
161 File_Type => Config,
162 First_Mapped_Line => No_Line_Number,
163 Full_Debug_Name => Path_Id,
164 Full_File_Name => Path_Id,
165 Full_Ref_Name => Path_Id,
166 Instance => No_Instance_Id,
167 Identifier_Casing => Unknown,
168 Inlined_Call => No_Location,
169 Inlined_Body => False,
170 Inherited_Pragma => False,
171 Keyword_Casing => Unknown,
172 Last_Source_Line => 1,
173 License => Unknown,
174 Lines_Table => null,
175 Lines_Table_Max => 1,
176 Logical_Lines_Table => null,
177 Num_SRef_Pragmas => 0,
178 Reference_Name => File_Id,
179 Sloc_Adjust => 0,
180 Source_Checksum => 0,
181 Source_First => Lo,
182 Source_Last => Hi,
183 Source_Text => Src,
184 Template => No_Source_File,
185 Unit => No_Unit,
186 Time_Stamp => Empty_Time_Stamp,
187 Index => X);
189 Alloc_Line_Tables (S, Opt.Table_Factor * Alloc.Lines_Initial);
190 S.Lines_Table (1) := Lo;
191 end;
193 Set_Source_File_Index_Table (X);
194 return X;
195 end Load_File;
197 end Sinput.C;