retdec
json_ctypes_parser.h
Go to the documentation of this file.
1 
7 #ifndef RETDEC_CTYPESPARSER_JSON_CTYPES_PARSER_H
8 #define RETDEC_CTYPESPARSER_JSON_CTYPES_PARSER_H
9 
10 #include <functional>
11 #include <string>
12 #include <unordered_map>
13 
14 #include <rapidjson/document.h>
15 
17 
18 namespace retdec {
19 namespace ctypesparser {
20 
25 {
26  public:
29 
30  virtual std::unique_ptr<retdec::ctypes::Module> parse(
31  std::istream &stream,
32  const TypeWidths &typeWidths = {},
34  virtual void parseInto(
35  std::istream &stream,
36  std::unique_ptr<retdec::ctypes::Module> &module,
37  const TypeWidths &typeWidths = {},
39 
40  private:
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);
46  void addTypesToMap(const rapidjson::Value &types);
47 
50  void handleParsingFailure(const rapidjson::ParseResult &err) const;
51  std::shared_ptr<retdec::ctypes::Function> getOrParseFunction(
52  const std::string &name,
53  const rapidjson::Value &jsonFunction
54  );
55  std::shared_ptr<retdec::ctypes::Function> parseFunction(
56  const rapidjson::Value &function,
57  const std::string &fName
58  );
60  const rapidjson::Value &jsonParams
61  );
63  const rapidjson::Value &param
64  );
66  const rapidjson::Value &function
67  ) const;
68  std::string parseCallConv(
69  const rapidjson::Value &function
70  ) const;
72  const std::string &annot
73  ) const;
74  std::shared_ptr<retdec::ctypes::FunctionType> parseFunctionType(
75  const rapidjson::Value &jsonFuncType
76  );
78  const rapidjson::Value &jsonParams
79  );
80  std::shared_ptr<retdec::ctypes::Type> getOrParseType(
81  const std::string &typeKey
82  );
83  std::shared_ptr<retdec::ctypes::Type> parseType(
84  const std::string &typeKey
85  );
86  std::shared_ptr<retdec::ctypes::Type> parseIntegralType(
87  const rapidjson::Value &type
88  );
89  std::shared_ptr<retdec::ctypes::Type> parseFloatingPointType(
90  const rapidjson::Value &type
91  );
92  std::shared_ptr<retdec::ctypes::Type> parseTypedefedType(
93  const rapidjson::Value &jsonTypedef
94  );
95  std::shared_ptr<retdec::ctypes::Type> parseStruct(
96  const rapidjson::Value &jsonStruct
97  );
98  std::shared_ptr<retdec::ctypes::Type> parseUnion(
99  const rapidjson::Value &jsonUnion
100  );
102  const rapidjson::Value &jsonMembers
103  );
104  std::shared_ptr<retdec::ctypes::PointerType> parsePointer(
105  const rapidjson::Value &jsonPointer
106  );
107  std::shared_ptr<retdec::ctypes::ArrayType> parseArray(
108  const rapidjson::Value &jsonArray
109  );
111  const rapidjson::Value &jsonDimensions
112  ) const;
113  std::shared_ptr<retdec::ctypes::Type> parseEnum(
114  const rapidjson::Value &jsonEnum
115  );
117  const rapidjson::Value &jsonEnumItems
118  ) const;
119  std::shared_ptr<retdec::ctypes::Type> getOrParseNamedType(
120  const rapidjson::Value &jsonType,
121  const std::function<
122  std::shared_ptr<retdec::ctypes::Type> (const std::string &typeName)
123  > &parseType
124  );
125  unsigned getIntegralTypeBitWidth(const std::string &type) const;
126  unsigned getBitWidthOrDefault(const std::string &typeName) const;
128 
129  private:
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>;
132 
133  private:
136 
139 
142 };
143 
144 } // namespace ctypesparser
145 } // namespace retdec
146 
147 #endif
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 &param)
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.
Parser for C-types.
ShPtr< Module > module
The current module.
Definition: hll_writer.cpp:100
Definition: archive_wrapper.h:19