From c64ac479d37f32198f75eee496d32c175fc4260b Mon Sep 17 00:00:00 2001 From: Piotr Trojanek Date: Thu, 5 Mar 2020 11:46:59 +0100 Subject: [PATCH] [Ada] Fold Enum_Rep attribute in evaluation and not in expansion 2020-06-10 Piotr Trojanek gcc/ada/ * exp_attr.adb (Expand_N_Attribute_Reference): Remove folding for Enum_Rep attribute. * exp_spark.adb (Expand_SPARK_N_Attribute_Reference): Remove duplicated code for folding Enum_Rep attribute. * sem_attr.adb (Eval_Attribute): Relax condition for folding Enum_Rep attribute; previously dead code is now executed when the attribute prefix is an enumeration literal; refine type in processing of Enum_Val. --- gcc/ada/exp_attr.adb | 15 +++------------ gcc/ada/exp_spark.adb | 23 ----------------------- gcc/ada/sem_attr.adb | 8 ++++++-- 3 files changed, 9 insertions(+), 37 deletions(-) diff --git a/gcc/ada/exp_attr.adb b/gcc/ada/exp_attr.adb index 67a18c66ed1..356d3dbd069 100644 --- a/gcc/ada/exp_attr.adb +++ b/gcc/ada/exp_attr.adb @@ -3159,17 +3159,8 @@ package body Exp_Attr is Expr := Pref; end if; - -- If the expression is an enumeration literal, it is replaced by the - -- literal value. - - if Nkind (Expr) in N_Has_Entity - and then Ekind (Entity (Expr)) = E_Enumeration_Literal - then - Rewrite (N, - Make_Integer_Literal (Loc, Enumeration_Rep (Entity (Expr)))); - - -- If not constant-folded above, Enum_Type'Enum_Rep (X) or - -- X'Enum_Rep expands to + -- If not constant-folded, Enum_Type'Enum_Rep (X) or X'Enum_Rep + -- expands to -- target-type (X) @@ -3185,7 +3176,7 @@ package body Exp_Attr is -- first convert to a small signed integer type in order not to lose -- the size information. - elsif Is_Enumeration_Type (Ptyp) then + if Is_Enumeration_Type (Ptyp) then Psiz := RM_Size (Base_Type (Ptyp)); if Psiz < 8 then diff --git a/gcc/ada/exp_spark.adb b/gcc/ada/exp_spark.adb index c115a453228..cab48f49b5c 100644 --- a/gcc/ada/exp_spark.adb +++ b/gcc/ada/exp_spark.adb @@ -199,29 +199,6 @@ package body Exp_SPARK is Parameter_Associations => New_List (Expr))); Analyze_And_Resolve (N, Typ); - -- Whenever possible, replace a prefix which is an enumeration literal - -- by the corresponding literal value, just like it happens in the GNAT - -- expander. - - elsif Attr_Id = Attribute_Enum_Rep then - declare - Exprs : constant List_Id := Expressions (N); - begin - if Is_Non_Empty_List (Exprs) then - Expr := First (Exprs); - else - Expr := Prefix (N); - end if; - - -- If the argument is a literal, expand it - - if Nkind (Expr) in N_Has_Entity - and then Ekind (Entity (Expr)) = E_Enumeration_Literal - then - Exp_Attr.Expand_N_Attribute_Reference (N); - end if; - end; - elsif Attr_Id = Attribute_Object_Size or else Attr_Id = Attribute_Size or else Attr_Id = Attribute_Value_Size diff --git a/gcc/ada/sem_attr.adb b/gcc/ada/sem_attr.adb index e82082bc07a..d2ac4c77298 100644 --- a/gcc/ada/sem_attr.adb +++ b/gcc/ada/sem_attr.adb @@ -7719,7 +7719,11 @@ package body Sem_Attr is -- purpose, a string literal counts as an object (attributes of string -- literals can only appear in generated code). - if Is_Object_Reference (P) or else Nkind (P) = N_String_Literal then + if Is_Object_Reference (P) + or else Nkind (P) = N_String_Literal + or else (Is_Entity_Name (P) + and then Ekind (Entity (P)) = E_Enumeration_Literal) + then -- For Component_Size, the prefix is an array object, and we apply -- the attribute to the type of the object. This is allowed for both @@ -8533,7 +8537,7 @@ package body Sem_Attr is -------------- when Attribute_Enum_Val => Enum_Val : declare - Lit : Node_Id; + Lit : Entity_Id; begin -- We have something like Enum_Type'Enum_Val (23), so search for a -- 2.30.2