OSDN Git Service

2004-10-04 Vincent Celier <celier@gnat.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-2004 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 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.                                                      --
21 --                                                                          --
22 -- GNAT was originally developed  by the GNAT team at  New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
24 --                                                                          --
25 ------------------------------------------------------------------------------
26
27 with Atree;    use Atree;
28 with Csets;    use Csets;
29 with Namet;    use Namet;
30 with Opt;      use Opt;
31 with Scans;    use Scans;
32 with Sinfo;    use Sinfo;
33 with Sinput;   use Sinput;
34
35 package body Scn is
36
37    use ASCII;
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    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.
47
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.
52
53    procedure Error_Long_Line;
54    --  Signal error of excessively long line
55
56    ---------------
57    -- Post_Scan --
58    ---------------
59
60    procedure Post_Scan is
61    begin
62       case Token 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);
67
68          when Tok_Identifier =>
69             Token_Node := New_Node (N_Identifier, Token_Ptr);
70             Set_Chars (Token_Node, Token_Name);
71
72          when Tok_Real_Literal =>
73             Token_Node := New_Node (N_Real_Literal, Token_Ptr);
74             Set_Realval (Token_Node, Real_Literal_Value);
75
76          when Tok_Integer_Literal =>
77             Token_Node := New_Node (N_Integer_Literal, Token_Ptr);
78             Set_Intval (Token_Node, Int_Literal_Value);
79
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);
84
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);
89
90          when others =>
91             null;
92       end case;
93    end Post_Scan;
94
95    -----------------------
96    -- Check_End_Of_Line --
97    -----------------------
98
99    procedure Check_End_Of_Line is
100       Len : constant Int := Int (Scan_Ptr) - Int (Current_Line_Start);
101    begin
102       if Style_Check then
103          Style.Check_Line_Terminator (Len);
104       elsif Len > Opt.Max_Line_Length then
105          Error_Long_Line;
106       end if;
107    end Check_End_Of_Line;
108
109    -----------------------
110    -- Determine_License --
111    -----------------------
112
113    function Determine_License return License_Type is
114       GPL_Found : Boolean := False;
115       Result    : License_Type;
116
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.
121
122       procedure Skip_EOL;
123       --  Skip to line terminator character
124
125       --------------
126       -- Contains --
127       --------------
128
129       function Contains (S : String) return Boolean is
130          CP : Natural;
131          SP : Source_Ptr;
132          SS : Source_Ptr;
133
134       begin
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.
138
139          SP := Scan_Ptr;
140          while Source (SP) /= CR and then
141                Source (SP) /= LF and then
142                Source (SP) /= EOF
143          loop
144             if Source (SP) = S (S'First) then
145                SS := SP;
146                CP := S'First;
147
148                loop
149                   SS := SS + 1;
150                   CP := CP + 1;
151
152                   if CP > S'Last then
153                      return True;
154                   end if;
155
156                   while Source (SS) = ' ' loop
157                      SS := SS + 1;
158                   end loop;
159
160                   exit when Source (SS) /= S (CP);
161                end loop;
162             end if;
163
164             SP := SP + 1;
165          end loop;
166
167          return False;
168       end Contains;
169
170       --------------
171       -- Skip_EOL --
172       --------------
173
174       procedure Skip_EOL is
175       begin
176          while Source (Scan_Ptr) /= CR
177            and then Source (Scan_Ptr) /= LF
178            and then Source (Scan_Ptr) /= EOF
179          loop
180             Scan_Ptr := Scan_Ptr + 1;
181          end loop;
182       end Skip_EOL;
183
184    --  Start of processing for Determine_License
185
186    begin
187       loop
188          if Source (Scan_Ptr) /= '-'
189            or else Source (Scan_Ptr + 1) /= '-'
190          then
191             if GPL_Found then
192                Result := GPL;
193                exit;
194             else
195                Result := Unknown;
196                exit;
197             end if;
198
199          elsif Contains ("Asaspecialexception") then
200             if GPL_Found then
201                Result := Modified_GPL;
202                exit;
203             end if;
204
205          elsif Contains ("GNUGeneralPublicLicense") then
206             GPL_Found := True;
207
208          elsif
209              Contains
210                ("ThisspecificationisadaptedfromtheAdaSemanticInterface")
211            or else
212              Contains
213               ("ThisspecificationisderivedfromtheAdaReferenceManual")
214          then
215             Result := Unrestricted;
216             exit;
217          end if;
218
219          Skip_EOL;
220
221          Check_End_Of_Line;
222
223          if Source (Scan_Ptr) /= EOF then
224
225             --  We have to take into account a degenerate case when the source
226             --  file contains only comments and no Ada code.
227
228             declare
229                Physical : Boolean;
230
231             begin
232                Skip_Line_Terminators (Scan_Ptr, Physical);
233
234                --  If we are at start of physical line, update scan pointers
235                --  to reflect the start of the new line.
236
237                if Physical then
238                   Current_Line_Start       := Scan_Ptr;
239                   Start_Column             := Scanner.Set_Start_Column;
240                   First_Non_Blank_Location := Scan_Ptr;
241                end if;
242             end;
243          end if;
244       end loop;
245
246       return Result;
247    end Determine_License;
248
249    ----------------------------
250    -- Determine_Token_Casing --
251    ----------------------------
252
253    function Determine_Token_Casing return Casing_Type is
254    begin
255       return Scanner.Determine_Token_Casing;
256    end Determine_Token_Casing;
257
258    ---------------------
259    -- Error_Long_Line --
260    ---------------------
261
262    procedure Error_Long_Line is
263    begin
264       Error_Msg
265         ("this line is too long",
266          Current_Line_Start + Source_Ptr (Opt.Max_Line_Length));
267    end Error_Long_Line;
268
269    ------------------------
270    -- Initialize_Scanner --
271    ------------------------
272
273    procedure Initialize_Scanner
274      (Unit  : Unit_Number_Type;
275       Index : Source_File_Index)
276    is
277       GNAT_Hedr : constant Text_Buffer (1 .. 78) := (others => '-');
278
279    begin
280       Scanner.Initialize_Scanner (Unit, Index);
281
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
286
287       if Index /= Internal_Source_File then
288          Set_Comes_From_Source_Default (True);
289       end if;
290
291       --  Check license if GNAT type header possibly present
292
293       if Source_Last (Index) - Scan_Ptr > 80
294         and then Source (Scan_Ptr .. Scan_Ptr + 77) = GNAT_Hedr
295       then
296          Set_License (Current_Source_File, Determine_License);
297       end if;
298
299       --  Because of the License stuff above, Scng.Initialize_Scanner cannot
300       --  call Scan. Scan initial token (note this initializes Prev_Token,
301       --  Prev_Token_Ptr).
302
303       --  There are two reasons not to do the Scan step in case if we
304       --  initialize the scanner for the internal source buffer:
305
306       --  - The artificial string may not be created by the compiler in this
307       --    buffer when we call Initialize_Scanner
308
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.
312
313       if Index /= Internal_Source_File then
314          Scan;
315       end if;
316
317       --  Clear flags for reserved words used as indentifiers
318
319       for J in Token_Type loop
320          Used_As_Identifier (J) := False;
321       end loop;
322    end Initialize_Scanner;
323
324    ------------------------------
325    -- Scan_Reserved_Identifier --
326    ------------------------------
327
328    procedure Scan_Reserved_Identifier (Force_Msg : Boolean) is
329       Token_Chars : constant String := Token_Type'Image (Token);
330
331    begin
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.
334
335       Name_Len := 0;
336
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));
340       end loop;
341
342       Token_Name := Name_Find;
343
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;
348       end if;
349
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;
354
355 end Scn;