slang_rs_context.h revision 15e44e66adc350adb4fe0533a442092c64333ab5
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_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 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  bool addForEach(const clang::FunctionDecl* FD);
173  bool processExports();
174  inline void newExportable(RSExportable *E) {
175    if (E != nullptr)
176      mExportables.push_back(E);
177  }
178  typedef ExportableList::iterator exportable_iterator;
179  exportable_iterator exportable_begin() {
180    return mExportables.begin();
181  }
182  exportable_iterator exportable_end() {
183    return mExportables.end();
184  }
185
186  typedef ExportVarList::const_iterator const_export_var_iterator;
187  const_export_var_iterator export_vars_begin() const {
188    return mExportVars.begin();
189  }
190  const_export_var_iterator export_vars_end() const {
191    return mExportVars.end();
192  }
193  inline bool hasExportVar() const {
194    return !mExportVars.empty();
195  }
196
197  typedef ExportFuncList::const_iterator const_export_func_iterator;
198  const_export_func_iterator export_funcs_begin() const {
199    return mExportFuncs.begin();
200  }
201  const_export_func_iterator export_funcs_end() const {
202    return mExportFuncs.end();
203  }
204  inline bool hasExportFunc() const { return !mExportFuncs.empty(); }
205
206  typedef ExportForEachVector::const_iterator const_export_foreach_iterator;
207  const_export_foreach_iterator export_foreach_begin() const {
208    return mExportForEach.begin();
209  }
210  const_export_foreach_iterator export_foreach_end() const {
211    return mExportForEach.end();
212  }
213  inline bool hasExportForEach() const { return !mExportForEach.empty(); }
214  int getForEachSlotNumber(const clang::FunctionDecl* FD);
215
216  typedef ExportReduceList::const_iterator const_export_reduce_iterator;
217  const_export_reduce_iterator export_reduce_begin() const {
218    return mExportReduce.begin();
219  }
220  const_export_reduce_iterator export_reduce_end() const {
221    return mExportReduce.end();
222  }
223  inline bool hasExportReduce() const { return !mExportReduce.empty(); }
224
225  typedef ExportReduceNewList::const_iterator const_export_reduce_new_iterator;
226  const_export_reduce_new_iterator export_reduce_new_begin() const {
227    return mExportReduceNew.begin();
228  }
229  const_export_reduce_new_iterator export_reduce_new_end() const {
230    return mExportReduceNew.end();
231  }
232  inline bool hasExportReduceNew() const { return !mExportReduceNew.empty(); }
233  void addExportReduceNew(RSExportReduceNew *ReduceNew) {
234    mExportReduceNew.push_back(ReduceNew);
235  }
236
237  typedef ExportTypeMap::iterator export_type_iterator;
238  typedef ExportTypeMap::const_iterator const_export_type_iterator;
239  export_type_iterator export_types_begin() { return mExportTypes.begin(); }
240  export_type_iterator export_types_end() { return mExportTypes.end(); }
241  const_export_type_iterator export_types_begin() const {
242    return mExportTypes.begin();
243  }
244  const_export_type_iterator export_types_end() const {
245    return mExportTypes.end();
246  }
247  inline bool hasExportType() const { return !mExportTypes.empty(); }
248  export_type_iterator findExportType(const llvm::StringRef &TypeName) {
249    return mExportTypes.find(TypeName);
250  }
251  const_export_type_iterator findExportType(const llvm::StringRef &TypeName)
252      const {
253    return mExportTypes.find(TypeName);
254  }
255
256  // Insert the specified Typename/Type pair into the map. If the key already
257  // exists in the map, return false and ignore the request, otherwise insert it
258  // and return true.
259  bool insertExportType(const llvm::StringRef &TypeName, RSExportType *Type);
260
261  int getVersion() const { return version; }
262  void setVersion(int v) {
263    version = v;
264  }
265
266  bool isCompatLib() const {
267    // If we are not targeting the actual Android Renderscript classes,
268    // we should reflect code that works with the compatibility library.
269    return (mRSPackageName.compare("android.renderscript") != 0);
270  }
271
272  void addPragma(const std::string &T, const std::string &V) {
273    mPragmas->push_back(make_pair(T, V));
274  }
275  void setPrecision(const std::string &P) { mPrecision = P; }
276  std::string getPrecision() { return mPrecision; }
277
278  // Report an error or a warning to the user.
279  template <unsigned N>
280  clang::DiagnosticBuilder Report(clang::DiagnosticsEngine::Level Level,
281                                             const char (&Message)[N]) {
282  clang::DiagnosticsEngine *DiagEngine = getDiagnostics();
283  return DiagEngine->Report(DiagEngine->getCustomDiagID(Level, Message));
284}
285
286  template <unsigned N>
287  clang::DiagnosticBuilder Report(clang::DiagnosticsEngine::Level Level,
288                                             const clang::SourceLocation Loc,
289                                             const char (&Message)[N]) {
290  clang::DiagnosticsEngine *DiagEngine = getDiagnostics();
291  const clang::SourceManager *SM = getSourceManager();
292  return DiagEngine->Report(clang::FullSourceLoc(Loc, *SM),
293                            DiagEngine->getCustomDiagID(Level, Message));
294}
295
296  // Utility functions to report errors and warnings to make the calling code
297  // easier to read.
298  template <unsigned N>
299  clang::DiagnosticBuilder ReportError(const char (&Message)[N]) {
300    return Report<N>(clang::DiagnosticsEngine::Error, Message);
301  }
302
303  template <unsigned N>
304  clang::DiagnosticBuilder ReportError(const clang::SourceLocation Loc,
305                                       const char (&Message)[N]) {
306    return Report<N>(clang::DiagnosticsEngine::Error, Loc, Message);
307  }
308
309  template <unsigned N>
310  clang::DiagnosticBuilder ReportWarning(const char (&Message)[N]) {
311    return Report<N>(clang::DiagnosticsEngine::Warning, Message);
312  }
313
314  template <unsigned N>
315  clang::DiagnosticBuilder ReportWarning(const clang::SourceLocation Loc,
316                                         const char (&Message)[N]) {
317    return Report<N>(clang::DiagnosticsEngine::Warning, Loc, Message);
318  }
319
320  ~RSContext();
321};
322
323}   // namespace slang
324
325#endif  // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_CONTEXT_H_  NOLINT
326