Skip to content

Commit 82e113f

Browse files
committed
Refactor named metadata now that it is a first-class type
1 parent 4cb0a8d commit 82e113f

File tree

2 files changed

+51
-9
lines changed

2 files changed

+51
-9
lines changed

Sources/LLVM/Module.swift

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,18 @@ public final class Module: CustomStringConvertible {
242242
}
243243
}
244244

245+
/// Retrieves the first alias in this module, if there are any aliases.
246+
public var firstAlias: Alias? {
247+
guard let fn = LLVMGetFirstGlobalAlias(llvm) else { return nil }
248+
return Alias(llvm: fn)
249+
}
250+
251+
/// Retrieves the last alias in this module, if there are any aliases.
252+
public var lastAlias: Alias? {
253+
guard let fn = LLVMGetLastGlobalAlias(llvm) else { return nil }
254+
return Alias(llvm: fn)
255+
}
256+
245257
/// Retrieves the sequence of aliases that make up this module.
246258
public var aliases: AnySequence<Alias> {
247259
var current = firstAlias
@@ -254,15 +266,26 @@ public final class Module: CustomStringConvertible {
254266
}
255267

256268
/// Retrieves the first alias in this module, if there are any aliases.
257-
public var firstAlias: Alias? {
258-
guard let fn = LLVMGetFirstGlobalAlias(llvm) else { return nil }
259-
return Alias(llvm: fn)
269+
public var firstNamedMetadata: NamedMetadata? {
270+
guard let fn = LLVMGetFirstNamedMetadata(llvm) else { return nil }
271+
return NamedMetadata(module: self, llvm: fn)
260272
}
261273

262274
/// Retrieves the last alias in this module, if there are any aliases.
263-
public var lastAlias: Alias? {
264-
guard let fn = LLVMGetLastGlobalAlias(llvm) else { return nil }
265-
return Alias(llvm: fn)
275+
public var lastNamedMetadata: NamedMetadata? {
276+
guard let fn = LLVMGetLastNamedMetadata(llvm) else { return nil }
277+
return NamedMetadata(module: self, llvm: fn)
278+
}
279+
280+
/// Retrieves the sequence of aliases that make up this module.
281+
public var namedMetadata: AnySequence<NamedMetadata> {
282+
var current = firstNamedMetadata
283+
return AnySequence<NamedMetadata> {
284+
return AnyIterator<NamedMetadata> {
285+
defer { current = current?.next() }
286+
return current
287+
}
288+
}
266289
}
267290

268291
/// The current debug metadata version number.
@@ -375,7 +398,7 @@ extension Module {
375398
/// - returns: A representation of the newly created metadata with the
376399
/// given name.
377400
public func metadata(named name: String) -> NamedMetadata {
378-
return NamedMetadata(module: self, name: name)
401+
return NamedMetadata(module: self, llvm: LLVMGetOrInsertNamedMetadata(self.llvm, name, name.count))
379402
}
380403

381404
/// Build a named global of the given type.

Sources/LLVM/NamedMetadata.swift

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,28 @@ public class NamedMetadata {
1111
/// The name associated with this named metadata.
1212
public let name: String
1313

14-
init(module: Module, name: String) {
14+
private let llvm: LLVMNamedMDNodeRef
15+
16+
init(module: Module, llvm: LLVMNamedMDNodeRef) {
1517
self.module = module
16-
self.name = name
18+
self.llvm = llvm
19+
var nameLen = 0
20+
guard let rawName = LLVMGetNamedMetadataName(llvm, &nameLen) else {
21+
fatalError("Could not retrieve name for named MD node?")
22+
}
23+
self.name = String(cString: rawName)
24+
}
25+
26+
/// Retrieves the previous alias in the module, if there is one.
27+
public func previous() -> NamedMetadata? {
28+
guard let previous = LLVMGetPreviousNamedMetadata(llvm) else { return nil }
29+
return NamedMetadata(module: self.module, llvm: previous)
30+
}
31+
32+
/// Retrieves the next alias in the module, if there is one.
33+
public func next() -> NamedMetadata? {
34+
guard let next = LLVMGetNextNamedMetadata(llvm) else { return nil }
35+
return NamedMetadata(module: self.module,llvm: next)
1736
}
1837

1938
/// Computes the operands of a named metadata node.

0 commit comments

Comments
 (0)