[][src]Module c2rust_refactor::matcher

AST pattern matching implementation.

The matching in this module allows matching one AST fragment against another fragment of the same type. The "pattern" fragment can use some special forms to capture parts of the target AST, or to impose additional requirements on the matching.

Matching forms:

  • __x: An ident starting with double underscores will capture the AST matched against it, and save it in the Bindings. Other idents can also be used if the MatchCtxt is appropriately configured first.

    By default, this form captures the largest AST that it encounters. For example, if the target AST is MyStruct, it will try first to capture the entire Expr, then try the inner Path, then the innermost Ident. Normally the first attempt succeeds, but if a type is set for the ident in the MatchCtxt, then it will only capture that type.

    For itemlikes, a lone ident can't be used as a pattern because it's not a valid itemlike. Use a zero-argument macro invocation __x!() instead.

  • marked!(x [, label]): Matches x only if the node is marked with the given label. The label defaults to "target" if omitted.

  • def!(path): Matches a path Expr or Ty that refers to a definition whose absolute path is path. Specifically, the path of the definition is converted back to an AST using the reflect module, and the new AST is matched against path.

  • typed!(x, ty): Matches an Expr or Ty whose resolved type matches ty. Specifically, the resolved type of the node is converted back to an AST using the reflect module, and the new AST is matched against ty.

  • cast!(x): Matches the Exprs x, x as __t, x as __t as __u, etc.

Structs

BindingTypes

A set of binding types, mapping names to binding types.

Bindings

A set of bindings, mapping names to AST fragments.

ExprPatternFolder

Automatically generated Folder implementation, for use by Pattern.

MatchCtxt

Pattern-matching context. Stores configuration that affects pattern matching behavior, and collects bindings captured during the match.

MultiStmtPatternFolder

Custom Folder for multi-statement Patterns.

StmtPatternFolder

Automatically generated Folder implementation, for use by Pattern.

TyPatternFolder

Automatically generated Folder implementation, for use by Pattern.

Enums

BindingType

The types of AST fragments that can be used in Bindings.

Error

Traits

Pattern

Trait for AST types that can be used as patterns in a search-and-replace (mut_visit_match).

Subst
TryMatch

Functions

find_first

Find the first place where pattern matches, and return the resulting Bindings.

find_first_with

Find the first place where pattern matches under initial context init_mcx, and return the resulting Bindings.

flat_map_match_with
match_multi_stmt
mut_visit_match

Find every match for pattern within target, and rewrite each one by invoking callback.

mut_visit_match_with

Find every match for pattern within target, and rewrite each one by invoking callback.

parse_bindings
replace_expr

Replace all instances of expression pat with expression repl.

replace_stmts

Replace all instances of the statement sequence pat with repl.

Type Definitions

Result