Skip to content

Commit

Permalink
Use new iteration protocol.
Browse files Browse the repository at this point in the history
  • Loading branch information
maleadt committed May 18, 2018
1 parent 234ae35 commit 8ce7aa8
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 60 deletions.
10 changes: 4 additions & 6 deletions src/core/basicblock.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,10 @@ instructions(bb::BasicBlock) = BasicBlockInstructionSet(bb)

Base.eltype(::BasicBlockInstructionSet) = Instruction

Base.start(iter::BasicBlockInstructionSet) = API.LLVMGetFirstInstruction(blockref(iter.bb))

Base.next(::BasicBlockInstructionSet, state) =
(Instruction(state), API.LLVMGetNextInstruction(state))

Base.done(::BasicBlockInstructionSet, state) = state == C_NULL
function Base.iterate(iter::BasicBlockInstructionSet,
state=API.LLVMGetFirstInstruction(blockref(iter.bb)))
state == C_NULL ? nothing : (Instruction(state), API.LLVMGetNextInstruction(state))
end

Base.last(iter::BasicBlockInstructionSet) =
Instruction(API.LLVMGetLastInstruction(blockref(iter.bb)))
Expand Down
18 changes: 6 additions & 12 deletions src/core/function.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,9 @@ Base.eltype(::FunctionParameterSet) = Argument
Base.getindex(iter::FunctionParameterSet, i) =
Argument(API.LLVMGetParam(ref(iter.f), Cuint(i-1)))

Base.start(iter::FunctionParameterSet) = API.LLVMGetFirstParam(ref(iter.f))

Base.next(::FunctionParameterSet, state) =
(Argument(state), API.LLVMGetNextParam(state))

Base.done(::FunctionParameterSet, state) = state == C_NULL
function Base.iterate(iter::FunctionParameterSet, state=API.LLVMGetFirstParam(ref(iter.f)))
state == C_NULL ? nothing : (Argument(state), API.LLVMGetNextParam(state))
end

Base.last(iter::FunctionParameterSet) =
Argument(API.LLVMGetLastParam(ref(iter.f)))
Expand All @@ -118,12 +115,9 @@ blocks(f::Function) = FunctionBlockSet(f)

Base.eltype(::FunctionBlockSet) = BasicBlock

Base.start(iter::FunctionBlockSet) = API.LLVMGetFirstBasicBlock(ref(iter.f))

Base.next(::FunctionBlockSet, state) =
(BasicBlock(state), API.LLVMGetNextBasicBlock(state))

Base.done(::FunctionBlockSet, state) = state == C_NULL
function Base.iterate(iter::FunctionBlockSet, state=API.LLVMGetFirstBasicBlock(ref(iter.f)))
state == C_NULL ? nothing : (BasicBlock(state), API.LLVMGetNextBasicBlock(state))
end

Base.last(iter::FunctionBlockSet) =
BasicBlock(API.LLVMGetLastBasicBlock(ref(iter.f)))
Expand Down
9 changes: 3 additions & 6 deletions src/core/instructions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,9 @@ Base.getindex(iter::TerminatorSuccessorSet, i) =
Base.setindex!(iter::TerminatorSuccessorSet, bb::BasicBlock, i) =
API.LLVMSetSuccessor(ref(iter.term), Cuint(i-1), blockref(bb))

Base.start(iter::TerminatorSuccessorSet) = (1,length(iter))

Base.next(iter::TerminatorSuccessorSet, state) =
(iter[state[1]], (state[1]+1,state[2]))

Base.done(::TerminatorSuccessorSet, state) = (state[1] > state[2])
function Base.iterate(iter::TerminatorSuccessorSet, i=1)
i >= length(iter) + 1 ? nothing : (iter[i], i+1)
end

Base.length(iter::TerminatorSuccessorSet) = API.LLVMGetNumSuccessors(ref(iter.term))

Expand Down
18 changes: 6 additions & 12 deletions src/core/module.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,9 @@ globals(mod::Module) = ModuleGlobalSet(mod)

Base.eltype(::ModuleGlobalSet) = GlobalVariable

Base.start(iter::ModuleGlobalSet) = API.LLVMGetFirstGlobal(ref(iter.mod))

Base.next(::ModuleGlobalSet, state) =
(GlobalVariable(state), API.LLVMGetNextGlobal(state))

