1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2007, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Csets; use Csets;
28 with Hostparm; use Hostparm;
29 with Namet; use Namet;
31 with Restrict; use Restrict;
32 with Rident; use Rident;
33 with Scans; use Scans;
34 with Sinfo; use Sinfo;
35 with Sinput; use Sinput;
36 with Uintp; use Uintp;
42 Used_As_Identifier : array (Token_Type) of Boolean;
43 -- Flags set True if a given keyword is used as an identifier (used to
44 -- make sure that we only post an error message for incorrect use of a
45 -- keyword as an identifier once for a given keyword).
47 procedure Check_End_Of_Line;
48 -- Called when end of line encountered. Checks that line is not too long,
49 -- and that other style checks for the end of line are met.
51 function Determine_License return License_Type;
52 -- Scan header of file and check that it has an appropriate GNAT-style
53 -- header with a proper license statement. Returns GPL, Unrestricted,
54 -- or Modified_GPL depending on header. If none of these, returns Unknown.
56 procedure Error_Long_Line;
57 -- Signal error of excessively long line
59 -----------------------
60 -- Check_End_Of_Line --
61 -----------------------
63 procedure Check_End_Of_Line is
64 Len : constant Int := Int (Scan_Ptr) - Int (Current_Line_Start);
67 Style.Check_Line_Terminator (Len);
68 elsif Len > Max_Line_Length then
71 end Check_End_Of_Line;
73 -----------------------
74 -- Determine_License --
75 -----------------------
77 function Determine_License return License_Type is
78 GPL_Found : Boolean := False;
79 Result : License_Type;
81 function Contains (S : String) return Boolean;
82 -- See if current comment contains successive non-blank characters
83 -- matching the contents of S. If so leave Scan_Ptr unchanged and
84 -- return True, otherwise leave Scan_Ptr unchanged and return False.
87 -- Skip to line terminator character
93 function Contains (S : String) return Boolean is
99 -- Loop to check characters. This loop is terminated by end of
100 -- line, and also we need to check for the EOF case, to take
101 -- care of files containing only comments.
104 while Source (SP) /= CR and then
105 Source (SP) /= LF and then
108 if Source (SP) = S (S'First) then
120 while Source (SS) = ' ' loop
124 exit when Source (SS) /= S (CP);
138 procedure Skip_EOL is
140 while Source (Scan_Ptr) /= CR
141 and then Source (Scan_Ptr) /= LF
142 and then Source (Scan_Ptr) /= EOF
144 Scan_Ptr := Scan_Ptr + 1;
148 -- Start of processing for Determine_License
152 if Source (Scan_Ptr) /= '-'
153 or else Source (Scan_Ptr + 1) /= '-'
163 elsif Contains ("Asaspecialexception") then
165 Result := Modified_GPL;
169 elsif Contains ("GNUGeneralPublicLicense") then
174 ("ThisspecificationisadaptedfromtheAdaSemanticInterface")
177 ("ThisspecificationisderivedfromtheAdaReferenceManual")
179 Result := Unrestricted;
187 if Source (Scan_Ptr) /= EOF then
189 -- We have to take into account a degenerate case when the source
190 -- file contains only comments and no Ada code.
196 Skip_Line_Terminators (Scan_Ptr, Physical);
198 -- If we are at start of physical line, update scan pointers
199 -- to reflect the start of the new line.
202 Current_Line_Start := Scan_Ptr;
203 Start_Column := Scanner.Set_Start_Column;
204 First_Non_Blank_Location := Scan_Ptr;
211 end Determine_License;
213 ----------------------------
214 -- Determine_Token_Casing --
215 ----------------------------
217 function Determine_Token_Casing return Casing_Type is
219 return Scanner.Determine_Token_Casing;
220 end Determine_Token_Casing;
222 ---------------------
223 -- Error_Long_Line --
224 ---------------------
226 procedure Error_Long_Line is
229 ("this line is too long",
230 Current_Line_Start + Source_Ptr (Max_Line_Length));
233 ------------------------
234 -- Initialize_Scanner --
235 ------------------------
237 procedure Initialize_Scanner
238 (Unit : Unit_Number_Type;
239 Index : Source_File_Index)
241 GNAT_Hedr : constant Text_Buffer (1 .. 78) := (others => '-');
244 Scanner.Initialize_Scanner (Index);
246 if Index /= Internal_Source_File then
247 Set_Unit (Index, Unit);
250 Current_Source_Unit := Unit;
252 -- Set default for Comes_From_Source (except if we are going to process
253 -- an artificial string internally created within the compiler and
254 -- placed into internal source duffer). All nodes built now until we
255 -- reenter the analyzer will have Comes_From_Source set to True
257 if Index /= Internal_Source_File then
258 Set_Comes_From_Source_Default (True);
261 -- Check license if GNAT type header possibly present
263 if Source_Last (Index) - Scan_Ptr > 80
264 and then Source (Scan_Ptr .. Scan_Ptr + 77) = GNAT_Hedr
266 Set_License (Current_Source_File, Determine_License);
269 -- Because of the License stuff above, Scng.Initialize_Scanner cannot
270 -- call Scan. Scan initial token (note this initializes Prev_Token,
273 -- There are two reasons not to do the Scan step in case if we
274 -- initialize the scanner for the internal source buffer:
276 -- - The artificial string may not be created by the compiler in this
277 -- buffer when we call Initialize_Scanner
279 -- - For these artificial strings a special way of scanning is used, so
280 -- the standard step of the scanner may just break the algorithm of
281 -- processing these strings.
283 if Index /= Internal_Source_File then
287 -- Clear flags for reserved words used as indentifiers
289 for J in Token_Type loop
290 Used_As_Identifier (J) := False;
292 end Initialize_Scanner;
294 -----------------------
295 -- Obsolescent_Check --
296 -----------------------
298 procedure Obsolescent_Check (S : Source_Ptr) is
300 -- This is a pain in the neck case, since we normally need a node to
301 -- call Check_Restrictions, and all we have is a source pointer. The
302 -- easiest thing is to construct a dummy node. A bit kludgy, but this
303 -- is a marginal case. It's not worth trying to do things more cleanly.
305 Check_Restriction (No_Obsolescent_Features, New_Node (N_Empty, S));
306 end Obsolescent_Check;
312 procedure Post_Scan is
315 when Tok_Char_Literal =>
316 Token_Node := New_Node (N_Character_Literal, Token_Ptr);
317 Set_Char_Literal_Value (Token_Node, UI_From_CC (Character_Code));
318 Set_Chars (Token_Node, Token_Name);
320 when Tok_Identifier =>
321 Token_Node := New_Node (N_Identifier, Token_Ptr);
322 Set_Chars (Token_Node, Token_Name);
324 when Tok_Real_Literal =>
325 Token_Node := New_Node (N_Real_Literal, Token_Ptr);
326 Set_Realval (Token_Node, Real_Literal_Value);
328 when Tok_Integer_Literal =>
329 Token_Node := New_Node (N_Integer_Literal, Token_Ptr);
330 Set_Intval (Token_Node, Int_Literal_Value);
332 when Tok_String_Literal =>
333 Token_Node := New_Node (N_String_Literal, Token_Ptr);
334 Set_Has_Wide_Character (Token_Node, Wide_Character_Found);
335 Set_Strval (Token_Node, String_Literal_Id);
337 when Tok_Operator_Symbol =>
338 Token_Node := New_Node (N_Operator_Symbol, Token_Ptr);
339 Set_Chars (Token_Node, Token_Name);
340 Set_Strval (Token_Node, String_Literal_Id);
347 ------------------------------
348 -- Scan_Reserved_Identifier --
349 ------------------------------
351 procedure Scan_Reserved_Identifier (Force_Msg : Boolean) is
352 Token_Chars : constant String := Token_Type'Image (Token);
355 -- We have in Token_Chars the image of the Token name, i.e. Tok_xxx.
356 -- This code extracts the xxx and makes an identifier out of it.
360 for J in 5 .. Token_Chars'Length loop
361 Name_Len := Name_Len + 1;
362 Name_Buffer (Name_Len) := Fold_Lower (Token_Chars (J));
365 Token_Name := Name_Find;
367 if not Used_As_Identifier (Token) or else Force_Msg then
368 Error_Msg_Name_1 := Token_Name;
369 Error_Msg_SC ("reserved word* cannot be used as identifier!");
370 Used_As_Identifier (Token) := True;
373 Token := Tok_Identifier;
374 Token_Node := New_Node (N_Identifier, Token_Ptr);
375 Set_Chars (Token_Node, Token_Name);
376 end Scan_Reserved_Identifier;