fe43ed4f69f80dee4fd3895823a18b8cbcd2f35e
[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 was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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   };
43
44   /// @brief An enumeration for the kinds of visibility of global values.
45   enum VisibilityTypes {
46     DefaultVisibility = 0,  ///< The GV is visible
47     HiddenVisibility,       ///< The GV is hidden
48     ProtectedVisibility     ///< The GV is protected
49   };
50
51 protected:
52   GlobalValue(const Type *Ty, ValueTy vty, Use *Ops, unsigned NumOps,
53               LinkageTypes linkage, const std::string &name = "")
54     : Constant(Ty, vty, Ops, NumOps), Parent(0),
55       Linkage(linkage), Visibility(DefaultVisibility), Alignment(0) {
56     if (!name.empty()) setName(name);
57   }
58
59   Module *Parent;
60   // Note: VC++ treats enums as signed, so an extra bit is required to prevent
61   // Linkage and Visibility from turning into negative values.
62   LinkageTypes Linkage : 5;   // The linkage of this global
63   unsigned Visibility : 2;    // The visibility style of this global
64   unsigned Alignment : 16;    // Alignment of this symbol, must be power of two
65   std::string Section;        // Section to emit this into, empty mean default
66 public:
67   ~GlobalValue() {
68     removeDeadConstantUsers();   // remove any dead constants using this.
69   }
70
71   unsigned getAlignment() const { return Alignment; }
72   void setAlignment(unsigned Align) {
73     assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
74     Alignment = Align;
75   }
76
77   VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); }
78   bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; }
79   bool hasProtectedVisibility() const {
80     return Visibility == ProtectedVisibility;
81   }
82   void setVisibility(VisibilityTypes V) { Visibility = V; }
83   
84   bool hasSection() const { return !Section.empty(); }
85   const std::string &getSection() const { return Section; }
86   void setSection(const std::string &S) { Section = S; }
87   
88   /// If the usage is empty (except transitively dead constants), then this
89   /// global value can can be safely deleted since the destructor will
90   /// delete the dead constants as well.
91   /// @brief Determine if the usage of this global value is empty except
92   /// for transitively dead constants.
93   bool use_empty_except_constants();
94
95   /// getType - Global values are always pointers.
96   inline const PointerType *getType() const {
97     return reinterpret_cast<const PointerType*>(User::getType());
98   }
99
100   bool hasExternalLinkage()   const { return Linkage == ExternalLinkage; }
101   bool hasLinkOnceLinkage()   const { return Linkage == LinkOnceLinkage; }
102   bool hasWeakLinkage()       const { return Linkage == WeakLinkage; }
103   bool hasAppendingLinkage()  const { return Linkage == AppendingLinkage; }
104   bool hasInternalLinkage()   const { return Linkage == InternalLinkage; }
105   bool hasDLLImportLinkage()  const { return Linkage == DLLImportLinkage; }
106   bool hasDLLExportLinkage()  const { return Linkage == DLLExportLinkage; }
107   bool hasExternalWeakLinkage() const { return Linkage == ExternalWeakLinkage; }
108   void setLinkage(LinkageTypes LT) { Linkage = LT; }
109   LinkageTypes getLinkage() const { return Linkage; }
110
111   /// hasNotBeenReadFromBitcode - If a module provider is being used to lazily
112   /// stream in functions from disk, this method can be used to check to see if
113   /// the function has been read in yet or not.  Unless you are working on the
114   /// JIT or something else that streams stuff in lazily, you don't need to
115   /// worry about this.
116   bool hasNotBeenReadFromBitcode() const { return Linkage == GhostLinkage; }
117
118   /// Override from Constant class. No GlobalValue's are null values so this
119   /// always returns false.
120   virtual bool isNullValue() const { return false; }
121
122   /// Override from Constant class.
123   virtual void destroyConstant();
124
125   /// isDeclaration - Return true if the primary definition of this global 
126   /// value is outside of the current translation unit...
127   virtual bool isDeclaration() const = 0;
128
129   /// getParent - Get the module that this global value is contained inside
130   /// of...
131   inline Module *getParent() { return Parent; }
132   inline const Module *getParent() const { return Parent; }
133
134   /// removeDeadConstantUsers - If there are any dead constant users dangling
135   /// off of this global value, remove them.  This method is useful for clients
136   /// that want to check to see if a global is unused, but don't want to deal
137   /// with potentially dead constants hanging off of the globals.
138   void removeDeadConstantUsers();
139
140   // Methods for support type inquiry through isa, cast, and dyn_cast:
141   static inline bool classof(const GlobalValue *) { return true; }
142   static inline bool classof(const Value *V) {
143     return V->getValueID() == Value::FunctionVal ||
144            V->getValueID() == Value::GlobalVariableVal ||
145            V->getValueID() == Value::GlobalAliasVal;
146   }
147 };
148
149 } // End llvm namespace
150
151 #endif