handle_let_and_var.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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/decl_introducer_state.h"
  7. #include "toolchain/check/handle.h"
  8. #include "toolchain/check/interface.h"
  9. #include "toolchain/check/modifiers.h"
  10. #include "toolchain/diagnostics/diagnostic_emitter.h"
  11. #include "toolchain/lex/token_kind.h"
  12. #include "toolchain/sem_ir/inst.h"
  13. #include "toolchain/sem_ir/name_scope.h"
  14. #include "toolchain/sem_ir/typed_insts.h"
  15. namespace Carbon::Check {
  16. template <Lex::TokenKind::RawEnumType Kind>
  17. static auto HandleIntroducer(Context& context, Parse::NodeId node_id) -> bool {
  18. context.decl_introducer_state_stack().Push<Kind>();
  19. // Push a bracketing node and pattern block to establish the pattern context.
  20. context.node_stack().Push(node_id);
  21. context.pattern_block_stack().Push();
  22. context.BeginSubpattern();
  23. return true;
  24. }
  25. auto HandleParseNode(Context& context, Parse::LetIntroducerId node_id) -> bool {
  26. return HandleIntroducer<Lex::TokenKind::Let>(context, node_id);
  27. }
  28. auto HandleParseNode(Context& context, Parse::VariableIntroducerId node_id)
  29. -> bool {
  30. return HandleIntroducer<Lex::TokenKind::Var>(context, node_id);
  31. }
  32. auto HandleParseNode(Context& context, Parse::ReturnedModifierId node_id)
  33. -> bool {
  34. // This is pushed to be seen by HandleBindingPattern.
  35. context.node_stack().Push(node_id);
  36. return true;
  37. }
  38. static auto HandleInitializer(Context& context, Parse::NodeId node_id) -> bool {
  39. if (context.scope_stack().PeekIndex() == ScopeIndex::Package) {
  40. context.global_init().Resume();
  41. }
  42. context.node_stack().Push(node_id);
  43. return true;
  44. }
  45. auto HandleParseNode(Context& context, Parse::LetInitializerId node_id)
  46. -> bool {
  47. return HandleInitializer(context, node_id);
  48. }
  49. auto HandleParseNode(Context& context, Parse::VariableInitializerId node_id)
  50. -> bool {
  51. return HandleInitializer(context, node_id);
  52. }
  53. // Builds an associated constant declaration for a `let`.
  54. static auto BuildAssociatedConstantDecl(Context& context,
  55. Parse::LetDeclId node_id,
  56. SemIR::InstId pattern_id,
  57. SemIR::LocIdAndInst pattern,
  58. SemIR::InterfaceId interface_id,
  59. SemIR::AccessKind access_kind) -> void {
  60. auto& interface_info = context.interfaces().Get(interface_id);
  61. auto binding_pattern = pattern.inst.TryAs<SemIR::BindSymbolicName>();
  62. if (!binding_pattern) {
  63. CARBON_DIAGNOSTIC(ExpectedSymbolicBindingInAssociatedConstant, Error,
  64. "pattern in associated constant declaration must be a "
  65. "single `:!` binding");
  66. context.emitter().Emit(pattern.loc_id,
  67. ExpectedSymbolicBindingInAssociatedConstant);
  68. context.name_scopes().Get(interface_info.scope_id).set_has_error();
  69. return;
  70. }
  71. // Replace the tentative BindName instruction with the associated constant
  72. // declaration.
  73. auto name_id =
  74. context.entity_names().Get(binding_pattern->entity_name_id).name_id;
  75. context.ReplaceLocIdAndInstBeforeConstantUse(
  76. pattern_id,
  77. SemIR::LocIdAndInst(node_id, SemIR::AssociatedConstantDecl{
  78. binding_pattern->type_id, name_id}));
  79. auto decl_id = pattern_id;
  80. context.inst_block_stack().AddInstId(decl_id);
  81. // Add an associated entity name to the interface scope.
  82. auto assoc_id = BuildAssociatedEntity(context, interface_id, decl_id);
  83. auto name_context =
  84. context.decl_name_stack().MakeUnqualifiedName(pattern.loc_id, name_id);
  85. context.decl_name_stack().AddNameOrDiagnose(name_context, assoc_id,
  86. access_kind);
  87. }
  88. // Adds name bindings. Returns the resulting ID for the references.
  89. static auto HandleNameBinding(Context& context, SemIR::InstId pattern_id,
  90. SemIR::AccessKind access_kind) -> SemIR::InstId {
  91. // Extract the name binding.
  92. if (auto bind_name =
  93. context.insts().TryGetAs<SemIR::AnyBindName>(pattern_id)) {
  94. // Form a corresponding name in the current context, and bind the name to
  95. // the variable.
  96. auto name_context = context.decl_name_stack().MakeUnqualifiedName(
  97. context.insts().GetLocId(pattern_id),
  98. context.entity_names().Get(bind_name->entity_name_id).name_id);
  99. context.decl_name_stack().AddNameOrDiagnose(name_context, pattern_id,
  100. access_kind);
  101. return bind_name->value_id;
  102. } else if (auto field_decl =
  103. context.insts().TryGetAs<SemIR::FieldDecl>(pattern_id)) {
  104. // Introduce the field name into the class.
  105. auto name_context = context.decl_name_stack().MakeUnqualifiedName(
  106. context.insts().GetLocId(pattern_id), field_decl->name_id);
  107. context.decl_name_stack().AddNameOrDiagnose(name_context, pattern_id,
  108. access_kind);
  109. return pattern_id;
  110. } else {
  111. // TODO: Handle other kinds of pattern.
  112. return pattern_id;
  113. }
  114. }
  115. namespace {
  116. // State from HandleDecl, returned for type-specific handling.
  117. struct DeclInfo {
  118. // The optional initializer.
  119. std::optional<SemIR::InstId> init_id = std::nullopt;
  120. SemIR::InstId pattern_id = SemIR::InstId::Invalid;
  121. std::optional<SemIR::Inst> parent_scope_inst = std::nullopt;
  122. DeclIntroducerState introducer = DeclIntroducerState();
  123. };
  124. } // namespace
  125. // Handles common logic for `let` and `var` declarations.
  126. // TODO: There's still a lot of divergence here, including logic in
  127. // handle_binding_pattern. These should really be better unified.
  128. template <const Lex::TokenKind& IntroducerTokenKind,
  129. const Parse::NodeKind& IntroducerNodeKind,
  130. const Parse::NodeKind& InitializerNodeKind, typename NodeT>
  131. static auto HandleDecl(Context& context, NodeT node_id)
  132. -> std::optional<DeclInfo> {
  133. std::optional<DeclInfo> decl_info = DeclInfo();
  134. // TODO: Update binding-pattern handling to use the pattern block even in
  135. // a let/var context, and then consume it here.
  136. context.pattern_block_stack().PopAndDiscard();
  137. // Handle the optional initializer.
  138. if (context.node_stack().PeekNextIs<InitializerNodeKind>()) {
  139. decl_info->init_id = context.node_stack().PopExpr();
  140. context.node_stack().PopAndDiscardSoloNodeId<InitializerNodeKind>();
  141. }
  142. if (context.node_stack().PeekIs<Parse::NodeKind::TuplePattern>()) {
  143. if (decl_info->init_id &&
  144. context.scope_stack().PeekIndex() == ScopeIndex::Package) {
  145. context.global_init().Suspend();
  146. }
  147. context.TODO(node_id, "tuple pattern in let/var");
  148. decl_info = std::nullopt;
  149. return decl_info;
  150. }
  151. decl_info->pattern_id = context.node_stack().PopPattern();
  152. if constexpr (IntroducerTokenKind == Lex::TokenKind::Var) {
  153. // Pop the `returned` modifier if present.
  154. context.node_stack()
  155. .PopAndDiscardSoloNodeIdIf<Parse::NodeKind::ReturnedModifier>();
  156. }
  157. context.node_stack().PopAndDiscardSoloNodeId<IntroducerNodeKind>();
  158. // Process declaration modifiers.
  159. // TODO: For a qualified `let` or `var` declaration, this should use the
  160. // target scope of the name introduced in the declaration. See #2590.
  161. decl_info->parent_scope_inst =
  162. context.name_scopes()
  163. .GetInstIfValid(context.scope_stack().PeekNameScopeId())
  164. .second;
  165. decl_info->introducer =
  166. context.decl_introducer_state_stack().Pop<IntroducerTokenKind>();
  167. CheckAccessModifiersOnDecl(context, decl_info->introducer,
  168. decl_info->parent_scope_inst);
  169. return decl_info;
  170. }
  171. auto HandleParseNode(Context& context, Parse::LetDeclId node_id) -> bool {
  172. auto decl_info =
  173. HandleDecl<Lex::TokenKind::Let, Parse::NodeKind::LetIntroducer,
  174. Parse::NodeKind::LetInitializer>(context, node_id);
  175. if (!decl_info) {
  176. return false;
  177. }
  178. RequireDefaultFinalOnlyInInterfaces(context, decl_info->introducer,
  179. decl_info->parent_scope_inst);
  180. LimitModifiersOnDecl(
  181. context, decl_info->introducer,
  182. KeywordModifierSet::Access | KeywordModifierSet::Interface);
  183. if (decl_info->introducer.modifier_set.HasAnyOf(
  184. KeywordModifierSet::Interface)) {
  185. context.TODO(decl_info->introducer.modifier_node_id(ModifierOrder::Decl),
  186. "interface modifier");
  187. }
  188. auto pattern = context.insts().GetWithLocId(decl_info->pattern_id);
  189. if (decl_info->init_id) {
  190. // Convert the value to match the type of the pattern.
  191. decl_info->init_id = ConvertToValueOfType(
  192. context, node_id, *decl_info->init_id, pattern.inst.type_id());
  193. }
  194. auto interface_scope = context.GetCurrentScopeAs<SemIR::InterfaceDecl>();
  195. // At interface scope, we are forming an associated constant, which has
  196. // different rules.
  197. if (interface_scope) {
  198. BuildAssociatedConstantDecl(
  199. context, node_id, decl_info->pattern_id, pattern,
  200. interface_scope->interface_id,
  201. decl_info->introducer.modifier_set.GetAccessKind());
  202. return true;
  203. }
  204. if (!decl_info->init_id) {
  205. CARBON_DIAGNOSTIC(
  206. ExpectedInitializerAfterLet, Error,
  207. "expected `=`; `let` declaration must have an initializer");
  208. context.emitter().Emit(TokenOnly(node_id), ExpectedInitializerAfterLet);
  209. }
  210. // Update the binding with its value and add it to the current block, after
  211. // the computation of the value.
  212. // TODO: Support other kinds of pattern here.
  213. auto bind_name = pattern.inst.As<SemIR::AnyBindName>();
  214. CARBON_CHECK(!bind_name.value_id.is_valid(),
  215. "Binding should not already have a value!");
  216. bind_name.value_id = decl_info->init_id ? *decl_info->init_id
  217. : SemIR::ErrorInst::SingletonInstId;
  218. context.ReplaceInstBeforeConstantUse(decl_info->pattern_id, bind_name);
  219. context.inst_block_stack().AddInstId(decl_info->pattern_id);
  220. HandleNameBinding(context, decl_info->pattern_id,
  221. decl_info->introducer.modifier_set.GetAccessKind());
  222. if (decl_info->init_id &&
  223. context.scope_stack().PeekIndex() == ScopeIndex::Package) {
  224. context.global_init().Suspend();
  225. }
  226. return true;
  227. }
  228. auto HandleParseNode(Context& context, Parse::VariableDeclId node_id) -> bool {
  229. auto decl_info =
  230. HandleDecl<Lex::TokenKind::Var, Parse::NodeKind::VariableIntroducer,
  231. Parse::NodeKind::VariableInitializer>(context, node_id);
  232. if (!decl_info) {
  233. return false;
  234. }
  235. LimitModifiersOnDecl(context, decl_info->introducer,
  236. KeywordModifierSet::Access);
  237. decl_info->pattern_id =
  238. HandleNameBinding(context, decl_info->pattern_id,
  239. decl_info->introducer.modifier_set.GetAccessKind());
  240. // If there was an initializer, assign it to the storage.
  241. if (decl_info->init_id) {
  242. if (context.GetCurrentScopeAs<SemIR::ClassDecl>()) {
  243. // TODO: In a class scope, we should instead save the initializer
  244. // somewhere so that we can use it as a default.
  245. context.TODO(node_id, "Field initializer");
  246. } else {
  247. decl_info->init_id = Initialize(context, node_id, decl_info->pattern_id,
  248. *decl_info->init_id);
  249. // TODO: Consider using different instruction kinds for assignment
  250. // versus initialization.
  251. context.AddInst<SemIR::Assign>(node_id, {.lhs_id = decl_info->pattern_id,
  252. .rhs_id = *decl_info->init_id});
  253. }
  254. if (context.scope_stack().PeekIndex() == ScopeIndex::Package) {
  255. context.global_init().Suspend();
  256. }
  257. }
  258. return true;
  259. }
  260. } // namespace Carbon::Check