retdec
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | List of all members
retdec::llvmir2hll::Statement Class Referenceabstract

A representation of a program statement. More...

#include <statement.h>

Inheritance diagram for retdec::llvmir2hll::Statement:
Inheritance graph
[legend]
Collaboration diagram for retdec::llvmir2hll::Statement:
Collaboration graph
[legend]

Public Types

using predecessor_iterator = StmtSet::const_iterator
 Predecessor iterator. More...
 
- Public Types inherited from retdec::llvmir2hll::Subject< Value >
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< ExpressionasExpression () const =0
 Returns the statement as an expression. More...
 
ShPtr< StatementgetParent () 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< StatementgetSuccessor () 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< StatementgetUniquePredecessor () 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...
 
- Public Member Functions inherited from retdec::llvmir2hll::Value
virtual ~Value ()=default
 
virtual ShPtr< ValuegetSelf () override
 Returns a shared pointer of self. More...
 
virtual ShPtr< Valueclone ()=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...
 
- Public Member Functions inherited from retdec::llvmir2hll::Visitable
virtual void accept (Visitor *v)=0
 Visitor pattern implementation. More...
 
- Public Member Functions inherited from retdec::llvmir2hll::Metadatable< std::string >
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...
 
- Public Member Functions inherited from retdec::llvmir2hll::Observer< Value >
 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...
 
- Public Member Functions inherited from retdec::llvmir2hll::Subject< Value >
 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< StatementmergeStatements (ShPtr< Statement > stmt1, ShPtr< Statement > stmt2)
 Merges the two given statements. More...
 
static ShPtr< StatementcloneStatements (ShPtr< Statement > stmts)
 Clones the given list of statements. More...
 
static ShPtr< StatementgetLastStatement (ShPtr< Statement > stmts)
 Returns the last statement in stmts. More...
 

Protected Member Functions

 Statement (Address a=Address::Undefined)
 Constructs a new statement. More...
 
- Protected Member Functions inherited from retdec::llvmir2hll::Value
 Value ()=default
 
- Protected Member Functions inherited from retdec::llvmir2hll::Visitable
 Visitable ()=default
 
 ~Visitable ()=default
 
- Protected Member Functions inherited from retdec::llvmir2hll::Metadatable< std::string >
 Metadatable ()
 Constructs a new metadatable object. More...
 
- Protected Member Functions inherited from retdec::llvmir2hll::Subject< Value >
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< Statementsucc
 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

- Protected Types inherited from retdec::llvmir2hll::Subject< Value >
using ObserverContainer = std::vector< ObserverPtr >
 A container to store observers. More...
 
using observer_iterator = typename ObserverContainer::const_iterator
 

Detailed Description

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.

Member Typedef Documentation

◆ predecessor_iterator

using retdec::llvmir2hll::Statement::predecessor_iterator = StmtSet::const_iterator

Predecessor iterator.

Constructor & Destructor Documentation

◆ Statement()

retdec::llvmir2hll::Statement::Statement ( Address  a = Address::Undefined)
protected

Constructs a new statement.

Member Function Documentation

◆ addPredecessor()

void retdec::llvmir2hll::Statement::addPredecessor ( ShPtr< Statement stmt)

Adds a new predecessor: stmt.

Parameters
[in]stmtStatement 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:

A --> B

When we call B->addPredecessor(stmt), we get

A --> B
^
|
stmt --

However, if we call B->prependStatement(stmt), we get

A --> stmt -> B

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.

◆ appendStatement()

void retdec::llvmir2hll::Statement::appendStatement ( ShPtr< Statement stmt)

Appends stmt to the statement.

Parameters
[in]stmtStatement 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:

A --> B

When we call A->setSuccessor(stmt), we get

A --> stmt

However, if we call A->appendStatement(stmt), we get

A --> stmt -> B
Preconditions
  • stmt is non-null

◆ areEqualStatements()

bool retdec::llvmir2hll::Statement::areEqualStatements ( ShPtr< Statement stmts1,
ShPtr< Statement stmts2 
)
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.

◆ asExpression()

virtual ShPtr<Expression> retdec::llvmir2hll::Statement::asExpression ( ) const
pure virtual

◆ cloneStatements()

