handle_struct.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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(SemIR::StructTypeField{
  37. name_node, name_id, context.insts().Get(value_inst_id).type_id()});
  38. // Push the value back on the stack as an argument.
  39. context.node_stack().Push(parse_node, value_inst_id);
  40. return true;
  41. }
  42. auto HandleStructFieldType(Context& context,
  43. Parse::StructFieldTypeId parse_node) -> bool {
  44. auto [type_node, type_id] = context.node_stack().PopExprWithParseNode();
  45. SemIR::TypeId cast_type_id = ExprAsType(context, type_node, type_id);
  46. auto [name_node, name_id] = context.node_stack().PopNameWithParseNode();
  47. context.AddInstAndPush(
  48. parse_node, SemIR::StructTypeField{name_node, name_id, cast_type_id});
  49. return true;
  50. }
  51. static auto DiagnoseDuplicateNames(Context& context,
  52. SemIR::InstBlockId type_block_id,
  53. llvm::StringRef construct) -> bool {
  54. auto& sem_ir = context.sem_ir();
  55. auto fields = sem_ir.inst_blocks().Get(type_block_id);
  56. llvm::SmallDenseMap<SemIR::NameId, Parse::NodeId> names;
  57. auto& insts = sem_ir.insts();
  58. for (SemIR::InstId field_inst_id : fields) {
  59. auto field_inst = insts.GetAs<SemIR::StructTypeField>(field_inst_id);
  60. auto [it, added] =
  61. names.insert({field_inst.name_id, field_inst.parse_node});
  62. if (!added) {
  63. CARBON_DIAGNOSTIC(StructNameDuplicate, Error,
  64. "Duplicated field name `{1}` in {0}.", std::string,
  65. std::string);
  66. CARBON_DIAGNOSTIC(StructNamePrevious, Note,
  67. "Field with the same name here.");
  68. context.emitter()
  69. .Build(field_inst.parse_node, StructNameDuplicate, construct.str(),
  70. sem_ir.names().GetFormatted(field_inst.name_id).str())
  71. .Note(it->second, StructNamePrevious)
  72. .Emit();
  73. return true;
  74. }
  75. }
  76. return false;
  77. }
  78. auto HandleStructLiteral(Context& context, Parse::StructLiteralId parse_node)
  79. -> bool {
  80. auto refs_id = context.ParamOrArgEnd(
  81. Parse::NodeKind::StructLiteralOrStructTypeLiteralStart);
  82. context.PopScope();
  83. context.node_stack()
  84. .PopAndDiscardSoloParseNode<
  85. Parse::NodeKind::StructLiteralOrStructTypeLiteralStart>();
  86. auto type_block_id = context.args_type_info_stack().Pop();
  87. if (DiagnoseDuplicateNames(context, type_block_id, "struct literal")) {
  88. context.node_stack().Push(parse_node, SemIR::InstId::BuiltinError);
  89. return true;
  90. }
  91. auto type_id = context.CanonicalizeStructType(parse_node, type_block_id);
  92. auto value_id =
  93. context.AddInst(SemIR::StructLiteral{parse_node, type_id, refs_id});
  94. context.node_stack().Push(parse_node, value_id);
  95. return true;
  96. }
  97. auto HandleStructTypeLiteral(Context& context,
  98. Parse::StructTypeLiteralId parse_node) -> bool {
  99. auto refs_id = context.ParamOrArgEnd(
  100. Parse::NodeKind::StructLiteralOrStructTypeLiteralStart);
  101. context.PopScope();
  102. context.node_stack()
  103. .PopAndDiscardSoloParseNode<
  104. Parse::NodeKind::StructLiteralOrStructTypeLiteralStart>();
  105. // This is only used for value literals.
  106. context.args_type_info_stack().Pop();
  107. CARBON_CHECK(refs_id != SemIR::InstBlockId::Empty)
  108. << "{} is handled by StructLiteral.";
  109. if (DiagnoseDuplicateNames(context, refs_id, "struct type literal")) {
  110. context.node_stack().Push(parse_node, SemIR::InstId::BuiltinError);
  111. return true;
  112. }
  113. context.AddInstAndPush(
  114. parse_node,
  115. SemIR::StructType{parse_node, SemIR::TypeId::TypeType, refs_id});
  116. return true;
  117. }
  118. } // namespace Carbon::Check