semantics_handle_struct.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 {
  6. auto SemanticsHandleStructComma(SemanticsContext& context,
  7. ParseTree::Node /*parse_node*/) -> 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 SemanticsHandleStructFieldDesignator(SemanticsContext& context,
  15. ParseTree::Node /*parse_node*/)
  16. -> bool {
  17. // This leaves the designated name on top because the `.` isn't interesting.
  18. CARBON_CHECK(
  19. context.parse_tree().node_kind(context.node_stack().PeekParseNode()) ==
  20. ParseNodeKind::Name);
  21. return true;
  22. }
  23. auto SemanticsHandleStructFieldType(SemanticsContext& context,
  24. ParseTree::Node parse_node) -> bool {
  25. auto [type_node, type_id] = context.node_stack().PopExpressionWithParseNode();
  26. SemanticsTypeId cast_type_id = context.ExpressionAsType(type_node, type_id);
  27. auto [name_node, name_id] =
  28. context.node_stack().PopWithParseNode<ParseNodeKind::Name>();
  29. context.AddNodeAndPush(parse_node, SemanticsNode::StructTypeField::Make(
  30. name_node, name_id, cast_type_id));
  31. return true;
  32. }
  33. auto SemanticsHandleStructFieldUnknown(SemanticsContext& context,
  34. ParseTree::Node parse_node) -> bool {
  35. return context.TODO(parse_node, "HandleStructFieldUnknown");
  36. }
  37. auto SemanticsHandleStructFieldValue(SemanticsContext& context,
  38. ParseTree::Node parse_node) -> bool {
  39. auto [value_parse_node, value_node_id] =
  40. context.node_stack().PopExpressionWithParseNode();
  41. SemanticsStringId name_id = context.node_stack().Pop<ParseNodeKind::Name>();
  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. SemanticsNode::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 SemanticsHandleStructLiteral(SemanticsContext& context,
  54. ParseTree::Node parse_node) -> bool {
  55. auto refs_id = context.ParamOrArgEnd(
  56. /*for_args=*/true, ParseNodeKind::StructLiteralOrStructTypeLiteralStart);
  57. context.PopScope();
  58. context.node_stack()
  59. .PopAndDiscardSoloParseNode<
  60. ParseNodeKind::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. SemanticsNode::StructValue::Make(parse_node, type_id, refs_id));
  65. context.node_stack().Push(parse_node, value_id);
  66. return true;
  67. }
  68. auto SemanticsHandleStructLiteralOrStructTypeLiteralStart(
  69. SemanticsContext& context, ParseTree::Node parse_node) -> bool {
  70. context.PushScope();
  71. context.node_stack().Push(parse_node);
  72. // At this point we aren't sure whether this will be a value or type literal,
  73. // so we push onto args irrespective. It just won't be used for a type
  74. // literal.
  75. context.args_type_info_stack().Push();
  76. context.ParamOrArgStart();
  77. return true;
  78. }
  79. auto SemanticsHandleStructTypeLiteral(SemanticsContext& context,
  80. ParseTree::Node parse_node) -> bool {
  81. auto refs_id = context.ParamOrArgEnd(
  82. /*for_args=*/false, ParseNodeKind::StructLiteralOrStructTypeLiteralStart);
  83. context.PopScope();
  84. context.node_stack()
  85. .PopAndDiscardSoloParseNode<
  86. ParseNodeKind::StructLiteralOrStructTypeLiteralStart>();
  87. // This is only used for value literals.
  88. context.args_type_info_stack().Pop();
  89. CARBON_CHECK(refs_id != SemanticsNodeBlockId::Empty)
  90. << "{} is handled by StructLiteral.";
  91. context.AddNodeAndPush(parse_node,
  92. SemanticsNode::StructType::Make(
  93. parse_node, SemanticsTypeId::TypeType, refs_id));
  94. return true;
  95. }
  96. } // namespace Carbon