pattern_match.cpp 17 KB

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