clang 20.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"
22#include "clang/Basic/LLVM.h"
28#include "llvm/Frontend/HLSL/HLSLResource.h"
29#include "llvm/Support/CodeGen.h"
30#include "llvm/Support/ErrorHandling.h"
31#include "llvm/Support/VersionTuple.h"
32#include "llvm/Support/raw_ostream.h"
33#include <algorithm>
34#include <cassert>
35
36namespace clang {
37class ASTContext;
38class AttributeCommonInfo;
39class FunctionDecl;
40class OMPTraitInfo;
41
42/// Attr - This represents one attribute.
43class Attr : public AttributeCommonInfo {
44private:
45 LLVM_PREFERRED_TYPE(attr::Kind)
46 unsigned AttrKind : 16;
47
48protected:
49 /// An index into the spelling list of an
50 /// attribute defined in Attr.td file.
51 LLVM_PREFERRED_TYPE(bool)
53 LLVM_PREFERRED_TYPE(bool)
54 unsigned IsPackExpansion : 1;
55 LLVM_PREFERRED_TYPE(bool)
56 unsigned Implicit : 1;
57 // FIXME: These are properties of the attribute kind, not state for this
58 // instance of the attribute.
59 LLVM_PREFERRED_TYPE(bool)
60 unsigned IsLateParsed : 1;
61 LLVM_PREFERRED_TYPE(bool)
63
64 void *operator new(size_t bytes) noexcept {
65 llvm_unreachable("Attrs cannot be allocated with regular 'new'.");
66 }
67 void operator delete(void *data) noexcept {
68 llvm_unreachable("Attrs cannot be released with regular 'delete'.");
69 }
70
71public:
72 // Forward so that the regular new and delete do not hide global ones.
73 void *operator new(size_t Bytes, ASTContext &C,
74 size_t Alignment = 8) noexcept {
75 return ::operator new(Bytes, C, Alignment);
76 }
77 void operator delete(void *Ptr, ASTContext &C, size_t Alignment) noexcept {
78 return ::operator delete(Ptr, C, Alignment);
79 }
80
81protected:
82 Attr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
83 attr::Kind AK, bool IsLateParsed)
84 : AttributeCommonInfo(CommonInfo), AttrKind(AK), Inherited(false),
87
88public:
89 attr::Kind getKind() const { return static_cast<attr::Kind>(AttrKind); }
90
91 unsigned getSpellingListIndex() const {
93 }
94 const char *getSpelling() const;
95
97
98 bool isInherited() const { return Inherited; }
99
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; }
104
105 void setPackExpansion(bool PE) { IsPackExpansion = PE; }
106 bool isPackExpansion() const { return IsPackExpansion; }
107
108 // Clone this attribute.
110
111 bool isLateParsed() const { return IsLateParsed; }
112
113 // Pretty print this attribute.
114 void printPretty(raw_ostream &OS, const PrintingPolicy &Policy) const;
115
116 static StringRef getDocumentation(attr::Kind);
117};
118
119class TypeAttr : public Attr {
120protected:
121 TypeAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
122 attr::Kind AK, bool IsLateParsed)
123 : Attr(Context, CommonInfo, AK, IsLateParsed) {}
124
125public:
126 static bool classof(const Attr *A) {
127 return A->getKind() >= attr::FirstTypeAttr &&
128 A->getKind() <= attr::LastTypeAttr;
129 }
130};
131
132class StmtAttr : public Attr {
133protected:
134 StmtAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
135 attr::Kind AK, bool IsLateParsed)
136 : Attr(Context, CommonInfo, AK, IsLateParsed) {}
137
138public:
139 static bool classof(const Attr *A) {
140 return A->getKind() >= attr::FirstStmtAttr &&
141 A->getKind() <= attr::LastStmtAttr;
142 }
143};
144
145class InheritableAttr : public Attr {
146protected:
147 InheritableAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
148 attr::Kind AK, bool IsLateParsed,
150 : Attr(Context, CommonInfo, AK, IsLateParsed) {
151 this->InheritEvenIfAlreadyPresent = InheritEvenIfAlreadyPresent;
152 }
153
154public:
155 void setInherited(bool I) { Inherited = I; }
156
157 /// Should this attribute be inherited from a prior declaration even if it's
158 /// explicitly provided in the current declaration?
161 }
162
163 // Implement isa/cast/dyncast/etc.
164 static bool classof(const Attr *A) {
165 return A->getKind() >= attr::FirstInheritableAttr &&
166 A->getKind() <= attr::LastInheritableAttr;
167 }
168};
169
171protected:
172 DeclOrStmtAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
173 attr::Kind AK, bool IsLateParsed,
175 : InheritableAttr(Context, CommonInfo, AK, IsLateParsed,
177
178public:
179 static bool classof(const Attr *A) {
180 return A->getKind() >= attr::FirstDeclOrStmtAttr &&
181 A->getKind() <= attr::LastDeclOrStmtAttr;
182 }
183};
184
186protected:
188 const AttributeCommonInfo &CommonInfo, attr::Kind AK,
190 : InheritableAttr(Context, CommonInfo, AK, IsLateParsed,
192
193public:
194 // Implement isa/cast/dyncast/etc.
195 static bool classof(const Attr *A) {
196 return A->getKind() >= attr::FirstInheritableParamAttr &&
197 A->getKind() <= attr::LastInheritableParamAttr;
198 }
199};
200
202protected:
204 const AttributeCommonInfo &CommonInfo,
205 attr::Kind AK, bool IsLateParsed,
207 : InheritableParamAttr(Context, CommonInfo, AK, IsLateParsed,
209
210public:
211 // Implement isa/cast/dyncast/etc.
212 static bool classof(const Attr *A) {
213 return A->getKind() >= attr::FirstInheritableParamOrStmtAttr &&
214 A->getKind() <= attr::LastInheritableParamOrStmtAttr;
215 }
216};
217
219protected:
221 attr::Kind AK, bool IsLateParsed,
223 : InheritableAttr(Context, CommonInfo, AK, IsLateParsed,
225
226public:
227 // Implement isa/cast/dyncast/etc.
228 static bool classof(const Attr *A) {
229 return A->getKind() >= attr::FirstHLSLAnnotationAttr &&
230 A->getKind() <= attr::LastHLSLAnnotationAttr;
231 }
232};
233
234/// A parameter attribute which changes the argument-passing ABI rule
235/// for the parameter.
237protected:
239 attr::Kind AK, bool IsLateParsed,
241 : InheritableParamAttr(Context, CommonInfo, AK, IsLateParsed,
243
244public:
245 ParameterABI getABI() const;
246
247 static bool classof(const Attr *A) {
248 return A->getKind() >= attr::FirstParameterABIAttr &&
249 A->getKind() <= attr::LastParameterABIAttr;
250 }
251};
252
253/// A single parameter index whose accessors require each use to make explicit
254/// the parameter index encoding needed.
255class ParamIdx {
256 // Idx is exposed only via accessors that specify specific encodings.
257 unsigned Idx : 30;
258 LLVM_PREFERRED_TYPE(bool)
259 unsigned HasThis : 1;
260 LLVM_PREFERRED_TYPE(bool)
261 unsigned IsValid : 1;
262
263 void assertComparable(const ParamIdx &I) const {
264 assert(isValid() && I.isValid() &&
265 "ParamIdx must be valid to be compared");
266 // It's possible to compare indices from separate functions, but so far
267 // it's not proven useful. Moreover, it might be confusing because a
268 // comparison on the results of getASTIndex might be inconsistent with a
269 // comparison on the ParamIdx objects themselves.
270 assert(HasThis == I.HasThis &&
271 "ParamIdx must be for the same function to be compared");
272 }
273
274public:
275 /// Construct an invalid parameter index (\c isValid returns false and
276 /// accessors fail an assert).
277 ParamIdx() : Idx(0), HasThis(false), IsValid(false) {}
278
279 /// \param Idx is the parameter index as it is normally specified in
280 /// attributes in the source: one-origin including any C++ implicit this
281 /// parameter.
282 ///
283 /// \param D is the declaration containing the parameters. It is used to
284 /// determine if there is a C++ implicit this parameter.
285 ParamIdx(unsigned Idx, const Decl *D)
286 : Idx(Idx), HasThis(false), IsValid(true) {
287 assert(Idx >= 1 && "Idx must be one-origin");
288 if (const auto *FD = dyn_cast<FunctionDecl>(D))
289 HasThis = FD->isCXXInstanceMember();
290 }
291
292 /// A type into which \c ParamIdx can be serialized.
293 ///
294 /// A static assertion that it's of the correct size follows the \c ParamIdx
295 /// class definition.
296 typedef uint32_t SerialType;
297
298 /// Produce a representation that can later be passed to \c deserialize to
299 /// construct an equivalent \c ParamIdx.
301 return *reinterpret_cast<const SerialType *>(this);
302 }
303
304 /// Construct from a result from \c serialize.
306 // Using this two-step static_cast via void * instead of reinterpret_cast
307 // silences a -Wstrict-aliasing false positive from GCC7 and earlier.
308 void *ParamIdxPtr = static_cast<void *>(&S);
309 ParamIdx P(*static_cast<ParamIdx *>(ParamIdxPtr));
310 assert((!P.IsValid || P.Idx >= 1) && "valid Idx must be one-origin");
311 return P;
312 }
313
314 /// Is this parameter index valid?
315 bool isValid() const { return IsValid; }
316
317 /// Get the parameter index as it would normally be encoded for attributes at
318 /// the source level of representation: one-origin including any C++ implicit
319 /// this parameter.
320 ///
321 /// This encoding thus makes sense for diagnostics, pretty printing, and
322 /// constructing new attributes from a source-like specification.
323 unsigned getSourceIndex() const {
324 assert(isValid() && "ParamIdx must be valid");
325 return Idx;
326 }
327
328 /// Get the parameter index as it would normally be encoded at the AST level
329 /// of representation: zero-origin not including any C++ implicit this
330 /// parameter.
331 ///
332 /// This is the encoding primarily used in Sema. However, in diagnostics,
333 /// Sema uses \c getSourceIndex instead.
334 unsigned getASTIndex() const {
335 assert(isValid() && "ParamIdx must be valid");
336 assert(Idx >= 1 + HasThis &&
337 "stored index must be base-1 and not specify C++ implicit this");
338 return Idx - 1 - HasThis;
339 }
340
341 /// Get the parameter index as it would normally be encoded at the LLVM level
342 /// of representation: zero-origin including any C++ implicit this parameter.
343 ///
344 /// This is the encoding primarily used in CodeGen.
345 unsigned getLLVMIndex() const {
346 assert(isValid() && "ParamIdx must be valid");
347 assert(Idx >= 1 && "stored index must be base-1");
348 return Idx - 1;
349 }
350
351 bool operator==(const ParamIdx &I) const {
352 assertComparable(I);
353 return Idx == I.Idx;
354 }
355 bool operator!=(const ParamIdx &I) const {
356 assertComparable(I);
357 return Idx != I.Idx;
358 }
359 bool operator<(const ParamIdx &I) const {
360 assertComparable(I);
361 return Idx < I.Idx;
362 }
363 bool operator>(const ParamIdx &I) const {
364 assertComparable(I);
365 return Idx > I.Idx;
366 }
367 bool operator<=(const ParamIdx &I) const {
368 assertComparable(I);
369 return Idx <= I.Idx;
370 }
371 bool operator>=(const ParamIdx &I) const {
372 assertComparable(I);
373 return Idx >= I.Idx;
374 }
375};
376
377static_assert(sizeof(ParamIdx) == sizeof(ParamIdx::SerialType),
378 "ParamIdx does not fit its serialization type");
379
380#include "clang/AST/Attrs.inc" // IWYU pragma: export
381
383 const Attr *At) {
384 DB.AddTaggedVal(reinterpret_cast<uint64_t>(At), DiagnosticsEngine::ak_attr);
385 return DB;
386}
387
389 switch (getKind()) {
390 case attr::SwiftContext:
392 case attr::SwiftAsyncContext:
394 case attr::SwiftErrorResult:
396 case attr::SwiftIndirectResult:
398 case attr::HLSLParamModifier: {
399 const auto *A = cast<HLSLParamModifierAttr>(this);
400 if (A->isOut())
402 if (A->isInOut())
405 }
406 default:
407 llvm_unreachable("bad parameter ABI attribute kind");
408 }
409}
410} // end namespace clang
411
412#endif
Forward declaration of all AST node types.
StringRef P
static StringRef bytes(const std::vector< T, Allocator > &v)
Definition: ASTWriter.cpp:131
Defines the clang::attr::Kind enum.
const Decl * D
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:188
Attr - This represents one attribute.
Definition: Attr.h:43
unsigned getSpellingListIndex() const
Definition: Attr.h:91
unsigned IsPackExpansion
Definition: Attr.h:54
Attr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed)
Definition: Attr.h:82
attr::Kind getKind() const
Definition: Attr.h:89
unsigned Implicit
Definition: Attr.h:56
void printPretty(raw_ostream &OS, const PrintingPolicy &Policy) const
static StringRef getDocumentation(attr::Kind)
unsigned InheritEvenIfAlreadyPresent
Definition: Attr.h:62
unsigned Inherited
An index into the spelling list of an attribute defined in Attr.td file.
Definition: Attr.h:52
const char * getSpelling() const
void setPackExpansion(bool PE)
Definition: Attr.h:105
bool isInherited() const
Definition: Attr.h:98
bool isImplicit() const
Returns true if the attribute has been implicitly created instead of explicitly written by the user.
Definition: Attr.h:102
unsigned IsLateParsed
Definition: Attr.h:60
Attr * clone(ASTContext &C) const
void setImplicit(bool I)
Definition: Attr.h:103
SourceLocation getLocation() const
Definition: Attr.h:96
bool isLateParsed() const
Definition: Attr.h:111
bool isPackExpansion() const
Definition: Attr.h:106
unsigned getAttributeSpellingListIndex() const
static bool classof(const Attr *A)
Definition: Attr.h:179
DeclOrStmtAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
Definition: Attr.h:172
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
HLSLAnnotationAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
Definition: Attr.h:220
static bool classof(const Attr *A)
Definition: Attr.h:228
void setInherited(bool I)
Definition: Attr.h:155
static bool classof(const Attr *A)
Definition: Attr.h:164
InheritableAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
Definition: Attr.h:147
bool shouldInheritEvenIfAlreadyPresent() const
Should this attribute be inherited from a prior declaration even if it's explicitly provided in the c...
Definition: Attr.h:159
static bool classof(const Attr *A)
Definition: Attr.h:195
InheritableParamAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
Definition: Attr.h:187
InheritableParamOrStmtAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
Definition: Attr.h:203
static bool classof(const Attr *A)
Definition: Attr.h:212
A single parameter index whose accessors require each use to make explicit the parameter index encodi...
Definition: Attr.h:255
bool operator==(const ParamIdx &I) const
Definition: Attr.h:351
bool operator<=(const ParamIdx &I) const
Definition: Attr.h:367
ParamIdx()
Construct an invalid parameter index (isValid returns false and accessors fail an assert).
Definition: Attr.h:277
bool operator<(const ParamIdx &I) const
Definition: Attr.h:359
bool isValid() const
Is this parameter index valid?
Definition: Attr.h:315
static ParamIdx deserialize(SerialType S)
Construct from a result from serialize.
Definition: Attr.h:305
unsigned getSourceIndex() const
Get the parameter index as it would normally be encoded for attributes at the source level of represe...
Definition: Attr.h:323
bool operator>=(const ParamIdx &I) const
Definition: Attr.h:371
unsigned getLLVMIndex() const
Get the parameter index as it would normally be encoded at the LLVM level of representation: zero-ori...
Definition: Attr.h:345
ParamIdx(unsigned Idx, const Decl *D)
Definition: Attr.h:285
unsigned getASTIndex() const
Get the parameter index as it would normally be encoded at the AST level of representation: zero-orig...
Definition: Attr.h:334
bool operator>(const ParamIdx &I) const
Definition: Attr.h:363
uint32_t SerialType
A type into which ParamIdx can be serialized.
Definition: Attr.h:296
SerialType serialize() const
Produce a representation that can later be passed to deserialize to construct an equivalent ParamIdx.
Definition: Attr.h:300
bool operator!=(const ParamIdx &I) const
Definition: Attr.h:355
A parameter attribute which changes the argument-passing ABI rule for the parameter.
Definition: Attr.h:236
ParameterABI getABI() const
Definition: Attr.h:388
ParameterABIAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
Definition: Attr.h:238
static bool classof(const Attr *A)
Definition: Attr.h:247
Encodes a location in the source.
SourceLocation getBegin() const
static bool classof(const Attr *A)
Definition: Attr.h:139
StmtAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed)
Definition: Attr.h:134
The streaming interface shared between DiagnosticBuilder and PartialDiagnostic.
Definition: Diagnostic.h:1102
void AddTaggedVal(uint64_t V, DiagnosticsEngine::ArgumentKind Kind) const
Definition: Diagnostic.h:1143
TypeAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed)
Definition: Attr.h:121
static bool classof(const Attr *A)
Definition: Attr.h:126
The JSON file list parser is used to communicate input to InstallAPI.
const StreamingDiagnostic & operator<<(const StreamingDiagnostic &DB, const ASTContext::SectionInfo &Section)
Insertion operator for diagnostics.
ParameterABI
Kinds of parameter ABI.
Definition: Specifiers.h:366
@ 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...
@ Ordinary
This parameter uses ordinary ABI rules for its type.
@ 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:25
#define false
Definition: stdbool.h:26
Describes how types, statements, expressions, and declarations should be printed.
Definition: PrettyPrinter.h:57