slang_rs_context.h revision ee4016d1247d3fbe50822de279d3da273d8aef4c
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  unsigned int mTargetAPI;
71  std::vector<std::string> *mGeneratedFileNames;
72
73  llvm::DataLayout *mDataLayout;
74  llvm::LLVMContext &mLLVMContext;
75
76  ExportableList mExportables;
77
78  NeedExportTypeSet mNeedExportTypes;
79
80  std::string *mLicenseNote;
81  std::string mReflectJavaPackageName;
82  std::string mReflectJavaPathName;
83
84  std::string mRSPackageName;
85
86  int version;
87
88  bool mIsCompatLib;
89
90  llvm::OwningPtr<clang::MangleContext> mMangleCtx;
91
92  bool processExportVar(const clang::VarDecl *VD);
93  bool processExportFunc(const clang::FunctionDecl *FD);
94  bool processExportType(const llvm::StringRef &Name);
95
96  void cleanupForEach();
97
98  ExportVarList mExportVars;
99  ExportFuncList mExportFuncs;
100  ExportForEachList mExportForEach;
101  ExportTypeMap mExportTypes;
102
103 public:
104  RSContext(clang::Preprocessor &PP,
105            clang::ASTContext &Ctx,
106            const clang::TargetInfo &Target,
107            PragmaList *Pragmas,
108            unsigned int TargetAPI,
109            std::vector<std::string> *GeneratedFileNames);
110
111  inline clang::Preprocessor &getPreprocessor() const { return mPP; }
112  inline clang::ASTContext &getASTContext() const { return mCtx; }
113  inline clang::MangleContext &getMangleContext() const {
114    return *mMangleCtx;
115  }
116  inline const llvm::DataLayout *getDataLayout() const { return mDataLayout; }
117  inline llvm::LLVMContext &getLLVMContext() const { return mLLVMContext; }
118  inline const clang::SourceManager *getSourceManager() const {
119    return &mPP.getSourceManager();
120  }
121  inline clang::DiagnosticsEngine *getDiagnostics() const {
122    return &mPP.getDiagnostics();
123  }
124  inline unsigned int getTargetAPI() const {
125    return mTargetAPI;
126  }
127
128  inline void setLicenseNote(const std::string &S) {
129    mLicenseNote = new std::string(S);
130  }
131  inline const std::string *getLicenseNote() const { return mLicenseNote; }
132
133  inline void addExportType(const std::string &S) {
134    mNeedExportTypes.insert(S);
135    return;
136  }
137
138  inline void setReflectJavaPackageName(const std::string &S) {
139    mReflectJavaPackageName = S;
140    return;
141  }
142  inline const std::string &getReflectJavaPackageName() const {
143    return mReflectJavaPackageName;
144  }
145
146  inline void setRSPackageName(const std::string &S) {
147    mRSPackageName = S;
148    return;
149  }
150  inline const std::string &getRSPackageName() const {
151    return mRSPackageName;
152  }
153
154  bool processExport();
155  inline void newExportable(RSExportable *E) {
156    if (E != NULL)
157      mExportables.push_back(E);
158  }
159  typedef ExportableList::iterator exportable_iterator;
160  exportable_iterator exportable_begin() {
161    return mExportables.begin();
162  }
163  exportable_iterator exportable_end() {
164    return mExportables.end();
165  }
166
167  typedef ExportVarList::const_iterator const_export_var_iterator;
168  const_export_var_iterator export_vars_begin() const {
169    return mExportVars.begin();
170  }
171  const_export_var_iterator export_vars_end() const {
172    return mExportVars.end();
173  }
174  inline bool hasExportVar() const {
175    return !mExportVars.empty();
176  }
177
178  typedef ExportFuncList::const_iterator const_export_func_iterator;
179  const_export_func_iterator export_funcs_begin() const {
180    return mExportFuncs.begin();
181  }
182  const_export_func_iterator export_funcs_end() const {
183    return mExportFuncs.end();
184  }
185  inline bool hasExportFunc() const { return !mExportFuncs.empty(); }
186
187  typedef ExportForEachList::const_iterator const_export_foreach_iterator;
188  const_export_foreach_iterator export_foreach_begin() const {
189    return mExportForEach.begin();
190  }
191  const_export_foreach_iterator export_foreach_end() const {
192    return mExportForEach.end();
193  }
194  inline bool hasExportForEach() const { return !mExportForEach.empty(); }
195
196  typedef ExportTypeMap::iterator export_type_iterator;
197  typedef ExportTypeMap::const_iterator const_export_type_iterator;
198  export_type_iterator export_types_begin() { return mExportTypes.begin(); }
199  export_type_iterator export_types_end() { return mExportTypes.end(); }
200  const_export_type_iterator export_types_begin() const {
201    return mExportTypes.begin();
202  }
203  const_export_type_iterator export_types_end() const {
204    return mExportTypes.end();
205  }
206  inline bool hasExportType() const { return !mExportTypes.empty(); }
207  export_type_iterator findExportType(const llvm::StringRef &TypeName) {
208    return mExportTypes.find(TypeName);
209  }
210  const_export_type_iterator findExportType(const llvm::StringRef &TypeName)
211      const {
212    return mExportTypes.find(TypeName);
213  }
214
215  // Insert the specified Typename/Type pair into the map. If the key already
216  // exists in the map, return false and ignore the request, otherwise insert it
217  // and return true.
218  bool insertExportType(const llvm::StringRef &TypeName, RSExportType *Type);
219
220  bool reflectToJava(const std::string &OutputPathBase,
221                     const std::string &RSPackageName,
222                     const std::string &InputFileName,
223                     const std::string &OutputBCFileName);
224
225  int getVersion() const { return version; }
226  void setVersion(int v) {
227    version = v;
228    return;
229  }
230
231  bool isCompatLib() const { return mIsCompatLib; }
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