package ppxlib

  1. Overview
  2. Docs
Standard infrastructure for ppx rewriters

Install

Dune Dependency

Authors

Maintainers

Sources

757f6c284b1fe748d5027eef3bbef924b6bbd7ce.tar.gz
sha256=89a98c95ddd0bfbac17b5a936f6811af7097be3258c482d5859b73e9de9b4552
sha512=b19306473d867252d382e58e9b697531c5edccdc9283b5eaf72f524803c2fca2a58a5e8f25bee198b00de82cf8ef805b43f7488791c3ac5beb0ffba938ded826

doc/ppxlib/Ppxlib/Ast_builder/Make/index.html

Module Ast_builder.MakeSource

Build Ast helpers with the location argument factorized.

Parameters

module Loc : Loc

Signature

Sourcemodule Located : sig ... end

Value description

Sourceval value_description : name:string Astlib.Location.loc -> type_:Astlib.Ast_502.Parsetree.core_type -> prim:string list -> Astlib.Ast_502.Parsetree.value_description

value_description constructs an Ast.value_description

Example OCaml

Values of type value_description represents:

  • val x: T, when pval_prim is []
  • external x: T = "s1" ... "sn" when pval_prim is ["s1";..."sn"]

Value binding

Type extension

type_extension constructs an Ast.type_extension

Example OCaml

Definition of new extensions constructors for the extensive sum type t (type t += ...).

Type exception

type_exception constructs an Ast.type_exception

Example OCaml

Definition of a new exception (exception E).

Type declaration

type_declaration constructs an Ast.type_declaration

Example OCaml

Here are type declarations and their representation, for various ptype_kind and ptype_manifest values:

  • type t when type_kind is Ptype_abstract, and manifest is None,
  • type t = T0 when type_kind is Ptype_abstract, and manifest is Some T0,
  • type t = C of T | ... when type_kind is Ptype_variant, and manifest is None,
  • type t = T0 = C of T | ... when type_kind is Ptype_variant, and manifest is Some T0,
  • type t = {l: T; ...} when type_kind is Ptype_record, and manifest is None,
  • type t = T0 = {l : T; ...} when type_kind is Ptype_record, and manifest is Some T0,
  • type t = .. when type_kind is Ptype_open, and manifest is None.

Toplevel directive

toplevel_directive constructs an Ast.toplevel_directive

Structure item

pstr_class_type constructs an Ast.structure_item_desc.Pstr_class_type

Example OCaml

class type ct1 = ... and ... and ctn = ...

pstr_class constructs an Ast.structure_item_desc.Pstr_class

Example OCaml

class c1 = ... and ... and cn = ...

pstr_recmodule constructs an Ast.structure_item_desc.Pstr_recmodule

Example OCaml

module rec X1 = ME1 and ... and Xn = MEn

pstr_exception constructs an Ast.structure_item_desc.Pstr_exception

Example OCaml

  • exception C of T
  • exception C = M.X

pstr_primitive constructs an Ast.structure_item_desc.Pstr_primitive

Example OCaml

  • val x: T
  • external x: T = "s1" ... "sn"

pstr_value constructs an Ast.structure_item_desc.Pstr_value

Example OCaml

Pstr_value(rec, [(P1, E1 ; ... ; (Pn, En))]) represents:

  • let P1 = E1 and ... and Pn = EN when rec is Nonrecursive,
  • let rec P1 = E1 and ... and Pn = EN when rec is Recursive.

Signature item

psig_class_type constructs an Ast.signature_item_desc.Psig_class_type

Example OCaml

class type ct1 = ... and ... and ctn = ...

psig_class constructs an Ast.signature_item_desc.Psig_class

Example OCaml

class c1 : ... and ... and cn : ...

psig_modtypesubst constructs an Ast.signature_item_desc.Psig_modtypesubst

Example OCaml

module type S := ...

psig_modtype constructs an Ast.signature_item_desc.Psig_modtype

