blob: bef4766aafb4dcd93d6d463264ebfbbd7b1dc58c [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
Jean-Luc Brouillet5210d102014-07-07 17:36:07 -070064 AddPragmaHandlers(PP, this);
Stephen Hines96ab06c2011-01-05 15:29:26 -080065
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070066 // Prepare target data
Stephen Hines23c43582013-01-09 20:02:04 -080067 mDataLayout = new llvm::DataLayout(Target.getTargetDescription());
Shih-wei Liao462aefd2010-06-04 15:32:04 -070068}
69
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070070bool RSContext::processExportVar(const clang::VarDecl *VD) {
Stephen Hines6e6578a2011-02-07 18:05:48 -080071 slangAssert(!VD->getName().empty() && "Variable name should not be empty");
Shih-wei Liao462aefd2010-06-04 15:32:04 -070072
zonr6315f762010-10-05 15:35:14 +080073 // TODO(zonr): some check on variable
Shih-wei Liao462aefd2010-06-04 15:32:04 -070074
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070075 RSExportType *ET = RSExportType::CreateFromDecl(this, VD);
76 if (!ET)
77 return false;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070078
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070079 RSExportVar *EV = new RSExportVar(this, VD, ET);
80 if (EV == NULL)
81 return false;
82 else
83 mExportVars.push_back(EV);
Shih-wei Liao462aefd2010-06-04 15:32:04 -070084
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070085 return true;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070086}
87
zonr6315f762010-10-05 15:35:14 +080088bool RSContext::processExportFunc(const clang::FunctionDecl *FD) {
Stephen Hines6e6578a2011-02-07 18:05:48 -080089 slangAssert(!FD->getName().empty() && "Function name should not be empty");
Shih-wei Liao537446c2010-06-11 16:05:55 -070090
Stephen Hines3fbe68a2010-11-17 17:28:59 -080091 if (!FD->isThisDeclarationADefinition()) {
92 return true;
93 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -070094
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070095 if (FD->getStorageClass() != clang::SC_None) {
96 fprintf(stderr, "RSContext::processExportFunc : cannot export extern or "
97 "static function '%s'\n", FD->getName().str().c_str());
98 return false;
99 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700100
Stephen Hines9999ec32012-02-10 18:22:14 -0800101 if (RSExportForEach::isSpecialRSFunc(mTargetAPI, FD)) {
102 // Do not reflect specialized functions like init, dtor, or graphics root.
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800103 return RSExportForEach::validateSpecialFuncDecl(mTargetAPI, this, FD);
104 } else if (RSExportForEach::isRSForEachFunc(mTargetAPI, this, FD)) {
Stephen Hines593a8942011-05-10 15:29:50 -0700105 RSExportForEach *EFE = RSExportForEach::Create(this, FD);
106 if (EFE == NULL)
107 return false;
108 else
109 mExportForEach.push_back(EFE);
110 return true;
Stephen Hines3fbe68a2010-11-17 17:28:59 -0800111 }
112
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700113 RSExportFunc *EF = RSExportFunc::Create(this, FD);
114 if (EF == NULL)
115 return false;
116 else
117 mExportFuncs.push_back(EF);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700118
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700119 return true;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700120}
121
122
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700123bool RSContext::processExportType(const llvm::StringRef &Name) {
Stephen Hines9e5b5032010-11-03 13:19:14 -0700124 clang::TranslationUnitDecl *TUDecl = mCtx.getTranslationUnitDecl();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700125
Stephen Hines6e6578a2011-02-07 18:05:48 -0800126 slangAssert(TUDecl != NULL && "Translation unit declaration (top-level "
127 "declaration) is null object");
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700128
Stephen Hines9e5b5032010-11-03 13:19:14 -0700129 const clang::IdentifierInfo *II = mPP.getIdentifierInfo(Name);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700130 if (II == NULL)
zonr6315f762010-10-05 15:35:14 +0800131 // TODO(zonr): alert identifier @Name mark as an exportable type cannot be
132 // found
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700133 return false;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700134
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700135 clang::DeclContext::lookup_const_result R = TUDecl->lookup(II);
136 RSExportType *ET = NULL;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700137
Stephen Hines23c43582013-01-09 20:02:04 -0800138 for (clang::DeclContext::lookup_const_iterator I = R.begin(), E = R.end();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700139 I != E;
140 I++) {
141 clang::NamedDecl *const ND = *I;
142 const clang::Type *T = NULL;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700143
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700144 switch (ND->getKind()) {
145 case clang::Decl::Typedef: {
146 T = static_cast<const clang::TypedefDecl*>(
147 ND)->getCanonicalDecl()->getUnderlyingType().getTypePtr();
148 break;
149 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700150 case clang::Decl::Record: {
151 T = static_cast<const clang::RecordDecl*>(ND)->getTypeForDecl();
152 break;
153 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700154 default: {
155 // unsupported, skip
156 break;
Shih-wei Liaoac918152010-08-29 02:43:24 -0700157 }
158 }
Shih-wei Liao537446c2010-06-11 16:05:55 -0700159
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700160 if (T != NULL)
161 ET = RSExportType::Create(this, T);
162 }
163
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700164 return (ET != NULL);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700165}
166
Stephen Hinesc17e1982012-02-22 12:30:45 -0800167
168// Possibly re-order ForEach exports (maybe generating a dummy "root" function).
169// We require "root" to be listed as slot 0 of our exported compute kernels,
170// so this only needs to be created if we have other non-root kernels.
171void RSContext::cleanupForEach() {
172 bool foundNonRoot = false;
173 ExportForEachList::iterator begin = mExportForEach.begin();
174
175 for (ExportForEachList::iterator I = begin, E = mExportForEach.end();
176 I != E;
177 I++) {
178 RSExportForEach *EFE = *I;
179 if (!EFE->getName().compare("root")) {
180 if (I == begin) {
181 // Nothing to do, since it is the first function
182 return;
183 }
184
185 mExportForEach.erase(I);
186 mExportForEach.push_front(EFE);
187 return;
188 } else {
189 foundNonRoot = true;
190 }
191 }
192
193 // If we found a non-root kernel, but no root() function, we need to add a
194 // dummy version (so that script->script calls of rsForEach don't behave
195 // erratically).
196 if (foundNonRoot) {
197 RSExportForEach *DummyRoot = RSExportForEach::CreateDummyRoot(this);
198 mExportForEach.push_front(DummyRoot);
199 }
200}
201
202
Stephen Hinesc808a992010-11-29 17:20:42 -0800203bool RSContext::processExport() {
Stephen Hinesb6809ed2011-05-09 13:05:04 -0700204 bool valid = true;
Stephen Hines593a8942011-05-10 15:29:50 -0700205
206 if (getDiagnostics()->hasErrorOccurred()) {
207 return false;
208 }
209
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700210 // Export variable
Stephen Hines9e5b5032010-11-03 13:19:14 -0700211 clang::TranslationUnitDecl *TUDecl = mCtx.getTranslationUnitDecl();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700212 for (clang::DeclContext::decl_iterator DI = TUDecl->decls_begin(),
213 DE = TUDecl->decls_end();
Stephen Hinesc97a3332010-11-30 15:31:08 -0800214 DI != DE;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700215 DI++) {
216 if (DI->getKind() == clang::Decl::Var) {
217 clang::VarDecl *VD = (clang::VarDecl*) (*DI);
Stephen Hines44f10062013-06-13 00:33:23 -0700218 if (VD->getFormalLinkage() == clang::ExternalLinkage) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700219 if (!processExportVar(VD)) {
Stephen Hinesb6809ed2011-05-09 13:05:04 -0700220 valid = false;
Ying Wang3f8b44d2010-09-04 01:17:01 -0700221 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700222 }
223 } else if (DI->getKind() == clang::Decl::Function) {
224 // Export functions
225 clang::FunctionDecl *FD = (clang::FunctionDecl*) (*DI);
Stephen Hines44f10062013-06-13 00:33:23 -0700226 if (FD->getFormalLinkage() == clang::ExternalLinkage) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700227 if (!processExportFunc(FD)) {
Stephen Hinesb6809ed2011-05-09 13:05:04 -0700228 valid = false;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700229 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700230 }
Ying Wang3f8b44d2010-09-04 01:17:01 -0700231 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700232 }
Ying Wang3f8b44d2010-09-04 01:17:01 -0700233
Stephen Hinesc17e1982012-02-22 12:30:45 -0800234 if (valid) {
235 cleanupForEach();
236 }
237
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700238 // Finally, export type forcely set to be exported by user
239 for (NeedExportTypeSet::const_iterator EI = mNeedExportTypes.begin(),
240 EE = mNeedExportTypes.end();
241 EI != EE;
242 EI++) {
243 if (!processExportType(EI->getKey())) {
Stephen Hinesb6809ed2011-05-09 13:05:04 -0700244 valid = false;
Ying Wang3f8b44d2010-09-04 01:17:01 -0700245 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700246 }
Shih-wei Liao537446c2010-06-11 16:05:55 -0700247
Stephen Hinesb6809ed2011-05-09 13:05:04 -0700248 return valid;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700249}
250
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700251bool RSContext::insertExportType(const llvm::StringRef &TypeName,
252 RSExportType *ET) {
253 ExportTypeMap::value_type *NewItem =
Stephen Hines7ac9d0d2014-07-15 16:50:03 -0700254 ExportTypeMap::value_type::Create(TypeName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700255 mExportTypes.getAllocator(),
256 ET);
Shih-wei Liao6de89272010-07-15 15:26:20 -0700257
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700258 if (mExportTypes.insert(NewItem)) {
Shih-wei Liao6de89272010-07-15 15:26:20 -0700259 return true;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700260 } else {
261 free(NewItem);
262 return false;
263 }
264}
265
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700266RSContext::~RSContext() {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700267 delete mLicenseNote;
Stephen Hines23c43582013-01-09 20:02:04 -0800268 delete mDataLayout;
Zonr Changa41ce1d2010-10-06 02:23:12 +0800269 for (ExportableList::iterator I = mExportables.begin(),
270 E = mExportables.end();
271 I != E;
272 I++) {
Zonr Chang641558f2010-10-12 21:07:06 +0800273 if (!(*I)->isKeep())
274 delete *I;
Zonr Changa41ce1d2010-10-06 02:23:12 +0800275 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700276}
Stephen Hinese639eb52010-11-08 19:27:20 -0800277
278} // namespace slang