Module Ppx_deriving
Public API of ppx_deriving executable.
Registration
type deriver={}A type of deriving plugins.
A structure or signature deriving function accepts a list of
~options, a~pathof modules for the type declaration currently being processed (with[]for toplevel phrases), and a type declaration item (type t = .. and t' = ..), and returns a list of items to be appended after the type declaration item in structure and signature. It is invoked by[\@\@deriving]annotations.A type deriving function accepts a type and returns a corresponding derived expression. It is invoked by
[%derive.foo:]and[%foo:]annotations. If this function is missing, the corresponding[%foo:]annotation is ignored.The structure and signature deriving functions are invoked in the order in which they appear in the source code.
val register : deriver -> unitregister deriverregistersderiveraccording to itsnamefield.
val add_register_hook : (deriver -> unit) -> unitadd_register_hook hookaddshookto be executed whenever a new deriver is registered.
val derivers : unit -> deriver listderivers ()returns all currently registered derivers.
val create : string -> ?core_type:(Parsetree.core_type -> Parsetree.expression) -> ?type_ext_str:(options:(string * Parsetree.expression) list -> path:string list -> Parsetree.type_extension -> Parsetree.structure) -> ?type_ext_sig:(options:(string * Parsetree.expression) list -> path:string list -> Parsetree.type_extension -> Parsetree.signature) -> ?type_decl_str:(options:(string * Parsetree.expression) list -> path:string list -> Parsetree.type_declaration list -> Parsetree.structure) -> ?type_decl_sig:(options:(string * Parsetree.expression) list -> path:string list -> Parsetree.type_declaration list -> Parsetree.signature) -> ?module_type_decl_str:(options:(string * Parsetree.expression) list -> path:string list -> Parsetree.module_type_declaration -> Parsetree.structure) -> ?module_type_decl_sig:(options:(string * Parsetree.expression) list -> path:string list -> Parsetree.module_type_declaration -> Parsetree.signature) -> unit -> deriverCreating
deriverstructure.
val lookup : string -> deriver optionlookup namelooks up a deriver calledname.
Option parsing
Hygiene
val create_quoter : unit -> quoterquoter ()creates an empty quoter.
val quote : quoter:quoter -> Parsetree.expression -> Parsetree.expressionquote quoter exprrecords a pure expressionexprwithinquoterand returns an expression which has the same value asexprin the context thatsanitizeprovides.
val sanitize : ?module_:Longident.t -> ?quoter:quoter -> Parsetree.expression -> Parsetree.expressionsanitize module_ quoter exprwrapsexprin a way that ensures that the contents ofmodule_andPervasives, as well as the identifiers in expressions returned byquoteare in scope, and returns the wrapped expression.module_defaults toPpx_deriving_runtimeif it's not provided
val with_quoter : (quoter -> 'a -> Parsetree.expression) -> 'a -> Parsetree.expressionwith_quoter fn≡fun fn a -> let quoter = create_quoter () in sanitize ~quoter (fn quoter a)
AST manipulation
val expand_path : path:string list -> string -> stringexpand_path namereturnsnamewith thepathmodule path prepended, e.g.expand_path ["Foo";"M"] "t"="Foo.M.t"andexpand_path [] "t"="t"
val path_of_type_decl : path:string list -> Parsetree.type_declaration -> string listpath_of_type_decl ~path type_returnspathiftype_does not have a manifest or the manifest is not a constructor, and the module path of manifest otherwise.path_of_type_declis useful when determining the canonical path location of fields and constructors; e.g. fortype bar = M.foo = A | B, it will return["M"].
val mangle_type_decl : ?fixpoint:string -> [ `Prefix of string | `Suffix of string | `PrefixSuffix of string * string ] -> Parsetree.type_declaration -> stringmangle_type_decl ~fixpoint affix type_derives a function name fromtype_name by doing nothing iftype_is namedfixpoint("t"by default), or appending and/or prependingaffixvia an underscore.
val mangle_lid : ?fixpoint:string -> [ `Prefix of string | `Suffix of string | `PrefixSuffix of string * string ] -> Longident.t -> Longident.tmangle_lid ~fixpoint affix liddoes the same asmangle_type_decl, but for the last component oflid.
val attr : deriver:string -> string -> Parsetree.attributes -> Parsetree.attribute optionattr ~deriver name attrssearches for an attribute[\@deriving.deriver.attr]inattrsif any attribute with name starting with\@deriving.deriverexists, or[\@deriver.attr]if any attribute with name starting with\@deriverexists, or[\@attr]otherwise.
val attr_warning : Parsetree.expression -> Parsetree.attributeattr_warning exprbuilds the attribute\@ocaml.warning expr
val free_vars_in_core_type : Parsetree.core_type -> tyvar listfree_vars_in_core_type typreturns unique free variables intypin lexical order.
val remove_pervasives : deriver:string -> Parsetree.core_type -> Parsetree.core_typeremove_pervasives ~deriver typremoves the leading "Pervasives." module name in longidents. Type expressions marked with[\@nobuiltin]are ignored.The name of the deriving plugin should be passed as
deriver; it is used in error messages.
val fresh_var : string list -> stringfresh_var boundreturns a fresh variable name not present inbound. The name is selected in alphabetical succession.
val fold_left_type_decl : ('a -> tyvar -> 'a) -> 'a -> Parsetree.type_declaration -> 'afold_left_type_decl fn accum type_performs a left fold over all type variable (i.e. not wildcard) parameters intype_.
val fold_right_type_decl : (tyvar -> 'a -> 'a) -> Parsetree.type_declaration -> 'a -> 'afold_right_type_decl fn accum type_performs a right fold over all type variable (i.e. not wildcard) parameters intype_.
val fold_left_type_ext : ('a -> tyvar -> 'a) -> 'a -> Parsetree.type_extension -> 'afold_left_type_ext fn accum type_performs a left fold over all type variable (i.e. not wildcard) parameters intype_.
val fold_right_type_ext : (tyvar -> 'a -> 'a) -> Parsetree.type_extension -> 'a -> 'afold_right_type_ext fn accum type_performs a right fold over all type variable (i.e. not wildcard) parameters intype_.
val poly_fun_of_type_decl : Parsetree.type_declaration -> Parsetree.expression -> Parsetree.expressionpoly_fun_of_type_decl type_ exprwrapsexprintofun poly_N -> ...for every type parameter'Npresent intype_. For example, iftype_refers totype ('a, 'b) map,exprwill be wrapped intofun poly_a poly_b -> [%e expr]._parameters are ignored.
val poly_fun_of_type_ext : Parsetree.type_extension -> Parsetree.expression -> Parsetree.expressionSame as
poly_fun_of_type_declbut for type extension.
val poly_apply_of_type_decl : Parsetree.type_declaration -> Parsetree.expression -> Parsetree.expressionpoly_apply_of_type_decl type_ exprwrapsexprintoexpr poly_Nfor every type parameter'Npresent intype_. For example, iftype_refers totype ('a, 'b) map,exprwill be wrapped into[%e expr] poly_a poly_b._parameters are ignored.
val poly_apply_of_type_ext : Parsetree.type_extension -> Parsetree.expression -> Parsetree.expressionSame as
poly_apply_of_type_declbut for type extension.
val poly_arrow_of_type_decl : (Parsetree.core_type -> Parsetree.core_type) -> Parsetree.type_declaration -> Parsetree.core_type -> Parsetree.core_typepoly_arrow_of_type_decl fn type_ typwrapstypin an arrow withfn [%type: 'N]as argument for every type parameter'Npresent intype_. For example, iftype_refers totype ('a, 'b) mapandfnisfun var -> [%type: [%t var] -> string],typwill be wrapped into('a -> string) -> ('b -> string) -> [%t typ]._parameters are ignored.
val poly_arrow_of_type_ext : (Parsetree.core_type -> Parsetree.core_type) -> Parsetree.type_extension -> Parsetree.core_type -> Parsetree.core_typeSame as
poly_arrow_of_type_declbut for type extension.
val core_type_of_type_decl : Parsetree.type_declaration -> Parsetree.core_typecore_type_of_type_decl type_constructs type('a, 'b, ...) tfor type declarationtype ('a, 'b, ...) t = ....
val core_type_of_type_ext : Parsetree.type_extension -> Parsetree.core_typeSame as
core_type_of_type_declbut for type extension.
val instantiate : string list -> Parsetree.type_declaration -> Parsetree.core_type * string list * string listinstantiate bound type_returnstyp, vars, bound'wheretypis a type instantiated from type declarationtype_,vars≡free_vars_in_core_type typandbound'≡bound @ vars.
val fold_exprs : ?unit:Parsetree.expression -> (Parsetree.expression -> Parsetree.expression -> Parsetree.expression) -> Parsetree.expression list -> Parsetree.expressionfold_exprs ~unit fn exprsfoldsexprsusing head ofexprsas initial accumulator value, orunitifexprs = [].See also
seq_reduceandbinop_reduce.
val seq_reduce : ?sep:Parsetree.expression -> Parsetree.expression -> Parsetree.expression -> Parsetree.expressionWhen
sepis present:seq_reduce≡fun x a b -> [%expr [%e a]; [%e x]; [%e b]]. Whensepis missing:seq_reduce≡fun a b -> [%expr [%e a]; [%e b]].
val binop_reduce : Parsetree.expression -> Parsetree.expression -> Parsetree.expression -> Parsetree.expressionbinop_reduce≡fun x a b -> [%expr [%e x] [%e a] [%e b]].