pattern_match.h 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #ifndef CARBON_TOOLCHAIN_CHECK_PATTERN_MATCH_H_
  5. #define CARBON_TOOLCHAIN_CHECK_PATTERN_MATCH_H_
  6. #include "toolchain/check/context.h"
  7. #include "toolchain/sem_ir/function.h"
  8. #include "toolchain/sem_ir/ids.h"
  9. namespace Carbon::Check {
  10. // TODO: Find a better place for this overview, once it has stabilized.
  11. //
  12. // The signature pattern of a function call is matched partially by the caller
  13. // and partially by the callee. `ParamPattern` insts mark the boundary
  14. // between the two: pattern insts that are descendants of a `ParamPattern`
  15. // are matched by the callee, and pattern insts that have a `ParamPattern`
  16. // as a descendant are matched by the caller.
  17. // Return type for CalleePatternMatch.
  18. struct CalleePatternMatchResults {
  19. SemIR::InstBlockId call_param_patterns_id;
  20. SemIR::InstBlockId call_params_id;
  21. SemIR::Function::CallParamIndexRanges param_ranges;
  22. };
  23. // Emits the pattern-match IR for the declaration of a parameterized entity with
  24. // the given implicit and explicit parameter patterns, and the given return
  25. // pattern (any of which may be `None` if not applicable). This IR performs the
  26. // callee side of pattern matching, starting at the `ParamPattern` insts, and
  27. // matching them against the corresponding `Call` parameters (see
  28. // entity_with_params_base.h for the definition of that term).
  29. // Returns the IDs of inst blocks consisting of references to the `Call`
  30. // parameter patterns and `Call` parameters of the function, as well as
  31. // the implicit, explicit, and return index ranges of those blocks.
  32. auto CalleePatternMatch(Context& context,
  33. SemIR::InstBlockId implicit_param_patterns_id,
  34. SemIR::InstBlockId param_patterns_id,
  35. SemIR::InstId return_pattern_id)
  36. -> CalleePatternMatchResults;
  37. // Return type for ThunkPatternMatch.
  38. struct ThunkPatternMatchResults {
  39. // The syntactic argument list. If `self_pattern_id` is not `None`, the first
  40. // element will be the corresponding argument.
  41. llvm::SmallVector<SemIR::InstId> syntactic_args;
  42. // The trailing elements of `outer_call_args` that were not used in
  43. // `syntactic_args`. These presumably represent the output arguments for the
  44. // return.
  45. llvm::ArrayRef<SemIR::InstId> ignored_call_args;
  46. };
  47. // Given the `Call` arguments for the outer part of a thunked function call,
  48. // computes the corresponding syntactic argument list, suitable for passing to
  49. // the inner part of the thunked function call.
  50. auto ThunkPatternMatch(Context& context, SemIR::InstId self_pattern_id,
  51. llvm::ArrayRef<SemIR::InstId> param_pattern_ids,
  52. llvm::ArrayRef<SemIR::InstId> outer_call_args)
  53. -> ThunkPatternMatchResults;
  54. // Emits the pattern-match IR for matching the given arguments with the given
  55. // parameter patterns, and returns an inst block of the arguments that should
  56. // be passed to the `Call` inst. `is_operator_syntax` indicates that this call
  57. // was generated from an operator rather than from function call syntax, so
  58. // arguments to `ref` parameters aren't required to have `ref` tags.
  59. auto CallerPatternMatch(Context& context, SemIR::SpecificId specific_id,
  60. SemIR::InstId self_pattern_id,
  61. SemIR::InstBlockId param_patterns_id,
  62. SemIR::InstId return_pattern_id,
  63. SemIR::InstId self_arg_id,
  64. llvm::ArrayRef<SemIR::InstId> arg_refs,
  65. SemIR::InstId return_arg_id, bool is_operator_syntax)
  66. -> SemIR::InstBlockId;
  67. // Emits the pattern-match IR for a local pattern matching operation with the
  68. // given pattern and scrutinee.
  69. auto LocalPatternMatch(Context& context, SemIR::InstId pattern_id,
  70. SemIR::InstId scrutinee_id) -> void;
  71. } // namespace Carbon::Check
  72. #endif // CARBON_TOOLCHAIN_CHECK_PATTERN_MATCH_H_