blob: e54ed1e507c742b41513f15d46d7d5b1360df1fd [file] [log] [blame]
zonr6315f762010-10-05 15:35:14 +08001#ifndef _SLANG_COMPILER_RS_CONTEXT_H
2#define _SLANG_COMPILER_RS_CONTEXT_H
Shih-wei Liao462aefd2010-06-04 15:32:04 -07003
4#include <map>
5#include <list>
6#include <string>
7#include <cstdio>
8
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07009#include "llvm/ADT/StringSet.h"
10#include "llvm/ADT/StringMap.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070011
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070012#include "clang/Lex/Preprocessor.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070013
14namespace llvm {
zonr6315f762010-10-05 15:35:14 +080015 class LLVMContext;
16 class TargetData;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070017} // namespace llvm
Shih-wei Liao462aefd2010-06-04 15:32:04 -070018
19namespace clang {
zonr6315f762010-10-05 15:35:14 +080020 class VarDecl;
21 class ASTContext;
22 class TargetInfo;
23 class FunctionDecl;
24 class SourceManager;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070025} // namespace clang
Shih-wei Liao462aefd2010-06-04 15:32:04 -070026
27namespace slang {
Zonr Changa41ce1d2010-10-06 02:23:12 +080028 class RSExportable;
29 class RSExportVar;
30 class RSExportFunc;
31 class RSExportType;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070032
33class RSContext {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070034 typedef llvm::StringSet<> NeedExportVarSet;
35 typedef llvm::StringSet<> NeedExportFuncSet;
36 typedef llvm::StringSet<> NeedExportTypeSet;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070037
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070038 public:
Zonr Changa41ce1d2010-10-06 02:23:12 +080039 typedef std::list<RSExportable*> ExportableList;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070040 typedef std::list<RSExportVar*> ExportVarList;
41 typedef std::list<RSExportFunc*> ExportFuncList;
42 typedef llvm::StringMap<RSExportType*> ExportTypeMap;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070043
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070044 private:
45 clang::Preprocessor *mPP;
46 clang::ASTContext *mCtx;
47 const clang::TargetInfo *mTarget;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070048
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070049 llvm::TargetData *mTargetData;
50 llvm::LLVMContext &mLLVMContext;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070051
Zonr Changa41ce1d2010-10-06 02:23:12 +080052 ExportableList mExportables;
53
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070054 // Record the variables/types/elements annotated in #pragma to be exported
55 NeedExportVarSet mNeedExportVars;
56 NeedExportFuncSet mNeedExportFuncs;
57 NeedExportTypeSet mNeedExportTypes;
58 bool mExportAllNonStaticVars;
59 bool mExportAllNonStaticFuncs;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070060
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070061 std::string *mLicenseNote;
62 std::string mReflectJavaPackageName;
63 std::string mReflectJavaPathName;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070064
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070065 bool processExportVar(const clang::VarDecl *VD);
66 bool processExportFunc(const clang::FunctionDecl *FD);
67 bool processExportType(const llvm::StringRef &Name);
Shih-wei Liao537446c2010-06-11 16:05:55 -070068
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070069 ExportVarList mExportVars;
70 ExportFuncList mExportFuncs;
71 ExportTypeMap mExportTypes;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070072
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070073 public:
74 RSContext(clang::Preprocessor *PP,
75 clang::ASTContext *Ctx,
76 const clang::TargetInfo *Target);
Shih-wei Liao462aefd2010-06-04 15:32:04 -070077
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070078 inline clang::Preprocessor *getPreprocessor() const { return mPP; }
79 inline clang::ASTContext *getASTContext() const { return mCtx; }
80 inline const llvm::TargetData *getTargetData() const { return mTargetData; }
81 inline llvm::LLVMContext &getLLVMContext() const { return mLLVMContext; }
82 inline const clang::SourceManager *getSourceManager() const {
83 return &mPP->getSourceManager();
84 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -070085
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070086 inline void setLicenseNote(const std::string &S) {
87 mLicenseNote = new std::string(S);
88 }
89 inline const std::string *getLicenseNote() const { return mLicenseNote; }
Shih-wei Liao462aefd2010-06-04 15:32:04 -070090
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070091 inline void addExportVar(const std::string &S) {
92 mNeedExportVars.insert(S);
93 return;
94 }
95 inline void addExportFunc(const std::string &S) {
96 mNeedExportFuncs.insert(S);
97 return;
98 }
99 inline void addExportType(const std::string &S) {
100 mNeedExportTypes.insert(S);
101 return;
102 }
Victor Hsiehd8a0d182010-07-07 19:22:33 +0800103
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700104 inline void setExportAllNonStaticVars(bool flag) {
105 mExportAllNonStaticVars = flag;
106 }
107 inline void setExportAllNonStaticFuncs(bool flag) {
108 mExportAllNonStaticFuncs = flag;
109 }
110 inline void setReflectJavaPackageName(const std::string &S) {
111 mReflectJavaPackageName = S;
112 return;
113 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700114
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700115 void processExport();
Zonr Changa41ce1d2010-10-06 02:23:12 +0800116 inline void newExportable(RSExportable *E) {
117 if (E != NULL)
118 mExportables.push_back(E);
119 }
Shih-wei Liao537446c2010-06-11 16:05:55 -0700120
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700121 typedef ExportVarList::const_iterator const_export_var_iterator;
122 const_export_var_iterator export_vars_begin() const {
123 return mExportVars.begin();
124 }
125 const_export_var_iterator export_vars_end() const {
126 return mExportVars.end();
127 }
128 inline bool hasExportVar() const {
129 return !mExportVars.empty();
130 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700131
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700132 typedef ExportFuncList::const_iterator const_export_func_iterator;
133 const_export_func_iterator export_funcs_begin() const {
134 return mExportFuncs.begin();
135 }
136 const_export_func_iterator export_funcs_end() const {
137 return mExportFuncs.end();
138 }
139 inline bool hasExportFunc() const { return !mExportFuncs.empty(); }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700140
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700141 typedef ExportTypeMap::iterator export_type_iterator;
142 typedef ExportTypeMap::const_iterator const_export_type_iterator;
143 export_type_iterator export_types_begin() { return mExportTypes.begin(); }
144 export_type_iterator export_types_end() { return mExportTypes.end(); }
145 const_export_type_iterator export_types_begin() const {
146 return mExportTypes.begin();
147 }
148 const_export_type_iterator export_types_end() const {
149 return mExportTypes.end();
150 }
151 inline bool hasExportType() const { return !mExportTypes.empty(); }
152 export_type_iterator findExportType(const llvm::StringRef &TypeName) {
153 return mExportTypes.find(TypeName);
154 }
155 const_export_type_iterator findExportType(const llvm::StringRef &TypeName)
156 const {
157 return mExportTypes.find(TypeName);
158 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700159
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700160 // Insert the specified Typename/Type pair into the map. If the key already
161 // exists in the map, return false and ignore the request, otherwise insert it
162 // and return true.
163 bool insertExportType(const llvm::StringRef &TypeName, RSExportType *Type);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700164
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700165 bool reflectToJava(const std::string &OutputPathBase,
166 const std::string &OutputPackageName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700167 const std::string &InputFileName,
168 const std::string &OutputBCFileName,
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700169 std::string *RealPackageName);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700170
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700171 ~RSContext();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700172};
173
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700174} // namespace slang
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700175
zonr6315f762010-10-05 15:35:14 +0800176#endif // _SLANG_COMPILER_RS_CONTEXT_H