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,22 +1,27 @@
1
1
  # frozen_string_literal: true
2
+ # typed: strict
2
3
 
3
4
  module LLVM
4
5
  class Context
6
+ #: (?FFI::Pointer?) -> void
5
7
  def initialize(ptr = nil)
6
- @ptr = ptr || C.context_create()
8
+ @ptr = ptr || C.context_create() #: FFI::Pointer?
7
9
  end
8
10
 
9
11
  # @private
12
+ #: -> FFI::Pointer?
10
13
  def to_ptr
11
14
  @ptr
12
15
  end
13
16
 
14
17
  # Obtains a reference to the global Context.
18
+ #: -> Context
15
19
  def self.global
16
- new(C.get_global_context())
20
+ new(C.get_global_context)
17
21
  end
18
22
 
19
23
  # Diposes the Context.
24
+ #: -> void
20
25
  def dispose
21
26
  return if @ptr.nil?
22
27
  C.context_dispose(@ptr)
@@ -1,10 +1,12 @@
1
1
  # frozen_string_literal: true
2
+ # typed: true
2
3
 
3
4
  module LLVM
4
5
  class Module
5
6
  include PointerIdentity
6
7
 
7
8
  # @private
9
+ #: (FFI::Pointer) -> LLVM::Module?
8
10
  def self.from_ptr(ptr)
9
11
  return if ptr.null?
10
12
  mod = allocate
@@ -24,10 +26,12 @@ module LLVM
24
26
  @ptr = nil
25
27
  end
26
28
 
29
+ #: -> LLVM::Module?
27
30
  def clone_module
28
31
  Module.from_ptr(C.clone_module(self))
29
32
  end
30
33
 
34
+ #: -> String
31
35
  def inspect
