handle_let.cpp 5.9 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/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 node_id)
  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(node_id);
  17. return true;
  18. }
  19. auto HandleLetInitializer(Context& context, Parse::LetInitializerId node_id)
  20. -> bool {
  21. context.node_stack().Push(node_id);
  22. return true;
  23. }
  24. static auto BuildAssociatedConstantDecl(
  25. Context& context, Parse::LetDeclId node_id, SemIR::InstId pattern_id,
  26. SemIR::LocIdAndInst 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.loc_id,
  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.ReplaceLocIdAndInstBeforeConstantUse(
  43. pattern_id, {node_id, 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 =
  50. context.decl_name_stack().MakeUnqualifiedName(pattern.loc_id, name_id);
  51. context.decl_name_stack().AddNameOrDiagnoseDuplicate(name_context, assoc_id);
  52. }
  53. auto HandleLetDecl(Context& context, Parse::LetDeclId node_id) -> 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. .PopAndDiscardSoloNodeId<Parse::NodeKind::LetInitializer>();
  60. }
  61. if (context.node_stack().PeekIs<Parse::NodeKind::TuplePattern>()) {
  62. return context.TODO(node_id, "tuple pattern in let");
  63. }
  64. SemIR::InstId pattern_id = context.node_stack().PopPattern();
  65. context.node_stack()
  66. .PopAndDiscardSoloNodeId<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. auto [enclosing_scope_inst_id, enclosing_scope_inst] =
  71. context.name_scopes().GetInstIfValid(
  72. context.scope_stack().PeekNameScopeId());
  73. CheckAccessModifiersOnDecl(context, Lex::TokenKind::Let,
  74. enclosing_scope_inst);
  75. RequireDefaultFinalOnlyInInterfaces(context, Lex::TokenKind::Let,
  76. enclosing_scope_inst);
  77. LimitModifiersOnDecl(
  78. context, KeywordModifierSet::Access | KeywordModifierSet::Interface,
  79. Lex::TokenKind::Let);
  80. auto modifiers = context.decl_state_stack().innermost().modifier_set;
  81. if (modifiers.HasAnyOf(KeywordModifierSet::Access)) {
  82. context.TODO(context.decl_state_stack().innermost().modifier_node_id(
  83. ModifierOrder::Access),
  84. "access modifier");
  85. }
  86. if (modifiers.HasAnyOf(KeywordModifierSet::Interface)) {
  87. context.TODO(context.decl_state_stack().innermost().modifier_node_id(
  88. ModifierOrder::Decl),
  89. "interface modifier");
  90. }
  91. context.decl_state_stack().Pop(DeclState::Let);
  92. auto pattern = context.insts().GetWithLocId(pattern_id);
  93. auto interface_scope = context.GetCurrentScopeAs<SemIR::InterfaceDecl>();
  94. if (value_id) {
  95. // Convert the value to match the type of the pattern.
  96. value_id = ConvertToValueOfType(context, node_id, *value_id,
  97. pattern.inst.type_id());
  98. }
  99. // At interface scope, we are forming an associated constant, which has
  100. // different rules.
  101. if (interface_scope) {
  102. BuildAssociatedConstantDecl(context, node_id, pattern_id, pattern,
  103. interface_scope->interface_id);
  104. return true;
  105. }
  106. if (!value_id) {
  107. CARBON_DIAGNOSTIC(
  108. ExpectedInitializerAfterLet, Error,
  109. "Expected `=`; `let` declaration must have an initializer.");
  110. context.emitter().Emit(TokenOnly(node_id), ExpectedInitializerAfterLet);
  111. value_id = SemIR::InstId::BuiltinError;
  112. }
  113. // Update the binding with its value and add it to the current block, after
  114. // the computation of the value.
  115. // TODO: Support other kinds of pattern here.
  116. auto bind_name = pattern.inst.As<SemIR::AnyBindName>();
  117. CARBON_CHECK(!bind_name.value_id.is_valid())
  118. << "Binding should not already have a value!";
  119. bind_name.value_id = *value_id;
  120. context.ReplaceInstBeforeConstantUse(pattern_id, bind_name);
  121. context.inst_block_stack().AddInstId(pattern_id);
  122. // Add the name of the binding to the current scope.
  123. auto name_id = context.bind_names().Get(bind_name.bind_name_id).name_id;
  124. context.AddNameToLookup(name_id, pattern_id);
  125. if (enclosing_scope_inst_id == SemIR::InstId::PackageNamespace) {
  126. context.AddExport(pattern_id);
  127. }
  128. return true;
  129. }
  130. } // namespace Carbon::Check