semantics_handle_struct.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. // Convert the operand to a value.
  42. // TODO: We need to decide how struct literals interact with expression
  43. // categories.
  44. value_node_id = context.ConvertToValueExpression(value_node_id);
  45. // Store the name for the type.
  46. auto type_block_id = context.args_type_info_stack().PeekForAdd();
  47. context.semantics_ir().AddNode(
  48. type_block_id,
  49. SemIR::Node::StructTypeField::Make(
  50. parse_node, name_id,
  51. context.semantics_ir().GetNode(value_node_id).type_id()));
  52. // Push the value back on the stack as an argument.
  53. context.node_stack().Push(parse_node, value_node_id);
  54. return true;
  55. }
  56. auto HandleStructLiteral(Context& context, ParseTree::Node parse_node) -> bool {
  57. auto refs_id = context.ParamOrArgEnd(
  58. /*for_args=*/true, ParseNodeKind::StructLiteralOrStructTypeLiteralStart);
  59. context.PopScope();
  60. context.node_stack()
  61. .PopAndDiscardSoloParseNode<
  62. ParseNodeKind::StructLiteralOrStructTypeLiteralStart>();
  63. auto type_block_id = context.args_type_info_stack().Pop();
  64. auto type_id = context.CanonicalizeStructType(parse_node, type_block_id);
  65. auto value_id = context.AddNode(
  66. SemIR::Node::StructValue::Make(parse_node, type_id, refs_id));
  67. context.node_stack().Push(parse_node, value_id);
  68. return true;
  69. }
  70. auto HandleStructLiteralOrStructTypeLiteralStart(Context& context,
  71. ParseTree::Node parse_node)
  72. -> bool {
  73. context.PushScope();
  74. context.node_stack().Push(parse_node);
  75. // At this point we aren't sure whether this will be a value or type literal,
  76. // so we push onto args irrespective. It just won't be used for a type
  77. // literal.
  78. context.args_type_info_stack().Push();
  79. context.ParamOrArgStart();
  80. return true;
  81. }
  82. auto HandleStructTypeLiteral(Context& context, ParseTree::Node parse_node)
  83. -> bool {
  84. auto refs_id = context.ParamOrArgEnd(
  85. /*for_args=*/false, ParseNodeKind::StructLiteralOrStructTypeLiteralStart);
  86. context.PopScope();
  87. context.node_stack()
  88. .PopAndDiscardSoloParseNode<
  89. ParseNodeKind::StructLiteralOrStructTypeLiteralStart>();
  90. // This is only used for value literals.
  91. context.args_type_info_stack().Pop();
  92. CARBON_CHECK(refs_id != SemIR::NodeBlockId::Empty)
  93. << "{} is handled by StructLiteral.";
  94. context.AddNodeAndPush(parse_node,
  95. SemIR::Node::StructType::Make(
  96. parse_node, SemIR::TypeId::TypeType, refs_id));
  97. return true;
  98. }
  99. } // namespace Carbon::Check