Skip to content

Commit 39ace33

Browse files
committed
Implement ConstructorCall in JitCall, disentangled from generics
1 parent 7962bb0 commit 39ace33

File tree

5 files changed

+49
-15
lines changed

5 files changed

+49
-15
lines changed

include/CppInterOp/CppInterOp.h

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ class JitCall {
103103
enum Kind : char {
104104
kUnknown = 0,
105105
kGenericCall,
106+
kConstructorCall,
106107
kDestructorCall,
107108
};
108109
struct ArgList {
@@ -116,20 +117,25 @@ class JitCall {
116117
// FIXME: Hide these implementation details by moving wrapper generation in
117118
// this class.
118119
// (self, nargs, args, result, nary)
119-
using GenericCall = void (*)(void*, size_t, void**, void*, size_t);
120+
using GenericCall = void (*)(void*, size_t, void**, void*);
121+
// (result, nary, nargs, args, is_arena)
122+
using ConstructorCall = void (*)(void*, size_t, size_t, void**, int);
120123
// (self, nary, withFree)
121124
using DestructorCall = void (*)(void*, size_t, int);
122125

123126
private:
124127
union {
125128
GenericCall m_GenericCall;
129+
ConstructorCall m_ConstructorCall;
126130
DestructorCall m_DestructorCall;
127131
};
128132
Kind m_Kind;
129133
TCppConstFunction_t m_FD;
130134
JitCall() : m_GenericCall(nullptr), m_Kind(kUnknown), m_FD(nullptr) {}
131135
JitCall(Kind K, GenericCall C, TCppConstFunction_t FD)
132136
: m_GenericCall(C), m_Kind(K), m_FD(FD) {}
137+
JitCall(Kind K, ConstructorCall C, TCppConstFunction_t Ctor)
138+
: m_ConstructorCall(C), m_Kind(K), m_FD(Ctor) {}
133139
JitCall(Kind K, DestructorCall C, TCppConstFunction_t Dtor)
134140
: m_DestructorCall(C), m_Kind(K), m_FD(Dtor) {}
135141

@@ -164,20 +170,24 @@ class JitCall {
164170
// self can go in the end and be nullptr by default; result can be a nullptr
165171
// by default. These changes should be synchronized with the wrapper if we
166172
// decide to directly.
167-
void Invoke(void* result, ArgList args = {}, void* self = nullptr,
168-
size_t nary = 0UL) const {
173+
void Invoke(void* result, ArgList args = {}, void* self = nullptr) const {
169174
// NOLINTBEGIN(*-type-union-access)
170175
// Forward if we intended to call a dtor with only 1 parameter.
171176
if (m_Kind == kDestructorCall && result && !args.m_Args) {
172-
InvokeDestructor(result, nary, /*withFree=*/true);
177+
InvokeDestructor(result, /*nary=*/0UL, /*withFree=*/true);
178+
return;
179+
}
180+
181+
else if (m_Kind == kConstructorCall && result && !args.m_Args) {
182+
InvokeConstructor(result, /*nary=*/1UL, args, false);
173183
return;
174184
}
175185

176186
#ifndef NDEBUG
177187
assert(AreArgumentsValid(result, args, self) && "Invalid args!");
178188
ReportInvokeStart(result, args, self);
179189
#endif // NDEBUG
180-
m_GenericCall(self, args.m_ArgSize, args.m_Args, result, nary);
190+
m_GenericCall(self, args.m_ArgSize, args.m_Args, result);
181191
// NOLINTEND(*-type-union-access)
182192
}
183193
/// Makes a call to a destructor.
@@ -195,6 +205,23 @@ class JitCall {
195205
#endif // NDEBUG
196206
m_DestructorCall(object, nary, withFree);
197207
}
208+
209+
/// Makes a call to a constructor.
210+
///\param[in] result - the address at which we construct the object
211+
///\param[in] nary - Use array new if we have to construct an
212+
/// array of objects (nary > 1).
213+
///\param[in] args - a pointer to a argument list
214+
/// and argument size.
215+
///\param[in] is_arena - indicate if we want to use placement new
216+
void InvokeConstructor(void* result, unsigned long nary = 1,
217+
ArgList args = {}, bool is_arena = false) const {
218+
assert(m_Kind == kConstructorCall && "Wrong overload!");
219+
#ifndef NDEBUG
220+
assert(AreArgumentsValid(result, args, nullptr, nary) && "Invalid args!");
221+
ReportInvokeStart(result, args, nullptr);
222+
#endif // NDEBUG
223+
m_ConstructorCall(result, nary, args.m_ArgSize, args.m_Args, (int)is_arena);
224+
}
198225
};
199226

200227
///\returns the version string information of the library.

include/clang-c/CXCppInterOp.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ CINDEX_LINKAGE void clang_deallocate(CXObject address);
333333
* arena is set it uses placement new.
334334
*/
335335
CINDEX_LINKAGE CXObject clang_construct(CXScope scope, void* arena,
336-
size_t count);
336+
size_t count = 1UL);
337337

338338
/**
339339
* Creates a trampoline function and makes a call to a generic function or
@@ -350,7 +350,7 @@ CINDEX_LINKAGE CXObject clang_construct(CXScope scope, void* arena,
350350
* \param self The 'this pointer' of the object.
351351
*/
352352
CINDEX_LINKAGE void clang_invoke(CXScope func, void* result, void** args,
353-
size_t n, size_t nary, void* self);
353+
size_t n, void* self);
354354

355355
/**
356356
* Calls the destructor of object of type \c type. When withFree is true it

lib/CppInterOp/CXCppInterOp.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,9 +608,9 @@ CXObject clang_construct(CXScope scope, void* arena, size_t count) {
608608
}
609609

610610
void clang_invoke(CXScope func, void* result, void** args, size_t n,
611-
size_t nary, void* self) {
611+
void* self) {
612612
Cpp::MakeFunctionCallable(getInterpreter(func), getDecl(func))
613-
.Invoke(result, {args, n}, self, nary);
613+
.Invoke(result, {args, n}, self);
614614
}
615615

616616
namespace Cpp {

lib/CppInterOp/CppInterOp.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2831,6 +2831,13 @@ CPPINTEROP_API JitCall MakeFunctionCallable(TInterp_t I,
28312831
return {};
28322832
}
28332833

2834+
if (const auto* Ctor = dyn_cast<CXXConstructorDecl>(D)) {
2835+
if (auto Wrapper = make_wrapper(*interp, cast<FunctionDecl>(D)))
2836+
return {JitCall::kConstructorCall, Wrapper, Ctor};
2837+
// FIXME: else error we failed to compile the wrapper.
2838+
return {};
2839+
}
2840+
28342841
if (auto Wrapper = make_wrapper(*interp, cast<FunctionDecl>(D))) {
28352842
return {JitCall::kGenericCall, Wrapper, cast<FunctionDecl>(D)};
28362843
}
@@ -3547,13 +3554,13 @@ TCppObject_t Construct(compat::Interpreter& interp, TCppScope_t scope,
35473554
auto* const Ctor = GetDefaultConstructor(interp, Class);
35483555
if (JitCall JC = MakeFunctionCallable(&interp, Ctor)) {
35493556
if (arena) {
3550-
JC.Invoke(&arena, {}, (void*)~0,
3551-
count); // Tell Invoke to use placement new.
3557+
JC.InvokeConstructor(&arena, count, {},
3558+
true); // Tell Invoke to use placement new.
35523559
return arena;
35533560
}
35543561

35553562
void* obj = nullptr;
3556-
JC.Invoke(&obj);
3563+
JC.InvokeConstructor(&obj, count, {}, false);
35573564
return obj;
35583565
}
35593566
return nullptr;

unittests/CppInterOp/FunctionReflectionTest.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,7 +1471,7 @@ TEST(FunctionReflectionTest, JitCallAdvanced) {
14711471
auto* I = clang_createInterpreterFromRawPtr(Cpp::GetInterpreter());
14721472
auto S = clang_getDefaultConstructor(make_scope(Decls[0], I));
14731473
void* object_c = nullptr;
1474-
clang_invoke(S, &object_c, nullptr, 0, 0UL, nullptr);
1474+
clang_invoke(S, &object_c, nullptr, 0, nullptr);
14751475
EXPECT_TRUE(object_c) << "Failed to call the ctor.";
14761476
clang_destruct(object_c, make_scope(Decls[1], I), true);
14771477
// Clean up resources
@@ -1883,7 +1883,7 @@ TEST(FunctionReflectionTest, Construct) {
18831883
testing::internal::CaptureStdout();
18841884
auto* I = clang_createInterpreterFromRawPtr(Cpp::GetInterpreter());
18851885
auto scope_c = make_scope(static_cast<clang::Decl*>(scope), I);
1886-
auto object_c = clang_construct(scope_c, nullptr, 0UL);
1886+
auto object_c = clang_construct(scope_c, nullptr, 1UL);
18871887
EXPECT_TRUE(object_c != nullptr);
18881888
output = testing::internal::GetCapturedStdout();
18891889
EXPECT_EQ(output, "Constructor Executed");
@@ -2006,7 +2006,7 @@ TEST(FunctionReflectionTest, Destruct) {
20062006
testing::internal::CaptureStdout();
20072007
auto* I = clang_createInterpreterFromRawPtr(Cpp::GetInterpreter());
20082008
auto scope_c = make_scope(static_cast<clang::Decl*>(scope), I);
2009-
auto object_c = clang_construct(scope_c, nullptr, 0UL);
2009+
auto object_c = clang_construct(scope_c, nullptr, 1UL);
20102010
clang_destruct(object_c, scope_c, true);
20112011
output = testing::internal::GetCapturedStdout();
20122012
EXPECT_EQ(output, "Destructor Executed");

0 commit comments

Comments
 (0)