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