Add some comments, fix an 80 cols violation.
[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 and by Function.  This is used because you
12 // can do certain things with these global objects that you can't do to anything
13 // else.  For example, use the address of one as a constant.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_GLOBALVALUE_H
18 #define LLVM_GLOBALVALUE_H
19
20 #include "llvm/Constant.h"
21
22 namespace llvm {
23
24 class PointerType;
25 class Module;
26
27 class GlobalValue : public Constant {
28   GlobalValue(const GlobalValue &);             // do not implement
29 public:
30   /// @brief An enumeration for the kinds of linkage for global values.
31   enum LinkageTypes {
32     ExternalLinkage,    ///< Externally visible function
33     LinkOnceLinkage,    ///< Keep one copy of function when linking (inline)
34     WeakLinkage,        ///< Keep one copy of named function when linking (weak)
35     AppendingLinkage,   ///< Special purpose, only applies to global arrays
36     InternalLinkage,    ///< Rename collisions when linking (static functions)
37     DLLImportLinkage,   ///< Function to be imported from DLL
38     DLLExportLinkage,   ///< Function to be accessible from DLL
39     ExternalWeakLinkage,///< ExternalWeak linkage description
40     GhostLinkage        ///< Stand-in functions for streaming fns from BC files    
41   };
42
43   /// @brief An enumeration for the kinds of visibility of global values.
44   enum VisibilityTypes {
45     DefaultVisibility,  ///< The GV is visible
46     HiddenVisibility    ///< The GV is hidden
47   };
48
49 protected:
50   GlobalValue(const Type *Ty, ValueTy vty, Use *Ops, unsigned NumOps,
51               LinkageTypes linkage, const std::string &name = "")
52     : Constant(Ty, vty, Ops, NumOps, name), Parent(0),
53       Linkage(linkage), Visibility(DefaultVisibility), Alignment(0) { }
54
55   Module *Parent;
56   LinkageTypes Linkage;   // The linkage of this global
57   VisibilityTypes Visibility;  // The visibility style of this global
58   unsigned Alignment;     // Alignment of this symbol, must be power of two
59   std::string Section;    // Section to emit this into, empty mean default
60 public:
61   ~GlobalValue() {
62     removeDeadConstantUsers();   // remove any dead constants using this.
63   }
64
65   unsigned getAlignment() const { return Alignment; }
66   void setAlignment(unsigned Align) {
67     assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
68     Alignment = Align;
69   }
70
71   VisibilityTypes getVisibility() const { return Visibility; }
72   bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; }
73   void setVisibility(VisibilityTypes V) { Visibility = V; }
74   
75   bool hasSection() const { return !Section.empty(); }
76   const std::string &getSection() const { return Section; }
77   void setSection(const std::string &S) { Section = S; }
78   
79   /// If the usage is empty (except transitively dead constants), then this
80   /// global value can can be safely deleted since the destructor will
81   /// delete the dead constants as well.
82   /// @brief Determine if the usage of this global value is empty except
83   /// for transitively dead constants.
84   bool use_empty_except_constants();
85
86   /// getType - Global values are always pointers.
87   inline const PointerType *getType() const {
88     return reinterpret_cast<const PointerType*>(User::getType());
89   }
90
91   bool hasExternalLinkage()   const { return Linkage == ExternalLinkage; }
92   bool hasLinkOnceLinkage()   const { return Linkage == LinkOnceLinkage; }
93   bool hasWeakLinkage()       const { return Linkage == WeakLinkage; }
94   bool hasAppendingLinkage()  const { return Linkage == AppendingLinkage; }
95   bool hasInternalLinkage()   const { return Linkage == InternalLinkage; }
96   bool hasDLLImportLinkage()  const { return Linkage == DLLImportLinkage; }
97   bool hasDLLExportLinkage()  const { return Linkage == DLLExportLinkage; }
98   bool hasExternalWeakLinkage() const { return Linkage == ExternalWeakLinkage; }
99   void setLinkage(LinkageTypes LT) { Linkage = LT; }
100   LinkageTypes getLinkage() const { return Linkage; }
101
102   /// hasNotBeenReadFromBytecode - If a module provider is being used to lazily
103   /// stream in functions from disk, this method can be used to check to see if
104   /// the function has been read in yet or not.  Unless you are working on the
105   /// JIT or something else that streams stuff in lazily, you don't need to
106   /// worry about this.
107   bool hasNotBeenReadFromBytecode() const { return Linkage == GhostLinkage; }
108
109   /// Override from Constant class. No GlobalValue's are null values so this
110   /// always returns false.
111   virtual bool isNullValue() const { return false; }
112
113   /// Override from Constant class.
114   virtual void destroyConstant();
115
116   /// isExternal - Return true if the primary definition of this global value is
117   /// outside of the current translation unit...
118   virtual bool isExternal() const = 0;
119
120   /// getParent - Get the module that this global value is contained inside
121   /// of...
122   inline Module *getParent() { return Parent; }
123   inline const Module *getParent() const { return Parent; }
124
125   /// removeDeadConstantUsers - If there are any dead constant users dangling
126   /// off of this global value, remove them.  This method is useful for clients
127   /// that want to check to see if a global is unused, but don't want to deal
128   /// with potentially dead constants hanging off of the globals.
129   ///
130   /// This method tries to make the global dead.  If it detects a user that
131   /// would prevent it from becoming completely dead, it gives up early,
132   /// potentially leaving some dead constant users around.
133   void removeDeadConstantUsers();
134
135   // Methods for support type inquiry through isa, cast, and dyn_cast:
136   static inline bool classof(const GlobalValue *) { return true; }
137   static inline bool classof(const Value *V) {
138     return V->getValueType() == Value::FunctionVal ||
139            V->getValueType() == Value::GlobalVariableVal;
140   }
141 };
142
143 } // End llvm namespace
144
145 #endif