handle_struct.cpp 5.1 KB

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