Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1 | #include "slang.hpp" |
| 2 | #include "slang_rs_context.hpp" |
| 3 | #include "slang_rs_reflection.hpp" |
| 4 | #include "slang_rs_export_var.hpp" |
| 5 | #include "slang_rs_export_func.hpp" |
| 6 | #include "slang_rs_export_type.hpp" |
| 7 | #include "slang_rs_pragma_handler.hpp" |
| 8 | |
| 9 | #include "clang/AST/Type.h" /* for class clang::QualType */ |
| 10 | #include "clang/AST/Decl.h" /* for class clang::*Decl */ |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 11 | #include "clang/AST/DeclBase.h" /* for class clang::Decl and clang::DeclContext */ |
| 12 | #include "clang/AST/ASTContext.h" /* for class clang::ASTContext */ |
| 13 | #include "clang/Basic/TargetInfo.h" /* for class clang::TargetInfo */ |
Victor Hsieh | e3756aa | 2010-07-12 19:28:22 +0800 | [diff] [blame] | 14 | #include "clang/Basic/Linkage.h" /* for class clang::Linkage */ |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 15 | #include "clang/Index/ASTLocation.h" /* for class clang::idx::ASTLocation */ |
| 16 | |
| 17 | #include "llvm/LLVMContext.h" /* for function llvm::getGlobalContext() */ |
| 18 | #include "llvm/Target/TargetData.h" /* for class llvm::TargetData */ |
| 19 | |
| 20 | using namespace clang::idx; |
| 21 | |
| 22 | namespace slang { |
| 23 | |
Shih-wei Liao | 001fb6d | 2010-06-21 11:17:11 -0700 | [diff] [blame] | 24 | RSContext::RSContext(Preprocessor* PP, ASTContext* Ctx, const TargetInfo* Target) : |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 25 | mPP(PP), |
Shih-wei Liao | 001fb6d | 2010-06-21 11:17:11 -0700 | [diff] [blame] | 26 | mCtx(Ctx), |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 27 | mTarget(Target), |
| 28 | mTargetData(NULL), |
Victor Hsieh | c6718b3 | 2010-06-23 09:29:44 +0800 | [diff] [blame] | 29 | mRSExportVarAllPragma(NULL), |
Victor Hsieh | c6718b3 | 2010-06-23 09:29:44 +0800 | [diff] [blame] | 30 | mRSExportFuncAllPragma(NULL), |
Victor Hsieh | d8a0d18 | 2010-07-07 19:22:33 +0800 | [diff] [blame] | 31 | mRSReflectLicensePragma(NULL), |
Victor Hsieh | a507042 | 2010-07-08 09:21:07 +0800 | [diff] [blame] | 32 | mExportAllNonStaticVars(false), |
Shih-wei Liao | 4c9f742 | 2010-08-05 04:30:02 -0700 | [diff] [blame] | 33 | mExportAllNonStaticFuncs(false), |
Shih-wei Liao | f52a620 | 2010-09-10 17:40:53 -0700 | [diff] [blame] | 34 | mLicenseNote(NULL), |
| 35 | mLLVMContext(llvm::getGlobalContext()) |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 36 | { |
| 37 | /* For #pragma rs export_var */ |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 38 | |
Victor Hsieh | c6718b3 | 2010-06-23 09:29:44 +0800 | [diff] [blame] | 39 | /* For #pragma rs export_var_all */ |
| 40 | mRSExportVarAllPragma = RSPragmaHandler::CreatePragmaExportVarAllHandler(this); |
| 41 | if(mRSExportVarAllPragma != NULL) |
| 42 | PP->AddPragmaHandler("rs", mRSExportVarAllPragma); |
| 43 | |
Victor Hsieh | c6718b3 | 2010-06-23 09:29:44 +0800 | [diff] [blame] | 44 | /* For #pragma rs export_func_all */ |
| 45 | mRSExportFuncAllPragma = RSPragmaHandler::CreatePragmaExportFuncAllHandler(this); |
| 46 | if(mRSExportFuncAllPragma != NULL) |
| 47 | PP->AddPragmaHandler("rs", mRSExportFuncAllPragma); |
| 48 | |
Shih-wei Liao | f52a620 | 2010-09-10 17:40:53 -0700 | [diff] [blame] | 49 | PP->AddPragmaHandler("rs", RSPragmaHandler::CreatePragmaExportVarHandler(this)); |
| 50 | PP->AddPragmaHandler("rs", RSPragmaHandler::CreatePragmaExportFuncHandler(this)); |
| 51 | PP->AddPragmaHandler("rs", RSPragmaHandler::CreatePragmaExportTypeHandler(this)); |
| 52 | PP->AddPragmaHandler("rs", RSPragmaHandler::CreatePragmaJavaPackageNameHandler(this)); |
Shih-wei Liao | 537446c | 2010-06-11 16:05:55 -0700 | [diff] [blame] | 53 | |
Victor Hsieh | d8a0d18 | 2010-07-07 19:22:33 +0800 | [diff] [blame] | 54 | /* For #pragma rs set_reflect_license */ |
| 55 | mRSReflectLicensePragma = RSPragmaHandler::RSPragmaHandler::CreatePragmaReflectLicenseHandler(this); |
| 56 | if (mRSReflectLicensePragma != NULL) |
| 57 | PP->AddPragmaHandler("rs", mRSReflectLicensePragma); |
| 58 | |
Shih-wei Liao | 001fb6d | 2010-06-21 11:17:11 -0700 | [diff] [blame] | 59 | /* Prepare target data */ |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 60 | mTargetData = new llvm::TargetData(Slang::TargetDescription); |
| 61 | |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | bool RSContext::processExportVar(const VarDecl* VD) { |
| 66 | assert(!VD->getName().empty() && "Variable name should not be empty"); |
| 67 | |
| 68 | /* TODO: some check on variable */ |
| 69 | |
| 70 | RSExportType* ET = RSExportType::CreateFromDecl(this, VD); |
Shih-wei Liao | 537446c | 2010-06-11 16:05:55 -0700 | [diff] [blame] | 71 | if(!ET) |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 72 | return false; |
| 73 | |
| 74 | RSExportVar* EV = new RSExportVar(this, VD, ET); |
| 75 | if(EV == NULL) |
| 76 | return false; |
| 77 | else |
| 78 | mExportVars.push_back(EV); |
| 79 | |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | bool RSContext::processExportFunc(const FunctionDecl* FD) { |
| 84 | assert(!FD->getName().empty() && "Function name should not be empty"); |
Shih-wei Liao | 537446c | 2010-06-11 16:05:55 -0700 | [diff] [blame] | 85 | |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 86 | if(!FD->isThisDeclarationADefinition()) |
| 87 | return false; |
| 88 | |
Shih-wei Liao | f52a620 | 2010-09-10 17:40:53 -0700 | [diff] [blame] | 89 | if(FD->getStorageClass() != clang::SC_None) { |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 90 | printf("RSContext::processExportFunc : cannot export extern or static function '%s'\n", FD->getName().str().c_str()); |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | RSExportFunc* EF = RSExportFunc::Create(this, FD); |
| 95 | if(EF == NULL) |
| 96 | return false; |
| 97 | else |
| 98 | mExportFuncs.push_back(EF); |
| 99 | |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | |
Shih-wei Liao | 001fb6d | 2010-06-21 11:17:11 -0700 | [diff] [blame] | 104 | bool RSContext::processExportType(const llvm::StringRef& Name) { |
| 105 | TranslationUnitDecl* TUDecl = mCtx->getTranslationUnitDecl(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 106 | |
| 107 | assert(TUDecl != NULL && "Translation unit declaration (top-level declaration) is null object"); |
| 108 | |
| 109 | const IdentifierInfo* II = mPP->getIdentifierInfo(Name); |
| 110 | if(II == NULL) |
| 111 | /* TODO: error: identifier @Name mark as an exportable type cannot be found */ |
| 112 | return false; |
| 113 | |
| 114 | DeclContext::lookup_const_result R = TUDecl->lookup(II); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 115 | RSExportType* ET = NULL; |
| 116 | |
Shih-wei Liao | 001fb6d | 2010-06-21 11:17:11 -0700 | [diff] [blame] | 117 | RSExportPointerType::IntegerType = mCtx->IntTy.getTypePtr(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 118 | |
| 119 | for(DeclContext::lookup_const_iterator I = R.first; |
| 120 | I != R.second; |
Shih-wei Liao | 537446c | 2010-06-11 16:05:55 -0700 | [diff] [blame] | 121 | I++) |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 122 | { |
| 123 | NamedDecl* const ND = *I; |
Shih-wei Liao | f52a620 | 2010-09-10 17:40:53 -0700 | [diff] [blame] | 124 | const Type* T = NULL; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 125 | |
Shih-wei Liao | f52a620 | 2010-09-10 17:40:53 -0700 | [diff] [blame] | 126 | switch(ND->getKind()) { |
| 127 | case Decl::Typedef: |
| 128 | T = static_cast<const TypedefDecl*>(ND)->getCanonicalDecl()->getUnderlyingType().getTypePtr(); |
| 129 | break; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 130 | |
Shih-wei Liao | f52a620 | 2010-09-10 17:40:53 -0700 | [diff] [blame] | 131 | case Decl::Record: |
| 132 | T = static_cast<const RecordDecl*>(ND)->getTypeForDecl(); |
| 133 | break; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 134 | |
Shih-wei Liao | f52a620 | 2010-09-10 17:40:53 -0700 | [diff] [blame] | 135 | default: |
| 136 | /* unsupported, skip */ |
| 137 | break; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 138 | } |
Shih-wei Liao | f52a620 | 2010-09-10 17:40:53 -0700 | [diff] [blame] | 139 | |
| 140 | if(T != NULL) |
| 141 | ET = RSExportType::Create(this, T); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | RSExportPointerType::IntegerType = NULL; |
| 145 | |
| 146 | return (ET != NULL); |
| 147 | } |
| 148 | |
Shih-wei Liao | 001fb6d | 2010-06-21 11:17:11 -0700 | [diff] [blame] | 149 | void RSContext::processExport() { |
Shih-wei Liao | 28cf66d | 2010-08-29 03:11:07 -0700 | [diff] [blame] | 150 | if (mNeedExportVars.empty() && mNeedExportFuncs.empty() && !mExportAllNonStaticVars && !mExportAllNonStaticFuncs) { |
| 151 | printf("note: No reflection because there are no #pragma rs export_var(...), #pragma rs export_func(...), #pragma rs export_var_all, or #pragma rs export_func_all\n"); |
| 152 | //mExportAllNonStaticVars = true; |
| 153 | //mExportAllNonStaticFuncs = true; |
Shih-wei Liao | ac91815 | 2010-08-29 02:43:24 -0700 | [diff] [blame] | 154 | } |
Shih-wei Liao | 001fb6d | 2010-06-21 11:17:11 -0700 | [diff] [blame] | 155 | TranslationUnitDecl* TUDecl = mCtx->getTranslationUnitDecl(); |
Shih-wei Liao | ac91815 | 2010-08-29 02:43:24 -0700 | [diff] [blame] | 156 | for (DeclContext::decl_iterator DI = TUDecl->decls_begin(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 157 | DI != TUDecl->decls_end(); |
Shih-wei Liao | ac91815 | 2010-08-29 02:43:24 -0700 | [diff] [blame] | 158 | DI++) { |
Victor Hsieh | d8a0d18 | 2010-07-07 19:22:33 +0800 | [diff] [blame] | 159 | if (DI->getKind() == Decl::Var) { |
Shih-wei Liao | ac91815 | 2010-08-29 02:43:24 -0700 | [diff] [blame] | 160 | /* Export variables */ |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 161 | VarDecl* VD = (VarDecl*) (*DI); |
Victor Hsieh | e3756aa | 2010-07-12 19:28:22 +0800 | [diff] [blame] | 162 | if (mExportAllNonStaticVars && VD->getLinkage() == ExternalLinkage) { |
Victor Hsieh | c6718b3 | 2010-06-23 09:29:44 +0800 | [diff] [blame] | 163 | if (!processExportVar(VD)) { |
Shih-wei Liao | f52a620 | 2010-09-10 17:40:53 -0700 | [diff] [blame] | 164 | printf("RSContext::processExport : failed to export var '%s'\n", VD->getNameAsString().c_str()); |
Victor Hsieh | c6718b3 | 2010-06-23 09:29:44 +0800 | [diff] [blame] | 165 | } |
| 166 | } else { |
| 167 | NeedExportVarSet::iterator EI = mNeedExportVars.find(VD->getName()); |
Shih-wei Liao | ac91815 | 2010-08-29 02:43:24 -0700 | [diff] [blame] | 168 | if (EI != mNeedExportVars.end() && processExportVar(VD)) { |
Victor Hsieh | c6718b3 | 2010-06-23 09:29:44 +0800 | [diff] [blame] | 169 | mNeedExportVars.erase(EI); |
Shih-wei Liao | ac91815 | 2010-08-29 02:43:24 -0700 | [diff] [blame] | 170 | } |
Victor Hsieh | c6718b3 | 2010-06-23 09:29:44 +0800 | [diff] [blame] | 171 | } |
Victor Hsieh | d8a0d18 | 2010-07-07 19:22:33 +0800 | [diff] [blame] | 172 | } else if (DI->getKind() == Decl::Function) { |
Shih-wei Liao | ac91815 | 2010-08-29 02:43:24 -0700 | [diff] [blame] | 173 | /* Export functions */ |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 174 | FunctionDecl* FD = (FunctionDecl*) (*DI); |
Victor Hsieh | e3756aa | 2010-07-12 19:28:22 +0800 | [diff] [blame] | 175 | if (mExportAllNonStaticFuncs && FD->getLinkage() == ExternalLinkage) { |
Victor Hsieh | c6718b3 | 2010-06-23 09:29:44 +0800 | [diff] [blame] | 176 | if (!processExportFunc(FD)) { |
Shih-wei Liao | f52a620 | 2010-09-10 17:40:53 -0700 | [diff] [blame] | 177 | printf("RSContext::processExport : failed to export func '%s'\n", FD->getNameAsString().c_str()); |
Victor Hsieh | c6718b3 | 2010-06-23 09:29:44 +0800 | [diff] [blame] | 178 | } |
| 179 | } else { |
| 180 | NeedExportFuncSet::iterator EI = mNeedExportFuncs.find(FD->getName()); |
Shih-wei Liao | ac91815 | 2010-08-29 02:43:24 -0700 | [diff] [blame] | 181 | if (EI != mNeedExportFuncs.end() && processExportFunc(FD)) |
Victor Hsieh | c6718b3 | 2010-06-23 09:29:44 +0800 | [diff] [blame] | 182 | mNeedExportFuncs.erase(EI); |
| 183 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
| 187 | /* Finally, export type forcely set to be exported by user */ |
Shih-wei Liao | ac91815 | 2010-08-29 02:43:24 -0700 | [diff] [blame] | 188 | for (NeedExportTypeSet::const_iterator EI = mNeedExportTypes.begin(); |
| 189 | EI != mNeedExportTypes.end(); |
| 190 | EI++) { |
| 191 | if (!processExportType(EI->getKey())) { |
| 192 | printf("RSContext::processExport : failed to export type '%s'\n", EI->getKey().str().c_str()); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 193 | |
Shih-wei Liao | ac91815 | 2010-08-29 02:43:24 -0700 | [diff] [blame] | 194 | } |
| 195 | } |
Shih-wei Liao | 537446c | 2010-06-11 16:05:55 -0700 | [diff] [blame] | 196 | |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 197 | return; |
| 198 | } |
| 199 | |
| 200 | bool RSContext::insertExportType(const llvm::StringRef& TypeName, RSExportType* ET) { |
| 201 | ExportTypeMap::value_type* NewItem = ExportTypeMap::value_type::Create(TypeName.begin(), TypeName.end(), mExportTypes.getAllocator(), ET); |
| 202 | |
| 203 | if(mExportTypes.insert(NewItem)) { |
| 204 | return true; |
| 205 | } else { |
Shih-wei Liao | c81dd70 | 2010-08-20 04:58:32 -0700 | [diff] [blame] | 206 | free(NewItem); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 207 | return false; |
| 208 | } |
| 209 | } |
| 210 | |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 211 | bool RSContext::reflectToJava(const char* OutputPackageName, const std::string& InputFileName, const std::string& OutputBCFileName, char realPackageName[], int bSize) { |
| 212 | if (realPackageName != NULL) { |
| 213 | *realPackageName = 0; |
| 214 | } |
| 215 | |
| 216 | if(OutputPackageName == NULL) { |
| 217 | if(!mReflectJavaPackageName.empty()) { |
Shih-wei Liao | 537446c | 2010-06-11 16:05:55 -0700 | [diff] [blame] | 218 | OutputPackageName = mReflectJavaPackageName.c_str(); |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 219 | } else { |
Shih-wei Liao | 537446c | 2010-06-11 16:05:55 -0700 | [diff] [blame] | 220 | return true; /* no package name, just return */ |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
| 224 | // Copy back the really applied package name |
| 225 | if (realPackageName != NULL) { |
| 226 | strncpy(realPackageName, OutputPackageName, bSize - 1); |
| 227 | } |
Shih-wei Liao | 537446c | 2010-06-11 16:05:55 -0700 | [diff] [blame] | 228 | |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 229 | RSReflection* R = new RSReflection(this); |
Shih-wei Liao | 537446c | 2010-06-11 16:05:55 -0700 | [diff] [blame] | 230 | bool ret = R->reflect(OutputPackageName, InputFileName, OutputBCFileName); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 231 | if(!ret) |
| 232 | printf("RSContext::reflectToJava : failed to do reflection (%s)\n", R->getLastError()); |
| 233 | delete R; |
| 234 | return ret; |
| 235 | } |
| 236 | |
Shih-wei Liao | 6de8927 | 2010-07-15 15:26:20 -0700 | [diff] [blame] | 237 | bool RSContext::reflectToJavaPath(const char* OutputPathName) { |
| 238 | if(OutputPathName == NULL) |
| 239 | return true; /* no path name, just return */ |
| 240 | |
| 241 | setReflectJavaPathName(std::string(OutputPathName)); |
| 242 | return true; |
| 243 | } |
| 244 | |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 245 | RSContext::~RSContext() { |
Shih-wei Liao | f83d3c3 | 2010-07-30 22:22:49 -0700 | [diff] [blame] | 246 | /* delete mRSExportVarPragma; // These are deleted by mPP.reset() that was invoked at the end of Slang::compile(). Doing it again in ~RSContext here will cause seg-faults |
Shih-wei Liao | 537446c | 2010-06-11 16:05:55 -0700 | [diff] [blame] | 247 | delete mRSExportFuncPragma; |
| 248 | delete mRSExportTypePragma; |
| 249 | delete mRSJavaPackageNamePragma; |
Victor Hsieh | d8a0d18 | 2010-07-07 19:22:33 +0800 | [diff] [blame] | 250 | delete mRSReflectLicensePragma; |
Shih-wei Liao | f83d3c3 | 2010-07-30 22:22:49 -0700 | [diff] [blame] | 251 | */ |
Victor Hsieh | d8a0d18 | 2010-07-07 19:22:33 +0800 | [diff] [blame] | 252 | delete mLicenseNote; |
Shih-wei Liao | 537446c | 2010-06-11 16:05:55 -0700 | [diff] [blame] | 253 | delete mTargetData; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | } /* namespace slang */ |