diagnostic_helpers.h 5.2 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. #ifndef CARBON_TOOLCHAIN_CHECK_DIAGNOSTIC_HELPERS_H_
  5. #define CARBON_TOOLCHAIN_CHECK_DIAGNOSTIC_HELPERS_H_
  6. #include <concepts>
  7. #include "llvm/ADT/APSInt.h"
  8. #include "toolchain/parse/node_ids.h"
  9. #include "toolchain/sem_ir/ids.h"
  10. namespace Carbon::Check {
  11. // The `DiagnosticEmitterBase` is templated on this type so that
  12. // diagnostics can be passed an `InstId` as a location, without having to
  13. // explicitly construct a `LocId` from it first.
  14. class LocIdForDiagnostics {
  15. public:
  16. template <class LocT>
  17. requires std::constructible_from<SemIR::LocId, LocT>
  18. // NOLINTNEXTLINE(google-explicit-constructor)
  19. LocIdForDiagnostics(LocT loc_id) : loc_id_(SemIR::LocId(loc_id)) {}
  20. explicit operator SemIR::LocId() const { return loc_id_; }
  21. private:
  22. SemIR::LocId loc_id_;
  23. };
  24. // We define the emitter separately for dependencies, so only provide a base
  25. // here.
  26. using DiagnosticEmitterBase = Diagnostics::Emitter<LocIdForDiagnostics>;
  27. using DiagnosticBuilder = DiagnosticEmitterBase::Builder;
  28. // A function that forms a diagnostic for some kind of problem. The
  29. // DiagnosticBuilder is returned rather than emitted so that the caller
  30. // can add contextual notes as appropriate.
  31. using MakeDiagnosticBuilderFn = llvm::function_ref<auto()->DiagnosticBuilder>;
  32. // An expression with a constant value, for rendering in a diagnostic. The
  33. // diagnostic rendering will include enclosing "`"s.
  34. struct InstIdAsConstant {
  35. using DiagnosticType = Diagnostics::TypeInfo<std::string>;
  36. // NOLINTNEXTLINE(google-explicit-constructor)
  37. InstIdAsConstant(SemIR::InstId inst_id) : inst_id(inst_id) {}
  38. SemIR::InstId inst_id;
  39. };
  40. // An expression whose type should be rendered in a diagnostic. The diagnostic
  41. // rendering will include enclosing "`"s, and may also include extra information
  42. // about the type if it might otherwise be ambiguous or context-dependent, such
  43. // as the targets of aliases used in the type.
  44. //
  45. // TODO: Include such additional information where relevant. For example:
  46. // "`StdString` (aka `Cpp.std.basic_string(Char)`)".
  47. //
  48. // This should be used instead of `TypeId` as a diagnostic argument wherever
  49. // possible, because we should eventually be able to produce a sugared type name
  50. // in this case, whereas a `TypeId` will render as a canonical type.
  51. struct TypeOfInstId {
  52. using DiagnosticType = Diagnostics::TypeInfo<std::string>;
  53. // NOLINTNEXTLINE(google-explicit-constructor)
  54. TypeOfInstId(SemIR::InstId inst_id) : inst_id(inst_id) {}
  55. SemIR::InstId inst_id;
  56. };
  57. // A type expression, for rendering in a diagnostic. The diagnostic rendering
  58. // will include enclosing "`"s, and may also include extra information about the
  59. // type if it would otherwise be ambiguous.
  60. //
  61. // TODO: Include such additional information where relevant.
  62. //
  63. // This should be used when the source expression used to construct a type is
  64. // available.
  65. //
  66. // Note that this is currently an alias for InstIdAsConstant. However, using
  67. // InstIdAsType is clearer when defining CARBON_DIAGNOSTICs, and we may wish to
  68. // distinguish type arguments in diagnostics from more general constants in some
  69. // way in the future.
  70. using InstIdAsType = InstIdAsConstant;
  71. // A type expression, for rendering in a diagnostic as a raw type. When
  72. // formatting as a raw type in a diagnostic, the type will be formatted as a
  73. // simple Carbon expression, without enclosing "`"s. Once we start including
  74. // extra information about types, such annotations will also not be included for
  75. // raw types.
  76. //
  77. // This is intended for cases where the type is part of a larger syntactic
  78. // construct in a diagnostic, such as "redefinition of `impl {0} as {1}`".
  79. struct InstIdAsRawType {
  80. using DiagnosticType = Diagnostics::TypeInfo<std::string>;
  81. // NOLINTNEXTLINE(google-explicit-constructor)
  82. InstIdAsRawType(SemIR::InstId inst_id) : inst_id(inst_id) {}
  83. SemIR::InstId inst_id;
  84. };
  85. // A type value for rendering in a diagnostic without enclosing "`"s. See
  86. // `InstIdAsRawType` for details on raw type formatting.
  87. //
  88. // As with `TypeId`, this should be avoided as a diagnostic argument where
  89. // possible, because it can't be formatted with syntactic sugar such as aliases
  90. // that describe how the type was written.
  91. struct TypeIdAsRawType {
  92. using DiagnosticType = Diagnostics::TypeInfo<std::string>;
  93. // NOLINTNEXTLINE(google-explicit-constructor)
  94. TypeIdAsRawType(SemIR::TypeId type_id) : type_id(type_id) {}
  95. SemIR::TypeId type_id;
  96. };
  97. // An integer value together with its type. The type is used to determine how to
  98. // format the value in diagnostics.
  99. struct TypedInt {
  100. using DiagnosticType = Diagnostics::TypeInfo<llvm::APSInt>;
  101. SemIR::TypeId type;
  102. llvm::APInt value;
  103. };
  104. struct SpecificInterfaceIdAsRawType {
  105. using DiagnosticType = Diagnostics::TypeInfo<std::string>;
  106. // NOLINTNEXTLINE(google-explicit-constructor)
  107. SpecificInterfaceIdAsRawType(SemIR::SpecificInterfaceId specific_interface_id)
  108. : specific_interface_id(specific_interface_id) {}
  109. SemIR::SpecificInterfaceId specific_interface_id;
  110. };
  111. } // namespace Carbon::Check
  112. #endif // CARBON_TOOLCHAIN_CHECK_DIAGNOSTIC_HELPERS_H_