MCPcopy Index your code
hub / github.com/python/mypy / maybe_process_conditional_comparison

Function maybe_process_conditional_comparison

mypyc/irbuild/ast_helpers.py:56–100  ·  view source on GitHub ↗

Transform simple tagged integer comparisons in a conditional context. Return True if the operation is supported (and was transformed). Otherwise, do nothing and return False. Args: self: IR form Builder e: Arbitrary expression true: Branch target if comparison i

(
    self: IRBuilder, e: Expression, true: BasicBlock, false: BasicBlock
)

Source from the content-addressed store, hash-verified

54
55
56def maybe_process_conditional_comparison(
57 self: IRBuilder, e: Expression, true: BasicBlock, false: BasicBlock
58) -> bool:
59 """Transform simple tagged integer comparisons in a conditional context.
60
61 Return True if the operation is supported (and was transformed). Otherwise,
62 do nothing and return False.
63
64 Args:
65 self: IR form Builder
66 e: Arbitrary expression
67 true: Branch target if comparison is true
68 false: Branch target if comparison is false
69 """
70 if not isinstance(e, ComparisonExpr) or len(e.operands) != 2:
71 return False
72 ltype = self.node_type(e.operands[0])
73 rtype = self.node_type(e.operands[1])
74 if not (
75 (is_tagged(ltype) or is_fixed_width_rtype(ltype))
76 and (is_tagged(rtype) or is_fixed_width_rtype(rtype))
77 ):
78 return False
79 op = e.operators[0]
80 if op not in ("==", "!=", "<", "<=", ">", ">="):
81 return False
82 left_expr = e.operands[0]
83 right_expr = e.operands[1]
84 borrow_left = is_borrow_friendly_expr(self, right_expr)
85 left = self.accept(left_expr, can_borrow=borrow_left)
86 right = self.accept(right_expr, can_borrow=True)
87 if is_fixed_width_rtype(ltype) or is_fixed_width_rtype(rtype):
88 if not is_fixed_width_rtype(ltype):
89 left = self.coerce(left, rtype, e.line)
90 elif not is_fixed_width_rtype(rtype):
91 right = self.coerce(right, ltype, e.line)
92 reg = self.binary_op(left, right, op, e.line)
93 self.builder.flush_keep_alives(e.line)
94 self.add_bool_branch(reg, true, false)
95 else:
96 # "left op right" for two tagged integers
97 reg = self.builder.binary_op(left, right, op, e.line)
98 self.flush_keep_alives(e.line)
99 self.add_bool_branch(reg, true, false)
100 return True
101
102
103def is_borrow_friendly_expr(self: IRBuilder, expr: Expression) -> bool:

Callers 1

process_conditionalFunction · 0.85

Calls 11

is_taggedFunction · 0.90
is_fixed_width_rtypeFunction · 0.90
isinstanceFunction · 0.85
lenFunction · 0.85
is_borrow_friendly_exprFunction · 0.85
node_typeMethod · 0.80
acceptMethod · 0.45
coerceMethod · 0.45
binary_opMethod · 0.45
flush_keep_alivesMethod · 0.45
add_bool_branchMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…