slang_rs_context.cpp revision b5a89fbfcba6d8817c1c3700ed78bd6482cf1a5d
1/* 2 * Copyright 2010, 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 "clang/Index/ASTLocation.h" 31 32#include "llvm/LLVMContext.h" 33 34#include "llvm/Target/TargetData.h" 35 36#include "slang.h" 37#include "slang_assert.h" 38#include "slang_rs_export_foreach.h" 39#include "slang_rs_export_func.h" 40#include "slang_rs_export_type.h" 41#include "slang_rs_export_var.h" 42#include "slang_rs_exportable.h" 43#include "slang_rs_pragma_handler.h" 44#include "slang_rs_reflection.h" 45 46namespace slang { 47 48RSContext::RSContext(clang::Preprocessor &PP, 49 clang::ASTContext &Ctx, 50 const clang::TargetInfo &Target, 51 PragmaList *Pragmas, 52 std::vector<std::string> *GeneratedFileNames) 53 : mPP(PP), 54 mCtx(Ctx), 55 mTarget(Target), 56 mPragmas(Pragmas), 57 mGeneratedFileNames(GeneratedFileNames), 58 mTargetData(NULL), 59 mLLVMContext(llvm::getGlobalContext()), 60 mLicenseNote(NULL), 61 version(0), 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 mTargetData = new llvm::TargetData(Target.getTargetDescription()); 82 83 return; 84} 85 86bool RSContext::processExportVar(const clang::VarDecl *VD) { 87 slangAssert(!VD->getName().empty() && "Variable name should not be empty"); 88 89 // TODO(zonr): some check on variable 90 91 RSExportType *ET = RSExportType::CreateFromDecl(this, VD); 92 if (!ET) 93 return false; 94 95 RSExportVar *EV = new RSExportVar(this, VD, ET); 96 if (EV == NULL) 97 return false; 98 else 99 mExportVars.push_back(EV); 100 101 return true; 102} 103 104bool RSContext::processExportFunc(const clang::FunctionDecl *FD) { 105 slangAssert(!FD->getName().empty() && "Function name should not be empty"); 106 107 if (!FD->isThisDeclarationADefinition()) { 108 return true; 109 } 110 111 if (FD->getStorageClass() != clang::SC_None) { 112 fprintf(stderr, "RSContext::processExportFunc : cannot export extern or " 113 "static function '%s'\n", FD->getName().str().c_str()); 114 return false; 115 } 116 117 if (RSExportForEach::isRSForEachFunc(FD)) { 118 RSExportForEach *EFE = RSExportForEach::Create(this, FD); 119 if (EFE == NULL) 120 return false; 121 else 122 mExportForEach.push_back(EFE); 123 return true; 124 } else if (RSExportForEach::isSpecialRSFunc(FD)) { 125 // Do not reflect specialized RS functions like init or graphics root. 126 if (!RSExportForEach::validateSpecialFuncDecl(getDiagnostics(), FD)) { 127 return false; 128 } 129 return true; 130 } 131 132 RSExportFunc *EF = RSExportFunc::Create(this, FD); 133 if (EF == NULL) 134 return false; 135 else 136 mExportFuncs.push_back(EF); 137 138 return true; 139} 140 141 142bool RSContext::processExportType(const llvm::StringRef &Name) { 143 clang::TranslationUnitDecl *TUDecl = mCtx.getTranslationUnitDecl(); 144 145 slangAssert(TUDecl != NULL && "Translation unit declaration (top-level " 146 "declaration) is null object"); 147 148 const clang::IdentifierInfo *II = mPP.getIdentifierInfo(Name); 149 if (II == NULL) 150 // TODO(zonr): alert identifier @Name mark as an exportable type cannot be 151 // found 152 return false; 153 154 clang::DeclContext::lookup_const_result R = TUDecl->lookup(II); 155 RSExportType *ET = NULL; 156 157 for (clang::DeclContext::lookup_const_iterator I = R.first, E = R.second; 158 I != E; 159 I++) { 160 clang::NamedDecl *const ND = *I; 161 const clang::Type *T = NULL; 162 163 switch (ND->getKind()) { 164 case clang::Decl::Typedef: { 165 T = static_cast<const clang::TypedefDecl*>( 166 ND)->getCanonicalDecl()->getUnderlyingType().getTypePtr(); 167 break; 168 } 169 case clang::Decl::Record: { 170 T = static_cast<const clang::RecordDecl*>(ND)->getTypeForDecl(); 171 break; 172 } 173 default: { 174 // unsupported, skip 175 break; 176 } 177 } 178 179 if (T != NULL) 180 ET = RSExportType::Create(this, T); 181 } 182 183 return (ET != NULL); 184} 185 186bool RSContext::processExport() { 187 bool valid = true; 188 189 if (getDiagnostics()->hasErrorOccurred()) { 190 return false; 191 } 192 193 // Export variable 194 clang::TranslationUnitDecl *TUDecl = mCtx.getTranslationUnitDecl(); 195 for (clang::DeclContext::decl_iterator DI = TUDecl->decls_begin(), 196 DE = TUDecl->decls_end(); 197 DI != DE; 198 DI++) { 199 if (DI->getKind() == clang::Decl::Var) { 200 clang::VarDecl *VD = (clang::VarDecl*) (*DI); 201 if (VD->getLinkage() == clang::ExternalLinkage) { 202 if (!processExportVar(VD)) { 203 valid = false; 204 } 205 } 206 } else if (DI->getKind() == clang::Decl::Function) { 207 // Export functions 208 clang::FunctionDecl *FD = (clang::FunctionDecl*) (*DI); 209 if (FD->getLinkage() == clang::ExternalLinkage) { 210 if (!processExportFunc(FD)) { 211 valid = false; 212 } 213 } 214 } 215 } 216 217 // Finally, export type forcely set to be exported by user 218 for (NeedExportTypeSet::const_iterator EI = mNeedExportTypes.begin(), 219 EE = mNeedExportTypes.end(); 220 EI != EE; 221 EI++) { 222 if (!processExportType(EI->getKey())) { 223 valid = false; 224 } 225 } 226 227 return valid; 228} 229 230bool RSContext::insertExportType(const llvm::StringRef &TypeName, 231 RSExportType *ET) { 232 ExportTypeMap::value_type *NewItem = 233 ExportTypeMap::value_type::Create(TypeName.begin(), 234 TypeName.end(), 235 mExportTypes.getAllocator(), 236 ET); 237 238 if (mExportTypes.insert(NewItem)) { 239 return true; 240 } else { 241 free(NewItem); 242 return false; 243 } 244} 245 246bool RSContext::reflectToJava(const std::string &OutputPathBase, 247 const std::string &OutputPackageName, 248 const std::string &InputFileName, 249 const std::string &OutputBCFileName, 250 std::string *RealPackageName) { 251 if (RealPackageName != NULL) 252 RealPackageName->clear(); 253 254 const std::string &PackageName = 255 ((OutputPackageName.empty()) ? mReflectJavaPackageName : 256 OutputPackageName); 257 if (PackageName.empty()) { 258 std::cerr << "Error: Missing \"#pragma rs " 259 << "java_package_name(com.foo.bar)\" in " 260 << InputFileName << std::endl; 261 return false; 262 } 263 264 // Copy back the really applied package name 265 RealPackageName->assign(PackageName); 266 267 RSReflection *R = new RSReflection(this, mGeneratedFileNames); 268 bool ret = R->reflect(OutputPathBase, PackageName, 269 InputFileName, OutputBCFileName); 270 if (!ret) 271 fprintf(stderr, "RSContext::reflectToJava : failed to do reflection " 272 "(%s)\n", R->getLastError()); 273 delete R; 274 return ret; 275} 276 277RSContext::~RSContext() { 278 delete mLicenseNote; 279 delete mTargetData; 280 for (ExportableList::iterator I = mExportables.begin(), 281 E = mExportables.end(); 282 I != E; 283 I++) { 284 if (!(*I)->isKeep()) 285 delete *I; 286 } 287} 288 289} // namespace slang 290