blob: c9da497411fdefbbd7dfdc3276c60821f2864747 [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>
Yang Nifb40ee22015-10-13 20:34:06 +000024#include <vector>
Stephen Hinese639eb52010-11-08 19:27:20 -080025
26#include "clang/Lex/Preprocessor.h"
Loganbe274822011-02-16 22:02:54 +080027#include "clang/AST/Mangle.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070028
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
David Gross34e62052015-11-05 09:55:03 -080032#include "slang_pragma_list.h"
Stephen Hines3fd0a942011-01-18 12:27:39 -080033
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;
Yang Nifb40ee22015-10-13 20:34:06 +000044 class QualType;
zonr6315f762010-10-05 15:35:14 +080045 class SourceManager;
Yang Nifb40ee22015-10-13 20:34:06 +000046 class TypeDecl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070047} // namespace clang
Shih-wei Liao462aefd2010-06-04 15:32:04 -070048
49namespace slang {
Zonr Changa41ce1d2010-10-06 02:23:12 +080050 class RSExportable;
51 class RSExportVar;
52 class RSExportFunc;
Stephen Hines593a8942011-05-10 15:29:50 -070053 class RSExportForEach;
Matt Walac0c5dd82015-07-23 17:29:37 -070054 class RSExportReduce;
David Gross15e44e62015-11-10 11:12:48 -080055 class RSExportReduceNew;
Zonr Changa41ce1d2010-10-06 02:23:12 +080056 class RSExportType;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070057
58class RSContext {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070059 typedef llvm::StringSet<> NeedExportVarSet;
60 typedef llvm::StringSet<> NeedExportFuncSet;
61 typedef llvm::StringSet<> NeedExportTypeSet;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070062
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070063 public:
Zonr Changa41ce1d2010-10-06 02:23:12 +080064 typedef std::list<RSExportable*> ExportableList;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070065 typedef std::list<RSExportVar*> ExportVarList;
66 typedef std::list<RSExportFunc*> ExportFuncList;
Yang Nifb40ee22015-10-13 20:34:06 +000067 typedef std::vector<RSExportForEach*> ExportForEachVector;
Matt Walac0c5dd82015-07-23 17:29:37 -070068 typedef std::list<RSExportReduce*> ExportReduceList;
David Gross15e44e62015-11-10 11:12:48 -080069 typedef std::list<RSExportReduceNew*> ExportReduceNewList;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070070 typedef llvm::StringMap<RSExportType*> ExportTypeMap;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070071
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070072 private:
Stephen Hines9e5b5032010-11-03 13:19:14 -070073 clang::Preprocessor &mPP;
74 clang::ASTContext &mCtx;
Stephen Hines3fd0a942011-01-18 12:27:39 -080075 PragmaList *mPragmas;
Jean-Luc Brouillet109e90a2014-07-07 17:36:07 -070076 // Precision specified via pragma, either rs_fp_full or rs_fp_relaxed. If
77 // empty, rs_fp_full is assumed.
78 std::string mPrecision;
Chris Wailesc9454af2014-06-13 17:25:40 -070079 unsigned int mTargetAPI;
Stephen Hinesfc4f78b2014-06-10 18:07:10 -070080 bool mVerbose;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070081
Stephen Hines23c43582013-01-09 20:02:04 -080082 llvm::DataLayout *mDataLayout;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070083 llvm::LLVMContext &mLLVMContext;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070084
Zonr Changa41ce1d2010-10-06 02:23:12 +080085 ExportableList mExportables;
86
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070087 NeedExportTypeSet mNeedExportTypes;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070088
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070089 std::string *mLicenseNote;
90 std::string mReflectJavaPackageName;
91 std::string mReflectJavaPathName;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070092
Stephen Hines0a813a32012-08-03 16:52:40 -070093 std::string mRSPackageName;
94
Stephen Hines96ab06c2011-01-05 15:29:26 -080095 int version;
Stephen Hines82754d82013-01-18 19:46:06 -080096
Stephen Hines2eb9a3f2014-07-15 16:50:03 -070097 std::unique_ptr<clang::MangleContext> mMangleCtx;
Stephen Hines96ab06c2011-01-05 15:29:26 -080098
Stephen Hines9ae18b22014-06-10 23:53:00 -070099 bool mIs64Bit;
100
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700101 bool processExportVar(const clang::VarDecl *VD);
102 bool processExportFunc(const clang::FunctionDecl *FD);
103 bool processExportType(const llvm::StringRef &Name);
Shih-wei Liao537446c2010-06-11 16:05:55 -0700104
Yang Nifb40ee22015-10-13 20:34:06 +0000105 int getForEachSlotNumber(const clang::StringRef& funcName);
106 unsigned mNextSlot;
Stephen Hinesc17e1982012-02-22 12:30:45 -0800107
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700108 ExportVarList mExportVars;
109 ExportFuncList mExportFuncs;
Yang Nifb40ee22015-10-13 20:34:06 +0000110 std::map<llvm::StringRef, unsigned> mExportForEachMap;
111 ExportForEachVector mExportForEach;
Matt Walac0c5dd82015-07-23 17:29:37 -0700112 ExportReduceList mExportReduce;
David Gross15e44e62015-11-10 11:12:48 -0800113 ExportReduceNewList mExportReduceNew;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700114 ExportTypeMap mExportTypes;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700115
Yang Nifb40ee22015-10-13 20:34:06 +0000116 clang::QualType mAllocationType;
117
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700118 public:
Stephen Hines9e5b5032010-11-03 13:19:14 -0700119 RSContext(clang::Preprocessor &PP,
120 clang::ASTContext &Ctx,
Stephen Hines3fd0a942011-01-18 12:27:39 -0800121 const clang::TargetInfo &Target,
Stephen Hines4cc67fc2011-01-31 16:48:57 -0800122 PragmaList *Pragmas,
Stephen Hinesfc4f78b2014-06-10 18:07:10 -0700123 unsigned int TargetAPI,
124 bool Verbose);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700125
Stephen Hines9e5b5032010-11-03 13:19:14 -0700126 inline clang::Preprocessor &getPreprocessor() const { return mPP; }
127 inline clang::ASTContext &getASTContext() const { return mCtx; }
Loganbe274822011-02-16 22:02:54 +0800128 inline clang::MangleContext &getMangleContext() const {
129 return *mMangleCtx;
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800130 }
Stephen Hines23c43582013-01-09 20:02:04 -0800131 inline const llvm::DataLayout *getDataLayout() const { return mDataLayout; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700132 inline llvm::LLVMContext &getLLVMContext() const { return mLLVMContext; }
133 inline const clang::SourceManager *getSourceManager() const {
Stephen Hines9e5b5032010-11-03 13:19:14 -0700134 return &mPP.getSourceManager();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700135 }
Logan Chien9207a2e2011-10-21 15:39:28 +0800136 inline clang::DiagnosticsEngine *getDiagnostics() const {
Stephen Hines2ef9bc02010-12-13 18:33:23 -0800137 return &mPP.getDiagnostics();
138 }
Chris Wailesc9454af2014-06-13 17:25:40 -0700139 inline unsigned int getTargetAPI() const {
Stephen Hines4a4bf922011-08-18 17:20:33 -0700140 return mTargetAPI;
141 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700142
Stephen Hinesfc4f78b2014-06-10 18:07:10 -0700143 inline bool getVerbose() const {
144 return mVerbose;
145 }
Stephen Hines9ae18b22014-06-10 23:53:00 -0700146 inline bool is64Bit() const {
147 return mIs64Bit;
148 }
Stephen Hinesfc4f78b2014-06-10 18:07:10 -0700149
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700150 inline void setLicenseNote(const std::string &S) {
151 mLicenseNote = new std::string(S);
152 }
153 inline const std::string *getLicenseNote() const { return mLicenseNote; }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700154
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700155 inline void addExportType(const std::string &S) {
156 mNeedExportTypes.insert(S);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700157 }
Victor Hsiehd8a0d182010-07-07 19:22:33 +0800158
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700159 inline void setReflectJavaPackageName(const std::string &S) {
160 mReflectJavaPackageName = S;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700161 }
Tim Murraydde98532013-07-23 15:55:19 -0700162 inline const std::string &getReflectJavaPackageName() const {
Stephen Hines4cc67fc2011-01-31 16:48:57 -0800163 return mReflectJavaPackageName;
164 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700165
Stephen Hines0a813a32012-08-03 16:52:40 -0700166 inline void setRSPackageName(const std::string &S) {
167 mRSPackageName = S;
Stephen Hines0a813a32012-08-03 16:52:40 -0700168 }
Jean-Luc Brouillet12fc2832014-06-04 21:32:31 +0000169
170 inline const std::string &getRSPackageName() const { return mRSPackageName; }
Stephen Hines0a813a32012-08-03 16:52:40 -0700171
Yang Ni88f21e12015-11-18 15:59:00 -0800172 void setAllocationType(const clang::TypeDecl* TD);
173 inline const clang::QualType& getAllocationType() const {
174 return mAllocationType;
175 }
176
Yang Nifb40ee22015-10-13 20:34:06 +0000177 bool addForEach(const clang::FunctionDecl* FD);
178 bool processExports();
Zonr Changa41ce1d2010-10-06 02:23:12 +0800179 inline void newExportable(RSExportable *E) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700180 if (E != nullptr)
Zonr Changa41ce1d2010-10-06 02:23:12 +0800181 mExportables.push_back(E);
182 }
Zonr Chang641558f2010-10-12 21:07:06 +0800183 typedef ExportableList::iterator exportable_iterator;
184 exportable_iterator exportable_begin() {
185 return mExportables.begin();
186 }
187 exportable_iterator exportable_end() {
188 return mExportables.end();
189 }
Shih-wei Liao537446c2010-06-11 16:05:55 -0700190
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700191 typedef ExportVarList::const_iterator const_export_var_iterator;
192 const_export_var_iterator export_vars_begin() const {
193 return mExportVars.begin();
194 }
195 const_export_var_iterator export_vars_end() const {
196 return mExportVars.end();
197 }
198 inline bool hasExportVar() const {
199 return !mExportVars.empty();
200 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700201
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700202 typedef ExportFuncList::const_iterator const_export_func_iterator;
203 const_export_func_iterator export_funcs_begin() const {
204 return mExportFuncs.begin();
205 }
206 const_export_func_iterator export_funcs_end() const {
207 return mExportFuncs.end();
208 }
209 inline bool hasExportFunc() const { return !mExportFuncs.empty(); }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700210
Yang Nifb40ee22015-10-13 20:34:06 +0000211 typedef ExportForEachVector::const_iterator const_export_foreach_iterator;
Stephen Hines593a8942011-05-10 15:29:50 -0700212 const_export_foreach_iterator export_foreach_begin() const {
213 return mExportForEach.begin();
214 }
215 const_export_foreach_iterator export_foreach_end() const {
216 return mExportForEach.end();
217 }
218 inline bool hasExportForEach() const { return !mExportForEach.empty(); }
Yang Nifb40ee22015-10-13 20:34:06 +0000219 int getForEachSlotNumber(const clang::FunctionDecl* FD);
Stephen Hines593a8942011-05-10 15:29:50 -0700220
Matt Walac0c5dd82015-07-23 17:29:37 -0700221 typedef ExportReduceList::const_iterator const_export_reduce_iterator;
222 const_export_reduce_iterator export_reduce_begin() const {
223 return mExportReduce.begin();
224 }
225 const_export_reduce_iterator export_reduce_end() const {
226 return mExportReduce.end();
227 }
228 inline bool hasExportReduce() const { return !mExportReduce.empty(); }
229
David Gross15e44e62015-11-10 11:12:48 -0800230 typedef ExportReduceNewList::const_iterator const_export_reduce_new_iterator;
231 const_export_reduce_new_iterator export_reduce_new_begin() const {
232 return mExportReduceNew.begin();
233 }
234 const_export_reduce_new_iterator export_reduce_new_end() const {
235 return mExportReduceNew.end();
236 }
237 inline bool hasExportReduceNew() const { return !mExportReduceNew.empty(); }
238 void addExportReduceNew(RSExportReduceNew *ReduceNew) {
239 mExportReduceNew.push_back(ReduceNew);
240 }
241
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700242 typedef ExportTypeMap::iterator export_type_iterator;
243 typedef ExportTypeMap::const_iterator const_export_type_iterator;
244 export_type_iterator export_types_begin() { return mExportTypes.begin(); }
245 export_type_iterator export_types_end() { return mExportTypes.end(); }
246 const_export_type_iterator export_types_begin() const {
247 return mExportTypes.begin();
248 }
249 const_export_type_iterator export_types_end() const {
250 return mExportTypes.end();
251 }
252 inline bool hasExportType() const { return !mExportTypes.empty(); }
253 export_type_iterator findExportType(const llvm::StringRef &TypeName) {
254 return mExportTypes.find(TypeName);
255 }
256 const_export_type_iterator findExportType(const llvm::StringRef &TypeName)
257 const {
258 return mExportTypes.find(TypeName);
259 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700260
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700261 // Insert the specified Typename/Type pair into the map. If the key already
262 // exists in the map, return false and ignore the request, otherwise insert it
263 // and return true.
264 bool insertExportType(const llvm::StringRef &TypeName, RSExportType *Type);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700265
Stephen Hines96ab06c2011-01-05 15:29:26 -0800266 int getVersion() const { return version; }
267 void setVersion(int v) {
268 version = v;
Stephen Hines96ab06c2011-01-05 15:29:26 -0800269 }
270
Jean-Luc Brouillet12fc2832014-06-04 21:32:31 +0000271 bool isCompatLib() const {
272 // If we are not targeting the actual Android Renderscript classes,
273 // we should reflect code that works with the compatibility library.
274 return (mRSPackageName.compare("android.renderscript") != 0);
275 }
Stephen Hines82754d82013-01-18 19:46:06 -0800276
Stephen Hines3fd0a942011-01-18 12:27:39 -0800277 void addPragma(const std::string &T, const std::string &V) {
278 mPragmas->push_back(make_pair(T, V));
279 }
Jean-Luc Brouillet109e90a2014-07-07 17:36:07 -0700280 void setPrecision(const std::string &P) { mPrecision = P; }
281 std::string getPrecision() { return mPrecision; }
Stephen Hines3fd0a942011-01-18 12:27:39 -0800282
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800283 // Report an error or a warning to the user.
Tim Murrayee4016d2014-04-10 15:49:08 -0700284 template <unsigned N>
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800285 clang::DiagnosticBuilder Report(clang::DiagnosticsEngine::Level Level,
Tim Murrayee4016d2014-04-10 15:49:08 -0700286 const char (&Message)[N]) {
287 clang::DiagnosticsEngine *DiagEngine = getDiagnostics();
288 return DiagEngine->Report(DiagEngine->getCustomDiagID(Level, Message));
289}
290
291 template <unsigned N>
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800292 clang::DiagnosticBuilder Report(clang::DiagnosticsEngine::Level Level,
Tim Murrayee4016d2014-04-10 15:49:08 -0700293 const clang::SourceLocation Loc,
294 const char (&Message)[N]) {
295 clang::DiagnosticsEngine *DiagEngine = getDiagnostics();
296 const clang::SourceManager *SM = getSourceManager();
297 return DiagEngine->Report(clang::FullSourceLoc(Loc, *SM),
298 DiagEngine->getCustomDiagID(Level, Message));
299}
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800300
301 // Utility functions to report errors and warnings to make the calling code
302 // easier to read.
Tim Murrayee4016d2014-04-10 15:49:08 -0700303 template <unsigned N>
304 clang::DiagnosticBuilder ReportError(const char (&Message)[N]) {
305 return Report<N>(clang::DiagnosticsEngine::Error, Message);
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800306 }
Tim Murrayee4016d2014-04-10 15:49:08 -0700307
308 template <unsigned N>
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800309 clang::DiagnosticBuilder ReportError(const clang::SourceLocation Loc,
Tim Murrayee4016d2014-04-10 15:49:08 -0700310 const char (&Message)[N]) {
311 return Report<N>(clang::DiagnosticsEngine::Error, Loc, Message);
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800312 }
Tim Murrayee4016d2014-04-10 15:49:08 -0700313
314 template <unsigned N>
315 clang::DiagnosticBuilder ReportWarning(const char (&Message)[N]) {
316 return Report<N>(clang::DiagnosticsEngine::Warning, Message);
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800317 }
Tim Murrayee4016d2014-04-10 15:49:08 -0700318
319 template <unsigned N>
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800320 clang::DiagnosticBuilder ReportWarning(const clang::SourceLocation Loc,
Tim Murrayee4016d2014-04-10 15:49:08 -0700321 const char (&Message)[N]) {
322 return Report<N>(clang::DiagnosticsEngine::Warning, Loc, Message);
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800323 }
324
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700325 ~RSContext();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700326};
327
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700328} // namespace slang
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700329
Stephen Hinese639eb52010-11-08 19:27:20 -0800330#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_CONTEXT_H_ NOLINT