handle_binding_pattern.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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] = context.node_stack().PopNameWithParseNode();
  51. // Allocate an instruction of the appropriate kind, linked to the name for
  52. // error locations.
  53. // TODO: The node stack is a fragile way of getting context information.
  54. // Get this information from somewhere else.
  55. switch (auto context_parse_node_kind = context.parse_tree().node_kind(
  56. context.node_stack().PeekParseNode())) {
  57. case Parse::NodeKind::ReturnedModifier:
  58. case Parse::NodeKind::VariableIntroducer: {
  59. // A `var` declaration at class scope introduces a field.
  60. auto enclosing_class_decl = context.GetCurrentScopeAs<SemIR::ClassDecl>();
  61. if (!context.TryToCompleteType(cast_type_id, [&] {
  62. CARBON_DIAGNOSTIC(IncompleteTypeInVarDecl, Error,
  63. "{0} has incomplete type `{1}`.",
  64. llvm::StringLiteral, std::string);
  65. return context.emitter().Build(
  66. type_node_copy, IncompleteTypeInVarDecl,
  67. enclosing_class_decl ? llvm::StringLiteral("Field")
  68. : llvm::StringLiteral("Variable"),
  69. context.sem_ir().StringifyType(cast_type_id));
  70. })) {
  71. cast_type_id = SemIR::TypeId::Error;
  72. }
  73. SemIR::InstId value_id = SemIR::InstId::Invalid;
  74. SemIR::TypeId value_type_id = cast_type_id;
  75. if (context_parse_node_kind == Parse::NodeKind::ReturnedModifier) {
  76. CARBON_CHECK(!enclosing_class_decl) << "`returned var` at class scope";
  77. value_id =
  78. CheckReturnedVar(context, context.node_stack().PeekParseNode(),
  79. name_node, name_id, type_node, cast_type_id);
  80. } else if (enclosing_class_decl) {
  81. auto& class_info =
  82. context.classes().Get(enclosing_class_decl->class_id);
  83. auto field_type_inst_id = context.AddInst(SemIR::UnboundElementType{
  84. parse_node, context.GetBuiltinType(SemIR::BuiltinKind::TypeType),
  85. class_info.self_type_id, cast_type_id});
  86. value_type_id = context.CanonicalizeType(field_type_inst_id);
  87. value_id = context.AddInst(
  88. SemIR::FieldDecl{parse_node, value_type_id, name_id,
  89. SemIR::ElementIndex(context.args_type_info_stack()
  90. .PeekCurrentBlockContents()
  91. .size())});
  92. // Add a corresponding field to the object representation of the class.
  93. context.args_type_info_stack().AddInst(
  94. SemIR::StructTypeField{parse_node, name_id, cast_type_id});
  95. } else {
  96. value_id = context.AddInst(
  97. SemIR::VarStorage{name_node, value_type_id, name_id});
  98. }
  99. auto bind_id = context.AddInst(
  100. SemIR::BindName{name_node, value_type_id, name_id, value_id});
  101. context.node_stack().Push(parse_node, bind_id);
  102. if (context_parse_node_kind == Parse::NodeKind::ReturnedModifier) {
  103. RegisterReturnedVar(context, bind_id);
  104. }
  105. break;
  106. }
  107. case Parse::NodeKind::ImplicitParamListStart:
  108. case Parse::NodeKind::ParamListStart:
  109. // Parameters can have incomplete types in a function declaration, but not
  110. // in a function definition. We don't know which kind we have here.
  111. context.AddInstAndPush(parse_node,
  112. SemIR::Param{name_node, cast_type_id, name_id});
  113. // TODO: Create a `BindName` instruction.
  114. break;
  115. case Parse::NodeKind::LetIntroducer:
  116. if (!context.TryToCompleteType(cast_type_id, [&] {
  117. CARBON_DIAGNOSTIC(IncompleteTypeInLetDecl, Error,
  118. "`let` binding has incomplete type `{0}`.",
  119. std::string);
  120. return context.emitter().Build(
  121. type_node_copy, IncompleteTypeInLetDecl,
  122. context.sem_ir().StringifyType(cast_type_id));
  123. })) {
  124. cast_type_id = SemIR::TypeId::Error;
  125. }
  126. // Create the instruction, but don't add it to a block until after we've
  127. // formed its initializer.
  128. // TODO: For general pattern parsing, we'll need to create a block to hold
  129. // the `let` pattern before we see the initializer.
  130. context.node_stack().Push(
  131. parse_node,
  132. context.insts().AddInNoBlock(SemIR::BindName{
  133. name_node, cast_type_id, name_id, SemIR::InstId::Invalid}));
  134. break;
  135. default:
  136. CARBON_FATAL() << "Found a pattern binding in unexpected context "
  137. << context_parse_node_kind;
  138. }
  139. return true;
  140. }
  141. auto HandleTemplate(Context& context, Parse::NodeId parse_node) -> bool {
  142. return context.TODO(parse_node, "HandleTemplate");
  143. }
  144. } // namespace Carbon::Check