Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file ppx_cstubs_internals.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481(* Copyright 2018 fdopen
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. *)openCtypes_staticletrecseal:typea.aCtypes_static.typ->size:int->align:int->unit=funt~size~align->matchtwith|Struct({spec=Incomplete_;_}ass)->s.fields<-List.revs.fields;s.spec<-Complete{size;align}|Union({uspec=None;_}asu)->u.ufields<-List.revu.ufields;u.uspec<-Some{size;align}|Struct{tag;_}->raise(ModifyingSealedTypetag)|Union{utag;_}->raise(ModifyingSealedTypeutag)|View{ty;_}->sealty~size~align|_->raise(Unsupported"Sealing a non-structured type")letrecadd_field:typeta.tCtypes_static.typ->string->int->aCtypes_static.typ->(a,t)Ctypes_static.field=funtfnamefoffsetftype->matchtwith|Structs->letr={fname;foffset;ftype}ins.fields<-BoxedFieldr::s.fields;r|Unionu->letr={fname;foffset;ftype}inu.ufields<-BoxedFieldr::u.ufields;r|View{ty;_}->let({fname=_;_}asr)=add_fieldtyfnamefoffsetftypeinr|_->failwith("Unexpected field "^fname)externalidentity:'a->'a="%identity"letbuild_enum:typeab.string->aCtypes.typ->typedef:bool->?unexpected:(int64->b)->(b*a)list->bCtypes.typ=funnametyp~typedef?unexpectedalist->letfailt=Printf.ksprintffailwith"Invalid enum type %s"(Ctypes.string_of_typt)inletrlist=List.map(fun(l,r)->(r,l))alistinletunexpected=matchunexpectedwith|None->letto_string=matchtypwith|Ctypes_static.Primitivep->(matchpwith|Ctypes_primitive_types.Int8_t->string_of_int|Ctypes_primitive_types.Int32_t->(Int32.to_string:a->string)|Ctypes_primitive_types.Int16_t->string_of_int|Ctypes_primitive_types.Int->string_of_int|Ctypes_primitive_types.Int64_t->Int64.to_string|Ctypes_primitive_types.Uint8_t->Unsigned.UInt8.to_string|Ctypes_primitive_types.Uint16_t->Unsigned.UInt16.to_string|Ctypes_primitive_types.Uint32_t->Unsigned.UInt32.to_string|Ctypes_primitive_types.Uint64_t->Unsigned.UInt64.to_string|_->failtyp:a->string)|_->failtypinfunk->Printf.ksprintffailwith"Unexpected enum value for %s: %s"name(to_stringk)|Somef->letto_int64=matchtypwith|Ctypes_static.Primitivep->(matchpwith|Ctypes_primitive_types.Int8_t->Int64.of_int|Ctypes_primitive_types.Int32_t->Int64.of_int32|Ctypes_primitive_types.Int16_t->Int64.of_int|Ctypes_primitive_types.Int->Int64.of_int|Ctypes_primitive_types.Int64_t->identity|Ctypes_primitive_types.Uint8_t->Unsigned.UInt8.to_int64|Ctypes_primitive_types.Uint16_t->Unsigned.UInt16.to_int64|Ctypes_primitive_types.Uint32_t->Unsigned.UInt32.to_int64|Ctypes_primitive_types.Uint64_t->Unsigned.UInt64.to_int64|_->failtyp:a->int64)|_->failtypinfunk->f(to_int64k)inletpname=iftypedefthennameelse"enum "^nameinletwritek=List.assockalistandreadk=tryList.assockrlistwithNot_found->unexpectedkandformat_typkfmt=Format.fprintffmt"%s%t"pnamekinCtypes_static.view~format_typ~read~writetypletbuild_enum_bitmask:typeab.string->aCtypes.typ->typedef:bool->?unexpected:(blist->int64->blist)->(b*a)list->blistCtypes.typ=funnametyp~typedef?unexpectedalist->letfailt=Printf.ksprintffailwith"Invalid enum type %s"(Ctypes.string_of_typt)inletlor',land',zero,lnot'=matchtypwith|Ctypes_static.Primitivep->(matchpwith|Ctypes_primitive_types.Int8_t->((lor),(land),0,lnot)|Ctypes_primitive_types.Int16_t->((lor),(land),0,lnot)|Ctypes_primitive_types.Int->((lor),(land),0,lnot)|Ctypes_primitive_types.Int32_t->Int32.(logor,logand,zero,lognot)|Ctypes_primitive_types.Int64_t->Int64.(logor,logand,zero,lognot)|Ctypes_primitive_types.Uint8_t->Unsigned.UInt8.(logor,logand,zero,lognot)|Ctypes_primitive_types.Uint16_t->Unsigned.UInt16.(logor,logand,zero,lognot)|Ctypes_primitive_types.Uint32_t->Unsigned.UInt32.(logor,logand,zero,lognot)|Ctypes_primitive_types.Uint64_t->Unsigned.UInt64.(logor,logand,zero,lognot)|_->failtyp:(a->a->a)*(a->a->a)*a*(a->a))|_->failtypinletunexpected=matchunexpectedwith|None->letto_string=matchtypwith|Ctypes_static.Primitivep->(matchpwith|Ctypes_primitive_types.Int8_t->string_of_int|Ctypes_primitive_types.Int32_t->(Int32.to_string:a->string)|Ctypes_primitive_types.Int16_t->string_of_int|Ctypes_primitive_types.Int->string_of_int|Ctypes_primitive_types.Int64_t->Int64.to_string|Ctypes_primitive_types.Uint8_t->Unsigned.UInt8.to_string|Ctypes_primitive_types.Uint16_t->Unsigned.UInt16.to_string|Ctypes_primitive_types.Uint32_t->Unsigned.UInt32.to_string|Ctypes_primitive_types.Uint64_t->Unsigned.UInt64.to_string|_->failtyp:a->string)|_->failtypinfun_k->Printf.ksprintffailwith"Unexpected enum value for %s: %s"name(to_stringk)|Somef->letto_int64=matchtypwith|Ctypes_static.Primitivep->(matchpwith|Ctypes_primitive_types.Int8_t->Int64.of_int|Ctypes_primitive_types.Int32_t->Int64.of_int32|Ctypes_primitive_types.Int16_t->Int64.of_int|Ctypes_primitive_types.Int->Int64.of_int|Ctypes_primitive_types.Int64_t->identity|Ctypes_primitive_types.Uint8_t->Unsigned.UInt8.to_int64|Ctypes_primitive_types.Uint16_t->Unsigned.UInt16.to_int64|Ctypes_primitive_types.Uint32_t->Unsigned.UInt32.to_int64|Ctypes_primitive_types.Uint64_t->Unsigned.UInt64.to_int64|_->failtyp:a->int64)|_->failtypinfunak->fa(to_int64k)inletpname=iftypedefthennameelse"enum "^nameinletralist=List.revalistinlet(write:blist->a)=funl->List.fold_left(funack->lor'(List.assockalist)ac)zeroland(read:a->blist)=funres->letreciterres_origacresl=matchlwith|[]->ifres=zerothenacelseunexpectedacres|(a,b)::tl->ifland'bres_orig=btheniterres_orig(a::ac)(land'res(lnot'b))tlelseiterres_origacrestliniterres[]resralistandformat_typkfmt=Format.fprintffmt"%s%t"pnamekinCtypes_static.view~format_typ~read~writetypexternalto_voidp:nativeint->Cstubs_internals.voidp="%identity"letinvalid_code()=failwith"ppx_cstub generated invalid code"moduleSigned=structmoduleNativeint=structincludestruct[@@@ocaml.warning"-32"]letequal(x:nativeint)(y:nativeint)=x=yletppfmtx=Format.fprintffmt"%nd"xendincludeNativeintmoduleInfix=structlet(+)=addlet(-)=sublet(*)=mullet(/)=divlet(mod)=remlet(land)=logandlet(lor)=logorlet(lxor)=logxorlet(lsl)=shift_leftlet(lsr)=shift_right_logical[@@@ocaml.warning"-32"]let(asr)=shift_rightendexternalof_nativeint:t->t="%identity"externalto_nativeint:t->t="%identity"letof_int64=Int64.to_nativeintletto_int64=Int64.of_nativeintletmax=maxletmin=minendmoduletypeInt_size=sigvalint_size:intendmoduleShort_int(X:Int_size)=structopenXtypet=intletland_mask=(1lslint_size)-1letcor=(1lslint_size)*-1letmin_int=cor/2letmax_int=land_mask/2letsign_bit=int_size-1letof_intx=letres=xlandland_maskin(cor*(reslsrsign_bit))+resletaddxy=of_int(x+y)letsubxy=of_int(x-y)letmulxy=of_int(x*y)letdivxy=of_int(x/y)letremxy=xmodyletlogandxy=xlandyletlogorxy=xloryletlogxorxy=xlxoryletshift_leftxy=of_int(xlsly)letshift_right_logicalxy=of_int((xlsry)land((1lsl(int_size-y))-1))letshift_rightxy=of_int(xasry)moduleInfix=structlet(+)=addlet(-)=sublet(*)=mullet(/)=divlet(mod)=remlet(land)=logandlet(lor)=logorlet(lxor)=logxorlet(lsl)=shift_leftlet(lsr)=shift_right_logical[@@@ocaml.warning"-32"]let(asr)=shift_rightendletlognotx=lnotxletcompare=compareexternalto_int:t->t="%identity"letof_stringx=letr=int_of_stringxinifr<min_int||r>max_intthenfailwith"int_of_string";rletto_string=string_of_intletzero=0letone=1letminus_one=-1letsuccx=of_int(succx)letpredx=of_int(predx)letto_int64=Int64.of_intletof_int64x=of_int(Int64.to_intx)letto_nativeint=Nativeint.of_intletof_nativeintx=of_int(Nativeint.to_intx)letabsx=of_int(absx)letnegx=of_int(-x)letmax=maxletmin=min[@@@ocaml.warning"-32"]letequal(x:t)(y:t)=x=yletppfmtn=Format.fprintffmt"%d"nendmoduleInt8=Short_int(structletint_size=8end)moduleInt16=Short_int(structletint_size=16end)moduleInt32=Short_int(structletint_size=32end)moduletypeShort=sigincludeSigned.Swithtypet=intendmoduleShort=(valmatchCtypes.sizeofCtypes.shortwith|1->(moduleInt8)|2->(moduleInt16)|4whenCtypes.sizeofCtypes.int=4->ifSys.word_size=64then(moduleInt32)else(moduleSigned.Int)|_->failwith"invalid size of short":Short)moduleSchar=Int8endmoduleCallback=structmoduletypeInfo=sigtyperealvalreal:realCtypes.fnendmoduleMake(H:Info):sigtypefn=H.realtype'attyperaw_pointervalt:H.realtCtypes.static_funptrCtypes.typvalfn:H.realCtypes.fnvalmake_pointer:raw_pointer->H.realtCtypes.static_funptrend=structopenHtypefn=realtype'at=realtyperaw_pointer=nativeintlett=Ctypes.static_funptrrealletfn=H.realletmake_pointerp=Cstubs_internals.make_fun_ptrH.real(to_voidpp)endletmake(typea)(fn:aCtypes.fn)=(modulestructtypereal=aletreal=fnend:Infowithtypereal=a)endmoduleShadow=structletrecpassable:typea.atyp->bool=function|Void->true|Primitive_->true|Struct{spec=Incomplete_;_}->raiseIncompleteType|Struct{spec=Complete_;_}->true|Union{uspec=None;_}->raiseIncompleteType|Union{uspec=Some_;_}->true|Array_->false|Bigarray_->false|Pointer_->true|Funptr_->true(* Allow to pass and return abstract types. I don't know why it is
disabled upstream. They are handled like structs and unions *)|Abstract_->true|OCaml_->true|View{ty;_}->passabletylet(@->)ab=ifnot(passablea)thenraise(Unsupported"Unsupported argument type")elseFunction(a,b)letreturninga=ifnot(passablea)thenraise(Unsupported"Unsupported return type")elseReturnsaendexternalobj_magic:'a->'b="%identity"