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/ASTContextAllocate.h" // For Attrs.inc
18 #include "clang/AST/AttrIterator.h"
19 #include "clang/AST/Decl.h"
20 #include "clang/AST/Expr.h"
21 #include "clang/AST/Type.h"
22 #include "clang/Basic/AttrKinds.h"
23 #include "clang/Basic/LLVM.h"
24 #include "clang/Basic/OpenMPKinds.h"
25 #include "clang/Basic/Sanitizers.h"
26 #include "clang/Basic/SourceLocation.h"
27 #include "llvm/ADT/StringSwitch.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Support/VersionTuple.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;
53 unsigned Inherited : 1;
54 unsigned IsPackExpansion : 1;
55 unsigned Implicit : 1;
56 // FIXME: These are properties of the attribute kind, not state for this
57 // instance of the attribute.
58 unsigned IsLateParsed : 1;
59 unsigned InheritEvenIfAlreadyPresent : 1;
61 void *operator new(size_t bytes) noexcept {
62 llvm_unreachable("Attrs cannot be allocated with regular 'new'.");
64 void operator delete(void *data) noexcept {
65 llvm_unreachable("Attrs cannot be released with regular 'delete'.");
69 // Forward so that the regular new and delete do not hide global ones.
70 void *operator new(size_t Bytes, ASTContext &C,
71 size_t Alignment = 8) noexcept {
72 return ::operator new(Bytes, C, Alignment);
74 void operator delete(void *Ptr, ASTContext &C, size_t Alignment) noexcept {
75 return ::operator delete(Ptr, C, Alignment);
79 Attr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex,
81 : Range(R), AttrKind(AK), SpellingListIndex(SpellingListIndex),
82 Inherited(false), IsPackExpansion(false), Implicit(false),
83 IsLateParsed(IsLateParsed), InheritEvenIfAlreadyPresent(false) {}
87 attr::Kind getKind() const {
88 return static_cast<attr::Kind>(AttrKind);
91 unsigned getSpellingListIndex() const { return SpellingListIndex; }
92 const char *getSpelling() const;
94 SourceLocation getLocation() const { return Range.getBegin(); }
95 SourceRange getRange() const { return Range; }
96 void setRange(SourceRange R) { Range = R; }
98 bool isInherited() const { return Inherited; }
100 /// Returns true if the attribute has been implicitly created instead
101 /// of explicitly written by the user.
102 bool isImplicit() const { return Implicit; }
103 void setImplicit(bool I) { Implicit = I; }
105 void setPackExpansion(bool PE) { IsPackExpansion = PE; }
106 bool isPackExpansion() const { return IsPackExpansion; }
108 // Clone this attribute.
109 Attr *clone(ASTContext &C) const;
111 bool isLateParsed() const { return IsLateParsed; }
113 // Pretty print this attribute.
114 void printPretty(raw_ostream &OS, const PrintingPolicy &Policy) const;
117 class TypeAttr : public Attr {
119 TypeAttr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex,
121 : Attr(AK, R, SpellingListIndex, IsLateParsed) {}
124 static bool classof(const Attr *A) {
125 return A->getKind() >= attr::FirstTypeAttr &&
126 A->getKind() <= attr::LastTypeAttr;
130 class StmtAttr : public Attr {
132 StmtAttr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex,
134 : Attr(AK, R, SpellingListIndex, IsLateParsed) {}
137 static bool classof(const Attr *A) {
138 return A->getKind() >= attr::FirstStmtAttr &&
139 A->getKind() <= attr::LastStmtAttr;
143 class InheritableAttr : public Attr {
145 InheritableAttr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex,
146 bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
147 : Attr(AK, R, SpellingListIndex, IsLateParsed) {
148 this->InheritEvenIfAlreadyPresent = InheritEvenIfAlreadyPresent;
152 void setInherited(bool I) { Inherited = I; }
154 /// Should this attribute be inherited from a prior declaration even if it's
155 /// explicitly provided in the current declaration?
156 bool shouldInheritEvenIfAlreadyPresent() const {
157 return InheritEvenIfAlreadyPresent;
160 // Implement isa/cast/dyncast/etc.
161 static bool classof(const Attr *A) {
162 return A->getKind() >= attr::FirstInheritableAttr &&
163 A->getKind() <= attr::LastInheritableAttr;
167 class InheritableParamAttr : public InheritableAttr {
169 InheritableParamAttr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex,
170 bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
171 : InheritableAttr(AK, R, SpellingListIndex, IsLateParsed,
172 InheritEvenIfAlreadyPresent) {}
175 // Implement isa/cast/dyncast/etc.
176 static bool classof(const Attr *A) {
177 return A->getKind() >= attr::FirstInheritableParamAttr &&
178 A->getKind() <= attr::LastInheritableParamAttr;
182 /// A parameter attribute which changes the argument-passing ABI rule
183 /// for the parameter.
184 class ParameterABIAttr : public InheritableParamAttr {
186 ParameterABIAttr(attr::Kind AK, SourceRange R,
187 unsigned SpellingListIndex, bool IsLateParsed,
188 bool InheritEvenIfAlreadyPresent)
189 : InheritableParamAttr(AK, R, SpellingListIndex, IsLateParsed,
190 InheritEvenIfAlreadyPresent) {}
193 ParameterABI getABI() const {
195 case attr::SwiftContext:
196 return ParameterABI::SwiftContext;
197 case attr::SwiftErrorResult:
198 return ParameterABI::SwiftErrorResult;
199 case attr::SwiftIndirectResult:
200 return ParameterABI::SwiftIndirectResult;
202 llvm_unreachable("bad parameter ABI attribute kind");
206 static bool classof(const Attr *A) {
207 return A->getKind() >= attr::FirstParameterABIAttr &&
208 A->getKind() <= attr::LastParameterABIAttr;
212 /// A single parameter index whose accessors require each use to make explicit
213 /// the parameter index encoding needed.
215 // Idx is exposed only via accessors that specify specific encodings.
217 unsigned HasThis : 1;
218 unsigned IsValid : 1;
220 void assertComparable(const ParamIdx &I) const {
221 assert(isValid() && I.isValid() &&
222 "ParamIdx must be valid to be compared");
223 // It's possible to compare indices from separate functions, but so far
224 // it's not proven useful. Moreover, it might be confusing because a
225 // comparison on the results of getASTIndex might be inconsistent with a
226 // comparison on the ParamIdx objects themselves.
227 assert(HasThis == I.HasThis &&
228 "ParamIdx must be for the same function to be compared");
232 /// Construct an invalid parameter index (\c isValid returns false and
233 /// accessors fail an assert).
234 ParamIdx() : Idx(0), HasThis(false), IsValid(false) {}
236 /// \param Idx is the parameter index as it is normally specified in
237 /// attributes in the source: one-origin including any C++ implicit this
240 /// \param D is the declaration containing the parameters. It is used to
241 /// determine if there is a C++ implicit this parameter.
242 ParamIdx(unsigned Idx, const Decl *D)
243 : Idx(Idx), HasThis(false), IsValid(true) {
244 assert(Idx >= 1 && "Idx must be one-origin");
245 if (const auto *FD = dyn_cast<FunctionDecl>(D))
246 HasThis = FD->isCXXInstanceMember();
249 /// A type into which \c ParamIdx can be serialized.
251 /// A static assertion that it's of the correct size follows the \c ParamIdx
252 /// class definition.
253 typedef uint32_t SerialType;
255 /// Produce a representation that can later be passed to \c deserialize to
256 /// construct an equivalent \c ParamIdx.
257 SerialType serialize() const {
258 return *reinterpret_cast<const SerialType *>(this);
261 /// Construct from a result from \c serialize.
262 static ParamIdx deserialize(SerialType S) {
263 ParamIdx P(*reinterpret_cast<ParamIdx *>(&S));
264 assert((!P.IsValid || P.Idx >= 1) && "valid Idx must be one-origin");
268 /// Is this parameter index valid?
269 bool isValid() const { return IsValid; }
271 /// Get the parameter index as it would normally be encoded for attributes at
272 /// the source level of representation: one-origin including any C++ implicit
275 /// This encoding thus makes sense for diagnostics, pretty printing, and
276 /// constructing new attributes from a source-like specification.
277 unsigned getSourceIndex() const {
278 assert(isValid() && "ParamIdx must be valid");
282 /// Get the parameter index as it would normally be encoded at the AST level
283 /// of representation: zero-origin not including any C++ implicit this
286 /// This is the encoding primarily used in Sema. However, in diagnostics,
287 /// Sema uses \c getSourceIndex instead.
288 unsigned getASTIndex() const {
289 assert(isValid() && "ParamIdx must be valid");
290 assert(Idx >= 1 + HasThis &&
291 "stored index must be base-1 and not specify C++ implicit this");
292 return Idx - 1 - HasThis;
295 /// Get the parameter index as it would normally be encoded at the LLVM level
296 /// of representation: zero-origin including any C++ implicit this parameter.
298 /// This is the encoding primarily used in CodeGen.
299 unsigned getLLVMIndex() const {
300 assert(isValid() && "ParamIdx must be valid");
301 assert(Idx >= 1 && "stored index must be base-1");
305 bool operator==(const ParamIdx &I) const {
309 bool operator!=(const ParamIdx &I) const {
313 bool operator<(const ParamIdx &I) const {
317 bool operator>(const ParamIdx &I) const {
321 bool operator<=(const ParamIdx &I) const {
325 bool operator>=(const ParamIdx &I) const {
331 static_assert(sizeof(ParamIdx) == sizeof(ParamIdx::SerialType),
332 "ParamIdx does not fit its serialization type");
334 #include "clang/AST/Attrs.inc"
336 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
338 DB.AddTaggedVal(reinterpret_cast<intptr_t>(At),
339 DiagnosticsEngine::ak_attr);
343 inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
345 PD.AddTaggedVal(reinterpret_cast<intptr_t>(At),
346 DiagnosticsEngine::ak_attr);
349 } // end namespace clang