-
Notifications
You must be signed in to change notification settings - Fork 0
/
Function.h
61 lines (47 loc) · 1.26 KB
/
Function.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#pragma once
#include <memory>
#include <ostream>
#include <string>
#include <llvm-c/Core.h>
#include "BasicBlock.h"
class Module;
class Function {
LLVMValueRef func_ = nullptr;
public:
Function(LLVMValueRef f):func_(f) {}
::std::string getName() const
{ return ::std::string(LLVMGetValueName(func_)); }
Module getModule() const;
LLVMValueRef value() const
{ return func_; }
class bb_iterator {
LLVMValueRef &func_;
LLVMBasicBlockRef bb_;
::std::unique_ptr<BasicBlock> instance_ = nullptr;
public:
bb_iterator(LLVMValueRef &f, LLVMBasicBlockRef bb):func_(f), bb_(bb) {}
bool operator != (const bb_iterator &bb)
{ return bb_ != bb.bb_; }
BasicBlock& operator *()
{ return *get_instance_(); }
BasicBlock* operator -> ()
{ return get_instance_(); }
const bb_iterator & operator++()
{
bb_ = LLVMGetNextBasicBlock(bb_);
instance_ = nullptr;
return *this;
}
private:
BasicBlock *get_instance_(){
if (!instance_)
instance_ = ::std::unique_ptr<BasicBlock>(new BasicBlock(bb_));
return instance_.get();
}
};
bb_iterator begin()
{ return bb_iterator(func_, LLVMGetFirstBasicBlock(func_)); }
bb_iterator end()
{ return bb_iterator(func_, nullptr); }
friend ::std::ostream & operator << (::std::ostream &, const Function &);
};