Skip to content

Commit

Permalink
Merge pull request rust-lang#15 from 6A/copy_and_clone
Browse files Browse the repository at this point in the history
Added Copy + Clone traits to values & types
  • Loading branch information
TheDan64 authored Sep 29, 2017
2 parents c9a751b + df22627 commit e85bc09
Show file tree
Hide file tree
Showing 22 changed files with 53 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/types/array_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use types::traits::AsTypeRef;
use types::{Type, BasicType, PointerType, FunctionType};
use values::{BasicValue, ArrayValue, PointerValue, IntValue};

#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct ArrayType {
array_type: Type,
}
Expand Down
2 changes: 1 addition & 1 deletion src/types/float_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use types::traits::AsTypeRef;
use types::{Type, PointerType, FunctionType, BasicType, ArrayType, VectorType};
use values::{FloatValue, GenericValue, PointerValue, IntValue};

#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct FloatType {
float_type: Type,
}
Expand Down
2 changes: 1 addition & 1 deletion src/types/fn_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use types::traits::AsTypeRef;
use types::{Type, BasicTypeEnum};
// use values::FunctionValue;

#[derive(PartialEq, Eq)]
#[derive(PartialEq, Eq, Clone, Copy)]
pub struct FunctionType {
fn_type: Type,
}
Expand Down
2 changes: 1 addition & 1 deletion src/types/int_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use types::traits::AsTypeRef;
use types::{Type, ArrayType, BasicType, VectorType, PointerType, FunctionType};
use values::{GenericValue, IntValue, PointerValue};

#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct IntType {
int_type: Type,
}
Expand Down
2 changes: 1 addition & 1 deletion src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use values::{IntValue, PointerValue};
// Worth noting that types seem to be singletons. At the very least, primitives are.
// Though this is likely only true per thread since LLVM claims to not be very thread-safe.
// REVIEW: Maybe move this into its own module?
#[derive(PartialEq, Eq)]
#[derive(PartialEq, Eq, Clone, Copy)]
struct Type {
type_: LLVMTypeRef,
}
Expand Down
2 changes: 1 addition & 1 deletion src/types/ptr_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use types::traits::AsTypeRef;
use types::{Type, BasicType, ArrayType, FunctionType, VectorType};
use values::{PointerValue, IntValue};

#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct PointerType {
ptr_type: Type,
}
Expand Down
2 changes: 1 addition & 1 deletion src/types/struct_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use types::traits::AsTypeRef;
use types::{Type, BasicType, BasicTypeEnum, ArrayType, PointerType, FunctionType, VectorType};
use values::{BasicValue, StructValue, PointerValue, IntValue};

#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct StructType {
struct_type: Type,
}
Expand Down
2 changes: 1 addition & 1 deletion src/types/vec_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use values::{BasicValue, PointerValue, VectorValue, IntValue};

// REVIEW: vec_type() is impl for IntType & FloatType. Need to
// find out if it is valid for other types too. Maybe PointerType?
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct VectorType {
vec_type: Type,
}
Expand Down
2 changes: 1 addition & 1 deletion src/types/void_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use values::PointerValue;

use std::ffi::CStr;

#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct VoidType {
void_type: Type,
}
Expand Down
2 changes: 1 addition & 1 deletion src/values/array_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use types::ArrayType;
use values::traits::AsValueRef;
use values::{Value, InstructionValue, MetadataValue};

#[derive(PartialEq, Eq)]
#[derive(PartialEq, Eq, Clone, Copy)]
pub struct ArrayValue {
array_value: Value
}
Expand Down
2 changes: 1 addition & 1 deletion src/values/float_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use types::{AsTypeRef, FloatType, IntType};
use values::traits::AsValueRef;
use values::{InstructionValue, IntValue, Value, MetadataValue};

#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct FloatValue {
float_value: Value
}
Expand Down
2 changes: 1 addition & 1 deletion src/values/fn_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use types::{BasicTypeEnum, FunctionType};
use values::traits::AsValueRef;
use values::{BasicValueEnum, Value, MetadataValue};

