handle_binding_pattern.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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/check/return.h"
  7. #include "toolchain/sem_ir/inst.h"
  8. namespace Carbon::Check {
  9. auto HandleAddress(Context& context, Parse::NodeId parse_node) -> bool {
  10. auto self_param_id =
  11. context.node_stack().Peek<Parse::NodeKind::BindingPattern>();
  12. if (auto self_param =
  13. context.insts().Get(self_param_id).TryAs<SemIR::SelfParam>()) {
  14. self_param->is_addr_self = SemIR::BoolValue::True;
  15. context.insts().Set(self_param_id, *self_param);
  16. } else {
  17. CARBON_DIAGNOSTIC(AddrOnNonSelfParam, Error,
  18. "`addr` can only be applied to a `self` parameter.");
  19. context.emitter().Emit(parse_node, AddrOnNonSelfParam);
  20. }
  21. return true;
  22. }
  23. auto HandleGenericBindingPattern(Context& context, Parse::NodeId parse_node)
  24. -> bool {
  25. return context.TODO(parse_node, "GenericBindingPattern");
  26. }
  27. auto HandleBindingPattern(Context& context, Parse::NodeId parse_node) -> bool {
  28. auto [type_node, parsed_type_id] =
  29. context.node_stack().PopExprWithParseNode();
  30. auto type_node_copy = type_node;
  31. auto cast_type_id = ExprAsType(context, type_node, parsed_type_id);
  32. // A `self` binding doesn't have a name.
  33. if (auto self_node =
  34. context.node_stack()
  35. .PopForSoloParseNodeIf<Parse::NodeKind::SelfValueName>()) {
  36. if (context.parse_tree().node_kind(context.node_stack().PeekParseNode()) !=
  37. Parse::NodeKind::ImplicitParamListStart) {
  38. CARBON_DIAGNOSTIC(
  39. SelfOutsideImplicitParamList, Error,
  40. "`self` can only be declared in an implicit parameter list.");
  41. context.emitter().Emit(parse_node, SelfOutsideImplicitParamList);
  42. }
  43. context.AddInstAndPush(
  44. parse_node, SemIR::SelfParam{*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 an instruction of the appropriate kind, linked to the name for
  53. // error locations.
  54. switch (auto context_parse_node_kind = context.parse_tree().node_kind(
  55. context.node_stack().PeekParseNode())) {
  56. case Parse::NodeKind::ReturnedModifier:
  57. case Parse::NodeKind::VariableIntroducer: {
  58. // A `var` declaration at class scope introduces a field.
  59. auto enclosing_class_decl = context.GetCurrentScopeAs<SemIR::ClassDecl>();
  60. if (!context.TryToCompleteType(cast_type_id, [&] {
  61. CARBON_DIAGNOSTIC(IncompleteTypeInVarDecl, Error,
  62. "{0} has incomplete type `{1}`.",
  63. llvm::StringLiteral, std::string);
  64. return context.emitter().Build(
  65. type_node_copy, IncompleteTypeInVarDecl,
  66. enclosing_class_decl ? llvm::StringLiteral("Field")
  67. : llvm::StringLiteral("Variable"),
  68. context.sem_ir().StringifyType(cast_type_id, true));
  69. })) {
  70. cast_type_id = SemIR::TypeId::Error;
  71. }
  72. SemIR::InstId value_id = SemIR::InstId::Invalid;
  73. SemIR::TypeId value_type_id = cast_type_id;
  74. if (context_parse_node_kind == Parse::NodeKind::ReturnedModifier) {
  75. CARBON_CHECK(!enclosing_class_decl) << "`returned var` at class scope";
  76. value_id =
  77. CheckReturnedVar(context, context.node_stack().PeekParseNode(),
  78. name_node, name_id, type_node, cast_type_id);
  79. } else if (enclosing_class_decl) {
  80. auto& class_info =
  81. context.classes().Get(enclosing_class_decl->class_id);
  82. auto field_type_inst_id = context.AddInst(SemIR::UnboundFieldType{
  83. parse_node, context.GetBuiltinType(SemIR::BuiltinKind::TypeType),
  84. class_info.self_type_id, cast_type_id});
  85. value_type_id = context.CanonicalizeType(field_type_inst_id);
  86. value_id = context.AddInst(
  87. SemIR::Field{parse_node, value_type_id, name_id,
  88. SemIR::MemberIndex(context.args_type_info_stack()
  89. .PeekCurrentBlockContents()
  90. .size())});
  91. // Add a corresponding field to the object representation of the class.
  92. context.args_type_info_stack().AddInst(
  93. SemIR::StructTypeField{parse_node, name_id, cast_type_id});
  94. } else {
  95. value_id = context.AddInst(
  96. SemIR::VarStorage{name_node, value_type_id, name_id});
  97. }
  98. auto bind_id = context.AddInst(
  99. SemIR::BindName{name_node, value_type_id, name_id, value_id});
  100. context.node_stack().Push(parse_node, bind_id);
  101. if (context_parse_node_kind == Parse::NodeKind::ReturnedModifier) {
  102. RegisterReturnedVar(context, bind_id);
  103. }
  104. break;
  105. }
  106. case Parse::NodeKind::ImplicitParamListStart:
  107. case Parse::NodeKind::ParamListStart:
  108. // Parameters can have incomplete types in a function declaration, but not
  109. // in a function definition. We don't know which kind we have here.
  110. context.AddInstAndPush(parse_node,
  111. SemIR::Param{name_node, cast_type_id, name_id});
  112. // TODO: Create a `BindName` instruction.
  113. break;
  114. case Parse::NodeKind::LetIntroducer:
  115. if (!context.TryToCompleteType(cast_type_id, [&] {
  116. CARBON_DIAGNOSTIC(IncompleteTypeInLetDecl, Error,
  117. "`let` binding has incomplete type `{0}`.",
  118. std::string);
  119. return context.emitter().Build(
  120. type_node_copy, IncompleteTypeInLetDecl,
  121. context.sem_ir().StringifyType(cast_type_id, true));
  122. })) {
  123. cast_type_id = SemIR::TypeId::Error;
  124. }
  125. // Create the instruction, but don't add it to a block until after we've
  126. // formed its initializer.
  127. // TODO: For general pattern parsing, we'll need to create a block to hold
  128. // the `let` pattern before we see the initializer.
  129. context.node_stack().Push(
  130. parse_node,
  131. context.insts().AddInNoBlock(SemIR::BindName{
  132. name_node, cast_type_id, name_id, SemIR::InstId::Invalid}));
  133. break;
  134. default:
  135. CARBON_FATAL() << "Found a pattern binding in unexpected context "
  136. << context_parse_node_kind;
  137. }
  138. return true;
  139. }
  140. auto HandleTemplate(Context& context, Parse::NodeId parse_node) -> bool {
  141. return context.TODO(parse_node, "HandleTemplate");
  142. }
  143. } // namespace Carbon::Check