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