retdec
Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
retdec::ctypesparser::JSONCTypesParser Class Reference

Parser for C-types represented in JSON. More...

#include <json_ctypes_parser.h>

Inheritance diagram for retdec::ctypesparser::JSONCTypesParser:
Inheritance graph
[legend]
Collaboration diagram for retdec::ctypesparser::JSONCTypesParser:
Collaboration graph
[legend]

Public Member Functions

 JSONCTypesParser ()
 Constructs a new parser. More...
 
 JSONCTypesParser (unsigned defaultBitWidth)
 Constructs a new parser. More...
 
virtual std::unique_ptr< retdec::ctypes::Moduleparse (std::istream &stream, const TypeWidths &typeWidths={}, const retdec::ctypes::CallConvention &callConvention=retdec::ctypes::CallConvention())
 Parses C-types from JSON representation. More...
 
virtual void parseInto (std::istream &stream, std::unique_ptr< retdec::ctypes::Module > &module, const TypeWidths &typeWidths={}, const retdec::ctypes::CallConvention &callConvention=retdec::ctypes::CallConvention())
 Parses C-types from JSON representation to user's module. More...
 
- Public Member Functions inherited from retdec::ctypesparser::CTypesParser
virtual ~CTypesParser ()=default
 

Private Types

using ParserContext = std::unordered_map< std::string, std::shared_ptr< retdec::ctypes::Type > >
 
using TypesMap = std::unordered_map< std::string, rapidjson::Value::ConstMemberIterator >
 

Private Member Functions

std::string loadJson (std::istream &stream) const
 Loads JSON from the input stream to a string. More...
 
std::unique_ptr< rapidjson::Document > parseJson (char *buffer) const
 Parses JSON in-situ. More...
 
void parseJsonIntoModule (const std::unique_ptr< rapidjson::Document > &root, std::unique_ptr< retdec::ctypes::Module > &module)
 Parses C-types from JSON representation. More...
 
void addTypesToMap (const rapidjson::Value &types)
 Stores types part of JSON to map. More...
 
Parsing methods.
void handleParsingFailure (const rapidjson::ParseResult &err) const
 Gets additional info about failure in parsing and throws exception. More...
 
std::shared_ptr< retdec::ctypes::FunctiongetOrParseFunction (const std::string &name, const rapidjson::Value &jsonFunction)
 Returns function from context, if already stored, otherwise parse new one. More...
 
std::shared_ptr< retdec::ctypes::FunctionparseFunction (const rapidjson::Value &function, const std::string &fName)
 Parses C function from JSON representation. More...
 
retdec::ctypes::Function::Parameters parseParameters (const rapidjson::Value &jsonParams)
 Parses function parameters from JSON representation. More...
 
retdec::ctypes::Parameter parseParameter (const rapidjson::Value &param)
 Parses function parameter from JSON representation. More...
 
retdec::ctypes::FunctionType::VarArgness parseVarArgness (const rapidjson::Value &function) const
 Returns IsVarArg when function has varArg attribute set to true, IsNotVarArg otherwise. More...
 
std::string parseCallConv (const rapidjson::Value &function) const
 Returns call_conv attribute's value if exists, default otherwise. More...
 
retdec::ctypes::Parameter::Annotations parseAnnotations (const std::string &annot) const
 Parses parameter's annotations. More...
 
std::shared_ptr< retdec::ctypes::FunctionTypeparseFunctionType (const rapidjson::Value &jsonFuncType)
 Parses function type from JSON representation. More...
 
retdec::ctypes::FunctionType::Parameters parseFunctionTypeParameters (const rapidjson::Value &jsonParams)
 Parses function type parameters from JSON representation. More...
 
std::shared_ptr< retdec::ctypes::TypegetOrParseType (const std::string &typeKey)
 Returns C type from parser's context or parses it from JSON representation. More...
 
std::shared_ptr< retdec::ctypes::TypeparseType (const std::string &typeKey)
 Parses C-type from JSON representation. More...
 
