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