diagnostic_helpers.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 "llvm/ADT/APSInt.h"
  7. #include "toolchain/parse/node_ids.h"
  8. #include "toolchain/sem_ir/ids.h"
  9. namespace Carbon::Check {
  10. // Tracks a location for diagnostic use, which is either a parse node or an inst
  11. // ID which can be translated to a parse node. Used when code needs to support
  12. // multiple possible ways of reporting a diagnostic location.
  13. class SemIRLoc {
  14. public:
  15. // NOLINTNEXTLINE(google-explicit-constructor)
  16. SemIRLoc(SemIR::InstId inst_id)
  17. : inst_id_(inst_id), is_inst_id_(true), token_only_(false) {}
  18. // NOLINTNEXTLINE(google-explicit-constructor)
  19. SemIRLoc(Parse::NodeId node_id) : SemIRLoc(node_id, false) {}
  20. // NOLINTNEXTLINE(google-explicit-constructor)
  21. SemIRLoc(SemIR::LocId loc_id) : SemIRLoc(loc_id, false) {}
  22. // If `token_only` is true, refers to the specific node; otherwise, refers to
  23. // the node and its children.
  24. explicit SemIRLoc(SemIR::LocId loc_id, bool token_only)
  25. : loc_id_(loc_id), is_inst_id_(false), token_only_(token_only) {}
  26. private:
  27. // Only allow member access for diagnostics.
  28. friend class SemIRDiagnosticConverter;
  29. union {
  30. SemIR::InstId inst_id_;
  31. SemIR::LocId loc_id_;
  32. };
  33. bool is_inst_id_;
  34. bool token_only_;
  35. };
  36. inline auto TokenOnly(SemIR::LocId loc_id) -> SemIRLoc {
  37. return SemIRLoc(loc_id, true);
  38. }
  39. // An expression whose type should be rendered in a diagnostic. The diagnostic
  40. // rendering will include enclosing "`"s, and may also include extra information
  41. // about the type if it might otherwise be ambiguous or context-dependent, such
  42. // as the targets of aliases used in the type.
  43. //
  44. // TODO: Include such additional information where relevant. For example:
  45. // "`StdString` (aka `Cpp.std.basic_string(Char)`)".
  46. //
  47. // This should be used instead of `TypeId` as a diagnostic argument wherever
  48. // possible, because we should eventually be able to produce a sugared type name
  49. // in this case, whereas a `TypeId` will render as a canonical type.
  50. struct TypeOfInstId {
  51. using DiagnosticType = DiagnosticTypeInfo<std::string>;
  52. // NOLINTNEXTLINE(google-explicit-constructor)
  53. TypeOfInstId(SemIR::InstId inst_id) : inst_id(inst_id) {}
  54. SemIR::InstId inst_id;
  55. };
  56. // A type expression, for rendering in a diagnostic. The diagnostic rendering
  57. // will include enclosing "`"s, and may also include extra information about the
  58. // type if it would otherwise be ambiguous.
  59. //
  60. // TODO: Include such additional information where relevant.
  61. //
  62. // This should be used when the source expression used to construct a type is
  63. // available.
  64. struct InstIdAsType {
  65. using DiagnosticType = DiagnosticTypeInfo<std::string>;
  66. // NOLINTNEXTLINE(google-explicit-constructor)
  67. InstIdAsType(SemIR::InstId inst_id) : inst_id(inst_id) {}
  68. SemIR::InstId inst_id;
  69. };
  70. // A type expression, for rendering in a diagnostic as a raw type. When
  71. // formatting as a raw type in a diagnostic, the type will be formatted as a
  72. // simple Carbon expression, without enclosing "`"s. Once we start including
  73. // extra information about types, such annotations will also not be included for
  74. // raw types.
  75. //
  76. // This is intended for cases where the type is part of a larger syntactic
  77. // construct in a diagnostic, such as "redefinition of `impl {0} as {1}`".
  78. struct InstIdAsRawType {
  79. using DiagnosticType = DiagnosticTypeInfo<std::string>;
  80. // NOLINTNEXTLINE(google-explicit-constructor)
  81. InstIdAsRawType(SemIR::InstId inst_id) : inst_id(inst_id) {}
  82. SemIR::InstId inst_id;
  83. };
  84. // A type value for rendering in a diagnostic without enclosing "`"s. See
  85. // `InstIdAsRawType` for details on raw type formatting.
  86. //
  87. // As with `TypeId`, this should be avoided as a diagnostic argument where
  88. // possible, because it can't be formatted with syntactic sugar such as aliases
  89. // that describe how the type was written.
  90. struct TypeIdAsRawType {
  91. using DiagnosticType = DiagnosticTypeInfo<std::string>;
  92. // NOLINTNEXTLINE(google-explicit-constructor)
  93. TypeIdAsRawType(SemIR::TypeId type_id) : type_id(type_id) {}
  94. SemIR::TypeId type_id;
  95. };
  96. // An integer value together with its type. The type is used to determine how to
  97. // format the value in diagnostics.
  98. struct TypedInt {
  99. using DiagnosticType = DiagnosticTypeInfo<llvm::APSInt>;
  100. SemIR::TypeId type;
  101. llvm::APInt value;
  102. };
  103. } // namespace Carbon::Check
  104. #endif // CARBON_TOOLCHAIN_CHECK_DIAGNOSTIC_HELPERS_H_