std::shared_ptr< retdec::ctypes::TypeparseIntegralType (const rapidjson::Value &type)
 Parses integral type from JSON representation. More...
 
std::shared_ptr< retdec::ctypes::TypeparseFloatingPointType (const rapidjson::Value &type)
 Parses floating point type from JSON representation. More...
 
std::shared_ptr< retdec::ctypes::TypeparseTypedefedType (const rapidjson::Value &jsonTypedef)
 Parses typedef from JSON representation. More...
 
std::shared_ptr< retdec::ctypes::TypeparseStruct (const rapidjson::Value &jsonStruct)
 Parses struct from JSON representation. More...
 
std::shared_ptr< retdec::ctypes::TypeparseUnion (const rapidjson::Value &jsonUnion)
 Parses union from JSON representation. More...
 
retdec::ctypes::CompositeType::Members parseMembers (const rapidjson::Value &jsonMembers)
 Parses composite type's members. More...
 
std::shared_ptr< retdec::ctypes::PointerTypeparsePointer (const rapidjson::Value &jsonPointer)
 Parses pointer from JSON representation. More...
 
std::shared_ptr< retdec::ctypes::ArrayTypeparseArray (const rapidjson::Value &jsonArray)
 Parses array from JSON representation. More...
 
retdec::ctypes::ArrayType::Dimensions parseArrayDimensions (const rapidjson::Value &jsonDimensions) const
 Parses array dimensions from JSON representation. More...
 
std::shared_ptr< retdec::ctypes::TypeparseEnum (const rapidjson::Value &jsonEnum)
 Parses enum type from JSON representation. More...
 
retdec::ctypes::EnumType::Values parseEnumItems (const rapidjson::Value &jsonEnumItems) const
 Parses enum values from JSON representation. More...
 
std::shared_ptr< retdec::ctypes::TypegetOrParseNamedType (const rapidjson::Value &jsonType, const std::function< std::shared_ptr< retdec::ctypes::Type >(const std::string &typeName) > &parseType)
 Returns named type from context, if already stored, otherwise parse new type. More...
 
unsigned getIntegralTypeBitWidth (const std::string &type) const
 Returns bit width stored in typeWidths for integral type. More...
 
unsigned getBitWidthOrDefault (const std::string &typeName) const
 Returns bit width stored in typeWidths for type, default if not found. More...
 

Private Attributes

ParserContext parserContext
 Context for the parser (to speedup the parsing). More...
 
TypesMap typesMap
 Map used to store pointers to JSON types (to speedup the parsing). More...
 
retdec::ctypes::CallConvention defaultCallConv
 Call convention used when JSON does not contain one. More...
 

Additional Inherited Members

- Public Types inherited from retdec::ctypesparser::CTypesParser
using TypeWidths = std::map< std::string, unsigned >
 Set container for C-types' bit width. More...
 
using TypeSignedness = std::map< std::string, ctypes::IntegralType::Signess >
 Set container for C-types' signedness. More...
 
- Protected Member Functions inherited from retdec::ctypesparser::CTypesParser
 CTypesParser ()
 Creates new C-types parser. More...
 
 CTypesParser (unsigned defaultBitWidth)
 Creates new C-types parser. More...
 
- Protected Attributes inherited from retdec::ctypesparser::CTypesParser
std::shared_ptr< retdec::ctypes::Contextcontext
 Container for already parsed functions, types. More...
 
TypeWidths typeWidths
 C-types' bit widths. More...
 
TypeSignedness typeSignedness
 C-types' signedness. More...
 
unsigned defaultBitWidth = 0
 Bitwidth used for types not in typeWidths. More...
 

Detailed Description

Parser for C-types represented in JSON.

Member Typedef Documentation

◆ ParserContext

using retdec::ctypesparser::JSONCTypesParser::ParserContext = std::unordered_map<std::string, std::shared_ptr<retdec::ctypes::Type> >
private

◆ TypesMap

