ruby-llvm 21.1.0 → 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cbf4a61de5472ea85477b7b0c26d36a9d29512e43a8d1fc5c67babd7ec7ee9d5
4
- data.tar.gz: 031f350a31ae2c6202cb765fe1ac01345796ef5c8dfea1972c0048c857d19db3
3
+ metadata.gz: a6062a6e6b85cdcad5129b1c41205736606b7ed8067e93295784cbb5767c7c9c
4
+ data.tar.gz: affccd431e0047aed850a6af6c21420a766235972dc38deb6442174a9b5900b1
5
5
  SHA512:
6
- metadata.gz: a01da90f6dbe9f65826526a0cc8ca2e428b9e47bc3a4393824d13ae7683fa8b21971ace1aedfedc9f0db92d658e346ff75ca252821c29614355ee0d8aea67ae0
7
- data.tar.gz: f228a01b0495163e264410b1f379d60f4dfdd0d4e4716597a50228581371105b5222bbfffa12fdc643bf78d060870fd0f9a3256138f1057fe7b000f534fe5d09
6
+ metadata.gz: ffc8253d118f6f703ba59024ac2e2b5d39eb487095cd242b0d8ecc88567247cef302992874f2bba1d204401e52f3d43a99e671a77985562859510314a6c05f33
7
+ data.tar.gz: 5d3ac41a9c8a1193baf8b5bbdb54360854007d72d480e6a20c23b758fb873fd4db273e99d92e55ba981eac8dcbb24eb370efd4942505ea1bcfc5d1632d8ad7ac
data/README.md CHANGED
@@ -14,7 +14,7 @@ computationally intensive algorithms on the fly.
14
14
  Current version
15
15
  ---------------
16
16
 
