slang_rs_context.h revision c383a500aa59423264811be3874461bf8adbfea0
1/* 2 * Copyright 2010, 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 _SLANG_COMPILER_RS_CONTEXT_H 18#define _SLANG_COMPILER_RS_CONTEXT_H 19 20#include <map> 21#include <list> 22#include <string> 23#include <cstdio> 24 25#include "llvm/ADT/StringSet.h" 26#include "llvm/ADT/StringMap.h" 27 28#include "clang/Lex/Preprocessor.h" 29 30namespace llvm { 31 class LLVMContext; 32 class TargetData; 33} // namespace llvm 34 35namespace clang { 36 class VarDecl; 37 class ASTContext; 38 class TargetInfo; 39 class FunctionDecl; 40 class SourceManager; 41} // namespace clang 42 43namespace slang { 44 class RSExportable; 45 class RSExportVar; 46 class RSExportFunc; 47 class RSExportType; 48 49class RSContext { 50 typedef llvm::StringSet<> NeedExportVarSet; 51 typedef llvm::StringSet<> NeedExportFuncSet; 52 typedef llvm::StringSet<> NeedExportTypeSet; 53 54 public: 55 typedef std::list<RSExportable*> ExportableList; 56 typedef std::list<RSExportVar*> ExportVarList; 57 typedef std::list<RSExportFunc*> ExportFuncList; 58 typedef llvm::StringMap<RSExportType*> ExportTypeMap; 59 60 private: 61 clang::Preprocessor *mPP; 62 clang::ASTContext *mCtx; 63 const clang::TargetInfo *mTarget; 64 65 llvm::TargetData *mTargetData; 66 llvm::LLVMContext &mLLVMContext; 67 68 ExportableList mExportables; 69 70 // Record the variables/types/elements annotated in #pragma to be exported 71 NeedExportVarSet mNeedExportVars; 72 NeedExportFuncSet mNeedExportFuncs; 73 NeedExportTypeSet mNeedExportTypes; 74 bool mExportAllNonStaticVars; 75 bool mExportAllNonStaticFuncs; 76 77 std::string *mLicenseNote; 78 std::string mReflectJavaPackageName; 79 std::string mReflectJavaPathName; 80 81 bool processExportVar(const clang::VarDecl *VD); 82 bool processExportFunc(const clang::FunctionDecl *FD); 83 bool processExportType(const llvm::StringRef &Name); 84 85 ExportVarList mExportVars; 86 ExportFuncList mExportFuncs; 87 ExportTypeMap mExportTypes; 88 89 public: 90 RSContext(clang::Preprocessor *PP, 91 clang::ASTContext *Ctx, 92 const clang::TargetInfo *Target); 93 94 inline clang::Preprocessor *getPreprocessor() const { return mPP; } 95 inline clang::ASTContext *getASTContext() const { return mCtx; } 96 inline const llvm::TargetData *getTargetData() const { return mTargetData; } 97 inline llvm::LLVMContext &getLLVMContext() const { return mLLVMContext; } 98 inline const clang::SourceManager *getSourceManager() const { 99 return &mPP->getSourceManager(); 100 } 101 102 inline void setLicenseNote(const std::string &S) { 103 mLicenseNote = new std::string(S); 104 } 105 inline const std::string *getLicenseNote() const { return mLicenseNote; } 106 107 inline void addExportVar(const std::string &S) { 108 mNeedExportVars.insert(S); 109 return; 110 } 111 inline void addExportFunc(const std::string &S) { 112 mNeedExportFuncs.insert(S); 113 return; 114 } 115 inline void addExportType(const std::string &S) { 116 mNeedExportTypes.insert(S); 117 return; 118 } 119 120 inline void setExportAllNonStaticVars(bool flag) { 121 mExportAllNonStaticVars = flag; 122 } 123 inline void setExportAllNonStaticFuncs(bool flag) { 124 mExportAllNonStaticFuncs = flag; 125 } 126 inline void setReflectJavaPackageName(const std::string &S) { 127 mReflectJavaPackageName = S; 128 return; 129 } 130 131 void processExport(); 132 inline void newExportable(RSExportable *E) { 133 if (E != NULL) 134 mExportables.push_back(E); 135 } 136 137 typedef ExportVarList::const_iterator const_export_var_iterator; 138 const_export_var_iterator export_vars_begin() const { 139 return mExportVars.begin(); 140 } 141 const_export_var_iterator export_vars_end() const { 142 return mExportVars.end(); 143 } 144 inline bool hasExportVar() const { 145 return !mExportVars.empty(); 146 } 147 148 typedef ExportFuncList::const_iterator const_export_func_iterator; 149 const_export_func_iterator export_funcs_begin() const { 150 return mExportFuncs.begin(); 151 } 152 const_export_func_iterator export_funcs_end() const { 153 return mExportFuncs.end(); 154 } 155 inline bool hasExportFunc() const { return !mExportFuncs.empty(); } 156 157 typedef ExportTypeMap::iterator export_type_iterator; 158 typedef ExportTypeMap::const_iterator const_export_type_iterator; 159 export_type_iterator export_types_begin() { return mExportTypes.begin(); } 160 export_type_iterator export_types_end() { return mExportTypes.end(); } 161 const_export_type_iterator export_types_begin() const { 162 return mExportTypes.begin(); 163 } 164 const_export_type_iterator export_types_end() const { 165 return mExportTypes.end(); 166 } 167 inline bool hasExportType() const { return !mExportTypes.empty(); } 168 export_type_iterator findExportType(const llvm::StringRef &TypeName) { 169 return mExportTypes.find(TypeName); 170 } 171 const_export_type_iterator findExportType(const llvm::StringRef &TypeName) 172 const { 173 return mExportTypes.find(TypeName); 174 } 175 176 // Insert the specified Typename/Type pair into the map. If the key already 177 // exists in the map, return false and ignore the request, otherwise insert it 178 // and return true. 179 bool insertExportType(const llvm::StringRef &TypeName, RSExportType *Type); 180 181 bool reflectToJava(const std::string &OutputPathBase, 182 const std::string &OutputPackageName, 183 const std::string &InputFileName, 184 const std::string &OutputBCFileName, 185 std::string *RealPackageName); 186 187 ~RSContext(); 188}; 189 190} // namespace slang 191 192#endif // _SLANG_COMPILER_RS_CONTEXT_H 193