blob: 702c9c9b081a8456bb83493c65f11b4084d5cfa6 [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 bool addForEach(const clang::FunctionDecl* FD);
170 bool processExports();
Zonr Changa41ce1d2010-10-06 02:23:12 +0800171 inline void newExportable(RSExportable *E) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700172 if (E != nullptr)
Zonr Changa41ce1d2010-10-06 02:23:12 +0800173 mExportables.push_back(E);
174 }
Zonr Chang641558f2010-10-12 21:07:06 +0800175 typedef ExportableList::iterator exportable_iterator;
176 exportable_iterator exportable_begin() {
177 return mExportables.begin();
178 }
179 exportable_iterator exportable_end() {
180 return mExportables.end();
181 }
Shih-wei Liao537446c2010-06-11 16:05:55 -0700182
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700183 typedef ExportVarList::const_iterator const_export_var_iterator;
184 const_export_var_iterator export_vars_begin() const {
185 return mExportVars.begin();
186 }
187 const_export_var_iterator export_vars_end() const {
188 return mExportVars.end();
189 }
190 inline bool hasExportVar() const {
191 return !mExportVars.empty();
192 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700193
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700194 typedef ExportFuncList::const_iterator const_export_func_iterator;
195 const_export_func_iterator export_funcs_begin() const {
196 return mExportFuncs.begin();
197 }
198 const_export_func_iterator export_funcs_end() const {
199 return mExportFuncs.end();
200 }
201 inline bool hasExportFunc() const { return !mExportFuncs.empty(); }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700202
Yang Nifb40ee22015-10-13 20:34:06 +0000203 typedef ExportForEachVector::const_iterator const_export_foreach_iterator;
Stephen Hines593a8942011-05-10 15:29:50 -0700204 const_export_foreach_iterator export_foreach_begin() const {
205 return mExportForEach.begin();
206 }
207 const_export_foreach_iterator export_foreach_end() const {
208 return mExportForEach.end();
209 }
210 inline bool hasExportForEach() const { return !mExportForEach.empty(); }
Yang Nifb40ee22015-10-13 20:34:06 +0000211 int getForEachSlotNumber(const clang::FunctionDecl* FD);
Stephen Hines593a8942011-05-10 15:29:50 -0700212
Matt Walac0c5dd82015-07-23 17:29:37 -0700213 typedef ExportReduceList::const_iterator const_export_reduce_iterator;
214 const_export_reduce_iterator export_reduce_begin() const {
215 return mExportReduce.begin();
216 }
217 const_export_reduce_iterator export_reduce_end() const {
218 return mExportReduce.end();
219 }
220 inline bool hasExportReduce() const { return !mExportReduce.empty(); }
221
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700222 typedef ExportTypeMap::iterator export_type_iterator;
223 typedef ExportTypeMap::const_iterator const_export_type_iterator;
224 export_type_iterator export_types_begin() { return mExportTypes.begin(); }
225 export_type_iterator export_types_end() { return mExportTypes.end(); }
226 const_export_type_iterator export_types_begin() const {
227 return mExportTypes.begin();
228 }
229 const_export_type_iterator export_types_end() const {
230 return mExportTypes.end();
231 }
232 inline bool hasExportType() const { return !mExportTypes.empty(); }
233 export_type_iterator findExportType(const llvm::StringRef &TypeName) {
234 return mExportTypes.find(TypeName);
235 }
236 const_export_type_iterator findExportType(const llvm::StringRef &TypeName)
237 const {
238 return mExportTypes.find(TypeName);
239 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700240
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700241 // Insert the specified Typename/Type pair into the map. If the key already
242 // exists in the map, return false and ignore the request, otherwise insert it
243 // and return true.
244 bool insertExportType(const llvm::StringRef &TypeName, RSExportType *Type);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700245
Stephen Hines96ab06c2011-01-05 15:29:26 -0800246 int getVersion() const { return version; }
247 void setVersion(int v) {
248 version = v;
Stephen Hines96ab06c2011-01-05 15:29:26 -0800249 }
250
Jean-Luc Brouillet12fc2832014-06-04 21:32:31 +0000251 bool isCompatLib() const {
252 // If we are not targeting the actual Android Renderscript classes,
253 // we should reflect code that works with the compatibility library.
254 return (mRSPackageName.compare("android.renderscript") != 0);
255 }
Stephen Hines82754d82013-01-18 19:46:06 -0800256
Stephen Hines3fd0a942011-01-18 12:27:39 -0800257 void addPragma(const std::string &T, const std::string &V) {
258 mPragmas->push_back(make_pair(T, V));
259 }
Jean-Luc Brouillet109e90a2014-07-07 17:36:07 -0700260 void setPrecision(const std::string &P) { mPrecision = P; }
261 std::string getPrecision() { return mPrecision; }
Stephen Hines3fd0a942011-01-18 12:27:39 -0800262
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800263 // Report an error or a warning to the user.
Tim Murrayee4016d2014-04-10 15:49:08 -0700264 template <unsigned N>
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800265 clang::DiagnosticBuilder Report(clang::DiagnosticsEngine::Level Level,
Tim Murrayee4016d2014-04-10 15:49:08 -0700266 const char (&Message)[N]) {
267 clang::DiagnosticsEngine *DiagEngine = getDiagnostics();
268 return DiagEngine->Report(DiagEngine->getCustomDiagID(Level, Message));
269}
270
271 template <unsigned N>
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800272 clang::DiagnosticBuilder Report(clang::DiagnosticsEngine::Level Level,
Tim Murrayee4016d2014-04-10 15:49:08 -0700273 const clang::SourceLocation Loc,
274 const char (&Message)[N]) {
275 clang::DiagnosticsEngine *DiagEngine = getDiagnostics();
276 const clang::SourceManager *SM = getSourceManager();
277 return DiagEngine->Report(clang::FullSourceLoc(Loc, *SM),
278 DiagEngine->getCustomDiagID(Level, Message));
279}
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800280
281 // Utility functions to report errors and warnings to make the calling code
282 // easier to read.
Tim Murrayee4016d2014-04-10 15:49:08 -0700283 template <unsigned N>
284 clang::DiagnosticBuilder ReportError(const char (&Message)[N]) {
285 return Report<N>(clang::DiagnosticsEngine::Error, Message);
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800286 }
Tim Murrayee4016d2014-04-10 15:49:08 -0700287
288 template <unsigned N>
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800289 clang::DiagnosticBuilder ReportError(const clang::SourceLocation Loc,
Tim Murrayee4016d2014-04-10 15:49:08 -0700290 const char (&Message)[N]) {
291 return Report<N>(clang::DiagnosticsEngine::Error, Loc, Message);
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800292 }
Tim Murrayee4016d2014-04-10 15:49:08 -0700293
294 template <unsigned N>
295 clang::DiagnosticBuilder ReportWarning(const char (&Message)[N]) {
296 return Report<N>(clang::DiagnosticsEngine::Warning, Message);
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800297 }
Tim Murrayee4016d2014-04-10 15:49:08 -0700298
299 template <unsigned N>
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800300 clang::DiagnosticBuilder ReportWarning(const clang::SourceLocation Loc,
Tim Murrayee4016d2014-04-10 15:49:08 -0700301 const char (&Message)[N]) {
302 return Report<N>(clang::DiagnosticsEngine::Warning, Loc, Message);
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800303 }
304
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700305 ~RSContext();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700306};
307
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700308} // namespace slang
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700309
Stephen Hinese639eb52010-11-08 19:27:20 -0800310#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_CONTEXT_H_ NOLINT