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 
53  protected:
54   std::string GetCachedDeclSourceFile(const clang::Decl *decl,
55                                       const clang::CompilerInstance *cip);
56 
57  public:
58   static std::string GetMangledNameDecl(const clang::NamedDecl *decl,
59                                         clang::MangleContext *mangle_context);
60 
61  protected:
62   // Shared between FunctionDeclWrapper and RecordDeclWrapper.
63   bool SetupTemplateArguments(const clang::TemplateArgumentList *tl,
64                               repr::TemplatedArtifactIR *ta,
65                               const std::string &source_file);
66 
67  protected:
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  protected:
76   // Type-related functions
77   std::string GetTypeUniqueId(clang::QualType qual_type);
78 
79   bool CreateBasicNamedAndTypedDecl(clang::QualType,
80                                     const std::string &source_file);
81 
82   bool CreateBasicNamedAndTypedDecl(clang::QualType canonical_type,
83                                     repr::TypeIR *typep,
84                                     const std::string &source_file);
85 
86   bool CreateExtendedType(clang::QualType canonical_type,
87                           repr::TypeIR *typep);
88 
89  private:
90   std::string QualTypeToString(const clang::QualType &sweet_qt);
91 
92   TypeAndCreationStatus SetTypeKind(const clang::QualType qtype,
93                                     const std::string &source_file);
94 
95   bool CreateAnonymousRecord(const clang::RecordDecl *decl);
96 
97  protected:
98   const clang::CompilerInstance *cip_;
99   clang::MangleContext *mangle_contextp_;
100   clang::ASTContext *ast_contextp_;
101   repr::ModuleIR *module_;
102   ASTCaches *ast_caches_;
103 };
104 
105 
106 class RecordDeclWrapper : public ABIWrapper {
107  public:
108   RecordDeclWrapper(
109       clang::MangleContext *mangle_contextp, clang::ASTContext *ast_contextp,
110       const clang::CompilerInstance *compiler_instance_p,
111       const clang::RecordDecl *record_decl, repr::ModuleIR *module,
112       ASTCaches *ast_caches);
113 
114   bool GetRecordDecl();
115 
116  private:
117   const clang::RecordDecl *record_decl_;
118 
119  private:
120   bool SetupRecordInfo(repr::RecordTypeIR *type,
121                        const std::string &source_file);
122 
123   bool SetupRecordFields(repr::RecordTypeIR *record_declp,
124                          const std::string &source_file);
125 
126   bool SetupCXXBases(repr::RecordTypeIR *cxxp,
127                      const clang::CXXRecordDecl *cxx_record_decl);
128 
129   bool SetupTemplateInfo(repr::RecordTypeIR *record_declp,
130                          const clang::CXXRecordDecl *cxx_record_decl,
131                          const std::string &source_file);
132 
133   bool SetupRecordVTable(repr::RecordTypeIR *record_declp,
134                          const clang::CXXRecordDecl *cxx_record_decl);
135 
136   std::string GetMangledRTTI(const clang::CXXRecordDecl *cxx_record_decl);
137 
138   repr::VTableComponentIR
139   SetupRecordVTableComponent(const clang::VTableComponent &vtable_component,
140                              const clang::ThunkInfo &thunk_info);
141 
142   bool SetupCXXRecordInfo(repr::RecordTypeIR *record_declp,
143                           const std::string &source_file);
144 };
145 
146 
147 class FunctionDeclWrapper : public ABIWrapper {
148  public:
149   FunctionDeclWrapper(
150       clang::MangleContext *mangle_contextp, clang::ASTContext *ast_contextp,
151       const clang::CompilerInstance *compiler_instance_p,
152       const clang::FunctionDecl *decl, repr::ModuleIR *module,
153       ASTCaches *ast_caches);
154 
155   std::unique_ptr<repr::FunctionIR> GetFunctionDecl();
156 
157  private:
158   const clang::FunctionDecl *function_decl_;
159 
160  private:
161   bool SetupFunction(repr::FunctionIR *methodp,
162                      const std::string &source_file);
163 
164   bool SetupTemplateInfo(repr::FunctionIR *functionp,
165                          const std::string &source_file);
166 
167   bool SetupFunctionParameters(repr::FunctionIR *functionp,
168                                const std::string &source_file);
169 
170   bool SetupThisParameter(repr::FunctionIR *functionp,
171                           const std::string &source_file);
172 };
173 
174 
175 class FunctionTypeWrapper : public ABIWrapper {
176  private:
177   bool SetupFunctionType(repr::FunctionTypeIR *function_type_ir);
178 
179  public:
180   FunctionTypeWrapper(
181       clang::MangleContext *mangle_contextp, clang::ASTContext *ast_contextp,
182       const clang::CompilerInstance *compiler_instance_p,
183       const clang::FunctionType *function_type, repr::ModuleIR *module,
184       ASTCaches *ast_caches, const std::string &source_file);
185 
186   bool GetFunctionType();
187 
188  private:
189   const clang::FunctionType *function_type_= nullptr;
190   const std::string &source_file_;
191 };
192 
193 
194 class EnumDeclWrapper : public ABIWrapper {
195  public:
196   EnumDeclWrapper(
197       clang::MangleContext *mangle_contextp, clang::ASTContext *ast_contextp,
198       const clang::CompilerInstance *compiler_instance_p,
199       const clang::EnumDecl *decl, repr::ModuleIR *module,
200       ASTCaches *ast_caches);
201 
202   bool GetEnumDecl();
203 
204  private:
205   const clang::EnumDecl *enum_decl_;
206 
207  private:
208   bool SetupEnum(repr::EnumTypeIR *type,
209                  const std::string &source_file);
210 
211   bool SetupEnumFields(repr::EnumTypeIR *enump);
212 };
213 
214 
215 class GlobalVarDeclWrapper : public ABIWrapper {
216  public:
217   GlobalVarDeclWrapper(
218       clang::MangleContext *mangle_contextp, clang::ASTContext *ast_contextp,
219       const clang::CompilerInstance *compiler_instance_p,
220       const clang::VarDecl *decl, repr::ModuleIR *module,
221       ASTCaches *ast_caches);
222 
223   bool GetGlobalVarDecl();
224 
225  private:
226   const clang::VarDecl *global_var_decl_;
227 
228  private:
229   bool SetupGlobalVar(repr::GlobalVarIR *global_varp,
230                       const std::string &source_file);
231 };
232 
233 
234 }  // namespace dumper
235 }  // namespace header_checker
236 
237 
238 #endif  // ABI_WRAPPERS_H_
239