semantics_handle_struct.cpp 4.1 KB

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