Class: LLVM::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/llvm/core/type.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ptr) ⇒ Type

Returns a new instance of Type.



6
7
8
# File 'lib/llvm/core/type.rb', line 6

def initialize(ptr)
  @ptr = ptr
end

Class Method Details

.array(ty, sz = 0) ⇒ Object

Creates an array type of Type with the given size.



72
73
74
# File 'lib/llvm/core/type.rb', line 72

def self.array(ty, sz = 0)
  from_ptr(C.LLVMArrayType(LLVM::Type(ty), sz))
end

.from_ptr(ptr) ⇒ Object



67
68
69
# File 'lib/llvm/core/type.rb', line 67

def self.from_ptr(ptr)
  ptr.null? ? nil : new(ptr)
end

.function(arg_types, result_type, options = {}) ⇒ Object

Creates a function type. Takes an array of argument Types and the result Type. The only option is :varargs, which when set to true makes the function type take a variable number of args.



88
89
90
91
92
93
# File 'lib/llvm/core/type.rb', line 88

def self.function(arg_types, result_type, options = {})
  arg_types.map! { |ty| LLVM::Type(ty) }
  arg_types_ptr = FFI::MemoryPointer.new(FFI.type_size(:pointer) * arg_types.size)
  arg_types_ptr.write_array_of_pointer(arg_types)
  from_ptr(C.LLVMFunctionType(LLVM::Type(result_type), arg_types_ptr, arg_types.size, options[:varargs] ? 1 : 0))
end

.opaqueObject

Creates an opaque type.



109
110
111
# File 'lib/llvm/core/type.rb', line 109

def self.opaque
  from_ptr(C.LLVMOpaqueType)
end

.pointer(ty, address_space = 0) ⇒ Object

Creates the pointer type of Type with the given address space.



77
78
79
# File 'lib/llvm/core/type.rb', line 77

def self.pointer(ty, address_space = 0)
  from_ptr(C.LLVMPointerType(LLVM::Type(ty), address_space))
end

.recObject



113
114
115
116
117
118
# File 'lib/llvm/core/type.rb', line 113

def self.rec
  h = opaque
  ty = yield h
  h.refine(ty)
  ty
end

.struct(elt_types, is_packed) ⇒ Object

Creates a struct type with the given array of element types.



96
97
98
99
100
101
# File 'lib/llvm/core/type.rb', line 96

def self.struct(elt_types, is_packed)
  elt_types.map! { |ty| LLVM::Type(ty) }
  elt_types_ptr = FFI::MemoryPointer.new(FFI.type_size(:pointer) * elt_types.size)
  elt_types_ptr.write_array_of_pointer(elt_types)
  from_ptr(C.LLVMStructType(elt_types_ptr, elt_types.size, is_packed ? 1 : 0))
end

.vector(ty, element_count) ⇒ Object

Creates a vector type of Type with the given element count.



82
83
84
# File 'lib/llvm/core/type.rb', line 82

def self.vector(ty, element_count)
  from_ptr(C.LLVMVectorType(LLVM::Type(ty), element_count))
end

.voidObject

Creates a void type.



104
105
106
# File 'lib/llvm/core/type.rb', line 104

def self.void
  from_ptr(C.LLVMVoidType)
end

Instance Method Details

#==(type) ⇒ Object

LLVM’s represents types uniquely, and supports pointer equality.



16
17
18
19
20
21
22
23
# File 'lib/llvm/core/type.rb', line 16

def ==(type)
  case type
  when LLVM::Type
    @ptr == type.to_ptr
  else
    false
  end
end

#alignObject



40
41
42
# File 'lib/llvm/core/type.rb', line 40

def align
  Int64.from_ptr(C.LLVMAlignOf(self))
end

#element_typeObject

Returns the type of this types elements (works only for Pointer, Vector, and Array types.)



45
46
47
48
49
50
# File 'lib/llvm/core/type.rb', line 45

def element_type
  case self.kind
  when :pointer, :vector, :array
    Type.from_ptr(C.LLVMGetElementType(self))
  end
end

#eql?(other) ⇒ Boolean

Checks if the type is equal to other.

Returns:

  • (Boolean)


26
27
28
# File 'lib/llvm/core/type.rb', line 26

def eql?(other)
  other.instance_of?(self.class) && self == other
end

#kindObject

Returns a symbol representation of the types kind (ex. :pointer, :vector, :array.)



31
32
33
# File 'lib/llvm/core/type.rb', line 31

def kind
  C.LLVMGetTypeKind(self)
end

#nullObject

Returns a null ConstantExpr of this type.



58
59
60
# File 'lib/llvm/core/type.rb', line 58

def null
  ConstantExpr.from_ptr(C.LLVMConstNull(self))
end

#null_pointerObject

Returns a null pointer ConstantExpr of this type.



53
54
55
# File 'lib/llvm/core/type.rb', line 53

def null_pointer
  ConstantExpr.from_ptr(C.LLVMConstPointerNull(self))
end

#pointer(address_space = 0) ⇒ Object

Creates a pointer type with this type and the given address space.



63
64
65
# File 'lib/llvm/core/type.rb', line 63

def pointer(address_space = 0)
  Type.pointer(self, address_space)
end

#refine(ty) ⇒ Object



120
121
122
# File 'lib/llvm/core/type.rb', line 120

def refine(ty)
  C.LLVMRefineType(self, ty)
end

#sizeObject

Returns the size of the type.



36
37
38
# File 'lib/llvm/core/type.rb', line 36

def size
  Int64.from_ptr(C.LLVMSizeOf(self))
end

#to_ptrObject



11
12
13
# File 'lib/llvm/core/type.rb', line 11

def to_ptr
  @ptr
end