bug 122:
[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   enum LinkageTypes {
31     ExternalLinkage,   // Externally visible function
32     LinkOnceLinkage,   // Keep one copy of named function when linking (inline)
33     WeakLinkage,       // Keep one copy of named function when linking (weak)
34     AppendingLinkage,  // Special purpose, only applies to global arrays
35     InternalLinkage    // Rename collisions when linking (static functions)
36   };
37 protected:
38   GlobalValue(const Type *Ty, ValueTy vty, LinkageTypes linkage,
39               const std::string &name = "")
40     : Constant(Ty, vty, name), Linkage(linkage), Parent(0) { }
41
42   LinkageTypes Linkage;   // The linkage of this global
43   Module *Parent;
44 public:
45   virtual ~GlobalValue();
46
47   /// If the usage is empty (except transitively dead constants), then this
48   /// global value can can be safely deleted since the destructor wll 
49   /// delete the dead constants as well.
50   /// @brief Determine if theusage of this global value is empty except 
51   /// for transitively dead constants.
52   bool use_empty_except_constants();
53
54   /// getType - Global values are always pointers.
55   inline const PointerType *getType() const {
56     return reinterpret_cast<const PointerType*>(User::getType());
57   }
58
59   bool hasExternalLinkage()  const { return Linkage == ExternalLinkage; }
60   bool hasLinkOnceLinkage()  const { return Linkage == LinkOnceLinkage; }
61   bool hasWeakLinkage()      const { return Linkage == WeakLinkage; }
62   bool hasAppendingLinkage() const { return Linkage == AppendingLinkage; }
63   bool hasInternalLinkage()  const { return Linkage == InternalLinkage; }
64   void setLinkage(LinkageTypes LT) { Linkage = LT; }
65   LinkageTypes getLinkage() const { return Linkage; }
66
67   /// Override from Constant class. No GlobalValue's have null values so
68   /// this always returns false.
69   virtual bool isNullValue() const { return false; }
70
71   /// Override from Constant class.
72   virtual void destroyConstant();
73
74   /// isExternal - Return true if the primary definition of this global value is
75   /// outside of the current translation unit...
76   virtual bool isExternal() const = 0;
77
78   /// getParent - Get the module that this global value is contained inside
79   /// of...
80   inline Module *getParent() { return Parent; }
81   inline const Module *getParent() const { return Parent; }
82
83   /// removeDeadConstantUsers - If there are any dead constant users dangling
84   /// off of this global value, remove them.  This method is useful for clients
85   /// that want to check to see if a global is unused, but don't want to deal
86   /// with potentially dead constants hanging off of the globals.
87   ///
88   /// This function returns true if the global value is now dead.  If all 
89   /// users of this global are not dead, this method may return false and
90   /// leave some of them around.
91   bool removeDeadConstantUsers();
92
93   // Methods for support type inquiry through isa, cast, and dyn_cast:
94   static inline bool classof(const GlobalValue *T) { return true; }
95   static inline bool classof(const Value *V) {
96     return V->getValueType() == Value::FunctionVal || 
97            V->getValueType() == Value::GlobalVariableVal;
98   }
99 };
100
101 } // End llvm namespace
102
103 #endif