semantics_handle_struct.cpp 4.2 KB

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