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,15 +1,21 @@
1
1
  # frozen_string_literal: true
2
+ # typed: true
2
3
 
3
4
  module LLVM
4
5
  class Value
5
6
  include PointerIdentity
6
7
 
7
8
  # @private
9
+ #: (FFI::Pointer) -> instance
8
10
  def self.from_ptr(ptr)
9
- return if ptr.null?
10
- val = allocate
11
- val.instance_variable_set(:@ptr, ptr)
12
- val
11
+ new(ptr)
12
+ end
13
+
14
+ # @private
15
+ #: (FFI::Pointer) -> void
16
+ def initialize(ptr)
17
+ raise ArgumentError if ptr.null?
18
+ @ptr = ptr
13
19
  end
14
20
 
15
21
  def self.from_ptr_kind(ptr) # rubocop:disable Metrics/CyclomaticComplexity
@@ -31,6 +37,8 @@ module LLVM
31
37
  ConstantExpr.from_ptr(ptr)
32
38
  when :const_null
33
39
  ConstantNull.from_ptr(ptr)
40
+ when :const_struct, :const_aggregregate_zero
41
+ ConstantStruct.from_ptr(ptr)
34
42
  else
35
43
  raise "from_ptr_kind cannot handle: #{kind}"
36
44
  end
@@ -46,8 +54,9 @@ module LLVM
46
54
  end
47
55
 
48
56
  # Returns the value's type.
57
+ #: -> Type
49
58
  def type
50
- Type.from_ptr(C.type_of(self), nil)
59
+ Type.from_ptr(C.type_of(self))
51
60
  end
52
61
 
53
62
  # Returns the value's kind.
@@ -55,6 +64,7 @@ module LLVM
55
64
  C.get_value_kind(self)
56
65
  end
57
66
 
67
+ #: -> bool
58
68
  def allocated_type?
59
69
  case self
60
70
  when Instruction
@@ -72,9 +82,10 @@ module LLVM
72
82
  alloc_type = C.get_allocated_type(self)
73
83
  return nil if alloc_type.nil?
74
84
 
75
- Type.from_ptr(alloc_type, nil)
85
+ Type.from_ptr(alloc_type)
76
86
  end
77
87
 
88
+ #: -> bool
78
89
  def gep_source_element_type?
79
90
  is_a?(Instruction)
80
91
  end
@@ -96,33 +107,52 @@ module LLVM
96
107
  C.set_value_name(self, str)
97
108
  end
98
109
 
99
- # Print the value's IR to stdout.
110
+ # Print the value's IR to the standard error.
111
+ #
112
+ # Renders as LLVMDumpValue does, but written through Ruby's $stderr rather
113
+ # than LLVM's errs(), so reassigning $stderr captures it. Always ends with a
114
+ # newline; LLVMDumpValue emits none for values whose IR lacks one, such as
115
+ # constants.
116
+ #: -> void
100
117
  def dump
101
- # :nocov:
102
- C.dump_value(self)
103
- # :nocov:
118
+ # not warn: warn is silenced by -W0, and an explicit dump must always print
119
+ $stderr.puts(debug_s) # rubocop:disable Style/StderrPuts
104
120
  end
105
121
 
122
+ # Returns the value's IR as a string.
123
+ #: -> String
106
124
  def to_s
107
125
  C.print_value_to_string(self)
108
126
  end
109
127
 
128
+ # Returns the value's IR as #dump renders it (LLVM's IsForDebug), which
129
+ # differs from #to_s only for function declarations, whose parameters #to_s
130
+ # omits.
131
+ #: -> String
132
+ def debug_s
133
+ Support::C.print_value_to_string_debug(self)
134
+ end
135
+
110
136
  # Returns whether the value is constant.
137
+ #: -> bool
111
138
  def constant?
112
139
  C.is_constant(self)
113
140
  end
114
141
 
115
142
  # Returns whether the value is null.
143
+ #: -> bool
116
144
  def null?
117
145
  C.is_null(self)
118
146
  end
119
147
 
