#include "clang/Sema/ParsedAttr.h"
#include "clang/AST/ASTContext.h"
#include "clang/Basic/AttrSubjectMatchRules.h"
#include "clang/Basic/IdentifierTable.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Sema/SemaInternal.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/ManagedStatic.h"
#include <cassert>
#include <cstddef>
#include <utility>
using namespace clang;
LLVM_INSTANTIATE_REGISTRY(ParsedAttrInfoRegistry)
IdentifierLoc *IdentifierLoc::create(ASTContext &Ctx, SourceLocation Loc,
IdentifierInfo *Ident) {
IdentifierLoc *Result = new (Ctx) IdentifierLoc;
Result->Loc = Loc;
Result->Ident = Ident;
return Result;
}
size_t ParsedAttr::allocated_size() const {
if (IsAvailability) return AttributeFactory::AvailabilityAllocSize;
else if (IsTypeTagForDatatype)
return AttributeFactory::TypeTagForDatatypeAllocSize;
else if (IsProperty)
return AttributeFactory::PropertyAllocSize;
else if (HasParsedType)
return totalSizeToAlloc<ArgsUnion, detail::AvailabilityData,
detail::TypeTagForDatatypeData, ParsedType,
detail::PropertyData>(0, 0, 0, 1, 0);
return totalSizeToAlloc<ArgsUnion, detail::AvailabilityData,
detail::TypeTagForDatatypeData, ParsedType,
detail::PropertyData>(NumArgs, 0, 0, 0, 0);
}
AttributeFactory::AttributeFactory() {
FreeLists.resize(InlineFreeListsCapacity);
}
AttributeFactory::~AttributeFactory() = default;
static size_t getFreeListIndexForSize(size_t size) {
assert(size >= sizeof(ParsedAttr));
assert((size % sizeof(void*)) == 0);
return ((size - sizeof(ParsedAttr)) / sizeof(void *));
}
void *AttributeFactory::allocate(size_t size) {
size_t index = getFreeListIndexForSize(size);
if (index < FreeLists.size() && !FreeLists[index].empty()) {
ParsedAttr *attr = FreeLists[index].back();
FreeLists[index].pop_back();
return attr;
}
return Alloc.Allocate(size, alignof(AttributeFactory));
}
void AttributeFactory::deallocate(ParsedAttr *Attr) {
size_t size = Attr->allocated_size();
size_t freeListIndex = getFreeListIndexForSize(size);
if (freeListIndex >= FreeLists.size())
FreeLists.resize(freeListIndex + 1);
#ifndef NDEBUG
memset(Attr, 0, size);
#endif
FreeLists[freeListIndex].push_back(Attr);
}
void AttributeFactory::reclaimPool(AttributePool &cur) {
for (ParsedAttr *AL : cur.Attrs)
deallocate(AL);
}
void AttributePool::takePool(AttributePool &pool) {
Attrs.insert(Attrs.end(), pool.Attrs.begin(), pool.Attrs.end());
pool.Attrs.clear();
}
namespace {
#include "clang/Sema/AttrParsedAttrImpl.inc"
}
const ParsedAttrInfo &ParsedAttrInfo::get(const AttributeCommonInfo &A) {
if ((size_t)A.getParsedKind() < llvm::array_lengthof(AttrInfoMap))
return *AttrInfoMap[A.getParsedKind()];
static const ParsedAttrInfo IgnoredParsedAttrInfo(
AttributeCommonInfo::IgnoredAttribute);
if (A.getParsedKind() == AttributeCommonInfo::IgnoredAttribute)
return IgnoredParsedAttrInfo;
static llvm::ManagedStatic<std::list<std::unique_ptr<ParsedAttrInfo>>>
PluginAttrInstances;
if (PluginAttrInstances->empty())
for (auto It : ParsedAttrInfoRegistry::entries())
PluginAttrInstances->emplace_back(It.instantiate());
std::string FullName = A.getNormalizedFullName();
AttributeCommonInfo::Syntax SyntaxUsed = A.getSyntax();
if (SyntaxUsed == AttributeCommonInfo::AS_ContextSensitiveKeyword)
SyntaxUsed = AttributeCommonInfo::AS_Keyword;
for (auto &Ptr : *PluginAttrInstances)
for (auto &S : Ptr->Spellings)
if (S.Syntax == SyntaxUsed && S.NormalizedFullName == FullName)
return *Ptr;
static const ParsedAttrInfo DefaultParsedAttrInfo(
AttributeCommonInfo::UnknownAttribute);
return DefaultParsedAttrInfo;
}
ArrayRef<const ParsedAttrInfo *> ParsedAttrInfo::getAllBuiltin() {
return llvm::makeArrayRef(AttrInfoMap);
}
unsigned ParsedAttr::getMinArgs() const { return getInfo().NumArgs; }
unsigned ParsedAttr::getMaxArgs() const {
return getMinArgs() + getInfo().OptArgs;
}
unsigned ParsedAttr::getNumArgMembers() const {
return getInfo().NumArgMembers;
}
bool ParsedAttr::hasCustomParsing() const {
return getInfo().HasCustomParsing;
}
bool ParsedAttr::diagnoseAppertainsTo(Sema &S, const Decl *D) const {
return getInfo().diagAppertainsToDecl(S, *this, D);
}
bool ParsedAttr::diagnoseAppertainsTo(Sema &S, const Stmt *St) const {
return getInfo().diagAppertainsToStmt(S, *this, St);
}
bool ParsedAttr::diagnoseMutualExclusion(Sema &S, const Decl *D) const {
return getInfo().diagMutualExclusion(S, *this, D);
}
bool ParsedAttr::appliesToDecl(const Decl *D,
attr::SubjectMatchRule MatchRule) const {
return checkAttributeMatchRuleAppliesTo(D, MatchRule);
}
void ParsedAttr::getMatchRules(
const LangOptions &LangOpts,
SmallVectorImpl<std::pair<attr::SubjectMatchRule, bool>> &MatchRules)
const {
return getInfo().getPragmaAttributeMatchRules(MatchRules, LangOpts);
}
bool ParsedAttr::diagnoseLangOpts(Sema &S) const {
if (getInfo().acceptsLangOpts(S.getLangOpts()))
return true;
S.Diag(getLoc(), diag::warn_attribute_ignored) << *this;
return false;
}
bool ParsedAttr::isTargetSpecificAttr() const {
return getInfo().IsTargetSpecific;
}
bool ParsedAttr::isTypeAttr() const { return getInfo().IsType; }
bool ParsedAttr::isStmtAttr() const { return getInfo().IsStmt; }
bool ParsedAttr::existsInTarget(const TargetInfo &Target) const {
return getInfo().existsInTarget(Target);
}
bool ParsedAttr::isKnownToGCC() const { return getInfo().IsKnownToGCC; }
bool ParsedAttr::isSupportedByPragmaAttribute() const {
return getInfo().IsSupportedByPragmaAttribute;
}
bool ParsedAttr::slidesFromDeclToDeclSpecLegacyBehavior() const {
assert(isStandardAttributeSyntax());
switch (getParsedKind()) {
case AT_AddressSpace:
case AT_OpenCLPrivateAddressSpace:
case AT_OpenCLGlobalAddressSpace:
case AT_OpenCLGlobalDeviceAddressSpace:
case AT_OpenCLGlobalHostAddressSpace:
case AT_OpenCLLocalAddressSpace:
case AT_OpenCLConstantAddressSpace:
case AT_OpenCLGenericAddressSpace:
case AT_NeonPolyVectorType:
case AT_NeonVectorType:
case AT_ArmMveStrictPolymorphism:
case AT_BTFTypeTag:
case AT_ObjCGC:
case AT_MatrixType:
return true;
default:
return false;
}
}
bool ParsedAttr::acceptsExprPack() const { return getInfo().AcceptsExprPack; }
unsigned ParsedAttr::getSemanticSpelling() const {
return getInfo().spellingIndexToSemanticSpelling(*this);
}
bool ParsedAttr::hasVariadicArg() const {
return getInfo().OptArgs == 15;
}
bool ParsedAttr::isParamExpr(size_t N) const {
return getInfo().isParamExpr(N);
}
void ParsedAttr::handleAttrWithDelayedArgs(Sema &S, Decl *D) const {
::handleAttrWithDelayedArgs(S, D, *this);
}
static unsigned getNumAttributeArgs(const ParsedAttr &AL) {
return AL.getNumArgs() + AL.hasParsedType();
}
template <typename Compare>
static bool checkAttributeNumArgsImpl(Sema &S, const ParsedAttr &AL,
unsigned Num, unsigned Diag,
Compare Comp) {
if (Comp(getNumAttributeArgs(AL), Num)) {
S.Diag(AL.getLoc(), Diag) << AL << Num;
return false;
}
return true;
}
bool ParsedAttr::checkExactlyNumArgs(Sema &S, unsigned Num) const {
return checkAttributeNumArgsImpl(S, *this, Num,
diag::err_attribute_wrong_number_arguments,
std::not_equal_to<unsigned>());
}
bool ParsedAttr::checkAtLeastNumArgs(Sema &S, unsigned Num) const {
return checkAttributeNumArgsImpl(S, *this, Num,
diag::err_attribute_too_few_arguments,
std::less<unsigned>());
}
bool ParsedAttr::checkAtMostNumArgs(Sema &S, unsigned Num) const {
return checkAttributeNumArgsImpl(S, *this, Num,
diag::err_attribute_too_many_arguments,
std::greater<unsigned>());
}
void clang::takeAndConcatenateAttrs(ParsedAttributes &First,
ParsedAttributes &Second,
ParsedAttributes &Result) {
Result.takeAllFrom(Second);
Result.takeAllFrom(First);
if (First.Range.getBegin().isValid())
Result.Range.setBegin(First.Range.getBegin());
else
Result.Range.setBegin(Second.Range.getBegin());
if (Second.Range.getEnd().isValid())
Result.Range.setEnd(Second.Range.getEnd());
else
Result.Range.setEnd(First.Range.getEnd());
}