Zonr Chang | c383a50 | 2010-10-12 01:52:08 +0800 | [diff] [blame] | 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 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 17 | #include "slang_rs_export_func.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 18 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 19 | #include <string> |
Shih-wei Liao | 0a3f20e | 2010-08-10 13:09:49 -0700 | [diff] [blame] | 20 | |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 21 | #include "clang/AST/ASTContext.h" |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 22 | #include "clang/AST/Decl.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 23 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 24 | #include "llvm/DerivedTypes.h" |
| 25 | #include "llvm/Target/TargetData.h" |
| 26 | |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 27 | #include "slang_assert.h" |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 28 | #include "slang_rs_context.h" |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 29 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 30 | namespace slang { |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 31 | |
Stephen Hines | 5baf632 | 2011-04-25 17:21:15 -0700 | [diff] [blame] | 32 | namespace { |
| 33 | |
| 34 | // Ensure that the exported function is actually valid |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 35 | static bool ValidateFuncDecl(clang::DiagnosticsEngine *DiagEngine, |
Stephen Hines | 5baf632 | 2011-04-25 17:21:15 -0700 | [diff] [blame] | 36 | const clang::FunctionDecl *FD) { |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 37 | slangAssert(DiagEngine && FD); |
Stephen Hines | 5baf632 | 2011-04-25 17:21:15 -0700 | [diff] [blame] | 38 | const clang::ASTContext &C = FD->getASTContext(); |
| 39 | if (FD->getResultType().getCanonicalType() != C.VoidTy) { |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 40 | DiagEngine->Report( |
| 41 | clang::FullSourceLoc(FD->getLocation(), DiagEngine->getSourceManager()), |
| 42 | DiagEngine->getCustomDiagID(clang::DiagnosticsEngine::Error, |
| 43 | "invokable non-static functions are " |
| 44 | "required to return void")); |
Stephen Hines | 5baf632 | 2011-04-25 17:21:15 -0700 | [diff] [blame] | 45 | return false; |
| 46 | } |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | } // namespace |
| 51 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 52 | RSExportFunc *RSExportFunc::Create(RSContext *Context, |
| 53 | const clang::FunctionDecl *FD) { |
| 54 | llvm::StringRef Name = FD->getName(); |
| 55 | RSExportFunc *F; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 56 | |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 57 | slangAssert(!Name.empty() && "Function must have a name"); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 58 | |
Stephen Hines | 5baf632 | 2011-04-25 17:21:15 -0700 | [diff] [blame] | 59 | if (!ValidateFuncDecl(Context->getDiagnostics(), FD)) { |
| 60 | return NULL; |
| 61 | } |
| 62 | |
Shih-wei Liao | 3fa286b | 2011-02-10 11:04:44 -0800 | [diff] [blame] | 63 | F = new RSExportFunc(Context, Name, FD); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 64 | |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 65 | // Initialize mParamPacketType |
| 66 | if (FD->getNumParams() <= 0) { |
| 67 | F->mParamPacketType = NULL; |
| 68 | } else { |
Stephen Hines | 9e5b503 | 2010-11-03 13:19:14 -0700 | [diff] [blame] | 69 | clang::ASTContext &Ctx = Context->getASTContext(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 70 | |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 71 | std::string Id(DUMMY_RS_TYPE_NAME_PREFIX"helper_func_param:"); |
| 72 | Id.append(F->getName()).append(DUMMY_RS_TYPE_NAME_POSTFIX); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 73 | |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 74 | clang::RecordDecl *RD = |
Stephen Hines | 9e5b503 | 2010-11-03 13:19:14 -0700 | [diff] [blame] | 75 | clang::RecordDecl::Create(Ctx, clang::TTK_Struct, |
| 76 | Ctx.getTranslationUnitDecl(), |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 77 | clang::SourceLocation(), |
Shih-wei Liao | cc887ba | 2011-04-19 18:36:20 -0700 | [diff] [blame] | 78 | clang::SourceLocation(), |
Stephen Hines | 9e5b503 | 2010-11-03 13:19:14 -0700 | [diff] [blame] | 79 | &Ctx.Idents.get(Id)); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 80 | |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 81 | for (unsigned i = 0; i < FD->getNumParams(); i++) { |
| 82 | const clang::ParmVarDecl *PVD = FD->getParamDecl(i); |
| 83 | llvm::StringRef ParamName = PVD->getName(); |
| 84 | |
| 85 | if (PVD->hasDefaultArg()) |
| 86 | fprintf(stderr, "Note: parameter '%s' in function '%s' has default " |
| 87 | "value which is not supported\n", |
| 88 | ParamName.str().c_str(), |
| 89 | F->getName().c_str()); |
| 90 | |
| 91 | clang::FieldDecl *FD = |
Stephen Hines | 9e5b503 | 2010-11-03 13:19:14 -0700 | [diff] [blame] | 92 | clang::FieldDecl::Create(Ctx, |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 93 | RD, |
| 94 | clang::SourceLocation(), |
Shih-wei Liao | cc887ba | 2011-04-19 18:36:20 -0700 | [diff] [blame] | 95 | clang::SourceLocation(), |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 96 | PVD->getIdentifier(), |
| 97 | PVD->getOriginalType(), |
| 98 | NULL, |
Shih-wei Liao | fd6ea6a | 2011-06-22 04:50:15 -0700 | [diff] [blame] | 99 | /* BitWidth = */ NULL, |
| 100 | /* Mutable = */ false, |
Shih-wei Liao | 43730fe | 2012-08-02 23:06:18 -0700 | [diff] [blame] | 101 | /* HasInit = */ clang::ICIS_NoInit); |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 102 | RD->addDecl(FD); |
| 103 | } |
| 104 | |
| 105 | RD->completeDefinition(); |
| 106 | |
Stephen Hines | 9e5b503 | 2010-11-03 13:19:14 -0700 | [diff] [blame] | 107 | clang::QualType T = Ctx.getTagDeclType(RD); |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 108 | slangAssert(!T.isNull()); |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 109 | |
| 110 | RSExportType *ET = |
| 111 | RSExportType::Create(Context, T.getTypePtr()); |
| 112 | |
| 113 | if (ET == NULL) { |
Stephen Hines | 5baf632 | 2011-04-25 17:21:15 -0700 | [diff] [blame] | 114 | fprintf(stderr, "Failed to export the function %s. There's at least one " |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 115 | "parameter whose type is not supported by the " |
Stephen Hines | 5baf632 | 2011-04-25 17:21:15 -0700 | [diff] [blame] | 116 | "reflection\n", F->getName().c_str()); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 117 | return NULL; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 118 | } |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 119 | |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 120 | slangAssert((ET->getClass() == RSExportType::ExportClassRecord) && |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 121 | "Parameter packet must be a record"); |
| 122 | |
| 123 | F->mParamPacketType = static_cast<RSExportRecordType *>(ET); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 124 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 125 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 126 | return F; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 129 | bool |
Logan Chien | ab992e5 | 2011-07-20 22:06:52 +0800 | [diff] [blame] | 130 | RSExportFunc::checkParameterPacketType(llvm::StructType *ParamTy) const { |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 131 | if (ParamTy == NULL) |
| 132 | return !hasParam(); |
| 133 | else if (!hasParam()) |
| 134 | return false; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 135 | |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 136 | slangAssert(mParamPacketType != NULL); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 137 | |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 138 | const RSExportRecordType *ERT = mParamPacketType; |
| 139 | // must have same number of elements |
| 140 | if (ERT->getFields().size() != ParamTy->getNumElements()) |
| 141 | return false; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 142 | |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 143 | const llvm::StructLayout *ParamTySL = |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 144 | getRSContext()->getTargetData()->getStructLayout(ParamTy); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 145 | |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 146 | unsigned Index = 0; |
| 147 | for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(), |
| 148 | FE = ERT->fields_end(); FI != FE; FI++, Index++) { |
| 149 | const RSExportRecordType::Field *F = *FI; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 150 | |
Logan Chien | ab992e5 | 2011-07-20 22:06:52 +0800 | [diff] [blame] | 151 | llvm::Type *T1 = F->getType()->getLLVMType(); |
| 152 | llvm::Type *T2 = ParamTy->getTypeAtIndex(Index); |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 153 | |
| 154 | // Fast check |
| 155 | if (T1 == T2) |
| 156 | continue; |
| 157 | |
| 158 | // Check offset |
| 159 | size_t T1Offset = F->getOffsetInParent(); |
| 160 | size_t T2Offset = ParamTySL->getElementOffset(Index); |
| 161 | |
| 162 | if (T1Offset != T2Offset) |
| 163 | return false; |
| 164 | |
| 165 | // Check size |
| 166 | size_t T1Size = RSExportType::GetTypeAllocSize(F->getType()); |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 167 | size_t T2Size = getRSContext()->getTargetData()->getTypeAllocSize(T2); |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 168 | |
| 169 | if (T1Size != T2Size) |
| 170 | return false; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 173 | return true; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 174 | } |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 175 | |
| 176 | } // namespace slang |