diagnostic_helpers.h 4.3 KB

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