handle_struct.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. namespace Carbon::Check {
  6. auto HandleStructComma(Context& context, Parse::Node /*parse_node*/) -> bool {
  7. context.ParamOrArgComma();
  8. return true;
  9. }
  10. auto HandleStructFieldDesignator(Context& context, Parse::Node /*parse_node*/)
  11. -> bool {
  12. // This leaves the designated name on top because the `.` isn't interesting.
  13. CARBON_CHECK(
  14. context.parse_tree().node_kind(context.node_stack().PeekParseNode()) ==
  15. Parse::NodeKind::Name);
  16. return true;
  17. }
  18. auto HandleStructFieldType(Context& context, Parse::Node parse_node) -> bool {
  19. auto [type_node, type_id] = context.node_stack().PopExpressionWithParseNode();
  20. SemIR::TypeId cast_type_id = context.ExpressionAsType(type_node, type_id);
  21. auto [name_node, name_id] =
  22. context.node_stack().PopWithParseNode<Parse::NodeKind::Name>();
  23. context.AddNodeAndPush(parse_node, SemIR::Node::StructTypeField::Make(
  24. name_node, name_id, cast_type_id));
  25. return true;
  26. }
  27. auto HandleStructFieldUnknown(Context& context, Parse::Node parse_node)
  28. -> bool {
  29. return context.TODO(parse_node, "HandleStructFieldUnknown");
  30. }
  31. auto HandleStructFieldValue(Context& context, Parse::Node parse_node) -> bool {
  32. auto [value_parse_node, value_node_id] =
  33. context.node_stack().PopExpressionWithParseNode();
  34. SemIR::StringId name_id = context.node_stack().Pop<Parse::NodeKind::Name>();
  35. // Store the name for the type.
  36. context.args_type_info_stack().AddNode(SemIR::Node::StructTypeField::Make(
  37. parse_node, name_id,
  38. context.semantics_ir().GetNode(value_node_id).type_id()));
  39. // Push the value back on the stack as an argument.
  40. context.node_stack().Push(parse_node, value_node_id);
  41. return true;
  42. }
  43. auto HandleStructLiteral(Context& context, Parse::Node parse_node) -> bool {
  44. auto refs_id = context.ParamOrArgEnd(
  45. Parse::NodeKind::StructLiteralOrStructTypeLiteralStart);
  46. context.PopScope();
  47. context.node_stack()
  48. .PopAndDiscardSoloParseNode<
  49. Parse::NodeKind::StructLiteralOrStructTypeLiteralStart>();
  50. auto type_block_id = context.args_type_info_stack().Pop();
  51. auto type_id = context.CanonicalizeStructType(parse_node, type_block_id);
  52. auto value_id = context.AddNode(
  53. SemIR::Node::StructLiteral::Make(parse_node, type_id, refs_id));
  54. context.node_stack().Push(parse_node, value_id);
  55. return true;
  56. }
  57. auto HandleStructLiteralOrStructTypeLiteralStart(Context& context,
  58. Parse::Node parse_node)
  59. -> bool {
  60. context.PushScope();
  61. context.node_stack().Push(parse_node);
  62. // At this point we aren't sure whether this will be a value or type literal,
  63. // so we push onto args irrespective. It just won't be used for a type
  64. // literal.
  65. context.args_type_info_stack().Push();
  66. context.ParamOrArgStart();
  67. return true;
  68. }
  69. auto HandleStructTypeLiteral(Context& context, Parse::Node parse_node) -> bool {
  70. auto refs_id = context.ParamOrArgEnd(
  71. Parse::NodeKind::StructLiteralOrStructTypeLiteralStart);
  72. context.PopScope();
  73. context.node_stack()
  74. .PopAndDiscardSoloParseNode<
  75. Parse::NodeKind::StructLiteralOrStructTypeLiteralStart>();
  76. // This is only used for value literals.
  77. context.args_type_info_stack().Pop();
  78. CARBON_CHECK(refs_id != SemIR::NodeBlockId::Empty)
  79. << "{} is handled by StructLiteral.";
  80. context.AddNodeAndPush(parse_node,
  81. SemIR::Node::StructType::Make(
  82. parse_node, SemIR::TypeId::TypeType, refs_id));
  83. return true;
  84. }
  85. } // namespace Carbon::Check