Symbol.h 4.0 KB

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