blob: 2463ad967e7ac27114acb266acbaf9144860e6c8 [file] [log] [blame]
Zonr Changc383a502010-10-12 01:52:08 +08001/*
Stephen Hines0a813a32012-08-03 16:52:40 -07002 * Copyright 2010-2012, The Android Open Source Project
Zonr Changc383a502010-10-12 01:52:08 +08003 *
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
Stephen Hinese639eb52010-11-08 19:27:20 -080017#ifndef _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_CONTEXT_H_ // NOLINT
18#define _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_CONTEXT_H_
Shih-wei Liao462aefd2010-06-04 15:32:04 -070019
Shih-wei Liao462aefd2010-06-04 15:32:04 -070020#include <cstdio>
Stephen Hinese639eb52010-11-08 19:27:20 -080021#include <list>
22#include <map>
23#include <string>
24
25#include "clang/Lex/Preprocessor.h"
Loganbe274822011-02-16 22:02:54 +080026#include "clang/AST/Mangle.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070027
Loganbe274822011-02-16 22:02:54 +080028#include "llvm/ADT/OwningPtr.h"
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070029#include "llvm/ADT/StringSet.h"
30#include "llvm/ADT/StringMap.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070031
Stephen Hines3fd0a942011-01-18 12:27:39 -080032#include "slang_pragma_recorder.h"
33
Shih-wei Liao462aefd2010-06-04 15:32:04 -070034namespace llvm {
zonr6315f762010-10-05 15:35:14 +080035 class LLVMContext;
36 class TargetData;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070037} // namespace llvm
Shih-wei Liao462aefd2010-06-04 15:32:04 -070038
39namespace clang {
zonr6315f762010-10-05 15:35:14 +080040 class VarDecl;
41 class ASTContext;
42 class TargetInfo;
43 class FunctionDecl;
44 class SourceManager;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070045} // namespace clang
Shih-wei Liao462aefd2010-06-04 15:32:04 -070046
47namespace slang {
Zonr Changa41ce1d2010-10-06 02:23:12 +080048 class RSExportable;
49 class RSExportVar;
50 class RSExportFunc;
Stephen Hines593a8942011-05-10 15:29:50 -070051 class RSExportForEach;
Zonr Changa41ce1d2010-10-06 02:23:12 +080052 class RSExportType;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070053
54class RSContext {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070055 typedef llvm::StringSet<> NeedExportVarSet;
56 typedef llvm::StringSet<> NeedExportFuncSet;
57 typedef llvm::StringSet<> NeedExportTypeSet;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070058
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070059 public:
Zonr Changa41ce1d2010-10-06 02:23:12 +080060 typedef std::list<RSExportable*> ExportableList;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070061 typedef std::list<RSExportVar*> ExportVarList;
62 typedef std::list<RSExportFunc*> ExportFuncList;
Stephen Hines593a8942011-05-10 15:29:50 -070063 typedef std::list<RSExportForEach*> ExportForEachList;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070064 typedef llvm::StringMap<RSExportType*> ExportTypeMap;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070065
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070066 private:
Stephen Hines9e5b5032010-11-03 13:19:14 -070067 clang::Preprocessor &mPP;
68 clang::ASTContext &mCtx;
69 const clang::TargetInfo &mTarget;
Stephen Hines3fd0a942011-01-18 12:27:39 -080070 PragmaList *mPragmas;
Stephen Hines4a4bf922011-08-18 17:20:33 -070071 unsigned int mTargetAPI;
Stephen Hines4cc67fc2011-01-31 16:48:57 -080072 std::vector<std::string> *mGeneratedFileNames;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070073
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070074 llvm::TargetData *mTargetData;
75 llvm::LLVMContext &mLLVMContext;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070076
Zonr Changa41ce1d2010-10-06 02:23:12 +080077 ExportableList mExportables;
78
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070079 NeedExportTypeSet mNeedExportTypes;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070080
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070081 std::string *mLicenseNote;
82 std::string mReflectJavaPackageName;
83 std::string mReflectJavaPathName;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070084
Stephen Hines0a813a32012-08-03 16:52:40 -070085 std::string mRSPackageName;
86
Stephen Hines96ab06c2011-01-05 15:29:26 -080087 int version;
Loganbe274822011-02-16 22:02:54 +080088 llvm::OwningPtr<clang::MangleContext> mMangleCtx;
Stephen Hines96ab06c2011-01-05 15:29:26 -080089
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070090 bool processExportVar(const clang::VarDecl *VD);
91 bool processExportFunc(const clang::FunctionDecl *FD);
92 bool processExportType(const llvm::StringRef &Name);
Shih-wei Liao537446c2010-06-11 16:05:55 -070093
Stephen Hinesc17e1982012-02-22 12:30:45 -080094 void cleanupForEach();
95
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070096 ExportVarList mExportVars;
97 ExportFuncList mExportFuncs;
Stephen Hines593a8942011-05-10 15:29:50 -070098 ExportForEachList mExportForEach;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070099 ExportTypeMap mExportTypes;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700100
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700101 public:
Stephen Hines9e5b5032010-11-03 13:19:14 -0700102 RSContext(clang::Preprocessor &PP,
103 clang::ASTContext &Ctx,
Stephen Hines3fd0a942011-01-18 12:27:39 -0800104 const clang::TargetInfo &Target,
Stephen Hines4cc67fc2011-01-31 16:48:57 -0800105 PragmaList *Pragmas,
Stephen Hines4a4bf922011-08-18 17:20:33 -0700106 unsigned int TargetAPI,
Stephen Hines4cc67fc2011-01-31 16:48:57 -0800107 std::vector<std::string> *GeneratedFileNames);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700108
Stephen Hines9e5b5032010-11-03 13:19:14 -0700109 inline clang::Preprocessor &getPreprocessor() const { return mPP; }
110 inline clang::ASTContext &getASTContext() const { return mCtx; }
Loganbe274822011-02-16 22:02:54 +0800111 inline clang::MangleContext &getMangleContext() const {
112 return *mMangleCtx;
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800113 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700114 inline const llvm::TargetData *getTargetData() const { return mTargetData; }
115 inline llvm::LLVMContext &getLLVMContext() const { return mLLVMContext; }
116 inline const clang::SourceManager *getSourceManager() const {
Stephen Hines9e5b5032010-11-03 13:19:14 -0700117 return &mPP.getSourceManager();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700118 }
Logan Chien9207a2e2011-10-21 15:39:28 +0800119 inline clang::DiagnosticsEngine *getDiagnostics() const {
Stephen Hines2ef9bc02010-12-13 18:33:23 -0800120 return &mPP.getDiagnostics();
121 }
Stephen Hines4a4bf922011-08-18 17:20:33 -0700122 inline unsigned int getTargetAPI() const {
123 return mTargetAPI;
124 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700125
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700126 inline void setLicenseNote(const std::string &S) {
127 mLicenseNote = new std::string(S);
128 }
129 inline const std::string *getLicenseNote() const { return mLicenseNote; }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700130
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700131 inline void addExportType(const std::string &S) {
132 mNeedExportTypes.insert(S);
133 return;
134 }
Victor Hsiehd8a0d182010-07-07 19:22:33 +0800135
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700136 inline void setReflectJavaPackageName(const std::string &S) {
137 mReflectJavaPackageName = S;
138 return;
139 }
Stephen Hines4cc67fc2011-01-31 16:48:57 -0800140 inline const std::string &getReflectJavaPackageName() {
141 return mReflectJavaPackageName;
142 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700143
Stephen Hines0a813a32012-08-03 16:52:40 -0700144 inline void setRSPackageName(const std::string &S) {
145 mRSPackageName = S;
146 return;
147 }
148 inline const std::string &getRSPackageName() {
149 return mRSPackageName;
150 }
151
Stephen Hinesc808a992010-11-29 17:20:42 -0800152 bool processExport();
Zonr Changa41ce1d2010-10-06 02:23:12 +0800153 inline void newExportable(RSExportable *E) {
154 if (E != NULL)
155 mExportables.push_back(E);
156 }
Zonr Chang641558f2010-10-12 21:07:06 +0800157 typedef ExportableList::iterator exportable_iterator;
158 exportable_iterator exportable_begin() {
159 return mExportables.begin();
160 }
161 exportable_iterator exportable_end() {
162 return mExportables.end();
163 }
Shih-wei Liao537446c2010-06-11 16:05:55 -0700164
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700165 typedef ExportVarList::const_iterator const_export_var_iterator;
166 const_export_var_iterator export_vars_begin() const {
167 return mExportVars.begin();
168 }
169 const_export_var_iterator export_vars_end() const {
170 return mExportVars.end();
171 }
172 inline bool hasExportVar() const {
173 return !mExportVars.empty();
174 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700175
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700176 typedef ExportFuncList::const_iterator const_export_func_iterator;
177 const_export_func_iterator export_funcs_begin() const {
178 return mExportFuncs.begin();
179 }
180 const_export_func_iterator export_funcs_end() const {
181 return mExportFuncs.end();
182 }
183 inline bool hasExportFunc() const { return !mExportFuncs.empty(); }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700184
Stephen Hines593a8942011-05-10 15:29:50 -0700185 typedef ExportForEachList::const_iterator const_export_foreach_iterator;
186 const_export_foreach_iterator export_foreach_begin() const {
187 return mExportForEach.begin();
188 }
189 const_export_foreach_iterator export_foreach_end() const {
190 return mExportForEach.end();
191 }
192 inline bool hasExportForEach() const { return !mExportForEach.empty(); }
193
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700194 typedef ExportTypeMap::iterator export_type_iterator;
195 typedef ExportTypeMap::const_iterator const_export_type_iterator;
196 export_type_iterator export_types_begin() { return mExportTypes.begin(); }
197 export_type_iterator export_types_end() { return mExportTypes.end(); }
198 const_export_type_iterator export_types_begin() const {
199 return mExportTypes.begin();
200 }
201 const_export_type_iterator export_types_end() const {
202 return mExportTypes.end();
203 }
204 inline bool hasExportType() const { return !mExportTypes.empty(); }
205 export_type_iterator findExportType(const llvm::StringRef &TypeName) {
206 return mExportTypes.find(TypeName);
207 }
208 const_export_type_iterator findExportType(const llvm::StringRef &TypeName)
209 const {
210 return mExportTypes.find(TypeName);
211 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700212
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700213 // Insert the specified Typename/Type pair into the map. If the key already
214 // exists in the map, return false and ignore the request, otherwise insert it
215 // and return true.
216 bool insertExportType(const llvm::StringRef &TypeName, RSExportType *Type);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700217
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700218 bool reflectToJava(const std::string &OutputPathBase,
219 const std::string &OutputPackageName,
Stephen Hines0a813a32012-08-03 16:52:40 -0700220 const std::string &RSPackageName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700221 const std::string &InputFileName,
222 const std::string &OutputBCFileName,
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700223 std::string *RealPackageName);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700224
Stephen Hines96ab06c2011-01-05 15:29:26 -0800225 int getVersion() const { return version; }
226 void setVersion(int v) {
227 version = v;
228 return;
229 }
230
Stephen Hines3fd0a942011-01-18 12:27:39 -0800231 void addPragma(const std::string &T, const std::string &V) {
232 mPragmas->push_back(make_pair(T, V));
233 }
234
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700235 ~RSContext();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700236};
237
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700238} // namespace slang
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700239
Stephen Hinese639eb52010-11-08 19:27:20 -0800240#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_CONTEXT_H_ NOLINT