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 18 #include "clang/Basic/AttrSubjectMatchRules.h" 19 #include "clang/Basic/AttributeCommonInfo.h" 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 34 struct ParsedAttrInfo { 35 /// Corresponds to the Kind enum. 36 unsigned AttrKind : 16; 37 /// The number of required arguments of this attribute. 38 unsigned NumArgs : 4; 39 /// The number of optional arguments of this attributes. 40 unsigned OptArgs : 4; 41 /// The number of non-fake arguments specified in the attribute definition. 42 unsigned NumArgMembers : 4; 43 /// True if the parsing does not match the semantic content. 44 unsigned HasCustomParsing : 1; 45 // True if this attribute accepts expression parameter pack expansions. 46 unsigned AcceptsExprPack : 1; 47 /// True if this attribute is only available for certain targets. 48 unsigned IsTargetSpecific : 1; 49 /// True if this attribute applies to types. 50 unsigned IsType : 1; 51 /// True if this attribute applies to statements. 52 unsigned IsStmt : 1; 53 /// True if this attribute has any spellings that are known to gcc. 54 unsigned IsKnownToGCC : 1; 55 /// True if this attribute is supported by #pragma clang attribute. 56 unsigned IsSupportedByPragmaAttribute : 1; 57 /// The syntaxes supported by this attribute and how they're spelled. 58 struct Spelling { 59 AttributeCommonInfo::Syntax Syntax; 60 const char *NormalizedFullName; 61 }; 62 ArrayRef<Spelling> Spellings; 63 // The names of the known arguments of this attribute. 64 ArrayRef<const char *> ArgNames; 65 66 protected: 67 constexpr ParsedAttrInfo(AttributeCommonInfo::Kind AttrKind = 68 AttributeCommonInfo::NoSemaHandlerAttribute) 69 : AttrKind(AttrKind), NumArgs(0), OptArgs(0), NumArgMembers(0), 70 HasCustomParsing(0), AcceptsExprPack(0), IsTargetSpecific(0), IsType(0), 71 IsStmt(0), IsKnownToGCC(0), IsSupportedByPragmaAttribute(0) {} 72 73 constexpr ParsedAttrInfo(AttributeCommonInfo::Kind AttrKind, unsigned NumArgs, 74 unsigned OptArgs, unsigned NumArgMembers, 75 unsigned HasCustomParsing, unsigned AcceptsExprPack, 76 unsigned IsTargetSpecific, unsigned IsType, 77 unsigned IsStmt, unsigned IsKnownToGCC, 78 unsigned IsSupportedByPragmaAttribute, 79 ArrayRef<Spelling> Spellings, 80 ArrayRef<const char *> ArgNames) 81 : AttrKind(AttrKind), NumArgs(NumArgs), OptArgs(OptArgs), 82 NumArgMembers(NumArgMembers), HasCustomParsing(HasCustomParsing), 83 AcceptsExprPack(AcceptsExprPack), IsTargetSpecific(IsTargetSpecific), 84 IsType(IsType), IsStmt(IsStmt), IsKnownToGCC(IsKnownToGCC), 85 IsSupportedByPragmaAttribute(IsSupportedByPragmaAttribute), 86 Spellings(Spellings), ArgNames(ArgNames) {} 87 88 public: 89 virtual ~ParsedAttrInfo() = default; 90 91 /// Check if this attribute has specified spelling. 92 bool hasSpelling(AttributeCommonInfo::Syntax Syntax, StringRef Name) const { 93 return llvm::any_of(Spellings, [&](const Spelling &S) { 94 return (S.Syntax == Syntax && S.NormalizedFullName == Name); 95 }); 96 } 97 98 /// Check if this attribute appertains to D, and issue a diagnostic if not. 99 virtual bool diagAppertainsToDecl(Sema &S, const ParsedAttr &Attr, 100 const Decl *D) const { 101 return true; 102 } 103 /// Check if this attribute appertains to St, and issue a diagnostic if not. 104 virtual bool diagAppertainsToStmt(Sema &S, const ParsedAttr &Attr, 105 const Stmt *St) const { 106 return true; 107 } 108 /// Check if the given attribute is mutually exclusive with other attributes 109 /// already applied to the given declaration. 110 virtual bool diagMutualExclusion(Sema &S, const ParsedAttr &A, 111 const Decl *D) const { 112 return true; 113 } 114 /// Check if this attribute is allowed by the language we are compiling. 115 virtual bool acceptsLangOpts(const LangOptions &LO) const { return true; } 116 117 /// Check if this attribute is allowed when compiling for the given target. 118 virtual bool existsInTarget(const TargetInfo &Target) const { return true; } 119 /// Convert the spelling index of Attr to a semantic spelling enum value. 120 virtual unsigned 121 spellingIndexToSemanticSpelling(const ParsedAttr &Attr) const { 122 return UINT_MAX; 123 } 124 /// Returns true if the specified parameter index for this attribute in 125 /// Attr.td is an ExprArgument or VariadicExprArgument, or a subclass thereof; 126 /// returns false otherwise. 127 virtual bool isParamExpr(size_t N) const { return false; } 128 /// Populate Rules with the match rules of this attribute. 129 virtual void getPragmaAttributeMatchRules( 130 llvm::SmallVectorImpl<std::pair<attr::SubjectMatchRule, bool>> &Rules, 131 const LangOptions &LangOpts) const {} 132 133 enum AttrHandling { NotHandled, AttributeApplied, AttributeNotApplied }; 134 /// If this ParsedAttrInfo knows how to handle this ParsedAttr applied to this 135 /// Decl then do so and return either AttributeApplied if it was applied or 136 /// AttributeNotApplied if it wasn't. Otherwise return NotHandled. 137 virtual AttrHandling handleDeclAttribute(Sema &S, Decl *D, 138 const ParsedAttr &Attr) const { 139 return NotHandled; 140 } 141 142 static const ParsedAttrInfo &get(const AttributeCommonInfo &A); 143 static ArrayRef<const ParsedAttrInfo *> getAllBuiltin(); 144 }; 145 146 typedef llvm::Registry<ParsedAttrInfo> ParsedAttrInfoRegistry; 147 148 const std::list<std::unique_ptr<ParsedAttrInfo>> &getAttributePluginInstances(); 149 150 } // namespace clang 151 152 #endif // LLVM_CLANG_BASIC_PARSEDATTRINFO_H 153