o
    4ήc?                     @   s|  d Z ddlZddlmZ dd ZG dd deZG dd	 d	eZG d
d deZG dd deZG dd deZ	G dd deZ
G dd deZdd Zdd Zdd Zdd ZG dd deZG dd deZG d d! d!eZG d"d# d#eZeeefD ]Ze  q}G d$d% d%eZG d&d' d'eZG d(d) d)eZG d*d+ d+eZG d,d- d-eZG d.d/ d/eZG d0d1 d1eZdS )2z
Classes that are LLVM types
    N)_StrCachingc                 C   s   d | ddddS )Nz"{0}"\z\5c"z\22)formatreplace)x r   8/tmp/pip-target-vg8gfxp4/lib/python/llvmlite/ir/types.py	_wrapname
   s   r
   c                   @   sp   e Zd ZdZdZdZdd Zdd Zdd	d
Zdd Z	dddZ
dddZdddZdd Zdd Zdd ZdS )Typez,
    The base class for all LLVM types.
    Fzeroinitializerc                 C   s   dt | t| f S )Nz<%s %s>)typestrselfr   r   r	   __repr__   s   zType.__repr__c                 C      t N)NotImplementedErrorr   r   r   r	   
_to_string      zType._to_stringr   c                 C   s
   t | |S r   )PointerType)r   	addrspacer   r   r	   
as_pointer      
zType.as_pointerc                 C   s
   | |k S r   r   r   otherr   r   r	   __ne__   r   zType.__ne__Nc           	      C   s   ddl m}m} ddlm} |du r| }n||d}||| dd}|t|}||jjW  d   S 1 s:w   Y  dS )z;
        Convert this type object to an LLVM type.
        r   )ModuleGlobalVariable)parse_assemblyN)contextfooname)	llvmlite.irr   r   llvmlite.bindingr    r   get_global_variabler$   r   )	r   target_datar!   r   r   r    mr"   llmodr   r   r	   _get_ll_pointer_type!   s   
$zType._get_ll_pointer_typec                 C      |  ||}||S )zW
        Get the ABI size of this type according to data layout *target_data*.
        )r+   get_pointee_abi_sizer   r(   r!   lltyr   r   r	   get_abi_size0      
zType.get_abi_sizec                 C   r,   )zl
        Get the minimum ABI alignment of this type according to data layout
        *target_data*.
        )r+   get_pointee_abi_alignmentr.   r   r   r	   get_abi_alignment7   s   
zType.get_abi_alignmentc                 C      t |S )zl
        Format constant *value* of this type.  This method may be overriden
        by subclasses.
        r   r   valuer   r   r	   format_constant?   s   zType.format_constantc                 C   s   |S )z
        Wrap constant *value* if necessary.  This method may be overriden
        by subclasses (especially aggregate types).
        r   r6   r   r   r	   wrap_constant_valueF   s   zType.wrap_constant_valuec                 C   s   ddl m} || |S )zR
        Create a LLVM constant of this type with the given Python value.
        r   )Constant)r%   r:   )r   r7   r:   r   r   r	   __call__M   r1   zType.__call__r   r   )__name__
__module____qualname____doc__
is_pointernullr   r   r   r   r+   r0   r3   r8   r9   r;   r   r   r   r	   r      s    



r   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
MetaDataTypec                 C      dS )Nmetadatar   r   r   r   r	   r   W   r   zMetaDataType._to_stringc                 C   r   r   )	TypeErrorr   r   r   r	   r   Z   r   zMetaDataType.as_pointerc                 C   
   t |tS r   )
