clang 24.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;
42class OMPTraitInfo;
43class OpenACCClause;
45
46/// Attr - This represents one attribute.
47class Attr : public AttributeCommonInfo {
48private:
49 LLVM_PREFERRED_TYPE(attr::Kind)
50 unsigned AttrKind : 16;
51
52protected:
53 /// An index into the spelling list of an
54 /// attribute defined in Attr.td file.
55 LLVM_PREFERRED_TYPE(bool)
57 LLVM_PREFERRED_TYPE(bool)
58 unsigned IsPackExpansion : 1;
59 LLVM_PREFERRED_TYPE(bool)
60 unsigned Implicit : 1;
61 // FIXME: These are properties of the attribute kind, not state for this
62 // instance of the attribute.
63 LLVM_PREFERRED_TYPE(bool)
64 unsigned IsLateParsed : 1;
65 LLVM_PREFERRED_TYPE(bool)
67
68 void *operator new(size_t bytes) noexcept {
69 llvm_unreachable("Attrs cannot be allocated with regular 'new'.");
70 }
71 void operator delete(void *data) noexcept {
72 llvm_unreachable("Attrs cannot be released with regular 'delete'.");
73 }
74
75public:
76 // Forward so that the regular new and delete do not hide global ones.
77 void *operator new(size_t Bytes, ASTContext &C,
78 size_t Alignment = 8) noexcept {
79 return ::operator new(Bytes, C, Alignment);
80 }
81 void operator delete(void *Ptr, ASTContext &C, size_t Alignment) noexcept {
82 return ::operator delete(Ptr, C, Alignment);
83 }
84
85protected:
91
92public:
93 attr::Kind getKind() const { return static_cast<attr::Kind>(AttrKind); }
94
95 unsigned getSpellingListIndex() const {
97 }
98 const char *getSpelling() const;
99
101
102 bool isInherited() const { return Inherited; }
103
104 /// Returns true if the attribute has been implicitly created instead
105 /// of explicitly written by the user.
106 bool isImplicit() const { return Implicit; }
107 void setImplicit(bool I) { Implicit = I; }
108
109 void setPackExpansion(bool PE) { IsPackExpansion = PE; }
110 bool isPackExpansion() const { return IsPackExpansion; }
111
112 // Clone this attribute.
114
115 bool isLateParsed() const { return IsLateParsed; }
116
118 StructuralEquivalenceContext &Context) const;
119
120 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx) const;
121
122 // Pretty print this attribute.
123 void printPretty(raw_ostream &OS, const PrintingPolicy &Policy) const;
124
125 static StringRef getDocumentation(attr::Kind);
126};
127
128class TypeAttr : public Attr {
129protected:
130 TypeAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
131 attr::Kind AK, bool IsLateParsed)
132 : Attr(Context, CommonInfo, AK, IsLateParsed) {}
133
134public:
135 static bool classof(const Attr *A) {
136 return A->getKind() >= attr::FirstTypeAttr &&
137 A->getKind() <= attr::LastTypeAttr;
138 }
139};
140
141class StmtAttr : public Attr {
142protected:
143 StmtAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
144 attr::Kind AK, bool IsLateParsed)
145 : Attr(Context, CommonInfo, AK, IsLateParsed) {}
146
147public:
148 static bool classof(const Attr *A) {
149 return A->getKind() >= attr::FirstStmtAttr &&
150 A->getKind() <= attr::LastStmtAttr;
151 }
152};
153
154class InheritableAttr : public Attr {
155protected:
156 InheritableAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
157 attr::Kind AK, bool IsLateParsed,
159 : Attr(Context, CommonInfo, AK, IsLateParsed) {
160 this->InheritEvenIfAlreadyPresent = InheritEvenIfAlreadyPresent;
161 }
162
163public:
164 void setInherited(bool I) { Inherited = I; }
165
166 /// Should this attribute be inherited from a prior declaration even if it's
167 /// explicitly provided in the current declaration?
171
172 // Implement isa/cast/dyncast/etc.
173 static bool classof(const Attr *A) {
174 return A->getKind() >= attr::FirstInheritableAttr &&
175 A->getKind() <= attr::LastInheritableAttr;
176 }
177};
178
180protected:
181 DeclOrStmtAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
182 attr::Kind AK, bool IsLateParsed,
184 : InheritableAttr(Context, CommonInfo, AK, IsLateParsed,
186
187public:
188 static bool classof(const Attr *A) {
189 return A->getKind() >= attr::FirstDeclOrStmtAttr &&
190 A->getKind() <= attr::LastDeclOrStmtAttr;
191 }
192};
193
195protected:
201
202public:
203 // Implement isa/cast/dyncast/etc.
204 static bool classof(const Attr *A) {
205 return A->getKind() >= attr::FirstInheritableParamAttr &&
206 A->getKind() <= attr::LastInheritableParamAttr;
207 }
208};
209
211protected:
218
219public:
220 // Implement isa/cast/dyncast/etc.
221 static bool classof(const Attr *A) {
222 return A->getKind() >= attr::FirstInheritableParamOrStmtAttr &&
223 A->getKind() <= attr::LastInheritableParamOrStmtAttr;
224 }
225};
226
228protected:
230 attr::Kind AK, bool IsLateParsed,
232 : InheritableAttr(Context, CommonInfo, AK, IsLateParsed,
234
235public:
236 // Implement isa/cast/dyncast/etc.
237 static bool classof(const Attr *A) {
238 return A->getKind() >= attr::FirstHLSLAnnotationAttr &&
239 A->getKind() <= attr::LastHLSLAnnotationAttr;
240 }
241};
242
244protected:
250
251public:
252 // Implement isa/cast/dyncast/etc.
253 static bool classof(const Attr *A) {
254 return A->getKind() >= attr::FirstHLSLSemanticBaseAttr &&
255 A->getKind() <= attr::LastHLSLSemanticBaseAttr;
256 }
257};
258
259/// A parameter attribute which changes the argument-passing ABI rule
260/// for the parameter.
262protected:
268
269public:
270 ParameterABI getABI() const;
271
272 static bool classof(const Attr *A) {
273 return A->getKind() >= attr::FirstParameterABIAttr &&
274 A->getKind() <= attr::LastParameterABIAttr;
275 }
276};
277
278/// A single parameter index whose accessors require each use to make explicit
279/// the parameter index encoding needed.
280class ParamIdx {
281public:
282 constexpr static unsigned IdxBitWidth = 30;
283
284private:
285 // Idx is exposed only via accessors that specify specific encodings.
286 unsigned Idx : IdxBitWidth;
287 LLVM_PREFERRED_TYPE(bool)
288 unsigned HasThis : 1;
289 LLVM_PREFERRED_TYPE(bool)
290 unsigned IsValid : 1;
291
292 void assertComparable(const ParamIdx &I) const {
293 assert(isValid() && I.isValid() &&
294 "ParamIdx must be valid to be compared");
295 // It's possible to compare indices from separate functions, but so far
296 // it's not proven useful. Moreover, it might be confusing because a
297 // comparison on the results of getASTIndex might be inconsistent with a
298 // comparison on the ParamIdx objects themselves.
299 assert(HasThis == I.HasThis &&
300 "ParamIdx must be for the same function to be compared");
301 }
302
303public:
304 /// Construct an invalid parameter index (\c isValid returns false and
305 /// accessors fail an assert).
306 ParamIdx() : Idx(0), HasThis(false), IsValid(false) {}
307
308 /// \param Idx is the parameter index as it is normally specified in
309 /// attributes in the source: one-origin including any C++ implicit this
310 /// parameter.
311 ///
312 /// \param D is the declaration containing the parameters. It is used to
313 /// determine if there is a C++ implicit this parameter.
314 ParamIdx(unsigned Idx, const Decl *D)
315 : Idx(Idx), HasThis(false), IsValid(true) {
316 assert(Idx >= 1 && "Idx must be one-origin");
317 if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(D))
318 HasThis = MethodDecl->isImplicitObjectMemberFunction();
319 }
320
321 /// A type into which \c ParamIdx can be serialized.
322 ///
323 /// A static assertion that it's of the correct size follows the \c ParamIdx
324 /// class definition.
326
327 /// Produce a representation that can later be passed to \c deserialize to
328 /// construct an equivalent \c ParamIdx.
330 return *reinterpret_cast<const SerialType *>(this);
331 }
332
333 /// Construct from a result from \c serialize.
335 // Using this two-step static_cast via void * instead of reinterpret_cast
336 // silences a -Wstrict-aliasing false positive from GCC7 and earlier.
337 void *ParamIdxPtr = static_cast<void *>(&S);
338 ParamIdx P(*static_cast<ParamIdx *>(ParamIdxPtr));
339 assert((!P.IsValid || P.Idx >= 1) && "valid Idx must be one-origin");
340 return P;
341 }
342
343 /// Is this parameter index valid?
344 bool isValid() const { return IsValid; }
345
346 /// Get the parameter index as it would normally be encoded for attributes at
347 /// the source level of representation: one-origin including any C++ implicit
348 /// this parameter.
349 ///
350 /// This encoding thus makes sense for diagnostics, pretty printing, and
351 /// constructing new attributes from a source-like specification.
352 unsigned getSourceIndex() const {
353 assert(isValid() && "ParamIdx must be valid");
354 return Idx;
355 }
356
357 /// Get the parameter index as it would normally be encoded at the AST level
358 /// of representation: zero-origin not including any C++ implicit this
359 /// parameter.
360 ///
361 /// This is the encoding primarily used in Sema. However, in diagnostics,
362 /// Sema uses \c getSourceIndex instead.
363 unsigned getASTIndex() const {
364 assert(isValid() && "ParamIdx must be valid");
365 assert(Idx >= 1 + HasThis &&
366 "stored index must be base-1 and not specify C++ implicit this");
367 return Idx - 1 - HasThis;
368 }
369
370 /// Get the parameter index as it would normally be encoded at the LLVM level
371 /// of representation: zero-origin including any C++ implicit this parameter.
372 ///
373 /// This is the encoding primarily used in CodeGen.
374 unsigned getLLVMIndex() const {
375 assert(isValid() && "ParamIdx must be valid");
376 assert(Idx >= 1 && "stored index must be base-1");
377 return Idx - 1;
378 }
379
380 bool operator==(const ParamIdx &I) const {
381 assertComparable(I);
382 return Idx == I.Idx;
383 }
384 bool operator!=(const ParamIdx &I) const {
385 assertComparable(I);
386 return Idx != I.Idx;
387 }
388 bool operator<(const ParamIdx &I) const {
389 assertComparable(I);
390 return Idx < I.Idx;
391 }
392 bool operator>(const ParamIdx &I) const {
393 assertComparable(I);
394 return Idx > I.Idx;
395 }
396 bool operator<=(const ParamIdx &I) const {
397 assertComparable(I);
398 return Idx <= I.Idx;
399 }
400 bool operator>=(const ParamIdx &I) const {
401 assertComparable(I);
402 return Idx >= I.Idx;
403 }
404};
405
406static_assert(sizeof(ParamIdx) == sizeof(ParamIdx::SerialType),
407 "ParamIdx does not fit its serialization type");
408
409#include "clang/AST/Attrs.inc" // IWYU pragma: export
410
412 const Attr *At) {
413 DB.AddTaggedVal(reinterpret_cast<uint64_t>(At), DiagnosticsEngine::ak_attr);
414 return DB;
415}
416
418 switch (getKind()) {
419 case attr::SwiftContext:
421 case attr::SwiftAsyncContext:
423 case attr::SwiftErrorResult:
425 case attr::SwiftIndirectResult:
427 case attr::HLSLParamModifier: {
428 const auto *A = cast<HLSLParamModifierAttr>(this);
429 if (A->isOut())
431 if (A->isInOut())
434 }
435 default:
436 llvm_unreachable("bad parameter ABI attribute kind");
437 }
438}
439} // end namespace clang
440
441#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:245
Attr - This represents one attribute.
Definition Attr.h:47
unsigned getSpellingListIndex() const
Definition Attr.h:95
unsigned IsPackExpansion
Definition Attr.h:58
bool isEquivalent(const Attr &Other, StructuralEquivalenceContext &Context) const
Attr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed)
Definition Attr.h:86
attr::Kind getKind() const
Definition Attr.h:93
unsigned Implicit
Definition Attr.h:60
void printPretty(raw_ostream &OS, const PrintingPolicy &Policy) const
static StringRef getDocumentation(attr::Kind)
void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx) const
unsigned InheritEvenIfAlreadyPresent
Definition Attr.h:66
unsigned Inherited
An index into the spelling list of an attribute defined in Attr.td file.
Definition Attr.h:56
const char * getSpelling() const
void setPackExpansion(bool PE)
Definition Attr.h:109
bool isInherited() const
Definition Attr.h:102
bool isImplicit() const
Returns true if the attribute has been implicitly created instead of explicitly written by the user.
Definition Attr.h:106
unsigned IsLateParsed
Definition Attr.h:64
Attr * clone(ASTContext &C) const
void setImplicit(bool I)
Definition Attr.h:107
SourceLocation getLocation() const
Definition Attr.h:100
bool isLateParsed() const
Definition Attr.h:115
bool isPackExpansion() const
Definition Attr.h:110
AttributeCommonInfo(const IdentifierInfo *AttrName, AttributeScopeInfo AttrScope, SourceRange AttrRange, Kind AttrKind, Form FormUsed)
unsigned getAttributeSpellingListIndex() const
static bool classof(const Attr *A)
Definition Attr.h:188
DeclOrStmtAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
Definition Attr.h:181
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
Represents a function declaration or definition.
Definition Decl.h:2071
HLSLAnnotationAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
Definition Attr.h:229
static bool classof(const Attr *A)
Definition Attr.h:237
static bool classof(const Attr *A)
Definition Attr.h:253
HLSLSemanticBaseAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
Definition Attr.h:245
void setInherited(bool I)
Definition Attr.h:164
static bool classof(const Attr *A)
Definition Attr.h:173
InheritableAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
Definition Attr.h:156
bool shouldInheritEvenIfAlreadyPresent() const
Should this attribute be inherited from a prior declaration even if it's explicitly provided in the c...
Definition Attr.h:168
static bool classof(const Attr *A)
Definition Attr.h:204
InheritableParamAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
Definition Attr.h:196
InheritableParamOrStmtAttr(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:221
Describes the source-level contents of one OpenMP 'adjust_args' clause.
This is the base type for all OpenACC Clauses.
bool operator==(const ParamIdx &I) const
Definition Attr.h:380
bool operator<=(const ParamIdx &I) const
Definition Attr.h:396
ParamIdx()
Construct an invalid parameter index (isValid returns false and accessors fail an assert).
Definition Attr.h:306
bool operator<(const ParamIdx &I) const
Definition Attr.h:388
bool isValid() const
Is this parameter index valid?
Definition Attr.h:344
static ParamIdx deserialize(SerialType S)
Construct from a result from serialize.
Definition Attr.h:334
unsigned getSourceIndex() const
Get the parameter index as it would normally be encoded for attributes at the source level of represe...
Definition Attr.h:352
bool operator>=(const ParamIdx &I) const
Definition Attr.h:400
unsigned getLLVMIndex() const
Get the parameter index as it would normally be encoded at the LLVM level of representation: zero-ori...
Definition Attr.h:374
ParamIdx(unsigned Idx, const Decl *D)
Definition Attr.h:314
unsigned getASTIndex() const
Get the parameter index as it would normally be encoded at the AST level of representation: zero-orig...
Definition Attr.h:363
static constexpr unsigned IdxBitWidth
Definition Attr.h:282
bool operator>(const ParamIdx &I) const
Definition Attr.h:392
uint32_t SerialType
A type into which ParamIdx can be serialized.
Definition Attr.h:325
SerialType serialize() const
Produce a representation that can later be passed to deserialize to construct an equivalent ParamIdx.
Definition Attr.h:329
bool operator!=(const ParamIdx &I) const
Definition Attr.h:384
ParameterABI getABI() const
Definition Attr.h:417
ParameterABIAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
Definition Attr.h:263
static bool classof(const Attr *A)
Definition Attr.h:272
Encodes a location in the source.
SourceLocation getBegin() const
static bool classof(const Attr *A)
Definition Attr.h:148
StmtAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed)
Definition Attr.h:143
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:130
static bool classof(const Attr *A)
Definition Attr.h:135
Top level wrappers for InstallAPI frontend operations.
ParameterABI
Kinds of parameter ABI.
Definition Specifiers.h:382
@ SwiftAsyncContext
This parameter (which must have pointer type) uses the special Swift asynchronous context-pointer ABI...
Definition Specifiers.h:403
@ SwiftErrorResult
This parameter (which must have pointer-to-pointer type) uses the special Swift error-result ABI trea...
Definition Specifiers.h:393
@ Ordinary
This parameter uses ordinary ABI rules for its type.
Definition Specifiers.h:384
@ SwiftIndirectResult
This parameter (which must have pointer type) is a Swift indirect result parameter.
Definition Specifiers.h:388
@ SwiftContext
This parameter (which must have pointer type) uses the special Swift context-pointer ABI treatment.
Definition Specifiers.h:398
const StreamingDiagnostic & operator<<(const StreamingDiagnostic &DB, const ConceptReference *C)
Insertion operator for diagnostics.
U cast(CodeGen::Address addr)
Definition Address.h:327
@ Other
Other implicit parameter.
Definition Decl.h:1787
#define false
Definition stdbool.h:26
#define true
Definition stdbool.h:25
Describes how types, statements, expressions, and declarations should be printed.