ruby-llvm 20.1.7 → 22.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,43 +1,54 @@
1
1
  # frozen_string_literal: true
2
+ # typed: strict
2
3
 
3
4
  module LLVM
4
5
  class Type
5
6
  include PointerIdentity
6
7
 
7
8
  # @private
8
- def self.from_ptr(ptr, kind = nil)
9
- return if ptr.null?
9
+ #: (FFI::Pointer, ?kind: Symbol?) -> Type
10
+ def self.from_ptr(ptr, kind: nil)
11
+ raise ArgumentError if ptr.null?
10
12
  kind ||= C.get_type_kind(ptr)
11
- ty = case kind
13
+ case kind
12
14
  when :integer
13
- IntType.allocate
15
+ IntType.new(ptr, kind)
14
16
  when :float, :double
15
- RealType.allocate
17
+ RealType.new(ptr, kind)
16
18
  when :function
17
- FunctionType.allocate
19
+ FunctionType.new(ptr, kind)
18
20
  when :struct
19
- StructType.allocate
21
+ StructType.new(ptr, kind)
20
22
  else
21
- allocate
23
+ new(ptr, kind)
22
24
  end
23
- ty.instance_variable_set(:@ptr, ptr)
24
- ty.instance_variable_set(:@kind, kind)
25
- ty
25
+ end
26
+
27
+ # @private
28
+ #: (FFI::Pointer, Symbol?) -> void
29
+ def initialize(ptr, kind)
30
+ raise ArgumentError if ptr.null?
31
+ @ptr = ptr
32
+ @kind = kind
26
33
  end
27
34
 
28
35
  # Returns a symbol representation of the types kind (ex. :pointer, :vector, :array.)
36
+ #: Symbol?
29
37
  attr_reader :kind
30
38
 
31
39
  # Returns the size of the type.
40
+ #: -> ConstantInt
32
41
  def size
33
42
  LLVM::Int64.from_ptr(C.size_of(self))
34
43
  end
35
44
 
45
+ #: -> ConstantInt
36
46
  def align
37
47
  LLVM::Int64.from_ptr(C.align_of(self))
38
48
  end
39
49
 
40
50
  # Returns the type of this types elements (works only for Pointer, Vector, and Array types.)
51
+ #: -> Type
41
52
  def element_type
42
53
  case kind
43
54
  when :vector, :array
@@ -51,59 +62,82 @@ module LLVM
51
62
  end
52
63
 
53
64
  # Returns a null pointer ConstantExpr of this type.
65
+ #: -> ConstantExpr
54
66
  def null_pointer
55
67
  ConstantExpr.from_ptr(C.const_pointer_null(self))
56
68
  end
57
69
 
58
70
  # Returns a null ConstantExpr of this type.
71
+ #: -> Constant
59
72
  def null
60
73
  # ConstantExpr.from_ptr(C.const_null(self))
61
74
  Constant.null(self)
62
75
  end
63
76
 
77
+ #: -> Constant
64
78
  def poison
65
79
  # ConstantExpr.from_ptr(C.get_poison(self))
66
80
  Constant.poison(self)
67
81
  end
68
82
 
83
+ #: -> Constant
69
84
  def undef
70
85
  # ConstantExpr.from_ptr(C.get_undef(self))
71
86
  Constant.undef(self)
72
87
  end
73
88
 
74
89
  # Creates a pointer type with this type and the given address space.
75
- def pointer(address_space = 0)
76
- Type.pointer(self, address_space)
90
+ #: (?address_space: Integer) -> Type
91
+ def pointer(address_space: 0)
92
+ Type.pointer(self, address_space: address_space)
77
93
  end
78
94
 
79
- # Print the type's representation to stdout.
95
+ # Print the type's representation to the standard error.
96
+ #
97
+ # Renders as LLVMDumpType does, but written through Ruby's $stderr rather
98
+ # than LLVM's errs(), so reassigning $stderr captures it, and always ends
99
+ # with a newline where LLVMDumpType emits none.
100
+ #: -> void
80
101
  def dump
81
- # :nocov:
82
- C.dump_type(self)
83
- # :nocov:
102
+ # not warn: warn is silenced by -W0, and an explicit dump must always print
103
+ $stderr.puts(debug_s) # rubocop:disable Style/StderrPuts
84
104
  end
85
105
 
86
106
  # Build string of LLVM type representation.
107
+ #: -> String
87
108
  def to_s
88
109
  C.print_type_to_string(self)
89
110
  end
90
111
 
