ruby-llvm 20.1.2 → 21.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,14 +1,21 @@
1
1
  # frozen_string_literal: true
2
+ # typed: strict
2
3
 
3
4
  module LLVM
4
5
  class Builder
5
6
  extend Gem::Deprecate
6
7
 
7
8
  # Important: Call #dispose to free backend memory after use.
9
+ #: -> void
8
10
  def initialize
9
- @ptr = C.create_builder()
11
+ @ptr = C.create_builder() #: FFI::Pointer?
12
+ raise RuntimeError if @ptr.null? || ptr.nil?
10
13
  end
11
14
 
15
+ #: FFI::Pointer?
16
+ attr_reader :ptr
17
+
18
+ #: -> void
12
19
  def dispose
13
20
  return if @ptr.nil?
14
21
  C.dispose_builder(@ptr)
@@ -16,6 +23,7 @@ module LLVM
16
23
  end
17
24
 
18
25
  # @private
26
+ #: -> FFI::Pointer?
19
27
  def to_ptr
20
28
  @ptr
21
29
  end
@@ -25,6 +33,7 @@ module LLVM
25
33
  # @param [LLVM::BasicBlock] block
26
34
  # @param [LLVM::Instruction] instruction
27
35
  # @return [LLVM::Builder]
36
+ #: (BasicBlock, Instruction) -> self
28
37
  def position(block, instruction)
29
38
  raise ArgumentError, "Block must be LLVM::BasicBlock" if !block.is_a?(LLVM::BasicBlock)
30
39
 
@@ -38,6 +47,7 @@ module LLVM
38
47
  #
39
48
  # @param [LLVM::Instruction] instruction
40
49
  # @return [LLVM::Builder]
50
+ #: (Instruction) -> self
41
51
  def position_before(instruction)
42
52
  raise ArgumentError, "Instruction must be LLVM::Instruction" if !instruction.is_a?(LLVM::Instruction)
43
53
 
@@ -49,6 +59,7 @@ module LLVM
49
59
  #
50
60
  # @param [LLVM::BasicBlock] block
51
61
  # @return [LLVM::Builder]
62
+ #: (BasicBlock) -> self
52
63
  def position_at_end(block)
53
64
  raise ArgumentError, "Block must be LLVM::BasicBlock" if !block.is_a?(LLVM::BasicBlock)
54
65
 
@@ -59,12 +70,14 @@ module LLVM
59
70
  # The BasicBlock at which the Builder is currently positioned.
60
71
  #
61
72
  # @return [LLVM::BasicBlock]
73
+ #: -> BasicBlock
62
74
  def insert_block
63
75
  BasicBlock.from_ptr(C.get_insert_block(self))
64
76
  end
65
77
 
66
78
  # @return [LLVM::Instruction]
67
79
  # @LLVMinst ret
80
+ #: -> Value
68
81
  def ret_void
69
82
  Instruction.from_ptr(C.build_ret_void(self))
70
83
  end
@@ -72,6 +85,7 @@ module LLVM
72
85
  # @param [LLVM::Value] val The value to return
73
86
  # @return [LLVM::Instruction]
74
87
  # @LLVMinst ret
88
+ #: (?Value?) -> Value
75
89
  def ret(val = nil)
76
90
  unless [LLVM::Value, NilClass].any? { |c| val.is_a?(c) }
77
91
  raise ArgumentError, "Trying to build LLVM ret with non-value: #{val.inspect}"
@@ -84,22 +98,23 @@ module LLVM
84
98
  # @param [Array<LLVM::Value>] vals
85
99
  # @return [LLVM::Instruction]
86
100
  # @LLVMinst ret
101
+ #: (*Value) -> Value
87
102
  def aggregate_ret(*vals)
88
103
  FFI::MemoryPointer.new(FFI.type_size(:pointer) * vals.size) do |vals_ptr|
89
104
  vals_ptr.write_array_of_pointer(vals)
90
- Instruction.from_ptr(C.build_aggregate_ret(self, vals_ptr, vals.size))
91
- end
105
+ return Instruction.from_ptr(C.build_aggregate_ret(self, vals_ptr, vals.size))
106
+ end #: as Value
92
107
  end
93
108
 
94
109
  # Unconditional branching (i.e. goto)
95
110
  # @param [LLVM::BasicBlock] block Where to jump
96
111
  # @return [LLVM::Instruction]
97
112
  # @LLVMinst br
113
+ #: (BasicBlock) -> Value
98
114
  def br(block)
99
115
  raise ArgumentError, "Trying to build LLVM br with non-block: #{block.inspect}" if !block.is_a?(LLVM::BasicBlock)
100
116
 
101
- Instruction.from_ptr(
102
- C.build_br(self, block))
117
+ Instruction.from_ptr(C.build_br(self, block))
103
118
  end
104
119
 
105
120
  # Indirect branching (i.e. computed goto)
@@ -107,6 +122,7 @@ module LLVM
107
122
  # @param [Integer] num_dests Number of possible destinations to be added
108
123
  # @return [LLVM::Instruction]
109
124
  # @LLVMinst indirectbr
125
+ #: (BasicBlock, Integer) -> Value
110
126
  def ibr(addr, num_dests)
111
127
  IndirectBr.from_ptr(
112
128
  C.build_indirect_br(self, addr, num_dests))
@@ -118,6 +134,7 @@ module LLVM
118
134
  # @param [LLVM::BasicBlock] iffalse Where to jump if condition is false
119
135
  # @return [LLVM::Instruction]
120
136
  # @LLVMinst br
137
+ #: (Value, BasicBlock, BasicBlock) -> Value
121
138
  def cond(cond, iftrue, iffalse)
122
139
  raise ArgumentError, "Trying to build LLVM cond br with non-block (true branch): #{iftrue.inspect}" if !iftrue.is_a?(LLVM::BasicBlock)
123
140
 
@@ -125,10 +142,10 @@ module LLVM
125
142
 
126
143
  cond2 = cond_condition(cond)
127
144
 
128
- Instruction.from_ptr(
129
- C.build_cond_br(self, cond2, iftrue, iffalse))
145
+ Instruction.from_ptr(C.build_cond_br(self, cond2, iftrue, iffalse))
130
146
  end
