Home | History | Annotate | Download | only in AST
      1 //===--- AttrIterator.h - Classes for attribute iteration -------*- 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 vector and specific_attr_iterator interfaces.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_CLANG_AST_ATTRITERATOR_H
     15 #define LLVM_CLANG_AST_ATTRITERATOR_H
     16 
     17 #include "clang/Basic/LLVM.h"
     18 #include <iterator>
     19 
     20 namespace clang {
     21   class ASTContext;
     22   class Attr;
     23 }
     24 
     25 // Defined in ASTContext.h
     26 void *operator new(size_t Bytes, const clang::ASTContext &C,
     27                    size_t Alignment = 8);
     28 // FIXME: Being forced to not have a default argument here due to redeclaration
     29 //        rules on default arguments sucks
     30 void *operator new[](size_t Bytes, const clang::ASTContext &C,
     31                      size_t Alignment);
     32 
     33 // It is good practice to pair new/delete operators.  Also, MSVC gives many
     34 // warnings if a matching delete overload is not declared, even though the
     35 // throw() spec guarantees it will not be implicitly called.
     36 void operator delete(void *Ptr, const clang::ASTContext &C, size_t);
     37 void operator delete[](void *Ptr, const clang::ASTContext &C, size_t);
     38 
     39 namespace clang {
     40 
     41 /// AttrVec - A vector of Attr, which is how they are stored on the AST.
     42 typedef SmallVector<Attr *, 4> AttrVec;
     43 
     44 /// specific_attr_iterator - Iterates over a subrange of an AttrVec, only
     45 /// providing attributes that are of a specific type.
     46 template <typename SpecificAttr, typename Container = AttrVec>
     47 class specific_attr_iterator {
     48   typedef typename Container::const_iterator Iterator;
     49 
     50   /// Current - The current, underlying iterator.
     51   /// In order to ensure we don't dereference an invalid iterator unless
     52   /// specifically requested, we don't necessarily advance this all the
     53   /// way. Instead, we advance it when an operation is requested; if the
     54   /// operation is acting on what should be a past-the-end iterator,
     55   /// then we offer no guarantees, but this way we do not dereference a
     56   /// past-the-end iterator when we move to a past-the-end position.
     57   mutable Iterator Current;
     58 
     59   void AdvanceToNext() const {
     60     while (!isa<SpecificAttr>(*Current))
     61       ++Current;
     62   }
     63 
     64   void AdvanceToNext(Iterator I) const {
     65     while (Current != I && !isa<SpecificAttr>(*Current))
     66       ++Current;
     67   }
     68 
     69 public:
     70   typedef SpecificAttr*             value_type;
     71   typedef SpecificAttr*             reference;
     72   typedef SpecificAttr*             pointer;
     73   typedef std::forward_iterator_tag iterator_category;
     74   typedef std::ptrdiff_t            difference_type;
     75 
     76   specific_attr_iterator() : Current() { }
     77   explicit specific_attr_iterator(Iterator i) : Current(i) { }
     78 
     79   reference operator*() const {
     80     AdvanceToNext();
     81     return cast<SpecificAttr>(*Current);
     82   }
     83   pointer operator->() const {
     84     AdvanceToNext();
     85     return cast<SpecificAttr>(*Current);
     86   }
     87 
     88   specific_attr_iterator& operator++() {
     89     ++Current;
     90     return *this;
     91   }
     92   specific_attr_iterator operator++(int) {
     93     specific_attr_iterator Tmp(*this);
     94     ++(*this);
     95     return Tmp;
     96   }
     97 
     98   friend bool operator==(specific_attr_iterator Left,
     99                          specific_attr_iterator Right) {
    100     assert((Left.Current == nullptr) == (Right.Current == nullptr));
    101     if (Left.Current < Right.Current)
    102       Left.AdvanceToNext(Right.Current);
    103     else
    104       Right.AdvanceToNext(Left.Current);
    105     return Left.Current == Right.Current;
    106   }
    107   friend bool operator!=(specific_attr_iterator Left,
    108                          specific_attr_iterator Right) {
    109     return !(Left == Right);
    110   }
    111 };
    112 
    113 template <typename SpecificAttr, typename Container>
    114 inline specific_attr_iterator<SpecificAttr, Container>
    115           specific_attr_begin(const Container& container) {
    116   return specific_attr_iterator<SpecificAttr, Container>(container.begin());
    117 }
    118 template <typename SpecificAttr, typename Container>
    119 inline specific_attr_iterator<SpecificAttr, Container>
    120           specific_attr_end(const Container& container) {
    121   return specific_attr_iterator<SpecificAttr, Container>(container.end());
    122 }
    123 
    124 template <typename SpecificAttr, typename Container>
    125 inline bool hasSpecificAttr(const Container& container) {
    126   return specific_attr_begin<SpecificAttr>(container) !=
    127           specific_attr_end<SpecificAttr>(container);
    128 }
    129 template <typename SpecificAttr, typename Container>
    130 inline SpecificAttr *getSpecificAttr(const Container& container) {
    131   specific_attr_iterator<SpecificAttr, Container> i =
    132       specific_attr_begin<SpecificAttr>(container);
    133   if (i != specific_attr_end<SpecificAttr>(container))
    134     return *i;
    135   else
    136     return nullptr;
    137 }
    138 
    139 }  // end namespace clang
    140 
    141 #endif
    142