handle_let.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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::NodeIdAndInst 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.node_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.ReplaceInstBeforeConstantUse(
  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.node_id, name_id);
  51. context.decl_name_stack().AddNameToLookup(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. 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().modifier_node_id(
  80. ModifierOrder::Access),
  81. "access modifier");
  82. }
  83. if (!!(modifiers & KeywordModifierSet::Interface)) {
  84. context.TODO(context.decl_state_stack().innermost().modifier_node_id(
  85. ModifierOrder::Decl),
  86. "interface modifier");
  87. }
  88. context.decl_state_stack().Pop(DeclState::Let);
  89. auto pattern = context.insts().GetWithNodeId(pattern_id);
  90. auto interface_scope = context.GetCurrentScopeAs<SemIR::InterfaceDecl>();
  91. if (value_id) {
  92. // Convert the value to match the type of the pattern.
  93. value_id = ConvertToValueOfType(context, node_id, *value_id,
  94. pattern.inst.type_id());
  95. }
  96. // At interface scope, we are forming an associated constant, which has
  97. // different rules.
  98. if (interface_scope) {
  99. BuildAssociatedConstantDecl(context, node_id, pattern_id, pattern,
  100. interface_scope->interface_id);
  101. return true;
  102. }
  103. if (!value_id) {
  104. CARBON_DIAGNOSTIC(
  105. ExpectedInitializerAfterLet, Error,
  106. "Expected `=`; `let` declaration must have an initializer.");
  107. context.emitter().Emit(Parse::TokenOnly(node_id),
  108. ExpectedInitializerAfterLet);
  109. value_id = SemIR::InstId::BuiltinError;
  110. }
  111. // Update the binding with its value and add it to the current block, after
  112. // the computation of the value.
  113. // TODO: Support other kinds of pattern here.
  114. auto bind_name = pattern.inst.As<SemIR::AnyBindName>();
  115. CARBON_CHECK(!bind_name.value_id.is_valid())
  116. << "Binding should not already have a value!";
  117. bind_name.value_id = *value_id;
  118. pattern.inst = bind_name;
  119. context.ReplaceInstBeforeConstantUse(pattern_id, pattern);
  120. context.inst_block_stack().AddInstId(pattern_id);
  121. // Add the name of the binding to the current scope.
  122. auto name_id = context.bind_names().Get(bind_name.bind_name_id).name_id;
  123. context.AddNameToLookup(name_id, pattern_id);
  124. if (context.scope_stack().PeekNameScopeId() == SemIR::NameScopeId::Package) {
  125. context.AddExport(pattern_id);
  126. }
  127. return true;
  128. }
  129. } // namespace Carbon::Check