131
147
 
148
+ #: ((Value | bool)) -> Value
132
149
  private def cond_condition(cond)
133
150
  case cond
134
151
  when LLVM::Value
@@ -153,6 +170,7 @@ module LLVM
153
170
  # values to basic blocks. When a value is matched, control will jump
154
171
  # to the corresponding basic block.
155
172
  # @return [LLVM::Instruction]
173
+ #: (Value, BasicBlock, Hash[Value, BasicBlock]) -> Value
156
174
  def switch(val, default, cases)
157
175
  inst = SwitchInst.from_ptr(C.build_switch(self, val, default, cases.size))
158
176
  cases.each do |(c, block)|
@@ -170,33 +188,37 @@ module LLVM
170
188
  # @return [LLVM::Instruction] The value returned by 'fun', unless an
171
189
  # unwind instruction occurs
172
190
  # @LLVMinst invoke
191
+ #: (untyped, [Value], BasicBlock, BasicBlock, ?String) -> InvokeInst
173
192
  def invoke(fun, args, normal, exception, name = "")
174
193
  invoke2(nil, fun, args, normal, exception, name)
175
194
  end
176
195
 
196
+ #: (Type?, untyped, [Value], BasicBlock, BasicBlock, ?String) -> InvokeInst
177
197
  def invoke2(type, fun, args, normal, exception, name = "")
178
198
  type, fun = call2_infer_function_and_type(type, fun)
179
199
 
180
200
  arg_count = args.size
181
- invoke_ins = nil
201
+ invoke_ins = nil #: InvokeInst?
182
202
  FFI::MemoryPointer.new(FFI.type_size(:pointer) * arg_count) do |args_ptr|
183
203
  args_ptr.write_array_of_pointer(args)
184
204
  ins = C.build_invoke2(self, type, fun, args_ptr, arg_count, normal, exception, name)
185
- invoke_ins = InvokeInst.from_ptr(ins)
205
+ invoke_ins = InvokeInst.from_ptr(ins) #: as InvokeInst
186
206
  end
187
207
 
188
208
  if fun.is_a?(Function)
189
209
  invoke_ins.call_conv = fun.call_conv
190
210
  end
191
211
 
192
- invoke_ins
212
+ invoke_ins #: as !nil
193
213
  end
194
214
 
195
215
  # @return LLVM::Value
216
+ #: (Type, untyped, Integer, ?String) -> Value
196
217
  def landing_pad(type, personality_function, num_clauses, name = '')
197
218
  C.build_landing_pad(self, type, personality_function, num_clauses, name)
198
219
  end
199
220
 
221
+ #: (Type, untyped, Integer, ?String) -> Value
200
222
  def landing_pad_cleanup(type, personality_function, num_clauses, name = '')
201
223
  lp = landing_pad(type, personality_function, num_clauses, name)
202
224
  C.set_cleanup(lp, 1)
@@ -205,6 +227,7 @@ module LLVM
205
227
 
206
228
  # Builds an unwind Instruction.
207
229
  # @LLVMinst unwind
230
+ #: -> void
208
231
  def unwind
209
232
  raise DeprecationError
210
233
  end
@@ -213,6 +236,7 @@ module LLVM
213
236
  # provide hints to the optimizer.
214
237
  # @return [LLVM::Instruction]
215
238
  # @LLVMinst unreachable
239
+ #: -> Value
216
240
  def unreachable
217
241
  Instruction.from_ptr(C.build_unreachable(self))
218
242
  end
@@ -223,6 +247,7 @@ module LLVM
223
247
  # @param [String] name Name of the result in LLVM IR
224
248
  # @return [LLVM::Instruction] The integer sum of the two operands
225
249
  # @LLVMinst add
250
+ #: (Value, Value, ?String) -> Value
226
251
  def add(lhs, rhs, name = "")
227
252
  Instruction.from_ptr(C.build_add(self, lhs, rhs, name))
228
253
  end
@@ -233,6 +258,7 @@ module LLVM
233
258
  # @param [String] name Name of the result in LLVM IR
234
259
  # @return [LLVM::Instruction] The integer sum of the two operands
235
260
  # @LLVMinst add
261
+ #: (Value, Value, ?String) -> Value
236
262
  def nsw_add(lhs, rhs, name = "")
237
263
  Instruction.from_ptr(C.build_nsw_add(self, lhs, rhs, name))
238
264
  end
@@ -243,6 +269,7 @@ module LLVM
243
269
  # @param [String] name Name of the result in LLVM IR
244
270
  # @return [LLVM::Instruction] The integer sum of the two operands
245
271
  # @LLVMinst add
272
+ #: (Value, Value, ?String) -> Value
246
273
  def nuw_add(lhs, rhs, name = "")
247
274
  Instruction.from_ptr(C.build_nuw_add(self, lhs, rhs, name))
248
275
  end
@@ -252,6 +279,7 @@ module LLVM
252
279
  # @param [String] name Name of the result in LLVM IR
253
280
  # @return [LLVM::Instruction] The floating point sum of the two operands
254
281
  # @LLVMinst fadd
282
+ #: (Value, Value, ?String) -> Value
255
283
  def fadd(lhs, rhs, name = "")
256
284
  Instruction.from_ptr(C.build_f_add(self, lhs, rhs, name))
257
285
  end
@@ -262,6 +290,7 @@ module LLVM
262
290
  # @param [String] name Name of the result in LLVM IR
263
291
  # @return [LLVM::Instruction] The integer difference of the two operands
264
292
  # @LLVMinst sub
293
+ #: (Value, Value, ?String) -> Value
265
294
  def sub(lhs, rhs, name = "")
266
295
  Instruction.from_ptr(C.build_sub(self, lhs, rhs, name))
267
296
  end
@@ -272,6 +301,7 @@ module LLVM
272
301
  # @param [String] name Name of the result in LLVM IR
273
302
  # @return [LLVM::Instruction] The integer difference of the two operands
274
303
  # @LLVMinst sub
304
+ #: (Value, Value, ?String) -> Value
275
305
  def nsw_sub(lhs, rhs, name = "")
