thunk.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 "toolchain/base/kind_switch.h"
  6. #include "toolchain/check/call.h"
  7. #include "toolchain/check/deferred_definition_scope.h"
  8. #include "toolchain/check/diagnostic_helpers.h"
  9. #include "toolchain/check/function.h"
  10. #include "toolchain/check/generic.h"
  11. #include "toolchain/check/inst.h"
  12. #include "toolchain/check/member_access.h"
  13. #include "toolchain/check/pattern.h"
  14. #include "toolchain/check/pattern_match.h"
  15. #include "toolchain/check/pointer_dereference.h"
  16. #include "toolchain/check/return.h"
  17. #include "toolchain/check/type.h"
  18. #include "toolchain/diagnostics/diagnostic.h"
  19. #include "toolchain/sem_ir/function.h"
  20. #include "toolchain/sem_ir/generic.h"
  21. #include "toolchain/sem_ir/ids.h"
  22. #include "toolchain/sem_ir/inst.h"
  23. #include "toolchain/sem_ir/pattern.h"
  24. #include "toolchain/sem_ir/typed_insts.h"
  25. namespace Carbon::Check {
  26. // Adds a pattern instruction for a thunk, copying the location from an existing
  27. // instruction.
  28. static auto RebuildPatternInst(Context& context, SemIR::InstId orig_inst_id,
  29. SemIR::Inst new_inst) -> SemIR::InstId {
  30. // Ensure we built the same kind of instruction. In particular, this ensures
  31. // that the location of the old instruction can be reused for the new one.
  32. CARBON_CHECK(context.insts().Get(orig_inst_id).kind() == new_inst.kind(),
  33. "Rebuilt pattern with the wrong kind: {0} -> {1}",
  34. context.insts().Get(orig_inst_id), new_inst);
  35. return AddPatternInst(context, SemIR::LocIdAndInst::UncheckedLoc(
  36. SemIR::LocId(orig_inst_id), new_inst));
  37. }
  38. // Wrapper to allow the type to be specified as a template argument for API
  39. // consistency with `AddInst`.
  40. template <typename InstT>
  41. static auto RebuildPatternInst(Context& context, SemIR::InstId orig_inst_id,
  42. InstT new_inst) -> SemIR::InstId {
  43. return RebuildPatternInst(context, orig_inst_id, SemIR::Inst(new_inst));
  44. }
  45. // Makes a copy of the given binding pattern, with its type adjusted to be
  46. // `new_pattern_type_id`.
  47. static auto CloneBindingPattern(Context& context, SemIR::InstId pattern_id,
  48. SemIR::AnyBindingPattern pattern,
  49. SemIR::TypeId new_pattern_type_id)
  50. -> SemIR::InstId {
  51. bool is_generic = pattern.kind == SemIR::SymbolicBindingPattern::Kind;
  52. auto entity_name = context.entity_names().Get(pattern.entity_name_id);
  53. CARBON_CHECK(is_generic == entity_name.bind_index().has_value());
  54. // Get the transformed type of the binding.
  55. if (new_pattern_type_id == SemIR::ErrorInst::TypeId) {
  56. return SemIR::ErrorInst::InstId;
  57. }
  58. auto type_inst_id = context.types()
  59. .GetAs<SemIR::PatternType>(new_pattern_type_id)
  60. .scrutinee_type_inst_id;
  61. auto type_id = context.types().GetTypeIdForTypeInstId(type_inst_id);
  62. auto type_expr_region_id = context.sem_ir().expr_regions().Add(
  63. {.block_ids = {SemIR::InstBlockId::Empty}, .result_id = type_inst_id});
  64. // Rebuild the binding pattern.
  65. return AddBindingPattern(context, SemIR::LocId(pattern_id),
  66. entity_name.name_id, type_id, type_expr_region_id,
  67. is_generic, entity_name.is_template)
  68. .pattern_id;
  69. }
  70. // Makes a copy of the given pattern instruction, substituting values from a
  71. // specific as needed. The resulting pattern behaves like a newly-created
  72. // pattern, so is suitable for running `CalleePatternMatch` against.
  73. static auto ClonePattern(Context& context, SemIR::SpecificId specific_id,
  74. SemIR::InstId pattern_id) -> SemIR::InstId {
  75. if (!pattern_id.has_value()) {
  76. return SemIR::InstId::None;
  77. }
  78. auto get_type = [&](SemIR::InstId inst_id) -> SemIR::TypeId {
  79. return SemIR::GetTypeOfInstInSpecific(context.sem_ir(), specific_id,
  80. inst_id);
  81. };
  82. auto pattern = context.insts().Get(pattern_id);
  83. // Decompose the pattern. The forms we allow for patterns in a function
  84. // parameter list are currently fairly restrictive.
  85. // Optional `addr`, only for `self`.
  86. auto [addr, addr_id] = context.insts().TryUnwrap(
  87. pattern, pattern_id, &SemIR::AddrPattern::inner_id);
  88. // Optional parameter pattern.
  89. auto [param, param_id] = context.insts().TryUnwrap(
  90. pattern, pattern_id, &SemIR::AnyParamPattern::subpattern_id);
  91. // Finally, either a binding pattern or a return slot pattern.
  92. auto new_pattern_id = SemIR::InstId::None;
  93. if (auto binding = pattern.TryAs<SemIR::AnyBindingPattern>()) {
  94. new_pattern_id = CloneBindingPattern(context, pattern_id, *binding,
  95. get_type(pattern_id));
  96. } else if (auto return_slot = pattern.TryAs<SemIR::ReturnSlotPattern>()) {
  97. new_pattern_id = RebuildPatternInst<SemIR::ReturnSlotPattern>(
  98. context, pattern_id,
  99. {.type_id = get_type(pattern_id),
  100. .type_inst_id = SemIR::TypeInstId::None});
  101. } else {
  102. CARBON_CHECK(pattern.Is<SemIR::ErrorInst>(),
  103. "Unexpected pattern {0} in function signature", pattern);
  104. return SemIR::ErrorInst::InstId;
  105. }
  106. // Rebuild parameter.
  107. if (param) {
  108. new_pattern_id = RebuildPatternInst<SemIR::AnyParamPattern>(
  109. context, param_id,
  110. {.kind = param->kind,
  111. .type_id = get_type(param_id),
  112. .subpattern_id = new_pattern_id,
  113. .index = SemIR::CallParamIndex::None});
  114. }
  115. // Rebuild `addr`.
  116. if (addr) {
  117. new_pattern_id = RebuildPatternInst<SemIR::AddrPattern>(
  118. context, addr_id,
  119. {.type_id = get_type(addr_id), .inner_id = new_pattern_id});
  120. }
  121. return new_pattern_id;
  122. }
  123. static auto ClonePatternBlock(Context& context, SemIR::SpecificId specific_id,
  124. SemIR::InstBlockId inst_block_id)
  125. -> SemIR::InstBlockId {
  126. if (!inst_block_id.has_value()) {
  127. return SemIR::InstBlockId::None;
  128. }
  129. return context.inst_blocks().Transform(
  130. inst_block_id, [&](SemIR::InstId inst_id) {
  131. return ClonePattern(context, specific_id, inst_id);
  132. });
  133. }
  134. static auto CloneFunctionDecl(Context& context, SemIR::LocId loc_id,
  135. SemIR::FunctionId signature_id,
  136. SemIR::SpecificId signature_specific_id,
  137. SemIR::FunctionId callee_id)
  138. -> std::pair<SemIR::FunctionId, SemIR::InstId> {
  139. StartGenericDecl(context);
  140. // Clone the signature. Note that we re-get the function after each of these,
  141. // because they might trigger imports that invalidate the function.
  142. context.pattern_block_stack().Push();
  143. auto implicit_param_patterns_id = ClonePatternBlock(
  144. context, signature_specific_id,
  145. context.functions().Get(signature_id).implicit_param_patterns_id);
  146. auto param_patterns_id = ClonePatternBlock(
  147. context, signature_specific_id,
  148. context.functions().Get(signature_id).param_patterns_id);
  149. auto return_slot_pattern_id = ClonePattern(
  150. context, signature_specific_id,
  151. context.functions().Get(signature_id).return_slot_pattern_id);
  152. auto self_param_id = FindSelfPattern(context, implicit_param_patterns_id);
  153. auto pattern_block_id = context.pattern_block_stack().Pop();
  154. // Perform callee-side pattern matching to rebuild the parameter list.
  155. context.inst_block_stack().Push();
  156. auto call_params_id =
  157. CalleePatternMatch(context, implicit_param_patterns_id, param_patterns_id,
  158. return_slot_pattern_id);
  159. auto decl_block_id = context.inst_block_stack().Pop();
  160. // Create the `FunctionDecl` instruction.
  161. SemIR::FunctionDecl function_decl = {SemIR::TypeId::None,
  162. SemIR::FunctionId::None, decl_block_id};
  163. auto decl_id = AddPlaceholderInst(
  164. context, SemIR::LocIdAndInst::UncheckedLoc(loc_id, function_decl));
  165. auto generic_id = BuildGenericDecl(context, decl_id);
  166. // Create the `Function` object.
  167. auto& signature = context.functions().Get(signature_id);
  168. auto& callee = context.functions().Get(callee_id);
  169. function_decl.function_id = context.functions().Add(SemIR::Function{
  170. {.name_id = signature.name_id,
  171. .parent_scope_id = callee.parent_scope_id,
  172. .generic_id = generic_id,
  173. .first_param_node_id = signature.first_param_node_id,
  174. .last_param_node_id = signature.last_param_node_id,
  175. .pattern_block_id = pattern_block_id,
  176. .implicit_param_patterns_id = implicit_param_patterns_id,
  177. .param_patterns_id = param_patterns_id,
  178. .is_extern = false,
  179. .extern_library_id = SemIR::LibraryNameId::None,
  180. .non_owning_decl_id = SemIR::InstId::None,
  181. .first_owning_decl_id = decl_id,
  182. .definition_id = decl_id},
  183. {.call_params_id = call_params_id,
  184. .return_slot_pattern_id = return_slot_pattern_id,
  185. .special_function_kind = SemIR::Function::SpecialFunctionKind::Thunk,
  186. .virtual_modifier = callee.virtual_modifier,
  187. .virtual_index = callee.virtual_index,
  188. .self_param_id = self_param_id}});
  189. function_decl.type_id =
  190. GetFunctionType(context, function_decl.function_id,
  191. context.scope_stack().PeekSpecificId());
  192. ReplaceInstBeforeConstantUse(context, decl_id, function_decl);
  193. return {function_decl.function_id, decl_id};
  194. }
  195. static auto HasDeclaredReturnType(Context& context,
  196. SemIR::FunctionId function_id) -> bool {
  197. return context.functions()
  198. .Get(function_id)
  199. .return_slot_pattern_id.has_value();
  200. }
  201. auto BuildThunk(Context& context, SemIR::FunctionId signature_id,
  202. SemIR::SpecificId signature_specific_id,
  203. SemIR::InstId callee_id) -> SemIR::InstId {
  204. auto callee = SemIR::GetCalleeFunction(context.sem_ir(), callee_id);
  205. // Check whether we can use the given function without a thunk.
  206. // TODO: For virtual functions, we want different rules for checking `self`.
  207. // TODO: This is too strict; for example, we should not compare parameter
  208. // names here.
  209. if (CheckFunctionTypeMatches(
  210. context, context.functions().Get(callee.function_id),
  211. context.functions().Get(signature_id), signature_specific_id,
  212. /*check_syntax=*/false, /*check_self=*/true, /*diagnose=*/false)) {
  213. return callee_id;
  214. }
  215. // From P3763:
  216. // If the function in the interface does not have a return type, the
  217. // program is invalid if the function in the impl specifies a return type.
  218. //
  219. // Call into the redeclaration checking logic to produce a suitable error.
  220. //
  221. // TODO: Consider a different rule: always use an explicit return type for the
  222. // thunk, and always convert the result of the wrapped call to the return type
  223. // of the thunk.
  224. if (!HasDeclaredReturnType(context, signature_id) &&
  225. HasDeclaredReturnType(context, callee.function_id)) {
  226. bool success = CheckFunctionReturnTypeMatches(
  227. context, context.functions().Get(callee.function_id),
  228. context.functions().Get(signature_id), signature_specific_id);
  229. CARBON_CHECK(!success, "Return type unexpectedly matches");
  230. return SemIR::ErrorInst::InstId;
  231. }
  232. // Create a scope for the function's parameters and generic parameters.
  233. context.scope_stack().PushForDeclName();
  234. // We can't use the function directly. Build a thunk.
  235. // TODO: Check for and diagnose obvious reasons why this will fail, such as
  236. // arity mismatch, before trying to build the thunk.
  237. auto [function_id, thunk_id] =
  238. CloneFunctionDecl(context, SemIR::LocId(callee_id), signature_id,
  239. signature_specific_id, callee.function_id);
  240. // Register the thunk to be defined when we reach the end of the enclosing
  241. // deferred definition scope, for example an `impl` or `class` definition, as
  242. // if the thunk's body were written inline in this location.
  243. context.deferred_definition_scope_stack().AddPendingThunk({
  244. .signature_id = signature_id,
  245. .function_id = function_id,
  246. .decl_id = thunk_id,
  247. .callee_id = callee_id,
  248. .scope = context.scope_stack().Suspend(),
  249. });
  250. return thunk_id;
  251. }
  252. // Build an expression that names the value matched by a pattern.
  253. static auto BuildPatternRef(Context& context, SemIR::FunctionId function_id,
  254. SemIR::InstId pattern_id) -> SemIR::InstId {
  255. auto pattern = context.insts().Get(pattern_id);
  256. auto addr = context.insts()
  257. .TryUnwrap(pattern, pattern_id, &SemIR::AddrPattern::inner_id)
  258. .first;
  259. auto pattern_ref_id = SemIR::InstId::None;
  260. if (auto value_param = pattern.TryAs<SemIR::ValueParamPattern>()) {
  261. // Build a reference to this parameter.
  262. auto call_param_id = context.inst_blocks().Get(
  263. context.functions()
  264. .Get(function_id)
  265. .call_params_id)[value_param->index.index];
  266. // Use a pretty name for the `name_ref`. While it's suspicious to use a
  267. // pretty name in the IR like this, the only reason we include a name at
  268. // all here is to make the formatted SemIR more readable.
  269. pattern_ref_id = AddInst<SemIR::NameRef>(
  270. context, SemIR::LocId(pattern_id),
  271. {.type_id = context.insts().Get(call_param_id).type_id(),
  272. .name_id = SemIR::GetPrettyNameFromPatternId(
  273. context.sem_ir(), value_param->subpattern_id),
  274. .value_id = call_param_id});
  275. } else {
  276. if (pattern_id != SemIR::ErrorInst::InstId) {
  277. context.TODO(
  278. pattern_id,
  279. "don't know how to build reference to this pattern in thunk");
  280. }
  281. return SemIR::ErrorInst::InstId;
  282. }
  283. if (addr) {
  284. pattern_ref_id = PerformPointerDereference(
  285. context, SemIR::LocId(pattern_id), pattern_ref_id, [](SemIR::TypeId) {
  286. CARBON_FATAL("addr subpattern is not a pointer");
  287. });
  288. }
  289. return pattern_ref_id;
  290. }
  291. // Build a call to a function that forwards the arguments of the enclosing
  292. // function, for use when constructing a thunk.
  293. static auto BuildThunkCall(Context& context, SemIR::FunctionId function_id,
  294. SemIR::InstId callee_id) -> SemIR::InstId {
  295. auto loc_id = SemIR::LocId(callee_id);
  296. auto& function = context.functions().Get(function_id);
  297. // If we have a self parameter, form `self.<callee_id>`.
  298. if (function.self_param_id.has_value()) {
  299. callee_id = PerformCompoundMemberAccess(
  300. context, loc_id,
  301. BuildPatternRef(context, function_id, function.self_param_id),
  302. callee_id);
  303. }
  304. // Form an argument list.
  305. llvm::SmallVector<SemIR::InstId> args;
  306. for (auto pattern_id :
  307. context.inst_blocks().Get(function.param_patterns_id)) {
  308. args.push_back(BuildPatternRef(context, function_id, pattern_id));
  309. }
  310. return PerformCall(context, loc_id, callee_id, args);
  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. CheckFunctionDefinitionSignature(context, function_id);
  334. }
  335. // TODO: This duplicates much of the handling for FunctionDefinitionStart and
  336. // FunctionDefinition parse nodes. Consider refactoring.
  337. context.scope_stack().PushForFunctionBody(thunk_id);
  338. context.inst_block_stack().Push();
  339. context.region_stack().PushRegion(context.inst_block_stack().PeekOrAdd());
  340. StartGenericDefinition(context,
  341. context.functions().Get(function_id).generic_id);
  342. // The checks below produce diagnostics pointing at the callee, so also note
  343. // the signature.
  344. Diagnostics::AnnotationScope annot_scope(
  345. &context.emitter(), [&](DiagnosticBuilder& builder) {
  346. CARBON_DIAGNOSTIC(
  347. ThunkSignature, Note,
  348. "while building thunk to match the signature of this function");
  349. builder.Note(context.functions().Get(signature_id).first_owning_decl_id,
  350. ThunkSignature);
  351. });
  352. auto call_id = BuildThunkCall(context, function_id, callee_id);
  353. if (HasDeclaredReturnType(context, function_id)) {
  354. BuildReturnWithExpr(context, SemIR::LocId(callee_id), call_id);
  355. } else {
  356. BuildReturnWithNoExpr(context, SemIR::LocId(callee_id));
  357. }
  358. context.inst_block_stack().Pop();
  359. context.scope_stack().Pop();
  360. auto& function = context.functions().Get(function_id);
  361. function.body_block_ids = context.region_stack().PopRegion();
  362. FinishGenericDefinition(context, function.generic_id);
  363. }
  364. auto BuildThunkDefinition(Context& context, PendingThunk&& thunk) -> void {
  365. context.scope_stack().Restore(std::move(thunk.scope));
  366. BuildThunkDefinition(context, thunk.signature_id, thunk.function_id,
  367. thunk.decl_id, thunk.callee_id);
  368. context.scope_stack().Pop();
  369. }
  370. } // namespace Carbon::Check