Skip to content

Commit 1e81d51

Browse files
committed
Use array new for codegen in JitCall
1 parent a6a4a15 commit 1e81d51

File tree

1 file changed

+101
-33
lines changed

1 file changed

+101
-33
lines changed

lib/CppInterOp/CppInterOp.cpp

Lines changed: 101 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353

5454
#include <algorithm>
5555
#include <cassert>
56+
#include <cstddef>
5657
#include <deque>
5758
#include <iterator>
5859
#include <map>
@@ -1920,42 +1921,58 @@ void collect_type_info(const FunctionDecl* FD, QualType& QT,
19201921

19211922
void make_narg_ctor(const FunctionDecl* FD, const unsigned N,
19221923
std::ostringstream& typedefbuf, std::ostringstream& callbuf,
1923-
const std::string& class_name, int indent_level) {
1924+
const std::string& class_name, int indent_level,
1925+
bool array = false) {
19241926
// Make a code string that follows this pattern:
19251927
//
19261928
// ClassName(args...)
1929+
// OR
1930+
// ClassName[nary] // array of objects
19271931
//
19281932

1929-
callbuf << class_name << "(";
1930-
for (unsigned i = 0U; i < N; ++i) {
1931-
const ParmVarDecl* PVD = FD->getParamDecl(i);
1932-
QualType Ty = PVD->getType();
1933-
QualType QT = Ty.getCanonicalType();
1934-
std::string type_name;
1935-
EReferenceType refType = kNotReference;
1936-
bool isPointer = false;
1937-
collect_type_info(FD, QT, typedefbuf, callbuf, type_name, refType,
1938-
isPointer, indent_level, true);
1939-
if (i) {
1940-
callbuf << ',';
1941-
if (i % 2) {
1942-
callbuf << ' ';
1933+
if (array)
1934+
callbuf << class_name << "[nary]";
1935+
else
1936+
callbuf << class_name;
1937+
1938+
// We cannot pass initialization parameters if we call array new
1939+
if (N && !array) {
1940+
callbuf << "(";
1941+
for (unsigned i = 0U; i < N; ++i) {
1942+
const ParmVarDecl* PVD = FD->getParamDecl(i);
1943+
QualType Ty = PVD->getType();
1944+
QualType QT = Ty.getCanonicalType();
1945+
std::string type_name;
1946+
EReferenceType refType = kNotReference;
1947+
bool isPointer = false;
1948+
collect_type_info(FD, QT, typedefbuf, callbuf, type_name, refType,
1949+
isPointer, indent_level, true);
1950+
if (i) {
1951+
callbuf << ',';
1952+
if (i % 2) {
1953+
callbuf << ' ';
1954+
} else {
1955+
callbuf << "\n";
1956+
indent(callbuf, indent_level);
1957+
}
1958+
}
1959+
if (refType != kNotReference) {
1960+
callbuf << "(" << type_name.c_str()
1961+
<< (refType == kLValueReference ? "&" : "&&") << ")*("
1962+
<< type_name.c_str() << "*)args[" << i << "]";
1963+
} else if (isPointer) {
1964+
callbuf << "*(" << type_name.c_str() << "**)args[" << i << "]";
19431965
} else {
1944-
callbuf << "\n";
1945-
indent(callbuf, indent_level + 1);
1966+
callbuf << "*(" << type_name.c_str() << "*)args[" << i << "]";
19461967
}
19471968
}
1948-
if (refType != kNotReference) {
1949-
callbuf << "(" << type_name.c_str()
1950-
<< (refType == kLValueReference ? "&" : "&&") << ")*("
1951-
<< type_name.c_str() << "*)args[" << i << "]";
1952-
} else if (isPointer) {
1953-
callbuf << "*(" << type_name.c_str() << "**)args[" << i << "]";
1954-
} else {
1955-
callbuf << "*(" << type_name.c_str() << "*)args[" << i << "]";
1956-
}
1969+
callbuf << ")";
1970+
}
1971+
// This can be zero or default-initialized
1972+
else if (const auto* CD = dyn_cast<CXXConstructorDecl>(FD);
1973+
CD && CD->isDefaultConstructor() && !array) {
1974+
callbuf << "()";
19571975
}
1958-
callbuf << ")";
19591976
}
19601977

19611978
const DeclContext* get_non_transparent_decl_context(const FunctionDecl* FD) {
@@ -2115,18 +2132,59 @@ void make_narg_ctor_with_return(const FunctionDecl* FD, const unsigned N,
21152132
std::ostringstream& buf, int indent_level) {
21162133
// Make a code string that follows this pattern:
21172134
//
2118-
// (*(ClassName**)ret) = (obj) ?
2119-
// new (*(ClassName**)ret) ClassName(args...) : new ClassName(args...);
2120-
//
2135+
// Array new if nary has been passed, and nargs is 0 (must be default ctor)
2136+
// if (nary) {
2137+
// (*(ClassName**)ret) = (obj) ? new (*(ClassName**)ret) ClassName[nary] :
2138+
// new ClassName[nary];
2139+
// }
2140+
// else {
2141+
// (*(ClassName**)ret) = (obj) ? new (*(ClassName**)ret) ClassName(args...)
2142+
// : new ClassName(args...);
2143+
// }
21212144
{
21222145
std::ostringstream typedefbuf;
21232146
std::ostringstream callbuf;
21242147
//
21252148
// Write the return value assignment part.
21262149
//
21272150
indent(callbuf, indent_level);
2151+
const auto* CD = dyn_cast<CXXConstructorDecl>(FD);
2152+
2153+
// Activate this block only if array new is possible
2154+
// if (nary) {
2155+
// (*(ClassName**)ret) = (obj) ? new (*(ClassName**)ret) ClassName[nary]
2156+
// : new ClassName[nary];
2157+
// }
2158+
// else {
2159+
if (CD->isDefaultConstructor()) {
2160+
callbuf << "if (nary > 1) {\n";
2161+
indent(callbuf, indent_level);
2162+
callbuf << "(*(" << class_name << "**)ret) = ";
2163+
callbuf << "(is_arena) ? new (*(" << class_name << "**)ret) ";
2164+
make_narg_ctor(FD, N, typedefbuf, callbuf, class_name, indent_level,
2165+
true);
2166+
2167+
callbuf << ": new ";
2168+
//
2169+
// Write the actual expression.
2170+
//
2171+
make_narg_ctor(FD, N, typedefbuf, callbuf, class_name, indent_level,
2172+
true);
2173+
//
2174+
// End the new expression statement.
2175+
//
2176+
callbuf << ";\n";
2177+
indent(callbuf, indent_level);
2178+
callbuf << "}\n";
2179+
callbuf << "else {\n";
2180+
}
2181+
2182+
// Standard branch:
2183+
// (*(ClassName**)ret) = (obj) ? new (*(ClassName**)ret) ClassName(args...)
2184+
// : new ClassName(args...);
2185+
indent(callbuf, indent_level);
21282186
callbuf << "(*(" << class_name << "**)ret) = ";
2129-
callbuf << "(obj) ? new (*(" << class_name << "**)ret) ";
2187+
callbuf << "(is_arena) ? new (*(" << class_name << "**)ret) ";
21302188
make_narg_ctor(FD, N, typedefbuf, callbuf, class_name, indent_level);
21312189

21322190
callbuf << ": new ";
@@ -2138,6 +2196,10 @@ void make_narg_ctor_with_return(const FunctionDecl* FD, const unsigned N,
21382196
// End the new expression statement.
21392197
//
21402198
callbuf << ";\n";
2199+
indent(callbuf, --indent_level);
2200+
if (CD->isDefaultConstructor())
2201+
callbuf << "}\n";
2202+
21412203
//
21422204
// Output the whole new expression and return statement.
21432205
//
@@ -2652,8 +2714,14 @@ int get_wrapper_code(compat::Interpreter& I, const FunctionDecl* FD,
26522714
"__attribute__((annotate(\"__cling__ptrcheck(off)\")))\n"
26532715
"extern \"C\" void ";
26542716
buf << wrapper_name;
2655-
buf << "(void* obj, int nargs, void** args, void* ret)\n"
2656-
"{\n";
2717+
if (Cpp::IsConstructor(FD)) {
2718+
buf << "(void* ret, unsigned long nary, unsigned long nargs, void** args, "
2719+
"void* is_arena)\n"
2720+
"{\n";
2721+
} else
2722+
buf << "(void* obj, unsigned long nargs, void** args, void* ret)\n"
2723+
"{\n";
2724+
26572725
++indent_level;
26582726
if (min_args == num_params) {
26592727
// No parameters with defaults.

0 commit comments

Comments
 (0)