c2rust-refactor 0.10.1

C2Rust refactoring tool implementation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// 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),
}