1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT COMPILER COMPONENTS                         --
4--                                                                          --
5--                                  S C N                                   --
6--                                                                          --
7--                                 B o d y                                  --
8--                                                                          --
9--          Copyright (C) 1992-2013, 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------------------------------------------------------------------------------
25
26with Atree;    use Atree;
27with Csets;    use Csets;
28with Namet;    use Namet;
29with Opt;      use Opt;
30with Restrict; use Restrict;
31with Rident;   use Rident;
32with Scans;    use Scans;
33with Sinfo;    use Sinfo;
34with Sinput;   use Sinput;
35with Uintp;    use Uintp;
36
37package body Scn is
38
39   use ASCII;
40
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).
45
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.
50
51   -----------------------
52   -- Determine_License --
53   -----------------------
54
55   function Determine_License return License_Type is
56      GPL_Found : Boolean := False;
57      Result    : License_Type;
58
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.
63
64      procedure Skip_EOL;
65      --  Skip to line terminator character
66
67      --------------
68      -- Contains --
69      --------------
70
71      function Contains (S : String) return Boolean is
72         CP : Natural;
73         SP : Source_Ptr;
74         SS : Source_Ptr;
75
76      begin
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.
80
81         SP := Scan_Ptr;
82         while Source (SP) /= CR and then
83               Source (SP) /= LF and then
84               Source (SP) /= EOF
85         loop
86            if Source (SP) = S (S'First) then
87               SS := SP;
88               CP := S'First;
89
90               loop
91                  SS := SS + 1;
92                  CP := CP + 1;
93
94                  if CP > S'Last then
95                     return True;
96                  end if;
97
98                  while Source (SS) = ' ' loop
99                     SS := SS + 1;
100                  end loop;
101
102                  exit when Source (SS) /= S (CP);
103               end loop;
104            end if;
105
106            SP := SP + 1;
107         end loop;
108
109         return False;
110      end Contains;
111
112      --------------
113      -- Skip_EOL --
114      --------------
115
116      procedure Skip_EOL is
117      begin
118         while Source (Scan_Ptr) /= CR
119           and then Source (Scan_Ptr) /= LF
120           and then Source (Scan_Ptr) /= EOF
121         loop
122            Scan_Ptr := Scan_Ptr + 1;
123         end loop;
124      end Skip_EOL;
125
126   --  Start of processing for Determine_License
127
128   begin
129      loop
130         if Source (Scan_Ptr) /= '-'
131           or else Source (Scan_Ptr + 1) /= '-'
132         then
133            if GPL_Found then
134               Result := GPL;
135               exit;
136            else
137               Result := Unknown;
138               exit;
139            end if;
140
141         elsif Contains ("Asaspecialexception") then
142            if GPL_Found then
143               Result := Modified_GPL;
144               exit;
145            end if;
146
147         elsif Contains ("GNUGeneralPublicLicense") then
148            GPL_Found := True;
149
150         elsif
151             Contains
152               ("ThisspecificationisadaptedfromtheAdaSemanticInterface")
153           or else
154             Contains
155              ("ThisspecificationisderivedfromtheAdaReferenceManual")
156         then
157            Result := Unrestricted;
158            exit;
159         end if;
160
161         Skip_EOL;
162
163         Scanner.Check_End_Of_Line;
164
165         if Source (Scan_Ptr) /= EOF then
166
167            --  We have to take into account a degenerate case when the source
168            --  file contains only comments and no Ada code.
169
170            declare
171               Physical : Boolean;
172
173            begin
174               Skip_Line_Terminators (Scan_Ptr, Physical);
175
176               --  If we are at start of physical line, update scan pointers
177               --  to reflect the start of the new line.
178
179               if Physical then
180                  Current_Line_Start       := Scan_Ptr;
181                  Start_Column             := Scanner.Set_Start_Column;
182                  First_Non_Blank_Location := Scan_Ptr;
183               end if;
184            end;
185         end if;
186      end loop;
187
188      return Result;
189   end Determine_License;
190
191   ----------------------------
192   -- Determine_Token_Casing --
193   ----------------------------
194
195   function Determine_Token_Casing return Casing_Type is
196   begin
197      return Scanner.Determine_Token_Casing;
198   end Determine_Token_Casing;
199
200   ------------------------
201   -- Initialize_Scanner --
202   ------------------------
203
204   procedure Initialize_Scanner
205     (Unit  : Unit_Number_Type;
206      Index : Source_File_Index)
207   is
208      GNAT_Hedr : constant Text_Buffer (1 .. 78) := (others => '-');
209
210   begin
211      Scanner.Initialize_Scanner (Index);
212
213      if Index /= Internal_Source_File then
214         Set_Unit (Index, Unit);
215      end if;
216
217      Current_Source_Unit := Unit;
218
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
223
224      if Index /= Internal_Source_File then
225         Set_Comes_From_Source_Default (True);
226      end if;
227
228      --  Check license if GNAT type header possibly present
229
230      if Source_Last (Index) - Scan_Ptr > 80
231        and then Source (Scan_Ptr .. Scan_Ptr + 77) = GNAT_Hedr
232      then
233         Set_License (Current_Source_File, Determine_License);
234      end if;
235
236      Check_For_BOM;
237
238      --  Because of the License stuff above, Scng.Initialize_Scanner cannot
239      --  call Scan. Scan initial token (note this initializes Prev_Token,
240      --  Prev_Token_Ptr).
241
242      --  There are two reasons not to do the Scan step in case if we
243      --  initialize the scanner for the internal source buffer:
244
245      --  - The artificial string may not be created by the compiler in this
246      --    buffer when we call Initialize_Scanner
247
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.
251
252      if Index /= Internal_Source_File then
253         Scan;
254      end if;
255
256      --  Clear flags for reserved words used as identifiers
257
258      for J in Token_Type loop
259         Used_As_Identifier (J) := False;
260      end loop;
261   end Initialize_Scanner;
262
263   ---------------
264   -- Post_Scan --
265   ---------------
266
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.
271
272      procedure Check_Obsolete_Base_Char;
273      --  Check for numeric literal using ':' instead of '#' for based case
274
275      --------------------------------------------
276      -- Check_Obsolescent_Features_Restriction --
277      --------------------------------------------
278
279      procedure Check_Obsolescent_Features_Restriction (S : Source_Ptr) is
280      begin
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.
284
285         Check_Restriction (No_Obsolescent_Features, New_Node (N_Empty, S));
286      end Check_Obsolescent_Features_Restriction;
287
288      ------------------------------
289      -- Check_Obsolete_Base_Char --
290      ------------------------------
291
292      procedure Check_Obsolete_Base_Char is
293         S : Source_Ptr;
294
295      begin
296         if Based_Literal_Uses_Colon then
297
298            --  Find the : for the restriction or warning message
299
300            S := Token_Ptr;
301            while Source (S) /= ':' loop
302               S := S + 1;
303            end loop;
304
305            Check_Obsolescent_Features_Restriction (S);
306
307            if Warn_On_Obsolescent_Feature then
308               Error_Msg
309                 ("?j?use of "":"" is an obsolescent feature (RM J.2(3))", S);
310               Error_Msg
311                 ("\?j?use ""'#"" instead", S);
312            end if;
313         end if;
314      end Check_Obsolete_Base_Char;
315
316   --  Start of processing for Post_Scan
317
318   begin
319      case Token is
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);
324
325         when Tok_Identifier =>
326            Token_Node := New_Node (N_Identifier, Token_Ptr);
327            Set_Chars (Token_Node, Token_Name);
328
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;
333
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;
338
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);
346
347            if Source (Token_Ptr) = '%' then
348               Check_Obsolescent_Features_Restriction (Token_Ptr);
349
350               if Warn_On_Obsolescent_Feature then
351                  Error_Msg_SC
352                    ("?j?use of ""'%"" is an obsolescent feature (RM J.2(4))");
353                  Error_Msg_SC ("\?j?use """""" instead");
354               end if;
355            end if;
356
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);
361
362         when Tok_Vertical_Bar =>
363            if Source (Token_Ptr) = '!' then
364               Check_Obsolescent_Features_Restriction (Token_Ptr);
365
366               if Warn_On_Obsolescent_Feature then
367                  Error_Msg_SC
368                    ("?j?use of ""'!"" is an obsolescent feature (RM J.2(2))");
369                  Error_Msg_SC ("\?j?use ""'|"" instead");
370               end if;
371            end if;
372
373         when others =>
374            null;
375      end case;
376   end Post_Scan;
377
378   ------------------------------
379   -- Scan_Reserved_Identifier --
380   ------------------------------
381
382   procedure Scan_Reserved_Identifier (Force_Msg : Boolean) is
383      Token_Chars : constant String := Token_Type'Image (Token);
384
385   begin
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.
388
389      Name_Len := 0;
390
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));
394      end loop;
395
396      Token_Name := Name_Find;
397
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;
402      end if;
403
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;
408
409end Scn;
410