handle_let.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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/interface.h"
  7. #include "toolchain/check/modifiers.h"
  8. #include "toolchain/diagnostics/diagnostic_emitter.h"
  9. #include "toolchain/sem_ir/inst.h"
  10. #include "toolchain/sem_ir/typed_insts.h"
  11. namespace Carbon::Check {
  12. auto HandleLetIntroducer(Context& context, Parse::LetIntroducerId parse_node)
  13. -> bool {
  14. context.decl_state_stack().Push(DeclState::Let);
  15. // Push a bracketing node to establish the pattern context.
  16. context.node_stack().Push(parse_node);
  17. return true;
  18. }
  19. auto HandleLetInitializer(Context& context, Parse::LetInitializerId parse_node)
  20. -> bool {
  21. context.node_stack().Push(parse_node);
  22. return true;
  23. }
  24. static auto BuildAssociatedConstantDecl(
  25. Context& context, Parse::LetDeclId parse_node, SemIR::InstId pattern_id,
  26. SemIR::ParseNodeAndInst pattern, SemIR::InterfaceId interface_id) -> void {
  27. auto& interface_info = context.interfaces().Get(interface_id);
  28. auto binding_pattern = pattern.inst.TryAs<SemIR::BindSymbolicName>();
  29. if (!binding_pattern) {
  30. CARBON_DIAGNOSTIC(ExpectedSymbolicBindingInAssociatedConstant, Error,
  31. "Pattern in associated constant declaration must be a "
  32. "single `:!` binding.");
  33. context.emitter().Emit(pattern.parse_node,
  34. ExpectedSymbolicBindingInAssociatedConstant);
  35. context.name_scopes().Get(interface_info.scope_id).has_error = true;
  36. return;
  37. }
  38. // Replace the tentative BindName instruction with the associated constant
  39. // declaration.
  40. auto name_id =
  41. context.bind_names().Get(binding_pattern->bind_name_id).name_id;
  42. context.ReplaceInstBeforeConstantUse(
  43. pattern_id, {parse_node, SemIR::AssociatedConstantDecl{
  44. binding_pattern->type_id, name_id}});
  45. auto decl_id = pattern_id;
  46. context.inst_block_stack().AddInstId(decl_id);
  47. // Add an associated entity name to the interface scope.
  48. auto assoc_id = BuildAssociatedEntity(context, interface_id, decl_id);
  49. auto name_context = context.decl_name_stack().MakeUnqualifiedName(
  50. pattern.parse_node, name_id);
  51. context.decl_name_stack().AddNameToLookup(name_context, assoc_id);
  52. }
  53. auto HandleLetDecl(Context& context, Parse::LetDeclId parse_node) -> bool {
  54. // Pop the optional initializer.
  55. std::optional<SemIR::InstId> value_id;
  56. if (context.node_stack().PeekNextIs<Parse::NodeKind::LetInitializer>()) {
  57. value_id = context.node_stack().PopExpr();
  58. context.node_stack()
  59. .PopAndDiscardSoloParseNode<Parse::NodeKind::LetInitializer>();
  60. }
  61. if (context.node_stack().PeekIs<Parse::NodeKind::TuplePattern>()) {
  62. return context.TODO(parse_node, "tuple pattern in let");
  63. }
  64. SemIR::InstId pattern_id = context.node_stack().PopPattern();
  65. context.node_stack()
  66. .PopAndDiscardSoloParseNode<Parse::NodeKind::LetIntroducer>();
  67. // Process declaration modifiers.
  68. // TODO: For a qualified `let` declaration, this should use the target scope
  69. // of the name introduced in the declaration. See #2590.
  70. CheckAccessModifiersOnDecl(context, Lex::TokenKind::Let,
  71. context.scope_stack().PeekNameScopeId());
  72. RequireDefaultFinalOnlyInInterfaces(context, Lex::TokenKind::Let,
  73. context.scope_stack().PeekNameScopeId());
  74. LimitModifiersOnDecl(
  75. context, KeywordModifierSet::Access | KeywordModifierSet::Interface,
  76. Lex::TokenKind::Let);
  77. auto modifiers = context.decl_state_stack().innermost().modifier_set;
  78. if (!!(modifiers & KeywordModifierSet::Access)) {
  79. context.TODO(context.decl_state_stack().innermost().saw_access_modifier,
  80. "access modifier");
  81. }
  82. if (!!(modifiers & KeywordModifierSet::Interface)) {
  83. context.TODO(context.decl_state_stack().innermost().saw_decl_modifier,
  84. "interface modifier");
  85. }
  86. context.decl_state_stack().Pop(DeclState::Let);
  87. auto pattern = context.insts().GetWithParseNode(pattern_id);
  88. auto interface_scope = context.GetCurrentScopeAs<SemIR::InterfaceDecl>();
  89. if (value_id) {
  90. // Convert the value to match the type of the pattern.
  91. value_id = ConvertToValueOfType(context, parse_node, *value_id,
  92. pattern.inst.type_id());
  93. }
  94. // At interface scope, we are forming an associated constant, which has
  95. // different rules.
  96. if (interface_scope) {
  97. BuildAssociatedConstantDecl(context, parse_node, pattern_id, pattern,
  98. interface_scope->interface_id);
  99. return true;
  100. }
  101. if (!value_id) {
  102. CARBON_DIAGNOSTIC(
  103. ExpectedInitializerAfterLet, Error,
  104. "Expected `=`; `let` declaration must have an initializer.");
  105. context.emitter().Emit(Parse::TokenOnly(parse_node),
  106. ExpectedInitializerAfterLet);
  107. value_id = SemIR::InstId::BuiltinError;
  108. }
  109. // Update the binding with its value and add it to the current block, after
  110. // the computation of the value.
  111. // TODO: Support other kinds of pattern here.
  112. auto bind_name = pattern.inst.As<SemIR::AnyBindName>();
  113. CARBON_CHECK(!bind_name.value_id.is_valid())
  114. << "Binding should not already have a value!";
  115. bind_name.value_id = *value_id;
  116. pattern.inst = bind_name;
  117. context.ReplaceInstBeforeConstantUse(pattern_id, pattern);
  118. context.inst_block_stack().AddInstId(pattern_id);
  119. // Add the name of the binding to the current scope.
  120. auto name_id = context.bind_names().Get(bind_name.bind_name_id).name_id;
  121. context.AddNameToLookup(name_id, pattern_id);
  122. if (context.scope_stack().PeekNameScopeId() == SemIR::NameScopeId::Package) {
  123. context.AddExport(pattern_id);
  124. }
  125. return true;
  126. }
  127. } // namespace Carbon::Check