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 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 32 | RSExportFunc *RSExportFunc::Create(RSContext *Context, |
| 33 | const clang::FunctionDecl *FD) { |
| 34 | llvm::StringRef Name = FD->getName(); |
| 35 | RSExportFunc *F; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 36 | |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 37 | slangAssert(!Name.empty() && "Function must have a name"); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 38 | |
Shih-wei Liao | 3fa286b | 2011-02-10 11:04:44 -0800 | [diff] [blame] | 39 | F = new RSExportFunc(Context, Name, FD); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 40 | |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 41 | // Initialize mParamPacketType |
| 42 | if (FD->getNumParams() <= 0) { |
| 43 | F->mParamPacketType = NULL; |
| 44 | } else { |
Stephen Hines | 9e5b503 | 2010-11-03 13:19:14 -0700 | [diff] [blame] | 45 | clang::ASTContext &Ctx = Context->getASTContext(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 46 | |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 47 | std::string Id(DUMMY_RS_TYPE_NAME_PREFIX"helper_func_param:"); |
| 48 | Id.append(F->getName()).append(DUMMY_RS_TYPE_NAME_POSTFIX); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 49 | |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 50 | clang::RecordDecl *RD = |
Stephen Hines | 9e5b503 | 2010-11-03 13:19:14 -0700 | [diff] [blame] | 51 | clang::RecordDecl::Create(Ctx, clang::TTK_Struct, |
| 52 | Ctx.getTranslationUnitDecl(), |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 53 | clang::SourceLocation(), |
Stephen Hines | 9e5b503 | 2010-11-03 13:19:14 -0700 | [diff] [blame] | 54 | &Ctx.Idents.get(Id)); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 55 | |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 56 | for (unsigned i = 0; i < FD->getNumParams(); i++) { |
| 57 | const clang::ParmVarDecl *PVD = FD->getParamDecl(i); |
| 58 | llvm::StringRef ParamName = PVD->getName(); |
| 59 | |
| 60 | if (PVD->hasDefaultArg()) |
| 61 | fprintf(stderr, "Note: parameter '%s' in function '%s' has default " |
| 62 | "value which is not supported\n", |
| 63 | ParamName.str().c_str(), |
| 64 | F->getName().c_str()); |
| 65 | |
| 66 | clang::FieldDecl *FD = |
Stephen Hines | 9e5b503 | 2010-11-03 13:19:14 -0700 | [diff] [blame] | 67 | clang::FieldDecl::Create(Ctx, |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 68 | RD, |
| 69 | clang::SourceLocation(), |
| 70 | PVD->getIdentifier(), |
| 71 | PVD->getOriginalType(), |
| 72 | NULL, |
| 73 | /* BitWidth = */NULL, |
| 74 | /* Mutable = */false); |
| 75 | RD->addDecl(FD); |
| 76 | } |
| 77 | |
| 78 | RD->completeDefinition(); |
| 79 | |
Stephen Hines | 9e5b503 | 2010-11-03 13:19:14 -0700 | [diff] [blame] | 80 | clang::QualType T = Ctx.getTagDeclType(RD); |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 81 | slangAssert(!T.isNull()); |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 82 | |
| 83 | RSExportType *ET = |
| 84 | RSExportType::Create(Context, T.getTypePtr()); |
| 85 | |
| 86 | if (ET == NULL) { |
| 87 | fprintf(stderr, "Failed to export the function %s. There's at least one " |
| 88 | "parameter whose type is not supported by the " |
| 89 | "reflection", F->getName().c_str()); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 90 | delete F; |
| 91 | return NULL; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 92 | } |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 93 | |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 94 | slangAssert((ET->getClass() == RSExportType::ExportClassRecord) && |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 95 | "Parameter packet must be a record"); |
| 96 | |
| 97 | F->mParamPacketType = static_cast<RSExportRecordType *>(ET); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 98 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 99 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 100 | return F; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 103 | bool |
| 104 | RSExportFunc::checkParameterPacketType(const llvm::StructType *ParamTy) const { |
| 105 | if (ParamTy == NULL) |
| 106 | return !hasParam(); |
| 107 | else if (!hasParam()) |
| 108 | return false; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 109 | |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 110 | slangAssert(mParamPacketType != NULL); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 111 | |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 112 | const RSExportRecordType *ERT = mParamPacketType; |
| 113 | // must have same number of elements |
| 114 | if (ERT->getFields().size() != ParamTy->getNumElements()) |
| 115 | return false; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 116 | |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 117 | const llvm::StructLayout *ParamTySL = |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 118 | getRSContext()->getTargetData()->getStructLayout(ParamTy); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 119 | |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 120 | unsigned Index = 0; |
| 121 | for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(), |
| 122 | FE = ERT->fields_end(); FI != FE; FI++, Index++) { |
| 123 | const RSExportRecordType::Field *F = *FI; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 124 | |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 125 | const llvm::Type *T1 = F->getType()->getLLVMType(); |
| 126 | const llvm::Type *T2 = ParamTy->getTypeAtIndex(Index); |
| 127 | |
| 128 | // Fast check |
| 129 | if (T1 == T2) |
| 130 | continue; |
| 131 | |
| 132 | // Check offset |
| 133 | size_t T1Offset = F->getOffsetInParent(); |
| 134 | size_t T2Offset = ParamTySL->getElementOffset(Index); |
| 135 | |
| 136 | if (T1Offset != T2Offset) |
| 137 | return false; |
| 138 | |
| 139 | // Check size |
| 140 | size_t T1Size = RSExportType::GetTypeAllocSize(F->getType()); |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 141 | size_t T2Size = getRSContext()->getTargetData()->getTypeAllocSize(T2); |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 142 | |
| 143 | if (T1Size != T2Size) |
| 144 | return false; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 147 | return true; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 148 | } |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 149 | |
| 150 | } // namespace slang |