llvm-ir 0.11.3

LLVM IR in natural Rust data structures
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
//! See [LLVM 14 docs on Metadata](https://releases.llvm.org/14.0.0/docs/LangRef.html#metadata)

use either::Either;
use std::fmt::Debug;

use crate::operand::Operand;
use crate::types::{Type, Typed};

#[derive(PartialEq, Clone, Debug, Hash)]
pub enum MetadataRef<T> where T: PartialEq + Clone + Debug {
    Ref(MetadataNodeID),
    Inline(Box<T>),
}

pub type MetadataNodeID = usize;

/// See [LLVM 14 docs on Metadata Nodes and Metadata Strings](https://releases.llvm.org/14.0.0/docs/LangRef.html#metadata-nodes-and-metadata-strings)
#[derive(PartialEq, Clone, Debug, Hash)]
pub enum Metadata {
    String(String),
    Node(MetadataRef<MetadataNode>),
    Value(Operand),
}

impl Typed for Metadata {
    fn get_type(&self) -> Type {
        Type::MetadataType
    }
}

/// See [LLVM 14 docs on Metadata Nodes and Metadata Strings](https://releases.llvm.org/14.0.0/docs/LangRef.html#metadata-nodes-and-metadata-strings)
#[derive(PartialEq, Clone, Debug, Hash)]
pub enum MetadataNode {
    Tuple(Vec<Option<Metadata>>),  // None represents null
    Expression(DIExpression),
    GlobalVariableExpression(DIGlobalVariableExpression),
    Location(DILocation),
    MacroNode(DIMacroNode),
    Node(DINode),
}

// DI* types are in alphabetical order in this file

#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
pub enum DIAccessibility {
    Private,
    Protected,
    Public,
}

#[derive(PartialEq, Clone, Debug, Hash)]
pub struct DIArrayType {
    pub subscripts: Vec<DISubrange>,
    pub element_type: Option<MetadataRef<DIType>>,
    pub size_in_bits: u64,
    pub align_in_bits: u32,
    pub flags: Vec<DIFlag>,
}

/// See [LLVM 14 docs on DIBasicType](https://releases.llvm.org/14.0.0/docs/LangRef.html#dibasictype)
#[derive(PartialEq, Eq, Clone, Debug, Hash)]
pub struct DIBasicType {
    pub name: String,
    pub size_in_bits: u64,
    pub align_in_bits: u32,
    pub encoding: Option<Encoding>,
    pub tag: DIBasicTypeTag,
    pub flags: Vec<DIFlag>,
}

#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
pub enum DIBasicTypeTag {
    BaseType,
    UnspecifiedType,
}

#[derive(PartialEq, Eq, Clone, Debug, Hash)]
pub struct DIChecksumInfo {
    pub kind: DIChecksumKind,
    pub value: String,
}

#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
pub enum DIChecksumKind {
    MD5,
    SHA1,
}

#[derive(PartialEq, Clone, Debug, Hash)]
pub struct DIClassType {
    pub name: String,
    pub scope: Option<MetadataRef<DIScope>>,
    pub file: Option<MetadataRef<DIFile>>,
    pub line: u32,
    pub derived_from: Option<MetadataRef<DIType>>,
    pub elements: Vec<MetadataRef<Either<DIDerivedType,  DISubprogram>>>,
    pub vtable_holder: Option<MetadataRef<DIType>>,
    pub template_params: Vec<DITemplateParameter>,
    pub identifier: String,
    pub size_in_bits: u64,
    pub align_in_bits: u32,
    pub flags: Vec<DIFlag>,
}

