clang 18.0.0git
Attr.h
Go to the documentation of this file.
1//===--- Attr.h - Classes for representing attributes ----------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the Attr interface and subclasses.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_AST_ATTR_H
14#define LLVM_CLANG_AST_ATTR_H
15
16#include "clang/AST/ASTFwd.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/Type.h"
23#include "clang/Basic/LLVM.h"
27#include "llvm/Support/ErrorHandling.h"
28#include "llvm/Support/VersionTuple.h"
29#include "llvm/Support/raw_ostream.h"
30#include <algorithm>
31#include <cassert>
32
33namespace clang {
34class ASTContext;
35class AttributeCommonInfo;
36class FunctionDecl;
37class OMPTraitInfo;
38
39/// Attr - This represents one attribute.
40class Attr : public AttributeCommonInfo {
41private:
42 unsigned AttrKind : 16;
43
44protected:
45 /// An index into the spelling list of an
46 /// attribute defined in Attr.td file.
47 unsigned Inherited : 1;
48 unsigned IsPackExpansion : 1;
49 unsigned Implicit : 1;
50 // FIXME: These are properties of the attribute kind, not state for this
51 // instance of the attribute.
52 unsigned IsLateParsed : 1;
54
55 void *operator new(size_t bytes) noexcept {
56 llvm_unreachable("Attrs cannot be allocated with regular 'new'.");
57 }
58 void operator delete(void *data) noexcept {
59 llvm_unreachable("Attrs cannot be released with regular 'delete'.");
60 }
61
62public:
63 // Forward so that the regular new and delete do not hide global ones.
64 void *operator new(size_t Bytes, ASTContext &C,
65 size_t Alignment = 8) noexcept {
66 return ::operator new(Bytes, C, Alignment);
67 }
68 void operator delete(void *Ptr, ASTContext &C, size_t Alignment) noexcept {
69 return ::operator delete(Ptr, C, Alignment);
70 }
71
72protected:
73 Attr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
74 attr::Kind AK, bool IsLateParsed)
75 : AttributeCommonInfo(CommonInfo), AttrKind(AK), Inherited(false),
78
79public:
80 attr::Kind getKind() const { return static_cast<attr::Kind>(AttrKind); }
81
82 unsigned getSpellingListIndex() const {
84 }
85 const char *getSpelling() const;
86
88
89 bool isInherited() const { return Inherited; }
90
91 /// Returns true if the attribute has been implicitly created instead
92 /// of explicitly written by the user.
93 bool isImplicit() const { return Implicit; }
94 void setImplicit(bool I) { Implicit = I; }
95
96 void setPackExpansion(bool PE) { IsPackExpansion = PE; }
97 bool isPackExpansion() const { return IsPackExpansion; }
98
99 // Clone this attribute.
101
102 bool isLateParsed() const { return IsLateParsed; }
103
104 // Pretty print this attribute.
105 void printPretty(raw_ostream &OS, const PrintingPolicy &Policy) const;
106
107 static StringRef getDocumentation(attr::Kind);
108};
109
110class TypeAttr : public Attr {
111protected:
112 TypeAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
113 attr::Kind AK, bool IsLateParsed)
114 : Attr(Context, CommonInfo, AK, IsLateParsed) {}
115
116public:
117 static bool classof(const Attr *A) {
118 return A->getKind() >= attr::FirstTypeAttr &&
119 A->getKind() <= attr::LastTypeAttr;
120 }
121};
122
123class StmtAttr : public Attr {
124protected:
125 StmtAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
126 attr::Kind AK, bool IsLateParsed)
127 : Attr(Context, CommonInfo, AK, IsLateParsed) {}
128
129public:
130 static bool classof(const Attr *A) {
131 return A->getKind() >= attr::FirstStmtAttr &&
132 A->getKind() <= attr::LastStmtAttr;
133 }
134};
135
136class InheritableAttr : public Attr {
137protected:
138 InheritableAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
139 attr::Kind AK, bool IsLateParsed,
141 : Attr(Context, CommonInfo, AK, IsLateParsed) {
142 this->InheritEvenIfAlreadyPresent = InheritEvenIfAlreadyPresent;
143 }
144
145public:
146 void setInherited(bool I) { Inherited = I; }
147
148 /// Should this attribute be inherited from a prior declaration even if it's
149 /// explicitly provided in the current declaration?
152 }
153
154 // Implement isa/cast/dyncast/etc.
155 static bool classof(const Attr *A) {
156 return A->getKind() >= attr::FirstInheritableAttr &&
157 A->getKind() <= attr::LastInheritableAttr;
158 }
159};
160
162protected:
163 DeclOrStmtAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
164 attr::Kind AK, bool IsLateParsed,
166 : InheritableAttr(Context, CommonInfo, AK, IsLateParsed,
168
169public:
170 static bool classof(const Attr *A) {
171 return A->getKind() >= attr::FirstDeclOrStmtAttr &&
172 A->getKind() <= attr::LastDeclOrStmtAttr;
173 }
174};
175
177protected:
179 const AttributeCommonInfo &CommonInfo, attr::Kind AK,
181 : InheritableAttr(Context, CommonInfo, AK, IsLateParsed,
183
184public:
185 // Implement isa/cast/dyncast/etc.
186 static bool classof(const Attr *A) {
187 return A->getKind() >= attr::FirstInheritableParamAttr &&
188 A->getKind() <= attr::LastInheritableParamAttr;
189 }
190};
191
193protected:
195 attr::Kind AK, bool IsLateParsed,
197 : InheritableAttr(Context, CommonInfo, AK, IsLateParsed,
199
200public:
201 // Implement isa/cast/dyncast/etc.
202 static bool classof(const Attr *A) {
203 return A->getKind() >= attr::FirstHLSLAnnotationAttr &&
204 A->getKind() <= attr::LastHLSLAnnotationAttr;
205 }
206};
207
208/// A parameter attribute which changes the argument-passing ABI rule
209/// for the parameter.
211protected:
213 attr::Kind AK, bool IsLateParsed,
215 : InheritableParamAttr(Context, CommonInfo, AK, IsLateParsed,
217
218public:
220 switch (getKind()) {
221 case attr::SwiftContext:
223 case attr::SwiftAsyncContext:
225 case attr::SwiftErrorResult:
227 case attr::SwiftIndirectResult:
229 default:
230 llvm_unreachable("bad parameter ABI attribute kind");
231 }
232 }
233
234 static bool classof(const Attr *A) {
235 return A->getKind() >= attr::FirstParameterABIAttr &&
236 A->getKind() <= attr::LastParameterABIAttr;
237 }
238};
239
240/// A single parameter index whose accessors require each use to make explicit
241/// the parameter index encoding needed.
242class ParamIdx {
243 // Idx is exposed only via accessors that specify specific encodings.
244 unsigned Idx : 30;
245 unsigned HasThis : 1;
246 unsigned IsValid : 1;
247
248 void assertComparable(const ParamIdx &I) const {
249 assert(isValid() && I.isValid() &&
250 "ParamIdx must be valid to be compared");
251 // It's possible to compare indices from separate functions, but so far
252 // it's not proven useful. Moreover, it might be confusing because a
253 // comparison on the results of getASTIndex might be inconsistent with a
254 // comparison on the ParamIdx objects themselves.
255 assert(HasThis == I.HasThis &&
256 "ParamIdx must be for the same function to be compared");
257 }
258
259public:
260 /// Construct an invalid parameter index (\c isValid returns false and
261 /// accessors fail an assert).
262 ParamIdx() : Idx(0), HasThis(false), IsValid(false) {}
263
264 /// \param Idx is the parameter index as it is normally specified in
265 /// attributes in the source: one-origin including any C++ implicit this
266 /// parameter.
267 ///
268 /// \param D is the declaration containing the parameters. It is used to
269 /// determine if there is a C++ implicit this parameter.
270 ParamIdx(unsigned Idx, const Decl *D)
271 : Idx(Idx), HasThis(false), IsValid(true) {
272 assert(Idx >= 1 && "Idx must be one-origin");
273 if (const auto *FD = dyn_cast<FunctionDecl>(D))
274 HasThis = FD->isCXXInstanceMember();
275 }
276
277 /// A type into which \c ParamIdx can be serialized.
278 ///
279 /// A static assertion that it's of the correct size follows the \c ParamIdx
280 /// class definition.
281 typedef uint32_t SerialType;
282
283 /// Produce a representation that can later be passed to \c deserialize to
284 /// construct an equivalent \c ParamIdx.
286 return *reinterpret_cast<const SerialType *>(this);
287 }
288
289 /// Construct from a result from \c serialize.
291 // Using this two-step static_cast via void * instead of reinterpret_cast
292 // silences a -Wstrict-aliasing false positive from GCC7 and earlier.
293 void *ParamIdxPtr = static_cast<void *>(&S);
294 ParamIdx P(*static_cast<ParamIdx *>(ParamIdxPtr));
295 assert((!P.IsValid || P.Idx >= 1) && "valid Idx must be one-origin");
296 return P;
297 }
298
299 /// Is this parameter index valid?
300 bool isValid() const { return IsValid; }
301
302 /// Get the parameter index as it would normally be encoded for attributes at
303 /// the source level of representation: one-origin including any C++ implicit
304 /// this parameter.
305 ///
306 /// This encoding thus makes sense for diagnostics, pretty printing, and
307 /// constructing new attributes from a source-like specification.
308 unsigned getSourceIndex() const {
309 assert(isValid() && "ParamIdx must be valid");
310 return Idx;
311 }
312
313 /// Get the parameter index as it would normally be encoded at the AST level
314 /// of representation: zero-origin not including any C++ implicit this
315 /// parameter.
316 ///
317 /// This is the encoding primarily used in Sema. However, in diagnostics,
318 /// Sema uses \c getSourceIndex instead.
319 unsigned getASTIndex() const {
320 assert(isValid() && "ParamIdx must be valid");
321 assert(Idx >= 1 + HasThis &&
322 "stored index must be base-1 and not specify C++ implicit this");
323 return Idx - 1 - HasThis;
324 }
325
326 /// Get the parameter index as it would normally be encoded at the LLVM level
327 /// of representation: zero-origin including any C++ implicit this parameter.
328 ///
329 /// This is the encoding primarily used in CodeGen.
330 unsigned getLLVMIndex() const {
331 assert(isValid() && "ParamIdx must be valid");
332 assert(Idx >= 1 && "stored index must be base-1");
333 return Idx - 1;
334 }
335
336 bool operator==(const ParamIdx &I) const {
337 assertComparable(I);
338 return Idx == I.Idx;
339 }
340 bool operator!=(const ParamIdx &I) const {
341 assertComparable(I);
342 return Idx != I.Idx;
343 }
344 bool operator<(const ParamIdx &I) const {
345 assertComparable(I);
346 return Idx < I.Idx;
347 }
348 bool operator>(const ParamIdx &I) const {
349 assertComparable(I);
350 return Idx > I.Idx;
351 }
352 bool operator<=(const ParamIdx &I) const {
353 assertComparable(I);
354 return Idx <= I.Idx;
355 }
356 bool operator>=(const ParamIdx &I) const {
357 assertComparable(I);
358 return Idx >= I.Idx;
359 }
360};
361
362static_assert(sizeof(ParamIdx) == sizeof(ParamIdx::SerialType),
363 "ParamIdx does not fit its serialization type");
364
365#include "clang/AST/Attrs.inc"
366
368 const Attr *At) {
369 DB.AddTaggedVal(reinterpret_cast<uint64_t>(At), DiagnosticsEngine::ak_attr);
370 return DB;
371}
372} // end namespace clang
373
374#endif
Forward declaration of all AST node types.
StringRef P
static StringRef bytes(const std::vector< T, Allocator > &v)
Definition: ASTWriter.cpp:123
Defines the clang::attr::Kind enum.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Defines the clang::LangOptions interface.
Defines some OpenMP-specific enums and functions.
Defines the clang::SanitizerKind enum.
Defines the clang::SourceLocation class and associated facilities.
C Language Family Type Representation.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:182
Attr - This represents one attribute.
Definition: Attr.h:40
unsigned getSpellingListIndex() const
Definition: Attr.h:82
unsigned IsPackExpansion
Definition: Attr.h:48
Attr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed)
Definition: Attr.h:73
attr::Kind getKind() const
Definition: Attr.h:80
unsigned Implicit
Definition: Attr.h:49
void printPretty(raw_ostream &OS, const PrintingPolicy &Policy) const
static StringRef getDocumentation(attr::Kind)
unsigned InheritEvenIfAlreadyPresent
Definition: Attr.h:53
unsigned Inherited
An index into the spelling list of an attribute defined in Attr.td file.
Definition: Attr.h:47
const char * getSpelling() const
void setPackExpansion(bool PE)
Definition: Attr.h:96
bool isInherited() const
Definition: Attr.h:89
bool isImplicit() const
Returns true if the attribute has been implicitly created instead of explicitly written by the user.
Definition: Attr.h:93
unsigned IsLateParsed
Definition: Attr.h:52
Attr * clone(ASTContext &C) const
void setImplicit(bool I)
Definition: Attr.h:94
SourceLocation getLocation() const
Definition: Attr.h:87
bool isLateParsed() const
Definition: Attr.h:102
bool isPackExpansion() const
Definition: Attr.h:97
unsigned getAttributeSpellingListIndex() const
static bool classof(const Attr *A)
Definition: Attr.h:170
DeclOrStmtAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
Definition: Attr.h:163
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:83
HLSLAnnotationAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
Definition: Attr.h:194
static bool classof(const Attr *A)
Definition: Attr.h:202
void setInherited(bool I)
Definition: Attr.h:146
static bool classof(const Attr *A)
Definition: Attr.h:155
InheritableAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
Definition: Attr.h:138
bool shouldInheritEvenIfAlreadyPresent() const
Should this attribute be inherited from a prior declaration even if it's explicitly provided in the c...
Definition: Attr.h:150
static bool classof(const Attr *A)
Definition: Attr.h:186
InheritableParamAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
Definition: Attr.h:178
A single parameter index whose accessors require each use to make explicit the parameter index encodi...
Definition: Attr.h:242
bool operator==(const ParamIdx &I) const
Definition: Attr.h:336
bool operator<=(const ParamIdx &I) const
Definition: Attr.h:352
ParamIdx()
Construct an invalid parameter index (isValid returns false and accessors fail an assert).
Definition: Attr.h:262
bool operator<(const ParamIdx &I) const
Definition: Attr.h:344
bool isValid() const
Is this parameter index valid?
Definition: Attr.h:300
static ParamIdx deserialize(SerialType S)
Construct from a result from serialize.
Definition: Attr.h:290
unsigned getSourceIndex() const
Get the parameter index as it would normally be encoded for attributes at the source level of represe...
Definition: Attr.h:308
bool operator>=(const ParamIdx &I) const
Definition: Attr.h:356
unsigned getLLVMIndex() const
Get the parameter index as it would normally be encoded at the LLVM level of representation: zero-ori...
Definition: Attr.h:330
ParamIdx(unsigned Idx, const Decl *D)
Definition: Attr.h:270
unsigned getASTIndex() const
Get the parameter index as it would normally be encoded at the AST level of representation: zero-orig...
Definition: Attr.h:319
bool operator>(const ParamIdx &I) const
Definition: Attr.h:348
uint32_t SerialType
A type into which ParamIdx can be serialized.
Definition: Attr.h:281
SerialType serialize() const
Produce a representation that can later be passed to deserialize to construct an equivalent ParamIdx.
Definition: Attr.h:285
bool operator!=(const ParamIdx &I) const
Definition: Attr.h:340
A parameter attribute which changes the argument-passing ABI rule for the parameter.
Definition: Attr.h:210
ParameterABI getABI() const
Definition: Attr.h:219
ParameterABIAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
Definition: Attr.h:212
static bool classof(const Attr *A)
Definition: Attr.h:234
Encodes a location in the source.
SourceLocation getBegin() const
static bool classof(const Attr *A)
Definition: Attr.h:130
StmtAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed)
Definition: Attr.h:125
The streaming interface shared between DiagnosticBuilder and PartialDiagnostic.
Definition: Diagnostic.h:1110
void AddTaggedVal(uint64_t V, DiagnosticsEngine::ArgumentKind Kind) const
Definition: Diagnostic.h:1184
TypeAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed)
Definition: Attr.h:112
static bool classof(const Attr *A)
Definition: Attr.h:117
const StreamingDiagnostic & operator<<(const StreamingDiagnostic &DB, const ASTContext::SectionInfo &Section)
Insertion operator for diagnostics.
@ C
Languages that the frontend can parse and compile.
ParameterABI
Kinds of parameter ABI.
Definition: Specifiers.h:353
@ SwiftAsyncContext
This parameter (which must have pointer type) uses the special Swift asynchronous context-pointer ABI...
@ SwiftErrorResult
This parameter (which must have pointer-to-pointer type) uses the special Swift error-result ABI trea...
@ SwiftIndirectResult
This parameter (which must have pointer type) is a Swift indirect result parameter.
@ SwiftContext
This parameter (which must have pointer type) uses the special Swift context-pointer ABI treatment.
#define true
Definition: stdbool.h:21
#define false
Definition: stdbool.h:22
Describes how types, statements, expressions, and declarations should be printed.
Definition: PrettyPrinter.h:57