handle_let.cpp 2.7 KB

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