blob: c47f4c1301ed2f964634bbbfd2c261dda3136d82 [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
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070028#include "llvm/ADT/StringSet.h"
29#include "llvm/ADT/StringMap.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070030
Stephen Hines3fd0a942011-01-18 12:27:39 -080031#include "slang_pragma_recorder.h"
32
Shih-wei Liao462aefd2010-06-04 15:32:04 -070033namespace llvm {
zonr6315f762010-10-05 15:35:14 +080034 class LLVMContext;
Stephen Hines23c43582013-01-09 20:02:04 -080035 class DataLayout;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070036} // namespace llvm
Shih-wei Liao462aefd2010-06-04 15:32:04 -070037
38namespace clang {
zonr6315f762010-10-05 15:35:14 +080039 class VarDecl;
40 class ASTContext;
41 class TargetInfo;
42 class FunctionDecl;
43 class SourceManager;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070044} // namespace clang
Shih-wei Liao462aefd2010-06-04 15:32:04 -070045
46namespace slang {
Zonr Changa41ce1d2010-10-06 02:23:12 +080047 class RSExportable;
48 class RSExportVar;
49 class RSExportFunc;
Stephen Hines593a8942011-05-10 15:29:50 -070050 class RSExportForEach;
Zonr Changa41ce1d2010-10-06 02:23:12 +080051 class RSExportType;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070052
53class RSContext {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070054 typedef llvm::StringSet<> NeedExportVarSet;
55 typedef llvm::StringSet<> NeedExportFuncSet;
56 typedef llvm::StringSet<> NeedExportTypeSet;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070057
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070058 public:
Zonr Changa41ce1d2010-10-06 02:23:12 +080059 typedef std::list<RSExportable*> ExportableList;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070060 typedef std::list<RSExportVar*> ExportVarList;
61 typedef std::list<RSExportFunc*> ExportFuncList;
Stephen Hines593a8942011-05-10 15:29:50 -070062 typedef std::list<RSExportForEach*> ExportForEachList;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070063 typedef llvm::StringMap<RSExportType*> ExportTypeMap;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070064
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070065 private:
Stephen Hines9e5b5032010-11-03 13:19:14 -070066 clang::Preprocessor &mPP;
67 clang::ASTContext &mCtx;
Stephen Hines3fd0a942011-01-18 12:27:39 -080068 PragmaList *mPragmas;
Jean-Luc Brouillet5210d102014-07-07 17:36:07 -070069 // Precision specified via pragma, either rs_fp_full or rs_fp_relaxed. If
70 // empty, rs_fp_full is assumed.
71 std::string mPrecision;
Chris Wailesc9454af2014-06-13 17:25:40 -070072 unsigned int mTargetAPI;
Stephen Hinesfc4f78b2014-06-10 18:07:10 -070073 bool mVerbose;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070074
Stephen Hines23c43582013-01-09 20:02:04 -080075 llvm::DataLayout *mDataLayout;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070076 llvm::LLVMContext &mLLVMContext;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070077
Zonr Changa41ce1d2010-10-06 02:23:12 +080078 ExportableList mExportables;
79
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070080 NeedExportTypeSet mNeedExportTypes;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070081
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070082 std::string *mLicenseNote;
83 std::string mReflectJavaPackageName;
84 std::string mReflectJavaPathName;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070085
Stephen Hines0a813a32012-08-03 16:52:40 -070086 std::string mRSPackageName;
87
Stephen Hines96ab06c2011-01-05 15:29:26 -080088 int version;
Stephen Hines82754d82013-01-18 19:46:06 -080089
Stephen Hines7ac9d0d2014-07-15 16:50:03 -070090 std::unique_ptr<clang::MangleContext> mMangleCtx;
Stephen Hines96ab06c2011-01-05 15:29:26 -080091
Stephen Hines9ae18b22014-06-10 23:53:00 -070092 bool mIs64Bit;
93
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070094 bool processExportVar(const clang::VarDecl *VD);
95 bool processExportFunc(const clang::FunctionDecl *FD);
96 bool processExportType(const llvm::StringRef &Name);
Shih-wei Liao537446c2010-06-11 16:05:55 -070097
Stephen Hinesc17e1982012-02-22 12:30:45 -080098 void cleanupForEach();
99
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700100 ExportVarList mExportVars;
101 ExportFuncList mExportFuncs;
Stephen Hines593a8942011-05-10 15:29:50 -0700102 ExportForEachList mExportForEach;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700103 ExportTypeMap mExportTypes;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700104
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700105 public:
Stephen Hines9e5b5032010-11-03 13:19:14 -0700106 RSContext(clang::Preprocessor &PP,
107 clang::ASTContext &Ctx,
Stephen Hines3fd0a942011-01-18 12:27:39 -0800108 const clang::TargetInfo &Target,
Stephen Hines4cc67fc2011-01-31 16:48:57 -0800109 PragmaList *Pragmas,
Stephen Hinesfc4f78b2014-06-10 18:07:10 -0700110 unsigned int TargetAPI,
111 bool Verbose);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700112
Stephen Hines9e5b5032010-11-03 13:19:14 -0700113 inline clang::Preprocessor &getPreprocessor() const { return mPP; }
114 inline clang::ASTContext &getASTContext() const { return mCtx; }
Loganbe274822011-02-16 22:02:54 +0800115 inline clang::MangleContext &getMangleContext() const {
116 return *mMangleCtx;
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800117 }
Stephen Hines23c43582013-01-09 20:02:04 -0800118 inline const llvm::DataLayout *getDataLayout() const { return mDataLayout; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700119 inline llvm::LLVMContext &getLLVMContext() const { return mLLVMContext; }
120 inline const clang::SourceManager *getSourceManager() const {
Stephen Hines9e5b5032010-11-03 13:19:14 -0700121 return &mPP.getSourceManager();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700122 }
Logan Chien9207a2e2011-10-21 15:39:28 +0800123 inline clang::DiagnosticsEngine *getDiagnostics() const {
Stephen Hines2ef9bc02010-12-13 18:33:23 -0800124 return &mPP.getDiagnostics();
125 }
Chris Wailesc9454af2014-06-13 17:25:40 -0700126 inline unsigned int getTargetAPI() const {
Stephen Hines4a4bf922011-08-18 17:20:33 -0700127 return mTargetAPI;
128 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700129
Stephen Hinesfc4f78b2014-06-10 18:07:10 -0700130 inline bool getVerbose() const {
131 return mVerbose;
132 }
Stephen Hines9ae18b22014-06-10 23:53:00 -0700133 inline bool is64Bit() const {
134 return mIs64Bit;
135 }
Stephen Hinesfc4f78b2014-06-10 18:07:10 -0700136
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700137 inline void setLicenseNote(const std::string &S) {
138 mLicenseNote = new std::string(S);
139 }
140 inline const std::string *getLicenseNote() const { return mLicenseNote; }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700141
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700142 inline void addExportType(const std::string &S) {
143 mNeedExportTypes.insert(S);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700144 }
Victor Hsiehd8a0d182010-07-07 19:22:33 +0800145
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700146 inline void setReflectJavaPackageName(const std::string &S) {
147 mReflectJavaPackageName = S;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700148 }
Tim Murraydde98532013-07-23 15:55:19 -0700149 inline const std::string &getReflectJavaPackageName() const {
Stephen Hines4cc67fc2011-01-31 16:48:57 -0800150 return mReflectJavaPackageName;
151 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700152
Stephen Hines0a813a32012-08-03 16:52:40 -0700153 inline void setRSPackageName(const std::string &S) {
154 mRSPackageName = S;
Stephen Hines0a813a32012-08-03 16:52:40 -0700155 }
Jean-Luc Brouillet12fc2832014-06-04 21:32:31 +0000156
157 inline const std::string &getRSPackageName() const { return mRSPackageName; }
Stephen Hines0a813a32012-08-03 16:52:40 -0700158
Stephen Hinesc808a992010-11-29 17:20:42 -0800159 bool processExport();
Zonr Changa41ce1d2010-10-06 02:23:12 +0800160 inline void newExportable(RSExportable *E) {
161 if (E != NULL)
162 mExportables.push_back(E);
163 }
Zonr Chang641558f2010-10-12 21:07:06 +0800164 typedef ExportableList::iterator exportable_iterator;
165 exportable_iterator exportable_begin() {
166 return mExportables.begin();
167 }
168 exportable_iterator exportable_end() {
169 return mExportables.end();
170 }
Shih-wei Liao537446c2010-06-11 16:05:55 -0700171
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700172 typedef ExportVarList::const_iterator const_export_var_iterator;
173 const_export_var_iterator export_vars_begin() const {
174 return mExportVars.begin();
175 }
176 const_export_var_iterator export_vars_end() const {
177 return mExportVars.end();
178 }
179 inline bool hasExportVar() const {
180 return !mExportVars.empty();
181 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700182
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700183 typedef ExportFuncList::const_iterator const_export_func_iterator;
184 const_export_func_iterator export_funcs_begin() const {
185 return mExportFuncs.begin();
186 }
187 const_export_func_iterator export_funcs_end() const {
188 return mExportFuncs.end();
189 }
190 inline bool hasExportFunc() const { return !mExportFuncs.empty(); }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700191
Stephen Hines593a8942011-05-10 15:29:50 -0700192 typedef ExportForEachList::const_iterator const_export_foreach_iterator;
193 const_export_foreach_iterator export_foreach_begin() const {
194 return mExportForEach.begin();
195 }
196 const_export_foreach_iterator export_foreach_end() const {
197 return mExportForEach.end();
198 }
199 inline bool hasExportForEach() const { return !mExportForEach.empty(); }
200
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700201 typedef ExportTypeMap::iterator export_type_iterator;
202 typedef ExportTypeMap::const_iterator const_export_type_iterator;
203 export_type_iterator export_types_begin() { return mExportTypes.begin(); }
204 export_type_iterator export_types_end() { return mExportTypes.end(); }
205 const_export_type_iterator export_types_begin() const {
206 return mExportTypes.begin();
207 }
208 const_export_type_iterator export_types_end() const {
209 return mExportTypes.end();
210 }
211 inline bool hasExportType() const { return !mExportTypes.empty(); }
212 export_type_iterator findExportType(const llvm::StringRef &TypeName) {
213 return mExportTypes.find(TypeName);
214 }
215 const_export_type_iterator findExportType(const llvm::StringRef &TypeName)
216 const {
217 return mExportTypes.find(TypeName);
218 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700219
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700220 // Insert the specified Typename/Type pair into the map. If the key already
221 // exists in the map, return false and ignore the request, otherwise insert it
222 // and return true.
223 bool insertExportType(const llvm::StringRef &TypeName, RSExportType *Type);
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;
Stephen Hines96ab06c2011-01-05 15:29:26 -0800228 }
229
Jean-Luc Brouillet12fc2832014-06-04 21:32:31 +0000230 bool isCompatLib() const {
231 // If we are not targeting the actual Android Renderscript classes,
232 // we should reflect code that works with the compatibility library.
233 return (mRSPackageName.compare("android.renderscript") != 0);
234 }
Stephen Hines82754d82013-01-18 19:46:06 -0800235
Stephen Hines3fd0a942011-01-18 12:27:39 -0800236 void addPragma(const std::string &T, const std::string &V) {
237 mPragmas->push_back(make_pair(T, V));
238 }
Jean-Luc Brouillet5210d102014-07-07 17:36:07 -0700239 void setPrecision(const std::string &P) { mPrecision = P; }
240 std::string getPrecision() { return mPrecision; }
Stephen Hines3fd0a942011-01-18 12:27:39 -0800241
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800242 // Report an error or a warning to the user.
Tim Murrayee4016d2014-04-10 15:49:08 -0700243 template <unsigned N>
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800244 clang::DiagnosticBuilder Report(clang::DiagnosticsEngine::Level Level,
Tim Murrayee4016d2014-04-10 15:49:08 -0700245 const char (&Message)[N]) {
246 clang::DiagnosticsEngine *DiagEngine = getDiagnostics();
247 return DiagEngine->Report(DiagEngine->getCustomDiagID(Level, Message));
248}
249
250 template <unsigned N>
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800251 clang::DiagnosticBuilder Report(clang::DiagnosticsEngine::Level Level,
Tim Murrayee4016d2014-04-10 15:49:08 -0700252 const clang::SourceLocation Loc,
253 const char (&Message)[N]) {
254 clang::DiagnosticsEngine *DiagEngine = getDiagnostics();
255 const clang::SourceManager *SM = getSourceManager();
256 return DiagEngine->Report(clang::FullSourceLoc(Loc, *SM),
257 DiagEngine->getCustomDiagID(Level, Message));
258}
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800259
260 // Utility functions to report errors and warnings to make the calling code
261 // easier to read.
Tim Murrayee4016d2014-04-10 15:49:08 -0700262 template <unsigned N>
263 clang::DiagnosticBuilder ReportError(const char (&Message)[N]) {
264 return Report<N>(clang::DiagnosticsEngine::Error, Message);
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800265 }
Tim Murrayee4016d2014-04-10 15:49:08 -0700266
267 template <unsigned N>
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800268 clang::DiagnosticBuilder ReportError(const clang::SourceLocation Loc,
Tim Murrayee4016d2014-04-10 15:49:08 -0700269 const char (&Message)[N]) {
270 return Report<N>(clang::DiagnosticsEngine::Error, Loc, Message);
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800271 }
Tim Murrayee4016d2014-04-10 15:49:08 -0700272
273 template <unsigned N>
274 clang::DiagnosticBuilder ReportWarning(const char (&Message)[N]) {
275 return Report<N>(clang::DiagnosticsEngine::Warning, Message);
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800276 }
Tim Murrayee4016d2014-04-10 15:49:08 -0700277
278 template <unsigned N>
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800279 clang::DiagnosticBuilder ReportWarning(const clang::SourceLocation Loc,
Tim Murrayee4016d2014-04-10 15:49:08 -0700280 const char (&Message)[N]) {
281 return Report<N>(clang::DiagnosticsEngine::Warning, Loc, Message);
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800282 }
283
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700284 ~RSContext();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700285};
286
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700287} // namespace slang
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700288
Stephen Hinese639eb52010-11-08 19:27:20 -0800289#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_CONTEXT_H_ NOLINT