ShPtr< Statement > retdec::llvmir2hll::Statement::cloneStatements ( ShPtr< Statement stmts)
static

Clones the given list of statements.

Returns
Cloned list of statements appearing in stmts.

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.

◆ containsJustGotosToCurrentStatement()

bool retdec::llvmir2hll::Statement::containsJustGotosToCurrentStatement ( const StmtSet stmts) const
private

Returns true if stmts contains just goto statements to the current statement (or it is empty), false otherwise.

◆ getAddress()

Address retdec::llvmir2hll::Statement::getAddress ( ) const

Returns statement's address.

◆ getLabel()

std::string retdec::llvmir2hll::Statement::getLabel ( ) const

Returns the statement's label.

◆ getLastStatement()

ShPtr< Statement > retdec::llvmir2hll::Statement::getLastStatement ( ShPtr< Statement stmts)
static

Returns the last statement in stmts.

Parameters
[in]stmtsSequence of statements.

If stmts is null, then it returns the null pointer.

◆ getNumberOfPredecessors()

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.

◆ getParent()

ShPtr< Statement > retdec::llvmir2hll::Statement::getParent ( ) const

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:

def testStr():
i = 0 (1)
while str[i] != 0: (2)
i = i + 1 (3)
printf("test: %d", i) (4)
printf("end") (5)

In this example, statements (1), (2), and (5) do not have any parents, and statement (2) is the parent of statements (3) and (4).

◆ getSuccessor()

ShPtr< Statement > retdec::llvmir2hll::Statement::getSuccessor ( ) const

Returns the successor of statement.

If there is no successor, it returns the null pointer.

◆ getUniquePredecessor()

ShPtr< Statement > retdec::llvmir2hll::Statement::getUniquePredecessor ( ) const

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.

◆ hasLabel()

bool retdec::llvmir2hll::Statement::hasLabel ( ) const

Does the statement has a label set?

◆ hasPredecessors()

bool retdec::llvmir2hll::Statement::hasPredecessors ( ) const

Returns true if the statement has any predecessor, false otherwise.

◆ hasSuccessor()

bool retdec::llvmir2hll::Statement::hasSuccessor ( ) const

Returns true if the statement has a successor, false otherwise.

◆ isCompound()

virtual bool retdec::llvmir2hll::Statement::isCompound ( )
pure virtual

◆ isGotoTarget()

bool retdec::llvmir2hll::Statement::isGotoTarget ( ) const

Returns true if the statement is the target of a goto statement, false otherwise.

◆ isStatementInStatements()

bool retdec::llvmir2hll::Statement::isStatementInStatements ( ShPtr< Statement stmt,
ShPtr< Statement stmts 
)
static

Returns true if statement stmt is in the sequence of statements stmts, false otherwise.

Parameters
[in]stmtStatement to be checked.
[in]stmtsSequence 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:

  • stmt is non-null

◆ mergeStatements()

ShPtr< Statement > retdec::llvmir2hll::Statement::mergeStatements ( ShPtr< Statement stmt1,
ShPtr< Statement stmt2 
)
static

Merges the two given statements.

Parameters
[in]stmt1First statement to be merged.
[in]stmt2Second statement to be merged.
Returns
Merged stmt1 and stmt2.

If stmt1 contains a, b, c, ..., and stmt2 contains 0, 1, 2, ..., then the result will contain a, b, c, ..., 0, 1, 2, ...

◆ predecessor_begin()

Statement::predecessor_iterator retdec::llvmir2hll::Statement::predecessor_begin ( ) const

Returns an iterator to the first predecessor (if any).

◆ predecessor_end()

Statement::predecessor_iterator retdec::llvmir2hll::Statement::predecessor_end ( ) const

Returns an iterator past the last predecessor.

◆ prependStatement()

void retdec::llvmir2hll::Statement::prependStatement ( ShPtr< Statement stmt)

Prepends stmt to the statement.

Parameters
[in]stmtStatement to be prepended.

Example: Consider the following situation:

int b = 5;
return a; // <-- this stmt

The following call in pseudocode

stmt->prependStatement(`int a = b;`)

results in

int b = 5;
int a = b;
return a; // <-- this stmt

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.

Preconditions
  • stmt is non-null

