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