handle_pattern_binding.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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/inst.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.insts().Get(self_param_id).TryAs<SemIR::SelfParameter>()) {
  13. self_param->is_addr_self = SemIR::BoolValue::True;
  14. context.insts().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().PopExprWithParseNode();
  29. auto type_node_copy = type_node;
  30. auto cast_type_id = ExprAsType(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.AddInstAndPush(
  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 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::VariableIntroducer: {
  57. // A `var` declaration at class scope introduces a field.
  58. auto enclosing_class_decl = context.GetCurrentScopeAs<SemIR::ClassDecl>();
  59. if (!context.TryToCompleteType(cast_type_id, [&] {
  60. CARBON_DIAGNOSTIC(IncompleteTypeInVarDecl, Error,
  61. "{0} has incomplete type `{1}`.", llvm::StringRef,
  62. std::string);
  63. return context.emitter().Build(
  64. type_node_copy, IncompleteTypeInVarDecl,
  65. enclosing_class_decl ? "Field" : "Variable",
  66. context.sem_ir().StringifyType(cast_type_id, true));
  67. })) {
  68. cast_type_id = SemIR::TypeId::Error;
  69. }
  70. SemIR::InstId value_id = SemIR::InstId::Invalid;
  71. SemIR::TypeId value_type_id = cast_type_id;
  72. if (enclosing_class_decl) {
  73. auto& class_info =
  74. context.classes().Get(enclosing_class_decl->class_id);
  75. auto field_type_inst_id = context.AddInst(SemIR::UnboundFieldType{
  76. parse_node, context.GetBuiltinType(SemIR::BuiltinKind::TypeType),
  77. class_info.self_type_id, cast_type_id});
  78. value_type_id = context.CanonicalizeType(field_type_inst_id);
  79. value_id = context.AddInst(
  80. SemIR::Field{parse_node, value_type_id, name_id,
  81. SemIR::MemberIndex(context.args_type_info_stack()
  82. .PeekCurrentBlockContents()
  83. .size())});
  84. // Add a corresponding field to the object representation of the class.
  85. context.args_type_info_stack().AddInst(
  86. SemIR::StructTypeField{parse_node, name_id, cast_type_id});
  87. } else {
  88. value_id = context.AddInst(
  89. SemIR::VarStorage{name_node, value_type_id, name_id});
  90. }
  91. context.AddInstAndPush(
  92. parse_node,
  93. SemIR::BindName{name_node, value_type_id, name_id, value_id});
  94. break;
  95. }
  96. case Parse::NodeKind::ImplicitParameterListStart:
  97. case Parse::NodeKind::ParameterListStart:
  98. // Parameters can have incomplete types in a function declaration, but not
  99. // in a function definition. We don't know which kind we have here.
  100. context.AddInstAndPush(
  101. parse_node, SemIR::Parameter{name_node, cast_type_id, name_id});
  102. // TODO: Create a `BindName` instruction.
  103. break;
  104. case Parse::NodeKind::LetIntroducer:
  105. if (!context.TryToCompleteType(cast_type_id, [&] {
  106. CARBON_DIAGNOSTIC(IncompleteTypeInLetDecl, Error,
  107. "`let` binding has incomplete type `{0}`.",
  108. std::string);
  109. return context.emitter().Build(
  110. type_node_copy, IncompleteTypeInLetDecl,
  111. context.sem_ir().StringifyType(cast_type_id, true));
  112. })) {
  113. cast_type_id = SemIR::TypeId::Error;
  114. }
  115. // Create the instruction, but don't add it to a block until after we've
  116. // formed its initializer.
  117. // TODO: For general pattern parsing, we'll need to create a block to hold
  118. // the `let` pattern before we see the initializer.
  119. context.node_stack().Push(
  120. parse_node,
  121. context.insts().AddInNoBlock(SemIR::BindName{
  122. name_node, cast_type_id, name_id, SemIR::InstId::Invalid}));
  123. break;
  124. default:
  125. CARBON_FATAL() << "Found a pattern binding in unexpected context "
  126. << context_parse_node_kind;
  127. }
  128. return true;
  129. }
  130. auto HandleTemplate(Context& context, Parse::Node parse_node) -> bool {
  131. return context.TODO(parse_node, "HandleTemplate");
  132. }
  133. } // namespace Carbon::Check