OSDN Git Service

2007-04-20 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / scn.adb
index 1e829e4..6f8ea91 100644 (file)
@@ -6,7 +6,7 @@
 --                                                                          --
 --                                 B o d y                                  --
 --                                                                          --
---          Copyright (C) 1992-2003 Free Software Foundation, Inc.          --
+--          Copyright (C) 1992-2006, Free Software Foundation, Inc.         --
 --                                                                          --
 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
 -- terms of the  GNU General Public License as published  by the Free Soft- --
@@ -16,8 +16,8 @@
 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
 -- for  more details.  You should have  received  a copy of the GNU General --
 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
--- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
--- MA 02111-1307, USA.                                                      --
+-- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
+-- Boston, MA 02110-1301, USA.                                              --
 --                                                                          --
 -- GNAT was originally developed  by the GNAT team at  New York University. --
 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
 
 with Atree;    use Atree;
 with Csets;    use Csets;
-with Hostparm;
+with Hostparm; use Hostparm;
 with Namet;    use Namet;
 with Opt;      use Opt;
+with Restrict; use Restrict;
+with Rident;   use Rident;
 with Scans;    use Scans;
 with Sinfo;    use Sinfo;
 with Sinput;   use Sinput;
+with Uintp;    use Uintp;
 
 package body Scn is
 
@@ -43,8 +46,8 @@ package body Scn is
    --  keyword as an identifier once for a given keyword).
 
    procedure Check_End_Of_Line;
-   --  Called when end of line encountered. Checks that line is not
-   --  too long, and that other style checks for the end of line are met.
+   --  Called when end of line encountered. Checks that line is not too long,
+   --  and that other style checks for the end of line are met.
 
    function Determine_License return License_Type;
    --  Scan header of file and check that it has an appropriate GNAT-style
@@ -54,58 +57,17 @@ package body Scn is
    procedure Error_Long_Line;
    --  Signal error of excessively long line
 
-   ---------------
-   -- Post_Scan --
-   ---------------
-
-   procedure Post_Scan is
-   begin
-      case Token is
-         when Tok_Char_Literal =>
-            Token_Node := New_Node (N_Character_Literal, Token_Ptr);
-            Set_Char_Literal_Value (Token_Node, Character_Code);
-            Set_Chars (Token_Node, Token_Name);
-
-         when Tok_Identifier =>
-            Token_Node := New_Node (N_Identifier, Token_Ptr);
-            Set_Chars (Token_Node, Token_Name);
-
-         when Tok_Real_Literal =>
-            Token_Node := New_Node (N_Real_Literal, Token_Ptr);
-            Set_Realval (Token_Node, Real_Literal_Value);
-
-         when Tok_Integer_Literal =>
-            Token_Node := New_Node (N_Integer_Literal, Token_Ptr);
-            Set_Intval (Token_Node, Int_Literal_Value);
-
-         when Tok_String_Literal =>
-            Token_Node := New_Node (N_String_Literal, Token_Ptr);
-            Set_Has_Wide_Character (Token_Node, Wide_Character_Found);
-            Set_Strval (Token_Node, String_Literal_Id);
-
-         when Tok_Operator_Symbol =>
-            Token_Node := New_Node (N_Operator_Symbol, Token_Ptr);
-            Set_Chars (Token_Node, Token_Name);
-            Set_Strval (Token_Node, String_Literal_Id);
-
-         when others =>
-            null;
-      end case;
-   end Post_Scan;
-
    -----------------------
    -- Check_End_Of_Line --
    -----------------------
 
    procedure Check_End_Of_Line is
       Len : constant Int := Int (Scan_Ptr) - Int (Current_Line_Start);
-
    begin
-      if Len > Hostparm.Max_Line_Length then
-         Error_Long_Line;
-
-      elsif Style_Check then
+      if Style_Check then
          Style.Check_Line_Terminator (Len);
+      elsif Len > Max_Line_Length then
+         Error_Long_Line;
       end if;
    end Check_End_Of_Line;
 
@@ -115,6 +77,7 @@ package body Scn is
 
    function Determine_License return License_Type is
       GPL_Found : Boolean := False;
+      Result    : License_Type;
 
       function Contains (S : String) return Boolean;
       --  See if current comment contains successive non-blank characters
@@ -134,8 +97,15 @@ package body Scn is
          SS : Source_Ptr;
 
       begin
+         --  Loop to check characters. This loop is terminated by end of
+         --  line, and also we need to check for the EOF case, to take
+         --  care of files containing only comments.
+
          SP := Scan_Ptr;