17
- This library currently binds to LLVM-18 (specifically llvm-c 18.
17
+ This library currently binds to LLVM 22.1.x (specifically llvm-c 22).
18
18
 
19
19
  About version numbers
20
20
  ---------------------
@@ -32,7 +32,7 @@ Debian/Ubuntu
32
32
  Homebrew
33
33
  --------
34
34
 
35
- LLVM can be installed with Homebrew by executing `brew install llvm --shared`
35
+ LLVM can be installed with Homebrew by executing `brew install llvm@22`
36
36
 
37
37
  Source and other binaries
38
38
  -------------------------
@@ -26,8 +26,8 @@ end
26
26
 
27
27
  def find_llvm_config
28
28
  check_for('llvm-config',
29
- %W[llvm-config-#{LLVM_VERSION} llvm-config],
30
- 'LLVM_CONFIG') do |llvm_config|
29
+ %W[llvm-config-#{LLVM_VERSION} llvm-config],
30
+ 'LLVM_CONFIG') do |llvm_config|
31
31
  begin
32
32
  actual_version = `#{llvm_config} --version`.strip
33
33
  actual_maj, actual_min, _ = actual_version.split('.')
@@ -44,7 +44,6 @@ end
44
44
  def find_cxx
45
45
  check_for('C++ compiler', %W[clang++-#{LLVM_VERSION} clang++ g++], 'CXX') do |cxx|
46
46
  system(cxx, '--version', out: File::NULL, err: File::NULL)
47
- $?.success?
48
47
  end
49
48
  end
50
49
 
@@ -69,6 +68,7 @@ end
69
68
 
70
69
  SUPPORT_LIB = FFI.map_library_name("RubyLLVMSupport-#{LLVM_VERSION}")
71
70
  CONFIG_MOD = File.expand_path('../../lib/llvm/config.rb', File.dirname(__FILE__))
71
+ VERSION_RB = File.expand_path('../../lib/llvm/version.rb', File.dirname(__FILE__))
72
72
 
73
73
  CLEAN.include(SUPPORT_LIB, CONFIG_MOD)
74
74
 
@@ -92,7 +92,8 @@ LLVM_CONFIG_OPTS = [
92
92
  ['LIBS', :string, '--libs'],
93
93
  ].freeze
94
94
 
95
- file CONFIG_MOD do
95
+ # regenerate when version.rb changes, so a version bump does not leave a stale config
96
+ file CONFIG_MOD => [VERSION_RB] do
96
97
  open(CONFIG_MOD, 'w') do |f|
97
98
  f.puts '# Generated by ruby-llvm. Please do not change this file by hand.'
98
99
  f.puts 'module LLVM'
@@ -3,7 +3,23 @@
3
3
  */
4
4
 
5
5
  #include <llvm/Support/TargetSelect.h>
6
+ #include <llvm/Support/raw_ostream.h>
6
7
  #include <llvm/IR/Attributes.h>
8
+ #include <llvm/IR/Module.h>
9
+ #include <llvm/IR/Value.h>
10
+ #include <llvm/IR/Type.h>
11
+ #include <llvm-c/Core.h>
12
+ #include <string>
13
+
14
+ // Allocates a caller-owned copy of s, to be released with LLVMDisposeMessage.
15
+ //
16
+ // Uses LLVMCreateMessage rather than strdup so the allocation happens inside
17
+ // LLVM's own module, pairing with the free() that LLVMDisposeMessage performs
18
+ // there. Every string this file hands back is declared `char *` (not `const
19
+ // char *`) and goes through here, so ownership is stated in one place.
20
+ static char* owned_message(const std::string &s) {
21
+ return LLVMCreateMessage(s.c_str());
22
+ }
7
23
 
8
24
  extern "C" {
9
25
  void LLVMInitializeAllTargetInfos() {
@@ -40,10 +56,65 @@ extern "C" {
40
56
 
41
57
  // std::string Attribute::getAsString(bool InAttrGrp = false) const
42
58
  // https://llvm.org/doxygen/classllvm_1_1Attribute.html
43
- // string must be disposed with LLVMDisposeMessage
44
- const char* LLVMGetAttributeAsString(LLVMAttributeRef A) {
45
- auto S = llvm::unwrap(A).getAsString();
46
- return strdup(S.c_str());
59
+ char* LLVMGetAttributeAsString(LLVMAttributeRef A) {
60
+ return owned_message(llvm::unwrap(A).getAsString());
61
+ }
62
+
63
+ // void Type::print(raw_ostream &O, bool IsForDebug = false,
64
+ // bool NoDetails = false) const
65
+ // https://llvm.org/doxygen/classllvm_1_1Type.html
66
+ //
67
+ // As LLVMPrintTypeToString, but with IsForDebug=true, matching what
68
+ // LLVMDumpType writes to stderr. As of LLVM 21 this renders identically to
69
+ // IsForDebug=false for every type kind; it exists so the API matches Module
70
+ // and Value, and so a future divergence is picked up rather than missed.
71
+ // String must be disposed with LLVMDisposeMessage.
72
+ char* LLVMPrintTypeToStringDebug(LLVMTypeRef Ty) {
73
+ std::string buf;
74
+ llvm::raw_string_ostream os(buf);
75
+ if (llvm::unwrap(Ty))
76
+ llvm::unwrap(Ty)->print(os, /*IsForDebug=*/true);
77
+ else
78
+ os << "Printing <null> Type";
79
+ os.flush();
80
+ return owned_message(buf);
81
+ }
82
+
83
+ // void Value::print(raw_ostream &O, bool IsForDebug = false) const
84
+ // https://llvm.org/doxygen/classllvm_1_1Value.html
85
+ //
86
+ // As LLVMPrintValueToString, but with IsForDebug=true, matching what
87
+ // LLVMDumpValue writes to stderr. Only affects function declarations, whose
88
+ // parameters LLVMPrintValueToString omits.
89
+ // String must be disposed with LLVMDisposeMessage.
90
+ char* LLVMPrintValueToStringDebug(LLVMValueRef V) {
91
+ std::string buf;
92
+ llvm::raw_string_ostream os(buf);
93
+ if (llvm::unwrap(V))
94
+ llvm::unwrap(V)->print(os, /*IsForDebug=*/true);
95
+ else
96
+ os << "Printing <null> Value";
97
+ os.flush();
98
+ return owned_message(buf);
99
+ }
100
+
101
+ // void Module::print(raw_ostream &OS, AssemblyAnnotationWriter *AAW,
102
+ // bool ShouldPreserveUseListOrder = false,
103
+ // bool IsForDebug = false) const
104
+ // https://llvm.org/doxygen/classllvm_1_1Module.html
105
+ //
106
+ // As LLVMPrintModuleToString, but with IsForDebug=true, matching what
107
+ // LLVMDumpModule writes to stderr. Only affects declarations, whose
108
+ // parameters LLVMPrintModuleToString omits.
109
+ // String must be disposed with LLVMDisposeMessage.
110
+ char* LLVMPrintModuleToStringDebug(LLVMModuleRef M) {
111
+ std::string buf;
112
+ llvm::raw_string_ostream os(buf);
113
+ llvm::unwrap(M)->print(os, nullptr,
114
+ /*ShouldPreserveUseListOrder=*/false,
115
+ /*IsForDebug=*/true);
116
+ os.flush();
117
+ return owned_message(buf);
47
118
  }
48
119
  }
49
120
 
@@ -1,11 +1,9 @@
1
1
  # Generated by ffi_gen. Please do not change this file by hand.
2
2
 
3
3
  require 'ffi'
4
+ require 'llvm/core_ffi'
4
5
 
5
6
  module LLVM::C
6
- extend FFI::Library
7
-
8
- ffi_lib ["LLVM-21", "libLLVM-21.so.1", "libLLVM.so.21", "libLLVM.so.21.1"]
9
7
  # @defgroup LLVMCAnalysis Analysis
10
8
  # @ingroup LLVMC
11
9
  #
@@ -34,7 +34,7 @@ module LLVM
34
34
  end
35
35
 
36
36
  # create enum attribute with optional value and context
37
- #: (untyped, ?Integer, ?Context) -> Attribute?
37
+ #: (String | Symbol, ?Integer, ?Context) -> Attribute?
38
38
  def enum(kind, value = 0, context = Context.global)
39
39
  attr_id = attribute_id(kind)
40
40
  ptr = C.create_enum_attribute(context, attr_id, value)
@@ -42,7 +42,7 @@ module LLVM
42
42
  end
43
43
 
44
44
  # create string attribute with key and value
45
- #: (untyped, untyped, ?Context) -> Attribute?
45
+ #: (String | Symbol, untyped, ?Context) -> Attribute?
46
46
  def string(key, value, context = Context.global)
47
47
  key = key.to_s
48
48
  value = value.to_s
@@ -34,7 +34,7 @@ module LLVM
34
34
  C.write_bitcode_to_file(self, path_or_io.path)
35
35
  elsif path_or_io.respond_to?(:fileno)
36
36
  C.write_bitcode_to_fd(self, path_or_io.fileno, 0, 1)
37
- elsif path_or_io.kind_of?(Integer)
37
+ elsif path_or_io.is_a?(Integer)
38
38
  C.write_bitcode_to_fd(self, path_or_io, 0, 1)
39
39
  else
40
40
  C.write_bitcode_to_file(self, path_or_io.to_str)
@@ -1,11 +1,9 @@
1
1
  # Generated by ffi_gen. Please do not change this file by hand.
2
2
 
3
3
  require 'ffi'
4
+ require 'llvm/core_ffi'
4
5
 
5
6
  module LLVM::C
6
- extend FFI::Library
7
-
8
- ffi_lib ["LLVM-21", "libLLVM-21.so.1", "libLLVM.so.21", "libLLVM.so.21.1"]
9
7
  # (Not documented)
10
8
  #
11
9
  # @method parse_bitcode(mem_buf, out_module, out_message)
@@ -125,7 +125,8 @@ module LLVM
125
125
  #: (BasicBlock, Integer) -> Value
126
126
  def ibr(addr, num_dests)
127
127
  IndirectBr.from_ptr(
128
- C.build_indirect_br(self, addr, num_dests))
128
+ C.build_indirect_br(self, addr, num_dests)
129
+ )
129
130
  end
130
131
 
131
132
  # Conditional branching (i.e. if)
@@ -650,7 +651,7 @@ module LLVM
650
651
  # @LLVMinst store
651
652
  #: (Value, Value) -> Value
652
653
  def store(val, ptr)
653
- raise "val must be a Value, got #{val.class.name}" unless Value === val
654
+ raise "val must be a Value, got #{val.class.name}" unless val.is_a?(Value)
654
655
  Instruction.from_ptr(C.build_store(self, val, ptr))
655
656
  end
656
657
 
@@ -1093,7 +1094,7 @@ module LLVM
1093
1094
  def call2(type, fun, *args)
1094
1095
  type, fun = call2_infer_function_and_type(type, fun)
1095
1096
 
1096
- name = if args.last.kind_of? String
1097
+ name = if args.last.is_a? String
1097
1098
  args.pop
1098
1099
  else
1099
1100
  ""
@@ -1267,13 +1268,13 @@ module LLVM
1267
1268
 
1268
1269
  #: (untyped) -> void
1269
1270
  def must_be_value!(value)
1270
- raise ArgumentError, "must be a Value, got #{value.class.name}" unless Value === value
1271
+ raise ArgumentError, "must be a Value, got #{value.class.name}" unless value.is_a?(Value)
1271
1272
  end
1272
1273
 
1273
1274
  #: (untyped) -> void
1274
1275
  def must_be_type!(type)
1275
1276
  type2 = LLVM.Type(type)
1276
- raise ArgumentError, "must be a Type (LLVMTypeRef), got #{type2.class.name}" unless Type === type2
1277
+ raise ArgumentError, "must be a Type (LLVMTypeRef), got #{type2.class.name}" unless type2.is_a?(Type)
1277
1278
  end
1278
1279
 
1279
1280
  #: (untyped) -> void
@@ -17,7 +17,7 @@ module LLVM
17
17
  # Obtains a reference to the global Context.
18
18
  #: -> Context
19
19
  def self.global
20
- new(C.get_global_context())
20
+ new(C.get_global_context)
21
21
  end
22
22
 
23
23
  # Diposes the Context.
@@ -6,6 +6,7 @@ module LLVM
6
6
  include PointerIdentity
7
7
 
8
8
  # @private
9
+ #: (FFI::Pointer) -> LLVM::Module?
9
10
  def self.from_ptr(ptr)
10
11
  return if ptr.null?
11
12
  mod = allocate
@@ -25,7 +26,7 @@ module LLVM
25
26
  @ptr = nil
26
27
  end
27
28
 
28
- #: -> LLVM::Module
29
+ #: -> LLVM::Module?
29
30
  def clone_module
30
31
  Module.from_ptr(C.clone_module(self))
31
32
  end
@@ -85,7 +86,7 @@ module LLVM
85
86
  end
86
87
 
87
88
  # Returns the Type with the given name (symbol or string).
88
- #: (untyped) -> Type
89
+ #: (String | Symbol) -> Type
89
90
  def named(name)
90
91
  Type.from_ptr(C.get_type_by_name(@module, name.to_s))
91
92
  end
@@ -107,40 +108,46 @@ module LLVM
107
108
  end
108
109
 
109
110
  # Adds a GlobalVariable with the given type and name to the collection (symbol or string).
110
- def add(ty, name)
111
+ #: (untyped, String | Symbol) ?{ (GlobalVariable) -> void } -> GlobalVariable
112
+ def add(ty, name, &)
111
113
  GlobalVariable.from_ptr(C.add_global(@module, LLVM::Type(ty), name.to_s)).tap do |gvar|
112
114
  yield gvar if block_given?
113
115
  end
114
116
  end
115
117
 
116
118
  # Returns the GlobalVariable with the given name (symbol or string).
117
- #: (untyped) -> GlobalVariable
119
+ #: (String | Symbol) -> GlobalVariable?
118
120
  def named(name)
119
- GlobalVariable.from_ptr(C.get_named_global(@module, name.to_s))
121
+ ptr = C.get_named_global(@module, name.to_s)
122
+ GlobalVariable.from_ptr(ptr) unless ptr.null?
120
123
  end
121
124
 
122
125
  # Returns the first GlobalVariable in the collection.
123
- #: -> GlobalVariable
126
+ #: -> GlobalVariable?
124
127
  def first
125
- GlobalVariable.from_ptr(C.get_first_global(@module))
128
+ ptr = C.get_first_global(@module)
129
+ GlobalVariable.from_ptr(ptr) unless ptr.null?
126
130
  end
127
131
 
128
132
  # Returns the last GlobalVariable in the collection.
129
- #: -> GlobalVariable
133
+ #: -> GlobalVariable?
130
134
  def last
131
- GlobalVariable.from_ptr(C.get_last_global(@module))
135
+ ptr = C.get_last_global(@module)
136
+ GlobalVariable.from_ptr(ptr) unless ptr.null?
132
137
  end
133
138
 
134
139
  # Returns the next GlobalVariable in the collection after global.
135
- #: (GlobalVariable) -> GlobalVariable
140
+ #: (GlobalVariable) -> GlobalVariable?
136
141
  def next(global)
137
- GlobalVariable.from_ptr(C.get_next_global(global))
142
+ ptr = C.get_next_global(global)
143
+ GlobalVariable.from_ptr(ptr) unless ptr.null?
138
144
  end
139
145
 
140
146
  # Returns the previous GlobalVariable in the collection before global.
141
- #: (GlobalVariable) -> GlobalVariable
147
+ #: (GlobalVariable) -> GlobalVariable?
142
148
  def previous(global)
143
- GlobalVariable.from_ptr(C.get_previous_global(global))
149
+ ptr = C.get_previous_global(global)
150
+ GlobalVariable.from_ptr(ptr) unless ptr.null?
144
151
  end
145
152
 
146
153
  # Deletes the GlobalVariable from the collection.
@@ -150,12 +157,13 @@ module LLVM
150
157
  end
151
158
 
152
159
  # Returns the GlobalVariable with a name equal to key (symbol or string) or at key (integer).
160
+ #: (String | Symbol | Integer) -> GlobalVariable?
153
161
  def [](key)
154
162
  case key
155
163
  when String, Symbol then named(key)
156
- when Integer then
164
+ when Integer
157
165
  i = 0
158
- g = first
166
+ g = first #: GlobalVariable?
159
167
  until i >= key || g.nil?
160
168
  g = self.next(g)
161
169
  i += 1
@@ -165,8 +173,10 @@ module LLVM
165
173
  end
166
174
 
167
175
  # Iterates through each GlobalVariable in the collection.
176
+ # @override
177
+ #: { (GlobalVariable) -> void } -> void
168
178
  def each(&)
169
- g = first
179
+ g = first #: GlobalVariable?
170
180
  until g.nil?
171
181
  yield g
172
182
  g = self.next(g)
@@ -189,10 +199,10 @@ module LLVM
189
199
 
190
200
  # Adds a Function with the given name (symbol or string) and args (Types).
191
201
  def add(name, *args)
192
- if args.first.kind_of? FunctionType
193
- type = args.first
202
+ type = if args.first.is_a? FunctionType
203
+ args.first
194
204
  else
195
- type = Type.function(
205
+ Type.function(
196
206
  *args #: as untyped
197
207
  )
198
208
  end
@@ -207,28 +217,38 @@ module LLVM
207
217
  end
208
218
 
209
219
  # Returns the Function with the given name (symbol or string).
220
+ #: (String | Symbol) -> Function?
210
221
  def named(name)
211
- Function.from_ptr(C.get_named_function(@module, name.to_s))
222
+ ptr = C.get_named_function(@module, name.to_s)
223
+ Function.from_ptr(ptr) unless ptr.null?
212
224
  end
213
225
 
214
226
  # Returns the first Function in the collection.
227
+ #: -> Function?
215
228
  def first
216
- Function.from_ptr(C.get_first_function(@module))
229
+ ptr = C.get_first_function(@module)
230
+ Function.from_ptr(ptr) unless ptr.null?
217
231
  end
218
232
 
219
233
  # Returns the last Function in the collection.
234
+ #: -> Function?
220
235
  def last
221
- Function.from_ptr(C.get_last_function(@module))
236
+ ptr = C.get_last_function(@module)
237
+ Function.from_ptr(ptr) unless ptr.null?
222
238
  end
223
239
 
224
240
  # Returns the next Function in the collection after function.
241
+ #: (Function) -> Function?
225
242
  def next(function)
226
- Function.from_ptr(C.get_next_function(function))
243
+ ptr = C.get_next_function(function)
244
+ Function.from_ptr(ptr) unless ptr.null?
227
245
  end
228
246
 
229
247
  # Returns the previous Function in the collection before function.
248
+ #: (Function) -> Function?
230
249
  def previous(function)
231
- Function.from_ptr(C.get_previous_function(function))
250
+ ptr = C.get_previous_function(function)
251
+ Function.from_ptr(ptr) unless ptr.null?
232
252
  end
233
253
 
234
254
  # Deletes the Function from the collection.
@@ -237,12 +257,13 @@ module LLVM
237
257
  end
238
258
 
239
259
  # Returns the Function with a name equal to key (symbol or string) or at key (integer).
260
+ #: (String | Symbol | Integer) -> Function?
240
261
  def [](key)
241
262
  case key
242
263
  when String, Symbol then named(key)
243
264
  when Integer
244
265
  i = 0
245
- f = first
266
+ f = first #: Function?
246
267
  until i >= key || f.nil?
247
268
  f = self.next(f)
248
269
  i += 1
@@ -252,8 +273,10 @@ module LLVM
252
273
  end
253
274
 
254
275
  # Iterates through each Function in the collection.
276
+ # @override
277
+ #: { (Function) -> void } -> void
255
278
  def each(&)
256
- f = first
279
+ f = first #: Function?
257
280
  until f.nil?
258
281
  yield f
259
282
  f = self.next(f)
@@ -262,13 +285,27 @@ module LLVM
262
285
  end
263
286
 
264
287
  # Returns the LLVM IR of the module as a string.
288
+ #: -> String
265
289
  def to_s
266
290
  C.print_module_to_string(self)
267
291
  end
268
292
 
293
+ # Returns the LLVM IR of the module as #dump renders it (LLVM's IsForDebug),
294
+ # which differs from #to_s only for declarations, whose parameters #to_s
295
+ # omits.
296
+ #: -> String
297
+ def debug_s
298
+ Support::C.print_module_to_string_debug(self)
299
+ end
300
+
269
301
  # Print the module's IR to the standard error.
302
+ #
303
+ # Equivalent to LLVMDumpModule, but written through Ruby's $stderr rather
304
+ # than LLVM's errs(), so reassigning $stderr captures it.
305
+ #: -> void
270
306
  def dump
271
- C.dump_module(self)
307
+ # not warn: warn is silenced by -W0, and an explicit dump must always print
308
+ $stderr.puts(debug_s) # rubocop:disable Style/StderrPuts
272
309
  end
273
310
  end
274
311
  end
@@ -6,29 +6,35 @@ module LLVM
6
6
  include PointerIdentity
7
7
 
8
8
  # @private
9
- #: (untyped, ?kind: Symbol?) -> Type
9
+ #: (FFI::Pointer, ?kind: Symbol?) -> Type
10
10
  def self.from_ptr(ptr, kind: nil)
11
11
  raise ArgumentError if ptr.null?
12
12
  kind ||= C.get_type_kind(ptr)
13
- ty = case kind
13
+ case kind
14
14
  when :integer
15
- IntType.allocate
15
+ IntType.new(ptr, kind)
16
16
  when :float, :double
17
- RealType.allocate
17
+ RealType.new(ptr, kind)
18
18
  when :function
19
- FunctionType.allocate
19
+ FunctionType.new(ptr, kind)
20
20
  when :struct
21
- StructType.allocate
21
+ StructType.new(ptr, kind)
22
22
  else
23
- allocate
23
+ new(ptr, kind)
24
24
  end
25
- ty.instance_variable_set(:@ptr, ptr)
26
- ty.instance_variable_set(:@kind, kind)
27
- 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
28
33
  end
29
34
 
30
35
  # Returns a symbol representation of the types kind (ex. :pointer, :vector, :array.)
31
- attr_reader :kind #: Symbol?
36
+ #: Symbol?
37
+ attr_reader :kind
32
38
 
33
39
  # Returns the size of the type.
34
40
  #: -> ConstantInt
@@ -86,12 +92,15 @@ module LLVM
86
92
  Type.pointer(self, address_space: address_space)
87
93
  end
88
94
 
89
- # 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.
90
100
  #: -> void
91
101
  def dump
92
- # :nocov:
93
- C.dump_type(self)
94
- # :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
95
104
  end
96
105
 
97
106
  # Build string of LLVM type representation.
@@ -100,6 +109,14 @@ module LLVM
100
109
  C.print_type_to_string(self)
101
110
  end
102
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
+
103
120
  #: -> bool
104
121
  def aggregate?
105
122
  [:struct, :array].include?(kind)
@@ -191,12 +208,12 @@ module LLVM
191
208
  end
192
209
  end
193
210
 
194
- #: (untyped) -> Type
211
+ #: (String | Symbol) -> Type
195
212
  def self.opaque_struct(name)
196
213
  from_ptr(C.struct_create_named(Context.global, name.to_s), kind: :struct)
197
214
  end
198
215
 
199
- #: (untyped) -> Type?
216
+ #: (String | Symbol) -> Type?
200
217
  def self.named(name)
201
218
  from_ptr(C.get_type_by_name2(Context.global, name.to_s))
202
219
  end
@@ -354,7 +371,7 @@ module LLVM
354
371
  #: -> Array[Type?]
355
372
  def argument_types
356
373
  size = C.count_param_types(self)
357
- result = [] #: Array[Type]
374
+ result = [] #: Array[FFI::Pointer]
358
375
  FFI::MemoryPointer.new(FFI.type_size(:pointer) * size) do |types_ptr|
359
376
  C.get_param_types(self, types_ptr)
360
377
  result = types_ptr.read_array_of_pointer(size)
@@ -417,7 +434,7 @@ module LLVM
417
434
  end
418
435
 
419
436
  # Shortcut to Type.pointer.
420
- #: (?Type?) -> Type`
437
+ #: (?Type?) -> Type
421
438
  def Pointer(ty = nil)
422
439
  LLVM::Type.pointer(ty)
423
440
  end
@@ -439,8 +456,6 @@ module LLVM
439
456
  def Struct(*elt_types)
440
457
  name = if elt_types.last.is_a? String
441
458
  elt_types.pop
442
- else
443
- nil
444
459
  end
445
460
  LLVM::Type.struct(
446
461
  elt_types, #: as [LLVM::Type]