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