Commit 1e3c434f by Bob Duff Committed by Pierre-Marie de Rodat

[Ada] System'To_Address not always static

System'To_Address is supposed to be static when its parameter is static.
This patch fixes a bug in which it is considered nonstatic when used as
the initial value of a variable with the Thread_Local_Storage aspect, so
the compiler incorrectly gives an error when initializing such a
variable with System'To_Address (0).

2018-11-14  Bob Duff  <duff@adacore.com>

gcc/ada/

	* sem_attr.adb (To_Address): Simplify setting of
	Is_Static_Expression. Remove second (unconditional) call to
	Set_Is_Static_Expression -- surely it's not static if the
	operand is not.  Initialize Static on declaration.  Do not try
	to fold 'To_Address, even though it's static.
	* exp_attr.adb (To_Address): Preserve Is_Static_Expression.
	* sinfo.ads, sem_eval.ads, sem_eval.adb (Is_Static_Expression,
	Is_OK_Static_Expression, Raises_Constraint_Error): Simplify
	documentation.  There was too much repetition and redundancy.

From-SVN: r266124
parent 43018f58
2018-11-14 Bob Duff <duff@adacore.com>
* sem_attr.adb (To_Address): Simplify setting of
Is_Static_Expression. Remove second (unconditional) call to
Set_Is_Static_Expression -- surely it's not static if the
operand is not. Initialize Static on declaration. Do not try
to fold 'To_Address, even though it's static.
* exp_attr.adb (To_Address): Preserve Is_Static_Expression.
* sinfo.ads, sem_eval.ads, sem_eval.adb (Is_Static_Expression,
Is_OK_Static_Expression, Raises_Constraint_Error): Simplify
documentation. There was too much repetition and redundancy.
2018-11-14 Ed Schonberg <schonberg@adacore.com>
* sem_ch3.adb (Analyze_Object_Declaration): Use the
......
......@@ -6605,15 +6605,20 @@ package body Exp_Attr is
----------------
-- Transforms System'To_Address (X) and System.Address'Ref (X) into
-- unchecked conversion from (integral) type of X to type address.
-- unchecked conversion from (integral) type of X to type address. If
-- the To_Address is a static expression, the transformed expression
-- also needs to be static, because we do some legality checks (e.g.
-- for Thread_Local_Storage) after this transformation.
when Attribute_Ref
| Attribute_To_Address
=>
when Attribute_Ref | Attribute_To_Address => To_Address : declare
Is_Static : constant Boolean := Is_Static_Expression (N);
begin
Rewrite (N,
Unchecked_Convert_To (RTE (RE_Address),
Relocate_Node (First (Exprs))));
Set_Is_Static_Expression (N, Is_Static);
Analyze_And_Resolve (N, RTE (RE_Address));
end To_Address;
------------
-- To_Any --
......
......@@ -6144,7 +6144,6 @@ package body Sem_Attr is
when Attribute_To_Address => To_Address : declare
Val : Uint;
begin
Check_E1;
Analyze (P);
......@@ -6153,10 +6152,7 @@ package body Sem_Attr is
Generate_Reference (RTE (RE_Address), P);
Analyze_And_Resolve (E1, Any_Integer);
Set_Etype (N, RTE (RE_Address));
if Is_Static_Expression (E1) then
Set_Is_Static_Expression (N, True);
end if;
Set_Is_Static_Expression (N, Is_Static_Expression (E1));
-- OK static expression case, check range and set appropriate type
......@@ -6188,8 +6184,6 @@ package body Sem_Attr is
Set_Etype (E1, Standard_Unsigned_64);
end if;
end if;
Set_Is_Static_Expression (N, True);
end To_Address;
------------
......@@ -7202,7 +7196,7 @@ package body Sem_Attr is
P_Root_Type : Entity_Id;
-- The root type of the prefix type
Static : Boolean;
Static : Boolean := False;
-- True if the result is Static. This is set by the general processing
-- to true if the prefix is static, and all expressions are static. It
-- can be reset as processing continues for particular attributes. This
......@@ -7563,10 +7557,16 @@ package body Sem_Attr is
-- Start of processing for Eval_Attribute
begin
-- The To_Address attribute can be static, but it cannot be evaluated at
-- compile time, so just return.
if Id = Attribute_To_Address then
return;
end if;
-- Initialize result as non-static, will be reset if appropriate
Set_Is_Static_Expression (N, False);
Static := False;
-- Acquire first two expressions (at the moment, no attributes take more
-- than two expressions in any case).
......@@ -8283,8 +8283,8 @@ package body Sem_Attr is
-- static attribute in GNAT.
Analyze_And_Resolve (N, Standard_Boolean);
Static := True;
Set_Is_Static_Expression (N, True);
Static := True;
Set_Is_Static_Expression (N, True);
end Atomic_Always_Lock_Free;
---------
......@@ -8346,7 +8346,6 @@ package body Sem_Attr is
-- attribute reference, and this reference is not static.
Set_Is_Static_Expression (N, False);
null;
---------------
-- Copy_Sign --
......@@ -8737,8 +8736,8 @@ package body Sem_Attr is
-- static attribute in GNAT.
Analyze_And_Resolve (N, Standard_Boolean);
Static := True;
Set_Is_Static_Expression (N, True);
Static := True;
Set_Is_Static_Expression (N, True);
end Lock_Free;
----------
......
......@@ -51,13 +51,7 @@ package Sem_Eval is
-- Is_Static_Expression
-- This flag is set on any expression that is static according to the
-- rules in (RM 4.9(3-32)). This flag should be tested during testing
-- of legality of parts of a larger static expression. For all other
-- contexts that require static expressions, use the separate predicate
-- Is_OK_Static_Expression, since an expression that meets the RM 4.9
-- requirements, but raises a constraint error when evaluated in a non-
-- static context does not meet the legality requirements.
-- True for static expressions, as defined in RM-4.9.
-- Raises_Constraint_Error
......@@ -68,31 +62,28 @@ package Sem_Eval is
-- (i.e. the flag is accurate for static expressions, and conservative
-- for non-static expressions.
-- If a static expression does not raise constraint error, then it will
-- have the flag Raises_Constraint_Error flag False, and the expression
-- must be computed at compile time, which means that it has the form of
-- either a literal, or a constant that is itself (recursively) either a
-- literal or a constant.
-- See also Is_OK_Static_Expression, which is True for static
-- expressions that do not raise Constraint_Error. This is used in most
-- legality checks, because static expressions that raise Constraint_Error
-- are usually illegal.
-- The above rules must be followed exactly in order for legality checks to
-- be accurate. For subexpressions that are not static according to the RM
-- definition, they are sometimes folded anyway, but of course in this case
-- Is_Static_Expression is not set.
-- See also Compile_Time_Known_Value, which is True for an expression whose
-- value is known at compile time. In this case, the expression is folded
-- to a literal or to a constant that is itself (recursively) either a
-- literal or a constant
-- Is_[OK_]Static_Expression are used for legality checks, whereas
-- Compile_Time_Known_Value is used for optimization purposes.
-- When we are analyzing and evaluating static expressions, we propagate
-- both flags accurately. Usually if a subexpression raises a constraint
-- error, then so will its parent expression, and Raise_Constraint_Error
-- will be propagated to this parent. The exception is conditional cases
-- like (True or else 1/0 = 0) which results in an expresion that has the
-- both flags. Usually if a subexpression raises a Constraint_Error, then
-- so will its parent expression, and Raise_Constraint_Error will be
-- propagated to this parent. The exception is conditional cases like
-- (True or else 1/0 = 0), which results in an expression that has the
-- Is_Static_Expression flag True, and Raises_Constraint_Error False. Even
-- though 1/0 would raise an exception, the right operand is never actually
-- executed, so the expression as a whole does not raise CE.
-- For constructs in the language where static expressions are part of the
-- required semantics, we need an expression that meets the 4.9 rules and
-- does not raise CE. So nearly everywhere, callers should call function
-- Is_OK_Static_Expression rather than Is_Static_Expression.
-- Finally, the case of static predicates. These are applied only to entire
-- expressions, not to subexpressions, so we do not have the case of having
-- to propagate this information. We handle this case simply by resetting
......
......@@ -1966,12 +1966,7 @@ package Sinfo is
-- Is_Static_Expression (Flag6-Sem)
-- Indicates that an expression is a static expression according to the
-- rules in (RM 4.9). Note that it is possible for this flag to be set
-- when Raises_Constraint_Error is also set. In practice almost all cases
-- where a static expression is required do not allow an expression which
-- raises Constraint_Error, so almost always, callers should call the
-- Is_Ok_Static_Expression routine instead of testing this flag. See
-- spec of package Sem_Eval for full details on the use of this flag.
-- rules in RM-4.9. See Sem_Eval for details.
-- Is_Subprogram_Descriptor (Flag16-Sem)
-- Present in N_Object_Declaration, and set only for the object
......@@ -2297,15 +2292,7 @@ package Sinfo is
-- Raises_Constraint_Error (Flag7-Sem)
-- Set on an expression whose evaluation will definitely fail constraint
-- error check. In the case of static expressions, this flag must be set
-- accurately (and if it is set, the expression is typically illegal
-- unless it appears as a non-elaborated branch of a short-circuit form).
-- For a non-static expression, this flag may be set whenever an
-- expression (e.g. an aggregate) is known to raise constraint error. If
-- set, the expression definitely will raise CE if elaborated at runtime.
-- If not set, the expression may or may not raise CE. In other words, on
-- static expressions, the flag is set accurately, on non-static
-- expressions it is set conservatively.
-- error check. See Sem_Eval for details.
-- Redundant_Use (Flag13-Sem)
-- Present in nodes that can appear as an operand in a use clause or use
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment