blob: f634f0fda578c6cb410c90dc2c5b29ec9c5b8946 [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
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070032RSExportFunc *RSExportFunc::Create(RSContext *Context,
33 const clang::FunctionDecl *FD) {
34 llvm::StringRef Name = FD->getName();
35 RSExportFunc *F;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070036
Stephen Hines6e6578a2011-02-07 18:05:48 -080037 slangAssert(!Name.empty() && "Function must have a name");
Shih-wei Liao462aefd2010-06-04 15:32:04 -070038
Shih-wei Liao3fa286b2011-02-10 11:04:44 -080039 F = new RSExportFunc(Context, Name, FD);
Shih-wei Liao462aefd2010-06-04 15:32:04 -070040
Zonr Chang0da0a7d2010-10-05 21:26:37 +080041 // Initialize mParamPacketType
42 if (FD->getNumParams() <= 0) {
43 F->mParamPacketType = NULL;
44 } else {
Stephen Hines9e5b5032010-11-03 13:19:14 -070045 clang::ASTContext &Ctx = Context->getASTContext();
Shih-wei Liao462aefd2010-06-04 15:32:04 -070046
Zonr Chang0da0a7d2010-10-05 21:26:37 +080047 std::string Id(DUMMY_RS_TYPE_NAME_PREFIX"helper_func_param:");
48 Id.append(F->getName()).append(DUMMY_RS_TYPE_NAME_POSTFIX);
Shih-wei Liao462aefd2010-06-04 15:32:04 -070049
Zonr Chang0da0a7d2010-10-05 21:26:37 +080050 clang::RecordDecl *RD =
Stephen Hines9e5b5032010-11-03 13:19:14 -070051 clang::RecordDecl::Create(Ctx, clang::TTK_Struct,
52 Ctx.getTranslationUnitDecl(),
Zonr Chang0da0a7d2010-10-05 21:26:37 +080053 clang::SourceLocation(),
Stephen Hines9e5b5032010-11-03 13:19:14 -070054 &Ctx.Idents.get(Id));
Shih-wei Liao462aefd2010-06-04 15:32:04 -070055
Zonr Chang0da0a7d2010-10-05 21:26:37 +080056 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 Hines9e5b5032010-11-03 13:19:14 -070067 clang::FieldDecl::Create(Ctx,
Zonr Chang0da0a7d2010-10-05 21:26:37 +080068 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 Hines9e5b5032010-11-03 13:19:14 -070080 clang::QualType T = Ctx.getTagDeclType(RD);
Stephen Hines6e6578a2011-02-07 18:05:48 -080081 slangAssert(!T.isNull());
Zonr Chang0da0a7d2010-10-05 21:26:37 +080082
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 Liao9ef2f782010-10-01 12:31:37 -070090 delete F;
91 return NULL;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070092 }
Zonr Chang0da0a7d2010-10-05 21:26:37 +080093
Stephen Hines6e6578a2011-02-07 18:05:48 -080094 slangAssert((ET->getClass() == RSExportType::ExportClassRecord) &&
Zonr Chang0da0a7d2010-10-05 21:26:37 +080095 "Parameter packet must be a record");
96
97 F->mParamPacketType = static_cast<RSExportRecordType *>(ET);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070098 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -070099
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700100 return F;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700101}
102
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800103bool
104RSExportFunc::checkParameterPacketType(const llvm::StructType *ParamTy) const {
105 if (ParamTy == NULL)
106 return !hasParam();
107 else if (!hasParam())
108 return false;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700109
Stephen Hines6e6578a2011-02-07 18:05:48 -0800110 slangAssert(mParamPacketType != NULL);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700111
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800112 const RSExportRecordType *ERT = mParamPacketType;
113 // must have same number of elements
114 if (ERT->getFields().size() != ParamTy->getNumElements())
115 return false;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700116
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800117 const llvm::StructLayout *ParamTySL =
Zonr Chang641558f2010-10-12 21:07:06 +0800118 getRSContext()->getTargetData()->getStructLayout(ParamTy);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700119
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800120 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 Liao9ef2f782010-10-01 12:31:37 -0700124
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800125 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 Chang641558f2010-10-12 21:07:06 +0800141 size_t T2Size = getRSContext()->getTargetData()->getTypeAllocSize(T2);
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800142
143 if (T1Size != T2Size)
144 return false;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700145 }
146
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800147 return true;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700148}
Stephen Hinese639eb52010-11-08 19:27:20 -0800149
150} // namespace slang