o
    2g                     @  s   d Z ddlmZ ddlmZmZmZmZmZm	Z	m
Z
mZmZmZmZmZ ddlmZ ddlmZmZ ddlmZ ddlmZ dddZdddZdddZdS )zIRBuilder AST transform helpers shared between expressions and statements.

Shared code that is tightly coupled to mypy ASTs can be put here instead of
making mypyc.irbuild.builder larger.
    )annotations)LDEF	BytesExprComparisonExpr
Expression	FloatExprIntExpr
MemberExprNameExprOpExprStrExpr	UnaryExprVar)
BasicBlock)is_fixed_width_rtype	is_tagged)	IRBuilder)constant_fold_exprselfr   er   truer   falsereturnNonec                 C  s   t |trC|jdv rC|jdkr)t }t| |j|| | | t| |j|| d S t }t| |j|| | | t| |j|| d S t |trW|jdkrWt| |j	|| d S t
| |||}|rbd S | |}| ||| d S )N)andorr   not)
isinstancer   opr   process_conditionalleftactivate_blockrightr   expr$maybe_process_conditional_comparisonacceptadd_bool_branch)r   r   r   r   newresreg r*   Q/home/garg/my-data/venv/lib/python3.10/site-packages/mypyc/irbuild/ast_helpers.pyr      s"   



r   boolc                 C  sN  t |trt|jdkrdS | |jd }| |jd }t|s&t|r.t|s0t|s0dS |jd }|dvr;dS |jd }|jd }t| |}	| j	||	d}
| j	|dd}t|s`t|rt|sm| 
|
||j}
nt|sy| 
|||j}| |
|||j}| j  | ||| dS | j|
|||j}|   | ||| dS )ak  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 is true
        false: Branch target if comparison is false
       Fr      )z==z!=<z<=>z>=)
can_borrowT)r   r   lenoperands	node_typer   r   	operatorsis_borrow_friendly_exprr%   coerceline	binary_opbuilderflush_keep_alivesr&   )r   r   r   r   ltypertyper   	left_expr
right_exprborrow_leftr    r"   r)   r*   r*   r+   r$   8   sD   




r$   r#   c                 C  sz   t |ttttfrdS t |ttttfrt	| |durdS t |tr/t |j
tr/|jtkr/dS t |tr;| |r;dS dS )zCan the result of the expression borrowed temporarily?

    Borrowing means keeping a reference without incrementing the reference count.
    TNF)r   r   r   r   r   r   r   r
   r	   r   noder   kindr   is_native_attr_ref)r   r#   r*   r*   r+   r6   g   s   
r6   N)
r   r   r   r   r   r   r   r   r   r   )
r   r   r   r   r   r   r   r   r   r,   )r   r   r#   r   r   r,   )__doc__
__future__r   
mypy.nodesr   r   r   r   r   r   r	   r
   r   r   r   r   mypyc.ir.opsr   mypyc.ir.rtypesr   r   mypyc.irbuild.builderr   mypyc.irbuild.constant_foldr   r   r$   r6   r*   r*   r*   r+   <module>   s    8

/