120
148
  # Returns whether the value is undefined.
149
+ #: -> bool
121
150
  def undef?
122
151
  C.is_undef(self)
123
152
  end
124
153
  alias_method :undefined?, :undef?
125
154
 
155
+ #: -> bool
126
156
  def poison?
127
157
  C.is_poison(self)
128
158
  end
@@ -238,7 +268,7 @@ module LLVM
238
268
  end
239
269
 
240
270
  # Iterates through each Instruction in the collection.
241
- def each
271
+ def each(&)
242
272
  return to_enum :each unless block_given?
243
273
  inst = first
244
274
  final = last
@@ -297,7 +327,7 @@ module LLVM
297
327
  end
298
328
 
299
329
  # Iterates through each operand in the collection.
300
- def each
330
+ def each(&)
301
331
  return to_enum :each unless block_given?
302
332
  0.upto(size - 1) { |i| yield self[i] }
303
333
  self
@@ -330,6 +360,11 @@ module LLVM
330
360
  ConstantExpr.from_ptr(C.const_bit_cast(self, type))
331
361
  end
332
362
 
363
+ # Conversion to pointer.
364
+ def int_to_ptr(type = LLVM.Pointer)
365
+ ConstantExpr.from_ptr(C.const_int_to_ptr(self, type))
366
+ end
367
+
333
368
  # @deprecated
334
369
  alias_method :bit_cast, :bitcast_to
335
370
 
@@ -340,7 +375,8 @@ module LLVM
340
375
  FFI::MemoryPointer.new(FFI.type_size(:pointer) * indices.size) do |indices_ptr|
341
376
  indices_ptr.write_array_of_pointer(indices)
342
377
  return ConstantExpr.from_ptr(
343
- C.const_gep(self, indices_ptr, indices.size))
378
+ C.const_gep(self, indices_ptr, indices.size)
379
+ )
344
380
  end
345
381
  end
346
382
 
@@ -355,7 +391,7 @@ module LLVM
355
391
  if size_or_values.is_a?(Integer)
356
392
  raise ArgumentError, 'block not given' unless block
357
393
  size = size_or_values
358
- values = (0...size).map { |i| yield i }
394
+ values = (0...size).map(&block)
359
395
  else
360
396
  values = size_or_values
361
397
  size = values.size
@@ -377,8 +413,8 @@ module LLVM
377
413
 
378
414
  # ConstantArray.const(type, 3) {|i| ... } or
379
415
  # ConstantArray.const(type, [...])
380
- def self.const(type, size_or_values, &block)
381
- vals = LLVM::Support.allocate_pointers(size_or_values, &block)
416
+ def self.const(type, size_or_values, &)
417
+ vals = LLVM::Support.allocate_pointers(size_or_values, &)
382
418
  from_ptr C.const_array(type, vals, vals.size / vals.type_size)
383
419
  end
384
420
 
@@ -428,6 +464,7 @@ module LLVM
428
464
  # end
429
465
 
430
466
  # Negation.
467
+ #: -> ConstantInt
431
468
  def -@
432
469
  self.class.from_ptr(C.const_neg(self))
433
470
  end
@@ -435,12 +472,14 @@ module LLVM
435
472
  alias_method :neg, :-@
436
473
 
437
474
  # "No signed wrap" negation.
475
+ #: -> ConstantInt
438
476
  def nsw_neg
439
477
  self.class.from_ptr(C.const_nsw_neg(self))
440
478
  end
441
479
 
442
480
  # "No unsigned wrap" negation.
443
481
  # @deprecated
482
+ #: -> ConstantInt
444
483
  def nuw_neg
445
484
  # :nocov:
446
485
  self.class.from_ptr(C.const_nuw_neg(self))
@@ -449,6 +488,7 @@ module LLVM
449
488
  deprecate :nuw_neg, "neg", 2025, 3
450
489
 
451
490
  # Addition.
491
+ #: (ConstantInt) -> ConstantInt
452
492
  def +(rhs)