isinstancerC   r   r   r   r	   __eq__]   r   zMetaDataType.__eq__c                 C      t tS r   )hashrC   r   r   r   r	   __hash__`      zMetaDataType.__hash__N)r=   r>   r?   r   r   rI   rL   r   r   r   r	   rC   U   s
    rC   c                   @      e Zd ZdZdd ZdS )	LabelTypez:
    The label type is the type of e.g. basic blocks.
    c                 C   rD   )Nlabelr   r   r   r   r	   r   i   r   zLabelType._to_stringN)r=   r>   r?   r@   r   r   r   r   r	   rO   d   s    rO   c                   @   sN   e Zd ZdZdZdZdddZdd Zd	d
 Zdd Z	dd Z
edd ZdS )r   z)
    The type of all pointer values.
    TrB   r   c                 C   s   t |trJ || _|| _d S r   )rH   VoidTypepointeer   )r   rR   r   r   r   r	   __init__t   s   
zPointerType.__init__c                 C   s&   | j dkrd| j| j S d| jS )Nr   z{0} addrspace({1})*z{0}*)r   r   rR   r   r   r   r	   r   y   s   
zPointerType._to_stringc                 C   s&   t |tr| j| jf|j|jfkS dS NF)rH   r   rR   r   r   r   r   r	   rI      s
   
zPointerType.__eq__c                 C   rJ   r   )rK   r   r   r   r   r	   rL      rM   zPointerType.__hash__c                 C      t |jtst|j| jS zS
        Resolve the type of the i-th element (for getelementptr lookups).
        )rH   r   IntTyperF   rR   r   ir   r   r	   gep      
zPointerType.gepc                 C   s   d| j | jjf S )Nzp%d%s)r   rR   intrinsic_namer   r   r   r	   r\      s   zPointerType.intrinsic_nameNr<   )r=   r>   r?   r@   rA   rB   rS   r   rI   rL   rZ   propertyr\   r   r   r   r	   r   m   s    
r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	rQ   zI
    The type for empty values (e.g. a function returning no value).
    c                 C   rD   )Nvoidr   r   r   r   r	   r      r   zVoidType._to_stringc                 C   rG   r   )rH   rQ   r   r   r   r	   rI      r   zVoidType.__eq__c                 C   rJ   r   )rK   rQ   r   r   r   r	   rL      rM   zVoidType.__hash__N)r=   r>   r?   r@   r   rI   rL   r   r   r   r	   rQ      s
    rQ   c                   @   s2   e Zd ZdZdddZdd Zdd Zd	d
 ZdS )FunctionTypez!
    The type for functions.
    Fc                 C   s   || _ t|| _|| _d S r   )return_typetupleargsvar_arg)r   r`   rb   rc   r   r   r	   rS      s   

zFunctionType.__init__c                 C   s\   | j rddd | j D }| jrd| j|S d| j|S | jr(d| jS d| jS )N, c                 S      g | ]}t |qS r   r5   ).0ar   r   r	   
<listcomp>       z+FunctionType._to_string.<locals>.<listcomp>z{0} ({1}, ...)z	{0} ({1})z	{0} (...)z{0} ())rb   joinrc   r   r`   )r   strargsr   r   r	   r      s   zFunctionType._to_stringc                 C   s2   t |tr| j|jko| j|jko| j|jkS dS rT   )rH   r_   r`   rb   rc   r   r   r   r	   rI      s   


zFunctionType.__eq__c                 C   rJ   r   )rK   r_   r   r   r   r	   rL      rM   zFunctionType.__hash__NF)r=   r>   r?   r@   rS   r   rI   rL   r   r   r   r	   r_      s    
r_   c                       sx   e Zd ZdZdZi Zdd Ze f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  ZS )rW   z 
    The type for integers.
    0c                 C   sZ   d|  kr
