pattern_match.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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 <vector>
  7. #include "llvm/ADT/STLExtras.h"
  8. #include "llvm/ADT/SmallVector.h"
  9. #include "toolchain/base/kind_switch.h"
  10. #include "toolchain/check/context.h"
  11. #include "toolchain/check/convert.h"
  12. #include "toolchain/check/subpattern.h"
  13. #include "toolchain/check/type.h"
  14. namespace Carbon::Check {
  15. // Returns a best-effort name for the given ParamPattern, suitable for use in
  16. // IR pretty-printing.
  17. template <typename ParamPattern>
  18. static auto GetPrettyName(Context& context, ParamPattern param_pattern)
  19. -> SemIR::NameId {
  20. if (context.insts().Is<SemIR::ReturnSlotPattern>(
  21. param_pattern.subpattern_id)) {
  22. return SemIR::NameId::ReturnSlot;
  23. }
  24. if (auto binding_pattern = context.insts().TryGetAs<SemIR::AnyBindingPattern>(
  25. param_pattern.subpattern_id)) {
  26. return context.entity_names().Get(binding_pattern->entity_name_id).name_id;
  27. }
  28. return SemIR::NameId::None;
  29. }
  30. namespace {
  31. // Selects between the different kinds of pattern matching.
  32. enum class MatchKind : uint8_t {
  33. // Caller pattern matching occurs on the caller side of a function call, and
  34. // is responsible for matching the argument expression against the portion
  35. // of the pattern above the ParamPattern insts.
  36. Caller,
  37. // Callee pattern matching occurs in the function decl block, and is
  38. // responsible for matching the function's calling-convention parameters
  39. // against the portion of the pattern below the ParamPattern insts.
  40. Callee,
  41. // Local pattern matching is pattern matching outside of a function call,
  42. // such as in a let/var declaration.
  43. Local,
  44. };
  45. // The collected state of a pattern-matching operation.
  46. class MatchContext {
  47. public:
  48. struct WorkItem {
  49. SemIR::InstId pattern_id;
  50. // `None` when processing the callee side.
  51. SemIR::InstId scrutinee_id;
  52. };
  53. // Constructs a MatchContext. If `callee_specific_id` is not `None`, this
  54. // pattern match operation is part of implementing the signature of the given
  55. // specific.
  56. explicit MatchContext(MatchKind kind, SemIR::SpecificId callee_specific_id =
  57. SemIR::SpecificId::None)
  58. : next_index_(0), kind_(kind), callee_specific_id_(callee_specific_id) {}
  59. // Adds a work item to the stack.
  60. auto AddWork(WorkItem work_item) -> void { stack_.push_back(work_item); }
  61. // Processes all work items on the stack. When performing caller pattern
  62. // matching, returns an inst block with one inst reference for each
  63. // calling-convention argument. When performing callee pattern matching,
  64. // returns an inst block with references to all the emitted BindName insts.
  65. auto DoWork(Context& context) -> SemIR::InstBlockId;
  66. private:
  67. // Allocates the next unallocated RuntimeParamIndex, starting from 0.
  68. auto NextRuntimeIndex() -> SemIR::RuntimeParamIndex {
  69. auto result = next_index_;
  70. ++next_index_.index;
  71. return result;
  72. }
  73. // Emits the pattern-match insts necessary to match the pattern inst
  74. // `entry.pattern_id` against the scrutinee value `entry.scrutinee_id`, and
  75. // adds to `stack_` any work necessary to traverse into its subpatterns. This
  76. // behavior is contingent on the kind of match being performed, as indicated
  77. // by kind_`. For example, when performing a callee pattern match, this does
  78. // not emit insts for patterns on the caller side. However, it still traverses
  79. // into subpatterns if any of their descendants might emit insts.
  80. // TODO: Require that `entry.scrutinee_id` is valid if and only if insts
  81. // should be emitted, once we start emitting `Param` insts in the
  82. // `ParamPattern` case.
  83. auto EmitPatternMatch(Context& context, MatchContext::WorkItem entry) -> void;
  84. // The stack of work to be processed.
  85. llvm::SmallVector<WorkItem> stack_;
  86. // The next index to be allocated by `NextRuntimeIndex`.
  87. SemIR::RuntimeParamIndex next_index_;
  88. // The pending results that will be returned by the current `DoWork` call.
  89. llvm::SmallVector<SemIR::InstId> results_;
  90. // The kind of pattern match being performed.
  91. MatchKind kind_;
  92. // The SpecificId of the function being called (if any).
  93. SemIR::SpecificId callee_specific_id_;
  94. };
  95. } // namespace
  96. auto MatchContext::DoWork(Context& context) -> SemIR::InstBlockId {
  97. results_.reserve(stack_.size());
  98. while (!stack_.empty()) {
  99. EmitPatternMatch(context, stack_.pop_back_val());
  100. }
  101. auto block_id = context.inst_blocks().Add(results_);
  102. results_.clear();
  103. return block_id;
  104. }
  105. // Inserts the given region into the current code block. If the region
  106. // consists of a single block, this will be implemented as a `splice_block`
  107. // inst. Otherwise, this will end the current block with a branch to the entry
  108. // block of the region, and add future insts to a new block which is the
  109. // immediate successor of the region's exit block. As a result, this cannot be
  110. // called more than once for the same region.
  111. static auto InsertHere(Context& context, SemIR::ExprRegionId region_id)
  112. -> SemIR::InstId {
  113. auto region = context.sem_ir().expr_regions().Get(region_id);
  114. auto loc_id = context.insts().GetLocId(region.result_id);
  115. auto exit_block = context.inst_blocks().Get(region.block_ids.back());
  116. if (region.block_ids.size() == 1) {
  117. // TODO: Is it possible to avoid leaving an "orphan" block in the IR in the
  118. // first two cases?
  119. if (exit_block.empty()) {
  120. return region.result_id;
  121. }
  122. if (exit_block.size() == 1) {
  123. context.inst_block_stack().AddInstId(exit_block.front());
  124. return region.result_id;
  125. }
  126. return context.AddInst<SemIR::SpliceBlock>(
  127. loc_id, {.type_id = context.insts().Get(region.result_id).type_id(),
  128. .block_id = region.block_ids.front(),
  129. .result_id = region.result_id});
  130. }
  131. if (context.region_stack().empty()) {
  132. context.TODO(loc_id,
  133. "Control flow expressions are currently only supported inside "
  134. "functions.");
  135. return SemIR::ErrorInst::SingletonInstId;
  136. }
  137. context.AddInst(SemIR::LocIdAndInst::NoLoc<SemIR::Branch>(
  138. {.target_id = region.block_ids.front()}));
  139. context.inst_block_stack().Pop();
  140. // TODO: this will cumulatively cost O(MN) running time for M blocks
  141. // at the Nth level of the stack. Figure out how to do better.
  142. context.region_stack().AddToRegion(region.block_ids);
  143. auto resume_with_block_id =
  144. context.insts().GetAs<SemIR::Branch>(exit_block.back()).target_id;
  145. CARBON_CHECK(context.inst_blocks().GetOrEmpty(resume_with_block_id).empty());
  146. context.inst_block_stack().Push(resume_with_block_id);
  147. context.region_stack().AddToRegion(resume_with_block_id, loc_id);
  148. return region.result_id;
  149. }
  150. auto MatchContext::EmitPatternMatch(Context& context,
  151. MatchContext::WorkItem entry) -> void {
  152. if (entry.pattern_id == SemIR::ErrorInst::SingletonInstId) {
  153. results_.push_back(SemIR::ErrorInst::SingletonInstId);
  154. return;
  155. }
  156. DiagnosticAnnotationScope annotate_diagnostics(
  157. &context.emitter(), [&](auto& builder) {
  158. if (kind_ == MatchKind::Caller) {
  159. CARBON_DIAGNOSTIC(InCallToFunctionParam, Note,
  160. "initializing function parameter");
  161. builder.Note(entry.pattern_id, InCallToFunctionParam);
  162. }
  163. });
  164. auto pattern = context.insts().GetWithLocId(entry.pattern_id);
  165. CARBON_KIND_SWITCH(pattern.inst) {
  166. case SemIR::BindingPattern::Kind:
  167. case SemIR::SymbolicBindingPattern::Kind: {
  168. auto binding_pattern = pattern.inst.As<SemIR::AnyBindingPattern>();
  169. // We're logically consuming this map entry, so we invalidate it in order
  170. // to avoid accidentally consuming it twice.
  171. auto [bind_name_id, type_expr_region_id] = std::exchange(
  172. context.bind_name_map().Lookup(entry.pattern_id).value(),
  173. {.bind_name_id = SemIR::InstId::None,
  174. .type_expr_region_id = SemIR::ExprRegionId::None});
  175. InsertHere(context, type_expr_region_id);
  176. auto value_id = entry.scrutinee_id;
  177. switch (kind_) {
  178. case MatchKind::Local: {
  179. value_id = ConvertToValueOrRefOfType(
  180. context, context.insts().GetLocId(entry.scrutinee_id),
  181. entry.scrutinee_id, binding_pattern.type_id);
  182. break;
  183. }
  184. case MatchKind::Callee: {
  185. if (context.insts()
  186. .GetAs<SemIR::AnyParam>(value_id)
  187. .runtime_index.has_value()) {
  188. results_.push_back(value_id);
  189. }
  190. break;
  191. }
  192. case MatchKind::Caller:
  193. CARBON_FATAL("Found binding pattern during caller pattern match");
  194. }
  195. auto bind_name = context.insts().GetAs<SemIR::AnyBindName>(bind_name_id);
  196. CARBON_CHECK(!bind_name.value_id.has_value());
  197. bind_name.value_id = value_id;
  198. context.ReplaceInstBeforeConstantUse(bind_name_id, bind_name);
  199. context.inst_block_stack().AddInstId(bind_name_id);
  200. break;
  201. }
  202. case CARBON_KIND(SemIR::AddrPattern addr_pattern): {
  203. CARBON_CHECK(kind_ != MatchKind::Local);
  204. if (kind_ == MatchKind::Callee) {
  205. // We're emitting pattern-match IR for the callee, but we're still on
  206. // the caller side of the pattern, so we traverse without emitting any
  207. // insts.
  208. AddWork({.pattern_id = addr_pattern.inner_id,
  209. .scrutinee_id = SemIR::InstId::None});
  210. break;
  211. }
  212. CARBON_CHECK(entry.scrutinee_id.has_value());
  213. auto scrutinee_ref_id =
  214. ConvertToValueOrRefExpr(context, entry.scrutinee_id);
  215. switch (SemIR::GetExprCategory(context.sem_ir(), scrutinee_ref_id)) {
  216. case SemIR::ExprCategory::Error:
  217. case SemIR::ExprCategory::DurableRef:
  218. case SemIR::ExprCategory::EphemeralRef:
  219. break;
  220. default:
  221. CARBON_DIAGNOSTIC(AddrSelfIsNonRef, Error,
  222. "`addr self` method cannot be invoked on a value");
  223. context.emitter().Emit(
  224. TokenOnly(context.insts().GetLocId(entry.scrutinee_id)),
  225. AddrSelfIsNonRef);
  226. results_.push_back(SemIR::ErrorInst::SingletonInstId);
  227. return;
  228. }
  229. auto scrutinee_ref = context.insts().Get(scrutinee_ref_id);
  230. auto new_scrutinee = context.AddInst<SemIR::AddrOf>(
  231. context.insts().GetLocId(scrutinee_ref_id),
  232. {.type_id = GetPointerType(context, scrutinee_ref.type_id()),
  233. .lvalue_id = scrutinee_ref_id});
  234. AddWork(
  235. {.pattern_id = addr_pattern.inner_id, .scrutinee_id = new_scrutinee});
  236. break;
  237. }
  238. case CARBON_KIND(SemIR::ValueParamPattern param_pattern): {
  239. CARBON_CHECK(param_pattern.runtime_index.index < 0 ||
  240. static_cast<size_t>(param_pattern.runtime_index.index) ==
  241. results_.size(),
  242. "Parameters out of order; expecting {0} but got {1}",
  243. results_.size(), param_pattern.runtime_index.index);
  244. switch (kind_) {
  245. case MatchKind::Caller: {
  246. CARBON_CHECK(entry.scrutinee_id.has_value());
  247. if (entry.scrutinee_id == SemIR::ErrorInst::SingletonInstId) {
  248. results_.push_back(SemIR::ErrorInst::SingletonInstId);
  249. } else {
  250. results_.push_back(ConvertToValueOfType(
  251. context, context.insts().GetLocId(entry.scrutinee_id),
  252. entry.scrutinee_id,
  253. SemIR::GetTypeInSpecific(context.sem_ir(), callee_specific_id_,
  254. param_pattern.type_id)));
  255. }
  256. // Do not traverse farther, because the caller side of the pattern
  257. // ends here.
  258. break;
  259. }
  260. case MatchKind::Callee: {
  261. if (param_pattern.runtime_index ==
  262. SemIR::RuntimeParamIndex::Unknown) {
  263. param_pattern.runtime_index = NextRuntimeIndex();
  264. context.ReplaceInstBeforeConstantUse(entry.pattern_id,
  265. param_pattern);
  266. }
  267. AddWork(
  268. {.pattern_id = param_pattern.subpattern_id,
  269. .scrutinee_id = context.AddInst<SemIR::ValueParam>(
  270. pattern.loc_id,
  271. {.type_id = param_pattern.type_id,
  272. .runtime_index = param_pattern.runtime_index,
  273. .pretty_name_id = GetPrettyName(context, param_pattern)})});
  274. break;
  275. }
  276. case MatchKind::Local: {
  277. CARBON_FATAL("Found ValueParamPattern during local pattern match");
  278. }
  279. }
  280. break;
  281. }
  282. case CARBON_KIND(SemIR::OutParamPattern param_pattern): {
  283. switch (kind_) {
  284. case MatchKind::Caller: {
  285. CARBON_CHECK(entry.scrutinee_id.has_value());
  286. CARBON_CHECK(context.insts().Get(entry.scrutinee_id).type_id() ==
  287. SemIR::GetTypeInSpecific(context.sem_ir(),
  288. callee_specific_id_,
  289. param_pattern.type_id));
  290. results_.push_back(entry.scrutinee_id);
  291. // Do not traverse farther, because the caller side of the pattern
  292. // ends here.
  293. break;
  294. }
  295. case MatchKind::Callee: {
  296. // TODO: Consider ways to address near-duplication with the
  297. // ValueParamPattern case.
  298. if (param_pattern.runtime_index ==
  299. SemIR::RuntimeParamIndex::Unknown) {
  300. param_pattern.runtime_index = NextRuntimeIndex();
  301. context.ReplaceInstBeforeConstantUse(entry.pattern_id,
  302. param_pattern);
  303. }
  304. AddWork(
  305. {.pattern_id = param_pattern.subpattern_id,
  306. .scrutinee_id = context.AddInst<SemIR::OutParam>(
  307. pattern.loc_id,
  308. {.type_id = param_pattern.type_id,
  309. .runtime_index = param_pattern.runtime_index,
  310. .pretty_name_id = GetPrettyName(context, param_pattern)})});
  311. break;
  312. }
  313. case MatchKind::Local: {
  314. CARBON_FATAL("Found OutParamPattern during local pattern match");
  315. }
  316. }
  317. break;
  318. }
  319. case CARBON_KIND(SemIR::ReturnSlotPattern return_slot_pattern): {
  320. CARBON_CHECK(kind_ == MatchKind::Callee);
  321. auto return_slot_id = context.AddInst<SemIR::ReturnSlot>(
  322. pattern.loc_id, {.type_id = return_slot_pattern.type_id,
  323. .type_inst_id = return_slot_pattern.type_inst_id,
  324. .storage_id = entry.scrutinee_id});
  325. bool already_in_lookup =
  326. context.scope_stack()
  327. .LookupOrAddName(SemIR::NameId::ReturnSlot, return_slot_id)
  328. .has_value();
  329. CARBON_CHECK(!already_in_lookup);
  330. results_.push_back(entry.scrutinee_id);
  331. break;
  332. }
  333. case CARBON_KIND(SemIR::VarPattern var_pattern): {
  334. auto var_id = context.var_storage_map().Lookup(entry.pattern_id).value();
  335. // TODO: Find a more efficient way to put these insts in the global_init
  336. // block (or drop the distinction between the global_init block and the
  337. // file scope?)
  338. if (context.scope_stack().PeekIndex() == ScopeIndex::Package) {
  339. context.global_init().Resume();
  340. }
  341. if (entry.scrutinee_id.has_value()) {
  342. auto init_id =
  343. Initialize(context, pattern.loc_id, var_id, entry.scrutinee_id);
  344. // TODO: Consider using different instruction kinds for assignment
  345. // versus initialization.
  346. context.AddInst<SemIR::Assign>(pattern.loc_id,
  347. {.lhs_id = var_id, .rhs_id = init_id});
  348. }
  349. AddWork(
  350. {.pattern_id = var_pattern.subpattern_id, .scrutinee_id = var_id});
  351. if (context.scope_stack().PeekIndex() == ScopeIndex::Package) {
  352. context.global_init().Suspend();
  353. }
  354. break;
  355. }
  356. default: {
  357. CARBON_FATAL("Inst kind not handled: {0}", pattern.inst.kind());
  358. }
  359. }
  360. }
  361. auto CalleePatternMatch(Context& context,
  362. SemIR::InstBlockId implicit_param_patterns_id,
  363. SemIR::InstBlockId param_patterns_id,
  364. SemIR::InstId return_slot_pattern_id)
  365. -> SemIR::InstBlockId {
  366. if (!return_slot_pattern_id.has_value() && !param_patterns_id.has_value() &&
  367. !implicit_param_patterns_id.has_value()) {
  368. return SemIR::InstBlockId::None;
  369. }
  370. MatchContext match(MatchKind::Callee);
  371. // We add work to the stack in reverse so that the results will be produced
  372. // in the original order.
  373. if (return_slot_pattern_id.has_value()) {
  374. match.AddWork({.pattern_id = return_slot_pattern_id,
  375. .scrutinee_id = SemIR::InstId::None});
  376. }
  377. if (param_patterns_id.has_value()) {
  378. for (SemIR::InstId inst_id :
  379. llvm::reverse(context.inst_blocks().Get(param_patterns_id))) {
  380. match.AddWork(
  381. {.pattern_id = inst_id, .scrutinee_id = SemIR::InstId::None});
  382. }
  383. }
  384. if (implicit_param_patterns_id.has_value()) {
  385. for (SemIR::InstId inst_id :
  386. llvm::reverse(context.inst_blocks().Get(implicit_param_patterns_id))) {
  387. match.AddWork(
  388. {.pattern_id = inst_id, .scrutinee_id = SemIR::InstId::None});
  389. }
  390. }
  391. return match.DoWork(context);
  392. }
  393. auto CallerPatternMatch(Context& context, SemIR::SpecificId specific_id,
  394. SemIR::InstId self_pattern_id,
  395. SemIR::InstBlockId param_patterns_id,
  396. SemIR::InstId return_slot_pattern_id,
  397. SemIR::InstId self_arg_id,
  398. llvm::ArrayRef<SemIR::InstId> arg_refs,
  399. SemIR::InstId return_slot_arg_id)
  400. -> SemIR::InstBlockId {
  401. MatchContext match(MatchKind::Caller, specific_id);
  402. // Track the return storage, if present.
  403. if (return_slot_arg_id.has_value()) {
  404. CARBON_CHECK(return_slot_pattern_id.has_value());
  405. match.AddWork({.pattern_id = return_slot_pattern_id,
  406. .scrutinee_id = return_slot_arg_id});
  407. }
  408. // Check type conversions per-element.
  409. for (auto [arg_id, param_pattern_id] : llvm::reverse(llvm::zip_equal(
  410. arg_refs, context.inst_blocks().GetOrEmpty(param_patterns_id)))) {
  411. auto runtime_index = SemIR::Function::GetParamPatternInfoFromPatternId(
  412. context.sem_ir(), param_pattern_id)
  413. .inst.runtime_index;
  414. if (!runtime_index.has_value()) {
  415. // Not a runtime parameter: we don't pass an argument.
  416. continue;
  417. }
  418. match.AddWork({.pattern_id = param_pattern_id, .scrutinee_id = arg_id});
  419. }
  420. if (self_pattern_id.has_value()) {
  421. match.AddWork({.pattern_id = self_pattern_id, .scrutinee_id = self_arg_id});
  422. }
  423. return match.DoWork(context);
  424. }
  425. auto LocalPatternMatch(Context& context, SemIR::InstId pattern_id,
  426. SemIR::InstId scrutinee_id) -> void {
  427. MatchContext match(MatchKind::Local);
  428. match.AddWork({.pattern_id = pattern_id, .scrutinee_id = scrutinee_id});
  429. match.DoWork(context);
  430. }
  431. } // namespace Carbon::Check