453
493
  self.class.from_ptr(C.const_add(self, rhs))
454
494
  end
@@ -456,16 +496,19 @@ module LLVM
456
496
  alias_method :add, :+
457
497
 
458
498
  # "No signed wrap" addition.
499
+ #: (ConstantInt) -> ConstantInt
459
500
  def nsw_add(rhs)
460
501
  self.class.from_ptr(C.const_nsw_add(self, rhs))
461
502
  end
462
503
 
463
504
  # "No unsigned wrap" addition.
505
+ #: (ConstantInt) -> ConstantInt
464
506
  def nuw_add(rhs)
465
507
  self.class.from_ptr(C.const_nuw_add(self, rhs))
466
508
  end
467
509
 
468
510
  # Subtraction.
511
+ #: (ConstantInt) -> ConstantInt
469
512
  def -(rhs)
470
513
  self.class.from_ptr(C.const_sub(self, rhs))
471
514
  end
@@ -473,58 +516,71 @@ module LLVM
473
516
  alias_method :sub, :-
474
517
 
475
518
  # "No signed wrap" subtraction.
519
+ #: (ConstantInt) -> ConstantInt
476
520
  def nsw_sub(rhs)
477
521
  self.class.from_ptr(C.const_nsw_sub(self, rhs))
478
522
  end
479
523
 
480
524
  # "No unsigned wrap" subtraction.
525
+ #: (ConstantInt) -> ConstantInt
481
526
  def nuw_sub(rhs)
482
527
  self.class.from_ptr(C.const_nuw_sub(self, rhs))
483
528
  end
484
529
 
485
530
  # Multiplication.
531
+ #: (ConstantInt) -> ConstantInt
486
532
  def *(rhs)
487
- self.class.from_ptr(C.const_mul(self, rhs))
533
+ width = [type.width, rhs.type.width].max
534
+ product = to_i * rhs.to_i
535
+ modulo = product % (2**width)
536
+ LLVM::Type.integer(width).from_i(modulo, false)
488
537
  end
489
538
 
490
539
  alias_method :mul, :*
491
540
 
492
541
  # "No signed wrap" multiplication.
542
+ #: (ConstantInt) -> ConstantInt
493
543
  def nsw_mul(rhs)
494
- self.class.from_ptr(C.const_nsw_mul(self, rhs))
544
+ mul(rhs)
495
545
  end
496
546
 
497
547
  # "No unsigned wrap" multiplication.
548
+ #: (ConstantInt) -> ConstantInt
498
549
  def nuw_mul(rhs)
499
- self.class.from_ptr(C.const_nuw_mul(self, rhs))
550
+ mul(rhs)
500
551
  end
501
552
 
502
553
  # Unsigned division.
554
+ #: (ConstantInt) -> ConstantInt
503
555
  def udiv(rhs)
504
556
  width = [type.width, rhs.type.width].max
505
557
  LLVM::Type.integer(width).from_i(to_ui / rhs.to_ui, false)
506
558
  end
507
559
 
508
560
  # Signed division.
561
+ #: (ConstantInt) -> ConstantInt
509
562
  def /(rhs)
510
563
  width = [type.width, rhs.type.width].max
511
564
  LLVM::Type.integer(width).from_i(to_si / rhs.to_si, true)
512
565
  end
513
566
 
514
567
  # Unsigned remainder.
568
+ #: (ConstantInt) -> ConstantInt
515
569
  def urem(rhs)
516
570
  width = [type.width, rhs.type.width].max
517
571
  LLVM::Type.integer(width).from_i(to_ui % rhs.to_ui, false)
518
572
  end
519
573
 
520
574
  # Signed remainder.
575
+ #: (ConstantInt) -> ConstantInt
521
576
  def rem(rhs)
522
577
  width = [type.width, rhs.type.width].max
523
578
  LLVM::Type.integer(width).from_i(to_si % rhs.to_si, true)
524
579
  end
525
580
 
526
581
  # Boolean negation.