dkr(n nz| j | W S  ty'   | | }| j |< | Y S w | |S )Nr      )_instance_cacheKeyError_IntType__new)clsbitsinstr   r   r	   __new__   s   
zIntType.__new__c                    s0   t |tr	|dksJ tt| | }||_|S Nr   )rH   intsuperrW   ru   width)rr   rs   r   	__class__r   r	   __new   s   zIntType.__newc                 C   s   | j fS r   ry   r   r   r   r	   __getnewargs__   rM   zIntType.__getnewargs__c                 C      | S r   r   r   r   r   r	   __copy__   r   zIntType.__copy__c                 C   s   d| j f S )Nzi%ur}   r   r   r   r	   r         zIntType._to_stringc                 C   s   t |tr| j|jkS dS rT   )rH   rW   ry   r   r   r   r	   rI      s   
zIntType.__eq__c                 C   rJ   r   )rK   rW   r   r   r   r	   rL      rM   zIntType.__hash__c                 C   s   t |trt| S t|S r   )rH   boolr   lowerr   valr   r   r	   r8      s   
zIntType.format_constantc                 C   s   |d u rdS |S rv   r   r   r   r   r	   r9      s   zIntType.wrap_constant_valuec                 C   s   t | S r   r5   r   r   r   r	   r\         zIntType.intrinsic_name)r=   r>   r?   r@   rB   ro   ru   classmethodrq   r~   r   r   rI   rL   r8   r9   r]   r\   __classcell__r   r   rz   r	   rW      s     
rW   c                 C   s   t dt d| d S )z-
    Truncate to single-precision float.
    fr   )structunpackpackr7   r   r   r	   	_as_float   s   r   c                 C   s8   zt dt d| d W S  t jy   t|  Y S w )z+
    Truncate to half-precision float.
    er   )r   r   r   errorr   r   r   r   r	   _as_half  s
   r   c                 C   s4   t |t| }t ||d }d||}|S )Nr   z{{0:#{0}x}})r   r   floatr   r   )r7   packfmt	unpackfmt	numdigitsrawintrepoutr   r   r	   _format_float_as_hex  s   r   c                 C   s   t | dddS )za
    Format *value* as a hexadecimal string of its IEEE double precision
    representation.
    dQ   )r   r   r   r   r	   _format_double  s   r   c                       s8   e Zd Zdd Zdd Zdd Ze fddZ  ZS )	_BaseFloatTypec                 C      | j S r   )ro   rr   r   r   r	   ru   #     z_BaseFloatType.__new__c                 C   s   t |t| S r   )rH   r   r   r   r   r	   rI   &  s   z_BaseFloatType.__eq__c                 C   s   t t| S r   )rK   r   r   r   r   r	   rL   )  r   z_BaseFloatType.__hash__c                    s   t t| | | _d S r   )rx   r   ru   ro   r   rz   r   r	   _create_instance,  s   z_BaseFloatType._create_instance)	r=   r>   r?   ru   rI   rL   r   r   r   r   r   rz   r	   r   !  s    r   c                   @   (   e Zd ZdZdZdZdd Zdd ZdS )	HalfType/
    The type for single-precision floats.
    0.0f16c                 C   rD   )Nhalfr   r   r   r   r	   __str__8  r   zHalfType.__str__c                 C      t t|S r   )r   r   r6   r   r   r	   r8   ;  r   zHalfType.format_constantNr=   r>   r?   r@   rB   r\   r   r8   r   r   r   r	   r   1      r   c                   @   r   )		FloatTyper   r   f32c                 C   rD   )Nr   r   r   r   r   r	   r   F  r   zFloatType.__str__c                 C   r   r   )r   r   r6   r   r   r	   r8   I  r   zFloatType.format_constantNr   r   r   r   r	   r   ?  r   r   c                   @   r   )	