112
+ # Returns the type as #dump renders it (LLVM's IsForDebug). As of LLVM 21
113
+ # this is identical to #to_s for every type kind, unlike Module and Value
114
+ # where it changes how declarations render.
115
+ #: -> String
116
+ def debug_s
117
+ Support::C.print_type_to_string_debug(self)
118
+ end
119
+
120
+ #: -> bool
91
121
  def aggregate?
92
122
  [:struct, :array].include?(kind)
93
123
  end
94
124
 
125
+ #: -> bool
95
126
  def opaque_struct?
96
127
  C.is_opaque_struct(self)
97
128
  end
98
129
 
130
+ #: -> bool
99
131
  def packed_struct?
100
132
  C.is_packed_struct(self)
101
133
  end
102
134
 
135
+ #: -> bool
103
136
  def literal_struct?
104
137
  C.is_literal_struct(self)
105
138
  end
106
139
 
140
+ #: (untyped) -> bool
107
141
  def ===(other)
108
142
  if other.is_a?(LLVM::Value)
109
143
  return self == other.type
@@ -114,82 +148,95 @@ module LLVM
114
148
 
115
149
  # Creates an array type of Type with the given size.
116
150
  # arrays can be size >= 0, https://llvm.org/docs/LangRef.html#array-type
151
+ #: (Type, ?Integer) -> Type
117
152
  def self.array(ty, sz = 0)
118
153
  sz = sz.to_i
119
154
  raise ArgumentError, "LLVM Array size must be >= 0" if sz.negative?
120
155
 
121
- from_ptr(C.array_type(LLVM::Type(ty), sz), :array)
156
+ from_ptr(C.array_type(LLVM::Type(ty), sz), kind: :array)
122
157
  end
123
158
 
124
159
  # Creates the pointer type of Type with the given address space.
125
- def self.pointer(ty = nil, address_space = 0)
160
+ #: (?Type?, ?address_space: Integer) -> Type
161
+ def self.pointer(ty = nil, address_space: 0)
126
162
  if ty
127
- from_ptr(C.pointer_type(LLVM::Type(ty), address_space), :pointer)
163
+ from_ptr(C.pointer_type(LLVM::Type(ty), address_space), kind: :pointer)
128
164
  else
129
- ptr(address_space)
165
+ ptr(address_space: address_space)
130
166
  end
131
167
  end
132
168
 
133
169
  # opaque pointer
134
- def self.ptr(address_space = 0)
135
- from_ptr(C.pointer_type(void, address_space), :pointer)
170
+ #: (?address_space: Integer) -> Type
171
+ def self.ptr(address_space: 0)
172
+ from_ptr(C.pointer_type(void, address_space), kind: :pointer)
136
173
  end
137
174
 
138
175
  # Creates a vector type of Type with the given element count.
139
176
  # vectors can be size > 0, https://llvm.org/docs/LangRef.html#vector-type
177
+ #: (Type, Integer) -> Type
140
178
  def self.vector(ty, element_count)
141
179
  element_count = element_count.to_i
142
180
  raise ArgumentError, "LLVM Vector size must be > 0" unless element_count.positive?
143
181
 
144
- from_ptr(C.vector_type(LLVM::Type(ty), element_count), :vector)
182
+ from_ptr(C.vector_type(LLVM::Type(ty), element_count), kind: :vector)
145
183
  end
146
184
 
147
185
  # Creates a function type. Takes an array of argument Types and the result Type. The only option is <tt>:varargs</tt>,
148
186
  # which when set to true makes the function type take a variable number of args.
187
+ #: ([Type] | [], Type, ?Hash[untyped, untyped]) -> FunctionType
149
188
  def self.function(arg_types, result_type, options = {})
150
189
  arg_types.map! { |ty| LLVM::Type(ty) }
151
190
  arg_types_ptr = FFI::MemoryPointer.new(FFI.type_size(:pointer) * arg_types.size)
152
191
  arg_types_ptr.write_array_of_pointer(arg_types)
153
- from_ptr(C.function_type(LLVM::Type(result_type), arg_types_ptr, arg_types.size, options[:varargs] ? 1 : 0), :function)
192
+ function_type_ptr = C.function_type(LLVM::Type(result_type), arg_types_ptr, arg_types.size, options[:varargs] ? 1 : 0)
193
+ from_ptr(function_type_ptr, kind: :function) #: as FunctionType
154
194
  end
155
195
 
156
196
  # Creates a struct type with the given array of element types.
