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