Skip to content

Commit 6074d20

Browse files
authored
Merge pull request rust-lang#132 from seanyoung/metadata
Move MetadataValue methods to InstructionValue
2 parents 832955a + 6ba88b8 commit 6074d20

File tree

11 files changed

+91
-206
lines changed

11 files changed

+91
-206
lines changed

src/context.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -648,10 +648,21 @@ impl Context {
648648
/// let f32_zero = f32_type.const_float(0.);
649649
/// let md_node = context.metadata_node(&[i8_two.into(), f32_zero.into()]);
650650
/// let f32_one = f32_type.const_float(1.);
651+
/// let void_type = context.void_type();
652+
///
653+
/// let builder = context.create_builder();
654+
/// let module = context.create_module("my_mod");
655+
/// let fn_type = void_type.fn_type(&[f32_type.into()], false);
656+
/// let fn_value = module.add_function("my_func", fn_type, None);
657+
/// let entry_block = fn_value.append_basic_block("entry");
658+
///
659+
/// builder.position_at_end(&entry_block);
660+
///
661+
/// let ret_instr = builder.build_return(None);
651662
///
652663
/// assert!(md_node.is_node());
653664
///
654-
/// f32_one.set_metadata(md_node, 0);
665+
/// ret_instr.set_metadata(md_node, 0);
655666
/// ```
656667
// REVIEW: Maybe more helpful to beginners to call this metadata_tuple?
657668
// REVIEW: Seems to be unassgned to anything
@@ -678,12 +689,23 @@ impl Context {
678689
/// let md_string = context.metadata_string("Floats are awesome!");
679690
/// let f32_type = context.f32_type();
680691
/// let f32_one = f32_type.const_float(1.);
692+
/// let void_type = context.void_type();
693+
///
694+
/// let builder = context.create_builder();
695+
/// let module = context.create_module("my_mod");
696+
/// let fn_type = void_type.fn_type(&[f32_type.into()], false);
697+
/// let fn_value = module.add_function("my_func", fn_type, None);
698+
/// let entry_block = fn_value.append_basic_block("entry");
699+
///
700+
/// builder.position_at_end(&entry_block);
701+
///
702+
/// let ret_instr = builder.build_return(None);
681703
///
682704
/// assert!(md_string.is_string());
683705
///
684-
/// f32_one.set_metadata(md_string, 0);
706+
/// ret_instr.set_metadata(md_string, 0);
685707
/// ```
686-
// REVIEW: Seems to be unassgned to anything
708+
// REVIEW: Seems to be unassigned to anything
687709
pub fn metadata_string(&self, string: &str) -> MetadataValue {
688710
let c_string = CString::new(string).expect("Conversion to CString failed unexpectedly");
689711

src/values/array_value.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::fmt;
77
use crate::support::LLVMString;
88
use crate::types::ArrayType;
99
use crate::values::traits::AsValueRef;
10-
use crate::values::{Value, InstructionValue, MetadataValue};
10+
use crate::values::{Value, InstructionValue};
1111

1212
/// An `ArrayValue` is a block of contiguous constants or variables.
1313
#[derive(PartialEq, Eq, Clone, Copy, Hash)]
@@ -65,21 +65,6 @@ impl ArrayValue {
6565
self.array_value.as_instruction()
6666
}
6767

68-
/// Determines whether or not this `ArrayValue` has any associated metadata.
69-
pub fn has_metadata(&self) -> bool {
70-
self.array_value.has_metadata()
71-
}
72-
73-
/// Gets the `MetadataValue` associated with this `ArrayValue` at a specific
74-
/// `kind_id`.
75-
pub fn get_metadata(&self, kind_id: u32) -> Option<MetadataValue> {
76-
self.array_value.get_metadata(kind_id)
77-
}
78-
79-
/// Assigns a `MetadataValue` to this `ArrayValue` at a specific `kind_id`.
80-
pub fn set_metadata(&self, metadata: MetadataValue, kind_id: u32) {
81-
self.array_value.set_metadata(metadata, kind_id)
82-
}
8368

8469
/// Replaces all uses of this value with another value of the same type.
8570
/// If used incorrectly this may result in invalid IR.

src/values/float_value.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::FloatPredicate;
77
use crate::support::LLVMString;
88
use crate::types::{AsTypeRef, FloatType, IntType};
99
use crate::values::traits::AsValueRef;
10-
use crate::values::{InstructionValue, IntValue, Value, MetadataValue};
10+
use crate::values::{InstructionValue, IntValue, Value};
1111

1212
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
1313
pub struct FloatValue {
@@ -143,18 +143,6 @@ impl FloatValue {
143143
FloatValue::new(value)
144144
}
145145

146-
pub fn has_metadata(&self) -> bool {
147-
self.float_value.has_metadata()
148-
}
149-
150-
pub fn get_metadata(&self, kind_id: u32) -> Option<MetadataValue> {
151-
self.float_value.get_metadata(kind_id)
152-
}
153-
154-
pub fn set_metadata(&self, metadata: MetadataValue, kind_id: u32) {
155-
self.float_value.set_metadata(metadata, kind_id)
156-
}
157-
158146
// SubType: rhs same as lhs; return IntValue<bool>
159147
pub fn const_compare(&self, op: FloatPredicate, rhs: FloatValue) -> IntValue {
160148
let value = unsafe {

src/values/fn_value.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::module::Linkage;
1717
use crate::support::LLVMString;
1818
use crate::types::{FunctionType, PointerType};
1919
use crate::values::traits::AsValueRef;
20-
use crate::values::{BasicValueEnum, GlobalValue, MetadataValue, Value};
20+
use crate::values::{BasicValueEnum, GlobalValue, Value};
2121

2222
#[derive(PartialEq, Eq, Clone, Copy, Hash)]
2323
pub struct FunctionValue {
@@ -243,18 +243,6 @@ impl FunctionValue {
243243
ptr_type.get_element_type().into_function_type()
244244
}
245245

246-
pub fn has_metadata(&self) -> bool {
247-
self.fn_value.has_metadata()
248-
}
249-
250-
pub fn get_metadata(&self, kind_id: u32) -> Option<MetadataValue> {
251-
self.fn_value.get_metadata(kind_id)
252-
}
253-
254-
pub fn set_metadata(&self, metadata: MetadataValue, kind_id: u32) {
255-
self.fn_value.set_metadata(metadata, kind_id)
256-
}
257-
258246
// TODOC: How this works as an exception handler
259247
#[llvm_versions(3.9..=latest)]
260248
pub fn has_personality_function(&self) -> bool {

src/values/instruction_value.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use either::{Either, Either::{Left, Right}};
2-
use llvm_sys::core::{LLVMGetAlignment, LLVMSetAlignment, LLVMGetInstructionOpcode, LLVMIsTailCall, LLVMGetPreviousInstruction, LLVMGetNextInstruction, LLVMGetInstructionParent, LLVMInstructionEraseFromParent, LLVMInstructionClone, LLVMSetVolatile, LLVMGetVolatile, LLVMGetNumOperands, LLVMGetOperand, LLVMGetOperandUse, LLVMSetOperand, LLVMValueAsBasicBlock, LLVMIsABasicBlock, LLVMGetICmpPredicate, LLVMGetFCmpPredicate, LLVMIsAAllocaInst, LLVMIsALoadInst, LLVMIsAStoreInst};
2+
use llvm_sys::core::{LLVMGetAlignment, LLVMSetAlignment, LLVMGetInstructionOpcode, LLVMIsTailCall, LLVMGetPreviousInstruction, LLVMGetNextInstruction, LLVMGetInstructionParent, LLVMInstructionEraseFromParent, LLVMInstructionClone, LLVMSetVolatile, LLVMGetVolatile, LLVMGetNumOperands, LLVMGetOperand, LLVMGetOperandUse, LLVMSetOperand, LLVMValueAsBasicBlock, LLVMIsABasicBlock, LLVMGetICmpPredicate, LLVMGetFCmpPredicate, LLVMIsAAllocaInst, LLVMIsALoadInst, LLVMIsAStoreInst, LLVMGetMetadata, LLVMHasMetadata, LLVMSetMetadata};
33
#[llvm_versions(3.8..=latest)]
44
use llvm_sys::core::{LLVMGetOrdering, LLVMSetOrdering};
55
#[llvm_versions(3.9..=latest)]
@@ -9,7 +9,7 @@ use llvm_sys::prelude::LLVMValueRef;
99

1010
use crate::basic_block::BasicBlock;
1111
use crate::values::traits::AsValueRef;
12-
use crate::values::{BasicValue, BasicValueEnum, BasicValueUse, Value};
12+
use crate::values::{BasicValue, BasicValueEnum, BasicValueUse, Value, MetadataValue};
1313
use crate::{AtomicOrdering, IntPredicate, FloatPredicate};
1414

1515
// REVIEW: Split up into structs for SubTypes on InstructionValues?
@@ -586,6 +586,35 @@ impl InstructionValue {
586586
None
587587
}
588588
}
589+
590+
/// Determines whether or not this `Instruction` has any associated metadata.
591+
pub fn has_metadata(&self) -> bool {
592+
unsafe {
593+
LLVMHasMetadata(self.instruction_value.value) == 1
594+
}
595+
}
596+
597+
/// Gets the `MetadataValue` associated with this `Instruction` at a specific
598+
/// `kind_id`.
599+
pub fn get_metadata(&self, kind_id: u32) -> Option<MetadataValue> {
600+
let metadata_value = unsafe {
601+
LLVMGetMetadata(self.instruction_value.value, kind_id)
602+
};
603+
604+
if metadata_value.is_null() {
605+
return None;
606+
}
607+
608+
Some(MetadataValue::new(metadata_value))
609+
}
610+
611+
/// Determines whether or not this `Instruction` has any associated metadata
612+
/// `kind_id`.
613+
pub fn set_metadata(&self, metadata: MetadataValue, kind_id: u32) {
614+
unsafe {
615+
LLVMSetMetadata(self.instruction_value.value, kind_id, metadata.as_value_ref())
616+
}
617+
}
589618
}
590619

591620
impl Clone for InstructionValue {

src/values/int_value.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::IntPredicate;
99
use crate::support::LLVMString;
1010
use crate::types::{AsTypeRef, FloatType, PointerType, IntType};
1111
use crate::values::traits::AsValueRef;
12-
use crate::values::{BasicValue, BasicValueEnum, FloatValue, InstructionValue, PointerValue, Value, MetadataValue};
12+
use crate::values::{BasicValue, BasicValueEnum, FloatValue, InstructionValue, PointerValue, Value};
1313

1414
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
1515
pub struct IntValue {
@@ -355,18 +355,6 @@ impl IntValue {
355355
IntValue::new(value)
356356
}
357357

358-
pub fn has_metadata(&self) -> bool {
359-
self.int_value.has_metadata()
360-
}
361-
362-
pub fn get_metadata(&self, kind_id: u32) -> Option<MetadataValue> {
363-
self.int_value.get_metadata(kind_id)
364-
}
365-
366-
pub fn set_metadata(&self, metadata: MetadataValue, kind_id: u32) {
367-
self.int_value.set_metadata(metadata, kind_id)
368-
}
369-
370358
// SubType: rhs same as lhs; return IntValue<bool>
371359
pub fn const_int_compare(&self, op: IntPredicate, rhs: IntValue) -> IntValue {
372360
let value = unsafe {

src/values/mod.rs

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub use crate::values::traits::{AnyValue, AggregateValue, BasicValue, IntMathVal
4040
pub use crate::values::vec_value::VectorValue;
4141
pub(crate) use crate::values::traits::AsValueRef;
4242

43-
use llvm_sys::core::{LLVMIsConstant, LLVMIsNull, LLVMIsUndef, LLVMPrintTypeToString, LLVMPrintValueToString, LLVMTypeOf, LLVMDumpValue, LLVMIsAInstruction, LLVMGetMetadata, LLVMHasMetadata, LLVMSetMetadata, LLVMReplaceAllUsesWith, LLVMGetFirstUse};
43+
use llvm_sys::core::{LLVMIsConstant, LLVMIsNull, LLVMIsUndef, LLVMPrintTypeToString, LLVMPrintValueToString, LLVMTypeOf, LLVMDumpValue, LLVMIsAInstruction, LLVMReplaceAllUsesWith, LLVMGetFirstUse};
4444
use llvm_sys::prelude::{LLVMValueRef, LLVMTypeRef};
4545

4646
use std::ffi::CStr;
@@ -167,32 +167,6 @@ impl Value {
167167
}
168168
}
169169

170-
fn has_metadata(&self) -> bool {
171-
unsafe {
172-
LLVMHasMetadata(self.value) == 1
173-
}
174-
}
175-
176-
// SubTypes: -> Option<MetadataValue<Node>>
177-
// TODOC: This always returns a metadata node, which can be used to get its node values
178-
fn get_metadata(&self, kind_id: u32) -> Option<MetadataValue> {
179-
let metadata_value = unsafe {
180-
LLVMGetMetadata(self.value, kind_id)
181-
};
182-
183-
if metadata_value.is_null() {
184-
return None;
185-
}
186-
187-
Some(MetadataValue::new(metadata_value))
188-
}
189-
190-
fn set_metadata(&self, metadata: MetadataValue, kind_id: u32) {
191-
unsafe {
192-
LLVMSetMetadata(self.value, kind_id, metadata.as_value_ref())
193-
}
194-
}
195-
196170
// REVIEW: I think this is memory safe, though it may result in an IR error
197171
// if used incorrectly, which is OK.
198172
fn replace_all_uses_with(&self, other: LLVMValueRef) {

src/values/ptr_value.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::ffi::CStr;
55

66
use crate::support::LLVMString;
77
use crate::types::{AsTypeRef, IntType, PointerType};
8-
use crate::values::{AsValueRef, InstructionValue, IntValue, Value, MetadataValue};
8+
use crate::values::{AsValueRef, InstructionValue, IntValue, Value};
99

1010
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
1111
pub struct PointerValue {
@@ -68,18 +68,6 @@ impl PointerValue {
6868
self.ptr_value.as_instruction()
6969
}
7070

71-
pub fn has_metadata(&self) -> bool {
72-
self.ptr_value.has_metadata()
73-
}
74-
75-
pub fn get_metadata(&self, kind_id: u32) -> Option<MetadataValue> {
76-
self.ptr_value.get_metadata(kind_id)
77-
}
78-
79-
pub fn set_metadata(&self, metadata: MetadataValue, kind_id: u32) {
80-
self.ptr_value.set_metadata(metadata, kind_id)
81-
}
82-
8371
// REVIEW: Should this be on array value too?
8472
/// GEP is very likely to segfault if indexes are used incorrectly, and is therefore an unsafe function. Maybe we can change this in the future.
8573
pub unsafe fn const_gep(&self, ordered_indexes: &[IntValue]) -> PointerValue {

src/values/struct_value.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::ffi::CStr;
55
use crate::support::LLVMString;
66
use crate::types::StructType;
77
use crate::values::traits::AsValueRef;
8-
use crate::values::{InstructionValue, Value, MetadataValue};
8+
use crate::values::{InstructionValue, Value};
99

1010
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
1111
pub struct StructValue {
@@ -53,18 +53,6 @@ impl StructValue {
5353
self.struct_value.as_instruction()
5454
}
5555

56-
pub fn has_metadata(&self) -> bool {
57-
self.struct_value.has_metadata()
58-
}
59-
60-
pub fn get_metadata(&self, kind_id: u32) -> Option<MetadataValue> {
61-
self.struct_value.get_metadata(kind_id)
62-
}
63-
64-
pub fn set_metadata(&self, metadata: MetadataValue, kind_id: u32) {
65-
self.struct_value.set_metadata(metadata, kind_id)
66-
}
67-
6856
pub fn replace_all_uses_with(&self, other: StructValue) {
6957
self.struct_value.replace_all_uses_with(other.as_value_ref())
7058
}

src/values/vec_value.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::ffi::CStr;
66
use crate::support::LLVMString;
77
use crate::types::{VectorType};
88
use crate::values::traits::AsValueRef;
9-
use crate::values::{BasicValueEnum, BasicValue, InstructionValue, Value, IntValue, MetadataValue};
9+
use crate::values::{BasicValueEnum, BasicValue, InstructionValue, Value, IntValue};
1010

1111
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
1212
pub struct VectorValue {
@@ -101,18 +101,6 @@ impl VectorValue {
101101
BasicValueEnum::new(value)
102102
}
103103

104-
pub fn has_metadata(&self) -> bool {
105-
self.vec_value.has_metadata()
106-
}
107-
108-
pub fn get_metadata(&self, kind_id: u32) -> Option<MetadataValue> {
109-
self.vec_value.get_metadata(kind_id)
110-
}
111-
112-
pub fn set_metadata(&self, metadata: MetadataValue, kind_id: u32) {
113-
self.vec_value.set_metadata(metadata, kind_id)
114-
}
115-
116104
pub fn replace_all_uses_with(&self, other: VectorValue) {
117105
self.vec_value.replace_all_uses_with(other.as_value_ref())
118106
}

0 commit comments

Comments
 (0)