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