diff mbox series

[Ada] Do not use Long_Long_Long_Integer'Image with pragma Discard_Names

Message ID 20201023082640.GA127656@adacore.com
State New
Headers show
Series [Ada] Do not use Long_Long_Long_Integer'Image with pragma Discard_Names | expand

Commit Message

Pierre-Marie de Rodat Oct. 23, 2020, 8:26 a.m. UTC
When the Image attribute is applied to an enumeration type subject to
pragma Discard_Names, the underlying numeric value is printed instead
of the string of the literal and this is done by invoking the Image
routine of Long_Long_Long_Integer, which is overkill in the context.

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

gcc/ada/

	* exp_imgv.adb (Expand_Image_Attribute): For an enumeration type
	subject to pragma Discard_Names, convert 'Pos to Long_Long_Integer
	before applying 'Img to the result.
diff mbox series

Patch

diff --git a/gcc/ada/exp_imgv.adb b/gcc/ada/exp_imgv.adb
--- a/gcc/ada/exp_imgv.adb
+++ b/gcc/ada/exp_imgv.adb
@@ -619,15 +619,18 @@  package body Exp_Imgv is
            or else No (Lit_Strings (Rtyp))
          then
             --  When pragma Discard_Names applies to the first subtype, build
-            --  (Pref'Pos (Expr))'Img.
+            --  (Long_Long_Integer (Pref'Pos (Expr)))'Img. The conversion is
+            --  there to avoid applying 'Img directly in Universal_Integer,
+            --  which can be a very large type. See also the handling of 'Val.
 
             Rewrite (N,
               Make_Attribute_Reference (Loc,
                 Prefix =>
-                   Make_Attribute_Reference (Loc,
-                     Prefix         => Pref,
-                     Attribute_Name => Name_Pos,
-                     Expressions    => New_List (Expr)),
+                  Convert_To (Standard_Long_Long_Integer,
+                    Make_Attribute_Reference (Loc,
+                    Prefix         => Pref,
+                    Attribute_Name => Name_Pos,
+                    Expressions    => New_List (Expr))),
                 Attribute_Name =>
                   Name_Img));
             Analyze_And_Resolve (N, Standard_String);