gcc:
[official-gcc.git] / gcc / ada / scn.adb
blob6f8ea91d64656bf515676ca8b1a6d5ec6c18c474
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S C N --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2006, 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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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 Hostparm; use Hostparm;
30 with Namet; use Namet;
31 with Opt; use Opt;
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 package body Scn is
41 use ASCII;
43 Used_As_Identifier : array (Token_Type) of Boolean;
44 -- Flags set True if a given keyword is used as an identifier (used to
45 -- make sure that we only post an error message for incorrect use of a
46 -- keyword as an identifier once for a given keyword).
48 procedure Check_End_Of_Line;
49 -- Called when end of line encountered. Checks that line is not too long,
50 -- and that other style checks for the end of line are met.
52 function Determine_License return License_Type;
53 -- Scan header of file and check that it has an appropriate GNAT-style
54 -- header with a proper license statement. Returns GPL, Unrestricted,
55 -- or Modified_GPL depending on header. If none of these, returns Unknown.
57 procedure Error_Long_Line;
58 -- Signal error of excessively long line
60 -----------------------
61 -- Check_End_Of_Line --
62 -----------------------
64 procedure Check_End_Of_Line is
65 Len : constant Int := Int (Scan_Ptr) - Int (Current_Line_Start);
66 begin
67 if Style_Check then
68 Style.Check_Line_Terminator (Len);
69 elsif Len > Max_Line_Length then
70 Error_Long_Line;
71 end if;
72 end Check_End_Of_Line;
74 -----------------------
75 -- Determine_License --
76 -----------------------
78 function Determine_License return License_Type is
79 GPL_Found : Boolean := False;
80 Result : License_Type;
82 function Contains (S : String) return Boolean;
83 -- See if current comment contains successive non-blank characters
84 -- matching the contents of S. If so leave Scan_Ptr unchanged and
85 -- return True, otherwise leave Scan_Ptr unchanged and return False.
87 procedure Skip_EOL;
88 -- Skip to line terminator character
90 --------------
91 -- Contains --
92 --------------
94 function Contains (S : String) return Boolean is
95 CP : Natural;
96 SP : Source_Ptr;
97 SS : Source_Ptr;
99 begin
100 -- Loop to check characters. This loop is terminated by end of
101 -- line, and also we need to check for the EOF case, to take
102 -- care of files containing only comments.
104 SP := Scan_Ptr;
105 while Source (SP) /= CR and then
106 Source (SP) /= LF and then
107 Source (SP) /= EOF
108 loop
109 if Source (SP) = S (S'First) then
110 SS := SP;
111 CP := S'First;
113 loop
114 SS := SS + 1;
115 CP := CP + 1;
117 if CP > S'Last then
118 return True;
119 end if;
121 while Source (SS) = ' ' loop
122 SS := SS + 1;
123 end loop;
125 exit when Source (SS) /= S (CP);
126 end loop;
127 end if;
129 SP := SP + 1;
130 end loop;
132 return False;
133 end Contains;
135 --------------
136 -- Skip_EOL --
137 --------------
139 procedure Skip_EOL is
140 begin
141 while Source (Scan_Ptr) /= CR
142 and then Source (Scan_Ptr) /= LF
143 and then Source (Scan_Ptr) /= EOF
144 loop
145 Scan_Ptr := Scan_Ptr + 1;
146 end loop;
147 end Skip_EOL;
149 -- Start of processing for Determine_License
151 begin
152 loop
153 if Source (Scan_Ptr) /= '-'
154 or else Source (Scan_Ptr + 1) /= '-'
155 then
156 if GPL_Found then
157 Result := GPL;
158 exit;
159 else
160 Result := Unknown;
161 exit;
162 end if;
164 elsif Contains ("Asaspecialexception") then
165 if GPL_Found then
166 Result := Modified_GPL;
167 exit;
168 end if;
170 elsif Contains ("GNUGeneralPublicLicense") then
171 GPL_Found := True;
173 elsif
174 Contains
175 ("ThisspecificationisadaptedfromtheAdaSemanticInterface")
176 or else
177 Contains
178 ("ThisspecificationisderivedfromtheAdaReferenceManual")
179 then
180 Result := Unrestricted;
181 exit;
182 end if;
184 Skip_EOL;
186 Check_End_Of_Line;
188 if Source (Scan_Ptr) /= EOF then
190 -- We have to take into account a degenerate case when the source
191 -- file contains only comments and no Ada code.
193 declare
194 Physical : Boolean;
196 begin
197 Skip_Line_Terminators (Scan_Ptr, Physical);
199 -- If we are at start of physical line, update scan pointers
200 -- to reflect the start of the new line.
202 if Physical then
203 Current_Line_Start := Scan_Ptr;
204 Start_Column := Scanner.Set_Start_Column;
205 First_Non_Blank_Location := Scan_Ptr;
206 end if;
207 end;
208 end if;
209 end loop;
211 return Result;
212 end Determine_License;
214 ----------------------------
215 -- Determine_Token_Casing --
216 ----------------------------
218 function Determine_Token_Casing return Casing_Type is
219 begin
220 return Scanner.Determine_Token_Casing;
221 end Determine_Token_Casing;
223 ---------------------
224 -- Error_Long_Line --
225 ---------------------
227 procedure Error_Long_Line is
228 begin
229 Error_Msg
230 ("this line is too long",
231 Current_Line_Start + Source_Ptr (Max_Line_Length));
232 end Error_Long_Line;
234 ------------------------
235 -- Initialize_Scanner --
236 ------------------------
238 procedure Initialize_Scanner
239 (Unit : Unit_Number_Type;
240 Index : Source_File_Index)
242 GNAT_Hedr : constant Text_Buffer (1 .. 78) := (others => '-');
244 begin
245 Scanner.Initialize_Scanner (Index);
247 if Index /= Internal_Source_File then
248 Set_Unit (Index, Unit);
249 end if;
251 Current_Source_Unit := Unit;
253 -- Set default for Comes_From_Source (except if we are going to process
254 -- an artificial string internally created within the compiler and
255 -- placed into internal source duffer). All nodes built now until we
256 -- reenter the analyzer will have Comes_From_Source set to True
258 if Index /= Internal_Source_File then
259 Set_Comes_From_Source_Default (True);
260 end if;
262 -- Check license if GNAT type header possibly present
264 if Source_Last (Index) - Scan_Ptr > 80
265 and then Source (Scan_Ptr .. Scan_Ptr + 77) = GNAT_Hedr
266 then
267 Set_License (Current_Source_File, Determine_License);
268 end if;
270 -- Because of the License stuff above, Scng.Initialize_Scanner cannot
271 -- call Scan. Scan initial token (note this initializes Prev_Token,
272 -- Prev_Token_Ptr).
274 -- There are two reasons not to do the Scan step in case if we
275 -- initialize the scanner for the internal source buffer:
277 -- - The artificial string may not be created by the compiler in this
278 -- buffer when we call Initialize_Scanner
280 -- - For these artificial strings a special way of scanning is used, so
281 -- the standard step of the scanner may just break the algorithm of
282 -- processing these strings.
284 if Index /= Internal_Source_File then
285 Scan;
286 end if;
288 -- Clear flags for reserved words used as indentifiers
290 for J in Token_Type loop
291 Used_As_Identifier (J) := False;
292 end loop;
293 end Initialize_Scanner;
295 -----------------------
296 -- Obsolescent_Check --
297 -----------------------
299 procedure Obsolescent_Check (S : Source_Ptr) is
300 begin
301 -- This is a pain in the neck case, since we normally need a node to
302 -- call Check_Restrictions, and all we have is a source pointer. The
303 -- easiest thing is to construct a dummy node. A bit kludgy, but this
304 -- is a marginal case. It's not worth trying to do things more cleanly.
306 Check_Restriction (No_Obsolescent_Features, New_Node (N_Empty, S));
307 end Obsolescent_Check;
309 ---------------
310 -- Post_Scan --
311 ---------------
313 procedure Post_Scan is
314 begin
315 case Token is
316 when Tok_Char_Literal =>
317 Token_Node := New_Node (N_Character_Literal, Token_Ptr);
318 Set_Char_Literal_Value (Token_Node, UI_From_CC (Character_Code));
319 Set_Chars (Token_Node, Token_Name);
321 when Tok_Identifier =>
322 Token_Node := New_Node (N_Identifier, Token_Ptr);
323 Set_Chars (Token_Node, Token_Name);
325 when Tok_Real_Literal =>
326 Token_Node := New_Node (N_Real_Literal, Token_Ptr);
327 Set_Realval (Token_Node, Real_Literal_Value);
329 when Tok_Integer_Literal =>
330 Token_Node := New_Node (N_Integer_Literal, Token_Ptr);
331 Set_Intval (Token_Node, Int_Literal_Value);
333 when Tok_String_Literal =>
334 Token_Node := New_Node (N_String_Literal, Token_Ptr);
335 Set_Has_Wide_Character (Token_Node, Wide_Character_Found);
336 Set_Strval (Token_Node, String_Literal_Id);
338 when Tok_Operator_Symbol =>
339 Token_Node := New_Node (N_Operator_Symbol, Token_Ptr);
340 Set_Chars (Token_Node, Token_Name);
341 Set_Strval (Token_Node, String_Literal_Id);
343 when others =>
344 null;
345 end case;
346 end Post_Scan;
348 ------------------------------
349 -- Scan_Reserved_Identifier --
350 ------------------------------
352 procedure Scan_Reserved_Identifier (Force_Msg : Boolean) is
353 Token_Chars : constant String := Token_Type'Image (Token);
355 begin
356 -- We have in Token_Chars the image of the Token name, i.e. Tok_xxx.
357 -- This code extracts the xxx and makes an identifier out of it.
359 Name_Len := 0;
361 for J in 5 .. Token_Chars'Length loop
362 Name_Len := Name_Len + 1;
363 Name_Buffer (Name_Len) := Fold_Lower (Token_Chars (J));
364 end loop;
366 Token_Name := Name_Find;
368 if not Used_As_Identifier (Token) or else Force_Msg then
369 Error_Msg_Name_1 := Token_Name;
370 Error_Msg_SC ("reserved word* cannot be used as identifier!");
371 Used_As_Identifier (Token) := True;
372 end if;
374 Token := Tok_Identifier;
375 Token_Node := New_Node (N_Identifier, Token_Ptr);
376 Set_Chars (Token_Node, Token_Name);
377 end Scan_Reserved_Identifier;
379 end Scn;