2010-07-22 Andi Kleen <ak@linux.intel.com>
[official-gcc.git] / gcc / ada / scn.adb
blob98485506cba9d260cf2754e0b217aff1d80f8cf8
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S C N --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2009, 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 Atree; use Atree;
27 with Csets; use Csets;
28 with Hostparm; use Hostparm;
29 with Namet; use Namet;
30 with Opt; use Opt;
31 with Output; use Output;
32 with Restrict; use Restrict;
33 with Rident; use Rident;
34 with Scans; use Scans;
35 with Sinfo; use Sinfo;
36 with Sinput; use Sinput;
37 with Uintp; use Uintp;
39 with GNAT.Byte_Order_Mark; use GNAT.Byte_Order_Mark;
41 with System.WCh_Con; use System.WCh_Con;
43 package body Scn is
45 use ASCII;
47 Obsolescent_Check_Flag : Boolean := True;
48 -- Obsolescent check activation. Set to False during integrated
49 -- preprocessing.
51 Used_As_Identifier : array (Token_Type) of Boolean;
52 -- Flags set True if a given keyword is used as an identifier (used to
53 -- make sure that we only post an error message for incorrect use of a
54 -- keyword as an identifier once for a given keyword).
56 procedure Check_End_Of_Line;
57 -- Called when end of line encountered. Checks that line is not too long,
58 -- and that other style checks for the end of line are met.
60 function Determine_License return License_Type;
61 -- Scan header of file and check that it has an appropriate GNAT-style
62 -- header with a proper license statement. Returns GPL, Unrestricted,
63 -- or Modified_GPL depending on header. If none of these, returns Unknown.
65 procedure Error_Long_Line;
66 -- Signal error of excessively long line
68 -----------------------
69 -- Check_End_Of_Line --
70 -----------------------
72 procedure Check_End_Of_Line is
73 Len : constant Int := Int (Scan_Ptr) - Int (Current_Line_Start);
74 begin
75 if Style_Check then
76 Style.Check_Line_Terminator (Len);
77 elsif Len > Max_Line_Length then
78 Error_Long_Line;
79 end if;
80 end Check_End_Of_Line;
82 -----------------------
83 -- Determine_License --
84 -----------------------
86 function Determine_License return License_Type is
87 GPL_Found : Boolean := False;
88 Result : License_Type;
90 function Contains (S : String) return Boolean;
91 -- See if current comment contains successive non-blank characters
92 -- matching the contents of S. If so leave Scan_Ptr unchanged and
93 -- return True, otherwise leave Scan_Ptr unchanged and return False.
95 procedure Skip_EOL;
96 -- Skip to line terminator character
98 --------------
99 -- Contains --
100 --------------
102 function Contains (S : String) return Boolean is
103 CP : Natural;
104 SP : Source_Ptr;
105 SS : Source_Ptr;
107 begin
108 -- Loop to check characters. This loop is terminated by end of
109 -- line, and also we need to check for the EOF case, to take
110 -- care of files containing only comments.
112 SP := Scan_Ptr;
113 while Source (SP) /= CR and then
114 Source (SP) /= LF and then
115 Source (SP) /= EOF
116 loop
117 if Source (SP) = S (S'First) then
118 SS := SP;
119 CP := S'First;
121 loop
122 SS := SS + 1;
123 CP := CP + 1;
125 if CP > S'Last then
126 return True;
127 end if;
129 while Source (SS) = ' ' loop
130 SS := SS + 1;
131 end loop;
133 exit when Source (SS) /= S (CP);
134 end loop;
135 end if;
137 SP := SP + 1;
138 end loop;
140 return False;
141 end Contains;
143 --------------
144 -- Skip_EOL --
145 --------------
147 procedure Skip_EOL is
148 begin
149 while Source (Scan_Ptr) /= CR
150 and then Source (Scan_Ptr) /= LF
151 and then Source (Scan_Ptr) /= EOF
152 loop
153 Scan_Ptr := Scan_Ptr + 1;
154 end loop;
155 end Skip_EOL;
157 -- Start of processing for Determine_License
159 begin
160 loop
161 if Source (Scan_Ptr) /= '-'
162 or else Source (Scan_Ptr + 1) /= '-'
163 then
164 if GPL_Found then
165 Result := GPL;
166 exit;
167 else
168 Result := Unknown;
169 exit;
170 end if;
172 elsif Contains ("Asaspecialexception") then
173 if GPL_Found then
174 Result := Modified_GPL;
175 exit;
176 end if;
178 elsif Contains ("GNUGeneralPublicLicense") then
179 GPL_Found := True;
181 elsif
182 Contains
183 ("ThisspecificationisadaptedfromtheAdaSemanticInterface")
184 or else
185 Contains
186 ("ThisspecificationisderivedfromtheAdaReferenceManual")
187 then
188 Result := Unrestricted;
189 exit;
190 end if;
192 Skip_EOL;
194 Check_End_Of_Line;
196 if Source (Scan_Ptr) /= EOF then
198 -- We have to take into account a degenerate case when the source
199 -- file contains only comments and no Ada code.
201 declare
202 Physical : Boolean;
204 begin
205 Skip_Line_Terminators (Scan_Ptr, Physical);
207 -- If we are at start of physical line, update scan pointers
208 -- to reflect the start of the new line.
210 if Physical then
211 Current_Line_Start := Scan_Ptr;
212 Start_Column := Scanner.Set_Start_Column;
213 First_Non_Blank_Location := Scan_Ptr;
214 end if;
215 end;
216 end if;
217 end loop;
219 return Result;
220 end Determine_License;
222 ----------------------------
223 -- Determine_Token_Casing --
224 ----------------------------
226 function Determine_Token_Casing return Casing_Type is
227 begin
228 return Scanner.Determine_Token_Casing;
229 end Determine_Token_Casing;
231 ---------------------
232 -- Error_Long_Line --
233 ---------------------
235 procedure Error_Long_Line is
236 begin
237 Error_Msg
238 ("this line is too long",
239 Current_Line_Start + Source_Ptr (Max_Line_Length));
240 end Error_Long_Line;
242 ------------------------
243 -- Initialize_Scanner --
244 ------------------------
246 procedure Initialize_Scanner
247 (Unit : Unit_Number_Type;
248 Index : Source_File_Index)
250 GNAT_Hedr : constant Text_Buffer (1 .. 78) := (others => '-');
252 begin
253 Scanner.Initialize_Scanner (Index);
255 if Index /= Internal_Source_File then
256 Set_Unit (Index, Unit);
257 end if;
259 Current_Source_Unit := Unit;
261 -- Set default for Comes_From_Source (except if we are going to process
262 -- an artificial string internally created within the compiler and
263 -- placed into internal source duffer). All nodes built now until we
264 -- reenter the analyzer will have Comes_From_Source set to True
266 if Index /= Internal_Source_File then
267 Set_Comes_From_Source_Default (True);
268 end if;
270 -- Check license if GNAT type header possibly present
272 if Source_Last (Index) - Scan_Ptr > 80
273 and then Source (Scan_Ptr .. Scan_Ptr + 77) = GNAT_Hedr
274 then
275 Set_License (Current_Source_File, Determine_License);
276 end if;
278 -- Check for BOM
280 declare
281 BOM : BOM_Kind;
282 Len : Natural;
283 Tst : String (1 .. 5);
285 begin
286 for J in 1 .. 5 loop
287 Tst (J) := Source (Scan_Ptr + Source_Ptr (J) - 1);
288 end loop;
290 Read_BOM (Tst, Len, BOM, False);
292 case BOM is
293 when UTF8_All =>
294 Scan_Ptr := Scan_Ptr + Source_Ptr (Len);
295 Wide_Character_Encoding_Method := WCEM_UTF8;
296 Upper_Half_Encoding := True;
298 when UTF16_LE | UTF16_BE =>
299 Set_Standard_Error;
300 Write_Line ("UTF-16 encoding format not recognized");
301 Set_Standard_Output;
302 raise Unrecoverable_Error;
304 when UTF32_LE | UTF32_BE =>
305 Set_Standard_Error;
306 Write_Line ("UTF-32 encoding format not recognized");
307 Set_Standard_Output;
308 raise Unrecoverable_Error;
310 when Unknown =>
311 null;
313 when others =>
314 raise Program_Error;
315 end case;
316 end;
318 -- Because of the License stuff above, Scng.Initialize_Scanner cannot
319 -- call Scan. Scan initial token (note this initializes Prev_Token,
320 -- Prev_Token_Ptr).
322 -- There are two reasons not to do the Scan step in case if we
323 -- initialize the scanner for the internal source buffer:
325 -- - The artificial string may not be created by the compiler in this
326 -- buffer when we call Initialize_Scanner
328 -- - For these artificial strings a special way of scanning is used, so
329 -- the standard step of the scanner may just break the algorithm of
330 -- processing these strings.
332 if Index /= Internal_Source_File then
333 Scan;
334 end if;
336 -- Clear flags for reserved words used as identifiers
338 for J in Token_Type loop
339 Used_As_Identifier (J) := False;
340 end loop;
341 end Initialize_Scanner;
343 -----------------------
344 -- Obsolescent_Check --
345 -----------------------
347 procedure Obsolescent_Check (S : Source_Ptr) is
348 begin
349 if Obsolescent_Check_Flag then
350 -- This is a pain in the neck case, since we normally need a node to
351 -- call Check_Restrictions, and all we have is a source pointer. The
352 -- easiest thing is to construct a dummy node. A bit kludgy, but this
353 -- is a marginal case. It's not worth trying to do things more
354 -- cleanly.
356 Check_Restriction (No_Obsolescent_Features, New_Node (N_Empty, S));
357 end if;
358 end Obsolescent_Check;
360 ---------------
361 -- Post_Scan --
362 ---------------
364 procedure Post_Scan is
365 begin
366 case Token is
367 when Tok_Char_Literal =>
368 Token_Node := New_Node (N_Character_Literal, Token_Ptr);
369 Set_Char_Literal_Value (Token_Node, UI_From_CC (Character_Code));
370 Set_Chars (Token_Node, Token_Name);
372 when Tok_Identifier =>
373 Token_Node := New_Node (N_Identifier, Token_Ptr);
374 Set_Chars (Token_Node, Token_Name);
376 when Tok_Real_Literal =>
377 Token_Node := New_Node (N_Real_Literal, Token_Ptr);
378 Set_Realval (Token_Node, Real_Literal_Value);
380 when Tok_Integer_Literal =>
381 Token_Node := New_Node (N_Integer_Literal, Token_Ptr);
382 Set_Intval (Token_Node, Int_Literal_Value);
384 when Tok_String_Literal =>
385 Token_Node := New_Node (N_String_Literal, Token_Ptr);
386 Set_Has_Wide_Character
387 (Token_Node, Wide_Character_Found);
388 Set_Has_Wide_Wide_Character
389 (Token_Node, Wide_Wide_Character_Found);
390 Set_Strval (Token_Node, String_Literal_Id);
392 when Tok_Operator_Symbol =>
393 Token_Node := New_Node (N_Operator_Symbol, Token_Ptr);
394 Set_Chars (Token_Node, Token_Name);
395 Set_Strval (Token_Node, String_Literal_Id);
397 when others =>
398 null;
399 end case;
400 end Post_Scan;
402 ------------------------------
403 -- Scan_Reserved_Identifier --
404 ------------------------------
406 procedure Scan_Reserved_Identifier (Force_Msg : Boolean) is
407 Token_Chars : constant String := Token_Type'Image (Token);
409 begin
410 -- We have in Token_Chars the image of the Token name, i.e. Tok_xxx.
411 -- This code extracts the xxx and makes an identifier out of it.
413 Name_Len := 0;
415 for J in 5 .. Token_Chars'Length loop
416 Name_Len := Name_Len + 1;
417 Name_Buffer (Name_Len) := Fold_Lower (Token_Chars (J));
418 end loop;
420 Token_Name := Name_Find;
422 if not Used_As_Identifier (Token) or else Force_Msg then
423 Error_Msg_Name_1 := Token_Name;
424 Error_Msg_SC ("reserved word* cannot be used as identifier!");
425 Used_As_Identifier (Token) := True;
426 end if;
428 Token := Tok_Identifier;
429 Token_Node := New_Node (N_Identifier, Token_Ptr);
430 Set_Chars (Token_Node, Token_Name);
431 end Scan_Reserved_Identifier;
433 ---------------------------
434 -- Set_Obsolescent_Check --
435 ---------------------------
437 procedure Set_Obsolescent_Check (Value : Boolean) is
438 begin
439 Obsolescent_Check_Flag := Value;
440 end Set_Obsolescent_Check;
442 end Scn;