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