pattern_match.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  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/pattern_match.h"
  5. #include <functional>
  6. #include <utility>
  7. #include <vector>
  8. #include "llvm/ADT/STLExtras.h"
  9. #include "llvm/ADT/SmallVector.h"
  10. #include "toolchain/base/kind_switch.h"
  11. #include "toolchain/check/context.h"
  12. #include "toolchain/check/control_flow.h"
  13. #include "toolchain/check/convert.h"
  14. #include "toolchain/check/pattern.h"
  15. #include "toolchain/check/type.h"
  16. #include "toolchain/diagnostics/format_providers.h"
  17. #include "toolchain/sem_ir/expr_info.h"
  18. #include "toolchain/sem_ir/pattern.h"
  19. namespace Carbon::Check {
  20. namespace {
  21. // Selects between the different kinds of pattern matching.
  22. enum class MatchKind : uint8_t {
  23. // Caller pattern matching occurs on the caller side of a function call, and
  24. // is responsible for matching the argument expression against the portion
  25. // of the pattern above the ParamPattern insts.
  26. Caller,
  27. // Callee pattern matching occurs in the function decl block, and is
  28. // responsible for matching the function's calling-convention parameters
  29. // against the portion of the pattern below the ParamPattern insts.
  30. Callee,
  31. // Local pattern matching is pattern matching outside of a function call,
  32. // such as in a let/var declaration.
  33. Local,
  34. };
  35. // The collected state of a pattern-matching operation.
  36. //
  37. // Conceptually, pattern matching is a recursive traversal of the pattern inst
  38. // tree: we match a pattern inst to a scrutinee inst by converting the scrutinee
  39. // as needed, matching any subpatterns against corresponding parts of the
  40. // scrutinee, and assembling the results of those sub-matches to form the result
  41. // of the whole match.
  42. //
  43. // This recursive traversal is implemented as a stack of work items, each
  44. // associated with a particular pattern inst. There are two types of work items,
  45. // PreWork and PostWork, which correspond to the work that is done before and
  46. // after visiting an inst's subpatterns, and are handled by DoPreWork and
  47. // DoPostWork overloads, respectively. Note that when there are no subpatterns,
  48. // DoPreWork may push a PostWork onto the stack, or may do the post-work (if
  49. // any) locally.
  50. //
  51. // DoPostWork is primarily responsible for computing the pattern's result and
  52. // adding it to result_stack_. However, the result of matching a pattern is
  53. // often not needed, so to avoid emitting unnecessary SemIR, it should only do
  54. // that if need_subpattern_results() is true.
  55. //
  56. // The traversal behavior depends on the kind of matching being performed. In
  57. // particular, many parts of a function signature pattern are irrelevant to the
  58. // caller, or to the callee, in which case no work will be done in that part of
  59. // the traversal. If an entire subpattern is known to be irrelevant in the
  60. // current matching context, it will not be traversed at all.
  61. class MatchContext {
  62. public:
  63. struct PreWork : Printable<PreWork> {
  64. // `None` when processing the callee side.
  65. SemIR::InstId scrutinee_id;
  66. auto Print(llvm::raw_ostream& out) const -> void {
  67. out << "{PreWork, scrutinee_id: " << scrutinee_id << "}";
  68. }
  69. };
  70. struct PostWork : Printable<PostWork> {
  71. auto Print(llvm::raw_ostream& out) const -> void { out << "{PostWork}"; }
  72. };
  73. struct WorkItem : Printable<WorkItem> {
  74. SemIR::InstId pattern_id;
  75. std::variant<PreWork, PostWork> work;
  76. // If true, disables diagnostics that would otherwise require scrutinee_id
  77. // to be tagged with `ref`. Only affects caller pattern matching.
  78. bool allow_unmarked_ref = false;
  79. auto Print(llvm::raw_ostream& out) const -> void {
  80. out << "{pattern_id: " << pattern_id << ", work: ";
  81. std::visit([&](const auto& work) { out << work; }, work);
  82. out << ", allow_unmarked_ref: " << allow_unmarked_ref << "}";
  83. }
  84. };
  85. // Constructs a MatchContext. If `callee_specific_id` is not `None`, this
  86. // pattern match operation is part of implementing the signature of the given
  87. // specific.
  88. explicit MatchContext(MatchKind kind, SemIR::SpecificId callee_specific_id =
  89. SemIR::SpecificId::None)
  90. : kind_(kind), callee_specific_id_(callee_specific_id) {}
  91. // Whether the result of the work item at the top of the stack is needed.
  92. auto need_subpattern_results() const -> bool {
  93. return !results_stack_.empty();
  94. }
  95. // Adds `entry` to the front of the worklist.
  96. auto AddWork(WorkItem entry) -> void { stack_.push_back(entry); }
  97. // Sets `entry.work` to `PostWork` and adds it to the front of the worklist.
  98. auto AddAsPostWork(WorkItem entry) -> void {
  99. entry.work = PostWork{};
  100. AddWork(entry);
  101. }
  102. // Processes all work items on the stack.
  103. auto DoWork(Context& context) -> void;
  104. // Returns an inst block of references to all the emitted `Call` arguments.
  105. // Can only be called once, at the end of Caller pattern matching.
  106. auto GetCallArgs(Context& context) && -> SemIR::InstBlockId;
  107. // Returns an inst block of references to all the emitted `Call` params,
  108. // and an inst block of references to the `Call` param patterns they were
  109. // emitted to match. Can only be called once, at the end of Callee pattern
  110. // matching.
  111. struct ParamBlocks {
  112. SemIR::InstBlockId call_param_patterns_id;
  113. SemIR::InstBlockId call_params_id;
  114. };
  115. auto GetCallParams(Context& context) && -> ParamBlocks;
  116. // Returns the number of call parameters that have been emitted so far.
  117. auto param_count() -> int { return call_params_.size(); }
  118. ~MatchContext();
  119. private:
  120. // Dispatches `entry` to the appropriate DoWork method based on the kinds of
  121. // `entry.pattern_id` and `entry.work`.
  122. auto Dispatch(Context& context, WorkItem entry) -> void;
  123. // Do the pre-work for `entry`. `entry.work` must be a `PreWork` containing
  124. // `scrutinee_id`, and the pattern argument must be the value of
  125. // `entry.pattern_id` in `context`.
  126. auto DoPreWork(Context& context, SemIR::AnyBindingPattern binding_pattern,
  127. SemIR::InstId scrutinee_id, WorkItem entry) -> void;
  128. auto DoPreWork(Context& context, SemIR::AnyParamPattern param_pattern,
  129. SemIR::InstId scrutinee_id, WorkItem entry) -> void;
  130. auto DoPreWork(Context& context, SemIR::ReturnSlotPattern return_slot_pattern,
  131. SemIR::InstId scrutinee_id, WorkItem entry) -> void;
  132. auto DoPreWork(Context& context, SemIR::VarPattern var_pattern,
  133. SemIR::InstId scrutinee_id, WorkItem entry) -> void;
  134. auto DoPreWork(Context& context, SemIR::TuplePattern tuple_pattern,
  135. SemIR::InstId scrutinee_id, WorkItem entry) -> void;
  136. // Do the post-work for `entry`. `entry.work` must be a `PostWork`, and
  137. // the pattern argument must be the value of `entry.pattern_id` in `context`.
  138. auto DoPostWork(Context& context, SemIR::AnyBindingPattern binding_pattern,
  139. WorkItem entry) -> void;
  140. auto DoPostWork(Context& context, SemIR::VarPattern var_pattern,
  141. WorkItem entry) -> void;
  142. auto DoPostWork(Context& context, SemIR::AnyParamPattern param_pattern,
  143. WorkItem entry) -> void;
  144. auto DoPostWork(Context& context,
  145. SemIR::ReturnSlotPattern return_slot_pattern, WorkItem entry)
  146. -> void;
  147. auto DoPostWork(Context& context, SemIR::TuplePattern tuple_pattern,
  148. WorkItem entry) -> void;
  149. // Asserts that there is a single inst in the top array in `results_stack_`,
  150. // pops that array, and returns the inst.
  151. auto PopResult() -> SemIR::InstId {
  152. CARBON_CHECK(results_stack_.PeekArray().size() == 1);
  153. auto value_id = results_stack_.PeekArray()[0];
  154. results_stack_.PopArray();
  155. return value_id;
  156. }
  157. // Performs the core logic of matching a variable pattern whose type is
  158. // `pattern_type_id`, but returns the scrutinee that its subpattern should be
  159. // matched with, rather than pushing it onto the worklist. This is factored
  160. // out so it can be reused when handling a `FormBindingPattern` or
  161. // `FormParamPattern` with an initializing form.
  162. auto DoVarPreWorkImpl(Context& context, SemIR::TypeId pattern_type_id,
  163. SemIR::InstId scrutinee_id, WorkItem entry) const
  164. -> SemIR::InstId;
  165. // The stack of work to be processed.
  166. llvm::SmallVector<WorkItem> stack_;
  167. // The stack of in-progress match results. Each array in the stack represents
  168. // a single result, which may have multiple sub-results.
  169. ArrayStack<SemIR::InstId> results_stack_;
  170. // The in-progress contents of the `Call` arguments block. This is populated
  171. // only when kind_ is Caller.
  172. llvm::SmallVector<SemIR::InstId> call_args_;
  173. // The in-progress contents of the `Call` parameters block. This is populated
  174. // only when kind_ is Callee.
  175. llvm::SmallVector<SemIR::InstId> call_params_;
  176. // The in-progress contents of the `Call` parameter patterns block. This is
  177. // populated only when kind_ is Callee.
  178. llvm::SmallVector<SemIR::InstId> call_param_patterns_;
  179. // The kind of pattern match being performed.
  180. MatchKind kind_;
  181. // The SpecificId of the function being called (if any).
  182. SemIR::SpecificId callee_specific_id_;
  183. };
  184. } // namespace
  185. auto MatchContext::DoWork(Context& context) -> void {
  186. while (!stack_.empty()) {
  187. Dispatch(context, stack_.pop_back_val());
  188. }
  189. }
  190. auto MatchContext::GetCallArgs(Context& context) && -> SemIR::InstBlockId {
  191. CARBON_CHECK(kind_ == MatchKind::Caller);
  192. auto block_id = context.inst_blocks().Add(call_args_);
  193. call_args_.clear();
  194. return block_id;
  195. }
  196. auto MatchContext::GetCallParams(Context& context) && -> ParamBlocks {
  197. CARBON_CHECK(kind_ == MatchKind::Callee);
  198. CARBON_CHECK(call_params_.size() == call_param_patterns_.size());
  199. auto call_param_patterns_id = context.inst_blocks().Add(call_param_patterns_);
  200. call_param_patterns_.clear();
  201. auto call_params_id = context.inst_blocks().Add(call_params_);
  202. call_params_.clear();
  203. return {.call_param_patterns_id = call_param_patterns_id,
  204. .call_params_id = call_params_id};
  205. }
  206. MatchContext::~MatchContext() {
  207. CARBON_CHECK(call_args_.empty() && call_params_.empty() &&
  208. call_param_patterns_.empty(),
  209. "Unhandled pattern matching outputs. call_args_.size(): {0}, "
  210. "call_params_.size(): {1}, call_param_patterns_.size(): {2}",
  211. call_args_.size(), call_params_.size(),
  212. call_param_patterns_.size());
  213. }
  214. // Inserts the given region into the current code block. If the region
  215. // consists of a single block, this will be implemented as a `splice_block`
  216. // inst. Otherwise, this will end the current block with a branch to the entry
  217. // block of the region, and add future insts to a new block which is the
  218. // immediate successor of the region's exit block. As a result, this cannot be
  219. // called more than once for the same region.
  220. static auto InsertHere(Context& context, SemIR::ExprRegionId region_id)
  221. -> SemIR::InstId {
  222. auto region = context.sem_ir().expr_regions().Get(region_id);
  223. auto exit_block = context.inst_blocks().Get(region.block_ids.back());
  224. if (region.block_ids.size() == 1) {
  225. // TODO: Is it possible to avoid leaving an "orphan" block in the IR in the
  226. // first two cases?
  227. if (exit_block.empty()) {
  228. return region.result_id;
  229. }
  230. if (exit_block.size() == 1) {
  231. context.inst_block_stack().AddInstId(exit_block.front());
  232. return region.result_id;
  233. }
  234. return AddInst<SemIR::SpliceBlock>(
  235. context, SemIR::LocId(region.result_id),
  236. {.type_id = context.insts().Get(region.result_id).type_id(),
  237. .block_id = region.block_ids.front(),
  238. .result_id = region.result_id});
  239. }
  240. if (context.region_stack().empty()) {
  241. context.TODO(region.result_id,
  242. "Control flow expressions are currently only supported inside "
  243. "functions.");
  244. return SemIR::ErrorInst::InstId;
  245. }
  246. AddInst(context, SemIR::LocIdAndInst::NoLoc<SemIR::Branch>(
  247. {.target_id = region.block_ids.front()}));
  248. context.inst_block_stack().Pop();
  249. // TODO: this will cumulatively cost O(MN) running time for M blocks
  250. // at the Nth level of the stack. Figure out how to do better.
  251. context.region_stack().AddToRegion(region.block_ids);
  252. auto resume_with_block_id =
  253. context.insts().GetAs<SemIR::Branch>(exit_block.back()).target_id;
  254. CARBON_CHECK(context.inst_blocks().GetOrEmpty(resume_with_block_id).empty());
  255. context.inst_block_stack().Push(resume_with_block_id);
  256. context.region_stack().AddToRegion(resume_with_block_id,
  257. SemIR::LocId(region.result_id));
  258. return region.result_id;
  259. }
  260. // Returns the kind of conversion to perform on the scrutinee when matching the
  261. // given pattern. Note that this returns `NoOp` for `var` patterns, because
  262. // their conversion needs special handling, prior to any general-purpose
  263. // conversion that would use this function.
  264. static auto ConversionKindFor(Context& context, SemIR::Inst pattern,
  265. MatchContext::WorkItem entry)
  266. -> ConversionTarget::Kind {
  267. CARBON_KIND_SWITCH(pattern) {
  268. case SemIR::VarParamPattern::Kind:
  269. case SemIR::VarPattern::Kind:
  270. // See function comment.
  271. case SemIR::OutParamPattern::Kind:
  272. // OutParamPattern conversion is handled by the enclosing
  273. // ReturnSlotPattern.
  274. case SemIR::WrapperBindingPattern::Kind:
  275. // WrapperBindingPattern conversion is handled by its subpattern.
  276. return ConversionTarget::NoOp;
  277. case SemIR::RefBindingPattern::Kind:
  278. return ConversionTarget::DurableRef;
  279. case SemIR::RefParamPattern::Kind:
  280. return entry.allow_unmarked_ref ? ConversionTarget::UnmarkedRefParam
  281. : ConversionTarget::RefParam;
  282. case SemIR::SymbolicBindingPattern::Kind:
  283. case SemIR::ValueBindingPattern::Kind:
  284. case SemIR::ValueParamPattern::Kind:
  285. return ConversionTarget::Value;
  286. case CARBON_KIND(SemIR::FormBindingPattern form_binding_pattern): {
  287. auto form_id = context.entity_names()
  288. .Get(form_binding_pattern.entity_name_id)
  289. .form_id;
  290. auto form_inst_id = context.constant_values().GetInstId(form_id);
  291. auto form_inst = context.insts().Get(form_inst_id);
  292. switch (form_inst.kind()) {
  293. case SemIR::InitForm::Kind:
  294. context.TODO(entry.pattern_id, "Support local initializing forms");
  295. [[fallthrough]];
  296. case SemIR::RefForm::Kind:
  297. return ConversionTarget::DurableRef;
  298. case SemIR::SymbolicBinding::Kind:
  299. context.TODO(entry.pattern_id, "Support symbolic form bindings");
  300. [[fallthrough]];
  301. case SemIR::ValueForm::Kind:
  302. case SemIR::ErrorInst::Kind:
  303. return ConversionTarget::Value;
  304. default:
  305. CARBON_FATAL("Unexpected form {0}", form_inst);
  306. }
  307. }
  308. case CARBON_KIND(SemIR::FormParamPattern form_param_pattern): {
  309. auto form_inst_id =
  310. context.constant_values().GetInstId(form_param_pattern.form_id);
  311. auto form_inst = context.insts().Get(form_inst_id);
  312. switch (form_inst.kind()) {
  313. case SemIR::InitForm::Kind:
  314. return ConversionTarget::NoOp;
  315. case SemIR::RefForm::Kind:
  316. // TODO: Figure out rules for when the argument must have a `ref` tag.
  317. return entry.allow_unmarked_ref ? ConversionTarget::UnmarkedRefParam
  318. : ConversionTarget::RefParam;
  319. case SemIR::SymbolicBinding::Kind:
  320. context.TODO(entry.pattern_id, "Support symbolic form params");
  321. [[fallthrough]];
  322. case SemIR::ErrorInst::Kind:
  323. case SemIR::ValueForm::Kind:
  324. return ConversionTarget::Value;
  325. default:
  326. CARBON_FATAL("Unexpected form {0}", form_inst);
  327. }
  328. }
  329. default:
  330. CARBON_FATAL("Unexpected pattern kind in {0}", pattern);
  331. }
  332. }
  333. auto MatchContext::DoPreWork(Context& /*context*/,
  334. SemIR::AnyBindingPattern binding_pattern,
  335. SemIR::InstId scrutinee_id,
  336. MatchContext::WorkItem entry) -> void {
  337. bool scheduled_post_work = false;
  338. if (kind_ != MatchKind::Caller) {
  339. results_stack_.PushArray();
  340. AddAsPostWork(entry);
  341. scheduled_post_work = true;
  342. } else {
  343. CARBON_CHECK(!need_subpattern_results());
  344. }
  345. if (binding_pattern.kind == SemIR::WrapperBindingPattern::Kind) {
  346. AddWork({.pattern_id = binding_pattern.subpattern_id,
  347. .work = PreWork{.scrutinee_id = scrutinee_id},
  348. .allow_unmarked_ref = entry.allow_unmarked_ref});
  349. } else if (scheduled_post_work) {
  350. // PostWork expects a result to bind the name to. If we scheduled PostWork,
  351. // but didn't schedule PreWork for a subpattern, the name should be bound to
  352. // the scrutinee.
  353. results_stack_.AppendToTop(scrutinee_id);
  354. }
  355. }
  356. auto MatchContext::DoPostWork(Context& context,
  357. SemIR::AnyBindingPattern binding_pattern,
  358. MatchContext::WorkItem entry) -> void {
  359. // We're logically consuming this map entry, so we invalidate it in order
  360. // to avoid accidentally consuming it twice.
  361. auto [bind_name_id, type_expr_region_id] =
  362. std::exchange(context.bind_name_map().Lookup(entry.pattern_id).value(),
  363. {.bind_name_id = SemIR::InstId::None,
  364. .type_expr_region_id = SemIR::ExprRegionId::None});
  365. if (type_expr_region_id.has_value()) {
  366. InsertHere(context, type_expr_region_id);
  367. }
  368. auto value_id = PopResult();
  369. if (value_id.has_value()) {
  370. auto conversion_kind = ConversionKindFor(context, binding_pattern, entry);
  371. if (!bind_name_id.has_value()) {
  372. // TODO: Is this appropriate, or should we perform a conversion based on
  373. // the category of the `_` binding first, and then separately discard the
  374. // initializer for a `_` binding?
  375. conversion_kind = ConversionTarget::Discarded;
  376. }
  377. value_id =
  378. Convert(context, SemIR::LocId(value_id), value_id,
  379. {.kind = conversion_kind,
  380. .type_id = context.insts().Get(bind_name_id).type_id()});
  381. } else {
  382. CARBON_CHECK(binding_pattern.kind == SemIR::SymbolicBindingPattern::Kind);
  383. }
  384. if (bind_name_id.has_value()) {
  385. auto bind_name = context.insts().GetAs<SemIR::AnyBinding>(bind_name_id);
  386. CARBON_CHECK(!bind_name.value_id.has_value());
  387. bind_name.value_id = value_id;
  388. ReplaceInstBeforeConstantUse(context, bind_name_id, bind_name);
  389. context.inst_block_stack().AddInstId(bind_name_id);
  390. }
  391. if (need_subpattern_results()) {
  392. results_stack_.AppendToTop(value_id);
  393. }
  394. }
  395. // Returns the inst kind to use for the parameter corresponding to the given
  396. // parameter pattern.
  397. static auto ParamKindFor(Context& context, SemIR::Inst param_pattern,
  398. MatchContext::WorkItem entry) -> SemIR::InstKind {
  399. CARBON_KIND_SWITCH(param_pattern) {
  400. case SemIR::OutParamPattern::Kind:
  401. return SemIR::OutParam::Kind;
  402. case SemIR::RefParamPattern::Kind:
  403. case SemIR::VarParamPattern::Kind:
  404. return SemIR::RefParam::Kind;
  405. case SemIR::ValueParamPattern::Kind:
  406. return SemIR::ValueParam::Kind;
  407. case CARBON_KIND(SemIR::FormParamPattern form_param_pattern): {
  408. auto form_inst_id =
  409. context.constant_values().GetInstId(form_param_pattern.form_id);
  410. auto form_inst = context.insts().Get(form_inst_id);
  411. switch (form_inst.kind()) {
  412. case SemIR::InitForm::Kind:
  413. case SemIR::RefForm::Kind:
  414. return SemIR::RefParam::Kind;
  415. case SemIR::SymbolicBinding::Kind:
  416. context.TODO(entry.pattern_id, "Support symbolic form params");
  417. [[fallthrough]];
  418. case SemIR::ErrorInst::Kind:
  419. case SemIR::ValueForm::Kind:
  420. return SemIR::ValueParam::Kind;
  421. default:
  422. CARBON_FATAL("Unexpected form {0}", form_inst);
  423. }
  424. }
  425. default:
  426. CARBON_FATAL("Unexpected param pattern kind: {0}", param_pattern);
  427. }
  428. }
  429. auto MatchContext::DoPreWork(Context& context,
  430. SemIR::AnyParamPattern param_pattern,
  431. SemIR::InstId scrutinee_id, WorkItem entry)
  432. -> void {
  433. AddAsPostWork(entry);
  434. // If `param_pattern` has initializing form, match it as a `VarPattern`
  435. // before matching it as a parameter pattern.
  436. switch (param_pattern.kind) {
  437. case SemIR::FormParamPattern::Kind: {
  438. auto form_param_pattern =
  439. context.insts().GetAs<SemIR::FormParamPattern>(entry.pattern_id);
  440. auto form_inst_id =
  441. context.constant_values().GetInstId(form_param_pattern.form_id);
  442. if (!context.insts().Is<SemIR::InitForm>(form_inst_id)) {
  443. break;
  444. }
  445. [[fallthrough]];
  446. }
  447. case SemIR::VarParamPattern::Kind: {
  448. scrutinee_id =
  449. DoVarPreWorkImpl(context, param_pattern.type_id, scrutinee_id, entry);
  450. entry.allow_unmarked_ref = true;
  451. break;
  452. }
  453. default:
  454. break;
  455. }
  456. switch (kind_) {
  457. case MatchKind::Caller: {
  458. CARBON_CHECK(scrutinee_id.has_value());
  459. if (scrutinee_id == SemIR::ErrorInst::InstId) {
  460. call_args_.push_back(SemIR::ErrorInst::InstId);
  461. } else {
  462. auto scrutinee_type_id = ExtractScrutineeType(
  463. context.sem_ir(),
  464. SemIR::GetTypeOfInstInSpecific(
  465. context.sem_ir(), callee_specific_id_, entry.pattern_id));
  466. call_args_.push_back(
  467. Convert(context, SemIR::LocId(scrutinee_id), scrutinee_id,
  468. {.kind = ConversionKindFor(context, param_pattern, entry),
  469. .type_id = scrutinee_type_id}));
  470. }
  471. // Do not traverse farther or schedule PostWork, because the caller side
  472. // of the pattern ends here.
  473. break;
  474. }
  475. case MatchKind::Callee: {
  476. SemIR::Inst param =
  477. SemIR::AnyParam{.kind = ParamKindFor(context, param_pattern, entry),
  478. .type_id = ExtractScrutineeType(
  479. context.sem_ir(), param_pattern.type_id),
  480. .index = SemIR::CallParamIndex(call_params_.size()),
  481. .pretty_name_id = SemIR::GetPrettyNameFromPatternId(
  482. context.sem_ir(), entry.pattern_id)};
  483. auto loc_id = SemIR::LocId(entry.pattern_id);
  484. auto param_id = SemIR::InstId::None;
  485. // TODO: find a way to avoid this boilerplate.
  486. switch (param.kind()) {
  487. case SemIR::OutParam::Kind:
  488. param_id = AddInst(context, loc_id, param.As<SemIR::OutParam>());
  489. break;
  490. case SemIR::RefParam::Kind:
  491. param_id = AddInst(context, loc_id, param.As<SemIR::RefParam>());
  492. break;
  493. case SemIR::ValueParam::Kind:
  494. param_id = AddInst(context, loc_id, param.As<SemIR::ValueParam>());
  495. break;
  496. default:
  497. CARBON_FATAL("Unexpected parameter kind");
  498. }
  499. if (auto var_param_pattern =
  500. context.insts().TryGetAs<SemIR::VarParamPattern>(
  501. entry.pattern_id)) {
  502. AddWork({.pattern_id = var_param_pattern->subpattern_id,
  503. .work = PreWork{.scrutinee_id = param_id},
  504. .allow_unmarked_ref = entry.allow_unmarked_ref});
  505. } else {
  506. results_stack_.AppendToTop(param_id);
  507. }
  508. call_params_.push_back(param_id);
  509. call_param_patterns_.push_back(entry.pattern_id);
  510. break;
  511. }
  512. case MatchKind::Local: {
  513. CARBON_FATAL("Found ValueParamPattern during local pattern match");
  514. }
  515. }
  516. }
  517. auto MatchContext::DoPostWork(Context& /*context*/,
  518. SemIR::AnyParamPattern /*param_pattern*/,
  519. WorkItem /*entry*/) -> void {
  520. // No-op: the subpattern's result is this pattern's result. Note that if
  521. // there were any post-work corresponding to DoVarPreWorkImpl, that work
  522. // would have to be done here.
  523. }
  524. auto MatchContext::DoPreWork(Context& /*context*/,
  525. SemIR::ReturnSlotPattern return_slot_pattern,
  526. SemIR::InstId scrutinee_id, WorkItem entry)
  527. -> void {
  528. if (kind_ == MatchKind::Callee) {
  529. CARBON_CHECK(!scrutinee_id.has_value());
  530. results_stack_.PushArray();
  531. AddAsPostWork(entry);
  532. }
  533. AddWork({.pattern_id = return_slot_pattern.subpattern_id,
  534. .work = PreWork{.scrutinee_id = scrutinee_id}});
  535. }
  536. auto MatchContext::DoPostWork(Context& context,
  537. SemIR::ReturnSlotPattern return_slot_pattern,
  538. WorkItem entry) -> void {
  539. CARBON_CHECK(kind_ == MatchKind::Callee);
  540. auto type_id =
  541. ExtractScrutineeType(context.sem_ir(), return_slot_pattern.type_id);
  542. auto return_slot_id = AddInst<SemIR::ReturnSlot>(
  543. context, SemIR::LocId(entry.pattern_id),
  544. {.type_id = type_id,
  545. .type_inst_id = context.types().GetTypeInstId(type_id),
  546. .storage_id = PopResult()});
  547. bool already_in_lookup =
  548. context.scope_stack()
  549. .LookupOrAddName(SemIR::NameId::ReturnSlot, return_slot_id)
  550. .has_value();
  551. CARBON_CHECK(!already_in_lookup);
  552. if (need_subpattern_results()) {
  553. results_stack_.AppendToTop(return_slot_id);
  554. }
  555. }
  556. auto MatchContext::DoPreWork(Context& context, SemIR::VarPattern var_pattern,
  557. SemIR::InstId scrutinee_id, WorkItem entry)
  558. -> void {
  559. auto new_scrutinee_id =
  560. DoVarPreWorkImpl(context, var_pattern.type_id, scrutinee_id, entry);
  561. if (need_subpattern_results()) {
  562. AddAsPostWork(entry);
  563. }
  564. AddWork({.pattern_id = var_pattern.subpattern_id,
  565. .work = PreWork{.scrutinee_id = new_scrutinee_id},
  566. .allow_unmarked_ref = true});
  567. }
  568. auto MatchContext::DoVarPreWorkImpl(Context& context,
  569. SemIR::TypeId pattern_type_id,
  570. SemIR::InstId scrutinee_id,
  571. WorkItem entry) const -> SemIR::InstId {
  572. auto storage_id = SemIR::InstId::None;
  573. switch (kind_) {
  574. case MatchKind::Callee: {
  575. // We're emitting pattern-match IR for the callee, but we're still on
  576. // the caller side of the pattern, so we traverse without emitting any
  577. // insts.
  578. return scrutinee_id;
  579. }
  580. case MatchKind::Local: {
  581. // In a `var`/`let` declaration, the `VarStorage` inst is created before
  582. // we start pattern matching.
  583. auto lookup_result = context.var_storage_map().Lookup(entry.pattern_id);
  584. CARBON_CHECK(lookup_result);
  585. storage_id = lookup_result.value();
  586. break;
  587. }
  588. case MatchKind::Caller: {
  589. storage_id = AddInst<SemIR::TemporaryStorage>(
  590. context, SemIR::LocId(entry.pattern_id),
  591. {.type_id = ExtractScrutineeType(context.sem_ir(), pattern_type_id)});
  592. CARBON_CHECK(scrutinee_id.has_value());
  593. break;
  594. }
  595. }
  596. // TODO: Find a more efficient way to put these insts in the global_init
  597. // block (or drop the distinction between the global_init block and the
  598. // file scope?)
  599. if (context.scope_stack().PeekIndex() == ScopeIndex::Package) {
  600. context.global_init().Resume();
  601. }
  602. if (scrutinee_id.has_value()) {
  603. auto init_id = Initialize(context, SemIR::LocId(entry.pattern_id),
  604. storage_id, scrutinee_id);
  605. // If we created a `TemporaryStorage` to hold the var, create a
  606. // corresponding `Temporary` to model that its initialization is complete.
  607. // TODO: If the subpattern is a binding, we may want to destroy the
  608. // parameter variable in the callee instead of the caller so that we can
  609. // support destructive move from it.
  610. if (kind_ == MatchKind::Caller) {
  611. storage_id = AddInstWithCleanup<SemIR::Temporary>(
  612. context, SemIR::LocId(entry.pattern_id),
  613. {.type_id = context.insts().Get(storage_id).type_id(),
  614. .storage_id = storage_id,
  615. .init_id = init_id});
  616. } else {
  617. // TODO: Consider using different instruction kinds for assignment
  618. // versus initialization.
  619. AddInst<SemIR::Assign>(context, SemIR::LocId(entry.pattern_id),
  620. {.lhs_id = storage_id, .rhs_id = init_id});
  621. }
  622. }
  623. if (context.scope_stack().PeekIndex() == ScopeIndex::Package) {
  624. context.global_init().Suspend();
  625. }
  626. return storage_id;
  627. }
  628. auto MatchContext::DoPostWork(Context& /*context*/,
  629. SemIR::VarPattern /*var_pattern*/,
  630. WorkItem /*entry*/) -> void {
  631. // No-op: the subpattern's result is this pattern's result.
  632. }
  633. auto MatchContext::DoPreWork(Context& context,
  634. SemIR::TuplePattern tuple_pattern,
  635. SemIR::InstId scrutinee_id, WorkItem entry)
  636. -> void {
  637. if (tuple_pattern.type_id == SemIR::ErrorInst::TypeId) {
  638. return;
  639. }
  640. auto subpattern_ids = context.inst_blocks().Get(tuple_pattern.elements_id);
  641. if (need_subpattern_results()) {
  642. results_stack_.PushArray();
  643. AddAsPostWork(entry);
  644. }
  645. auto add_all_subscrutinees =
  646. [&](llvm::ArrayRef<SemIR::InstId> subscrutinee_ids) {
  647. for (auto [subpattern_id, subscrutinee_id] :
  648. llvm::reverse(llvm::zip_equal(subpattern_ids, subscrutinee_ids))) {
  649. AddWork({.pattern_id = subpattern_id,
  650. .work = PreWork{.scrutinee_id = subscrutinee_id}});
  651. }
  652. };
  653. if (!scrutinee_id.has_value()) {
  654. CARBON_CHECK(kind_ == MatchKind::Callee);
  655. // If we don't have a scrutinee yet, we're still on the caller side of the
  656. // pattern, so the subpatterns don't have a scrutinee either.
  657. for (auto subpattern_id : llvm::reverse(subpattern_ids)) {
  658. AddWork({.pattern_id = subpattern_id,
  659. .work = PreWork{.scrutinee_id = SemIR::InstId::None}});
  660. }
  661. return;
  662. }
  663. auto scrutinee = context.insts().GetWithLocId(scrutinee_id);
  664. if (auto scrutinee_literal = scrutinee.inst.TryAs<SemIR::TupleLiteral>()) {
  665. auto subscrutinee_ids =
  666. context.inst_blocks().Get(scrutinee_literal->elements_id);
  667. if (subscrutinee_ids.size() != subpattern_ids.size()) {
  668. CARBON_DIAGNOSTIC(TuplePatternSizeDoesntMatchLiteral, Error,
  669. "tuple pattern expects {0} element{0:s}, but tuple "
  670. "literal has {1}",
  671. Diagnostics::IntAsSelect, Diagnostics::IntAsSelect);
  672. context.emitter().Emit(entry.pattern_id,
  673. TuplePatternSizeDoesntMatchLiteral,
  674. subpattern_ids.size(), subscrutinee_ids.size());
  675. return;
  676. }
  677. add_all_subscrutinees(subscrutinee_ids);
  678. return;
  679. }
  680. auto tuple_type_id =
  681. ExtractScrutineeType(context.sem_ir(), tuple_pattern.type_id);
  682. auto converted_scrutinee_id = ConvertToValueOrRefOfType(
  683. context, SemIR::LocId(entry.pattern_id), scrutinee_id, tuple_type_id);
  684. if (auto scrutinee_value =
  685. context.insts().TryGetAs<SemIR::TupleValue>(converted_scrutinee_id)) {
  686. add_all_subscrutinees(
  687. context.inst_blocks().Get(scrutinee_value->elements_id));
  688. return;
  689. }
  690. auto tuple_type = context.types().GetAs<SemIR::TupleType>(tuple_type_id);
  691. auto element_type_inst_ids =
  692. context.inst_blocks().Get(tuple_type.type_elements_id);
  693. llvm::SmallVector<SemIR::InstId> subscrutinee_ids;
  694. subscrutinee_ids.reserve(element_type_inst_ids.size());
  695. for (auto [i, element_type_id] : llvm::enumerate(
  696. context.types().GetBlockAsTypeIds(element_type_inst_ids))) {
  697. subscrutinee_ids.push_back(
  698. AddInst<SemIR::TupleAccess>(context, scrutinee.loc_id,
  699. {.type_id = element_type_id,
  700. .tuple_id = converted_scrutinee_id,
  701. .index = SemIR::ElementIndex(i)}));
  702. }
  703. add_all_subscrutinees(subscrutinee_ids);
  704. }
  705. auto MatchContext::DoPostWork(Context& context,
  706. SemIR::TuplePattern tuple_pattern, WorkItem entry)
  707. -> void {
  708. auto elements_id = context.inst_blocks().Add(results_stack_.PeekArray());
  709. results_stack_.PopArray();
  710. auto tuple_value_id =
  711. AddInst<SemIR::TupleValue>(context, SemIR::LocId(entry.pattern_id),
  712. {.type_id = SemIR::ExtractScrutineeType(
  713. context.sem_ir(), tuple_pattern.type_id),
  714. .elements_id = elements_id});
  715. results_stack_.AppendToTop(tuple_value_id);
  716. }
  717. auto MatchContext::Dispatch(Context& context, WorkItem entry) -> void {
  718. if (entry.pattern_id == SemIR::ErrorInst::InstId) {
  719. return;
  720. }
  721. Diagnostics::AnnotationScope annotate_diagnostics(
  722. &context.emitter(), [&](auto& builder) {
  723. if (kind_ == MatchKind::Caller) {
  724. CARBON_DIAGNOSTIC(InCallToFunctionParam, Note,
  725. "initializing function parameter");
  726. builder.Note(entry.pattern_id, InCallToFunctionParam);
  727. }
  728. });
  729. auto pattern = context.insts().Get(entry.pattern_id);
  730. CARBON_KIND_SWITCH(entry.work) {
  731. case CARBON_KIND(PreWork work): {
  732. // TODO: Require that `work.scrutinee_id` is valid if and only if insts
  733. // should be emitted, once we start emitting `Param` insts in the
  734. // `ParamPattern` case.
  735. CARBON_KIND_SWITCH(pattern) {
  736. case CARBON_KIND_ANY(SemIR::AnyBindingPattern, any_binding_pattern): {
  737. DoPreWork(context, any_binding_pattern, work.scrutinee_id, entry);
  738. break;
  739. }
  740. case CARBON_KIND_ANY(SemIR::AnyParamPattern, any_param_pattern): {
  741. DoPreWork(context, any_param_pattern, work.scrutinee_id, entry);
  742. break;
  743. }
  744. case CARBON_KIND(SemIR::ReturnSlotPattern return_slot_pattern): {
  745. DoPreWork(context, return_slot_pattern, work.scrutinee_id, entry);
  746. break;
  747. }
  748. case CARBON_KIND(SemIR::VarPattern var_pattern): {
  749. DoPreWork(context, var_pattern, work.scrutinee_id, entry);
  750. break;
  751. }
  752. case CARBON_KIND(SemIR::TuplePattern tuple_pattern): {
  753. DoPreWork(context, tuple_pattern, work.scrutinee_id, entry);
  754. break;
  755. }
  756. default: {
  757. CARBON_FATAL("Inst kind not handled: {0}", pattern.kind());
  758. }
  759. }
  760. break;
  761. }
  762. case CARBON_KIND(PostWork _): {
  763. CARBON_KIND_SWITCH(pattern) {
  764. case CARBON_KIND_ANY(SemIR::AnyBindingPattern, any_binding_pattern): {
  765. DoPostWork(context, any_binding_pattern, entry);
  766. break;
  767. }
  768. case CARBON_KIND_ANY(SemIR::AnyParamPattern, any_param_pattern): {
  769. DoPostWork(context, any_param_pattern, entry);
  770. break;
  771. }
  772. case CARBON_KIND(SemIR::ReturnSlotPattern return_slot_pattern): {
  773. DoPostWork(context, return_slot_pattern, entry);
  774. break;
  775. }
  776. case CARBON_KIND(SemIR::VarPattern var_pattern): {
  777. DoPostWork(context, var_pattern, entry);
  778. break;
  779. }
  780. case CARBON_KIND(SemIR::TuplePattern tuple_pattern): {
  781. DoPostWork(context, tuple_pattern, entry);
  782. break;
  783. }
  784. default: {
  785. CARBON_FATAL("Inst kind not handled: {0}", pattern.kind());
  786. }
  787. }
  788. break;
  789. }
  790. }
  791. }
  792. auto CalleePatternMatch(Context& context,
  793. SemIR::InstBlockId implicit_param_patterns_id,
  794. SemIR::InstBlockId param_patterns_id,
  795. SemIR::InstBlockId return_patterns_id)
  796. -> CalleePatternMatchResults {
  797. if (!return_patterns_id.has_value() && !param_patterns_id.has_value() &&
  798. !implicit_param_patterns_id.has_value()) {
  799. return {.call_param_patterns_id = SemIR::InstBlockId::None,
  800. .call_params_id = SemIR::InstBlockId::None,
  801. .param_ranges = SemIR::Function::CallParamIndexRanges::Empty};
  802. }
  803. MatchContext match(MatchKind::Callee);
  804. // We add work to the stack in reverse so that the results will be produced
  805. // in the original order.
  806. if (implicit_param_patterns_id.has_value()) {
  807. for (SemIR::InstId inst_id :
  808. llvm::reverse(context.inst_blocks().Get(implicit_param_patterns_id))) {
  809. match.AddWork(
  810. {.pattern_id = inst_id,
  811. .work = MatchContext::PreWork{.scrutinee_id = SemIR::InstId::None}});
  812. }
  813. }
  814. match.DoWork(context);
  815. auto implicit_end = SemIR::CallParamIndex(match.param_count());
  816. if (param_patterns_id.has_value()) {
  817. for (SemIR::InstId inst_id :
  818. llvm::reverse(context.inst_blocks().Get(param_patterns_id))) {
  819. match.AddWork(
  820. {.pattern_id = inst_id,
  821. .work = MatchContext::PreWork{.scrutinee_id = SemIR::InstId::None}});
  822. }
  823. }
  824. match.DoWork(context);
  825. auto explicit_end = SemIR::CallParamIndex(match.param_count());
  826. for (auto return_pattern_id :
  827. context.inst_blocks().GetOrEmpty(return_patterns_id)) {
  828. match.AddWork(
  829. {.pattern_id = return_pattern_id,
  830. .work = MatchContext::PreWork{.scrutinee_id = SemIR::InstId::None}});
  831. }
  832. match.DoWork(context);
  833. auto return_end = SemIR::CallParamIndex(match.param_count());
  834. match.DoWork(context);
  835. auto blocks = std::move(match).GetCallParams(context);
  836. return {.call_param_patterns_id = blocks.call_param_patterns_id,
  837. .call_params_id = blocks.call_params_id,
  838. .param_ranges = {implicit_end, explicit_end, return_end}};
  839. }
  840. auto CallerPatternMatch(Context& context, SemIR::SpecificId specific_id,
  841. SemIR::InstId self_pattern_id,
  842. SemIR::InstBlockId param_patterns_id,
  843. SemIR::InstBlockId return_patterns_id,
  844. SemIR::InstId self_arg_id,
  845. llvm::ArrayRef<SemIR::InstId> arg_refs,
  846. llvm::ArrayRef<SemIR::InstId> return_arg_ids,
  847. bool is_operator_syntax) -> SemIR::InstBlockId {
  848. MatchContext match(MatchKind::Caller, specific_id);
  849. auto return_patterns = context.inst_blocks().GetOrEmpty(return_patterns_id);
  850. // Track the return storage, if present.
  851. for (auto [return_pattern_id, return_arg_id] :
  852. llvm::zip_equal(return_patterns, return_arg_ids)) {
  853. if (return_arg_id.has_value()) {
  854. match.AddWork(
  855. {.pattern_id = return_pattern_id,
  856. .work = MatchContext::PreWork{.scrutinee_id = return_arg_id}});
  857. } else {
  858. CARBON_CHECK(return_arg_ids.size() == 1,
  859. "TODO: do the match even if return_arg_id is None, so that "
  860. "subsequent args are at the right index in the arg block");
  861. }
  862. }
  863. // Check type conversions per-element.
  864. for (auto [arg_id, param_pattern_id] : llvm::reverse(llvm::zip_equal(
  865. arg_refs, context.inst_blocks().GetOrEmpty(param_patterns_id)))) {
  866. match.AddWork({.pattern_id = param_pattern_id,
  867. .work = MatchContext::PreWork{.scrutinee_id = arg_id},
  868. .allow_unmarked_ref = is_operator_syntax});
  869. }
  870. if (self_pattern_id.has_value()) {
  871. match.AddWork({.pattern_id = self_pattern_id,
  872. .work = MatchContext::PreWork{.scrutinee_id = self_arg_id},
  873. .allow_unmarked_ref = true});
  874. }
  875. match.DoWork(context);
  876. return std::move(match).GetCallArgs(context);
  877. }
  878. auto LocalPatternMatch(Context& context, SemIR::InstId pattern_id,
  879. SemIR::InstId scrutinee_id) -> void {
  880. MatchContext match(MatchKind::Local);
  881. match.AddWork({.pattern_id = pattern_id,
  882. .work = MatchContext::PreWork{.scrutinee_id = scrutinee_id}});
  883. match.DoWork(context);
  884. }
  885. } // namespace Carbon::Check