cppshift 0.1.0

CPP parser and transpiler
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
//! Item types for C++20 top-level declarations
//!
//! Each variant of [`Item`] corresponds to a top-level declaration in a C++ translation unit,
//! following the naming conventions of `syn::Item`.

use crate::SourceSpan;
use crate::lex::Token;

use super::expr::Expr;
use super::punct::Punctuated;
use super::stmt::Block;
use super::ty::Type;

// ---------------------------------------------------------------------------
// Core support types
// ---------------------------------------------------------------------------

/// An identifier with its source span, analogous to `syn::Ident`.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Ident<'de> {
    pub sym: &'de str,
    pub span: SourceSpan<'de>,
}

/// Visibility of a declaration.
///
/// In C++, visibility applies within class/struct bodies via access specifiers.
/// At namespace scope, everything is effectively public.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum Visibility {
    Public,
    Protected,
    Private,
    /// No explicit access specifier (default for context)
    #[default]
    Inherited,
}

/// A C++20 attribute `[[...]]`, analogous to `syn::Attribute`.
#[derive(Debug, Clone, PartialEq)]
pub struct Attribute<'de> {
    pub span: SourceSpan<'de>,
    pub path: Path<'de>,
    pub args: Vec<Token<'de>>,
}

/// A qualified path like `std::vector` or `::global::Foo`.
///
/// Analogous to `syn::Path`.
#[derive(Debug, Clone, PartialEq)]
pub struct Path<'de> {
    pub leading_colon: bool,
    pub segments: Vec<PathSegment<'de>>,
}

/// A single segment of a path, analogous to `syn::PathSegment`.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct PathSegment<'de> {
    pub ident: Ident<'de>,
}

/// A base class specifier in a class/struct definition.
///
/// Example: `public Base`, `virtual protected Interface`
#[derive(Debug, Clone, PartialEq)]
pub struct BaseSpecifier<'de> {
    pub access: Visibility,
    pub virtual_token: bool,
    pub path: Path<'de>,
}

/// A field (data member) in a struct, class, or union.
///
/// Analogous to `syn::Field`.
#[derive(Debug, Clone, PartialEq)]
pub struct Field<'de> {
    pub attrs: Vec<Attribute<'de>>,
    pub vis: Visibility,
    pub ty: Type<'de>,
    pub ident: Option<Ident<'de>>,
    pub default_value: Option<Expr<'de>>,
}

/// Fields of a struct, class, or union.
///
/// Analogous to `syn::Fields`.
#[derive(Debug, Clone, PartialEq)]
pub enum Fields<'de> {
    /// Named fields with access specifier grouping: `{ public: int x; private: int y; }`
    Named(FieldsNamed<'de>),
    /// Forward declaration (no body): `struct Foo;`
    Unit,
}

/// Named fields grouped by access specifier.
///
/// Analogous to `syn::FieldsNamed`.
#[derive(Debug, Clone, PartialEq)]
pub struct FieldsNamed<'de> {
    pub members: Vec<Member<'de>>,
}