using retdec::ctypesparser::JSONCTypesParser::TypesMap = std::unordered_map<std::string, rapidjson::Value::ConstMemberIterator>
private

Constructor & Destructor Documentation

◆ JSONCTypesParser() [1/2]

retdec::ctypesparser::JSONCTypesParser::JSONCTypesParser ( )
default

Constructs a new parser.

◆ JSONCTypesParser() [2/2]

retdec::ctypesparser::JSONCTypesParser::JSONCTypesParser ( unsigned  defaultBitWidth)

Constructs a new parser.

Parameters
defaultBitWidthBitWidth used for types that are not in typeWidths.

Member Function Documentation

◆ addTypesToMap()

void retdec::ctypesparser::JSONCTypesParser::addTypesToMap ( const rapidjson::Value &  types)
private

Stores types part of JSON to map.

We need fast random access to types, but RapidJSON method FindMember uses linear search.

◆ getBitWidthOrDefault()

unsigned retdec::ctypesparser::JSONCTypesParser::getBitWidthOrDefault ( const std::string &  typeName) const
private

Returns bit width stored in typeWidths for type, default if not found.

◆ getIntegralTypeBitWidth()

unsigned retdec::ctypesparser::JSONCTypesParser::getIntegralTypeBitWidth ( const std::string &  type) const
private

Returns bit width stored in typeWidths for integral type.

Returns default bit width if not found.

◆ getOrParseFunction()

std::shared_ptr< retdec::ctypes::Function > retdec::ctypesparser::JSONCTypesParser::getOrParseFunction ( const std::string &  funcName,
const rapidjson::Value &  jsonFunction 
)
private

Returns function from context, if already stored, otherwise parse new one.

Parameters
funcNameName of function.
jsonFunctionJSON representation of function.

◆ getOrParseNamedType()

std::shared_ptr< retdec::ctypes::Type > retdec::ctypesparser::JSONCTypesParser::getOrParseNamedType ( const rapidjson::Value &  jsonType,
const std::function< std::shared_ptr< retdec::ctypes::Type >(const std::string &typeName) > &  parseType 
)
private

Returns named type from context, if already stored, otherwise parse new type.

Parameters
jsonTypeType to get/parse.
parseTypeFunction to parse specific type (typedef, struct...).

◆ getOrParseType()

std::shared_ptr< retdec::ctypes::Type > retdec::ctypesparser::JSONCTypesParser::getOrParseType ( const std::string &  typeKey)
private

Returns C type from parser's context or parses it from JSON representation.

Parameters
typeKeyKey of type stored in JSON types.

◆ handleParsingFailure()

void retdec::ctypesparser::JSONCTypesParser::handleParsingFailure ( const rapidjson::ParseResult &  err) const
private

Gets additional info about failure in parsing and throws exception.

Exceptions
CTypesParseError

◆ loadJson()

std::string retdec::ctypesparser::JSONCTypesParser::loadJson ( std::istream &  stream) const
private

Loads JSON from the input stream to a string.

◆ parse()

std::unique_ptr< retdec::ctypes::Module > retdec::ctypesparser::JSONCTypesParser::parse ( std::istream &  stream,
const TypeWidths typeWidths = {},
const retdec::ctypes::CallConvention callConvention = retdec::ctypes::CallConvention() 
)
virtual

Parses C-types from JSON representation.

Parameters
[in]streamInput stream containing C-types in JSON.
[in]typeWidthsC-types' bit widths.
[in]callConventionFunction call convention.
Returns
Module filled with C-types information.
Exceptions
CTypesParseErrorwhen the input JSON is invalid.

Call convention is used when function itself does not specify its call convention.

◆ parseAnnotations()

retdec::ctypes::Parameter::Annotations retdec::ctypesparser::JSONCTypesParser::parseAnnotations ( const std::string &  annot) const
private

Parses parameter's annotations.

Distinguish in, out and inout annotations, they all may be optional.

◆ parseArray()