/// See [LLVM 14 docs on DICompileUnit](https://releases.llvm.org/14.0.0/docs/LangRef.html#dicompileunit)
#[derive(PartialEq, Clone, Debug, Hash)]
pub struct DICompileUnit {
    pub language: u32,
    pub file: MetadataRef<DIFile>,
    pub producer: String,
    pub optimized: bool,
    pub flags: String,
    pub runtime_version: u32,
    pub split_debug_filename: String,
    pub emission_kind: DIDebugEmissionKind,
    pub enums: Vec<MetadataRef<DIEnumerationType>>,
    pub retained_types: Vec<MetadataRef<Either<DIType, DISubprogram>>>,
    pub globals: Vec<MetadataRef<DIGlobalVariableExpression>>,
    pub imports: Vec<MetadataRef<DIImportedEntity>>,
    pub macros: Vec<MetadataRef<DIMacroNode>>,
    pub dwoid: u64,
    pub split_debug_inlining: bool,
    pub debug_info_for_profiling: bool,
    pub name_table_kind: DIDebugNameTableKind,
    pub debug_base_address: bool,
}

/// See [LLVM 14 docs on DICompositeType](https://releases.llvm.org/14.0.0/docs/LangRef.html#dicompositetype)
#[derive(PartialEq, Clone, Debug, Hash)]
pub enum DICompositeType {
    Array(DIArrayType),
    Class(DIClassType),
    Enumeration(DIEnumerationType),
    Structure(DIStructureType),
    Union(DIUnionType),
}

#[derive(PartialEq, Clone, Debug, Hash)]
pub enum DICount {
    Constant(i64),
    Variable(MetadataRef<DIVariable>),
}

#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
pub enum DIDebugEmissionKind {
    NoDebug,
    FullDebug,
    LineTablesOnly,
}

#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
pub enum DIDebugNameTableKind {
    Default,
    GNU,
    None,
}

/// See [LLVM 14 docs on DIDerivedType](https://releases.llvm.org/14.0.0/docs/LangRef.html#diderivedtype)
#[derive(PartialEq, Clone, Debug, Hash)]
pub struct DIDerivedType {
    pub tag: DIDerivedTypeTag,
    pub name: String,
    pub file: Option<MetadataRef<DIFile>>,
    pub line: u32,
    pub scope: Option<MetadataRef<DIScope>>,
    pub base_type: Option<MetadataRef<DIType>>,  // `None` would represent `void*`
    pub size_in_bits: u64,
    pub align_in_bits: u32,
    pub offset_in_bits: u64,
    pub address_space: Option<u32>,
    pub flags: Vec<DIFlag>,
}

#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
pub enum DIDerivedTypeTag {
    Typedef,
    PointerType,
    PtrToMemberType,
    ReferenceType,
    RValueReferenceType,
    ConstType,
    VolatileType,
    RestrictType,
    AtomicType,
    Member,
    Inheritance,
    Friend,
}

#[derive(PartialEq, Clone, Debug, Hash)]
pub struct DIEnumerationType {
    pub name: String,
    pub scope: Option<MetadataRef<DIScope>>,
    pub file: Option<MetadataRef<DIFile>>,
    pub line: u32,
    pub values: Vec<DIEnumerator>,
    pub base_type: Option<MetadataRef<DIType>>,
    pub identifier: String,
    pub size_in_bits: u64,
    pub align_in_bits: u32,
}

/// See [LLVM 14 docs on DIEnumerator](https://releases.llvm.org/14.0.0/docs/LangRef.html#dienumerator)
#[derive(PartialEq, Eq, Clone, Debug, Hash)]
pub struct DIEnumerator {
    pub name: String,
    pub value: i64,
    pub is_unsigned: bool,
}

/// See [LLVM 14 docs on DIExpression](https://releases.llvm.org/14.0.0/docs/LangRef.html#diexpression)
pub type DIExpression = Vec<DWOp>;

/// See [LLVM 14 docs on DIFile](https://releases.llvm.org/14.0.0/docs/LangRef.html#difile)
#[derive(PartialEq, Eq, Clone, Debug, Hash)]
pub struct DIFile {
    pub filename: String,
    pub directory: String,
    pub checksum: Option<DIChecksumInfo>,
}

