blob: e03ccc65d0d92d4a75d5b3b46531ecf088d35f64 [file] [log] [blame]
Zonr Changc383a502010-10-12 01:52:08 +08001/*
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
zonr6315f762010-10-05 15:35:14 +080017#include "slang_rs_export_func.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070018
Stephen Hinese639eb52010-11-08 19:27:20 -080019#include <string>
Shih-wei Liao0a3f20e2010-08-10 13:09:49 -070020
Zonr Chang0da0a7d2010-10-05 21:26:37 +080021#include "clang/AST/ASTContext.h"
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070022#include "clang/AST/Decl.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070023
Stephen Hinese639eb52010-11-08 19:27:20 -080024#include "llvm/DerivedTypes.h"
25#include "llvm/Target/TargetData.h"
26
Stephen Hines6e6578a2011-02-07 18:05:48 -080027#include "slang_assert.h"
zonr6315f762010-10-05 15:35:14 +080028#include "slang_rs_context.h"
zonr6315f762010-10-05 15:35:14 +080029
Stephen Hinese639eb52010-11-08 19:27:20 -080030namespace slang {
Shih-wei Liao462aefd2010-06-04 15:32:04 -070031
Stephen Hines5baf6322011-04-25 17:21:15 -070032namespace {
33
34// Ensure that the exported function is actually valid
Logan Chien9207a2e2011-10-21 15:39:28 +080035static bool ValidateFuncDecl(clang::DiagnosticsEngine *DiagEngine,
Stephen Hines5baf6322011-04-25 17:21:15 -070036 const clang::FunctionDecl *FD) {
Logan Chien9207a2e2011-10-21 15:39:28 +080037 slangAssert(DiagEngine && FD);
Stephen Hines5baf6322011-04-25 17:21:15 -070038 const clang::ASTContext &C = FD->getASTContext();
39 if (FD->getResultType().getCanonicalType() != C.VoidTy) {
Logan Chien9207a2e2011-10-21 15:39:28 +080040 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 Hines5baf6322011-04-25 17:21:15 -070045 return false;
46 }
47 return true;
48}
49
50} // namespace
51
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070052RSExportFunc *RSExportFunc::Create(RSContext *Context,
53 const clang::FunctionDecl *FD) {
54 llvm::StringRef Name = FD->getName();
55 RSExportFunc *F;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070056
Stephen Hines6e6578a2011-02-07 18:05:48 -080057 slangAssert(!Name.empty() && "Function must have a name");
Shih-wei Liao462aefd2010-06-04 15:32:04 -070058
Stephen Hines5baf6322011-04-25 17:21:15 -070059 if (!ValidateFuncDecl(Context->getDiagnostics(), FD)) {
60 return NULL;
61 }
62
Shih-wei Liao3fa286b2011-02-10 11:04:44 -080063 F = new RSExportFunc(Context, Name, FD);
Shih-wei Liao462aefd2010-06-04 15:32:04 -070064
Zonr Chang0da0a7d2010-10-05 21:26:37 +080065 // Initialize mParamPacketType
66 if (FD->getNumParams() <= 0) {
67 F->mParamPacketType = NULL;
68 } else {
Stephen Hines9e5b5032010-11-03 13:19:14 -070069 clang::ASTContext &Ctx = Context->getASTContext();
Shih-wei Liao462aefd2010-06-04 15:32:04 -070070
Zonr Chang0da0a7d2010-10-05 21:26:37 +080071 std::string Id(DUMMY_RS_TYPE_NAME_PREFIX"helper_func_param:");
72 Id.append(F->getName()).append(DUMMY_RS_TYPE_NAME_POSTFIX);
Shih-wei Liao462aefd2010-06-04 15:32:04 -070073
Zonr Chang0da0a7d2010-10-05 21:26:37 +080074 clang::RecordDecl *RD =
Stephen Hines9e5b5032010-11-03 13:19:14 -070075 clang::RecordDecl::Create(Ctx, clang::TTK_Struct,
76 Ctx.getTranslationUnitDecl(),
Zonr Chang0da0a7d2010-10-05 21:26:37 +080077 clang::SourceLocation(),
Shih-wei Liaocc887ba2011-04-19 18:36:20 -070078 clang::SourceLocation(),
Stephen Hines9e5b5032010-11-03 13:19:14 -070079 &Ctx.Idents.get(Id));
Shih-wei Liao462aefd2010-06-04 15:32:04 -070080
Zonr Chang0da0a7d2010-10-05 21:26:37 +080081 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 Hines9e5b5032010-11-03 13:19:14 -070092 clang::FieldDecl::Create(Ctx,
Zonr Chang0da0a7d2010-10-05 21:26:37 +080093 RD,
94 clang::SourceLocation(),
Shih-wei Liaocc887ba2011-04-19 18:36:20 -070095 clang::SourceLocation(),
Zonr Chang0da0a7d2010-10-05 21:26:37 +080096 PVD->getIdentifier(),
97 PVD->getOriginalType(),
98 NULL,
Shih-wei Liaofd6ea6a2011-06-22 04:50:15 -070099 /* BitWidth = */ NULL,
100 /* Mutable = */ false,
Shih-wei Liao43730fe2012-08-02 23:06:18 -0700101 /* HasInit = */ clang::ICIS_NoInit);
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800102 RD->addDecl(FD);
103 }
104
105 RD->completeDefinition();
106
Stephen Hines9e5b5032010-11-03 13:19:14 -0700107 clang::QualType T = Ctx.getTagDeclType(RD);
Stephen Hines6e6578a2011-02-07 18:05:48 -0800108 slangAssert(!T.isNull());
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800109
110 RSExportType *ET =
111 RSExportType::Create(Context, T.getTypePtr());
112
113 if (ET == NULL) {
Stephen Hines5baf6322011-04-25 17:21:15 -0700114 fprintf(stderr, "Failed to export the function %s. There's at least one "
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800115 "parameter whose type is not supported by the "
Stephen Hines5baf6322011-04-25 17:21:15 -0700116 "reflection\n", F->getName().c_str());
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700117 return NULL;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700118 }
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800119
Stephen Hines6e6578a2011-02-07 18:05:48 -0800120 slangAssert((ET->getClass() == RSExportType::ExportClassRecord) &&
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800121 "Parameter packet must be a record");
122
123 F->mParamPacketType = static_cast<RSExportRecordType *>(ET);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700124 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700125
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700126 return F;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700127}
128
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800129bool
Logan Chienab992e52011-07-20 22:06:52 +0800130RSExportFunc::checkParameterPacketType(llvm::StructType *ParamTy) const {
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800131 if (ParamTy == NULL)
132 return !hasParam();
133 else if (!hasParam())
134 return false;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700135
Stephen Hines6e6578a2011-02-07 18:05:48 -0800136 slangAssert(mParamPacketType != NULL);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700137
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800138 const RSExportRecordType *ERT = mParamPacketType;
139 // must have same number of elements
140 if (ERT->getFields().size() != ParamTy->getNumElements())
141 return false;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700142
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800143 const llvm::StructLayout *ParamTySL =
Zonr Chang641558f2010-10-12 21:07:06 +0800144 getRSContext()->getTargetData()->getStructLayout(ParamTy);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700145
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800146 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 Liao9ef2f782010-10-01 12:31:37 -0700150
Logan Chienab992e52011-07-20 22:06:52 +0800151 llvm::Type *T1 = F->getType()->getLLVMType();
152 llvm::Type *T2 = ParamTy->getTypeAtIndex(Index);
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800153
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 Chang641558f2010-10-12 21:07:06 +0800167 size_t T2Size = getRSContext()->getTargetData()->getTypeAllocSize(T2);
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800168
169 if (T1Size != T2Size)
170 return false;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700171 }
172
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800173 return true;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700174}
Stephen Hinese639eb52010-11-08 19:27:20 -0800175
176} // namespace slang