-         while Source (SP) /= CR and then Source (SP) /= LF loop
+         while Source (SP) /= CR and then
+               Source (SP) /= LF and then
+               Source (SP) /= EOF
+         loop
             if Source (SP) = S (S'First) then
                SS := SP;
                CP := S'First;
@@ -170,6 +140,7 @@ package body Scn is
       begin
          while Source (Scan_Ptr) /= CR
            and then Source (Scan_Ptr) /= LF
+           and then Source (Scan_Ptr) /= EOF
          loop
             Scan_Ptr := Scan_Ptr + 1;
          end loop;
@@ -183,14 +154,17 @@ package body Scn is
            or else Source (Scan_Ptr + 1) /= '-'
          then
             if GPL_Found then
-               return GPL;
+               Result := GPL;
+               exit;
             else
-               return Unknown;
+               Result := Unknown;
+               exit;
             end if;
 
          elsif Contains ("Asaspecialexception") then
             if GPL_Found then
-               return Modified_GPL;
+               Result := Modified_GPL;
+               exit;
             end if;
 
          elsif Contains ("GNUGeneralPublicLicense") then
@@ -203,29 +177,38 @@ package body Scn is
              Contains
               ("ThisspecificationisderivedfromtheAdaReferenceManual")
          then
-            return Unrestricted;
+            Result := Unrestricted;
+            exit;
          end if;
 
          Skip_EOL;
 
          Check_End_Of_Line;
 
-         declare
-            Physical : Boolean;
+         if Source (Scan_Ptr) /= EOF then
 
-         begin
-            Skip_Line_Terminators (Scan_Ptr, Physical);
+            --  We have to take into account a degenerate case when the source
+            --  file contains only comments and no Ada code.
 
-            --  If we are at start of physical line, update scan pointers
-            --  to reflect the start of the new line.
+            declare
+               Physical : Boolean;
 
-            if Physical then
-               Current_Line_Start       := Scan_Ptr;
-               Start_Column             := Scanner.Set_Start_Column;
-               First_Non_Blank_Location := Scan_Ptr;
-            end if;
-         end;
+            begin
+               Skip_Line_Terminators (Scan_Ptr, Physical);
+
+               --  If we are at start of physical line, update scan pointers
+               --  to reflect the start of the new line.
+
+               if Physical then
+                  Current_Line_Start       := Scan_Ptr;
+                  Start_Column             := Scanner.Set_Start_Column;
+                  First_Non_Blank_Location := Scan_Ptr;
+               end if;
+            end;
+         end if;
       end loop;
+
+      return Result;
    end Determine_License;
 
    ----------------------------
@@ -245,7 +228,7 @@ package body Scn is
    begin
       Error_Msg
         ("this line is too long",
-         Current_Line_Start + Hostparm.Max_Line_Length);
+         Current_Line_Start + Source_Ptr (Max_Line_Length));
    end Error_Long_Line;
 
    ------------------------
@@ -259,9 +242,17 @@ package body Scn is
       GNAT_Hedr : constant Text_Buffer (1 .. 78) := (others => '-');
 
    begin
-      Scanner.Initialize_Scanner (Unit, Index);
+      Scanner.Initialize_Scanner (Index);
+
+      if Index /= Internal_Source_File then
+         Set_Unit (Index, Unit);
+      end if;
 
-      --  Set default for Comes_From_Source. All nodes built now until we
+      Current_Source_Unit := Unit;
+
+      --  Set default for Comes_From_Source (except if we are going to process
+      --  an artificial string internally created within the compiler and
+      --  placed into internal source duffer). All nodes built now until we
       --  reenter the analyzer will have Comes_From_Source set to True
 
       if Index /= Internal_Source_File then
@@ -280,6 +271,16 @@ package body Scn is
       --  call Scan. Scan initial token (note this initializes Prev_Token,
       --  Prev_Token_Ptr).
 
+      --  There are two reasons not to do the Scan step in case if we
+      --  initialize the scanner for the internal source buffer:
+
+      --  - The artificial string may not be created by the compiler in this
+      --    buffer when we call Initialize_Scanner
+
+      --  - For these artificial strings a special way of scanning is used, so
+      --    the standard step of the scanner may just break the algorithm of
+      --    processing these strings.
+
       if Index /= Internal_Source_File then
          Scan;
       end if;
@@ -291,6 +292,59 @@ package body Scn is
       end loop;
    end Initialize_Scanner;
 
+   -----------------------
+   -- Obsolescent_Check --
+   -----------------------
+
+   procedure Obsolescent_Check (S : Source_Ptr) is
+   begin
+      --  This is a pain in the neck case, since we normally need a node to
+      --  call Check_Restrictions, and all we have is a source pointer. The
+      --  easiest thing is to construct a dummy node. A bit kludgy, but this
+      --  is a marginal case. It's not worth trying to do things more cleanly.
+
+      Check_Restriction (No_Obsolescent_Features, New_Node (N_Empty, S));
+   end Obsolescent_Check;
+
+   ---------------
+   -- Post_Scan --
+   ---------------
+
+   procedure Post_Scan is
+   begin
+      case Token is
+         when Tok_Char_Literal =>
+            Token_Node := New_Node (N_Character_Literal, Token_Ptr);
+            Set_Char_Literal_Value (Token_Node, UI_From_CC (Character_Code));
+            Set_Chars (Token_Node, Token_Name);
+
+         when Tok_Identifier =>
+            Token_Node := New_Node (N_Identifier, Token_Ptr);
+            Set_Chars (Token_Node, Token_Name);
+
+         when Tok_Real_Literal =>
+            Token_Node := New_Node (N_Real_Literal, Token_Ptr);
+            Set_Realval (Token_Node, Real_Literal_Value);
+
+         when Tok_Integer_Literal =>
+            Token_Node := New_Node (N_Integer_Literal, Token_Ptr);
+            Set_Intval (Token_Node, Int_Literal_Value);
+
+         when Tok_String_Literal =>
+            Token_Node := New_Node (N_String_Literal, Token_Ptr);
+            Set_Has_Wide_Character (Token_Node, Wide_Character_Found);
+            Set_Strval (Token_Node, String_Literal_Id);
+
+         when Tok_Operator_Symbol =>
+            Token_Node := New_Node (N_Operator_Symbol, Token_Ptr);
+            Set_Chars (Token_Node, Token_Name);
+            Set_Strval (Token_Node, String_Literal_Id);
+
+         when others =>
+            null;
+      end case;
+   end Post_Scan;
+
    ------------------------------
    -- Scan_Reserved_Identifier --
    ------------------------------