Skip to content

Commit e85bc09

Browse files
authored
Merge pull request rust-lang#15 from 6A/copy_and_clone
Added Copy + Clone traits to values & types
2 parents c9a751b + df22627 commit e85bc09

22 files changed

+53
-20
lines changed

src/types/array_type.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use types::traits::AsTypeRef;
88
use types::{Type, BasicType, PointerType, FunctionType};
99
use values::{BasicValue, ArrayValue, PointerValue, IntValue};
1010

11-
#[derive(Debug, PartialEq, Eq)]
11+
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
1212
pub struct ArrayType {
1313
array_type: Type,
1414
}

src/types/float_type.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use types::traits::AsTypeRef;
99
use types::{Type, PointerType, FunctionType, BasicType, ArrayType, VectorType};
1010
use values::{FloatValue, GenericValue, PointerValue, IntValue};
1111

12-
#[derive(Debug, PartialEq, Eq)]
12+
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
1313
pub struct FloatType {
1414
float_type: Type,
1515
}

src/types/fn_type.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use types::traits::AsTypeRef;
1010
use types::{Type, BasicTypeEnum};
1111
// use values::FunctionValue;
1212

13-
#[derive(PartialEq, Eq)]
13+
#[derive(PartialEq, Eq, Clone, Copy)]
1414
pub struct FunctionType {
1515
fn_type: Type,
1616
}

src/types/int_type.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use types::traits::AsTypeRef;
99
use types::{Type, ArrayType, BasicType, VectorType, PointerType, FunctionType};
1010
use values::{GenericValue, IntValue, PointerValue};
1111

12-
#[derive(Debug, PartialEq, Eq)]
12+
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
1313
pub struct IntType {
1414
int_type: Type,
1515
}

src/types/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use values::{IntValue, PointerValue};
3434
// Worth noting that types seem to be singletons. At the very least, primitives are.
3535
// Though this is likely only true per thread since LLVM claims to not be very thread-safe.
3636
// REVIEW: Maybe move this into its own module?
37-
#[derive(PartialEq, Eq)]
37+
#[derive(PartialEq, Eq, Clone, Copy)]
3838
struct Type {
3939
type_: LLVMTypeRef,
4040
}

src/types/ptr_type.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use types::traits::AsTypeRef;
88
use types::{Type, BasicType, ArrayType, FunctionType, VectorType};
99
use values::{PointerValue, IntValue};
1010

11-
#[derive(Debug, PartialEq, Eq)]
11+
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
1212
pub struct PointerType {
1313
ptr_type: Type,
1414
}

src/types/struct_type.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use types::traits::AsTypeRef;
99
use types::{Type, BasicType, BasicTypeEnum, ArrayType, PointerType, FunctionType, VectorType};
1010
use values::{BasicValue, StructValue, PointerValue, IntValue};
1111

12-
#[derive(Debug, PartialEq, Eq)]
12+
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
1313
pub struct StructType {
1414
struct_type: Type,
1515
}

src/types/vec_type.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use values::{BasicValue, PointerValue, VectorValue, IntValue};
99

1010
// REVIEW: vec_type() is impl for IntType & FloatType. Need to
1111
// find out if it is valid for other types too. Maybe PointerType?
12-
#[derive(Debug, PartialEq, Eq)]
12+
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
1313
pub struct VectorType {
1414
vec_type: Type,
1515
}

src/types/void_type.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use values::PointerValue;
88

99
use std::ffi::CStr;
1010

11-
#[derive(Debug, PartialEq, Eq)]
11+
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
1212
pub struct VoidType {
1313
void_type: Type,
1414
}

src/values/array_value.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use types::ArrayType;
88
use values::traits::AsValueRef;
99
use values::{Value, InstructionValue, MetadataValue};
1010

11-
#[derive(PartialEq, Eq)]
11+
#[derive(PartialEq, Eq, Clone, Copy)]
1212
pub struct ArrayValue {
1313
array_value: Value
1414
}

src/values/float_value.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use types::{AsTypeRef, FloatType, IntType};
88
use values::traits::AsValueRef;
99
use values::{InstructionValue, IntValue, Value, MetadataValue};
1010

11-
#[derive(Debug, PartialEq, Eq)]
11+
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
1212
pub struct FloatValue {
1313
float_value: Value
1414
}

src/values/fn_value.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use types::{BasicTypeEnum, FunctionType};
1212
use values::traits::AsValueRef;
1313
use values::{BasicValueEnum, Value, MetadataValue};
1414

15-
#[derive(PartialEq, Eq)]
15+
#[derive(PartialEq, Eq, Clone, Copy)]
1616
pub struct FunctionValue {
1717
fn_value: Value,
1818
}

src/values/instruction_value.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl InstructionOpcode {
151151
}
152152
}
153153

