slang_rs_context.cpp revision 3fbe68a4ec20fec25f8a40191437bbc02d00f591
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/Type.h" 25 26#include "clang/Basic/Linkage.h" 27#include "clang/Basic/TargetInfo.h" 28 29#include "clang/Index/ASTLocation.h" 30 31#include "llvm/LLVMContext.h" 32 33#include "llvm/Target/TargetData.h" 34 35#include "slang.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 : mPP(PP), 49 mCtx(Ctx), 50 mTarget(Target), 51 mTargetData(NULL), 52 mLLVMContext(llvm::getGlobalContext()), 53 mExportAllNonStaticVars(true), 54 mExportAllNonStaticFuncs(true), 55 mLicenseNote(NULL) { 56 // For #pragma rs export_var 57 PP.AddPragmaHandler( 58 "rs", RSPragmaHandler::CreatePragmaExportVarHandler(this)); 59 60 // For #pragma rs export_var_all 61 PP.AddPragmaHandler( 62 "rs", RSPragmaHandler::CreatePragmaExportVarAllHandler(this)); 63 64 // For #pragma rs export_func 65 PP.AddPragmaHandler( 66 "rs", RSPragmaHandler::CreatePragmaExportFuncHandler(this)); 67 68 // For #pragma rs export_func_all 69 PP.AddPragmaHandler( 70 "rs", RSPragmaHandler::CreatePragmaExportFuncAllHandler(this)); 71 72 // For #pragma rs export_type 73 PP.AddPragmaHandler( 74 "rs", RSPragmaHandler::CreatePragmaExportTypeHandler(this)); 75 76 // For #pragma rs java_package_name 77 PP.AddPragmaHandler( 78 "rs", RSPragmaHandler::CreatePragmaJavaPackageNameHandler(this)); 79 80 // For #pragma rs set_reflect_license 81 PP.AddPragmaHandler( 82 "rs", RSPragmaHandler::CreatePragmaReflectLicenseHandler(this)); 83 84 // Prepare target data 85 mTargetData = new llvm::TargetData(Target.getTargetDescription()); 86 87 return; 88} 89 90bool RSContext::processExportVar(const clang::VarDecl *VD) { 91 assert(!VD->getName().empty() && "Variable name should not be empty"); 92 93 // TODO(zonr): some check on variable 94 95 RSExportType *ET = RSExportType::CreateFromDecl(this, VD); 96 if (!ET) 97 return false; 98 99 RSExportVar *EV = new RSExportVar(this, VD, ET); 100 if (EV == NULL) 101 return false; 102 else 103 mExportVars.push_back(EV); 104 105 return true; 106} 107 108static bool isSpecialRSFunc(const llvm::StringRef& Name) { 109 static llvm::StringRef FuncInit("init"); 110 static llvm::StringRef FuncRoot("root"); 111 return Name.equals(FuncInit) || Name.equals(FuncRoot); 112} 113 114bool RSContext::processExportFunc(const clang::FunctionDecl *FD) { 115 assert(!FD->getName().empty() && "Function name should not be empty"); 116 117 if (!FD->isThisDeclarationADefinition()) { 118 return true; 119 } 120 121 if (FD->getStorageClass() != clang::SC_None) { 122 fprintf(stderr, "RSContext::processExportFunc : cannot export extern or " 123 "static function '%s'\n", FD->getName().str().c_str()); 124 return false; 125 } 126 127 // Do not reflect specialized RS functions like init/root. 128 if (isSpecialRSFunc(FD->getName())) { 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 assert(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 RSExportPointerType::IntegerType = mCtx.IntTy.getTypePtr(); 158 159 for (clang::DeclContext::lookup_const_iterator I = R.first, E = R.second; 160 I != E; 161 I++) { 162 clang::NamedDecl *const ND = *I; 163 const clang::Type *T = NULL; 164 165 switch (ND->getKind()) { 166 case clang::Decl::Typedef: { 167 T = static_cast<const clang::TypedefDecl*>( 168 ND)->getCanonicalDecl()->getUnderlyingType().getTypePtr(); 169 break; 170 } 171 case clang::Decl::Record: { 172 T = static_cast<const clang::RecordDecl*>(ND)->getTypeForDecl(); 173 break; 174 } 175 default: { 176 // unsupported, skip 177 break; 178 } 179 } 180 181 if (T != NULL) 182 ET = RSExportType::Create(this, T); 183 } 184 185 RSExportPointerType::IntegerType = NULL; 186 187 return (ET != NULL); 188} 189 190void RSContext::processExport() { 191 if (mNeedExportVars.empty() && 192 mNeedExportFuncs.empty() && 193 !mExportAllNonStaticVars && !mExportAllNonStaticFuncs) { 194 fprintf(stderr, "note: No reflection because there are no #pragma " 195 "rs export_var(...), #pragma rs export_func(...), " 196 "#pragma rs export_var_all, or #pragma rs " 197 "export_func_all\n"); 198 } 199 200 // Export variable 201 clang::TranslationUnitDecl *TUDecl = mCtx.getTranslationUnitDecl(); 202 for (clang::DeclContext::decl_iterator DI = TUDecl->decls_begin(), 203 DE = TUDecl->decls_end(); 204 DI != TUDecl->decls_end(); 205 DI++) { 206 if (DI->getKind() == clang::Decl::Var) { 207 clang::VarDecl *VD = (clang::VarDecl*) (*DI); 208 if (mExportAllNonStaticVars && 209 (VD->getLinkage() == clang::ExternalLinkage)) { 210 if (!processExportVar(VD)) { 211 fprintf(stderr, "RSContext::processExport : failed to export var " 212 "'%s'\n", VD->getNameAsString().c_str()); 213 } 214 } else { 215 NeedExportVarSet::iterator EI = mNeedExportVars.find(VD->getName()); 216 if (EI != mNeedExportVars.end() && processExportVar(VD)) { 217 mNeedExportVars.erase(EI); 218 } 219 } 220 } else if (DI->getKind() == clang::Decl::Function) { 221 // Export functions 222 clang::FunctionDecl *FD = (clang::FunctionDecl*) (*DI); 223 if (mExportAllNonStaticFuncs && 224 (FD->getLinkage() == clang::ExternalLinkage)) { 225 if (!processExportFunc(FD)) { 226 fprintf(stderr, "RSContext::processExport : failed to export func " 227 "'%s'\n", FD->getNameAsString().c_str()); 228 } 229 } else { 230 NeedExportFuncSet::iterator EI = mNeedExportFuncs.find(FD->getName()); 231 if (EI != mNeedExportFuncs.end() && processExportFunc(FD)) 232 mNeedExportFuncs.erase(EI); 233 } 234 } 235 } 236 237 // Finally, export type forcely set to be exported by user 238 for (NeedExportTypeSet::const_iterator EI = mNeedExportTypes.begin(), 239 EE = mNeedExportTypes.end(); 240 EI != EE; 241 EI++) { 242 if (!processExportType(EI->getKey())) { 243 fprintf(stderr, "RSContext::processExport : failed to export type " 244 "'%s'\n", EI->getKey().str().c_str()); 245 } 246 } 247 248 return; 249} 250 251bool RSContext::insertExportType(const llvm::StringRef &TypeName, 252 RSExportType *ET) { 253 ExportTypeMap::value_type *NewItem = 254 ExportTypeMap::value_type::Create(TypeName.begin(), 255 TypeName.end(), 256 mExportTypes.getAllocator(), 257 ET); 258 259 if (mExportTypes.insert(NewItem)) { 260 return true; 261 } else { 262 free(NewItem); 263 return false; 264 } 265} 266 267bool RSContext::reflectToJava(const std::string &OutputPathBase, 268 const std::string &OutputPackageName, 269 const std::string &InputFileName, 270 const std::string &OutputBCFileName, 271 std::string *RealPackageName) { 272 if (RealPackageName != NULL) 273 RealPackageName->clear(); 274 275 const std::string &PackageName = 276 ((OutputPackageName.empty()) ? mReflectJavaPackageName : 277 OutputPackageName); 278 if (PackageName.empty()) 279 // no package name, just return 280 return true; 281 282 // Copy back the really applied package name 283 RealPackageName->assign(PackageName); 284 285 RSReflection *R = new RSReflection(this); 286 bool ret = R->reflect(OutputPathBase, PackageName, 287 InputFileName, OutputBCFileName); 288 if (!ret) 289 fprintf(stderr, "RSContext::reflectToJava : failed to do reflection " 290 "(%s)\n", R->getLastError()); 291 delete R; 292 return ret; 293} 294 295RSContext::~RSContext() { 296 delete mLicenseNote; 297 delete mTargetData; 298 for (ExportableList::iterator I = mExportables.begin(), 299 E = mExportables.end(); 300 I != E; 301 I++) { 302 if (!(*I)->isKeep()) 303 delete *I; 304 } 305} 306 307} // namespace slang 308