handle_pattern_binding.cpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 type_node_copy = type_node;
  19. auto cast_type_id = ExpressionAsType(context, type_node, parsed_type_id);
  20. // Get the name.
  21. auto [name_node, name_id] =
  22. context.node_stack().PopWithParseNode<Parse::NodeKind::Name>();
  23. // Allocate a node of the appropriate kind, linked to the name for error
  24. // locations.
  25. // TODO: Each of these cases should create a `BindName` node.
  26. switch (auto context_parse_node_kind = context.parse_tree().node_kind(
  27. context.node_stack().PeekParseNode())) {
  28. case Parse::NodeKind::VariableIntroducer:
  29. if (!context.TryToCompleteType(cast_type_id, [&] {
  30. CARBON_DIAGNOSTIC(IncompleteTypeInVarDeclaration, Error,
  31. "Variable has incomplete type `{0}`.",
  32. std::string);
  33. return context.emitter().Build(
  34. type_node_copy, IncompleteTypeInVarDeclaration,
  35. context.sem_ir().StringifyType(cast_type_id, true));
  36. })) {
  37. cast_type_id = SemIR::TypeId::Error;
  38. }
  39. context.AddNodeAndPush(
  40. parse_node, SemIR::VarStorage{name_node, cast_type_id, name_id});
  41. break;
  42. case Parse::NodeKind::ParameterListStart:
  43. // Parameters can have incomplete types in a function declaration, but not
  44. // in a function definition. We don't know which kind we have here.
  45. context.AddNodeAndPush(
  46. parse_node, SemIR::Parameter{name_node, cast_type_id, name_id});
  47. break;
  48. case Parse::NodeKind::LetIntroducer:
  49. if (!context.TryToCompleteType(cast_type_id, [&] {
  50. CARBON_DIAGNOSTIC(IncompleteTypeInLetDeclaration, Error,
  51. "`let` binding has incomplete type `{0}`.",
  52. std::string);
  53. return context.emitter().Build(
  54. type_node_copy, IncompleteTypeInLetDeclaration,
  55. context.sem_ir().StringifyType(cast_type_id, true));
  56. })) {
  57. cast_type_id = SemIR::TypeId::Error;
  58. }
  59. // Create the node, but don't add it to a block until after we've formed
  60. // its initializer.
  61. // TODO: For general pattern parsing, we'll need to create a block to hold
  62. // the `let` pattern before we see the initializer.
  63. context.node_stack().Push(
  64. parse_node,
  65. context.nodes().AddInNoBlock(SemIR::BindName{
  66. name_node, cast_type_id, name_id, SemIR::NodeId::Invalid}));
  67. break;
  68. default:
  69. CARBON_FATAL() << "Found a pattern binding in unexpected context "
  70. << context_parse_node_kind;
  71. }
  72. return true;
  73. }
  74. auto HandleTemplate(Context& context, Parse::Node parse_node) -> bool {
  75. return context.TODO(parse_node, "HandleTemplate");
  76. }
  77. } // namespace Carbon::Check