123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374# 1 "ppx_h5struct.cppo.ml"openMigrate_parsetreeopenAst_406openAst_mapperopenAst_helperopenAsttypesopenParsetreeopenLongidentmoduleType=structtypet=|Float64|Int|Int64|Stringofintletto_string=function|Float64->"Float64"|Int->"Int"|Int64->"Int64"|String_->"String"letwsize=function|Float64|Int|Int64->1|Stringlength->(length+7)/8endmoduleField=structtypet={id:string;name:string;type_:Type.t;ocaml_type:Longident.t;seek:bool;}end# 42 "ppx_h5struct.cppo.ml"letrecextract_fieldsexpression=matchexpression.pexp_descwith|Pexp_sequence(expression1,expression2)->extract_fieldsexpression1@extract_fieldsexpression2|Pexp_apply({pexp_desc=Pexp_ident{txt=id;_};pexp_loc;_},expressions)->letid=matchidwith|Lidentid->id|_->raise(Location.Error(Location.error~loc:pexp_loc(Printf.sprintf"[%%h5struct] invalid field %s, field identifiers must be simple"(Longident.lastid))))inbeginmatchexpressionswith|(_,name)::(_,type_)::expressions->letname=matchname.pexp_descwith|Pexp_constant(Pconst_string(name,_))->name|_->raise(Location.Error(Location.error~loc:name.pexp_loc(Printf.sprintf"[%%h5struct] invalid field %s, field name must be a string constant"id)))inlettype_,ocaml_type=matchtype_with|{pexp_desc=Pexp_construct(type_,expression_opt);pexp_loc=loc;_}->beginmatchtype_.txtwith|Lidenttype_->beginmatchtype_with|"Discrete"->letocaml_type=matchexpression_optwith|Some{pexp_desc=Pexp_ident{txt;_};_}->txt|_->raise(Location.Error(Location.error~loc(Printf.sprintf"[%%h5struct] invalid field %s, field type Discrete requires type"id)))inType.Int,ocaml_type|"Float64"->Type.Float64,Longident.Lident"float"|"Int"->Type.Int,Longident.Lident"int"|"Int64"->Type.Int64,Longident.Lident"int64"|"String"->lettype_=matchexpression_optwith# 87 "ppx_h5struct.cppo.ml"|Some{pexp_desc=Pexp_constant(Pconst_integer(length,_));_}->Type.String(int_of_stringlength)# 93 "ppx_h5struct.cppo.ml"|_->raise(Location.Error(Location.error~loc(Printf.sprintf"[%%h5struct] invalid field %s, field type String requires length"id)))intype_,Longident.Lident"string"|_->raise(Location.Error(Location.error~loc(Printf.sprintf"[%%h5struct] invalid field %s, unrecognized type %s"idtype_)))end|_->raise(Location.Error(Location.error~loc(Printf.sprintf"[%%h5struct] invalid field %s, field type must be simple"id)))end|_->raise(Location.Error(Location.error~loc:type_.pexp_loc(Printf.sprintf"[%%h5struct] invalid field %s, field type must be a construct"id)))inletseek=reffalseinList.iter(fun(_,expression)->matchexpression.pexp_descwith|Pexp_construct({txt=Lident"Seek";_},None)->seek:=true|_->raise(Location.Error(Location.error~loc:expression.pexp_loc(Printf.sprintf"[%%h5struct] invalid field %s, unexpected modifiers"id))))expressions;[{Field.id;name;type_;ocaml_type;seek=!seek}]|_->raise(Location.Error(Location.error~loc:pexp_loc(Printf.sprintf"[%%h5struct] invalid field %s, exactly two arguments expected: name and type"id)))end|_->raise(Location.Error(Location.error~loc:expression.pexp_loc"[%h5struct] accepts a list of fields, \
e.g. [%h5struct time \"Time\" Int; price \"Price\" Float64]"))letrecconstruct_fields_listfieldsloc=matchfieldswith|[]->Exp.construct~loc{txt=Longident.Lident"[]";loc}None;|field::fields->Exp.construct~loc{txt=Longident.Lident"::";loc}(Some(Exp.tuple~loc[Exp.apply~loc(Exp.ident{txt=Longident.(Ldot(Ldot(Lident"Hdf5_caml","Field"),"create"));loc})[Nolabel,Exp.constant~loc(Pconst_string(field.Field.name,None));Nolabel,Exp.construct~loc{loc;txt=Longident.(Ldot(Ldot(Lident"Hdf5_caml","Type"),matchfield.Field.type_with|Type.Float64->"Float64"|Type.Int->"Int"|Type.Int64->"Int64"|Type.String_->"String"))}(matchfield.Field.type_with# 150 "ppx_h5struct.cppo.ml"|Type.Stringlength->Some(Exp.constant~loc(Pconst_integer(string_of_intlength,None)))# 155 "ppx_h5struct.cppo.ml"|_->None)];construct_fields_listfieldsloc]))letconstruct_function~locnameargsbody=letrecconstruct_args=function|[]->body|(arg,typ)::args->Exp.fun_~locNolabelNone(Pat.constraint_~loc(Pat.var~loc{txt=arg;loc})(Typ.constr~loc{txt=typ;loc}[]))(construct_argsargs)inStr.value~locNonrecursive[Vb.mk~loc(Pat.var~loc{txt=name;loc})(construct_argsargs)]letrecconstruct_function_call~locnameargs=Exp.apply~loc(Exp.ident~loc{txt=name;loc})(List.map(funarg->Nolabel,matchargwith|`Expe->e# 179 "ppx_h5struct.cppo.ml"|`Inti->Exp.constant~loc(Pconst_integer(string_of_inti,None))# 183 "ppx_h5struct.cppo.ml"|`Varv->Exp.ident~loc{txt=Longident.Lidentv;loc}|`Mgcv->obj_magic~loc(Exp.ident~loc{txt=Longident.Lidentv;loc}))args)andobj_magic~locexp=construct_function_call~locLongident.(Ldot(Lident"Obj","magic"))[`Expexp]letconstruct_field_getfieldposloc=construct_function~locfield.Field.id["t",Longident.Lident"t"](Exp.constraint_~loc(* Types [Discrete], [Time] and [Time_ns] are stored as [int] or [float] and to
access them we need to use [Obj.magic]. *)(obj_magic~loc(construct_function_call~locLongident.(Ldot(Ldot(Ldot(Lident"Hdf5_caml","Struct"),"Ptr"),(matchfield.Field.type_with|Type.Float64->"get_float64"|Type.Int->"get_int"|Type.Int64->"get_int64"|Type.String_->"get_string")))(* It is hidden that [t] is of type [Struct.Ptr.t] so it's necessary to use
[Obj.magic] to access it. *)([`Mgc"t"]@(matchfield.Field.type_with|Type.Float64|Type.Int|Type.Int64->[`Intpos]|Type.Stringlength->[`Intpos;`Intlength]))))(Typ.constr~loc{txt=field.Field.ocaml_type;loc}[]))letconstruct_field_setfieldposloc=construct_function~loc("set_"^field.Field.id)["t",Longident.Lident"t";"v",field.Field.ocaml_type](construct_function_call~locLongident.(Ldot(Ldot(Ldot(Lident"Hdf5_caml","Struct"),"Ptr"),(matchfield.Field.type_with|Type.Float64->"set_float64"|Type.Int->"set_int"|Type.Int64->"set_int64"|Type.String_->"set_string")))(* It is hidden that [t] is of type [Struct.Ptr.t] so it's necessary to use
[Obj.magic] to access it. *)([`Mgc"t"]@(matchfield.Field.type_with|Type.Float64|Type.Int|Type.Int64->[`Intpos]|Type.Stringlength->[`Intpos;`Intlength])(* Types [Discrete], [Time] and [Time_ns] are stored as [int] or [float] and to
access them we need to use [Obj.magic]. *)@[`Mgc"v"]))letconstruct_field_seekfield~bsizeposloc=construct_function~loc("seek_"^field.Field.id)["t",Longident.Lident"t";"v",field.Field.ocaml_type](construct_function_call~locLongident.(Ldot(Ldot(Ldot(Lident"Hdf5_caml","Struct"),"Ptr"),(matchfield.Field.type_with|Type.Float64->"seek_float64"|Type.Int->"seek_int"|Type.Int64->"seek_int64"|Type.String_->"seek_string")))(* It is hidden that [t] is of type [Struct.Ptr.t] so it's necessary to use
[Obj.magic] to access it. *)([`Mgc"t";`Int(bsize/2)]@(matchfield.Field.type_with|Type.Float64|Type.Int|Type.Int64->[`Intpos]|Type.Stringlen->[`Intpos;`Intlen])(* Types [Discrete], [Time] and [Time_ns] are stored as [int] or [float] and to
access them we need to use [Obj.magic]. *)@[`Mgc"v"]))letconstruct_set_all_fieldsfieldsloc=letrecconstruct_sets=function|[]->assertfalse|field::fields->letset=Exp.apply~loc(Exp.ident~loc{txt=Longident.Lident("set_"^field.Field.id);loc})[Nolabel,Exp.ident~loc{txt=Longident.Lident"t";loc};Nolabel,Exp.ident~loc{txt=Longident.Lidentfield.Field.id;loc}]inmatchfieldswith|[]->set|_->Exp.sequence~locset(construct_setsfields)inletrecconstruct_funs=function|[]->construct_setsfields|field::fields-># 274 "ppx_h5struct.cppo.ml"Exp.fun_~loc(Labelledfield.Field.id)None# 278 "ppx_h5struct.cppo.ml"(Pat.var~loc{txt=field.Field.id;loc})(construct_funsfields)in[Str.value~locNonrecursive[Vb.mk~loc(Pat.var~loc{txt="set";loc})(Exp.fun_~locNolabelNone(Pat.var~loc{txt="t";loc})(construct_funsfields))];Str.value~locNonrecursive[Vb.mk~loc(Pat.var~loc{txt="_";loc})(Exp.ident~loc{txt=Longident.Lident"set";loc})]]letconstruct_size_dependent_funname~bsize~indexloc=letcall=Exp.apply~loc(Exp.ident~loc{loc;txt=Longident.(Ldot(Ldot(Ldot(Lident"Hdf5_caml","Struct"),"Ptr"),name))})(* It is hidden that [t] is of type [Struct.Ptr.t] so it's necessary to use
[Obj.magic] to access it. *)([Nolabel,obj_magic~loc(Exp.ident~loc{txt=Longident.Lident"t";loc})]@(ifindexthen[Nolabel,Exp.ident~loc{txt=Longident.Lident"i";loc}]else[])@[Nolabel,# 304 "ppx_h5struct.cppo.ml"Exp.constant~loc(Pconst_integer(string_of_int(bsize/2),None))])# 308 "ppx_h5struct.cppo.ml"in[Str.value~locNonrecursive[Vb.mk~loc(Pat.var~loc{txt=name;loc})(Exp.fun_~locNolabelNone(Pat.constraint_~loc(Pat.var~loc{txt="t";loc})(Typ.constr~loc{txt=Longident.Lident"t";loc}[]))(ifindexthenExp.fun_~locNolabelNone(Pat.var~loc{txt="i";loc})callelsecall))];Str.value~locNonrecursive[Vb.mk~loc(Pat.var~loc{txt="_";loc})(Exp.ident~loc{txt=Longident.Lidentname;loc})]]letmap_structure_itemmapperstructure_item=matchstructure_itemwith|{pstr_desc=Pstr_extension(({txt="h5struct";_},payload),attrs);pstr_loc=loc}->letfields=matchpayloadwith|PStr[{pstr_desc=Pstr_eval(expression,_);_}]->extract_fieldsexpression|_->raise(Location.Error(Location.error~loc"[%h5struct] accepts a list of fields, \
e.g. [%h5struct time \"Time\" Int; price \"Price\" Float64]"))inletinclude_=Str.include_~loc(Incl.mk~loc(Mod.apply~loc(Mod.ident~loc{loc;txt=Longident.(Ldot(Ldot(Lident"Hdf5_caml","Struct"),"Make"))})(Mod.structure~loc[Str.value~locNonrecursive[Vb.mk~loc(Pat.var~loc{txt="fields";loc})(construct_fields_listfieldsloc)]])))inletbsize=8*List.fold_left(funsumfield->sum+Type.wsizefield.Field.type_)0fieldsinletpos=ref0inletfunctions=List.map(funfield->letfunctions=[construct_field_getfield!posloc;construct_field_setfield!posloc]@(iffield.Field.seekthen[construct_field_seekfield~bsize!posloc]else[])inpos:=!pos+(matchfield.Field.type_with|Type.Float64|Type.Int|Type.Int64->4|Type.Stringlength->(length+7)/8*4);functions)fields|>List.concatinStr.include_~loc(Incl.mk~loc~attrs(Mod.structure~loc(include_::functions@(construct_set_all_fieldsfieldsloc)@(construct_size_dependent_fun"unsafe_next"~bsize~index:falseloc)@(construct_size_dependent_fun"unsafe_prev"~bsize~index:falseloc)@(construct_size_dependent_fun"unsafe_move"~bsize~index:trueloc)@(construct_size_dependent_fun"next"~bsize~index:falseloc)@(construct_size_dependent_fun"prev"~bsize~index:falseloc)@(construct_size_dependent_fun"move"~bsize~index:trueloc))))|s->default_mapper.structure_itemmappersleth5struct_mapper_config_cookies={default_mapperwithstructure_item=map_structure_item}let()=Driver.register~name:"h5struct"Versions.ocaml_406h5struct_mapper