LLVM API Documentation

Constant.h

Go to the documentation of this file.
00001 //===-- llvm/Constant.h - Constant class definition -------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains the declaration of the Constant class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_CONSTANT_H
00015 #define LLVM_CONSTANT_H
00016 
00017 #include "llvm/User.h"
00018 
00019 namespace llvm {
00020 
00021 /// This is an important base class in LLVM. It provides the common facilities
00022 /// of all constant values in an LLVM program. A constant is a value that is
00023 /// immutable at runtime. Functions are constants because their address is
00024 /// immutable. Same with global variables. 
00025 /// 
00026 /// All constants share the capabilities provided in this class. All constants
00027 /// can have a null value. They can have an operand list. Constants can be
00028 /// simple (integer and floating point values), complex (arrays and structures),
00029 /// or expression based (computations yielding a constant value composed of 
00030 /// only certain operators and other constant values).
00031 /// 
00032 /// Note that Constants are immutable (once created they never change) 
00033 /// and are fully shared by structural equivalence.  This means that two 
00034 /// structurally equivalent constants will always have the same address.  
00035 /// Constant's are created on demand as needed and never deleted: thus clients 
00036 /// don't have to worry about the lifetime of the objects.
00037 /// @brief LLVM Constant Representation
00038 class Constant : public User {
00039   void operator=(const Constant &);     // Do not implement
00040   Constant(const Constant &);           // Do not implement
00041 protected:
00042   Constant(const Type *Ty, ValueTy vty, Use *Ops, unsigned NumOps,
00043            const std::string& Name = "")
00044     : User(Ty, vty, Ops, NumOps, Name) {}
00045 
00046   void destroyConstantImpl();
00047 public:
00048   /// Static constructor to get a '0' constant of arbitrary type...
00049   ///
00050   static Constant *getNullValue(const Type *Ty);
00051 
00052   /// isNullValue - Return true if this is the value that would be returned by
00053   /// getNullValue.
00054   virtual bool isNullValue() const = 0;
00055 
00056   virtual void print(std::ostream &O) const;
00057 
00058   // Specialize get/setOperand for Constant's as their operands are always
00059   // constants as well.
00060   Constant *getOperand(unsigned i) {
00061     return static_cast<Constant*>(User::getOperand(i));
00062   }
00063   const Constant *getOperand(unsigned i) const {
00064     return static_cast<const Constant*>(User::getOperand(i));
00065   }
00066   void setOperand(unsigned i, Constant *C) {
00067     User::setOperand(i, C);
00068   }
00069 
00070   /// destroyConstant - Called if some element of this constant is no longer
00071   /// valid.  At this point only other constants may be on the use_list for this
00072   /// constant.  Any constants on our Use list must also be destroy'd.  The
00073   /// implementation must be sure to remove the constant from the list of
00074   /// available cached constants.  Implementations should call
00075   /// destroyConstantImpl as the last thing they do, to destroy all users and
00076   /// delete this.
00077   virtual void destroyConstant() { assert(0 && "Not reached!"); }
00078 
00079   //// Methods for support type inquiry through isa, cast, and dyn_cast:
00080   static inline bool classof(const Constant *) { return true; }
00081   static inline bool classof(const GlobalValue *) { return true; }
00082   static inline bool classof(const Value *V) {
00083     return V->getValueType() >= ConstantFirstVal &&
00084            V->getValueType() <= ConstantLastVal;
00085   }
00086 
00087   /// replaceUsesOfWithOnConstant - This method is a special form of
00088   /// User::replaceUsesOfWith (which does not work on constants) that does work
00089   /// on constants.  Basically this method goes through the trouble of building
00090   /// a new constant that is equivalent to the current one, with all uses of
00091   /// From replaced with uses of To.  After this construction is completed, all
00092   /// of the users of 'this' are replaced to use the new constant, and then
00093   /// 'this' is deleted.  In general, you should not call this method, instead,
00094   /// use Value::replaceAllUsesWith, which automatically dispatches to this
00095   /// method as needed.
00096   ///
00097   virtual void replaceUsesOfWithOnConstant(Value *, Value *, Use *) {
00098     // Provide a default implementation for constants (like integers) that
00099     // cannot use any other values.  This cannot be called at runtime, but needs
00100     // to be here to avoid link errors.
00101     assert(getNumOperands() == 0 && "replaceUsesOfWithOnConstant must be "
00102            "implemented for all constants that have operands!");
00103     assert(0 && "Constants that do not have operands cannot be using 'From'!");
00104   }
00105 
00106   /// clearAllValueMaps - This method frees all internal memory used by the
00107   /// constant subsystem, which can be used in environments where this memory
00108   /// is otherwise reported as a leak.
00109   static void clearAllValueMaps();
00110   
00111   /// getStringValue - Turn an LLVM constant pointer that eventually points to a
00112   /// global into a string value.  Return an empty string if we can't do it.
00113   /// Parameter Chop determines if the result is chopped at the first null
00114   /// terminator.
00115   ///
00116   std::string getStringValue(bool Chop = true, unsigned Offset = 0);
00117 };
00118 
00119 } // End llvm namespace
00120 
00121 #endif