276
306
  Instruction.from_ptr(C.build_nsw_sub(self, lhs, rhs, name))
277
307
  end
@@ -282,6 +312,7 @@ module LLVM
282
312
  # @param [String] name Name of the result in LLVM IR
283
313
  # @return [LLVM::Instruction] The integer difference of the two operands
284
314
  # @LLVMinst sub
315
+ #: (Value, Value, ?String) -> Value
285
316
  def nuw_sub(lhs, rhs, name = "")
286
317
  Instruction.from_ptr(C.build_nuw_sub(self, lhs, rhs, name))
287
318
  end
@@ -292,6 +323,7 @@ module LLVM
292
323
  # @return [LLVM::Instruction] The floating point difference of the two
293
324
  # operands
294
325
  # @LLVMinst fsub
326
+ #: (Value, Value, ?String) -> Value
295
327
  def fsub(lhs, rhs, name = "")
296
328
  Instruction.from_ptr(C.build_f_sub(self, lhs, rhs, name))
297
329
  end
@@ -302,6 +334,7 @@ module LLVM
302
334
  # @param [String] name Name of the result in LLVM IR
303
335
  # @return [LLVM::Instruction] The integer product of the two operands
304
336
  # @LLVMinst mul
337
+ #: (Value, Value, ?String) -> Value
305
338
  def mul(lhs, rhs, name = "")
306
339
  Instruction.from_ptr(C.build_mul(self, lhs, rhs, name))
307
340
  end
@@ -312,6 +345,7 @@ module LLVM
312
345
  # @param [String] name Name of the result in LLVM IR
313
346
  # @return [LLVM::Instruction] The integer product of the two operands
314
347
  # @LLVMinst mul
348
+ #: (Value, Value, ?String) -> Value
315
349
  def nsw_mul(lhs, rhs, name = "")
316
350
  Instruction.from_ptr(C.build_nsw_mul(self, lhs, rhs, name))
317
351
  end
@@ -322,6 +356,7 @@ module LLVM
322
356
  # @param [String] name Name of the result in LLVM IR
323
357
  # @return [LLVM::Instruction] The integer product of the two operands
324
358
  # @LLVMinst mul
359
+ #: (Value, Value, ?String) -> Value
325
360
  def nuw_mul(lhs, rhs, name = "")
326
361
  Instruction.from_ptr(C.build_nuw_mul(self, lhs, rhs, name))
327
362
  end
@@ -333,6 +368,7 @@ module LLVM
333
368
  # @return [LLVM::Instruction] The floating point product of the two
334
369
  # operands
335
370
  # @LLVMinst fmul
371
+ #: (Value, Value, ?String) -> Value
336
372
  def fmul(lhs, rhs, name = "")
337
373
  Instruction.from_ptr(C.build_f_mul(self, lhs, rhs, name))
338
374
  end
@@ -343,6 +379,7 @@ module LLVM
343
379
  # @param [String] name Name of the result in LLVM IR
344
380
  # @return [LLVM::Instruction] The integer quotient of the two operands
345
381
  # @LLVMinst udiv
382
+ #: (Value, Value, ?String) -> Value
346
383
  def udiv(lhs, rhs, name = "")
347
384
  Instruction.from_ptr(C.build_u_div(self, lhs, rhs, name))
348
385
  end
@@ -353,6 +390,7 @@ module LLVM
353
390
  # @param [String] name Name of the result in LLVM IR
354
391
  # @return [LLVM::Instruction] The integer quotient of the two operands
355
392
  # @LLVMinst sdiv
393
+ #: (Value, Value, ?String) -> Value
356
394
  def sdiv(lhs, rhs, name = "")
357
395
  Instruction.from_ptr(C.build_s_div(self, lhs, rhs, name))
358
396
  end
@@ -363,6 +401,7 @@ module LLVM
363
401
  # @param [String] name Name of the result in LLVM IR
364
402
  # @return [LLVM::Instruction] The integer quotient of the two operands
365
403
  # @LLVMinst sdiv
404
+ #: (Value, Value, ?String) -> Value
366
405
  def exact_sdiv(lhs, rhs, name = "")
367
406
  Instruction.from_ptr(C.build_exact_s_div(self, lhs, rhs, name))
368
407
  end
@@ -373,6 +412,7 @@ module LLVM
373
412
  # @return [LLVM::Instruction] The floating point quotient of the two
374
413
  # operands
375
414
  # @LLVMinst fdiv
415
+ #: (Value, Value, ?String) -> Value
376
416
  def fdiv(lhs, rhs, name = "")
377
417
  Instruction.from_ptr(C.build_f_div(self, lhs, rhs, name))
378
418
  end
@@ -383,6 +423,7 @@ module LLVM
383
423
  # @param [String] name Name of the result in LLVM IR
384
424
  # @return [LLVM::Instruction] The integer remainder
385
425
  # @LLVMinst urem
426
+ #: (Value, Value, ?String) -> Value
386
427
  def urem(lhs, rhs, name = "")
387
428
  Instruction.from_ptr(C.build_u_rem(self, lhs, rhs, name))
388
429
  end
@@ -393,6 +434,7 @@ module LLVM
393
434
  # @param [String] name Name of the result in LLVM IR
394
435
  # @return [LLVM::Instruction] The integer remainder
395
436
  # @LLVMinst srem
437
+ #: (Value, Value, ?String) -> Value
396
438
  def srem(lhs, rhs, name = "")
397
439
  Instruction.from_ptr(C.build_s_rem(self, lhs, rhs, name))
398
440
  end
@@ -402,6 +444,7 @@ module LLVM
402
444
  # @param [String] name Name of the result in LLVM IR
403
445
  # @return [LLVM::Instruction] The floating point remainder
404
446
  # @LLVMinst frem
447
+ #: (Value, Value, ?String) -> Value
405
448
  def frem(lhs, rhs, name = "")
406
449
  Instruction.from_ptr(C.build_f_rem(self, lhs, rhs, name))
407
450
  end
@@ -412,6 +455,7 @@ module LLVM
412
455
  # @return [LLVM::Instruction] The floating point negation
413
456
  # @LLVMinst fneg
414
457
  # https://llvm.org/docs/LangRef.html#fneg-instruction
