slang_rs_context.h revision 9ab3abd01219b458eb8be20dad6dcffd8dfc0f27
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
118 public:
119  RSContext(clang::Preprocessor &PP,
120            clang::ASTContext &Ctx,
121            const clang::TargetInfo &Target,
122            PragmaList *Pragmas,
123            unsigned int TargetAPI,
124            bool Verbose);
125
126  inline clang::Preprocessor &getPreprocessor() const { return mPP; }
127  inline clang::ASTContext &getASTContext() const { return mCtx; }
128  inline clang::MangleContext &getMangleContext() const {
129    return *mMangleCtx;
130  }
131  inline const llvm::DataLayout *getDataLayout() const { return mDataLayout; }
132  inline llvm::LLVMContext &getLLVMContext() const { return mLLVMContext; }
133  inline const clang::SourceManager *getSourceManager() const {
134    return &mPP.getSourceManager();
135  }
136  inline clang::DiagnosticsEngine *getDiagnostics() const {
137    return &mPP.getDiagnostics();
138  }
139  inline unsigned int getTargetAPI() const {
140    return mTargetAPI;
141  }
142
143  inline bool getVerbose() const {
144    return mVerbose;
145  }
146  inline bool is64Bit() const {
147    return mIs64Bit;
148  }
149
150  inline void setLicenseNote(const std::string &S) {
151    mLicenseNote = new std::string(S);
152  }
153  inline const std::string *getLicenseNote() const { return mLicenseNote; }
154
155  inline void addExportType(const std::string &S) {
156    mNeedExportTypes.insert(S);
157  }
158
159  inline void setReflectJavaPackageName(const std::string &S) {
160    mReflectJavaPackageName = S;
161  }
162  inline const std::string &getReflectJavaPackageName() const {
163    return mReflectJavaPackageName;
164  }
165
166  inline void setRSPackageName(const std::string &S) {
167    mRSPackageName = S;
168  }
169
170  inline const std::string &getRSPackageName() const { return mRSPackageName; }
171
172  void setAllocationType(const clang::TypeDecl* TD);
173  inline const clang::QualType& getAllocationType() const {
174    return mAllocationType;
175  }
176
177  bool addForEach(const clang::FunctionDecl* FD);
178  bool processExports();
179  inline void newExportable(RSExportable *E) {
180    if (E != nullptr)
181      mExportables.push_back(E);
182  }
183  typedef ExportableList::iterator exportable_iterator;
184  exportable_iterator exportable_begin() {
185    return mExportables.begin();
186  }
187  exportable_iterator exportable_end() {
188    return mExportables.end();
189  }
190
191  typedef ExportVarList::const_iterator const_export_var_iterator;
192  const_export_var_iterator export_vars_begin() const {
193    return mExportVars.begin();
194  }
195  const_export_var_iterator export_vars_end() const {
196    return mExportVars.end();
197  }
198  inline bool hasExportVar() const {
199    return !mExportVars.empty();
200  }
201
202  typedef ExportFuncList::const_iterator const_export_func_iterator;
203  const_export_func_iterator export_funcs_begin() const {
204    return mExportFuncs.begin();
205  }
206  const_export_func_iterator export_funcs_end() const {
207    return mExportFuncs.end();
208  }
209  inline bool hasExportFunc() const { return !mExportFuncs.empty(); }
210
211  typedef ExportForEachVector::const_iterator const_export_foreach_iterator;
212  const_export_foreach_iterator export_foreach_begin() const {
213    return mExportForEach.begin();
214  }
215  const_export_foreach_iterator export_foreach_end() const {
216    return mExportForEach.end();
217  }
218  inline bool hasExportForEach() const { return !mExportForEach.empty(); }
219  int getForEachSlotNumber(const clang::FunctionDecl* FD);
220
221  typedef ExportReduceList::const_iterator const_export_reduce_iterator;
222  const_export_reduce_iterator export_reduce_begin() const {
223    return mExportReduce.begin();
224  }
225  const_export_reduce_iterator export_reduce_end() const {
226    return mExportReduce.end();
227  }
228  inline bool hasExportReduce() const { return !mExportReduce.empty(); }
229
230  typedef ExportReduceNewList::const_iterator const_export_reduce_new_iterator;
231  const_export_reduce_new_iterator export_reduce_new_begin() const {
232    return mExportReduceNew.begin();
233  }
234  const_export_reduce_new_iterator export_reduce_new_end() const {
235    return mExportReduceNew.end();
236  }
237  inline bool hasExportReduceNew() const { return !mExportReduceNew.empty(); }
238  void addExportReduceNew(RSExportReduceNew *ReduceNew) {
239    mExportReduceNew.push_back(ReduceNew);
240  }
241
242  typedef ExportTypeMap::iterator export_type_iterator;
243  typedef ExportTypeMap::const_iterator const_export_type_iterator;
244  export_type_iterator export_types_begin() { return mExportTypes.begin(); }
245  export_type_iterator export_types_end() { return mExportTypes.end(); }
246  const_export_type_iterator export_types_begin() const {
247    return mExportTypes.begin();
248  }
249  const_export_type_iterator export_types_end() const {
250    return mExportTypes.end();
251  }
252  inline bool hasExportType() const { return !mExportTypes.empty(); }
253  export_type_iterator findExportType(const llvm::StringRef &TypeName) {
254    return mExportTypes.find(TypeName);
255  }
256  const_export_type_iterator findExportType(const llvm::StringRef &TypeName)
257      const {
258    return mExportTypes.find(TypeName);
259  }
260
261  // Insert the specified Typename/Type pair into the map. If the key already
262  // exists in the map, return false and ignore the request, otherwise insert it
263  // and return true.
264  bool insertExportType(const llvm::StringRef &TypeName, RSExportType *Type);
265
266  int getVersion() const { return version; }
267  void setVersion(int v) {
268    version = v;
269  }
270
271  bool isCompatLib() const {
272    // If we are not targeting the actual Android Renderscript classes,
273    // we should reflect code that works with the compatibility library.
274    return (mRSPackageName.compare("android.renderscript") != 0);
275  }
276
277  void addPragma(const std::string &T, const std::string &V) {
278    mPragmas->push_back(make_pair(T, V));
279  }
280  void setPrecision(const std::string &P) { mPrecision = P; }
281  std::string getPrecision() { return mPrecision; }
282
283  // Report an error or a warning to the user.
284  template <unsigned N>
285  clang::DiagnosticBuilder Report(clang::DiagnosticsEngine::Level Level,
286                                             const char (&Message)[N]) {
287  clang::DiagnosticsEngine *DiagEngine = getDiagnostics();
288  return DiagEngine->Report(DiagEngine->getCustomDiagID(Level, Message));
289}
290
291  template <unsigned N>
292  clang::DiagnosticBuilder Report(clang::DiagnosticsEngine::Level Level,
293                                             const clang::SourceLocation Loc,
294                                             const char (&Message)[N]) {
295  clang::DiagnosticsEngine *DiagEngine = getDiagnostics();
296  const clang::SourceManager *SM = getSourceManager();
297  return DiagEngine->Report(clang::FullSourceLoc(Loc, *SM),
298                            DiagEngine->getCustomDiagID(Level, Message));
299}
300
301  // Utility functions to report errors and warnings to make the calling code
302  // easier to read.
303  template <unsigned N>
304  clang::DiagnosticBuilder ReportError(const char (&Message)[N]) {
305    return Report<N>(clang::DiagnosticsEngine::Error, Message);
306  }
307
308  template <unsigned N>
309  clang::DiagnosticBuilder ReportError(const clang::SourceLocation Loc,
310                                       const char (&Message)[N]) {
311    return Report<N>(clang::DiagnosticsEngine::Error, Loc, Message);
312  }
313
314  template <unsigned N>
315  clang::DiagnosticBuilder ReportWarning(const char (&Message)[N]) {
316    return Report<N>(clang::DiagnosticsEngine::Warning, Message);
317  }
318
319  template <unsigned N>
320  clang::DiagnosticBuilder ReportWarning(const clang::SourceLocation Loc,
321                                         const char (&Message)[N]) {
322    return Report<N>(clang::DiagnosticsEngine::Warning, Loc, Message);
323  }
324
325  ~RSContext();
326};
327
328}   // namespace slang
329
330#endif  // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_CONTEXT_H_  NOLINT
331