197
+ #: ([Type] | [], bool, ?String?) -> Type
157
198
  def self.struct(elt_types, is_packed, name = nil)
158
199
  elt_types.map! { |ty| LLVM::Type(ty) }
159
200
  elt_types_ptr = FFI::MemoryPointer.new(FFI.type_size(:pointer) * elt_types.size)
160
201
  elt_types_ptr.write_array_of_pointer(elt_types)
161
202
  if name
162
- struct = from_ptr(C.struct_create_named(Context.global, name), :struct)
203
+ struct = from_ptr(C.struct_create_named(Context.global, name), kind: :struct)
163
204
  C.struct_set_body(struct, elt_types_ptr, elt_types.size, is_packed ? 1 : 0) unless elt_types.empty?
164
205
  struct
165
206
  else
166
- from_ptr(C.struct_type(elt_types_ptr, elt_types.size, is_packed ? 1 : 0), :struct)
207
+ from_ptr(C.struct_type(elt_types_ptr, elt_types.size, is_packed ? 1 : 0), kind: :struct)
167
208
  end
168
209
  end
169
210
 
211
+ #: (String | Symbol) -> Type
170
212
  def self.opaque_struct(name)
171
- from_ptr(C.struct_create_named(Context.global, name.to_s), :struct)
213
+ from_ptr(C.struct_create_named(Context.global, name.to_s), kind: :struct)
172
214
  end
173
215
 
216
+ #: (String | Symbol) -> Type?
174
217
  def self.named(name)
175
- from_ptr(C.get_type_by_name2(Context.global, name.to_s), nil)
218
+ from_ptr(C.get_type_by_name2(Context.global, name.to_s))
176
219
  end
177
220
 
178
221
  # Creates a void type.
222
+ #: -> Type
179
223
  def self.void
180
- from_ptr(C.void_type, :void)
224
+ from_ptr(C.void_type, kind: :void)
181
225
  end
182
226
 
227
+ #: -> Type
183
228
  def self.label
184
- from_ptr(C.label_type, :label)
229
+ from_ptr(C.label_type, kind: :label)
185
230
  end
186
231
 
232
+ #: -> Type
187
233
  def self.x86_mmx
188
234
  raise DeprecationError
189
235
  end
190
236
 
237
+ #: -> Type
191
238
  def self.x86_amx
192
- from_ptr(C.x86amx_type, :x86amx)
239
+ from_ptr(C.x86amx_type, kind: :x86amx)
193
240
  end
194
241
  # def self.opaque_pointer
195
242
  # from_ptr(C.opaque_type, :pointer)
@@ -202,32 +249,38 @@ module LLVM
202
249
  # ty
203
250
  # end
204
251
 
252
+ #: (Integer) -> Type
205
253
  def self.integer(width)
206
- IntType.from_ptr(C.int_type(width), :integer)
254
+ IntType.from_ptr(C.int_type(width), kind: :integer)
207
255
  end
208
256
 
209
257
  class << self
210
258
  alias_method :i, :integer
211
259
  end
212
260
 
261
+ #: -> RealType
213
262
  def self.float
214
- RealType.from_ptr(C.float_type, kind: :float)
263
+ RealType.from_ptr(C.float_type, kind: :float) #: as RealType
215
264
  end
216
265
 
266
+ #: -> RealType
217
267
  def self.double
218
- RealType.from_ptr(C.double_type, kind: :double)
268
+ RealType.from_ptr(C.double_type, kind: :double) #: as RealType
219
269
  end
220
270
  end
221
271
 
222
272
  class IntType < Type
273
+ #: -> ConstantInt
223
274
  def all_ones
224
275
  from_ptr(C.const_all_ones(self))
225
276
  end
226
277
 
278
+ #: -> Integer
227
279
  def width
228
280
  C.get_int_type_width(self)
229
281
  end
230
282
 
283
+ #: (Hash[Symbol, untyped] | bool) -> bool
231
284
  private def get_sign_option(options)
232
285
  case options
233
286
  when true, false
@@ -239,6 +292,7 @@ module LLVM
239
292
  end
240
293
  end
241
294
 
295
+ #: ((Numeric | String), ?Hash[untyped, untyped] | bool) -> Value
242
296
  def from_i(int, options = {})
243
297
  signed = get_sign_option(options)
244
298
 
@@ -249,23 +303,14 @@ module LLVM
249
303
  unpoisoned ? val : poison
250
304
  end
251
305
 
