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