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