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