sem_ir_diagnostic_converter.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. CARBON_CHECK(import_ir.decl_id.is_valid(),
  18. "If we get invalid locations here, we may need to more "
  19. "thoroughly track ImportDecls.");
  20. DiagnosticLoc in_import_loc;
  21. auto import_loc_id = cursor_ir->insts().GetLocId(import_ir.decl_id);
  22. if (import_loc_id.is_node_id()) {
  23. // For imports in the current file, the location is simple.
  24. in_import_loc = ConvertLocInFile(cursor_ir, import_loc_id.node_id(),
  25. loc.token_only, context_fn);
  26. } else if (import_loc_id.is_import_ir_inst_id()) {
  27. // For implicit imports, we need to unravel the location a little
  28. // further.
  29. auto implicit_import_ir_inst =
  30. cursor_ir->import_ir_insts().Get(import_loc_id.import_ir_inst_id());
  31. const auto& implicit_ir =
  32. cursor_ir->import_irs().Get(implicit_import_ir_inst.ir_id);
  33. auto implicit_loc_id =
  34. implicit_ir.sem_ir->insts().GetLocId(implicit_import_ir_inst.inst_id);
  35. CARBON_CHECK(implicit_loc_id.is_node_id(),
  36. "Should only be one layer of implicit imports");
  37. in_import_loc =
  38. ConvertLocInFile(implicit_ir.sem_ir, implicit_loc_id.node_id(),
  39. loc.token_only, context_fn);
  40. }
  41. // TODO: Add an "In implicit import of prelude." note for the case where we
  42. // don't have a location.
  43. if (import_loc_id.is_valid()) {
  44. CARBON_DIAGNOSTIC(InImport, Note, "in import");
  45. context_fn(in_import_loc, InImport);
  46. }
  47. cursor_ir = import_ir.sem_ir;
  48. cursor_inst_id = import_ir_inst.inst_id;
  49. };
  50. // If the location is is an import, follows it and returns nullopt.
  51. // Otherwise, it's a parse node, so return the final location.
  52. auto handle_loc = [&](SemIR::LocId loc_id) -> std::optional<DiagnosticLoc> {
  53. if (loc_id.is_import_ir_inst_id()) {
  54. follow_import_ref(loc_id.import_ir_inst_id());
  55. return std::nullopt;
  56. } else {
  57. // Parse nodes always refer to the current IR.
  58. return ConvertLocInFile(cursor_ir, loc_id.node_id(), loc.token_only,
  59. context_fn);
  60. }
  61. };
  62. // Handle the base location.
  63. if (loc.is_inst_id) {
  64. cursor_inst_id = loc.inst_id;
  65. } else {
  66. if (auto diag_loc = handle_loc(loc.loc_id)) {
  67. return *diag_loc;
  68. }
  69. CARBON_CHECK(cursor_inst_id.is_valid(), "Should have been set");
  70. }
  71. while (true) {
  72. if (cursor_inst_id.is_valid()) {
  73. auto cursor_inst = cursor_ir->insts().Get(cursor_inst_id);
  74. if (auto bind_ref = cursor_inst.TryAs<SemIR::ExportDecl>();
  75. bind_ref && bind_ref->value_id.is_valid()) {
  76. cursor_inst_id = bind_ref->value_id;
  77. continue;
  78. }
  79. // If the parse node is valid, use it for the location.
  80. if (auto loc_id = cursor_ir->insts().GetLocId(cursor_inst_id);
  81. loc_id.is_valid()) {
  82. if (auto diag_loc = handle_loc(loc_id)) {
  83. return *diag_loc;
  84. }
  85. continue;
  86. }
  87. // If a namespace has an instruction for an import, switch to looking at
  88. // it.
  89. if (auto ns = cursor_inst.TryAs<SemIR::Namespace>()) {
  90. if (ns->import_id.is_valid()) {
  91. cursor_inst_id = ns->import_id;
  92. continue;
  93. }
  94. }
  95. }
  96. // Invalid parse node but not an import; just nothing to point at.
  97. return ConvertLocInFile(cursor_ir, Parse::NodeId::Invalid, loc.token_only,
  98. context_fn);
  99. }
  100. }
  101. auto SemIRDiagnosticConverter::ConvertArg(llvm::Any arg) const -> llvm::Any {
  102. if (auto* library_name_id = llvm::any_cast<SemIR::LibraryNameId>(&arg)) {
  103. std::string library_name;
  104. if (*library_name_id == SemIR::LibraryNameId::Default) {
  105. library_name = "default library";
  106. } else if (!library_name_id->is_valid()) {
  107. library_name = "library <invalid>";
  108. } else {
  109. llvm::raw_string_ostream stream(library_name);
  110. stream << "library \"";
  111. stream << sem_ir_->string_literal_values().Get(
  112. library_name_id->AsStringLiteralValueId());
  113. stream << "\"";
  114. }
  115. return library_name;
  116. }
  117. if (auto* name_id = llvm::any_cast<SemIR::NameId>(&arg)) {
  118. return sem_ir_->names().GetFormatted(*name_id).str();
  119. }
  120. if (auto* type_id = llvm::any_cast<SemIR::TypeId>(&arg)) {
  121. return sem_ir_->StringifyType(*type_id);
  122. }
  123. if (auto* typed_int = llvm::any_cast<TypedInt>(&arg)) {
  124. return llvm::APSInt(typed_int->value,
  125. !sem_ir_->types().IsSignedInt(typed_int->type));
  126. }
  127. return DiagnosticConverter<SemIRLoc>::ConvertArg(arg);
  128. }
  129. auto SemIRDiagnosticConverter::ConvertLocInFile(const SemIR::File* sem_ir,
  130. Parse::NodeId node_id,
  131. bool token_only,
  132. ContextFnT context_fn) const
  133. -> DiagnosticLoc {
  134. return node_converters_[sem_ir->check_ir_id().index].ConvertLoc(
  135. Parse::NodeLoc(node_id, token_only), context_fn);
  136. }
  137. } // namespace Carbon::Check