retdec
if_stmt.h
Go to the documentation of this file.
1 
7 #ifndef RETDEC_LLVMIR2HLL_IR_IF_STMT_H
8 #define RETDEC_LLVMIR2HLL_IR_IF_STMT_H
9 
10 #include <list>
11 #include <utility>
12 
15 
16 namespace retdec {
17 namespace llvmir2hll {
18 
19 class Expression;
20 class Visitor;
21 
28 class IfStmt final: public Statement {
29 public:
31  using IfClause = std::pair<ShPtr<Expression>, ShPtr<Statement>>;
32 
34  // Note to developers: We have to use std::list as the underlying container
35  // due to the requirements of several member functions.
36  using IfClauseList = std::list<IfClause>;
37 
42  using clause_iterator = IfClauseList::const_iterator;
43 
44 public:
47 
48  virtual ShPtr<Value> clone() override;
49  virtual bool isEqualTo(ShPtr<Value> otherValue) const override;
50  virtual bool isCompound() override { return true; }
51  virtual void replace(ShPtr<Expression> oldExpr, ShPtr<Expression> newExpr) override;
52  virtual ShPtr<Expression> asExpression() const override;
53 
56  virtual void update(ShPtr<Value> subject, ShPtr<Value> arg = nullptr) override;
58 
63 
66  bool hasClauses() const;
67  bool hasIfClause() const;
70  void setFirstIfCond(ShPtr<Expression> newCond);
71  void setFirstIfBody(ShPtr<Statement> newBody);
72  bool hasElseIfClauses() const;
74  bool hasElseClause() const;
76  void removeElseClause();
78 
81  virtual void accept(Visitor *v) override;
83 
84 private:
85  // Since instances are created by calling the static function create(), the
86  // constructor can be private.
89 
90 private:
93 
96 };
97 
98 } // namespace llvmir2hll
99 } // namespace retdec
100 
101 #endif
Definition: address.h:21
static const uint64_t Undefined
Definition: address.h:47
An if/else-if/else statement.
Definition: if_stmt.h:28
clause_iterator clause_end() const
Returns an iterator past the last else-if clause.
Definition: if_stmt.cpp:242
std::list< IfClause > IfClauseList
A list of if clauses.
Definition: if_stmt.h:36
clause_iterator removeClause(clause_iterator clauseIterator)
Removes the given clause, specified by an iterator.
Definition: if_stmt.cpp:163
void removeElseClause()
Removes the else clause (if any).
Definition: if_stmt.cpp:214
bool hasElseIfClauses() const
Returns true if there is at least one else-if clause, false otherwise.
Definition: if_stmt.cpp:130
IfClauseList::const_iterator clause_iterator
Definition: if_stmt.h:42
std::pair< ShPtr< Expression >, ShPtr< Statement > > IfClause
If clause (condition and body).
Definition: if_stmt.h:31
ShPtr< Statement > getFirstIfBody() const
Returns the body of the first if clause in the statement.
Definition: if_stmt.cpp:349
ShPtr< Statement > getElseClause() const
Returns the else clause (if any), the null pointer otherwise.
Definition: if_stmt.cpp:249
bool hasIfClause() const
Returns true if the statement has the if clause, false otherwise.
Definition: if_stmt.cpp:186
bool hasElseClause() const
Returns true if this if statement has an else clause, false otherwise.
Definition: if_stmt.cpp:222
virtual void replace(ShPtr< Expression > oldExpr, ShPtr< Expression > newExpr) override
Replaces all occurrences of oldExpr with newExpr in the current statement.
Definition: if_stmt.cpp:81
clause_iterator clause_begin() const
Returns an iterator to the first if clause (if cond then body).
Definition: if_stmt.cpp:232
virtual bool isEqualTo(ShPtr< Value > otherValue) const override
Returns true if this value is equal to otherValue, false otherwise.
Definition: if_stmt.cpp:50
void setElseClause(ShPtr< Statement > body)
Sets the else clause (else body).
Definition: if_stmt.cpp:197
static ShPtr< IfStmt > create(ShPtr< Expression > cond, ShPtr< Statement > body, ShPtr< Statement > succ=nullptr, Address a=Address::Undefined)
Constructs a new if statement.
Definition: if_stmt.cpp:264
virtual ShPtr< Value > clone() override
Returns a clone of the value.
Definition: if_stmt.cpp:24
void setFirstIfCond(ShPtr< Expression > newCond)
Sets a new condition of the first if clause.
Definition: if_stmt.cpp:362
IfClauseList ifClauseList
A list of if clauses.
Definition: if_stmt.h:92
ShPtr< Statement > elseClause
The else clause (if any).
Definition: if_stmt.h:95
void addClause(ShPtr< Expression > cond, ShPtr< Statement > body)
Adds a new clause ([else] if cond then body).
Definition: if_stmt.cpp:113
IfStmt(ShPtr< Expression > cond, ShPtr< Statement > body, Address a=Address::Undefined)
Constructs a new if/else-if/else statement.
Definition: if_stmt.cpp:21
virtual ShPtr< Expression > asExpression() const override
Returns the statement as an expression.
Definition: if_stmt.cpp:94
bool hasClauses() const
Returns true if there is at least one clause, false otherwise.
Definition: if_stmt.cpp:176
virtual void accept(Visitor *v) override
Visitor pattern implementation.
Definition: if_stmt.cpp:328
void setFirstIfBody(ShPtr< Statement > newBody)
Sets a new body of the first if clause.
Definition: if_stmt.cpp:376
virtual void update(ShPtr< Value > subject, ShPtr< Value > arg=nullptr) override
Updates the statement according to the changes of subject.
Definition: if_stmt.cpp:301
ShPtr< Expression > getFirstIfCond() const
Returns the condition of the first if clause in the statement.
Definition: if_stmt.cpp:337
virtual bool isCompound() override
Returns true if the statement is a compound statement, false otherwise.
Definition: if_stmt.h:50
A representation of a program statement.
Definition: statement.h:39
ShPtr< Statement > succ
Successor statement.
Definition: statement.h:141
A base class of all visitors.
Definition: visitor.h:95
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
Definition: archive_wrapper.h:19
Declarations, aliases, macros, etc. for the use of smart pointers.
A representation of a program statement.