handle_pattern_binding.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 = context.AddNode(
  52. SemIR::Field{parse_node, value_type_id, name_id,
  53. SemIR::MemberIndex(context.args_type_info_stack()
  54. .PeekCurrentBlockContents()
  55. .size())});
  56. // Add a corresponding field to the object representation of the class.
  57. context.args_type_info_stack().AddNode(
  58. SemIR::StructTypeField{parse_node, name_id, cast_type_id});
  59. } else {
  60. value_id = context.AddNode(
  61. SemIR::VarStorage{name_node, value_type_id, name_id});
  62. }
  63. context.AddNodeAndPush(
  64. parse_node,
  65. SemIR::BindName{name_node, value_type_id, name_id, value_id});
  66. break;
  67. }
  68. case Parse::NodeKind::ParameterListStart:
  69. // Parameters can have incomplete types in a function declaration, but not
  70. // in a function definition. We don't know which kind we have here.
  71. context.AddNodeAndPush(
  72. parse_node, SemIR::Parameter{name_node, cast_type_id, name_id});
  73. // TODO: Create a `BindName` node.
  74. break;
  75. case Parse::NodeKind::LetIntroducer:
  76. if (!context.TryToCompleteType(cast_type_id, [&] {
  77. CARBON_DIAGNOSTIC(IncompleteTypeInLetDeclaration, Error,
  78. "`let` binding has incomplete type `{0}`.",
  79. std::string);
  80. return context.emitter().Build(
  81. type_node_copy, IncompleteTypeInLetDeclaration,
  82. context.sem_ir().StringifyType(cast_type_id, true));
  83. })) {
  84. cast_type_id = SemIR::TypeId::Error;
  85. }
  86. // Create the node, but don't add it to a block until after we've formed
  87. // its initializer.
  88. // TODO: For general pattern parsing, we'll need to create a block to hold
  89. // the `let` pattern before we see the initializer.
  90. context.node_stack().Push(
  91. parse_node,
  92. context.nodes().AddInNoBlock(SemIR::BindName{
  93. name_node, cast_type_id, name_id, SemIR::NodeId::Invalid}));
  94. break;
  95. default:
  96. CARBON_FATAL() << "Found a pattern binding in unexpected context "
  97. << context_parse_node_kind;
  98. }
  99. return true;
  100. }
  101. auto HandleTemplate(Context& context, Parse::Node parse_node) -> bool {
  102. return context.TODO(parse_node, "HandleTemplate");
  103. }
  104. } // namespace Carbon::Check