458
+ #: (Value, ?String) -> Value
415
459
  def fneg(lhs, name = "")
416
460
  Instruction.from_ptr(C.build_f_neg(self, lhs, name))
417
461
  end
@@ -421,6 +465,7 @@ module LLVM
421
465
  # @param [String] name Name of the result in LLVM IR
422
466
  # @return [LLVM::Instruction] An integer instruction
423
467
  # @LLVMinst shl
468
+ #: (Value, Value, ?String) -> Value
424
469
  def shl(lhs, rhs, name = "")
425
470
  Instruction.from_ptr(C.build_shl(self, lhs, rhs, name))
426
471
  end
@@ -431,6 +476,7 @@ module LLVM
431
476
  # @param [String] name Name of the result in LLVM IR
432
477
  # @return [LLVM::Instruction] An integer instruction
433
478
  # @LLVMinst lshr
479
+ #: (Value, Value, ?String) -> Value
434
480
  def lshr(lhs, rhs, name = "")
435
481
  Instruction.from_ptr(C.build_l_shr(self, lhs, rhs, name))
436
482
  end
@@ -441,6 +487,7 @@ module LLVM
441
487
  # @param [String] name Name of the result in LLVM IR
442
488
  # @return [LLVM::Instruction] An integer instruction
443
489
  # @LLVMinst ashr
490
+ #: (Value, Value, ?String) -> Value
444
491
  def ashr(lhs, rhs, name = "")
445
492
  Instruction.from_ptr(C.build_a_shr(self, lhs, rhs, name))
446
493
  end
@@ -450,6 +497,7 @@ module LLVM
450
497
  # @param [String] name Name of the result in LLVM IR
451
498
  # @return [LLVM::Instruction] An integer instruction
452
499
  # @LLVMinst and
500
+ #: (Value, Value, ?String) -> Value
453
501
  def and(lhs, rhs, name = "")
454
502
  Instruction.from_ptr(C.build_and(self, lhs, rhs, name))
455
503
  end
@@ -459,6 +507,7 @@ module LLVM
459
507
  # @param [String] name Name of the result in LLVM IR
460
508
  # @return [LLVM::Instruction] An integer instruction
461
509
  # @LLVMinst or
510
+ #: (Value, Value, ?String) -> Value
462
511
  def or(lhs, rhs, name = "")
463
512
  Instruction.from_ptr(C.build_or(self, lhs, rhs, name))
464
513
  end
@@ -468,6 +517,7 @@ module LLVM
468
517
  # @param [String] name Name of the result in LLVM IR
469
518
  # @return [LLVM::Instruction] An integer instruction
470
519
  # @LLVMinst xor
520
+ #: (Value, Value, ?String) -> Value
471
521
  def xor(lhs, rhs, name = "")
472
522
  Instruction.from_ptr(C.build_xor(self, lhs, rhs, name))
473
523
  end
@@ -478,6 +528,7 @@ module LLVM
478
528
  # @param [String] name Name of the result in LLVM IR
479
529
  # @return [LLVM::Instruction] The negated operand
480
530
  # @LLVMinst sub
531
+ #: (Value, ?String) -> Value
481
532
  def neg(arg, name = "")
482
533
  Instruction.from_ptr(C.build_neg(self, arg, name))
483
534
  end
@@ -487,6 +538,7 @@ module LLVM
487
538
  # @param [String] name Name of the result in LLVM IR
488
539
  # @return [LLVM::Instruction] The negated operand
489
540
  # @LLVMinst sub
541
+ #: (Value, ?String) -> Value
490
542
  def nsw_neg(arg, name = "")
491
543
  Instruction.from_ptr(C.build_nsw_neg(self, arg, name))
492
544
  end
@@ -497,6 +549,7 @@ module LLVM
497
549
  # @return [LLVM::Instruction] The negated operand
498
550
  # @LLVMinst sub
499
551
  # @deprecated
552
+ #: (Value, ?String) -> Value
500
553
  def nuw_neg(arg, name = "")
501
554
  Instruction.from_ptr(C.build_nuw_neg(self, arg, name))
502
555
  end
@@ -506,6 +559,7 @@ module LLVM
506
559
  # @param [LLVM::Value] arg Integer or vector of integers
507
560
  # @param [String] name The name of the result in LLVM IR
508
561
  # @return [LLVM::Instruction] The negated operand
562
+ #: (Value, ?String) -> Value
509
563
  def not(arg, name = "")
510
564
  Instruction.from_ptr(C.build_not(self, arg, name))
511
565
  end
@@ -514,6 +568,7 @@ module LLVM
514
568
  # should be malloced
515
569
  # @param [String] name The name of the result in LLVM IR
516
570
  # @return [LLVM::Instruction] A pointer to the malloced bytes
571
+ #: (Type, ?String) -> Value
517
572
  def malloc(ty, name = "")
518
573
  Instruction.from_ptr(C.build_malloc(self, LLVM::Type(ty), name))
519
574
  end
@@ -523,6 +578,7 @@ module LLVM
523
578
  # @param [LLVM::Value] sz Unsigned integer representing size of the array
524
579
  # @param [String] name The name of the result in LLVM IR
525
580
  # @return [LLVM::Instruction] A pointer to the malloced array
581
+ #: (Type, Integer, ?String) -> Value
526
582
  def array_malloc(ty, sz, name = "")
527
583
  size = case sz
528
584
  when LLVM::Value
@@ -541,8 +597,9 @@ module LLVM
541
597
  # @param [String] name The name of the result in LLVM IR
542
598
  # @return [LLVM::Instruction] A pointer to the allocad bytes
543
599
  # @LLVMinst alloca
600
+ #: (Type, ?String) -> Alloca
544
601
  def alloca(ty, name = "")
545
- Instruction.from_ptr(C.build_alloca(self, LLVM::Type(ty), name))
602
+ Alloca.from_ptr(C.build_alloca(self, LLVM::Type(ty), name)) #: as Alloca
546
603
  end
547
604
 
548
605
  # Array stack allocation
@@ -552,12 +609,14 @@ module LLVM
552
609
  # @param [String] name The name of the result in LLVM IR
