Debian Codegraph @

mypyc/irbuild/constant_fold.py

raw
1 """Constant folding of IR values.
2
3 For example, 3 + 5 can be constant folded into 8.
4
5 This is mostly like mypy.constant_fold, but we can bind some additional
6 NameExpr and MemberExpr references here, since we have more knowledge
7 about which definitions can be trusted -- we constant fold only references
8 to other compiled modules in the same compilation unit.
9 """
10
11 from __future__ import annotations
12
13 from typing import TYPE_CHECKING, Final
14
15 from mypy.constant_fold import constant_fold_binary_op, constant_fold_unary_op
16 from mypy.nodes import (
17 BytesExpr,
18 ComplexExpr,
19 Expression,
20 FloatExpr,
21 IntExpr,
22 MemberExpr,
23 NameExpr,
24 OpExpr,
25 StrExpr,
26 UnaryExpr,
27 Var,
28 )
29 from mypyc.irbuild.util import bytes_from_str
30
31 if TYPE_CHECKING:
32 from mypyc.irbuild.builder import IRBuilder
33
34 # All possible result types of constant folding
35 ConstantValue = int | float | complex | str | bytes
36 CONST_TYPES: Final = (int, float, complex, str, bytes)
37
38
39 def constant_fold_expr(builder: IRBuilder, expr: Expression) -> ConstantValue | None:
40 """Return the constant value of an expression for supported operations.
41
42 Return None otherwise.
43 """
44 if isinstance(expr, IntExpr):
45 return expr.value
46 if isinstance(expr, FloatExpr):
47 return expr.value
48 if isinstance(expr, StrExpr):
49 return expr.value
50 if isinstance(expr, BytesExpr):
51 return bytes_from_str(expr.value)
52 if isinstance(expr, ComplexExpr):
53 return expr.value
54 elif isinstance(expr, NameExpr):
55 node = expr.node
56 if isinstance(node, Var) and node.is_final:
57 final_value = node.final_value
58 if isinstance(final_value, (CONST_TYPES)):
59 return final_value
60 elif isinstance(expr, MemberExpr):
61 final = builder.get_final_ref(expr)
62 if final is not None:
63 fn, final_var, native = final
64 if final_var.is_final:
65 final_value = final_var.final_value
66 if isinstance(final_value, (CONST_TYPES)):
67 return final_value
68 elif isinstance(expr, OpExpr):
69 left = constant_fold_expr(builder, expr.left)
70 right = constant_fold_expr(builder, expr.right)
71 if left is not None and right is not None:
72 return constant_fold_binary_op_extended(expr.op, left, right)
73 elif isinstance(expr, UnaryExpr):
74 value = constant_fold_expr(builder, expr.expr)
75 if value is not None and not isinstance(value, bytes):
76 return constant_fold_unary_op(expr.op, value)
77 return None
78
79
80 def constant_fold_binary_op_extended(
81 op: str, left: ConstantValue, right: ConstantValue
82 ) -> ConstantValue | None:
83 """Like mypy's constant_fold_binary_op(), but includes bytes support.
84
85 mypy cannot use constant folded bytes easily so it's simpler to only support them in mypyc.
86 """
87 if not isinstance(left, bytes) and not isinstance(right, bytes):
88 return constant_fold_binary_op(op, left, right)
89
90 if op == "+" and isinstance(left, bytes) and isinstance(right, bytes):
91 return left + right
92 elif op == "*" and isinstance(left, bytes) and isinstance(right, int):
93 return left * right
94 elif op == "*" and isinstance(left, int) and isinstance(right, bytes):
95 return left * right
96
97 return None