Home | History | Annotate | Download | only in AST
      1 //===--- Attr.h - Classes for representing expressions ----------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 //  This file defines the Attr interface and subclasses.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_CLANG_AST_ATTR_H
     15 #define LLVM_CLANG_AST_ATTR_H
     16 
     17 #include "clang/Basic/LLVM.h"
     18 #include "clang/Basic/AttrKinds.h"
     19 #include "clang/AST/Type.h"
     20 #include "clang/Basic/SourceLocation.h"
     21 #include "clang/Basic/VersionTuple.h"
     22 #include "llvm/ADT/SmallVector.h"
     23 #include "llvm/ADT/StringRef.h"
     24 #include "llvm/ADT/StringSwitch.h"
     25 #include "llvm/Support/ErrorHandling.h"
     26 #include "llvm/Support/raw_ostream.h"
     27 #include <cassert>
     28 #include <cstring>
     29 #include <algorithm>
     30 
     31 namespace clang {
     32   class ASTContext;
     33   class IdentifierInfo;
     34   class ObjCInterfaceDecl;
     35   class Expr;
     36   class QualType;
     37   class FunctionDecl;
     38   class TypeSourceInfo;
     39 }
     40 
     41 // Defined in ASTContext.h
     42 void *operator new(size_t Bytes, const clang::ASTContext &C,
     43                    size_t Alignment = 16);
     44 // FIXME: Being forced to not have a default argument here due to redeclaration
     45 //        rules on default arguments sucks
     46 void *operator new[](size_t Bytes, const clang::ASTContext &C,
     47                      size_t Alignment);
     48 
     49 // It is good practice to pair new/delete operators.  Also, MSVC gives many
     50 // warnings if a matching delete overload is not declared, even though the
     51 // throw() spec guarantees it will not be implicitly called.
     52 void operator delete(void *Ptr, const clang::ASTContext &C, size_t);
     53 void operator delete[](void *Ptr, const clang::ASTContext &C, size_t);
     54 
     55 namespace clang {
     56 
     57 /// Attr - This represents one attribute.
     58 class Attr {
     59 private:
     60   SourceRange Range;
     61   unsigned AttrKind : 16;
     62 
     63 protected:
     64   bool Inherited : 1;
     65 
     66   virtual ~Attr();
     67 
     68   void* operator new(size_t bytes) throw() {
     69     llvm_unreachable("Attrs cannot be allocated with regular 'new'.");
     70   }
     71   void operator delete(void* data) throw() {
     72     llvm_unreachable("Attrs cannot be released with regular 'delete'.");
     73   }
     74 
     75 public:
     76   // Forward so that the regular new and delete do not hide global ones.
     77   void* operator new(size_t Bytes, ASTContext &C,
     78                      size_t Alignment = 16) throw() {
     79     return ::operator new(Bytes, C, Alignment);
     80   }
     81   void operator delete(void *Ptr, ASTContext &C,
     82                        size_t Alignment) throw() {
     83     return ::operator delete(Ptr, C, Alignment);
     84   }
     85 
     86 protected:
     87   Attr(attr::Kind AK, SourceRange R)
     88     : Range(R), AttrKind(AK), Inherited(false) {}
     89 
     90 public:
     91 
     92   attr::Kind getKind() const {
     93     return static_cast<attr::Kind>(AttrKind);
     94   }
     95 
     96   SourceLocation getLocation() const { return Range.getBegin(); }
     97   SourceRange getRange() const { return Range; }
     98   void setRange(SourceRange R) { Range = R; }
     99 
    100   bool isInherited() const { return Inherited; }
    101 
    102   // Clone this attribute.
    103   virtual Attr* clone(ASTContext &C) const = 0;
    104 
    105   virtual bool isLateParsed() const { return false; }
    106 
    107   // Pretty print this attribute.
    108   virtual void printPretty(llvm::raw_ostream &OS, ASTContext &C) const = 0;
    109 
    110   // Implement isa/cast/dyncast/etc.
    111   static bool classof(const Attr *) { return true; }
    112 };
    113 
    114 class InheritableAttr : public Attr {
    115   virtual void anchor();
    116 protected:
    117   InheritableAttr(attr::Kind AK, SourceRange R)
    118     : Attr(AK, R) {}
    119 
    120 public:
    121   void setInherited(bool I) { Inherited = I; }
    122 
    123   // Implement isa/cast/dyncast/etc.
    124   static bool classof(const Attr *A) {
    125     return A->getKind() <= attr::LAST_INHERITABLE;
    126   }
    127   static bool classof(const InheritableAttr *) { return true; }
    128 };
    129 
    130 class InheritableParamAttr : public InheritableAttr {
    131   virtual void anchor();
    132 protected:
    133   InheritableParamAttr(attr::Kind AK, SourceRange R)
    134     : InheritableAttr(AK, R) {}
    135 
    136 public:
    137   // Implement isa/cast/dyncast/etc.
    138   static bool classof(const Attr *A) {
    139     return A->getKind() <= attr::LAST_INHERITABLE_PARAM;
    140   }
    141   static bool classof(const InheritableParamAttr *) { return true; }
    142 };
    143 
    144 #include "clang/AST/Attrs.inc"
    145 
    146 /// AttrVec - A vector of Attr, which is how they are stored on the AST.
    147 typedef SmallVector<Attr*, 2> AttrVec;
    148 typedef SmallVector<const Attr*, 2> ConstAttrVec;
    149 
    150 /// DestroyAttrs - Destroy the contents of an AttrVec.
    151 inline void DestroyAttrs (AttrVec& V, ASTContext &C) {
    152 }
    153 
    154 /// specific_attr_iterator - Iterates over a subrange of an AttrVec, only
    155 /// providing attributes that are of a specifc type.
    156 template <typename SpecificAttr>
    157 class specific_attr_iterator {
    158   /// Current - The current, underlying iterator.
    159   /// In order to ensure we don't dereference an invalid iterator unless
    160   /// specifically requested, we don't necessarily advance this all the
    161   /// way. Instead, we advance it when an operation is requested; if the
    162   /// operation is acting on what should be a past-the-end iterator,
    163   /// then we offer no guarantees, but this way we do not dererence a
    164   /// past-the-end iterator when we move to a past-the-end position.
    165   mutable AttrVec::const_iterator Current;
    166 
    167   void AdvanceToNext() const {
    168     while (!isa<SpecificAttr>(*Current))
    169       ++Current;
    170   }
    171 
    172   void AdvanceToNext(AttrVec::const_iterator I) const {
    173     while (Current != I && !isa<SpecificAttr>(*Current))
    174       ++Current;
    175   }
    176 
    177 public:
    178   typedef SpecificAttr*             value_type;
    179   typedef SpecificAttr*             reference;
    180   typedef SpecificAttr*             pointer;
    181   typedef std::forward_iterator_tag iterator_category;
    182   typedef std::ptrdiff_t            difference_type;
    183 
    184   specific_attr_iterator() : Current() { }
    185   explicit specific_attr_iterator(AttrVec::const_iterator i) : Current(i) { }
    186 
    187   reference operator*() const {
    188     AdvanceToNext();
    189     return cast<SpecificAttr>(*Current);
    190   }
    191   pointer operator->() const {
    192     AdvanceToNext();
    193     return cast<SpecificAttr>(*Current);
    194   }
    195 
    196   specific_attr_iterator& operator++() {
    197     ++Current;
    198     return *this;
    199   }
    200   specific_attr_iterator operator++(int) {
    201     specific_attr_iterator Tmp(*this);
    202     ++(*this);
    203     return Tmp;
    204   }
    205 
    206   friend bool operator==(specific_attr_iterator Left,
    207                          specific_attr_iterator Right) {
    208     if (Left.Current < Right.Current)
    209       Left.AdvanceToNext(Right.Current);
    210     else
    211       Right.AdvanceToNext(Left.Current);
    212     return Left.Current == Right.Current;
    213   }
    214   friend bool operator!=(specific_attr_iterator Left,
    215                          specific_attr_iterator Right) {
    216     return !(Left == Right);
    217   }
    218 };
    219 
    220 template <typename T>
    221 inline specific_attr_iterator<T> specific_attr_begin(const AttrVec& vec) {
    222   return specific_attr_iterator<T>(vec.begin());
    223 }
    224 template <typename T>
    225 inline specific_attr_iterator<T> specific_attr_end(const AttrVec& vec) {
    226   return specific_attr_iterator<T>(vec.end());
    227 }
    228 
    229 template <typename T>
    230 inline bool hasSpecificAttr(const AttrVec& vec) {
    231   return specific_attr_begin<T>(vec) != specific_attr_end<T>(vec);
    232 }
    233 template <typename T>
    234 inline T *getSpecificAttr(const AttrVec& vec) {
    235   specific_attr_iterator<T> i = specific_attr_begin<T>(vec);
    236   if (i != specific_attr_end<T>(vec))
    237     return *i;
    238   else
    239     return 0;
    240 }
    241 
    242 /// getMaxAlignment - Returns the highest alignment value found among
    243 /// AlignedAttrs in an AttrVec, or 0 if there are none.
    244 inline unsigned getMaxAttrAlignment(const AttrVec& V, ASTContext &Ctx) {
    245   unsigned Align = 0;
    246   specific_attr_iterator<AlignedAttr> i(V.begin()), e(V.end());
    247   for(; i != e; ++i)
    248     Align = std::max(Align, i->getAlignment(Ctx));
    249   return Align;
    250 }
    251 
    252 }  // end namespace clang
    253 
    254 #endif
    255