sem_ir_diagnostic_converter.cpp 7.8 KB

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