Skip to content

Commit bfedc8e

Browse files
committed
Add support for array of objects in Construct/Destruct
1 parent 03135b6 commit bfedc8e

File tree

4 files changed

+74
-38
lines changed

4 files changed

+74
-38
lines changed

include/clang-c/CXCppInterOp.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ CINDEX_LINKAGE void clang_deallocate(CXObject address);
332332
* Creates an object of class \c scope and calls its default constructor. If \c
333333
* arena is set it uses placement new.
334334
*/
335-
CINDEX_LINKAGE CXObject clang_construct(CXScope scope, void* arena);
335+
CINDEX_LINKAGE CXObject clang_construct(CXScope scope, void* arena, size_t count);
336336

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

354354
/**
355355
* Calls the destructor of object of type \c type. When withFree is true it
@@ -361,7 +361,7 @@ CINDEX_LINKAGE void clang_invoke(CXScope func, void* result, void** args,
361361
*
362362
* \param withFree Whether to call operator delete/free or not.
363363
*/
364-
CINDEX_LINKAGE void clang_destruct(CXObject This, CXScope S, bool withFree);
364+
CINDEX_LINKAGE void clang_destruct(CXObject This, CXScope S, bool withFree = true, size_t nary = 0UL);
365365

366366
/**
367367
* @}

include/clang/Interpreter/CppInterOp.h

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,10 @@ namespace Cpp {
111111
// FIXME: Figure out how to unify the wrapper signatures.
112112
// FIXME: Hide these implementation details by moving wrapper generation in
113113
// this class.
114-
using GenericCall = void (*)(void*, int, void**, void*);
114+
115+
// (self, args, args, result, nary)
116+
using GenericCall = void (*)(void*, int, void**, void*, size_t);
117+
// (self, nary, withFree)
115118
using DestructorCall = void (*)(void*, unsigned long, int);
116119
private:
117120
union {
@@ -156,16 +159,16 @@ namespace Cpp {
156159
// self can go in the end and be nullptr by default; result can be a nullptr
157160
// by default. These changes should be synchronized with the wrapper if we
158161
// decide to directly.
159-
void Invoke(void* result, ArgList args = {}, void* self = nullptr) const {
162+
void Invoke(void* result, ArgList args = {}, void* self = nullptr, size_t nary = 0UL) const {
160163
// Forward if we intended to call a dtor with only 1 parameter.
161164
if (m_Kind == kDestructorCall && result && !args.m_Args)
162-
return InvokeDestructor(result, /*nary=*/0UL, /*withFree=*/true);
165+
return InvokeDestructor(result, nary, /*withFree=*/true);
163166

164167
#ifndef NDEBUG
165168
assert(AreArgumentsValid(result, args, self) && "Invalid args!");
166169
ReportInvokeStart(result, args, self);
167170
#endif // NDEBUG
168-
m_GenericCall(self, args.m_ArgSize, args.m_Args, result);
171+
m_GenericCall(self, args.m_ArgSize, args.m_Args, result, nary);
169172
}
170173
/// Makes a call to a destructor.
171174
///\param[in] object - the pointer of the object whose destructor we call.
@@ -753,20 +756,22 @@ namespace Cpp {
753756
CPPINTEROP_API std::vector<long int> GetDimensions(TCppType_t type);
754757

755758
/// Allocates memory for a given class.
756-
CPPINTEROP_API TCppObject_t Allocate(TCppScope_t scope);
759+
/// \c count is used to indicate the number of objects to allocate for.
760+
CPPINTEROP_API TCppObject_t Allocate(TCppScope_t scope, TCppIndex_t count = 1UL);
757761

758762
/// Deallocates memory for a given class.
759-
CPPINTEROP_API void Deallocate(TCppScope_t scope, TCppObject_t address);
763+
CPPINTEROP_API void Deallocate(TCppScope_t scope, TCppObject_t address, TCppIndex_t count = 1UL);
760764

761765
/// Creates an object of class \c scope and calls its default constructor. If
762766
/// \c arena is set it uses placement new.
767+
/// \c count is used to indicate the number of objects to construct.
763768
CPPINTEROP_API TCppObject_t Construct(TCppScope_t scope,
764-
void* arena = nullptr);
769+
void* arena = nullptr, TCppIndex_t count = 1UL);
765770

766771
/// Calls the destructor of object of type \c type. When withFree is true it
767772
/// calls operator delete/free.
768773
CPPINTEROP_API void Destruct(TCppObject_t This, TCppScope_t type,
769-
bool withFree = true);
774+
bool withFree = true, TCppIndex_t count = 0UL);
770775

