xref: /aosp_15_r20/development/vndk/tools/header-checker/src/dumper/abi_wrappers.h (revision 90c8c64db3049935a07c6143d7fd006e26f8ecca)
1 // Copyright (C) 2016 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 ABI_WRAPPERS_H_
16 #define ABI_WRAPPERS_H_
17 
18 #include "dumper/ast_util.h"
19 #include "repr/ir_representation.h"
20 
21 #include <clang/AST/AST.h>
22 #include <clang/AST/ASTConsumer.h>
23 #include <clang/AST/Mangle.h>
24 #include <clang/AST/VTableBuilder.h>
25 #include <clang/Frontend/CompilerInstance.h>
26 
27 
28 namespace header_checker {
29 namespace dumper {
30 
31 
32 struct TypeAndCreationStatus {
33   std::unique_ptr<repr::TypeIR> typep_;
34   bool should_create_type_;  // Whether the type is to be created.
35   TypeAndCreationStatus(std::unique_ptr<repr::TypeIR> &&typep,
36                         bool should_create_type = true)
typep_TypeAndCreationStatus37       : typep_(std::move(typep)), should_create_type_(should_create_type) {}
38 };
39 
40 
41 class ABIWrapper {
42  public:
43   ABIWrapper(clang::MangleContext *mangle_contextp,
44              clang::ASTContext *ast_contextp,
45              const clang::CompilerInstance *cip,
46              repr::ModuleIR *module,
47              ASTCaches *ast_caches);
48 
49  public:
50   static std::string GetDeclSourceFile(const clang::Decl *decl,
51                                        const clang::CompilerInstance *cip,
52                                        const utils::RootDirs &root_dirs);
53 
54  protected:
55   std::string GetCachedDeclSourceFile(const clang::Decl *decl,
56                                       const clang::CompilerInstance *cip);
57 
58  public:
59   static std::string GetMangledNameDecl(const clang::NamedDecl *decl,
60                                         clang::MangleContext *mangle_context);
61 
62  protected:
63   // Shared between FunctionDeclWrapper and RecordDeclWrapper.
64   bool SetupTemplateArguments(const clang::TemplateArgumentList *tl,
65                               repr::TemplatedArtifactIR *ta,
66                               const std::string &source_file);
67 
68   // Shared between FunctionTypeWrapper and FunctionDeclWrapper.
69   bool SetupFunctionParameter(repr::CFunctionLikeIR *functionp,
70                               const clang::QualType qual_type,
71                               bool has_default_arg,
72                               const std::string &source_file,
73                               bool is_this_parameter = false);
74 
75   // Shared between FunctionDeclWrapper, GlobalVarDeclWrapper,
76   // RecordDeclWrapper, and EnumDeclWrapper.
77   void SetupAvailabilityAttrs(repr::HasAvailabilityAttrs *decl_ir,
78                               const clang::Decl *decl);
79 
80  protected:
81   // Type-related functions
82   std::string GetTypeUniqueId(clang::QualType qual_type);
83 
84   bool CreateBasicNamedAndTypedDecl(clang::QualType,
85                                     const std::string &source_file);
86 
87   bool CreateBasicNamedAndTypedDecl(clang::QualType canonical_type,
88                                     repr::TypeIR *typep,
89                                     const std::string &source_file);
90 
91   bool CreateExtendedType(clang::QualType canonical_type,
92                           repr::TypeIR *typep);
93 
94  private:
95   std::string QualTypeToString(const clang::QualType &sweet_qt);
96 
97   TypeAndCreationStatus SetTypeKind(const clang::QualType qtype,
98                                     const std::string &source_file);
99 
100   bool CreateAnonymousRecord(const clang::RecordDecl *decl);
101 
102  protected:
103   const clang::CompilerInstance *cip_;
104   clang::MangleContext *mangle_contextp_;
105   clang::ASTContext *ast_contextp_;
106   repr::ModuleIR *module_;
107   ASTCaches *ast_caches_;
108 };
109 
110 
111 class RecordDeclWrapper : public ABIWrapper {
112  public:
113   RecordDeclWrapper(
114       clang::MangleContext *mangle_contextp, clang::ASTContext *ast_contextp,
115       const clang::CompilerInstance *compiler_instance_p,
116       const clang::RecordDecl *record_decl, repr::ModuleIR *module,
117       ASTCaches *ast_caches);
118 
119   bool GetRecordDecl();
120 
121  private:
122   const clang::RecordDecl *record_decl_;
123 
124  private:
125   bool SetupRecordInfo(repr::RecordTypeIR *type,
126                        const std::string &source_file);
127 
128   bool SetupRecordFields(repr::RecordTypeIR *record_declp,
129                          const std::string &source_file);
130 
131   bool SetupCXXBases(repr::RecordTypeIR *cxxp,
132                      const clang::CXXRecordDecl *cxx_record_decl);
133 
134   bool SetupTemplateInfo(repr::RecordTypeIR *record_declp,
135                          const clang::CXXRecordDecl *cxx_record_decl,
136                          const std::string &source_file);
137 
138   bool SetupRecordVTable(repr::RecordTypeIR *record_declp,
139                          const clang::CXXRecordDecl *cxx_record_decl);
140 
141   std::string GetMangledRTTI(const clang::CXXRecordDecl *cxx_record_decl);
142 
143   repr::VTableComponentIR
144   SetupRecordVTableComponent(const clang::VTableComponent &vtable_component,
145                              const clang::ThunkInfo &thunk_info);
146 
147   bool SetupCXXRecordInfo(repr::RecordTypeIR *record_declp,
148                           const std::string &source_file);
149 };
150 
151 
152 class FunctionDeclWrapper : public ABIWrapper {
153  public:
154   FunctionDeclWrapper(
155       clang::MangleContext *mangle_contextp, clang::ASTContext *ast_contextp,
156       const clang::CompilerInstance *compiler_instance_p,
157       const clang::FunctionDecl *decl, repr::ModuleIR *module,
158       ASTCaches *ast_caches);
159 
160   std::unique_ptr<repr::FunctionIR> GetFunctionDecl();
161 
162  private:
163   const clang::FunctionDecl *function_decl_;
164 
165  private:
166   bool SetupFunction(repr::FunctionIR *methodp,
167                      const std::string &source_file);
168 
169   bool SetupTemplateInfo(repr::FunctionIR *functionp,
170                          const std::string &source_file);
171 
172   bool SetupFunctionParameters(repr::FunctionIR *functionp,
173                                const std::string &source_file);
174 
175   bool SetupThisParameter(repr::FunctionIR *functionp,
176                           const std::string &source_file);
177 };
178 
179 
180 class FunctionTypeWrapper : public ABIWrapper {
181  private:
182   bool SetupFunctionType(repr::FunctionTypeIR *function_type_ir);
183 
184  public:
185   FunctionTypeWrapper(
186       clang::MangleContext *mangle_contextp, clang::ASTContext *ast_contextp,
187       const clang::CompilerInstance *compiler_instance_p,
188       const clang::FunctionType *function_type, repr::ModuleIR *module,
189       ASTCaches *ast_caches, const std::string &source_file);
190 
191   bool GetFunctionType();
192 
193  private:
194   const clang::FunctionType *function_type_= nullptr;
195   const std::string &source_file_;
196 };
197 
198 
199 class EnumDeclWrapper : public ABIWrapper {
200  public:
201   EnumDeclWrapper(
202       clang::MangleContext *mangle_contextp, clang::ASTContext *ast_contextp,
203       const clang::CompilerInstance *compiler_instance_p,
204       const clang::EnumDecl *decl, repr::ModuleIR *module,
205       ASTCaches *ast_caches);
206 
207   bool GetEnumDecl();
208 
209  private:
210   const clang::EnumDecl *enum_decl_;
211 
212  private:
213   bool SetupEnum(repr::EnumTypeIR *type,
214                  const std::string &source_file);
215 
216   bool SetupEnumFields(repr::EnumTypeIR *enump);
217 };
218 
219 
220 class GlobalVarDeclWrapper : public ABIWrapper {
221  public:
222   GlobalVarDeclWrapper(
223       clang::MangleContext *mangle_contextp, clang::ASTContext *ast_contextp,
224       const clang::CompilerInstance *compiler_instance_p,
225       const clang::VarDecl *decl, repr::ModuleIR *module,
226       ASTCaches *ast_caches);
227 
228   bool GetGlobalVarDecl();
229 
230  private:
231   const clang::VarDecl *global_var_decl_;
232 
233  private:
234   bool SetupGlobalVar(repr::GlobalVarIR *global_varp,
235                       const std::string &source_file);
236 };
237 
238 
239 }  // namespace dumper
240 }  // namespace header_checker
241 
242 
243 #endif  // ABI_WRAPPERS_H_
244