handle_let.cpp 5.9 KB

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