handle_variable.cpp 3.7 KB

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