2004-07-02 20:41:18 +02:00
|
|
|
//===-- llvm/GlobalVariable.h - GlobalVariable class ------------*- C++ -*-===//
|
2005-04-21 22:19:05 +02:00
|
|
|
//
|
2003-10-20 22:19:47 +02: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 22:19:05 +02:00
|
|
|
//
|
2003-10-20 22:19:47 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-09-10 09:58:01 +02:00
|
|
|
//
|
|
|
|
// This file contains the declaration of the GlobalVariable class, which
|
2001-09-18 06:01:05 +02:00
|
|
|
// represents a single global variable (or constant) in the VM.
|
2001-09-10 09:58:01 +02:00
|
|
|
//
|
|
|
|
// Global variables are constant pointers that refer to hunks of space that are
|
2001-09-18 06:01:05 +02: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 09:58:01 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_GLOBAL_VARIABLE_H
|
|
|
|
#define LLVM_GLOBAL_VARIABLE_H
|
|
|
|
|
2001-10-03 16:53:21 +02:00
|
|
|
#include "llvm/GlobalValue.h"
|
2003-06-30 23:59:07 +02:00
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
namespace llvm {
|
|
|
|
|
2001-09-10 09:58:01 +02:00
|
|
|
class Module;
|
2001-12-03 23:26:30 +01:00
|
|
|
class Constant;
|
2001-09-18 06:01:05 +02:00
|
|
|
class PointerType;
|
2002-06-25 18:12:52 +02:00
|
|
|
template<typename SC> struct ilist_traits;
|
|
|
|
template<typename ValueSubClass, typename ItemParentClass, typename SymTabClass,
|
|
|
|
typename SubClass> class SymbolTableListTraits;
|
2001-09-10 09:58:01 +02:00
|
|
|
|
2001-10-03 16:53:21 +02:00
|
|
|
class GlobalVariable : public GlobalValue {
|
2002-06-25 18:12:52 +02:00
|
|
|
friend class SymbolTableListTraits<GlobalVariable, Module, Module,
|
|
|
|
ilist_traits<GlobalVariable> >;
|
2005-12-17 01:19:22 +01:00
|
|
|
void operator=(const GlobalVariable &); // Do not implement
|
|
|
|
GlobalVariable(const GlobalVariable &); // Do not implement
|
|
|
|
|
2002-09-06 23:31:57 +02:00
|
|
|
void setParent(Module *parent);
|
2001-09-10 09:58:01 +02:00
|
|
|
|
2002-06-25 18:12:52 +02:00
|
|
|
GlobalVariable *Prev, *Next;
|
|
|
|
void setNext(GlobalVariable *N) { Next = N; }
|
|
|
|
void setPrev(GlobalVariable *N) { Prev = N; }
|
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
bool isConstantGlobal; // Is this a global constant?
|
2005-01-29 01:33:00 +01:00
|
|
|
Use Initializer;
|
|
|
|
|
2001-09-10 09:58:01 +02:00
|
|
|
public:
|
2002-09-06 23:31:57 +02: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 22:28:45 +02:00
|
|
|
GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
|
2005-04-22 05:18:56 +02:00
|
|
|
Constant *Initializer = 0, const std::string &Name = "",
|
2002-09-06 23:31:57 +02:00
|
|
|
Module *Parent = 0);
|
2001-09-10 09:58:01 +02:00
|
|
|
|
2002-10-07 00:29:58 +02: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.
|
|
|
|
///
|
2005-01-29 01:33:00 +01:00
|
|
|
virtual bool isExternal() const { return getNumOperands() == 0; }
|
2002-10-07 00:29:58 +02:00
|
|
|
|
|
|
|
/// hasInitializer - Unless a global variable isExternal(), it has an
|
|
|
|
/// initializer. The initializer for the global variable/constant is held by
|
2005-01-29 01:33:00 +01:00
|
|
|
/// Initializer if an initializer is specified.
|
2002-10-07 00:29:58 +02:00
|
|
|
///
|
|
|
|
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 23:26:30 +01:00
|
|
|
inline Constant *getInitializer() const {
|
2001-10-13 08:12:38 +02:00
|
|
|
assert(hasInitializer() && "GV doesn't have initializer!");
|
2005-01-29 01:33:00 +01:00
|
|
|
return reinterpret_cast<Constant*>(Initializer.get());
|
2001-09-18 06:01:05 +02:00
|
|
|
}
|
2001-12-03 23:26:30 +01:00
|
|
|
inline Constant *getInitializer() {
|
2001-10-13 08:12:38 +02:00
|
|
|
assert(hasInitializer() && "GV doesn't have initializer!");
|
2005-01-29 01:33:00 +01:00
|
|
|
return reinterpret_cast<Constant*>(Initializer.get());
|
2001-09-18 06:01:05 +02:00
|
|
|
}
|
2001-12-03 23:26:30 +01:00
|
|
|
inline void setInitializer(Constant *CPV) {
|
2001-10-03 17:38:36 +02:00
|
|
|
if (CPV == 0) {
|
2005-01-29 01:33:00 +01:00
|
|
|
if (hasInitializer()) {
|
|
|
|
Initializer.set(0);
|
|
|
|
NumOperands = 0;
|
|
|
|
}
|
2001-10-03 17:38:36 +02:00
|
|
|
} else {
|
2005-01-29 01:33:00 +01:00
|
|
|
if (!hasInitializer())
|
|
|
|
NumOperands = 1;
|
|
|
|
Initializer.set(CPV);
|
2001-10-03 17:38:36 +02:00
|
|
|
}
|
|
|
|
}
|
2001-09-18 06:01:05 +02:00
|
|
|
|
2002-10-07 00:29:58 +02:00
|
|
|
// getNext/Prev - Return the next or previous global variable in the list.
|
2002-06-25 18:12:52 +02:00
|
|
|
GlobalVariable *getNext() { return Next; }
|
|
|
|
const GlobalVariable *getNext() const { return Next; }
|
|
|
|
GlobalVariable *getPrev() { return Prev; }
|
|
|
|
const GlobalVariable *getPrev() const { return Prev; }
|
2001-09-18 06:01:05 +02:00
|
|
|
|
2002-10-07 00:29:58 +02: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 17:40:40 +01:00
|
|
|
bool isConstant() const { return isConstantGlobal; }
|
|
|
|
void setConstant(bool Value) { isConstantGlobal = Value; }
|
2004-10-12 00:21:13 +02: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 22:19:05 +02:00
|
|
|
|
|
|
|
/// Override Constant's implementation of this method so we can
|
2004-07-18 01:29:46 +02:00
|
|
|
/// replace constant initializers.
|
2005-10-04 20:12:13 +02:00
|
|
|
virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
|
2005-04-21 22:19:05 +02:00
|
|
|
|
2002-04-08 23:56:02 +02:00
|
|
|
virtual void print(std::ostream &OS) const;
|
|
|
|
|
2001-10-01 20:26:53 +02:00
|
|
|
// Methods for support type inquiry through isa, cast, and dyn_cast:
|
2001-10-02 05:41:24 +02:00
|
|
|
static inline bool classof(const GlobalVariable *) { return true; }
|
|
|
|
static inline bool classof(const Value *V) {
|
2001-10-03 16:53:21 +02:00
|
|
|
return V->getValueType() == Value::GlobalVariableVal;
|
2001-10-01 20:26:53 +02:00
|
|
|
}
|
2001-09-10 09:58:01 +02:00
|
|
|
};
|
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2001-09-10 09:58:01 +02:00
|
|
|
#endif
|