Skip to content

Commit 8cead7e

Browse files
committed
Add array new for ctor codegen in JitCall
1 parent 81a81ba commit 8cead7e

File tree

1 file changed

+94
-33
lines changed

1 file changed

+94
-33
lines changed

lib/CppInterOp/CppInterOp.cpp

Lines changed: 94 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>
@@ -1902,42 +1903,51 @@ void collect_type_info(const FunctionDecl* FD, QualType& QT,
19021903

19031904
void make_narg_ctor(const FunctionDecl* FD, const unsigned N,
19041905
std::ostringstream& typedefbuf, std::ostringstream& callbuf,
1905-
const std::string& class_name, int indent_level) {
1906+
const std::string& class_name, int indent_level,
1907+
bool array = false) {
19061908
// Make a code string that follows this pattern:
19071909
//
19081910
// ClassName(args...)
1911+
// OR
1912+
// ClassName[nary] // array of objects
19091913
//
19101914

1911-
callbuf << class_name << "(";
1912-
for (unsigned i = 0U; i < N; ++i) {
1913-
const ParmVarDecl* PVD = FD->getParamDecl(i);
1914-
QualType Ty = PVD->getType();
1915-
QualType QT = Ty.getCanonicalType();
1916-
std::string type_name;
1917-
EReferenceType refType = kNotReference;
1918-
bool isPointer = false;
1919-
collect_type_info(FD, QT, typedefbuf, callbuf, type_name, refType,
1920-
isPointer, indent_level, true);
1921-
if (i) {
1922-
callbuf << ',';
1923-
if (i % 2) {
1924-
callbuf << ' ';
1915+
if (array)
1916+
callbuf << class_name << "[nary]";
1917+
else
1918+
callbuf << class_name;
1919+
if (N) {
1920+
callbuf << "(";
1921+
for (unsigned i = 0U; i < N; ++i) {
1922+
const ParmVarDecl* PVD = FD->getParamDecl(i);
1923+
QualType Ty = PVD->getType();
1924+
QualType QT = Ty.getCanonicalType();
1925+
std::string type_name;
1926+
EReferenceType refType = kNotReference;
1927+
bool isPointer = false;
1928+
collect_type_info(FD, QT, typedefbuf, callbuf, type_name, refType,
1929+
isPointer, indent_level, true);
1930+
if (i) {
1931+
callbuf << ',';
1932+
if (i % 2) {
1933+
callbuf << ' ';
1934+
} else {
1935+
callbuf << "\n";
1936+
indent(callbuf, indent_level);
1937+
}
1938+
}
1939+
if (refType != kNotReference) {
1940+
callbuf << "(" << type_name.c_str()
1941+
<< (refType == kLValueReference ? "&" : "&&") << ")*("
1942+
<< type_name.c_str() << "*)args[" << i << "]";
1943+
} else if (isPointer) {
1944+
callbuf << "*(" << type_name.c_str() << "**)args[" << i << "]";
19251945
} else {
1926-
callbuf << "\n";
1927-
indent(callbuf, indent_level + 1);
1946+
callbuf << "*(" << type_name.c_str() << "*)args[" << i << "]";
19281947
}
19291948
}
1930-
if (refType != kNotReference) {
1931-
callbuf << "(" << type_name.c_str()
1932-
<< (refType == kLValueReference ? "&" : "&&") << ")*("
1933-
<< type_name.c_str() << "*)args[" << i << "]";
1934-
} else if (isPointer) {
1935-
callbuf << "*(" << type_name.c_str() << "**)args[" << i << "]";
1936-
} else {
1937-
callbuf << "*(" << type_name.c_str() << "*)args[" << i << "]";
1938-
}
1949+
callbuf << ")";
19391950
}
1940-
callbuf << ")";
19411951
}
19421952

19431953
const DeclContext* get_non_transparent_decl_context(const FunctionDecl* FD) {
@@ -2095,18 +2105,59 @@ void make_narg_ctor_with_return(const FunctionDecl* FD, const unsigned N,
20952105
std::ostringstream& buf, int indent_level) {
20962106
// Make a code string that follows this pattern:
20972107
//
2098-
// (*(ClassName**)ret) = (obj) ?
2099-
// new (*(ClassName**)ret) ClassName(args...) : new ClassName(args...);
2100-
//
2108+
// Array new if nary has been passed, and nargs is 0 (must be default ctor)
2109+
// if (nary) {
2110+
// (*(ClassName**)ret) = (obj) ? new (*(ClassName**)ret) ClassName[nary] :
2111+
// new ClassName[nary];
2112+
// }
2113+
// else {
2114+
// (*(ClassName**)ret) = (obj) ? new (*(ClassName**)ret) ClassName(args...)
2115+
// : new ClassName(args...);
2116+
// }
21012117
{
21022118
std::ostringstream typedefbuf;
21032119
std::ostringstream callbuf;
21042120
//
21052121
// Write the return value assignment part.
21062122
//
21072123
indent(callbuf, indent_level);
2124+
const auto* CD = dyn_cast<CXXConstructorDecl>(FD);
2125+
2126+
// Activate this block only if array new is possible
2127+
// if (nary) {
2128+
// (*(ClassName**)ret) = (obj) ? new (*(ClassName**)ret) ClassName[nary]
2129+
// : new ClassName[nary];
2130+
// }
2131+
// else {
2132+
if (CD->getNumParams() == 0) {
2133+
callbuf << "if (nary > 1) {\n";
2134+
indent(callbuf, indent_level);
2135+
callbuf << "(*(" << class_name << "**)ret) = ";
2136+
callbuf << "(is_arena) ? new (*(" << class_name << "**)ret) ";
2137+
make_narg_ctor(FD, N, typedefbuf, callbuf, class_name, indent_level,
2138+
true);
2139+
2140+
callbuf << ": new ";
2141+
//
2142+
// Write the actual expression.
2143+
//
2144+
make_narg_ctor(FD, N, typedefbuf, callbuf, class_name, indent_level,
2145+
true);
2146+
//
2147+
// End the new expression statement.
2148+
//
2149+
callbuf << ";\n";
2150+
indent(callbuf, indent_level);
2151+
callbuf << "}\n";
2152+
callbuf << "else {\n";
2153+
}
2154+
2155+
// Standard branch:
2156+
// (*(ClassName**)ret) = (obj) ? new (*(ClassName**)ret) ClassName(args...)
2157+
// : new ClassName(args...);
2158+
indent(callbuf, indent_level);
21082159
callbuf << "(*(" << class_name << "**)ret) = ";
2109-
callbuf << "(obj) ? new (*(" << class_name << "**)ret) ";
2160+
callbuf << "(is_arena) ? new (*(" << class_name << "**)ret) ";
21102161
make_narg_ctor(FD, N, typedefbuf, callbuf, class_name, indent_level);
21112162

21122163
callbuf << ": new ";
@@ -2118,6 +2169,10 @@ void make_narg_ctor_with_return(const FunctionDecl* FD, const unsigned N,
21182169
// End the new expression statement.
21192170
//
21202171
callbuf << ";\n";
2172+
indent(callbuf, --indent_level);
2173+
if (CD->getNumParams() == 0)
2174+
callbuf << "}\n";
2175+
21212176
//
21222177
// Output the whole new expression and return statement.
21232178
//
@@ -2630,8 +2685,14 @@ int get_wrapper_code(compat::Interpreter& I, const FunctionDecl* FD,
26302685
"__attribute__((annotate(\"__cling__ptrcheck(off)\")))\n"
26312686
"extern \"C\" void ";
26322687
buf << wrapper_name;
2633-
buf << "(void* obj, int nargs, void** args, void* ret)\n"
2634-
"{\n";
2688+
if (Cpp::IsConstructor(FD)) {
2689+
buf << "(void* ret, unsigned long nary, unsigned long nargs, void** args, "
2690+
"void* is_arena)\n"
2691+
"{\n";
2692+
} else
2693+
buf << "(void* obj, unsigned long nargs, void** args, void* ret)\n"
2694+
"{\n";
2695+
26352696
++indent_level;
26362697
if (min_args == num_params) {
26372698
// No parameters with defaults.

0 commit comments

Comments
 (0)