527
- def ~@
582
+ #: -> ConstantInt
583
+ def ~
528
584
  self.class.from_ptr(C.const_not(self))
529
585
  end
530
586
 
@@ -593,11 +649,6 @@ module LLVM
593
649
  raise DeprecationError
594
650
  end
595
651
 
596
- # Conversion to pointer.
597
- def int_to_ptr(type = LLVM.Pointer)
598
- ConstantExpr.from_ptr(C.const_int_to_ptr(self, type))
599
- end
600
-
601
652
  def to_ui
602
653
  to_i(false)
603
654
  end
@@ -664,6 +715,12 @@ module LLVM
664
715
  end
665
716
  end
666
717
 
718
+ ::LLVM::Int1 = const_get(:Int1) #: LLVM::IntType
719
+ ::LLVM::Int8 = const_get(:Int8) #: LLVM::IntType
720
+ ::LLVM::Int16 = const_get(:Int16) #: LLVM::IntType
721
+ ::LLVM::Int32 = const_get(:Int32) #: LLVM::IntType
722
+ ::LLVM::Int64 = const_get(:Int64) #: LLVM::IntType
723
+
667
724
  # Native integer type
668
725
  bits = FFI.type_size(:int) * 8
669
726
  ::LLVM::Int = const_get(:"Int#{bits}")
@@ -687,34 +744,40 @@ module LLVM
687
744
  class ConstantReal < Constant
688
745
  # Negation.
689
746
  # fneg
747
+ #: -> ConstantReal
690
748
  def -@
691
749
  type.from_f(-to_f)
692
750
  end
693
751
 
694
752
  # Returns the result of adding this ConstantReal to rhs.
753
+ #: (ConstantReal) -> ConstantReal
695
754
  def +(rhs)
696
755
  type = LLVM::RealType.fits([self.type, rhs.type])
697
756
  type.from_f(to_f + rhs.to_f)
698
757
  end
699
758
 
759
+ #: (ConstantReal) -> ConstantReal
700
760
  def -(rhs)
701
761
  type = LLVM::RealType.fits([self.type, rhs.type])
702
762
  type.from_f(to_f - rhs.to_f)
703
763
  end
704
764
 
705
765
  # Returns the result of multiplying this ConstantReal by rhs.
766
+ #: (ConstantReal) -> ConstantReal
706
767
  def *(rhs)
707
768
  type = LLVM::RealType.fits([self.type, rhs.type])
708
769
  type.from_f(to_f * rhs.to_f)
709
770
  end
710
771
 
711
772
  # Returns the result of dividing this ConstantReal by rhs.
773
+ #: (ConstantReal) -> ConstantReal
712
774
  def /(rhs)
713
775
  type = LLVM::RealType.fits([self.type, rhs.type])
714
776
  type.from_f(to_f / rhs.to_f)
715
777
  end
716
778
 
717
779
  # Remainder.
780
+ #: (ConstantReal) -> ConstantReal
718
781
  def rem(rhs)
719
782
  type = LLVM::RealType.fits([self.type, rhs.type])
720
783
  type.from_f(to_f.divmod(rhs.to_f).last)
@@ -763,10 +826,11 @@ module LLVM
763
826
  end
764
827
 
765
828
  # get double from value
829
+ #: -> ::Float
766
830
  def to_f
767
- double = nil
831
+ double = 0.0 #: ::Float
768
832
  FFI::MemoryPointer.new(:bool, 1) do |loses_info|
769
- double = C.const_real_get_double(self, loses_info)
833
+ double = C.const_real_get_double(self, loses_info) #: ::Float
770
834
  end
771
835
  double
772
836
  end
@@ -775,18 +839,18 @@ module LLVM
775
839
  class ConstantStruct < Constant
776
840
  # ConstantStruct.const(size) {|i| ... } or
777
841
  # ConstantStruct.const([...])
