Symbol.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //===- llvm/TextAPI/Symbol.h - TAPI Symbol ----------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef LLVM_TEXTAPI_MACHO_SYMBOL_H
  9. #define LLVM_TEXTAPI_MACHO_SYMBOL_H
  10. #include "llvm/ADT/BitmaskEnum.h"
  11. #include "llvm/ADT/StringRef.h"
  12. #include "llvm/Support/Error.h"
  13. #include "llvm/Support/raw_ostream.h"
  14. #include "llvm/TextAPI/ArchitectureSet.h"
  15. #include "llvm/TextAPI/Target.h"
  16. namespace llvm {
  17. namespace MachO {
  18. // clang-format off
  19. /// Symbol flags.
  20. enum class SymbolFlags : uint8_t {
  21. /// No flags
  22. None = 0,
  23. /// Thread-local value symbol
  24. ThreadLocalValue = 1U << 0,
  25. /// Weak defined symbol
  26. WeakDefined = 1U << 1,
  27. /// Weak referenced symbol
  28. WeakReferenced = 1U << 2,
  29. /// Undefined
  30. Undefined = 1U << 3,
  31. /// Rexported
  32. Rexported = 1U << 4,
  33. LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/Rexported),
  34. };
  35. // clang-format on
  36. enum class SymbolKind : uint8_t {
  37. GlobalSymbol,
  38. ObjectiveCClass,
  39. ObjectiveCClassEHType,
  40. ObjectiveCInstanceVariable,
  41. };
  42. using TargetList = SmallVector<Target, 5>;
  43. class Symbol {
  44. public:
  45. Symbol(SymbolKind Kind, StringRef Name, TargetList Targets, SymbolFlags Flags)
  46. : Name(Name), Targets(std::move(Targets)), Kind(Kind), Flags(Flags) {}
  47. void addTarget(Target target) { Targets.emplace_back(target); }
  48. SymbolKind getKind() const { return Kind; }
  49. StringRef getName() const { return Name; }
  50. ArchitectureSet getArchitectures() const {
  51. return mapToArchitectureSet(Targets);
  52. }
  53. SymbolFlags getFlags() const { return Flags; }
  54. bool isWeakDefined() const {
  55. return (Flags & SymbolFlags::WeakDefined) == SymbolFlags::WeakDefined;
  56. }
  57. bool isWeakReferenced() const {
  58. return (Flags & SymbolFlags::WeakReferenced) == SymbolFlags::WeakReferenced;
  59. }
  60. bool isThreadLocalValue() const {
  61. return (Flags & SymbolFlags::ThreadLocalValue) ==
  62. SymbolFlags::ThreadLocalValue;
  63. }
  64. bool isUndefined() const {
  65. return (Flags & SymbolFlags::Undefined) == SymbolFlags::Undefined;
  66. }
  67. bool isReexported() const {
  68. return (Flags & SymbolFlags::Rexported) == SymbolFlags::Rexported;
  69. }
  70. using const_target_iterator = TargetList::const_iterator;
  71. using const_target_range = llvm::iterator_range<const_target_iterator>;
  72. const_target_range targets() const { return {Targets}; }
  73. using const_filtered_target_iterator =
  74. llvm::filter_iterator<const_target_iterator,
  75. std::function<bool(const Target &)>>;
  76. using const_filtered_target_range =
  77. llvm::iterator_range<const_filtered_target_iterator>;
  78. const_filtered_target_range targets(ArchitectureSet architectures) const;
  79. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  80. void dump(raw_ostream &OS) const;
  81. void dump() const { dump(llvm::errs()); }
  82. #endif
  83. bool operator==(const Symbol &O) const {
  84. return (Kind == O.Kind) && (Name == O.Name) && (Targets == O.Targets) &&
  85. (Flags == O.Flags);
  86. }
  87. bool operator!=(const Symbol &O) const { return !(*this == O); }
  88. private:
  89. StringRef Name;
  90. TargetList Targets;
  91. SymbolKind Kind;
  92. SymbolFlags Flags;
  93. };
  94. } // end namespace MachO.
  95. } // end namespace llvm.
  96. #endif // LLVM_TEXTAPI_MACHO_SYMBOL_H