123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237moduleOt=Pb_codegen_ocaml_typemoduleF=Pb_codegen_formattingmoduleE=Pb_exceptionopenPb_codegen_utilletdefault_value_of_basic_type?field_namebasic_typefield_default=matchbasic_type,field_defaultwith|Ot.Bt_string,None->"\"\""|Ot.Bt_string,Some(Pb_option.Constant_strings)->sp"\"%s\""s|Ot.Bt_float,None->"0."|Ot.Bt_float,Some(Pb_option.Constant_floatf)->string_of_floatf|Ot.Bt_int,None->"0"|Ot.Bt_int,Some(Pb_option.Constant_inti)->string_of_inti|Ot.Bt_int32,None->"0l"|Ot.Bt_int32,Some(Pb_option.Constant_inti)->sp"%il"i|Ot.Bt_uint32,None->"(`unsigned 0l)"|Ot.Bt_uint32,Some(Pb_option.Constant_inti)->sp"(`unsigned %il)"i|Ot.Bt_int64,None->"0L"|Ot.Bt_int64,Some(Pb_option.Constant_inti)->sp"%iL"i|Ot.Bt_uint64,None->"(`unsigned 0L)"|Ot.Bt_uint64,Some(Pb_option.Constant_inti)->sp"(`unsigned %iL)"i|Ot.Bt_bytes,None->"Bytes.create 0"|Ot.Bt_bytes,Some(Pb_option.Constant_strings)->sp"Bytes.of_string \"%s\""s|Ot.Bt_bool,None->"false"|Ot.Bt_bool,Some(Pb_option.Constant_boolb)->string_of_boolb|_->E.invalid_default_value?field_name~info:"invalid default type"()(* Generate the string which is the default value for a given field
type and default information. *)letdefault_value_of_field_type?field_namefield_typefield_default:string=matchfield_typewith|Ot.Ft_user_defined_typeudt->letfunction_prefix="default"inletf=function_name_of_user_defined~function_prefixudtinPrintf.sprintf"%s ()"f|Ot.Ft_unit->"()"|Ot.Ft_basic_typebt->default_value_of_basic_type?field_namebtfield_default|Ot.Ft_wrapper_type_->"None"typedefault_info={fname:string;ftype:string;ftype_underlying:string;(** Type of the field without [option] around for optionals *)default_value:string;(** Code for the default value *)optional:bool;(** Are we passing an option? *)rfp:Ot.record_field_presence;bitfield_idx:int;}(** This function returns [(field_name, field_default_value, field_type)] for a
record field. *)letrecord_field_default_info(record_field:Ot.record_field):default_info=let{Ot.rf_label;Ot.rf_field_type;_}=record_fieldinlettype_string=Pb_codegen_util.string_of_record_field_type~with_option:truerf_field_typeinlettype_string_underlying=Pb_codegen_util.string_of_record_field_type~with_option:falserf_field_typeinletfield_name=rf_labelinletdfvftfield_typefield_default=default_value_of_field_type~field_namefield_typefield_defaultinletdefault_value_of_field_type=function|Ot.Rft_nolabel(field_type,_,_)->dfvftfield_typeNone|Ot.Rft_required(field_type,_,_,default_value)->dfvftfield_typedefault_value|Ot.Rft_optional(field_type,_,_,default_value)->(matchdefault_valuewith|None->"None"|Some_->sp"Some (%s)"@@dfvftfield_typedefault_value)|Ot.Rft_repeated(rt,field_type,_,_,_)->(matchrtwith|Ot.Rt_list->"[]"|Ot.Rt_repeated_field->sp"Pbrt.Repeated_field.make (%s)"(dfvftfield_typeNone))|Ot.Rft_associative(at,_,_,_)->(matchatwith|Ot.At_list->"[]"|Ot.At_hashtable->"Hashtbl.create 8")|Ot.Rft_variant{Ot.v_constructors;_}->(* TODO This initial value could be configurable either via
the default function or via a protobuf option. *)(matchv_constructorswith|[]->assertfalse|{Ot.vc_constructor;vc_field_type;_}::_->(matchvc_field_typewith|Ot.Vct_nullary->vc_constructor|Ot.Vct_non_nullary_constructorfield_type->sp"%s (%s)"vc_constructor(dfvftfield_typeNone)))inletdefault_value,optional=matchrecord_field.rf_presencewith|Ot.Rfp_wrapped_option->"None",true|Ot.Rfp_bitfield_->default_value_of_field_typerf_field_type,true|Ot.Rfp_always->default_value_of_field_typerf_field_type,false|Ot.Rfp_repeated->default_value_of_field_typerf_field_type,falseinletbitfield_idx=matchrecord_field.rf_presencewith|Ot.Rfp_bitfieldidx->idx|_->-1in{fname=field_name;default_value;ftype=type_string;ftype_underlying=type_string_underlying;optional;rfp=record_field.rf_presence;bitfield_idx;}letin_bitfield(d:default_info):bool=matchd.rfpwith|Rfp_bitfield_->true|_->falseletgen_record{Ot.r_name;r_fields}sc:unit=letfields_default_info=List.map(funr_field->record_field_default_infor_field)r_fieldsinletlen_bitfield=List.filterin_bitfieldfields_default_info|>List.lengthinF.linepsc"let default_%s (): %s ="r_namer_name;F.linepsc"{";F.sub_scopesc(funsc->(* add bitfield *)iflen_bitfield>0thenF.linepsc"_presence=Pbrt.Bitfield.empty;";List.iter(fund->(*F.linep sc "(* optional=%b, in_bitfield=%b *)" d.optional d.in_bitfield;*)F.linepsc"%s=%s;"d.fnamed.default_value)fields_default_info);F.linesc"}"letgen_unit{Ot.er_name}sc=F.linepsc"let default_%s : %s = ()"er_nameer_nameletgen_variant{Ot.v_name;Ot.v_constructors;v_use_polyvariant=_}sc=matchv_constructorswith|[]->failwith"programmatic TODO error"|{Ot.vc_constructor;vc_field_type;_}::_->(matchvc_field_typewith|Ot.Vct_nullary->F.linepsc"let default_%s (): %s = %s"v_namev_namevc_constructor|Ot.Vct_non_nullary_constructorfield_type->letdefault_value=letfield_name=v_nameindefault_value_of_field_type~field_namefield_typeNonein(* TODO need to fix the default value *)F.linepsc"let default_%s (): %s = %s (%s)"v_namev_namevc_constructordefault_value)letgen_const_variant{Ot.cv_name;Ot.cv_constructors}sc=letfirst_constructor_name=matchcv_constructorswith|[]->failwith"programmatic TODO error"|{Ot.cvc_name;_}::_->cvc_nameinF.linepsc"let default_%s () = (%s:%s)"cv_namefirst_constructor_namecv_nameletgen_struct_full?and_:_tsc=let{Ot.spec;_}=tinlethas_encoded=matchspecwith|Ot.Recordr->gen_recordrsc;true|Ot.Variantv->gen_variantvsc;true|Ot.Const_variantv->gen_const_variantvsc;true|Ot.Unitu->gen_unitusc;trueinhas_encodedletgen_struct?and_~mode:_tsc=gen_struct_full?and_tscletgen_sig_recordsc{Ot.r_name;_}:unit=F.linepsc"val default_%s : unit -> %s "r_namer_name;F.linepsc"(** [default_%s ()] is a new empty value for type [%s] *)"r_namer_nameletgen_sig_unitsc{Ot.er_name}=F.linepsc"val default_%s : unit"er_name;letrn=er_nameinF.linepsc"(** [default_%s] is the default value for type [%s] *)"rnrnletgen_sig?and_:_~mode:_tsc=letgen_default_fun_type_name=F.linepsc"val default_%s : unit -> %s"type_nametype_name;F.linepsc"(** [default_%s ()] is a new empty value for type [%s] *)"type_nametype_nameinlet{Ot.spec;_}=tinlethas_encoded=matchspecwith|Ot.Recordr->gen_sig_recordscr;true|Ot.Variantv->gen_default_fun_v.Ot.v_name;true|Ot.Const_variant{Ot.cv_name;_}->gen_default_fun_cv_name;true|Ot.Unitu->gen_sig_unitscu;trueinhas_encodedletocamldoc_title="Basic values"