undname 1.1.2

A Rust library for demangling Microsoft symbols
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
// Copyright 2024 Ryan McKenzie
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::{
    nodes::Result,
    Buffer,
    OutputFlags,
    Writer,
};
use std::io::Write as _;

bitflags::bitflags! {
    // Storage classes
    #[derive(Clone, Copy, Default, Eq, PartialEq)]
    pub(crate) struct Qualifiers: u8 {
        const Q_None = 0;
        const Q_Const = 1 << 0;
        const Q_Volatile = 1 << 1;
        const Q_Far = 1 << 2;
        const Q_Huge = 1 << 3;
        const Q_Unaligned = 1 << 4;
        const Q_Restrict = 1 << 5;
        const Q_Pointer64 = 1 << 6;
    }
}

impl From<SingleQualifier> for Qualifiers {
    fn from(value: SingleQualifier) -> Self {
        match value {
            SingleQualifier::Const => Self::Q_Const,
            SingleQualifier::Volatile => Self::Q_Volatile,
            SingleQualifier::Restrict => Self::Q_Restrict,
        }
    }
}

#[derive(Clone, Copy)]
enum SingleQualifier {
    Const,
    Volatile,
    Restrict,
}

impl SingleQualifier {
    fn output_single_qualifier<B: Buffer>(
        self,
        ob: &mut Writer<'_, B>,
        flags: OutputFlags,
    ) -> Result<()> {
        let qualifier = match self {
            Self::Const => "const",
            Self::Volatile => "volatile",
            Self::Restrict => {
                if flags.no_ms_keywords() {
                    return Ok(());
                }
                if flags.no_leading_underscores() {
                    "restrict"
                } else {
                    "__restrict"
                }
            }
        };
        write!(ob, "{qualifier}")?;
        Ok(())
    }
}

impl Qualifiers {
    #[must_use]
    pub(super) fn is_const(self) -> bool {
        self.contains(Self::Q_Const)
    }

    #[must_use]
    pub(super) fn is_volatile(self) -> bool {
        self.contains(Self::Q_Volatile)
    }

    #[must_use]
    pub(super) fn is_unaligned(self) -> bool {
        self.contains(Self::Q_Unaligned)
    }

    #[must_use]
    pub(super) fn is_restrict(self) -> bool {
        self.contains(Self::Q_Restrict)
    }

    pub(super) fn output<B: Buffer>(
        self,
        ob: &mut Writer<'_, B>,
        flags: OutputFlags,
        space_before: bool,
        space_after: bool,
    ) -> Result<()> {
        if self != Self::Q_None {
            let len_before = ob.len();
            let space_before =
                self.output_if_present(ob, flags, SingleQualifier::Const, space_before)?;
            let space_before =
                self.output_if_present(ob, flags, SingleQualifier::Volatile, space_before)?;
            self.output_if_present(ob, flags, SingleQualifier::Restrict, space_before)?;
            let len_after = ob.len();
            if space_after && len_after > len_before {
                write!(ob, " ")?;
            }
        }

        Ok(())
    }

    fn output_if_present<B: Buffer>(
        self,
        ob: &mut Writer<'_, B>,
        flags: OutputFlags,
        mask: SingleQualifier,
        needs_space: bool,
    ) -> Result<bool> {
        if !self.contains(mask.into()) {
            return Ok(needs_space);
        }

        if needs_space {
            write!(ob, " ")?;
        }

        mask.output_single_qualifier(ob, flags)?;
        Ok(true)
    }
}

#[derive(Clone, Copy)]
pub(crate) enum StorageClass {
    PrivateStatic,
    ProtectedStatic,
    PublicStatic,
    Global,
    FunctionLocalStatic,
}

#[derive(Clone, Copy, Eq, PartialEq)]
pub(crate) enum PointerAffinity {
    Pointer,
    Reference,
    RValueReference,
}

#[derive(Clone, Copy)]
pub(crate) enum FunctionRefQualifier {
    Reference,
    RValueReference,
}

// Calling conventions
#[derive(Clone, Copy)]
pub(crate) enum CallingConv {
    Cdecl,
    Pascal,
    Thiscall,
    Stdcall,
    Fastcall,
    Clrcall,
    Eabi,
    Vectorcall,
    Swift,      // Clang-only
    SwiftAsync, // Clang-only
}

