handle_variable.cpp 4.4 KB

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