handle_variable.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 HandleVariableIntroducer(Context& context,
  10. Parse::VariableIntroducerId parse_node) -> bool {
  11. // No action, just a bracketing node.
  12. context.node_stack().Push(parse_node);
  13. context.decl_state_stack().Push(DeclState::Var);
  14. return true;
  15. }
  16. auto HandleReturnedModifier(Context& context,
  17. Parse::ReturnedModifierId parse_node) -> bool {
  18. // No action, just a bracketing node.
  19. context.node_stack().Push(parse_node);
  20. return true;
  21. }
  22. auto HandleVariableInitializer(Context& context,
  23. Parse::VariableInitializerId parse_node)
  24. -> bool {
  25. SemIR::InstId init_id = context.node_stack().PopExpr();
  26. context.node_stack().Push(parse_node, init_id);
  27. return true;
  28. }
  29. auto HandleVariableDecl(Context& context, Parse::VariableDeclId parse_node)
  30. -> bool {
  31. // Handle the optional initializer.
  32. std::optional<SemIR::InstId> init_id =
  33. context.node_stack().PopIf<Parse::NodeKind::VariableInitializer>();
  34. if (context.node_stack().PeekIs<Parse::NodeKind::TuplePattern>()) {
  35. return context.TODO(parse_node, "tuple pattern in var");
  36. }
  37. // Extract the name binding.
  38. auto value_id = context.node_stack().PopPattern();
  39. if (auto bind_name =
  40. context.insts().Get(value_id).TryAs<SemIR::AnyBindName>()) {
  41. // Form a corresponding name in the current context, and bind the name to
  42. // the variable.
  43. auto name_context = context.decl_name_stack().MakeUnqualifiedName(
  44. bind_name->parse_node,
  45. context.bind_names().Get(bind_name->bind_name_id).name_id);
  46. context.decl_name_stack().AddNameToLookup(name_context, value_id);
  47. value_id = bind_name->value_id;
  48. }
  49. // TODO: Handle other kinds of pattern.
  50. // Pop the `returned` specifier if present.
  51. context.node_stack()
  52. .PopAndDiscardSoloParseNodeIf<Parse::NodeKind::ReturnedModifier>();
  53. // If there was an initializer, assign it to the storage.
  54. if (init_id.has_value()) {
  55. if (context.GetCurrentScopeAs<SemIR::ClassDecl>()) {
  56. // TODO: In a class scope, we should instead save the initializer
  57. // somewhere so that we can use it as a default.
  58. context.TODO(parse_node, "Field initializer");
  59. } else {
  60. init_id = Initialize(context, parse_node, value_id, *init_id);
  61. // TODO: Consider using different instruction kinds for assignment versus
  62. // initialization.
  63. context.AddInst(SemIR::Assign{parse_node, value_id, *init_id});
  64. }
  65. }
  66. context.node_stack()
  67. .PopAndDiscardSoloParseNode<Parse::NodeKind::VariableIntroducer>();
  68. // Process declaration modifiers.
  69. CheckAccessModifiersOnDecl(context, Lex::TokenKind::Var);
  70. LimitModifiersOnDecl(context, KeywordModifierSet::Access,
  71. Lex::TokenKind::Var);
  72. auto modifiers = context.decl_state_stack().innermost().modifier_set;
  73. if (!!(modifiers & KeywordModifierSet::Access)) {
  74. context.TODO(context.decl_state_stack().innermost().saw_access_modifier,
  75. "access modifier");
  76. }
  77. context.decl_state_stack().Pop(DeclState::Var);
  78. return true;
  79. }
  80. } // namespace Carbon::Check