Example OCaml

module type S = MT and module type S

psig_recmodule constructs an Ast.signature_item_desc.Psig_recmodule

Example OCaml

module rec X1 : MT1 and ... and Xn : MTn

psig_module constructs an Ast.signature_item_desc.Psig_module

Example OCaml

module X = M and module X : MT

psig_exception constructs an Ast.signature_item_desc.Psig_exception

Example OCaml

exception C of T

psig_typesubst constructs an Ast.signature_item_desc.Psig_typesubst

Example OCaml

type t1 := ... and ... and tn := ...

psig_value constructs an Ast.signature_item_desc.Psig_value

Example OCaml

  • val x: T
  • external x: T = "s1" ... "sn"

Row field

rtag constructs an Ast.row_field_desc.Rtag

Example OCaml

Rtag(`A, b, l) represents:

  • `A when b is true and l is [],
  • `A of T when b is false and l is [T],
  • `A of T1 & .. & Tn when b is false and l is [T1;...Tn],
  • `A of & T1 & .. & Tn when b is true and l is [T1;...Tn].
  • The bool field is true if the tag contains a constant (empty) constructor.
  • & occurs when several types are used for the same constructor (see 4.2 in the manual)

Position

Sourceval position : fname:string -> lnum:int -> bol:int -> cnum:int -> Lexing.position

position constructs an Ast.position

Pattern

ppat_extension constructs an Ast.pattern_desc.Ppat_extension

Example OCaml

Pattern [%id]

ppat_exception constructs an Ast.pattern_desc.Ppat_exception

Example OCaml

Pattern exception P

ppat_unpack constructs an Ast.pattern_desc.Ppat_unpack

Example OCaml

Ppat_unpack(s) represents:

  • (module P) when s is Some "P"
  • (module _) when s is None

Note: (module P : S) is represented as Ppat_constraint(Ppat_unpack(Some "P"), Ptyp_package S)

ppat_lazy constructs an Ast.pattern_desc.Ppat_lazy

Example OCaml

Pattern lazy P

ppat_type constructs an Ast.pattern_desc.Ppat_type

Example OCaml

Pattern #tconst

ppat_array constructs an Ast.pattern_desc.Ppat_array

Example OCaml

Pattern [| P1; ...; Pn |]

ppat_record constructs an Ast.pattern_desc.Ppat_record

Example OCaml

Ppat_record([(l1, P1) ; ... ; (ln, Pn)], flag) represents:

  • { l1=P1; ...; ln=Pn } when flag is Closed
  • { l1=P1; ...; ln=Pn; _} when flag is Open

Invariant: n > 0

ppat_variant constructs an Ast.pattern_desc.Ppat_variant

Example OCaml

Ppat_variant(`A, pat) represents:

  • `A when pat is None,
  • `A P when pat is Some P

ppat_tuple constructs an Ast.pattern_desc.Ppat_tuple

Example OCaml

Patterns (P1, ..., Pn).

Invariant: n >= 2

ppat_interval constructs an Ast.pattern_desc.Ppat_interval

Example OCaml

Patterns such as 'a'..'z'.

Other forms of interval are recognized by the parser but rejected by the type-checker.

ppat_constant constructs an Ast.pattern_desc.Ppat_constant

Example OCaml

Patterns such as 1, 'a', "true", 1.0, 1l, 1L, 1n

ppat_alias constructs an Ast.pattern_desc.Ppat_alias

Example OCaml

An alias pattern such as P as 'a

ppat_var constructs an Ast.pattern_desc.Ppat_var

Example OCaml

A variable pattern such as x

ppat_any constructs an Ast.pattern_desc.Ppat_any

Example OCaml

The pattern _.

Object field

Module type declaration

module_type_declaration constructs an Ast.module_type_declaration

Example OCaml

Values of type module_type_declaration represents:

  • S = MT,
  • S for abstract module type declaration, when pmtd_type is None.

Module type