771776
/// @name Stream Redirection
772777
///

lib/Interpreter/CXCppInterOp.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -598,25 +598,25 @@ void clang_deallocate(CXObject address) { ::operator delete(address); }
598598

599599
namespace Cpp {
600600
void* Construct(compat::Interpreter& interp, TCppScope_t scope,
601-
void* arena /*=nullptr*/);
601+
void* arena /*=nullptr*/, TCppIndex_t count);
602602
} // namespace Cpp
603603

604-
CXObject clang_construct(CXScope scope, void* arena) {
604+
CXObject clang_construct(CXScope scope, void* arena, size_t count) {
605605
return Cpp::Construct(*getInterpreter(scope),
606-
static_cast<void*>(getDecl(scope)), arena);
606+
static_cast<void*>(getDecl(scope)), arena, count);
607607
}
608608

609-
void clang_invoke(CXScope func, void* result, void** args, size_t n,
609+
void clang_invoke(CXScope func, void* result, void** args, size_t n, size_t nary,
610610
void* self) {
611611
Cpp::MakeFunctionCallable(getInterpreter(func), getDecl(func))
612-
.Invoke(result, {args, n}, self);
612+
.Invoke(result, {args, n}, self, nary);
613613
}
614614

615615
namespace Cpp {
616616
void Destruct(compat::Interpreter& interp, TCppObject_t This,
617-
clang::Decl* Class, bool withFree);
617+
clang::Decl* Class, bool withFree, size_t nary);
618618
} // namespace Cpp
619619

