handle_let.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. // TODO: For a qualified `let` declaration, this should use the target scope
  30. // of the name introduced in the declaration. See #2590.
  31. CheckAccessModifiersOnDecl(context, Lex::TokenKind::Let,
  32. context.scope_stack().PeekNameScopeId());
  33. RequireDefaultFinalOnlyInInterfaces(context, Lex::TokenKind::Let,
  34. context.scope_stack().PeekNameScopeId());
  35. LimitModifiersOnDecl(
  36. context, KeywordModifierSet::Access | KeywordModifierSet::Interface,
  37. Lex::TokenKind::Let);
  38. auto modifiers = context.decl_state_stack().innermost().modifier_set;
  39. if (!!(modifiers & KeywordModifierSet::Access)) {
  40. context.TODO(context.decl_state_stack().innermost().saw_access_modifier,
  41. "access modifier");
  42. }
  43. if (!!(modifiers & KeywordModifierSet::Interface)) {
  44. context.TODO(context.decl_state_stack().innermost().saw_decl_modifier,
  45. "interface modifier");
  46. }
  47. context.decl_state_stack().Pop(DeclState::Let);
  48. // Convert the value to match the type of the pattern.
  49. auto pattern = context.insts().GetWithParseNode(pattern_id);
  50. value_id = ConvertToValueOfType(context, parse_node, value_id,
  51. pattern.inst.type_id());
  52. // Update the binding with its value and add it to the current block, after
  53. // the computation of the value.
  54. // TODO: Support other kinds of pattern here.
  55. auto bind_name = pattern.inst.As<SemIR::AnyBindName>();
  56. CARBON_CHECK(!bind_name.value_id.is_valid())
  57. << "Binding should not already have a value!";
  58. bind_name.value_id = value_id;
  59. pattern.inst = bind_name;
  60. context.ReplaceInstBeforeConstantUse(pattern_id, pattern);
  61. context.inst_block_stack().AddInstId(pattern_id);
  62. // Add the name of the binding to the current scope.
  63. auto name_id = context.bind_names().Get(bind_name.bind_name_id).name_id;
  64. context.AddNameToLookup(name_id, pattern_id);
  65. return true;
  66. }
  67. } // namespace Carbon::Check