778
- def self.const(size_or_values, packed = false, &block)
779
- vals = LLVM::Support.allocate_pointers(size_or_values, &block)
842
+ def self.const(size_or_values, packed = false, &)
843
+ vals = LLVM::Support.allocate_pointers(size_or_values, &)
780
844
  from_ptr C.const_struct(vals, vals.size / vals.type_size, packed ? 1 : 0)
781
845
  end
782
846
 
783
- def self.named_const(type, size_or_values, &block)
784
- vals = LLVM::Support.allocate_pointers(size_or_values, &block)
847
+ def self.named_const(type, size_or_values, &)
848
+ vals = LLVM::Support.allocate_pointers(size_or_values, &)
785
849
  from_ptr C.const_named_struct(type, vals, vals.size / vals.type_size)
786
850
  end
787
851
 
788
852
  def [](idx)
789
- self.class.from_ptr(C.get_aggregate_element(self, idx))
853
+ Value.from_ptr_kind(C.get_aggregate_element(self, idx))
790
854
  end
791
855
  end
792
856
 
@@ -795,8 +859,8 @@ module LLVM
795
859
  from_ptr(C.const_all_ones(type))
796
860
  end
797
861
 
798
- def self.const(size_or_values, &block)
799
- vals = LLVM::Support.allocate_pointers(size_or_values, &block)
862
+ def self.const(size_or_values, &)
863
+ vals = LLVM::Support.allocate_pointers(size_or_values, &)
800
864
 
801
865
  # size 0, or empty values
802
866
  # this will segfault in const_vector
@@ -818,6 +882,7 @@ module LLVM
818
882
  end
819
883
 
820
884
  class GlobalValue < Constant
885
+ #: -> bool
821
886
  def declaration?
822
887
  C.is_declaration(self)
823
888
  end
@@ -854,6 +919,7 @@ module LLVM
854
919
  C.set_alignment(self, bytes)
855
920
  end
856
921
 
922
+ #: -> bool
857
923
  def unnamed_addr?
858
924
  C.has_unnamed_addr(self) != 0
859
925
  end
@@ -893,20 +959,24 @@ module LLVM
893
959
  # Set personality function of function
894
960
  # @note Experimental and unsupported
895
961
  # @return self
962
+ #: (Value) -> void
896
963
  def personality_function=(personality_function)
897
964
  C.set_personality_fn(self, personality_function)
898
965
  end
899
966
 
900
967
  # Returns an Enumerable of the BasicBlocks in this function.
968
+ #: -> BasicBlockCollection
901
969
  def basic_blocks
902
970
  @basic_block_collection ||= BasicBlockCollection.new(self)
903
971
  end
904
972
 
973
+ #: -> Type?
905
974
  def function_type
906
- Type.from_ptr(C.get_element_type(self), :function)
975
+ Type.from_ptr(C.get_element_type(self), kind: :function)
907
976
  end
908
977
 
909
978
  # In LLVM 15, not overriding this yields a pointer type instead of a function type
979
+ #: -> Type?
910
980
  def type
911
981
  function_type
912
982
  end
@@ -929,30 +999,37 @@ module LLVM
929
999
  function_attributes.count
930
1000
  end
931
1001
 
1002
+ # -> [Attribute]
932
1003
  def attributes
933
1004
  function_attributes.to_a
934
1005
  end
935
1006
 
1007
+ #: -> bool
936
1008
  def readnone?
937
1009
  attributes.detect(&:readnone?)
938
1010
  end
939
1011
 
1012
+ #: -> bool
940
1013
  def readonly?
941
1014
  attributes.detect(&:readonly?)
942
1015
  end
943
1016
 
1017
+ #: -> bool
944
1018
  def writeonly?
945
1019
  attributes.detect(&:writeonly?)
946
1020
  end
947
1021
 
1022
+ # -> AttributeCollection
948
1023
  def function_attributes
949
1024
  AttributeCollection.new(self, -1)
950
1025
  end
951
1026
 
1027
+ # -> AttributeCollection
952
1028
  def return_attributes
953
1029
  AttributeCollection.new(self, 0)
954
1030
  end
955
1031
 
1032
+ # -> AttributeCollection
956
1033
  def param_attributes(index)
