handle_variable.cpp 3.8 KB

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