sem_ir_diagnostic_converter.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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/sem_ir_diagnostic_converter.h"
  5. namespace Carbon::Check {
  6. auto SemIRDiagnosticConverter::ConvertLoc(SemIRLoc loc,
  7. ContextFnT context_fn) const
  8. -> DiagnosticLoc {
  9. // Cursors for the current IR and instruction in that IR.
  10. const auto* cursor_ir = sem_ir_;
  11. auto cursor_inst_id = SemIR::InstId::Invalid;
  12. // Notes an import on the diagnostic and updates cursors to point at the
  13. // imported IR.
  14. auto follow_import_ref = [&](SemIR::ImportIRInstId import_ir_inst_id) {
  15. auto import_ir_inst = cursor_ir->import_ir_insts().Get(import_ir_inst_id);
  16. const auto& import_ir = cursor_ir->import_irs().Get(import_ir_inst.ir_id);
  17. auto context_loc = ConvertLocInFile(cursor_ir, import_ir.node_id,
  18. loc.token_only, context_fn);
  19. CARBON_DIAGNOSTIC(InImport, Note, "In import.");
  20. context_fn(context_loc, InImport);
  21. cursor_ir = import_ir.sem_ir;
  22. cursor_inst_id = import_ir_inst.inst_id;
  23. };
  24. // If the location is is an import, follows it and returns nullopt.
  25. // Otherwise, it's a parse node, so return the final location.
  26. auto handle_loc = [&](SemIR::LocId loc_id) -> std::optional<DiagnosticLoc> {
  27. if (loc_id.is_import_ir_inst_id()) {
  28. follow_import_ref(loc_id.import_ir_inst_id());
  29. return std::nullopt;
  30. } else {
  31. // Parse nodes always refer to the current IR.
  32. return ConvertLocInFile(cursor_ir, loc_id.node_id(), loc.token_only,
  33. context_fn);
  34. }
  35. };
  36. // Handle the base location.
  37. if (loc.is_inst_id) {
  38. cursor_inst_id = loc.inst_id;
  39. } else {
  40. if (auto diag_loc = handle_loc(loc.loc_id)) {
  41. return *diag_loc;
  42. }
  43. CARBON_CHECK(cursor_inst_id.is_valid()) << "Should have been set";
  44. }
  45. while (true) {
  46. if (cursor_inst_id.is_valid()) {
  47. auto cursor_inst = cursor_ir->insts().Get(cursor_inst_id);
  48. if (auto bind_ref = cursor_inst.TryAs<SemIR::ExportDecl>();
  49. bind_ref && bind_ref->value_id.is_valid()) {
  50. cursor_inst_id = bind_ref->value_id;
  51. continue;
  52. }
  53. // If the parse node is valid, use it for the location.
  54. if (auto loc_id = cursor_ir->insts().GetLocId(cursor_inst_id);
  55. loc_id.is_valid()) {
  56. if (auto diag_loc = handle_loc(loc_id)) {
  57. return *diag_loc;
  58. }
  59. continue;
  60. }
  61. // If a namespace has an instruction for an import, switch to looking at
  62. // it.
  63. if (auto ns = cursor_inst.TryAs<SemIR::Namespace>()) {
  64. if (ns->import_id.is_valid()) {
  65. cursor_inst_id = ns->import_id;
  66. continue;
  67. }
  68. }
  69. }
  70. // Invalid parse node but not an import; just nothing to point at.
  71. return ConvertLocInFile(cursor_ir, Parse::NodeId::Invalid, loc.token_only,
  72. context_fn);
  73. }
  74. }
  75. auto SemIRDiagnosticConverter::ConvertArg(llvm::Any arg) const -> llvm::Any {
  76. if (auto* name_id = llvm::any_cast<SemIR::NameId>(&arg)) {
  77. return sem_ir_->names().GetFormatted(*name_id).str();
  78. }
  79. if (auto* type_id = llvm::any_cast<SemIR::TypeId>(&arg)) {
  80. return sem_ir_->StringifyType(*type_id);
  81. }
  82. if (auto* typed_int = llvm::any_cast<TypedInt>(&arg)) {
  83. return llvm::APSInt(typed_int->value,
  84. !sem_ir_->types().IsSignedInt(typed_int->type));
  85. }
  86. return DiagnosticConverter<SemIRLoc>::ConvertArg(arg);
  87. }
  88. auto SemIRDiagnosticConverter::ConvertLocInFile(const SemIR::File* sem_ir,
  89. Parse::NodeId node_id,
  90. bool token_only,
  91. ContextFnT context_fn) const
  92. -> DiagnosticLoc {
  93. return node_converters_[sem_ir->check_ir_id().index]->ConvertLoc(
  94. Parse::NodeLoc(node_id, token_only), context_fn);
  95. }
  96. } // namespace Carbon::Check