handle_pattern_binding.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. switch (auto context_parse_node_kind = context.parse_tree().node_kind(
  26. context.node_stack().PeekParseNode())) {
  27. case Parse::NodeKind::VariableIntroducer: {
  28. // A `var` declaration at class scope introduces a field.
  29. auto enclosing_class_decl =
  30. context.GetCurrentScopeAs<SemIR::ClassDeclaration>();
  31. if (!context.TryToCompleteType(cast_type_id, [&] {
  32. CARBON_DIAGNOSTIC(IncompleteTypeInVarDeclaration, Error,
  33. "{0} has incomplete type `{1}`.", llvm::StringRef,
  34. std::string);
  35. return context.emitter().Build(
  36. type_node_copy, IncompleteTypeInVarDeclaration,
  37. enclosing_class_decl ? "Field" : "Variable",
  38. context.sem_ir().StringifyType(cast_type_id, true));
  39. })) {
  40. cast_type_id = SemIR::TypeId::Error;
  41. }
  42. SemIR::NodeId value_id = SemIR::NodeId::Invalid;
  43. SemIR::TypeId value_type_id = cast_type_id;
  44. if (enclosing_class_decl) {
  45. auto& class_info =
  46. context.classes().Get(enclosing_class_decl->class_id);
  47. auto field_type_node_id = context.AddNode(SemIR::UnboundFieldType{
  48. parse_node, context.GetBuiltinType(SemIR::BuiltinKind::TypeType),
  49. class_info.self_type_id, cast_type_id});
  50. value_type_id = context.CanonicalizeType(field_type_node_id);
  51. value_id =
  52. context.AddNode(SemIR::Field{parse_node, value_type_id, name_id});
  53. // Add a corresponding field to the object representation of the class.
  54. context.args_type_info_stack().AddNode(
  55. SemIR::StructTypeField{parse_node, name_id, cast_type_id});
  56. } else {
  57. value_id = context.AddNode(
  58. SemIR::VarStorage{name_node, value_type_id, name_id});
  59. }
  60. context.AddNodeAndPush(
  61. parse_node,
  62. SemIR::BindName{name_node, value_type_id, name_id, value_id});
  63. break;
  64. }
  65. case Parse::NodeKind::ParameterListStart:
  66. // Parameters can have incomplete types in a function declaration, but not
  67. // in a function definition. We don't know which kind we have here.
  68. context.AddNodeAndPush(
  69. parse_node, SemIR::Parameter{name_node, cast_type_id, name_id});
  70. // TODO: Create a `BindName` node.
  71. break;
  72. case Parse::NodeKind::LetIntroducer:
  73. if (!context.TryToCompleteType(cast_type_id, [&] {
  74. CARBON_DIAGNOSTIC(IncompleteTypeInLetDeclaration, Error,
  75. "`let` binding has incomplete type `{0}`.",
  76. std::string);
  77. return context.emitter().Build(
  78. type_node_copy, IncompleteTypeInLetDeclaration,
  79. context.sem_ir().StringifyType(cast_type_id, true));
  80. })) {
  81. cast_type_id = SemIR::TypeId::Error;
  82. }
  83. // Create the node, but don't add it to a block until after we've formed
  84. // its initializer.
  85. // TODO: For general pattern parsing, we'll need to create a block to hold
  86. // the `let` pattern before we see the initializer.
  87. context.node_stack().Push(
  88. parse_node,
  89. context.nodes().AddInNoBlock(SemIR::BindName{
  90. name_node, cast_type_id, name_id, SemIR::NodeId::Invalid}));
  91. break;
  92. default:
  93. CARBON_FATAL() << "Found a pattern binding in unexpected context "
  94. << context_parse_node_kind;
  95. }
  96. return true;
  97. }
  98. auto HandleTemplate(Context& context, Parse::Node parse_node) -> bool {
  99. return context.TODO(parse_node, "HandleTemplate");
  100. }
  101. } // namespace Carbon::Check