553
610
  # @return [LLVM::Instruction] A pointer to the allocad array
554
611
  # @LLVMinst alloca
612
+ #: (Type, Integer, ?String) -> Alloca
555
613
  def array_alloca(ty, sz, name = "")
556
- Instruction.from_ptr(C.build_array_alloca(self, LLVM::Type(ty), sz, name))
614
+ Alloca.from_ptr(C.build_array_alloca(self, LLVM::Type(ty), sz, name)) #: as Alloca
557
615
  end
558
616
 
559
617
  # @param [LLVM::Value] ptr The pointer to be freed
560
618
  # @return [LLVM::Instruction] The result of the free instruction
619
+ #: (Value) -> Value
561
620
  def free(ptr)
562
621
  Instruction.from_ptr(C.build_free(self, ptr))
563
622
  end
@@ -568,10 +627,12 @@ module LLVM
568
627
  # @return [LLVM::Instruction] The result of the load operation. Represents
569
628
  # a value of the pointer's type.
570
629
  # @LLVMinst load
630
+ #: (Value, ?String) -> Value
571
631
  def load(ptr, name = "")
572
632
  load2(nil, ptr, name)
573
633
  end
574
634
 
635
+ #: (Type?, Value, ?String) -> Value
575
636
  def load2(type, ptr, name = "")
576
637
  must_be_value!(ptr)
577
638
 
@@ -587,6 +648,7 @@ module LLVM
587
648
  # @param [LLVM::Value] ptr A pointer to the same type as val
588
649
  # @return [LLVM::Instruction] The result of the store operation
589
650
  # @LLVMinst store
651
+ #: (Value, Value) -> Value
590
652
  def store(val, ptr)
591
653
  raise "val must be a Value, got #{val.class.name}" unless Value === val
592
654
  Instruction.from_ptr(C.build_store(self, val, ptr))
@@ -601,6 +663,7 @@ module LLVM
601
663
  # @LLVMinst gep
602
664
  # @see http://llvm.org/docs/GetElementPtr.html
603
665
  # may return Instruction or GlobalVariable
666
+ #: (Value, [Value], ?String) -> Value
604
667
  def gep(ptr, indices, name = "")
605
668
  gep2(nil, ptr, indices, name)
606
669
  end
@@ -615,6 +678,7 @@ module LLVM
615
678
  # @LLVMinst gep2
616
679
  # @see http://llvm.org/docs/GetElementPtr.html
617
680
  # may return Instruction or GlobalVariable
681
+ #: (Type?, Value, [Value], ?String) -> Value
618
682
  def gep2(type, ptr, indices, name = '')
619
683
  must_be_value!(ptr)
620
684
 
@@ -626,7 +690,7 @@ module LLVM
626
690
  indices_ptr.write_array_of_pointer(indices)
627
691
  ins = C.build_gep2(self, type, ptr, indices_ptr, indices.size, name)
628
692
  return Instruction.from_ptr(ins)
629
- end
693
+ end #: as Value
630
694
  end
631
695
 
632
696
  # Builds a inbounds getelementptr instruction. If the indices are outside
@@ -638,10 +702,12 @@ module LLVM
638
702
  # @return [LLVM::Instruction] The resulting pointer
639
703
  # @LLVMinst gep
640
704
  # @see http://llvm.org/docs/GetElementPtr.html
705
+ #: (Value, [Value], ?String) -> Value
641
706
  def inbounds_gep(ptr, indices, name = "")
642
707
  inbounds_gep2(nil, ptr, indices, name)
643
708
  end
644
709
 
710
+ #: (Type?, Value, [Value], ?String) -> Value
645
711
  def inbounds_gep2(type, ptr, indices, name = "")
646
712
  must_be_value!(ptr)
647
713
 
@@ -653,7 +719,7 @@ module LLVM
653
719
  indices_ptr.write_array_of_pointer(indices)
654
720
  ins = C.build_inbounds_gep2(self, type, ptr, indices_ptr, indices.size, name)
655
721
  return Instruction.from_ptr(ins)
656
- end
722
+ end #: as Value
657
723
  end
658
724
 
659
725
  # Builds a struct getelementptr Instruction.
@@ -665,10 +731,12 @@ module LLVM
665
731
  # @return [LLVM::Instruction] The resulting pointer
666
732
  # @LLVMinst gep
667
733
  # @see http://llvm.org/docs/GetElementPtr.html
734
+ #: (Value, Value, ?String) -> Value
668
735
  def struct_gep(ptr, idx, name = "")
669
736
  struct_gep2(nil, ptr, idx, name)
670
737
  end
671
738
 
739
+ #: (Type?, Value, Value, ?String) -> Value
672
740
  def struct_gep2(type, ptr, idx, name = "")
673
741
  must_be_value!(ptr)
674
742
 
@@ -683,6 +751,7 @@ module LLVM
683
751
  # @param [String] string The string used by the initialize
684
752
  # @param [Name] name Name of the result in LLVM IR
685
753
  # @return [LLVM::Instruction] Reference to the global string
754
+ #: (String, ?String) -> Value
686
755
  def global_string(string, name = "")
687
756
  Instruction.from_ptr(C.build_global_string(self, string, name))
688
757
  end
@@ -691,6 +760,7 @@ module LLVM
691
760
  # @param [String] string The string used by the initializer
692
761
  # @param [String] name The name of the result in LLVM IR
693
762
  # @return [LLVM::Instruction] Reference to the global string pointer
763
+ #: (String, ?String) -> Value
694
764
  def global_string_pointer(string, name = "")
695
765
  Instruction.from_ptr(C.build_global_string_ptr(self, string, name))
696
766
  end
@@ -703,6 +773,7 @@ module LLVM
703
773
  # @param [String] name The name of the result in LLVM IR
704
774
  # @return [LLVM::Instruction] The truncated value
705
775
  # @LLVMinst trunc
776
+ #: (Value, Type, ?String) -> Value
706
777
  def trunc(val, ty, name = "")
707
778
  Instruction.from_ptr(C.build_trunc(self, val, LLVM::Type(ty), name))
708
779
  end
@@ -715,6 +786,7 @@ module LLVM
715
786
  # @param [String] name The name of the result in LLVM IR