32
36
  {
33
37
  triple: triple,
@@ -41,6 +45,7 @@ module LLVM
41
45
  # Get module triple.
42
46
  #
43
47
  # @return [String]
48
+ #: -> String
44
49
  def triple
45
50
  C.get_target(self)
46
51
  end
@@ -48,6 +53,7 @@ module LLVM
48
53
  # Set module triple.
49
54
  #
50
55
  # @param [String] triple
56
+ #: (String) -> void
51
57
  def triple=(triple)
52
58
  C.set_target(self, triple.to_s)
53
59
  end
@@ -55,6 +61,7 @@ module LLVM
55
61
  # Get module data layout.
56
62
  #
57
63
  # @return [String]
64
+ #: -> String
58
65
  def data_layout
59
66
  C.get_data_layout(self)
60
67
  end
@@ -62,11 +69,13 @@ module LLVM
62
69
  # Set module data layout.
63
70
  #
64
71
  # @param [String, TargetDataLayout] data_layout
72
+ #: (String) -> void
65
73
  def data_layout=(data_layout)
66
74
  C.set_data_layout(self, data_layout.to_s)
67
75
  end
68
76
 
69
77
  # Returns a TypeCollection of all the Types in the module.
78
+ #: -> TypeCollection
70
79
  def types
71
80
  @types ||= TypeCollection.new(self)
72
81
  end
@@ -77,14 +86,16 @@ module LLVM
77
86
  end
78
87
 
79
88
  # Returns the Type with the given name (symbol or string).
89
+ #: (String | Symbol) -> Type
80
90
  def named(name)
81
- Type.from_ptr(C.get_type_by_name(@module, name.to_s), nil)
91
+ Type.from_ptr(C.get_type_by_name(@module, name.to_s))
82
92
  end
83
93
 
84
94
  alias_method :[], :named
85
95
  end
86
96
 
87
97
  # Returns an Enumerable of all the GlobalVariables in the module.
98
+ #: -> GlobalCollection
88
99
  def globals
89
100
  @globals ||= GlobalCollection.new(self)
90
101
  end
@@ -97,49 +108,62 @@ module LLVM
97
108
  end
98
109
 
99
110
  # Adds a GlobalVariable with the given type and name to the collection (symbol or string).
100
- def add(ty, name)
111
+ #: (untyped, String | Symbol) ?{ (GlobalVariable) -> void } -> GlobalVariable
112
+ def add(ty, name, &)
101
113
  GlobalVariable.from_ptr(C.add_global(@module, LLVM::Type(ty), name.to_s)).tap do |gvar|
102
114
  yield gvar if block_given?
103
115
  end
104
116
  end
105
117
 
106
118
  # Returns the GlobalVariable with the given name (symbol or string).
119
+ #: (String | Symbol) -> GlobalVariable?
107
120
  def named(name)
108
- 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?
109
123
  end
110
124
 
111
125
  # Returns the first GlobalVariable in the collection.
126
+ #: -> GlobalVariable?
112
127
  def first
113
- GlobalVariable.from_ptr(C.get_first_global(@module))
128
+ ptr = C.get_first_global(@module)
129
+ GlobalVariable.from_ptr(ptr) unless ptr.null?
114
130
  end
115
131
 
116
132
  # Returns the last GlobalVariable in the collection.
133
+ #: -> GlobalVariable?
117
134
  def last
118
- GlobalVariable.from_ptr(C.get_last_global(@module))
135
+ ptr = C.get_last_global(@module)
136
+ GlobalVariable.from_ptr(ptr) unless ptr.null?
119
137
  end
120
138
 
121
139
  # Returns the next GlobalVariable in the collection after global.
140
+ #: (GlobalVariable) -> GlobalVariable?
122
141
  def next(global)
123
- GlobalVariable.from_ptr(C.get_next_global(global))
142
+ ptr = C.get_next_global(global)
143
+ GlobalVariable.from_ptr(ptr) unless ptr.null?
124
144
  end
125
145
 
126
146
  # Returns the previous GlobalVariable in the collection before global.
147
+ #: (GlobalVariable) -> GlobalVariable?
127
148
  def previous(global)
128
- GlobalVariable.from_ptr(C.get_previous_global(global))
149
+ ptr = C.get_previous_global(global)
150
+ GlobalVariable.from_ptr(ptr) unless ptr.null?
129
151
  end
130
152
 
131
153
  # Deletes the GlobalVariable from the collection.
154
+ #: (GlobalVariable) -> void
132
155
  def delete(global)
133
156
  C.delete_global(global)
134
157
  end
135
158
 
136
159
  # Returns the GlobalVariable with a name equal to key (symbol or string) or at key (integer).
160
+ #: (String | Symbol | Integer) -> GlobalVariable?
137
161
  def [](key)
138
162
  case key
139
163
  when String, Symbol then named(key)
140
- when Integer then
164
+ when Integer
141
165
  i = 0
142
- g = first
166
+ g = first #: GlobalVariable?
143
167
  until i >= key || g.nil?
144
168
  g = self.next(g)
145
169
  i += 1
@@ -149,8 +173,10 @@ module LLVM
149
173
  end
150
174
 
151
175
  # Iterates through each GlobalVariable in the collection.
152
- def each
153
- g = first
176
+ # @override
177
+ #: { (GlobalVariable) -> void } -> void
178
+ def each(&)
179
+ g = first #: GlobalVariable?
154
180
  until g.nil?
155
181
  yield g
156
182
  g = self.next(g)
@@ -159,6 +185,7 @@ module LLVM
159
185
  end
160
186
 
161
187
  # Returns a FunctionCollection of all the Functions in the module.
188
+ #: -> FunctionCollection
162
189
  def functions
163
190
  @functions ||= FunctionCollection.new(self)
164
191
  end
@@ -172,10 +199,12 @@ module LLVM
172
199
 
173
200
  # Adds a Function with the given name (symbol or string) and args (Types).
174
201
  def add(name, *args)
175
- if args.first.kind_of? FunctionType
176
- type = args.first
202
+ type = if args.first.is_a? FunctionType
203
+ args.first
177
204
  else
178
- type = Type.function(*args)
205
+ Type.function(
206
+ *args #: as untyped
207
+ )
179
208
  end
180
209
  function = Function.from_ptr(C.add_function(@module, name.to_s, type))
181
210
 
@@ -188,28 +217,38 @@ module LLVM
188
217
  end
189
218
 
190
219
  # Returns the Function with the given name (symbol or string).
220
+ #: (String | Symbol) -> Function?
191
221
  def named(name)
192
- 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?
193
224
  end
194
225
 
195
226
  # Returns the first Function in the collection.
227
+ #: -> Function?
196
228
  def first
197
- Function.from_ptr(C.get_first_function(@module))
229
+ ptr = C.get_first_function(@module)
230
+ Function.from_ptr(ptr) unless ptr.null?
198
231
  end
199
232
 
200
233
  # Returns the last Function in the collection.
234
+ #: -> Function?
201
235
  def last
202
- Function.from_ptr(C.get_last_function(@module))
236
+ ptr = C.get_last_function(@module)
237
+ Function.from_ptr(ptr) unless ptr.null?
203
238
  end
204
239
 
205
240
  # Returns the next Function in the collection after function.
241
+ #: (Function) -> Function?
206
242
  def next(function)
207
- Function.from_ptr(C.get_next_function(function))
243
+ ptr = C.get_next_function(function)
244
+ Function.from_ptr(ptr) unless ptr.null?
208
245
  end
209
246
 
210
247
  # Returns the previous Function in the collection before function.
248
+ #: (Function) -> Function?
211
249
  def previous(function)
212
- Function.from_ptr(C.get_previous_function(function))
250
+ ptr = C.get_previous_function(function)
251
+ Function.from_ptr(ptr) unless ptr.null?
213
252
  end
214
253
 
215
254
  # Deletes the Function from the collection.
@@ -218,12 +257,13 @@ module LLVM
218
257
  end
219
258
 
220
259
  # Returns the Function with a name equal to key (symbol or string) or at key (integer).
260
+ #: (String | Symbol | Integer) -> Function?
221
261
  def [](key)
222
262
  case key
223
263
  when String, Symbol then named(key)
224
264
  when Integer
225
265
  i = 0
226
- f = first
266
+ f = first #: Function?
227
267
  until i >= key || f.nil?
228
268
  f = self.next(f)
229
269
  i += 1
@@ -233,8 +273,10 @@ module LLVM
233
273
  end
234
274
 
235
275
  # Iterates through each Function in the collection.
236
- def each
237
- f = first
276
+ # @override
277
+ #: { (Function) -> void } -> void
278
+ def each(&)
279
+ f = first #: Function?
238
280
  until f.nil?
239
281
  yield f
240
282
  f = self.next(f)
@@ -243,13 +285,27 @@ module LLVM
243
285
  end
244
286
 
245
287
  # Returns the LLVM IR of the module as a string.
288
+ #: -> String
246
289
  def to_s
247
290
  C.print_module_to_string(self)
248
291
  end
249
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
+
250
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
251
306
  def dump
252
- 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
253
309
  end
254
310
  end
255
311
  end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+ # typed: true
2
3
 
3
4
  require "llvm/transforms/ipo"
4
5
  require "llvm/transforms/scalar"