handle_variable.cpp 4.4 KB

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