957
1034
  AttributeCollection.new(self, index)
958
1035
  end
@@ -975,6 +1052,8 @@ module LLVM
975
1052
  else
976
1053
  attr_kind_id
977
1054
  end
1055
+ when Integer
1056
+ C.create_enum_attribute(ctx, attr, 0)
978
1057
  else
979
1058
  raise "Adding unknown attribute type: #{attr.inspect}"
980
1059
  end
@@ -990,8 +1069,9 @@ module LLVM
990
1069
  C.get_attribute_count_at_index(@fun, @index)
991
1070
  end
992
1071
 
1072
+ # -> Array[Attribute]
993
1073
  def to_a
994
- attr_refs = nil
1074
+ attr_refs = [] #: Array[Attribute]
995
1075
  n = count
996
1076
  FFI::MemoryPointer.new(:pointer, n) do |p|
997
1077
  C.get_attributes_at_index(@fun, @index, p)
@@ -1003,6 +1083,7 @@ module LLVM
1003
1083
 
1004
1084
  private
1005
1085
 
1086
+ # (Symbol | String) -> String
1006
1087
  def attribute_name(attr_name)
1007
1088
  attr_name = attr_name.to_s
1008
1089
  if /_attribute$/.match?(attr_name)
@@ -1012,6 +1093,7 @@ module LLVM
1012
1093
  end
1013
1094
  end
1014
1095
 
1096
+ # (Symbol | String) -> Integer
1015
1097
  def attribute_id(attr_name)
1016
1098
  upgrade = upgrade_attr(attr_name)
1017
1099
  return upgrade if upgrade
@@ -1025,6 +1107,7 @@ module LLVM
1025
1107
 
1026
1108
  # Upgrade attributes from before LLVM 16
1027
1109
  # readnone, readonly, writeonly
1110
+ #: (Symbol | String) -> LLVM::Attribute?
1028
1111
  def upgrade_attr(attr)
1029
1112
  case attr.to_sym
1030
1113
  when :readnone
@@ -1035,6 +1118,8 @@ module LLVM
1035
1118
  LLVM::Attribute.memory(memory: :write)
1036
1119
  when :inaccessiblememonly
1037
1120
  LLVM::Attribute.memory(inaccessiblemem: :readwrite)
1121
+ when :no_capture_attribute
1122
+ LLVM::Attribute.captures_none
1038
1123
  end
1039
1124
  end
1040
1125
  end
@@ -1048,16 +1133,17 @@ module LLVM
1048
1133
  end
1049
1134
 
1050
1135
  # Returns the number of BasicBlocks in the collection.
1136
+ #: -> Integer
1051
1137
  def size
1052
1138
  C.count_basic_blocks(@fun)
1053
1139
  end
1054
1140
 
1055
1141
  # Iterates through each BasicBlock in the collection.
1056
- def each
1142
+ def each(&)
1057
1143
  return to_enum :each unless block_given?
1058
1144
 
1059
1145
  ptr = C.get_first_basic_block(@fun)
1060
- 0.upto(size - 1) do |i|
1146
+ size.times do
1061
1147
  yield BasicBlock.from_ptr(ptr)
1062
1148
  ptr = C.get_next_basic_block(ptr)
1063
1149
  end
@@ -1066,23 +1152,27 @@ module LLVM
1066
1152
  end
1067
1153
 
1068
1154
  # Adds a BasicBlock with the given name to the end of the collection.
1155
+ #: (?String) -> BasicBlock
1069
1156
  def append(name = "")
1070
1157
  BasicBlock.create(@fun, name)
1071
1158
  end
1072
1159
 
1073
1160
  # Returns the entry BasicBlock in the collection. This is the block the
1074
1161
  # function starts on.
1162
+ #: -> BasicBlock
1075
1163
  def entry
1076
1164
  BasicBlock.from_ptr(C.get_entry_basic_block(@fun))
1077
1165
  end
1078
1166
 
1079
1167
  # Returns the first BasicBlock in the collection.
