slang_rs_context.h revision 2615f383dfc1542a05f19aee23b03a09bd018f4e
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#include <vector>
25
26#include "clang/Lex/Preprocessor.h"
27#include "clang/AST/Mangle.h"
28
29#include "llvm/ADT/StringSet.h"
30#include "llvm/ADT/StringMap.h"
31
32#include "slang_pragma_list.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 QualType;
45  class SourceManager;
46  class TypeDecl;
47}   // namespace clang
48
49namespace slang {
50  class RSExportable;
51  class RSExportVar;
52  class RSExportFunc;
53  class RSExportForEach;
54  class RSExportReduce;
55  class RSExportReduceNew;
56  class RSExportType;
57
58class RSContext {
59  typedef llvm::StringSet<> NeedExportVarSet;
60  typedef llvm::StringSet<> NeedExportFuncSet;
61  typedef llvm::StringSet<> NeedExportTypeSet;
62
63 public:
64  typedef std::list<RSExportable*> ExportableList;
65  typedef std::list<RSExportVar*> ExportVarList;
66  typedef std::list<RSExportFunc*> ExportFuncList;
67  typedef std::vector<RSExportForEach*> ExportForEachVector;
68  typedef std::list<RSExportReduce*> ExportReduceList;
69  typedef std::list<RSExportReduceNew*> ExportReduceNewList;
70  typedef llvm::StringMap<RSExportType*> ExportTypeMap;
71
72 private:
73  clang::Preprocessor &mPP;
74  clang::ASTContext &mCtx;
75  PragmaList *mPragmas;
76  // Precision specified via pragma, either rs_fp_full or rs_fp_relaxed. If
77  // empty, rs_fp_full is assumed.
78  std::string mPrecision;
79  unsigned int mTargetAPI;
80  bool mVerbose;
81
82  llvm::DataLayout *mDataLayout;
83  llvm::LLVMContext &mLLVMContext;
84
85  ExportableList mExportables;
86
87  NeedExportTypeSet mNeedExportTypes;
88
89  std::string *mLicenseNote;
90  std::string mReflectJavaPackageName;
91  std::string mReflectJavaPathName;
92
93  std::string mRSPackageName;
94
95  int version;
96
97  std::unique_ptr<clang::MangleContext> mMangleCtx;
98
99  bool mIs64Bit;
100
101  bool processExportVar(const clang::VarDecl *VD);
102  bool processExportFunc(const clang::FunctionDecl *FD);
103  bool processExportType(const llvm::StringRef &Name);
104
105  int getForEachSlotNumber(const clang::StringRef& funcName);
106  unsigned mNextSlot;
107
108  ExportVarList mExportVars;
109  ExportFuncList mExportFuncs;
110  std::map<llvm::StringRef, unsigned> mExportForEachMap;
111  ExportForEachVector mExportForEach;
112  ExportReduceList mExportReduce;
113  ExportReduceNewList mExportReduceNew;
114  ExportTypeMap mExportTypes;
115
116  clang::QualType mAllocationType;
117  clang::QualType mScriptCallType;
118
119 public:
120  RSContext(clang::Preprocessor &PP,
121            clang::ASTContext &Ctx,
122            const clang::TargetInfo &Target,
123            PragmaList *Pragmas,
124            unsigned int TargetAPI,
125            bool Verbose);
126
127  inline clang::Preprocessor &getPreprocessor() const { return mPP; }
128  inline clang::ASTContext &getASTContext() const { return mCtx; }
129  inline clang::MangleContext &getMangleContext() const {
130    return *mMangleCtx;
131  }
132  inline const llvm::DataLayout *getDataLayout() const { return mDataLayout; }
133  inline llvm::LLVMContext &getLLVMContext() const { return mLLVMContext; }
134  inline const clang::SourceManager *getSourceManager() const {
135    return &mPP.getSourceManager();
136  }
137  inline clang::DiagnosticsEngine *getDiagnostics() const {
138    return &mPP.getDiagnostics();
139  }
140  inline unsigned int getTargetAPI() const {
141    return mTargetAPI;
142  }
143
144  inline bool getVerbose() const {
145    return mVerbose;
146  }
147  inline bool is64Bit() const {
148    return mIs64Bit;
149  }
150
151  inline void setLicenseNote(const std::string &S) {
152    mLicenseNote = new std::string(S);
153  }
154  inline const std::string *getLicenseNote() const { return mLicenseNote; }
155
156  inline void addExportType(const std::string &S) {
157    mNeedExportTypes.insert(S);
158  }
159
160  inline void setReflectJavaPackageName(const std::string &S) {
161    mReflectJavaPackageName = S;
162  }
163  inline const std::string &getReflectJavaPackageName() const {
164    return mReflectJavaPackageName;
165  }
166
167  inline void setRSPackageName(const std::string &S) {
168    mRSPackageName = S;
169  }
170
171  inline const std::string &getRSPackageName() const { return mRSPackageName; }
172
173  void setAllocationType(const clang::TypeDecl* TD);
174  inline const clang::QualType& getAllocationType() const {
175    return mAllocationType;
176  }
177
178  void setScriptCallType(const clang::TypeDecl* TD);
179  inline const clang::QualType& getScriptCallType() const {
180    return mScriptCallType;
181  }
182
183  bool addForEach(const clang::FunctionDecl* FD);
184  bool processExports();
185  inline void newExportable(RSExportable *E) {
186    if (E != nullptr)
187      mExportables.push_back(E);
188  }
189  typedef ExportableList::iterator exportable_iterator;
190  exportable_iterator exportable_begin() {
191    return mExportables.begin();
192  }
193  exportable_iterator exportable_end() {
194    return mExportables.end();
195  }
196
197  typedef ExportVarList::const_iterator const_export_var_iterator;
198  const_export_var_iterator export_vars_begin() const {
199    return mExportVars.begin();
200  }
201  const_export_var_iterator export_vars_end() const {
202    return mExportVars.end();
203  }
204  inline bool hasExportVar() const {
205    return !mExportVars.empty();
206  }
207
208  typedef ExportFuncList::const_iterator const_export_func_iterator;
209  const_export_func_iterator export_funcs_begin() const {
210    return mExportFuncs.begin();
211  }
212  const_export_func_iterator export_funcs_end() const {
213    return mExportFuncs.end();
214  }
215  inline bool hasExportFunc() const { return !mExportFuncs.empty(); }
216
217  typedef ExportForEachVector::const_iterator const_export_foreach_iterator;
218  const_export_foreach_iterator export_foreach_begin() const {
219    return mExportForEach.begin();
220  }
221  const_export_foreach_iterator export_foreach_end() const {
222    return mExportForEach.end();
223  }
224  inline bool hasExportForEach() const { return !mExportForEach.empty(); }
225  int getForEachSlotNumber(const clang::FunctionDecl* FD);
226
227  typedef ExportReduceList::const_iterator const_export_reduce_iterator;
228  const_export_reduce_iterator export_reduce_begin() const {
229    return mExportReduce.begin();
230  }
231  const_export_reduce_iterator export_reduce_end() const {
232    return mExportReduce.end();
233  }
234  inline bool hasExportReduce() const { return !mExportReduce.empty(); }
235
236  typedef ExportReduceNewList::const_iterator const_export_reduce_new_iterator;
237  const_export_reduce_new_iterator export_reduce_new_begin() const {
238    return mExportReduceNew.begin();
239  }
240  const_export_reduce_new_iterator export_reduce_new_end() const {
241    return mExportReduceNew.end();
242  }
243  inline bool hasExportReduceNew() const { return !mExportReduceNew.empty(); }
244  void addExportReduceNew(RSExportReduceNew *ReduceNew) {
245    mExportReduceNew.push_back(ReduceNew);
246  }
247
248  typedef ExportTypeMap::iterator export_type_iterator;
249  typedef ExportTypeMap::const_iterator const_export_type_iterator;
250  export_type_iterator export_types_begin() { return mExportTypes.begin(); }
251  export_type_iterator export_types_end() { return mExportTypes.end(); }
252  const_export_type_iterator export_types_begin() const {
253    return mExportTypes.begin();
254  }
255  const_export_type_iterator export_types_end() const {
256    return mExportTypes.end();
257  }
258  inline bool hasExportType() const { return !mExportTypes.empty(); }
259  export_type_iterator findExportType(const llvm::StringRef &TypeName) {
260    return mExportTypes.find(TypeName);
261  }
262  const_export_type_iterator findExportType(const llvm::StringRef &TypeName)
263      const {
264    return mExportTypes.find(TypeName);
265  }
266
267  // Insert the specified Typename/Type pair into the map. If the key already
268  // exists in the map, return false and ignore the request, otherwise insert it
269  // and return true.
270  bool insertExportType(const llvm::StringRef &TypeName, RSExportType *Type);
271
272  int getVersion() const { return version; }
273  void setVersion(int v) {
274    version = v;
275  }
276
277  bool isCompatLib() const {
278    // If we are not targeting the actual Android Renderscript classes,
279    // we should reflect code that works with the compatibility library.
280    return (mRSPackageName.compare("android.renderscript") != 0);
281  }
282
283  void addPragma(const std::string &T, const std::string &V) {
284    mPragmas->push_back(make_pair(T, V));
285  }
286  void setPrecision(const std::string &P) { mPrecision = P; }
287  std::string getPrecision() { return mPrecision; }
288
289  // Report an error or a warning to the user.
290  template <unsigned N>
291  clang::DiagnosticBuilder Report(clang::DiagnosticsEngine::Level Level,
292                                             const char (&Message)[N]) {
293  clang::DiagnosticsEngine *DiagEngine = getDiagnostics();
294  return DiagEngine->Report(DiagEngine->getCustomDiagID(Level, Message));
295}
296
297  template <unsigned N>
298  clang::DiagnosticBuilder Report(clang::DiagnosticsEngine::Level Level,
299                                             const clang::SourceLocation Loc,
300                                             const char (&Message)[N]) {
301  clang::DiagnosticsEngine *DiagEngine = getDiagnostics();
302  const clang::SourceManager *SM = getSourceManager();
303  return DiagEngine->Report(clang::FullSourceLoc(Loc, *SM),
304                            DiagEngine->getCustomDiagID(Level, Message));
305}
306
307  // Utility functions to report errors and warnings to make the calling code
308  // easier to read.
309  template <unsigned N>
310  clang::DiagnosticBuilder ReportError(const char (&Message)[N]) {
311    return Report<N>(clang::DiagnosticsEngine::Error, Message);
312  }
313
314  template <unsigned N>
315  clang::DiagnosticBuilder ReportError(const clang::SourceLocation Loc,
316                                       const char (&Message)[N]) {
317    return Report<N>(clang::DiagnosticsEngine::Error, Loc, Message);
318  }
319
320  template <unsigned N>
321  clang::DiagnosticBuilder ReportWarning(const char (&Message)[N]) {
322    return Report<N>(clang::DiagnosticsEngine::Warning, Message);
323  }
324
325  template <unsigned N>
326  clang::DiagnosticBuilder ReportWarning(const clang::SourceLocation Loc,
327                                         const char (&Message)[N]) {
328    return Report<N>(clang::DiagnosticsEngine::Warning, Loc, Message);
329  }
330
331  ~RSContext();
332};
333
334}   // namespace slang
335
336#endif  // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_CONTEXT_H_  NOLINT
337