pattern.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. auto AddBindingPattern(Context& context, SemIR::LocId name_loc,
  32. SemIR::NameId name_id, SemIR::TypeId type_id,
  33. SemIR::ExprRegionId type_region_id, bool is_generic,
  34. bool is_template) -> BindingPatternInfo;
  35. // Creates storage for `var` patterns nested within the given pattern at the
  36. // current location in the output SemIR. For a `returned var`, this
  37. // reuses the function's return slot when present.
  38. auto AddPatternVarStorage(Context& context, SemIR::InstBlockId pattern_block_id,
  39. bool is_returned_var) -> void;
  40. // Adds a `self` parameter pattern with the specified type information. This
  41. // only sets up the binding pattern and type; callers are expected to add the
  42. // returned instruction to appropriate blocks. This is used when generating
  43. // functions, rather than processing a user-authored `self: Self`.
  44. auto AddSelfParamPattern(Context& context, SemIR::LocId loc_id,
  45. SemIR::ExprRegionId type_expr_region_id,
  46. SemIR::TypeId type_id) -> SemIR::InstId;
  47. // As the above, but for `addr self: Self*`.
  48. auto AddAddrSelfParamPattern(Context& context, SemIR::LocId loc_id,
  49. SemIR::ExprRegionId type_expr_region_id,
  50. SemIR::TypeInstId type_inst_id) -> SemIR::InstId;
  51. } // namespace Carbon::Check
  52. #endif // CARBON_TOOLCHAIN_CHECK_PATTERN_H_