handle_pattern_binding.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. #include "toolchain/check/context.h"
  5. #include "toolchain/check/convert.h"
  6. #include "toolchain/sem_ir/node.h"
  7. namespace Carbon::Check {
  8. auto HandleAddress(Context& context, Parse::Node parse_node) -> bool {
  9. return context.TODO(parse_node, "HandleAddress");
  10. }
  11. auto HandleGenericPatternBinding(Context& context, Parse::Node parse_node)
  12. -> bool {
  13. return context.TODO(parse_node, "GenericPatternBinding");
  14. }
  15. auto HandlePatternBinding(Context& context, Parse::Node parse_node) -> bool {
  16. auto [type_node, parsed_type_id] =
  17. context.node_stack().PopExpressionWithParseNode();
  18. auto cast_type_id = ExpressionAsType(context, type_node, parsed_type_id);
  19. // Get the name.
  20. auto [name_node, name_id] =
  21. context.node_stack().PopWithParseNode<Parse::NodeKind::Name>();
  22. // Allocate a node of the appropriate kind, linked to the name for error
  23. // locations.
  24. // TODO: Each of these cases should create a `BindName` node.
  25. switch (auto context_parse_node_kind = context.parse_tree().node_kind(
  26. context.node_stack().PeekParseNode())) {
  27. case Parse::NodeKind::VariableIntroducer:
  28. context.AddNodeAndPush(
  29. parse_node, SemIR::VarStorage(name_node, cast_type_id, name_id));
  30. break;
  31. case Parse::NodeKind::ParameterListStart:
  32. context.AddNodeAndPush(
  33. parse_node, SemIR::Parameter(name_node, cast_type_id, name_id));
  34. break;
  35. case Parse::NodeKind::LetIntroducer:
  36. // Create the node, but don't add it to a block until after we've formed
  37. // its initializer.
  38. // TODO: For general pattern parsing, we'll need to create a block to hold
  39. // the `let` pattern before we see the initializer.
  40. context.node_stack().Push(
  41. parse_node,
  42. context.semantics_ir().AddNodeInNoBlock(SemIR::BindName(
  43. name_node, cast_type_id, name_id, SemIR::NodeId::Invalid)));
  44. break;
  45. default:
  46. CARBON_FATAL() << "Found a pattern binding in unexpected context "
  47. << context_parse_node_kind;
  48. }
  49. return true;
  50. }
  51. auto HandleTemplate(Context& context, Parse::Node parse_node) -> bool {
  52. return context.TODO(parse_node, "HandleTemplate");
  53. }
  54. } // namespace Carbon::Check