1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2013, 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 Namet
; use Namet
;
30 with Restrict
; use Restrict
;
31 with Rident
; use Rident
;
32 with Scans
; use Scans
;
33 with Sinfo
; use Sinfo
;
34 with Sinput
; use Sinput
;
35 with Uintp
; use Uintp
;
41 Used_As_Identifier
: array (Token_Type
) of Boolean;
42 -- Flags set True if a given keyword is used as an identifier (used to
43 -- make sure that we only post an error message for incorrect use of a
44 -- keyword as an identifier once for a given keyword).
46 function Determine_License
return License_Type
;
47 -- Scan header of file and check that it has an appropriate GNAT-style
48 -- header with a proper license statement. Returns GPL, Unrestricted,
49 -- or Modified_GPL depending on header. If none of these, returns Unknown.
51 -----------------------
52 -- Determine_License --
53 -----------------------
55 function Determine_License
return License_Type
is
56 GPL_Found
: Boolean := False;
57 Result
: License_Type
;
59 function Contains
(S
: String) return Boolean;
60 -- See if current comment contains successive non-blank characters
61 -- matching the contents of S. If so leave Scan_Ptr unchanged and
62 -- return True, otherwise leave Scan_Ptr unchanged and return False.
65 -- Skip to line terminator character
71 function Contains
(S
: String) return Boolean is
77 -- Loop to check characters. This loop is terminated by end of
78 -- line, and also we need to check for the EOF case, to take
79 -- care of files containing only comments.
82 while Source
(SP
) /= CR
and then
83 Source
(SP
) /= LF
and then
86 if Source
(SP
) = S
(S
'First) then
98 while Source
(SS
) = ' ' loop
102 exit when Source
(SS
) /= S
(CP
);
116 procedure Skip_EOL
is
118 while Source
(Scan_Ptr
) /= CR
119 and then Source
(Scan_Ptr
) /= LF
120 and then Source
(Scan_Ptr
) /= EOF
122 Scan_Ptr
:= Scan_Ptr
+ 1;
126 -- Start of processing for Determine_License
130 if Source
(Scan_Ptr
) /= '-'
131 or else Source
(Scan_Ptr
+ 1) /= '-'
141 elsif Contains
("Asaspecialexception") then
143 Result
:= Modified_GPL
;
147 elsif Contains
("GNUGeneralPublicLicense") then
152 ("ThisspecificationisadaptedfromtheAdaSemanticInterface")
155 ("ThisspecificationisderivedfromtheAdaReferenceManual")
157 Result
:= Unrestricted
;
163 Scanner
.Check_End_Of_Line
;
165 if Source
(Scan_Ptr
) /= EOF
then
167 -- We have to take into account a degenerate case when the source
168 -- file contains only comments and no Ada code.
174 Skip_Line_Terminators
(Scan_Ptr
, Physical
);
176 -- If we are at start of physical line, update scan pointers
177 -- to reflect the start of the new line.
180 Current_Line_Start
:= Scan_Ptr
;
181 Start_Column
:= Scanner
.Set_Start_Column
;
182 First_Non_Blank_Location
:= Scan_Ptr
;
189 end Determine_License
;
191 ----------------------------
192 -- Determine_Token_Casing --
193 ----------------------------
195 function Determine_Token_Casing
return Casing_Type
is
197 return Scanner
.Determine_Token_Casing
;
198 end Determine_Token_Casing
;
200 ------------------------
201 -- Initialize_Scanner --
202 ------------------------
204 procedure Initialize_Scanner
205 (Unit
: Unit_Number_Type
;
206 Index
: Source_File_Index
)
208 GNAT_Hedr
: constant Text_Buffer
(1 .. 78) := (others => '-');
211 Scanner
.Initialize_Scanner
(Index
);
213 if Index
/= Internal_Source_File
then
214 Set_Unit
(Index
, Unit
);
217 Current_Source_Unit
:= Unit
;
219 -- Set default for Comes_From_Source (except if we are going to process
220 -- an artificial string internally created within the compiler and
221 -- placed into internal source duffer). All nodes built now until we
222 -- reenter the analyzer will have Comes_From_Source set to True
224 if Index
/= Internal_Source_File
then
225 Set_Comes_From_Source_Default
(True);
228 -- Check license if GNAT type header possibly present
230 if Source_Last
(Index
) - Scan_Ptr
> 80
231 and then Source
(Scan_Ptr
.. Scan_Ptr
+ 77) = GNAT_Hedr
233 Set_License
(Current_Source_File
, Determine_License
);
238 -- Because of the License stuff above, Scng.Initialize_Scanner cannot
239 -- call Scan. Scan initial token (note this initializes Prev_Token,
242 -- There are two reasons not to do the Scan step in case if we
243 -- initialize the scanner for the internal source buffer:
245 -- - The artificial string may not be created by the compiler in this
246 -- buffer when we call Initialize_Scanner
248 -- - For these artificial strings a special way of scanning is used, so
249 -- the standard step of the scanner may just break the algorithm of
250 -- processing these strings.
252 if Index
/= Internal_Source_File
then
256 -- Clear flags for reserved words used as identifiers
258 for J
in Token_Type
loop
259 Used_As_Identifier
(J
) := False;
261 end Initialize_Scanner
;
267 procedure Post_Scan
is
268 procedure Check_Obsolescent_Features_Restriction
(S
: Source_Ptr
);
269 -- This checks for Obsolescent_Features restriction being active, and
270 -- if so, flags the restriction as occurring at the given scan location.
272 procedure Check_Obsolete_Base_Char
;
273 -- Check for numeric literal using ':' instead of '#' for based case
275 --------------------------------------------
276 -- Check_Obsolescent_Features_Restriction --
277 --------------------------------------------
279 procedure Check_Obsolescent_Features_Restriction
(S
: Source_Ptr
) is
281 -- Normally we have a node handy for posting restrictions. We don't
282 -- have such a node here, so construct a dummy one with the right
283 -- scan pointer. This is only used to get the Sloc value anyway.
285 Check_Restriction
(No_Obsolescent_Features
, New_Node
(N_Empty
, S
));
286 end Check_Obsolescent_Features_Restriction
;
288 ------------------------------
289 -- Check_Obsolete_Base_Char --
290 ------------------------------
292 procedure Check_Obsolete_Base_Char
is
296 if Based_Literal_Uses_Colon
then
298 -- Find the : for the restriction or warning message
301 while Source
(S
) /= ':' loop
305 Check_Obsolescent_Features_Restriction
(S
);
307 if Warn_On_Obsolescent_Feature
then
309 ("?j?use of "":"" is an obsolescent feature (RM J.2(3))", S
);
311 ("\?j?use ""'#"" instead", S
);
314 end Check_Obsolete_Base_Char
;
316 -- Start of processing for Post_Scan
320 when Tok_Char_Literal
=>
321 Token_Node
:= New_Node
(N_Character_Literal
, Token_Ptr
);
322 Set_Char_Literal_Value
(Token_Node
, UI_From_CC
(Character_Code
));
323 Set_Chars
(Token_Node
, Token_Name
);
325 when Tok_Identifier
=>
326 Token_Node
:= New_Node
(N_Identifier
, Token_Ptr
);
327 Set_Chars
(Token_Node
, Token_Name
);
329 when Tok_Real_Literal
=>
330 Token_Node
:= New_Node
(N_Real_Literal
, Token_Ptr
);
331 Set_Realval
(Token_Node
, Real_Literal_Value
);
332 Check_Obsolete_Base_Char
;
334 when Tok_Integer_Literal
=>
335 Token_Node
:= New_Node
(N_Integer_Literal
, Token_Ptr
);
336 Set_Intval
(Token_Node
, Int_Literal_Value
);
337 Check_Obsolete_Base_Char
;
339 when Tok_String_Literal
=>
340 Token_Node
:= New_Node
(N_String_Literal
, Token_Ptr
);
341 Set_Has_Wide_Character
342 (Token_Node
, Wide_Character_Found
);
343 Set_Has_Wide_Wide_Character
344 (Token_Node
, Wide_Wide_Character_Found
);
345 Set_Strval
(Token_Node
, String_Literal_Id
);
347 if Source
(Token_Ptr
) = '%' then
348 Check_Obsolescent_Features_Restriction
(Token_Ptr
);
350 if Warn_On_Obsolescent_Feature
then
352 ("?j?use of ""'%"" is an obsolescent feature (RM J.2(4))");
353 Error_Msg_SC
("\?j?use """""" instead");
357 when Tok_Operator_Symbol
=>
358 Token_Node
:= New_Node
(N_Operator_Symbol
, Token_Ptr
);
359 Set_Chars
(Token_Node
, Token_Name
);
360 Set_Strval
(Token_Node
, String_Literal_Id
);
362 when Tok_Vertical_Bar
=>
363 if Source
(Token_Ptr
) = '!' then
364 Check_Obsolescent_Features_Restriction
(Token_Ptr
);
366 if Warn_On_Obsolescent_Feature
then
368 ("?j?use of ""'!"" is an obsolescent feature (RM J.2(2))");
369 Error_Msg_SC
("\?j?use ""'|"" instead");
378 ------------------------------
379 -- Scan_Reserved_Identifier --
380 ------------------------------
382 procedure Scan_Reserved_Identifier
(Force_Msg
: Boolean) is
383 Token_Chars
: constant String := Token_Type
'Image (Token
);
386 -- We have in Token_Chars the image of the Token name, i.e. Tok_xxx.
387 -- This code extracts the xxx and makes an identifier out of it.
391 for J
in 5 .. Token_Chars
'Length loop
392 Name_Len
:= Name_Len
+ 1;
393 Name_Buffer
(Name_Len
) := Fold_Lower
(Token_Chars
(J
));
396 Token_Name
:= Name_Find
;
398 if not Used_As_Identifier
(Token
) or else Force_Msg
then
399 Error_Msg_Name_1
:= Token_Name
;
400 Error_Msg_SC
("reserved word* cannot be used as identifier!");
401 Used_As_Identifier
(Token
) := True;
404 Token
:= Tok_Identifier
;
405 Token_Node
:= New_Node
(N_Identifier
, Token_Ptr
);
406 Set_Chars
(Token_Node
, Token_Name
);
407 end Scan_Reserved_Identifier
;