blob: e9bb008e3011cd0a7d5e933289d2ca3a42e12508 [file] [log] [blame]
Zonr Changc383a502010-10-12 01:52:08 +08001/*
Stephen Hines9999ec32012-02-10 18:22:14 -08002 * 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
zonr6315f762010-10-05 15:35:14 +080017#include "slang_rs_context.h"
18
Stephen Hinese639eb52010-11-08 19:27:20 -080019#include <string>
20
21#include "clang/AST/ASTContext.h"
22#include "clang/AST/Decl.h"
23#include "clang/AST/DeclBase.h"
Loganbe274822011-02-16 22:02:54 +080024#include "clang/AST/Mangle.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080025#include "clang/AST/Type.h"
26
27#include "clang/Basic/Linkage.h"
28#include "clang/Basic/TargetInfo.h"
29
Stephen Hines23c43582013-01-09 20:02:04 -080030#include "llvm/IR/LLVMContext.h"
31#include "llvm/IR/DataLayout.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070032
zonr6315f762010-10-05 15:35:14 +080033#include "slang.h"
Stephen Hines6e6578a2011-02-07 18:05:48 -080034#include "slang_assert.h"
Stephen Hines593a8942011-05-10 15:29:50 -070035#include "slang_rs_export_foreach.h"
zonr6315f762010-10-05 15:35:14 +080036#include "slang_rs_export_func.h"
37#include "slang_rs_export_type.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080038#include "slang_rs_export_var.h"
39#include "slang_rs_exportable.h"
zonr6315f762010-10-05 15:35:14 +080040#include "slang_rs_pragma_handler.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080041#include "slang_rs_reflection.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070042
Stephen Hinese639eb52010-11-08 19:27:20 -080043namespace slang {
Shih-wei Liao462aefd2010-06-04 15:32:04 -070044
Stephen Hines9e5b5032010-11-03 13:19:14 -070045RSContext::RSContext(clang::Preprocessor &PP,
46 clang::ASTContext &Ctx,
Stephen Hines3fd0a942011-01-18 12:27:39 -080047 const clang::TargetInfo &Target,
Stephen Hines4cc67fc2011-01-31 16:48:57 -080048 PragmaList *Pragmas,
Stephen Hinesfc4f78b2014-06-10 18:07:10 -070049 unsigned int TargetAPI,
50 bool Verbose)
zonr6315f762010-10-05 15:35:14 +080051 : mPP(PP),
52 mCtx(Ctx),
Stephen Hines3fd0a942011-01-18 12:27:39 -080053 mPragmas(Pragmas),
Stephen Hines4a4bf922011-08-18 17:20:33 -070054 mTargetAPI(TargetAPI),
Stephen Hinesfc4f78b2014-06-10 18:07:10 -070055 mVerbose(Verbose),
Stephen Hines23c43582013-01-09 20:02:04 -080056 mDataLayout(NULL),
zonr6315f762010-10-05 15:35:14 +080057 mLLVMContext(llvm::getGlobalContext()),
Stephen Hines96ab06c2011-01-05 15:29:26 -080058 mLicenseNote(NULL),
Stephen Hines0a813a32012-08-03 16:52:40 -070059 mRSPackageName("android.renderscript"),
Shih-wei Liao3fa286b2011-02-10 11:04:44 -080060 version(0),
Stephen Hines9ae18b22014-06-10 23:53:00 -070061 mMangleCtx(Ctx.createMangleContext()),
62 mIs64Bit(Target.getPointerWidth(0) == 64) {
Stephen Hines4cc67fc2011-01-31 16:48:57 -080063
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070064 // For #pragma rs export_type
Stephen Hines9e5b5032010-11-03 13:19:14 -070065 PP.AddPragmaHandler(
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070066 "rs", RSPragmaHandler::CreatePragmaExportTypeHandler(this));
Victor Hsiehd8a0d182010-07-07 19:22:33 +080067
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070068 // For #pragma rs java_package_name
Stephen Hines9e5b5032010-11-03 13:19:14 -070069 PP.AddPragmaHandler(
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070070 "rs", RSPragmaHandler::CreatePragmaJavaPackageNameHandler(this));
Shih-wei Liao462aefd2010-06-04 15:32:04 -070071
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070072 // For #pragma rs set_reflect_license
Stephen Hines9e5b5032010-11-03 13:19:14 -070073 PP.AddPragmaHandler(
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070074 "rs", RSPragmaHandler::CreatePragmaReflectLicenseHandler(this));
75
Stephen Hines96ab06c2011-01-05 15:29:26 -080076 // For #pragma version
77 PP.AddPragmaHandler(RSPragmaHandler::CreatePragmaVersionHandler(this));
78
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070079 // Prepare target data
Stephen Hines23c43582013-01-09 20:02:04 -080080 mDataLayout = new llvm::DataLayout(Target.getTargetDescription());
Shih-wei Liao462aefd2010-06-04 15:32:04 -070081}
82
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070083bool RSContext::processExportVar(const clang::VarDecl *VD) {
Stephen Hines6e6578a2011-02-07 18:05:48 -080084 slangAssert(!VD->getName().empty() && "Variable name should not be empty");
Shih-wei Liao462aefd2010-06-04 15:32:04 -070085
zonr6315f762010-10-05 15:35:14 +080086 // TODO(zonr): some check on variable
Shih-wei Liao462aefd2010-06-04 15:32:04 -070087
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070088 RSExportType *ET = RSExportType::CreateFromDecl(this, VD);
89 if (!ET)
90 return false;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070091
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070092 RSExportVar *EV = new RSExportVar(this, VD, ET);
93 if (EV == NULL)
94 return false;
95 else
96 mExportVars.push_back(EV);
Shih-wei Liao462aefd2010-06-04 15:32:04 -070097
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070098 return true;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070099}
100
zonr6315f762010-10-05 15:35:14 +0800101bool RSContext::processExportFunc(const clang::FunctionDecl *FD) {
Stephen Hines6e6578a2011-02-07 18:05:48 -0800102 slangAssert(!FD->getName().empty() && "Function name should not be empty");
Shih-wei Liao537446c2010-06-11 16:05:55 -0700103
Stephen Hines3fbe68a2010-11-17 17:28:59 -0800104 if (!FD->isThisDeclarationADefinition()) {
105 return true;
106 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700107
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700108 if (FD->getStorageClass() != clang::SC_None) {
109 fprintf(stderr, "RSContext::processExportFunc : cannot export extern or "
110 "static function '%s'\n", FD->getName().str().c_str());
111 return false;
112 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700113
Stephen Hines9999ec32012-02-10 18:22:14 -0800114 if (RSExportForEach::isSpecialRSFunc(mTargetAPI, FD)) {
115 // Do not reflect specialized functions like init, dtor, or graphics root.
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800116 return RSExportForEach::validateSpecialFuncDecl(mTargetAPI, this, FD);
117 } else if (RSExportForEach::isRSForEachFunc(mTargetAPI, this, FD)) {
Stephen Hines593a8942011-05-10 15:29:50 -0700118 RSExportForEach *EFE = RSExportForEach::Create(this, FD);
119 if (EFE == NULL)
120 return false;
121 else
122 mExportForEach.push_back(EFE);
123 return true;
Stephen Hines3fbe68a2010-11-17 17:28:59 -0800124 }
125
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700126 RSExportFunc *EF = RSExportFunc::Create(this, FD);
127 if (EF == NULL)
128 return false;
129 else
130 mExportFuncs.push_back(EF);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700131
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700132 return true;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700133}
134
135
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700136bool RSContext::processExportType(const llvm::StringRef &Name) {
Stephen Hines9e5b5032010-11-03 13:19:14 -0700137 clang::TranslationUnitDecl *TUDecl = mCtx.getTranslationUnitDecl();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700138
Stephen Hines6e6578a2011-02-07 18:05:48 -0800139 slangAssert(TUDecl != NULL && "Translation unit declaration (top-level "
140 "declaration) is null object");
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700141
Stephen Hines9e5b5032010-11-03 13:19:14 -0700142 const clang::IdentifierInfo *II = mPP.getIdentifierInfo(Name);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700143 if (II == NULL)
zonr6315f762010-10-05 15:35:14 +0800144 // TODO(zonr): alert identifier @Name mark as an exportable type cannot be
145 // found
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700146 return false;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700147
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700148 clang::DeclContext::lookup_const_result R = TUDecl->lookup(II);
149 RSExportType *ET = NULL;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700150
Stephen Hines23c43582013-01-09 20:02:04 -0800151 for (clang::DeclContext::lookup_const_iterator I = R.begin(), E = R.end();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700152 I != E;
153 I++) {
154 clang::NamedDecl *const ND = *I;
155 const clang::Type *T = NULL;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700156
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700157 switch (ND->getKind()) {
158 case clang::Decl::Typedef: {
159 T = static_cast<const clang::TypedefDecl*>(
160 ND)->getCanonicalDecl()->getUnderlyingType().getTypePtr();
161 break;
162 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700163 case clang::Decl::Record: {
164 T = static_cast<const clang::RecordDecl*>(ND)->getTypeForDecl();
165 break;
166 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700167 default: {
168 // unsupported, skip
169 break;
Shih-wei Liaoac918152010-08-29 02:43:24 -0700170 }
171 }
Shih-wei Liao537446c2010-06-11 16:05:55 -0700172
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700173 if (T != NULL)
174 ET = RSExportType::Create(this, T);
175 }
176
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700177 return (ET != NULL);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700178}
179
Stephen Hinesc17e1982012-02-22 12:30:45 -0800180
181// Possibly re-order ForEach exports (maybe generating a dummy "root" function).
182// We require "root" to be listed as slot 0 of our exported compute kernels,
183// so this only needs to be created if we have other non-root kernels.
184void RSContext::cleanupForEach() {
185 bool foundNonRoot = false;
186 ExportForEachList::iterator begin = mExportForEach.begin();
187
188 for (ExportForEachList::iterator I = begin, E = mExportForEach.end();
189 I != E;
190 I++) {
191 RSExportForEach *EFE = *I;
192 if (!EFE->getName().compare("root")) {
193 if (I == begin) {
194 // Nothing to do, since it is the first function
195 return;
196 }
197
198 mExportForEach.erase(I);
199 mExportForEach.push_front(EFE);
200 return;
201 } else {
202 foundNonRoot = true;
203 }
204 }
205
206 // If we found a non-root kernel, but no root() function, we need to add a
207 // dummy version (so that script->script calls of rsForEach don't behave
208 // erratically).
209 if (foundNonRoot) {
210 RSExportForEach *DummyRoot = RSExportForEach::CreateDummyRoot(this);
211 mExportForEach.push_front(DummyRoot);
212 }
213}
214
215
Stephen Hinesc808a992010-11-29 17:20:42 -0800216bool RSContext::processExport() {
Stephen Hinesb6809ed2011-05-09 13:05:04 -0700217 bool valid = true;
Stephen Hines593a8942011-05-10 15:29:50 -0700218
219 if (getDiagnostics()->hasErrorOccurred()) {
220 return false;
221 }
222
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700223 // Export variable
Stephen Hines9e5b5032010-11-03 13:19:14 -0700224 clang::TranslationUnitDecl *TUDecl = mCtx.getTranslationUnitDecl();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700225 for (clang::DeclContext::decl_iterator DI = TUDecl->decls_begin(),
226 DE = TUDecl->decls_end();
Stephen Hinesc97a3332010-11-30 15:31:08 -0800227 DI != DE;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700228 DI++) {
229 if (DI->getKind() == clang::Decl::Var) {
230 clang::VarDecl *VD = (clang::VarDecl*) (*DI);
Stephen Hines44f10062013-06-13 00:33:23 -0700231 if (VD->getFormalLinkage() == clang::ExternalLinkage) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700232 if (!processExportVar(VD)) {
Stephen Hinesb6809ed2011-05-09 13:05:04 -0700233 valid = false;
Ying Wang3f8b44d2010-09-04 01:17:01 -0700234 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700235 }
236 } else if (DI->getKind() == clang::Decl::Function) {
237 // Export functions
238 clang::FunctionDecl *FD = (clang::FunctionDecl*) (*DI);
Stephen Hines44f10062013-06-13 00:33:23 -0700239 if (FD->getFormalLinkage() == clang::ExternalLinkage) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700240 if (!processExportFunc(FD)) {
Stephen Hinesb6809ed2011-05-09 13:05:04 -0700241 valid = false;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700242 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700243 }
Ying Wang3f8b44d2010-09-04 01:17:01 -0700244 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700245 }
Ying Wang3f8b44d2010-09-04 01:17:01 -0700246
Stephen Hinesc17e1982012-02-22 12:30:45 -0800247 if (valid) {
248 cleanupForEach();
249 }
250
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700251 // Finally, export type forcely set to be exported by user
252 for (NeedExportTypeSet::const_iterator EI = mNeedExportTypes.begin(),
253 EE = mNeedExportTypes.end();
254 EI != EE;
255 EI++) {
256 if (!processExportType(EI->getKey())) {
Stephen Hinesb6809ed2011-05-09 13:05:04 -0700257 valid = false;
Ying Wang3f8b44d2010-09-04 01:17:01 -0700258 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700259 }
Shih-wei Liao537446c2010-06-11 16:05:55 -0700260
Stephen Hinesb6809ed2011-05-09 13:05:04 -0700261 return valid;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700262}
263
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700264bool RSContext::insertExportType(const llvm::StringRef &TypeName,
265 RSExportType *ET) {
266 ExportTypeMap::value_type *NewItem =
267 ExportTypeMap::value_type::Create(TypeName.begin(),
268 TypeName.end(),
269 mExportTypes.getAllocator(),
270 ET);
Shih-wei Liao6de89272010-07-15 15:26:20 -0700271
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700272 if (mExportTypes.insert(NewItem)) {
Shih-wei Liao6de89272010-07-15 15:26:20 -0700273 return true;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700274 } else {
275 free(NewItem);
276 return false;
277 }
278}
279
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700280RSContext::~RSContext() {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700281 delete mLicenseNote;
Stephen Hines23c43582013-01-09 20:02:04 -0800282 delete mDataLayout;
Zonr Changa41ce1d2010-10-06 02:23:12 +0800283 for (ExportableList::iterator I = mExportables.begin(),
284 E = mExportables.end();
285 I != E;
286 I++) {
Zonr Chang641558f2010-10-12 21:07:06 +0800287 if (!(*I)->isKeep())
288 delete *I;
Zonr Changa41ce1d2010-10-06 02:23:12 +0800289 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700290}
Stephen Hinese639eb52010-11-08 19:27:20 -0800291
292} // namespace slang