zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 1 | #ifndef _SLANG_COMPILER_RS_CONTEXT_H |
| 2 | #define _SLANG_COMPILER_RS_CONTEXT_H |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 3 | |
| 4 | #include <map> |
| 5 | #include <list> |
| 6 | #include <string> |
| 7 | #include <cstdio> |
| 8 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 9 | #include "llvm/ADT/StringSet.h" |
| 10 | #include "llvm/ADT/StringMap.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 11 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 12 | #include "clang/Lex/Preprocessor.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 13 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 14 | #include "slang_rs_export_element.h" |
| 15 | |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 16 | namespace llvm { |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 17 | class LLVMContext; |
| 18 | class TargetData; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 19 | } // namespace llvm |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 20 | |
| 21 | namespace clang { |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 22 | class VarDecl; |
| 23 | class ASTContext; |
| 24 | class TargetInfo; |
| 25 | class FunctionDecl; |
| 26 | class SourceManager; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 27 | } // namespace clang |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 28 | |
| 29 | namespace slang { |
| 30 | |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 31 | class RSExportVar; |
| 32 | class RSExportFunc; |
| 33 | class RSExportType; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 34 | |
| 35 | class RSContext { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 36 | typedef llvm::StringSet<> NeedExportVarSet; |
| 37 | typedef llvm::StringSet<> NeedExportFuncSet; |
| 38 | typedef llvm::StringSet<> NeedExportTypeSet; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 39 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 40 | public: |
| 41 | typedef std::list<RSExportVar*> ExportVarList; |
| 42 | typedef std::list<RSExportFunc*> ExportFuncList; |
| 43 | typedef llvm::StringMap<RSExportType*> ExportTypeMap; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 44 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 45 | private: |
| 46 | clang::Preprocessor *mPP; |
| 47 | clang::ASTContext *mCtx; |
| 48 | const clang::TargetInfo *mTarget; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 49 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 50 | llvm::TargetData *mTargetData; |
| 51 | llvm::LLVMContext &mLLVMContext; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 52 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 53 | // Record the variables/types/elements annotated in #pragma to be exported |
| 54 | NeedExportVarSet mNeedExportVars; |
| 55 | NeedExportFuncSet mNeedExportFuncs; |
| 56 | NeedExportTypeSet mNeedExportTypes; |
| 57 | bool mExportAllNonStaticVars; |
| 58 | bool mExportAllNonStaticFuncs; |
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 | std::string *mLicenseNote; |
| 61 | std::string mReflectJavaPackageName; |
| 62 | std::string mReflectJavaPathName; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 63 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 64 | bool processExportVar(const clang::VarDecl *VD); |
| 65 | bool processExportFunc(const clang::FunctionDecl *FD); |
| 66 | bool processExportType(const llvm::StringRef &Name); |
Shih-wei Liao | 537446c | 2010-06-11 16:05:55 -0700 | [diff] [blame] | 67 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 68 | ExportVarList mExportVars; |
| 69 | ExportFuncList mExportFuncs; |
| 70 | ExportTypeMap mExportTypes; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 71 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 72 | public: |
| 73 | RSContext(clang::Preprocessor *PP, |
| 74 | clang::ASTContext *Ctx, |
| 75 | const clang::TargetInfo *Target); |
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 | inline clang::Preprocessor *getPreprocessor() const { return mPP; } |
| 78 | inline clang::ASTContext *getASTContext() const { return mCtx; } |
| 79 | inline const llvm::TargetData *getTargetData() const { return mTargetData; } |
| 80 | inline llvm::LLVMContext &getLLVMContext() const { return mLLVMContext; } |
| 81 | inline const clang::SourceManager *getSourceManager() const { |
| 82 | return &mPP->getSourceManager(); |
| 83 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 84 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 85 | inline void setLicenseNote(const std::string &S) { |
| 86 | mLicenseNote = new std::string(S); |
| 87 | } |
| 88 | inline const std::string *getLicenseNote() const { return mLicenseNote; } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 89 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 90 | inline void addExportVar(const std::string &S) { |
| 91 | mNeedExportVars.insert(S); |
| 92 | return; |
| 93 | } |
| 94 | inline void addExportFunc(const std::string &S) { |
| 95 | mNeedExportFuncs.insert(S); |
| 96 | return; |
| 97 | } |
| 98 | inline void addExportType(const std::string &S) { |
| 99 | mNeedExportTypes.insert(S); |
| 100 | return; |
| 101 | } |
Victor Hsieh | d8a0d18 | 2010-07-07 19:22:33 +0800 | [diff] [blame] | 102 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 103 | inline void setExportAllNonStaticVars(bool flag) { |
| 104 | mExportAllNonStaticVars = flag; |
| 105 | } |
| 106 | inline void setExportAllNonStaticFuncs(bool flag) { |
| 107 | mExportAllNonStaticFuncs = flag; |
| 108 | } |
| 109 | inline void setReflectJavaPackageName(const std::string &S) { |
| 110 | mReflectJavaPackageName = S; |
| 111 | return; |
| 112 | } |
| 113 | inline void setReflectJavaPathName(const std::string &S) { |
| 114 | mReflectJavaPathName = S; |
| 115 | return; |
| 116 | } |
| 117 | inline std::string getReflectJavaPathName() const { |
| 118 | return mReflectJavaPathName; |
| 119 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 120 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 121 | void processExport(); |
Shih-wei Liao | 537446c | 2010-06-11 16:05:55 -0700 | [diff] [blame] | 122 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 123 | typedef ExportVarList::const_iterator const_export_var_iterator; |
| 124 | const_export_var_iterator export_vars_begin() const { |
| 125 | return mExportVars.begin(); |
| 126 | } |
| 127 | const_export_var_iterator export_vars_end() const { |
| 128 | return mExportVars.end(); |
| 129 | } |
| 130 | inline bool hasExportVar() const { |
| 131 | return !mExportVars.empty(); |
| 132 | } |
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 | typedef ExportFuncList::const_iterator const_export_func_iterator; |
| 135 | const_export_func_iterator export_funcs_begin() const { |
| 136 | return mExportFuncs.begin(); |
| 137 | } |
| 138 | const_export_func_iterator export_funcs_end() const { |
| 139 | return mExportFuncs.end(); |
| 140 | } |
| 141 | inline bool hasExportFunc() const { return !mExportFuncs.empty(); } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 142 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 143 | typedef ExportTypeMap::iterator export_type_iterator; |
| 144 | typedef ExportTypeMap::const_iterator const_export_type_iterator; |
| 145 | export_type_iterator export_types_begin() { return mExportTypes.begin(); } |
| 146 | export_type_iterator export_types_end() { return mExportTypes.end(); } |
| 147 | const_export_type_iterator export_types_begin() const { |
| 148 | return mExportTypes.begin(); |
| 149 | } |
| 150 | const_export_type_iterator export_types_end() const { |
| 151 | return mExportTypes.end(); |
| 152 | } |
| 153 | inline bool hasExportType() const { return !mExportTypes.empty(); } |
| 154 | export_type_iterator findExportType(const llvm::StringRef &TypeName) { |
| 155 | return mExportTypes.find(TypeName); |
| 156 | } |
| 157 | const_export_type_iterator findExportType(const llvm::StringRef &TypeName) |
| 158 | const { |
| 159 | return mExportTypes.find(TypeName); |
| 160 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 161 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 162 | // Insert the specified Typename/Type pair into the map. If the key already |
| 163 | // exists in the map, return false and ignore the request, otherwise insert it |
| 164 | // and return true. |
| 165 | bool insertExportType(const llvm::StringRef &TypeName, RSExportType *Type); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 166 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 167 | bool reflectToJava(const char *OutputPackageName, |
| 168 | const std::string &InputFileName, |
| 169 | const std::string &OutputBCFileName, |
| 170 | char realPackageName[], |
| 171 | int bSize); |
| 172 | bool reflectToJavaPath(const char *OutputPathName); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 173 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 174 | ~RSContext(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 175 | }; |
| 176 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 177 | } // namespace slang |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 178 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 179 | #endif // _SLANG_COMPILER_RS_CONTEXT_H |