Change method to return void. Inline dtor
[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   ~GlobalValue() {
46     removeDeadConstantUsers();   // remove any dead constants using this.
47   }
48
49   /// If the usage is empty (except transitively dead constants), then this
50   /// global value can can be safely deleted since the destructor will 
51   /// delete the dead constants as well.
52   /// @brief Determine if the usage of this global value is empty except 
53   /// for transitively dead constants.
54   bool use_empty_except_constants();
55
56   /// getType - Global values are always pointers.
57   inline const PointerType *getType() const {
58     return reinterpret_cast<const PointerType*>(User::getType());
59   }
60
61   bool hasExternalLinkage()  const { return Linkage == ExternalLinkage; }
62   bool hasLinkOnceLinkage()  const { return Linkage == LinkOnceLinkage; }
63   bool hasWeakLinkage()      const { return Linkage == WeakLinkage; }
64   bool hasAppendingLinkage() const { return Linkage == AppendingLinkage; }
65   bool hasInternalLinkage()  const { return Linkage == InternalLinkage; }
66   void setLinkage(LinkageTypes LT) { Linkage = LT; }
67   LinkageTypes getLinkage() const { return Linkage; }
68
69   /// Override from Constant class. No GlobalValue's have null values so
70   /// this always returns false.
71   virtual bool isNullValue() const { return false; }
72
73   /// Override from Constant class.
74   virtual void destroyConstant();
75
76   /// isExternal - Return true if the primary definition of this global value is
77   /// outside of the current translation unit...
78   virtual bool isExternal() const = 0;
79
80   /// getParent - Get the module that this global value is contained inside
81   /// of...
82   inline Module *getParent() { return Parent; }
83   inline const Module *getParent() const { return Parent; }
84
85   /// removeDeadConstantUsers - If there are any dead constant users dangling
86   /// off of this global value, remove them.  This method is useful for clients
87   /// that want to check to see if a global is unused, but don't want to deal
88   /// with potentially dead constants hanging off of the globals.
89   ///
90   /// This method tries to make the global dead.  If it detects a user that
91   /// would prevent it from becoming completely dead, it gives up early,
92   /// potentially leaving some dead constant users around.
93   void removeDeadConstantUsers();
94
95   // Methods for support type inquiry through isa, cast, and dyn_cast:
96   static inline bool classof(const GlobalValue *T) { return true; }
97   static inline bool classof(const Value *V) {
98     return V->getValueType() == Value::FunctionVal || 
99            V->getValueType() == Value::GlobalVariableVal;
100   }
101 };
102
103 } // End llvm namespace
104
105 #endif