handle_variable.cpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 = context.insts().TryGetAs<SemIR::AnyBindName>(value_id)) {
  40. // Form a corresponding name in the current context, and bind the name to
  41. // the variable.
  42. auto name_context = context.decl_name_stack().MakeUnqualifiedName(
  43. context.insts().GetParseNode(value_id),
  44. context.bind_names().Get(bind_name->bind_name_id).name_id);
  45. context.decl_name_stack().AddNameToLookup(name_context, value_id);
  46. value_id = bind_name->value_id;
  47. }
  48. // TODO: Handle other kinds of pattern.
  49. // Pop the `returned` specifier if present.
  50. context.node_stack()
  51. .PopAndDiscardSoloParseNodeIf<Parse::NodeKind::ReturnedModifier>();
  52. // If there was an initializer, assign it to the storage.
  53. if (init_id.has_value()) {
  54. if (context.GetCurrentScopeAs<SemIR::ClassDecl>()) {
  55. // TODO: In a class scope, we should instead save the initializer
  56. // somewhere so that we can use it as a default.
  57. context.TODO(parse_node, "Field initializer");
  58. } else {
  59. init_id = Initialize(context, parse_node, value_id, *init_id);
  60. // TODO: Consider using different instruction kinds for assignment versus
  61. // initialization.
  62. context.AddInst({parse_node, SemIR::Assign{value_id, *init_id}});
  63. }
  64. }
  65. context.node_stack()
  66. .PopAndDiscardSoloParseNode<Parse::NodeKind::VariableIntroducer>();
  67. // Process declaration modifiers.
  68. CheckAccessModifiersOnDecl(context, Lex::TokenKind::Var);
  69. LimitModifiersOnDecl(context, KeywordModifierSet::Access,
  70. Lex::TokenKind::Var);
  71. auto modifiers = context.decl_state_stack().innermost().modifier_set;
  72. if (!!(modifiers & KeywordModifierSet::Access)) {
  73. context.TODO(context.decl_state_stack().innermost().saw_access_modifier,
  74. "access modifier");
  75. }
  76. context.decl_state_stack().Pop(DeclState::Var);
  77. return true;
  78. }
  79. } // namespace Carbon::Check