]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/include/lld/Core/Atom.h
Bring lld (release_39 branch, r279477) to contrib
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / include / lld / Core / Atom.h
1 //===- Core/Atom.h - A node in linking graph ------------------------------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLD_CORE_ATOM_H
11 #define LLD_CORE_ATOM_H
12
13 #include "lld/Core/LLVM.h"
14
15 namespace lld {
16
17 class File;
18
19 template<typename T>
20 class OwningAtomPtr;
21
22 ///
23 /// The linker has a Graph Theory model of linking. An object file is seen
24 /// as a set of Atoms with References to other Atoms.  Each Atom is a node
25 /// and each Reference is an edge. An Atom can be a DefinedAtom which has
26 /// content or a UndefinedAtom which is a placeholder and represents an
27 /// undefined symbol (extern declaration).
28 ///
29 class Atom {
30   template<typename T> friend class OwningAtomPtr;
31 public:
32   /// Whether this atom is defined or a proxy for an undefined symbol
33   enum Definition {
34     definitionRegular,      ///< Normal C/C++ function or global variable.
35     definitionAbsolute,     ///< Asm-only (foo = 10). Not tied to any content.
36     definitionUndefined,    ///< Only in .o files to model reference to undef.
37     definitionSharedLibrary ///< Only in shared libraries to model export.
38   };
39
40   /// The scope in which this atom is acessible to other atoms.
41   enum Scope {
42     scopeTranslationUnit,  ///< Accessible only to atoms in the same translation
43                            ///  unit (e.g. a C static).
44     scopeLinkageUnit,      ///< Accessible to atoms being linked but not visible
45                            ///  to runtime loader (e.g. visibility=hidden).
46     scopeGlobal            ///< Accessible to all atoms and visible to runtime
47                            ///  loader (e.g. visibility=default).
48   };
49
50
51   /// file - returns the File that produced/owns this Atom
52   virtual const File& file() const = 0;
53
54   /// name - The name of the atom. For a function atom, it is the (mangled)
55   /// name of the function.
56   virtual StringRef name() const = 0;
57
58   /// definition - Whether this atom is a definition or represents an undefined
59   /// symbol.
60   Definition definition() const { return _definition; }
61
62   static bool classof(const Atom *a) { return true; }
63
64 protected:
65   /// Atom is an abstract base class.  Only subclasses can access constructor.
66   explicit Atom(Definition def) : _definition(def) {}
67
68   /// The memory for Atom objects is always managed by the owning File
69   /// object.  Therefore, no one but the owning File object should call
70   /// delete on an Atom.  In fact, some File objects may bulk allocate
71   /// an array of Atoms, so they cannot be individually deleted by anyone.
72   virtual ~Atom() {}
73
74 private:
75   Definition _definition;
76 };
77
78 /// Class which owns an atom pointer and runs the atom destructor when the
79 /// owning pointer goes out of scope.
80 template<typename T>
81 class OwningAtomPtr {
82 private:
83   OwningAtomPtr(const OwningAtomPtr &) = delete;
84   void operator=(const OwningAtomPtr&) = delete;
85 public:
86   OwningAtomPtr() : atom(nullptr) { }
87   OwningAtomPtr(T *atom) : atom(atom) { }
88
89   ~OwningAtomPtr() {
90     if (atom)
91       runDestructor(atom);
92   }
93
94   void runDestructor(Atom *atom) {
95     atom->~Atom();
96   }
97
98   OwningAtomPtr(OwningAtomPtr &&ptr) : atom(ptr.atom) {
99     ptr.atom = nullptr;
100   }
101
102   void operator=(OwningAtomPtr&& ptr) {
103     if (atom)
104       runDestructor(atom);
105     atom = ptr.atom;
106     ptr.atom = nullptr;
107   }
108
109   T *const &get() const {
110     return atom;
111   }
112
113   T *&get() {
114     return atom;
115   }
116
117   T *release() {
118     auto *v = atom;
119     atom = nullptr;
120     return v;
121   }
122
123 private:
124   T *atom;
125 };
126
127 } // namespace lld
128
129 #endif // LLD_CORE_ATOM_H