Skip to content

Commit

Permalink
ctypes: added named types
Browse files Browse the repository at this point in the history
  • Loading branch information
xvenge00 committed Mar 12, 2019
1 parent 1035e88 commit 72b935b
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 1 deletion.
2 changes: 2 additions & 0 deletions include/retdec/bin2llvmir/providers/lti.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class ToLlvmTypeVisitor: public retdec::ctypes::Visitor
const std::shared_ptr<retdec::ctypes::FunctionType>&) override;
virtual void visit(
const std::shared_ptr<retdec::ctypes::IntegralType>&) override;
virtual void visit(
const std::shared_ptr<retdec::ctypes::NamedType>&) override;
virtual void visit(
const std::shared_ptr<retdec::ctypes::PointerType>&) override;
virtual void visit(
Expand Down
35 changes: 35 additions & 0 deletions include/retdec/ctypes/named_type.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

#ifndef RETDEC_CTYPES_NAMED_TYPE_H
#define RETDEC_CTYPES_NAMED_TYPE_H

#include "retdec/ctypes/type.h"
#include "retdec/ctypes/context.h"

namespace retdec {
namespace ctypes {

/**
* @brief A representation of custom types.
*/
class NamedType: public Type {
public:
static std::shared_ptr<NamedType> create(
const std::shared_ptr<Context> &context,
const std::string &name
);

/// @name Visitor interface.
/// @{
void accept(Visitor *v) override;
/// @}

bool isNamed() const override;

private:
explicit NamedType(const std::string &name);
};

} // namespace ctypes
} // namespace retdec

#endif //RETDEC_CTYPES_NAMED_TYPE_H
1 change: 1 addition & 0 deletions include/retdec/ctypes/type.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Type: public Visitable, public std::enable_shared_from_this<Type>
virtual bool isFloatingPoint() const;
virtual bool isFunction() const;
virtual bool isIntegral() const;
virtual bool isNamed() const;
virtual bool isPointer() const;
virtual bool isReference() const;
virtual bool isStruct() const;
Expand Down
1 change: 1 addition & 0 deletions include/retdec/ctypes/visit_all_visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class VisitAllVisitor: public Visitor
virtual void visit(const std::shared_ptr<FloatingPointType> &type) override;
virtual void visit(const std::shared_ptr<FunctionType> &type) override;
virtual void visit(const std::shared_ptr<IntegralType> &type) override;
virtual void visit(const std::shared_ptr<NamedType> &type) override;
virtual void visit(const std::shared_ptr<PointerType> &type) override;
virtual void visit(const std::shared_ptr<ReferenceType> &type) override;
virtual void visit(const std::shared_ptr<StructType> &type) override;
Expand Down
2 changes: 2 additions & 0 deletions include/retdec/ctypes/visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class EnumType;
class FloatingPointType;
class FunctionType;
class IntegralType;
class NamedType;
class PointerType;
class ReferenceType;
class StructType;
Expand All @@ -38,6 +39,7 @@ class Visitor
virtual void visit(const std::shared_ptr<FloatingPointType> &type) = 0;
virtual void visit(const std::shared_ptr<FunctionType> &type) = 0;
virtual void visit(const std::shared_ptr<IntegralType> &type) = 0;
virtual void visit(const std::shared_ptr<NamedType> &type) = 0;
virtual void visit(const std::shared_ptr<PointerType> &type) = 0;
virtual void visit(const std::shared_ptr<ReferenceType> &type) = 0;
virtual void visit(const std::shared_ptr<StructType> &type) = 0;
Expand Down
7 changes: 7 additions & 0 deletions src/bin2llvmir/providers/lti.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ void ToLlvmTypeVisitor::visit(
_type = Type::getIntNTy(_module->getContext(), sz);
}

void ToLlvmTypeVisitor::visit(
const std::shared_ptr<retdec::ctypes::NamedType>& type)
{
// TODO
_type = Abi::getDefaultType(_module);
}

void ToLlvmTypeVisitor::visit(
const std::shared_ptr<retdec::ctypes::PointerType>& type)
{
Expand Down
1 change: 1 addition & 0 deletions src/ctypes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ set(CTYPES_SOURCES
integral_type.cpp
member.cpp
module.cpp
named_type.cpp
parameter.cpp
pointer_type.cpp
reference_type.cpp
Expand Down
62 changes: 62 additions & 0 deletions src/ctypes/named_type.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* @file src/ctypes/named_type.cpp
* @brief Implementation of custom types.
* @copyright (c) 2018 Avast Software, licensed under the MIT license
*/

#include <cassert>

#include "retdec/ctypes/named_type.h"
#include "retdec/ctypes/visitor.h"

namespace retdec {
namespace ctypes {

/**
* @brief Constructs new NamedType
*/
NamedType::NamedType(const std::string &name) : Type(name, 0) {}

/**
* @brief Creates named type.
*
* @param context Storage for already created functions, types.
* @param name Name of new named type.
*
* @par Preconditions
* - @a context is not null
*
* Does not create new name type, if one
* has already been created and stored in @c context.
*/
std::shared_ptr<NamedType> NamedType::create(
const std::shared_ptr<retdec::ctypes::Context> &context,
const std::string &name)
{
assert(context && "violated precondition - context cannot be null");

auto type = context->getNamedType(name);
if (type && type->isNamed()) {
return std::static_pointer_cast<NamedType>(type);
}

std::shared_ptr<NamedType> newType(new NamedType(name));
context->addNamedType(newType);
return newType;
}

/**
* Returns @c true when Type is class, @c false otherwise.
*/
bool NamedType::isNamed() const
{
return true;
}

void NamedType::accept(retdec::ctypes::Visitor *v)
{
v->visit(std::static_pointer_cast<NamedType>(shared_from_this()));
}

} // namespace ctypes
} // namespace retdec
7 changes: 6 additions & 1 deletion src/ctypes/type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace ctypes {
/**
* @brief Constructs a new type.
*/
Type::Type(const std::string &name, unsigned bitWidth):
Type::Type(const std::string &name, unsigned bitWidth) :
name(name), bitWidth(bitWidth) {}

const std::string &Type::getName() const
Expand Down Expand Up @@ -50,6 +50,11 @@ bool Type::isIntegral() const
return false;
}

bool Type::isNamed() const
{
return false;
}

bool Type::isPointer() const
{
return false;
Expand Down
11 changes: 11 additions & 0 deletions src/ctypes/visit_all_visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "retdec/ctypes/floating_point_type.h"
#include "retdec/ctypes/function_type.h"
#include "retdec/ctypes/integral_type.h"
#include "retdec/ctypes/named_type.h"
#include "retdec/ctypes/member.h"
#include "retdec/ctypes/pointer_type.h"
#include "retdec/ctypes/reference_type.h"
Expand Down Expand Up @@ -81,6 +82,16 @@ void VisitAllVisitor::visit(const std::shared_ptr<IntegralType> &type)
}
}

void VisitAllVisitor::visit(const std::shared_ptr<NamedType> &type)
{
// TODO

if (makeAccessedAndCheckIfAccessed(type))
{
return;
}
}

void VisitAllVisitor::visit(const std::shared_ptr<PointerType> &type)
{
if (makeAccessedAndCheckIfAccessed(type))
Expand Down

0 comments on commit 72b935b

Please sign in to comment.