Patch to fix -mcpu=G5 interface to EH runtime library.
[official-gcc.git] / gcc / ada / scn.adb
blob5e8fbbf22988c3360eafd75b9c4160d0cd21c749
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S C N --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2004 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Csets; use Csets;
29 with Namet; use Namet;
30 with Opt; use Opt;
31 with Scans; use Scans;
32 with Sinfo; use Sinfo;
33 with Sinput; use Sinput;
35 package body Scn is
37 use ASCII;
39 Used_As_Identifier : array (Token_Type) of Boolean;
40 -- Flags set True if a given keyword is used as an identifier (used to
41 -- make sure that we only post an error message for incorrect use of a
42 -- keyword as an identifier once for a given keyword).
44 procedure Check_End_Of_Line;
45 -- Called when end of line encountered. Checks that line is not
46 -- too long, and that other style checks for the end of line are met.
48 function Determine_License return License_Type;
49 -- Scan header of file and check that it has an appropriate GNAT-style
50 -- header with a proper license statement. Returns GPL, Unrestricted,
51 -- or Modified_GPL depending on header. If none of these, returns Unknown.
53 procedure Error_Long_Line;
54 -- Signal error of excessively long line
56 ---------------
57 -- Post_Scan --
58 ---------------
60 procedure Post_Scan is
61 begin
62 case Token is
63 when Tok_Char_Literal =>
64 Token_Node := New_Node (N_Character_Literal, Token_Ptr);
65 Set_Char_Literal_Value (Token_Node, Character_Code);
66 Set_Chars (Token_Node, Token_Name);
68 when Tok_Identifier =>
69 Token_Node := New_Node (N_Identifier, Token_Ptr);
70 Set_Chars (Token_Node, Token_Name);
72 when Tok_Real_Literal =>
73 Token_Node := New_Node (N_Real_Literal, Token_Ptr);
74 Set_Realval (Token_Node, Real_Literal_Value);
76 when Tok_Integer_Literal =>
77 Token_Node := New_Node (N_Integer_Literal, Token_Ptr);
78 Set_Intval (Token_Node, Int_Literal_Value);
80 when Tok_String_Literal =>
81 Token_Node := New_Node (N_String_Literal, Token_Ptr);
82 Set_Has_Wide_Character (Token_Node, Wide_Character_Found);
83 Set_Strval (Token_Node, String_Literal_Id);
85 when Tok_Operator_Symbol =>
86 Token_Node := New_Node (N_Operator_Symbol, Token_Ptr);
87 Set_Chars (Token_Node, Token_Name);
88 Set_Strval (Token_Node, String_Literal_Id);
90 when others =>
91 null;
92 end case;
93 end Post_Scan;
95 -----------------------
96 -- Check_End_Of_Line --
97 -----------------------
99 procedure Check_End_Of_Line is
100 Len : constant Int := Int (Scan_Ptr) - Int (Current_Line_Start);
101 begin
102 if Style_Check then
103 Style.Check_Line_Terminator (Len);
104 elsif Len > Opt.Max_Line_Length then
105 Error_Long_Line;
106 end if;
107 end Check_End_Of_Line;
109 -----------------------
110 -- Determine_License --
111 -----------------------
113 function Determine_License return License_Type is
114 GPL_Found : Boolean := False;
115 Result : License_Type;
117 function Contains (S : String) return Boolean;
118 -- See if current comment contains successive non-blank characters
119 -- matching the contents of S. If so leave Scan_Ptr unchanged and
120 -- return True, otherwise leave Scan_Ptr unchanged and return False.
122 procedure Skip_EOL;
123 -- Skip to line terminator character
125 --------------
126 -- Contains --
127 --------------
129 function Contains (S : String) return Boolean is
130 CP : Natural;
131 SP : Source_Ptr;
132 SS : Source_Ptr;
134 begin
135 -- Loop to check characters. This loop is terminated by end of
136 -- line, and also we need to check for the EOF case, to take
137 -- care of files containing only comments.
139 SP := Scan_Ptr;
140 while Source (SP) /= CR and then
141 Source (SP) /= LF and then
142 Source (SP) /= EOF
143 loop
144 if Source (SP) = S (S'First) then
145 SS := SP;
146 CP := S'First;
148 loop
149 SS := SS + 1;
150 CP := CP + 1;
152 if CP > S'Last then
153 return True;
154 end if;
156 while Source (SS) = ' ' loop
157 SS := SS + 1;
158 end loop;
160 exit when Source (SS) /= S (CP);
161 end loop;
162 end if;
164 SP := SP + 1;
165 end loop;
167 return False;
168 end Contains;
170 --------------
171 -- Skip_EOL --
172 --------------
174 procedure Skip_EOL is
175 begin
176 while Source (Scan_Ptr) /= CR
177 and then Source (Scan_Ptr) /= LF
178 and then Source (Scan_Ptr) /= EOF
179 loop
180 Scan_Ptr := Scan_Ptr + 1;
181 end loop;
182 end Skip_EOL;
184 -- Start of processing for Determine_License
186 begin
187 loop
188 if Source (Scan_Ptr) /= '-'
189 or else Source (Scan_Ptr + 1) /= '-'
190 then
191 if GPL_Found then
192 Result := GPL;
193 exit;
194 else
195 Result := Unknown;
196 exit;
197 end if;
199 elsif Contains ("Asaspecialexception") then
200 if GPL_Found then
201 Result := Modified_GPL;
202 exit;
203 end if;
205 elsif Contains ("GNUGeneralPublicLicense") then
206 GPL_Found := True;
208 elsif
209 Contains
210 ("ThisspecificationisadaptedfromtheAdaSemanticInterface")
211 or else
212 Contains
213 ("ThisspecificationisderivedfromtheAdaReferenceManual")
214 then
215 Result := Unrestricted;
216 exit;
217 end if;
219 Skip_EOL;
221 Check_End_Of_Line;
223 if Source (Scan_Ptr) /= EOF then
225 -- We have to take into account a degenerate case when the source
226 -- file contains only comments and no Ada code.
228 declare
229 Physical : Boolean;
231 begin
232 Skip_Line_Terminators (Scan_Ptr, Physical);
234 -- If we are at start of physical line, update scan pointers
235 -- to reflect the start of the new line.
237 if Physical then
238 Current_Line_Start := Scan_Ptr;
239 Start_Column := Scanner.Set_Start_Column;
240 First_Non_Blank_Location := Scan_Ptr;
241 end if;
242 end;
243 end if;
244 end loop;
246 return Result;
247 end Determine_License;
249 ----------------------------
250 -- Determine_Token_Casing --
251 ----------------------------
253 function Determine_Token_Casing return Casing_Type is
254 begin
255 return Scanner.Determine_Token_Casing;
256 end Determine_Token_Casing;
258 ---------------------
259 -- Error_Long_Line --
260 ---------------------
262 procedure Error_Long_Line is
263 begin
264 Error_Msg
265 ("this line is too long",
266 Current_Line_Start + Source_Ptr (Opt.Max_Line_Length));
267 end Error_Long_Line;
269 ------------------------
270 -- Initialize_Scanner --
271 ------------------------
273 procedure Initialize_Scanner
274 (Unit : Unit_Number_Type;
275 Index : Source_File_Index)
277 GNAT_Hedr : constant Text_Buffer (1 .. 78) := (others => '-');
279 begin
280 Scanner.Initialize_Scanner (Unit, Index);
282 -- Set default for Comes_From_Source (except if we are going to process
283 -- an artificial string internally created within the compiler and
284 -- placed into internal source duffer). All nodes built now until we
285 -- reenter the analyzer will have Comes_From_Source set to True
287 if Index /= Internal_Source_File then
288 Set_Comes_From_Source_Default (True);
289 end if;
291 -- Check license if GNAT type header possibly present
293 if Source_Last (Index) - Scan_Ptr > 80
294 and then Source (Scan_Ptr .. Scan_Ptr + 77) = GNAT_Hedr
295 then
296 Set_License (Current_Source_File, Determine_License);
297 end if;
299 -- Because of the License stuff above, Scng.Initialize_Scanner cannot
300 -- call Scan. Scan initial token (note this initializes Prev_Token,
301 -- Prev_Token_Ptr).
303 -- There are two reasons not to do the Scan step in case if we
304 -- initialize the scanner for the internal source buffer:
306 -- - The artificial string may not be created by the compiler in this
307 -- buffer when we call Initialize_Scanner
309 -- - For these artificial strings a special way of scanning is used, so
310 -- the standard step of the scanner may just break the algorithm of
311 -- processing these strings.
313 if Index /= Internal_Source_File then
314 Scan;
315 end if;
317 -- Clear flags for reserved words used as indentifiers
319 for J in Token_Type loop
320 Used_As_Identifier (J) := False;
321 end loop;
322 end Initialize_Scanner;
324 ------------------------------
325 -- Scan_Reserved_Identifier --
326 ------------------------------
328 procedure Scan_Reserved_Identifier (Force_Msg : Boolean) is
329 Token_Chars : constant String := Token_Type'Image (Token);
331 begin
332 -- We have in Token_Chars the image of the Token name, i.e. Tok_xxx.
333 -- This code extracts the xxx and makes an identifier out of it.
335 Name_Len := 0;
337 for J in 5 .. Token_Chars'Length loop
338 Name_Len := Name_Len + 1;
339 Name_Buffer (Name_Len) := Fold_Lower (Token_Chars (J));
340 end loop;
342 Token_Name := Name_Find;
344 if not Used_As_Identifier (Token) or else Force_Msg then
345 Error_Msg_Name_1 := Token_Name;
346 Error_Msg_SC ("reserved word* cannot be used as identifier!");
347 Used_As_Identifier (Token) := True;
348 end if;
350 Token := Tok_Identifier;
351 Token_Node := New_Node (N_Identifier, Token_Ptr);
352 Set_Chars (Token_Node, Token_Name);
353 end Scan_Reserved_Identifier;
355 end Scn;