3 M��Z0T�@s�ddlZddlZddlmZddlmZmZmZdZdZ dZ dd�Z Gd d �d e �Z Gd d �d e �ZGd d�de�Ze�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�Zee�Zdd �Zee�ZGd!d"�d"e�ZGd#d$�d$e�Zeed%�d�ZGd&d'�d'e �ZGd(d)�d)e�Z Gd*d+�d+e �Z!Gd,d-�d-e �Z"Gd.d/�d/e�Z#d�N�)� allocate_lock)� CDefError�VerificationError�VerificationMissing��cCs@|t@rd|j�}|t@r(d|j�}|t@r)r)rrrr�__repr__AszBaseTypeByIdentity.__repr__cs�fdd��jD�S)Ncsg|]}|t�|�f�qSr)�getattr)�.0�name)rrr� Esz1BaseTypeByIdentity._get_items..)�_attrs_)rr)rr� _get_itemsDszBaseTypeByIdentity._get_itemsN)rrr)F) �__name__� __module__� __qualname__� is_array_type�is_raw_functionr!rr"r#r,r-r3rrrrrs  rc@s$eZdZdd�Zdd�Zdd�ZdS)�BaseTypecCs|j|jko|j�|j�kS)N)� __class__r3)r�otherrrr�__eq__Js zBaseType.__eq__cCs ||k S)Nr)rr;rrr�__ne__NszBaseType.__ne__cCst|jt|j��f�S)N)�hashr:�tupler3)rrrr�__hash__QszBaseType.__hash__N)r4r5r6r<r=r@rrrrr9Hsr9c@s eZdZfZdd�Zdd�ZdS)�VoidTypecCs d|_dS)Nzvoid&)r)rrrr�__init__XszVoidType.__init__cCs t||d�S)NZ new_void_type)� global_cache)rr(r)rrrr&[szVoidType.build_backend_typeN)r4r5r6r2rBr&rrrrrAUsrAc@seZdZdd�ZdS)�BasePrimitiveTypecCsdS)NFr)rrrr�is_complex_typebsz!BasePrimitiveType.is_complex_typeN)r4r5r6rErrrrrDasrDc4@s�eZdZdZdddddddddddddddddddddddddddddddddddddddddddddddddddd�3Zdd�Zd d �Zd d �Zd d�Zdd�Z dd�Z dS)� PrimitiveTyper0�c�i�f�j)3�charZshort�int�longz long longz signed charz unsigned charzunsigned shortz unsigned intz unsigned longzunsigned long long�floatZdoublez long doublezfloat _Complexzdouble _ComplexZ_BoolZwchar_tZchar16_tZchar32_tZint8_tZuint8_tZint16_tZuint16_tZint32_tZuint32_tZint64_tZuint64_tZ int_least8_tZ uint_least8_tZ int_least16_tZuint_least16_tZ int_least32_tZuint_least32_tZ int_least64_tZuint_least64_tZ int_fast8_tZ uint_fast8_tZ int_fast16_tZ uint_fast16_tZ int_fast32_tZ uint_fast32_tZ int_fast64_tZ uint_fast64_tZintptr_tZ uintptr_tZintmax_tZ uintmax_tZ ptrdiff_tZsize_tZssize_tcCs||_|d|_dS)Nr)r0r)rr0rrrrB�szPrimitiveType.__init__cCs|j|jdkS)NrG)�ALL_PRIMITIVE_TYPESr0)rrrr� is_char_type�szPrimitiveType.is_char_typecCs|j|jdkS)NrH)rOr0)rrrrr#�szPrimitiveType.is_integer_typecCs|j|jdkS)NrI)rOr0)rrrr� is_float_type�szPrimitiveType.is_float_typecCs|j|jdkS)NrJ)rOr0)rrrrrE�szPrimitiveType.is_complex_typecCst||d|j�S)NZnew_primitive_type)rCr0)rr(r)rrrr&�sz PrimitiveType.build_backend_typeN)r0) r4r5r6r2rOrBrPr#rQrEr&rrrrrFfstrFc@s(eZdZd Zdd�Zdd�Zdd�ZdS) �UnknownIntegerTyper0cCs||_|d|_dS)Nr)r0r)rr0rrrrB�szUnknownIntegerType.__init__cCsdS)NTr)rrrrr#�sz"UnknownIntegerType.is_integer_typecCstd|j��dS)Nz4integer type '%s' can only be used after compilation)�NotImplementedErrorr0)rr(r)rrrr&�sz%UnknownIntegerType.build_backend_typeN)r0)r4r5r6r2rBr#r&rrrrrR�srRc@s eZdZdZdd�Zdd�ZdS)�UnknownFloatTyper0cCs||_|d|_dS)Nr)r0r)rr0rrrrB�szUnknownFloatType.__init__cCstd|j��dS)Nz2float type '%s' can only be used after compilation)rSr0)rr(r)rrrr&�sz#UnknownFloatType.build_backend_typeN)r0)r4r5r6r2rBr&rrrrrT�srTc@seZdZdZd dd�ZdS) �BaseFunctionType�argsr �ellipsis�abiNcCs�||_||_||_||_dd�|jD�}|jr8|jd�|p@dg}|jdj|�f}|dk r||dd�|d|dd�}|jjjd|�|_dS) NcSsg|] }|j��qSr)r)r/�argrrrr1�sz-BaseFunctionType.__init__..z...�voidz, rrr) rVr rWrX�append� _base_pattern�joinrr)rrVr rWrXZreprargsrrrrrB�s   zBaseFunctionType.__init__)rVr rWrX)N)r4r5r6r2rBrrrrrU�srUc@s$eZdZdZdZdd�Zdd�ZdS)�RawFunctionTypez(&)(%s)TcCstd|f��dS)NzPcannot render the type %r: it is a function type, not a pointer-to-function type)r)rr(r)rrrr&�sz"RawFunctionType.build_backend_typecCst|j|j|j|j�S)N)�FunctionPtrTyperVr rWrX)rrrr�as_function_pointer�sz#RawFunctionType.as_function_pointerN)r4r5r6r\r8r&r`rrrrr^�sr^c@s eZdZdZdd�Zdd�ZdS)r_z(*&)(%s)cCs�|jj||�}g}x |jD]}|j|j||��qWf}|jdkrl|jsly|jjf}Wntk rjYnXt ||dt |�||jf|��S)NZ __stdcallZnew_function_type) r r,rVr[rXrW�_backendZ FFI_STDCALL�AttributeErrorrCr?)rr(r)r rV�tpZabi_argsrrrr&�s  z"FunctionPtrType.build_backend_typecCst|j|j|j|j�S)N)r^rVr rWrX)rrrr�as_raw_functionszFunctionPtrType.as_raw_functionN)r4r5r6r\r&rdrrrrr_�sr_c@s"eZdZd Zd dd�Zdd�ZdS) � PointerType�totyper rcCs>||_||_t|d�}|jr*d|j�f}|jjd|�|_dS)Nz *&z(%s)r)rfr rr7r rr)rrfr ZextrarrrrBs  zPointerType.__init__cCs |jj||dd�}t||d|�S)NT)r*�new_pointer_type)rfr,rC)rr(r)ZBItemrrrr&szPointerType.build_backend_typeN)rfr )r)r4r5r6r2rBr&rrrrres recCs t|t�S)N)rer )rfrrr�ConstPointerTypesrhc@seZdZdZddd�ZdS) �NamedPointerTyperfr0rcCs"tj|||�||_|d|_dS)Nr)rerBr0r)rrfr0r rrrrBszNamedPointerType.__init__N)rfr0)r)r4r5r6r2rBrrrrrisric@s,eZdZd ZdZdd�Zdd�Zdd �Zd S) � ArrayType�item�lengthTcCsF||_||_|dkrd}n|dkr(d}nd|}|jjjd|�|_dS)Nz&[]z...z &[/*...*/]z&[%s]r)rkrlrr)rrkrlZbracketsrrrrB)szArrayType.__init__cCs t|j|�S)N)rjrk)rZ newlengthrrr�resolve_length6szArrayType.resolve_lengthcCsJ|jdkrtd|f��|jj||�t|j�j||�}t||d||j�S)Nz...z)cannot render the type %r: unknown lengthZnew_array_type)rlrrkr,rerC)rr(r)ZBPtrItemrrrr&9s   zArrayType.build_backend_typeN)rkrl)r4r5r6r2r7rBrmr&rrrrrj%s  rjrKc@s,eZdZd ZdZdd�Zdd�Zdd�ZdS) �StructOrUnionOrEnumr0NcCs$|jpd|j|jf}|d|_dS)Nz%s %sr)� forcename�kindr0r)rr0rrr�build_c_name_with_markerHsz,StructOrUnionOrEnum.build_c_name_with_markercCs||_|j�dS)N)rorq)rrorrr�force_the_nameLsz"StructOrUnionOrEnum.force_the_namecCs|jdd�S)Nr�����)r)rrrr�get_official_namePsz%StructOrUnionOrEnum.get_official_name)r0)r4r5r6r2rorqrrrtrrrrrnDs rnc@sjeZdZdZdZdZdZddd�Zdd�Zdd d �Z d d �Z dd d�Z dd�Z dd�Z dd�Zdd�ZdS)� StructOrUnionNrFcCs*||_||_||_||_||_|j�dS)N)r0�fldnames�fldtypes� fldbitsize�fldqualsrq)rr0rvrwrxryrrrrB[s zStructOrUnion.__init__ccsB|jdk r>x2t|j|j�D] \}}|dkrt|t�r|VqWdS)Nr)rw�ziprv� isinstanceru)rr0�typerrr�anonymous_struct_fieldscs z%StructOrUnion.anonymous_struct_fieldsTccs�|j}|dkrdt|j�}xbt|j|j|j|�D]J\}}}}|dkrnt|t�rn|rnx&|j�D] }|Vq^Wq2||||fVq2WdS)Nrr)r) ry�lenrvrzrwrxr{ru� enumfields)rZexpand_anonymous_struct_unionryr0r|�bitsizer r rrrris  zStructOrUnion.enumfieldsc Cs~g}g}g}g}x@|j�D]4\}}}}|j|�|j|�|j|�|j|�qWt|�|_t|�|_t|�|_t|�|_dS)N)rr[r?rvrwrxry) r�names�typesZbitsizesryr0r|r�r rrr� force_flattenws      zStructOrUnion.force_flattencCs$tj||||�}|s |j||�|S)N)rnr,�finish_backend_type)rr(r)r*r+rrrr,�s   zStructOrUnion.get_cached_btypecs|jr$|jdkr td|jf��dS�j|}d|_|jdkrB�n�|jdkr���fdd�|jD�}tt|j||j ��}d}|j r�d}�j j |||d d |��nfg}|j\}}} } �x&t t|j��D�]} || } |j| } t| t�o�| jdk�rn| jj���}t| �j|��\}}|dk�r<|jd |j|j| �p4d f�| j|�} |jd| �| f|j| dd�|_| j���}t| t��r�| jdk�r�n8�j|�}|| k�r�|jd |j|j| �p�d || f�|j|�q�Wtt|j||j |��}�j j |||| | �d|_dS)Nrz(recursive structure declaration for '%s'rcsg|]}|j����qSr)r,)r/rc)r(r)rrr1�sz5StructOrUnion.finish_backend_type..r�z...zfield '%s.%s' has a bogus size?z{}z=field '%s.%s' is declared as %d bytes, but is really %d bytesrsrs)� completedrSr0r$rw� fixedlayout�listrzrvrx�packedraZcomplete_struct_or_union�ranger~r{rjrlrkr,�divmod�sizeof�_verification_errorrmr[)rr(r)r+rwZlstZsflagsZfieldofsZ fieldsizeZ totalsizeZtotalalignmentrHZfsizeZftypeZ BItemTypeZnlenZnrestZ BFieldTypeZ bitemsizer)r(r)rr��s`                z!StructOrUnion.finish_backend_typecCs t|��dS)N)r)r�msgrrrr��sz!StructOrUnion._verification_errorcCs |jr|jdkrt|j���dS)N)�partialr�rr)rrrr�check_not_partial�szStructOrUnion.check_not_partialcCs.|j�|j|�t||d|j|j�|d�S)Nz new_%s_type)�key)r�r[rCrprt)rr(r)rrrr&�s z StructOrUnion.build_backend_type)N)T)F)r4r5r6r�r�r�r�rBr}rr�r,r�r�r�r&rrrrruUs   <ruc@seZdZdZdS)� StructType�structN)r4r5r6rprrrrr��sr�c@seZdZdZdS)� UnionType�unionN)r4r5r6rprrrrr��sr�c@sBeZdZdZdZdZddd�Zdd�Zdd �Zd d �Z d d �Z dS)�EnumType�enumFNcCs$||_||_||_||_|j�dS)N)r0� enumerators� enumvalues� baseinttyperq)rr0r�r�r�rrrrB�s zEnumType.__init__cCs4tj||�|jdkr0|j�}d|jdd�|_dS)Nrr�_)rnrrrortr)rror0rrrrr�s  zEnumType.force_the_namecCs|jr|j rt|j���dS)N)r��partial_resolvedrr)rrrrr��szEnumType.check_not_partialc Cs4|j�|j||�}t||d|j�|j|j||d�S)NZ new_enum_type)r�)r��build_baseinttyperCrtr�r�)rr(r)Z base_btyperrrr&�s  zEnumType.build_backend_typec CsH|jdk r|jj||�S|jr4t|j�}t|j�}nDddl}y tj�Wntk r\YnX|j d|j ��d}}|dkr�d}t d�}t d�}nd}t d�}t d�}|j||�} |j||�} |j | �} |j | �} |d d| d>ko�|dd| |>k�r| S|d d| d>k�r4|dd| |>k�r4| St d |j ���dS) NrzU%r has no values explicitly defined; guessing that it is equivalent to 'unsigned int'rrLrMz unsigned intz unsigned longr�z=%s values don't all fit into either 'long' or 'unsigned long'rsrs)r�r,r��min�max�warningsZ__warningregistry__�clear� NameError�warnrrFr�r) rr(r)Zsmallest_valueZ largest_valuer�ZsignZ candidate1Z candidate2Zbtype1Zbtype2Zsize1Zsize2rrrr�s@          zEnumType.build_baseinttype)N) r4r5r6rpr�r�rBrrr�r&r�rrrrr��s r�cCs2|dkrd|}t|ddd�}|j|�d|_|S)Nz$%s� unknown_type)r�rr�origin)r0� structnamercrrrr�*s  r�cCs(|dkrd|}t|ddd�}t||�S)Nz$$%s)r�ri)r0r�rcrrr�unknown_ptr_type2sr�c CsFt|tj�rtSt�(tt|�d�s2tj�t|�_ t|�j SQRXdS)N� __typecache) r{r�� ModuleType�_typecache_cffi_backend� global_lock�hasattrr|�weakref�WeakValueDictionaryr�)Zbackendrrr� get_typecache<s  r�c Os�|jd||f�}y |j|Stk r.YnXyt|j|�|�}Wn4tk rx}ztd|||f��WYdd}~XnX|j}t�(|j|�} | dkr�|||<|S| SWdQRXdS)Nr�z %s: %r: %s)�popZ _typecacher%r.rarSr��get) Zsrctyper(�funcnamerV�kwdsr��res�e�cacheZres1rrrrCGs  $ rCcCstd|d|�S)N�?rg)rC)r(r+rrr� pointer_cache^sr�cCs@|jrsN  /  L  G