Add convenient helper for checking whether global is weak in linker sense
[oota-llvm.git] / include / llvm / GlobalValue.h
1 //===-- llvm/GlobalValue.h - Class to represent a global value --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a common base class of all globally definable objects.  As such,
11 // it is subclassed by GlobalVariable, GlobalAlias and by Function.  This is
12 // used because you can do certain things with these global objects that you
13 // can't do to anything else.  For example, use the address of one as a
14 // constant.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #ifndef LLVM_GLOBALVALUE_H
19 #define LLVM_GLOBALVALUE_H
20
21 #include "llvm/Constant.h"
22
23 namespace llvm {
24
25 class PointerType;
26 class Module;
27
28 class GlobalValue : public Constant {
29   GlobalValue(const GlobalValue &);             // do not implement
30 public:
31   /// @brief An enumeration for the kinds of linkage for global values.
32   enum LinkageTypes {
33     ExternalLinkage = 0,///< Externally visible function
34     LinkOnceLinkage,    ///< Keep one copy of function when linking (inline)
35     WeakLinkage,        ///< Keep one copy of named function when linking (weak)
36     AppendingLinkage,   ///< Special purpose, only applies to global arrays
37     InternalLinkage,    ///< Rename collisions when linking (static functions)
38     DLLImportLinkage,   ///< Function to be imported from DLL
39     DLLExportLinkage,   ///< Function to be accessible from DLL
40     ExternalWeakLinkage,///< ExternalWeak linkage description
41     GhostLinkage,       ///< Stand-in functions for streaming fns from BC files
42     CommonLinkage       ///< Tentative definitions
43   };
44
45   /// @brief An enumeration for the kinds of visibility of global values.
46   enum VisibilityTypes {
47     DefaultVisibility = 0,  ///< The GV is visible
48     HiddenVisibility,       ///< The GV is hidden
49     ProtectedVisibility     ///< The GV is protected
50   };
51
52 protected:
53   GlobalValue(const Type *ty, ValueTy vty, Use *Ops, unsigned NumOps,
54               LinkageTypes linkage, const std::string &name = "")
55     : Constant(ty, vty, Ops, NumOps), Parent(0),
56       Linkage(linkage), Visibility(DefaultVisibility), Alignment(0) {
57     if (!name.empty()) setName(name);
58   }
59
60   Module *Parent;
61   // Note: VC++ treats enums as signed, so an extra bit is required to prevent
62   // Linkage and Visibility from turning into negative values.
63   LinkageTypes Linkage : 5;   // The linkage of this global
64   unsigned Visibility : 2;    // The visibility style of this global
65   unsigned Alignment : 16;    // Alignment of this symbol, must be power of two
66   std::string Section;        // Section to emit this into, empty mean default
67 public:
68   ~GlobalValue() {
69     removeDeadConstantUsers();   // remove any dead constants using this.
70   }
71
72   unsigned getAlignment() const { return Alignment; }
73   void setAlignment(unsigned Align) {
74     assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
75     Alignment = Align;
76   }
77
78   VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); }
79   bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; }
80   bool hasProtectedVisibility() const {
81     return Visibility == ProtectedVisibility;
82   }
83   void setVisibility(VisibilityTypes V) { Visibility = V; }
84   
85   bool hasSection() const { return !Section.empty(); }
86   const std::string &getSection() const { return Section; }
87   void setSection(const std::string &S) { Section = S; }
88   
89   /// If the usage is empty (except transitively dead constants), then this
90   /// global value can can be safely deleted since the destructor will
91   /// delete the dead constants as well.
92   /// @brief Determine if the usage of this global value is empty except
93   /// for transitively dead constants.
94   bool use_empty_except_constants();
95
96   /// getType - Global values are always pointers.
97   inline const PointerType *getType() const {
98     return reinterpret_cast<const PointerType*>(User::getType());
99   }
100
101   bool hasExternalLinkage()   const { return Linkage == ExternalLinkage; }
102   bool hasLinkOnceLinkage()   const { return Linkage == LinkOnceLinkage; }
103   bool hasWeakLinkage()       const { return Linkage == WeakLinkage; }
104   bool hasCommonLinkage()     const { return Linkage == CommonLinkage; }
105   bool hasAppendingLinkage()  const { return Linkage == AppendingLinkage; }
106   bool hasInternalLinkage()   const { return Linkage == InternalLinkage; }
107   bool hasDLLImportLinkage()  const { return Linkage == DLLImportLinkage; }
108   bool hasDLLExportLinkage()  const { return Linkage == DLLExportLinkage; }
109   bool hasExternalWeakLinkage() const { return Linkage == ExternalWeakLinkage; }
110   void setLinkage(LinkageTypes LT) { Linkage = LT; }
111   LinkageTypes getLinkage() const { return Linkage; }
112
113   /// isWeakForLinker - Determines if symbol is weak for linker having weak or
114   /// linkonce or common or extweak LLVM linkage.
115   bool isWeakForLinker() const {
116     return (Linkage == WeakLinkage ||
117             Linkage == LinkOnceLinkage ||
118             Linkage == CommonLinkage ||
119             Linkage == ExternalWeakLinkage);
120   }
121
122   /// copyAttributesFrom - copy all additional attributes (those not needed to
123   /// create a GlobalValue) from the GlobalValue Src to this one.
124   virtual void copyAttributesFrom(const GlobalValue *Src);
125
126   /// hasNotBeenReadFromBitcode - If a module provider is being used to lazily
127   /// stream in functions from disk, this method can be used to check to see if
128   /// the function has been read in yet or not.  Unless you are working on the
129   /// JIT or something else that streams stuff in lazily, you don't need to
130   /// worry about this.
131   bool hasNotBeenReadFromBitcode() const { return Linkage == GhostLinkage; }
132
133   /// Override from Constant class. No GlobalValue's are null values so this
134   /// always returns false.
135   virtual bool isNullValue() const { return false; }
136
137   /// Override from Constant class.
138   virtual void destroyConstant();
139
140   /// isDeclaration - Return true if the primary definition of this global 
141   /// value is outside of the current translation unit...
142   virtual bool isDeclaration() const = 0;
143
144   /// getParent - Get the module that this global value is contained inside
145   /// of...
146   inline Module *getParent() { return Parent; }
147   inline const Module *getParent() const { return Parent; }
148
149   /// removeDeadConstantUsers - If there are any dead constant users dangling
150   /// off of this global value, remove them.  This method is useful for clients
151   /// that want to check to see if a global is unused, but don't want to deal
152   /// with potentially dead constants hanging off of the globals.
153   void removeDeadConstantUsers();
154
155   // Methods for support type inquiry through isa, cast, and dyn_cast:
156   static inline bool classof(const GlobalValue *) { return true; }
157   static inline bool classof(const Value *V) {
158     return V->getValueID() == Value::FunctionVal ||
159            V->getValueID() == Value::GlobalVariableVal ||
160            V->getValueID() == Value::GlobalAliasVal;
161   }
162 };
163
164 } // End llvm namespace
165
166 #endif