slang_rs_context.h revision fc4f78b9c7941132fb048a83f0e4ba528c3b4fd0
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 34namespace llvm { 35 class LLVMContext; 36 class DataLayout; 37} // namespace llvm 38 39namespace clang { 40 class VarDecl; 41 class ASTContext; 42 class TargetInfo; 43 class FunctionDecl; 44 class SourceManager; 45} // namespace clang 46 47namespace slang { 48 class RSExportable; 49 class RSExportVar; 50 class RSExportFunc; 51 class RSExportForEach; 52 class RSExportType; 53 54class 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 PragmaList *mPragmas; 70 unsigned int mTargetAPI; 71 bool mVerbose; 72 73 llvm::DataLayout *mDataLayout; 74 llvm::LLVMContext &mLLVMContext; 75 76 ExportableList mExportables; 77 78 NeedExportTypeSet mNeedExportTypes; 79 80 std::string *mLicenseNote; 81 std::string mReflectJavaPackageName; 82 std::string mReflectJavaPathName; 83 84 std::string mRSPackageName; 85 86 int version; 87 88 llvm::OwningPtr<clang::MangleContext> mMangleCtx; 89 90 bool processExportVar(const clang::VarDecl *VD); 91 bool processExportFunc(const clang::FunctionDecl *FD); 92 bool processExportType(const llvm::StringRef &Name); 93 94 void cleanupForEach(); 95 96 ExportVarList mExportVars; 97 ExportFuncList mExportFuncs; 98 ExportForEachList mExportForEach; 99 ExportTypeMap mExportTypes; 100 101 public: 102 RSContext(clang::Preprocessor &PP, 103 clang::ASTContext &Ctx, 104 const clang::TargetInfo &Target, 105 PragmaList *Pragmas, 106 unsigned int TargetAPI, 107 bool Verbose); 108 109 inline clang::Preprocessor &getPreprocessor() const { return mPP; } 110 inline clang::ASTContext &getASTContext() const { return mCtx; } 111 inline clang::MangleContext &getMangleContext() const { 112 return *mMangleCtx; 113 } 114 inline const llvm::DataLayout *getDataLayout() const { return mDataLayout; } 115 inline llvm::LLVMContext &getLLVMContext() const { return mLLVMContext; } 116 inline const clang::SourceManager *getSourceManager() const { 117 return &mPP.getSourceManager(); 118 } 119 inline clang::DiagnosticsEngine *getDiagnostics() const { 120 return &mPP.getDiagnostics(); 121 } 122 inline unsigned int getTargetAPI() const { 123 return mTargetAPI; 124 } 125 126 inline bool getVerbose() const { 127 return mVerbose; 128 } 129 130 inline void setLicenseNote(const std::string &S) { 131 mLicenseNote = new std::string(S); 132 } 133 inline const std::string *getLicenseNote() const { return mLicenseNote; } 134 135 inline void addExportType(const std::string &S) { 136 mNeedExportTypes.insert(S); 137 } 138 139 inline void setReflectJavaPackageName(const std::string &S) { 140 mReflectJavaPackageName = S; 141 } 142 inline const std::string &getReflectJavaPackageName() const { 143 return mReflectJavaPackageName; 144 } 145 146 inline void setRSPackageName(const std::string &S) { 147 mRSPackageName = S; 148 } 149 150 inline const std::string &getRSPackageName() const { return mRSPackageName; } 151 152 bool processExport(); 153 inline void newExportable(RSExportable *E) { 154 if (E != NULL) 155 mExportables.push_back(E); 156 } 157 typedef ExportableList::iterator exportable_iterator; 158 exportable_iterator exportable_begin() { 159 return mExportables.begin(); 160 } 161 exportable_iterator exportable_end() { 162 return mExportables.end(); 163 } 164 165 typedef ExportVarList::const_iterator const_export_var_iterator; 166 const_export_var_iterator export_vars_begin() const { 167 return mExportVars.begin(); 168 } 169 const_export_var_iterator export_vars_end() const { 170 return mExportVars.end(); 171 } 172 inline bool hasExportVar() const { 173 return !mExportVars.empty(); 174 } 175 176 typedef ExportFuncList::const_iterator const_export_func_iterator; 177 const_export_func_iterator export_funcs_begin() const { 178 return mExportFuncs.begin(); 179 } 180 const_export_func_iterator export_funcs_end() const { 181 return mExportFuncs.end(); 182 } 183 inline bool hasExportFunc() const { return !mExportFuncs.empty(); } 184 185 typedef ExportForEachList::const_iterator const_export_foreach_iterator; 186 const_export_foreach_iterator export_foreach_begin() const { 187 return mExportForEach.begin(); 188 } 189 const_export_foreach_iterator export_foreach_end() const { 190 return mExportForEach.end(); 191 } 192 inline bool hasExportForEach() const { return !mExportForEach.empty(); } 193 194 typedef ExportTypeMap::iterator export_type_iterator; 195 typedef ExportTypeMap::const_iterator const_export_type_iterator; 196 export_type_iterator export_types_begin() { return mExportTypes.begin(); } 197 export_type_iterator export_types_end() { return mExportTypes.end(); } 198 const_export_type_iterator export_types_begin() const { 199 return mExportTypes.begin(); 200 } 201 const_export_type_iterator export_types_end() const { 202 return mExportTypes.end(); 203 } 204 inline bool hasExportType() const { return !mExportTypes.empty(); } 205 export_type_iterator findExportType(const llvm::StringRef &TypeName) { 206 return mExportTypes.find(TypeName); 207 } 208 const_export_type_iterator findExportType(const llvm::StringRef &TypeName) 209 const { 210 return mExportTypes.find(TypeName); 211 } 212 213 // Insert the specified Typename/Type pair into the map. If the key already 214 // exists in the map, return false and ignore the request, otherwise insert it 215 // and return true. 216 bool insertExportType(const llvm::StringRef &TypeName, RSExportType *Type); 217 218 int getVersion() const { return version; } 219 void setVersion(int v) { 220 version = v; 221 } 222 223 bool isCompatLib() const { 224 // If we are not targeting the actual Android Renderscript classes, 225 // we should reflect code that works with the compatibility library. 226 return (mRSPackageName.compare("android.renderscript") != 0); 227 } 228 229 void addPragma(const std::string &T, const std::string &V) { 230 mPragmas->push_back(make_pair(T, V)); 231 } 232 233 // Report an error or a warning to the user. 234 template <unsigned N> 235 clang::DiagnosticBuilder Report(clang::DiagnosticsEngine::Level Level, 236 const char (&Message)[N]) { 237 clang::DiagnosticsEngine *DiagEngine = getDiagnostics(); 238 return DiagEngine->Report(DiagEngine->getCustomDiagID(Level, Message)); 239} 240 241 template <unsigned N> 242 clang::DiagnosticBuilder Report(clang::DiagnosticsEngine::Level Level, 243 const clang::SourceLocation Loc, 244 const char (&Message)[N]) { 245 clang::DiagnosticsEngine *DiagEngine = getDiagnostics(); 246 const clang::SourceManager *SM = getSourceManager(); 247 return DiagEngine->Report(clang::FullSourceLoc(Loc, *SM), 248 DiagEngine->getCustomDiagID(Level, Message)); 249} 250 251 // Utility functions to report errors and warnings to make the calling code 252 // easier to read. 253 template <unsigned N> 254 clang::DiagnosticBuilder ReportError(const char (&Message)[N]) { 255 return Report<N>(clang::DiagnosticsEngine::Error, Message); 256 } 257 258 template <unsigned N> 259 clang::DiagnosticBuilder ReportError(const clang::SourceLocation Loc, 260 const char (&Message)[N]) { 261 return Report<N>(clang::DiagnosticsEngine::Error, Loc, Message); 262 } 263 264 template <unsigned N> 265 clang::DiagnosticBuilder ReportWarning(const char (&Message)[N]) { 266 return Report<N>(clang::DiagnosticsEngine::Warning, Message); 267 } 268 269 template <unsigned N> 270 clang::DiagnosticBuilder ReportWarning(const clang::SourceLocation Loc, 271 const char (&Message)[N]) { 272 return Report<N>(clang::DiagnosticsEngine::Warning, Loc, Message); 273 } 274 275 ~RSContext(); 276}; 277 278} // namespace slang 279 280#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_CONTEXT_H_ NOLINT 281