handle_pattern_binding.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. auto self_param_id =
  10. context.node_stack().Peek<Parse::NodeKind::PatternBinding>();
  11. if (auto self_param =
  12. context.nodes().Get(self_param_id).TryAs<SemIR::SelfParameter>()) {
  13. self_param->is_addr_self = SemIR::BoolValue::True;
  14. context.nodes().Set(self_param_id, *self_param);
  15. } else {
  16. CARBON_DIAGNOSTIC(AddrOnNonSelfParameter, Error,
  17. "`addr` can only be applied to a `self` parameter");
  18. context.emitter().Emit(parse_node, AddrOnNonSelfParameter);
  19. }
  20. return true;
  21. }
  22. auto HandleGenericPatternBinding(Context& context, Parse::Node parse_node)
  23. -> bool {
  24. return context.TODO(parse_node, "GenericPatternBinding");
  25. }
  26. auto HandlePatternBinding(Context& context, Parse::Node parse_node) -> bool {
  27. auto [type_node, parsed_type_id] =
  28. context.node_stack().PopExpressionWithParseNode();
  29. auto type_node_copy = type_node;
  30. auto cast_type_id = ExpressionAsType(context, type_node, parsed_type_id);
  31. // A `self` binding doesn't have a name.
  32. if (auto self_node =
  33. context.node_stack()
  34. .PopForSoloParseNodeIf<Parse::NodeKind::SelfValueName>()) {
  35. if (context.parse_tree().node_kind(context.node_stack().PeekParseNode()) !=
  36. Parse::NodeKind::ImplicitParameterListStart) {
  37. CARBON_DIAGNOSTIC(
  38. SelfOutsideImplicitParameterList, Error,
  39. "`self` can only be declared in an implicit parameter list");
  40. context.emitter().Emit(parse_node, SelfOutsideImplicitParameterList);
  41. }
  42. context.AddNodeAndPush(
  43. parse_node,
  44. SemIR::SelfParameter{*self_node, cast_type_id,
  45. /*is_addr_self=*/SemIR::BoolValue::False});
  46. return true;
  47. }
  48. // TODO: Handle `_` bindings.
  49. // Every other kind of pattern binding has a name.
  50. auto [name_node, name_id] =
  51. context.node_stack().PopWithParseNode<Parse::NodeKind::Name>();
  52. // Allocate a node of the appropriate kind, linked to the name for error
  53. // locations.
  54. switch (auto context_parse_node_kind = context.parse_tree().node_kind(
  55. context.node_stack().PeekParseNode())) {
  56. case Parse::NodeKind::VariableIntroducer: {
  57. // A `var` declaration at class scope introduces a field.
  58. auto enclosing_class_decl =
  59. context.GetCurrentScopeAs<SemIR::ClassDeclaration>();
  60. if (!context.TryToCompleteType(cast_type_id, [&] {
  61. CARBON_DIAGNOSTIC(IncompleteTypeInVarDeclaration, Error,
  62. "{0} has incomplete type `{1}`.", llvm::StringRef,
  63. std::string);
  64. return context.emitter().Build(
  65. type_node_copy, IncompleteTypeInVarDeclaration,
  66. enclosing_class_decl ? "Field" : "Variable",
  67. context.sem_ir().StringifyType(cast_type_id, true));
  68. })) {
  69. cast_type_id = SemIR::TypeId::Error;
  70. }
  71. SemIR::NodeId value_id = SemIR::NodeId::Invalid;
  72. SemIR::TypeId value_type_id = cast_type_id;
  73. if (enclosing_class_decl) {
  74. auto& class_info =
  75. context.classes().Get(enclosing_class_decl->class_id);
  76. auto field_type_node_id = context.AddNode(SemIR::UnboundFieldType{
  77. parse_node, context.GetBuiltinType(SemIR::BuiltinKind::TypeType),
  78. class_info.self_type_id, cast_type_id});
  79. value_type_id = context.CanonicalizeType(field_type_node_id);
  80. value_id = context.AddNode(
  81. SemIR::Field{parse_node, value_type_id, name_id,
  82. SemIR::MemberIndex(context.args_type_info_stack()
  83. .PeekCurrentBlockContents()
  84. .size())});
  85. // Add a corresponding field to the object representation of the class.
  86. context.args_type_info_stack().AddNode(
  87. SemIR::StructTypeField{parse_node, name_id, cast_type_id});
  88. } else {
  89. value_id = context.AddNode(
  90. SemIR::VarStorage{name_node, value_type_id, name_id});
  91. }
  92. context.AddNodeAndPush(
  93. parse_node,
  94. SemIR::BindName{name_node, value_type_id, name_id, value_id});
  95. break;
  96. }
  97. case Parse::NodeKind::ImplicitParameterListStart:
  98. case Parse::NodeKind::ParameterListStart:
  99. // Parameters can have incomplete types in a function declaration, but not
  100. // in a function definition. We don't know which kind we have here.
  101. context.AddNodeAndPush(
  102. parse_node, SemIR::Parameter{name_node, cast_type_id, name_id});
  103. // TODO: Create a `BindName` node.
  104. break;
  105. case Parse::NodeKind::LetIntroducer:
  106. if (!context.TryToCompleteType(cast_type_id, [&] {
  107. CARBON_DIAGNOSTIC(IncompleteTypeInLetDeclaration, Error,
  108. "`let` binding has incomplete type `{0}`.",
  109. std::string);
  110. return context.emitter().Build(
  111. type_node_copy, IncompleteTypeInLetDeclaration,
  112. context.sem_ir().StringifyType(cast_type_id, true));
  113. })) {
  114. cast_type_id = SemIR::TypeId::Error;
  115. }
  116. // Create the node, but don't add it to a block until after we've formed
  117. // its initializer.
  118. // TODO: For general pattern parsing, we'll need to create a block to hold
  119. // the `let` pattern before we see the initializer.
  120. context.node_stack().Push(
  121. parse_node,
  122. context.nodes().AddInNoBlock(SemIR::BindName{
  123. name_node, cast_type_id, name_id, SemIR::NodeId::Invalid}));
  124. break;
  125. default:
  126. CARBON_FATAL() << "Found a pattern binding in unexpected context "
  127. << context_parse_node_kind;
  128. }
  129. return true;
  130. }
  131. auto HandleTemplate(Context& context, Parse::Node parse_node) -> bool {
  132. return context.TODO(parse_node, "HandleTemplate");
  133. }
  134. } // namespace Carbon::Check