Base.done(::ModuleGlobalSet, state) = state == C_NULL
function Base.iterate(iter::ModuleGlobalSet, state=API.LLVMGetFirstGlobal(ref(iter.mod)))
state == C_NULL ? nothing : (GlobalVariable(state), API.LLVMGetNextGlobal(state))
end

Base.last(iter::ModuleGlobalSet) =
GlobalVariable(API.LLVMGetLastGlobal(ref(iter.mod)))
Expand Down Expand Up @@ -153,12 +150,9 @@ functions(mod::Module) = ModuleFunctionSet(mod)

Base.eltype(::ModuleFunctionSet) = Function

Base.start(iter::ModuleFunctionSet) = API.LLVMGetFirstFunction(ref(iter.mod))

Base.next(::ModuleFunctionSet, state) =
(Function(state), API.LLVMGetNextFunction(state))

Base.done(::ModuleFunctionSet, state) = state == C_NULL
function Base.iterate(iter::ModuleFunctionSet, state=API.LLVMGetFirstFunction(ref(iter.mod)))
state == C_NULL ? nothing : (Function(state), API.LLVMGetNextFunction(state))
end

Base.last(iter::ModuleFunctionSet) =
Function(API.LLVMGetLastFunction(ref(iter.mod)))
Expand Down
9 changes: 3 additions & 6 deletions src/core/type.jl
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,9 @@ Base.eltype(::StructTypeElementSet) = LLVMType
Base.getindex(iter::StructTypeElementSet, i) =
LLVMType(API.LLVMStructGetTypeAtIndex(ref(iter.typ), Cuint(i-1)))

Base.start(iter::StructTypeElementSet) = (1,length(iter))

Base.next(iter::StructTypeElementSet, state) =
(iter[state[1]], (state[1]+1,state[2]))

Base.done(::StructTypeElementSet, state) = (state[1] > state[2])
function Base.iterate(iter::StructTypeElementSet, i=1)
i >= length(iter) + 1 ? nothing : (iter[i], i+1)
end

Base.length(iter::StructTypeElementSet) = API.LLVMCountStructElementTypes(ref(iter.typ))

Expand Down
9 changes: 3 additions & 6 deletions src/core/value.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,8 @@ uses(val::Value) = ValueUseSet(val)

Base.eltype(::ValueUseSet) = Use

Base.start(iter::ValueUseSet) = API.LLVMGetFirstUse(ref(iter.val))

Base.next(::ValueUseSet, state) =
(Use(state), API.LLVMGetNextUse(state))

Base.done(::ValueUseSet, state) = state == C_NULL
function Base.iterate(iter::ValueUseSet, state=API.LLVMGetFirstUse(ref(iter.val)))
state == C_NULL ? nothing : (Use(state), API.LLVMGetNextUse(state))
end

Base.IteratorSize(::ValueUseSet) = Base.SizeUnknown()
9 changes: 3 additions & 6 deletions src/core/value/user.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@ Base.getindex(iter::UserOperandSet, i) =
Base.setindex!(iter::UserOperandSet, val::Value, i) =
API.LLVMSetOperand(ref(iter.user), Cuint(i-1), ref(val))

Base.start(iter::UserOperandSet) = (1,length(iter))

Base.next(iter::UserOperandSet, state) =
(iter[state[1]], (state[1]+1,state[2]))

Base.done(::UserOperandSet, state) = (state[1] > state[2])
function Base.iterate(iter::UserOperandSet, i=1)
i >= length(iter) + 1 ? nothing : (iter[i], i+1)
end

Base.length(iter::UserOperandSet) = API.LLVMGetNumOperands(ref(iter.user))

Expand Down
9 changes: 3 additions & 6 deletions src/target.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,8 @@ function Base.getindex(targetset::TargetSet, name::String)
return f == nothing ? throw(KeyError(name)) : f
end

Base.start(::TargetSet) = API.LLVMGetFirstTarget()

Base.next(::TargetSet, state) =
(Target(state), API.LLVMGetNextTarget(state))

Base.done(::TargetSet, state) = state == C_NULL
function Base.iterate(iter::TargetSet, state=API.LLVMGetFirstTarget())
state == C_NULL ? nothing : (Target(state), API.LLVMGetNextTarget(state))
end

Base.IteratorSize(::TargetSet) = Base.SizeUnknown()

0 comments on commit 8ce7aa8

Please sign in to comment.