diagnostic_helpers.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 integer value together with its type. The type is used to determine how to
  34. // format the value in diagnostics.
  35. struct TypedInt {
  36. using DiagnosticType = DiagnosticTypeInfo<llvm::APSInt>;
  37. SemIR::TypeId type;
  38. llvm::APInt value;
  39. };
  40. } // namespace Carbon::Check
  41. #endif // CARBON_TOOLCHAIN_CHECK_DIAGNOSTIC_HELPERS_H_