#[derive(PartialEq, Eq)]
#[derive(PartialEq, Eq, Clone, Copy)]
pub struct FunctionValue {
fn_value: Value,
}
Expand Down
4 changes: 3 additions & 1 deletion src/values/instruction_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl InstructionOpcode {
}
}

#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Copy)]
pub struct InstructionValue {
instruction_value: Value,
}
Expand Down Expand Up @@ -231,6 +231,8 @@ impl InstructionValue {
}

impl Clone for InstructionValue {
/// Creates a clone of this `InstructionValue`, and returns it.
/// The clone will have no parent, and no name.
fn clone(&self) -> Self {
let value = unsafe {
LLVMInstructionClone(self.as_value_ref())
Expand Down
2 changes: 1 addition & 1 deletion src/values/int_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use types::{AsTypeRef, FloatType, PointerType, IntType};
use values::traits::AsValueRef;
use values::{FloatValue, InstructionValue, PointerValue, Value, MetadataValue};

#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct IntValue {
int_value: Value,
}
Expand Down
2 changes: 1 addition & 1 deletion src/values/metadata_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::slice::from_raw_parts;

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

#[derive(PartialEq, Eq)]
#[derive(PartialEq, Eq, Clone, Copy)]
pub struct MetadataValue {
metadata_value: Value,
}
Expand Down
2 changes: 1 addition & 1 deletion src/values/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use llvm_sys::prelude::{LLVMValueRef, LLVMTypeRef};
use std::ffi::{CString, CStr};
use std::fmt;

#[derive(PartialEq, Eq)]
#[derive(PartialEq, Eq, Clone, Copy)]
struct Value {
value: LLVMValueRef,
}
Expand Down
2 changes: 1 addition & 1 deletion src/values/phi_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use values::traits::AsValueRef;
use values::{BasicValue, BasicValueEnum, InstructionValue, Value};

// REVIEW: Metadata for phi values?
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct PhiValue {
phi_value: Value
}
Expand Down
2 changes: 1 addition & 1 deletion src/values/ptr_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::ffi::CStr;
use types::{AsTypeRef, IntType, PointerType};
use values::{AsValueRef, InstructionValue, IntValue, Value, MetadataValue};

#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct PointerValue {
ptr_value: Value,
}
Expand Down
2 changes: 1 addition & 1 deletion src/values/struct_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use types::StructType;
use values::traits::AsValueRef;
use values::{InstructionValue, Value, MetadataValue};

#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct StructValue {
struct_value: Value
}
Expand Down
2 changes: 1 addition & 1 deletion src/values/vec_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use types::{VectorType};
use values::traits::AsValueRef;
use values::{BasicValueEnum, BasicValue, InstructionValue, Value, IntValue, MetadataValue};

#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct VectorValue {
vec_value: Value,
}
Expand Down
9 changes: 9 additions & 0 deletions tests/test_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,12 @@ fn test_vec_type() {

assert_eq!(vec_type.get_size(), 42);
}

#[test]
fn test_type_copies() {
let context = Context::create();
let i8_type = context.i8_type();
let i8_type_copy = i8_type;

assert_eq!(i8_type, i8_type_copy);
}
22 changes: 22 additions & 0 deletions tests/test_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ fn test_instructions() {
assert_eq!(ptr.as_instruction().unwrap().get_opcode(), IntToPtr);
assert_eq!(free_instruction.get_opcode(), Call);
assert_eq!(return_instruction.get_opcode(), Return);

// test instruction cloning
let instruction_clone = return_instruction.clone();

assert_eq!(instruction_clone.get_opcode(), return_instruction.get_opcode());
assert_ne!(instruction_clone, return_instruction);

// test copying
let instruction_clone_copy = instruction_clone;

assert_eq!(instruction_clone, instruction_clone_copy);
}

#[test]
Expand Down Expand Up @@ -683,3 +694,14 @@ fn test_int_from_string() {

assert_eq!(i8_val.print_to_string(), &*CString::new("i8 -15").unwrap());
}

#[test]
fn test_value_copies() {
let context = Context::create();
let i8_type = context.i8_type();

let i8_value = i8_type.const_int(12, false);
let i8_value_copy = i8_value;

assert_eq!(i8_value, i8_value_copy);
}

0 comments on commit e85bc09

Please sign in to comment.