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"
17#include "clang/AST/AttrIterator.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclCXX.h"
20#include "clang/AST/Type.h"
21#include "clang/Basic/AttrKinds.h"
22#include "clang/Basic/AttributeCommonInfo.h"
23#include "clang/Basic/LLVM.h"
24#include "clang/Basic/LangOptions.h"
25#include "clang/Basic/OpenMPKinds.h"
26#include "clang/Basic/Sanitizers.h"
27#include "clang/Basic/SourceLocation.h"
28#include "clang/Support/Compiler.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;
39class AttributeCommonInfo;
40class FunctionDecl;
41class OMPAdjustArgsDescriptor;
42class OMPTraitInfo;
43class OpenACCClause;
44struct StructuralEquivalenceContext;
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)
56 unsigned Inherited : 1;
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)
66 unsigned InheritEvenIfAlreadyPresent : 1;
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:
86 Attr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
87 attr::Kind AK, bool IsLateParsed)
88 : AttributeCommonInfo(CommonInfo), AttrKind(AK), Inherited(false),
89 IsPackExpansion(false), Implicit(false), IsLateParsed(IsLateParsed),
90 InheritEvenIfAlreadyPresent(false) {}
91
92public:
93 attr::Kind getKind() const { return static_cast<attr::Kind>(AttrKind); }
94
95 unsigned getSpellingListIndex() const {
96 return getAttributeSpellingListIndex();
97 }
98 const char *getSpelling() const;
99
100 SourceLocation getLocation() const { return getRange().getBegin(); }
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.
113 Attr *clone(ASTContext &C) const;
114
115 bool isLateParsed() const { return IsLateParsed; }
116
117 bool isEquivalent(const Attr &Other,
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,
158 bool InheritEvenIfAlreadyPresent)
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?
168 bool shouldInheritEvenIfAlreadyPresent() const {
169 return InheritEvenIfAlreadyPresent;
170 }
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
179class DeclOrStmtAttr : public InheritableAttr {
180protected:
181 DeclOrStmtAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
182 attr::Kind AK, bool IsLateParsed,
183 bool InheritEvenIfAlreadyPresent)
184 : InheritableAttr(Context, CommonInfo, AK, IsLateParsed,
185 InheritEvenIfAlreadyPresent) {}
186
187public:
188 static bool classof(const Attr *A) {
189 return A->getKind() >= attr::FirstDeclOrStmtAttr &&
190 A->getKind() <= attr::LastDeclOrStmtAttr;
191 }
192};
193
194class InheritableParamAttr : public InheritableAttr {
195protected:
196 InheritableParamAttr(ASTContext &Context,
197 const AttributeCommonInfo &CommonInfo, attr::Kind AK,
198 bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
199 : InheritableAttr(Context, CommonInfo, AK, IsLateParsed,
200 InheritEvenIfAlreadyPresent) {}
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
210class InheritableParamOrStmtAttr : public InheritableParamAttr {
211protected:
212 InheritableParamOrStmtAttr(ASTContext &Context,
213 const AttributeCommonInfo &CommonInfo,
214 attr::Kind AK, bool IsLateParsed,
215 bool InheritEvenIfAlreadyPresent)
216 : InheritableParamAttr(Context, CommonInfo, AK, IsLateParsed,
217 InheritEvenIfAlreadyPresent) {}
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
227class HLSLAnnotationAttr : public InheritableAttr {
228protected:
229 HLSLAnnotationAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
230 attr::Kind AK, bool IsLateParsed,
231 bool InheritEvenIfAlreadyPresent)
232 : InheritableAttr(Context, CommonInfo, AK, IsLateParsed,
233 InheritEvenIfAlreadyPresent) {}
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
243class HLSLSemanticBaseAttr : public HLSLAnnotationAttr {
244protected:
245 HLSLSemanticBaseAttr(ASTContext &Context,
246 const AttributeCommonInfo &CommonInfo, attr::Kind AK,
247 bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
248 : HLSLAnnotationAttr(Context, CommonInfo, AK, IsLateParsed,
249 InheritEvenIfAlreadyPresent) {}
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.
261class ParameterABIAttr : public InheritableParamAttr {
262protected:
263 ParameterABIAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
264 attr::Kind AK, bool IsLateParsed,
265 bool InheritEvenIfAlreadyPresent)
266 : InheritableParamAttr(Context, CommonInfo, AK, IsLateParsed,
267 InheritEvenIfAlreadyPresent) {}
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>(Val: 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.
325 typedef uint32_t SerialType;
326
327 /// Produce a representation that can later be passed to \c deserialize to
328 /// construct an equivalent \c ParamIdx.
329 SerialType serialize() const {
330 return *reinterpret_cast<const SerialType *>(this);
331 }
332
333 /// Construct from a result from \c serialize.
334 static ParamIdx deserialize(SerialType S) {
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
411inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB,
412 const Attr *At) {
413 DB.AddTaggedVal(V: reinterpret_cast<uint64_t>(At), Kind: DiagnosticsEngine::ak_attr);
414 return DB;
415}
416
417inline ParameterABI ParameterABIAttr::getABI() const {
418 switch (getKind()) {
419 case attr::SwiftContext:
420 return ParameterABI::SwiftContext;
421 case attr::SwiftAsyncContext:
422 return ParameterABI::SwiftAsyncContext;
423 case attr::SwiftErrorResult:
424 return ParameterABI::SwiftErrorResult;
425 case attr::SwiftIndirectResult:
426 return ParameterABI::SwiftIndirectResult;
427 case attr::HLSLParamModifier: {
428 const auto *A = cast<HLSLParamModifierAttr>(Val: this);
429 if (A->isOut())
430 return ParameterABI::HLSLOut;
431 if (A->isInOut())
432 return ParameterABI::HLSLInOut;
433 return ParameterABI::Ordinary;
434 }
435 default:
436 llvm_unreachable("bad parameter ABI attribute kind");
437 }
438}
439} // end namespace clang
440
441#endif
442