3 MZ0T@sddlZddlZddlmZddlmZmZmZdZdZ dZ ddZ Gd d d e Z Gd d d e ZGd ddeZeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZeeZdd ZeeZGd!d"d"eZGd#d$d$eZeed%dZGd&d'd'e ZGd(d)d)eZ Gd*d+d+e Z!Gd,d-d-e Z"Gd.d/d/eZ#dN) allocate_lock) CDefErrorVerificationErrorVerificationMissingcCs@|t@rd|j}|t@r(d|j}|t@r)r)rrrr__repr__AszBaseTypeByIdentity.__repr__csfddjDS)Ncsg|]}|t|fqSr)getattr).0name)rrr Esz1BaseTypeByIdentity._get_items..)_attrs_)rr)rr _get_itemsDszBaseTypeByIdentity._get_itemsN)rrr)F) __name__ __module__ __qualname__ is_array_typeis_raw_functionr!rr"r#r,r-r3rrrrrs  rc@s$eZdZddZddZddZdS)BaseTypecCs|j|jko|j|jkS)N) __class__r3)rotherrrr__eq__Js zBaseType.__eq__cCs ||k S)Nr)rr;rrr__ne__NszBaseType.__ne__cCst|jt|jfS)N)hashr:tupler3)rrrr__hash__QszBaseType.__hash__N)r4r5r6r<r=r@rrrrr9Hsr9c@s eZdZfZddZddZdS)VoidTypecCs d|_dS)Nzvoid&)r)rrrr__init__XszVoidType.__init__cCs t||dS)NZ new_void_type) global_cache)rr(r)rrrr&[szVoidType.build_backend_typeN)r4r5r6r2rBr&rrrrrAUsrAc@seZdZddZdS)BasePrimitiveTypecCsdS)NFr)rrrris_complex_typebsz!BasePrimitiveType.is_complex_typeN)r4r5r6rErrrrrDasrDc4@seZdZdZdddddddddddddddddddddddddddddddddddddddddddddddddddd3ZddZd d Zd d Zd dZddZ ddZ dS) PrimitiveTyper0cifj)3charZshortintlongz long longz signed charz unsigned charzunsigned shortz unsigned intz unsigned longzunsigned long longfloatZdoublez 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)rr0rrrrBszPrimitiveType.__init__cCs|j|jdkS)NrG)ALL_PRIMITIVE_TYPESr0)rrrr is_char_typeszPrimitiveType.is_char_typecCs|j|jdkS)NrH)rOr0)rrrrr#szPrimitiveType.is_integer_typecCs|j|jdkS)NrI)rOr0)rrrr is_float_typeszPrimitiveType.is_float_typecCs|j|jdkS)NrJ)rOr0)rrrrrEszPrimitiveType.is_complex_typecCst||d|jS)NZnew_primitive_type)rCr0)rr(r)rrrr&sz PrimitiveType.build_backend_typeN)r0) r4r5r6r2rOrBrPr#rQrEr&rrrrrFfstrFc@s(eZdZd ZddZddZddZdS) UnknownIntegerTyper0cCs||_|d|_dS)Nr)r0r)rr0rrrrBszUnknownIntegerType.__init__cCsdS)NTr)rrrrr#sz"UnknownIntegerType.is_integer_typecCstd|jdS)Nz4integer type '%s' can only be used after compilation)NotImplementedErrorr0)rr(r)rrrr&sz%UnknownIntegerType.build_backend_typeN)r0)r4r5r6r2rBr#r&rrrrrRsrRc@s eZdZdZddZddZdS)UnknownFloatTyper0cCs||_|d|_dS)Nr)r0r)rr0rrrrBszUnknownFloatType.__init__cCstd|jdS)Nz2float type '%s' can only be used after compilation)rSr0)rr(r)rrrr&sz#UnknownFloatType.build_backend_typeN)r0)r4r5r6r2rBr&rrrrrTsrTc@seZdZdZd ddZdS) BaseFunctionTypeargsr ellipsisabiNcCs||_||_||_||_dd|jD}|jr8|jd|p@dg}|jdj|f}|dk r||dd|d|dd}|jjjd||_dS) NcSsg|] }|jqSr)r)r/argrrrr1sz-BaseFunctionType.__init__..z...voidz, rrr) rVr rWrXappend _base_patternjoinrr)rrVr rWrXZreprargsrrrrrBs   zBaseFunctionType.__init__)rVr rWrX)N)r4r5r6r2rBrrrrrUsrUc@s$eZdZdZdZddZddZdS)RawFunctionTypez(&)(%s)TcCstd|fdS)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|jS)N)FunctionPtrTyperVr rWrX)rrrras_function_pointersz#RawFunctionType.as_function_pointerN)r4r5r6r\r8r&r`rrrrr^sr^c@s eZdZdZddZddZdS)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_STDCALLAttributeErrorrCr?)rr(r)r rVtpZabi_argsrrrr&s  z"FunctionPtrType.build_backend_typecCst|j|j|j|jS)N)r^rVr rWrX)rrrras_raw_functionszFunctionPtrType.as_raw_functionN)r4r5r6r\r&rdrrrrr_sr_c@s"eZdZd Zd ddZddZdS) PointerTypetotyper rcCs>||_||_t|d}|jr*d|jf}|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|tS)N)rer )rfrrrConstPointerTypesrhc@seZdZdZdddZdS) NamedPointerTyperfr0rcCs"tj|||||_|d|_dS)Nr)rerBr0r)rrfr0r rrrrBszNamedPointerType.__init__N)rfr0)r)r4r5r6r2rBrrrrrisric@s,eZdZd ZdZddZddZdd Zd S) ArrayTypeitemlengthTcCsF||_||_|dkrd}n|dkr(d}nd|}|jjjd||_dS)Nz&[]z...z &[/*...*/]z&[%s]r)rkrlrr)rrkrlZbracketsrrrrB)szArrayType.__init__cCs t|j|S)N)rjrk)rZ newlengthrrrresolve_length6szArrayType.resolve_lengthcCsJ|jdkrtd|f|jj||t|jj||}t||d||jS)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 ZdZddZddZddZdS) StructOrUnionOrEnumr0NcCs$|jpd|j|jf}|d|_dS)Nz%s %sr) forcenamekindr0r)rr0rrrbuild_c_name_with_markerHsz,StructOrUnionOrEnum.build_c_name_with_markercCs||_|jdS)N)rorq)rrorrrforce_the_nameLsz"StructOrUnionOrEnum.force_the_namecCs|jddS)Nr)r)rrrrget_official_namePsz%StructOrUnionOrEnum.get_official_name)r0)r4r5r6r2rorqrrrtrrrrrnDs rnc@sjeZdZdZdZdZdZdddZddZdd d Z d d Z dd dZ ddZ ddZ ddZddZdS) StructOrUnionNrFcCs*||_||_||_||_||_|jdS)N)r0fldnamesfldtypes fldbitsizefldqualsrq)rr0rvrwrxryrrrrB[s zStructOrUnion.__init__ccsB|jdk r>x2t|j|jD] \}}|dkrt|tr|VqWdS)Nr)rwziprv isinstanceru)rr0typerrranonymous_struct_fieldscs z%StructOrUnion.anonymous_struct_fieldsTccs|j}|dkrdt|j}xbt|j|j|j|D]J\}}}}|dkrnt|trn|rnx&|jD] }|Vq^Wq2||||fVq2WdS)Nrr)r) rylenrvrzrwrxr{ru enumfields)rZexpand_anonymous_struct_unionryr0r|bitsizer r rrrris  zStructOrUnion.enumfieldsc Cs~g}g}g}g}x@|jD]4\}}}}|j||j||j||j|qWt||_t||_t||_t||_dS)N)rr[r?rvrwrxry) rnamestypesZbitsizesryr0r|rr 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|jfdSj|}d|_|jdkrBn|jdkrfdd|jD}tt|j||j }d}|j rd}j j |||d d |nfg}|j\}}} } x&t t|jD]} || } |j| } t| to| jdkrn| jj}t| j|\}}|dkr<|jd |j|j| p4d f| j|} |jd| | f|j| dd|_| j}t| tr| jdkrn8j|}|| kr|jd |j|j| pd || f|j|qWtt|j||j |}j j |||| | d|_dS)Nrz(recursive structure declaration for '%s'rcsg|]}|jqSr)r,)r/rc)r(r)rrr1sz5StructOrUnion.finish_backend_type..rz...zfield '%s.%s' has a bogus size?z{}z=field '%s.%s' is declared as %d bytes, but is really %d bytesrsrs) completedrSr0r$rw fixedlayoutlistrzrvrxpackedraZcomplete_struct_or_unionranger~r{rjrlrkr,divmodsizeof_verification_errorrmr[)rr(r)r+rwZlstZsflagsZfieldofsZ fieldsizeZ totalsizeZtotalalignmentrHZfsizeZftypeZ BItemTypeZnlenZnrestZ BFieldTypeZ bitemsizer)r(r)rrs`                z!StructOrUnion.finish_backend_typecCs t|dS)N)r)rmsgrrrrsz!StructOrUnion._verification_errorcCs |jr|jdkrt|jdS)N)partialrrr)rrrrcheck_not_partialszStructOrUnion.check_not_partialcCs.|j|j|t||d|j|j|dS)Nz new_%s_type)key)rr[rCrprt)rr(r)rrrr&s z StructOrUnion.build_backend_type)N)T)F)r4r5r6rrrrrBr}rrr,rrrr&rrrrruUs   <ruc@seZdZdZdS) StructTypestructN)r4r5r6rprrrrrsrc@seZdZdZdS) UnionTypeunionN)r4r5r6rprrrrrsrc@sBeZdZdZdZdZdddZddZdd Zd d Z d d Z dS)EnumTypeenumFNcCs$||_||_||_||_|jdS)N)r0 enumerators enumvalues baseinttyperq)rr0rrrrrrrBs zEnumType.__init__cCs4tj|||jdkr0|j}d|jdd|_dS)Nrr_)rnrrrortr)rror0rrrrrs  zEnumType.force_the_namecCs|jr|j rt|jdS)N)rpartial_resolvedrr)rrrrrszEnumType.check_not_partialc Cs4|j|j||}t||d|j|j|j||dS)NZ new_enum_type)r)rbuild_baseinttyperCrtrr)rr(r)Z base_btyperrrr&s  zEnumType.build_backend_typec CsH|jdk r|jj||S|jr4t|j}t|j}nDddl}y tjWntk r\YnX|j d|j d}}|dkrd}t d}t d}nd}t d}t d}|j||} |j||} |j | } |j | } |d d| d>ko|dd| |>kr| S|d d| d>kr4|dd| |>kr4| St d |j dS) NrzU%r has no values explicitly defined; guessing that it is equivalent to 'unsigned int'rrLrMz unsigned intz unsigned longrz=%s values don't all fit into either 'long' or 'unsigned long'rsrs)rr,rminmaxwarningsZ__warningregistry__clear NameErrorwarnrrFrr) rr(r)Zsmallest_valueZ largest_valuerZsignZ candidate1Z candidate2Zbtype1Zbtype2Zsize1Zsize2rrrrs@          zEnumType.build_baseinttype)N) r4r5r6rprrrBrrrr&rrrrrrs rcCs2|dkrd|}t|ddd}|j|d|_|S)Nz$%s unknown_type)rrrorigin)r0 structnamercrrrr*s  rcCs(|dkrd|}t|ddd}t||S)Nz$$%s)rri)r0rrcrrrunknown_ptr_type2src CsFt|tjrtSt(tt|ds2tjt|_ t|j SQRXdS)N __typecache) r{r ModuleType_typecache_cffi_backend global_lockhasattrr|weakrefWeakValueDictionaryr)Zbackendrrr get_typecache<s  rc Os|jd||f}y |j|Stk r.YnXyt|j||}Wn4tk rx}ztd|||fWYdd}~XnX|j}t(|j|} | dkr|||<|S| SWdQRXdS)Nrz %s: %r: %s)popZ _typecacher%r.rarSrget) Zsrctyper(funcnamerVkwdsrresecacheZres1rrrrCGs  $ rCcCstd|d|S)N?rg)rC)r(r+rrr pointer_cache^srcCs@|jrsN  /  L  G