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 // Pretty print this attribute.
120 void printPretty(raw_ostream &OS, const PrintingPolicy &Policy) const;
121
122 static StringRef getDocumentation(attr::Kind);
123};
124
125class TypeAttr : public Attr {
126protected:
127 TypeAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
128 attr::Kind AK, bool IsLateParsed)
129 : Attr(Context, CommonInfo, AK, IsLateParsed) {}
130
131public:
132 static bool classof(const Attr *A) {
133 return A->getKind() >= attr::FirstTypeAttr &&
134 A->getKind() <= attr::LastTypeAttr;
135 }
136};
137
138class StmtAttr : public Attr {
139protected:
140 StmtAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
141 attr::Kind AK, bool IsLateParsed)
142 : Attr(Context, CommonInfo, AK, IsLateParsed) {}
143
144public:
145 static bool classof(const Attr *A) {
146 return A->getKind() >= attr::FirstStmtAttr &&
147 A->getKind() <= attr::LastStmtAttr;
148 }
149};
150
151class InheritableAttr : public Attr {
152protected:
153 InheritableAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
154 attr::Kind AK, bool IsLateParsed,
156 : Attr(Context, CommonInfo, AK, IsLateParsed) {
157 this->InheritEvenIfAlreadyPresent = InheritEvenIfAlreadyPresent;
158 }
159
160public:
161 void setInherited(bool I) { Inherited = I; }
162
163 /// Should this attribute be inherited from a prior declaration even if it's
164 /// explicitly provided in the current declaration?
168
169 // Implement isa/cast/dyncast/etc.
170 static bool classof(const Attr *A) {
171 return A->getKind() >= attr::FirstInheritableAttr &&
172 A->getKind() <= attr::LastInheritableAttr;
173 }
174};
175
177protected:
178 DeclOrStmtAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
179 attr::Kind AK, bool IsLateParsed,
181 : InheritableAttr(Context, CommonInfo, AK, IsLateParsed,
183
184public:
185 static bool classof(const Attr *A) {
186 return A->getKind() >= attr::FirstDeclOrStmtAttr &&
187 A->getKind() <= attr::LastDeclOrStmtAttr;
188 }
189};
190
192protected:
198
199public:
200 // Implement isa/cast/dyncast/etc.
201 static bool classof(const Attr *A) {
202 return A->getKind() >= attr::FirstInheritableParamAttr &&
203 A->getKind() <= attr::LastInheritableParamAttr;
204 }
205};
206
208protected:
215
216public:
217 // Implement isa/cast/dyncast/etc.
218 static bool classof(const Attr *A) {
219 return A->getKind() >= attr::FirstInheritableParamOrStmtAttr &&
220 A->getKind() <= attr::LastInheritableParamOrStmtAttr;
221 }
222};
223
225protected:
227 attr::Kind AK, bool IsLateParsed,
229 : InheritableAttr(Context, CommonInfo, AK, IsLateParsed,
231
232public:
233 // Implement isa/cast/dyncast/etc.
234 static bool classof(const Attr *A) {
235 return A->getKind() >= attr::FirstHLSLAnnotationAttr &&
236 A->getKind() <= attr::LastHLSLAnnotationAttr;
237 }
238};
239
241protected:
247
248public:
249 // Implement isa/cast/dyncast/etc.
250 static bool classof(const Attr *A) {
251 return A->getKind() >= attr::FirstHLSLSemanticBaseAttr &&
252 A->getKind() <= attr::LastHLSLSemanticBaseAttr;
253 }
254};
255
256/// A parameter attribute which changes the argument-passing ABI rule
257/// for the parameter.
259protected:
265
266public:
267 ParameterABI getABI() const;
268
269 static bool classof(const Attr *A) {
270 return A->getKind() >= attr::FirstParameterABIAttr &&
271 A->getKind() <= attr::LastParameterABIAttr;
272 }
273};
274
275/// A single parameter index whose accessors require each use to make explicit
276/// the parameter index encoding needed.
277class ParamIdx {
278 // Idx is exposed only via accessors that specify specific encodings.
279 unsigned Idx : 30;
280 LLVM_PREFERRED_TYPE(bool)
281 unsigned HasThis : 1;
282 LLVM_PREFERRED_TYPE(bool)
283 unsigned IsValid : 1;
284
285 void assertComparable(const ParamIdx &I) const {
286 assert(isValid() && I.isValid() &&
287 "ParamIdx must be valid to be compared");
288 // It's possible to compare indices from separate functions, but so far
289 // it's not proven useful. Moreover, it might be confusing because a
290 // comparison on the results of getASTIndex might be inconsistent with a
291 // comparison on the ParamIdx objects themselves.
292 assert(HasThis == I.HasThis &&
293 "ParamIdx must be for the same function to be compared");
294 }
295
296public:
297 /// Construct an invalid parameter index (\c isValid returns false and
298 /// accessors fail an assert).
299 ParamIdx() : Idx(0), HasThis(false), IsValid(false) {}
300
301 /// \param Idx is the parameter index as it is normally specified in
302 /// attributes in the source: one-origin including any C++ implicit this
303 /// parameter.
304 ///
305 /// \param D is the declaration containing the parameters. It is used to
306 /// determine if there is a C++ implicit this parameter.
307 ParamIdx(unsigned Idx, const Decl *D)
308 : Idx(Idx), HasThis(false), IsValid(true) {
309 assert(Idx >= 1 && "Idx must be one-origin");
310 if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(D))
311 HasThis = MethodDecl->isImplicitObjectMemberFunction();
312 }
313
314 /// A type into which \c ParamIdx can be serialized.
315 ///
316 /// A static assertion that it's of the correct size follows the \c ParamIdx
317 /// class definition.
318 typedef uint32_t SerialType;
319
320 /// Produce a representation that can later be passed to \c deserialize to
321 /// construct an equivalent \c ParamIdx.
323 return *reinterpret_cast<const SerialType *>(this);
324 }
325
326 /// Construct from a result from \c serialize.
328 // Using this two-step static_cast via void * instead of reinterpret_cast
329 // silences a -Wstrict-aliasing false positive from GCC7 and earlier.
330 void *ParamIdxPtr = static_cast<void *>(&S);
331 ParamIdx P(*static_cast<ParamIdx *>(ParamIdxPtr));
332 assert((!P.IsValid || P.Idx >= 1) && "valid Idx must be one-origin");
333 return P;
334 }
335
336 /// Is this parameter index valid?
337 bool isValid() const { return IsValid; }
338
339 /// Get the parameter index as it would normally be encoded for attributes at
340 /// the source level of representation: one-origin including any C++ implicit
341 /// this parameter.
342 ///
343 /// This encoding thus makes sense for diagnostics, pretty printing, and
344 /// constructing new attributes from a source-like specification.
345 unsigned getSourceIndex() const {
346 assert(isValid() && "ParamIdx must be valid");
347 return Idx;
348 }
349
350 /// Get the parameter index as it would normally be encoded at the AST level
351 /// of representation: zero-origin not including any C++ implicit this
352 /// parameter.
353 ///
354 /// This is the encoding primarily used in Sema. However, in diagnostics,
355 /// Sema uses \c getSourceIndex instead.
356 unsigned getASTIndex() const {
357 assert(isValid() && "ParamIdx must be valid");
358 assert(Idx >= 1 + HasThis &&
359 "stored index must be base-1 and not specify C++ implicit this");
360 return Idx - 1 - HasThis;
361 }
362
363 /// Get the parameter index as it would normally be encoded at the LLVM level
364 /// of representation: zero-origin including any C++ implicit this parameter.
365 ///
366 /// This is the encoding primarily used in CodeGen.
367 unsigned getLLVMIndex() const {
368 assert(isValid() && "ParamIdx must be valid");
369 assert(Idx >= 1 && "stored index must be base-1");
370 return Idx - 1;
371 }
372
373 bool operator==(const ParamIdx &I) const {
374 assertComparable(I);
375 return Idx == I.Idx;
376 }
377 bool operator!=(const ParamIdx &I) const {
378 assertComparable(I);
379 return Idx != I.Idx;
380 }
381 bool operator<(const ParamIdx &I) const {
382 assertComparable(I);
383 return Idx < I.Idx;
384 }
385 bool operator>(const ParamIdx &I) const {
386 assertComparable(I);
387 return Idx > I.Idx;
388 }
389 bool operator<=(const ParamIdx &I) const {
390 assertComparable(I);
391 return Idx <= I.Idx;
392 }
393 bool operator>=(const ParamIdx &I) const {
394 assertComparable(I);
395 return Idx >= I.Idx;
396 }
397};
398
399static_assert(sizeof(ParamIdx) == sizeof(ParamIdx::SerialType),
400 "ParamIdx does not fit its serialization type");
401
402#include "clang/AST/Attrs.inc" // IWYU pragma: export
403
405 const Attr *At) {
406 DB.AddTaggedVal(reinterpret_cast<uint64_t>(At), DiagnosticsEngine::ak_attr);
407 return DB;
408}
409
411 switch (getKind()) {
412 case attr::SwiftContext:
414 case attr::SwiftAsyncContext:
416 case attr::SwiftErrorResult:
418 case attr::SwiftIndirectResult:
420 case attr::HLSLParamModifier: {
421 const auto *A = cast<HLSLParamModifierAttr>(this);
422 if (A->isOut())
424 if (A->isInOut())
427 }
428 default:
429 llvm_unreachable("bad parameter ABI attribute kind");
430 }
431}
432} // end namespace clang
433
434#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: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)
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:185
DeclOrStmtAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
Definition Attr.h:178
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:226
static bool classof(const Attr *A)
Definition Attr.h:234
static bool classof(const Attr *A)
Definition Attr.h:250
HLSLSemanticBaseAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
Definition Attr.h:242
void setInherited(bool I)
Definition Attr.h:161
static bool classof(const Attr *A)
Definition Attr.h:170
InheritableAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
Definition Attr.h:153
bool shouldInheritEvenIfAlreadyPresent() const
Should this attribute be inherited from a prior declaration even if it's explicitly provided in the c...
Definition Attr.h:165
static bool classof(const Attr *A)
Definition Attr.h:201
InheritableParamAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
Definition Attr.h:193
InheritableParamOrStmtAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
Definition Attr.h:209
static bool classof(const Attr *A)
Definition Attr.h:218
This is the base type for all OpenACC Clauses.
bool operator==(const ParamIdx &I) const
Definition Attr.h:373
bool operator<=(const ParamIdx &I) const
Definition Attr.h:389
ParamIdx()
Construct an invalid parameter index (isValid returns false and accessors fail an assert).
Definition Attr.h:299
bool operator<(const ParamIdx &I) const
Definition Attr.h:381
bool isValid() const
Is this parameter index valid?
Definition Attr.h:337
static ParamIdx deserialize(SerialType S)
Construct from a result from serialize.
Definition Attr.h:327
unsigned getSourceIndex() const
Get the parameter index as it would normally be encoded for attributes at the source level of represe...
Definition Attr.h:345
bool operator>=(const ParamIdx &I) const
Definition Attr.h:393
unsigned getLLVMIndex() const
Get the parameter index as it would normally be encoded at the LLVM level of representation: zero-ori...
Definition Attr.h:367
ParamIdx(unsigned Idx, const Decl *D)
Definition Attr.h:307
unsigned getASTIndex() const
Get the parameter index as it would normally be encoded at the AST level of representation: zero-orig...
Definition Attr.h:356
bool operator>(const ParamIdx &I) const
Definition Attr.h:385
uint32_t SerialType
A type into which ParamIdx can be serialized.
Definition Attr.h:318
SerialType serialize() const
Produce a representation that can later be passed to deserialize to construct an equivalent ParamIdx.
Definition Attr.h:322
bool operator!=(const ParamIdx &I) const
Definition Attr.h:377
ParameterABI getABI() const
Definition Attr.h:410
ParameterABIAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
Definition Attr.h:260
static bool classof(const Attr *A)
Definition Attr.h:269
Encodes a location in the source.
SourceLocation getBegin() const
static bool classof(const Attr *A)
Definition Attr.h:145
StmtAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed)
Definition Attr.h:140
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:127
static bool classof(const Attr *A)
Definition Attr.h:132
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
@ Other
Other implicit parameter.
Definition Decl.h:1746
#define false
Definition stdbool.h:26
#define true
Definition stdbool.h:25
Describes how types, statements, expressions, and declarations should be printed.