Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Factor out PointerIdentity #11

Merged
merged 1 commit into from

2 participants

whitequark Jeremy Voorhis
whitequark
Owner

This pull request has some backwards-incompatible change in semantics, but I doubt it will be a problem.

Particularly, if someone has subclassed, for example, LLVM::Function, and would be comparing an instance of that class with LLVM::Function with the same @ptr:

  • #== returns true,
  • #hash returns the same value, but
  • #eql? returns false

My change would cause #eql? to return true in this case.

I would say that this doesn't quite make sense, and is very unlikely to be an actual problem. I'm not sure that an ability to subclass LLVM::Stuff is valuable at all.

whitequark whitequark referenced this pull request
Merged

Add Target APIs #12

Jeremy Voorhis
Owner

This looks good. I'm stretched thin at the moment, so I suggest you go ahead and merge it.

Jeremy Voorhis jvoorhis merged commit 514b73f into from
whitequark whitequark deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 15, 2013
  1. whitequark

    Factor out PointerIdentity.

    whitequark authored
This page is out of date. Refresh to see the latest.
23 lib/llvm.rb
View
@@ -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 lib/llvm/core/module.rb
View
@@ -1,5 +1,7 @@
module LLVM
class Module
+ include PointerIdentity
+
# @private
def self.from_ptr(ptr)
return if ptr.null?
@@ -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)
48 lib/llvm/core/type.rb
View
@@ -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.)
@@ -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)
26 lib/llvm/core/value.rb
View
@@ -1,5 +1,7 @@
module LLVM
class Value
+ include PointerIdentity
+
# @private
def self.from_ptr(ptr)
return if ptr.null?
@@ -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."
8 test/equality_test.rb
View
@@ -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
@@ -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
@@ -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
@@ -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
Something went wrong with that request. Please try again.