std::shared_ptr< retdec::ctypes::ArrayType > retdec::ctypesparser::JSONCTypesParser::parseArray ( const rapidjson::Value &  jsonArray)
private

Parses array from JSON representation.

Parameters
jsonArrayJSON object representing array.

◆ parseArrayDimensions()

retdec::ctypes::ArrayType::Dimensions retdec::ctypesparser::JSONCTypesParser::parseArrayDimensions ( const rapidjson::Value &  jsonDimensions) const
private

Parses array dimensions from JSON representation.

Parameters
jsonDimensionsJSON array containing dimensions.

◆ parseCallConv()

std::string retdec::ctypesparser::JSONCTypesParser::parseCallConv ( const rapidjson::Value &  function) const
private

Returns call_conv attribute's value if exists, default otherwise.

◆ parseEnum()

std::shared_ptr< retdec::ctypes::Type > retdec::ctypesparser::JSONCTypesParser::parseEnum ( const rapidjson::Value &  jsonEnum)
private

Parses enum type from JSON representation.

Parameters
jsonEnumJSON object representing enum.

◆ parseEnumItems()

retdec::ctypes::EnumType::Values retdec::ctypesparser::JSONCTypesParser::parseEnumItems ( const rapidjson::Value &  jsonEnumItems) const
private

Parses enum values from JSON representation.

Parameters
jsonEnumItemsJSON object representing enum values.

◆ parseFloatingPointType()

std::shared_ptr< retdec::ctypes::Type > retdec::ctypesparser::JSONCTypesParser::parseFloatingPointType ( const rapidjson::Value &  type)
private

Parses floating point type from JSON representation.

Parameters
typeJSON object representing floating point type.

◆ parseFunction()

std::shared_ptr< retdec::ctypes::Function > retdec::ctypesparser::JSONCTypesParser::parseFunction ( const rapidjson::Value &  function,
const std::string &  fName 
)
private

Parses C function from JSON representation.

Parameters
functionJSON object representing one function.
fNameName of new function.

Call convention is used when function itself does not specify its call convention. This function should be called from getOrParseFunction(), where function name is parsed from JSON.

◆ parseFunctionType()

std::shared_ptr< retdec::ctypes::FunctionType > retdec::ctypesparser::JSONCTypesParser::parseFunctionType ( const rapidjson::Value &  jsonFuncType)
private

Parses function type from JSON representation.

Parameters
jsonFuncTypeJSON object representing one function type.

◆ parseFunctionTypeParameters()

retdec::ctypes::FunctionType::Parameters retdec::ctypesparser::JSONCTypesParser::parseFunctionTypeParameters ( const rapidjson::Value &  jsonParams)
private

Parses function type parameters from JSON representation.

Parameters
jsonParamsJSON object representing function parameters.

Ignores parameters' names.

◆ parseIntegralType()

std::shared_ptr< retdec::ctypes::Type > retdec::ctypesparser::JSONCTypesParser::parseIntegralType ( const rapidjson::Value &  type)
private

Parses integral type from JSON representation.

Parameters
typeJSON object representing integral type.

◆ parseInto()

void retdec::ctypesparser::JSONCTypesParser::parseInto ( std::istream &  stream,
std::unique_ptr< retdec::ctypes::Module > &  module,
const TypeWidths typeWidths = {},
const retdec::ctypes::CallConvention callConvention = retdec::ctypes::CallConvention() 
)
virtual

Parses C-types from JSON representation to user's module.

Parameters
[in]streamInput stream containing C-types in JSON.
[in]moduleUser's module.
[in]typeWidthsC-types' bit widths.
[in]callConventionFunction call convention.
Exceptions
CTypesParseErrorwhen the input JSON is invalid.

Call convention is used when function itself does not specify its call convention.

◆ parseJson()

std::unique_ptr< rapidjson::Document > retdec::ctypesparser::JSONCTypesParser::parseJson ( char *  buffer) const
private

Parses JSON in-situ.

