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