thunk.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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/thunk.h"
  5. #include <utility>
  6. #include "toolchain/base/kind_switch.h"
  7. #include "toolchain/check/call.h"
  8. #include "toolchain/check/convert.h"
  9. #include "toolchain/check/cpp/operators.h"
  10. #include "toolchain/check/deferred_definition_worklist.h"
  11. #include "toolchain/check/diagnostic_helpers.h"
  12. #include "toolchain/check/function.h"
  13. #include "toolchain/check/generic.h"
  14. #include "toolchain/check/inst.h"
  15. #include "toolchain/check/member_access.h"
  16. #include "toolchain/check/name_ref.h"
  17. #include "toolchain/check/pattern.h"
  18. #include "toolchain/check/pattern_match.h"
  19. #include "toolchain/check/pointer_dereference.h"
  20. #include "toolchain/check/return.h"
  21. #include "toolchain/check/type.h"
  22. #include "toolchain/diagnostics/diagnostic.h"
  23. #include "toolchain/sem_ir/function.h"
  24. #include "toolchain/sem_ir/generic.h"
  25. #include "toolchain/sem_ir/ids.h"
  26. #include "toolchain/sem_ir/inst.h"
  27. #include "toolchain/sem_ir/pattern.h"
  28. #include "toolchain/sem_ir/typed_insts.h"
  29. namespace Carbon::Check {
  30. // Adds a pattern instruction for a thunk, copying the location from an existing
  31. // instruction.
  32. static auto RebuildPatternInst(Context& context, SemIR::InstId orig_inst_id,
  33. SemIR::Inst new_inst) -> SemIR::InstId {
  34. // Ensure we built the same kind of instruction. In particular, this ensures
  35. // that the location of the old instruction can be reused for the new one.
  36. CARBON_CHECK(context.insts().Get(orig_inst_id).kind() == new_inst.kind(),
  37. "Rebuilt pattern with the wrong kind: {0} -> {1}",
  38. context.insts().Get(orig_inst_id), new_inst);
  39. return AddPatternInst(context, SemIR::LocIdAndInst::UncheckedLoc(
  40. SemIR::LocId(orig_inst_id), new_inst));
  41. }
  42. // Wrapper to allow the type to be specified as a template argument for API
  43. // consistency with `AddInst`.
  44. template <typename InstT>
  45. static auto RebuildPatternInst(Context& context, SemIR::InstId orig_inst_id,
  46. InstT new_inst) -> SemIR::InstId {
  47. return RebuildPatternInst(context, orig_inst_id, SemIR::Inst(new_inst));
  48. }
  49. // Makes a copy of the given binding pattern, with its type adjusted to be
  50. // `new_pattern_type_id`.
  51. static auto CloneBindingPattern(Context& context, SemIR::InstId pattern_id,
  52. SemIR::AnyBindingPattern pattern,
  53. SemIR::TypeId new_pattern_type_id)
  54. -> SemIR::InstId {
  55. auto entity_name = context.entity_names().Get(pattern.entity_name_id);
  56. CARBON_CHECK((pattern.kind == SemIR::SymbolicBindingPattern::Kind) ==
  57. entity_name.bind_index().has_value());
  58. CARBON_CHECK((pattern.kind == SemIR::FormBindingPattern::Kind) ==
  59. entity_name.form_id.has_value());
  60. if (pattern.kind == SemIR::FormBindingPattern::Kind) {
  61. context.TODO(pattern_id, "Support for cloning form bindings");
  62. return SemIR::ErrorInst::InstId;
  63. }
  64. // Get the transformed type of the binding.
  65. if (new_pattern_type_id == SemIR::ErrorInst::TypeId) {
  66. return SemIR::ErrorInst::InstId;
  67. }
  68. auto type_inst_id = context.types()
  69. .GetAs<SemIR::PatternType>(new_pattern_type_id)
  70. .scrutinee_type_inst_id;
  71. auto type_id = context.types().GetTypeIdForTypeInstId(type_inst_id);
  72. auto type_expr_region_id = context.sem_ir().expr_regions().Add(
  73. {.block_ids = {SemIR::InstBlockId::Empty}, .result_id = type_inst_id});
  74. // Rebuild the binding pattern.
  75. return AddBindingPattern(
  76. context, SemIR::LocId(pattern_id), entity_name.name_id, type_id,
  77. /*form_id=*/SemIR::ConstantId::None, type_expr_region_id,
  78. pattern.kind, entity_name.is_template,
  79. /*is_unused=*/false)
  80. .pattern_id;
  81. }
  82. // Makes a copy of the given pattern instruction, substituting values from a
  83. // specific as needed. The resulting pattern behaves like a newly-created
  84. // pattern, so is suitable for running `CalleePatternMatch` against.
  85. static auto ClonePattern(Context& context, SemIR::SpecificId specific_id,
  86. SemIR::InstId pattern_id) -> SemIR::InstId {
  87. if (!pattern_id.has_value()) {
  88. return SemIR::InstId::None;
  89. }
  90. auto get_type = [&](SemIR::InstId inst_id) -> SemIR::TypeId {
  91. return SemIR::GetTypeOfInstInSpecific(context.sem_ir(), specific_id,
  92. inst_id);
  93. };
  94. auto pattern = context.insts().Get(pattern_id);
  95. // Decompose the pattern. The forms we allow for patterns in a function
  96. // parameter list are currently fairly restrictive.
  97. // Optional parameter pattern.
  98. auto [param, param_id] = context.insts().TryUnwrap(
  99. pattern, pattern_id, &SemIR::AnyParamPattern::subpattern_id);
  100. // Finally, either a binding pattern or a return slot pattern.
  101. auto new_pattern_id = SemIR::InstId::None;
  102. if (auto binding = pattern.TryAs<SemIR::AnyBindingPattern>()) {
  103. new_pattern_id = CloneBindingPattern(context, pattern_id, *binding,
  104. get_type(pattern_id));
  105. } else if (auto return_slot = pattern.TryAs<SemIR::ReturnSlotPattern>()) {
  106. new_pattern_id = RebuildPatternInst<SemIR::ReturnSlotPattern>(
  107. context, pattern_id,
  108. {.type_id = get_type(pattern_id),
  109. .type_inst_id = SemIR::TypeInstId::None});
  110. } else {
  111. CARBON_CHECK(pattern.Is<SemIR::ErrorInst>(),
  112. "Unexpected pattern {0} in function signature", pattern);
  113. return SemIR::ErrorInst::InstId;
  114. }
  115. // Rebuild parameter.
  116. if (param) {
  117. new_pattern_id = RebuildPatternInst<SemIR::AnyParamPattern>(
  118. context, param_id,
  119. {.kind = param->kind,
  120. .type_id = get_type(param_id),
  121. .subpattern_id = new_pattern_id});
  122. }
  123. return new_pattern_id;
  124. }
  125. static auto ClonePatternBlock(Context& context, SemIR::SpecificId specific_id,
  126. SemIR::InstBlockId inst_block_id)
  127. -> SemIR::InstBlockId {
  128. if (!inst_block_id.has_value()) {
  129. return SemIR::InstBlockId::None;
  130. }
  131. return context.inst_blocks().Transform(
  132. inst_block_id, [&](SemIR::InstId inst_id) {
  133. return ClonePattern(context, specific_id, inst_id);
  134. });
  135. }
  136. static auto CloneInstId(Context& context, SemIR::SpecificId specific_id,
  137. SemIR::InstId inst_id) -> SemIR::InstId {
  138. if (!inst_id.has_value()) {
  139. return SemIR::InstId::None;
  140. }
  141. return GetOrAddInst<SemIR::SpecificConstant>(
  142. context, SemIR::LocId(inst_id),
  143. {.type_id = SemIR::TypeType::TypeId,
  144. .inst_id = inst_id,
  145. .specific_id = specific_id});
  146. }
  147. static auto CloneTypeInstId(Context& context, SemIR::SpecificId specific_id,
  148. SemIR::TypeInstId inst_id) -> SemIR::TypeInstId {
  149. if (!inst_id.has_value()) {
  150. return SemIR::TypeInstId::None;
  151. }
  152. return context.types().GetAsTypeInstId(
  153. CloneInstId(context, specific_id, inst_id));
  154. }
  155. static auto CloneFunctionDecl(Context& context, SemIR::LocId loc_id,
  156. SemIR::FunctionId signature_id,
  157. SemIR::SpecificId signature_specific_id,
  158. SemIR::FunctionId callee_id)
  159. -> std::pair<SemIR::FunctionId, SemIR::InstId> {
  160. StartGenericDecl(context);
  161. const auto& signature = context.functions().Get(signature_id);
  162. // Clone the signature.
  163. context.pattern_block_stack().Push();
  164. auto implicit_param_patterns_id = ClonePatternBlock(
  165. context, signature_specific_id, signature.implicit_param_patterns_id);
  166. auto param_patterns_id = ClonePatternBlock(context, signature_specific_id,
  167. signature.param_patterns_id);
  168. auto return_patterns_id = ClonePatternBlock(context, signature_specific_id,
  169. signature.return_patterns_id);
  170. auto return_type_inst_id = CloneTypeInstId(context, signature_specific_id,
  171. signature.return_type_inst_id);
  172. auto return_form_inst_id = CloneInstId(context, signature_specific_id,
  173. signature.return_form_inst_id);
  174. auto self_param_id = FindSelfPattern(context, implicit_param_patterns_id);
  175. auto pattern_block_id = context.pattern_block_stack().Pop();
  176. // Perform callee-side pattern matching to rebuild the parameter list.
  177. context.inst_block_stack().Push();
  178. auto match_results =
  179. CalleePatternMatch(context, implicit_param_patterns_id, param_patterns_id,
  180. return_patterns_id);
  181. auto decl_block_id = context.inst_block_stack().Pop();
  182. // Create the `FunctionDecl` instruction.
  183. auto& callee = context.functions().Get(callee_id);
  184. auto [decl_id, function_id] = MakeFunctionDecl(
  185. context, loc_id, decl_block_id, /*build_generic=*/true,
  186. /*is_definition=*/true,
  187. SemIR::Function{
  188. {
  189. .name_id = signature.name_id,
  190. .parent_scope_id = callee.parent_scope_id,
  191. // Set by `MakeFunctionDecl`.
  192. .generic_id = SemIR::GenericId::None,
  193. .first_param_node_id = signature.first_param_node_id,
  194. .last_param_node_id = signature.last_param_node_id,
  195. .pattern_block_id = pattern_block_id,
  196. .implicit_param_patterns_id = implicit_param_patterns_id,
  197. .param_patterns_id = param_patterns_id,
  198. .is_extern = false,
  199. .extern_library_id = SemIR::LibraryNameId::None,
  200. .non_owning_decl_id = SemIR::InstId::None,
  201. // Set by `MakeFunctionDecl`.
  202. .first_owning_decl_id = SemIR::InstId::None,
  203. },
  204. {
  205. .call_param_patterns_id = match_results.call_param_patterns_id,
  206. .call_params_id = match_results.call_params_id,
  207. .call_param_ranges = match_results.param_ranges,
  208. .return_type_inst_id = return_type_inst_id,
  209. .return_form_inst_id = return_form_inst_id,
  210. .return_patterns_id = return_patterns_id,
  211. .virtual_modifier = callee.virtual_modifier,
  212. .virtual_index = callee.virtual_index,
  213. .evaluation_mode = signature.evaluation_mode,
  214. .self_param_id = self_param_id,
  215. }});
  216. context.inst_block_stack().AddInstId(decl_id);
  217. return {function_id, decl_id};
  218. }
  219. static auto HasDeclaredReturnType(Context& context,
  220. SemIR::FunctionId function_id) -> bool {
  221. return context.functions().Get(function_id).return_type_inst_id.has_value();
  222. }
  223. auto PerformThunkCall(Context& context, SemIR::LocId loc_id,
  224. SemIR::FunctionId function_id,
  225. llvm::ArrayRef<SemIR::InstId> call_arg_ids,
  226. SemIR::InstId callee_id) -> SemIR::InstId {
  227. auto& function = context.functions().Get(function_id);
  228. auto param_pattern_ids =
  229. context.inst_blocks().Get(function.call_param_patterns_id);
  230. // Maps each `Call` parameter pattern ID to its index.
  231. // TODO: is it possible to arrange for the param patterns to be created in
  232. // order, so that we could use `param_pattern_ids` for this directly?
  233. struct InstWithIndex {
  234. SemIR::InstId inst_id;
  235. int index;
  236. auto operator<(InstWithIndex other) const -> bool {
  237. return inst_id.index < other.inst_id.index;
  238. }
  239. };
  240. llvm::SmallVector<InstWithIndex> param_to_index;
  241. param_to_index.reserve(param_pattern_ids.size());
  242. for (auto [index, inst_id] : llvm::enumerate(param_pattern_ids)) {
  243. param_to_index.push_back({inst_id, static_cast<int>(index)});
  244. }
  245. llvm::sort(param_to_index);
  246. // Given that `call_arg_ids` is a list of the _`Call`_ arguments for a call to
  247. // `function_id`, this returns the _syntactic_ argument that was passed for
  248. // param_pattern_id in that call.
  249. auto build_syntactic_arg = [&](SemIR::InstId param_pattern_id) {
  250. // NOLINTNEXTLINE(readability-qualified-auto)
  251. auto result =
  252. llvm::lower_bound(param_to_index, InstWithIndex{param_pattern_id, -1});
  253. if (result < param_to_index.end() && result->inst_id == param_pattern_id) {
  254. return call_arg_ids[result->index];
  255. } else {
  256. if (param_pattern_id != SemIR::ErrorInst::InstId) {
  257. context.TODO(param_pattern_id,
  258. "don't know how to reconstruct the syntactic argument for "
  259. "this pattern in thunk");
  260. }
  261. return SemIR::ErrorInst::InstId;
  262. }
  263. };
  264. llvm::SmallVector<SemIR::InstId> args;
  265. // If we have a self parameter, form `self.<callee_id>`.
  266. if (function.self_param_id.has_value()) {
  267. auto self_arg_id = build_syntactic_arg(function.self_param_id);
  268. if (IsCppConstructorOrNonMethodOperator(context, callee_id)) {
  269. // When calling a C++ constructor to implement `Copy`, or calling a C++
  270. // non-method operator to implement a Carbon operator, the interface has a
  271. // `self` parameter but C++ models that parameter as an explicit argument
  272. // instead, so add the `self` to the argument list instead in that case.
  273. args.push_back(self_arg_id);
  274. } else {
  275. callee_id =
  276. PerformCompoundMemberAccess(context, loc_id, self_arg_id, callee_id);
  277. }
  278. }
  279. // Form an argument list.
  280. for (auto pattern_id :
  281. context.inst_blocks().Get(function.param_patterns_id)) {
  282. args.push_back(build_syntactic_arg(pattern_id));
  283. }
  284. return PerformCall(context, loc_id, callee_id, args);
  285. }
  286. // Build a call to a function that forwards the arguments of the enclosing
  287. // function, for use when constructing a thunk.
  288. static auto BuildThunkCall(Context& context, SemIR::FunctionId function_id,
  289. SemIR::InstId callee_id) -> SemIR::InstId {
  290. auto& function = context.functions().Get(function_id);
  291. // Build a `NameRef` naming the callee, and a `SpecificConstant` if needed.
  292. auto loc_id = SemIR::LocId(callee_id);
  293. auto callee_type = context.types().GetAs<SemIR::FunctionType>(
  294. context.insts().Get(callee_id).type_id());
  295. callee_id = BuildNameRef(context, loc_id, function.name_id, callee_id,
  296. callee_type.specific_id);
  297. // Build a reference to each parameter for use as call arguments.
  298. llvm::SmallVector<SemIR::InstId> call_args;
  299. auto call_params = context.inst_blocks().Get(function.call_params_id);
  300. call_args.reserve(call_params.size());
  301. for (auto call_param_id : call_params) {
  302. // Use a pretty name for the `name_ref`. While it's suspicious to use a
  303. // pretty name in the IR like this, the only reason we include a name at all
  304. // here is to make the formatted SemIR more readable.
  305. auto call_param = context.insts().GetAs<SemIR::AnyParam>(call_param_id);
  306. call_args.push_back(BuildNameRef(context, SemIR::LocId(call_param_id),
  307. call_param.pretty_name_id, call_param_id,
  308. SemIR::SpecificId::None));
  309. }
  310. return PerformThunkCall(context, loc_id, function_id, call_args, callee_id);
  311. }
  312. // Given a declaration of a thunk and the function that it should call, build
  313. // the thunk body.
  314. static auto BuildThunkDefinition(Context& context,
  315. SemIR::FunctionId signature_id,
  316. SemIR::FunctionId function_id,
  317. SemIR::InstId thunk_id,
  318. SemIR::InstId callee_id) {
  319. // TODO: Improve the diagnostics produced here. Specifically, it would likely
  320. // be better for the primary error message to be that we tried to produce a
  321. // thunk because of a type mismatch, but couldn't, with notes explaining
  322. // why, rather than the primary error message being whatever went wrong
  323. // building the thunk.
  324. {
  325. // The check below produces diagnostics referring to the signature, so also
  326. // note the callee.
  327. Diagnostics::AnnotationScope annot_scope(
  328. &context.emitter(), [&](DiagnosticBuilder& builder) {
  329. CARBON_DIAGNOSTIC(ThunkCallee, Note,
  330. "while building thunk calling this function");
  331. builder.Note(callee_id, ThunkCallee);
  332. });
  333. StartFunctionDefinition(context, thunk_id, function_id);
  334. }
  335. // The checks below produce diagnostics pointing at the callee, so also note
  336. // the signature.
  337. Diagnostics::AnnotationScope annot_scope(
  338. &context.emitter(), [&](DiagnosticBuilder& builder) {
  339. CARBON_DIAGNOSTIC(
  340. ThunkSignature, Note,
  341. "while building thunk to match the signature of this function");
  342. builder.Note(context.functions().Get(signature_id).first_owning_decl_id,
  343. ThunkSignature);
  344. });
  345. auto call_id = BuildThunkCall(context, function_id, callee_id);
  346. if (HasDeclaredReturnType(context, function_id)) {
  347. BuildReturnWithExpr(context, SemIR::LocId(callee_id), call_id);
  348. } else {
  349. DiscardExpr(context, call_id);
  350. BuildReturnWithNoExpr(context, SemIR::LocId(callee_id));
  351. }
  352. FinishFunctionDefinition(context, function_id);
  353. }
  354. auto BuildThunkDefinition(Context& context,
  355. DeferredDefinitionWorklist::DefineThunk&& task)
  356. -> void {
  357. context.scope_stack().Restore(std::move(task.scope));
  358. BuildThunkDefinition(context, task.info.signature_id, task.info.function_id,
  359. task.info.decl_id, task.info.callee_id);
  360. context.scope_stack().Pop();
  361. }
  362. auto BuildThunk(Context& context, SemIR::FunctionId signature_id,
  363. SemIR::SpecificId signature_specific_id,
  364. SemIR::InstId callee_id, bool defer_definition)
  365. -> SemIR::InstId {
  366. auto callee = SemIR::GetCalleeAsFunction(context.sem_ir(), callee_id);
  367. // Check whether we can use the given function without a thunk.
  368. // TODO: For virtual functions, we want different rules for checking `self`.
  369. // TODO: This is too strict; for example, we should not compare parameter
  370. // names here.
  371. if (CheckFunctionTypeMatches(
  372. context, context.functions().Get(callee.function_id),
  373. context.functions().Get(signature_id), signature_specific_id,
  374. /*check_syntax=*/false, /*check_self=*/true, /*diagnose=*/false)) {
  375. return callee_id;
  376. }
  377. // From P3763:
  378. // If the function in the interface does not have a return type, the
  379. // program is invalid if the function in the impl specifies a return type.
  380. //
  381. // Call into the redeclaration checking logic to produce a suitable error.
  382. //
  383. // TODO: Consider a different rule: always use an explicit return type for the
  384. // thunk, and always convert the result of the wrapped call to the return type
  385. // of the thunk.
  386. if (!HasDeclaredReturnType(context, signature_id) &&
  387. HasDeclaredReturnType(context, callee.function_id)) {
  388. bool success = CheckFunctionReturnTypeMatches(
  389. context, context.functions().Get(callee.function_id),
  390. context.functions().Get(signature_id), signature_specific_id);
  391. CARBON_CHECK(!success, "Return type unexpectedly matches");
  392. return SemIR::ErrorInst::InstId;
  393. }
  394. // Create a scope for the function's parameters and generic parameters.
  395. context.scope_stack().PushForDeclName();
  396. // We can't use the function directly. Build a thunk.
  397. // TODO: Check for and diagnose obvious reasons why this will fail, such as
  398. // arity mismatch, before trying to build the thunk.
  399. auto [function_id, thunk_id] =
  400. CloneFunctionDecl(context, SemIR::LocId(callee_id), signature_id,
  401. signature_specific_id, callee.function_id);
  402. // Track that this function is a thunk.
  403. context.functions().Get(function_id).SetThunk(callee_id);
  404. if (defer_definition) {
  405. // Register the thunk to be defined when we reach the end of the enclosing
  406. // deferred definition scope, for example an `impl` or `class` definition,
  407. // as if the thunk's body were written inline in this location.
  408. context.deferred_definition_worklist().SuspendThunkAndPush(
  409. context, {
  410. .signature_id = signature_id,
  411. .function_id = function_id,
  412. .decl_id = thunk_id,
  413. .callee_id = callee_id,
  414. });
  415. } else {
  416. BuildThunkDefinition(context, signature_id, function_id, thunk_id,
  417. callee_id);
  418. context.scope_stack().Pop();
  419. }
  420. return thunk_id;
  421. }
  422. } // namespace Carbon::Check