Can the result of the expression borrowed temporarily? Borrowing means keeping a reference without incrementing the reference count.
(self: IRBuilder, expr: Expression)
| 101 | |
| 102 | |
| 103 | def is_borrow_friendly_expr(self: IRBuilder, expr: Expression) -> bool: |
| 104 | """Can the result of the expression borrowed temporarily? |
| 105 | |
| 106 | Borrowing means keeping a reference without incrementing the reference count. |
| 107 | """ |
| 108 | if isinstance(expr, (IntExpr, FloatExpr, StrExpr, BytesExpr)): |
| 109 | # Literals are immortal and can always be borrowed |
| 110 | return True |
| 111 | if ( |
| 112 | isinstance(expr, (UnaryExpr, OpExpr, NameExpr, MemberExpr)) |
| 113 | and constant_fold_expr(self, expr) is not None |
| 114 | ): |
| 115 | # Literal expressions are similar to literals |
| 116 | return True |
| 117 | if isinstance(expr, NameExpr): |
| 118 | if isinstance(expr.node, Var) and expr.kind == LDEF: |
| 119 | # Local variable reference can be borrowed |
| 120 | return True |
| 121 | if isinstance(expr, MemberExpr) and self.is_native_attr_ref(expr): |
| 122 | return True |
| 123 | return False |
no test coverage detected
searching dependent graphs…