Skip to content

Commit 6ffe444

Browse files
committed
Lift LLVMAttributeIndex with a raw representable enum
1 parent 5a4d51c commit 6ffe444

File tree

1 file changed

+39
-41
lines changed

1 file changed

+39
-41
lines changed

Sources/LLVM/Function+Attributes.swift

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import cllvm
33
#endif
44

5-
/// Enumerates the attributes of LLVM functions.
5+
/// Enumerates the attributes of LLVM functions and function parameters.
66
public enum FunctionAttribute: String {
77
/// This attribute indicates that, when emitting the prologue and epilogue,
88
/// the backend should forcibly align the stack pointer.
@@ -149,15 +149,6 @@ public enum FunctionAttribute: String {
149149
/// unwind table entry be produced for this function even if we can show
150150
/// that no exceptions passes by it.
151151
case uwtable
152-
153-
/// ID of the attribute.
154-
internal var kindID: UInt32 {
155-
return LLVMGetEnumAttributeKindForName(rawValue, rawValue.count)
156-
}
157-
}
158-
159-
/// Enumerates the parameter attributes of LLVM functions.
160-
public enum ParameterAttribute: String {
161152
/// This indicates to the code generator that the parameter or return value
162153
/// should be zero-extended to the extent required by the target’s ABI by the
163154
/// caller (for a parameter) or the callee (for a return value).
@@ -215,48 +206,55 @@ public enum ParameterAttribute: String {
215206
}
216207
}
217208

218-
extension Function {
219-
/// Adds an attribute to the function.
220-
///
221-
/// - parameter attr: The attribute to add.
222-
/// - parameter value: The optional value of the attribute.
223-
public func addAttribute(_ attr: FunctionAttribute, value: UInt64 = 0) {
224-
let ctx = LLVMGetModuleContext(LLVMGetGlobalParent(llvm))
225-
let attrRef = LLVMCreateEnumAttribute(ctx, attr.kindID, value)
226-
LLVMAddAttributeAtIndex(llvm, 0, attrRef)
209+
/// Represents the possible indices of function attributes.
210+
public enum AttributeIndex: ExpressibleByIntegerLiteral, RawRepresentable {
211+
/// Represents the function itself.
212+
case function
213+
/// Represents the function's return value.
214+
case returnValue
215+
/// Represents the function's i-th argument.
216+
case argument(Int)
217+
218+
public init(integerLiteral value: UInt32) {
219+
switch value {
220+
case ~0: self = .function
221+
case 0: self = .returnValue
222+
default: self = .argument(Int(value) - 1)
223+
}
227224
}
228225

229-
/// Adds an attribute to the given function's parameter.
226+
public init?(rawValue: RawValue) {
227+
self.init(integerLiteral: rawValue)
228+
}
229+
230+
public var rawValue: UInt32 {
231+
switch self {
232+
case .function: return ~0
233+
case .returnValue: return 0
234+
case .argument(let i): return UInt32(i) + 1
235+
}
236+
}
237+
}
238+
239+
extension Function {
240+
/// Adds an attribute to the function, its return value or its parameters.
230241
///
231242
/// - parameter attr: The attribute to add.
232243
/// - parameter value: The optional value of the attribute.
233-
/// - parameterIndex: The index of the parameter to which add the
234-
/// attribute, starting from 0.
235-
public func addParameterAttribute(_ attr: ParameterAttribute, value: UInt64 = 0,
236-
for parameterIndex: LLVMAttributeIndex) {
244+
/// - parameter index: The index representing the function, its return value
245+
/// or one of its parameters.
246+
public func addAttribute(_ attr: FunctionAttribute, value: UInt64 = 0, to index: AttributeIndex) {
237247
let ctx = LLVMGetModuleContext(LLVMGetGlobalParent(llvm))
238248
let attrRef = LLVMCreateEnumAttribute(ctx, attr.kindID, value)
239-
LLVMAddAttributeAtIndex(llvm, parameterIndex + 1, attrRef)
240-
}
241-
242-
/// Removes an attribute from the function.
243-
///
244-
/// - parameter attr: The attribute to remove.
245-
public func removeAttribute(_ attr: FunctionAttribute, value: UInt64 = 0) {
246-
LLVMRemoveEnumAttributeAtIndex(llvm, 0, attr.kindID)
249+
LLVMAddAttributeAtIndex(llvm, index.rawValue, attrRef)
247250
}
248251

249252
/// Removes an attribute from the function.
250253
///
251-
/// - parameter attr: The attribute to add.
252-
253-
/// Removes an attribute from the given function's parameter.
254-
///
255254
/// - parameter attr: The attribute to remove.
256-
/// - parameter parameterIndex: The index of the parameter to which add the
257-
/// attribute, starting from 0.
258-
public func removeParameterAttribute(_ attr: ParameterAttribute,
259-
for parameterIndex: LLVMAttributeIndex) {
260-
LLVMRemoveEnumAttributeAtIndex(llvm, parameterIndex + 1, attr.kindID)
255+
/// - parameter index: The index representing the function, its return value
256+
/// or one of its parameters.
257+
public func removeAttribute(_ attr: FunctionAttribute, value: UInt64 = 0, from index: AttributeIndex) {
258+
LLVMRemoveEnumAttributeAtIndex(llvm, index.rawValue, attr.kindID)
261259
}
262260
}

0 commit comments

Comments
 (0)