handle_struct.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #include "toolchain/check/handle.h"
  7. namespace Carbon::Check {
  8. auto HandleStructTypeLiteralStart(Context& context,
  9. Parse::StructTypeLiteralStartId node_id)
  10. -> bool {
  11. context.scope_stack().Push();
  12. context.node_stack().Push(node_id);
  13. context.param_and_arg_refs_stack().Push();
  14. return true;
  15. }
  16. auto HandleStructLiteralStart(Context& context,
  17. Parse::StructLiteralStartId node_id) -> bool {
  18. context.scope_stack().Push();
  19. context.node_stack().Push(node_id);
  20. context.args_type_info_stack().Push();
  21. context.param_and_arg_refs_stack().Push();
  22. return true;
  23. }
  24. auto HandleStructFieldDesignator(Context& context,
  25. Parse::StructFieldDesignatorId /*node_id*/)
  26. -> bool {
  27. // This leaves the designated name on top because the `.` isn't interesting.
  28. CARBON_CHECK(context.node_stack().PeekIsName());
  29. return true;
  30. }
  31. auto HandleStructComma(Context& context, Parse::StructCommaId /*node_id*/)
  32. -> bool {
  33. context.param_and_arg_refs_stack().ApplyComma();
  34. return true;
  35. }
  36. auto HandleStructField(Context& context, Parse::StructFieldId node_id) -> bool {
  37. auto value_inst_id = context.node_stack().PopExpr();
  38. auto [name_node, name_id] = context.node_stack().PopNameWithNodeId();
  39. // Store the name for the type.
  40. context.args_type_info_stack().AddInstId(
  41. context.AddInstInNoBlock<SemIR::StructTypeField>(
  42. name_node,
  43. {.name_id = name_id,
  44. .field_type_id = context.insts().Get(value_inst_id).type_id()}));
  45. // Push the value back on the stack as an argument.
  46. context.node_stack().Push(node_id, value_inst_id);
  47. return true;
  48. }
  49. auto HandleStructTypeField(Context& context, Parse::StructTypeFieldId node_id)
  50. -> bool {
  51. auto [type_node, type_id] = context.node_stack().PopExprWithNodeId();
  52. SemIR::TypeId cast_type_id = ExprAsType(context, type_node, type_id);
  53. auto [name_node, name_id] = context.node_stack().PopNameWithNodeId();
  54. auto inst_id = context.AddInst<SemIR::StructTypeField>(
  55. name_node, {.name_id = name_id, .field_type_id = cast_type_id});
  56. context.node_stack().Push(node_id, inst_id);
  57. return true;
  58. }
  59. static auto DiagnoseDuplicateNames(Context& context,
  60. SemIR::InstBlockId type_block_id,
  61. llvm::StringRef construct) -> bool {
  62. auto& sem_ir = context.sem_ir();
  63. auto fields = sem_ir.inst_blocks().Get(type_block_id);
  64. llvm::SmallDenseMap<SemIR::NameId, SemIR::InstId> names;
  65. auto& insts = sem_ir.insts();
  66. for (SemIR::InstId field_inst_id : fields) {
  67. auto field_inst = insts.GetAs<SemIR::StructTypeField>(field_inst_id);
  68. auto [it, added] = names.insert({field_inst.name_id, field_inst_id});
  69. if (!added) {
  70. CARBON_DIAGNOSTIC(StructNameDuplicate, Error,
  71. "Duplicated field name `{1}` in {0}.", std::string,
  72. SemIR::NameId);
  73. CARBON_DIAGNOSTIC(StructNamePrevious, Note,
  74. "Field with the same name here.");
  75. context.emitter()
  76. .Build(field_inst_id, StructNameDuplicate, construct.str(),
  77. field_inst.name_id)
  78. .Note(it->second, StructNamePrevious)
  79. .Emit();
  80. return true;
  81. }
  82. }
  83. return false;
  84. }
  85. auto HandleStructLiteral(Context& context, Parse::StructLiteralId node_id)
  86. -> bool {
  87. auto refs_id = context.param_and_arg_refs_stack().EndAndPop(
  88. Parse::NodeKind::StructLiteralStart);
  89. context.scope_stack().Pop();
  90. context.node_stack()
  91. .PopAndDiscardSoloNodeId<Parse::NodeKind::StructLiteralStart>();
  92. auto type_block_id = context.args_type_info_stack().Pop();
  93. if (DiagnoseDuplicateNames(context, type_block_id, "struct literal")) {
  94. context.node_stack().Push(node_id, SemIR::InstId::BuiltinError);
  95. return true;
  96. }
  97. auto type_id = context.GetStructType(type_block_id);
  98. auto value_id = context.AddInst<SemIR::StructLiteral>(
  99. node_id, {.type_id = type_id, .elements_id = refs_id});
  100. context.node_stack().Push(node_id, value_id);
  101. return true;
  102. }
  103. auto HandleStructTypeLiteral(Context& context,
  104. Parse::StructTypeLiteralId node_id) -> bool {
  105. auto refs_id = context.param_and_arg_refs_stack().EndAndPop(
  106. Parse::NodeKind::StructTypeLiteralStart);
  107. context.scope_stack().Pop();
  108. context.node_stack()
  109. .PopAndDiscardSoloNodeId<Parse::NodeKind::StructTypeLiteralStart>();
  110. CARBON_CHECK(refs_id != SemIR::InstBlockId::Empty)
  111. << "{} is handled by StructLiteral.";
  112. if (DiagnoseDuplicateNames(context, refs_id, "struct type literal")) {
  113. context.node_stack().Push(node_id, SemIR::InstId::BuiltinError);
  114. return true;
  115. }
  116. context.AddInstAndPush<SemIR::StructType>(
  117. node_id, {.type_id = SemIR::TypeId::TypeType, .fields_id = refs_id});
  118. return true;
  119. }
  120. } // namespace Carbon::Check