o
    2g                     @  s   U d Z ddlmZ ddlmZmZ ddlmZ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 ddlmZ ddlmZ eeeeeef ZeeeeefZde d	< dddZ!dddZ"dS )ad  Constant folding of IR values.

For example, 3 + 5 can be constant folded into 8.

This is mostly like mypy.constant_fold, but we can bind some additional
NameExpr and MemberExpr references here, since we have more knowledge
about which definitions can be trusted -- we constant fold only references
to other compiled modules in the same compilation unit.
    )annotations)FinalUnion)constant_fold_binary_opconstant_fold_unary_op)	BytesExprComplexExpr
Expression	FloatExprIntExpr
MemberExprNameExprOpExprStrExpr	UnaryExprVar)	IRBuilder)bytes_from_strr   CONST_TYPESbuilderr   exprr	   returnConstantValue | Nonec                 C  sL  t |tr|jS t |tr|jS t |tr|jS t |tr"t|jS t |tr*|jS t |trF|j	}t |t
rD|jrD|j}t |trD|S dS t |trh| |}|durf|\}}}|jrf|j}t |trf|S dS t |trt| |j}t| |j}	|dur|	durt|j||	S dS t |trt| |j}
|
durt |
tst|j|
S dS )zeReturn the constant value of an expression for supported operations.

    Return None otherwise.
    N)
isinstancer   valuer
   r   r   r   r   r   noder   is_finalfinal_valuer   r   get_final_refr   constant_fold_exprleftright constant_fold_binary_op_extendedopr   r   bytesr   )r   r   r   r   finalfn	final_varnativer    r!   r    r)   S/home/garg/my-data/venv/lib/python3.10/site-packages/mypyc/irbuild/constant_fold.pyr   %   sJ   














r   r#   strr    ConstantValuer!   c                 C  s   t |tst |tst| ||S | dkr"t |tr"t |tr"|| S | dkr4t |tr4t |tr4|| S | dkrFt |trFt |trF|| S dS )zLike mypy's constant_fold_binary_op(), but includes bytes support.

    mypy cannot use constant folded bytes easily so it's simpler to only support them in mypyc.
    +*N)r   r$   r   int)r#   r    r!   r)   r)   r*   r"   N   s   r"   N)r   r   r   r	   r   r   )r#   r+   r    r,   r!   r,   r   r   )#__doc__
__future__r   typingr   r   mypy.constant_foldr   r   
mypy.nodesr   r   r	   r
   r   r   r   r   r   r   r   mypyc.irbuild.builderr   mypyc.irbuild.utilr   r/   floatcomplexr+   r$   r,   r   __annotations__r   r"   r)   r)   r)   r*   <module>   s    
4
)