blob: 7e95d560210f8cd8e4ae1dd4b34f3ea2fe4faacb [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
zonr6315f762010-10-05 15:35:14 +080014#include "slang_rs_export_element.h"
15
Shih-wei Liao462aefd2010-06-04 15:32:04 -070016namespace llvm {
zonr6315f762010-10-05 15:35:14 +080017 class LLVMContext;
18 class TargetData;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070019} // namespace llvm
Shih-wei Liao462aefd2010-06-04 15:32:04 -070020
21namespace clang {
zonr6315f762010-10-05 15:35:14 +080022 class VarDecl;
23 class ASTContext;
24 class TargetInfo;
25 class FunctionDecl;
26 class SourceManager;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070027} // namespace clang
Shih-wei Liao462aefd2010-06-04 15:32:04 -070028
29namespace slang {
30
Shih-wei Liao462aefd2010-06-04 15:32:04 -070031class RSExportVar;
32class RSExportFunc;
33class RSExportType;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070034
35class RSContext {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070036 typedef llvm::StringSet<> NeedExportVarSet;
37 typedef llvm::StringSet<> NeedExportFuncSet;
38 typedef llvm::StringSet<> NeedExportTypeSet;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070039
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070040 public:
41 typedef std::list<RSExportVar*> ExportVarList;
42 typedef std::list<RSExportFunc*> ExportFuncList;
43 typedef llvm::StringMap<RSExportType*> ExportTypeMap;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070044
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070045 private:
46 clang::Preprocessor *mPP;
47 clang::ASTContext *mCtx;
48 const clang::TargetInfo *mTarget;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070049
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070050 llvm::TargetData *mTargetData;
51 llvm::LLVMContext &mLLVMContext;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070052
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070053 // 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 Liao462aefd2010-06-04 15:32:04 -070059
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070060 std::string *mLicenseNote;
61 std::string mReflectJavaPackageName;
62 std::string mReflectJavaPathName;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070063
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070064 bool processExportVar(const clang::VarDecl *VD);
65 bool processExportFunc(const clang::FunctionDecl *FD);
66 bool processExportType(const llvm::StringRef &Name);
Shih-wei Liao537446c2010-06-11 16:05:55 -070067
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070068 ExportVarList mExportVars;
69 ExportFuncList mExportFuncs;
70 ExportTypeMap mExportTypes;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070071
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070072 public:
73 RSContext(clang::Preprocessor *PP,
74 clang::ASTContext *Ctx,
75 const clang::TargetInfo *Target);
Shih-wei Liao462aefd2010-06-04 15:32:04 -070076
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070077 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 Liao462aefd2010-06-04 15:32:04 -070084
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070085 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 Liao462aefd2010-06-04 15:32:04 -070089
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070090 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 Hsiehd8a0d182010-07-07 19:22:33 +0800102
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700103 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 Liao462aefd2010-06-04 15:32:04 -0700120
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700121 void processExport();
Shih-wei Liao537446c2010-06-11 16:05:55 -0700122
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700123 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 Liao462aefd2010-06-04 15:32:04 -0700133
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700134 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 Liao462aefd2010-06-04 15:32:04 -0700142
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700143 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 Liao462aefd2010-06-04 15:32:04 -0700161
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700162 // 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 Liao462aefd2010-06-04 15:32:04 -0700166
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700167 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 Liao462aefd2010-06-04 15:32:04 -0700173
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700174 ~RSContext();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700175};
176
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700177} // namespace slang
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700178
zonr6315f762010-10-05 15:35:14 +0800179#endif // _SLANG_COMPILER_RS_CONTEXT_H