620-
void clang_destruct(CXObject This, CXScope S, bool withFree) {
621-
Cpp::Destruct(*getInterpreter(S), This, getDecl(S), withFree);
620+
void clang_destruct(CXObject This, CXScope S, bool withFree, size_t nary) {
621+
Cpp::Destruct(*getInterpreter(S), This, getDecl(S), withFree, nary);
622622
}

lib/Interpreter/CppInterOp.cpp

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,13 +1896,17 @@ namespace Cpp {
18961896
void make_narg_ctor(const FunctionDecl* FD, const unsigned N,
18971897
std::ostringstream& typedefbuf,
18981898
std::ostringstream& callbuf,
1899-
const std::string& class_name, int indent_level) {
1899+
const std::string& class_name, int indent_level, bool array = false) {
19001900
// Make a code string that follows this pattern:
19011901
//
19021902
// ClassName(args...)
1903+
// OR
1904+
// ClassName[nary](args...) // array of objects
19031905
//
1904-
1905-
callbuf << class_name << "(";
1906+
1907+
callbuf << class_name;
1908+
if (array) callbuf << "[nary]";
1909+
callbuf << "(";
19061910
for (unsigned i = 0U; i < N; ++i) {
19071911
const ParmVarDecl* PVD = FD->getParamDecl(i);
19081912
QualType Ty = PVD->getType();
@@ -2085,16 +2089,39 @@ namespace Cpp {
20852089
std::ostringstream& buf, int indent_level) {
20862090
// Make a code string that follows this pattern:
20872091
//
2088-
// (*(ClassName**)ret) = (obj) ?
2089-
// new (*(ClassName**)ret) ClassName(args...) : new ClassName(args...);
2090-
//
2092+
// // array of objects construction
2093+
// if (nary > 1) {
2094+
// (*(ClassName**)ret) = (obj) ? new (*(ClassName**)ret) ClassName[nary](args...) : new ClassName[nary](args...);
2095+
// }
2096+
// else {
2097+
// (*(ClassName**)ret) = (obj) ? new (*(ClassName**)ret) ClassName(args...) : new ClassName(args...);
2098+
// }
20912099
{
20922100
std::ostringstream typedefbuf;
20932101
std::ostringstream callbuf;
20942102
//
20952103
// Write the return value assignment part.
20962104
//
20972105
indent(callbuf, indent_level);
2106+
callbuf << "if (nary > 1) {\n";
2107+
indent(callbuf, indent_level);
2108+
callbuf << "(*(" << class_name << "**)ret) = ";
2109+
callbuf << "(obj) ? new (*(" << class_name << "**)ret) ";
2110+
make_narg_ctor(FD, N, typedefbuf, callbuf, class_name, indent_level, true);
2111+
2112+
callbuf << ": new ";
2113+
//
2114+
// Write the actual expression.
2115+
//
2116+
make_narg_ctor(FD, N, typedefbuf, callbuf, class_name, indent_level, true);
2117+
//
2118+
// End the new expression statement.
2119+
//
2120+
callbuf << ";\n";
2121+
indent(callbuf, indent_level);
2122+
callbuf << "}\n";
2123+
callbuf << "else {\n";
2124+
indent(callbuf, indent_level);
20982125
callbuf << "(*(" << class_name << "**)ret) = ";
20992126
callbuf << "(obj) ? new (*(" << class_name << "**)ret) ";
21002127
make_narg_ctor(FD, N, typedefbuf, callbuf, class_name, indent_level);
@@ -2108,6 +2135,8 @@ namespace Cpp {
21082135
// End the new expression statement.
21092136
//
21102137
callbuf << ";\n";
2138+
indent(callbuf, --indent_level);
2139+
callbuf << "}\n";
21112140
//
21122141
// Output the whole new expression and return statement.
21132142
//
@@ -2626,7 +2655,7 @@ namespace Cpp {
26262655
"__attribute__((annotate(\"__cling__ptrcheck(off)\")))\n"
26272656
"extern \"C\" void ";
26282657
buf << wrapper_name;
2629-
buf << "(void* obj, int nargs, void** args, void* ret)\n"
2658+
buf << "(void* obj, int nargs, void** args, void* ret, unsigned long nary)\n"
26302659
"{\n";
26312660
++indent_level;
26322661
if (min_args == num_params) {
@@ -3577,17 +3606,19 @@ namespace Cpp {
35773606
}
35783607
}
35793608

3580-
TCppObject_t Allocate(TCppScope_t scope) {
3581-
return (TCppObject_t)::operator new(Cpp::SizeOf(scope));
3609+
TCppObject_t Allocate(TCppScope_t scope, TCppIndex_t count) {
3610+
return (TCppObject_t)::operator new(Cpp::SizeOf(scope) * count);
35823611
}
35833612

3584-
void Deallocate(TCppScope_t scope, TCppObject_t address) {
3585-
::operator delete(address);
3613+
void Deallocate(TCppScope_t scope, TCppObject_t address, TCppIndex_t count)
3614+
{
3615+
size_t bytes = Cpp::SizeOf(scope) * count;
3616+
::operator delete(address, bytes);
35863617
}
35873618

35883619
// FIXME: Add optional arguments to the operator new.
35893620
TCppObject_t Construct(compat::Interpreter& interp, TCppScope_t scope,
3590-
void* arena /*=nullptr*/) {
3621+
void* arena /*=nullptr*/, TCppIndex_t count /*=1UL*/) {
35913622
auto* Class = (Decl*) scope;
35923623
// FIXME: Diagnose.
35933624
if (!HasDefaultConstructor(Class))
@@ -3596,7 +3627,7 @@ namespace Cpp {
35963627
auto* const Ctor = GetDefaultConstructor(interp, Class);
35973628
if (JitCall JC = MakeFunctionCallable(&interp, Ctor)) {
35983629
if (arena) {
3599-
JC.Invoke(&arena, {}, (void*)~0); // Tell Invoke to use placement new.
3630+
JC.Invoke(&arena, {}, (void*)~0, count); // Tell Invoke to use placement new.
36003631
return arena;
36013632
}
36023633

@@ -3607,22 +3638,22 @@ namespace Cpp {
36073638
return nullptr;
36083639
}
36093640

3610-
TCppObject_t Construct(TCppScope_t scope, void* arena /*=nullptr*/) {
3611-
return Construct(getInterp(), scope, arena);
3641+
TCppObject_t Construct(TCppScope_t scope, void* arena /*=nullptr*/, TCppIndex_t count /*=0UL*/) {
3642+
return Construct(getInterp(), scope, arena, count);
36123643
}
36133644

36143645
void Destruct(compat::Interpreter& interp, TCppObject_t This, Decl* Class,
3615-
bool withFree) {
3646+
bool withFree, TCppIndex_t nary) {
36163647
if (auto wrapper = make_dtor_wrapper(interp, Class)) {
3617-
(*wrapper)(This, /*nary=*/0, withFree);
3648+
(*wrapper)(This, nary, withFree);
36183649
return;
36193650
}
36203651
// FIXME: Diagnose.
36213652
}
36223653

3623-
void Destruct(TCppObject_t This, TCppScope_t scope, bool withFree /*=true*/) {
3654+
void Destruct(TCppObject_t This, TCppScope_t scope, bool withFree /*=true*/, TCppIndex_t count /*=1UL*/) {
36243655
auto* Class = static_cast<Decl*>(scope);
3625-
Destruct(getInterp(), This, Class, withFree);
3656+
Destruct(getInterp(), This, Class, withFree, count);
36263657
}
36273658

36283659
class StreamCaptureInfo {

0 commit comments

Comments
 (0)