Skip to content

Commit 45f60d7

Browse files
committed
Complete LLVM Module implementation
1 parent ec6feb3 commit 45f60d7

File tree

2 files changed

+169
-12
lines changed

2 files changed

+169
-12
lines changed

llvm-api/LLVM/Core/Modules.swift

+166-2
Original file line numberDiff line numberDiff line change
@@ -468,17 +468,181 @@ public final class Module: ModuleRef {
468468
}
469469

470470
/// Obtain the context to which this module is associated.
471-
func getModuleContext() -> ContextRef {
471+
public func getModuleContext() -> ContextRef {
472472
let context = LLVMGetModuleContext(llvm)
473473
return Context(llvm: context!)
474474
}
475475

476476
/// Obtain an iterator to the first NamedMDNode in a Module.
477-
func getFirstNamedMetadata() -> NamedMetadataNodeRef {
477+
public func getFirstNamedMetadata() -> NamedMetadataNodeRef {
478478
let namedMD = LLVMGetFirstNamedMetadata(llvm)
479479
return NamedMetadataNode(llvm: namedMD!)
480480
}
481481

482+
/// Obtain an iterator to the last NamedMDNode in a Module.
483+
public func getLastNamedMetadata() -> NamedMetadataNodeRef {
484+
let namedMD = LLVMGetLastNamedMetadata(llvm)
485+
return NamedMetadataNode(llvm: namedMD!)
486+
}
487+
488+
/// Advance a NamedMDNode iterator to the next NamedMDNode.
489+
///
490+
/// Returns NULL if the iterator was already at the end and there are no more
491+
/// named metadata nodes.
492+
public func getNextNamedMetadata(namedMDNode: NamedMetadataNodeRef) -> NamedMetadataNodeRef {
493+
let namedMD = LLVMGetNextNamedMetadata(namedMDNode.namedMetadataNodeRef)
494+
return NamedMetadataNode(llvm: namedMD!)
495+
}
496+
497+
/// Decrement a NamedMDNode iterator to the previous NamedMDNode.
498+
///
499+
/// Returns NULL if the iterator was already at the beginning and there are
500+
/// no previous named metadata nodes.
501+
public func getPreviousNamedMetadata(namedMDNode: NamedMetadataNodeRef) -> NamedMetadataNodeRef {
502+
let namedMD = LLVMGetPreviousNamedMetadata(namedMDNode.namedMetadataNodeRef)
503+
return NamedMetadataNode(llvm: namedMD!)
504+
}
505+
506+
/// Retrieve a NamedMDNode with the given name, returning NULL if no such
507+
/// node exists.
508+
public func getNamedMetadata(name: String) -> NamedMetadataNodeRef {
509+
let namedMD = name.withCString { cString in
510+
LLVMGetNamedMetadata(llvm, cString, name.utf8.count)
511+
}
512+
return NamedMetadataNode(llvm: namedMD!)
513+
}
514+
515+
/// Retrieve a NamedMDNode with the given name, creating a new node if no such
516+
/// node exists.
517+
public func getOrInsertNamedMetadata(name: String) -> NamedMetadataNodeRef {
518+
let namedMD = name.withCString { cString in
519+
LLVMGetOrInsertNamedMetadata(llvm, cString, name.utf8.count)
520+
}
521+
return NamedMetadataNode(llvm: namedMD!)
522+
}
523+
524+
/// Retrieve the name of a NamedMDNode.
525+
public func getNamedMetadataName(namedMD: NamedMetadataNodeRef) -> String? {
526+
var nameLen = 0
527+
guard let cString = LLVMGetNamedMetadataName(namedMD.namedMetadataNodeRef, &nameLen) else {
528+
return nil
529+
}
530+
return String(cString: cString)
531+
}
532+
533+
/// Obtain the number of operands for named metadata in a module.
534+
public func getNamedMetadataNumOperands(name: String) -> UInt32 {
535+
name.withCString { cString in
536+
LLVMGetNamedMetadataNumOperands(llvm, cString)
537+
}
538+
}
539+
540+
/// Obtain the named metadata operands for a module.
541+
///
542+
/// The passed LLVMValueRef pointer should refer to an array of
543+
/// LLVMValueRef at least LLVMGetNamedMetadataNumOperands long. This
544+
/// array will be populated with the LLVMValueRef instances. Each
545+
/// instance corresponds to a llvm::MDNode.
546+
public func getNamedMetadataOperands(name: String) -> [ValueRef] {
547+
let numOperands = getNamedMetadataNumOperands(name: name)
548+
var operands = [LLVMValueRef?](repeating: nil, count: Int(numOperands))
549+
550+
operands.withUnsafeMutableBufferPointer { buffer in
551+
name.withCString { cString in
552+
LLVMGetNamedMetadataOperands(llvm, cString, buffer.baseAddress)
553+
}
554+
}
555+
556+
return operands.compactMap { Value(llvm: $0!) }
557+
}
558+
559+
/// Add an operand to named metadata.
560+
public func addNamedMetadataOperand(name: String, value: ValueRef) {
561+
name.withCString { cString in
562+
LLVMAddNamedMetadataOperand(llvm, cString, value.valueRef)
563+
}
564+
}
565+
566+
/// Return the directory of the debug location for this value, which must be
567+
/// an llvm Instruction, llvm GlobalVariable, or llvm Function.
568+
public func getDebugLocDirectory(value: ValueRef) -> String? {
569+
var length: UInt32 = 0
570+
guard let cString = LLVMGetDebugLocDirectory(value.valueRef, &length) else {
571+
return nil
572+
}
573+
return String(cString: cString)
574+
}
575+
576+
/// Return the filename of the debug location for this value, which must be
577+
/// an llvm Instruction, llvm GlobalVariable, or llvm Function.
578+
public func getDebugLocFilename(value: ValueRef) -> String? {
579+
var length: UInt32 = 0
580+
guard let cString = LLVMGetDebugLocFilename(value.valueRef, &length) else {
581+
return nil
582+
}
583+
return String(cString: cString)
584+
}
585+
586+
/// Return the line number of the debug location for this value, which must be
587+
/// an llvm Instruction, llvm GlobalVariable, or llvm Function.
588+
public func getDebugLocLine(value: ValueRef) -> UInt32 {
589+
LLVMGetDebugLocLine(value.valueRef)
590+
}
591+
592+
/// Return the column number of the debug location for this value, which must be
593+
/// an llvm Instruction.
594+
public func getDebugLocColumn(value: ValueRef) -> UInt32 {
595+
LLVMGetDebugLocColumn(value.valueRef)
596+
}
597+
598+
/// Add a function to a module under a specified name.
599+
public func addFunction(name: String, functionType: TypeRef) -> ValueRef? {
600+
guard let value = name.withCString({ cString in
601+
LLVMAddFunction(llvm, cString, functionType.typeRef)
602+
}) else { return nil }
603+
return Value(llvm: value)
604+
}
605+
606+
/// Obtain a Function value from a Module by its name.
607+
///
608+
/// The returned value corresponds to a llvm::Function value.
609+
public func getNamedFunction(name: String) -> ValueRef? {
610+
guard let value = name.withCString({ cString in
611+
LLVMGetNamedFunction(llvm, cString)
612+
}) else { return nil }
613+
return Value(llvm: value)
614+
}
615+
616+
/// Obtain an iterator to the first Function in a Module.
617+
public func getFirstFunction() -> ValueRef? {
618+
guard let value = LLVMGetFirstFunction(llvm) else { return nil }
619+
return Value(llvm: value)
620+
}
621+
622+
/// Obtain an iterator to the last Function in a Module.
623+
public func getLastFunction() -> ValueRef? {
624+
guard let value = LLVMGetLastFunction(llvm) else { return nil }
625+
return Value(llvm: value)
626+
}
627+
628+
/// Advance a Function iterator to the next Function.
629+
///
630+
/// Returns NULL if the iterator was already at the end and there are no more
631+
/// functions.
632+
public func getNextFunction(function: ValueRef) -> ValueRef? {
633+
guard let value = LLVMGetNextFunction(function.valueRef) else { return nil }
634+
return Value(llvm: value)
635+
}
636+
637+
/// Decrement a Function iterator to the previous Function.
638+
///
639+
/// Returns NULL if the iterator was already at the beginning and there are
640+
/// no previous functions.
641+
public func getPreviousFunction(function: ValueRef) -> ValueRef? {
642+
guard let value = LLVMGetPreviousFunction(function.valueRef) else { return nil }
643+
return Value(llvm: value)
644+
}
645+
482646
/// Destroy a module instance.
483647
///
484648
/// This must be called for every created module or memory will be leaked.

llvm-api/LLVM/Core/Values/Constants/Composite.swift

+3-10
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,11 @@ public enum CompositeConstant {
2828

2929
/// Get the given constant data sequential as a string.
3030
public static func getString(value: ValueRef) -> String? {
31-
var length: UInt = 0
32-
let lengthPtr = UnsafeMutablePointer<UInt>.allocate(capacity: 1)
33-
defer {
34-
lengthPtr.deallocate()
35-
}
36-
37-
if let cStr = LLVMGetAsString(value.valueRef, lengthPtr) {
38-
length = lengthPtr.pointee
39-
return String(cString: cStr)
40-
} else {
31+
var length: size_t = 0
32+
guard let cString = LLVMGetAsString(value.valueRef, &length) else {
4133
return nil
4234
}
35+
return String(cString: cString)
4336
}
4437

4538
/// Create an anonymous `ConstantStruct` with the specified values.

0 commit comments

Comments
 (0)