1168
+ #: -> BasicBlock?
1080
1169
  def first
1081
1170
  ptr = C.get_first_basic_block(@fun)
1082
1171
  BasicBlock.from_ptr(ptr) unless ptr.null?
1083
1172
  end
1084
1173
 
1085
1174
  # Returns the last BasicBlock in the collection.
1175
+ #: -> BasicBlock?
1086
1176
  def last
1087
1177
  ptr = C.get_last_basic_block(@fun)
1088
1178
  BasicBlock.from_ptr(ptr) unless ptr.null?
@@ -1090,6 +1180,7 @@ module LLVM
1090
1180
  end
1091
1181
 
1092
1182
  # Returns an Enumerable of the parameters in the function.
1183
+ #: -> ParameterCollection
1093
1184
  def params
1094
1185
  @parameter_collection ||= ParameterCollection.new(self)
1095
1186
  end
@@ -1101,6 +1192,7 @@ module LLVM
1101
1192
  end
1102
1193
 
1103
1194
  # Returns a Value representation of the parameter at the given index.
1195
+ # (Integer) -> Value?
1104
1196
  def [](i)
1105
1197
  sz = size
1106
1198
  i = sz + i if i.negative?
@@ -1109,6 +1201,7 @@ module LLVM
1109
1201
  end
1110
1202
 
1111
1203
  # Returns the number of paramters in the collection.
1204
+ #: -> Integer
1112
1205
  def size
1113
1206
  C.count_params(@fun)
1114
1207
  end
@@ -1116,7 +1209,7 @@ module LLVM
1116
1209
  include Enumerable
1117
1210
 
1118
1211
  # Iteraters through each parameter in the collection.
1119
- def each
1212
+ def each(&)
1120
1213
  return to_enum :each unless block_given?
1121
1214
  0.upto(size - 1) { |i| yield self[i] }
1122
1215
  self
@@ -1131,6 +1224,7 @@ module LLVM
1131
1224
  C.get_gc(self)
1132
1225
  end
1133
1226
 
1227
+ #: -> String
1134
1228
  def inspect
