Class: LLVM::Type

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

Direct Known Subclasses

FunctionType, IntType

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.array(ty, sz = 0) ⇒ Object

Creates an array type of Type with the given size.



69
70
71
# File 'lib/llvm/core/type.rb', line 69

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

.from_ptr(ptr) ⇒ Object



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

def self.from_ptr(ptr)
  return if ptr.null?
  ty = allocate
  ty.instance_variable_set(:@ptr, ptr)
  ty
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.



85
86
87
88
89
90
# File 'lib/llvm/core/type.rb', line 85

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)
  FunctionType.from_ptr(C.LLVMFunctionType(LLVM::Type(result_type), arg_types_ptr, arg_types.size, options[:varargs] ? 1 : 0))
end

.opaqueObject

Creates an opaque type.



106
107
108
# File 'lib/llvm/core/type.rb', line 106

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.



74
75
76
# File 'lib/llvm/core/type.rb', line 74

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

.recObject



110
111
112
113
114
115
# File 'lib/llvm/core/type.rb', line 110

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.



93
94
95
96
97
98
# File 'lib/llvm/core/type.rb', line 93

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.



79
80
81
# File 'lib/llvm/core/type.rb', line 79

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

.voidObject

Creates a void type.



101
102
103
# File 'lib/llvm/core/type.rb', line 101

def self.void
  from_ptr(C.LLVMVoidType)
end

Instance Method Details

#==(type) ⇒ Object

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



9
10
11
12
13
14
15
16
# File 'lib/llvm/core/type.rb', line 9

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

#alignObject



33
34
35
# File 'lib/llvm/core/type.rb', line 33

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

#element_typeObject

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



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

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)


19
20
21
# File 'lib/llvm/core/type.rb', line 19

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

#kindObject

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



24
25
26
# File 'lib/llvm/core/type.rb', line 24

def kind
  C.LLVMGetTypeKind(self)
end

#nullObject

Returns a null ConstantExpr of this type.



51
52
53
# File 'lib/llvm/core/type.rb', line 51

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

#null_pointerObject

Returns a null pointer ConstantExpr of this type.



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

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.



56
57
58
# File 'lib/llvm/core/type.rb', line 56

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

#refine(ty) ⇒ Object



117
118
119
# File 'lib/llvm/core/type.rb', line 117

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

#sizeObject

Returns the size of the type.



29
30
31
# File 'lib/llvm/core/type.rb', line 29

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

#to_ptrObject



4
5
6
# File 'lib/llvm/core/type.rb', line 4

def to_ptr
  @ptr
end