716
787
  # @return [LLVM::Instruction] The extended value
717
788
  # @LLVMinst zext
789
+ #: (Value, Type, ?String) -> Value
718
790
  def zext(val, ty, name = "")
719
791
  Instruction.from_ptr(C.build_z_ext(self, val, LLVM::Type(ty), name))
720
792
  end
@@ -727,6 +799,7 @@ module LLVM
727
799
  # @param [String] name The name of the result in LLVM IR
728
800
  # @return [LLVM::Instruction] The extended value
729
801
  # @LLVMinst sext
802
+ #: (Value, Type, ?String) -> Value
730
803
  def sext(val, ty, name = "")
731
804
  Instruction.from_ptr(C.build_s_ext(self, val, LLVM::Type(ty), name))
732
805
  end
@@ -738,6 +811,7 @@ module LLVM
738
811
  # @param [String] name The name of the result in LLVM IR
739
812
  # @return [LLVM::Instruction] The converted value
740
813
  # @LLVMinst fptoui
814
+ #: (Value, Type, ?String) -> Value
741
815
  def fp2ui(val, ty, name = "")
742
816
  Instruction.from_ptr(C.build_fp_to_ui(self, val, LLVM::Type(ty), name))
743
817
  end
@@ -749,6 +823,7 @@ module LLVM
749
823
  # @param [String] name The name of the result in LLVM IR
750
824
  # @return [LLVM::Instruction] The converted value
751
825
  # @LLVMinst fptosi
826
+ #: (Value, Type, ?String) -> Value
752
827
  def fp2si(val, ty, name = "")
753
828
  Instruction.from_ptr(C.build_fp_to_si(self, val, LLVM::Type(ty), name))
754
829
  end
@@ -761,6 +836,7 @@ module LLVM
761
836
  # @param [String] name The name of the result in LLVM IR
762
837
  # @return [LLVM::Instruction] The converted value
763
838
  # @LLVMinst uitofp
839
+ #: (Value, Type, ?String) -> Value
764
840
  def ui2fp(val, ty, name = "")
765
841
  Instruction.from_ptr(C.build_ui_to_fp(self, val, LLVM::Type(ty), name))
766
842
  end
@@ -773,6 +849,7 @@ module LLVM
773
849
  # @param [String] name The name of the result in LLVM IR
774
850
  # @return [LLVM::Instruction] The converted value
775
851
  # @LLVMinst sitofp
852
+ #: (Value, Type, ?String) -> Value
776
853
  def si2fp(val, ty, name = "")
777
854
  Instruction.from_ptr(C.build_si_to_fp(self, val, LLVM::Type(ty), name))
778
855
  end
@@ -784,6 +861,7 @@ module LLVM
784
861
  # @param [String] name The name of the result in LLVM IR
785
862
  # @return [LLVM::Instruction] The truncated value
786
863
  # @LLVMinst fptrunc
864
+ #: (Value, Type, ?String) -> Value
787
865
  def fp_trunc(val, ty, name = "")
788
866
  Instruction.from_ptr(C.build_fp_trunc(self, val, LLVM::Type(ty), name))
789
867
  end
@@ -795,6 +873,7 @@ module LLVM
795
873
  # @param [String] name The name of the result in LLVM IR
796
874
  # @return [LLVM::Instruction] The extended value
797
875
  # @LLVMinst fpext
876
+ #: (Value, Type, ?String) -> Value
798
877
  def fp_ext(val, ty, name = "")
799
878
  Instruction.from_ptr(C.build_fp_ext(self, val, LLVM::Type(ty), name))
800
879
  end
@@ -806,6 +885,7 @@ module LLVM
806
885
  # @return [LLVM::Instruction] An integer of the given type representing
807
886
  # the pointer's address
808
887
  # @LLVMinst ptrtoint
888
+ #: (Value, Type, ?String) -> Value
809
889
  def ptr2int(val, ty, name = "")
810
890
  Instruction.from_ptr(C.build_ptr_to_int(self, val, LLVM::Type(ty), name))
811
891
  end
@@ -817,6 +897,7 @@ module LLVM
817
897
  # @return [LLVM::Instruction] A pointer of the given type and the address
818
898
  # held in val
819
899
  # @LLVMinst inttoptr
900
+ #: (Value, Type, ?String) -> Value
820
901
  def int2ptr(val, ty, name = "")
821
902
  Instruction.from_ptr(C.build_int_to_ptr(self, val, LLVM::Type(ty), name))
822
903
  end
@@ -827,6 +908,7 @@ module LLVM
827
908
  # @param [String] name The name of the result in LLVM IR
828
909
  # @return [LLVM::Instruction] A value of the target type
829
910
  # @LLVMinst bitcast
911
+ #: (Value, Type, ?String) -> Value
830
912
  def bit_cast(val, ty, name = "")
831
913
  Instruction.from_ptr(C.build_bit_cast(self, val, LLVM::Type(ty), name))
832
914
  end
@@ -837,6 +919,7 @@ module LLVM
837
919
  # @return [LLVM::Instruction]
838
920
  # @LLVMinst zext
839
921
  # @LLVMinst bitcast
922
+ #: (Value, Type, ?String) -> Value
840
923
  def zext_or_bit_cast(val, ty, name = "")
841
924
  Instruction.from_ptr(C.build_z_ext_or_bit_cast(self, val, LLVM::Type(ty), name))
842
925
  end
@@ -847,6 +930,7 @@ module LLVM
847
930
  # @return [LLVM::Instruction]
848
931
  # @LLVMinst sext
849
932
  # @LLVMinst bitcast
933
+ #: (Value, Type, ?String) -> Value
850
934
  def sext_or_bit_cast(val, ty, name = "")
851
935
  Instruction.from_ptr(C.build_s_ext_or_bit_cast(self, val, LLVM::Type(ty), name))
852
936
  end
@@ -857,6 +941,7 @@ module LLVM
857
941
  # @return [LLVM::Instruction]
858
942
  # @LLVMinst trunc
859
943
  # @LLVMinst bitcast
944
+ #: (Value, Type, ?String) -> Value
860
945
  def trunc_or_bit_cast(val, ty, name = "")
