Symbol.h 4.0 KB

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