pattern.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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_H_
  5. #define CARBON_TOOLCHAIN_CHECK_PATTERN_H_
  6. #include "toolchain/check/context.h"
  7. #include "toolchain/sem_ir/ids.h"
  8. namespace Carbon::Check {
  9. // Marks the start of a region of insts in a pattern context that might
  10. // represent an expression or a pattern. Typically this is called when
  11. // handling a parse node that can immediately precede a subpattern (such
  12. // as `let` or a `,` in a pattern list), and the handler for the subpattern
  13. // node makes the matching `EndSubpatternAs*` call.
  14. auto BeginSubpattern(Context& context) -> void;
  15. // Ends a region started by BeginSubpattern (in stack order), treating it as
  16. // an expression with the given result, and returns the ID of the region. The
  17. // region will not yet have any control-flow edges into or out of it.
  18. auto EndSubpatternAsExpr(Context& context, SemIR::InstId result_id)
  19. -> SemIR::ExprRegionId;
  20. // Ends a region started by BeginSubpattern (in stack order), asserting that
  21. // it had no expression content.
  22. auto EndSubpatternAsNonExpr(Context& context) -> void;
  23. // Information about a created binding pattern.
  24. struct BindingPatternInfo {
  25. SemIR::InstId pattern_id;
  26. SemIR::InstId bind_id;
  27. };
  28. // TODO: Add EndSubpatternAsPattern, when needed.
  29. // Creates a binding pattern. Returns the binding pattern and the bind name
  30. // instruction.
  31. // TODO: remove is_template once we have a separate InstKind for template
  32. // bindings.
  33. auto AddBindingPattern(Context& context, SemIR::LocId name_loc,
  34. SemIR::NameId name_id, SemIR::TypeId type_id,
  35. SemIR::ExprRegionId type_region_id,
  36. SemIR::InstKind pattern_kind, bool is_template)
  37. -> BindingPatternInfo;
  38. // Creates storage for `var` patterns nested within the given pattern at the
  39. // current location in the output SemIR. For a `returned var`, this
  40. // reuses the function's return slot when present.
  41. auto AddPatternVarStorage(Context& context, SemIR::InstBlockId pattern_block_id,
  42. bool is_returned_var) -> void;
  43. // Adds a `self` parameter pattern with the specified type information. This
  44. // only sets up the binding pattern and type; callers are expected to add the
  45. // returned instruction to appropriate blocks. This is used when generating
  46. // functions, rather than processing a user-authored `self: Self`.
  47. auto AddSelfParamPattern(Context& context, SemIR::LocId loc_id,
  48. SemIR::ExprRegionId type_expr_region_id,
  49. SemIR::TypeId type_id) -> SemIR::InstId;
  50. // As the above, but for `addr self: Self*`.
  51. auto AddAddrSelfParamPattern(Context& context, SemIR::LocId loc_id,
  52. SemIR::ExprRegionId type_expr_region_id,
  53. SemIR::TypeInstId type_inst_id) -> SemIR::InstId;
  54. } // namespace Carbon::Check
  55. #endif // CARBON_TOOLCHAIN_CHECK_PATTERN_H_