semantics_handle_struct.cpp 4.4 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 {
  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] =
  26. context.node_stack().PopWithParseNode<SemanticsNodeId>();
  27. SemanticsTypeId cast_type_id = context.ExpressionAsType(type_node, type_id);
  28. auto [name_node, name_id] =
  29. context.node_stack().PopWithParseNode<SemanticsStringId>(
  30. ParseNodeKind::Name);
  31. context.AddNode(
  32. SemanticsNode::StructTypeField::Make(name_node, cast_type_id, name_id));
  33. context.node_stack().Push(parse_node);
  34. return true;
  35. }
  36. auto SemanticsHandleStructFieldUnknown(SemanticsContext& context,
  37. ParseTree::Node parse_node) -> bool {
  38. return context.TODO(parse_node, "HandleStructFieldUnknown");
  39. }
  40. auto SemanticsHandleStructFieldValue(SemanticsContext& context,
  41. ParseTree::Node parse_node) -> bool {
  42. auto [value_parse_node, value_node_id] =
  43. context.node_stack().PopWithParseNode<SemanticsNodeId>();
  44. auto name_id =
  45. context.node_stack().Pop<SemanticsStringId>(ParseNodeKind::Name);
  46. // Store the name for the type.
  47. auto type_block_id = context.args_type_info_stack().PeekForAdd();
  48. context.semantics_ir().AddNode(
  49. type_block_id,
  50. SemanticsNode::StructTypeField::Make(
  51. parse_node, context.semantics_ir().GetNode(value_node_id).type_id(),
  52. name_id));
  53. // Push the value back on the stack as an argument.
  54. context.node_stack().Push(parse_node, value_node_id);
  55. return true;
  56. }
  57. auto SemanticsHandleStructLiteral(SemanticsContext& context,
  58. ParseTree::Node parse_node) -> bool {
  59. auto refs_id = context.ParamOrArgEnd(
  60. /*for_args=*/true, ParseNodeKind::StructLiteralOrStructTypeLiteralStart);
  61. context.PopScope();
  62. context.node_stack().PopAndDiscardSoloParseNode(
  63. ParseNodeKind::StructLiteralOrStructTypeLiteralStart);
  64. auto type_block_id = context.args_type_info_stack().Pop();
  65. auto type_id = context.CanonicalizeStructType(parse_node, type_block_id);
  66. auto value_id = context.AddNode(
  67. SemanticsNode::StructValue::Make(parse_node, type_id, refs_id));
  68. context.node_stack().Push(parse_node, value_id);
  69. return true;
  70. }
  71. auto SemanticsHandleStructLiteralOrStructTypeLiteralStart(
  72. SemanticsContext& context, ParseTree::Node parse_node) -> 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 SemanticsHandleStructTypeLiteral(SemanticsContext& context,
  83. ParseTree::Node parse_node) -> bool {
  84. auto refs_id = context.ParamOrArgEnd(
  85. /*for_args=*/false, ParseNodeKind::StructLiteralOrStructTypeLiteralStart);
  86. context.PopScope();
  87. context.node_stack().PopAndDiscardSoloParseNode(
  88. ParseNodeKind::StructLiteralOrStructTypeLiteralStart);
  89. // This is only used for value literals.
  90. context.args_type_info_stack().Pop();
  91. CARBON_CHECK(refs_id != SemanticsNodeBlockId::Empty)
  92. << "{} is handled by StructLiteral.";
  93. auto type_id = context.CanonicalizeStructType(parse_node, refs_id);
  94. context.node_stack().Push(parse_node,
  95. context.semantics_ir().GetType(type_id));
  96. return true;
  97. }
  98. } // namespace Carbon