1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT COMPILER COMPONENTS                         --
4--                                                                          --
5--                                  S C N                                   --
6--                                                                          --
7--                                 B o d y                                  --
8--                                                                          --
9--          Copyright (C) 1992-2020, 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   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).
43
44   ----------------------------
45   -- Determine_Token_Casing --
46   ----------------------------
47
48   function Determine_Token_Casing return Casing_Type is
49   begin
50      return Scanner.Determine_Token_Casing;
51   end Determine_Token_Casing;
52
53   ------------------------
54   -- Initialize_Scanner --
55   ------------------------
56
57   procedure Initialize_Scanner
58     (Unit  : Unit_Number_Type;
59      Index : Source_File_Index) is
60   begin
61      Scanner.Initialize_Scanner (Index);
62      Set_Unit (Index, Unit);
63
64      Current_Source_Unit := Unit;
65
66      --  Set default for Comes_From_Source. All nodes built now until we
67      --  reenter the analyzer will have Comes_From_Source set to True
68
69      Set_Comes_From_Source_Default (True);
70
71      Check_For_BOM;
72
73      --  Because of the License stuff above, Scng.Initialize_Scanner cannot
74      --  call Scan. Scan initial token (note this initializes Prev_Token,
75      --  Prev_Token_Ptr).
76
77      Scan;
78
79      --  Clear flags for reserved words used as identifiers
80
81      Used_As_Identifier := (others => False);
82   end Initialize_Scanner;
83
84   ---------------
85   -- Post_Scan --
86   ---------------
87
88   procedure Post_Scan is
89      procedure Check_Obsolescent_Features_Restriction (S : Source_Ptr);
90      --  This checks for Obsolescent_Features restriction being active, and
91      --  if so, flags the restriction as occurring at the given scan location.
92
93      procedure Check_Obsolete_Base_Char;
94      --  Check for numeric literal using ':' instead of '#' for based case
95
96      --------------------------------------------
97      -- Check_Obsolescent_Features_Restriction --
98      --------------------------------------------
99
100      procedure Check_Obsolescent_Features_Restriction (S : Source_Ptr) is
101      begin
102         --  Normally we have a node handy for posting restrictions. We don't
103         --  have such a node here, so construct a dummy one with the right
104         --  scan pointer. This is only used to get the Sloc value anyway.
105
106         Check_Restriction (No_Obsolescent_Features, New_Node (N_Empty, S));
107      end Check_Obsolescent_Features_Restriction;
108
109      ------------------------------
110      -- Check_Obsolete_Base_Char --
111      ------------------------------
112
113      procedure Check_Obsolete_Base_Char is
114         S : Source_Ptr;
115
116      begin
117         if Based_Literal_Uses_Colon then
118
119            --  Find the : for the restriction or warning message
120
121            S := Token_Ptr;
122            while Source (S) /= ':' loop
123               S := S + 1;
124            end loop;
125
126            Check_Obsolescent_Features_Restriction (S);
127
128            if Warn_On_Obsolescent_Feature then
129               Error_Msg
130                 ("?j?use of "":"" is an obsolescent feature (RM J.2(3))", S);
131               Error_Msg
132                 ("\?j?use ""'#"" instead", S);
133            end if;
134         end if;
135      end Check_Obsolete_Base_Char;
136
137   --  Start of processing for Post_Scan
138
139   begin
140      case Token is
141         when Tok_Char_Literal =>
142            Token_Node := New_Node (N_Character_Literal, Token_Ptr);
143            Set_Char_Literal_Value (Token_Node, UI_From_CC (Character_Code));
144            Set_Chars (Token_Node, Token_Name);
145
146         when Tok_Identifier =>
147            Token_Node := New_Node (N_Identifier, Token_Ptr);
148            Set_Chars (Token_Node, Token_Name);
149
150         when Tok_Real_Literal =>
151            Token_Node := New_Node (N_Real_Literal, Token_Ptr);
152            Set_Realval (Token_Node, Real_Literal_Value);
153            Check_Obsolete_Base_Char;
154
155         when Tok_Integer_Literal =>
156            Token_Node := New_Node (N_Integer_Literal, Token_Ptr);
157            Set_Intval (Token_Node, Int_Literal_Value);
158            Check_Obsolete_Base_Char;
159
160         when Tok_String_Literal =>
161            Token_Node := New_Node (N_String_Literal, Token_Ptr);
162            Set_Has_Wide_Character
163              (Token_Node, Wide_Character_Found);
164            Set_Has_Wide_Wide_Character
165              (Token_Node, Wide_Wide_Character_Found);
166            Set_Strval (Token_Node, String_Literal_Id);
167
168            if Source (Token_Ptr) = '%' then
169               Check_Obsolescent_Features_Restriction (Token_Ptr);
170
171               if Warn_On_Obsolescent_Feature then
172                  Error_Msg_SC
173                    ("?j?use of ""'%"" is an obsolescent feature (RM J.2(4))");
174                  Error_Msg_SC ("\?j?use """""" instead");
175               end if;
176            end if;
177
178         when Tok_Operator_Symbol =>
179            Token_Node := New_Node (N_Operator_Symbol, Token_Ptr);
180            Set_Chars (Token_Node, Token_Name);
181            Set_Strval (Token_Node, String_Literal_Id);
182
183         when Tok_Vertical_Bar =>
184            if Source (Token_Ptr) = '!' then
185               Check_Obsolescent_Features_Restriction (Token_Ptr);
186
187               if Warn_On_Obsolescent_Feature then
188                  Error_Msg_SC
189                    ("?j?use of ""'!"" is an obsolescent feature (RM J.2(2))");
190                  Error_Msg_SC ("\?j?use ""'|"" instead");
191               end if;
192            end if;
193
194         when others =>
195            null;
196      end case;
197   end Post_Scan;
198
199   ------------------------------
200   -- Scan_Reserved_Identifier --
201   ------------------------------
202
203   procedure Scan_Reserved_Identifier (Force_Msg : Boolean) is
204      Token_Chars : String  := Token_Type'Image (Token);
205      Len         : Natural := 0;
206
207   begin
208      --  AI12-0125 : '@' denotes the target_name, i.e. serves as an
209      --  abbreviation for the LHS of an assignment.
210
211      if Token = Tok_At_Sign then
212         Token_Node := New_Node (N_Target_Name, Token_Ptr);
213         return;
214      end if;
215
216      --  We have in Token_Chars the image of the Token name, i.e. Tok_xxx.
217      --  This code extracts the xxx and makes an identifier out of it.
218
219      for J in 5 .. Token_Chars'Length loop
220         Len := Len + 1;
221         Token_Chars (Len) := Fold_Lower (Token_Chars (J));
222      end loop;
223
224      Token_Name := Name_Find (Token_Chars (1 .. Len));
225
226      --  If Inside_Pragma is True, we don't give an error. This is to allow
227      --  things like "pragma Ignore_Pragma (Interface)", where "Interface" is
228      --  a reserved word. There is no danger of missing errors, because any
229      --  misuse must have been preceded by an illegal declaration. For
230      --  example, in "pragma Pack (Begin);", either Begin is not declared,
231      --  which is an error, or it is declared, which will be an error on that
232      --  declaration.
233
234      if (not Used_As_Identifier (Token) or else Force_Msg)
235        and then not Inside_Pragma
236      then
237         Error_Msg_Name_1 := Token_Name;
238         Error_Msg_SC ("reserved word* cannot be used as identifier!");
239         Used_As_Identifier (Token) := True;
240      end if;
241
242      Token := Tok_Identifier;
243      Token_Node := New_Node (N_Identifier, Token_Ptr);
244      Set_Chars (Token_Node, Token_Name);
245   end Scan_Reserved_Identifier;
246
247end Scn;
248