handle_variable.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #include "toolchain/sem_ir/inst.h"
  8. namespace Carbon::Check {
  9. auto HandleVariableIntroducer(Context& context, Parse::NodeId parse_node)
  10. -> bool {
  11. // No action, just a bracketing node.
  12. context.node_stack().Push(parse_node);
  13. context.decl_state_stack().Push(DeclState::Var, parse_node);
  14. return true;
  15. }
  16. auto HandleReturnedModifier(Context& context, Parse::NodeId parse_node)
  17. -> bool {
  18. // No action, just a bracketing node.
  19. context.node_stack().Push(parse_node);
  20. return true;
  21. }
  22. auto HandleVariableInitializer(Context& context, Parse::NodeId parse_node)
  23. -> bool {
  24. // No action, just a bracketing node.
  25. context.node_stack().Push(parse_node);
  26. return true;
  27. }
  28. auto HandleVariableDecl(Context& context, Parse::NodeId parse_node) -> bool {
  29. // Handle the optional initializer.
  30. auto init_id = SemIR::InstId::Invalid;
  31. Parse::NodeKind next_kind =
  32. context.parse_tree().node_kind(context.node_stack().PeekParseNode());
  33. if (next_kind == Parse::NodeKind::ParamList) {
  34. return context.TODO(parse_node, "tuple pattern in var");
  35. }
  36. // TODO: find a more robust way to determine if there was an initializer.
  37. bool has_init = next_kind != Parse::NodeKind::BindingPattern;
  38. if (has_init) {
  39. init_id = context.node_stack().PopExpr();
  40. context.node_stack()
  41. .PopAndDiscardSoloParseNode<Parse::NodeKind::VariableInitializer>();
  42. }
  43. if (context.node_stack().PeekIs<Parse::NodeKind::ParamList>()) {
  44. return context.TODO(parse_node, "tuple pattern in var");
  45. }
  46. // Extract the name binding.
  47. auto value_id = context.node_stack().Pop<Parse::NodeKind::BindingPattern>();
  48. if (auto bind_name = context.insts().Get(value_id).TryAs<SemIR::BindName>()) {
  49. // Form a corresponding name in the current context, and bind the name to
  50. // the variable.
  51. context.decl_name_stack().AddNameToLookup(
  52. context.decl_name_stack().MakeUnqualifiedName(bind_name->parse_node,
  53. bind_name->name_id),
  54. value_id);
  55. value_id = bind_name->value_id;
  56. }
  57. // Pop the `returned` specifier if present.
  58. context.node_stack()
  59. .PopAndDiscardSoloParseNodeIf<Parse::NodeKind::ReturnedModifier>();
  60. // If there was an initializer, assign it to the storage.
  61. if (has_init) {
  62. if (context.GetCurrentScopeAs<SemIR::ClassDecl>()) {
  63. // TODO: In a class scope, we should instead save the initializer
  64. // somewhere so that we can use it as a default.
  65. context.TODO(parse_node, "Field initializer");
  66. } else {
  67. init_id = Initialize(context, parse_node, value_id, init_id);
  68. // TODO: Consider using different instruction kinds for assignment versus
  69. // initialization.
  70. context.AddInst(SemIR::Assign{parse_node, value_id, init_id});
  71. }
  72. }
  73. context.node_stack()
  74. .PopAndDiscardSoloParseNode<Parse::NodeKind::VariableIntroducer>();
  75. // Process declaration modifiers.
  76. CheckAccessModifiersOnDecl(context, Lex::TokenKind::Var);
  77. LimitModifiersOnDecl(context, KeywordModifierSet::Access,
  78. Lex::TokenKind::Var);
  79. auto modifiers = context.decl_state_stack().innermost().modifier_set;
  80. if (!!(modifiers & KeywordModifierSet::Access)) {
  81. context.TODO(context.decl_state_stack().innermost().saw_access_modifier,
  82. "access modifier");
  83. }
  84. context.decl_state_stack().Pop(DeclState::Var);
  85. return true;
  86. }
  87. } // namespace Carbon::Check