slang_rs_context.h revision 92b344a51c6c4934e96882bd401e4b13d6d03db8
1#ifndef _SLANG_COMPILER_RS_CONTEXT_H
2#define _SLANG_COMPILER_RS_CONTEXT_H
3
4#include <map>
5#include <list>
6#include <string>
7#include <cstdio>
8
9#include "llvm/ADT/StringSet.h"
10#include "llvm/ADT/StringMap.h"
11
12#include "clang/Lex/Preprocessor.h"
13
14#include "slang_rs_export_element.h"
15
16namespace llvm {
17  class LLVMContext;
18  class TargetData;
19}   // namespace llvm
20
21namespace clang {
22  class VarDecl;
23  class ASTContext;
24  class TargetInfo;
25  class FunctionDecl;
26  class SourceManager;
27}   // namespace clang
28
29namespace slang {
30
31class RSExportVar;
32class RSExportFunc;
33class RSExportType;
34
35class RSContext {
36  typedef llvm::StringSet<> NeedExportVarSet;
37  typedef llvm::StringSet<> NeedExportFuncSet;
38  typedef llvm::StringSet<> NeedExportTypeSet;
39
40 public:
41  typedef std::list<RSExportVar*> ExportVarList;
42  typedef std::list<RSExportFunc*> ExportFuncList;
43  typedef llvm::StringMap<RSExportType*> ExportTypeMap;
44
45 private:
46  clang::Preprocessor *mPP;
47  clang::ASTContext *mCtx;
48  const clang::TargetInfo *mTarget;
49
50  llvm::TargetData *mTargetData;
51  llvm::LLVMContext &mLLVMContext;
52
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;
59
60  std::string *mLicenseNote;
61  std::string mReflectJavaPackageName;
62  std::string mReflectJavaPathName;
63
64  bool processExportVar(const clang::VarDecl *VD);
65  bool processExportFunc(const clang::FunctionDecl *FD);
66  bool processExportType(const llvm::StringRef &Name);
67
68  ExportVarList mExportVars;
69  ExportFuncList mExportFuncs;
70  ExportTypeMap mExportTypes;
71
72 public:
73  RSContext(clang::Preprocessor *PP,
74            clang::ASTContext *Ctx,
75            const clang::TargetInfo *Target);
76
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  }
84
85  inline void setLicenseNote(const std::string &S) {
86    mLicenseNote = new std::string(S);
87  }
88  inline const std::string *getLicenseNote() const { return mLicenseNote; }
89
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  }
102
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  }
120
121  void processExport();
122
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  }
133
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(); }
142
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  }
161
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);
166
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);
173
174  ~RSContext();
175};
176
177}   // namespace slang
178
179#endif  // _SLANG_COMPILER_RS_CONTEXT_H
180