slang_rs_context.cpp revision 2ef9bc0cfbca2152d972c0975005f8c897c2a42c
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 mLicenseNote(NULL) { 54 // For #pragma rs export_type 55 PP.AddPragmaHandler( 56 "rs", RSPragmaHandler::CreatePragmaExportTypeHandler(this)); 57 58 // For #pragma rs java_package_name 59 PP.AddPragmaHandler( 60 "rs", RSPragmaHandler::CreatePragmaJavaPackageNameHandler(this)); 61 62 // For #pragma rs set_reflect_license 63 PP.AddPragmaHandler( 64 "rs", RSPragmaHandler::CreatePragmaReflectLicenseHandler(this)); 65 66 // Prepare target data 67 mTargetData = new llvm::TargetData(Target.getTargetDescription()); 68 69 return; 70} 71 72bool RSContext::processExportVar(const clang::VarDecl *VD) { 73 assert(!VD->getName().empty() && "Variable name should not be empty"); 74 75 // TODO(zonr): some check on variable 76 77 RSExportType *ET = RSExportType::CreateFromDecl(this, VD); 78 if (!ET) 79 return false; 80 81 RSExportVar *EV = new RSExportVar(this, VD, ET); 82 if (EV == NULL) 83 return false; 84 else 85 mExportVars.push_back(EV); 86 87 return true; 88} 89 90static bool isSpecialRSFunc(const llvm::StringRef& Name) { 91 static llvm::StringRef FuncInit("init"); 92 static llvm::StringRef FuncRoot("root"); 93 return Name.equals(FuncInit) || Name.equals(FuncRoot); 94} 95 96bool RSContext::processExportFunc(const clang::FunctionDecl *FD) { 97 assert(!FD->getName().empty() && "Function name should not be empty"); 98 99 if (!FD->isThisDeclarationADefinition()) { 100 return true; 101 } 102 103 if (FD->getStorageClass() != clang::SC_None) { 104 fprintf(stderr, "RSContext::processExportFunc : cannot export extern or " 105 "static function '%s'\n", FD->getName().str().c_str()); 106 return false; 107 } 108 109 // Do not reflect specialized RS functions like init/root. 110 if (isSpecialRSFunc(FD->getName())) { 111 return true; 112 } 113 114 RSExportFunc *EF = RSExportFunc::Create(this, FD); 115 if (EF == NULL) 116 return false; 117 else 118 mExportFuncs.push_back(EF); 119 120 return true; 121} 122 123 124bool RSContext::processExportType(const llvm::StringRef &Name) { 125 clang::TranslationUnitDecl *TUDecl = mCtx.getTranslationUnitDecl(); 126 127 assert(TUDecl != NULL && "Translation unit declaration (top-level " 128 "declaration) is null object"); 129 130 const clang::IdentifierInfo *II = mPP.getIdentifierInfo(Name); 131 if (II == NULL) 132 // TODO(zonr): alert identifier @Name mark as an exportable type cannot be 133 // found 134 return false; 135 136 clang::DeclContext::lookup_const_result R = TUDecl->lookup(II); 137 RSExportType *ET = NULL; 138 139 for (clang::DeclContext::lookup_const_iterator I = R.first, E = R.second; 140 I != E; 141 I++) { 142 clang::NamedDecl *const ND = *I; 143 const clang::Type *T = NULL; 144 145 switch (ND->getKind()) { 146 case clang::Decl::Typedef: { 147 T = static_cast<const clang::TypedefDecl*>( 148 ND)->getCanonicalDecl()->getUnderlyingType().getTypePtr(); 149 break; 150 } 151 case clang::Decl::Record: { 152 T = static_cast<const clang::RecordDecl*>(ND)->getTypeForDecl(); 153 break; 154 } 155 default: { 156 // unsupported, skip 157 break; 158 } 159 } 160 161 if (T != NULL) 162 ET = RSExportType::Create(this, T); 163 } 164 165 return (ET != NULL); 166} 167 168bool RSContext::processExport() { 169 // Export variable 170 clang::TranslationUnitDecl *TUDecl = mCtx.getTranslationUnitDecl(); 171 for (clang::DeclContext::decl_iterator DI = TUDecl->decls_begin(), 172 DE = TUDecl->decls_end(); 173 DI != DE; 174 DI++) { 175 if (DI->getKind() == clang::Decl::Var) { 176 clang::VarDecl *VD = (clang::VarDecl*) (*DI); 177 if (VD->getLinkage() == clang::ExternalLinkage) { 178 if (!processExportVar(VD)) { 179 fprintf(stderr, "RSContext::processExport : failed to export var " 180 "'%s'\n", VD->getNameAsString().c_str()); 181 return false; 182 } 183 } 184 } else if (DI->getKind() == clang::Decl::Function) { 185 // Export functions 186 clang::FunctionDecl *FD = (clang::FunctionDecl*) (*DI); 187 if (FD->getLinkage() == clang::ExternalLinkage) { 188 if (!processExportFunc(FD)) { 189 fprintf(stderr, "RSContext::processExport : failed to export func " 190 "'%s'\n", FD->getNameAsString().c_str()); 191 return false; 192 } 193 } 194 } 195 } 196 197 // Finally, export type forcely set to be exported by user 198 for (NeedExportTypeSet::const_iterator EI = mNeedExportTypes.begin(), 199 EE = mNeedExportTypes.end(); 200 EI != EE; 201 EI++) { 202 if (!processExportType(EI->getKey())) { 203 fprintf(stderr, "RSContext::processExport : failed to export type " 204 "'%s'\n", EI->getKey().str().c_str()); 205 return false; 206 } 207 } 208 209 return true; 210} 211 212bool RSContext::insertExportType(const llvm::StringRef &TypeName, 213 RSExportType *ET) { 214 ExportTypeMap::value_type *NewItem = 215 ExportTypeMap::value_type::Create(TypeName.begin(), 216 TypeName.end(), 217 mExportTypes.getAllocator(), 218 ET); 219 220 if (mExportTypes.insert(NewItem)) { 221 return true; 222 } else { 223 free(NewItem); 224 return false; 225 } 226} 227 228bool RSContext::reflectToJava(const std::string &OutputPathBase, 229 const std::string &OutputPackageName, 230 const std::string &InputFileName, 231 const std::string &OutputBCFileName, 232 std::string *RealPackageName) { 233 if (RealPackageName != NULL) 234 RealPackageName->clear(); 235 236 const std::string &PackageName = 237 ((OutputPackageName.empty()) ? mReflectJavaPackageName : 238 OutputPackageName); 239 if (PackageName.empty()) { 240 std::cerr << "Error: Missing \"#pragma rs " 241 << "java_package_name(com.foo.bar)\" in " 242 << InputFileName << std::endl; 243 return false; 244 } 245 246 // Copy back the really applied package name 247 RealPackageName->assign(PackageName); 248 249 RSReflection *R = new RSReflection(this); 250 bool ret = R->reflect(OutputPathBase, PackageName, 251 InputFileName, OutputBCFileName); 252 if (!ret) 253 fprintf(stderr, "RSContext::reflectToJava : failed to do reflection " 254 "(%s)\n", R->getLastError()); 255 delete R; 256 return ret; 257} 258 259RSContext::~RSContext() { 260 delete mLicenseNote; 261 delete mTargetData; 262 for (ExportableList::iterator I = mExportables.begin(), 263 E = mExportables.end(); 264 I != E; 265 I++) { 266 if (!(*I)->isKeep()) 267 delete *I; 268 } 269} 270 271} // namespace slang 272