1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2004 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 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. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Csets; use Csets;
29 with Namet; use Namet;
31 with Scans; use Scans;
32 with Sinfo; use Sinfo;
33 with Sinput; use Sinput;
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
60 procedure Post_Scan 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);
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);
103 Style.Check_Line_Terminator (Len);
104 elsif Len > Opt.Max_Line_Length then
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.
123 -- Skip to line terminator character
129 function Contains (S : String) return Boolean is
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.
140 while Source (SP) /= CR and then
141 Source (SP) /= LF and then
144 if Source (SP) = S (S'First) then
156 while Source (SS) = ' ' loop
160 exit when Source (SS) /= S (CP);
174 procedure Skip_EOL is
176 while Source (Scan_Ptr) /= CR
177 and then Source (Scan_Ptr) /= LF
178 and then Source (Scan_Ptr) /= EOF
180 Scan_Ptr := Scan_Ptr + 1;
184 -- Start of processing for Determine_License
188 if Source (Scan_Ptr) /= '-'
189 or else Source (Scan_Ptr + 1) /= '-'
199 elsif Contains ("Asaspecialexception") then
201 Result := Modified_GPL;
205 elsif Contains ("GNUGeneralPublicLicense") then
210 ("ThisspecificationisadaptedfromtheAdaSemanticInterface")
213 ("ThisspecificationisderivedfromtheAdaReferenceManual")
215 Result := Unrestricted;
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.
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.
238 Current_Line_Start := Scan_Ptr;
239 Start_Column := Scanner.Set_Start_Column;
240 First_Non_Blank_Location := Scan_Ptr;
247 end Determine_License;
249 ----------------------------
250 -- Determine_Token_Casing --
251 ----------------------------
253 function Determine_Token_Casing return Casing_Type is
255 return Scanner.Determine_Token_Casing;
256 end Determine_Token_Casing;
258 ---------------------
259 -- Error_Long_Line --
260 ---------------------
262 procedure Error_Long_Line is
265 ("this line is too long",
266 Current_Line_Start + Source_Ptr (Opt.Max_Line_Length));
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 => '-');
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);
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
296 Set_License (Current_Source_File, Determine_License);
299 -- Because of the License stuff above, Scng.Initialize_Scanner cannot
300 -- call Scan. Scan initial token (note this initializes Prev_Token,
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
317 -- Clear flags for reserved words used as indentifiers
319 for J in Token_Type loop
320 Used_As_Identifier (J) := False;
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);
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.
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));
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;
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;