Skip to content

Commit 47fa270

Browse files
committed
Extend Context functions
1 parent 101213b commit 47fa270

File tree

1 file changed

+58
-33
lines changed

1 file changed

+58
-33
lines changed

llvm-codegen/LLVM/Context.swift

Lines changed: 58 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public class Context: ContextRef {
2121
/// Diagnostic handler type
2222
public typealias DiagnosticHandler = @convention(c) (LLVMDiagnosticInfoRef, UnsafeMutableRawPointer?) -> Void
2323

24+
public typealias YieldCallback = @convention(c) (LLVMContextRef?, UnsafeMutableRawPointer?) -> Void
25+
2426
/// Retrieves the global context instance.
2527
///
2628
/// The global context is an particularly convenient instance managed by LLVM
@@ -86,37 +88,41 @@ public class Context: ContextRef {
8688
}
8789
}
8890

89-
//===============================
90-
//###############################
91-
//===============================
92-
9391
/// Get the diagnostic context of this context.
94-
public func getDiagnosticContext() {
95-
// void *LLVMContextGetDiagnosticContext(LLVMContextRef C);
92+
public func getDiagnosticContext() -> UnsafeMutableRawPointer {
93+
LLVMContextGetDiagnosticContext(llvm)
9694
}
9795

9896
/// Set the yield callback function for this context.
99-
public func setYieldCallback() {
100-
// void LLVMContextSetYieldCallback(LLVMContextRef C, LLVMYieldCallback Callback,
97+
public func setYieldCallback(callback: YieldCallback?, opaqueHandle: UnsafeMutableRawPointer?) {
98+
LLVMContextSetYieldCallback(llvm, callback, opaqueHandle)
10199
}
102100

103101
/// Return a string representation of the DiagnosticInfo. Use
104102
/// LLVMDisposeMessage to free the string.
105-
public func getDiagInfoDescription() {
106-
// char *LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef DI);
103+
public func getDiagInfoDescription(diagnosticInfo: DiagnosticInfoRef) -> String? {
104+
if let cString = LLVMGetDiagInfoDescription(diagnosticInfo.diagnosticInfoRef) {
105+
return String(cString: cString)
106+
} else {
107+
return nil
108+
}
107109
}
108110

109-
/// Return an enum LLVMDiagnosticSeverity.
110-
public func getDiagInfoSeverity() {
111-
// LLVMDiagnosticSeverity LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI);
111+
/// Return an enum LLVMDiagnosticSeverity.
112+
public func getDiagInfoSeverity(diagnosticInfo: DiagnosticInfoRef) -> LLVMDiagnosticSeverity {
113+
LLVMGetDiagInfoSeverity(diagnosticInfo.diagnosticInfoRef)
112114
}
113115

114-
public func getMDKindIDInContext() {
115-
// unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char *Name,
116+
public func getMDKindIDInContext(name: String) -> UInt32 {
117+
name.withCString { cString in
118+
LLVMGetMDKindIDInContext(llvm, cString, UInt32(name.utf8.count))
119+
}
116120
}
117121

118-
public func getMDKindID() {
119-
// unsigned LLVMGetMDKindID(const char *Name, unsigned SLen);
122+
public func getMDKindID(name: String) -> UInt32 {
123+
name.withCString { cString in
124+
LLVMGetMDKindIDInContext(llvm, cString, UInt32(name.utf8.count))
125+
}
120126
}
121127

122128
/// Return an unique id given the name of a enum attribute,
@@ -128,44 +134,63 @@ public class Context: ContextRef {
128134
///
129135
/// NB: Attribute names and/or id are subject to change without
130136
/// going through the C API deprecation cycle.
131-
public func getEnumAttributeKindForName() {
132-
// unsigned LLVMGetEnumAttributeKindForName(const char *Name, size_t SLen);
137+
public func getEnumAttributeKindForName(name: String) -> UInt32 {
138+
name.withCString { cString in
139+
LLVMGetEnumAttributeKindForName(cString, name.utf8.count)
140+
}
141+
}
142+
143+
public func getLastEnumAttributeKind() -> UInt32 {
144+
LLVMGetLastEnumAttributeKind()
133145
}
134146

135-
public func getLastEnumAttributeKind() {
136-
// unsigned LLVMGetLastEnumAttributeKind(void);
147+
struct Attribute: AttributeRef {
148+
var attributeRef: LLVMAttributeRef
137149
}
138150

139151
/// Create an enum attribute.
140-
public func attributeRef() {
141-
// LLVMAttributeRef LLVMCreateEnumAttribute(LLVMContextRef C, unsigned KindID, uint64_t Val);
152+
public func createEnumAttribute(kindID: UInt32, value: UInt64) -> AttributeRef? {
153+
guard let attributeRef = LLVMCreateEnumAttribute(llvm, kindID, value) else { return nil }
154+
return Attribute(attributeRef: attributeRef)
142155
}
143156

144157
/// Get the unique id corresponding to the enum attribute passed as argument.
145-
public func getEnumAttributeKind() {
146-
// unsigned LLVMGetEnumAttributeKind(LLVMAttributeRef A);
158+
public func getEnumAttributeKind(attributeRef: AttributeRef) -> UInt32 {
159+
LLVMGetEnumAttributeKind(attributeRef.attributeRef)
147160
}
148161

149162
/// Get the enum attribute's value. 0 is returned if none exists.
150-
public func getEnumAttributeValue() {
151-
// uint64_t LLVMGetEnumAttributeValue(LLVMAttributeRef A);
163+
public func getEnumAttributeValue(attributeRef: AttributeRef) -> UInt64 {
164+
LLVMGetEnumAttributeValue(attributeRef.attributeRef)
152165
}
153166

154167
/// Create a type attribute
155-
public func createTypeAttribute() {
156-
// LLVMAttributeRef LLVMCreateTypeAttribute(LLVMContextRef C, unsigned KindID, LLVMTypeRef type_ref);
168+
public func createTypeAttribute(kindID: UInt32, typeRef: TypeRef) -> AttributeRef? {
169+
guard let attributeRef = LLVMCreateTypeAttribute(llvm, kindID, typeRef.typeRef) else { return nil }
170+
return Attribute(attributeRef: attributeRef)
157171
}
158172

159173
/// Get the type attribute's value.
160-
public func getTypeAttributeValue() {
161-
// LLVMTypeRef LLVMGetTypeAttributeValue(LLVMAttributeRef A);
174+
public func getTypeAttributeValue(attributeRef: AttributeRef) -> TypeRef? {
175+
guard let typeRef = LLVMGetTypeAttributeValue(attributeRef.attributeRef) else { return nil }
176+
return Types(llvm: typeRef)
162177
}
163178

164179
/// Create a string attribute.
165-
public func createStringAttribute() {
166-
// TODO: LLVMAttributeRef LLVMCreateStringAttribute(LLVMContextRef C,
180+
public func createStringAttribute(key: String, value: String) -> AttributeRef? {
181+
let attribute = key.withCString { keyCString in
182+
value.withCString { valueCString in
183+
LLVMCreateStringAttribute(llvm, keyCString, UInt32(key.utf8.count), valueCString, UInt32(value.utf8.count))
184+
}
185+
}
186+
guard let attributeRef = attribute else { return nil }
187+
return Attribute(attributeRef: attributeRef)
167188
}
168189

190+
//===============================
191+
// ###############################
192+
//===============================
193+
169194
/// Get the string attribute's kind.
170195
public func getStringAttributeKind() {
171196
// const char *LLVMGetStringAttributeKind(LLVMAttributeRef A, unsigned *Length);

0 commit comments

Comments
 (0)