861
946
  Instruction.from_ptr(C.build_trunc_or_bit_cast(self, val, LLVM::Type(ty), name))
862
947
  end
@@ -866,6 +951,7 @@ module LLVM
866
951
  # @param [String] name The name of the result in LLVM IR
867
952
  # @return [LLVM::Instruction]
868
953
  # Cast pointer to other type
954
+ #: (Value, Type, ?String) -> Value
869
955
  def pointer_cast(val, ty, name = "")
870
956
  Instruction.from_ptr(C.build_pointer_cast(self, val, LLVM::Type(ty), name))
871
957
  end
@@ -874,6 +960,7 @@ module LLVM
874
960
  # @param [LLVM::Type, #ty] ty
875
961
  # @param [String] name The name of the result in LLVM IR
876
962
  # @return [LLVM::Instruction]
963
+ #: (Value, Type, ?String) -> Value
877
964
  def int_cast(val, ty, name = "")
878
965
  Instruction.from_ptr(C.build_int_cast(self, val, LLVM::Type(ty), name))
879
966
  end
@@ -883,6 +970,7 @@ module LLVM
883
970
  # @param [String] name The name of the result in LLVM IR
884
971
  # @param [bool] signed whether to sign or zero extend
885
972
  # @return [LLVM::Instruction]
973
+ #: (Value, Type, bool, ?String) -> Value
886
974
  def int_cast2(val, ty, signed, name = "")
887
975
  Instruction.from_ptr(C.build_int_cast2(self, val, LLVM::Type(ty), signed, name))
888
976
  end
@@ -891,6 +979,7 @@ module LLVM
891
979
  # @param [LLVM::Type, #ty] ty
892
980
  # @param [String] name The name of the result in LLVM IR
893
981
  # @return [LLVM::Instruction]
982
+ #: (Value, Type, ?String) -> Value
894
983
  def fp_cast(val, ty, name = "")
895
984
  Instruction.from_ptr(C.build_fp_cast(self, val, LLVM::Type(ty), name))
896
985
  end
@@ -915,6 +1004,7 @@ module LLVM
915
1004
  # @param [String] name The name of the result in LLVM IR
916
1005
  # @return [LLVM::Instruction] A boolean represented as i1
917
1006
  # @LLVMinst icmp
1007
+ #: (Symbol, Value, Value, ?String) -> Value
918
1008
  def icmp(pred, lhs, rhs, name = "")
919
1009
  Instruction.from_ptr(C.build_i_cmp(self, pred, lhs, rhs, name))
920
1010
  end
@@ -945,6 +1035,7 @@ module LLVM
945
1035
  # @param [String] name The name of the result in LLVM IR
946
1036
  # @return [LLVM::Instruction] A boolean represented as i1
947
1037
  # @LLVMinst fcmp
1038
+ #: (Symbol, Value, Value, ?String) -> Value
948
1039
  def fcmp(pred, lhs, rhs, name = "")
949
1040
  Instruction.from_ptr(C.build_f_cmp(self, pred, lhs, rhs, name))
950
1041
  end
@@ -958,6 +1049,7 @@ module LLVM
958
1049
  # @param [String] name The name of the result in LLVM IR
959
1050
  # @return [LLVM::Instruction] The phi node
960
1051
  # @LLVMinst phi
1052
+ #: (Type, Hash[BasicBlock, Value], ?String) -> Value
961
1053
  def phi(ty, incoming, name = "")
962
1054
  phi = Phi.from_ptr(C.build_phi(self, LLVM::Type(ty), name))
963
1055
  phi.add_incoming(incoming)
@@ -971,10 +1063,17 @@ module LLVM
971
1063
  # @param [Array<LLVM::Value>] args
972
1064
  # @param [LLVM::Instruction]
973
1065
  # @LLVMinst call
1066
+ #: (untyped, *Value) -> CallInst
974
1067
  def call(fun, *args)
975
- call2(nil, fun, *args)
1068
+ call2(
1069
+ nil,
1070
+ fun,
1071
+ *args #: untyped
1072
+ )
976
1073
  end
977
1074
 
1075
+ # TODO: (Type?, untyped) -> [Type, Function]
1076
+ #: (Type?, untyped) -> [untyped, untyped]
978
1077
  private def call2_infer_function_and_type(type, fun)
979
1078
  fun2 = fun.is_a?(LLVM::Value) ? fun : insert_block.parent.global_parent.functions[fun.to_s]
980
1079
 
@@ -990,6 +1089,7 @@ module LLVM
990
1089
  [type, fun2]
991
1090
  end
992
1091
 
1092
+ #: (Type, untyped, *Value) -> CallInst
993
1093
  def call2(type, fun, *args)
994
1094
  type, fun = call2_infer_function_and_type(type, fun)
995
1095
 
@@ -1003,7 +1103,7 @@ module LLVM
1003
1103
  args_ptr.write_array_of_pointer(args)
1004
1104
  ins = C.build_call2(self, type, fun, args_ptr, args.size, name)
1005
1105
 
1006
- call_inst = CallInst.from_ptr(ins)
1106
+ call_inst = CallInst.from_ptr(ins) #: as CallInst
1007
1107
 
1008
1108
  if fun.is_a?(Function)
1009
1109
  call_inst.call_conv = fun.call_conv
@@ -1023,6 +1123,7 @@ module LLVM
1023
1123
  # @return [LLVM::Instruction] An instruction representing either _then or
1024
1124
  # _else
1025
1125
  # @LLVMinst select
1126
+ #: (Value, Value, Value, ?String) -> Value
1026
1127
  def select(_if, _then, _else, name = "")
1027
1128
  Instruction.from_ptr(C.build_select(self, _if, _then, _else, name))
1028
1129
  end
@@ -1034,6 +1135,7 @@ module LLVM
1034
1135
  # @param [String] name The value of the result in LLVM IR
1035
1136
  # @return [LLVM::Instruction] The extracted element
1036
1137
  # @LLVMinst extractelement
1138
+ #: (Value, Value, ?String) -> Value
1037
1139
  def extract_element(vector, idx, name = "")
1038
1140
  must_be_value!(vector)
