pattern.h 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 contain
  10. // an expression. Typically this is called when handling a parse node that can
  11. // immediately precede a subpattern (such as `let` or a `,` in a pattern list).
  12. // `End[Empty]Subpattern` should be called later by the consumer of the
  13. // subpattern.
  14. auto BeginSubpattern(Context& context) -> void;
  15. // Consumes the expression in a region started by the most recent
  16. // BeginSubpattern, and returns the ID of the region. The region will not yet
  17. // have any control-flow edges into or out of it.
  18. auto ConsumeSubpatternExpr(Context& context, SemIR::InstId result_id)
  19. -> SemIR::ExprRegionId;
  20. // Ends a region started by BeginSubpattern (in stack order), asserting that
  21. // it either had no expression content or the expression has been consumed.
  22. auto EndEmptySubpattern(Context& context) -> void;
  23. // Ends a region started by BeginSubpattern (in stack order). If the top of the
  24. // node stack is an expression, the subpattern region is consumed and converted
  25. // to an expression pattern, which replaces the expression on the node stack.
  26. // Otherwise, the top of the node stack should be a pattern, in which case this
  27. // asserts that the subpattern region is either empty or has been consumed.
  28. //
  29. // The node stack is passed explicitly as a reminder that this function affects
  30. // the node stack, unlike the other *Subpattern functions.
  31. auto EndSubpattern(Context& context, NodeStack& node_stack) -> void;
  32. // Information about a created binding pattern.
  33. struct BindingPatternInfo {
  34. SemIR::InstId pattern_id;
  35. SemIR::InstId bind_id;
  36. };
  37. // The phase of a binding pattern.
  38. enum class BindingPhase { Template, Symbolic, Runtime };
  39. // Creates an entity name for a binding pattern with the given properties.
  40. auto AddBindingEntityName(Context& context, SemIR::NameId name_id,
  41. SemIR::InstId form_id, bool is_unused,
  42. BindingPhase phase) -> SemIR::EntityNameId;
  43. // Creates a binding pattern and the associated binding inst, and returns their
  44. // IDs. `type_region_id` is the region representing the binding's type
  45. // expression.
  46. auto AddBindingPattern(Context& context, SemIR::LocId name_loc,
  47. SemIR::ExprRegionId type_region_id,
  48. SemIR::AnyBindingPattern pattern) -> BindingPatternInfo;
  49. // Creates storage for `var` patterns nested within the given pattern at the
  50. // current location in the output SemIR. For a `returned var`, this
  51. // reuses the function's return slot when present.
  52. auto AddPatternVarStorage(Context& context, SemIR::InstBlockId pattern_block_id,
  53. bool is_returned_var) -> void;
  54. // Kinds of parameters that can be added by `AddParamPattern`.
  55. enum class ParamPatternKind {
  56. // A value parameter, `x: T`.
  57. Value,
  58. // A reference parameter, `ref x: T`.
  59. Ref,
  60. // A variable parameter, `var x: T`.
  61. Var,
  62. };
  63. // Adds a parameter pattern with the specified name and type information. The
  64. // pattern emulates `x: T`, `ref x: T`, or `var x: T` depending on the value of
  65. // `kind`. This only sets up the parameter pattern, binding pattern and type;
  66. // callers are expected to add the returned parameter pattern instruction to
  67. // appropriate blocks. This is used when generating functions, rather than
  68. // processing a user-authored declaration.
  69. auto AddParamPattern(Context& context, SemIR::LocId loc_id,
  70. SemIR::NameId name_id,
  71. SemIR::ExprRegionId type_expr_region_id,
  72. SemIR::TypeId type_id, ParamPatternKind kind)
  73. -> SemIR::InstId;
  74. } // namespace Carbon::Check
  75. #endif // CARBON_TOOLCHAIN_CHECK_PATTERN_H_