comparison gcc/ada/sem_eval.adb @ 145:1830386684a0

gcc-9.2.0
author anatofuz
date Thu, 13 Feb 2020 11:34:05 +0900
parents 84e7813d76e9
children
comparison
equal deleted inserted replaced
131:84e7813d76e9 145:1830386684a0
4 -- -- 4 -- --
5 -- S E M _ E V A L -- 5 -- S E M _ E V A L --
6 -- -- 6 -- --
7 -- B o d y -- 7 -- B o d y --
8 -- -- 8 -- --
9 -- Copyright (C) 1992-2018, Free Software Foundation, Inc. -- 9 -- Copyright (C) 1992-2019, Free Software Foundation, Inc. --
10 -- -- 10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under -- 11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- -- 12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- -- 13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- 14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
64 -- The compile time evaluation of expressions is distributed over several 64 -- The compile time evaluation of expressions is distributed over several
65 -- Eval_xxx procedures. These procedures are called immediately after 65 -- Eval_xxx procedures. These procedures are called immediately after
66 -- a subexpression is resolved and is therefore accomplished in a bottom 66 -- a subexpression is resolved and is therefore accomplished in a bottom
67 -- up fashion. The flags are synthesized using the following approach. 67 -- up fashion. The flags are synthesized using the following approach.
68 68
69 -- Is_Static_Expression is determined by following the detailed rules 69 -- Is_Static_Expression is determined by following the rules in
70 -- in RM 4.9(4-14). This involves testing the Is_Static_Expression 70 -- RM-4.9. This involves testing the Is_Static_Expression flag of
71 -- flag of the operands in many cases. 71 -- the operands in many cases.
72 72
73 -- Raises_Constraint_Error is set if any of the operands have the flag 73 -- Raises_Constraint_Error is usually set if any of the operands have
74 -- set or if an attempt to compute the value of the current expression 74 -- the flag set or if an attempt to compute the value of the current
75 -- results in detection of a runtime constraint error. 75 -- expression results in Constraint_Error.
76
77 -- As described in the spec, the requirement is that Is_Static_Expression
78 -- be accurately set, and in addition for nodes for which this flag is set,
79 -- Raises_Constraint_Error must also be set. Furthermore a node which has
80 -- Is_Static_Expression set, and Raises_Constraint_Error clear, then the
81 -- requirement is that the expression value must be precomputed, and the
82 -- node is either a literal, or the name of a constant entity whose value
83 -- is a static expression.
84 76
85 -- The general approach is as follows. First compute Is_Static_Expression. 77 -- The general approach is as follows. First compute Is_Static_Expression.
86 -- If the node is not static, then the flag is left off in the node and 78 -- If the node is not static, then the flag is left off in the node and
87 -- we are all done. Otherwise for a static node, we test if any of the 79 -- we are all done. Otherwise for a static node, we test if any of the
88 -- operands will raise constraint error, and if so, propagate the flag 80 -- operands will raise Constraint_Error, and if so, propagate the flag
89 -- Raises_Constraint_Error to the result node and we are done (since the 81 -- Raises_Constraint_Error to the result node and we are done (since the
90 -- error was already posted at a lower level). 82 -- error was already posted at a lower level).
91 83
92 -- For the case of a static node whose operands do not raise constraint 84 -- For the case of a static node whose operands do not raise constraint
93 -- error, we attempt to evaluate the node. If this evaluation succeeds, 85 -- error, we attempt to evaluate the node. If this evaluation succeeds,
94 -- then the node is replaced by the result of this computation. If the 86 -- then the node is replaced by the result of this computation. If the
95 -- evaluation raises constraint error, then we rewrite the node with 87 -- evaluation raises Constraint_Error, then we rewrite the node with
96 -- Apply_Compile_Time_Constraint_Error to raise the exception and also 88 -- Apply_Compile_Time_Constraint_Error to raise the exception and also
97 -- to post appropriate error messages. 89 -- to post appropriate error messages.
98 90
99 ---------------- 91 ----------------
100 -- Local Data -- 92 -- Local Data --
106 -- The following declarations are used to maintain a cache of nodes that 98 -- The following declarations are used to maintain a cache of nodes that
107 -- have compile-time-known values. The cache is maintained only for 99 -- have compile-time-known values. The cache is maintained only for
108 -- discrete types (the most common case), and is populated by calls to 100 -- discrete types (the most common case), and is populated by calls to
109 -- Compile_Time_Known_Value and Expr_Value, but only used by Expr_Value 101 -- Compile_Time_Known_Value and Expr_Value, but only used by Expr_Value
110 -- since it is possible for the status to change (in particular it is 102 -- since it is possible for the status to change (in particular it is
111 -- possible for a node to get replaced by a constraint error node). 103 -- possible for a node to get replaced by a Constraint_Error node).
112 104
113 CV_Bits : constant := 5; 105 CV_Bits : constant := 5;
114 -- Number of low order bits of Node_Id value used to reference entries 106 -- Number of low order bits of Node_Id value used to reference entries
115 -- in the cache table. 107 -- in the cache table.
116 108
293 -- the following extra actions: 285 -- the following extra actions:
294 -- 286 --
295 -- If either operand is Any_Type then propagate it to result to prevent 287 -- If either operand is Any_Type then propagate it to result to prevent
296 -- cascaded errors. 288 -- cascaded errors.
297 -- 289 --
298 -- If some operand raises constraint error, then replace the node N 290 -- If some operand raises Constraint_Error, then replace the node N
299 -- with the raise constraint error node. This replacement inherits the 291 -- with the raise Constraint_Error node. This replacement inherits the
300 -- Is_Static_Expression flag from the operands. 292 -- Is_Static_Expression flag from the operands.
301 293
302 procedure Test_Expression_Is_Foldable 294 procedure Test_Expression_Is_Foldable
303 (N : Node_Id; 295 (N : Node_Id;
304 Op1 : Node_Id; 296 Op1 : Node_Id;
568 -- Check out of range of base type 560 -- Check out of range of base type
569 561
570 elsif Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) then 562 elsif Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) then
571 Out_Of_Range (N); 563 Out_Of_Range (N);
572 564
573 -- Give warning if outside subtype (where one or both of the bounds of 565 -- Give a warning or error on the value outside the subtype. A warning
574 -- the subtype is static). This warning is omitted if the expression 566 -- is omitted if the expression appears in a range that could be null
575 -- appears in a range that could be null (warnings are handled elsewhere 567 -- (warnings are handled elsewhere for this case).
576 -- for this case).
577 568
578 elsif T /= Base_Type (T) and then Nkind (Parent (N)) /= N_Range then 569 elsif T /= Base_Type (T) and then Nkind (Parent (N)) /= N_Range then
579 if Is_In_Range (N, T, Assume_Valid => True) then 570 if Is_In_Range (N, T, Assume_Valid => True) then
580 null; 571 null;
581 572
582 elsif Is_Out_Of_Range (N, T, Assume_Valid => True) then 573 elsif Is_Out_Of_Range (N, T, Assume_Valid => True) then
583
584 -- Ignore out of range values for System.Priority in CodePeer 574 -- Ignore out of range values for System.Priority in CodePeer
585 -- mode since the actual target compiler may provide a wider 575 -- mode since the actual target compiler may provide a wider
586 -- range. 576 -- range.
587 577
588 if CodePeer_Mode and then T = RTE (RE_Priority) then 578 if CodePeer_Mode and then T = RTE (RE_Priority) then
589 Set_Do_Range_Check (N, False); 579 Set_Do_Range_Check (N, False);
580
581 -- Determine if the out-of-range violation constitutes a warning
582 -- or an error based on context, according to RM 4.9 (34/3).
583
584 elsif Nkind_In (Original_Node (N), N_Type_Conversion,
585 N_Qualified_Expression)
586 and then Comes_From_Source (Original_Node (N))
587 then
588 Apply_Compile_Time_Constraint_Error
589 (N, "value not in range of}", CE_Range_Check_Failed);
590 else 590 else
591 Apply_Compile_Time_Constraint_Error 591 Apply_Compile_Time_Constraint_Error
592 (N, "value not in range of}<<", CE_Range_Check_Failed); 592 (N, "value not in range of}<<", CE_Range_Check_Failed);
593 end if; 593 end if;
594 594
992 992
993 function Is_Same_Value (L, R : Node_Id) return Boolean is 993 function Is_Same_Value (L, R : Node_Id) return Boolean is
994 Lf : constant Node_Id := Compare_Fixup (L); 994 Lf : constant Node_Id := Compare_Fixup (L);
995 Rf : constant Node_Id := Compare_Fixup (R); 995 Rf : constant Node_Id := Compare_Fixup (R);
996 996
997 function Is_Rewritten_Loop_Entry (N : Node_Id) return Boolean;
998 -- An attribute reference to Loop_Entry may have been rewritten into
999 -- its prefix as a way to avoid generating a constant for that
1000 -- attribute when the corresponding pragma is ignored. These nodes
1001 -- should be ignored when deciding if they can be equal to one
1002 -- another.
1003
997 function Is_Same_Subscript (L, R : List_Id) return Boolean; 1004 function Is_Same_Subscript (L, R : List_Id) return Boolean;
998 -- L, R are the Expressions values from two attribute nodes for First 1005 -- L, R are the Expressions values from two attribute nodes for First
999 -- or Last attributes. Either may be set to No_List if no expressions 1006 -- or Last attributes. Either may be set to No_List if no expressions
1000 -- are present (indicating subscript 1). The result is True if both 1007 -- are present (indicating subscript 1). The result is True if both
1001 -- expressions represent the same subscript (note one case is where 1008 -- expressions represent the same subscript (note one case is where
1002 -- one subscript is missing and the other is explicitly set to 1). 1009 -- one subscript is missing and the other is explicitly set to 1).
1010
1011 -----------------------------
1012 -- Is_Rewritten_Loop_Entry --
1013 -----------------------------
1014
1015 function Is_Rewritten_Loop_Entry (N : Node_Id) return Boolean is
1016 Orig_N : constant Node_Id := Original_Node (N);
1017 begin
1018 return Orig_N /= N
1019 and then Nkind (Orig_N) = N_Attribute_Reference
1020 and then Get_Attribute_Id (Attribute_Name (Orig_N)) =
1021 Attribute_Loop_Entry;
1022 end Is_Rewritten_Loop_Entry;
1003 1023
1004 ----------------------- 1024 -----------------------
1005 -- Is_Same_Subscript -- 1025 -- Is_Same_Subscript --
1006 ----------------------- 1026 -----------------------
1007 1027
1024 end Is_Same_Subscript; 1044 end Is_Same_Subscript;
1025 1045
1026 -- Start of processing for Is_Same_Value 1046 -- Start of processing for Is_Same_Value
1027 1047
1028 begin 1048 begin
1049 -- Loop_Entry nodes rewritten into their prefix inside ignored
1050 -- pragmas should never lead to a decision of equality.
1051
1052 if Is_Rewritten_Loop_Entry (Lf)
1053 or else Is_Rewritten_Loop_Entry (Rf)
1054 then
1055 return False;
1056
1029 -- Values are the same if they refer to the same entity and the 1057 -- Values are the same if they refer to the same entity and the
1030 -- entity is non-volatile. This does not however apply to Float 1058 -- entity is nonvolatile.
1031 -- types, since we may have two NaN values and they should never 1059
1032 -- compare equal. 1060 elsif Nkind_In (Lf, N_Identifier, N_Expanded_Name)
1033
1034 -- If the entity is a discriminant, the two expressions may be bounds
1035 -- of components of objects of the same discriminated type. The
1036 -- values of the discriminants are not static, and therefore the
1037 -- result is unknown.
1038
1039 -- It would be better to comment individual branches of this test ???
1040
1041 if Nkind_In (Lf, N_Identifier, N_Expanded_Name)
1042 and then Nkind_In (Rf, N_Identifier, N_Expanded_Name) 1061 and then Nkind_In (Rf, N_Identifier, N_Expanded_Name)
1043 and then Entity (Lf) = Entity (Rf) 1062 and then Entity (Lf) = Entity (Rf)
1063
1064 -- If the entity is a discriminant, the two expressions may be
1065 -- bounds of components of objects of the same discriminated type.
1066 -- The values of the discriminants are not static, and therefore
1067 -- the result is unknown.
1068
1044 and then Ekind (Entity (Lf)) /= E_Discriminant 1069 and then Ekind (Entity (Lf)) /= E_Discriminant
1045 and then Present (Entity (Lf)) 1070 and then Present (Entity (Lf))
1071
1072 -- This does not however apply to Float types, since we may have
1073 -- two NaN values and they should never compare equal.
1074
1046 and then not Is_Floating_Point_Type (Etype (L)) 1075 and then not Is_Floating_Point_Type (Etype (L))
1047 and then not Is_Volatile_Reference (L) 1076 and then not Is_Volatile_Reference (L)
1048 and then not Is_Volatile_Reference (R) 1077 and then not Is_Volatile_Reference (R)
1049 then 1078 then
1050 return True; 1079 return True;
1127 Is_OK_Static_Expression (R))) 1156 Is_OK_Static_Expression (R)))
1128 then 1157 then
1129 return Unknown; 1158 return Unknown;
1130 end if; 1159 end if;
1131 1160
1132 -- If either operand could raise constraint error, then we cannot 1161 -- If either operand could raise Constraint_Error, then we cannot
1133 -- know the result at compile time (since CE may be raised). 1162 -- know the result at compile time (since CE may be raised).
1134 1163
1135 if not (Cannot_Raise_Constraint_Error (L) 1164 if not (Cannot_Raise_Constraint_Error (L)
1136 and then 1165 and then
1137 Cannot_Raise_Constraint_Error (R)) 1166 Cannot_Raise_Constraint_Error (R))
1694 function Compile_Time_Known_Value (Op : Node_Id) return Boolean is 1723 function Compile_Time_Known_Value (Op : Node_Id) return Boolean is
1695 K : constant Node_Kind := Nkind (Op); 1724 K : constant Node_Kind := Nkind (Op);
1696 CV_Ent : CV_Entry renames CV_Cache (Nat (Op) mod CV_Cache_Size); 1725 CV_Ent : CV_Entry renames CV_Cache (Nat (Op) mod CV_Cache_Size);
1697 1726
1698 begin 1727 begin
1699 -- Never known at compile time if bad type or raises constraint error 1728 -- Never known at compile time if bad type or raises Constraint_Error
1700 -- or empty (latter case occurs only as a result of a previous error). 1729 -- or empty (latter case occurs only as a result of a previous error).
1701 1730
1702 if No (Op) then 1731 if No (Op) then
1703 Check_Error_Detected; 1732 Check_Error_Detected;
1704 return False; 1733 return False;
2199 Check_Non_Static_Context (Expression (N)); 2228 Check_Non_Static_Context (Expression (N));
2200 return; 2229 return;
2201 end if; 2230 end if;
2202 2231
2203 -- First loop, make sure all the alternatives are static expressions 2232 -- First loop, make sure all the alternatives are static expressions
2204 -- none of which raise Constraint_Error. We make the constraint error 2233 -- none of which raise Constraint_Error. We make the Constraint_Error
2205 -- check because part of the legality condition for a correct static 2234 -- check because part of the legality condition for a correct static
2206 -- case expression is that the cases are covered, like any other case 2235 -- case expression is that the cases are covered, like any other case
2207 -- expression. And we can't do that if any of the conditions raise an 2236 -- expression. And we can't do that if any of the conditions raise an
2208 -- exception, so we don't even try to evaluate if that is the case. 2237 -- exception, so we don't even try to evaluate if that is the case.
2209 2238
2235 -- static (don't raise exceptions), so the whole case is static, and we 2264 -- static (don't raise exceptions), so the whole case is static, and we
2236 -- can find the matching alternative. 2265 -- can find the matching alternative.
2237 2266
2238 Set_Is_Static_Expression (N); 2267 Set_Is_Static_Expression (N);
2239 2268
2240 -- Now to deal with propagating a possible constraint error 2269 -- Now to deal with propagating a possible Constraint_Error
2241 2270
2242 -- If the selecting expression raises CE, propagate and we are done 2271 -- If the selecting expression raises CE, propagate and we are done
2243 2272
2244 if Raises_Constraint_Error (Expression (N)) then 2273 if Raises_Constraint_Error (Expression (N)) then
2245 Set_Raises_Constraint_Error (N); 2274 Set_Raises_Constraint_Error (N);
2406 Def_Id : constant Entity_Id := Entity (N); 2435 Def_Id : constant Entity_Id := Entity (N);
2407 Val : Node_Id; 2436 Val : Node_Id;
2408 2437
2409 begin 2438 begin
2410 -- Enumeration literals are always considered to be constants 2439 -- Enumeration literals are always considered to be constants
2411 -- and cannot raise constraint error (RM 4.9(22)). 2440 -- and cannot raise Constraint_Error (RM 4.9(22)).
2412 2441
2413 if Ekind (Def_Id) = E_Enumeration_Literal then 2442 if Ekind (Def_Id) = E_Enumeration_Literal then
2414 Set_Is_Static_Expression (N); 2443 Set_Is_Static_Expression (N);
2415 return; 2444 return;
2416 2445
2504 Set_Etype (N, Any_Type); 2533 Set_Etype (N, Any_Type);
2505 Set_Is_Static_Expression (N, False); 2534 Set_Is_Static_Expression (N, False);
2506 return; 2535 return;
2507 end if; 2536 end if;
2508 2537
2509 -- If condition raises constraint error then we have already signaled 2538 -- If condition raises Constraint_Error then we have already signaled
2510 -- an error, and we just propagate to the result and do not fold. 2539 -- an error, and we just propagate to the result and do not fold.
2511 2540
2512 if Raises_Constraint_Error (Condition) then 2541 if Raises_Constraint_Error (Condition) then
2513 Set_Raises_Constraint_Error (N); 2542 Set_Raises_Constraint_Error (N);
2514 return; 2543 return;
2529 Result := Else_Expr; 2558 Result := Else_Expr;
2530 Non_Result := Then_Expr; 2559 Non_Result := Then_Expr;
2531 end if; 2560 end if;
2532 2561
2533 -- Note that it does not matter if the non-result operand raises a 2562 -- Note that it does not matter if the non-result operand raises a
2534 -- Constraint_Error, but if the result raises constraint error then we 2563 -- Constraint_Error, but if the result raises Constraint_Error then we
2535 -- replace the node with a raise constraint error. This will properly 2564 -- replace the node with a raise Constraint_Error. This will properly
2536 -- propagate Raises_Constraint_Error since this flag is set in Result. 2565 -- propagate Raises_Constraint_Error since this flag is set in Result.
2537 2566
2538 if Raises_Constraint_Error (Result) then 2567 if Raises_Constraint_Error (Result) then
2539 Rewrite_In_Raise_CE (N, Result); 2568 Rewrite_In_Raise_CE (N, Result);
2540 Check_Non_Static_Context (Non_Result); 2569 Check_Non_Static_Context (Non_Result);
2882 2911
2883 -- Otherwise we definitely have a static expression 2912 -- Otherwise we definitely have a static expression
2884 2913
2885 Set_Is_Static_Expression (N); 2914 Set_Is_Static_Expression (N);
2886 2915
2887 -- If left operand raises constraint error, propagate and we are done 2916 -- If left operand raises Constraint_Error, propagate and we are done
2888 2917
2889 if Raises_Constraint_Error (Expr) then 2918 if Raises_Constraint_Error (Expr) then
2890 Set_Raises_Constraint_Error (N, True); 2919 Set_Raises_Constraint_Error (N, True);
2891 2920
2892 -- See if we match 2921 -- See if we match
3115 Test_Expression_Is_Foldable (N, Operand, Stat, Fold); 3144 Test_Expression_Is_Foldable (N, Operand, Stat, Fold);
3116 3145
3117 if not Fold then 3146 if not Fold then
3118 return; 3147 return;
3119 3148
3120 -- Don't try fold if target type has constraint error bounds 3149 -- Don't try fold if target type has Constraint_Error bounds
3121 3150
3122 elsif not Is_OK_Static_Subtype (Target_Type) then 3151 elsif not Is_OK_Static_Subtype (Target_Type) then
3123 Set_Raises_Constraint_Error (N); 3152 Set_Raises_Constraint_Error (N);
3124 return; 3153 return;
3125 end if; 3154 end if;
3643 end if; 3672 end if;
3644 3673
3645 -- Now look at the operands, we can't quite use the normal call to 3674 -- Now look at the operands, we can't quite use the normal call to
3646 -- Test_Expression_Is_Foldable here because short circuit operations 3675 -- Test_Expression_Is_Foldable here because short circuit operations
3647 -- are a special case, they can still be foldable, even if the right 3676 -- are a special case, they can still be foldable, even if the right
3648 -- operand raises constraint error. 3677 -- operand raises Constraint_Error.
3649 3678
3650 -- If either operand is Any_Type, just propagate to result and do not 3679 -- If either operand is Any_Type, just propagate to result and do not
3651 -- try to fold, this prevents cascaded errors. 3680 -- try to fold, this prevents cascaded errors.
3652 3681
3653 if Etype (Left) = Any_Type or else Etype (Right) = Any_Type then 3682 if Etype (Left) = Any_Type or else Etype (Right) = Any_Type then
3654 Set_Etype (N, Any_Type); 3683 Set_Etype (N, Any_Type);
3655 return; 3684 return;
3656 3685
3657 -- If left operand raises constraint error, then replace node N with 3686 -- If left operand raises Constraint_Error, then replace node N with
3658 -- the raise constraint error node, and we are obviously not foldable. 3687 -- the raise Constraint_Error node, and we are obviously not foldable.
3659 -- Is_Static_Expression is set from the two operands in the normal way, 3688 -- Is_Static_Expression is set from the two operands in the normal way,
3660 -- and we check the right operand if it is in a non-static context. 3689 -- and we check the right operand if it is in a non-static context.
3661 3690
3662 elsif Raises_Constraint_Error (Left) then 3691 elsif Raises_Constraint_Error (Left) then
3663 if not Rstat then 3692 if not Rstat then
3676 return; 3705 return;
3677 end if; 3706 end if;
3678 3707
3679 -- Here the result is static, note that, unlike the normal processing 3708 -- Here the result is static, note that, unlike the normal processing
3680 -- in Test_Expression_Is_Foldable, we did *not* check above to see if 3709 -- in Test_Expression_Is_Foldable, we did *not* check above to see if
3681 -- the right operand raises constraint error, that's because it is not 3710 -- the right operand raises Constraint_Error, that's because it is not
3682 -- significant if the left operand is decisive. 3711 -- significant if the left operand is decisive.
3683 3712
3684 Set_Is_Static_Expression (N); 3713 Set_Is_Static_Expression (N);
3685 3714
3686 -- It does not matter if the right operand raises constraint error if 3715 -- It does not matter if the right operand raises Constraint_Error if
3687 -- it will not be evaluated. So deal specially with the cases where 3716 -- it will not be evaluated. So deal specially with the cases where
3688 -- the right operand is not evaluated. Note that we will fold these 3717 -- the right operand is not evaluated. Note that we will fold these
3689 -- cases even if the right operand is non-static, which is fine, but 3718 -- cases even if the right operand is non-static, which is fine, but
3690 -- of course in these cases the result is not potentially static. 3719 -- of course in these cases the result is not potentially static.
3691 3720
3698 Fold_Uint (N, Left_Int, Rstat); 3727 Fold_Uint (N, Left_Int, Rstat);
3699 return; 3728 return;
3700 end if; 3729 end if;
3701 3730
3702 -- If first operand not decisive, then it does matter if the right 3731 -- If first operand not decisive, then it does matter if the right
3703 -- operand raises constraint error, since it will be evaluated, so 3732 -- operand raises Constraint_Error, since it will be evaluated, so
3704 -- we simply replace the node with the right operand. Note that this 3733 -- we simply replace the node with the right operand. Note that this
3705 -- properly propagates Is_Static_Expression and Raises_Constraint_Error 3734 -- properly propagates Is_Static_Expression and Raises_Constraint_Error
3706 -- (both are set to True in Right). 3735 -- (both are set to True in Right).
3707 3736
3708 if Raises_Constraint_Error (Right) then 3737 if Raises_Constraint_Error (Right) then
3949 Test_Expression_Is_Foldable (N, Operand, Stat, Fold); 3978 Test_Expression_Is_Foldable (N, Operand, Stat, Fold);
3950 3979
3951 if not Fold then 3980 if not Fold then
3952 return; 3981 return;
3953 3982
3954 -- Don't try fold if target type has constraint error bounds 3983 -- Don't try fold if target type has Constraint_Error bounds
3955 3984
3956 elsif not Is_OK_Static_Subtype (Target_Type) then 3985 elsif not Is_OK_Static_Subtype (Target_Type) then
3957 Set_Raises_Constraint_Error (N); 3986 Set_Raises_Constraint_Error (N);
3958 return; 3987 return;
3959 end if; 3988 end if;
4247 Val := Corresponding_Integer_Value (N); 4276 Val := Corresponding_Integer_Value (N);
4248 4277
4249 -- The NULL access value 4278 -- The NULL access value
4250 4279
4251 elsif Kind = N_Null then 4280 elsif Kind = N_Null then
4252 pragma Assert (Is_Access_Type (Underlying_Type (Etype (N)))); 4281 pragma Assert (Is_Access_Type (Underlying_Type (Etype (N)))
4282 or else Error_Posted (N));
4253 Val := Uint_0; 4283 Val := Uint_0;
4254 4284
4255 -- Otherwise must be character literal 4285 -- Character literal
4256 4286
4257 else 4287 elsif Kind = N_Character_Literal then
4258 pragma Assert (Kind = N_Character_Literal);
4259 Ent := Entity (N); 4288 Ent := Entity (N);
4260 4289
4261 -- Since Character literals of type Standard.Character don't 4290 -- Since Character literals of type Standard.Character don't
4262 -- have any defining character literals built for them, they 4291 -- have any defining character literals built for them, they
4263 -- do not have their Entity set, so just use their Char 4292 -- do not have their Entity set, so just use their Char
4267 if No (Ent) then 4296 if No (Ent) then
4268 Val := Char_Literal_Value (N); 4297 Val := Char_Literal_Value (N);
4269 else 4298 else
4270 Val := Enumeration_Pos (Ent); 4299 Val := Enumeration_Pos (Ent);
4271 end if; 4300 end if;
4301
4302 -- Unchecked conversion, which can come from System'To_Address (X)
4303 -- where X is a static integer expression. Recursively evaluate X.
4304
4305 elsif Kind = N_Unchecked_Type_Conversion then
4306 Val := Expr_Value (Expression (N));
4307
4308 else
4309 raise Program_Error;
4272 end if; 4310 end if;
4273 4311
4274 -- Come here with Val set to value to be returned, set cache 4312 -- Come here with Val set to value to be returned, set cache
4275 4313
4276 CV_Ent.N := N; 4314 CV_Ent.N := N;
4287 begin 4325 begin
4288 if Ekind (Ent) = E_Enumeration_Literal then 4326 if Ekind (Ent) = E_Enumeration_Literal then
4289 return Ent; 4327 return Ent;
4290 else 4328 else
4291 pragma Assert (Ekind (Ent) = E_Constant); 4329 pragma Assert (Ekind (Ent) = E_Constant);
4292 return Expr_Value_E (Constant_Value (Ent)); 4330
4331 -- We may be dealing with a enumerated character type constant, so
4332 -- handle that case here.
4333
4334 if Nkind (Constant_Value (Ent)) = N_Character_Literal then
4335 return Ent;
4336 else
4337 return Expr_Value_E (Constant_Value (Ent));
4338 end if;
4293 end if; 4339 end if;
4294 end Expr_Value_E; 4340 end Expr_Value_E;
4295 4341
4296 ------------------ 4342 ------------------
4297 -- Expr_Value_R -- 4343 -- Expr_Value_R --
4617 -- Note that we have to reset Is_Static_Expression both after the 4663 -- Note that we have to reset Is_Static_Expression both after the
4618 -- analyze step (because Resolve will evaluate the literal, which 4664 -- analyze step (because Resolve will evaluate the literal, which
4619 -- will cause semantic errors if it is marked as static), and after 4665 -- will cause semantic errors if it is marked as static), and after
4620 -- the Resolve step (since Resolve in some cases sets this flag). 4666 -- the Resolve step (since Resolve in some cases sets this flag).
4621 4667
4668 -- We mark the node as analyzed so that its type is not erased by
4669 -- calling Analyze_Real_Literal.
4670
4622 Analyze (N); 4671 Analyze (N);
4623 Set_Is_Static_Expression (N, Static); 4672 Set_Is_Static_Expression (N, Static);
4624 Set_Etype (N, Typ); 4673 Set_Etype (N, Typ);
4625 Resolve (N); 4674 Resolve (N);
4675 Set_Analyzed (N);
4626 Set_Is_Static_Expression (N, Static); 4676 Set_Is_Static_Expression (N, Static);
4627 end Fold_Ureal; 4677 end Fold_Ureal;
4628 4678
4629 --------------- 4679 ---------------
4630 -- From_Bits -- 4680 -- From_Bits --
4913 -------------------------- 4963 --------------------------
4914 -- Is_OK_Static_Subtype -- 4964 -- Is_OK_Static_Subtype --
4915 -------------------------- 4965 --------------------------
4916 4966
4917 -- Determines if Typ is a static subtype as defined in (RM 4.9(26)) where 4967 -- Determines if Typ is a static subtype as defined in (RM 4.9(26)) where
4918 -- neither bound raises constraint error when evaluated. 4968 -- neither bound raises Constraint_Error when evaluated.
4919 4969
4920 function Is_OK_Static_Subtype (Typ : Entity_Id) return Boolean is 4970 function Is_OK_Static_Subtype (Typ : Entity_Id) return Boolean is
4921 Base_T : constant Entity_Id := Base_Type (Typ); 4971 Base_T : constant Entity_Id := Base_Type (Typ);
4922 Anc_Subt : Entity_Id; 4972 Anc_Subt : Entity_Id;
4923 4973
5480 -- All cases except the special array case. 5530 -- All cases except the special array case.
5481 -- No message if we are dealing with System.Priority values in 5531 -- No message if we are dealing with System.Priority values in
5482 -- CodePeer mode where the target runtime may have more priorities. 5532 -- CodePeer mode where the target runtime may have more priorities.
5483 5533
5484 elsif not CodePeer_Mode or else Etype (N) /= RTE (RE_Priority) then 5534 elsif not CodePeer_Mode or else Etype (N) /= RTE (RE_Priority) then
5485 Apply_Compile_Time_Constraint_Error 5535 -- Determine if the out-of-range violation constitutes a warning
5486 (N, "value not in range of}", CE_Range_Check_Failed); 5536 -- or an error based on context, according to RM 4.9 (34/3).
5537
5538 if Nkind (Original_Node (N)) = N_Type_Conversion
5539 and then not Comes_From_Source (Original_Node (N))
5540 then
5541 Apply_Compile_Time_Constraint_Error
5542 (N, "value not in range of}??", CE_Range_Check_Failed);
5543 else
5544 Apply_Compile_Time_Constraint_Error
5545 (N, "value not in range of}", CE_Range_Check_Failed);
5546 end if;
5487 end if; 5547 end if;
5488 5548
5489 -- Here we generate a warning for the Ada 83 case, or when we are in an 5549 -- Here we generate a warning for the Ada 83 case, or when we are in an
5490 -- instance, or when we have a non-static expression case. 5550 -- instance, or when we have a non-static expression case.
5491 5551
5843 -- values match (RM 4.9.1(1)). 5903 -- values match (RM 4.9.1(1)).
5844 5904
5845 -- In addition, in GNAT, the object size (Esize) values of the types must 5905 -- In addition, in GNAT, the object size (Esize) values of the types must
5846 -- match if they are set (unless checking an actual for a formal derived 5906 -- match if they are set (unless checking an actual for a formal derived
5847 -- type). The use of 'Object_Size can cause this to be false even if the 5907 -- type). The use of 'Object_Size can cause this to be false even if the
5848 -- types would otherwise match in the RM sense. 5908 -- types would otherwise match in the Ada 95 RM sense, but this deviation
5909 -- is adopted by AI12-059 which introduces Object_Size in Ada 2020.
5849 5910
5850 function Subtypes_Statically_Match 5911 function Subtypes_Statically_Match
5851 (T1 : Entity_Id; 5912 (T1 : Entity_Id;
5852 T2 : Entity_Id; 5913 T2 : Entity_Id;
5853 Formal_Derived_Matching : Boolean := False) return Boolean 5914 Formal_Derived_Matching : Boolean := False) return Boolean
5859 return True; 5920 return True;
5860 5921
5861 -- No match if sizes different (from use of 'Object_Size). This test 5922 -- No match if sizes different (from use of 'Object_Size). This test
5862 -- is excluded if Formal_Derived_Matching is True, as the base types 5923 -- is excluded if Formal_Derived_Matching is True, as the base types
5863 -- can be different in that case and typically have different sizes. 5924 -- can be different in that case and typically have different sizes.
5864 -- ??? Frontend_Layout_On_Target used to set Esizes but this is no
5865 -- longer the case, consider removing the last test below.
5866 5925
5867 elsif not Formal_Derived_Matching 5926 elsif not Formal_Derived_Matching
5868 and then Known_Static_Esize (T1) 5927 and then Known_Static_Esize (T1)
5869 and then Known_Static_Esize (T2) 5928 and then Known_Static_Esize (T2)
5870 and then Esize (T1) /= Esize (T2) 5929 and then Esize (T1) /= Esize (T2)
5978 -- In such a case, use the discriminant constraint of the full view, 6037 -- In such a case, use the discriminant constraint of the full view,
5979 -- which must exist because we know that the two subtypes have the 6038 -- which must exist because we know that the two subtypes have the
5980 -- same base type. 6039 -- same base type.
5981 6040
5982 if Has_Discriminants (T1) /= Has_Discriminants (T2) then 6041 if Has_Discriminants (T1) /= Has_Discriminants (T2) then
5983 -- A generic actual type is declared through a subtype declaration 6042 if In_Instance then
5984 -- and may have an inconsistent indication of the presence of
5985 -- discriminants, so check the type it renames.
5986
5987 if Is_Generic_Actual_Type (T1)
5988 and then not Has_Discriminants (Etype (T1))
5989 and then not Has_Discriminants (T2)
5990 then
5991 return True;
5992
5993 elsif In_Instance then
5994 if Is_Private_Type (T2) 6043 if Is_Private_Type (T2)
5995 and then Present (Full_View (T2)) 6044 and then Present (Full_View (T2))
5996 and then Has_Discriminants (Full_View (T2)) 6045 and then Has_Discriminants (Full_View (T2))
5997 then 6046 then
5998 return Subtypes_Statically_Match (T1, Full_View (T2)); 6047 return Subtypes_Statically_Match (T1, Full_View (T2));
6042 if not Is_OK_Static_Expression (Expr1) 6091 if not Is_OK_Static_Expression (Expr1)
6043 or else not Is_OK_Static_Expression (Expr2) 6092 or else not Is_OK_Static_Expression (Expr2)
6044 then 6093 then
6045 return False; 6094 return False;
6046 6095
6047 -- If either expression raised a constraint error, 6096 -- If either expression raised a Constraint_Error,
6048 -- consider the expressions as matching, since this 6097 -- consider the expressions as matching, since this
6049 -- helps to prevent cascading errors. 6098 -- helps to prevent cascading errors.
6050 6099
6051 elsif Raises_Constraint_Error (Expr1) 6100 elsif Raises_Constraint_Error (Expr1)
6052 or else Raises_Constraint_Error (Expr2) 6101 or else Raises_Constraint_Error (Expr2)
6253 6302
6254 if Etype (Op1) = Any_Type then 6303 if Etype (Op1) = Any_Type then
6255 Set_Etype (N, Any_Type); 6304 Set_Etype (N, Any_Type);
6256 return; 6305 return;
6257 6306
6258 -- If operand raises constraint error, then replace node N with the 6307 -- If operand raises Constraint_Error, then replace node N with the
6259 -- raise constraint error node, and we are obviously not foldable. 6308 -- raise Constraint_Error node, and we are obviously not foldable.
6260 -- Note that this replacement inherits the Is_Static_Expression flag 6309 -- Note that this replacement inherits the Is_Static_Expression flag
6261 -- from the operand. 6310 -- from the operand.
6262 6311
6263 elsif Raises_Constraint_Error (Op1) then 6312 elsif Raises_Constraint_Error (Op1) then
6264 Rewrite_In_Raise_CE (N, Op1); 6313 Rewrite_In_Raise_CE (N, Op1);
6281 then 6330 then
6282 Check_Non_Static_Context (Op1); 6331 Check_Non_Static_Context (Op1);
6283 return; 6332 return;
6284 6333
6285 -- Here we have the case of an operand whose type is OK, which is 6334 -- Here we have the case of an operand whose type is OK, which is
6286 -- static, and which does not raise constraint error, we can fold. 6335 -- static, and which does not raise Constraint_Error, we can fold.
6287 6336
6288 else 6337 else
6289 Set_Is_Static_Expression (N); 6338 Set_Is_Static_Expression (N);
6290 Fold := True; 6339 Fold := True;
6291 Stat := True; 6340 Stat := True;
6321 6370
6322 if Etype (Op1) = Any_Type or else Etype (Op2) = Any_Type then 6371 if Etype (Op1) = Any_Type or else Etype (Op2) = Any_Type then
6323 Set_Etype (N, Any_Type); 6372 Set_Etype (N, Any_Type);
6324 return; 6373 return;
6325 6374
6326 -- If left operand raises constraint error, then replace node N with the 6375 -- If left operand raises Constraint_Error, then replace node N with the
6327 -- Raise_Constraint_Error node, and we are obviously not foldable. 6376 -- Raise_Constraint_Error node, and we are obviously not foldable.
6328 -- Is_Static_Expression is set from the two operands in the normal way, 6377 -- Is_Static_Expression is set from the two operands in the normal way,
6329 -- and we check the right operand if it is in a non-static context. 6378 -- and we check the right operand if it is in a non-static context.
6330 6379
6331 elsif Raises_Constraint_Error (Op1) then 6380 elsif Raises_Constraint_Error (Op1) then
6374 end if; 6423 end if;
6375 6424
6376 return; 6425 return;
6377 6426
6378 -- Else result is static and foldable. Both operands are static, and 6427 -- Else result is static and foldable. Both operands are static, and
6379 -- neither raises constraint error, so we can definitely fold. 6428 -- neither raises Constraint_Error, so we can definitely fold.
6380 6429
6381 else 6430 else
6382 Set_Is_Static_Expression (N); 6431 Set_Is_Static_Expression (N);
6383 Fold := True; 6432 Fold := True;
6384 Stat := True; 6433 Stat := True;
6411 -- want cascaded errors based on some false analysis of a junk node. 6460 -- want cascaded errors based on some false analysis of a junk node.
6412 6461
6413 if Error_Posted (N) then 6462 if Error_Posted (N) then
6414 return Unknown; 6463 return Unknown;
6415 6464
6416 -- Expression that raises constraint error is an odd case. We certainly 6465 -- Expression that raises Constraint_Error is an odd case. We certainly
6417 -- do not want to consider it to be in range. It might make sense to 6466 -- do not want to consider it to be in range. It might make sense to
6418 -- consider it always out of range, but this causes incorrect error 6467 -- consider it always out of range, but this causes incorrect error
6419 -- messages about static expressions out of range. So we just return 6468 -- messages about static expressions out of range. So we just return
6420 -- Unknown, which is always safe. 6469 -- Unknown, which is always safe.
6421 6470
6599 6648
6600 if Is_OK_Static_Expression (Expr) then 6649 if Is_OK_Static_Expression (Expr) then
6601 return; 6650 return;
6602 end if; 6651 end if;
6603 6652
6604 -- Test for constraint error raised 6653 -- Test for Constraint_Error raised
6605 6654
6606 if Raises_Constraint_Error (Expr) then 6655 if Raises_Constraint_Error (Expr) then
6607 6656
6608 -- Special case membership to find out which piece to flag 6657 -- Special case membership to find out which piece to flag
6609 6658