Zonr Chang | c383a50 | 2010-10-12 01:52:08 +0800 | [diff] [blame] | 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 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 17 | #ifndef _SLANG_COMPILER_RS_CONTEXT_H |
| 18 | #define _SLANG_COMPILER_RS_CONTEXT_H |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 19 | |
| 20 | #include <map> |
| 21 | #include <list> |
| 22 | #include <string> |
| 23 | #include <cstdio> |
| 24 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 25 | #include "llvm/ADT/StringSet.h" |
| 26 | #include "llvm/ADT/StringMap.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 27 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 28 | #include "clang/Lex/Preprocessor.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 29 | |
| 30 | namespace llvm { |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 31 | class LLVMContext; |
| 32 | class TargetData; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 33 | } // namespace llvm |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 34 | |
| 35 | namespace clang { |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 36 | class VarDecl; |
| 37 | class ASTContext; |
| 38 | class TargetInfo; |
| 39 | class FunctionDecl; |
| 40 | class SourceManager; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 41 | } // namespace clang |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 42 | |
| 43 | namespace slang { |
Zonr Chang | a41ce1d | 2010-10-06 02:23:12 +0800 | [diff] [blame] | 44 | class RSExportable; |
| 45 | class RSExportVar; |
| 46 | class RSExportFunc; |
| 47 | class RSExportType; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 48 | |
| 49 | class RSContext { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 50 | typedef llvm::StringSet<> NeedExportVarSet; |
| 51 | typedef llvm::StringSet<> NeedExportFuncSet; |
| 52 | typedef llvm::StringSet<> NeedExportTypeSet; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 53 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 54 | public: |
Zonr Chang | a41ce1d | 2010-10-06 02:23:12 +0800 | [diff] [blame] | 55 | typedef std::list<RSExportable*> ExportableList; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 56 | typedef std::list<RSExportVar*> ExportVarList; |
| 57 | typedef std::list<RSExportFunc*> ExportFuncList; |
| 58 | typedef llvm::StringMap<RSExportType*> ExportTypeMap; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 59 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 60 | private: |
Stephen Hines | 9e5b503 | 2010-11-03 13:19:14 -0700 | [diff] [blame^] | 61 | clang::Preprocessor &mPP; |
| 62 | clang::ASTContext &mCtx; |
| 63 | const clang::TargetInfo &mTarget; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 64 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 65 | llvm::TargetData *mTargetData; |
| 66 | llvm::LLVMContext &mLLVMContext; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 67 | |
Zonr Chang | a41ce1d | 2010-10-06 02:23:12 +0800 | [diff] [blame] | 68 | ExportableList mExportables; |
| 69 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 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; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 76 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 77 | std::string *mLicenseNote; |
| 78 | std::string mReflectJavaPackageName; |
| 79 | std::string mReflectJavaPathName; |
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 | bool processExportVar(const clang::VarDecl *VD); |
| 82 | bool processExportFunc(const clang::FunctionDecl *FD); |
| 83 | bool processExportType(const llvm::StringRef &Name); |
Shih-wei Liao | 537446c | 2010-06-11 16:05:55 -0700 | [diff] [blame] | 84 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 85 | ExportVarList mExportVars; |
| 86 | ExportFuncList mExportFuncs; |
| 87 | ExportTypeMap mExportTypes; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 88 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 89 | public: |
Stephen Hines | 9e5b503 | 2010-11-03 13:19:14 -0700 | [diff] [blame^] | 90 | RSContext(clang::Preprocessor &PP, |
| 91 | clang::ASTContext &Ctx, |
| 92 | const clang::TargetInfo &Target); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 93 | |
Stephen Hines | 9e5b503 | 2010-11-03 13:19:14 -0700 | [diff] [blame^] | 94 | inline clang::Preprocessor &getPreprocessor() const { return mPP; } |
| 95 | inline clang::ASTContext &getASTContext() const { return mCtx; } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 96 | inline const llvm::TargetData *getTargetData() const { return mTargetData; } |
| 97 | inline llvm::LLVMContext &getLLVMContext() const { return mLLVMContext; } |
| 98 | inline const clang::SourceManager *getSourceManager() const { |
Stephen Hines | 9e5b503 | 2010-11-03 13:19:14 -0700 | [diff] [blame^] | 99 | return &mPP.getSourceManager(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 100 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 101 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 102 | inline void setLicenseNote(const std::string &S) { |
| 103 | mLicenseNote = new std::string(S); |
| 104 | } |
| 105 | inline const std::string *getLicenseNote() const { return mLicenseNote; } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 106 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 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 | } |
Victor Hsieh | d8a0d18 | 2010-07-07 19:22:33 +0800 | [diff] [blame] | 119 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 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 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 130 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 131 | void processExport(); |
Zonr Chang | a41ce1d | 2010-10-06 02:23:12 +0800 | [diff] [blame] | 132 | inline void newExportable(RSExportable *E) { |
| 133 | if (E != NULL) |
| 134 | mExportables.push_back(E); |
| 135 | } |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 136 | typedef ExportableList::iterator exportable_iterator; |
| 137 | exportable_iterator exportable_begin() { |
| 138 | return mExportables.begin(); |
| 139 | } |
| 140 | exportable_iterator exportable_end() { |
| 141 | return mExportables.end(); |
| 142 | } |
Shih-wei Liao | 537446c | 2010-06-11 16:05:55 -0700 | [diff] [blame] | 143 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 144 | typedef ExportVarList::const_iterator const_export_var_iterator; |
| 145 | const_export_var_iterator export_vars_begin() const { |
| 146 | return mExportVars.begin(); |
| 147 | } |
| 148 | const_export_var_iterator export_vars_end() const { |
| 149 | return mExportVars.end(); |
| 150 | } |
| 151 | inline bool hasExportVar() const { |
| 152 | return !mExportVars.empty(); |
| 153 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 154 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 155 | typedef ExportFuncList::const_iterator const_export_func_iterator; |
| 156 | const_export_func_iterator export_funcs_begin() const { |
| 157 | return mExportFuncs.begin(); |
| 158 | } |
| 159 | const_export_func_iterator export_funcs_end() const { |
| 160 | return mExportFuncs.end(); |
| 161 | } |
| 162 | inline bool hasExportFunc() const { return !mExportFuncs.empty(); } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 163 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 164 | typedef ExportTypeMap::iterator export_type_iterator; |
| 165 | typedef ExportTypeMap::const_iterator const_export_type_iterator; |
| 166 | export_type_iterator export_types_begin() { return mExportTypes.begin(); } |
| 167 | export_type_iterator export_types_end() { return mExportTypes.end(); } |
| 168 | const_export_type_iterator export_types_begin() const { |
| 169 | return mExportTypes.begin(); |
| 170 | } |
| 171 | const_export_type_iterator export_types_end() const { |
| 172 | return mExportTypes.end(); |
| 173 | } |
| 174 | inline bool hasExportType() const { return !mExportTypes.empty(); } |
| 175 | export_type_iterator findExportType(const llvm::StringRef &TypeName) { |
| 176 | return mExportTypes.find(TypeName); |
| 177 | } |
| 178 | const_export_type_iterator findExportType(const llvm::StringRef &TypeName) |
| 179 | const { |
| 180 | return mExportTypes.find(TypeName); |
| 181 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 182 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 183 | // Insert the specified Typename/Type pair into the map. If the key already |
| 184 | // exists in the map, return false and ignore the request, otherwise insert it |
| 185 | // and return true. |
| 186 | bool insertExportType(const llvm::StringRef &TypeName, RSExportType *Type); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 187 | |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 188 | bool reflectToJava(const std::string &OutputPathBase, |
| 189 | const std::string &OutputPackageName, |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 190 | const std::string &InputFileName, |
| 191 | const std::string &OutputBCFileName, |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 192 | std::string *RealPackageName); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 193 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 194 | ~RSContext(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 195 | }; |
| 196 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 197 | } // namespace slang |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 198 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 199 | #endif // _SLANG_COMPILER_RS_CONTEXT_H |