• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 0.
50   const Json::Value &GetIntegralValue(const std::string &key) const;
51 
52   // Default to "".
53   std::string GetString(const std::string &key) const;
54 
55   // Default to {}.
56   JsonObjectRef GetObject(const std::string &key) const;
57 
58   // Default to [].
59   JsonArrayRef<JsonObjectRef> GetObjects(const std::string &key) const;
60 
61   JsonArrayRef<std::string> GetStrings(const std::string &key) const;
62 
63  private:
64   typedef bool (Json::Value::*IsExpectedJsonType)() const;
65 
66   const Json::Value &Get(const std::string &key,
67                          const Json::Value &default_value,
68                          IsExpectedJsonType is_expected_type) const;
69 
70   const Json::Value &object_;
71   bool &ok_;
72 };
73 
74 // This class loads elements as type T from a read-only JSON array.
75 template <typename T> class JsonArrayRef {
76  public:
77   class Iterator {
78    public:
Iterator(const Json::Value & json_value,bool & ok,int index)79     Iterator(const Json::Value &json_value, bool &ok, int index)
80         : array_(json_value), ok_(ok), index_(index) {}
81 
82     Iterator &operator++() {
83       ++index_;
84       return *this;
85     }
86 
87     bool operator!=(const Iterator &other) const {
88       return index_ != other.index_;
89     }
90 
91     T operator*() const;
92 
93    private:
94     const Json::Value &array_;
95     bool &ok_;
96     int index_;
97   };
98 
99   // The caller ensures json_value.isArray() == true.
JsonArrayRef(const Json::Value & json_value,bool & ok)100   JsonArrayRef(const Json::Value &json_value, bool &ok)
101       : array_(json_value), ok_(ok) {}
102 
begin()103   Iterator begin() const { return Iterator(array_, ok_, 0); }
104 
end()105   Iterator end() const { return Iterator(array_, ok_, array_.size()); }
106 
107  private:
108   const Json::Value &array_;
109   bool &ok_;
110 };
111 
112 template <>
113 JsonObjectRef JsonArrayRef<JsonObjectRef>::Iterator::operator*() const;
114 
115 template <> std::string JsonArrayRef<std::string>::Iterator::operator*() const;
116 
117 class JsonIRReader : public IRReader {
118  public:
JsonIRReader(const std::set<std::string> * exported_headers)119   JsonIRReader(const std::set<std::string> *exported_headers)
120       : IRReader(exported_headers) {}
121 
122  private:
123   bool ReadDumpImpl(const std::string &dump_file) override;
124 
125   void ReadFunctions(const JsonObjectRef &tu);
126 
127   void ReadGlobalVariables(const JsonObjectRef &tu);
128 
129   void ReadEnumTypes(const JsonObjectRef &tu);
130 
131   void ReadRecordTypes(const JsonObjectRef &tu);
132 
133   void ReadFunctionTypes(const JsonObjectRef &tu);
134 
135   void ReadPointerTypes(const JsonObjectRef &tu);
136 
137   void ReadBuiltinTypes(const JsonObjectRef &tu);
138 
139   void ReadQualifiedTypes(const JsonObjectRef &tu);
140 
141   void ReadArrayTypes(const JsonObjectRef &tu);
142 
143   void ReadLvalueReferenceTypes(const JsonObjectRef &tu);
144 
145   void ReadRvalueReferenceTypes(const JsonObjectRef &tu);
146 
147   void ReadElfFunctions(const JsonObjectRef &tu);
148 
149   void ReadElfObjects(const JsonObjectRef &tu);
150 
151   static void ReadTemplateInfo(const JsonObjectRef &type_decl,
152                                TemplatedArtifactIR *template_ir);
153 
154   static void ReadTypeInfo(const JsonObjectRef &type_decl, TypeIR *type_ir);
155 
156   static void ReadRecordFields(const JsonObjectRef &record_type,
157                                RecordTypeIR *record_ir);
158 
159   static void ReadBaseSpecifiers(const JsonObjectRef &record_type,
160                                  RecordTypeIR *record_ir);
161 
162   static void ReadVTableLayout(const JsonObjectRef &record_type,
163                                RecordTypeIR *record_ir);
164 
165   static void ReadEnumFields(const JsonObjectRef &enum_type,
166                              EnumTypeIR *enum_ir);
167 
168   static void ReadFunctionParametersAndReturnType(const JsonObjectRef &function,
169                                                   CFunctionLikeIR *function_ir);
170 
171   static FunctionIR FunctionJsonToIR(const JsonObjectRef &function);
172 
173   static FunctionTypeIR
174   FunctionTypeJsonToIR(const JsonObjectRef &function_type);
175 
176   static RecordTypeIR RecordTypeJsonToIR(const JsonObjectRef &record_type);
177 
178   static EnumTypeIR EnumTypeJsonToIR(const JsonObjectRef &enum_type);
179 };
180 
181 
182 }  // namespace repr
183 }  // namespace header_checker
184 
185 
186 #endif  // HEADER_CHECKER_REPR_JSON_IR_READER_H_
187