clang  19.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 
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/Support/Registry.h"
22 #include <climits>
23 #include <list>
24 
25 namespace clang {
26 
27 class Decl;
28 class LangOptions;
29 class ParsedAttr;
30 class Sema;
31 class Stmt;
32 class TargetInfo;
33 
35  /// Corresponds to the Kind enum.
36  LLVM_PREFERRED_TYPE(AttributeCommonInfo::Kind)
38  /// The number of required arguments of this attribute.
39  unsigned NumArgs : 4;
40  /// The number of optional arguments of this attributes.
41  unsigned OptArgs : 4;
42  /// The number of non-fake arguments specified in the attribute definition.
43  unsigned NumArgMembers : 4;
44  /// True if the parsing does not match the semantic content.
45  LLVM_PREFERRED_TYPE(bool)
46  unsigned HasCustomParsing : 1;
47  // True if this attribute accepts expression parameter pack expansions.
48  LLVM_PREFERRED_TYPE(bool)
49  unsigned AcceptsExprPack : 1;
50  /// True if this attribute is only available for certain targets.
51  LLVM_PREFERRED_TYPE(bool)
52  unsigned IsTargetSpecific : 1;
53  /// True if this attribute applies to types.
54  LLVM_PREFERRED_TYPE(bool)
55  unsigned IsType : 1;
56  /// True if this attribute applies to statements.
57  LLVM_PREFERRED_TYPE(bool)
58  unsigned IsStmt : 1;
59  /// True if this attribute has any spellings that are known to gcc.
60  LLVM_PREFERRED_TYPE(bool)
61  unsigned IsKnownToGCC : 1;
62  /// True if this attribute is supported by #pragma clang attribute.
63  LLVM_PREFERRED_TYPE(bool)
65  /// True if this attribute supports a nonconforming behavior when applied to
66  /// a lambda in the type position.
68  /// The syntaxes supported by this attribute and how they're spelled.
69  struct Spelling {
71  const char *NormalizedFullName;
72  };
74  // The names of the known arguments of this attribute.
76 
77 protected:
84 
86  unsigned OptArgs, unsigned NumArgMembers,
87  unsigned HasCustomParsing, unsigned AcceptsExprPack,
88  unsigned IsTargetSpecific, unsigned IsType,
89  unsigned IsStmt, unsigned IsKnownToGCC,
101 
102 public:
103  virtual ~ParsedAttrInfo() = default;
104 
105  /// Check if this attribute has specified spelling.
106  bool hasSpelling(AttributeCommonInfo::Syntax Syntax, StringRef Name) const {
107  return llvm::any_of(Spellings, [&](const Spelling &S) {
108  return (S.Syntax == Syntax && S.NormalizedFullName == Name);
109  });
110  }
111 
112  /// Check if this attribute appertains to D, and issue a diagnostic if not.
113  virtual bool diagAppertainsToDecl(Sema &S, const ParsedAttr &Attr,
114  const Decl *D) const {
115  return true;
116  }
117  /// Check if this attribute appertains to St, and issue a diagnostic if not.
118  virtual bool diagAppertainsToStmt(Sema &S, const ParsedAttr &Attr,
119  const Stmt *St) const {
120  return true;
121  }
122  /// Check if the given attribute is mutually exclusive with other attributes
123  /// already applied to the given declaration.
124  virtual bool diagMutualExclusion(Sema &S, const ParsedAttr &A,
125  const Decl *D) const {
126  return true;
127  }
128  /// Check if this attribute is allowed by the language we are compiling.
129  virtual bool acceptsLangOpts(const LangOptions &LO) const { return true; }
130 
131  /// Check if this attribute is allowed when compiling for the given target.
132  virtual bool existsInTarget(const TargetInfo &Target) const { return true; }
133 
134  /// Check if this attribute's spelling is allowed when compiling for the given
135  /// target.
137  const unsigned SpellingListIndex) const {
138  return true;
139  }
140 
141  /// Convert the spelling index of Attr to a semantic spelling enum value.
142  virtual unsigned
144  return UINT_MAX;
145  }
146  /// Returns true if the specified parameter index for this attribute in
147  /// Attr.td is an ExprArgument or VariadicExprArgument, or a subclass thereof;
148  /// returns false otherwise.
149  virtual bool isParamExpr(size_t N) const { return false; }
150  /// Populate Rules with the match rules of this attribute.
152  llvm::SmallVectorImpl<std::pair<attr::SubjectMatchRule, bool>> &Rules,
153  const LangOptions &LangOpts) const {}
154 
156  /// If this ParsedAttrInfo knows how to handle this ParsedAttr applied to this
157  /// Decl then do so and return either AttributeApplied if it was applied or
158  /// AttributeNotApplied if it wasn't. Otherwise return NotHandled.
160  const ParsedAttr &Attr) const {
161  return NotHandled;
162  }
163 
164  static const ParsedAttrInfo &get(const AttributeCommonInfo &A);
166 };
167 
168 typedef llvm::Registry<ParsedAttrInfo> ParsedAttrInfoRegistry;
169 
170 const std::list<std::unique_ptr<ParsedAttrInfo>> &getAttributePluginInstances();
171 
172 } // namespace clang
173 
174 #endif // LLVM_CLANG_BASIC_PARSEDATTRINFO_H
llvm::MachO::Target Target
Definition: MachO.h:50
Attr - This represents one attribute.
Definition: Attr.h:46
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:482
ParsedAttr - Represents a syntactic attribute.
Definition: ParsedAttr.h:129
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:462
Stmt - This represents one statement.
Definition: Stmt.h:84
Exposes information about the current target.
Definition: TargetInfo.h:218
#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.
const std::list< std::unique_ptr< ParsedAttrInfo > > & getAttributePluginInstances()
llvm::Registry< ParsedAttrInfo > ParsedAttrInfoRegistry
The syntaxes supported by this attribute and how they're spelled.
AttributeCommonInfo::Syntax Syntax
constexpr ParsedAttrInfo(AttributeCommonInfo::Kind AttrKind=AttributeCommonInfo::NoSemaHandlerAttribute)
unsigned SupportsNonconformingLambdaSyntax
True if this attribute supports a nonconforming behavior when applied to a lambda in the type positio...
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:144
unsigned IsKnownToGCC
True if this attribute has any spellings that are known to gcc.
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:115
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...
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, unsigned SupportsNonconformingLambdaSyntax, ArrayRef< Spelling > Spellings, ArrayRef< const char * > ArgNames)
unsigned IsStmt
True if this attribute applies to statements.
unsigned NumArgMembers
The number of non-fake arguments specified in the attribute definition.