handle_struct.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #include "toolchain/check/convert.h"
  6. namespace Carbon::Check {
  7. auto HandleStructComma(Context& context, Parse::NodeId /*parse_node*/) -> bool {
  8. context.ParamOrArgComma();
  9. return true;
  10. }
  11. auto HandleStructFieldDesignator(Context& context, Parse::NodeId /*parse_node*/)
  12. -> bool {
  13. // This leaves the designated name on top because the `.` isn't interesting.
  14. CARBON_CHECK(context.node_stack().PeekIsName());
  15. return true;
  16. }
  17. auto HandleStructFieldType(Context& context, Parse::NodeId parse_node) -> bool {
  18. auto [type_node, type_id] = context.node_stack().PopExprWithParseNode();
  19. SemIR::TypeId cast_type_id = ExprAsType(context, type_node, type_id);
  20. auto [name_node, name_id] = context.node_stack().PopNameWithParseNode();
  21. context.AddInstAndPush(
  22. parse_node, SemIR::StructTypeField{name_node, name_id, cast_type_id});
  23. return true;
  24. }
  25. auto HandleStructFieldValue(Context& context, Parse::NodeId parse_node)
  26. -> bool {
  27. auto value_inst_id = context.node_stack().PopExpr();
  28. auto [name_node, name_id] = context.node_stack().PopNameWithParseNode();
  29. // Store the name for the type.
  30. context.args_type_info_stack().AddInst(SemIR::StructTypeField{
  31. name_node, name_id, context.insts().Get(value_inst_id).type_id()});
  32. // Push the value back on the stack as an argument.
  33. context.node_stack().Push(parse_node, value_inst_id);
  34. return true;
  35. }
  36. static auto DiagnoseDuplicateNames(Context& context,
  37. SemIR::InstBlockId type_block_id,
  38. llvm::StringRef construct) -> bool {
  39. auto& sem_ir = context.sem_ir();
  40. auto fields = sem_ir.inst_blocks().Get(type_block_id);
  41. llvm::SmallDenseMap<SemIR::NameId, Parse::NodeId> names;
  42. auto& insts = sem_ir.insts();
  43. for (SemIR::InstId field_inst_id : fields) {
  44. auto field_inst = insts.GetAs<SemIR::StructTypeField>(field_inst_id);
  45. auto [it, added] =
  46. names.insert({field_inst.name_id, field_inst.parse_node});
  47. if (!added) {
  48. CARBON_DIAGNOSTIC(StructNameDuplicate, Error,
  49. "Duplicated field name `{1}` in {0}.", std::string,
  50. std::string);
  51. CARBON_DIAGNOSTIC(StructNamePrevious, Note,
  52. "Field with the same name here.");
  53. context.emitter()
  54. .Build(field_inst.parse_node, StructNameDuplicate, construct.str(),
  55. sem_ir.names().GetFormatted(field_inst.name_id).str())
  56. .Note(it->second, StructNamePrevious)
  57. .Emit();
  58. return true;
  59. }
  60. }
  61. return false;
  62. }
  63. auto HandleStructLiteral(Context& context, Parse::NodeId parse_node) -> bool {
  64. auto refs_id = context.ParamOrArgEnd(
  65. Parse::NodeKind::StructLiteralOrStructTypeLiteralStart);
  66. context.PopScope();
  67. context.node_stack()
  68. .PopAndDiscardSoloParseNode<
  69. Parse::NodeKind::StructLiteralOrStructTypeLiteralStart>();
  70. auto type_block_id = context.args_type_info_stack().Pop();
  71. if (DiagnoseDuplicateNames(context, type_block_id, "struct literal")) {
  72. context.node_stack().Push(parse_node, SemIR::InstId::BuiltinError);
  73. return true;
  74. }
  75. auto type_id = context.CanonicalizeStructType(parse_node, type_block_id);
  76. auto value_id =
  77. context.AddInst(SemIR::StructLiteral{parse_node, type_id, refs_id});
  78. context.node_stack().Push(parse_node, value_id);
  79. return true;
  80. }
  81. auto HandleStructLiteralOrStructTypeLiteralStart(Context& context,
  82. Parse::NodeId parse_node)
  83. -> bool {
  84. context.PushScope();
  85. context.node_stack().Push(parse_node);
  86. // At this point we aren't sure whether this will be a value or type literal,
  87. // so we push onto args irrespective. It just won't be used for a type
  88. // literal.
  89. context.args_type_info_stack().Push();
  90. context.ParamOrArgStart();
  91. return true;
  92. }
  93. auto HandleStructTypeLiteral(Context& context, Parse::NodeId parse_node)
  94. -> bool {
  95. auto refs_id = context.ParamOrArgEnd(
  96. Parse::NodeKind::StructLiteralOrStructTypeLiteralStart);
  97. context.PopScope();
  98. context.node_stack()
  99. .PopAndDiscardSoloParseNode<
  100. Parse::NodeKind::StructLiteralOrStructTypeLiteralStart>();
  101. // This is only used for value literals.
  102. context.args_type_info_stack().Pop();
  103. CARBON_CHECK(refs_id != SemIR::InstBlockId::Empty)
  104. << "{} is handled by StructLiteral.";
  105. if (DiagnoseDuplicateNames(context, refs_id, "struct type literal")) {
  106. context.node_stack().Push(parse_node, SemIR::InstId::BuiltinError);
  107. return true;
  108. }
  109. context.AddInstAndPush(
  110. parse_node,
  111. SemIR::StructType{parse_node, SemIR::TypeId::TypeType, refs_id});
  112. return true;
  113. }
  114. } // namespace Carbon::Check