Class: LLVM::Type

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

Direct Known Subclasses

FunctionType, IntType, RealType, StructType

Instance Attribute Summary collapse

Attributes included from PointerIdentity

#ptr

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PointerIdentity

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

Instance Attribute Details

#kindObject (readonly)

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
  @kind
end

Class Method Details

.array(ty, sz = 0) ⇒ Object

Creates an array type of Type with the given size. arrays can be size >= 0, llvm.org/docs/LangRef.html#array-type : (Type, ?Integer) -> Type

Raises:

  • (ArgumentError)


135
136
137
138
139
140
# File 'lib/llvm/core/type.rb', line 135

def self.array(ty, sz = 0)
  sz = sz.to_i
  raise ArgumentError, "LLVM Array size must be >= 0" if sz.negative?

  from_ptr(C.array_type(LLVM::Type(ty), sz), kind: :array)
end

.doubleObject

: -> RealType



250
251
252
# File 'lib/llvm/core/type.rb', line 250

def self.double
  RealType.from_ptr(C.double_type, kind: :double) #: as RealType
end

.floatObject

: -> RealType



245
246
247
# File 'lib/llvm/core/type.rb', line 245

def self.float
  RealType.from_ptr(C.float_type, kind: :float) #: as RealType
end

.from_ptr(ptr, kind: nil) ⇒ Object

: (untyped, ?kind: Symbol?) -> Type

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/llvm/core/type.rb', line 10

def self.from_ptr(ptr, kind: nil)
  raise ArgumentError if ptr.null?
  kind ||= C.get_type_kind(ptr)
  ty = case kind
  when :integer
    IntType.allocate
  when :float, :double
    RealType.allocate
  when :function
    FunctionType.allocate
  when :struct
    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. : ([Type] | [], Type, ?Hash[untyped, untyped]) -> FunctionType



171
172
173
174
175
176
177
# File 'lib/llvm/core/type.rb', line 171

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)
  function_type_ptr = C.function_type(LLVM::Type(result_type), arg_types_ptr, arg_types.size, options[:varargs] ? 1 : 0)
  from_ptr(function_type_ptr, kind: :function) #: as FunctionType
end

.integer(width) ⇒ Object Also known as: i

: (Integer) -> Type



236
237
238
# File 'lib/llvm/core/type.rb', line 236

def self.integer(width)
  IntType.from_ptr(C.int_type(width), kind: :integer)
end

.labelObject

: -> Type



211
212
213
# File 'lib/llvm/core/type.rb', line 211

def self.label
  from_ptr(C.label_type, kind: :label)
end

.named(name) ⇒ Object

: (untyped) -> Type?



200
201
202
# File 'lib/llvm/core/type.rb', line 200

def self.named(name)
  from_ptr(C.get_type_by_name2(Context.global, name.to_s))
end

.opaque_struct(name) ⇒ Object

: (untyped) -> Type



195
196
197
# File 'lib/llvm/core/type.rb', line 195

def self.opaque_struct(name)
  from_ptr(C.struct_create_named(Context.global, name.to_s), kind: :struct)
end

.pointer(ty = nil, address_space: 0) ⇒ Object

Creates the pointer type of Type with the given address space. : (?Type?, ?address_space: Integer) -> Type



144
145
146
147
148
149
150
# File 'lib/llvm/core/type.rb', line 144

def self.pointer(ty = nil, address_space: 0)
  if ty
    from_ptr(C.pointer_type(LLVM::Type(ty), address_space), kind: :pointer)
  else
    ptr(address_space: address_space)
  end
end

.ptr(address_space: 0) ⇒ Object

opaque pointer : (?address_space: Integer) -> Type



154
155
156
# File 'lib/llvm/core/type.rb', line 154

def self.ptr(address_space: 0)
  from_ptr(C.pointer_type(void, address_space), kind: :pointer)
end

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

Creates a struct type with the given array of element types. : ([Type] | [], bool, ?String?) -> Type



181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/llvm/core/type.rb', line 181

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), kind: :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), kind: :struct)
  end
end

.vector(ty, element_count) ⇒ Object

Creates a vector type of Type with the given element count. vectors can be size > 0, llvm.org/docs/LangRef.html#vector-type : (Type, Integer) -> Type

Raises:

  • (ArgumentError)


161
162
163
164
165
166
# File 'lib/llvm/core/type.rb', line 161

def self.vector(ty, element_count)
  element_count = element_count.to_i
  raise ArgumentError, "LLVM Vector size must be > 0" unless element_count.positive?

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

.voidObject

Creates a void type. : -> Type



206
207
208
# File 'lib/llvm/core/type.rb', line 206

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

.x86_amxObject

: -> Type



221
222
223
# File 'lib/llvm/core/type.rb', line 221

def self.x86_amx
  from_ptr(C.x86amx_type, kind: :x86amx)
end

.x86_mmxObject

: -> Type

Raises:



216
217
218
# File 'lib/llvm/core/type.rb', line 216

def self.x86_mmx
  raise DeprecationError
end

Instance Method Details

#===(other) ⇒ Object

: (untyped) -> bool



124
125
126
127
128
129
130
# File 'lib/llvm/core/type.rb', line 124

def ===(other)
  if other.is_a?(LLVM::Value)
    return self == other.type
  end

  super
end

#aggregate?Boolean

: -> bool

Returns:

  • (Boolean)


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

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

#alignObject

: -> ConstantInt



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

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

#dumpObject

Print the type’s representation to stdout. : -> void



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

def dump
  # :nocov:
  C.dump_type(self)
  # :nocov:
end

#element_typeObject

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



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/llvm/core/type.rb', line 46

def element_type
  case kind
  when :vector, :array
    element_type = C.get_element_type(self)
    Type.from_ptr(element_type)
  when :pointer
    LLVM.Void
  else
    raise ArgumentError, "element_type not supported for kind: #{kind}"
  end
end

#literal_struct?Boolean

: -> bool

Returns:

  • (Boolean)


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

def literal_struct?
  C.is_literal_struct(self)
end

#nullObject

Returns a null ConstantExpr of this type. : -> Constant



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

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

#null_pointerObject

Returns a null pointer ConstantExpr of this type. : -> ConstantExpr



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

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

#opaque_struct?Boolean

: -> bool

Returns:

  • (Boolean)


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

def opaque_struct?
  C.is_opaque_struct(self)
end

#packed_struct?Boolean

: -> bool

Returns:

  • (Boolean)


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

def packed_struct?
  C.is_packed_struct(self)
end

#pointer(address_space: 0) ⇒ Object

Creates a pointer type with this type and the given address space. : (?address_space: Integer) -> Type



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

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

#poisonObject

: -> Constant



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

def poison
  # ConstantExpr.from_ptr(C.get_poison(self))
  Constant.poison(self)
end

#sizeObject

Returns the size of the type. : -> ConstantInt



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

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

#to_sObject

Build string of LLVM type representation. : -> String



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

def to_s
  C.print_type_to_string(self)
end

#undefObject

: -> Constant



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

def undef
  # ConstantExpr.from_ptr(C.get_undef(self))
  Constant.undef(self)
end