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/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().GetNodeId(value_id),
  51. context.bind_names().Get(bind_name->bind_name_id).name_id);
  52. context.decl_name_stack().AddNameToLookup(name_context, value_id);
  53. value_id = bind_name->value_id;
  54. } else if (auto field_decl =
  55. context.insts().TryGetAs<SemIR::FieldDecl>(value_id)) {
  56. // Introduce the field name into the class.
  57. auto name_context = context.decl_name_stack().MakeUnqualifiedName(
  58. context.insts().GetNodeId(value_id), field_decl->name_id);
  59. context.decl_name_stack().AddNameToLookup(name_context, value_id);
  60. }
  61. // TODO: Handle other kinds of pattern.
  62. // Pop the `returned` specifier if present.
  63. context.node_stack()
  64. .PopAndDiscardSoloNodeIdIf<Parse::NodeKind::ReturnedModifier>();
  65. // If there was an initializer, assign it to the storage.
  66. if (init_id) {
  67. if (context.GetCurrentScopeAs<SemIR::ClassDecl>()) {
  68. // TODO: In a class scope, we should instead save the initializer
  69. // somewhere so that we can use it as a default.
  70. context.TODO(node_id, "Field initializer");
  71. } else {
  72. init_id = Initialize(context, node_id, value_id, *init_id);
  73. // TODO: Consider using different instruction kinds for assignment versus
  74. // initialization.
  75. context.AddInst({node_id, SemIR::Assign{value_id, *init_id}});
  76. }
  77. if (context.scope_stack().PeekIndex() == ScopeIndex::Package) {
  78. context.inst_block_stack().PopGlobalInit();
  79. }
  80. }
  81. context.node_stack()
  82. .PopAndDiscardSoloNodeId<Parse::NodeKind::VariableIntroducer>();
  83. // Process declaration modifiers.
  84. // TODO: For a qualified `var` declaration, this should use the target scope
  85. // of the name introduced in the declaration. See #2590.
  86. CheckAccessModifiersOnDecl(context, Lex::TokenKind::Var,
  87. context.scope_stack().PeekNameScopeId());
  88. LimitModifiersOnDecl(context, KeywordModifierSet::Access,
  89. Lex::TokenKind::Var);
  90. auto modifiers = context.decl_state_stack().innermost().modifier_set;
  91. if (!!(modifiers & KeywordModifierSet::Access)) {
  92. context.TODO(context.decl_state_stack().innermost().modifier_node_id(
  93. ModifierOrder::Access),
  94. "access modifier");
  95. }
  96. context.decl_state_stack().Pop(DeclState::Var);
  97. return true;
  98. }
  99. } // namespace Carbon::Check