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