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