absolute_node_id.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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_SEM_IR_ABSOLUTE_NODE_ID_H_
  5. #define CARBON_TOOLCHAIN_SEM_IR_ABSOLUTE_NODE_ID_H_
  6. #include "toolchain/parse/tree_and_subtrees.h"
  7. #include "toolchain/sem_ir/file.h"
  8. #include "toolchain/sem_ir/ids.h"
  9. namespace Carbon::SemIR {
  10. // A specific node location in a file. Can refer to a Clang source location
  11. // within imported C++ code.
  12. class AbsoluteNodeId {
  13. public:
  14. // A specific node location in a file.
  15. explicit AbsoluteNodeId(CheckIRId check_ir_id, Parse::NodeId node_id)
  16. : check_ir_id_(check_ir_id), node_id_(node_id) {
  17. CARBON_CHECK(check_ir_id != CheckIRId::Cpp);
  18. }
  19. // A Clang source location within imported C++ code.
  20. explicit AbsoluteNodeId(ClangSourceLocId clang_source_loc_id)
  21. : check_ir_id_(CheckIRId::Cpp),
  22. clang_source_loc_id_(clang_source_loc_id) {}
  23. // For a specific node location in a file, the ID of the IR.
  24. // For Clang source location, this returns `Cpp`.
  25. auto check_ir_id() const -> CheckIRId { return check_ir_id_; }
  26. // The specific node location in a file. Must be called only if
  27. // `check_ir_id()` doesn't return `Cpp`.
  28. auto node_id() const -> Parse::NodeId {
  29. CARBON_CHECK(check_ir_id() != CheckIRId::Cpp);
  30. return node_id_;
  31. }
  32. // The Clang source location. Must be called only if `check_ir_id()` returns
  33. // `Cpp`.
  34. auto clang_source_loc_id() const -> ClangSourceLocId {
  35. CARBON_CHECK(check_ir_id() == CheckIRId::Cpp);
  36. return clang_source_loc_id_;
  37. }
  38. private:
  39. // See `check_ir_id()`.
  40. CheckIRId check_ir_id_;
  41. union {
  42. // See `node_id()`.
  43. Parse::NodeId node_id_;
  44. // See `clang_source_loc_id()`.
  45. ClangSourceLocId clang_source_loc_id_;
  46. };
  47. };
  48. // Resolves the `LocId` to a series of `NodeId`s, which may be in different
  49. // files. The vector will have one entry if there were no imports, and multiple
  50. // entries when imports are traversed. The final entry is the actual
  51. // declaration.
  52. //
  53. // Note that the `LocId` here is typically not canonical, and it uses that fact
  54. // for non-canonical locations built from an `ExportDecl` instruction.
  55. auto GetAbsoluteNodeId(const File* sem_ir, LocId loc_id)
  56. -> llvm::SmallVector<AbsoluteNodeId>;
  57. } // namespace Carbon::SemIR
  58. #endif // CARBON_TOOLCHAIN_SEM_IR_ABSOLUTE_NODE_ID_H_