Skip to content

Commit 2839d14

Browse files
committed
further simplify by using Cpp::Construct
1 parent c024f3b commit 2839d14

File tree

3 files changed

+27
-41
lines changed

3 files changed

+27
-41
lines changed

core/metacling/src/TClingCallFunc.cxx

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,38 +1418,19 @@ void *TClingCallFunc::ExecDefaultConstructor(const TClingClassInfo *info,
14181418
::Error("TClingCallFunc::ExecDefaultConstructor", "Invalid class info!");
14191419
return nullptr;
14201420
}
1421-
clang::Decl *D = const_cast<clang::Decl *>(info->GetDecl());
1422-
// ensure D is a constructor
1423-
if (!Cpp::IsConstructor(D)) {
1424-
// we could have the scope decl instead
1425-
if (Cpp::IsClass(D)) {
1426-
D = static_cast<clang::Decl *>(Cpp::GetDefaultConstructor(D));
1427-
} else {
1428-
::Error("TClingCallFunc::ExecDefaultConstructor",
1429-
"Could not find a default constructor for the given ClassInfo");
1430-
return nullptr;
1431-
}
1432-
}
1433-
1434-
// generate wrapper with jitcall
1435-
Cpp::JitCall JC = Cpp::MakeFunctionCallable(D);
1436-
if (JC.getKind() != Cpp::JitCall::kConstructorCall) {
1437-
::Error("TClingCallFunc::ExecDefaultConstructor", "Could not generate/compile wrapper for given constructor");
1438-
return nullptr;
1439-
}
1440-
14411421
// this function's clients assume nary = 0 for operator new and nary > 0 for array new. JitCall expects
14421422
// the number of objects you want to construct; nary = 1 constructs a single object
14431423
// This handles this difference in semantics
14441424
if (nary == 0)
14451425
nary = 1;
14461426

1447-
// invoke the constructor (placement/heap) in one shot
1448-
// flag is non-null for placement new, null for normal new
1449-
void *flag = address ? reinterpret_cast<void *>(1) : nullptr;
1450-
void *result = address;
1451-
JC.InvokeConstructor(&result, nary, {}, flag);
1452-
return result;
1427+
clang::Decl *D = const_cast<clang::Decl *>(info->GetDecl());
1428+
1429+
if (Cpp::IsClass(D) || Cpp::IsConstructor(D))
1430+
return Cpp::Construct(D, address, nary);
1431+
1432+
::Error("TClingCallFunc::ExecDefaultConstructor", "ClassInfo missing a valid Scope/Constructor");
1433+
return nullptr;
14531434
}
14541435

14551436
void TClingCallFunc::ExecDestructor(const TClingClassInfo *info, void *address /*=0*/,

interpreter/CppInterOp/include/CppInterOp/CppInterOp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ CPPINTEROP_API void Deallocate(TCppScope_t scope, TCppObject_t address,
849849

850850
/// Creates one or more objects of class \c scope by calling its default
851851
/// constructor.
852-
/// \param[in] scope Class to construct
852+
/// \param[in] scope Class to construct or handle to constructor
853853
/// \param[in] arena If set, this API uses placement new to construct at this
854854
/// address.
855855
/// \param[in] is used to indicate the number of objects to construct.

interpreter/CppInterOp/lib/CppInterOp/CppInterOp.cpp

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3753,22 +3753,27 @@ void Deallocate(TCppScope_t scope, TCppObject_t address, TCppIndex_t count) {
37533753
// FIXME: Add optional arguments to the operator new.
37543754
TCppObject_t Construct(compat::Interpreter& interp, TCppScope_t scope,
37553755
void* arena /*=nullptr*/, TCppIndex_t count /*=1UL*/) {
3756-
auto* Class = (Decl*)scope;
3757-
// FIXME: Diagnose.
3758-
if (!HasDefaultConstructor(Class))
3759-
return nullptr;
37603756

3761-
auto* const Ctor = GetDefaultConstructor(interp, Class);
3762-
if (JitCall JC = MakeFunctionCallable(&interp, Ctor)) {
3763-
if (arena) {
3764-
JC.InvokeConstructor(&arena, count, {},
3765-
(void*)~0); // Tell Invoke to use placement new.
3766-
return arena;
3767-
}
3757+
TCppFunction_t ctor;
3758+
if (Cpp::IsConstructor(scope))
3759+
ctor = scope;
3760+
else if (Cpp::IsClass(scope)) {
3761+
if (!HasDefaultConstructor(scope))
3762+
return nullptr;
3763+
ctor = Cpp::GetDefaultConstructor(scope);
3764+
}
3765+
else
3766+
return nullptr;
37683767

3769-
void* obj = nullptr;
3770-
JC.InvokeConstructor(&obj, count, {}, nullptr);
3771-
return obj;
3768+
if (JitCall JC = MakeFunctionCallable(&interp, ctor)) {
3769+
if (JC.getKind() != Cpp::JitCall::kConstructorCall)
3770+
return nullptr;
3771+
// invoke the constructor (placement/heap) in one shot
3772+
// flag is non-null for placement new, null for normal new
3773+
void* flag = arena ? reinterpret_cast<void*>(1) : nullptr;
3774+
void* result = arena;
3775+
JC.InvokeConstructor(&result, count, {}, flag);
3776+
return result;
37723777
}
37733778
return nullptr;
37743779
}

0 commit comments

Comments
 (0)