pmty_typeof constructs an Ast.module_type_desc.Pmty_typeof

Example OCaml

module type of ME

pmty_signature constructs an Ast.module_type_desc.Pmty_signature

Example OCaml

sig ... end

pmty_ident constructs an Ast.module_type_desc.Pmty_ident

Example OCaml

Pmty_ident(S) represents S

Module substitution

module_substitution constructs an Ast.module_substitution

Example OCaml

Values of type module_substitution represents S := M

Module expr

pmod_structure constructs an Ast.module_expr_desc.Pmod_structure

Example OCaml

struct ... end

Module declaration

module_declaration constructs an Ast.module_declaration

Example OCaml

Values of type module_declaration represents S : MT

Module binding

module_binding constructs an Ast.module_binding

Example OCaml

Values of type module_binding represents module X = ME

Location

Sourceval location : start:Lexing.position -> end_:Lexing.position -> ghost:bool -> Astlib.Location.t

location constructs an Ast.location

Letop

Label declaration

label_declaration constructs an Ast.label_declaration

Example OCaml

Note: T can be a Ptyp_poly.

Function param

pparam_newtype constructs an Ast.function_param_desc.Pparam_newtype

Example OCaml

Pparam_newtype x represents the parameter (type x). x carries the location of the identifier, whereas the pparam_loc on the enclosing function_param node is the location of the (type x) as a whole.

Multiple parameters (type a b c) are represented as multiple Pparam_newtype nodes, let's say:

  [
    { pparam_kind = Pparam_newtype a; pparam_loc = loc1 };
    { pparam_kind = Pparam_newtype b; pparam_loc = loc2 };
    { pparam_kind = Pparam_newtype c; pparam_loc = loc3 };
  ]

Here, the first loc loc1 is the location of (type a b c), and the subsequent locs loc2 and loc3 are the same as loc1, except marked as ghost locations. The locations on a, b, c, correspond to the variables a, b, and c in the source code.

pparam_val constructs an Ast.function_param_desc.Pparam_val

Example OCaml

Pparam_val (lbl, exp0, P) represents the parameter:

Note: If E0 is provided, only Optional is allowed.

Extension constructor

Expression

pexp_unreachable constructs an Ast.expression_desc.Pexp_unreachable

Example OCaml

.

pexp_extension constructs an Ast.expression_desc.Pexp_extension

Example OCaml

[%id]

pexp_letop constructs an Ast.expression_desc.Pexp_letop

Example OCaml

  • let* P = E0 in E1
  • let* P0 = E00 and* P1 = E01 in E1

pexp_pack constructs an Ast.expression_desc.Pexp_pack

Example OCaml

(module ME).

(module ME : S) is represented as Pexp_constraint(Pexp_pack ME, Ptyp_package S)

pexp_newtype constructs an Ast.expression_desc.Pexp_newtype

Example OCaml

fun (type t) -> E

pexp_object constructs an Ast.expression_desc.Pexp_object

Example OCaml

object ... end

pexp_poly constructs an Ast.expression_desc.Pexp_poly

Example OCaml

Used for method bodies.

Can only be used as the expression under Cfk_concrete for methods (not values).

pexp_assert constructs an Ast.expression_desc.Pexp_assert

Example OCaml

assert E.

Note: assert false is treated in a special way by the type-checker.

pexp_letmodule constructs an Ast.expression_desc.Pexp_letmodule

Example OCaml

let module M = ME in E

pexp_override constructs an Ast.expression_desc.Pexp_override

Example OCaml

{< x1 = E1; ...; xn = En >}

pexp_coerce constructs an Ast.expression_desc.Pexp_coerce

Example OCaml

Pexp_coerce(E, from, T) represents

  • (E :> T) when from is None,
  • (E : T0 :> T) when from is Some T0.

pexp_for constructs an Ast.expression_desc.Pexp_for

Example OCaml

