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