slang_rs_context.cpp revision cc1b9699446aea20773e4c3c6ff5759fedd8ab51
1/* 2 * Copyright 2010-2012, The Android Open Source Project 3 * 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 17#include "slang_rs_context.h" 18 19#include <string> 20 21#include "clang/AST/ASTContext.h" 22#include "clang/AST/Decl.h" 23#include "clang/AST/DeclBase.h" 24#include "clang/AST/Mangle.h" 25#include "clang/AST/Type.h" 26 27#include "clang/Basic/Linkage.h" 28#include "clang/Basic/TargetInfo.h" 29 30#include "llvm/IR/LLVMContext.h" 31#include "llvm/IR/DataLayout.h" 32 33#include "slang.h" 34#include "slang_assert.h" 35#include "slang_rs_export_foreach.h" 36#include "slang_rs_export_func.h" 37#include "slang_rs_export_type.h" 38#include "slang_rs_export_var.h" 39#include "slang_rs_exportable.h" 40#include "slang_rs_pragma_handler.h" 41#include "slang_rs_reflection.h" 42 43namespace slang { 44 45RSContext::RSContext(clang::Preprocessor &PP, 46 clang::ASTContext &Ctx, 47 const clang::TargetInfo &Target, 48 PragmaList *Pragmas, 49 unsigned int TargetAPI, 50 std::vector<std::string> *GeneratedFileNames) 51 : mPP(PP), 52 mCtx(Ctx), 53 mPragmas(Pragmas), 54 mTargetAPI(TargetAPI), 55 mGeneratedFileNames(GeneratedFileNames), 56 mDataLayout(NULL), 57 mLLVMContext(llvm::getGlobalContext()), 58 mLicenseNote(NULL), 59 mRSPackageName("android.renderscript"), 60 version(0), 61 mIsCompatLib(false), 62 mMangleCtx(Ctx.createMangleContext()) { 63 slangAssert(mGeneratedFileNames && "Must supply GeneratedFileNames"); 64 65 // For #pragma rs export_type 66 PP.AddPragmaHandler( 67 "rs", RSPragmaHandler::CreatePragmaExportTypeHandler(this)); 68 69 // For #pragma rs java_package_name 70 PP.AddPragmaHandler( 71 "rs", RSPragmaHandler::CreatePragmaJavaPackageNameHandler(this)); 72 73 // For #pragma rs set_reflect_license 74 PP.AddPragmaHandler( 75 "rs", RSPragmaHandler::CreatePragmaReflectLicenseHandler(this)); 76 77 // For #pragma version 78 PP.AddPragmaHandler(RSPragmaHandler::CreatePragmaVersionHandler(this)); 79 80 // Prepare target data 81 mDataLayout = new llvm::DataLayout(Target.getTargetDescription()); 82} 83 84bool RSContext::processExportVar(const clang::VarDecl *VD) { 85 slangAssert(!VD->getName().empty() && "Variable name should not be empty"); 86 87 // TODO(zonr): some check on variable 88 89 RSExportType *ET = RSExportType::CreateFromDecl(this, VD); 90 if (!ET) 91 return false; 92 93 RSExportVar *EV = new RSExportVar(this, VD, ET); 94 if (EV == NULL) 95 return false; 96 else 97 mExportVars.push_back(EV); 98 99 return true; 100} 101 102bool RSContext::processExportFunc(const clang::FunctionDecl *FD) { 103 slangAssert(!FD->getName().empty() && "Function name should not be empty"); 104 105 if (!FD->isThisDeclarationADefinition()) { 106 return true; 107 } 108 109 if (FD->getStorageClass() != clang::SC_None) { 110 fprintf(stderr, "RSContext::processExportFunc : cannot export extern or " 111 "static function '%s'\n", FD->getName().str().c_str()); 112 return false; 113 } 114 115 if (RSExportForEach::isSpecialRSFunc(mTargetAPI, FD)) { 116 // Do not reflect specialized functions like init, dtor, or graphics root. 117 return RSExportForEach::validateSpecialFuncDecl(mTargetAPI, this, FD); 118 } else if (RSExportForEach::isRSForEachFunc(mTargetAPI, this, FD)) { 119 RSExportForEach *EFE = RSExportForEach::Create(this, FD); 120 if (EFE == NULL) 121 return false; 122 else 123 mExportForEach.push_back(EFE); 124 return true; 125 } 126 127 RSExportFunc *EF = RSExportFunc::Create(this, FD); 128 if (EF == NULL) 129 return false; 130 else 131 mExportFuncs.push_back(EF); 132 133 return true; 134} 135 136 137bool RSContext::processExportType(const llvm::StringRef &Name) { 138 clang::TranslationUnitDecl *TUDecl = mCtx.getTranslationUnitDecl(); 139 140 slangAssert(TUDecl != NULL && "Translation unit declaration (top-level " 141 "declaration) is null object"); 142 143 const clang::IdentifierInfo *II = mPP.getIdentifierInfo(Name); 144 if (II == NULL) 145 // TODO(zonr): alert identifier @Name mark as an exportable type cannot be 146 // found 147 return false; 148 149 clang::DeclContext::lookup_const_result R = TUDecl->lookup(II); 150 RSExportType *ET = NULL; 151 152 for (clang::DeclContext::lookup_const_iterator I = R.begin(), E = R.end(); 153 I != E; 154 I++) { 155 clang::NamedDecl *const ND = *I; 156 const clang::Type *T = NULL; 157 158 switch (ND->getKind()) { 159 case clang::Decl::Typedef: { 160 T = static_cast<const clang::TypedefDecl*>( 161 ND)->getCanonicalDecl()->getUnderlyingType().getTypePtr(); 162 break; 163 } 164 case clang::Decl::Record: { 165 T = static_cast<const clang::RecordDecl*>(ND)->getTypeForDecl(); 166 break; 167 } 168 default: { 169 // unsupported, skip 170 break; 171 } 172 } 173 174 if (T != NULL) 175 ET = RSExportType::Create(this, T); 176 } 177 178 return (ET != NULL); 179} 180 181 182// Possibly re-order ForEach exports (maybe generating a dummy "root" function). 183// We require "root" to be listed as slot 0 of our exported compute kernels, 184// so this only needs to be created if we have other non-root kernels. 185void RSContext::cleanupForEach() { 186 bool foundNonRoot = false; 187 ExportForEachList::iterator begin = mExportForEach.begin(); 188 189 for (ExportForEachList::iterator I = begin, E = mExportForEach.end(); 190 I != E; 191 I++) { 192 RSExportForEach *EFE = *I; 193 if (!EFE->getName().compare("root")) { 194 if (I == begin) { 195 // Nothing to do, since it is the first function 196 return; 197 } 198 199 mExportForEach.erase(I); 200 mExportForEach.push_front(EFE); 201 return; 202 } else { 203 foundNonRoot = true; 204 } 205 } 206 207 // If we found a non-root kernel, but no root() function, we need to add a 208 // dummy version (so that script->script calls of rsForEach don't behave 209 // erratically). 210 if (foundNonRoot) { 211 RSExportForEach *DummyRoot = RSExportForEach::CreateDummyRoot(this); 212 mExportForEach.push_front(DummyRoot); 213 } 214} 215 216 217bool RSContext::processExport() { 218 bool valid = true; 219 220 if (getDiagnostics()->hasErrorOccurred()) { 221 return false; 222 } 223 224 // Export variable 225 clang::TranslationUnitDecl *TUDecl = mCtx.getTranslationUnitDecl(); 226 for (clang::DeclContext::decl_iterator DI = TUDecl->decls_begin(), 227 DE = TUDecl->decls_end(); 228 DI != DE; 229 DI++) { 230 if (DI->getKind() == clang::Decl::Var) { 231 clang::VarDecl *VD = (clang::VarDecl*) (*DI); 232 if (VD->getFormalLinkage() == clang::ExternalLinkage) { 233 if (!processExportVar(VD)) { 234 valid = false; 235 } 236 } 237 } else if (DI->getKind() == clang::Decl::Function) { 238 // Export functions 239 clang::FunctionDecl *FD = (clang::FunctionDecl*) (*DI); 240 if (FD->getFormalLinkage() == clang::ExternalLinkage) { 241 if (!processExportFunc(FD)) { 242 valid = false; 243 } 244 } 245 } 246 } 247 248 if (valid) { 249 cleanupForEach(); 250 } 251 252 // Finally, export type forcely set to be exported by user 253 for (NeedExportTypeSet::const_iterator EI = mNeedExportTypes.begin(), 254 EE = mNeedExportTypes.end(); 255 EI != EE; 256 EI++) { 257 if (!processExportType(EI->getKey())) { 258 valid = false; 259 } 260 } 261 262 return valid; 263} 264 265bool RSContext::insertExportType(const llvm::StringRef &TypeName, 266 RSExportType *ET) { 267 ExportTypeMap::value_type *NewItem = 268 ExportTypeMap::value_type::Create(TypeName.begin(), 269 TypeName.end(), 270 mExportTypes.getAllocator(), 271 ET); 272 273 if (mExportTypes.insert(NewItem)) { 274 return true; 275 } else { 276 free(NewItem); 277 return false; 278 } 279} 280 281RSContext::~RSContext() { 282 delete mLicenseNote; 283 delete mDataLayout; 284 for (ExportableList::iterator I = mExportables.begin(), 285 E = mExportables.end(); 286 I != E; 287 I++) { 288 if (!(*I)->isKeep()) 289 delete *I; 290 } 291} 292 293} // namespace slang 294