Skip to content

Commit 5b328da

Browse files
authored
Avoid creating a vector of bools to mark which C++ thunk parameter types were modified (#5941)
Instead, check which types were modified. This simplifies the logic and could make it easier to add return value support. Part of #5514.
1 parent fd3eb13 commit 5b328da

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed

toolchain/check/cpp_thunk.cpp

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,13 @@ static auto IsSimpleAbiType(clang::ASTContext& ast_context,
119119
return false;
120120
}
121121

122-
// Creates the thunk parameter types given the callee function. Also returns for
123-
// each type whether it is different from the matching callee function parameter
124-
// type.
122+
// Creates the thunk parameter types given the callee function.
125123
static auto BuildThunkParameterTypes(
126124
clang::ASTContext& ast_context,
127125
const clang::FunctionDecl& callee_function_decl)
128-
-> std::tuple<llvm::SmallVector<clang::QualType>, llvm::SmallVector<bool>> {
129-
std::tuple<llvm::SmallVector<clang::QualType>, llvm::SmallVector<bool>>
130-
result;
131-
auto& [thunk_param_types, param_type_changed] = result;
132-
133-
unsigned num_params = callee_function_decl.getNumParams();
134-
thunk_param_types.reserve(num_params);
135-
param_type_changed.reserve(num_params);
126+
-> llvm::SmallVector<clang::QualType> {
127+
llvm::SmallVector<clang::QualType> thunk_param_types;
128+
thunk_param_types.reserve(callee_function_decl.getNumParams());
136129

137130
for (const clang::ParmVarDecl* callee_param :
138131
callee_function_decl.parameters()) {
@@ -143,11 +136,10 @@ static auto BuildThunkParameterTypes(
143136
param_type = ast_context.getAttributedType(
144137
clang::NullabilityKind::NonNull, pointer_type, pointer_type);
145138
}
146-
param_type_changed.push_back(!is_simple_abi_type);
147139
thunk_param_types.push_back(param_type);
148140
}
149141

150-
return result;
142+
return thunk_param_types;
151143
}
152144

153145
// Returns the thunk parameters using the callee function parameter identifiers.
@@ -225,19 +217,20 @@ static auto CreateThunkFunctionDecl(
225217
// callee function which is the thunk parameter or its address.
226218
static auto BuildCalleeArgs(clang::Sema& sema,
227219
clang::FunctionDecl* thunk_function_decl,
228-
llvm::ArrayRef<bool> param_type_changed)
220+
const clang::FunctionDecl& callee_function_decl)
229221
-> llvm::SmallVector<clang::Expr*> {
230222
llvm::SmallVector<clang::Expr*> call_args;
231223
size_t num_params = thunk_function_decl->getNumParams();
232-
CARBON_CHECK(param_type_changed.size() == num_params);
224+
CARBON_CHECK(callee_function_decl.getNumParams() == num_params);
233225
call_args.reserve(num_params);
234226
for (unsigned i = 0; i < num_params; ++i) {
235227
clang::ParmVarDecl* thunk_param = thunk_function_decl->getParamDecl(i);
236228
clang::SourceLocation clang_loc = thunk_param->getLocation();
237229

238230
clang::Expr* call_arg = sema.BuildDeclRefExpr(
239231
thunk_param, thunk_param->getType(), clang::VK_LValue, clang_loc);
240-
if (param_type_changed[i]) {
232+
if (thunk_param->getType() !=
233+
callee_function_decl.getParamDecl(i)->getType()) {
241234
// TODO: Consider inserting a cast to an rvalue. Note that we currently
242235
// pass pointers to non-temporary objects as the argument when calling a
243236
// thunk, so we'll need to either change that or generate different thunks
@@ -288,7 +281,7 @@ auto BuildCppThunk(Context& context, const SemIR::Function& callee_function)
288281
CARBON_CHECK(callee_function_decl);
289282

290283
// Build the thunk function declaration.
291-
auto [thunk_param_types, param_type_changed] =
284+
auto thunk_param_types =
292285
BuildThunkParameterTypes(context.ast_context(), *callee_function_decl);
293286
clang::FunctionDecl* thunk_function_decl = CreateThunkFunctionDecl(
294287
context, *callee_function_decl, thunk_param_types);
@@ -299,7 +292,7 @@ auto BuildCppThunk(Context& context, const SemIR::Function& callee_function)
299292
sema.ActOnStartOfFunctionDef(nullptr, thunk_function_decl);
300293

301294
llvm::SmallVector<clang::Expr*> call_args =
302-
BuildCalleeArgs(sema, thunk_function_decl, param_type_changed);
295+
BuildCalleeArgs(sema, thunk_function_decl, *callee_function_decl);
303296
clang::Stmt* body = BuildThunkBody(sema, callee_function_decl, call_args);
304297
sema.ActOnFinishFunctionBody(thunk_function_decl, body);
305298
if (!body) {

0 commit comments

Comments
 (0)