#[derive(PartialEq, Eq, Clone, Debug, Hash)]
pub enum DIFlag {
    Accessibility(DIAccessibility),
    FwdDecl,
    AppleBlock,
    BlockByrefStruct,
    VirtualFlag,
    Artificial,
    Explicit,
    Prototyped,
    ObjcClassComplete,
    ObjectPointer,
    Vector,
    StaticMember,
    LValueReference,
    RValueReference,
    InheritanceFlag(DIInheritance),
    IntroducedVirtual,
    BitField,
    NoReturn,
    MainSubprogram,
}

/// See [LLVM 14 docs on DIGlobalVariable](https://releases.llvm.org/14.0.0/docs/LangRef.html#diglobalvariable)
#[derive(PartialEq, Clone, Debug, Hash)]
pub struct DIGlobalVariable {
    pub name: String,
    pub linkage_name: String,
    pub scope: Option<MetadataRef<DIScope>>,
    pub file: Option<MetadataRef<DIFile>>,
    pub line: u32,
    pub ty: Option<MetadataRef<DIType>>,
    pub local: bool,
    pub definition: bool,
    pub static_data_member_declaration: Option<MetadataRef<DIDerivedType>>,
    pub template_params: Vec<MetadataRef<DITemplateParameter>>,
    pub align_in_bits: u32,
}

#[derive(PartialEq, Clone, Debug, Hash)]
pub struct DIGlobalVariableExpression {
    pub var: MetadataRef<DIGlobalVariable>,
    pub expr: MetadataRef<DIExpression>,
}

/// See [LLVM 14 docs on DIImportedEntity](https://releases.llvm.org/14.0.0/docs/LangRef.html#diimportedentity)
#[derive(PartialEq, Clone, Debug, Hash)]
pub struct DIImportedEntity {
    pub tag: DIImportedEntityTag,
    pub name: String,
    pub scope: MetadataRef<DIScope>,
    pub entity: Option<MetadataRef<DINode>>,
    pub file: Option<MetadataRef<DIFile>>,
    pub line: u32,
}

#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
pub enum DIImportedEntityTag {
    Module,
    Declaration,
}

#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
pub enum DIInheritance {
    SingleInheritance,
    MultipleInheritance,
    VirtualInheritance,
}

#[derive(PartialEq, Clone, Debug, Hash)]
pub enum DILexicalBlockBase {
    LexicalBlock(DILexicalBlock),
    LexicalBlockFile(DILexicalBlockFile),
}

/// See [LLVM 14 docs on DILexicalBlock](https://releases.llvm.org/14.0.0/docs/LangRef.html#dilexicalblock)
#[derive(PartialEq, Clone, Debug, Hash)]
pub struct DILexicalBlock {
    pub scope: MetadataRef<DILocalScope>,
    pub file: Option<MetadataRef<DIFile>>,
    pub line: u32,
    pub column: u32,
}

/// See [LLVM 14 docs on DILexicalBlockFile](https://releases.llvm.org/14.0.0/docs/LangRef.html#dilexicalblockfile)
#[derive(PartialEq, Clone, Debug, Hash)]
pub struct DILexicalBlockFile {
    pub scope: MetadataRef<DILocalScope>,
    pub file: Option<MetadataRef<DIFile>>,
    pub discriminator: u32,
}

#[derive(PartialEq, Clone, Debug, Hash)]
pub enum DILocalScope {
    LexicalBlockBase(DILexicalBlockBase),
    Subprogram(DISubprogram),
}

/// See [LLVM 14 docs on DILocalVariable](https://releases.llvm.org/14.0.0/docs/LangRef.html#dilocalvariable)
#[derive(PartialEq, Clone, Debug, Hash)]
pub struct DILocalVariable {
    pub name: String,
    pub scope: MetadataRef<DIScope>,
    pub file: Option<MetadataRef<DIFile>>,
    pub line: u32,
    pub ty: Option<MetadataRef<DIType>>,
    pub flags: Vec<DIFlag>,
    pub arg: u16,
    pub align_in_bits: u32,
}

/// See [LLVM 14 docs on DILocation](https://releases.llvm.org/14.0.0/docs/LangRef.html#dilocation)
#[derive(PartialEq, Clone, Debug, Hash)]
pub struct DILocation {
    pub line: u32,
    pub column: u32,
    pub scope: MetadataRef<DILocalScope>,
}

