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