blob: 6722a57c5470c4ef46181d1d21a6dc2ff38d0c85 [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;
Stephen Hines23c43582013-01-09 20:02:04 -080036 class DataLayout;
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
Stephen Hines23c43582013-01-09 20:02:04 -080074 llvm::DataLayout *mDataLayout;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070075 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;
Stephen Hines82754d82013-01-18 19:46:06 -080088
89 bool mIsCompatLib;
90
Loganbe274822011-02-16 22:02:54 +080091 llvm::OwningPtr<clang::MangleContext> mMangleCtx;
Stephen Hines96ab06c2011-01-05 15:29:26 -080092
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070093 bool processExportVar(const clang::VarDecl *VD);
94 bool processExportFunc(const clang::FunctionDecl *FD);
95 bool processExportType(const llvm::StringRef &Name);
Shih-wei Liao537446c2010-06-11 16:05:55 -070096
Stephen Hinesc17e1982012-02-22 12:30:45 -080097 void cleanupForEach();
98
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070099 ExportVarList mExportVars;
100 ExportFuncList mExportFuncs;
Stephen Hines593a8942011-05-10 15:29:50 -0700101 ExportForEachList mExportForEach;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700102 ExportTypeMap mExportTypes;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700103
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700104 public:
Stephen Hines9e5b5032010-11-03 13:19:14 -0700105 RSContext(clang::Preprocessor &PP,
106 clang::ASTContext &Ctx,
Stephen Hines3fd0a942011-01-18 12:27:39 -0800107 const clang::TargetInfo &Target,
Stephen Hines4cc67fc2011-01-31 16:48:57 -0800108 PragmaList *Pragmas,
Stephen Hines4a4bf922011-08-18 17:20:33 -0700109 unsigned int TargetAPI,
Stephen Hines4cc67fc2011-01-31 16:48:57 -0800110 std::vector<std::string> *GeneratedFileNames);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700111
Stephen Hines9e5b5032010-11-03 13:19:14 -0700112 inline clang::Preprocessor &getPreprocessor() const { return mPP; }
113 inline clang::ASTContext &getASTContext() const { return mCtx; }
Loganbe274822011-02-16 22:02:54 +0800114 inline clang::MangleContext &getMangleContext() const {
115 return *mMangleCtx;
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800116 }
Stephen Hines23c43582013-01-09 20:02:04 -0800117 inline const llvm::DataLayout *getDataLayout() const { return mDataLayout; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700118 inline llvm::LLVMContext &getLLVMContext() const { return mLLVMContext; }
119 inline const clang::SourceManager *getSourceManager() const {
Stephen Hines9e5b5032010-11-03 13:19:14 -0700120 return &mPP.getSourceManager();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700121 }
Logan Chien9207a2e2011-10-21 15:39:28 +0800122 inline clang::DiagnosticsEngine *getDiagnostics() const {
Stephen Hines2ef9bc02010-12-13 18:33:23 -0800123 return &mPP.getDiagnostics();
124 }
Stephen Hines4a4bf922011-08-18 17:20:33 -0700125 inline unsigned int getTargetAPI() const {
126 return mTargetAPI;
127 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700128
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700129 inline void setLicenseNote(const std::string &S) {
130 mLicenseNote = new std::string(S);
131 }
132 inline const std::string *getLicenseNote() const { return mLicenseNote; }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700133
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700134 inline void addExportType(const std::string &S) {
135 mNeedExportTypes.insert(S);
136 return;
137 }
Victor Hsiehd8a0d182010-07-07 19:22:33 +0800138
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700139 inline void setReflectJavaPackageName(const std::string &S) {
140 mReflectJavaPackageName = S;
141 return;
142 }
Tim Murraydde98532013-07-23 15:55:19 -0700143 inline const std::string &getReflectJavaPackageName() const {
Stephen Hines4cc67fc2011-01-31 16:48:57 -0800144 return mReflectJavaPackageName;
145 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700146
Stephen Hines0a813a32012-08-03 16:52:40 -0700147 inline void setRSPackageName(const std::string &S) {
148 mRSPackageName = S;
149 return;
150 }
Tim Murraydde98532013-07-23 15:55:19 -0700151 inline const std::string &getRSPackageName() const {
Stephen Hines0a813a32012-08-03 16:52:40 -0700152 return mRSPackageName;
153 }
154
Stephen Hinesc808a992010-11-29 17:20:42 -0800155 bool processExport();
Zonr Changa41ce1d2010-10-06 02:23:12 +0800156 inline void newExportable(RSExportable *E) {
157 if (E != NULL)
158 mExportables.push_back(E);
159 }
Zonr Chang641558f2010-10-12 21:07:06 +0800160 typedef ExportableList::iterator exportable_iterator;
161 exportable_iterator exportable_begin() {
162 return mExportables.begin();
163 }
164 exportable_iterator exportable_end() {
165 return mExportables.end();
166 }
Shih-wei Liao537446c2010-06-11 16:05:55 -0700167
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700168 typedef ExportVarList::const_iterator const_export_var_iterator;
169 const_export_var_iterator export_vars_begin() const {
170 return mExportVars.begin();
171 }
172 const_export_var_iterator export_vars_end() const {
173 return mExportVars.end();
174 }
175 inline bool hasExportVar() const {
176 return !mExportVars.empty();
177 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700178
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700179 typedef ExportFuncList::const_iterator const_export_func_iterator;
180 const_export_func_iterator export_funcs_begin() const {
181 return mExportFuncs.begin();
182 }
183 const_export_func_iterator export_funcs_end() const {
184 return mExportFuncs.end();
185 }
186 inline bool hasExportFunc() const { return !mExportFuncs.empty(); }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700187
Stephen Hines593a8942011-05-10 15:29:50 -0700188 typedef ExportForEachList::const_iterator const_export_foreach_iterator;
189 const_export_foreach_iterator export_foreach_begin() const {
190 return mExportForEach.begin();
191 }
192 const_export_foreach_iterator export_foreach_end() const {
193 return mExportForEach.end();
194 }
195 inline bool hasExportForEach() const { return !mExportForEach.empty(); }
196
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700197 typedef ExportTypeMap::iterator export_type_iterator;
198 typedef ExportTypeMap::const_iterator const_export_type_iterator;
199 export_type_iterator export_types_begin() { return mExportTypes.begin(); }
200 export_type_iterator export_types_end() { return mExportTypes.end(); }
201 const_export_type_iterator export_types_begin() const {
202 return mExportTypes.begin();
203 }
204 const_export_type_iterator export_types_end() const {
205 return mExportTypes.end();
206 }
207 inline bool hasExportType() const { return !mExportTypes.empty(); }
208 export_type_iterator findExportType(const llvm::StringRef &TypeName) {
209 return mExportTypes.find(TypeName);
210 }
211 const_export_type_iterator findExportType(const llvm::StringRef &TypeName)
212 const {
213 return mExportTypes.find(TypeName);
214 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700215
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700216 // Insert the specified Typename/Type pair into the map. If the key already
217 // exists in the map, return false and ignore the request, otherwise insert it
218 // and return true.
219 bool insertExportType(const llvm::StringRef &TypeName, RSExportType *Type);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700220
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700221 bool reflectToJava(const std::string &OutputPathBase,
Stephen Hines0a813a32012-08-03 16:52:40 -0700222 const std::string &RSPackageName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700223 const std::string &InputFileName,
Stephen Hines925879f2013-07-19 18:19:04 -0700224 const std::string &OutputBCFileName);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700225
Stephen Hines96ab06c2011-01-05 15:29:26 -0800226 int getVersion() const { return version; }
227 void setVersion(int v) {
228 version = v;
229 return;
230 }
231
Stephen Hines82754d82013-01-18 19:46:06 -0800232 bool isCompatLib() const { return mIsCompatLib; }
233
Stephen Hines3fd0a942011-01-18 12:27:39 -0800234 void addPragma(const std::string &T, const std::string &V) {
235 mPragmas->push_back(make_pair(T, V));
236 }
237
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800238 // Report an error or a warning to the user.
239 clang::DiagnosticBuilder Report(clang::DiagnosticsEngine::Level Level,
240 const char *Message);
241 clang::DiagnosticBuilder Report(clang::DiagnosticsEngine::Level Level,
242 const clang::SourceLocation Loc,
243 const char *Message);
244
245 // Utility functions to report errors and warnings to make the calling code
246 // easier to read.
247 clang::DiagnosticBuilder ReportError(const char *Message) {
248 return Report(clang::DiagnosticsEngine::Error, Message);
249 }
250 clang::DiagnosticBuilder ReportError(const clang::SourceLocation Loc,
251 const char *Message) {
252 return Report(clang::DiagnosticsEngine::Error, Loc, Message);
253 }
254 clang::DiagnosticBuilder ReportWarning(const char *Message) {
255 return Report(clang::DiagnosticsEngine::Warning, Message);
256 }
257 clang::DiagnosticBuilder ReportWarning(const clang::SourceLocation Loc,
258 const char *Message) {
259 return Report(clang::DiagnosticsEngine::Warning, Loc, Message);
260 }
261
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700262 ~RSContext();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700263};
264
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700265} // namespace slang
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700266
Stephen Hinese639eb52010-11-08 19:27:20 -0800267#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_CONTEXT_H_ NOLINT