/// See LLVM 14 docs on [DIMacro](https://releases.llvm.org/14.0.0/docs/LangRef.html#dimacro) and
/// [DIMacroFile](https://releases.llvm.org/14.0.0/docs/LangRef.html#dimacrofile)
#[derive(PartialEq, Clone, Debug, Hash)]
pub enum DIMacroNode {
    Macro { name: String, value: String, info: DIMacroInfo, line: u32 },
    MacroFile { file: MetadataRef<DIFile>, elements: Vec<MetadataRef<DIMacroNode>>, line: u32 },
}

#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
pub enum DIMacroInfo {
    Define,
    Undef,
}

#[derive(PartialEq, Clone, Debug, Hash)]
pub struct DIModule {
    pub name: String,
    pub scope: Option<MetadataRef<DIScope>>,
    pub configuration_macros: String,
    pub include_path: String,
    pub isys_root: String,
}

/// See [LLVM 14 docs on DINamespace](https://releases.llvm.org/14.0.0/docs/LangRef.html#dinamespace)
#[derive(PartialEq, Clone, Debug, Hash)]
pub struct DINamespace {
    pub name: String,
    pub scope: Option<MetadataRef<DIScope>>,
    pub export_symbols: bool,
}

#[derive(PartialEq, Clone, Debug, Hash)]
pub enum DINode {
    Enumerator(DIEnumerator),
    ImportedEntity(DIImportedEntity),
    ObjCProperty(DIObjCProperty),
    Scope(DIScope),
    Subrange(DISubrange),
    TemplateParameter(DITemplateParameter),
    Variable(DIVariable),
}

/// See [LLVM 14 docs on DIObjCProperty](https://releases.llvm.org/14.0.0/docs/LangRef.html#diobjcproperty)
#[derive(PartialEq, Clone, Debug, Hash)]
pub struct DIObjCProperty {
    pub name: String,
    pub file: Option<MetadataRef<DIFile>>,
    pub line: u32,
    pub getter_name: String,
    pub setter_name: String,
    pub attributes: u32,
    pub ty: Option<MetadataRef<DIType>>,
}

#[derive(PartialEq, Clone, Debug, Hash)]
pub enum DIScope {
    CompileUnit(DICompileUnit),
    File(DIFile),
    LocalScope(DILocalScope),
    Module(DIModule),
    Namespace(DINamespace),
    Type(DIType),
}

#[derive(PartialEq, Clone, Debug, Hash)]
pub struct DIStructureType {
    pub name: String,
    pub scope: Option<MetadataRef<DIScope>>,
    pub file: Option<MetadataRef<DIFile>>,
    pub line: u32,
    pub flags: Vec<DIFlag>,
    pub derived_from: Option<MetadataRef<DIType>>,
    pub elements: Vec<MetadataRef<Either<DIDerivedType, DISubprogram>>>,
    pub runtime_lang: u16,
    pub vtable_holder: Option<MetadataRef<DIType>>,
    pub identifier: String,
    pub size_in_bits: u64,
    pub align_in_bits: u32,
}

/// See [LLVM 14 docs on DISubprogram](https://releases.llvm.org/14.0.0/docs/LangRef.html#disubprogram)
#[derive(PartialEq, Clone, Debug, Hash)]
pub struct DISubprogram {
    pub name: String,
    pub linkage_name: String,
    pub scope: Option<MetadataRef<DIScope>>,
    pub file: Option<MetadataRef<DIFile>>,
    pub line: u32,
    pub subroutine_type: Option<MetadataRef<DISubroutineType>>,
    pub local_to_unit: bool,
    pub definition: bool,
    pub scope_line: u32,
    pub containing_type: Option<MetadataRef<DIType>>,
    pub virtuality: Virtuality,
    pub virtuality_index: u32,
    pub this_adjustment: i32,
    pub flags: Vec<DIFlag>,
    pub optimized: bool,
    pub unit: Option<MetadataRef<DICompileUnit>>,
    pub template_params: Vec<MetadataRef<DITemplateParameter>>,
    pub declaration: Option<MetadataRef<DISubprogram>>,
    pub retained_nodes: Vec<MetadataRef<DILocalVariable>>,
    pub thrown_types: Vec<MetadataRef<DIType>>,
}

