Skip to content

Commit 46c92d5

Browse files
committed
Improve alias bindings
1 parent 98b95ab commit 46c92d5

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

Sources/LLVM/Alias.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,22 @@ public struct Alias: IRGlobal {
1111
public func asLLVM() -> LLVMValueRef {
1212
return llvm
1313
}
14+
15+
/// Access the target value of this alias.
16+
public var aliasee: IRValue {
17+
get { return LLVMAliasGetAliasee(llvm) }
18+
set { LLVMAliasSetAliasee(llvm, newValue.asLLVM()) }
19+
}
20+
21+
/// Retrieves the previous alias in the module, if there is one.
22+
public func previous() -> Alias? {
23+
guard let previous = LLVMGetPreviousGlobalAlias(llvm) else { return nil }
24+
return Alias(llvm: previous)
25+
}
26+
27+
/// Retrieves the next alias in the module, if there is one.
28+
public func next() -> Alias? {
29+
guard let next = LLVMGetNextGlobalAlias(llvm) else { return nil }
30+
return Alias(llvm: next)
31+
}
1432
}

Sources/LLVM/Module.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,29 @@ public final class Module: CustomStringConvertible {
230230
}
231231
}
232232

233+
/// Retrieves the sequence of aliases that make up this module.
234+
public var aliases: AnySequence<Alias> {
235+
var current = firstAlias
236+
return AnySequence<Alias> {
237+
return AnyIterator<Alias> {
238+
defer { current = current?.next() }
239+
return current
240+
}
241+
}
242+
}
243+
244+
/// Retrieves the first alias in this module, if there are any aliases.
245+
public var firstAlias: Alias? {
246+
guard let fn = LLVMGetFirstGlobalAlias(llvm) else { return nil }
247+
return Alias(llvm: fn)
248+
}
249+
250+
/// Retrieves the last alias in this module, if there are any aliases.
251+
public var lastAlias: Alias? {
252+
guard let fn = LLVMGetLastGlobalAlias(llvm) else { return nil }
253+
return Alias(llvm: fn)
254+
}
255+
233256
/// The current debug metadata version number.
234257
public static var debugMetadataVersion: UInt32 {
235258
return LLVMDebugMetadataVersion();
@@ -307,6 +330,18 @@ extension Module {
307330
return Function(llvm: fn)
308331
}
309332

333+
/// Searches for and retrieves an alias with the given name in this module
334+
/// if that name references an existing alias.
335+
///
336+
/// - parameter name: The name of the alias to search for.
337+
///
338+
/// - returns: A representation of an alias with the given
339+
/// name or nil if no such named alias exists.
340+
public func alias(named name: String) -> Alias? {
341+
guard let alias = LLVMGetNamedGlobalAlias(llvm, name, name.count) else { return nil }
342+
return Alias(llvm: alias)
343+
}
344+
310345
/// Searches for and retrieves a comdat section with the given name in this
311346
/// module. If none is found, one with that name is created and returned.
312347
///

0 commit comments

Comments
 (0)