OSDN Git Service

2008-03-26 Javier Miranda <miranda@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / scn.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                                  S C N                                   --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2007, 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
26 with Atree;    use Atree;
27 with Csets;    use Csets;
28 with Hostparm; use Hostparm;
29 with Namet;    use Namet;
30 with Opt;      use Opt;
31 with Output;   use Output;
32 with Restrict; use Restrict;
33 with Rident;   use Rident;
34 with Scans;    use Scans;
35 with Sinfo;    use Sinfo;
36 with Sinput;   use Sinput;
37 with Uintp;    use Uintp;
38
39 with GNAT.Byte_Order_Mark; use GNAT.Byte_Order_Mark;
40
41 with System.WCh_Con; use System.WCh_Con;
42
43 package body Scn is
44
45    use ASCII;
46
47    Used_As_Identifier : array (Token_Type) of Boolean;
48    --  Flags set True if a given keyword is used as an identifier (used to
49    --  make sure that we only post an error message for incorrect use of a
50    --  keyword as an identifier once for a given keyword).
51
52    procedure Check_End_Of_Line;
53    --  Called when end of line encountered. Checks that line is not too long,
54    --  and that other style checks for the end of line are met.
55
56    function Determine_License return License_Type;
57    --  Scan header of file and check that it has an appropriate GNAT-style
58    --  header with a proper license statement. Returns GPL, Unrestricted,
59    --  or Modified_GPL depending on header. If none of these, returns Unknown.
60
61    procedure Error_Long_Line;
62    --  Signal error of excessively long line
63
64    -----------------------
65    -- Check_End_Of_Line --
66    -----------------------
67
68    procedure Check_End_Of_Line is
69       Len : constant Int := Int (Scan_Ptr) - Int (Current_Line_Start);
70    begin
71       if Style_Check then
72          Style.Check_Line_Terminator (Len);
73       elsif Len > Max_Line_Length then
74          Error_Long_Line;
75       end if;
76    end Check_End_Of_Line;
77
78    -----------------------
79    -- Determine_License --
80    -----------------------
81
82    function Determine_License return License_Type is
83       GPL_Found : Boolean := False;
84       Result    : License_Type;
85
86       function Contains (S : String) return Boolean;
87       --  See if current comment contains successive non-blank characters
88       --  matching the contents of S. If so leave Scan_Ptr unchanged and
89       --  return True, otherwise leave Scan_Ptr unchanged and return False.
90
91       procedure Skip_EOL;
92       --  Skip to line terminator character
93
94       --------------
95       -- Contains --
96       --------------
97
98       function Contains (S : String) return Boolean is
99          CP : Natural;
100          SP : Source_Ptr;
101          SS : Source_Ptr;
102
103       begin
104          --  Loop to check characters. This loop is terminated by end of
105          --  line, and also we need to check for the EOF case, to take
106          --  care of files containing only comments.
107
108          SP := Scan_Ptr;
109          while Source (SP) /= CR and then
110                Source (SP) /= LF and then
111                Source (SP) /= EOF
112          loop
113             if Source (SP) = S (S'First) then
114                SS := SP;
115                CP := S'First;
116
117                loop
118                   SS := SS + 1;
119                   CP := CP + 1;
120
121                   if CP > S'Last then
122                      return True;
123                   end if;
124
125                   while Source (SS) = ' ' loop
126                      SS := SS + 1;
127                   end loop;
128
129                   exit when Source (SS) /= S (CP);
130                end loop;
131             end if;
132
133             SP := SP + 1;
134          end loop;
135
136          return False;
137       end Contains;
138
139       --------------
140       -- Skip_EOL --
141       --------------
142
143       procedure Skip_EOL is
144       begin
145          while Source (Scan_Ptr) /= CR
146            and then Source (Scan_Ptr) /= LF
147            and then Source (Scan_Ptr) /= EOF
148          loop
149             Scan_Ptr := Scan_Ptr + 1;
150          end loop;
151       end Skip_EOL;
152
153    --  Start of processing for Determine_License
154
155    begin
156       loop
157          if Source (Scan_Ptr) /= '-'
158            or else Source (Scan_Ptr + 1) /= '-'
159          then
160             if GPL_Found then
161                Result := GPL;
162                exit;
163             else
164                Result := Unknown;
165                exit;
166             end if;
167
168          elsif Contains ("Asaspecialexception") then
169             if GPL_Found then
170                Result := Modified_GPL;
171                exit;
172             end if;
173
174          elsif Contains ("GNUGeneralPublicLicense") then
175             GPL_Found := True;
176
177          elsif
178              Contains
179                ("ThisspecificationisadaptedfromtheAdaSemanticInterface")
180            or else
181              Contains
182               ("ThisspecificationisderivedfromtheAdaReferenceManual")
183          then
184             Result := Unrestricted;
185             exit;
186          end if;
187
188          Skip_EOL;
189
190          Check_End_Of_Line;
191
192          if Source (Scan_Ptr) /= EOF then
193
194             --  We have to take into account a degenerate case when the source
195             --  file contains only comments and no Ada code.
196
197             declare
198                Physical : Boolean;
199
200             begin
201                Skip_Line_Terminators (Scan_Ptr, Physical);
202
203                --  If we are at start of physical line, update scan pointers
204                --  to reflect the start of the new line.
205
206                if Physical then
207                   Current_Line_Start       := Scan_Ptr;
208                   Start_Column             := Scanner.Set_Start_Column;
209                   First_Non_Blank_Location := Scan_Ptr;
210                end if;
211             end;
212          end if;
213       end loop;
214
215       return Result;
216    end Determine_License;
217
218    ----------------------------
219    -- Determine_Token_Casing --
220    ----------------------------
221
222    function Determine_Token_Casing return Casing_Type is
223    begin
224       return Scanner.Determine_Token_Casing;
225    end Determine_Token_Casing;
226
227    ---------------------
228    -- Error_Long_Line --
229    ---------------------
230
231    procedure Error_Long_Line is
232    begin
233       Error_Msg
234         ("this line is too long",
235          Current_Line_Start + Source_Ptr (Max_Line_Length));
236    end Error_Long_Line;
237
238    ------------------------
239    -- Initialize_Scanner --
240    ------------------------
241
242    procedure Initialize_Scanner
243      (Unit  : Unit_Number_Type;
244       Index : Source_File_Index)
245    is
246       GNAT_Hedr : constant Text_Buffer (1 .. 78) := (others => '-');
247
248    begin
249       Scanner.Initialize_Scanner (Index);
250
251       if Index /= Internal_Source_File then
252          Set_Unit (Index, Unit);
253       end if;
254
255       Current_Source_Unit := Unit;
256
257       --  Set default for Comes_From_Source (except if we are going to process
258       --  an artificial string internally created within the compiler and
259       --  placed into internal source duffer). All nodes built now until we
260       --  reenter the analyzer will have Comes_From_Source set to True
261
262       if Index /= Internal_Source_File then
263          Set_Comes_From_Source_Default (True);
264       end if;
265
266       --  Check license if GNAT type header possibly present
267
268       if Source_Last (Index) - Scan_Ptr > 80
269         and then Source (Scan_Ptr .. Scan_Ptr + 77) = GNAT_Hedr
270       then
271          Set_License (Current_Source_File, Determine_License);
272       end if;
273
274       --  Check for BOM
275
276       declare
277          BOM : BOM_Kind;
278          Len : Natural;
279          Tst : String (1 .. 5);
280
281       begin
282          for J in 1 .. 5 loop
283             Tst (J) := Source (Scan_Ptr + Source_Ptr (J) - 1);
284          end loop;
285
286          Read_BOM (Tst, Len, BOM, False);
287
288          case BOM is
289             when UTF8_All =>
290                Scan_Ptr := Scan_Ptr + Source_Ptr (Len);
291                Wide_Character_Encoding_Method := WCEM_UTF8;
292                Upper_Half_Encoding := True;
293
294             when UTF16_LE | UTF16_BE =>
295                Write_Line ("UTF-16 encoding format not recognized");
296                raise Unrecoverable_Error;
297
298             when UTF32_LE | UTF32_BE =>
299                Write_Line ("UTF-32 encoding format not recognized");
300                raise Unrecoverable_Error;
301
302             when Unknown =>
303                null;
304
305             when others =>
306                raise Program_Error;
307          end case;
308       end;
309
310       --  Because of the License stuff above, Scng.Initialize_Scanner cannot
311       --  call Scan. Scan initial token (note this initializes Prev_Token,
312       --  Prev_Token_Ptr).
313
314       --  There are two reasons not to do the Scan step in case if we
315       --  initialize the scanner for the internal source buffer:
316
317       --  - The artificial string may not be created by the compiler in this
318       --    buffer when we call Initialize_Scanner
319
320       --  - For these artificial strings a special way of scanning is used, so
321       --    the standard step of the scanner may just break the algorithm of
322       --    processing these strings.
323
324       if Index /= Internal_Source_File then
325          Scan;
326       end if;
327
328       --  Clear flags for reserved words used as indentifiers
329
330       for J in Token_Type loop
331          Used_As_Identifier (J) := False;
332       end loop;
333    end Initialize_Scanner;
334
335    -----------------------
336    -- Obsolescent_Check --
337    -----------------------
338
339    procedure Obsolescent_Check (S : Source_Ptr) is
340    begin
341       --  This is a pain in the neck case, since we normally need a node to
342       --  call Check_Restrictions, and all we have is a source pointer. The
343       --  easiest thing is to construct a dummy node. A bit kludgy, but this
344       --  is a marginal case. It's not worth trying to do things more cleanly.
345
346       Check_Restriction (No_Obsolescent_Features, New_Node (N_Empty, S));
347    end Obsolescent_Check;
348
349    ---------------
350    -- Post_Scan --
351    ---------------
352
353    procedure Post_Scan is
354    begin
355       case Token is
356          when Tok_Char_Literal =>
357             Token_Node := New_Node (N_Character_Literal, Token_Ptr);
358             Set_Char_Literal_Value (Token_Node, UI_From_CC (Character_Code));
359             Set_Chars (Token_Node, Token_Name);
360
361          when Tok_Identifier =>
362             Token_Node := New_Node (N_Identifier, Token_Ptr);
363             Set_Chars (Token_Node, Token_Name);
364
365          when Tok_Real_Literal =>
366             Token_Node := New_Node (N_Real_Literal, Token_Ptr);
367             Set_Realval (Token_Node, Real_Literal_Value);
368
369          when Tok_Integer_Literal =>
370             Token_Node := New_Node (N_Integer_Literal, Token_Ptr);
371             Set_Intval (Token_Node, Int_Literal_Value);
372
373          when Tok_String_Literal =>
374             Token_Node := New_Node (N_String_Literal, Token_Ptr);
375             Set_Has_Wide_Character (Token_Node, Wide_Character_Found);
376             Set_Strval (Token_Node, String_Literal_Id);
377
378          when Tok_Operator_Symbol =>
379             Token_Node := New_Node (N_Operator_Symbol, Token_Ptr);
380             Set_Chars (Token_Node, Token_Name);
381             Set_Strval (Token_Node, String_Literal_Id);
382
383          when others =>
384             null;
385       end case;
386    end Post_Scan;
387
388    ------------------------------
389    -- Scan_Reserved_Identifier --
390    ------------------------------
391
392    procedure Scan_Reserved_Identifier (Force_Msg : Boolean) is
393       Token_Chars : constant String := Token_Type'Image (Token);
394
395    begin
396       --  We have in Token_Chars the image of the Token name, i.e. Tok_xxx.
397       --  This code extracts the xxx and makes an identifier out of it.
398
399       Name_Len := 0;
400
401       for J in 5 .. Token_Chars'Length loop
402          Name_Len := Name_Len + 1;
403          Name_Buffer (Name_Len) := Fold_Lower (Token_Chars (J));
404       end loop;
405
406       Token_Name := Name_Find;
407
408       if not Used_As_Identifier (Token) or else Force_Msg then
409          Error_Msg_Name_1 := Token_Name;
410          Error_Msg_SC ("reserved word* cannot be used as identifier!");
411          Used_As_Identifier (Token) := True;
412       end if;
413
414       Token := Tok_Identifier;
415       Token_Node := New_Node (N_Identifier, Token_Ptr);
416       Set_Chars (Token_Node, Token_Name);
417    end Scan_Reserved_Identifier;
418
419 end Scn;