handle_variable.cpp 4.6 KB

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