Exceptions
CTypesParseErrorwhen the input JSON is invalid.

◆ parseJsonIntoModule()

void retdec::ctypesparser::JSONCTypesParser::parseJsonIntoModule ( const std::unique_ptr< rapidjson::Document > &  root,
std::unique_ptr< retdec::ctypes::Module > &  module 
)
private

Parses C-types from JSON representation.

Parameters
rootWhole JSON containing functions and types.
moduleContainer for parsed functions.

Call convention is used when function itself does not specify its call convention.

◆ parseMembers()

retdec::ctypes::CompositeType::Members retdec::ctypesparser::JSONCTypesParser::parseMembers ( const rapidjson::Value &  jsonMembers)
private

Parses composite type's members.

Parameters
jsonMembersJSON object representing composite type's members.

◆ parseParameter()

retdec::ctypes::Parameter retdec::ctypesparser::JSONCTypesParser::parseParameter ( const rapidjson::Value &  param)
private

Parses function parameter from JSON representation.

Parameters
paramJSON object representing one function parameter.

◆ parseParameters()

retdec::ctypes::Function::Parameters retdec::ctypesparser::JSONCTypesParser::parseParameters ( const rapidjson::Value &  jsonParams)
private

Parses function parameters from JSON representation.

Parameters
jsonParamsJSON object representing function parameters.

◆ parsePointer()

std::shared_ptr< retdec::ctypes::PointerType > retdec::ctypesparser::JSONCTypesParser::parsePointer ( const rapidjson::Value &  jsonPointer)
private

Parses pointer from JSON representation.

Parameters
jsonPointerJSON object representing pointer.

◆ parseStruct()

std::shared_ptr< retdec::ctypes::Type > retdec::ctypesparser::JSONCTypesParser::parseStruct ( const rapidjson::Value &  jsonStruct)
private

Parses struct from JSON representation.

Parameters
jsonStructJSON object representing struct.

A new struct is created at the beginning (like a forward declaration) and its members are set subsequently. This prevents parser from infinite looping, as in the following case:

struct x { struct x *next; };

◆ parseType()

std::shared_ptr< retdec::ctypes::Type > retdec::ctypesparser::JSONCTypesParser::parseType ( const std::string &  typeKey)
private

Parses C-type from JSON representation.

Parameters
typeKeyKey of type stored in JSON types.

Parsed types are stored in parserContext, so you should use getOrParseType() method.

◆ parseTypedefedType()

std::shared_ptr< retdec::ctypes::Type > retdec::ctypesparser::JSONCTypesParser::parseTypedefedType ( const rapidjson::Value &  jsonTypedef)
private

Parses typedef from JSON representation.

Parameters
jsonTypedefJSON object representing typedefed type.

◆ parseUnion()

std::shared_ptr< retdec::ctypes::Type > retdec::ctypesparser::JSONCTypesParser::parseUnion ( const rapidjson::Value &  jsonUnion)
private

Parses union from JSON representation.

Parameters
jsonUnionJSON object representing union.

A new union is created at the beginning (like a forward declaration) and its members are set subsequently. This prevents parser from infinite looping, as in the following case:

union x { union x *next; };

◆ parseVarArgness()

retdec::ctypes::FunctionType::VarArgness retdec::ctypesparser::JSONCTypesParser::parseVarArgness ( const rapidjson::Value &  function) const
private

Returns IsVarArg when function has varArg attribute set to true, IsNotVarArg otherwise.

Member Data Documentation

◆ defaultCallConv

retdec::ctypes::CallConvention retdec::ctypesparser::JSONCTypesParser::defaultCallConv
private

Call convention used when JSON does not contain one.

◆ parserContext

ParserContext retdec::ctypesparser::JSONCTypesParser::parserContext
private

Context for the parser (to speedup the parsing).

◆ typesMap

TypesMap retdec::ctypesparser::JSONCTypesParser::typesMap
private

Map used to store pointers to JSON types (to speedup the parsing).


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