llvm_scratch/core/global_variable/
var.rs

1use crate::core::{
2    instruction,
3    llvm_type,
4    llvm_value,
5};
6use std::fmt;
7use std::fmt::Formatter;
8
9#[derive(Eq, PartialEq, PartialOrd, Ord, Hash)]
10pub struct GlobalVariable {
11    linkage: Option<instruction::Linkage>,
12    unnamed_addr: bool,
13    constant: bool,
14    ty: llvm_type::LLVMType,
15    initializer: Option<llvm_value::LLVMValue>,
16    align: Option<u32>,
17}
18
19impl GlobalVariable {
20    pub fn new(
21        linkage: Option<instruction::Linkage>,
22        unnamed_addr: bool,
23        constant: bool,
24        ty: llvm_type::LLVMType,
25        initializer: Option<llvm_value::LLVMValue>,
26        align: Option<u32>,
27    ) -> Self {
28        Self {
29            linkage,
30            unnamed_addr,
31            constant,
32            ty,
33            initializer,
34            align,
35        }
36    }
37}
38
39impl fmt::Display for GlobalVariable{
40    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
41        if let Some(linkage) = &self.linkage {
42            write!(f, "{} ", linkage)?;
43        }
44
45        if self.unnamed_addr{
46            write!(f, "unnamed_addr ")?;
47        } else{
48            write!(f, "local_unnamed_addr ")?;
49        }
50
51        if self.constant{
52            write!(f, "constant ")?;
53        } else{
54            write!(f, "global ")?;
55        }
56
57        write!(f, "{} ", self.ty)?;
58
59        if let Some(initializer) = &self.initializer{
60            write!(f, "{} ", initializer)?;
61        }
62
63        if let Some(align) = &self.align{
64            write!(f, "{} ", align)?;
65        }
66        Ok(())
67    }
68}