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 class Constant : public User {
00022   void operator=(const Constant &);     // Do not implement
00023   Constant(const Constant &);           // Do not implement
00024 protected:
00025   Constant(const Type *Ty, ValueTy vty, Use *Ops, unsigned NumOps,
00026            const std::string& Name = "")
00027     : User(Ty, vty, Ops, NumOps, Name) {}
00028 
00029   void destroyConstantImpl();
00030 public:
00031   /// Static constructor to get a '0' constant of arbitrary type...
00032   ///
00033   static Constant *getNullValue(const Type *Ty);
00034 
00035   /// isNullValue - Return true if this is the value that would be returned by
00036   /// getNullValue.
00037   virtual bool isNullValue() const = 0;
00038 
00039   virtual void print(std::ostream &O) const;
00040 
00041   // Specialize get/setOperand for Constant's as their operands are always
00042   // constants as well.
00043   Constant *getOperand(unsigned i) {
00044     return static_cast<Constant*>(User::getOperand(i));
00045   }
00046   const Constant *getOperand(unsigned i) const {
00047     return static_cast<const Constant*>(User::getOperand(i));
00048   }
00049   void setOperand(unsigned i, Constant *C) {
00050     User::setOperand(i, C);
00051   }
00052 
00053   /// destroyConstant - Called if some element of this constant is no longer
00054   /// valid.  At this point only other constants may be on the use_list for this
00055   /// constant.  Any constants on our Use list must also be destroy'd.  The
00056   /// implementation must be sure to remove the constant from the list of
00057   /// available cached constants.  Implementations should call
00058   /// destroyConstantImpl as the last thing they do, to destroy all users and
00059   /// delete this.
00060   virtual void destroyConstant() { assert(0 && "Not reached!"); }
00061 
00062   //// Methods for support type inquiry through isa, cast, and dyn_cast:
00063   static inline bool classof(const Constant *) { return true; }
00064   static inline bool classof(const GlobalValue *) { return true; }
00065   static inline bool classof(const Value *V) {
00066     return V->getValueType() >= ConstantFirstVal &&
00067            V->getValueType() <= ConstantLastVal;
00068   }
00069 
00070   /// replaceUsesOfWithOnConstant - This method is a special form of
00071   /// User::replaceUsesOfWith (which does not work on constants) that does work
00072   /// on constants.  Basically this method goes through the trouble of building
00073   /// a new constant that is equivalent to the current one, with all uses of
00074   /// From replaced with uses of To.  After this construction is completed, all
00075   /// of the users of 'this' are replaced to use the new constant, and then
00076   /// 'this' is deleted.  In general, you should not call this method, instead,
00077   /// use Value::replaceAllUsesWith, which automatically dispatches to this
00078   /// method as needed.
00079   ///
00080   virtual void replaceUsesOfWithOnConstant(Value *, Value *, Use *) {
00081     // Provide a default implementation for constants (like integers) that
00082     // cannot use any other values.  This cannot be called at runtime, but needs
00083     // to be here to avoid link errors.
00084     assert(getNumOperands() == 0 && "replaceUsesOfWithOnConstant must be "
00085            "implemented for all constants that have operands!");
00086     assert(0 && "Constants that do not have operands cannot be using 'From'!");
00087   }
00088 
00089   /// clearAllValueMaps - This method frees all internal memory used by the
00090   /// constant subsystem, which can be used in environments where this memory
00091   /// is otherwise reported as a leak.
00092   static void clearAllValueMaps();
00093   
00094   /// getStringValue - Turn an LLVM constant pointer that eventually points to a
00095   /// global into a string value.  Return an empty string if we can't do it.
00096   /// Parameter Chop determines if the result is chopped at the first null
00097   /// terminator.
00098   ///
00099   std::string getStringValue(bool Chop = true, unsigned Offset = 0);
00100 };
00101 
00102 } // End llvm namespace
00103 
00104 #endif