1039
1141
  must_be_value!(idx)
@@ -1052,6 +1154,7 @@ module LLVM
1052
1154
  # @param [String] name The name of the result in LLVM IR
1053
1155
  # @return [LLVM::Instruction] A vector the same type as 'vector'
1054
1156
  # @LLVMinst insertelement
1157
+ #: (Value, Value, Value, ?String) -> Value
1055
1158
  def insert_element(vector, elem, idx, name = "")
1056
1159
  must_be_value!(vector)
1057
1160
  must_be_value!(elem)
@@ -1072,6 +1175,7 @@ module LLVM
1072
1175
  # @param [String] name The name of the result in LLVM IR
1073
1176
  # @return [LLVM::Instruction] The shuffled vector
1074
1177
  # @LLVMinst shufflevector
1178
+ #: (Value, Value, Value, ?String) -> Value
1075
1179
  def shuffle_vector(vec1, vec2, mask, name = "")
1076
1180
  Instruction.from_ptr(C.build_shuffle_vector(self, vec1, vec2, mask, name))
1077
1181
  end
@@ -1082,6 +1186,7 @@ module LLVM
1082
1186
  # @param [String] name The name of the result in LLVM IR
1083
1187
  # @return [LLVM::Instruction] The extracted value
1084
1188
  # @LLVMinst extractvalue
1189
+ #: (Value, Integer, ?String) -> Value
1085
1190
  def extract_value(aggregate, idx, name = "")
1086
1191
  must_be_value!(aggregate)
1087
1192
  error = value_error(aggregate, idx)
@@ -1099,6 +1204,7 @@ module LLVM
1099
1204
  # @param [String] name The name of the result in LLVM IR
1100
1205
  # @return [LLVM::Instruction] An aggregate value of the same type as 'aggregate'
1101
1206
  # @LLVMinst insertvalue
1207
+ #: (Value, Value, Integer, ?String) -> Value
1102
1208
  def insert_value(aggregate, elem, idx, name = "")
1103
1209
  must_be_value!(aggregate)
1104
1210
  must_be_value!(elem)
@@ -1114,6 +1220,8 @@ module LLVM
1114
1220
  # @param [LLVM::Value] val The value to check
1115
1221
  # @param [String] name The name of the result in LLVM IR
1116
1222
  # @return [LLVM::Instruction] An i1
1223
+ # rubocop:disable Naming/PredicatePrefix
1224
+ #: (Value, ?String) -> Value
1117
1225
  def is_null(val, name = "")
1118
1226
  Instruction.from_ptr(C.build_is_null(self, val, name))
1119
1227
  end
@@ -1122,7 +1230,8 @@ module LLVM
1122
1230
  # @param [LLVM::Value] val The value to check
1123
1231
  # @param [String] name The name of the result in LLVM IR
1124
1232
  # @return [LLVM::Instruction] An i1
1125
- def is_not_null(val, name = "")
1233
+ #: (Value, ?String) -> Value
1234
+ def is_not_null(val, name = "") # rubocop:disable Naming/PredicatePrefix
1126
1235
  Instruction.from_ptr(C.build_is_not_null(self, val, name))
1127
1236
  end
1128
1237
 
@@ -1132,10 +1241,12 @@ module LLVM
1132
1241
  # @param [String] name The name of the result in LLVM IR
1133
1242
  # @return [LLVM::Instruction] The integer difference between the two
1134
1243
  # pointers
1244
+ #: (Value, Value, ?String) -> Value
1135
1245
  def ptr_diff(lhs, rhs, name = "")
1136
1246
  ptr_diff2(nil, lhs, rhs, name)
1137
1247
  end
1138
1248
 
1249
+ #: (Type?, Value, Value, ?String) -> Value
1139
1250
  def ptr_diff2(type, lhs, rhs, name = "")
1140
1251
  must_be_value!(lhs)
1141
1252
  must_be_value!(rhs)
@@ -1154,19 +1265,23 @@ module LLVM
1154
1265
 
1155
1266
  private
1156
1267
 
1268
+ #: (untyped) -> void
1157
1269
  def must_be_value!(value)
1158
1270
  raise ArgumentError, "must be a Value, got #{value.class.name}" unless Value === value
1159
1271
  end
1160
1272
 
1273
+ #: (untyped) -> void
1161
1274
  def must_be_type!(type)
1162
1275
  type2 = LLVM.Type(type)
1163
1276
  raise ArgumentError, "must be a Type (LLVMTypeRef), got #{type2.class.name}" unless Type === type2
1164
1277
  end
1165
1278
 
1279
+ #: (untyped) -> void
1166
1280
  def must_infer_type!(value)
1167
1281
  infer_type(value)
1168
1282
  end
1169
1283
 
1284
+ #: (untyped) -> void
1170
1285
  def infer_type(ptr)
1171
1286
  case ptr
1172
1287
  when GlobalVariable
@@ -1178,6 +1293,7 @@ module LLVM
1178
1293
  end
1179
1294
  end
1180
1295
 
1296
+ #: (untyped) -> void
1181
1297
  def must_infer_instruction_type!(ptr)
1182
1298
  case ptr.opcode
1183
1299
  when :get_element_ptr
@@ -1191,6 +1307,7 @@ module LLVM
1191
1307
  end
1192
1308
  end
1193
1309
 
1310
+ #: (untyped) -> void
1194
1311
  def must_infer_gep!(ptr)
1195
1312
  source_type = Type.from_ptr(C.get_gep_source_element_type(ptr))
1196
1313
  case source_type.kind
@@ -1205,6 +1322,7 @@ module LLVM
1205
1322
  end
1206
1323
  end
1207
1324
 
1325
+ #: (untyped, Value) -> String?
1208
1326
  def element_error(vector, idx)
1209
1327
  if !vector.is_a?(LLVM::Value)
1210
1328
  # :nocov:
@@ -1221,6 +1339,7 @@ module LLVM
1221
1339
  end
1222
1340
  end
1223
1341
 
1342
+ #: (untyped, Integer) -> String?
1224
1343
  def value_error(aggregate, idx)
1225
1344
  if !aggregate.is_a?(LLVM::Value)
1226
1345
  # :nocov: