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