handle_binding_pattern.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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().Pop<Parse::NodeKind::BindingPattern>();
  12. auto self_param = context.insts().TryGetAs<SemIR::Param>(self_param_id);
  13. if (self_param && self_param->name_id == SemIR::NameId::SelfValue) {
  14. // TODO: The type of an `addr_pattern` should probably be the non-pointer
  15. // type, because that's the type that the pattern matches.
  16. context.AddInstAndPush(
  17. parse_node,
  18. SemIR::AddrPattern{parse_node, self_param->type_id, self_param_id});
  19. } else {
  20. CARBON_DIAGNOSTIC(AddrOnNonSelfParam, Error,
  21. "`addr` can only be applied to a `self` parameter.");
  22. context.emitter().Emit(TokenOnly(parse_node), AddrOnNonSelfParam);
  23. context.node_stack().Push(parse_node, self_param_id);
  24. }
  25. return true;
  26. }
  27. auto HandleGenericBindingPattern(Context& context, Parse::NodeId parse_node)
  28. -> bool {
  29. return context.TODO(parse_node, "GenericBindingPattern");
  30. }
  31. auto HandleBindingPattern(Context& context, Parse::NodeId parse_node) -> bool {
  32. auto [type_node, parsed_type_id] =
  33. context.node_stack().PopExprWithParseNode();
  34. auto type_node_copy = type_node;
  35. auto cast_type_id = ExprAsType(context, type_node, parsed_type_id);
  36. // TODO: Handle `_` bindings.
  37. // Every other kind of pattern binding has a name.
  38. auto [name_node, name_id] = context.node_stack().PopNameWithParseNode();
  39. // A `self` binding can only appear in an implicit parameter list.
  40. if (name_id == SemIR::NameId::SelfValue &&
  41. !context.node_stack().PeekIs<Parse::NodeKind::ImplicitParamListStart>()) {
  42. CARBON_DIAGNOSTIC(
  43. SelfOutsideImplicitParamList, Error,
  44. "`self` can only be declared in an implicit parameter list.");
  45. context.emitter().Emit(parse_node, SelfOutsideImplicitParamList);
  46. }
  47. // Allocate an instruction of the appropriate kind, linked to the name for
  48. // error locations.
  49. // TODO: The node stack is a fragile way of getting context information.
  50. // Get this information from somewhere else.
  51. switch (auto context_parse_node_kind = context.parse_tree().node_kind(
  52. context.node_stack().PeekParseNode())) {
  53. case Parse::NodeKind::ReturnedModifier:
  54. case Parse::NodeKind::VariableIntroducer: {
  55. // A `var` declaration at class scope introduces a field.
  56. auto enclosing_class_decl = context.GetCurrentScopeAs<SemIR::ClassDecl>();
  57. cast_type_id = context.AsCompleteType(cast_type_id, [&] {
  58. CARBON_DIAGNOSTIC(IncompleteTypeInVarDecl, Error,
  59. "{0} has incomplete type `{1}`.", llvm::StringLiteral,
  60. std::string);
  61. return context.emitter().Build(
  62. type_node_copy, IncompleteTypeInVarDecl,
  63. enclosing_class_decl ? llvm::StringLiteral("Field")
  64. : llvm::StringLiteral("Variable"),
  65. context.sem_ir().StringifyType(cast_type_id));
  66. });
  67. SemIR::InstId value_id = SemIR::InstId::Invalid;
  68. SemIR::TypeId value_type_id = cast_type_id;
  69. if (context_parse_node_kind == Parse::NodeKind::ReturnedModifier) {
  70. CARBON_CHECK(!enclosing_class_decl) << "`returned var` at class scope";
  71. value_id =
  72. CheckReturnedVar(context, context.node_stack().PeekParseNode(),
  73. name_node, name_id, type_node, cast_type_id);
  74. } else if (enclosing_class_decl) {
  75. auto& class_info =
  76. context.classes().Get(enclosing_class_decl->class_id);
  77. auto field_type_inst_id = context.AddInst(SemIR::UnboundElementType{
  78. parse_node, context.GetBuiltinType(SemIR::BuiltinKind::TypeType),
  79. class_info.self_type_id, cast_type_id});
  80. value_type_id = context.CanonicalizeType(field_type_inst_id);
  81. value_id = context.AddInst(
  82. SemIR::FieldDecl{parse_node, value_type_id, name_id,
  83. SemIR::ElementIndex(context.args_type_info_stack()
  84. .PeekCurrentBlockContents()
  85. .size())});
  86. // Add a corresponding field to the object representation of the class.
  87. context.args_type_info_stack().AddInst(
  88. SemIR::StructTypeField{parse_node, name_id, cast_type_id});
  89. } else {
  90. value_id = context.AddInst(
  91. SemIR::VarStorage{name_node, value_type_id, name_id});
  92. }
  93. auto bind_id = context.AddInst(
  94. SemIR::BindName{name_node, value_type_id, name_id, value_id});
  95. context.node_stack().Push(parse_node, bind_id);
  96. if (context_parse_node_kind == Parse::NodeKind::ReturnedModifier) {
  97. RegisterReturnedVar(context, bind_id);
  98. }
  99. break;
  100. }
  101. case Parse::NodeKind::ImplicitParamListStart:
  102. case Parse::NodeKind::TuplePatternStart:
  103. // Parameters can have incomplete types in a function declaration, but not
  104. // in a function definition. We don't know which kind we have here.
  105. // TODO: A tuple pattern can appear in other places than function
  106. // parameters.
  107. context.AddInstAndPush(parse_node,
  108. SemIR::Param{name_node, cast_type_id, name_id});
  109. // TODO: Create a `BindName` instruction.
  110. break;
  111. case Parse::NodeKind::LetIntroducer:
  112. cast_type_id = context.AsCompleteType(cast_type_id, [&] {
  113. CARBON_DIAGNOSTIC(IncompleteTypeInLetDecl, Error,
  114. "`let` binding has incomplete type `{0}`.",
  115. std::string);
  116. return context.emitter().Build(
  117. type_node_copy, IncompleteTypeInLetDecl,
  118. context.sem_ir().StringifyType(cast_type_id));
  119. });
  120. // Create the instruction, but don't add it to a block until after we've
  121. // formed its initializer.
  122. // TODO: For general pattern parsing, we'll need to create a block to hold
  123. // the `let` pattern before we see the initializer.
  124. context.node_stack().Push(
  125. parse_node,
  126. context.insts().AddInNoBlock(SemIR::BindName{
  127. name_node, cast_type_id, name_id, SemIR::InstId::Invalid}));
  128. break;
  129. default:
  130. CARBON_FATAL() << "Found a pattern binding in unexpected context "
  131. << context_parse_node_kind;
  132. }
  133. return true;
  134. }
  135. auto HandleTemplate(Context& context, Parse::NodeId parse_node) -> bool {
  136. // TODO: diagnose if this occurs in a `var` context.
  137. return context.TODO(parse_node, "HandleTemplate");
  138. }
  139. } // namespace Carbon::Check