handle_let.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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/sem_ir/inst.h"
  7. namespace Carbon::Check {
  8. auto HandleLetDecl(Context& context, Parse::NodeId parse_node) -> bool {
  9. auto value_id = context.node_stack().PopExpr();
  10. SemIR::InstId pattern_id =
  11. context.node_stack().Pop<Parse::NodeKind::BindingPattern>();
  12. context.node_stack()
  13. .PopAndDiscardSoloParseNode<Parse::NodeKind::LetIntroducer>();
  14. // Convert the value to match the type of the pattern.
  15. auto pattern = context.insts().Get(pattern_id);
  16. value_id =
  17. ConvertToValueOfType(context, parse_node, value_id, pattern.type_id());
  18. // Update the binding with its value and add it to the current block, after
  19. // the computation of the value.
  20. // TODO: Support other kinds of pattern here.
  21. auto bind_name = pattern.As<SemIR::BindName>();
  22. CARBON_CHECK(!bind_name.value_id.is_valid())
  23. << "Binding should not already have a value!";
  24. bind_name.value_id = value_id;
  25. context.insts().Set(pattern_id, bind_name);
  26. context.inst_block_stack().AddInstId(pattern_id);
  27. // Add the name of the binding to the current scope.
  28. context.AddNameToLookup(pattern.parse_node(), bind_name.name_id, pattern_id);
  29. return true;
  30. }
  31. auto HandleLetIntroducer(Context& context, Parse::NodeId parse_node) -> bool {
  32. // Push a bracketing node to establish the pattern context.
  33. context.node_stack().Push(parse_node);
  34. return true;
  35. }
  36. auto HandleLetInitializer(Context& /*context*/, Parse::NodeId /*parse_node*/)
  37. -> bool {
  38. return true;
  39. }
  40. } // namespace Carbon::Check