Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factor out PointerIdentity #11

Merged
merged 1 commit into from Jan 17, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions lib/llvm.rb
Expand Up @@ -9,4 +9,27 @@ module C
extend ::FFI::Library
ffi_lib ['LLVM-3.2']
end

module PointerIdentity
# @private
def to_ptr
@ptr
end

# Checks if the value is equal to other.
def ==(other)
other.respond_to?(:to_ptr) &&
@ptr == other.to_ptr
end

# Computes hash.
def hash
@ptr.address.hash
end

# Checks if the value is equivalent to other.
def eql?(other)
self == other
end
end
end
22 changes: 2 additions & 20 deletions lib/llvm/core/module.rb
@@ -1,5 +1,7 @@
module LLVM
class Module
include PointerIdentity

# @private
def self.from_ptr(ptr)
return if ptr.null?
Expand All @@ -19,26 +21,6 @@ def dispose
@ptr = nil
end

# @private
def to_ptr
@ptr
end

# Checks if the module is equal to other.
def ==(other)
case other
when LLVM::Module
@ptr == other.to_ptr
else
false
end
end

# Checks if the module is equal to other.
def eql?(other)
other.instance_of?(self.class) && self == other
end

# Returns a TypeCollection of all the Types in the module.
def types
@types ||= TypeCollection.new(self)
Expand Down
48 changes: 13 additions & 35 deletions lib/llvm/core/type.rb
@@ -1,27 +1,20 @@
module LLVM
class Type
# @private
def to_ptr
@ptr
end
include PointerIdentity

# LLVM's represents types uniquely, and supports pointer equality.
def ==(type)
case type
when LLVM::Type
@ptr == type.to_ptr
else
false
# @private
def self.from_ptr(ptr, kind)
return if ptr.null?
kind ||= C.get_type_kind(ptr)
ty = case kind
when :integer then IntType.allocate
when :function then FunctionType.allocate
when :struct then StructType.allocate
else allocate
end
end

def hash
@ptr.address.hash
end

# Checks if the type is equal to other.
def eql?(other)
other.instance_of?(self.class) && self == other
ty.instance_variable_set(:@ptr, ptr)
ty.instance_variable_set(:@kind, kind)
ty
end

# Returns a symbol representation of the types kind (ex. :pointer, :vector, :array.)
Expand Down Expand Up @@ -66,21 +59,6 @@ def dump
Support::C.dump_type(self)
end

# @private
def self.from_ptr(ptr, kind)
return if ptr.null?
kind ||= C.get_type_kind(ptr)
ty = case kind
when :integer then IntType.allocate
when :function then FunctionType.allocate
when :struct then StructType.allocate
else allocate
end
ty.instance_variable_set(:@ptr, ptr)
ty.instance_variable_set(:@kind, kind)
ty
end

# Creates an array type of Type with the given size.
def self.array(ty, sz = 0)
from_ptr(C.array_type(LLVM::Type(ty), sz), :array)
Expand Down
26 changes: 2 additions & 24 deletions lib/llvm/core/value.rb
@@ -1,5 +1,7 @@
module LLVM
class Value
include PointerIdentity

# @private
def self.from_ptr(ptr)
return if ptr.null?
Expand All @@ -8,30 +10,6 @@ def self.from_ptr(ptr)
val
end

# @private
def to_ptr
@ptr
end

# Checks if the value is equal to other.
def ==(other)
case other
when LLVM::Value
@ptr == other.to_ptr
else
false
end
end

def hash
@ptr.address.hash
end

# Checks if the value is equal to other.
def eql?(other)
other.instance_of?(self.class) && self == other
end

# Returns the Value type. This is abstract and is overidden by its subclasses.
def self.type
raise NotImplementedError, "#{self.name}.type() is abstract."
Expand Down
8 changes: 4 additions & 4 deletions test/equality_test.rb
Expand Up @@ -40,7 +40,7 @@ def test_int_value
:same => [int1, int1],
:not_same => [int1, int2, int3, int4],
:eql => [int1, int2],
:not_eql => [int1, int3, int4]
:not_eql => [int1, int3]
end

def test_module
Expand All @@ -54,7 +54,7 @@ def test_module
:same => [mod1, mod1],
:not_same => [mod1, mod2, mod3, mod4],
:eql => [mod1, mod2],
:not_eql => [mod1, mod3, mod4]
:not_eql => [mod1, mod3]
end

def test_type
Expand All @@ -68,7 +68,7 @@ def test_type
:same => [type1, type1],
:not_same => [type1, type2, type3, type4],
:eql => [type1, type2],
:not_eql => [type1, type3, type4]
:not_eql => [type1, type3]
end

def test_function
Expand All @@ -84,7 +84,7 @@ def test_function
:same => [fn1, fn1],
:not_same => [fn1, fn2, fn3, fn4],
:eql => [fn1, fn2],
:not_eql => [fn1, fn3, fn4]
:not_eql => [fn1, fn3]
end

end
Expand Down