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