1 // Copyright (C) 2019 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef HEADER_CHECKER_REPR_JSON_IR_READER_H_
16 #define HEADER_CHECKER_REPR_JSON_IR_READER_H_
17 
18 #include "repr/ir_dumper.h"
19 #include "repr/ir_reader.h"
20 #include "repr/ir_representation.h"
21 
22 #include <json/value.h>
23 
24 
25 namespace header_checker {
26 namespace repr {
27 
28 
29 template <typename T> class JsonArrayRef;
30 
31 // This class loads values from a read-only JSON object.
32 class JsonObjectRef {
33  public:
34   // The constructor sets ok to false if json_value is not an object.
35   JsonObjectRef(const Json::Value &json_value, bool &ok);
36 
37   // This method gets a value from the object and checks the type.
38   // If the type mismatches, it sets ok_ to false and returns default value.
39   // If the key doesn't exist, it doesn't change ok_ and returns default value.
40   // Default to false.
41   bool GetBool(const std::string &key) const;
42 
43   // Default to 0.
44   int64_t GetInt(const std::string &key) const;
45 
46   // Default to 0.
47   uint64_t GetUint(const std::string &key) const;
48 
49   // Default to "".
50   std::string GetString(const std::string &key) const;
51 
52   // Default to {}.
53   JsonObjectRef GetObject(const std::string &key) const;
54 
55   // Default to [].
56   JsonArrayRef<JsonObjectRef> GetObjects(const std::string &key) const;
57 
58   JsonArrayRef<std::string> GetStrings(const std::string &key) const;
59 
60  private:
61   typedef bool (Json::Value::*IsExpectedJsonType)() const;
62 
63   const Json::Value &Get(const std::string &key,
64                          const Json::Value &default_value,
65                          IsExpectedJsonType is_expected_type) const;
66 
67   const Json::Value &object_;
68   bool &ok_;
69 };
70 
71 // This class loads elements as type T from a read-only JSON array.
72 template <typename T> class JsonArrayRef {
73  public:
74   class Iterator {
75    public:
Iterator(const Json::Value & json_value,bool & ok,int index)76     Iterator(const Json::Value &json_value, bool &ok, int index)
77         : array_(json_value), ok_(ok), index_(index) {}
78 
79     Iterator &operator++() {
80       ++index_;
81       return *this;
82     }
83 
84     bool operator!=(const Iterator &other) const {
85       return index_ != other.index_;
86     }
87 
88     T operator*() const;
89 
90    private:
91     const Json::Value &array_;
92     bool &ok_;
93     int index_;
94   };
95 
96   // The caller ensures json_value.isArray() == true.
JsonArrayRef(const Json::Value & json_value,bool & ok)97   JsonArrayRef(const Json::Value &json_value, bool &ok)
98       : array_(json_value), ok_(ok) {}
99 
begin()100   Iterator begin() const { return Iterator(array_, ok_, 0); }
101 
end()102   Iterator end() const { return Iterator(array_, ok_, array_.size()); }
103 
104  private:
105   const Json::Value &array_;
106   bool &ok_;
107 };
108 
109 template <>
110 JsonObjectRef JsonArrayRef<JsonObjectRef>::Iterator::operator*() const;
111 
112 template <> std::string JsonArrayRef<std::string>::Iterator::operator*() const;
113 
114 class JsonIRReader : public IRReader {
115  public:
JsonIRReader(const std::set<std::string> * exported_headers)116   JsonIRReader(const std::set<std::string> *exported_headers)
117       : IRReader(exported_headers) {}
118 
119  private:
120   bool ReadDumpImpl(const std::string &dump_file) override;
121 
122   void ReadFunctions(const JsonObjectRef &tu);
123 
124   void ReadGlobalVariables(const JsonObjectRef &tu);
125 
126   void ReadEnumTypes(const JsonObjectRef &tu);
127 
128   void ReadRecordTypes(const JsonObjectRef &tu);
129 
130   void ReadFunctionTypes(const JsonObjectRef &tu);
131 
132   void ReadPointerTypes(const JsonObjectRef &tu);
133 
134   void ReadBuiltinTypes(const JsonObjectRef &tu);
135 
136   void ReadQualifiedTypes(const JsonObjectRef &tu);
137 
138   void ReadArrayTypes(const JsonObjectRef &tu);
139 
140   void ReadLvalueReferenceTypes(const JsonObjectRef &tu);
141 
142   void ReadRvalueReferenceTypes(const JsonObjectRef &tu);
143 
144   void ReadElfFunctions(const JsonObjectRef &tu);
145 
146   void ReadElfObjects(const JsonObjectRef &tu);
147 
148   static void ReadTemplateInfo(const JsonObjectRef &type_decl,
149                                TemplatedArtifactIR *template_ir);
150 
151   static void ReadTypeInfo(const JsonObjectRef &type_decl, TypeIR *type_ir);
152 
153   static void ReadRecordFields(const JsonObjectRef &record_type,
154                                RecordTypeIR *record_ir);
155 
156   static void ReadBaseSpecifiers(const JsonObjectRef &record_type,
157                                  RecordTypeIR *record_ir);
158 
159   static void ReadVTableLayout(const JsonObjectRef &record_type,
160                                RecordTypeIR *record_ir);
161 
162   static void ReadEnumFields(const JsonObjectRef &enum_type,
163                              EnumTypeIR *enum_ir);
164 
165   static void ReadFunctionParametersAndReturnType(const JsonObjectRef &function,
166                                                   CFunctionLikeIR *function_ir);
167 
168   static FunctionIR FunctionJsonToIR(const JsonObjectRef &function);
169 
170   static FunctionTypeIR
171   FunctionTypeJsonToIR(const JsonObjectRef &function_type);
172 
173   static RecordTypeIR RecordTypeJsonToIR(const JsonObjectRef &record_type);
174 
175   static EnumTypeIR EnumTypeJsonToIR(const JsonObjectRef &enum_type);
176 };
177 
178 
179 }  // namespace repr
180 }  // namespace header_checker
181 
182 
183 #endif  // HEADER_CHECKER_REPR_JSON_IR_READER_H_
184