retdec
function.h
Go to the documentation of this file.
1 
7 #ifndef RETDEC_LLVMIR2HLL_IR_FUNCTION_H
8 #define RETDEC_LLVMIR2HLL_IR_FUNCTION_H
9 
10 #include <cstddef>
11 #include <string>
12 
16 
17 namespace retdec {
18 namespace llvmir2hll {
19 
20 class Module;
21 class Statement;
22 class Type;
23 class Variable;
24 class Visitor;
25 
33 class Function final: public Value {
34 public:
36  std::string name, VarVector params, VarSet localVars = VarSet(),
37  ShPtr<Statement> body = nullptr, bool isVarArg = false);
38 
39  virtual ShPtr<Value> clone() override;
40 
41  virtual bool isEqualTo(ShPtr<Value> otherValue) const override;
42 
43  ShPtr<Type> getRetType() const;
44  const std::string &getInitialName() const;
45  const std::string &getName() const;
46  const VarVector &getParams() const;
47  ShPtr<Variable> getParam(std::size_t n) const;
48  std::size_t getParamPos(ShPtr<Variable> param) const;
49  std::size_t getNumOfParams() const;
50  VarSet getLocalVars(bool includeParams = false) const;
51  std::size_t getNumOfLocalVars(bool includeParams = false) const;
52  bool hasLocalVar(ShPtr<Variable> var, bool includeParams = false) const;
53  ShPtr<Statement> getBody() const;
54  ShPtr<Variable> getAsVar() const;
55  ShPtr<Type> getType() const;
56  ShPtr<Module> getModule() const;
58  Address getStartAddress() const;
59  Address getEndAddress() const;
60 
61  bool isVarArg() const;
62  bool isDeclaration() const;
63  bool isDefinition() const;
64  bool hasParam(ShPtr<Variable> var) const;
65  bool hasParam(std::size_t n) const;
66 
67  void setRetType(ShPtr<Type> newRetType);
68  void setName(const std::string &newName);
69  void setParams(VarVector newParams);
70  void setLocalVars(VarSet newLocalVars);
71  void addParam(ShPtr<Variable> var);
72  void addLocalVar(ShPtr<Variable> var);
73  void replaceParam(ShPtr<Variable> oldParam, ShPtr<Variable> newParam);
74  void replaceLocalVar(ShPtr<Variable> oldVar, ShPtr<Variable> newVar);
76  void removeParam(ShPtr<Variable> param);
77  void setBody(ShPtr<Statement> newBody);
78  void setVarArg(bool isVarArg = true);
79  void convertToDeclaration();
80 
83  virtual void update(ShPtr<Value> subject, ShPtr<Value> arg = nullptr) override;
85 
88  virtual void accept(Visitor *v) override;
90 
91 private:
94 
97 
100 
103 
106 
110 
111  // Takes the function a variable number of arguments?
112  bool varArg;
113 
114 private:
115  // Since instances are created by calling the static function create(), the
116  // constructor can be private.
117  Function(ShPtr<Module> module, ShPtr<Type>, std::string name,
119  ShPtr<Statement> body = nullptr, bool isVarArg = false);
120 
123 };
124 
125 } // namespace llvmir2hll
126 } // namespace retdec
127 
128 #endif
Definition: address.h:21
Definition: range.h:45
A representation of a function.
Definition: function.h:33
void replaceLocalVar(ShPtr< Variable > oldVar, ShPtr< Variable > newVar)
Replaces oldVar with newVar.
Definition: function.cpp:398
VarSet localVars
Local variables, including parameters.
Definition: function.h:102
void removeParam(ShPtr< Variable > param)
Removes the given parameter.
Definition: function.cpp:438
Address getStartAddress() const
Definition: function.cpp:245
ShPtr< Variable > funcVar
Definition: function.h:109
void convertToDeclaration()
Makes the function to be a declaration.
Definition: function.cpp:485
WkPtr< Module > module
The module to which the function belongs.
Definition: function.h:93
void addParam(ShPtr< Variable > var)
Adds a new parameter to the function.
Definition: function.cpp:342
void removeLocalVar(ShPtr< Variable > var)
Removes the given variable from the set of local variables.
Definition: function.cpp:422
const VarVector & getParams() const
Returns function parameters.
Definition: function.cpp:116
void addLocalVar(ShPtr< Variable > var)
Adds a new local variable to the function.
Definition: function.cpp:364
Address getEndAddress() const
Definition: function.cpp:249
void setBody(ShPtr< Statement > newBody)
Sets a new body.
Definition: function.cpp:452
void updateUnderlyingVarType()
Updates the type of the underlying variable.
Definition: function.cpp:618
bool isDeclaration() const
Returns true if the function is just a declaration, false otherwise.
Definition: function.cpp:265
void setLocalVars(VarSet newLocalVars)
Sets a new set of local variables.
Definition: function.cpp:332
void setRetType(ShPtr< Type > newRetType)
Sets a new return type;.
Definition: function.cpp:300
ShPtr< Statement > body
Function body.
Definition: function.h:105
ShPtr< Type > getRetType() const
Returns function type.
Definition: function.cpp:93
void setParams(VarVector newParams)
Sets a new parameter list.
Definition: function.cpp:316
ShPtr< Module > getModule() const
Definition: function.cpp:234
ShPtr< Variable > getAsVar() const
Returns a variable corresponding the function.
Definition: function.cpp:220
void setVarArg(bool isVarArg=true)
Sets the function's status concerning the number of arguments it takes.
Definition: function.cpp:469
virtual void update(ShPtr< Value > subject, ShPtr< Value > arg=nullptr) override
Updates the statement according to the changes of subject.
Definition: function.cpp:553
bool hasParam(ShPtr< Variable > var) const
Returns true if var is a parameter of the function, false otherwise.
Definition: function.cpp:280
std::size_t getNumOfLocalVars(bool includeParams=false) const
Returns the number of local variables.
Definition: function.cpp:186
virtual ShPtr< Value > clone() override
Returns a clone of the value.
Definition: function.cpp:494
std::size_t getNumOfParams() const
Returns the number of parameters.
Definition: function.cpp:162
bool hasLocalVar(ShPtr< Variable > var, bool includeParams=false) const
Returns true if var is a local variable of the function, false otherwise.
Definition: function.cpp:201
bool isVarArg() const
Returns true if the function takes a variable number of arguments, false otherwise.
Definition: function.cpp:257
const std::string & getInitialName() const
Returns the initial name of the function.
Definition: function.cpp:102
ShPtr< Type > getType() const
Returns the type of the function.
Definition: function.cpp:230
ShPtr< Statement > getBody() const
Returns function body.
Definition: function.cpp:211
Function(ShPtr< Module > module, ShPtr< Type >, std::string name, VarVector params, VarSet localVars=VarSet(), ShPtr< Statement > body=nullptr, bool isVarArg=false)
Constructs a new function.
Definition: function.cpp:31
const std::string & getName() const
Returns function name.
Definition: function.cpp:109
void includeParamsIntoLocalVars()
Includes all parameters into the set of local variables.
Definition: function.cpp:627
VarSet getLocalVars(bool includeParams=false) const
Returns local variables of the function.
Definition: function.cpp:171
void setName(const std::string &newName)
Sets a new name.
Definition: function.cpp:309
std::size_t getParamPos(ShPtr< Variable > param) const
Returns the position of the given parameter.
Definition: function.cpp:148
VarVector params
Parameters.
Definition: function.h:99
AddressRange getAddressRange() const
Definition: function.cpp:238
static ShPtr< Function > create(ShPtr< Module > module, ShPtr< Type > retType, std::string name, VarVector params, VarSet localVars=VarSet(), ShPtr< Statement > body=nullptr, bool isVarArg=false)
Constructs a new function.
Definition: function.cpp:516
ShPtr< Type > retType
Return type.
Definition: function.h:96
virtual void accept(Visitor *v) override
Visitor pattern implementation.
Definition: function.cpp:608
ShPtr< Variable > getParam(std::size_t n) const
Returns the n-th parameter.
Definition: function.cpp:132
bool varArg
Definition: function.h:112
void replaceParam(ShPtr< Variable > oldParam, ShPtr< Variable > newParam)
Replaces oldParam with newParam.
Definition: function.cpp:376
virtual bool isEqualTo(ShPtr< Value > otherValue) const override
Returns true if this value is equal to otherValue, false otherwise.
Definition: function.cpp:43
bool isDefinition() const
Returns true if the function is a definition, false otherwise.
Definition: function.cpp:272
A base class of all objects a module can contain.
Definition: value.h:32
A base class of all visitors.
Definition: visitor.h:95
A library providing API for working with back-end IR.
std::weak_ptr< T > WkPtr
An alias for a weak pointer.
Definition: smart_ptr.h:22
std::shared_ptr< T > ShPtr
An alias for a shared pointer.
Definition: smart_ptr.h:18
std::set< ShPtr< Variable > > VarSet
Set of variables.
Definition: types.h:57
std::vector< ShPtr< Variable > > VarVector
Vector of variables.
Definition: types.h:93
Definition: archive_wrapper.h:19
Declarations, aliases, macros, etc. for the use of smart pointers.
Aliases for several useful types.
A base class of all objects a module can contain.