slang_rs_context.h revision a41ce1d98094da84643995d40d71c529905123fc
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
14namespace llvm {
15  class LLVMContext;
16  class TargetData;
17}   // namespace llvm
18
19namespace clang {
20  class VarDecl;
21  class ASTContext;
22  class TargetInfo;
23  class FunctionDecl;
24  class SourceManager;
25}   // namespace clang
26
27namespace slang {
28  class RSExportable;
29  class RSExportVar;
30  class RSExportFunc;
31  class RSExportType;
32
33class RSContext {
34  typedef llvm::StringSet<> NeedExportVarSet;
35  typedef llvm::StringSet<> NeedExportFuncSet;
36  typedef llvm::StringSet<> NeedExportTypeSet;
37
38 public:
39  typedef std::list<RSExportable*> ExportableList;
40  typedef std::list<RSExportVar*> ExportVarList;
41  typedef std::list<RSExportFunc*> ExportFuncList;
42  typedef llvm::StringMap<RSExportType*> ExportTypeMap;
43
44 private:
45  clang::Preprocessor *mPP;
46  clang::ASTContext *mCtx;
47  const clang::TargetInfo *mTarget;
48
49  llvm::TargetData *mTargetData;
50  llvm::LLVMContext &mLLVMContext;
51
52  ExportableList mExportables;
53
54  // 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;
60
61  std::string *mLicenseNote;
62  std::string mReflectJavaPackageName;
63  std::string mReflectJavaPathName;
64
65  bool processExportVar(const clang::VarDecl *VD);
66  bool processExportFunc(const clang::FunctionDecl *FD);
67  bool processExportType(const llvm::StringRef &Name);
68
69  ExportVarList mExportVars;
70  ExportFuncList mExportFuncs;
71  ExportTypeMap mExportTypes;
72
73 public:
74  RSContext(clang::Preprocessor *PP,
75            clang::ASTContext *Ctx,
76            const clang::TargetInfo *Target);
77
78  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  }
85
86  inline void setLicenseNote(const std::string &S) {
87    mLicenseNote = new std::string(S);
88  }
89  inline const std::string *getLicenseNote() const { return mLicenseNote; }
90
91  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  }
103
104  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  }
114  inline void setReflectJavaPathName(const std::string &S) {
115    mReflectJavaPathName = S;
116    return;
117  }
118  inline std::string getReflectJavaPathName() const {
119    return mReflectJavaPathName;
120  }
121
122  void processExport();
123  inline void newExportable(RSExportable *E) {
124    if (E != NULL)
125      mExportables.push_back(E);
126  }
127
128  typedef ExportVarList::const_iterator const_export_var_iterator;
129  const_export_var_iterator export_vars_begin() const {
130    return mExportVars.begin();
131  }
132  const_export_var_iterator export_vars_end() const {
133    return mExportVars.end();
134  }
135  inline bool hasExportVar() const {
136    return !mExportVars.empty();
137  }
138
139  typedef ExportFuncList::const_iterator const_export_func_iterator;
140  const_export_func_iterator export_funcs_begin() const {
141    return mExportFuncs.begin();
142  }
143  const_export_func_iterator export_funcs_end() const {
144    return mExportFuncs.end();
145  }
146  inline bool hasExportFunc() const { return !mExportFuncs.empty(); }
147
148  typedef ExportTypeMap::iterator export_type_iterator;
149  typedef ExportTypeMap::const_iterator const_export_type_iterator;
150  export_type_iterator export_types_begin() { return mExportTypes.begin(); }
151  export_type_iterator export_types_end() { return mExportTypes.end(); }
152  const_export_type_iterator export_types_begin() const {
153    return mExportTypes.begin();
154  }
155  const_export_type_iterator export_types_end() const {
156    return mExportTypes.end();
157  }
158  inline bool hasExportType() const { return !mExportTypes.empty(); }
159  export_type_iterator findExportType(const llvm::StringRef &TypeName) {
160    return mExportTypes.find(TypeName);
161  }
162  const_export_type_iterator findExportType(const llvm::StringRef &TypeName)
163      const {
164    return mExportTypes.find(TypeName);
165  }
166
167  // Insert the specified Typename/Type pair into the map. If the key already
168  // exists in the map, return false and ignore the request, otherwise insert it
169  // and return true.
170  bool insertExportType(const llvm::StringRef &TypeName, RSExportType *Type);
171
172  bool reflectToJava(const char *OutputPackageName,
173                     const std::string &InputFileName,
174                     const std::string &OutputBCFileName,
175                     char realPackageName[],
176                     int bSize);
177  bool reflectToJavaPath(const char *OutputPathName);
178
179  ~RSContext();
180};
181
182}   // namespace slang
183
184#endif  // _SLANG_COMPILER_RS_CONTEXT_H
185