handle_let.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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_introducer_state_stack().Push<Lex::TokenKind::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. auto introducer =
  76. context.decl_introducer_state_stack().Pop<Lex::TokenKind::Let>();
  77. CheckAccessModifiersOnDecl(context, introducer, parent_scope_inst);
  78. RequireDefaultFinalOnlyInInterfaces(context, introducer, parent_scope_inst);
  79. LimitModifiersOnDecl(
  80. context, introducer,
  81. KeywordModifierSet::Access | KeywordModifierSet::Interface);
  82. if (introducer.modifier_set.HasAnyOf(KeywordModifierSet::Access)) {
  83. context.TODO(introducer.modifier_node_id(ModifierOrder::Access),
  84. "access modifier");
  85. }
  86. if (introducer.modifier_set.HasAnyOf(KeywordModifierSet::Interface)) {
  87. context.TODO(introducer.modifier_node_id(ModifierOrder::Decl),
  88. "interface modifier");
  89. }
  90. auto pattern = context.insts().GetWithLocId(pattern_id);
  91. auto interface_scope = context.GetCurrentScopeAs<SemIR::InterfaceDecl>();
  92. if (value_id) {
  93. // Convert the value to match the type of the pattern.
  94. value_id = ConvertToValueOfType(context, node_id, *value_id,
  95. pattern.inst.type_id());
  96. }
  97. // At interface scope, we are forming an associated constant, which has
  98. // different rules.
  99. if (interface_scope) {
  100. BuildAssociatedConstantDecl(context, node_id, pattern_id, pattern,
  101. interface_scope->interface_id);
  102. return true;
  103. }
  104. if (!value_id) {
  105. CARBON_DIAGNOSTIC(
  106. ExpectedInitializerAfterLet, Error,
  107. "Expected `=`; `let` declaration must have an initializer.");
  108. context.emitter().Emit(TokenOnly(node_id), 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. context.ReplaceInstBeforeConstantUse(pattern_id, bind_name);
  119. context.inst_block_stack().AddInstId(pattern_id);
  120. // Add the name of the binding to the current scope.
  121. auto name_id = context.bind_names().Get(bind_name.bind_name_id).name_id;
  122. context.AddNameToLookup(name_id, pattern_id);
  123. if (parent_scope_inst_id == SemIR::InstId::PackageNamespace) {
  124. context.AddExport(pattern_id);
  125. }
  126. return true;
  127. }
  128. } // namespace Carbon::Check