Home | History | Annotate | Download | only in slang
      1 /*
      2  * Copyright 2010-2012, The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_CONTEXT_H_  // NOLINT
     18 #define _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_CONTEXT_H_
     19 
     20 #include <cstdio>
     21 #include <list>
     22 #include <map>
     23 #include <string>
     24 
     25 #include "clang/Lex/Preprocessor.h"
     26 #include "clang/AST/Mangle.h"
     27 
     28 #include "llvm/ADT/OwningPtr.h"
     29 #include "llvm/ADT/StringSet.h"
     30 #include "llvm/ADT/StringMap.h"
     31 
     32 #include "slang_pragma_recorder.h"
     33 
     34 namespace llvm {
     35   class LLVMContext;
     36   class DataLayout;
     37 }   // namespace llvm
     38 
     39 namespace clang {
     40   class VarDecl;
     41   class ASTContext;
     42   class TargetInfo;
     43   class FunctionDecl;
     44   class SourceManager;
     45 }   // namespace clang
     46 
     47 namespace slang {
     48   class RSExportable;
     49   class RSExportVar;
     50   class RSExportFunc;
     51   class RSExportForEach;
     52   class RSExportType;
     53 
     54 class RSContext {
     55   typedef llvm::StringSet<> NeedExportVarSet;
     56   typedef llvm::StringSet<> NeedExportFuncSet;
     57   typedef llvm::StringSet<> NeedExportTypeSet;
     58 
     59  public:
     60   typedef std::list<RSExportable*> ExportableList;
     61   typedef std::list<RSExportVar*> ExportVarList;
     62   typedef std::list<RSExportFunc*> ExportFuncList;
     63   typedef std::list<RSExportForEach*> ExportForEachList;
     64   typedef llvm::StringMap<RSExportType*> ExportTypeMap;
     65 
     66  private:
     67   clang::Preprocessor &mPP;
     68   clang::ASTContext &mCtx;
     69   const clang::TargetInfo &mTarget;
     70   PragmaList *mPragmas;
     71   unsigned int mTargetAPI;
     72   std::vector<std::string> *mGeneratedFileNames;
     73 
     74   llvm::DataLayout *mDataLayout;
     75   llvm::LLVMContext &mLLVMContext;
     76 
     77   ExportableList mExportables;
     78 
     79   NeedExportTypeSet mNeedExportTypes;
     80 
     81   std::string *mLicenseNote;
     82   std::string mReflectJavaPackageName;
     83   std::string mReflectJavaPathName;
     84 
     85   std::string mRSPackageName;
     86 
     87   int version;
     88 
     89   bool mIsCompatLib;
     90 
     91   llvm::OwningPtr<clang::MangleContext> mMangleCtx;
     92 
     93   bool processExportVar(const clang::VarDecl *VD);
     94   bool processExportFunc(const clang::FunctionDecl *FD);
     95   bool processExportType(const llvm::StringRef &Name);
     96 
     97   void cleanupForEach();
     98 
     99   ExportVarList mExportVars;
    100   ExportFuncList mExportFuncs;
    101   ExportForEachList mExportForEach;
    102   ExportTypeMap mExportTypes;
    103 
    104  public:
    105   RSContext(clang::Preprocessor &PP,
    106             clang::ASTContext &Ctx,
    107             const clang::TargetInfo &Target,
    108             PragmaList *Pragmas,
    109             unsigned int TargetAPI,
    110             std::vector<std::string> *GeneratedFileNames);
    111 
    112   inline clang::Preprocessor &getPreprocessor() const { return mPP; }
    113   inline clang::ASTContext &getASTContext() const { return mCtx; }
    114   inline clang::MangleContext &getMangleContext() const {
    115     return *mMangleCtx;
    116   }
    117   inline const llvm::DataLayout *getDataLayout() const { return mDataLayout; }
    118   inline llvm::LLVMContext &getLLVMContext() const { return mLLVMContext; }
    119   inline const clang::SourceManager *getSourceManager() const {
    120     return &mPP.getSourceManager();
    121   }
    122   inline clang::DiagnosticsEngine *getDiagnostics() const {
    123     return &mPP.getDiagnostics();
    124   }
    125   inline unsigned int getTargetAPI() const {
    126     return mTargetAPI;
    127   }
    128 
    129   inline void setLicenseNote(const std::string &S) {
    130     mLicenseNote = new std::string(S);
    131   }
    132   inline const std::string *getLicenseNote() const { return mLicenseNote; }
    133 
    134   inline void addExportType(const std::string &S) {
    135     mNeedExportTypes.insert(S);
    136     return;
    137   }
    138 
    139   inline void setReflectJavaPackageName(const std::string &S) {
    140     mReflectJavaPackageName = S;
    141     return;
    142   }
    143   inline const std::string &getReflectJavaPackageName() const {
    144     return mReflectJavaPackageName;
    145   }
    146 
    147   inline void setRSPackageName(const std::string &S) {
    148     mRSPackageName = S;
    149     return;
    150   }
    151   inline const std::string &getRSPackageName() const {
    152     return mRSPackageName;
    153   }
    154 
    155   bool processExport();
    156   inline void newExportable(RSExportable *E) {
    157     if (E != NULL)
    158       mExportables.push_back(E);
    159   }
    160   typedef ExportableList::iterator exportable_iterator;
    161   exportable_iterator exportable_begin() {
    162     return mExportables.begin();
    163   }
    164   exportable_iterator exportable_end() {
    165     return mExportables.end();
    166   }
    167 
    168   typedef ExportVarList::const_iterator const_export_var_iterator;
    169   const_export_var_iterator export_vars_begin() const {
    170     return mExportVars.begin();
    171   }
    172   const_export_var_iterator export_vars_end() const {
    173     return mExportVars.end();
    174   }
    175   inline bool hasExportVar() const {
    176     return !mExportVars.empty();
    177   }
    178 
    179   typedef ExportFuncList::const_iterator const_export_func_iterator;
    180   const_export_func_iterator export_funcs_begin() const {
    181     return mExportFuncs.begin();
    182   }
    183   const_export_func_iterator export_funcs_end() const {
    184     return mExportFuncs.end();
    185   }
    186   inline bool hasExportFunc() const { return !mExportFuncs.empty(); }
    187 
    188   typedef ExportForEachList::const_iterator const_export_foreach_iterator;
    189   const_export_foreach_iterator export_foreach_begin() const {
    190     return mExportForEach.begin();
    191   }
    192   const_export_foreach_iterator export_foreach_end() const {
    193     return mExportForEach.end();
    194   }
    195   inline bool hasExportForEach() const { return !mExportForEach.empty(); }
    196 
    197   typedef ExportTypeMap::iterator export_type_iterator;
    198   typedef ExportTypeMap::const_iterator const_export_type_iterator;
    199   export_type_iterator export_types_begin() { return mExportTypes.begin(); }
    200   export_type_iterator export_types_end() { return mExportTypes.end(); }
    201   const_export_type_iterator export_types_begin() const {
    202     return mExportTypes.begin();
    203   }
    204   const_export_type_iterator export_types_end() const {
    205     return mExportTypes.end();
    206   }
    207   inline bool hasExportType() const { return !mExportTypes.empty(); }
    208   export_type_iterator findExportType(const llvm::StringRef &TypeName) {
    209     return mExportTypes.find(TypeName);
    210   }
    211   const_export_type_iterator findExportType(const llvm::StringRef &TypeName)
    212       const {
    213     return mExportTypes.find(TypeName);
    214   }
    215 
    216   // Insert the specified Typename/Type pair into the map. If the key already
    217   // exists in the map, return false and ignore the request, otherwise insert it
    218   // and return true.
    219   bool insertExportType(const llvm::StringRef &TypeName, RSExportType *Type);
    220 
    221   bool reflectToJava(const std::string &OutputPathBase,
    222                      const std::string &RSPackageName,
    223                      const std::string &InputFileName,
    224                      const std::string &OutputBCFileName);
    225 
    226   int getVersion() const { return version; }
    227   void setVersion(int v) {
    228     version = v;
    229     return;
    230   }
    231 
    232   bool isCompatLib() const { return mIsCompatLib; }
    233 
    234   void addPragma(const std::string &T, const std::string &V) {
    235     mPragmas->push_back(make_pair(T, V));
    236   }
    237 
    238   ~RSContext();
    239 };
    240 
    241 }   // namespace slang
    242 
    243 #endif  // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_CONTEXT_H_  NOLINT
    244