handle_variable.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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/sem_ir/inst.h"
  7. namespace Carbon::Check {
  8. auto HandleVariableIntroducer(Context& context, Parse::Node parse_node)
  9. -> bool {
  10. // No action, just a bracketing node.
  11. context.node_stack().Push(parse_node);
  12. return true;
  13. }
  14. auto HandleReturnedModifier(Context& context, Parse::Node parse_node) -> bool {
  15. // No action, just a bracketing node.
  16. context.node_stack().Push(parse_node);
  17. return true;
  18. }
  19. auto HandleVariableInitializer(Context& context, Parse::Node parse_node)
  20. -> bool {
  21. // No action, just a bracketing node.
  22. context.node_stack().Push(parse_node);
  23. return true;
  24. }
  25. auto HandleVariableDecl(Context& context, Parse::Node parse_node) -> bool {
  26. // Handle the optional initializer.
  27. auto init_id = SemIR::InstId::Invalid;
  28. bool has_init =
  29. context.parse_tree().node_kind(context.node_stack().PeekParseNode()) !=
  30. Parse::NodeKind::PatternBinding;
  31. if (has_init) {
  32. init_id = context.node_stack().PopExpr();
  33. context.node_stack()
  34. .PopAndDiscardSoloParseNode<Parse::NodeKind::VariableInitializer>();
  35. }
  36. // Extract the name binding.
  37. auto value_id = context.node_stack().Pop<Parse::NodeKind::PatternBinding>();
  38. if (auto bind_name = context.insts().Get(value_id).TryAs<SemIR::BindName>()) {
  39. // Form a corresponding name in the current context, and bind the name to
  40. // the variable.
  41. context.decl_name_stack().AddNameToLookup(
  42. context.decl_name_stack().MakeUnqualifiedName(bind_name->parse_node,
  43. bind_name->name_id),
  44. value_id);
  45. value_id = bind_name->value_id;
  46. }
  47. // Pop the `returned` specifier if present.
  48. context.node_stack()
  49. .PopAndDiscardSoloParseNodeIf<Parse::NodeKind::ReturnedModifier>();
  50. // If there was an initializer, assign it to the storage.
  51. if (has_init) {
  52. if (context.GetCurrentScopeAs<SemIR::ClassDecl>()) {
  53. // TODO: In a class scope, we should instead save the initializer
  54. // somewhere so that we can use it as a default.
  55. context.TODO(parse_node, "Field initializer");
  56. } else {
  57. init_id = Initialize(context, parse_node, value_id, init_id);
  58. // TODO: Consider using different instruction kinds for assignment versus
  59. // initialization.
  60. context.AddInst(SemIR::Assign{parse_node, value_id, init_id});
  61. }
  62. }
  63. context.node_stack()
  64. .PopAndDiscardSoloParseNode<Parse::NodeKind::VariableIntroducer>();
  65. return true;
  66. }
  67. } // namespace Carbon::Check