pattern.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. // The phase of a binding pattern.
  30. enum class BindingPhase { Template, Symbolic, Runtime };
  31. // Creates an entity name for a binding pattern with the given properties.
  32. auto AddBindingEntityName(Context& context, SemIR::NameId name_id,
  33. SemIR::ConstantId form_id, bool is_unused,
  34. BindingPhase phase) -> SemIR::EntityNameId;
  35. // Creates a binding pattern and the associated binding inst, and returns their
  36. // IDs. `type_region_id` is the region representing the binding's type
  37. // expression.
  38. auto AddBindingPattern(Context& context, SemIR::LocId name_loc,
  39. SemIR::ExprRegionId type_region_id,
  40. SemIR::AnyBindingPattern pattern) -> BindingPatternInfo;
  41. // Creates storage for `var` patterns nested within the given pattern at the
  42. // current location in the output SemIR. For a `returned var`, this
  43. // reuses the function's return slot when present.
  44. auto AddPatternVarStorage(Context& context, SemIR::InstBlockId pattern_block_id,
  45. bool is_returned_var) -> void;
  46. // Adds a parameter pattern with the specified name and type information. The
  47. // pattern emulates `x: T` or `ref x: T` depending on the value of
  48. // `is_ref` (`var x: T` is not supported). This only sets up the parameter
  49. // pattern, binding pattern and type; callers are expected to add the returned
  50. // parameter pattern instruction to appropriate blocks. This is used when
  51. // generating functions, rather than processing a user-authored declaration.
  52. auto AddParamPattern(Context& context, SemIR::LocId loc_id,
  53. SemIR::NameId name_id,
  54. SemIR::ExprRegionId type_expr_region_id,
  55. SemIR::TypeId type_id, bool is_ref) -> SemIR::InstId;
  56. } // namespace Carbon::Check
  57. #endif // CARBON_TOOLCHAIN_CHECK_PATTERN_H_