Class: LLVM::Type

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

Direct Known Subclasses

FunctionType, IntType, StructType

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PointerIdentity

#==, #eql?, #hash, #to_ptr

Class Method Details

.array(ty, sz = 0) ⇒ Object

Creates an array type of Type with the given size.



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

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

.from_ptr(ptr, kind) ⇒ Object



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

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

.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.



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

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

.pointer(ty, address_space = 0) ⇒ Object

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



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

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

.recObject



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

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

.struct(elt_types, is_packed, name = nil) ⇒ Object

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



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/llvm/core/type.rb', line 98

def self.struct(elt_types, is_packed, name = nil)
  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)
  if name
    struct = from_ptr(C.struct_create_named(Context.global, name), :struct)
    C.struct_set_body(struct, elt_types_ptr, elt_types.size, is_packed ? 1 : 0) unless elt_types.empty?
    struct
  else
    from_ptr(C.struct_type(elt_types_ptr, elt_types.size, is_packed ? 1 : 0), :struct)
  end
end

.vector(ty, element_count) ⇒ Object

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



84
85
86
# File 'lib/llvm/core/type.rb', line 84

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

.voidObject

Creates a void type.



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

def self.void
  from_ptr(C.void_type, :void)
end

Instance Method Details

#aggregate?Boolean

Returns:

  • (Boolean)


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

def aggregate?
  [:struct, :array].include?(kind)
end

#alignObject



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

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

#dumpObject

Print the type’s representation to stdout.



60
61
62
# File 'lib/llvm/core/type.rb', line 60

def dump
  C.dump_type(self)
end

#element_typeObject

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



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

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

#kindObject

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



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

def kind
  @kind
end

#nullObject

Returns a null ConstantExpr of this type.



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

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

#null_pointerObject

Returns a null pointer ConstantExpr of this type.



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

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

#pointer(address_space = 0) ⇒ Object

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



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

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

#sizeObject

Returns the size of the type.



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

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

#to_sObject

Build string of LLVM type representation.



65
66
67
# File 'lib/llvm/core/type.rb', line 65

def to_s
  C.print_type_to_string(self)
end