// This file describes nearly all Rust AST types. It is used by gen/process_ast.py to generate
// impls of various traits for all AST nodes.
//
// There are three types of declarations that can appear in this file.
// - `struct`s: Similar to Rust struct declarations, except that only field names are given, not
// their types. Both "normal" and tuple structs are supported. In normal structs declarations,
// the field names must match the actual names of the fields. For tuple structs, names must
// still be provided, but they can be chosen arbitrarily (except they must be valid Rust
// identifiers, i.e., not keywords).
// - `enum`s: Similar to Rust enum declarations. Each variant follows the same format as a struct
// declaration. Both tuple-like and struct-like variants are supported.
// - `flag`s: These indicate types with no interesting internal structure, such as `Mutability` (an
// enum with two nullary variants, `Mutable` and `Immutable`). The code generators will either
// ignore these or use a simple default implementation.
//
// Top-level declarations, enum variants, and struct/variant fields may all be prefixed with
// attributes. The attribute format is `#[attr]` or `#[key=value]` (where `value` is a single
// word). Consult the doc comments for the code generator modules for information on the supported
// attributes and their effects.
struct Crate {
module,
#[seq_rewrite_outer_span='calc_outer_span(&self.attrs, self.span.shrink_to_lo())']
attrs,
span,
}
// Ignore inline because we flip it from false to true when printing (see
// `<Item as PrintParse>::to_string`).
struct Mod { inner, #[mac_table_seq] items, #[rewrite_ignore] inline }
#[rewrite_print_recover] #[rewrite_seq_item] #[rewrite_extra_strategies=item_header]
#[nonterminal] #[extend_span]
struct Item { ident, #[match=ignore] attrs, id, node, vis, span,
#[match=ignore] #[rewrite_ignore] tokens }
enum ItemKind {
ExternCrate(name),
Use(vp),
Static(ty, mutbl, init),
Const(ty, init),
Fn(decl, header, generics, block),
Mod(module),
ForeignMod(fm),
GlobalAsm(asm),
Ty(ty, generics),
Existential(bounds, generics),
Enum(def, generics),
Struct(vd, generics),
Union(vd, generics),
Trait(is_auto, unsafety, generics, bounds, #[mac_table_seq] items),
Impl(unsafety, polarity, generics, defaultness, trait_ref, ty, #[mac_table_seq] items),
Mac(mac),
MacroDef(tts),
TraitAlias(generics,bounds),
}
enum UseTreeKind {
Simple(ident, id1, id2),
Glob,
Nested(nested),
}
struct UseTree { kind, prefix, span }
#[nonterminal] #[extend_span]
struct TraitItem { id, ident, #[match=ignore] attrs, generics, node, span,
#[match=ignore] #[rewrite_ignore] tokens }
enum TraitItemKind {
Const(ty, init),
Method(sig, body),
Type(bounds, ty),
Macro(mac),
}
#[nonterminal] #[extend_span]
struct ImplItem { id, ident, vis, defaultness, #[match=ignore] attrs, generics, node, span,
#[match=ignore] #[rewrite_ignore] tokens }
enum ImplItemKind {
Const(ty, init),
Method(sig, body),
Type(ty),
Existential(bounds),
Macro(mac),
}
struct TraitRef { path, ref_id }
struct EnumDef { variants }
#[extend_span]
struct Variant_ { ident, #[match=ignore] attrs, id, data, disr_expr }
enum VariantData {
Struct(fields, id),
Tuple(fields, id),
Unit(id),
}
#[extend_span]
struct StructField { span, ident, vis, id, ty, #[match=ignore] attrs }
struct MethodSig { header, decl }
struct ForeignMod { abi, #[mac_table_seq] items }
#[rewrite_print_recover] #[rewrite_seq_item] #[nonterminal] #[extend_span]
struct ForeignItem { ident, #[match=ignore] attrs, node, id, span, vis }
enum ForeignItemKind {
Fn(decl, generics),
Static(ty, mutbl),
Ty,
Macro(mac),
}
#[nonterminal]
struct Generics { params, where_clause, span }
#[extend_span]
struct GenericParam { id, ident, attrs, bounds, kind }
enum GenericParamKind {
Lifetime,
Type { default },
Const { ty },
}
#[nonterminal]
struct WhereClause { id, predicates, span }
enum WherePredicate {
BoundPredicate(pred),
RegionPredicate(pred),
EqPredicate(pred),
}
struct WhereBoundPredicate { span, bounded_ty, bounds, bound_generic_params }
struct WhereRegionPredicate { span, lifetime, bounds }
struct WhereEqPredicate { id, span, lhs_ty, rhs_ty }
flag TraitBoundModifier;
#[match=ignore]
enum VisibilityKind {
Public,
Crate(crate_sugar),
Restricted { path, id },
Inherited,
}
enum CrateSugar {
PubCrate,
JustCrate,
}
#[match=custom] #[rewrite_print_recover] #[mac_table_record] #[nonterminal]
struct Ty { id, node, span }
struct MutTy {ty, mutbl}
enum TyKind {
Slice(ty),
Array(ty, len),
Ptr(mty),
Rptr(lt, mty),
BareFn(ty),
Never,
Tup(tys),
Path(qself, path),
TraitObject(bounds, trait_object_syntax),
ImplTrait(id, bounds),
Paren(ty),
Typeof(expr),
Infer,
ImplicitSelf,
#[mac_table_record] Mac(mac),
Err,
CVarArgs,
}
flag TraitObjectSyntax;
flag LitIntType;
flag FloatTy;
struct BareFnTy { unsafety, abi, decl, generic_params }
struct Lifetime { id, ident }
enum GenericBound {
Trait(poly_trait_ref, modifier),
Outlives(lt),
}
struct PolyTraitRef { trait_ref, span, bound_generic_params }
struct FnDecl { inputs, output, c_variadic }
struct FnHeader { unsafety, asyncness, constness, abi }
#[rewrite_print]
struct Arg { ty, pat, id }
enum FunctionRetTy {
Default(sp),
Ty(ty),
}
struct TypeBinding { id, ident, ty, span }
#[match=custom] #[rewrite_print_recover] #[rewrite_seq_item] #[nonterminal]
struct Stmt { id, node, span }
#[no_debug]
enum StmtKind {
Local(local),
Item(item),
Expr(expr),
Semi(expr),
Mac(mac),
}
#[extend_span]
struct Local { pat, ty, init, id, span, #[match=ignore] attrs }
#[match=custom] #[rewrite_print_recover] #[extend_span] #[mac_table_record] #[nonterminal]
struct Expr { id, node, span, #[match=ignore] attrs }
#[prec_contains_expr]
enum ExprKind {
Box(#[prec=PREFIX] expr),
ObsoleteInPlace(expr1, expr2),
Array(elems),
Call(#[prec=POSTFIX] #[prec_special=Callee] func, args),
MethodCall(path_seg, #[prec_first=POSTFIX] args),
Tup(elems),
Binary(op, #[prec_left_of_binop=op] a, #[prec_right_of_binop=op] b),
Unary(op, #[prec=PREFIX] a),
Lit(lit),
Cast(#[prec=As] expr, ty),
Type(#[prec=Colon] expr, ty),
If(#[prec_special=Cond] cond, then_body, else_body),
IfLet(pat, #[prec_special=Cond] expr, then_body, else_body),
While(#[prec_special=Cond] cond, body, label),
WhileLet(pat, #[prec_special=Cond] expr, body, label),
ForLoop(pat, #[prec_special=Cond] iter, body, label),
Loop(body, label),
Match(#[prec_special=Cond] target, arms),
Closure(cap, is_async, mov, decl, body, span),
Block(body, label),
Async(cap, id, block),
TryBlock(body),
Assign(#[lvalue_mut] #[prec_inc=Assign] lhs, #[prec=Assign] rhs),
AssignOp(op, #[lvalue_mut] #[prec_inc=Assign] lhs, #[prec=Assign] rhs),
Field(#[lr_propagate] #[prec=POSTFIX] expr, ident),
Index(#[lr_propagate] #[prec=POSTFIX] arr, idx),
// Special case for `Range`. `AssocOp` claims that `Range` has higher precedence than
// `Assign`, but `x .. x = x` gives a parse error instead of `x .. (x = x)`. Here we use a
// fake precedence value so that any child with lower precedence than a "normal" binop gets
// parenthesized. (`LOr` is the lowest-precedence binop.)
Range(#[prec=LOr] lo, #[prec=LOr] hi, limits),
Path(qself, path),
AddrOf(mutbl, #[lvalue_kind=mutbl] #[prec=PREFIX] expr),
Break(label, #[prec=JUMP] expr),
Continue(label),
Ret(#[prec=JUMP] expr),
InlineAsm(asm),
#[mac_table_record] Mac(mac),
Struct(path, fields, base),
Repeat(item, count),
Paren(expr),
Try(#[prec=POSTFIX] expr),
Yield(#[prec=JUMP] expr),
Err,
}
enum IsAsync {
Async { closure_id, return_impl_trait_id },
NotAsync,
}
enum Movability {
Static,
Movable,
}
enum UnOp {
Deref,
Not,
Neg,
}
#[match=eq]
flag BinOpKind;
#[extend_span]
struct Field { ident, expr, span, is_shorthand, attrs }
#[extend_span]
struct Arm { attrs, pats, guard, body }
enum Guard {
If(expr),
}
#[match=custom] #[rewrite_print_recover] #[nonterminal]
struct Block { #[mac_table_seq] stmts, id, rules, span }
#[match=custom] #[mac_table_record] #[nonterminal]
struct Pat { id, node, span }
enum PatKind {
Wild,
Ident(mode, id, pat),
Struct(path, fields, dotdot),
TupleStruct(path, fields, dotdot),
Path(qself, path),
Tuple(pats, dotdot),
Box(pat),
Ref(pat, mutbl),
Lit(expr),
Range(lo, hi, end),
Slice(start, mid, end),
Paren(pat),
#[mac_table_record] Mac(mac),
}
#[extend_span]
struct FieldPat { ident, pat, is_shorthand, attrs }
enum LitKind {
Str(sym, style),
ByteStr(bytes),
Byte(x),
Char(x),
Int(x, ty),
Float(sym, ty),
FloatUnsuffixed(sym),
Bool(x),
Err(sym),
}
flag Defaultness;
flag Constness;
flag ImplPolarity;
flag IsAuto;
flag Unsafety;
flag Abi;
#[match=eq] flag Mutability;
enum RangeEnd {
Included(syntax),
Excluded,
}
enum RangeSyntax {
DotDotDot,
DotDotEq,
}
flag BindingMode;
flag CaptureBy;
enum BlockCheckMode {
Default,
Unsafe(source),
}
enum UnsafeSource {
CompilerGenerated,
UserProvided,
}
flag StrStyle;
flag AsmDialect;
flag RangeLimits;
#[no_node_id] #[rewrite_print] #[rewrite_custom=SeqItem]
struct Attribute { id, style, path, tokens, is_sugared_doc, span }
flag AttrStyle;
#[match=custom] #[nonterminal]
struct Path { span, segments }
struct PathSegment { ident, id, args }
enum GenericArgs {
AngleBracketed(abpd),
Parenthesized(ppd),
}
struct AngleBracketedArgs { span, args, bindings }
struct ParenthesizedArgs { span, inputs, output }
enum GenericArg {
Lifetime(lt),
Type(ty),
Const(c),
}
struct QSelf { ty, path_span, position }
struct Mac_ { path, delim, tts }
flag MacStmtStyle;
#[equiv_mode=ignore]
flag TokenStream;
struct MacroDef { tokens, legacy }
struct InlineAsm {
asm, asm_str_style, outputs, inputs, clobbers,
volatile, alignstack, dialect, ctxt
}
struct GlobalAsm { asm, ctxt }
struct InlineAsmOutput { constraint, expr, is_rw, is_indirect }
#[match=custom] struct Label { ident }
enum MacDelimiter {
Parenthesis,
Bracket,
Brace,
}
struct AnonConst {
id,
value,
}
#[match=custom] struct Ident { name, span }
#[match=eq] flag Name;
#[match=eq] flag SyntaxContext;
#[equiv_mode=ignore] #[rewrite_ignore]
flag Span;
#[equiv_mode=ignore] #[rewrite_ignore] #[list_node_ids=custom] #[mac_table_custom]
flag NodeId;
#[equiv_mode=ignore] #[rewrite_ignore]
flag AttrId;
flag usize;
#[match=eq] flag bool;
#[match=eq] flag u128;
#[match=eq] flag u8;
#[match=eq] flag char;
enum Nonterminal {
NtItem(i),
NtBlock(b),
NtStmt(s),
NtPat(p),
NtExpr(y),
NtTy(t),
NtIdent(i, raw),
NtLifetime(ident),
NtLiteral(expr),
NtMeta(m),
NtPath(p),
NtVis(v),
NtTT(tt),
NtArm(a),
NtImplItem(ii),
NtTraitItem(ti),
NtForeignItem(fi),
NtGenerics(g),
NtWhereClause(wc),
NtArg(a),
}
enum TokenTree {
Token(sp, t),
Delimited(sp, d, tts),
}
struct DelimSpan { open, close }
flag DelimToken;
flag Token;
struct MetaItem { path, node, span }
enum MetaItemKind {
Word,
List(l),
NameValue(lit),
}
enum NestedMetaItem {
MetaItem(mi),
Literal(lit),
}