retdec
|
Parser for C-types represented in JSON. More...
#include <json_ctypes_parser.h>
Public Member Functions | |
JSONCTypesParser () | |
Constructs a new parser. More... | |
JSONCTypesParser (unsigned defaultBitWidth) | |
Constructs a new parser. More... | |
virtual std::unique_ptr< retdec::ctypes::Module > | parse (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... | |
![]() | |
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::Function > | getOrParseFunction (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::Function > | parseFunction (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 ¶m) |
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::FunctionType > | parseFunctionType (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::Type > | getOrParseType (const std::string &typeKey) |
Returns C type from parser's context or parses it from JSON representation. More... | |
std::shared_ptr< retdec::ctypes::Type > | parseType (const std::string &typeKey) |
Parses C-type from JSON representation. More... | |
std::shared_ptr< retdec::ctypes::Type > | parseIntegralType (const rapidjson::Value &type) |
Parses integral type from JSON representation. More... | |
std::shared_ptr< retdec::ctypes::Type > | parseFloatingPointType (const rapidjson::Value &type) |
Parses floating point type from JSON representation. More... | |
std::shared_ptr< retdec::ctypes::Type > | parseTypedefedType (const rapidjson::Value &jsonTypedef) |
Parses typedef from JSON representation. More... | |
std::shared_ptr< retdec::ctypes::Type > | parseStruct (const rapidjson::Value &jsonStruct) |
Parses struct from JSON representation. More... | |
std::shared_ptr< retdec::ctypes::Type > | parseUnion (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::PointerType > | parsePointer (const rapidjson::Value &jsonPointer) |
Parses pointer from JSON representation. More... | |
std::shared_ptr< retdec::ctypes::ArrayType > | parseArray (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::Type > | parseEnum (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::Type > | getOrParseNamedType (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 | |
![]() | |
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... | |
![]() | |
CTypesParser () | |
Creates new C-types parser. More... | |
CTypesParser (unsigned defaultBitWidth) | |
Creates new C-types parser. More... | |
![]() | |
std::shared_ptr< retdec::ctypes::Context > | context |
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... | |
Parser for C-types represented in JSON.
|
private |
|
private |
|
default |
Constructs a new parser.
retdec::ctypesparser::JSONCTypesParser::JSONCTypesParser | ( | unsigned | defaultBitWidth | ) |
Constructs a new parser.
defaultBitWidth | BitWidth used for types that are not in typeWidths. |
|
private |
Stores types
part of JSON to map.
We need fast random access to types, but RapidJSON
method FindMember
uses linear search.
|
private |
Returns bit width stored in typeWidths
for type, default if not found.
|
private |
Returns bit width stored in typeWidths
for integral type.
Returns default bit width if not found.
|
private |
Returns function from context, if already stored, otherwise parse new one.
funcName | Name of function. |
jsonFunction | JSON representation of function. |
|
private |
Returns named type from context, if already stored, otherwise parse new type.
jsonType | Type to get/parse. |
parseType | Function to parse specific type (typedef, struct...). |
|
private |
Returns C type from parser's context or parses it from JSON representation.
typeKey | Key of type stored in JSON types. |
|
private |
Gets additional info about failure in parsing and throws exception.
CTypesParseError |
|
private |
Loads JSON from the input stream to a string.
|
virtual |
Parses C-types from JSON representation.
[in] | stream | Input stream containing C-types in JSON. |
[in] | typeWidths | C-types' bit widths. |
[in] | callConvention | Function call convention. |
CTypesParseError | when the input JSON is invalid. |
Call convention is used when function itself does not specify its call convention.
|
private |
Parses parameter's annotations.
Distinguish in
, out
and inout
annotations, they all may be optional.
|
private |
Parses array from JSON representation.
jsonArray | JSON object representing array. |
|
private |
Parses array dimensions from JSON representation.
jsonDimensions | JSON array containing dimensions. |
|
private |
Returns call_conv
attribute's value if exists, default otherwise.
|
private |
Parses enum type from JSON representation.
jsonEnum | JSON object representing enum. |
|
private |
Parses enum values from JSON representation.
jsonEnumItems | JSON object representing enum values. |
|
private |
Parses floating point type from JSON representation.
type | JSON object representing floating point type. |
|
private |
Parses C function from JSON representation.
function | JSON object representing one function. |
fName | Name 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.
|
private |
Parses function type from JSON representation.
jsonFuncType | JSON object representing one function type. |
|
private |
Parses function type parameters from JSON representation.
jsonParams | JSON object representing function parameters. |
Ignores parameters' names.
|
private |
Parses integral type from JSON representation.
type | JSON object representing integral type. |
|
virtual |
Parses C-types from JSON representation to user's module.
[in] | stream | Input stream containing C-types in JSON. |
[in] | module | User's module. |
[in] | typeWidths | C-types' bit widths. |
[in] | callConvention | Function call convention. |
CTypesParseError | when the input JSON is invalid. |
Call convention is used when function itself does not specify its call convention.
|
private |
Parses JSON in-situ.
CTypesParseError | when the input JSON is invalid. |
|
private |
Parses C-types from JSON representation.
root | Whole JSON containing functions and types. |
module | Container for parsed functions. |
Call convention is used when function itself does not specify its call convention.
|
private |
Parses composite type's members.
jsonMembers | JSON object representing composite type's members. |
|
private |
Parses function parameter from JSON representation.
param | JSON object representing one function parameter. |
|
private |
Parses function parameters from JSON representation.
jsonParams | JSON object representing function parameters. |
|
private |
Parses pointer from JSON representation.
jsonPointer | JSON object representing pointer. |
|
private |
Parses struct from JSON representation.
jsonStruct | JSON 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:
|
private |
Parses C-type from JSON representation.
typeKey | Key of type stored in JSON types. |
Parsed types are stored in parserContext
, so you should use getOrParseType()
method.
|
private |
Parses typedef from JSON representation.
jsonTypedef | JSON object representing typedefed type. |
|
private |
Parses union from JSON representation.
jsonUnion | JSON 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:
|
private |
Returns IsVarArg
when function
has varArg attribute set to true
, IsNotVarArg
otherwise.
|
private |
Call convention used when JSON does not contain one.
|
private |
Context for the parser (to speedup the parsing).
|
private |
Map used to store pointers to JSON types (to speedup the parsing).