blob: 8298a6772e48fc9287b30cce977aaf6dbee47e06 [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
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;
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;
Zonr Changa41ce1d2010-10-06 02:23:12 +080055 class RSExportType;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070056
57class RSContext {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070058 typedef llvm::StringSet<> NeedExportVarSet;
59 typedef llvm::StringSet<> NeedExportFuncSet;
60 typedef llvm::StringSet<> NeedExportTypeSet;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070061
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070062 public:
Zonr Changa41ce1d2010-10-06 02:23:12 +080063 typedef std::list<RSExportable*> ExportableList;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070064 typedef std::list<RSExportVar*> ExportVarList;
65 typedef std::list<RSExportFunc*> ExportFuncList;
Yang Nifb40ee22015-10-13 20:34:06 +000066 typedef std::vector<RSExportForEach*> ExportForEachVector;
Matt Walac0c5dd82015-07-23 17:29:37 -070067 typedef std::list<RSExportReduce*> ExportReduceList;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070068 typedef llvm::StringMap<RSExportType*> ExportTypeMap;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070069
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070070 private:
Stephen Hines9e5b5032010-11-03 13:19:14 -070071 clang::Preprocessor &mPP;
72 clang::ASTContext &mCtx;
Stephen Hines3fd0a942011-01-18 12:27:39 -080073 PragmaList *mPragmas;
Jean-Luc Brouillet109e90a2014-07-07 17:36:07 -070074 // Precision specified via pragma, either rs_fp_full or rs_fp_relaxed. If
75 // empty, rs_fp_full is assumed.
76 std::string mPrecision;
Chris Wailesc9454af2014-06-13 17:25:40 -070077 unsigned int mTargetAPI;
Stephen Hinesfc4f78b2014-06-10 18:07:10 -070078 bool mVerbose;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070079
Stephen Hines23c43582013-01-09 20:02:04 -080080 llvm::DataLayout *mDataLayout;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070081 llvm::LLVMContext &mLLVMContext;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070082
Zonr Changa41ce1d2010-10-06 02:23:12 +080083 ExportableList mExportables;
84
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070085 NeedExportTypeSet mNeedExportTypes;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070086
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070087 std::string *mLicenseNote;
88 std::string mReflectJavaPackageName;
89 std::string mReflectJavaPathName;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070090
Stephen Hines0a813a32012-08-03 16:52:40 -070091 std::string mRSPackageName;
92
Stephen Hines96ab06c2011-01-05 15:29:26 -080093 int version;
Stephen Hines82754d82013-01-18 19:46:06 -080094
Stephen Hines2eb9a3f2014-07-15 16:50:03 -070095 std::unique_ptr<clang::MangleContext> mMangleCtx;
Stephen Hines96ab06c2011-01-05 15:29:26 -080096
Stephen Hines9ae18b22014-06-10 23:53:00 -070097 bool mIs64Bit;
98
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070099 bool processExportVar(const clang::VarDecl *VD);
100 bool processExportFunc(const clang::FunctionDecl *FD);
101 bool processExportType(const llvm::StringRef &Name);
Shih-wei Liao537446c2010-06-11 16:05:55 -0700102
Yang Nifb40ee22015-10-13 20:34:06 +0000103 int getForEachSlotNumber(const clang::StringRef& funcName);
104 unsigned mNextSlot;
Stephen Hinesc17e1982012-02-22 12:30:45 -0800105
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700106 ExportVarList mExportVars;
107 ExportFuncList mExportFuncs;
Yang Nifb40ee22015-10-13 20:34:06 +0000108 std::map<llvm::StringRef, unsigned> mExportForEachMap;
109 ExportForEachVector mExportForEach;
Matt Walac0c5dd82015-07-23 17:29:37 -0700110 ExportReduceList mExportReduce;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700111 ExportTypeMap mExportTypes;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700112
Yang Nifb40ee22015-10-13 20:34:06 +0000113 clang::QualType mAllocationType;
114
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700115 public:
Stephen Hines9e5b5032010-11-03 13:19:14 -0700116 RSContext(clang::Preprocessor &PP,
117 clang::ASTContext &Ctx,
Stephen Hines3fd0a942011-01-18 12:27:39 -0800118 const clang::TargetInfo &Target,
Stephen Hines4cc67fc2011-01-31 16:48:57 -0800119 PragmaList *Pragmas,
Stephen Hinesfc4f78b2014-06-10 18:07:10 -0700120 unsigned int TargetAPI,
121 bool Verbose);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700122
Stephen Hines9e5b5032010-11-03 13:19:14 -0700123 inline clang::Preprocessor &getPreprocessor() const { return mPP; }
124 inline clang::ASTContext &getASTContext() const { return mCtx; }
Loganbe274822011-02-16 22:02:54 +0800125 inline clang::MangleContext &getMangleContext() const {
126 return *mMangleCtx;
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800127 }
Stephen Hines23c43582013-01-09 20:02:04 -0800128 inline const llvm::DataLayout *getDataLayout() const { return mDataLayout; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700129 inline llvm::LLVMContext &getLLVMContext() const { return mLLVMContext; }
130 inline const clang::SourceManager *getSourceManager() const {
Stephen Hines9e5b5032010-11-03 13:19:14 -0700131 return &mPP.getSourceManager();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700132 }
Logan Chien9207a2e2011-10-21 15:39:28 +0800133 inline clang::DiagnosticsEngine *getDiagnostics() const {
Stephen Hines2ef9bc02010-12-13 18:33:23 -0800134 return &mPP.getDiagnostics();
135 }
Chris Wailesc9454af2014-06-13 17:25:40 -0700136 inline unsigned int getTargetAPI() const {
Stephen Hines4a4bf922011-08-18 17:20:33 -0700137 return mTargetAPI;
138 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700139
Stephen Hinesfc4f78b2014-06-10 18:07:10 -0700140 inline bool getVerbose() const {
141 return mVerbose;
142 }
Stephen Hines9ae18b22014-06-10 23:53:00 -0700143 inline bool is64Bit() const {
144 return mIs64Bit;
145 }
Stephen Hinesfc4f78b2014-06-10 18:07:10 -0700146
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700147 inline void setLicenseNote(const std::string &S) {
148 mLicenseNote = new std::string(S);
149 }
150 inline const std::string *getLicenseNote() const { return mLicenseNote; }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700151
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700152 inline void addExportType(const std::string &S) {
153 mNeedExportTypes.insert(S);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700154 }
Victor Hsiehd8a0d182010-07-07 19:22:33 +0800155
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700156 inline void setReflectJavaPackageName(const std::string &S) {
157 mReflectJavaPackageName = S;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700158 }
Tim Murraydde98532013-07-23 15:55:19 -0700159 inline const std::string &getReflectJavaPackageName() const {
Stephen Hines4cc67fc2011-01-31 16:48:57 -0800160 return mReflectJavaPackageName;
161 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700162
Stephen Hines0a813a32012-08-03 16:52:40 -0700163 inline void setRSPackageName(const std::string &S) {
164 mRSPackageName = S;
Stephen Hines0a813a32012-08-03 16:52:40 -0700165 }
Jean-Luc Brouillet12fc2832014-06-04 21:32:31 +0000166
167 inline const std::string &getRSPackageName() const { return mRSPackageName; }
Stephen Hines0a813a32012-08-03 16:52:40 -0700168
Yang Nifb40ee22015-10-13 20:34:06 +0000169 void addAllocationType(const clang::TypeDecl* TD);
170 inline const clang::QualType& getAllocationType() const {
171 return mAllocationType;
172 }
173
174 bool addForEach(const clang::FunctionDecl* FD);
175 bool processExports();
Zonr Changa41ce1d2010-10-06 02:23:12 +0800176 inline void newExportable(RSExportable *E) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700177 if (E != nullptr)
Zonr Changa41ce1d2010-10-06 02:23:12 +0800178 mExportables.push_back(E);
179 }
Zonr Chang641558f2010-10-12 21:07:06 +0800180 typedef ExportableList::iterator exportable_iterator;
181 exportable_iterator exportable_begin() {
182 return mExportables.begin();
183 }
184 exportable_iterator exportable_end() {
185 return mExportables.end();
186 }
Shih-wei Liao537446c2010-06-11 16:05:55 -0700187
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700188 typedef ExportVarList::const_iterator const_export_var_iterator;
189 const_export_var_iterator export_vars_begin() const {
190 return mExportVars.begin();
191 }
192 const_export_var_iterator export_vars_end() const {
193 return mExportVars.end();
194 }
195 inline bool hasExportVar() const {
196 return !mExportVars.empty();
197 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700198
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700199 typedef ExportFuncList::const_iterator const_export_func_iterator;
200 const_export_func_iterator export_funcs_begin() const {
201 return mExportFuncs.begin();
202 }
203 const_export_func_iterator export_funcs_end() const {
204 return mExportFuncs.end();
205 }
206 inline bool hasExportFunc() const { return !mExportFuncs.empty(); }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700207
Yang Nifb40ee22015-10-13 20:34:06 +0000208 typedef ExportForEachVector::const_iterator const_export_foreach_iterator;
Stephen Hines593a8942011-05-10 15:29:50 -0700209 const_export_foreach_iterator export_foreach_begin() const {
210 return mExportForEach.begin();
211 }
212 const_export_foreach_iterator export_foreach_end() const {
213 return mExportForEach.end();
214 }
215 inline bool hasExportForEach() const { return !mExportForEach.empty(); }
Yang Nifb40ee22015-10-13 20:34:06 +0000216 int getForEachSlotNumber(const clang::FunctionDecl* FD);
Stephen Hines593a8942011-05-10 15:29:50 -0700217
Matt Walac0c5dd82015-07-23 17:29:37 -0700218 typedef ExportReduceList::const_iterator const_export_reduce_iterator;
219 const_export_reduce_iterator export_reduce_begin() const {
220 return mExportReduce.begin();
221 }
222 const_export_reduce_iterator export_reduce_end() const {
223 return mExportReduce.end();
224 }
225 inline bool hasExportReduce() const { return !mExportReduce.empty(); }
226
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700227 typedef ExportTypeMap::iterator export_type_iterator;
228 typedef ExportTypeMap::const_iterator const_export_type_iterator;
229 export_type_iterator export_types_begin() { return mExportTypes.begin(); }
230 export_type_iterator export_types_end() { return mExportTypes.end(); }
231 const_export_type_iterator export_types_begin() const {
232 return mExportTypes.begin();
233 }
234 const_export_type_iterator export_types_end() const {
235 return mExportTypes.end();
236 }
237 inline bool hasExportType() const { return !mExportTypes.empty(); }
238 export_type_iterator findExportType(const llvm::StringRef &TypeName) {
239 return mExportTypes.find(TypeName);
240 }
241 const_export_type_iterator findExportType(const llvm::StringRef &TypeName)
242 const {
243 return mExportTypes.find(TypeName);
244 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700245
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700246 // Insert the specified Typename/Type pair into the map. If the key already
247 // exists in the map, return false and ignore the request, otherwise insert it
248 // and return true.
249 bool insertExportType(const llvm::StringRef &TypeName, RSExportType *Type);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700250
Stephen Hines96ab06c2011-01-05 15:29:26 -0800251 int getVersion() const { return version; }
252 void setVersion(int v) {
253 version = v;
Stephen Hines96ab06c2011-01-05 15:29:26 -0800254 }
255
Jean-Luc Brouillet12fc2832014-06-04 21:32:31 +0000256 bool isCompatLib() const {
257 // If we are not targeting the actual Android Renderscript classes,
258 // we should reflect code that works with the compatibility library.
259 return (mRSPackageName.compare("android.renderscript") != 0);
260 }
Stephen Hines82754d82013-01-18 19:46:06 -0800261
Stephen Hines3fd0a942011-01-18 12:27:39 -0800262 void addPragma(const std::string &T, const std::string &V) {
263 mPragmas->push_back(make_pair(T, V));
264 }
Jean-Luc Brouillet109e90a2014-07-07 17:36:07 -0700265 void setPrecision(const std::string &P) { mPrecision = P; }
266 std::string getPrecision() { return mPrecision; }
Stephen Hines3fd0a942011-01-18 12:27:39 -0800267
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800268 // Report an error or a warning to the user.
Tim Murrayee4016d2014-04-10 15:49:08 -0700269 template <unsigned N>
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800270 clang::DiagnosticBuilder Report(clang::DiagnosticsEngine::Level Level,
Tim Murrayee4016d2014-04-10 15:49:08 -0700271 const char (&Message)[N]) {
272 clang::DiagnosticsEngine *DiagEngine = getDiagnostics();
273 return DiagEngine->Report(DiagEngine->getCustomDiagID(Level, Message));
274}
275
276 template <unsigned N>
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800277 clang::DiagnosticBuilder Report(clang::DiagnosticsEngine::Level Level,
Tim Murrayee4016d2014-04-10 15:49:08 -0700278 const clang::SourceLocation Loc,
279 const char (&Message)[N]) {
280 clang::DiagnosticsEngine *DiagEngine = getDiagnostics();
281 const clang::SourceManager *SM = getSourceManager();
282 return DiagEngine->Report(clang::FullSourceLoc(Loc, *SM),
283 DiagEngine->getCustomDiagID(Level, Message));
284}
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800285
286 // Utility functions to report errors and warnings to make the calling code
287 // easier to read.
Tim Murrayee4016d2014-04-10 15:49:08 -0700288 template <unsigned N>
289 clang::DiagnosticBuilder ReportError(const char (&Message)[N]) {
290 return Report<N>(clang::DiagnosticsEngine::Error, Message);
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800291 }
Tim Murrayee4016d2014-04-10 15:49:08 -0700292
293 template <unsigned N>
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800294 clang::DiagnosticBuilder ReportError(const clang::SourceLocation Loc,
Tim Murrayee4016d2014-04-10 15:49:08 -0700295 const char (&Message)[N]) {
296 return Report<N>(clang::DiagnosticsEngine::Error, Loc, Message);
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800297 }
Tim Murrayee4016d2014-04-10 15:49:08 -0700298
299 template <unsigned N>
300 clang::DiagnosticBuilder ReportWarning(const char (&Message)[N]) {
301 return Report<N>(clang::DiagnosticsEngine::Warning, Message);
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800302 }
Tim Murrayee4016d2014-04-10 15:49:08 -0700303
304 template <unsigned N>
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800305 clang::DiagnosticBuilder ReportWarning(const clang::SourceLocation Loc,
Tim Murrayee4016d2014-04-10 15:49:08 -0700306 const char (&Message)[N]) {
307 return Report<N>(clang::DiagnosticsEngine::Warning, Loc, Message);
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800308 }
309
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700310 ~RSContext();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700311};
312
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700313} // namespace slang
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700314
Stephen Hinese639eb52010-11-08 19:27:20 -0800315#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_CONTEXT_H_ NOLINT