154-
#[derive(Debug, PartialEq, Eq)]
154+
#[derive(Debug, PartialEq, Eq, Copy)]
155155
pub struct InstructionValue {
156156
instruction_value: Value,
157157
}
@@ -231,6 +231,8 @@ impl InstructionValue {
231231
}
232232

233233
impl Clone for InstructionValue {
234+
/// Creates a clone of this `InstructionValue`, and returns it.
235+
/// The clone will have no parent, and no name.
234236
fn clone(&self) -> Self {
235237
let value = unsafe {
236238
LLVMInstructionClone(self.as_value_ref())

src/values/int_value.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use types::{AsTypeRef, FloatType, PointerType, IntType};
88
use values::traits::AsValueRef;
99
use values::{FloatValue, InstructionValue, PointerValue, Value, MetadataValue};
1010

11-
#[derive(Debug, PartialEq, Eq)]
11+
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
1212
pub struct IntValue {
1313
int_value: Value,
1414
}

src/values/metadata_value.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::slice::from_raw_parts;
1111

1212
pub const FIRST_CUSTOM_METADATA_KIND_ID: u32 = 14; // TODO: Varies by version
1313

14-
#[derive(PartialEq, Eq)]
14+
#[derive(PartialEq, Eq, Clone, Copy)]
1515
pub struct MetadataValue {
1616
metadata_value: Value,
1717
}

src/values/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use llvm_sys::prelude::{LLVMValueRef, LLVMTypeRef};
3333
use std::ffi::{CString, CStr};
3434
use std::fmt;
3535

36-
#[derive(PartialEq, Eq)]
36+
#[derive(PartialEq, Eq, Clone, Copy)]
3737
struct Value {
3838
value: LLVMValueRef,
3939
}

src/values/phi_value.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use values::traits::AsValueRef;
88
use values::{BasicValue, BasicValueEnum, InstructionValue, Value};
99

1010
// REVIEW: Metadata for phi values?
11-
#[derive(Debug, PartialEq, Eq)]
11+
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
1212
pub struct PhiValue {
1313
phi_value: Value
1414
}

src/values/ptr_value.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::ffi::CStr;
66
use types::{AsTypeRef, IntType, PointerType};
77
use values::{AsValueRef, InstructionValue, IntValue, Value, MetadataValue};
88

9-
#[derive(Debug, PartialEq, Eq)]
9+
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
1010
pub struct PointerValue {
1111
ptr_value: Value,
1212
}

src/values/struct_value.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use types::StructType;
66
use values::traits::AsValueRef;
77
use values::{InstructionValue, Value, MetadataValue};
88

9-
#[derive(Debug, PartialEq, Eq)]
9+
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
1010
pub struct StructValue {
1111
struct_value: Value
1212
}

src/values/vec_value.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use types::{VectorType};
77
use values::traits::AsValueRef;
88
use values::{BasicValueEnum, BasicValue, InstructionValue, Value, IntValue, MetadataValue};
99

10-
#[derive(Debug, PartialEq, Eq)]
10+
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
1111
pub struct VectorValue {
1212
vec_value: Value,
1313
}

tests/test_types.rs

+9
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,12 @@ fn test_vec_type() {
221221

222222
assert_eq!(vec_type.get_size(), 42);
223223
}
224+
225+
#[test]
226+
fn test_type_copies() {
227+
let context = Context::create();
228+
let i8_type = context.i8_type();
229+
let i8_type_copy = i8_type;
230+
231+
assert_eq!(i8_type, i8_type_copy);
232+
}

tests/test_values.rs

+22
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ fn test_instructions() {
5353
assert_eq!(ptr.as_instruction().unwrap().get_opcode(), IntToPtr);
5454
assert_eq!(free_instruction.get_opcode(), Call);
5555
assert_eq!(return_instruction.get_opcode(), Return);
56+
57+
// test instruction cloning
58+
let instruction_clone = return_instruction.clone();
59+
60+
assert_eq!(instruction_clone.get_opcode(), return_instruction.get_opcode());
61+
assert_ne!(instruction_clone, return_instruction);
62+
63+
// test copying
64+
let instruction_clone_copy = instruction_clone;
65+
66+
assert_eq!(instruction_clone, instruction_clone_copy);
5667
}
5768

5869
#[test]
@@ -683,3 +694,14 @@ fn test_int_from_string() {
683694

684695
assert_eq!(i8_val.print_to_string(), &*CString::new("i8 -15").unwrap());
685696
}
697+
698+
#[test]
699+
fn test_value_copies() {
700+
let context = Context::create();
701+
let i8_type = context.i8_type();
702+
703+
let i8_value = i8_type.const_int(12, false);
704+
let i8_value_copy = i8_value;
705+
706+
assert_eq!(i8_value, i8_value_copy);
707+
}

0 commit comments

Comments
 (0)