123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305(*
* Copyright (c) 2013 Jeremy Yallop.
*
* This file is distributed under the terms of the MIT License.
* See the file LICENSE for details.
*)(* C type construction *)[@@@warning"-9"]exceptionIncompleteTypeexceptionModifyingSealedTypeofstringexceptionUnsupportedofstringletunsupportedfmt=Printf.ksprintf(funs->raise(Unsupporteds))fmttypeincomplete_size={mutableisize:int}typestructured_spec={size:int;align:int;}type'astructspec=Incompleteofincomplete_size|Completeofstructured_spectypeabstract_type={aname:string;asize:int;aalignment:int;}type_ocaml_type=String:stringocaml_type|Bytes:bytesocaml_type|FloatArray:floatarrayocaml_typetypequalifier=Const|Volatiletype_typ=Void:unittyp|Primitive:'aCtypes_primitive_types.prim->'atyp|Pointer:'atyp->'aptrtyp|Funptr:'afn->'astatic_funptrtyp|Struct:'astructure_type->'astructuretyp|Union:'aunion_type->'auniontyp|Abstract:abstract_type->'aabstracttyp|View:('a,'b)view->'atyp|Qualified:qualifier*'atyp->'atyp|Array:'atyp*int->'acarraytyp|Bigarray:(_,'a,_)Ctypes_bigarray.t->'atyp|OCaml:'aocaml_type->'aocamltypand'acarray={astart:'aptr;alength:int}and('a,'kind)structured={structured:('a,'kind)structuredptr}[@@unboxed]and'aunion=('a,[`Union])structuredand'astructure=('a,[`Struct])structuredand'aabstract=('a,[`Abstract])structuredand(_,_)pointer=CPointer:(Obj.toption,'atyp)Ctypes_ptr.Fat.t->('a,[`C])pointer|OCamlRef:int*'a*'aocaml_type->('a,[`OCaml])pointerand'aptr=('a,[`C])pointerand'aocaml=('a,[`OCaml])pointerand'astatic_funptr=Static_funptr:(Obj.toption,'afn)Ctypes_ptr.Fat.t->'astatic_funptrand('a,'b)view={read:'b->'a;write:'a->'b;format_typ:((Format.formatter->unit)->Format.formatter->unit)option;format:(Format.formatter->'a->unit)option;ty:'btyp;}and('a,'s)field={ftype:'atyp;foffset:int;fname:string;}and'astructure_type={tag:string;mutablespec:'astructspec;(* fields are in reverse order iff the struct type is incomplete *)mutablefields:'astructureboxed_fieldlist;}and'aunion_type={utag:string;mutableuspec:structured_specoption;(* fields are in reverse order iff the union type is incomplete *)mutableufields:'aunionboxed_fieldlist;}and'sboxed_field=BoxedField:('a,'s)field->'sboxed_fieldand_fn=|Returns:'atyp->'afn|Function:'atyp*'bfn->('a->'b)fntype_bigarray_class=Genarray:<element:'a;layout:'l;dims:intarray;ba_repr:'b;bigarray:('a,'b,'l)Bigarray_compat.Genarray.t;carray:'acarray>bigarray_class|Array1:<element:'a;layout:'l;dims:int;ba_repr:'b;bigarray:('a,'b,'l)Bigarray_compat.Array1.t;carray:'acarray>bigarray_class|Array2:<element:'a;layout:'l;dims:int*int;ba_repr:'b;bigarray:('a,'b,'l)Bigarray_compat.Array2.t;carray:'acarraycarray>bigarray_class|Array3:<element:'a;layout:'l;dims:int*int*int;ba_repr:'b;bigarray:('a,'b,'l)Bigarray_compat.Array3.t;carray:'acarraycarraycarray>bigarray_classtypeboxed_typ=BoxedType:'atyp->boxed_typletrecsizeof:typea.atyp->int=functionVoid->raiseIncompleteType|Primitivep->Ctypes_primitives.sizeofp|Struct{spec=Incomplete_}->raiseIncompleteType|Struct{spec=Complete{size}}->size|Union{uspec=None}->raiseIncompleteType|Union{uspec=Some{size}}->size|Array(t,i)->i*sizeoft|Bigarrayba->Ctypes_bigarray.sizeofba|Abstract{asize}->asize|Pointer_->Ctypes_primitives.pointer_size|Funptr_->Ctypes_primitives.pointer_size|OCaml_->raiseIncompleteType|View{ty}->sizeofty|Qualified(_,ty)->sizeoftyletrecalignment:typea.atyp->int=functionVoid->raiseIncompleteType|Primitivep->Ctypes_primitives.alignmentp|Struct{spec=Incomplete_}->raiseIncompleteType|Struct{spec=Complete{align}}->align|Union{uspec=None}->raiseIncompleteType|Union{uspec=Some{align}}->align|Array(t,_)->alignmentt|Bigarrayba->Ctypes_bigarray.alignmentba|Abstract{aalignment}->aalignment|Pointer_->Ctypes_primitives.pointer_alignment|Funptr_->Ctypes_primitives.pointer_alignment|OCaml_->raiseIncompleteType|View{ty}->alignmentty|Qualified(_,ty)->alignmenttyletrecpassable:typea.atyp->bool=functionVoid->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|Abstract_->false|OCaml_->true|View{ty}->passablety|Qualified(_,ty)->passablety(* Whether a value resides in OCaml-managed memory.
Values that reside in OCaml memory cannot be accessed
when the runtime lock is not held. *)letrecocaml_value:typea.atyp->bool=functionVoid->false|Primitive_->false|Struct_->false|Union_->false|Array_->false|Bigarray_->false|Pointer_->false|Funptr_->false|Abstract_->false|OCaml_->true|View{ty}->ocaml_valuety|Qualified(_,ty)->ocaml_valuetyletrechas_ocaml_argument:typea.afn->bool=functionReturns_->false|Function(t,_)whenocaml_valuet->true|Function(_,t)->has_ocaml_argumenttletvoid=Voidletchar=PrimitiveCtypes_primitive_types.Charletschar=PrimitiveCtypes_primitive_types.Scharletfloat=PrimitiveCtypes_primitive_types.Floatletdouble=PrimitiveCtypes_primitive_types.Doubleletldouble=PrimitiveCtypes_primitive_types.LDoubleletcomplex32=PrimitiveCtypes_primitive_types.Complex32letcomplex64=PrimitiveCtypes_primitive_types.Complex64letcomplexld=PrimitiveCtypes_primitive_types.Complexldletshort=PrimitiveCtypes_primitive_types.Shortletint=PrimitiveCtypes_primitive_types.Intletsint=PrimitiveCtypes_primitive_types.Sintletlong=PrimitiveCtypes_primitive_types.Longletllong=PrimitiveCtypes_primitive_types.Llongletnativeint=PrimitiveCtypes_primitive_types.Nativeintletint8_t=PrimitiveCtypes_primitive_types.Int8_tletint16_t=PrimitiveCtypes_primitive_types.Int16_tletint32_t=PrimitiveCtypes_primitive_types.Int32_tletint64_t=PrimitiveCtypes_primitive_types.Int64_tletcamlint=PrimitiveCtypes_primitive_types.Camlintletuchar=PrimitiveCtypes_primitive_types.Ucharletbool=PrimitiveCtypes_primitive_types.Boolletuint8_t=PrimitiveCtypes_primitive_types.Uint8_tletuint16_t=PrimitiveCtypes_primitive_types.Uint16_tletuint32_t=PrimitiveCtypes_primitive_types.Uint32_tletuint64_t=PrimitiveCtypes_primitive_types.Uint64_tletsize_t=PrimitiveCtypes_primitive_types.Size_tletushort=PrimitiveCtypes_primitive_types.Ushortletuint=PrimitiveCtypes_primitive_types.Uintletulong=PrimitiveCtypes_primitive_types.Ulongletullong=PrimitiveCtypes_primitive_types.Ullongletarrayit=Array(t,i)letocaml_string=OCamlStringletocaml_bytes=OCamlBytesletocaml_float_array=OCamlFloatArrayletptrt=Pointertlet(@->)ft=ifnot(passablef)thenraise(Unsupported"Unsupported argument type")elseFunction(f,t)letabstract~name~size~alignment=Abstract{aname=name;asize=size;aalignment=alignment}letview?format_typ?format~read~writety=View{read;write;format_typ;format;ty}letidv=vlettypedefoldname=view~format_typ:(funkfmt->Format.fprintffmt"%s%t"namek)~read:id~write:idoldletbigarray_:typeabcdel.<element:a;layout:l;dims:b;ba_repr:c;bigarray:d;carray:e>bigarray_class->b->(a,c)Bigarray_compat.kind->lBigarray_compat.layout->dtyp=funspecdimskindl->matchspecwith|Genarray->Bigarray(Ctypes_bigarray.bigarraydimskindl)|Array1->Bigarray(Ctypes_bigarray.bigarray1dimskindl)|Array2->letd1,d2=dimsinBigarray(Ctypes_bigarray.bigarray2d1d2kindl)|Array3->letd1,d2,d3=dimsinBigarray(Ctypes_bigarray.bigarray3d1d2d3kindl)letbigarrayspecck=bigarray_specckBigarray_compat.c_layoutletfortran_bigarrayspecck=bigarray_specckBigarray_compat.fortran_layoutletreturningv=ifnot(passablev)thenraise(Unsupported"Unsupported return type")elseReturnsvletstatic_funptrfn=Funptrfnletstructuretag=Struct{spec=Incomplete{isize=0};tag;fields=[]}letunionutag=Union{utag;uspec=None;ufields=[]}letoffsetof{foffset}=foffsetletfield_type{ftype}=ftypeletfield_name{fname}=fnameletrecconst:typea.atyp->atyp=function|Qualified(Const,_)asty->ty|Qualified(Volatile,ty)->Qualified(Volatile,constty)|ty->Qualified(Const,ty)letrecvolatile:typea.atyp->atyp=function|Qualified(Volatile,_)asty->ty|Qualified(Const,ty)->Qualified(Const,volatilety)|ty->Qualified(Volatile,ty)(* This corresponds to the enum in ctypes_primitives.h *)typearithmetic=Int8|Int16|Int32|Int64|Uint8|Uint16|Uint32|Uint64|Float|Double