retdec
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
retdec::llvmir2hll::Function Class Referencefinal

A representation of a function. More...

#include <function.h>

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

Public Member Functions

virtual ShPtr< Valueclone () override
 Returns a clone of the value. More...
 
virtual bool isEqualTo (ShPtr< Value > otherValue) const override
 Returns true if this value is equal to otherValue, false otherwise. More...
 
ShPtr< TypegetRetType () const
 Returns function type. More...
 
const std::string & getInitialName () const
 Returns the initial name of the function. More...
 
const std::string & getName () const
 Returns function name. More...
 
const VarVectorgetParams () const
 Returns function parameters. More...
 
ShPtr< VariablegetParam (std::size_t n) const
 Returns the n-th parameter. More...
 
std::size_t getParamPos (ShPtr< Variable > param) const
 Returns the position of the given parameter. More...
 
std::size_t getNumOfParams () const
 Returns the number of parameters. More...
 
VarSet getLocalVars (bool includeParams=false) const
 Returns local variables of the function. More...
 
std::size_t getNumOfLocalVars (bool includeParams=false) const
 Returns the number of local variables. More...
 
bool hasLocalVar (ShPtr< Variable > var, bool includeParams=false) const
 Returns true if var is a local variable of the function, false otherwise. More...
 
ShPtr< StatementgetBody () const
 Returns function body. More...
 
ShPtr< VariablegetAsVar () const
 Returns a variable corresponding the function. More...
 
ShPtr< TypegetType () const
 Returns the type of the function. More...
 
ShPtr< ModulegetModule () const
 
AddressRange getAddressRange () const
 
Address getStartAddress () const
 
Address getEndAddress () const
 
bool isVarArg () const
 Returns true if the function takes a variable number of arguments, false otherwise. More...
 
bool isDeclaration () const
 Returns true if the function is just a declaration, false otherwise. More...
 
bool isDefinition () const
 Returns true if the function is a definition, false otherwise. More...
 
bool hasParam (ShPtr< Variable > var) const
 Returns true if var is a parameter of the function, false otherwise. More...
 
bool hasParam (std::size_t n) const
 Returns true if the function has an n-th parameter, false otherwise. More...
 
void setRetType (ShPtr< Type > newRetType)
 Sets a new return type;. More...
 
void setName (const std::string &newName)
 Sets a new name. More...
 
void setParams (VarVector newParams)
 Sets a new parameter list. More...
 
void setLocalVars (VarSet newLocalVars)
 Sets a new set of local variables. More...
 
void addParam (ShPtr< Variable > var)
 Adds a new parameter to the function. More...
 
void addLocalVar (ShPtr< Variable > var)
 Adds a new local variable to the function. More...
 
void replaceParam (ShPtr< Variable > oldParam, ShPtr< Variable > newParam)
 Replaces oldParam with newParam. More...
 
void replaceLocalVar (ShPtr< Variable > oldVar, ShPtr< Variable > newVar)
 Replaces oldVar with newVar. More...
 
void removeLocalVar (ShPtr< Variable > var)
 Removes the given variable from the set of local variables. More...
 
void removeParam (ShPtr< Variable > param)
 Removes the given parameter. More...
 
void setBody (ShPtr< Statement > newBody)
 Sets a new body. More...
 
void setVarArg (bool isVarArg=true)
 Sets the function's status concerning the number of arguments it takes. More...
 
void convertToDeclaration ()
 Makes the function to be a declaration. More...
 
Observer Interface
virtual void update (ShPtr< Value > subject, ShPtr< Value > arg=nullptr) override
 Updates the statement according to the changes of subject. More...
 
Visitor Interface
virtual void accept (Visitor *v) override
 Visitor pattern implementation. More...
 
- Public Member Functions inherited from retdec::llvmir2hll::Value
virtual ~Value ()=default
 
virtual ShPtr< ValuegetSelf () override
 Returns a shared pointer of self. More...
 
std::string getTextRepr ()
 Returns a textual representation of the value. 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...
 
