sem_ir_diagnostic_converter.cpp 6.9 KB

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