OSDN Git Service

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