U
    ,d=&                     @   s   d Z ddlZddlZddlZddlmZ ddlmZ G dd dZG dd dej	d	d
Z
G dd dZG dd deZG dd dZe ZG dd dedZdS )z[
This module contains utils for manipulating target configurations such as
compiler flags.
    N)MappingProxyType)utilsc                   @   s@   e Zd ZdZdZdd Zedd Zedd Zed	d
 Z	dS )Optionz.An option to be used in ``TargetConfig``.
    _type_default_docc                C   s   || _ || _|| _dS )a-  
        Parameters
        ----------
        type :
            Type of the option value. It can be a callable.
            The setter always calls ``self._type(value)``.
        default :
            The default value for the option.
        doc : str
            Docstring for the option.
        Nr   )selftypedefaultdoc r   ;/tmp/pip-unpacked-wheel-eu7e0c37/numba/core/targetconfig.py__init__   s    zOption.__init__c                 C   s   | j S N)r   r	   r   r   r   r
   "   s    zOption.typec                 C   s   | j S r   )r   r   r   r   r   r   &   s    zOption.defaultc                 C   s   | j S r   )r   r   r   r   r   r   *   s    z
Option.docN)
__name__
__module____qualname____doc__	__slots__r   propertyr
   r   r   r   r   r   r   r      s   

r   c                   @   s   e Zd ZdS )_FlagsStackN)r   r   r   r   r   r   r   r   /   s   r   flags)Z
stack_namec                   @   s<   e Zd ZdZedd Zdd Zdd Zdd	 Zd
d Z	dS )ConfigStackzA stack for tracking target configurations in the compiler.

    It stores the stack in a thread-local class attribute. All instances in the
    same thread will see the same stack.
    c                 C   s   |  }|r|  }nd}|S )z8Get the TOS or return None if no config is set.
        N)top)clsr	   r   r   r   r   top_or_none9   s
    
zConfigStack.top_or_nonec                 C   s   t  | _d S r   )r   _stkr   r   r   r   r   E   s    zConfigStack.__init__c                 C   s
   | j  S r   )r   r   r   r   r   r   r   H   s    zConfigStack.topc                 C   s
   t | jS r   )lenr   r   r   r   r   __len__K   s    zConfigStack.__len__c                 C   s   | j |S )zgReturns a contextmanager that performs ``push(flags)`` on enter and
        ``pop()`` on exit.
        )r   enter)r	   r   r   r   r   r!   N   s    zConfigStack.enterN)
r   r   r   r   classmethodr   r   r   r    r!   r   r   r   r   r   3   s   
r   c                   @   s    e Zd ZdZdd Zdd ZdS )_MetaTargetConfigzMetaclass for ``TargetConfig``.

    When a subclass of ``TargetConfig`` is created, all ``Option`` defined
    as class members will be parsed and corresponding getters, setters, and
    delters will be inserted.
    c                 C   sj   i }t |D ]}||j q|| | t|| _dd }| j D ]\}}t| |||| qJdS )zInvoked when subclass is created.

        Insert properties for each ``Option`` that are class members.
        All the options will be grouped inside the ``.options`` class
        attribute.
        c                    s8    fdd} fdd} fdd}t |||jS )Nc                    s   | j  jS r   )_valuesgetr   r   nameoptionr   r   gettero   s    z=_MetaTargetConfig.__init__.<locals>.make_prop.<locals>.getterc                    s    || j < d S r   )r
   r$   )r	   valr&   r   r   setterr   s    z=_MetaTargetConfig.__init__.<locals>.make_prop.<locals>.setterc                    s   | j  = d S r   )r$   r   )r'   r   r   delteru   s    z=_MetaTargetConfig.__init__.<locals>.make_prop.<locals>.delter)r   r   )r'   r(   r)   r+   r,   r   r&   r   	make_propn   s    z-_MetaTargetConfig.__init__.<locals>.make_propN)reversedupdateoptionsfind_optionsr   itemssetattr)r   r'   basesdctoptsZbase_clsr-   r(   r   r   r   r   \   s    
z_MetaTargetConfig.__init__c                 C   s   dd |  D S )z[Returns a new dict with all the items that are a mapping to an
        ``Option``.
        c                 S   s    i | ]\}}t |tr||qS r   )
isinstancer   .0kvr   r   r   
<dictcomp>   s     
  z2_MetaTargetConfig.find_options.<locals>.<dictcomp>)r2   )r   r5   r   r   r   r1   }   s    z_MetaTargetConfig.find_optionsN)r   r   r   r   r   r1   r   r   r   r   r#   U   s   !r#   c                   @   s   e Zd Zdd ZdS )_NotSetTypec                 C   s   dS )Nz<NotSet>r   r   r   r   r   __repr__   s    z_NotSetType.__repr__N)r   r   r   r>   r   r   r   r   r=      s   r=   c                   @   s   e Zd ZdZddiZd%ddZdd Zd	d
 Zdd Zdd Z	dd Z
