slang_rs_context.h revision 12fc283f4108fd6f7f0164c121ff2f6fb5044225
1/*
2 * Copyright 2010-2012, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_CONTEXT_H_  // NOLINT
18#define _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_CONTEXT_H_
19
20#include <cstdio>
21#include <list>
22#include <map>
23#include <string>
24
25#include "clang/Lex/Preprocessor.h"
26#include "clang/AST/Mangle.h"
27
28#include "llvm/ADT/OwningPtr.h"
29#include "llvm/ADT/StringSet.h"
30#include "llvm/ADT/StringMap.h"
31
32#include "slang_pragma_recorder.h"
33
34namespace llvm {
35  class LLVMContext;
36  class DataLayout;
37}   // namespace llvm
38
39namespace clang {
40  class VarDecl;
41  class ASTContext;
42  class TargetInfo;
43  class FunctionDecl;
44  class SourceManager;
45}   // namespace clang
46
47namespace slang {
48  class RSExportable;
49  class RSExportVar;
50  class RSExportFunc;
51  class RSExportForEach;
52  class RSExportType;
53
54class RSContext {
55  typedef llvm::StringSet<> NeedExportVarSet;
56  typedef llvm::StringSet<> NeedExportFuncSet;
57  typedef llvm::StringSet<> NeedExportTypeSet;
58
59 public:
60  typedef std::list<RSExportable*> ExportableList;
61  typedef std::list<RSExportVar*> ExportVarList;
62  typedef std::list<RSExportFunc*> ExportFuncList;
63  typedef std::list<RSExportForEach*> ExportForEachList;
64  typedef llvm::StringMap<RSExportType*> ExportTypeMap;
65
66 private:
67  clang::Preprocessor &mPP;
68  clang::ASTContext &mCtx;
69  PragmaList *mPragmas;
70  unsigned int mTargetAPI;
71
72  llvm::DataLayout *mDataLayout;
73  llvm::LLVMContext &mLLVMContext;
74
75  ExportableList mExportables;
76
77  NeedExportTypeSet mNeedExportTypes;
78
79  std::string *mLicenseNote;
80  std::string mReflectJavaPackageName;
81  std::string mReflectJavaPathName;
82
83  std::string mRSPackageName;
84
85  int version;
86
87  llvm::OwningPtr<clang::MangleContext> mMangleCtx;
88
89  bool processExportVar(const clang::VarDecl *VD);
90  bool processExportFunc(const clang::FunctionDecl *FD);
91  bool processExportType(const llvm::StringRef &Name);
92
93  void cleanupForEach();
94
95  ExportVarList mExportVars;
96  ExportFuncList mExportFuncs;
97  ExportForEachList mExportForEach;
98  ExportTypeMap mExportTypes;
99
100 public:
101  RSContext(clang::Preprocessor &PP,
102            clang::ASTContext &Ctx,
103            const clang::TargetInfo &Target,
104            PragmaList *Pragmas,
105            unsigned int TargetAPI);
106
107  inline clang::Preprocessor &getPreprocessor() const { return mPP; }
108  inline clang::ASTContext &getASTContext() const { return mCtx; }
109  inline clang::MangleContext &getMangleContext() const {
110    return *mMangleCtx;
111  }
112  inline const llvm::DataLayout *getDataLayout() const { return mDataLayout; }
113  inline llvm::LLVMContext &getLLVMContext() const { return mLLVMContext; }
114  inline const clang::SourceManager *getSourceManager() const {
115    return &mPP.getSourceManager();
116  }
117  inline clang::DiagnosticsEngine *getDiagnostics() const {
118    return &mPP.getDiagnostics();
119  }
120  inline unsigned int getTargetAPI() const {
121    return mTargetAPI;
122  }
123
124  inline void setLicenseNote(const std::string &S) {
125    mLicenseNote = new std::string(S);
126  }
127  inline const std::string *getLicenseNote() const { return mLicenseNote; }
128
129  inline void addExportType(const std::string &S) {
130    mNeedExportTypes.insert(S);
131  }
132
133  inline void setReflectJavaPackageName(const std::string &S) {
134    mReflectJavaPackageName = S;
135  }
136  inline const std::string &getReflectJavaPackageName() const {
137    return mReflectJavaPackageName;
138  }
139
140  inline void setRSPackageName(const std::string &S) {
141    mRSPackageName = S;
142  }
143
144  inline const std::string &getRSPackageName() const { return mRSPackageName; }
145
146  bool processExport();
147  inline void newExportable(RSExportable *E) {
148    if (E != NULL)
149      mExportables.push_back(E);
150  }
151  typedef ExportableList::iterator exportable_iterator;
152  exportable_iterator exportable_begin() {
153    return mExportables.begin();
154  }
155  exportable_iterator exportable_end() {
156    return mExportables.end();
157  }
158
159  typedef ExportVarList::const_iterator const_export_var_iterator;
160  const_export_var_iterator export_vars_begin() const {
161    return mExportVars.begin();
162  }
163  const_export_var_iterator export_vars_end() const {
164    return mExportVars.end();
165  }
166  inline bool hasExportVar() const {
167    return !mExportVars.empty();
168  }
169
170  typedef ExportFuncList::const_iterator const_export_func_iterator;
171  const_export_func_iterator export_funcs_begin() const {
172    return mExportFuncs.begin();
173  }
174  const_export_func_iterator export_funcs_end() const {
175    return mExportFuncs.end();
176  }
177  inline bool hasExportFunc() const { return !mExportFuncs.empty(); }
178
179  typedef ExportForEachList::const_iterator const_export_foreach_iterator;
180  const_export_foreach_iterator export_foreach_begin() const {
181    return mExportForEach.begin();
182  }
183  const_export_foreach_iterator export_foreach_end() const {
184    return mExportForEach.end();
185  }
186  inline bool hasExportForEach() const { return !mExportForEach.empty(); }
187
188  typedef ExportTypeMap::iterator export_type_iterator;
189  typedef ExportTypeMap::const_iterator const_export_type_iterator;
190  export_type_iterator export_types_begin() { return mExportTypes.begin(); }
191  export_type_iterator export_types_end() { return mExportTypes.end(); }
192  const_export_type_iterator export_types_begin() const {
193    return mExportTypes.begin();
194  }
195  const_export_type_iterator export_types_end() const {
196    return mExportTypes.end();
197  }
198  inline bool hasExportType() const { return !mExportTypes.empty(); }
199  export_type_iterator findExportType(const llvm::StringRef &TypeName) {
200    return mExportTypes.find(TypeName);
201  }
202  const_export_type_iterator findExportType(const llvm::StringRef &TypeName)
203      const {
204    return mExportTypes.find(TypeName);
205  }
206
207  // Insert the specified Typename/Type pair into the map. If the key already
208  // exists in the map, return false and ignore the request, otherwise insert it
209  // and return true.
210  bool insertExportType(const llvm::StringRef &TypeName, RSExportType *Type);
211
212  int getVersion() const { return version; }
213  void setVersion(int v) {
214    version = v;
215  }
216
217  bool isCompatLib() const {
218    // If we are not targeting the actual Android Renderscript classes,
219    // we should reflect code that works with the compatibility library.
220    return (mRSPackageName.compare("android.renderscript") != 0);
221  }
222
223  void addPragma(const std::string &T, const std::string &V) {
224    mPragmas->push_back(make_pair(T, V));
225  }
226
227  // Report an error or a warning to the user.
228  template <unsigned N>
229  clang::DiagnosticBuilder Report(clang::DiagnosticsEngine::Level Level,
230                                             const char (&Message)[N]) {
231  clang::DiagnosticsEngine *DiagEngine = getDiagnostics();
232  return DiagEngine->Report(DiagEngine->getCustomDiagID(Level, Message));
233}
234
235  template <unsigned N>
236  clang::DiagnosticBuilder Report(clang::DiagnosticsEngine::Level Level,
237                                             const clang::SourceLocation Loc,
238                                             const char (&Message)[N]) {
239  clang::DiagnosticsEngine *DiagEngine = getDiagnostics();
240  const clang::SourceManager *SM = getSourceManager();
241  return DiagEngine->Report(clang::FullSourceLoc(Loc, *SM),
242                            DiagEngine->getCustomDiagID(Level, Message));
243}
244
245  // Utility functions to report errors and warnings to make the calling code
246  // easier to read.
247  template <unsigned N>
248  clang::DiagnosticBuilder ReportError(const char (&Message)[N]) {
249    return Report<N>(clang::DiagnosticsEngine::Error, Message);
250  }
251
252  template <unsigned N>
253  clang::DiagnosticBuilder ReportError(const clang::SourceLocation Loc,
254                                       const char (&Message)[N]) {
255    return Report<N>(clang::DiagnosticsEngine::Error, Loc, Message);
256  }
257
258  template <unsigned N>
259  clang::DiagnosticBuilder ReportWarning(const char (&Message)[N]) {
260    return Report<N>(clang::DiagnosticsEngine::Warning, Message);
261  }
262
263  template <unsigned N>
264  clang::DiagnosticBuilder ReportWarning(const clang::SourceLocation Loc,
265                                         const char (&Message)[N]) {
266    return Report<N>(clang::DiagnosticsEngine::Warning, Loc, Message);
267  }
268
269  ~RSContext();
270};
271
272}   // namespace slang
273
274#endif  // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_CONTEXT_H_  NOLINT
275