/// A member inside a class/struct body.
///
/// Can be a field, a method, a nested type, an access specifier, etc.
#[derive(Debug, Clone, PartialEq)]
pub enum Member<'de> {
    /// Access specifier: `public:`, `private:`, `protected:`
    AccessSpecifier(Visibility),
    /// Data member (field)
    Field(Field<'de>),
    /// Member function (method)
    Method(ItemFn<'de>),
    /// Constructor
    Constructor(ItemConstructor<'de>),
    /// Destructor
    Destructor(ItemDestructor<'de>),
    /// Nested type (class, struct, enum, etc.)
    Item(Box<Item<'de>>),
    /// Friend declaration
    Friend(ItemFriend<'de>),
    /// Using declaration inside a class
    Using(ItemUse<'de>),
    /// Static assertion
    StaticAssert(ItemStaticAssert<'de>),
}

/// A function argument, analogous to `syn::FnArg`.
#[derive(Debug, Clone, PartialEq)]
pub struct FnArg<'de> {
    pub attrs: Vec<Attribute<'de>>,
    pub ty: Type<'de>,
    pub ident: Option<Ident<'de>>,
    pub default_value: Option<Expr<'de>>,
}

/// A function signature, analogous to `syn::Signature`.
#[derive(Debug, Clone, PartialEq)]
pub struct Signature<'de> {
    pub constexpr_token: bool,
    pub consteval_token: bool,
    pub inline_token: bool,
    pub virtual_token: bool,
    pub static_token: bool,
    pub explicit_token: bool,
    pub return_type: Type<'de>,
    pub ident: Ident<'de>,
    pub inputs: Punctuated<'de, FnArg<'de>>,
    pub variadic: bool,
    // Trailing qualifiers
    pub const_token: bool,
    pub noexcept_token: bool,
    pub override_token: bool,
    pub final_token: bool,
    pub pure_virtual: bool,
    pub defaulted: bool,
    pub deleted: bool,
}

/// An enum variant, analogous to `syn::Variant`.
#[derive(Debug, Clone, PartialEq)]
pub struct Variant<'de> {
    pub attrs: Vec<Attribute<'de>>,
    pub ident: Ident<'de>,
    pub discriminant: Option<Expr<'de>>,
}

/// A template parameter.
#[derive(Debug, Clone, PartialEq)]
pub enum TemplateParam<'de> {
    /// `typename T` or `class T`
    Type {
        ident: Option<Ident<'de>>,
        default: Option<Type<'de>>,
    },
    /// Non-type: `int N`
    NonType {
        ty: Type<'de>,
        ident: Option<Ident<'de>>,
        default: Option<Expr<'de>>,
    },
    /// Template template: `template<...> class C`
    Template {
        params: Vec<TemplateParam<'de>>,
        ident: Option<Ident<'de>>,
    },
    /// Parameter pack: `typename... Args`
    Pack { ident: Ident<'de> },
}

/// A foreign item inside an `extern "C"` block.
#[derive(Debug, Clone, PartialEq)]
pub enum ForeignItem<'de> {
    /// Function declaration
    Fn(ItemFn<'de>),
    /// Variable declaration
    Static(ItemStatic<'de>),
    /// Unparsed tokens
    Verbatim(ItemVerbatim<'de>),
}

/// A member initializer in a constructor initializer list.
///
/// Example: `m_x(x)`, `Base(arg)`
#[derive(Debug, Clone, PartialEq)]
pub struct MemberInit<'de> {
    pub member: Ident<'de>,
    pub args: Punctuated<'de, Expr<'de>>,
}

// ---------------------------------------------------------------------------
// Item enum and variants
// ---------------------------------------------------------------------------

/// A top-level item (declaration) in a C++ translation unit.
///
/// Analogous to `syn::Item`. Each variant corresponds to a kind of
/// declaration that can appear at file scope or namespace scope.
#[derive(Debug, Clone, PartialEq)]
pub enum Item<'de> {
    /// Function declaration or definition: `int foo() { ... }`
    Fn(ItemFn<'de>),
    /// Struct definition: `struct Foo { ... };`
    Struct(ItemStruct<'de>),
    /// Class definition: `class Foo { ... };`
    Class(ItemClass<'de>),
    /// Enum definition: `enum Color { Red, Green, Blue };`
    Enum(ItemEnum<'de>),
    /// Union definition: `union Data { int i; float f; };`
    Union(ItemUnion<'de>),
    /// Namespace: `namespace foo { ... }`
    Namespace(ItemNamespace<'de>),
    /// Using declaration/directive/alias: `using std::cout;`
    Use(ItemUse<'de>),
    /// Type alias: `using size_t = unsigned long;`
    Type(ItemType<'de>),
    /// Typedef: `typedef unsigned long size_t;`
    Typedef(ItemTypedef<'de>),
    /// Const/constexpr variable: `constexpr int MAX = 100;`
    Const(ItemConst<'de>),
    /// Static variable: `static int count;`
    Static(ItemStatic<'de>),
    /// Extern block: `extern "C" { ... }`
    ForeignMod(ItemForeignMod<'de>),
    /// Template declaration: `template<typename T> ...`
    Template(ItemTemplate<'de>),
    /// Static assertion: `static_assert(sizeof(int) == 4);`
    StaticAssert(ItemStaticAssert<'de>),
    /// Include directive: `#include <iostream>` or `#include "myfile.h"`
    Include(ItemInclude<'de>),
    /// Preprocessor directive: `#include <iostream>`
    Macro(ItemMacro<'de>),
    /// Tokens not interpreted by the parser
    Verbatim(ItemVerbatim<'de>),
}

/// A function declaration or definition, analogous to `syn::ItemFn`.
#[derive(Debug, Clone, PartialEq)]
pub struct ItemFn<'de> {
    pub attrs: Vec<Attribute<'de>>,
    pub vis: Visibility,
    pub sig: Signature<'de>,
    pub block: Option<Block<'de>>,
}

/// A struct definition, analogous to `syn::ItemStruct`.
#[derive(Debug, Clone, PartialEq)]
pub struct ItemStruct<'de> {
    pub attrs: Vec<Attribute<'de>>,
    pub ident: Option<Ident<'de>>,
    pub generics: Option<Generics<'de>>,
    pub bases: Vec<BaseSpecifier<'de>>,
    pub fields: Fields<'de>,
}

/// A class definition (C++ specific).
#[derive(Debug, Clone, PartialEq)]
pub struct ItemClass<'de> {
    pub attrs: Vec<Attribute<'de>>,
    pub ident: Option<Ident<'de>>,
    pub generics: Option<Generics<'de>>,
    pub bases: Vec<BaseSpecifier<'de>>,
    pub fields: Fields<'de>,
}

/// An enum definition, analogous to `syn::ItemEnum`.
#[derive(Debug, Clone, PartialEq)]
pub struct ItemEnum<'de> {
    pub attrs: Vec<Attribute<'de>>,
    pub ident: Option<Ident<'de>>,
    pub scoped: bool,
    pub underlying_type: Option<Type<'de>>,
    pub variants: Punctuated<'de, Variant<'de>>,
}

/// A union definition, analogous to `syn::ItemUnion`.
#[derive(Debug, Clone, PartialEq)]
pub struct ItemUnion<'de> {
    pub attrs: Vec<Attribute<'de>>,
    pub ident: Option<Ident<'de>>,
    pub fields: FieldsNamed<'de>,
}

/// A namespace declaration, analogous to `syn::ItemMod`.
#[derive(Debug, Clone, PartialEq)]
pub struct ItemNamespace<'de> {
    pub attrs: Vec<Attribute<'de>>,
    pub inline_token: bool,
    pub ident: Option<Ident<'de>>,
    pub content: Vec<Item<'de>>,
}

/// A using declaration, directive, or alias, analogous to `syn::ItemUse`.
#[derive(Debug, Clone, PartialEq)]
pub enum ItemUse<'de> {
    /// `using std::cout;`
    Declaration {
        attrs: Vec<Attribute<'de>>,
        name: Path<'de>,
    },
    /// `using namespace std;`
    Directive {
        attrs: Vec<Attribute<'de>>,
        namespace: Path<'de>,
    },
    /// `using size_t = unsigned long;`
    Alias {
        attrs: Vec<Attribute<'de>>,
        ident: Ident<'de>,
        ty: Type<'de>,
    },
}

/// A type alias (`using X = Y`), analogous to `syn::ItemType`.
#[derive(Debug, Clone, PartialEq)]
pub struct ItemType<'de> {
    pub attrs: Vec<Attribute<'de>>,
    pub ident: Ident<'de>,
    pub generics: Option<Generics<'de>>,
    pub ty: Type<'de>,
}

/// A typedef declaration (C-style type alias).
#[derive(Debug, Clone, PartialEq)]
pub struct ItemTypedef<'de> {
    pub attrs: Vec<Attribute<'de>>,
    pub ty: Type<'de>,
    pub ident: Ident<'de>,
}

/// A const or constexpr variable, analogous to `syn::ItemConst`.
#[derive(Debug, Clone, PartialEq)]
pub struct ItemConst<'de> {
    pub attrs: Vec<Attribute<'de>>,
    pub constexpr_token: bool,
    pub ty: Type<'de>,
    pub ident: Ident<'de>,
    pub expr: Expr<'de>,
}

/// A static variable, analogous to `syn::ItemStatic`.
#[derive(Debug, Clone, PartialEq)]
pub struct ItemStatic<'de> {
    pub attrs: Vec<Attribute<'de>>,
    pub ty: Type<'de>,
    pub ident: Ident<'de>,
    pub expr: Option<Expr<'de>>,
}

/// An extern block (`extern "C" { ... }`), analogous to `syn::ItemForeignMod`.
#[derive(Debug, Clone, PartialEq)]
pub struct ItemForeignMod<'de> {
    pub attrs: Vec<Attribute<'de>>,
    pub abi: &'de str,
    pub items: Vec<ForeignItem<'de>>,
}

/// A template declaration (C++ specific).
#[derive(Debug, Clone, PartialEq)]
pub struct ItemTemplate<'de> {
    pub attrs: Vec<Attribute<'de>>,
    pub params: Punctuated<'de, TemplateParam<'de>>,
    pub item: Box<Item<'de>>,
}

/// A static assertion (C++ specific).
#[derive(Debug, Clone, PartialEq)]
pub struct ItemStaticAssert<'de> {
    pub expr: Expr<'de>,
    pub message: Option<Expr<'de>>,
}

/// The path in a `#include` directive.
#[derive(Debug, Clone, PartialEq)]
pub enum IncludePath<'de> {
    /// System header enclosed in angle brackets: `<iostream>`
    System(SourceSpan<'de>),
    /// Local header enclosed in quotes: `"myheader.h"`
    Local(SourceSpan<'de>),
}

/// A `#include` preprocessor directive: `#include <iostream>` or `#include "myfile.h"`.
#[derive(Debug, Clone, PartialEq)]
pub struct ItemInclude<'de> {
    pub span: SourceSpan<'de>,
    pub path: IncludePath<'de>,
}

/// A preprocessor directive.
#[derive(Debug, Clone, PartialEq)]
pub struct ItemMacro<'de> {
    pub span: SourceSpan<'de>,
    pub tokens: Vec<Token<'de>>,
}

/// Tokens not interpreted by the parser, analogous to `syn::Item::Verbatim`.
#[derive(Debug, Clone, PartialEq)]
pub struct ItemVerbatim<'de> {
    pub tokens: Vec<Token<'de>>,
}

/// A constructor declaration/definition.
#[derive(Debug, Clone, PartialEq)]
pub struct ItemConstructor<'de> {
    pub attrs: Vec<Attribute<'de>>,
    pub explicit_token: bool,
    pub constexpr_token: bool,
    pub ident: Ident<'de>,
    pub inputs: Punctuated<'de, FnArg<'de>>,
    pub noexcept_token: bool,
    pub member_init_list: Vec<MemberInit<'de>>,
    pub block: Option<Block<'de>>,
    pub defaulted: bool,
    pub deleted: bool,
}

/// A destructor declaration/definition.
#[derive(Debug, Clone, PartialEq)]
pub struct ItemDestructor<'de> {
    pub attrs: Vec<Attribute<'de>>,
    pub virtual_token: bool,
    pub ident: Ident<'de>,
    pub noexcept_token: bool,
    pub block: Option<Block<'de>>,
    pub defaulted: bool,
    pub deleted: bool,
    pub pure_virtual: bool,
}

/// A friend declaration.
#[derive(Debug, Clone, PartialEq)]
pub struct ItemFriend<'de> {
    pub attrs: Vec<Attribute<'de>>,
    pub item: Box<Item<'de>>,
}

/// Template generics on a class/struct/function.
#[derive(Debug, Clone, PartialEq)]
pub struct Generics<'de> {
    pub params: Punctuated<'de, TemplateParam<'de>>,
}