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