Zonr Chang | c383a50 | 2010-10-12 01:52:08 +0800 | [diff] [blame] | 1 | /* |
Stephen Hines | 0a813a3 | 2012-08-03 16:52:40 -0700 | [diff] [blame] | 2 | * Copyright 2010-2012, The Android Open Source Project |
Zonr Chang | c383a50 | 2010-10-12 01:52:08 +0800 | [diff] [blame] | 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 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 17 | #ifndef _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_CONTEXT_H_ // NOLINT |
| 18 | #define _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_CONTEXT_H_ |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 19 | |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 20 | #include <cstdio> |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 21 | #include <list> |
| 22 | #include <map> |
| 23 | #include <string> |
| 24 | |
| 25 | #include "clang/Lex/Preprocessor.h" |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 26 | #include "clang/AST/Mangle.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 27 | |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 28 | #include "llvm/ADT/OwningPtr.h" |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 29 | #include "llvm/ADT/StringSet.h" |
| 30 | #include "llvm/ADT/StringMap.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 31 | |
Stephen Hines | 3fd0a94 | 2011-01-18 12:27:39 -0800 | [diff] [blame] | 32 | #include "slang_pragma_recorder.h" |
| 33 | |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 34 | namespace llvm { |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 35 | class LLVMContext; |
| 36 | class TargetData; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 37 | } // namespace llvm |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 38 | |
| 39 | namespace clang { |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 40 | class VarDecl; |
| 41 | class ASTContext; |
| 42 | class TargetInfo; |
| 43 | class FunctionDecl; |
| 44 | class SourceManager; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 45 | } // namespace clang |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 46 | |
| 47 | namespace slang { |
Zonr Chang | a41ce1d | 2010-10-06 02:23:12 +0800 | [diff] [blame] | 48 | class RSExportable; |
| 49 | class RSExportVar; |
| 50 | class RSExportFunc; |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 51 | class RSExportForEach; |
Zonr Chang | a41ce1d | 2010-10-06 02:23:12 +0800 | [diff] [blame] | 52 | class RSExportType; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 53 | |
| 54 | class RSContext { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 55 | typedef llvm::StringSet<> NeedExportVarSet; |
| 56 | typedef llvm::StringSet<> NeedExportFuncSet; |
| 57 | typedef llvm::StringSet<> NeedExportTypeSet; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 58 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 59 | public: |
Zonr Chang | a41ce1d | 2010-10-06 02:23:12 +0800 | [diff] [blame] | 60 | typedef std::list<RSExportable*> ExportableList; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 61 | typedef std::list<RSExportVar*> ExportVarList; |
| 62 | typedef std::list<RSExportFunc*> ExportFuncList; |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 63 | typedef std::list<RSExportForEach*> ExportForEachList; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 64 | typedef llvm::StringMap<RSExportType*> ExportTypeMap; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 65 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 66 | private: |
Stephen Hines | 9e5b503 | 2010-11-03 13:19:14 -0700 | [diff] [blame] | 67 | clang::Preprocessor &mPP; |
| 68 | clang::ASTContext &mCtx; |
| 69 | const clang::TargetInfo &mTarget; |
Stephen Hines | 3fd0a94 | 2011-01-18 12:27:39 -0800 | [diff] [blame] | 70 | PragmaList *mPragmas; |
Stephen Hines | 4a4bf92 | 2011-08-18 17:20:33 -0700 | [diff] [blame] | 71 | unsigned int mTargetAPI; |
Stephen Hines | 4cc67fc | 2011-01-31 16:48:57 -0800 | [diff] [blame] | 72 | std::vector<std::string> *mGeneratedFileNames; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 73 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 74 | llvm::TargetData *mTargetData; |
| 75 | llvm::LLVMContext &mLLVMContext; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 76 | |
Zonr Chang | a41ce1d | 2010-10-06 02:23:12 +0800 | [diff] [blame] | 77 | ExportableList mExportables; |
| 78 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 79 | NeedExportTypeSet mNeedExportTypes; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 80 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 81 | std::string *mLicenseNote; |
| 82 | std::string mReflectJavaPackageName; |
| 83 | std::string mReflectJavaPathName; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 84 | |
Stephen Hines | 0a813a3 | 2012-08-03 16:52:40 -0700 | [diff] [blame] | 85 | std::string mRSPackageName; |
| 86 | |
Stephen Hines | 96ab06c | 2011-01-05 15:29:26 -0800 | [diff] [blame] | 87 | int version; |
Stephen Hines | 82754d8 | 2013-01-18 19:46:06 -0800 | [diff] [blame^] | 88 | |
| 89 | bool mIsCompatLib; |
| 90 | |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 91 | llvm::OwningPtr<clang::MangleContext> mMangleCtx; |
Stephen Hines | 96ab06c | 2011-01-05 15:29:26 -0800 | [diff] [blame] | 92 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 93 | bool processExportVar(const clang::VarDecl *VD); |
| 94 | bool processExportFunc(const clang::FunctionDecl *FD); |
| 95 | bool processExportType(const llvm::StringRef &Name); |
Shih-wei Liao | 537446c | 2010-06-11 16:05:55 -0700 | [diff] [blame] | 96 | |
Stephen Hines | c17e198 | 2012-02-22 12:30:45 -0800 | [diff] [blame] | 97 | void cleanupForEach(); |
| 98 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 99 | ExportVarList mExportVars; |
| 100 | ExportFuncList mExportFuncs; |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 101 | ExportForEachList mExportForEach; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 102 | ExportTypeMap mExportTypes; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 103 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 104 | public: |
Stephen Hines | 9e5b503 | 2010-11-03 13:19:14 -0700 | [diff] [blame] | 105 | RSContext(clang::Preprocessor &PP, |
| 106 | clang::ASTContext &Ctx, |
Stephen Hines | 3fd0a94 | 2011-01-18 12:27:39 -0800 | [diff] [blame] | 107 | const clang::TargetInfo &Target, |
Stephen Hines | 4cc67fc | 2011-01-31 16:48:57 -0800 | [diff] [blame] | 108 | PragmaList *Pragmas, |
Stephen Hines | 4a4bf92 | 2011-08-18 17:20:33 -0700 | [diff] [blame] | 109 | unsigned int TargetAPI, |
Stephen Hines | 4cc67fc | 2011-01-31 16:48:57 -0800 | [diff] [blame] | 110 | std::vector<std::string> *GeneratedFileNames); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 111 | |
Stephen Hines | 9e5b503 | 2010-11-03 13:19:14 -0700 | [diff] [blame] | 112 | inline clang::Preprocessor &getPreprocessor() const { return mPP; } |
| 113 | inline clang::ASTContext &getASTContext() const { return mCtx; } |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 114 | inline clang::MangleContext &getMangleContext() const { |
| 115 | return *mMangleCtx; |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 116 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 117 | inline const llvm::TargetData *getTargetData() const { return mTargetData; } |
| 118 | inline llvm::LLVMContext &getLLVMContext() const { return mLLVMContext; } |
| 119 | inline const clang::SourceManager *getSourceManager() const { |
Stephen Hines | 9e5b503 | 2010-11-03 13:19:14 -0700 | [diff] [blame] | 120 | return &mPP.getSourceManager(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 121 | } |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 122 | inline clang::DiagnosticsEngine *getDiagnostics() const { |
Stephen Hines | 2ef9bc0 | 2010-12-13 18:33:23 -0800 | [diff] [blame] | 123 | return &mPP.getDiagnostics(); |
| 124 | } |
Stephen Hines | 4a4bf92 | 2011-08-18 17:20:33 -0700 | [diff] [blame] | 125 | inline unsigned int getTargetAPI() const { |
| 126 | return mTargetAPI; |
| 127 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 128 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 129 | inline void setLicenseNote(const std::string &S) { |
| 130 | mLicenseNote = new std::string(S); |
| 131 | } |
| 132 | inline const std::string *getLicenseNote() const { return mLicenseNote; } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 133 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 134 | inline void addExportType(const std::string &S) { |
| 135 | mNeedExportTypes.insert(S); |
| 136 | return; |
| 137 | } |
Victor Hsieh | d8a0d18 | 2010-07-07 19:22:33 +0800 | [diff] [blame] | 138 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 139 | inline void setReflectJavaPackageName(const std::string &S) { |
| 140 | mReflectJavaPackageName = S; |
| 141 | return; |
| 142 | } |
Stephen Hines | 4cc67fc | 2011-01-31 16:48:57 -0800 | [diff] [blame] | 143 | inline const std::string &getReflectJavaPackageName() { |
| 144 | return mReflectJavaPackageName; |
| 145 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 146 | |
Stephen Hines | 0a813a3 | 2012-08-03 16:52:40 -0700 | [diff] [blame] | 147 | inline void setRSPackageName(const std::string &S) { |
| 148 | mRSPackageName = S; |
| 149 | return; |
| 150 | } |
| 151 | inline const std::string &getRSPackageName() { |
| 152 | return mRSPackageName; |
| 153 | } |
| 154 | |
Stephen Hines | c808a99 | 2010-11-29 17:20:42 -0800 | [diff] [blame] | 155 | bool processExport(); |
Zonr Chang | a41ce1d | 2010-10-06 02:23:12 +0800 | [diff] [blame] | 156 | inline void newExportable(RSExportable *E) { |
| 157 | if (E != NULL) |
| 158 | mExportables.push_back(E); |
| 159 | } |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 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 | } |
Shih-wei Liao | 537446c | 2010-06-11 16:05:55 -0700 | [diff] [blame] | 167 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 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 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 178 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 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(); } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 187 | |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 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 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 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 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 215 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 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); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 220 | |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 221 | bool reflectToJava(const std::string &OutputPathBase, |
| 222 | const std::string &OutputPackageName, |
Stephen Hines | 0a813a3 | 2012-08-03 16:52:40 -0700 | [diff] [blame] | 223 | const std::string &RSPackageName, |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 224 | const std::string &InputFileName, |
| 225 | const std::string &OutputBCFileName, |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 226 | std::string *RealPackageName); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 227 | |
Stephen Hines | 96ab06c | 2011-01-05 15:29:26 -0800 | [diff] [blame] | 228 | int getVersion() const { return version; } |
| 229 | void setVersion(int v) { |
| 230 | version = v; |
| 231 | return; |
| 232 | } |
| 233 | |
Stephen Hines | 82754d8 | 2013-01-18 19:46:06 -0800 | [diff] [blame^] | 234 | bool isCompatLib() const { return mIsCompatLib; } |
| 235 | |
Stephen Hines | 3fd0a94 | 2011-01-18 12:27:39 -0800 | [diff] [blame] | 236 | void addPragma(const std::string &T, const std::string &V) { |
| 237 | mPragmas->push_back(make_pair(T, V)); |
| 238 | } |
| 239 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 240 | ~RSContext(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 241 | }; |
| 242 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 243 | } // namespace slang |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 244 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 245 | #endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_CONTEXT_H_ NOLINT |