[Ada] Parametrized expressions must be parenthesized

Arnaud Charlet charlet@adacore.com
Thu Oct 7 12:46:00 GMT 2010


This patch implements the restriction that parametrized expressions
must be parenthesized, which was missed in the original implementation.
The following test program is compiled first with -gnat2012 then with
-gnat2005.

     1. package PexpParen is
     2.    function F (A : Integer)
     3.      return Integer is A * A;
                               |
        >>> parametrized expression must be enclosed in parentheses

     4.    function G (A : Integer)
     5.      return Integer is (A * A);
     6. end;

     1. package PexpParen is
     2.    function F (A : Integer)
     3.      return Integer is A * A;
                               |
        >>> parametrized expression must be enclosed in parentheses
        >>> parametrized expression is an Ada 2012 feature
        >>> unit must be compiled with -gnat2012 switch

     4.    function G (A : Integer)
     5.      return Integer is (A * A);
                               |
        >>> parametrized expression is an Ada 2012 feature
        >>> unit must be compiled with -gnat2012 switch

     6. end;

Tested on x86_64-pc-linux-gnu, committed on trunk

2010-10-07  Robert Dewar  <dewar@adacore.com>

	* par-ch3.adb, par-ch6.adb, par-ch7.adb, par-ch9.adb, par-ch10.adb: Add
	Pexp to Pf_Rec constants
	(P_Subprogram): Expression is always enclosed in parentheses
	* par.adb (Pf_Rec): add Pexp flag for parametrized expression
	* sinfo.ads (N_Parametrized_Expression): Expression must be in parens

-------------- next part --------------
Index: par-ch9.adb
===================================================================
--- par-ch9.adb	(revision 165080)
+++ par-ch9.adb	(working copy)
@@ -651,7 +651,7 @@ package body Ch9 is
                Set_Must_Not_Override (Decl, Not_Overriding);
 
             elsif Token = Tok_Function or else Token = Tok_Procedure then
-               Decl := P_Subprogram (Pf_Decl);
+               Decl := P_Subprogram (Pf_Decl_Pexp);
 
                Set_Must_Override     (Specification (Decl), Is_Overriding);
                Set_Must_Not_Override (Specification (Decl), Not_Overriding);
@@ -682,7 +682,7 @@ package body Ch9 is
             return P_Entry_Declaration;
 
          elsif Token = Tok_Function or else Token = Tok_Procedure then
-            return P_Subprogram (Pf_Decl);
+            return P_Subprogram (Pf_Decl_Pexp);
 
          elsif Token = Tok_Identifier then
             L := New_List;
@@ -754,7 +754,7 @@ package body Ch9 is
                  or else
                Token = Tok_Not or else Bad_Spelling_Of (Tok_Not)
          then
-            Append (P_Subprogram (Pf_Decl_Pbod), Item_List);
+            Append (P_Subprogram (Pf_Decl_Pbod_Pexp), Item_List);
 
          elsif Token = Tok_Pragma or else Bad_Spelling_Of (Tok_Pragma) then
             P_Pragmas_Opt (Item_List);
Index: sinfo.ads
===================================================================
--- sinfo.ads	(revision 165098)
+++ sinfo.ads	(working copy)
@@ -4435,10 +4435,7 @@ package Sinfo is
       --  and put in its proper section when we know exactly where that is!
 
       --  PARAMETRIZED_EXPRESSION ::=
-      --    FUNCTION SPECIFICATION IS EXPRESSION;
-
-      --  Note: there are no separate nodes for the profiles, instead the
-      --  information appears directly in the following nodes.
+      --    FUNCTION SPECIFICATION IS (EXPRESSION);
 
       --  N_Parametrized_Expression
       --  Sloc points to FUNCTION
