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