handle_variable.cpp 3.3 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, Parse::NodeId parse_node)
  10. -> bool {
  11. // No action, just a bracketing node.
  12. context.node_stack().Push(parse_node);
  13. context.decl_state_stack().Push(DeclState::Var, parse_node);
  14. return true;
  15. }
  16. auto HandleReturnedModifier(Context& context, Parse::NodeId parse_node)
  17. -> bool {
  18. // No action, just a bracketing node.
  19. context.node_stack().Push(parse_node);
  20. return true;
  21. }
  22. auto HandleVariableInitializer(Context& context, Parse::NodeId parse_node)
  23. -> bool {
  24. // No action, just a bracketing node.
  25. context.node_stack().Push(parse_node);
  26. return true;
  27. }
  28. auto HandleVariableDecl(Context& context, Parse::NodeId parse_node) -> bool {
  29. // Handle the optional initializer.
  30. auto init_id = SemIR::InstId::Invalid;
  31. bool has_init =
  32. context.parse_tree().node_kind(context.node_stack().PeekParseNode()) !=
  33. Parse::NodeKind::BindingPattern;
  34. if (has_init) {
  35. init_id = context.node_stack().PopExpr();
  36. context.node_stack()
  37. .PopAndDiscardSoloParseNode<Parse::NodeKind::VariableInitializer>();
  38. }
  39. // Extract the name binding.
  40. auto value_id = context.node_stack().Pop<Parse::NodeKind::BindingPattern>();
  41. if (auto bind_name = context.insts().Get(value_id).TryAs<SemIR::BindName>()) {
  42. // Form a corresponding name in the current context, and bind the name to
  43. // the variable.
  44. context.decl_name_stack().AddNameToLookup(
  45. context.decl_name_stack().MakeUnqualifiedName(bind_name->parse_node,
  46. bind_name->name_id),
  47. value_id);
  48. value_id = bind_name->value_id;
  49. }
  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 (has_init) {
  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