o
    @a                     @   s   d Z ddl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 Zd	d
 Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zed#d$ ZdS )%z+ Common function use for AST manipulation.     NMODULES)mangledemangle)reduce)contextmanagerc                 C   s   t | tjot | jtS N)
isinstanceastConstantvaluestrnode r   //usr/lib/python3/dist-packages/pythran/utils.pyisstr
   s   r   c                 C   s   t | tjot | jttfS r   )r	   r
   r   r   intboolr   r   r   r   
isintegral   s   r   c                 C   s   t | tjot | jtttfS r   )r	   r
   r   r   r   floatr   r   r   r   r   isnum   s   r   c                 C   s$   t | tjsdS tdd | jD S )NFc                 s   s    | ]	}t |tjV  qd S r   )r	   r
   ZSlice).0eltr   r   r   	<genexpr>   s    zisextslice.<locals>.<genexpr>)r	   r
   ZTupleanyeltsr   r   r   r   
isextslice   s   r   c                 C   s,   t | jtjs	dS t| jsdS | jjdkS )NFr   )r	   opr
   ZPowr   rightr   r   r   r   r   ispowi   s
   
r    c                    sF    fdd  t | \}}| s|dd d|d f }|d| fS )z5 Compute path and final object for an attribute node c                    s\   t |tjr| t|j t|jffS t |tjr, | |j\}}||j ||jf fS dS )z9 Get function path and intrinsic from an ast.Attribute.  N)r	   r
   Namer   id	Attributer   attr)modulesr$   modulepathget_intrinsic_pathr   r   r)   *   s   z(attr_to_path.<locals>.get_intrinsic_pathNZfunctor)Zpythonic)r   Z	isliteral)r   objr'   r   r(   r   attr_to_path'   s
   r,   c              	   C   s0   t dd | dd tt| d t ddS )aO  
    Transform path to ast.Attribute.

    >>> import gast as ast
    >>> path = ('builtins', 'my', 'constant')
    >>> value = path_to_attr(path)
    >>> ref = ast.Attribute(
    ...     value=ast.Attribute(value=ast.Name(id="builtins",
    ...                                        ctx=ast.Load(),
    ...                                        annotation=None,
    ...                                        type_comment=None),
    ...                         attr="my", ctx=ast.Load()),
    ...     attr="constant", ctx=ast.Load())
    >>> ast.dump(ref) == ast.dump(value)
    True
    c                 S   s   t | |t  S r   )r
   r#   Load)ZhpathZlastr   r   r   <lambda>H   s    zpath_to_attr.<locals>.<lambda>   Nr   )r   r
   r!   r   r-   r'   r   r   r   path_to_attr7   s   $r1   c                 C   s0   t | dkrt| d  S t| dd | d  S )z
    Retrieve a symbol in MODULES based on its path
    >>> path = ('math', 'pi')
    >>> path_to_node(path) #doctest: +ELLIPSIS
    <pythran.intrinsic.ConstantIntr object at 0x...>
    r/   r   Nr*   )lenr   path_to_noder0   r   r   r   r3   L   s   r3   c                 C   s   t | tjot| jdd dkS )Nr$   getattr)r	   r
   ZCallr4   funcr   r   r   r   isattrY   s   r6   c                 C   sd   d}t | tjtjfsJ |t | tjst| r0t| r#| jd } n| j} t | tjst| s| S )a  
    Return modified variable name.

    >>> import gast as ast
    >>> ref = ast.Subscript(
    ...     value=ast.Subscript(
    ...         value=ast.Name('a', ast.Load(), None, None),
    ...         slice=ast.Name('i', ast.Load(), None, None),
    ...         ctx=ast.Load()),
    ...     slice=ast.Name('j', ast.Load(), None, None),
    ...     ctx=ast.Load())
    >>> ast.dump(get_variable(ref))
    "Name(id='a', ctx=Load(), annotation=None, type_comment=None)"
    z(Only name and subscript can be assigned.r   )r	   r
   r!   Z	Subscriptr6   argsr   )Z
assignablemsgr   r   r   get_variable^   s   r9   c                 C   s   t d d |  S NbuiltinsZpythranr   namer   r   r   pythran_builtinw   s   r>   c                 C   s   | t d d v s
J dd| fS r:   r   r<   r   r   r   pythran_builtin_path{   s   
r?   c                 C   s   t t| S r   )r1   r?   r<   r   r   r   pythran_builtin_attr   s   r@   c                 C   s   ddl m} | d| |v   S )Nr   )cxx_keywords_)pythran.tablesrA   )r=   rA   r   r   r   cxxid   s   rD   c                 c   s    |  | d V  |   d S r   )appendpop)lvr   r   r   pushpop   s   
rI   )__doc__Zgastr
   rC   r   Zpythran.conversionr   r   	functoolsr   
contextlibr   r   r   r   r   r    r,   r1   r3   r6   r9   r>   r?   r@   rD   rI   r   r   r   r   <module>   s,    