Added a parameter to control whether Constant::getStringValue() would chop
[oota-llvm.git] / include / llvm / Constant.h
1 //===-- llvm/Constant.h - Constant class definition -------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the declaration of the Constant class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CONSTANT_H
15 #define LLVM_CONSTANT_H
16
17 #include "llvm/User.h"
18
19 namespace llvm {
20
21 class Constant : public User {
22   void operator=(const Constant &);     // Do not implement
23   Constant(const Constant &);           // Do not implement
24 protected:
25   Constant(const Type *Ty, ValueTy vty, Use *Ops, unsigned NumOps,
26            const std::string& Name = "")
27     : User(Ty, vty, Ops, NumOps, Name) {}
28
29   void destroyConstantImpl();
30 public:
31   /// Static constructor to get a '0' constant of arbitrary type...
32   ///
33   static Constant *getNullValue(const Type *Ty);
34
35   /// isNullValue - Return true if this is the value that would be returned by
36   /// getNullValue.
37   virtual bool isNullValue() const = 0;
38
39   virtual void print(std::ostream &O) const;
40
41   // Specialize get/setOperand for Constant's as their operands are always
42   // constants as well.
43   Constant *getOperand(unsigned i) {
44     return static_cast<Constant*>(User::getOperand(i));
45   }
46   const Constant *getOperand(unsigned i) const {
47     return static_cast<const Constant*>(User::getOperand(i));
48   }
49   void setOperand(unsigned i, Constant *C) {
50     User::setOperand(i, C);
51   }
52
53   /// destroyConstant - Called if some element of this constant is no longer
54   /// valid.  At this point only other constants may be on the use_list for this
55   /// constant.  Any constants on our Use list must also be destroy'd.  The
56   /// implementation must be sure to remove the constant from the list of
57   /// available cached constants.  Implementations should call
58   /// destroyConstantImpl as the last thing they do, to destroy all users and
59   /// delete this.
60   virtual void destroyConstant() { assert(0 && "Not reached!"); }
61
62   //// Methods for support type inquiry through isa, cast, and dyn_cast:
63   static inline bool classof(const Constant *) { return true; }
64   static inline bool classof(const GlobalValue *) { return true; }
65   static inline bool classof(const Value *V) {
66     return V->getValueType() >= ConstantFirstVal &&
67            V->getValueType() <= ConstantLastVal;
68   }
69
70   /// replaceUsesOfWithOnConstant - This method is a special form of
71   /// User::replaceUsesOfWith (which does not work on constants) that does work
72   /// on constants.  Basically this method goes through the trouble of building
73   /// a new constant that is equivalent to the current one, with all uses of
74   /// From replaced with uses of To.  After this construction is completed, all
75   /// of the users of 'this' are replaced to use the new constant, and then
76   /// 'this' is deleted.  In general, you should not call this method, instead,
77   /// use Value::replaceAllUsesWith, which automatically dispatches to this
78   /// method as needed.
79   ///
80   virtual void replaceUsesOfWithOnConstant(Value *, Value *, Use *) {
81     // Provide a default implementation for constants (like integers) that
82     // cannot use any other values.  This cannot be called at runtime, but needs
83     // to be here to avoid link errors.
84     assert(getNumOperands() == 0 && "replaceUsesOfWithOnConstant must be "
85            "implemented for all constants that have operands!");
86     assert(0 && "Constants that do not have operands cannot be using 'From'!");
87   }
88
89   /// clearAllValueMaps - This method frees all internal memory used by the
90   /// constant subsystem, which can be used in environments where this memory
91   /// is otherwise reported as a leak.
92   static void clearAllValueMaps();
93   
94   /// getStringValue - Turn an LLVM constant pointer that eventually points to a
95   /// global into a string value.  Return an empty string if we can't do it.
96   /// Parameter Chop determines if the result is chopped at the first null
97   /// terminator.
98   ///
99   std::string getStringValue(bool Chop = true, unsigned Offset = 0);
100 };
101
102 } // End llvm namespace
103
104 #endif