/// See [LLVM 14 docs on DISubrange](https://releases.llvm.org/14.0.0/docs/LangRef.html#disubrange)
#[derive(PartialEq, Clone, Debug, Hash)]
pub struct DISubrange {
    pub count: DICount,
    pub lower_bound: i64,
}

/// See [LLVM 14 docs on DISubroutineType](https://releases.llvm.org/14.0.0/docs/LangRef.html#disubroutinetype)
#[derive(PartialEq, Clone, Debug, Hash)]
pub struct DISubroutineType {
    /// First the return type, then the operand types. `None` means `void`.
    pub type_array: Vec<Option<MetadataRef<DIType>>>,
    pub cc: u8,
    pub flags: Vec<DIFlag>,
}

/// See LLVM 14 docs on [DITemplateTypeParameter](https://releases.llvm.org/14.0.0/docs/LangRef.html#ditemplatetypeparameter)
/// and [DITemplateValueParameter](https://releases.llvm.org/14.0.0/docs/LangRef.html#ditemplatevalueparameter)
#[derive(PartialEq, Clone, Debug, Hash)]
pub enum DITemplateParameter {
    TypeParameter { name: String, ty: Option<MetadataRef<DIType>> },
    ValueParameter { name: String, ty: Option<MetadataRef<DIType>>, value: Option<Box<Metadata>>, tag: DITemplateValueParameterTag },
}

#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
pub enum DITemplateValueParameterTag {
    TemplateValueParameter,
    GNUTemplateTemplateParam,
    GNUTemplateParameterPack,
}

#[derive(PartialEq, Clone, Debug, Hash)]
pub enum DIType {
    Basic(DIBasicType),
    Composite(DICompositeType),
    Derived(DIDerivedType),
    Subroutine(DISubroutineType),
}

#[derive(PartialEq, Clone, Debug, Hash)]
pub struct DIUnionType {
    pub name: String,
    pub scope: Option<MetadataRef<DIScope>>,
    pub file: Option<MetadataRef<DIFile>>,
    pub line: u32,
    pub flags: Vec<DIFlag>,
    pub elements: Vec<MetadataRef<Either<DIDerivedType, DISubprogram>>>,
    pub runtime_lang: u16,
    pub identifier: String,
    pub size_in_bits: u64,
    pub align_in_bits: u32,
}

#[derive(PartialEq, Clone, Debug, Hash)]
pub enum DIVariable {
    Global(DIGlobalVariable),
    Local(DILocalVariable),
}

#[derive(PartialEq, Eq, Clone, Debug, Hash)]
pub enum DWOp {
    Fragment { offset: u64, size: u64 },  // must be last in the list
    StackValue,  // must be either last or followed by Fragment
    Swap,
    ConstU(u64),
    Lit0,
    PlusUConst(u64),
    Plus,
    Minus,
    Mul,
    Div,
    Mod,
    Not,
    Or,
    Xor,
    And,
    Shr,
    Shra,
    Shl,
    Dup,
    Deref,
    XDeref,
}

#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
pub enum Encoding {
    AddressEncoding,
    BooleanEncoding,
    FloatEncoding,
    SignedEncoding,
    SignedCharEncoding,
    UnsignedEncoding,
    UnsignedCharEncoding,
    UTFEncoding,
}

#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
pub enum Virtuality {
    NoVirtuality,
    Virtual,
    PureVirtual,
}

// ********* //
// from_llvm //
// ********* //

use crate::from_llvm::*;

impl Metadata {
    pub(crate) fn from_llvm_ref(md: LLVMValueRef) -> Self {
        unimplemented!("Metadata::from_llvm_ref");
    }
}