2004-07-02 18:41:18 +00:00
|
|
|
//===-- llvm/GlobalVariable.h - GlobalVariable class ------------*- C++ -*-===//
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2003-10-20 20:19:47 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2003-10-20 20:19:47 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-09-10 07:58:01 +00:00
|
|
|
//
|
|
|
|
// This file contains the declaration of the GlobalVariable class, which
|
2001-09-18 04:01:05 +00:00
|
|
|
// represents a single global variable (or constant) in the VM.
|
2001-09-10 07:58:01 +00:00
|
|
|
//
|
|
|
|
// Global variables are constant pointers that refer to hunks of space that are
|
2001-09-18 04:01:05 +00:00
|
|
|
// allocated by either the VM, or by the linker in a static compiler. A global
|
|
|
|
// variable may have an intial value, which is copied into the executables .data
|
|
|
|
// area. Global Constants are required to have initializers.
|
2001-09-10 07:58:01 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_GLOBAL_VARIABLE_H
|
|
|
|
#define LLVM_GLOBAL_VARIABLE_H
|
|
|
|
|
2001-10-03 14:53:21 +00:00
|
|
|
#include "llvm/GlobalValue.h"
|
2003-06-30 21:59:07 +00:00
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
2001-09-10 07:58:01 +00:00
|
|
|
class Module;
|
2001-12-03 22:26:30 +00:00
|
|
|
class Constant;
|
2001-09-18 04:01:05 +00:00
|
|
|
class PointerType;
|
2007-04-17 03:26:42 +00:00
|
|
|
template<typename ValueSubClass, typename ItemParentClass>
|
|
|
|
class SymbolTableListTraits;
|
2001-09-10 07:58:01 +00:00
|
|
|
|
2001-10-03 14:53:21 +00:00
|
|
|
class GlobalVariable : public GlobalValue {
|
2007-04-17 03:26:42 +00:00
|
|
|
friend class SymbolTableListTraits<GlobalVariable, Module>;
|
2005-12-17 00:19:22 +00:00
|
|
|
void operator=(const GlobalVariable &); // Do not implement
|
|
|
|
GlobalVariable(const GlobalVariable &); // Do not implement
|
|
|
|
|
2002-09-06 21:31:57 +00:00
|
|
|
void setParent(Module *parent);
|
2001-09-10 07:58:01 +00:00
|
|
|
|
2002-06-25 16:12:52 +00:00
|
|
|
GlobalVariable *Prev, *Next;
|
|
|
|
void setNext(GlobalVariable *N) { Next = N; }
|
|
|
|
void setPrev(GlobalVariable *N) { Prev = N; }
|
|
|
|
|
2007-04-17 18:15:04 +00:00
|
|
|
bool isConstantGlobal : 1; // Is this a global constant?
|
|
|
|
bool isThreadLocalSymbol : 1; // Is this symbol "Thread Local"?
|
2005-01-29 00:33:00 +00:00
|
|
|
Use Initializer;
|
|
|
|
|
2001-09-10 07:58:01 +00:00
|
|
|
public:
|
2002-09-06 21:31:57 +00:00
|
|
|
/// GlobalVariable ctor - If a parent module is specified, the global is
|
|
|
|
/// automatically inserted into the end of the specified modules global list.
|
2003-04-16 20:28:45 +00:00
|
|
|
GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
|
2005-04-22 03:18:56 +00:00
|
|
|
Constant *Initializer = 0, const std::string &Name = "",
|
2007-04-12 18:32:50 +00:00
|
|
|
Module *Parent = 0, bool ThreadLocal = false);
|
2006-09-30 21:31:26 +00:00
|
|
|
/// GlobalVariable ctor - This creates a global and inserts it before the
|
|
|
|
/// specified other global.
|
|
|
|
GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
|
|
|
|
Constant *Initializer, const std::string &Name,
|
2007-04-12 18:32:50 +00:00
|
|
|
GlobalVariable *InsertBefore, bool ThreadLocal = false);
|
2006-09-30 21:31:26 +00:00
|
|
|
|
2007-01-30 20:08:39 +00:00
|
|
|
/// isDeclaration - Is this global variable lacking an initializer? If so,
|
|
|
|
/// the global variable is defined in some other translation unit, and is thus
|
|
|
|
/// only a declaration here.
|
|
|
|
virtual bool isDeclaration() const { return getNumOperands() == 0; }
|
2002-10-06 22:29:58 +00:00
|
|
|
|
|
|
|
/// hasInitializer - Unless a global variable isExternal(), it has an
|
|
|
|
/// initializer. The initializer for the global variable/constant is held by
|
2005-01-29 00:33:00 +00:00
|
|
|
/// Initializer if an initializer is specified.
|
2002-10-06 22:29:58 +00:00
|
|
|
///
|
2007-01-30 20:08:39 +00:00
|
|
|
inline bool hasInitializer() const { return !isDeclaration(); }
|
2002-10-06 22:29:58 +00:00
|
|
|
|
|
|
|
/// getInitializer - Return the initializer for this global variable. It is
|
|
|
|
/// illegal to call this method if the global is external, because we cannot
|
|
|
|
/// tell what the value is initialized to!
|
|
|
|
///
|
2001-12-03 22:26:30 +00:00
|
|
|
inline Constant *getInitializer() const {
|
2001-10-13 06:12:38 +00:00
|
|
|
assert(hasInitializer() && "GV doesn't have initializer!");
|
2005-01-29 00:33:00 +00:00
|
|
|
return reinterpret_cast<Constant*>(Initializer.get());
|
2001-09-18 04:01:05 +00:00
|
|
|
}
|
2001-12-03 22:26:30 +00:00
|
|
|
inline Constant *getInitializer() {
|
2001-10-13 06:12:38 +00:00
|
|
|
assert(hasInitializer() && "GV doesn't have initializer!");
|
2005-01-29 00:33:00 +00:00
|
|
|
return reinterpret_cast<Constant*>(Initializer.get());
|
2001-09-18 04:01:05 +00:00
|
|
|
}
|
2001-12-03 22:26:30 +00:00
|
|
|
inline void setInitializer(Constant *CPV) {
|
2001-10-03 15:38:36 +00:00
|
|
|
if (CPV == 0) {
|
2005-01-29 00:33:00 +00:00
|
|
|
if (hasInitializer()) {
|
|
|
|
Initializer.set(0);
|
|
|
|
NumOperands = 0;
|
|
|
|
}
|
2001-10-03 15:38:36 +00:00
|
|
|
} else {
|
2005-01-29 00:33:00 +00:00
|
|
|
if (!hasInitializer())
|
|
|
|
NumOperands = 1;
|
|
|
|
Initializer.set(CPV);
|
2001-10-03 15:38:36 +00:00
|
|
|
}
|
|
|
|
}
|
2001-09-18 04:01:05 +00:00
|
|
|
|
2002-10-06 22:29:58 +00:00
|
|
|
/// If the value is a global constant, its value is immutable throughout the
|
|
|
|
/// runtime execution of the program. Assigning a value into the constant
|
|
|
|
/// leads to undefined behavior.
|
|
|
|
///
|
2003-02-02 16:40:40 +00:00
|
|
|
bool isConstant() const { return isConstantGlobal; }
|
|
|
|
void setConstant(bool Value) { isConstantGlobal = Value; }
|
2004-10-11 22:21:13 +00:00
|
|
|
|
2007-04-12 18:32:50 +00:00
|
|
|
/// If the value is "Thread Local", its value isn't shared by the threads.
|
|
|
|
bool isThreadLocal() const { return isThreadLocalSymbol; }
|
|
|
|
void setThreadLocal(bool Value) { isThreadLocalSymbol = Value; }
|
|
|
|
|
2004-10-11 22:21:13 +00:00
|
|
|
/// removeFromParent - This method unlinks 'this' from the containing module,
|
|
|
|
/// but does not delete it.
|
|
|
|
///
|
|
|
|
void removeFromParent();
|
|
|
|
|
|
|
|
/// eraseFromParent - This method unlinks 'this' from the containing module
|
|
|
|
/// and deletes it.
|
|
|
|
///
|
|
|
|
void eraseFromParent();
|
2005-04-21 20:19:05 +00:00
|
|
|
|
|
|
|
/// Override Constant's implementation of this method so we can
|
2004-07-17 23:29:46 +00:00
|
|
|
/// replace constant initializers.
|
2005-10-04 18:12:13 +00:00
|
|
|
virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
|
2005-04-21 20:19:05 +00:00
|
|
|
|
2002-04-08 21:56:02 +00:00
|
|
|
virtual void print(std::ostream &OS) const;
|
2006-12-17 05:15:13 +00:00
|
|
|
void print(std::ostream *OS) const { if (OS) print(*OS); }
|
2002-04-08 21:56:02 +00:00
|
|
|
|
2001-10-01 18:26:53 +00:00
|
|
|
// Methods for support type inquiry through isa, cast, and dyn_cast:
|
2001-10-02 03:41:24 +00:00
|
|
|
static inline bool classof(const GlobalVariable *) { return true; }
|
|
|
|
static inline bool classof(const Value *V) {
|
2007-04-13 18:12:09 +00:00
|
|
|
return V->getValueID() == Value::GlobalVariableVal;
|
2001-10-01 18:26:53 +00:00
|
|
|
}
|
2007-04-17 18:30:41 +00:00
|
|
|
private:
|
|
|
|
// getNext/Prev - Return the next or previous global variable in the list.
|
|
|
|
GlobalVariable *getNext() { return Next; }
|
|
|
|
const GlobalVariable *getNext() const { return Next; }
|
|
|
|
GlobalVariable *getPrev() { return Prev; }
|
|
|
|
const GlobalVariable *getPrev() const { return Prev; }
|
2001-09-10 07:58:01 +00:00
|
|
|
};
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2001-09-10 07:58:01 +00:00
|
|
|
#endif
|