◆ redirectGotosTo()

void retdec::llvmir2hll::Statement::redirectGotosTo ( ShPtr< Statement stmt)

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.

Preconditions
  • stmt is non-null

◆ removeLabel()

void retdec::llvmir2hll::Statement::removeLabel ( )

Removes the statement's label (if any).

◆ removeLastStatement()

void retdec::llvmir2hll::Statement::removeLastStatement ( ShPtr< Statement stmts)
static

Removes the last statements in the sequence of statements stmts.

Preconditions
  • stmts is non-null

◆ removePredecessor()

void retdec::llvmir2hll::Statement::removePredecessor ( ShPtr< Statement stmt)

Removes the predecessor stmt.

Parameters
[in]stmtPredecessor to be removed.

If stmt is not a predecessor of the current statement, this function does nothing.

◆ removePredecessors()

void retdec::llvmir2hll::Statement::removePredecessors ( bool  onlyNonGoto = false)

Removes all predecessors of the statement.

Parameters
[in]onlyNonGotoRemoves only non-goto statements.

It only removes them from being the current statement's predecessors, it doesn't delete them.

◆ removeStatement()

void retdec::llvmir2hll::Statement::removeStatement ( ShPtr< Statement stmt)
static

Removes this statement from a block which contains it.

Parameters
[in]stmtStatement to be removed.

After calling this function, stmt will have no predecessors and no successor.

Preconditions
  • stmt is non-null

◆ removeStatementButKeepDebugComment()

void retdec::llvmir2hll::Statement::removeStatementButKeepDebugComment ( ShPtr< Statement stmt)
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.

◆ removeSuccessor()

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.

◆ replace()

virtual void retdec::llvmir2hll::Statement::replace ( ShPtr< Expression oldExpr,
ShPtr< Expression newExpr 
)
pure virtual

Replaces all occurrences of oldExpr with newExpr in the current statement.

Parameters
[in]oldExprOld expression to be replaced.
[in]newExprReplacement.

In compound statements, their body is left untouched, i.e. this function doesn't replace expressions in the bodies of statements.

Preconditions
  • oldExpr is non-null

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.

◆ replaceStatement()

void retdec::llvmir2hll::Statement::replaceStatement ( ShPtr< Statement oldStmt,
ShPtr< Statement newStmt 
)
static

Replaces oldStmt with newStmt.

Parameters
[in]oldStmtOld statement to be replaced.
[in]newStmtReplacement.
Preconditions
  • oldStmt is non-null

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.

◆ setLabel()

void retdec::llvmir2hll::Statement::setLabel ( const std::string &  newLabel)

Sets a new label of the statement.

Preconditions
  • newLabel is non-empty

◆ setSuccessor()

void retdec::llvmir2hll::Statement::setSuccessor ( ShPtr< Statement newSucc)

Sets newSucc as the current statement's successor.

Example: Consider the following situation:

int a = 5; // <-- this stmt

The following call in pseudocode

stmt->setSuccessor(`return a`)

results in

int a = 5; // <-- this stmt
return a;

The original successor (if any) is discarded. If you do not want it to be discarded, use appendStatement() instead.

◆ targetIsCurrentStatement()

bool retdec::llvmir2hll::Statement::targetIsCurrentStatement ( ShPtr< GotoStmt gotoStmt) const
private

Does gotoStmt target the current statement?

◆ transferLabelFrom()

void retdec::llvmir2hll::Statement::transferLabelFrom ( ShPtr< Statement stmt)

Transfers the label from the given statement to the current statement.

◆ transferLabelTo()

void retdec::llvmir2hll::Statement::transferLabelTo ( ShPtr< Statement stmt)

Transfers the label from the current statement to the given statement.

Member Data Documentation

◆ address

Address retdec::llvmir2hll::Statement::address
protected

Address of ASM instruction from which this statement was created from.

◆ label

std::string retdec::llvmir2hll::Statement::label
protected

Label.

◆ preds

StmtSet retdec::llvmir2hll::Statement::preds
protected

Predecessor statements.

◆ succ

ShPtr<Statement> retdec::llvmir2hll::Statement::succ
protected

Successor statement.


The documentation for this class was generated from the following files: