cpp_thunk.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/check/cpp_thunk.h"
  5. #include "clang/AST/GlobalDecl.h"
  6. #include "clang/AST/Mangle.h"
  7. #include "clang/Sema/Sema.h"
  8. #include "toolchain/check/call.h"
  9. #include "toolchain/check/context.h"
  10. #include "toolchain/check/control_flow.h"
  11. #include "toolchain/check/literal.h"
  12. #include "toolchain/check/type.h"
  13. #include "toolchain/check/type_completion.h"
  14. #include "toolchain/sem_ir/ids.h"
  15. #include "toolchain/sem_ir/typed_insts.h"
  16. namespace Carbon::Check {
  17. // Returns the C++ thunk mangled name given the callee function.
  18. static auto GenerateThunkMangledName(
  19. clang::MangleContext& mangle_context,
  20. const clang::FunctionDecl& callee_function_decl) -> std::string {
  21. RawStringOstream mangled_name_stream;
  22. mangle_context.mangleName(clang::GlobalDecl(&callee_function_decl),
  23. mangled_name_stream);
  24. mangled_name_stream << ".carbon_thunk";
  25. return mangled_name_stream.TakeStr();
  26. }
  27. // Returns true if a C++ thunk is required for the given type. A C++ thunk is
  28. // required for any type except for void, pointer types and signed 32-bit and
  29. // 64-bit integers.
  30. static auto IsThunkRequiredForType(Context& context, SemIR::TypeId type_id)
  31. -> bool {
  32. if (!type_id.has_value() || type_id == SemIR::ErrorInst::TypeId) {
  33. return false;
  34. }
  35. type_id = context.types().GetUnqualifiedType(type_id);
  36. switch (context.types().GetAsInst(type_id).kind()) {
  37. case SemIR::PointerType::Kind: {
  38. return false;
  39. }
  40. case SemIR::ClassType::Kind: {
  41. if (!context.types().IsComplete(type_id)) {
  42. // Signed integers of 32 or 64 bits should be completed when imported.
  43. return true;
  44. }
  45. if (!context.types().IsSignedInt(type_id)) {
  46. return true;
  47. }
  48. llvm::APInt bit_width =
  49. context.ints().Get(context.types().GetIntTypeInfo(type_id).bit_width);
  50. return bit_width != 32 && bit_width != 64;
  51. }
  52. default:
  53. return true;
  54. }
  55. }
  56. auto IsCppThunkRequired(Context& context, const SemIR::Function& function)
  57. -> bool {
  58. if (!function.clang_decl_id.has_value()) {
  59. return false;
  60. }
  61. if (function.self_param_id.has_value()) {
  62. // TODO: Support member methods.
  63. return false;
  64. }
  65. SemIR::TypeId return_type_id =
  66. function.GetDeclaredReturnType(context.sem_ir());
  67. if (return_type_id.has_value()) {
  68. // TODO: Support non-void return values.
  69. return false;
  70. }
  71. bool thunk_required_for_param = false;
  72. for (auto param_id :
  73. context.inst_blocks().GetOrEmpty(function.call_params_id)) {
  74. if (param_id == SemIR::ErrorInst::InstId) {
  75. return false;
  76. }
  77. if (!thunk_required_for_param &&
  78. IsThunkRequiredForType(
  79. context,
  80. context.insts().GetAs<SemIR::AnyParam>(param_id).type_id)) {
  81. thunk_required_for_param = true;
  82. }
  83. }
  84. return thunk_required_for_param;
  85. }
  86. // Returns whether the type is a pointer or a signed int of 32 or 64 bits.
  87. static auto IsSimpleAbiType(clang::ASTContext& ast_context,
  88. clang::QualType type) -> bool {
  89. if (type->isPointerType()) {
  90. return true;
  91. }
  92. if (const auto* builtin_type = type->getAs<clang::BuiltinType>()) {
  93. if (builtin_type->isSignedInteger()) {
  94. uint64_t type_size = ast_context.getIntWidth(type);
  95. return type_size == 32 || type_size == 64;
  96. }
  97. }
  98. return false;
  99. }
  100. // Creates the thunk parameter types given the callee function. Also returns for
  101. // each type whether it is different from the matching callee function parameter
  102. // type.
  103. static auto BuildThunkParameterTypes(
  104. clang::ASTContext& ast_context,
  105. const clang::FunctionDecl& callee_function_decl)
  106. -> std::tuple<llvm::SmallVector<clang::QualType>, llvm::SmallVector<bool>> {
  107. std::tuple<llvm::SmallVector<clang::QualType>, llvm::SmallVector<bool>>
  108. result;
  109. auto& [thunk_param_types, param_type_changed] = result;
  110. unsigned num_params = callee_function_decl.getNumParams();
  111. thunk_param_types.reserve(num_params);
  112. param_type_changed.reserve(num_params);
  113. for (const clang::ParmVarDecl* callee_param :
  114. callee_function_decl.parameters()) {
  115. clang::QualType param_type = callee_param->getType();
  116. bool is_simple_abi_type = IsSimpleAbiType(ast_context, param_type);
  117. if (!is_simple_abi_type) {
  118. clang::QualType pointer_type = ast_context.getPointerType(param_type);
  119. param_type = ast_context.getAttributedType(
  120. clang::NullabilityKind::NonNull, pointer_type, pointer_type);
  121. }
  122. param_type_changed.push_back(!is_simple_abi_type);
  123. thunk_param_types.push_back(param_type);
  124. }
  125. return result;
  126. }
  127. // Returns the thunk parameters using the callee function parameter identifiers.
  128. static auto BuildThunkParameters(
  129. clang::ASTContext& ast_context,
  130. const clang::FunctionDecl& callee_function_decl,
  131. clang::FunctionDecl* thunk_function_decl)
  132. -> llvm::SmallVector<clang::ParmVarDecl*> {
  133. clang::SourceLocation clang_loc = callee_function_decl.getLocation();
  134. unsigned num_params = thunk_function_decl->getNumParams();
  135. CARBON_CHECK(callee_function_decl.getNumParams() == num_params);
  136. const auto* thunk_function_proto_type =
  137. thunk_function_decl->getFunctionType()->getAs<clang::FunctionProtoType>();
  138. llvm::SmallVector<clang::ParmVarDecl*> thunk_params;
  139. thunk_params.reserve(num_params);
  140. for (unsigned i = 0; i < num_params; ++i) {
  141. clang::ParmVarDecl* thunk_param = clang::ParmVarDecl::Create(
  142. ast_context, thunk_function_decl, clang_loc, clang_loc,
  143. callee_function_decl.getParamDecl(i)->getIdentifier(),
  144. thunk_function_proto_type->getParamType(i), nullptr, clang::SC_None,
  145. nullptr);
  146. thunk_params.push_back(thunk_param);
  147. }
  148. return thunk_params;
  149. }
  150. // Returns the thunk function declaration given the callee function and the
  151. // thunk parameter types.
  152. static auto CreateThunkFunctionDecl(
  153. Context& context, const clang::FunctionDecl& callee_function_decl,
  154. llvm::ArrayRef<clang::QualType> thunk_param_types) -> clang::FunctionDecl* {
  155. clang::ASTContext& ast_context = context.ast_context();
  156. clang::SourceLocation clang_loc = callee_function_decl.getLocation();
  157. clang::IdentifierInfo& identifier_info = ast_context.Idents.get(
  158. callee_function_decl.getNameAsString() + "__carbon_thunk");
  159. const auto* callee_function_type = callee_function_decl.getFunctionType()
  160. ->castAs<clang::FunctionProtoType>();
  161. // TODO: Check whether we need to modify `ExtParameterInfo` in `ExtProtoInfo`.
  162. clang::QualType thunk_function_type = ast_context.getFunctionType(
  163. callee_function_decl.getReturnType(), thunk_param_types,
  164. callee_function_type->getExtProtoInfo());
  165. clang::DeclContext* decl_context = ast_context.getTranslationUnitDecl();
  166. // TODO: Thunks should not have external linkage, consider using `SC_Static`.
  167. clang::FunctionDecl* thunk_function_decl = clang::FunctionDecl::Create(
  168. ast_context, decl_context, clang_loc, clang_loc,
  169. clang::DeclarationName(&identifier_info), thunk_function_type,
  170. /*TInfo=*/nullptr, clang::SC_Extern);
  171. decl_context->addDecl(thunk_function_decl);
  172. thunk_function_decl->setParams(BuildThunkParameters(
  173. ast_context, callee_function_decl, thunk_function_decl));
  174. // Set always_inline.
  175. thunk_function_decl->addAttr(
  176. clang::AlwaysInlineAttr::CreateImplicit(ast_context));
  177. // Set asm("<callee function mangled name>.carbon_thunk").
  178. thunk_function_decl->addAttr(clang::AsmLabelAttr::CreateImplicit(
  179. ast_context,
  180. GenerateThunkMangledName(*context.sem_ir().clang_mangle_context(),
  181. callee_function_decl),
  182. clang_loc));
  183. return thunk_function_decl;
  184. }
  185. // Takes the thunk function parameters and for each one creates an arg for the
  186. // callee function which is the thunk parameter or its address.
  187. static auto BuildCalleeArgs(clang::Sema& sema,
  188. clang::FunctionDecl* thunk_function_decl,
  189. llvm::ArrayRef<bool> param_type_changed)
  190. -> llvm::SmallVector<clang::Expr*> {
  191. llvm::SmallVector<clang::Expr*> call_args;
  192. size_t num_params = thunk_function_decl->getNumParams();
  193. CARBON_CHECK(param_type_changed.size() == num_params);
  194. call_args.reserve(num_params);
  195. for (unsigned i = 0; i < num_params; ++i) {
  196. clang::ParmVarDecl* thunk_param = thunk_function_decl->getParamDecl(i);
  197. clang::SourceLocation clang_loc = thunk_param->getLocation();
  198. clang::Expr* call_arg = sema.BuildDeclRefExpr(
  199. thunk_param, thunk_param->getType(), clang::VK_LValue, clang_loc);
  200. if (param_type_changed[i]) {
  201. // TODO: Insert a cast to an rvalue.
  202. clang::ExprResult deref_result =
  203. sema.BuildUnaryOp(nullptr, clang_loc, clang::UO_Deref, call_arg);
  204. CARBON_CHECK(deref_result.isUsable());
  205. call_arg = deref_result.get();
  206. }
  207. call_args.push_back(call_arg);
  208. }
  209. return call_args;
  210. }
  211. // Builds the thunk function body which calls the callee function using the call
  212. // args and returns the callee function return value. Returns nullptr on
  213. // failure.
  214. static auto BuildThunkBody(clang::Sema& sema,
  215. clang::FunctionDecl* callee_function_decl,
  216. llvm::MutableArrayRef<clang::Expr*> call_args)
  217. -> clang::Stmt* {
  218. clang::SourceLocation clang_loc = callee_function_decl->getLocation();
  219. clang::DeclRefExpr* callee_function_ref = sema.BuildDeclRefExpr(
  220. callee_function_decl, callee_function_decl->getType(), clang::VK_PRValue,
  221. clang_loc);
  222. clang::ExprResult call_result = sema.BuildCallExpr(
  223. nullptr, callee_function_ref, clang_loc, call_args, clang_loc);
  224. if (!call_result.isUsable()) {
  225. return nullptr;
  226. }
  227. clang::Expr* call = call_result.get();
  228. clang::StmtResult return_result = sema.BuildReturnStmt(clang_loc, call);
  229. CARBON_CHECK(return_result.isUsable());
  230. return return_result.get();
  231. }
  232. auto BuildCppThunk(Context& context, const SemIR::Function& callee_function)
  233. -> clang::FunctionDecl* {
  234. clang::FunctionDecl* callee_function_decl =
  235. context.sem_ir()
  236. .clang_decls()
  237. .Get(callee_function.clang_decl_id)
  238. .decl->getAsFunction();
  239. CARBON_CHECK(callee_function_decl);
  240. // Build the thunk function declaration.
  241. auto [thunk_param_types, param_type_changed] =
  242. BuildThunkParameterTypes(context.ast_context(), *callee_function_decl);
  243. clang::FunctionDecl* thunk_function_decl = CreateThunkFunctionDecl(
  244. context, *callee_function_decl, thunk_param_types);
  245. // Build the thunk function body.
  246. clang::Sema& sema = context.sem_ir().clang_ast_unit()->getSema();
  247. clang::Sema::ContextRAII context_raii(sema, thunk_function_decl);
  248. sema.ActOnStartOfFunctionDef(nullptr, thunk_function_decl);
  249. llvm::SmallVector<clang::Expr*> call_args =
  250. BuildCalleeArgs(sema, thunk_function_decl, param_type_changed);
  251. clang::Stmt* body = BuildThunkBody(sema, callee_function_decl, call_args);
  252. sema.ActOnFinishFunctionBody(thunk_function_decl, body);
  253. if (!body) {
  254. return nullptr;
  255. }
  256. return thunk_function_decl;
  257. }
  258. auto PerformCppThunkCall(Context& context, SemIR::LocId loc_id,
  259. SemIR::FunctionId callee_function_id,
  260. llvm::ArrayRef<SemIR::InstId> callee_arg_ids,
  261. SemIR::InstId thunk_callee_id) -> SemIR::InstId {
  262. llvm::ArrayRef<SemIR::InstId> callee_function_params =
  263. context.inst_blocks().GetOrEmpty(
  264. context.functions().Get(callee_function_id).call_params_id);
  265. llvm::ArrayRef<SemIR::InstId> thunk_function_params =
  266. context.inst_blocks().GetOrEmpty(
  267. context.functions()
  268. .Get(GetCalleeFunction(context.sem_ir(), thunk_callee_id)
  269. .function_id)
  270. .call_params_id);
  271. size_t num_params = callee_function_params.size();
  272. CARBON_CHECK(thunk_function_params.size() == num_params);
  273. CARBON_CHECK(callee_arg_ids.size() == num_params);
  274. llvm::SmallVector<SemIR::InstId> thunk_arg_ids;
  275. thunk_arg_ids.reserve(callee_arg_ids.size());
  276. for (size_t i = 0; i < callee_function_params.size(); ++i) {
  277. SemIR::TypeId callee_param_type_id =
  278. context.insts()
  279. .GetAs<SemIR::AnyParam>(callee_function_params[i])
  280. .type_id;
  281. SemIR::TypeId thunk_param_type_id =
  282. context.insts()
  283. .GetAs<SemIR::AnyParam>(thunk_function_params[i])
  284. .type_id;
  285. SemIR::InstId arg_id = callee_arg_ids[i];
  286. if (callee_param_type_id != thunk_param_type_id) {
  287. CARBON_CHECK(thunk_param_type_id ==
  288. GetPointerType(context, context.types().GetInstId(
  289. callee_param_type_id)));
  290. // TODO: Don't create storage if it's already in a storage (depends on
  291. // expression category).
  292. SemIR::InstId temporary_storage_inst_id = AddInstWithCleanup(
  293. context, loc_id,
  294. SemIR::TemporaryStorage{.type_id = callee_param_type_id});
  295. AddInst(context, loc_id,
  296. SemIR::InitializeFrom{.type_id = callee_param_type_id,
  297. .src_id = arg_id,
  298. .dest_id = temporary_storage_inst_id});
  299. // TODO: Do not use `InitializeFrom` directly. Use the `Initialize`
  300. // machinery. See
  301. // https://github.com/carbon-language/carbon-lang/pull/5850/files#r2249030529.
  302. arg_id = AddInst(context, loc_id,
  303. SemIR::AddrOf{.type_id = thunk_param_type_id,
  304. .lvalue_id = temporary_storage_inst_id});
  305. }
  306. thunk_arg_ids.push_back(arg_id);
  307. }
  308. return PerformCall(context, loc_id, thunk_callee_id, thunk_arg_ids);
  309. }
  310. } // namespace Carbon::Check