2012-08-15 20:02:54 +00:00
|
|
|
//===--- Attr.h - Classes for representing attributes ----------*- C++ -*-===//
|
2009-06-02 17:58:47 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the Attr interface and subclasses.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_CLANG_AST_ATTR_H
|
|
|
|
#define LLVM_CLANG_AST_ATTR_H
|
|
|
|
|
2011-10-20 21:14:49 +00:00
|
|
|
#include "clang/Basic/LLVM.h"
|
2010-07-13 17:21:42 +00:00
|
|
|
#include "clang/Basic/AttrKinds.h"
|
2010-09-17 15:54:40 +00:00
|
|
|
#include "clang/AST/Type.h"
|
|
|
|
#include "clang/Basic/SourceLocation.h"
|
2011-05-02 19:39:53 +00:00
|
|
|
#include "clang/Basic/VersionTuple.h"
|
2011-10-20 21:14:49 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2012-04-14 14:01:31 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-06-02 17:58:47 +00:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstring>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
class ASTContext;
|
2010-05-27 15:17:06 +00:00
|
|
|
class IdentifierInfo;
|
|
|
|
class ObjCInterfaceDecl;
|
2010-07-13 17:21:42 +00:00
|
|
|
class Expr;
|
2010-09-17 15:54:40 +00:00
|
|
|
class QualType;
|
|
|
|
class FunctionDecl;
|
|
|
|
class TypeSourceInfo;
|
2009-06-02 17:58:47 +00:00
|
|
|
}
|
|
|
|
|
2009-06-14 09:24:02 +00:00
|
|
|
// Defined in ASTContext.h
|
2011-02-20 13:06:31 +00:00
|
|
|
void *operator new(size_t Bytes, const clang::ASTContext &C,
|
2012-04-14 14:01:31 +00:00
|
|
|
size_t Alignment = 16);
|
2010-09-17 15:54:40 +00:00
|
|
|
// FIXME: Being forced to not have a default argument here due to redeclaration
|
|
|
|
// rules on default arguments sucks
|
2011-02-20 13:06:31 +00:00
|
|
|
void *operator new[](size_t Bytes, const clang::ASTContext &C,
|
2012-04-14 14:01:31 +00:00
|
|
|
size_t Alignment);
|
2009-06-02 17:58:47 +00:00
|
|
|
|
2009-06-14 09:24:02 +00:00
|
|
|
// It is good practice to pair new/delete operators. Also, MSVC gives many
|
|
|
|
// warnings if a matching delete overload is not declared, even though the
|
|
|
|
// throw() spec guarantees it will not be implicitly called.
|
2012-04-14 14:01:31 +00:00
|
|
|
void operator delete(void *Ptr, const clang::ASTContext &C, size_t);
|
|
|
|
void operator delete[](void *Ptr, const clang::ASTContext &C, size_t);
|
2009-06-14 09:24:02 +00:00
|
|
|
|
2009-06-02 17:58:47 +00:00
|
|
|
namespace clang {
|
|
|
|
|
|
|
|
/// Attr - This represents one attribute.
|
|
|
|
class Attr {
|
|
|
|
private:
|
2011-10-20 21:14:49 +00:00
|
|
|
SourceRange Range;
|
2010-09-17 15:54:40 +00:00
|
|
|
unsigned AttrKind : 16;
|
2009-06-02 17:58:47 +00:00
|
|
|
|
|
|
|
protected:
|
2011-02-20 13:06:31 +00:00
|
|
|
bool Inherited : 1;
|
|
|
|
|
2010-09-17 15:54:40 +00:00
|
|
|
virtual ~Attr();
|
|
|
|
|
2009-06-02 17:58:47 +00:00
|
|
|
void* operator new(size_t bytes) throw() {
|
2011-10-20 21:14:49 +00:00
|
|
|
llvm_unreachable("Attrs cannot be allocated with regular 'new'.");
|
2009-06-02 17:58:47 +00:00
|
|
|
}
|
|
|
|
void operator delete(void* data) throw() {
|
2011-10-20 21:14:49 +00:00
|
|
|
llvm_unreachable("Attrs cannot be released with regular 'delete'.");
|
2009-06-02 17:58:47 +00:00
|
|
|
}
|
2009-10-14 18:03:49 +00:00
|
|
|
|
2010-09-17 15:54:40 +00:00
|
|
|
public:
|
|
|
|
// Forward so that the regular new and delete do not hide global ones.
|
|
|
|
void* operator new(size_t Bytes, ASTContext &C,
|
|
|
|
size_t Alignment = 16) throw() {
|
|
|
|
return ::operator new(Bytes, C, Alignment);
|
2009-06-02 17:58:47 +00:00
|
|
|
}
|
2010-09-17 15:54:40 +00:00
|
|
|
void operator delete(void *Ptr, ASTContext &C,
|
|
|
|
size_t Alignment) throw() {
|
|
|
|
return ::operator delete(Ptr, C, Alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2011-10-20 21:14:49 +00:00
|
|
|
Attr(attr::Kind AK, SourceRange R)
|
|
|
|
: Range(R), AttrKind(AK), Inherited(false) {}
|
2010-09-17 15:54:40 +00:00
|
|
|
|
2009-06-02 17:58:47 +00:00
|
|
|
public:
|
2009-10-14 18:03:49 +00:00
|
|
|
|
2010-09-17 15:54:40 +00:00
|
|
|
attr::Kind getKind() const {
|
|
|
|
return static_cast<attr::Kind>(AttrKind);
|
2009-10-14 18:03:49 +00:00
|
|
|
}
|
|
|
|
|
2011-10-20 21:14:49 +00:00
|
|
|
SourceLocation getLocation() const { return Range.getBegin(); }
|
|
|
|
SourceRange getRange() const { return Range; }
|
|
|
|
void setRange(SourceRange R) { Range = R; }
|
2009-10-14 18:03:49 +00:00
|
|
|
|
2010-09-17 15:54:40 +00:00
|
|
|
bool isInherited() const { return Inherited; }
|
2009-10-14 18:03:49 +00:00
|
|
|
|
2009-06-02 17:58:47 +00:00
|
|
|
// Clone this attribute.
|
|
|
|
virtual Attr* clone(ASTContext &C) const = 0;
|
|
|
|
|
2012-04-14 14:01:31 +00:00
|
|
|
virtual bool isLateParsed() const { return false; }
|
|
|
|
|
|
|
|
// Pretty print this attribute.
|
|
|
|
virtual void printPretty(llvm::raw_ostream &OS, ASTContext &C) const = 0;
|
|
|
|
|
2009-06-02 17:58:47 +00:00
|
|
|
// Implement isa/cast/dyncast/etc.
|
|
|
|
static bool classof(const Attr *) { return true; }
|
|
|
|
};
|
2010-07-13 17:21:42 +00:00
|
|
|
|
2011-02-20 13:06:31 +00:00
|
|
|
class InheritableAttr : public Attr {
|
2012-04-14 14:01:31 +00:00
|
|
|
virtual void anchor();
|
2011-02-20 13:06:31 +00:00
|
|
|
protected:
|
2011-10-20 21:14:49 +00:00
|
|
|
InheritableAttr(attr::Kind AK, SourceRange R)
|
|
|
|
: Attr(AK, R) {}
|
2011-02-20 13:06:31 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
void setInherited(bool I) { Inherited = I; }
|
|
|
|
|
|
|
|
// Implement isa/cast/dyncast/etc.
|
|
|
|
static bool classof(const Attr *A) {
|
|
|
|
return A->getKind() <= attr::LAST_INHERITABLE;
|
|
|
|
}
|
|
|
|
static bool classof(const InheritableAttr *) { return true; }
|
|
|
|
};
|
|
|
|
|
2011-05-02 19:39:53 +00:00
|
|
|
class InheritableParamAttr : public InheritableAttr {
|
2012-04-14 14:01:31 +00:00
|
|
|
virtual void anchor();
|
2011-05-02 19:39:53 +00:00
|
|
|
protected:
|
2011-10-20 21:14:49 +00:00
|
|
|
InheritableParamAttr(attr::Kind AK, SourceRange R)
|
|
|
|
: InheritableAttr(AK, R) {}
|
2011-05-02 19:39:53 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
// Implement isa/cast/dyncast/etc.
|
|
|
|
static bool classof(const Attr *A) {
|
|
|
|
return A->getKind() <= attr::LAST_INHERITABLE_PARAM;
|
|
|
|
}
|
|
|
|
static bool classof(const InheritableParamAttr *) { return true; }
|
|
|
|
};
|
|
|
|
|
2010-07-13 17:21:42 +00:00
|
|
|
#include "clang/AST/Attrs.inc"
|
2009-06-02 17:58:47 +00:00
|
|
|
|
2010-09-17 15:54:40 +00:00
|
|
|
/// AttrVec - A vector of Attr, which is how they are stored on the AST.
|
2011-10-20 21:14:49 +00:00
|
|
|
typedef SmallVector<Attr*, 2> AttrVec;
|
|
|
|
typedef SmallVector<const Attr*, 2> ConstAttrVec;
|
2009-06-02 17:58:47 +00:00
|
|
|
|
2010-09-17 15:54:40 +00:00
|
|
|
/// specific_attr_iterator - Iterates over a subrange of an AttrVec, only
|
|
|
|
/// providing attributes that are of a specifc type.
|
2012-08-15 20:02:54 +00:00
|
|
|
template <typename SpecificAttr, typename Container = AttrVec>
|
2010-09-17 15:54:40 +00:00
|
|
|
class specific_attr_iterator {
|
2012-08-15 20:02:54 +00:00
|
|
|
typedef typename Container::const_iterator Iterator;
|
|
|
|
|
2010-09-17 15:54:40 +00:00
|
|
|
/// Current - The current, underlying iterator.
|
|
|
|
/// In order to ensure we don't dereference an invalid iterator unless
|
|
|
|
/// specifically requested, we don't necessarily advance this all the
|
|
|
|
/// way. Instead, we advance it when an operation is requested; if the
|
|
|
|
/// operation is acting on what should be a past-the-end iterator,
|
|
|
|
/// then we offer no guarantees, but this way we do not dererence a
|
|
|
|
/// past-the-end iterator when we move to a past-the-end position.
|
2012-08-15 20:02:54 +00:00
|
|
|
mutable Iterator Current;
|
2009-06-02 17:58:47 +00:00
|
|
|
|
2010-09-17 15:54:40 +00:00
|
|
|
void AdvanceToNext() const {
|
2011-10-20 21:14:49 +00:00
|
|
|
while (!isa<SpecificAttr>(*Current))
|
2010-09-17 15:54:40 +00:00
|
|
|
++Current;
|
2009-12-01 11:08:04 +00:00
|
|
|
}
|
2009-06-02 17:58:47 +00:00
|
|
|
|
2012-08-15 20:02:54 +00:00
|
|
|
void AdvanceToNext(Iterator I) const {
|
2011-10-20 21:14:49 +00:00
|
|
|
while (Current != I && !isa<SpecificAttr>(*Current))
|
2010-09-17 15:54:40 +00:00
|
|
|
++Current;
|
2009-06-02 17:58:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2010-09-17 15:54:40 +00:00
|
|
|
typedef SpecificAttr* value_type;
|
|
|
|
typedef SpecificAttr* reference;
|
|
|
|
typedef SpecificAttr* pointer;
|
|
|
|
typedef std::forward_iterator_tag iterator_category;
|
|
|
|
typedef std::ptrdiff_t difference_type;
|
2009-06-02 17:58:47 +00:00
|
|
|
|
2010-09-17 15:54:40 +00:00
|
|
|
specific_attr_iterator() : Current() { }
|
2012-08-15 20:02:54 +00:00
|
|
|
explicit specific_attr_iterator(Iterator i) : Current(i) { }
|
2009-10-14 18:03:49 +00:00
|
|
|
|
2010-09-17 15:54:40 +00:00
|
|
|
reference operator*() const {
|
|
|
|
AdvanceToNext();
|
2011-10-20 21:14:49 +00:00
|
|
|
return cast<SpecificAttr>(*Current);
|
2009-06-02 17:58:47 +00:00
|
|
|
}
|
2010-09-17 15:54:40 +00:00
|
|
|
pointer operator->() const {
|
|
|
|
AdvanceToNext();
|
2011-10-20 21:14:49 +00:00
|
|
|
return cast<SpecificAttr>(*Current);
|
2010-05-27 15:17:06 +00:00
|
|
|
}
|
2009-06-02 17:58:47 +00:00
|
|
|
|
2010-09-17 15:54:40 +00:00
|
|
|
specific_attr_iterator& operator++() {
|
|
|
|
++Current;
|
|
|
|
return *this;
|
2009-06-02 17:58:47 +00:00
|
|
|
}
|
2010-09-17 15:54:40 +00:00
|
|
|
specific_attr_iterator operator++(int) {
|
|
|
|
specific_attr_iterator Tmp(*this);
|
|
|
|
++(*this);
|
|
|
|
return Tmp;
|
2009-06-02 17:58:47 +00:00
|
|
|
}
|
|
|
|
|
2010-09-17 15:54:40 +00:00
|
|
|
friend bool operator==(specific_attr_iterator Left,
|
|
|
|
specific_attr_iterator Right) {
|
|
|
|
if (Left.Current < Right.Current)
|
|
|
|
Left.AdvanceToNext(Right.Current);
|
|
|
|
else
|
|
|
|
Right.AdvanceToNext(Left.Current);
|
|
|
|
return Left.Current == Right.Current;
|
2009-10-14 18:03:49 +00:00
|
|
|
}
|
2010-09-17 15:54:40 +00:00
|
|
|
friend bool operator!=(specific_attr_iterator Left,
|
|
|
|
specific_attr_iterator Right) {
|
|
|
|
return !(Left == Right);
|
2009-06-27 10:45:02 +00:00
|
|
|
}
|
|
|
|
};
|
2009-10-14 18:03:49 +00:00
|
|
|
|
2012-08-15 20:02:54 +00:00
|
|
|
template <typename SpecificAttr, typename Container>
|
|
|
|
inline specific_attr_iterator<SpecificAttr, Container>
|
|
|
|
specific_attr_begin(const Container& container) {
|
|
|
|
return specific_attr_iterator<SpecificAttr, Container>(container.begin());
|
2010-09-17 15:54:40 +00:00
|
|
|
}
|
2012-08-15 20:02:54 +00:00
|
|
|
template <typename SpecificAttr, typename Container>
|
|
|
|
inline specific_attr_iterator<SpecificAttr, Container>
|
|
|
|
specific_attr_end(const Container& container) {
|
|
|
|
return specific_attr_iterator<SpecificAttr, Container>(container.end());
|
2010-09-17 15:54:40 +00:00
|
|
|
}
|
2010-01-15 15:39:40 +00:00
|
|
|
|
2012-08-15 20:02:54 +00:00
|
|
|
template <typename SpecificAttr, typename Container>
|
|
|
|
inline bool hasSpecificAttr(const Container& container) {
|
|
|
|
return specific_attr_begin<SpecificAttr>(container) !=
|
|
|
|
specific_attr_end<SpecificAttr>(container);
|
2010-09-17 15:54:40 +00:00
|
|
|
}
|
2012-08-15 20:02:54 +00:00
|
|
|
template <typename SpecificAttr, typename Container>
|
|
|
|
inline SpecificAttr *getSpecificAttr(const Container& container) {
|
|
|
|
specific_attr_iterator<SpecificAttr, Container> i =
|
|
|
|
specific_attr_begin<SpecificAttr>(container);
|
|
|
|
if (i != specific_attr_end<SpecificAttr>(container))
|
2010-09-17 15:54:40 +00:00
|
|
|
return *i;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
2010-02-16 09:31:36 +00:00
|
|
|
|
2010-09-17 15:54:40 +00:00
|
|
|
/// getMaxAlignment - Returns the highest alignment value found among
|
|
|
|
/// AlignedAttrs in an AttrVec, or 0 if there are none.
|
|
|
|
inline unsigned getMaxAttrAlignment(const AttrVec& V, ASTContext &Ctx) {
|
|
|
|
unsigned Align = 0;
|
|
|
|
specific_attr_iterator<AlignedAttr> i(V.begin()), e(V.end());
|
|
|
|
for(; i != e; ++i)
|
|
|
|
Align = std::max(Align, i->getAlignment(Ctx));
|
|
|
|
return Align;
|
|
|
|
}
|
2009-10-14 18:03:49 +00:00
|
|
|
|
2009-06-02 17:58:47 +00:00
|
|
|
} // end namespace clang
|
|
|
|
|
|
|
|
#endif
|