impl CallingConv {
    pub(super) fn output<B: Buffer>(
        self,
        ob: &mut Writer<'_, B>,
        flags: OutputFlags,
    ) -> Result<()> {
        super::output_space_if_necessary(ob)?;
        let cc = if flags.no_leading_underscores() {
            match self {
                CallingConv::Cdecl => "cdecl",
                CallingConv::Fastcall => "fastcall",
                CallingConv::Pascal => "pascal",
                CallingConv::Stdcall => "stdcall",
                CallingConv::Thiscall => "thiscall",
                CallingConv::Eabi => "eabi",
                CallingConv::Vectorcall => "vectorcall",
                CallingConv::Clrcall => "clrcall",
                CallingConv::Swift => "__attribute__((__swiftcall__)) ",
                CallingConv::SwiftAsync => "__attribute__((__swiftasynccall__)) ",
            }
        } else {
            match self {
                CallingConv::Cdecl => "__cdecl",
                CallingConv::Fastcall => "__fastcall",
                CallingConv::Pascal => "__pascal",
                CallingConv::Stdcall => "__stdcall",
                CallingConv::Thiscall => "__thiscall",
                CallingConv::Eabi => "__eabi",
                CallingConv::Vectorcall => "__vectorcall",
                CallingConv::Clrcall => "__clrcall",
                CallingConv::Swift => "__attribute__((__swiftcall__)) ",
                CallingConv::SwiftAsync => "__attribute__((__swiftasynccall__)) ",
            }
        };
        write!(ob, "{cc}")?;
        Ok(())
    }
}

#[derive(Clone, Copy)]
pub(crate) enum PrimitiveKind {
    Void,
    Bool,
    Char,
    Schar,
    Uchar,
    Char8,
    Char16,
    Char32,
    Short,
    Ushort,
    Int,
    Uint,
    Long,
    Ulong,
    Int64,
    Uint64,
    Wchar,
    Float,
    Double,
    Ldouble,
    Nullptr,
}

#[derive(Clone, Copy)]
pub(crate) enum CharKind {
    Char,
    Char16,
    Char32,
    Wchar,
}

#[derive(Clone, Copy)]
pub(crate) enum IntrinsicFunctionKind {
    New,                        // ?2 # operator new
    Delete,                     // ?3 # operator delete
    Assign,                     // ?4 # operator=
    RightShift,                 // ?5 # operator>>
    LeftShift,                  // ?6 # operator<<
    LogicalNot,                 // ?7 # operator!
    Equals,                     // ?8 # operator==
    NotEquals,                  // ?9 # operator!=
    ArraySubscript,             // ?A # operator[]
    Pointer,                    // ?C # operator->
    Dereference,                // ?D # operator*
    Increment,                  // ?E # operator++
    Decrement,                  // ?F # operator--
    Minus,                      // ?G # operator-
    Plus,                       // ?H # operator+
    BitwiseAnd,                 // ?I # operator&
    MemberPointer,              // ?J # operator->*
    Divide,                     // ?K # operator/
    Modulus,                    // ?L # operator%
    LessThan,                   // ?M operator<
    LessThanEqual,              // ?N operator<=
    GreaterThan,                // ?O operator>
    GreaterThanEqual,           // ?P operator>=
    Comma,                      // ?Q operator,
    Parens,                     // ?R operator()
    BitwiseNot,                 // ?S operator~
    BitwiseXor,                 // ?T operator^
    BitwiseOr,                  // ?U operator|
    LogicalAnd,                 // ?V operator&&
    LogicalOr,                  // ?W operator||
    TimesEqual,                 // ?X operator*=
    PlusEqual,                  // ?Y operator+=
    MinusEqual,                 // ?Z operator-=
    DivEqual,                   // ?_0 operator/=
    ModEqual,                   // ?_1 operator%=
    RshEqual,                   // ?_2 operator>>=
    LshEqual,                   // ?_3 operator<<=
    BitwiseAndEqual,            // ?_4 operator&=
    BitwiseOrEqual,             // ?_5 operator|=
    BitwiseXorEqual,            // ?_6 operator^=
    VbaseDtor,                  // ?_D # vbase destructor
    VecDelDtor,                 // ?_E # vector deleting destructor
    DefaultCtorClosure,         // ?_F # default constructor closure
    ScalarDelDtor,              // ?_G # scalar deleting destructor
    VecCtorIter,                // ?_H # vector constructor iterator
    VecDtorIter,                // ?_I # vector destructor iterator
    VecVbaseCtorIter,           // ?_J # vector vbase constructor iterator
    VdispMap,                   // ?_K # virtual displacement map
    EHVecCtorIter,              // ?_L # eh vector constructor iterator
    EHVecDtorIter,              // ?_M # eh vector destructor iterator
    EHVecVbaseCtorIter,         // ?_N # eh vector vbase constructor iterator
    CopyCtorClosure,            // ?_O # copy constructor closure
    LocalVftableCtorClosure,    // ?_T # local vftable constructor closure
    ArrayNew,                   // ?_U operator new[]
    ArrayDelete,                // ?_V operator delete[]
    ManVectorCtorIter,          // ?__A managed vector ctor iterator
    ManVectorDtorIter,          // ?__B managed vector dtor iterator
    EHVectorCopyCtorIter,       // ?__C EH vector copy ctor iterator
    EHVectorVbaseCopyCtorIter,  // ?__D EH vector vbase copy ctor iterator
    VectorCopyCtorIter,         // ?__G vector copy constructor iterator
    VectorVbaseCopyCtorIter,    // ?__H vector vbase copy constructor iterator
    ManVectorVbaseCopyCtorIter, // ?__I managed vector vbase copy constructor
    CoAwait,                    // ?__L operator co_await
    Spaceship,                  // ?__M operator<=>
}

