clang 20.0.0git
ParsedAttrInfo.h
Go to the documentation of this file.
1//===- ParsedAttrInfo.h - Info needed to parse an attribute -----*- 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 ParsedAttrInfo class, which dictates how to
10// parse an attribute. This class is the one that plugins derive to
11// define a new attribute.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_BASIC_PARSEDATTRINFO_H
16#define LLVM_CLANG_BASIC_PARSEDATTRINFO_H
17
21#include "llvm/ADT/ArrayRef.h"
22#include "llvm/Support/Registry.h"
23#include <climits>
24#include <list>
25
26namespace clang {
27
28class Attr;
29class Decl;
30class LangOptions;
31class ParsedAttr;
32class Sema;
33class Stmt;
34class TargetInfo;
35
37 /// Corresponds to the Kind enum.
38 LLVM_PREFERRED_TYPE(AttributeCommonInfo::Kind)
40 /// The number of required arguments of this attribute.
41 unsigned NumArgs : 4;
42 /// The number of optional arguments of this attributes.
43 unsigned OptArgs : 4;
44 /// The number of non-fake arguments specified in the attribute definition.
45 unsigned NumArgMembers : 4;
46 /// True if the parsing does not match the semantic content.
47 LLVM_PREFERRED_TYPE(bool)
48 unsigned HasCustomParsing : 1;
49 // True if this attribute accepts expression parameter pack expansions.
50 LLVM_PREFERRED_TYPE(bool)
51 unsigned AcceptsExprPack : 1;
52 /// True if this attribute is only available for certain targets.
53 LLVM_PREFERRED_TYPE(bool)
54 unsigned IsTargetSpecific : 1;
55 /// True if this attribute applies to types.
56 LLVM_PREFERRED_TYPE(bool)
57 unsigned IsType : 1;
58 /// True if this attribute applies to statements.
59 LLVM_PREFERRED_TYPE(bool)
60 unsigned IsStmt : 1;
61 /// True if this attribute has any spellings that are known to gcc.
62 LLVM_PREFERRED_TYPE(bool)
63 unsigned IsKnownToGCC : 1;
64 /// True if this attribute is supported by #pragma clang attribute.
65 LLVM_PREFERRED_TYPE(bool)
67 /// The syntaxes supported by this attribute and how they're spelled.
68 struct Spelling {
70 const char *NormalizedFullName;
71 };
73 // The names of the known arguments of this attribute.
75
76protected:
82
84 unsigned OptArgs, unsigned NumArgMembers,
85 unsigned HasCustomParsing, unsigned AcceptsExprPack,
86 unsigned IsTargetSpecific, unsigned IsType,
87 unsigned IsStmt, unsigned IsKnownToGCC,
97
98public:
99 virtual ~ParsedAttrInfo() = default;
100
101 /// Check if this attribute has specified spelling.
102 bool hasSpelling(AttributeCommonInfo::Syntax Syntax, StringRef Name) const {
103 return llvm::any_of(Spellings, [&](const Spelling &S) {
104 return (S.Syntax == Syntax && S.NormalizedFullName == Name);
105 });
106 }
107
108 /// Check if this attribute appertains to D, and issue a diagnostic if not.
109 virtual bool diagAppertainsToDecl(Sema &S, const ParsedAttr &Attr,
110 const Decl *D) const {
111 return true;
112 }
113 /// Check if this attribute appertains to St, and issue a diagnostic if not.
114 virtual bool diagAppertainsToStmt(Sema &S, const ParsedAttr &Attr,
115 const Stmt *St) const {
116 return true;
117 }
118 /// Check if the given attribute is mutually exclusive with other attributes
119 /// already applied to the given declaration.
120 virtual bool diagMutualExclusion(Sema &S, const ParsedAttr &A,
121 const Decl *D) const {
122 return true;
123 }
124 /// Check if this attribute is allowed by the language we are compiling.
125 virtual bool acceptsLangOpts(const LangOptions &LO) const { return true; }
126
127 /// Check if this attribute is allowed when compiling for the given target.
128 virtual bool existsInTarget(const TargetInfo &Target) const { return true; }
129
130 /// Check if this attribute's spelling is allowed when compiling for the given
131 /// target.
133 const unsigned SpellingListIndex) const {
134 return true;
135 }
136
137 /// Convert the spelling index of Attr to a semantic spelling enum value.
138 virtual unsigned
140 return UINT_MAX;
141 }
142 /// Returns true if the specified parameter index for this attribute in
143 /// Attr.td is an ExprArgument or VariadicExprArgument, or a subclass thereof;
144 /// returns false otherwise.
145 virtual bool isParamExpr(size_t N) const { return false; }
146 /// Populate Rules with the match rules of this attribute.
148 llvm::SmallVectorImpl<std::pair<attr::SubjectMatchRule, bool>> &Rules,
149 const LangOptions &LangOpts) const {}
150
152 /// If this ParsedAttrInfo knows how to handle this ParsedAttr applied to this
153 /// Decl then do so and return either AttributeApplied if it was applied or
154 /// AttributeNotApplied if it wasn't. Otherwise return NotHandled.
156 const ParsedAttr &Attr) const {
157 return NotHandled;
158 }
159 /// If this ParsedAttrInfo knows how to handle this ParsedAttr applied to this
160 /// Stmt then do so (referencing the resulting Attr in Result) and return
161 /// either AttributeApplied if it was applied or AttributeNotApplied if it
162 /// wasn't. Otherwise return NotHandled.
164 const ParsedAttr &Attr,
165 class Attr *&Result) const {
166 return NotHandled;
167 }
168
169 static const ParsedAttrInfo &get(const AttributeCommonInfo &A);
171};
172
173typedef llvm::Registry<ParsedAttrInfo> ParsedAttrInfoRegistry;
174
175const std::list<std::unique_ptr<ParsedAttrInfo>> &getAttributePluginInstances();
176
177} // namespace clang
178
179namespace llvm {
180extern template class CLANG_TEMPLATE_ABI Registry<clang::ParsedAttrInfo>;
181} // namespace llvm
182
183#endif // LLVM_CLANG_BASIC_PARSEDATTRINFO_H
const Decl * D
llvm::MachO::Target Target
Definition: MachO.h:51
Attr - This represents one attribute.
Definition: Attr.h:43
Syntax
The style used to specify an attribute.
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:499
ParsedAttr - Represents a syntactic attribute.
Definition: ParsedAttr.h:129
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:463
Stmt - This represents one statement.
Definition: Stmt.h:84
Exposes information about the current target.
Definition: TargetInfo.h:220
#define UINT_MAX
Definition: limits.h:64
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
The JSON file list parser is used to communicate input to InstallAPI.
@ Result
The result type of a method or function.
const std::list< std::unique_ptr< ParsedAttrInfo > > & getAttributePluginInstances()
llvm::Registry< ParsedAttrInfo > ParsedAttrInfoRegistry
Diagnostic wrappers for TextAPI types for error reporting.
Definition: Dominators.h:30
The syntaxes supported by this attribute and how they're spelled.
AttributeCommonInfo::Syntax Syntax
constexpr ParsedAttrInfo(AttributeCommonInfo::Kind AttrKind=AttributeCommonInfo::NoSemaHandlerAttribute)
bool hasSpelling(AttributeCommonInfo::Syntax Syntax, StringRef Name) const
Check if this attribute has specified spelling.
virtual bool acceptsLangOpts(const LangOptions &LO) const
Check if this attribute is allowed by the language we are compiling.
static ArrayRef< const ParsedAttrInfo * > getAllBuiltin()
Definition: ParsedAttr.cpp:142
unsigned IsKnownToGCC
True if this attribute has any spellings that are known to gcc.
virtual AttrHandling handleStmtAttribute(Sema &S, Stmt *St, const ParsedAttr &Attr, class Attr *&Result) const
If this ParsedAttrInfo knows how to handle this ParsedAttr applied to this Stmt then do so (referenci...
ArrayRef< const char * > ArgNames
unsigned HasCustomParsing
True if the parsing does not match the semantic content.
ArrayRef< Spelling > Spellings
unsigned IsType
True if this attribute applies to types.
unsigned IsTargetSpecific
True if this attribute is only available for certain targets.
unsigned IsSupportedByPragmaAttribute
True if this attribute is supported by #pragma clang attribute.
unsigned AttrKind
Corresponds to the Kind enum.
unsigned NumArgs
The number of required arguments of this attribute.
virtual bool diagAppertainsToStmt(Sema &S, const ParsedAttr &Attr, const Stmt *St) const
Check if this attribute appertains to St, and issue a diagnostic if not.
virtual unsigned spellingIndexToSemanticSpelling(const ParsedAttr &Attr) const
Convert the spelling index of Attr to a semantic spelling enum value.
virtual AttrHandling handleDeclAttribute(Sema &S, Decl *D, const ParsedAttr &Attr) const
If this ParsedAttrInfo knows how to handle this ParsedAttr applied to this Decl then do so and return...
static const ParsedAttrInfo & get(const AttributeCommonInfo &A)
Definition: ParsedAttr.cpp:113
constexpr ParsedAttrInfo(AttributeCommonInfo::Kind AttrKind, unsigned NumArgs, unsigned OptArgs, unsigned NumArgMembers, unsigned HasCustomParsing, unsigned AcceptsExprPack, unsigned IsTargetSpecific, unsigned IsType, unsigned IsStmt, unsigned IsKnownToGCC, unsigned IsSupportedByPragmaAttribute, ArrayRef< Spelling > Spellings, ArrayRef< const char * > ArgNames)
virtual bool isParamExpr(size_t N) const
Returns true if the specified parameter index for this attribute in Attr.td is an ExprArgument or Var...
virtual void getPragmaAttributeMatchRules(llvm::SmallVectorImpl< std::pair< attr::SubjectMatchRule, bool > > &Rules, const LangOptions &LangOpts) const
Populate Rules with the match rules of this attribute.
virtual bool existsInTarget(const TargetInfo &Target) const
Check if this attribute is allowed when compiling for the given target.
virtual ~ParsedAttrInfo()=default
virtual bool spellingExistsInTarget(const TargetInfo &Target, const unsigned SpellingListIndex) const
Check if this attribute's spelling is allowed when compiling for the given target.
virtual bool diagAppertainsToDecl(Sema &S, const ParsedAttr &Attr, const Decl *D) const
Check if this attribute appertains to D, and issue a diagnostic if not.
unsigned OptArgs
The number of optional arguments of this attributes.
virtual bool diagMutualExclusion(Sema &S, const ParsedAttr &A, const Decl *D) const
Check if the given attribute is mutually exclusive with other attributes already applied to the given...
unsigned IsStmt
True if this attribute applies to statements.
unsigned NumArgMembers
The number of non-fake arguments specified in the attribute definition.