123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209openPrintfopenAtd_astopenAg_erroropenAg_mappingtypeo=Ag_ocaml.atd_ocaml_reprtypej=Ag_json.json_reprtypeoj_mapping=(Ag_ocaml.atd_ocaml_repr,Ag_json.json_repr)Ag_mapping.mappingtypeoj_def=(Ag_ocaml.atd_ocaml_repr,Ag_json.json_repr)Ag_mapping.def(*
Translation of the types into the ocaml/json mapping.
*)letrecmapping_of_expr(x:type_expr):oj_mapping=matchxwith`Sum(loc,l,an)->letocaml_t=`Sum(Ag_ocaml.get_ocaml_suman)inletjson_t=`Sumin`Sum(loc,Array.of_list(List.mapmapping_of_variantl),ocaml_t,json_t)|`Record(loc,l,an)->letocaml_t=`Record(Ag_ocaml.get_ocaml_recordan)inletocaml_field_prefix=Ag_ocaml.get_ocaml_field_prefixaninletjson_t=`Record(Ag_json.get_json_recordan)in`Record(loc,Array.of_list(List.map(mapping_of_fieldocaml_field_prefix)l),ocaml_t,json_t)|`Tuple(loc,l,an)->letocaml_t=`Tupleinletjson_t=`Tuplein`Tuple(loc,Array.of_list(List.mapmapping_of_celll),ocaml_t,json_t)|`List(loc,x,an)->letocaml_t=`List(Ag_ocaml.get_ocaml_listan)inletjson_t=`List(Ag_json.get_json_listan)in`List(loc,mapping_of_exprx,ocaml_t,json_t)|`Option(loc,x,an)->letocaml_t=`Optioninletjson_t=`Optionin`Option(loc,mapping_of_exprx,ocaml_t,json_t)|`Nullable(loc,x,an)->letocaml_t=`Nullableinletjson_t=`Nullablein`Nullable(loc,mapping_of_exprx,ocaml_t,json_t)|`Shared(loc,x,an)->errorloc"Sharing is not supported by the JSON interface"|`Wrap(loc,x,an)->letocaml_t=`Wrap(Ag_ocaml.get_ocaml_wraplocan)inletjson_t=`Wrapin`Wrap(loc,mapping_of_exprx,ocaml_t,json_t)|`Name(loc,(loc2,s,l),an)->(matchswith"unit"->`Unit(loc,`Unit,`Unit)|"bool"->`Bool(loc,`Bool,`Bool)|"int"->leto=Ag_ocaml.get_ocaml_intanin`Int(loc,`Into,`Int)|"float"->letj=Ag_json.get_json_floatanin`Float(loc,`Float,`Floatj)|"string"->`String(loc,`String,`String)|s->`Name(loc,s,List.mapmapping_of_exprl,None,None))|`Tvar(loc,s)->`Tvar(loc,s)andmapping_of_cell(loc,x,an)=letdefault=Ag_ocaml.get_ocaml_defaultaninletdoc=Ag_doc.get_doclocaninletocaml_t=`Cell{Ag_ocaml.ocaml_default=default;ocaml_fname="";ocaml_mutable=false;ocaml_fdoc=doc;}inletjson_t=`Cellin{cel_loc=loc;cel_value=mapping_of_exprx;cel_arepr=ocaml_t;cel_brepr=json_t}andmapping_of_variant=function`Variant(loc,(s,an),o)->letocaml_cons=Ag_ocaml.get_ocaml_conssaninletdoc=Ag_doc.get_doclocaninletocaml_t=`Variant{Ag_ocaml.ocaml_cons=ocaml_cons;ocaml_vdoc=doc;}inletjson_t=ifAg_json.get_json_untypedanthen`Variant{Ag_json.json_cons=None;}elseletjson_cons=Ag_json.get_json_conssanin`Variant{Ag_json.json_cons=Somejson_cons;}inletarg=matchowithNone->None|Somex->Some(mapping_of_exprx)in{var_loc=loc;var_cons=s;var_arg=arg;var_arepr=ocaml_t;var_brepr=json_t}|`Inherit_->assertfalseandmapping_of_fieldocaml_field_prefix=function`Field(loc,(s,fk,an),x)->letfvalue=mapping_of_exprxinletocaml_default,json_unwrapped=matchfk,Ag_ocaml.get_ocaml_defaultanwith`Required,None->None,false|`Optional,None->Some"None",true|(`Required|`Optional),Some_->errorloc"Superfluous default OCaml value"|`With_default,Somes->Somes,false|`With_default,None->(* will try to determine implicit default value later *)None,falseinletocaml_fname=Ag_ocaml.get_ocaml_fname(ocaml_field_prefix^s)aninletocaml_mutable=Ag_ocaml.get_ocaml_mutableaninletdoc=Ag_doc.get_doclocaninletjson_fname=Ag_json.get_json_fnamesaninletjson_tag_field=Ag_json.get_json_tag_fieldanin{f_loc=loc;f_name=s;f_kind=fk;f_value=fvalue;f_arepr=`Field{Ag_ocaml.ocaml_default=ocaml_default;ocaml_fname=ocaml_fname;ocaml_mutable=ocaml_mutable;ocaml_fdoc=doc;};f_brepr=`Field{Ag_json.json_fname;json_tag_field;json_unwrapped;};}|`Inherit_->assertfalseletdef_of_atd(loc,(name,param,an),x)=letocaml_predef=Ag_ocaml.get_ocaml_predef`Jsonaninletdoc=Ag_doc.get_doclocaninleto=matchas_abstractxwithSome(loc2,an2)->(matchAg_ocaml.get_ocaml_module_and_t`JsonnameanwithNone->None|Some(types_module,main_module,ext_name)->letargs=List.map(funs->`Tvar(loc,s))paraminSome(`External(loc,name,args,`External(types_module,main_module,ext_name),`External)))|None->Some(mapping_of_exprx)in{def_loc=loc;def_name=name;def_param=param;def_value=o;def_arepr=`Def{Ag_ocaml.ocaml_predef=ocaml_predef;ocaml_ddoc=doc};def_brepr=`Def;}letdefs_of_atd_modulel=List.map(function`Typedef->def_of_atddef)lletdefs_of_atd_modulesl=List.map(fun(is_rec,l)->(is_rec,defs_of_atd_modulel))l