retdec
statement.h
Go to the documentation of this file.
1 
7 #ifndef RETDEC_LLVMIR2HLL_IR_STATEMENT_H
8 #define RETDEC_LLVMIR2HLL_IR_STATEMENT_H
9 
10 #include <cstddef>
11 #include <set>
12 #include <string>
13 
17 
18 namespace retdec {
19 namespace llvmir2hll {
20 
21 class GotoStmt;
22 
39 class Statement: public Value {
40 public:
42  using predecessor_iterator = StmtSet::const_iterator;
43 
44 public:
58  virtual void replace(ShPtr<Expression> oldExpr,
59  ShPtr<Expression> newExpr) = 0;
60 
68  virtual bool isCompound() = 0;
69 
79  virtual ShPtr<Expression> asExpression() const = 0;
80 
83  bool hasSuccessor() const;
85  void setSuccessor(ShPtr<Statement> newSucc);
86  void removeSuccessor();
89 
92  bool hasPredecessors() const;
93  std::size_t getNumberOfPredecessors() const;
97  void removePredecessors(bool onlyNonGoto = false);
99 
103 
106  bool hasLabel() const;
107  std::string getLabel() const;
108  void setLabel(const std::string &newLabel);
109  void removeLabel();
113 
114  ShPtr<Statement> getParent() const;
115  Address getAddress() const;
116 
119  bool isGotoTarget() const;
122 
123  static void removeStatement(ShPtr<Statement> stmt);
125  static bool areEqualStatements(ShPtr<Statement> stmts1, ShPtr<Statement> stmts2);
126  static bool isStatementInStatements(ShPtr<Statement> stmt,
127  ShPtr<Statement> stmts);
128  static void replaceStatement(ShPtr<Statement> oldStmt,
129  ShPtr<Statement> newStmt);
130  static void removeLastStatement(ShPtr<Statement> stmts);
132  ShPtr<Statement> stmt1, ShPtr<Statement> stmt2);
135 
136 protected:
138 
139 protected:
142 
145 
147  std::string label;
148 
151 
152 private:
153  bool targetIsCurrentStatement(ShPtr<GotoStmt> gotoStmt) const;
154  bool containsJustGotosToCurrentStatement(const StmtSet &stmts) const;
155 };
156 
157 } // namespace llvmir2hll
158 } // namespace retdec
159 
160 #endif
Definition: address.h:21
static const uint64_t Undefined
Definition: address.h:47
A representation of a program statement.
Definition: statement.h:39
ShPtr< Statement > getUniquePredecessor() const
Returns the unique predecessor of the current statement.
Definition: statement.cpp:257
Address getAddress() const
Returns statement's address.
Definition: statement.cpp:679
bool isGotoTarget() const
Returns true if the statement is the target of a goto statement, false otherwise.
Definition: statement.cpp:687
static void removeLastStatement(ShPtr< Statement > stmts)
Removes the last statements in the sequence of statements stmts.
Definition: statement.cpp:438
static ShPtr< Statement > mergeStatements(ShPtr< Statement > stmt1, ShPtr< Statement > stmt2)
Merges the two given statements.
Definition: statement.cpp:520
void removeSuccessor()
Removes the successor of the statement (if there is any).
Definition: statement.cpp:78
void addPredecessor(ShPtr< Statement > stmt)
Adds a new predecessor: stmt.
Definition: statement.cpp:237
bool targetIsCurrentStatement(ShPtr< GotoStmt > gotoStmt) const
Does gotoStmt target the current statement?
Definition: statement.cpp:776
void setSuccessor(ShPtr< Statement > newSucc)
Sets newSucc as the current statement's successor.
Definition: statement.cpp:57
void prependStatement(ShPtr< Statement > stmt)
Prepends stmt to the statement.
Definition: statement.cpp:129
std::string getLabel() const
Returns the statement's label.
Definition: statement.cpp:734
bool hasLabel() const
Does the statement has a label set?
Definition: statement.cpp:727
void transferLabelTo(ShPtr< Statement > stmt)
Transfers the label from the current statement to the given statement.
Definition: statement.cpp:768
void setLabel(const std::string &newLabel)
Sets a new label of the statement.
Definition: statement.cpp:751
std::size_t getNumberOfPredecessors() const
Returns the number of predecessors of the current statement.
Definition: statement.cpp:247
virtual void replace(ShPtr< Expression > oldExpr, ShPtr< Expression > newExpr)=0
Replaces all occurrences of oldExpr with newExpr in the current statement.
bool containsJustGotosToCurrentStatement(const StmtSet &stmts) const
Returns true if stmts contains just goto statements to the current statement (or it is empty),...
Definition: statement.cpp:792
static bool areEqualStatements(ShPtr< Statement > stmts1, ShPtr< Statement > stmts2)
Returns true if stmts1 and stmts are equal, false otherwise.
Definition: statement.cpp:387
Statement(Address a=Address::Undefined)
Constructs a new statement.
Definition: statement.cpp:33
void removePredecessor(ShPtr< Statement > stmt)
Removes the predecessor stmt.
Definition: statement.cpp:589
static void replaceStatement(ShPtr< Statement > oldStmt, ShPtr< Statement > newStmt)
Replaces oldStmt with newStmt.
Definition: statement.cpp:462
ShPtr< Statement > succ
Successor statement.
Definition: statement.h:141
ShPtr< Statement > getSuccessor() const
Returns the successor of statement.
Definition: statement.cpp:87
std::string label
Label.
Definition: statement.h:147
Address address
Address of ASM instruction from which this statement was created from.
Definition: statement.h:150
static void removeStatementButKeepDebugComment(ShPtr< Statement > stmt)
Removes the given statement, but keeps its debug comment (if any).
Definition: statement.cpp:353
ShPtr< Statement > getParent() const
Returns the parent of the given statement.
Definition: statement.cpp:642
static bool isStatementInStatements(ShPtr< Statement > stmt, ShPtr< Statement > stmts)
Returns true if statement stmt is in the sequence of statements stmts, false otherwise.
Definition: statement.cpp:421
void removeLabel()
Removes the statement's label (if any).
Definition: statement.cpp:741
bool hasPredecessors() const
Returns true if the statement has any predecessor, false otherwise.
Definition: statement.cpp:204
predecessor_iterator predecessor_begin() const
Returns an iterator to the first predecessor (if any).
Definition: statement.cpp:267
StmtSet preds
Predecessor statements.
Definition: statement.h:144
virtual bool isCompound()=0
Returns true if the statement is a compound statement, false otherwise.
static ShPtr< Statement > getLastStatement(ShPtr< Statement > stmts)
Returns the last statement in stmts.
Definition: statement.cpp:569
void redirectGotosTo(ShPtr< Statement > stmt)
Redirects gotos to the statement to the given statement stmt.
Definition: statement.cpp:708
predecessor_iterator predecessor_end() const
Returns an iterator past the last predecessor.
Definition: statement.cpp:274
void removePredecessors(bool onlyNonGoto=false)
Removes all predecessors of the statement.
Definition: statement.cpp:601
virtual ShPtr< Expression > asExpression() const =0
Returns the statement as an expression.
StmtSet::const_iterator predecessor_iterator
Predecessor iterator.
Definition: statement.h:42
void transferLabelFrom(ShPtr< Statement > stmt)
Transfers the label from the given statement to the current statement.
Definition: statement.cpp:760
void appendStatement(ShPtr< Statement > stmt)
Appends stmt to the statement.
Definition: statement.cpp:187
static ShPtr< Statement > cloneStatements(ShPtr< Statement > stmts)
Clones the given list of statements.
Definition: statement.cpp:548
static void removeStatement(ShPtr< Statement > stmt)
Removes this statement from a block which contains it.
Definition: statement.cpp:289
bool hasSuccessor() const
Returns true if the statement has a successor, false otherwise.
Definition: statement.cpp:94
A base class of all objects a module can contain.
Definition: value.h:32
A library providing API for working with back-end IR.
std::shared_ptr< T > ShPtr
An alias for a shared pointer.
Definition: smart_ptr.h:18
std::set< ShPtr< Statement > > StmtSet
Set of statements.
Definition: types.h:69
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.