slang_rs_context.h revision f2174cfd6a556b51aadf2b8765e50df080e8f18e
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 {
102    return mMangleCtx;
103  }
104  inline const llvm::TargetData *getTargetData() const { return mTargetData; }
105  inline llvm::LLVMContext &getLLVMContext() const { return mLLVMContext; }
106  inline const clang::SourceManager *getSourceManager() const {
107    return &mPP.getSourceManager();
108  }
109  inline clang::Diagnostic *getDiagnostics() const {
110    return &mPP.getDiagnostics();
111  }
112
113  inline void setLicenseNote(const std::string &S) {
114    mLicenseNote = new std::string(S);
115  }
116  inline const std::string *getLicenseNote() const { return mLicenseNote; }
117
118  inline void addExportType(const std::string &S) {
119    mNeedExportTypes.insert(S);
120    return;
121  }
122
123  inline void setReflectJavaPackageName(const std::string &S) {
124    mReflectJavaPackageName = S;
125    return;
126  }
127  inline const std::string &getReflectJavaPackageName() {
128    return mReflectJavaPackageName;
129  }
130
131  bool processExport();
132  inline void newExportable(RSExportable *E) {
133    if (E != NULL)
134      mExportables.push_back(E);
135  }
136  typedef ExportableList::iterator exportable_iterator;
137  exportable_iterator exportable_begin() {
138    return mExportables.begin();
139  }
140  exportable_iterator exportable_end() {
141    return mExportables.end();
142  }
143
144  typedef ExportVarList::const_iterator const_export_var_iterator;
145  const_export_var_iterator export_vars_begin() const {
146    return mExportVars.begin();
147  }
148  const_export_var_iterator export_vars_end() const {
149    return mExportVars.end();
150  }
151  inline bool hasExportVar() const {
152    return !mExportVars.empty();
153  }
154
155  typedef ExportFuncList::const_iterator const_export_func_iterator;
156  const_export_func_iterator export_funcs_begin() const {
157    return mExportFuncs.begin();
158  }
159  const_export_func_iterator export_funcs_end() const {
160    return mExportFuncs.end();
161  }
162  inline bool hasExportFunc() const { return !mExportFuncs.empty(); }
163
164  typedef ExportTypeMap::iterator export_type_iterator;
165  typedef ExportTypeMap::const_iterator const_export_type_iterator;
166  export_type_iterator export_types_begin() { return mExportTypes.begin(); }
167  export_type_iterator export_types_end() { return mExportTypes.end(); }
168  const_export_type_iterator export_types_begin() const {
169    return mExportTypes.begin();
170  }
171  const_export_type_iterator export_types_end() const {
172    return mExportTypes.end();
173  }
174  inline bool hasExportType() const { return !mExportTypes.empty(); }
175  export_type_iterator findExportType(const llvm::StringRef &TypeName) {
176    return mExportTypes.find(TypeName);
177  }
178  const_export_type_iterator findExportType(const llvm::StringRef &TypeName)
179      const {
180    return mExportTypes.find(TypeName);
181  }
182
183  // Insert the specified Typename/Type pair into the map. If the key already
184  // exists in the map, return false and ignore the request, otherwise insert it
185  // and return true.
186  bool insertExportType(const llvm::StringRef &TypeName, RSExportType *Type);
187
188  bool reflectToJava(const std::string &OutputPathBase,
189                     const std::string &OutputPackageName,
190                     const std::string &InputFileName,
191                     const std::string &OutputBCFileName,
192                     std::string *RealPackageName);
193
194  int getVersion() const { return version; }
195  void setVersion(int v) {
196    version = v;
197    return;
198  }
199
200  void addPragma(const std::string &T, const std::string &V) {
201    mPragmas->push_back(make_pair(T, V));
202  }
203
204  ~RSContext();
205};
206
207}   // namespace slang
208
209#endif  // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_CONTEXT_H_  NOLINT
210