dd ZefddZdd ZedddZdd Zdd ZeedddZedd d!Zeeed"d#d$ZdS )&TargetConfiga  Base class for ``TargetConfig``.

    Subclass should fill class members with ``Option``. For example:

    >>> class MyTargetConfig(TargetConfig):
    >>>     a_bool_option = Option(type=bool, default=False, doc="a bool")
    >>>     an_int_option = Option(type=int, default=0, doc="an int")

    The metaclass will insert properties for each ``Option``. For example:

    >>> tc = MyTargetConfig()
    >>> tc.a_bool_option = True  # invokes the setter
    >>> print(tc.an_int_option)  # print the default
    wbitsiNc                 C   s.   i | _ |dk	r*t|tst| j |j  dS )z
        Parameters
        ----------
        copy_from : TargetConfig or None
            if None, creates an empty ``TargetConfig``.
            Otherwise, creates a copy.
        N)r$   r7   r?   AssertionErrorr/   )r	   Z	copy_fromr   r   r   r      s    zTargetConfig.__init__c                 C   sr   g }g }| j D ]8}| dt| | }| |s<|| q|| q| jj}| dd| dd| dS )N=(, z, [z]))r0   getattris_setappend	__class__r   join)r	   argsZdefsr:   msgclsnamer   r   r   r>      s    

zTargetConfig.__repr__c                 C   s   t tt|  S r   )hashtuplesortedvaluesr   r   r   r   __hash__   s    zTargetConfig.__hash__c                 C   s"   t |tr|  | kS tS d S r   )r7   r?   rP   NotImplemented)r	   otherr   r   r   __eq__   s    
zTargetConfig.__eq__c                    s    fdd j D S )z)Returns a dict of all the values
        c                    s   i | ]}|t  |qS r   )rE   )r9   r:   r   r   r   r<      s      z'TargetConfig.values.<locals>.<dictcomp>)r0   r   r   r   r   rP      s    zTargetConfig.valuesc                 C   s   |  | || jkS )zIs the option set?
        )_guard_optionr$   r	   r'   r   r   r   rF      s    
zTargetConfig.is_setc                 C   s   |  | | j|d dS )zRemove the option by name if it is defined.

        After this, the value for the option will be set to its default value.
        N)rU   r$   poprV   r   r   r   discard   s    
zTargetConfig.discardc                 C   sR   |  | | |sNt }|r:| }t| |t|| n|tk	rNt| || dS )aL  Inherit flag from ``ConfigStack``.

        Parameters
        ----------
        name : str
            Option name.
        default : optional
            When given, it overrides the default value.
            It is only used when the flag is not defined locally and there is
            no entry in the ``ConfigStack``.
        N)rU   rF   r   r   r3   rE   _NotSet)r	   r'   r   Zcstkr   r   r   r   inherit_if_not_set   s    

zTargetConfig.inherit_if_not_setc                 C   s   t | | S )zClone this instance.
        )r
   r   r   r   r   copy   s    zTargetConfig.copy)returnc                 C   s0   dd |   D }| jj}| dd| dS )zReturns a ``str`` that summarizes this instance.

        In contrast to ``__repr__``, only options that are explicitly set will
        be shown.
        c                 S   s   g | ]\}}| d | qS )rB   r   r8   r   r   r   
<listcomp>   s     z(TargetConfig.summary.<locals>.<listcomp>rC   rD   ))_summary_argsrH   r   rI   )r	   rJ   rL   r   r   r   summary   s    zTargetConfig.summaryc                 C   s(   || j kr$|dt|  }t|d S )Nz is not a valid option for )r0   r
   
ValueError)r	   r'   rK   r   r   r   rU      s    
zTargetConfig._guard_optionc                 C   sR   g }t | jD ]>}| j| }| |rt| |}|j|kr||f}|| q|S )zreturns a sorted sequence of 2-tuple containing the
        ``(flag_name, flag_value)`` for flag that are set with a non-default
        value.
        )rO   r0   rF   rE   r   rG   )r	   rJ   r:   optZflagvalr;   r   r   r   r_     s    



zTargetConfig._summary_argsc                 C   sf   g }| d | | jj |ddg | j D ]"\}}| | | t|j q4d|	 S )z]Returns a ``bytes`` object suitable for use as a dictionary for
        compression.
        ZnumbaTrueFalse )
rG   rH   r   extendr0   r2   strr   rI   encode)r   bufr:   rb   r   r   r   _make_compression_dictionary  s    

z)TargetConfig._make_compression_dictionaryc                 C   sX   |   }tjf |tjd| j}||   g}||	  t
d| S )z6Return a string suitable for symbol mangling.
        )zdictlevel    )rj   zlibcompressobjZ_BEST_COMPRESSION_ZLIB_CONFIGcompressr`   rh   rG   flushbase64	b64encoderI   decode)r	   rk   compri   r   r   r   get_mangle_string"  s    zTargetConfig.get_mangle_string)mangledr\   c                 C   sz   dd }t d||}t|}|  }tjf d|i| j}g }|r^||	| |j
}qB||  d| S )zCReturns the demangled result from ``.get_mangle_string()``
        c                 S   s    t td| ddd   dS )N0xr         )chrintgroup)xr   r   r   repl4  s    z#TargetConfig.demangle.<locals>.replz_[a-zA-Z0-9][a-zA-Z0-9]rk   rm   )resubrt   	b64decoderj   rn   decompressobjrq   rG   
decompressunconsumed_tailrs   rI   rv   )r   ry   r   Z	unescapedrawrk   dcri   r   r   r   demangle/  s    
zTargetConfig.demangle)N)r   r   r   r   rq   r   r>   rQ   rT   rP   rF   rX   rY   rZ   r[   rg   r`   rU   r_   r"   bytesrj   rx   r   r   r   r   r   r?      s&   

r?   )	metaclass)r   r   rn   rt   typesr   Z
numba.corer   r   ZThreadLocalStackr   r   r
   r#   r=   rY   r?   r   r   r   r   <module>   s   ""/