retdec
|
A representation of a program statement. More...
#include <statement.h>
Public Types | |
using | predecessor_iterator = StmtSet::const_iterator |
Predecessor iterator. More... | |
![]() | |
using | ConcreteObserver = Observer< Value, Value > |
A concrete observer. More... | |
using | ObserverPtr = WkPtr< ConcreteObserver > |
A pointer to an observer. More... | |
Public Member Functions | |
virtual void | replace (ShPtr< Expression > oldExpr, ShPtr< Expression > newExpr)=0 |
Replaces all occurrences of oldExpr with newExpr in the current statement. More... | |
virtual bool | isCompound ()=0 |
Returns true if the statement is a compound statement, false otherwise. More... | |
virtual ShPtr< Expression > | asExpression () const =0 |
Returns the statement as an expression. More... | |
ShPtr< Statement > | getParent () const |
Returns the parent of the given statement. More... | |
Address | getAddress () const |
Returns statement's address. More... | |
Successor Management | |
bool | hasSuccessor () const |
Returns true if the statement has a successor, false otherwise. More... | |
ShPtr< Statement > | getSuccessor () const |
Returns the successor of statement. More... | |
void | setSuccessor (ShPtr< Statement > newSucc) |
Sets newSucc as the current statement's successor. More... | |
void | removeSuccessor () |
Removes the successor of the statement (if there is any). More... | |
void | appendStatement (ShPtr< Statement > stmt) |
Appends stmt to the statement. More... | |
Predecessor Management | |
bool | hasPredecessors () const |
Returns true if the statement has any predecessor, false otherwise. More... | |
std::size_t | getNumberOfPredecessors () const |
Returns the number of predecessors of the current statement. More... | |
void | addPredecessor (ShPtr< Statement > stmt) |
Adds a new predecessor: stmt. More... | |
ShPtr< Statement > | getUniquePredecessor () const |
Returns the unique predecessor of the current statement. More... | |
void | removePredecessor (ShPtr< Statement > stmt) |
Removes the predecessor stmt. More... | |
void | removePredecessors (bool onlyNonGoto=false) |
Removes all predecessors of the statement. More... | |
void | prependStatement (ShPtr< Statement > stmt) |
Prepends stmt to the statement. More... | |
predecessor_iterator | predecessor_begin () const |
Returns an iterator to the first predecessor (if any). More... | |
predecessor_iterator | predecessor_end () const |
Returns an iterator past the last predecessor. More... | |
Label | |
bool | hasLabel () const |
Does the statement has a label set? More... | |
std::string | getLabel () const |
Returns the statement's label. More... | |
void | setLabel (const std::string &newLabel) |
Sets a new label of the statement. More... | |
void | removeLabel () |
Removes the statement's label (if any). More... | |
void | transferLabelFrom (ShPtr< Statement > stmt) |
Transfers the label from the given statement to the current statement. More... | |
void | transferLabelTo (ShPtr< Statement > stmt) |
Transfers the label from the current statement to the given statement. More... | |
Goto Targets | |
bool | isGotoTarget () const |
Returns true if the statement is the target of a goto statement, false otherwise. More... | |
void | redirectGotosTo (ShPtr< Statement > stmt) |
Redirects gotos to the statement to the given statement stmt. More... | |
![]() | |
virtual | ~Value ()=default |
virtual ShPtr< Value > | getSelf () override |
Returns a shared pointer of self. More... | |
virtual ShPtr< Value > | clone ()=0 |
Returns a clone of the value. More... | |
virtual bool | isEqualTo (ShPtr< Value > otherValue) const =0 |
Returns true if this value is equal to otherValue, false otherwise. More... | |
std::string | getTextRepr () |
Returns a textual representation of the value. More... | |
![]() | |
virtual void | accept (Visitor *v)=0 |
Visitor pattern implementation. More... | |
![]() | |
void | setMetadata (std::string data) |
Attaches new metadata. More... | |
std::string | getMetadata () const |
Returns the attached metadata. More... | |
bool | hasMetadata () const |
Are there any non-empty metadata? More... | |
![]() | |
Observer ()=default | |
Creates a new observer. More... | |
virtual | ~Observer ()=default |
Destructs the observer. More... | |
virtual void | update (ShPtr< Value > subject, ShPtr< Value > arg=nullptr) |
Subject has changed its state. More... | |
![]() | |
Subject () | |
Creates a new subject. More... | |
virtual | ~Subject ()=default |
Destructs the subject. More... | |
void | addObserver (ObserverPtr observer) |
Adds a new observer to the list of observers. More... | |
void | removeObserver (ObserverPtr observer) |
Removes the selected observer from the list of observers. More... | |
void | removeObservers () |
Removes all observers. More... | |
void | notifyObservers (ShPtr< Value > arg=nullptr) |
Notifies all observers by calling Observer::update() on them. More... | |
Static Public Member Functions | |
static void | removeStatement (ShPtr< Statement > stmt) |
Removes this statement from a block which contains it. More... | |
static void | removeStatementButKeepDebugComment (ShPtr< Statement > stmt) |
Removes the given statement, but keeps its debug comment (if any). More... | |
static bool | areEqualStatements (ShPtr< Statement > stmts1, ShPtr< Statement > stmts2) |
Returns true if stmts1 and stmts are equal, false otherwise. More... | |
static bool | isStatementInStatements (ShPtr< Statement > stmt, ShPtr< Statement > stmts) |
Returns true if statement stmt is in the sequence of statements stmts, false otherwise. More... | |
static void | replaceStatement (ShPtr< Statement > oldStmt, ShPtr< Statement > newStmt) |
Replaces oldStmt with newStmt. More... | |
static void | removeLastStatement (ShPtr< Statement > stmts) |
Removes the last statements in the sequence of statements stmts. More... | |
static ShPtr< Statement > | mergeStatements (ShPtr< Statement > stmt1, ShPtr< Statement > stmt2) |
Merges the two given statements. More... | |
static ShPtr< Statement > | cloneStatements (ShPtr< Statement > stmts) |
Clones the given list of statements. More... | |
static ShPtr< Statement > | getLastStatement (ShPtr< Statement > stmts) |
Returns the last statement in stmts. More... | |
Protected Member Functions | |
Statement (Address a=Address::Undefined) | |
Constructs a new statement. More... | |
![]() | |
Value ()=default | |
![]() | |
Visitable ()=default | |
~Visitable ()=default | |
![]() | |
Metadatable () | |
Constructs a new metadatable object. More... | |
![]() | |
observer_iterator | observer_begin () const |
Returns a constant iterator to the first observer. More... | |
observer_iterator | observer_end () const |
Returns a constant iterator past the last observer. More... | |
Protected Attributes | |
ShPtr< Statement > | succ |
Successor statement. More... | |
StmtSet | preds |
Predecessor statements. More... | |
std::string | label |
Label. More... | |
Address | address |
Address of ASM instruction from which this statement was created from. More... | |
Private Member Functions | |
bool | targetIsCurrentStatement (ShPtr< GotoStmt > gotoStmt) const |
Does gotoStmt target the current statement? More... | |
bool | containsJustGotosToCurrentStatement (const StmtSet &stmts) const |
Returns true if stmts contains just goto statements to the current statement (or it is empty), false otherwise. More... | |
Additional Inherited Members | |
![]() | |
using | ObserverContainer = std::vector< ObserverPtr > |
A container to store observers. More... | |
using | observer_iterator = typename ObserverContainer::const_iterator |
A representation of a program statement.
Each statement can have at most one parent, i.e. it has to be used at most once in the whole program. If you want to have the same statement on multiple places, clone it by calling clone().
Instances of this class have reference object semantics.
Statement successors have to be set in subclasses by calling setSuccessor() (the reason is that shared_from_this()
cannot be called from a constructor).
Other functions for manipulating statements can be found in Utils/IR.h
.
using retdec::llvmir2hll::Statement::predecessor_iterator = StmtSet::const_iterator |
Predecessor iterator.
|
protected |
Constructs a new statement.
Adds a new predecessor: stmt.
[in] | stmt | Statement to be added as a predecessor. |
Notice that this function differs from prependStatement(). Indeed, prependStatement() inserts stmt between the current statement and its predecessor while this function creates a new predecessor. For example, lets have the following sequence of statements:
When we call B->addPredecessor(stmt)
, we get
However, if we call B->prependStatement(stmt)
, we get
Therefore, if you want to add a statement before the current statement, use prependStatement() instead.
By using this function, a statement may have more than one predecessor. This comes handy in terms of goto statements.
Appends stmt to the statement.
[in] | stmt | Statement to be appended. |
If stmt is actually a sequence of statements, it appends all of them so the successor of the last statement in this sequence is the current statement's successor.
Notice that this function differs from setSuccessor(). Indeed, setSuccessor() just sets stmt as the statement's successor, discarding the original successor, while this function places stmt between the current statements and its successor. For example, lets have the following sequence of statements:
When we call A->setSuccessor(stmt)
, we get
However, if we call A->appendStatement(stmt)
, we get
|
static |
Returns true
if stmts1 and stmts are equal, false
otherwise.
stmts1 and stmts2 may be sequences of statements, in which case their successors are also checked.
If stmts1 and stmts2 are the null pointers, true
is returned.
This function recursively calls itself.
|
pure virtual |
Returns the statement as an expression.
If the statement cannot be converted into an expression, the null pointer is returned.
Parts of the statement are not cloned (if you want a clone, call clone()
on the returned expression).
Implemented in retdec::llvmir2hll::WhileLoopStmt, retdec::llvmir2hll::VarDefStmt, retdec::llvmir2hll::UnreachableStmt, retdec::llvmir2hll::UForLoopStmt, retdec::llvmir2hll::SwitchStmt, retdec::llvmir2hll::ReturnStmt, retdec::llvmir2hll::IfStmt, retdec::llvmir2hll::GotoStmt, retdec::llvmir2hll::ForLoopStmt, retdec::llvmir2hll::EmptyStmt, retdec::llvmir2hll::ContinueStmt, retdec::llvmir2hll::CallStmt, retdec::llvmir2hll::BreakStmt, and retdec::llvmir2hll::AssignStmt.
|
static |
Clones the given list of statements.
Clones the given list of statements, one by one, by calling clone()
on every one of them. Successors are properly set. If stmts is the null pointer, the null pointer is returned.
Note that Statement::clone()
doesn't clone successors. Hence, if you want to clone a sequence of statements, including successors, use this function.
|
private |
Returns true
if stmts contains just goto statements to the current statement (or it is empty), false
otherwise.
Address retdec::llvmir2hll::Statement::getAddress | ( | ) | const |
Returns statement's address.
std::string retdec::llvmir2hll::Statement::getLabel | ( | ) | const |
Returns the statement's label.
|
static |
Returns the last statement in stmts.
[in] | stmts | Sequence of statements. |
If stmts is null, then it returns the null pointer.
std::size_t retdec::llvmir2hll::Statement::getNumberOfPredecessors | ( | ) | const |
Returns the number of predecessors of the current statement.
Recall that by using addPredecessor(), a statement may have more than one predecessor. This is the case when goto statements are used.
Returns the parent of the given statement.
A parent of a statement stmt
is a statement which directly contains it. For example, consider the following code:
In this example, statements (1), (2), and (5) do not have any parents, and statement (2) is the parent of statements (3) and (4).
Returns the successor of statement.
If there is no successor, it returns the null pointer.
Returns the unique predecessor of the current statement.
If the statement has no predecessors, or if there is more than one predecessor, the null pointer is returned.
bool retdec::llvmir2hll::Statement::hasLabel | ( | ) | const |
Does the statement has a label set?
bool retdec::llvmir2hll::Statement::hasPredecessors | ( | ) | const |
Returns true
if the statement has any predecessor, false
otherwise.
bool retdec::llvmir2hll::Statement::hasSuccessor | ( | ) | const |
Returns true
if the statement has a successor, false
otherwise.
|
pure virtual |
Returns true
if the statement is a compound statement, false
otherwise.
A compound statement is a statement with nested statements, e.g. an if statement, a switch statement, a while loop, and a for loop.
Implemented in retdec::llvmir2hll::WhileLoopStmt, retdec::llvmir2hll::VarDefStmt, retdec::llvmir2hll::UnreachableStmt, retdec::llvmir2hll::UForLoopStmt, retdec::llvmir2hll::SwitchStmt, retdec::llvmir2hll::ReturnStmt, retdec::llvmir2hll::IfStmt, retdec::llvmir2hll::GotoStmt, retdec::llvmir2hll::ForLoopStmt, retdec::llvmir2hll::EmptyStmt, retdec::llvmir2hll::ContinueStmt, retdec::llvmir2hll::CallStmt, retdec::llvmir2hll::BreakStmt, and retdec::llvmir2hll::AssignStmt.
bool retdec::llvmir2hll::Statement::isGotoTarget | ( | ) | const |
Returns true
if the statement is the target of a goto statement, false
otherwise.
|
static |
Returns true
if statement stmt is in the sequence of statements stmts, false
otherwise.
[in] | stmt | Statement to be checked. |
[in] | stmts | Sequence of statements (may be empty). |
Only successors of statements in stmts are searched, i.e. if there is a compound statement, no search in the nested statements is done.
Precondition:
|
static |
Merges the two given statements.
[in] | stmt1 | First statement to be merged. |
[in] | stmt2 | Second statement to be merged. |
If stmt1 contains a, b, c, ..., and stmt2 contains 0, 1, 2, ..., then the result will contain a, b, c, ..., 0, 1, 2, ...
Statement::predecessor_iterator retdec::llvmir2hll::Statement::predecessor_begin | ( | ) | const |
Returns an iterator to the first predecessor (if any).
Statement::predecessor_iterator retdec::llvmir2hll::Statement::predecessor_end | ( | ) | const |
Returns an iterator past the last predecessor.
Prepends stmt to the statement.
[in] | stmt | Statement to be prepended. |
Example: Consider the following situation:
The following call in pseudocode
results in
If stmt is actually a sequence of statements, it prepends all of them so the successor of the last statement in this sequence is the statement on which this function is called.
If you want to add another predecessor while leaving the original predecessor of the current statement untouched, use addPredecessor() instead.
Redirects gotos to the statement to the given statement stmt.
If the statement is not a goto target, this function does nothing.
Labels are also transferred.
void retdec::llvmir2hll::Statement::removeLabel | ( | ) |
Removes the statement's label (if any).
Removes the last statements in the sequence of statements stmts.
Removes the predecessor stmt.
[in] | stmt | Predecessor to be removed. |
If stmt is not a predecessor of the current statement, this function does nothing.
void retdec::llvmir2hll::Statement::removePredecessors | ( | bool | onlyNonGoto = false | ) |
Removes all predecessors of the statement.
[in] | onlyNonGoto | Removes only non-goto statements. |
It only removes them from being the current statement's predecessors, it doesn't delete them.
Removes this statement from a block which contains it.
[in] | stmt | Statement to be removed. |
After calling this function, stmt will have no predecessors and no successor.
|
static |
Removes the given statement, but keeps its debug comment (if any).
An empty statement may be introduced if either there is no successor of stmt or its successor already has its debug comment.
After calling this function, stmt will have no predecessors and no successor.
void retdec::llvmir2hll::Statement::removeSuccessor | ( | ) |
Removes the successor of the statement (if there is any).
Note that the actual successor is not removed, only after calling this function, the statement will no longer have a successor.
|
pure virtual |
Replaces all occurrences of oldExpr with newExpr in the current statement.
[in] | oldExpr | Old expression to be replaced. |
[in] | newExpr | Replacement. |
In compound statements, their body is left untouched, i.e. this function doesn't replace expressions in the bodies of statements.
Implemented in retdec::llvmir2hll::WhileLoopStmt, retdec::llvmir2hll::VarDefStmt, retdec::llvmir2hll::UnreachableStmt, retdec::llvmir2hll::UForLoopStmt, retdec::llvmir2hll::SwitchStmt, retdec::llvmir2hll::ReturnStmt, retdec::llvmir2hll::IfStmt, retdec::llvmir2hll::GotoStmt, retdec::llvmir2hll::ForLoopStmt, retdec::llvmir2hll::EmptyStmt, retdec::llvmir2hll::ContinueStmt, retdec::llvmir2hll::CallStmt, retdec::llvmir2hll::BreakStmt, and retdec::llvmir2hll::AssignStmt.
|
static |
Replaces oldStmt with newStmt.
[in] | oldStmt | Old statement to be replaced. |
[in] | newStmt | Replacement. |
If oldStmt has a successor and newStmt is the null pointer, then the successor of oldStmt is (of course) lost.
newStmt can be a list of statements, not necessary just a single statement.
After calling this function, oldStmt will have no predecessors and no successor.
void retdec::llvmir2hll::Statement::setLabel | ( | const std::string & | newLabel | ) |
Sets a new label of the statement.
Sets newSucc as the current statement's successor.
Example: Consider the following situation:
The following call in pseudocode
results in
The original successor (if any) is discarded. If you do not want it to be discarded, use appendStatement() instead.
|
private |
Does gotoStmt target the current statement?
Transfers the label from the given statement to the current statement.
Transfers the label from the current statement to the given statement.
|
protected |
Address of ASM instruction from which this statement was created from.
|
protected |
Label.
|
protected |
Predecessor statements.