LLVM API Documentation

GlobalVariable.h

Go to the documentation of this file.
00001 //===-- llvm/GlobalVariable.h - GlobalVariable class ------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains the declaration of the GlobalVariable class, which
00011 // represents a single global variable (or constant) in the VM.
00012 //
00013 // Global variables are constant pointers that refer to hunks of space that are
00014 // allocated by either the VM, or by the linker in a static compiler.  A global
00015 // variable may have an intial value, which is copied into the executables .data
00016 // area.  Global Constants are required to have initializers.
00017 //
00018 //===----------------------------------------------------------------------===//
00019 
00020 #ifndef LLVM_GLOBAL_VARIABLE_H
00021 #define LLVM_GLOBAL_VARIABLE_H
00022 
00023 #include "llvm/GlobalValue.h"
00024 #include "llvm/OperandTraits.h"
00025 #include "llvm/ADT/ilist_node.h"
00026 
00027 namespace llvm {
00028 
00029 class Module;
00030 class Constant;
00031 template<typename ValueSubClass, typename ItemParentClass>
00032   class SymbolTableListTraits;
00033 
00034 class GlobalVariable : public GlobalValue, public ilist_node<GlobalVariable> {
00035   friend class SymbolTableListTraits<GlobalVariable, Module>;
00036   void *operator new(size_t, unsigned);       // Do not implement
00037   void operator=(const GlobalVariable &);     // Do not implement
00038   GlobalVariable(const GlobalVariable &);     // Do not implement
00039 
00040   void setParent(Module *parent);
00041 
00042   bool isConstantGlobal : 1;           // Is this a global constant?
00043   bool isThreadLocalSymbol : 1;        // Is this symbol "Thread Local"?
00044 
00045 public:
00046   // allocate space for exactly one operand
00047   void *operator new(size_t s) {
00048     return User::operator new(s, 1);
00049   }
00050   /// GlobalVariable ctor - If a parent module is specified, the global is
00051   /// automatically inserted into the end of the specified modules global list.
00052   GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
00053                  Constant *Initializer = 0, const std::string &Name = "",
00054                  Module *Parent = 0, bool ThreadLocal = false,
00055                  unsigned AddressSpace = 0);
00056   /// GlobalVariable ctor - This creates a global and inserts it before the
00057   /// specified other global.
00058   GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
00059                  Constant *Initializer, const std::string &Name,
00060                  GlobalVariable *InsertBefore, bool ThreadLocal = false,
00061                  unsigned AddressSpace = 0);
00062 
00063   ~GlobalVariable() {
00064     NumOperands = 1; // FIXME: needed by operator delete
00065   }
00066 
00067   /// Provide fast operand accessors
00068   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
00069 
00070   /// isDeclaration - Is this global variable lacking an initializer?  If so, 
00071   /// the global variable is defined in some other translation unit, and is thus
00072   /// only a declaration here.
00073   virtual bool isDeclaration() const { return getNumOperands() == 0; }
00074 
00075   /// hasInitializer - Unless a global variable isExternal(), it has an
00076   /// initializer.  The initializer for the global variable/constant is held by
00077   /// Initializer if an initializer is specified.
00078   ///
00079   inline bool hasInitializer() const { return !isDeclaration(); }
00080 
00081   /// getInitializer - Return the initializer for this global variable.  It is
00082   /// illegal to call this method if the global is external, because we cannot
00083   /// tell what the value is initialized to!
00084   ///
00085   inline /*const FIXME*/ Constant *getInitializer() const {
00086     assert(hasInitializer() && "GV doesn't have initializer!");
00087     return static_cast<Constant*>(Op<0>().get());
00088   }
00089   inline Constant *getInitializer() {
00090     assert(hasInitializer() && "GV doesn't have initializer!");
00091     return static_cast<Constant*>(Op<0>().get());
00092   }
00093   inline void setInitializer(Constant *CPV) {
00094     if (CPV == 0) {
00095       if (hasInitializer()) {
00096         Op<0>().set(0);
00097         NumOperands = 0;
00098       }
00099     } else {
00100       if (!hasInitializer())
00101         NumOperands = 1;
00102       Op<0>().set(CPV);
00103     }
00104   }
00105 
00106   /// If the value is a global constant, its value is immutable throughout the
00107   /// runtime execution of the program.  Assigning a value into the constant
00108   /// leads to undefined behavior.
00109   ///
00110   bool isConstant() const { return isConstantGlobal; }
00111   void setConstant(bool Val) { isConstantGlobal = Val; }
00112 
00113   /// If the value is "Thread Local", its value isn't shared by the threads.
00114   bool isThreadLocal() const { return isThreadLocalSymbol; }
00115   void setThreadLocal(bool Val) { isThreadLocalSymbol = Val; }
00116 
00117   /// copyAttributesFrom - copy all additional attributes (those not needed to
00118   /// create a GlobalVariable) from the GlobalVariable Src to this one.
00119   void copyAttributesFrom(const GlobalValue *Src);
00120 
00121   /// removeFromParent - This method unlinks 'this' from the containing module,
00122   /// but does not delete it.
00123   ///
00124   virtual void removeFromParent();
00125 
00126   /// eraseFromParent - This method unlinks 'this' from the containing module
00127   /// and deletes it.
00128   ///
00129   virtual void eraseFromParent();
00130 
00131   /// Override Constant's implementation of this method so we can
00132   /// replace constant initializers.
00133   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
00134 
00135   // Methods for support type inquiry through isa, cast, and dyn_cast:
00136   static inline bool classof(const GlobalVariable *) { return true; }
00137   static inline bool classof(const Value *V) {
00138     return V->getValueID() == Value::GlobalVariableVal;
00139   }
00140 };
00141 
00142 template <>
00143 struct OperandTraits<GlobalVariable> : OptionalOperandTraits<> {
00144 };
00145 
00146 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable, Value)
00147 
00148 } // End llvm namespace
00149 
00150 #endif



This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.