blob: 8cced4d72d2ae799c445652f88054c866f955fe2 [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;
Matt Walac0c5dd82015-07-23 17:29:37 -070051 class RSExportReduce;
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 Hines5d9263d2015-10-13 20:07:52 +000063 typedef std::list<RSExportForEach*> ExportForEachList;
Matt Walac0c5dd82015-07-23 17:29:37 -070064 typedef std::list<RSExportReduce*> ExportReduceList;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070065 typedef llvm::StringMap<RSExportType*> ExportTypeMap;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070066
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070067 private:
Stephen Hines9e5b5032010-11-03 13:19:14 -070068 clang::Preprocessor &mPP;
69 clang::ASTContext &mCtx;
Stephen Hines3fd0a942011-01-18 12:27:39 -080070 PragmaList *mPragmas;
Jean-Luc Brouillet109e90a2014-07-07 17:36:07 -070071 // Precision specified via pragma, either rs_fp_full or rs_fp_relaxed. If
72 // empty, rs_fp_full is assumed.
73 std::string mPrecision;
Chris Wailesc9454af2014-06-13 17:25:40 -070074 unsigned int mTargetAPI;
Stephen Hinesfc4f78b2014-06-10 18:07:10 -070075 bool mVerbose;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070076
Stephen Hines23c43582013-01-09 20:02:04 -080077 llvm::DataLayout *mDataLayout;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070078 llvm::LLVMContext &mLLVMContext;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070079
Zonr Changa41ce1d2010-10-06 02:23:12 +080080 ExportableList mExportables;
81
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070082 NeedExportTypeSet mNeedExportTypes;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070083
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070084 std::string *mLicenseNote;
85 std::string mReflectJavaPackageName;
86 std::string mReflectJavaPathName;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070087
Stephen Hines0a813a32012-08-03 16:52:40 -070088 std::string mRSPackageName;
89
Stephen Hines96ab06c2011-01-05 15:29:26 -080090 int version;
Stephen Hines82754d82013-01-18 19:46:06 -080091
Stephen Hines2eb9a3f2014-07-15 16:50:03 -070092 std::unique_ptr<clang::MangleContext> mMangleCtx;
Stephen Hines96ab06c2011-01-05 15:29:26 -080093
Stephen Hines9ae18b22014-06-10 23:53:00 -070094 bool mIs64Bit;
95
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070096 bool processExportVar(const clang::VarDecl *VD);
97 bool processExportFunc(const clang::FunctionDecl *FD);
98 bool processExportType(const llvm::StringRef &Name);
Shih-wei Liao537446c2010-06-11 16:05:55 -070099
Stephen Hines5d9263d2015-10-13 20:07:52 +0000100 void cleanupForEach();
Stephen Hinesc17e1982012-02-22 12:30:45 -0800101
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700102 ExportVarList mExportVars;
103 ExportFuncList mExportFuncs;
Stephen Hines5d9263d2015-10-13 20:07:52 +0000104 ExportForEachList mExportForEach;
Matt Walac0c5dd82015-07-23 17:29:37 -0700105 ExportReduceList mExportReduce;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700106 ExportTypeMap mExportTypes;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700107
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700108 public:
Stephen Hines9e5b5032010-11-03 13:19:14 -0700109 RSContext(clang::Preprocessor &PP,
110 clang::ASTContext &Ctx,
Stephen Hines3fd0a942011-01-18 12:27:39 -0800111 const clang::TargetInfo &Target,
Stephen Hines4cc67fc2011-01-31 16:48:57 -0800112 PragmaList *Pragmas,
Stephen Hinesfc4f78b2014-06-10 18:07:10 -0700113 unsigned int TargetAPI,
114 bool Verbose);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700115
Stephen Hines9e5b5032010-11-03 13:19:14 -0700116 inline clang::Preprocessor &getPreprocessor() const { return mPP; }
117 inline clang::ASTContext &getASTContext() const { return mCtx; }
Loganbe274822011-02-16 22:02:54 +0800118 inline clang::MangleContext &getMangleContext() const {
119 return *mMangleCtx;
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800120 }
Stephen Hines23c43582013-01-09 20:02:04 -0800121 inline const llvm::DataLayout *getDataLayout() const { return mDataLayout; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700122 inline llvm::LLVMContext &getLLVMContext() const { return mLLVMContext; }
123 inline const clang::SourceManager *getSourceManager() const {
Stephen Hines9e5b5032010-11-03 13:19:14 -0700124 return &mPP.getSourceManager();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700125 }
Logan Chien9207a2e2011-10-21 15:39:28 +0800126 inline clang::DiagnosticsEngine *getDiagnostics() const {
Stephen Hines2ef9bc02010-12-13 18:33:23 -0800127 return &mPP.getDiagnostics();
128 }
Chris Wailesc9454af2014-06-13 17:25:40 -0700129 inline unsigned int getTargetAPI() const {
Stephen Hines4a4bf922011-08-18 17:20:33 -0700130 return mTargetAPI;
131 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700132
Stephen Hinesfc4f78b2014-06-10 18:07:10 -0700133 inline bool getVerbose() const {
134 return mVerbose;
135 }
Stephen Hines9ae18b22014-06-10 23:53:00 -0700136 inline bool is64Bit() const {
137 return mIs64Bit;
138 }
Stephen Hinesfc4f78b2014-06-10 18:07:10 -0700139
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700140 inline void setLicenseNote(const std::string &S) {
141 mLicenseNote = new std::string(S);
142 }
143 inline const std::string *getLicenseNote() const { return mLicenseNote; }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700144
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700145 inline void addExportType(const std::string &S) {
146 mNeedExportTypes.insert(S);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700147 }
Victor Hsiehd8a0d182010-07-07 19:22:33 +0800148
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700149 inline void setReflectJavaPackageName(const std::string &S) {
150 mReflectJavaPackageName = S;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700151 }
Tim Murraydde98532013-07-23 15:55:19 -0700152 inline const std::string &getReflectJavaPackageName() const {
Stephen Hines4cc67fc2011-01-31 16:48:57 -0800153 return mReflectJavaPackageName;
154 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700155
Stephen Hines0a813a32012-08-03 16:52:40 -0700156 inline void setRSPackageName(const std::string &S) {
157 mRSPackageName = S;
Stephen Hines0a813a32012-08-03 16:52:40 -0700158 }
Jean-Luc Brouillet12fc2832014-06-04 21:32:31 +0000159
160 inline const std::string &getRSPackageName() const { return mRSPackageName; }
Stephen Hines0a813a32012-08-03 16:52:40 -0700161
Stephen Hinesc808a992010-11-29 17:20:42 -0800162 bool processExport();
Zonr Changa41ce1d2010-10-06 02:23:12 +0800163 inline void newExportable(RSExportable *E) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700164 if (E != nullptr)
Zonr Changa41ce1d2010-10-06 02:23:12 +0800165 mExportables.push_back(E);
166 }
Zonr Chang641558f2010-10-12 21:07:06 +0800167 typedef ExportableList::iterator exportable_iterator;
168 exportable_iterator exportable_begin() {
169 return mExportables.begin();
170 }
171 exportable_iterator exportable_end() {
172 return mExportables.end();
173 }
Shih-wei Liao537446c2010-06-11 16:05:55 -0700174
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700175 typedef ExportVarList::const_iterator const_export_var_iterator;
176 const_export_var_iterator export_vars_begin() const {
177 return mExportVars.begin();
178 }
179 const_export_var_iterator export_vars_end() const {
180 return mExportVars.end();
181 }
182 inline bool hasExportVar() const {
183 return !mExportVars.empty();
184 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700185
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700186 typedef ExportFuncList::const_iterator const_export_func_iterator;
187 const_export_func_iterator export_funcs_begin() const {
188 return mExportFuncs.begin();
189 }
190 const_export_func_iterator export_funcs_end() const {
191 return mExportFuncs.end();
192 }
193 inline bool hasExportFunc() const { return !mExportFuncs.empty(); }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700194
Stephen Hines5d9263d2015-10-13 20:07:52 +0000195 typedef ExportForEachList::const_iterator const_export_foreach_iterator;
Stephen Hines593a8942011-05-10 15:29:50 -0700196 const_export_foreach_iterator export_foreach_begin() const {
197 return mExportForEach.begin();
198 }
199 const_export_foreach_iterator export_foreach_end() const {
200 return mExportForEach.end();
201 }
202 inline bool hasExportForEach() const { return !mExportForEach.empty(); }
203
Matt Walac0c5dd82015-07-23 17:29:37 -0700204 typedef ExportReduceList::const_iterator const_export_reduce_iterator;
205 const_export_reduce_iterator export_reduce_begin() const {
206 return mExportReduce.begin();
207 }
208 const_export_reduce_iterator export_reduce_end() const {
209 return mExportReduce.end();
210 }
211 inline bool hasExportReduce() const { return !mExportReduce.empty(); }
212
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700213 typedef ExportTypeMap::iterator export_type_iterator;
214 typedef ExportTypeMap::const_iterator const_export_type_iterator;
215 export_type_iterator export_types_begin() { return mExportTypes.begin(); }
216 export_type_iterator export_types_end() { return mExportTypes.end(); }
217 const_export_type_iterator export_types_begin() const {
218 return mExportTypes.begin();
219 }
220 const_export_type_iterator export_types_end() const {
221 return mExportTypes.end();
222 }
223 inline bool hasExportType() const { return !mExportTypes.empty(); }
224 export_type_iterator findExportType(const llvm::StringRef &TypeName) {
225 return mExportTypes.find(TypeName);
226 }
227 const_export_type_iterator findExportType(const llvm::StringRef &TypeName)
228 const {
229 return mExportTypes.find(TypeName);
230 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700231
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700232 // Insert the specified Typename/Type pair into the map. If the key already
233 // exists in the map, return false and ignore the request, otherwise insert it
234 // and return true.
235 bool insertExportType(const llvm::StringRef &TypeName, RSExportType *Type);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700236
Stephen Hines96ab06c2011-01-05 15:29:26 -0800237 int getVersion() const { return version; }
238 void setVersion(int v) {
239 version = v;
Stephen Hines96ab06c2011-01-05 15:29:26 -0800240 }
241
Jean-Luc Brouillet12fc2832014-06-04 21:32:31 +0000242 bool isCompatLib() const {
243 // If we are not targeting the actual Android Renderscript classes,
244 // we should reflect code that works with the compatibility library.
245 return (mRSPackageName.compare("android.renderscript") != 0);
246 }
Stephen Hines82754d82013-01-18 19:46:06 -0800247
Stephen Hines3fd0a942011-01-18 12:27:39 -0800248 void addPragma(const std::string &T, const std::string &V) {
249 mPragmas->push_back(make_pair(T, V));
250 }
Jean-Luc Brouillet109e90a2014-07-07 17:36:07 -0700251 void setPrecision(const std::string &P) { mPrecision = P; }
252 std::string getPrecision() { return mPrecision; }
Stephen Hines3fd0a942011-01-18 12:27:39 -0800253
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800254 // Report an error or a warning to the user.
Tim Murrayee4016d2014-04-10 15:49:08 -0700255 template <unsigned N>
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800256 clang::DiagnosticBuilder Report(clang::DiagnosticsEngine::Level Level,
Tim Murrayee4016d2014-04-10 15:49:08 -0700257 const char (&Message)[N]) {
258 clang::DiagnosticsEngine *DiagEngine = getDiagnostics();
259 return DiagEngine->Report(DiagEngine->getCustomDiagID(Level, Message));
260}
261
262 template <unsigned N>
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800263 clang::DiagnosticBuilder Report(clang::DiagnosticsEngine::Level Level,
Tim Murrayee4016d2014-04-10 15:49:08 -0700264 const clang::SourceLocation Loc,
265 const char (&Message)[N]) {
266 clang::DiagnosticsEngine *DiagEngine = getDiagnostics();
267 const clang::SourceManager *SM = getSourceManager();
268 return DiagEngine->Report(clang::FullSourceLoc(Loc, *SM),
269 DiagEngine->getCustomDiagID(Level, Message));
270}
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800271
272 // Utility functions to report errors and warnings to make the calling code
273 // easier to read.
Tim Murrayee4016d2014-04-10 15:49:08 -0700274 template <unsigned N>
275 clang::DiagnosticBuilder ReportError(const char (&Message)[N]) {
276 return Report<N>(clang::DiagnosticsEngine::Error, Message);
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800277 }
Tim Murrayee4016d2014-04-10 15:49:08 -0700278
279 template <unsigned N>
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800280 clang::DiagnosticBuilder ReportError(const clang::SourceLocation Loc,
Tim Murrayee4016d2014-04-10 15:49:08 -0700281 const char (&Message)[N]) {
282 return Report<N>(clang::DiagnosticsEngine::Error, Loc, Message);
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800283 }
Tim Murrayee4016d2014-04-10 15:49:08 -0700284
285 template <unsigned N>
286 clang::DiagnosticBuilder ReportWarning(const char (&Message)[N]) {
287 return Report<N>(clang::DiagnosticsEngine::Warning, Message);
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800288 }
Tim Murrayee4016d2014-04-10 15:49:08 -0700289
290 template <unsigned N>
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800291 clang::DiagnosticBuilder ReportWarning(const clang::SourceLocation Loc,
Tim Murrayee4016d2014-04-10 15:49:08 -0700292 const char (&Message)[N]) {
293 return Report<N>(clang::DiagnosticsEngine::Warning, Loc, Message);
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800294 }
295
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700296 ~RSContext();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700297};
298
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700299} // namespace slang
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700300
Stephen Hinese639eb52010-11-08 19:27:20 -0800301#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_CONTEXT_H_ NOLINT