blob: 82cee4048c7616b527b49e44baa7d2ee4cf505f0 [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
Loganbe274822011-02-16 22:02:54 +080028#include "llvm/ADT/OwningPtr.h"
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;
44 class SourceManager;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070045} // namespace clang
Shih-wei Liao462aefd2010-06-04 15:32:04 -070046
47namespace slang {
Zonr Changa41ce1d2010-10-06 02:23:12 +080048 class RSExportable;
49 class RSExportVar;
50 class RSExportFunc;
Stephen Hines593a8942011-05-10 15:29:50 -070051 class RSExportForEach;
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 Hines593a8942011-05-10 15:29:50 -070063 typedef std::list<RSExportForEach*> ExportForEachList;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070064 typedef llvm::StringMap<RSExportType*> ExportTypeMap;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070065
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070066 private:
Stephen Hines9e5b5032010-11-03 13:19:14 -070067 clang::Preprocessor &mPP;
68 clang::ASTContext &mCtx;
Stephen Hines3fd0a942011-01-18 12:27:39 -080069 PragmaList *mPragmas;
Jean-Luc Brouillet109e90a2014-07-07 17:36:07 -070070 // Precision specified via pragma, either rs_fp_full or rs_fp_relaxed. If
71 // empty, rs_fp_full is assumed.
72 std::string mPrecision;
Chris Wailesc9454af2014-06-13 17:25:40 -070073 unsigned int mTargetAPI;
Stephen Hinesfc4f78b2014-06-10 18:07:10 -070074 bool mVerbose;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070075
Stephen Hines23c43582013-01-09 20:02:04 -080076 llvm::DataLayout *mDataLayout;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070077 llvm::LLVMContext &mLLVMContext;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070078
Zonr Changa41ce1d2010-10-06 02:23:12 +080079 ExportableList mExportables;
80
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070081 NeedExportTypeSet mNeedExportTypes;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070082
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070083 std::string *mLicenseNote;
84 std::string mReflectJavaPackageName;
85 std::string mReflectJavaPathName;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070086
Stephen Hines0a813a32012-08-03 16:52:40 -070087 std::string mRSPackageName;
88
Stephen Hines96ab06c2011-01-05 15:29:26 -080089 int version;
Stephen Hines82754d82013-01-18 19:46:06 -080090
Loganbe274822011-02-16 22:02:54 +080091 llvm::OwningPtr<clang::MangleContext> mMangleCtx;
Stephen Hines96ab06c2011-01-05 15:29:26 -080092
Stephen Hines9ae18b22014-06-10 23:53:00 -070093 bool mIs64Bit;
94
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070095 bool processExportVar(const clang::VarDecl *VD);
96 bool processExportFunc(const clang::FunctionDecl *FD);
97 bool processExportType(const llvm::StringRef &Name);
Shih-wei Liao537446c2010-06-11 16:05:55 -070098
Stephen Hinesc17e1982012-02-22 12:30:45 -080099 void cleanupForEach();
100
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700101 ExportVarList mExportVars;
102 ExportFuncList mExportFuncs;
Stephen Hines593a8942011-05-10 15:29:50 -0700103 ExportForEachList mExportForEach;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700104 ExportTypeMap mExportTypes;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700105
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700106 public:
Stephen Hines9e5b5032010-11-03 13:19:14 -0700107 RSContext(clang::Preprocessor &PP,
108 clang::ASTContext &Ctx,
Stephen Hines3fd0a942011-01-18 12:27:39 -0800109 const clang::TargetInfo &Target,
Stephen Hines4cc67fc2011-01-31 16:48:57 -0800110 PragmaList *Pragmas,
Stephen Hinesfc4f78b2014-06-10 18:07:10 -0700111 unsigned int TargetAPI,
112 bool Verbose);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700113
Stephen Hines9e5b5032010-11-03 13:19:14 -0700114 inline clang::Preprocessor &getPreprocessor() const { return mPP; }
115 inline clang::ASTContext &getASTContext() const { return mCtx; }
Loganbe274822011-02-16 22:02:54 +0800116 inline clang::MangleContext &getMangleContext() const {
117 return *mMangleCtx;
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800118 }
Stephen Hines23c43582013-01-09 20:02:04 -0800119 inline const llvm::DataLayout *getDataLayout() const { return mDataLayout; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700120 inline llvm::LLVMContext &getLLVMContext() const { return mLLVMContext; }
121 inline const clang::SourceManager *getSourceManager() const {
Stephen Hines9e5b5032010-11-03 13:19:14 -0700122 return &mPP.getSourceManager();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700123 }
Logan Chien9207a2e2011-10-21 15:39:28 +0800124 inline clang::DiagnosticsEngine *getDiagnostics() const {
Stephen Hines2ef9bc02010-12-13 18:33:23 -0800125 return &mPP.getDiagnostics();
126 }
Chris Wailesc9454af2014-06-13 17:25:40 -0700127 inline unsigned int getTargetAPI() const {
Stephen Hines4a4bf922011-08-18 17:20:33 -0700128 return mTargetAPI;
129 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700130
Stephen Hinesfc4f78b2014-06-10 18:07:10 -0700131 inline bool getVerbose() const {
132 return mVerbose;
133 }
Stephen Hines9ae18b22014-06-10 23:53:00 -0700134 inline bool is64Bit() const {
135 return mIs64Bit;
136 }
Stephen Hinesfc4f78b2014-06-10 18:07:10 -0700137
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700138 inline void setLicenseNote(const std::string &S) {
139 mLicenseNote = new std::string(S);
140 }
141 inline const std::string *getLicenseNote() const { return mLicenseNote; }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700142
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700143 inline void addExportType(const std::string &S) {
144 mNeedExportTypes.insert(S);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700145 }
Victor Hsiehd8a0d182010-07-07 19:22:33 +0800146
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700147 inline void setReflectJavaPackageName(const std::string &S) {
148 mReflectJavaPackageName = S;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700149 }
Tim Murraydde98532013-07-23 15:55:19 -0700150 inline const std::string &getReflectJavaPackageName() const {
Stephen Hines4cc67fc2011-01-31 16:48:57 -0800151 return mReflectJavaPackageName;
152 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700153
Stephen Hines0a813a32012-08-03 16:52:40 -0700154 inline void setRSPackageName(const std::string &S) {
155 mRSPackageName = S;
Stephen Hines0a813a32012-08-03 16:52:40 -0700156 }
Jean-Luc Brouillet12fc2832014-06-04 21:32:31 +0000157
158 inline const std::string &getRSPackageName() const { return mRSPackageName; }
Stephen Hines0a813a32012-08-03 16:52:40 -0700159
Stephen Hinesc808a992010-11-29 17:20:42 -0800160 bool processExport();
Zonr Changa41ce1d2010-10-06 02:23:12 +0800161 inline void newExportable(RSExportable *E) {
162 if (E != NULL)
163 mExportables.push_back(E);
164 }
Zonr Chang641558f2010-10-12 21:07:06 +0800165 typedef ExportableList::iterator exportable_iterator;
166 exportable_iterator exportable_begin() {
167 return mExportables.begin();
168 }
169 exportable_iterator exportable_end() {
170 return mExportables.end();
171 }
Shih-wei Liao537446c2010-06-11 16:05:55 -0700172
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700173 typedef ExportVarList::const_iterator const_export_var_iterator;
174 const_export_var_iterator export_vars_begin() const {
175 return mExportVars.begin();
176 }
177 const_export_var_iterator export_vars_end() const {
178 return mExportVars.end();
179 }
180 inline bool hasExportVar() const {
181 return !mExportVars.empty();
182 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700183
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700184 typedef ExportFuncList::const_iterator const_export_func_iterator;
185 const_export_func_iterator export_funcs_begin() const {
186 return mExportFuncs.begin();
187 }
188 const_export_func_iterator export_funcs_end() const {
189 return mExportFuncs.end();
190 }
191 inline bool hasExportFunc() const { return !mExportFuncs.empty(); }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700192
Stephen Hines593a8942011-05-10 15:29:50 -0700193 typedef ExportForEachList::const_iterator const_export_foreach_iterator;
194 const_export_foreach_iterator export_foreach_begin() const {
195 return mExportForEach.begin();
196 }
197 const_export_foreach_iterator export_foreach_end() const {
198 return mExportForEach.end();
199 }
200 inline bool hasExportForEach() const { return !mExportForEach.empty(); }
201
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700202 typedef ExportTypeMap::iterator export_type_iterator;
203 typedef ExportTypeMap::const_iterator const_export_type_iterator;
204 export_type_iterator export_types_begin() { return mExportTypes.begin(); }
205 export_type_iterator export_types_end() { return mExportTypes.end(); }
206 const_export_type_iterator export_types_begin() const {
207 return mExportTypes.begin();
208 }
209 const_export_type_iterator export_types_end() const {
210 return mExportTypes.end();
211 }
212 inline bool hasExportType() const { return !mExportTypes.empty(); }
213 export_type_iterator findExportType(const llvm::StringRef &TypeName) {
214 return mExportTypes.find(TypeName);
215 }
216 const_export_type_iterator findExportType(const llvm::StringRef &TypeName)
217 const {
218 return mExportTypes.find(TypeName);
219 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700220
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700221 // Insert the specified Typename/Type pair into the map. If the key already
222 // exists in the map, return false and ignore the request, otherwise insert it
223 // and return true.
224 bool insertExportType(const llvm::StringRef &TypeName, RSExportType *Type);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700225
Stephen Hines96ab06c2011-01-05 15:29:26 -0800226 int getVersion() const { return version; }
227 void setVersion(int v) {
228 version = v;
Stephen Hines96ab06c2011-01-05 15:29:26 -0800229 }
230
Jean-Luc Brouillet12fc2832014-06-04 21:32:31 +0000231 bool isCompatLib() const {
232 // If we are not targeting the actual Android Renderscript classes,
233 // we should reflect code that works with the compatibility library.
234 return (mRSPackageName.compare("android.renderscript") != 0);
235 }
Stephen Hines82754d82013-01-18 19:46:06 -0800236
Stephen Hines3fd0a942011-01-18 12:27:39 -0800237 void addPragma(const std::string &T, const std::string &V) {
238 mPragmas->push_back(make_pair(T, V));
239 }
Jean-Luc Brouillet109e90a2014-07-07 17:36:07 -0700240 void setPrecision(const std::string &P) { mPrecision = P; }
241 std::string getPrecision() { return mPrecision; }
Stephen Hines3fd0a942011-01-18 12:27:39 -0800242
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800243 // Report an error or a warning to the user.
Tim Murrayee4016d2014-04-10 15:49:08 -0700244 template <unsigned N>
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800245 clang::DiagnosticBuilder Report(clang::DiagnosticsEngine::Level Level,
Tim Murrayee4016d2014-04-10 15:49:08 -0700246 const char (&Message)[N]) {
247 clang::DiagnosticsEngine *DiagEngine = getDiagnostics();
248 return DiagEngine->Report(DiagEngine->getCustomDiagID(Level, Message));
249}
250
251 template <unsigned N>
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800252 clang::DiagnosticBuilder Report(clang::DiagnosticsEngine::Level Level,
Tim Murrayee4016d2014-04-10 15:49:08 -0700253 const clang::SourceLocation Loc,
254 const char (&Message)[N]) {
255 clang::DiagnosticsEngine *DiagEngine = getDiagnostics();
256 const clang::SourceManager *SM = getSourceManager();
257 return DiagEngine->Report(clang::FullSourceLoc(Loc, *SM),
258 DiagEngine->getCustomDiagID(Level, Message));
259}
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800260
261 // Utility functions to report errors and warnings to make the calling code
262 // easier to read.
Tim Murrayee4016d2014-04-10 15:49:08 -0700263 template <unsigned N>
264 clang::DiagnosticBuilder ReportError(const char (&Message)[N]) {
265 return Report<N>(clang::DiagnosticsEngine::Error, Message);
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800266 }
Tim Murrayee4016d2014-04-10 15:49:08 -0700267
268 template <unsigned N>
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800269 clang::DiagnosticBuilder ReportError(const clang::SourceLocation Loc,
Tim Murrayee4016d2014-04-10 15:49:08 -0700270 const char (&Message)[N]) {
271 return Report<N>(clang::DiagnosticsEngine::Error, Loc, Message);
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800272 }
Tim Murrayee4016d2014-04-10 15:49:08 -0700273
274 template <unsigned N>
275 clang::DiagnosticBuilder ReportWarning(const char (&Message)[N]) {
276 return Report<N>(clang::DiagnosticsEngine::Warning, 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 ReportWarning(const clang::SourceLocation Loc,
Tim Murrayee4016d2014-04-10 15:49:08 -0700281 const char (&Message)[N]) {
282 return Report<N>(clang::DiagnosticsEngine::Warning, Loc, Message);
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800283 }
284
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700285 ~RSContext();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700286};
287
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700288} // namespace slang
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700289
Stephen Hinese639eb52010-11-08 19:27:20 -0800290#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_CONTEXT_H_ NOLINT