7 #ifndef RETDEC_CTYPESPARSER_JSON_CTYPES_PARSER_H
8 #define RETDEC_CTYPESPARSER_JSON_CTYPES_PARSER_H
12 #include <unordered_map>
14 #include <rapidjson/document.h>
19 namespace ctypesparser {
30 virtual std::unique_ptr<retdec::ctypes::Module>
parse(
36 std::unique_ptr<retdec::ctypes::Module> &
module,
41 std::string
loadJson(std::istream &stream)
const;
42 std::unique_ptr<rapidjson::Document>
parseJson(
char *buffer)
const;
44 const std::unique_ptr<rapidjson::Document> &root,
45 std::unique_ptr<retdec::ctypes::Module> &
module);
52 const std::string &name,
53 const rapidjson::Value &jsonFunction
56 const rapidjson::Value &
function,
57 const std::string &fName
60 const rapidjson::Value &jsonParams
63 const rapidjson::Value ¶m
66 const rapidjson::Value &
function
69 const rapidjson::Value &
function
72 const std::string &annot
75 const rapidjson::Value &jsonFuncType
78 const rapidjson::Value &jsonParams
81 const std::string &typeKey
83 std::shared_ptr<retdec::ctypes::Type>
parseType(
84 const std::string &typeKey
87 const rapidjson::Value &type
90 const rapidjson::Value &type
93 const rapidjson::Value &jsonTypedef
96 const rapidjson::Value &jsonStruct
98 std::shared_ptr<retdec::ctypes::Type>
parseUnion(
99 const rapidjson::Value &jsonUnion
102 const rapidjson::Value &jsonMembers
104 std::shared_ptr<retdec::ctypes::PointerType>
parsePointer(
105 const rapidjson::Value &jsonPointer
107 std::shared_ptr<retdec::ctypes::ArrayType>
parseArray(
108 const rapidjson::Value &jsonArray
111 const rapidjson::Value &jsonDimensions
113 std::shared_ptr<retdec::ctypes::Type>
parseEnum(
114 const rapidjson::Value &jsonEnum
117 const rapidjson::Value &jsonEnumItems
120 const rapidjson::Value &jsonType,
122 std::shared_ptr<retdec::ctypes::Type> (
const std::string &typeName)
130 using ParserContext = std::unordered_map<std::string, std::shared_ptr<retdec::ctypes::Type>>;
131 using TypesMap = std::unordered_map<std::string, rapidjson::Value::ConstMemberIterator>;
std::vector< DimensionType > Dimensions
Definition: array_type.h:27
A representation of a C call convention.
Definition: call_convention.h:19
std::vector< Member > Members
Definition: composite_type.h:28
std::vector< Value > Values
Definition: enum_type.h:54
VarArgness
Definition: function_type.h:33
std::vector< std::shared_ptr< Type > > Parameters
Definition: function_type.h:28
std::vector< Parameter > Parameters
Definition: function.h:32
A representation of a function parameter.
Definition: parameter.h:24
std::set< std::shared_ptr< Annotation > > Annotations
Definition: parameter.h:26
A base class for parsing to C-types.
Definition: ctypes_parser.h:25
unsigned defaultBitWidth
Bitwidth used for types not in typeWidths.
Definition: ctypes_parser.h:47
std::map< std::string, unsigned > TypeWidths
Set container for C-types' bit width.
Definition: ctypes_parser.h:28
TypeWidths typeWidths
C-types' bit widths.
Definition: ctypes_parser.h:43
Parser for C-types represented in JSON.
Definition: json_ctypes_parser.h:25
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.
Definition: json_ctypes_parser.cpp:712
std::unordered_map< std::string, rapidjson::Value::ConstMemberIterator > TypesMap
Definition: json_ctypes_parser.h:131
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.
Definition: json_ctypes_parser.cpp:193
std::shared_ptr< retdec::ctypes::Type > parseIntegralType(const rapidjson::Value &type)
Parses integral type from JSON representation.
Definition: json_ctypes_parser.cpp:571
retdec::ctypes::FunctionType::VarArgness parseVarArgness(const rapidjson::Value &function) const
Returns IsVarArg when function has varArg attribute set to true, IsNotVarArg otherwise.
Definition: json_ctypes_parser.cpp:398
retdec::ctypes::EnumType::Values parseEnumItems(const rapidjson::Value &jsonEnumItems) const
Parses enum values from JSON representation.
Definition: json_ctypes_parser.cpp:862
std::shared_ptr< retdec::ctypes::Type > parseStruct(const rapidjson::Value &jsonStruct)
Parses struct from JSON representation.
Definition: json_ctypes_parser.cpp:735
std::shared_ptr< retdec::ctypes::Function > parseFunction(const rapidjson::Value &function, const std::string &fName)
Parses C function from JSON representation.
Definition: json_ctypes_parser.cpp:330
std::shared_ptr< retdec::ctypes::Type > parseEnum(const rapidjson::Value &jsonEnum)
Parses enum type from JSON representation.
Definition: json_ctypes_parser.cpp:845
retdec::ctypes::Parameter::Annotations parseAnnotations(const std::string &annot) const
Parses parameter's annotations.
Definition: json_ctypes_parser.cpp:426
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.
Definition: json_ctypes_parser.cpp:216
TypesMap typesMap
Map used to store pointers to JSON types (to speedup the parsing).
Definition: json_ctypes_parser.h:138
std::unique_ptr< rapidjson::Document > parseJson(char *buffer) const
Parses JSON in-situ.
Definition: json_ctypes_parser.cpp:254
unsigned getIntegralTypeBitWidth(const std::string &type) const
Returns bit width stored in typeWidths for integral type.
Definition: json_ctypes_parser.cpp:616
ParserContext parserContext
Context for the parser (to speedup the parsing).
Definition: json_ctypes_parser.h:135
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.
Definition: json_ctypes_parser.cpp:311
std::shared_ptr< retdec::ctypes::Type > parseType(const std::string &typeKey)
Parses C-type from JSON representation.
Definition: json_ctypes_parser.cpp:504
void parseJsonIntoModule(const std::unique_ptr< rapidjson::Document > &root, std::unique_ptr< retdec::ctypes::Module > &module)
Parses C-types from JSON representation.
Definition: json_ctypes_parser.cpp:273
retdec::ctypes::Parameter parseParameter(const rapidjson::Value ¶m)
Parses function parameter from JSON representation.
Definition: json_ctypes_parser.cpp:376
std::unordered_map< std::string, std::shared_ptr< retdec::ctypes::Type > > ParserContext
Definition: json_ctypes_parser.h:130
void addTypesToMap(const rapidjson::Value &types)
Stores types part of JSON to map.
Definition: json_ctypes_parser.cpp:295
std::shared_ptr< retdec::ctypes::Type > parseUnion(const rapidjson::Value &jsonUnion)
Parses union from JSON representation.
Definition: json_ctypes_parser.cpp:762
retdec::ctypes::CompositeType::Members parseMembers(const rapidjson::Value &jsonMembers)
Parses composite type's members.
Definition: json_ctypes_parser.cpp:782
std::shared_ptr< retdec::ctypes::Type > parseFloatingPointType(const rapidjson::Value &type)
Parses floating point type from JSON representation.
Definition: json_ctypes_parser.cpp:595
retdec::ctypes::ArrayType::Dimensions parseArrayDimensions(const rapidjson::Value &jsonDimensions) const
Parses array dimensions from JSON representation.
Definition: json_ctypes_parser.cpp:828
std::shared_ptr< retdec::ctypes::Type > getOrParseType(const std::string &typeKey)
Returns C type from parser's context or parses it from JSON representation.
Definition: json_ctypes_parser.cpp:489
retdec::ctypes::CallConvention defaultCallConv
Call convention used when JSON does not contain one.
Definition: json_ctypes_parser.h:141
std::string loadJson(std::istream &stream) const
Loads JSON from the input stream to a string.
Definition: json_ctypes_parser.cpp:238
retdec::ctypes::FunctionType::Parameters parseFunctionTypeParameters(const rapidjson::Value &jsonParams)
Parses function type parameters from JSON representation.
Definition: json_ctypes_parser.cpp:473
std::shared_ptr< retdec::ctypes::FunctionType > parseFunctionType(const rapidjson::Value &jsonFuncType)
Parses function type from JSON representation.
Definition: json_ctypes_parser.cpp:455
void handleParsingFailure(const rapidjson::ParseResult &err) const
Gets additional info about failure in parsing and throws exception.
Definition: json_ctypes_parser.cpp:170
std::shared_ptr< retdec::ctypes::ArrayType > parseArray(const rapidjson::Value &jsonArray)
Parses array from JSON representation.
Definition: json_ctypes_parser.cpp:813
std::string parseCallConv(const rapidjson::Value &function) const
Returns call_conv attribute's value if exists, default otherwise.
Definition: json_ctypes_parser.cpp:410
retdec::ctypes::Function::Parameters parseParameters(const rapidjson::Value &jsonParams)
Parses function parameters from JSON representation.
Definition: json_ctypes_parser.cpp:359
std::shared_ptr< retdec::ctypes::PointerType > parsePointer(const rapidjson::Value &jsonPointer)
Parses pointer from JSON representation.
Definition: json_ctypes_parser.cpp:800
unsigned getBitWidthOrDefault(const std::string &typeName) const
Returns bit width stored in typeWidths for type, default if not found.
Definition: json_ctypes_parser.cpp:665
std::shared_ptr< retdec::ctypes::Type > parseTypedefedType(const rapidjson::Value &jsonTypedef)
Parses typedef from JSON representation.
Definition: json_ctypes_parser.cpp:675
JSONCTypesParser()
Constructs a new parser.
ShPtr< Module > module
The current module.
Definition: hll_writer.cpp:100
Definition: archive_wrapper.h:19