llir/values/constant/
constant.rs1use llvm_sys::core::LLVMGetValueKind;
2use llvm_sys::prelude::LLVMValueRef;
3use llvm_sys::LLVMValueKind;
4
5use super::*;
6use crate::values::*;
7use crate::*;
8
9#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
15pub enum Constant<'ctx> {
16 Int(IntConstant<'ctx>),
17 Float(FloatConstant<'ctx>),
18 Null(NullConstant<'ctx>),
19 Struct(StructConstant<'ctx>),
20 Array(ArrayConstant<'ctx>),
21 Vector(VectorConstant<'ctx>),
22 BlockAddress(BlockAddress<'ctx>),
23 Undef(Undef<'ctx>),
24 Global(Global<'ctx>),
25 Function(Function<'ctx>),
26 ConstExpr(ConstExpr<'ctx>),
27 Other(GenericValue<'ctx>),
28}
29
30impl<'ctx> Constant<'ctx> {
31 pub fn as_operand(&self) -> Operand<'ctx> {
33 Operand::Constant(*self)
34 }
35}
36
37impl<'ctx> GetType<'ctx> for Constant<'ctx> {}
38
39impl<'ctx> ValueRef for Constant<'ctx> {
40 fn value_ref(&self) -> LLVMValueRef {
41 match self {
42 Self::Int(ic) => ic.value_ref(),
43 Self::Float(fc) => fc.value_ref(),
44 Self::Null(nc) => nc.value_ref(),
45 Self::Struct(sc) => sc.value_ref(),
46 Self::Array(ac) => ac.value_ref(),
47 Self::Vector(vc) => vc.value_ref(),
48 Self::BlockAddress(ba) => ba.value_ref(),
49 Self::Undef(ud) => ud.value_ref(),
50 Self::Global(gc) => gc.value_ref(),
51 Self::Function(fc) => fc.value_ref(),
52 Self::ConstExpr(cec) => cec.value_ref(),
53 Self::Other(oc) => oc.value_ref(),
54 }
55 }
56}
57
58impl<'ctx> FromLLVMValue for Constant<'ctx> {
59 fn from_llvm(ptr: LLVMValueRef) -> Self {
60 use LLVMValueKind::*;
61 match unsafe { LLVMGetValueKind(ptr) } {
62 LLVMConstantIntValueKind => Self::Int(IntConstant::from_llvm(ptr)),
63 LLVMConstantFPValueKind => Self::Float(FloatConstant::from_llvm(ptr)),
64 LLVMConstantPointerNullValueKind => Self::Null(NullConstant::from_llvm(ptr)),
65 LLVMConstantStructValueKind => Self::Struct(StructConstant::from_llvm(ptr)),
66 LLVMConstantArrayValueKind => Self::Array(ArrayConstant::from_llvm(ptr)),
67 LLVMConstantDataArrayValueKind => Self::Array(ArrayConstant::from_llvm(ptr)),
68 LLVMConstantVectorValueKind => Self::Vector(VectorConstant::from_llvm(ptr)),
69 LLVMConstantDataVectorValueKind => Self::Vector(VectorConstant::from_llvm(ptr)),
70 LLVMBlockAddressValueKind => Self::BlockAddress(BlockAddress::from_llvm(ptr)),
71 LLVMUndefValueValueKind => Self::Undef(Undef::from_llvm(ptr)),
72 LLVMGlobalIFuncValueKind | LLVMFunctionValueKind => Self::Function(Function::from_llvm(ptr)),
73 LLVMGlobalAliasValueKind | LLVMGlobalVariableValueKind => Self::Global(Global::from_llvm(ptr)),
74 LLVMConstantExprValueKind => Self::ConstExpr(ConstExpr::from_llvm(ptr)),
75 _ => Self::Other(GenericValue::from_llvm(ptr)),
76 }
77 }
78}
79
80impl<'ctx> AsConstant<'ctx> for Constant<'ctx> {
81 fn as_constant(&self) -> Constant<'ctx> {
82 self.clone()
83 }
84}
85
86impl_as_operand_for_constant!(Constant);