Add doxygen comments, patch contributed by Evan Jones.
[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     GhostLinkage       /// Stand-in functions for streaming fns from BC files
37   };
38 protected:
39   GlobalValue(const Type *Ty, ValueTy vty, Use *Ops, unsigned NumOps,
40               LinkageTypes linkage, const std::string &name = "")
41     : Constant(Ty, vty, Ops, NumOps, name), Linkage(linkage), Parent(0) { }
42
43   LinkageTypes Linkage;   // The linkage of this global
44   Module *Parent;
45 public:
46   ~GlobalValue() {
47     removeDeadConstantUsers();   // remove any dead constants using this.
48   }
49
50   /// If the usage is empty (except transitively dead constants), then this
51   /// global value can can be safely deleted since the destructor will 
52   /// delete the dead constants as well.
53   /// @brief Determine if the usage of this global value is empty except 
54   /// for transitively dead constants.
55   bool use_empty_except_constants();
56
57   /// getType - Global values are always pointers.
58   inline const PointerType *getType() const {
59     return reinterpret_cast<const PointerType*>(User::getType());
60   }
61
62   bool hasExternalLinkage()  const { return Linkage == ExternalLinkage; }
63   bool hasLinkOnceLinkage()  const { return Linkage == LinkOnceLinkage; }
64   bool hasWeakLinkage()      const { return Linkage == WeakLinkage; }
65   bool hasAppendingLinkage() const { return Linkage == AppendingLinkage; }
66   bool hasInternalLinkage()  const { return Linkage == InternalLinkage; }
67   void setLinkage(LinkageTypes LT) { Linkage = LT; }
68   LinkageTypes getLinkage() const { return Linkage; }
69
70   /// hasNotBeenReadFromBytecode - If a module provider is being used to lazily
71   /// stream in functions from disk, this method can be used to check to see if
72   /// the function has been read in yet or not.  Unless you are working on the
73   /// JIT or something else that streams stuff in lazily, you don't need to
74   /// worry about this.
75   bool hasNotBeenReadFromBytecode() const { return Linkage == GhostLinkage; }
76
77   /// Override from Constant class. No GlobalValue's are null values so this
78   /// always returns false.
79   virtual bool isNullValue() const { return false; }
80
81   /// Override from Constant class.
82   virtual void destroyConstant();
83
84   /// isExternal - Return true if the primary definition of this global value is
85   /// outside of the current translation unit...
86   virtual bool isExternal() const = 0;
87
88   /// getParent - Get the module that this global value is contained inside
89   /// of...
90   inline Module *getParent() { return Parent; }
91   inline const Module *getParent() const { return Parent; }
92
93   /// removeDeadConstantUsers - If there are any dead constant users dangling
94   /// off of this global value, remove them.  This method is useful for clients
95   /// that want to check to see if a global is unused, but don't want to deal
96   /// with potentially dead constants hanging off of the globals.
97   ///
98   /// This method tries to make the global dead.  If it detects a user that
99   /// would prevent it from becoming completely dead, it gives up early,
100   /// potentially leaving some dead constant users around.
101   void removeDeadConstantUsers();
102
103   // Methods for support type inquiry through isa, cast, and dyn_cast:
104   static inline bool classof(const GlobalValue *T) { return true; }
105   static inline bool classof(const Value *V) {
106     return V->getValueType() == Value::FunctionVal || 
107            V->getValueType() == Value::GlobalVariableVal;
108   }
109 };
110
111 } // End llvm namespace
112
113 #endif