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
use llvm_sys::core::{LLVMIsAConstantVector, LLVMIsAConstantDataVector, LLVMConstInsertElement, LLVMConstExtractElement, LLVMIsConstantString, LLVMConstString, LLVMGetElementAsConstant, LLVMGetAsString, LLVMConstSelect, LLVMConstShuffleVector};
use llvm_sys::prelude::LLVMValueRef;

use std::ffi::CStr;

use crate::support::LLVMString;
use crate::types::{VectorType};
use crate::values::traits::AsValueRef;
use crate::values::{BasicValueEnum, BasicValue, InstructionValue, Value, IntValue, MetadataValue};

#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
pub struct VectorValue {
    vec_value: Value,
}

impl VectorValue {
    pub(crate) fn new(vector_value: LLVMValueRef) -> Self {
        assert!(!vector_value.is_null());

        VectorValue {
            vec_value: Value::new(vector_value)
        }
    }

    /// Determines whether or not a `VectorValue` is a constant.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use inkwell::context::Context;
    ///
    /// let context = Context::create();
    /// let i8_type = context.i8_type();
    /// let i8_vec_type = i8_type.vec_type(3);
    /// let i8_vec_zero = i8_vec_type.const_zero();
    ///
    /// assert!(i8_vec_zero.is_const());
    /// ```
    pub fn is_const(&self) -> bool {
        self.vec_value.is_const()
    }

    pub fn is_constant_vector(&self) -> bool {
        unsafe {
            !LLVMIsAConstantVector(self.as_value_ref()).is_null()
        }
    }

    pub fn is_constant_data_vector(&self) -> bool {
        unsafe {
            !LLVMIsAConstantDataVector(self.as_value_ref()).is_null()
        }
    }

    pub fn print_to_string(&self) -> LLVMString {
        self.vec_value.print_to_string()
    }

    pub fn print_to_stderr(&self) {
        self.vec_value.print_to_stderr()
    }

    pub fn get_name(&self) -> &CStr {
        self.vec_value.get_name()
    }

    pub fn set_name(&self, name: &str) {
        self.vec_value.set_name(name);
    }

    pub fn get_type(&self) -> VectorType {
        VectorType::new(self.vec_value.get_type())
    }

    pub fn is_null(&self) -> bool {
        self.vec_value.is_null()
    }

    pub fn is_undef(&self) -> bool {
        self.vec_value.is_undef()
    }

    pub fn as_instruction(&self) -> Option<InstructionValue> {
        self.vec_value.as_instruction()
    }

    pub fn const_extract_element(&self, index: IntValue) -> BasicValueEnum {
        let value = unsafe {
            LLVMConstExtractElement(self.as_value_ref(), index.as_value_ref())
        };

        BasicValueEnum::new(value)
    }

    // SubTypes: value should really be T in self: VectorValue<T> I think
    pub fn const_insert_element<BV: BasicValue>(&self, index: IntValue, value: BV) -> BasicValueEnum {
        let value = unsafe {
            LLVMConstInsertElement(self.as_value_ref(), value.as_value_ref(), index.as_value_ref())
        };

        BasicValueEnum::new(value)
    }

    pub fn has_metadata(&self) -> bool {
        self.vec_value.has_metadata()
    }

    pub fn get_metadata(&self, kind_id: u32) -> Option<MetadataValue> {
        self.vec_value.get_metadata(kind_id)
    }

    pub fn set_metadata(&self, metadata: MetadataValue, kind_id: u32) {
        self.vec_value.set_metadata(metadata, kind_id)
    }

    pub fn replace_all_uses_with(&self, other: VectorValue) {
        self.vec_value.replace_all_uses_with(other.as_value_ref())
    }

    /// Creates a const string which may be null terminated.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use inkwell::values::VectorValue;
    ///
    /// let string = VectorValue::const_string("my_string", false);
    ///
    /// assert_eq!(string.print_to_string().to_string(), "[9 x i8] c\"my_string\"");
    /// ```
    // SubTypes: Should return VectorValue<IntValue<i8>>
    pub fn const_string(string: &str, null_terminated: bool) -> Self {
        let ptr = unsafe {
            LLVMConstString(string.as_ptr() as *const i8, string.len() as u32, !null_terminated as i32)
        };

        VectorValue::new(ptr)
    }

    /// Creates a const string which may be null terminated.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use inkwell::values::VectorValue;
    ///
    /// let string = VectorValue::const_string("my_string", false);
    ///
    /// assert!(string.is_const_string());
    /// ```
    // SubTypes: Impl only for VectorValue<IntValue<i8>>
    pub fn is_const_string(&self) -> bool {
        unsafe {
            LLVMIsConstantString(self.as_value_ref()) == 1
        }
    }

    // SubTypes: Impl only for VectorValue<IntValue<i8>>
    pub fn get_string_constant(&self) -> &CStr {
        // REVIEW: Maybe need to check is_const_string?

        let mut len = 0;
        let ptr = unsafe {
            LLVMGetAsString(self.as_value_ref(), &mut len)
        };

        unsafe {
            CStr::from_ptr(ptr)
        }
    }

    // TODOC: Value seems to be zero initialized if index out of bounds
    // SubType: VectorValue<BV> -> BV
    pub fn get_element_as_constant(&self, index: u32) -> BasicValueEnum {
        let ptr = unsafe {
            LLVMGetElementAsConstant(self.as_value_ref(), index)
        };

        BasicValueEnum::new(ptr)
    }

    // SubTypes: self can only be VectoValue<IntValue<bool>>
    pub fn const_select<BV: BasicValue>(&self, then: BV, else_: BV) -> BasicValueEnum {
        let value = unsafe {
            LLVMConstSelect(self.as_value_ref(), then.as_value_ref(), else_.as_value_ref())
        };

        BasicValueEnum::new(value)
    }

    // SubTypes: <V: VectorValue<T, Const>> self: V, right: V, mask: V -> V
    pub fn const_shuffle_vector(&self, right: VectorValue, mask: VectorValue) -> VectorValue {
        let value = unsafe {
            LLVMConstShuffleVector(self.as_value_ref(), right.as_value_ref(), mask.as_value_ref())
        };

        VectorValue::new(value)
    }
}

impl AsValueRef for VectorValue {
    fn as_value_ref(&self) -> LLVMValueRef {
        self.vec_value.value
    }
}