slang_rs_context.h revision 4c7c95cab6a7baf46bbedaf6c2a67d93eafc3e66
122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao/*
222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao * Copyright 2010-2012, The Android Open Source Project
322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao *
422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao * Licensed under the Apache License, Version 2.0 (the "License");
522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao * you may not use this file except in compliance with the License.
622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao * You may obtain a copy of the License at
722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao *
822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao *     http://www.apache.org/licenses/LICENSE-2.0
922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao *
1022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao * Unless required by applicable law or agreed to in writing, software
1122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao * distributed under the License is distributed on an "AS IS" BASIS,
1222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1337b74a387bb3993387029859c2d9d051c41c724eStephen Hines * See the License for the specific language governing permissions and
1437b74a387bb3993387029859c2d9d051c41c724eStephen Hines * limitations under the License.
1537b74a387bb3993387029859c2d9d051c41c724eStephen Hines */
1637b74a387bb3993387029859c2d9d051c41c724eStephen Hines
1722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#ifndef _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_CONTEXT_H_  // NOLINT
1822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#define _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_CONTEXT_H_
1937b74a387bb3993387029859c2d9d051c41c724eStephen Hines
2022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <cstdio>
2122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <list>
2222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <map>
2322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <string>
2422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
2537b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "clang/Lex/Preprocessor.h"
2637b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "clang/AST/Mangle.h"
2737b74a387bb3993387029859c2d9d051c41c724eStephen Hines
2837b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "llvm/ADT/OwningPtr.h"
2937b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "llvm/ADT/StringSet.h"
3022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include "llvm/ADT/StringMap.h"
3122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
3222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include "slang_pragma_recorder.h"
3337b74a387bb3993387029859c2d9d051c41c724eStephen Hines
3437b74a387bb3993387029859c2d9d051c41c724eStephen Hinesnamespace llvm {
3537b74a387bb3993387029859c2d9d051c41c724eStephen Hines  class LLVMContext;
3637b74a387bb3993387029859c2d9d051c41c724eStephen Hines  class DataLayout;
3722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao}   // namespace llvm
3822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
390dea6bc96bb52346737966839ac68644f7939f58Stephen Hinesnamespace clang {
4037b74a387bb3993387029859c2d9d051c41c724eStephen Hines  class VarDecl;
4137b74a387bb3993387029859c2d9d051c41c724eStephen Hines  class ASTContext;
4237b74a387bb3993387029859c2d9d051c41c724eStephen Hines  class TargetInfo;
4337b74a387bb3993387029859c2d9d051c41c724eStephen Hines  class FunctionDecl;
4422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  class SourceManager;
4522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao}   // namespace clang
4622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
4722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaonamespace slang {
480dea6bc96bb52346737966839ac68644f7939f58Stephen Hines  class RSExportable;
4922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  class RSExportVar;
5022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  class RSExportFunc;
5122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  class RSExportForEach;
5222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  class RSExportType;
5322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
5422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaoclass RSContext {
5522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  typedef llvm::StringSet<> NeedExportVarSet;
5622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  typedef llvm::StringSet<> NeedExportFuncSet;
5722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  typedef llvm::StringSet<> NeedExportTypeSet;
5822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
590dea6bc96bb52346737966839ac68644f7939f58Stephen Hines public:
600dea6bc96bb52346737966839ac68644f7939f58Stephen Hines  typedef std::list<RSExportable*> ExportableList;
6137b74a387bb3993387029859c2d9d051c41c724eStephen Hines  typedef std::list<RSExportVar*> ExportVarList;
6237b74a387bb3993387029859c2d9d051c41c724eStephen Hines  typedef std::list<RSExportFunc*> ExportFuncList;
6337b74a387bb3993387029859c2d9d051c41c724eStephen Hines  typedef std::list<RSExportForEach*> ExportForEachList;
6437b74a387bb3993387029859c2d9d051c41c724eStephen Hines  typedef llvm::StringMap<RSExportType*> ExportTypeMap;
6522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
6622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao private:
6722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  clang::Preprocessor &mPP;
6822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  clang::ASTContext &mCtx;
6937b74a387bb3993387029859c2d9d051c41c724eStephen Hines  const clang::TargetInfo &mTarget;
7022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  PragmaList *mPragmas;
7122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  unsigned int mTargetAPI;
7222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  std::vector<std::string> *mGeneratedFileNames;
7322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
7437b74a387bb3993387029859c2d9d051c41c724eStephen Hines  llvm::DataLayout *mDataLayout;
7522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  llvm::LLVMContext &mLLVMContext;
7622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
7722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ExportableList mExportables;
7822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
7922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  NeedExportTypeSet mNeedExportTypes;
8022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
8122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  std::string *mLicenseNote;
8222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  std::string mReflectJavaPackageName;
8322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  std::string mReflectJavaPathName;
840dea6bc96bb52346737966839ac68644f7939f58Stephen Hines
8537b74a387bb3993387029859c2d9d051c41c724eStephen Hines  std::string mRSPackageName;
8637b74a387bb3993387029859c2d9d051c41c724eStephen Hines
8737b74a387bb3993387029859c2d9d051c41c724eStephen Hines  int version;
8837b74a387bb3993387029859c2d9d051c41c724eStephen Hines
8937b74a387bb3993387029859c2d9d051c41c724eStephen Hines  bool mIsCompatLib;
900dea6bc96bb52346737966839ac68644f7939f58Stephen Hines
910dea6bc96bb52346737966839ac68644f7939f58Stephen Hines  llvm::OwningPtr<clang::MangleContext> mMangleCtx;
920dea6bc96bb52346737966839ac68644f7939f58Stephen Hines
930dea6bc96bb52346737966839ac68644f7939f58Stephen Hines  bool processExportVar(const clang::VarDecl *VD);
940dea6bc96bb52346737966839ac68644f7939f58Stephen Hines  bool processExportFunc(const clang::FunctionDecl *FD);
950dea6bc96bb52346737966839ac68644f7939f58Stephen Hines  bool processExportType(const llvm::StringRef &Name);
960dea6bc96bb52346737966839ac68644f7939f58Stephen Hines
9722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  void cleanupForEach();
9822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
9922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ExportVarList mExportVars;
10022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ExportFuncList mExportFuncs;
10122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ExportForEachList mExportForEach;
10222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ExportTypeMap mExportTypes;
10322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
10422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao public:
10522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  RSContext(clang::Preprocessor &PP,
10637b74a387bb3993387029859c2d9d051c41c724eStephen Hines            clang::ASTContext &Ctx,
10722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao            const clang::TargetInfo &Target,
10822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao            PragmaList *Pragmas,
10922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao            unsigned int TargetAPI,
11037b74a387bb3993387029859c2d9d051c41c724eStephen Hines            std::vector<std::string> *GeneratedFileNames);
11122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
11222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  inline clang::Preprocessor &getPreprocessor() const { return mPP; }
11322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  inline clang::ASTContext &getASTContext() const { return mCtx; }
11437b74a387bb3993387029859c2d9d051c41c724eStephen Hines  inline clang::MangleContext &getMangleContext() const {
11522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    return *mMangleCtx;
11622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  }
11722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  inline const llvm::DataLayout *getDataLayout() const { return mDataLayout; }
11837b74a387bb3993387029859c2d9d051c41c724eStephen Hines  inline llvm::LLVMContext &getLLVMContext() const { return mLLVMContext; }
11922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  inline const clang::SourceManager *getSourceManager() const {
12022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    return &mPP.getSourceManager();
12122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  }
12237b74a387bb3993387029859c2d9d051c41c724eStephen Hines  inline clang::DiagnosticsEngine *getDiagnostics() const {
12322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    return &mPP.getDiagnostics();
12422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  }
12522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  inline unsigned int getTargetAPI() const {
12637b74a387bb3993387029859c2d9d051c41c724eStephen Hines    return mTargetAPI;
12737b74a387bb3993387029859c2d9d051c41c724eStephen Hines  }
12837b74a387bb3993387029859c2d9d051c41c724eStephen Hines
12922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  inline void setLicenseNote(const std::string &S) {
13037b74a387bb3993387029859c2d9d051c41c724eStephen Hines    mLicenseNote = new std::string(S);
13137b74a387bb3993387029859c2d9d051c41c724eStephen Hines  }
132  inline const std::string *getLicenseNote() const { return mLicenseNote; }
133
134  inline void addExportType(const std::string &S) {
135    mNeedExportTypes.insert(S);
136    return;
137  }
138
139  inline void setReflectJavaPackageName(const std::string &S) {
140    mReflectJavaPackageName = S;
141    return;
142  }
143  inline const std::string &getReflectJavaPackageName() {
144    return mReflectJavaPackageName;
145  }
146
147  inline void setRSPackageName(const std::string &S) {
148    mRSPackageName = S;
149    return;
150  }
151  inline const std::string &getRSPackageName() {
152    return mRSPackageName;
153  }
154
155  bool processExport();
156  inline void newExportable(RSExportable *E) {
157    if (E != NULL)
158      mExportables.push_back(E);
159  }
160  typedef ExportableList::iterator exportable_iterator;
161  exportable_iterator exportable_begin() {
162    return mExportables.begin();
163  }
164  exportable_iterator exportable_end() {
165    return mExportables.end();
166  }
167
168  typedef ExportVarList::const_iterator const_export_var_iterator;
169  const_export_var_iterator export_vars_begin() const {
170    return mExportVars.begin();
171  }
172  const_export_var_iterator export_vars_end() const {
173    return mExportVars.end();
174  }
175  inline bool hasExportVar() const {
176    return !mExportVars.empty();
177  }
178
179  typedef ExportFuncList::const_iterator const_export_func_iterator;
180  const_export_func_iterator export_funcs_begin() const {
181    return mExportFuncs.begin();
182  }
183  const_export_func_iterator export_funcs_end() const {
184    return mExportFuncs.end();
185  }
186  inline bool hasExportFunc() const { return !mExportFuncs.empty(); }
187
188  typedef ExportForEachList::const_iterator const_export_foreach_iterator;
189  const_export_foreach_iterator export_foreach_begin() const {
190    return mExportForEach.begin();
191  }
192  const_export_foreach_iterator export_foreach_end() const {
193    return mExportForEach.end();
194  }
195  inline bool hasExportForEach() const { return !mExportForEach.empty(); }
196
197  typedef ExportTypeMap::iterator export_type_iterator;
198  typedef ExportTypeMap::const_iterator const_export_type_iterator;
199  export_type_iterator export_types_begin() { return mExportTypes.begin(); }
200  export_type_iterator export_types_end() { return mExportTypes.end(); }
201  const_export_type_iterator export_types_begin() const {
202    return mExportTypes.begin();
203  }
204  const_export_type_iterator export_types_end() const {
205    return mExportTypes.end();
206  }
207  inline bool hasExportType() const { return !mExportTypes.empty(); }
208  export_type_iterator findExportType(const llvm::StringRef &TypeName) {
209    return mExportTypes.find(TypeName);
210  }
211  const_export_type_iterator findExportType(const llvm::StringRef &TypeName)
212      const {
213    return mExportTypes.find(TypeName);
214  }
215
216  // Insert the specified Typename/Type pair into the map. If the key already
217  // exists in the map, return false and ignore the request, otherwise insert it
218  // and return true.
219  bool insertExportType(const llvm::StringRef &TypeName, RSExportType *Type);
220
221  bool reflectToJava(const std::string &OutputPathBase,
222                     const std::string &OutputPackageName,
223                     const std::string &RSPackageName,
224                     const std::string &InputFileName,
225                     const std::string &OutputBCFileName,
226                     std::string *RealPackageName);
227
228  int getVersion() const { return version; }
229  void setVersion(int v) {
230    version = v;
231    return;
232  }
233
234  bool isCompatLib() const { return mIsCompatLib; }
235
236  void addPragma(const std::string &T, const std::string &V) {
237    mPragmas->push_back(make_pair(T, V));
238  }
239
240  ~RSContext();
241};
242
243}   // namespace slang
244
245#endif  // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_CONTEXT_H_  NOLINT
246