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