Home | History | Annotate | Download | only in src
      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 #pragma clang diagnostic push
     19 #pragma clang diagnostic ignored "-Wunused-parameter"
     20 #pragma clang diagnostic ignored "-Wnested-anon-types"
     21 #include "proto/abi_dump.pb.h"
     22 #pragma clang diagnostic pop
     23 
     24 #include <clang/AST/AST.h>
     25 #include <clang/AST/ASTConsumer.h>
     26 #include <clang/AST/Mangle.h>
     27 #include <clang/AST/VTableBuilder.h>
     28 #include <clang/Frontend/CompilerInstance.h>
     29 
     30 namespace abi_wrapper {
     31 class ABIWrapper {
     32  public:
     33   ABIWrapper(clang::MangleContext *mangle_contextp,
     34              clang::ASTContext *ast_contextp,
     35              const clang::CompilerInstance *cip);
     36 
     37   static std::string GetDeclSourceFile(const clang::Decl *decl,
     38                                        const clang::CompilerInstance *cip);
     39 
     40   static std::string GetMangledNameDecl(const clang::NamedDecl *decl,
     41                                         clang::MangleContext *mangle_context);
     42  protected:
     43   abi_dump::AccessSpecifier AccessClangToDump(
     44       const clang::AccessSpecifier sp) const;
     45 
     46   bool SetupTemplateParamNames(abi_dump::TemplateInfo *tinfo,
     47                                clang::TemplateParameterList *pl) const;
     48 
     49   bool SetupTemplateArguments(abi_dump::TemplateInfo *tinfo,
     50                               const clang::TemplateArgumentList *tl) const;
     51 
     52   std::string QualTypeToString(const clang::QualType &sweet_qt) const;
     53 
     54   std::string GetTagDeclQualifiedName(const clang::TagDecl *decl) const;
     55 
     56   bool SetupBasicTypeAbi(abi_dump::BasicTypeAbi *type_abi,
     57                          const clang::QualType type, bool dump_size) const;
     58 
     59   bool SetupBasicNamedAndTypedDecl(
     60       abi_dump::BasicNamedAndTypedDecl *basic_named_and_typed_decl,
     61       const clang::QualType type, const std::string &name,
     62       const clang::AccessSpecifier &access, std::string key,
     63       bool dump_size) const;
     64 
     65   std::string GetTypeLinkageName(const clang::Type *typep) const;
     66 
     67 protected:
     68   const clang::CompilerInstance *cip_;
     69   clang::MangleContext *mangle_contextp_;
     70   clang::ASTContext *ast_contextp_;
     71 };
     72 
     73 class RecordDeclWrapper : public ABIWrapper {
     74  public:
     75   RecordDeclWrapper(clang::MangleContext *mangle_contextp,
     76                     clang::ASTContext *ast_contextp,
     77                     const clang::CompilerInstance *compiler_instance_p,
     78                     const clang::RecordDecl *decl);
     79 
     80   std::unique_ptr<abi_dump::RecordDecl> GetRecordDecl() const;
     81 
     82  private:
     83   const clang::RecordDecl *record_decl_;
     84 
     85  private:
     86   bool SetupRecordInfo(abi_dump::RecordDecl *record_declp,
     87                        const std::string &source_file) const;
     88 
     89   bool SetupRecordFields(abi_dump::RecordDecl *record_declp) const;
     90 
     91   bool SetupCXXBases(abi_dump::RecordDecl *cxxp,
     92                      const clang::CXXRecordDecl *cxx_record_decl) const;
     93 
     94   bool SetupTemplateInfo(abi_dump::RecordDecl *record_declp,
     95                          const clang::CXXRecordDecl *cxx_record_decl) const;
     96 
     97   bool SetupRecordVTable(abi_dump::RecordDecl *record_declp,
     98                          const clang::CXXRecordDecl *cxx_record_decl) const;
     99   bool SetupRecordVTableComponent(
    100       abi_dump::VTableComponent *added_vtable_component,
    101       const clang::VTableComponent &vtable_component) const;
    102 
    103   bool SetupCXXRecordInfo(abi_dump::RecordDecl *record_declp) const;
    104 };
    105 
    106 class FunctionDeclWrapper : public ABIWrapper {
    107  public:
    108   FunctionDeclWrapper(clang::MangleContext *mangle_contextp,
    109                       clang::ASTContext *ast_contextp,
    110                       const clang::CompilerInstance *compiler_instance_p,
    111                       const clang::FunctionDecl *decl);
    112 
    113   std::unique_ptr<abi_dump::FunctionDecl> GetFunctionDecl() const;
    114 
    115  private:
    116   const clang::FunctionDecl *function_decl_;
    117 
    118  private:
    119   bool SetupFunction(abi_dump::FunctionDecl *methodp,
    120                      const std::string &source_file) const;
    121 
    122   bool SetupTemplateInfo(abi_dump::FunctionDecl *functionp) const;
    123 
    124   bool SetupFunctionParameters(abi_dump::FunctionDecl *functionp) const;
    125 };
    126 
    127 class EnumDeclWrapper : public ABIWrapper {
    128  public:
    129   EnumDeclWrapper(clang::MangleContext *mangle_contextp,
    130                   clang::ASTContext *ast_contextp,
    131                   const clang::CompilerInstance *compiler_instance_p,
    132                   const clang::EnumDecl *decl);
    133 
    134   std::unique_ptr<abi_dump::EnumDecl> GetEnumDecl() const;
    135 
    136  private:
    137   const clang::EnumDecl *enum_decl_;
    138 
    139  private:
    140   bool SetupEnum(abi_dump::EnumDecl *enump,
    141                  const std::string &source_file) const;
    142   bool SetupEnumFields(abi_dump::EnumDecl *enump) const;
    143 };
    144 
    145 class GlobalVarDeclWrapper : public ABIWrapper {
    146  public:
    147   GlobalVarDeclWrapper(clang::MangleContext *mangle_contextp,
    148                   clang::ASTContext *ast_contextp,
    149                   const clang::CompilerInstance *compiler_instance_p,
    150                   const clang::VarDecl *decl);
    151 
    152   std::unique_ptr<abi_dump::GlobalVarDecl> GetGlobalVarDecl() const;
    153 
    154  private:
    155   const clang::VarDecl *global_var_decl_;
    156  private:
    157   bool SetupGlobalVar(abi_dump::GlobalVarDecl *global_varp,
    158                       const std::string &source_file) const;
    159 };
    160 
    161 } //end namespace abi_wrapper
    162 
    163 #endif  // ABI_WRAPPERS_H_
    164