#[derive(Clone, Copy)]
pub(crate) enum SpecialIntrinsicKind {
    Vftable,
    Vbtable,
    Typeof,
    VcallThunk,
    LocalStaticGuard,
    StringLiteralSymbol,
    UdtReturning,
    DynamicInitializer,
    DynamicAtexitDestructor,
    RttiTypeDescriptor,
    RttiBaseClassDescriptor,
    RttiBaseClassArray,
    RttiClassHierarchyDescriptor,
    RttiCompleteObjLocator,
    LocalVftable,
    LocalStaticThreadGuard,
}

bitflags::bitflags! {
    // Function classes
    #[derive(Clone, Copy, Default)]
    pub(crate) struct FuncClass: u16  {
        const FC_None = 0;
        const FC_Public = 1 << 0;
        const FC_Protected = 1 << 1;
        const FC_Private = 1 << 2;
        const FC_Global = 1 << 3;
        const FC_Static = 1 << 4;
        const FC_Virtual = 1 << 5;
        const FC_Far = 1 << 6;
        const FC_ExternC = 1 << 7;
        const FC_NoParameterList = 1 << 8;
        const FC_VirtualThisAdjust = 1 << 9;
        const FC_VirtualThisAdjustEx = 1 << 10;
        const FC_StaticThisAdjust = 1 << 11;
    }
}

impl FuncClass {
    #[must_use]
    pub(crate) fn is_public(self) -> bool {
        self.contains(Self::FC_Public)
    }

    #[must_use]
    pub(crate) fn is_protected(self) -> bool {
        self.contains(Self::FC_Protected)
    }

    #[must_use]
    pub(crate) fn is_private(self) -> bool {
        self.contains(Self::FC_Private)
    }

    #[must_use]
    pub(crate) fn is_global(self) -> bool {
        self.contains(Self::FC_Global)
    }

    #[must_use]
    pub(crate) fn is_static(self) -> bool {
        self.contains(Self::FC_Static)
    }

    #[must_use]
    pub(crate) fn is_virtual(self) -> bool {
        self.contains(Self::FC_Virtual)
    }

    #[must_use]
    pub(crate) fn is_extern_c(self) -> bool {
        self.contains(Self::FC_ExternC)
    }

    #[must_use]
    pub(crate) fn no_parameter_list(self) -> bool {
        self.contains(Self::FC_NoParameterList)
    }

    #[must_use]
    pub(crate) fn has_virtual_this_adjust(self) -> bool {
        self.contains(Self::FC_VirtualThisAdjust)
    }

    #[must_use]
    pub(crate) fn has_virtual_this_adjust_ex(self) -> bool {
        self.contains(Self::FC_VirtualThisAdjustEx)
    }

    #[must_use]
    pub(crate) fn has_static_this_adjust(self) -> bool {
        self.contains(Self::FC_StaticThisAdjust)
    }
}

#[derive(Clone, Copy)]
pub(crate) enum TagKind {
    Class,
    Struct,
    Union,
    Enum,
}