2003-09-30 18:37:50 +00:00
|
|
|
//===-- llvm/Global.h - Class to represent a global variable ----*- C++ -*-===//
|
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
|
|
|
|
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;
|
2002-06-25 16:12:52 +00:00
|
|
|
template<typename SC> struct ilist_traits;
|
|
|
|
template<typename ValueSubClass, typename ItemParentClass, typename SymTabClass,
|
|
|
|
typename SubClass> class SymbolTableListTraits;
|
2001-09-10 07:58:01 +00:00
|
|
|
|
2001-10-03 14:53:21 +00:00
|
|
|
class GlobalVariable : public GlobalValue {
|
2002-06-25 16:12:52 +00:00
|
|
|
friend class SymbolTableListTraits<GlobalVariable, Module, Module,
|
|
|
|
ilist_traits<GlobalVariable> >;
|
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; }
|
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
bool isConstantGlobal; // Is this a global constant?
|
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,
|
2002-09-06 21:31:57 +00:00
|
|
|
Constant *Initializer = 0, const std::string &Name = "",
|
|
|
|
Module *Parent = 0);
|
2001-09-10 07:58:01 +00:00
|
|
|
|
|
|
|
// Specialize setName to handle symbol table majik...
|
2002-01-20 22:54:45 +00:00
|
|
|
virtual void setName(const std::string &name, SymbolTable *ST = 0);
|
2001-09-10 07:58:01 +00:00
|
|
|
|
2002-10-06 22:29:58 +00:00
|
|
|
/// isExternal - Is this global variable lacking an initializer? If so, the
|
|
|
|
/// global variable is defined in some other translation unit, and is thus
|
|
|
|
/// externally defined here.
|
|
|
|
///
|
2002-10-09 23:11:33 +00:00
|
|
|
virtual bool isExternal() const { return Operands.empty(); }
|
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
|
|
|
|
/// Operands[0] if an initializer is specified.
|
|
|
|
///
|
|
|
|
inline bool hasInitializer() const { return !isExternal(); }
|
|
|
|
|
|
|
|
/// 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!");
|
2001-12-03 22:26:30 +00:00
|
|
|
return (Constant*)Operands[0].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!");
|
2001-12-03 22:26:30 +00:00
|
|
|
return (Constant*)Operands[0].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) {
|
|
|
|
if (hasInitializer()) Operands.pop_back();
|
|
|
|
} else {
|
|
|
|
if (!hasInitializer()) Operands.push_back(Use(0, this));
|
|
|
|
Operands[0] = (Value*)CPV;
|
|
|
|
}
|
|
|
|
}
|
2001-09-18 04:01:05 +00:00
|
|
|
|
2002-10-06 22:29:58 +00:00
|
|
|
// getNext/Prev - Return the next or previous global variable in the list.
|
2002-06-25 16:12:52 +00:00
|
|
|
GlobalVariable *getNext() { return Next; }
|
|
|
|
const GlobalVariable *getNext() const { return Next; }
|
|
|
|
GlobalVariable *getPrev() { return Prev; }
|
|
|
|
const GlobalVariable *getPrev() const { return Prev; }
|
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; }
|
|
|
|
|
2002-04-08 21:56:02 +00:00
|
|
|
virtual void print(std::ostream &OS) const;
|
|
|
|
|
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) {
|
2001-10-03 14:53:21 +00:00
|
|
|
return V->getValueType() == Value::GlobalVariableVal;
|
2001-10-01 18:26:53 +00:00
|
|
|
}
|
2001-09-10 07:58:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|