OSDN Git Service

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