handle_let.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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/modifiers.h"
  7. #include "toolchain/sem_ir/inst.h"
  8. namespace Carbon::Check {
  9. auto HandleLetIntroducer(Context& context, Parse::LetIntroducerId parse_node)
  10. -> bool {
  11. context.decl_state_stack().Push(DeclState::Let);
  12. // Push a bracketing node to establish the pattern context.
  13. context.node_stack().Push(parse_node);
  14. return true;
  15. }
  16. auto HandleLetInitializer(Context& /*context*/,
  17. Parse::LetInitializerId /*parse_node*/) -> bool {
  18. return true;
  19. }
  20. auto HandleLetDecl(Context& context, Parse::LetDeclId parse_node) -> bool {
  21. auto value_id = context.node_stack().PopExpr();
  22. if (context.node_stack().PeekIs<Parse::NodeKind::TuplePattern>()) {
  23. return context.TODO(parse_node, "tuple pattern in let");
  24. }
  25. SemIR::InstId pattern_id = context.node_stack().PopPattern();
  26. context.node_stack()
  27. .PopAndDiscardSoloParseNode<Parse::NodeKind::LetIntroducer>();
  28. // Process declaration modifiers.
  29. CheckAccessModifiersOnDecl(context, Lex::TokenKind::Let);
  30. RequireDefaultFinalOnlyInInterfaces(context, Lex::TokenKind::Let);
  31. LimitModifiersOnDecl(
  32. context, KeywordModifierSet::Access | KeywordModifierSet::Interface,
  33. Lex::TokenKind::Let);
  34. auto modifiers = context.decl_state_stack().innermost().modifier_set;
  35. if (!!(modifiers & KeywordModifierSet::Access)) {
  36. context.TODO(context.decl_state_stack().innermost().saw_access_modifier,
  37. "access modifier");
  38. }
  39. if (!!(modifiers & KeywordModifierSet::Interface)) {
  40. context.TODO(context.decl_state_stack().innermost().saw_decl_modifier,
  41. "interface modifier");
  42. }
  43. context.decl_state_stack().Pop(DeclState::Let);
  44. // Convert the value to match the type of the pattern.
  45. auto pattern = context.insts().GetWithParseNode(pattern_id);
  46. value_id = ConvertToValueOfType(context, parse_node, value_id,
  47. pattern.inst.type_id());
  48. // Update the binding with its value and add it to the current block, after
  49. // the computation of the value.
  50. // TODO: Support other kinds of pattern here.
  51. auto bind_name = pattern.inst.As<SemIR::AnyBindName>();
  52. CARBON_CHECK(!bind_name.value_id.is_valid())
  53. << "Binding should not already have a value!";
  54. bind_name.value_id = value_id;
  55. pattern.inst = bind_name;
  56. context.ReplaceInstBeforeConstantUse(pattern_id, pattern);
  57. context.inst_block_stack().AddInstId(pattern_id);
  58. // Add the name of the binding to the current scope.
  59. auto name_id = context.bind_names().Get(bind_name.bind_name_id).name_id;
  60. context.AddNameToLookup(name_id, pattern_id);
  61. return true;
  62. }
  63. } // namespace Carbon::Check