xref: /aosp_15_r20/development/vndk/tools/header-checker/src/repr/json/ir_reader.h (revision 90c8c64db3049935a07c6143d7fd006e26f8ecca)
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_reader.h"
19 #include "repr/ir_representation.h"
20 
21 #include <json/value.h>
22 
23 
24 namespace header_checker {
25 namespace repr {
26 
27 
28 template <typename T> class JsonArrayRef;
29 
30 // This class loads values from a read-only JSON object.
31 class JsonObjectRef {
32  public:
33   // The constructor sets ok to false if json_value is not an object.
34   JsonObjectRef(const Json::Value &json_value, bool &ok);
35 
36   bool IsMember(const std::string &key) const;
37 
38   // This method gets a value from the object and checks the type.
39   // If the type mismatches, it sets ok_ to false and returns default value.
40   // If the key doesn't exist, it doesn't change ok_ and returns default value.
41   // Default to false.
42   bool GetBool(const std::string &key) const;
43 
44   // Default to 0.
45   int64_t GetInt(const std::string &key) const;
46 
47   // Default to 0.
48   uint64_t GetUint(const std::string &key) const;
49 
50   // Default to 0.
51   const Json::Value &GetIntegralValue(const std::string &key) const;
52 
53   // Default to "".
54   std::string GetString(const std::string &key) const;
55 
56   std::string GetString(const std::string &key,
57                         const std::string &default_value) const;
58 
59   // Default to {}.
60   JsonObjectRef GetObject(const std::string &key) const;
61 
62   // Default to [].
63   JsonArrayRef<JsonObjectRef> GetObjects(const std::string &key) const;
64 
65   JsonArrayRef<std::string> GetStrings(const std::string &key) const;
66 
67  private:
68   typedef bool (Json::Value::*IsExpectedJsonType)() const;
69 
70   const Json::Value &Get(const std::string &key,
71                          const Json::Value &default_value,
72                          IsExpectedJsonType is_expected_type) const;
73 
74   const Json::Value &object_;
75   bool &ok_;
76 };
77 
78 // This class loads elements as type T from a read-only JSON array.
79 template <typename T> class JsonArrayRef {
80  public:
81   class Iterator {
82    public:
Iterator(const Json::Value & json_value,bool & ok,int index)83     Iterator(const Json::Value &json_value, bool &ok, int index)
84         : array_(json_value), ok_(ok), index_(index) {}
85 
86     Iterator &operator++() {
87       ++index_;
88       return *this;
89     }
90 
91     bool operator!=(const Iterator &other) const {
92       return index_ != other.index_;
93     }
94 
95     T operator*() const;
96 
97    private:
98     const Json::Value &array_;
99     bool &ok_;
100     int index_;
101   };
102 
103   // The caller ensures json_value.isArray() == true.
JsonArrayRef(const Json::Value & json_value,bool & ok)104   JsonArrayRef(const Json::Value &json_value, bool &ok)
105       : array_(json_value), ok_(ok) {}
106 
begin()107   Iterator begin() const { return Iterator(array_, ok_, 0); }
108 
end()109   Iterator end() const { return Iterator(array_, ok_, array_.size()); }
110 
111  private:
112   const Json::Value &array_;
113   bool &ok_;
114 };
115 
116 template <>
117 JsonObjectRef JsonArrayRef<JsonObjectRef>::Iterator::operator*() const;
118 
119 template <> std::string JsonArrayRef<std::string>::Iterator::operator*() const;
120 
121 class JsonIRReader : public IRReader {
122  public:
JsonIRReader(std::unique_ptr<ModuleIR> module_ir)123   JsonIRReader(std::unique_ptr<ModuleIR> module_ir)
124       : IRReader(std::move(module_ir)) {}
125 
126  private:
127   bool ReadDumpImpl(const std::string &dump_file) override;
128 
129   void ReadFunctions(const JsonObjectRef &tu);
130 
131   void ReadGlobalVariables(const JsonObjectRef &tu);
132 
133   void ReadEnumTypes(const JsonObjectRef &tu);
134 
135   void ReadRecordTypes(const JsonObjectRef &tu);
136 
137   void ReadFunctionTypes(const JsonObjectRef &tu);
138 
139   void ReadPointerTypes(const JsonObjectRef &tu);
140 
141   void ReadBuiltinTypes(const JsonObjectRef &tu);
142 
143   void ReadQualifiedTypes(const JsonObjectRef &tu);
144 
145   void ReadArrayTypes(const JsonObjectRef &tu);
146 
147   void ReadLvalueReferenceTypes(const JsonObjectRef &tu);
148 
149   void ReadRvalueReferenceTypes(const JsonObjectRef &tu);
150 
151   void ReadElfFunctions(const JsonObjectRef &tu);
152 
153   void ReadElfObjects(const JsonObjectRef &tu);
154 
155   static void ReadTemplateInfo(const JsonObjectRef &type_decl,
156                                TemplatedArtifactIR *template_ir);
157 
158   static void ReadTypeInfo(const JsonObjectRef &type_decl, TypeIR *type_ir);
159 
160   static void ReadRecordFields(const JsonObjectRef &record_type,
161                                RecordTypeIR *record_ir);
162 
163   static void ReadBaseSpecifiers(const JsonObjectRef &record_type,
164                                  RecordTypeIR *record_ir);
165 
166   static void ReadVTableLayout(const JsonObjectRef &record_type,
167                                RecordTypeIR *record_ir);
168 
169   static void ReadEnumFields(const JsonObjectRef &enum_type,
170                              EnumTypeIR *enum_ir);
171 
172   static void ReadFunctionParametersAndReturnType(const JsonObjectRef &function,
173                                                   CFunctionLikeIR *function_ir);
174 
175   static FunctionIR FunctionJsonToIR(const JsonObjectRef &function);
176 
177   static FunctionTypeIR
178   FunctionTypeJsonToIR(const JsonObjectRef &function_type);
179 
180   static RecordTypeIR RecordTypeJsonToIR(const JsonObjectRef &record_type);
181 
182   static EnumTypeIR EnumTypeJsonToIR(const JsonObjectRef &enum_type);
183 };
184 
185 
186 }  // namespace repr
187 }  // namespace header_checker
188 
189 
190 #endif  // HEADER_CHECKER_REPR_JSON_IR_READER_H_
191