handle_let.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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().Get(pattern_id);
  46. value_id =
  47. ConvertToValueOfType(context, parse_node, value_id, pattern.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.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. context.insts().Set(pattern_id, bind_name);
  56. context.inst_block_stack().AddInstId(pattern_id);
  57. // Add the name of the binding to the current scope.
  58. auto name_id = context.bind_names().Get(bind_name.bind_name_id).name_id;
  59. context.AddNameToLookup(pattern.parse_node(), name_id, pattern_id);
  60. return true;
  61. }
  62. } // namespace Carbon::Check