handle_binding_pattern.cpp 6.7 KB

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