Index: par.adb
===================================================================
--- par.adb	(revision 165097)
+++ par.adb	(working copy)
@@ -352,7 +352,7 @@ function Par (Configuration_Pragmas : Bo
       Pbod : Boolean;                  -- True if proper body OK
       Rnam : Boolean;                  -- True if renaming declaration OK
       Stub : Boolean;                  -- True if body stub OK
-      Fil1 : Boolean;                  -- Filler to fill to 8 bits
+      Pexp : Boolean;                  -- True if parametried expression OK
       Fil2 : Boolean;                  -- Filler to fill to 8 bits
    end record;
    pragma Pack (Pf_Rec);
@@ -360,18 +360,18 @@ function Par (Configuration_Pragmas : Bo
    function T return Boolean renames True;
    function F return Boolean renames False;
 
-   Pf_Decl_Gins_Pbod_Rnam_Stub : constant Pf_Rec :=
-                                   Pf_Rec'(F, T, T, T, T, T, F, F);
-   Pf_Decl                     : constant Pf_Rec :=
-                                   Pf_Rec'(F, T, F, F, F, F, F, F);
-   Pf_Decl_Gins_Pbod_Rnam      : constant Pf_Rec :=
-                                   Pf_Rec'(F, T, T, T, T, F, F, F);
-   Pf_Decl_Pbod                : constant Pf_Rec :=
-                                   Pf_Rec'(F, T, F, T, F, F, F, F);
-   Pf_Pbod                     : constant Pf_Rec :=
-                                   Pf_Rec'(F, F, F, T, F, F, F, F);
-   Pf_Spcn                     : constant Pf_Rec :=
-                                   Pf_Rec'(T, F, F, F, F, F, F, F);
+   Pf_Decl_Gins_Pbod_Rnam_Stub_Pexp : constant Pf_Rec :=
+                                       Pf_Rec'(F, T, T, T, T, T, T, F);
+   Pf_Decl_Pexp                     : constant Pf_Rec :=
+                                       Pf_Rec'(F, T, F, F, F, F, T, F);
+   Pf_Decl_Gins_Pbod_Rnam_Pexp      : constant Pf_Rec :=
+                                       Pf_Rec'(F, T, T, T, T, F, T, F);
+   Pf_Decl_Pbod_Pexp                : constant Pf_Rec :=
+                                       Pf_Rec'(F, T, F, T, F, F, T, F);
+   Pf_Pbod_Pexp                     : constant Pf_Rec :=
+                                       Pf_Rec'(F, F, F, T, F, F, T, F);
+   Pf_Spcn                         : constant Pf_Rec :=
+                                       Pf_Rec'(T, F, F, F, F, F, F, F);
    --  The above are the only allowed values of Pf_Rec arguments
 
    type SS_Rec is record
Index: par-ch6.adb
===================================================================
--- par-ch6.adb	(revision 165099)
+++ par-ch6.adb	(working copy)
@@ -124,7 +124,7 @@ package body Ch6 is
    --  other subprogram constructs.
 
    --  PARAMETRIZED_EXPRESSION ::=
-   --    FUNCTION SPECIFICATION IS EXPRESSION;
+   --    FUNCTION SPECIFICATION IS (EXPRESSION);
 
    --  The value in Pf_Flags indicates which of these possible declarations
    --  is acceptable to the caller:
@@ -134,6 +134,7 @@ package body Ch6 is
    --    Pf_Flags.Pbod                 Set if proper body OK
    --    Pf_Flags.Rnam                 Set if renaming declaration OK
    --    Pf_Flags.Stub                 Set if body stub OK
+   --    Pf_Flags.Pexp                 Set if parametrized expression OK
 
    --  If an inappropriate form is encountered, it is scanned out but an
    --  error message indicating that it is appearing in an inappropriate
@@ -221,17 +222,17 @@ package body Ch6 is
          --  already been given, so no need to give another message here.
 
          --  An overriding indicator is allowed for subprogram declarations,
-         --  bodies (including subunits), renamings, stubs, and
-         --  instantiations. The test against Pf_Decl_Pbod is added to account
-         --  for the case of subprograms declared in a protected type, where
-         --  only subprogram declarations and bodies can occur. The Pf_Pbod
-         --  case is for subunits.
+         --  bodies (including subunits), renamings, stubs, and instantiations.
+         --  The test against Pf_Decl_Pbod is added to account for the case of
+         --  subprograms declared in a protected type, where only subprogram
+         --  declarations and bodies can occur. The Pf_Pbod case is for
+         --  subunits.
 
-         if Pf_Flags /= Pf_Decl_Gins_Pbod_Rnam_Stub
+         if Pf_Flags /= Pf_Decl_Gins_Pbod_Rnam_Stub_Pexp
               and then
-            Pf_Flags /= Pf_Decl_Pbod
+            Pf_Flags /= Pf_Decl_Pbod_Pexp
               and then
-            Pf_Flags /= Pf_Pbod
+            Pf_Flags /= Pf_Pbod_Pexp
          then
             Error_Msg_SC ("overriding indicator not allowed here!");
 
@@ -583,12 +584,9 @@ package body Ch6 is
          end if;
       end if;
 
-      --  Processing for subprogram body or parametrized expression
+      --  Processing for stub or subprogram body or parametrized expression
 
       <<Subprogram_Body>>
-         if not Pf_Flags.Pbod then
-            Error_Msg_SP ("subprogram body not allowed here!");
-         end if;
 
          --  Subprogram body stub case
 
@@ -614,28 +612,24 @@ package body Ch6 is
          --  Subprogram body or parametrized expression case
 
          else
-            --  Here we must distinguish a body and a parametrized expression
-
-            Parse_Body_Or_Parametrized_Expression : declare
-               function Is_Parametrized_Expression return Boolean;
-               --  Returns True if we have case of parametrized epression
+            Scan_Body_Or_Parametrized_Expression : declare
 
-               --------------------------------
-               -- Is_Parametrized_Expression --
-               --------------------------------
+               function Likely_Parametrized_Expression return Boolean;
+               --  Returns True if we have a probably case of a parametrized
+               --  expression omitting the parentheses, if so, returns True
+               --  and emits an appropriate error message, else returns False.
+
+               ------------------------------------
+               -- Likely_Parametrized_Expression --
+               ------------------------------------
 
-               function Is_Parametrized_Expression return Boolean is
+               function Likely_Parametrized_Expression return Boolean is
                begin
-                  --  Parametrized expression only allowed in Ada 2012
-
-                  if Ada_Version < Ada_12 then
-                     return False;
-
                   --  If currently pointing to BEGIN or a declaration keyword
                   --  or a pragma, then we definitely have a subprogram body.
                   --  This is a common case, so worth testing first.
 
-                  elsif Token = Tok_Begin
+                  if Token = Tok_Begin
                     or else Token in Token_Class_Declk
                     or else Token = Tok_Pragma
                   then
@@ -652,42 +646,79 @@ package body Ch6 is
                     or else Token = Tok_New
                     or else Token = Tok_Not
                   then
-                     return True;
+                     null;
 
-                  --  Anything other than an identifier must be a body at
-                  --  this stage. Probably we could do a little better job of
-                  --  distingushing some more error cases, but it seems right
-                  --  to err on the side of favoring a body over the
-                  --  new-fangled parametrized expression.
+                  --  Anything other than an identifier must be a body
 
                   elsif Token /= Tok_Identifier then
                      return False;
 
-                  --  For identifier we have to scan ahead if identifier is
-                  --  followed by a colon or a comma, it is a declaration and
-                  --  hence we have a subprogram body. Otherwise we have an
-                  --  expression.
+                  --  Here for an identifier
 
                   else
-                     declare
-                        Scan_State : Saved_Scan_State;
-                        Tok        : Token_Type;
-                     begin
-                        Save_Scan_State (Scan_State);
-                        Scan; -- past identifier
-                        Tok := Token;
-                        Restore_Scan_State (Scan_State);
-                        return Tok /= Tok_Colon and then Tok /= Tok_Comma;
-                     end;
+                     --  If the identifier is the first token on its line, then
+                     --  let's assume that we have a missing begin and this is
+                     --  intended as a subprogram body.
+
+                     if Token_Is_At_Start_Of_Line then
+                        return False;
+
+                     --  Otherwise we have to scan ahead. If the identifier is
+                     --  followed by a colon or a comma, it is a declaration
+                     --  and hence we have a subprogram body. Otherwise assume
+                     --  a parametrized expression.
+
+                     else
+                        declare
+                           Scan_State : Saved_Scan_State;
+                           Tok        : Token_Type;
+                        begin
+                           Save_Scan_State (Scan_State);
+                           Scan; -- past identifier
+                           Tok := Token;
+                           Restore_Scan_State (Scan_State);
+
+                           if Tok = Tok_Colon or else Tok = Tok_Comma then
+                              return False;
+                           end if;
+                        end;
+                     end if;
                   end if;
-               end Is_Parametrized_Expression;
 
-            --  Start of processing for Parse_Body_Or_Parametrized_Expression
+                  --  Fall through if we have a likely parametrized expression
+
+                  Error_Msg_SC
+                    ("parametrized expression must be "
+                     & "enclosed in parentheses");
+                  return True;
+               end Likely_Parametrized_Expression;
+
+            --  Start of processing for Scan_Body_Or_Parametrized_Expression
 
             begin
-               --  Parametrized_Expression case, parse expression
+               --  Parametrized_Expression case
+
+               if Token = Tok_Left_Paren
+                 or else Likely_Parametrized_Expression
+               then
+                  --  Check parametrized expression allowed here
+
+                  if not Pf_Flags.Pexp then
+                     Error_Msg_SC
+                       ("parametrized expression not allowed here!");
+                  end if;
+
+                  --  Check we are in Ada 2012 mode
+
+                  if Ada_Version < Ada_12 then
+                     Error_Msg_SC
+                       ("parametrized expression is an Ada 2012 feature!");
+                     Error_Msg_SC
+                       ("\unit must be compiled with -gnat2012 switch!");
+                  end if;
+
+                  --  Parse out expression and build parametrized expression
 
-               if Is_Parametrized_Expression then
                   Body_Node :=
                     New_Node
                       (N_Parametrized_Expression, Sloc (Specification_Node));
@@ -699,10 +730,16 @@ package body Ch6 is
                --  Subprogram body case
 
                else
-                  --  Here is the test for a suspicious IS (i.e. one that looks
-                  --  like it might more properly be a semicolon). See separate
-                  --  section discussing use of IS instead of semicolon in
-                  --  package Parse.
+                  --  Check body allowed here
+
+                  if not Pf_Flags.Pbod then
+                     Error_Msg_SP ("subprogram body not allowed here!");
+                  end if;
+
+                  --  Here is the test for a suspicious IS (i.e. one that
+                  --  looks like it might more properly be a semicolon).
+                  --  See separate section describing use of IS instead
+                  --  of semicolon in package Parse.
 
                   if (Token in Token_Class_Declk
                         or else
@@ -715,7 +752,7 @@ package body Ch6 is
                   end if;
 
                   --  Build and return subprogram body, parsing declarations
-                  --  an statement sequence that belong to the body.
+                  --  and statement sequence that belong to the body.
 
                   Body_Node :=
                     New_Node (N_Subprogram_Body, Sloc (Specification_Node));
@@ -724,7 +761,7 @@ package body Ch6 is
                end if;
 
                return Body_Node;
-            end Parse_Body_Or_Parametrized_Expression;
+            end Scan_Body_Or_Parametrized_Expression;
          end if;
 
       --  Processing for subprogram declaration
Index: par-ch10.adb
===================================================================
--- par-ch10.adb	(revision 165097)
+++ par-ch10.adb	(working copy)
@@ -347,10 +347,10 @@ package body Ch10 is
             Error_Msg_BC -- CODEFIX
               ("keyword BODY expected here [see file name]");
             Restore_Scan_State (Scan_State);
-            Set_Unit (Comp_Unit_Node, P_Package (Pf_Pbod));
+            Set_Unit (Comp_Unit_Node, P_Package (Pf_Pbod_Pexp));
          else
             Restore_Scan_State (Scan_State);
-            Set_Unit (Comp_Unit_Node, P_Package (Pf_Decl_Gins_Pbod_Rnam));
+            Set_Unit (Comp_Unit_Node, P_Package (Pf_Decl_Gins_Pbod_Rnam_Pexp));
          end if;
 
       elsif Token = Tok_Generic then
@@ -364,7 +364,7 @@ package body Ch10 is
         or else Token = Tok_Overriding
         or else Token = Tok_Procedure
       then
-         Set_Unit (Comp_Unit_Node, P_Subprogram (Pf_Decl_Gins_Pbod_Rnam));
+         Set_Unit (Comp_Unit_Node, P_Subprogram (Pf_Decl_Gins_Pbod_Rnam_Pexp));
 
          --  A little bit of an error recovery check here. If we just scanned
          --  a subprogram declaration (as indicated by an SIS entry being
@@ -1034,10 +1034,10 @@ package body Ch10 is
         or else Token = Tok_Overriding
         or else Token = Tok_Procedure
       then
-         Body_Node := P_Subprogram (Pf_Pbod);
+         Body_Node := P_Subprogram (Pf_Pbod_Pexp);
 
       elsif Token = Tok_Package then
-         Body_Node := P_Package (Pf_Pbod);
+         Body_Node := P_Package (Pf_Pbod_Pexp);
 
       elsif Token = Tok_Protected then
          Scan; -- past PROTECTED
Index: par-ch3.adb
===================================================================
--- par-ch3.adb	(revision 165097)
+++ par-ch3.adb	(working copy)
@@ -4142,7 +4142,7 @@ package body Ch3 is
 
          when Tok_Function =>
             Check_Bad_Layout;
-            Append (P_Subprogram (Pf_Decl_Gins_Pbod_Rnam_Stub), Decls);
+            Append (P_Subprogram (Pf_Decl_Gins_Pbod_Rnam_Stub_Pexp), Decls);
             Done := False;
 
          when Tok_For =>
@@ -4186,7 +4186,7 @@ package body Ch3 is
                Error_Msg_SC ("\unit must be compiled with -gnat05 switch");
 
                Token := Tok_Overriding;
-               Append (P_Subprogram (Pf_Decl_Gins_Pbod_Rnam_Stub), Decls);
+               Append (P_Subprogram (Pf_Decl_Gins_Pbod_Rnam_Stub_Pexp), Decls);
                Done := False;
 
             --  Normal case, no overriding, or overriding followed by colon
@@ -4201,17 +4201,17 @@ package body Ch3 is
 
          when Tok_Not =>
             Check_Bad_Layout;
-            Append (P_Subprogram (Pf_Decl_Gins_Pbod_Rnam_Stub), Decls);
+            Append (P_Subprogram (Pf_Decl_Gins_Pbod_Rnam_Stub_Pexp), Decls);
             Done := False;
 
          when Tok_Overriding =>
             Check_Bad_Layout;
-            Append (P_Subprogram (Pf_Decl_Gins_Pbod_Rnam_Stub), Decls);
+            Append (P_Subprogram (Pf_Decl_Gins_Pbod_Rnam_Stub_Pexp), Decls);
             Done := False;
 
          when Tok_Package =>
             Check_Bad_Layout;
-            Append (P_Package (Pf_Decl_Gins_Pbod_Rnam_Stub), Decls);
+            Append (P_Package (Pf_Decl_Gins_Pbod_Rnam_Stub_Pexp), Decls);
             Done := False;
 
          when Tok_Pragma =>
@@ -4220,7 +4220,7 @@ package body Ch3 is
 
          when Tok_Procedure =>
             Check_Bad_Layout;
-            Append (P_Subprogram (Pf_Decl_Gins_Pbod_Rnam_Stub), Decls);
+            Append (P_Subprogram (Pf_Decl_Gins_Pbod_Rnam_Stub_Pexp), Decls);
             Done := False;
 
          when Tok_Protected =>
Index: par-ch7.adb
===================================================================
--- par-ch7.adb	(revision 165080)
+++ par-ch7.adb	(working copy)
@@ -109,7 +109,7 @@ package body Ch7 is
       --  Case of package body. Note that we demand a package body if that
       --  is the only possibility (even if the BODY keyword is not present)
 
-      if Token = Tok_Body or else Pf_Flags = Pf_Pbod then
+      if Token = Tok_Body or else Pf_Flags = Pf_Pbod_Pexp then
          if not Pf_Flags.Pbod then
             Error_Msg_SC ("package body cannot appear here!");
          end if;


More information about the Gcc-patches mailing list