handle_variable.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. namespace Carbon::Check {
  8. auto HandleVariableIntroducer(Context& context,
  9. Parse::VariableIntroducerId parse_node) -> bool {
  10. // No action, just a bracketing node.
  11. context.node_stack().Push(parse_node);
  12. context.decl_state_stack().Push(DeclState::Var);
  13. return true;
  14. }
  15. auto HandleReturnedModifier(Context& context,
  16. Parse::ReturnedModifierId parse_node) -> bool {
  17. // No action, just a bracketing node.
  18. context.node_stack().Push(parse_node);
  19. return true;
  20. }
  21. auto HandleVariableInitializer(Context& context,
  22. Parse::VariableInitializerId parse_node)
  23. -> bool {
  24. SemIR::InstId init_id = context.node_stack().PopExpr();
  25. context.node_stack().Push(parse_node, init_id);
  26. return true;
  27. }
  28. auto HandleVariableDecl(Context& context, Parse::VariableDeclId parse_node)
  29. -> bool {
  30. // Handle the optional initializer.
  31. std::optional<SemIR::InstId> init_id =
  32. context.node_stack().PopIf<Parse::NodeKind::VariableInitializer>();
  33. if (context.node_stack().PeekIs<Parse::NodeKind::TuplePattern>()) {
  34. return context.TODO(parse_node, "tuple pattern in var");
  35. }
  36. // Extract the name binding.
  37. auto value_id = context.node_stack().PopPattern();
  38. if (auto bind_name = context.insts().TryGetAs<SemIR::AnyBindName>(value_id)) {
  39. // Form a corresponding name in the current context, and bind the name to
  40. // the variable.
  41. auto name_context = context.decl_name_stack().MakeUnqualifiedName(
  42. context.insts().GetParseNode(value_id),
  43. context.bind_names().Get(bind_name->bind_name_id).name_id);
  44. context.decl_name_stack().AddNameToLookup(name_context, value_id);
  45. value_id = bind_name->value_id;
  46. } else if (auto field_decl =
  47. context.insts().TryGetAs<SemIR::FieldDecl>(value_id)) {
  48. // Introduce the field name into the class.
  49. auto name_context = context.decl_name_stack().MakeUnqualifiedName(
  50. context.insts().GetParseNode(value_id), field_decl->name_id);
  51. context.decl_name_stack().AddNameToLookup(name_context, value_id);
  52. }
  53. // TODO: Handle other kinds of pattern.
  54. // Pop the `returned` specifier if present.
  55. context.node_stack()
  56. .PopAndDiscardSoloParseNodeIf<Parse::NodeKind::ReturnedModifier>();
  57. // If there was an initializer, assign it to the storage.
  58. if (init_id.has_value()) {
  59. if (context.GetCurrentScopeAs<SemIR::ClassDecl>()) {
  60. // TODO: In a class scope, we should instead save the initializer
  61. // somewhere so that we can use it as a default.
  62. context.TODO(parse_node, "Field initializer");
  63. } else {
  64. init_id = Initialize(context, parse_node, value_id, *init_id);
  65. // TODO: Consider using different instruction kinds for assignment versus
  66. // initialization.
  67. context.AddInst({parse_node, SemIR::Assign{value_id, *init_id}});
  68. }
  69. }
  70. context.node_stack()
  71. .PopAndDiscardSoloParseNode<Parse::NodeKind::VariableIntroducer>();
  72. // Process declaration modifiers.
  73. // TODO: For a qualified `var` declaration, this should use the target scope
  74. // of the name introduced in the declaration. See #2590.
  75. CheckAccessModifiersOnDecl(context, Lex::TokenKind::Var,
  76. context.scope_stack().PeekNameScopeId());
  77. LimitModifiersOnDecl(context, KeywordModifierSet::Access,
  78. Lex::TokenKind::Var);
  79. auto modifiers = context.decl_state_stack().innermost().modifier_set;
  80. if (!!(modifiers & KeywordModifierSet::Access)) {
  81. context.TODO(context.decl_state_stack().innermost().saw_access_modifier,
  82. "access modifier");
  83. }
  84. context.decl_state_stack().Pop(DeclState::Var);
  85. return true;
  86. }
  87. } // namespace Carbon::Check