- 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 ShPtr< Functioncreate (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. More...
 

Private Member Functions

 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. More...
 
void updateUnderlyingVarType ()
 Updates the type of the underlying variable. More...
 
void includeParamsIntoLocalVars ()
 Includes all parameters into the set of local variables. More...
 

Private Attributes

WkPtr< Modulemodule
 The module to which the function belongs. More...
 
ShPtr< TyperetType
 Return type. More...
 
VarVector params
 Parameters. More...
 
VarSet localVars
 Local variables, including parameters. More...
 
ShPtr< Statementbody
 Function body. More...
 
ShPtr< VariablefuncVar
 
bool varArg
 

Additional Inherited Members

- 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...
 
- 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
 
- 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...
 

Detailed Description

A representation of a function.

Use either create() or FunctionBuilder to create instances. Instances of this class have reference object semantics. This class is not meant to be subclassed.

Constructor & Destructor Documentation

◆ Function()

retdec::llvmir2hll::Function::Function ( ShPtr< Module module,
ShPtr< Type retType,
std::string  name,
VarVector  params,
VarSet  localVars = VarSet(),
ShPtr< Statement body = nullptr,
bool  isVarArg = false 
)
private

Constructs a new function.

See create() for more information.

Member Function Documentation

◆ accept()

void retdec::llvmir2hll::Function::accept ( Visitor v)
overridevirtual

Visitor pattern implementation.

Subclasses should implement this method as:

v->visit(ucast<T>(shared_from_this()));

where T is the name of the subclass, and shared_from_this() and ucast<> are from Decompiler/Support/SmartPtr.h.

Implements retdec::llvmir2hll::Visitable.

◆ addLocalVar()

void retdec::llvmir2hll::Function::addLocalVar ( ShPtr< Variable var)

Adds a new local variable to the function.

If var is already a local variable in the function, this function does nothing.

To add a local variable including a VarDefStmt, use addLocalVar() from Support/Utils.

Preconditions
  • var is non-null

◆ addParam()

void retdec::llvmir2hll::Function::addParam ( ShPtr< Variable var)

Adds a new parameter to the function.

Preconditions
  • var is non-null

◆ clone()

ShPtr< Value > retdec::llvmir2hll::Function::clone ( )
overridevirtual

Returns a clone of the value.

A clone is (in most cases) an exact copy of the value. This member function provides the copy mechanism for reference objects.

The following parts of values are not cloned:

  • predecessors and successors of statements

The following subclasses of Value are not cloned, i.e. they are returned without any copying:

Statements in compound statements (i.e. statements where isCompound() returns true) are cloned without their successors; therefore, e.g., just the first statement of every if's clause is cloned.

Implements retdec::llvmir2hll::Value.

◆ convertToDeclaration()

void retdec::llvmir2hll::Function::convertToDeclaration ( )

Makes the function to be a declaration.

If the function is already a declaration, nothing happens. Once you make a function a declaration, the only way of making it a definition again is to set its body to a non-null pointer by setBody().

When the function is converted from a definition into a declaration, observers are notified.

◆ create()

ShPtr< Function > retdec::llvmir2hll::Function::create ( ShPtr< Module module,
ShPtr< Type retType,
std::string  name,
VarVector  params,
VarSet  localVars = VarSet(),
ShPtr< Statement body = nullptr,
bool  isVarArg = false 
)
static

Constructs a new function.

Parameters
[in]moduleModule this function belongs to.
[in]retTypeFunction return type.
[in]nameFunction name.
[in]paramsParameter list.
[in]localVarsLocal variables (without parameters).
[in]bodyFunction body.
[in]isVarArgtrue if the function takes a variable number of arguments, false otherwise.

If body is the null pointer, then the function is a declaration. Otherwise, it is a definition.

To build functions in a simpler way, use FunctionBuilder.

◆ getAddressRange()

AddressRange retdec::llvmir2hll::Function::getAddressRange ( ) const

◆ getAsVar()

ShPtr< Variable > retdec::llvmir2hll::Function::getAsVar ( ) const

Returns a variable corresponding the function.

This variable may be used when calling this function.

◆ getBody()

ShPtr< Statement > retdec::llvmir2hll::Function::getBody ( ) const

Returns function body.

◆ getEndAddress()

Address retdec::llvmir2hll::Function::getEndAddress ( ) const

◆ getInitialName()

const std::string & retdec::llvmir2hll::Function::getInitialName ( ) const

Returns the initial name of the function.

This is the name that was assigned to the function before any renaming.

◆ getLocalVars()

VarSet retdec::llvmir2hll::Function::getLocalVars ( bool  includeParams = false) const

Returns local variables of the function.

Parameters
[in]includeParamsIf true, function parameters will be included.

◆ getModule()

ShPtr< Module > retdec::llvmir2hll::Function::getModule ( ) const

◆ getName()

const std::string & retdec::llvmir2hll::Function::getName ( ) const

Returns function name.

◆ getNumOfLocalVars()

std::size_t retdec::llvmir2hll::Function::getNumOfLocalVars ( bool  includeParams = false) const

Returns the number of local variables.

Parameters
[in]includeParamsIf true, function parameters will be included.

◆ getNumOfParams()

std::size_t retdec::llvmir2hll::Function::getNumOfParams ( ) const

Returns the number of parameters.

◆ getParam()

ShPtr< Variable > retdec::llvmir2hll::Function::getParam ( std::size_t  n) const

Returns the n-th parameter.

The parameters are numbered in the following way:

func(1, 2, 3, 4, ...)
Preconditions
  • 0 < n <= NUM_OF_PARAMS, where NUM_OF_PARAMS is the number of parameters that the function has

◆ getParamPos()

std::size_t retdec::llvmir2hll::Function::getParamPos ( ShPtr< Variable param) const

Returns the position of the given parameter.

The parameter numbering is described in getParam().

Preconditions
  • param is a parameter of the function

◆ getParams()

const VarVector & retdec::llvmir2hll::Function::getParams ( ) const

Returns function parameters.

◆ getRetType()

ShPtr< Type > retdec::llvmir2hll::Function::getRetType ( ) const

Returns function type.

◆ getStartAddress()

Address retdec::llvmir2hll::Function::getStartAddress ( ) const

◆ getType()

ShPtr< Type > retdec::llvmir2hll::Function::getType ( ) const

Returns the type of the function.

Currently, it returns the return type of the function, not the complete type of the function. This simplifies the fixing of signed/unsigned types.

◆ hasLocalVar()

bool retdec::llvmir2hll::Function::hasLocalVar ( ShPtr< Variable var,
bool  includeParams = false 
) const

Returns true if var is a local variable of the function, false otherwise.

Parameters
[in]varVariable to be checked.
[in]includeParamsIf true, function parameters will be also considered as local variables.

◆ hasParam() [1/2]

bool retdec::llvmir2hll::Function::hasParam ( ShPtr< Variable var) const

Returns true if var is a parameter of the function, false otherwise.

◆ hasParam() [2/2]

bool retdec::llvmir2hll::Function::hasParam ( std::size_t  n) const

Returns true if the function has an n-th parameter, false otherwise.

The parameters are numbered in the following way:

func(1, 2, 3, 4, ...)

◆ includeParamsIntoLocalVars()

void retdec::llvmir2hll::Function::includeParamsIntoLocalVars ( )
private

Includes all parameters into the set of local variables.

Parameters are local variables, too.

◆ isDeclaration()

bool retdec::llvmir2hll::Function::isDeclaration ( ) const

Returns true if the function is just a declaration, false otherwise.

◆ isDefinition()

bool retdec::llvmir2hll::Function::isDefinition ( ) const

Returns true if the function is a definition, false otherwise.

◆ isEqualTo()

bool retdec::llvmir2hll::Function::isEqualTo ( ShPtr< Value otherValue) const
overridevirtual

Returns true if this value is equal to otherValue, false otherwise.

This member function brings the support of value object semantics into reference objects, namely equality based not only on identity.

This function doesn't consider observers, metadata, etc.

Implements retdec::llvmir2hll::Value.

◆ isVarArg()

bool retdec::llvmir2hll::Function::isVarArg ( ) const

Returns true if the function takes a variable number of arguments, false otherwise.

◆ removeLocalVar()

void retdec::llvmir2hll::Function::removeLocalVar ( ShPtr< Variable var)

Removes the given variable from the set of local variables.

This function does nothing if var is either a parameter of the function or the function does not have any local variable var.

Preconditions
  • var is non-null

◆ removeParam()

void retdec::llvmir2hll::Function::removeParam ( ShPtr< Variable param)

Removes the given parameter.

If param is not a parameter of this function, this function does nothing.

Preconditions
  • param is non-null

◆ replaceLocalVar()

void retdec::llvmir2hll::Function::replaceLocalVar ( ShPtr< Variable oldVar,
ShPtr< Variable newVar 
)

Replaces oldVar with newVar.

If oldVar does not correspond to any local variable, this function does nothing.

Preconditions
  • oldVar and newVar are non-null

◆ replaceParam()

void retdec::llvmir2hll::Function::replaceParam ( ShPtr< Variable oldParam,
ShPtr< Variable newParam 
)

Replaces oldParam with newParam.

If oldParam does not correspond to any parameter, this function does nothing.

◆ setBody()

void retdec::llvmir2hll::Function::setBody ( ShPtr< Statement newBody)

Sets a new body.

If newBody is the null pointer, this function becomes a declaration. Conversely, if newBody is non-null, this function becomes a definition.

◆ setLocalVars()

void retdec::llvmir2hll::Function::setLocalVars ( VarSet  newLocalVars)

Sets a new set of local variables.

◆ setName()

void retdec::llvmir2hll::Function::setName ( const std::string &  newName)

Sets a new name.

◆ setParams()

void retdec::llvmir2hll::Function::setParams ( VarVector  newParams)

Sets a new parameter list.

◆ setRetType()

void retdec::llvmir2hll::Function::setRetType ( ShPtr< Type newRetType)

Sets a new return type;.

◆ setVarArg()

void retdec::llvmir2hll::Function::setVarArg ( bool  isVarArg = true)

Sets the function's status concerning the number of arguments it takes.

If isVarArg is true, it marks the function as with a variable number of arguments. Otherwise, it marks it as a function with a fixed number of arguments.

◆ update()

void retdec::llvmir2hll::Function::update ( ShPtr< Value subject,
ShPtr< Value arg = nullptr 
)
overridevirtual

Updates the statement according to the changes of subject.

Parameters
[in]subjectObservable object.
[in]argOptional argument.

Replaces subject with arg. For example, if subject is one of the parameters of this function, this function replaces it with arg. If subject is the null pointer and corresponds to a part of the function, this part of the function is removed.

This function does nothing when:

  • subject does not correspond to any part of this function
Preconditions
  • subject is non-null
See also
Subject::update()

Reimplemented from retdec::llvmir2hll::Observer< Value >.

◆ updateUnderlyingVarType()

void retdec::llvmir2hll::Function::updateUnderlyingVarType ( )
private

Updates the type of the underlying variable.

This function has to be called whenever the signature of the function is changed (e.g. its return type or parameters).

Member Data Documentation

◆ body

ShPtr<Statement> retdec::llvmir2hll::Function::body
private

Function body.

◆ funcVar

ShPtr<Variable> retdec::llvmir2hll::Function::funcVar
private

Variable corresponding to the function. This variable may be used when calling this function.

◆ localVars

VarSet retdec::llvmir2hll::Function::localVars
private

Local variables, including parameters.

◆ module

WkPtr<Module> retdec::llvmir2hll::Function::module
private

The module to which the function belongs.

◆ params

VarVector retdec::llvmir2hll::Function::params
private

Parameters.

◆ retType

ShPtr<Type> retdec::llvmir2hll::Function::retType
private

Return type.

◆ varArg

bool retdec::llvmir2hll::Function::varArg
private

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