DoubleTypez/
    The type for double-precision floats.
    r   f64c                 C   rD   )Ndoubler   r   r   r   r	   r   T  r   zDoubleType.__str__c                 C   r4   r   )r   r6   r   r   r	   r8   W  rM   zDoubleType.format_constantNr   r   r   r   r	   r   M  r   r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )_Repeatc                 C      || _ || _d S r   )r7   size)r   r7   r   r   r   r	   rS   `     
z_Repeat.__init__c                 C   r   r   )r   r   r   r   r	   __len__d  r   z_Repeat.__len__c                 C   s,   d|  kr| j k r| jS  t|t|rv   )r   r7   
IndexError)r   itemr   r   r	   __getitem__g  s   z_Repeat.__getitem__N)r=   r>   r?   rS   r   r   r   r   r   r	   r   _  s    r   c                   @   s\   e Zd ZdZdd Ze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S )
VectorTypezJ
    The type for vectors of primitive data items (e.g. "<f32 x 4>").
    c                 C   r   r   elementcountr   r   r   r   r   r	   rS   s  r   zVectorType.__init__c                 C      t | j| jS r   r   r   r   r   r   r   r	   elementsw     zVectorType.elementsc                 C   r   r   r   r   r   r   r	   r   {  r   zVectorType.__len__c                 C      d| j | jf S )Nz	<%d x %s>r   r   r   r   r   r	   r   ~     zVectorType._to_stringc                 C   &   t |tr| j|jko| j|jkS d S r   )rH   r   r   r   r   r   r   r	   rI        
zVectorType.__eq__c                 C   rJ   r   )rK   r   r   r   r   r	   rL     r   zVectorType.__hash__c                 C   r   r   r   r   r   r   r	   r     r   zVectorType.__copy__c                 C      d dd |D }d|S )Nrd   c                 S      g | ]}d  |j| qS z{0} {1}r   r   get_referencerf   r   r   r   r	   rh         z.VectorType.format_constant.<locals>.<listcomp>z<{0}>rj   r   r   r7   
itemstringr   r   r	   r8        

zVectorType.format_constantc                    s   ddl mm  t|ttfs4t| r*|j| jkr$td	| j|j|f| j
 S  | j|f| j
 S t|t| krFtd| t|f  fddt| j|D S )N   Valuer:   zexpected {} for {}+wrong constant size for %s: got %d elementsc                    (   g | ]\}}t |s ||n|qS r   rH   rf   tyr   r:   r   r   r	   rh         z2VectorType.wrap_constant_value.<locals>.<listcomp>) r   r:   rH   listra   r   r   rF   r   r   len
ValueErrorzipr   r   valuesr   r   r	   r9     s    


zVectorType.wrap_constant_valueN)r=   r>   r?   r@   rS   r]   r   r   r   rI   rL   r   r8   r9   r   r   r   r	   r   n  s    
r   c                   @   rN   )	Aggregatez_
    Base class for aggregate types.
    See http://llvm.org/docs/LangRef.html#t-aggregate
    c                    sb   ddl mm  t|ttfs|S t|t| kr#td| t|f  fddt| j	|D S )Nr   r   r   c                    r   r   r   r   r   r   r	   rh     r   z1Aggregate.wrap_constant_value.<locals>.<listcomp>)
r   r   r:   rH   r   ra   r   r   r   r   r   r   r   r	   r9     s   

zAggregate.wrap_constant_valueN)r=   r>   r?   r@   r9   r   r   r   r	   r     s    r   c                   @   sT   e Zd ZdZdd Zedd Zdd Zdd	 Zd
d Z	dd Z
dd Zdd ZdS )	ArrayTypezG
    The type for fixed-size homogenous arrays (e.g. "[f32 x 3]").
    c                 C   r   r   r   r   r   r   r	   rS     r   zArrayType.__init__c                 C   r   r   r   r   r   r   r	   r     r   zArrayType.elementsc                 C   r   r   r   r   r   r   r	   r     r   zArrayType.__len__c                 C   r   )Nz	[%d x %s]r   r   r   r   r	   r     r   zArrayType._to_stringc                 C   r   r   )rH   r   r   r   r   r   r   r	   rI     r   zArrayType.__eq__c                 C   rJ   r   )rK   r   r   r   r   r	   rL     rM   zArrayType.__hash__c                 C   rU   rV   )rH   r   rW   rF   r   rX   r   r   r	   rZ     r[   zArrayType.gepc                 C   r   )Nrd   c                 S   r   r   r   r   r   r   r	   rh     r   z-ArrayType.format_constant.<locals>.<listcomp>z[{0}]r   r   r   r   r	   r8     r   zArrayType.format_constantN)r=   r>   r?   r@   rS   r]   r   r   r   rI   rL   rZ   r8   r   r   r   r	   r     s    
r   c                   @   sj   e Zd ZdZdZedd Zejdd Zdd Zdd	 Z	ed
d Z
dd Zdd Zdd Zdd ZdS )BaseStructTypez6
    The base type for heterogenous struct types.
    Fc                 C   r   )zf
        A boolean attribute that indicates whether the structure uses
        packed layout.
        )_packedr   r   r   r	   packed  s   zBaseStructType.packedc                 C   s   t || _d S r   )r   r   r   r   r   r	   r     r   c                 C      | j d usJ t| j S r   )r   r   r   r   r   r	   r        
zBaseStructType.__len__c                 C   r   r   )r   iterr   r   r   r	   __iter__  r   zBaseStructType.__iter__c                 C   s
   | j d u S r   )r   r   r   r   r	   	is_opaque  s   
zBaseStructType.is_opaquec                 C   s$   dd dd | jD  }| |S )zE
        Return the LLVM IR for the structure representation
        z{%s}rd   c                 S   re   r   r5   r   r   r   r	   rh     ri   z1BaseStructType.structure_repr.<locals>.<listcomp>)rj   r   _wrap_packed)r   retr   r   r	   structure_repr  s   
zBaseStructType.structure_reprc                 C   s(   d dd |D }d|}| |S )Nrd   c                 S   r   r   r   r   r   r   r	   rh     r   z2BaseStructType.format_constant.<locals>.<listcomp>z{{{0}}})rj   r   r   )r   r7   r   r   r   r   r	   r8      s
   


zBaseStructType.format_constantc                 C   s"   t |jtst|j| j|j S )z
        Resolve the type of the i-th element (for getelementptr lookups).

        *i* needs to be a LLVM constant, so that the type can be determined
        at compile-time.
        )rH   r   rW   rF   r   constantrX   r   r   r	   rZ     s   
zBaseStructType.gepc                 C   s   | j rd|S |S )zX
        Internal helper to wrap textual repr of struct type into packed struct
        z<{}>)r   r   )r   textreprr   r   r	   r     s   
zBaseStructType._wrap_packedN)r=   r>   r?   r@   r   r]   r   setterr   r   r   r   r8   rZ   r   r   r   r   r	   r     s    


r   c                   @   s6   e Zd ZdZdZdddZdd Zdd	 Zd
d ZdS )LiteralStructTypez
    The type of "literal" structs, i.e. structs with a literally-defined
    type (by contrast with IdentifiedStructType).
    r   Fc                 C   s   t || _|| _dS )z{
        *elems* is a sequence of types to be used as members.
        *packed* controls the use of packed layout.
        N)ra   r   r   )r   elemsr   r   r   r	   rS   #  s   

zLiteralStructType.__init__c                 C   s   |   S r   )r   r   r   r   r	   r   +  rM   zLiteralStructType._to_stringc                 C      t |tr| j|jkS d S r   )rH   r   r   r   r   r   r	   rI   .     
zLiteralStructType.__eq__c                 C   rJ   r   )rK   r   r   r   r   r	   rL   2  rM   zLiteralStructType.__hash__Nrl   )	r=   r>   r?   r@   rB   rS   r   rI   rL   r   r   r   r	   r     s    
r   c                   @   sF   e Zd ZdZdZdddZdd Zdd	 Zd
d Zdd Z	dd Z
dS )IdentifiedStructTypez
    A type which is a named alias for another struct type, akin to a typedef.
    While literal struct types can be structurally equal (see
    LiteralStructType), identified struct types are compared by name.

    Do not use this directly.
    r   Fc                 C   s$   |sJ || _ || _d| _|| _dS )z
        *context* is a llvmlite.ir.Context.
        *name* is the identifier for the new struct type.
        *packed* controls the use of packed layout.
        N)r!   r$   r   r   )r   r!   r$   r   r   r   r	   rS   @  s
   
zIdentifiedStructType.__init__c                 C   s   dj t| jdS )Nz%{name}r#   )r   r
   r$   r   r   r   r	   r   L  s   zIdentifiedStructType._to_stringc                 C   s4   | j rdjt| d}|S djt| |  d}|S )zD
        Returns the string for the declaration of the type
        z{strrep} = type opaque)strrepz{strrep} = type {struct})r   r   )r   r   r   r   )r   r   r   r   r	   get_declarationO  s   z$IdentifiedStructType.get_declarationc                 C   r   r   )rH   r   r$   r   r   r   r	   rI   Z  r   zIdentifiedStructType.__eq__c                 C   rJ   r   )rK   r   r   r   r   r	   rL   ^  rM   zIdentifiedStructType.__hash__c                 G   s&   | j stdj| jdt|| _d S )Nz{name} is already definedr#   )r   RuntimeErrorr   r$   ra   r   )r   r   r   r   r	   set_bodya  s
   zIdentifiedStructType.set_bodyNrl   )r=   r>   r?   r@   rB   rS   r   r   rI   rL   r   r   r   r   r	   r   6  s    
r   )r@   r   llvmlite.ir._utilsr   r
   r   rC   rO   r   rQ   r_   rW   r   r   r   r   r   r   r   r   _clsr   objectr   r   r   r   r   r   r   r   r   r   r	   <module>   s8    G	)!:
3(@