handle_variable.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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);
  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. Parse::NodeKind next_kind = context.node_stack().PeekParseNodeKind();
  32. if (next_kind == Parse::NodeKind::TuplePattern) {
  33. return context.TODO(parse_node, "tuple pattern in var");
  34. }
  35. // TODO: find a more robust way to determine if there was an initializer.
  36. bool has_init = next_kind != Parse::NodeKind::BindingPattern;
  37. if (has_init) {
  38. init_id = context.node_stack().PopExpr();
  39. context.node_stack()
  40. .PopAndDiscardSoloParseNode<Parse::NodeKind::VariableInitializer>();
  41. }
  42. if (context.node_stack().PeekIs<Parse::NodeKind::TuplePattern>()) {
  43. return context.TODO(parse_node, "tuple pattern in var");
  44. }
  45. // Extract the name binding.
  46. auto value_id = context.node_stack().Pop<Parse::NodeKind::BindingPattern>();
  47. if (auto bind_name = context.insts().Get(value_id).TryAs<SemIR::BindName>()) {
  48. // Form a corresponding name in the current context, and bind the name to
  49. // the variable.
  50. context.decl_name_stack().AddNameToLookup(
  51. context.decl_name_stack().MakeUnqualifiedName(bind_name->parse_node,
  52. bind_name->name_id),
  53. value_id);
  54. value_id = bind_name->value_id;
  55. }
  56. // Pop the `returned` specifier if present.
  57. context.node_stack()
  58. .PopAndDiscardSoloParseNodeIf<Parse::NodeKind::ReturnedModifier>();
  59. // If there was an initializer, assign it to the storage.
  60. if (has_init) {
  61. if (context.GetCurrentScopeAs<SemIR::ClassDecl>()) {
  62. // TODO: In a class scope, we should instead save the initializer
  63. // somewhere so that we can use it as a default.
  64. context.TODO(parse_node, "Field initializer");
  65. } else {
  66. init_id = Initialize(context, parse_node, value_id, init_id);
  67. // TODO: Consider using different instruction kinds for assignment versus
  68. // initialization.
  69. context.AddInst(SemIR::Assign{parse_node, value_id, init_id});
  70. }
  71. }
  72. context.node_stack()
  73. .PopAndDiscardSoloParseNode<Parse::NodeKind::VariableIntroducer>();
  74. // Process declaration modifiers.
  75. CheckAccessModifiersOnDecl(context, Lex::TokenKind::Var);
  76. LimitModifiersOnDecl(context, KeywordModifierSet::Access,
  77. Lex::TokenKind::Var);
  78. auto modifiers = context.decl_state_stack().innermost().modifier_set;
  79. if (!!(modifiers & KeywordModifierSet::Access)) {
  80. context.TODO(context.decl_state_stack().innermost().saw_access_modifier,
  81. "access modifier");
  82. }
  83. context.decl_state_stack().Pop(DeclState::Var);
  84. return true;
  85. }
  86. } // namespace Carbon::Check