1135
1229
  {
1136
1230
  signature: to_s.lines[attribute_count.zero? ? 0 : 1],
@@ -1157,6 +1251,7 @@ module LLVM
1157
1251
  C.set_initializer(self, val)
1158
1252
  end
1159
1253
 
1254
+ #: -> bool
1160
1255
  def thread_local?
1161
1256
  C.is_thread_local(self)
1162
1257
  end
@@ -1165,12 +1260,13 @@ module LLVM
1165
1260
  C.set_thread_local(self, local ? 1 : 0)
1166
1261
  end
1167
1262
 
1263
+ #: -> bool
1168
1264
  def global_constant?
1169
1265
  C.is_global_constant(self)
1170
1266
  end
1171
1267
 
1172
1268
  def global_constant=(flag)
1173
- if flag.kind_of?(Integer)
1269
+ if flag.is_a?(Integer)
1174
1270
  warn 'Warning: Passing Integer value to LLVM::GlobalValue#global_constant=(Boolean) is deprecated.'
1175
1271
  flag = !flag.zero?
1176
1272
  end
@@ -1178,6 +1274,7 @@ module LLVM
1178
1274
  C.set_global_constant(self, flag ? 1 : 0)
1179
1275
  end
1180
1276
 
1277
+ #: -> bool
1181
1278
  def externally_initialized?
1182
1279
  C.is_externally_initialized(self)
1183
1280
  end
@@ -1190,57 +1287,68 @@ module LLVM
1190
1287
  class Instruction < User
1191
1288
  # Create Instruction from pointer to value
1192
1289
  # many places where this is called, the instruction may be a constant, so create those instead
1290
+ #: (FFI::Pointer) -> (Value | Instruction)
1193
1291
  def self.from_ptr(ptr)
1194
1292
  kind = C.get_value_kind(ptr)
1195
1293
  kind == :instruction ? super : LLVM::Value.from_ptr_kind(ptr)
1196
1294
  end
1197
1295
 
1198
1296
  # Returns the parent of the instruction (a BasicBlock).
1297
+ #: -> LLVM::BasicBlock?
1199
1298
  def parent
1200
1299
  ptr = C.get_instruction_parent(self)
1201
1300
  LLVM::BasicBlock.from_ptr(ptr) unless ptr.null?
1202
1301
  end
1203
1302
 
1204
1303
  # Returns the next instruction after this one.
1304
+ #: -> LLVM::Instruction?
1205
1305
  def next
1206
1306
  ptr = C.get_next_instruction(self)
1207
- LLVM::Instruction.from_ptr(ptr) unless ptr.null?
1307
+ LLVM::Instruction.from_ptr(ptr) unless ptr.null? #: as LLVM::Instruction?
1208
1308
  end
1209
1309
 
1210
1310
  # Returns the previous instruction before this one.
1311
+ #: -> LLVM::Instruction?
1211
1312
  def previous
1212
1313
  ptr = C.get_previous_instruction(self)
1213
- LLVM::Instruction.from_ptr(ptr) unless ptr.null?
1314
+ LLVM::Instruction.from_ptr(ptr) unless ptr.null? #: as LLVM::Instruction?
1214
1315
  end
1215
1316
 
1317
+ #: -> Integer
1216
1318
  def opcode
1217
1319
  C.get_instruction_opcode(self)
1218
1320
  end
1219
1321
 
1322
+ #: -> String
1220
1323
  def inspect
1221
1324
  { self.class.name => { opcode: opcode, ptr: @ptr } }.to_s
1222
1325
  end
1223
1326
 
1327
+ #: -> self
1224
1328
  def nsw!
1225
1329
  C.set_nsw(self, true)
1226
1330
  self
1227
1331
  end
1228
1332
 
1333
+ #: -> self
1229
1334
  def clear_nsw!
1230
1335
  C.set_nsw(self, false)
1231
1336
  self
1232
1337
  end
1233
1338
 
1339
+ #: -> self
1234
1340
  def nuw!
1235
1341
  C.set_nuw(self, true)
1236
1342
  self
1237
1343
  end
1238
1344
 
1345
+ #: -> self
1239
1346
  def clear_nuw!
1240
1347
  C.set_nuw(self, false)
1241
1348
  self
1242
1349
  end
1243
1350
 
1351
+ #: -> self
1244
1352
  def exact!
1245
1353
  C.set_exact(self, true)
1246
1354
  self
@@ -1257,6 +1365,7 @@ module LLVM
1257
1365
 
1258
1366
  class CallInst < Instruction
1259
1367
  # Sets the call convention to conv.
1368
+ #: (Symbol) -> void
1260
1369
  def call_conv=(conv)
1261
1370
  C.set_instruction_call_conv(self, conv)
1262
1371
  end
@@ -1270,6 +1379,9 @@ module LLVM
1270
1379
  class InvokeInst < CallInst
1271
1380
  end
1272
1381
 
1382
+ class Alloca < Instruction
1383
+ end
1384
+
1273
1385
  # @private
1274
1386
  class Phi < Instruction
1275
1387
  # Add incoming branches to a phi node by passing an alternating list of
@@ -1296,6 +1408,7 @@ module LLVM
1296
1408
  class SwitchInst < Instruction
1297
1409
  # Adds a case to a switch instruction. First the value to match on, then
1298
1410
  # the basic block.
1411
+ #: (Value, BasicBlock) -> void
1299
1412
  def add_case(val, block)
1300
1413
  C.add_case(self, val, block)
1301
1414
  end
@@ -1304,6 +1417,7 @@ module LLVM
1304
1417
  # @private
1305
1418
  class IndirectBr < Instruction
1306
1419
  # Adds a basic block reference as a destination for this indirect branch.
1420
+ #: (BasicBlock) -> void
1307
1421
  def add_dest(dest)
1308
1422
  C.add_destination(self, dest)
1309
1423
  end