Patchwork [Ada] Special error message for 'Range used as integer type definition

login
register
mail settings
Submitter Arnaud Charlet
Date Feb. 22, 2012, 2:13 p.m.
Message ID <20120222141306.GA5641@adacore.com>
Download mbox | patch
Permalink /patch/142466/
State New
Headers show

Comments

Arnaud Charlet - Feb. 22, 2012, 2:13 p.m.
Surprisingly, a Range attribute reference many not be used as a signed
integer type reference. This patch gives a clearer error message in
this case as shown by this example program:

     1. package Typ_Is_Range is
     2.    S : constant String := "abc";
     3.    type T is range S'Range;
                            |
        >>> Range attribute not allowed here, use First .. Last

     4. end Typ_Is_Range;

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

2012-02-22  Robert Dewar  <dewar@adacore.com>

	* par-ch3.adb (P_Signed_Integer_Type_Definition): Specialize
	error message for 'Range.

Patch

Index: par-ch3.adb
===================================================================
--- par-ch3.adb	(revision 184470)
+++ par-ch3.adb	(working copy)
@@ -6,7 +6,7 @@ 
 --                                                                          --
 --                                 B o d y                                  --
 --                                                                          --
---          Copyright (C) 1992-2011, Free Software Foundation, Inc.         --
+--          Copyright (C) 1992-2012, 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- --
@@ -2279,13 +2279,30 @@ 
          Scan; -- past RANGE
       end if;
 
-      Expr_Node := P_Expression;
-      Check_Simple_Expression (Expr_Node);
-      Set_Low_Bound (Typedef_Node, Expr_Node);
-      T_Dot_Dot;
-      Expr_Node := P_Expression;
-      Check_Simple_Expression (Expr_Node);
-      Set_High_Bound (Typedef_Node, Expr_Node);
+      Expr_Node := P_Expression_Or_Range_Attribute;
+
+      --  Range case (not permitted by the grammar, this is surprising but
+      --  the grammar in the RM is as quoted above, and does not allow Range).
+
+      if Expr_Form = EF_Range_Attr then
+         Error_Msg_N
+           ("Range attribute not allowed here, use First .. Last", Expr_Node);
+         Set_Low_Bound (Typedef_Node, Expr_Node);
+         Set_Attribute_Name (Expr_Node, Name_First);
+         Set_High_Bound (Typedef_Node, Copy_Separate_Tree (Expr_Node));
+         Set_Attribute_Name (High_Bound (Typedef_Node), Name_Last);
+
+      --  Normal case of explicit range
+
+      else
+         Check_Simple_Expression (Expr_Node);
+         Set_Low_Bound (Typedef_Node, Expr_Node);
+         T_Dot_Dot;
+         Expr_Node := P_Expression;
+         Check_Simple_Expression (Expr_Node);
+         Set_High_Bound (Typedef_Node, Expr_Node);
+      end if;
+
       return Typedef_Node;
    end P_Signed_Integer_Type_Definition;