handle_let.cpp 2.6 KB

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