252
- # unused
253
- private def fits_width?(int, width, signed)
254
- # :nocov:
255
- if signed
256
- int.bit_length < width || int == 1
257
- else
258
- int >= 0 && int.bit_length <= width
259
- end
260
- # :nocov:
261
- end
262
-
306
+ #: (FFI::Pointer) -> ConstantInt
263
307
  def from_ptr(ptr)
264
308
  ConstantInt.from_ptr(ptr)
265
309
  end
266
310
 
267
311
  # parse string using const_int_of_string_and_size
268
312
  # normalize the string to base 10 (used in llvm ir), and handle prefixes like 0x
313
+ #: (String, Integer) -> Value
269
314
  private def const_int_of_string_and_size(str, radix)
270
315
  normalized_string = radix == 10 ? str : str.to_i(radix).to_s
271
316
  val = from_ptr(C.const_int_of_string_and_size(self, normalized_string, normalized_string.size, 10))
@@ -277,54 +322,64 @@ module LLVM
277
322
  # parse string using const_int_of_string_and_size
278
323
  # alternative implementation parsing with ruby:
279
324
  # from_i(str.to_i(radix))
325
+ #: (untyped, ?Integer) -> Value
280
326
  def parse(str, radix = 10)
281
- const_int_of_string_and_size(str, radix)
327
+ const_int_of_string_and_size(str.to_s, radix)
282
328
  end
283
329
 
330
+ #: -> self
284
331
  def type
285
332
  self
286
333
  end
287
334
  end
288
335
 
289
336
  class RealType < Type
290
- # given an array of values, return the type that will fit all of them
337
+ # given an array of values or types, return the type that will fit all of them
291
338
  # currently only works for floats and doubles
339
+ #: ([Value | Type]) -> RealType
292
340
  def self.fits(values)
293
341
  values.detect { |v| v.type.to_s == 'double' } ? double : float
294
342
  end
295
343
 
344
+ #: (Integer | ::Float | Rational | BigDecimal) -> ConstantReal
296
345
  def from_f(value)
297
- ConstantReal.from_ptr(C.const_real(self, value))
346
+ ConstantReal.from_ptr(C.const_real(self, value.to_f))
298
347
  end
299
348
 
349
+ #: (untyped) -> ConstantReal
300
350
  def parse(str)
301
- ConstantReal.from_ptr(C.const_real_of_string(self, str))
351
+ ConstantReal.from_ptr(C.const_real_of_string(self, str.to_s))
302
352
  end
303
353
 
354
+ #: -> self
304
355
  def type
305
356
  self
306
357
  end
307
358
  end
308
359
 
309
360
  class FunctionType < Type
361
+ #: -> Type
310
362
  def return_type
311
363
  Type.from_ptr(C.get_return_type(self))
312
364
  end
313
365
 
366
+ #: -> self
314
367
  def element_type
315
368
  self
316
369
  end
317
370
 
371
+ #: -> Array[Type?]
318
372
  def argument_types
319
373
  size = C.count_param_types(self)
320
- result = nil
374
+ result = [] #: Array[FFI::Pointer]
321
375
  FFI::MemoryPointer.new(FFI.type_size(:pointer) * size) do |types_ptr|
322
376
  C.get_param_types(self, types_ptr)
323
377
  result = types_ptr.read_array_of_pointer(size)
324
378
  end
325
- result.map { |p| Type.from_ptr(p, nil) }
379
+ result.map { |p| Type.from_ptr(p) }
326
380
  end
327
381
 
382
+ #: -> bool
328
383
  def vararg?
329
384
  C.is_function_var_arg(self) != 0
330
385
  end
@@ -332,22 +387,25 @@ module LLVM
332
387
 
333
388
  class StructType < Type
334
389
  # Returns the name of the struct.
390
+ #: -> String
335
391
  def name
336
392
  C.get_struct_name(self)
337
393
  end
338
394
 
339
395
  # Returns the element types of the struct.
396
+ #: -> Array[Type]
340
397
  def element_types
341
398
  count = C.count_struct_element_types(self)
342
- elt_types = nil
399
+ elt_types = [] #: Array[Type]
343
400
  FFI::MemoryPointer.new(FFI.type_size(:pointer) * count) do |types_ptr|
344
401
  C.get_struct_element_types(self, types_ptr)
345
- elt_types = types_ptr.read_array_of_pointer(count).map { |type_ptr| Type.from_ptr(type_ptr, nil) }
402
+ elt_types = types_ptr.read_array_of_pointer(count).map { |type_ptr| Type.from_ptr(type_ptr) }
346
403
  end
347
404
  elt_types
348
405
  end
