LLVM API Documentation

AttributeImpl.h
Go to the documentation of this file.
00001 //===-- AttributeImpl.h - Attribute Internals -------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 ///
00010 /// \file
00011 /// \brief This file defines various helper methods and classes used by
00012 /// LLVMContextImpl for creating and managing attributes.
00013 ///
00014 //===----------------------------------------------------------------------===//
00015 
00016 #ifndef LLVM_LIB_IR_ATTRIBUTEIMPL_H
00017 #define LLVM_LIB_IR_ATTRIBUTEIMPL_H
00018 
00019 #include "llvm/ADT/FoldingSet.h"
00020 #include "llvm/IR/Attributes.h"
00021 #include <string>
00022 
00023 namespace llvm {
00024 
00025 class Constant;
00026 class LLVMContext;
00027 
00028 //===----------------------------------------------------------------------===//
00029 /// \class
00030 /// \brief This class represents a single, uniqued attribute. That attribute
00031 /// could be a single enum, a tuple, or a string.
00032 class AttributeImpl : public FoldingSetNode {
00033   unsigned char KindID; ///< Holds the AttrEntryKind of the attribute
00034 
00035   // AttributesImpl is uniqued, these should not be publicly available.
00036   void operator=(const AttributeImpl &) LLVM_DELETED_FUNCTION;
00037   AttributeImpl(const AttributeImpl &) LLVM_DELETED_FUNCTION;
00038 
00039 protected:
00040   enum AttrEntryKind {
00041     EnumAttrEntry,
00042     IntAttrEntry,
00043     StringAttrEntry
00044   };
00045 
00046   AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {}
00047 
00048 public:
00049   virtual ~AttributeImpl();
00050 
00051   bool isEnumAttribute() const { return KindID == EnumAttrEntry; }
00052   bool isIntAttribute() const { return KindID == IntAttrEntry; }
00053   bool isStringAttribute() const { return KindID == StringAttrEntry; }
00054 
00055   bool hasAttribute(Attribute::AttrKind A) const;
00056   bool hasAttribute(StringRef Kind) const;
00057 
00058   Attribute::AttrKind getKindAsEnum() const;
00059   uint64_t getValueAsInt() const;
00060 
00061   StringRef getKindAsString() const;
00062   StringRef getValueAsString() const;
00063 
00064   /// \brief Used when sorting the attributes.
00065   bool operator<(const AttributeImpl &AI) const;
00066 
00067   void Profile(FoldingSetNodeID &ID) const {
00068     if (isEnumAttribute())
00069       Profile(ID, getKindAsEnum(), 0);
00070     else if (isIntAttribute())
00071       Profile(ID, getKindAsEnum(), getValueAsInt());
00072     else
00073       Profile(ID, getKindAsString(), getValueAsString());
00074   }
00075   static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind,
00076                       uint64_t Val) {
00077     ID.AddInteger(Kind);
00078     if (Val) ID.AddInteger(Val);
00079   }
00080   static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) {
00081     ID.AddString(Kind);
00082     if (!Values.empty()) ID.AddString(Values);
00083   }
00084 
00085   // FIXME: Remove this!
00086   static uint64_t getAttrMask(Attribute::AttrKind Val);
00087 };
00088 
00089 //===----------------------------------------------------------------------===//
00090 /// \class
00091 /// \brief A set of classes that contain the value of the
00092 /// attribute object. There are three main categories: enum attribute entries,
00093 /// represented by Attribute::AttrKind; alignment attribute entries; and string
00094 /// attribute enties, which are for target-dependent attributes.
00095 
00096 class EnumAttributeImpl : public AttributeImpl {
00097   virtual void anchor();
00098   Attribute::AttrKind Kind;
00099 
00100 protected:
00101   EnumAttributeImpl(AttrEntryKind ID, Attribute::AttrKind Kind)
00102       : AttributeImpl(ID), Kind(Kind) {}
00103 
00104 public:
00105   EnumAttributeImpl(Attribute::AttrKind Kind)
00106       : AttributeImpl(EnumAttrEntry), Kind(Kind) {}
00107 
00108   Attribute::AttrKind getEnumKind() const { return Kind; }
00109 };
00110 
00111 class IntAttributeImpl : public EnumAttributeImpl {
00112   void anchor() override;
00113   uint64_t Val;
00114 
00115 public:
00116   IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val)
00117       : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) {
00118     assert(
00119         (Kind == Attribute::Alignment || Kind == Attribute::StackAlignment ||
00120          Kind == Attribute::Dereferenceable) &&
00121         "Wrong kind for int attribute!");
00122   }
00123 
00124   uint64_t getValue() const { return Val; }
00125 };
00126 
00127 class StringAttributeImpl : public AttributeImpl {
00128   virtual void anchor();
00129   std::string Kind;
00130   std::string Val;
00131 
00132 public:
00133   StringAttributeImpl(StringRef Kind, StringRef Val = StringRef())
00134       : AttributeImpl(StringAttrEntry), Kind(Kind), Val(Val) {}
00135 
00136   StringRef getStringKind() const { return Kind; }
00137   StringRef getStringValue() const { return Val; }
00138 };
00139 
00140 //===----------------------------------------------------------------------===//
00141 /// \class
00142 /// \brief This class represents a group of attributes that apply to one
00143 /// element: function, return type, or parameter.
00144 class AttributeSetNode : public FoldingSetNode {
00145   unsigned NumAttrs; ///< Number of attributes in this node.
00146 
00147   AttributeSetNode(ArrayRef<Attribute> Attrs) : NumAttrs(Attrs.size()) {
00148     // There's memory after the node where we can store the entries in.
00149     std::copy(Attrs.begin(), Attrs.end(),
00150               reinterpret_cast<Attribute *>(this + 1));
00151   }
00152 
00153   // AttributesSetNode is uniqued, these should not be publicly available.
00154   void operator=(const AttributeSetNode &) LLVM_DELETED_FUNCTION;
00155   AttributeSetNode(const AttributeSetNode &) LLVM_DELETED_FUNCTION;
00156 public:
00157   static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
00158 
00159   bool hasAttribute(Attribute::AttrKind Kind) const;
00160   bool hasAttribute(StringRef Kind) const;
00161   bool hasAttributes() const { return NumAttrs != 0; }
00162 
00163   Attribute getAttribute(Attribute::AttrKind Kind) const;
00164   Attribute getAttribute(StringRef Kind) const;
00165 
00166   unsigned getAlignment() const;
00167   unsigned getStackAlignment() const;
00168   uint64_t getDereferenceableBytes() const;
00169   std::string getAsString(bool InAttrGrp) const;
00170 
00171   typedef const Attribute *iterator;
00172   iterator begin() const { return reinterpret_cast<iterator>(this + 1); }
00173   iterator end() const { return begin() + NumAttrs; }
00174 
00175   void Profile(FoldingSetNodeID &ID) const {
00176     Profile(ID, makeArrayRef(begin(), end()));
00177   }
00178   static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
00179     for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
00180       AttrList[I].Profile(ID);
00181   }
00182 };
00183 
00184 //===----------------------------------------------------------------------===//
00185 /// \class
00186 /// \brief This class represents a set of attributes that apply to the function,
00187 /// return type, and parameters.
00188 class AttributeSetImpl : public FoldingSetNode {
00189   friend class AttributeSet;
00190 
00191   LLVMContext &Context;
00192 
00193   typedef std::pair<unsigned, AttributeSetNode*> IndexAttrPair;
00194   unsigned NumAttrs; ///< Number of entries in this set.
00195 
00196   /// \brief Return a pointer to the IndexAttrPair for the specified slot.
00197   const IndexAttrPair *getNode(unsigned Slot) const {
00198     return reinterpret_cast<const IndexAttrPair *>(this + 1) + Slot;
00199   }
00200 
00201   // AttributesSet is uniqued, these should not be publicly available.
00202   void operator=(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
00203   AttributeSetImpl(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
00204 public:
00205   AttributeSetImpl(LLVMContext &C,
00206                    ArrayRef<std::pair<unsigned, AttributeSetNode *> > Attrs)
00207       : Context(C), NumAttrs(Attrs.size()) {
00208 #ifndef NDEBUG
00209     if (Attrs.size() >= 2) {
00210       for (const std::pair<unsigned, AttributeSetNode *> *i = Attrs.begin() + 1,
00211                                                          *e = Attrs.end();
00212            i != e; ++i) {
00213         assert((i-1)->first <= i->first && "Attribute set not ordered!");
00214       }
00215     }
00216 #endif
00217     // There's memory after the node where we can store the entries in.
00218     std::copy(Attrs.begin(), Attrs.end(),
00219               reinterpret_cast<IndexAttrPair *>(this + 1));
00220   }
00221 
00222   /// \brief Get the context that created this AttributeSetImpl.
00223   LLVMContext &getContext() { return Context; }
00224 
00225   /// \brief Return the number of attributes this AttributeSet contains.
00226   unsigned getNumAttributes() const { return NumAttrs; }
00227 
00228   /// \brief Get the index of the given "slot" in the AttrNodes list. This index
00229   /// is the index of the return, parameter, or function object that the
00230   /// attributes are applied to, not the index into the AttrNodes list where the
00231   /// attributes reside.
00232   unsigned getSlotIndex(unsigned Slot) const {
00233     return getNode(Slot)->first;
00234   }
00235 
00236   /// \brief Retrieve the attributes for the given "slot" in the AttrNode list.
00237   /// \p Slot is an index into the AttrNodes list, not the index of the return /
00238   /// parameter/ function which the attributes apply to.
00239   AttributeSet getSlotAttributes(unsigned Slot) const {
00240     return AttributeSet::get(Context, *getNode(Slot));
00241   }
00242 
00243   /// \brief Retrieve the attribute set node for the given "slot" in the
00244   /// AttrNode list.
00245   AttributeSetNode *getSlotNode(unsigned Slot) const {
00246     return getNode(Slot)->second;
00247   }
00248 
00249   typedef AttributeSetNode::iterator iterator;
00250   iterator begin(unsigned Slot) const { return getSlotNode(Slot)->begin(); }
00251   iterator end(unsigned Slot) const { return getSlotNode(Slot)->end(); }
00252 
00253   void Profile(FoldingSetNodeID &ID) const {
00254     Profile(ID, makeArrayRef(getNode(0), getNumAttributes()));
00255   }
00256   static void Profile(FoldingSetNodeID &ID,
00257                       ArrayRef<std::pair<unsigned, AttributeSetNode*> > Nodes) {
00258     for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
00259       ID.AddInteger(Nodes[i].first);
00260       ID.AddPointer(Nodes[i].second);
00261     }
00262   }
00263 
00264   // FIXME: This atrocity is temporary.
00265   uint64_t Raw(unsigned Index) const;
00266 
00267   void dump() const;
00268 };
00269 
00270 } // end llvm namespace
00271 
00272 #endif