Pexp_for(i, E1, E2, direction, E3) represents:

  • for i = E1 to E2 do E3 done when direction is Upto
  • for i = E1 downto E2 do E3 done when direction is Downto

pexp_array constructs an Ast.expression_desc.Pexp_array

Example OCaml

[| E1; ...; En |]

pexp_record constructs an Ast.expression_desc.Pexp_record

Example OCaml

Pexp_record([(l1,P1) ; ... ; (ln,Pn)], exp0) represents

  • { l1=P1; ...; ln=Pn } when exp0 is None
  • { E0 with l1=P1; ...; ln=Pn } when exp0 is Some E0

Invariant: n > 0

pexp_variant constructs an Ast.expression_desc.Pexp_variant

Example OCaml

Pexp_variant(`A, exp) represents

  • `A when exp is None
  • `A E when exp is Some E

pexp_construct constructs an Ast.expression_desc.Pexp_construct

Example OCaml

Pexp_construct(C, exp) represents:

  • C when exp is None,
  • C E when exp is Some E,
  • C (E1, ..., En) when exp is Some (Pexp_tuple[E1;...;En])

pexp_tuple constructs an Ast.expression_desc.Pexp_tuple

Example OCaml

Expressions (E1, ..., En)

Invariant: n >= 2

pexp_try constructs an Ast.expression_desc.Pexp_try

Example OCaml

try E0 with P1 -> E1 | ... | Pn -> En

pexp_match constructs an Ast.expression_desc.Pexp_match

Example OCaml

match E0 with P1 -> E1 | ... | Pn -> En

pexp_apply constructs an Ast.expression_desc.Pexp_apply

Example OCaml

Pexp_apply(E0, [(l1, E1) ; ... ; (ln, En)]) represents E0 ~l1:E1 ... ~ln:En

li can be Nolabel (non labeled argument), Labelled (labelled arguments) or Optional (optional argument).

Invariant: n > 0

pexp_let constructs an Ast.expression_desc.Pexp_let

Example OCaml

Pexp_let(flag, [(P1,E1) ; ... ; (Pn,En)], E) represents:

  • let P1 = E1 and ... and Pn = EN in E when flag is Nonrecursive,
  • let rec P1 = E1 and ... and Pn = EN in E when flag is Recursive.

pexp_constant constructs an Ast.expression_desc.Pexp_constant

Example OCaml

Expressions constant such as 1, 'a', "true", 1.0, 1l, 1L, 1n

pexp_ident constructs an Ast.expression_desc.Pexp_ident

Example OCaml

Identifiers such as x and M.x

Directive argument

Sourceval pdir_int : string -> char option -> Astlib.Ast_502.Parsetree.directive_argument

Core type

ptyp_extension constructs an Ast.core_type_desc.Ptyp_extension

Example OCaml

[%id].

ptyp_poly constructs an Ast.core_type_desc.Ptyp_poly

Example OCaml

'a1 ... 'an. T

Can only appear in the following context:

  let x : 'a1 ... 'an. T = e ...

ptyp_variant constructs an Ast.core_type_desc.Ptyp_variant

Example OCaml

Ptyp_variant([`A;`B], flag, labels) represents:

  • [ `A|`B ] when flag is Closed, and labels is None,
  • [> `A|`B ] when flag is Open, and labels is None,
  • [< `A|`B ] when flag is Closed, and labels is Some [],
  • [< `A|`B > `X `Y ] when flag is Closed, and labels is Some ["X";"Y"].

ptyp_class constructs an Ast.core_type_desc.Ptyp_class

Example OCaml

Ptyp_class(tconstr, l) represents:

  • #tconstr when l=[],
  • T #tconstr when l=[T],
  • (T1, ..., Tn) #tconstr when l=[T1 ; ... ; Tn].

ptyp_object constructs an Ast.core_type_desc.Ptyp_object

Example OCaml

Ptyp_object([ l1:T1; ...; ln:Tn ], flag) represents:

  • < l1:T1; ...; ln:Tn > when flag is Closed,
  • < l1:T1; ...; ln:Tn; .. > when flag is Open.

ptyp_constr constructs an Ast.core_type_desc.Ptyp_constr

Example OCaml

Ptyp_constr(lident, l) represents:

  • tconstr when l=[],
  • T tconstr when l=[T],
  • (T1, ..., Tn) tconstr when l=[T1 ; ... ; Tn].

ptyp_tuple constructs an Ast.core_type_desc.Ptyp_tuple

Example OCaml

Ptyp_tuple([T1 ; ... ; Tn]) represents a product type T1 * ... * Tn.

Invariant: n >= 2.

ptyp_arrow constructs an Ast.core_type_desc.Ptyp_arrow

Example OCaml

Ptyp_arrow(lbl, T1, T2) represents:

ptyp_var constructs an Ast.core_type_desc.Ptyp_var

Example OCaml

A type variable such as 'a

ptyp_any constructs an Ast.core_type_desc.Ptyp_any

Example OCaml

_

Constructor declaration

Class type field

Class type

pcty_arrow constructs an Ast.class_type_desc.Pcty_arrow

Example OCaml

Pcty_arrow(lbl, T, CT) represents:

pcty_signature constructs an Ast.class_type_desc.Pcty_signature

Example OCaml

object ... end

Class structure

class_structure constructs an Ast.class_structure

Example OCaml

Values of type class_structure represents:

Class signature

class_signature constructs an Ast.class_signature

Example OCaml

Values of type class_signature represents:

Class field

pcf_attribute constructs an Ast.class_field_desc.Pcf_attribute

Example OCaml

[\@\@\@id]

pcf_initializer constructs an Ast.class_field_desc.Pcf_initializer

Example OCaml

initializer E

pcf_method constructs an Ast.class_field_desc.Pcf_method

Example OCaml

pcf_val constructs an Ast.class_field_desc.Pcf_val

Example OCaml

Pcf_val(x,flag, kind) represents:

pcf_inherit constructs an Ast.class_field_desc.Pcf_inherit

Example OCaml

Pcf_inherit(flag, CE, s) represents:

  • inherit CE when flag is Fresh and s is None,
  • inherit CE as x when flag is Fresh and s is Some x,
  • inherit! CE when flag is Override and s is None,
  • inherit! CE as x when flag is Override and s is Some x

Class expr

pcl_let constructs an Ast.class_expr_desc.Pcl_let

Example OCaml

Pcl_let(rec, [(P1, E1); ... ; (Pn, En)], CE) represents:

  • let P1 = E1 and ... and Pn = EN in CE when rec is Nonrecursive,
  • let rec P1 = E1 and ... and Pn = EN in CE when rec is Recursive.

pcl_apply constructs an Ast.class_expr_desc.Pcl_apply

Example OCaml

Pcl_apply(CE, [(l1,E1) ; ... ; (ln,En)]) represents CE ~l1:E1 ... ~ln:En. li can be empty (non labeled argument) or start with ? (optional argument).

Invariant: n > 0

pcl_fun constructs an Ast.class_expr_desc.Pcl_fun

Example OCaml

Pcl_fun(lbl, exp0, P, CE) represents:

  • fun P -> CE when lbl is Nolabel and exp0 is None,
  • fun ~l:P -> CE when lbl is Labelled l and exp0 is None,
  • fun ?l:P -> CE when lbl is Optional l and exp0 is None,
  • fun ?l:(P = E0) -> CE when lbl is Optional l and exp0 is Some E0.

pcl_structure constructs an Ast.class_expr_desc.Pcl_structure

Example OCaml

object ... end

Case

case constructs an Ast.case

Example OCaml

Values of type case represents (P -> E) or (P when E0 -> E)

Binding op

Attribute

attribute constructs an Ast.attribute

Example OCaml

Attributes such as [\@id ARG] and [\@\@id ARG].

Metadata containers passed around within the AST. The compiler ignores unknown attributes.

'a open infos

open_infos constructs an Ast.'aopen_infos

Example OCaml

Values of type 'a open_infos represents:

'a include infos

include_infos constructs an Ast.'ainclude_infos

'a class infos

class_infos constructs an Ast.'aclass_infos

Example OCaml

Values of type class_expr class_infos represents:

  • class c = ...
  • class ['a1,...,'an] c = ...
  • class virtual c = ...

They are also used for "class type" declaration.

Sourceval enativeint : nativeint -> Astlib.Ast_502.Parsetree.expression
Sourceval pnativeint : nativeint -> Astlib.Ast_502.Parsetree.pattern

evar id produces a Pexp_ident _ expression, it parses its input so you can pass any dot-separated identifier, for instance: evar ~loc "Foo.bar".

pexp_fun can be used to create function expressions. It will check if the function's body is itself a function expression and if so it will coalesce the arguments.

For example, if we have pexp_fun Nolabel None (var "x") f and f is fun y -> x + y then the function expression returned will be fun x y -> x + y and not fun x -> y -> x + y. However, it will be more efficient to create maximum arity functions directly with pexp_function.

pexp_function_cases builds an expression in the shape function C1 -> E1 | ....

elist_tail ~loc [expr1; expr2; expr3] expr_tail produces the expression expr1::expr2::expr3::expr_tail.

elist ~loc [expr1; expr2; expr3] produces the list litteral expression [expr1; expr2; expr3].

plist_tail ~loc [pat1; pat2; pat3] pat_tail produces the pattern pat1::pat2::pat3::pat_tail.

plist ~loc [pat1; pat2; pat3] produces the list pattern [pat1; pat2; pat3].

pstr_value_list ~loc rf vbs = pstr_value ~loc rf vbs if vbs <> [], [] otherwise.

  • deprecated [since 2016-10] use Nonrecursive on the P(str|sig)_type instead
Sourceval unapplied_type_constr_conv : Longident.t Loc.t -> f:(string -> string) -> Astlib.Ast_502.Parsetree.expression

unapplied_type_constr_conv is the standard way to map identifiers to conversion fonctions, for preprocessor that creates values that follow the structure of types. More precisely, path_conv path (sprintf "sexp_of_%s") is:

  • sexp_of_t if path is "t"
  • A.B.sexp_of_foo if path is "A.B.foo"
  • A.B.sexp_of_f__foo (module A1) (module A2) if path is "A.B.F(A1)(A2).foo" type_constr_conv also applies it to a list of expression, which both prevents the compiler from allocating useless closures, and almost always what is needed, since type constructors are always applied.

Tries to simplify fun v1 v2 .. -> f v1 v2 .. into f. Only works when f is a path, not an arbitrary expression as that would change the meaning of the code. This can be used either for cleaning up the generated code, or to reduce allocation if f is a local variable (the compiler won't optimize the allocation of the closure).

Eta-reduction can change the types/behavior in some corner cases that are unlikely to show up in generated code:

  • if f has optional arguments, eta-expanding f can drop them
  • because labels commute, it can change the type of an expression: $ let f ~x y = x + y let f2 = fun x -> add x;; val f : x:int -> int -> int = <fun> val f2 : int -> x:int -> int = <fun> In fact, if f does side effects before receiving all its arguments, and if the eta-expansion is partially applied, eta-reducing could change behavior.

eta_reduce_if_possible_and_nonrec is meant for the case where the resulting expression is going to be bound in a potentially recursive let-binding, where we have to keep the eta-expansion when rec_flag is Recursive to avoid a compile error.

Sourcemodule Latest : sig ... end

This module contains updated versions of node constructors that were kept stable when the node changed. For every function in this module, there's an equally-named function outside this module. The function outside this module will stay stable, whereas the function inside this module will adapt potential upcoming new compiler features. Only use a function in this module, if the equally-named one outside this module is missing a feature you need.

OCaml

Innovation. Community. Security.