349
406
 
350
407
  # Sets the struct body.
408
+ #: ([untyped]) -> void
351
409
  def element_types=(elt_types)
352
410
  elt_types.map! { |ty| LLVM::Type(ty) }
353
411
  elt_types_ptr = FFI::MemoryPointer.new(FFI.type_size(:pointer) * elt_types.size)
@@ -359,6 +417,7 @@ module LLVM
359
417
  module_function
360
418
 
361
419
  # Creates a Type from the given object.
420
+ #: (LLVM::Type | LLVM::Value) -> LLVM::Type
362
421
  def Type(ty)
363
422
  case ty
364
423
  when LLVM::Type
@@ -369,76 +428,93 @@ module LLVM
369
428
  end
370
429
 
371
430
  # Shortcut to Type.array.
431
+ #: (LLVM::Type, ?Integer) -> LLVM::Type
372
432
  def Array(ty, sz = 0)
373
433
  LLVM::Type.array(ty, sz)
374
434
  end
375
435
 
376
436
  # Shortcut to Type.pointer.
437
+ #: (?Type?) -> Type
377
438
  def Pointer(ty = nil)
378
439
  LLVM::Type.pointer(ty)
379
440
  end
380
441
 
381
442
  # Shortcut to Type.vector.
443
+ #: (LLVM::Type, Integer) -> LLVM::Type
382
444
  def Vector(ty, sz)
383
445
  LLVM::Type.vector(ty, sz)
384
446
  end
385
447
 
386
448
  # Shortcut to Type.function.
449
+ #: ([Type] | [], Type, ?Hash[untyped, untyped]) -> FunctionType
387
450
  def Function(argtypes, rettype, options = {})
388
451
  LLVM::Type.function(argtypes, rettype, options)
389
452
  end
390
453
 
391
454
  # Shortcut to Type.struct.
455
+ #: (*untyped) -> LLVM::Type
392
456
  def Struct(*elt_types)
393
457
  name = if elt_types.last.is_a? String
394
458
  elt_types.pop
395
- else
396
- nil
397
459
  end
398
- LLVM::Type.struct(elt_types, false, name)
460
+ LLVM::Type.struct(
461
+ elt_types, #: as [LLVM::Type]
462
+ false,
463
+ name
464
+ )
399
465
  end
400
466
 
401
467
  # Shortcut to Type.void.
468
+ #: -> Type
402
469
  def Void
403
470
  LLVM::Type.void
404
471
  end
405
472
 
473
+ #: -> Type
406
474
  def void
407
475
  LLVM::Type.void
408
476
  end
409
477
 
478
+ #: (Integer, ?(Numeric | String)?) -> (ConstantInt | IntType)
410
479
  def i(width, value = nil)
411
480
  type = LLVM::Type.i(width)
412
481
  value ? type.from_i(value) : type
413
482
  end
414
483
 
484
+ #: (?Numeric?) -> (ConstantReal | RealType)
415
485
  def double(value = nil)
416
486
  type = LLVM::Type.double
417
- value ? type.from_f(value) : type
487
+ value ? type.from_f(value.to_f) : type
418
488
  end
419
489
 
490
+ #: (?Numeric?) -> (ConstantReal | RealType)
420
491
  def float(value = nil)
421
492
  type = LLVM::Type.float
422
- value ? type.from_f(value) : type
493
+ value ? type.from_f(value.to_f) : type
423
494
  end
424
495
 
496
+ #: -> Type
425
497
  def ptr
426
498
  LLVM::Type.pointer
427
499
  end
428
500
 
429
501
  # for compatibility
430
502
  # Create a float LLVM::ContantReal from a Ruby Float (value).
503
+ # This will always return ConstantReal because value will always be Numeric
504
+ #: (Numeric) -> ConstantReal
431
505
  def Float(value)
432
- float(value)
506
+ float(value) #: as ConstantReal
433
507
  end
434
508
 
435
509
  # for compatibility
436
510
  # Create a double LLVM::ContantReal from a Ruby Float (value).
511
+ # This will always return ConstantReal because value will always be Numeric
512
+ #: (Numeric) -> ConstantReal
437
513
  def Double(value)
438
- double(value)
514
+ double(value) #: as ConstantReal
439
515
  end
440
516
 
441
517
  # for compatibility
442
- Float = float.freeze
443
- Double = double.freeze
518
+ Float = LLVM::Type.float.freeze #: LLVM::RealType
519
+ Double = LLVM::Type.double.freeze #: LLVM::RealType
444
520
  end