123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133openImportletwith_outputfn~binary~f=matchfnwith|None|Some"-"->fstdout|Somefn->Out_channel.with_filefn~binary~f;;moduleKind=structtypet=Intf|Implletof_filenamefn:toption=ifCaml.Filename.check_suffixfn".ml"thenSomeImplelseifCaml.Filename.check_suffixfn".mli"thenSomeIntfelseNone;;letdescribe=function|Impl->"implementation"|Intf->"interface";;letequal:t->t->bool=Poly.equalendmoduleAst_io=structtypet=|IntfofCompiler_ast.Parsetree.signature|ImplofCompiler_ast.Parsetree.structuretyperead_error=|Not_a_binary_astofstring(* The input doesn't contain a binary AST. The argument
corresponds to the bytes from the input that were consumed. *)|Unknown_versionofstring(* The input contains a binary AST for an unknown version of
OCaml. The argument is the unknown magic number. *)letmagic_length=String.lengthOcaml_common.Config.ast_impl_magic_numberletread_magicic=letbuf=Bytes.createmagic_lengthinletlen=inputicbuf0magic_lengthinlets=Bytes.sub_stringbuf~pos:0~leniniflen=magic_lengththenOkselseErrorsletreadic=matchread_magicicwith|Errors->Error(Not_a_binary_asts)|Oks->ifString.equalsOcaml_common.Config.ast_impl_magic_numberthenletfilename:string=input_valueicinletpayload=Impl(input_valueic)inOk(filename,payload)elseifString.equalsOcaml_common.Config.ast_intf_magic_numberthenletfilename:string=input_valueicinletpayload=Intf(input_valueic)inOk(filename,payload)elseifString.equals(String.subOcaml_common.Config.ast_impl_magic_number~pos:0~len:9)||String.equals(String.subOcaml_common.Config.ast_intf_magic_number~pos:0~len:9)thenError(Unknown_versions)elseError(Not_a_binary_asts)letwriteoc(filename:string)x=matchxwith|Intfx->output_stringocOcaml_common.Config.ast_intf_magic_number;output_valueocfilename;output_valueocx|Implx->output_stringocOcaml_common.Config.ast_impl_magic_number;output_valueocfilename;output_valueocxendmoduleIntf_or_impl=structtypet=|Intfofsignature|Implofstructureletmapt(map:Ast_traverse.map)=matchtwith|Implx->Impl(map#structurex)|Intfx->Intf(map#signaturex);;letmap_with_contextt(map:_Ast_traverse.map_with_context)ctx=matchtwith|Implx->Impl(map#structurectxx)|Intfx->Intf(map#signaturectxx);;letkind:_->Kind.t=function|Intf_->Intf|Impl_->Implletof_ast_ioast:t=matchastwith|Ast_io.Intfsg->Intf(Selected_ast.Of_ocaml.copy_signaturesg)|Ast_io.Implst->Impl(Selected_ast.Of_ocaml.copy_structurest)letto_ast_io(ast:t)~add_ppx_context=matchastwith|Intfsg->letsg=Selected_ast.To_ocaml.copy_signaturesginletsg=ifadd_ppx_contextthenOcaml_common.Ast_mapper.add_ppx_context_sig~tool_name:"ppx_driver"sgelsesginAst_io.Intfsg|Implst->letst=Selected_ast.To_ocaml.copy_structurestinletst=ifadd_ppx_contextthenOcaml_common.Ast_mapper.add_ppx_context_str~tool_name:"ppx_driver"stelsestinAst_io.Implstend