1 //===--- Attr.h - Classes for representing attributes ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the Attr interface and subclasses.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_ATTR_H
15 #define LLVM_CLANG_AST_ATTR_H
17 #include "clang/AST/AttrIterator.h"
18 #include "clang/AST/Decl.h"
19 #include "clang/AST/Expr.h"
20 #include "clang/AST/Type.h"
21 #include "clang/Basic/AttrKinds.h"
22 #include "clang/Basic/LLVM.h"
23 #include "clang/Basic/Sanitizers.h"
24 #include "clang/Basic/SourceLocation.h"
25 #include "clang/Basic/VersionTuple.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/ADT/StringRef.h"
28 #include "llvm/ADT/StringSwitch.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Support/raw_ostream.h"
37 class ObjCInterfaceDecl;
43 /// Attr - This represents one attribute.
47 unsigned AttrKind : 16;
50 /// An index into the spelling list of an
51 /// attribute defined in Attr.td file.
52 unsigned SpellingListIndex : 4;
54 bool IsPackExpansion : 1;
56 bool IsLateParsed : 1;
57 bool DuplicatesAllowed : 1;
59 void *operator new(size_t bytes) LLVM_NOEXCEPT {
60 llvm_unreachable("Attrs cannot be allocated with regular 'new'.");
62 void operator delete(void *data) LLVM_NOEXCEPT {
63 llvm_unreachable("Attrs cannot be released with regular 'delete'.");
67 // Forward so that the regular new and delete do not hide global ones.
68 void *operator new(size_t Bytes, ASTContext &C,
69 size_t Alignment = 8) LLVM_NOEXCEPT {
70 return ::operator new(Bytes, C, Alignment);
72 void operator delete(void *Ptr, ASTContext &C,
73 size_t Alignment) LLVM_NOEXCEPT {
74 return ::operator delete(Ptr, C, Alignment);
78 Attr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex,
79 bool IsLateParsed, bool DuplicatesAllowed)
80 : Range(R), AttrKind(AK), SpellingListIndex(SpellingListIndex),
81 Inherited(false), IsPackExpansion(false), Implicit(false),
82 IsLateParsed(IsLateParsed), DuplicatesAllowed(DuplicatesAllowed) {}
86 attr::Kind getKind() const {
87 return static_cast<attr::Kind>(AttrKind);
90 unsigned getSpellingListIndex() const { return SpellingListIndex; }
91 const char *getSpelling() const;
93 SourceLocation getLocation() const { return Range.getBegin(); }
94 SourceRange getRange() const { return Range; }
95 void setRange(SourceRange R) { Range = R; }
97 bool isInherited() const { return Inherited; }
99 /// \brief Returns true if the attribute has been implicitly created instead
100 /// of explicitly written by the user.
101 bool isImplicit() const { return Implicit; }
102 void setImplicit(bool I) { Implicit = I; }
104 void setPackExpansion(bool PE) { IsPackExpansion = PE; }
105 bool isPackExpansion() const { return IsPackExpansion; }
107 // Clone this attribute.
108 Attr *clone(ASTContext &C) const;
110 bool isLateParsed() const { return IsLateParsed; }
112 // Pretty print this attribute.
113 void printPretty(raw_ostream &OS, const PrintingPolicy &Policy) const;
115 /// \brief By default, attributes cannot be duplicated when being merged;
116 /// however, an attribute can override this. Returns true if the attribute
117 /// can be duplicated when merging.
118 bool duplicatesAllowed() const { return DuplicatesAllowed; }
121 class InheritableAttr : public Attr {
123 InheritableAttr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex,
124 bool IsLateParsed, bool DuplicatesAllowed)
125 : Attr(AK, R, SpellingListIndex, IsLateParsed, DuplicatesAllowed) {}
128 void setInherited(bool I) { Inherited = I; }
130 // Implement isa/cast/dyncast/etc.
131 static bool classof(const Attr *A) {
132 return A->getKind() <= attr::LAST_INHERITABLE;
136 class InheritableParamAttr : public InheritableAttr {
138 InheritableParamAttr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex,
139 bool IsLateParsed, bool DuplicatesAllowed)
140 : InheritableAttr(AK, R, SpellingListIndex, IsLateParsed,
141 DuplicatesAllowed) {}
144 // Implement isa/cast/dyncast/etc.
145 static bool classof(const Attr *A) {
146 // Relies on relative order of enum emission with respect to MS inheritance
148 return A->getKind() <= attr::LAST_INHERITABLE_PARAM;
152 #include "clang/AST/Attrs.inc"
154 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
156 DB.AddTaggedVal(reinterpret_cast<intptr_t>(At),
157 DiagnosticsEngine::ak_attr);
161 inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
163 PD.AddTaggedVal(reinterpret_cast<intptr_t>(At),
164 DiagnosticsEngine::ak_attr);
167 } // end namespace clang